Bug 745661 - Make TI assertion handle safe mode, too. r=khuey
authorBobby Holley <bobbyholley@gmail.com>
Mon, 16 Apr 2012 16:57:36 -0700
changeset 91801 af7a735a2d9ed73696ed6c99d5fb9952c838eb40
parent 91800 9e11f5d4b7d9e185f6a69e64799b2c634a856e69
child 91802 61189954ca171da5767152658c8040dcd4639bfe
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewerskhuey
bugs745661
milestone14.0a1
Bug 745661 - Make TI assertion handle safe mode, too. r=khuey
js/xpconnect/src/nsXPConnect.cpp
--- a/js/xpconnect/src/nsXPConnect.cpp
+++ b/js/xpconnect/src/nsXPConnect.cpp
@@ -40,17 +40,16 @@
  *
  * ***** END LICENSE BLOCK ***** */
 
 /* High level class and public functions implementation. */
 
 #include "mozilla/Assertions.h"
 #include "mozilla/Base64.h"
 #include "mozilla/Util.h"
-#include "mozilla/Preferences.h"
 
 #include "xpcprivate.h"
 #include "XPCWrapper.h"
 #include "nsBaseHashtable.h"
 #include "nsHashKeys.h"
 #include "jsatom.h"
 #include "jsfriendapi.h"
 #include "jsgc.h"
@@ -1189,34 +1188,65 @@ TraceXPCGlobal(JSTracer *trc, JSObject *
         return;
     }
 #endif
 
     if (XPCWrappedNativeScope *scope = XPCWrappedNativeScope::GetNativeScope(obj))
         scope->TraceDOMPrototypes(trc);
 }
 
+#ifdef DEBUG
+#include "mozilla/Preferences.h"
+#include "nsIXULRuntime.h"
+static void
+CheckTypeInference(JSContext *cx, JSClass *clasp, nsIPrincipal *principal)
+{
+    // Check that the global class isn't whitelisted.
+    if (strcmp(clasp->name, "Sandbox") ||
+        strcmp(clasp->name, "nsXBLPrototypeScript compilation scope") ||
+        strcmp(clasp->name, "nsXULPrototypeScript compilation scope"))
+        return;
+
+    // Check that the pref is on.
+    if (!mozilla::Preferences::GetBool("javascript.options.typeinference"))
+        return;
+
+    // Check that we're not chrome.
+    bool isSystem;
+    nsIScriptSecurityManager* ssm;
+    ssm = XPCWrapper::GetSecurityManager();
+    if (NS_FAILED(ssm->IsSystemPrincipal(principal, &isSystem)) || !isSystem)
+        return;
+
+    // Check that safe mode isn't on.
+    bool safeMode;
+    nsCOMPtr<nsIXULRuntime> xr = do_GetService("@mozilla.org/xre/runtime;1");
+    if (!xr) {
+        NS_WARNING("Couldn't get XUL runtime!");
+        return;
+    }
+    if (NS_FAILED(xr->GetInSafeMode(&safeMode)) || safeMode)
+        return;
+
+    // Finally, do the damn assert.
+    MOZ_ASSERT(JS_GetOptions(cx) & JSOPTION_TYPE_INFERENCE);
+}
+#else
+#define CheckTypeInference(cx, clasp, principal) {}
+#endif
+
 nsresult
 xpc_CreateGlobalObject(JSContext *cx, JSClass *clasp,
                        nsIPrincipal *principal, nsISupports *ptr,
                        bool wantXrays, JSObject **global,
                        JSCompartment **compartment)
 {
     // Make sure that Type Inference is enabled for everything non-chrome.
     // Sandboxes and compilation scopes are exceptions. See bug 744034.
-    mozilla::DebugOnly<bool> isSystem;
-    mozilla::DebugOnly<nsIScriptSecurityManager*> ssm;
-    MOZ_ASSERT_IF(strcmp(clasp->name, "Sandbox") &&
-                  strcmp(clasp->name, "nsXBLPrototypeScript compilation scope") &&
-                  strcmp(clasp->name, "nsXULPrototypeScript compilation scope") &&
-                  mozilla::Preferences::GetBool("javascript.options.typeinference") &&
-                  (ssm = XPCWrapper::GetSecurityManager()) &&
-                  NS_SUCCEEDED(ssm->IsSystemPrincipal(principal, &isSystem.value)) &&
-                  !isSystem.value,
-                  JS_GetOptions(cx) & JSOPTION_TYPE_INFERENCE);
+    CheckTypeInference(cx, clasp, principal);
 
     NS_ABORT_IF_FALSE(NS_IsMainThread(), "using a principal off the main thread?");
     NS_ABORT_IF_FALSE(principal, "bad key");
 
     XPCCompartmentMap& map = nsXPConnect::GetRuntimeInstance()->GetCompartmentMap();
     xpc::PtrAndPrincipalHashKey key(ptr, principal);
     if (!map.Get(&key, compartment)) {
         xpc::PtrAndPrincipalHashKey *priv_key =