提交 34f20a44 编写于 作者: L lutao

Add prctl hook

Issue: I6NO46

Test: Pass: 1146/1156
Signed-off-by: Nlutao <lutao31@huawei.com>
上级 6185aa85
......@@ -98,4 +98,21 @@ size_t malloc_usable_size(void* addr)
return MuslMalloc(malloc_usable_size)(addr);
}
}
int prctl(int option, ...)
{
unsigned long x[4];
int i;
va_list ap;
va_start(ap, option);
for (i=0; i<4; i++) x[i] = va_arg(ap, unsigned long);
va_end(ap);
volatile const struct MallocDispatchType* dispatch_table = get_current_dispatch_table();
if (__predict_false(dispatch_table != NULL)) {
return dispatch_table->prctl(option, x[0], x[1], x[2], x[3]);
} else {
return MuslMalloc(prctl)(option, x[0], x[1], x[2], x[3]);
}
}
#endif
......@@ -36,6 +36,7 @@ void *__libc_realloc(void *, size_t);
void *__libc_valloc(size_t);
void *__libc_memalign(size_t, size_t);
size_t __libc_malloc_usable_size(void *);
int __libc_prctl(int, ...);
struct mallinfo2 __libc_mallinfo2(void);
......
......@@ -18,6 +18,7 @@ typedef void* (*MallocVallocType)(size_t);
typedef void (*MallocFreeType)(void*);
typedef void* (*MallocMemalignType)(size_t, size_t);
typedef size_t (*MallocMallocUsableSizeType)(void*);
typedef int (*MallocPrctlType)(int, ...);
typedef struct mallinfo (*MallinfoType)(void);
typedef struct mallinfo2 (*Mallinfo2Type)(void);
......@@ -55,6 +56,7 @@ struct MallocDispatchType {
GetHookFlagType get_hook_flag;
SetHookFlagType set_hook_flag;
MemTrace memtrace;
MallocPrctlType prctl;
};
#ifdef __cplusplus
}
......
......@@ -45,6 +45,7 @@ static struct MallocDispatchType __ohos_malloc_hook_init_dispatch = {
.munmap = MuslMalloc(munmap),
.calloc = MuslFunc(calloc),
.realloc = MuslFunc(realloc),
.prctl = MuslMalloc(prctl),
.memtrace = default_memtrace,
};
#define MAX_SYM_NAME_SIZE 1000
......@@ -253,6 +254,18 @@ static bool init_malloc_usable_size_function(void* malloc_shared_library_handler
return true;
}
static bool init_prctl_function(void* malloc_shared_library_handler, MallocPrctlType* func, const char* prefix)
{
char symbol[MAX_SYM_NAME_SIZE];
snprintf(symbol, sizeof(symbol), "%s_%s", prefix, "prctl");
*func = (MallocPrctlType)(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)) {
......@@ -279,6 +292,9 @@ static bool init_hook_functions(void* shared_library_handler, struct MallocDispa
if (!init_malloc_usable_size_function(shared_library_handler, &table->malloc_usable_size, prefix)) {
return false;
}
if (!init_prctl_function(shared_library_handler, &table->prctl, prefix)) {
return false;
}
return true;
}
......
......@@ -15,6 +15,7 @@ struct MallocDispatchType __libc_malloc_default_dispatch = {
.calloc = MuslFunc(calloc),
.realloc = MuslFunc(realloc),
.malloc_usable_size = MuslMalloc(malloc_usable_size),
.prctl = MuslMalloc(prctl),
};
volatile atomic_bool __hook_enable_hook_flag;
......
#include <sys/prctl.h>
#include <stdarg.h>
#include "syscall.h"
#ifdef HOOK_ENABLE
int __libc_prctl(int op, ...);
#endif
#ifdef HOOK_ENABLE
int __libc_prctl(int op, ...)
#else
int prctl(int op, ...)
#endif
{
unsigned long x[4];
int i;
va_list ap;
va_start(ap, op);
for (i=0; i<4; i++) x[i] = va_arg(ap, unsigned long);
va_end(ap);
return syscall(SYS_prctl, op, x[0], x[1], x[2], x[3]);
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册