2016-04-10 15:32:26 -06:00
|
|
|
// Copyright 2015 The Go Authors. All rights reserved.
|
2015-05-07 03:29:47 -06:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package shared_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"debug/elf"
|
2015-05-24 19:59:08 -06:00
|
|
|
"encoding/binary"
|
2017-08-15 05:53:49 -06:00
|
|
|
"flag"
|
2015-05-07 03:29:47 -06:00
|
|
|
"fmt"
|
|
|
|
"go/build"
|
2015-05-24 19:59:08 -06:00
|
|
|
"io"
|
2015-05-07 03:29:47 -06:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2015-05-10 19:39:28 -06:00
|
|
|
"regexp"
|
2015-09-02 04:26:42 -06:00
|
|
|
"runtime"
|
2019-11-22 08:34:55 -07:00
|
|
|
"sort"
|
2015-05-07 03:29:47 -06:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2019-11-22 08:34:55 -07:00
|
|
|
var gopathInstallDir, gorootInstallDir string
|
2015-05-07 03:29:47 -06:00
|
|
|
|
|
|
|
// This is the smallest set of packages we can link into a shared
|
|
|
|
// library (runtime/cgo is built implicitly).
|
|
|
|
var minpkgs = []string{"runtime", "sync/atomic"}
|
|
|
|
var soname = "libruntime,sync-atomic.so"
|
|
|
|
|
2019-11-21 13:03:24 -07:00
|
|
|
var testX = flag.Bool("testx", false, "if true, pass -x to 'go' subcommands invoked by the test")
|
2019-11-22 08:34:55 -07:00
|
|
|
var testWork = flag.Bool("testwork", false, "if true, log and do not delete the temporary working directory")
|
2019-11-21 13:03:24 -07:00
|
|
|
|
2015-05-07 03:29:47 -06:00
|
|
|
// run runs a command and calls t.Errorf if it fails.
|
|
|
|
func run(t *testing.T, msg string, args ...string) {
|
|
|
|
c := exec.Command(args[0], args[1:]...)
|
|
|
|
if output, err := c.CombinedOutput(); err != nil {
|
|
|
|
t.Errorf("executing %s (%s) failed %s:\n%s", strings.Join(args, " "), msg, err, output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// goCmd invokes the go tool with the installsuffix set up by TestMain. It calls
|
2016-07-14 15:07:18 -06:00
|
|
|
// t.Fatalf if the command fails.
|
2019-02-21 12:17:19 -07:00
|
|
|
func goCmd(t *testing.T, args ...string) string {
|
2019-11-22 08:34:55 -07:00
|
|
|
newargs := []string{args[0]}
|
2019-11-21 13:03:24 -07:00
|
|
|
if *testX {
|
2017-06-09 09:15:53 -06:00
|
|
|
newargs = append(newargs, "-x")
|
2015-05-07 03:29:47 -06:00
|
|
|
}
|
|
|
|
newargs = append(newargs, args[1:]...)
|
|
|
|
c := exec.Command("go", newargs...)
|
2019-02-21 12:17:19 -07:00
|
|
|
stderr := new(strings.Builder)
|
2019-11-21 13:03:24 -07:00
|
|
|
c.Stderr = stderr
|
|
|
|
|
|
|
|
if testing.Verbose() && t == nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "+ go %s\n", strings.Join(args, " "))
|
2015-05-26 16:30:55 -06:00
|
|
|
c.Stderr = os.Stderr
|
2015-05-07 03:29:47 -06:00
|
|
|
}
|
2019-11-21 13:03:24 -07:00
|
|
|
output, err := c.Output()
|
2019-02-21 12:17:19 -07:00
|
|
|
|
2015-05-26 16:30:55 -06:00
|
|
|
if err != nil {
|
2015-05-07 03:29:47 -06:00
|
|
|
if t != nil {
|
2019-02-21 12:17:19 -07:00
|
|
|
t.Helper()
|
|
|
|
t.Fatalf("executing %s failed %v:\n%s", strings.Join(c.Args, " "), err, stderr)
|
2015-05-07 03:29:47 -06:00
|
|
|
} else {
|
2019-11-22 08:34:55 -07:00
|
|
|
// Panic instead of using log.Fatalf so that deferred cleanup may run in testMain.
|
|
|
|
log.Panicf("executing %s failed %v:\n%s", strings.Join(c.Args, " "), err, stderr)
|
2015-05-07 03:29:47 -06:00
|
|
|
}
|
|
|
|
}
|
2019-11-21 13:03:24 -07:00
|
|
|
if testing.Verbose() && t != nil {
|
|
|
|
t.Logf("go %s", strings.Join(args, " "))
|
|
|
|
if stderr.Len() > 0 {
|
|
|
|
t.Logf("%s", stderr)
|
|
|
|
}
|
|
|
|
}
|
2019-02-21 12:17:19 -07:00
|
|
|
return string(bytes.TrimSpace(output))
|
2015-05-07 03:29:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestMain calls testMain so that the latter can use defer (TestMain exits with os.Exit).
|
|
|
|
func testMain(m *testing.M) (int, error) {
|
2019-11-22 08:34:55 -07:00
|
|
|
workDir, err := ioutil.TempDir("", "shared_test")
|
2015-05-07 03:29:47 -06:00
|
|
|
if err != nil {
|
2019-11-22 08:34:55 -07:00
|
|
|
return 0, err
|
2015-05-07 03:29:47 -06:00
|
|
|
}
|
2019-11-22 08:34:55 -07:00
|
|
|
if *testWork || testing.Verbose() {
|
|
|
|
fmt.Printf("+ mkdir -p %s\n", workDir)
|
2015-05-07 03:29:47 -06:00
|
|
|
}
|
2019-11-22 08:34:55 -07:00
|
|
|
if !*testWork {
|
|
|
|
defer os.RemoveAll(workDir)
|
2016-08-15 14:51:00 -06:00
|
|
|
}
|
2015-05-07 03:29:47 -06:00
|
|
|
|
|
|
|
// Some tests need to edit the source in GOPATH, so copy this directory to a
|
|
|
|
// temporary directory and chdir to that.
|
2019-11-22 08:34:55 -07:00
|
|
|
gopath := filepath.Join(workDir, "gopath")
|
|
|
|
modRoot, err := cloneTestdataModule(gopath)
|
2015-05-07 03:29:47 -06:00
|
|
|
if err != nil {
|
2019-02-21 12:17:19 -07:00
|
|
|
return 0, err
|
2015-05-07 03:29:47 -06:00
|
|
|
}
|
2016-08-15 14:51:00 -06:00
|
|
|
if testing.Verbose() {
|
2019-11-22 08:34:55 -07:00
|
|
|
fmt.Printf("+ export GOPATH=%s\n", gopath)
|
2019-02-21 12:17:19 -07:00
|
|
|
fmt.Printf("+ cd %s\n", modRoot)
|
|
|
|
}
|
2019-11-22 08:34:55 -07:00
|
|
|
os.Setenv("GOPATH", gopath)
|
2020-02-24 11:58:29 -07:00
|
|
|
// Explicitly override GOBIN as well, in case it was set through a GOENV file.
|
|
|
|
os.Setenv("GOBIN", filepath.Join(gopath, "bin"))
|
2019-02-21 12:17:19 -07:00
|
|
|
os.Chdir(modRoot)
|
2019-02-25 20:09:46 -07:00
|
|
|
os.Setenv("PWD", modRoot)
|
2019-11-22 08:34:55 -07:00
|
|
|
|
|
|
|
// The test also needs to install libraries into GOROOT/pkg, so copy the
|
|
|
|
// subset of GOROOT that we need.
|
|
|
|
//
|
|
|
|
// TODO(golang.org/issue/28553): Rework -buildmode=shared so that it does not
|
|
|
|
// need to write to GOROOT.
|
|
|
|
goroot := filepath.Join(workDir, "goroot")
|
|
|
|
if err := cloneGOROOTDeps(goroot); err != nil {
|
2019-02-21 12:17:19 -07:00
|
|
|
return 0, err
|
2016-08-15 14:51:00 -06:00
|
|
|
}
|
|
|
|
if testing.Verbose() {
|
2019-11-22 08:34:55 -07:00
|
|
|
fmt.Fprintf(os.Stderr, "+ export GOROOT=%s\n", goroot)
|
2016-08-15 14:51:00 -06:00
|
|
|
}
|
2019-11-22 08:34:55 -07:00
|
|
|
os.Setenv("GOROOT", goroot)
|
|
|
|
|
|
|
|
myContext := build.Default
|
|
|
|
myContext.GOROOT = goroot
|
2019-02-21 12:17:19 -07:00
|
|
|
myContext.GOPATH = gopath
|
2019-11-22 08:34:55 -07:00
|
|
|
runtimeP, err := myContext.Import("runtime", ".", build.ImportComment)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("import failed: %v", err)
|
|
|
|
}
|
|
|
|
gorootInstallDir = runtimeP.PkgTargetRoot + "_dynlink"
|
2015-05-07 03:29:47 -06:00
|
|
|
|
|
|
|
// All tests depend on runtime being built into a shared library. Because
|
|
|
|
// that takes a few seconds, do it here and have all tests use the version
|
|
|
|
// built here.
|
|
|
|
goCmd(nil, append([]string{"install", "-buildmode=shared"}, minpkgs...)...)
|
|
|
|
|
2019-11-22 08:34:55 -07:00
|
|
|
myContext.InstallSuffix = "_dynlink"
|
2019-02-21 12:17:19 -07:00
|
|
|
depP, err := myContext.Import("./depBase", ".", build.ImportComment)
|
2015-05-07 03:29:47 -06:00
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("import failed: %v", err)
|
|
|
|
}
|
2019-02-21 12:17:19 -07:00
|
|
|
if depP.PkgTargetRoot == "" {
|
|
|
|
gopathInstallDir = filepath.Dir(goCmd(nil, "list", "-buildmode=shared", "-f", "{{.Target}}", "./depBase"))
|
|
|
|
} else {
|
|
|
|
gopathInstallDir = filepath.Join(depP.PkgTargetRoot, "testshared")
|
|
|
|
}
|
2015-05-07 03:29:47 -06:00
|
|
|
return m.Run(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
2019-02-21 12:17:19 -07:00
|
|
|
log.SetFlags(log.Lshortfile)
|
2017-08-15 05:53:49 -06:00
|
|
|
flag.Parse()
|
|
|
|
|
2015-05-07 03:29:47 -06:00
|
|
|
exitCode, err := testMain(m)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
os.Exit(exitCode)
|
|
|
|
}
|
|
|
|
|
2019-11-22 08:34:55 -07:00
|
|
|
// cloneTestdataModule clones the packages from src/testshared into gopath.
|
|
|
|
// It returns the directory within gopath at which the module root is located.
|
|
|
|
func cloneTestdataModule(gopath string) (string, error) {
|
|
|
|
modRoot := filepath.Join(gopath, "src", "testshared")
|
|
|
|
if err := overlayDir(modRoot, "testdata"); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if err := ioutil.WriteFile(filepath.Join(modRoot, "go.mod"), []byte("module testshared\n"), 0644); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return modRoot, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// cloneGOROOTDeps copies (or symlinks) the portions of GOROOT/src and
|
|
|
|
// GOROOT/pkg relevant to this test into the given directory.
|
|
|
|
// It must be run from within the testdata module.
|
|
|
|
func cloneGOROOTDeps(goroot string) error {
|
|
|
|
oldGOROOT := strings.TrimSpace(goCmd(nil, "env", "GOROOT"))
|
|
|
|
if oldGOROOT == "" {
|
|
|
|
return fmt.Errorf("go env GOROOT returned an empty string")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Before we clone GOROOT, figure out which packages we need to copy over.
|
|
|
|
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 strings.HasPrefix(pkg, prev) {
|
|
|
|
// We will copy in the source for pkg when we copy in 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() {
|
2020-03-20 07:03:39 -06:00
|
|
|
fmt.Fprintf(os.Stderr, "+ cp -r %s %s\n", filepath.Join(oldGOROOT, dir), filepath.Join(goroot, dir))
|
2019-11-22 08:34:55 -07:00
|
|
|
}
|
|
|
|
if err := overlayDir(filepath.Join(goroot, dir), filepath.Join(oldGOROOT, dir)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-07 03:29:47 -06:00
|
|
|
// The shared library was built at the expected location.
|
|
|
|
func TestSOBuilt(t *testing.T) {
|
|
|
|
_, err := os.Stat(filepath.Join(gorootInstallDir, soname))
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-20 19:07:19 -06:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// The shared library does not have relocations against the text segment.
|
|
|
|
func TestNoTextrel(t *testing.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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-16 17:38:51 -06:00
|
|
|
// The shared library does not contain symbols called ".dup"
|
2019-11-22 08:34:55 -07:00
|
|
|
// (See golang.org/issue/14841.)
|
2016-03-16 17:38:51 -06:00
|
|
|
func TestNoDupSymbols(t *testing.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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-07 03:29:47 -06:00
|
|
|
// The install command should have created a "shlibname" file for the
|
2015-09-02 04:26:42 -06:00
|
|
|
// listed packages (and runtime/cgo, and math on arm) indicating the
|
|
|
|
// name of the shared library containing it.
|
2015-05-07 03:29:47 -06:00
|
|
|
func TestShlibnameFiles(t *testing.T) {
|
|
|
|
pkgs := append([]string{}, minpkgs...)
|
|
|
|
pkgs = append(pkgs, "runtime/cgo")
|
2015-09-02 04:26:42 -06:00
|
|
|
if runtime.GOARCH == "arm" {
|
|
|
|
pkgs = append(pkgs, "math")
|
|
|
|
}
|
2015-05-07 03:29:47 -06:00
|
|
|
for _, pkg := range pkgs {
|
|
|
|
shlibnamefile := filepath.Join(gorootInstallDir, pkg+".shlibname")
|
|
|
|
contentsb, err := ioutil.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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-24 19:59:08 -06:00
|
|
|
// Is a given offset into the file contained in a loaded segment?
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read all notes from f. As ELF section names are not supposed to be special, one
|
|
|
|
// looks for a particular note by scanning all SHT_NOTE sections looking for a note
|
|
|
|
// with a particular "name" and "tag".
|
|
|
|
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
|
|
|
|
}
|
2015-11-21 02:54:34 -07:00
|
|
|
return nil, fmt.Errorf("read namesize failed: %v", err)
|
2015-05-24 19:59:08 -06:00
|
|
|
}
|
|
|
|
err = binary.Read(r, f.ByteOrder, &descsize)
|
|
|
|
if err != nil {
|
2015-11-21 02:54:34 -07:00
|
|
|
return nil, fmt.Errorf("read descsize failed: %v", err)
|
2015-05-24 19:59:08 -06:00
|
|
|
}
|
|
|
|
err = binary.Read(r, f.ByteOrder, &tag)
|
|
|
|
if err != nil {
|
2015-11-21 02:54:34 -07:00
|
|
|
return nil, fmt.Errorf("read type failed: %v", err)
|
2015-05-24 19:59:08 -06:00
|
|
|
}
|
|
|
|
name, err := readwithpad(r, namesize)
|
|
|
|
if err != nil {
|
2015-11-21 02:54:34 -07:00
|
|
|
return nil, fmt.Errorf("read name failed: %v", err)
|
2015-05-24 19:59:08 -06:00
|
|
|
}
|
|
|
|
desc, err := readwithpad(r, descsize)
|
|
|
|
if err != nil {
|
2015-11-21 02:54:34 -07:00
|
|
|
return nil, fmt.Errorf("read desc failed: %v", err)
|
2015-05-24 19:59:08 -06:00
|
|
|
}
|
|
|
|
notes = append(notes, ¬e{name: string(name), tag: tag, desc: string(desc), section: sect})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return notes, nil
|
|
|
|
}
|
|
|
|
|
2016-04-18 05:24:48 -06:00
|
|
|
func dynStrings(t *testing.T, path string, flag elf.DynTag) []string {
|
2019-02-21 12:17:19 -07:00
|
|
|
t.Helper()
|
2015-05-07 03:29:47 -06:00
|
|
|
f, err := elf.Open(path)
|
|
|
|
if err != nil {
|
2016-04-18 05:24:48 -06:00
|
|
|
t.Fatalf("elf.Open(%q) failed: %v", path, err)
|
2015-05-07 03:29:47 -06:00
|
|
|
}
|
2018-01-11 19:22:23 -07:00
|
|
|
defer f.Close()
|
2015-05-07 03:29:47 -06:00
|
|
|
dynstrings, err := f.DynString(flag)
|
|
|
|
if err != nil {
|
2016-04-18 05:24:48 -06:00
|
|
|
t.Fatalf("DynString(%s) failed on %s: %v", flag, path, err)
|
2015-05-07 03:29:47 -06:00
|
|
|
}
|
|
|
|
return dynstrings
|
|
|
|
}
|
|
|
|
|
2015-05-10 19:39:28 -06:00
|
|
|
func AssertIsLinkedToRegexp(t *testing.T, path string, re *regexp.Regexp) {
|
2019-02-21 12:17:19 -07:00
|
|
|
t.Helper()
|
2016-04-18 05:24:48 -06:00
|
|
|
for _, dynstring := range dynStrings(t, path, elf.DT_NEEDED) {
|
2015-05-10 19:39:28 -06:00
|
|
|
if re.MatchString(dynstring) {
|
2015-05-07 03:29:47 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2015-05-10 19:39:28 -06:00
|
|
|
t.Errorf("%s is not linked to anything matching %v", path, re)
|
|
|
|
}
|
|
|
|
|
|
|
|
func AssertIsLinkedTo(t *testing.T, path, lib string) {
|
2019-02-21 12:17:19 -07:00
|
|
|
t.Helper()
|
2015-05-10 19:39:28 -06:00
|
|
|
AssertIsLinkedToRegexp(t, path, regexp.MustCompile(regexp.QuoteMeta(lib)))
|
2015-05-07 03:29:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func AssertHasRPath(t *testing.T, path, dir string) {
|
2019-02-21 12:17:19 -07:00
|
|
|
t.Helper()
|
2015-05-19 00:48:15 -06:00
|
|
|
for _, tag := range []elf.DynTag{elf.DT_RPATH, elf.DT_RUNPATH} {
|
2016-04-18 05:24:48 -06:00
|
|
|
for _, dynstring := range dynStrings(t, path, tag) {
|
2015-05-19 00:48:15 -06:00
|
|
|
for _, rpath := range strings.Split(dynstring, ":") {
|
|
|
|
if filepath.Clean(rpath) == filepath.Clean(dir) {
|
|
|
|
return
|
|
|
|
}
|
2015-05-07 03:29:47 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t.Errorf("%s does not have rpath %s", path, dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build a trivial program that links against the shared runtime and check it runs.
|
|
|
|
func TestTrivialExecutable(t *testing.T) {
|
2019-02-21 12:17:19 -07:00
|
|
|
goCmd(t, "install", "-linkshared", "./trivial")
|
|
|
|
run(t, "trivial executable", "../../bin/trivial")
|
|
|
|
AssertIsLinkedTo(t, "../../bin/trivial", soname)
|
|
|
|
AssertHasRPath(t, "../../bin/trivial", gorootInstallDir)
|
2015-05-07 03:29:47 -06:00
|
|
|
}
|
|
|
|
|
2016-09-12 19:29:13 -06:00
|
|
|
// Build a trivial program in PIE mode that links against the shared runtime and check it runs.
|
|
|
|
func TestTrivialExecutablePIE(t *testing.T) {
|
2019-02-21 12:17:19 -07:00
|
|
|
goCmd(t, "build", "-buildmode=pie", "-o", "trivial.pie", "-linkshared", "./trivial")
|
2016-09-12 19:29:13 -06:00
|
|
|
run(t, "trivial executable", "./trivial.pie")
|
|
|
|
AssertIsLinkedTo(t, "./trivial.pie", soname)
|
|
|
|
AssertHasRPath(t, "./trivial.pie", gorootInstallDir)
|
|
|
|
}
|
|
|
|
|
2017-02-27 00:56:57 -07:00
|
|
|
// Build a division test program and check it runs.
|
|
|
|
func TestDivisionExecutable(t *testing.T) {
|
2019-02-21 12:17:19 -07:00
|
|
|
goCmd(t, "install", "-linkshared", "./division")
|
|
|
|
run(t, "division executable", "../../bin/division")
|
2017-02-27 00:56:57 -07:00
|
|
|
}
|
|
|
|
|
2015-06-15 19:41:11 -06:00
|
|
|
// Build an executable that uses cgo linked against the shared runtime and check it
|
|
|
|
// runs.
|
|
|
|
func TestCgoExecutable(t *testing.T) {
|
2019-02-21 12:17:19 -07:00
|
|
|
goCmd(t, "install", "-linkshared", "./execgo")
|
|
|
|
run(t, "cgo executable", "../../bin/execgo")
|
2015-06-15 19:41:11 -06:00
|
|
|
}
|
|
|
|
|
2015-10-19 14:31:20 -06:00
|
|
|
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) {
|
|
|
|
name := "trivial_pie"
|
2019-02-21 12:17:19 -07:00
|
|
|
goCmd(t, "build", "-buildmode=pie", "-o="+name, "./trivial")
|
2015-10-19 14:31:20 -06:00
|
|
|
defer os.Remove(name)
|
|
|
|
run(t, name, "./"+name)
|
|
|
|
checkPIE(t, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCgoPIE(t *testing.T) {
|
|
|
|
name := "cgo_pie"
|
2019-02-21 12:17:19 -07:00
|
|
|
goCmd(t, "build", "-buildmode=pie", "-o="+name, "./execgo")
|
2015-10-19 14:31:20 -06:00
|
|
|
defer os.Remove(name)
|
|
|
|
run(t, name, "./"+name)
|
|
|
|
checkPIE(t, name)
|
|
|
|
}
|
|
|
|
|
2015-05-07 03:29:47 -06:00
|
|
|
// Build a GOPATH package into a shared library that links against the goroot runtime
|
|
|
|
// and an executable that links against both.
|
2015-05-10 19:39:28 -06:00
|
|
|
func TestGopathShlib(t *testing.T) {
|
2019-02-21 12:17:19 -07:00
|
|
|
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))
|
2015-05-07 03:29:47 -06:00
|
|
|
// And check it runs.
|
2019-02-21 12:17:19 -07:00
|
|
|
run(t, "executable linked to GOPATH library", "../../bin/exe")
|
2015-05-07 03:29:47 -06:00
|
|
|
}
|
|
|
|
|
2015-05-24 19:59:08 -06:00
|
|
|
// The shared library contains a note listing the packages it contains in a section
|
|
|
|
// that is not mapped into memory.
|
|
|
|
func testPkgListNote(t *testing.T, f *elf.File, note *note) {
|
|
|
|
if note.section.Flags != 0 {
|
2017-10-09 19:07:32 -06:00
|
|
|
t.Errorf("package list section has flags %v, want 0", note.section.Flags)
|
2015-05-24 19:59:08 -06:00
|
|
|
}
|
|
|
|
if isOffsetLoaded(f, note.section.Offset) {
|
|
|
|
t.Errorf("package list section contained in PT_LOAD segment")
|
|
|
|
}
|
2019-02-21 12:17:19 -07:00
|
|
|
if note.desc != "testshared/depBase\n" {
|
|
|
|
t.Errorf("incorrect package list %q, want %q", note.desc, "testshared/depBase\n")
|
2015-05-24 19:59:08 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The shared library contains a note containing the ABI hash that is mapped into
|
|
|
|
// memory and there is a local symbol called go.link.abihashbytes that points 16
|
|
|
|
// bytes into it.
|
|
|
|
func testABIHashNote(t *testing.T, f *elf.File, note *note) {
|
|
|
|
if note.section.Flags != elf.SHF_ALLOC {
|
2017-10-09 19:07:32 -06:00
|
|
|
t.Errorf("abi hash section has flags %v, want SHF_ALLOC", note.section.Flags)
|
2015-05-24 19:59:08 -06:00
|
|
|
}
|
|
|
|
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 {
|
2017-10-09 19:07:32 -06:00
|
|
|
t.Errorf("%s has incorrect binding %v, want STB_LOCAL", hashbytes.Name, elf.ST_BIND(hashbytes.Info))
|
2015-05-24 19:59:08 -06:00
|
|
|
}
|
|
|
|
if f.Sections[hashbytes.Section] != note.section {
|
cmd/go: run vet automatically during go test
This CL adds an automatic, limited "go vet" to "go test".
If the building of a test package fails, vet is not run.
If vet fails, the test is not run.
The goal is that users don't notice vet as part of the "go test"
process at all, until vet speaks up and says something important.
This should help users find real problems in their code faster
(vet can just point to them instead of needing to debug a
test failure) and expands the scope of what kinds of things
vet can help with.
The "go vet" runs in parallel with the linking of the test binary,
so for incremental builds it typically does not slow the overall
"go test" at all: there's spare machine capacity during the link.
all.bash has less spare machine capacity. This CL increases
the time for all.bash on my laptop from 4m41s to 4m48s (+2.5%)
To opt out for a given run, use "go test -vet=off".
The vet checks used during "go test" are a subset of the full set,
restricted to ones that are 100% correct and therefore acceptable
to make mandatory. In this CL, that set is atomic, bool, buildtags,
nilfunc, and printf. Including printf is debatable, but I want to
include it for now and find out what needs to be scaled back.
(It already found one real problem in package os's tests that
previous go vet os had not turned up.)
Now that we can rely on type information it may be that printf
should make its function-name-based heuristic less aggressive
and have a whitelist of known print/printf functions.
Determining the exact set for Go 1.10 is #18085.
Running vet also means that programs now have to type-check
with both cmd/compile and go/types in order to pass "go test".
We don't start vet until cmd/compile has built the test package,
so normally the added go/types check doesn't find anything.
However, there is at least one instance where go/types is more
precise than cmd/compile: declared and not used errors involving
variables captured into closures.
This CL includes a printf fix to os/os_test.go and many declared
and not used fixes in the race detector tests.
Fixes #18084.
Change-Id: I353e00b9d1f9fec540c7557db5653e7501f5e1c9
Reviewed-on: https://go-review.googlesource.com/74356
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-10-31 11:00:51 -06:00
|
|
|
t.Errorf("%s has incorrect section %v, want %s", hashbytes.Name, f.Sections[hashbytes.Section].Name, note.section.Name)
|
2015-05-24 19:59:08 -06:00
|
|
|
}
|
|
|
|
if hashbytes.Value-note.section.Addr != 16 {
|
2017-10-09 19:07:32 -06:00
|
|
|
t.Errorf("%s has incorrect offset into section %d, want 16", hashbytes.Name, hashbytes.Value-note.section.Addr)
|
2015-05-24 19:59:08 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-24 20:51:02 -06:00
|
|
|
// A Go shared library contains a note indicating which other Go shared libraries it
|
|
|
|
// was linked against in an unmapped section.
|
|
|
|
func testDepsNote(t *testing.T, f *elf.File, note *note) {
|
|
|
|
if note.section.Flags != 0 {
|
2017-10-09 19:07:32 -06:00
|
|
|
t.Errorf("package list section has flags %v, want 0", note.section.Flags)
|
2015-05-24 20:51:02 -06:00
|
|
|
}
|
|
|
|
if isOffsetLoaded(f, note.section.Offset) {
|
|
|
|
t.Errorf("package list section contained in PT_LOAD segment")
|
|
|
|
}
|
2016-05-03 17:23:24 -06:00
|
|
|
// libdepBase.so just links against the lib containing the runtime.
|
2015-05-24 20:51:02 -06:00
|
|
|
if note.desc != soname {
|
2017-10-09 19:07:32 -06:00
|
|
|
t.Errorf("incorrect dependency list %q, want %q", note.desc, soname)
|
2015-05-24 20:51:02 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-24 19:59:08 -06:00
|
|
|
// The shared library contains notes with defined contents; see above.
|
|
|
|
func TestNotes(t *testing.T) {
|
2019-02-21 12:17:19 -07:00
|
|
|
goCmd(t, "install", "-buildmode=shared", "-linkshared", "./depBase")
|
|
|
|
shlib := goCmd(t, "list", "-f", "{{.Shlib}}", "-buildmode=shared", "-linkshared", "./depBase")
|
|
|
|
f, err := elf.Open(shlib)
|
2015-05-24 19:59:08 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
notes, err := readNotes(f)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
pkgListNoteFound := false
|
|
|
|
abiHashNoteFound := false
|
2015-05-24 20:51:02 -06:00
|
|
|
depsNoteFound := false
|
2015-05-24 19:59:08 -06:00
|
|
|
for _, note := range notes {
|
2015-06-04 12:27:39 -06:00
|
|
|
if note.name != "Go\x00\x00" {
|
2015-05-24 19:59:08 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch note.tag {
|
|
|
|
case 1: // ELF_NOTE_GOPKGLIST_TAG
|
|
|
|
if pkgListNoteFound {
|
|
|
|
t.Error("multiple package list notes")
|
|
|
|
}
|
|
|
|
testPkgListNote(t, f, note)
|
|
|
|
pkgListNoteFound = true
|
|
|
|
case 2: // ELF_NOTE_GOABIHASH_TAG
|
|
|
|
if abiHashNoteFound {
|
|
|
|
t.Error("multiple abi hash notes")
|
|
|
|
}
|
|
|
|
testABIHashNote(t, f, note)
|
|
|
|
abiHashNoteFound = true
|
2015-05-24 20:51:02 -06:00
|
|
|
case 3: // ELF_NOTE_GODEPS_TAG
|
|
|
|
if depsNoteFound {
|
2018-08-22 23:06:47 -06:00
|
|
|
t.Error("multiple dependency list notes")
|
2015-05-24 20:51:02 -06:00
|
|
|
}
|
|
|
|
testDepsNote(t, f, note)
|
|
|
|
depsNoteFound = true
|
2015-05-24 19:59:08 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !pkgListNoteFound {
|
|
|
|
t.Error("package list note not found")
|
|
|
|
}
|
|
|
|
if !abiHashNoteFound {
|
|
|
|
t.Error("abi hash note not found")
|
|
|
|
}
|
2015-05-24 20:51:02 -06:00
|
|
|
if !depsNoteFound {
|
|
|
|
t.Error("deps note not found")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 17:23:24 -06:00
|
|
|
// Build a GOPATH package (depBase) into a shared library that links against the goroot
|
2018-10-06 00:10:25 -06:00
|
|
|
// runtime, another package (dep2) that links against the first, and an
|
2015-05-24 20:51:02 -06:00
|
|
|
// executable that links against dep2.
|
2015-05-10 19:39:28 -06:00
|
|
|
func TestTwoGopathShlibs(t *testing.T) {
|
2019-02-21 12:17:19 -07:00
|
|
|
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")
|
2015-05-24 19:59:08 -06:00
|
|
|
}
|
|
|
|
|
2016-05-03 17:23:24 -06:00
|
|
|
func TestThreeGopathShlibs(t *testing.T) {
|
2019-02-21 12:17:19 -07:00
|
|
|
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")
|
2016-05-03 17:23:24 -06:00
|
|
|
}
|
|
|
|
|
2019-02-21 12:17:19 -07:00
|
|
|
// If gccgo is not available or not new enough, call t.Skip.
|
|
|
|
func requireGccgo(t *testing.T) {
|
|
|
|
t.Helper()
|
|
|
|
|
2015-05-10 19:39:28 -06:00
|
|
|
gccgoName := os.Getenv("GCCGO")
|
|
|
|
if gccgoName == "" {
|
|
|
|
gccgoName = "gccgo"
|
|
|
|
}
|
2015-08-23 16:44:36 -06:00
|
|
|
gccgoPath, err := exec.LookPath(gccgoName)
|
2015-05-10 19:39:28 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Skip("gccgo not found")
|
|
|
|
}
|
2015-08-23 16:44:36 -06:00
|
|
|
cmd := exec.Command(gccgoPath, "-dumpversion")
|
|
|
|
output, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%s -dumpversion failed: %v\n%s", gccgoPath, err, output)
|
|
|
|
}
|
|
|
|
if string(output) < "5" {
|
|
|
|
t.Skipf("gccgo too old (%s)", strings.TrimSpace(string(output)))
|
|
|
|
}
|
2019-02-21 12:17:19 -07:00
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
2015-08-23 16:44:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build a GOPATH package into a shared library with gccgo and an executable that
|
|
|
|
// links against it.
|
|
|
|
func TestGoPathShlibGccgo(t *testing.T) {
|
2019-02-21 12:17:19 -07:00
|
|
|
requireGccgo(t)
|
2015-08-23 16:44:36 -06:00
|
|
|
|
|
|
|
libgoRE := regexp.MustCompile("libgo.so.[0-9]+")
|
|
|
|
|
2019-02-21 12:17:19 -07:00
|
|
|
goCmd(t, "install", "-compiler=gccgo", "-buildmode=shared", "-linkshared", "./depBase")
|
|
|
|
|
|
|
|
// Run 'go list' after 'go install': with gccgo, we apparently don't know the
|
|
|
|
// shlib location until after we've installed it.
|
|
|
|
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))
|
2015-05-10 19:39:28 -06:00
|
|
|
// And check it runs.
|
2019-02-21 12:17:19 -07:00
|
|
|
run(t, "gccgo-built", "../../bin/exe")
|
2015-05-10 19:39:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// The gccgo version of TestTwoGopathShlibs: build a GOPATH package into a shared
|
|
|
|
// library with gccgo, another GOPATH package that depends on the first and an
|
|
|
|
// executable that links the second library.
|
|
|
|
func TestTwoGopathShlibsGccgo(t *testing.T) {
|
2019-02-21 12:17:19 -07:00
|
|
|
requireGccgo(t)
|
2015-05-10 19:39:28 -06:00
|
|
|
|
|
|
|
libgoRE := regexp.MustCompile("libgo.so.[0-9]+")
|
|
|
|
|
2019-02-21 12:17:19 -07:00
|
|
|
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")
|
|
|
|
|
|
|
|
// Run 'go list' after 'go install': with gccgo, we apparently don't know the
|
|
|
|
// shlib location until after we've installed it.
|
|
|
|
dep2 := goCmd(t, "list", "-compiler=gccgo", "-buildmode=shared", "-linkshared", "-f", "{{.Shlib}}", "./dep2")
|
|
|
|
depBase := goCmd(t, "list", "-compiler=gccgo", "-buildmode=shared", "-linkshared", "-f", "{{.Shlib}}", "./depBase")
|
2015-05-10 19:39:28 -06:00
|
|
|
|
2019-02-21 12:17:19 -07:00
|
|
|
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))
|
2015-05-10 19:39:28 -06:00
|
|
|
|
|
|
|
// And check it runs.
|
2019-02-21 12:17:19 -07:00
|
|
|
run(t, "gccgo-built", "../../bin/exe2")
|
2015-05-10 19:39:28 -06:00
|
|
|
}
|
|
|
|
|
2015-05-07 03:29:47 -06:00
|
|
|
// Testing rebuilding of shared libraries when they are stale is a bit more
|
|
|
|
// complicated that it seems like it should be. First, we make everything "old": but
|
2015-06-23 17:50:12 -06:00
|
|
|
// only a few seconds old, or it might be older than gc (or the runtime source) and
|
2015-05-07 03:29:47 -06:00
|
|
|
// everything will get rebuilt. Then define a timestamp slightly newer than this
|
|
|
|
// time, which is what we set the mtime to of a file to cause it to be seen as new,
|
|
|
|
// and finally another slightly even newer one that we can compare files against to
|
|
|
|
// see if they have been rebuilt.
|
|
|
|
var oldTime = time.Now().Add(-9 * time.Second)
|
|
|
|
var nearlyNew = time.Now().Add(-6 * time.Second)
|
|
|
|
var stampTime = time.Now().Add(-3 * time.Second)
|
|
|
|
|
|
|
|
// resetFileStamps makes "everything" (bin, src, pkg from GOPATH and the
|
|
|
|
// test-specific parts of GOROOT) appear old.
|
|
|
|
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 {
|
2019-11-22 08:34:55 -07:00
|
|
|
log.Panicf("resetFileStamps failed: %v", err)
|
2015-05-07 03:29:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-02-21 12:17:19 -07:00
|
|
|
reset("../../bin")
|
|
|
|
reset("../../pkg")
|
|
|
|
reset("../../src")
|
2015-05-07 03:29:47 -06:00
|
|
|
reset(gorootInstallDir)
|
|
|
|
}
|
|
|
|
|
2017-10-27 18:36:14 -06:00
|
|
|
// touch changes path and returns a function that changes it back.
|
|
|
|
// It also sets the time of the file, so that we can see if it is rewritten.
|
|
|
|
func touch(t *testing.T, path string) (cleanup func()) {
|
2019-11-22 08:34:55 -07:00
|
|
|
t.Helper()
|
2017-10-27 18:36:14 -06:00
|
|
|
data, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
old := make([]byte, len(data))
|
|
|
|
copy(old, data)
|
|
|
|
if bytes.HasPrefix(data, []byte("!<arch>\n")) {
|
|
|
|
// Change last digit of build ID.
|
|
|
|
// (Content ID in the new content-based build IDs.)
|
|
|
|
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 {
|
|
|
|
// assume it's a text file
|
|
|
|
data = append(data, '\n')
|
|
|
|
}
|
2019-11-22 08:34:55 -07:00
|
|
|
|
|
|
|
// If the file is still a symlink from an overlay, delete it so that we will
|
|
|
|
// replace it with a regular file instead of overwriting the symlinked one.
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we're replacing a symlink to a read-only file, make the new file
|
|
|
|
// user-writable.
|
|
|
|
perm := fi.Mode().Perm() | 0200
|
|
|
|
|
|
|
|
if err := ioutil.WriteFile(path, data, perm); err != nil {
|
2017-10-27 18:36:14 -06:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-07 03:29:47 -06:00
|
|
|
if err := os.Chtimes(path, nearlyNew, nearlyNew); err != nil {
|
2017-10-27 18:36:14 -06:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return func() {
|
2019-11-22 08:34:55 -07:00
|
|
|
if err := ioutil.WriteFile(path, old, perm); err != nil {
|
2017-10-27 18:36:14 -06:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-07 03:29:47 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// isNew returns if the path is newer than the time stamp used by touch.
|
2017-10-27 18:36:14 -06:00
|
|
|
func isNew(t *testing.T, path string) bool {
|
2019-02-21 12:17:19 -07:00
|
|
|
t.Helper()
|
2015-05-07 03:29:47 -06:00
|
|
|
fi, err := os.Stat(path)
|
|
|
|
if err != nil {
|
2017-10-27 18:36:14 -06:00
|
|
|
t.Fatal(err)
|
2015-05-07 03:29:47 -06:00
|
|
|
}
|
|
|
|
return fi.ModTime().After(stampTime)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fail unless path has been rebuilt (i.e. is newer than the time stamp used by
|
|
|
|
// isNew)
|
|
|
|
func AssertRebuilt(t *testing.T, msg, path string) {
|
2017-10-27 18:36:14 -06:00
|
|
|
t.Helper()
|
|
|
|
if !isNew(t, path) {
|
2015-05-07 03:29:47 -06:00
|
|
|
t.Errorf("%s was not rebuilt (%s)", msg, path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fail if path has been rebuilt (i.e. is newer than the time stamp used by isNew)
|
|
|
|
func AssertNotRebuilt(t *testing.T, msg, path string) {
|
2017-10-27 18:36:14 -06:00
|
|
|
t.Helper()
|
|
|
|
if isNew(t, path) {
|
2015-05-07 03:29:47 -06:00
|
|
|
t.Errorf("%s was rebuilt (%s)", msg, path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRebuilding(t *testing.T) {
|
2019-02-21 12:17:19 -07:00
|
|
|
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]
|
2015-05-07 03:29:47 -06:00
|
|
|
|
|
|
|
// If the source is newer than both the .a file and the .so, both are rebuilt.
|
2017-10-27 18:36:14 -06:00
|
|
|
t.Run("newsource", func(t *testing.T) {
|
|
|
|
resetFileStamps()
|
2019-02-21 12:17:19 -07:00
|
|
|
cleanup := touch(t, "./depBase/dep.go")
|
2017-10-27 18:36:14 -06:00
|
|
|
defer func() {
|
|
|
|
cleanup()
|
2019-02-21 12:17:19 -07:00
|
|
|
goCmd(t, "install", "-linkshared", "./exe")
|
2017-10-27 18:36:14 -06:00
|
|
|
}()
|
2019-02-21 12:17:19 -07:00
|
|
|
goCmd(t, "install", "-linkshared", "./exe")
|
|
|
|
AssertRebuilt(t, "new source", target)
|
|
|
|
AssertRebuilt(t, "new source", shlib)
|
2017-10-27 18:36:14 -06:00
|
|
|
})
|
2015-05-07 03:29:47 -06:00
|
|
|
|
|
|
|
// If the .a file is newer than the .so, the .so is rebuilt (but not the .a)
|
2017-10-27 18:36:14 -06:00
|
|
|
t.Run("newarchive", func(t *testing.T) {
|
|
|
|
resetFileStamps()
|
2019-02-21 12:17:19 -07:00
|
|
|
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)
|
2017-10-27 18:36:14 -06:00
|
|
|
defer func() {
|
|
|
|
cleanup()
|
2019-02-21 12:17:19 -07:00
|
|
|
goCmd(t, "install", "-v", "-linkshared", "./exe")
|
2017-10-27 18:36:14 -06:00
|
|
|
}()
|
2019-02-21 12:17:19 -07:00
|
|
|
goCmd(t, "install", "-v", "-linkshared", "./exe")
|
|
|
|
AssertNotRebuilt(t, "new .a file", target)
|
|
|
|
AssertRebuilt(t, "new .a file", shlib)
|
2017-10-27 18:36:14 -06:00
|
|
|
})
|
2015-05-07 03:29:47 -06:00
|
|
|
}
|
|
|
|
|
2017-10-27 18:36:14 -06:00
|
|
|
func appendFile(t *testing.T, path, content string) {
|
2019-02-21 12:17:19 -07:00
|
|
|
t.Helper()
|
2015-05-07 03:29:47 -06:00
|
|
|
f, err := os.OpenFile(path, os.O_WRONLY|os.O_APPEND, 0660)
|
|
|
|
if err != nil {
|
2017-10-27 18:36:14 -06:00
|
|
|
t.Fatalf("os.OpenFile failed: %v", err)
|
2015-05-07 03:29:47 -06:00
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err := f.Close()
|
|
|
|
if err != nil {
|
2017-10-27 18:36:14 -06:00
|
|
|
t.Fatalf("f.Close failed: %v", err)
|
2015-05-07 03:29:47 -06:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
_, err = f.WriteString(content)
|
|
|
|
if err != nil {
|
2017-10-27 18:36:14 -06:00
|
|
|
t.Fatalf("f.WriteString failed: %v", err)
|
2015-05-07 03:29:47 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 12:17:19 -07:00
|
|
|
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)
|
2017-04-11 22:08:46 -06:00
|
|
|
if err != nil {
|
2019-02-21 12:17:19 -07:00
|
|
|
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)
|
2017-04-11 22:08:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-07 03:29:47 -06:00
|
|
|
func TestABIChecking(t *testing.T) {
|
2019-02-21 12:17:19 -07:00
|
|
|
goCmd(t, "install", "-buildmode=shared", "-linkshared", "./depBase")
|
|
|
|
goCmd(t, "install", "-linkshared", "./exe")
|
2015-05-07 03:29:47 -06:00
|
|
|
|
2016-05-03 17:23:24 -06:00
|
|
|
// If we make an ABI-breaking change to depBase and rebuild libp.so but not exe,
|
2015-05-07 03:29:47 -06:00
|
|
|
// exe will abort with a complaint on startup.
|
|
|
|
// This assumes adding an exported function breaks ABI, which is not true in
|
2016-04-03 05:43:27 -06:00
|
|
|
// some senses but suffices for the narrow definition of ABI compatibility the
|
2015-05-07 03:29:47 -06:00
|
|
|
// toolchain uses today.
|
2015-06-16 12:03:19 -06:00
|
|
|
resetFileStamps()
|
2019-02-21 12:17:19 -07:00
|
|
|
|
|
|
|
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")
|
2015-05-07 03:29:47 -06:00
|
|
|
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
|
2019-02-21 12:17:19 -07:00
|
|
|
const wantPrefix = "abi mismatch detected between the executable and lib"
|
2015-05-07 03:29:47 -06:00
|
|
|
for scanner.Scan() {
|
2019-02-21 12:17:19 -07:00
|
|
|
if strings.HasPrefix(scanner.Text(), wantPrefix) {
|
2015-05-07 03:29:47 -06:00
|
|
|
foundMsg = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err = scanner.Err(); err != nil {
|
|
|
|
t.Errorf("scanner encountered error: %v", err)
|
|
|
|
}
|
|
|
|
if !foundMsg {
|
2019-02-21 12:17:19 -07:00
|
|
|
t.Fatalf("exe failed, but without line %q; got output:\n%s", wantPrefix, output)
|
2015-05-07 03:29:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rebuilding exe makes it work again.
|
2019-02-21 12:17:19 -07:00
|
|
|
goCmd(t, "install", "-linkshared", "./exe")
|
|
|
|
run(t, "rebuilt exe", "../../bin/exe")
|
2015-05-07 03:29:47 -06:00
|
|
|
|
|
|
|
// If we make a change which does not break ABI (such as adding an unexported
|
2017-04-11 22:08:46 -06:00
|
|
|
// function) and rebuild libdepBase.so, exe still works, even if new function
|
|
|
|
// is in a file by itself.
|
2015-06-16 12:03:19 -06:00
|
|
|
resetFileStamps()
|
2019-02-21 12:17:19 -07:00
|
|
|
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")
|
2015-05-07 03:29:47 -06:00
|
|
|
}
|
2016-01-19 19:31:26 -07:00
|
|
|
|
|
|
|
// If a package 'explicit' imports a package 'implicit', building
|
|
|
|
// 'explicit' into a shared library implicitly includes implicit in
|
|
|
|
// the shared library. Building an executable that imports both
|
|
|
|
// explicit and implicit builds the code from implicit into the
|
|
|
|
// executable rather than fetching it from the shared library. The
|
|
|
|
// link still succeeds and the executable still runs though.
|
|
|
|
func TestImplicitInclusion(t *testing.T) {
|
2019-02-21 12:17:19 -07:00
|
|
|
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")
|
2016-01-19 19:31:26 -07:00
|
|
|
}
|
2017-01-11 16:02:16 -07:00
|
|
|
|
|
|
|
// Tests to make sure that the type fields of empty interfaces and itab
|
|
|
|
// fields of nonempty interfaces are unique even across modules,
|
|
|
|
// so that interface equality works correctly.
|
|
|
|
func TestInterface(t *testing.T) {
|
2019-02-21 12:17:19 -07:00
|
|
|
goCmd(t, "install", "-buildmode=shared", "-linkshared", "./iface_a")
|
2017-01-11 16:02:16 -07:00
|
|
|
// Note: iface_i gets installed implicitly as a dependency of iface_a.
|
2019-02-21 12:17:19 -07:00
|
|
|
goCmd(t, "install", "-buildmode=shared", "-linkshared", "./iface_b")
|
|
|
|
goCmd(t, "install", "-linkshared", "./iface")
|
|
|
|
run(t, "running type/itab uniqueness tester", "../../bin/iface")
|
2017-01-11 16:02:16 -07:00
|
|
|
}
|
2017-09-20 10:12:33 -06:00
|
|
|
|
|
|
|
// Access a global variable from a library.
|
|
|
|
func TestGlobal(t *testing.T) {
|
2019-02-21 12:17:19 -07:00
|
|
|
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)
|
2017-09-20 10:12:33 -06:00
|
|
|
}
|
2018-07-16 20:09:24 -06:00
|
|
|
|
|
|
|
// Run a test using -linkshared of an installed shared package.
|
|
|
|
// Issue 26400.
|
|
|
|
func TestTestInstalledShared(t *testing.T) {
|
|
|
|
goCmd(nil, "test", "-linkshared", "-test.short", "sync/atomic")
|
|
|
|
}
|
2018-12-07 19:22:04 -07:00
|
|
|
|
|
|
|
// Test generated pointer method with -linkshared.
|
|
|
|
// Issue 25065.
|
|
|
|
func TestGeneratedMethod(t *testing.T) {
|
2019-02-21 12:17:19 -07:00
|
|
|
goCmd(t, "install", "-buildmode=shared", "-linkshared", "./issue25065")
|
2018-12-07 19:22:04 -07:00
|
|
|
}
|
2019-07-13 13:20:43 -06:00
|
|
|
|
|
|
|
// Test use of shared library struct with generated hash function.
|
|
|
|
// Issue 30768.
|
|
|
|
func TestGeneratedHash(t *testing.T) {
|
|
|
|
goCmd(nil, "install", "-buildmode=shared", "-linkshared", "./issue30768/issue30768lib")
|
|
|
|
goCmd(nil, "test", "-linkshared", "./issue30768")
|
|
|
|
}
|