From a9441aea2780da8c93da1c73da860219f98438de Mon Sep 17 00:00:00 2001 From: Ayush Ranjan Date: Wed, 3 Mar 2021 10:23:55 -0800 Subject: [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 --- pkg/sentry/pgalloc/BUILD | 1 + pkg/sentry/pgalloc/pgalloc.go | 30 +++++++++++++++--------------- pkg/sentry/pgalloc/pgalloc_unsafe.go | 7 ++++--- pkg/sentry/pgalloc/save_restore.go | 4 ++-- 4 files changed, 22 insertions(+), 20 deletions(-) (limited to 'pkg/sentry/pgalloc') diff --git a/pkg/sentry/pgalloc/BUILD b/pkg/sentry/pgalloc/BUILD index 5b09b9feb..e5bf13c40 100644 --- a/pkg/sentry/pgalloc/BUILD +++ b/pkg/sentry/pgalloc/BUILD @@ -97,6 +97,7 @@ go_library( "//pkg/sync", "//pkg/syserror", "//pkg/usermem", + "@org_golang_x_sys//unix:go_default_library", ], ) diff --git a/pkg/sentry/pgalloc/pgalloc.go b/pkg/sentry/pgalloc/pgalloc.go index d99be7f46..58cc11a13 100644 --- a/pkg/sentry/pgalloc/pgalloc.go +++ b/pkg/sentry/pgalloc/pgalloc.go @@ -26,9 +26,9 @@ import ( "math" "os" "sync/atomic" - "syscall" "time" + "golang.org/x/sys/unix" "gvisor.dev/gvisor/pkg/abi/linux" "gvisor.dev/gvisor/pkg/context" "gvisor.dev/gvisor/pkg/log" @@ -341,12 +341,12 @@ func NewMemoryFile(file *os.File, opts MemoryFileOpts) (*MemoryFile, error) { // Work around IMA by immediately creating a temporary PROT_EXEC mapping, // while the backing file is still small. IMA will ignore any future // mappings. - m, _, errno := syscall.Syscall6( - syscall.SYS_MMAP, + m, _, errno := unix.Syscall6( + unix.SYS_MMAP, 0, usermem.PageSize, - syscall.PROT_EXEC, - syscall.MAP_SHARED, + unix.PROT_EXEC, + unix.MAP_SHARED, file.Fd(), 0) if errno != 0 { @@ -354,8 +354,8 @@ func NewMemoryFile(file *os.File, opts MemoryFileOpts) (*MemoryFile, error) { // don't return it. log.Warningf("Failed to pre-map MemoryFile PROT_EXEC: %v", errno) } else { - if _, _, errno := syscall.Syscall( - syscall.SYS_MUNMAP, + if _, _, errno := unix.Syscall( + unix.SYS_MUNMAP, m, usermem.PageSize, 0); errno != 0 { @@ -584,7 +584,7 @@ func (f *MemoryFile) decommitFile(fr memmap.FileRange) error { // "After a successful call, subsequent reads from this range will // return zeroes. The FALLOC_FL_PUNCH_HOLE flag must be ORed with // FALLOC_FL_KEEP_SIZE in mode ..." - fallocate(2) - return syscall.Fallocate( + return unix.Fallocate( int(f.file.Fd()), _FALLOC_FL_PUNCH_HOLE|_FALLOC_FL_KEEP_SIZE, int64(fr.Start), @@ -730,12 +730,12 @@ func (f *MemoryFile) getChunkMapping(chunk int) ([]uintptr, uintptr, error) { if m := mappings[chunk]; m != 0 { return mappings, m, nil } - m, _, errno := syscall.Syscall6( - syscall.SYS_MMAP, + m, _, errno := unix.Syscall6( + unix.SYS_MMAP, 0, chunkSize, - syscall.PROT_READ|syscall.PROT_WRITE, - syscall.MAP_SHARED, + unix.PROT_READ|unix.PROT_WRITE, + unix.MAP_SHARED, f.file.Fd(), uintptr(chunk<