虽然突破检测功能(如@Mark Plotnick和@Iwillnotexist Idonotexist所述)是一种选择,但更好的方法是打破检测后报告这些问题的功能。这种方法也用于ASAN,其中一个会在__asan_report_error
上破裂。
摘要:您可以通过断点上ubsan报告停止__ubsan::ScopedReport::~ScopedReport
或__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-toolchain-r/test,您需要libubsan0-dbg
使上述断点可用。使用Clang 3.3和3.4的Ubuntu 14.04不支持__ubsan::ScopedReport::~ScopedReport
断点,因此只能在使用__ubsan::Diag::~Diag
打印消息之前中断。
例马车源代码和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项目,compiler-rt。这被Clang使用,并最终在GCC中结束。以下各节中的链接指向编译器-rt项目代码,版本3.6。
ASAN已将其内部__asan_report_error
的一部分documented public interface。只要检测到违反此函数被调用,它的流量继续lib/asan/asan_report.c:938:在另一方面
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
,人们发现功能MaybePrintStackTrace被称为虽然ScopedReport destructor:
ScopedReport::~ScopedReport() {
MaybePrintStackTrace(Opts.pc, Opts.bp);
MaybeReportErrorSummary(SummaryLoc);
CommonSanitizerReportMutex.Unlock();
if (Opts.DieAfterReport || flags()->halt_on_error)
Die();
}
正如你所看到的,有杀死在错误程序的方法,但遗憾的是没有内置机制来触发调试器陷阱。那么我们找一个合适的断点。
GDB命令info functions <function name>
可以将MaybePrintStackTrace
识别为可设置断点的函数。 info functions ScopedReport::~ScopedReport
的执行给了另一个功能:__ubsan::ScopedReport::~ScopedReport
。如果这些功能都不可用(即使安装了调试符号),您可以尝试使用info functions ubsan
或info functions sanitizer
以获取所有(UndefinedBehavior)Sanitizer相关功能。
我认为,直到一个API被实现并记录在案,一个好的方法来捕获对UBSan运行时库的调用,以继续你的程序的目的是做'rbreak^__ ubsan_handle_',这将在图书馆事业之前停止执行到其分配Diag类的实例的C++领土。打开所有你想要的,然后输入'return'继续你的程序。 –
为了将来的参考,'abort_on_error'似乎对UBSAN未实现。请改为使用它:'UBSAN_OPTIONS = print_stacktrace = 1:halt_on_error = 1' – Lekensteyn
另请参阅[当-fsanitize = undefined打印某些内容时如何在调试器中断开](http://clang-developers.42468.n3.nabble.com/ How to-break-in-debugger-when-fsanitize-undefined-prints-something-td4032345.html)在Clang Dev邮件列表中。 – jww