• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
5package main
6
7// This program failed when run under the C/C++ ThreadSanitizer.
8//
9// cgocallback on a new thread calls into runtime.needm -> _cgo_getstackbound
10// to update gp.stack.lo with the stack bounds. If the G itself is passed to
11// _cgo_getstackbound, then writes to the same G can be seen on multiple
12// threads (when the G is reused after thread exit). This would trigger TSAN.
13
14/*
15#include <pthread.h>
16
17void go_callback();
18
19static void *thr(void *arg) {
20    go_callback();
21    return 0;
22}
23
24static void foo() {
25    pthread_t th;
26    pthread_attr_t attr;
27    pthread_attr_init(&attr);
28    pthread_attr_setstacksize(&attr, 256 << 10);
29    pthread_create(&th, &attr, thr, 0);
30    pthread_join(th, 0);
31}
32*/
33import "C"
34
35import (
36	"time"
37)
38
39//export go_callback
40func go_callback() {
41}
42
43func main() {
44	for i := 0; i < 2; i++ {
45		go func() {
46			for {
47				C.foo()
48			}
49		}()
50	}
51
52	time.Sleep(1000*time.Millisecond)
53}
54