我编译以下main.c
内容gcc main.c -Wl,--wrap=foo -lcmocka
:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
typedef struct ExampleStruct {
int32_t foo;
int32_t qux;
void* bar;
} ExampleStruct;
ExampleStruct __wrap_foo(int32_t foo, int32_t qux, void* bar) {
printf("bar after: %p\n", bar);
return (ExampleStruct) {
.foo = foo,
.qux = qux,
.bar = bar
};
}
void test() {
void* bar = "fef";
printf("bar before: %p\n", bar);
foo(1, 1, bar);
assert_int_equal(2,2);
}
int main() {
const struct CMUnitTest tests[] = {
cmocka_unit_test(test),
};
return cmocka_run_group_tests_name("success_test", tests, NULL, NULL);
}
我得到以下输出:
[==========] Running 1 test(s).
[ RUN ] test
bar before: 0x40087f
bar after: 0x40087f
[ OK ] test
[==========] 1 test(s) run.
[ PASSED ] 1 test(s).
但是如果我添加一个新成员baz
到ExampleStruct
:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
typedef struct ExampleStruct {
int32_t foo;
int32_t qux;
int32_t baz;
void* bar;
} ExampleStruct;
ExampleStruct __wrap_foo(int32_t foo, int32_t qux, int32_t baz, void* bar) {
printf("bar after: %p\n", bar);
return (ExampleStruct) {
.foo = foo,
.qux = qux,
.baz = baz,
.bar = bar
};
}
void test() {
void* bar = "fef";
printf("bar before: %p\n", bar);
foo(1, 1, 1, bar);
assert_int_equal(2,2);
}
int main() {
const struct CMUnitTest tests[] = {
cmocka_unit_test(test),
};
return cmocka_run_group_tests_name("success_test", tests, NULL, NULL);
}
我得到以下输出:
[==========] Running 1 test(s).
[ RUN ] test
bar before: 0x40086f
bar after: 0x7f3ff17cb2cd
[ ERROR ] --- Test failed with exception: Segmentation fault(11)
[ FAILED ] test
[==========] 1 test(s) run.
[ PASSED ] 0 test(s).
[ FAILED ] 1 test(s), listed below:
[ FAILED ] test
如果我不使用包装函数,一切似乎都按预期工作。
更多细节
我正在使用 GNU ld 版本 2.27-44 运行 gcc 4.8.5
问题
为什么添加另一个成员会ExampleStruct
导致分段错误?为什么在复制到's 参数bar
时会改变值?__wrap_foo