Debian Codegraph @

js/src/jsapi-tests/testGCHeapBarriers.cpp

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 */
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7
8 #include "mozilla/Maybe.h"
9 #include "mozilla/UniquePtr.h"
10
11 #include "gc/AllocKind.h"
12 #include "gc/Cell.h"
13 #include "gc/GCInternals.h"
14 #include "gc/GCRuntime.h"
15 #include "js/ArrayBuffer.h" // JS::NewArrayBuffer
16 #include "js/experimental/TypedData.h"
17 #include "js/PropertyAndElement.h" // JS_DefineProperty, JS_GetProperty
18 #include "js/RootingAPI.h"
19 #include "jsapi-tests/tests.h"
20 #include "vm/Runtime.h"
21 #include "vm/TypedArrayObject.h"
22
23 #include "gc/Heap-inl.h"
24 #include "vm/JSContext-inl.h"
25
26 using namespace js;
27
28 static js::gc::CellColor GetColor(JSObject* obj) { return obj->color(); }
29 static js::gc::CellColor GetColor(const JS::ArrayBufferOrView& view) {
30 return view.asObjectUnbarriered()->color();
31 }
32
33 static MOZ_MAYBE_UNUSED bool IsInsideNursery(gc::Cell* cell) {
34 return !cell->isTenured();
35 }
36 static MOZ_MAYBE_UNUSED bool IsInsideNursery(
37 const JS::ArrayBufferOrView& view) {
38 return IsInsideNursery(view.asObjectUnbarriered());
39 }
40
41 // A heap-allocated structure containing one of our barriered pointer wrappers
42 // to test.
43 template <typename W, typename T>
44 struct TestStruct {
45 W wrapper;
46
47 void trace(JSTracer* trc) {
48 TraceNullableEdge(trc, &wrapper, "TestStruct::wrapper");
49 }
50
51 TestStruct() {}
52 explicit TestStruct(T init) : wrapper(init) {}
53 };
54
55 template <typename T>
56 static T CreateNurseryGCThing(JSContext* cx) = delete;
57
58 template <>
59 JSObject* CreateNurseryGCThing(JSContext* cx) {
60 JS::RootedObject obj(cx, JS_NewPlainObject(cx));
61 if (!obj) {
62 return nullptr;
63 }
64 JS_DefineProperty(cx, obj, "x", 42, 0);
65 MOZ_ASSERT(IsInsideNursery(obj));
66 return obj;
67 }
68
69 template <>
70 JSFunction* CreateNurseryGCThing(JSContext* cx) {
71 /*
72 * We don't actually use the function as a function, so here we cheat and
73 * cast a JSObject.
74 */
75 return static_cast<JSFunction*>(CreateNurseryGCThing<JSObject*>(cx));
76 }
77
78 template <>
79 JS::Uint8Array CreateNurseryGCThing(JSContext* cx) {
80 JS::Rooted<JS::Uint8Array> arr(cx, JS::Uint8Array::create(cx, 100));
81 JS::RootedObject obj(cx, arr.asObject());
82 JS_DefineProperty(cx, obj, "x", 42, 0);
83 MOZ_ASSERT(IsInsideNursery(obj));
84 return arr;
85 }
86
87 template <typename T>
88 static T CreateTenuredGCThing(JSContext* cx) = delete;
89
90 template <>
91 JSObject* CreateTenuredGCThing(JSContext* cx) {
92 // Use ArrayBuffers because they have finalizers, which allows using them in
93 // TenuredHeap<> without awkward conversations about nursery allocatability.
94 // Note that at some point ArrayBuffers might become nursery allocated at
95 // which point this test will have to change.
96 JSObject* obj = JS::NewArrayBuffer(cx, 20);
97 MOZ_ASSERT(!IsInsideNursery(obj));
98 MOZ_ASSERT(obj->getClass()->hasFinalize() &&
99 !(obj->getClass()->flags & JSCLASS_SKIP_NURSERY_FINALIZE));
100 return obj;
101 }
102
103 template <>
104 JS::ArrayBuffer CreateTenuredGCThing(JSContext* cx) {
105 return JS::ArrayBuffer::fromObject(CreateTenuredGCThing<JSObject*>(cx));
106 }
107
108 template <>
109 JS::Uint8Array CreateTenuredGCThing(JSContext* cx) {
110 // Use internal APIs that lets us specify the InitialHeap so we can ensure
111 // that this is tenured.
112 JSObject* obj = js::NewUint8ArrayWithLength(cx, 100, gc::Heap::Tenured);
113 MOZ_ASSERT(!IsInsideNursery(obj));
114 return JS::Uint8Array::fromObject(obj);
115 }
116
117 template <typename T>
118 void* CreateHiddenTenuredGCThing(JSContext* cx) {
119 return CreateTenuredGCThing<T>(cx);
120 }
121
122 template <>
123 void* CreateHiddenTenuredGCThing<JS::ArrayBuffer>(JSContext* cx) {
124 return CreateTenuredGCThing<JS::ArrayBuffer>(cx).asObjectUnbarriered();
125 }
126
127 template <>
128 void* CreateHiddenTenuredGCThing<JS::Uint8Array>(JSContext* cx) {
129 return CreateTenuredGCThing<JS::Uint8Array>(cx).asObjectUnbarriered();
130 }
131
132 static uintptr_t UnbarrieredCastToInt(gc::Cell* cell) {
133 return reinterpret_cast<uintptr_t>(cell);
134 }
135 static uintptr_t UnbarrieredCastToInt(const JS::ArrayBufferOrView& view) {
136 return UnbarrieredCastToInt(view.asObjectUnbarriered());
137 }
138
139 template <typename T>
140 T RecoverHiddenGCThing(void* ptr) {
141 return reinterpret_cast<T>(ptr);
142 }
143
144 template <>
145 JS::ArrayBuffer RecoverHiddenGCThing(void* ptr) {
146 return JS::ArrayBuffer::fromObject(RecoverHiddenGCThing<JSObject*>(ptr));
147 }
148
149 template <>
150 JS::Uint8Array RecoverHiddenGCThing(void* ptr) {
151 return JS::Uint8Array::fromObject(RecoverHiddenGCThing<JSObject*>(ptr));
152 }
153
154 static void MakeGray(JSObject* obj) {
155 gc::TenuredCell* cell = &obj->asTenured();
156 cell->unmark();
157 cell->markIfUnmarked(gc::MarkColor::Gray);
158 MOZ_ASSERT(obj->isMarkedGray());
159 }
160
161 static void MakeGray(const JS::ArrayBufferOrView& view) {
162 MakeGray(view.asObjectUnbarriered());
163 }
164
165 // Test post-barrier implementation on wrapper types. The following wrapper
166 // types have post barriers:
167 // - JS::Heap
168 // - GCPtr
169 // - HeapPtr
170 // - WeakHeapPtr
171 BEGIN_TEST(testGCHeapPostBarriers) {
172 AutoLeaveZeal nozeal(cx);
173 AutoGCParameter disableSemispace(cx, JSGC_SEMISPACE_NURSERY_ENABLED, 0);
174
175 /* Sanity check - objects start in the nursery and then become tenured. */
176 JS_GC(cx);
177 JS::RootedObject obj(cx, CreateNurseryGCThing<JSObject*>(cx));
178 CHECK(IsInsideNursery(obj.get()));
179 JS_GC(cx);
180 CHECK(!IsInsideNursery(obj.get()));
181 JS::RootedObject tenuredObject(cx, obj);
182
183 /* JSObject and JSFunction objects are nursery allocated. */
184 CHECK(TestHeapPostBarriersForType<JSObject*>());
185 CHECK(TestHeapPostBarriersForType<JSFunction*>());
186 CHECK(TestHeapPostBarriersForType<JS::Uint8Array>());
187 // Bug 1599378: Add string tests.
188
189 return true;
190 }
191
192 [[nodiscard]] bool TestCanAccessObject(JSObject* obj) {
193 JS::RootedObject rootedObj(cx, obj);
194 JS::RootedValue value(cx);
195 CHECK(JS_GetProperty(cx, rootedObj, "x", &value));
196 CHECK(value.isInt32());
197 CHECK(value.toInt32() == 42);
198 return true;
199 }
200 [[nodiscard]] bool TestCanAccessObject(const JS::ArrayBufferOrView& view) {
201 return TestCanAccessObject(view.asObject());
202 }
203
204 template <typename T>
205 [[nodiscard]] bool TestHeapPostBarriersForType() {
206 CHECK((TestHeapPostBarriersForWrapper<js::GCPtr, T>()));
207 CHECK((TestHeapPostBarriersForMovableWrapper<JS::Heap, T>()));
208 CHECK((TestHeapPostBarriersForMovableWrapper<js::HeapPtr, T>()));
209 CHECK((TestHeapPostBarriersForMovableWrapper<js::WeakHeapPtr, T>()));
210 return true;
211 }
212
213 template <template <typename> class W, typename T>
214 [[nodiscard]] bool TestHeapPostBarriersForMovableWrapper() {
215 CHECK((TestHeapPostBarriersForWrapper<W, T>()));
216 CHECK((TestHeapPostBarrierMoveConstruction<W<T>, T>()));
217 CHECK((TestHeapPostBarrierMoveAssignment<W<T>, T>()));
218 return true;
219 }
220
221 template <template <typename> class W, typename T>
222 [[nodiscard]] bool TestHeapPostBarriersForWrapper() {
223 CHECK((TestHeapPostBarrierConstruction<W<T>, T>()));
224 CHECK((TestHeapPostBarrierConstruction<const W<T>, T>()));
225 CHECK((TestHeapPostBarrierUpdate<W<T>, T>()));
226 if constexpr (!std::is_same_v<W<T>, GCPtr<T>>) {
227 // It is not allowed to delete heap memory containing GCPtrs on
228 // initialization failure like this and doing so will cause an assertion to
229 // fail in the GCPtr destructor (although we disable this in some places in
230 // this test).
231 CHECK((TestHeapPostBarrierInitFailure<W<T>, T>()));
232 CHECK((TestHeapPostBarrierInitFailure<const W<T>, T>()));
233 }
234 return true;
235 }
236
237 template <typename W, typename T>
238 [[nodiscard]] bool TestHeapPostBarrierConstruction() {
239 T initialObj = CreateNurseryGCThing<T>(cx);
240 CHECK(initialObj);
241 CHECK(IsInsideNursery(initialObj));
242 uintptr_t initialObjAsInt = UnbarrieredCastToInt(initialObj);
243
244 {
245 // We don't root our structure because that would end up tracing it on minor
246 // GC and we're testing that heap post barrier works for things that aren't
247 // roots.
248 JS::AutoSuppressGCAnalysis noAnalysis(cx);
249
250 auto* testStruct = js_new<TestStruct<W, T>>(initialObj);
251 CHECK(testStruct);
252
253 auto& wrapper = testStruct->wrapper;
254 CHECK(wrapper == initialObj);
255
256 cx->minorGC(JS::GCReason::API);
257
258 CHECK(UnbarrieredCastToInt(wrapper.get()) != initialObjAsInt);
259 CHECK(!IsInsideNursery(wrapper.get()));
260 CHECK(TestCanAccessObject(wrapper.get()));
261
262 // Disable the check that GCPtrs are only destroyed by the GC. What happens
263 // on destruction isn't relevant to the test.
264 gc::AutoSetThreadIsFinalizing threadIsFinalizing;
265
266 js_delete(testStruct);
267 }
268
269 cx->minorGC(JS::GCReason::API);
270
271 return true;
272 }
273
274 template <typename W, typename T>
275 [[nodiscard]] bool TestHeapPostBarrierUpdate() {
276 // Normal case - allocate a heap object, write a nursery pointer into it and
277 // check that it gets updated on minor GC.
278
279 T initialObj = CreateNurseryGCThing<T>(cx);
280 CHECK(initialObj);
281 CHECK(IsInsideNursery(initialObj));
282 uintptr_t initialObjAsInt = UnbarrieredCastToInt(initialObj);
283
284 {
285 // We don't root our structure because that would end up tracing it on minor
286 // GC and we're testing that heap post barrier works for things that aren't
287 // roots.
288 JS::AutoSuppressGCAnalysis noAnalysis(cx);
289
290 auto* testStruct = js_new<TestStruct<W, T>>();
291 CHECK(testStruct);
292
293 auto& wrapper = testStruct->wrapper;
294 CHECK(!wrapper.get());
295
296 wrapper = initialObj;
297 CHECK(wrapper == initialObj);
298
299 cx->minorGC(JS::GCReason::API);
300
301 CHECK(UnbarrieredCastToInt(wrapper.get()) != initialObjAsInt);
302 CHECK(!IsInsideNursery(wrapper.get()));
303 CHECK(TestCanAccessObject(wrapper.get()));
304
305 // Disable the check that GCPtrs are only destroyed by the GC. What happens
306 // on destruction isn't relevant to the test.
307 gc::AutoSetThreadIsFinalizing threadIsFinalizing;
308
309 js_delete(testStruct);
310 }
311
312 cx->minorGC(JS::GCReason::API);
313
314 return true;
315 }
316
317 template <typename W, typename T>
318 [[nodiscard]] bool TestHeapPostBarrierInitFailure() {
319 // Failure case - allocate a heap object, write a nursery pointer into it
320 // and fail to complete initialization.
321
322 T initialObj = CreateNurseryGCThing<T>(cx);
323 CHECK(initialObj);
324 CHECK(IsInsideNursery(initialObj));
325
326 {
327 // We don't root our structure because that would end up tracing it on minor
328 // GC and we're testing that heap post barrier works for things that aren't
329 // roots.
330 JS::AutoSuppressGCAnalysis noAnalysis(cx);
331
332 auto testStruct = cx->make_unique<TestStruct<W, T>>(initialObj);
333 CHECK(testStruct);
334
335 auto& wrapper = testStruct->wrapper;
336 CHECK(wrapper == initialObj);
337
338 // testStruct deleted here, as if we left this block due to an error.
339 }
340
341 cx->minorGC(JS::GCReason::API);
342
343 return true;
344 }
345
346 template <typename W, typename T>
347 [[nodiscard]] bool TestHeapPostBarrierMoveConstruction() {
348 T initialObj = CreateNurseryGCThing<T>(cx);
349 CHECK(initialObj);
350 CHECK(IsInsideNursery(initialObj));
351 uintptr_t initialObjAsInt = UnbarrieredCastToInt(initialObj);
352
353 {
354 // We don't root our structure because that would end up tracing it on minor
355 // GC and we're testing that heap post barrier works for things that aren't
356 // roots.
357 JS::AutoSuppressGCAnalysis noAnalysis(cx);
358
359 W wrapper1(initialObj);
360 CHECK(wrapper1 == initialObj);
361
362 W wrapper2(std::move(wrapper1));
363 CHECK(wrapper2 == initialObj);
364
365 cx->minorGC(JS::GCReason::API);
366
367 CHECK(UnbarrieredCastToInt(wrapper1.get()) != initialObjAsInt);
368 CHECK(UnbarrieredCastToInt(wrapper2.get()) != initialObjAsInt);
369 CHECK(!IsInsideNursery(wrapper2.get()));
370 CHECK(TestCanAccessObject(wrapper2.get()));
371 }
372
373 cx->minorGC(JS::GCReason::API);
374
375 return true;
376 }
377
378 template <typename W, typename T>
379 [[nodiscard]] bool TestHeapPostBarrierMoveAssignment() {
380 T initialObj = CreateNurseryGCThing<T>(cx);
381 CHECK(initialObj);
382 CHECK(IsInsideNursery(initialObj));
383 uintptr_t initialObjAsInt = UnbarrieredCastToInt(initialObj);
384
385 {
386 // We don't root our structure because that would end up tracing it on minor
387 // GC and we're testing that heap post barrier works for things that aren't
388 // roots.
389 JS::AutoSuppressGCAnalysis noAnalysis(cx);
390
391 W wrapper1(initialObj);
392 CHECK(wrapper1 == initialObj);
393
394 W wrapper2;
395 wrapper2 = std::move(wrapper1);
396 CHECK(wrapper2 == initialObj);
397
398 cx->minorGC(JS::GCReason::API);
399
400 CHECK(UnbarrieredCastToInt(wrapper1.get()) != initialObjAsInt);
401 CHECK(UnbarrieredCastToInt(wrapper2.get()) != initialObjAsInt);
402 CHECK(!IsInsideNursery(wrapper2.get()));
403 CHECK(TestCanAccessObject(wrapper2.get()));
404 }
405
406 cx->minorGC(JS::GCReason::API);
407
408 return true;
409 }
410
411 END_TEST(testGCHeapPostBarriers)
412
413 // Test read barrier implementation on wrapper types. The following wrapper
414 // types have read barriers:
415 // - JS::Heap
416 // - JS::TenuredHeap
417 // - WeakHeapPtr
418 //
419 // Also check that equality comparisons on wrappers do not trigger the read
420 // barrier.
421 BEGIN_TEST(testGCHeapReadBarriers) {
422 AutoLeaveZeal nozeal(cx);
423
424 CHECK((TestWrapperType<JS::Heap<JSObject*>, JSObject*>()));
425 CHECK((TestWrapperType<JS::TenuredHeap<JSObject*>, JSObject*>()));
426 CHECK((TestWrapperType<WeakHeapPtr<JSObject*>, JSObject*>()));
427 CHECK((TestWrapperType<JS::Heap<JS::ArrayBuffer>, JS::ArrayBuffer>()));
428 CHECK((TestWrapperType<JS::Heap<JS::Uint8Array>, JS::Uint8Array>()));
429
430 return true;
431 }
432
433 template <typename WrapperT, typename ObjectT>
434 [[nodiscard]] bool TestWrapperType() {
435 // Check that the read barrier normally marks gray things black.
436 CHECK((TestReadBarrierUnmarksGray<WrapperT, ObjectT>()));
437
438 // Check that the read barrier marks gray and white things black during an
439 // incremental GC.
440 CHECK((TestReadBarrierMarksBlack<WrapperT, ObjectT>(true)));
441 CHECK((TestReadBarrierMarksBlack<WrapperT, ObjectT>(false)));
442
443 // Allocate test objects and make them gray. We will make sure they stay
444 // gray. (For most reads, the barrier will unmark gray.)
445 Rooted<ObjectT> obj1(cx, CreateTenuredGCThing<ObjectT>(cx));
446 Rooted<ObjectT> obj2(cx, CreateTenuredGCThing<ObjectT>(cx));
447 MakeGray(obj1);
448 MakeGray(obj2);
449
450 WrapperT wrapper1(obj1);
451 WrapperT wrapper2(obj2);
452 const ObjectT constobj1 = obj1;
453 const ObjectT constobj2 = obj2;
454 CHECK((TestUnbarrieredOperations<WrapperT, ObjectT>(obj1, obj2, wrapper1,
455 wrapper2)));
456 CHECK((TestUnbarrieredOperations<WrapperT, ObjectT>(constobj1, constobj2,
457 wrapper1, wrapper2)));
458
459 return true;
460 }
461
462 template <typename WrapperT, typename ObjectT>
463 void Access(const WrapperT& wrapper) {
464 if constexpr (std::is_base_of_v<JS::ArrayBufferOrView, ObjectT>) {
465 (void)wrapper.asObject();
466 } else {
467 (void)*wrapper;
468 }
469 }
470
471 template <typename WrapperT, typename ObjectT>
472 [[nodiscard]] bool TestReadBarrierUnmarksGray() {
473 Rooted<ObjectT> obj(cx, CreateTenuredGCThing<ObjectT>(cx));
474 WrapperT wrapper(obj);
475
476 CHECK(GetColor(obj) == gc::CellColor::White);
477
478 Access<WrapperT, ObjectT>(wrapper);
479
480 CHECK(GetColor(obj) == gc::CellColor::White);
481
482 MakeGray(obj);
483 Access<WrapperT, ObjectT>(wrapper);
484
485 CHECK(GetColor(obj) == gc::CellColor::Black);
486
487 return true;
488 }
489
490 // Execute thunk |f| between two slices of an incremental GC controlled by zeal
491 // mode |mode|.
492 template <typename F>
493 [[nodiscard]] bool CallDuringIncrementalGC(uint32_t mode, F&& f) {
494 #ifndef JS_GC_ZEAL
495 fprintf(stderr, "This test requires building with --enable-gczeal\n");
496 #else
497 AutoGCParameter incremental(cx, JSGC_INCREMENTAL_GC_ENABLED, true);
498
499 const int64_t BudgetMS = 10000; // 10S should be long enough for anyone.
500
501 JS::SetGCZeal(cx, mode, 0);
502 JS::PrepareZoneForGC(cx, js::GetContextZone(cx));
503 JS::SliceBudget budget{JS::TimeBudget(BudgetMS)};
504 JS::StartIncrementalGC(cx, JS::GCOptions(), JS::GCReason::DEBUG_GC, budget);
505 CHECK(JS::IsIncrementalGCInProgress(cx));
506
507 CHECK(f());
508
509 JS::FinishIncrementalGC(cx, JS::GCReason::DEBUG_GC);
510 #endif
511
512 return true;
513 }
514
515 template <typename WrapperT, typename ObjectT>
516 [[nodiscard]] bool TestReadBarrierMarksBlack(bool fromWhite) {
517 AutoLeaveZeal noZeal(cx);
518
519 // Create an object and hide it from the hazard analysis.
520 void* ptr = CreateHiddenTenuredGCThing<ObjectT>(cx);
521 CHECK(ptr);
522
523 CHECK(CallDuringIncrementalGC(9 /* YieldBeforeSweeping */, [&]() -> bool {
524 CHECK(JS::IsIncrementalBarrierNeeded(cx));
525
526 auto obj = RecoverHiddenGCThing<ObjectT>(ptr);
527
528 WrapperT wrapper(obj);
529
530 CHECK(GetColor(obj) == gc::CellColor::White);
531 if (!fromWhite) {
532 MakeGray(obj);
533 }
534
535 Access<WrapperT, ObjectT>(wrapper);
536
537 CHECK(GetColor(obj) == gc::CellColor::Black);
538
539 return true;
540 }));
541
542 return true;
543 }
544
545 template <typename WrapperT, typename ObjectT>
546 [[nodiscard]] bool TestUnbarrieredOperations(ObjectT obj, ObjectT obj2,
547 WrapperT& wrapper,
548 WrapperT& wrapper2) {
549 (void)bool(wrapper);
550 (void)bool(wrapper2);
551 CHECK(GetColor(obj) == gc::CellColor::Gray);
552 CHECK(GetColor(obj2) == gc::CellColor::Gray);
553
554 int x = 0;
555
556 CHECK(GetColor(obj) == gc::CellColor::Gray);
557 CHECK(GetColor(obj2) == gc::CellColor::Gray);
558 x += obj == obj2;
559 CHECK(GetColor(obj) == gc::CellColor::Gray);
560 CHECK(GetColor(obj2) == gc::CellColor::Gray);
561 x += obj == wrapper2;
562 CHECK(GetColor(obj) == gc::CellColor::Gray);
563 CHECK(GetColor(obj2) == gc::CellColor::Gray);
564 x += wrapper == obj2;
565 CHECK(GetColor(obj) == gc::CellColor::Gray);
566 CHECK(GetColor(obj2) == gc::CellColor::Gray);
567 x += wrapper == wrapper2;
568 CHECK(GetColor(obj) == gc::CellColor::Gray);
569 CHECK(GetColor(obj2) == gc::CellColor::Gray);
570
571 CHECK(x == 0);
572
573 x += obj != obj2;
574 CHECK(GetColor(obj) == gc::CellColor::Gray);
575 CHECK(GetColor(obj2) == gc::CellColor::Gray);
576 x += obj != wrapper2;
577 CHECK(GetColor(obj) == gc::CellColor::Gray);
578 CHECK(GetColor(obj2) == gc::CellColor::Gray);
579 x += wrapper != obj2;
580 CHECK(GetColor(obj) == gc::CellColor::Gray);
581 CHECK(GetColor(obj2) == gc::CellColor::Gray);
582 x += wrapper != wrapper2;
583 CHECK(GetColor(obj) == gc::CellColor::Gray);
584 CHECK(GetColor(obj2) == gc::CellColor::Gray);
585
586 CHECK(x == 4);
587
588 return true;
589 }
590
591 END_TEST(testGCHeapReadBarriers)
592
593 using ObjectVector = Vector<JSObject*, 0, SystemAllocPolicy>;
594
595 // Test pre-barrier implementation on wrapper types. The following wrapper types
596 // have a pre-barrier:
597 // - GCPtr
598 // - HeapPtr
599 // - PreBarriered
600 BEGIN_TEST(testGCHeapPreBarriers) {
601 AutoLeaveZeal nozeal(cx);
602
603 AutoGCParameter param1(cx, JSGC_INCREMENTAL_GC_ENABLED, true);
604
605 // Create a bunch of objects. These are unrooted and will be used to test
606 // whether barriers have fired by checking whether they have been marked
607 // black.
608 size_t objectCount = 100; // Increase this if necessary when adding tests.
609 ObjectVector testObjects;
610 for (size_t i = 0; i < objectCount; i++) {
611 JSObject* obj = CreateTenuredGCThing<JSObject*>(cx);
612 CHECK(obj);
613 CHECK(testObjects.append(obj));
614 }
615
616 // Start an incremental GC so we can detect if we cause barriers to fire, as
617 // these will mark objects black.
618 JS::PrepareForFullGC(cx);
619 JS::SliceBudget budget(JS::WorkBudget(1));
620 gc::GCRuntime* gc = &cx->runtime()->gc;
621 gc->startDebugGC(JS::GCOptions::Normal, budget);
622 while (gc->state() != gc::State::Mark) {
623 gc->debugGCSlice(budget);
624 }
625 MOZ_ASSERT(cx->zone()->needsIncrementalBarrier());
626
627 CHECK(TestWrapper<HeapPtr<JSObject*>>(testObjects));
628 CHECK(TestWrapper<PreBarriered<JSObject*>>(testObjects));
629
630 // GCPtr is different because 1) it doesn't support move operations as it's
631 // supposed to be part of a GC thing and 2) it doesn't perform a pre-barrier
632 // in its destructor because these are only destroyed as part of a GC where
633 // the barrier is unnecessary.
634 CHECK(TestGCPtr(testObjects));
635
636 gc::FinishGC(cx, JS::GCReason::API);
637
638 return true;
639 }
640
641 template <typename Wrapper>
642 [[nodiscard]] bool TestWrapper(ObjectVector& testObjects) {
643 CHECK(TestCopyConstruction<Wrapper>(testObjects.popCopy()));
644 CHECK(TestMoveConstruction<Wrapper>(testObjects.popCopy()));
645 CHECK(TestAssignment<Wrapper>(testObjects.popCopy(), testObjects.popCopy()));
646 CHECK(TestMoveAssignment<Wrapper>(testObjects.popCopy(),
647 testObjects.popCopy()));
648 return true;
649 }
650
651 template <typename Wrapper>
652 [[nodiscard]] bool TestCopyConstruction(JSObject* obj) {
653 CHECK(GetColor(obj) == gc::CellColor::White);
654
655 {
656 Wrapper wrapper1(obj);
657 Wrapper wrapper2(wrapper1);
658 CHECK(wrapper1 == obj);
659 CHECK(wrapper2 == obj);
660 CHECK(GetColor(obj) == gc::CellColor::White);
661 }
662
663 // Check destructor performs pre-barrier.
664 CHECK(GetColor(obj) == gc::CellColor::Black);
665
666 return true;
667 }
668
669 template <typename Wrapper>
670 [[nodiscard]] bool TestMoveConstruction(JSObject* obj) {
671 CHECK(GetColor(obj) == gc::CellColor::White);
672
673 {
674 Wrapper wrapper1(obj);
675 MakeGray(obj); // Check that we allow move of gray GC thing.
676 Wrapper wrapper2(std::move(wrapper1));
677 CHECK(!wrapper1);
678 CHECK(wrapper2 == obj);
679 CHECK(GetColor(obj) == gc::CellColor::Gray);
680 }
681
682 // Check destructor performs pre-barrier.
683 CHECK(GetColor(obj) == gc::CellColor::Black);
684
685 return true;
686 }
687
688 template <typename Wrapper>
689 [[nodiscard]] bool TestAssignment(JSObject* obj1, JSObject* obj2) {
690 CHECK(GetColor(obj1) == gc::CellColor::White);
691 CHECK(GetColor(obj2) == gc::CellColor::White);
692
693 {
694 Wrapper wrapper1(obj1);
695 Wrapper wrapper2(obj2);
696
697 wrapper2 = wrapper1;
698
699 CHECK(wrapper1 == obj1);
700 CHECK(wrapper2 == obj1);
701 CHECK(GetColor(obj1) == gc::CellColor::White); // No barrier fired.
702 CHECK(GetColor(obj2) == gc::CellColor::Black); // Pre barrier fired.
703 }
704
705 // Check destructor performs pre-barrier.
706 CHECK(GetColor(obj1) == gc::CellColor::Black);
707
708 return true;
709 }
710
711 template <typename Wrapper>
712 [[nodiscard]] bool TestMoveAssignment(JSObject* obj1, JSObject* obj2) {
713 CHECK(GetColor(obj1) == gc::CellColor::White);
714 CHECK(GetColor(obj2) == gc::CellColor::White);
715
716 {
717 Wrapper wrapper1(obj1);
718 Wrapper wrapper2(obj2);
719
720 MakeGray(obj1); // Check we allow move of gray thing.
721 wrapper2 = std::move(wrapper1);
722
723 CHECK(!wrapper1);
724 CHECK(wrapper2 == obj1);
725 CHECK(GetColor(obj1) == gc::CellColor::Gray); // No barrier fired.
726 CHECK(GetColor(obj2) == gc::CellColor::Black); // Pre barrier fired.
727 }
728
729 // Check destructor performs pre-barrier.
730 CHECK(GetColor(obj1) == gc::CellColor::Black);
731
732 return true;
733 }
734
735 [[nodiscard]] bool TestGCPtr(ObjectVector& testObjects) {
736 CHECK(TestGCPtrCopyConstruction(testObjects.popCopy()));
737 CHECK(TestGCPtrAssignment(testObjects.popCopy(), testObjects.popCopy()));
738 return true;
739 }
740
741 [[nodiscard]] bool TestGCPtrCopyConstruction(JSObject* obj) {
742 CHECK(GetColor(obj) == gc::CellColor::White);
743
744 {
745 // Let us destroy GCPtrs ourselves for testing purposes.
746 gc::AutoSetThreadIsFinalizing threadIsFinalizing;
747
748 GCPtr<JSObject*> wrapper1(obj);
749 GCPtr<JSObject*> wrapper2(wrapper1);
750 CHECK(wrapper1 == obj);
751 CHECK(wrapper2 == obj);
752 CHECK(GetColor(obj) == gc::CellColor::White);
753 }
754
755 // GCPtr doesn't perform pre-barrier in destructor.
756 CHECK(GetColor(obj) == gc::CellColor::White);
757
758 return true;
759 }
760
761 [[nodiscard]] bool TestGCPtrAssignment(JSObject* obj1, JSObject* obj2) {
762 CHECK(GetColor(obj1) == gc::CellColor::White);
763 CHECK(GetColor(obj2) == gc::CellColor::White);
764
765 {
766 // Let us destroy GCPtrs ourselves for testing purposes.
767 gc::AutoSetThreadIsFinalizing threadIsFinalizing;
768
769 GCPtr<JSObject*> wrapper1(obj1);
770 GCPtr<JSObject*> wrapper2(obj2);
771
772 wrapper2 = wrapper1;
773
774 CHECK(wrapper1 == obj1);
775 CHECK(wrapper2 == obj1);
776 CHECK(GetColor(obj1) == gc::CellColor::White); // No barrier fired.
777 CHECK(GetColor(obj2) == gc::CellColor::Black); // Pre barrier fired.
778 }
779
780 // GCPtr doesn't perform pre-barrier in destructor.
781 CHECK(GetColor(obj1) == gc::CellColor::White);
782
783 return true;
784 }
785
786 END_TEST(testGCHeapPreBarriers)