Bug 1466118 part 6 - Rename CompartmentChecker to ContextChecks and support realm checks. r=luke
authorJan de Mooij <jdemooij@mozilla.com>
Tue, 21 Aug 2018 12:48:03 +0200
changeset 488142 ff5cb8442b5d452de5677dbacf9bec9df0bf5a83
parent 488141 af49f7a464d573a7008a301b315bd4303996e320
child 488143 1a263d3e088e0cc47b111185579d0d8699b8d190
push id9719
push userffxbld-merge
push dateFri, 24 Aug 2018 17:49:46 +0000
treeherdermozilla-beta@719ec98fba77 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersluke
bugs1466118
milestone63.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 1466118 part 6 - Rename CompartmentChecker to ContextChecks and support realm checks. r=luke
js/src/vm/JSContext-inl.h
js/src/vm/JSContext.cpp
js/src/vm/JSContext.h
--- a/js/src/vm/JSContext-inl.h
+++ b/js/src/vm/JSContext-inl.h
@@ -15,98 +15,100 @@
 #include "vm/HelperThreads.h"
 #include "vm/Interpreter.h"
 #include "vm/Iteration.h"
 #include "vm/Realm.h"
 #include "vm/SymbolType.h"
 
 namespace js {
 
-class CompartmentChecker
+class ContextChecks
 {
-    JS::Compartment* compartment;
+    JSContext* cx;
+
+    JS::Realm* realm() const {
+        return cx->realm();
+    }
+    JS::Compartment* compartment() const {
+        return cx->compartment();
+    }
+    JS::Zone* zone() const {
+        return cx->zone();
+    }
 
   public:
-    explicit CompartmentChecker(JSContext* cx)
-      : compartment(cx->compartment())
+    explicit ContextChecks(JSContext* cx)
+      : cx(cx)
     {
     }
 
     /*
-     * Set a breakpoint here (break js::CompartmentChecker::fail) to debug
-     * compartment mismatches.
+     * Set a breakpoint here (break js::ContextChecks::fail) to debug
+     * realm/compartment/zone mismatches.
      */
+    static void fail(JS::Realm* r1, JS::Realm* r2, int argIndex) {
+        MOZ_CRASH_UNSAFE_PRINTF("*** Realm mismatch %p vs. %p at argument %d\n",
+                                r1, r2, argIndex);
+    }
     static void fail(JS::Compartment* c1, JS::Compartment* c2, int argIndex) {
         MOZ_CRASH_UNSAFE_PRINTF("*** Compartment mismatch %p vs. %p at argument %d\n",
-                                (void*) c1, (void*) c2, argIndex);
+                                c1, c2, argIndex);
+    }
+    static void fail(JS::Zone* z1, JS::Zone* z2, int argIndex) {
+        MOZ_CRASH_UNSAFE_PRINTF("*** Zone mismatch %p vs. %p at argument %d\n",
+                                z1, z2, argIndex);
     }
 
-    static void fail(JS::Zone* z1, JS::Zone* z2, int argIndex) {
-        MOZ_CRASH_UNSAFE_PRINTF("*** Zone mismatch %p vs. %p at argument %d\n",
-                                (void*) z1, (void*) z2, argIndex);
+    void check(JS::Realm* r, int argIndex) {
+        if (r && r != realm())
+            fail(realm(), r, argIndex);
     }
 
     void check(JS::Compartment* c, int argIndex) {
-        if (c && c != compartment)
-            fail(compartment, c, argIndex);
+        if (c && c != compartment())
+            fail(compartment(), c, argIndex);
     }
 
-    void checkZone(JS::Zone* z, int argIndex) {
-        if (compartment && z != compartment->zone())
-            fail(compartment->zone(), z, argIndex);
+    void check(JS::Zone* z, int argIndex) {
+        if (zone() && z != zone())
+            fail(zone(), z, argIndex);
     }
 
     void check(JSObject* obj, int argIndex) {
         if (obj) {
             MOZ_ASSERT(JS::ObjectIsNotGray(obj));
             MOZ_ASSERT(!js::gc::IsAboutToBeFinalizedUnbarriered(&obj));
             check(obj->compartment(), argIndex);
         }
     }
 
-    template<typename T>
-    void check(const Rooted<T>& rooted, int argIndex) {
-        check(rooted.get(), argIndex);
-    }
-
-    template<typename T>
-    void check(Handle<T> handle, int argIndex) {
-        check(handle.get(), argIndex);
-    }
-
-    template<typename T>
-    void check(MutableHandle<T> handle, int argIndex) {
-        check(handle.get(), argIndex);
-    }
-
     template <typename T>
     void checkAtom(T* thing, int argIndex) {
         static_assert(mozilla::IsSame<T, JSAtom>::value ||
                       mozilla::IsSame<T, JS::Symbol>::value,
                       "Should only be called with JSAtom* or JS::Symbol* argument");
 
 #ifdef DEBUG
         // Atoms which move across zone boundaries need to be marked in the new
         // zone, see JS_MarkCrossZoneId.
-        if (compartment) {
-            JSRuntime* rt = compartment->runtimeFromAnyThread();
-            if (!rt->gc.atomMarking.atomIsMarked(compartment->zone(), thing)) {
+        if (zone()) {
+            if (!cx->runtime()->gc.atomMarking.atomIsMarked(zone(), thing)) {
                 MOZ_CRASH_UNSAFE_PRINTF("*** Atom not marked for zone %p at argument %d\n",
-                                        compartment->zone(), argIndex);
+                                        zone(), argIndex);
             }
         }
 #endif
     }
 
     void check(JSString* str, int argIndex) {
         MOZ_ASSERT(JS::CellIsNotGray(str));
         if (str->isAtom())
             checkAtom(&str->asAtom(), argIndex);
         else
-            checkZone(str->zone(), argIndex);
+            check(str->zone(), argIndex);
     }
 
     void check(JS::Symbol* symbol, int argIndex) {
         checkAtom(symbol, argIndex);
     }
 
     void check(const js::Value& v, int argIndex) {
         if (v.isObject())
@@ -177,18 +179,17 @@ class CompartmentChecker
 
 template <class T1> inline void
 JSContext::checkImpl(int argIndex, const T1& t1)
 {
     // Don't perform these checks when called from a finalizer. The checking
     // depends on other objects not having been swept yet.
     if (JS::RuntimeHeapIsCollecting())
         return;
-    js::CompartmentChecker c(this);
-    c.check(t1, argIndex);
+    js::ContextChecks(this).check(t1, argIndex);
 }
 
 template <class Head, class... Tail> inline void
 JSContext::checkImpl(int argIndex, const Head& head, const Tail&... tail)
 {
     checkImpl(argIndex, head);
     checkImpl(argIndex + 1, tail...);
 }
--- a/js/src/vm/JSContext.cpp
+++ b/js/src/vm/JSContext.cpp
@@ -1557,24 +1557,24 @@ JS::AutoCheckRequestDepth::~AutoCheckReq
         cx->checkRequestDepth--;
     }
 }
 
 #endif
 
 #ifdef JS_CRASH_DIAGNOSTICS
 void
-CompartmentChecker::check(InterpreterFrame* fp, int argIndex)
+ContextChecks::check(InterpreterFrame* fp, int argIndex)
 {
     if (fp)
         check(fp->environmentChain(), argIndex);
 }
 
 void
-CompartmentChecker::check(AbstractFramePtr frame, int argIndex)
+ContextChecks::check(AbstractFramePtr frame, int argIndex)
 {
     if (frame)
         check(frame.environmentChain(), argIndex);
 }
 #endif
 
 void
 AutoEnterOOMUnsafeRegion::crash(const char* reason)
--- a/js/src/vm/JSContext.h
+++ b/js/src/vm/JSContext.h
@@ -958,16 +958,18 @@ struct JSContext : public JS::RootingCon
   private:
     template <class T1>
     inline void checkImpl(int argIndex, const T1& t1);
 
     template <class Head, class... Tail>
     inline void checkImpl(int argIndex, const Head& head, const Tail&... tail);
 
   public:
+    // Assert the arguments are in this context's realm (for scripts),
+    // compartment (for objects) or zone (for strings, symbols).
     template <class... Args> inline void check(const Args&... args);
     template <class... Args> inline void releaseCheck(const Args&... args);
     template <class... Args> MOZ_ALWAYS_INLINE void debugOnlyCheck(const Args&... args);
 }; /* struct JSContext */
 
 inline JS::Result<>
 JSContext::boolToResult(bool ok)
 {