Module ztcl::wasm::host
ztcl::wasm::host gives a WebAssembly module of ztcl::wasm a family of imports for a feature of ztcl. A program in Rust, Go, C, or another language that compiles to wasm calls the feature through these imports. A family has one wasm module name, such as vm. The first family is vm: the module starts Linux guests of ztcl::vm, runs programs in them, and reads their output.
The imports exist only in the engine of ztcl. A browser or another WebAssembly runtime does not have them.
The grant
A module gets a family only when the host script gives it on purpose, with the option -grant of ztcl::wasm::host instantiate. With no grant, the imports do not exist, and the instantiation of a module that imports them fails with missing import "vm" "last_error". A grant of a family needs the capability of its feature: the family vm needs the capability vm. So a module never gets more than its host.
package require ztcl::wasm::host
set w [ztcl::wasm::host instantiate [ztcl::wasm load app.wasm] \
-grant {vm {-fixed {-image ztcl-guest} -options {-memory -cpus} -guests 1 -timeout 10000}}]
ztcl::wasm call $w run
ztcl::wasm::host delete $w
| Command | What it does |
|---|---|
ztcl::wasm::host instantiate module ?-grant dict? ?option value ...? |
ztcl::wasm instantiate with the imports of each family of -grant, a dict of the family names and their options. The other options go to ztcl::wasm instantiate. A family name in -imports too is an error. Gives the instance handle. |
ztcl::wasm::host delete instance ?instance ...? |
Frees what each family made for the instance (each guest of vm), then deletes the instance. |
ztcl::wasm::host families |
The names of the families of the build. |
ztcl::wasm delete of the instance keeps the guests of the module until the end of the interpreter. Use ztcl::wasm::host delete.
The rules of each family
The same rules apply to each family, so a binding for one family of a language is a model for the next.
- Each import gives an
i32. A value of 0 or more is a result. A negative value is an error code, and the importlast_error(buf, cap)of the family gives the text of the last error. - A handle is a positive
i32from the host, for one instance. The host never gives the same number again in that instance. A wrong or deleted handle gives the codehandle. - Data is a byte range of the linear memory of the module: a pointer and a length, both unsigned. Each call checks each range against the size of the memory at the time of the call, so a memory that grows between two calls is no problem. A range outside the memory gives the code
memory, and a length above the limit of the grant givestoo-large. - A result of bytes goes to a buffer of the module: a pointer and a capacity. The host writes at most the capacity and gives the full length. The module calls again with a larger buffer when the full length is more than the capacity.
- A list of words is a byte range. For each word: its byte length as a 32-bit little-endian number, then its UTF-8 bytes. No word goes through the Tcl parser.
- Each import is a Tcl proc, so each call uses compute units. A module that calls imports in a loop stops at the compute limit, as a Tcl loop does.
ztcl::wasm call -limitcaps one call.
| Code | Name | Meaning |
|---|---|---|
| -1 | failed |
An error of the feature. last_error has its text. |
| -2 | handle |
A wrong or deleted handle. |
| -3 | memory |
A byte range outside the memory of the module. |
| -4 | too-large |
A length above the limit of the grant. |
| -5 | timeout |
The feature gave no answer in time. |
| -6 | unavailable |
The host has no backend for the feature. |
| -7 | limit |
A limit of the grant or of the interpreter. |
| -8 | denied |
An option or a path that the grant does not permit. |
| -9 | args |
A malformed argument. |
| -10 | state |
The object is in the wrong state, or has no result. |
The family vm
wit/host/vm.wit defines the interface. Each function is one core import of the module vm.
| Import | Core type | What it does |
|---|---|---|
last_error(buf, cap) |
(i32 i32) -> i32 |
Writes the text of the last error. Gives its full byte length. |
create(opts, len) |
(i32 i32) -> i32 |
ztcl::vm create with the -fixed options of the grant and the pairs of this list of words, such as -memory 64. Gives a guest handle. |
start(g) |
(i32) -> i32 |
ztcl::vm start. |
exec(g, argv, argv_len, stdin, stdin_len, timeout_ms) |
(i32 i32 i32 i32 i32 i32) -> i32 |
Runs the list of words argv in the guest and waits for its end. The program reads the bytes of stdin, then the end of its input. Gives 0. |
exit_code(g) |
(i32) -> i32 |
The exit code of the last exec: 0 to 255, or 256 plus the number of the signal that ended the program. |
output(g, stream, buf, cap) |
(i32 i32 i32 i32) -> i32 |
The bytes of the last exec: stream 1 is stdout and 2 is stderr, each up to 64 MiB. Gives the full length. |
status(g) |
(i32) -> i32 |
The state: 0 created, 1 starting, 2 running, 3 pausing, 4 paused, 5 stopping, 6 stopped, 7 error. |
stop(g, force) |
(i32 i32) -> i32 |
ztcl::vm stop, with -force when force is not 0. |
delete(g) |
(i32) -> i32 |
ztcl::vm delete. Frees the handle. |
snapshot(g, name, len) |
(i32 i32 i32) -> i32 |
ztcl::vm snapshot to the file name of the folder of -snapshots. Linux with KVM only. |
restore(name, len) |
(i32 i32) -> i32 |
ztcl::vm create -snapshot of the file name of that folder. Gives a guest handle. |
The options of the grant of vm:
| Option | Value | Default |
|---|---|---|
-options LIST |
The options of ztcl::vm create that the module can give. A path option such as -kernel, -disk, or -share gives the module a choice of files of the host: list it only for a module that you trust with those files. |
-image -cpus -memory -cmdline -entropy -balloon -warm |
-fixed LIST |
Options and values of create that the host gives to each guest. The module cannot give one of them. |
none |
-guests N |
The most guests of the module at one time. | 4 |
-timeout MS |
The time limit of exec, and the value for an exec with timeout_ms 0. A larger timeout_ms gives this limit. |
30,000 |
-snapshots DIR |
The folder of snapshot and restore. A name is letters, digits, ., _, and -, so it stays in the folder. With no folder, the two give denied. |
none |
-max-bytes N |
The most bytes of stdin and of a list of words. |
67,108,864 |
The imports call ztcl::vm in the interpreter of the host. So a guest of a module counts toward the limits of Interp.vm_host (the count of guests, the CPUs, and the memory) with the guests of the host script, and a path of -fixed follows the root folder of file. An error code of ztcl::vm gives a code of the family: ZTCL VM UNAVAILABLE gives unavailable, ZTCL VM LIMIT gives limit, ZTCL VM TIMEOUT gives timeout, and ZTCL VM STATE gives state. On a host with no hypervisor for ztcl::vm, create gives unavailable, and last_error has the text of ztcl::vm.
A wait in exec
exec blocks the interpreter until the agent of the guest answers. While it waits, the event loop of the host does not run, and the call uses no compute units, so the compute limit cannot end the wait. The time limit is the -timeout of ztcl::vm exec: the most milliseconds of a wait with no output from the guest. Past it, the call gives timeout, and the agent ends the program. A program that writes output more often than the time limit keeps the call alive until it ends. For a time limit on the whole run, run the program under a timer of the guest, such as /bin/busybox timeout 10 PROGRAM.
This version of the family has no streamed input and output of a long program, and no copy of files into the guest. A module copies a file with exec of /bin/busybox sh -c {/bin/busybox cat > FILE} and the bytes as stdin.
The examples
examples/wasm-vm has a module in Rust and a module in Go that create a guest, run a command in it, print its output, and delete it. host.tcl gives each module the family vm and one more import, env.print, for its output. The Rust module builds for wasm32-unknown-unknown, and the Go module builds with TinyGo for wasm-unknown. Neither needs WASI.
cd examples/wasm-vm/rust && cargo build --release --target wasm32-unknown-unknown
cd examples/wasm-vm/go && tinygo build -target=wasm-unknown -opt=z -no-debug -o vm-go.wasm .
ztcl --allow vm,file,channels --tclsh examples/wasm-vm/host.tcl vm-go.wasm GUESTDIR
exit 0
Linux x86_64
HELLO FROM GO
GUESTDIR has bzImage and agent.cpio of tools/vm-guest. Without GUESTDIR, the guest boots the image ztcl-guest of the store. The standard Go toolchain (GOOS=wasip1 GOARCH=wasm) also makes modules that the engine runs, with WASI and the capabilities clock and random.