I'm implementing GuestMemory
for a memory type in my Wasm interpreter, to be able to use wasi-common
. Can I assume that whenever wasi-common
writes to the memory it first calls mut_borrow
, or does it only do that if for some reason it's going to store a memory region somewhere to be used later, or something like that? Would be good to know in what cases mut_borrow
will be called by wasi-common
.
Hi. Yes that’s the idea behind the runtime borrow checker. It will call the borrow functions at the start of (possible) access and the unborrow functions at the end.
The runtime borrow checking is something the wiggle crate abstracts away. Wiggle-generated code will call the is_borrowed methods when copying items out of guest memory (to ensure the copy operation is safe) and the borrow methods get called at creation and drop of the GuestSlice / GuestSliceMut / GuestStr / GuestStrMut structs
In general I suggest that you use the wiggle-borrow crates implementation of the borrow checker and wire that into your guest memory impl.
It’s probably not the most efficient implementation possible but so far we haven’t found that runtime borrow checking is a performance bottleneck. If you do find ways to improve the wiggle-borrow crate we are happy to accept them
Btw, I’m most of the way through a rewrite of wasi-common, it’s in a draft PR. So, expect the way you build a wasi common context to change, and other interfaces to it as well.
I’m on vacation this week but sometimes I check zulip. I haven’t been watching GitHub. Will be back next week.
Thanks @Pat Hickey for the details . I'm already using wiggle-borrow.
Last updated: Dec 23 2024 at 13:07 UTC