1 // Check that the stack trace debugging API works and returns correct
2 // malloc and free stacks.
3 // RUN: %clangxx_asan -O0 %s -o %t && not %run %t 2>&1 | FileCheck %s
5 // FIXME: Figure out why allocation/free stack traces may be too short on ARM.
6 // REQUIRES: stable-runtime
14 #include <sanitizer/asan_interface.h>
20 mem
= (char *)malloc(10);
28 // Disable stderr buffering. Needed on Windows.
29 setvbuf(stderr
, NULL
, _IONBF
, 0);
35 size_t num_frames
= 100;
37 num_frames
= __asan_get_alloc_stack(mem
, trace
, num_frames
, &thread_id
);
39 fprintf(stderr
, "alloc stack retval %s\n", (num_frames
> 0 && num_frames
< 10)
41 // CHECK: alloc stack retval ok
42 fprintf(stderr
, "thread id = %d\n", thread_id
);
43 // CHECK: thread id = 0
44 fprintf(stderr
, "0x" PTR
"\n", trace
[0]);
45 // CHECK: [[ALLOC_FRAME_0:0x[0-9a-f]+]]
46 fprintf(stderr
, "0x" PTR
"\n", trace
[1]);
47 // CHECK: [[ALLOC_FRAME_1:0x[0-9a-f]+]]
50 num_frames
= __asan_get_free_stack(mem
, trace
, num_frames
, &thread_id
);
52 fprintf(stderr
, "free stack retval %s\n", (num_frames
> 0 && num_frames
< 10)
54 // CHECK: free stack retval ok
55 fprintf(stderr
, "thread id = %d\n", thread_id
);
56 // CHECK: thread id = 0
57 fprintf(stderr
, "0x" PTR
"\n", trace
[0]);
58 // CHECK: [[FREE_FRAME_0:0x[0-9a-f]+]]
59 fprintf(stderr
, "0x" PTR
"\n", trace
[1]);
60 // CHECK: [[FREE_FRAME_1:0x[0-9a-f]+]]
64 // CHECK: ERROR: AddressSanitizer: heap-use-after-free
65 // CHECK: WRITE of size 1 at 0x{{.*}}
66 // CHECK: freed by thread T0 here:
67 // CHECK: #0 [[FREE_FRAME_0]]
68 // CHECK: #1 [[FREE_FRAME_1]]
69 // CHECK: previously allocated by thread T0 here:
70 // CHECK: #0 [[ALLOC_FRAME_0]]
71 // CHECK: #1 [[ALLOC_FRAME_1]]