Loading...
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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 | /* xcrun -sdk iphoneos.internal clang -ldarwintest -o freeable_vnodes freeable_vnodes.c -g -Weverything */ #include <darwintest.h> #include <darwintest_utils.h> #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <signal.h> #include <fcntl.h> #include <string.h> #include <sys/sysctl.h> #include <sys/mman.h> #include <sys/stat.h> #include <sys/socket.h> #include <TargetConditionals.h> static pid_t vnoder_pid = -1; static uint32_t maxvnodes = 0; T_GLOBAL_META(T_META_NAMESPACE("xnu.vfs"), T_META_TAG_VM_PREFERRED); #if TARGET_OS_IOS static char *dlpaths[] = { "/System/Library/Assistant/FlowDelegatePlugins/HomeAutomationFlowDelegatePlugin.bundle/HomeAutomationFlowDelegatePlugin", "/System/Library/NanoPreferenceBundles/Applications/NanoPassbookBridgeSettings.bundle/NanoPassbookBridgeSettings", "/System/Library/Assistant/FlowDelegatePlugins/MessagesFlowDelegatePlugin.bundle/MessagesFlowDelegatePlugin", "/System/Library/NanoPreferenceBundles/Discover/HealthAndFitnessPlugin.bundle/HealthAndFitnessPlugin", "/System/Library/NanoPreferenceBundles/SetupBundles/DepthCompanionSetup.bundle/DepthCompanionSetup", "/System/Library/PreferenceBundles/DigitalSeparationSettings.bundle/DigitalSeparationSettings", "/System/Library/Assistant/FlowDelegatePlugins/NotebookFlowPlugin.bundle/NotebookFlowPlugin", "/System/Library/NanoPreferenceBundles/Discover/UserGuidePlugin.bundle/UserGuidePlugin", "/System/Library/PreferenceBundles/NotificationsSettings.bundle/NotificationsSettings", "/System/Library/PreferenceBundles/AssistantSettings.bundle/AssistantSettings", "/System/Library/PreferenceBundles/SettingsCellular.bundle/SettingsCellular", "/System/Library/PreferenceBundles/FocusSettings.bundle/FocusSettings", "/private/preboot/Cryptexes/App/System/Library/PreferenceBundles/MobileSafariSettings.bundle/MobileSafariSettings", NULL }; #elif TARGET_OS_WATCH static char *dlpaths[] = { "/System/Library/Assistant/FlowDelegatePlugins/HomeAutomationFlowDelegatePlugin.bundle/HomeAutomationFlowDelegatePlugin", "/System/Library/Assistant/FlowDelegatePlugins/MessagesFlowDelegatePlugin.bundle/MessagesFlowDelegatePlugin", "/System/Library/PreferenceBundles/NanoAccessibilitySettings.bundle/NanoAccessibilitySettings", "/System/Library/Assistant/FlowDelegatePlugins/NotebookFlowPlugin.bundle/NotebookFlowPlugin", NULL }; #elif TARGET_OS_OSX static char *dlpaths[] = { "/System/Library/Assistant/FlowDelegatePlugins/HomeAutomationFlowDelegatePlugin.bundle/Contents/MacOS/HomeAutomationFlowDelegatePlugin", "/System/Library/Assistant/FlowDelegatePlugins/MessagesFlowDelegatePlugin.bundle/Contents/MacOS/MessagesFlowDelegatePlugin", "/System/Library/Assistant/FlowDelegatePlugins/NotebookFlowPlugin.bundle/Contents/MacOS/NotebookFlowPlugin", NULL }; #else static char *dlpaths[] = { NULL }; #endif static uint32_t get_sysctl_int(char *sysctl_name) { uint32_t max_vnodes = 0; size_t length = sizeof(max_vnodes); int return_code = 0; return_code = sysctlbyname(sysctl_name, &max_vnodes, &length, NULL, 0); T_QUIET; T_ASSERT_POSIX_SUCCESS(return_code, "sysctl call should return 0"); return max_vnodes; } #define MINVNODES_FOR_TEST 200 #define MAXVNODES_FOR_TEST 18000 static void run_vnoder(void) { void *buffer; uint32_t i = 0; uint32_t current_num = 0; uint32_t num_files = maxvnodes - MINVNODES_FOR_TEST; int fd = -1; int dir_fd = -1; char dirpath[PATH_MAX]; char filepath[NAME_MAX]; T_WITH_ERRNO; /* Create a temporary working directory */ sprintf(dirpath, "%s/tmp-mmap-bomb-dir.%d", dt_tmpdir(), getpid()); T_QUIET; T_ASSERT_POSIX_SUCCESS(mkdir(dirpath, S_IRWXU | S_IRWXG ), NULL); T_LOG("Created test dir %s\n", dirpath); T_QUIET; T_ASSERT_POSIX_SUCCESS(dir_fd = open(dirpath, O_RDONLY), NULL); /* use mmap to exhaust vnode pool */ uint32_t log_interval = (num_files / 4); uint32_t next_test_log_number = log_interval; T_LOG("trying to map %u files, progress every %u files\n", num_files, log_interval); /* * This loop can take an ideterminate amount of time on different devices since the * it is based on file creation (which is rate limited by the SEP) and the number we * will attempt to create. At some point it should be replaced by walking a large hierarchy * (like /System) and mapping those files instead of creating files. */ for (i = 0; i < num_files; ++i) { if (i == next_test_log_number) { T_LOG("created and mapped %u files so far\n", next_test_log_number); next_test_log_number += log_interval; current_num = get_sysctl_int("vfs.vnstats.num_vnodes"); if (current_num >= (maxvnodes + 5000)) { T_LOG("numvnodes is >= maxvnodes + 5000 (%u), done with creation and mmap loop\n", current_num); break; } } sprintf(filepath, "file-%d", i); T_QUIET; T_ASSERT_POSIX_SUCCESS(fd = openat(dir_fd, filepath, O_CREAT | O_RDWR, 0666), NULL); T_QUIET; T_ASSERT_POSIX_SUCCESS(ftruncate(fd, (off_t)PAGE_SIZE), NULL); T_QUIET; T_ASSERT_POSIX_SUCCESS(unlinkat(dir_fd, filepath, 0), NULL); T_QUIET; T_ASSERT_POSIX_SUCCESS(buffer = mmap(NULL, PAGE_SIZE, PROT_READ, MAP_FILE | MAP_NOCACHE | MAP_PRIVATE, fd, 0), NULL); T_QUIET; T_ASSERT_POSIX_SUCCESS(close(fd), NULL); } T_LOG("created and mapped %u files\n", i); T_QUIET; T_ASSERT_POSIX_SUCCESS(close(dir_fd), NULL); T_QUIET; T_ASSERT_POSIX_SUCCESS(rmdir(dirpath), NULL); } static void cleanup(void) { if (vnoder_pid > 0) { T_LOG("Killing vnoder pid in cleanup: %d", vnoder_pid); kill(vnoder_pid, SIGKILL); } } T_DECL(vnode_max_increase, "Consume vnodes to cause the max vnodes available to increase", T_META_REQUIRES_SYSCTL_EQ("vfs.vnstats.vn_dealloc_level", 1), T_META_RADAR_COMPONENT_NAME("xnu"), T_META_RADAR_COMPONENT_VERSION("VFS")) { int sock_fd[2]; char buf[10]; uint32_t initial_num = 0, current_num = 0; uint32_t deallocateable_vnodes = 0; uint32_t deallocateable_busy_vnodes = 0; uint32_t vnode_delta = 100; uint32_t timeout = 10; uint32_t i = 0; T_ATEND(cleanup); T_WITH_ERRNO; T_SETUPBEGIN; /* * We use this to handshake certain actions between this process and its * child. */ T_QUIET; T_ASSERT_POSIX_SUCCESS(socketpair(AF_UNIX, SOCK_STREAM, 0, sock_fd), NULL); maxvnodes = get_sysctl_int("kern.maxvnodes"); T_LOG("max vnodes: %d", maxvnodes); if (maxvnodes > MAXVNODES_FOR_TEST) { T_SKIP("maxvnodes can't be more than %d for test", MAXVNODES_FOR_TEST); } else if (maxvnodes <= MINVNODES_FOR_TEST) { T_SKIP("maxvnodes can't be less than %d for test", MINVNODES_FOR_TEST); } initial_num = get_sysctl_int("vfs.vnstats.num_vnodes"); T_LOG("Initial num vnodes: %d", initial_num); T_SETUPEND; T_QUIET; T_ASSERT_POSIX_SUCCESS(vnoder_pid = fork(), NULL); if (vnoder_pid == 0) { /* child */ T_QUIET; T_ASSERT_POSIX_SUCCESS(close(sock_fd[0]), NULL); run_vnoder(); /* now let parent know we're done with creating all the vnodes */ T_QUIET; T_ASSERT_POSIX_SUCCESS(write(sock_fd[1], "done", sizeof("done")), NULL); /* wait for parent to set us to send us a signal */ T_QUIET; T_ASSERT_POSIX_SUCCESS(read(sock_fd[1], buf, sizeof(buf)), NULL); pause(); exit(0); } T_QUIET; T_ASSERT_POSIX_SUCCESS(close(sock_fd[1]), NULL); /* wait for child to run and run up the vnodes */ T_QUIET; T_ASSERT_POSIX_SUCCESS(read(sock_fd[0], buf, sizeof(buf)), NULL); int num_getpaths = 0; for (i = 0; i < 5 && dlpaths[0] != NULL; i++) { for (int j = 0; dlpaths[j] != NULL; j++) { int dlpath_fd; char path[256] = {0}; struct stat sb; if (stat(dlpaths[j], &sb) != -1) { T_QUIET; T_ASSERT_POSIX_SUCCESS(dlpath_fd = open(dlpaths[j], O_RDONLY), NULL); T_QUIET; T_ASSERT_POSIX_SUCCESS(fcntl(dlpath_fd, F_GETPATH, &path), "path is %s, iteration number is %d and path number is %d", dlpaths[j], i + 1, j + 1); T_QUIET; T_ASSERT_POSIX_SUCCESS(close(dlpath_fd), NULL); num_getpaths++; } } } T_LOG("Num getpaths done = %d", num_getpaths); current_num = get_sysctl_int("vfs.vnstats.num_vnodes"); T_LOG("num vnodes after vnoder: %d", current_num); T_QUIET; T_ASSERT_GT(current_num, maxvnodes, "vnode maximum should increase under vnode presssure"); T_LOG("Killing vnoder (pid %d) to free up held vnodes", vnoder_pid); T_QUIET; T_ASSERT_POSIX_SUCCESS(write(sock_fd[0], "done", sizeof("done")), NULL); kill(vnoder_pid, SIGKILL); vnoder_pid = -1; T_LOG("Waiting up to %ds for vnodes to be deallocated", timeout); for (i = 0; i < timeout; i++) { sleep(1); deallocateable_vnodes = get_sysctl_int( "vfs.vnstats.num_deallocable_vnodes"); deallocateable_busy_vnodes = get_sysctl_int( "vfs.vnstats.num_deallocable_busy_vnodes"); T_LOG("deallocateable_vnodes after %d second%s : %d", i + 1, (i == 0) ? "" : "s", deallocateable_vnodes); T_LOG("deallocateable_busy_vnodes after %d second%s : %d", i + 1, (i == 0) ? "" : "s", deallocateable_busy_vnodes); if (deallocateable_vnodes < vnode_delta) { break; } /* This can happen because we don't fetch atomically */ if (deallocateable_busy_vnodes > deallocateable_vnodes) { deallocateable_busy_vnodes = deallocateable_vnodes; } if ((i == (timeout - 1)) && ((deallocateable_vnodes - deallocateable_busy_vnodes) < vnode_delta)) { break; } } T_QUIET; T_ASSERT_NE(i, timeout, "Deallocateable vnodes should drop in under %ds", timeout); current_num = get_sysctl_int("vfs.vnstats.num_vnodes"); T_LOG("num vnodes after killing vnoder: %d", current_num); T_QUIET; T_ASSERT_LE(current_num, maxvnodes + deallocateable_busy_vnodes + vnode_delta, "vnode maximum should be within %d of the initial maximum", vnode_delta); } |