From f051ec64639b83faabcfe766ff078072def3c2aa Mon Sep 17 00:00:00 2001 From: Jamie Liu Date: Wed, 17 Feb 2021 17:39:24 -0800 Subject: Add gohacks.Slice/StringHeader. See https://github.com/golang/go/issues/19367 for rationale. Note that the upstream decision arrived at in that thread, while useful for some of our use cases, doesn't account for all of our SliceHeader use cases (we often use SliceHeader to extract pointers from slices in a way that avoids bounds checking and/or handles nil slices correctly) and also doesn't exist yet. PiperOrigin-RevId: 358071574 --- pkg/sentry/arch/stack_unsafe.go | 23 ++++------------------- 1 file changed, 4 insertions(+), 19 deletions(-) (limited to 'pkg/sentry/arch/stack_unsafe.go') diff --git a/pkg/sentry/arch/stack_unsafe.go b/pkg/sentry/arch/stack_unsafe.go index a90d297ee..0e478e434 100644 --- a/pkg/sentry/arch/stack_unsafe.go +++ b/pkg/sentry/arch/stack_unsafe.go @@ -15,8 +15,6 @@ package arch import ( - "reflect" - "runtime" "unsafe" "gvisor.dev/gvisor/pkg/marshal/primitive" @@ -33,35 +31,22 @@ import ( // On error, the contents of the stack and the bottom cursor are undefined. func (s *Stack) pushAddrSliceAndTerminator(src []usermem.Addr) (int, error) { // Note: Stack grows upwards, so push the terminator first. - srcHdr := (*reflect.SliceHeader)(unsafe.Pointer(&src)) switch s.Arch.Width() { case 8: nNull, err := primitive.CopyUint64Out(s, StackBottomMagic, 0) if err != nil { return 0, err } - var dst []uint64 - dstHdr := (*reflect.SliceHeader)(unsafe.Pointer(&dst)) - dstHdr.Data = srcHdr.Data - dstHdr.Len = srcHdr.Len - dstHdr.Cap = srcHdr.Cap - n, err := primitive.CopyUint64SliceOut(s, StackBottomMagic, dst) - // Ensures src doesn't get GCed until we're done using it through dst. - runtime.KeepAlive(src) + srcAsUint64 := *(*[]uint64)(unsafe.Pointer(&src)) + n, err := primitive.CopyUint64SliceOut(s, StackBottomMagic, srcAsUint64) return n + nNull, err case 4: nNull, err := primitive.CopyUint32Out(s, StackBottomMagic, 0) if err != nil { return 0, err } - var dst []uint32 - dstHdr := (*reflect.SliceHeader)(unsafe.Pointer(&dst)) - dstHdr.Data = srcHdr.Data - dstHdr.Len = srcHdr.Len - dstHdr.Cap = srcHdr.Cap - n, err := primitive.CopyUint32SliceOut(s, StackBottomMagic, dst) - // Ensure src doesn't get GCed until we're done using it through dst. - runtime.KeepAlive(src) + srcAsUint32 := *(*[]uint32)(unsafe.Pointer(&src)) + n, err := primitive.CopyUint32SliceOut(s, StackBottomMagic, srcAsUint32) return n + nNull, err default: panic("Unsupported arch width") -- cgit v1.2.3