| 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_RealmFuses_h |
| 8 |
#define vm_RealmFuses_h |
| 9 |
|
| 10 |
#include "vm/GuardFuse.h" |
| 11 |
#include "vm/InvalidatingFuse.h" |
| 12 |
|
| 13 |
namespace js { |
| 14 |
|
| 15 |
class NativeObject; |
| 16 |
struct RealmFuses; |
| 17 |
|
| 18 |
// [SMDOC] RealmFuses: |
| 19 |
// |
| 20 |
// Realm fuses are fuses associated with a specific realm. As a result, |
| 21 |
// popFuse for realmFuses has another argument, the set of realmFuses related to |
| 22 |
// the fuse being popped. This is used to find any dependent fuses in the realm |
| 23 |
// (rather than using the context). |
| 24 |
class RealmFuse : public GuardFuse { |
| 25 |
public: |
| 26 |
virtual void popFuse(JSContext* cx, RealmFuses& realmFuses) { popFuse(cx); } |
| 27 |
|
| 28 |
protected: |
| 29 |
virtual void popFuse(JSContext* cx) override { GuardFuse::popFuse(cx); } |
| 30 |
}; |
| 31 |
|
| 32 |
class InvalidatingRealmFuse : public InvalidatingFuse { |
| 33 |
public: |
| 34 |
virtual void popFuse(JSContext* cx, RealmFuses& realmFuses); |
| 35 |
virtual bool addFuseDependency(JSContext* cx, |
| 36 |
Handle<JSScript*> script) override; |
| 37 |
|
| 38 |
protected: |
| 39 |
virtual void popFuse(JSContext* cx) override { |
| 40 |
InvalidatingFuse::popFuse(cx); |
| 41 |
} |
| 42 |
}; |
| 43 |
|
| 44 |
// Fuse guarding against changes to `Array.prototype[@@iterator]` and |
| 45 |
// `%ArrayIteratorPrototype%` that affect the iterator protocol for packed |
| 46 |
// arrays. |
| 47 |
// |
| 48 |
// Popped when one of the following fuses is popped: |
| 49 |
// - ArrayPrototypeIteratorFuse (for `Array.prototype[@@iterator]`) |
| 50 |
// - OptimizeArrayIteratorPrototypeFuse (for `%ArrayIteratorPrototype%`) |
| 51 |
struct OptimizeGetIteratorFuse final : public InvalidatingRealmFuse { |
| 52 |
virtual const char* name() override { return "OptimizeGetIteratorFuse"; } |
| 53 |
virtual bool checkInvariant(JSContext* cx) override; |
| 54 |
virtual void popFuse(JSContext* cx, RealmFuses& realmFuses) override; |
| 55 |
}; |
| 56 |
|
| 57 |
struct PopsOptimizedGetIteratorFuse : public RealmFuse { |
| 58 |
virtual void popFuse(JSContext* cx, RealmFuses& realmFuses) override; |
| 59 |
}; |
| 60 |
|
| 61 |
// Fuse guarding against changes to `%ArrayIteratorPrototype%` (and its |
| 62 |
// prototype chain) that affect the iterator protocol. |
| 63 |
// |
| 64 |
// Popped when one of the following fuses is popped: |
| 65 |
// - ArrayPrototypeIteratorNextFuse |
| 66 |
// - ArrayIteratorPrototypeHasNoReturnProperty |
| 67 |
// - ArrayIteratorPrototypeHasIteratorProto |
| 68 |
// - IteratorPrototypeHasNoReturnProperty |
| 69 |
// - IteratorPrototypeHasObjectProto |
| 70 |
// - ObjectPrototypeHasNoReturnProperty |
| 71 |
struct OptimizeArrayIteratorPrototypeFuse final |
| 72 |
: public PopsOptimizedGetIteratorFuse { |
| 73 |
virtual const char* name() override { |
| 74 |
return "OptimizeArrayIteratorPrototypeFuse"; |
| 75 |
} |
| 76 |
virtual bool checkInvariant(JSContext* cx) override; |
| 77 |
}; |
| 78 |
|
| 79 |
struct PopsOptimizedArrayIteratorPrototypeFuse : public RealmFuse { |
| 80 |
virtual void popFuse(JSContext* cx, RealmFuses& realmFuses) override; |
| 81 |
}; |
| 82 |
|
| 83 |
struct ArrayPrototypeIteratorFuse final : public PopsOptimizedGetIteratorFuse { |
| 84 |
virtual const char* name() override { return "ArrayPrototypeIteratorFuse"; } |
| 85 |
virtual bool checkInvariant(JSContext* cx) override; |
| 86 |
}; |
| 87 |
|
| 88 |
struct ArrayPrototypeIteratorNextFuse final |
| 89 |
: public PopsOptimizedArrayIteratorPrototypeFuse { |
| 90 |
virtual const char* name() override { |
| 91 |
return "ArrayPrototypeIteratorNextFuse"; |
| 92 |
} |
| 93 |
virtual bool checkInvariant(JSContext* cx) override; |
| 94 |
}; |
| 95 |
|
| 96 |
// This fuse covers ArrayIteratorPrototype not having a return property; |
| 97 |
// however the fuse doesn't pop if a prototype acquires the return property. |
| 98 |
struct ArrayIteratorPrototypeHasNoReturnProperty final |
| 99 |
: public PopsOptimizedArrayIteratorPrototypeFuse { |
| 100 |
virtual const char* name() override { |
| 101 |
return "ArrayIteratorPrototypeHasNoReturnProperty"; |
| 102 |
} |
| 103 |
virtual bool checkInvariant(JSContext* cx) override; |
| 104 |
}; |
| 105 |
|
| 106 |
// This fuse covers IteratorPrototype not having a return property; |
| 107 |
// however the fuse doesn't pop if a prototype acquires the return property. |
| 108 |
struct IteratorPrototypeHasNoReturnProperty final |
| 109 |
: public PopsOptimizedArrayIteratorPrototypeFuse { |
| 110 |
virtual const char* name() override { |
| 111 |
return "IteratorPrototypeHasNoReturnProperty"; |
| 112 |
} |
| 113 |
virtual bool checkInvariant(JSContext* cx) override; |
| 114 |
}; |
| 115 |
|
| 116 |
struct ArrayIteratorPrototypeHasIteratorProto final |
| 117 |
: public PopsOptimizedArrayIteratorPrototypeFuse { |
| 118 |
virtual const char* name() override { |
| 119 |
return "ArrayIteratorPrototypeHasIteratorProto"; |
| 120 |
} |
| 121 |
virtual bool checkInvariant(JSContext* cx) override; |
| 122 |
}; |
| 123 |
|
| 124 |
struct IteratorPrototypeHasObjectProto final |
| 125 |
: public PopsOptimizedArrayIteratorPrototypeFuse { |
| 126 |
virtual const char* name() override { |
| 127 |
return "IteratorPrototypeHasObjectProto"; |
| 128 |
} |
| 129 |
virtual bool checkInvariant(JSContext* cx) override; |
| 130 |
}; |
| 131 |
|
| 132 |
struct ObjectPrototypeHasNoReturnProperty final |
| 133 |
: public PopsOptimizedArrayIteratorPrototypeFuse { |
| 134 |
virtual const char* name() override { |
| 135 |
return "ObjectPrototypeHasNoReturnProperty"; |
| 136 |
} |
| 137 |
virtual bool checkInvariant(JSContext* cx) override; |
| 138 |
}; |
| 139 |
|
| 140 |
// Fuse used to optimize @@species lookups for arrays. If this fuse is intact, |
| 141 |
// the following invariants must hold: |
| 142 |
// |
| 143 |
// - The builtin `Array.prototype` object has a `constructor` property that's |
| 144 |
// the builtin `Array` constructor. |
| 145 |
// - This `Array` constructor has a `Symbol.species` property that's the |
| 146 |
// original accessor. |
| 147 |
struct OptimizeArraySpeciesFuse final : public InvalidatingRealmFuse { |
| 148 |
virtual const char* name() override { return "OptimizeArraySpeciesFuse"; } |
| 149 |
virtual bool checkInvariant(JSContext* cx) override; |
| 150 |
virtual void popFuse(JSContext* cx, RealmFuses& realmFuses) override; |
| 151 |
}; |
| 152 |
|
| 153 |
// Fuse used to optimize @@species lookups for ArrayBuffers. If this fuse is |
| 154 |
// intact, the following invariants must hold: |
| 155 |
// |
| 156 |
// - The builtin `ArrayBuffer.prototype` object has a `constructor` property |
| 157 |
// that's the builtin `ArrayBuffer` constructor. |
| 158 |
// - This `ArrayBuffer` constructor has a `Symbol.species` property that's the |
| 159 |
// original accessor. |
| 160 |
struct OptimizeArrayBufferSpeciesFuse final : public RealmFuse { |
| 161 |
virtual const char* name() override { |
| 162 |
return "OptimizeArrayBufferSpeciesFuse"; |
| 163 |
} |
| 164 |
virtual bool checkInvariant(JSContext* cx) override; |
| 165 |
}; |
| 166 |
|
| 167 |
// Fuse used to optimize @@species lookups for SharedArrayBuffers. If this fuse |
| 168 |
// is intact, the following invariants must hold: |
| 169 |
// |
| 170 |
// - The builtin `SharedArrayBuffer.prototype` object has a `constructor` |
| 171 |
// property that's the builtin `SharedArrayBuffer` constructor. |
| 172 |
// - This `SharedArrayBuffer` constructor has a `Symbol.species` property that's |
| 173 |
// the original accessor. |
| 174 |
struct OptimizeSharedArrayBufferSpeciesFuse final : public RealmFuse { |
| 175 |
virtual const char* name() override { |
| 176 |
return "OptimizeSharedArrayBufferSpeciesFuse"; |
| 177 |
} |
| 178 |
virtual bool checkInvariant(JSContext* cx) override; |
| 179 |
}; |
| 180 |
|
| 181 |
// Fuse used to optimize various property lookups for promises. If this fuse is |
| 182 |
// intact, the following invariants must hold: |
| 183 |
// |
| 184 |
// - The builtin `Promise.prototype` object has unchanged `constructor` and |
| 185 |
// `then` properties. |
| 186 |
// - The builtin `Promise` constructor has unchanged `Symbol.species` and |
| 187 |
// `resolve` properties. |
| 188 |
struct OptimizePromiseLookupFuse final : public RealmFuse { |
| 189 |
virtual const char* name() override { return "OptimizePromiseLookupFuse"; } |
| 190 |
virtual bool checkInvariant(JSContext* cx) override; |
| 191 |
virtual void popFuse(JSContext* cx, RealmFuses& realmFuses) override; |
| 192 |
}; |
| 193 |
|
| 194 |
// Fuse used to guard against changes to various properties on RegExp.prototype. |
| 195 |
// |
| 196 |
// If this fuse is intact, RegExp.prototype must have the following original |
| 197 |
// getter properties: |
| 198 |
// - .flags ($RegExpFlagsGetter) |
| 199 |
// - .global (regexp_global) |
| 200 |
// - .hasIndices (regexp_hasIndices) |
| 201 |
// - .ignoreCase (regexp_ignoreCase) |
| 202 |
// - .multiline (regexp_multiline) |
| 203 |
// - .sticky (regexp_sticky) |
| 204 |
// - .unicode (regexp_unicode) |
| 205 |
// - .unicodeSets (regexp_unicodeSets) |
| 206 |
// - .dotAll (regexp_dotAll) |
| 207 |
// |
| 208 |
// And the following unchanged data properties: |
| 209 |
// - .exec (RegExp_prototype_Exec) |
| 210 |
// - [@@match] (RegExpMatch) |
| 211 |
// - [@@matchAll] (RegExpMatchAll) |
| 212 |
// - [@@replace] (RegExpReplace) |
| 213 |
// - [@@search] (RegExpSearch) |
| 214 |
// - [@@split] (RegExpSplit) |
| 215 |
struct OptimizeRegExpPrototypeFuse final : public InvalidatingRealmFuse { |
| 216 |
virtual const char* name() override { return "OptimizeRegExpPrototypeFuse"; } |
| 217 |
virtual bool checkInvariant(JSContext* cx) override; |
| 218 |
}; |
| 219 |
|
| 220 |
// Fuse used to optimize lookups of certain symbols on String.prototype. |
| 221 |
// If this fuse is intact, the following invariants must hold: |
| 222 |
// |
| 223 |
// - The builtin String.prototype object has the builtin Object.prototype object |
| 224 |
// as prototype. |
| 225 |
// - Both String.prototype and Object.prototype don't have any of the following |
| 226 |
// properties: Symbol.match, Symbol.replace, Symbol.search, Symbol.split. |
| 227 |
struct OptimizeStringPrototypeSymbolsFuse final : public InvalidatingRealmFuse { |
| 228 |
virtual const char* name() override { |
| 229 |
return "OptimizeStringPrototypeSymbolsFuse"; |
| 230 |
} |
| 231 |
virtual bool checkInvariant(JSContext* cx) override; |
| 232 |
}; |
| 233 |
|
| 234 |
// Guard used to optimize iterating over Map objects. If this fuse is intact, |
| 235 |
// the following invariants must hold: |
| 236 |
// |
| 237 |
// - The builtin `Map.prototype` object has a `Symbol.iterator` property that's |
| 238 |
// the original `%Map.prototype.entries%` function. |
| 239 |
// - The builtin `%MapIteratorPrototype%` object has a `next` property that's |
| 240 |
// the original `MapIteratorNext` self-hosted function. |
| 241 |
// |
| 242 |
// Note: because this doesn't guard against `return` properties on the iterator |
| 243 |
// prototype, this should only be used in places where we don't have to call |
| 244 |
// `IteratorClose`. |
| 245 |
struct OptimizeMapObjectIteratorFuse final : public RealmFuse { |
| 246 |
virtual const char* name() override { |
| 247 |
return "OptimizeMapObjectIteratorFuse"; |
| 248 |
} |
| 249 |
virtual bool checkInvariant(JSContext* cx) override; |
| 250 |
}; |
| 251 |
|
| 252 |
// Guard used to optimize iterating over Set objects. If this fuse is intact, |
| 253 |
// the following invariants must hold: |
| 254 |
// |
| 255 |
// - The builtin `Set.prototype` object has a `Symbol.iterator` property that's |
| 256 |
// the original `%Set.prototype.values%` function. |
| 257 |
// - The builtin `%SetIteratorPrototype%` object has a `next` property that's |
| 258 |
// the original `SetIteratorNext` self-hosted function. |
| 259 |
// |
| 260 |
// Note: because this doesn't guard against `return` properties on the iterator |
| 261 |
// prototype, this should only be used in places where we don't have to call |
| 262 |
// `IteratorClose`. |
| 263 |
struct OptimizeSetObjectIteratorFuse final : public RealmFuse { |
| 264 |
virtual const char* name() override { |
| 265 |
return "OptimizeSetObjectIteratorFuse"; |
| 266 |
} |
| 267 |
virtual bool checkInvariant(JSContext* cx) override; |
| 268 |
}; |
| 269 |
|
| 270 |
// This fuse is popped when the `Map.prototype.set` property is mutated. |
| 271 |
struct OptimizeMapPrototypeSetFuse final : public RealmFuse { |
| 272 |
virtual const char* name() override { return "OptimizeMapPrototypeSetFuse"; } |
| 273 |
virtual bool checkInvariant(JSContext* cx) override; |
| 274 |
}; |
| 275 |
|
| 276 |
// This fuse is popped when the `Set.prototype.add` property is mutated. |
| 277 |
struct OptimizeSetPrototypeAddFuse final : public RealmFuse { |
| 278 |
virtual const char* name() override { return "OptimizeSetPrototypeAddFuse"; } |
| 279 |
virtual bool checkInvariant(JSContext* cx) override; |
| 280 |
}; |
| 281 |
|
| 282 |
// This fuse is popped when the `WeakMap.prototype.set` property is mutated. |
| 283 |
struct OptimizeWeakMapPrototypeSetFuse final : public RealmFuse { |
| 284 |
virtual const char* name() override { |
| 285 |
return "OptimizeWeakMapPrototypeSetFuse"; |
| 286 |
} |
| 287 |
virtual bool checkInvariant(JSContext* cx) override; |
| 288 |
}; |
| 289 |
|
| 290 |
// This fuse is popped when the `WeakSet.prototype.add` property is mutated. |
| 291 |
struct OptimizeWeakSetPrototypeAddFuse final : public RealmFuse { |
| 292 |
virtual const char* name() override { |
| 293 |
return "OptimizeWeakSetPrototypeAddFuse"; |
| 294 |
} |
| 295 |
virtual bool checkInvariant(JSContext* cx) override; |
| 296 |
}; |
| 297 |
|
| 298 |
#define FOR_EACH_REALM_FUSE(FUSE) \ |
| 299 |
FUSE(OptimizeGetIteratorFuse, optimizeGetIteratorFuse) \ |
| 300 |
FUSE(OptimizeArrayIteratorPrototypeFuse, optimizeArrayIteratorPrototypeFuse) \ |
| 301 |
FUSE(ArrayPrototypeIteratorFuse, arrayPrototypeIteratorFuse) \ |
| 302 |
FUSE(ArrayPrototypeIteratorNextFuse, arrayPrototypeIteratorNextFuse) \ |
| 303 |
FUSE(ArrayIteratorPrototypeHasNoReturnProperty, \ |
| 304 |
arrayIteratorPrototypeHasNoReturnProperty) \ |
| 305 |
FUSE(IteratorPrototypeHasNoReturnProperty, \ |
| 306 |
iteratorPrototypeHasNoReturnProperty) \ |
| 307 |
FUSE(ArrayIteratorPrototypeHasIteratorProto, \ |
| 308 |
arrayIteratorPrototypeHasIteratorProto) \ |
| 309 |
FUSE(IteratorPrototypeHasObjectProto, iteratorPrototypeHasObjectProto) \ |
| 310 |
FUSE(ObjectPrototypeHasNoReturnProperty, objectPrototypeHasNoReturnProperty) \ |
| 311 |
FUSE(OptimizeArraySpeciesFuse, optimizeArraySpeciesFuse) \ |
| 312 |
FUSE(OptimizeArrayBufferSpeciesFuse, optimizeArrayBufferSpeciesFuse) \ |
| 313 |
FUSE(OptimizeSharedArrayBufferSpeciesFuse, \ |
| 314 |
optimizeSharedArrayBufferSpeciesFuse) \ |
| 315 |
FUSE(OptimizePromiseLookupFuse, optimizePromiseLookupFuse) \ |
| 316 |
FUSE(OptimizeRegExpPrototypeFuse, optimizeRegExpPrototypeFuse) \ |
| 317 |
FUSE(OptimizeStringPrototypeSymbolsFuse, optimizeStringPrototypeSymbolsFuse) \ |
| 318 |
FUSE(OptimizeMapObjectIteratorFuse, optimizeMapObjectIteratorFuse) \ |
| 319 |
FUSE(OptimizeSetObjectIteratorFuse, optimizeSetObjectIteratorFuse) \ |
| 320 |
FUSE(OptimizeMapPrototypeSetFuse, optimizeMapPrototypeSetFuse) \ |
| 321 |
FUSE(OptimizeSetPrototypeAddFuse, optimizeSetPrototypeAddFuse) \ |
| 322 |
FUSE(OptimizeWeakMapPrototypeSetFuse, optimizeWeakMapPrototypeSetFuse) \ |
| 323 |
FUSE(OptimizeWeakSetPrototypeAddFuse, optimizeWeakSetPrototypeAddFuse) |
| 324 |
|
| 325 |
struct RealmFuses { |
| 326 |
RealmFuses() = default; |
| 327 |
|
| 328 |
#define FUSE(Name, LowerName) Name LowerName{}; |
| 329 |
FOR_EACH_REALM_FUSE(FUSE) |
| 330 |
#undef FUSE |
| 331 |
|
| 332 |
void assertInvariants(JSContext* cx) { |
| 333 |
// Generate the invariant checking calls. |
| 334 |
#define FUSE(Name, LowerName) LowerName.assertInvariant(cx); |
| 335 |
FOR_EACH_REALM_FUSE(FUSE) |
| 336 |
#undef FUSE |
| 337 |
} |
| 338 |
|
| 339 |
// Code Generation Code: |
| 340 |
enum class FuseIndex : uint8_t { |
| 341 |
// Generate Fuse Indexes |
| 342 |
#define FUSE(Name, LowerName) Name, |
| 343 |
FOR_EACH_REALM_FUSE(FUSE) |
| 344 |
#undef FUSE |
| 345 |
LastFuseIndex |
| 346 |
}; |
| 347 |
|
| 348 |
GuardFuse* getFuseByIndex(FuseIndex index) { |
| 349 |
switch (index) { |
| 350 |
// Return fuses. |
| 351 |
#define FUSE(Name, LowerName) \ |
| 352 |
case FuseIndex::Name: \ |
| 353 |
return &this->LowerName; |
| 354 |
FOR_EACH_REALM_FUSE(FUSE) |
| 355 |
#undef FUSE |
| 356 |
default: |
| 357 |
break; |
| 358 |
} |
| 359 |
MOZ_CRASH("Fuse Not Found"); |
| 360 |
} |
| 361 |
|
| 362 |
DependentScriptGroup fuseDependencies; |
| 363 |
|
| 364 |
static int32_t fuseOffsets[]; |
| 365 |
static const char* fuseNames[]; |
| 366 |
|
| 367 |
static int32_t offsetOfFuseWordRelativeToRealm(FuseIndex index); |
| 368 |
static const char* getFuseName(FuseIndex index); |
| 369 |
|
| 370 |
#ifdef DEBUG |
| 371 |
static bool isInvalidatingFuse(FuseIndex index) { |
| 372 |
switch (index) { |
| 373 |
# define FUSE(Name, LowerName) \ |
| 374 |
case FuseIndex::Name: \ |
| 375 |
static_assert(std::is_base_of_v<RealmFuse, Name> || \ |
| 376 |
std::is_base_of_v<InvalidatingRealmFuse, Name>); \ |
| 377 |
return std::is_base_of_v<InvalidatingRealmFuse, Name>; |
| 378 |
FOR_EACH_REALM_FUSE(FUSE) |
| 379 |
# undef FUSE |
| 380 |
default: |
| 381 |
break; |
| 382 |
} |
| 383 |
MOZ_CRASH("Fuse Not Found"); |
| 384 |
} |
| 385 |
#endif |
| 386 |
}; |
| 387 |
|
| 388 |
} // namespace js |
| 389 |
|
| 390 |
#endif |