Commit 3b22ffc0 authored by David Crawshaw's avatar David Crawshaw

runtime: make cgocallback wait on package init

With the new buildmodes c-archive and c-shared, it is possible for a
cgo call to come in early in the lifecycle of a Go program. Calls
before the runtime has been initialized are caught by
_cgo_wait_runtime_init_done. However a call can come in after the
runtime has initialized, but before the program's package init
functions have finished running.

To avoid this cgocallback checks m.ncgo to see if we are on a thread
running Go. If not, we may be a foreign thread and it blocks until
main_init is complete.

Change-Id: I7a9f137fa2a40c322a0b93764261f9aa17fcf5b8
Reviewed-on: https://go-review.googlesource.com/8897Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
Run-TryBot: David Crawshaw <crawshaw@golang.org>
parent cea272de
...@@ -12,13 +12,13 @@ extern int32_t FromPkg(); ...@@ -12,13 +12,13 @@ extern int32_t FromPkg();
int main(void) { int main(void) {
int32_t res; int32_t res;
if (DidMainRun()) { if (!DidInitRun()) {
fprintf(stderr, "ERROR: buildmode=c-archive should not run main\n"); fprintf(stderr, "ERROR: buildmode=c-archive init should run\n");
return 2; return 2;
} }
if (!DidInitRun()) { if (DidMainRun()) {
fprintf(stderr, "ERROR: buildmode=c-archive init should run\n"); fprintf(stderr, "ERROR: buildmode=c-archive should not run main\n");
return 2; return 2;
} }
......
...@@ -4,21 +4,39 @@ ...@@ -4,21 +4,39 @@
package main package main
import _ "p" import (
_ "p"
"syscall"
"time"
)
import "C" import "C"
var ( var initCh = make(chan int, 1)
ranInit bool var ranMain bool
ranMain bool
)
func init() { ranInit = true } func init() {
// emulate an exceedingly slow package initialization function
time.Sleep(100 * time.Millisecond)
initCh <- 42
}
func main() { ranMain = true } func main() { ranMain = true }
//export DidInitRun //export DidInitRun
func DidInitRun() bool { return ranInit } func DidInitRun() bool {
select {
case x := <-initCh:
if x != 42 {
// Just in case initCh was not correctly made.
println("want init value of 42, got: ", x)
syscall.Exit(2)
}
return true
default:
return false
}
}
//export DidMainRun //export DidMainRun
func DidMainRun() bool { return ranMain } func DidMainRun() bool { return ranMain }
...@@ -193,6 +193,14 @@ func cgocallbackg1() { ...@@ -193,6 +193,14 @@ func cgocallbackg1() {
systemstack(newextram) systemstack(newextram)
} }
if gp.m.ncgo == 0 {
// The C call to Go came from a thread not currently running
// any Go. In the case of -buildmode=c-archive or c-shared,
// this call may be coming in before package initialization
// is complete. Wait until it is.
<-main_init_done
}
// Add entry to defer stack in case of panic. // Add entry to defer stack in case of panic.
restore := true restore := true
defer unwindm(&restore) defer unwindm(&restore)
......
...@@ -12,6 +12,12 @@ func runtime_init() ...@@ -12,6 +12,12 @@ func runtime_init()
//go:linkname main_init main.init //go:linkname main_init main.init
func main_init() func main_init()
// main_init_done is a signal used by cgocallbackg that initialization
// has been completed. It is made before _cgo_notify_runtime_init_done,
// so all cgo calls can rely on it existing. When main_init is complete,
// it is closed, meaning cgocallbackg can reliably receive from it.
var main_init_done chan bool
//go:linkname main_main main.main //go:linkname main_main main.main
func main_main() func main_main()
...@@ -70,6 +76,7 @@ func main() { ...@@ -70,6 +76,7 @@ func main() {
// Allocate new M as main_main() is expected to block forever. // Allocate new M as main_main() is expected to block forever.
systemstack(newextram) systemstack(newextram)
} }
main_init_done = make(chan bool)
if iscgo { if iscgo {
if _cgo_thread_start == nil { if _cgo_thread_start == nil {
throw("_cgo_thread_start missing") throw("_cgo_thread_start missing")
...@@ -95,6 +102,7 @@ func main() { ...@@ -95,6 +102,7 @@ func main() {
} }
main_init() main_init()
close(main_init_done)
needUnlock = false needUnlock = false
unlockOSThread() unlockOSThread()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment