c - 如果返回值结构有太多成员,则包装的函数指针参数会更改

标签 c ld cmocka

我用 gcc main.c -Wl,--wrap=foo -lcmocka 编译了以下 main.c:

#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).

但是如果我向 ExampleStruct 添加一个新成员 baz:

#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

如果我不使用包装函数,一切似乎都按预期工作。

更多详情

我正在运行 gcc 4.8.5 使用 GNU ld 版本 2.27-44

问题

为什么向 ExampleStruct 添加另一个成员会导致段错误?为什么 bar 在复制到 __wrap_foo 的参数中时会更改值?

最佳答案

您需要为 foo 函数添加声明。目前 foo(1, 1, 1, bar); 将使用 int foo() 的隐式声明。自 C99 以来,隐式声明无效,因此您应该能够打开更多警告来捕获此问题 (-Wimplicit-function-declaration -Werror)。

隐式声明的函数接受你给它的参数(它实际上匹配 __wrap_foo,假设 int32_t 是一个 typedef int) 并将返回 int,而不是 ExampleStruct,因此您的程序将具有未定义的行为

例子:

#include <assert.h>
#include <stdio.h>
#include <stdint.h>

typedef struct ExampleStruct {
    int32_t foo;
    int32_t qux;
    int32_t baz;
    void* bar;
} ExampleStruct;

// ** add this declaration **
ExampleStruct foo(int32_t foo, int32_t qux, int32_t baz, void* bar);

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
    };
}

int main() {
    void* bar = "fef";
    printf("bar before: %p\n", bar);
    foo(1, 1, 1, bar);
    assert(2 == 2);
}

关于c - 如果返回值结构有太多成员,则包装的函数指针参数会更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71087449/

相关文章:

c++ - 如何在 C 中设计样式?

c - 如何将 getline() 的每个字符串存储在(动态)字符串数组中?

c - C 宏存储在内存中的什么位置?与函数相比,它如何消耗更多内存?

c - 可以从第 x 行开始并返回当前行的 Linux 命令行阅读器(如 "less")

c++ - 映射文件中成员结构的地址?

c - 如何使用 cmocka 库模拟无法修改的第三方库中的函数?

c - 被测非静态函数调用的模拟/包装静态函数

ios-simulator - ld 警告 : too many personality routines for compact unwind to encode

Gcc/ld 警告 "liba"需要 "libb",未找到,但属于 rpath 的一部分

c - 当在同一文件中定义其调用函数时如何在 C 中模拟函数?