| 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 |
#ifndef gc_Marking_inl_h |
| 8 |
#define gc_Marking_inl_h |
| 9 |
|
| 10 |
#include "gc/Marking.h" |
| 11 |
|
| 12 |
#include <type_traits> |
| 13 |
|
| 14 |
#include "gc/RelocationOverlay.h" |
| 15 |
#include "gc/Zone.h" |
| 16 |
#include "js/Id.h" |
| 17 |
#include "js/Value.h" |
| 18 |
#include "vm/StringType.h" |
| 19 |
#include "vm/TaggedProto.h" |
| 20 |
#include "wasm/WasmAnyRef.h" |
| 21 |
|
| 22 |
#include "gc/Nursery-inl.h" |
| 23 |
|
| 24 |
namespace js { |
| 25 |
namespace gc { |
| 26 |
|
| 27 |
// An abstraction to re-wrap any kind of typed pointer back to the tagged |
| 28 |
// pointer it came from with |TaggedPtr<TargetType>::wrap(sourcePtr)|. |
| 29 |
template <typename T> |
| 30 |
struct TaggedPtr {}; |
| 31 |
|
| 32 |
template <> |
| 33 |
struct TaggedPtr<JS::Value> { |
| 34 |
static JS::Value wrap(JSObject* obj) { |
| 35 |
if (!obj) { |
| 36 |
return JS::NullValue(); |
| 37 |
} |
| 38 |
return JS::ObjectValue(*obj); |
| 39 |
} |
| 40 |
static JS::Value wrap(JSString* str) { return JS::StringValue(str); } |
| 41 |
static JS::Value wrap(JS::Symbol* sym) { return JS::SymbolValue(sym); } |
| 42 |
static JS::Value wrap(JS::BigInt* bi) { return JS::BigIntValue(bi); } |
| 43 |
template <typename T> |
| 44 |
static JS::Value wrap(T* priv) { |
| 45 |
static_assert(std::is_base_of_v<Cell, T>, |
| 46 |
"Type must be a GC thing derived from js::gc::Cell"); |
| 47 |
return JS::PrivateGCThingValue(priv); |
| 48 |
} |
| 49 |
static JS::Value empty() { return JS::UndefinedValue(); } |
| 50 |
}; |
| 51 |
|
| 52 |
template <> |
| 53 |
struct TaggedPtr<jsid> { |
| 54 |
static jsid wrap(JSString* str) { return JS::PropertyKey::NonIntAtom(str); } |
| 55 |
static jsid wrap(JS::Symbol* sym) { return PropertyKey::Symbol(sym); } |
| 56 |
static jsid empty() { return JS::PropertyKey::Void(); } |
| 57 |
}; |
| 58 |
|
| 59 |
template <> |
| 60 |
struct TaggedPtr<TaggedProto> { |
| 61 |
static TaggedProto wrap(JSObject* obj) { return TaggedProto(obj); } |
| 62 |
static TaggedProto empty() { return TaggedProto(); } |
| 63 |
}; |
| 64 |
|
| 65 |
template <> |
| 66 |
struct TaggedPtr<wasm::AnyRef> { |
| 67 |
static wasm::AnyRef wrap(JSObject* obj) { |
| 68 |
return wasm::AnyRef::fromJSObjectOrNull(obj); |
| 69 |
} |
| 70 |
static wasm::AnyRef wrap(JSString* str) { |
| 71 |
return wasm::AnyRef::fromJSString(str); |
| 72 |
} |
| 73 |
static wasm::AnyRef empty() { return wasm::AnyRef(); } |
| 74 |
}; |
| 75 |
|
| 76 |
template <typename T> |
| 77 |
struct MightBeForwarded { |
| 78 |
static_assert(std::is_base_of_v<Cell, T>); |
| 79 |
static_assert(!std::is_same_v<Cell, T> && !std::is_same_v<TenuredCell, T>); |
| 80 |
|
| 81 |
#define CAN_FORWARD_KIND_OR(_1, _2, Type, _3, _4, _5, canCompact) \ |
| 82 |
std::is_base_of_v<Type, T> ? canCompact: |
| 83 |
|
| 84 |
// FOR_EACH_ALLOCKIND doesn't cover every possible type: make sure |
| 85 |
// to default to `true` for unknown types. |
| 86 |
static constexpr bool value = FOR_EACH_ALLOCKIND(CAN_FORWARD_KIND_OR) true; |
| 87 |
#undef CAN_FORWARD_KIND_OR |
| 88 |
}; |
| 89 |
|
| 90 |
template <typename T> |
| 91 |
inline bool IsForwarded(const T* t) { |
| 92 |
if constexpr (!MightBeForwarded<T>::value) { |
| 93 |
MOZ_ASSERT(!t->isForwarded()); |
| 94 |
return false; |
| 95 |
} |
| 96 |
|
| 97 |
return t->isForwarded(); |
| 98 |
} |
| 99 |
|
| 100 |
template <> |
| 101 |
inline bool IsForwarded<Cell>(const Cell* t) { |
| 102 |
return t->isForwarded(); |
| 103 |
} |
| 104 |
|
| 105 |
template <typename T> |
| 106 |
inline T* Forwarded(const T* t) { |
| 107 |
const RelocationOverlay* overlay = RelocationOverlay::fromCell(t); |
| 108 |
MOZ_ASSERT(overlay->isForwarded()); |
| 109 |
return reinterpret_cast<T*>(overlay->forwardingAddress()); |
| 110 |
} |
| 111 |
|
| 112 |
template <typename T> |
| 113 |
inline T MaybeForwarded(T t) { |
| 114 |
if (IsForwarded(t)) { |
| 115 |
t = Forwarded(t); |
| 116 |
} |
| 117 |
MOZ_ASSERT(!IsForwarded(t)); |
| 118 |
return t; |
| 119 |
} |
| 120 |
|
| 121 |
inline const JSClass* MaybeForwardedObjectClass(const JSObject* obj) { |
| 122 |
Shape* shape = MaybeForwarded(obj->shapeMaybeForwarded()); |
| 123 |
BaseShape* baseShape = MaybeForwarded(shape->base()); |
| 124 |
return baseShape->clasp(); |
| 125 |
} |
| 126 |
|
| 127 |
template <typename T> |
| 128 |
inline bool MaybeForwardedObjectIs(const JSObject* obj) { |
| 129 |
MOZ_ASSERT(!obj->isForwarded()); |
| 130 |
return MaybeForwardedObjectClass(obj) == &T::class_; |
| 131 |
} |
| 132 |
|
| 133 |
template <typename T> |
| 134 |
inline T& MaybeForwardedObjectAs(JSObject* obj) { |
| 135 |
MOZ_ASSERT(MaybeForwardedObjectIs<T>(obj)); |
| 136 |
return *static_cast<T*>(obj); |
| 137 |
} |
| 138 |
|
| 139 |
inline RelocationOverlay::RelocationOverlay(Cell* dst) { |
| 140 |
MOZ_ASSERT(dst->flags() == 0); |
| 141 |
uintptr_t ptr = uintptr_t(dst); |
| 142 |
header_.setForwardingAddress(ptr); |
| 143 |
} |
| 144 |
|
| 145 |
/* static */ |
| 146 |
inline RelocationOverlay* RelocationOverlay::forwardCell(Cell* src, Cell* dst) { |
| 147 |
MOZ_ASSERT(!src->isForwarded()); |
| 148 |
MOZ_ASSERT(!dst->isForwarded()); |
| 149 |
return new (src) RelocationOverlay(dst); |
| 150 |
} |
| 151 |
|
| 152 |
inline bool IsAboutToBeFinalizedDuringMinorSweep(Cell** cellp) { |
| 153 |
MOZ_ASSERT(JS::RuntimeHeapIsMinorCollecting()); |
| 154 |
|
| 155 |
if ((*cellp)->isTenured()) { |
| 156 |
return false; |
| 157 |
} |
| 158 |
|
| 159 |
return !Nursery::getForwardedPointer(cellp); |
| 160 |
} |
| 161 |
|
| 162 |
// Special case pre-write barrier for strings used during rope flattening. This |
| 163 |
// avoids eager marking of ropes which does not immediately mark the cells if we |
| 164 |
// hit OOM. This does not traverse ropes and is instead called on every node in |
| 165 |
// a rope during flattening. |
| 166 |
inline void PreWriteBarrierDuringFlattening(JSString* str) { |
| 167 |
MOZ_ASSERT(str); |
| 168 |
MOZ_ASSERT(!JS::RuntimeHeapIsMajorCollecting()); |
| 169 |
|
| 170 |
if (IsInsideNursery(str)) { |
| 171 |
return; |
| 172 |
} |
| 173 |
|
| 174 |
auto* cell = reinterpret_cast<TenuredCell*>(str); |
| 175 |
JS::shadow::Zone* zone = cell->shadowZoneFromAnyThread(); |
| 176 |
if (!zone->needsIncrementalBarrier()) { |
| 177 |
return; |
| 178 |
} |
| 179 |
|
| 180 |
MOZ_ASSERT(!str->isPermanentAndMayBeShared()); |
| 181 |
MOZ_ASSERT(CurrentThreadCanAccessRuntime(zone->runtimeFromAnyThread())); |
| 182 |
PerformIncrementalBarrierDuringFlattening(str); |
| 183 |
} |
| 184 |
|
| 185 |
#ifdef JSGC_HASH_TABLE_CHECKS |
| 186 |
|
| 187 |
// Moving GC things whose pointers are used in hash table keys has the potential |
| 188 |
// to break hash tables in subtle and terrifying ways. For example, a key might |
| 189 |
// be reported as not present but iterating the table could still return it. |
| 190 |
// |
| 191 |
// Check that a table is correct following a moving GC, ensuring that nothing is |
| 192 |
// present in the table that points into the nursery or that has not been moved, |
| 193 |
// and that the hash table entries are discoverable. |
| 194 |
// |
| 195 |
// |checkEntryAndGetLookup| should check any GC thing pointers in the entry are |
| 196 |
// valid and return the lookup required to get this entry from the table. |
| 197 |
|
| 198 |
template <typename Table, typename Range, typename Lookup> |
| 199 |
void CheckTableEntryAfterMovingGC(const Table& table, const Range& r, |
| 200 |
const Lookup& lookup) { |
| 201 |
auto ptr = table.lookup(lookup); |
| 202 |
MOZ_RELEASE_ASSERT(ptr.found() && &*ptr == &r.front()); |
| 203 |
} |
| 204 |
|
| 205 |
template <typename Table, typename F> |
| 206 |
void CheckTableAfterMovingGC(const Table& table, F&& checkEntryAndGetLookup) { |
| 207 |
for (auto r = table.all(); !r.empty(); r.popFront()) { |
| 208 |
auto lookup = checkEntryAndGetLookup(r.front()); |
| 209 |
CheckTableEntryAfterMovingGC(table, r, lookup); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
template <typename T> |
| 214 |
inline bool IsGCThingValidAfterMovingGC(T* t) { |
| 215 |
if (!t->isTenured()) { |
| 216 |
return false; |
| 217 |
} |
| 218 |
|
| 219 |
TenuredCell* cell = &t->asTenured(); |
| 220 |
return cell->arena()->allocated() && !cell->isForwarded(); |
| 221 |
} |
| 222 |
|
| 223 |
template <typename T> |
| 224 |
inline void CheckGCThingAfterMovingGC(T* t) { |
| 225 |
if (t) { |
| 226 |
MOZ_RELEASE_ASSERT(IsGCThingValidAfterMovingGC(t)); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
template <typename T> |
| 231 |
inline void CheckGCThingAfterMovingGC(T* t, JS::Zone* expectedZone) { |
| 232 |
if (t) { |
| 233 |
MOZ_RELEASE_ASSERT(IsGCThingValidAfterMovingGC(t)); |
| 234 |
JS::Zone* zone = t->zoneFromAnyThread(); |
| 235 |
MOZ_RELEASE_ASSERT(zone == expectedZone || zone->isAtomsZone()); |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
template <typename T> |
| 240 |
inline void CheckGCThingAfterMovingGC(const WeakHeapPtr<T*>& t, |
| 241 |
JS::Zone* expectedZone) { |
| 242 |
CheckGCThingAfterMovingGC(t.unbarrieredGet(), expectedZone); |
| 243 |
} |
| 244 |
|
| 245 |
inline void CheckProtoAfterMovingGC(const TaggedProto& proto, JS::Zone* zone) { |
| 246 |
if (proto.isObject()) { |
| 247 |
CheckGCThingAfterMovingGC(proto.toObject(), zone); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
#endif // JSGC_HASH_TABLE_CHECKS |
| 252 |
|
| 253 |
} /* namespace gc */ |
| 254 |
} /* namespace js */ |
| 255 |
|
| 256 |
#endif // gc_Marking_inl_h |