Bug 1576254 - Move Clamping and Jittering from RealmCreationOptions to Behaviors r=luke
authorTom Ritter <tom@mozilla.com>
Fri, 04 Oct 2019 17:35:26 +0000
changeset 496385 f49089ef86eaa3bddd19ecd05a7c9d361fadc67f
parent 496384 8cdc2f617b0da7d3a52b98ebcddc250f75fccb48
child 496386 138a502097d45881aaaa461be2c6d8e7508d440a
push id97215
push usertritter@mozilla.com
push dateFri, 04 Oct 2019 19:40:14 +0000
treeherderautoland@35dfc96baca1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersluke
bugs1576254
milestone71.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 1576254 - Move Clamping and Jittering from RealmCreationOptions to Behaviors r=luke CreationOptions are intended to be immutable and not change during realm operation. Behaviors change, and clamping/jittering should reside on behaviors. Differential Revision: https://phabricator.services.mozilla.com/D43296
dom/workers/WorkerPrivate.cpp
js/public/RealmOptions.h
js/src/jsdate.cpp
js/xpconnect/src/Sandbox.cpp
js/xpconnect/src/nsXPConnect.cpp
--- a/dom/workers/WorkerPrivate.cpp
+++ b/dom/workers/WorkerPrivate.cpp
@@ -2172,19 +2172,19 @@ WorkerPrivate::WorkerPrivate(
     if (aParent->mParentFrozen) {
       Freeze(nullptr);
     }
   } else {
     AssertIsOnMainThread();
 
     RuntimeService::GetDefaultJSSettings(mJSSettings);
 
-    mJSSettings.chrome.realmOptions.creationOptions().setClampAndJitterTime(
+    mJSSettings.chrome.realmOptions.behaviors().setClampAndJitterTime(
         !UsesSystemPrincipal());
-    mJSSettings.content.realmOptions.creationOptions().setClampAndJitterTime(
+    mJSSettings.content.realmOptions.behaviors().setClampAndJitterTime(
         !UsesSystemPrincipal());
 
     if (mIsSecureContext) {
       mJSSettings.chrome.realmOptions.creationOptions().setSecureContext(true);
       mJSSettings.content.realmOptions.creationOptions().setSecureContext(true);
     }
 
     mIsInAutomation = xpc::IsInAutomation();
--- a/js/public/RealmOptions.h
+++ b/js/public/RealmOptions.h
@@ -169,22 +169,16 @@ class JS_PUBLIC_API RealmCreationOptions
   // https://w3c.github.io/webappsec-secure-contexts/
   // https://bugzilla.mozilla.org/show_bug.cgi?id=1162772#c34
   bool secureContext() const { return secureContext_; }
   RealmCreationOptions& setSecureContext(bool flag) {
     secureContext_ = flag;
     return *this;
   }
 
-  bool clampAndJitterTime() const { return clampAndJitterTime_; }
-  RealmCreationOptions& setClampAndJitterTime(bool flag) {
-    clampAndJitterTime_ = flag;
-    return *this;
-  }
-
  private:
   JSTraceOp traceGlobal_ = nullptr;
   CompartmentSpecifier compSpec_ = CompartmentSpecifier::NewCompartmentAndZone;
   union {
     Compartment* comp_;
     Zone* zone_;
   };
   bool invisibleToDebugger_ = false;
@@ -194,17 +188,16 @@ class JS_PUBLIC_API RealmCreationOptions
   bool sharedMemoryAndAtomics_ = false;
   bool streams_ = false;
   bool readableByteStreams_ = false;
   bool byobStreamReaders_ = false;
   bool writableStreams_ = false;
   bool fields_ = false;
   bool awaitFix_ = false;
   bool secureContext_ = false;
-  bool clampAndJitterTime_ = true;
 };
 
 /**
  * RealmBehaviors specifies behaviors of a realm that can be changed after the
  * realm's been created.
  */
 class JS_PUBLIC_API RealmBehaviors {
  public:
@@ -225,16 +218,22 @@ class JS_PUBLIC_API RealmBehaviors {
   }
 
   bool deferredParserAlloc() const { return deferredParserAlloc_; }
   RealmBehaviors& setDeferredParserAlloc(bool flag) {
     deferredParserAlloc_ = flag;
     return *this;
   }
 
+  bool clampAndJitterTime() const { return clampAndJitterTime_; }
+  RealmBehaviors& setClampAndJitterTime(bool flag) {
+    clampAndJitterTime_ = flag;
+    return *this;
+  }
+
   class Override {
    public:
     Override() : mode_(Default) {}
 
     bool get(bool defaultValue) const {
       if (mode_ == Default) {
         return defaultValue;
       }
@@ -269,16 +268,17 @@ class JS_PUBLIC_API RealmBehaviors {
   RealmBehaviors& setNonLive() {
     isNonLive_ = true;
     return *this;
   }
 
  private:
   bool discardSource_ = false;
   bool disableLazyParsing_ = false;
+  bool clampAndJitterTime_ = true;
   Override extraWarningsOverride_ = {};
 
   // To XDR singletons, we need to ensure that all singletons are all used as
   // templates, by making JSOP_OBJECT return a clone of the JSScript
   // singleton, instead of returning the value which is baked in the JSScript.
   bool singletonsAsTemplates_ = true;
   bool isNonLive_ = false;
   bool deferredParserAlloc_ = false;
--- a/js/src/jsdate.cpp
+++ b/js/src/jsdate.cpp
@@ -1406,17 +1406,17 @@ static bool date_parse(JSContext* cx, un
   }
 
   args.rval().set(TimeValue(result));
   return true;
 }
 
 static ClippedTime NowAsMillis(JSContext* cx) {
   double now = PRMJ_Now();
-  bool clampAndJitter = cx->realm()->creationOptions().clampAndJitterTime();
+  bool clampAndJitter = cx->realm()->behaviors().clampAndJitterTime();
   if (clampAndJitter && sReduceMicrosecondTimePrecisionCallback) {
     now = sReduceMicrosecondTimePrecisionCallback(now);
   } else if (clampAndJitter && sResolutionUsec) {
     double clamped = floor(now / sResolutionUsec) * sResolutionUsec;
 
     if (sJitter) {
       // Calculate a random midpoint for jittering. In the browser, we are
       // adversarial: Web Content may try to calculate the midpoint themselves
--- a/js/xpconnect/src/Sandbox.cpp
+++ b/js/xpconnect/src/Sandbox.cpp
@@ -1039,19 +1039,16 @@ nsresult xpc::CreateSandboxObject(JSCont
 
   auto& creationOptions = realmOptions.creationOptions();
 
   // XXXjwatt: Consider whether/when sandboxes should be able to see
   // [SecureContext] API (bug 1273687).  In that case we'd call
   // creationOptions.setSecureContext(true).
 
   bool isSystemPrincipal = principal->IsSystemPrincipal();
-  if (isSystemPrincipal) {
-    creationOptions.setClampAndJitterTime(false);
-  }
 
   xpc::SetPrefableRealmOptions(realmOptions);
   if (options.sameZoneAs) {
     creationOptions.setNewCompartmentInExistingZone(
         js::UncheckedUnwrap(options.sameZoneAs));
   } else if (options.freshZone) {
     creationOptions.setNewCompartmentAndZone();
   } else if (isSystemPrincipal && !options.invisibleToDebugger &&
@@ -1064,16 +1061,20 @@ nsresult xpc::CreateSandboxObject(JSCont
     creationOptions.setNewCompartmentInSystemZone();
   }
 
   creationOptions.setInvisibleToDebugger(options.invisibleToDebugger)
       .setTrace(TraceXPCGlobal);
 
   realmOptions.behaviors().setDiscardSource(options.discardSource);
 
+  if (isSystemPrincipal) {
+    realmOptions.behaviors().setClampAndJitterTime(false);
+  }
+
   const JSClass* clasp = &SandboxClass;
 
   RootedObject sandbox(
       cx, xpc::CreateGlobalObject(cx, clasp, principal, realmOptions));
   if (!sandbox) {
     return NS_ERROR_FAILURE;
   }
 
--- a/js/xpconnect/src/nsXPConnect.cpp
+++ b/js/xpconnect/src/nsXPConnect.cpp
@@ -485,17 +485,17 @@ void InitGlobalObjectOptions(JS::RealmOp
   bool shouldDiscardSystemSource = ShouldDiscardSystemSource();
   bool extraWarningsForSystemJS = ExtraWarningsForSystemJS();
 
   bool isSystem = nsContentUtils::IsSystemPrincipal(aPrincipal);
 
   if (isSystem) {
     // Make sure [SecureContext] APIs are visible:
     aOptions.creationOptions().setSecureContext(true);
-    aOptions.creationOptions().setClampAndJitterTime(false);
+    aOptions.behaviors().setClampAndJitterTime(false);
   }
 
   if (shouldDiscardSystemSource) {
     bool discardSource = isSystem;
 
     aOptions.behaviors().setDiscardSource(discardSource);
   }