2
0

ServerScriptContext.cpp 8.9 KB

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