diff options
author | Ayush Ranjan <ayushranjan@google.com> | 2021-03-03 10:23:55 -0800 |
---|---|---|
committer | gVisor bot <gvisor-bot@google.com> | 2021-03-03 10:25:58 -0800 |
commit | a9441aea2780da8c93da1c73da860219f98438de (patch) | |
tree | 8b12915756f5bfb926218214cd7bc0b3281605fd /pkg/safecopy | |
parent | b8a5420f49a2afd622ec08b5019e1bf537f7da82 (diff) |
[op] Replace syscall package usage with golang.org/x/sys/unix in pkg/.
The syscall package has been deprecated in favor of golang.org/x/sys.
Note that syscall is still used in the following places:
- pkg/sentry/socket/hostinet/stack.go: some netlink related functionalities
are not yet available in golang.org/x/sys.
- syscall.Stat_t is still used in some places because os.FileInfo.Sys() still
returns it and not unix.Stat_t.
Updates #214
PiperOrigin-RevId: 360701387
Diffstat (limited to 'pkg/safecopy')
-rw-r--r-- | pkg/safecopy/BUILD | 6 | ||||
-rw-r--r-- | pkg/safecopy/safecopy.go | 10 | ||||
-rw-r--r-- | pkg/safecopy/safecopy_test.go | 21 | ||||
-rw-r--r-- | pkg/safecopy/safecopy_unsafe.go | 13 |
4 files changed, 28 insertions, 22 deletions
diff --git a/pkg/safecopy/BUILD b/pkg/safecopy/BUILD index 426ef30c9..b77c40279 100644 --- a/pkg/safecopy/BUILD +++ b/pkg/safecopy/BUILD @@ -17,7 +17,10 @@ go_library( "sighandler_arm64.s", ], visibility = ["//:sandbox"], - deps = ["//pkg/syserror"], + deps = [ + "//pkg/syserror", + "@org_golang_x_sys//unix:go_default_library", + ], ) go_test( @@ -26,4 +29,5 @@ go_test( "safecopy_test.go", ], library = ":safecopy", + deps = ["@org_golang_x_sys//unix:go_default_library"], ) diff --git a/pkg/safecopy/safecopy.go b/pkg/safecopy/safecopy.go index 2fb7e5809..1e0af5889 100644 --- a/pkg/safecopy/safecopy.go +++ b/pkg/safecopy/safecopy.go @@ -20,8 +20,8 @@ import ( "fmt" "reflect" "runtime" - "syscall" + "golang.org/x/sys/unix" "gvisor.dev/gvisor/pkg/syserror" ) @@ -127,16 +127,16 @@ func initializeAddresses() { func init() { initializeAddresses() - if err := ReplaceSignalHandler(syscall.SIGSEGV, reflect.ValueOf(signalHandler).Pointer(), &savedSigSegVHandler); err != nil { + if err := ReplaceSignalHandler(unix.SIGSEGV, reflect.ValueOf(signalHandler).Pointer(), &savedSigSegVHandler); err != nil { panic(fmt.Sprintf("Unable to set handler for SIGSEGV: %v", err)) } - if err := ReplaceSignalHandler(syscall.SIGBUS, reflect.ValueOf(signalHandler).Pointer(), &savedSigBusHandler); err != nil { + if err := ReplaceSignalHandler(unix.SIGBUS, reflect.ValueOf(signalHandler).Pointer(), &savedSigBusHandler); err != nil { panic(fmt.Sprintf("Unable to set handler for SIGBUS: %v", err)) } - syserror.AddErrorUnwrapper(func(e error) (syscall.Errno, bool) { + syserror.AddErrorUnwrapper(func(e error) (unix.Errno, bool) { switch e.(type) { case SegvError, BusError, AlignmentError: - return syscall.EFAULT, true + return unix.EFAULT, true default: return 0, false } diff --git a/pkg/safecopy/safecopy_test.go b/pkg/safecopy/safecopy_test.go index 7f7f69d61..d2ce8ff86 100644 --- a/pkg/safecopy/safecopy_test.go +++ b/pkg/safecopy/safecopy_test.go @@ -21,9 +21,10 @@ import ( "math/rand" "os" "runtime/debug" - "syscall" "testing" "unsafe" + + "golang.org/x/sys/unix" ) // Size of a page in bytes. Cloned from usermem.PageSize to avoid a circular @@ -224,12 +225,12 @@ func TestCompareAndSwapUint32AlignmentError(t *testing.T) { // withSegvErrorTestMapping calls fn with a two-page mapping. The first page // contains random data, and the second page generates SIGSEGV when accessed. func withSegvErrorTestMapping(t *testing.T, fn func(m []byte)) { - mapping, err := syscall.Mmap(-1, 0, 2*pageSize, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_ANONYMOUS|syscall.MAP_PRIVATE) + mapping, err := unix.Mmap(-1, 0, 2*pageSize, unix.PROT_READ|unix.PROT_WRITE, unix.MAP_ANONYMOUS|unix.MAP_PRIVATE) if err != nil { t.Fatalf("Mmap failed: %v", err) } - defer syscall.Munmap(mapping) - if err := syscall.Mprotect(mapping[pageSize:], syscall.PROT_NONE); err != nil { + defer unix.Munmap(mapping) + if err := unix.Mprotect(mapping[pageSize:], unix.PROT_NONE); err != nil { t.Fatalf("Mprotect failed: %v", err) } initRandom(mapping[:pageSize]) @@ -248,11 +249,11 @@ func withBusErrorTestMapping(t *testing.T, fn func(m []byte)) { if err := f.Truncate(pageSize); err != nil { t.Fatalf("Truncate failed: %v", err) } - mapping, err := syscall.Mmap(int(f.Fd()), 0, 2*pageSize, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED) + mapping, err := unix.Mmap(int(f.Fd()), 0, 2*pageSize, unix.PROT_READ|unix.PROT_WRITE, unix.MAP_SHARED) if err != nil { t.Fatalf("Mmap failed: %v", err) } - defer syscall.Munmap(mapping) + defer unix.Munmap(mapping) initRandom(mapping[:pageSize]) fn(mapping) @@ -583,12 +584,12 @@ func TestSegVOnMemmove(t *testing.T) { // Test that SIGSEGVs received by runtime.memmove when *not* doing // CopyIn or CopyOut work gets propagated to the runtime. const bufLen = pageSize - a, err := syscall.Mmap(-1, 0, bufLen, syscall.PROT_NONE, syscall.MAP_ANON|syscall.MAP_PRIVATE) + a, err := unix.Mmap(-1, 0, bufLen, unix.PROT_NONE, unix.MAP_ANON|unix.MAP_PRIVATE) if err != nil { t.Fatalf("Mmap failed: %v", err) } - defer syscall.Munmap(a) + defer unix.Munmap(a) b := randBuf(bufLen) if !testCopy(b, a) { @@ -611,12 +612,12 @@ func TestSigbusOnMemmove(t *testing.T) { os.Remove(f.Name()) defer f.Close() - a, err := syscall.Mmap(int(f.Fd()), 0, bufLen, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED) + a, err := unix.Mmap(int(f.Fd()), 0, bufLen, unix.PROT_READ|unix.PROT_WRITE, unix.MAP_SHARED) if err != nil { t.Fatalf("Mmap failed: %v", err) } - defer syscall.Munmap(a) + defer unix.Munmap(a) b := randBuf(bufLen) if !testCopy(b, a) { diff --git a/pkg/safecopy/safecopy_unsafe.go b/pkg/safecopy/safecopy_unsafe.go index 41dd567f3..a075cf88e 100644 --- a/pkg/safecopy/safecopy_unsafe.go +++ b/pkg/safecopy/safecopy_unsafe.go @@ -17,8 +17,9 @@ package safecopy import ( "fmt" "runtime" - "syscall" "unsafe" + + "golang.org/x/sys/unix" ) // maxRegisterSize is the maximum register size used in memcpy and memclr. It @@ -310,9 +311,9 @@ func errorFromFaultSignal(addr uintptr, sig int32) error { switch sig { case 0: return nil - case int32(syscall.SIGSEGV): + case int32(unix.SIGSEGV): return SegvError{addr} - case int32(syscall.SIGBUS): + case int32(unix.SIGBUS): return BusError{addr} default: panic(fmt.Sprintf("safecopy got unexpected signal %d at address %#x", sig, addr)) @@ -328,7 +329,7 @@ func errorFromFaultSignal(addr uintptr, sig int32) error { // handlers for appropriate signals. These handlers will call the previous // handler however, and if this is function is being used externally then the // same courtesy is expected. -func ReplaceSignalHandler(sig syscall.Signal, handler uintptr, previous *uintptr) error { +func ReplaceSignalHandler(sig unix.Signal, handler uintptr, previous *uintptr) error { var sa struct { handler uintptr flags uint64 @@ -340,7 +341,7 @@ func ReplaceSignalHandler(sig syscall.Signal, handler uintptr, previous *uintptr // Get the existing signal handler information, and save the current // handler. Once we replace it, we will use this pointer to fall back to // it when we receive other signals. - if _, _, e := syscall.RawSyscall6(syscall.SYS_RT_SIGACTION, uintptr(sig), 0, uintptr(unsafe.Pointer(&sa)), maskLen, 0, 0); e != 0 { + if _, _, e := unix.RawSyscall6(unix.SYS_RT_SIGACTION, uintptr(sig), 0, uintptr(unsafe.Pointer(&sa)), maskLen, 0, 0); e != 0 { return e } @@ -353,7 +354,7 @@ func ReplaceSignalHandler(sig syscall.Signal, handler uintptr, previous *uintptr // Install our own handler. sa.handler = handler - if _, _, e := syscall.RawSyscall6(syscall.SYS_RT_SIGACTION, uintptr(sig), uintptr(unsafe.Pointer(&sa)), 0, maskLen, 0, 0); e != 0 { + if _, _, e := unix.RawSyscall6(unix.SYS_RT_SIGACTION, uintptr(sig), uintptr(unsafe.Pointer(&sa)), 0, maskLen, 0, 0); e != 0 { return e } |