1/*
2 * Copyright (C) 2012, 2014 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#ifndef JITStubRoutine_h
27#define JITStubRoutine_h
28
29#if ENABLE(JIT)
30
31#include "ExecutableAllocator.h"
32#include "MacroAssemblerCodeRef.h"
33#include <wtf/RefCounted.h>
34#include <wtf/Vector.h>
35
36namespace JSC {
37
38class JITStubRoutineSet;
39
40// This is a base-class for JIT stub routines, and also the class you want
41// to instantiate directly if you have a routine that does not need any
42// help from the GC. If in doubt, use one of the other stub routines. But
43// if you know for sure that the stub routine cannot be on the stack while
44// someone triggers a stub routine reset, then using this will speed up
45// memory reclamation. One case where a stub routine satisfies this
46// condition is if it doesn't make any calls, to either C++ or JS code. In
47// such a routine you know that it cannot be on the stack when anything
48// interesting happens.
49// See GCAwareJITStubRoutine.h for the other stub routines.
50class JITStubRoutine {
51 WTF_MAKE_NONCOPYABLE(JITStubRoutine);
52 WTF_MAKE_FAST_ALLOCATED;
53public:
54 JITStubRoutine(const MacroAssemblerCodeRef& code)
55 : m_code(code)
56 , m_refCount(1)
57 {
58 }
59
60 // Use this if you want to pass a CodePtr to someone who insists on taking
61 // a RefPtr<JITStubRoutine>.
62 static Ref<JITStubRoutine> createSelfManagedRoutine(
63 MacroAssemblerCodePtr rawCodePointer)
64 {
65 return adoptRef(*new JITStubRoutine(MacroAssemblerCodeRef::createSelfManagedCodeRef(rawCodePointer)));
66 }
67
68 virtual ~JITStubRoutine();
69 virtual void aboutToDie() { }
70
71 // MacroAssemblerCodeRef is copyable, but at the cost of reference
72 // counting churn. Returning a reference is a good way of reducing
73 // the churn.
74 const MacroAssemblerCodeRef& code() const { return m_code; }
75
76 static MacroAssemblerCodePtr asCodePtr(PassRefPtr<JITStubRoutine> stubRoutine)
77 {
78 if (!stubRoutine)
79 return MacroAssemblerCodePtr();
80
81 MacroAssemblerCodePtr result = stubRoutine->code().code();
82 ASSERT(!!result);
83 return result;
84 }
85
86 void ref()
87 {
88 m_refCount++;
89 }
90
91 void deref()
92 {
93 if (--m_refCount)
94 return;
95 observeZeroRefCount();
96 }
97
98 // Helpers for the GC to determine how to deal with marking JIT stub
99 // routines.
100 uintptr_t startAddress() const { return m_code.executableMemory()->startAsInteger(); }
101 uintptr_t endAddress() const { return m_code.executableMemory()->endAsInteger(); }
102 static uintptr_t addressStep() { return jitAllocationGranule; }
103
104 static bool canPerformRangeFilter()
105 {
106#if ENABLE(EXECUTABLE_ALLOCATOR_FIXED)
107 return true;
108#else
109 return false;
110#endif
111 }
112 static uintptr_t filteringStartAddress()
113 {
114#if ENABLE(EXECUTABLE_ALLOCATOR_FIXED)
115 return startOfFixedExecutableMemoryPool;
116#else
117 UNREACHABLE_FOR_PLATFORM();
118 return 0;
119#endif
120 }
121 static size_t filteringExtentSize()
122 {
123#if ENABLE(EXECUTABLE_ALLOCATOR_FIXED)
124 return fixedExecutableMemoryPoolSize;
125#else
126 UNREACHABLE_FOR_PLATFORM();
127 return 0;
128#endif
129 }
130 static bool passesFilter(uintptr_t address)
131 {
132 if (!canPerformRangeFilter()) {
133 // Just check that the address doesn't use any special values that would make
134 // our hashtables upset.
135 return address >= jitAllocationGranule && address != std::numeric_limits<uintptr_t>::max();
136 }
137
138 if (address - filteringStartAddress() >= filteringExtentSize())
139 return false;
140
141 return true;
142 }
143
144 // Return true if you are still valid after. Return false if you are now invalid. If you return
145 // false, you will usually not do any clearing because the idea is that you will simply be
146 // destroyed.
147 virtual bool visitWeak(VM&);
148
149protected:
150 virtual void observeZeroRefCount();
151
152 MacroAssemblerCodeRef m_code;
153 unsigned m_refCount;
154};
155
156// Helper for the creation of simple stub routines that need no help from the GC.
157#define FINALIZE_CODE_FOR_STUB(codeBlock, patchBuffer, dataLogFArguments) \
158 (adoptRef(new JITStubRoutine(FINALIZE_CODE_FOR((codeBlock), (patchBuffer), dataLogFArguments))))
159
160} // namespace JSC
161
162#endif // ENABLE(JIT)
163
164#endif // JITStubRoutine_h
165
166