github.com/google/syzkaller@v0.0.0-20251211124644-a066d2bc4b02/executor/shmem.h (about) 1 // Copyright 2024 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 #include <fcntl.h> 5 #include <stddef.h> 6 #include <stdlib.h> 7 #include <sys/mman.h> 8 #include <unistd.h> 9 10 // ShmemFile is shared memory region wrapper. 11 class ShmemFile 12 { 13 public: 14 // Maps shared memory region of size 'size' from a new temp file. 15 ShmemFile(size_t size) 16 { 17 char file_name[] = "syz.XXXXXX"; 18 fd_ = mkstemp(file_name); 19 if (fd_ == -1) 20 failmsg("shmem open failed", "file=%s", file_name); 21 // OpenBSD has neither fallocate nor posix_fallocate. 22 if (ftruncate(fd_, size)) 23 failmsg("shmem ftruncate failed", "size=%zu", size); 24 Mmap(fd_, nullptr, size, true); 25 if (unlink(file_name)) 26 fail("shmem unlink failed"); 27 } 28 29 // Maps shared memory region from the file 'fd' in read/write or write-only mode, 30 // preferably at the address 'preferred'. 31 ShmemFile(int fd, void* preferred, size_t size, bool write) 32 { 33 Mmap(fd, preferred, size, write); 34 } 35 36 ~ShmemFile() 37 { 38 if (munmap(mem_, size_)) 39 fail("shmem munmap failed"); 40 if (fd_ != -1) 41 close(fd_); 42 } 43 44 // Prevents any future modifications to the region. 45 void Seal() 46 { 47 if (mprotect(mem_, size_, PROT_READ)) 48 fail("shmem mprotect failed"); 49 if (fd_ != -1) 50 close(fd_); 51 fd_ = -1; 52 } 53 54 int FD() const 55 { 56 return fd_; 57 } 58 59 void* Mem() const 60 { 61 return mem_; 62 } 63 64 private: 65 void* mem_ = nullptr; 66 size_t size_ = 0; 67 int fd_ = -1; 68 69 void Mmap(int fd, void* preferred, size_t size, bool write) 70 { 71 size_ = size; 72 mem_ = mmap(preferred, size, PROT_READ | (write ? PROT_WRITE : 0), MAP_SHARED, fd, 0); 73 if (mem_ == MAP_FAILED) 74 failmsg("shmem mmap failed", "size=%zu", size); 75 } 76 77 ShmemFile(const ShmemFile&) = delete; 78 ShmemFile& operator=(const ShmemFile&) = delete; 79 };