bytecodealliance / lucet

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

Calling guest function via get_func_from_idx() can trigger "Invalid Hostcall" #606

Closed dshiell15 closed 3 years ago

dshiell15 commented 3 years ago

Apologies if I am missing something obvious here. I am working on embedding Lucet in a runtime with hostcalls which require copying host information back into the guest. To this end I've attempted to follow the memory management design outlined in the Fastly blog here: https://www.fastly.com/blog/webassembly-memory-management-guide-for-c-rust-programmers.

What I'm finding is that I am triggering this panic message: "Invalid state: Instance marked as in a hostcall while entering a hostcall." because when I call the registered guest memory allocation function (via the function table idx) it in turn is triggering a hostcall to grow the wasm memory (lucet_vmctx_grow_memory).

Any suggestions or pointers to workarounds would be appreciated. Thank you!

P.S. - I am using Lucet v0.6.1

acfoltzer commented 3 years ago

@dshiell15 we don't currently recommend calling back into Wasm from hostcalls. The memory management strategy I'd recommend is to have the guest provide the host with a pointer to a pre-allocated buffer that the host can copy results into. This of course means having to get the buffer size right, but we have had good results with the following approach:

  1. Return a status/error code from the hostcall so that you can identify when the write succeeds or fails.
  2. Use an nwritten output parameter in the hostcall so the host can return to the guest how many bytes were written by a successful copy.
  3. When the hostcall fails because the guest-provided buffer is not large enough, write the number of bytes that are necessary to an output parameter (you can reuse nwritten as the guest checks for the error case).
  4. Allocate a new buffer of the required size in the guest, and call the hostcall again.
dshiell15 commented 3 years ago

Excellent, thank you! - got a little too hung up on the other approach...