【问题标题】:What do -fsanitize=function (UndefinedBehaviorSanitizer) errors actually mean?-fsanitize=function (UndefinedBehaviorSanitizer) 错误实际上是什么意思?
【发布时间】:2014-10-30 16:11:18
【问题描述】:

在使用 -fsanitize=function 在 ubuntu 14.04 上使用 clang 3.5 编译 MAME/MESS (https://github.com/mamedev/mame/) 时,我收到很多以下错误:

src/lib/util/delegate.h:651:64: runtime error: call to function osd_file_output_callback(_IO_FILE*, char const*, __va_list_tag*) through pointer to incorrect function type 'void (*)(delegate_generic_class *, const char *, __va_list_tag *)'
/home/notroot/trunk/src/osd/osdcore.c:14: note: osd_file_output_callback(_IO_FILE*, char const*, __va_list_tag*) defined here

src/lib/util/delegate.h:649:42: runtime error: call to function rom_exit(running_machine&) through pointer to incorrect function type 'void (*)(delegate_generic_class *)'
/home/notroot/trunk/src/emu/romload.c:1514: note: rom_exit(running_machine&) defined here

我真的不明白这些错误是什么意思,因为如果函数签名不匹配,我希望应用程序在某个时候崩溃。到目前为止,没有人能向我解释这个错误的真正含义或为什么会这样。

【问题讨论】:

    标签: clang llvm function-pointers


    【解决方案1】:

    我不太明白这些错误是什么意思,因为如果函数签名不匹配,我预计应用程序会在某个时候崩溃。

    这就是未定义行为的危险。这确实意味着函数签名不匹配。这并不意味着它会崩溃,或者等到它在崩溃之前寻找的老板,或者破坏堆栈或诸如此类的东西。它甚至可能在今天变得完全不可观察,但对于编译器的未来版本(或旧版本)则不会。未定义的行为就是这样。您无法从观察到的行为中得出关于不存在未定义行为的结论。

    在您的情况下,它不会崩溃,因为pointer-to-_IO_FILE 和pointer-to-delegate_generic_class 的大小相同,并且在ABI 中它们没有什么特别之处(假设x86-64 linux)。

    【讨论】:

    • 啊,所以这个警告是关于在函数签名中强制执行“类型安全”,只要它们的大小匹配就没有必要。我现在明白了 - 有道理。
    猜你喜欢
    • 2017-02-23
    • 2018-08-08
    • 1970-01-01
    • 1970-01-01
    • 2020-02-15
    • 2021-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多