Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 1 | // RUN: %clang_asan -O0 %s -o %t |
| 2 | // RUN: not %run %t 2>&1 | FileCheck %s |
| 3 | |
| 4 | #include <execinfo.h> |
| 5 | #include <sanitizer/common_interface_defs.h> |
| 6 | #include <stdio.h> |
| 7 | #include <stdlib.h> |
| 8 | |
| 9 | void death_function() { |
| 10 | fprintf(stderr, "DEATH CALLBACK\n"); |
| 11 | |
| 12 | void* callstack[128]; |
| 13 | int i, frames = backtrace(callstack, 128); |
| 14 | char** strs = backtrace_symbols(callstack, frames); |
| 15 | for (i = 0; i < frames; ++i) { |
| 16 | fprintf(stderr, "%s\n", strs[i]); |
| 17 | } |
| 18 | free(strs); |
| 19 | |
| 20 | fprintf(stderr, "END OF BACKTRACE\n"); |
| 21 | } |
| 22 | |
| 23 | int fault_function() { |
| 24 | char *x = (char*)malloc(10 * sizeof(char)); |
| 25 | free(x); |
| 26 | return x[5]; // BOOM |
| 27 | } |
| 28 | |
| 29 | int main() { |
| 30 | __sanitizer_set_death_callback(death_function); |
| 31 | fault_function(); |
| 32 | return 0; |
| 33 | } |
| 34 | |
| 35 | // CHECK: {{.*ERROR: AddressSanitizer: heap-use-after-free on address}} |
| 36 | // CHECK: {{READ of size 1 at 0x.* thread T0}} |
| 37 | // CHECK: {{ #0 0x.* in fault_function}} |
| 38 | |
| 39 | // CHECK: DEATH CALLBACK |
| 40 | // CHECK: death_function |
| 41 | // CHECK: fault_function |
| 42 | // CHECK: main |
| 43 | // CHECK: END OF BACKTRACE |