Replace unsafeSlice with unsafe.Slice.

reflect.SliceHeader has been deprecated.

PiperOrigin-RevId: 714222929
This commit is contained in:
Konstantin Bogomolov
2025-01-10 15:11:53 -08:00
committed by gVisor bot
parent 2d0e39a87f
commit 17962e58c1
4 changed files with 8 additions and 27 deletions
+1
View File
@@ -198,6 +198,7 @@ analyzers:
- pkg/sentry/platform/kvm/machine_unsafe.go # Special case.
- pkg/sentry/platform/kvm/bluepill_amd64_unsafe.go # Special case.
- pkg/sentry/platform/pgalloc/pgalloc_unsafe.go # Special case.
- pkg/sentry/platform/ptrace/stub_unsafe.go # Special case.
- pkg/sentry/platform/systrap/stub_unsafe.go # Special case.
- pkg/sentry/platform/systrap/syscall_thread_unsafe.go # Special case.
- pkg/sentry/platform/systrap/sysmsg_thread_unsafe.go # Special case.
+2 -12
View File
@@ -15,7 +15,6 @@
package ptrace
import (
"reflect"
"unsafe"
"golang.org/x/sys/unix"
@@ -36,21 +35,12 @@ func addrOfStub() uintptr
// stubCall calls the stub at the given address with the given pid.
func stubCall(addr, pid uintptr)
// unsafeSlice returns a slice for the given address and length.
func unsafeSlice(addr uintptr, length int) (slice []byte) {
sh := (*reflect.SliceHeader)(unsafe.Pointer(&slice))
sh.Data = addr
sh.Len = length
sh.Cap = length
return
}
// stubInit initializes the stub.
func stubInit() {
// Grab the existing stub.
stubBegin := addrOfStub()
stubLen := int(safecopy.FindEndAddress(stubBegin) - stubBegin)
stubSlice := unsafeSlice(stubBegin, stubLen)
stubSlice := unsafe.Slice((*byte)(unsafe.Pointer(stubBegin)), stubLen)
mapLen := uintptr(stubLen)
if offset := mapLen % hostarch.PageSize; offset != 0 {
mapLen += hostarch.PageSize - offset
@@ -82,7 +72,7 @@ func stubInit() {
}
// Copy the stub to the address.
targetSlice := unsafeSlice(addr, stubLen)
targetSlice := unsafe.Slice((*byte)(unsafe.Pointer(addr)), stubLen)
copy(targetSlice, stubSlice)
// Make the stub executable.
+3 -13
View File
@@ -46,15 +46,6 @@ func addrOfInitStubProcess() uintptr
// stubCall calls the stub at the given address with the given pid.
func stubCall(addr, pid uintptr)
// unsafeSlice returns a slice for the given address and length.
func unsafeSlice(addr uintptr, length int) (slice []byte) {
sh := (*reflect.SliceHeader)(unsafe.Pointer(&slice))
sh.Data = addr
sh.Len = length
sh.Cap = length
return
}
// prepareSeccompRules compiles stub process seccomp filters and fill
// the sock_fprog structure. So the stub process will only need to call
// seccomp system call to apply these filters.
@@ -199,7 +190,7 @@ func stubInit() {
// Grab the existing stub.
procStubBegin := addrOfInitStubProcess()
procStubLen := int(safecopy.FindEndAddress(procStubBegin) - procStubBegin)
procStubSlice := unsafeSlice(procStubBegin, procStubLen)
procStubSlice := unsafe.Slice((*byte)(unsafe.Pointer(procStubBegin)), procStubLen)
mapLen, _ := hostarch.PageRoundUp(uintptr(procStubLen))
stubSysmsgStart = mapLen
@@ -281,7 +272,7 @@ func stubInit() {
stubContextRegion += uintptr(gap)
// Copy the stub to the address.
targetSlice := unsafeSlice(stubStart, procStubLen)
targetSlice := unsafe.Slice((*byte)(unsafe.Pointer(stubStart)), procStubLen)
copy(targetSlice, procStubSlice)
stubInitProcess = stubStart
@@ -299,8 +290,7 @@ func stubInit() {
}
stubSysmsgRules += stubStart
stubSyscallRules += stubStart
targetSlice = unsafeSlice(stubSysmsgStart, stubSysmsgLen)
targetSlice = unsafe.Slice((*byte)(unsafe.Pointer(stubSysmsgStart)), stubSysmsgLen)
copy(targetSlice, sysmsg.SighandlerBlob)
// Initialize stub globals
@@ -81,7 +81,7 @@ func mmapContextQueueForSentry(memoryFile *pgalloc.MemoryFile, opts pgalloc.Allo
func saveFPState(ctx *sharedContext, ac *arch.Context64) {
fpState := ac.FloatingPointData().BytePointer()
dst := unsafeSlice(uintptr(unsafe.Pointer(fpState)), archState.FpLen())
dst := unsafe.Slice(fpState, archState.FpLen())
src := ctx.shared.FPState[:]
copy(dst, src)
}
@@ -96,7 +96,7 @@ func restoreFPState(ctx *sharedContext, c *platformContext, ac *arch.Context64)
ctx.setFPStateChanged()
fpState := ac.FloatingPointData().BytePointer()
src := unsafeSlice(uintptr(unsafe.Pointer(fpState)), archState.FpLen())
src := unsafe.Slice(fpState, archState.FpLen())
dst := ctx.shared.FPState[:]
copy(dst, src)
}