]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go/internal/modload: ensure that __debug_modinfo__ is not discarded during linking
authorBryan C. Mills <bcmills@google.com>
Thu, 17 Jan 2019 19:39:31 +0000 (14:39 -0500)
committerBryan C. Mills <bcmills@google.com>
Wed, 23 Jan 2019 23:13:03 +0000 (23:13 +0000)
Fixes #28753
Updates #29628

Change-Id: I4a561be7d491a0d088e656b00151ae1bdbd16a84
Reviewed-on: https://go-review.googlesource.com/c/158357
Run-TryBot: Bryan C. Mills <bcmills@google.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
src/cmd/go/internal/modload/build.go
src/cmd/go/testdata/script/mod_modinfo.txt

index 10bea15fa3ef7f2505152422fbccf047816d6615..2a8be90b78f542de486af40749d0e6b8dd2e5059 100644 (file)
@@ -252,10 +252,19 @@ func findModule(target, path string) module.Version {
 func ModInfoProg(info string) []byte {
        // Inject a variable with the debug information as runtime/debug.modinfo,
        // but compile it in package main so that it is specific to the binary.
-       // No need to populate it in an init func; it will still work with go:linkname.
+       //
+       // The variable must be a literal so that it will have the correct value
+       // before the initializer for package main runs.
+       //
+       // We also want the value to be present even if runtime/debug.modinfo is
+       // otherwise unused in the rest of the program. Reading it in an init function
+       // suffices for now.
+
        return []byte(fmt.Sprintf(`package main
 import _ "unsafe"
 //go:linkname __debug_modinfo__ runtime/debug.modinfo
 var __debug_modinfo__ = %q
+var keepalive_modinfo = __debug_modinfo__
+func init() { keepalive_modinfo = __debug_modinfo__ }
        `, string(infoStart)+info+string(infoEnd)))
 }
index f8ad18f1368a7ecd09644a1251beb4382d8d3c02..fb31f9e43b22d078625295698c2dd1f7f4c928f3 100644 (file)
@@ -7,34 +7,83 @@ cd x
 go mod edit -require=rsc.io/quote@v1.5.2
 go mod edit -replace=rsc.io/quote@v1.5.2=rsc.io/quote@v1.0.0
 
-go run main.go
-
-stderr 'Hello, world.'
+# Build a binary and ensure that it can output its own debug info.
+# The debug info should be accessible before main starts (golang.org/issue/29628).
+go build
+exec ./x$GOEXE
 stderr 'mod\s+x\s+\(devel\)'
 stderr 'dep\s+rsc.io/quote\s+v1.5.2\s+'
 stderr '=>\s+rsc.io/quote\s+v1.0.0\s+h1:'
+stderr 'Hello, world.'
+
+[short] skip
+
+# Build a binary that accesses its debug info by reading the binary directly
+# (rather than through debug.ReadBuildInfo).
+# The debug info should still be present (golang.org/issue/28753).
+cd unused
+go build
+exec ./unused$GOEXE
 
 -- x/go.mod --
 module x
 
+-- x/lib/lib.go --
+// Package lib accesses runtime/debug.modinfo before package main's init
+// functions have run.
+package lib
+
+import "runtime/debug"
+
+func init() {
+       m, ok := debug.ReadBuildInfo()
+       if !ok {
+               panic("failed debug.ReadBuildInfo")
+       }
+       println("mod", m.Main.Path, m.Main.Version)
+       for _, d := range m.Deps {
+               println("dep", d.Path, d.Version, d.Sum)
+               if r := d.Replace; r != nil {
+                       println("=>", r.Path, r.Version, r.Sum)
+               }
+       }
+}
+
 -- x/main.go --
 package main
 
-import "runtime/debug"
-import "rsc.io/quote"
+import (
+       "rsc.io/quote"
+       _ "x/lib"
+)
 
 func main() {
-  println(quote.Hello())
-
-  m, ok := debug.ReadBuildInfo()
-  if !ok {
-     panic("failed debug.ReadBuildInfo")
-  }
-  println("mod", m.Main.Path, m.Main.Version)
-  for _, d := range m.Deps {
-     println("dep", d.Path, d.Version, d.Sum)
-     if r := d.Replace; r != nil {
-        println("=>", r.Path, r.Version, r.Sum)
-     }
-  }
+       println(quote.Hello())
+}
+
+-- x/unused/main.go --
+// The unused binary does not access runtime/debug.modinfo.
+package main
+
+import (
+       "bytes"
+       "encoding/hex"
+       "io/ioutil"
+       "log"
+       "os"
+
+       _ "rsc.io/quote"
+)
+
+func main() {
+       b, err := ioutil.ReadFile(os.Args[0])
+       if err != nil {
+               log.Fatal(err)
+       }
+
+       infoStart, _ := hex.DecodeString("3077af0c9274080241e1c107e6d618e6")
+       if !bytes.Contains(b, infoStart) {
+               log.Fatal("infoStart not found in binary")
+       }
+       log.Println("ok")
 }