1 #ifdef HOOK_ENABLE
2 #include <unistd.h>
3 #include <sys/types.h>
4 #include "musl_malloc.h"
5 #include <malloc.h>
6 #include "musl_malloc_dispatch_table.h"
7 #include "common_def.h"
8 #include "musl_preinit_common.h"
9
malloc(size_t bytes)10 void* malloc(size_t bytes)
11 {
12 volatile const struct MallocDispatchType* dispatch_table = (struct MallocDispatchType *)atomic_load_explicit(
13 &__musl_libc_globals.current_dispatch_table, memory_order_acquire);
14 if (__predict_false(dispatch_table != NULL)) {
15 if (__get_memleak_hook_flag()) {
16 return dispatch_table->malloc(bytes);
17 }
18 if (!__get_global_hook_flag()) {
19 return MuslFunc(malloc)(bytes);
20 }
21 else if (!__get_hook_flag()) {
22 return MuslFunc(malloc)(bytes);
23 }
24 return dispatch_table->malloc(bytes);
25 }
26 return MuslFunc(malloc)(bytes);
27 }
28
free(void * mem)29 void free(void* mem)
30 {
31 volatile const struct MallocDispatchType* dispatch_table = (struct MallocDispatchType *)atomic_load_explicit(
32 &__musl_libc_globals.current_dispatch_table, memory_order_acquire);
33 if (__predict_false(dispatch_table != NULL)) {
34 if (__get_memleak_hook_flag()) {
35 dispatch_table->free(mem);
36 return;
37 }
38 if (!__get_global_hook_flag()) {
39 MuslFunc(free)(mem);
40 return;
41 }
42 else if (!__get_hook_flag()) {
43 MuslFunc(free)(mem);
44 return;
45 }
46 dispatch_table->free(mem);
47 return;
48 }
49 MuslFunc(free)(mem);
50 }
51
mmap(void * addr,size_t length,int prot,int flags,int fd,off_t offset)52 void* mmap(void* addr, size_t length, int prot, int flags, int fd, off_t offset)
53 {
54 volatile const struct MallocDispatchType* dispatch_table = get_current_dispatch_table();
55 if (__predict_false(dispatch_table != NULL)) {
56 return dispatch_table->mmap(addr, length, prot, flags, fd, offset);
57 } else {
58 return MuslMalloc(mmap)(addr, length, prot, flags, fd, offset);
59 }
60 }
61
munmap(void * addr,size_t length)62 int munmap(void* addr, size_t length)
63 {
64 volatile const struct MallocDispatchType* dispatch_table = get_current_dispatch_table();
65 if (__predict_false(dispatch_table != NULL)) {
66 return dispatch_table->munmap(addr, length);
67 } else {
68 return MuslMalloc(munmap)(addr, length);
69 }
70 }
71
calloc(size_t m,size_t n)72 void* calloc(size_t m, size_t n)
73 {
74 volatile const struct MallocDispatchType* dispatch_table = get_current_dispatch_table();
75 if (__predict_false(dispatch_table != NULL)) {
76 return dispatch_table->calloc(m, n);
77 } else {
78 return MuslFunc(calloc)(m, n);
79 }
80 }
81
realloc(void * p,size_t n)82 void* realloc(void *p, size_t n)
83 {
84 volatile const struct MallocDispatchType* dispatch_table = get_current_dispatch_table();
85 if (__predict_false(dispatch_table != NULL)) {
86 return dispatch_table->realloc(p, n);
87 } else {
88 return MuslFunc(realloc)(p, n);
89 }
90 }
91
malloc_usable_size(void * addr)92 size_t malloc_usable_size(void* addr)
93 {
94 volatile const struct MallocDispatchType* dispatch_table = get_current_dispatch_table();
95 if (__predict_false(dispatch_table != NULL)) {
96 return dispatch_table->malloc_usable_size(addr);
97 } else {
98 return MuslMalloc(malloc_usable_size)(addr);
99 }
100 }
101
prctl(int option,...)102 int prctl(int option, ...)
103 {
104 unsigned long x[4];
105 int i;
106 va_list ap;
107 va_start(ap, option);
108 for (i=0; i<4; i++) x[i] = va_arg(ap, unsigned long);
109 va_end(ap);
110 volatile const struct MallocDispatchType* dispatch_table = get_current_dispatch_table();
111 if (__predict_false(dispatch_table != NULL)) {
112 return dispatch_table->prctl(option, x[0], x[1], x[2], x[3]);
113 } else {
114 return MuslMalloc(prctl)(option, x[0], x[1], x[2], x[3]);
115 }
116 }
117
118 #endif
119