Add an inCompartments config for bindings

This commit is contained in:
Bastien Orivel 2019-05-24 22:24:41 +02:00
parent 2181872973
commit 7dbff6efb7
7 changed files with 37 additions and 10 deletions

View file

@ -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)]

View file

@ -40,6 +40,14 @@ DOMInterfaces = {
'WindowProxy' : {
'path': 'crate::dom::windowproxy::WindowProxy',
'register': False,
},
'Window': {
'inCompartments': ['Fetch'],
},
'WorkerGlobalScope': {
'inCompartments': ['Fetch'],
}
}

View file

@ -3316,6 +3316,8 @@ class CGCallGenerator(CGThing):
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 @@ class CGInterfaceTrait(CGThing):
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 @@ class CGInterfaceTrait(CGThing):
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 @@ class CGInterfaceTrait(CGThing):
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)

View file

@ -223,6 +223,7 @@ class Descriptor(DescriptorProvider):
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

View file

@ -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> {

View file

@ -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

View file

@ -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