pchickey opened Issue #1917:
Issue filed based on discussion: https://github.com/bytecodealliance/wasmtime/pull/1910#discussion_r444963814
The
wiggle
crate relies on performing run-time borrow checking of accesses into a linear memory for safety. Currently, there is no mechanism in Wasmtime which can associate a borrow checker with a linear memory. Presently, we create a fresh borrow checker each time we enter host code, but this requires the host code to never re-enter the same WebAssembly instance, a property which we cannot enforce statically or dynamically.We need to design some mechanism by which the
wasmtime
crate can associate a borrow checker with a memory for the memory's entire lifetime. Wasmtime would then be responsible for checking that the borrow checker has no outstanding borrows whenever an instance which has access to that memory is called.Ideally, the wasmtime borrow checker would be represented as a trait object, so that we can keep the definition of the borrow checker in the wiggle crate (where it is also used in Lucet), and not require wasmtime users to depend on wiggle nor wiggle users to depend on wasmtime.
pchickey labeled Issue #1917:
Issue filed based on discussion: https://github.com/bytecodealliance/wasmtime/pull/1910#discussion_r444963814
The
wiggle
crate relies on performing run-time borrow checking of accesses into a linear memory for safety. Currently, there is no mechanism in Wasmtime which can associate a borrow checker with a linear memory. Presently, we create a fresh borrow checker each time we enter host code, but this requires the host code to never re-enter the same WebAssembly instance, a property which we cannot enforce statically or dynamically.We need to design some mechanism by which the
wasmtime
crate can associate a borrow checker with a memory for the memory's entire lifetime. Wasmtime would then be responsible for checking that the borrow checker has no outstanding borrows whenever an instance which has access to that memory is called.Ideally, the wasmtime borrow checker would be represented as a trait object, so that we can keep the definition of the borrow checker in the wiggle crate (where it is also used in Lucet), and not require wasmtime users to depend on wiggle nor wiggle users to depend on wasmtime.
alexcrichton commented on Issue #1917:
One thing we could do here is allow attaching a type map or a
Box<Any>
to an instance, but I think the best route here may actually be to move the borrow checker into wasmtime itself. It seems like trying to get a raw view into wasm memory is going to be a pretty common thing to do in Rust, so making this a safe method onMemory
would be pretty compelling. I'm not sure how to best work with the wasmtime/lucet split, though?
pchickey commented on Issue #1917:
My idea to manage the wasmtime/lucet split:
- change the
wiggle::GuestMemory
trait to have all ofBorrowChecker
's methods inline (has_outstanding_borrows
,borrow
,unborrow
,is_borrowed
) rather than aborrow_checker(&self) -> &BorrowChecker
method.- move the concrete
wiggle::BorrowChecker
type to be part of wasmtime. Impl theGuestMemory
trait inwasmtime-wiggle
with wasmtime'sBorrowChecker
andMemory
.- Lucet can make its own copy of
BorrowChecker
to keep inlucet-wiggle
or elsewhere.
pchickey edited a comment on Issue #1917:
My idea to manage the wasmtime/lucet split:
- change the
wiggle::GuestMemory
trait to have all ofBorrowChecker
's methods inline (has_outstanding_borrows
,borrow
,unborrow
,is_borrowed
) rather than aborrow_checker(&self) -> &BorrowChecker
method.- move the concrete
wiggle::BorrowChecker
type to be part of wasmtime. Impl theGuestMemory
trait inwasmtime-wiggle
with wasmtime'sBorrowChecker
andMemory
.- Lucet can make its own copy of
BorrowChecker
to keep inlucet-wiggle
or elsewhere, and i mpl theGuestMemory
trait in terms of that concrete type and ourVmctx
(we dont have aMemory
equivelant)
pchickey edited a comment on Issue #1917:
My idea to manage the wasmtime/lucet split:
- change the
wiggle::GuestMemory
trait to have all ofBorrowChecker
's methods inline (has_outstanding_borrows
,borrow
,unborrow
,is_borrowed
) rather than aborrow_checker(&self) -> &BorrowChecker
method.- move the concrete
wiggle::BorrowChecker
type to be part of wasmtime. Impl theGuestMemory
trait inwasmtime-wiggle
with wasmtime'sBorrowChecker
andMemory
.- Lucet can make its own copy of
BorrowChecker
to keep inlucet-wiggle
or elsewhere, and i mpl theGuestMemory
trait in terms of that concrete type and ourVmctx
(we dont have aMemory
equivalent)
pchickey edited a comment on Issue #1917:
My idea to manage the wasmtime/lucet split:
- change the
wiggle::GuestMemory
trait to have all ofBorrowChecker
's methods inline (has_outstanding_borrows
,borrow
,unborrow
,is_borrowed
) rather than aborrow_checker(&self) -> &BorrowChecker
method.- move the concrete
wiggle::BorrowChecker
type to be part of wasmtime. Impl theGuestMemory
trait inwasmtime-wiggle
with wasmtime'sBorrowChecker
andMemory
.- Lucet can make its own copy of
BorrowChecker
to keep inlucet-wiggle
or elsewhere, and impl theGuestMemory
trait in terms of that concrete type and ourVmctx
(we dont have aMemory
equivalent)
pchickey edited a comment on Issue #1917:
My idea to manage the wasmtime/lucet split:
- change the
wiggle::GuestMemory
trait to have all ofBorrowChecker
's methods inline (has_outstanding_borrows
,borrow
,unborrow
,is_borrowed
) rather than aborrow_checker(&self) -> &BorrowChecker
method. We can then delete theBorrowChecker
type from wiggle.- move the concrete
wiggle::BorrowChecker
type to be part of wasmtime. Impl theGuestMemory
trait inwasmtime-wiggle
with wasmtime'sBorrowChecker
andMemory
.- Lucet can make its own copy of
BorrowChecker
to keep inlucet-wiggle
or elsewhere, and impl theGuestMemory
trait in terms of that concrete type and ourVmctx
(we dont have aMemory
equivalent)
alexcrichton commented on Issue #1917:
Sounds reasonable to me!
pchickey commented on Issue #1917:
I'm preparing a PR which does steps 1 and 3 of this transformation, moving the
BorrowChecker
concrete impl towasmtime-wiggle
as a staging ground for step 2, where it can be moved fully intowasmtime
.
Last updated: Dec 23 2024 at 12:05 UTC