runtime: skip doInit of plugins in runtime.main
Plugins may be loaded in the user's init code.
If loading fails, md.bad is true, and doInit should not be executed.
If loading succeeds, the plugin must run modulesinit and typelinksinit
before doInit. Here is not protected by pluginsMu, and in concurrent
scenarios it is possible to obtain the moduledata of the plugin that
is still in the loading process.
Any added modules after loop starts will do their own doInit calls.
This fixes the issue introduced by CL 520375.
Fixes #75146
Change-Id: I48e91ae21615a0c54176875a6a2dea8e1dade906
Reviewed-on: https://go-review.googlesource.com/c/go/+/697675
LUCI-TryBot-Result: Go LUCI <golang...@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Keith Randall <k...@golang.org>
Reviewed-by: Keith Randall <k...@google.com>
Reviewed-by: Keith Randall <k...@golang.org>
Reviewed-by: Cherry Mui <cher...@google.com>
(cherry picked from commit ea55ca360005c607fa54304d95c6821a6ce20e7c)
diff --git a/src/cmd/cgo/internal/testplugin/plugin_test.go b/src/cmd/cgo/internal/testplugin/plugin_test.go
index 85dfd31..26ac3b2 100644
--- a/src/cmd/cgo/internal/testplugin/plugin_test.go
+++ b/src/cmd/cgo/internal/testplugin/plugin_test.go
@@ -422,3 +422,11 @@
globalSkip(t)
goCmd(t, "build", "-buildmode=plugin", "-o", "issue67976.so", "./issue67976/plugin.go")
}
+
+func TestIssue75102(t *testing.T) {
+ globalSkip(t)
+ // add gcflags different from the executable file to trigger plugin open failed.
+ goCmd(t, "build", "-gcflags=all=-N -l", "-buildmode=plugin", "-o", "issue75102.so", "./issue75102/plugin.go")
+ goCmd(t, "build", "-o", "issue75102.exe", "./issue75102/main.go")
+ run(t, "./issue75102.exe")
+}
diff --git a/src/cmd/cgo/internal/testplugin/testdata/issue75102/main.go b/src/cmd/cgo/internal/testplugin/testdata/issue75102/main.go
new file mode 100644
index 0000000..217a47b
--- /dev/null
+++ b/src/cmd/cgo/internal/testplugin/testdata/issue75102/main.go
@@ -0,0 +1,21 @@
+// Copyright 2025 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+import (
+ "fmt"
+ "plugin"
+)
+
+func init() {
+ _, err := plugin.Open("issue75102.so")
+ if err == nil {
+ panic("unexpected success to open a different version plugin")
+ }
+}
+
+func main() {
+ fmt.Println("done")
+}
diff --git a/src/cmd/cgo/internal/testplugin/testdata/issue75102/plugin.go b/src/cmd/cgo/internal/testplugin/testdata/issue75102/plugin.go
new file mode 100644
index 0000000..d09137d
--- /dev/null
+++ b/src/cmd/cgo/internal/testplugin/testdata/issue75102/plugin.go
@@ -0,0 +1,11 @@
+// Copyright 2025 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+func init() {
+ panic("unexpected call to init")
+}
+
+func main() {}
diff --git a/src/runtime/proc.go b/src/runtime/proc.go
index 5016c71..7c2b2bb 100644
--- a/src/runtime/proc.go
+++ b/src/runtime/proc.go
@@ -250,8 +250,12 @@
// by package plugin). Run through the modules in dependency
// order (the order they are initialized by the dynamic
// loader, i.e. they are added to the moduledata linked list).
- for m := &firstmoduledata; m != nil; m = m.next {
+ last := lastmoduledatap // grab before loop starts. Any added modules after this point will do their own doInit calls.
+ for m := &firstmoduledata; true; m = m.next {
doInit(m.inittasks)
+ if m == last {
+ break
+ }
}
// Disable init tracing after main init done to avoid overhead
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
runtime: skip doInit of plugins in runtime.main
Plugins may be loaded in the user's init code.
If loading fails, md.bad is true, and doInit should not be executed.
If loading succeeds, the plugin must run modulesinit and typelinksinit
before doInit. Here is not protected by pluginsMu, and in concurrent
scenarios it is possible to obtain the moduledata of the plugin that
is still in the loading process.
Any added modules after loop starts will do their own doInit calls.
This fixes the issue introduced by CL 520375.
Fixes #75147
Change-Id: I48e91ae21615a0c54176875a6a2dea8e1dade906
Reviewed-on: https://go-review.googlesource.com/c/go/+/697675
LUCI-TryBot-Result: Go LUCI <golang...@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Keith Randall <k...@golang.org>
Reviewed-by: Keith Randall <k...@google.com>
Reviewed-by: Keith Randall <k...@golang.org>
Reviewed-by: Cherry Mui <cher...@google.com>
(cherry picked from commit ea55ca360005c607fa54304d95c6821a6ce20e7c)
diff --git a/src/cmd/cgo/internal/testplugin/plugin_test.go b/src/cmd/cgo/internal/testplugin/plugin_test.go
index d2d998e..5bff810 100644
index b41bbe9..94c0194 100644
--- a/src/runtime/proc.go
+++ b/src/runtime/proc.go
@@ -252,8 +252,12 @@
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |