ServerThreadContext.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. ServerThreadContext::ServerThreadContext(ThreadContextDataIDL * data) :
  7. m_threadContextData(*data),
  8. m_refCount(0),
  9. m_policyManager(true),
  10. m_propertyMap(nullptr),
  11. m_pageAllocs(&HeapAllocator::Instance),
  12. m_preReservedVirtualAllocator((HANDLE)data->processHandle),
  13. m_codePageAllocators(&m_policyManager, ALLOC_XDATA, &m_preReservedVirtualAllocator, (HANDLE)data->processHandle),
  14. m_codeGenAlloc(&m_policyManager, nullptr, &m_codePageAllocators, (HANDLE)data->processHandle),
  15. // TODO: OOP JIT, don't hardcode name
  16. #ifdef NTBUILD
  17. m_jitChakraBaseAddress((intptr_t)GetModuleHandle(_u("Chakra.dll"))),
  18. #else
  19. m_jitChakraBaseAddress((intptr_t)GetModuleHandle(_u("ChakraCore.dll"))),
  20. #endif
  21. m_jitCRTBaseAddress((intptr_t)GetModuleHandle(UCrtC99MathApis::LibraryName))
  22. {
  23. m_pid = GetProcessId((HANDLE)data->processHandle);
  24. #if !_M_X64_OR_ARM64 && _CONTROL_FLOW_GUARD
  25. m_codeGenAlloc.canCreatePreReservedSegment = data->allowPrereserveAlloc != FALSE;
  26. #endif
  27. m_propertyMap = HeapNew(PropertyMap, &HeapAllocator::Instance, TotalNumberOfBuiltInProperties + 700);
  28. }
  29. ServerThreadContext::~ServerThreadContext()
  30. {
  31. // TODO: OOP JIT, clear out elements of map. maybe should arena alloc?
  32. if (this->m_propertyMap != nullptr)
  33. {
  34. this->m_propertyMap->Map([](const Js::PropertyRecord* record)
  35. {
  36. size_t allocLength = record->byteCount + sizeof(char16) + (record->isNumeric ? sizeof(uint32) : 0);
  37. HeapDeletePlus(allocLength, const_cast<Js::PropertyRecord*>(record));
  38. });
  39. HeapDelete(m_propertyMap);
  40. this->m_propertyMap = nullptr;
  41. }
  42. this->m_pageAllocs.Map([](DWORD thread, PageAllocator* alloc)
  43. {
  44. HeapDelete(alloc);
  45. });
  46. }
  47. PreReservedVirtualAllocWrapper *
  48. ServerThreadContext::GetPreReservedVirtualAllocator()
  49. {
  50. return &m_preReservedVirtualAllocator;
  51. }
  52. PageAllocator*
  53. ServerThreadContext::GetPageAllocator()
  54. {
  55. PageAllocator * alloc;
  56. if (!m_pageAllocs.TryGetValue(GetCurrentThreadId(), &alloc))
  57. {
  58. alloc = HeapNew(PageAllocator,
  59. &m_policyManager,
  60. Js::Configuration::Global.flags, PageAllocatorType_BGJIT,
  61. AutoSystemInfo::Data.IsLowMemoryProcess() ?
  62. PageAllocator::DefaultLowMaxFreePageCount :
  63. PageAllocator::DefaultMaxFreePageCount);
  64. m_pageAllocs.Add(GetCurrentThreadId(), alloc);
  65. }
  66. return alloc;
  67. }
  68. intptr_t
  69. ServerThreadContext::GetBailOutRegisterSaveSpaceAddr() const
  70. {
  71. return static_cast<intptr_t>(m_threadContextData.bailOutRegisterSaveSpaceAddr);
  72. }
  73. intptr_t
  74. ServerThreadContext::GetDebuggingFlagsAddr() const
  75. {
  76. return static_cast<intptr_t>(m_threadContextData.debuggingFlagsAddr);
  77. }
  78. intptr_t
  79. ServerThreadContext::GetDebugStepTypeAddr() const
  80. {
  81. return static_cast<intptr_t>(m_threadContextData.debugStepTypeAddr);
  82. }
  83. intptr_t
  84. ServerThreadContext::GetDebugFrameAddressAddr() const
  85. {
  86. return static_cast<intptr_t>(m_threadContextData.debugFrameAddressAddr);
  87. }
  88. intptr_t
  89. ServerThreadContext::GetDebugScriptIdWhenSetAddr() const
  90. {
  91. return static_cast<intptr_t>(m_threadContextData.debugScriptIdWhenSetAddr);
  92. }
  93. ptrdiff_t
  94. ServerThreadContext::GetChakraBaseAddressDifference() const
  95. {
  96. return GetRuntimeChakraBaseAddress() - m_jitChakraBaseAddress;
  97. }
  98. ptrdiff_t
  99. ServerThreadContext::GetCRTBaseAddressDifference() const
  100. {
  101. return GetRuntimeCRTBaseAddress() - m_jitCRTBaseAddress;
  102. }
  103. intptr_t
  104. ServerThreadContext::GetDisableImplicitFlagsAddr() const
  105. {
  106. return static_cast<intptr_t>(m_threadContextData.disableImplicitFlagsAddr);
  107. }
  108. intptr_t
  109. ServerThreadContext::GetImplicitCallFlagsAddr() const
  110. {
  111. return static_cast<intptr_t>(m_threadContextData.implicitCallFlagsAddr);
  112. }
  113. #if defined(ENABLE_SIMDJS) && (defined(_M_IX86) || defined(_M_X64))
  114. intptr_t
  115. ServerThreadContext::GetSimdTempAreaAddr(uint8 tempIndex) const
  116. {
  117. Assert(tempIndex < SIMD_TEMP_SIZE);
  118. return m_threadContextData.simdTempAreaBaseAddr + tempIndex * sizeof(_x86_SIMDValue);
  119. }
  120. #endif
  121. intptr_t
  122. ServerThreadContext::GetThreadStackLimitAddr() const
  123. {
  124. return static_cast<intptr_t>(m_threadContextData.threadStackLimitAddr);
  125. }
  126. size_t
  127. ServerThreadContext::GetScriptStackLimit() const
  128. {
  129. return static_cast<size_t>(m_threadContextData.scriptStackLimit);
  130. }
  131. bool
  132. ServerThreadContext::IsThreadBound() const
  133. {
  134. return m_threadContextData.isThreadBound != FALSE;
  135. }
  136. HANDLE
  137. ServerThreadContext::GetProcessHandle() const
  138. {
  139. return reinterpret_cast<HANDLE>(m_threadContextData.processHandle);
  140. }
  141. CustomHeap::CodePageAllocators *
  142. ServerThreadContext::GetCodePageAllocators()
  143. {
  144. return &m_codePageAllocators;
  145. }
  146. CodeGenAllocators *
  147. ServerThreadContext::GetCodeGenAllocators()
  148. {
  149. return &m_codeGenAlloc;
  150. }
  151. AllocationPolicyManager *
  152. ServerThreadContext::GetAllocationPolicyManager()
  153. {
  154. return &m_policyManager;
  155. }
  156. intptr_t
  157. ServerThreadContext::GetRuntimeChakraBaseAddress() const
  158. {
  159. return static_cast<intptr_t>(m_threadContextData.chakraBaseAddress);
  160. }
  161. intptr_t
  162. ServerThreadContext::GetRuntimeCRTBaseAddress() const
  163. {
  164. return static_cast<intptr_t>(m_threadContextData.crtBaseAddress);
  165. }
  166. Js::PropertyRecord const *
  167. ServerThreadContext::GetPropertyRecord(Js::PropertyId propertyId)
  168. {
  169. if (propertyId >= 0 && Js::IsInternalPropertyId(propertyId))
  170. {
  171. return Js::InternalPropertyRecords::GetInternalPropertyName(propertyId);
  172. }
  173. const Js::PropertyRecord * propertyRecord = nullptr;
  174. m_propertyMap->LockResize();
  175. bool found = m_propertyMap->TryGetValue(propertyId, &propertyRecord);
  176. m_propertyMap->UnlockResize();
  177. AssertMsg(found && propertyRecord != nullptr, "using invalid propertyid");
  178. return propertyRecord;
  179. }
  180. void
  181. ServerThreadContext::RemoveFromPropertyMap(Js::PropertyId reclaimedId)
  182. {
  183. const Js::PropertyRecord * oldRecord = nullptr;
  184. if (m_propertyMap->TryGetValue(reclaimedId, &oldRecord))
  185. {
  186. // if there was reclaimed property that had its pid reused, delete the old property record
  187. m_propertyMap->Remove(oldRecord);
  188. PropertyRecordTrace(_u("Reclaimed JIT property '%s' at 0x%08x, pid = %d\n"), oldRecord->GetBuffer(), oldRecord, oldRecord->pid);
  189. size_t oldLength = oldRecord->byteCount + sizeof(char16) + (oldRecord->isNumeric ? sizeof(uint32) : 0);
  190. HeapDeletePlus(oldLength, const_cast<Js::PropertyRecord*>(oldRecord));
  191. }
  192. else
  193. {
  194. // we should only ever ask to reclaim properties which were previously added to the jit map
  195. Assert(UNREACHED);
  196. }
  197. }
  198. void
  199. ServerThreadContext::AddToPropertyMap(const Js::PropertyRecord * origRecord)
  200. {
  201. size_t allocLength = origRecord->byteCount + sizeof(char16) + (origRecord->isNumeric ? sizeof(uint32) : 0);
  202. Js::PropertyRecord * record = HeapNewPlus(allocLength, Js::PropertyRecord, origRecord->byteCount, origRecord->isNumeric, origRecord->hash, origRecord->isSymbol);
  203. record->isBound = origRecord->isBound;
  204. char16* buffer = (char16 *)(record + 1);
  205. js_memcpy_s(buffer, origRecord->byteCount, origRecord->GetBuffer(), origRecord->byteCount);
  206. buffer[record->GetLength()] = _u('\0');
  207. if (record->isNumeric)
  208. {
  209. *(uint32 *)(buffer + record->GetLength() + 1) = origRecord->GetNumericValue();
  210. Assert(record->GetNumericValue() == origRecord->GetNumericValue());
  211. }
  212. record->pid = origRecord->pid;
  213. const Js::PropertyRecord * oldRecord = nullptr;
  214. // this should only happen if there was reclaimed property that we failed to add to reclaimed list due to a prior oom
  215. if (m_propertyMap->TryGetValue(origRecord->GetPropertyId(), &oldRecord))
  216. {
  217. m_propertyMap->Remove(oldRecord);
  218. size_t oldLength = oldRecord->byteCount + sizeof(char16) + (oldRecord->isNumeric ? sizeof(uint32) : 0);
  219. HeapDeletePlus(oldLength, const_cast<Js::PropertyRecord*>(oldRecord));
  220. }
  221. m_propertyMap->Add(record);
  222. PropertyRecordTrace(_u("Added JIT property '%s' at 0x%08x, pid = %d\n"), record->GetBuffer(), record, record->pid);
  223. }
  224. void ServerThreadContext::AddRef()
  225. {
  226. InterlockedExchangeAdd(&m_refCount, (uint)1);
  227. }
  228. void ServerThreadContext::Release()
  229. {
  230. InterlockedExchangeSubtract(&m_refCount, (uint)1);
  231. if (m_isClosed && m_refCount == 0)
  232. {
  233. HeapDelete(this);
  234. }
  235. }
  236. void ServerThreadContext::Close()
  237. {
  238. this->m_isClosed = true;
  239. }