Pass Fd by reference#1375
Conversation
ede3f22 to
09c5c44
Compare
279b56c to
09c5c44
Compare
Make handles non-`Copy` and non-`Clone`. Pass them by reference, and store them instructs as raw `u32` values. And make `Fd::from_raw` unsafe so that we're careful about where we get handles from. This also obviates the `Fd` trait. With `EntryTable`, it's now straightforward to let `FdPool` just operate on `u32` values.
This commit updates `wiggle` to treat handles as non-transparent types. As a result, if any compound data type includes handle as a field, it can no longer be `Copy` nor `PartialEq`. I've also added missing methods to handles `unsafe fn from_raw(u32) -> Self` and `fn as_raw(&self) -> u32` which replace `From` impls.
f1f75dd to
c8a2b52
Compare
|
OK, so the only thing missing is storing handles as part of a struct and passing this struct around. Personally, I think I'm leaning towards the first suggestion of always passing handles by ref. |
|
On second thought, this is going to be more tricky than I originally anticipated. |
|
I think I may be missing the motivation here, it sounds like we have u32 values from wasm, I feel like a better eventuality may be something where if you take a trait WasiSnapshot {
fn lookup_fd(&self, idx: u32) -> Result<&Entry>;
}(or something like that) And that way APIs don't ever actually deal with I guess I'm sort of confused why we want more types passed by reference when we already have a type passed by reference, |
|
Hmm, those are some really good points, thanks @alexcrichton! I too don’t want to make this overly complicated and convoluted, and indeed this somewhat starts to look like it. However, one motivation for handles being non-Copy and non-Clone, and passed by ref, would be to signal that they are something more than just an “index” into a capabilities table. I guess you could think of it as |
|
@alexcrichton I like that idea. If it's feasible to have wiggle generate the |
|
I like the idea of looking up witx handles to get a ref to some other Rust type, and passing that to the trait method. I've been fielding requests for wiggle to be able to convert to and from types defined in the user's code in other contexts. So I wonder if we can come up with a generalized way for wiggle users to specify that a witx type needs to be transformed to/from some externally-defined Rust type before consumption / after production. The other big use case that has come up is in Fastly's HTTP code, we have a rich (as in This is a pretty complicated design space to figure out so if anyone wants to have a zoom call to chat about it more, lets. |
|
This might be a bit more premature to ask, but I guess in a trait suggested by @alexcrichton we'd expect something like this: trait WasiSnapshot {
type Entry;
fn lookup_fd(&self, idx: u32) -> Result<&Self::Entry>;
}in the sense that we wouldn't tie some specific type to the trait and the lookup method, or would we actually? I'm just trying to get a better sense of how we'd like to achieve this. |
|
That's my rough thinking yeah, although it may not literally work out as such. The idea though would be that each implementation would define what an This may be a bit pie-in-the-sky though, so don't feel like anything needs to be blocked on it. |
Subscribe to Label ActionThis issue or pull request has been labeled: "wasi" Users Subscribed to "wasi"To subscribe or unsubscribe from this label, edit the |
|
Closing this PR to pursue other approaches as discussed above. |
This is a draft PR partially illustrating the idea mentioned in #1202 for passing
Fdhandles by reference.This provides I/O safety within the WASI implementation code, though it depends on being careful around calling the
Fd::from_rawfunction. It also simplifies theFdPool, by taking advantage of the newEntryTable.This doesn't build at the moment, because it doesn't change the wiggle code, but it shows how the rest of the code would look.
Fdis passed by reference when it's an argument in host code.Fdno longer needs to implementCopyorClone.types::SubscriptionU::FdReadandtypes::SubscriptionU::FdWritestill need to be stored asu32; it can be converted to anFdby calling theunsafefrom_rawIn Wiggle, would it be better to special-case Handle to implement pass-by-reference behavior, or add a new
TypePassedBykind inwitx?