2019-02-21 15:38:53 -07:00
|
|
|
// Copyright 2019 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package plugin_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2019-05-15 12:31:48 -06:00
|
|
|
"flag"
|
2019-02-21 15:38:53 -07:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var gcflags string = os.Getenv("GO_GCFLAGS")
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
2019-05-15 12:31:48 -06:00
|
|
|
flag.Parse()
|
|
|
|
if testing.Short() && os.Getenv("GO_BUILDER_NAME") == "" {
|
|
|
|
fmt.Printf("SKIP - short mode and $GO_BUILDER_NAME not set\n")
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
2019-02-21 15:38:53 -07:00
|
|
|
log.SetFlags(log.Lshortfile)
|
2019-03-01 14:05:33 -07:00
|
|
|
os.Exit(testMain(m))
|
|
|
|
}
|
2019-02-21 15:38:53 -07:00
|
|
|
|
2021-04-13 12:12:43 -06:00
|
|
|
// tmpDir is used to cleanup logged commands -- s/tmpDir/$TMPDIR/
|
|
|
|
var tmpDir string
|
|
|
|
|
|
|
|
// prettyPrintf prints lines with tmpDir sanitized.
|
|
|
|
func prettyPrintf(format string, args ...interface{}) {
|
|
|
|
s := fmt.Sprintf(format, args...)
|
|
|
|
if tmpDir != "" {
|
|
|
|
s = strings.ReplaceAll(s, tmpDir, "$TMPDIR")
|
|
|
|
}
|
|
|
|
fmt.Print(s)
|
|
|
|
}
|
|
|
|
|
2019-03-01 14:05:33 -07:00
|
|
|
func testMain(m *testing.M) int {
|
2020-06-01 16:06:45 -06:00
|
|
|
// Copy testdata into GOPATH/src/testplugin, along with a go.mod file
|
2019-02-21 15:38:53 -07:00
|
|
|
// declaring the same path.
|
|
|
|
|
2021-04-03 02:10:47 -06:00
|
|
|
GOPATH, err := os.MkdirTemp("", "plugin_test")
|
2019-02-21 15:38:53 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(GOPATH)
|
2021-04-13 12:12:43 -06:00
|
|
|
tmpDir = GOPATH
|
2019-02-21 15:38:53 -07:00
|
|
|
|
|
|
|
modRoot := filepath.Join(GOPATH, "src", "testplugin")
|
|
|
|
altRoot := filepath.Join(GOPATH, "alt", "src", "testplugin")
|
|
|
|
for srcRoot, dstRoot := range map[string]string{
|
|
|
|
"testdata": modRoot,
|
|
|
|
filepath.Join("altpath", "testdata"): altRoot,
|
|
|
|
} {
|
|
|
|
if err := overlayDir(dstRoot, srcRoot); err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
2021-04-13 12:12:43 -06:00
|
|
|
prettyPrintf("mkdir -p %s\n", dstRoot)
|
|
|
|
prettyPrintf("rsync -a %s/ %s\n", srcRoot, dstRoot)
|
|
|
|
|
2021-04-03 02:10:47 -06:00
|
|
|
if err := os.WriteFile(filepath.Join(dstRoot, "go.mod"), []byte("module testplugin\n"), 0666); err != nil {
|
2019-02-21 15:38:53 -07:00
|
|
|
log.Panic(err)
|
|
|
|
}
|
2021-04-13 12:12:43 -06:00
|
|
|
prettyPrintf("echo 'module testplugin' > %s/go.mod\n", dstRoot)
|
2019-02-21 15:38:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
os.Setenv("GOPATH", filepath.Join(GOPATH, "alt"))
|
|
|
|
if err := os.Chdir(altRoot); err != nil {
|
|
|
|
log.Panic(err)
|
2021-04-13 12:12:43 -06:00
|
|
|
} else {
|
|
|
|
prettyPrintf("cd %s\n", altRoot)
|
2019-02-21 15:38:53 -07:00
|
|
|
}
|
2019-02-25 20:09:46 -07:00
|
|
|
os.Setenv("PWD", altRoot)
|
2019-02-21 15:38:53 -07:00
|
|
|
goCmd(nil, "build", "-buildmode=plugin", "-o", filepath.Join(modRoot, "plugin-mismatch.so"), "./plugin-mismatch")
|
|
|
|
|
|
|
|
os.Setenv("GOPATH", GOPATH)
|
|
|
|
if err := os.Chdir(modRoot); err != nil {
|
|
|
|
log.Panic(err)
|
2021-04-13 12:12:43 -06:00
|
|
|
} else {
|
|
|
|
prettyPrintf("cd %s\n", modRoot)
|
2019-02-21 15:38:53 -07:00
|
|
|
}
|
2019-02-25 20:09:46 -07:00
|
|
|
os.Setenv("PWD", modRoot)
|
2019-02-21 15:38:53 -07:00
|
|
|
|
|
|
|
os.Setenv("LD_LIBRARY_PATH", modRoot)
|
|
|
|
|
2019-11-20 12:43:04 -07:00
|
|
|
goCmd(nil, "build", "-buildmode=plugin", "./plugin1")
|
2019-02-21 15:38:53 -07:00
|
|
|
goCmd(nil, "build", "-buildmode=plugin", "./plugin2")
|
2021-04-03 02:10:47 -06:00
|
|
|
so, err := os.ReadFile("plugin2.so")
|
2019-02-21 15:38:53 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
2021-04-03 02:10:47 -06:00
|
|
|
if err := os.WriteFile("plugin2-dup.so", so, 0444); err != nil {
|
2019-02-21 15:38:53 -07:00
|
|
|
log.Panic(err)
|
|
|
|
}
|
2021-04-13 12:12:43 -06:00
|
|
|
prettyPrintf("cp plugin2.so plugin2-dup.so\n")
|
2019-02-21 15:38:53 -07:00
|
|
|
|
|
|
|
goCmd(nil, "build", "-buildmode=plugin", "-o=sub/plugin1.so", "./sub/plugin1")
|
|
|
|
goCmd(nil, "build", "-buildmode=plugin", "-o=unnamed1.so", "./unnamed1/main.go")
|
|
|
|
goCmd(nil, "build", "-buildmode=plugin", "-o=unnamed2.so", "./unnamed2/main.go")
|
|
|
|
goCmd(nil, "build", "-o", "host.exe", "./host")
|
|
|
|
|
2019-03-01 14:05:33 -07:00
|
|
|
return m.Run()
|
2019-02-21 15:38:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func goCmd(t *testing.T, op string, args ...string) {
|
|
|
|
if t != nil {
|
|
|
|
t.Helper()
|
|
|
|
}
|
|
|
|
run(t, "go", append([]string{op, "-gcflags", gcflags}, args...)...)
|
|
|
|
}
|
|
|
|
|
2021-04-13 12:12:43 -06:00
|
|
|
// escape converts a string to something suitable for a shell command line.
|
|
|
|
func escape(s string) string {
|
|
|
|
s = strings.Replace(s, "\\", "\\\\", -1)
|
|
|
|
s = strings.Replace(s, "'", "\\'", -1)
|
|
|
|
// Conservative guess at characters that will force quoting
|
|
|
|
if s == "" || strings.ContainsAny(s, "\\ ;#*&$~?!|[]()<>{}`") {
|
|
|
|
s = "'" + s + "'"
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// asCommandLine renders cmd as something that could be copy-and-pasted into a command line
|
|
|
|
func asCommandLine(cwd string, cmd *exec.Cmd) string {
|
|
|
|
s := "("
|
|
|
|
if cmd.Dir != "" && cmd.Dir != cwd {
|
|
|
|
s += "cd" + escape(cmd.Dir) + ";"
|
|
|
|
}
|
|
|
|
for _, e := range cmd.Env {
|
|
|
|
if !strings.HasPrefix(e, "PATH=") &&
|
|
|
|
!strings.HasPrefix(e, "HOME=") &&
|
|
|
|
!strings.HasPrefix(e, "USER=") &&
|
|
|
|
!strings.HasPrefix(e, "SHELL=") {
|
|
|
|
s += " "
|
|
|
|
s += escape(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// These EVs are relevant to this test.
|
|
|
|
for _, e := range os.Environ() {
|
|
|
|
if strings.HasPrefix(e, "PWD=") ||
|
|
|
|
strings.HasPrefix(e, "GOPATH=") ||
|
|
|
|
strings.HasPrefix(e, "LD_LIBRARY_PATH=") {
|
|
|
|
s += " "
|
|
|
|
s += escape(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, a := range cmd.Args {
|
|
|
|
s += " "
|
|
|
|
s += escape(a)
|
|
|
|
}
|
|
|
|
s += " )"
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2019-02-21 15:38:53 -07:00
|
|
|
func run(t *testing.T, bin string, args ...string) string {
|
|
|
|
cmd := exec.Command(bin, args...)
|
2021-04-13 12:12:43 -06:00
|
|
|
cmdLine := asCommandLine(".", cmd)
|
|
|
|
prettyPrintf("%s\n", cmdLine)
|
2019-02-21 15:38:53 -07:00
|
|
|
cmd.Stderr = new(strings.Builder)
|
|
|
|
out, err := cmd.Output()
|
|
|
|
if err != nil {
|
|
|
|
if t == nil {
|
|
|
|
log.Panicf("%s: %v\n%s", strings.Join(cmd.Args, " "), err, cmd.Stderr)
|
|
|
|
} else {
|
|
|
|
t.Helper()
|
|
|
|
t.Fatalf("%s: %v\n%s", strings.Join(cmd.Args, " "), err, cmd.Stderr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(bytes.TrimSpace(out))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDWARFSections(t *testing.T) {
|
|
|
|
// test that DWARF sections are emitted for plugins and programs importing "plugin"
|
2019-11-08 14:11:44 -07:00
|
|
|
goCmd(t, "run", "./checkdwarf/main.go", "plugin2.so", "plugin2.UnexportedNameReuse")
|
2019-02-21 15:38:53 -07:00
|
|
|
goCmd(t, "run", "./checkdwarf/main.go", "./host.exe", "main.main")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunHost(t *testing.T) {
|
|
|
|
run(t, "./host.exe")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUniqueTypesAndItabs(t *testing.T) {
|
|
|
|
goCmd(t, "build", "-buildmode=plugin", "./iface_a")
|
|
|
|
goCmd(t, "build", "-buildmode=plugin", "./iface_b")
|
|
|
|
goCmd(t, "build", "-o", "iface.exe", "./iface")
|
|
|
|
run(t, "./iface.exe")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIssue18676(t *testing.T) {
|
|
|
|
// make sure we don't add the same itab twice.
|
|
|
|
// The buggy code hangs forever, so use a timeout to check for that.
|
|
|
|
goCmd(t, "build", "-buildmode=plugin", "-o", "plugin.so", "./issue18676/plugin.go")
|
|
|
|
goCmd(t, "build", "-o", "issue18676.exe", "./issue18676/main.go")
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
cmd := exec.CommandContext(ctx, "./issue18676.exe")
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%s: %v\n%s", strings.Join(cmd.Args, " "), err, out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIssue19534(t *testing.T) {
|
|
|
|
// Test that we can load a plugin built in a path with non-alpha characters.
|
|
|
|
goCmd(t, "build", "-buildmode=plugin", "-ldflags='-pluginpath=issue.19534'", "-o", "plugin.so", "./issue19534/plugin.go")
|
|
|
|
goCmd(t, "build", "-o", "issue19534.exe", "./issue19534/main.go")
|
|
|
|
run(t, "./issue19534.exe")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIssue18584(t *testing.T) {
|
|
|
|
goCmd(t, "build", "-buildmode=plugin", "-o", "plugin.so", "./issue18584/plugin.go")
|
|
|
|
goCmd(t, "build", "-o", "issue18584.exe", "./issue18584/main.go")
|
|
|
|
run(t, "./issue18584.exe")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIssue19418(t *testing.T) {
|
|
|
|
goCmd(t, "build", "-buildmode=plugin", "-ldflags=-X main.Val=linkstr", "-o", "plugin.so", "./issue19418/plugin.go")
|
|
|
|
goCmd(t, "build", "-o", "issue19418.exe", "./issue19418/main.go")
|
|
|
|
run(t, "./issue19418.exe")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIssue19529(t *testing.T) {
|
|
|
|
goCmd(t, "build", "-buildmode=plugin", "-o", "plugin.so", "./issue19529/plugin.go")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIssue22175(t *testing.T) {
|
|
|
|
goCmd(t, "build", "-buildmode=plugin", "-o", "issue22175_plugin1.so", "./issue22175/plugin1.go")
|
|
|
|
goCmd(t, "build", "-buildmode=plugin", "-o", "issue22175_plugin2.so", "./issue22175/plugin2.go")
|
|
|
|
goCmd(t, "build", "-o", "issue22175.exe", "./issue22175/main.go")
|
|
|
|
run(t, "./issue22175.exe")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIssue22295(t *testing.T) {
|
|
|
|
goCmd(t, "build", "-buildmode=plugin", "-o", "issue.22295.so", "./issue22295.pkg")
|
|
|
|
goCmd(t, "build", "-o", "issue22295.exe", "./issue22295.pkg/main.go")
|
|
|
|
run(t, "./issue22295.exe")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIssue24351(t *testing.T) {
|
|
|
|
goCmd(t, "build", "-buildmode=plugin", "-o", "issue24351.so", "./issue24351/plugin.go")
|
|
|
|
goCmd(t, "build", "-o", "issue24351.exe", "./issue24351/main.go")
|
|
|
|
run(t, "./issue24351.exe")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIssue25756(t *testing.T) {
|
|
|
|
goCmd(t, "build", "-buildmode=plugin", "-o", "life.so", "./issue25756/plugin")
|
|
|
|
goCmd(t, "build", "-o", "issue25756.exe", "./issue25756/main.go")
|
|
|
|
// Fails intermittently, but 20 runs should cause the failure
|
|
|
|
for n := 20; n > 0; n-- {
|
|
|
|
t.Run(fmt.Sprint(n), func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
run(t, "./issue25756.exe")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-11-13 12:42:45 -07:00
|
|
|
|
2021-05-12 11:11:03 -06:00
|
|
|
// Test with main using -buildmode=pie with plugin for issue #43228
|
|
|
|
func TestIssue25756pie(t *testing.T) {
|
|
|
|
goCmd(t, "build", "-buildmode=plugin", "-o", "life.so", "./issue25756/plugin")
|
|
|
|
goCmd(t, "build", "-buildmode=pie", "-o", "issue25756pie.exe", "./issue25756/main.go")
|
|
|
|
run(t, "./issue25756pie.exe")
|
|
|
|
}
|
|
|
|
|
2020-11-13 12:42:45 -07:00
|
|
|
func TestMethod(t *testing.T) {
|
|
|
|
// Exported symbol's method must be live.
|
|
|
|
goCmd(t, "build", "-buildmode=plugin", "-o", "plugin.so", "./method/plugin.go")
|
|
|
|
goCmd(t, "build", "-o", "method.exe", "./method/main.go")
|
2021-02-25 18:01:53 -07:00
|
|
|
run(t, "./method.exe")
|
|
|
|
}
|
2020-11-13 12:42:45 -07:00
|
|
|
|
2021-02-25 18:01:53 -07:00
|
|
|
func TestMethod2(t *testing.T) {
|
|
|
|
goCmd(t, "build", "-buildmode=plugin", "-o", "method2.so", "./method2/plugin.go")
|
|
|
|
goCmd(t, "build", "-o", "method2.exe", "./method2/main.go")
|
|
|
|
run(t, "./method2.exe")
|
2020-11-13 12:42:45 -07:00
|
|
|
}
|
2021-03-12 21:21:09 -07:00
|
|
|
|
2022-03-16 11:07:57 -06:00
|
|
|
func TestMethod3(t *testing.T) {
|
|
|
|
goCmd(t, "build", "-buildmode=plugin", "-o", "method3.so", "./method3/plugin.go")
|
|
|
|
goCmd(t, "build", "-o", "method3.exe", "./method3/main.go")
|
|
|
|
run(t, "./method3.exe")
|
|
|
|
}
|
|
|
|
|
2021-03-12 21:21:09 -07:00
|
|
|
func TestIssue44956(t *testing.T) {
|
|
|
|
goCmd(t, "build", "-buildmode=plugin", "-o", "issue44956p1.so", "./issue44956/plugin1.go")
|
|
|
|
goCmd(t, "build", "-buildmode=plugin", "-o", "issue44956p2.so", "./issue44956/plugin2.go")
|
|
|
|
goCmd(t, "build", "-o", "issue44956.exe", "./issue44956/main.go")
|
|
|
|
run(t, "./issue44956.exe")
|
|
|
|
}
|
2021-12-16 12:33:13 -07:00
|
|
|
|
|
|
|
func TestForkExec(t *testing.T) {
|
|
|
|
// Issue 38824: importing the plugin package causes it hang in forkExec on darwin.
|
|
|
|
|
|
|
|
t.Parallel()
|
|
|
|
goCmd(t, "build", "-o", "forkexec.exe", "./forkexec/main.go")
|
|
|
|
|
|
|
|
var cmd *exec.Cmd
|
|
|
|
done := make(chan int, 1)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
cmd = exec.Command("./forkexec.exe", "1")
|
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("running command failed: %v", err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
done <- 1
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-time.After(5 * time.Minute):
|
|
|
|
cmd.Process.Kill()
|
|
|
|
t.Fatalf("subprocess hang")
|
|
|
|
}
|
|
|
|
}
|