Debian Codegraph @

js/src/vm/Watchtower.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 * 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 #include "vm/Watchtower.h"
8
9 #include "js/CallAndConstruct.h"
10 #include "vm/Compartment.h"
11 #include "vm/JSContext.h"
12 #include "vm/JSObject.h"
13 #include "vm/NativeObject.h"
14 #include "vm/PlainObject.h"
15 #include "vm/Realm.h"
16
17 #include "vm/Compartment-inl.h"
18 #include "vm/JSObject-inl.h"
19 #include "vm/NativeObject-inl.h"
20 #include "vm/Realm-inl.h"
21 #include "vm/Shape-inl.h"
22
23 using namespace js;
24
25 static bool AddToWatchtowerLog(JSContext* cx, const char* kind,
26 HandleObject obj, HandleValue extra) {
27 // Add an object storing {kind, object, extra} to the log for testing
28 // purposes.
29
30 MOZ_ASSERT(obj->useWatchtowerTestingLog());
31
32 RootedString kindString(cx, NewStringCopyZ<CanGC>(cx, kind));
33 if (!kindString) {
34 return false;
35 }
36
37 Rooted<PlainObject*> logObj(cx, NewPlainObjectWithProto(cx, nullptr));
38 if (!logObj) {
39 return false;
40 }
41 if (!JS_DefineProperty(cx, logObj, "kind", kindString, JSPROP_ENUMERATE)) {
42 return false;
43 }
44 if (!JS_DefineProperty(cx, logObj, "object", obj, JSPROP_ENUMERATE)) {
45 return false;
46 }
47 if (!JS_DefineProperty(cx, logObj, "extra", extra, JSPROP_ENUMERATE)) {
48 return false;
49 }
50
51 if (!cx->runtime()->watchtowerTestingLog->append(logObj)) {
52 ReportOutOfMemory(cx);
53 return false;
54 }
55
56 return true;
57 }
58
59 static bool ReshapeForShadowedProp(JSContext* cx, Handle<NativeObject*> obj,
60 HandleId id) {
61 // |obj| has been used as the prototype of another object. Check if we're
62 // shadowing a property on its proto chain. In this case we need to reshape
63 // that object for shape teleporting to work correctly.
64 //
65 // See also the 'Shape Teleporting Optimization' comment in jit/CacheIR.cpp.
66
67 MOZ_ASSERT(obj->isUsedAsPrototype());
68
69 // Lookups on integer ids cannot be cached through prototypes.
70 if (id.isInt()) {
71 return true;
72 }
73
74 bool useDictionaryTeleporting =
75 cx->zone()->shapeZone().useDictionaryModeTeleportation();
76
77 RootedObject proto(cx, obj->staticPrototype());
78 while (proto) {
79 // Lookups will not be cached through non-native protos.
80 if (!proto->is<NativeObject>()) {
81 break;
82 }
83 if (proto->as<NativeObject>().contains(cx, id)) {
84 if (useDictionaryTeleporting) {
85 JS_LOG(teleporting, Debug,
86 "Shadowed Prop: Dictionary Reshape for Teleporting");
87
88 return JSObject::reshapeForTeleporting(cx, proto);
89 }
90
91 JS_LOG(teleporting, Info,
92 "Shadowed Prop: Invalidating Reshape for Teleporting");
93 return JSObject::setInvalidatedTeleporting(cx, proto);
94 }
95
96 proto = proto->staticPrototype();
97 }
98
99 return true;
100 }
101
102 static void InvalidateMegamorphicCache(JSContext* cx, Handle<NativeObject*> obj,
103 bool invalidateGetPropCache = true) {
104 // The megamorphic cache only checks the receiver object's shape. We need to
105 // invalidate the cache when a prototype object changes its set of properties,
106 // to account for cached properties that are deleted, turned into an accessor
107 // property, or shadowed by another object on the proto chain.
108
109 MOZ_ASSERT(obj->isUsedAsPrototype());
110
111 if (invalidateGetPropCache) {
112 cx->caches().megamorphicCache.bumpGeneration();
113 }
114 cx->caches().megamorphicSetPropCache->bumpGeneration();
115 }
116
117 void MaybePopReturnFuses(JSContext* cx, Handle<NativeObject*> nobj) {
118 GlobalObject* global = &nobj->global();
119 JSObject* objectProto = &global->getObjectPrototype();
120 if (nobj == objectProto) {
121 nobj->realm()->realmFuses.objectPrototypeHasNoReturnProperty.popFuse(
122 cx, nobj->realm()->realmFuses);
123 return;
124 }
125
126 JSObject* iteratorProto = global->maybeGetIteratorPrototype();
127 if (nobj == iteratorProto) {
128 nobj->realm()->realmFuses.iteratorPrototypeHasNoReturnProperty.popFuse(
129 cx, nobj->realm()->realmFuses);
130 return;
131 }
132
133 JSObject* arrayIterProto = global->maybeGetArrayIteratorPrototype();
134 if (nobj == arrayIterProto) {
135 nobj->realm()->realmFuses.arrayIteratorPrototypeHasNoReturnProperty.popFuse(
136 cx, nobj->realm()->realmFuses);
137 return;
138 }
139 }
140
141 static void MaybePopStringPrototypeSymbolsFuse(JSContext* cx, NativeObject* obj,
142 PropertyKey key) {
143 if (!key.isSymbol()) {
144 return;
145 }
146 GlobalObject* global = &obj->global();
147 if (obj != global->maybeGetPrototype(JSProto_String) &&
148 obj != global->maybeGetPrototype(JSProto_Object)) {
149 return;
150 }
151 if (key.toSymbol() == cx->wellKnownSymbols().match ||
152 key.toSymbol() == cx->wellKnownSymbols().replace ||
153 key.toSymbol() == cx->wellKnownSymbols().search ||
154 key.toSymbol() == cx->wellKnownSymbols().split) {
155 obj->realm()->realmFuses.optimizeStringPrototypeSymbolsFuse.popFuse(
156 cx, obj->realm()->realmFuses);
157 }
158 }
159
160 // static
161 bool Watchtower::watchPropertyAddSlow(JSContext* cx, Handle<NativeObject*> obj,
162 HandleId id) {
163 MOZ_ASSERT(watchesPropertyAdd(obj));
164
165 if (obj->isUsedAsPrototype()) {
166 if (!ReshapeForShadowedProp(cx, obj, id)) {
167 return false;
168 }
169 if (!id.isInt()) {
170 InvalidateMegamorphicCache(cx, obj);
171 }
172
173 if (id == NameToId(cx->names().return_)) {
174 MaybePopReturnFuses(cx, obj);
175 }
176
177 MaybePopStringPrototypeSymbolsFuse(cx, obj, id);
178 }
179
180 if (MOZ_UNLIKELY(obj->useWatchtowerTestingLog())) {
181 RootedValue val(cx, IdToValue(id));
182 if (!AddToWatchtowerLog(cx, "add-prop", obj, val)) {
183 return false;
184 }
185 }
186
187 return true;
188 }
189
190 static bool ReshapeForProtoMutation(JSContext* cx, HandleObject obj) {
191 // To avoid the JIT guarding on each prototype in the proto chain to detect
192 // prototype mutation, we can instead reshape the rest of the proto chain such
193 // that a guard on any of them is sufficient. To avoid excessive reshaping and
194 // invalidation, we apply heuristics to decide when to apply this and when
195 // to require a guard.
196 //
197 // There are two cases:
198 //
199 // (1) The object is not marked IsUsedAsPrototype. This is the common case.
200 // Because shape implies proto, we rely on the caller changing the
201 // object's shape. The JIT guards on this object's shape or prototype so
202 // there's nothing we have to do here for objects on the proto chain.
203 //
204 // (2) The object is marked IsUsedAsPrototype. This implies the object may be
205 // participating in shape teleporting. To invalidate JIT ICs depending on
206 // the proto chain being unchanged, set the InvalidatedTeleporting shape
207 // flag for this object and objects on its proto chain.
208 //
209 // This flag disables future shape teleporting attempts, so next time this
210 // happens the loop below will be a no-op.
211 //
212 // NOTE: We only handle NativeObjects and don't propagate reshapes through
213 // any non-native objects on the chain.
214 //
215 // See Also:
216 // - GeneratePrototypeGuards
217 // - GeneratePrototypeHoleGuards
218
219 MOZ_ASSERT(obj->isUsedAsPrototype());
220
221 RootedObject pobj(cx, obj);
222
223 bool useDictionaryTeleporting =
224 cx->zone()->shapeZone().useDictionaryModeTeleportation();
225
226 while (pobj && pobj->is<NativeObject>()) {
227 if (useDictionaryTeleporting) {
228 MOZ_ASSERT(!pobj->hasInvalidatedTeleporting(),
229 "Once we start using invalidation shouldn't do any more "
230 "dictionary mode teleportation");
231 JS_LOG(teleporting, Debug,
232 "Proto Mutation: Dictionary Reshape for Teleporting");
233
234 if (!JSObject::reshapeForTeleporting(cx, pobj)) {
235 return false;
236 }
237 } else if (!pobj->hasInvalidatedTeleporting()) {
238 JS_LOG(teleporting, Info,
239 "Proto Mutation: Invalidating Reshape for Teleporting");
240
241 if (!JSObject::setInvalidatedTeleporting(cx, pobj)) {
242 return false;
243 }
244 }
245 pobj = pobj->staticPrototype();
246 }
247
248 return true;
249 }
250
251 static bool WatchProtoChangeImpl(JSContext* cx, HandleObject obj) {
252 if (!obj->isUsedAsPrototype()) {
253 return true;
254 }
255 if (!ReshapeForProtoMutation(cx, obj)) {
256 return false;
257 }
258 if (obj->is<NativeObject>()) {
259 InvalidateMegamorphicCache(cx, obj.as<NativeObject>());
260
261 NativeObject* nobj = &obj->as<NativeObject>();
262 if (nobj == nobj->global().maybeGetArrayIteratorPrototype()) {
263 nobj->realm()->realmFuses.arrayIteratorPrototypeHasIteratorProto.popFuse(
264 cx, nobj->realm()->realmFuses);
265 }
266
267 if (nobj == nobj->global().maybeGetIteratorPrototype()) {
268 nobj->realm()->realmFuses.iteratorPrototypeHasObjectProto.popFuse(
269 cx, nobj->realm()->realmFuses);
270 }
271
272 if (nobj == nobj->global().maybeGetPrototype(JSProto_String)) {
273 nobj->realm()->realmFuses.optimizeStringPrototypeSymbolsFuse.popFuse(
274 cx, nobj->realm()->realmFuses);
275 }
276 }
277
278 return true;
279 }
280
281 // static
282 bool Watchtower::watchProtoChangeSlow(JSContext* cx, HandleObject obj) {
283 MOZ_ASSERT(watchesProtoChange(obj));
284
285 if (!WatchProtoChangeImpl(cx, obj)) {
286 return false;
287 }
288
289 if (MOZ_UNLIKELY(obj->useWatchtowerTestingLog())) {
290 if (!AddToWatchtowerLog(cx, "proto-change", obj,
291 JS::UndefinedHandleValue)) {
292 return false;
293 }
294 }
295
296 return true;
297 }
298
299 static void MaybePopArrayConstructorFuses(JSContext* cx, NativeObject* obj,
300 jsid id) {
301 if (obj != obj->global().maybeGetConstructor(JSProto_Array)) {
302 return;
303 }
304 if (id.isWellKnownSymbol(JS::SymbolCode::species)) {
305 obj->realm()->realmFuses.optimizeArraySpeciesFuse.popFuse(
306 cx, obj->realm()->realmFuses);
307 }
308 }
309
310 static void MaybePopArrayPrototypeFuses(JSContext* cx, NativeObject* obj,
311 jsid id) {
312 if (obj != obj->global().maybeGetArrayPrototype()) {
313 return;
314 }
315 if (id.isWellKnownSymbol(JS::SymbolCode::iterator)) {
316 obj->realm()->realmFuses.arrayPrototypeIteratorFuse.popFuse(
317 cx, obj->realm()->realmFuses);
318 }
319 if (id.isAtom(cx->names().constructor)) {
320 obj->realm()->realmFuses.optimizeArraySpeciesFuse.popFuse(
321 cx, obj->realm()->realmFuses);
322 }
323 }
324
325 static void MaybePopArrayIteratorPrototypeFuses(JSContext* cx,
326 NativeObject* obj, jsid id) {
327 if (obj != obj->global().maybeGetArrayIteratorPrototype()) {
328 return;
329 }
330 if (id.isAtom(cx->names().next)) {
331 obj->realm()->realmFuses.arrayPrototypeIteratorNextFuse.popFuse(
332 cx, obj->realm()->realmFuses);
333 }
334 }
335
336 static void MaybePopMapPrototypeFuses(JSContext* cx, NativeObject* obj,
337 jsid id) {
338 if (obj != obj->global().maybeGetPrototype(JSProto_Map)) {
339 return;
340 }
341 if (id.isWellKnownSymbol(JS::SymbolCode::iterator)) {
342 obj->realm()->realmFuses.optimizeMapObjectIteratorFuse.popFuse(
343 cx, obj->realm()->realmFuses);
344 }
345 if (id.isAtom(cx->names().set)) {
346 obj->realm()->realmFuses.optimizeMapPrototypeSetFuse.popFuse(
347 cx, obj->realm()->realmFuses);
348 }
349 }
350
351 static void MaybePopMapIteratorPrototypeFuses(JSContext* cx, NativeObject* obj,
352 jsid id) {
353 if (obj != obj->global().maybeBuiltinProto(
354 GlobalObject::ProtoKind::MapIteratorProto)) {
355 return;
356 }
357 if (id.isAtom(cx->names().next)) {
358 obj->realm()->realmFuses.optimizeMapObjectIteratorFuse.popFuse(
359 cx, obj->realm()->realmFuses);
360 }
361 }
362
363 static void MaybePopSetPrototypeFuses(JSContext* cx, NativeObject* obj,
364 jsid id) {
365 if (obj != obj->global().maybeGetPrototype(JSProto_Set)) {
366 return;
367 }
368 if (id.isWellKnownSymbol(JS::SymbolCode::iterator)) {
369 obj->realm()->realmFuses.optimizeSetObjectIteratorFuse.popFuse(
370 cx, obj->realm()->realmFuses);
371 }
372 if (id.isAtom(cx->names().add)) {
373 obj->realm()->realmFuses.optimizeSetPrototypeAddFuse.popFuse(
374 cx, obj->realm()->realmFuses);
375 }
376 }
377
378 static void MaybePopSetIteratorPrototypeFuses(JSContext* cx, NativeObject* obj,
379 jsid id) {
380 if (obj != obj->global().maybeBuiltinProto(
381 GlobalObject::ProtoKind::SetIteratorProto)) {
382 return;
383 }
384 if (id.isAtom(cx->names().next)) {
385 obj->realm()->realmFuses.optimizeSetObjectIteratorFuse.popFuse(
386 cx, obj->realm()->realmFuses);
387 }
388 }
389
390 static void MaybePopWeakMapPrototypeFuses(JSContext* cx, NativeObject* obj,
391 jsid id) {
392 if (obj != obj->global().maybeGetPrototype(JSProto_WeakMap)) {
393 return;
394 }
395 if (id.isAtom(cx->names().set)) {
396 obj->realm()->realmFuses.optimizeWeakMapPrototypeSetFuse.popFuse(
397 cx, obj->realm()->realmFuses);
398 }
399 }
400
401 static void MaybePopWeakSetPrototypeFuses(JSContext* cx, NativeObject* obj,
402 jsid id) {
403 if (obj != obj->global().maybeGetPrototype(JSProto_WeakSet)) {
404 return;
405 }
406 if (id.isAtom(cx->names().add)) {
407 obj->realm()->realmFuses.optimizeWeakSetPrototypeAddFuse.popFuse(
408 cx, obj->realm()->realmFuses);
409 }
410 }
411
412 static void MaybePopPromiseConstructorFuses(JSContext* cx, NativeObject* obj,
413 jsid id) {
414 if (obj != obj->global().maybeGetConstructor(JSProto_Promise)) {
415 return;
416 }
417 if (id.isWellKnownSymbol(JS::SymbolCode::species) ||
418 id.isAtom(cx->names().resolve)) {
419 obj->realm()->realmFuses.optimizePromiseLookupFuse.popFuse(
420 cx, obj->realm()->realmFuses);
421 }
422 }
423
424 static void MaybePopPromisePrototypeFuses(JSContext* cx, NativeObject* obj,
425 jsid id) {
426 if (obj != obj->global().maybeGetPrototype(JSProto_Promise)) {
427 return;
428 }
429 if (id.isAtom(cx->names().constructor) || id.isAtom(cx->names().then)) {
430 obj->realm()->realmFuses.optimizePromiseLookupFuse.popFuse(
431 cx, obj->realm()->realmFuses);
432 }
433 }
434
435 static void MaybePopRegExpPrototypeFuses(JSContext* cx, NativeObject* obj,
436 jsid id) {
437 if (obj != obj->global().maybeGetPrototype(JSProto_RegExp)) {
438 return;
439 }
440 if (id.isAtom(cx->names().flags) || id.isAtom(cx->names().global) ||
441 id.isAtom(cx->names().hasIndices) || id.isAtom(cx->names().ignoreCase) ||
442 id.isAtom(cx->names().multiline) || id.isAtom(cx->names().sticky) ||
443 id.isAtom(cx->names().unicode) || id.isAtom(cx->names().unicodeSets) ||
444 id.isAtom(cx->names().dotAll) || id.isAtom(cx->names().exec) ||
445 id.isWellKnownSymbol(JS::SymbolCode::match) ||
446 id.isWellKnownSymbol(JS::SymbolCode::matchAll) ||
447 id.isWellKnownSymbol(JS::SymbolCode::replace) ||
448 id.isWellKnownSymbol(JS::SymbolCode::search) ||
449 id.isWellKnownSymbol(JS::SymbolCode::split)) {
450 obj->realm()->realmFuses.optimizeRegExpPrototypeFuse.popFuse(
451 cx, obj->realm()->realmFuses);
452 }
453 }
454
455 static void MaybePopArrayBufferConstructorFuses(JSContext* cx,
456 NativeObject* obj, jsid id) {
457 if (obj != obj->global().maybeGetConstructor(JSProto_ArrayBuffer)) {
458 return;
459 }
460 if (id.isWellKnownSymbol(JS::SymbolCode::species)) {
461 obj->realm()->realmFuses.optimizeArrayBufferSpeciesFuse.popFuse(
462 cx, obj->realm()->realmFuses);
463 }
464 }
465
466 static void MaybePopArrayBufferPrototypeFuses(JSContext* cx, NativeObject* obj,
467 jsid id) {
468 if (obj != obj->global().maybeGetPrototype(JSProto_ArrayBuffer)) {
469 return;
470 }
471 if (id.isAtom(cx->names().constructor)) {
472 obj->realm()->realmFuses.optimizeArrayBufferSpeciesFuse.popFuse(
473 cx, obj->realm()->realmFuses);
474 }
475 }
476
477 static void MaybePopSharedArrayBufferConstructorFuses(JSContext* cx,
478 NativeObject* obj,
479 jsid id) {
480 if (obj != obj->global().maybeGetConstructor(JSProto_SharedArrayBuffer)) {
481 return;
482 }
483 if (id.isWellKnownSymbol(JS::SymbolCode::species)) {
484 obj->realm()->realmFuses.optimizeSharedArrayBufferSpeciesFuse.popFuse(
485 cx, obj->realm()->realmFuses);
486 }
487 }
488
489 static void MaybePopSharedArrayBufferPrototypeFuses(JSContext* cx,
490 NativeObject* obj,
491 jsid id) {
492 if (obj != obj->global().maybeGetPrototype(JSProto_SharedArrayBuffer)) {
493 return;
494 }
495 if (id.isAtom(cx->names().constructor)) {
496 obj->realm()->realmFuses.optimizeSharedArrayBufferSpeciesFuse.popFuse(
497 cx, obj->realm()->realmFuses);
498 }
499 }
500
501 static void MaybePopFuses(JSContext* cx, NativeObject* obj, jsid id) {
502 // Handle writes to Array constructor fuse properties.
503 MaybePopArrayConstructorFuses(cx, obj, id);
504
505 // Handle writes to Array.prototype fuse properties.
506 MaybePopArrayPrototypeFuses(cx, obj, id);
507
508 // Handle writes to %ArrayIteratorPrototype% fuse properties.
509 MaybePopArrayIteratorPrototypeFuses(cx, obj, id);
510
511 // Handle writes to Map.prototype fuse properties.
512 MaybePopMapPrototypeFuses(cx, obj, id);
513
514 // Handle writes to %MapIteratorPrototype% fuse properties.
515 MaybePopMapIteratorPrototypeFuses(cx, obj, id);
516
517 // Handle writes to Set.prototype fuse properties.
518 MaybePopSetPrototypeFuses(cx, obj, id);
519
520 // Handle writes to %SetIteratorPrototype% fuse properties.
521 MaybePopSetIteratorPrototypeFuses(cx, obj, id);
522
523 // Handle writes to WeakMap.prototype fuse properties.
524 MaybePopWeakMapPrototypeFuses(cx, obj, id);
525
526 // Handle writes to WeakSet.prototype fuse properties.
527 MaybePopWeakSetPrototypeFuses(cx, obj, id);
528
529 // Handle writes to Promise constructor fuse properties.
530 MaybePopPromiseConstructorFuses(cx, obj, id);
531
532 // Handle writes to Promise.prototype fuse properties.
533 MaybePopPromisePrototypeFuses(cx, obj, id);
534
535 // Handle writes to RegExp.prototype fuse properties.
536 MaybePopRegExpPrototypeFuses(cx, obj, id);
537
538 // Handle writes to ArrayBuffer constructor fuse properties.
539 MaybePopArrayBufferConstructorFuses(cx, obj, id);
540
541 // Handle writes to ArrayBuffer.prototype fuse properties.
542 MaybePopArrayBufferPrototypeFuses(cx, obj, id);
543
544 // Handle writes to SharedArrayBuffer constructor fuse properties.
545 MaybePopSharedArrayBufferConstructorFuses(cx, obj, id);
546
547 // Handle writes to SharedArrayBuffer.prototype fuse properties.
548 MaybePopSharedArrayBufferPrototypeFuses(cx, obj, id);
549 }
550
551 // static
552 bool Watchtower::watchPropertyRemoveSlow(JSContext* cx,
553 Handle<NativeObject*> obj,
554 HandleId id) {
555 MOZ_ASSERT(watchesPropertyRemove(obj));
556
557 if (obj->isUsedAsPrototype() && !id.isInt()) {
558 InvalidateMegamorphicCache(cx, obj);
559 }
560
561 if (obj->isGenerationCountedGlobal()) {
562 obj->as<GlobalObject>().bumpGenerationCount();
563 }
564
565 if (MOZ_UNLIKELY(obj->hasFuseProperty())) {
566 MaybePopFuses(cx, obj, id);
567 }
568
569 if (MOZ_UNLIKELY(obj->useWatchtowerTestingLog())) {
570 RootedValue val(cx, IdToValue(id));
571 if (!AddToWatchtowerLog(cx, "remove-prop", obj, val)) {
572 return false;
573 }
574 }
575
576 return true;
577 }
578
579 // static
580 bool Watchtower::watchPropertyFlagsChangeSlow(JSContext* cx,
581 Handle<NativeObject*> obj,
582 HandleId id,
583 PropertyInfo propInfo,
584 PropertyFlags newFlags) {
585 MOZ_ASSERT(watchesPropertyFlagsChange(obj));
586 MOZ_ASSERT(obj->lookupPure(id).ref() == propInfo);
587 MOZ_ASSERT(propInfo.flags() != newFlags);
588
589 if (obj->isUsedAsPrototype() && !id.isInt()) {
590 InvalidateMegamorphicCache(cx, obj);
591 }
592
593 if (obj->isGenerationCountedGlobal()) {
594 // The global generation counter only cares whether a property
595 // changes from data property to accessor or vice-versa. Changing
596 // the flags on a property doesn't matter.
597 bool wasAccessor = propInfo.isAccessorProperty();
598 bool isAccessor = newFlags.isAccessorProperty();
599 if (wasAccessor != isAccessor) {
600 obj->as<GlobalObject>().bumpGenerationCount();
601 }
602 }
603
604 if (MOZ_UNLIKELY(obj->useWatchtowerTestingLog())) {
605 RootedValue val(cx, IdToValue(id));
606 if (!AddToWatchtowerLog(cx, "change-prop-flags", obj, val)) {
607 return false;
608 }
609 }
610
611 return true;
612 }
613
614 // static
615 template <AllowGC allowGC>
616 void Watchtower::watchPropertyValueChangeSlow(
617 JSContext* cx, typename MaybeRooted<NativeObject*, allowGC>::HandleType obj,
618 typename MaybeRooted<PropertyKey, allowGC>::HandleType id,
619 typename MaybeRooted<Value, allowGC>::HandleType value,
620 PropertyInfo propInfo) {
621 MOZ_ASSERT(watchesPropertyValueChange(obj));
622
623 // Note: this is also called when changing the GetterSetter value of an
624 // accessor property or when redefining a data property as an accessor
625 // property and vice versa.
626
627 if (propInfo.hasSlot() && obj->getSlot(propInfo.slot()) == value) {
628 // We're not actually changing the property's value.
629 return;
630 }
631
632 if (MOZ_UNLIKELY(obj->hasFuseProperty())) {
633 MaybePopFuses(cx, obj, id);
634 }
635
636 // If we cannot GC, we can't manipulate the log, but we need to be able to
637 // call this in places we cannot GC.
638 if constexpr (allowGC == AllowGC::CanGC) {
639 if (MOZ_UNLIKELY(obj->useWatchtowerTestingLog())) {
640 RootedValue val(cx, IdToValue(id));
641 if (!AddToWatchtowerLog(cx, "change-prop-value", obj, val)) {
642 // Ignore OOM because this is just a testing feature and infallible
643 // watchPropertyValueChange simplifies the callers.
644 cx->clearPendingException();
645 }
646 }
647 }
648 }
649
650 template void Watchtower::watchPropertyValueChangeSlow<AllowGC::CanGC>(
651 JSContext* cx,
652 typename MaybeRooted<NativeObject*, AllowGC::CanGC>::HandleType obj,
653 typename MaybeRooted<PropertyKey, AllowGC::CanGC>::HandleType id,
654 typename MaybeRooted<Value, AllowGC::CanGC>::HandleType value,
655 PropertyInfo propInfo);
656 template void Watchtower::watchPropertyValueChangeSlow<AllowGC::NoGC>(
657 JSContext* cx,
658 typename MaybeRooted<NativeObject*, AllowGC::NoGC>::HandleType obj,
659 typename MaybeRooted<PropertyKey, AllowGC::NoGC>::HandleType id,
660 typename MaybeRooted<Value, AllowGC::NoGC>::HandleType value,
661 PropertyInfo propInfo);
662
663 // static
664 bool Watchtower::watchFreezeOrSealSlow(JSContext* cx, Handle<NativeObject*> obj,
665 IntegrityLevel level) {
666 MOZ_ASSERT(watchesFreezeOrSeal(obj));
667
668 // Invalidate the megamorphic set-property cache when freezing a prototype
669 // object. Non-writable prototype properties can't be shadowed (through
670 // SetProp) so this affects the behavior of add-property cache entries.
671 if (level == IntegrityLevel::Frozen && obj->isUsedAsPrototype()) {
672 InvalidateMegamorphicCache(cx, obj, /* invalidateGetPropCache = */ false);
673 }
674
675 if (MOZ_UNLIKELY(obj->useWatchtowerTestingLog())) {
676 if (!AddToWatchtowerLog(cx, "freeze-or-seal", obj,
677 JS::UndefinedHandleValue)) {
678 return false;
679 }
680 }
681
682 return true;
683 }
684
685 // static
686 bool Watchtower::watchObjectSwapSlow(JSContext* cx, HandleObject a,
687 HandleObject b) {
688 MOZ_ASSERT(watchesObjectSwap(a, b));
689
690 // If we're swapping an object that's used as prototype, we're mutating the
691 // proto chains of other objects. Treat this as a proto change to ensure we
692 // invalidate shape teleporting and megamorphic caches.
693 if (!WatchProtoChangeImpl(cx, a)) {
694 return false;
695 }
696 if (!WatchProtoChangeImpl(cx, b)) {
697 return false;
698 }
699
700 // Note: we don't invoke the testing callback for swap because the objects may
701 // not be safe to expose to JS at this point. See bug 1754699.
702
703 return true;
704 }