| | |
| | |
| | |
| |
|
| | package shared_test |
| |
|
| | import ( |
| | "bufio" |
| | "bytes" |
| | "cmd/cgo/internal/cgotest" |
| | "debug/elf" |
| | "encoding/binary" |
| | "flag" |
| | "fmt" |
| | "go/build" |
| | "internal/platform" |
| | "internal/testenv" |
| | "io" |
| | "log" |
| | "os" |
| | "os/exec" |
| | "path/filepath" |
| | "regexp" |
| | "runtime" |
| | "sort" |
| | "strconv" |
| | "strings" |
| | "testing" |
| | "time" |
| | ) |
| |
|
| | var globalSkip = func(t testing.TB) {} |
| |
|
| | var gopathInstallDir, gorootInstallDir string |
| | var oldGOROOT string |
| |
|
| | |
| | |
| | var minpkgs = []string{"runtime", "sync/atomic"} |
| | var soname = "libruntime,sync-atomic.so" |
| |
|
| | var testX = flag.Bool("testx", false, "if true, pass -x to 'go' subcommands invoked by the test") |
| | var testWork = flag.Bool("testwork", false, "if true, log and do not delete the temporary working directory") |
| |
|
| | |
| | func run(t *testing.T, msg string, args ...string) { |
| | runWithEnv(t, msg, nil, args...) |
| | } |
| |
|
| | |
| | func runWithEnv(t *testing.T, msg string, env []string, args ...string) { |
| | c := exec.Command(args[0], args[1:]...) |
| | if len(env) != 0 { |
| | c.Env = append(os.Environ(), env...) |
| | } |
| | if output, err := c.CombinedOutput(); err != nil { |
| | t.Errorf("executing %s (%s) failed %s:\n%s", strings.Join(args, " "), msg, err, output) |
| | } |
| | } |
| |
|
| | |
| | |
| | func goCmd(t *testing.T, args ...string) string { |
| | newargs := []string{args[0]} |
| | if *testX && args[0] != "env" { |
| | newargs = append(newargs, "-x", "-ldflags=-v") |
| | } |
| | newargs = append(newargs, args[1:]...) |
| | c := exec.Command(filepath.Join(oldGOROOT, "bin", "go"), newargs...) |
| | stderr := new(strings.Builder) |
| | c.Stderr = stderr |
| |
|
| | if testing.Verbose() && t == nil { |
| | fmt.Fprintf(os.Stderr, "+ go %s\n", strings.Join(args, " ")) |
| | c.Stderr = os.Stderr |
| | } |
| | output, err := c.Output() |
| |
|
| | if err != nil { |
| | if t != nil { |
| | t.Helper() |
| | t.Fatalf("executing %#q failed %v:\n%s\n%s", c, err, stderr, output) |
| | } else { |
| | |
| | log.Panicf("executing %#q failed %v:\n%s\n%s", c, err, stderr, output) |
| | } |
| | } |
| | if testing.Verbose() && t != nil { |
| | t.Logf("go %s", strings.Join(args, " ")) |
| | if stderr.Len() > 0 { |
| | t.Logf("%s", stderr) |
| | } |
| | } |
| | return string(bytes.TrimSpace(output)) |
| | } |
| |
|
| | |
| | func testMain(m *testing.M) (int, error) { |
| | if testing.Short() && testenv.Builder() == "" { |
| | globalSkip = func(t testing.TB) { t.Skip("short mode and $GO_BUILDER_NAME not set") } |
| | return m.Run(), nil |
| | } |
| | if !platform.BuildModeSupported(runtime.Compiler, "shared", runtime.GOOS, runtime.GOARCH) { |
| | globalSkip = func(t testing.TB) { t.Skip("shared build mode not supported") } |
| | return m.Run(), nil |
| | } |
| | if !testenv.HasCGO() { |
| | globalSkip = testenv.MustHaveCGO |
| | return m.Run(), nil |
| | } |
| |
|
| | cwd, err := os.Getwd() |
| | if err != nil { |
| | log.Fatal(err) |
| | } |
| | oldGOROOT = filepath.Join(cwd, "../../../../..") |
| |
|
| | workDir, err := os.MkdirTemp("", "shared_test") |
| | if err != nil { |
| | return 0, err |
| | } |
| | if *testWork || testing.Verbose() { |
| | fmt.Printf("+ mkdir -p %s\n", workDir) |
| | } |
| | if !*testWork { |
| | defer os.RemoveAll(workDir) |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | os.Setenv("GO111MODULE", "off") |
| |
|
| | |
| | |
| | gopath := filepath.Join(workDir, "gopath") |
| | modRoot, err := cloneTestdataModule(gopath) |
| | if err != nil { |
| | return 0, err |
| | } |
| | if testing.Verbose() { |
| | fmt.Printf("+ export GOPATH=%s\n", gopath) |
| | fmt.Printf("+ cd %s\n", modRoot) |
| | } |
| | os.Setenv("GOPATH", gopath) |
| | |
| | os.Setenv("GOBIN", filepath.Join(gopath, "bin")) |
| | os.Chdir(modRoot) |
| | os.Setenv("PWD", modRoot) |
| |
|
| | |
| | |
| | |
| | |
| | |
| | goroot := filepath.Join(workDir, "goroot") |
| | if err := cloneGOROOTDeps(goroot); err != nil { |
| | return 0, err |
| | } |
| | if testing.Verbose() { |
| | fmt.Fprintf(os.Stderr, "+ export GOROOT=%s\n", goroot) |
| | } |
| | os.Setenv("GOROOT", goroot) |
| |
|
| | myContext := build.Default |
| | myContext.GOROOT = goroot |
| | myContext.GOPATH = gopath |
| |
|
| | |
| | |
| | |
| | goCmd(nil, append([]string{"install", "-buildmode=shared"}, minpkgs...)...) |
| |
|
| | shlib := goCmd(nil, "list", "-linkshared", "-f={{.Shlib}}", "runtime") |
| | if shlib != "" { |
| | gorootInstallDir = filepath.Dir(shlib) |
| | } |
| |
|
| | myContext.InstallSuffix = "_dynlink" |
| | depP, err := myContext.Import("./depBase", ".", build.ImportComment) |
| | if err != nil { |
| | return 0, fmt.Errorf("import failed: %v", err) |
| | } |
| | if depP.PkgTargetRoot == "" { |
| | gopathInstallDir = filepath.Dir(goCmd(nil, "list", "-buildmode=shared", "-f", "{{.Target}}", "./depBase")) |
| | } else { |
| | gopathInstallDir = filepath.Join(depP.PkgTargetRoot, "testshared") |
| | } |
| | return m.Run(), nil |
| | } |
| |
|
| | func TestMain(m *testing.M) { |
| | log.SetFlags(log.Lshortfile) |
| | flag.Parse() |
| |
|
| | exitCode, err := testMain(m) |
| | if err != nil { |
| | log.Fatal(err) |
| | } |
| | os.Exit(exitCode) |
| | } |
| |
|
| | |
| | |
| | func cloneTestdataModule(gopath string) (string, error) { |
| | modRoot := filepath.Join(gopath, "src", "testshared") |
| | if err := cgotest.OverlayDir(modRoot, "testdata"); err != nil { |
| | return "", err |
| | } |
| | if err := os.WriteFile(filepath.Join(modRoot, "go.mod"), []byte("module testshared\n"), 0644); err != nil { |
| | return "", err |
| | } |
| | return modRoot, nil |
| | } |
| |
|
| | |
| | |
| | |
| | func cloneGOROOTDeps(goroot string) error { |
| | |
| | listArgs := []string{ |
| | "list", |
| | "-deps", |
| | "-f", "{{if and .Standard (not .ForTest)}}{{.ImportPath}}{{end}}", |
| | } |
| | stdDeps := goCmd(nil, append(listArgs, minpkgs...)...) |
| | testdataDeps := goCmd(nil, append(listArgs, "-test", "./...")...) |
| |
|
| | pkgs := append(strings.Split(strings.TrimSpace(stdDeps), "\n"), |
| | strings.Split(strings.TrimSpace(testdataDeps), "\n")...) |
| | sort.Strings(pkgs) |
| | var pkgRoots []string |
| | for _, pkg := range pkgs { |
| | parentFound := false |
| | for _, prev := range pkgRoots { |
| | if pkg == prev || strings.HasPrefix(pkg, prev+"/") { |
| | |
| | parentFound = true |
| | break |
| | } |
| | } |
| | if !parentFound { |
| | pkgRoots = append(pkgRoots, pkg) |
| | } |
| | } |
| |
|
| | gorootDirs := []string{ |
| | "pkg/tool", |
| | "pkg/include", |
| | } |
| | for _, pkg := range pkgRoots { |
| | gorootDirs = append(gorootDirs, filepath.Join("src", pkg)) |
| | } |
| |
|
| | for _, dir := range gorootDirs { |
| | if testing.Verbose() { |
| | fmt.Fprintf(os.Stderr, "+ cp -r %s %s\n", filepath.Join(oldGOROOT, dir), filepath.Join(goroot, dir)) |
| | } |
| | if err := cgotest.OverlayDir(filepath.Join(goroot, dir), filepath.Join(oldGOROOT, dir)); err != nil { |
| | return err |
| | } |
| | } |
| |
|
| | return nil |
| | } |
| |
|
| | |
| | func TestSOBuilt(t *testing.T) { |
| | globalSkip(t) |
| | _, err := os.Stat(filepath.Join(gorootInstallDir, soname)) |
| | if err != nil { |
| | t.Error(err) |
| | } |
| | } |
| |
|
| | func hasDynTag(f *elf.File, tag elf.DynTag) bool { |
| | ds := f.SectionByType(elf.SHT_DYNAMIC) |
| | if ds == nil { |
| | return false |
| | } |
| | d, err := ds.Data() |
| | if err != nil { |
| | return false |
| | } |
| | for len(d) > 0 { |
| | var t elf.DynTag |
| | switch f.Class { |
| | case elf.ELFCLASS32: |
| | t = elf.DynTag(f.ByteOrder.Uint32(d[0:4])) |
| | d = d[8:] |
| | case elf.ELFCLASS64: |
| | t = elf.DynTag(f.ByteOrder.Uint64(d[0:8])) |
| | d = d[16:] |
| | } |
| | if t == tag { |
| | return true |
| | } |
| | } |
| | return false |
| | } |
| |
|
| | |
| | func TestNoTextrel(t *testing.T) { |
| | globalSkip(t) |
| | sopath := filepath.Join(gorootInstallDir, soname) |
| | f, err := elf.Open(sopath) |
| | if err != nil { |
| | t.Fatal("elf.Open failed: ", err) |
| | } |
| | defer f.Close() |
| | if hasDynTag(f, elf.DT_TEXTREL) { |
| | t.Errorf("%s has DT_TEXTREL set", soname) |
| | } |
| | } |
| |
|
| | |
| | |
| | func TestNoDupSymbols(t *testing.T) { |
| | globalSkip(t) |
| | sopath := filepath.Join(gorootInstallDir, soname) |
| | f, err := elf.Open(sopath) |
| | if err != nil { |
| | t.Fatal("elf.Open failed: ", err) |
| | } |
| | defer f.Close() |
| | syms, err := f.Symbols() |
| | if err != nil { |
| | t.Errorf("error reading symbols %v", err) |
| | return |
| | } |
| | for _, s := range syms { |
| | if s.Name == ".dup" { |
| | t.Fatalf("%s contains symbol called .dup", sopath) |
| | } |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | func TestShlibnameFiles(t *testing.T) { |
| | globalSkip(t) |
| | pkgs := append([]string{}, minpkgs...) |
| | pkgs = append(pkgs, "runtime/cgo") |
| | if runtime.GOARCH == "arm" { |
| | pkgs = append(pkgs, "math") |
| | } |
| | for _, pkg := range pkgs { |
| | shlibnamefile := filepath.Join(gorootInstallDir, pkg+".shlibname") |
| | contentsb, err := os.ReadFile(shlibnamefile) |
| | if err != nil { |
| | t.Errorf("error reading shlibnamefile for %s: %v", pkg, err) |
| | continue |
| | } |
| | contents := strings.TrimSpace(string(contentsb)) |
| | if contents != soname { |
| | t.Errorf("shlibnamefile for %s has wrong contents: %q", pkg, contents) |
| | } |
| | } |
| | } |
| |
|
| | |
| | func isOffsetLoaded(f *elf.File, offset uint64) bool { |
| | for _, prog := range f.Progs { |
| | if prog.Type == elf.PT_LOAD { |
| | if prog.Off <= offset && offset < prog.Off+prog.Filesz { |
| | return true |
| | } |
| | } |
| | } |
| | return false |
| | } |
| |
|
| | func rnd(v int32, r int32) int32 { |
| | if r <= 0 { |
| | return v |
| | } |
| | v += r - 1 |
| | c := v % r |
| | if c < 0 { |
| | c += r |
| | } |
| | v -= c |
| | return v |
| | } |
| |
|
| | func readwithpad(r io.Reader, sz int32) ([]byte, error) { |
| | data := make([]byte, rnd(sz, 4)) |
| | _, err := io.ReadFull(r, data) |
| | if err != nil { |
| | return nil, err |
| | } |
| | data = data[:sz] |
| | return data, nil |
| | } |
| |
|
| | type note struct { |
| | name string |
| | tag int32 |
| | desc string |
| | section *elf.Section |
| | } |
| |
|
| | |
| | |
| | |
| | func readNotes(f *elf.File) ([]*note, error) { |
| | var notes []*note |
| | for _, sect := range f.Sections { |
| | if sect.Type != elf.SHT_NOTE { |
| | continue |
| | } |
| | r := sect.Open() |
| | for { |
| | var namesize, descsize, tag int32 |
| | err := binary.Read(r, f.ByteOrder, &namesize) |
| | if err != nil { |
| | if err == io.EOF { |
| | break |
| | } |
| | return nil, fmt.Errorf("read namesize failed: %v", err) |
| | } |
| | err = binary.Read(r, f.ByteOrder, &descsize) |
| | if err != nil { |
| | return nil, fmt.Errorf("read descsize failed: %v", err) |
| | } |
| | err = binary.Read(r, f.ByteOrder, &tag) |
| | if err != nil { |
| | return nil, fmt.Errorf("read type failed: %v", err) |
| | } |
| | name, err := readwithpad(r, namesize) |
| | if err != nil { |
| | return nil, fmt.Errorf("read name failed: %v", err) |
| | } |
| | desc, err := readwithpad(r, descsize) |
| | if err != nil { |
| | return nil, fmt.Errorf("read desc failed: %v", err) |
| | } |
| | notes = append(notes, ¬e{name: string(name), tag: tag, desc: string(desc), section: sect}) |
| | } |
| | } |
| | return notes, nil |
| | } |
| |
|
| | func dynStrings(t *testing.T, path string, flag elf.DynTag) []string { |
| | t.Helper() |
| | f, err := elf.Open(path) |
| | if err != nil { |
| | t.Fatalf("elf.Open(%q) failed: %v", path, err) |
| | } |
| | defer f.Close() |
| | dynstrings, err := f.DynString(flag) |
| | if err != nil { |
| | t.Fatalf("DynString(%s) failed on %s: %v", flag, path, err) |
| | } |
| | return dynstrings |
| | } |
| |
|
| | func AssertIsLinkedToRegexp(t *testing.T, path string, re *regexp.Regexp) { |
| | t.Helper() |
| | for _, dynstring := range dynStrings(t, path, elf.DT_NEEDED) { |
| | if re.MatchString(dynstring) { |
| | return |
| | } |
| | } |
| | t.Errorf("%s is not linked to anything matching %v", path, re) |
| | } |
| |
|
| | func AssertIsLinkedTo(t *testing.T, path, lib string) { |
| | t.Helper() |
| | AssertIsLinkedToRegexp(t, path, regexp.MustCompile(regexp.QuoteMeta(lib))) |
| | } |
| |
|
| | func AssertHasRPath(t *testing.T, path, dir string) { |
| | t.Helper() |
| | for _, tag := range []elf.DynTag{elf.DT_RPATH, elf.DT_RUNPATH} { |
| | for _, dynstring := range dynStrings(t, path, tag) { |
| | for _, rpath := range strings.Split(dynstring, ":") { |
| | if filepath.Clean(rpath) == filepath.Clean(dir) { |
| | return |
| | } |
| | } |
| | } |
| | } |
| | t.Errorf("%s does not have rpath %s", path, dir) |
| | } |
| |
|
| | |
| | func TestTrivialExecutable(t *testing.T) { |
| | globalSkip(t) |
| | goCmd(t, "install", "-linkshared", "./trivial") |
| | run(t, "trivial executable", "../../bin/trivial") |
| | AssertIsLinkedTo(t, "../../bin/trivial", soname) |
| | AssertHasRPath(t, "../../bin/trivial", gorootInstallDir) |
| | |
| | |
| | checkSize(t, "../../bin/trivial", 256000) |
| | } |
| |
|
| | |
| | func TestTrivialExecutablePIE(t *testing.T) { |
| | globalSkip(t) |
| | goCmd(t, "build", "-buildmode=pie", "-o", "trivial.pie", "-linkshared", "./trivial") |
| | run(t, "trivial executable", "./trivial.pie") |
| | AssertIsLinkedTo(t, "./trivial.pie", soname) |
| | AssertHasRPath(t, "./trivial.pie", gorootInstallDir) |
| | |
| | |
| | checkSize(t, "./trivial.pie", 256000) |
| | } |
| |
|
| | |
| | func checkSize(t *testing.T, f string, limit int64) { |
| | fi, err := os.Stat(f) |
| | if err != nil { |
| | t.Fatalf("stat failed: %v", err) |
| | } |
| | if sz := fi.Size(); sz > limit { |
| | t.Errorf("file too large: got %d, want <= %d", sz, limit) |
| | } |
| | } |
| |
|
| | |
| | func TestDivisionExecutable(t *testing.T) { |
| | globalSkip(t) |
| | goCmd(t, "install", "-linkshared", "./division") |
| | run(t, "division executable", "../../bin/division") |
| | } |
| |
|
| | |
| | |
| | func TestCgoExecutable(t *testing.T) { |
| | globalSkip(t) |
| | goCmd(t, "install", "-linkshared", "./execgo") |
| | run(t, "cgo executable", "../../bin/execgo") |
| | } |
| |
|
| | func checkPIE(t *testing.T, name string) { |
| | f, err := elf.Open(name) |
| | if err != nil { |
| | t.Fatal("elf.Open failed: ", err) |
| | } |
| | defer f.Close() |
| | if f.Type != elf.ET_DYN { |
| | t.Errorf("%s has type %v, want ET_DYN", name, f.Type) |
| | } |
| | if hasDynTag(f, elf.DT_TEXTREL) { |
| | t.Errorf("%s has DT_TEXTREL set", name) |
| | } |
| | } |
| |
|
| | func TestTrivialPIE(t *testing.T) { |
| | if strings.Contains(testenv.Builder(), "-alpine") { |
| | t.Skip("skipping on alpine until issue #54354 resolved") |
| | } |
| | globalSkip(t) |
| | testenv.MustHaveBuildMode(t, "pie") |
| | name := "trivial_pie" |
| | goCmd(t, "build", "-buildmode=pie", "-o="+name, "./trivial") |
| | defer os.Remove(name) |
| | run(t, name, "./"+name) |
| | checkPIE(t, name) |
| | } |
| |
|
| | func TestCgoPIE(t *testing.T) { |
| | globalSkip(t) |
| | testenv.MustHaveCGO(t) |
| | testenv.MustHaveBuildMode(t, "pie") |
| | name := "cgo_pie" |
| | goCmd(t, "build", "-buildmode=pie", "-o="+name, "./execgo") |
| | defer os.Remove(name) |
| | run(t, name, "./"+name) |
| | checkPIE(t, name) |
| | } |
| |
|
| | |
| | |
| | func TestGopathShlib(t *testing.T) { |
| | globalSkip(t) |
| | goCmd(t, "install", "-buildmode=shared", "-linkshared", "./depBase") |
| | shlib := goCmd(t, "list", "-f", "{{.Shlib}}", "-buildmode=shared", "-linkshared", "./depBase") |
| | AssertIsLinkedTo(t, shlib, soname) |
| | goCmd(t, "install", "-linkshared", "./exe") |
| | AssertIsLinkedTo(t, "../../bin/exe", soname) |
| | AssertIsLinkedTo(t, "../../bin/exe", filepath.Base(shlib)) |
| | AssertHasRPath(t, "../../bin/exe", gorootInstallDir) |
| | AssertHasRPath(t, "../../bin/exe", filepath.Dir(gopathInstallDir)) |
| | |
| | run(t, "executable linked to GOPATH library", "../../bin/exe") |
| | } |
| |
|
| | |
| | |
| | func testPkgListNote(t *testing.T, f *elf.File, note *note) { |
| | if note.section.Flags != 0 { |
| | t.Errorf("package list section has flags %v, want 0", note.section.Flags) |
| | } |
| | if isOffsetLoaded(f, note.section.Offset) { |
| | t.Errorf("package list section contained in PT_LOAD segment") |
| | } |
| | if note.desc != "testshared/depBase\n" { |
| | t.Errorf("incorrect package list %q, want %q", note.desc, "testshared/depBase\n") |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | func testABIHashNote(t *testing.T, f *elf.File, note *note) { |
| | if note.section.Flags != elf.SHF_ALLOC { |
| | t.Errorf("abi hash section has flags %v, want SHF_ALLOC", note.section.Flags) |
| | } |
| | if !isOffsetLoaded(f, note.section.Offset) { |
| | t.Errorf("abihash section not contained in PT_LOAD segment") |
| | } |
| | var hashbytes elf.Symbol |
| | symbols, err := f.Symbols() |
| | if err != nil { |
| | t.Errorf("error reading symbols %v", err) |
| | return |
| | } |
| | for _, sym := range symbols { |
| | if sym.Name == "go:link.abihashbytes" { |
| | hashbytes = sym |
| | } |
| | } |
| | if hashbytes.Name == "" { |
| | t.Errorf("no symbol called go:link.abihashbytes") |
| | return |
| | } |
| | if elf.ST_BIND(hashbytes.Info) != elf.STB_LOCAL { |
| | t.Errorf("%s has incorrect binding %v, want STB_LOCAL", hashbytes.Name, elf.ST_BIND(hashbytes.Info)) |
| | } |
| | if f.Sections[hashbytes.Section] != note.section { |
| | t.Errorf("%s has incorrect section %v, want %s", hashbytes.Name, f.Sections[hashbytes.Section].Name, note.section.Name) |
| | } |
| | if hashbytes.Value-note.section.Addr != 16 { |
| | t.Errorf("%s has incorrect offset into section %d, want 16", hashbytes.Name, hashbytes.Value-note.section.Addr) |
| | } |
| | } |
| |
|
| | |
| | |
| | func testDepsNote(t *testing.T, f *elf.File, note *note) { |
| | if note.section.Flags != 0 { |
| | t.Errorf("package list section has flags %v, want 0", note.section.Flags) |
| | } |
| | if isOffsetLoaded(f, note.section.Offset) { |
| | t.Errorf("package list section contained in PT_LOAD segment") |
| | } |
| | |
| | if note.desc != soname { |
| | t.Errorf("incorrect dependency list %q, want %q", note.desc, soname) |
| | } |
| | } |
| |
|
| | |
| | func TestNotes(t *testing.T) { |
| | globalSkip(t) |
| | goCmd(t, "install", "-buildmode=shared", "-linkshared", "./depBase") |
| | shlib := goCmd(t, "list", "-f", "{{.Shlib}}", "-buildmode=shared", "-linkshared", "./depBase") |
| | f, err := elf.Open(shlib) |
| | if err != nil { |
| | t.Fatal(err) |
| | } |
| | defer f.Close() |
| | notes, err := readNotes(f) |
| | if err != nil { |
| | t.Fatal(err) |
| | } |
| | pkgListNoteFound := false |
| | abiHashNoteFound := false |
| | depsNoteFound := false |
| | for _, note := range notes { |
| | if note.name != "Go\x00\x00" { |
| | continue |
| | } |
| | switch note.tag { |
| | case 1: |
| | if pkgListNoteFound { |
| | t.Error("multiple package list notes") |
| | } |
| | testPkgListNote(t, f, note) |
| | pkgListNoteFound = true |
| | case 2: |
| | if abiHashNoteFound { |
| | t.Error("multiple abi hash notes") |
| | } |
| | testABIHashNote(t, f, note) |
| | abiHashNoteFound = true |
| | case 3: |
| | if depsNoteFound { |
| | t.Error("multiple dependency list notes") |
| | } |
| | testDepsNote(t, f, note) |
| | depsNoteFound = true |
| | } |
| | } |
| | if !pkgListNoteFound { |
| | t.Error("package list note not found") |
| | } |
| | if !abiHashNoteFound { |
| | t.Error("abi hash note not found") |
| | } |
| | if !depsNoteFound { |
| | t.Error("deps note not found") |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | func TestTwoGopathShlibs(t *testing.T) { |
| | globalSkip(t) |
| | goCmd(t, "install", "-buildmode=shared", "-linkshared", "./depBase") |
| | goCmd(t, "install", "-buildmode=shared", "-linkshared", "./dep2") |
| | goCmd(t, "install", "-linkshared", "./exe2") |
| | run(t, "executable linked to GOPATH library", "../../bin/exe2") |
| | } |
| |
|
| | func TestThreeGopathShlibs(t *testing.T) { |
| | globalSkip(t) |
| | goCmd(t, "install", "-buildmode=shared", "-linkshared", "./depBase") |
| | goCmd(t, "install", "-buildmode=shared", "-linkshared", "./dep2") |
| | goCmd(t, "install", "-buildmode=shared", "-linkshared", "./dep3") |
| | goCmd(t, "install", "-linkshared", "./exe3") |
| | run(t, "executable linked to GOPATH library", "../../bin/exe3") |
| | } |
| |
|
| | |
| | func requireGccgo(t *testing.T) { |
| | t.Helper() |
| |
|
| | if runtime.GOARCH == "ppc64" || runtime.GOARCH == "ppc64le" { |
| | t.Skip("gccgo test skipped on PPC64 until issue #60798 is resolved") |
| | } |
| |
|
| | gccgoName := os.Getenv("GCCGO") |
| | if gccgoName == "" { |
| | gccgoName = "gccgo" |
| | } |
| | gccgoPath, err := exec.LookPath(gccgoName) |
| | if err != nil { |
| | t.Skip("gccgo not found") |
| | } |
| | cmd := exec.Command(gccgoPath, "-dumpversion") |
| | output, err := cmd.CombinedOutput() |
| | if err != nil { |
| | t.Fatalf("%s -dumpversion failed: %v\n%s", gccgoPath, err, output) |
| | } |
| | dot := bytes.Index(output, []byte{'.'}) |
| | if dot > 0 { |
| | output = output[:dot] |
| | } |
| | major, err := strconv.Atoi(strings.TrimSpace(string(output))) |
| | if err != nil { |
| | t.Skipf("can't parse gccgo version number %s", output) |
| | } |
| | if major < 5 { |
| | t.Skipf("gccgo too old (%s)", strings.TrimSpace(string(output))) |
| | } |
| |
|
| | gomod, err := exec.Command("go", "env", "GOMOD").Output() |
| | if err != nil { |
| | t.Fatalf("go env GOMOD: %v", err) |
| | } |
| | if len(bytes.TrimSpace(gomod)) > 0 { |
| | t.Skipf("gccgo not supported in module mode; see golang.org/issue/30344") |
| | } |
| | } |
| |
|
| | |
| | |
| | func TestGoPathShlibGccgo(t *testing.T) { |
| | globalSkip(t) |
| | requireGccgo(t) |
| |
|
| | libgoRE := regexp.MustCompile("libgo.so.[0-9]+") |
| |
|
| | goCmd(t, "install", "-compiler=gccgo", "-buildmode=shared", "-linkshared", "./depBase") |
| |
|
| | |
| | |
| | shlib := goCmd(t, "list", "-compiler=gccgo", "-buildmode=shared", "-linkshared", "-f", "{{.Shlib}}", "./depBase") |
| |
|
| | AssertIsLinkedToRegexp(t, shlib, libgoRE) |
| | goCmd(t, "install", "-compiler=gccgo", "-linkshared", "./exe") |
| | AssertIsLinkedToRegexp(t, "../../bin/exe", libgoRE) |
| | AssertIsLinkedTo(t, "../../bin/exe", filepath.Base(shlib)) |
| | AssertHasRPath(t, "../../bin/exe", filepath.Dir(shlib)) |
| | |
| | run(t, "gccgo-built", "../../bin/exe") |
| | } |
| |
|
| | |
| | |
| | |
| | func TestTwoGopathShlibsGccgo(t *testing.T) { |
| | globalSkip(t) |
| | requireGccgo(t) |
| |
|
| | libgoRE := regexp.MustCompile("libgo.so.[0-9]+") |
| |
|
| | goCmd(t, "install", "-compiler=gccgo", "-buildmode=shared", "-linkshared", "./depBase") |
| | goCmd(t, "install", "-compiler=gccgo", "-buildmode=shared", "-linkshared", "./dep2") |
| | goCmd(t, "install", "-compiler=gccgo", "-linkshared", "./exe2") |
| |
|
| | |
| | |
| | dep2 := goCmd(t, "list", "-compiler=gccgo", "-buildmode=shared", "-linkshared", "-f", "{{.Shlib}}", "./dep2") |
| | depBase := goCmd(t, "list", "-compiler=gccgo", "-buildmode=shared", "-linkshared", "-f", "{{.Shlib}}", "./depBase") |
| |
|
| | AssertIsLinkedToRegexp(t, depBase, libgoRE) |
| | AssertIsLinkedToRegexp(t, dep2, libgoRE) |
| | AssertIsLinkedTo(t, dep2, filepath.Base(depBase)) |
| | AssertIsLinkedToRegexp(t, "../../bin/exe2", libgoRE) |
| | AssertIsLinkedTo(t, "../../bin/exe2", filepath.Base(dep2)) |
| | AssertIsLinkedTo(t, "../../bin/exe2", filepath.Base(depBase)) |
| |
|
| | |
| | run(t, "gccgo-built", "../../bin/exe2") |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | var oldTime = time.Now().Add(-9 * time.Second) |
| | var nearlyNew = time.Now().Add(-6 * time.Second) |
| | var stampTime = time.Now().Add(-3 * time.Second) |
| |
|
| | |
| | |
| | func resetFileStamps() { |
| | chtime := func(path string, info os.FileInfo, err error) error { |
| | return os.Chtimes(path, oldTime, oldTime) |
| | } |
| | reset := func(path string) { |
| | if err := filepath.Walk(path, chtime); err != nil { |
| | log.Panicf("resetFileStamps failed: %v", err) |
| | } |
| |
|
| | } |
| | reset("../../bin") |
| | reset("../../pkg") |
| | reset("../../src") |
| | reset(gorootInstallDir) |
| | } |
| |
|
| | |
| | |
| | func touch(t *testing.T, path string) (cleanup func()) { |
| | t.Helper() |
| | data, err := os.ReadFile(path) |
| | if err != nil { |
| | t.Fatal(err) |
| | } |
| | old := make([]byte, len(data)) |
| | copy(old, data) |
| | if bytes.HasPrefix(data, []byte("!<arch>\n")) { |
| | |
| | |
| | const marker = `build id "` |
| | i := bytes.Index(data, []byte(marker)) |
| | if i < 0 { |
| | t.Fatal("cannot find build id in archive") |
| | } |
| | j := bytes.IndexByte(data[i+len(marker):], '"') |
| | if j < 0 { |
| | t.Fatal("cannot find build id in archive") |
| | } |
| | i += len(marker) + j - 1 |
| | if data[i] == 'a' { |
| | data[i] = 'b' |
| | } else { |
| | data[i] = 'a' |
| | } |
| | } else { |
| | |
| | data = append(data, '\n') |
| | } |
| |
|
| | |
| | |
| | fi, err := os.Lstat(path) |
| | if err == nil && !fi.Mode().IsRegular() { |
| | fi, err = os.Stat(path) |
| | if err := os.Remove(path); err != nil { |
| | t.Fatal(err) |
| | } |
| | } |
| | if err != nil { |
| | t.Fatal(err) |
| | } |
| |
|
| | |
| | |
| | perm := fi.Mode().Perm() | 0200 |
| |
|
| | if err := os.WriteFile(path, data, perm); err != nil { |
| | t.Fatal(err) |
| | } |
| | if err := os.Chtimes(path, nearlyNew, nearlyNew); err != nil { |
| | t.Fatal(err) |
| | } |
| | return func() { |
| | if err := os.WriteFile(path, old, perm); err != nil { |
| | t.Fatal(err) |
| | } |
| | } |
| | } |
| |
|
| | |
| | func isNew(t *testing.T, path string) bool { |
| | t.Helper() |
| | fi, err := os.Stat(path) |
| | if err != nil { |
| | t.Fatal(err) |
| | } |
| | return fi.ModTime().After(stampTime) |
| | } |
| |
|
| | |
| | |
| | func AssertRebuilt(t *testing.T, msg, path string) { |
| | t.Helper() |
| | if !isNew(t, path) { |
| | t.Errorf("%s was not rebuilt (%s)", msg, path) |
| | } |
| | } |
| |
|
| | |
| | func AssertNotRebuilt(t *testing.T, msg, path string) { |
| | t.Helper() |
| | if isNew(t, path) { |
| | t.Errorf("%s was rebuilt (%s)", msg, path) |
| | } |
| | } |
| |
|
| | func TestRebuilding(t *testing.T) { |
| | globalSkip(t) |
| | goCmd(t, "install", "-buildmode=shared", "-linkshared", "./depBase") |
| | goCmd(t, "install", "-linkshared", "./exe") |
| | info := strings.Fields(goCmd(t, "list", "-buildmode=shared", "-linkshared", "-f", "{{.Target}} {{.Shlib}}", "./depBase")) |
| | if len(info) != 2 { |
| | t.Fatalf("go list failed to report Target and/or Shlib") |
| | } |
| | target := info[0] |
| | shlib := info[1] |
| |
|
| | |
| | t.Run("newsource", func(t *testing.T) { |
| | resetFileStamps() |
| | cleanup := touch(t, "./depBase/dep.go") |
| | defer func() { |
| | cleanup() |
| | goCmd(t, "install", "-linkshared", "./exe") |
| | }() |
| | goCmd(t, "install", "-linkshared", "./exe") |
| | AssertRebuilt(t, "new source", target) |
| | AssertRebuilt(t, "new source", shlib) |
| | }) |
| |
|
| | |
| | t.Run("newarchive", func(t *testing.T) { |
| | resetFileStamps() |
| | AssertNotRebuilt(t, "new .a file before build", target) |
| | goCmd(t, "list", "-linkshared", "-f={{.ImportPath}} {{.Stale}} {{.StaleReason}} {{.Target}}", "./depBase") |
| | AssertNotRebuilt(t, "new .a file before build", target) |
| | cleanup := touch(t, target) |
| | defer func() { |
| | cleanup() |
| | goCmd(t, "install", "-v", "-linkshared", "./exe") |
| | }() |
| | goCmd(t, "install", "-v", "-linkshared", "./exe") |
| | AssertNotRebuilt(t, "new .a file", target) |
| | AssertRebuilt(t, "new .a file", shlib) |
| | }) |
| | } |
| |
|
| | func appendFile(t *testing.T, path, content string) { |
| | t.Helper() |
| | f, err := os.OpenFile(path, os.O_WRONLY|os.O_APPEND, 0660) |
| | if err != nil { |
| | t.Fatalf("os.OpenFile failed: %v", err) |
| | } |
| | defer func() { |
| | err := f.Close() |
| | if err != nil { |
| | t.Fatalf("f.Close failed: %v", err) |
| | } |
| | }() |
| | _, err = f.WriteString(content) |
| | if err != nil { |
| | t.Fatalf("f.WriteString failed: %v", err) |
| | } |
| | } |
| |
|
| | func createFile(t *testing.T, path, content string) { |
| | t.Helper() |
| | f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0644) |
| | if err != nil { |
| | t.Fatalf("os.OpenFile failed: %v", err) |
| | } |
| | _, err = f.WriteString(content) |
| | if closeErr := f.Close(); err == nil { |
| | err = closeErr |
| | } |
| | if err != nil { |
| | t.Fatalf("WriteString failed: %v", err) |
| | } |
| | } |
| |
|
| | func TestABIChecking(t *testing.T) { |
| | globalSkip(t) |
| | goCmd(t, "install", "-buildmode=shared", "-linkshared", "./depBase") |
| | goCmd(t, "install", "-linkshared", "./exe") |
| |
|
| | |
| | |
| | |
| | |
| | |
| | resetFileStamps() |
| |
|
| | createFile(t, "./depBase/break.go", "package depBase\nfunc ABIBreak() {}\n") |
| | defer os.Remove("./depBase/break.go") |
| |
|
| | goCmd(t, "install", "-buildmode=shared", "-linkshared", "./depBase") |
| | c := exec.Command("../../bin/exe") |
| | output, err := c.CombinedOutput() |
| | if err == nil { |
| | t.Fatal("executing exe did not fail after ABI break") |
| | } |
| | scanner := bufio.NewScanner(bytes.NewReader(output)) |
| | foundMsg := false |
| | const wantPrefix = "abi mismatch detected between the executable and lib" |
| | for scanner.Scan() { |
| | if strings.HasPrefix(scanner.Text(), wantPrefix) { |
| | foundMsg = true |
| | break |
| | } |
| | } |
| | if err = scanner.Err(); err != nil { |
| | t.Errorf("scanner encountered error: %v", err) |
| | } |
| | if !foundMsg { |
| | t.Fatalf("exe failed, but without line %q; got output:\n%s", wantPrefix, output) |
| | } |
| |
|
| | |
| | goCmd(t, "install", "-linkshared", "./exe") |
| | run(t, "rebuilt exe", "../../bin/exe") |
| |
|
| | |
| | |
| | |
| | resetFileStamps() |
| | createFile(t, "./depBase/dep2.go", "package depBase\nfunc noABIBreak() {}\n") |
| | goCmd(t, "install", "-buildmode=shared", "-linkshared", "./depBase") |
| | run(t, "after non-ABI breaking change", "../../bin/exe") |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | func TestImplicitInclusion(t *testing.T) { |
| | globalSkip(t) |
| | goCmd(t, "install", "-buildmode=shared", "-linkshared", "./explicit") |
| | goCmd(t, "install", "-linkshared", "./implicitcmd") |
| | run(t, "running executable linked against library that contains same package as it", "../../bin/implicitcmd") |
| | } |
| |
|
| | |
| | |
| | |
| | func TestInterface(t *testing.T) { |
| | globalSkip(t) |
| | goCmd(t, "install", "-buildmode=shared", "-linkshared", "./iface_a") |
| | |
| | goCmd(t, "install", "-buildmode=shared", "-linkshared", "./iface_b") |
| | goCmd(t, "install", "-linkshared", "./iface") |
| | run(t, "running type/itab uniqueness tester", "../../bin/iface") |
| | } |
| |
|
| | |
| | func TestGlobal(t *testing.T) { |
| | globalSkip(t) |
| | goCmd(t, "install", "-buildmode=shared", "-linkshared", "./globallib") |
| | goCmd(t, "install", "-linkshared", "./global") |
| | run(t, "global executable", "../../bin/global") |
| | AssertIsLinkedTo(t, "../../bin/global", soname) |
| | AssertHasRPath(t, "../../bin/global", gorootInstallDir) |
| | } |
| |
|
| | |
| | |
| | func TestTestInstalledShared(t *testing.T) { |
| | globalSkip(t) |
| | goCmd(t, "test", "-linkshared", "-test.short", "sync/atomic") |
| | } |
| |
|
| | |
| | |
| | func TestGeneratedMethod(t *testing.T) { |
| | globalSkip(t) |
| | goCmd(t, "install", "-buildmode=shared", "-linkshared", "./issue25065") |
| | } |
| |
|
| | |
| | |
| | func TestGeneratedHash(t *testing.T) { |
| | globalSkip(t) |
| | goCmd(t, "install", "-buildmode=shared", "-linkshared", "./issue30768/issue30768lib") |
| | goCmd(t, "test", "-linkshared", "./issue30768") |
| | } |
| |
|
| | |
| | |
| | func TestPackageOrder(t *testing.T) { |
| | globalSkip(t) |
| | goCmd(t, "install", "-buildmode=shared", "-linkshared", "./issue39777/a", "./issue39777/b") |
| | } |
| |
|
| | |
| | |
| | func TestGCData(t *testing.T) { |
| | globalSkip(t) |
| | goCmd(t, "install", "-buildmode=shared", "-linkshared", "./gcdata/p") |
| | goCmd(t, "build", "-linkshared", "./gcdata/main") |
| | runWithEnv(t, "running gcdata/main", []string{"GODEBUG=clobberfree=1"}, "./main") |
| | } |
| |
|
| | |
| | |
| | func TestIssue44031(t *testing.T) { |
| | globalSkip(t) |
| | goCmd(t, "install", "-buildmode=shared", "-linkshared", "./issue44031/a") |
| | goCmd(t, "install", "-buildmode=shared", "-linkshared", "./issue44031/b") |
| | goCmd(t, "run", "-linkshared", "./issue44031/main") |
| | } |
| |
|
| | |
| | |
| | |
| | func TestIssue47873(t *testing.T) { |
| | globalSkip(t) |
| | goCmd(t, "install", "-buildmode=shared", "-linkshared", "./issue47837/a") |
| | goCmd(t, "run", "-linkshared", "./issue47837/main") |
| | } |
| |
|
| | func TestIssue62277(t *testing.T) { |
| | globalSkip(t) |
| | goCmd(t, "install", "-buildmode=shared", "-linkshared", "./issue62277/p") |
| | goCmd(t, "test", "-linkshared", "./issue62277") |
| | } |
| |
|
| | |
| | func TestStd(t *testing.T) { |
| | if testing.Short() { |
| | t.Skip("skip in short mode") |
| | } |
| | globalSkip(t) |
| | t.Parallel() |
| | tmpDir := t.TempDir() |
| | |
| | |
| | runWithEnv(t, "building std", []string{"GOROOT=" + oldGOROOT}, |
| | filepath.Join(oldGOROOT, "bin", "go"), "install", "-buildmode=shared", "-pkgdir="+tmpDir, "std") |
| |
|
| | |
| | runWithEnv(t, "testing issue #58966", []string{"GOROOT=" + oldGOROOT}, |
| | filepath.Join(oldGOROOT, "bin", "go"), "run", "-linkshared", "-pkgdir="+tmpDir, "./issue58966/main.go") |
| | } |
| |
|