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 | #import "internal.h" #import <XCTest/XCTest.h> #define XCTAssertNotNull(ptr) XCTAssertNotEqual(ptr, NULL) static void assert_zero(void *ptr, size_t len) { char *p = ptr; // naive zero check for (size_t i = 0; i < len; i++) { XCTAssertEqual(p[i], 0, @"at byte %zu", i); } } static void assert_freelist_block_zero(void *ptr, size_t len, bool cache) { size_t orig_len = len; char *p = ptr; if (!cache) { // Skip the free list pointers p += sizeof(tiny_free_list_t); len -= sizeof(tiny_free_list_t); if (orig_len > TINY_QUANTUM) { // Skip leading inline size p += sizeof(msize_t); len -= sizeof(msize_t); // Skip trailing inline size len -= sizeof(msize_t); } } assert_zero(p, len); } @interface magazine_tiny_tests : XCTestCase { @public struct rack_s tiny_rack; } - (void *)tiny_malloc:(size_t)size; - (void *)tiny_calloc:(size_t)size; - (void)tiny_free:(void *)ptr; @end @implementation magazine_tiny_tests - (void)setUp { malloc_zero_policy = MALLOC_ZERO_ON_FREE; memset(&tiny_rack, 'a', sizeof(tiny_rack)); rack_init(&tiny_rack, RACK_TYPE_TINY, 1, 0); // make an arbitrary initial allocation just to make sure the region isn't // fully free at any point during the subsequent test (void)[self tiny_malloc:42]; } - (void)tearDown { rack_destroy_regions(&tiny_rack, TINY_REGION_SIZE); rack_destroy(&tiny_rack); } - (void *)tiny_malloc:(size_t)size { return tiny_malloc_should_clear(&tiny_rack, TINY_MSIZE_FOR_BYTES(size), false); } - (void *)tiny_calloc:(size_t)size { return tiny_malloc_should_clear(&tiny_rack, TINY_MSIZE_FOR_BYTES(size), true); } - (void)tiny_free:(void *)ptr { region_t region = tiny_region_for_ptr_no_lock(&tiny_rack, ptr); XCTAssertNotNull(region); free_tiny(&tiny_rack, ptr, region, 0, false); } @end @interface magazine_tiny_regular_tests : magazine_tiny_tests @end @implementation magazine_tiny_regular_tests - (void)tearDown { XCTAssertNotEqual(tiny_check(&tiny_rack, 0), 0); [super tearDown]; } - (void)testTinyMallocSucceeds { XCTAssertNotNull([self tiny_malloc:256]); } - (void)testTinyRegionFoundAfterMalloc { void *ptr = [self tiny_malloc:256]; XCTAssertNotNull(ptr); XCTAssertNotNull(tiny_region_for_ptr_no_lock(&tiny_rack, ptr)); } - (void)testTinySizeMatchesMalloc { void *ptr = [self tiny_malloc:256]; XCTAssertNotNull(ptr); XCTAssertEqual(tiny_size(&tiny_rack, ptr), 256); } // A block freed to the tiny cache should be cleared - (void)testTinyZeroOnFreeToCache { const size_t size = 64; void *ptr = [self tiny_malloc:size]; memset(ptr, 'a', size); [self tiny_free:ptr]; assert_freelist_block_zero(ptr, size, true); } // A block coalesced with a previous block should result in a cleared coalesced // block - (void)testTinyZeroOnFreeCoalescePrevious { const size_t size = 272; // skip the tiny cache void *ptr1 = [self tiny_malloc:size]; memset(ptr1, 'a', size); void *ptr2 = [self tiny_malloc:size]; memset(ptr2, 'b', size); XCTAssertEqual((uintptr_t)ptr1 + size, (uintptr_t)ptr2); [self tiny_free:ptr1]; // Should coalesce backward with block 1 [self tiny_free:ptr2]; assert_freelist_block_zero(ptr1, size * 2, false); // Make sure calloc clears void *ptr3 = [self tiny_calloc:size]; XCTAssertEqual(ptr3, ptr1); assert_zero(ptr3, size); } // A block coalesced with a next block should result in a cleared coalesced // block - (void)testTinyZeroOnFreeCoalesceNext { const size_t size = 272; // skip the tiny cache void *ptr1 = [self tiny_malloc:size]; memset(ptr1, 'a', size); void *ptr2 = [self tiny_malloc:size]; memset(ptr2, 'b', size); XCTAssertEqual((uintptr_t)ptr1 + size, (uintptr_t)ptr2); [self tiny_free:ptr2]; // Should coalesce forward with block 2 [self tiny_free:ptr1]; assert_freelist_block_zero(ptr1, size * 2, false); // Make sure calloc clears void *ptr3 = [self tiny_calloc:size]; XCTAssertEqual(ptr3, ptr1); assert_zero(ptr3, size); } // A block coalesced with a small next block should result in a cleared // coalesced block - (void)testTinyZeroOnFreeCoalesceNextSmall { const size_t size = 272; // skip the cache const size_t small_size = 64; // go through the cache void *ptr1 = [self tiny_malloc:size]; memset(ptr1, 'a', size); void *ptr2 = [self tiny_malloc:small_size]; memset(ptr2, 'b', small_size); void *ptr3 = [self tiny_malloc:small_size]; memset(ptr3, 'c', small_size); XCTAssertEqual((uintptr_t)ptr1 + size, (uintptr_t)ptr2); XCTAssertEqual((uintptr_t)ptr2 + small_size, (uintptr_t)ptr3); [self tiny_free:ptr2]; // Push block 2 out of the cache [self tiny_free:ptr3]; // Should coalesce forward with block 2 [self tiny_free:ptr1]; assert_freelist_block_zero(ptr1, size + small_size, false); } // A leftover block should be cleared correctly - (void)testTinyZeroOnFreeLeftover { const size_t size = 272; // skip the tiny cache void *ptr1 = [self tiny_malloc:size]; memset(ptr1, 'a', size); void *ptr2 = [self tiny_malloc:size]; memset(ptr2, 'b', size); void *ptr3 = [self tiny_malloc:size]; memset(ptr3, 'c', size); XCTAssertEqual((uintptr_t)ptr1 + size, (uintptr_t)ptr2); XCTAssertEqual((uintptr_t)ptr2 + size, (uintptr_t)ptr3); // Should all coalesce together [self tiny_free:ptr1]; [self tiny_free:ptr2]; [self tiny_free:ptr3]; // Now pull the first one off again void *ptr4 = [self tiny_malloc:size]; XCTAssertEqual(ptr4, ptr1); // Should get ptr1 back // The leftover starting at ptr2 should be cleared correctly assert_freelist_block_zero(ptr2, size * 2, false); } // A leftover block from realloc should be cleared correctly - (void)testTinyZeroOnFreeReallocLeftover { const size_t size = 272; // skip the tiny cache void *ptr1 = [self tiny_malloc:size]; memset(ptr1, 'a', size); void *ptr2 = [self tiny_malloc:size]; memset(ptr2, 'b', size); void *ptr3 = [self tiny_malloc:size]; memset(ptr3, 'c', size); void *ptr4 = [self tiny_malloc:size]; memset(ptr4, 'd', size); XCTAssertEqual((uintptr_t)ptr1 + size, (uintptr_t)ptr2); XCTAssertEqual((uintptr_t)ptr2 + size, (uintptr_t)ptr3); XCTAssertEqual((uintptr_t)ptr3 + size, (uintptr_t)ptr4); // Should coalesce together [self tiny_free:ptr2]; [self tiny_free:ptr3]; [self tiny_free:ptr4]; // Now grow the first allocation into the free block after, which should // consume up until ptr3 boolean_t result = tiny_try_realloc_in_place(&tiny_rack, ptr1, size, size * 2); XCTAssertEqual(result, 1); // The leftover starting at ptr3 should be cleared correctly assert_freelist_block_zero(ptr3, size * 2, false); } @end @interface magazine_tiny_scribble_tests : magazine_tiny_tests @end @implementation magazine_tiny_scribble_tests - (void)setUp { malloc_zero_policy = MALLOC_ZERO_ON_FREE; aggressive_madvise_enabled = true; memset(&tiny_rack, 'a', sizeof(tiny_rack)); rack_init(&tiny_rack, RACK_TYPE_TINY, 1, MALLOC_DO_SCRIBBLE); // make an arbitrary initial allocation just to make sure the region isn't // fully free at any point during the subsequent test (void)[self tiny_malloc:42]; } // 128 * 256 == 32k #define SCRIBBLE_TEST_ALLOCATIONS 128 - (void)testTinyZeroOnFreeScribbleCalloc { const size_t size = 256; // Allocate and free two full max-size pages, twice, to exercise madvise // scribbling logic for (int outer = 0; outer < 2; outer++) { void *allocations[SCRIBBLE_TEST_ALLOCATIONS]; for (int i = 0; i < SCRIBBLE_TEST_ALLOCATIONS; i++) { allocations[i] = [self tiny_malloc:size]; } for (int i = 0; i < SCRIBBLE_TEST_ALLOCATIONS; i++) { [self tiny_free:allocations[i]]; } } // Now make sure we get back cleared allocations for (int i = 0; i < SCRIBBLE_TEST_ALLOCATIONS; i++) { void *allocation = [self tiny_calloc:size]; assert_zero(allocation, size); } } @end |