From 91aa2f190aa07addb964afa48f98466bdb1a8556 Mon Sep 17 00:00:00 2001 From: zhouguangyuan Date: Thu, 16 Sep 2021 02:10:17 +0800 Subject: [release-branch.go1.17] cmd/link: disable weak reference in itab if build with "-linkshared" When build with "-linkshared", we can't tell if the interface method will be used or not. It can be used in shared library. This CL backport this fix to 1.17. Fixes #49086 Change-Id: Iba12812f199b7679cf2fd41a304268d6d6dd03c6 Reviewed-on: https://go-review.googlesource.com/c/go/+/350189 Reviewed-by: Cherry Mui Run-TryBot: Cherry Mui TryBot-Result: Go Bot Trust: Alexander Rakoczy (cherry picked from commit f687831e4cfa06d19533d47ae93c0451bd1ca688) Reviewed-on: https://go-review.googlesource.com/c/go/+/357231 Trust: Ian Lance Taylor --- misc/cgo/testshared/shared_test.go | 8 ++++++++ misc/cgo/testshared/testdata/issue47837/a/a.go | 19 +++++++++++++++++++ misc/cgo/testshared/testdata/issue47837/main/main.go | 14 ++++++++++++++ src/cmd/link/internal/ld/deadcode.go | 4 +++- 4 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 misc/cgo/testshared/testdata/issue47837/a/a.go create mode 100644 misc/cgo/testshared/testdata/issue47837/main/main.go diff --git a/misc/cgo/testshared/shared_test.go b/misc/cgo/testshared/shared_test.go index e77f848915..0568ac773f 100644 --- a/misc/cgo/testshared/shared_test.go +++ b/misc/cgo/testshared/shared_test.go @@ -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 index 0000000000..68588eda2f --- /dev/null +++ b/misc/cgo/testshared/testdata/issue47837/a/a.go @@ -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 index 0000000000..77c6f34379 --- /dev/null +++ b/misc/cgo/testshared/testdata/issue47837/main/main.go @@ -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) +} diff --git a/src/cmd/link/internal/ld/deadcode.go b/src/cmd/link/internal/ld/deadcode.go index 416e5da398..e4fa75f8e1 100644 --- a/src/cmd/link/internal/ld/deadcode.go +++ b/src/cmd/link/internal/ld/deadcode.go @@ -132,7 +132,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() -- cgit v1.2.3-54-g00ecf