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: Plugin variable pointer being set to nil #21066

Closed
patrobinson opened this issue Jul 18, 2017 · 2 comments
Closed

plugin: Plugin variable pointer being set to nil #21066

patrobinson opened this issue Jul 18, 2017 · 2 comments

Comments

@patrobinson
Copy link

patrobinson commented Jul 18, 2017

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

go version go1.9beta2 linux/amd64

I've also tried 1.8.3

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

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build756041191=/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?

a.go

package main

import (
	"fmt"
)

type aPlugin struct {
	str *string
}

func (r aPlugin) Init() {
	str := "a"
	r.str = &str
	fmt.Printf("String pointer is %v\n", r.str)
}

func (r aPlugin) String() string {
	fmt.Printf("String pointer is %v\n", r.str)
	return fmt.Sprintf("String: %v", *r.str)
}

var Plugin aPlugin

main.go

package main

import "plugin"
import "fmt"

type P interface {
	Init()
	String() string
}

func main() {
	aPlug, _ := plugin.Open("a.so")
	aSymPlug, _ := aPlug.Lookup("Plugin")
	plug, ok := aSymPlug.(P)
	if !ok {
		fmt.Print("Bad plugin\n")
	}
	plug.Init()
	plug.String()
}
go build -buildmode=plugin -o a.so a.go
go build

What did you expect to see?

# ./go-plugin-bug
String pointer is 0xc420010220
String pointer is 0xc420010220

What did you see instead?

# ./go-plugin-bug
String pointer is 0xc420010220
String pointer is <nil>
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x7f4fe19fc3f7]

goroutine 1 [running]:
plugin/unnamed-43d067c47d00006d749146ae708f2304df4b538b.aPlugin.String(0x0, 0x7f4fe19fc53e, 0x0)
	/go/src/github.com/patrobinson/go-plugin-bug/a.go:19 +0x97
plugin/unnamed-43d067c47d00006d749146ae708f2304df4b538b.(*aPlugin).String(0x7f4fe1cd29b8, 0x7f4fe1c43500, 0x7f4fe1cd29b8)
	<autogenerated>:1 +0x3e
main.main()
	/go/src/github.com/patrobinson/go-plugin-bug/main.go:19 +0xf2
@randall77
Copy link
Contributor

This is expected. Your Init method takes its receiver by value, not by pointer. So any modifications it makes to its receiver are lost when the function returns.

@patrobinson
Copy link
Author

🤦‍♂️

@golang golang locked and limited conversation to collaborators Jul 18, 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

3 participants