Skip to content
This repository has been archived by the owner on Mar 24, 2022. It is now read-only.

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

Closed
dshiell15 opened this issue Nov 16, 2020 · 2 comments

Comments

@dshiell15
Copy link

dshiell15 commented Nov 16, 2020

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
Copy link
Contributor

@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
Copy link
Author

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants