aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/osutil/sharedmem_memfd.go
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/osutil/sharedmem_memfd.go')
-rw-r--r--pkg/osutil/sharedmem_memfd.go30
1 files changed, 30 insertions, 0 deletions
diff --git a/pkg/osutil/sharedmem_memfd.go b/pkg/osutil/sharedmem_memfd.go
new file mode 100644
index 000000000..bdcea486f
--- /dev/null
+++ b/pkg/osutil/sharedmem_memfd.go
@@ -0,0 +1,30 @@
+// Copyright 2021 syzkaller project authors. All rights reserved.
+// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
+
+//go:build linux
+// +build linux
+
+package osutil
+
+import (
+ "fmt"
+ "os"
+
+ "golang.org/x/sys/unix"
+)
+
+// In the case of Linux, we can just use the memfd_create syscall.
+func CreateSharedMemFile(size int) (f *os.File, err error) {
+ // The name is actually irrelevant and can even be the same for all such files.
+ fd, err := unix.MemfdCreate("syz-shared-mem", 0)
+ if err != nil {
+ err = fmt.Errorf("failed to do memfd_create: %v", err)
+ return
+ }
+ f = os.NewFile(uintptr(fd), fmt.Sprintf("/proc/self/fd/%d", fd))
+ return
+}
+
+func CloseSharedMemFile(f *os.File) error {
+ return f.Close()
+}