1 #if defined __amd64__ || defined __i386__
3 * Copyright (c) 2022 Alexey Dobriyan <adobriyan@gmail.com>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 * Create a process without mappings by unmapping everything at once and
19 * holding it with ptrace(2). See what happens to
22 * /proc/${pid}/numa_maps
24 * /proc/${pid}/smaps_rollup
35 #include <sys/ptrace.h>
36 #include <sys/resource.h>
37 #include <sys/types.h>
46 * 0: vsyscall VMA doesn't exist vsyscall=none
47 * 1: vsyscall VMA is --xp vsyscall=xonly
48 * 2: vsyscall VMA is r-xp vsyscall=emulate
50 static volatile int g_vsyscall;
51 static const char *g_proc_pid_maps_vsyscall;
52 static const char *g_proc_pid_smaps_vsyscall;
54 static const char proc_pid_maps_vsyscall_0[] = "";
55 static const char proc_pid_maps_vsyscall_1[] =
56 "ffffffffff600000-ffffffffff601000 --xp 00000000 00:00 0 [vsyscall]\n";
57 static const char proc_pid_maps_vsyscall_2[] =
58 "ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]\n";
60 static const char proc_pid_smaps_vsyscall_0[] = "";
62 static const char proc_pid_smaps_vsyscall_1[] =
63 "ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]\n"
65 "KernelPageSize: 4 kB\n"
70 "Shared_Clean: 0 kB\n"
71 "Shared_Dirty: 0 kB\n"
72 "Private_Clean: 0 kB\n"
73 "Private_Dirty: 0 kB\n"
77 "AnonHugePages: 0 kB\n"
78 "ShmemPmdMapped: 0 kB\n"
79 "FilePmdMapped: 0 kB\n"
80 "Shared_Hugetlb: 0 kB\n"
81 "Private_Hugetlb: 0 kB\n"
87 * "ProtectionKey:" field is conditional. It is possible to check it as well,
88 * but I don't have such machine.
92 static const char proc_pid_smaps_vsyscall_2[] =
93 "ffffffffff600000-ffffffffff601000 --xp 00000000 00:00 0 [vsyscall]\n"
95 "KernelPageSize: 4 kB\n"
100 "Shared_Clean: 0 kB\n"
101 "Shared_Dirty: 0 kB\n"
102 "Private_Clean: 0 kB\n"
103 "Private_Dirty: 0 kB\n"
107 "AnonHugePages: 0 kB\n"
108 "ShmemPmdMapped: 0 kB\n"
109 "FilePmdMapped: 0 kB\n"
110 "Shared_Hugetlb: 0 kB\n"
111 "Private_Hugetlb: 0 kB\n"
117 * "ProtectionKey:" field is conditional. It is possible to check it as well,
122 static void sigaction_SIGSEGV(int _, siginfo_t *__, void *___)
128 static void sigaction_SIGSEGV_vsyscall(int _, siginfo_t *__, void *___)
134 * vsyscall page can't be unmapped, probe it directly.
136 static void vsyscall(void)
143 fprintf(stderr, "fork, errno %d\n", errno);
147 setrlimit(RLIMIT_CORE, &(struct rlimit){});
149 /* Hide "segfault at ffffffffff600000" messages. */
150 struct sigaction act = {};
151 act.sa_flags = SA_SIGINFO;
152 act.sa_sigaction = sigaction_SIGSEGV_vsyscall;
153 sigaction(SIGSEGV, &act, NULL);
156 /* gettimeofday(NULL, NULL); */
157 uint64_t rax = 0xffffffffff600000;
161 : "D" (NULL), "S" (NULL)
166 *(volatile int *)0xffffffffff600000UL;
171 waitpid(pid, &wstatus, 0);
172 if (WIFEXITED(wstatus)) {
173 g_vsyscall = WEXITSTATUS(wstatus);
175 fprintf(stderr, "error: vsyscall wstatus %08x\n", wstatus);
181 static int test_proc_pid_maps(pid_t pid)
184 snprintf(buf, sizeof(buf), "/proc/%u/maps", pid);
185 int fd = open(buf, O_RDONLY);
187 perror("open /proc/${pid}/maps");
190 ssize_t rv = read(fd, buf, sizeof(buf));
192 if (g_vsyscall == 0) {
195 size_t len = strlen(g_proc_pid_maps_vsyscall);
197 assert(memcmp(buf, g_proc_pid_maps_vsyscall, len) == 0);
203 static int test_proc_pid_numa_maps(pid_t pid)
206 snprintf(buf, sizeof(buf), "/proc/%u/numa_maps", pid);
207 int fd = open(buf, O_RDONLY);
209 if (errno == ENOENT) {
211 * /proc/${pid}/numa_maps is under CONFIG_NUMA,
212 * it doesn't necessarily exist.
216 perror("open /proc/${pid}/numa_maps");
219 ssize_t rv = read(fd, buf, sizeof(buf));
226 static int test_proc_pid_smaps(pid_t pid)
229 snprintf(buf, sizeof(buf), "/proc/%u/smaps", pid);
230 int fd = open(buf, O_RDONLY);
232 if (errno == ENOENT) {
234 * /proc/${pid}/smaps is under CONFIG_PROC_PAGE_MONITOR,
235 * it doesn't necessarily exist.
239 perror("open /proc/${pid}/smaps");
242 ssize_t rv = read(fd, buf, sizeof(buf));
244 if (g_vsyscall == 0) {
247 size_t len = strlen(g_proc_pid_maps_vsyscall);
248 /* TODO "ProtectionKey:" */
250 assert(memcmp(buf, g_proc_pid_maps_vsyscall, len) == 0);
256 static const char g_smaps_rollup[] =
257 "00000000-00000000 ---p 00000000 00:00 0 [rollup]\n"
264 "Shared_Clean: 0 kB\n"
265 "Shared_Dirty: 0 kB\n"
266 "Private_Clean: 0 kB\n"
267 "Private_Dirty: 0 kB\n"
272 "AnonHugePages: 0 kB\n"
273 "ShmemPmdMapped: 0 kB\n"
274 "FilePmdMapped: 0 kB\n"
275 "Shared_Hugetlb: 0 kB\n"
276 "Private_Hugetlb: 0 kB\n"
282 static int test_proc_pid_smaps_rollup(pid_t pid)
285 snprintf(buf, sizeof(buf), "/proc/%u/smaps_rollup", pid);
286 int fd = open(buf, O_RDONLY);
288 if (errno == ENOENT) {
290 * /proc/${pid}/smaps_rollup is under CONFIG_PROC_PAGE_MONITOR,
291 * it doesn't necessarily exist.
295 perror("open /proc/${pid}/smaps_rollup");
298 ssize_t rv = read(fd, buf, sizeof(buf));
300 assert(rv == sizeof(g_smaps_rollup) - 1);
301 assert(memcmp(buf, g_smaps_rollup, sizeof(g_smaps_rollup) - 1) == 0);
308 int rv = EXIT_SUCCESS;
314 switch (g_vsyscall) {
316 g_proc_pid_maps_vsyscall = proc_pid_maps_vsyscall_0;
317 g_proc_pid_smaps_vsyscall = proc_pid_smaps_vsyscall_0;
320 g_proc_pid_maps_vsyscall = proc_pid_maps_vsyscall_1;
321 g_proc_pid_smaps_vsyscall = proc_pid_smaps_vsyscall_1;
324 g_proc_pid_maps_vsyscall = proc_pid_maps_vsyscall_2;
325 g_proc_pid_smaps_vsyscall = proc_pid_smaps_vsyscall_2;
335 } else if (pid == 0) {
336 rv = ptrace(PTRACE_TRACEME, 0, NULL, NULL);
338 if (errno == EPERM) {
340 "Did you know? ptrace(PTRACE_TRACEME) doesn't work under strace.\n"
342 kill(getppid(), SIGTERM);
345 perror("ptrace PTRACE_TRACEME");
350 * Hide "segfault at ..." messages. Signal handler won't run.
352 struct sigaction act = {};
353 act.sa_flags = SA_SIGINFO;
354 act.sa_sigaction = sigaction_SIGSEGV;
355 sigaction(SIGSEGV, &act, NULL);
358 munmap(NULL, ((size_t)1 << 47) - 4096);
359 #elif defined __i386__
363 for (len = -4096;; len -= 4096) {
368 #error "implement 'unmap everything'"
373 * TODO find reliable way to signal parent that munmap(2) completed.
374 * Child can't do it directly because it effectively doesn't exist
375 * anymore. Looking at child's VM files isn't 100% reliable either:
376 * due to a bug they may not become empty or empty-like.
380 if (rv == EXIT_SUCCESS) {
381 rv = test_proc_pid_maps(pid);
383 if (rv == EXIT_SUCCESS) {
384 rv = test_proc_pid_numa_maps(pid);
386 if (rv == EXIT_SUCCESS) {
387 rv = test_proc_pid_smaps(pid);
389 if (rv == EXIT_SUCCESS) {
390 rv = test_proc_pid_smaps_rollup(pid);
393 * TODO test /proc/${pid}/statm, task_statm()
394 * ->start_code, ->end_code aren't updated by munmap().
395 * Output can be "0 0 0 2 0 0 0\n" where "2" can be anything.
400 waitpid(pid, &wstatus, 0);
401 assert(WIFSTOPPED(wstatus));
402 assert(WSTOPSIG(wstatus) == SIGSEGV);