github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/pkg/osutil/sharedmem_memfd.go (about)

     1  // Copyright 2021 syzkaller project authors. All rights reserved.
     2  // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
     3  
     4  //go:build linux
     5  
     6  package osutil
     7  
     8  import (
     9  	"fmt"
    10  	"os"
    11  
    12  	"golang.org/x/sys/unix"
    13  )
    14  
    15  // In the case of Linux, we can just use the memfd_create syscall.
    16  func CreateSharedMemFile(size int) (f *os.File, err error) {
    17  	// The name is actually irrelevant and can even be the same for all such files.
    18  	fd, err := unix.MemfdCreate("syz-shared-mem", unix.MFD_CLOEXEC)
    19  	if err != nil {
    20  		err = fmt.Errorf("failed to do memfd_create: %w", err)
    21  		return
    22  	}
    23  	f = os.NewFile(uintptr(fd), fmt.Sprintf("/proc/self/fd/%d", fd))
    24  	return
    25  }
    26  
    27  func CloseSharedMemFile(f *os.File) error {
    28  	return f.Close()
    29  }