Add example for LIFO assurance to AutoRealm Drop implementation#720
Merged
Add example for LIFO assurance to AutoRealm Drop implementation#720
AutoRealm Drop implementation#720Conversation
Signed-off-by: Euclid Ye <yezhizhenjiakang@gmail.com>
sagudev
reviewed
Mar 17, 2026
mozjs/src/realm.rs
Outdated
| // If we do not implement this, Rust can end the borrow of cx early, | ||
| // while the JSAutoRealm is still active. | ||
| // That would allow a second mutable borrow of cx before the first realm is exited, | ||
| // breaking the LIFO entering/exiting realms. |
Member
There was a problem hiding this comment.
IIRC The real problem is that JSAutoRealm internally uses cx (it's raw ptr), so if we need to make sure that &cx remains bounded until that happens.
I think breaking LIFO is then side effect of this.
Member
Author
There was a problem hiding this comment.
Thanks! How about this?
// If we do not implement this, Rust can end the borrow of [JSContext]
// early while [JSAutoRealm] is still active. Since [JSAutoRealm]
// internally holds a raw pointer to the context, we must ensure the
// cx borrow remains bounded until the guard is dropped to not
// use after free and ensure strict LIFO realm entry/exit.
Member
There was a problem hiding this comment.
There is indeed LIFO problem otherwise, so we could link it: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=8799c0a41b5e007ff3b352b668dbf603
but otherwise LGTM.
Member
Author
There was a problem hiding this comment.
I played with the example and added some more cases.
Signed-off-by: Euclid Ye <yezhizhenjiakang@gmail.com>
sagudev
approved these changes
Mar 17, 2026
Member
|
Feel free to land when done. |
AutoRealm Drop implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
There was some typo and wrong space.
Also add more texts based on my understanding.
Added a link for example.