在破坏检测功能的同时(如@马克·普洛特尼克 https://stackoverflow.com/questions/30809022/how-can-i-break-on-ubsan-reports-in-gdb-and-continue#comment49749394_30809022 and @Iwillnotexist Idonotexist https://stackoverflow.com/a/31459198/427545)是一种选择,更好的方法是破坏检测后报告这些问题的功能。这种方法也适用于 ASAN,其中人们会中断__asan_report_error
.
摘要:您可以通过断点停止 ubsan 报告__ubsan::ScopedReport::~ScopedReport
or __ubsan::Diag::~Diag
。这些是私有的实现细节,但将来可能会发生变化。使用 GCC 4.9、5.1.0、5.2.0 和 Clang 3.3、3.4、3.6.2 进行测试。
对于 GCC 4.9.2 从ppa:ubuntu-工具链-r/测试 https://launchpad.net/~ubuntu-toolchain-r/+archive/ubuntu/test?field.series_filter=trusty, 你需要libubsan0-dbg
使上述断点可用。 Ubuntu 14.04 与 Clang 3.3 和 3.4 不支持__ubsan::ScopedReport::~ScopedReport
断点,因此您只能在打印消息之前中断__ubsan::Diag::~Diag https://github.com/llvm-mirror/compiler-rt/blob/release_34/lib/ubsan/ubsan_diag.cc#L248.
有问题的源代码和 gdb 会话示例:
$ cat undef.c
int main(void) { return 1 << 1000; }
$ clang --version
clang version 3.6.2 (tags/RELEASE_362/final)
Target: x86_64-unknown-linux-gnu
Thread model: posix
$ clang -w -fsanitize=undefined undef.c -g
$ gdb -q -ex break\ __ubsan::ScopedReport::~ScopedReport -ex r ./a.out
Reading symbols from ./a.out...done.
Breakpoint 1 at 0x428fb0
Starting program: ./a.out
undef.c:1:27: runtime error: shift exponent 1000 is too large for 32-bit type 'int'
Breakpoint 1, 0x0000000000428fb0 in __ubsan::ScopedReport::~ScopedReport() ()
(gdb) bt
#0 0x0000000000428fb0 in __ubsan::ScopedReport::~ScopedReport() ()
#1 0x000000000042affb in handleShiftOutOfBoundsImpl(__ubsan::ShiftOutOfBoundsData*, unsigned long, unsigned long, __ubsan::ReportOptions) ()
#2 0x000000000042a952 in __ubsan_handle_shift_out_of_bounds ()
#3 0x000000000042d057 in main () at undef.c:1
详细分析如下。请注意,ASAN 和 ubsan 均源自 LLVM 项目,编译器-rt http://compiler-rt.llvm.org/。它由 Clang 使用,最终也出现在 GCC 中。以下部分中的链接指向编译器-rt 项目代码,版本 3.6。
ASAN 已经做出了内部__asan_report_error
的一部分记录公共接口 https://github.com/llvm-mirror/compiler-rt/blob/release_36/include/sanitizer/asan_interface.h#L107。每当检测到违规时就会调用此函数,其流程继续lib/asan/asan_report.c:938 https://github.com/llvm-mirror/compiler-rt/blob/release_36/lib/asan/asan_report.cc#L938:
void __asan_report_error(uptr pc, uptr bp, uptr sp, uptr addr, int is_write,
uptr access_size) {
// Determine the error type.
const char *bug_descr = "unknown-crash";
...
ReportData report = { pc, sp, bp, addr, (bool)is_write, access_size,
bug_descr };
ScopedInErrorReport in_report(&report);
Decorator d;
Printf("%s", d.Warning());
Report("ERROR: AddressSanitizer: %s on address "
"%p at pc %p bp %p sp %p\n",
bug_descr, (void*)addr, pc, bp, sp);
Printf("%s", d.EndWarning());
u32 curr_tid = GetCurrentTidOrInvalid();
char tname[128];
Printf("%s%s of size %zu at %p thread T%d%s%s\n",
d.Access(),
access_size ? (is_write ? "WRITE" : "READ") : "ACCESS",
access_size, (void*)addr, curr_tid,
ThreadNameWithParenthesis(curr_tid, tname, sizeof(tname)),
d.EndAccess());
GET_STACK_TRACE_FATAL(pc, bp);
stack.Print();
DescribeAddress(addr, access_size);
ReportErrorSummary(bug_descr, &stack);
PrintShadowMemoryForAddress(addr);
}
另一方面,ubsan 没有公共接口,但其当前的实现也更加简单和有限(选项较少)。出现错误时,可以打印堆栈跟踪UBSAN_OPTIONS=print_stacktrace=1
环境变量已设置。因此,通过搜索源代码print_stacktrace
, 找到函数也许是 PrintStackTrace https://github.com/llvm-mirror/compiler-rt/blob/release_36/lib/ubsan/ubsan_diag.cc#L25这被称为虽然ScopedReport 析构函数 https://github.com/llvm-mirror/compiler-rt/blob/release_36/lib/ubsan/ubsan_diag.cc#L351:
ScopedReport::~ScopedReport() {
MaybePrintStackTrace(Opts.pc, Opts.bp);
MaybeReportErrorSummary(SummaryLoc);
CommonSanitizerReportMutex.Unlock();
if (Opts.DieAfterReport || flags()->halt_on_error)
Die();
}
正如您所看到的,有一种方法可以在出现错误时终止程序,但不幸的是没有内置机制来触发调试器陷阱。那我们就找一个合适的断点吧。
GDB命令info functions <function name> https://sourceware.org/gdb/onlinedocs/gdb/Symbols.html#index-info-functions-1071使得识别成为可能MaybePrintStackTrace
作为可以设置断点的函数。执行info functions ScopedReport::~ScopedReport
给出了另一个函数:__ubsan::ScopedReport::~ScopedReport
。如果这些功能似乎都不可用(即使安装了调试符号),您可以尝试info functions ubsan
or info functions sanitizer
获取所有 (UndefinedBehaviorSanitizer 相关函数。