ThreadContextInfo.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. ThreadContextInfo::ThreadContextInfo(ThreadContextData * data) :
  7. m_threadContextData(*data),
  8. m_policyManager(true),
  9. m_pageAlloc(&m_policyManager, Js::Configuration::Global.flags, PageAllocatorType_BGJIT,
  10. AutoSystemInfo::Data.IsLowMemoryProcess() ?
  11. PageAllocator::DefaultLowMaxFreePageCount :
  12. PageAllocator::DefaultMaxFreePageCount),
  13. m_codeGenAlloc(&m_policyManager, nullptr, (HANDLE)data->processHandle),
  14. m_isAllJITCodeInPreReservedRegion(true),
  15. m_jitChakraBaseAddress((intptr_t)GetModuleHandle(L"Chakra.dll")), // TODO: OOP JIT, don't hardcode name
  16. m_jitCRTBaseAddress((intptr_t)GetModuleHandle(UCrtC99MathApis::LibraryName)),
  17. m_delayLoadWinCoreProcessThreads()
  18. {
  19. }
  20. intptr_t
  21. ThreadContextInfo::GetNullFrameDisplayAddr() const
  22. {
  23. return SHIFT_ADDR(this, &Js::NullFrameDisplay);
  24. }
  25. intptr_t
  26. ThreadContextInfo::GetStrictNullFrameDisplayAddr() const
  27. {
  28. return SHIFT_ADDR(this, &Js::StrictNullFrameDisplay);
  29. }
  30. intptr_t
  31. ThreadContextInfo::GetAbsDoubleCstAddr() const
  32. {
  33. return SHIFT_ADDR(this, &Js::JavascriptNumber::AbsDoubleCst);
  34. }
  35. intptr_t
  36. ThreadContextInfo::GetAbsFloatCstAddr() const
  37. {
  38. return SHIFT_ADDR(this, &Js::JavascriptNumber::AbsFloatCst);
  39. }
  40. intptr_t
  41. ThreadContextInfo::GetMaskNegFloatAddr() const
  42. {
  43. return SHIFT_ADDR(this, &Js::JavascriptNumber::MaskNegFloat);
  44. }
  45. intptr_t
  46. ThreadContextInfo::GetMaskNegDoubleAddr() const
  47. {
  48. return SHIFT_ADDR(this, &Js::JavascriptNumber::MaskNegDouble);
  49. }
  50. intptr_t
  51. ThreadContextInfo::GetUIntConvertConstAddr() const
  52. {
  53. return SHIFT_ADDR(this, &Js::JavascriptNumber::UIntConvertConst);
  54. }
  55. intptr_t
  56. ThreadContextInfo::GetUint8ClampedArraySetItemAddr() const
  57. {
  58. return SHIFT_ADDR(this, (BOOL(*)(Js::Uint8ClampedArray * arr, uint32 index, Js::Var value))&Js::Uint8ClampedArray::DirectSetItem);
  59. }
  60. intptr_t
  61. ThreadContextInfo::GetConstructorCacheDefaultInstanceAddr() const
  62. {
  63. return SHIFT_ADDR(this, &Js::ConstructorCache::DefaultInstance);
  64. }
  65. intptr_t
  66. ThreadContextInfo::GetJavascriptObjectNewInstanceAddr() const
  67. {
  68. return SHIFT_ADDR(this, &Js::JavascriptObject::EntryInfo::NewInstance);
  69. }
  70. intptr_t
  71. ThreadContextInfo::GetDoubleOnePointZeroAddr() const
  72. {
  73. return SHIFT_ADDR(this, &Js::JavascriptNumber::ONE_POINT_ZERO);
  74. }
  75. intptr_t
  76. ThreadContextInfo::GetDoublePointFiveAddr() const
  77. {
  78. return SHIFT_ADDR(this, &Js::JavascriptNumber::k_PointFive);
  79. }
  80. intptr_t
  81. ThreadContextInfo::GetFloatPointFiveAddr() const
  82. {
  83. return SHIFT_ADDR(this, &Js::JavascriptNumber::k_Float32PointFive);
  84. }
  85. intptr_t
  86. ThreadContextInfo::GetDoubleNegPointFiveAddr() const
  87. {
  88. return SHIFT_ADDR(this, &Js::JavascriptNumber::k_NegPointFive);
  89. }
  90. intptr_t
  91. ThreadContextInfo::GetFloatNegPointFiveAddr() const
  92. {
  93. return SHIFT_ADDR(this, &Js::JavascriptNumber::k_Float32NegPointFive);
  94. }
  95. intptr_t
  96. ThreadContextInfo::GetDoubleTwoToFractionAddr() const
  97. {
  98. return SHIFT_ADDR(this, &Js::JavascriptNumber::k_TwoToFraction);
  99. }
  100. intptr_t
  101. ThreadContextInfo::GetFloatTwoToFractionAddr() const
  102. {
  103. return SHIFT_ADDR(this, &Js::JavascriptNumber::k_Float32TwoToFraction);
  104. }
  105. intptr_t
  106. ThreadContextInfo::GetDoubleNegTwoToFractionAddr() const
  107. {
  108. return SHIFT_ADDR(this, &Js::JavascriptNumber::k_NegTwoToFraction);
  109. }
  110. intptr_t
  111. ThreadContextInfo::GetFloatNegTwoToFractionAddr() const
  112. {
  113. return SHIFT_ADDR(this, &Js::JavascriptNumber::k_Float32NegTwoToFraction);
  114. }
  115. intptr_t
  116. ThreadContextInfo::GetDoubleZeroAddr() const
  117. {
  118. return SHIFT_ADDR(this, &Js::JavascriptNumber::k_Zero);
  119. }
  120. intptr_t
  121. ThreadContextInfo::GetFloatZeroAddr() const
  122. {
  123. return SHIFT_ADDR(this, &Js::JavascriptNumber::k_Float32Zero);
  124. }
  125. intptr_t
  126. ThreadContextInfo::GetNativeFloatArrayMissingItemAddr() const
  127. {
  128. return SHIFT_ADDR(this, &Js::JavascriptNativeFloatArray::MissingItem);
  129. }
  130. intptr_t
  131. ThreadContextInfo::GetThreadStackLimitAddr() const
  132. {
  133. return static_cast<intptr_t>(m_threadContextData.threadStackLimitAddr);
  134. }
  135. size_t
  136. ThreadContextInfo::GetScriptStackLimit() const
  137. {
  138. return static_cast<size_t>(m_threadContextData.scriptStackLimit);
  139. }
  140. bool
  141. ThreadContextInfo::IsThreadBound() const
  142. {
  143. return m_threadContextData.isThreadBound != FALSE;
  144. }
  145. PageAllocator *
  146. ThreadContextInfo::GetPageAllocator()
  147. {
  148. return &m_pageAlloc;
  149. }
  150. CodeGenAllocators *
  151. ThreadContextInfo::GetCodeGenAllocators()
  152. {
  153. return &m_codeGenAlloc;
  154. }
  155. AllocationPolicyManager *
  156. ThreadContextInfo::GetAllocationPolicyManager()
  157. {
  158. return &m_policyManager;
  159. }
  160. HANDLE
  161. ThreadContextInfo::GetProcessHandle() const
  162. {
  163. return reinterpret_cast<HANDLE>(m_threadContextData.processHandle);
  164. }
  165. bool
  166. ThreadContextInfo::IsAllJITCodeInPreReservedRegion() const
  167. {
  168. return m_isAllJITCodeInPreReservedRegion;
  169. }
  170. void
  171. ThreadContextInfo::ResetIsAllJITCodeInPreReservedRegion()
  172. {
  173. m_isAllJITCodeInPreReservedRegion = false;
  174. }
  175. intptr_t
  176. ThreadContextInfo::GetRuntimeChakraBaseAddress() const
  177. {
  178. return static_cast<intptr_t>(m_threadContextData.chakraBaseAddress);
  179. }
  180. intptr_t
  181. ThreadContextInfo::GetRuntimeCRTBaseAddress() const
  182. {
  183. return static_cast<intptr_t>(m_threadContextData.crtBaseAddress);
  184. }
  185. ptrdiff_t
  186. ThreadContextInfo::GetChakraBaseAddressDifference() const
  187. {
  188. return m_jitChakraBaseAddress - GetRuntimeChakraBaseAddress();
  189. }
  190. ptrdiff_t
  191. ThreadContextInfo::GetCRTBaseAddressDifference() const
  192. {
  193. return m_jitCRTBaseAddress - GetRuntimeCRTBaseAddress();
  194. }
  195. bool
  196. ThreadContextInfo::IsCFGEnabled()
  197. {
  198. #if defined(_CONTROL_FLOW_GUARD)
  199. PROCESS_MITIGATION_CONTROL_FLOW_GUARD_POLICY CfgPolicy;
  200. m_delayLoadWinCoreProcessThreads.EnsureFromSystemDirOnly();
  201. BOOL isGetMitigationPolicySucceeded = m_delayLoadWinCoreProcessThreads.GetMitigationPolicyForProcess(
  202. this->GetProcessHandle(),
  203. ProcessControlFlowGuardPolicy,
  204. &CfgPolicy,
  205. sizeof(CfgPolicy));
  206. Assert(isGetMitigationPolicySucceeded || !AutoSystemInfo::Data.IsCFGEnabled());
  207. return CfgPolicy.EnableControlFlowGuard && AutoSystemInfo::Data.IsCFGEnabled();
  208. #else
  209. return false;
  210. #endif
  211. }