5

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.
Szabolcs
  • 24,728
  • 9
  • 85
  • 174
  • 1
    One hacky workaround would be to have an invalid memory access in your `abort` function, that is guaranteed to trip ASAN. – icebp Feb 13 '21 at 12:04

1 Answers1

5

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)
yugr
  • 19,769
  • 3
  • 51
  • 96
  • 1
    Based on your hint to look into `asan_interface.h`, I actually found `__sanitizer_print_stack_trace()`, which works on macOS. Perhaps this is not always available? You said the API is not stable. – Szabolcs Feb 13 '21 at 17:40
  • @Szabolcs Hm, nice catch, looks like it's has been added to `include/sanitizer/common_interface_defs.h` a while ago. – yugr Feb 13 '21 at 18:33
  • @Szabolcs Can you add an answer? I'll then add a comment to mine suggesting that your solution is preferred. – yugr Feb 13 '21 at 18:39
  • Feel free to add it to your answer, and I will accept it. I would not have found this without your hints. – Szabolcs Feb 13 '21 at 19:08