| OLD | NEW |
| (Empty) | |
| 1 #include "base/allocator/allocator_shim.h" |
| 2 #include "base/logging.h" |
| 3 #include "base/process/memory.h" |
| 4 |
| 5 volatile bool did_call_new_handler = false; |
| 6 |
| 7 static void NewHandler() { |
| 8 printf("Did call the std::new_handler. SUCCESS.\n"); |
| 9 fflush(stdout); |
| 10 exit(0); |
| 11 } |
| 12 |
| 13 extern "C" void __attribute__((visibility("default"))) Hax() { |
| 14 void* ptr; |
| 15 |
| 16 // Normal malloc/free - no problems |
| 17 ptr = malloc(64); |
| 18 free(ptr); |
| 19 |
| 20 // Unchecked malloc/free - problems! |
| 21 CHECK(base::UncheckedMalloc(42, &ptr)); |
| 22 free(ptr); |
| 23 |
| 24 printf("malloc and unchecked malloc test passed, now testing SetCallNewHandler
OnMallocFailure\n"); |
| 25 |
| 26 std::set_new_handler(&NewHandler); |
| 27 base::allocator::SetCallNewHandlerOnMallocFailure(true); |
| 28 while(true) { |
| 29 const size_t kAllocSize = 128 * 1024 * 102; |
| 30 volatile uint8_t* x = (volatile uint8_t*) malloc(kAllocSize); |
| 31 if (!x) { |
| 32 printf("Bummer, malloc returned nullptr without calling the new handler. F
AIL\n"); |
| 33 exit(1); |
| 34 break; |
| 35 } |
| 36 x[0] = 1; |
| 37 x[kAllocSize - 1] = 1; |
| 38 } |
| 39 |
| 40 } |
| OLD | NEW |