1//===-- test.c ------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Sanity test for Go runtime.
10//
11//===----------------------------------------------------------------------===//
12
13#include <sys/mman.h>
14#include <errno.h>
15#include <stdio.h>
16#include <stdlib.h>
17
18void __tsan_init(void **thr, void **proc, void (*cb)(long, void*));
19void __tsan_fini();
20void __tsan_map_shadow(void *addr, unsigned long size);
21void __tsan_go_start(void *thr, void **chthr, void *pc);
22void __tsan_go_end(void *thr);
23void __tsan_proc_create(void **pproc);
24void __tsan_proc_destroy(void *proc);
25void __tsan_proc_wire(void *proc, void *thr);
26void __tsan_proc_unwire(void *proc, void *thr);
27void __tsan_read(void *thr, void *addr, void *pc);
28void __tsan_write(void *thr, void *addr, void *pc);
29void __tsan_func_enter(void *thr, void *pc);
30void __tsan_func_exit(void *thr);
31void __tsan_malloc(void *thr, void *pc, void *p, unsigned long sz);
32void __tsan_free(void *p, unsigned long sz);
33void __tsan_acquire(void *thr, void *addr);
34void __tsan_release(void *thr, void *addr);
35void __tsan_release_merge(void *thr, void *addr);
36
37void *current_proc;
38
39void symbolize_cb(long cmd, void *ctx) {
40  switch (cmd) {
41  case 0:
42    if (current_proc == 0)
43      abort();
44    *(void**)ctx = current_proc;
45  }
46}
47
48/*
49 * See lib/tsan/rtl/tsan_platform.h for details of what the memory layout
50 * of Go programs looks like.  To prevent running over existing mappings,
51 * we pick an address slightly inside the Go heap region.
52 */
53void *go_heap = (void *)0xC011110000;
54char *buf0;
55
56void foobar() {}
57void barfoo() {}
58
59int main(void) {
60  void *thr0 = 0;
61  void *proc0 = 0;
62  __tsan_init(&thr0, &proc0, symbolize_cb);
63  current_proc = proc0;
64
65  // Allocate something resembling a heap in Go.
66  buf0 = mmap(go_heap, 16384, PROT_READ | PROT_WRITE,
67              MAP_PRIVATE | MAP_FIXED | MAP_ANON, -1, 0);
68  if (buf0 == MAP_FAILED) {
69    fprintf(stderr, "failed to allocate Go-like heap at %p; errno %d\n",
70            go_heap, errno);
71    return 1;
72  }
73  char *buf = (char*)((unsigned long)buf0 + (64<<10) - 1 & ~((64<<10) - 1));
74  __tsan_map_shadow(buf, 4096);
75  __tsan_malloc(thr0, (char*)&barfoo + 1, buf, 10);
76  __tsan_free(buf, 10);
77  __tsan_func_enter(thr0, (char*)&main + 1);
78  __tsan_malloc(thr0, (char*)&barfoo + 1, buf, 10);
79  __tsan_release(thr0, buf);
80  __tsan_release_merge(thr0, buf);
81  void *thr1 = 0;
82  __tsan_go_start(thr0, &thr1, (char*)&barfoo + 1);
83  void *thr2 = 0;
84  __tsan_go_start(thr0, &thr2, (char*)&barfoo + 1);
85  __tsan_func_exit(thr0);
86  __tsan_func_enter(thr1, (char*)&foobar + 1);
87  __tsan_func_enter(thr1, (char*)&foobar + 1);
88  __tsan_write(thr1, buf, (char*)&barfoo + 1);
89  __tsan_acquire(thr1, buf);
90  __tsan_func_exit(thr1);
91  __tsan_func_exit(thr1);
92  __tsan_go_end(thr1);
93  void *proc1 = 0;
94  __tsan_proc_create(&proc1);
95  current_proc = proc1;
96  __tsan_func_enter(thr2, (char*)&foobar + 1);
97  __tsan_read(thr2, buf, (char*)&barfoo + 1);
98  __tsan_free(buf, 10);
99  __tsan_func_exit(thr2);
100  __tsan_go_end(thr2);
101  __tsan_proc_destroy(proc1);
102  current_proc = proc0;
103  __tsan_fini();
104  return 0;
105}
106