]> git.ipfire.org Git - thirdparty/gcc.git/blob - libgo/misc/cgo/testshared/shared_test.go
libgo: update to Go1.14beta1
[thirdparty/gcc.git] / libgo / misc / cgo / testshared / shared_test.go
1 // Copyright 2015 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package shared_test
6
7 import (
8 "bufio"
9 "bytes"
10 "debug/elf"
11 "encoding/binary"
12 "flag"
13 "fmt"
14 "go/build"
15 "io"
16 "io/ioutil"
17 "log"
18 "os"
19 "os/exec"
20 "path/filepath"
21 "regexp"
22 "runtime"
23 "sort"
24 "strings"
25 "testing"
26 "time"
27 )
28
29 var gopathInstallDir, gorootInstallDir string
30
31 // This is the smallest set of packages we can link into a shared
32 // library (runtime/cgo is built implicitly).
33 var minpkgs = []string{"runtime", "sync/atomic"}
34 var soname = "libruntime,sync-atomic.so"
35
36 var testX = flag.Bool("testx", false, "if true, pass -x to 'go' subcommands invoked by the test")
37 var testWork = flag.Bool("testwork", false, "if true, log and do not delete the temporary working directory")
38
39 // run runs a command and calls t.Errorf if it fails.
40 func run(t *testing.T, msg string, args ...string) {
41 c := exec.Command(args[0], args[1:]...)
42 if output, err := c.CombinedOutput(); err != nil {
43 t.Errorf("executing %s (%s) failed %s:\n%s", strings.Join(args, " "), msg, err, output)
44 }
45 }
46
47 // goCmd invokes the go tool with the installsuffix set up by TestMain. It calls
48 // t.Fatalf if the command fails.
49 func goCmd(t *testing.T, args ...string) string {
50 newargs := []string{args[0]}
51 if *testX {
52 newargs = append(newargs, "-x")
53 }
54 newargs = append(newargs, args[1:]...)
55 c := exec.Command("go", newargs...)
56 stderr := new(strings.Builder)
57 c.Stderr = stderr
58
59 if testing.Verbose() && t == nil {
60 fmt.Fprintf(os.Stderr, "+ go %s\n", strings.Join(args, " "))
61 c.Stderr = os.Stderr
62 }
63 output, err := c.Output()
64
65 if err != nil {
66 if t != nil {
67 t.Helper()
68 t.Fatalf("executing %s failed %v:\n%s", strings.Join(c.Args, " "), err, stderr)
69 } else {
70 // Panic instead of using log.Fatalf so that deferred cleanup may run in testMain.
71 log.Panicf("executing %s failed %v:\n%s", strings.Join(c.Args, " "), err, stderr)
72 }
73 }
74 if testing.Verbose() && t != nil {
75 t.Logf("go %s", strings.Join(args, " "))
76 if stderr.Len() > 0 {
77 t.Logf("%s", stderr)
78 }
79 }
80 return string(bytes.TrimSpace(output))
81 }
82
83 // TestMain calls testMain so that the latter can use defer (TestMain exits with os.Exit).
84 func testMain(m *testing.M) (int, error) {
85 workDir, err := ioutil.TempDir("", "shared_test")
86 if err != nil {
87 return 0, err
88 }
89 if *testWork || testing.Verbose() {
90 fmt.Printf("+ mkdir -p %s\n", workDir)
91 }
92 if !*testWork {
93 defer os.RemoveAll(workDir)
94 }
95
96 // Some tests need to edit the source in GOPATH, so copy this directory to a
97 // temporary directory and chdir to that.
98 gopath := filepath.Join(workDir, "gopath")
99 modRoot, err := cloneTestdataModule(gopath)
100 if err != nil {
101 return 0, err
102 }
103 if testing.Verbose() {
104 fmt.Printf("+ export GOPATH=%s\n", gopath)
105 fmt.Printf("+ cd %s\n", modRoot)
106 }
107 os.Setenv("GOPATH", gopath)
108 os.Chdir(modRoot)
109 os.Setenv("PWD", modRoot)
110
111 // The test also needs to install libraries into GOROOT/pkg, so copy the
112 // subset of GOROOT that we need.
113 //
114 // TODO(golang.org/issue/28553): Rework -buildmode=shared so that it does not
115 // need to write to GOROOT.
116 goroot := filepath.Join(workDir, "goroot")
117 if err := cloneGOROOTDeps(goroot); err != nil {
118 return 0, err
119 }
120 if testing.Verbose() {
121 fmt.Fprintf(os.Stderr, "+ export GOROOT=%s\n", goroot)
122 }
123 os.Setenv("GOROOT", goroot)
124
125 myContext := build.Default
126 myContext.GOROOT = goroot
127 myContext.GOPATH = gopath
128 runtimeP, err := myContext.Import("runtime", ".", build.ImportComment)
129 if err != nil {
130 return 0, fmt.Errorf("import failed: %v", err)
131 }
132 gorootInstallDir = runtimeP.PkgTargetRoot + "_dynlink"
133
134 // All tests depend on runtime being built into a shared library. Because
135 // that takes a few seconds, do it here and have all tests use the version
136 // built here.
137 goCmd(nil, append([]string{"install", "-buildmode=shared"}, minpkgs...)...)
138
139 myContext.InstallSuffix = "_dynlink"
140 depP, err := myContext.Import("./depBase", ".", build.ImportComment)
141 if err != nil {
142 return 0, fmt.Errorf("import failed: %v", err)
143 }
144 if depP.PkgTargetRoot == "" {
145 gopathInstallDir = filepath.Dir(goCmd(nil, "list", "-buildmode=shared", "-f", "{{.Target}}", "./depBase"))
146 } else {
147 gopathInstallDir = filepath.Join(depP.PkgTargetRoot, "testshared")
148 }
149 return m.Run(), nil
150 }
151
152 func TestMain(m *testing.M) {
153 log.SetFlags(log.Lshortfile)
154 flag.Parse()
155
156 // Some of the tests install binaries into a custom GOPATH.
157 // That won't work if GOBIN is set.
158 os.Unsetenv("GOBIN")
159
160 exitCode, err := testMain(m)
161 if err != nil {
162 log.Fatal(err)
163 }
164 os.Exit(exitCode)
165 }
166
167 // cloneTestdataModule clones the packages from src/testshared into gopath.
168 // It returns the directory within gopath at which the module root is located.
169 func cloneTestdataModule(gopath string) (string, error) {
170 modRoot := filepath.Join(gopath, "src", "testshared")
171 if err := overlayDir(modRoot, "testdata"); err != nil {
172 return "", err
173 }
174 if err := ioutil.WriteFile(filepath.Join(modRoot, "go.mod"), []byte("module testshared\n"), 0644); err != nil {
175 return "", err
176 }
177 return modRoot, nil
178 }
179
180 // cloneGOROOTDeps copies (or symlinks) the portions of GOROOT/src and
181 // GOROOT/pkg relevant to this test into the given directory.
182 // It must be run from within the testdata module.
183 func cloneGOROOTDeps(goroot string) error {
184 oldGOROOT := strings.TrimSpace(goCmd(nil, "env", "GOROOT"))
185 if oldGOROOT == "" {
186 return fmt.Errorf("go env GOROOT returned an empty string")
187 }
188
189 // Before we clone GOROOT, figure out which packages we need to copy over.
190 listArgs := []string{
191 "list",
192 "-deps",
193 "-f", "{{if and .Standard (not .ForTest)}}{{.ImportPath}}{{end}}",
194 }
195 stdDeps := goCmd(nil, append(listArgs, minpkgs...)...)
196 testdataDeps := goCmd(nil, append(listArgs, "-test", "./...")...)
197
198 pkgs := append(strings.Split(strings.TrimSpace(stdDeps), "\n"),
199 strings.Split(strings.TrimSpace(testdataDeps), "\n")...)
200 sort.Strings(pkgs)
201 var pkgRoots []string
202 for _, pkg := range pkgs {
203 parentFound := false
204 for _, prev := range pkgRoots {
205 if strings.HasPrefix(pkg, prev) {
206 // We will copy in the source for pkg when we copy in prev.
207 parentFound = true
208 break
209 }
210 }
211 if !parentFound {
212 pkgRoots = append(pkgRoots, pkg)
213 }
214 }
215
216 gorootDirs := []string{
217 "pkg/tool",
218 "pkg/include",
219 }
220 for _, pkg := range pkgRoots {
221 gorootDirs = append(gorootDirs, filepath.Join("src", pkg))
222 }
223
224 for _, dir := range gorootDirs {
225 if testing.Verbose() {
226 fmt.Fprintf(os.Stderr, "+ cp -r %s %s\n", filepath.Join(goroot, dir), filepath.Join(oldGOROOT, dir))
227 }
228 if err := overlayDir(filepath.Join(goroot, dir), filepath.Join(oldGOROOT, dir)); err != nil {
229 return err
230 }
231 }
232
233 return nil
234 }
235
236 // The shared library was built at the expected location.
237 func TestSOBuilt(t *testing.T) {
238 _, err := os.Stat(filepath.Join(gorootInstallDir, soname))
239 if err != nil {
240 t.Error(err)
241 }
242 }
243
244 func hasDynTag(f *elf.File, tag elf.DynTag) bool {
245 ds := f.SectionByType(elf.SHT_DYNAMIC)
246 if ds == nil {
247 return false
248 }
249 d, err := ds.Data()
250 if err != nil {
251 return false
252 }
253 for len(d) > 0 {
254 var t elf.DynTag
255 switch f.Class {
256 case elf.ELFCLASS32:
257 t = elf.DynTag(f.ByteOrder.Uint32(d[0:4]))
258 d = d[8:]
259 case elf.ELFCLASS64:
260 t = elf.DynTag(f.ByteOrder.Uint64(d[0:8]))
261 d = d[16:]
262 }
263 if t == tag {
264 return true
265 }
266 }
267 return false
268 }
269
270 // The shared library does not have relocations against the text segment.
271 func TestNoTextrel(t *testing.T) {
272 sopath := filepath.Join(gorootInstallDir, soname)
273 f, err := elf.Open(sopath)
274 if err != nil {
275 t.Fatal("elf.Open failed: ", err)
276 }
277 defer f.Close()
278 if hasDynTag(f, elf.DT_TEXTREL) {
279 t.Errorf("%s has DT_TEXTREL set", soname)
280 }
281 }
282
283 // The shared library does not contain symbols called ".dup"
284 // (See golang.org/issue/14841.)
285 func TestNoDupSymbols(t *testing.T) {
286 sopath := filepath.Join(gorootInstallDir, soname)
287 f, err := elf.Open(sopath)
288 if err != nil {
289 t.Fatal("elf.Open failed: ", err)
290 }
291 defer f.Close()
292 syms, err := f.Symbols()
293 if err != nil {
294 t.Errorf("error reading symbols %v", err)
295 return
296 }
297 for _, s := range syms {
298 if s.Name == ".dup" {
299 t.Fatalf("%s contains symbol called .dup", sopath)
300 }
301 }
302 }
303
304 // The install command should have created a "shlibname" file for the
305 // listed packages (and runtime/cgo, and math on arm) indicating the
306 // name of the shared library containing it.
307 func TestShlibnameFiles(t *testing.T) {
308 pkgs := append([]string{}, minpkgs...)
309 pkgs = append(pkgs, "runtime/cgo")
310 if runtime.GOARCH == "arm" {
311 pkgs = append(pkgs, "math")
312 }
313 for _, pkg := range pkgs {
314 shlibnamefile := filepath.Join(gorootInstallDir, pkg+".shlibname")
315 contentsb, err := ioutil.ReadFile(shlibnamefile)
316 if err != nil {
317 t.Errorf("error reading shlibnamefile for %s: %v", pkg, err)
318 continue
319 }
320 contents := strings.TrimSpace(string(contentsb))
321 if contents != soname {
322 t.Errorf("shlibnamefile for %s has wrong contents: %q", pkg, contents)
323 }
324 }
325 }
326
327 // Is a given offset into the file contained in a loaded segment?
328 func isOffsetLoaded(f *elf.File, offset uint64) bool {
329 for _, prog := range f.Progs {
330 if prog.Type == elf.PT_LOAD {
331 if prog.Off <= offset && offset < prog.Off+prog.Filesz {
332 return true
333 }
334 }
335 }
336 return false
337 }
338
339 func rnd(v int32, r int32) int32 {
340 if r <= 0 {
341 return v
342 }
343 v += r - 1
344 c := v % r
345 if c < 0 {
346 c += r
347 }
348 v -= c
349 return v
350 }
351
352 func readwithpad(r io.Reader, sz int32) ([]byte, error) {
353 data := make([]byte, rnd(sz, 4))
354 _, err := io.ReadFull(r, data)
355 if err != nil {
356 return nil, err
357 }
358 data = data[:sz]
359 return data, nil
360 }
361
362 type note struct {
363 name string
364 tag int32
365 desc string
366 section *elf.Section
367 }
368
369 // Read all notes from f. As ELF section names are not supposed to be special, one
370 // looks for a particular note by scanning all SHT_NOTE sections looking for a note
371 // with a particular "name" and "tag".
372 func readNotes(f *elf.File) ([]*note, error) {
373 var notes []*note
374 for _, sect := range f.Sections {
375 if sect.Type != elf.SHT_NOTE {
376 continue
377 }
378 r := sect.Open()
379 for {
380 var namesize, descsize, tag int32
381 err := binary.Read(r, f.ByteOrder, &namesize)
382 if err != nil {
383 if err == io.EOF {
384 break
385 }
386 return nil, fmt.Errorf("read namesize failed: %v", err)
387 }
388 err = binary.Read(r, f.ByteOrder, &descsize)
389 if err != nil {
390 return nil, fmt.Errorf("read descsize failed: %v", err)
391 }
392 err = binary.Read(r, f.ByteOrder, &tag)
393 if err != nil {
394 return nil, fmt.Errorf("read type failed: %v", err)
395 }
396 name, err := readwithpad(r, namesize)
397 if err != nil {
398 return nil, fmt.Errorf("read name failed: %v", err)
399 }
400 desc, err := readwithpad(r, descsize)
401 if err != nil {
402 return nil, fmt.Errorf("read desc failed: %v", err)
403 }
404 notes = append(notes, &note{name: string(name), tag: tag, desc: string(desc), section: sect})
405 }
406 }
407 return notes, nil
408 }
409
410 func dynStrings(t *testing.T, path string, flag elf.DynTag) []string {
411 t.Helper()
412 f, err := elf.Open(path)
413 if err != nil {
414 t.Fatalf("elf.Open(%q) failed: %v", path, err)
415 }
416 defer f.Close()
417 dynstrings, err := f.DynString(flag)
418 if err != nil {
419 t.Fatalf("DynString(%s) failed on %s: %v", flag, path, err)
420 }
421 return dynstrings
422 }
423
424 func AssertIsLinkedToRegexp(t *testing.T, path string, re *regexp.Regexp) {
425 t.Helper()
426 for _, dynstring := range dynStrings(t, path, elf.DT_NEEDED) {
427 if re.MatchString(dynstring) {
428 return
429 }
430 }
431 t.Errorf("%s is not linked to anything matching %v", path, re)
432 }
433
434 func AssertIsLinkedTo(t *testing.T, path, lib string) {
435 t.Helper()
436 AssertIsLinkedToRegexp(t, path, regexp.MustCompile(regexp.QuoteMeta(lib)))
437 }
438
439 func AssertHasRPath(t *testing.T, path, dir string) {
440 t.Helper()
441 for _, tag := range []elf.DynTag{elf.DT_RPATH, elf.DT_RUNPATH} {
442 for _, dynstring := range dynStrings(t, path, tag) {
443 for _, rpath := range strings.Split(dynstring, ":") {
444 if filepath.Clean(rpath) == filepath.Clean(dir) {
445 return
446 }
447 }
448 }
449 }
450 t.Errorf("%s does not have rpath %s", path, dir)
451 }
452
453 // Build a trivial program that links against the shared runtime and check it runs.
454 func TestTrivialExecutable(t *testing.T) {
455 goCmd(t, "install", "-linkshared", "./trivial")
456 run(t, "trivial executable", "../../bin/trivial")
457 AssertIsLinkedTo(t, "../../bin/trivial", soname)
458 AssertHasRPath(t, "../../bin/trivial", gorootInstallDir)
459 }
460
461 // Build a trivial program in PIE mode that links against the shared runtime and check it runs.
462 func TestTrivialExecutablePIE(t *testing.T) {
463 goCmd(t, "build", "-buildmode=pie", "-o", "trivial.pie", "-linkshared", "./trivial")
464 run(t, "trivial executable", "./trivial.pie")
465 AssertIsLinkedTo(t, "./trivial.pie", soname)
466 AssertHasRPath(t, "./trivial.pie", gorootInstallDir)
467 }
468
469 // Build a division test program and check it runs.
470 func TestDivisionExecutable(t *testing.T) {
471 goCmd(t, "install", "-linkshared", "./division")
472 run(t, "division executable", "../../bin/division")
473 }
474
475 // Build an executable that uses cgo linked against the shared runtime and check it
476 // runs.
477 func TestCgoExecutable(t *testing.T) {
478 goCmd(t, "install", "-linkshared", "./execgo")
479 run(t, "cgo executable", "../../bin/execgo")
480 }
481
482 func checkPIE(t *testing.T, name string) {
483 f, err := elf.Open(name)
484 if err != nil {
485 t.Fatal("elf.Open failed: ", err)
486 }
487 defer f.Close()
488 if f.Type != elf.ET_DYN {
489 t.Errorf("%s has type %v, want ET_DYN", name, f.Type)
490 }
491 if hasDynTag(f, elf.DT_TEXTREL) {
492 t.Errorf("%s has DT_TEXTREL set", name)
493 }
494 }
495
496 func TestTrivialPIE(t *testing.T) {
497 name := "trivial_pie"
498 goCmd(t, "build", "-buildmode=pie", "-o="+name, "./trivial")
499 defer os.Remove(name)
500 run(t, name, "./"+name)
501 checkPIE(t, name)
502 }
503
504 func TestCgoPIE(t *testing.T) {
505 name := "cgo_pie"
506 goCmd(t, "build", "-buildmode=pie", "-o="+name, "./execgo")
507 defer os.Remove(name)
508 run(t, name, "./"+name)
509 checkPIE(t, name)
510 }
511
512 // Build a GOPATH package into a shared library that links against the goroot runtime
513 // and an executable that links against both.
514 func TestGopathShlib(t *testing.T) {
515 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./depBase")
516 shlib := goCmd(t, "list", "-f", "{{.Shlib}}", "-buildmode=shared", "-linkshared", "./depBase")
517 AssertIsLinkedTo(t, shlib, soname)
518 goCmd(t, "install", "-linkshared", "./exe")
519 AssertIsLinkedTo(t, "../../bin/exe", soname)
520 AssertIsLinkedTo(t, "../../bin/exe", filepath.Base(shlib))
521 AssertHasRPath(t, "../../bin/exe", gorootInstallDir)
522 AssertHasRPath(t, "../../bin/exe", filepath.Dir(gopathInstallDir))
523 // And check it runs.
524 run(t, "executable linked to GOPATH library", "../../bin/exe")
525 }
526
527 // The shared library contains a note listing the packages it contains in a section
528 // that is not mapped into memory.
529 func testPkgListNote(t *testing.T, f *elf.File, note *note) {
530 if note.section.Flags != 0 {
531 t.Errorf("package list section has flags %v, want 0", note.section.Flags)
532 }
533 if isOffsetLoaded(f, note.section.Offset) {
534 t.Errorf("package list section contained in PT_LOAD segment")
535 }
536 if note.desc != "testshared/depBase\n" {
537 t.Errorf("incorrect package list %q, want %q", note.desc, "testshared/depBase\n")
538 }
539 }
540
541 // The shared library contains a note containing the ABI hash that is mapped into
542 // memory and there is a local symbol called go.link.abihashbytes that points 16
543 // bytes into it.
544 func testABIHashNote(t *testing.T, f *elf.File, note *note) {
545 if note.section.Flags != elf.SHF_ALLOC {
546 t.Errorf("abi hash section has flags %v, want SHF_ALLOC", note.section.Flags)
547 }
548 if !isOffsetLoaded(f, note.section.Offset) {
549 t.Errorf("abihash section not contained in PT_LOAD segment")
550 }
551 var hashbytes elf.Symbol
552 symbols, err := f.Symbols()
553 if err != nil {
554 t.Errorf("error reading symbols %v", err)
555 return
556 }
557 for _, sym := range symbols {
558 if sym.Name == "go.link.abihashbytes" {
559 hashbytes = sym
560 }
561 }
562 if hashbytes.Name == "" {
563 t.Errorf("no symbol called go.link.abihashbytes")
564 return
565 }
566 if elf.ST_BIND(hashbytes.Info) != elf.STB_LOCAL {
567 t.Errorf("%s has incorrect binding %v, want STB_LOCAL", hashbytes.Name, elf.ST_BIND(hashbytes.Info))
568 }
569 if f.Sections[hashbytes.Section] != note.section {
570 t.Errorf("%s has incorrect section %v, want %s", hashbytes.Name, f.Sections[hashbytes.Section].Name, note.section.Name)
571 }
572 if hashbytes.Value-note.section.Addr != 16 {
573 t.Errorf("%s has incorrect offset into section %d, want 16", hashbytes.Name, hashbytes.Value-note.section.Addr)
574 }
575 }
576
577 // A Go shared library contains a note indicating which other Go shared libraries it
578 // was linked against in an unmapped section.
579 func testDepsNote(t *testing.T, f *elf.File, note *note) {
580 if note.section.Flags != 0 {
581 t.Errorf("package list section has flags %v, want 0", note.section.Flags)
582 }
583 if isOffsetLoaded(f, note.section.Offset) {
584 t.Errorf("package list section contained in PT_LOAD segment")
585 }
586 // libdepBase.so just links against the lib containing the runtime.
587 if note.desc != soname {
588 t.Errorf("incorrect dependency list %q, want %q", note.desc, soname)
589 }
590 }
591
592 // The shared library contains notes with defined contents; see above.
593 func TestNotes(t *testing.T) {
594 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./depBase")
595 shlib := goCmd(t, "list", "-f", "{{.Shlib}}", "-buildmode=shared", "-linkshared", "./depBase")
596 f, err := elf.Open(shlib)
597 if err != nil {
598 t.Fatal(err)
599 }
600 defer f.Close()
601 notes, err := readNotes(f)
602 if err != nil {
603 t.Fatal(err)
604 }
605 pkgListNoteFound := false
606 abiHashNoteFound := false
607 depsNoteFound := false
608 for _, note := range notes {
609 if note.name != "Go\x00\x00" {
610 continue
611 }
612 switch note.tag {
613 case 1: // ELF_NOTE_GOPKGLIST_TAG
614 if pkgListNoteFound {
615 t.Error("multiple package list notes")
616 }
617 testPkgListNote(t, f, note)
618 pkgListNoteFound = true
619 case 2: // ELF_NOTE_GOABIHASH_TAG
620 if abiHashNoteFound {
621 t.Error("multiple abi hash notes")
622 }
623 testABIHashNote(t, f, note)
624 abiHashNoteFound = true
625 case 3: // ELF_NOTE_GODEPS_TAG
626 if depsNoteFound {
627 t.Error("multiple dependency list notes")
628 }
629 testDepsNote(t, f, note)
630 depsNoteFound = true
631 }
632 }
633 if !pkgListNoteFound {
634 t.Error("package list note not found")
635 }
636 if !abiHashNoteFound {
637 t.Error("abi hash note not found")
638 }
639 if !depsNoteFound {
640 t.Error("deps note not found")
641 }
642 }
643
644 // Build a GOPATH package (depBase) into a shared library that links against the goroot
645 // runtime, another package (dep2) that links against the first, and an
646 // executable that links against dep2.
647 func TestTwoGopathShlibs(t *testing.T) {
648 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./depBase")
649 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./dep2")
650 goCmd(t, "install", "-linkshared", "./exe2")
651 run(t, "executable linked to GOPATH library", "../../bin/exe2")
652 }
653
654 func TestThreeGopathShlibs(t *testing.T) {
655 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./depBase")
656 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./dep2")
657 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./dep3")
658 goCmd(t, "install", "-linkshared", "./exe3")
659 run(t, "executable linked to GOPATH library", "../../bin/exe3")
660 }
661
662 // If gccgo is not available or not new enough, call t.Skip.
663 func requireGccgo(t *testing.T) {
664 t.Helper()
665
666 gccgoName := os.Getenv("GCCGO")
667 if gccgoName == "" {
668 gccgoName = "gccgo"
669 }
670 gccgoPath, err := exec.LookPath(gccgoName)
671 if err != nil {
672 t.Skip("gccgo not found")
673 }
674 cmd := exec.Command(gccgoPath, "-dumpversion")
675 output, err := cmd.CombinedOutput()
676 if err != nil {
677 t.Fatalf("%s -dumpversion failed: %v\n%s", gccgoPath, err, output)
678 }
679 if string(output) < "5" {
680 t.Skipf("gccgo too old (%s)", strings.TrimSpace(string(output)))
681 }
682
683 gomod, err := exec.Command("go", "env", "GOMOD").Output()
684 if err != nil {
685 t.Fatalf("go env GOMOD: %v", err)
686 }
687 if len(bytes.TrimSpace(gomod)) > 0 {
688 t.Skipf("gccgo not supported in module mode; see golang.org/issue/30344")
689 }
690 }
691
692 // Build a GOPATH package into a shared library with gccgo and an executable that
693 // links against it.
694 func TestGoPathShlibGccgo(t *testing.T) {
695 requireGccgo(t)
696
697 libgoRE := regexp.MustCompile("libgo.so.[0-9]+")
698
699 goCmd(t, "install", "-compiler=gccgo", "-buildmode=shared", "-linkshared", "./depBase")
700
701 // Run 'go list' after 'go install': with gccgo, we apparently don't know the
702 // shlib location until after we've installed it.
703 shlib := goCmd(t, "list", "-compiler=gccgo", "-buildmode=shared", "-linkshared", "-f", "{{.Shlib}}", "./depBase")
704
705 AssertIsLinkedToRegexp(t, shlib, libgoRE)
706 goCmd(t, "install", "-compiler=gccgo", "-linkshared", "./exe")
707 AssertIsLinkedToRegexp(t, "../../bin/exe", libgoRE)
708 AssertIsLinkedTo(t, "../../bin/exe", filepath.Base(shlib))
709 AssertHasRPath(t, "../../bin/exe", filepath.Dir(shlib))
710 // And check it runs.
711 run(t, "gccgo-built", "../../bin/exe")
712 }
713
714 // The gccgo version of TestTwoGopathShlibs: build a GOPATH package into a shared
715 // library with gccgo, another GOPATH package that depends on the first and an
716 // executable that links the second library.
717 func TestTwoGopathShlibsGccgo(t *testing.T) {
718 requireGccgo(t)
719
720 libgoRE := regexp.MustCompile("libgo.so.[0-9]+")
721
722 goCmd(t, "install", "-compiler=gccgo", "-buildmode=shared", "-linkshared", "./depBase")
723 goCmd(t, "install", "-compiler=gccgo", "-buildmode=shared", "-linkshared", "./dep2")
724 goCmd(t, "install", "-compiler=gccgo", "-linkshared", "./exe2")
725
726 // Run 'go list' after 'go install': with gccgo, we apparently don't know the
727 // shlib location until after we've installed it.
728 dep2 := goCmd(t, "list", "-compiler=gccgo", "-buildmode=shared", "-linkshared", "-f", "{{.Shlib}}", "./dep2")
729 depBase := goCmd(t, "list", "-compiler=gccgo", "-buildmode=shared", "-linkshared", "-f", "{{.Shlib}}", "./depBase")
730
731 AssertIsLinkedToRegexp(t, depBase, libgoRE)
732 AssertIsLinkedToRegexp(t, dep2, libgoRE)
733 AssertIsLinkedTo(t, dep2, filepath.Base(depBase))
734 AssertIsLinkedToRegexp(t, "../../bin/exe2", libgoRE)
735 AssertIsLinkedTo(t, "../../bin/exe2", filepath.Base(dep2))
736 AssertIsLinkedTo(t, "../../bin/exe2", filepath.Base(depBase))
737
738 // And check it runs.
739 run(t, "gccgo-built", "../../bin/exe2")
740 }
741
742 // Testing rebuilding of shared libraries when they are stale is a bit more
743 // complicated that it seems like it should be. First, we make everything "old": but
744 // only a few seconds old, or it might be older than gc (or the runtime source) and
745 // everything will get rebuilt. Then define a timestamp slightly newer than this
746 // time, which is what we set the mtime to of a file to cause it to be seen as new,
747 // and finally another slightly even newer one that we can compare files against to
748 // see if they have been rebuilt.
749 var oldTime = time.Now().Add(-9 * time.Second)
750 var nearlyNew = time.Now().Add(-6 * time.Second)
751 var stampTime = time.Now().Add(-3 * time.Second)
752
753 // resetFileStamps makes "everything" (bin, src, pkg from GOPATH and the
754 // test-specific parts of GOROOT) appear old.
755 func resetFileStamps() {
756 chtime := func(path string, info os.FileInfo, err error) error {
757 return os.Chtimes(path, oldTime, oldTime)
758 }
759 reset := func(path string) {
760 if err := filepath.Walk(path, chtime); err != nil {
761 log.Panicf("resetFileStamps failed: %v", err)
762 }
763
764 }
765 reset("../../bin")
766 reset("../../pkg")
767 reset("../../src")
768 reset(gorootInstallDir)
769 }
770
771 // touch changes path and returns a function that changes it back.
772 // It also sets the time of the file, so that we can see if it is rewritten.
773 func touch(t *testing.T, path string) (cleanup func()) {
774 t.Helper()
775 data, err := ioutil.ReadFile(path)
776 if err != nil {
777 t.Fatal(err)
778 }
779 old := make([]byte, len(data))
780 copy(old, data)
781 if bytes.HasPrefix(data, []byte("!<arch>\n")) {
782 // Change last digit of build ID.
783 // (Content ID in the new content-based build IDs.)
784 const marker = `build id "`
785 i := bytes.Index(data, []byte(marker))
786 if i < 0 {
787 t.Fatal("cannot find build id in archive")
788 }
789 j := bytes.IndexByte(data[i+len(marker):], '"')
790 if j < 0 {
791 t.Fatal("cannot find build id in archive")
792 }
793 i += len(marker) + j - 1
794 if data[i] == 'a' {
795 data[i] = 'b'
796 } else {
797 data[i] = 'a'
798 }
799 } else {
800 // assume it's a text file
801 data = append(data, '\n')
802 }
803
804 // If the file is still a symlink from an overlay, delete it so that we will
805 // replace it with a regular file instead of overwriting the symlinked one.
806 fi, err := os.Lstat(path)
807 if err == nil && !fi.Mode().IsRegular() {
808 fi, err = os.Stat(path)
809 if err := os.Remove(path); err != nil {
810 t.Fatal(err)
811 }
812 }
813 if err != nil {
814 t.Fatal(err)
815 }
816
817 // If we're replacing a symlink to a read-only file, make the new file
818 // user-writable.
819 perm := fi.Mode().Perm() | 0200
820
821 if err := ioutil.WriteFile(path, data, perm); err != nil {
822 t.Fatal(err)
823 }
824 if err := os.Chtimes(path, nearlyNew, nearlyNew); err != nil {
825 t.Fatal(err)
826 }
827 return func() {
828 if err := ioutil.WriteFile(path, old, perm); err != nil {
829 t.Fatal(err)
830 }
831 }
832 }
833
834 // isNew returns if the path is newer than the time stamp used by touch.
835 func isNew(t *testing.T, path string) bool {
836 t.Helper()
837 fi, err := os.Stat(path)
838 if err != nil {
839 t.Fatal(err)
840 }
841 return fi.ModTime().After(stampTime)
842 }
843
844 // Fail unless path has been rebuilt (i.e. is newer than the time stamp used by
845 // isNew)
846 func AssertRebuilt(t *testing.T, msg, path string) {
847 t.Helper()
848 if !isNew(t, path) {
849 t.Errorf("%s was not rebuilt (%s)", msg, path)
850 }
851 }
852
853 // Fail if path has been rebuilt (i.e. is newer than the time stamp used by isNew)
854 func AssertNotRebuilt(t *testing.T, msg, path string) {
855 t.Helper()
856 if isNew(t, path) {
857 t.Errorf("%s was rebuilt (%s)", msg, path)
858 }
859 }
860
861 func TestRebuilding(t *testing.T) {
862 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./depBase")
863 goCmd(t, "install", "-linkshared", "./exe")
864 info := strings.Fields(goCmd(t, "list", "-buildmode=shared", "-linkshared", "-f", "{{.Target}} {{.Shlib}}", "./depBase"))
865 if len(info) != 2 {
866 t.Fatalf("go list failed to report Target and/or Shlib")
867 }
868 target := info[0]
869 shlib := info[1]
870
871 // If the source is newer than both the .a file and the .so, both are rebuilt.
872 t.Run("newsource", func(t *testing.T) {
873 resetFileStamps()
874 cleanup := touch(t, "./depBase/dep.go")
875 defer func() {
876 cleanup()
877 goCmd(t, "install", "-linkshared", "./exe")
878 }()
879 goCmd(t, "install", "-linkshared", "./exe")
880 AssertRebuilt(t, "new source", target)
881 AssertRebuilt(t, "new source", shlib)
882 })
883
884 // If the .a file is newer than the .so, the .so is rebuilt (but not the .a)
885 t.Run("newarchive", func(t *testing.T) {
886 resetFileStamps()
887 AssertNotRebuilt(t, "new .a file before build", target)
888 goCmd(t, "list", "-linkshared", "-f={{.ImportPath}} {{.Stale}} {{.StaleReason}} {{.Target}}", "./depBase")
889 AssertNotRebuilt(t, "new .a file before build", target)
890 cleanup := touch(t, target)
891 defer func() {
892 cleanup()
893 goCmd(t, "install", "-v", "-linkshared", "./exe")
894 }()
895 goCmd(t, "install", "-v", "-linkshared", "./exe")
896 AssertNotRebuilt(t, "new .a file", target)
897 AssertRebuilt(t, "new .a file", shlib)
898 })
899 }
900
901 func appendFile(t *testing.T, path, content string) {
902 t.Helper()
903 f, err := os.OpenFile(path, os.O_WRONLY|os.O_APPEND, 0660)
904 if err != nil {
905 t.Fatalf("os.OpenFile failed: %v", err)
906 }
907 defer func() {
908 err := f.Close()
909 if err != nil {
910 t.Fatalf("f.Close failed: %v", err)
911 }
912 }()
913 _, err = f.WriteString(content)
914 if err != nil {
915 t.Fatalf("f.WriteString failed: %v", err)
916 }
917 }
918
919 func createFile(t *testing.T, path, content string) {
920 t.Helper()
921 f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0644)
922 if err != nil {
923 t.Fatalf("os.OpenFile failed: %v", err)
924 }
925 _, err = f.WriteString(content)
926 if closeErr := f.Close(); err == nil {
927 err = closeErr
928 }
929 if err != nil {
930 t.Fatalf("WriteString failed: %v", err)
931 }
932 }
933
934 func TestABIChecking(t *testing.T) {
935 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./depBase")
936 goCmd(t, "install", "-linkshared", "./exe")
937
938 // If we make an ABI-breaking change to depBase and rebuild libp.so but not exe,
939 // exe will abort with a complaint on startup.
940 // This assumes adding an exported function breaks ABI, which is not true in
941 // some senses but suffices for the narrow definition of ABI compatibility the
942 // toolchain uses today.
943 resetFileStamps()
944
945 createFile(t, "./depBase/break.go", "package depBase\nfunc ABIBreak() {}\n")
946 defer os.Remove("./depBase/break.go")
947
948 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./depBase")
949 c := exec.Command("../../bin/exe")
950 output, err := c.CombinedOutput()
951 if err == nil {
952 t.Fatal("executing exe did not fail after ABI break")
953 }
954 scanner := bufio.NewScanner(bytes.NewReader(output))
955 foundMsg := false
956 const wantPrefix = "abi mismatch detected between the executable and lib"
957 for scanner.Scan() {
958 if strings.HasPrefix(scanner.Text(), wantPrefix) {
959 foundMsg = true
960 break
961 }
962 }
963 if err = scanner.Err(); err != nil {
964 t.Errorf("scanner encountered error: %v", err)
965 }
966 if !foundMsg {
967 t.Fatalf("exe failed, but without line %q; got output:\n%s", wantPrefix, output)
968 }
969
970 // Rebuilding exe makes it work again.
971 goCmd(t, "install", "-linkshared", "./exe")
972 run(t, "rebuilt exe", "../../bin/exe")
973
974 // If we make a change which does not break ABI (such as adding an unexported
975 // function) and rebuild libdepBase.so, exe still works, even if new function
976 // is in a file by itself.
977 resetFileStamps()
978 createFile(t, "./depBase/dep2.go", "package depBase\nfunc noABIBreak() {}\n")
979 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./depBase")
980 run(t, "after non-ABI breaking change", "../../bin/exe")
981 }
982
983 // If a package 'explicit' imports a package 'implicit', building
984 // 'explicit' into a shared library implicitly includes implicit in
985 // the shared library. Building an executable that imports both
986 // explicit and implicit builds the code from implicit into the
987 // executable rather than fetching it from the shared library. The
988 // link still succeeds and the executable still runs though.
989 func TestImplicitInclusion(t *testing.T) {
990 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./explicit")
991 goCmd(t, "install", "-linkshared", "./implicitcmd")
992 run(t, "running executable linked against library that contains same package as it", "../../bin/implicitcmd")
993 }
994
995 // Tests to make sure that the type fields of empty interfaces and itab
996 // fields of nonempty interfaces are unique even across modules,
997 // so that interface equality works correctly.
998 func TestInterface(t *testing.T) {
999 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./iface_a")
1000 // Note: iface_i gets installed implicitly as a dependency of iface_a.
1001 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./iface_b")
1002 goCmd(t, "install", "-linkshared", "./iface")
1003 run(t, "running type/itab uniqueness tester", "../../bin/iface")
1004 }
1005
1006 // Access a global variable from a library.
1007 func TestGlobal(t *testing.T) {
1008 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./globallib")
1009 goCmd(t, "install", "-linkshared", "./global")
1010 run(t, "global executable", "../../bin/global")
1011 AssertIsLinkedTo(t, "../../bin/global", soname)
1012 AssertHasRPath(t, "../../bin/global", gorootInstallDir)
1013 }
1014
1015 // Run a test using -linkshared of an installed shared package.
1016 // Issue 26400.
1017 func TestTestInstalledShared(t *testing.T) {
1018 goCmd(nil, "test", "-linkshared", "-test.short", "sync/atomic")
1019 }
1020
1021 // Test generated pointer method with -linkshared.
1022 // Issue 25065.
1023 func TestGeneratedMethod(t *testing.T) {
1024 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./issue25065")
1025 }
1026
1027 // Test use of shared library struct with generated hash function.
1028 // Issue 30768.
1029 func TestGeneratedHash(t *testing.T) {
1030 goCmd(nil, "install", "-buildmode=shared", "-linkshared", "./issue30768/issue30768lib")
1031 goCmd(nil, "test", "-linkshared", "./issue30768")
1032 }