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 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 | #define MFM_TESTING 1 #include <malloc/malloc.h> #include <stdlib.h> #include <darwintest.h> #if defined(__LP64__) #include "../src/internal.h" // MALLOC_TARGET_EXCLAVES #if !MALLOC_TARGET_EXCLAVES #include <mach/mach_init.h> #include <mach/vm_map.h> #endif // !MALLOC_TARGET_EXCLAVES T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true), T_META_TAG_VM_PREFERRED); static void * test_mvm_allocate_pages(size_t size, int vm_page_label, plat_map_t *map_out) { #if MALLOC_TARGET_EXCLAVES return mmap_plat(map_out, 0, size, LIBLIBC_MAP_PERM_READ | LIBLIBC_MAP_PERM_WRITE, LIBLIBC_MAP_TYPE_PRIVATE, 0, vm_page_label); #else vm_address_t vm_addr; kern_return_t kr; kr = vm_allocate(mach_task_self(), &vm_addr, size, VM_FLAGS_ANYWHERE | VM_MAKE_TAG(vm_page_label)); return kr == KERN_SUCCESS ? (void *)vm_addr : NULL; #endif // MALLOC_TARGET_EXCLAVES } #define mvm_allocate_pages_plat(size, align, debug_flags, vm_page_label, plat) \ test_mvm_allocate_pages(size, vm_page_label, plat) static void * test_mvm_allocate_plat(uintptr_t addr, size_t size, int flags, uint32_t debug_flags, int vm_page_label, plat_map_t *map_out) { #if MALLOC_TARGET_EXCLAVES const _liblibc_map_type_t type = LIBLIBC_MAP_TYPE_PRIVATE | ((debug_flags & MALLOC_CAN_FAULT) ? LIBLIBC_MAP_TYPE_FAULTABLE : LIBLIBC_MAP_TYPE_NONE) | ((debug_flags & MALLOC_NO_POPULATE) ? LIBLIBC_MAP_TYPE_NOCOMMIT : LIBLIBC_MAP_TYPE_NONE) | ((flags & VM_FLAGS_ANYWHERE) ? 0 : LIBLIBC_MAP_TYPE_FIXED); return mmap_plat(map_out, addr, size, LIBLIBC_MAP_PERM_READ | LIBLIBC_MAP_PERM_WRITE, type, 0, (unsigned)vm_page_label); #else vm_address_t vm_addr; kern_return_t kr; kr = vm_allocate(mach_task_self(), &vm_addr, size, VM_FLAGS_ANYWHERE | VM_MAKE_TAG(vm_page_label)); return kr == KERN_SUCCESS ? (void *)vm_addr : NULL; #endif // MALLOC_TARGET_EXCLAVES } #define mvm_allocate_plat(addr, size, align, flags, debug_flags, vm_page_label, plat) \ test_mvm_allocate_plat(addr, size, flags, debug_flags, vm_page_label, plat) static int test_mvm_madvise_plat(void *addr, size_t sz, int advice, unsigned debug_flags, plat_map_t *map) { kern_return_t kr; #if MALLOC_TARGET_EXCLAVES kr = !madvise_plat(map, addr, sz, advice) ? KERN_SUCCESS : errno; #else kr = !madvise(addr, sz, advice) ? KERN_SUCCESS : errno; #endif // MALLOC_TARGET_EXCLAVES return !(kr == KERN_SUCCESS); } #define mvm_madvise_plat(addr, size, advice, debug_flags, map) \ test_mvm_madvise_plat(addr, size, advice, debug_flags, map) static void test_malloc_lock_lock(_malloc_lock_s *lock) { #if MALLOC_HAS_OS_LOCK os_unfair_lock_lock(lock); #else T_QUIET; T_ASSERT_EQ(pthread_mutex_lock(lock), 0, "Lock lock"); #endif // MALLOC_HAS_OS_LOCK } #define _malloc_lock_lock(lock) test_malloc_lock_lock(lock); static void test_malloc_lock_unlock(_malloc_lock_s *lock) { #if MALLOC_HAS_OS_LOCK os_unfair_lock_unlock(lock); #else T_QUIET; T_ASSERT_EQ(pthread_mutex_unlock(lock), 0, "Unlock lock"); #endif // MALLOC_HAS_OS_LOCK } #define _malloc_lock_unlock(lock) test_malloc_lock_unlock(lock); #include "../src/early_malloc.c" T_DECL(mfm_basic, "mfm basic tests") { void *ptrs[10]; size_t index, size; mfm_initialize(); T_ASSERT_NOTNULL(mfm_arena, "mfm_initialize worked"); T_ASSERT_NULL(mfm_alloc(MFM_ALLOC_SIZE_MAX + 1), "allocations larger than %zd bytes should fail", MFM_ALLOC_SIZE_MAX); T_ASSERT_EQ(0ul, mfm_arena->mfmh_bump * MFM_QUANTUM, "The bump should be at 0"); ptrs[0] = mfm_alloc(10); T_EXPECT_EQ(16ul, mfm_alloc_size(ptrs[0]), "allocation should be 16 bytes"); index = __mfm_block_index(mfm_arena, ptrs[0]); size = __mfm_block_size(mfm_arena, index); T_QUIET; T_EXPECT_TRUE(__mfm_block_is_allocated(mfm_arena, index), "allocated"); T_QUIET; T_EXPECT_TRUE(!__mfm_block_is_allocated(mfm_arena, index + size), "allocated"); T_QUIET; T_EXPECT_TRUE(__mfm_prev_block_is_allocated(mfm_arena, index + size), "allocated"); ptrs[1] = mfm_alloc(10); T_EXPECT_EQ(16ul, mfm_alloc_size(ptrs[1]), "allocation should be 16 bytes"); index = __mfm_block_index(mfm_arena, ptrs[1]); size = __mfm_block_size(mfm_arena, index); T_QUIET; T_EXPECT_TRUE(__mfm_block_is_allocated(mfm_arena, index), "allocated"); T_QUIET; T_EXPECT_TRUE(!__mfm_block_is_allocated(mfm_arena, index + size), "allocated"); T_QUIET; T_EXPECT_TRUE(__mfm_prev_block_is_allocated(mfm_arena, index + size), "allocated"); ptrs[2] = mfm_alloc(128); T_EXPECT_EQ(128ul, mfm_alloc_size(ptrs[2]), "allocation should be 128 bytes"); index = __mfm_block_index(mfm_arena, ptrs[2]); size = __mfm_block_size(mfm_arena, index); T_QUIET; T_EXPECT_TRUE(__mfm_block_is_allocated(mfm_arena, index), "allocated"); T_QUIET; T_EXPECT_TRUE(!__mfm_block_is_allocated(mfm_arena, index + size), "allocated"); T_QUIET; T_EXPECT_TRUE(__mfm_prev_block_is_allocated(mfm_arena, index + size), "allocated"); ptrs[3] = mfm_alloc(1024); T_EXPECT_EQ(1024ul, mfm_alloc_size(ptrs[3]), "allocation should be 1024 bytes"); index = __mfm_block_index(mfm_arena, ptrs[3]); size = __mfm_block_size(mfm_arena, index); T_QUIET; T_EXPECT_TRUE(__mfm_block_is_allocated(mfm_arena, index), "allocated"); T_QUIET; T_EXPECT_TRUE(!__mfm_block_is_allocated(mfm_arena, index + size), "allocated"); T_QUIET; T_EXPECT_TRUE(__mfm_prev_block_is_allocated(mfm_arena, index + size), "allocated"); T_ASSERT_EQ(1184ul, mfm_arena->mfmh_bump * MFM_QUANTUM, "The bump should be at 1184"); mfm_free(ptrs[1]); mfm_free(ptrs[2]); index = __mfm_block_index(mfm_arena, ptrs[1]); size = __mfm_block_size(mfm_arena, index); T_EXPECT_EQ(size * MFM_QUANTUM, 144ul, "freed block should be 144 bytes"); T_QUIET; T_EXPECT_TRUE(!__mfm_block_is_allocated(mfm_arena, index), "allocated"); T_QUIET; T_EXPECT_TRUE(__mfm_block_is_allocated(mfm_arena, index + size), "allocated"); ptrs[4] = mfm_alloc(64); T_EXPECT_EQ(64ul, mfm_alloc_size(ptrs[4]), "allocation should be 64 bytes"); index = __mfm_block_index(mfm_arena, ptrs[4]); size = __mfm_block_size(mfm_arena, index); T_QUIET; T_EXPECT_TRUE(__mfm_block_is_allocated(mfm_arena, index), "allocated"); T_QUIET; T_EXPECT_TRUE(!__mfm_block_is_allocated(mfm_arena, index + size), "allocated"); T_QUIET; T_EXPECT_TRUE(__mfm_prev_block_is_allocated(mfm_arena, index + size), "allocated"); index += size; size = __mfm_block_size(mfm_arena, index); T_EXPECT_EQ(size * MFM_QUANTUM, 80ul, "freed block should be 80 bytes"); T_QUIET; T_EXPECT_TRUE(!__mfm_block_is_allocated(mfm_arena, index), "allocated"); T_QUIET; T_EXPECT_TRUE(__mfm_block_is_allocated(mfm_arena, index + size), "allocated"); mfm_free(ptrs[3]); T_ASSERT_EQ(80ul, mfm_arena->mfmh_bump * MFM_QUANTUM, "The bump should be at 80"); ptrs[5] = mfm_alloc(64); T_EXPECT_EQ(64ul, mfm_alloc_size(ptrs[5]), "allocation should be 64 bytes"); index = __mfm_block_index(mfm_arena, ptrs[5]); size = __mfm_block_size(mfm_arena, index); T_QUIET; T_EXPECT_TRUE(__mfm_block_is_allocated(mfm_arena, index), "allocated"); T_QUIET; T_EXPECT_TRUE(!__mfm_block_is_allocated(mfm_arena, index + size), "allocated"); T_QUIET; T_EXPECT_TRUE(__mfm_prev_block_is_allocated(mfm_arena, index + size), "allocated"); T_ASSERT_EQ(144ul, mfm_arena->mfmh_bump * MFM_QUANTUM, "The bump should be at 144"); print_mfm_arena(mfm_arena, true, (print_task_printer_t *)printf); } T_DECL(mfm_bits, "mfm bitmaps tests") { mfm_initialize(); T_ASSERT_NOTNULL(mfm_arena, "mfm_initialize worked"); for (size_t n = 0; n < 2 * 128 * 128; n++) { size_t i, j, k, t; void *ptrs[3]; size_t idx[3]; t = n; i = 1 + (t % 128); t /= 128; j = 1 + (t % 128); t /= 128; k = t; ptrs[0] = mfm_alloc(i * MFM_QUANTUM); ptrs[1] = mfm_alloc(j * MFM_QUANTUM); ptrs[2] = mfm_alloc(MFM_QUANTUM); idx[0] = __mfm_block_index(mfm_arena, ptrs[0]); idx[1] = __mfm_block_index(mfm_arena, ptrs[1]); idx[2] = __mfm_block_index(mfm_arena, ptrs[2]); T_QUIET; T_ASSERT_EQ(i, __mfm_block_size(mfm_arena, idx[0]), NULL); T_QUIET; T_ASSERT_EQ(i, __mfm_prev_block_size(mfm_arena, idx[1]), NULL); T_QUIET; T_ASSERT_EQ(j, __mfm_block_size(mfm_arena, idx[1]), NULL); T_QUIET; T_ASSERT_EQ(j, __mfm_prev_block_size(mfm_arena, idx[2]), NULL); mfm_free(ptrs[k]); T_QUIET; T_ASSERT_EQ(i, __mfm_block_size(mfm_arena, idx[0]), NULL); T_QUIET; T_ASSERT_EQ(i, __mfm_prev_block_size(mfm_arena, idx[1]), NULL); T_QUIET; T_ASSERT_EQ(j, __mfm_block_size(mfm_arena, idx[1]), NULL); T_QUIET; T_ASSERT_EQ(j, __mfm_prev_block_size(mfm_arena, idx[2]), NULL); mfm_free(ptrs[1 - k]); T_QUIET; T_ASSERT_EQ(i + j, __mfm_block_size(mfm_arena, idx[0]), NULL); T_QUIET; T_ASSERT_EQ(i + j, __mfm_prev_block_size(mfm_arena, idx[2]), NULL); mfm_free(ptrs[2]); T_ASSERT_EQ(mfm_arena->mfmh_bump, 0ul, "alloc(%zd * 16), alloc(%zd * 16), mfm_alloc(16), free(%zd), free(%zd))", i, j, k, 1 - k); } } struct record { ssize_t size; uintptr_t addr; }; struct alloc_state { size_t count; struct record *allocs; }; static void alloc_add(struct alloc_state *state, size_t size, uintptr_t addr) { state->allocs[state->count].size = (ssize_t)size; state->allocs[state->count].addr = addr; state->count++; } static void alloc_rm(struct alloc_state *state, uintptr_t addr) { for (size_t i = 0; i < state->count; i++) { if (state->allocs[i].addr != addr) { continue; } if (i + 1 <= state->count) { state->allocs[i] = state->allocs[state->count - 1]; state->allocs[state->count - 1].addr = 0; state->allocs[state->count - 1].size = 0; } state->count--; return; } T_FAIL("Couldn't find address %p", (void *)addr); } static size_t block_size(size_t alloc_size) { return roundup(alloc_size ?: 1, MFM_QUANTUM); } static void run_corruption_test(const struct record *recs, size_t count) { uintptr_t rec_base, mfm_base; struct alloc_state state; state.count = 0; state.allocs = calloc(count, sizeof(struct record)); T_ASSERT_NOTNULL(state.allocs, "could allocate state"); mfm_initialize(); T_ASSERT_NOTNULL(mfm_arena, "mfm_initialize worked"); rec_base = recs[0].addr; mfm_base = (uintptr_t)mfm_arena->mfm_blocks; for (size_t i = 0; i < count; i++) { const struct record *rec = &recs[i]; if (rec->size >= 0) { void *ptr = mfm_alloc(rec->size); size_t size = mfm_alloc_size(ptr); size_t want_size = block_size(rec->size); #if MFM_TRACE T_LOG("[%zd] mfm_alloc(%zd) = %p", i, rec->size, ptr); print_mfm_arena(mfm_arena, true, (print_task_printer_t *)printf); #endif T_QUIET; T_ASSERT_EQ(want_size, size, "size for %zd", i); T_QUIET; T_ASSERT_EQ((uintptr_t)ptr - mfm_base, rec->addr - rec_base, "ptr for %zd", i); alloc_add(&state, size, (uintptr_t)ptr); } else { void *ptr = (void *)(rec->addr + mfm_base - rec_base); #if MFM_TRACE T_LOG("[%zd] mfm_free(%p, %zd)", i, ptr, mfm_alloc_size(ptr)); #endif mfm_free(ptr); #if MFM_TRACE print_mfm_arena(mfm_arena, true, (print_task_printer_t *)printf); #endif alloc_rm(&state, (uintptr_t)ptr); } for (size_t j = 0; j < state.count; j++) { struct record *record = &state.allocs[j]; T_QUIET; T_EXPECT_EQ(mfm_alloc_size((void *)record->addr), block_size(record->size), "alloc %p is live", (void *)record->addr); } } print_mfm_arena(mfm_arena, true, (print_task_printer_t *)printf); T_PASS("Made it to the end"); free(state.allocs); } T_DECL(mfm_corruption, "mfm corruption test") { static const struct record recs[] = { #include "mfm/trace_1.in" }; run_corruption_test(recs, sizeof(recs) / sizeof(struct record)); } T_DECL(mfm_corruption2, "mfm corruption test") { static const struct record recs[] = { #include "mfm/trace_2.in" }; run_corruption_test(recs, sizeof(recs) / sizeof(struct record)); } T_DECL(mfm_corruption3, "mfm corruption test") { static const struct record recs[] = { #include "mfm/trace_3.in" }; run_corruption_test(recs, sizeof(recs) / sizeof(struct record)); } T_DECL(mfm_corruption4, "mfm corruption test") { static const struct record recs[] = { #include "mfm/trace_4.in" }; run_corruption_test(recs, sizeof(recs) / sizeof(struct record)); } #else // defined(__LP64__) T_DECL(mfm_not_supported, "mfm not supported") { T_SKIP("mfm not supported on this platform"); } #endif // defined(__LP64__) |