| 128 | FreeFunc real_free = nullptr; |
| 129 | |
| 130 | void init_real_allocators() { |
| 131 | if (real_malloc) return; |
| 132 | |
| 133 | HMODULE ucrt = GetModuleHandleA("ucrtbase.dll"); |
| 134 | if (!ucrt) ucrt = GetModuleHandleA("msvcrt.dll"); |
| 135 | |
| 136 | if (ucrt) { |
| 137 | real_malloc = (MallocFunc)GetProcAddress(ucrt, "malloc"); |
| 138 | real_calloc = (CallocFunc)GetProcAddress(ucrt, "calloc"); |
| 139 | real_realloc = (ReallocFunc)GetProcAddress(ucrt, "realloc"); |
| 140 | real_free = (FreeFunc)GetProcAddress(ucrt, "free"); |
| 141 | } |
| 142 | |
| 143 | // If we can't find the real allocator, abort (can't fallback to malloc in override) |
| 144 | if (!real_malloc) { |
| 145 | printf("FATAL: Cannot find real malloc in ucrtbase.dll or msvcrt.dll\n"); |
| 146 | abort(); |
| 147 | } |
| 148 | } |
| 149 | #else |
| 150 | void* real_malloc(size_t size) { |
| 151 | static auto fn = (void* (*)(size_t))dlsym(RTLD_NEXT, "malloc"); |