ServerScriptContext.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. bool
  195. ServerScriptContext::IsSIMDEnabled() const
  196. {
  197. return m_contextData.isSIMDEnabled != 0;
  198. }
  199. intptr_t
  200. ServerScriptContext::GetBuiltinFunctionsBaseAddr() const
  201. {
  202. return m_contextData.builtinFunctionsBaseAddr;
  203. }
  204. intptr_t
  205. ServerScriptContext::GetAddr() const
  206. {
  207. return m_contextData.scriptContextAddr;
  208. }
  209. intptr_t
  210. ServerScriptContext::GetVTableAddress(VTableValue vtableType) const
  211. {
  212. Assert(vtableType < VTableValue::Count);
  213. return m_contextData.vtableAddresses[vtableType];
  214. }
  215. bool
  216. ServerScriptContext::IsRecyclerVerifyEnabled() const
  217. {
  218. return m_contextData.isRecyclerVerifyEnabled != FALSE;
  219. }
  220. uint
  221. ServerScriptContext::GetRecyclerVerifyPad() const
  222. {
  223. return m_contextData.recyclerVerifyPad;
  224. }
  225. bool
  226. ServerScriptContext::IsPRNGSeeded() const
  227. {
  228. return m_isPRNGSeeded;
  229. }
  230. intptr_t
  231. ServerScriptContext::GetDebuggingFlagsAddr() const
  232. {
  233. return static_cast<intptr_t>(m_contextData.debuggingFlagsAddr);
  234. }
  235. intptr_t
  236. ServerScriptContext::GetDebugStepTypeAddr() const
  237. {
  238. return static_cast<intptr_t>(m_contextData.debugStepTypeAddr);
  239. }
  240. intptr_t
  241. ServerScriptContext::GetDebugFrameAddressAddr() const
  242. {
  243. return static_cast<intptr_t>(m_contextData.debugFrameAddressAddr);
  244. }
  245. intptr_t
  246. ServerScriptContext::GetDebugScriptIdWhenSetAddr() const
  247. {
  248. return static_cast<intptr_t>(m_contextData.debugScriptIdWhenSetAddr);
  249. }
  250. bool
  251. ServerScriptContext::IsClosed() const
  252. {
  253. return m_isClosed;
  254. }
  255. void
  256. ServerScriptContext::AddToDOMFastPathHelperMap(intptr_t funcInfoAddr, IR::JnHelperMethod helper)
  257. {
  258. m_domFastPathHelperMap->Add(funcInfoAddr, helper);
  259. }
  260. ArenaAllocator *
  261. ServerScriptContext::GetSourceCodeArena()
  262. {
  263. return &m_sourceCodeArena;
  264. }
  265. void
  266. ServerScriptContext::DecommitEmitBufferManager(bool asmJsManager)
  267. {
  268. GetEmitBufferManager(asmJsManager)->Decommit();
  269. }
  270. OOPEmitBufferManager *
  271. ServerScriptContext::GetEmitBufferManager(bool asmJsManager)
  272. {
  273. if (asmJsManager)
  274. {
  275. return &m_asmJsInterpreterThunkBufferManager;
  276. }
  277. else
  278. {
  279. return &m_interpreterThunkBufferManager;
  280. }
  281. }
  282. IR::JnHelperMethod
  283. ServerScriptContext::GetDOMFastPathHelper(intptr_t funcInfoAddr)
  284. {
  285. IR::JnHelperMethod helper;
  286. m_domFastPathHelperMap->LockResize();
  287. bool found = m_domFastPathHelperMap->TryGetValue(funcInfoAddr, &helper);
  288. m_domFastPathHelperMap->UnlockResize();
  289. Assert(found);
  290. return helper;
  291. }
  292. void
  293. ServerScriptContext::Close()
  294. {
  295. Assert(!IsClosed());
  296. m_isClosed = true;
  297. #ifdef STACK_BACK_TRACE
  298. ServerContextManager::RecordCloseContext(this);
  299. #endif
  300. }
  301. void
  302. ServerScriptContext::AddRef()
  303. {
  304. InterlockedExchangeAdd(&m_refCount, 1u);
  305. }
  306. void
  307. ServerScriptContext::Release()
  308. {
  309. InterlockedExchangeSubtract(&m_refCount, 1u);
  310. if (m_isClosed && m_refCount == 0)
  311. {
  312. // Not freeing here, we'll expect explicit ServerCleanupScriptContext() call to do the free
  313. // otherwise after free, the CodeGen call can still get same scriptContext if there's another
  314. // ServerInitializeScriptContext call
  315. }
  316. }
  317. Field(Js::Var)*
  318. ServerScriptContext::GetModuleExportSlotArrayAddress(uint moduleIndex, uint slotIndex)
  319. {
  320. Assert(m_moduleRecords.ContainsKey(moduleIndex));
  321. auto record = m_moduleRecords.Item(moduleIndex);
  322. return record->localExportSlotsAddr;
  323. }
  324. void
  325. ServerScriptContext::SetIsPRNGSeeded(bool value)
  326. {
  327. m_isPRNGSeeded = value;
  328. }
  329. void
  330. ServerScriptContext::AddModuleRecordInfo(unsigned int moduleId, __int64 localExportSlotsAddr)
  331. {
  332. Js::ServerSourceTextModuleRecord* record = HeapNewStructZ(Js::ServerSourceTextModuleRecord);
  333. record->moduleId = moduleId;
  334. record->localExportSlotsAddr = (Field(Js::Var)*)localExportSlotsAddr;
  335. m_moduleRecords.Add(moduleId, record);
  336. }
  337. Js::ScriptContextProfiler *
  338. ServerScriptContext::GetCodeGenProfiler() const
  339. {
  340. #ifdef PROFILE_EXEC
  341. return m_codeGenProfiler;
  342. #else
  343. return nullptr;
  344. #endif
  345. }
  346. #endif