File: debug_stacks.cc

package info (click to toggle)
llvm-toolchain-3.7 1%3A3.7.1-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 345,556 kB
  • ctags: 362,199
  • sloc: cpp: 2,156,381; ansic: 458,339; objc: 91,547; python: 89,988; asm: 86,305; sh: 21,479; makefile: 6,853; perl: 5,601; ml: 5,458; pascal: 3,933; lisp: 2,429; xml: 686; cs: 239; php: 202; csh: 117
file content (65 lines) | stat: -rw-r--r-- 1,825 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Check that the stack trace debugging API works and returns correct
// malloc and free stacks.
// RUN: %clangxx_asan -O0 %s -o %t && not %run %t 2>&1 | FileCheck %s

// FIXME: Figure out why allocation/free stack traces may be too short on ARM.
// REQUIRES: stable-runtime

#include <sanitizer/asan_interface.h>
#include <stdio.h>
#include <stdlib.h>

char *mem;
void func1() {
  mem = (char *)malloc(10);
}

void func2() {
  free(mem);
}

int main() {
  func1();
  func2();

  void *trace[100];
  size_t num_frames = 100;
  int thread_id;
  num_frames = __asan_get_alloc_stack(mem, trace, num_frames, &thread_id);

  fprintf(stderr, "alloc stack retval %s\n", (num_frames > 0 && num_frames < 10)
          ? "ok" : "");
  // CHECK: alloc stack retval ok
  fprintf(stderr, "thread id = %d\n", thread_id);
  // CHECK: thread id = 0
  fprintf(stderr, "0x%lx\n", trace[0]);
  // CHECK: [[ALLOC_FRAME_0:0x[0-9a-f]+]]
  fprintf(stderr, "0x%lx\n", trace[1]);
  // CHECK: [[ALLOC_FRAME_1:0x[0-9a-f]+]]

  num_frames = 100;
  num_frames = __asan_get_free_stack(mem, trace, num_frames, &thread_id);

  fprintf(stderr, "free stack retval %s\n", (num_frames > 0 && num_frames < 10)
          ? "ok" : "");
  // CHECK: free stack retval ok
  fprintf(stderr, "thread id = %d\n", thread_id);
  // CHECK: thread id = 0
  fprintf(stderr, "0x%lx\n", trace[0]);
  // CHECK: [[FREE_FRAME_0:0x[0-9a-f]+]]
  fprintf(stderr, "0x%lx\n", trace[1]);
  // CHECK: [[FREE_FRAME_1:0x[0-9a-f]+]]

  mem[0] = 'A'; // BOOM

  // CHECK: ERROR: AddressSanitizer: heap-use-after-free
  // CHECK: WRITE of size 1 at 0x{{.*}}
  // CHECK: freed by thread T0 here:
  // CHECK: #0 [[FREE_FRAME_0]]
  // CHECK: #1 [[FREE_FRAME_1]]
  // CHECK: previously allocated by thread T0 here:
  // CHECK: #0 [[ALLOC_FRAME_0]]
  // CHECK: #1 [[ALLOC_FRAME_1]]

  return 0;
}