Skip to content

ext/wasm: Check exit code on returned error #2223

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

Merged
merged 1 commit into from
Apr 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 33 additions & 11 deletions internal/ext/wasm/wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,32 +258,54 @@ func (r *Runner) Generate(ctx context.Context, req *plugin.CodeGenRequest) (*plu
store := wasmtime.NewStore(engine)
store.SetWasi(wasiConfig)

linkRegion := trace.StartRegion(ctx, "linker.Instantiate")
instance, err := linker.Instantiate(store, module)
linkRegion := trace.StartRegion(ctx, "linker.DefineModule")
err = linker.DefineModule(store, "", module)
linkRegion.End()
if err != nil {
return nil, fmt.Errorf("define wasi: %w", err)
}

// Run the function
fn, err := linker.GetDefault(store, "")
if err != nil {
return nil, fmt.Errorf("wasi: get default: %w", err)
}

callRegion := trace.StartRegion(ctx, "call _start")
nom := instance.GetExport(store, "_start").Func()
_, err = nom.Call(store)
_, err = fn.Call(store)
callRegion.End()
if err != nil {
// Print WASM stdout
stderrBlob, err := os.ReadFile(stderrPath)
if err == nil && len(stderrBlob) > 0 {
return nil, errors.New(string(stderrBlob))
}
return nil, fmt.Errorf("call: %w", err)

if cerr := checkError(err, stderrPath); cerr != nil {
return nil, cerr
}

// Print WASM stdout
stdoutBlob, err := os.ReadFile(stdoutPath)
if err != nil {
return nil, fmt.Errorf("read file: %w", err)
}

var resp plugin.CodeGenResponse
return &resp, resp.UnmarshalVT(stdoutBlob)
}

func checkError(err error, stderrPath string) error {
if err == nil {
return err
}

var wtError *wasmtime.Error
if errors.As(err, &wtError) {
if code, ok := wtError.ExitStatus(); ok {
if code == 0 {
return nil
}
}
}
// Print WASM stdout
stderrBlob, rferr := os.ReadFile(stderrPath)
if rferr == nil && len(stderrBlob) > 0 {
return errors.New(string(stderrBlob))
}
return fmt.Errorf("call: %w", err)
}