gcc ASAN 不会因所谓的运行时错误而停止

gcc ASAN doesn't stop on purported runtime error

我希望 address sanitizer 在捕获到某些东西时中止。我认为它被设计为默认执行此操作,但它似乎对我不起作用。我也试过 ASAN_OPTIONS=halt_on_error=1 没有效果。详情如下:

在我工作的一个项目中,我们使用了地址清理器,它在 warning/error 几周内一直在释放这个 warning/error 而没有人意识到:

runtime error: null pointer passed as argument xx, which is declared to never be null

尽管被称为运行时错误,但它不会停止程序或导致错误的退出代码。这是一个简单的程序来演示它:

/*
gcc -fsanitize=address,undefined \
    -Wformat \
    -Werror=format-security \
    -Werror=array-bounds \
    -g -o xasan xasan.c
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[]) {
  fprintf(stderr, "before\n");
  memcpy(NULL, argc > 1 ? "" : NULL, argc > 1 ? 1 : 0);
  fprintf(stderr, "after\n");
  return 0;
}

argc 技巧只是为了让 gcc 不优化 memcpy 调用,基本上在我们的代码中它最终成为 memcpy(dst, NULL, 0),这导致运行时 error/warning.

我预计 'after' 不会在运行时错误后输出,但它会输出并且程序退出代码为 0。这是一个错误吗?手册说它应该停止。

事实证明错误来自 UBSAN,答案是使用编译器开关 -fno-sanitize-recover,如 here.

如果你不想重新编译,你也可以运行和export UBSAN_OPTIONS=halt_on_error=1