aboutsummaryrefslogtreecommitdiffstats
path: root/gcc-4.9/gcc/testsuite/g++.dg/asan/deep-thread-stack-1.C
blob: d5d85c42d93f3883372eee444f4e56c5f0f3d8e5 (plain)
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
// { dg-do run { target pthread } }
// { dg-options "-lasan -lpthread" }
// { dg-shouldfail "asan" }

#include <pthread.h>

int *x;

void *AllocThread(void *arg) {
  x = new int;
  *x = 42;
  return NULL;
}

void *FreeThread(void *arg) {
  delete x;
  return NULL;
}

void *AccessThread(void *arg) {
  *x = 43;  // BOOM
  return NULL;
}

typedef void* (*callback_type)(void* arg);

void *RunnerThread(void *function) {
  pthread_t thread;
  pthread_create(&thread, NULL, (callback_type)function, NULL);
  pthread_join(thread, NULL);
  return NULL;
}

void RunThread(callback_type function) {
  pthread_t runner;
  pthread_create(&runner, NULL, RunnerThread, (void*)function);
  pthread_join(runner, NULL);
}

int main(int argc, char *argv[]) {
  RunThread(AllocThread);
  RunThread(FreeThread);
  RunThread(AccessThread);
  return (x != 0);
}

// { dg-output "ERROR: AddressSanitizer: heap-use-after-free.*(\n|\r\n|\r)" }
// { dg-output "\[^\n\r]*WRITE of size 4 at 0x\[0-9a-f\]+ thread T(\[0-9\]+).*(\n|\r\n|\r)" }
// { dg-output "\[^\n\r]*freed by thread T(\[0-9\]+) here:.*(\n|\r\n|\r)" }
// { dg-output "\[^\n\r]*previously allocated by thread T(\[0-9\]+) here:.*(\n|\r\n|\r)" }
// { dg-output "Thread T\\2 created by T(\[0-9\]+) here:.*(\n|\r\n|\r)" }
// { dg-output "Thread T\\8 created by T0 here:.*(\n|\r\n|\r)" }
// { dg-output "Thread T\\4 created by T(\[0-9\]+) here:.*(\n|\r\n|\r)" }
// { dg-output "Thread T\\11 created by T0 here:.*(\n|\r\n|\r)" }
// { dg-output "Thread T\\6 created by T(\[0-9\]+) here:.*(\n|\r\n|\r)" }
// { dg-output "Thread T\\14 created by T0 here:" }