From b7f62daa59ea5983d5825e166abc527d4ea69777 Mon Sep 17 00:00:00 2001 From: Than McIntosh Date: Tue, 15 Dec 2020 16:01:34 -0500 Subject: [PATCH] cmd/internal/goobj: add test case for object file reader Add test in which a input Go object file contains a very large number of relocations (more than 1<<20). Updates #41621. Change-Id: If1ebf3c4fefbf55ddec4e05c5299e7c48fc697d8 Reviewed-on: https://go-review.googlesource.com/c/go/+/278493 Run-TryBot: Than McIntosh TryBot-Result: Go Bot Reviewed-by: Cherry Zhang Trust: Than McIntosh --- src/cmd/internal/goobj/objfile_test.go | 62 ++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/src/cmd/internal/goobj/objfile_test.go b/src/cmd/internal/goobj/objfile_test.go index c6fd427c150..99d02a1bf1d 100644 --- a/src/cmd/internal/goobj/objfile_test.go +++ b/src/cmd/internal/goobj/objfile_test.go @@ -9,6 +9,11 @@ import ( "bytes" "cmd/internal/bio" "cmd/internal/objabi" + "fmt" + "internal/testenv" + "io/ioutil" + "os" + "os/exec" "testing" ) @@ -69,3 +74,60 @@ func TestReadWrite(t *testing.T) { t.Errorf("read Aux2 mismatch: got %v %v", a2.Type(), a2.Sym()) } } + +var issue41621prolog = ` +package main +var lines = []string{ +` + +var issue41621epilog = ` +} +func getLines() []string { + return lines +} +func main() { + println(getLines()) +} +` + +func TestIssue41621LargeNumberOfRelocations(t *testing.T) { + if testing.Short() || (objabi.GOARCH != "amd64") { + t.Skipf("Skipping large number of relocations test in short mode or on %s", objabi.GOARCH) + } + testenv.MustHaveGoBuild(t) + + tmpdir, err := ioutil.TempDir("", "lotsofrelocs") + if err != nil { + t.Fatalf("can't create temp directory: %v\n", err) + } + defer os.RemoveAll(tmpdir) + + // Emit testcase. + var w bytes.Buffer + fmt.Fprintf(&w, issue41621prolog) + for i := 0; i < 1048576+13; i++ { + fmt.Fprintf(&w, "\t\"%d\",\n", i) + } + fmt.Fprintf(&w, issue41621epilog) + err = ioutil.WriteFile(tmpdir+"/large.go", w.Bytes(), 0666) + if err != nil { + t.Fatalf("can't write output: %v\n", err) + } + + // Emit go.mod + w.Reset() + fmt.Fprintf(&w, "module issue41621\n\ngo 1.12\n") + err = ioutil.WriteFile(tmpdir+"/go.mod", w.Bytes(), 0666) + if err != nil { + t.Fatalf("can't write output: %v\n", err) + } + w.Reset() + + // Build. + cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", "large") + cmd.Dir = tmpdir + out, err := cmd.CombinedOutput() + if err != nil { + t.Fatalf("Build failed: %v, output: %s", err, out) + } +}