| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
/* |
| 8 |
* GC-internal definitions. |
| 9 |
*/ |
| 10 |
|
| 11 |
#ifndef gc_GCInternals_h |
| 12 |
#define gc_GCInternals_h |
| 13 |
|
| 14 |
#include "mozilla/Maybe.h" |
| 15 |
#include "mozilla/Range.h" |
| 16 |
#include "mozilla/TimeStamp.h" |
| 17 |
#include "mozilla/Vector.h" |
| 18 |
|
| 19 |
#include "gc/Cell.h" |
| 20 |
#include "gc/GC.h" |
| 21 |
#include "gc/GCContext.h" |
| 22 |
#include "gc/GCMarker.h" |
| 23 |
#include "vm/GeckoProfiler.h" |
| 24 |
#include "vm/HelperThreads.h" |
| 25 |
#include "vm/JSContext.h" |
| 26 |
|
| 27 |
namespace js { |
| 28 |
|
| 29 |
class GCMarker; |
| 30 |
|
| 31 |
namespace gc { |
| 32 |
|
| 33 |
/* |
| 34 |
* There are a couple of classes here that serve mostly as "tokens" indicating |
| 35 |
* that a precondition holds. Some functions force the caller to possess such a |
| 36 |
* token because they require the precondition to hold, and it is better to make |
| 37 |
* the precondition explicit at the API entry point than to crash in an |
| 38 |
* assertion later on when it is relied upon. |
| 39 |
*/ |
| 40 |
|
| 41 |
struct MOZ_RAII AutoAssertNoNurseryAlloc { |
| 42 |
#ifdef DEBUG |
| 43 |
AutoAssertNoNurseryAlloc(); |
| 44 |
~AutoAssertNoNurseryAlloc(); |
| 45 |
#else |
| 46 |
AutoAssertNoNurseryAlloc() {} |
| 47 |
#endif |
| 48 |
}; |
| 49 |
|
| 50 |
/* |
| 51 |
* A class that serves as a token that the nursery in the current thread's zone |
| 52 |
* group is empty. |
| 53 |
*/ |
| 54 |
class MOZ_RAII AutoAssertEmptyNursery { |
| 55 |
protected: |
| 56 |
JSContext* cx; |
| 57 |
|
| 58 |
mozilla::Maybe<AutoAssertNoNurseryAlloc> noAlloc; |
| 59 |
|
| 60 |
// Check that the nursery is empty. |
| 61 |
void checkCondition(JSContext* cx); |
| 62 |
|
| 63 |
// For subclasses that need to empty the nursery in their constructors. |
| 64 |
AutoAssertEmptyNursery() : cx(nullptr) {} |
| 65 |
|
| 66 |
public: |
| 67 |
explicit AutoAssertEmptyNursery(JSContext* cx) : cx(nullptr) { |
| 68 |
checkCondition(cx); |
| 69 |
} |
| 70 |
|
| 71 |
AutoAssertEmptyNursery(const AutoAssertEmptyNursery& other) |
| 72 |
: AutoAssertEmptyNursery(other.cx) {} |
| 73 |
}; |
| 74 |
|
| 75 |
/* |
| 76 |
* Evict the nursery upon construction. Serves as a token indicating that the |
| 77 |
* nursery is empty. (See AutoAssertEmptyNursery, above.) |
| 78 |
*/ |
| 79 |
class MOZ_RAII AutoEmptyNursery : public AutoAssertEmptyNursery { |
| 80 |
public: |
| 81 |
explicit AutoEmptyNursery(JSContext* cx); |
| 82 |
}; |
| 83 |
|
| 84 |
// Abstract base class for exclusive heap access for tracing or GC. |
| 85 |
class MOZ_RAII AutoHeapSession { |
| 86 |
public: |
| 87 |
~AutoHeapSession(); |
| 88 |
|
| 89 |
protected: |
| 90 |
AutoHeapSession(GCRuntime* gc, JS::HeapState state); |
| 91 |
|
| 92 |
private: |
| 93 |
AutoHeapSession(const AutoHeapSession&) = delete; |
| 94 |
void operator=(const AutoHeapSession&) = delete; |
| 95 |
|
| 96 |
GCRuntime* gc; |
| 97 |
JS::HeapState prevState; |
| 98 |
mozilla::Maybe<AutoGeckoProfilerEntry> profilingStackFrame; |
| 99 |
}; |
| 100 |
|
| 101 |
class MOZ_RAII AutoGCSession : public AutoHeapSession { |
| 102 |
public: |
| 103 |
explicit AutoGCSession(GCRuntime* gc, JS::HeapState state) |
| 104 |
: AutoHeapSession(gc, state) {} |
| 105 |
}; |
| 106 |
|
| 107 |
class MOZ_RAII AutoMajorGCProfilerEntry : public AutoGeckoProfilerEntry { |
| 108 |
public: |
| 109 |
explicit AutoMajorGCProfilerEntry(GCRuntime* gc); |
| 110 |
}; |
| 111 |
|
| 112 |
class MOZ_RAII AutoTraceSession : public AutoHeapSession { |
| 113 |
public: |
| 114 |
explicit AutoTraceSession(JSRuntime* rt) |
| 115 |
: AutoHeapSession(&rt->gc, JS::HeapState::Tracing) {} |
| 116 |
}; |
| 117 |
|
| 118 |
struct MOZ_RAII AutoFinishGC { |
| 119 |
explicit AutoFinishGC(JSContext* cx, JS::GCReason reason) { |
| 120 |
FinishGC(cx, reason); |
| 121 |
} |
| 122 |
}; |
| 123 |
|
| 124 |
// This class should be used by any code that needs exclusive access to the heap |
| 125 |
// in order to trace through it. |
| 126 |
class MOZ_RAII AutoPrepareForTracing : private AutoFinishGC, |
| 127 |
public AutoTraceSession { |
| 128 |
public: |
| 129 |
explicit AutoPrepareForTracing(JSContext* cx) |
| 130 |
: AutoFinishGC(cx, JS::GCReason::PREPARE_FOR_TRACING), |
| 131 |
AutoTraceSession(cx->runtime()) {} |
| 132 |
}; |
| 133 |
|
| 134 |
// This class should be used by any code that needs exclusive access to the heap |
| 135 |
// in order to trace through it. |
| 136 |
// |
| 137 |
// This version also empties the nursery after finishing any ongoing GC. |
| 138 |
class MOZ_RAII AutoEmptyNurseryAndPrepareForTracing : private AutoFinishGC, |
| 139 |
public AutoEmptyNursery, |
| 140 |
public AutoTraceSession { |
| 141 |
public: |
| 142 |
explicit AutoEmptyNurseryAndPrepareForTracing(JSContext* cx) |
| 143 |
: AutoFinishGC(cx, JS::GCReason::PREPARE_FOR_TRACING), |
| 144 |
AutoEmptyNursery(cx), |
| 145 |
AutoTraceSession(cx->runtime()) {} |
| 146 |
}; |
| 147 |
|
| 148 |
/* |
| 149 |
* Temporarily disable incremental barriers. |
| 150 |
*/ |
| 151 |
class AutoDisableBarriers { |
| 152 |
public: |
| 153 |
explicit AutoDisableBarriers(GCRuntime* gc); |
| 154 |
~AutoDisableBarriers(); |
| 155 |
|
| 156 |
private: |
| 157 |
GCRuntime* gc; |
| 158 |
}; |
| 159 |
|
| 160 |
// Set compartments' maybeAlive flags if anything is marked while this class is |
| 161 |
// live. This is used while marking roots. |
| 162 |
class AutoUpdateLiveCompartments { |
| 163 |
GCRuntime* gc; |
| 164 |
|
| 165 |
public: |
| 166 |
explicit AutoUpdateLiveCompartments(GCRuntime* gc); |
| 167 |
~AutoUpdateLiveCompartments(); |
| 168 |
}; |
| 169 |
|
| 170 |
class MOZ_RAII AutoRunParallelTask : public GCParallelTask { |
| 171 |
// This class takes a pointer to a member function of GCRuntime. |
| 172 |
using TaskFunc = JS_MEMBER_FN_PTR_TYPE(GCRuntime, void); |
| 173 |
|
| 174 |
TaskFunc func_; |
| 175 |
AutoLockHelperThreadState& lock_; |
| 176 |
|
| 177 |
public: |
| 178 |
AutoRunParallelTask(GCRuntime* gc, TaskFunc func, gcstats::PhaseKind phase, |
| 179 |
GCUse use, AutoLockHelperThreadState& lock) |
| 180 |
: GCParallelTask(gc, phase, use), func_(func), lock_(lock) { |
| 181 |
gc->startTask(*this, lock_); |
| 182 |
} |
| 183 |
|
| 184 |
~AutoRunParallelTask() { gc->joinTask(*this, lock_); } |
| 185 |
|
| 186 |
void run(AutoLockHelperThreadState& lock) override { |
| 187 |
AutoUnlockHelperThreadState unlock(lock); |
| 188 |
|
| 189 |
// The hazard analysis can't tell what the call to func_ will do but it's |
| 190 |
// not allowed to GC. |
| 191 |
JS::AutoSuppressGCAnalysis nogc; |
| 192 |
|
| 193 |
// Call pointer to member function on |gc|. |
| 194 |
JS_CALL_MEMBER_FN_PTR(gc, func_); |
| 195 |
} |
| 196 |
}; |
| 197 |
|
| 198 |
#ifdef JS_GC_ZEAL |
| 199 |
|
| 200 |
class MOZ_RAII AutoStopVerifyingBarriers { |
| 201 |
GCRuntime* gc; |
| 202 |
bool restartPreVerifier; |
| 203 |
|
| 204 |
public: |
| 205 |
AutoStopVerifyingBarriers(JSRuntime* rt, bool isShutdown) : gc(&rt->gc) { |
| 206 |
if (gc->isVerifyPreBarriersEnabled()) { |
| 207 |
gc->endVerifyPreBarriers(); |
| 208 |
restartPreVerifier = !isShutdown; |
| 209 |
} else { |
| 210 |
restartPreVerifier = false; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
~AutoStopVerifyingBarriers() { |
| 215 |
// Nasty special case: verification runs a minor GC, which *may* nest |
| 216 |
// inside of an outer minor GC. This is not allowed by the |
| 217 |
// gc::Statistics phase tree. So we pause the "real" GC, if in fact one |
| 218 |
// is in progress. |
| 219 |
gcstats::PhaseKind outer = gc->stats().currentPhaseKind(); |
| 220 |
if (outer != gcstats::PhaseKind::NONE) { |
| 221 |
gc->stats().endPhase(outer); |
| 222 |
} |
| 223 |
MOZ_ASSERT(gc->stats().currentPhaseKind() == gcstats::PhaseKind::NONE); |
| 224 |
|
| 225 |
if (restartPreVerifier) { |
| 226 |
gc->startVerifyPreBarriers(); |
| 227 |
} |
| 228 |
|
| 229 |
if (outer != gcstats::PhaseKind::NONE) { |
| 230 |
gc->stats().beginPhase(outer); |
| 231 |
} |
| 232 |
} |
| 233 |
}; |
| 234 |
#else |
| 235 |
struct MOZ_RAII AutoStopVerifyingBarriers { |
| 236 |
AutoStopVerifyingBarriers(JSRuntime*, bool) {} |
| 237 |
}; |
| 238 |
#endif /* JS_GC_ZEAL */ |
| 239 |
|
| 240 |
class MOZ_RAII AutoPoisonFreedJitCode { |
| 241 |
JS::GCContext* const gcx; |
| 242 |
|
| 243 |
public: |
| 244 |
explicit AutoPoisonFreedJitCode(JS::GCContext* gcx) : gcx(gcx) {} |
| 245 |
~AutoPoisonFreedJitCode() { gcx->poisonJitCode(); } |
| 246 |
}; |
| 247 |
|
| 248 |
// Set/restore the GCContext GC use flag for the current thread. |
| 249 |
|
| 250 |
class MOZ_RAII AutoSetThreadGCUse { |
| 251 |
public: |
| 252 |
AutoSetThreadGCUse(JS::GCContext* gcx, GCUse use) |
| 253 |
: gcx(gcx), prevUse(gcx->gcUse_) { |
| 254 |
gcx->gcUse_ = use; |
| 255 |
} |
| 256 |
explicit AutoSetThreadGCUse(GCUse use) |
| 257 |
: AutoSetThreadGCUse(TlsGCContext.get(), use) {} |
| 258 |
|
| 259 |
~AutoSetThreadGCUse() { gcx->gcUse_ = prevUse; } |
| 260 |
|
| 261 |
protected: |
| 262 |
JS::GCContext* gcx; |
| 263 |
GCUse prevUse; |
| 264 |
}; |
| 265 |
|
| 266 |
template <GCUse Use> |
| 267 |
class AutoSetThreadGCUseT : public AutoSetThreadGCUse { |
| 268 |
public: |
| 269 |
explicit AutoSetThreadGCUseT(JS::GCContext* gcx) |
| 270 |
: AutoSetThreadGCUse(gcx, Use) {} |
| 271 |
AutoSetThreadGCUseT() : AutoSetThreadGCUseT(TlsGCContext.get()) {} |
| 272 |
}; |
| 273 |
|
| 274 |
using AutoSetThreadIsPerformingGC = AutoSetThreadGCUseT<GCUse::Unspecified>; |
| 275 |
using AutoSetThreadIsMarking = AutoSetThreadGCUseT<GCUse::Marking>; |
| 276 |
using AutoSetThreadIsFinalizing = AutoSetThreadGCUseT<GCUse::Finalizing>; |
| 277 |
|
| 278 |
class AutoSetThreadIsSweeping : public AutoSetThreadGCUseT<GCUse::Sweeping> { |
| 279 |
public: |
| 280 |
explicit AutoSetThreadIsSweeping(JS::GCContext* gcx, |
| 281 |
JS::Zone* sweepZone = nullptr) |
| 282 |
: AutoSetThreadGCUseT(gcx) { |
| 283 |
#ifdef DEBUG |
| 284 |
prevZone = gcx->gcSweepZone_; |
| 285 |
gcx->gcSweepZone_ = sweepZone; |
| 286 |
#endif |
| 287 |
} |
| 288 |
explicit AutoSetThreadIsSweeping(JS::Zone* sweepZone = nullptr) |
| 289 |
: AutoSetThreadIsSweeping(TlsGCContext.get(), sweepZone) {} |
| 290 |
|
| 291 |
~AutoSetThreadIsSweeping() { |
| 292 |
#ifdef DEBUG |
| 293 |
MOZ_ASSERT_IF(prevUse == GCUse::None, !prevZone); |
| 294 |
gcx->gcSweepZone_ = prevZone; |
| 295 |
#endif |
| 296 |
} |
| 297 |
|
| 298 |
private: |
| 299 |
#ifdef DEBUG |
| 300 |
JS::Zone* prevZone; |
| 301 |
#endif |
| 302 |
}; |
| 303 |
|
| 304 |
class MOZ_RAII AutoDisallowPreWriteBarrier { |
| 305 |
public: |
| 306 |
explicit AutoDisallowPreWriteBarrier(JS::GCContext* gcx) { |
| 307 |
#ifdef DEBUG |
| 308 |
gcx_ = gcx; |
| 309 |
MOZ_ASSERT(gcx->preWriteBarrierAllowed_); |
| 310 |
gcx->preWriteBarrierAllowed_ = false; |
| 311 |
#endif |
| 312 |
} |
| 313 |
~AutoDisallowPreWriteBarrier() { |
| 314 |
#ifdef DEBUG |
| 315 |
MOZ_ASSERT(!gcx_->preWriteBarrierAllowed_); |
| 316 |
gcx_->preWriteBarrierAllowed_ = true; |
| 317 |
#endif |
| 318 |
} |
| 319 |
|
| 320 |
private: |
| 321 |
#ifdef DEBUG |
| 322 |
JS::GCContext* gcx_; |
| 323 |
#endif |
| 324 |
}; |
| 325 |
|
| 326 |
#ifdef JSGC_HASH_TABLE_CHECKS |
| 327 |
void CheckHashTablesAfterMovingGC(JSRuntime* rt); |
| 328 |
void CheckHeapAfterGC(JSRuntime* rt); |
| 329 |
#endif |
| 330 |
|
| 331 |
struct MovingTracer final : public GenericTracerImpl<MovingTracer> { |
| 332 |
explicit MovingTracer(JSRuntime* rt); |
| 333 |
|
| 334 |
private: |
| 335 |
template <typename T> |
| 336 |
void onEdge(T** thingp, const char* name); |
| 337 |
friend class GenericTracerImpl<MovingTracer>; |
| 338 |
}; |
| 339 |
|
| 340 |
struct MinorSweepingTracer final |
| 341 |
: public GenericTracerImpl<MinorSweepingTracer> { |
| 342 |
explicit MinorSweepingTracer(JSRuntime* rt); |
| 343 |
|
| 344 |
private: |
| 345 |
template <typename T> |
| 346 |
void onEdge(T** thingp, const char* name); |
| 347 |
friend class GenericTracerImpl<MinorSweepingTracer>; |
| 348 |
}; |
| 349 |
|
| 350 |
class MOZ_RAII AutoUpdateMarkStackRanges { |
| 351 |
GCMarker& marker_; |
| 352 |
|
| 353 |
public: |
| 354 |
explicit AutoUpdateMarkStackRanges(GCMarker& marker) : marker_(marker) { |
| 355 |
marker_.updateRangesAtStartOfSlice(); |
| 356 |
} |
| 357 |
~AutoUpdateMarkStackRanges() { marker_.updateRangesAtEndOfSlice(); } |
| 358 |
}; |
| 359 |
|
| 360 |
extern void DelayCrossCompartmentGrayMarking(GCMarker* maybeMarker, |
| 361 |
JSObject* src); |
| 362 |
|
| 363 |
inline bool IsOOMReason(JS::GCReason reason) { |
| 364 |
return reason == JS::GCReason::LAST_DITCH || |
| 365 |
reason == JS::GCReason::MEM_PRESSURE; |
| 366 |
} |
| 367 |
|
| 368 |
void* AllocateTenuredCellInGC(JS::Zone* zone, AllocKind thingKind); |
| 369 |
|
| 370 |
void ReadProfileEnv(const char* envName, const char* helpText, bool* enableOut, |
| 371 |
bool* workersOut, mozilla::TimeDuration* thresholdOut); |
| 372 |
|
| 373 |
bool ShouldPrintProfile(JSRuntime* runtime, bool enable, bool workers, |
| 374 |
mozilla::TimeDuration threshold, |
| 375 |
mozilla::TimeDuration duration); |
| 376 |
|
| 377 |
using CharRange = mozilla::Range<const char>; |
| 378 |
using CharRangeVector = Vector<CharRange, 0, SystemAllocPolicy>; |
| 379 |
|
| 380 |
// Split a string on a given character and produce a vector of CharRanges that |
| 381 |
// reference the input string. |
| 382 |
extern bool SplitStringBy(const char* string, char delimiter, |
| 383 |
CharRangeVector* resultOut); |
| 384 |
extern bool SplitStringBy(const CharRange& string, char delimiter, |
| 385 |
CharRangeVector* resultOut); |
| 386 |
|
| 387 |
} /* namespace gc */ |
| 388 |
} /* namespace js */ |
| 389 |
|
| 390 |
#endif /* gc_GCInternals_h */ |