offset := (signext24(r.Add&0xffffff) + 2) * 4
var tramp *sym.Symbol
for i := 0; ; i++ {
- name := r.Sym.Name + fmt.Sprintf("%+d-tramp%d", offset, i)
+ oName := r.Sym.Name
+ name := oName + fmt.Sprintf("%+d-tramp%d", offset, i)
tramp = ctxt.Syms.Lookup(name, int(r.Sym.Version))
+ if oName == "runtime.deferreturn" {
+ tramp.Attr.Set(sym.AttrDeferReturnTramp, true)
+ }
if tramp.Type == sym.SDYNIMPORT {
// don't reuse trampoline defined in other module
continue
// target is at some offset within the function. Calls to duff+8 and duff+256 must appear as
// distinct trampolines.
- name := r.Sym.Name
+ oName := r.Sym.Name
+ name := oName
if r.Add == 0 {
name = name + fmt.Sprintf("-tramp%d", i)
} else {
// Look up the trampoline in case it already exists
tramp = ctxt.Syms.Lookup(name, int(r.Sym.Version))
+ if oName == "runtime.deferreturn" {
+ tramp.Attr.Set(sym.AttrDeferReturnTramp, true)
+ }
if tramp.Value == 0 {
break
}
// AttrReadOnly indicates whether the symbol's content (Symbol.P) is backed by
// read-only memory.
AttrReadOnly
- // 19 attributes defined so far.
+ // AttrDeferReturnTramp indicates the symbol is a trampoline of a deferreturn
+ // call.
+ AttrDeferReturnTramp
+ // 20 attributes defined so far.
)
func (a Attribute) DuplicateOK() bool { return a&AttrDuplicateOK != 0 }
func (a Attribute) Container() bool { return a&AttrContainer != 0 }
func (a Attribute) TopFrame() bool { return a&AttrTopFrame != 0 }
func (a Attribute) ReadOnly() bool { return a&AttrReadOnly != 0 }
+func (a Attribute) DeferReturnTramp() bool { return a&AttrDeferReturnTramp != 0 }
func (a Attribute) CgoExport() bool {
return a.CgoExportDynamic() || a.CgoExportStatic()
t.Errorf("unexpected output:\n%s", out)
}
}
+
+const testTrampSrc = `
+package main
+import "fmt"
+func main() {
+ fmt.Println("hello")
+
+ defer func(){
+ if e := recover(); e == nil {
+ panic("did not panic")
+ }
+ }()
+ f1()
+}
+
+// Test deferreturn trampolines. See issue #39049.
+func f1() { defer f2() }
+func f2() { panic("XXX") }
+`
+
+func TestTrampoline(t *testing.T) {
+ // Test that trampoline insertion works as expected.
+ // For stress test, we set -debugtramp=2 flag, which sets a very low
+ // threshold for trampoline generation, and essentially all cross-package
+ // calls will use trampolines.
+ switch runtime.GOARCH {
+ case "arm", "ppc64", "ppc64le":
+ default:
+ t.Skipf("trampoline insertion is not implemented on %s", runtime.GOARCH)
+ }
+ if runtime.GOOS == "aix" {
+ t.Skip("trampolines on AIX doesn't work in Go 1.14") // fixed in Go 1.15
+ }
+
+ testenv.MustHaveGoBuild(t)
+
+ tmpdir, err := ioutil.TempDir("", "TestTrampoline")
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer os.RemoveAll(tmpdir)
+
+ src := filepath.Join(tmpdir, "hello.go")
+ err = ioutil.WriteFile(src, []byte(testTrampSrc), 0666)
+ if err != nil {
+ t.Fatal(err)
+ }
+ exe := filepath.Join(tmpdir, "hello.exe")
+
+ cmd := exec.Command(testenv.GoToolPath(t), "build", "-ldflags=-debugtramp=2", "-o", exe, src)
+ out, err := cmd.CombinedOutput()
+ if err != nil {
+ t.Fatalf("build failed: %v\n%s", err, out)
+ }
+ cmd = exec.Command(exe)
+ out, err = cmd.CombinedOutput()
+ if err != nil {
+ t.Errorf("executable failed to run: %v\n%s", err, out)
+ }
+ if string(out) != "hello\n" {
+ t.Errorf("unexpected output:\n%s", out)
+ }
+}