xref: /llvm-project/compiler-rt/test/asan/TestCases/backtrace_symbols_interceptor.cpp (revision d9377c1deda2fa1ba8ee7a0a59831d22ec57284f)
1 // RUN: %clangxx_asan -O0 %s -o %t && not %run %t 2>&1 | FileCheck %s
2 
3 // Windows does not have execinfo.h. For now, be conservative and
4 // restrict the test to glibc.
5 // REQUIRES: glibc-2.27
6 
7 // Test the backtrace_symbols() interceptor.
8 
9 #include <assert.h>
10 #include <execinfo.h>
11 #include <math.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 
15 #define MAX_BT 100
16 
main()17 int main() {
18   void **buffer = (void **)malloc(sizeof(void *) * MAX_BT);
19   assert(buffer != NULL);
20 
21   int numEntries = backtrace(buffer, MAX_BT);
22   printf("backtrace returned %d entries\n", numEntries);
23 
24   free(buffer);
25 
26   // Deliberate use-after-free of 'buffer'. We expect ASan to
27   // catch this, without triggering internal sanitizer errors.
28   char **strings = backtrace_symbols(buffer, numEntries);
29   assert(strings != NULL);
30 
31   for (int i = 0; i < numEntries; i++) {
32     printf("%s\n", strings[i]);
33   }
34 
35   free(strings);
36 
37   // CHECK: use-after-free
38   // CHECK: SUMMARY
39   return 0;
40 }
41