golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
124.08k stars 17.68k forks source link

plugin: plugin.Open() segfault with statically linked binaries #21822

Open d1vyank opened 7 years ago

d1vyank commented 7 years ago

What version of Go are you using (go version)? go version go1.9 linux/amd64

Does this issue reproduce with the latest release? Yes

What operating system and processor architecture are you using (go env)? GOARCH="amd64" GOBIN="" GOEXE="" GOHOSTARCH="amd64" GOHOSTOS="linux" GOOS="linux" GOPATH="/opt/gopath" GORACE="" GOROOT="/opt/go" GOTOOLDIR="/opt/go/pkg/tool/linux_amd64" GCCGO="gccgo" CC="gcc" GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build683959130=/tmp/go-build -gno-record-gcc-switches" CXX="g++" CGO_ENABLED="1" CGO_CFLAGS="-g -O2" CGO_CPPFLAGS="" CGO_CXXFLAGS="-g -O2" CGO_FFLAGS="-g -O2" CGO_LDFLAGS="-g -O2" PKG_CONFIG="pkg-config"

What did you do? Compiled the simple plugin example described in the plugin docs with the -static flag go run -ldflags '-extldflags "-static"' main.go

What did you expect to see? The same output as building without the -static flag: go run main.go Hello, number 7

What did you see instead? Segfault in cgo runtime:

go run -ldflags '-extldflags "-static"' main.go # command-line-arguments /tmp/go-link-658275617/000000.o: In function `pluginOpen': /tmp/workdir/go/src/plugin/plugin_dlopen.go:19: warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking fatal error: unexpected signal during runtime execution [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x0]

runtime stack: runtime.throw(0x52c324, 0x2a) /opt/go/src/runtime/panic.go:605 +0x95 runtime.sigpanic() /opt/go/src/runtime/signal_unix.go:351 +0x2b8

goroutine 1 [syscall, locked to thread]: runtime.cgocall(0x459260, 0xc420053c18, 0xc42000e010) /opt/go/src/runtime/cgocall.go:132 +0xe4 fp=0xc420053be8 sp=0xc420053ba8 pc=0x401f84 plugin._Cfunc_pluginOpen(0x25cdfe0, 0xc42000e010, 0x0) plugin/_obj/_cgo_gotypes.go:98 +0x4e fp=0xc420053c18 sp=0xc420053be8 pc=0x45758e plugin.open.func3(0x25cdfe0, 0xc42000e010, 0xc42001c0a0) /tmp/workdir/go/src/plugin/plugin_dlopen.go:102 +0xae fp=0xc420053c58 sp=0xc420053c18 pc=0x4589ae plugin.open(0x52ce1a, 0x4c, 0x0, 0x0, 0x0) /tmp/workdir/go/src/plugin/plugin_dlopen.go:102 +0x1d1 fp=0xc420053e88 sp=0xc420053c58 pc=0x457c31 plugin.Open(0x52ce1a, 0x4c, 0xc42007a000, 0xe, 0xc420012000) /opt/go/src/plugin/plugin.go:31 +0x35 fp=0xc420053ec0 sp=0xc420053e88 pc=0x4570c5 main.loadPlugins()

ianlancetaylor commented 7 years ago

I would not count on this ever working. If someone wants to figure out how to fix it, great, but otherwise it seems both difficult and low priority.

ianlancetaylor commented 7 years ago

CC @crawshaw

crawshaw commented 7 years ago

As Ian says, difficult and low priority. Unless an intrepid sort with the need and tenacity shows up to implement this, it won't happen.

I suppose we could produce a link-time error easily enough if both the -static flag and the plugin.Open symbol are present. The current run time error seems quite clear though.

wgerlach commented 4 years ago

It would be nice if the documentation https://golang.org/pkg/plugin/ were more clear about the limitations. Also see issues raised in this thread: https://www.reddit.com/r/golang/comments/b6h8qq/is_anyone_actually_using_go_plugins/ejkxd2k/