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