Skip to content
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

Fix unsound use of Rooted<T> with RootKind::Traceable #514

Merged
merged 4 commits into from
Oct 28, 2024
Merged

Conversation

jdm
Copy link
Member

@jdm jdm commented Oct 26, 2024

The existing implementation for Rooted<T> is correct for most JS types that are rooted, but there's one significant case that is broken. Rooted<T> uses C++ template magic to change its base class depending on the type that it's templated over, and certain types that map to RootKind::Traceable end up with a vtable that contains a custom tracing implementation, while other types do not have any such vtable.

These template shenanigans are very likely the reason why we have a handwritten Rooted<T> definition, since bindgen is not able to handle this situation. This PR makes the following changes:

  • we introduce a vtable field into Rooted<T> which is zero-sized for types that do not have Traceable RootKind value
  • we introduce a TraceableTrace trait that Rooted<T>'s vtable delegates to, allowing implementers to write straightforward tracing implementations for their types

These changes were verified by running the property_descriptor unit test with zealous GC enabled (ie. perform a GC every time there's an allocation) and the test no longer crashes after these changes are applied.

mozjs-sys/Cargo.toml Outdated Show resolved Hide resolved
mozjs-sys/src/jsimpls.rs Outdated Show resolved Hide resolved
@jdm jdm force-pushed the propdesc branch 2 times, most recently from 9fa98bd to 8c7ff63 Compare October 28, 2024 04:30
mozjs-sys/src/jsgc.rs Outdated Show resolved Hide resolved
Copy link
Member

@sagudev sagudev left a comment

Choose a reason for hiding this comment

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

LGTM

mozjs-sys/src/jsgc.rs Outdated Show resolved Hide resolved
@jdm jdm enabled auto-merge October 28, 2024 06:31
Comment on lines 118 to 120
fn do_trace(&mut self, trc: *mut JSTracer) {
unsafe {
CallPropertyDescriptorTracer(trc, self);
}
unsafe fn do_trace(&mut self, trc: *mut JSTracer) {
CallPropertyDescriptorTracer(trc, self);
}
Copy link
Member

Choose a reason for hiding this comment

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

Actually unsafe block in unsafe fn will be the only correct way in the future: https://doc.rust-lang.org/edition-guide/rust-2024/unsafe-op-in-unsafe-fn.html, but that is not yet relevant.

@jdm jdm added this pull request to the merge queue Oct 28, 2024
Merged via the queue into servo:main with commit f7c263b Oct 28, 2024
25 checks passed
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.

3 participants