Debian Codegraph @

js/src/vm/GuardFuse.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_GuardFuse_h
8 #define vm_GuardFuse_h
9
10 #include "mozilla/Array.h"
11 #include "mozilla/Assertions.h"
12
13 #include <stddef.h>
14
15 #include "jstypes.h"
16
17 struct JS_PUBLIC_API JSContext;
18
19 namespace js {
20
21 // [SMDOC] Fuses
22 // A Fuse is a data structure in memory that asserts some property of the
23 // system.
24 //
25 // A fuse may be popped; this indicates that the property the fuse was asserting
26 // no longer holds.
27 //
28 // There are two models of Fuse:
29 // 1. Guard fuses are used as guards at various points through the engine to
30 // allow dynamic choice of a fallback path vs. fast path. A guard is checked
31 // explicitly before each action.
32 // 2. An Invalidating Fuse instead performs some cleanup actions invalidating
33 // Ion compiled under the assumption the fuse is intact.
34 //
35 // Fuses are designed to be checked easily for validity -- a single load and
36 // compare should suffice.
37 //
38 //
39 // In order to try make bugs in Fuses fuzzable, each fuse has a
40 // `checkInvariants` callback. This callback should return `true` iff the
41 // invariant the fuse asserts still holds. We can then assert that if a fuse is
42 // intact, then its invariant should hold. This can be triggered by a shell
43 // testing function, or (not implemented) triggered on a regular basis ala
44 // JS_GC_ZEAL.
45 //
46 // Fuses are agnostic about how they are popped. To support watching for a few
47 // operations we expect to be important to fuses, see:
48 //
49 // - Watchtower::watchPropertyModification
50 // - Watchtower::watchProtoChange
51 // - Watchtower::watchPropertyModification.
52 //
53 // and ObjectFlags::HasFuseProperty. As well, see MGuardFuse::aliasSet, which
54 // should be updated if there is any modification to the possible set of places
55 // fuses could be popped.
56 //
57 // In order to support relationships, the popFuse method is virtual and can be
58 // overridden by subclasses. See RealmFuses.h for examples of how RealmScoped
59 // fuses are implemented there through overrides of popFuse.
60 class GuardFuse {
61 public:
62 GuardFuse() = default;
63 GuardFuse(GuardFuse&&) = delete;
64
65 virtual const char* name() = 0;
66
67 // Basic fuse interface: Takes a JSContext argument for subclasses.
68 virtual void popFuse(JSContext* cx) {
69 MOZ_ASSERT_IF(fuse_, fuse_ == PoppedFuseValue);
70 fuse_ = PoppedFuseValue;
71 }
72
73 bool intact() {
74 MOZ_ASSERT_IF(fuse_, fuse_ == PoppedFuseValue);
75 return fuse_ == 0;
76 }
77
78 GuardFuse* self() { return this; }
79
80 // Code-Generation Fuse interface
81 size_t* fuseRef() { return &fuse_; }
82 static int32_t fuseOffset() { return offsetof(GuardFuse, fuse_); }
83
84 // Invariant Maintenance Interface: If an invariant doesn't hold, we should
85 // crash the process.
86 //
87 // Since a fuse is a statement about an invariant in the system, if the fuse
88 // is intact, the invariant must hold. However, the converse is not true:
89 // a fuse may be popped while its invariant still holds (and for testing
90 // purposes we explicitly require this; see popAllFusesInRealm).
91 virtual void assertInvariant(JSContext* cx) {
92 if (intact()) {
93 if (!checkInvariant(cx)) {
94 fprintf(stderr, "Fuse %s failed invariant check\n", name());
95 MOZ_CRASH("Failed invariant check");
96 }
97 }
98 }
99
100 // Return true iff the invariant asserted by a particular fuse holds; this can
101 // safely return false if the fuse is popped.
102 virtual bool checkInvariant(JSContext* cx) = 0;
103
104 private:
105 // Use a bit pattern to mark a popped fuse -- May be useful in the future for
106 // disambiguating between a real popped fuse and a bit-flip.
107 static constexpr size_t PoppedFuseValue = 0x808;
108
109 size_t fuse_ = 0;
110 };
111
112 } // namespace js
113 #endif // vm_GuardFuse_h