xref: /openbsd-src/gnu/llvm/compiler-rt/lib/tsan/go/test.c (revision 810390e339a5425391477d5d41c78d7cab2424ac)
13cab2bb3Spatrick //===-- test.c ------------------------------------------------------------===//
23cab2bb3Spatrick //
33cab2bb3Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
43cab2bb3Spatrick // See https://llvm.org/LICENSE.txt for license information.
53cab2bb3Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
63cab2bb3Spatrick //
73cab2bb3Spatrick //===----------------------------------------------------------------------===//
83cab2bb3Spatrick //
9*810390e3Srobert // Test for Go runtime.
103cab2bb3Spatrick //
113cab2bb3Spatrick //===----------------------------------------------------------------------===//
123cab2bb3Spatrick 
133cab2bb3Spatrick #include <sys/mman.h>
143cab2bb3Spatrick #include <errno.h>
153cab2bb3Spatrick #include <stdio.h>
163cab2bb3Spatrick #include <stdlib.h>
173cab2bb3Spatrick 
183cab2bb3Spatrick void __tsan_init(void **thr, void **proc, void (*cb)(long, void*));
193cab2bb3Spatrick void __tsan_fini();
203cab2bb3Spatrick void __tsan_map_shadow(void *addr, unsigned long size);
213cab2bb3Spatrick void __tsan_go_start(void *thr, void **chthr, void *pc);
223cab2bb3Spatrick void __tsan_go_end(void *thr);
233cab2bb3Spatrick void __tsan_proc_create(void **pproc);
243cab2bb3Spatrick void __tsan_proc_destroy(void *proc);
253cab2bb3Spatrick void __tsan_proc_wire(void *proc, void *thr);
263cab2bb3Spatrick void __tsan_proc_unwire(void *proc, void *thr);
273cab2bb3Spatrick void __tsan_read(void *thr, void *addr, void *pc);
283cab2bb3Spatrick void __tsan_write(void *thr, void *addr, void *pc);
293cab2bb3Spatrick void __tsan_func_enter(void *thr, void *pc);
303cab2bb3Spatrick void __tsan_func_exit(void *thr);
313cab2bb3Spatrick void __tsan_malloc(void *thr, void *pc, void *p, unsigned long sz);
323cab2bb3Spatrick void __tsan_free(void *p, unsigned long sz);
333cab2bb3Spatrick void __tsan_acquire(void *thr, void *addr);
343cab2bb3Spatrick void __tsan_release(void *thr, void *addr);
351f9cb04fSpatrick void __tsan_release_acquire(void *thr, void *addr);
363cab2bb3Spatrick void __tsan_release_merge(void *thr, void *addr);
373cab2bb3Spatrick 
383cab2bb3Spatrick void *current_proc;
393cab2bb3Spatrick 
symbolize_cb(long cmd,void * ctx)403cab2bb3Spatrick void symbolize_cb(long cmd, void *ctx) {
413cab2bb3Spatrick   switch (cmd) {
423cab2bb3Spatrick   case 0:
433cab2bb3Spatrick     if (current_proc == 0)
443cab2bb3Spatrick       abort();
453cab2bb3Spatrick     *(void**)ctx = current_proc;
463cab2bb3Spatrick   }
473cab2bb3Spatrick }
483cab2bb3Spatrick 
493cab2bb3Spatrick /*
503cab2bb3Spatrick  * See lib/tsan/rtl/tsan_platform.h for details of what the memory layout
513cab2bb3Spatrick  * of Go programs looks like.  To prevent running over existing mappings,
523cab2bb3Spatrick  * we pick an address slightly inside the Go heap region.
533cab2bb3Spatrick  */
543cab2bb3Spatrick void *go_heap = (void *)0xC011110000;
553cab2bb3Spatrick char *buf0;
563cab2bb3Spatrick 
foobar()573cab2bb3Spatrick void foobar() {}
barfoo()583cab2bb3Spatrick void barfoo() {}
593cab2bb3Spatrick 
main(void)603cab2bb3Spatrick int main(void) {
613cab2bb3Spatrick   void *thr0 = 0;
623cab2bb3Spatrick   void *proc0 = 0;
633cab2bb3Spatrick   __tsan_init(&thr0, &proc0, symbolize_cb);
643cab2bb3Spatrick   current_proc = proc0;
653cab2bb3Spatrick 
663cab2bb3Spatrick   // Allocate something resembling a heap in Go.
673cab2bb3Spatrick   buf0 = mmap(go_heap, 16384, PROT_READ | PROT_WRITE,
683cab2bb3Spatrick               MAP_PRIVATE | MAP_FIXED | MAP_ANON, -1, 0);
693cab2bb3Spatrick   if (buf0 == MAP_FAILED) {
703cab2bb3Spatrick     fprintf(stderr, "failed to allocate Go-like heap at %p; errno %d\n",
713cab2bb3Spatrick             go_heap, errno);
723cab2bb3Spatrick     return 1;
733cab2bb3Spatrick   }
743cab2bb3Spatrick   char *buf = (char*)((unsigned long)buf0 + (64<<10) - 1 & ~((64<<10) - 1));
753cab2bb3Spatrick   __tsan_map_shadow(buf, 4096);
763cab2bb3Spatrick   __tsan_malloc(thr0, (char*)&barfoo + 1, buf, 10);
773cab2bb3Spatrick   __tsan_free(buf, 10);
783cab2bb3Spatrick   __tsan_func_enter(thr0, (char*)&main + 1);
793cab2bb3Spatrick   __tsan_malloc(thr0, (char*)&barfoo + 1, buf, 10);
803cab2bb3Spatrick   __tsan_release(thr0, buf);
811f9cb04fSpatrick   __tsan_release_acquire(thr0, buf);
823cab2bb3Spatrick   __tsan_release_merge(thr0, buf);
833cab2bb3Spatrick   void *thr1 = 0;
843cab2bb3Spatrick   __tsan_go_start(thr0, &thr1, (char*)&barfoo + 1);
853cab2bb3Spatrick   void *thr2 = 0;
863cab2bb3Spatrick   __tsan_go_start(thr0, &thr2, (char*)&barfoo + 1);
873cab2bb3Spatrick   __tsan_func_exit(thr0);
883cab2bb3Spatrick   __tsan_func_enter(thr1, (char*)&foobar + 1);
893cab2bb3Spatrick   __tsan_func_enter(thr1, (char*)&foobar + 1);
903cab2bb3Spatrick   __tsan_write(thr1, buf, (char*)&barfoo + 1);
913cab2bb3Spatrick   __tsan_acquire(thr1, buf);
923cab2bb3Spatrick   __tsan_func_exit(thr1);
933cab2bb3Spatrick   __tsan_func_exit(thr1);
943cab2bb3Spatrick   __tsan_go_end(thr1);
953cab2bb3Spatrick   void *proc1 = 0;
963cab2bb3Spatrick   __tsan_proc_create(&proc1);
973cab2bb3Spatrick   current_proc = proc1;
983cab2bb3Spatrick   __tsan_func_enter(thr2, (char*)&foobar + 1);
993cab2bb3Spatrick   __tsan_read(thr2, buf, (char*)&barfoo + 1);
1003cab2bb3Spatrick   __tsan_free(buf, 10);
1013cab2bb3Spatrick   __tsan_func_exit(thr2);
1023cab2bb3Spatrick   __tsan_go_end(thr2);
1033cab2bb3Spatrick   __tsan_proc_destroy(proc1);
1043cab2bb3Spatrick   current_proc = proc0;
1053cab2bb3Spatrick   __tsan_fini();
1063cab2bb3Spatrick   return 0;
1073cab2bb3Spatrick }
108