Avoid use of gohacks.SliceHeader to access pointer to data

Accessing a pointer to the data in a slice can be achieved with
`unsafe.Pointer(&slice[0])`.

f051ec6463 motivated using gohacks.SliceHeader in
this way with "we often use SliceHeader to extract pointers from slices in a
way that avoids bounds checking and/or handles nil slices correctly", but this
no longer seems to be the case. None of the remaining uses are obviously
performance sensitive or necessarily include bounds checks, nor get used with
nil slices.

This brings us one step closer to removing gohacks.SliceHeader, which is one
less internal detail to keep in sync with Go.

For #8422

PiperOrigin-RevId: 504408578
This commit is contained in:
Michael Pratt
2023-01-24 16:42:27 -08:00
committed by gVisor bot
parent 1e4d19665c
commit 8604e1c200
7 changed files with 6 additions and 16 deletions
-1
View File
@@ -7,7 +7,6 @@ go_library(
srcs = ["fdchannel_unsafe.go"],
visibility = ["//visibility:public"],
deps = [
"//pkg/gohacks",
"@org_golang_x_sys//unix:go_default_library",
],
)
+2 -4
View File
@@ -24,7 +24,6 @@ import (
"unsafe"
"golang.org/x/sys/unix"
"gvisor.dev/gvisor/pkg/gohacks"
)
// int32 is the real type of a file descriptor.
@@ -55,10 +54,9 @@ 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, unix.CmsgSpace(sizeofInt32))
cmsgSliceHdr := (*gohacks.SliceHeader)(unsafe.Pointer(&cmsgSlice))
ep.sockfd = int32(sockfd)
ep.msghdr.Control = (*byte)(cmsgSliceHdr.Data)
ep.cmsg = (*unix.Cmsghdr)(cmsgSliceHdr.Data)
ep.msghdr.Control = (*byte)(unsafe.Pointer(&cmsgSlice[0]))
ep.cmsg = (*unix.Cmsghdr)(unsafe.Pointer(&cmsgSlice[0]))
// ep.msghdr.Controllen and ep.cmsg.* are mutated by recvmsg(2), so they're
// set before calling sendmsg/recvmsg.
}
+1 -3
View File
@@ -33,13 +33,11 @@ func snapshotDistribution(samples []atomicbitops.Uint64) []uint64 {
// no race condition from getting the number of buckets upfront.
numBuckets := len(samples)
snapshot := make([]uint64, numBuckets)
samplesHeader := (*gohacks.SliceHeader)(unsafe.Pointer(&samples))
snapshotHeader := (*gohacks.SliceHeader)(unsafe.Pointer(&snapshot))
if sync.RaceEnabled {
// runtime.RaceDisable() doesn't actually stop the race detector, so it
// can't help us here. Instead, call runtime.memmove directly, which is
// not instrumented by the race detector.
gohacks.Memmove(snapshotHeader.Data, samplesHeader.Data, unsafe.Sizeof(uint64(0))*uintptr(numBuckets))
gohacks.Memmove(unsafe.Pointer(&snapshot[0]), unsafe.Pointer(&samples[0]), unsafe.Sizeof(uint64(0))*uintptr(numBuckets))
} else {
for i := range samples {
snapshot[i] = samples[i].RacyLoad()
-1
View File
@@ -16,7 +16,6 @@ go_library(
"//pkg/atomicbitops",
"//pkg/context",
"//pkg/errors/linuxerr",
"//pkg/gohacks",
"//pkg/hostarch",
"//pkg/safemem",
"//pkg/sentry/kernel",
@@ -19,7 +19,6 @@ import (
"unsafe"
"gvisor.dev/gvisor/pkg/atomicbitops"
"gvisor.dev/gvisor/pkg/gohacks"
)
func atomicUint32AtOffset(buf []byte, offset int) *atomicbitops.Uint32 {
@@ -30,6 +29,5 @@ func atomicUint32AtOffset(buf []byte, offset int) *atomicbitops.Uint32 {
if offset%sizeOfUint32 != 0 {
panic(fmt.Sprintf("cast at offset %d would produce unaligned pointer", offset))
}
hdr := (*gohacks.SliceHeader)(unsafe.Pointer(&buf))
return (*atomicbitops.Uint32)(unsafe.Add(hdr.Data, offset))
return (*atomicbitops.Uint32)(unsafe.Pointer(&buf[offset]))
}
+1 -2
View File
@@ -153,8 +153,7 @@ func (mt *mountTable) Init() {
func newMountTableSlots(cap uintptr) unsafe.Pointer {
slice := make([]mountSlot, cap, cap)
hdr := (*gohacks.SliceHeader)(unsafe.Pointer(&slice))
return hdr.Data
return unsafe.Pointer(&slice[0])
}
// Lookup returns the Mount with the given parent, mounted at the given point.
@@ -371,8 +371,7 @@ func (shard *apmShard) rehash(oldSlots unsafe.Pointer) {
// Allocate the new table.
newSlotsSlice := make([]apmSlot, newSize)
newSlotsHeader := (*gohacks.SliceHeader)(unsafe.Pointer(&newSlotsSlice))
newSlots := newSlotsHeader.Data
newSlots := unsafe.Pointer(&newSlotsSlice[0])
newMask := newSize - 1
// Start a writer critical section now so that racing users of the old