Debian Codegraph @

js/src/gc/Heap-inl.h

raw
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_Heap_inl_h
8 #define gc_Heap_inl_h
9
10 #include "gc/Heap.h"
11
12 #include "gc/StoreBuffer.h"
13 #include "gc/Zone.h"
14 #include "util/Poison.h"
15 #include "vm/Runtime.h"
16
17 inline void js::gc::Arena::init(GCRuntime* gc, JS::Zone* zone, AllocKind kind) {
18 MOZ_ASSERT(zone);
19 MOZ_ASSERT(IsValidAllocKind(kind));
20
21 MOZ_MAKE_MEM_UNDEFINED(this, ArenaSize);
22
23 allocKind = kind;
24 zone_ = zone;
25 next = nullptr;
26 isNewlyCreated_ = 1;
27 onDelayedMarkingList_ = 0;
28 hasDelayedBlackMarking_ = 0;
29 hasDelayedGrayMarking_ = 0;
30 nextDelayedMarkingArena_ = 0;
31 if (zone_->isAtomsZone()) {
32 atomBitmapStart() = gc->atomMarking.allocateIndex(gc);
33 } else {
34 bufferedCells() = &ArenaCellSet::Empty;
35 }
36
37 setAsFullyUnused(); // Initializes firstFreeSpan.
38
39 #ifdef DEBUG
40 checkNoMarkedCells();
41 #endif
42 }
43
44 inline void js::gc::Arena::freeAtomMarkingBitmapIndex(GCRuntime* gc,
45 const AutoLockGC& lock) {
46 MOZ_ASSERT(zone_->isAtomsZone());
47 gc->atomMarking.freeIndex(atomBitmapStart(), lock);
48 #ifdef DEBUG
49 atomBitmapStart() = 0; // Also zeroed by write to bufferedCells_ in release.
50 #endif
51 }
52
53 inline void js::gc::Arena::release() {
54 MOZ_ASSERT(allocated());
55
56 // Clients should call freeAtomMarkingBitmapIndex() if necessary.
57 MOZ_ASSERT_IF(zone_->isAtomsZone(), atomBitmapStart_ == 0);
58
59 // Poison zone pointer to highlight UAF on released arenas in crash data.
60 AlwaysPoison(&zone_, JS_FREED_ARENA_PATTERN, sizeof(zone_),
61 MemCheckKind::MakeNoAccess);
62
63 firstFreeSpan.initAsEmpty();
64 allocKind = AllocKind::LIMIT;
65 onDelayedMarkingList_ = 0;
66 hasDelayedBlackMarking_ = 0;
67 hasDelayedGrayMarking_ = 0;
68 nextDelayedMarkingArena_ = 0;
69 bufferedCells_ = nullptr;
70
71 MOZ_ASSERT(!allocated());
72 }
73
74 inline js::gc::ArenaCellSet*& js::gc::Arena::bufferedCells() {
75 MOZ_ASSERT(zone_ && !zone_->isAtomsZone());
76 return bufferedCells_;
77 }
78
79 inline size_t& js::gc::Arena::atomBitmapStart() {
80 MOZ_ASSERT(zone_ && zone_->isAtomsZone());
81 return atomBitmapStart_;
82 }
83
84 // Mark bitmap API:
85
86 // Unless noted otherwise, the following methods that update the mark bits are
87 // not thread safe and must not be called in parallel with each other.
88 //
89 // They use separate read and write operations to avoid an unnecessarily strict
90 // atomic update on the marking bitmap.
91 //
92 // They may be called in parallel with read operations on the mark bitmap where
93 // there is no required ordering between the operations. This happens when gray
94 // unmarking occurs in parallel with background sweeping.
95
96 // The return value indicates if the cell went from unmarked to marked.
97 template <size_t BytesPerMarkBit, size_t FirstThingOffset>
98 MOZ_ALWAYS_INLINE bool
99 js::gc::MarkBitmap<BytesPerMarkBit, FirstThingOffset>::markIfUnmarked(
100 const void* cell, MarkColor color) {
101 MarkBitmapWord* word;
102 uintptr_t mask;
103 getMarkWordAndMask(cell, ColorBit::BlackBit, &word, &mask);
104 if (*word & mask) {
105 return false;
106 }
107 if (color == MarkColor::Black) {
108 uintptr_t bits = *word;
109 *word = bits | mask;
110 } else {
111 // We use getMarkWordAndMask to recalculate both mask and word as doing just
112 // mask << color may overflow the mask.
113 getMarkWordAndMask(cell, ColorBit::GrayOrBlackBit, &word, &mask);
114 if (*word & mask) {
115 return false;
116 }
117 uintptr_t bits = *word;
118 *word = bits | mask;
119 }
120 return true;
121 }
122
123 // This version of the method is safe in the face of concurrent writes to the
124 // mark bitmap but if two threads attempt to mark the same cell at the same time
125 // then both calls can succeed and return true.
126 //
127 // This method is used for parallel marking where the extra synchronization
128 // required to avoid this results in worse performance overall.
129 template <size_t BytesPerMarkBit, size_t FirstThingOffset>
130 MOZ_ALWAYS_INLINE bool
131 js::gc::MarkBitmap<BytesPerMarkBit, FirstThingOffset>::markIfUnmarkedThreadSafe(
132 const void* cell, MarkColor color) {
133 MarkBitmapWord* word;
134 uintptr_t mask;
135 getMarkWordAndMask(cell, ColorBit::BlackBit, &word, &mask);
136 if (*word & mask) {
137 return false;
138 }
139 if (color == MarkColor::Black) {
140 *word |= mask;
141 } else {
142 // We use getMarkWordAndMask to recalculate both mask and word as doing just
143 // mask << color may overflow the mask.
144 getMarkWordAndMask(cell, ColorBit::GrayOrBlackBit, &word, &mask);
145 if (*word & mask) {
146 return false;
147 }
148 *word |= mask;
149 }
150 return true;
151 }
152
153 template <size_t BytesPerMarkBit, size_t FirstThingOffset>
154 MOZ_ALWAYS_INLINE void
155 js::gc::MarkBitmap<BytesPerMarkBit, FirstThingOffset>::markBlack(
156 const void* cell) {
157 MarkBitmapWord* word;
158 uintptr_t mask;
159 getMarkWordAndMask(cell, ColorBit::BlackBit, &word, &mask);
160 uintptr_t bits = *word;
161 *word = bits | mask;
162 }
163
164 template <size_t BytesPerMarkBit, size_t FirstThingOffset>
165 MOZ_ALWAYS_INLINE void
166 js::gc::MarkBitmap<BytesPerMarkBit, FirstThingOffset>::markBlackAtomic(
167 const void* cell) {
168 MarkBitmapWord* word;
169 uintptr_t mask;
170 getMarkWordAndMask(cell, ColorBit::BlackBit, &word, &mask);
171 *word |= mask;
172 }
173
174 template <size_t BytesPerMarkBit, size_t FirstThingOffset>
175 MOZ_ALWAYS_INLINE void
176 js::gc::MarkBitmap<BytesPerMarkBit, FirstThingOffset>::copyMarkBit(
177 TenuredCell* dst, const TenuredCell* src, ColorBit colorBit) {
178 ArenaChunkBase* srcChunk = detail::GetCellChunkBase(src);
179 MarkBitmapWord* srcWord;
180 uintptr_t srcMask;
181 srcChunk->markBits.getMarkWordAndMask(src, colorBit, &srcWord, &srcMask);
182
183 MarkBitmapWord* dstWord;
184 uintptr_t dstMask;
185 getMarkWordAndMask(dst, colorBit, &dstWord, &dstMask);
186
187 uintptr_t bits = *dstWord;
188 bits &= ~dstMask;
189 if (*srcWord & srcMask) {
190 bits |= dstMask;
191 }
192 *dstWord = bits;
193 }
194
195 template <size_t BytesPerMarkBit, size_t FirstThingOffset>
196 MOZ_ALWAYS_INLINE void
197 js::gc::MarkBitmap<BytesPerMarkBit, FirstThingOffset>::unmark(
198 const void* cell) {
199 unmarkOneBit(cell, ColorBit::BlackBit);
200 unmarkOneBit(cell, ColorBit::GrayOrBlackBit);
201 }
202
203 template <size_t BytesPerMarkBit, size_t FirstThingOffset>
204 MOZ_ALWAYS_INLINE void
205 js::gc::MarkBitmap<BytesPerMarkBit, FirstThingOffset>::unmarkOneBit(
206 const void* cell, ColorBit colorBit) {
207 MarkBitmapWord* word;
208 uintptr_t mask;
209 uintptr_t bits;
210 getMarkWordAndMask(cell, colorBit, &word, &mask);
211 bits = *word;
212 *word = bits & ~mask;
213 }
214
215 template <size_t BytesPerMarkBit, size_t FirstThingOffset>
216 inline js::gc::MarkBitmapWord*
217 js::gc::MarkBitmap<BytesPerMarkBit, FirstThingOffset>::arenaBits(Arena* arena) {
218 static_assert(
219 ArenaBitmapBits == ArenaBitmapWords * JS_BITS_PER_WORD,
220 "We assume that the part of the bitmap corresponding to the arena "
221 "has the exact number of words so we do not need to deal with a word "
222 "that covers bits from two arenas.");
223
224 MarkBitmapWord* word;
225 uintptr_t unused;
226 getMarkWordAndMask(arena, ColorBit::BlackBit, &word, &unused);
227 return word;
228 }
229
230 template <size_t BytesPerMarkBit, size_t FirstThingOffset>
231 void js::gc::MarkBitmap<BytesPerMarkBit, FirstThingOffset>::copyFrom(
232 const MarkBitmap& other) {
233 for (size_t i = 0; i < WordCount; i++) {
234 bitmap[i] = uintptr_t(other.bitmap[i]);
235 }
236 }
237
238 template <size_t BytesPerMarkBit, size_t FirstThingOffset>
239 void js::gc::MarkBitmap<BytesPerMarkBit, FirstThingOffset>::clear() {
240 for (size_t i = 0; i < WordCount; i++) {
241 bitmap[i] = 0;
242 }
243 }
244
245 bool js::gc::TenuredCell::markIfUnmarked(MarkColor color /* = Black */) const {
246 return chunk()->markBits.markIfUnmarked(this, color);
247 }
248
249 bool js::gc::TenuredCell::markIfUnmarkedThreadSafe(MarkColor color) const {
250 return chunk()->markBits.markIfUnmarkedThreadSafe(this, color);
251 }
252
253 void js::gc::TenuredCell::markBlack() const {
254 chunk()->markBits.markBlack(this);
255 }
256
257 void js::gc::TenuredCell::markBlackAtomic() const {
258 chunk()->markBits.markBlackAtomic(this);
259 }
260
261 void js::gc::TenuredCell::copyMarkBitsFrom(const TenuredCell* src) {
262 ChunkMarkBitmap& markBits = chunk()->markBits;
263 markBits.copyMarkBit(this, src, ColorBit::BlackBit);
264 markBits.copyMarkBit(this, src, ColorBit::GrayOrBlackBit);
265 }
266
267 void js::gc::TenuredCell::unmark() { chunk()->markBits.unmark(this); }
268
269 #endif