|
| 1 | +#include <stdint.h> |
| 2 | +#include <signal.h> |
| 3 | +#include <sys/mman.h> |
| 4 | +#include <sys/stat.h> |
| 5 | +#include <sys/types.h> |
| 6 | +#include <sys/wait.h> |
| 7 | +#include "zdtmtst.h" |
| 8 | + |
| 9 | +#ifndef MAP_DROPPABLE |
| 10 | +#define MAP_DROPPABLE 0x08 |
| 11 | +#endif |
| 12 | + |
| 13 | +#ifndef MADV_WIPEONFORK |
| 14 | +#define MADV_WIPEONFORK 18 |
| 15 | +#endif |
| 16 | + |
| 17 | +const char *test_doc = "Test MAP_DROPPABLE/MADV_WIPEONFORK mappings with 2 processes"; |
| 18 | +const char *test_author = "Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com>"; |
| 19 | + |
| 20 | +bool mem_is_zero(const uint8_t *buffer, size_t length) |
| 21 | +{ |
| 22 | + size_t i; |
| 23 | + |
| 24 | + for (i = 0; i < length; i++) |
| 25 | + if (buffer[i] != 0) |
| 26 | + return false; |
| 27 | + |
| 28 | + return true; |
| 29 | +} |
| 30 | + |
| 31 | +int main(int argc, char **argv) |
| 32 | +{ |
| 33 | + uint8_t *p1, *p2; |
| 34 | + pid_t pid; |
| 35 | + int status; |
| 36 | + const char data[] = "MADV_WIPEONFORK vma data"; |
| 37 | + bool criu_was_there = false; |
| 38 | + struct stat st1, st2; |
| 39 | + |
| 40 | + test_init(argc, argv); |
| 41 | + |
| 42 | + p1 = mmap(NULL, sizeof(data), PROT_READ | PROT_WRITE, |
| 43 | + MAP_DROPPABLE | MAP_ANONYMOUS, 0, 0); |
| 44 | + if (p1 == MAP_FAILED) { |
| 45 | + if (errno == EINVAL) { |
| 46 | + skip("mmap failed, no kernel support for MAP_DROPPABLE\n"); |
| 47 | + goto skip; |
| 48 | + } else { |
| 49 | + pr_perror("mmap failed"); |
| 50 | + return -1; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + p2 = mmap(NULL, sizeof(data), PROT_READ | PROT_WRITE, |
| 55 | + MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); |
| 56 | + if (p2 == MAP_FAILED) { |
| 57 | + pr_perror("mmap failed"); |
| 58 | + return 1; |
| 59 | + } |
| 60 | + |
| 61 | + if (madvise(p2, sizeof(data), MADV_WIPEONFORK)) { |
| 62 | + pr_perror("madvise failed"); |
| 63 | + return -1; |
| 64 | + } |
| 65 | + |
| 66 | + /* contents of this mapping is supposed to be dropped after C/R */ |
| 67 | + memcpy(p1, data, sizeof(data)); |
| 68 | + |
| 69 | + /* contents of this mapping is supposed to be dropped after fork() */ |
| 70 | + memcpy(p2, data, sizeof(data)); |
| 71 | + |
| 72 | + /* |
| 73 | + * Let's spawn a process before C/R so our mappings get inherited |
| 74 | + * then, after C/R we need to ensure that CRIU memory premapping |
| 75 | + * machinery works properly. |
| 76 | + * |
| 77 | + * It is important, because we restore MADV_WIPEONFORK on a later |
| 78 | + * stages (after vma premapping happens) and we need to ensure that |
| 79 | + * CRIU handles everything in a right way. |
| 80 | + */ |
| 81 | + pid = test_fork(); |
| 82 | + if (pid < 0) { |
| 83 | + pr_perror("fork failed"); |
| 84 | + return 1; |
| 85 | + } |
| 86 | + |
| 87 | + if (pid == 0) { |
| 88 | + test_waitsig(); |
| 89 | + |
| 90 | + /* |
| 91 | + * Both mappings have VM_WIPEONFORK flag set, |
| 92 | + * so we expect to have it null-ified after fork(). |
| 93 | + */ |
| 94 | + if (!mem_is_zero(p1, sizeof(data)) || |
| 95 | + !mem_is_zero(p2, sizeof(data))) { |
| 96 | + pr_err("1st child: memory check failed\n"); |
| 97 | + return 1; |
| 98 | + } |
| 99 | + |
| 100 | + return 0; |
| 101 | + } |
| 102 | + |
| 103 | + /* |
| 104 | + * A simple way to detect if C/R happened is to compare st_ino |
| 105 | + * fields of stat() on the procfs files of the current task. |
| 106 | + * |
| 107 | + * Hopefully, this terrible hack is never used in real-world |
| 108 | + * applications ;-) Here, we only need this to make test |
| 109 | + * to pass with/without --nocr option. |
| 110 | + */ |
| 111 | + if (stat("/proc/self/status", &st1)) { |
| 112 | + pr_perror("stat"); |
| 113 | + return 1; |
| 114 | + } |
| 115 | + |
| 116 | + test_daemon(); |
| 117 | + test_waitsig(); |
| 118 | + |
| 119 | + /* signal a child process to continue */ |
| 120 | + if (kill(pid, SIGTERM)) { |
| 121 | + pr_perror("kill"); |
| 122 | + goto err; |
| 123 | + } |
| 124 | + |
| 125 | + if (waitpid(pid, &status, 0) != pid) { |
| 126 | + pr_perror("1st waitpid"); |
| 127 | + goto err; |
| 128 | + } |
| 129 | + |
| 130 | + if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 131 | + fail("1st process didn't exit cleanly: status=%d", status); |
| 132 | + goto err; |
| 133 | + } |
| 134 | + |
| 135 | + if (stat("/proc/self/status", &st2)) { |
| 136 | + pr_perror("stat"); |
| 137 | + return 1; |
| 138 | + } |
| 139 | + |
| 140 | + /* detect CRIU */ |
| 141 | + criu_was_there = st1.st_ino != st2.st_ino; |
| 142 | + |
| 143 | + /* |
| 144 | + * We should mark failure if one of the following happens: |
| 145 | + * 1. MAP_DROPPABLE memory is not zero after C/R |
| 146 | + * 2. MAP_DROPPABLE memory somehow changed without C/R |
| 147 | + * (kernel issue? memory pressure?) |
| 148 | + * 3. MADV_WIPEONFORK memory is not preserved |
| 149 | + * |
| 150 | + * We care about 2nd case only because we would like test |
| 151 | + * to pass even with --nocr zdtm.py option. |
| 152 | + */ |
| 153 | + if ((criu_was_there && !mem_is_zero(p1, sizeof(data))) || |
| 154 | + (!criu_was_there && memcmp(p1, data, sizeof(data))) || |
| 155 | + memcmp(p2, data, sizeof(data))) { |
| 156 | + fail("Data mismatch"); |
| 157 | + return 1; |
| 158 | + } |
| 159 | + |
| 160 | + /* contents of these mappings is supposed to be dropped after fork() */ |
| 161 | + memcpy(p1, data, sizeof(data)); |
| 162 | + memcpy(p2, data, sizeof(data)); |
| 163 | + |
| 164 | + pid = test_fork(); |
| 165 | + if (pid < 0) { |
| 166 | + pr_perror("fork failed"); |
| 167 | + return 1; |
| 168 | + } |
| 169 | + |
| 170 | + if (pid == 0) { |
| 171 | + if (!mem_is_zero(p1, sizeof(data)) || |
| 172 | + !mem_is_zero(p2, sizeof(data))) { |
| 173 | + pr_err("2nd child: memory check failed\n"); |
| 174 | + return 1; |
| 175 | + } |
| 176 | + |
| 177 | + return 0; |
| 178 | + } |
| 179 | + |
| 180 | + if (waitpid(pid, &status, 0) != pid) { |
| 181 | + pr_perror("2nd waitpid"); |
| 182 | + goto err; |
| 183 | + } |
| 184 | + |
| 185 | + if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 186 | + fail("2nd process didn't exit cleanly: status=%d", status); |
| 187 | + goto err; |
| 188 | + } |
| 189 | + |
| 190 | + pass(); |
| 191 | + |
| 192 | + return 0; |
| 193 | +err: |
| 194 | + if (waitpid(-1, NULL, WNOHANG) == 0) { |
| 195 | + kill(pid, SIGTERM); |
| 196 | + wait(NULL); |
| 197 | + } |
| 198 | + return 1; |
| 199 | + |
| 200 | +skip: |
| 201 | + test_daemon(); |
| 202 | + test_waitsig(); |
| 203 | + pass(); |
| 204 | + return 0; |
| 205 | +} |
0 commit comments