aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/osutil/sharedmem_memfd.go
blob: bdcea486f61c572ae955c95c935af3e72ed924a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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()
}