ServerThreadContext.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #include "Backend.h"
  6. #if ENABLE_OOP_NATIVE_CODEGEN
  7. #include "JITServer/JITServer.h"
  8. ServerThreadContext::ServerThreadContext(ThreadContextDataIDL * data, HANDLE processHandle) :
  9. m_autoProcessHandle(processHandle),
  10. m_processHandle(processHandle),
  11. m_threadContextData(*data),
  12. m_refCount(0),
  13. m_numericPropertyBV(nullptr),
  14. m_preReservedSectionAllocator(processHandle),
  15. m_sectionAllocator(processHandle),
  16. m_thunkPageAllocators(nullptr, /* allocXData */ false, &m_sectionAllocator, nullptr, processHandle),
  17. m_codePageAllocators(nullptr, ALLOC_XDATA, &m_sectionAllocator, &m_preReservedSectionAllocator, processHandle),
  18. m_codeGenAlloc(nullptr, nullptr, &m_codePageAllocators, processHandle),
  19. #if defined(_CONTROL_FLOW_GUARD) && (_M_IX86 || _M_X64)
  20. m_jitThunkEmitter(this, &m_sectionAllocator, processHandle),
  21. #endif
  22. m_pageAlloc(nullptr, Js::Configuration::Global.flags, PageAllocatorType_BGJIT,
  23. AutoSystemInfo::Data.IsLowMemoryProcess() ?
  24. PageAllocator::DefaultLowMaxFreePageCount :
  25. PageAllocator::DefaultMaxFreePageCount
  26. )
  27. {
  28. m_pid = GetProcessId(processHandle);
  29. #if !_M_X64_OR_ARM64 && _CONTROL_FLOW_GUARD
  30. m_codeGenAlloc.canCreatePreReservedSegment = data->allowPrereserveAlloc != FALSE;
  31. #endif
  32. m_numericPropertyBV = HeapNew(BVSparse<HeapAllocator>, &HeapAllocator::Instance);
  33. }
  34. ServerThreadContext::~ServerThreadContext()
  35. {
  36. if (this->m_numericPropertyBV != nullptr)
  37. {
  38. HeapDelete(m_numericPropertyBV);
  39. this->m_numericPropertyBV = nullptr;
  40. }
  41. }
  42. PreReservedSectionAllocWrapper *
  43. ServerThreadContext::GetPreReservedSectionAllocator()
  44. {
  45. return &m_preReservedSectionAllocator;
  46. }
  47. intptr_t
  48. ServerThreadContext::GetBailOutRegisterSaveSpaceAddr() const
  49. {
  50. return static_cast<intptr_t>(m_threadContextData.bailOutRegisterSaveSpaceAddr);
  51. }
  52. ptrdiff_t
  53. ServerThreadContext::GetChakraBaseAddressDifference() const
  54. {
  55. return GetRuntimeChakraBaseAddress() - (intptr_t)AutoSystemInfo::Data.GetChakraBaseAddr();
  56. }
  57. ptrdiff_t
  58. ServerThreadContext::GetCRTBaseAddressDifference() const
  59. {
  60. return GetRuntimeCRTBaseAddress() - GetJITCRTBaseAddress();
  61. }
  62. intptr_t
  63. ServerThreadContext::GetDisableImplicitFlagsAddr() const
  64. {
  65. return static_cast<intptr_t>(m_threadContextData.disableImplicitFlagsAddr);
  66. }
  67. intptr_t
  68. ServerThreadContext::GetImplicitCallFlagsAddr() const
  69. {
  70. return static_cast<intptr_t>(m_threadContextData.implicitCallFlagsAddr);
  71. }
  72. #if defined(ENABLE_SIMDJS) && (defined(_M_IX86) || defined(_M_X64))
  73. intptr_t
  74. ServerThreadContext::GetSimdTempAreaAddr(uint8 tempIndex) const
  75. {
  76. Assert(tempIndex < SIMD_TEMP_SIZE);
  77. return m_threadContextData.simdTempAreaBaseAddr + tempIndex * sizeof(_x86_SIMDValue);
  78. }
  79. #endif
  80. intptr_t
  81. ServerThreadContext::GetThreadStackLimitAddr() const
  82. {
  83. return static_cast<intptr_t>(m_threadContextData.threadStackLimitAddr);
  84. }
  85. size_t
  86. ServerThreadContext::GetScriptStackLimit() const
  87. {
  88. return static_cast<size_t>(m_threadContextData.scriptStackLimit);
  89. }
  90. bool
  91. ServerThreadContext::IsThreadBound() const
  92. {
  93. return m_threadContextData.isThreadBound != FALSE;
  94. }
  95. HANDLE
  96. ServerThreadContext::GetProcessHandle() const
  97. {
  98. return m_autoProcessHandle.GetHandle();
  99. }
  100. CustomHeap::OOPCodePageAllocators *
  101. ServerThreadContext::GetThunkPageAllocators()
  102. {
  103. return &m_thunkPageAllocators;
  104. }
  105. CustomHeap::OOPCodePageAllocators *
  106. ServerThreadContext::GetCodePageAllocators()
  107. {
  108. return &m_codePageAllocators;
  109. }
  110. SectionAllocWrapper *
  111. ServerThreadContext::GetSectionAllocator()
  112. {
  113. return &m_sectionAllocator;
  114. }
  115. OOPCodeGenAllocators *
  116. ServerThreadContext::GetCodeGenAllocators()
  117. {
  118. return &m_codeGenAlloc;
  119. }
  120. #if defined(_CONTROL_FLOW_GUARD) && (_M_IX86 || _M_X64)
  121. OOPJITThunkEmitter *
  122. ServerThreadContext::GetJITThunkEmitter()
  123. {
  124. return &m_jitThunkEmitter;
  125. }
  126. #endif
  127. intptr_t
  128. ServerThreadContext::GetRuntimeChakraBaseAddress() const
  129. {
  130. return static_cast<intptr_t>(m_threadContextData.chakraBaseAddress);
  131. }
  132. intptr_t
  133. ServerThreadContext::GetRuntimeCRTBaseAddress() const
  134. {
  135. return static_cast<intptr_t>(m_threadContextData.crtBaseAddress);
  136. }
  137. /* static */
  138. intptr_t
  139. ServerThreadContext::GetJITCRTBaseAddress()
  140. {
  141. return (intptr_t)AutoSystemInfo::GetCRTHandle();
  142. }
  143. PageAllocator *
  144. ServerThreadContext::GetForegroundPageAllocator()
  145. {
  146. return &m_pageAlloc;
  147. }
  148. bool
  149. ServerThreadContext::IsNumericProperty(Js::PropertyId propertyId)
  150. {
  151. if (propertyId >= 0 && Js::IsInternalPropertyId(propertyId))
  152. {
  153. return Js::InternalPropertyRecords::GetInternalPropertyName(propertyId)->IsNumeric();
  154. }
  155. bool found = false;
  156. {
  157. AutoCriticalSection lock(&m_cs);
  158. found = m_numericPropertyBV->Test(propertyId) != FALSE;
  159. }
  160. return found;
  161. }
  162. void
  163. ServerThreadContext::UpdateNumericPropertyBV(BVSparseNode * newProps)
  164. {
  165. AutoCriticalSection lock(&m_cs);
  166. m_numericPropertyBV->CopyFromNode(newProps);
  167. }
  168. void ServerThreadContext::AddRef()
  169. {
  170. InterlockedExchangeAdd(&m_refCount, (uint)1);
  171. }
  172. void ServerThreadContext::Release()
  173. {
  174. InterlockedExchangeSubtract(&m_refCount, (uint)1);
  175. if (m_isClosed && m_refCount == 0)
  176. {
  177. HeapDelete(this);
  178. }
  179. }
  180. void ServerThreadContext::Close()
  181. {
  182. this->m_isClosed = true;
  183. #ifdef STACK_BACK_TRACE
  184. ServerContextManager::RecordCloseContext(this);
  185. #endif
  186. }
  187. #endif