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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
#include <errno.h>
#include <limits.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/mman.h>
#include <bits/ensure.h>
#include <mlibc/debug.hpp>
#include <mlibc/posix-sysdeps.hpp>
int mprotect(void *pointer, size_t size, int prot) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_vm_protect, -1);
if(int e = mlibc::sys_vm_protect(pointer, size, prot); e) {
errno = e;
return -1;
}
return 0;
}
int mlock(const void *addr, size_t len) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_mlock, -1);
if(int e = mlibc::sys_mlock(addr, len); e) {
errno = e;
return -1;
}
return 0;
}
int mlockall(int flags) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_mlockall, -1);
if(int e = mlibc::sys_mlockall(flags); e) {
errno = e;
return -1;
}
return 0;
}
int munlock(const void *addr, size_t len) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_munlock, -1);
if(int e = mlibc::sys_munlock(addr, len); e) {
errno = e;
return -1;
}
return 0;
}
int munlockall(void) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_munlockall, -1);
if(int e = mlibc::sys_munlockall(); e) {
errno = e;
return -1;
}
return 0;
}
int posix_madvise(void *, size_t, int) {
mlibc::infoLogger() << "\e[31m" "mlibc: posix_madvise() fails unconditionally" "\e[39m"
<< frg::endlog;
return ENOSYS;
}
int msync(void *addr, size_t length, int flags) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_msync, -1);
if(int e = mlibc::sys_msync(addr, length, flags); e) {
errno = e;
return -1;
}
return 0;
}
void *mremap(void *pointer, size_t size, size_t new_size, int flags, ...) {
__ensure(flags == MREMAP_MAYMOVE);
void *window;
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_vm_remap, (void *)-1);
if(int e = mlibc::sys_vm_remap(pointer, size, new_size, &window); e) {
errno = e;
return (void *)-1;
}
return window;
}
int remap_file_pages(void *, size_t, int, size_t, int) {
__ensure(!"Not implemented");
__builtin_unreachable();
}
void *mmap(void *hint, size_t size, int prot, int flags, int fd, off_t offset) {
void *window;
if(int e = mlibc::sys_vm_map(hint, size, prot, flags, fd, offset, &window); e) {
errno = e;
return (void *)-1;
}
return window;
}
int munmap(void *pointer, size_t size) {
if(int e = mlibc::sys_vm_unmap(pointer, size); e) {
errno = e;
return -1;
}
return 0;
}
// The implementation of shm_open and shm_unlink is taken from musl.
namespace {
char *shm_mapname(const char *name, char *buf) {
char *p;
while(*name == '/')
name++;
if(*(p = strchrnul(name, '/')) || p == name ||
(p - name <= 2 && name[0] == '.' && p[-1] == '.')) {
errno = EINVAL;
return 0;
}
if(p - name > NAME_MAX) {
errno = ENAMETOOLONG;
return 0;
}
memcpy(buf, "/dev/shm/", 9);
memcpy(buf + 9, name, p - name + 1);
return buf;
}
}
int shm_open(const char *name, int flags, mode_t mode) {
int cs;
char buf[NAME_MAX + 10];
if(!(name = shm_mapname(name, buf)))
return -1;
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
int fd = open(name, flags | O_NOFOLLOW | O_CLOEXEC | O_NONBLOCK, mode);
pthread_setcancelstate(cs, 0);
return fd;
}
int shm_unlink(const char *name) {
char buf[NAME_MAX + 10];
if(!(name = shm_mapname(name, buf)))
return -1;
return unlink(name);
}
#if __MLIBC_LINUX_OPTION
int memfd_create(const char *name, unsigned int flags) {
int ret = -1;
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_memfd_create, -1);
if(int e = mlibc::sys_memfd_create(name, flags, &ret)) {
errno = e;
return -1;
}
return ret;
}
int madvise(void *addr, size_t length, int advice) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_madvise, -1);
if(int e = mlibc::sys_madvise(addr, length, advice)) {
errno = e;
return -1;
}
return 0;
}
int mincore(void *addr, size_t length, unsigned char *vec) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_munlockall, -1);
if(int e = mlibc::sys_mincore(addr, length, vec); e) {
errno = e;
return -1;
}
return 0;
}
#endif /* __MLIBC_LINUX_OPTION */
|