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 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
#include <darwintest.h> #include "xzone_testing.h" T_GLOBAL_META(T_META_RUN_CONCURRENTLY(TRUE), T_META_TAG_VM_PREFERRED, T_META_TAG_NO_ALLOCATOR_OVERRIDE); #if CONFIG_XZONE_MALLOC && CONFIG_VM_USER_RANGES #include "../src/xzone_malloc/xzone_segment.c" struct ptr_range_test { const char *desc; struct { struct mach_vm_range left_void; struct mach_vm_range right_void; uint64_t ptr_range_size; uint64_t entropy; } input; struct { struct mach_vm_range expected_ranges[2]; size_t range_count_out; } range_output; struct { struct xzm_range_group_s expected_range_groups[XZM_RANGE_GROUP_PTR + 2]; } range_group_output; }; static void test_fake_range_groups_init(struct xzm_range_group_s *range_groups) { // Copied from main xzone setup size_t rg_idx = 0; size_t allocation_front_count = 2; for (size_t i = 0; i < XZM_RANGE_GROUP_COUNT; i++) { xzm_range_group_id_t rgid = (xzm_range_group_id_t)i; size_t rg_fronts = (rgid == XZM_RANGE_GROUP_PTR) ? allocation_front_count : 1; for (size_t j = 0; j < rg_fronts; j++) { xzm_range_group_t rg = &range_groups[rg_idx]; rg->xzrg_id = rgid; rg->xzrg_front = (xzm_front_index_t)j; rg->xzrg_main_ref = NULL; _malloc_lock_init(&rg->xzrg_lock); rg_idx++; } } } static void test_exhaust_range_group(xzm_range_group_t rg) { uintptr_t last_allocated = 0; uint64_t total_allocated = 0; bool warn_on_exhaustion = true; while (true) { uintptr_t addr = _xzm_range_group_bump_alloc_segment(rg, XZM_SEGMENT_SIZE, warn_on_exhaustion); if (!addr) { T_EXPECT_GE(total_allocated, (XZM_POINTER_RANGE_SIZE / 2) - MiB(16), "allocated at least expected amount"); break; } T_QUIET; T_EXPECT_EQ(addr % XZM_SEGMENT_SIZE, 0ull, "segment alignment"); if (rg->xzrg_direction == XZM_FRONT_INCREASING) { T_QUIET; T_EXPECT_GE((uint64_t)addr, last_allocated + XZM_SEGMENT_SIZE, "address increased"); T_QUIET; T_EXPECT_LE(addr + XZM_SEGMENT_SIZE, rg->xzrg_base + rg->xzrg_size + rg->xzrg_skip_size, "address in bounds"); if (rg->xzrg_skip_addr && addr >= rg->xzrg_skip_addr) { T_QUIET; T_EXPECT_GE((uint64_t)addr, rg->xzrg_skip_addr + rg->xzrg_skip_size, "skip respected"); } } else { T_QUIET; T_EXPECT_LE((uint64_t)addr, last_allocated - XZM_SEGMENT_SIZE, "address decreased"); T_QUIET; T_EXPECT_GE((uint64_t)addr, rg->xzrg_base - (rg->xzrg_size + rg->xzrg_skip_size), "address in bounds"); if (rg->xzrg_skip_addr && addr < rg->xzrg_skip_addr) { T_QUIET; T_EXPECT_LE((uint64_t)addr, rg->xzrg_skip_addr - (rg->xzrg_skip_size + XZM_SEGMENT_SIZE), "skip respected"); } } last_allocated = addr; total_allocated += XZM_SEGMENT_SIZE; } } static void test_ptr_range_setup(struct ptr_range_test *test) { T_LOG("testing %s", test->desc); struct mach_vm_range ranges[2]; size_t range_count = 2; _xzm_main_malloc_zone_choose_ptr_ranges(test->input.left_void, test->input.right_void, test->input.ptr_range_size, test->input.entropy, ranges, &range_count); T_ASSERT_EQ(range_count, test->range_output.range_count_out, "range_count_out"); for (size_t i = 0; i < range_count; i++) { T_EXPECT_EQ(ranges[i].min_address, test->range_output.expected_ranges[i].min_address, "expected min address"); T_EXPECT_EQ(ranges[i].max_address, test->range_output.expected_ranges[i].max_address, "expected max address"); } struct xzm_range_group_s range_groups[XZM_RANGE_GROUP_PTR + 2] = { 0 }; test_fake_range_groups_init(range_groups); _xzm_main_malloc_zone_init_ptr_fronts(range_groups, 2, (struct xzm_vm_range *)ranges, range_count, NULL); for (size_t i = XZM_RANGE_GROUP_PTR; i < XZM_RANGE_GROUP_PTR + 2; i++) { xzm_range_group_t actual = &range_groups[i]; xzm_range_group_t expected = &test->range_group_output.expected_range_groups[i]; T_EXPECT_EQ(actual->xzrg_id, expected->xzrg_id, "xzrg_id"); T_EXPECT_EQ_INT(actual->xzrg_front, expected->xzrg_front, "xzrg_front"); T_EXPECT_EQ(actual->xzrg_base, expected->xzrg_base, "xzrg_base"); T_EXPECT_EQ(actual->xzrg_size, expected->xzrg_size, "xzrg_size"); T_EXPECT_EQ(actual->xzrg_skip_addr, expected->xzrg_skip_addr, "xzrg_skip_addr"); T_EXPECT_EQ(actual->xzrg_skip_size, expected->xzrg_skip_size, "xzrg_skip_size"); T_EXPECT_EQ(actual->xzrg_next, expected->xzrg_next, "xzrg_next"); T_EXPECT_EQ(actual->xzrg_direction, expected->xzrg_direction, "xzrg_direction"); test_exhaust_range_group(actual); } } T_DECL(xzone_segment_ptr_range_setup, "set up ptr ranges") { struct ptr_range_test empty_left_beginning = { .desc = "empty left void, ptr range at the beginning", .input = { .left_void = { .min_address = GiB(16), .max_address = GiB(16), }, .right_void = { .min_address = GiB(26), .max_address = GiB(63), }, .ptr_range_size = XZM_POINTER_RANGE_SIZE, .entropy = 0, }, .range_output = { .expected_ranges = { { .min_address = GiB(30), .max_address = GiB(30) + XZM_POINTER_RANGE_SIZE, }, }, .range_count_out = 1, }, .range_group_output = { .expected_range_groups = { [XZM_RANGE_GROUP_PTR + 0] = { .xzrg_id = XZM_RANGE_GROUP_PTR, .xzrg_front = 0, .xzrg_base = GiB(38) + MiB(16), .xzrg_size = (XZM_POINTER_RANGE_SIZE / 2) - MiB(16), .xzrg_skip_addr = 0, .xzrg_skip_size = 0, .xzrg_next = GiB(38) + MiB(16), .xzrg_remaining = (XZM_POINTER_RANGE_SIZE / 2) - MiB(16), .xzrg_direction = XZM_FRONT_INCREASING, }, [XZM_RANGE_GROUP_PTR + 1] = { .xzrg_id = XZM_RANGE_GROUP_PTR, .xzrg_front = 1, .xzrg_base = GiB(38) + MiB(16), .xzrg_size = (XZM_POINTER_RANGE_SIZE / 2) + MiB(16), .xzrg_skip_addr = 0, .xzrg_skip_size = 0, .xzrg_next = GiB(38) + MiB(16), .xzrg_remaining = (XZM_POINTER_RANGE_SIZE / 2) + MiB(16), .xzrg_direction = XZM_FRONT_DECREASING, }, } } }; test_ptr_range_setup(&empty_left_beginning); // (63 - 16) - ((10 + 4) + 16) == 17 empty_left_beginning.input.entropy += ((GiB(17) / XZM_PAGE_TABLE_GRANULE) + 1) * 3; empty_left_beginning.desc = "empty left void, ptr range at beginning (entropy offset)"; test_ptr_range_setup(&empty_left_beginning); struct ptr_range_test empty_left_middle = { .desc = "empty left void, ptr range in the middle", .input = { .left_void = { .min_address = GiB(16), .max_address = GiB(16), }, .right_void = { .min_address = GiB(26), .max_address = GiB(63), }, .ptr_range_size = XZM_POINTER_RANGE_SIZE, .entropy = (((GiB(17) / XZM_PAGE_TABLE_GRANULE) + 1) * 42) + (GiB(5) / XZM_PAGE_TABLE_GRANULE), }, .range_output = { .expected_ranges = { { .min_address = GiB(35), .max_address = GiB(35) + XZM_POINTER_RANGE_SIZE, }, }, .range_count_out = 1, }, .range_group_output = { .expected_range_groups = { [XZM_RANGE_GROUP_PTR + 0] = { .xzrg_id = XZM_RANGE_GROUP_PTR, .xzrg_front = 0, .xzrg_base = GiB(43) + MiB(16), .xzrg_size = (XZM_POINTER_RANGE_SIZE / 2) - MiB(16), .xzrg_skip_addr = 0, .xzrg_skip_size = 0, .xzrg_next = GiB(43) + MiB(16), .xzrg_remaining = (XZM_POINTER_RANGE_SIZE / 2) - MiB(16), .xzrg_direction = XZM_FRONT_INCREASING, }, [XZM_RANGE_GROUP_PTR + 1] = { .xzrg_id = XZM_RANGE_GROUP_PTR, .xzrg_front = 1, .xzrg_base = GiB(43) + MiB(16), .xzrg_size = (XZM_POINTER_RANGE_SIZE / 2) + MiB(16), .xzrg_skip_addr = 0, .xzrg_skip_size = 0, .xzrg_next = GiB(43) + MiB(16), .xzrg_remaining = (XZM_POINTER_RANGE_SIZE / 2) + MiB(16), .xzrg_direction = XZM_FRONT_DECREASING, }, } } }; test_ptr_range_setup(&empty_left_middle); struct ptr_range_test empty_left_end = { .desc = "empty left void, ptr range at the end", .input = { .left_void = { .min_address = GiB(16), .max_address = GiB(16), }, .right_void = { .min_address = GiB(26), .max_address = GiB(63), }, .ptr_range_size = XZM_POINTER_RANGE_SIZE, .entropy = (((GiB(17) / XZM_PAGE_TABLE_GRANULE) + 1) * 42) + (GiB(17) / XZM_PAGE_TABLE_GRANULE), }, .range_output = { .expected_ranges = { { .min_address = GiB(47), .max_address = GiB(47) + XZM_POINTER_RANGE_SIZE, }, }, .range_count_out = 1, }, .range_group_output = { .expected_range_groups = { [XZM_RANGE_GROUP_PTR + 0] = { .xzrg_id = XZM_RANGE_GROUP_PTR, .xzrg_front = 0, .xzrg_base = GiB(55) + MiB(16), .xzrg_size = (XZM_POINTER_RANGE_SIZE / 2) - MiB(16), .xzrg_skip_addr = 0, .xzrg_skip_size = 0, .xzrg_next = GiB(55) + MiB(16), .xzrg_remaining = (XZM_POINTER_RANGE_SIZE / 2) - MiB(16), .xzrg_direction = XZM_FRONT_INCREASING, }, [XZM_RANGE_GROUP_PTR + 1] = { .xzrg_id = XZM_RANGE_GROUP_PTR, .xzrg_front = 1, .xzrg_base = GiB(55) + MiB(16), .xzrg_size = (XZM_POINTER_RANGE_SIZE / 2) + MiB(16), .xzrg_skip_addr = 0, .xzrg_skip_size = 0, .xzrg_next = GiB(55) + MiB(16), .xzrg_remaining = (XZM_POINTER_RANGE_SIZE / 2) + MiB(16), .xzrg_direction = XZM_FRONT_DECREASING, }, } } }; test_ptr_range_setup(&empty_left_end); struct ptr_range_test left_void_too_small_beginning = { .desc = "small left void, ptr range at the beginning", .input = { .left_void = { .min_address = GiB(16), .max_address = GiB(17), }, .right_void = { .min_address = GiB(27), .max_address = GiB(63), }, .ptr_range_size = XZM_POINTER_RANGE_SIZE, .entropy = (((GiB(16) / XZM_PAGE_TABLE_GRANULE) + 1) * 42), }, .range_output = { .expected_ranges = { { .min_address = GiB(31), .max_address = GiB(31) + XZM_POINTER_RANGE_SIZE, }, }, .range_count_out = 1, }, .range_group_output = { .expected_range_groups = { [XZM_RANGE_GROUP_PTR + 0] = { .xzrg_id = XZM_RANGE_GROUP_PTR, .xzrg_front = 0, .xzrg_base = GiB(39) + MiB(16), .xzrg_size = (XZM_POINTER_RANGE_SIZE / 2) - MiB(16), .xzrg_skip_addr = 0, .xzrg_skip_size = 0, .xzrg_next = GiB(39) + MiB(16), .xzrg_remaining = (XZM_POINTER_RANGE_SIZE / 2) - MiB(16), .xzrg_direction = XZM_FRONT_INCREASING, }, [XZM_RANGE_GROUP_PTR + 1] = { .xzrg_id = XZM_RANGE_GROUP_PTR, .xzrg_front = 1, .xzrg_base = GiB(39) + MiB(16), .xzrg_size = (XZM_POINTER_RANGE_SIZE / 2) + MiB(16), .xzrg_skip_addr = 0, .xzrg_skip_size = 0, .xzrg_next = GiB(39) + MiB(16), .xzrg_remaining = (XZM_POINTER_RANGE_SIZE / 2) + MiB(16), .xzrg_direction = XZM_FRONT_DECREASING, }, } } }; test_ptr_range_setup(&left_void_too_small_beginning); struct ptr_range_test left_void_split_decreasing = { .desc = "small left void, ptr range split on left", .input = { .left_void = { .min_address = GiB(16), .max_address = GiB(23), }, .right_void = { .min_address = GiB(33), .max_address = GiB(63), }, .ptr_range_size = XZM_POINTER_RANGE_SIZE, .entropy = (((GiB(13) / XZM_PAGE_TABLE_GRANULE) + 1) * 42) + (GiB(1) / XZM_PAGE_TABLE_GRANULE), }, .range_output = { .expected_ranges = { { .min_address = GiB(17), .max_address = GiB(19), }, { .min_address = GiB(37), .max_address = GiB(51), }, }, .range_count_out = 2, }, .range_group_output = { .expected_range_groups = { [XZM_RANGE_GROUP_PTR + 0] = { .xzrg_id = XZM_RANGE_GROUP_PTR, .xzrg_front = 0, .xzrg_base = GiB(43) + MiB(16), .xzrg_size = (XZM_POINTER_RANGE_SIZE / 2) - MiB(16), .xzrg_skip_addr = 0, .xzrg_skip_size = 0, .xzrg_next = GiB(43) + MiB(16), .xzrg_remaining = (XZM_POINTER_RANGE_SIZE / 2) - MiB(16), .xzrg_direction = XZM_FRONT_INCREASING, }, [XZM_RANGE_GROUP_PTR + 1] = { .xzrg_id = XZM_RANGE_GROUP_PTR, .xzrg_front = 1, .xzrg_base = GiB(43) + MiB(16), .xzrg_size = (XZM_POINTER_RANGE_SIZE / 2) + MiB(16), .xzrg_skip_addr = GiB(37), .xzrg_skip_size = GiB(18), .xzrg_next = GiB(43) + MiB(16), .xzrg_remaining = (XZM_POINTER_RANGE_SIZE / 2) + MiB(16), .xzrg_direction = XZM_FRONT_DECREASING, }, } } }; test_ptr_range_setup(&left_void_split_decreasing); struct ptr_range_test split_exact_middle_left = { .desc = "split almost exactly down the middle on the left", .input = { .left_void = { .min_address = GiB(16), .max_address = GiB(30), }, .right_void = { .min_address = GiB(40), .max_address = GiB(63), }, .ptr_range_size = XZM_POINTER_RANGE_SIZE, .entropy = (((GiB(13) / XZM_PAGE_TABLE_GRANULE) + 1) * 42) + (GiB(2) / XZM_PAGE_TABLE_GRANULE), }, .range_output = { .expected_ranges = { { .min_address = GiB(18), .max_address = GiB(26), }, { .min_address = GiB(44), .max_address = GiB(52), }, }, .range_count_out = 2, }, .range_group_output = { .expected_range_groups = { [XZM_RANGE_GROUP_PTR + 0] = { .xzrg_id = XZM_RANGE_GROUP_PTR, .xzrg_front = 0, .xzrg_base = GiB(44) + MiB(16), .xzrg_size = (XZM_POINTER_RANGE_SIZE / 2) - MiB(16), .xzrg_skip_addr = 0, .xzrg_skip_size = 0, .xzrg_next = GiB(44) + MiB(16), .xzrg_remaining = (XZM_POINTER_RANGE_SIZE / 2) - MiB(16), .xzrg_direction = XZM_FRONT_INCREASING, }, [XZM_RANGE_GROUP_PTR + 1] = { .xzrg_id = XZM_RANGE_GROUP_PTR, .xzrg_front = 1, .xzrg_base = GiB(44) + MiB(16), .xzrg_size = (XZM_POINTER_RANGE_SIZE / 2) + MiB(16), .xzrg_skip_addr = GiB(44), .xzrg_skip_size = GiB(18), .xzrg_next = GiB(44) + MiB(16), .xzrg_remaining = (XZM_POINTER_RANGE_SIZE / 2) + MiB(16), .xzrg_direction = XZM_FRONT_DECREASING, }, } } }; test_ptr_range_setup(&split_exact_middle_left); struct ptr_range_test split_exact_middle_right = { .desc = "split almost exactly down the middle on the right", .input = { .left_void = { .min_address = GiB(16), .max_address = GiB(30), }, .right_void = { .min_address = GiB(40), .max_address = GiB(63), }, .ptr_range_size = XZM_POINTER_RANGE_SIZE, .entropy = (((GiB(13) / XZM_PAGE_TABLE_GRANULE) + 1) * 42) + ((GiB(2) - MiB(32)) / XZM_PAGE_TABLE_GRANULE), }, .range_output = { .expected_ranges = { { .min_address = GiB(18) - MiB(32), .max_address = GiB(26), }, { .min_address = GiB(44), .max_address = GiB(52) - MiB(32), }, }, .range_count_out = 2, }, .range_group_output = { .expected_range_groups = { [XZM_RANGE_GROUP_PTR + 0] = { .xzrg_id = XZM_RANGE_GROUP_PTR, .xzrg_front = 0, .xzrg_base = (GiB(26) - MiB(32)) + MiB(16), .xzrg_size = (XZM_POINTER_RANGE_SIZE / 2) - MiB(16), .xzrg_skip_addr = GiB(26), .xzrg_skip_size = GiB(18), .xzrg_next = (GiB(26) - MiB(32)) + MiB(16), .xzrg_remaining = (XZM_POINTER_RANGE_SIZE / 2) - MiB(16), .xzrg_direction = XZM_FRONT_INCREASING, }, [XZM_RANGE_GROUP_PTR + 1] = { .xzrg_id = XZM_RANGE_GROUP_PTR, .xzrg_front = 1, .xzrg_base = (GiB(26) - MiB(32)) + MiB(16), .xzrg_size = (XZM_POINTER_RANGE_SIZE / 2) + MiB(16), .xzrg_skip_addr = 0, .xzrg_skip_size = 0, .xzrg_next = (GiB(26) - MiB(32)) + MiB(16), .xzrg_remaining = (XZM_POINTER_RANGE_SIZE / 2) + MiB(16), .xzrg_direction = XZM_FRONT_DECREASING, }, } } }; test_ptr_range_setup(&split_exact_middle_right); struct ptr_range_test empty_right_void_end = { .desc = "empty right void, last possible position", .input = { .left_void = { .min_address = GiB(16), .max_address = GiB(53), }, .right_void = { .min_address = GiB(63), .max_address = GiB(63), }, .ptr_range_size = XZM_POINTER_RANGE_SIZE, .entropy = (((GiB(17) / XZM_PAGE_TABLE_GRANULE) + 1) * 42) + (GiB(17) / XZM_PAGE_TABLE_GRANULE), }, .range_output = { .expected_ranges = { { .min_address = GiB(33), .max_address = GiB(49), }, }, .range_count_out = 1, }, .range_group_output = { .expected_range_groups = { [XZM_RANGE_GROUP_PTR + 0] = { .xzrg_id = XZM_RANGE_GROUP_PTR, .xzrg_front = 0, .xzrg_base = GiB(41) + MiB(16), .xzrg_size = (XZM_POINTER_RANGE_SIZE / 2) - MiB(16), .xzrg_skip_addr = 0, .xzrg_skip_size = 0, .xzrg_next = GiB(41) + MiB(16), .xzrg_remaining = (XZM_POINTER_RANGE_SIZE / 2) - MiB(16), .xzrg_direction = XZM_FRONT_INCREASING, }, [XZM_RANGE_GROUP_PTR + 1] = { .xzrg_id = XZM_RANGE_GROUP_PTR, .xzrg_front = 1, .xzrg_base = GiB(41) + MiB(16), .xzrg_size = (XZM_POINTER_RANGE_SIZE / 2) + MiB(16), .xzrg_skip_addr = 0, .xzrg_skip_size = 0, .xzrg_next = GiB(41) + MiB(16), .xzrg_remaining = (XZM_POINTER_RANGE_SIZE / 2) + MiB(16), .xzrg_direction = XZM_FRONT_DECREASING, }, } } }; test_ptr_range_setup(&empty_right_void_end); } #else // CONFIG_XZONE_MALLOC && CONFIG_VM_USER_RANGES T_DECL(xzm_segment_not_supported, "xzone segment tests not supported", T_META_ENABLED(false)) { T_SKIP("xzone segment tests not supported on this platform"); } #endif // CONFIG_XZONE_MALLOC && CONFIG_VM_USER_RANGES