Skip to content

Commit

Permalink
Auto merge of #20399 - Xanewok:remove-heap-handle-mut, r=jdm
Browse files Browse the repository at this point in the history
Sanitize Heap::handle(_mut) functions

<!-- Please describe your changes on the following line: -->
Complementary to servo/rust-mozjs#404.

Removing `Heap::handle_mut` didn't warrant any changes on Servo side, and so the changes here are only to fix compilation with `Heap::handle` being now marked as `unsafe`.

The main idea is that we can't hand out handles to heap values themselves, since they're not guaranteed to be rooted, but it's safe to do when we are - hence why the safe impl on `RootedTraceableBox<Heap<T>>` and why it's safe to use inside structs that hold a Heap and are `#[must_root]`.

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [ ] These changes fix #__ (github issue number if applicable).

<!-- Either: -->
- [ ] There are tests for these changes OR
- [X] These changes do not require tests because the compiler forces correctness here.

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/20399)
<!-- Reviewable:end -->
  • Loading branch information
bors-servo committed Mar 23, 2018
2 parents 4a6453a + f7c0395 commit 18ef587
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 11 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion components/malloc_size_of/Cargo.toml
Expand Up @@ -16,7 +16,7 @@ app_units = "0.6"
cssparser = "0.23.0"
euclid = "0.17"
hashglobe = { path = "../hashglobe" }
mozjs = { version = "0.3", features = ["promises"], optional = true }
mozjs = { version = "0.4", features = ["promises"], optional = true }
selectors = { path = "../selectors" }
servo_arc = { path = "../servo_arc" }
smallbitvec = "1.0.3"
Expand Down
2 changes: 1 addition & 1 deletion components/script/Cargo.toml
Expand Up @@ -62,7 +62,7 @@ metrics = {path = "../metrics"}
mitochondria = "1.1.2"
mime = "0.2.1"
mime_guess = "1.8.0"
mozjs = { version = "0.3", features = ["promises"]}
mozjs = { version = "0.4", features = ["promises"]}
msg = {path = "../msg"}
net_traits = {path = "../net_traits"}
num-traits = "0.1.32"
Expand Down
3 changes: 2 additions & 1 deletion components/script/dom/bindings/reflector.rs
Expand Up @@ -46,7 +46,8 @@ impl Reflector {
/// Get the reflector.
#[inline]
pub fn get_jsobject(&self) -> HandleObject {
self.object.handle()
// We're rooted, so it's safe to hand out a handle to object in Heap
unsafe { self.object.handle() }
}

/// Initialize the reflector. (May be called only once.)
Expand Down
14 changes: 12 additions & 2 deletions components/script/dom/bindings/trace.rs
Expand Up @@ -59,9 +59,9 @@ use hyper::mime::Mime;
use hyper::status::StatusCode;
use ipc_channel::ipc::{IpcReceiver, IpcSender};
use js::glue::{CallObjectTracer, CallValueTracer};
use js::jsapi::{GCTraceKindToAscii, Heap, JSObject, JSTracer, TraceKind};
use js::jsapi::{GCTraceKindToAscii, Heap, Handle, JSObject, JSTracer, TraceKind};
use js::jsval::JSVal;
use js::rust::Runtime;
use js::rust::{GCMethods, Runtime};
use js::typedarray::TypedArray;
use js::typedarray::TypedArrayElement;
use metrics::{InteractiveMetrics, InteractiveWindow};
Expand Down Expand Up @@ -788,6 +788,16 @@ impl<T: JSTraceable + 'static> RootedTraceableBox<T> {
}
}

impl<T> RootedTraceableBox<Heap<T>>
where
Heap<T>: JSTraceable + 'static,
T: GCMethods + Copy,
{
pub fn handle(&self) -> Handle<T> {
unsafe { (*self.ptr).handle() }
}
}

impl<T: JSTraceable + Default> Default for RootedTraceableBox<T> {
fn default() -> RootedTraceableBox<T> {
RootedTraceableBox::new(T::default())
Expand Down
3 changes: 2 additions & 1 deletion components/script/dom/customelementregistry.rs
Expand Up @@ -604,7 +604,8 @@ impl CustomElementReaction {
match *self {
CustomElementReaction::Upgrade(ref definition) => upgrade_element(definition.clone(), element),
CustomElementReaction::Callback(ref callback, ref arguments) => {
let arguments = arguments.iter().map(|arg| arg.handle()).collect();
// We're rooted, so it's safe to hand out a handle to objects in Heap
let arguments = arguments.iter().map(|arg| unsafe { arg.handle() }).collect();
let _ = callback.Call_(&*element, arguments, ExceptionHandling::Report);
}
}
Expand Down
9 changes: 8 additions & 1 deletion components/script/timers.rs
Expand Up @@ -500,7 +500,7 @@ impl JsTimerTask {
code_str, rval.handle_mut());
},
InternalTimerCallback::FunctionTimerCallback(ref function, ref arguments) => {
let arguments = arguments.iter().map(|arg| arg.handle()).collect();
let arguments = self.collect_heap_args(arguments);
let _ = function.Call_(this, arguments, Report);
},
};
Expand All @@ -516,4 +516,11 @@ impl JsTimerTask {
timers.initialize_and_schedule(&this.global(), self);
}
}

// Returning Handles directly from Heap values is inherently unsafe, but here it's
// always done via rooted JsTimers, which is safe.
#[allow(unsafe_code)]
fn collect_heap_args(&self, args: &[Heap<JSVal>]) -> Vec<HandleValue> {
args.iter().map(|arg| unsafe { arg.handle() }).collect()
}
}

0 comments on commit 18ef587

Please sign in to comment.