Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integrate custom assert() with AddressSanitizer

I have a custom assert()-like macro that calls abort() on failure. When using AddressSanitizer, I would prefer to have the usual nice stack trace printed on assertion failures. How can this be achieved?

  • Is it possible to get AddressSanitizer to print diagnostic information when abort() is called?
  • Is there an AddressSanitizer function I can call to manually print the stack trace?
  • Does AddressSanitizer provide a function I could use instead of abort() here?
  • I am interested in a solution both for Clang and GCC.
like image 660
Szabolcs Avatar asked Feb 13 '21 11:02

Szabolcs


1 Answers

You can use __sanitizer_print_stack_trace from sanitizer/common_interface_defs.h:

$ cat tmp.cc
#include <sanitizer/common_interface_defs.h>

int main() {
  __sanitizer_print_stack_trace();
  return 0;
}

$ g++ tmp.cc -fsanitize=address
$ ./a.out 
    #0 0x7fe00b381e58 in __sanitizer_print_stack_trace (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xece58)
    #1 0x55a059f7f802 in main (/home/yugr/a.out+0x802)
    #2 0x7fe00aec5b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    #3 0x55a059f7f719 in _start (/home/yugr/a.out+0x719)
like image 194
yugr Avatar answered Nov 15 '22 05:11

yugr