[問題] dlsym hook function crash
#include <stdio.h>
#include <string.h>
#include <stddef.h>
#include <unistd.h>
#include <dlfcn.h>
void* malloc(size_t size)
{
char buf[32];
static void* (*real_malloc)(size_t) = NULL;
printf("ggggggggggg");
if (real_malloc == NULL) {
*(void**)(&real_malloc) = dlsym(RTLD_NEXT, "malloc");
}
sprintf(buf, "malloc called, size = %zu\n", size);
write(2, buf, strlen(buf));
return real_malloc(size);
}
void free(void* ptr)
{
char buf[32];
static void* (*real_free)(void* ptr) = NULL;
if (real_free == NULL) {
*(void**)(&real_free) = dlsym(RTLD_NEXT, "free");
}
sprintf(buf, "free called, ptr address = %p\n", ptr);
write(2, buf, strlen(buf));
real_free(ptr);
}
gcc -D_GNU_SOURCE -shared -fPIC -o libmcount.so malloc_count.c -ldl
LD_PRELOAD=./libmcount.so ls
那悶在malloc函數裡面 p
rintf 函數會造成 crash ? 看網路上dlsym hook一些例子只用 write 而不用 printf
---------------------------------------------------------------------
我又測試一版本是 hook strlen 在裡面用 printf 沒問題
https://gist.github.com/anonymous/348a8102ddccb295b6ef573c68955748
編譯指令:
gcc -shared -fPIC hook.c -o hook.so -ldl
LD_PRELOAD=$PWD/hook.so ./test
這樣是 printf 跟 malloc 之間有什麼關係嘛?
printf 底層是會call到 malloc 所以造成 crash?
謝謝
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 223.136.83.2
※ 文章網址: https://www.ptt.cc/bbs/LinuxDev/M.1499759097.A.3B9.html
※ 編輯: gigigigi (123.204.140.54), 07/12/2017 00:26:21
推
07/12 01:13, , 1F
07/12 01:13, 1F
→
07/12 14:16, , 2F
07/12 14:16, 2F
→
07/12 14:26, , 3F
07/12 14:26, 3F
推
07/13 00:26, , 4F
07/13 00:26, 4F
→
07/13 01:14, , 5F
07/13 01:14, 5F
推
07/13 07:38, , 6F
07/13 07:38, 6F
→
07/13 16:47, , 7F
07/13 16:47, 7F
LinuxDev 近期熱門文章
PTT數位生活區 即時熱門文章