go_linux_amd64.go 2.8 KB

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