• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- test.c ------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Sanity test for Go runtime.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include <stdio.h>
15 #include <stdlib.h>
16 
17 void __tsan_init(void **thr, void **proc, void (*cb)(long, void*));
18 void __tsan_fini();
19 void __tsan_map_shadow(void *addr, unsigned long size);
20 void __tsan_go_start(void *thr, void **chthr, void *pc);
21 void __tsan_go_end(void *thr);
22 void __tsan_proc_create(void **pproc);
23 void __tsan_proc_destroy(void *proc);
24 void __tsan_proc_wire(void *proc, void *thr);
25 void __tsan_proc_unwire(void *proc, void *thr);
26 void __tsan_read(void *thr, void *addr, void *pc);
27 void __tsan_write(void *thr, void *addr, void *pc);
28 void __tsan_func_enter(void *thr, void *pc);
29 void __tsan_func_exit(void *thr);
30 void __tsan_malloc(void *thr, void *pc, void *p, unsigned long sz);
31 void __tsan_free(void *p, unsigned long sz);
32 void __tsan_acquire(void *thr, void *addr);
33 void __tsan_release(void *thr, void *addr);
34 void __tsan_release_merge(void *thr, void *addr);
35 
36 void *current_proc;
37 
symbolize_cb(long cmd,void * ctx)38 void symbolize_cb(long cmd, void *ctx) {
39   switch (cmd) {
40   case 0:
41     if (current_proc == 0)
42       abort();
43     *(void**)ctx = current_proc;
44   }
45 }
46 
47 char buf0[100<<10];
48 
foobar()49 void foobar() {}
barfoo()50 void barfoo() {}
51 
main(void)52 int main(void) {
53   void *thr0 = 0;
54   void *proc0 = 0;
55   __tsan_init(&thr0, &proc0, symbolize_cb);
56   current_proc = proc0;
57   char *buf = (char*)((unsigned long)buf0 + (64<<10) - 1 & ~((64<<10) - 1));
58   __tsan_map_shadow(buf, 4096);
59   __tsan_malloc(thr0, (char*)&barfoo + 1, buf, 10);
60   __tsan_free(buf, 10);
61   __tsan_func_enter(thr0, (char*)&main + 1);
62   __tsan_malloc(thr0, (char*)&barfoo + 1, buf, 10);
63   __tsan_release(thr0, buf);
64   __tsan_release_merge(thr0, buf);
65   void *thr1 = 0;
66   __tsan_go_start(thr0, &thr1, (char*)&barfoo + 1);
67   void *thr2 = 0;
68   __tsan_go_start(thr0, &thr2, (char*)&barfoo + 1);
69   __tsan_func_exit(thr0);
70   __tsan_func_enter(thr1, (char*)&foobar + 1);
71   __tsan_func_enter(thr1, (char*)&foobar + 1);
72   __tsan_write(thr1, buf, (char*)&barfoo + 1);
73   __tsan_acquire(thr1, buf);
74   __tsan_func_exit(thr1);
75   __tsan_func_exit(thr1);
76   __tsan_go_end(thr1);
77   void *proc1 = 0;
78   __tsan_proc_create(&proc1);
79   current_proc = proc1;
80   __tsan_func_enter(thr2, (char*)&foobar + 1);
81   __tsan_read(thr2, buf, (char*)&barfoo + 1);
82   __tsan_free(buf, 10);
83   __tsan_func_exit(thr2);
84   __tsan_go_end(thr2);
85   __tsan_proc_destroy(proc1);
86   current_proc = proc0;
87   __tsan_fini();
88   return 0;
89 }
90