作者gigigigi (gigigigi)
看板LinuxDev
标题[问题] dlsym hook function crash
时间Tue Jul 11 15:44:55 2017
#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://webptt.com/cn.aspx?n=bbs/LinuxDev/M.1499759097.A.3B9.html
※ 编辑: gigigigi (123.204.140.54), 07/12/2017 00:26:21
1F:推 Bencrie: 你先把 sprintf 改成 snprintf 再说 XD 07/12 01:13
2F:→ gigigigi: coredump gdb ls core 结果看起来是死在printf 里面 07/12 14:16
4F:推 Bencrie: 还是挂个 strace / ltrace 跑看看堆叠是不是坏了 07/13 00:26
5F:→ CP64: 这 trace 结果 一脸堆叠过深的样子 07/13 01:14
6F:推 x000032001: printf 又呼叫malloc 造成无限循环 07/13 07:38
7F:→ gigigigi: 目前结论也是递回无限循环造成! 07/13 16:47