[英]Using dlsym() to stub malloc/free leads to segmentation fault
I started to dabble in unit testing C code (using check) and stubbing functions.我开始涉足单元测试 C 代码(使用检查)和存根函数。 I am trying to unit test a small library of data structures that I wrote and wanted to test how it would react to OOM.我正在尝试对我编写的一个小型数据结构库进行单元测试,并想测试它对 OOM 的反应。 So I wrote a simple stubs.c
file containing:所以我写了一个简单的stubs.c
文件,其中包含:
#include <stdlib.h>
#include <errno.h>
#include <dlfcn.h>
static int malloc_fail_code = 0;
static int calloc_fail_code = 0;
void set_malloc_fail_code(int no) { malloc_fail_code = no; }
void set_calloc_fail_code(int no) { calloc_fail_code = no; }
void *malloc(size_t size)
{
static void *(*real_malloc)(size_t) = NULL;
if (!real_malloc)
real_malloc = (void *(*)(size_t)) dlsym(RTLD_NEXT, "malloc");
if (malloc_fail_code != 0) {
errno = malloc_fail_code;
malloc_fail_code = 0;
return NULL;
}
return real_malloc(size);
}
void *calloc(size_t nmemb, size_t size)
{
static void *(*real_calloc)(size_t, size_t) = NULL;
if (!real_calloc)
real_calloc = (void *(*)(size_t, size_t)) dlsym(RTLD_NEXT, "calloc");
if (calloc_fail_code != 0) {
errno = calloc_fail_code;
calloc_fail_code = 0;
return NULL;
}
return real_calloc(nmemb, size);
}
with its relative stubs.h
containing the definitions for the two setters.其相关的stubs.h
包含两个 setter 的定义。 I then compiled stubs.c as a shared object called libstubs.so
.然后我将 stubs.c 编译为名为libstubs.so
。 I also compiled my library as a shared object called libmy_lib.so
.我还将我的库编译为名为libmy_lib.so
。
My test code is in test.c
is something like this:我的测试代码在test.c
是这样的:
#include <stdlib.h>
#include <errno.h>
#include <check.h>
#include "my_lib.h"
#include "stubs.h"
START_TEST(my_test)
{
... // using the two setters I force malloc and calloc to return null and set errno to ENOMEM
}
END_TEST
... // check boilerplate to create suite and add tests
I then linked the test executable against libmy_lib.so
and libstubs.so
.然后我将测试可执行文件与libmy_lib.so
和libstubs.so
链接起来。 Running said executable greets me with a segfault.运行所说的可执行文件以段错误迎接我。 Inspecting the crash with gdb makes me believe that I encountered a stack overflow due to infinte recursion (gdb backtrace):使用 gdb 检查崩溃让我相信我遇到了由于无限递归(gdb 回溯)导致的堆栈溢出:
#0 0x00007ffff7fc143c in calloc (
nmemb=<error reading variable: Cannot access memory at address 0x7fffff7feff8>,
size=<error reading variable: Cannot access memory at address 0x7fffff7feff0>)
at stubs.c
#1 0x00007ffff7db9c88 in _dlerror_run (operate=operate@entry=0x7ffff7db94f0 <dlsym_doit>,
args=args@entry=0x7fffff7ff030) at dlerror.c:148
#2 0x00007ffff7db9570 in __dlsym (handle=<optimized out>, name=<optimized out>) at dlsym.c:70
#3 0x00007ffff7fc1487 in calloc (nmemb=1, size=32) at stubs.c
...
I tried including directly stubs.c
into test.c
but no luck.我尝试直接将stubs.c
到test.c
但没有运气。 I also tried writing a small unit testing framework of my own that extends stubs.c
and it worked.我还尝试编写我自己的一个小型单元测试框架,它扩展stubs.c
并且它有效。 However I don't want to waste time reinventing the wheel and I am sure there is something I am doing wrong in linking since I don't know much in compilation/linking.但是我不想浪费时间重新发明轮子,而且我确信我在链接方面做错了,因为我对编译/链接了解不多。
For compilation I am using the meson build system so I don't know how to get the exact command line arguments but I can write a MWE of my build targets:对于编译,我使用的是介子构建系统,所以我不知道如何获得确切的命令行 arguments 但我可以编写我的构建目标的 MWE:
lib = library(
'my_lib',
sources,
include_directories: includes,
install: true
)
stubs = shared_library(
'stubs',
'stubs.c',
c_args: ['-g'],
include_directories: test_includes,
link_args: ['-ldl']
)
test_exe = executable(
'test_exe',
c_args: ['-g'],
sources: 'test.c',
dependencies: check,
link_with: [stubs, lib],
include_directories: includes + test_includes
)
test('test', test_exe, suite: 'suite')
Try using LD_PRELOAD trick .尝试使用LD_PRELOAD 技巧。 The meson-ish way to accomplish it would be:实现它的介子式方法是:
test_env = environment()
test_env.prepend('LD_PRELOAD', stubs.full_path())
test('test', test_exe, suite: 'suite', env: test_env)
note: do not link executable with stubs.注意:不要将可执行文件与存根链接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.