Bug 1518753 part 1 - Add --more-compartments JS shell flag, make same-compartment the default for newGlobal. r=jorendorff
authorJan de Mooij <jdemooij@mozilla.com>
Sat, 12 Jan 2019 10:48:00 +0000
changeset 453627 450b8f0cbb4e494b399ebcf23a33b8d9cb883245
parent 453626 10907464fcc25c1e2fd9b0aed806bfdea2e90ee8
child 453628 92f0cf2761988f08431a5af88e66de95f2d36651
push id35362
push userncsoregi@mozilla.com
push dateSat, 12 Jan 2019 21:35:38 +0000
treeherdermozilla-central@877169d8ef49 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjorendorff
bugs1518753
milestone66.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 1518753 part 1 - Add --more-compartments JS shell flag, make same-compartment the default for newGlobal. r=jorendorff We want to use this shell flag in automation. Some globals really need their own compartment so tests can use newGlobal({newCompartment: true}) to opt-out. Differential Revision: https://phabricator.services.mozilla.com/D16166
js/src/jit-test/tests/basic/more-compartments-flag.js
js/src/shell/fuzz-flags.txt
js/src/shell/js.cpp
new file mode 100644
--- /dev/null
+++ b/js/src/jit-test/tests/basic/more-compartments-flag.js
@@ -0,0 +1,8 @@
+// |jit-test| --more-compartments
+
+// With --more-compartments we should default to creating a new compartment for
+// new globals.
+
+var g = newGlobal();
+assertEq(objectGlobal(g), null); // CCW
+assertEq(isProxy(g), true);
--- a/js/src/shell/fuzz-flags.txt
+++ b/js/src/shell/fuzz-flags.txt
@@ -36,16 +36,17 @@
 --ion-sincos=off
 --ion-sincos=on
 --ion-warmup-threshold=100
 --no-native-regexp
 --nursery-strings=off
 --nursery-strings=on
 --spectre-mitigations=off
 --spectre-mitigations=on
+--more-compartments
 
 # GC-related
 # These 2 flags can cause the shell to slow down
 # --gc-zeal=2
 # --gc-zeal=10
 --no-cgc
 --no-ggc
 --no-incremental-gc
--- a/js/src/shell/js.cpp
+++ b/js/src/shell/js.cpp
@@ -512,16 +512,17 @@ static bool printTiming = false;
 static const char* jsCacheDir = nullptr;
 static const char* jsCacheAsmJSPath = nullptr;
 static RCFile* gErrFile = nullptr;
 static RCFile* gOutFile = nullptr;
 static bool reportWarnings = true;
 static bool compileOnly = false;
 static bool fuzzingSafe = false;
 static bool disableOOMFunctions = false;
+static bool defaultToSameCompartment = true;
 
 #ifdef DEBUG
 static bool dumpEntrainedVariables = false;
 static bool OOM_printAllocationCount = false;
 #endif
 
 // Shell state this is only accessed on the main thread.
 bool jsCachingEnabled = false;
@@ -6172,17 +6173,24 @@ static bool WrapWithProto(JSContext* cx,
 static bool NewGlobal(JSContext* cx, unsigned argc, Value* vp) {
   JSPrincipals* principals = nullptr;
 
   JS::RealmOptions options;
   JS::RealmCreationOptions& creationOptions = options.creationOptions();
   JS::RealmBehaviors& behaviors = options.behaviors();
 
   SetStandardRealmOptions(options);
-  options.creationOptions().setNewCompartmentAndZone();
+
+  // Default to creating the global in the current compartment unless
+  // --more-compartments is used.
+  if (defaultToSameCompartment) {
+    creationOptions.setExistingCompartment(cx->global());
+  } else {
+    creationOptions.setNewCompartmentAndZone();
+  }
 
   CallArgs args = CallArgsFromVp(argc, vp);
   if (args.length() == 1 && args[0].isObject()) {
     RootedObject opts(cx, &args[0].toObject());
     RootedValue v(cx);
 
     if (!JS_GetProperty(cx, opts, "invisibleToDebugger", &v)) {
       return false;
@@ -6208,16 +6216,23 @@ static bool NewGlobal(JSContext* cx, uns
 
     if (!JS_GetProperty(cx, opts, "sameCompartmentAs", &v)) {
       return false;
     }
     if (v.isObject()) {
       creationOptions.setExistingCompartment(UncheckedUnwrap(&v.toObject()));
     }
 
+    if (!JS_GetProperty(cx, opts, "newCompartment", &v)) {
+      return false;
+    }
+    if (v.isBoolean() && v.toBoolean()) {
+      creationOptions.setNewCompartmentAndZone();
+    }
+
     if (!JS_GetProperty(cx, opts, "disableLazyParsing", &v)) {
       return false;
     }
     if (v.isBoolean()) {
       behaviors.setDisableLazyParsing(v.toBoolean());
     }
 
     if (!JS_GetProperty(cx, opts, "systemPrincipal", &v)) {
@@ -8532,17 +8547,20 @@ JS_FN_HELP("parseBin", BinParse, 1, 0,
     JS_FN_HELP("newGlobal", NewGlobal, 1, 0,
 "newGlobal([options])",
 "  Return a new global object in a new realm. If options\n"
 "  is given, it may have any of the following properties:\n"
 "\n"
 "      sameZoneAs: The compartment will be in the same zone as the given\n"
 "         object (defaults to a new zone).\n"
 "      sameCompartmentAs: The global will be in the same compartment and\n"
-"         zone as the given object (defaults to a new compartment).\n"
+"         zone as the given object (defaults to the current compartment,\n"
+"         unless the --more-compartments option is used).\n"
+"      newCompartment: If true, the global will always be created in a new\n"
+"         compartment, even without --more-compartments.\n"
 "      cloneSingletons: If true, always clone the objects baked into\n"
 "         scripts, even if it's a top-level script that will only run once\n"
 "         (defaults to using them directly in scripts that will only run\n"
 "         once).\n"
 "      invisibleToDebugger: If true, the global will be invisible to the\n"
 "         debugger (default false)\n"
 "      disableLazyParsing: If true, don't create lazy scripts for functions\n"
 "         (default false).\n"
@@ -10678,16 +10696,20 @@ static int Shell(JSContext* cx, OptionPa
     fuzzingSafe =
         (getenv("MOZ_FUZZING_SAFE") && getenv("MOZ_FUZZING_SAFE")[0] != '0');
   }
 
   if (op->getBoolOption("disable-oom-functions")) {
     disableOOMFunctions = true;
   }
 
+  if (op->getBoolOption("more-compartments")) {
+    defaultToSameCompartment = false;
+  }
+
   JS::RealmOptions options;
   SetStandardRealmOptions(options);
   RootedObject glob(cx, NewGlobalObject(cx, options, nullptr));
   if (!glob) {
     return 1;
   }
 
   JSAutoRealm ar(cx, glob);
@@ -11048,16 +11070,19 @@ int main(int argc, char** argv, char** e
           '\0', "no-sse4",
           "Pretend CPU does not support SSE4 instructions "
           "to test JIT codegen (no-op on platforms other than x86 and x64).") ||
       !op.addBoolOption('\0', "enable-avx",
                         "AVX is disabled by default. Enable AVX. "
                         "(no-op on platforms other than x86 and x64).") ||
       !op.addBoolOption('\0', "no-avx",
                         "No-op. AVX is currently disabled by default.") ||
+      !op.addBoolOption('\0', "more-compartments",
+                        "Make newGlobal default to creating a new "
+                        "compartment.") ||
       !op.addBoolOption('\0', "fuzzing-safe",
                         "Don't expose functions that aren't safe for "
                         "fuzzers to call") ||
       !op.addBoolOption('\0', "disable-oom-functions",
                         "Disable functions that cause "
                         "artificial OOMs") ||
       !op.addBoolOption('\0', "no-threads", "Disable helper threads")
 #ifdef DEBUG