Skip to content

Add example for LIFO assurance to AutoRealm Drop implementation#720

Merged
yezhizhen merged 2 commits intomainfrom
yezhizhen-patch-1
Mar 18, 2026
Merged

Add example for LIFO assurance to AutoRealm Drop implementation#720
yezhizhen merged 2 commits intomainfrom
yezhizhen-patch-1

Conversation

@yezhizhen
Copy link
Member

@yezhizhen yezhizhen commented Mar 17, 2026

There was some typo and wrong space.
Also add more texts based on my understanding.
Added a link for example.

Signed-off-by: Euclid Ye <yezhizhenjiakang@gmail.com>
@yezhizhen yezhizhen requested review from sagudev and xiaochengh March 17, 2026 11:31
// 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Member Author

@yezhizhen yezhizhen Mar 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Member Author

@yezhizhen yezhizhen Mar 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Signed-off-by: Euclid Ye <yezhizhenjiakang@gmail.com>
@sagudev
Copy link
Member

sagudev commented Mar 17, 2026

Feel free to land when done.

@yezhizhen yezhizhen changed the title Update comments in AutoRealm Drop implementation Add example for LIFO assurance to AutoRealm Drop implementation Mar 18, 2026
@yezhizhen yezhizhen added this pull request to the merge queue Mar 18, 2026
Merged via the queue into main with commit b01d302 Mar 18, 2026
42 checks passed
@yezhizhen yezhizhen deleted the yezhizhen-patch-1 branch March 18, 2026 04:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants