1 | /*
|
---|
2 | * Copyright (C) 2012-2021 Apple Inc. All rights reserved.
|
---|
3 | *
|
---|
4 | * Redistribution and use in source and binary forms, with or without
|
---|
5 | * modification, are permitted provided that the following conditions
|
---|
6 | * are met:
|
---|
7 | * 1. Redistributions of source code must retain the above copyright
|
---|
8 | * notice, this list of conditions and the following disclaimer.
|
---|
9 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer in the
|
---|
11 | * documentation and/or other materials provided with the distribution.
|
---|
12 | *
|
---|
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
|
---|
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
---|
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
|
---|
17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
---|
18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
---|
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
---|
20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
---|
21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
24 | */
|
---|
25 |
|
---|
26 | #pragma once
|
---|
27 |
|
---|
28 | #if ENABLE(JIT)
|
---|
29 |
|
---|
30 | #include "JITStubRoutine.h"
|
---|
31 | #include "JSObject.h"
|
---|
32 | #include "WriteBarrier.h"
|
---|
33 | #include <wtf/FixedVector.h>
|
---|
34 | #include <wtf/Hasher.h>
|
---|
35 | #include <wtf/Vector.h>
|
---|
36 |
|
---|
37 | namespace JSC {
|
---|
38 | namespace DFG {
|
---|
39 | class CodeOriginPool;
|
---|
40 | }
|
---|
41 |
|
---|
42 | class AccessCase;
|
---|
43 | class CallLinkInfo;
|
---|
44 | class JITStubRoutineSet;
|
---|
45 | class OptimizingCallLinkInfo;
|
---|
46 |
|
---|
47 | // Use this stub routine if you know that your code might be on stack when
|
---|
48 | // either GC or other kinds of stub deletion happen. Basicaly, if your stub
|
---|
49 | // routine makes calls (either to JS code or to C++ code) then you should
|
---|
50 | // assume that it's possible for that JS or C++ code to do something that
|
---|
51 | // causes the system to try to delete your routine. Using this routine type
|
---|
52 | // ensures that the actual deletion is delayed until the GC proves that the
|
---|
53 | // routine is no longer running. You can also subclass this routine if you
|
---|
54 | // want to mark additional objects during GC in those cases where the
|
---|
55 | // routine is known to be executing, or if you want to force this routine to
|
---|
56 | // keep other routines alive (for example due to the use of a slow-path
|
---|
57 | // list which does not get reclaimed all at once).
|
---|
58 | class GCAwareJITStubRoutine : public JITStubRoutine {
|
---|
59 | public:
|
---|
60 | GCAwareJITStubRoutine(const MacroAssemblerCodeRef<JITStubRoutinePtrTag>&);
|
---|
61 | ~GCAwareJITStubRoutine() override;
|
---|
62 |
|
---|
63 | static Ref<JITStubRoutine> create(VM& vm, const MacroAssemblerCodeRef<JITStubRoutinePtrTag>& code)
|
---|
64 | {
|
---|
65 | auto stub = adoptRef(*new GCAwareJITStubRoutine(code));
|
---|
66 | stub->makeGCAware(vm);
|
---|
67 | return stub;
|
---|
68 | }
|
---|
69 |
|
---|
70 | template<typename Visitor>
|
---|
71 | void markRequiredObjects(Visitor& visitor)
|
---|
72 | {
|
---|
73 | markRequiredObjectsInternal(visitor);
|
---|
74 | }
|
---|
75 |
|
---|
76 | void deleteFromGC();
|
---|
77 |
|
---|
78 | void makeGCAware(VM&);
|
---|
79 |
|
---|
80 | protected:
|
---|
81 | void observeZeroRefCount() override;
|
---|
82 |
|
---|
83 | virtual void markRequiredObjectsInternal(AbstractSlotVisitor&) { }
|
---|
84 | virtual void markRequiredObjectsInternal(SlotVisitor&) { }
|
---|
85 |
|
---|
86 | private:
|
---|
87 | friend class JITStubRoutineSet;
|
---|
88 |
|
---|
89 | bool m_mayBeExecuting { false };
|
---|
90 | bool m_isJettisoned { false };
|
---|
91 | bool m_isGCAware { false };
|
---|
92 | };
|
---|
93 |
|
---|
94 | class PolymorphicAccessJITStubRoutine : public GCAwareJITStubRoutine {
|
---|
95 | public:
|
---|
96 | using Base = GCAwareJITStubRoutine;
|
---|
97 |
|
---|
98 | PolymorphicAccessJITStubRoutine(const MacroAssemblerCodeRef<JITStubRoutinePtrTag>&, VM&, FixedVector<RefPtr<AccessCase>>&&, FixedVector<StructureID>&&);
|
---|
99 |
|
---|
100 | const FixedVector<RefPtr<AccessCase>>& cases() const { return m_cases; }
|
---|
101 | const FixedVector<StructureID>& weakStructures() const { return m_weakStructures; }
|
---|
102 |
|
---|
103 | unsigned hash() const
|
---|
104 | {
|
---|
105 | if (!m_hash)
|
---|
106 | m_hash = computeHash(m_cases, m_weakStructures);
|
---|
107 | return m_hash;
|
---|
108 | }
|
---|
109 |
|
---|
110 | static unsigned computeHash(const FixedVector<RefPtr<AccessCase>>&, const FixedVector<StructureID>&);
|
---|
111 |
|
---|
112 | protected:
|
---|
113 | void observeZeroRefCount() override;
|
---|
114 |
|
---|
115 | private:
|
---|
116 | VM& m_vm;
|
---|
117 | FixedVector<RefPtr<AccessCase>> m_cases;
|
---|
118 | FixedVector<StructureID> m_weakStructures;
|
---|
119 | };
|
---|
120 |
|
---|
121 | // Use this if you want to mark one additional object during GC if your stub
|
---|
122 | // routine is known to be executing.
|
---|
123 | class MarkingGCAwareJITStubRoutine : public PolymorphicAccessJITStubRoutine {
|
---|
124 | public:
|
---|
125 | using Base = PolymorphicAccessJITStubRoutine;
|
---|
126 |
|
---|
127 | MarkingGCAwareJITStubRoutine(
|
---|
128 | const MacroAssemblerCodeRef<JITStubRoutinePtrTag>&, VM&, FixedVector<RefPtr<AccessCase>>&&, FixedVector<StructureID>&&, const JSCell* owner, const Vector<JSCell*>&, Bag<OptimizingCallLinkInfo>&&);
|
---|
129 | ~MarkingGCAwareJITStubRoutine() override;
|
---|
130 |
|
---|
131 | protected:
|
---|
132 | template<typename Visitor> void markRequiredObjectsInternalImpl(Visitor&);
|
---|
133 | void markRequiredObjectsInternal(AbstractSlotVisitor&) final;
|
---|
134 | void markRequiredObjectsInternal(SlotVisitor&) final;
|
---|
135 |
|
---|
136 | private:
|
---|
137 | FixedVector<WriteBarrier<JSCell>> m_cells;
|
---|
138 | Bag<OptimizingCallLinkInfo> m_callLinkInfos;
|
---|
139 | };
|
---|
140 |
|
---|
141 |
|
---|
142 | // The stub has exception handlers in it. So it clears itself from exception
|
---|
143 | // handling table when it dies. It also frees space in CodeOrigin table
|
---|
144 | // for new exception handlers to use the same DisposableCallSiteIndex.
|
---|
145 | class GCAwareJITStubRoutineWithExceptionHandler final : public MarkingGCAwareJITStubRoutine {
|
---|
146 | public:
|
---|
147 | using Base = MarkingGCAwareJITStubRoutine;
|
---|
148 |
|
---|
149 | GCAwareJITStubRoutineWithExceptionHandler(const MacroAssemblerCodeRef<JITStubRoutinePtrTag>&, VM&, FixedVector<RefPtr<AccessCase>>&&, FixedVector<StructureID>&&, const JSCell* owner, const Vector<JSCell*>&, Bag<OptimizingCallLinkInfo>&&, CodeBlock*, DisposableCallSiteIndex);
|
---|
150 | ~GCAwareJITStubRoutineWithExceptionHandler() final;
|
---|
151 |
|
---|
152 | void aboutToDie() final;
|
---|
153 | void observeZeroRefCount() final;
|
---|
154 |
|
---|
155 | private:
|
---|
156 | CodeBlock* m_codeBlockWithExceptionHandler;
|
---|
157 | #if ENABLE(DFG_JIT)
|
---|
158 | RefPtr<DFG::CodeOriginPool> m_codeOriginPool;
|
---|
159 | #endif
|
---|
160 | DisposableCallSiteIndex m_exceptionHandlerCallSiteIndex;
|
---|
161 | };
|
---|
162 |
|
---|
163 | // Helper for easily creating a GC-aware JIT stub routine. For the varargs,
|
---|
164 | // pass zero or more JSCell*'s. This will either create a JITStubRoutine, a
|
---|
165 | // GCAwareJITStubRoutine, or an ObjectMarkingGCAwareJITStubRoutine as
|
---|
166 | // appropriate. Generally you only need to pass pointers that will be used
|
---|
167 | // after the first call to C++ or JS.
|
---|
168 | //
|
---|
169 | // Ref<PolymorphicAccessJITStubRoutine> createICJITStubRoutine(
|
---|
170 | // const MacroAssemblerCodeRef<JITStubRoutinePtrTag>& code,
|
---|
171 | // VM& vm,
|
---|
172 | // FixedVector<RefPtr<AccessCase>>&& cases,
|
---|
173 | // const JSCell* owner,
|
---|
174 | // bool makesCalls,
|
---|
175 | // ...);
|
---|
176 | //
|
---|
177 | // Note that we don't actually use C-style varargs because that leads to
|
---|
178 | // strange type-related problems. For example it would preclude us from using
|
---|
179 | // our custom of passing '0' as NULL pointer. Besides, when I did try to write
|
---|
180 | // this function using varargs, I ended up with more code than this simple
|
---|
181 | // way.
|
---|
182 |
|
---|
183 | Ref<PolymorphicAccessJITStubRoutine> createICJITStubRoutine(
|
---|
184 | const MacroAssemblerCodeRef<JITStubRoutinePtrTag>&, FixedVector<RefPtr<AccessCase>>&& cases, FixedVector<StructureID>&& weakStructures, VM&, const JSCell* owner, bool makesCalls,
|
---|
185 | const Vector<JSCell*>&, Bag<OptimizingCallLinkInfo>&& callLinkInfos,
|
---|
186 | CodeBlock* codeBlockForExceptionHandlers, DisposableCallSiteIndex exceptionHandlingCallSiteIndex);
|
---|
187 |
|
---|
188 | } // namespace JSC
|
---|
189 |
|
---|
190 | #endif // ENABLE(JIT)
|
---|