bytecodealliance / lucet

Lucet, the Sandboxing WebAssembly Compiler.
Apache License 2.0
4.06k stars 164 forks source link

Unable to get hostcall examples working: lucetc errors with both Rust and AssemblyScript version #594

Closed jsoverson closed 3 years ago

jsoverson commented 3 years ago

I'm trying to work through the documentation, but I can't get the hostcall examples to work from the lucet-runtime docs.

lucetc errors out on both AssemblyScript and Rust versions:

$ lucetc rust_test.wasm --output rust_test.so  --bindings ../lucet/lucet-wasi/bindings.json
Error: Lucet Module: Unknown module for symbol `env::hostcall_factorials
$ lucetc asc/build/optimized.wasm --output subproject.so  --bindings ../lucet/lucet-wasi/bindings.json
Error: Lucet Module: Unknown module for symbol `index::hostcall_factorials

Rust code that builds to the above wasm:

// factorials_guest.rs
extern "C" {
  fn hostcall_factorials(n: u64) -> u64;
}

#[no_mangle]
pub extern "C" fn run() -> u64 {
  unsafe { hostcall_factorials(5) }
}

AssemblyScript code that builds the wasm above:

import "wasi"; // to prevent Error: Unknown module for symbol `env::abort`

@external("hostcall_factorials")
declare function hostcall_factorials(val: u64): u64;

export function run(): u64 {
  return hostcall_factorials(5);
}

This is probably my fault, but I'd like to contribute back to documentation or examples once I get passed the problems.

jedisct1 commented 3 years ago

Hi Jarrod,

lucetc rust_test.wasm --output rust_test.so --bindings ../lucet/lucet-wasi/bindings.json

lucetc needs to know the set of allowed hostcalls.

This is the purpose of the bindings.json file for WASI. If you implement your own hostcalls, you need to provide a JSON file similar to that one, mapping imported (as seen by the WebAssembly module) names to names provided by the runtime.

The --bindings option can be present multiple times in the command line.