c - 我怎样才能打破 gdb 中的 UBSan 报告并继续?

标签 c gdb sanitizer address-sanitizer ubsan

最新版本的 GCC 和 Clang 具有未定义行为 sanitizer (UBSan),它是一个编译标志 (-fsanitize=undefined),可添加运行时检测代码。出现错误时,会显示如下警告:

packet-ber.c:1917:23: runtime error: left shift of 54645397829836991 by 8 places cannot be represented in type 'long int'

现在我想调试它并在所述行上获得调试中断。对于 Address Sanitizer (ASAN),ASAN_OPTIONS=abort_on_error=1 会导致可捕获的 fatal error 。唯一可用的 UBSan 选项是 UBSAN_OPTIONS=print_stacktrace=1这会导致报告的调用跟踪转储。然而,这不允许我检查局部变量然后继续程序。因此无法使用 -fsanitize-undefined-trap-on-error

我应该如何在 UBSan 报告中中断 gdb?虽然 break __sanitizer::SharedPrintfCode 似乎有效,但名称看​​起来很内部。

最佳答案

虽然中断检测函数(如 @Mark Plotnick@Iwillnotexist Idonotexist 所述)是一种选择,但更好的方法是中断检测后报告这些问题的函数。这种方法也用于 ASAN,它会在 __asan_report_error 上中断。 .

总结:您可以通过 __ubsan::ScopedReport::~ScopedReport 上的断点停止 ubsan 报告 __ubsan::Diag::~Diag 。这些是私有(private)实现细节,但将来可能会发生变化。已使用 GCC 4.9、5.1.0、5.2.0 和 Clang 3.3、3.4、3.6.2 进行测试。

对于来自 ppa:ubuntu-toolchain-r/test 的 GCC 4.9.2 , 你需要 libubsan0-dbg使上述断点可用。带有 Clang 3.3 和 3.4 的 Ubuntu 14.04 不支持 __ubsan::ScopedReport::~ScopedReport断点,因此您只能在使用 __ubsan::Diag::~Diag 打印消息之前中断.

错误源代码示例和 gdb session :

$ 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

详 segmentation 析如下。请注意,ASAN 和 ubsan 都源自 LLVM 项目,compiler-rt .这被 Clang 使用并最终出现在 GCC 中。以下部分中的链接指向 compiler-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 没有公共(public)接口(interface),但它当前的实现也更加简单和有限(选项更少)。出现错误时,可以在 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 ubsaninfo functions sanitizer获取所有 (UndefinedBehavior)Sanitizer 相关的函数。

关于c - 我怎样才能打破 gdb 中的 UBSan 报告并继续?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30809022/

相关文章:

c - 在 Linux 中, stub 用于标准库。为什么需要 stub ?

c++ - 有没有办法将 POSIXct 对象从 C 返回给 R?

qt - Qt找不到dll,尽管一切都已设置

clang - 地址 sanitizer 警告

c - 我如何知道 Leak Sanitizer 是否在编译时启用?

c - 使用 Malloc for i endless C -String

c++ - 为内存中的位图声明缓冲区

linux - 远程调试gdb多进程

gdb - 被调试程序的 block 输出(gdb)

debugging - 找不到 -lasan 和 libasan_preinit.o