1 | /* |
2 | * Copyright (C) 2012-2018 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/Vector.h> |
34 | |
35 | namespace JSC { |
36 | |
37 | class JITStubRoutineSet; |
38 | |
39 | // Use this stub routine if you know that your code might be on stack when |
40 | // either GC or other kinds of stub deletion happen. Basicaly, if your stub |
41 | // routine makes calls (either to JS code or to C++ code) then you should |
42 | // assume that it's possible for that JS or C++ code to do something that |
43 | // causes the system to try to delete your routine. Using this routine type |
44 | // ensures that the actual deletion is delayed until the GC proves that the |
45 | // routine is no longer running. You can also subclass this routine if you |
46 | // want to mark additional objects during GC in those cases where the |
47 | // routine is known to be executing, or if you want to force this routine to |
48 | // keep other routines alive (for example due to the use of a slow-path |
49 | // list which does not get reclaimed all at once). |
50 | class GCAwareJITStubRoutine : public JITStubRoutine { |
51 | public: |
52 | GCAwareJITStubRoutine(const MacroAssemblerCodeRef<JITStubRoutinePtrTag>&, VM&); |
53 | virtual ~GCAwareJITStubRoutine(); |
54 | |
55 | void markRequiredObjects(SlotVisitor& visitor) |
56 | { |
57 | markRequiredObjectsInternal(visitor); |
58 | } |
59 | |
60 | void deleteFromGC(); |
61 | |
62 | protected: |
63 | void observeZeroRefCount() override; |
64 | |
65 | virtual void markRequiredObjectsInternal(SlotVisitor&); |
66 | |
67 | private: |
68 | friend class JITStubRoutineSet; |
69 | |
70 | bool m_mayBeExecuting { false }; |
71 | bool m_isJettisoned { false }; |
72 | }; |
73 | |
74 | // Use this if you want to mark one additional object during GC if your stub |
75 | // routine is known to be executing. |
76 | class MarkingGCAwareJITStubRoutine : public GCAwareJITStubRoutine { |
77 | public: |
78 | MarkingGCAwareJITStubRoutine( |
79 | const MacroAssemblerCodeRef<JITStubRoutinePtrTag>&, VM&, const JSCell* owner, const Vector<JSCell*>&); |
80 | virtual ~MarkingGCAwareJITStubRoutine(); |
81 | |
82 | protected: |
83 | void markRequiredObjectsInternal(SlotVisitor&) override; |
84 | |
85 | private: |
86 | Vector<WriteBarrier<JSCell>> m_cells; |
87 | }; |
88 | |
89 | |
90 | // The stub has exception handlers in it. So it clears itself from exception |
91 | // handling table when it dies. It also frees space in CodeOrigin table |
92 | // for new exception handlers to use the same DisposableCallSiteIndex. |
93 | class GCAwareJITStubRoutineWithExceptionHandler : public MarkingGCAwareJITStubRoutine { |
94 | public: |
95 | typedef GCAwareJITStubRoutine Base; |
96 | |
97 | GCAwareJITStubRoutineWithExceptionHandler(const MacroAssemblerCodeRef<JITStubRoutinePtrTag>&, VM&, const JSCell* owner, const Vector<JSCell*>&, CodeBlock*, DisposableCallSiteIndex); |
98 | |
99 | void aboutToDie() override; |
100 | void observeZeroRefCount() override; |
101 | |
102 | private: |
103 | CodeBlock* m_codeBlockWithExceptionHandler; |
104 | DisposableCallSiteIndex m_exceptionHandlerCallSiteIndex; |
105 | }; |
106 | |
107 | // Helper for easily creating a GC-aware JIT stub routine. For the varargs, |
108 | // pass zero or more JSCell*'s. This will either create a JITStubRoutine, a |
109 | // GCAwareJITStubRoutine, or an ObjectMarkingGCAwareJITStubRoutine as |
110 | // appropriate. Generally you only need to pass pointers that will be used |
111 | // after the first call to C++ or JS. |
112 | // |
113 | // Ref<JITStubRoutine> createJITStubRoutine( |
114 | // const MacroAssemblerCodeRef<JITStubRoutinePtrTag>& code, |
115 | // VM& vm, |
116 | // const JSCell* owner, |
117 | // bool makesCalls, |
118 | // ...); |
119 | // |
120 | // Note that we don't actually use C-style varargs because that leads to |
121 | // strange type-related problems. For example it would preclude us from using |
122 | // our custom of passing '0' as NULL pointer. Besides, when I did try to write |
123 | // this function using varargs, I ended up with more code than this simple |
124 | // way. |
125 | |
126 | Ref<JITStubRoutine> createJITStubRoutine( |
127 | const MacroAssemblerCodeRef<JITStubRoutinePtrTag>&, VM&, const JSCell* owner, bool makesCalls, |
128 | const Vector<JSCell*>& = { }, |
129 | CodeBlock* codeBlockForExceptionHandlers = nullptr, DisposableCallSiteIndex exceptionHandlingCallSiteIndex = DisposableCallSiteIndex()); |
130 | |
131 | } // namespace JSC |
132 | |
133 | #endif // ENABLE(JIT) |
134 | |