[go/release-branch.go1.24] runtime: skip doInit of plugins in runtime.main

2 views
Skip to first unread message

王德宇 (Gerrit)

unread,
Aug 26, 2025, 10:37:00 PMAug 26
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

王德宇 has uploaded the change for review

Commit message

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)

Change diff

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

Change information

Files:
  • M src/cmd/cgo/internal/testplugin/plugin_test.go
  • A src/cmd/cgo/internal/testplugin/testdata/issue75102/main.go
  • A src/cmd/cgo/internal/testplugin/testdata/issue75102/plugin.go
  • M src/runtime/proc.go
Change size: S
Delta: 4 files changed, 45 insertions(+), 1 deletion(-)
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement is not satisfiedMatching-Subject-Prefix
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newchange
Gerrit-Project: go
Gerrit-Branch: release-branch.go1.24
Gerrit-Change-Id: I48e91ae21615a0c54176875a6a2dea8e1dade906
Gerrit-Change-Number: 699335
Gerrit-PatchSet: 1
Gerrit-Owner: 王德宇 <wangde...@bytedance.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

王德宇 (Gerrit)

unread,
Aug 26, 2025, 10:38:00 PMAug 26
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

王德宇 has uploaded the change for review

Commit message

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)

Change diff

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 @@
Gerrit-Branch: release-branch.go1.25
Gerrit-Change-Id: I48e91ae21615a0c54176875a6a2dea8e1dade906
Gerrit-Change-Number: 699336
unsatisfied_requirement
satisfied_requirement
open
diffy

王德宇 (Gerrit)

unread,
Aug 26, 2025, 10:42:05 PMAug 26
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

王德宇 uploaded new patchset

王德宇 uploaded patch set #2 to this change.
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: newpatchset
    Gerrit-Project: go
    Gerrit-Branch: release-branch.go1.24
    Gerrit-Change-Id: I48e91ae21615a0c54176875a6a2dea8e1dade906
    Gerrit-Change-Number: 699335
    Gerrit-PatchSet: 2
    Gerrit-Owner: 王德宇 <wangde...@bytedance.com>
    unsatisfied_requirement
    satisfied_requirement
    open
    diffy

    王德宇 (Gerrit)

    unread,
    Aug 26, 2025, 10:42:50 PMAug 26
    to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

    王德宇 uploaded new patchset

    王德宇 uploaded patch set #2 to this change.
    Open in Gerrit

    Related details

    Attention set is empty
    Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: newpatchset
    Gerrit-Project: go
    Gerrit-Branch: release-branch.go1.25
    Gerrit-Change-Id: I48e91ae21615a0c54176875a6a2dea8e1dade906
    Gerrit-Change-Number: 699336
    unsatisfied_requirement
    satisfied_requirement
    open
    diffy

    Dmitri Shuralyov (Gerrit)

    unread,
    Oct 1, 2025, 3:20:28 PM (13 hours ago) Oct 1
    to 王德宇, goph...@pubsubhelper.golang.org, Dmitry Vyukov, Ian Lance Taylor, Martin Möhrmann, Gopher Robot, golang-co...@googlegroups.com, Dmitri Shuralyov

    Dmitri Shuralyov abandoned this change

    Related details

    Attention set is empty
    Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: abandon
    Gerrit-Project: go
    Gerrit-Branch: release-branch.go1.25
    Gerrit-Change-Id: I48e91ae21615a0c54176875a6a2dea8e1dade906
    Gerrit-Change-Number: 699336
    Gerrit-PatchSet: 2
    Gerrit-Owner: 王德宇 <wangde...@bytedance.com>
    Gerrit-Reviewer: Dmitry Vyukov <dvy...@google.com>
    Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
    Gerrit-Reviewer: Martin Möhrmann <moeh...@google.com>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    unsatisfied_requirement
    satisfied_requirement
    open
    diffy

    Dmitri Shuralyov (Gerrit)

    unread,
    Oct 1, 2025, 3:20:46 PM (13 hours ago) Oct 1
    to 王德宇, goph...@pubsubhelper.golang.org, Carlos Amedee, Ian Lance Taylor, Michael Pratt, Gopher Robot, golang-co...@googlegroups.com, Dmitri Shuralyov

    Dmitri Shuralyov abandoned this change

    Related details

    Attention set is empty
    Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: abandon
    Gerrit-Project: go
    Gerrit-Branch: release-branch.go1.24
    Gerrit-Change-Id: I48e91ae21615a0c54176875a6a2dea8e1dade906
    Gerrit-Change-Number: 699335
    Gerrit-PatchSet: 2
    Gerrit-Owner: 王德宇 <wangde...@bytedance.com>
    Gerrit-Reviewer: Carlos Amedee <car...@golang.org>
    Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
    Gerrit-Reviewer: Michael Pratt <mpr...@google.com>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    unsatisfied_requirement
    satisfied_requirement
    open
    diffy
    Reply all
    Reply to author
    Forward
    0 new messages