]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/link: disable weak reference in itab if build with "-linkshared"
authorzhouguangyuan <zhouguangyuan.xian@gmail.com>
Wed, 15 Sep 2021 18:10:17 +0000 (02:10 +0800)
committerAlexander Rakoczy <alex@golang.org>
Wed, 13 Oct 2021 15:12:46 +0000 (15:12 +0000)
When build with "-linkshared", we can't tell if the interface method will be used or not. It can be used in shared library.

Fixes #47873

Change-Id: Iba12812f199b7679cf2fd41a304268d6d6dd03c6
Reviewed-on: https://go-review.googlesource.com/c/go/+/350189
Reviewed-by: Cherry Mui <cherryyz@google.com>
Run-TryBot: Cherry Mui <cherryyz@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Alexander Rakoczy <alex@golang.org>

misc/cgo/testshared/shared_test.go
misc/cgo/testshared/testdata/issue47837/a/a.go [new file with mode: 0644]
misc/cgo/testshared/testdata/issue47837/main/main.go [new file with mode: 0644]
src/cmd/link/internal/ld/deadcode.go

index 1bd89bc27ab462c47b0cbfb959acdb81bfe736b4..672811fe0e333b7031b7fcd41c6e4b206c882cad 100644 (file)
@@ -1070,3 +1070,11 @@ func TestIssue44031(t *testing.T) {
        goCmd(t, "install", "-buildmode=shared", "-linkshared", "./issue44031/b")
        goCmd(t, "run", "-linkshared", "./issue44031/main")
 }
+
+// Test that we use a variable from shared libraries (which implement an
+// interface in shared libraries.). A weak reference is used in the itab
+// in main process. It can cause unreacheble panic. See issue 47873.
+func TestIssue47873(t *testing.T) {
+       goCmd(t, "install", "-buildmode=shared", "-linkshared", "./issue47837/a")
+       goCmd(t, "run", "-linkshared", "./issue47837/main")
+}
diff --git a/misc/cgo/testshared/testdata/issue47837/a/a.go b/misc/cgo/testshared/testdata/issue47837/a/a.go
new file mode 100644 (file)
index 0000000..68588ed
--- /dev/null
@@ -0,0 +1,19 @@
+// Copyright 2021 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 a
+
+type A interface {
+       M()
+}
+
+//go:noinline
+func TheFuncWithArgA(a A) {
+       a.M()
+}
+
+type ImplA struct{}
+
+//go:noinline
+func (A *ImplA) M() {}
diff --git a/misc/cgo/testshared/testdata/issue47837/main/main.go b/misc/cgo/testshared/testdata/issue47837/main/main.go
new file mode 100644 (file)
index 0000000..77c6f34
--- /dev/null
@@ -0,0 +1,14 @@
+// Copyright 2021 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 (
+       "testshared/issue47837/a"
+)
+
+func main() {
+       var vara a.ImplA
+       a.TheFuncWithArgA(&vara)
+}
index 0221024d56668926d42d39ec11d73d5fc983b4b3..79acd73387aaab38a6309e17382da9dba797d3f9 100644 (file)
@@ -134,7 +134,9 @@ func (d *deadcodePass) flood() {
                methods = methods[:0]
                for i := 0; i < relocs.Count(); i++ {
                        r := relocs.At(i)
-                       if r.Weak() {
+                       // When build with "-linkshared", we can't tell if the interface
+                       // method in itab will be used or not. Ignore the weak attribute.
+                       if r.Weak() && !(d.ctxt.linkShared && d.ldr.IsItab(symIdx)) {
                                continue
                        }
                        t := r.Type()