Debian Codegraph @

js/src/vm/Watchtower.h

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #ifndef vm_Watchtower_h
8 #define vm_Watchtower_h
9
10 #include "js/TypeDecls.h"
11 #include "vm/NativeObject.h"
12
13 namespace js {
14
15 // [SMDOC] Watchtower
16 //
17 // Watchtower is a framework to hook into changes to certain objects. This gives
18 // us the ability to, for instance, invalidate caches or purge Warp code on
19 // object layout changes.
20 //
21 // Watchtower is only used for objects with certain ObjectFlags set on the
22 // Shape. This minimizes performance overhead for most objects.
23 //
24 // We currently use Watchtower for:
25 //
26 // - Invalidating the shape teleporting optimization. See the "Shape Teleporting
27 // Optimization" SMDOC comment in CacheIR.cpp.
28 //
29 // - Invalidating the MegamorphicCache, a property lookup cache for megamorphic
30 // property accesses. See the SMDOC comment in vm/Caches.h.
31 //
32 // There's also a testing mechanism that lets us write tests for Watchtower
33 // hooks. See setWatchtowerCallback and addWatchtowerTarget defined in
34 // TestingFunctions.cpp.
35 class Watchtower {
36 static bool watchPropertyAddSlow(JSContext* cx, Handle<NativeObject*> obj,
37 HandleId id);
38 static bool watchPropertyRemoveSlow(JSContext* cx, Handle<NativeObject*> obj,
39 HandleId id);
40 static bool watchPropertyFlagsChangeSlow(JSContext* cx,
41 Handle<NativeObject*> obj,
42 HandleId id, PropertyInfo propInfo,
43 PropertyFlags newFlags);
44 template <AllowGC allowGC>
45 static void watchPropertyValueChangeSlow(
46 JSContext* cx,
47 typename MaybeRooted<NativeObject*, allowGC>::HandleType obj,
48 typename MaybeRooted<PropertyKey, allowGC>::HandleType id,
49 typename MaybeRooted<Value, allowGC>::HandleType value,
50 PropertyInfo propInfo);
51 static bool watchFreezeOrSealSlow(JSContext* cx, Handle<NativeObject*> obj,
52 IntegrityLevel level);
53 static bool watchProtoChangeSlow(JSContext* cx, HandleObject obj);
54 static bool watchObjectSwapSlow(JSContext* cx, HandleObject a,
55 HandleObject b);
56
57 public:
58 static bool watchesPropertyAdd(NativeObject* obj) {
59 return obj->hasAnyFlag(
60 {ObjectFlag::IsUsedAsPrototype, ObjectFlag::UseWatchtowerTestingLog});
61 }
62 static bool watchesPropertyRemove(NativeObject* obj) {
63 return obj->hasAnyFlag(
64 {ObjectFlag::IsUsedAsPrototype, ObjectFlag::GenerationCountedGlobal,
65 ObjectFlag::UseWatchtowerTestingLog, ObjectFlag::HasFuseProperty});
66 }
67 static bool watchesPropertyFlagsChange(NativeObject* obj) {
68 return obj->hasAnyFlag({ObjectFlag::IsUsedAsPrototype,
69 ObjectFlag::GenerationCountedGlobal,
70 ObjectFlag::UseWatchtowerTestingLog});
71 }
72 static bool watchesPropertyValueChange(NativeObject* obj) {
73 return obj->hasAnyFlag(
74 {ObjectFlag::HasFuseProperty, ObjectFlag::UseWatchtowerTestingLog});
75 }
76 static bool watchesFreezeOrSeal(NativeObject* obj) {
77 return obj->hasAnyFlag(
78 {ObjectFlag::IsUsedAsPrototype, ObjectFlag::UseWatchtowerTestingLog});
79 }
80 static bool watchesProtoChange(JSObject* obj) {
81 return obj->hasAnyFlag(
82 {ObjectFlag::IsUsedAsPrototype, ObjectFlag::UseWatchtowerTestingLog});
83 }
84 static bool watchesObjectSwap(JSObject* a, JSObject* b) {
85 auto watches = [](JSObject* obj) {
86 return obj->hasAnyFlag(
87 {ObjectFlag::IsUsedAsPrototype, ObjectFlag::UseWatchtowerTestingLog});
88 };
89 return watches(a) || watches(b);
90 }
91
92 static bool watchPropertyAdd(JSContext* cx, Handle<NativeObject*> obj,
93 HandleId id) {
94 if (MOZ_LIKELY(!watchesPropertyAdd(obj))) {
95 return true;
96 }
97 return watchPropertyAddSlow(cx, obj, id);
98 }
99 static bool watchPropertyRemove(JSContext* cx, Handle<NativeObject*> obj,
100 HandleId id) {
101 if (MOZ_LIKELY(!watchesPropertyRemove(obj))) {
102 return true;
103 }
104 return watchPropertyRemoveSlow(cx, obj, id);
105 }
106 static bool watchPropertyFlagsChange(JSContext* cx, Handle<NativeObject*> obj,
107 HandleId id, PropertyInfo propInfo,
108 PropertyFlags newFlags) {
109 if (MOZ_LIKELY(!watchesPropertyFlagsChange(obj))) {
110 return true;
111 }
112 return watchPropertyFlagsChangeSlow(cx, obj, id, propInfo, newFlags);
113 }
114
115 // Note: We can only watch property value changes for regular object slots
116 // with an id, not reserved slots.
117 template <AllowGC allowGC>
118 static void watchPropertyValueChange(
119 JSContext* cx,
120 typename MaybeRooted<NativeObject*, allowGC>::HandleType obj,
121 typename MaybeRooted<PropertyKey, allowGC>::HandleType id,
122 typename MaybeRooted<Value, allowGC>::HandleType value,
123 PropertyInfo propInfo) {
124 if (MOZ_LIKELY(!watchesPropertyValueChange(obj))) {
125 return;
126 }
127 watchPropertyValueChangeSlow<allowGC>(cx, obj, id, value, propInfo);
128 }
129 static bool watchFreezeOrSeal(JSContext* cx, Handle<NativeObject*> obj,
130 IntegrityLevel level) {
131 if (MOZ_LIKELY(!watchesFreezeOrSeal(obj))) {
132 return true;
133 }
134 return watchFreezeOrSealSlow(cx, obj, level);
135 }
136 static bool watchProtoChange(JSContext* cx, HandleObject obj) {
137 if (MOZ_LIKELY(!watchesProtoChange(obj))) {
138 return true;
139 }
140 return watchProtoChangeSlow(cx, obj);
141 }
142
143 static bool watchObjectSwap(JSContext* cx, HandleObject a, HandleObject b) {
144 if (MOZ_LIKELY(!watchesObjectSwap(a, b))) {
145 return true;
146 }
147 return watchObjectSwapSlow(cx, a, b);
148 }
149 };
150
151 } // namespace js
152
153 #endif /* vm_Watchtower_h */