Skip to content
Permalink
Browse files

Add an inCompartments config for bindings

  • Loading branch information...
Eijebong committed May 24, 2019
1 parent 2181872 commit 7dbff6efb7ce93ecfb04883cd1dffa24a03ed0ad
@@ -3,7 +3,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use crate::dom::globalscope::GlobalScope;
use js::jsapi::{GetCurrentRealmOrNull, JSAutoRealm};
use js::jsapi::{GetCurrentRealmOrNull, JSAutoRealm, JSContext};

pub struct AlreadyInCompartment(());

@@ -15,6 +15,13 @@ impl AlreadyInCompartment {
}
AlreadyInCompartment(())
}

pub fn assert_for_cx(cx: *mut JSContext) -> AlreadyInCompartment {
unsafe {
assert!(!GetCurrentRealmOrNull(cx).is_null());
}
AlreadyInCompartment(())
}
}

#[derive(Clone, Copy)]
@@ -40,6 +40,14 @@ DOMInterfaces = {
'WindowProxy' : {
'path': 'crate::dom::windowproxy::WindowProxy',
'register': False,
},

'Window': {
'inCompartments': ['Fetch'],
},

'WorkerGlobalScope': {
'inCompartments': ['Fetch'],
}

}
@@ -3316,6 +3316,8 @@ def __init__(self, errorResult, arguments, argsPre, returnType,

if "cx" not in argsPre and needsCx:
args.prepend(CGGeneric("cx"))
if nativeMethodName in descriptor.inCompartmentMethods:
args.append(CGGeneric("InCompartment::in_compartment(&AlreadyInCompartment::assert_for_cx(cx))"));

# Build up our actual call
self.cgRoot = CGList([], "\n")
@@ -5640,7 +5642,7 @@ def members():
name = CGSpecializedMethod.makeNativeName(descriptor, m)
infallible = 'infallible' in descriptor.getExtendedAttributes(m)
for idx, (rettype, arguments) in enumerate(m.signatures()):
arguments = method_arguments(descriptor, rettype, arguments)
arguments = method_arguments(descriptor, rettype, arguments, inCompartment=name in descriptor.inCompartmentMethods)
rettype = return_type(descriptor, rettype, infallible)
yield name + ('_' * idx), arguments, rettype
elif m.isAttr() and not m.isStatic():
@@ -5671,7 +5673,7 @@ def members():
if operation.isGetter():
if not rettype.nullable():
rettype = IDLNullableType(rettype.location, rettype)
arguments = method_arguments(descriptor, rettype, arguments)
arguments = method_arguments(descriptor, rettype, arguments, inCompartment=name in descriptor.inCompartmentMethods)

# If this interface 'supports named properties', then we
# should be able to access 'supported property names'
@@ -5681,7 +5683,7 @@ def members():
if operation.isNamed():
yield "SupportedPropertyNames", [], "Vec<DOMString>"
else:
arguments = method_arguments(descriptor, rettype, arguments)
arguments = method_arguments(descriptor, rettype, arguments, inCompartment=name in descriptor.inCompartmentMethods)
rettype = return_type(descriptor, rettype, infallible)
yield name, arguments, rettype

@@ -5975,6 +5977,8 @@ def generate_imports(config, cgthings, descriptors, callbacks=None, dictionaries
'crate::dom::windowproxy::WindowProxy',
'crate::dom::globalscope::GlobalScope',
'crate::mem::malloc_size_of_including_raw_self',
'crate::compartments::InCompartment',
'crate::compartments::AlreadyInCompartment',
'libc',
'servo_config::pref',
'servo_config::prefs',
@@ -6676,7 +6680,7 @@ def argument_type(descriptorProvider, ty, optional=False, defaultValue=None, var
return declType.define()


def method_arguments(descriptorProvider, returnType, arguments, passJSBits=True, trailing=None):
def method_arguments(descriptorProvider, returnType, arguments, passJSBits=True, trailing=None, inCompartment=False):
if needCx(returnType, arguments, passJSBits):
yield "cx", "*mut JSContext"

@@ -6688,6 +6692,9 @@ def method_arguments(descriptorProvider, returnType, arguments, passJSBits=True,
if trailing:
yield trailing

if inCompartment:
yield "_comp", "InCompartment"


def return_type(descriptorProvider, rettype, infallible):
result = getRetvalDeclarationForType(rettype, descriptorProvider)
@@ -223,6 +223,7 @@ def __init__(self, config, interface, desc):
self.concreteType = typeName
self.register = desc.get('register', True)
self.path = desc.get('path', pathDefault)
self.inCompartmentMethods = [name for name in desc.get('inCompartments', [])]
self.bindingPath = 'crate::dom::bindings::codegen::Bindings::%s' % ('::'.join([ifaceName + 'Binding'] * 2))
self.outerObjectHook = desc.get('outerObjectHook', 'None')
self.proxy = False
@@ -2,6 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use crate::compartments::InCompartment;
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::DocumentBinding::{
DocumentMethods, DocumentReadyState,
@@ -1144,8 +1145,9 @@ impl WindowMethods for Window {
&self,
input: RequestOrUSVString,
init: RootedTraceableBox<RequestInit>,
comp: InCompartment,
) -> Rc<Promise> {
fetch::Fetch(&self.upcast(), input, init)
fetch::Fetch(&self.upcast(), input, init, comp)
}

fn TestRunner(&self) -> DomRoot<TestRunner> {
@@ -2,6 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use crate::compartments::InCompartment;
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::FunctionBinding::Function;
use crate::dom::bindings::codegen::Bindings::RequestBinding::RequestInit;
@@ -368,8 +369,9 @@ impl WorkerGlobalScopeMethods for WorkerGlobalScope {
&self,
input: RequestOrUSVString,
init: RootedTraceableBox<RequestInit>,
comp: InCompartment,
) -> Rc<Promise> {
fetch::Fetch(self.upcast(), input, init)
fetch::Fetch(self.upcast(), input, init, comp)
}

// https://w3c.github.io/hr-time/#the-performance-attribute
@@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use crate::compartments::{AlreadyInCompartment, InCompartment};
use crate::compartments::InCompartment;
use crate::dom::bindings::codegen::Bindings::RequestBinding::RequestInfo;
use crate::dom::bindings::codegen::Bindings::RequestBinding::RequestInit;
use crate::dom::bindings::codegen::Bindings::ResponseBinding::ResponseBinding::ResponseMethods;
@@ -134,12 +134,12 @@ pub fn Fetch(
global: &GlobalScope,
input: RequestInfo,
init: RootedTraceableBox<RequestInit>,
comp: InCompartment,
) -> Rc<Promise> {
let core_resource_thread = global.core_resource_thread();

// Step 1
let aic = AlreadyInCompartment::assert(global);
let promise = Promise::new_in_current_compartment(global, InCompartment::Already(&aic));
let promise = Promise::new_in_current_compartment(global, comp);
let response = Response::new(global);

// Step 2

0 comments on commit 7dbff6e

Please sign in to comment.
You can’t perform that action at this time.