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