2 * Copyright (C) 2009-2018 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
28 #include "ExecutableAllocator.h"
29 #include "JSCPoison.h"
31 #include <wtf/DataLog.h>
32 #include <wtf/PrintStream.h>
33 #include <wtf/RefPtr.h>
34 #include <wtf/text/CString.h>
36 // ASSERT_VALID_CODE_POINTER checks that ptr is a non-null pointer, and that it is a valid
37 // instruction address on the platform (for example, check any alignment requirements).
38 #if CPU(ARM_THUMB2) && ENABLE(JIT)
39 // ARM instructions must be 16-bit aligned. Thumb2 code pointers to be loaded into
40 // into the processor are decorated with the bottom bit set, while traditional ARM has
41 // the lower bit clear. Since we don't know what kind of pointer, we check for both
42 // decorated and undecorated null.
43 #define ASSERT_VALID_CODE_POINTER(ptr) \
44 ASSERT(reinterpret_cast<intptr_t>(ptr) & ~1)
45 #define ASSERT_VALID_CODE_OFFSET(offset) \
46 ASSERT(!(offset & 1)) // Must be multiple of 2.
48 #define ASSERT_VALID_CODE_POINTER(ptr) \
50 #define ASSERT_VALID_CODE_OFFSET(offset) // Anything goes!
55 class MacroAssemblerCodePtr;
57 enum OpcodeID : unsigned;
61 // FunctionPtr should be used to wrap pointers to C/C++ functions in JSC
62 // (particularly, the stub functions).
67 template<typename ReturnType, typename... Arguments>
68 FunctionPtr(ReturnType(*value)(Arguments...), PtrTag tag = SlowPathPtrTag)
69 : m_value(tagCFunctionPtr<void*>(value, tag))
71 PoisonedMasmPtr::assertIsNotPoisoned(m_value);
72 ASSERT_VALID_CODE_POINTER(m_value);
75 // MSVC doesn't seem to treat functions with different calling conventions as
76 // different types; these methods already defined for fastcall, below.
77 #if CALLING_CONVENTION_IS_STDCALL && !OS(WINDOWS)
79 template<typename ReturnType, typename... Arguments>
80 FunctionPtr(ReturnType(CDECL *value)(Arguments...), PtrTag tag = SlowPathPtrTag)
81 : m_value(tagCFunctionPtr<void*>(value, tag))
83 PoisonedMasmPtr::assertIsNotPoisoned(m_value);
84 ASSERT_VALID_CODE_POINTER(m_value);
87 #endif // CALLING_CONVENTION_IS_STDCALL && !OS(WINDOWS)
89 #if COMPILER_SUPPORTS(FASTCALL_CALLING_CONVENTION)
91 template<typename ReturnType, typename... Arguments>
92 FunctionPtr(ReturnType(FASTCALL *value)(Arguments...), PtrTag tag = SlowPathPtrTag)
93 : m_value(tagCFunctionPtr<void*>(value, tag))
95 PoisonedMasmPtr::assertIsNotPoisoned(m_value);
96 ASSERT_VALID_CODE_POINTER(m_value);
99 #endif // COMPILER_SUPPORTS(FASTCALL_CALLING_CONVENTION)
101 template<typename FunctionType>
102 explicit FunctionPtr(FunctionType* value, PtrTag tag = SlowPathPtrTag)
103 // Using a C-ctyle cast here to avoid compiler error on RVTC:
104 // Error: #694: reinterpret_cast cannot cast away const or other type qualifiers
105 // (I guess on RVTC function pointers have a different constness to GCC/MSVC?)
106 : m_value(tagCodePtr<void*>(value, tag))
108 PoisonedMasmPtr::assertIsNotPoisoned(m_value);
109 ASSERT_VALID_CODE_POINTER(m_value);
112 explicit FunctionPtr(MacroAssemblerCodePtr);
116 PoisonedMasmPtr::assertIsNotPoisoned(m_value);
117 return removeCodePtrTag(m_value);
119 void* executableAddress() const
121 PoisonedMasmPtr::assertIsNotPoisoned(m_value);
126 void* m_value { nullptr };
131 // ReturnAddressPtr should be used to wrap return addresses generated by processor
132 // 'call' instructions exectued in JIT code. We use return addresses to look up
133 // exception and optimization information, and to repatch the call instruction
134 // that is the source of the return address.
135 class ReturnAddressPtr {
137 ReturnAddressPtr() { }
139 explicit ReturnAddressPtr(void* value)
142 PoisonedMasmPtr::assertIsNotPoisoned(m_value);
143 ASSERT_VALID_CODE_POINTER(m_value);
146 explicit ReturnAddressPtr(FunctionPtr function)
147 : m_value(function.value())
149 PoisonedMasmPtr::assertIsNotPoisoned(m_value);
150 ASSERT_VALID_CODE_POINTER(m_value);
155 PoisonedMasmPtr::assertIsNotPoisoned(m_value);
159 void dump(PrintStream& out) const
161 out.print(RawPointer(m_value));
165 void* m_value { nullptr };
168 // MacroAssemblerCodePtr:
170 // MacroAssemblerCodePtr should be used to wrap pointers to JIT generated code.
171 class MacroAssemblerCodePtr {
173 MacroAssemblerCodePtr() { }
175 explicit MacroAssemblerCodePtr(void* value)
177 // Decorate the pointer as a thumb code pointer.
178 : m_value(reinterpret_cast<char*>(value) + 1)
183 m_value.assertIsPoisoned();
185 ASSERT_VALID_CODE_POINTER(m_value.unpoisoned());
188 static MacroAssemblerCodePtr createFromExecutableAddress(void* value)
191 ASSERT_VALID_CODE_POINTER(value);
192 MacroAssemblerCodePtr result;
193 result.m_value = PoisonedMasmPtr(value);
194 result.m_value.assertIsPoisoned();
198 static MacroAssemblerCodePtr createLLIntCodePtr(OpcodeID codeId);
200 explicit MacroAssemblerCodePtr(ReturnAddressPtr ra)
201 : m_value(ra.value())
204 m_value.assertIsPoisoned();
205 ASSERT_VALID_CODE_POINTER(m_value.unpoisoned());
208 PoisonedMasmPtr poisonedPtr() const { return m_value; }
210 template<typename T = void*>
211 T executableAddress() const
213 m_value.assertIsPoisoned();
214 return m_value.unpoisoned<T>();
217 // To use this pointer as a data address remove the decoration.
218 template<typename T = void*>
219 T dataLocation() const
221 m_value.assertIsPoisoned();
222 ASSERT_VALID_CODE_POINTER(m_value.unpoisoned());
223 return bitwise_cast<T>(m_value ? m_value.unpoisoned<char*>() - 1 : nullptr);
226 template<typename T = void*>
227 T dataLocation() const
229 m_value.assertIsPoisoned();
230 ASSERT_VALID_CODE_POINTER(m_value);
231 return bitwise_cast<T>(m_value ? removeCodePtrTag(m_value.unpoisoned()) : nullptr);
235 bool operator!() const
237 #if ENABLE(POISON_ASSERTS)
238 if (!isEmptyValue() && !isDeletedValue())
239 m_value.assertIsPoisoned();
243 explicit operator bool() const { return !(!*this); }
245 bool operator==(const MacroAssemblerCodePtr& other) const
247 #if ENABLE(POISON_ASSERTS)
248 if (!isEmptyValue() && !isDeletedValue())
249 m_value.assertIsPoisoned();
250 if (!other.isEmptyValue() && !other.isDeletedValue())
251 other.m_value.assertIsPoisoned();
253 return m_value == other.m_value;
256 // Disallow any casting operations (except for booleans). Instead, the client
257 // should be asking for poisonedPtr() or executableAddress() explicitly.
258 template<typename T, typename = std::enable_if_t<!std::is_same<T, bool>::value>>
259 operator T() = delete;
261 void dumpWithName(const char* name, PrintStream& out) const;
263 void dump(PrintStream& out) const;
265 enum EmptyValueTag { EmptyValue };
266 enum DeletedValueTag { DeletedValue };
268 MacroAssemblerCodePtr(EmptyValueTag)
269 : m_value(emptyValue())
272 MacroAssemblerCodePtr(DeletedValueTag)
273 : m_value(deletedValue())
276 bool isEmptyValue() const { return m_value == emptyValue(); }
277 bool isDeletedValue() const { return m_value == deletedValue(); }
279 unsigned hash() const { return IntHash<uintptr_t>::hash(m_value.bits()); }
281 static void initialize();
284 static PoisonedMasmPtr emptyValue() { return PoisonedMasmPtr(AlreadyPoisoned, 1); }
285 static PoisonedMasmPtr deletedValue() { return PoisonedMasmPtr(AlreadyPoisoned, 2); }
287 PoisonedMasmPtr m_value;
290 struct MacroAssemblerCodePtrHash {
291 static unsigned hash(const MacroAssemblerCodePtr& ptr) { return ptr.hash(); }
292 static bool equal(const MacroAssemblerCodePtr& a, const MacroAssemblerCodePtr& b)
296 static const bool safeToCompareToEmptyOrDeleted = true;
299 // MacroAssemblerCodeRef:
301 // A reference to a section of JIT generated code. A CodeRef consists of a
302 // pointer to the code, and a ref pointer to the pool from within which it
304 class MacroAssemblerCodeRef {
306 // This is private because it's dangerous enough that we want uses of it
307 // to be easy to find - hence the static create method below.
308 explicit MacroAssemblerCodeRef(MacroAssemblerCodePtr codePtr)
315 MacroAssemblerCodeRef()
319 MacroAssemblerCodeRef(Ref<ExecutableMemoryHandle>&& executableMemory, PtrTag tag)
320 : m_codePtr(tagCodePtr(executableMemory->start(), tag))
321 , m_executableMemory(WTFMove(executableMemory))
323 ASSERT(m_executableMemory->isManaged());
324 ASSERT(m_executableMemory->start());
328 // Use this only when you know that the codePtr refers to code that is
329 // already being kept alive through some other means. Typically this means
330 // that codePtr is immortal.
331 static MacroAssemblerCodeRef createSelfManagedCodeRef(MacroAssemblerCodePtr codePtr)
333 return MacroAssemblerCodeRef(codePtr);
336 // Helper for creating self-managed code refs from LLInt.
337 static MacroAssemblerCodeRef createLLIntCodeRef(OpcodeID codeId);
339 ExecutableMemoryHandle* executableMemory() const
341 return m_executableMemory.get();
344 MacroAssemblerCodePtr code() const
349 MacroAssemblerCodePtr retaggedCode(PtrTag oldTag, PtrTag newTag) const
351 return MacroAssemblerCodePtr(retagCodePtr(m_codePtr.executableAddress(), oldTag, newTag));
356 if (!m_executableMemory)
358 return m_executableMemory->sizeInBytes();
361 bool tryToDisassemble(PrintStream& out, const char* prefix = "") const;
363 bool tryToDisassemble(const char* prefix = "") const;
365 JS_EXPORT_PRIVATE CString disassembly() const;
367 explicit operator bool() const { return !!m_codePtr; }
369 void dump(PrintStream& out) const;
372 MacroAssemblerCodePtr m_codePtr;
373 RefPtr<ExecutableMemoryHandle> m_executableMemory;
376 inline FunctionPtr::FunctionPtr(MacroAssemblerCodePtr ptr)
377 : m_value(ptr.executableAddress())
379 PoisonedMasmPtr::assertIsNotPoisoned(m_value);
380 ASSERT_VALID_CODE_POINTER(m_value);
387 template<typename T> struct DefaultHash;
388 template<> struct DefaultHash<JSC::MacroAssemblerCodePtr> {
389 typedef JSC::MacroAssemblerCodePtrHash Hash;
392 template<typename T> struct HashTraits;
393 template<> struct HashTraits<JSC::MacroAssemblerCodePtr> : public CustomHashTraits<JSC::MacroAssemblerCodePtr> { };