aboutsummaryrefslogtreecommitdiffstats
path: root/executor/shmem.h
blob: ab9d173006a656913213b986b0d67686f43cbdd8 (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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright 2024 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.

#include <fcntl.h>
#include <stddef.h>
#include <sys/mman.h>
#include <unistd.h>

// ShmemFile is shared memory region wrapper.
class ShmemFile
{
public:
	// Maps shared memory region of size 'size' from a new file 'file', preferably at the address 'preferred'.
	ShmemFile(const char* file, void* preferred, size_t size)
	{
		fd_ = open(file, O_RDWR | O_CREAT | O_TRUNC, 0600);
		if (fd_ == -1)
			failmsg("shmem open failed", "file=%s", file);
		if (fallocate(fd_, 0, 0, size))
			failmsg("shmem fallocate failed", "size=%zu", size);
		Mmap(fd_, preferred, size, true);
		if (unlink(file))
			fail("shmem unlink failed");
	}

	// Maps shared memory region from the file 'fd' in read/write or write-only mode.
	ShmemFile(int fd, void* preferred, size_t size, bool write)
	{
		Mmap(fd, preferred, size, write);
	}

	~ShmemFile()
	{
		if (munmap(mem_, size_))
			fail("shmem munmap failed");
		if (fd_ != -1)
			close(fd_);
	}

	// Prevents any future modifications to the region.
	void Seal()
	{
		if (mprotect(mem_, size_, PROT_READ))
			fail("shmem mprotect failed");
		if (fd_ != -1)
			close(fd_);
		fd_ = -1;
	}

	int FD() const
	{
		return fd_;
	}

	void* Mem() const
	{
		return mem_;
	}

private:
	void* mem_ = nullptr;
	size_t size_ = 0;
	int fd_ = -1;

	void Mmap(int fd, void* preferred, size_t size, bool write)
	{
		size_ = size;
		mem_ = mmap(preferred, size, PROT_READ | (write ? PROT_WRITE : 0), MAP_SHARED, fd, 0);
		if (mem_ == MAP_FAILED)
			failmsg("shmem mmap failed", "size=%zu", size);
	}

	ShmemFile(const ShmemFile&) = delete;
	ShmemFile& operator=(const ShmemFile&) = delete;
};