-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Likely undefined behavior in Wasm blog post #4
Comments
If you know it will only be accessed in this scope, you can use |
Ooh, that's quite subtle! I'm guessing NLL won't provide any guarantees around when things are dropped because that would let people become coupled to the implementation. I guess I could remove the I've made a thread on the internal forums. Hopefully people smarter than I will be able to shed some light on the situation.
This same point was brought up when proofreading the article. I've opted to clear the |
This Comment seems pretty relevant:
|
In light of mbrubeck's comment I don't think this is actually UB. NLL is purely a compile-time check and won't have any affect on runtime code (the "with non-lexcial lifetimes I believe it's valid for Rust to call drop on your state here" comment). |
Specifically, if a type implements So long as your type has drop glue (it implements |
Thanks for asking about it! It's always nice to update my mental model of these things! I'm glad to hear that Rust isn't doing anything too fancy with NLL, like eagerly dropping values after last use!
Does shadowing the variable here count as reassignment or by reassignment do they mean overwriting the value at that location?
I could read the above quote to mean either, though I'm guessing it probably doesn't include shadowing. If that is the case, then the original quoted code is using a dropped value, but it's not because of NLL, because the reassignment (shadowing).
If I understand it correctly, it should be possible to use safe Rust types for this if you wanted to. You'd have to create a wrapper type that you store a pointer to your That's more of a personal preference thing though! |
@MarkMcCaskey shadowing just makes the value inaccessible*. Reassignment is actually rebinding the same Here's a playground that shows off some of the behavior: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=c04ffccccd5a844d6ef147a14202d60d *actually, macros mean that's not the case 100% of the time! See the playground link. |
@CAD97 Wow, that's super useful! I shared that with my team -- thanks for the explanation and example! |
By the way, regarding the initial pattern, I am more fond of doing it like this: let ref mut state = State { env };
// point the data pointer at our temporary state.
instance.context_mut().data = { state } as *mut State<'_> as *mut _; As you can see in https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=f2083bd6bcd9a37e053fc7833887158c, the |
That's a really nice way to use patterns! I might leave it as-is though seeing as the |
Note that using |
Hello! I'm still in the process of reading your wasm as a platform for abstraction blogpost and I love it! I actually work on Wasmer and we're super impressed with the depth of your post!
The use of Ctx::data is a bit subtle; it doesn't have to be reset on every host call if you don't want it to be, and the code:
Is a bit concerning because with non-lexcial lifetimes I believe it's valid for Rust to call
drop
on yourstate
here and then any access toCtx::data
would be accessing freed memory.The way I've been avoiding this when I do this is to use
Box::into_raw(Box::new(...))
and then usingBox::from_raw
to destroy it when I want to.I don't have a full mental model of your system yet, I'll keep reading and then edit this post if I realize I've made a mistake!
The text was updated successfully, but these errors were encountered: