提交 ff2384d6 编写于 作者: 卢韬 提交者: Gitee

Merge branch 'master' of gitee.com:openharmony/third_party_musl into master

Signed-off-by: N卢韬 <lutao31@huawei.com>
LIBC {
{
global:
___environ;
__adjtime64;
......@@ -2060,4 +2060,4 @@ LIBC {
trace_marker_async_end;
trace_marker_count;
trace_marker_end;
};
\ No newline at end of file
};
......@@ -2137,6 +2137,8 @@ musl_src_porting_file = [
"src/process/vfork.c",
"src/process/arm/__vfork.s",
"src/process/x86_64/__vfork.s",
"src/linux/cache.c",
"src/sched/sched_getcpu.c",
]
musl_inc_hook_files = [
......
......@@ -70,4 +70,14 @@ void* realloc(void *p, size_t n)
return MuslMalloc(realloc)(p, n);
}
}
size_t malloc_usable_size(void* addr)
{
volatile const struct MallocDispatchType* dispatch_table = get_current_dispatch_table();
if (__predict_false(dispatch_table != NULL)) {
return dispatch_table->malloc_usable_size(addr);
} else {
return MuslMalloc(malloc_usable_size)(addr);
}
}
#endif
......@@ -51,14 +51,24 @@ static struct MallocDispatchType __ohos_malloc_hook_init_dispatch = {
static char *__malloc_hook_shared_lib = "libnative_hook.z.so";
static char *__malloc_hook_function_prefix = "ohos_malloc_hook";
volatile atomic_llong ohos_malloc_hook_shared_library;
static char *kMemTrackSharedLib = "libmemleak_tracker.so";
static char *kMemTrackPrefix = "track";
static char *kMemTrackPropertyEnable = "const.hiview.memleak_tracker.enable";
static char *kMemTrackSign = "true";
bool checkLoadMallocMemTrack = false;
void* function_of_shared_lib[LAST_FUNCTION];
static enum EnumHookMode __hook_mode = STEP_HOOK_MODE;
static char __memleak_param_value[OHOS_PARAM_MAX_SIZE + 1] = {0};
static void get_native_hook_param(char *buf, unsigned int buf_len)
{
#ifdef OHOS_ENABLE_PARAMETER
const char *key = MUSL_HOOK_PARAM_NAME;
unsigned int len = buf_len;
(void)SystemReadParam(kMemTrackPropertyEnable, __memleak_param_value, &len);
if (strncmp(__memleak_param_value, kMemTrackSign, strlen(kMemTrackSign)) == 0) {
checkLoadMallocMemTrack = true;
}
(void)SystemReadParam(key, buf, &len);
#else
return;
......@@ -213,6 +223,17 @@ static bool init_realloc_function(void* malloc_shared_library_handler, MallocRea
return true;
}
static bool init_malloc_usable_size_function(void* malloc_shared_library_handler, MallocMallocUsableSizeType* func, const char* prefix)
{
char symbol[MAX_SYM_NAME_SIZE];
snprintf(symbol, sizeof(symbol), "%s_%s", prefix, "malloc_usable_size");
*func = (MallocMallocUsableSizeType)(dlsym(malloc_shared_library_handler, symbol));
if (*func == NULL) {
return false;
}
return true;
}
static bool init_hook_functions(void* shared_library_handler, struct MallocDispatchType* table, const char* prefix)
{
if (!init_malloc_function(shared_library_handler, &table->malloc, prefix)) {
......@@ -236,6 +257,9 @@ static bool init_hook_functions(void* shared_library_handler, struct MallocDispa
if (!init_memorytag_function(shared_library_handler, prefix)) {
return false;
}
if (!init_malloc_usable_size_function(shared_library_handler, &table->malloc_usable_size, prefix)) {
return false;
}
return true;
}
......@@ -344,11 +368,11 @@ static bool is_empty_string(const char* str)
return true;
}
static void install_ohos_malloc_hook(struct musl_libc_globals* globals)
static void install_ohos_malloc_hook(struct musl_libc_globals* globals, const char* shared_lib, const char* prefix)
{
volatile void* shared_library_handle = (volatile void *)atomic_load_explicit(&ohos_malloc_hook_shared_library, memory_order_acquire);
assert(shared_library_handle == NULL || shared_library_handle == (volatile void*)-1);
shared_library_handle = (volatile void*)load_malloc_hook_shared_library(__malloc_hook_shared_lib, __malloc_hook_function_prefix, &globals->malloc_dispatch_table);
shared_library_handle = (volatile void*)load_malloc_hook_shared_library(shared_lib, prefix, &globals->malloc_dispatch_table);
if (shared_library_handle == NULL) {
// __musl_log(__MUSL_LOG_ERROR, "Can't load shared library '%s'\n", __malloc_hook_shared_lib);
return;
......@@ -365,7 +389,11 @@ static void install_ohos_malloc_hook(struct musl_libc_globals* globals)
static void* init_ohos_malloc_hook()
{
install_ohos_malloc_hook(&__musl_libc_globals);
if (checkLoadMallocMemTrack) {
install_ohos_malloc_hook(&__musl_libc_globals, kMemTrackSharedLib, kMemTrackPrefix);
} else {
install_ohos_malloc_hook(&__musl_libc_globals, __malloc_hook_shared_lib, __malloc_hook_function_prefix);
}
return NULL;
}
......@@ -472,6 +500,10 @@ __attribute__((constructor(1))) static void __musl_initialize()
__hook_mode = STEP_HOOK_MODE;
}
}
volatile bool hook_disable = atomic_load_explicit(&__hook_enable_hook_flag, memory_order_acquire);
if (!hook_disable && checkLoadMallocMemTrack) {
init_ohos_malloc_hook();
}
__initialize_malloc();
errno = 0;
}
......
......@@ -14,6 +14,7 @@ struct MallocDispatchType __libc_malloc_default_dispatch = {
.munmap = MuslMalloc(munmap),
.calloc = MuslMalloc(calloc),
.realloc = MuslMalloc(realloc),
.malloc_usable_size = MuslMalloc(malloc_usable_size),
};
volatile atomic_bool __hook_enable_hook_flag;
......
......@@ -11,6 +11,8 @@ extern struct MallocDispatchType __libc_malloc_default_dispatch;
extern volatile atomic_bool __hook_enable_hook_flag;
extern bool checkLoadMallocMemTrack;
enum EnumFunc {
INITIALIZE_FUNCTION,
FINALIZE_FUNCTION,
......@@ -94,6 +96,9 @@ inline volatile const struct MallocDispatchType* get_current_dispatch_table()
#ifdef HOOK_ENABLE
volatile const struct MallocDispatchType* ret = (struct MallocDispatchType *)atomic_load_explicit(&__musl_libc_globals.current_dispatch_table, memory_order_acquire);
if (ret != NULL) {
if (checkLoadMallocMemTrack) {
return ret;
}
if (!__get_global_hook_flag()) {
ret = NULL;
}
......
......@@ -95,6 +95,8 @@ hidden void *internal_calloc(size_t m, size_t n);
hidden void *internal_realloc(void *p, size_t n);
hidden size_t internal_malloc_usable_size(void *p);
#ifdef MALLOC_RED_ZONE
hidden void chunk_checksum_set(struct chunk *c);
......
/* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <errno.h>
#include "syscall.h"
#include "atomic.h"
#ifdef SYS_cacheflush
int _flush_cache(void *addr, int len, int op)
{
return syscall(SYS_cacheflush, addr, len, op);
}
weak_alias(_flush_cache, cacheflush);
#endif
#ifdef SYS_cachectl
int __cachectl(void *addr, int len, int op)
{
return syscall(SYS_cachectl, addr, len, op);
}
weak_alias(__cachectl, cachectl);
#endif
#ifdef SYS_riscv_flush_icache
#define VDSO_FLUSH_ICACHE_SYM "__vdso_flush_icache"
#define VDSO_FLUSH_ICACHE_VER "LINUX_4.5"
static void *volatile vdso_func;
static int flush_icache_init(void *start, void *end, unsigned long int flags)
{
__get_vdso_info();
void *p = __get_vdso_addr(VDSO_FLUSH_ICACHE_VER, VDSO_FLUSH_ICACHE_SYM);
int (*f)(void *, void *, unsigned long int) =
(int (*)(void *, void *, unsigned long int))p;
a_cas_p(&vdso_func, (void *)flush_icache_init, p);
return f ? f(start, end, flags) : -ENOSYS;
}
static void *volatile vdso_func = (void *)flush_icache_init;
int __riscv_flush_icache(void *start, void *end, unsigned long int flags)
{
int (*f)(void *, void *, unsigned long int) =
(int (*)(void *, void *, unsigned long int))vdso_func;
if (f) {
int r = f(start, end, flags);
if (!r) return r;
if (r != -ENOSYS) return __syscall_ret(r);
}
}
weak_alias(__riscv_flush_icache, riscv_flush_icache);
#endif
......@@ -11,7 +11,16 @@ extern size_t je_malloc_usable_size(void *p);
hidden void *(*const __realloc_dep)(void *, size_t) = realloc;
#ifdef HOOK_ENABLE
size_t __libc_malloc_usable_size(void* p)
#else
size_t malloc_usable_size(void *p)
#endif
{
return internal_malloc_usable_size(p);
}
size_t internal_malloc_usable_size(void* p)
{
#ifdef USE_JEMALLOC
return je_malloc_usable_size(p);
......
/* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define _GNU_SOURCE
#include <errno.h>
#include <sched.h>
#include "syscall.h"
#include "atomic.h"
#ifdef VDSO_GETCPU_SYM
static void *volatile vdso_func;
typedef long (*getcpu_f)(unsigned *, unsigned *, void *);
static long getcpu_init(unsigned *cpu, unsigned *node, void *unused)
{
__get_vdso_info();
void *p = __get_vdso_addr(VDSO_GETCPU_VER, VDSO_GETCPU_SYM);
getcpu_f f = (getcpu_f)p;
a_cas_p(&vdso_func, (void *)getcpu_init, p);
return f ? f(cpu, node, unused) : -ENOSYS;
}
static void *volatile vdso_func = (void *)getcpu_init;
#endif
int sched_getcpu(void)
{
int r;
unsigned cpu;
#ifdef VDSO_GETCPU_SYM
getcpu_f f = (getcpu_f)vdso_func;
if (f) {
r = f(&cpu, 0, 0);
if (!r) return cpu;
if (r != -ENOSYS) return __syscall_ret(r);
}
#endif
r = __syscall(SYS_getcpu, &cpu, 0, 0);
if (!r) return cpu;
return __syscall_ret(r);
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册