2017-02-24 00:23:45 +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: */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
// We're dividing JS objects into 3 categories:
|
|
|
|
//
|
|
|
|
// 1. "real" roots, held by the JS engine itself or rooted through the root
|
|
|
|
// and lock JS APIs. Roots from this category are considered black in the
|
|
|
|
// cycle collector, any cycle they participate in is uncollectable.
|
|
|
|
//
|
|
|
|
// 2. certain roots held by C++ objects that are guaranteed to be alive.
|
|
|
|
// Roots from this category are considered black in the cycle collector,
|
|
|
|
// and any cycle they participate in is uncollectable. These roots are
|
|
|
|
// traced from TraceNativeBlackRoots.
|
|
|
|
//
|
|
|
|
// 3. all other roots held by C++ objects that participate in cycle
|
|
|
|
// collection, held by us (see TraceNativeGrayRoots). Roots from this
|
|
|
|
// category are considered grey in the cycle collector; whether or not
|
|
|
|
// they are collected depends on the objects that hold them.
|
|
|
|
//
|
|
|
|
// Note that if a root is in multiple categories the fact that it is in
|
|
|
|
// category 1 or 2 that takes precedence, so it will be considered black.
|
|
|
|
//
|
|
|
|
// During garbage collection we switch to an additional mark color (gray)
|
|
|
|
// when tracing inside TraceNativeGrayRoots. This allows us to walk those
|
|
|
|
// roots later on and add all objects reachable only from them to the
|
|
|
|
// cycle collector.
|
|
|
|
//
|
|
|
|
// Phases:
|
|
|
|
//
|
|
|
|
// 1. marking of the roots in category 1 by having the JS GC do its marking
|
|
|
|
// 2. marking of the roots in category 2 by having the JS GC call us back
|
|
|
|
// (via JS_SetExtraGCRootsTracer) and running TraceNativeBlackRoots
|
|
|
|
// 3. marking of the roots in category 3 by TraceNativeGrayRoots using an
|
|
|
|
// additional color (gray).
|
|
|
|
// 4. end of GC, GC can sweep its heap
|
|
|
|
//
|
|
|
|
// At some later point, when the cycle collector runs:
|
|
|
|
//
|
|
|
|
// 5. walk gray objects and add them to the cycle collector, cycle collect
|
|
|
|
//
|
|
|
|
// JS objects that are part of cycles the cycle collector breaks will be
|
|
|
|
// collected by the next JS GC.
|
|
|
|
//
|
|
|
|
// If WantAllTraces() is false the cycle collector will not traverse roots
|
|
|
|
// from category 1 or any JS objects held by them. Any JS objects they hold
|
|
|
|
// will already be marked by the JS GC and will thus be colored black
|
|
|
|
// themselves. Any C++ objects they hold will have a missing (untraversed)
|
|
|
|
// edge from the JS object to the C++ object and so it will be marked black
|
|
|
|
// too. This decreases the number of objects that the cycle collector has to
|
|
|
|
// deal with.
|
|
|
|
// To improve debugging, if WantAllTraces() is true all JS objects are
|
|
|
|
// traversed.
|
|
|
|
|
|
|
|
#include "mozilla/CycleCollectedJSRuntime.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#include "mozilla/ArrayUtils.h"
|
|
|
|
#include "mozilla/AutoRestore.h"
|
|
|
|
#include "mozilla/CycleCollectedJSContext.h"
|
|
|
|
#include "mozilla/Move.h"
|
|
|
|
#include "mozilla/MemoryReporting.h"
|
|
|
|
#include "mozilla/Sprintf.h"
|
|
|
|
#include "mozilla/Telemetry.h"
|
|
|
|
#include "mozilla/TimelineConsumers.h"
|
|
|
|
#include "mozilla/TimelineMarker.h"
|
|
|
|
#include "mozilla/Unused.h"
|
|
|
|
#include "mozilla/DebuggerOnGCRunnable.h"
|
|
|
|
#include "mozilla/dom/DOMJSClass.h"
|
|
|
|
#include "mozilla/dom/ProfileTimelineMarkerBinding.h"
|
|
|
|
#include "mozilla/dom/Promise.h"
|
|
|
|
#include "mozilla/dom/PromiseBinding.h"
|
|
|
|
#include "mozilla/dom/PromiseDebugging.h"
|
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
|
|
|
#include "js/Debug.h"
|
|
|
|
#include "js/GCAPI.h"
|
2018-12-07 00:52:15 +03:00
|
|
|
#include "jsfriendapi.h"
|
2017-02-24 00:23:45 +03:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsCycleCollectionNoteRootCallback.h"
|
|
|
|
#include "nsCycleCollectionParticipant.h"
|
|
|
|
#include "nsCycleCollector.h"
|
|
|
|
#include "nsDOMJSUtils.h"
|
2017-10-10 12:59:39 +03:00
|
|
|
#include "nsExceptionHandler.h"
|
2017-02-24 00:23:45 +03:00
|
|
|
#include "nsJSUtils.h"
|
|
|
|
#include "nsWrapperCache.h"
|
|
|
|
#include "nsStringBuffer.h"
|
2017-06-16 05:26:26 +03:00
|
|
|
#include "GeckoProfiler.h"
|
2017-10-04 01:11:18 +03:00
|
|
|
|
|
|
|
#ifdef MOZ_GECKO_PROFILER
|
2017-06-16 05:26:26 +03:00
|
|
|
# include "ProfilerMarkerPayload.h"
|
2017-10-04 01:11:18 +03:00
|
|
|
#endif
|
2017-02-24 00:23:45 +03:00
|
|
|
|
|
|
|
#include "nsIException.h"
|
|
|
|
#include "nsThread.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "xpcpublic.h"
|
|
|
|
|
2017-11-16 12:48:45 +03:00
|
|
|
#ifdef NIGHTLY_BUILD
|
|
|
|
// For performance reasons, we make the JS Dev Error Interceptor a Nightly-only
|
|
|
|
// feature.
|
|
|
|
# define MOZ_JS_DEV_ERROR_INTERCEPTOR = 1
|
|
|
|
#endif // NIGHTLY_BUILD
|
|
|
|
|
2017-02-24 00:23:45 +03:00
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
struct DeferredFinalizeFunctionHolder {
|
|
|
|
DeferredFinalizeFunction run;
|
|
|
|
void* data;
|
|
|
|
};
|
|
|
|
|
2017-07-21 18:30:55 +03:00
|
|
|
class IncrementalFinalizeRunnable : public CancelableRunnable {
|
2017-02-24 00:23:45 +03:00
|
|
|
typedef AutoTArray<DeferredFinalizeFunctionHolder, 16> DeferredFinalizeArray;
|
|
|
|
typedef CycleCollectedJSRuntime::DeferredFinalizerTable
|
|
|
|
DeferredFinalizerTable;
|
|
|
|
|
|
|
|
CycleCollectedJSRuntime* mRuntime;
|
|
|
|
DeferredFinalizeArray mDeferredFinalizeFunctions;
|
|
|
|
uint32_t mFinalizeFunctionToRun;
|
|
|
|
bool mReleasing;
|
|
|
|
|
|
|
|
static const PRTime SliceMillis = 5; /* ms */
|
|
|
|
|
|
|
|
public:
|
|
|
|
IncrementalFinalizeRunnable(CycleCollectedJSRuntime* aRt,
|
|
|
|
DeferredFinalizerTable& aFinalizerTable);
|
|
|
|
virtual ~IncrementalFinalizeRunnable();
|
|
|
|
|
|
|
|
void ReleaseNow(bool aLimited);
|
|
|
|
|
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
struct NoteWeakMapChildrenTracer : public JS::CallbackTracer {
|
2017-04-24 05:30:06 +03:00
|
|
|
NoteWeakMapChildrenTracer(JSRuntime* aRt,
|
2017-02-24 00:23:45 +03:00
|
|
|
nsCycleCollectionNoteRootCallback& aCb)
|
2017-04-24 05:30:06 +03:00
|
|
|
: JS::CallbackTracer(aRt),
|
|
|
|
mCb(aCb),
|
|
|
|
mTracedAny(false),
|
|
|
|
mMap(nullptr),
|
2017-02-24 00:23:45 +03:00
|
|
|
mKey(nullptr),
|
|
|
|
mKeyDelegate(nullptr) {
|
2018-05-11 21:38:58 +03:00
|
|
|
setCanSkipJsids(true);
|
2017-02-24 00:23:45 +03:00
|
|
|
}
|
|
|
|
void onChild(const JS::GCCellPtr& aThing) override;
|
|
|
|
nsCycleCollectionNoteRootCallback& mCb;
|
|
|
|
bool mTracedAny;
|
|
|
|
JSObject* mMap;
|
|
|
|
JS::GCCellPtr mKey;
|
|
|
|
JSObject* mKeyDelegate;
|
|
|
|
};
|
|
|
|
|
|
|
|
void NoteWeakMapChildrenTracer::onChild(const JS::GCCellPtr& aThing) {
|
|
|
|
if (aThing.is<JSString>()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!JS::GCThingIsMarkedGray(aThing) && !mCb.WantAllTraces()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-15 12:11:10 +03:00
|
|
|
if (JS::IsCCTraceKind(aThing.kind())) {
|
2017-02-24 00:23:45 +03:00
|
|
|
mCb.NoteWeakMapping(mMap, mKey, mKeyDelegate, aThing);
|
|
|
|
mTracedAny = true;
|
|
|
|
} else {
|
|
|
|
JS::TraceChildren(this, aThing);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct NoteWeakMapsTracer : public js::WeakMapTracer {
|
2017-04-24 05:30:06 +03:00
|
|
|
NoteWeakMapsTracer(JSRuntime* aRt, nsCycleCollectionNoteRootCallback& aCccb)
|
|
|
|
: js::WeakMapTracer(aRt), mCb(aCccb), mChildTracer(aRt, aCccb) {}
|
2017-02-24 00:23:45 +03:00
|
|
|
void trace(JSObject* aMap, JS::GCCellPtr aKey, JS::GCCellPtr aValue) override;
|
|
|
|
nsCycleCollectionNoteRootCallback& mCb;
|
|
|
|
NoteWeakMapChildrenTracer mChildTracer;
|
|
|
|
};
|
|
|
|
|
|
|
|
void NoteWeakMapsTracer::trace(JSObject* aMap, JS::GCCellPtr aKey,
|
|
|
|
JS::GCCellPtr aValue) {
|
|
|
|
// If nothing that could be held alive by this entry is marked gray, return.
|
|
|
|
if ((!aKey || !JS::GCThingIsMarkedGray(aKey)) &&
|
|
|
|
MOZ_LIKELY(!mCb.WantAllTraces())) {
|
|
|
|
if (!aValue || !JS::GCThingIsMarkedGray(aValue) || aValue.is<JSString>()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The cycle collector can only properly reason about weak maps if it can
|
|
|
|
// reason about the liveness of their keys, which in turn requires that
|
|
|
|
// the key can be represented in the cycle collector graph. All existing
|
|
|
|
// uses of weak maps use either objects or scripts as keys, which are okay.
|
2018-11-15 12:11:10 +03:00
|
|
|
MOZ_ASSERT(JS::IsCCTraceKind(aKey.kind()));
|
2017-02-24 00:23:45 +03:00
|
|
|
|
|
|
|
// As an emergency fallback for non-debug builds, if the key is not
|
|
|
|
// representable in the cycle collector graph, we treat it as marked. This
|
|
|
|
// can cause leaks, but is preferable to ignoring the binding, which could
|
|
|
|
// cause the cycle collector to free live objects.
|
2018-11-15 12:11:10 +03:00
|
|
|
if (!JS::IsCCTraceKind(aKey.kind())) {
|
2017-02-24 00:23:45 +03:00
|
|
|
aKey = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* kdelegate = nullptr;
|
|
|
|
if (aKey.is<JSObject>()) {
|
2018-12-08 01:38:01 +03:00
|
|
|
kdelegate = js::UncheckedUnwrapWithoutExpose(&aKey.as<JSObject>());
|
2017-02-24 00:23:45 +03:00
|
|
|
}
|
|
|
|
|
2018-11-15 12:11:10 +03:00
|
|
|
if (JS::IsCCTraceKind(aValue.kind())) {
|
2017-02-24 00:23:45 +03:00
|
|
|
mCb.NoteWeakMapping(aMap, aKey, kdelegate, aValue);
|
|
|
|
} else {
|
|
|
|
mChildTracer.mTracedAny = false;
|
|
|
|
mChildTracer.mMap = aMap;
|
|
|
|
mChildTracer.mKey = aKey;
|
|
|
|
mChildTracer.mKeyDelegate = kdelegate;
|
|
|
|
|
|
|
|
if (!aValue.is<JSString>()) {
|
|
|
|
JS::TraceChildren(&mChildTracer, aValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The delegate could hold alive the key, so report something to the CC
|
|
|
|
// if we haven't already.
|
|
|
|
if (!mChildTracer.mTracedAny && aKey && JS::GCThingIsMarkedGray(aKey) &&
|
|
|
|
kdelegate) {
|
|
|
|
mCb.NoteWeakMapping(aMap, aKey, kdelegate, nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Report whether the key or value of a weak mapping entry are gray but need to
|
|
|
|
// be marked black.
|
|
|
|
static void ShouldWeakMappingEntryBeBlack(JSObject* aMap, JS::GCCellPtr aKey,
|
|
|
|
JS::GCCellPtr aValue,
|
|
|
|
bool* aKeyShouldBeBlack,
|
|
|
|
bool* aValueShouldBeBlack) {
|
|
|
|
*aKeyShouldBeBlack = false;
|
|
|
|
*aValueShouldBeBlack = false;
|
|
|
|
|
|
|
|
// If nothing that could be held alive by this entry is marked gray, return.
|
|
|
|
bool keyMightNeedMarking = aKey && JS::GCThingIsMarkedGray(aKey);
|
|
|
|
bool valueMightNeedMarking = aValue && JS::GCThingIsMarkedGray(aValue) &&
|
|
|
|
aValue.kind() != JS::TraceKind::String;
|
|
|
|
if (!keyMightNeedMarking && !valueMightNeedMarking) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-15 12:11:10 +03:00
|
|
|
if (!JS::IsCCTraceKind(aKey.kind())) {
|
2017-02-24 00:23:45 +03:00
|
|
|
aKey = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (keyMightNeedMarking && aKey.is<JSObject>()) {
|
2018-12-08 01:38:01 +03:00
|
|
|
JSObject* kdelegate =
|
|
|
|
js::UncheckedUnwrapWithoutExpose(&aKey.as<JSObject>());
|
2017-02-24 00:23:45 +03:00
|
|
|
if (kdelegate && !JS::ObjectIsMarkedGray(kdelegate) &&
|
|
|
|
(!aMap || !JS::ObjectIsMarkedGray(aMap))) {
|
|
|
|
*aKeyShouldBeBlack = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aValue && JS::GCThingIsMarkedGray(aValue) &&
|
|
|
|
(!aKey || !JS::GCThingIsMarkedGray(aKey)) &&
|
|
|
|
(!aMap || !JS::ObjectIsMarkedGray(aMap)) &&
|
|
|
|
aValue.kind() != JS::TraceKind::Shape) {
|
|
|
|
*aValueShouldBeBlack = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct FixWeakMappingGrayBitsTracer : public js::WeakMapTracer {
|
2017-04-24 05:30:06 +03:00
|
|
|
explicit FixWeakMappingGrayBitsTracer(JSRuntime* aRt)
|
|
|
|
: js::WeakMapTracer(aRt) {}
|
2017-02-24 00:23:45 +03:00
|
|
|
|
|
|
|
void FixAll() {
|
|
|
|
do {
|
|
|
|
mAnyMarked = false;
|
|
|
|
js::TraceWeakMaps(this);
|
|
|
|
} while (mAnyMarked);
|
|
|
|
}
|
|
|
|
|
|
|
|
void trace(JSObject* aMap, JS::GCCellPtr aKey,
|
|
|
|
JS::GCCellPtr aValue) override {
|
|
|
|
bool keyShouldBeBlack;
|
|
|
|
bool valueShouldBeBlack;
|
|
|
|
ShouldWeakMappingEntryBeBlack(aMap, aKey, aValue, &keyShouldBeBlack,
|
|
|
|
&valueShouldBeBlack);
|
|
|
|
if (keyShouldBeBlack && JS::UnmarkGrayGCThingRecursively(aKey)) {
|
|
|
|
mAnyMarked = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (valueShouldBeBlack && JS::UnmarkGrayGCThingRecursively(aValue)) {
|
|
|
|
mAnyMarked = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_INIT_OUTSIDE_CTOR bool mAnyMarked;
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
// Check whether weak maps are marked correctly according to the logic above.
|
|
|
|
struct CheckWeakMappingGrayBitsTracer : public js::WeakMapTracer {
|
2017-04-24 05:30:06 +03:00
|
|
|
explicit CheckWeakMappingGrayBitsTracer(JSRuntime* aRt)
|
|
|
|
: js::WeakMapTracer(aRt), mFailed(false) {}
|
2017-02-24 00:23:45 +03:00
|
|
|
|
2017-04-24 05:30:06 +03:00
|
|
|
static bool Check(JSRuntime* aRt) {
|
|
|
|
CheckWeakMappingGrayBitsTracer tracer(aRt);
|
2017-02-24 00:23:45 +03:00
|
|
|
js::TraceWeakMaps(&tracer);
|
|
|
|
return !tracer.mFailed;
|
|
|
|
}
|
|
|
|
|
|
|
|
void trace(JSObject* aMap, JS::GCCellPtr aKey,
|
|
|
|
JS::GCCellPtr aValue) override {
|
|
|
|
bool keyShouldBeBlack;
|
|
|
|
bool valueShouldBeBlack;
|
|
|
|
ShouldWeakMappingEntryBeBlack(aMap, aKey, aValue, &keyShouldBeBlack,
|
|
|
|
&valueShouldBeBlack);
|
|
|
|
|
|
|
|
if (keyShouldBeBlack) {
|
|
|
|
fprintf(stderr, "Weak mapping key %p of map %p should be black\n",
|
|
|
|
aKey.asCell(), aMap);
|
|
|
|
mFailed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (valueShouldBeBlack) {
|
|
|
|
fprintf(stderr, "Weak mapping value %p of map %p should be black\n",
|
|
|
|
aValue.asCell(), aMap);
|
|
|
|
mFailed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool mFailed;
|
|
|
|
};
|
|
|
|
#endif // DEBUG
|
|
|
|
|
|
|
|
static void CheckParticipatesInCycleCollection(JS::GCCellPtr aThing,
|
|
|
|
const char* aName,
|
|
|
|
void* aClosure) {
|
|
|
|
bool* cycleCollectionEnabled = static_cast<bool*>(aClosure);
|
|
|
|
|
|
|
|
if (*cycleCollectionEnabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-15 12:11:10 +03:00
|
|
|
if (JS::IsCCTraceKind(aThing.kind()) && JS::GCThingIsMarkedGray(aThing)) {
|
2017-02-24 00:23:45 +03:00
|
|
|
*cycleCollectionEnabled = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
JSGCThingParticipant::TraverseNative(void* aPtr,
|
|
|
|
nsCycleCollectionTraversalCallback& aCb) {
|
|
|
|
auto runtime = reinterpret_cast<CycleCollectedJSRuntime*>(
|
|
|
|
reinterpret_cast<char*>(this) -
|
|
|
|
offsetof(CycleCollectedJSRuntime, mGCThingCycleCollectorGlobal));
|
|
|
|
|
|
|
|
JS::GCCellPtr cellPtr(aPtr, JS::GCThingTraceKind(aPtr));
|
|
|
|
runtime->TraverseGCThing(CycleCollectedJSRuntime::TRAVERSE_FULL, cellPtr,
|
|
|
|
aCb);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// NB: This is only used to initialize the participant in
|
|
|
|
// CycleCollectedJSRuntime. It should never be used directly.
|
|
|
|
static JSGCThingParticipant sGCThingCycleCollectorGlobal;
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
JSZoneParticipant::TraverseNative(void* aPtr,
|
|
|
|
nsCycleCollectionTraversalCallback& aCb) {
|
|
|
|
auto runtime = reinterpret_cast<CycleCollectedJSRuntime*>(
|
|
|
|
reinterpret_cast<char*>(this) -
|
|
|
|
offsetof(CycleCollectedJSRuntime, mJSZoneCycleCollectorGlobal));
|
|
|
|
|
|
|
|
MOZ_ASSERT(!aCb.WantAllTraces());
|
|
|
|
JS::Zone* zone = static_cast<JS::Zone*>(aPtr);
|
|
|
|
|
|
|
|
runtime->TraverseZone(zone, aCb);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct TraversalTracer : public JS::CallbackTracer {
|
2017-04-24 05:30:06 +03:00
|
|
|
TraversalTracer(JSRuntime* aRt, nsCycleCollectionTraversalCallback& aCb)
|
|
|
|
: JS::CallbackTracer(aRt, DoNotTraceWeakMaps), mCb(aCb) {
|
2018-05-11 21:38:58 +03:00
|
|
|
setCanSkipJsids(true);
|
2017-02-24 00:23:45 +03:00
|
|
|
}
|
|
|
|
void onChild(const JS::GCCellPtr& aThing) override;
|
|
|
|
nsCycleCollectionTraversalCallback& mCb;
|
|
|
|
};
|
|
|
|
|
|
|
|
void TraversalTracer::onChild(const JS::GCCellPtr& aThing) {
|
2018-03-20 20:54:29 +03:00
|
|
|
// Checking strings and symbols for being gray is rather slow, and we don't
|
|
|
|
// need either of them for the cycle collector.
|
|
|
|
if (aThing.is<JSString>() || aThing.is<JS::Symbol>()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-24 00:23:45 +03:00
|
|
|
// Don't traverse non-gray objects, unless we want all traces.
|
|
|
|
if (!JS::GCThingIsMarkedGray(aThing) && !mCb.WantAllTraces()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function needs to be careful to avoid stack overflow. Normally, when
|
2018-11-15 12:11:10 +03:00
|
|
|
* IsCCTraceKind is true, the recursion terminates immediately as we just add
|
2017-02-24 00:23:45 +03:00
|
|
|
* |thing| to the CC graph. So overflow is only possible when there are long
|
2018-11-15 12:11:10 +03:00
|
|
|
* or cyclic chains of non-IsCCTraceKind GC things. Places where this can
|
2017-02-24 00:23:45 +03:00
|
|
|
* occur use special APIs to handle such chains iteratively.
|
|
|
|
*/
|
2018-11-15 12:11:10 +03:00
|
|
|
if (JS::IsCCTraceKind(aThing.kind())) {
|
2017-02-24 00:23:45 +03:00
|
|
|
if (MOZ_UNLIKELY(mCb.WantDebugInfo())) {
|
|
|
|
char buffer[200];
|
|
|
|
getTracingEdgeName(buffer, sizeof(buffer));
|
|
|
|
mCb.NoteNextEdgeName(buffer);
|
|
|
|
}
|
|
|
|
mCb.NoteJSChild(aThing);
|
|
|
|
} else if (aThing.is<js::Shape>()) {
|
|
|
|
// The maximum depth of traversal when tracing a Shape is unbounded, due to
|
|
|
|
// the parent pointers on the shape.
|
|
|
|
JS_TraceShapeCycleCollectorChildren(this, aThing);
|
|
|
|
} else if (aThing.is<js::ObjectGroup>()) {
|
|
|
|
// The maximum depth of traversal when tracing an ObjectGroup is unbounded,
|
|
|
|
// due to information attached to the groups which can lead other groups to
|
|
|
|
// be traced.
|
|
|
|
JS_TraceObjectGroupCycleCollectorChildren(this, aThing);
|
2018-03-20 20:54:29 +03:00
|
|
|
} else {
|
2017-02-24 00:23:45 +03:00
|
|
|
JS::TraceChildren(this, aThing);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void NoteJSChildGrayWrapperShim(void* aData, JS::GCCellPtr aThing) {
|
|
|
|
TraversalTracer* trc = static_cast<TraversalTracer*>(aData);
|
|
|
|
trc->onChild(aThing);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The cycle collection participant for a Zone is intended to produce the same
|
|
|
|
* results as if all of the gray GCthings in a zone were merged into a single
|
|
|
|
* node, except for self-edges. This avoids the overhead of representing all of
|
|
|
|
* the GCthings in the zone in the cycle collector graph, which should be much
|
|
|
|
* faster if many of the GCthings in the zone are gray.
|
|
|
|
*
|
|
|
|
* Zone merging should not always be used, because it is a conservative
|
|
|
|
* approximation of the true cycle collector graph that can incorrectly identify
|
|
|
|
* some garbage objects as being live. For instance, consider two cycles that
|
|
|
|
* pass through a zone, where one is garbage and the other is live. If we merge
|
|
|
|
* the entire zone, the cycle collector will think that both are alive.
|
|
|
|
*
|
|
|
|
* We don't have to worry about losing track of a garbage cycle, because any
|
|
|
|
* such garbage cycle incorrectly identified as live must contain at least one
|
|
|
|
* C++ to JS edge, and XPConnect will always add the C++ object to the CC graph.
|
|
|
|
* (This is in contrast to pure C++ garbage cycles, which must always be
|
|
|
|
* properly identified, because we clear the purple buffer during every CC,
|
|
|
|
* which may contain the last reference to a garbage cycle.)
|
|
|
|
*/
|
|
|
|
|
|
|
|
// NB: This is only used to initialize the participant in
|
|
|
|
// CycleCollectedJSRuntime. It should never be used directly.
|
|
|
|
static const JSZoneParticipant sJSZoneCycleCollectorGlobal;
|
|
|
|
|
|
|
|
static void JSObjectsTenuredCb(JSContext* aContext, void* aData) {
|
|
|
|
static_cast<CycleCollectedJSRuntime*>(aData)->JSObjectsTenured();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void MozCrashWarningReporter(JSContext*, JSErrorReport*) {
|
|
|
|
MOZ_CRASH("Why is someone touching JSAPI without an AutoJSAPI?");
|
|
|
|
}
|
|
|
|
|
2017-04-24 05:30:06 +03:00
|
|
|
CycleCollectedJSRuntime::CycleCollectedJSRuntime(JSContext* aCx)
|
2017-02-24 00:23:45 +03:00
|
|
|
: mGCThingCycleCollectorGlobal(sGCThingCycleCollectorGlobal),
|
|
|
|
mJSZoneCycleCollectorGlobal(sJSZoneCycleCollectorGlobal),
|
2017-04-24 05:30:06 +03:00
|
|
|
mJSRuntime(JS_GetRuntime(aCx)),
|
2017-10-30 13:07:42 +03:00
|
|
|
mHasPendingIdleGCTask(false),
|
2017-02-24 00:23:45 +03:00
|
|
|
mPrevGCSliceCallback(nullptr),
|
|
|
|
mPrevGCNurseryCollectionCallback(nullptr),
|
2017-07-11 18:52:01 +03:00
|
|
|
mJSHolderMap(256),
|
2017-02-24 00:23:45 +03:00
|
|
|
mOutOfMemoryState(OOMState::OK),
|
|
|
|
mLargeAllocationFailureState(OOMState::OK)
|
2017-12-18 19:59:30 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
,
|
|
|
|
mShutdownCalled(false)
|
|
|
|
#endif
|
2017-02-24 00:23:45 +03:00
|
|
|
{
|
2017-05-09 14:59:00 +03:00
|
|
|
MOZ_COUNT_CTOR(CycleCollectedJSRuntime);
|
2017-04-24 05:30:06 +03:00
|
|
|
MOZ_ASSERT(aCx);
|
|
|
|
MOZ_ASSERT(mJSRuntime);
|
|
|
|
|
|
|
|
if (!JS_AddExtraGCRootsTracer(aCx, TraceBlackJS, this)) {
|
2017-02-24 00:23:45 +03:00
|
|
|
MOZ_CRASH("JS_AddExtraGCRootsTracer failed");
|
|
|
|
}
|
2017-04-24 05:30:06 +03:00
|
|
|
JS_SetGrayGCRootsTracer(aCx, TraceGrayJS, this);
|
|
|
|
JS_SetGCCallback(aCx, GCCallback, this);
|
|
|
|
mPrevGCSliceCallback = JS::SetGCSliceCallback(aCx, GCSliceCallback);
|
2017-02-24 00:23:45 +03:00
|
|
|
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
// We would like to support all threads here, but the way timeline consumers
|
|
|
|
// are set up currently, you can either add a marker for one specific
|
|
|
|
// docshell, or for every consumer globally. We would like to add a marker
|
|
|
|
// for every consumer observing anything on this thread, but that is not
|
|
|
|
// currently possible. For now, add global markers only when we are on the
|
|
|
|
// main thread, since the UI for this tracing data only displays data
|
|
|
|
// relevant to the main-thread.
|
|
|
|
mPrevGCNurseryCollectionCallback =
|
|
|
|
JS::SetGCNurseryCollectionCallback(aCx, GCNurseryCollectionCallback);
|
|
|
|
}
|
|
|
|
|
2017-04-24 05:30:06 +03:00
|
|
|
JS_SetObjectsTenuredCallback(aCx, JSObjectsTenuredCb, this);
|
|
|
|
JS::SetOutOfMemoryCallback(aCx, OutOfMemoryCallback, this);
|
|
|
|
JS_SetExternalStringSizeofCallback(aCx, SizeofExternalStringCallback);
|
|
|
|
JS::SetWarningReporter(aCx, MozCrashWarningReporter);
|
2017-10-10 12:59:39 +03:00
|
|
|
|
|
|
|
js::AutoEnterOOMUnsafeRegion::setAnnotateOOMAllocationSizeCallback(
|
|
|
|
CrashReporter::AnnotateOOMAllocationSize);
|
2017-02-24 00:23:45 +03:00
|
|
|
|
|
|
|
static js::DOMCallbacks DOMcallbacks = {InstanceClassHasProtoAtDepth};
|
2017-04-24 05:30:06 +03:00
|
|
|
SetDOMCallbacks(aCx, &DOMcallbacks);
|
|
|
|
js::SetScriptEnvironmentPreparer(aCx, &mEnvironmentPreparer);
|
2017-02-24 00:23:45 +03:00
|
|
|
|
2017-04-24 05:30:06 +03:00
|
|
|
JS::dbg::SetDebuggerMallocSizeOf(aCx, moz_malloc_size_of);
|
2017-11-16 12:48:45 +03:00
|
|
|
|
|
|
|
#ifdef MOZ_JS_DEV_ERROR_INTERCEPTOR
|
|
|
|
JS_SetErrorInterceptorCallback(mJSRuntime, &mErrorInterceptor);
|
|
|
|
#endif // MOZ_JS_DEV_ERROR_INTERCEPTOR
|
2017-02-24 00:23:45 +03:00
|
|
|
}
|
|
|
|
|
2017-04-23 21:23:33 +03:00
|
|
|
void CycleCollectedJSRuntime::Shutdown(JSContext* cx) {
|
2017-11-16 12:48:45 +03:00
|
|
|
#ifdef MOZ_JS_DEV_ERROR_INTERCEPTOR
|
|
|
|
mErrorInterceptor.Shutdown(mJSRuntime);
|
|
|
|
#endif // MOZ_JS_DEV_ERROR_INTERCEPTOR
|
2017-04-25 23:34:01 +03:00
|
|
|
JS_RemoveExtraGCRootsTracer(cx, TraceBlackJS, this);
|
|
|
|
JS_RemoveExtraGCRootsTracer(cx, TraceGrayJS, this);
|
2017-12-18 19:59:30 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
mShutdownCalled = true;
|
|
|
|
#endif
|
2017-02-24 00:23:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CycleCollectedJSRuntime::~CycleCollectedJSRuntime() {
|
2017-05-09 14:59:00 +03:00
|
|
|
MOZ_COUNT_DTOR(CycleCollectedJSRuntime);
|
2017-02-24 00:23:45 +03:00
|
|
|
MOZ_ASSERT(!mDeferredFinalizerTable.Count());
|
2017-12-18 19:59:30 +03:00
|
|
|
MOZ_ASSERT(mShutdownCalled);
|
2017-02-24 00:23:45 +03:00
|
|
|
}
|
|
|
|
|
2017-04-25 00:15:46 +03:00
|
|
|
void CycleCollectedJSRuntime::AddContext(CycleCollectedJSContext* aContext) {
|
|
|
|
mContexts.insertBack(aContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CycleCollectedJSRuntime::RemoveContext(CycleCollectedJSContext* aContext) {
|
|
|
|
aContext->removeFrom(mContexts);
|
|
|
|
}
|
|
|
|
|
2017-02-24 00:23:45 +03:00
|
|
|
size_t CycleCollectedJSRuntime::SizeOfExcludingThis(
|
|
|
|
MallocSizeOf aMallocSizeOf) const {
|
|
|
|
size_t n = 0;
|
|
|
|
|
|
|
|
// We're deliberately not measuring anything hanging off the entries in
|
|
|
|
// mJSHolders.
|
2017-07-11 18:52:01 +03:00
|
|
|
n += mJSHolders.SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
n += mJSHolderMap.ShallowSizeOfExcludingThis(aMallocSizeOf);
|
2017-02-24 00:23:45 +03:00
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CycleCollectedJSRuntime::UnmarkSkippableJSHolders() {
|
2018-07-30 18:48:17 +03:00
|
|
|
// Prevent nsWrapperCaches accessed under CanSkip from adding recorded events
|
|
|
|
// which might not replay in the same order.
|
|
|
|
recordreplay::AutoDisallowThreadEvents disallow;
|
|
|
|
|
2017-02-24 00:23:45 +03:00
|
|
|
for (auto iter = mJSHolders.Iter(); !iter.Done(); iter.Next()) {
|
2017-07-11 18:52:01 +03:00
|
|
|
void* holder = iter.Get().mHolder;
|
|
|
|
nsScriptObjectTracer* tracer = iter.Get().mTracer;
|
2017-02-24 00:23:45 +03:00
|
|
|
tracer->CanSkip(holder, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CycleCollectedJSRuntime::DescribeGCThing(
|
|
|
|
bool aIsMarked, JS::GCCellPtr aThing,
|
|
|
|
nsCycleCollectionTraversalCallback& aCb) const {
|
|
|
|
if (!aCb.WantDebugInfo()) {
|
|
|
|
aCb.DescribeGCedNode(aIsMarked, "JS Object");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
char name[72];
|
|
|
|
uint64_t compartmentAddress = 0;
|
|
|
|
if (aThing.is<JSObject>()) {
|
|
|
|
JSObject* obj = &aThing.as<JSObject>();
|
|
|
|
compartmentAddress = (uint64_t)js::GetObjectCompartment(obj);
|
|
|
|
const js::Class* clasp = js::GetObjectClass(obj);
|
|
|
|
|
|
|
|
// Give the subclass a chance to do something
|
|
|
|
if (DescribeCustomObjects(obj, clasp, name)) {
|
|
|
|
// Nothing else to do!
|
|
|
|
} else if (js::IsFunctionObject(obj)) {
|
|
|
|
JSFunction* fun = JS_GetObjectFunction(obj);
|
|
|
|
JSString* str = JS_GetFunctionDisplayId(fun);
|
|
|
|
if (str) {
|
|
|
|
JSFlatString* flat = JS_ASSERT_STRING_IS_FLAT(str);
|
|
|
|
nsAutoString chars;
|
|
|
|
AssignJSFlatString(chars, flat);
|
|
|
|
NS_ConvertUTF16toUTF8 fname(chars);
|
|
|
|
SprintfLiteral(name, "JS Object (Function - %s)", fname.get());
|
|
|
|
} else {
|
|
|
|
SprintfLiteral(name, "JS Object (Function)");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
SprintfLiteral(name, "JS Object (%s)", clasp->name);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
SprintfLiteral(name, "JS %s", JS::GCTraceKindToAscii(aThing.kind()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disable printing global for objects while we figure out ObjShrink fallout.
|
|
|
|
aCb.DescribeGCedNode(aIsMarked, name, compartmentAddress);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CycleCollectedJSRuntime::NoteGCThingJSChildren(
|
|
|
|
JS::GCCellPtr aThing, nsCycleCollectionTraversalCallback& aCb) const {
|
2017-04-24 05:30:06 +03:00
|
|
|
TraversalTracer trc(mJSRuntime, aCb);
|
2017-02-24 00:23:45 +03:00
|
|
|
JS::TraceChildren(&trc, aThing);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CycleCollectedJSRuntime::NoteGCThingXPCOMChildren(
|
|
|
|
const js::Class* aClasp, JSObject* aObj,
|
|
|
|
nsCycleCollectionTraversalCallback& aCb) const {
|
|
|
|
MOZ_ASSERT(aClasp);
|
|
|
|
MOZ_ASSERT(aClasp == js::GetObjectClass(aObj));
|
|
|
|
|
|
|
|
if (NoteCustomGCThingXPCOMChildren(aClasp, aObj, aCb)) {
|
|
|
|
// Nothing else to do!
|
|
|
|
return;
|
|
|
|
}
|
2018-12-07 00:52:15 +03:00
|
|
|
|
2017-02-24 00:23:45 +03:00
|
|
|
// XXX This test does seem fragile, we should probably whitelist classes
|
|
|
|
// that do hold a strong reference, but that might not be possible.
|
2018-12-07 00:52:15 +03:00
|
|
|
if (aClasp->flags & JSCLASS_HAS_PRIVATE &&
|
|
|
|
aClasp->flags & JSCLASS_PRIVATE_IS_NSISUPPORTS) {
|
2017-02-24 00:23:45 +03:00
|
|
|
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(aCb, "js::GetObjectPrivate(obj)");
|
|
|
|
aCb.NoteXPCOMChild(static_cast<nsISupports*>(js::GetObjectPrivate(aObj)));
|
2018-12-07 00:52:15 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-28 22:34:02 +03:00
|
|
|
const DOMJSClass* domClass = GetDOMClass(aClasp);
|
2018-12-07 00:52:15 +03:00
|
|
|
if (domClass) {
|
|
|
|
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(aCb, "UnwrapDOMObject(obj)");
|
|
|
|
// It's possible that our object is an unforgeable holder object, in
|
|
|
|
// which case it doesn't actually have a C++ DOM object associated with
|
|
|
|
// it. Use UnwrapPossiblyNotInitializedDOMObject, which produces null in
|
|
|
|
// that case, since NoteXPCOMChild/NoteNativeChild are null-safe.
|
|
|
|
if (domClass->mDOMObjectIsISupports) {
|
|
|
|
aCb.NoteXPCOMChild(
|
|
|
|
UnwrapPossiblyNotInitializedDOMObject<nsISupports>(aObj));
|
|
|
|
} else if (domClass->mParticipant) {
|
|
|
|
aCb.NoteNativeChild(UnwrapPossiblyNotInitializedDOMObject<void>(aObj),
|
|
|
|
domClass->mParticipant);
|
2017-02-24 00:23:45 +03:00
|
|
|
}
|
2018-12-07 00:52:15 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-28 22:34:02 +03:00
|
|
|
if (IsRemoteObjectProxy(aObj)) {
|
|
|
|
auto handler =
|
|
|
|
static_cast<const RemoteObjectProxyBase*>(js::GetProxyHandler(aObj));
|
|
|
|
return handler->NoteChildren(aObj, aCb);
|
|
|
|
}
|
|
|
|
|
2018-12-07 00:52:15 +03:00
|
|
|
JS::Value value = js::MaybeGetScriptPrivate(aObj);
|
|
|
|
if (!value.isUndefined()) {
|
|
|
|
aCb.NoteXPCOMChild(static_cast<nsISupports*>(value.toPrivate()));
|
2017-02-24 00:23:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CycleCollectedJSRuntime::TraverseGCThing(
|
|
|
|
TraverseSelect aTs, JS::GCCellPtr aThing,
|
|
|
|
nsCycleCollectionTraversalCallback& aCb) {
|
|
|
|
bool isMarkedGray = JS::GCThingIsMarkedGray(aThing);
|
|
|
|
|
|
|
|
if (aTs == TRAVERSE_FULL) {
|
|
|
|
DescribeGCThing(!isMarkedGray, aThing, aCb);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this object is alive, then all of its children are alive. For JS
|
|
|
|
// objects, the black-gray invariant ensures the children are also marked
|
|
|
|
// black. For C++ objects, the ref count from this object will keep them
|
|
|
|
// alive. Thus we don't need to trace our children, unless we are debugging
|
|
|
|
// using WantAllTraces.
|
|
|
|
if (!isMarkedGray && !aCb.WantAllTraces()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aTs == TRAVERSE_FULL) {
|
|
|
|
NoteGCThingJSChildren(aThing, aCb);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aThing.is<JSObject>()) {
|
|
|
|
JSObject* obj = &aThing.as<JSObject>();
|
|
|
|
NoteGCThingXPCOMChildren(js::GetObjectClass(obj), obj, aCb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct TraverseObjectShimClosure {
|
|
|
|
nsCycleCollectionTraversalCallback& cb;
|
|
|
|
CycleCollectedJSRuntime* self;
|
|
|
|
};
|
|
|
|
|
|
|
|
void CycleCollectedJSRuntime::TraverseZone(
|
|
|
|
JS::Zone* aZone, nsCycleCollectionTraversalCallback& aCb) {
|
|
|
|
/*
|
|
|
|
* We treat the zone as being gray. We handle non-gray GCthings in the
|
|
|
|
* zone by not reporting their children to the CC. The black-gray invariant
|
|
|
|
* ensures that any JS children will also be non-gray, and thus don't need to
|
|
|
|
* be added to the graph. For C++ children, not representing the edge from the
|
|
|
|
* non-gray JS GCthings to the C++ object will keep the child alive.
|
|
|
|
*
|
|
|
|
* We don't allow zone merging in a WantAllTraces CC, because then these
|
|
|
|
* assumptions don't hold.
|
|
|
|
*/
|
|
|
|
aCb.DescribeGCedNode(false, "JS Zone");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Every JS child of everything in the zone is either in the zone
|
|
|
|
* or is a cross-compartment wrapper. In the former case, we don't need to
|
|
|
|
* represent these edges in the CC graph because JS objects are not ref
|
|
|
|
* counted. In the latter case, the JS engine keeps a map of these wrappers,
|
|
|
|
* which we iterate over. Edges between compartments in the same zone will add
|
|
|
|
* unnecessary loop edges to the graph (bug 842137).
|
|
|
|
*/
|
2017-04-24 05:30:06 +03:00
|
|
|
TraversalTracer trc(mJSRuntime, aCb);
|
2017-02-24 00:23:45 +03:00
|
|
|
js::VisitGrayWrapperTargets(aZone, NoteJSChildGrayWrapperShim, &trc);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* To find C++ children of things in the zone, we scan every JS Object in
|
|
|
|
* the zone. Only JS Objects can have C++ children.
|
|
|
|
*/
|
|
|
|
TraverseObjectShimClosure closure = {aCb, this};
|
|
|
|
js::IterateGrayObjects(aZone, TraverseObjectShim, &closure);
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:14:01 +03:00
|
|
|
/* static */
|
|
|
|
void CycleCollectedJSRuntime::TraverseObjectShim(void* aData,
|
|
|
|
JS::GCCellPtr aThing) {
|
2017-02-24 00:23:45 +03:00
|
|
|
TraverseObjectShimClosure* closure =
|
|
|
|
static_cast<TraverseObjectShimClosure*>(aData);
|
|
|
|
|
|
|
|
MOZ_ASSERT(aThing.is<JSObject>());
|
|
|
|
closure->self->TraverseGCThing(CycleCollectedJSRuntime::TRAVERSE_CPP, aThing,
|
|
|
|
closure->cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CycleCollectedJSRuntime::TraverseNativeRoots(
|
|
|
|
nsCycleCollectionNoteRootCallback& aCb) {
|
|
|
|
// NB: This is here just to preserve the existing XPConnect order. I doubt it
|
|
|
|
// would hurt to do this after the JS holders.
|
|
|
|
TraverseAdditionalNativeRoots(aCb);
|
|
|
|
|
|
|
|
for (auto iter = mJSHolders.Iter(); !iter.Done(); iter.Next()) {
|
2017-07-11 18:52:01 +03:00
|
|
|
void* holder = iter.Get().mHolder;
|
|
|
|
nsScriptObjectTracer* tracer = iter.Get().mTracer;
|
2017-02-24 00:23:45 +03:00
|
|
|
|
|
|
|
bool noteRoot = false;
|
|
|
|
if (MOZ_UNLIKELY(aCb.WantAllTraces())) {
|
|
|
|
noteRoot = true;
|
|
|
|
} else {
|
|
|
|
tracer->Trace(holder,
|
|
|
|
TraceCallbackFunc(CheckParticipatesInCycleCollection),
|
|
|
|
¬eRoot);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (noteRoot) {
|
|
|
|
aCb.NoteNativeRoot(holder, tracer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:14:01 +03:00
|
|
|
/* static */
|
|
|
|
void CycleCollectedJSRuntime::TraceBlackJS(JSTracer* aTracer, void* aData) {
|
2017-02-24 00:23:45 +03:00
|
|
|
CycleCollectedJSRuntime* self = static_cast<CycleCollectedJSRuntime*>(aData);
|
|
|
|
|
|
|
|
self->TraceNativeBlackRoots(aTracer);
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:14:01 +03:00
|
|
|
/* static */
|
|
|
|
void CycleCollectedJSRuntime::TraceGrayJS(JSTracer* aTracer, void* aData) {
|
2017-02-24 00:23:45 +03:00
|
|
|
CycleCollectedJSRuntime* self = static_cast<CycleCollectedJSRuntime*>(aData);
|
|
|
|
|
|
|
|
// Mark these roots as gray so the CC can walk them later.
|
|
|
|
self->TraceNativeGrayRoots(aTracer);
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:14:01 +03:00
|
|
|
/* static */
|
|
|
|
void CycleCollectedJSRuntime::GCCallback(JSContext* aContext,
|
|
|
|
JSGCStatus aStatus, void* aData) {
|
2017-02-24 00:23:45 +03:00
|
|
|
CycleCollectedJSRuntime* self = static_cast<CycleCollectedJSRuntime*>(aData);
|
|
|
|
|
|
|
|
MOZ_ASSERT(CycleCollectedJSContext::Get()->Context() == aContext);
|
|
|
|
MOZ_ASSERT(CycleCollectedJSContext::Get()->Runtime() == self);
|
|
|
|
|
2017-05-12 21:16:16 +03:00
|
|
|
self->OnGC(aContext, aStatus);
|
2017-02-24 00:23:45 +03:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:14:01 +03:00
|
|
|
/* static */
|
|
|
|
void CycleCollectedJSRuntime::GCSliceCallback(JSContext* aContext,
|
|
|
|
JS::GCProgress aProgress,
|
|
|
|
const JS::GCDescription& aDesc) {
|
2017-02-24 00:23:45 +03:00
|
|
|
CycleCollectedJSRuntime* self = CycleCollectedJSRuntime::Get();
|
|
|
|
MOZ_ASSERT(CycleCollectedJSContext::Get()->Context() == aContext);
|
|
|
|
|
2017-10-04 01:11:18 +03:00
|
|
|
#ifdef MOZ_GECKO_PROFILER
|
2018-11-19 09:29:15 +03:00
|
|
|
if (profiler_thread_is_being_profiled()) {
|
2017-05-03 02:13:49 +03:00
|
|
|
if (aProgress == JS::GC_CYCLE_END) {
|
2017-06-22 06:40:21 +03:00
|
|
|
profiler_add_marker(
|
2019-02-16 20:37:43 +03:00
|
|
|
"GCMajor", JS::ProfilingCategoryPair::GCCC,
|
2019-01-18 18:40:15 +03:00
|
|
|
MakeUnique<GCMajorMarkerPayload>(aDesc.startTime(aContext),
|
|
|
|
aDesc.endTime(aContext),
|
|
|
|
aDesc.formatJSONProfiler(aContext)));
|
2017-05-13 03:42:43 +03:00
|
|
|
} else if (aProgress == JS::GC_SLICE_END) {
|
2019-01-18 18:40:15 +03:00
|
|
|
profiler_add_marker(
|
2019-02-16 20:37:43 +03:00
|
|
|
"GCSlice", JS::ProfilingCategoryPair::GCCC,
|
2019-01-18 18:40:15 +03:00
|
|
|
MakeUnique<GCSliceMarkerPayload>(
|
|
|
|
aDesc.lastSliceStart(aContext), aDesc.lastSliceEnd(aContext),
|
|
|
|
aDesc.sliceToJSONProfiler(aContext)));
|
2017-05-03 02:13:49 +03:00
|
|
|
}
|
|
|
|
}
|
2017-10-04 01:11:18 +03:00
|
|
|
#endif
|
2017-05-03 02:13:49 +03:00
|
|
|
|
2017-07-12 20:31:56 +03:00
|
|
|
if (aProgress == JS::GC_CYCLE_END &&
|
|
|
|
JS::dbg::FireOnGarbageCollectionHookRequired(aContext)) {
|
2019-01-21 16:09:12 +03:00
|
|
|
JS::GCReason reason = aDesc.reason_;
|
2017-02-24 00:23:45 +03:00
|
|
|
Unused << NS_WARN_IF(
|
|
|
|
NS_FAILED(DebuggerOnGCRunnable::Enqueue(aContext, aDesc)) &&
|
2019-01-21 16:09:12 +03:00
|
|
|
reason != JS::GCReason::SHUTDOWN_CC &&
|
|
|
|
reason != JS::GCReason::DESTROY_RUNTIME &&
|
|
|
|
reason != JS::GCReason::XPCONNECT_SHUTDOWN);
|
2017-02-24 00:23:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (self->mPrevGCSliceCallback) {
|
|
|
|
self->mPrevGCSliceCallback(aContext, aProgress, aDesc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class MinorGCMarker : public TimelineMarker {
|
|
|
|
private:
|
2019-01-21 16:09:12 +03:00
|
|
|
JS::GCReason mReason;
|
2017-02-24 00:23:45 +03:00
|
|
|
|
|
|
|
public:
|
2019-01-21 16:09:12 +03:00
|
|
|
MinorGCMarker(MarkerTracingType aTracingType, JS::GCReason aReason)
|
2017-02-24 00:23:45 +03:00
|
|
|
: TimelineMarker("MinorGC", aTracingType, MarkerStackRequest::NO_STACK),
|
|
|
|
mReason(aReason) {
|
|
|
|
MOZ_ASSERT(aTracingType == MarkerTracingType::START ||
|
|
|
|
aTracingType == MarkerTracingType::END);
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2019-01-21 16:09:12 +03:00
|
|
|
MinorGCMarker(JS::GCNurseryProgress aProgress, JS::GCReason aReason)
|
2017-02-24 00:23:45 +03:00
|
|
|
: TimelineMarker(
|
|
|
|
"MinorGC",
|
|
|
|
aProgress == JS::GCNurseryProgress::GC_NURSERY_COLLECTION_START
|
|
|
|
? MarkerTracingType::START
|
|
|
|
: MarkerTracingType::END,
|
|
|
|
MarkerStackRequest::NO_STACK),
|
|
|
|
mReason(aReason) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-02-24 00:23:45 +03:00
|
|
|
virtual void AddDetails(JSContext* aCx,
|
|
|
|
dom::ProfileTimelineMarker& aMarker) override {
|
|
|
|
TimelineMarker::AddDetails(aCx, aMarker);
|
|
|
|
|
|
|
|
if (GetTracingType() == MarkerTracingType::START) {
|
2019-01-21 16:09:12 +03:00
|
|
|
auto reason = JS::ExplainGCReason(mReason);
|
2017-02-24 00:23:45 +03:00
|
|
|
aMarker.mCauseName.Construct(NS_ConvertUTF8toUTF16(reason));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual UniquePtr<AbstractTimelineMarker> Clone() override {
|
|
|
|
auto clone = MakeUnique<MinorGCMarker>(GetTracingType(), mReason);
|
|
|
|
clone->SetCustomTime(GetTime());
|
2018-05-30 22:15:35 +03:00
|
|
|
return UniquePtr<AbstractTimelineMarker>(std::move(clone));
|
2017-02-24 00:23:45 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-02-26 01:14:01 +03:00
|
|
|
/* static */
|
|
|
|
void CycleCollectedJSRuntime::GCNurseryCollectionCallback(
|
2017-02-24 00:23:45 +03:00
|
|
|
JSContext* aContext, JS::GCNurseryProgress aProgress,
|
2019-01-21 16:09:12 +03:00
|
|
|
JS::GCReason aReason) {
|
2017-02-24 00:23:45 +03:00
|
|
|
CycleCollectedJSRuntime* self = CycleCollectedJSRuntime::Get();
|
|
|
|
MOZ_ASSERT(CycleCollectedJSContext::Get()->Context() == aContext);
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2018-07-31 22:34:50 +03:00
|
|
|
RefPtr<TimelineConsumers> timelines = TimelineConsumers::Get();
|
|
|
|
if (timelines && !timelines->IsEmpty()) {
|
|
|
|
UniquePtr<AbstractTimelineMarker> abstractMarker(
|
|
|
|
MakeUnique<MinorGCMarker>(aProgress, aReason));
|
|
|
|
timelines->AddMarkerForAllObservedDocShells(abstractMarker);
|
2017-02-24 00:23:45 +03:00
|
|
|
}
|
|
|
|
|
2017-04-25 23:24:34 +03:00
|
|
|
if (aProgress == JS::GCNurseryProgress::GC_NURSERY_COLLECTION_START) {
|
|
|
|
self->mLatestNurseryCollectionStart = TimeStamp::Now();
|
2017-10-04 01:11:18 +03:00
|
|
|
}
|
|
|
|
#ifdef MOZ_GECKO_PROFILER
|
|
|
|
else if (aProgress == JS::GCNurseryProgress::GC_NURSERY_COLLECTION_END &&
|
2018-11-19 09:29:15 +03:00
|
|
|
profiler_thread_is_being_profiled()) {
|
2019-02-16 20:37:43 +03:00
|
|
|
profiler_add_marker("GCMinor", JS::ProfilingCategoryPair::GCCC,
|
2019-01-18 18:40:15 +03:00
|
|
|
MakeUnique<GCMinorMarkerPayload>(
|
|
|
|
self->mLatestNurseryCollectionStart,
|
|
|
|
TimeStamp::Now(), JS::MinorGcToJSON(aContext)));
|
2017-06-21 23:26:16 +03:00
|
|
|
}
|
2017-10-04 01:11:18 +03:00
|
|
|
#endif
|
2017-04-25 23:24:34 +03:00
|
|
|
|
2017-02-24 00:23:45 +03:00
|
|
|
if (self->mPrevGCNurseryCollectionCallback) {
|
|
|
|
self->mPrevGCNurseryCollectionCallback(aContext, aProgress, aReason);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:14:01 +03:00
|
|
|
/* static */
|
|
|
|
void CycleCollectedJSRuntime::OutOfMemoryCallback(JSContext* aContext,
|
|
|
|
void* aData) {
|
2017-02-24 00:23:45 +03:00
|
|
|
CycleCollectedJSRuntime* self = static_cast<CycleCollectedJSRuntime*>(aData);
|
|
|
|
|
|
|
|
MOZ_ASSERT(CycleCollectedJSContext::Get()->Context() == aContext);
|
|
|
|
MOZ_ASSERT(CycleCollectedJSContext::Get()->Runtime() == self);
|
|
|
|
|
|
|
|
self->OnOutOfMemory();
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:14:01 +03:00
|
|
|
/* static */
|
|
|
|
size_t CycleCollectedJSRuntime::SizeofExternalStringCallback(
|
2017-02-24 00:23:45 +03:00
|
|
|
JSString* aStr, MallocSizeOf aMallocSizeOf) {
|
|
|
|
// We promised the JS engine we would not GC. Enforce that:
|
|
|
|
JS::AutoCheckCannotGC autoCannotGC;
|
|
|
|
|
|
|
|
if (!XPCStringConvert::IsDOMString(aStr)) {
|
|
|
|
// Might be a literal or something we don't understand. Just claim 0.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char16_t* chars = JS_GetTwoByteExternalStringChars(aStr);
|
|
|
|
const nsStringBuffer* buf = nsStringBuffer::FromData((void*)chars);
|
|
|
|
// We want sizeof including this, because the entire string buffer is owned by
|
|
|
|
// the external string. But only report here if we're unshared; if we're
|
|
|
|
// shared then we don't know who really owns this data.
|
|
|
|
return buf->SizeOfIncludingThisIfUnshared(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct JsGcTracer : public TraceCallbacks {
|
|
|
|
virtual void Trace(JS::Heap<JS::Value>* aPtr, const char* aName,
|
|
|
|
void* aClosure) const override {
|
|
|
|
JS::TraceEdge(static_cast<JSTracer*>(aClosure), aPtr, aName);
|
|
|
|
}
|
|
|
|
virtual void Trace(JS::Heap<jsid>* aPtr, const char* aName,
|
|
|
|
void* aClosure) const override {
|
|
|
|
JS::TraceEdge(static_cast<JSTracer*>(aClosure), aPtr, aName);
|
|
|
|
}
|
|
|
|
virtual void Trace(JS::Heap<JSObject*>* aPtr, const char* aName,
|
|
|
|
void* aClosure) const override {
|
|
|
|
JS::TraceEdge(static_cast<JSTracer*>(aClosure), aPtr, aName);
|
|
|
|
}
|
|
|
|
virtual void Trace(JSObject** aPtr, const char* aName,
|
|
|
|
void* aClosure) const override {
|
|
|
|
js::UnsafeTraceManuallyBarrieredEdge(static_cast<JSTracer*>(aClosure), aPtr,
|
|
|
|
aName);
|
|
|
|
}
|
|
|
|
virtual void Trace(JS::TenuredHeap<JSObject*>* aPtr, const char* aName,
|
|
|
|
void* aClosure) const override {
|
|
|
|
JS::TraceEdge(static_cast<JSTracer*>(aClosure), aPtr, aName);
|
|
|
|
}
|
|
|
|
virtual void Trace(JS::Heap<JSString*>* aPtr, const char* aName,
|
|
|
|
void* aClosure) const override {
|
|
|
|
JS::TraceEdge(static_cast<JSTracer*>(aClosure), aPtr, aName);
|
|
|
|
}
|
|
|
|
virtual void Trace(JS::Heap<JSScript*>* aPtr, const char* aName,
|
|
|
|
void* aClosure) const override {
|
|
|
|
JS::TraceEdge(static_cast<JSTracer*>(aClosure), aPtr, aName);
|
|
|
|
}
|
|
|
|
virtual void Trace(JS::Heap<JSFunction*>* aPtr, const char* aName,
|
|
|
|
void* aClosure) const override {
|
|
|
|
JS::TraceEdge(static_cast<JSTracer*>(aClosure), aPtr, aName);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void mozilla::TraceScriptHolder(nsISupports* aHolder, JSTracer* aTracer) {
|
|
|
|
nsXPCOMCycleCollectionParticipant* participant = nullptr;
|
|
|
|
CallQueryInterface(aHolder, &participant);
|
|
|
|
participant->Trace(aHolder, JsGcTracer(), aTracer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CycleCollectedJSRuntime::TraceNativeGrayRoots(JSTracer* aTracer) {
|
|
|
|
// NB: This is here just to preserve the existing XPConnect order. I doubt it
|
|
|
|
// would hurt to do this after the JS holders.
|
|
|
|
TraceAdditionalNativeGrayRoots(aTracer);
|
|
|
|
|
|
|
|
for (auto iter = mJSHolders.Iter(); !iter.Done(); iter.Next()) {
|
2017-07-11 18:52:01 +03:00
|
|
|
void* holder = iter.Get().mHolder;
|
|
|
|
nsScriptObjectTracer* tracer = iter.Get().mTracer;
|
2017-02-24 00:23:45 +03:00
|
|
|
tracer->Trace(holder, JsGcTracer(), aTracer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CycleCollectedJSRuntime::AddJSHolder(void* aHolder,
|
|
|
|
nsScriptObjectTracer* aTracer) {
|
2017-08-17 19:51:17 +03:00
|
|
|
auto entry = mJSHolderMap.LookupForAdd(aHolder);
|
|
|
|
if (entry) {
|
|
|
|
JSHolderInfo* info = entry.Data();
|
2017-07-11 18:52:01 +03:00
|
|
|
MOZ_ASSERT(info->mHolder == aHolder);
|
|
|
|
info->mTracer = aTracer;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mJSHolders.InfallibleAppend(JSHolderInfo{aHolder, aTracer});
|
2017-08-17 19:51:17 +03:00
|
|
|
entry.OrInsert([&] { return &mJSHolders.GetLast(); });
|
2017-02-24 00:23:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
struct ClearJSHolder : public TraceCallbacks {
|
|
|
|
virtual void Trace(JS::Heap<JS::Value>* aPtr, const char*,
|
|
|
|
void*) const override {
|
|
|
|
aPtr->setUndefined();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Trace(JS::Heap<jsid>* aPtr, const char*, void*) const override {
|
|
|
|
*aPtr = JSID_VOID;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Trace(JS::Heap<JSObject*>* aPtr, const char*,
|
|
|
|
void*) const override {
|
|
|
|
*aPtr = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Trace(JSObject** aPtr, const char* aName,
|
|
|
|
void* aClosure) const override {
|
|
|
|
*aPtr = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Trace(JS::TenuredHeap<JSObject*>* aPtr, const char*,
|
|
|
|
void*) const override {
|
|
|
|
*aPtr = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Trace(JS::Heap<JSString*>* aPtr, const char*,
|
|
|
|
void*) const override {
|
|
|
|
*aPtr = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Trace(JS::Heap<JSScript*>* aPtr, const char*,
|
|
|
|
void*) const override {
|
|
|
|
*aPtr = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Trace(JS::Heap<JSFunction*>* aPtr, const char*,
|
|
|
|
void*) const override {
|
|
|
|
*aPtr = nullptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void CycleCollectedJSRuntime::RemoveJSHolder(void* aHolder) {
|
2017-08-17 19:51:17 +03:00
|
|
|
auto entry = mJSHolderMap.Lookup(aHolder);
|
|
|
|
if (entry) {
|
|
|
|
JSHolderInfo* info = entry.Data();
|
2017-07-11 18:52:01 +03:00
|
|
|
MOZ_ASSERT(info->mHolder == aHolder);
|
|
|
|
info->mTracer->Trace(aHolder, ClearJSHolder(), nullptr);
|
|
|
|
|
|
|
|
JSHolderInfo* lastInfo = &mJSHolders.GetLast();
|
2017-08-17 19:51:17 +03:00
|
|
|
bool updateLast = (info != lastInfo);
|
|
|
|
if (updateLast) {
|
2017-07-11 18:52:01 +03:00
|
|
|
*info = *lastInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
mJSHolders.PopLast();
|
2017-08-17 19:51:17 +03:00
|
|
|
entry.Remove();
|
|
|
|
|
|
|
|
if (updateLast) {
|
|
|
|
// We have to do this after removing the entry above to ensure that we
|
|
|
|
// don't trip over the hashtable generation number assertion.
|
|
|
|
mJSHolderMap.Put(info->mHolder, info);
|
|
|
|
}
|
2017-02-24 00:23:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
bool CycleCollectedJSRuntime::IsJSHolder(void* aHolder) {
|
2017-07-11 18:52:01 +03:00
|
|
|
return mJSHolderMap.Get(aHolder, nullptr);
|
2017-02-24 00:23:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void AssertNoGcThing(JS::GCCellPtr aGCThing, const char* aName,
|
|
|
|
void* aClosure) {
|
|
|
|
MOZ_ASSERT(!aGCThing);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CycleCollectedJSRuntime::AssertNoObjectsToTrace(void* aPossibleJSHolder) {
|
2017-07-11 18:52:01 +03:00
|
|
|
JSHolderInfo* info = nullptr;
|
|
|
|
if (!mJSHolderMap.Get(aPossibleJSHolder, &info)) {
|
|
|
|
return;
|
2017-02-24 00:23:45 +03:00
|
|
|
}
|
2017-07-11 18:52:01 +03:00
|
|
|
|
|
|
|
MOZ_ASSERT(info->mHolder == aPossibleJSHolder);
|
|
|
|
info->mTracer->Trace(aPossibleJSHolder, TraceCallbackFunc(AssertNoGcThing),
|
|
|
|
nullptr);
|
2017-02-24 00:23:45 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
nsCycleCollectionParticipant* CycleCollectedJSRuntime::GCThingParticipant() {
|
|
|
|
return &mGCThingCycleCollectorGlobal;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCycleCollectionParticipant* CycleCollectedJSRuntime::ZoneParticipant() {
|
|
|
|
return &mJSZoneCycleCollectorGlobal;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult CycleCollectedJSRuntime::TraverseRoots(
|
|
|
|
nsCycleCollectionNoteRootCallback& aCb) {
|
|
|
|
TraverseNativeRoots(aCb);
|
|
|
|
|
2017-04-24 05:30:06 +03:00
|
|
|
NoteWeakMapsTracer trc(mJSRuntime, aCb);
|
2017-02-24 00:23:45 +03:00
|
|
|
js::TraceWeakMaps(&trc);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CycleCollectedJSRuntime::UsefulToMergeZones() const { return false; }
|
|
|
|
|
|
|
|
void CycleCollectedJSRuntime::FixWeakMappingGrayBits() const {
|
2017-04-24 05:30:06 +03:00
|
|
|
MOZ_ASSERT(!JS::IsIncrementalGCInProgress(mJSRuntime),
|
2017-02-24 00:23:45 +03:00
|
|
|
"Don't call FixWeakMappingGrayBits during a GC.");
|
2017-04-24 05:30:06 +03:00
|
|
|
FixWeakMappingGrayBitsTracer fixer(mJSRuntime);
|
2017-02-24 00:23:45 +03:00
|
|
|
fixer.FixAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CycleCollectedJSRuntime::CheckGrayBits() const {
|
2017-04-24 05:30:06 +03:00
|
|
|
MOZ_ASSERT(!JS::IsIncrementalGCInProgress(mJSRuntime),
|
2017-02-24 00:23:45 +03:00
|
|
|
"Don't call CheckGrayBits during a GC.");
|
|
|
|
|
|
|
|
#ifndef ANDROID
|
|
|
|
// Bug 1346874 - The gray state check is expensive. Android tests are already
|
|
|
|
// slow enough that this check can easily push them over the threshold to a
|
|
|
|
// timeout.
|
|
|
|
|
2017-04-24 05:30:06 +03:00
|
|
|
MOZ_ASSERT(js::CheckGrayMarkingState(mJSRuntime));
|
|
|
|
MOZ_ASSERT(CheckWeakMappingGrayBitsTracer::Check(mJSRuntime));
|
2017-02-24 00:23:45 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CycleCollectedJSRuntime::AreGCGrayBitsValid() const {
|
2017-04-24 05:30:06 +03:00
|
|
|
return js::AreGCGrayBitsValid(mJSRuntime);
|
2017-02-24 00:23:45 +03:00
|
|
|
}
|
|
|
|
|
2019-01-21 16:09:12 +03:00
|
|
|
void CycleCollectedJSRuntime::GarbageCollect(JS::GCReason aReason) const {
|
2017-04-24 05:30:06 +03:00
|
|
|
JSContext* cx = CycleCollectedJSContext::Get()->Context();
|
|
|
|
JS::PrepareForFullGC(cx);
|
2019-01-21 16:09:12 +03:00
|
|
|
JS::NonIncrementalGC(cx, GC_NORMAL, aReason);
|
2017-02-24 00:23:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CycleCollectedJSRuntime::JSObjectsTenured() {
|
|
|
|
for (auto iter = mNurseryObjects.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
nsWrapperCache* cache = iter.Get();
|
2017-04-26 13:18:39 +03:00
|
|
|
JSObject* wrapper = cache->GetWrapperMaybeDead();
|
2018-07-23 17:46:37 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(wrapper || recordreplay::IsReplaying());
|
2017-02-24 00:23:45 +03:00
|
|
|
if (!JS::ObjectIsTenured(wrapper)) {
|
|
|
|
MOZ_ASSERT(!cache->PreservingWrapper());
|
|
|
|
const JSClass* jsClass = js::GetObjectJSClass(wrapper);
|
|
|
|
jsClass->doFinalize(nullptr, wrapper);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
for (auto iter = mPreservedNurseryObjects.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
MOZ_ASSERT(JS::ObjectIsTenured(iter.Get().get()));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
mNurseryObjects.Clear();
|
|
|
|
mPreservedNurseryObjects.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CycleCollectedJSRuntime::NurseryWrapperAdded(nsWrapperCache* aCache) {
|
|
|
|
MOZ_ASSERT(aCache);
|
2017-04-26 13:18:39 +03:00
|
|
|
MOZ_ASSERT(aCache->GetWrapperMaybeDead());
|
|
|
|
MOZ_ASSERT(!JS::ObjectIsTenured(aCache->GetWrapperMaybeDead()));
|
2017-02-24 00:23:45 +03:00
|
|
|
mNurseryObjects.InfallibleAppend(aCache);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CycleCollectedJSRuntime::NurseryWrapperPreserved(JSObject* aWrapper) {
|
|
|
|
mPreservedNurseryObjects.InfallibleAppend(
|
2017-04-24 05:30:06 +03:00
|
|
|
JS::PersistentRooted<JSObject*>(mJSRuntime, aWrapper));
|
2017-02-24 00:23:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CycleCollectedJSRuntime::DeferredFinalize(
|
|
|
|
DeferredFinalizeAppendFunction aAppendFunc, DeferredFinalizeFunction aFunc,
|
|
|
|
void* aThing) {
|
2017-06-28 02:03:18 +03:00
|
|
|
if (auto entry = mDeferredFinalizerTable.LookupForAdd(aFunc)) {
|
|
|
|
aAppendFunc(entry.Data(), aThing);
|
|
|
|
} else {
|
|
|
|
entry.OrInsert(
|
|
|
|
[aAppendFunc, aThing]() { return aAppendFunc(nullptr, aThing); });
|
2017-02-24 00:23:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CycleCollectedJSRuntime::DeferredFinalize(nsISupports* aSupports) {
|
|
|
|
typedef DeferredFinalizerImpl<nsISupports> Impl;
|
|
|
|
DeferredFinalize(Impl::AppendDeferredFinalizePointer, Impl::DeferredFinalize,
|
|
|
|
aSupports);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CycleCollectedJSRuntime::DumpJSHeap(FILE* aFile) {
|
2017-04-24 05:30:06 +03:00
|
|
|
JSContext* cx = CycleCollectedJSContext::Get()->Context();
|
|
|
|
js::DumpHeap(cx, aFile, js::CollectNurseryBeforeDump);
|
2017-02-24 00:23:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
IncrementalFinalizeRunnable::IncrementalFinalizeRunnable(
|
|
|
|
CycleCollectedJSRuntime* aRt, DeferredFinalizerTable& aFinalizers)
|
2017-07-21 18:30:55 +03:00
|
|
|
: CancelableRunnable("IncrementalFinalizeRunnable"),
|
2017-02-24 00:23:45 +03:00
|
|
|
mRuntime(aRt),
|
|
|
|
mFinalizeFunctionToRun(0),
|
|
|
|
mReleasing(false) {
|
|
|
|
for (auto iter = aFinalizers.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
DeferredFinalizeFunction& function = iter.Key();
|
|
|
|
void*& data = iter.Data();
|
|
|
|
|
|
|
|
DeferredFinalizeFunctionHolder* holder =
|
|
|
|
mDeferredFinalizeFunctions.AppendElement();
|
|
|
|
holder->run = function;
|
|
|
|
holder->data = data;
|
|
|
|
|
|
|
|
iter.Remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
IncrementalFinalizeRunnable::~IncrementalFinalizeRunnable() {
|
|
|
|
MOZ_ASSERT(this != mRuntime->mFinalizeRunnable);
|
|
|
|
}
|
|
|
|
|
|
|
|
void IncrementalFinalizeRunnable::ReleaseNow(bool aLimited) {
|
|
|
|
if (mReleasing) {
|
|
|
|
NS_WARNING("Re-entering ReleaseNow");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
mozilla::AutoRestore<bool> ar(mReleasing);
|
|
|
|
mReleasing = true;
|
|
|
|
MOZ_ASSERT(mDeferredFinalizeFunctions.Length() != 0,
|
|
|
|
"We should have at least ReleaseSliceNow to run");
|
|
|
|
MOZ_ASSERT(mFinalizeFunctionToRun < mDeferredFinalizeFunctions.Length(),
|
|
|
|
"No more finalizers to run?");
|
2018-07-23 17:35:17 +03:00
|
|
|
if (recordreplay::IsRecordingOrReplaying()) {
|
|
|
|
aLimited = false;
|
|
|
|
}
|
2017-02-24 00:23:45 +03:00
|
|
|
|
|
|
|
TimeDuration sliceTime = TimeDuration::FromMilliseconds(SliceMillis);
|
2018-07-23 17:35:17 +03:00
|
|
|
TimeStamp started = aLimited ? TimeStamp::Now() : TimeStamp();
|
2017-02-24 00:23:45 +03:00
|
|
|
bool timeout = false;
|
|
|
|
do {
|
|
|
|
const DeferredFinalizeFunctionHolder& function =
|
|
|
|
mDeferredFinalizeFunctions[mFinalizeFunctionToRun];
|
|
|
|
if (aLimited) {
|
|
|
|
bool done = false;
|
|
|
|
while (!timeout && !done) {
|
|
|
|
/*
|
|
|
|
* We don't want to read the clock too often, so we try to
|
|
|
|
* release slices of 100 items.
|
|
|
|
*/
|
|
|
|
done = function.run(100, function.data);
|
|
|
|
timeout = TimeStamp::Now() - started >= sliceTime;
|
|
|
|
}
|
|
|
|
if (done) {
|
|
|
|
++mFinalizeFunctionToRun;
|
|
|
|
}
|
|
|
|
if (timeout) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while (!function.run(UINT32_MAX, function.data))
|
|
|
|
;
|
|
|
|
++mFinalizeFunctionToRun;
|
|
|
|
}
|
|
|
|
} while (mFinalizeFunctionToRun < mDeferredFinalizeFunctions.Length());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mFinalizeFunctionToRun == mDeferredFinalizeFunctions.Length()) {
|
|
|
|
MOZ_ASSERT(mRuntime->mFinalizeRunnable == this);
|
|
|
|
mDeferredFinalizeFunctions.Clear();
|
|
|
|
// NB: This may delete this!
|
|
|
|
mRuntime->mFinalizeRunnable = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
IncrementalFinalizeRunnable::Run() {
|
2018-05-31 21:08:50 +03:00
|
|
|
AUTO_PROFILER_LABEL("IncrementalFinalizeRunnable::Run", GCCC);
|
|
|
|
|
2017-02-24 00:23:45 +03:00
|
|
|
if (mRuntime->mFinalizeRunnable != this) {
|
|
|
|
/* These items were already processed synchronously in JSGC_END. */
|
|
|
|
MOZ_ASSERT(!mDeferredFinalizeFunctions.Length());
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
TimeStamp start = TimeStamp::Now();
|
|
|
|
ReleaseNow(true);
|
|
|
|
|
|
|
|
if (mDeferredFinalizeFunctions.Length()) {
|
|
|
|
nsresult rv = NS_DispatchToCurrentThread(this);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
ReleaseNow(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t duration = (uint32_t)((TimeStamp::Now() - start).ToMilliseconds());
|
|
|
|
Telemetry::Accumulate(Telemetry::DEFERRED_FINALIZE_ASYNC, duration);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CycleCollectedJSRuntime::FinalizeDeferredThings(
|
|
|
|
CycleCollectedJSContext::DeferredFinalizeType aType) {
|
|
|
|
/*
|
|
|
|
* If the previous GC created a runnable to finalize objects
|
|
|
|
* incrementally, and if it hasn't finished yet, finish it now. We
|
|
|
|
* don't want these to build up. We also don't want to allow any
|
|
|
|
* existing incremental finalize runnables to run after a
|
|
|
|
* non-incremental GC, since they are often used to detect leaks.
|
|
|
|
*/
|
|
|
|
if (mFinalizeRunnable) {
|
|
|
|
mFinalizeRunnable->ReleaseNow(false);
|
|
|
|
if (mFinalizeRunnable) {
|
|
|
|
// If we re-entered ReleaseNow, we couldn't delete mFinalizeRunnable and
|
|
|
|
// we need to just continue processing it.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-31 18:35:36 +03:00
|
|
|
// When recording or replaying, execute triggers that were activated recently
|
|
|
|
// by mozilla::DeferredFinalize. This will populate the deferred finalizer
|
|
|
|
// table with a consistent set of entries between the recording and replay.
|
|
|
|
if (recordreplay::IsRecordingOrReplaying()) {
|
|
|
|
recordreplay::ExecuteTriggers();
|
|
|
|
}
|
|
|
|
|
2017-02-24 00:23:45 +03:00
|
|
|
if (mDeferredFinalizerTable.Count() == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mFinalizeRunnable =
|
|
|
|
new IncrementalFinalizeRunnable(this, mDeferredFinalizerTable);
|
|
|
|
|
|
|
|
// Everything should be gone now.
|
|
|
|
MOZ_ASSERT(mDeferredFinalizerTable.Count() == 0);
|
|
|
|
|
|
|
|
if (aType == CycleCollectedJSContext::FinalizeIncrementally) {
|
2019-01-26 20:18:05 +03:00
|
|
|
NS_DispatchToCurrentThreadQueue(do_AddRef(mFinalizeRunnable), 2500,
|
|
|
|
EventQueuePriority::Idle);
|
2017-02-24 00:23:45 +03:00
|
|
|
} else {
|
|
|
|
mFinalizeRunnable->ReleaseNow(false);
|
|
|
|
MOZ_ASSERT(!mFinalizeRunnable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Bug 1348273 - Convert crash annotations into a machine-readable list of constants; r=ted.mielczarek,njn,dholbert,mak,cpearce,mcmanus,froydnj,Dexter,jrmuizel,jchen,jimm,bz,surkov
This introduces the machinery needed to generate crash annotations from a YAML
file. The relevant C++ functions are updated to take a typed enum. JavaScript
calls are unaffected but they will throw if the string argument does not
correspond to one of the known entries in the C++ enum. The existing whitelists
and blacklists of annotations are also generated from the YAML file and all
duplicate code related to them has been consolidated. Once written out to the
.extra file the annotations are converted in string form and are no different
than the existing ones.
All existing annotations have been included in the list (and some obsolete ones
have been removed) and all call sites have been updated including tests where
appropriate.
--HG--
extra : source : 4f6c43f2830701ec5552e08e3f1b06fe6d045860
2018-07-05 16:42:11 +03:00
|
|
|
const char* CycleCollectedJSRuntime::OOMStateToString(
|
|
|
|
const OOMState aOomState) const {
|
|
|
|
switch (aOomState) {
|
|
|
|
case OOMState::OK:
|
|
|
|
return "OK";
|
|
|
|
case OOMState::Reporting:
|
|
|
|
return "Reporting";
|
|
|
|
case OOMState::Reported:
|
|
|
|
return "Reported";
|
|
|
|
case OOMState::Recovered:
|
|
|
|
return "Recovered";
|
|
|
|
default:
|
|
|
|
MOZ_ASSERT_UNREACHABLE("OOMState holds an invalid value");
|
|
|
|
return "Unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-24 00:23:45 +03:00
|
|
|
void CycleCollectedJSRuntime::AnnotateAndSetOutOfMemory(OOMState* aStatePtr,
|
|
|
|
OOMState aNewState) {
|
|
|
|
*aStatePtr = aNewState;
|
Bug 1348273 - Convert crash annotations into a machine-readable list of constants; r=ted.mielczarek,njn,dholbert,mak,cpearce,mcmanus,froydnj,Dexter,jrmuizel,jchen,jimm,bz,surkov
This introduces the machinery needed to generate crash annotations from a YAML
file. The relevant C++ functions are updated to take a typed enum. JavaScript
calls are unaffected but they will throw if the string argument does not
correspond to one of the known entries in the C++ enum. The existing whitelists
and blacklists of annotations are also generated from the YAML file and all
duplicate code related to them has been consolidated. Once written out to the
.extra file the annotations are converted in string form and are no different
than the existing ones.
All existing annotations have been included in the list (and some obsolete ones
have been removed) and all call sites have been updated including tests where
appropriate.
--HG--
extra : source : 4f6c43f2830701ec5552e08e3f1b06fe6d045860
2018-07-05 16:42:11 +03:00
|
|
|
CrashReporter::Annotation annotation =
|
|
|
|
(aStatePtr == &mOutOfMemoryState)
|
|
|
|
? CrashReporter::Annotation::JSOutOfMemory
|
|
|
|
: CrashReporter::Annotation::JSLargeAllocationFailure;
|
|
|
|
|
|
|
|
CrashReporter::AnnotateCrashReport(
|
|
|
|
annotation, nsDependentCString(OOMStateToString(aNewState)));
|
2017-02-24 00:23:45 +03:00
|
|
|
}
|
|
|
|
|
2017-05-12 21:16:16 +03:00
|
|
|
void CycleCollectedJSRuntime::OnGC(JSContext* aContext, JSGCStatus aStatus) {
|
2017-02-24 00:23:45 +03:00
|
|
|
switch (aStatus) {
|
|
|
|
case JSGC_BEGIN:
|
|
|
|
nsCycleCollector_prepareForGarbageCollection();
|
|
|
|
mZonesWaitingForGC.Clear();
|
|
|
|
break;
|
|
|
|
case JSGC_END: {
|
|
|
|
if (mOutOfMemoryState == OOMState::Reported) {
|
|
|
|
AnnotateAndSetOutOfMemory(&mOutOfMemoryState, OOMState::Recovered);
|
|
|
|
}
|
|
|
|
if (mLargeAllocationFailureState == OOMState::Reported) {
|
|
|
|
AnnotateAndSetOutOfMemory(&mLargeAllocationFailureState,
|
|
|
|
OOMState::Recovered);
|
|
|
|
}
|
|
|
|
|
2017-05-12 21:16:16 +03:00
|
|
|
// Do any deferred finalization of native objects. Normally we do this
|
|
|
|
// incrementally for an incremental GC, and immediately for a
|
|
|
|
// non-incremental GC, on the basis that the type of GC reflects how
|
|
|
|
// urgently resources should be destroyed. However under some
|
|
|
|
// circumstances (such as in js::InternalCallOrConstruct) we can end up
|
|
|
|
// running a non-incremental GC when there is a pending exception, and the
|
|
|
|
// finalizers are not set up to handle that. In that case, just run them
|
|
|
|
// later, after we've returned to the event loop.
|
2018-07-31 22:34:50 +03:00
|
|
|
bool finalizeIncrementally =
|
|
|
|
JS::WasIncrementalGC(mJSRuntime) || JS_IsExceptionPending(aContext);
|
|
|
|
FinalizeDeferredThings(
|
|
|
|
finalizeIncrementally ? CycleCollectedJSContext::FinalizeIncrementally
|
|
|
|
: CycleCollectedJSContext::FinalizeNow);
|
2017-05-12 21:16:16 +03:00
|
|
|
|
2017-02-24 00:23:45 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
|
|
|
|
CustomGCCallback(aStatus);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CycleCollectedJSRuntime::OnOutOfMemory() {
|
|
|
|
AnnotateAndSetOutOfMemory(&mOutOfMemoryState, OOMState::Reporting);
|
|
|
|
CustomOutOfMemoryCallback();
|
|
|
|
AnnotateAndSetOutOfMemory(&mOutOfMemoryState, OOMState::Reported);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CycleCollectedJSRuntime::SetLargeAllocationFailure(OOMState aNewState) {
|
|
|
|
AnnotateAndSetOutOfMemory(&mLargeAllocationFailureState, aNewState);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CycleCollectedJSRuntime::PrepareWaitingZonesForGC() {
|
2017-04-24 05:30:06 +03:00
|
|
|
JSContext* cx = CycleCollectedJSContext::Get()->Context();
|
2017-02-24 00:23:45 +03:00
|
|
|
if (mZonesWaitingForGC.Count() == 0) {
|
2017-04-24 05:30:06 +03:00
|
|
|
JS::PrepareForFullGC(cx);
|
2017-02-24 00:23:45 +03:00
|
|
|
} else {
|
|
|
|
for (auto iter = mZonesWaitingForGC.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
JS::PrepareZoneForGC(iter.Get()->GetKey());
|
|
|
|
}
|
|
|
|
mZonesWaitingForGC.Clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-11 12:44:52 +03:00
|
|
|
void CycleCollectedJSRuntime::EnvironmentPreparer::invoke(
|
2017-02-24 00:23:45 +03:00
|
|
|
JS::HandleObject global, js::ScriptEnvironmentPreparer::Closure& closure) {
|
2018-07-11 12:44:52 +03:00
|
|
|
MOZ_ASSERT(JS_IsGlobalObject(global));
|
|
|
|
nsIGlobalObject* nativeGlobal = xpc::NativeGlobal(global);
|
2017-02-24 00:23:45 +03:00
|
|
|
|
|
|
|
// Not much we can do if we simply don't have a usable global here...
|
2018-07-11 12:44:52 +03:00
|
|
|
NS_ENSURE_TRUE_VOID(nativeGlobal && nativeGlobal->GetGlobalJSObject());
|
2017-02-24 00:23:45 +03:00
|
|
|
|
2018-07-11 12:44:52 +03:00
|
|
|
AutoEntryScript aes(nativeGlobal, "JS-engine-initiated execution");
|
2017-02-24 00:23:45 +03:00
|
|
|
|
|
|
|
MOZ_ASSERT(!JS_IsExceptionPending(aes.cx()));
|
|
|
|
|
|
|
|
DebugOnly<bool> ok = closure(aes.cx());
|
|
|
|
|
|
|
|
MOZ_ASSERT_IF(ok, !JS_IsExceptionPending(aes.cx()));
|
|
|
|
|
|
|
|
// The AutoEntryScript will check for JS_IsExceptionPending on the
|
|
|
|
// JSContext and report it as needed as it comes off the stack.
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:14:01 +03:00
|
|
|
/* static */
|
|
|
|
CycleCollectedJSRuntime* CycleCollectedJSRuntime::Get() {
|
2017-02-24 00:23:45 +03:00
|
|
|
auto context = CycleCollectedJSContext::Get();
|
|
|
|
if (context) {
|
|
|
|
return context->Runtime();
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-11-16 12:48:45 +03:00
|
|
|
|
|
|
|
#ifdef MOZ_JS_DEV_ERROR_INTERCEPTOR
|
|
|
|
|
|
|
|
namespace js {
|
|
|
|
extern void DumpValue(const JS::Value& val);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CycleCollectedJSRuntime::ErrorInterceptor::Shutdown(JSRuntime* rt) {
|
|
|
|
JS_SetErrorInterceptorCallback(rt, nullptr);
|
|
|
|
mThrownError.reset();
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:14:01 +03:00
|
|
|
/* virtual */
|
|
|
|
void CycleCollectedJSRuntime::ErrorInterceptor::interceptError(
|
2018-10-26 20:38:30 +03:00
|
|
|
JSContext* cx, JS::HandleValue exn) {
|
2017-11-16 12:48:45 +03:00
|
|
|
if (mThrownError) {
|
|
|
|
// We already have an error, we don't need anything more.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!nsContentUtils::ThreadsafeIsSystemCaller(cx)) {
|
|
|
|
// We are only interested in chrome code.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto type = JS_GetErrorType(exn);
|
|
|
|
if (!type) {
|
|
|
|
// This is not one of the primitive error types.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (*type) {
|
|
|
|
case JSExnType::JSEXN_REFERENCEERR:
|
|
|
|
case JSExnType::JSEXN_SYNTAXERR:
|
|
|
|
case JSExnType::JSEXN_TYPEERR:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Not one of the errors we are interested in.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now copy the details of the exception locally.
|
|
|
|
// While copying the details of an exception could be expensive, in most runs,
|
|
|
|
// this will be done at most once during the execution of the process, so the
|
|
|
|
// total cost should be reasonable.
|
|
|
|
|
|
|
|
ErrorDetails details;
|
|
|
|
details.mType = *type;
|
|
|
|
// If `exn` isn't an exception object, `ExtractErrorValues` could end up
|
|
|
|
// calling `toString()`, which could in turn end up throwing an error. While
|
|
|
|
// this should work, we want to avoid that complex use case. Fortunately, we
|
|
|
|
// have already checked above that `exn` is an exception object, so nothing
|
|
|
|
// such should happen.
|
2018-10-26 20:38:30 +03:00
|
|
|
nsContentUtils::ExtractErrorValues(cx, exn, details.mFilename, &details.mLine,
|
|
|
|
&details.mColumn, details.mMessage);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-09-05 13:01:23 +03:00
|
|
|
JS::UniqueChars buf =
|
|
|
|
JS::FormatStackDump(cx, /* showArgs = */ false, /* showLocals = */ false,
|
|
|
|
/* showThisProps = */ false);
|
2018-07-06 10:44:43 +03:00
|
|
|
CopyUTF8toUTF16(mozilla::MakeStringSpan(buf.get()), details.mStack);
|
2017-11-16 12:48:45 +03:00
|
|
|
|
2018-05-30 22:15:35 +03:00
|
|
|
mThrownError.emplace(std::move(details));
|
2017-11-16 12:48:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CycleCollectedJSRuntime::ClearRecentDevError() {
|
|
|
|
mErrorInterceptor.mThrownError.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CycleCollectedJSRuntime::GetRecentDevError(
|
|
|
|
JSContext* cx, JS::MutableHandle<JS::Value> error) {
|
|
|
|
if (!mErrorInterceptor.mThrownError) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a copy of the exception.
|
|
|
|
JS::RootedObject obj(cx, JS_NewPlainObject(cx));
|
|
|
|
if (!obj) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::RootedValue message(cx);
|
|
|
|
JS::RootedValue filename(cx);
|
|
|
|
JS::RootedValue stack(cx);
|
|
|
|
if (!ToJSValue(cx, mErrorInterceptor.mThrownError->mMessage, &message) ||
|
|
|
|
!ToJSValue(cx, mErrorInterceptor.mThrownError->mFilename, &filename) ||
|
|
|
|
!ToJSValue(cx, mErrorInterceptor.mThrownError->mStack, &stack)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the object.
|
|
|
|
const auto FLAGS = JSPROP_READONLY | JSPROP_ENUMERATE | JSPROP_PERMANENT;
|
|
|
|
if (!JS_DefineProperty(cx, obj, "message", message, FLAGS) ||
|
|
|
|
!JS_DefineProperty(cx, obj, "fileName", filename, FLAGS) ||
|
|
|
|
!JS_DefineProperty(cx, obj, "lineNumber",
|
|
|
|
mErrorInterceptor.mThrownError->mLine, FLAGS) ||
|
|
|
|
!JS_DefineProperty(cx, obj, "stack", stack, FLAGS)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pass the result.
|
|
|
|
error.setObject(*obj);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif // MOZ_JS_DEV_ERROR_INTERCEPTOR
|
|
|
|
|
|
|
|
#undef MOZ_JS_DEV_ERROR_INTERCEPTOR
|