Debian Codegraph @

js/src/vm/InvalidatingFuse.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/InvalidatingFuse.h"
8
9 #include "gc/PublicIterators.h"
10 #include "jit/Invalidation.h"
11 #include "jit/JitSpewer.h"
12 #include "vm/JSContext.h"
13 #include "vm/JSScript.h"
14 #include "vm/Logging.h"
15
16 #include "gc/StableCellHasher-inl.h"
17 #include "vm/JSScript-inl.h"
18
19 js::DependentScriptSet::DependentScriptSet(JSContext* cx,
20 InvalidatingFuse* fuse)
21 : associatedFuse(fuse), weakScripts(cx->runtime()) {}
22
23 bool js::InvalidatingRuntimeFuse::addFuseDependency(JSContext* cx,
24 Handle<JSScript*> script) {
25 auto* zone = script->zone();
26 DependentScriptSet* dss =
27 zone->fuseDependencies.getOrCreateDependentScriptSet(cx, this);
28 if (!dss) {
29 return false;
30 }
31
32 return dss->addScriptForFuse(this, script);
33 }
34
35 void js::InvalidatingRuntimeFuse::popFuse(JSContext* cx) {
36 // Pop the fuse in the base class
37 GuardFuse::popFuse(cx);
38 JS_LOG(fuseInvalidation, Verbose, "Invalidating fuse popping: %s", name());
39 // do invalidation.
40 for (AllZonesIter z(cx->runtime()); !z.done(); z.next()) {
41 // There's one dependent script set per fuse; just iterate over them all to
42 // find the one we need (see comment on JS::Zone::fuseDependencies for
43 // reasoning).
44 for (auto& fd : z.get()->fuseDependencies) {
45 fd.invalidateForFuse(cx, this);
46 }
47 }
48 }
49
50 void js::DependentScriptSet::invalidateForFuse(JSContext* cx,
51 InvalidatingFuse* fuse) {
52 if (associatedFuse != fuse) {
53 return;
54 }
55
56 jit::InvalidateAndClearScriptSet(cx, weakScripts, "fuse");
57 }
58
59 void js::jit::InvalidateAndClearScriptSet(JSContext* cx,
60 WeakScriptCache& scripts,
61 const char* reason) {
62 // Move the cache contents into this local -- this clears the other one, and
63 // also protects from js::jit::Invalidate trying to modify scripts out from
64 // under us. See ClearPendingInvalidationDependencies.
65 WeakScriptSet localScripts = scripts.stealContents();
66 MOZ_ASSERT(scripts.empty());
67
68 for (auto r = localScripts.all(); !r.empty(); r.popFront()) {
69 JSScript* script = r.front().get();
70 // A script may have lost its ion script for other reasons
71 // by the time this is invoked, so need to ensure it's still there
72 // before calling invalidate.
73 if (script->hasIonScript()) {
74 JitSpew(jit::JitSpew_IonInvalidate, "Invalidating ion script %p for %s",
75 script->ionScript(), reason);
76 JS_LOG(fuseInvalidation, Debug,
77 "Invalidating ion script %s:%d for reason %s", script->filename(),
78 script->lineno(), reason);
79 js::jit::Invalidate(cx, script);
80 }
81 }
82 }
83
84 bool js::DependentScriptSet::addScriptForFuse(InvalidatingFuse* fuse,
85 Handle<JSScript*> script) {
86 MOZ_ASSERT(fuse == associatedFuse);
87 return jit::AddScriptToSet(weakScripts, script);
88 }
89
90 js::DependentScriptSet* js::DependentScriptGroup::getOrCreateDependentScriptSet(
91 JSContext* cx, js::InvalidatingFuse* fuse) {
92 for (auto& dss : dependencies) {
93 if (dss.associatedFuse == fuse) {
94 return &dss;
95 }
96 }
97
98 if (!dependencies.emplaceBack(cx, fuse)) {
99 return nullptr;
100 }
101
102 auto& dss = dependencies.back();
103 MOZ_ASSERT(dss.associatedFuse == fuse);
104 return &dss;
105 }
106
107 bool js::jit::AddScriptToSet(WeakScriptCache& scripts,
108 Handle<JSScript*> script) {
109 js::jit::WeakScriptSet::AddPtr p = scripts.lookupForAdd(script);
110 if (!p) {
111 if (!scripts.add(p, script)) {
112 return false;
113 }
114 }
115
116 // Script is already in the set, no need to re-add.
117 return true;
118 }
119
120 void js::jit::RemoveFromScriptSet(WeakScriptCache& scripts, JSScript* script) {
121 js::jit::WeakScriptSet::Ptr p = scripts.lookup(script);
122 if (p) {
123 scripts.remove(p);
124 }
125 }