ServerScriptContext.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. ServerScriptContext::ThreadContextHolder::ThreadContextHolder(ServerThreadContext* threadContextInfo) : threadContextInfo(threadContextInfo)
  9. {
  10. threadContextInfo->AddRef();
  11. }
  12. ServerScriptContext::ThreadContextHolder::~ThreadContextHolder()
  13. {
  14. threadContextInfo->Release();
  15. }
  16. ServerScriptContext::ServerScriptContext(ScriptContextDataIDL * contextData, ServerThreadContext* threadContextInfo) :
  17. m_contextData(*contextData),
  18. threadContextHolder(threadContextInfo),
  19. m_isPRNGSeeded(false),
  20. m_sourceCodeArena(_u("JITSourceCodeArena"), threadContextInfo->GetForegroundPageAllocator(), Js::Throw::OutOfMemory, nullptr),
  21. m_interpreterThunkBufferManager(&m_sourceCodeArena, threadContextInfo->GetThunkPageAllocators(), nullptr, _u("Interpreter thunk buffer"), GetThreadContext()->GetProcessHandle()),
  22. m_asmJsInterpreterThunkBufferManager(&m_sourceCodeArena, threadContextInfo->GetThunkPageAllocators(), nullptr, _u("Asm.js interpreter thunk buffer"), GetThreadContext()->GetProcessHandle()),
  23. m_domFastPathHelperMap(nullptr),
  24. m_moduleRecords(&HeapAllocator::Instance),
  25. m_globalThisAddr(0),
  26. #ifdef PROFILE_EXEC
  27. m_codeGenProfiler(nullptr),
  28. #endif
  29. m_refCount(0),
  30. m_isClosed(false)
  31. {
  32. #ifdef PROFILE_EXEC
  33. if (Js::Configuration::Global.flags.IsEnabled(Js::ProfileFlag))
  34. {
  35. m_codeGenProfiler = HeapNew(Js::ScriptContextProfiler);
  36. }
  37. #endif
  38. m_domFastPathHelperMap = HeapNew(JITDOMFastPathHelperMap, &HeapAllocator::Instance, 17);
  39. }
  40. ServerScriptContext::~ServerScriptContext()
  41. {
  42. HeapDelete(m_domFastPathHelperMap);
  43. m_moduleRecords.Map([](uint, Js::ServerSourceTextModuleRecord* record)
  44. {
  45. HeapDelete(record);
  46. });
  47. #ifdef PROFILE_EXEC
  48. if (m_codeGenProfiler)
  49. {
  50. HeapDelete(m_codeGenProfiler);
  51. }
  52. #endif
  53. }
  54. intptr_t
  55. ServerScriptContext::GetNullAddr() const
  56. {
  57. return m_contextData.nullAddr;
  58. }
  59. intptr_t
  60. ServerScriptContext::GetUndefinedAddr() const
  61. {
  62. return m_contextData.undefinedAddr;
  63. }
  64. intptr_t
  65. ServerScriptContext::GetTrueAddr() const
  66. {
  67. return m_contextData.trueAddr;
  68. }
  69. intptr_t
  70. ServerScriptContext::GetFalseAddr() const
  71. {
  72. return m_contextData.falseAddr;
  73. }
  74. intptr_t
  75. ServerScriptContext::GetUndeclBlockVarAddr() const
  76. {
  77. return m_contextData.undeclBlockVarAddr;
  78. }
  79. intptr_t
  80. ServerScriptContext::GetEmptyStringAddr() const
  81. {
  82. return m_contextData.emptyStringAddr;
  83. }
  84. intptr_t
  85. ServerScriptContext::GetNegativeZeroAddr() const
  86. {
  87. return m_contextData.negativeZeroAddr;
  88. }
  89. intptr_t
  90. ServerScriptContext::GetNumberTypeStaticAddr() const
  91. {
  92. return m_contextData.numberTypeStaticAddr;
  93. }
  94. intptr_t
  95. ServerScriptContext::GetStringTypeStaticAddr() const
  96. {
  97. return m_contextData.stringTypeStaticAddr;
  98. }
  99. intptr_t
  100. ServerScriptContext::GetObjectTypeAddr() const
  101. {
  102. return m_contextData.objectTypeAddr;
  103. }
  104. intptr_t
  105. ServerScriptContext::GetObjectHeaderInlinedTypeAddr() const
  106. {
  107. return m_contextData.objectHeaderInlinedTypeAddr;
  108. }
  109. intptr_t
  110. ServerScriptContext::GetRegexTypeAddr() const
  111. {
  112. return m_contextData.regexTypeAddr;
  113. }
  114. intptr_t
  115. ServerScriptContext::GetArrayTypeAddr() const
  116. {
  117. return m_contextData.arrayTypeAddr;
  118. }
  119. intptr_t
  120. ServerScriptContext::GetNativeIntArrayTypeAddr() const
  121. {
  122. return m_contextData.nativeIntArrayTypeAddr;
  123. }
  124. intptr_t
  125. ServerScriptContext::GetNativeFloatArrayTypeAddr() const
  126. {
  127. return m_contextData.nativeFloatArrayTypeAddr;
  128. }
  129. intptr_t
  130. ServerScriptContext::GetArrayConstructorAddr() const
  131. {
  132. return m_contextData.arrayConstructorAddr;
  133. }
  134. intptr_t
  135. ServerScriptContext::GetCharStringCacheAddr() const
  136. {
  137. return m_contextData.charStringCacheAddr;
  138. }
  139. intptr_t
  140. ServerScriptContext::GetSideEffectsAddr() const
  141. {
  142. return m_contextData.sideEffectsAddr;
  143. }
  144. intptr_t
  145. ServerScriptContext::GetArraySetElementFastPathVtableAddr() const
  146. {
  147. return m_contextData.arraySetElementFastPathVtableAddr;
  148. }
  149. intptr_t
  150. ServerScriptContext::GetIntArraySetElementFastPathVtableAddr() const
  151. {
  152. return m_contextData.intArraySetElementFastPathVtableAddr;
  153. }
  154. intptr_t
  155. ServerScriptContext::GetFloatArraySetElementFastPathVtableAddr() const
  156. {
  157. return m_contextData.floatArraySetElementFastPathVtableAddr;
  158. }
  159. intptr_t
  160. ServerScriptContext::GetLibraryAddr() const
  161. {
  162. return m_contextData.libraryAddr;
  163. }
  164. intptr_t
  165. ServerScriptContext::GetGlobalObjectAddr() const
  166. {
  167. return m_contextData.globalObjectAddr;
  168. }
  169. intptr_t
  170. ServerScriptContext::GetGlobalObjectThisAddr() const
  171. {
  172. return m_globalThisAddr;
  173. }
  174. void
  175. ServerScriptContext::UpdateGlobalObjectThisAddr(intptr_t globalThis)
  176. {
  177. m_globalThisAddr = globalThis;
  178. }
  179. intptr_t
  180. ServerScriptContext::GetNumberAllocatorAddr() const
  181. {
  182. return m_contextData.numberAllocatorAddr;
  183. }
  184. intptr_t
  185. ServerScriptContext::GetRecyclerAddr() const
  186. {
  187. return m_contextData.recyclerAddr;
  188. }
  189. bool
  190. ServerScriptContext::GetRecyclerAllowNativeCodeBumpAllocation() const
  191. {
  192. return m_contextData.recyclerAllowNativeCodeBumpAllocation != 0;
  193. }
  194. #ifdef ENABLE_SIMDJS
  195. bool
  196. ServerScriptContext::IsSIMDEnabled() const
  197. {
  198. return m_contextData.isSIMDEnabled != 0;
  199. }
  200. #endif
  201. intptr_t
  202. ServerScriptContext::GetBuiltinFunctionsBaseAddr() const
  203. {
  204. return m_contextData.builtinFunctionsBaseAddr;
  205. }
  206. intptr_t
  207. ServerScriptContext::GetAddr() const
  208. {
  209. return m_contextData.scriptContextAddr;
  210. }
  211. intptr_t
  212. ServerScriptContext::GetVTableAddress(VTableValue vtableType) const
  213. {
  214. Assert(vtableType < VTableValue::Count);
  215. return m_contextData.vtableAddresses[vtableType];
  216. }
  217. bool
  218. ServerScriptContext::IsRecyclerVerifyEnabled() const
  219. {
  220. return m_contextData.isRecyclerVerifyEnabled != FALSE;
  221. }
  222. uint
  223. ServerScriptContext::GetRecyclerVerifyPad() const
  224. {
  225. return m_contextData.recyclerVerifyPad;
  226. }
  227. bool
  228. ServerScriptContext::IsPRNGSeeded() const
  229. {
  230. return m_isPRNGSeeded;
  231. }
  232. intptr_t
  233. ServerScriptContext::GetDebuggingFlagsAddr() const
  234. {
  235. return static_cast<intptr_t>(m_contextData.debuggingFlagsAddr);
  236. }
  237. intptr_t
  238. ServerScriptContext::GetDebugStepTypeAddr() const
  239. {
  240. return static_cast<intptr_t>(m_contextData.debugStepTypeAddr);
  241. }
  242. intptr_t
  243. ServerScriptContext::GetDebugFrameAddressAddr() const
  244. {
  245. return static_cast<intptr_t>(m_contextData.debugFrameAddressAddr);
  246. }
  247. intptr_t
  248. ServerScriptContext::GetDebugScriptIdWhenSetAddr() const
  249. {
  250. return static_cast<intptr_t>(m_contextData.debugScriptIdWhenSetAddr);
  251. }
  252. bool
  253. ServerScriptContext::IsClosed() const
  254. {
  255. return m_isClosed;
  256. }
  257. void
  258. ServerScriptContext::AddToDOMFastPathHelperMap(intptr_t funcInfoAddr, IR::JnHelperMethod helper)
  259. {
  260. m_domFastPathHelperMap->Add(funcInfoAddr, helper);
  261. }
  262. ArenaAllocator *
  263. ServerScriptContext::GetSourceCodeArena()
  264. {
  265. return &m_sourceCodeArena;
  266. }
  267. void
  268. ServerScriptContext::DecommitEmitBufferManager(bool asmJsManager)
  269. {
  270. GetEmitBufferManager(asmJsManager)->Decommit();
  271. }
  272. OOPEmitBufferManager *
  273. ServerScriptContext::GetEmitBufferManager(bool asmJsManager)
  274. {
  275. if (asmJsManager)
  276. {
  277. return &m_asmJsInterpreterThunkBufferManager;
  278. }
  279. else
  280. {
  281. return &m_interpreterThunkBufferManager;
  282. }
  283. }
  284. IR::JnHelperMethod
  285. ServerScriptContext::GetDOMFastPathHelper(intptr_t funcInfoAddr)
  286. {
  287. IR::JnHelperMethod helper = IR::HelperInvalid;
  288. m_domFastPathHelperMap->LockResize();
  289. m_domFastPathHelperMap->TryGetValue(funcInfoAddr, &helper);
  290. m_domFastPathHelperMap->UnlockResize();
  291. return helper;
  292. }
  293. void
  294. ServerScriptContext::Close()
  295. {
  296. Assert(!IsClosed());
  297. m_isClosed = true;
  298. #ifdef STACK_BACK_TRACE
  299. ServerContextManager::RecordCloseContext(this);
  300. #endif
  301. }
  302. void
  303. ServerScriptContext::AddRef()
  304. {
  305. InterlockedExchangeAdd(&m_refCount, 1u);
  306. }
  307. void
  308. ServerScriptContext::Release()
  309. {
  310. InterlockedExchangeSubtract(&m_refCount, 1u);
  311. if (m_isClosed && m_refCount == 0)
  312. {
  313. // Not freeing here, we'll expect explicit ServerCleanupScriptContext() call to do the free
  314. // otherwise after free, the CodeGen call can still get same scriptContext if there's another
  315. // ServerInitializeScriptContext call
  316. }
  317. }
  318. Field(Js::Var)*
  319. ServerScriptContext::GetModuleExportSlotArrayAddress(uint moduleIndex, uint slotIndex)
  320. {
  321. Assert(m_moduleRecords.ContainsKey(moduleIndex));
  322. auto record = m_moduleRecords.Item(moduleIndex);
  323. return record->localExportSlotsAddr;
  324. }
  325. void
  326. ServerScriptContext::SetIsPRNGSeeded(bool value)
  327. {
  328. m_isPRNGSeeded = value;
  329. }
  330. void
  331. ServerScriptContext::AddModuleRecordInfo(unsigned int moduleId, __int64 localExportSlotsAddr)
  332. {
  333. Js::ServerSourceTextModuleRecord* record = HeapNewStructZ(Js::ServerSourceTextModuleRecord);
  334. record->moduleId = moduleId;
  335. record->localExportSlotsAddr = (Field(Js::Var)*)localExportSlotsAddr;
  336. m_moduleRecords.Add(moduleId, record);
  337. }
  338. Js::ScriptContextProfiler *
  339. ServerScriptContext::GetCodeGenProfiler() const
  340. {
  341. #ifdef PROFILE_EXEC
  342. return m_codeGenProfiler;
  343. #else
  344. return nullptr;
  345. #endif
  346. }
  347. #endif