Commit 605331f4 authored by David Crawshaw's avatar David Crawshaw

cmd/go: pass plugin package name to compile -p

When compiling a plugin, package main gets a new name so as not to
conflict with the main package in the host binary, or any other
plugins. It is already defined by cmd/go, and used by cmd/link when
filling out the "" package placeholder in symbols.

With this CL, the plugin-specific name for main is also passed to
cmd/compile's -p flag. This is used to fill out the pkgpath field
of types, and ensures that two types defined in two different plugin
mains with the same name will not be mistaken for one another at
runtime.

Fixes #21386

Change-Id: I8a646d8d7451caff533fe0007343ea8b8e1704ed
Reviewed-on: https://go-review.googlesource.com/60910
Run-TryBot: David Crawshaw <crawshaw@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: 's avatarIan Lance Taylor <iant@golang.org>
parent 9690d245
......@@ -126,10 +126,12 @@ func main() {
log.Fatalf(`plugin1.F()=%d, want 17`, gotf)
}
// plugin2 has no exported symbols, only an init function.
if _, err := plugin.Open("plugin2.so"); err != nil {
p2, err := plugin.Open("plugin2.so")
if err != nil {
log.Fatalf("plugin.Open failed: %v", err)
}
// Check that plugin2's init function was called, and
// that it modifies the same global variable as the host.
if got, want := common.X, 2; got != want {
log.Fatalf("after loading plugin2, common.X=%d, want %d", got, want)
}
......@@ -142,6 +144,15 @@ func main() {
log.Fatalf(`plugin.Open("plugin-mismatch.so"): error does not mention "different version": %v`, s)
}
// Test that unexported types with the same names in
// different plugins do not interfere with each other.
//
// See Issue #21386.
UnexportedNameReuse, _ := p.Lookup("UnexportedNameReuse")
UnexportedNameReuse.(func())()
UnexportedNameReuse, _ = p2.Lookup("UnexportedNameReuse")
UnexportedNameReuse.(func())()
testUnnamed()
fmt.Println("PASS")
......
......@@ -7,7 +7,10 @@ package main
// // No C code required.
import "C"
import "common"
import (
"common"
"reflect"
)
func F() int {
_ = make([]byte, 1<<21) // trigger stack unwind, Issue #18190.
......@@ -33,6 +36,21 @@ func init() {
call(g)
}
type sameNameReusedInPlugins struct {
X string
}
type sameNameHolder struct {
F *sameNameReusedInPlugins
}
func UnexportedNameReuse() {
h := sameNameHolder{}
v := reflect.ValueOf(&h).Elem().Field(0)
newval := reflect.New(v.Type().Elem())
v.Set(newval)
}
func main() {
panic("plugin1.main called")
}
......@@ -13,6 +13,7 @@ import "C"
import (
"common"
"reflect"
"strings"
)
......@@ -22,6 +23,21 @@ func init() {
common.X = 2
}
type sameNameReusedInPlugins struct {
X string
}
type sameNameHolder struct {
F *sameNameReusedInPlugins
}
func UnexportedNameReuse() {
h := sameNameHolder{}
v := reflect.ValueOf(&h).Elem().Field(0)
newval := reflect.New(v.Type().Elem())
v.Set(newval)
}
func main() {
panic("plugin1.main called")
}
......@@ -2198,10 +2198,15 @@ func (gcToolchain) gc(b *Builder, p *load.Package, archive, objdir string, asmhd
ofile = objdir + out
}
gcargs := []string{"-p", p.ImportPath}
if p.Name == "main" {
gcargs[1] = "main"
pkgpath := p.ImportPath
if cfg.BuildBuildmode == "plugin" {
if pkgpath == "command-line-arguments" {
pkgpath = "plugin/unnamed-" + p.Internal.BuildID
}
} else if p.Name == "main" {
pkgpath = "main"
}
gcargs := []string{"-p", pkgpath}
if p.Standard {
gcargs = append(gcargs, "-std")
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment