雖然突破檢測功能(如@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