| 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 <algorithm> |
| 9 |
|
| 10 |
#include "gc/GCInternals.h" |
| 11 |
#include "gc/WeakMap.h" |
| 12 |
#include "gc/Zone.h" |
| 13 |
#include "js/PropertyAndElement.h" // JS_DefineProperty, JS_DefinePropertyById |
| 14 |
#include "js/Proxy.h" |
| 15 |
#include "js/WeakMap.h" |
| 16 |
#include "jsapi-tests/tests.h" |
| 17 |
|
| 18 |
using namespace js; |
| 19 |
using namespace js::gc; |
| 20 |
|
| 21 |
static constexpr CellColor AllCellColors[] = {CellColor::White, CellColor::Gray, |
| 22 |
CellColor::Black}; |
| 23 |
|
| 24 |
static constexpr CellColor MarkedCellColors[] = {CellColor::Gray, |
| 25 |
CellColor::Black}; |
| 26 |
|
| 27 |
namespace js { |
| 28 |
|
| 29 |
struct GCManagedObjectWeakMap : public ObjectWeakMap { |
| 30 |
using ObjectWeakMap::ObjectWeakMap; |
| 31 |
}; |
| 32 |
|
| 33 |
} // namespace js |
| 34 |
|
| 35 |
namespace JS { |
| 36 |
|
| 37 |
template <> |
| 38 |
struct MapTypeToRootKind<js::GCManagedObjectWeakMap*> { |
| 39 |
static const JS::RootKind kind = JS::RootKind::Traceable; |
| 40 |
}; |
| 41 |
|
| 42 |
template <> |
| 43 |
struct GCPolicy<js::GCManagedObjectWeakMap*> |
| 44 |
: public NonGCPointerPolicy<js::GCManagedObjectWeakMap*> {}; |
| 45 |
|
| 46 |
} // namespace JS |
| 47 |
|
| 48 |
class AutoNoAnalysisForTest { |
| 49 |
public: |
| 50 |
AutoNoAnalysisForTest() {} |
| 51 |
} JS_HAZ_GC_SUPPRESSED; |
| 52 |
|
| 53 |
BEGIN_TEST(testGCGrayMarking) { |
| 54 |
AutoNoAnalysisForTest disableAnalysis; |
| 55 |
AutoDisableCompactingGC disableCompactingGC(cx); |
| 56 |
AutoLeaveZeal nozeal(cx); |
| 57 |
|
| 58 |
CHECK(InitGlobals()); |
| 59 |
JSAutoRealm ar(cx, global1); |
| 60 |
|
| 61 |
InitGrayRootTracer(); |
| 62 |
|
| 63 |
// Enable incremental GC. |
| 64 |
AutoGCParameter param1(cx, JSGC_INCREMENTAL_GC_ENABLED, true); |
| 65 |
AutoGCParameter param2(cx, JSGC_PER_ZONE_GC_ENABLED, true); |
| 66 |
|
| 67 |
bool ok = TestMarking() && TestJSWeakMaps() && TestInternalWeakMaps() && |
| 68 |
TestCCWs() && TestGrayUnmarking(); |
| 69 |
|
| 70 |
global1 = nullptr; |
| 71 |
global2 = nullptr; |
| 72 |
RemoveGrayRootTracer(); |
| 73 |
|
| 74 |
return ok; |
| 75 |
} |
| 76 |
|
| 77 |
bool TestMarking() { |
| 78 |
JSObject* sameTarget = AllocPlainObject(); |
| 79 |
CHECK(sameTarget); |
| 80 |
|
| 81 |
JSObject* sameSource = AllocSameCompartmentSourceObject(sameTarget); |
| 82 |
CHECK(sameSource); |
| 83 |
|
| 84 |
JSObject* crossTarget = AllocPlainObject(); |
| 85 |
CHECK(crossTarget); |
| 86 |
|
| 87 |
JSObject* crossSource = GetCrossCompartmentWrapper(crossTarget); |
| 88 |
CHECK(crossSource); |
| 89 |
|
| 90 |
// Test GC with black roots marks objects black. |
| 91 |
|
| 92 |
JS::RootedObject blackRoot1(cx, sameSource); |
| 93 |
JS::RootedObject blackRoot2(cx, crossSource); |
| 94 |
|
| 95 |
JS_GC(cx); |
| 96 |
|
| 97 |
CHECK(IsMarkedBlack(sameSource)); |
| 98 |
CHECK(IsMarkedBlack(crossSource)); |
| 99 |
CHECK(IsMarkedBlack(sameTarget)); |
| 100 |
CHECK(IsMarkedBlack(crossTarget)); |
| 101 |
|
| 102 |
// Test GC with black and gray roots marks objects black. |
| 103 |
|
| 104 |
grayRoots.grayRoot1 = sameSource; |
| 105 |
grayRoots.grayRoot2 = crossSource; |
| 106 |
|
| 107 |
JS_GC(cx); |
| 108 |
|
| 109 |
CHECK(IsMarkedBlack(sameSource)); |
| 110 |
CHECK(IsMarkedBlack(crossSource)); |
| 111 |
CHECK(IsMarkedBlack(sameTarget)); |
| 112 |
CHECK(IsMarkedBlack(crossTarget)); |
| 113 |
|
| 114 |
CHECK(!JS::ObjectIsMarkedGray(sameSource)); |
| 115 |
|
| 116 |
// Test GC with gray roots marks object gray. |
| 117 |
|
| 118 |
blackRoot1 = nullptr; |
| 119 |
blackRoot2 = nullptr; |
| 120 |
|
| 121 |
JS_GC(cx); |
| 122 |
|
| 123 |
CHECK(IsMarkedGray(sameSource)); |
| 124 |
CHECK(IsMarkedGray(crossSource)); |
| 125 |
CHECK(IsMarkedGray(sameTarget)); |
| 126 |
CHECK(IsMarkedGray(crossTarget)); |
| 127 |
|
| 128 |
CHECK(JS::ObjectIsMarkedGray(sameSource)); |
| 129 |
|
| 130 |
// Test ExposeToActiveJS marks gray objects black. |
| 131 |
|
| 132 |
JS::ExposeObjectToActiveJS(sameSource); |
| 133 |
JS::ExposeObjectToActiveJS(crossSource); |
| 134 |
CHECK(IsMarkedBlack(sameSource)); |
| 135 |
CHECK(IsMarkedBlack(crossSource)); |
| 136 |
CHECK(IsMarkedBlack(sameTarget)); |
| 137 |
CHECK(IsMarkedBlack(crossTarget)); |
| 138 |
|
| 139 |
// Test a zone GC with black roots marks gray object in other zone black. |
| 140 |
|
| 141 |
JS_GC(cx); |
| 142 |
|
| 143 |
CHECK(IsMarkedGray(crossSource)); |
| 144 |
CHECK(IsMarkedGray(crossTarget)); |
| 145 |
|
| 146 |
blackRoot1 = crossSource; |
| 147 |
CHECK(ZoneGC(crossSource->zone())); |
| 148 |
|
| 149 |
CHECK(IsMarkedBlack(crossSource)); |
| 150 |
CHECK(IsMarkedBlack(crossTarget)); |
| 151 |
|
| 152 |
blackRoot1 = nullptr; |
| 153 |
blackRoot2 = nullptr; |
| 154 |
grayRoots.grayRoot1 = nullptr; |
| 155 |
grayRoots.grayRoot2 = nullptr; |
| 156 |
|
| 157 |
return true; |
| 158 |
} |
| 159 |
|
| 160 |
static constexpr CellColor DontMark = CellColor::White; |
| 161 |
|
| 162 |
enum MarkKeyOrDelegate : bool { MarkKey = true, MarkDelegate = false }; |
| 163 |
|
| 164 |
bool TestJSWeakMaps() { |
| 165 |
for (auto keyOrDelegateColor : MarkedCellColors) { |
| 166 |
for (auto mapColor : MarkedCellColors) { |
| 167 |
for (auto markKeyOrDelegate : {MarkKey, MarkDelegate}) { |
| 168 |
CellColor expected = std::min(keyOrDelegateColor, mapColor); |
| 169 |
CHECK(TestJSWeakMap(markKeyOrDelegate, keyOrDelegateColor, mapColor, |
| 170 |
expected)); |
| 171 |
#ifdef JS_GC_ZEAL |
| 172 |
CHECK(TestJSWeakMapWithGrayUnmarking( |
| 173 |
markKeyOrDelegate, keyOrDelegateColor, mapColor, expected)); |
| 174 |
#endif |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
return true; |
| 180 |
} |
| 181 |
|
| 182 |
bool TestInternalWeakMaps() { |
| 183 |
for (auto keyMarkColor : AllCellColors) { |
| 184 |
for (auto delegateMarkColor : AllCellColors) { |
| 185 |
if (keyMarkColor == CellColor::White && |
| 186 |
delegateMarkColor == CellColor::White) { |
| 187 |
continue; |
| 188 |
} |
| 189 |
|
| 190 |
// The map is black. The delegate marks its key via wrapper preservation. |
| 191 |
// The key maps its delegate and the value. Thus, all three end up the |
| 192 |
// maximum of the key and delegate colors. |
| 193 |
CellColor expected = std::max(keyMarkColor, delegateMarkColor); |
| 194 |
CHECK(TestInternalWeakMap(keyMarkColor, delegateMarkColor, expected)); |
| 195 |
|
| 196 |
#ifdef JS_GC_ZEAL |
| 197 |
CHECK(TestInternalWeakMapWithGrayUnmarking(keyMarkColor, |
| 198 |
delegateMarkColor, expected)); |
| 199 |
#endif |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
return true; |
| 204 |
} |
| 205 |
|
| 206 |
bool TestJSWeakMap(MarkKeyOrDelegate markKey, CellColor weakMapMarkColor, |
| 207 |
CellColor keyOrDelegateMarkColor, |
| 208 |
CellColor expectedValueColor) { |
| 209 |
using std::swap; |
| 210 |
|
| 211 |
// Test marking a JS WeakMap object. |
| 212 |
// |
| 213 |
// This marks the map and one of the key or delegate. The key/delegate and the |
| 214 |
// value can end up different colors depending on the color of the map. |
| 215 |
|
| 216 |
JSObject* weakMap; |
| 217 |
JSObject* key; |
| 218 |
JSObject* value; |
| 219 |
|
| 220 |
// If both map and key are marked the same color, test both possible |
| 221 |
// orderings. |
| 222 |
unsigned markOrderings = weakMapMarkColor == keyOrDelegateMarkColor ? 2 : 1; |
| 223 |
|
| 224 |
for (unsigned markOrder = 0; markOrder < markOrderings; markOrder++) { |
| 225 |
CHECK(CreateJSWeakMapObjects(&weakMap, &key, &value)); |
| 226 |
|
| 227 |
JSObject* delegate = UncheckedUnwrapWithoutExpose(key); |
| 228 |
JSObject* keyOrDelegate = markKey ? key : delegate; |
| 229 |
|
| 230 |
RootedObject blackRoot1(cx); |
| 231 |
RootedObject blackRoot2(cx); |
| 232 |
|
| 233 |
RootObject(weakMap, weakMapMarkColor, blackRoot1, grayRoots.grayRoot1); |
| 234 |
RootObject(keyOrDelegate, keyOrDelegateMarkColor, blackRoot2, |
| 235 |
grayRoots.grayRoot2); |
| 236 |
|
| 237 |
if (markOrder != 0) { |
| 238 |
swap(blackRoot1.get(), blackRoot2.get()); |
| 239 |
swap(grayRoots.grayRoot1, grayRoots.grayRoot2); |
| 240 |
} |
| 241 |
|
| 242 |
JS_GC(cx); |
| 243 |
|
| 244 |
ClearGrayRoots(); |
| 245 |
|
| 246 |
CHECK(weakMap->color() == weakMapMarkColor); |
| 247 |
CHECK(keyOrDelegate->color() == keyOrDelegateMarkColor); |
| 248 |
CHECK(value->color() == expectedValueColor); |
| 249 |
} |
| 250 |
|
| 251 |
return true; |
| 252 |
} |
| 253 |
|
| 254 |
#ifdef JS_GC_ZEAL |
| 255 |
|
| 256 |
bool TestJSWeakMapWithGrayUnmarking(MarkKeyOrDelegate markKey, |
| 257 |
CellColor weakMapMarkColor, |
| 258 |
CellColor keyOrDelegateMarkColor, |
| 259 |
CellColor expectedValueColor) { |
| 260 |
// This is like the previous test, but things are marked black by gray |
| 261 |
// unmarking during incremental GC. |
| 262 |
|
| 263 |
JSObject* weakMap; |
| 264 |
JSObject* key; |
| 265 |
JSObject* value; |
| 266 |
|
| 267 |
// If both map and key are marked the same color, test both possible |
| 268 |
// orderings. |
| 269 |
unsigned markOrderings = weakMapMarkColor == keyOrDelegateMarkColor ? 2 : 1; |
| 270 |
|
| 271 |
JS::SetGCZeal(cx, uint8_t(ZealMode::YieldWhileGrayMarking), 0); |
| 272 |
|
| 273 |
for (unsigned markOrder = 0; markOrder < markOrderings; markOrder++) { |
| 274 |
CHECK(CreateJSWeakMapObjects(&weakMap, &key, &value)); |
| 275 |
|
| 276 |
JSObject* delegate = UncheckedUnwrapWithoutExpose(key); |
| 277 |
JSObject* keyOrDelegate = markKey ? key : delegate; |
| 278 |
|
| 279 |
grayRoots.grayRoot1 = keyOrDelegate; |
| 280 |
grayRoots.grayRoot2 = weakMap; |
| 281 |
|
| 282 |
// Start an incremental GC and run until gray roots have been pushed onto |
| 283 |
// the mark stack. |
| 284 |
JS::PrepareForFullGC(cx); |
| 285 |
JS::SliceBudget budget(JS::TimeBudget(1000000)); |
| 286 |
JS::StartIncrementalGC(cx, JS::GCOptions::Normal, JS::GCReason::DEBUG_GC, |
| 287 |
budget); |
| 288 |
MOZ_ASSERT(cx->runtime()->gc.state() == gc::State::Sweep); |
| 289 |
MOZ_ASSERT(cx->zone()->gcState() == Zone::MarkBlackAndGray); |
| 290 |
|
| 291 |
// Unmark gray things as specified. |
| 292 |
if (markOrder != 0) { |
| 293 |
MaybeExposeObject(weakMap, weakMapMarkColor); |
| 294 |
MaybeExposeObject(keyOrDelegate, keyOrDelegateMarkColor); |
| 295 |
} else { |
| 296 |
MaybeExposeObject(keyOrDelegate, keyOrDelegateMarkColor); |
| 297 |
MaybeExposeObject(weakMap, weakMapMarkColor); |
| 298 |
} |
| 299 |
|
| 300 |
JS::FinishIncrementalGC(cx, JS::GCReason::API); |
| 301 |
|
| 302 |
ClearGrayRoots(); |
| 303 |
|
| 304 |
CHECK(weakMap->color() == weakMapMarkColor); |
| 305 |
CHECK(keyOrDelegate->color() == keyOrDelegateMarkColor); |
| 306 |
CHECK(value->color() == expectedValueColor); |
| 307 |
} |
| 308 |
|
| 309 |
JS::UnsetGCZeal(cx, uint8_t(ZealMode::YieldWhileGrayMarking)); |
| 310 |
|
| 311 |
return true; |
| 312 |
} |
| 313 |
|
| 314 |
static void MaybeExposeObject(JSObject* object, CellColor color) { |
| 315 |
if (color == CellColor::Black) { |
| 316 |
JS::ExposeObjectToActiveJS(object); |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
#endif // JS_GC_ZEAL |
| 321 |
|
| 322 |
bool CreateJSWeakMapObjects(JSObject** weakMapOut, JSObject** keyOut, |
| 323 |
JSObject** valueOut) { |
| 324 |
RootedObject key(cx, AllocWeakmapKeyObject()); |
| 325 |
CHECK(key); |
| 326 |
|
| 327 |
RootedObject value(cx, AllocPlainObject()); |
| 328 |
CHECK(value); |
| 329 |
|
| 330 |
RootedObject weakMap(cx, JS::NewWeakMapObject(cx)); |
| 331 |
CHECK(weakMap); |
| 332 |
|
| 333 |
JS::RootedValue keyValue(cx, ObjectValue(*key)); |
| 334 |
JS::RootedValue valueValue(cx, ObjectValue(*value)); |
| 335 |
CHECK(SetWeakMapEntry(cx, weakMap, keyValue, valueValue)); |
| 336 |
|
| 337 |
*weakMapOut = weakMap; |
| 338 |
*keyOut = key; |
| 339 |
*valueOut = value; |
| 340 |
return true; |
| 341 |
} |
| 342 |
|
| 343 |
bool TestInternalWeakMap(CellColor keyMarkColor, CellColor delegateMarkColor, |
| 344 |
CellColor expectedColor) { |
| 345 |
using std::swap; |
| 346 |
|
| 347 |
// Test marking for internal weakmaps (without an owning JSObject). |
| 348 |
// |
| 349 |
// All of the key, delegate and value are expected to end up the same color. |
| 350 |
|
| 351 |
UniquePtr<GCManagedObjectWeakMap> weakMap; |
| 352 |
JSObject* key; |
| 353 |
JSObject* value; |
| 354 |
|
| 355 |
// If both key and delegate are marked the same color, test both possible |
| 356 |
// orderings. |
| 357 |
unsigned markOrderings = keyMarkColor == delegateMarkColor ? 2 : 1; |
| 358 |
|
| 359 |
for (unsigned markOrder = 0; markOrder < markOrderings; markOrder++) { |
| 360 |
CHECK(CreateInternalWeakMapObjects(&weakMap, &key, &value)); |
| 361 |
|
| 362 |
JSObject* delegate = UncheckedUnwrapWithoutExpose(key); |
| 363 |
|
| 364 |
RootedObject blackRoot1(cx); |
| 365 |
RootedObject blackRoot2(cx); |
| 366 |
|
| 367 |
Rooted<GCManagedObjectWeakMap*> rootMap(cx, weakMap.get()); |
| 368 |
RootObject(key, keyMarkColor, blackRoot1, grayRoots.grayRoot1); |
| 369 |
RootObject(delegate, delegateMarkColor, blackRoot2, grayRoots.grayRoot2); |
| 370 |
|
| 371 |
if (markOrder != 0) { |
| 372 |
swap(blackRoot1.get(), blackRoot2.get()); |
| 373 |
swap(grayRoots.grayRoot1, grayRoots.grayRoot2); |
| 374 |
} |
| 375 |
|
| 376 |
JS_GC(cx); |
| 377 |
|
| 378 |
ClearGrayRoots(); |
| 379 |
|
| 380 |
CHECK(key->color() == expectedColor); |
| 381 |
CHECK(delegate->color() == expectedColor); |
| 382 |
CHECK(value->color() == expectedColor); |
| 383 |
|
| 384 |
AutoSetThreadIsFinalizing setFinalizing; |
| 385 |
js_delete(weakMap.release()); |
| 386 |
} |
| 387 |
|
| 388 |
return true; |
| 389 |
} |
| 390 |
|
| 391 |
#ifdef JS_GC_ZEAL |
| 392 |
|
| 393 |
bool TestInternalWeakMapWithGrayUnmarking(CellColor keyMarkColor, |
| 394 |
CellColor delegateMarkColor, |
| 395 |
CellColor expectedColor) { |
| 396 |
UniquePtr<GCManagedObjectWeakMap> weakMap; |
| 397 |
JSObject* key; |
| 398 |
JSObject* value; |
| 399 |
|
| 400 |
// If both key and delegate are marked the same color, test both possible |
| 401 |
// orderings. |
| 402 |
unsigned markOrderings = keyMarkColor == delegateMarkColor ? 2 : 1; |
| 403 |
|
| 404 |
JS::SetGCZeal(cx, uint8_t(ZealMode::YieldWhileGrayMarking), 0); |
| 405 |
|
| 406 |
for (unsigned markOrder = 0; markOrder < markOrderings; markOrder++) { |
| 407 |
CHECK(CreateInternalWeakMapObjects(&weakMap, &key, &value)); |
| 408 |
|
| 409 |
JSObject* delegate = UncheckedUnwrapWithoutExpose(key); |
| 410 |
|
| 411 |
Rooted<GCManagedObjectWeakMap*> rootMap(cx, weakMap.get()); |
| 412 |
grayRoots.grayRoot1 = key; |
| 413 |
grayRoots.grayRoot2 = delegate; |
| 414 |
|
| 415 |
// Start an incremental GC and run until gray roots have been pushed onto |
| 416 |
// the mark stack. |
| 417 |
JS::PrepareForFullGC(cx); |
| 418 |
JS::SliceBudget budget(JS::TimeBudget(1000000)); |
| 419 |
JS::StartIncrementalGC(cx, JS::GCOptions::Normal, JS::GCReason::DEBUG_GC, |
| 420 |
budget); |
| 421 |
MOZ_ASSERT(cx->runtime()->gc.state() == gc::State::Sweep); |
| 422 |
MOZ_ASSERT(cx->zone()->gcState() == Zone::MarkBlackAndGray); |
| 423 |
|
| 424 |
// Unmark gray things as specified. |
| 425 |
if (markOrder != 0) { |
| 426 |
MaybeExposeObject(key, keyMarkColor); |
| 427 |
MaybeExposeObject(delegate, delegateMarkColor); |
| 428 |
} else { |
| 429 |
MaybeExposeObject(key, keyMarkColor); |
| 430 |
MaybeExposeObject(delegate, delegateMarkColor); |
| 431 |
} |
| 432 |
|
| 433 |
JS::FinishIncrementalGC(cx, JS::GCReason::API); |
| 434 |
|
| 435 |
ClearGrayRoots(); |
| 436 |
|
| 437 |
CHECK(key->color() == expectedColor); |
| 438 |
CHECK(delegate->color() == expectedColor); |
| 439 |
CHECK(value->color() == expectedColor); |
| 440 |
|
| 441 |
AutoSetThreadIsFinalizing setFinalizing; |
| 442 |
js_delete(weakMap.release()); |
| 443 |
} |
| 444 |
|
| 445 |
JS::UnsetGCZeal(cx, uint8_t(ZealMode::YieldWhileGrayMarking)); |
| 446 |
|
| 447 |
return true; |
| 448 |
} |
| 449 |
|
| 450 |
#endif // JS_GC_ZEAL |
| 451 |
|
| 452 |
bool CreateInternalWeakMapObjects(UniquePtr<GCManagedObjectWeakMap>* weakMapOut, |
| 453 |
JSObject** keyOut, JSObject** valueOut) { |
| 454 |
RootedObject key(cx, AllocWeakmapKeyObject()); |
| 455 |
CHECK(key); |
| 456 |
|
| 457 |
RootedObject value(cx, AllocPlainObject()); |
| 458 |
CHECK(value); |
| 459 |
|
| 460 |
auto weakMap = cx->make_unique<GCManagedObjectWeakMap>(cx); |
| 461 |
CHECK(weakMap); |
| 462 |
|
| 463 |
CHECK(weakMap->put(key, value)); |
| 464 |
|
| 465 |
*weakMapOut = std::move(weakMap); |
| 466 |
*keyOut = key; |
| 467 |
*valueOut = value; |
| 468 |
return true; |
| 469 |
} |
| 470 |
|
| 471 |
void RootObject(JSObject* object, CellColor color, RootedObject& blackRoot, |
| 472 |
JS::Heap<JSObject*>& grayRoot) { |
| 473 |
if (color == CellColor::Black) { |
| 474 |
blackRoot = object; |
| 475 |
} else if (color == CellColor::Gray) { |
| 476 |
grayRoot = object; |
| 477 |
} else { |
| 478 |
MOZ_RELEASE_ASSERT(color == CellColor::White); |
| 479 |
} |
| 480 |
} |
| 481 |
|
| 482 |
bool TestCCWs() { |
| 483 |
JSObject* target = AllocPlainObject(); |
| 484 |
CHECK(target); |
| 485 |
|
| 486 |
// Test getting a new wrapper doesn't return a gray wrapper. |
| 487 |
|
| 488 |
RootedObject blackRoot(cx, target); |
| 489 |
JSObject* wrapper = GetCrossCompartmentWrapper(target); |
| 490 |
CHECK(wrapper); |
| 491 |
CHECK(!IsMarkedGray(wrapper)); |
| 492 |
|
| 493 |
// Test getting an existing wrapper doesn't return a gray wrapper. |
| 494 |
|
| 495 |
grayRoots.grayRoot1 = wrapper; |
| 496 |
grayRoots.grayRoot2 = nullptr; |
| 497 |
JS_GC(cx); |
| 498 |
CHECK(IsMarkedGray(wrapper)); |
| 499 |
CHECK(IsMarkedBlack(target)); |
| 500 |
|
| 501 |
CHECK(GetCrossCompartmentWrapper(target) == wrapper); |
| 502 |
CHECK(!IsMarkedGray(wrapper)); |
| 503 |
|
| 504 |
// Test getting an existing wrapper doesn't return a gray wrapper |
| 505 |
// during incremental GC. |
| 506 |
|
| 507 |
JS_GC(cx); |
| 508 |
CHECK(IsMarkedGray(wrapper)); |
| 509 |
CHECK(IsMarkedBlack(target)); |
| 510 |
|
| 511 |
JSRuntime* rt = cx->runtime(); |
| 512 |
JS::PrepareForFullGC(cx); |
| 513 |
JS::SliceBudget budget(JS::WorkBudget(1)); |
| 514 |
rt->gc.startDebugGC(JS::GCOptions::Normal, budget); |
| 515 |
while (rt->gc.state() == gc::State::Prepare) { |
| 516 |
rt->gc.debugGCSlice(budget); |
| 517 |
} |
| 518 |
CHECK(JS::IsIncrementalGCInProgress(cx)); |
| 519 |
|
| 520 |
CHECK(!IsMarkedBlack(wrapper)); |
| 521 |
CHECK(wrapper->zone()->isGCMarkingBlackOnly()); |
| 522 |
|
| 523 |
CHECK(GetCrossCompartmentWrapper(target) == wrapper); |
| 524 |
CHECK(IsMarkedBlack(wrapper)); |
| 525 |
|
| 526 |
JS::FinishIncrementalGC(cx, JS::GCReason::API); |
| 527 |
|
| 528 |
// Test behaviour of gray CCWs marked black by a barrier during incremental |
| 529 |
// GC. |
| 530 |
|
| 531 |
// Initial state: source and target are gray. |
| 532 |
blackRoot = nullptr; |
| 533 |
grayRoots.grayRoot1 = wrapper; |
| 534 |
grayRoots.grayRoot2 = nullptr; |
| 535 |
JS_GC(cx); |
| 536 |
CHECK(IsMarkedGray(wrapper)); |
| 537 |
CHECK(IsMarkedGray(target)); |
| 538 |
|
| 539 |
// Incremental zone GC started: the source is now unmarked. |
| 540 |
JS::PrepareZoneForGC(cx, wrapper->zone()); |
| 541 |
budget = JS::SliceBudget(JS::WorkBudget(1)); |
| 542 |
rt->gc.startDebugGC(JS::GCOptions::Normal, budget); |
| 543 |
while (rt->gc.state() == gc::State::Prepare) { |
| 544 |
rt->gc.debugGCSlice(budget); |
| 545 |
} |
| 546 |
CHECK(JS::IsIncrementalGCInProgress(cx)); |
| 547 |
CHECK(wrapper->zone()->isGCMarkingBlackOnly()); |
| 548 |
CHECK(!target->zone()->wasGCStarted()); |
| 549 |
CHECK(!IsMarkedBlack(wrapper)); |
| 550 |
CHECK(!IsMarkedGray(wrapper)); |
| 551 |
CHECK(IsMarkedGray(target)); |
| 552 |
|
| 553 |
// Betweeen GC slices: source marked black by barrier, target is |
| 554 |
// still gray. Target will be marked gray |
| 555 |
// eventually. ObjectIsMarkedGray() is conservative and reports |
| 556 |
// that target is not marked gray; AssertObjectIsNotGray() will |
| 557 |
// assert. |
| 558 |
grayRoots.grayRoot1.get(); |
| 559 |
CHECK(IsMarkedBlack(wrapper)); |
| 560 |
CHECK(IsMarkedGray(target)); |
| 561 |
CHECK(!JS::ObjectIsMarkedGray(target)); |
| 562 |
|
| 563 |
// Final state: source and target are black. |
| 564 |
JS::FinishIncrementalGC(cx, JS::GCReason::API); |
| 565 |
CHECK(IsMarkedBlack(wrapper)); |
| 566 |
CHECK(IsMarkedBlack(target)); |
| 567 |
|
| 568 |
grayRoots.grayRoot1 = nullptr; |
| 569 |
grayRoots.grayRoot2 = nullptr; |
| 570 |
|
| 571 |
return true; |
| 572 |
} |
| 573 |
|
| 574 |
bool TestGrayUnmarking() { |
| 575 |
const size_t length = 2000; |
| 576 |
|
| 577 |
JSObject* chain = AllocObjectChain(length); |
| 578 |
CHECK(chain); |
| 579 |
|
| 580 |
RootedObject blackRoot(cx, chain); |
| 581 |
JS_GC(cx); |
| 582 |
size_t count; |
| 583 |
CHECK(IterateObjectChain(chain, ColorCheckFunctor(MarkColor::Black, &count))); |
| 584 |
CHECK(count == length); |
| 585 |
|
| 586 |
blackRoot = nullptr; |
| 587 |
grayRoots.grayRoot1 = chain; |
| 588 |
JS_GC(cx); |
| 589 |
CHECK(cx->runtime()->gc.areGrayBitsValid()); |
| 590 |
CHECK(IterateObjectChain(chain, ColorCheckFunctor(MarkColor::Gray, &count))); |
| 591 |
CHECK(count == length); |
| 592 |
|
| 593 |
JS::ExposeObjectToActiveJS(chain); |
| 594 |
CHECK(cx->runtime()->gc.areGrayBitsValid()); |
| 595 |
CHECK(IterateObjectChain(chain, ColorCheckFunctor(MarkColor::Black, &count))); |
| 596 |
CHECK(count == length); |
| 597 |
|
| 598 |
grayRoots.grayRoot1 = nullptr; |
| 599 |
|
| 600 |
return true; |
| 601 |
} |
| 602 |
|
| 603 |
struct ColorCheckFunctor { |
| 604 |
MarkColor color; |
| 605 |
size_t& count; |
| 606 |
|
| 607 |
ColorCheckFunctor(MarkColor colorArg, size_t* countArg) |
| 608 |
: color(colorArg), count(*countArg) { |
| 609 |
count = 0; |
| 610 |
} |
| 611 |
|
| 612 |
bool operator()(JSObject* obj) { |
| 613 |
if (!CheckCellColor(obj, color)) { |
| 614 |
return false; |
| 615 |
} |
| 616 |
|
| 617 |
NativeObject& nobj = obj->as<NativeObject>(); |
| 618 |
if (!CheckCellColor(nobj.shape(), color)) { |
| 619 |
return false; |
| 620 |
} |
| 621 |
|
| 622 |
NativeShape* shape = nobj.shape(); |
| 623 |
if (!CheckCellColor(shape, color)) { |
| 624 |
return false; |
| 625 |
} |
| 626 |
|
| 627 |
// Shapes and symbols are never marked gray. |
| 628 |
ShapePropertyIter<NoGC> iter(shape); |
| 629 |
jsid id = iter->key(); |
| 630 |
if (id.isGCThing() && |
| 631 |
!CheckCellColor(id.toGCCellPtr().asCell(), MarkColor::Black)) { |
| 632 |
return false; |
| 633 |
} |
| 634 |
|
| 635 |
count++; |
| 636 |
return true; |
| 637 |
} |
| 638 |
}; |
| 639 |
|
| 640 |
JS::PersistentRootedObject global1; |
| 641 |
JS::PersistentRootedObject global2; |
| 642 |
|
| 643 |
struct GrayRoots { |
| 644 |
JS::Heap<JSObject*> grayRoot1; |
| 645 |
JS::Heap<JSObject*> grayRoot2; |
| 646 |
}; |
| 647 |
|
| 648 |
GrayRoots grayRoots; |
| 649 |
|
| 650 |
bool InitGlobals() { |
| 651 |
global1.init(cx, global); |
| 652 |
if (!createGlobal()) { |
| 653 |
return false; |
| 654 |
} |
| 655 |
global2.init(cx, global); |
| 656 |
return global2 != nullptr; |
| 657 |
} |
| 658 |
|
| 659 |
void ClearGrayRoots() { |
| 660 |
grayRoots.grayRoot1 = nullptr; |
| 661 |
grayRoots.grayRoot2 = nullptr; |
| 662 |
} |
| 663 |
|
| 664 |
void InitGrayRootTracer() { |
| 665 |
ClearGrayRoots(); |
| 666 |
JS_SetGrayGCRootsTracer(cx, TraceGrayRoots, &grayRoots); |
| 667 |
} |
| 668 |
|
| 669 |
void RemoveGrayRootTracer() { |
| 670 |
ClearGrayRoots(); |
| 671 |
JS_SetGrayGCRootsTracer(cx, nullptr, nullptr); |
| 672 |
} |
| 673 |
|
| 674 |
static bool TraceGrayRoots(JSTracer* trc, JS::SliceBudget& budget, void* data) { |
| 675 |
auto grayRoots = static_cast<GrayRoots*>(data); |
| 676 |
TraceEdge(trc, &grayRoots->grayRoot1, "gray root 1"); |
| 677 |
TraceEdge(trc, &grayRoots->grayRoot2, "gray root 2"); |
| 678 |
return true; |
| 679 |
} |
| 680 |
|
| 681 |
JSObject* AllocPlainObject() { |
| 682 |
JS::RootedObject obj(cx, JS_NewPlainObject(cx)); |
| 683 |
EvictNursery(); |
| 684 |
|
| 685 |
MOZ_ASSERT(obj->compartment() == global1->compartment()); |
| 686 |
return obj; |
| 687 |
} |
| 688 |
|
| 689 |
JSObject* AllocSameCompartmentSourceObject(JSObject* target) { |
| 690 |
JS::RootedObject source(cx, JS_NewPlainObject(cx)); |
| 691 |
if (!source) { |
| 692 |
return nullptr; |
| 693 |
} |
| 694 |
|
| 695 |
JS::RootedObject obj(cx, target); |
| 696 |
if (!JS_DefineProperty(cx, source, "ptr", obj, 0)) { |
| 697 |
return nullptr; |
| 698 |
} |
| 699 |
|
| 700 |
EvictNursery(); |
| 701 |
|
| 702 |
MOZ_ASSERT(source->compartment() == global1->compartment()); |
| 703 |
return source; |
| 704 |
} |
| 705 |
|
| 706 |
JSObject* GetCrossCompartmentWrapper(JSObject* target) { |
| 707 |
MOZ_ASSERT(target->compartment() == global1->compartment()); |
| 708 |
JS::RootedObject obj(cx, target); |
| 709 |
JSAutoRealm ar(cx, global2); |
| 710 |
if (!JS_WrapObject(cx, &obj)) { |
| 711 |
return nullptr; |
| 712 |
} |
| 713 |
|
| 714 |
EvictNursery(); |
| 715 |
|
| 716 |
MOZ_ASSERT(obj->compartment() == global2->compartment()); |
| 717 |
return obj; |
| 718 |
} |
| 719 |
|
| 720 |
JSObject* AllocWeakmapKeyObject() { |
| 721 |
JS::RootedObject delegate(cx, JS_NewPlainObject(cx)); |
| 722 |
if (!delegate) { |
| 723 |
return nullptr; |
| 724 |
} |
| 725 |
|
| 726 |
JS::RootedObject key(cx, |
| 727 |
js::Wrapper::New(cx, delegate, &js::Wrapper::singleton)); |
| 728 |
|
| 729 |
EvictNursery(); |
| 730 |
return key; |
| 731 |
} |
| 732 |
|
| 733 |
JSObject* AllocObjectChain(size_t length) { |
| 734 |
// Allocate a chain of linked JSObjects. |
| 735 |
|
| 736 |
// Use a unique property name so the shape is not shared with any other |
| 737 |
// objects. |
| 738 |
RootedString nextPropName(cx, JS_NewStringCopyZ(cx, "unique14142135")); |
| 739 |
RootedId nextId(cx); |
| 740 |
if (!JS_StringToId(cx, nextPropName, &nextId)) { |
| 741 |
return nullptr; |
| 742 |
} |
| 743 |
|
| 744 |
RootedObject head(cx); |
| 745 |
for (size_t i = 0; i < length; i++) { |
| 746 |
RootedValue next(cx, ObjectOrNullValue(head)); |
| 747 |
head = AllocPlainObject(); |
| 748 |
if (!head) { |
| 749 |
return nullptr; |
| 750 |
} |
| 751 |
if (!JS_DefinePropertyById(cx, head, nextId, next, 0)) { |
| 752 |
return nullptr; |
| 753 |
} |
| 754 |
} |
| 755 |
|
| 756 |
return head; |
| 757 |
} |
| 758 |
|
| 759 |
template <typename F> |
| 760 |
bool IterateObjectChain(JSObject* chain, F f) { |
| 761 |
RootedObject obj(cx, chain); |
| 762 |
while (obj) { |
| 763 |
if (!f(obj)) { |
| 764 |
return false; |
| 765 |
} |
| 766 |
|
| 767 |
// Access the 'next' property via the object's slots to avoid triggering |
| 768 |
// gray marking assertions when calling JS_GetPropertyById. |
| 769 |
NativeObject& nobj = obj->as<NativeObject>(); |
| 770 |
MOZ_ASSERT(nobj.slotSpan() == 1); |
| 771 |
obj = nobj.getSlot(0).toObjectOrNull(); |
| 772 |
} |
| 773 |
|
| 774 |
return true; |
| 775 |
} |
| 776 |
|
| 777 |
static bool IsMarkedBlack(Cell* cell) { |
| 778 |
TenuredCell* tc = &cell->asTenured(); |
| 779 |
return tc->isMarkedBlack(); |
| 780 |
} |
| 781 |
|
| 782 |
static bool IsMarkedGray(Cell* cell) { |
| 783 |
TenuredCell* tc = &cell->asTenured(); |
| 784 |
bool isGray = tc->isMarkedGray(); |
| 785 |
MOZ_ASSERT_IF(isGray, tc->isMarkedAny()); |
| 786 |
return isGray; |
| 787 |
} |
| 788 |
|
| 789 |
static bool CheckCellColor(Cell* cell, MarkColor color) { |
| 790 |
MOZ_ASSERT(color == MarkColor::Black || color == MarkColor::Gray); |
| 791 |
if (color == MarkColor::Black && !IsMarkedBlack(cell)) { |
| 792 |
printf("Found non-black cell: %p\n", cell); |
| 793 |
return false; |
| 794 |
} else if (color == MarkColor::Gray && !IsMarkedGray(cell)) { |
| 795 |
printf("Found non-gray cell: %p\n", cell); |
| 796 |
return false; |
| 797 |
} |
| 798 |
|
| 799 |
return true; |
| 800 |
} |
| 801 |
|
| 802 |
void EvictNursery() { cx->runtime()->gc.evictNursery(); } |
| 803 |
|
| 804 |
bool ZoneGC(JS::Zone* zone) { |
| 805 |
JS::PrepareZoneForGC(cx, zone); |
| 806 |
cx->runtime()->gc.gc(JS::GCOptions::Normal, JS::GCReason::API); |
| 807 |
CHECK(!cx->runtime()->gc.isFullGc()); |
| 808 |
return true; |
| 809 |
} |
| 810 |
|
| 811 |
END_TEST(testGCGrayMarking) |