[09f3093] | 1 | // ***************************************************************************** |
---|
| 2 | // Copyright (C) 2015, UChicago Argonne, LLC |
---|
| 3 | // All Rights Reserved |
---|
| 4 | // memlog (ANL-SF-15-081) |
---|
| 5 | // Hal Finkel, Argonne National Laboratory |
---|
| 6 | // |
---|
| 7 | // OPEN SOURCE LICENSE |
---|
| 8 | // |
---|
| 9 | // Under the terms of Contract No. DE-AC02-06CH11357 with UChicago Argonne, LLC, |
---|
| 10 | // the U.S. Government retains certain rights in this software. |
---|
| 11 | // |
---|
| 12 | // Redistribution and use in source and binary forms, with or without |
---|
| 13 | // modification, are permitted provided that the following conditions are met: |
---|
| 14 | // |
---|
| 15 | // 1. Redistributions of source code must retain the above copyright notice, this |
---|
| 16 | // list of conditions and the following disclaimer. |
---|
| 17 | // |
---|
| 18 | // 2. Redistributions in binary form must reproduce the above copyright notice, |
---|
| 19 | // this list of conditions and the following disclaimer in the documentation |
---|
| 20 | // and/or other materials provided with the distribution. |
---|
| 21 | // |
---|
| 22 | // 3. Neither the names of UChicago Argonne, LLC or the Department of Energy nor |
---|
| 23 | // the names of its contributors may be used to endorse or promote products |
---|
| 24 | // derived from this software without specific prior written permission. |
---|
| 25 | // |
---|
| 26 | // ***************************************************************************** |
---|
| 27 | // DISCLAIMER |
---|
| 28 | // |
---|
[9f07fa4] | 29 | // THE SOFTWARE IS SUPPLIED "AS IS" WITHOUT WARRANTY OF ANY KIND. |
---|
[09f3093] | 30 | // |
---|
| 31 | // NEITHER THE UNTED STATES GOVERNMENT, NOR THE UNITED STATES DEPARTMENT OF |
---|
| 32 | // ENERGY, NOR UCHICAGO ARGONNE, LLC, NOR ANY OF THEIR EMPLOYEES, MAKES ANY |
---|
| 33 | // WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY LEGAL LIABILITY OR RESPONSIBILITY |
---|
| 34 | // FOR THE ACCURACY, COMPLETENESS, OR USEFULNESS OF ANY INFORMATION, DATA, |
---|
| 35 | // APPARATUS, PRODUCT, OR PROCESS DISCLOSED, OR REPRESENTS THAT ITS USE WOULD NOT |
---|
| 36 | // INFRINGE PRIVATELY OWNED RIGHTS. |
---|
| 37 | // |
---|
| 38 | // ***************************************************************************** |
---|
| 39 | |
---|
[0ec59c5] | 40 | #ifndef _GNU_SOURCE |
---|
| 41 | #define _GNU_SOURCE |
---|
| 42 | #endif |
---|
| 43 | |
---|
[1bd82e0] | 44 | #include <cstdlib> |
---|
| 45 | #include <cstdio> |
---|
| 46 | #include <cstring> |
---|
[21d3542] | 47 | #include <cstdint> |
---|
[1bd82e0] | 48 | |
---|
[f715c76] | 49 | // NOTE: This source makes very minimal use of C++11 features. It can still be |
---|
| 50 | // compiled by g++ 4.4.7 with -std=gnu++0x. |
---|
[1bd82e0] | 51 | #include <unordered_map> |
---|
| 52 | #include <utility> |
---|
[0ec59c5] | 53 | |
---|
[1bd82e0] | 54 | #include <limits.h> |
---|
[4c51931] | 55 | #include <errno.h> |
---|
[430548b] | 56 | #include <malloc.h> |
---|
[a736d81] | 57 | #include <execinfo.h> |
---|
[6444e03] | 58 | #include <sys/mman.h> |
---|
[134408c] | 59 | #include <sys/syscall.h> |
---|
[0a0ef57] | 60 | #include <sys/time.h> |
---|
| 61 | #include <sys/resource.h> |
---|
[0ec59c5] | 62 | #include <sys/types.h> |
---|
| 63 | #include <sys/stat.h> |
---|
| 64 | #include <sys/utsname.h> |
---|
| 65 | #include <fcntl.h> |
---|
| 66 | #include <unistd.h> |
---|
| 67 | |
---|
| 68 | #include <pthread.h> |
---|
| 69 | #include <dlfcn.h> |
---|
| 70 | |
---|
[a7b97b9] | 71 | #ifdef __bgq__ |
---|
| 72 | #include <spi/include/kernel/location.h> |
---|
[21d3542] | 73 | #include <spi/include/kernel/memory.h> |
---|
[a7b97b9] | 74 | #endif |
---|
| 75 | |
---|
[1bd82e0] | 76 | using namespace std; |
---|
| 77 | |
---|
[493cb97] | 78 | // NOTE: When static linking, this depends on linker wrapping. |
---|
| 79 | // Add to your LDFLAGS: |
---|
| 80 | // -Wl,--wrap,malloc,--wrap,free,--wrap,realloc,--wrap,calloc,--wrap,memalign /path/to/memlog_s.o -lpthread -ldl |
---|
| 81 | |
---|
[4c51931] | 82 | static FILE *log_file = 0; |
---|
[0ec59c5] | 83 | static pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER; |
---|
| 84 | |
---|
[69850c8] | 85 | // The malloc hook might use functions that call malloc, and we need to make |
---|
| 86 | // sure this does not cause an infinite loop. |
---|
| 87 | static __thread int in_malloc = 0; |
---|
[3105f50] | 88 | static char self_path[PATH_MAX+1] = { '\0' }; |
---|
[69850c8] | 89 | |
---|
[21d3542] | 90 | #ifdef __bgq__ |
---|
[4c51931] | 91 | static int on_bgq = 0; |
---|
[21d3542] | 92 | #endif |
---|
| 93 | |
---|
[4c51931] | 94 | static void *initial_brk = 0; |
---|
| 95 | |
---|
| 96 | static unordered_map<void *, Dl_info> *dladdr_cache = 0; |
---|
[21d3542] | 97 | |
---|
[0ec59c5] | 98 | __attribute__((__constructor__)) |
---|
| 99 | static void record_init() { |
---|
| 100 | struct utsname u; |
---|
| 101 | uname(&u); |
---|
| 102 | |
---|
[a7b97b9] | 103 | int id = (int) getpid(); |
---|
| 104 | #ifdef __bgq__ |
---|
| 105 | // If we're really running on a BG/Q compute node, use the job rank instead |
---|
| 106 | // of the pid because the node name might not really be globally unique. |
---|
[21d3542] | 107 | if (!strcmp(u.sysname, "CNK") && !strcmp(u.machine, "BGQ")) { |
---|
[a7b97b9] | 108 | id = (int) Kernel_GetRank(); |
---|
[21d3542] | 109 | on_bgq = 1; |
---|
| 110 | } |
---|
[a7b97b9] | 111 | #endif |
---|
| 112 | |
---|
[22f928f] | 113 | // If we're running under a common batch system, add the job id to the output |
---|
| 114 | // file names (add it as a prefix so that sorting the files will sort by job |
---|
| 115 | // first). |
---|
| 116 | char *job_id = 0; |
---|
| 117 | const char *job_id_vars[] = |
---|
| 118 | { "COBALT_JOBID", "PBS_JOBID", "SLURM_JOB_ID", "JOB_ID" }; |
---|
| 119 | for (int i = 0; i < sizeof(job_id_vars)/sizeof(job_id_vars[0]); ++i) { |
---|
| 120 | job_id = getenv(job_id_vars[i]); |
---|
| 121 | if (job_id) |
---|
| 122 | break; |
---|
| 123 | } |
---|
| 124 | |
---|
[3105f50] | 125 | char log_name[PATH_MAX+1]; |
---|
[22f928f] | 126 | if (job_id) |
---|
| 127 | snprintf(log_name, PATH_MAX+1, "%s.%s.%d.memlog", job_id, u.nodename, id); |
---|
| 128 | else |
---|
| 129 | snprintf(log_name, PATH_MAX+1, "%s.%d.memlog", u.nodename, id); |
---|
[0ec59c5] | 130 | log_file = fopen(log_name, "w"); |
---|
[0a0ef57] | 131 | if (!log_file) |
---|
| 132 | fprintf(stderr, "fopen failed for '%s': %m\n", log_name); |
---|
[3105f50] | 133 | |
---|
| 134 | const char *link_name = "/proc/self/exe"; |
---|
| 135 | readlink(link_name, self_path, PATH_MAX); |
---|
[21d3542] | 136 | |
---|
| 137 | initial_brk = sbrk(0); |
---|
[0ec59c5] | 138 | } |
---|
| 139 | |
---|
| 140 | __attribute__((__destructor__)) |
---|
| 141 | static void record_cleanup() { |
---|
| 142 | if (!log_file) |
---|
| 143 | return; |
---|
| 144 | |
---|
[69850c8] | 145 | // These functions might call free, but we're shutting down, so don't try to |
---|
| 146 | // unwind the stack from here... |
---|
| 147 | in_malloc = 1; |
---|
| 148 | |
---|
[5a9481e] | 149 | // Avoid any racing by obtaining the lock. |
---|
| 150 | if (pthread_mutex_lock(&log_mutex)) |
---|
| 151 | return; |
---|
| 152 | |
---|
[0ec59c5] | 153 | (void) fflush(log_file); |
---|
| 154 | (void) fclose(log_file); |
---|
[4c51931] | 155 | |
---|
| 156 | if (dladdr_cache) |
---|
| 157 | delete dladdr_cache; |
---|
[0ec59c5] | 158 | } |
---|
| 159 | |
---|
[5a9481e] | 160 | // dladdr is, relatively, quit slow. For this to work on a large application, |
---|
| 161 | // we need to cache the lookup results. |
---|
[1bd82e0] | 162 | static int dladdr_cached(void * addr, Dl_info *info) { |
---|
[4c51931] | 163 | if (!dladdr_cache) |
---|
| 164 | dladdr_cache = new unordered_map<void *, Dl_info>; |
---|
[1bd82e0] | 165 | |
---|
[4c51931] | 166 | auto I = dladdr_cache->find(addr); |
---|
| 167 | if (I == dladdr_cache->end()) { |
---|
[1bd82e0] | 168 | int r; |
---|
| 169 | if (!(r = dladdr(addr, info))) |
---|
| 170 | memset(info, 0, sizeof(Dl_info)); |
---|
| 171 | |
---|
[4c51931] | 172 | dladdr_cache->insert(make_pair(addr, *info)); |
---|
[1bd82e0] | 173 | return r; |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | memcpy(info, &I->second, sizeof(Dl_info)); |
---|
| 177 | return 1; |
---|
[5a9481e] | 178 | } |
---|
| 179 | |
---|
[1e5cce6] | 180 | static void print_context(const void *caller, int show_backtrace) { |
---|
[0a0ef57] | 181 | struct rusage usage; |
---|
| 182 | if (getrusage(RUSAGE_SELF, &usage)) { |
---|
| 183 | fprintf(stderr, "getrusage failed: %m\n"); |
---|
[0ec59c5] | 184 | return; |
---|
[0a0ef57] | 185 | } |
---|
[0ec59c5] | 186 | |
---|
[134408c] | 187 | fprintf(log_file, "\t%ld.%06ld %ld %ld", usage.ru_utime.tv_sec, |
---|
| 188 | usage.ru_utime.tv_usec, usage.ru_maxrss, syscall(SYS_gettid)); |
---|
[0a0ef57] | 189 | |
---|
[21d3542] | 190 | // Some other memory stats (like with maxrss, report these in KB). |
---|
| 191 | size_t arena_size = ((size_t) sbrk(0)) - (size_t) initial_brk; |
---|
| 192 | |
---|
| 193 | uint64_t mmap_size = 0; |
---|
| 194 | #ifdef __bgq__ |
---|
| 195 | if (on_bgq) |
---|
| 196 | (void) Kernel_GetMemorySize(KERNEL_MEMSIZE_MMAP, &mmap_size); |
---|
| 197 | #endif |
---|
| 198 | |
---|
| 199 | fprintf(log_file, " %ld %ld", arena_size >> 10, mmap_size >> 10); |
---|
| 200 | |
---|
[1e5cce6] | 201 | if (!show_backtrace) |
---|
| 202 | return; |
---|
| 203 | |
---|
[a736d81] | 204 | void *pcs[1024]; |
---|
| 205 | int num_pcs = backtrace(pcs, 1024); |
---|
[14e2ab9] | 206 | |
---|
| 207 | int found_caller = 0; |
---|
[a736d81] | 208 | for (int pci = 0; pci < num_pcs; ++pci) { |
---|
[14e2ab9] | 209 | intptr_t pc = (intptr_t) pcs[pci]; |
---|
[69850c8] | 210 | |
---|
| 211 | if (!pc) |
---|
| 212 | break; |
---|
| 213 | |
---|
[14e2ab9] | 214 | if (!found_caller) { |
---|
| 215 | if (pc != (intptr_t) caller) |
---|
| 216 | continue; |
---|
| 217 | |
---|
| 218 | found_caller = 1; |
---|
| 219 | } |
---|
| 220 | |
---|
| 221 | intptr_t off, relpc; |
---|
[69850c8] | 222 | const char *proc_name; |
---|
| 223 | const char *file_name; |
---|
| 224 | Dl_info dlinfo; |
---|
[5a9481e] | 225 | if (dladdr_cached((void *) pc, &dlinfo) && dlinfo.dli_fname && |
---|
[69850c8] | 226 | *dlinfo.dli_fname) { |
---|
[14e2ab9] | 227 | intptr_t saddr = (intptr_t) dlinfo.dli_saddr; |
---|
[69850c8] | 228 | if (saddr) { |
---|
| 229 | #if defined(__powerpc64__) && !defined(__powerpc64le__) |
---|
| 230 | // On PPC64 ELFv1, the symbol address points to the function descriptor, not |
---|
| 231 | // the actual starting address. |
---|
[14e2ab9] | 232 | saddr = *(intptr_t*) saddr; |
---|
[69850c8] | 233 | #endif |
---|
| 234 | |
---|
| 235 | off = pc - saddr; |
---|
[14e2ab9] | 236 | relpc = pc - ((intptr_t) dlinfo.dli_fbase); |
---|
[69850c8] | 237 | } else { |
---|
| 238 | off = 0; |
---|
| 239 | relpc = 0; |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | proc_name = dlinfo.dli_sname; |
---|
| 243 | if (!proc_name) |
---|
| 244 | proc_name = "?"; |
---|
| 245 | |
---|
| 246 | file_name = dlinfo.dli_fname; |
---|
| 247 | } else { |
---|
[3105f50] | 248 | // We don't know these... |
---|
| 249 | off = 0; |
---|
| 250 | relpc = 0; |
---|
[69850c8] | 251 | proc_name = "?"; |
---|
[3105f50] | 252 | |
---|
| 253 | // If we can't determine the file, assume it is the base executable |
---|
| 254 | // (which does the right thing for statically-linked binaries). |
---|
| 255 | file_name = self_path; |
---|
[69850c8] | 256 | } |
---|
| 257 | |
---|
| 258 | fprintf(log_file, "\t%s (%s+0x%x) [0x%lx (0x%lx)]", file_name, proc_name, (int) off, |
---|
| 259 | (long) pc, (long) relpc); |
---|
[0ec59c5] | 260 | } |
---|
| 261 | } |
---|
| 262 | |
---|
[430548b] | 263 | static void record_malloc(size_t size, void *ptr, const void *caller) { |
---|
[0ec59c5] | 264 | if (!log_file) |
---|
| 265 | return; |
---|
| 266 | |
---|
| 267 | if (pthread_mutex_lock(&log_mutex)) |
---|
| 268 | return; |
---|
| 269 | |
---|
| 270 | fprintf(log_file, "M: %zd %p", size, ptr); |
---|
[1e5cce6] | 271 | print_context(caller, 1); |
---|
[0ec59c5] | 272 | fprintf(log_file, "\n"); |
---|
| 273 | |
---|
| 274 | done: |
---|
| 275 | pthread_mutex_unlock(&log_mutex); |
---|
| 276 | } |
---|
| 277 | |
---|
[430548b] | 278 | static void record_free(void *ptr, const void *caller) { |
---|
[0ec59c5] | 279 | if (!log_file) |
---|
| 280 | return; |
---|
| 281 | |
---|
| 282 | if (pthread_mutex_lock(&log_mutex)) |
---|
| 283 | return; |
---|
| 284 | |
---|
| 285 | fprintf(log_file, "F: %p", ptr); |
---|
[1e5cce6] | 286 | print_context(caller, 0); |
---|
[0ec59c5] | 287 | fprintf(log_file, "\n"); |
---|
| 288 | |
---|
| 289 | done: |
---|
| 290 | pthread_mutex_unlock(&log_mutex); |
---|
| 291 | } |
---|
| 292 | |
---|
[ed8b809] | 293 | #ifdef __PIC__ |
---|
[4c51931] | 294 | static int (*__real_posix_memalign)(void **memptr, size_t alignment, |
---|
| 295 | size_t size) = 0; |
---|
| 296 | |
---|
| 297 | static void *(*__real_mmap)(void *addr, size_t length, int prot, int flags, |
---|
| 298 | int fd, off_t offset) = 0; |
---|
| 299 | static void *(*__real_mmap64)(void *addr, size_t length, int prot, int flags, |
---|
| 300 | int fd, off64_t offset) = 0; |
---|
| 301 | static int (*__real_munmap)(void *addr, size_t length) = 0; |
---|
[ed8b809] | 302 | #else |
---|
| 303 | extern "C" { |
---|
| 304 | extern int __real_posix_memalign(void **memptr, size_t alignment, size_t size); |
---|
| 305 | |
---|
| 306 | extern void *__real_mmap(void *addr, size_t length, int prot, int flags, |
---|
| 307 | int fd, off_t offset); |
---|
| 308 | extern void *__real_mmap64(void *addr, size_t length, int prot, int flags, |
---|
| 309 | int fd, off64_t offset); |
---|
| 310 | extern int __real_munmap(void *addr, size_t length); |
---|
| 311 | } |
---|
| 312 | #endif |
---|
| 313 | |
---|
[0ec59c5] | 314 | // glibc exports its underlying malloc implementation under the name |
---|
| 315 | // __libc_malloc so that hooks like this can use it. |
---|
[1bd82e0] | 316 | extern "C" { |
---|
[0ec59c5] | 317 | extern void *__libc_malloc(size_t size); |
---|
[6444e03] | 318 | extern void *__libc_valloc(size_t size); |
---|
[0ec59c5] | 319 | extern void *__libc_realloc(void *ptr, size_t size); |
---|
| 320 | extern void *__libc_calloc(size_t nmemb, size_t size); |
---|
| 321 | extern void *__libc_memalign(size_t boundary, size_t size); |
---|
| 322 | extern void __libc_free(void *ptr); |
---|
| 323 | |
---|
[430548b] | 324 | #ifdef __PIC__ |
---|
| 325 | #define FUNC(x) x |
---|
| 326 | #else |
---|
| 327 | #define FUNC(x) __wrap_ ## x |
---|
| 328 | #endif |
---|
| 329 | |
---|
| 330 | void *FUNC(malloc)(size_t size) { |
---|
[5a9481e] | 331 | const void *caller = |
---|
| 332 | __builtin_extract_return_addr(__builtin_return_address(0)); |
---|
[430548b] | 333 | |
---|
[0ec59c5] | 334 | if (in_malloc) |
---|
| 335 | return __libc_malloc(size); |
---|
| 336 | |
---|
| 337 | in_malloc = 1; |
---|
| 338 | |
---|
| 339 | void *ptr = __libc_malloc(size); |
---|
[6444e03] | 340 | if (ptr) |
---|
| 341 | record_malloc(size, ptr, caller); |
---|
[0ec59c5] | 342 | |
---|
[6444e03] | 343 | in_malloc = 0; |
---|
| 344 | return ptr; |
---|
| 345 | } |
---|
| 346 | |
---|
| 347 | void *FUNC(valloc)(size_t size) { |
---|
| 348 | const void *caller = |
---|
| 349 | __builtin_extract_return_addr(__builtin_return_address(0)); |
---|
[0ec59c5] | 350 | |
---|
[6444e03] | 351 | if (in_malloc) |
---|
| 352 | return __libc_valloc(size); |
---|
| 353 | |
---|
| 354 | in_malloc = 1; |
---|
| 355 | |
---|
| 356 | void *ptr = __libc_valloc(size); |
---|
| 357 | if (ptr) |
---|
| 358 | record_malloc(size, ptr, caller); |
---|
[0ec59c5] | 359 | |
---|
| 360 | in_malloc = 0; |
---|
| 361 | return ptr; |
---|
| 362 | } |
---|
| 363 | |
---|
[430548b] | 364 | void *FUNC(realloc)(void *ptr, size_t size) { |
---|
[5a9481e] | 365 | const void *caller = |
---|
| 366 | __builtin_extract_return_addr(__builtin_return_address(0)); |
---|
[430548b] | 367 | |
---|
[0ec59c5] | 368 | if (in_malloc) |
---|
| 369 | return __libc_realloc(ptr, size); |
---|
| 370 | |
---|
| 371 | in_malloc = 1; |
---|
| 372 | |
---|
| 373 | void *nptr = __libc_realloc(ptr, size); |
---|
| 374 | |
---|
[14e2ab9] | 375 | if (ptr) |
---|
| 376 | record_free(ptr, caller); |
---|
[6444e03] | 377 | if (nptr) |
---|
| 378 | record_malloc(size, nptr, caller); |
---|
[0ec59c5] | 379 | |
---|
| 380 | in_malloc = 0; |
---|
| 381 | |
---|
| 382 | return nptr; |
---|
| 383 | } |
---|
| 384 | |
---|
[430548b] | 385 | void *FUNC(calloc)(size_t nmemb, size_t size) { |
---|
[5a9481e] | 386 | const void *caller = |
---|
| 387 | __builtin_extract_return_addr(__builtin_return_address(0)); |
---|
[430548b] | 388 | |
---|
[0ec59c5] | 389 | if (in_malloc) |
---|
| 390 | return __libc_calloc(nmemb, size); |
---|
| 391 | |
---|
| 392 | in_malloc = 1; |
---|
| 393 | |
---|
| 394 | void *ptr = __libc_calloc(nmemb, size); |
---|
| 395 | |
---|
[6444e03] | 396 | if (ptr) |
---|
| 397 | record_malloc(nmemb*size, ptr, caller); |
---|
[0ec59c5] | 398 | |
---|
| 399 | in_malloc = 0; |
---|
| 400 | |
---|
| 401 | return ptr; |
---|
| 402 | } |
---|
| 403 | |
---|
[430548b] | 404 | void *FUNC(memalign)(size_t boundary, size_t size) { |
---|
[5a9481e] | 405 | const void *caller = |
---|
| 406 | __builtin_extract_return_addr(__builtin_return_address(0)); |
---|
[430548b] | 407 | |
---|
[0ec59c5] | 408 | if (in_malloc) |
---|
| 409 | return __libc_memalign(boundary, size); |
---|
| 410 | |
---|
| 411 | in_malloc = 1; |
---|
| 412 | |
---|
| 413 | void *ptr = __libc_memalign(boundary, size); |
---|
| 414 | |
---|
[6444e03] | 415 | if (ptr) |
---|
| 416 | record_malloc(size, ptr, caller); |
---|
[0ec59c5] | 417 | |
---|
| 418 | in_malloc = 0; |
---|
| 419 | |
---|
| 420 | return ptr; |
---|
| 421 | } |
---|
| 422 | |
---|
[430548b] | 423 | void FUNC(free)(void *ptr) { |
---|
[5a9481e] | 424 | const void *caller = |
---|
| 425 | __builtin_extract_return_addr(__builtin_return_address(0)); |
---|
[430548b] | 426 | |
---|
[14e2ab9] | 427 | if (in_malloc || !ptr) |
---|
[0ec59c5] | 428 | return __libc_free(ptr); |
---|
| 429 | |
---|
| 430 | in_malloc = 1; |
---|
| 431 | |
---|
[14e2ab9] | 432 | record_free(ptr, caller); |
---|
[0ec59c5] | 433 | |
---|
| 434 | __libc_free(ptr); |
---|
| 435 | |
---|
| 436 | in_malloc = 0; |
---|
| 437 | } |
---|
| 438 | |
---|
[6444e03] | 439 | int FUNC(posix_memalign)(void **memptr, size_t alignment, size_t size) { |
---|
| 440 | const void *caller = |
---|
| 441 | __builtin_extract_return_addr(__builtin_return_address(0)); |
---|
| 442 | |
---|
| 443 | #ifdef __PIC__ |
---|
| 444 | if (!__real_posix_memalign) |
---|
[ed8b809] | 445 | if (!(*(void **) (&__real_posix_memalign) = |
---|
| 446 | dlsym(RTLD_NEXT, "posix_memalign"))) { |
---|
| 447 | return ELIBACC; |
---|
| 448 | } |
---|
[6444e03] | 449 | #endif |
---|
| 450 | |
---|
| 451 | if (in_malloc) |
---|
| 452 | return __real_posix_memalign(memptr, alignment, size); |
---|
| 453 | |
---|
| 454 | in_malloc = 1; |
---|
| 455 | |
---|
| 456 | int r = __real_posix_memalign(memptr, alignment, size); |
---|
| 457 | |
---|
| 458 | if (!r) |
---|
| 459 | record_malloc(size, *memptr, caller); |
---|
| 460 | |
---|
| 461 | in_malloc = 0; |
---|
| 462 | |
---|
| 463 | return r; |
---|
| 464 | } |
---|
| 465 | |
---|
| 466 | void *FUNC(mmap)(void *addr, size_t length, int prot, int flags, |
---|
| 467 | int fd, off_t offset) { |
---|
| 468 | const void *caller = |
---|
| 469 | __builtin_extract_return_addr(__builtin_return_address(0)); |
---|
| 470 | |
---|
[ed8b809] | 471 | #ifdef __PIC__ |
---|
| 472 | if (!__real_mmap) |
---|
| 473 | if (!(*(void **) (&__real_mmap) = dlsym(RTLD_NEXT, "mmap"))) { |
---|
| 474 | errno = ELIBACC; |
---|
| 475 | return MAP_FAILED; |
---|
| 476 | } |
---|
| 477 | #endif |
---|
| 478 | |
---|
[6444e03] | 479 | if (in_malloc) |
---|
[ed8b809] | 480 | return __real_mmap(addr, length, prot, flags, fd, offset); |
---|
[6444e03] | 481 | |
---|
| 482 | in_malloc = 1; |
---|
| 483 | |
---|
[ed8b809] | 484 | void *ptr = __real_mmap(addr, length, prot, flags, fd, offset); |
---|
[6444e03] | 485 | |
---|
| 486 | if (ptr != MAP_FAILED) |
---|
| 487 | record_malloc(length, ptr, caller); |
---|
| 488 | |
---|
| 489 | in_malloc = 0; |
---|
| 490 | |
---|
| 491 | return ptr; |
---|
| 492 | } |
---|
| 493 | |
---|
| 494 | void *FUNC(mmap64)(void *addr, size_t length, int prot, int flags, |
---|
| 495 | int fd, off64_t offset) { |
---|
| 496 | const void *caller = |
---|
| 497 | __builtin_extract_return_addr(__builtin_return_address(0)); |
---|
| 498 | |
---|
[ed8b809] | 499 | #ifdef __PIC__ |
---|
| 500 | if (!__real_mmap64) |
---|
| 501 | if (!(*(void **) (&__real_mmap64) = dlsym(RTLD_NEXT, "mmap64"))) { |
---|
| 502 | errno = ELIBACC; |
---|
| 503 | return MAP_FAILED; |
---|
| 504 | } |
---|
| 505 | #endif |
---|
| 506 | |
---|
[6444e03] | 507 | if (in_malloc) |
---|
[ed8b809] | 508 | return __real_mmap64(addr, length, prot, flags, fd, offset); |
---|
[6444e03] | 509 | |
---|
| 510 | in_malloc = 1; |
---|
| 511 | |
---|
[ed8b809] | 512 | void *ptr = __real_mmap64(addr, length, prot, flags, fd, offset); |
---|
[6444e03] | 513 | |
---|
| 514 | if (ptr != MAP_FAILED) |
---|
| 515 | record_malloc(length, ptr, caller); |
---|
| 516 | |
---|
| 517 | in_malloc = 0; |
---|
| 518 | |
---|
| 519 | return ptr; |
---|
| 520 | } |
---|
| 521 | |
---|
| 522 | int FUNC(munmap)(void *addr, size_t length) { |
---|
| 523 | const void *caller = |
---|
| 524 | __builtin_extract_return_addr(__builtin_return_address(0)); |
---|
| 525 | |
---|
[ed8b809] | 526 | #ifdef __PIC__ |
---|
| 527 | if (!__real_munmap) |
---|
| 528 | if (!(*(void **) (&__real_munmap) = dlsym(RTLD_NEXT, "munmap"))) { |
---|
| 529 | errno = ELIBACC; |
---|
| 530 | return -1; |
---|
| 531 | } |
---|
| 532 | #endif |
---|
| 533 | |
---|
[6444e03] | 534 | if (in_malloc) |
---|
[ed8b809] | 535 | return __real_munmap(addr, length); |
---|
[6444e03] | 536 | |
---|
| 537 | in_malloc = 1; |
---|
| 538 | |
---|
| 539 | record_free(addr, caller); |
---|
| 540 | |
---|
[ed8b809] | 541 | int r = __real_munmap(addr, length); |
---|
[6444e03] | 542 | |
---|
| 543 | in_malloc = 0; |
---|
| 544 | |
---|
| 545 | return r; |
---|
| 546 | } |
---|
| 547 | |
---|
[1bd82e0] | 548 | } // extern "C" |
---|
| 549 | |
---|