JITStubRoutine.h 6.6 KB

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