读取指向函数中的变量时出现分段错误

Segmentation fault when reading variables in pointed function

我有以下几段代码:(注意:由于我无法共享数据,代码已简化并更改了很多)

exercise.cpp

#include "some_api.h"

extern "C" int extension_init(func_t*, VExtension*);

int main(int argc, char** argv)
{
    VExtension ve;
    extension_init(&func, &ve);
    return 0;
} 

some_api.h

bool func(int const& a, void* const& b, VExtension* const& v)
{
    std::cout << a << b << std::endl;
}

api.h

typedef int (func_t)(int c, void* p, VExtension* v)

file.cpp

#include "api.h" // this is included implicitly

extern "C" int extension_init(func_t* F, VExtension* v)
{
    intptr_t ver = 7;
    F(1, (void*)ver, v);
}

因此,当 F 被调用时 func 是从 some_api.h 调用的,但是 Seg Fault 在尝试输出值 a 和 [= 时出现24=]。 静态分析器给出以下错误信息:

ASAN:DEADLYSIGNAL
=================================================================
==15==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000001 (pc 0x55d6dbe48ca2 bp 0x7fff79dbf320 sp 0x7fff79dbf300 T0)
==15==The signal is caused by a READ memory access.
==15==Hint: address points to the zero page.
    #0 0x55d6dbe48ca1 in func(int const&, void* const&, VExtension* const&) some_api.h:279
    #1 0x55d6dbefe697 in file.cpp:809
    #2 0x55d6dbe5373a in main exercise.cpp:123
    #3 0x7f9c65393b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    #4 0x55d6dbd49839 in _start (//...)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: some_api.h:279 in func(int const&, void* const&, VExtension* const&)
==15==ABORTING

更正后的代码可以工作

如果 func_t 使用与 func() 相同的签名定义,一切都会好起来的,因为 b 的任意值永远不会被取消引用:

typedef bool (func_t)(int const& a, void* const& b, VExtension* const& v);

参见 demo

但是你的代码不能,所以这里有问题

您的代码似乎可以编译,至少从您显示的运行时跟踪来看是这样。但是根据您发布的片段,代码根本不应该编译。因此,我怀疑您的不同 headers 在不同的编译单元中使用来自 func_t 的不同定义,设法创建了一个危险的组合。如果是这种情况,您将处于未定义行为的世界中。

例如 return 类型 int 与 return 类型 bool 不匹配。但更致命的是,在 func_t 中参数是普通值,而在 func() 中它们都是引用。因此,如果编译器被欺骗,func() 的调用者使用的调用约定将与 func() 使用的调用约定不兼容,从而导致无效的内存访问(很可能——但这将是实现依赖——生成的调用代码会发送值参数,但生成的接收代码会期望这些参数是指向值的指针并尝试访问指向的内存位置,这将是无效的)。

补充说明

现在,我不是语言律师,但看到一个函数使用 C 调用约定 (extern "C"),使用参数和 return 中不存在的类型也很有趣C(即布尔和引用)。

其他见解

在一个编译器上使用 func_t 的更正定义查看 assembly code generated for extension_init

lea rdx, [rsp+8]           ; load effective address of third paramvalue
lea rsi, [rsp+24]          ; load effective address of second param value
mov QWORD PTR [rsp+24], 7  ; initialize value 
lea rdi, [rsp+20]          ; load effective address of first param value
mov DWORD PTR [rsp+20], 1  ; initilize value
call rax

如果 using your original definition,生成的代码看起来不同:

                ; the third value was left out by the global initializer
                ;  but it was previously set with an LEA to load the effective 
                ; address of the struct. 
mov esi, 7      ; pass the second value
mov edi, 1      ; pass the first value
call rax