未验证 提交 afd3a3b7 编写于 作者: O openharmony_ci 提交者: Gitee

回退 'Pull Request !789 : hook upload'

上级 3a2f6ceb
...@@ -730,13 +730,6 @@ template("musl_libs") { ...@@ -730,13 +730,6 @@ template("musl_libs") {
"//build/config/compiler:compiler", "//build/config/compiler:compiler",
":soft_hook", ":soft_hook",
] ]
cflags = [
"-mllvm",
"--instcombine-max-iterations=0",
"-ffp-contract=fast",
"-O3",
]
} }
source_set("soft_musl_jemalloc") { source_set("soft_musl_jemalloc") {
......
...@@ -9,44 +9,26 @@ ...@@ -9,44 +9,26 @@
void* malloc(size_t bytes) void* malloc(size_t bytes)
{ {
struct MallocDispatchType* dispatch_table = (struct MallocDispatchType *)atomic_load_explicit( volatile const struct MallocDispatchType* dispatch_table = get_current_dispatch_table();
&__musl_libc_globals.current_dispatch_table, memory_order_acquire);
if (__predict_false(dispatch_table != NULL)) { if (__predict_false(dispatch_table != NULL)) {
if (__get_memleak_hook_flag()) { void*ret = dispatch_table->malloc(bytes);
return dispatch_table->malloc(bytes); return ret;
} }
if (!__get_global_hook_flag()) { void* result = MuslMalloc(malloc)(bytes);
return MuslFunc(malloc)(bytes); if (__predict_false(result == NULL)) {
} //__musl_log(__MUSL_LOG_WARN, "malloc(%zu) failed: returning null pointer\n", bytes);
if (!__get_hook_flag()) {
return MuslFunc(malloc)(bytes);
}
return dispatch_table->malloc(bytes);
} }
return MuslFunc(malloc)(bytes); return result;
} }
void free(void* mem) void free(void* mem)
{ {
struct MallocDispatchType* dispatch_table = (struct MallocDispatchType *)atomic_load_explicit( volatile const struct MallocDispatchType* dispatch_table = get_current_dispatch_table();
&__musl_libc_globals.current_dispatch_table, memory_order_acquire);
if (__predict_false(dispatch_table != NULL)) { if (__predict_false(dispatch_table != NULL)) {
if (__get_memleak_hook_flag()) {
dispatch_table->free(mem);
return;
}
if (!__get_global_hook_flag()) {
MuslFunc(free)(mem);
return;
}
if (!__get_hook_flag()) {
MuslFunc(free)(mem);
return;
}
dispatch_table->free(mem); dispatch_table->free(mem);
return; } else {
MuslMalloc(free)(mem);
} }
MuslFunc(free)(mem);
} }
void* mmap(void* addr, size_t length, int prot, int flags, int fd, off_t offset) void* mmap(void* addr, size_t length, int prot, int flags, int fd, off_t offset)
......
...@@ -13,19 +13,12 @@ extern "C" { ...@@ -13,19 +13,12 @@ extern "C" {
#define MuslMalloc(func) func #define MuslMalloc(func) func
#endif #endif
#ifdef USE_JEMALLOC
#define MuslFunc(func) je_ ## func
#else
#define MuslFunc(func) internal_ ## func
#endif
extern void *internal_malloc(size_t);
extern void internal_free(void*);
void *__libc_mmap(void*, size_t, int, int, int, off_t); void *__libc_mmap(void*, size_t, int, int, int, off_t);
int __libc_munmap(void*, size_t); int __libc_munmap(void*, size_t);
void *__libc_malloc(size_t);
void *__libc_calloc(size_t, size_t); void *__libc_calloc(size_t, size_t);
void *__libc_realloc(void *, size_t); void *__libc_realloc(void *, size_t);
void __libc_free(void *);
void *__libc_valloc(size_t); void *__libc_valloc(size_t);
void *__libc_memalign(size_t, size_t); void *__libc_memalign(size_t, size_t);
size_t __libc_malloc_usable_size(void *); size_t __libc_malloc_usable_size(void *);
......
...@@ -40,7 +40,7 @@ void* ohos_malloc_hook_init_function(size_t bytes); ...@@ -40,7 +40,7 @@ void* ohos_malloc_hook_init_function(size_t bytes);
static struct MallocDispatchType __ohos_malloc_hook_init_dispatch = { static struct MallocDispatchType __ohos_malloc_hook_init_dispatch = {
.malloc = ohos_malloc_hook_init_function, .malloc = ohos_malloc_hook_init_function,
.free = MuslFunc(free), .free = MuslMalloc(free),
.mmap = MuslMalloc(mmap), .mmap = MuslMalloc(mmap),
.munmap = MuslMalloc(munmap), .munmap = MuslMalloc(munmap),
.calloc = MuslMalloc(calloc), .calloc = MuslMalloc(calloc),
...@@ -459,7 +459,7 @@ void* ohos_malloc_hook_init_function(size_t bytes) ...@@ -459,7 +459,7 @@ void* ohos_malloc_hook_init_function(size_t bytes)
// __musl_log(__MUSL_LOG_ERROR, "%s: ohos_malloc_hook: failed to pthread_detach\n", getprogname()); // __musl_log(__MUSL_LOG_ERROR, "%s: ohos_malloc_hook: failed to pthread_detach\n", getprogname());
} }
} }
void*ptr = MuslFunc(malloc)(bytes); void*ptr = MuslMalloc(malloc)(bytes);
return ptr; return ptr;
} }
......
...@@ -8,8 +8,8 @@ ...@@ -8,8 +8,8 @@
struct musl_libc_globals __musl_libc_globals; struct musl_libc_globals __musl_libc_globals;
struct MallocDispatchType __libc_malloc_default_dispatch = { struct MallocDispatchType __libc_malloc_default_dispatch = {
.malloc = MuslFunc(malloc), .malloc = MuslMalloc(malloc),
.free = MuslFunc(free), .free = MuslMalloc(free),
.mmap = MuslMalloc(mmap), .mmap = MuslMalloc(mmap),
.munmap = MuslMalloc(munmap), .munmap = MuslMalloc(munmap),
.calloc = MuslMalloc(calloc), .calloc = MuslMalloc(calloc),
......
...@@ -30,6 +30,11 @@ extern int je_mallopt(int param, int value); ...@@ -30,6 +30,11 @@ extern int je_mallopt(int param, int value);
#define inline inline __attribute__((always_inline)) #define inline inline __attribute__((always_inline))
#endif #endif
#ifdef HOOK_ENABLE
void *__libc_malloc(size_t);
void __libc_free(void *p);
#endif
static struct { static struct {
volatile uint64_t binmap; volatile uint64_t binmap;
struct bin bins[BINS_COUNT]; struct bin bins[BINS_COUNT];
...@@ -623,16 +628,16 @@ static void trim(struct chunk *self, size_t n) ...@@ -623,16 +628,16 @@ static void trim(struct chunk *self, size_t n)
} }
#ifdef HOOK_ENABLE #ifdef HOOK_ENABLE
void *__libc_malloc(size_t n)
#else #else
void *malloc(size_t n) void *malloc(size_t n)
#endif
{ {
#ifdef USE_JEMALLOC #ifdef USE_JEMALLOC
return je_malloc(n); return je_malloc(n);
#endif #endif
return internal_malloc(n); return internal_malloc(n);
} }
#endif
void *internal_malloc(size_t n) void *internal_malloc(size_t n)
{ {
...@@ -1240,16 +1245,16 @@ static void quarantine_bin(struct chunk *self) ...@@ -1240,16 +1245,16 @@ static void quarantine_bin(struct chunk *self)
#endif #endif
#ifdef HOOK_ENABLE #ifdef HOOK_ENABLE
void __libc_free(void *p)
#else #else
void free(void *p) void free(void *p)
#endif
{ {
#ifdef USE_JEMALLOC #ifdef USE_JEMALLOC
return je_free(p); return je_free(p);
#endif #endif
return internal_free(p); return internal_free(p);
} }
#endif
void internal_free(void *p) void internal_free(void *p)
{ {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册