Disallow direct call of host functions (#723)

Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
This commit is contained in:
Takeshi Yoneda
2022-07-30 10:33:20 +09:00
committed by GitHub
parent ce2f447555
commit 02c23d55db
33 changed files with 808 additions and 602 deletions

View File

@@ -9,7 +9,6 @@ import (
"strconv"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/api"
)
// addWasm was generated by the following:
@@ -34,28 +33,17 @@ func main() {
log.Panicln(err)
}
// Add a module to the runtime named "host/math" which exports one function "add", implemented in Go.
host, err := r.NewModuleBuilder("host/math").
ExportFunction("add", func(v1, v2 uint32) uint32 {
return v1 + v2
}).Instantiate(ctx, r)
// Read two args to add.
x, y := readTwoArgs()
// Call the `add` function in the module and print the results to the console.
add := wasm.ExportedFunction("add")
results, err := add.Call(ctx, x, y)
if err != nil {
log.Panicln(err)
}
// Read two args to add.
x, y := readTwoArgs()
// Call the same function in both modules and print the results to the console.
for _, mod := range []api.Module{wasm, host} {
add := mod.ExportedFunction("add")
results, err := add.Call(ctx, x, y)
if err != nil {
log.Panicln(err)
}
fmt.Printf("%s: %d + %d = %d\n", mod.Name(), x, y, results[0])
}
fmt.Printf("%s: %d + %d = %d\n", wasm.Name(), x, y, results[0])
}
func readTwoArgs() (uint64, uint64) {

View File

@@ -13,6 +13,5 @@ import (
func Test_main(t *testing.T) {
stdout, _ := maintester.TestMain(t, main, "add", "7", "9")
require.Equal(t, `wasm/math: 7 + 9 = 16
host/math: 7 + 9 = 16
`, stdout)
}