github.com/jwijenbergh/purego@v0.0.0-20240126093400-70ff3a61df13/internal/fakecgo/go_linux_amd64.go (about)

     1  // Copyright 2011 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 fakecgo
     6  
     7  import "unsafe"
     8  
     9  //go:nosplit
    10  func _cgo_sys_thread_start(ts *ThreadStart) {
    11  	var attr pthread_attr_t
    12  	var ign, oset sigset_t
    13  	var p pthread_t
    14  	var size size_t
    15  	var err int
    16  
    17  	//fprintf(stderr, "runtime/cgo: _cgo_sys_thread_start: fn=%p, g=%p\n", ts->fn, ts->g); // debug
    18  	sigfillset(&ign)
    19  	pthread_sigmask(SIG_SETMASK, &ign, &oset)
    20  
    21  	pthread_attr_init(&attr)
    22  	pthread_attr_getstacksize(&attr, &size)
    23  	// Leave stacklo=0 and set stackhi=size; mstart will do the rest.
    24  	ts.g.stackhi = uintptr(size)
    25  
    26  	err = _cgo_try_pthread_create(&p, &attr, unsafe.Pointer(threadentry_trampolineABI0), ts)
    27  
    28  	pthread_sigmask(SIG_SETMASK, &oset, nil)
    29  
    30  	if err != 0 {
    31  		print("fakecgo: pthread_create failed: ")
    32  		println(err)
    33  		abort()
    34  	}
    35  }
    36  
    37  // threadentry_trampolineABI0 maps the C ABI to Go ABI then calls the Go function
    38  //
    39  //go:linkname x_threadentry_trampoline threadentry_trampoline
    40  var x_threadentry_trampoline byte
    41  var threadentry_trampolineABI0 = &x_threadentry_trampoline
    42  
    43  //go:nosplit
    44  func threadentry(v unsafe.Pointer) unsafe.Pointer {
    45  	ts := *(*ThreadStart)(v)
    46  	free(v)
    47  
    48  	setg_trampoline(setg_func, uintptr(unsafe.Pointer(ts.g)))
    49  
    50  	// faking funcs in go is a bit a... involved - but the following works :)
    51  	fn := uintptr(unsafe.Pointer(&ts.fn))
    52  	(*(*func())(unsafe.Pointer(&fn)))()
    53  
    54  	return nil
    55  }
    56  
    57  // here we will store a pointer to the provided setg func
    58  var setg_func uintptr
    59  
    60  //go:nosplit
    61  func x_cgo_init(g *G, setg uintptr) {
    62  	var size size_t
    63  	var attr *pthread_attr_t
    64  
    65  	/* The memory sanitizer distributed with versions of clang
    66  	   before 3.8 has a bug: if you call mmap before malloc, mmap
    67  	   may return an address that is later overwritten by the msan
    68  	   library.  Avoid this problem by forcing a call to malloc
    69  	   here, before we ever call malloc.
    70  
    71  	   This is only required for the memory sanitizer, so it's
    72  	   unfortunate that we always run it.  It should be possible
    73  	   to remove this when we no longer care about versions of
    74  	   clang before 3.8.  The test for this is
    75  	   misc/cgo/testsanitizers.
    76  
    77  	   GCC works hard to eliminate a seemingly unnecessary call to
    78  	   malloc, so we actually use the memory we allocate.  */
    79  
    80  	setg_func = setg
    81  	attr = (*pthread_attr_t)(malloc(unsafe.Sizeof(*attr)))
    82  	if attr == nil {
    83  		println("fakecgo: malloc failed")
    84  		abort()
    85  	}
    86  	pthread_attr_init(attr)
    87  	pthread_attr_getstacksize(attr, &size)
    88  	// runtime/cgo uses __builtin_frame_address(0) instead of `uintptr(unsafe.Pointer(&size))`
    89  	// but this should be OK since we are taking the address of the first variable in this function.
    90  	g.stacklo = uintptr(unsafe.Pointer(&size)) - uintptr(size) + 4096
    91  	pthread_attr_destroy(attr)
    92  	free(unsafe.Pointer(attr))
    93  }