Bug 979481 - Make the SafeJSContext default to a null compartment, and have AutoSafeJSContext enter the compartment instead. r=bz
authorBobby Holley <bobbyholley@gmail.com>
Thu, 06 Mar 2014 08:55:01 -0800
changeset 172342 670fb9adcd29826b0ab35db2380121e6c99aa433
parent 172341 5423f1b0599ea06777db50e8729696fb26ceeb0d
child 172343 77f4622e20002af05aa733573f09d2135747aecb
push id26358
push usercbook@mozilla.com
push dateFri, 07 Mar 2014 11:48:31 +0000
treeherdermozilla-central@b9fc2eb18bd1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbz
bugs979481
milestone30.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 979481 - Make the SafeJSContext default to a null compartment, and have AutoSafeJSContext enter the compartment instead. r=bz
js/src/jsfriendapi.cpp
js/xpconnect/src/XPCJSContextStack.cpp
js/xpconnect/src/nsCxPusher.cpp
js/xpconnect/src/nsCxPusher.h
--- a/js/src/jsfriendapi.cpp
+++ b/js/src/jsfriendapi.cpp
@@ -411,16 +411,18 @@ js::AssertSameCompartment(JSObject *objA
 {
     JS_ASSERT(objA->compartment() == objB->compartment());
 }
 #endif
 
 JS_FRIEND_API(JSObject *)
 js::DefaultObjectForContextOrNull(JSContext *cx)
 {
+    if (cx->options().noDefaultCompartmentObject())
+        return nullptr;
     return cx->maybeDefaultCompartmentObject();
 }
 
 JS_FRIEND_API(void)
 js::SetDefaultObjectForContext(JSContext *cx, JSObject *obj)
 {
     cx->setDefaultCompartmentObject(obj);
 }
--- a/js/xpconnect/src/XPCJSContextStack.cpp
+++ b/js/xpconnect/src/XPCJSContextStack.cpp
@@ -170,32 +170,29 @@ XPCJSContextStack::InitSafeJSContext()
     JSRuntime *rt = xpc->GetRuntime()->Runtime();
     if (!rt)
         MOZ_CRASH();
 
     mSafeJSContext = JS_NewContext(rt, 8192);
     if (!mSafeJSContext)
         MOZ_CRASH();
     JSAutoRequest req(mSafeJSContext);
+    ContextOptionsRef(mSafeJSContext).setNoDefaultCompartmentObject(true);
 
     JS_SetErrorReporter(mSafeJSContext, xpc::SystemErrorReporter);
 
     JS::CompartmentOptions options;
     options.setZone(JS::SystemZone);
     mSafeJSContextGlobal = CreateGlobalObject(mSafeJSContext,
                                               &SafeJSContextGlobalClass,
                                               principal, options);
     if (!mSafeJSContextGlobal)
         MOZ_CRASH();
     JS_AddNamedObjectRoot(mSafeJSContext, &mSafeJSContextGlobal, "SafeJSContext global");
 
-    // Make sure the context is associated with a proper compartment
-    // and not the default compartment.
-    js::SetDefaultObjectForContext(mSafeJSContext, mSafeJSContextGlobal);
-
     // Note: make sure to set the private before calling
     // InitClasses
     nsRefPtr<SandboxPrivate> sp = new SandboxPrivate(principal, mSafeJSContextGlobal);
     JS_SetPrivate(mSafeJSContextGlobal, sp.forget().get());
 
     if (NS_FAILED(xpc->InitClasses(mSafeJSContext, mSafeJSContextGlobal)))
         MOZ_CRASH();
 
--- a/js/xpconnect/src/nsCxPusher.cpp
+++ b/js/xpconnect/src/nsCxPusher.cpp
@@ -223,16 +223,17 @@ ThreadsafeAutoJSContext::operator JSCont
     return mCx;
   } else {
     return mAutoJSContext.ref();
   }
 }
 
 AutoSafeJSContext::AutoSafeJSContext(MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM_IN_IMPL)
   : AutoJSContext(true MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT)
+  , mAc(mCx, XPCJSRuntime::Get()->GetJSContextStack()->GetSafeJSContextGlobal())
 {
 }
 
 ThreadsafeAutoSafeJSContext::ThreadsafeAutoSafeJSContext(MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM_IN_IMPL)
 {
   MOZ_GUARD_OBJECT_NOTIFIER_INIT;
 
   if (NS_IsMainThread()) {
--- a/js/xpconnect/src/nsCxPusher.h
+++ b/js/xpconnect/src/nsCxPusher.h
@@ -95,17 +95,16 @@ namespace mozilla {
 class MOZ_STACK_CLASS AutoJSContext {
 public:
   AutoJSContext(MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM);
   operator JSContext*() const;
 
 protected:
   AutoJSContext(bool aSafe MOZ_GUARD_OBJECT_NOTIFIER_PARAM);
 
-private:
   // We need this Init() method because we can't use delegating constructor for
   // the moment. It is a C++11 feature and we do not require C++11 to be
   // supported to be able to compile Gecko.
   void Init(bool aSafe MOZ_GUARD_OBJECT_NOTIFIER_PARAM);
 
   JSContext* mCx;
   Maybe<AutoCxPusher> mPusher;
   MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
@@ -129,16 +128,18 @@ private:
 
 /**
  * AutoSafeJSContext is similar to AutoJSContext but will only return the safe
  * JS context. That means it will never call ::GetCurrentJSContext().
  */
 class MOZ_STACK_CLASS AutoSafeJSContext : public AutoJSContext {
 public:
   AutoSafeJSContext(MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM);
+private:
+  JSAutoCompartment mAc;
 };
 
 /**
  * Like AutoSafeJSContext but can be used safely on worker threads.
  */
 class MOZ_STACK_CLASS ThreadsafeAutoSafeJSContext {
 public:
   ThreadsafeAutoSafeJSContext(MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM);