|
| 1 | +#ifndef MALLOC_GLUE_H |
| 2 | +#define MALLOC_GLUE_H |
| 3 | + |
| 4 | +#include <stdint.h> |
| 5 | +#include <sys/mman.h> |
| 6 | +#include <pthread.h> |
| 7 | +#include <unistd.h> |
| 8 | +#include <elf.h> |
| 9 | +#include <string.h> |
| 10 | +#include "atomic.h" |
| 11 | +#include "syscall.h" |
| 12 | +#include "libc.h" |
| 13 | +#include "lock.h" |
| 14 | +#include "dynlink.h" |
| 15 | + |
| 16 | +// use macros to appropriately namespace these. |
| 17 | +#define size_classes __malloc_size_classes |
| 18 | +#define ctx __malloc_context |
| 19 | +#define alloc_meta __malloc_alloc_meta |
| 20 | +#define is_allzero __malloc_allzerop |
| 21 | +#define dump_heap __dump_heap |
| 22 | + |
| 23 | +#if USE_REAL_ASSERT |
| 24 | +#include <assert.h> |
| 25 | +#else |
| 26 | +#undef assert |
| 27 | +#define assert(x) do { if (!(x)) a_crash(); } while(0) |
| 28 | +#endif |
| 29 | + |
| 30 | +#define brk(p) ((uintptr_t)__syscall(SYS_brk, p)) |
| 31 | + |
| 32 | +#define mmap __mmap |
| 33 | +#define madvise __madvise |
| 34 | +#define mremap __mremap |
| 35 | + |
| 36 | +#define DISABLE_ALIGNED_ALLOC (__malloc_replaced && !__aligned_alloc_replaced) |
| 37 | + |
| 38 | +static inline uint64_t get_random_secret() |
| 39 | +{ |
| 40 | + uint64_t secret = (uintptr_t)&secret * 1103515245; |
| 41 | + for (size_t i=0; libc.auxv[i]; i+=2) |
| 42 | + if (libc.auxv[i]==AT_RANDOM) |
| 43 | + memcpy(&secret, (char *)libc.auxv[i+1]+8, sizeof secret); |
| 44 | + return secret; |
| 45 | +} |
| 46 | + |
| 47 | +#ifndef PAGESIZE |
| 48 | +#define PAGESIZE PAGE_SIZE |
| 49 | +#endif |
| 50 | + |
| 51 | +#define MT (libc.need_locks) |
| 52 | + |
| 53 | +#define RDLOCK_IS_EXCLUSIVE 1 |
| 54 | + |
| 55 | +__attribute__((__visibility__("hidden"))) |
| 56 | +extern int __malloc_lock[1]; |
| 57 | + |
| 58 | +#define LOCK_OBJ_DEF \ |
| 59 | +int __malloc_lock[1]; |
| 60 | + |
| 61 | +static inline void rdlock() |
| 62 | +{ |
| 63 | + if (MT) LOCK(__malloc_lock); |
| 64 | +} |
| 65 | +static inline void wrlock() |
| 66 | +{ |
| 67 | + if (MT) LOCK(__malloc_lock); |
| 68 | +} |
| 69 | +static inline void unlock() |
| 70 | +{ |
| 71 | + UNLOCK(__malloc_lock); |
| 72 | +} |
| 73 | +static inline void upgradelock() |
| 74 | +{ |
| 75 | +} |
| 76 | + |
| 77 | +#endif |
0 commit comments