2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
#include "RuntimeService.h"
|
|
|
|
|
Bug 1583949 - Add a check for IsEvalAllowed to the worker callpath for eval() r=ckerschb,baku
This patch does several things. Because Workers aren't on the main thread,
many of the things done are in the name of off main thread access.
1) Changes a parameter in IsEvalAllowed from a nsIPrincipal to a bool.
We only used the principal to determined if it was the System Principal.
Principals aren't thread safe and can only be accessed on Main Thread, so
if we passed a Principal in, we would be in error. Instead only pass in
the bool which - for workers - comes from a thread-safe location.
2) Separates out the Telemetry Event Recording and sending a message to the
console into a new function nsContentSecurityUtils::NotifyEvalUsage. (And
creates a runnable that calls it.)
We do this because we will need to only call this method on the main thread.
Telemetry Event Recording has only ever been called on the Main Thread.
While I possibly-successfully cut it over to happen Off Main Thread (OMT)
by porting preferences to StaticPrefs, I don't know if there were other
threading assumptions in the Telemetry Code. So it would be much safer to
just continue recording Event Telemetry on the main thread.
Sending a message to the console requires calling GetStringBundleService()
which requires main thread. I didn't investigate if this could be made
thread-safe, I just threw it onto the main thread too.
If, in IsEvalAllowed, we are on the main thread - we call NotifyEvalUsage
directly. If we are not, we create a runnable which will then call
NotifyEvalUsage for us on the main thread.
3) Ports allow_eval_with_system_principal and allow_eval_in_parent_process
from bools to RelaxedAtomicBool - because we now check these prefs OMT.
4) In RuntimeService.cpp, adds the call to IsEvalAllowed.
5) Add resource://gre/modules/workers/require.js to the allowlist of eval
usage. This was the script that identified this gap in the first place.
It uses eval (twice) for structural reasons (scope and line number
massaging.) The contents of the eval are the result of a request to a
uri (which may be internal, like resource://). The whole point of this
is to implement a CommonJS require() api.
This usage of eval is safe because the only way an attacker can inject
into it is by either controlling the response of the uri request or
controlling (or appending to) the argument. If they can do that, they
are able to inject script into Firefox even if we cut this usage of eval
over to some other type of safe(r) script loader.
Bug 1584564 tracks making sure calls to require.js are safe.
6) Adds cld-worker.js to the allowlist. Bug 1584605 is for refactoring that
eval usage, which is decidedly non-trivial.
7) Does _not_ enforce the eval restrictions for workers. While I've gotten
try to be green and not throw up any instances of eval-usage by workers,
it is much safer to deploy this is Telemetry-only mode for Workers for
a little bit to see if anything pops up from the Nightly population.
Bug 1584602 is for enforcing the checks.
Differential Revision: https://phabricator.services.mozilla.com/D47480
--HG--
extra : moz-landing-system : lando
2019-10-08 20:31:35 +03:00
|
|
|
#include "nsContentSecurityUtils.h"
|
2012-09-15 22:51:55 +04:00
|
|
|
#include "nsIContentSecurityPolicy.h"
|
2019-01-02 16:05:23 +03:00
|
|
|
#include "mozilla/dom/Document.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
#include "nsIObserverService.h"
|
2013-06-05 18:04:23 +04:00
|
|
|
#include "nsIScriptContext.h"
|
2017-03-08 20:37:08 +03:00
|
|
|
#include "nsIStreamTransportService.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
#include "nsISupportsPriority.h"
|
2011-07-26 05:49:16 +04:00
|
|
|
#include "nsITimer.h"
|
2013-06-05 18:04:23 +04:00
|
|
|
#include "nsIURI.h"
|
2017-10-06 19:50:50 +03:00
|
|
|
#include "nsIXULRuntime.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
#include "nsPIDOMWindow.h"
|
|
|
|
|
2013-05-17 02:49:43 +04:00
|
|
|
#include <algorithm>
|
2018-01-27 00:08:58 +03:00
|
|
|
#include "mozilla/ipc/BackgroundChild.h"
|
2013-05-17 02:49:43 +04:00
|
|
|
#include "GeckoProfiler.h"
|
2012-12-30 22:21:52 +04:00
|
|
|
#include "jsfriendapi.h"
|
2019-01-24 03:56:56 +03:00
|
|
|
#include "js/ContextOptions.h"
|
2018-08-21 01:11:32 +03:00
|
|
|
#include "js/LocaleSensitive.h"
|
2013-12-09 06:52:54 +04:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2016-03-16 22:41:38 +03:00
|
|
|
#include "mozilla/Atomics.h"
|
2019-03-19 18:14:11 +03:00
|
|
|
#include "mozilla/Attributes.h"
|
2016-09-14 16:47:32 +03:00
|
|
|
#include "mozilla/CycleCollectedJSContext.h"
|
2017-02-24 00:23:45 +03:00
|
|
|
#include "mozilla/CycleCollectedJSRuntime.h"
|
2015-03-25 20:17:00 +03:00
|
|
|
#include "mozilla/Telemetry.h"
|
2014-11-15 05:47:30 +03:00
|
|
|
#include "mozilla/TimeStamp.h"
|
2013-08-23 09:17:09 +04:00
|
|
|
#include "mozilla/dom/AtomList.h"
|
2013-05-17 02:49:43 +04:00
|
|
|
#include "mozilla/dom/BindingUtils.h"
|
2013-11-05 18:16:24 +04:00
|
|
|
#include "mozilla/dom/ErrorEventBinding.h"
|
2012-05-03 08:35:38 +04:00
|
|
|
#include "mozilla/dom/EventTargetBinding.h"
|
2017-10-10 22:41:24 +03:00
|
|
|
#include "mozilla/dom/FetchUtil.h"
|
2015-09-16 06:27:56 +03:00
|
|
|
#include "mozilla/dom/MessageChannel.h"
|
2013-11-05 18:16:24 +04:00
|
|
|
#include "mozilla/dom/MessageEventBinding.h"
|
2017-12-18 19:49:54 +03:00
|
|
|
#include "mozilla/dom/PerformanceService.h"
|
2018-11-20 02:18:33 +03:00
|
|
|
#include "mozilla/dom/RemoteWorkerChild.h"
|
2013-11-05 18:16:24 +04:00
|
|
|
#include "mozilla/dom/WorkerBinding.h"
|
2014-06-30 19:21:27 +04:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2016-02-17 00:46:08 +03:00
|
|
|
#include "mozilla/dom/IndexedDatabaseManager.h"
|
2014-12-17 09:26:15 +03:00
|
|
|
#include "mozilla/ipc/BackgroundChild.h"
|
2013-05-17 02:49:43 +04:00
|
|
|
#include "mozilla/DebugOnly.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2013-11-19 23:10:15 +04:00
|
|
|
#include "mozilla/dom/Navigator.h"
|
2017-11-15 09:58:38 +03:00
|
|
|
#include "mozilla/Monitor.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
#include "nsContentUtils.h"
|
2013-08-04 03:55:40 +04:00
|
|
|
#include "nsCycleCollector.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
#include "nsDOMJSUtils.h"
|
2014-02-27 01:36:35 +04:00
|
|
|
#include "nsISupportsImpl.h"
|
2013-05-17 02:49:43 +04:00
|
|
|
#include "nsLayoutStatics.h"
|
2016-03-16 21:51:11 +03:00
|
|
|
#include "nsNetUtil.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsXPCOM.h"
|
|
|
|
#include "nsXPCOMPrivate.h"
|
2013-05-17 02:49:43 +04:00
|
|
|
#include "OSFileConstants.h"
|
2011-08-02 08:06:17 +04:00
|
|
|
#include "xpcpublic.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2019-05-08 01:16:28 +03:00
|
|
|
#if defined(XP_MACOSX)
|
2019-05-24 14:26:01 +03:00
|
|
|
# include "nsMacUtilsImpl.h"
|
2019-05-08 01:16:28 +03:00
|
|
|
#endif
|
|
|
|
|
2014-09-15 19:49:11 +04:00
|
|
|
#include "Principal.h"
|
2015-03-17 13:15:19 +03:00
|
|
|
#include "WorkerDebuggerManager.h"
|
2018-07-18 13:07:14 +03:00
|
|
|
#include "WorkerError.h"
|
2018-01-30 12:12:04 +03:00
|
|
|
#include "WorkerLoadInfo.h"
|
2013-10-23 17:16:49 +04:00
|
|
|
#include "WorkerPrivate.h"
|
|
|
|
#include "WorkerRunnable.h"
|
2016-03-16 21:51:11 +03:00
|
|
|
#include "WorkerScope.h"
|
2014-11-15 05:47:30 +03:00
|
|
|
#include "WorkerThread.h"
|
2016-03-16 22:41:38 +03:00
|
|
|
#include "prsystem.h"
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2018-01-30 12:14:00 +03:00
|
|
|
#define WORKERS_SHUTDOWN_TOPIC "web-workers-shutdown"
|
|
|
|
|
2018-01-31 10:24:30 +03:00
|
|
|
namespace mozilla {
|
2011-10-11 09:50:08 +04:00
|
|
|
|
2018-01-31 10:24:30 +03:00
|
|
|
using namespace ipc;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2018-01-31 10:24:30 +03:00
|
|
|
namespace dom {
|
2018-01-31 10:24:59 +03:00
|
|
|
|
|
|
|
using namespace workerinternals;
|
|
|
|
|
|
|
|
namespace workerinternals {
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-01-04 23:11:32 +04:00
|
|
|
// The size of the worker runtime heaps in bytes. May be changed via pref.
|
|
|
|
#define WORKER_DEFAULT_RUNTIME_HEAPSIZE 32 * 1024 * 1024
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-01-11 02:50:40 +04:00
|
|
|
// The size of the worker JS allocation threshold in MB. May be changed via
|
|
|
|
// pref.
|
|
|
|
#define WORKER_DEFAULT_ALLOCATION_THRESHOLD 30
|
|
|
|
|
2012-03-08 03:11:15 +04:00
|
|
|
// Half the size of the actual C stack, to be safe.
|
2011-08-16 09:53:33 +04:00
|
|
|
#define WORKER_CONTEXT_NATIVE_STACK_LIMIT 128 * sizeof(size_t) * 1024
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2016-08-19 09:38:58 +03:00
|
|
|
// The maximum number of hardware concurrency, overridable via pref.
|
|
|
|
#define MAX_HARDWARE_CONCURRENCY 8
|
|
|
|
|
|
|
|
// The maximum number of threads to use for workers, overridable via pref.
|
|
|
|
#define MAX_WORKERS_PER_DOMAIN 512
|
|
|
|
|
|
|
|
static_assert(MAX_WORKERS_PER_DOMAIN >= 1,
|
|
|
|
"We should allow at least one worker per domain.");
|
|
|
|
|
2013-05-17 02:49:43 +04:00
|
|
|
// The default number of seconds that close handlers will be allowed to run for
|
|
|
|
// content workers.
|
2011-07-17 23:09:13 +04:00
|
|
|
#define MAX_SCRIPT_RUN_TIME_SEC 10
|
|
|
|
|
2011-07-26 05:49:16 +04:00
|
|
|
// The number of seconds that idle threads can hang around before being killed.
|
|
|
|
#define IDLE_THREAD_TIMEOUT_SEC 30
|
|
|
|
|
|
|
|
// The maximum number of threads that can be idle at one time.
|
|
|
|
#define MAX_IDLE_THREADS 20
|
|
|
|
|
2013-05-17 02:49:43 +04:00
|
|
|
#define PREF_WORKERS_PREFIX "dom.workers."
|
2016-08-19 09:38:58 +03:00
|
|
|
#define PREF_WORKERS_MAX_PER_DOMAIN PREF_WORKERS_PREFIX "maxPerDomain"
|
|
|
|
#define PREF_WORKERS_MAX_HARDWARE_CONCURRENCY "dom.maxHardwareConcurrency"
|
2013-05-17 02:49:43 +04:00
|
|
|
|
|
|
|
#define PREF_MAX_SCRIPT_RUN_TIME_CONTENT "dom.max_script_run_time"
|
|
|
|
#define PREF_MAX_SCRIPT_RUN_TIME_CHROME "dom.max_chrome_script_run_time"
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-01-18 00:05:25 +04:00
|
|
|
#define GC_REQUEST_OBSERVER_TOPIC "child-gc-request"
|
2013-12-03 08:07:02 +04:00
|
|
|
#define CC_REQUEST_OBSERVER_TOPIC "child-cc-request"
|
2012-01-18 00:05:25 +04:00
|
|
|
#define MEMORY_PRESSURE_OBSERVER_TOPIC "memory-pressure"
|
2019-06-24 20:24:47 +03:00
|
|
|
#define LOW_MEMORY_DATA "low-memory"
|
|
|
|
#define LOW_MEMORY_ONGOING_DATA "low-memory-ongoing"
|
|
|
|
#define MEMORY_PRESSURE_STOP_OBSERVER_TOPIC "memory-pressure-stop"
|
2012-01-18 00:05:25 +04:00
|
|
|
|
|
|
|
#define BROADCAST_ALL_WORKERS(_func, ...) \
|
|
|
|
PR_BEGIN_MACRO \
|
|
|
|
AssertIsOnMainThread(); \
|
|
|
|
\
|
2018-02-05 21:55:07 +03:00
|
|
|
AutoTArray<WorkerPrivate*, 100> workers; \
|
2012-01-18 00:05:25 +04:00
|
|
|
{ \
|
|
|
|
MutexAutoLock lock(mMutex); \
|
|
|
|
\
|
2015-10-27 02:41:55 +03:00
|
|
|
AddAllTopLevelWorkersToArray(workers); \
|
2012-01-18 00:05:25 +04:00
|
|
|
} \
|
|
|
|
\
|
|
|
|
if (!workers.IsEmpty()) { \
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t index = 0; index < workers.Length(); index++) { \
|
2016-02-26 23:23:12 +03:00
|
|
|
workers[index]->_func(__VA_ARGS__); \
|
2012-01-18 00:05:25 +04:00
|
|
|
} \
|
|
|
|
} \
|
|
|
|
PR_END_MACRO
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-05-17 02:49:43 +04:00
|
|
|
// Prefixes for observing preference changes.
|
|
|
|
#define PREF_JS_OPTIONS_PREFIX "javascript.options."
|
|
|
|
#define PREF_WORKERS_OPTIONS_PREFIX PREF_WORKERS_PREFIX "options."
|
|
|
|
#define PREF_MEM_OPTIONS_PREFIX "mem."
|
|
|
|
#define PREF_GCZEAL "gcZeal"
|
|
|
|
|
2017-03-08 20:37:08 +03:00
|
|
|
static NS_DEFINE_CID(kStreamTransportServiceCID, NS_STREAMTRANSPORTSERVICE_CID);
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
namespace {
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
const uint32_t kNoIndex = uint32_t(-1);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2016-08-19 09:38:58 +03:00
|
|
|
uint32_t gMaxWorkersPerDomain = MAX_WORKERS_PER_DOMAIN;
|
|
|
|
uint32_t gMaxHardwareConcurrency = MAX_HARDWARE_CONCURRENCY;
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
// Does not hold an owning reference.
|
2012-07-30 18:20:58 +04:00
|
|
|
RuntimeService* gRuntimeService = nullptr;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2014-10-27 12:38:14 +03:00
|
|
|
// Only true during the call to Init.
|
|
|
|
bool gRuntimeServiceDuringInit = false;
|
|
|
|
|
2013-05-17 02:49:43 +04:00
|
|
|
class LiteralRebindingCString : public nsDependentCString {
|
|
|
|
public:
|
|
|
|
template <int N>
|
|
|
|
void RebindLiteral(const char (&aStr)[N]) {
|
|
|
|
Rebind(aStr, N - 1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct PrefTraits;
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct PrefTraits<bool> {
|
|
|
|
typedef bool PrefValueType;
|
|
|
|
|
|
|
|
static const PrefValueType kDefaultValue = false;
|
|
|
|
|
|
|
|
static inline PrefValueType Get(const char* aPref) {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
return Preferences::GetBool(aPref);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool Exists(const char* aPref) {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
return Preferences::GetType(aPref) == nsIPrefBranch::PREF_BOOL;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct PrefTraits<int32_t> {
|
|
|
|
typedef int32_t PrefValueType;
|
|
|
|
|
|
|
|
static inline PrefValueType Get(const char* aPref) {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
return Preferences::GetInt(aPref);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool Exists(const char* aPref) {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
return Preferences::GetType(aPref) == nsIPrefBranch::PREF_INT;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
T GetWorkerPref(const nsACString& aPref,
|
2020-04-30 12:18:27 +03:00
|
|
|
const T aDefault = PrefTraits<T>::kDefaultValue,
|
|
|
|
bool* aPresent = nullptr) {
|
2013-05-17 02:49:43 +04:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
typedef PrefTraits<T> PrefHelper;
|
|
|
|
|
|
|
|
T result;
|
2020-04-30 12:18:27 +03:00
|
|
|
bool present = true;
|
2013-05-17 02:49:43 +04:00
|
|
|
|
|
|
|
nsAutoCString prefName;
|
|
|
|
prefName.AssignLiteral(PREF_WORKERS_OPTIONS_PREFIX);
|
|
|
|
prefName.Append(aPref);
|
|
|
|
|
|
|
|
if (PrefHelper::Exists(prefName.get())) {
|
|
|
|
result = PrefHelper::Get(prefName.get());
|
|
|
|
} else {
|
|
|
|
prefName.AssignLiteral(PREF_JS_OPTIONS_PREFIX);
|
|
|
|
prefName.Append(aPref);
|
|
|
|
|
|
|
|
if (PrefHelper::Exists(prefName.get())) {
|
|
|
|
result = PrefHelper::Get(prefName.get());
|
|
|
|
} else {
|
|
|
|
result = aDefault;
|
2020-04-30 12:18:27 +03:00
|
|
|
present = false;
|
2013-05-17 02:49:43 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-30 12:18:27 +03:00
|
|
|
if (aPresent) {
|
|
|
|
*aPresent = present;
|
|
|
|
}
|
2013-05-17 02:49:43 +04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-07-07 09:15:15 +03:00
|
|
|
void LoadContextOptions(const char* aPrefName, void* /* aClosure */) {
|
2013-05-17 02:49:43 +04:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
RuntimeService* rts = RuntimeService::GetService();
|
2014-10-08 16:56:50 +04:00
|
|
|
if (!rts) {
|
2013-05-17 02:49:43 +04:00
|
|
|
// May be shutting down, just bail.
|
2013-12-11 03:10:01 +04:00
|
|
|
return;
|
2013-05-17 02:49:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
const nsDependentCString prefName(aPrefName);
|
|
|
|
|
|
|
|
// Several other pref branches will get included here so bail out if there is
|
|
|
|
// another callback that will handle this change.
|
|
|
|
if (StringBeginsWith(
|
|
|
|
prefName,
|
|
|
|
NS_LITERAL_CSTRING(PREF_JS_OPTIONS_PREFIX PREF_MEM_OPTIONS_PREFIX)) ||
|
|
|
|
StringBeginsWith(
|
|
|
|
prefName, NS_LITERAL_CSTRING(
|
2013-12-25 12:21:59 +04:00
|
|
|
PREF_WORKERS_OPTIONS_PREFIX PREF_MEM_OPTIONS_PREFIX))) {
|
2013-12-11 03:10:01 +04:00
|
|
|
return;
|
2013-05-17 02:49:43 +04:00
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
#ifdef JS_GC_ZEAL
|
2013-05-17 02:49:43 +04:00
|
|
|
if (prefName.EqualsLiteral(PREF_JS_OPTIONS_PREFIX PREF_GCZEAL) ||
|
|
|
|
prefName.EqualsLiteral(PREF_WORKERS_OPTIONS_PREFIX PREF_GCZEAL)) {
|
2013-12-11 03:10:01 +04:00
|
|
|
return;
|
2013-05-17 02:49:43 +04:00
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
#endif
|
|
|
|
|
2016-07-07 09:15:15 +03:00
|
|
|
// Context options.
|
|
|
|
JS::ContextOptions contextOptions;
|
2020-03-20 20:13:51 +03:00
|
|
|
contextOptions
|
|
|
|
.setAsmJS(GetWorkerPref<bool>(NS_LITERAL_CSTRING("asmjs")))
|
2020-03-11 15:20:21 +03:00
|
|
|
#ifdef FUZZING
|
|
|
|
.setFuzzing(GetWorkerPref<bool>(NS_LITERAL_CSTRING("fuzzing.enabled")))
|
|
|
|
#endif
|
2016-03-03 19:20:21 +03:00
|
|
|
.setWasm(GetWorkerPref<bool>(NS_LITERAL_CSTRING("wasm")))
|
2019-10-04 20:35:46 +03:00
|
|
|
.setWasmForTrustedPrinciples(
|
2019-10-04 20:36:08 +03:00
|
|
|
GetWorkerPref<bool>(NS_LITERAL_CSTRING("wasm_trustedprincipals")))
|
2017-02-02 15:22:40 +03:00
|
|
|
.setWasmBaseline(
|
|
|
|
GetWorkerPref<bool>(NS_LITERAL_CSTRING("wasm_baselinejit")))
|
2017-06-30 01:37:01 +03:00
|
|
|
.setWasmIon(GetWorkerPref<bool>(NS_LITERAL_CSTRING("wasm_ionjit")))
|
2020-04-30 14:55:13 +03:00
|
|
|
.setWasmReftypes(GetWorkerPref<bool>(NS_LITERAL_CSTRING("wasm_reftypes")))
|
2018-09-13 13:32:17 +03:00
|
|
|
#ifdef ENABLE_WASM_CRANELIFT
|
2019-01-23 14:33:01 +03:00
|
|
|
.setWasmCranelift(
|
2018-09-13 13:32:17 +03:00
|
|
|
GetWorkerPref<bool>(NS_LITERAL_CSTRING("wasm_cranelift")))
|
|
|
|
#endif
|
2020-03-26 14:47:59 +03:00
|
|
|
#ifdef ENABLE_WASM_MULTI_VALUE
|
|
|
|
.setWasmMultiValue(
|
|
|
|
GetWorkerPref<bool>(NS_LITERAL_CSTRING("wasm_multi_value")))
|
|
|
|
#endif
|
2020-05-05 11:17:47 +03:00
|
|
|
#ifdef ENABLE_WASM_SIMD
|
|
|
|
.setWasmSimd(GetWorkerPref<bool>(NS_LITERAL_CSTRING("wasm_simd")))
|
|
|
|
#endif
|
Bug 1508561 - Disentangle support for reftypes and gc. r=jseward
This does the following:
- It introduces a controlling ifdef ENABLE_WASM_REFTYPES that enables
exactly those features that are in the reftypes proposal, excluding
those in the gc proposal. Any remaining features (namely, ref.eq,
(ref T) types, struct types) are still under ENABLE_WASM_GC control.
ENABLE_WASM_GC requires ENABLE_WASM_REFTYPES and this is checked.
- It introduces a new TestingFunctions predicate, wasmReftypesEnabled,
that distinguishes reftype-proposal support from gc-proposal
support. We keep wasmGcEnabled to test for gc-proposal support.
- It segregates test cases so that gc-proposal relevant tests are in
their own files, and tests relevant to the reftypes-proposal are now
guarded by wasmReftypesEnabled.
- It renames the predicate HasGcSupport() as HasReftypesSupport(),
since that is what the predicate tests for.
- It has a drive-by fix for the DEBUG-only function wasm::Classify()
to properly put ref.null and ref.is_null under ifdef control.
Reftypes will soon be enabled unconditionally in Nightly (once we can
trace pointers from Ion frames) while gc-types will remain conditional
until Ion supports all the new instructions for struct types. Therefore:
- The command line switch and about:config option are still called
--wasm-gc and j.o.wasm_gc, respectively, which is fine since they will
fairly soon control only gc-proposal features.
- Internal names still use "Gc" rather than "Reftypes", eg,
HasGcTypes, wasmGc_, and so on. This is most appropriate since it
reduces the scope of the patch and these names will pertain mainly
to the gc feature in the future.
--HG--
extra : rebase_source : 51cf3bfe67da594e89195472e4ce1ccfa36c146d
2018-12-18 19:26:32 +03:00
|
|
|
#ifdef ENABLE_WASM_REFTYPES
|
2018-03-21 16:32:47 +03:00
|
|
|
.setWasmGc(GetWorkerPref<bool>(NS_LITERAL_CSTRING("wasm_gc")))
|
|
|
|
#endif
|
2018-12-21 05:51:11 +03:00
|
|
|
.setWasmVerbose(GetWorkerPref<bool>(NS_LITERAL_CSTRING("wasm_verbose")))
|
2015-09-28 13:40:03 +03:00
|
|
|
.setThrowOnAsmJSValidationFailure(GetWorkerPref<bool>(
|
|
|
|
NS_LITERAL_CSTRING("throw_on_asmjs_validation_failure")))
|
2020-05-08 03:37:21 +03:00
|
|
|
.setSourcePragmas(
|
|
|
|
GetWorkerPref<bool>(NS_LITERAL_CSTRING("source_pragmas")))
|
2020-03-11 15:20:21 +03:00
|
|
|
.setAsyncStack(GetWorkerPref<bool>(NS_LITERAL_CSTRING("asyncstack")));
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2017-10-06 19:50:50 +03:00
|
|
|
nsCOMPtr<nsIXULRuntime> xr = do_GetService("@mozilla.org/xre/runtime;1");
|
|
|
|
if (xr) {
|
|
|
|
bool safeMode = false;
|
|
|
|
xr->GetInSafeMode(&safeMode);
|
|
|
|
if (safeMode) {
|
|
|
|
contextOptions.disableOptionsForSafeMode();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-07 09:15:15 +03:00
|
|
|
RuntimeService::SetDefaultContextOptions(contextOptions);
|
2013-05-17 02:49:43 +04:00
|
|
|
|
|
|
|
if (rts) {
|
2016-07-07 09:15:15 +03:00
|
|
|
rts->UpdateAllWorkerContextOptions();
|
2013-05-17 02:49:43 +04:00
|
|
|
}
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
#ifdef JS_GC_ZEAL
|
2013-05-17 02:49:43 +04:00
|
|
|
void LoadGCZealOptions(const char* /* aPrefName */, void* /* aClosure */) {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
RuntimeService* rts = RuntimeService::GetService();
|
2014-10-08 16:56:50 +04:00
|
|
|
if (!rts) {
|
2013-05-17 02:49:43 +04:00
|
|
|
// May be shutting down, just bail.
|
2013-12-11 03:10:01 +04:00
|
|
|
return;
|
2013-05-17 02:49:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t gczeal = GetWorkerPref<int32_t>(NS_LITERAL_CSTRING(PREF_GCZEAL), -1);
|
|
|
|
if (gczeal < 0) {
|
|
|
|
gczeal = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t frequency =
|
|
|
|
GetWorkerPref<int32_t>(NS_LITERAL_CSTRING("gcZeal.frequency"), -1);
|
|
|
|
if (frequency < 0) {
|
|
|
|
frequency = JS_DEFAULT_ZEAL_FREQ;
|
|
|
|
}
|
|
|
|
|
|
|
|
RuntimeService::SetDefaultGCZeal(uint8_t(gczeal), uint32_t(frequency));
|
|
|
|
|
|
|
|
if (rts) {
|
|
|
|
rts->UpdateAllWorkerGCZeal();
|
|
|
|
}
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
#endif
|
|
|
|
|
2013-05-17 02:49:43 +04:00
|
|
|
void UpdateCommonJSGCMemoryOption(RuntimeService* aRuntimeService,
|
|
|
|
const nsACString& aPrefName,
|
|
|
|
JSGCParamKey aKey) {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
NS_ASSERTION(!aPrefName.IsEmpty(), "Empty pref name!");
|
|
|
|
|
|
|
|
int32_t prefValue = GetWorkerPref(aPrefName, -1);
|
2020-04-30 12:18:27 +03:00
|
|
|
Maybe<uint32_t> value = (prefValue < 0 || prefValue >= 10000)
|
|
|
|
? Nothing()
|
|
|
|
: Some(uint32_t(prefValue));
|
2013-05-17 02:49:43 +04:00
|
|
|
|
|
|
|
RuntimeService::SetDefaultJSGCSettings(aKey, value);
|
|
|
|
|
|
|
|
if (aRuntimeService) {
|
|
|
|
aRuntimeService->UpdateAllWorkerMemoryParameter(aKey, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-02 17:39:34 +03:00
|
|
|
void UpdateOtherJSGCMemoryOption(RuntimeService* aRuntimeService,
|
2020-04-30 12:18:27 +03:00
|
|
|
JSGCParamKey aKey, Maybe<uint32_t> aValue) {
|
2013-05-17 02:49:43 +04:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
RuntimeService::SetDefaultJSGCSettings(aKey, aValue);
|
|
|
|
|
|
|
|
if (aRuntimeService) {
|
|
|
|
aRuntimeService->UpdateAllWorkerMemoryParameter(aKey, aValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LoadJSGCMemoryOptions(const char* aPrefName, void* /* aClosure */) {
|
2011-07-17 23:09:13 +04:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2013-05-17 02:49:43 +04:00
|
|
|
RuntimeService* rts = RuntimeService::GetService();
|
|
|
|
|
2014-10-08 16:56:50 +04:00
|
|
|
if (!rts) {
|
2013-05-17 02:49:43 +04:00
|
|
|
// May be shutting down, just bail.
|
2013-12-11 03:10:01 +04:00
|
|
|
return;
|
2013-05-17 02:49:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_NAMED_LITERAL_CSTRING(jsPrefix, PREF_JS_OPTIONS_PREFIX);
|
|
|
|
NS_NAMED_LITERAL_CSTRING(workersPrefix, PREF_WORKERS_OPTIONS_PREFIX);
|
|
|
|
|
|
|
|
const nsDependentCString fullPrefName(aPrefName);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-05-17 02:49:43 +04:00
|
|
|
// Pull out the string that actually distinguishes the parameter we need to
|
|
|
|
// change.
|
|
|
|
nsDependentCSubstring memPrefName;
|
|
|
|
if (StringBeginsWith(fullPrefName, jsPrefix)) {
|
|
|
|
memPrefName.Rebind(fullPrefName, jsPrefix.Length());
|
|
|
|
} else if (StringBeginsWith(fullPrefName, workersPrefix)) {
|
|
|
|
memPrefName.Rebind(fullPrefName, workersPrefix.Length());
|
|
|
|
} else {
|
|
|
|
NS_ERROR("Unknown pref name!");
|
2013-12-11 03:10:01 +04:00
|
|
|
return;
|
2013-05-17 02:49:43 +04:00
|
|
|
}
|
|
|
|
|
2020-04-30 12:18:34 +03:00
|
|
|
struct WorkerGCPref {
|
|
|
|
nsLiteralCString name;
|
|
|
|
JSGCParamKey key;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define PREF(suffix_, key_) \
|
|
|
|
{ nsLiteralCString(PREF_MEM_OPTIONS_PREFIX suffix_), key_ }
|
|
|
|
constexpr WorkerGCPref kWorkerPrefs[] = {
|
|
|
|
PREF("max", JSGC_MAX_BYTES),
|
|
|
|
PREF("gc_high_frequency_time_limit_ms", JSGC_HIGH_FREQUENCY_TIME_LIMIT),
|
|
|
|
PREF("gc_low_frequency_heap_growth", JSGC_LOW_FREQUENCY_HEAP_GROWTH),
|
|
|
|
PREF("gc_high_frequency_large_heap_growth",
|
|
|
|
JSGC_HIGH_FREQUENCY_LARGE_HEAP_GROWTH),
|
|
|
|
PREF("gc_high_frequency_small_heap_growth",
|
|
|
|
JSGC_HIGH_FREQUENCY_SMALL_HEAP_GROWTH),
|
|
|
|
PREF("gc_small_heap_size_max_mb", JSGC_SMALL_HEAP_SIZE_MAX),
|
|
|
|
PREF("gc_large_heap_size_min_mb", JSGC_LARGE_HEAP_SIZE_MIN),
|
|
|
|
PREF("gc_allocation_threshold_mb", JSGC_ALLOCATION_THRESHOLD),
|
|
|
|
PREF("gc_incremental_slice_ms", JSGC_SLICE_TIME_BUDGET_MS),
|
|
|
|
PREF("gc_min_empty_chunk_count", JSGC_MIN_EMPTY_CHUNK_COUNT),
|
|
|
|
PREF("gc_max_empty_chunk_count", JSGC_MAX_EMPTY_CHUNK_COUNT),
|
|
|
|
PREF("gc_compacting", JSGC_COMPACTING_ENABLED),
|
|
|
|
};
|
|
|
|
#undef PREF
|
|
|
|
|
|
|
|
auto pref = kWorkerPrefs;
|
|
|
|
auto end = kWorkerPrefs + ArrayLength(kWorkerPrefs);
|
|
|
|
|
|
|
|
if (gRuntimeServiceDuringInit) {
|
|
|
|
// During init, we want to update every pref in kWorkerPrefs.
|
|
|
|
MOZ_ASSERT(memPrefName.EqualsLiteral(PREF_MEM_OPTIONS_PREFIX),
|
|
|
|
"Pref branch prefix only expected during init");
|
|
|
|
} else {
|
|
|
|
// Otherwise, find the single pref that changed.
|
|
|
|
while (pref != end) {
|
|
|
|
if (pref->name == memPrefName) {
|
|
|
|
end = pref + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++pref;
|
2014-07-16 13:01:20 +04:00
|
|
|
}
|
2020-04-30 12:18:34 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
if (pref == end) {
|
|
|
|
nsAutoCString message("Workers don't support the '");
|
|
|
|
message.Append(memPrefName);
|
|
|
|
message.AppendLiteral("' preference!");
|
|
|
|
NS_WARNING(message.get());
|
2014-07-16 13:01:20 +04:00
|
|
|
}
|
2020-04-30 12:18:34 +03:00
|
|
|
#endif
|
|
|
|
}
|
2014-07-16 13:01:20 +04:00
|
|
|
|
2020-04-30 12:18:34 +03:00
|
|
|
while (pref != end) {
|
|
|
|
switch (pref->key) {
|
|
|
|
case JSGC_MAX_BYTES: {
|
|
|
|
int32_t prefValue = GetWorkerPref(pref->name, -1);
|
|
|
|
Maybe<uint32_t> value = (prefValue <= 0 || prefValue >= 0x1000)
|
|
|
|
? Nothing()
|
|
|
|
: Some(uint32_t(prefValue) * 1024 * 1024);
|
|
|
|
UpdateOtherJSGCMemoryOption(rts, pref->key, value);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case JSGC_SLICE_TIME_BUDGET_MS: {
|
|
|
|
int32_t prefValue = GetWorkerPref(pref->name, -1);
|
|
|
|
Maybe<uint32_t> value = (prefValue <= 0 || prefValue >= 100000)
|
|
|
|
? Nothing()
|
|
|
|
: Some(uint32_t(prefValue));
|
|
|
|
UpdateOtherJSGCMemoryOption(rts, pref->key, value);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case JSGC_COMPACTING_ENABLED: {
|
|
|
|
bool present;
|
|
|
|
bool prefValue = GetWorkerPref(pref->name, false, &present);
|
|
|
|
Maybe<uint32_t> value = present ? Some(prefValue ? 1 : 0) : Nothing();
|
|
|
|
UpdateOtherJSGCMemoryOption(rts, pref->key, value);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case JSGC_HIGH_FREQUENCY_TIME_LIMIT:
|
|
|
|
case JSGC_LOW_FREQUENCY_HEAP_GROWTH:
|
|
|
|
case JSGC_HIGH_FREQUENCY_LARGE_HEAP_GROWTH:
|
|
|
|
case JSGC_HIGH_FREQUENCY_SMALL_HEAP_GROWTH:
|
|
|
|
case JSGC_SMALL_HEAP_SIZE_MAX:
|
|
|
|
case JSGC_LARGE_HEAP_SIZE_MIN:
|
|
|
|
case JSGC_ALLOCATION_THRESHOLD:
|
|
|
|
case JSGC_MIN_EMPTY_CHUNK_COUNT:
|
|
|
|
case JSGC_MAX_EMPTY_CHUNK_COUNT:
|
|
|
|
UpdateCommonJSGCMemoryOption(rts, pref->name, pref->key);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Unknown JSGCParamKey value");
|
|
|
|
break;
|
2015-02-02 17:39:34 +03:00
|
|
|
}
|
2020-04-30 12:18:34 +03:00
|
|
|
++pref;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
2013-05-17 02:49:43 +04:00
|
|
|
}
|
|
|
|
|
2014-03-11 01:28:43 +04:00
|
|
|
bool InterruptCallback(JSContext* aCx) {
|
2011-07-17 23:09:13 +04:00
|
|
|
WorkerPrivate* worker = GetWorkerPrivateFromContext(aCx);
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(worker);
|
2013-04-04 02:59:17 +04:00
|
|
|
|
|
|
|
// Now is a good time to turn on profiling if it's pending.
|
2017-10-04 01:11:18 +03:00
|
|
|
PROFILER_JS_INTERRUPT_CALLBACK();
|
2013-04-04 02:59:17 +04:00
|
|
|
|
2014-03-11 01:28:43 +04:00
|
|
|
return worker->InterruptCallback(aCx);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2016-05-03 11:21:57 +03:00
|
|
|
class LogViolationDetailsRunnable final : public WorkerMainThreadRunnable {
|
2012-09-15 22:51:55 +04:00
|
|
|
nsString mFileName;
|
|
|
|
uint32_t mLineNum;
|
2018-07-05 09:21:04 +03:00
|
|
|
uint32_t mColumnNum;
|
2018-07-16 18:58:04 +03:00
|
|
|
nsString mScriptSample;
|
2012-09-15 22:51:55 +04:00
|
|
|
|
|
|
|
public:
|
|
|
|
LogViolationDetailsRunnable(WorkerPrivate* aWorker, const nsString& aFileName,
|
2018-07-16 18:58:04 +03:00
|
|
|
uint32_t aLineNum, uint32_t aColumnNum,
|
|
|
|
const nsAString& aScriptSample)
|
2016-05-03 11:21:57 +03:00
|
|
|
: WorkerMainThreadRunnable(
|
|
|
|
aWorker,
|
|
|
|
NS_LITERAL_CSTRING("RuntimeService :: LogViolationDetails")),
|
2018-07-05 09:21:04 +03:00
|
|
|
mFileName(aFileName),
|
|
|
|
mLineNum(aLineNum),
|
|
|
|
mColumnNum(aColumnNum),
|
2018-07-16 18:58:04 +03:00
|
|
|
mScriptSample(aScriptSample) {
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(aWorker);
|
2012-09-15 22:51:55 +04:00
|
|
|
}
|
|
|
|
|
2016-05-03 11:21:57 +03:00
|
|
|
virtual bool MainThreadRun() override;
|
2012-09-22 16:45:00 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
private:
|
2020-02-12 13:38:22 +03:00
|
|
|
~LogViolationDetailsRunnable() = default;
|
2012-09-15 22:51:55 +04:00
|
|
|
};
|
|
|
|
|
2020-02-14 18:05:27 +03:00
|
|
|
bool ContentSecurityPolicyAllows(JSContext* aCx, JS::HandleString aCode) {
|
2012-09-15 22:51:55 +04:00
|
|
|
WorkerPrivate* worker = GetWorkerPrivateFromContext(aCx);
|
|
|
|
worker->AssertIsOnWorkerThread();
|
|
|
|
|
Bug 1583949 - Add a check for IsEvalAllowed to the worker callpath for eval() r=ckerschb,baku
This patch does several things. Because Workers aren't on the main thread,
many of the things done are in the name of off main thread access.
1) Changes a parameter in IsEvalAllowed from a nsIPrincipal to a bool.
We only used the principal to determined if it was the System Principal.
Principals aren't thread safe and can only be accessed on Main Thread, so
if we passed a Principal in, we would be in error. Instead only pass in
the bool which - for workers - comes from a thread-safe location.
2) Separates out the Telemetry Event Recording and sending a message to the
console into a new function nsContentSecurityUtils::NotifyEvalUsage. (And
creates a runnable that calls it.)
We do this because we will need to only call this method on the main thread.
Telemetry Event Recording has only ever been called on the Main Thread.
While I possibly-successfully cut it over to happen Off Main Thread (OMT)
by porting preferences to StaticPrefs, I don't know if there were other
threading assumptions in the Telemetry Code. So it would be much safer to
just continue recording Event Telemetry on the main thread.
Sending a message to the console requires calling GetStringBundleService()
which requires main thread. I didn't investigate if this could be made
thread-safe, I just threw it onto the main thread too.
If, in IsEvalAllowed, we are on the main thread - we call NotifyEvalUsage
directly. If we are not, we create a runnable which will then call
NotifyEvalUsage for us on the main thread.
3) Ports allow_eval_with_system_principal and allow_eval_in_parent_process
from bools to RelaxedAtomicBool - because we now check these prefs OMT.
4) In RuntimeService.cpp, adds the call to IsEvalAllowed.
5) Add resource://gre/modules/workers/require.js to the allowlist of eval
usage. This was the script that identified this gap in the first place.
It uses eval (twice) for structural reasons (scope and line number
massaging.) The contents of the eval are the result of a request to a
uri (which may be internal, like resource://). The whole point of this
is to implement a CommonJS require() api.
This usage of eval is safe because the only way an attacker can inject
into it is by either controlling the response of the uri request or
controlling (or appending to) the argument. If they can do that, they
are able to inject script into Firefox even if we cut this usage of eval
over to some other type of safe(r) script loader.
Bug 1584564 tracks making sure calls to require.js are safe.
6) Adds cld-worker.js to the allowlist. Bug 1584605 is for refactoring that
eval usage, which is decidedly non-trivial.
7) Does _not_ enforce the eval restrictions for workers. While I've gotten
try to be green and not throw up any instances of eval-usage by workers,
it is much safer to deploy this is Telemetry-only mode for Workers for
a little bit to see if anything pops up from the Nightly population.
Bug 1584602 is for enforcing the checks.
Differential Revision: https://phabricator.services.mozilla.com/D47480
--HG--
extra : moz-landing-system : lando
2019-10-08 20:31:35 +03:00
|
|
|
nsAutoJSString scriptSample;
|
2020-02-14 18:05:27 +03:00
|
|
|
if (NS_WARN_IF(!scriptSample.init(aCx, aCode))) {
|
Bug 1583949 - Add a check for IsEvalAllowed to the worker callpath for eval() r=ckerschb,baku
This patch does several things. Because Workers aren't on the main thread,
many of the things done are in the name of off main thread access.
1) Changes a parameter in IsEvalAllowed from a nsIPrincipal to a bool.
We only used the principal to determined if it was the System Principal.
Principals aren't thread safe and can only be accessed on Main Thread, so
if we passed a Principal in, we would be in error. Instead only pass in
the bool which - for workers - comes from a thread-safe location.
2) Separates out the Telemetry Event Recording and sending a message to the
console into a new function nsContentSecurityUtils::NotifyEvalUsage. (And
creates a runnable that calls it.)
We do this because we will need to only call this method on the main thread.
Telemetry Event Recording has only ever been called on the Main Thread.
While I possibly-successfully cut it over to happen Off Main Thread (OMT)
by porting preferences to StaticPrefs, I don't know if there were other
threading assumptions in the Telemetry Code. So it would be much safer to
just continue recording Event Telemetry on the main thread.
Sending a message to the console requires calling GetStringBundleService()
which requires main thread. I didn't investigate if this could be made
thread-safe, I just threw it onto the main thread too.
If, in IsEvalAllowed, we are on the main thread - we call NotifyEvalUsage
directly. If we are not, we create a runnable which will then call
NotifyEvalUsage for us on the main thread.
3) Ports allow_eval_with_system_principal and allow_eval_in_parent_process
from bools to RelaxedAtomicBool - because we now check these prefs OMT.
4) In RuntimeService.cpp, adds the call to IsEvalAllowed.
5) Add resource://gre/modules/workers/require.js to the allowlist of eval
usage. This was the script that identified this gap in the first place.
It uses eval (twice) for structural reasons (scope and line number
massaging.) The contents of the eval are the result of a request to a
uri (which may be internal, like resource://). The whole point of this
is to implement a CommonJS require() api.
This usage of eval is safe because the only way an attacker can inject
into it is by either controlling the response of the uri request or
controlling (or appending to) the argument. If they can do that, they
are able to inject script into Firefox even if we cut this usage of eval
over to some other type of safe(r) script loader.
Bug 1584564 tracks making sure calls to require.js are safe.
6) Adds cld-worker.js to the allowlist. Bug 1584605 is for refactoring that
eval usage, which is decidedly non-trivial.
7) Does _not_ enforce the eval restrictions for workers. While I've gotten
try to be green and not throw up any instances of eval-usage by workers,
it is much safer to deploy this is Telemetry-only mode for Workers for
a little bit to see if anything pops up from the Nightly population.
Bug 1584602 is for enforcing the checks.
Differential Revision: https://phabricator.services.mozilla.com/D47480
--HG--
extra : moz-landing-system : lando
2019-10-08 20:31:35 +03:00
|
|
|
JS_ClearPendingException(aCx);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!nsContentSecurityUtils::IsEvalAllowed(aCx, worker->UsesSystemPrincipal(),
|
|
|
|
scriptSample)) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-07-16 18:58:04 +03:00
|
|
|
|
Bug 1583949 - Add a check for IsEvalAllowed to the worker callpath for eval() r=ckerschb,baku
This patch does several things. Because Workers aren't on the main thread,
many of the things done are in the name of off main thread access.
1) Changes a parameter in IsEvalAllowed from a nsIPrincipal to a bool.
We only used the principal to determined if it was the System Principal.
Principals aren't thread safe and can only be accessed on Main Thread, so
if we passed a Principal in, we would be in error. Instead only pass in
the bool which - for workers - comes from a thread-safe location.
2) Separates out the Telemetry Event Recording and sending a message to the
console into a new function nsContentSecurityUtils::NotifyEvalUsage. (And
creates a runnable that calls it.)
We do this because we will need to only call this method on the main thread.
Telemetry Event Recording has only ever been called on the Main Thread.
While I possibly-successfully cut it over to happen Off Main Thread (OMT)
by porting preferences to StaticPrefs, I don't know if there were other
threading assumptions in the Telemetry Code. So it would be much safer to
just continue recording Event Telemetry on the main thread.
Sending a message to the console requires calling GetStringBundleService()
which requires main thread. I didn't investigate if this could be made
thread-safe, I just threw it onto the main thread too.
If, in IsEvalAllowed, we are on the main thread - we call NotifyEvalUsage
directly. If we are not, we create a runnable which will then call
NotifyEvalUsage for us on the main thread.
3) Ports allow_eval_with_system_principal and allow_eval_in_parent_process
from bools to RelaxedAtomicBool - because we now check these prefs OMT.
4) In RuntimeService.cpp, adds the call to IsEvalAllowed.
5) Add resource://gre/modules/workers/require.js to the allowlist of eval
usage. This was the script that identified this gap in the first place.
It uses eval (twice) for structural reasons (scope and line number
massaging.) The contents of the eval are the result of a request to a
uri (which may be internal, like resource://). The whole point of this
is to implement a CommonJS require() api.
This usage of eval is safe because the only way an attacker can inject
into it is by either controlling the response of the uri request or
controlling (or appending to) the argument. If they can do that, they
are able to inject script into Firefox even if we cut this usage of eval
over to some other type of safe(r) script loader.
Bug 1584564 tracks making sure calls to require.js are safe.
6) Adds cld-worker.js to the allowlist. Bug 1584605 is for refactoring that
eval usage, which is decidedly non-trivial.
7) Does _not_ enforce the eval restrictions for workers. While I've gotten
try to be green and not throw up any instances of eval-usage by workers,
it is much safer to deploy this is Telemetry-only mode for Workers for
a little bit to see if anything pops up from the Nightly population.
Bug 1584602 is for enforcing the checks.
Differential Revision: https://phabricator.services.mozilla.com/D47480
--HG--
extra : moz-landing-system : lando
2019-10-08 20:31:35 +03:00
|
|
|
if (worker->GetReportCSPViolations()) {
|
2012-10-16 00:54:58 +04:00
|
|
|
nsString fileName;
|
|
|
|
uint32_t lineNum = 0;
|
2018-07-05 09:21:04 +03:00
|
|
|
uint32_t columnNum = 0;
|
2012-10-16 00:54:58 +04:00
|
|
|
|
2016-03-09 13:20:11 +03:00
|
|
|
JS::AutoFilename file;
|
2018-07-05 09:21:04 +03:00
|
|
|
if (JS::DescribeScriptedCaller(aCx, &file, &lineNum, &columnNum) &&
|
|
|
|
file.get()) {
|
2014-02-25 19:43:14 +04:00
|
|
|
fileName = NS_ConvertUTF8toUTF16(file.get());
|
2012-10-16 00:54:58 +04:00
|
|
|
} else {
|
2016-02-26 23:23:13 +03:00
|
|
|
MOZ_ASSERT(!JS_IsExceptionPending(aCx));
|
2012-10-16 00:54:58 +04:00
|
|
|
}
|
2012-09-15 22:51:55 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<LogViolationDetailsRunnable> runnable =
|
2018-07-16 18:58:04 +03:00
|
|
|
new LogViolationDetailsRunnable(worker, fileName, lineNum, columnNum,
|
|
|
|
scriptSample);
|
2012-09-22 16:45:00 +04:00
|
|
|
|
2016-05-03 11:21:57 +03:00
|
|
|
ErrorResult rv;
|
2017-01-05 12:05:32 +03:00
|
|
|
runnable->Dispatch(Killing, rv);
|
2016-05-03 11:21:57 +03:00
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
rv.SuppressException();
|
|
|
|
}
|
2012-09-15 22:51:55 +04:00
|
|
|
}
|
|
|
|
|
2012-10-16 00:54:58 +04:00
|
|
|
return worker->IsEvalAllowed();
|
2012-09-15 22:51:55 +04:00
|
|
|
}
|
|
|
|
|
2012-12-30 22:21:52 +04:00
|
|
|
void CTypesActivityCallback(JSContext* aCx, js::CTypesActivityType aType) {
|
|
|
|
WorkerPrivate* worker = GetWorkerPrivateFromContext(aCx);
|
|
|
|
worker->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
switch (aType) {
|
|
|
|
case js::CTYPES_CALL_BEGIN:
|
|
|
|
worker->BeginCTypesCall();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case js::CTYPES_CALL_END:
|
|
|
|
worker->EndCTypesCall();
|
|
|
|
break;
|
|
|
|
|
2013-01-08 16:57:44 +04:00
|
|
|
case js::CTYPES_CALLBACK_BEGIN:
|
|
|
|
worker->BeginCTypesCallback();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case js::CTYPES_CALLBACK_END:
|
|
|
|
worker->EndCTypesCallback();
|
|
|
|
break;
|
|
|
|
|
2012-12-30 22:21:52 +04:00
|
|
|
default:
|
2013-06-29 05:38:30 +04:00
|
|
|
MOZ_CRASH("Unknown type flag!");
|
2012-12-30 22:21:52 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-16 22:38:53 +03:00
|
|
|
// JSDispatchableRunnables are WorkerRunnables used to dispatch JS::Dispatchable
|
|
|
|
// back to their worker thread. A WorkerRunnable is used for two reasons:
|
|
|
|
//
|
|
|
|
// 1. The JS::Dispatchable::run() callback may run JS so we cannot use a control
|
|
|
|
// runnable since they use async interrupts and break JS run-to-completion.
|
|
|
|
//
|
|
|
|
// 2. The DispatchToEventLoopCallback interface is *required* to fail during
|
|
|
|
// shutdown (see jsapi.h) which is exactly what WorkerRunnable::Dispatch() will
|
|
|
|
// do. Moreover, JS_DestroyContext() does *not* block on JS::Dispatchable::run
|
|
|
|
// being called, DispatchToEventLoopCallback failure is expected to happen
|
|
|
|
// during shutdown.
|
|
|
|
class JSDispatchableRunnable final : public WorkerRunnable {
|
|
|
|
JS::Dispatchable* mDispatchable;
|
2016-08-19 22:00:53 +03:00
|
|
|
|
2017-08-16 22:38:53 +03:00
|
|
|
~JSDispatchableRunnable() { MOZ_ASSERT(!mDispatchable); }
|
2016-08-19 22:00:53 +03:00
|
|
|
|
|
|
|
// Disable the usual pre/post-dispatch thread assertions since we are
|
|
|
|
// dispatching from some random JS engine internal thread:
|
|
|
|
|
|
|
|
bool PreDispatch(WorkerPrivate* aWorkerPrivate) override { return true; }
|
|
|
|
|
|
|
|
void PostDispatch(WorkerPrivate* aWorkerPrivate,
|
|
|
|
bool aDispatchResult) override {
|
|
|
|
// For the benefit of the destructor assert.
|
|
|
|
if (!aDispatchResult) {
|
2017-08-16 22:38:53 +03:00
|
|
|
mDispatchable = nullptr;
|
2016-08-19 22:00:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2017-08-16 22:38:53 +03:00
|
|
|
JSDispatchableRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
JS::Dispatchable* aDispatchable)
|
|
|
|
: WorkerRunnable(aWorkerPrivate,
|
|
|
|
WorkerRunnable::WorkerThreadUnchangedBusyCount),
|
|
|
|
mDispatchable(aDispatchable) {
|
|
|
|
MOZ_ASSERT(mDispatchable);
|
2016-08-19 22:00:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override {
|
|
|
|
MOZ_ASSERT(aWorkerPrivate == mWorkerPrivate);
|
|
|
|
MOZ_ASSERT(aCx == mWorkerPrivate->GetJSContext());
|
2017-08-16 22:38:53 +03:00
|
|
|
MOZ_ASSERT(mDispatchable);
|
2016-08-19 22:00:53 +03:00
|
|
|
|
|
|
|
AutoJSAPI jsapi;
|
|
|
|
jsapi.Init();
|
|
|
|
|
2017-08-16 22:38:53 +03:00
|
|
|
mDispatchable->run(mWorkerPrivate->GetJSContext(),
|
|
|
|
JS::Dispatchable::NotShuttingDown);
|
|
|
|
mDispatchable = nullptr; // mDispatchable may delete itself
|
2016-08-19 22:00:53 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult Cancel() override {
|
2017-08-16 22:38:53 +03:00
|
|
|
MOZ_ASSERT(mDispatchable);
|
2016-08-19 22:00:53 +03:00
|
|
|
|
|
|
|
AutoJSAPI jsapi;
|
|
|
|
jsapi.Init();
|
|
|
|
|
2017-08-16 22:38:53 +03:00
|
|
|
mDispatchable->run(mWorkerPrivate->GetJSContext(),
|
|
|
|
JS::Dispatchable::ShuttingDown);
|
|
|
|
mDispatchable = nullptr; // mDispatchable may delete itself
|
2016-08-19 22:00:53 +03:00
|
|
|
|
|
|
|
return WorkerRunnable::Cancel();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-08-16 22:38:53 +03:00
|
|
|
static bool DispatchToEventLoop(void* aClosure,
|
|
|
|
JS::Dispatchable* aDispatchable) {
|
|
|
|
// This callback may execute either on the worker thread or a random
|
|
|
|
// JS-internal helper thread.
|
|
|
|
|
|
|
|
// See comment at JS::InitDispatchToEventLoop() below for how we know the
|
|
|
|
// WorkerPrivate is alive.
|
|
|
|
WorkerPrivate* workerPrivate = reinterpret_cast<WorkerPrivate*>(aClosure);
|
|
|
|
|
|
|
|
// Dispatch is expected to fail during shutdown for the reasons outlined in
|
|
|
|
// the JSDispatchableRunnable comment above.
|
|
|
|
RefPtr<JSDispatchableRunnable> r =
|
|
|
|
new JSDispatchableRunnable(workerPrivate, aDispatchable);
|
|
|
|
return r->Dispatch();
|
2016-08-19 22:00:53 +03:00
|
|
|
}
|
|
|
|
|
2017-10-10 22:41:24 +03:00
|
|
|
static bool ConsumeStream(JSContext* aCx, JS::HandleObject aObj,
|
|
|
|
JS::MimeType aMimeType,
|
|
|
|
JS::StreamConsumer* aConsumer) {
|
|
|
|
WorkerPrivate* worker = GetWorkerPrivateFromContext(aCx);
|
|
|
|
if (!worker) {
|
|
|
|
JS_ReportErrorNumberASCII(aCx, js::GetErrorMessage, nullptr,
|
|
|
|
JSMSG_ERROR_CONSUMING_RESPONSE);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FetchUtil::StreamResponseToJS(aCx, aObj, aMimeType, aConsumer, worker);
|
|
|
|
}
|
|
|
|
|
2016-08-11 15:39:22 +03:00
|
|
|
bool InitJSContextForWorker(WorkerPrivate* aWorkerPrivate,
|
|
|
|
JSContext* aWorkerCx) {
|
2011-07-17 23:09:13 +04:00
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
NS_ASSERTION(!aWorkerPrivate->GetJSContext(), "Already has a context!");
|
|
|
|
|
2013-05-17 02:49:43 +04:00
|
|
|
JSSettings settings;
|
|
|
|
aWorkerPrivate->CopyJSSettings(settings);
|
|
|
|
|
2016-08-11 15:39:22 +03:00
|
|
|
JS::ContextOptionsRef(aWorkerCx) = settings.contextOptions;
|
2014-02-26 13:25:36 +04:00
|
|
|
|
2012-01-04 23:11:32 +04:00
|
|
|
// This is the real place where we set the max memory for the runtime.
|
2020-04-30 12:18:27 +03:00
|
|
|
for (const auto& setting : settings.gcSettings) {
|
|
|
|
if (setting.value) {
|
|
|
|
JS_SetGCParameter(aWorkerCx, setting.key, *setting.value);
|
|
|
|
} else {
|
|
|
|
JS_ResetGCParameter(aWorkerCx, setting.key);
|
2013-05-17 02:49:43 +04:00
|
|
|
}
|
|
|
|
}
|
2012-01-04 23:11:32 +04:00
|
|
|
|
2016-08-11 15:39:22 +03:00
|
|
|
JS_SetNativeStackQuota(aWorkerCx, WORKER_CONTEXT_NATIVE_STACK_LIMIT);
|
2012-02-01 02:28:22 +04:00
|
|
|
|
2012-09-15 22:51:55 +04:00
|
|
|
// Security policy:
|
2014-12-17 03:30:39 +03:00
|
|
|
static const JSSecurityCallbacks securityCallbacks = {
|
2012-09-15 22:51:55 +04:00
|
|
|
ContentSecurityPolicyAllows};
|
2016-08-11 15:39:22 +03:00
|
|
|
JS_SetSecurityCallbacks(aWorkerCx, &securityCallbacks);
|
2012-09-15 22:51:55 +04:00
|
|
|
|
2017-08-16 22:38:53 +03:00
|
|
|
// A WorkerPrivate lives strictly longer than its JSRuntime so we can safely
|
|
|
|
// store a raw pointer as the callback's closure argument on the JSRuntime.
|
|
|
|
JS::InitDispatchToEventLoop(aWorkerCx, DispatchToEventLoop,
|
|
|
|
(void*)aWorkerPrivate);
|
2016-08-19 22:00:53 +03:00
|
|
|
|
2018-11-16 19:32:20 +03:00
|
|
|
JS::InitConsumeStreamCallback(aWorkerCx, ConsumeStream,
|
|
|
|
FetchUtil::ReportJSStreamError);
|
2017-10-10 22:41:24 +03:00
|
|
|
|
2016-08-11 15:39:22 +03:00
|
|
|
if (!JS::InitSelfHostedCode(aWorkerCx)) {
|
2016-06-22 10:47:52 +03:00
|
|
|
NS_WARNING("Could not init self-hosted code!");
|
2016-08-11 15:39:22 +03:00
|
|
|
return false;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2016-06-24 03:03:03 +03:00
|
|
|
JS_AddInterruptCallback(aWorkerCx, InterruptCallback);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2016-08-11 15:39:22 +03:00
|
|
|
js::SetCTypesActivityCallback(aWorkerCx, CTypesActivityCallback);
|
2012-12-30 22:21:52 +04:00
|
|
|
|
2013-05-17 02:49:43 +04:00
|
|
|
#ifdef JS_GC_ZEAL
|
2016-08-11 15:39:22 +03:00
|
|
|
JS_SetGCZeal(aWorkerCx, settings.gcZeal, settings.gcZealFrequency);
|
2011-07-17 23:09:13 +04:00
|
|
|
#endif
|
|
|
|
|
2016-08-11 15:39:22 +03:00
|
|
|
return true;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2018-09-21 21:20:33 +03:00
|
|
|
static bool PreserveWrapper(JSContext* cx, JS::HandleObject obj) {
|
2014-10-09 03:27:55 +04:00
|
|
|
MOZ_ASSERT(cx);
|
|
|
|
MOZ_ASSERT(obj);
|
|
|
|
MOZ_ASSERT(mozilla::dom::IsDOMObject(obj));
|
|
|
|
|
|
|
|
return mozilla::dom::TryPreserveWrapper(obj);
|
|
|
|
}
|
|
|
|
|
2019-10-02 06:20:33 +03:00
|
|
|
static bool IsWorkerDebuggerGlobalOrSandbox(JS::HandleObject aGlobal) {
|
2019-05-28 21:32:06 +03:00
|
|
|
return IsWorkerDebuggerGlobal(aGlobal) || IsWorkerDebuggerSandbox(aGlobal);
|
|
|
|
}
|
|
|
|
|
2015-03-04 02:51:53 +03:00
|
|
|
JSObject* Wrap(JSContext* cx, JS::HandleObject existing, JS::HandleObject obj) {
|
2019-10-02 06:20:33 +03:00
|
|
|
JS::RootedObject targetGlobal(cx, JS::CurrentGlobalOrNull(cx));
|
2015-03-04 02:51:53 +03:00
|
|
|
|
2018-07-06 19:16:24 +03:00
|
|
|
// Note: the JS engine unwraps CCWs before calling this callback.
|
2019-10-02 06:20:33 +03:00
|
|
|
JS::RootedObject originGlobal(cx, JS::GetNonCCWObjectGlobal(obj));
|
2015-03-04 02:51:53 +03:00
|
|
|
|
|
|
|
const js::Wrapper* wrapper = nullptr;
|
2019-05-28 21:32:06 +03:00
|
|
|
if (IsWorkerDebuggerGlobalOrSandbox(targetGlobal) &&
|
|
|
|
IsWorkerDebuggerGlobalOrSandbox(originGlobal)) {
|
2015-03-04 02:51:53 +03:00
|
|
|
wrapper = &js::CrossCompartmentWrapper::singleton;
|
|
|
|
} else {
|
2015-06-02 17:54:54 +03:00
|
|
|
wrapper = &js::OpaqueCrossCompartmentWrapper::singleton;
|
2015-03-04 02:51:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (existing) {
|
2017-02-08 17:04:57 +03:00
|
|
|
js::Wrapper::Renew(existing, obj, wrapper);
|
2015-03-04 02:51:53 +03:00
|
|
|
}
|
|
|
|
return js::Wrapper::New(cx, obj, wrapper);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const JSWrapObjectCallbacks WrapObjectCallbacks = {
|
|
|
|
Wrap,
|
|
|
|
nullptr,
|
|
|
|
};
|
|
|
|
|
2017-02-24 00:23:45 +03:00
|
|
|
class WorkerJSRuntime final : public mozilla::CycleCollectedJSRuntime {
|
|
|
|
public:
|
|
|
|
// The heap size passed here doesn't matter, we will change it later in the
|
|
|
|
// call to JS_SetGCParameter inside InitJSContextForWorker.
|
|
|
|
explicit WorkerJSRuntime(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
: CycleCollectedJSRuntime(aCx), mWorkerPrivate(aWorkerPrivate) {
|
2017-05-09 14:59:00 +03:00
|
|
|
MOZ_COUNT_CTOR_INHERITED(WorkerJSRuntime, CycleCollectedJSRuntime);
|
2017-02-24 00:23:45 +03:00
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
2017-09-12 01:46:14 +03:00
|
|
|
|
|
|
|
{
|
|
|
|
JS::UniqueChars defaultLocale = aWorkerPrivate->AdoptDefaultLocale();
|
|
|
|
MOZ_ASSERT(defaultLocale,
|
|
|
|
"failure of a WorkerPrivate to have a default locale should "
|
|
|
|
"have made the worker fail to spawn");
|
|
|
|
|
|
|
|
if (!JS_SetDefaultLocale(Runtime(), defaultLocale.get())) {
|
|
|
|
NS_WARNING("failed to set workerCx's default locale");
|
|
|
|
}
|
|
|
|
}
|
2017-02-24 00:23:45 +03:00
|
|
|
}
|
|
|
|
|
2017-04-23 21:23:33 +03:00
|
|
|
void Shutdown(JSContext* cx) override {
|
2017-02-24 00:23:45 +03:00
|
|
|
// The CC is shut down, and the superclass destructor will GC, so make sure
|
|
|
|
// we don't try to CC again.
|
|
|
|
mWorkerPrivate = nullptr;
|
2017-12-18 19:59:30 +03:00
|
|
|
|
|
|
|
CycleCollectedJSRuntime::Shutdown(cx);
|
2017-02-24 00:23:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
~WorkerJSRuntime() {
|
2017-05-09 14:59:00 +03:00
|
|
|
MOZ_COUNT_DTOR_INHERITED(WorkerJSRuntime, CycleCollectedJSRuntime);
|
2017-02-24 00:23:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void PrepareForForgetSkippable() override {}
|
|
|
|
|
|
|
|
virtual void BeginCycleCollectionCallback() override {}
|
|
|
|
|
|
|
|
virtual void EndCycleCollectionCallback(
|
|
|
|
CycleCollectorResults& aResults) override {}
|
|
|
|
|
|
|
|
void DispatchDeferredDeletion(bool aContinuation, bool aPurge) override {
|
|
|
|
MOZ_ASSERT(!aContinuation);
|
|
|
|
|
|
|
|
// Do it immediately, no need for asynchronous behavior here.
|
|
|
|
nsCycleCollector_doDeferredDeletion();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void CustomGCCallback(JSGCStatus aStatus) override {
|
|
|
|
if (!mWorkerPrivate) {
|
|
|
|
// We're shutting down, no need to do anything.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
if (aStatus == JSGC_END) {
|
2018-07-31 22:34:50 +03:00
|
|
|
nsCycleCollector_collect(nullptr);
|
2017-02-24 00:23:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
WorkerPrivate* mWorkerPrivate;
|
|
|
|
};
|
|
|
|
|
2018-05-10 08:04:12 +03:00
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
} // namespace workerinternals
|
|
|
|
|
|
|
|
class WorkerJSContext final : public mozilla::CycleCollectedJSContext {
|
2013-08-04 03:55:40 +04:00
|
|
|
public:
|
|
|
|
// The heap size passed here doesn't matter, we will change it later in the
|
2016-06-22 10:47:52 +03:00
|
|
|
// call to JS_SetGCParameter inside InitJSContextForWorker.
|
2016-09-14 16:48:42 +03:00
|
|
|
explicit WorkerJSContext(WorkerPrivate* aWorkerPrivate)
|
2016-02-14 16:30:25 +03:00
|
|
|
: mWorkerPrivate(aWorkerPrivate) {
|
2017-05-09 14:59:00 +03:00
|
|
|
MOZ_COUNT_CTOR_INHERITED(WorkerJSContext, CycleCollectedJSContext);
|
2016-02-14 16:30:25 +03:00
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
2017-11-17 06:01:27 +03:00
|
|
|
// Magical number 2. Workers have the base recursion depth 1, and normal
|
|
|
|
// runnables run at level 2, and we don't want to process microtasks
|
|
|
|
// at any other level.
|
|
|
|
SetTargetedMicroTaskRecursionDepth(2);
|
2014-02-19 20:02:13 +04:00
|
|
|
}
|
2013-08-04 03:55:40 +04:00
|
|
|
|
2019-03-19 18:14:11 +03:00
|
|
|
// MOZ_CAN_RUN_SCRIPT_BOUNDARY because otherwise we have to annotate the
|
|
|
|
// SpiderMonkey JS::JobQueue's destructor as MOZ_CAN_RUN_SCRIPT, which is a
|
|
|
|
// bit of a pain.
|
|
|
|
MOZ_CAN_RUN_SCRIPT_BOUNDARY ~WorkerJSContext() {
|
2017-05-09 14:59:00 +03:00
|
|
|
MOZ_COUNT_DTOR_INHERITED(WorkerJSContext, CycleCollectedJSContext);
|
2016-08-11 15:39:22 +03:00
|
|
|
JSContext* cx = MaybeContext();
|
|
|
|
if (!cx) {
|
2016-04-19 07:04:32 +03:00
|
|
|
return; // Initialize() must have failed
|
|
|
|
}
|
2015-04-09 01:21:26 +03:00
|
|
|
|
2013-09-24 19:03:23 +04:00
|
|
|
// The worker global should be unrooted and the shutdown cycle collection
|
|
|
|
// should break all remaining cycles. The superclass destructor will run
|
|
|
|
// the GC one final time and finalize any JSObjects that were participating
|
2013-08-04 03:55:40 +04:00
|
|
|
// in cycles that were broken during CC shutdown.
|
|
|
|
nsCycleCollector_shutdown();
|
2013-08-14 04:07:40 +04:00
|
|
|
|
2013-09-24 19:03:23 +04:00
|
|
|
// The CC is shut down, and the superclass destructor will GC, so make sure
|
|
|
|
// we don't try to CC again.
|
2013-08-14 04:07:40 +04:00
|
|
|
mWorkerPrivate = nullptr;
|
2013-08-04 03:55:40 +04:00
|
|
|
}
|
|
|
|
|
2018-05-10 08:04:12 +03:00
|
|
|
WorkerJSContext* GetAsWorkerJSContext() override { return this; }
|
|
|
|
|
2017-02-24 00:23:45 +03:00
|
|
|
CycleCollectedJSRuntime* CreateRuntime(JSContext* aCx) override {
|
|
|
|
return new WorkerJSRuntime(aCx, mWorkerPrivate);
|
|
|
|
}
|
|
|
|
|
2017-02-11 02:47:50 +03:00
|
|
|
nsresult Initialize(JSRuntime* aParentRuntime) {
|
|
|
|
nsresult rv = CycleCollectedJSContext::Initialize(
|
2019-12-16 07:14:55 +03:00
|
|
|
aParentRuntime, WORKER_DEFAULT_RUNTIME_HEAPSIZE);
|
2016-02-14 16:30:25 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2016-07-23 20:54:19 +03:00
|
|
|
JSContext* cx = Context();
|
2016-07-05 12:06:05 +03:00
|
|
|
|
2016-07-05 15:35:26 +03:00
|
|
|
js::SetPreserveWrapperCallback(cx, PreserveWrapper);
|
2019-10-04 20:37:09 +03:00
|
|
|
JS_InitDestroyPrincipalsCallback(cx, WorkerPrincipal::Destroy);
|
2016-07-05 12:06:05 +03:00
|
|
|
JS_SetWrapObjectCallbacks(cx, &WrapObjectCallbacks);
|
2016-02-14 16:30:25 +03:00
|
|
|
if (mWorkerPrivate->IsDedicatedWorker()) {
|
2016-07-05 12:06:05 +03:00
|
|
|
JS_SetFutexCanWait(cx);
|
2016-02-14 16:30:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-11-17 06:01:27 +03:00
|
|
|
virtual void DispatchToMicroTask(
|
|
|
|
already_AddRefed<MicroTaskRunnable> aRunnable) override {
|
|
|
|
RefPtr<MicroTaskRunnable> runnable(aRunnable);
|
2016-07-06 01:49:06 +03:00
|
|
|
|
2016-03-24 18:12:00 +03:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2016-07-06 01:49:06 +03:00
|
|
|
MOZ_ASSERT(runnable);
|
2016-03-24 18:12:00 +03:00
|
|
|
|
2017-11-17 06:01:27 +03:00
|
|
|
std::queue<RefPtr<MicroTaskRunnable>>* microTaskQueue = nullptr;
|
2016-03-24 18:12:00 +03:00
|
|
|
|
2018-02-05 21:55:07 +03:00
|
|
|
JSContext* cx = GetCurrentWorkerThreadJSContext();
|
2016-03-24 18:12:00 +03:00
|
|
|
NS_ASSERTION(cx, "This should never be null!");
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> global(cx, JS::CurrentGlobalOrNull(cx));
|
|
|
|
NS_ASSERTION(global, "This should never be null!");
|
|
|
|
|
|
|
|
// On worker threads, if the current global is the worker global, we use the
|
2017-11-17 06:01:27 +03:00
|
|
|
// main micro task queue. Otherwise, the current global must be
|
2016-03-24 18:12:00 +03:00
|
|
|
// either the debugger global or a debugger sandbox, and we use the debugger
|
2017-11-17 06:01:27 +03:00
|
|
|
// micro task queue instead.
|
2016-03-24 18:12:00 +03:00
|
|
|
if (IsWorkerGlobal(global)) {
|
2017-11-17 06:01:27 +03:00
|
|
|
microTaskQueue = &GetMicroTaskQueue();
|
2016-03-24 18:12:00 +03:00
|
|
|
} else {
|
2018-02-05 21:55:07 +03:00
|
|
|
MOZ_ASSERT(IsWorkerDebuggerGlobal(global) ||
|
|
|
|
IsWorkerDebuggerSandbox(global));
|
2016-03-24 18:12:00 +03:00
|
|
|
|
2017-11-17 06:01:27 +03:00
|
|
|
microTaskQueue = &GetDebuggerMicroTaskQueue();
|
2016-03-24 18:12:00 +03:00
|
|
|
}
|
|
|
|
|
2018-08-02 10:11:57 +03:00
|
|
|
JS::JobQueueMayNotBeEmpty(cx);
|
2020-02-13 17:38:48 +03:00
|
|
|
microTaskQueue->push(std::move(runnable));
|
2016-03-24 18:12:00 +03:00
|
|
|
}
|
|
|
|
|
2018-04-06 09:53:25 +03:00
|
|
|
bool IsSystemCaller() const override {
|
|
|
|
return mWorkerPrivate->UsesSystemPrincipal();
|
|
|
|
}
|
|
|
|
|
2019-06-12 22:11:04 +03:00
|
|
|
void ReportError(JSErrorReport* aReport,
|
|
|
|
JS::ConstUTF8CharsZ aToStringResult) override {
|
|
|
|
mWorkerPrivate->ReportError(Context(), aToStringResult, aReport);
|
|
|
|
}
|
|
|
|
|
2018-05-10 08:04:12 +03:00
|
|
|
WorkerPrivate* GetWorkerPrivate() const { return mWorkerPrivate; }
|
|
|
|
|
2013-08-04 03:55:40 +04:00
|
|
|
private:
|
2013-08-14 04:07:40 +04:00
|
|
|
WorkerPrivate* mWorkerPrivate;
|
2013-08-04 03:55:40 +04:00
|
|
|
};
|
|
|
|
|
2018-05-10 08:04:12 +03:00
|
|
|
namespace workerinternals {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class WorkerThreadPrimaryRunnable final : public Runnable {
|
2011-07-17 23:09:13 +04:00
|
|
|
WorkerPrivate* mWorkerPrivate;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WorkerThread> mThread;
|
2017-02-11 02:47:50 +03:00
|
|
|
JSRuntime* mParentRuntime;
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class FinishedRunnable final : public Runnable {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WorkerThread> mThread;
|
2013-10-23 17:16:49 +04:00
|
|
|
|
|
|
|
public:
|
2014-11-15 05:47:30 +03:00
|
|
|
explicit FinishedRunnable(already_AddRefed<WorkerThread> aThread)
|
2017-01-18 03:50:34 +03:00
|
|
|
: Runnable("WorkerThreadPrimaryRunnable::FinishedRunnable"),
|
|
|
|
mThread(aThread) {
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(mThread);
|
|
|
|
}
|
|
|
|
|
2018-02-12 23:44:40 +03:00
|
|
|
NS_INLINE_DECL_REFCOUNTING_INHERITED(FinishedRunnable, Runnable)
|
2013-10-23 17:16:49 +04:00
|
|
|
|
|
|
|
private:
|
2020-02-12 13:38:22 +03:00
|
|
|
~FinishedRunnable() = default;
|
2013-10-23 17:16:49 +04:00
|
|
|
|
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
};
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
public:
|
2013-10-23 17:16:49 +04:00
|
|
|
WorkerThreadPrimaryRunnable(WorkerPrivate* aWorkerPrivate,
|
2017-02-11 02:47:50 +03:00
|
|
|
WorkerThread* aThread, JSRuntime* aParentRuntime)
|
2017-06-12 22:34:10 +03:00
|
|
|
: mozilla::Runnable("WorkerThreadPrimaryRunnable"),
|
|
|
|
mWorkerPrivate(aWorkerPrivate),
|
|
|
|
mThread(aThread),
|
|
|
|
mParentRuntime(aParentRuntime) {
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
MOZ_ASSERT(aThread);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2018-02-12 23:44:40 +03:00
|
|
|
NS_INLINE_DECL_REFCOUNTING_INHERITED(WorkerThreadPrimaryRunnable, Runnable)
|
2013-10-23 17:16:49 +04:00
|
|
|
|
|
|
|
private:
|
2020-02-12 13:38:22 +03:00
|
|
|
~WorkerThreadPrimaryRunnable() = default;
|
2013-10-23 17:16:49 +04:00
|
|
|
|
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
};
|
|
|
|
|
2014-09-05 18:26:34 +04:00
|
|
|
void PrefLanguagesChanged(const char* /* aPrefName */, void* /* aClosure */) {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
nsTArray<nsString> languages;
|
|
|
|
Navigator::GetAcceptLanguages(languages);
|
|
|
|
|
|
|
|
RuntimeService* runtime = RuntimeService::GetService();
|
|
|
|
if (runtime) {
|
|
|
|
runtime->UpdateAllWorkerLanguages(languages);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-23 23:11:18 +04:00
|
|
|
void AppNameOverrideChanged(const char* /* aPrefName */, void* /* aClosure */) {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2017-07-31 07:23:50 +03:00
|
|
|
nsAutoString override;
|
|
|
|
Preferences::GetString("general.appname.override", override);
|
2014-09-23 23:11:18 +04:00
|
|
|
|
|
|
|
RuntimeService* runtime = RuntimeService::GetService();
|
|
|
|
if (runtime) {
|
|
|
|
runtime->UpdateAppNameOverridePreference(override);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppVersionOverrideChanged(const char* /* aPrefName */,
|
|
|
|
void* /* aClosure */) {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2017-07-31 07:23:50 +03:00
|
|
|
nsAutoString override;
|
|
|
|
Preferences::GetString("general.appversion.override", override);
|
2014-09-23 23:11:18 +04:00
|
|
|
|
|
|
|
RuntimeService* runtime = RuntimeService::GetService();
|
|
|
|
if (runtime) {
|
|
|
|
runtime->UpdateAppVersionOverridePreference(override);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PlatformOverrideChanged(const char* /* aPrefName */,
|
|
|
|
void* /* aClosure */) {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2017-07-31 07:23:50 +03:00
|
|
|
nsAutoString override;
|
|
|
|
Preferences::GetString("general.platform.override", override);
|
2014-09-23 23:11:18 +04:00
|
|
|
|
|
|
|
RuntimeService* runtime = RuntimeService::GetService();
|
|
|
|
if (runtime) {
|
|
|
|
runtime->UpdatePlatformOverridePreference(override);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
} /* anonymous namespace */
|
2013-08-04 03:55:40 +04:00
|
|
|
|
2013-05-17 02:49:43 +04:00
|
|
|
// This is only touched on the main thread. Initialized in Init() below.
|
2020-04-30 12:18:27 +03:00
|
|
|
UniquePtr<JSSettings> RuntimeService::sDefaultJSSettings;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
RuntimeService::RuntimeService()
|
2011-07-26 05:49:16 +04:00
|
|
|
: mMutex("RuntimeService::mMutex"),
|
|
|
|
mObserved(false),
|
2013-11-20 03:08:50 +04:00
|
|
|
mShuttingDown(false),
|
|
|
|
mNavigatorPropertiesLoaded(false) {
|
2011-07-17 23:09:13 +04:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
NS_ASSERTION(!gRuntimeService, "More than one service!");
|
|
|
|
}
|
|
|
|
|
|
|
|
RuntimeService::~RuntimeService() {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
// gRuntimeService can be null if Init() fails.
|
|
|
|
NS_ASSERTION(!gRuntimeService || gRuntimeService == this,
|
|
|
|
"More than one service!");
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
gRuntimeService = nullptr;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
RuntimeService* RuntimeService::GetOrCreateService() {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
if (!gRuntimeService) {
|
2014-10-08 16:56:50 +04:00
|
|
|
// The observer service now owns us until shutdown.
|
|
|
|
gRuntimeService = new RuntimeService();
|
|
|
|
if (NS_FAILED(gRuntimeService->Init())) {
|
2011-07-17 23:09:13 +04:00
|
|
|
NS_WARNING("Failed to initialize!");
|
2014-10-08 16:56:50 +04:00
|
|
|
gRuntimeService->Cleanup();
|
|
|
|
gRuntimeService = nullptr;
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return gRuntimeService;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
RuntimeService* RuntimeService::GetService() { return gRuntimeService; }
|
|
|
|
|
2016-03-28 20:28:14 +03:00
|
|
|
bool RuntimeService::RegisterWorker(WorkerPrivate* aWorkerPrivate) {
|
2011-07-17 23:09:13 +04:00
|
|
|
aWorkerPrivate->AssertIsOnParentThread();
|
|
|
|
|
|
|
|
WorkerPrivate* parent = aWorkerPrivate->GetParent();
|
|
|
|
if (!parent) {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
if (mShuttingDown) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 20:17:00 +03:00
|
|
|
const bool isServiceWorker = aWorkerPrivate->IsServiceWorker();
|
2015-10-01 02:11:03 +03:00
|
|
|
const bool isSharedWorker = aWorkerPrivate->IsSharedWorker();
|
2016-08-19 09:38:58 +03:00
|
|
|
const bool isDedicatedWorker = aWorkerPrivate->IsDedicatedWorker();
|
2015-03-25 20:17:00 +03:00
|
|
|
if (isServiceWorker) {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
Telemetry::Accumulate(Telemetry::SERVICE_WORKER_SPAWN_ATTEMPTS, 1);
|
|
|
|
}
|
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
nsCString sharedWorkerScriptSpec;
|
|
|
|
if (isSharedWorker) {
|
2013-06-05 18:04:23 +04:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> scriptURI = aWorkerPrivate->GetResolvedScriptURI();
|
|
|
|
NS_ASSERTION(scriptURI, "Null script URI!");
|
|
|
|
|
|
|
|
nsresult rv = scriptURI->GetSpec(sharedWorkerScriptSpec);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("GetSpec failed?!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(!sharedWorkerScriptSpec.IsEmpty(), "Empty spec!");
|
|
|
|
}
|
|
|
|
|
2016-08-19 09:38:58 +03:00
|
|
|
bool exemptFromPerDomainMax = false;
|
|
|
|
if (isServiceWorker) {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
exemptFromPerDomainMax = Preferences::GetBool(
|
|
|
|
"dom.serviceWorkers.exemptFromPerDomainMax", false);
|
|
|
|
}
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
const nsCString& domain = aWorkerPrivate->Domain();
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
bool queued = false;
|
|
|
|
{
|
2011-07-26 05:49:16 +04:00
|
|
|
MutexAutoLock lock(mMutex);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2020-01-13 22:18:56 +03:00
|
|
|
const auto& domainInfo =
|
|
|
|
mDomainMap.LookupForAdd(domain).OrInsert([&domain, parent]() {
|
2017-06-14 18:27:25 +03:00
|
|
|
NS_ASSERTION(!parent, "Shouldn't have a parent here!");
|
2017-06-22 09:47:02 +03:00
|
|
|
Unused
|
|
|
|
<< parent; // silence clang -Wunused-lambda-capture in opt builds
|
2017-06-14 18:27:25 +03:00
|
|
|
WorkerDomainInfo* wdi = new WorkerDomainInfo();
|
|
|
|
wdi->mDomain = domain;
|
|
|
|
return wdi;
|
|
|
|
});
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2016-08-19 09:38:58 +03:00
|
|
|
queued = gMaxWorkersPerDomain &&
|
|
|
|
domainInfo->ActiveWorkerCount() >= gMaxWorkersPerDomain &&
|
|
|
|
!domain.IsEmpty() && !exemptFromPerDomainMax;
|
|
|
|
|
|
|
|
if (queued) {
|
|
|
|
domainInfo->mQueuedWorkers.AppendElement(aWorkerPrivate);
|
|
|
|
|
|
|
|
// Worker spawn gets queued due to hitting max workers per domain
|
|
|
|
// limit so let's log a warning.
|
|
|
|
WorkerPrivate::ReportErrorToConsole("HittingMaxWorkersPerDomain2");
|
|
|
|
|
|
|
|
if (isServiceWorker) {
|
|
|
|
Telemetry::Accumulate(Telemetry::SERVICE_WORKER_SPAWN_GETS_QUEUED, 1);
|
|
|
|
} else if (isSharedWorker) {
|
|
|
|
Telemetry::Accumulate(Telemetry::SHARED_WORKER_SPAWN_GETS_QUEUED, 1);
|
|
|
|
} else if (isDedicatedWorker) {
|
|
|
|
Telemetry::Accumulate(Telemetry::DEDICATED_WORKER_SPAWN_GETS_QUEUED, 1);
|
|
|
|
}
|
|
|
|
} else if (parent) {
|
2013-06-05 18:04:23 +04:00
|
|
|
domainInfo->mChildWorkerCount++;
|
2015-06-27 01:51:56 +03:00
|
|
|
} else if (isServiceWorker) {
|
2016-08-19 09:38:58 +03:00
|
|
|
domainInfo->mActiveServiceWorkers.AppendElement(aWorkerPrivate);
|
2013-06-05 18:04:23 +04:00
|
|
|
} else {
|
2016-08-19 09:38:58 +03:00
|
|
|
domainInfo->mActiveWorkers.AppendElement(aWorkerPrivate);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// From here on out we must call UnregisterWorker if something fails!
|
|
|
|
if (parent) {
|
2016-03-28 20:28:14 +03:00
|
|
|
if (!parent->AddChildWorker(aWorkerPrivate)) {
|
2016-03-28 20:28:14 +03:00
|
|
|
UnregisterWorker(aWorkerPrivate);
|
2011-07-17 23:09:13 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
2013-11-20 03:08:50 +04:00
|
|
|
if (!mNavigatorPropertiesLoaded) {
|
2014-09-23 23:11:18 +04:00
|
|
|
Navigator::AppName(mNavigatorProperties.mAppName,
|
2019-03-06 02:13:22 +03:00
|
|
|
aWorkerPrivate->GetPrincipal(),
|
2014-09-23 23:11:18 +04:00
|
|
|
false /* aUsePrefOverriddenValue */);
|
2019-03-06 02:13:22 +03:00
|
|
|
if (NS_FAILED(Navigator::GetAppVersion(
|
|
|
|
mNavigatorProperties.mAppVersion, aWorkerPrivate->GetPrincipal(),
|
|
|
|
false /* aUsePrefOverriddenValue */)) ||
|
|
|
|
NS_FAILED(Navigator::GetPlatform(
|
|
|
|
mNavigatorProperties.mPlatform, aWorkerPrivate->GetPrincipal(),
|
|
|
|
false /* aUsePrefOverriddenValue */))) {
|
2016-03-28 20:28:14 +03:00
|
|
|
UnregisterWorker(aWorkerPrivate);
|
2011-07-17 23:09:13 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-09-23 23:11:18 +04:00
|
|
|
// The navigator overridden properties should have already been read.
|
|
|
|
|
2014-09-05 18:26:34 +04:00
|
|
|
Navigator::GetAcceptLanguages(mNavigatorProperties.mLanguages);
|
2013-11-20 03:08:50 +04:00
|
|
|
mNavigatorPropertiesLoaded = true;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
nsPIDOMWindowInner* window = aWorkerPrivate->GetWindow();
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2015-10-01 02:11:03 +03:00
|
|
|
if (!isServiceWorker) {
|
|
|
|
// Service workers are excluded since their lifetime is separate from
|
|
|
|
// that of dom windows.
|
2017-06-14 18:27:25 +03:00
|
|
|
const auto& windowArray = mWindowMap.LookupForAdd(window).OrInsert(
|
|
|
|
[]() { return new nsTArray<WorkerPrivate*>(1); });
|
2015-10-01 02:11:03 +03:00
|
|
|
if (!windowArray->Contains(aWorkerPrivate)) {
|
|
|
|
windowArray->AppendElement(aWorkerPrivate);
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(aWorkerPrivate->IsSharedWorker());
|
|
|
|
}
|
2013-06-05 18:04:23 +04:00
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2016-03-28 20:28:14 +03:00
|
|
|
if (!queued && !ScheduleWorker(aWorkerPrivate)) {
|
2011-07-17 23:09:13 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-03-25 20:17:00 +03:00
|
|
|
if (isServiceWorker) {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
Telemetry::Accumulate(Telemetry::SERVICE_WORKER_WAS_SPAWNED, 1);
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-03-28 20:28:14 +03:00
|
|
|
void RuntimeService::UnregisterWorker(WorkerPrivate* aWorkerPrivate) {
|
2011-07-17 23:09:13 +04:00
|
|
|
aWorkerPrivate->AssertIsOnParentThread();
|
|
|
|
|
|
|
|
WorkerPrivate* parent = aWorkerPrivate->GetParent();
|
|
|
|
if (!parent) {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
}
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
const nsCString& domain = aWorkerPrivate->Domain();
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
WorkerPrivate* queuedWorker = nullptr;
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2011-07-26 05:49:16 +04:00
|
|
|
MutexAutoLock lock(mMutex);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
WorkerDomainInfo* domainInfo;
|
|
|
|
if (!mDomainMap.Get(domain, &domainInfo)) {
|
|
|
|
NS_ERROR("Don't have an entry for this domain!");
|
|
|
|
}
|
|
|
|
|
2016-08-19 09:38:58 +03:00
|
|
|
// Remove old worker from everywhere.
|
|
|
|
uint32_t index = domainInfo->mQueuedWorkers.IndexOf(aWorkerPrivate);
|
|
|
|
if (index != kNoIndex) {
|
|
|
|
// Was queued, remove from the list.
|
|
|
|
domainInfo->mQueuedWorkers.RemoveElementAt(index);
|
|
|
|
} else if (parent) {
|
2015-06-27 01:51:56 +03:00
|
|
|
MOZ_ASSERT(domainInfo->mChildWorkerCount, "Must be non-zero!");
|
2011-07-17 23:09:13 +04:00
|
|
|
domainInfo->mChildWorkerCount--;
|
2015-06-27 01:51:56 +03:00
|
|
|
} else if (aWorkerPrivate->IsServiceWorker()) {
|
2016-08-19 09:38:58 +03:00
|
|
|
MOZ_ASSERT(domainInfo->mActiveServiceWorkers.Contains(aWorkerPrivate),
|
2015-06-27 01:51:56 +03:00
|
|
|
"Don't know about this worker!");
|
2016-08-19 09:38:58 +03:00
|
|
|
domainInfo->mActiveServiceWorkers.RemoveElement(aWorkerPrivate);
|
2011-07-17 23:09:13 +04:00
|
|
|
} else {
|
2016-08-19 09:38:58 +03:00
|
|
|
MOZ_ASSERT(domainInfo->mActiveWorkers.Contains(aWorkerPrivate),
|
2015-06-27 01:51:56 +03:00
|
|
|
"Don't know about this worker!");
|
2016-08-19 09:38:58 +03:00
|
|
|
domainInfo->mActiveWorkers.RemoveElement(aWorkerPrivate);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2016-08-19 09:38:58 +03:00
|
|
|
// See if there's a queued worker we can schedule.
|
|
|
|
if (domainInfo->ActiveWorkerCount() < gMaxWorkersPerDomain &&
|
|
|
|
!domainInfo->mQueuedWorkers.IsEmpty()) {
|
|
|
|
queuedWorker = domainInfo->mQueuedWorkers[0];
|
|
|
|
domainInfo->mQueuedWorkers.RemoveElementAt(0);
|
|
|
|
|
|
|
|
if (queuedWorker->GetParent()) {
|
|
|
|
domainInfo->mChildWorkerCount++;
|
|
|
|
} else if (queuedWorker->IsServiceWorker()) {
|
|
|
|
domainInfo->mActiveServiceWorkers.AppendElement(queuedWorker);
|
|
|
|
} else {
|
|
|
|
domainInfo->mActiveWorkers.AppendElement(queuedWorker);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-02 00:32:52 +03:00
|
|
|
if (domainInfo->HasNoWorkers()) {
|
2016-08-19 09:38:58 +03:00
|
|
|
MOZ_ASSERT(domainInfo->mQueuedWorkers.IsEmpty());
|
2011-07-17 23:09:13 +04:00
|
|
|
mDomainMap.Remove(domain);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-05 09:11:18 +03:00
|
|
|
if (aWorkerPrivate->IsServiceWorker()) {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
Telemetry::AccumulateTimeDelta(Telemetry::SERVICE_WORKER_LIFE_TIME,
|
|
|
|
aWorkerPrivate->CreationTimeStamp());
|
|
|
|
}
|
|
|
|
|
2018-12-20 21:28:50 +03:00
|
|
|
// NB: For Shared Workers we used to call ShutdownOnMainThread on the
|
|
|
|
// RemoteWorkerController; however, that was redundant because
|
|
|
|
// RemoteWorkerChild uses a WeakWorkerRef which notifies at about the
|
|
|
|
// same time as us calling into the code here and would race with us.
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
if (parent) {
|
2016-03-28 20:28:14 +03:00
|
|
|
parent->RemoveChildWorker(aWorkerPrivate);
|
2015-10-01 02:11:03 +03:00
|
|
|
} else if (aWorkerPrivate->IsSharedWorker()) {
|
2015-10-27 03:09:44 +03:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
for (auto iter = mWindowMap.Iter(); !iter.Done(); iter.Next()) {
|
2020-01-13 22:18:56 +03:00
|
|
|
const auto& workers = iter.Data();
|
|
|
|
MOZ_ASSERT(workers);
|
2015-10-27 03:09:44 +03:00
|
|
|
|
|
|
|
if (workers->RemoveElement(aWorkerPrivate)) {
|
|
|
|
MOZ_ASSERT(!workers->Contains(aWorkerPrivate),
|
|
|
|
"Added worker more than once!");
|
|
|
|
|
|
|
|
if (workers->IsEmpty()) {
|
|
|
|
iter.Remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-01 02:11:03 +03:00
|
|
|
} else if (aWorkerPrivate->IsDedicatedWorker()) {
|
2013-06-05 18:04:23 +04:00
|
|
|
// May be null.
|
2016-01-30 20:05:36 +03:00
|
|
|
nsPIDOMWindowInner* window = aWorkerPrivate->GetWindow();
|
2017-06-18 18:07:54 +03:00
|
|
|
if (auto entry = mWindowMap.Lookup(window)) {
|
|
|
|
MOZ_ALWAYS_TRUE(entry.Data()->RemoveElement(aWorkerPrivate));
|
|
|
|
if (entry.Data()->IsEmpty()) {
|
|
|
|
entry.Remove();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT_UNREACHABLE("window is not in mWindowMap");
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2016-03-28 20:28:14 +03:00
|
|
|
if (queuedWorker && !ScheduleWorker(queuedWorker)) {
|
|
|
|
UnregisterWorker(queuedWorker);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-28 20:28:14 +03:00
|
|
|
bool RuntimeService::ScheduleWorker(WorkerPrivate* aWorkerPrivate) {
|
2011-07-17 23:09:13 +04:00
|
|
|
if (!aWorkerPrivate->Start()) {
|
|
|
|
// This is ok, means that we didn't need to make a thread for this worker.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WorkerThread> thread;
|
2011-07-26 05:49:16 +04:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
if (!mIdleThreadArray.IsEmpty()) {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t index = mIdleThreadArray.Length() - 1;
|
2011-07-26 05:49:16 +04:00
|
|
|
mIdleThreadArray[index].mThread.swap(thread);
|
|
|
|
mIdleThreadArray.RemoveElementAt(index);
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2014-11-17 22:55:37 +03:00
|
|
|
const WorkerThreadFriendKey friendKey;
|
|
|
|
|
2011-07-26 05:49:16 +04:00
|
|
|
if (!thread) {
|
2014-11-17 22:55:37 +03:00
|
|
|
thread = WorkerThread::Create(friendKey);
|
2013-10-23 17:16:49 +04:00
|
|
|
if (!thread) {
|
2016-03-28 20:28:14 +03:00
|
|
|
UnregisterWorker(aWorkerPrivate);
|
2011-07-26 05:49:16 +04:00
|
|
|
return false;
|
|
|
|
}
|
2013-05-17 02:49:43 +04:00
|
|
|
}
|
2011-07-26 05:49:16 +04:00
|
|
|
|
2019-07-11 13:49:55 +03:00
|
|
|
if (NS_FAILED(thread->SetPriority(nsISupportsPriority::PRIORITY_NORMAL))) {
|
2013-05-17 02:49:43 +04:00
|
|
|
NS_WARNING("Could not set the thread's priority!");
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
aWorkerPrivate->SetThread(thread);
|
2016-09-14 16:47:32 +03:00
|
|
|
JSContext* cx = CycleCollectedJSContext::Get()->Context();
|
2013-10-23 17:16:49 +04:00
|
|
|
nsCOMPtr<nsIRunnable> runnable = new WorkerThreadPrimaryRunnable(
|
2016-08-09 05:43:15 +03:00
|
|
|
aWorkerPrivate, thread, JS_GetParentRuntime(cx));
|
2015-07-10 06:21:46 +03:00
|
|
|
if (NS_FAILED(
|
|
|
|
thread->DispatchPrimaryRunnable(friendKey, runnable.forget()))) {
|
2016-03-28 20:28:14 +03:00
|
|
|
UnregisterWorker(aWorkerPrivate);
|
2011-07-17 23:09:13 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-07-26 05:49:16 +04:00
|
|
|
// static
|
|
|
|
void RuntimeService::ShutdownIdleThreads(nsITimer* aTimer,
|
|
|
|
void* /* aClosure */) {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
RuntimeService* runtime = RuntimeService::GetService();
|
|
|
|
NS_ASSERTION(runtime, "This should never be null!");
|
|
|
|
|
|
|
|
NS_ASSERTION(aTimer == runtime->mIdleThreadTimer, "Wrong timer!");
|
|
|
|
|
|
|
|
// Cheat a little and grab all threads that expire within one second of now.
|
2015-05-07 20:35:57 +03:00
|
|
|
TimeStamp now = TimeStamp::NowLoRes() + TimeDuration::FromSeconds(1);
|
2011-07-26 05:49:16 +04:00
|
|
|
|
|
|
|
TimeStamp nextExpiration;
|
|
|
|
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<RefPtr<WorkerThread>, 20> expiredThreads;
|
2011-07-26 05:49:16 +04:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(runtime->mMutex);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t index = 0; index < runtime->mIdleThreadArray.Length();
|
2011-07-26 05:49:16 +04:00
|
|
|
index++) {
|
|
|
|
IdleThreadInfo& info = runtime->mIdleThreadArray[index];
|
|
|
|
if (info.mExpirationTime > now) {
|
|
|
|
nextExpiration = info.mExpirationTime;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WorkerThread>* thread = expiredThreads.AppendElement();
|
2011-07-26 05:49:16 +04:00
|
|
|
thread->swap(info.mThread);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!expiredThreads.IsEmpty()) {
|
|
|
|
runtime->mIdleThreadArray.RemoveElementsAt(0, expiredThreads.Length());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!nextExpiration.IsNull()) {
|
2015-05-07 20:35:57 +03:00
|
|
|
TimeDuration delta = nextExpiration - TimeStamp::NowLoRes();
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t delay(delta > TimeDuration(0) ? delta.ToMilliseconds() : 0);
|
2011-07-26 05:49:16 +04:00
|
|
|
|
|
|
|
// Reschedule the timer.
|
2017-06-29 22:13:25 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(aTimer->InitWithNamedFuncCallback(
|
|
|
|
ShutdownIdleThreads, nullptr, delay, nsITimer::TYPE_ONE_SHOT,
|
|
|
|
"RuntimeService::ShutdownIdleThreads"));
|
2015-05-07 20:35:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t index = 0; index < expiredThreads.Length(); index++) {
|
|
|
|
if (NS_FAILED(expiredThreads[index]->Shutdown())) {
|
|
|
|
NS_WARNING("Failed to shutdown thread!");
|
2011-07-26 05:49:16 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
nsresult RuntimeService::Init() {
|
|
|
|
AssertIsOnMainThread();
|
2013-05-17 02:49:43 +04:00
|
|
|
|
2012-11-05 11:30:00 +04:00
|
|
|
nsLayoutStatics::AddRef();
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-05-17 02:49:43 +04:00
|
|
|
// Initialize JSSettings.
|
2020-04-30 12:18:27 +03:00
|
|
|
sDefaultJSSettings = MakeUnique<JSSettings>();
|
|
|
|
sDefaultJSSettings->chrome.maxScriptRuntime = -1;
|
|
|
|
sDefaultJSSettings->content.maxScriptRuntime = MAX_SCRIPT_RUN_TIME_SEC;
|
|
|
|
SetDefaultJSGCSettings(JSGC_MAX_BYTES, Some(WORKER_DEFAULT_RUNTIME_HEAPSIZE));
|
|
|
|
SetDefaultJSGCSettings(JSGC_ALLOCATION_THRESHOLD,
|
|
|
|
Some(WORKER_DEFAULT_ALLOCATION_THRESHOLD));
|
2013-05-17 02:49:43 +04:00
|
|
|
|
2017-03-08 20:37:08 +03:00
|
|
|
// nsIStreamTransportService is thread-safe but it must be initialized on the
|
|
|
|
// main-thread. FileReader needs it, so, let's initialize it now.
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIStreamTransportService> sts =
|
|
|
|
do_GetService(kStreamTransportServiceCID, &rv);
|
|
|
|
NS_ENSURE_TRUE(sts, NS_ERROR_FAILURE);
|
|
|
|
|
2017-10-16 09:15:40 +03:00
|
|
|
mIdleThreadTimer = NS_NewTimer();
|
2011-07-26 05:49:16 +04:00
|
|
|
NS_ENSURE_STATE(mIdleThreadTimer);
|
|
|
|
|
2011-12-29 22:28:09 +04:00
|
|
|
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
|
|
|
NS_ENSURE_TRUE(obs, NS_ERROR_FAILURE);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2017-03-08 20:37:08 +03:00
|
|
|
rv = obs->AddObserver(this, NS_XPCOM_SHUTDOWN_THREADS_OBSERVER_ID, false);
|
2011-07-17 23:09:13 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2018-02-06 03:46:08 +03:00
|
|
|
rv = obs->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false);
|
2013-09-09 21:54:05 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
mObserved = true;
|
|
|
|
|
2012-01-18 00:05:25 +04:00
|
|
|
if (NS_FAILED(obs->AddObserver(this, GC_REQUEST_OBSERVER_TOPIC, false))) {
|
|
|
|
NS_WARNING("Failed to register for GC request notifications!");
|
|
|
|
}
|
|
|
|
|
2013-12-03 08:07:02 +04:00
|
|
|
if (NS_FAILED(obs->AddObserver(this, CC_REQUEST_OBSERVER_TOPIC, false))) {
|
|
|
|
NS_WARNING("Failed to register for CC request notifications!");
|
|
|
|
}
|
|
|
|
|
2012-01-18 00:05:25 +04:00
|
|
|
if (NS_FAILED(
|
|
|
|
obs->AddObserver(this, MEMORY_PRESSURE_OBSERVER_TOPIC, false))) {
|
|
|
|
NS_WARNING("Failed to register for memory pressure notifications!");
|
|
|
|
}
|
|
|
|
|
2013-11-20 03:08:50 +04:00
|
|
|
if (NS_FAILED(
|
|
|
|
obs->AddObserver(this, NS_IOSERVICE_OFFLINE_STATUS_TOPIC, false))) {
|
|
|
|
NS_WARNING("Failed to register for offline notification event!");
|
|
|
|
}
|
|
|
|
|
2014-10-27 12:38:14 +03:00
|
|
|
MOZ_ASSERT(!gRuntimeServiceDuringInit, "This should be false!");
|
|
|
|
gRuntimeServiceDuringInit = true;
|
|
|
|
|
2020-04-30 12:18:23 +03:00
|
|
|
#define WORKER_PREF(name, callback) \
|
|
|
|
NS_FAILED(Preferences::RegisterCallbackAndCall(callback, name))
|
|
|
|
|
2017-03-21 21:59:02 +03:00
|
|
|
if (NS_FAILED(Preferences::RegisterPrefixCallback(
|
2013-05-17 02:49:43 +04:00
|
|
|
LoadJSGCMemoryOptions,
|
2017-03-21 21:59:02 +03:00
|
|
|
PREF_JS_OPTIONS_PREFIX PREF_MEM_OPTIONS_PREFIX)) ||
|
|
|
|
NS_FAILED(Preferences::RegisterPrefixCallbackAndCall(
|
2013-05-17 02:49:43 +04:00
|
|
|
LoadJSGCMemoryOptions,
|
2017-03-21 21:59:02 +03:00
|
|
|
PREF_WORKERS_OPTIONS_PREFIX PREF_MEM_OPTIONS_PREFIX)) ||
|
2013-05-17 02:49:43 +04:00
|
|
|
#ifdef JS_GC_ZEAL
|
|
|
|
NS_FAILED(Preferences::RegisterCallback(
|
2017-03-21 21:59:02 +03:00
|
|
|
LoadGCZealOptions, PREF_JS_OPTIONS_PREFIX PREF_GCZEAL)) ||
|
2013-10-31 01:40:34 +04:00
|
|
|
#endif
|
2020-04-30 12:18:23 +03:00
|
|
|
WORKER_PREF("intl.accept_languages", PrefLanguagesChanged) ||
|
|
|
|
WORKER_PREF("general.appname.override", AppNameOverrideChanged) ||
|
|
|
|
WORKER_PREF("general.appversion.override", AppVersionOverrideChanged) ||
|
|
|
|
WORKER_PREF("general.platform.override", PlatformOverrideChanged) ||
|
2018-01-08 16:05:07 +03:00
|
|
|
#ifdef JS_GC_ZEAL
|
2020-04-30 12:18:23 +03:00
|
|
|
WORKER_PREF("dom.workers.options.gcZeal", LoadGCZealOptions) ||
|
2018-01-08 16:05:07 +03:00
|
|
|
#endif
|
2020-04-30 12:18:23 +03:00
|
|
|
NS_FAILED(Preferences::RegisterPrefixCallbackAndCall(
|
|
|
|
LoadContextOptions, PREF_WORKERS_OPTIONS_PREFIX)) ||
|
2017-03-21 21:59:02 +03:00
|
|
|
NS_FAILED(Preferences::RegisterPrefixCallback(LoadContextOptions,
|
|
|
|
PREF_JS_OPTIONS_PREFIX))) {
|
2013-05-17 02:49:43 +04:00
|
|
|
NS_WARNING("Failed to register pref callbacks!");
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2020-04-30 12:18:23 +03:00
|
|
|
#undef WORKER_PREF
|
|
|
|
|
2014-10-27 12:38:14 +03:00
|
|
|
MOZ_ASSERT(gRuntimeServiceDuringInit, "Should be true!");
|
|
|
|
gRuntimeServiceDuringInit = false;
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
// We assume atomic 32bit reads/writes. If this assumption doesn't hold on
|
|
|
|
// some wacky platform then the worst that could happen is that the close
|
|
|
|
// handler will run for a slightly different amount of time.
|
2020-04-30 12:18:27 +03:00
|
|
|
Preferences::AddIntVarCache(&sDefaultJSSettings->content.maxScriptRuntime,
|
2019-07-30 09:19:46 +03:00
|
|
|
PREF_MAX_SCRIPT_RUN_TIME_CONTENT,
|
|
|
|
MAX_SCRIPT_RUN_TIME_SEC);
|
2020-04-30 12:18:27 +03:00
|
|
|
Preferences::AddIntVarCache(&sDefaultJSSettings->chrome.maxScriptRuntime,
|
2019-07-30 09:19:46 +03:00
|
|
|
PREF_MAX_SCRIPT_RUN_TIME_CHROME, -1);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2016-08-19 09:38:58 +03:00
|
|
|
int32_t maxPerDomain =
|
|
|
|
Preferences::GetInt(PREF_WORKERS_MAX_PER_DOMAIN, MAX_WORKERS_PER_DOMAIN);
|
|
|
|
gMaxWorkersPerDomain = std::max(0, maxPerDomain);
|
|
|
|
|
|
|
|
int32_t maxHardwareConcurrency = Preferences::GetInt(
|
|
|
|
PREF_WORKERS_MAX_HARDWARE_CONCURRENCY, MAX_HARDWARE_CONCURRENCY);
|
|
|
|
gMaxHardwareConcurrency = std::max(0, maxHardwareConcurrency);
|
|
|
|
|
2017-11-10 21:27:03 +03:00
|
|
|
RefPtr<OSFileConstantsService> osFileConstantsService =
|
|
|
|
OSFileConstantsService::GetOrCreate();
|
|
|
|
if (NS_WARN_IF(!osFileConstantsService)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2012-06-29 05:56:30 +04:00
|
|
|
}
|
|
|
|
|
2014-12-17 09:26:15 +03:00
|
|
|
if (NS_WARN_IF(!IndexedDatabaseManager::GetOrCreate())) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2017-12-18 19:49:54 +03:00
|
|
|
// PerformanceService must be initialized on the main-thread.
|
|
|
|
PerformanceService::GetOrCreate();
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-09-09 21:54:05 +04:00
|
|
|
void RuntimeService::Shutdown() {
|
2011-07-17 23:09:13 +04:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2013-09-09 21:54:05 +04:00
|
|
|
MOZ_ASSERT(!mShuttingDown);
|
|
|
|
// That's it, no more workers.
|
|
|
|
mShuttingDown = true;
|
|
|
|
|
2011-12-29 22:28:09 +04:00
|
|
|
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
2016-09-01 08:01:16 +03:00
|
|
|
NS_WARNING_ASSERTION(obs, "Failed to get observer service?!");
|
2011-12-29 22:28:09 +04:00
|
|
|
|
|
|
|
// Tell anyone that cares that they're about to lose worker support.
|
2012-07-30 18:20:58 +04:00
|
|
|
if (obs && NS_FAILED(obs->NotifyObservers(nullptr, WORKERS_SHUTDOWN_TOPIC,
|
|
|
|
nullptr))) {
|
2011-12-29 22:28:09 +04:00
|
|
|
NS_WARNING("NotifyObservers failed!");
|
|
|
|
}
|
|
|
|
|
2013-09-02 12:41:57 +04:00
|
|
|
{
|
2011-07-26 05:49:16 +04:00
|
|
|
MutexAutoLock lock(mMutex);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<WorkerPrivate*, 100> workers;
|
2015-10-27 02:41:55 +03:00
|
|
|
AddAllTopLevelWorkersToArray(workers);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
if (!workers.IsEmpty()) {
|
|
|
|
// Cancel all top-level workers.
|
|
|
|
{
|
2011-07-26 05:49:16 +04:00
|
|
|
MutexAutoUnlock unlock(mMutex);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t index = 0; index < workers.Length(); index++) {
|
2019-04-24 20:18:29 +03:00
|
|
|
if (!workers[index]->Cancel()) {
|
2011-07-17 23:09:13 +04:00
|
|
|
NS_WARNING("Failed to cancel worker!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-09 21:54:05 +04:00
|
|
|
}
|
|
|
|
}
|
2020-04-30 12:18:27 +03:00
|
|
|
|
|
|
|
sDefaultJSSettings = nullptr;
|
2013-09-09 21:54:05 +04:00
|
|
|
}
|
|
|
|
|
2017-11-15 09:58:38 +03:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class CrashIfHangingRunnable : public WorkerControlRunnable {
|
|
|
|
public:
|
|
|
|
explicit CrashIfHangingRunnable(WorkerPrivate* aWorkerPrivate)
|
|
|
|
: WorkerControlRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount),
|
|
|
|
mMonitor("CrashIfHangingRunnable::mMonitor") {}
|
|
|
|
|
|
|
|
bool WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override {
|
|
|
|
aWorkerPrivate->DumpCrashInformation(mMsg);
|
|
|
|
|
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
lock.Notify();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult Cancel() override {
|
|
|
|
mMsg.Assign("Canceled");
|
|
|
|
|
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
lock.Notify();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2020-05-13 20:48:19 +03:00
|
|
|
bool DispatchAndWait() {
|
2017-11-15 09:58:38 +03:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
|
|
|
|
if (!Dispatch()) {
|
2020-05-13 20:48:19 +03:00
|
|
|
// The worker is already dead but the main thread still didn't remove it
|
|
|
|
// from RuntimeService's registry.
|
|
|
|
return false;
|
2017-11-15 09:58:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
lock.Wait();
|
2020-05-13 20:48:19 +03:00
|
|
|
return true;
|
2017-11-15 09:58:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const nsCString& MsgData() const { return mMsg; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool PreDispatch(WorkerPrivate* aWorkerPrivate) override { return true; }
|
|
|
|
|
|
|
|
void PostDispatch(WorkerPrivate* aWorkerPrivate,
|
|
|
|
bool aDispatchResult) override {}
|
|
|
|
|
|
|
|
Monitor mMonitor;
|
|
|
|
nsCString mMsg;
|
|
|
|
};
|
|
|
|
|
2020-05-13 20:48:19 +03:00
|
|
|
struct ActiveWorkerStats {
|
|
|
|
template <uint32_t ActiveWorkerStats::*Category>
|
|
|
|
void Update(const nsTArray<WorkerPrivate*>& aWorkers) {
|
|
|
|
for (const auto worker : aWorkers) {
|
|
|
|
RefPtr<CrashIfHangingRunnable> runnable =
|
|
|
|
new CrashIfHangingRunnable(worker);
|
|
|
|
if (runnable->DispatchAndWait()) {
|
|
|
|
++(this->*Category);
|
|
|
|
|
|
|
|
// BC: Busy Count
|
|
|
|
mMessage.AppendPrintf("-BC:%d", worker->BusyCount());
|
|
|
|
mMessage.Append(runnable->MsgData());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t mWorkers = 0;
|
|
|
|
uint32_t mServiceWorkers = 0;
|
|
|
|
nsCString mMessage;
|
|
|
|
};
|
|
|
|
|
2017-11-15 09:58:38 +03:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void RuntimeService::CrashIfHanging() {
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
2020-05-13 20:48:19 +03:00
|
|
|
ActiveWorkerStats activeStats;
|
2017-11-15 09:58:38 +03:00
|
|
|
uint32_t inactiveWorkers = 0;
|
|
|
|
|
|
|
|
for (auto iter = mDomainMap.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
WorkerDomainInfo* aData = iter.UserData();
|
|
|
|
|
2020-05-13 20:48:19 +03:00
|
|
|
activeStats.Update<&ActiveWorkerStats::mWorkers>(aData->mActiveWorkers);
|
|
|
|
activeStats.Update<&ActiveWorkerStats::mServiceWorkers>(
|
|
|
|
aData->mActiveServiceWorkers);
|
2017-11-15 09:58:38 +03:00
|
|
|
|
|
|
|
// These might not be top-level workers...
|
|
|
|
for (uint32_t index = 0; index < aData->mQueuedWorkers.Length(); index++) {
|
|
|
|
WorkerPrivate* worker = aData->mQueuedWorkers[index];
|
|
|
|
if (!worker->GetParent()) {
|
|
|
|
++inactiveWorkers;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-13 20:48:19 +03:00
|
|
|
if (activeStats.mWorkers + activeStats.mServiceWorkers + inactiveWorkers ==
|
|
|
|
0) {
|
|
|
|
return;
|
|
|
|
}
|
2017-11-15 09:58:38 +03:00
|
|
|
|
|
|
|
nsCString msg;
|
|
|
|
|
|
|
|
// A: active Workers | S: active ServiceWorkers | Q: queued Workers
|
2018-01-29 19:08:41 +03:00
|
|
|
msg.AppendPrintf("Workers Hanging - %d|A:%d|S:%d|Q:%d", mShuttingDown ? 1 : 0,
|
2020-05-13 20:48:19 +03:00
|
|
|
activeStats.mWorkers, activeStats.mServiceWorkers,
|
|
|
|
inactiveWorkers);
|
|
|
|
msg.Append(activeStats.mMessage);
|
2017-11-15 09:58:38 +03:00
|
|
|
|
|
|
|
// This string will be leaked.
|
2019-02-03 11:09:37 +03:00
|
|
|
MOZ_CRASH_UNSAFE(strdup(msg.BeginReading()));
|
2017-11-15 09:58:38 +03:00
|
|
|
}
|
|
|
|
|
2013-09-09 21:54:05 +04:00
|
|
|
// This spins the event loop until all workers are finished and their threads
|
|
|
|
// have been joined.
|
|
|
|
void RuntimeService::Cleanup() {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2018-10-30 19:51:12 +03:00
|
|
|
if (!mShuttingDown) {
|
|
|
|
Shutdown();
|
|
|
|
}
|
|
|
|
|
2013-09-09 21:54:05 +04:00
|
|
|
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
2016-09-01 08:01:16 +03:00
|
|
|
NS_WARNING_ASSERTION(obs, "Failed to get observer service?!");
|
2013-09-09 21:54:05 +04:00
|
|
|
|
|
|
|
if (mIdleThreadTimer) {
|
|
|
|
if (NS_FAILED(mIdleThreadTimer->Cancel())) {
|
|
|
|
NS_WARNING("Failed to cancel idle timer!");
|
|
|
|
}
|
|
|
|
mIdleThreadTimer = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<WorkerPrivate*, 100> workers;
|
2015-10-27 02:41:55 +03:00
|
|
|
AddAllTopLevelWorkersToArray(workers);
|
2013-09-09 21:54:05 +04:00
|
|
|
|
|
|
|
if (!workers.IsEmpty()) {
|
|
|
|
nsIThread* currentThread = NS_GetCurrentThread();
|
|
|
|
NS_ASSERTION(currentThread, "This should never be null!");
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2011-07-26 05:49:16 +04:00
|
|
|
// Shut down any idle threads.
|
|
|
|
if (!mIdleThreadArray.IsEmpty()) {
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<RefPtr<WorkerThread>, 20> idleThreads;
|
2011-07-26 05:49:16 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t idleThreadCount = mIdleThreadArray.Length();
|
2011-07-26 05:49:16 +04:00
|
|
|
idleThreads.SetLength(idleThreadCount);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t index = 0; index < idleThreadCount; index++) {
|
2011-07-26 05:49:16 +04:00
|
|
|
NS_ASSERTION(mIdleThreadArray[index].mThread, "Null thread!");
|
|
|
|
idleThreads[index].swap(mIdleThreadArray[index].mThread);
|
|
|
|
}
|
|
|
|
|
|
|
|
mIdleThreadArray.Clear();
|
|
|
|
|
|
|
|
MutexAutoUnlock unlock(mMutex);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t index = 0; index < idleThreadCount; index++) {
|
2011-07-26 05:49:16 +04:00
|
|
|
if (NS_FAILED(idleThreads[index]->Shutdown())) {
|
|
|
|
NS_WARNING("Failed to shutdown thread!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
// And make sure all their final messages have run and all their threads
|
|
|
|
// have joined.
|
|
|
|
while (mDomainMap.Count()) {
|
2011-07-26 05:49:16 +04:00
|
|
|
MutexAutoUnlock unlock(mMutex);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-01-18 00:05:25 +04:00
|
|
|
if (!NS_ProcessNextEvent(currentThread)) {
|
2011-07-17 23:09:13 +04:00
|
|
|
NS_WARNING("Something bad happened!");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-02 12:41:57 +04:00
|
|
|
NS_ASSERTION(!mWindowMap.Count(), "All windows should have been released!");
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2020-04-30 12:18:23 +03:00
|
|
|
#define WORKER_PREF(name, callback) \
|
|
|
|
NS_FAILED(Preferences::UnregisterCallback(callback, name))
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
if (mObserved) {
|
2017-03-21 21:59:02 +03:00
|
|
|
if (NS_FAILED(Preferences::UnregisterPrefixCallback(
|
|
|
|
LoadContextOptions, PREF_JS_OPTIONS_PREFIX)) ||
|
|
|
|
NS_FAILED(Preferences::UnregisterPrefixCallback(
|
|
|
|
LoadContextOptions, PREF_WORKERS_OPTIONS_PREFIX)) ||
|
2020-04-30 12:18:23 +03:00
|
|
|
WORKER_PREF("intl.accept_languages", PrefLanguagesChanged) ||
|
|
|
|
WORKER_PREF("general.appname.override", AppNameOverrideChanged) ||
|
|
|
|
WORKER_PREF("general.appversion.override", AppVersionOverrideChanged) ||
|
|
|
|
WORKER_PREF("general.platform.override", PlatformOverrideChanged) ||
|
2018-01-08 16:05:07 +03:00
|
|
|
#ifdef JS_GC_ZEAL
|
2020-04-30 12:18:23 +03:00
|
|
|
WORKER_PREF("dom.workers.options.gcZeal", LoadGCZealOptions) ||
|
|
|
|
NS_FAILED(Preferences::UnregisterCallback(
|
|
|
|
LoadGCZealOptions, PREF_JS_OPTIONS_PREFIX PREF_GCZEAL)) ||
|
2013-05-17 02:49:43 +04:00
|
|
|
#endif
|
2017-03-21 21:59:02 +03:00
|
|
|
NS_FAILED(Preferences::UnregisterPrefixCallback(
|
2013-05-17 02:49:43 +04:00
|
|
|
LoadJSGCMemoryOptions,
|
2017-03-21 21:59:02 +03:00
|
|
|
PREF_JS_OPTIONS_PREFIX PREF_MEM_OPTIONS_PREFIX)) ||
|
|
|
|
NS_FAILED(Preferences::UnregisterPrefixCallback(
|
2013-05-17 02:49:43 +04:00
|
|
|
LoadJSGCMemoryOptions,
|
2017-03-21 21:59:02 +03:00
|
|
|
PREF_WORKERS_OPTIONS_PREFIX PREF_MEM_OPTIONS_PREFIX))) {
|
2013-05-17 02:49:43 +04:00
|
|
|
NS_WARNING("Failed to unregister pref callbacks!");
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2020-04-30 12:18:23 +03:00
|
|
|
#undef WORKER_PREF
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
if (obs) {
|
2012-01-18 00:05:25 +04:00
|
|
|
if (NS_FAILED(obs->RemoveObserver(this, GC_REQUEST_OBSERVER_TOPIC))) {
|
|
|
|
NS_WARNING("Failed to unregister for GC request notifications!");
|
|
|
|
}
|
|
|
|
|
2013-12-03 08:07:02 +04:00
|
|
|
if (NS_FAILED(obs->RemoveObserver(this, CC_REQUEST_OBSERVER_TOPIC))) {
|
|
|
|
NS_WARNING("Failed to unregister for CC request notifications!");
|
|
|
|
}
|
|
|
|
|
2012-01-18 00:05:25 +04:00
|
|
|
if (NS_FAILED(
|
|
|
|
obs->RemoveObserver(this, MEMORY_PRESSURE_OBSERVER_TOPIC))) {
|
|
|
|
NS_WARNING("Failed to unregister for memory pressure notifications!");
|
|
|
|
}
|
|
|
|
|
2013-11-20 03:08:50 +04:00
|
|
|
if (NS_FAILED(
|
|
|
|
obs->RemoveObserver(this, NS_IOSERVICE_OFFLINE_STATUS_TOPIC))) {
|
|
|
|
NS_WARNING("Failed to unregister for offline notification event!");
|
|
|
|
}
|
2013-09-09 21:54:05 +04:00
|
|
|
obs->RemoveObserver(this, NS_XPCOM_SHUTDOWN_THREADS_OBSERVER_ID);
|
2018-02-06 03:46:08 +03:00
|
|
|
obs->RemoveObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID);
|
2013-09-09 21:54:05 +04:00
|
|
|
mObserved = false;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
}
|
2012-06-29 05:56:30 +04:00
|
|
|
|
2012-11-05 11:30:00 +04:00
|
|
|
nsLayoutStatics::Release();
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2015-10-27 02:41:55 +03:00
|
|
|
void RuntimeService::AddAllTopLevelWorkersToArray(
|
|
|
|
nsTArray<WorkerPrivate*>& aWorkers) {
|
|
|
|
for (auto iter = mDomainMap.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
WorkerDomainInfo* aData = iter.UserData();
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2016-08-19 09:38:58 +03:00
|
|
|
for (uint32_t index = 0; index < aData->mActiveWorkers.Length(); index++) {
|
|
|
|
MOZ_ASSERT(!aData->mActiveWorkers[index]->GetParent(),
|
2015-10-27 02:41:55 +03:00
|
|
|
"Shouldn't have a parent in this list!");
|
|
|
|
}
|
2016-08-19 09:38:58 +03:00
|
|
|
for (uint32_t index = 0; index < aData->mActiveServiceWorkers.Length();
|
|
|
|
index++) {
|
|
|
|
MOZ_ASSERT(!aData->mActiveServiceWorkers[index]->GetParent(),
|
2015-10-27 02:41:55 +03:00
|
|
|
"Shouldn't have a parent in this list!");
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
#endif
|
|
|
|
|
2016-08-19 09:38:58 +03:00
|
|
|
aWorkers.AppendElements(aData->mActiveWorkers);
|
|
|
|
aWorkers.AppendElements(aData->mActiveServiceWorkers);
|
|
|
|
|
|
|
|
// These might not be top-level workers...
|
|
|
|
for (uint32_t index = 0; index < aData->mQueuedWorkers.Length(); index++) {
|
|
|
|
WorkerPrivate* worker = aData->mQueuedWorkers[index];
|
|
|
|
if (!worker->GetParent()) {
|
|
|
|
aWorkers.AppendElement(worker);
|
|
|
|
}
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
void RuntimeService::GetWorkersForWindow(nsPIDOMWindowInner* aWindow,
|
2011-07-17 23:09:13 +04:00
|
|
|
nsTArray<WorkerPrivate*>& aWorkers) {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
nsTArray<WorkerPrivate*>* workers;
|
|
|
|
if (mWindowMap.Get(aWindow, &workers)) {
|
|
|
|
NS_ASSERTION(!workers->IsEmpty(), "Should have been removed!");
|
|
|
|
aWorkers.AppendElements(*workers);
|
|
|
|
} else {
|
|
|
|
NS_ASSERTION(aWorkers.IsEmpty(), "Should be empty!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
void RuntimeService::CancelWorkersForWindow(nsPIDOMWindowInner* aWindow) {
|
2011-07-17 23:09:13 +04:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2016-08-22 13:13:15 +03:00
|
|
|
nsTArray<WorkerPrivate*> workers;
|
2011-07-17 23:09:13 +04:00
|
|
|
GetWorkersForWindow(aWindow, workers);
|
|
|
|
|
|
|
|
if (!workers.IsEmpty()) {
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t index = 0; index < workers.Length(); index++) {
|
2013-06-05 18:04:23 +04:00
|
|
|
WorkerPrivate*& worker = workers[index];
|
2018-11-20 02:18:21 +03:00
|
|
|
MOZ_ASSERT(!worker->IsSharedWorker());
|
|
|
|
worker->Cancel();
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
void RuntimeService::FreezeWorkersForWindow(nsPIDOMWindowInner* aWindow) {
|
2011-07-17 23:09:13 +04:00
|
|
|
AssertIsOnMainThread();
|
2013-06-05 18:04:23 +04:00
|
|
|
MOZ_ASSERT(aWindow);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2016-08-22 13:13:15 +03:00
|
|
|
nsTArray<WorkerPrivate*> workers;
|
2011-07-17 23:09:13 +04:00
|
|
|
GetWorkersForWindow(aWindow, workers);
|
|
|
|
|
2016-02-26 23:23:12 +03:00
|
|
|
for (uint32_t index = 0; index < workers.Length(); index++) {
|
2018-11-20 02:18:21 +03:00
|
|
|
MOZ_ASSERT(!workers[index]->IsSharedWorker());
|
2016-02-26 23:23:12 +03:00
|
|
|
workers[index]->Freeze(aWindow);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
void RuntimeService::ThawWorkersForWindow(nsPIDOMWindowInner* aWindow) {
|
2011-07-17 23:09:13 +04:00
|
|
|
AssertIsOnMainThread();
|
2013-06-05 18:04:23 +04:00
|
|
|
MOZ_ASSERT(aWindow);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2016-08-22 13:13:15 +03:00
|
|
|
nsTArray<WorkerPrivate*> workers;
|
2011-07-17 23:09:13 +04:00
|
|
|
GetWorkersForWindow(aWindow, workers);
|
|
|
|
|
2016-02-26 23:23:12 +03:00
|
|
|
for (uint32_t index = 0; index < workers.Length(); index++) {
|
2018-11-20 02:18:21 +03:00
|
|
|
MOZ_ASSERT(!workers[index]->IsSharedWorker());
|
2016-02-26 23:23:12 +03:00
|
|
|
workers[index]->Thaw(aWindow);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
void RuntimeService::SuspendWorkersForWindow(nsPIDOMWindowInner* aWindow) {
|
2015-10-07 13:20:59 +03:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(aWindow);
|
|
|
|
|
2016-08-22 13:13:15 +03:00
|
|
|
nsTArray<WorkerPrivate*> workers;
|
2015-10-07 13:20:59 +03:00
|
|
|
GetWorkersForWindow(aWindow, workers);
|
|
|
|
|
|
|
|
for (uint32_t index = 0; index < workers.Length(); index++) {
|
2018-11-20 02:18:21 +03:00
|
|
|
MOZ_ASSERT(!workers[index]->IsSharedWorker());
|
2016-10-04 13:14:07 +03:00
|
|
|
workers[index]->ParentWindowPaused();
|
2015-10-07 13:20:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
void RuntimeService::ResumeWorkersForWindow(nsPIDOMWindowInner* aWindow) {
|
2015-10-07 13:20:59 +03:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(aWindow);
|
|
|
|
|
2016-08-22 13:13:15 +03:00
|
|
|
nsTArray<WorkerPrivate*> workers;
|
2015-10-07 13:20:59 +03:00
|
|
|
GetWorkersForWindow(aWindow, workers);
|
|
|
|
|
|
|
|
for (uint32_t index = 0; index < workers.Length(); index++) {
|
2018-11-20 02:18:21 +03:00
|
|
|
MOZ_ASSERT(!workers[index]->IsSharedWorker());
|
2016-10-04 13:14:07 +03:00
|
|
|
workers[index]->ParentWindowResumed();
|
2015-10-07 13:20:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-02 15:02:31 +03:00
|
|
|
void RuntimeService::PropagateFirstPartyStorageAccessGranted(
|
2018-07-10 11:09:59 +03:00
|
|
|
nsPIDOMWindowInner* aWindow) {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(aWindow);
|
2020-03-04 11:59:08 +03:00
|
|
|
MOZ_ASSERT_IF(aWindow->GetExtantDoc(), aWindow->GetExtantDoc()
|
|
|
|
->CookieJarSettings()
|
2020-04-02 18:30:57 +03:00
|
|
|
->GetRejectThirdPartyContexts());
|
2018-07-10 11:09:59 +03:00
|
|
|
|
|
|
|
nsTArray<WorkerPrivate*> workers;
|
|
|
|
GetWorkersForWindow(aWindow, workers);
|
|
|
|
|
|
|
|
for (uint32_t index = 0; index < workers.Length(); index++) {
|
2020-06-02 15:02:31 +03:00
|
|
|
workers[index]->PropagateFirstPartyStorageAccessGranted();
|
2018-07-10 11:09:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
void RuntimeService::NoteIdleThread(WorkerThread* aThread) {
|
2011-07-26 05:49:16 +04:00
|
|
|
AssertIsOnMainThread();
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(aThread);
|
|
|
|
|
2015-05-07 20:35:57 +03:00
|
|
|
bool shutdownThread = mShuttingDown;
|
|
|
|
bool scheduleTimer = false;
|
2011-07-26 05:49:16 +04:00
|
|
|
|
2015-05-07 20:35:57 +03:00
|
|
|
if (!shutdownThread) {
|
|
|
|
static TimeDuration timeout =
|
|
|
|
TimeDuration::FromSeconds(IDLE_THREAD_TIMEOUT_SEC);
|
|
|
|
|
|
|
|
TimeStamp expirationTime = TimeStamp::NowLoRes() + timeout;
|
2011-07-26 05:49:16 +04:00
|
|
|
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
2015-05-07 20:35:57 +03:00
|
|
|
uint32_t previousIdleCount = mIdleThreadArray.Length();
|
|
|
|
|
|
|
|
if (previousIdleCount < MAX_IDLE_THREADS) {
|
2011-07-26 05:49:16 +04:00
|
|
|
IdleThreadInfo* info = mIdleThreadArray.AppendElement();
|
|
|
|
info->mThread = aThread;
|
|
|
|
info->mExpirationTime = expirationTime;
|
2015-05-07 20:35:57 +03:00
|
|
|
|
|
|
|
scheduleTimer = previousIdleCount == 0;
|
|
|
|
} else {
|
|
|
|
shutdownThread = true;
|
2011-07-26 05:49:16 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-07 20:35:57 +03:00
|
|
|
MOZ_ASSERT_IF(shutdownThread, !scheduleTimer);
|
|
|
|
MOZ_ASSERT_IF(scheduleTimer, !shutdownThread);
|
|
|
|
|
2011-07-26 05:49:16 +04:00
|
|
|
// Too many idle threads, just shut this one down.
|
2015-05-07 20:35:57 +03:00
|
|
|
if (shutdownThread) {
|
2016-03-28 20:28:15 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(aThread->Shutdown());
|
2015-05-07 20:35:57 +03:00
|
|
|
} else if (scheduleTimer) {
|
2017-06-29 22:13:25 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(mIdleThreadTimer->InitWithNamedFuncCallback(
|
|
|
|
ShutdownIdleThreads, nullptr, IDLE_THREAD_TIMEOUT_SEC * 1000,
|
|
|
|
nsITimer::TYPE_ONE_SHOT, "RuntimeService::ShutdownIdleThreads"));
|
2011-07-26 05:49:16 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-07 09:15:15 +03:00
|
|
|
void RuntimeService::UpdateAllWorkerContextOptions() {
|
|
|
|
BROADCAST_ALL_WORKERS(UpdateContextOptions,
|
2020-04-30 12:18:27 +03:00
|
|
|
sDefaultJSSettings->contextOptions);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2014-09-23 23:11:18 +04:00
|
|
|
void RuntimeService::UpdateAppNameOverridePreference(const nsAString& aValue) {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
mNavigatorProperties.mAppNameOverridden = aValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RuntimeService::UpdateAppVersionOverridePreference(
|
|
|
|
const nsAString& aValue) {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
mNavigatorProperties.mAppVersionOverridden = aValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RuntimeService::UpdatePlatformOverridePreference(const nsAString& aValue) {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
mNavigatorProperties.mPlatformOverridden = aValue;
|
|
|
|
}
|
|
|
|
|
2014-09-05 18:26:34 +04:00
|
|
|
void RuntimeService::UpdateAllWorkerLanguages(
|
|
|
|
const nsTArray<nsString>& aLanguages) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2020-05-11 11:22:17 +03:00
|
|
|
mNavigatorProperties.mLanguages = aLanguages.Clone();
|
2014-09-05 18:26:34 +04:00
|
|
|
BROADCAST_ALL_WORKERS(UpdateLanguages, aLanguages);
|
|
|
|
}
|
|
|
|
|
2013-05-17 02:49:43 +04:00
|
|
|
void RuntimeService::UpdateAllWorkerMemoryParameter(JSGCParamKey aKey,
|
2020-04-30 12:18:27 +03:00
|
|
|
Maybe<uint32_t> aValue) {
|
2013-05-17 02:49:43 +04:00
|
|
|
BROADCAST_ALL_WORKERS(UpdateJSWorkerMemoryParameter, aKey, aValue);
|
2012-01-04 23:11:32 +04:00
|
|
|
}
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
#ifdef JS_GC_ZEAL
|
|
|
|
void RuntimeService::UpdateAllWorkerGCZeal() {
|
2020-04-30 12:18:27 +03:00
|
|
|
BROADCAST_ALL_WORKERS(UpdateGCZeal, sDefaultJSSettings->gcZeal,
|
|
|
|
sDefaultJSSettings->gcZealFrequency);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-06-24 20:24:47 +03:00
|
|
|
void RuntimeService::SetLowMemoryStateAllWorkers(bool aState) {
|
|
|
|
BROADCAST_ALL_WORKERS(SetLowMemoryState, aState);
|
|
|
|
}
|
|
|
|
|
2012-01-18 00:05:25 +04:00
|
|
|
void RuntimeService::GarbageCollectAllWorkers(bool aShrinking) {
|
|
|
|
BROADCAST_ALL_WORKERS(GarbageCollect, aShrinking);
|
|
|
|
}
|
|
|
|
|
2013-12-03 08:07:02 +04:00
|
|
|
void RuntimeService::CycleCollectAllWorkers() {
|
|
|
|
BROADCAST_ALL_WORKERS(CycleCollect, /* dummy = */ false);
|
|
|
|
}
|
|
|
|
|
2013-11-20 03:08:50 +04:00
|
|
|
void RuntimeService::SendOfflineStatusChangeEventToAllWorkers(bool aIsOffline) {
|
|
|
|
BROADCAST_ALL_WORKERS(OfflineStatusChangeEvent, aIsOffline);
|
|
|
|
}
|
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
void RuntimeService::MemoryPressureAllWorkers() {
|
|
|
|
BROADCAST_ALL_WORKERS(MemoryPressure, /* dummy = */ false);
|
|
|
|
}
|
|
|
|
|
2016-03-16 22:41:38 +03:00
|
|
|
uint32_t RuntimeService::ClampedHardwareConcurrency() const {
|
2017-05-03 00:03:08 +03:00
|
|
|
// The Firefox Hardware Report says 70% of Firefox users have exactly 2 cores.
|
|
|
|
// When the resistFingerprinting pref is set, we want to blend into the crowd
|
|
|
|
// so spoof navigator.hardwareConcurrency = 2 to reduce user uniqueness.
|
|
|
|
if (MOZ_UNLIKELY(nsContentUtils::ShouldResistFingerprinting())) {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2016-03-16 22:41:38 +03:00
|
|
|
// This needs to be atomic, because multiple workers, and even mainthread,
|
|
|
|
// could race to initialize it at once.
|
|
|
|
static Atomic<uint32_t> clampedHardwareConcurrency;
|
|
|
|
|
|
|
|
// No need to loop here: if compareExchange fails, that just means that some
|
|
|
|
// other worker has initialized numberOfProcessors, so we're good to go.
|
|
|
|
if (!clampedHardwareConcurrency) {
|
2019-05-08 01:16:28 +03:00
|
|
|
int32_t numberOfProcessors = 0;
|
|
|
|
#if defined(XP_MACOSX)
|
|
|
|
if (nsMacUtilsImpl::IsTCSMAvailable()) {
|
|
|
|
// On failure, zero is returned from GetPhysicalCPUCount()
|
|
|
|
// and we fallback to PR_GetNumberOfProcessors below.
|
|
|
|
numberOfProcessors = nsMacUtilsImpl::GetPhysicalCPUCount();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (numberOfProcessors == 0) {
|
|
|
|
numberOfProcessors = PR_GetNumberOfProcessors();
|
|
|
|
}
|
2016-03-16 22:41:38 +03:00
|
|
|
if (numberOfProcessors <= 0) {
|
|
|
|
numberOfProcessors = 1; // Must be one there somewhere
|
|
|
|
}
|
2016-08-19 09:38:58 +03:00
|
|
|
uint32_t clampedValue =
|
|
|
|
std::min(uint32_t(numberOfProcessors), gMaxHardwareConcurrency);
|
2018-06-13 21:33:31 +03:00
|
|
|
Unused << clampedHardwareConcurrency.compareExchange(0, clampedValue);
|
2016-03-16 22:41:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return clampedHardwareConcurrency;
|
|
|
|
}
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
// nsISupports
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(RuntimeService, nsIObserver)
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
// nsIObserver
|
|
|
|
NS_IMETHODIMP
|
|
|
|
RuntimeService::Observe(nsISupports* aSubject, const char* aTopic,
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* aData) {
|
2011-07-17 23:09:13 +04:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2018-02-06 03:46:08 +03:00
|
|
|
if (!strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) {
|
2013-09-09 21:54:05 +04:00
|
|
|
Shutdown();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
if (!strcmp(aTopic, NS_XPCOM_SHUTDOWN_THREADS_OBSERVER_ID)) {
|
|
|
|
Cleanup();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-01-18 00:05:25 +04:00
|
|
|
if (!strcmp(aTopic, GC_REQUEST_OBSERVER_TOPIC)) {
|
2013-12-03 08:07:02 +04:00
|
|
|
GarbageCollectAllWorkers(/* shrinking = */ false);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (!strcmp(aTopic, CC_REQUEST_OBSERVER_TOPIC)) {
|
|
|
|
CycleCollectAllWorkers();
|
2012-01-18 00:05:25 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (!strcmp(aTopic, MEMORY_PRESSURE_OBSERVER_TOPIC)) {
|
2019-06-24 20:24:47 +03:00
|
|
|
nsDependentString data(aData);
|
2019-06-24 20:21:53 +03:00
|
|
|
// Don't continue to GC/CC if we are in an ongoing low-memory state since
|
|
|
|
// its very slow and it likely won't help us anyway.
|
2019-06-24 20:24:47 +03:00
|
|
|
if (data.EqualsLiteral(LOW_MEMORY_ONGOING_DATA)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (data.EqualsLiteral(LOW_MEMORY_DATA)) {
|
|
|
|
SetLowMemoryStateAllWorkers(true);
|
2019-06-24 20:21:53 +03:00
|
|
|
}
|
2019-06-24 20:24:47 +03:00
|
|
|
GarbageCollectAllWorkers(/* shrinking = */ true);
|
|
|
|
CycleCollectAllWorkers();
|
|
|
|
MemoryPressureAllWorkers();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (!strcmp(aTopic, MEMORY_PRESSURE_STOP_OBSERVER_TOPIC)) {
|
|
|
|
SetLowMemoryStateAllWorkers(false);
|
2012-01-18 00:05:25 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-11-20 03:08:50 +04:00
|
|
|
if (!strcmp(aTopic, NS_IOSERVICE_OFFLINE_STATUS_TOPIC)) {
|
|
|
|
SendOfflineStatusChangeEventToAllWorkers(NS_IsOffline());
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2018-06-18 08:43:11 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("Unknown observer topic!");
|
2011-07-17 23:09:13 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-10-31 01:40:34 +04:00
|
|
|
|
2016-05-03 11:21:57 +03:00
|
|
|
bool LogViolationDetailsRunnable::MainThreadRun() {
|
2013-10-23 17:16:49 +04:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
nsIContentSecurityPolicy* csp = mWorkerPrivate->GetCSP();
|
|
|
|
if (csp) {
|
|
|
|
if (mWorkerPrivate->GetReportCSPViolations()) {
|
|
|
|
csp->LogViolationDetails(nsIContentSecurityPolicy::VIOLATION_TYPE_EVAL,
|
2018-07-10 18:40:21 +03:00
|
|
|
nullptr, // triggering element
|
2018-10-23 09:17:13 +03:00
|
|
|
mWorkerPrivate->CSPEventListener(), mFileName,
|
2018-07-16 18:58:04 +03:00
|
|
|
mScriptSample, mLineNum, mColumnNum,
|
2014-01-02 23:14:06 +04:00
|
|
|
EmptyString(), EmptyString());
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 11:21:57 +03:00
|
|
|
return true;
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
2019-03-19 18:14:11 +03:00
|
|
|
// MOZ_CAN_RUN_SCRIPT_BOUNDARY until Runnable::Run is MOZ_CAN_RUN_SCRIPT. See
|
|
|
|
// bug 1535398.
|
|
|
|
MOZ_CAN_RUN_SCRIPT_BOUNDARY
|
2013-10-23 17:16:49 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
WorkerThreadPrimaryRunnable::Run() {
|
2018-01-25 02:20:27 +03:00
|
|
|
AUTO_PROFILER_LABEL_DYNAMIC_LOSSY_NSSTRING(
|
|
|
|
"WorkerThreadPrimaryRunnable::Run", OTHER, mWorkerPrivate->ScriptURL());
|
2014-11-17 22:55:37 +03:00
|
|
|
|
2018-01-25 02:20:27 +03:00
|
|
|
using mozilla::ipc::BackgroundChild;
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2016-07-18 12:46:36 +03:00
|
|
|
class MOZ_STACK_CLASS SetThreadHelper final {
|
|
|
|
// Raw pointer: this class is on the stack.
|
|
|
|
WorkerPrivate* mWorkerPrivate;
|
|
|
|
|
|
|
|
public:
|
|
|
|
SetThreadHelper(WorkerPrivate* aWorkerPrivate, WorkerThread* aThread)
|
2020-04-21 06:08:55 +03:00
|
|
|
: mWorkerPrivate(aWorkerPrivate) {
|
2016-07-18 12:46:36 +03:00
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
MOZ_ASSERT(aThread);
|
|
|
|
|
2018-11-13 23:22:40 +03:00
|
|
|
mWorkerPrivate->SetWorkerPrivateInWorkerThread(aThread);
|
2016-07-18 12:46:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
~SetThreadHelper() {
|
|
|
|
if (mWorkerPrivate) {
|
2018-11-13 23:22:40 +03:00
|
|
|
mWorkerPrivate->ResetWorkerPrivateInWorkerThread();
|
2016-07-18 12:46:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Nullify() {
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
2018-11-13 23:22:40 +03:00
|
|
|
mWorkerPrivate->ResetWorkerPrivateInWorkerThread();
|
2016-07-18 12:46:36 +03:00
|
|
|
mWorkerPrivate = nullptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
SetThreadHelper threadHelper(mWorkerPrivate, mThread);
|
2014-08-21 03:42:00 +04:00
|
|
|
|
2020-04-30 06:52:56 +03:00
|
|
|
auto failureCleanup = MakeScopeExit([&]() {
|
|
|
|
// The creation of threadHelper above is the point at which a worker is
|
|
|
|
// considered to have run, because the `mPreStartRunnables` are all
|
2020-05-14 10:44:44 +03:00
|
|
|
// re-dispatched after `mThread` is set. We need to let the WorkerPrivate
|
|
|
|
// know so it can clean up the various event loops and delete the worker.
|
|
|
|
mWorkerPrivate->RunLoopNeverRan();
|
2020-04-30 06:52:56 +03:00
|
|
|
});
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
2020-03-26 06:22:42 +03:00
|
|
|
// These need to be initialized on the worker thread before being used on
|
|
|
|
// the main thread.
|
|
|
|
mWorkerPrivate->EnsurePerformanceStorage();
|
|
|
|
mWorkerPrivate->EnsurePerformanceCounter();
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!BackgroundChild::GetOrCreateForCurrentThread())) {
|
2018-07-18 13:07:14 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
{
|
|
|
|
nsCycleCollector_startup();
|
|
|
|
|
2018-05-10 08:04:12 +03:00
|
|
|
auto context = MakeUnique<WorkerJSContext>(mWorkerPrivate);
|
|
|
|
nsresult rv = context->Initialize(mParentRuntime);
|
2016-02-14 16:30:25 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2018-05-10 08:04:12 +03:00
|
|
|
JSContext* cx = context->Context();
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2016-08-11 15:39:22 +03:00
|
|
|
if (!InitJSContextForWorker(mWorkerPrivate, cx)) {
|
2013-10-23 17:16:49 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2020-04-30 06:52:56 +03:00
|
|
|
failureCleanup.release();
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
{
|
2017-10-04 01:11:18 +03:00
|
|
|
PROFILER_SET_JS_CONTEXT(cx);
|
2013-10-23 17:16:49 +04:00
|
|
|
|
|
|
|
{
|
2019-03-19 18:14:11 +03:00
|
|
|
// We're on the worker thread here, and WorkerPrivate's refcounting is
|
|
|
|
// non-threadsafe: you can only do it on the parent thread. What that
|
|
|
|
// means in practice is that we're relying on it being kept alive while
|
|
|
|
// we run. Hopefully.
|
|
|
|
MOZ_KnownLive(mWorkerPrivate)->DoRunLoop(cx);
|
2016-02-26 23:23:13 +03:00
|
|
|
// The AutoJSAPI in DoRunLoop should have reported any exceptions left
|
2018-08-28 10:53:30 +03:00
|
|
|
// on cx.
|
2016-02-26 23:23:13 +03:00
|
|
|
MOZ_ASSERT(!JS_IsExceptionPending(cx));
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
2014-07-10 01:39:18 +04:00
|
|
|
BackgroundChild::CloseForCurrentThread();
|
|
|
|
|
2017-10-04 01:11:18 +03:00
|
|
|
PROFILER_CLEAR_JS_CONTEXT();
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
2016-02-26 19:32:28 +03:00
|
|
|
// There may still be runnables on the debugger event queue that hold a
|
|
|
|
// strong reference to the debugger global scope. These runnables are not
|
|
|
|
// visible to the cycle collector, so we need to make sure to clear the
|
|
|
|
// debugger event queue before we try to destroy the context. If we don't,
|
|
|
|
// the garbage collector will crash.
|
|
|
|
mWorkerPrivate->ClearDebuggerEventQueue();
|
|
|
|
|
2016-06-22 10:47:52 +03:00
|
|
|
// Perform a full GC. This will collect the main worker global and CC,
|
2015-04-09 01:20:59 +03:00
|
|
|
// which should break all cycles that touch JS.
|
2019-01-24 15:34:55 +03:00
|
|
|
JS_GC(cx, JS::GCReason::WORKER_SHUTDOWN);
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2015-04-09 01:20:59 +03:00
|
|
|
// Before shutting down the cycle collector we need to do one more pass
|
|
|
|
// through the event loop to clean up any C++ objects that need deferred
|
|
|
|
// cleanup.
|
|
|
|
mWorkerPrivate->ClearMainEventQueue(WorkerPrivate::WorkerRan);
|
|
|
|
|
2016-09-14 16:48:42 +03:00
|
|
|
// Now WorkerJSContext goes out of scope and its destructor will shut
|
2015-04-09 01:20:59 +03:00
|
|
|
// down the cycle collector. This breaks any remaining cycles and collects
|
|
|
|
// any remaining C++ objects.
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
2016-07-18 12:46:36 +03:00
|
|
|
threadHelper.Nullify();
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2014-03-05 03:09:23 +04:00
|
|
|
mWorkerPrivate->ScheduleDeletion(WorkerPrivate::WorkerRan);
|
2013-10-23 17:16:49 +04:00
|
|
|
|
|
|
|
// It is no longer safe to touch mWorkerPrivate.
|
|
|
|
mWorkerPrivate = nullptr;
|
|
|
|
|
|
|
|
// Now recycle this thread.
|
2017-06-01 23:42:05 +03:00
|
|
|
nsCOMPtr<nsIEventTarget> mainTarget = GetMainThreadEventTarget();
|
|
|
|
MOZ_ASSERT(mainTarget);
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<FinishedRunnable> finishedRunnable =
|
2013-10-23 17:16:49 +04:00
|
|
|
new FinishedRunnable(mThread.forget());
|
2017-06-01 23:42:05 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(
|
|
|
|
mainTarget->Dispatch(finishedRunnable, NS_DISPATCH_NORMAL));
|
2013-10-23 17:16:49 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WorkerThreadPrimaryRunnable::FinishedRunnable::Run() {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WorkerThread> thread;
|
2013-10-23 17:16:49 +04:00
|
|
|
mThread.swap(thread);
|
|
|
|
|
|
|
|
RuntimeService* rts = RuntimeService::GetService();
|
|
|
|
if (rts) {
|
|
|
|
rts->NoteIdleThread(thread);
|
|
|
|
} else if (thread->ShutdownRequired()) {
|
2016-03-28 20:28:15 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(thread->Shutdown());
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2018-01-31 10:24:30 +03:00
|
|
|
|
2018-01-31 10:24:59 +03:00
|
|
|
} // namespace workerinternals
|
|
|
|
|
|
|
|
void CancelWorkersForWindow(nsPIDOMWindowInner* aWindow) {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
RuntimeService* runtime = RuntimeService::GetService();
|
|
|
|
if (runtime) {
|
|
|
|
runtime->CancelWorkersForWindow(aWindow);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FreezeWorkersForWindow(nsPIDOMWindowInner* aWindow) {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
RuntimeService* runtime = RuntimeService::GetService();
|
|
|
|
if (runtime) {
|
|
|
|
runtime->FreezeWorkersForWindow(aWindow);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThawWorkersForWindow(nsPIDOMWindowInner* aWindow) {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
RuntimeService* runtime = RuntimeService::GetService();
|
|
|
|
if (runtime) {
|
|
|
|
runtime->ThawWorkersForWindow(aWindow);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SuspendWorkersForWindow(nsPIDOMWindowInner* aWindow) {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
RuntimeService* runtime = RuntimeService::GetService();
|
|
|
|
if (runtime) {
|
|
|
|
runtime->SuspendWorkersForWindow(aWindow);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResumeWorkersForWindow(nsPIDOMWindowInner* aWindow) {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
RuntimeService* runtime = RuntimeService::GetService();
|
|
|
|
if (runtime) {
|
|
|
|
runtime->ResumeWorkersForWindow(aWindow);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-02 15:02:31 +03:00
|
|
|
void PropagateFirstPartyStorageAccessGrantedToWorkers(
|
2018-07-10 11:09:59 +03:00
|
|
|
nsPIDOMWindowInner* aWindow) {
|
|
|
|
AssertIsOnMainThread();
|
2020-03-04 11:59:08 +03:00
|
|
|
MOZ_ASSERT_IF(aWindow->GetExtantDoc(), aWindow->GetExtantDoc()
|
|
|
|
->CookieJarSettings()
|
2020-04-02 18:30:57 +03:00
|
|
|
->GetRejectThirdPartyContexts());
|
2018-07-10 11:09:59 +03:00
|
|
|
|
|
|
|
RuntimeService* runtime = RuntimeService::GetService();
|
|
|
|
if (runtime) {
|
2020-06-02 15:02:31 +03:00
|
|
|
runtime->PropagateFirstPartyStorageAccessGranted(aWindow);
|
2018-07-10 11:09:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-31 10:24:59 +03:00
|
|
|
WorkerPrivate* GetWorkerPrivateFromContext(JSContext* aCx) {
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aCx);
|
|
|
|
|
2018-05-10 08:04:12 +03:00
|
|
|
CycleCollectedJSContext* ccjscx = CycleCollectedJSContext::GetFor(aCx);
|
|
|
|
if (!ccjscx) {
|
2018-01-31 10:24:59 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-05-10 08:04:12 +03:00
|
|
|
WorkerJSContext* workerjscx = ccjscx->GetAsWorkerJSContext();
|
|
|
|
// GetWorkerPrivateFromContext is called only for worker contexts. The
|
|
|
|
// context private is cleared early in ~CycleCollectedJSContext() and so
|
|
|
|
// GetFor() returns null above if called after ccjscx is no longer a
|
|
|
|
// WorkerJSContext.
|
|
|
|
MOZ_ASSERT(workerjscx);
|
|
|
|
return workerjscx->GetWorkerPrivate();
|
2018-01-31 10:24:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
WorkerPrivate* GetCurrentThreadWorkerPrivate() {
|
2019-11-18 00:09:52 +03:00
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-01-31 10:24:59 +03:00
|
|
|
|
|
|
|
CycleCollectedJSContext* ccjscx = CycleCollectedJSContext::Get();
|
|
|
|
if (!ccjscx) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-05-10 08:04:12 +03:00
|
|
|
WorkerJSContext* workerjscx = ccjscx->GetAsWorkerJSContext();
|
2019-11-18 00:09:52 +03:00
|
|
|
// Even when GetCurrentThreadWorkerPrivate() is called on worker
|
2018-05-10 08:04:12 +03:00
|
|
|
// threads, the ccjscx will no longer be a WorkerJSContext if called from
|
|
|
|
// stable state events during ~CycleCollectedJSContext().
|
|
|
|
if (!workerjscx) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-01-31 10:24:59 +03:00
|
|
|
|
2018-05-10 08:04:12 +03:00
|
|
|
return workerjscx->GetWorkerPrivate();
|
2018-01-31 10:24:59 +03:00
|
|
|
}
|
|
|
|
|
2018-03-13 23:16:54 +03:00
|
|
|
bool IsCurrentThreadRunningWorker() {
|
|
|
|
return !NS_IsMainThread() && !!GetCurrentThreadWorkerPrivate();
|
|
|
|
}
|
|
|
|
|
2018-01-31 10:24:59 +03:00
|
|
|
bool IsCurrentThreadRunningChromeWorker() {
|
2019-11-18 00:17:40 +03:00
|
|
|
WorkerPrivate* wp = GetCurrentThreadWorkerPrivate();
|
|
|
|
return wp && wp->UsesSystemPrincipal();
|
2018-01-31 10:24:59 +03:00
|
|
|
}
|
|
|
|
|
2018-02-05 21:55:07 +03:00
|
|
|
JSContext* GetCurrentWorkerThreadJSContext() {
|
2018-01-31 10:24:59 +03:00
|
|
|
WorkerPrivate* wp = GetCurrentThreadWorkerPrivate();
|
|
|
|
if (!wp) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return wp->GetJSContext();
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* GetCurrentThreadWorkerGlobal() {
|
|
|
|
WorkerPrivate* wp = GetCurrentThreadWorkerPrivate();
|
|
|
|
if (!wp) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
WorkerGlobalScope* scope = wp->GlobalScope();
|
|
|
|
if (!scope) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return scope->GetGlobalJSObject();
|
|
|
|
}
|
|
|
|
|
2019-03-25 12:26:48 +03:00
|
|
|
JSObject* GetCurrentThreadWorkerDebuggerGlobal() {
|
|
|
|
WorkerPrivate* wp = GetCurrentThreadWorkerPrivate();
|
|
|
|
if (!wp) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
WorkerDebuggerGlobalScope* scope = wp->DebuggerGlobalScope();
|
|
|
|
if (!scope) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return scope->GetGlobalJSObject();
|
|
|
|
}
|
|
|
|
|
2018-01-31 10:24:30 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|