Stream: wasmtime

Topic: Resource mutability in Rust


view this post on Zulip Cwasi4wasm (Feb 09 2024 at 18:34):

Hello, was wondering, I am defining some resources in my wit file but having a problem defining a constructor that contains a mutable reference of another resource. Would prefer not to use mutexs but if there is no other way that is fine as well. Was wondering if it was possible?

view this post on Zulip Pat Hickey (Feb 09 2024 at 18:35):

in wasmtime-wasi, when we need to store a reference to one host resource in another host resource, we do so by storing the table index to one inside the other

view this post on Zulip Pat Hickey (Feb 09 2024 at 18:36):

and we also make sure that the resource containing the reference is inserted into the ResourceTable as a child of the referent, which ensures that the referent outlives the reference

view this post on Zulip Pat Hickey (Feb 09 2024 at 18:38):

in general though this is a tricky design space because the rules for resources dont fit neatly into the rust type system. see, for more discussion and context, https://github.com/bytecodealliance/wasmtime/pull/7902 and https://github.com/bytecodealliance/wasmtime/pull/7812

Right now the abstraction for host-owned resources in Wasmtime is quite simple, it's "just an index". This can be error prone because the host can suffer both from use-after-free and ABA-style prob...
Prior discussion: https://bytecodealliance.zulipchat.com/#narrow/stream/217126-wasmtime/topic/Change.20Subscribe.20trait Renamed the existing Pollable struct to PollableResource Reimplemented wasi...

view this post on Zulip Pat Hickey (Feb 09 2024 at 18:38):

basically, we designed the resource type system to be maximally useful by components, not to make the host implementation straightforward

view this post on Zulip Cwasi4wasm (Feb 09 2024 at 18:45):

Interesting! I will read this and the links through to better understand. Thank you!

view this post on Zulip Pat Hickey (Feb 09 2024 at 18:52):

for sure. happy to talk more about it, its a really tricky topic, and we dont have all the answers yet.


Last updated: Dec 23 2024 at 14:03 UTC