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/fdchannel/BUILD | 7 ++++++- pkg/fdchannel/fdchannel_unsafe.go | 9 +++++---- 2 files changed, 11 insertions(+), 5 deletions(-) (limited to 'pkg/fdchannel') diff --git a/pkg/fdchannel/BUILD b/pkg/fdchannel/BUILD index d9104ef02..0b34cef03 100644 --- a/pkg/fdchannel/BUILD +++ b/pkg/fdchannel/BUILD @@ -6,6 +6,9 @@ go_library( name = "fdchannel", srcs = ["fdchannel_unsafe.go"], visibility = ["//visibility:public"], + deps = [ + "//pkg/gohacks", + ], ) go_test( @@ -13,5 +16,7 @@ go_test( size = "small", srcs = ["fdchannel_test.go"], library = ":fdchannel", - deps = ["//pkg/sync"], + deps = [ + "//pkg/sync", + ], ) diff --git a/pkg/fdchannel/fdchannel_unsafe.go b/pkg/fdchannel/fdchannel_unsafe.go index b253a8fdd..0ebdedf26 100644 --- a/pkg/fdchannel/fdchannel_unsafe.go +++ b/pkg/fdchannel/fdchannel_unsafe.go @@ -20,9 +20,10 @@ package fdchannel import ( "fmt" - "reflect" "syscall" "unsafe" + + "gvisor.dev/gvisor/pkg/gohacks" ) // int32 is the real type of a file descriptor. @@ -53,10 +54,10 @@ func (ep *Endpoint) Init(sockfd int) { // sendmsg+recvmsg for a zero-length datagram is slightly faster than // sendmsg+recvmsg for a single byte over a stream socket. cmsgSlice := make([]byte, syscall.CmsgSpace(sizeofInt32)) - cmsgReflect := (*reflect.SliceHeader)(unsafe.Pointer(&cmsgSlice)) + cmsgSliceHdr := (*gohacks.SliceHeader)(unsafe.Pointer(&cmsgSlice)) ep.sockfd = int32(sockfd) - ep.msghdr.Control = (*byte)(unsafe.Pointer(cmsgReflect.Data)) - ep.cmsg = (*syscall.Cmsghdr)(unsafe.Pointer(cmsgReflect.Data)) + ep.msghdr.Control = (*byte)(cmsgSliceHdr.Data) + ep.cmsg = (*syscall.Cmsghdr)(cmsgSliceHdr.Data) // ep.msghdr.Controllen and ep.cmsg.* are mutated by recvmsg(2), so they're // set before calling sendmsg/recvmsg. } -- cgit v1.2.3