Host Functions
A host function is a Java function that a Wasm module imports and calls, and it is the way a module reaches anything outside its sandbox.
Host functions cross the sandbox boundary: validate every argument that comes from Wasm, especially memory offsets and lengths, before touching host resources. See Security Best Practices.
In Wasm terms the module instance is the guest and the surrounding runtime, your Java application, is the host. A module can export functions for the host to call, and import functions that the host must provide when the module is instantiated. Without imports a module is pure compute: it cannot perform I/O or interact with other modules.
Writing a host function
Download the following module, which imports a console.log function:
curl https://raw.githubusercontent.com/bytecodealliance/endive/main/wasm-corpus/src/main/resources/compiled/host-function.wat.wasm > logger.wasm
The module expects an import named console.log that prints a message, written as a host function like this:
import run.endive.runtime.Instance;
import run.endive.runtime.HostFunction;
import run.endive.wasm.types.ValType;
import run.endive.wasm.types.FunctionType;
var func = new HostFunction(
"console",
"log",
FunctionType.of(
List.of(ValType.I32, ValType.I32),
List.of()
),
(Instance instance, long... args) -> {
var len = (int) args[0];
var offset = (int) args[1];
var message = instance.memory().readString(offset, len);
System.out.println(message);
return null;
});
The module calls console.log with the length of the string and an index (offset) in its memory.
This is essentially a pointer into the Wasm's linear memory.
The Instance class provides a reference to a Memory object instance, that allows
to pull a value out of memory. You can use the readString() convenience method
to read a buffer into a String; we can then print that to stdout on behalf of our Wasm program.
Note that the HostFunction needs 3 things:
- The namespace and function name of the import (in our case it's
consoleandlogrespectively) - The Wasm type signature (this function takes two
i32s as arguments and returns nothing) - A lambda to call when the Wasm module invokes the import
We now need to pass this host function when we instantiate the module.
We can do so by using a Store:
import run.endive.wasm.Parser;
import run.endive.runtime.Store;
// instantiate the store
var store = new Store();
// registers `console.log` in the store
store.addFunction(func);
var instance = store.instantiate("logger", Parser.parse(new File("./logger.wasm")));
var logIt = instance.export("logIt");
logIt.apply();
// should print "Hello, World!" 10 times
For an easier way to write host function and interact with a Wasm module, see Annotations.