From 4c4c5fc7a3248c063b279ffb3a28a12f0c6de04b Mon Sep 17 00:00:00 2001 From: Keith Randall Date: Wed, 11 Jan 2017 15:08:08 -0800 Subject: [PATCH] misc/cgo/testplugin: test that types and itabs are unique Make sure that the same type and itab generated in two different plugins are actually the same thing. See also CL 35115 Change-Id: I0c1ecb039d7e2bf5a601d58dfa162a435ae4ef76 Reviewed-on: https://go-review.googlesource.com/35116 Run-TryBot: Keith Randall TryBot-Result: Gobot Gobot Reviewed-by: David Crawshaw --- misc/cgo/testplugin/src/iface/main.go | 46 +++++++++++++++++++++++++++ misc/cgo/testplugin/src/iface_a/a.go | 17 ++++++++++ misc/cgo/testplugin/src/iface_b/b.go | 17 ++++++++++ misc/cgo/testplugin/src/iface_i/i.go | 17 ++++++++++ misc/cgo/testplugin/test.bash | 10 ++++-- 5 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 misc/cgo/testplugin/src/iface/main.go create mode 100644 misc/cgo/testplugin/src/iface_a/a.go create mode 100644 misc/cgo/testplugin/src/iface_b/b.go create mode 100644 misc/cgo/testplugin/src/iface_i/i.go diff --git a/misc/cgo/testplugin/src/iface/main.go b/misc/cgo/testplugin/src/iface/main.go new file mode 100644 index 0000000000..5e7e4d8b48 --- /dev/null +++ b/misc/cgo/testplugin/src/iface/main.go @@ -0,0 +1,46 @@ +// Copyright 2017 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 main + +import ( + "iface_i" + "log" + "plugin" +) + +func main() { + a, err := plugin.Open("iface_a.so") + if err != nil { + log.Fatalf(`plugin.Open("iface_a.so"): %v`, err) + } + b, err := plugin.Open("iface_b.so") + if err != nil { + log.Fatalf(`plugin.Open("iface_b.so"): %v`, err) + } + + af, err := a.Lookup("F") + if err != nil { + log.Fatalf(`a.Lookup("F") failed: %v`, err) + } + bf, err := b.Lookup("F") + if err != nil { + log.Fatalf(`b.Lookup("F") failed: %v`, err) + } + if af.(func() interface{})() != bf.(func() interface{})() { + panic("empty interfaces not equal") + } + + ag, err := a.Lookup("G") + if err != nil { + log.Fatalf(`a.Lookup("G") failed: %v`, err) + } + bg, err := b.Lookup("G") + if err != nil { + log.Fatalf(`b.Lookup("G") failed: %v`, err) + } + if ag.(func() iface_i.I)() != bg.(func() iface_i.I)() { + panic("nonempty interfaces not equal") + } +} diff --git a/misc/cgo/testplugin/src/iface_a/a.go b/misc/cgo/testplugin/src/iface_a/a.go new file mode 100644 index 0000000000..29d2e27764 --- /dev/null +++ b/misc/cgo/testplugin/src/iface_a/a.go @@ -0,0 +1,17 @@ +// Copyright 2017 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 main + +import "iface_i" + +//go:noinline +func F() interface{} { + return (*iface_i.T)(nil) +} + +//go:noinline +func G() iface_i.I { + return (*iface_i.T)(nil) +} diff --git a/misc/cgo/testplugin/src/iface_b/b.go b/misc/cgo/testplugin/src/iface_b/b.go new file mode 100644 index 0000000000..29d2e27764 --- /dev/null +++ b/misc/cgo/testplugin/src/iface_b/b.go @@ -0,0 +1,17 @@ +// Copyright 2017 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 main + +import "iface_i" + +//go:noinline +func F() interface{} { + return (*iface_i.T)(nil) +} + +//go:noinline +func G() iface_i.I { + return (*iface_i.T)(nil) +} diff --git a/misc/cgo/testplugin/src/iface_i/i.go b/misc/cgo/testplugin/src/iface_i/i.go new file mode 100644 index 0000000000..31c80387c7 --- /dev/null +++ b/misc/cgo/testplugin/src/iface_i/i.go @@ -0,0 +1,17 @@ +// Copyright 2017 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 iface_i + +type I interface { + M() +} + +type T struct { +} + +func (t *T) M() { +} + +// *T implements I diff --git a/misc/cgo/testplugin/test.bash b/misc/cgo/testplugin/test.bash index fee99a758c..584b83c744 100755 --- a/misc/cgo/testplugin/test.bash +++ b/misc/cgo/testplugin/test.bash @@ -15,8 +15,8 @@ goos=$(go env GOOS) goarch=$(go env GOARCH) function cleanup() { - rm -f plugin*.so unnamed*.so - rm -rf host pkg sub + rm -f plugin*.so unnamed*.so iface*.so + rm -rf host pkg sub iface } trap cleanup EXIT @@ -32,3 +32,9 @@ GOPATH=$(pwd) go build -buildmode=plugin unnamed2.go GOPATH=$(pwd) go build host LD_LIBRARY_PATH=$(pwd) ./host + +# Test that types and itabs get properly uniqified. +GOPATH=$(pwd) go build -buildmode=plugin iface_a +GOPATH=$(pwd) go build -buildmode=plugin iface_b +GOPATH=$(pwd) go build iface +LD_LIBRARY_PATH=$(pwd) ./iface