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