Source file src/cmd/cgo/internal/testsanitizers/testdata/tsan14.go

     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  //
     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  
    17  void go_callback();
    18  
    19  static void *thr(void *arg) {
    20      go_callback();
    21      return 0;
    22  }
    23  
    24  static 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  */
    33  import "C"
    34  
    35  import (
    36  	"time"
    37  )
    38  
    39  //export go_callback
    40  func go_callback() {
    41  }
    42  
    43  func 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  

View as plain text