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 | // // AllocatorTests.m // UnitTests // // Created by Louis Gerbarg on 12/27/20. // #include <map> #include <set> #include <vector> #import "DyldTestCase.h" #include "Allocator.h" using namespace dyld4; struct AllocOperation { size_t size; size_t alignment; char pattern; AllocOperation(size_t S, size_t A, char P) : size(S), alignment(A), pattern(P) {} }; struct TestOperation { AllocOperation* op; size_t idx; bool isAllocation; TestOperation(AllocOperation* O, size_t I, bool A) : op(O), idx(I), isAllocation(A) {} }; @interface AllocatorTests : DyldTestCase { std::vector<AllocOperation> _testVector; std::vector<TestOperation> _testOperations; size_t _testOperationMaxSize; size_t _smallAndMediumAllocationVectorMaxIndex; size_t _allocationVectorMaxIndex; Allocator _allocator; } @end @implementation AllocatorTests - (void)setUp { // FIrst build a set of small operations for (auto i = 0; i < 1024; ++i) { size_t size = [self uniformRandomFrom:0 to:1023]; char pattern = [self uniformRandomFrom:0 to:255]; auto alignment = 1 << [self uniformRandomFrom:4 to:7]; _testVector.emplace_back(size, alignment, pattern); } _testVector.emplace_back(4096, PAGE_SIZE, [self uniformRandomFrom:0 to:255]); _testVector.emplace_back(16384, PAGE_SIZE, [self uniformRandomFrom:0 to:255]); _testVector.emplace_back(65536, PAGE_SIZE, [self uniformRandomFrom:0 to:255]); _smallAndMediumAllocationVectorMaxIndex = _testVector.size()-1; // Add a couple of elements that are large enough that they will be allocated up stream _testVector.emplace_back(1024*1024*2, 16, [self uniformRandomFrom:0 to:255]); _testVector.emplace_back(1024*1024*3, 16, [self uniformRandomFrom:0 to:255]); _allocationVectorMaxIndex = _testVector.size()-1; } - (void) buildAllocationOperationsTestVector:(bool)includeLargeOperations { size_t targetPoolSize = (includeLargeOperations ? (10*1024*1024) : (512*1024)); uint64_t maxIndex = (includeLargeOperations ? _allocationVectorMaxIndex : _smallAndMediumAllocationVectorMaxIndex); uint64_t poolCycles = [self uniformRandomFrom:10 to:100]; size_t spaceUsed = 0; std::vector<bool> liveIdxes; std::vector<TestOperation> liveAllocations; for (auto i = 0; i < poolCycles; ++i) { bool growing = true; while(1) { // First we grow the pool, so bias in favor of allocations bool isAllocation = true; if (liveAllocations.size() != 0) { if (growing) { isAllocation = ([self uniformRandomFrom:0 to:2] > 0) ? true : false; } else { isAllocation = ([self uniformRandomFrom:0 to:2] > 0) ? false : true; } } if (isAllocation) { size_t allocationIndex = [self uniformRandomFrom:0 to:maxIndex]; size_t targetAlignment = std::max(16UL, _testVector[allocationIndex].alignment); size_t targetSize = (_testVector[allocationIndex].size + (targetAlignment-1)) & (-1*targetAlignment); spaceUsed += targetSize; auto j = std::find(liveIdxes.begin(), liveIdxes.end(), false); if (j == liveIdxes.end()) { j = liveIdxes.insert(liveIdxes.end(), true); } else { *j = true; } auto op = TestOperation(&_testVector[allocationIndex], j-liveIdxes.begin(), true); _testOperations.push_back(op); liveAllocations.push_back(op); } else { size_t liveAllocationIndex = [self uniformRandomFrom:0 to:liveAllocations.size()-1]; auto op = *(liveAllocations.begin() + liveAllocationIndex); size_t targetAlignment = std::max(16UL, _testVector[op.idx].alignment); size_t targetSize = (_testVector[op.idx].size + (targetAlignment-1)) & (-1*targetAlignment); spaceUsed -= targetSize; op.isAllocation = false; liveIdxes.begin()[op.idx] = false; _testOperations.push_back(op); liveAllocations.erase(liveAllocations.begin() + liveAllocationIndex); } if (growing && (spaceUsed >= targetPoolSize)) { growing = false; } if (!growing && (spaceUsed <= targetPoolSize/2)) { break; } } } _testOperationMaxSize = liveIdxes.size(); // Drain the pool while (liveAllocations.size()) { size_t liveAllocationIndex = [self uniformRandomFrom:0 to:liveAllocations.size()-1]; auto op = *(liveAllocations.begin() + liveAllocationIndex); op.isAllocation = false; liveIdxes.begin()[op.idx] = false; _testOperations.push_back(op); liveAllocations.erase(liveAllocations.begin() + liveAllocationIndex); } } - (void)tearDown { // Put teardown code here. This method is called after the invocation of each test method in the class. } - (void)verifyBuffer:(Allocator::Buffer)buffer pattern:(uint8_t)pattern { const uint8_t* charBuffer = (uint8_t*)buffer.address; uint64_t patternBuffer = 0; // Hacky manually optimization, the XCTAssert macro defeats loop unrolling, // so we manually unroll this to speed up verification for (auto i = 0; i < 8; ++i) { patternBuffer <<= 8; patternBuffer |= pattern; } size_t i = 0; for (i = 0; i < buffer.size/8; ++i) { XCTAssert(*(uint64_t*)&charBuffer[i] == patternBuffer, "failed 0x%lx[%zu] == %u\n", (uintptr_t)charBuffer, i, (unsigned char)pattern); } for (; i < buffer.size; ++i) { XCTAssert(charBuffer[i] == pattern, "failed 0x%lx[%zu] == %u\n", (uintptr_t)charBuffer, i, (unsigned char)pattern); } } - (void) runRandomAllocatorTests:(Allocator&)allocator verify:(bool)verify { auto liveAllocations = (std::pair<Allocator::Buffer,AllocOperation*>*)malloc(sizeof(std::pair<Allocator::Buffer,uint8_t>)*_testOperationMaxSize); bzero((void*)liveAllocations, sizeof(std::pair<Allocator::Buffer,AllocOperation*>)*_testOperationMaxSize); uint64_t count = 0; for (auto op : _testOperations) { if (op.isAllocation) { auto buffer = allocator.allocate_buffer(op.op->size, op.op->alignment); allocator.validateFreeList(); // printf("Allocated(%lu) 0x%lx\n", op.idx, (uintptr_t)buffer.address); // printf("\tsize: %zu, align = %zu, pattern: %u \n", op.op->size, op.op->alignment, (uint8_t)op.op->pattern); liveAllocations[op.idx].first = buffer; if (verify) { memset(buffer.address, op.op->pattern, buffer.size); liveAllocations[op.idx].second = op.op; } } else { allocator.deallocate_buffer(liveAllocations[op.idx].first); allocator.validateFreeList(); // printf("\tsize: %zu, align = %zu, pattern: %u \n", op.op->size, op.op->alignment, (uint8_t)op.op->pattern); liveAllocations[op.idx] = { Allocator::Buffer(), (AllocOperation*)nullptr }; } // We only check every 100th iteration in order to speed up test runs. If crashes happen change this to validate every // modification ++count; if (verify && (count%100 == 0)) { for (auto j = &liveAllocations[0]; j != &liveAllocations[_testOperationMaxSize]; ++j) { if (!j->first.valid()) { continue; } [self verifyBuffer:j->first pattern:j->second->pattern]; } } } free((void*)liveAllocations); XCTAssert(allocator.allocated_bytes() == 0, "allocator.allocated_bytes() = %zu\n", allocator.allocated_bytes()); } - (void) runRandomMallocTests:(Allocator&)allocator verify:(bool)verify { auto liveAllocations = (std::pair<void*,AllocOperation*>*)malloc(sizeof(std::pair<void*,uint8_t>)*_testOperationMaxSize); bzero((void*)liveAllocations, sizeof(std::pair<void*,AllocOperation*>)*_testOperationMaxSize); uint64_t count = 0; for (auto op : _testOperations) { if (op.isAllocation) { auto buffer = allocator.aligned_alloc(op.op->alignment, op.op->size); allocator.validateFreeList(); // printf("Allocated(%lu) 0x%lx\n", op.idx, (uintptr_t)buffer.address); // printf("\tsize: %zu, align = %zu, pattern: %u \n", op.op->size, op.op->alignment, (uint8_t)op.op->pattern); liveAllocations[op.idx].first = buffer; if (verify) { memset(buffer, op.op->pattern, op.op->size); liveAllocations[op.idx].second = op.op; } } else { allocator.free(liveAllocations[op.idx].first); allocator.validateFreeList(); // printf("\tsize: %zu, align = %zu, pattern: %u \n", op.op->size, op.op->alignment, (uint8_t)op.op->pattern); liveAllocations[op.idx] = { nullptr, (AllocOperation*)nullptr }; } // We only check every 100th iteration in order to speed up test runs. If crashes happen change this to validate every // modification ++count; if (verify && (count%100 == 0)) { for (auto j = &liveAllocations[0]; j != &liveAllocations[_testOperationMaxSize]; ++j) { if (j->first == nullptr) { continue; } [self verifyBuffer:{j->first, j->second->size} pattern:j->second->pattern]; } } } free((void*)liveAllocations); XCTAssert(allocator.allocated_bytes() == 0, "allocator.allocated_bytes() = %zu\n", allocator.allocated_bytes()); } - (void) testMalloc { auto allocator = Allocator(); [self buildAllocationOperationsTestVector:false]; [self runRandomMallocTests:allocator verify:true]; } - (void)testAlloactor { auto allocator = Allocator(); [self buildAllocationOperationsTestVector:false]; [self runRandomAllocatorTests:allocator verify:true]; } static bool sDestructorCalledAtLeastOnce = false; struct TestStruct { TestStruct() = default; TestStruct(uint32_t A, uint32_t B, uint32_t C, uint32_t D) : a(A), b(B), c(C), d(D) {} ~TestStruct() { sDestructorCalledAtLeastOnce = true; } uint32_t a = 0; uint32_t b = 1; uint32_t c = 2; uint32_t d = 3; }; - (void) testUniquePtr { sDestructorCalledAtLeastOnce = false; auto allocator = Allocator(); { UniquePtr<TestStruct> purposefullyUnusedToTestNullHandling; auto test1 = allocator.makeUnique<TestStruct>(); auto test2 = allocator.makeUnique<TestStruct>(4, 5, 6, 7); XCTAssertEqual(test1->a, 0); XCTAssertEqual(test1->b, 1); XCTAssertEqual(test1->c, 2); XCTAssertEqual(test1->d, 3); XCTAssertEqual(test2->a, 4); XCTAssertEqual(test2->b, 5); XCTAssertEqual(test2->c, 6); XCTAssertEqual(test2->d, 7); test1->a = 8; test1->b = 9; test1->c = 10; test1->d = 11; XCTAssertEqual(test1->a, 8); XCTAssertEqual(test1->b, 9); XCTAssertEqual(test1->c, 10); XCTAssertEqual(test1->d, 11); UniquePtr<TestStruct> test0; test0 = std::move(test1); XCTAssert((bool)test1 == false); XCTAssertEqual(test0->a, 8); XCTAssertEqual(test0->b, 9); XCTAssertEqual(test0->c, 10); XCTAssertEqual(test0->d, 11); test2 = allocator.makeUnique<TestStruct>(); XCTAssertTrue(sDestructorCalledAtLeastOnce); sDestructorCalledAtLeastOnce = false; } XCTAssertTrue(sDestructorCalledAtLeastOnce); } - (void) testSharedPtr { sDestructorCalledAtLeastOnce = false; auto allocator = Allocator(); { SharedPtr<TestStruct> purposefullyUnusedToTestNullHandling; auto test1 = allocator.makeShared<TestStruct>(); auto test2 = allocator.makeShared<TestStruct>(4, 5, 6, 7); XCTAssertEqual(test1->a, 0); XCTAssertEqual(test1->b, 1); XCTAssertEqual(test1->c, 2); XCTAssertEqual(test1->d, 3); XCTAssertEqual(test2->a, 4); XCTAssertEqual(test2->b, 5); XCTAssertEqual(test2->c, 6); XCTAssertEqual(test2->d, 7); test1->a = 8; test1->b = 9; test1->c = 10; test1->d = 11; XCTAssertEqual(test1->a, 8); XCTAssertEqual(test1->b, 9); XCTAssertEqual(test1->c, 10); XCTAssertEqual(test1->d, 11); SharedPtr<TestStruct> test0; test0 = std::move(test1); XCTAssert((bool)test1 == false); XCTAssertEqual(test0->a, 8); XCTAssertEqual(test0->b, 9); XCTAssertEqual(test0->c, 10); XCTAssertEqual(test0->d, 11); test1 = test0; XCTAssertEqual(test1->a, 8); XCTAssertEqual(test1->b, 9); XCTAssertEqual(test1->c, 10); XCTAssertEqual(test1->d, 11); XCTAssertEqual(test1.get(), test0.get()); test2 = allocator.makeShared<TestStruct>(); XCTAssertTrue(sDestructorCalledAtLeastOnce); sDestructorCalledAtLeastOnce = false; } XCTAssertTrue(sDestructorCalledAtLeastOnce); } - (void) testAllocatorNullHandling { // There are no XCTAsserts here, if test does not crash it passed auto allocator = Allocator(); auto nullUnique = allocator.makeUnique<char *>(nullptr); auto nullShared = allocator.makeShared<char *>(nullptr); staticFree(nullptr); } //FIXME: We need mach exception handling for this test to make sense - (void) disabledTestWriteProtect { auto allocator = Allocator(); auto buffer = allocator.allocate_buffer(128, 16); memset(buffer.address, 0x1f, buffer.size); allocator.writeProtect(true); [self verifyBuffer:buffer pattern:0x1f]; // FIXME: Implement mach exception handling on this to test write protection and make sure we do not regress it // memset(buffer.address, 0x2e, buffer.size); allocator.writeProtect(false); allocator.deallocate_bytes(buffer.address, buffer.size, 16); XCTAssert(allocator.allocated_bytes() == 0, "allocator.allocated_bytes() = %zu\n", allocator.allocated_bytes()); } - (void) testAllocatorPerformance { Allocator localAllocator; __block auto& allocator = localAllocator; [self buildAllocationOperationsTestVector:false]; [self measureBlock:^{ [self runRandomAllocatorTests:allocator verify:false]; }]; } - (void) testMallocPerformance { Allocator localAllocator; __block auto& allocator = localAllocator; [self buildAllocationOperationsTestVector:false]; [self measureBlock:^{ [self runRandomMallocTests:allocator verify:false]; }]; } @end |