| 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 "gc/Policy.h" |
| 9 |
#include "gc/Zone.h" |
| 10 |
#include "js/GCHashTable.h" |
| 11 |
#include "js/RootingAPI.h" |
| 12 |
#include "js/SweepingAPI.h" |
| 13 |
|
| 14 |
#include "jsapi-tests/tests.h" |
| 15 |
|
| 16 |
using namespace js; |
| 17 |
|
| 18 |
// Exercise WeakCache<GCHashSet>. |
| 19 |
BEGIN_TEST(testWeakCacheSet) { |
| 20 |
// Create two objects tenured and two in the nursery. If zeal is on, |
| 21 |
// this may fail and we'll get more tenured objects. That's fine: |
| 22 |
// the test will continue to work, it will just not test as much. |
| 23 |
JS::RootedObject tenured1(cx, JS_NewPlainObject(cx)); |
| 24 |
JS::RootedObject tenured2(cx, JS_NewPlainObject(cx)); |
| 25 |
JS_GC(cx); |
| 26 |
JS::RootedObject nursery1(cx, JS_NewPlainObject(cx)); |
| 27 |
JS::RootedObject nursery2(cx, JS_NewPlainObject(cx)); |
| 28 |
|
| 29 |
using ObjectSet = |
| 30 |
GCHashSet<HeapPtr<JSObject*>, StableCellHasher<HeapPtr<JSObject*>>, |
| 31 |
SystemAllocPolicy>; |
| 32 |
using Cache = JS::WeakCache<ObjectSet>; |
| 33 |
Cache cache(JS::GetObjectZone(tenured1)); |
| 34 |
|
| 35 |
cache.put(tenured1); |
| 36 |
cache.put(tenured2); |
| 37 |
cache.put(nursery1); |
| 38 |
cache.put(nursery2); |
| 39 |
|
| 40 |
// Verify relocation and that we don't sweep too aggressively. |
| 41 |
JS_GC(cx); |
| 42 |
CHECK(cache.has(tenured1)); |
| 43 |
CHECK(cache.has(tenured2)); |
| 44 |
CHECK(cache.has(nursery1)); |
| 45 |
CHECK(cache.has(nursery2)); |
| 46 |
|
| 47 |
// Unroot two entries and verify that they get removed. |
| 48 |
tenured2 = nursery2 = nullptr; |
| 49 |
JS_GC(cx); |
| 50 |
CHECK(cache.has(tenured1)); |
| 51 |
CHECK(cache.has(nursery1)); |
| 52 |
CHECK(cache.count() == 2); |
| 53 |
|
| 54 |
return true; |
| 55 |
} |
| 56 |
END_TEST(testWeakCacheSet) |
| 57 |
|
| 58 |
BEGIN_TEST(testWeakCacheSetWithJSHeap) { |
| 59 |
// Tests basically the same thing as testWeakCacheSet, but only using APIs |
| 60 |
// available to embedders. |
| 61 |
JS::RootedObject tenured1{cx, JS_NewPlainObject(cx)}; |
| 62 |
JS::RootedObject tenured2{cx, JS_NewPlainObject(cx)}; |
| 63 |
JS_GC(cx); |
| 64 |
JS::RootedObject nursery1{cx, JS_NewPlainObject(cx)}; |
| 65 |
JS::RootedObject nursery2{cx, JS_NewPlainObject(cx)}; |
| 66 |
|
| 67 |
using ObjectSet = JS::GCHashSet<JS::Heap<JSObject*>, |
| 68 |
js::StableCellHasher<JS::Heap<JSObject*>>, |
| 69 |
js::SystemAllocPolicy>; |
| 70 |
using Cache = JS::WeakCache<ObjectSet>; |
| 71 |
Cache cache{JS::GetObjectZone(tenured1)}; |
| 72 |
|
| 73 |
cache.put(tenured1); |
| 74 |
cache.put(tenured2); |
| 75 |
cache.put(nursery1); |
| 76 |
cache.put(nursery2); |
| 77 |
cache.put(nullptr); // nullptr entries should not be swept. |
| 78 |
|
| 79 |
// Verify relocation and that we don't sweep too aggressively. |
| 80 |
JS_GC(cx); |
| 81 |
CHECK(cache.has(tenured1)); |
| 82 |
CHECK(cache.has(tenured2)); |
| 83 |
CHECK(cache.has(nursery1)); |
| 84 |
CHECK(cache.has(nursery2)); |
| 85 |
CHECK(cache.has(nullptr)); |
| 86 |
|
| 87 |
// Unroot two entries and verify that they get removed. |
| 88 |
tenured2 = nursery2 = nullptr; |
| 89 |
JS_GC(cx); |
| 90 |
CHECK(cache.has(tenured1)); |
| 91 |
CHECK(cache.has(nursery1)); |
| 92 |
CHECK(cache.has(nullptr)); |
| 93 |
CHECK_EQUAL(cache.count(), unsigned{3}); |
| 94 |
|
| 95 |
return true; |
| 96 |
} |
| 97 |
END_TEST(testWeakCacheSetWithJSHeap) |
| 98 |
|
| 99 |
// Exercise WeakCache<GCHashMap>. |
| 100 |
BEGIN_TEST(testWeakCacheMap) { |
| 101 |
// Create two objects tenured and two in the nursery. If zeal is on, |
| 102 |
// this may fail and we'll get more tenured objects. That's fine: |
| 103 |
// the test will continue to work, it will just not test as much. |
| 104 |
JS::RootedObject tenured1(cx, JS_NewPlainObject(cx)); |
| 105 |
JS::RootedObject tenured2(cx, JS_NewPlainObject(cx)); |
| 106 |
JS_GC(cx); |
| 107 |
JS::RootedObject nursery1(cx, JS_NewPlainObject(cx)); |
| 108 |
JS::RootedObject nursery2(cx, JS_NewPlainObject(cx)); |
| 109 |
|
| 110 |
using ObjectMap = js::GCHashMap<HeapPtr<JSObject*>, uint32_t, |
| 111 |
js::StableCellHasher<HeapPtr<JSObject*>>>; |
| 112 |
using Cache = JS::WeakCache<ObjectMap>; |
| 113 |
Cache cache(JS::GetObjectZone(tenured1), cx); |
| 114 |
|
| 115 |
cache.put(tenured1, 1); |
| 116 |
cache.put(tenured2, 2); |
| 117 |
cache.put(nursery1, 3); |
| 118 |
cache.put(nursery2, 4); |
| 119 |
|
| 120 |
JS_GC(cx); |
| 121 |
CHECK(cache.has(tenured1)); |
| 122 |
CHECK(cache.has(tenured2)); |
| 123 |
CHECK(cache.has(nursery1)); |
| 124 |
CHECK(cache.has(nursery2)); |
| 125 |
|
| 126 |
tenured2 = nursery2 = nullptr; |
| 127 |
JS_GC(cx); |
| 128 |
CHECK(cache.has(tenured1)); |
| 129 |
CHECK(cache.has(nursery1)); |
| 130 |
CHECK(cache.count() == 2); |
| 131 |
|
| 132 |
return true; |
| 133 |
} |
| 134 |
END_TEST(testWeakCacheMap) |
| 135 |
|
| 136 |
BEGIN_TEST(testWeakCacheMapWithUniquePtr) { |
| 137 |
JS::RootedObject tenured1(cx, JS_NewPlainObject(cx)); |
| 138 |
JS::RootedObject tenured2(cx, JS_NewPlainObject(cx)); |
| 139 |
JS_GC(cx); |
| 140 |
JS::RootedObject nursery1(cx, JS_NewPlainObject(cx)); |
| 141 |
JS::RootedObject nursery2(cx, JS_NewPlainObject(cx)); |
| 142 |
|
| 143 |
using ObjectMap = js::GCHashMap<HeapPtr<JSObject*>, UniquePtr<uint32_t>, |
| 144 |
js::StableCellHasher<HeapPtr<JSObject*>>>; |
| 145 |
using Cache = JS::WeakCache<ObjectMap>; |
| 146 |
Cache cache(JS::GetObjectZone(tenured1), cx); |
| 147 |
|
| 148 |
cache.put(tenured1, MakeUnique<uint32_t>(1)); |
| 149 |
cache.put(tenured2, MakeUnique<uint32_t>(2)); |
| 150 |
cache.put(nursery1, MakeUnique<uint32_t>(3)); |
| 151 |
cache.put(nursery2, MakeUnique<uint32_t>(4)); |
| 152 |
|
| 153 |
JS_GC(cx); |
| 154 |
CHECK(cache.has(tenured1)); |
| 155 |
CHECK(cache.has(tenured2)); |
| 156 |
CHECK(cache.has(nursery1)); |
| 157 |
CHECK(cache.has(nursery2)); |
| 158 |
|
| 159 |
tenured2 = nursery2 = nullptr; |
| 160 |
JS_GC(cx); |
| 161 |
CHECK(cache.has(tenured1)); |
| 162 |
CHECK(cache.has(nursery1)); |
| 163 |
CHECK(cache.count() == 2); |
| 164 |
|
| 165 |
return true; |
| 166 |
} |
| 167 |
END_TEST(testWeakCacheMapWithUniquePtr) |
| 168 |
|
| 169 |
BEGIN_TEST(testWeakCacheMapWithJSHeap) { |
| 170 |
// Tests basically the same thing as testWeakCacheMap, but only using APIs |
| 171 |
// available to embedders, and with JS::Heap as both keys and values. |
| 172 |
JS::RootedObject tenured1{cx, JS_NewPlainObject(cx)}; |
| 173 |
JS::RootedObject tenured2{cx, JS_NewPlainObject(cx)}; |
| 174 |
JS_GC(cx); |
| 175 |
JS::RootedObject nursery1{cx, JS_NewPlainObject(cx)}; |
| 176 |
JS::RootedObject nursery2{cx, JS_NewPlainObject(cx)}; |
| 177 |
|
| 178 |
using ObjectMap = JS::GCHashMap<JS::Heap<JSObject*>, JS::Heap<JSObject*>, |
| 179 |
js::StableCellHasher<JS::Heap<JSObject*>>, |
| 180 |
js::SystemAllocPolicy>; |
| 181 |
using Cache = JS::WeakCache<ObjectMap>; |
| 182 |
Cache cache{JS::GetObjectZone(tenured1)}; |
| 183 |
|
| 184 |
cache.put(tenured1, tenured1); |
| 185 |
cache.put(tenured2, tenured2); |
| 186 |
cache.put(nursery1, nursery2); // These do not map to themselves. |
| 187 |
cache.put(nursery2, nursery1); |
| 188 |
cache.put(nullptr, nullptr); // nullptr entries should not be swept. |
| 189 |
|
| 190 |
JS_GC(cx); |
| 191 |
CHECK(cache.has(tenured1)); |
| 192 |
CHECK(cache.has(tenured2)); |
| 193 |
CHECK(cache.has(nursery1)); |
| 194 |
CHECK(cache.has(nursery2)); |
| 195 |
CHECK(cache.has(nullptr)); |
| 196 |
|
| 197 |
tenured2 = nursery2 = nullptr; |
| 198 |
JS_GC(cx); |
| 199 |
CHECK(cache.has(tenured1)); |
| 200 |
// nursery1 is gone because value is dead. |
| 201 |
CHECK(cache.has(nullptr)); |
| 202 |
CHECK_EQUAL(cache.count(), unsigned{2}); |
| 203 |
|
| 204 |
return true; |
| 205 |
} |
| 206 |
END_TEST(testWeakCacheMapWithJSHeap) |
| 207 |
|
| 208 |
// Exercise WeakCache<GCVector>. |
| 209 |
BEGIN_TEST(testWeakCacheGCVector) { |
| 210 |
// Create two objects tenured and two in the nursery. If zeal is on, |
| 211 |
// this may fail and we'll get more tenured objects. That's fine: |
| 212 |
// the test will continue to work, it will just not test as much. |
| 213 |
JS::RootedObject tenured1(cx, JS_NewPlainObject(cx)); |
| 214 |
JS::RootedObject tenured2(cx, JS_NewPlainObject(cx)); |
| 215 |
JS_GC(cx); |
| 216 |
JS::RootedObject nursery1(cx, JS_NewPlainObject(cx)); |
| 217 |
JS::RootedObject nursery2(cx, JS_NewPlainObject(cx)); |
| 218 |
|
| 219 |
using ObjectVector = JS::WeakCache<GCVector<HeapPtr<JSObject*>>>; |
| 220 |
ObjectVector cache(JS::GetObjectZone(tenured1), cx); |
| 221 |
|
| 222 |
CHECK(cache.append(tenured1)); |
| 223 |
CHECK(cache.append(tenured2)); |
| 224 |
CHECK(cache.append(nursery1)); |
| 225 |
CHECK(cache.append(nursery2)); |
| 226 |
|
| 227 |
JS_GC(cx); |
| 228 |
CHECK(cache.get().length() == 4); |
| 229 |
CHECK(cache.get()[0] == tenured1); |
| 230 |
CHECK(cache.get()[1] == tenured2); |
| 231 |
CHECK(cache.get()[2] == nursery1); |
| 232 |
CHECK(cache.get()[3] == nursery2); |
| 233 |
|
| 234 |
tenured2 = nursery2 = nullptr; |
| 235 |
JS_GC(cx); |
| 236 |
CHECK(cache.get().length() == 2); |
| 237 |
CHECK(cache.get()[0] == tenured1); |
| 238 |
CHECK(cache.get()[1] == nursery1); |
| 239 |
|
| 240 |
return true; |
| 241 |
} |
| 242 |
END_TEST(testWeakCacheGCVector) |
| 243 |
|
| 244 |
BEGIN_TEST(testWeakCacheGCVectorWithJSHeap) { |
| 245 |
// Tests basically the same thing as testWeakCacheGCVector, but only using |
| 246 |
// APIs available to embedders. |
| 247 |
JS::RootedObject tenured1{cx, JS_NewPlainObject(cx)}; |
| 248 |
JS::RootedObject tenured2{cx, JS_NewPlainObject(cx)}; |
| 249 |
JS_GC(cx); |
| 250 |
JS::RootedObject nursery1{cx, JS_NewPlainObject(cx)}; |
| 251 |
JS::RootedObject nursery2{cx, JS_NewPlainObject(cx)}; |
| 252 |
|
| 253 |
using ObjectVector = JS::WeakCache< |
| 254 |
JS::GCVector<JS::Heap<JSObject*>, 0, js::SystemAllocPolicy>>; |
| 255 |
ObjectVector cache{JS::GetObjectZone(tenured1)}; |
| 256 |
|
| 257 |
CHECK(cache.append(tenured1)); |
| 258 |
CHECK(cache.append(tenured2)); |
| 259 |
CHECK(cache.append(nursery1)); |
| 260 |
CHECK(cache.append(nursery2)); |
| 261 |
CHECK(cache.append(nullptr)); // nullptr entries should not be swept. |
| 262 |
CHECK_EQUAL(cache.get().length(), unsigned{5}); |
| 263 |
|
| 264 |
JS_GC(cx); |
| 265 |
CHECK_EQUAL(cache.get().length(), unsigned{5}); |
| 266 |
CHECK_EQUAL(cache.get()[0], tenured1); |
| 267 |
CHECK_EQUAL(cache.get()[1], tenured2); |
| 268 |
CHECK_EQUAL(cache.get()[2], nursery1); |
| 269 |
CHECK_EQUAL(cache.get()[3], nursery2); |
| 270 |
CHECK_NULL(cache.get()[4].get()); |
| 271 |
|
| 272 |
tenured2 = nursery2 = nullptr; |
| 273 |
JS_GC(cx); |
| 274 |
CHECK_EQUAL(cache.get().length(), unsigned{3}); |
| 275 |
CHECK_EQUAL(cache.get()[0], tenured1); |
| 276 |
CHECK_EQUAL(cache.get()[1], nursery1); |
| 277 |
CHECK_NULL(cache.get()[2].get()); |
| 278 |
|
| 279 |
return true; |
| 280 |
} |
| 281 |
END_TEST(testWeakCacheGCVectorWithJSHeap) |
| 282 |
|
| 283 |
#ifdef JS_GC_ZEAL |
| 284 |
|
| 285 |
// A simple structure that embeds an object pointer. We cripple the hash |
| 286 |
// implementation so that we can test hash table collisions. |
| 287 |
struct ObjectEntry { |
| 288 |
HeapPtr<JSObject*> obj; |
| 289 |
explicit ObjectEntry(JSObject* o) : obj(o) {} |
| 290 |
bool operator==(const ObjectEntry& other) const { return obj == other.obj; } |
| 291 |
bool traceWeak(JSTracer* trc) { |
| 292 |
return TraceWeakEdge(trc, &obj, "ObjectEntry::obj"); |
| 293 |
} |
| 294 |
}; |
| 295 |
|
| 296 |
namespace js { |
| 297 |
template <> |
| 298 |
struct StableCellHasher<ObjectEntry> { |
| 299 |
using Key = ObjectEntry; |
| 300 |
using Lookup = JSObject*; |
| 301 |
|
| 302 |
static bool maybeGetHash(const Lookup& l, HashNumber* hashOut) { |
| 303 |
if (!StableCellHasher<JSObject*>::maybeGetHash(l, hashOut)) { |
| 304 |
return false; |
| 305 |
} |
| 306 |
// Reduce hash code to single bit to generate hash collisions. |
| 307 |
*hashOut &= 0x1; |
| 308 |
return true; |
| 309 |
} |
| 310 |
static bool ensureHash(const Lookup& l, HashNumber* hashOut) { |
| 311 |
if (!StableCellHasher<JSObject*>::ensureHash(l, hashOut)) { |
| 312 |
return false; |
| 313 |
} |
| 314 |
// Reduce hash code to single bit to generate hash collisions. |
| 315 |
*hashOut &= 0x1; |
| 316 |
return true; |
| 317 |
} |
| 318 |
static HashNumber hash(const Lookup& l) { |
| 319 |
// Reduce hash code to single bit to generate hash collisions. |
| 320 |
return StableCellHasher<HeapPtr<JSObject*>>::hash(l) & 0x1; |
| 321 |
} |
| 322 |
static bool match(const Key& k, const Lookup& l) { |
| 323 |
return StableCellHasher<HeapPtr<JSObject*>>::match(k.obj, l); |
| 324 |
} |
| 325 |
}; |
| 326 |
} // namespace js |
| 327 |
|
| 328 |
// A structure that contains a pointer to a JSObject but is keyed based on an |
| 329 |
// integer. This lets us test replacing dying entries in a set. |
| 330 |
struct NumberAndObjectEntry { |
| 331 |
uint32_t number; |
| 332 |
HeapPtr<JSObject*> obj; |
| 333 |
|
| 334 |
NumberAndObjectEntry(uint32_t n, JSObject* o) : number(n), obj(o) {} |
| 335 |
bool operator==(const NumberAndObjectEntry& other) const { |
| 336 |
return number == other.number && obj == other.obj; |
| 337 |
} |
| 338 |
bool traceWeak(JSTracer* trc) { |
| 339 |
return TraceWeakEdge(trc, &obj, "NumberAndObjectEntry::obj"); |
| 340 |
} |
| 341 |
}; |
| 342 |
|
| 343 |
struct NumberAndObjectLookup { |
| 344 |
uint32_t number; |
| 345 |
HeapPtr<JSObject*> obj; |
| 346 |
|
| 347 |
NumberAndObjectLookup(uint32_t n, JSObject* o) : number(n), obj(o) {} |
| 348 |
MOZ_IMPLICIT NumberAndObjectLookup(const NumberAndObjectEntry& entry) |
| 349 |
: number(entry.number), obj(entry.obj) {} |
| 350 |
}; |
| 351 |
|
| 352 |
namespace js { |
| 353 |
template <> |
| 354 |
struct StableCellHasher<NumberAndObjectEntry> { |
| 355 |
using Key = NumberAndObjectEntry; |
| 356 |
using Lookup = NumberAndObjectLookup; |
| 357 |
|
| 358 |
static bool maybeGetHash(const Lookup& l, HashNumber* hashOut) { |
| 359 |
if (!StableCellHasher<JSObject*>::maybeGetHash(l.obj, hashOut)) { |
| 360 |
return false; |
| 361 |
} |
| 362 |
*hashOut ^= l.number; |
| 363 |
return true; |
| 364 |
} |
| 365 |
static bool ensureHash(const Lookup& l, HashNumber* hashOut) { |
| 366 |
if (!StableCellHasher<JSObject*>::ensureHash(l.obj, hashOut)) { |
| 367 |
return false; |
| 368 |
} |
| 369 |
*hashOut ^= l.number; |
| 370 |
return true; |
| 371 |
} |
| 372 |
static HashNumber hash(const Lookup& l) { |
| 373 |
return StableCellHasher<HeapPtr<JSObject*>>::hash(l.obj) ^ l.number; |
| 374 |
} |
| 375 |
static bool match(const Key& k, const Lookup& l) { |
| 376 |
return k.number == l.number && |
| 377 |
StableCellHasher<HeapPtr<JSObject*>>::match(k.obj, l.obj); |
| 378 |
} |
| 379 |
}; |
| 380 |
} // namespace js |
| 381 |
|
| 382 |
BEGIN_TEST(testIncrementalWeakCacheSweeping) { |
| 383 |
AutoLeaveZeal nozeal(cx); |
| 384 |
|
| 385 |
JS_SetGCParameter(cx, JSGC_INCREMENTAL_GC_ENABLED, true); |
| 386 |
JS::SetGCZeal(cx, 17, 1000000); |
| 387 |
|
| 388 |
CHECK(TestSet()); |
| 389 |
CHECK(TestMap()); |
| 390 |
CHECK(TestReplaceDyingInSet()); |
| 391 |
CHECK(TestReplaceDyingInMap()); |
| 392 |
CHECK(TestUniqueIDLookups()); |
| 393 |
|
| 394 |
JS::SetGCZeal(cx, 0, 0); |
| 395 |
JS_SetGCParameter(cx, JSGC_INCREMENTAL_GC_ENABLED, false); |
| 396 |
|
| 397 |
return true; |
| 398 |
} |
| 399 |
|
| 400 |
template <typename Cache> |
| 401 |
bool GCUntilCacheSweep(JSContext* cx, const Cache& cache) { |
| 402 |
CHECK(!IsIncrementalGCInProgress(cx)); |
| 403 |
|
| 404 |
JS::Zone* zone = JS::GetObjectZone(global); |
| 405 |
JS::PrepareZoneForGC(cx, zone); |
| 406 |
JS::SliceBudget budget(JS::WorkBudget(1)); |
| 407 |
cx->runtime()->gc.startDebugGC(JS::GCOptions::Normal, budget); |
| 408 |
|
| 409 |
CHECK(IsIncrementalGCInProgress(cx)); |
| 410 |
CHECK(zone->isGCSweeping()); |
| 411 |
CHECK(cache.needsIncrementalBarrier()); |
| 412 |
|
| 413 |
return true; |
| 414 |
} |
| 415 |
|
| 416 |
template <typename Cache> |
| 417 |
bool SweepCacheAndFinishGC(JSContext* cx, const Cache& cache) { |
| 418 |
CHECK(IsIncrementalGCInProgress(cx)); |
| 419 |
|
| 420 |
PrepareForIncrementalGC(cx); |
| 421 |
IncrementalGCSlice(cx, JS::GCReason::API, JS::SliceBudget::unlimited()); |
| 422 |
|
| 423 |
JS::Zone* zone = JS::GetObjectZone(global); |
| 424 |
CHECK(!IsIncrementalGCInProgress(cx)); |
| 425 |
CHECK(!zone->isCollecting()); |
| 426 |
CHECK(!cache.needsIncrementalBarrier()); |
| 427 |
|
| 428 |
return true; |
| 429 |
} |
| 430 |
|
| 431 |
bool TestSet() { |
| 432 |
using ObjectSet = |
| 433 |
GCHashSet<HeapPtr<JSObject*>, StableCellHasher<HeapPtr<JSObject*>>, |
| 434 |
TempAllocPolicy>; |
| 435 |
using Cache = JS::WeakCache<ObjectSet>; |
| 436 |
Cache cache(JS::GetObjectZone(global), cx); |
| 437 |
|
| 438 |
// Sweep empty cache. |
| 439 |
|
| 440 |
CHECK(cache.empty()); |
| 441 |
JS_GC(cx); |
| 442 |
CHECK(cache.empty()); |
| 443 |
|
| 444 |
// Add an entry while sweeping. |
| 445 |
|
| 446 |
JS::RootedObject obj1(cx, JS_NewPlainObject(cx)); |
| 447 |
JS::RootedObject obj2(cx, JS_NewPlainObject(cx)); |
| 448 |
JS::RootedObject obj3(cx, JS_NewPlainObject(cx)); |
| 449 |
JS::RootedObject obj4(cx, JS_NewPlainObject(cx)); |
| 450 |
CHECK(obj1); |
| 451 |
CHECK(obj2); |
| 452 |
CHECK(obj3); |
| 453 |
CHECK(obj4); |
| 454 |
|
| 455 |
CHECK(!cache.has(obj1)); |
| 456 |
CHECK(cache.put(obj1)); |
| 457 |
CHECK(cache.count() == 1); |
| 458 |
CHECK(cache.has(obj1)); |
| 459 |
CHECK(*cache.lookup(obj1) == obj1); |
| 460 |
|
| 461 |
CHECK(GCUntilCacheSweep(cx, cache)); |
| 462 |
|
| 463 |
CHECK(!cache.has(obj2)); |
| 464 |
CHECK(cache.put(obj2)); |
| 465 |
CHECK(cache.has(obj2)); |
| 466 |
CHECK(*cache.lookup(obj2) == obj2); |
| 467 |
|
| 468 |
CHECK(SweepCacheAndFinishGC(cx, cache)); |
| 469 |
|
| 470 |
CHECK(cache.count() == 2); |
| 471 |
CHECK(cache.has(obj1)); |
| 472 |
CHECK(cache.has(obj2)); |
| 473 |
|
| 474 |
// Test dying entries are not found while sweeping. |
| 475 |
|
| 476 |
CHECK(cache.put(obj3)); |
| 477 |
CHECK(cache.put(obj4)); |
| 478 |
void* old3 = obj3; |
| 479 |
void* old4 = obj4; |
| 480 |
obj3 = obj4 = nullptr; |
| 481 |
|
| 482 |
CHECK(GCUntilCacheSweep(cx, cache)); |
| 483 |
|
| 484 |
CHECK(cache.has(obj1)); |
| 485 |
CHECK(cache.has(obj2)); |
| 486 |
CHECK(!cache.has(static_cast<JSObject*>(old3))); |
| 487 |
CHECK(!cache.has(static_cast<JSObject*>(old4))); |
| 488 |
|
| 489 |
size_t count = 0; |
| 490 |
for (auto r = cache.all(); !r.empty(); r.popFront()) { |
| 491 |
CHECK(r.front() == obj1 || r.front() == obj2); |
| 492 |
count++; |
| 493 |
} |
| 494 |
CHECK(count == 2); |
| 495 |
|
| 496 |
CHECK(SweepCacheAndFinishGC(cx, cache)); |
| 497 |
|
| 498 |
CHECK(cache.count() == 2); |
| 499 |
|
| 500 |
// Test lookupForAdd while sweeping. |
| 501 |
|
| 502 |
obj3 = JS_NewPlainObject(cx); |
| 503 |
obj4 = JS_NewPlainObject(cx); |
| 504 |
CHECK(obj3); |
| 505 |
CHECK(obj4); |
| 506 |
|
| 507 |
CHECK(cache.lookupForAdd(obj1)); |
| 508 |
CHECK(*cache.lookupForAdd(obj1) == obj1); |
| 509 |
|
| 510 |
auto addp = cache.lookupForAdd(obj3); |
| 511 |
CHECK(!addp); |
| 512 |
CHECK(cache.add(addp, obj3)); |
| 513 |
CHECK(cache.has(obj3)); |
| 514 |
|
| 515 |
CHECK(GCUntilCacheSweep(cx, cache)); |
| 516 |
|
| 517 |
addp = cache.lookupForAdd(obj4); |
| 518 |
CHECK(!addp); |
| 519 |
CHECK(cache.add(addp, obj4)); |
| 520 |
CHECK(cache.has(obj4)); |
| 521 |
|
| 522 |
CHECK(SweepCacheAndFinishGC(cx, cache)); |
| 523 |
|
| 524 |
CHECK(cache.count() == 4); |
| 525 |
CHECK(cache.has(obj3)); |
| 526 |
CHECK(cache.has(obj4)); |
| 527 |
|
| 528 |
// Test remove while sweeping. |
| 529 |
|
| 530 |
cache.remove(obj3); |
| 531 |
|
| 532 |
CHECK(GCUntilCacheSweep(cx, cache)); |
| 533 |
|
| 534 |
cache.remove(obj4); |
| 535 |
|
| 536 |
CHECK(SweepCacheAndFinishGC(cx, cache)); |
| 537 |
|
| 538 |
CHECK(cache.count() == 2); |
| 539 |
CHECK(!cache.has(obj3)); |
| 540 |
CHECK(!cache.has(obj4)); |
| 541 |
|
| 542 |
// Test putNew while sweeping. |
| 543 |
|
| 544 |
CHECK(GCUntilCacheSweep(cx, cache)); |
| 545 |
|
| 546 |
CHECK(cache.putNew(obj3)); |
| 547 |
CHECK(cache.putNew(obj4, obj4)); |
| 548 |
|
| 549 |
CHECK(SweepCacheAndFinishGC(cx, cache)); |
| 550 |
|
| 551 |
CHECK(cache.count() == 4); |
| 552 |
CHECK(cache.has(obj3)); |
| 553 |
CHECK(cache.has(obj4)); |
| 554 |
|
| 555 |
cache.clear(); |
| 556 |
|
| 557 |
return true; |
| 558 |
} |
| 559 |
|
| 560 |
bool TestMap() { |
| 561 |
using ObjectMap = |
| 562 |
GCHashMap<HeapPtr<JSObject*>, uint32_t, |
| 563 |
StableCellHasher<HeapPtr<JSObject*>>, TempAllocPolicy>; |
| 564 |
using Cache = JS::WeakCache<ObjectMap>; |
| 565 |
Cache cache(JS::GetObjectZone(global), cx); |
| 566 |
|
| 567 |
// Sweep empty cache. |
| 568 |
|
| 569 |
CHECK(cache.empty()); |
| 570 |
JS_GC(cx); |
| 571 |
CHECK(cache.empty()); |
| 572 |
|
| 573 |
// Add an entry while sweeping. |
| 574 |
|
| 575 |
JS::RootedObject obj1(cx, JS_NewPlainObject(cx)); |
| 576 |
JS::RootedObject obj2(cx, JS_NewPlainObject(cx)); |
| 577 |
JS::RootedObject obj3(cx, JS_NewPlainObject(cx)); |
| 578 |
JS::RootedObject obj4(cx, JS_NewPlainObject(cx)); |
| 579 |
CHECK(obj1); |
| 580 |
CHECK(obj2); |
| 581 |
CHECK(obj3); |
| 582 |
CHECK(obj4); |
| 583 |
|
| 584 |
CHECK(!cache.has(obj1)); |
| 585 |
CHECK(cache.put(obj1, 1)); |
| 586 |
CHECK(cache.count() == 1); |
| 587 |
CHECK(cache.has(obj1)); |
| 588 |
CHECK(cache.lookup(obj1)->key() == obj1); |
| 589 |
|
| 590 |
CHECK(GCUntilCacheSweep(cx, cache)); |
| 591 |
CHECK(cache.needsIncrementalBarrier()); |
| 592 |
|
| 593 |
CHECK(!cache.has(obj2)); |
| 594 |
CHECK(cache.put(obj2, 2)); |
| 595 |
CHECK(cache.has(obj2)); |
| 596 |
CHECK(cache.lookup(obj2)->key() == obj2); |
| 597 |
|
| 598 |
CHECK(SweepCacheAndFinishGC(cx, cache)); |
| 599 |
CHECK(!cache.needsIncrementalBarrier()); |
| 600 |
|
| 601 |
CHECK(cache.count() == 2); |
| 602 |
CHECK(cache.has(obj1)); |
| 603 |
CHECK(cache.has(obj2)); |
| 604 |
|
| 605 |
// Test iteration. |
| 606 |
|
| 607 |
CHECK(cache.put(obj3, 3)); |
| 608 |
CHECK(cache.put(obj4, 4)); |
| 609 |
void* old3 = obj3; |
| 610 |
void* old4 = obj4; |
| 611 |
obj3 = obj4 = nullptr; |
| 612 |
|
| 613 |
CHECK(GCUntilCacheSweep(cx, cache)); |
| 614 |
|
| 615 |
CHECK(cache.has(obj1)); |
| 616 |
CHECK(cache.has(obj2)); |
| 617 |
CHECK(!cache.has(static_cast<JSObject*>(old3))); |
| 618 |
CHECK(!cache.has(static_cast<JSObject*>(old4))); |
| 619 |
|
| 620 |
size_t count = 0; |
| 621 |
for (auto r = cache.all(); !r.empty(); r.popFront()) { |
| 622 |
CHECK(r.front().key() == obj1 || r.front().key() == obj2); |
| 623 |
count++; |
| 624 |
} |
| 625 |
CHECK(count == 2); |
| 626 |
|
| 627 |
CHECK(SweepCacheAndFinishGC(cx, cache)); |
| 628 |
|
| 629 |
CHECK(cache.count() == 2); |
| 630 |
|
| 631 |
// Test lookupForAdd while sweeping. |
| 632 |
|
| 633 |
obj3 = JS_NewPlainObject(cx); |
| 634 |
obj4 = JS_NewPlainObject(cx); |
| 635 |
CHECK(obj3); |
| 636 |
CHECK(obj4); |
| 637 |
|
| 638 |
CHECK(cache.lookupForAdd(obj1)); |
| 639 |
CHECK(cache.lookupForAdd(obj1)->key() == obj1); |
| 640 |
|
| 641 |
auto addp = cache.lookupForAdd(obj3); |
| 642 |
CHECK(!addp); |
| 643 |
CHECK(cache.add(addp, obj3, 3)); |
| 644 |
CHECK(cache.has(obj3)); |
| 645 |
|
| 646 |
CHECK(GCUntilCacheSweep(cx, cache)); |
| 647 |
|
| 648 |
addp = cache.lookupForAdd(obj4); |
| 649 |
CHECK(!addp); |
| 650 |
CHECK(cache.add(addp, obj4, 4)); |
| 651 |
CHECK(cache.has(obj4)); |
| 652 |
|
| 653 |
CHECK(SweepCacheAndFinishGC(cx, cache)); |
| 654 |
|
| 655 |
CHECK(cache.count() == 4); |
| 656 |
CHECK(cache.has(obj3)); |
| 657 |
CHECK(cache.has(obj4)); |
| 658 |
|
| 659 |
// Test remove while sweeping. |
| 660 |
|
| 661 |
cache.remove(obj3); |
| 662 |
|
| 663 |
CHECK(GCUntilCacheSweep(cx, cache)); |
| 664 |
|
| 665 |
cache.remove(obj4); |
| 666 |
|
| 667 |
CHECK(SweepCacheAndFinishGC(cx, cache)); |
| 668 |
|
| 669 |
CHECK(cache.count() == 2); |
| 670 |
CHECK(!cache.has(obj3)); |
| 671 |
CHECK(!cache.has(obj4)); |
| 672 |
|
| 673 |
// Test putNew while sweeping. |
| 674 |
|
| 675 |
CHECK(GCUntilCacheSweep(cx, cache)); |
| 676 |
|
| 677 |
CHECK(cache.putNew(obj3, 3)); |
| 678 |
CHECK(cache.putNew(obj4, 4)); |
| 679 |
|
| 680 |
CHECK(SweepCacheAndFinishGC(cx, cache)); |
| 681 |
|
| 682 |
CHECK(cache.count() == 4); |
| 683 |
CHECK(cache.has(obj3)); |
| 684 |
CHECK(cache.has(obj4)); |
| 685 |
|
| 686 |
cache.clear(); |
| 687 |
|
| 688 |
return true; |
| 689 |
} |
| 690 |
|
| 691 |
bool TestReplaceDyingInSet() { |
| 692 |
// Test replacing dying entries with ones that have the same key using the |
| 693 |
// various APIs. |
| 694 |
|
| 695 |
using Cache = JS::WeakCache< |
| 696 |
GCHashSet<NumberAndObjectEntry, StableCellHasher<NumberAndObjectEntry>, |
| 697 |
TempAllocPolicy>>; |
| 698 |
Cache cache(JS::GetObjectZone(global), cx); |
| 699 |
|
| 700 |
RootedObject value1(cx, JS_NewPlainObject(cx)); |
| 701 |
RootedObject value2(cx, JS_NewPlainObject(cx)); |
| 702 |
CHECK(value1); |
| 703 |
CHECK(value2); |
| 704 |
|
| 705 |
CHECK(cache.put(NumberAndObjectEntry(1, value1))); |
| 706 |
CHECK(cache.put(NumberAndObjectEntry(2, value2))); |
| 707 |
CHECK(cache.put(NumberAndObjectEntry(3, value2))); |
| 708 |
CHECK(cache.put(NumberAndObjectEntry(4, value2))); |
| 709 |
CHECK(cache.put(NumberAndObjectEntry(5, value2))); |
| 710 |
CHECK(cache.put(NumberAndObjectEntry(6, value2))); |
| 711 |
CHECK(cache.put(NumberAndObjectEntry(7, value2))); |
| 712 |
|
| 713 |
value2 = nullptr; |
| 714 |
CHECK(GCUntilCacheSweep(cx, cache)); |
| 715 |
|
| 716 |
CHECK(!cache.has(NumberAndObjectLookup(2, value1))); |
| 717 |
CHECK(!cache.has(NumberAndObjectLookup(3, value1))); |
| 718 |
CHECK(!cache.has(NumberAndObjectLookup(4, value1))); |
| 719 |
CHECK(!cache.has(NumberAndObjectLookup(5, value1))); |
| 720 |
CHECK(!cache.has(NumberAndObjectLookup(6, value1))); |
| 721 |
|
| 722 |
auto ptr = cache.lookupForAdd(NumberAndObjectLookup(2, value1)); |
| 723 |
CHECK(!ptr); |
| 724 |
CHECK(cache.add(ptr, NumberAndObjectEntry(2, value1))); |
| 725 |
|
| 726 |
auto ptr2 = cache.lookupForAdd(NumberAndObjectLookup(3, value1)); |
| 727 |
CHECK(!ptr2); |
| 728 |
CHECK(cache.relookupOrAdd(ptr2, NumberAndObjectLookup(3, value1), |
| 729 |
NumberAndObjectEntry(3, value1))); |
| 730 |
|
| 731 |
CHECK(cache.put(NumberAndObjectEntry(4, value1))); |
| 732 |
CHECK(cache.putNew(NumberAndObjectEntry(5, value1))); |
| 733 |
|
| 734 |
CHECK(cache.putNew(NumberAndObjectLookup(6, value1), |
| 735 |
NumberAndObjectEntry(6, value1))); |
| 736 |
|
| 737 |
CHECK(SweepCacheAndFinishGC(cx, cache)); |
| 738 |
|
| 739 |
CHECK(cache.count() == 6); |
| 740 |
CHECK(cache.has(NumberAndObjectLookup(1, value1))); |
| 741 |
CHECK(cache.has(NumberAndObjectLookup(2, value1))); |
| 742 |
CHECK(cache.has(NumberAndObjectLookup(3, value1))); |
| 743 |
CHECK(cache.has(NumberAndObjectLookup(4, value1))); |
| 744 |
CHECK(cache.has(NumberAndObjectLookup(5, value1))); |
| 745 |
CHECK(cache.has(NumberAndObjectLookup(6, value1))); |
| 746 |
|
| 747 |
return true; |
| 748 |
} |
| 749 |
|
| 750 |
bool TestReplaceDyingInMap() { |
| 751 |
// Test replacing dying entries with ones that have the same key using the |
| 752 |
// various APIs. |
| 753 |
|
| 754 |
using Cache = |
| 755 |
JS::WeakCache<GCHashMap<uint32_t, HeapPtr<JSObject*>, |
| 756 |
DefaultHasher<uint32_t>, TempAllocPolicy>>; |
| 757 |
Cache cache(JS::GetObjectZone(global), cx); |
| 758 |
|
| 759 |
RootedObject value1(cx, JS_NewPlainObject(cx)); |
| 760 |
RootedObject value2(cx, JS_NewPlainObject(cx)); |
| 761 |
CHECK(value1); |
| 762 |
CHECK(value2); |
| 763 |
|
| 764 |
CHECK(cache.put(1, value1)); |
| 765 |
CHECK(cache.put(2, value2)); |
| 766 |
CHECK(cache.put(3, value2)); |
| 767 |
CHECK(cache.put(4, value2)); |
| 768 |
CHECK(cache.put(5, value2)); |
| 769 |
CHECK(cache.put(6, value2)); |
| 770 |
|
| 771 |
value2 = nullptr; |
| 772 |
CHECK(GCUntilCacheSweep(cx, cache)); |
| 773 |
|
| 774 |
CHECK(!cache.has(2)); |
| 775 |
CHECK(!cache.has(3)); |
| 776 |
CHECK(!cache.has(4)); |
| 777 |
CHECK(!cache.has(5)); |
| 778 |
CHECK(!cache.has(6)); |
| 779 |
|
| 780 |
auto ptr = cache.lookupForAdd(2); |
| 781 |
CHECK(!ptr); |
| 782 |
CHECK(cache.add(ptr, 2, value1)); |
| 783 |
|
| 784 |
auto ptr2 = cache.lookupForAdd(3); |
| 785 |
CHECK(!ptr2); |
| 786 |
CHECK(cache.add(ptr2, 3, HeapPtr<JSObject*>())); |
| 787 |
|
| 788 |
auto ptr3 = cache.lookupForAdd(4); |
| 789 |
CHECK(!ptr3); |
| 790 |
CHECK(cache.relookupOrAdd(ptr3, 4, value1)); |
| 791 |
|
| 792 |
CHECK(cache.put(5, value1)); |
| 793 |
CHECK(cache.putNew(6, value1)); |
| 794 |
|
| 795 |
CHECK(SweepCacheAndFinishGC(cx, cache)); |
| 796 |
|
| 797 |
CHECK(cache.count() == 6); |
| 798 |
CHECK(cache.lookup(1)->value() == value1); |
| 799 |
CHECK(cache.lookup(2)->value() == value1); |
| 800 |
CHECK(cache.lookup(3)->value() == nullptr); |
| 801 |
CHECK(cache.lookup(4)->value() == value1); |
| 802 |
CHECK(cache.lookup(5)->value() == value1); |
| 803 |
CHECK(cache.lookup(6)->value() == value1); |
| 804 |
|
| 805 |
return true; |
| 806 |
} |
| 807 |
|
| 808 |
bool TestUniqueIDLookups() { |
| 809 |
// Test hash table lookups during incremental sweeping where the hash is |
| 810 |
// generated based on a unique ID. The problem is that the unique ID table |
| 811 |
// will have already been swept by this point so looking up a dead pointer |
| 812 |
// in the table will fail. This lookup happens if we try to match a live key |
| 813 |
// against a dead table entry with the same hash code. |
| 814 |
|
| 815 |
const size_t DeadFactor = 3; |
| 816 |
const size_t ObjectCount = 100; |
| 817 |
|
| 818 |
using Cache = JS::WeakCache< |
| 819 |
GCHashSet<ObjectEntry, StableCellHasher<ObjectEntry>, TempAllocPolicy>>; |
| 820 |
Cache cache(JS::GetObjectZone(global), cx); |
| 821 |
|
| 822 |
Rooted<GCVector<JSObject*, 0, SystemAllocPolicy>> liveObjects(cx); |
| 823 |
|
| 824 |
for (size_t j = 0; j < ObjectCount; j++) { |
| 825 |
JSObject* obj = JS_NewPlainObject(cx); |
| 826 |
CHECK(obj); |
| 827 |
CHECK(cache.put(obj)); |
| 828 |
if (j % DeadFactor == 0) { |
| 829 |
CHECK(liveObjects.get().append(obj)); |
| 830 |
} |
| 831 |
} |
| 832 |
|
| 833 |
CHECK(cache.count() == ObjectCount); |
| 834 |
|
| 835 |
CHECK(GCUntilCacheSweep(cx, cache)); |
| 836 |
|
| 837 |
for (size_t j = 0; j < liveObjects.length(); j++) { |
| 838 |
CHECK(cache.has(liveObjects[j])); |
| 839 |
} |
| 840 |
|
| 841 |
CHECK(SweepCacheAndFinishGC(cx, cache)); |
| 842 |
|
| 843 |
CHECK(cache.count() == liveObjects.length()); |
| 844 |
|
| 845 |
return true; |
| 846 |
} |
| 847 |
|
| 848 |
END_TEST(testIncrementalWeakCacheSweeping) |
| 849 |
|
| 850 |
#endif // defined JS_GC_ZEAL |