1570af302Sopenharmony_ci#ifndef MALLOC_GLUE_H 2570af302Sopenharmony_ci#define MALLOC_GLUE_H 3570af302Sopenharmony_ci 4570af302Sopenharmony_ci#include <stdint.h> 5570af302Sopenharmony_ci#include <sys/mman.h> 6570af302Sopenharmony_ci#include <pthread.h> 7570af302Sopenharmony_ci#include <unistd.h> 8570af302Sopenharmony_ci#include <elf.h> 9570af302Sopenharmony_ci#include <string.h> 10570af302Sopenharmony_ci#include "atomic.h" 11570af302Sopenharmony_ci#include "syscall.h" 12570af302Sopenharmony_ci#include "libc.h" 13570af302Sopenharmony_ci#include "lock.h" 14570af302Sopenharmony_ci#include "dynlink.h" 15570af302Sopenharmony_ci 16570af302Sopenharmony_ci// use macros to appropriately namespace these. 17570af302Sopenharmony_ci#define size_classes __malloc_size_classes 18570af302Sopenharmony_ci#define ctx __malloc_context 19570af302Sopenharmony_ci#define alloc_meta __malloc_alloc_meta 20570af302Sopenharmony_ci#define is_allzero __malloc_allzerop 21570af302Sopenharmony_ci#define dump_heap __dump_heap 22570af302Sopenharmony_ci 23570af302Sopenharmony_ci#define malloc __libc_malloc_impl 24570af302Sopenharmony_ci#define realloc __libc_realloc 25570af302Sopenharmony_ci#define free __libc_free 26570af302Sopenharmony_ci 27570af302Sopenharmony_ci#define USE_MADV_FREE 0 28570af302Sopenharmony_ci 29570af302Sopenharmony_ci#if USE_REAL_ASSERT 30570af302Sopenharmony_ci#include <assert.h> 31570af302Sopenharmony_ci#else 32570af302Sopenharmony_ci#undef assert 33570af302Sopenharmony_ci#define assert(x) do { if (!(x)) a_crash(); } while(0) 34570af302Sopenharmony_ci#endif 35570af302Sopenharmony_ci 36570af302Sopenharmony_ci#define brk(p) ((uintptr_t)__syscall(SYS_brk, p)) 37570af302Sopenharmony_ci 38570af302Sopenharmony_ci#define mmap __mmap 39570af302Sopenharmony_ci#define madvise __madvise 40570af302Sopenharmony_ci#define mremap __mremap 41570af302Sopenharmony_ci 42570af302Sopenharmony_ci#define DISABLE_ALIGNED_ALLOC (__malloc_replaced && !__aligned_alloc_replaced) 43570af302Sopenharmony_ci 44570af302Sopenharmony_cistatic inline uint64_t get_random_secret() 45570af302Sopenharmony_ci{ 46570af302Sopenharmony_ci uint64_t secret = (uintptr_t)&secret * 1103515245; 47570af302Sopenharmony_ci for (size_t i=0; libc.auxv[i]; i+=2) 48570af302Sopenharmony_ci if (libc.auxv[i]==AT_RANDOM) 49570af302Sopenharmony_ci memcpy(&secret, (char *)libc.auxv[i+1]+8, sizeof secret); 50570af302Sopenharmony_ci return secret; 51570af302Sopenharmony_ci} 52570af302Sopenharmony_ci 53570af302Sopenharmony_ci#ifndef PAGESIZE 54570af302Sopenharmony_ci#define PAGESIZE PAGE_SIZE 55570af302Sopenharmony_ci#endif 56570af302Sopenharmony_ci 57570af302Sopenharmony_ci#define MT (libc.need_locks) 58570af302Sopenharmony_ci 59570af302Sopenharmony_ci#define RDLOCK_IS_EXCLUSIVE 1 60570af302Sopenharmony_ci 61570af302Sopenharmony_ci__attribute__((__visibility__("hidden"))) 62570af302Sopenharmony_ciextern int __malloc_lock[1]; 63570af302Sopenharmony_ci 64570af302Sopenharmony_ci#define LOCK_OBJ_DEF \ 65570af302Sopenharmony_ciint __malloc_lock[1]; \ 66570af302Sopenharmony_civoid __malloc_atfork(int who) { malloc_atfork(who); } 67570af302Sopenharmony_ci 68570af302Sopenharmony_cistatic inline void rdlock() 69570af302Sopenharmony_ci{ 70570af302Sopenharmony_ci if (MT) LOCK(__malloc_lock); 71570af302Sopenharmony_ci} 72570af302Sopenharmony_cistatic inline void wrlock() 73570af302Sopenharmony_ci{ 74570af302Sopenharmony_ci if (MT) LOCK(__malloc_lock); 75570af302Sopenharmony_ci} 76570af302Sopenharmony_cistatic inline void unlock() 77570af302Sopenharmony_ci{ 78570af302Sopenharmony_ci UNLOCK(__malloc_lock); 79570af302Sopenharmony_ci} 80570af302Sopenharmony_cistatic inline void upgradelock() 81570af302Sopenharmony_ci{ 82570af302Sopenharmony_ci} 83570af302Sopenharmony_cistatic inline void resetlock() 84570af302Sopenharmony_ci{ 85570af302Sopenharmony_ci __malloc_lock[0] = 0; 86570af302Sopenharmony_ci} 87570af302Sopenharmony_ci 88570af302Sopenharmony_cistatic inline void malloc_atfork(int who) 89570af302Sopenharmony_ci{ 90570af302Sopenharmony_ci if (who<0) rdlock(); 91570af302Sopenharmony_ci else if (who>0) resetlock(); 92570af302Sopenharmony_ci else unlock(); 93570af302Sopenharmony_ci} 94570af302Sopenharmony_ci 95570af302Sopenharmony_ci#endif 96