Skip to content

Commit

Permalink
fix: copy buffers read from wasm memory (#22)
Browse files Browse the repository at this point in the history
* fix: copy buffers read from wasm memory

* test: call plugin twice in var test
  • Loading branch information
mhmd-azeez authored Sep 21, 2023
1 parent 7f516b1 commit 9dae66a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
15 changes: 12 additions & 3 deletions extism_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,15 @@ func TestVar(t *testing.T) {

assert.Equal(t, expected, actual)
}

exit, _, err = plugin.Call("run_test", []byte{})

if assertCall(t, err, exit) {
actual := uintFromLEBytes(plugin.Var["a"])
expected := uint(40)

assert.Equal(t, expected, actual)
}
}

}
Expand Down Expand Up @@ -619,14 +628,14 @@ func TestMultipleCallsOutput(t *testing.T) {
return
}

exit, output2, err := plugin.Call("count_vowels", []byte("bbb"))
exit, output2, err := plugin.Call("count_vowels", []byte("bbba"))

if !assertCall(t, err, exit) {
return
}

assert.Equal(t, `{"count": 3}`, string(output1))
assert.Equal(t, `{"count": 0}`, string(output2))
assert.Equal(t, `{"count":3,"total":3,"vowels":"aeiouAEIOU"}`, string(output1))
assert.Equal(t, `{"count":1,"total":4,"vowels":"aeiouAEIOU"}`, string(output2))
}
}

Expand Down
5 changes: 4 additions & 1 deletion host.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,10 @@ func (p *CurrentPlugin) ReadBytes(offset uint64) ([]byte, error) {
return []byte{}, fmt.Errorf("Invalid memory block")
}

return buffer, nil
cpy := make([]byte, len(buffer))
copy(cpy, buffer)

return cpy, nil
}

func buildHostModule(ctx context.Context, rt wazero.Runtime, name string, funcs []HostFunction) (api.Module, error) {
Expand Down
Binary file modified wasm/count_vowels.wasm
Binary file not shown.

0 comments on commit 9dae66a

Please sign in to comment.