Skip to content

Commit 618fb4a

Browse files
ianlancetaylorgopherbot
authored andcommitted
runtime/cgo: add tsan sync for traceback function
Change-Id: Ifb8d64f18b67c8b712feec29ffb6719c6e9718ec Reviewed-on: https://go-review.googlesource.com/c/go/+/474198 TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Bryan Mills <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]>
1 parent 54d05e4 commit 618fb4a

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Copyright 2023 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
// This program failed when run under the C/C++ ThreadSanitizer.
8+
// There was no TSAN synchronization for the call to the cgo
9+
// traceback routine.
10+
11+
/*
12+
#cgo CFLAGS: -g -fsanitize=thread
13+
#cgo LDFLAGS: -g -fsanitize=thread
14+
15+
#include <pthread.h>
16+
#include <stdint.h>
17+
#include <stdlib.h>
18+
#include <sys/time.h>
19+
#include <unistd.h>
20+
21+
struct tracebackArg {
22+
uintptr_t Context;
23+
uintptr_t SigContext;
24+
uintptr_t* Buf;
25+
uintptr_t Max;
26+
};
27+
28+
void tsanTraceback(struct tracebackArg *arg) {
29+
arg->Buf[0] = 0;
30+
}
31+
32+
static void* spin(void *arg) {
33+
size_t n;
34+
struct timeval tvstart, tvnow;
35+
int diff;
36+
void *prev;
37+
void *cur;
38+
39+
prev = NULL;
40+
gettimeofday(&tvstart, NULL);
41+
for (n = 0; n < 1<<20; n++) {
42+
cur = malloc(n);
43+
free(prev);
44+
prev = cur;
45+
46+
gettimeofday(&tvnow, NULL);
47+
diff = (tvnow.tv_sec - tvstart.tv_sec) * 1000 * 1000 + (tvnow.tv_usec - tvstart.tv_usec);
48+
49+
// Profile frequency is 100Hz so we should definitely
50+
// get some signals in 50 milliseconds.
51+
if (diff > 50 * 1000) {
52+
break;
53+
}
54+
}
55+
56+
free(prev);
57+
58+
return NULL;
59+
}
60+
61+
static void runThreads(int n) {
62+
pthread_t ids[64];
63+
int i;
64+
65+
if (n > 64) {
66+
n = 64;
67+
}
68+
for (i = 0; i < n; i++) {
69+
pthread_create(&ids[i], NULL, spin, NULL);
70+
}
71+
for (i = 0; i < n; i++) {
72+
pthread_join(ids[i], NULL);
73+
}
74+
}
75+
*/
76+
import "C"
77+
78+
import (
79+
"io"
80+
"runtime"
81+
"runtime/pprof"
82+
"unsafe"
83+
)
84+
85+
func main() {
86+
runtime.SetCgoTraceback(0, unsafe.Pointer(C.tsanTraceback), nil, nil)
87+
pprof.StartCPUProfile(io.Discard)
88+
C.runThreads(C.int(runtime.GOMAXPROCS(0)))
89+
pprof.StopCPUProfile()
90+
}

misc/cgo/testsanitizers/tsan_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func TestTSAN(t *testing.T) {
4646
{src: "tsan10.go", needsRuntime: true},
4747
{src: "tsan11.go", needsRuntime: true},
4848
{src: "tsan12.go", needsRuntime: true},
49+
{src: "tsan13.go", needsRuntime: true},
4950
}
5051
for _, tc := range cases {
5152
tc := tc
@@ -63,6 +64,9 @@ func TestTSAN(t *testing.T) {
6364
if tc.needsRuntime {
6465
config.skipIfRuntimeIncompatible(t)
6566
}
67+
// If we don't see halt_on_error, the program
68+
// will only exit non-zero if we call C.exit.
69+
cmd.Env = append(cmd.Environ(), "TSAN_OPTIONS=halt_on_error=1")
6670
mustRun(t, cmd)
6771
})
6872
}

src/runtime/cgo/gcc_traceback.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ x_cgo_callers(uintptr_t sig, void *info, void *context, void (*cgoTraceback)(str
3939
__msan_unpoison(&arg, sizeof arg);
4040
#endif
4141

42+
_cgo_tsan_acquire();
4243
(*cgoTraceback)(&arg);
44+
_cgo_tsan_release();
4345
sigtramp(sig, info, context);
4446
}

0 commit comments

Comments
 (0)