summaryrefslogtreecommitdiffhomepage
path: root/pkg/safemem
diff options
context:
space:
mode:
authorJamie Liu <jamieliu@google.com>2021-02-17 17:39:24 -0800
committergVisor bot <gvisor-bot@google.com>2021-02-17 17:41:10 -0800
commitf051ec64639b83faabcfe766ff078072def3c2aa (patch)
tree892c0fc2f4fd4138299c3c0d5a5836a940a670b5 /pkg/safemem
parent4bc7daf91a0d9102fa477b199964e7db45066da1 (diff)
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
Diffstat (limited to 'pkg/safemem')
-rw-r--r--pkg/safemem/BUILD5
-rw-r--r--pkg/safemem/block_unsafe.go13
-rw-r--r--pkg/safemem/seq_unsafe.go7
3 files changed, 14 insertions, 11 deletions
diff --git a/pkg/safemem/BUILD b/pkg/safemem/BUILD
index 68ed074f8..d3b9b0ca9 100644
--- a/pkg/safemem/BUILD
+++ b/pkg/safemem/BUILD
@@ -11,7 +11,10 @@ go_library(
"seq_unsafe.go",
],
visibility = ["//:sandbox"],
- deps = ["//pkg/safecopy"],
+ deps = [
+ "//pkg/gohacks",
+ "//pkg/safecopy",
+ ],
)
go_test(
diff --git a/pkg/safemem/block_unsafe.go b/pkg/safemem/block_unsafe.go
index 7857f5853..93879bb4f 100644
--- a/pkg/safemem/block_unsafe.go
+++ b/pkg/safemem/block_unsafe.go
@@ -16,9 +16,9 @@ package safemem
import (
"fmt"
- "reflect"
"unsafe"
+ "gvisor.dev/gvisor/pkg/gohacks"
"gvisor.dev/gvisor/pkg/safecopy"
)
@@ -148,12 +148,11 @@ func (b Block) TakeFirst64(n uint64) Block {
// ToSlice returns a []byte equivalent to b.
func (b Block) ToSlice() []byte {
- var bs []byte
- hdr := (*reflect.SliceHeader)(unsafe.Pointer(&bs))
- hdr.Data = uintptr(b.start)
- hdr.Len = b.length
- hdr.Cap = b.length
- return bs
+ return *(*[]byte)(unsafe.Pointer(&gohacks.SliceHeader{
+ Data: b.start,
+ Len: b.length,
+ Cap: b.length,
+ }))
}
// Addr returns b's start address as a uintptr. It returns uintptr instead of
diff --git a/pkg/safemem/seq_unsafe.go b/pkg/safemem/seq_unsafe.go
index fc4049eeb..b315b0e5a 100644
--- a/pkg/safemem/seq_unsafe.go
+++ b/pkg/safemem/seq_unsafe.go
@@ -17,9 +17,10 @@ package safemem
import (
"bytes"
"fmt"
- "reflect"
"syscall"
"unsafe"
+
+ "gvisor.dev/gvisor/pkg/gohacks"
)
// A BlockSeq represents a sequence of Blocks, each of which has non-zero
@@ -184,8 +185,8 @@ func (bs BlockSeq) Tail() BlockSeq {
return BlockSeq{}
}
var extSlice []Block
- extSliceHdr := (*reflect.SliceHeader)(unsafe.Pointer(&extSlice))
- extSliceHdr.Data = uintptr(bs.data)
+ extSliceHdr := (*gohacks.SliceHeader)(unsafe.Pointer(&extSlice))
+ extSliceHdr.Data = bs.data
extSliceHdr.Len = bs.length
extSliceHdr.Cap = bs.length
tailSlice := skipEmpty(extSlice[1:])