Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plugin: failing to import "C" in plugin produces unintuitive runtime panic #18648

Closed
ianlewis opened this issue Jan 13, 2017 · 4 comments
Closed

Comments

@ianlewis
Copy link

If I compile a Go plugin without importing "C" I can compile it but when plugin.Open() is called on it, the application crashes with "fatal error: invalid runtime symbol table"

What version of Go are you using (go version)?

go version go1.8rc1 linux/amd64

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/usr/local/google/home/ianlewis/opt/go-workspace"
GORACE=""
GOROOT="/usr/local/google/home/ianlewis/opt/go1.8rc1.linux-amd64"
GOTOOLDIR="/usr/local/google/home/ianlewis/opt/go1.8rc1.linux-amd64/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build476586089=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"

What did you do?

main.go

package main

import "plugin"

func main() {
	p, err := plugin.Open("plugin_name.so")
	if err != nil {
		panic(err)
	}
	v, err := p.Lookup("V")
	if err != nil {
		panic(err)
	}
	f, err := p.Lookup("F")
	if err != nil {
		panic(err)
	}
	*v.(*int) = 7
	f.(func())() // prints "Hello, number 7"
}

plugin.go

package main

// // No C code needed.
// import "C"

import "fmt"

var V int

func F() { fmt.Printf("Hello, number %d\n", V) }

I compile the plugin:
go build -buildmode=plugin -o plugin_name.so plugin.go

and run the program:
go run main.go

What did you expect to see?

I expect a more useful error message at plugin compile time.

What did you see instead?

The application then crashes with the following traceback.

runtime: invalid pc-encoded table f=plugin/unnamed-a8b5035e2bbc8d2b4d8ec27e3a58d96aa8d3bcf2.init pc=0x7f65c033ce29 targetpc=0x7f65c033ced0 tab=[0/0]0x0
	value=141 until pc=0x7f65c033ce29
fatal error: invalid runtime symbol table

goroutine 1 [running]:
runtime.throw(0x4b2525, 0x1c)
	/usr/local/google/home/ianlewis/opt/go1.8rc1.linux-amd64/src/runtime/panic.go:596 +0x95 fp=0xc42005d750 sp=0xc42005d730
runtime.pcvalue(0x7f65c05d3df8, 0x4f141, 0x7f65c033ced0, 0xc42005d9a8, 0xc42005db01, 0x58)
	/usr/local/google/home/ianlewis/opt/go1.8rc1.linux-amd64/src/runtime/symtab.go:565 +0x4e5 fp=0xc42005d900 sp=0xc42005d750
runtime.moduledataverify1(0x7f65c05db360)
	/usr/local/google/home/ianlewis/opt/go1.8rc1.linux-amd64/src/runtime/symtab.go:377 +0x42f fp=0xc42005db38 sp=0xc42005d900
plugin.lastmoduleinit(0xf9d400, 0xc42000e010, 0xf9e460, 0x42, 0x7050a0)
	/usr/local/google/home/ianlewis/opt/go1.8rc1.linux-amd64/src/runtime/plugin.go:55 +0x315 fp=0xc42005dc68 sp=0xc42005db38
plugin.open(0x4b0584, 0xe, 0x0, 0x0, 0x0)
	/usr/local/google/home/ianlewis/opt/go1.8rc1.linux-amd64/src/plugin/plugin_dlopen.go:72 +0x25d fp=0xc42005dea8 sp=0xc42005dc68
plugin.Open(0x4b0584, 0xe, 0xc42005df78, 0x44125c, 0xc42007e058)
	/usr/local/google/home/ianlewis/opt/go1.8rc1.linux-amd64/src/plugin/plugin.go:30 +0x35 fp=0xc42005dee0 sp=0xc42005dea8
main.main()
	/usr/local/google/home/ianlewis/src/plugin-test/tmp/main.go:6 +0x48 fp=0xc42005df88 sp=0xc42005dee0
runtime.main()
	/usr/local/google/home/ianlewis/opt/go1.8rc1.linux-amd64/src/runtime/proc.go:185 +0x20a fp=0xc42005dfe0 sp=0xc42005df88
runtime.goexit()
	/usr/local/google/home/ianlewis/opt/go1.8rc1.linux-amd64/src/runtime/asm_amd64.s:2197 +0x1 fp=0xc42005dfe8 sp=0xc42005dfe0

goroutine 17 [syscall, locked to thread]:
runtime.goexit()
	/usr/local/google/home/ianlewis/opt/go1.8rc1.linux-amd64/src/runtime/asm_amd64.s:2197 +0x1
exit status 2
@ianlewis ianlewis changed the title Go 1.8rc1 not importing "C" in plugin produces unintuitive error at runtime Go 1.8rc1 not importing "C" in plugin produces unintuitive panic at runtime Jan 13, 2017
@josharian josharian changed the title Go 1.8rc1 not importing "C" in plugin produces unintuitive panic at runtime plugin: import "C" in plugin produces unintuitive panic Jan 13, 2017
@josharian
Copy link
Contributor

cc @crawshaw

@ianlewis ianlewis changed the title plugin: import "C" in plugin produces unintuitive panic plugin: failing to import "C" in plugin produces unintuitive runtime panic Jan 13, 2017
@bradfitz bradfitz added this to the Go1.8Maybe milestone Jan 13, 2017
@bradfitz
Copy link
Contributor

And cc @ianlancetaylor

@crawshaw
Copy link
Member

Ah yes, this kept slipping my mind. I think it should be straightforward to add a runtime/cgo dependency for plugins in cmd/go. Let me give it a try...

@crawshaw
Copy link
Member

I believe cgo is a red herring.

It looks like cmd/go already has logic for adding a runtime/cgo dependency to all plugins, cmd/go/pkg.go:937. The error you are seeing here is the same one Keith ran into in CL 35116. It was fixed yesterday in CL 35190. Your example succeeds when built against HEAD.

@golang golang locked and limited conversation to collaborators Jan 13, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants