Skip to content

Commit eb75219

Browse files
committed
[release-branch.go1.17] cmd/link: mark unexported methods for plugins
When plugin is used, we already mark all exported methods reachable. However, when the plugin and the host program share a common package, an unexported method could also be reachable from both the plugin and the host via interfaces. We need to mark them as well. Fixes #51736. Updates #51621. Change-Id: I1a70d3f96b66b803f2d0ab14d00ed0df276ea500 Reviewed-on: https://go-review.googlesource.com/c/go/+/393365 Trust: Cherry Mui <[email protected]> Run-TryBot: Cherry Mui <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Than McIntosh <[email protected]> (cherry picked from commit 91631bc) Reviewed-on: https://go-review.googlesource.com/c/go/+/397484
1 parent 4e69fdd commit eb75219

File tree

5 files changed

+67
-1
lines changed

5 files changed

+67
-1
lines changed

misc/cgo/testplugin/plugin_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,12 @@ func TestMethod2(t *testing.T) {
287287
run(t, "./method2.exe")
288288
}
289289

290+
func TestMethod3(t *testing.T) {
291+
goCmd(t, "build", "-buildmode=plugin", "-o", "method3.so", "./method3/plugin.go")
292+
goCmd(t, "build", "-o", "method3.exe", "./method3/main.go")
293+
run(t, "./method3.exe")
294+
}
295+
290296
func TestIssue44956(t *testing.T) {
291297
goCmd(t, "build", "-buildmode=plugin", "-o", "issue44956p1.so", "./issue44956/plugin1.go")
292298
goCmd(t, "build", "-buildmode=plugin", "-o", "issue44956p2.so", "./issue44956/plugin2.go")
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2022 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+
// An unexported method can be reachable from the plugin via interface
6+
// when a package is shared. So it need to be live.
7+
8+
package main
9+
10+
import (
11+
"plugin"
12+
13+
"testplugin/method3/p"
14+
)
15+
16+
var i p.I
17+
18+
func main() {
19+
pl, err := plugin.Open("method3.so")
20+
if err != nil {
21+
panic(err)
22+
}
23+
24+
f, err := pl.Lookup("F")
25+
if err != nil {
26+
panic(err)
27+
}
28+
29+
f.(func())()
30+
31+
i = p.T(123)
32+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2022 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 p
6+
7+
type T int
8+
9+
func (T) m() { println("m") }
10+
11+
type I interface { m() }
12+
13+
func F() {
14+
i.m()
15+
}
16+
17+
var i I = T(123)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2022 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 main
6+
7+
import "testplugin/method3/p"
8+
9+
func main() {}
10+
11+
func F() { p.F() }

src/cmd/link/internal/ld/deadcode.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ func deadcode(ctxt *Link) {
350350
// in the last pass.
351351
rem := d.markableMethods[:0]
352352
for _, m := range d.markableMethods {
353-
if (d.reflectSeen && m.isExported()) || d.ifaceMethod[m.m] {
353+
if (d.reflectSeen && (m.isExported() || d.dynlink)) || d.ifaceMethod[m.m] {
354354
d.markMethod(m)
355355
} else {
356356
rem = append(rem, m)

0 commit comments

Comments
 (0)