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

Can't run plugin compiled with standard go compiler #95

Open
mhmd-azeez opened this issue Feb 17, 2025 · 10 comments · May be fixed by #98
Open

Can't run plugin compiled with standard go compiler #95

mhmd-azeez opened this issue Feb 17, 2025 · 10 comments · May be fixed by #98
Assignees

Comments

@mhmd-azeez
Copy link
Collaborator

mhmd-azeez commented Feb 17, 2025

This issue was reported by a community member, here is a repro:

https://github.com/mhmd-azeez/extism_std_go_module_closed_repro

$ go run .
2025/02/17 10:34:01 No runtime detected
2025/02/17 10:34:01 Calling function : _start
module is closed

The same host, can run the example count_vowels in https://github.com/extism/plugins just fine

@mhmd-azeez mhmd-azeez self-assigned this Feb 17, 2025
@syke99
Copy link
Collaborator

syke99 commented Feb 22, 2025

extism cli version: 1.6.2

go version: 1.24.0

when compiling plugin:
GOOS=wasip1
GOARCH=wasm

i've also run into this whenever trying to call a function that i've exported myself (using //go:wasmexport). this is the error i receive in the console:

wasm stack trace:
        	            		.runtime.abort(i32) i32
        	            		.runtime.notInitialized()
        	            		.genTypesProto() // getTypesProto is the name of my exported function

and this is the error modal that pops up whenever trying to use the compiled module in the extism playground:

Image

in my case, it looks like it's failing specifically because of the sock_accept function import

@mhmd-azeez
Copy link
Collaborator Author

@syke99 can you please share a code snippet? are you trying to use http? I don't think the Extism Playground supports wasi functions

@syke99
Copy link
Collaborator

syke99 commented Feb 22, 2025

yeah!! please give me a few minutes as i'll have to grab a couple for you (plugin creation and use are in two separate areas of the codebase)

@syke99
Copy link
Collaborator

syke99 commented Feb 22, 2025

plugin creation:

//go:embed gen.wasm
var wasm []byte

func NewGenerator(islandsPath string, mapPath string, genFilePath string, oapiConfigPath string) (*extism.Plugin, error) {
	manifest := extism.Manifest{
		Wasm: []extism.Wasm{
			extism.WasmData{
				Data: wasm,
			},
		},
		Config: map[string]string{
			"islandsPath":    islandsPath,
			"mapPath":        mapPath,
			"oapiConfigPath": oapiConfigPath,
		},
		AllowedPaths: map[string]string{
			genFilePath: filepath.Join("oasis", "types"),
		},
	}

	ctx := context.Background()
	config := extism.PluginConfig{EnableWasi: true}

	plugin, err := extism.NewPlugin(ctx, manifest, config, []extism.HostFunction{})
	if err != nil {
		fmt.Printf("Failed to initialize plugin: %v\n", err)
		os.Exit(1)
	}

	return plugin, nil
}

plugin use:

	_, output, err := testGenerator.Call("genTypesProto", islandsBytes) // islandsBytes is just an arbitrary byte slice
	assert.NoError(t, err)

	assert.Equal(t, expected, string(output))

@mhmd-azeez
Copy link
Collaborator Author

@syke99 thanks! seeing the plugin code would aslo be really helpful!

@syke99
Copy link
Collaborator

syke99 commented Feb 22, 2025

yep! workin on it!

@syke99
Copy link
Collaborator

syke99 commented Feb 22, 2025

actual plugin code:

//go:wasmexport genTypesProto
func genTypesProto() {
	in := pdk.Input()

	islands := []*models.Isl[any, any]{}

	err := yaml.Unmarshal(in, &islands)
	if err != nil {
		pdk.SetError(err)
		return
	}

	islandPath, ok := pdk.GetConfig("islandPath")
	if !ok {
		pdk.SetError(errors.New("island path not found"))
		return
	}

	out, err := template(islands, islandPath) // just executes a template.Must() call
	if err != nil {
		pdk.SetError(err)
		return
	}

	pdk.OutputString(out)
}

@mhmd-azeez
Copy link
Collaborator Author

@syke99 can you please try building with -buildmode=c-shared, for example: GOOS=wasip1 GOARCH=wasm go build -buildmode=c-shared -tags std -o plugin.wasm .

@syke99
Copy link
Collaborator

syke99 commented Feb 27, 2025

yeah! i'll give it a shot later today and report back!

@syke99
Copy link
Collaborator

syke99 commented Feb 27, 2025

awesome! that was the fix! thanks @mhmd-azeez!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants