4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 * Create a process without mappings by unmapping everything at once and
18 * holding it with ptrace(2). See what happens to
21 * /proc/${pid}/numa_maps
23 * /proc/${pid}/smaps_rollup
33 #include <sys/ptrace.h>
34 #include <sys/resource.h>
35 #include <sys/types.h>
40 * 0: vsyscall VMA doesn't exist vsyscall=none
41 * 1: vsyscall VMA is --xp vsyscall=xonly
42 * 2: vsyscall VMA is r-xp vsyscall=emulate
44 static int g_vsyscall;
45 static const char *g_proc_pid_maps_vsyscall;
46 static const char *g_proc_pid_smaps_vsyscall;
48 static const char proc_pid_maps_vsyscall_0[] = "";
49 static const char proc_pid_maps_vsyscall_1[] =
50 "ffffffffff600000-ffffffffff601000 --xp 00000000 00:00 0 [vsyscall]\n";
51 static const char proc_pid_maps_vsyscall_2[] =
52 "ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]\n";
54 static const char proc_pid_smaps_vsyscall_0[] = "";
56 static const char proc_pid_smaps_vsyscall_1[] =
57 "ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]\n"
59 "KernelPageSize: 4 kB\n"
64 "Shared_Clean: 0 kB\n"
65 "Shared_Dirty: 0 kB\n"
66 "Private_Clean: 0 kB\n"
67 "Private_Dirty: 0 kB\n"
71 "AnonHugePages: 0 kB\n"
72 "ShmemPmdMapped: 0 kB\n"
73 "FilePmdMapped: 0 kB\n"
74 "Shared_Hugetlb: 0 kB\n"
75 "Private_Hugetlb: 0 kB\n"
81 * "ProtectionKey:" field is conditional. It is possible to check it as well,
82 * but I don't have such machine.
86 static const char proc_pid_smaps_vsyscall_2[] =
87 "ffffffffff600000-ffffffffff601000 --xp 00000000 00:00 0 [vsyscall]\n"
89 "KernelPageSize: 4 kB\n"
94 "Shared_Clean: 0 kB\n"
95 "Shared_Dirty: 0 kB\n"
96 "Private_Clean: 0 kB\n"
97 "Private_Dirty: 0 kB\n"
101 "AnonHugePages: 0 kB\n"
102 "ShmemPmdMapped: 0 kB\n"
103 "FilePmdMapped: 0 kB\n"
104 "Shared_Hugetlb: 0 kB\n"
105 "Private_Hugetlb: 0 kB\n"
111 * "ProtectionKey:" field is conditional. It is possible to check it as well,
116 static void sigaction_SIGSEGV(int _, siginfo_t *__, void *___)
121 static void sigaction_SIGSEGV_vsyscall(int _, siginfo_t *__, void *___)
127 * vsyscall page can't be unmapped, probe it directly.
129 static void vsyscall(void)
136 fprintf(stderr, "fork, errno %d\n", errno);
140 setrlimit(RLIMIT_CORE, &(struct rlimit){});
142 /* Hide "segfault at ffffffffff600000" messages. */
143 struct sigaction act = {};
144 act.sa_flags = SA_SIGINFO;
145 act.sa_sigaction = sigaction_SIGSEGV_vsyscall;
146 sigaction(SIGSEGV, &act, NULL);
149 /* gettimeofday(NULL, NULL); */
153 : "i" (0xffffffffff600000), "D" (NULL), "S" (NULL)
154 : "rax", "rcx", "r11"
158 *(volatile int *)0xffffffffff600000UL;
163 waitpid(pid, &wstatus, 0);
164 if (WIFEXITED(wstatus)) {
165 g_vsyscall = WEXITSTATUS(wstatus);
167 fprintf(stderr, "error: vsyscall wstatus %08x\n", wstatus);
172 static int test_proc_pid_maps(pid_t pid)
175 snprintf(buf, sizeof(buf), "/proc/%u/maps", pid);
176 int fd = open(buf, O_RDONLY);
178 perror("open /proc/${pid}/maps");
181 ssize_t rv = read(fd, buf, sizeof(buf));
183 if (g_vsyscall == 0) {
186 size_t len = strlen(g_proc_pid_maps_vsyscall);
188 assert(memcmp(buf, g_proc_pid_maps_vsyscall, len) == 0);
194 static int test_proc_pid_numa_maps(pid_t pid)
197 snprintf(buf, sizeof(buf), "/proc/%u/numa_maps", pid);
198 int fd = open(buf, O_RDONLY);
200 if (errno == ENOENT) {
202 * /proc/${pid}/numa_maps is under CONFIG_NUMA,
203 * it doesn't necessarily exist.
207 perror("open /proc/${pid}/numa_maps");
210 ssize_t rv = read(fd, buf, sizeof(buf));
217 static int test_proc_pid_smaps(pid_t pid)
220 snprintf(buf, sizeof(buf), "/proc/%u/smaps", pid);
221 int fd = open(buf, O_RDONLY);
223 if (errno == ENOENT) {
225 * /proc/${pid}/smaps is under CONFIG_PROC_PAGE_MONITOR,
226 * it doesn't necessarily exist.
230 perror("open /proc/${pid}/smaps");
233 ssize_t rv = read(fd, buf, sizeof(buf));
235 if (g_vsyscall == 0) {
238 size_t len = strlen(g_proc_pid_maps_vsyscall);
239 /* TODO "ProtectionKey:" */
241 assert(memcmp(buf, g_proc_pid_maps_vsyscall, len) == 0);
247 static const char g_smaps_rollup[] =
248 "00000000-00000000 ---p 00000000 00:00 0 [rollup]\n"
255 "Shared_Clean: 0 kB\n"
256 "Shared_Dirty: 0 kB\n"
257 "Private_Clean: 0 kB\n"
258 "Private_Dirty: 0 kB\n"
262 "AnonHugePages: 0 kB\n"
263 "ShmemPmdMapped: 0 kB\n"
264 "FilePmdMapped: 0 kB\n"
265 "Shared_Hugetlb: 0 kB\n"
266 "Private_Hugetlb: 0 kB\n"
272 static int test_proc_pid_smaps_rollup(pid_t pid)
275 snprintf(buf, sizeof(buf), "/proc/%u/smaps_rollup", pid);
276 int fd = open(buf, O_RDONLY);
278 if (errno == ENOENT) {
280 * /proc/${pid}/smaps_rollup is under CONFIG_PROC_PAGE_MONITOR,
281 * it doesn't necessarily exist.
285 perror("open /proc/${pid}/smaps_rollup");
288 ssize_t rv = read(fd, buf, sizeof(buf));
290 assert(rv == sizeof(g_smaps_rollup) - 1);
291 assert(memcmp(buf, g_smaps_rollup, sizeof(g_smaps_rollup) - 1) == 0);
298 int rv = EXIT_SUCCESS;
302 switch (g_vsyscall) {
304 g_proc_pid_maps_vsyscall = proc_pid_maps_vsyscall_0;
305 g_proc_pid_smaps_vsyscall = proc_pid_smaps_vsyscall_0;
308 g_proc_pid_maps_vsyscall = proc_pid_maps_vsyscall_1;
309 g_proc_pid_smaps_vsyscall = proc_pid_smaps_vsyscall_1;
312 g_proc_pid_maps_vsyscall = proc_pid_maps_vsyscall_2;
313 g_proc_pid_smaps_vsyscall = proc_pid_smaps_vsyscall_2;
323 } else if (pid == 0) {
324 rv = ptrace(PTRACE_TRACEME, 0, NULL, NULL);
326 if (errno == EPERM) {
328 "Did you know? ptrace(PTRACE_TRACEME) doesn't work under strace.\n"
330 kill(getppid(), SIGTERM);
333 perror("ptrace PTRACE_TRACEME");
338 * Hide "segfault at ..." messages. Signal handler won't run.
340 struct sigaction act = {};
341 act.sa_flags = SA_SIGINFO;
342 act.sa_sigaction = sigaction_SIGSEGV;
343 sigaction(SIGSEGV, &act, NULL);
346 munmap(NULL, ((size_t)1 << 47) - 4096);
348 #error "implement 'unmap everything'"
353 * TODO find reliable way to signal parent that munmap(2) completed.
354 * Child can't do it directly because it effectively doesn't exist
355 * anymore. Looking at child's VM files isn't 100% reliable either:
356 * due to a bug they may not become empty or empty-like.
360 if (rv == EXIT_SUCCESS) {
361 rv = test_proc_pid_maps(pid);
363 if (rv == EXIT_SUCCESS) {
364 rv = test_proc_pid_numa_maps(pid);
366 if (rv == EXIT_SUCCESS) {
367 rv = test_proc_pid_smaps(pid);
369 if (rv == EXIT_SUCCESS) {
370 rv = test_proc_pid_smaps_rollup(pid);
373 * TODO test /proc/${pid}/statm, task_statm()
374 * ->start_code, ->end_code aren't updated by munmap().
375 * Output can be "0 0 0 2 0 0 0\n" where "2" can be anything.
380 waitpid(pid, &wstatus, 0);
381 assert(WIFSTOPPED(wstatus));
382 assert(WSTOPSIG(wstatus) == SIGSEGV);