JnHelperMethod.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft Corporation and contributors. 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. #include "ExternalHelperMethod.h"
  7. // Parser includes
  8. #include "RegexCommon.h"
  9. #include "Library/RegexHelper.h"
  10. #ifdef ENABLE_SCRIPT_DEBUGGING
  11. #include "Debug/DiagHelperMethodWrapper.h"
  12. #endif
  13. #include "Math/CrtSSE2Math.h"
  14. #include "Library/JavascriptGeneratorFunction.h"
  15. #include "RuntimeMathPch.h"
  16. namespace IR
  17. {
  18. intptr_t const JnHelperMethodAddresses[] =
  19. {
  20. #define HELPERCALL(Name, Address, Attributes) reinterpret_cast<intptr_t>(Address),
  21. // Because of order-of-initialization problems with the vtable address static field
  22. // and this array, we're going to have to fill these in as we go along.
  23. #include "JnHelperMethodList.h"
  24. NULL
  25. };
  26. intptr_t const *GetHelperMethods()
  27. {
  28. return JnHelperMethodAddresses;
  29. }
  30. #if ENABLE_DEBUG_CONFIG_OPTIONS && defined(_CONTROL_FLOW_GUARD)
  31. class HelperTableCheck
  32. {
  33. public:
  34. HelperTableCheck() {
  35. CheckJnHelperTable(JnHelperMethodAddresses);
  36. }
  37. };
  38. // Dummy global to trigger CheckJnHelperTable call at load time.
  39. static HelperTableCheck LoadTimeHelperTableCheck;
  40. void CheckJnHelperTable(intptr_t const* table)
  41. {
  42. MEMORY_BASIC_INFORMATION memBuffer;
  43. // Make sure the helper table is in read-only memory for security reasons.
  44. SIZE_T byteCount;
  45. byteCount = VirtualQuery(table, &memBuffer, sizeof(memBuffer));
  46. Assert(byteCount);
  47. // Note: .rdata is merged with .text on x86.
  48. if (memBuffer.Protect != PAGE_READONLY && memBuffer.Protect != PAGE_EXECUTE_READ)
  49. {
  50. AssertMsg(false, "JnHelperMethodAddress table needs to be read-only for security reasons");
  51. Fatal();
  52. }
  53. }
  54. #endif
  55. #ifdef ENABLE_SCRIPT_DEBUGGING
  56. static intptr_t const helperMethodWrappers[] = {
  57. reinterpret_cast<intptr_t>(&Js::HelperMethodWrapper0),
  58. reinterpret_cast<intptr_t>(&Js::HelperMethodWrapper1),
  59. reinterpret_cast<intptr_t>(&Js::HelperMethodWrapper2),
  60. reinterpret_cast<intptr_t>(&Js::HelperMethodWrapper3),
  61. reinterpret_cast<intptr_t>(&Js::HelperMethodWrapper4),
  62. reinterpret_cast<intptr_t>(&Js::HelperMethodWrapper5),
  63. reinterpret_cast<intptr_t>(&Js::HelperMethodWrapper6),
  64. reinterpret_cast<intptr_t>(&Js::HelperMethodWrapper7),
  65. reinterpret_cast<intptr_t>(&Js::HelperMethodWrapper8),
  66. reinterpret_cast<intptr_t>(&Js::HelperMethodWrapper9),
  67. reinterpret_cast<intptr_t>(&Js::HelperMethodWrapper10),
  68. reinterpret_cast<intptr_t>(&Js::HelperMethodWrapper11),
  69. reinterpret_cast<intptr_t>(&Js::HelperMethodWrapper12),
  70. reinterpret_cast<intptr_t>(&Js::HelperMethodWrapper13),
  71. reinterpret_cast<intptr_t>(&Js::HelperMethodWrapper14),
  72. reinterpret_cast<intptr_t>(&Js::HelperMethodWrapper15),
  73. reinterpret_cast<intptr_t>(&Js::HelperMethodWrapper16),
  74. };
  75. #endif
  76. ///----------------------------------------------------------------------------
  77. ///
  78. /// GetMethodAddress
  79. ///
  80. /// returns the memory address of the helperMethod,
  81. /// which can the address of debugger wrapper that intercept the original helper.
  82. ///
  83. ///----------------------------------------------------------------------------
  84. intptr_t
  85. GetMethodAddress(ThreadContextInfo * context, IR::HelperCallOpnd* opnd)
  86. {
  87. Assert(opnd);
  88. #ifdef ENABLE_SCRIPT_DEBUGGING
  89. #if defined(_M_ARM32_OR_ARM64)
  90. #define LowererMDFinal LowererMD
  91. #else
  92. #define LowererMDFinal LowererMDArch
  93. #endif
  94. CompileAssert(_countof(helperMethodWrappers) == LowererMDFinal::MaxArgumentsToHelper + 1);
  95. if (opnd->IsDiagHelperCallOpnd())
  96. {
  97. // Note: all arguments are already loaded for the original helper. Here we just return the address.
  98. IR::DiagHelperCallOpnd* diagOpnd = (IR::DiagHelperCallOpnd*)opnd;
  99. if (0 <= diagOpnd->m_argCount && diagOpnd->m_argCount <= LowererMDFinal::MaxArgumentsToHelper)
  100. {
  101. return ShiftAddr(context, helperMethodWrappers[diagOpnd->m_argCount]);
  102. }
  103. else
  104. {
  105. AssertMsg(FALSE, "Unsupported arg count (need to implement).");
  106. }
  107. }
  108. #endif
  109. return GetMethodOriginalAddress(context, opnd->m_fnHelper);
  110. }
  111. // TODO: Remove this define once makes it into WINNT.h
  112. #ifndef DECLSPEC_GUARDIGNORE
  113. #if (_MSC_FULL_VER >= 170065501) && !defined(__clang__)
  114. #define DECLSPEC_GUARDIGNORE __declspec(guard(ignore))
  115. #else
  116. #define DECLSPEC_GUARDIGNORE
  117. #endif
  118. #endif
  119. // We need the helper table to be in read-only memory for obvious security reasons.
  120. // Import function ptr require dynamic initialization, and cause the table to be in read-write memory.
  121. // Additionally, all function ptrs are automatically marked as safe CFG addresses by the compiler.
  122. // __declspec(guard(ignore)) can be used on methods to have the compiler not mark these as valid CFG targets.
  123. DECLSPEC_GUARDIGNORE _NOINLINE intptr_t GetNonTableMethodAddress(ThreadContextInfo * context, JnHelperMethod helperMethod)
  124. {
  125. switch (helperMethod)
  126. {
  127. //
  128. // DllImport methods
  129. //
  130. #if defined(_M_IX86)
  131. // These are internal CRT functions which don't use a standard calling convention
  132. case HelperDirectMath_Acos:
  133. return ShiftAddr(context, __libm_sse2_acos);
  134. case HelperDirectMath_Asin:
  135. return ShiftAddr(context, __libm_sse2_asin);
  136. case HelperDirectMath_Atan:
  137. return ShiftAddr(context, __libm_sse2_atan);
  138. case HelperDirectMath_Atan2:
  139. return ShiftAddr(context, __libm_sse2_atan2);
  140. case HelperDirectMath_Cos:
  141. return ShiftAddr(context, __libm_sse2_cos);
  142. case HelperDirectMath_Exp:
  143. return ShiftAddr(context, __libm_sse2_exp);
  144. case HelperDirectMath_Log:
  145. return ShiftAddr(context, __libm_sse2_log);
  146. case HelperDirectMath_Sin:
  147. return ShiftAddr(context, __libm_sse2_sin);
  148. case HelperDirectMath_Tan:
  149. return ShiftAddr(context, __libm_sse2_tan);
  150. case HelperAtomicStore64:
  151. return ShiftAddr(context, (double(*)(double))InterlockedExchange64);
  152. case HelperMemoryBarrier:
  153. #ifdef _M_HYBRID_X86_ARM64
  154. AssertOrFailFastMsg(false, "The usage below fails to build for CHPE, and HelperMemoryBarrier is only required "
  155. "for WASM threads, which are currently disabled");
  156. return 0;
  157. #else
  158. return ShiftAddr(context, (void(*)())MemoryBarrier);
  159. #endif // !_M_HYBRID_X86_ARM64
  160. #endif
  161. case HelperDirectMath_FloorDb:
  162. return ShiftStdcallAddr(context, Js::JavascriptMath::Floor);
  163. case HelperDirectMath_CeilDb:
  164. return ShiftStdcallAddr(context, Js::JavascriptMath::Ceil);
  165. case HelperDirectMath_FloorFlt:
  166. return ShiftStdcallAddr(context, Js::JavascriptMath::FloorF);
  167. case HelperDirectMath_CeilFlt:
  168. return ShiftStdcallAddr(context, Js::JavascriptMath::CeilF);
  169. //
  170. // These are statically initialized to an import thunk, but let's keep them out of the table in case a new CRT changes this
  171. //
  172. case HelperWMemCmp:
  173. return ShiftCdeclAddr(context, wmemcmp);
  174. case HelperMemCpy:
  175. return ShiftCdeclAddr(context, (void *(__cdecl *)(void *, void const*, size_t))memcpy);
  176. #if defined(_M_X64) || defined(_M_ARM32_OR_ARM64)
  177. case HelperDirectMath_Acos:
  178. return ShiftCdeclAddr(context, (double(__cdecl *)(double))acos);
  179. case HelperDirectMath_Asin:
  180. return ShiftCdeclAddr(context, (double(__cdecl *)(double))asin);
  181. case HelperDirectMath_Atan:
  182. return ShiftCdeclAddr(context, (double(__cdecl *)(double))atan);
  183. case HelperDirectMath_Atan2:
  184. return ShiftCdeclAddr(context, (double(__cdecl *)(double, double))atan2);
  185. case HelperDirectMath_Cos:
  186. return ShiftCdeclAddr(context, (double(__cdecl *)(double))cos);
  187. case HelperDirectMath_Exp:
  188. return ShiftCdeclAddr(context, (double(__cdecl *)(double))exp);
  189. case HelperDirectMath_Log:
  190. return ShiftCdeclAddr(context, (double(__cdecl *)(double))log);
  191. case HelperDirectMath_Sin:
  192. return ShiftCdeclAddr(context, (double(__cdecl *)(double))sin);
  193. case HelperDirectMath_Tan:
  194. return ShiftCdeclAddr(context, (double(__cdecl *)(double))tan);
  195. #endif
  196. //
  197. // Methods that we don't want to get marked as CFG targets as they make unprotected calls
  198. //
  199. #ifdef _CONTROL_FLOW_GUARD
  200. case HelperGuardCheckCall:
  201. return (intptr_t)__guard_check_icall_fptr; // OOP JIT: ntdll load at same address across all process
  202. #endif
  203. case HelperOp_TryCatch:
  204. return ShiftStdcallAddr(context, Js::JavascriptExceptionOperators::OP_TryCatch);
  205. case HelperOp_TryFinally:
  206. return ShiftStdcallAddr(context, Js::JavascriptExceptionOperators::OP_TryFinally);
  207. case HelperOp_TryFinallyNoOpt:
  208. return ShiftStdcallAddr(context, Js::JavascriptExceptionOperators::OP_TryFinallyNoOpt);
  209. //
  210. // Methods that we don't want to get marked as CFG targets as they dump all registers to a controlled address
  211. //
  212. case HelperSaveAllRegistersAndBailOut:
  213. return ShiftStdcallAddr(context, LinearScanMD::SaveAllRegistersAndBailOut);
  214. case HelperSaveAllRegistersAndBranchBailOut:
  215. return ShiftStdcallAddr(context, LinearScanMD::SaveAllRegistersAndBranchBailOut);
  216. #ifdef _M_IX86
  217. case HelperSaveAllRegistersNoSse2AndBailOut:
  218. return ShiftStdcallAddr(context, LinearScanMD::SaveAllRegistersNoSse2AndBailOut);
  219. case HelperSaveAllRegistersNoSse2AndBranchBailOut:
  220. return ShiftStdcallAddr(context, LinearScanMD::SaveAllRegistersNoSse2AndBranchBailOut);
  221. #endif
  222. }
  223. Assume(UNREACHED);
  224. return 0;
  225. }
  226. ///----------------------------------------------------------------------------
  227. ///
  228. /// GetMethodOriginalAddress
  229. ///
  230. /// returns the memory address of the helperMethod,
  231. /// this one is never the intercepted by debugger helper.
  232. ///
  233. ///----------------------------------------------------------------------------
  234. intptr_t GetMethodOriginalAddress(ThreadContextInfo * context, JnHelperMethod helperMethod)
  235. {
  236. AssertOrFailFast(helperMethod >= 0 && helperMethod < IR::JnHelperMethodCount);
  237. intptr_t address = GetHelperMethods()[static_cast<WORD>(helperMethod)];
  238. if (address == 0)
  239. {
  240. return GetNonTableMethodAddress(context, helperMethod);
  241. }
  242. return ShiftAddr(context, address);
  243. }
  244. #if DBG_DUMP || defined(ENABLE_IR_VIEWER)
  245. char16 const * const JnHelperMethodNames[] =
  246. {
  247. #define HELPERCALL(Name, Address, Attributes) _u("") STRINGIZEW(Name) _u(""),
  248. #include "JnHelperMethodList.h"
  249. NULL
  250. };
  251. ///----------------------------------------------------------------------------
  252. ///
  253. /// GetMethodName
  254. ///
  255. /// returns the string representing the name of the helperMethod.
  256. ///
  257. ///----------------------------------------------------------------------------
  258. char16 const*
  259. GetMethodName(JnHelperMethod helperMethod)
  260. {
  261. return JnHelperMethodNames[static_cast<WORD>(helperMethod)];
  262. }
  263. #endif //#if DBG_DUMP
  264. } //namespace IR
  265. #if DBG_DUMP || defined(ENABLE_IR_VIEWER)
  266. const char16 *GetVtableName(VTableValue value)
  267. {
  268. switch (value)
  269. {
  270. #if !defined(_M_X64)
  271. case VtableJavascriptNumber:
  272. return _u("vtable JavascriptNumber");
  273. break;
  274. #endif
  275. case VtableDynamicObject:
  276. return _u("vtable DynamicObject");
  277. break;
  278. case VtableInvalid:
  279. return _u("vtable Invalid");
  280. break;
  281. case VtablePropertyString:
  282. return _u("vtable PropertyString");
  283. break;
  284. case VtableJavascriptBoolean:
  285. return _u("vtable JavascriptBoolean");
  286. break;
  287. case VtableJavascriptArray:
  288. return _u("vtable JavascriptArray");
  289. break;
  290. case VtableInt8Array:
  291. return _u("vtable Int8Array");
  292. break;
  293. case VtableUint8Array:
  294. return _u("vtable Uint8Array");
  295. break;
  296. case VtableUint8ClampedArray:
  297. return _u("vtable Uint8ClampedArray");
  298. break;
  299. case VtableInt16Array:
  300. return _u("vtable Int16Array");
  301. break;
  302. case VtableUint16Array:
  303. return _u("vtable Uint16Array");
  304. break;
  305. case VtableInt32Array:
  306. return _u("vtable Int32Array");
  307. break;
  308. case VtableUint32Array:
  309. return _u("vtable Uint32Array");
  310. break;
  311. case VtableFloat32Array:
  312. return _u("vtable Float32Array");
  313. break;
  314. case VtableFloat64Array:
  315. return _u("vtable Float64Array");
  316. break;
  317. case VtableJavascriptPixelArray:
  318. return _u("vtable JavascriptPixelArray");
  319. break;
  320. case VtableInt64Array:
  321. return _u("vtable Int64Array");
  322. break;
  323. case VtableUint64Array:
  324. return _u("vtable Uint64Array");
  325. break;
  326. case VtableInt8VirtualArray:
  327. return _u("vtable Int8VirtualArray");
  328. break;
  329. case VtableUint8VirtualArray:
  330. return _u("vtable Uint8VirtualArray");
  331. break;
  332. case VtableUint8ClampedVirtualArray:
  333. return _u("vtable Uint8ClampedVirtualArray");
  334. break;
  335. case VtableInt16VirtualArray:
  336. return _u("vtable Int16VirtualArray");
  337. break;
  338. case VtableUint16VirtualArray:
  339. return _u("vtable Uint16VirtualArray");
  340. break;
  341. case VtableInt32VirtualArray:
  342. return _u("vtable Int32VirtualArray");
  343. break;
  344. case VtableUint32VirtualArray:
  345. return _u("vtable Uint32VirtualArray");
  346. break;
  347. case VtableFloat32VirtualArray:
  348. return _u("vtable Float32VirtualArray");
  349. break;
  350. case VtableFloat64VirtualArray:
  351. return _u("vtable Float64VirtualArray");
  352. break;
  353. case VtableBoolArray:
  354. return _u("vtable BoolArray");
  355. break;
  356. case VtableCharArray:
  357. return _u("vtable CharArray");
  358. break;
  359. case VtableNativeIntArray:
  360. return _u("vtable NativeIntArray");
  361. break;
  362. case VtableNativeFloatArray:
  363. return _u("vtable NativeFloatArray");
  364. break;
  365. case VtableJavascriptNativeIntArray:
  366. return _u("vtable JavascriptNativeIntArray");
  367. break;
  368. case VtableJavascriptRegExp:
  369. return _u("vtable JavascriptRegExp");
  370. break;
  371. case VtableStackScriptFunction:
  372. return _u("vtable StackScriptFunction");
  373. break;
  374. case VtableScriptFunctionWithInlineCacheAndHomeObj:
  375. return _u("vtable ScriptFunctionWithInlineCacheAndHomeObj");
  376. break;
  377. case VtableScriptFunctionWithInlineCacheHomeObjAndComputedName:
  378. return _u("vtable ScriptFunctionWithInlineCacheHomeObjAndComputedName");
  379. break;
  380. case VtableConcatStringMulti:
  381. return _u("vtable ConcatStringMulti");
  382. break;
  383. case VtableCompoundString:
  384. return _u("vtable CompoundString");
  385. break;
  386. default:
  387. Assert(false);
  388. break;
  389. }
  390. return _u("vtable unknown");
  391. }
  392. #endif
  393. namespace HelperMethodAttributes
  394. {
  395. // Position: same as in JnHelperMethod enum.
  396. // Value: one or more of OR'ed HelperMethodAttribute values.
  397. static const BYTE JnHelperMethodAttributes[] =
  398. {
  399. #define HELPERCALL(Name, Address, Attributes) Attributes,
  400. #include "JnHelperMethodList.h"
  401. };
  402. // Returns true if the helper can throw non-OOM / non-SO exception.
  403. bool CanThrow(IR::JnHelperMethod helper)
  404. {
  405. return (JnHelperMethodAttributes[helper] & AttrCanThrow) != 0;
  406. }
  407. bool IsInVariant(IR::JnHelperMethod helper)
  408. {
  409. return (JnHelperMethodAttributes[helper] & AttrInVariant) != 0;
  410. }
  411. bool CanBeReentrant(IR::JnHelperMethod helper)
  412. {
  413. return (JnHelperMethodAttributes[helper] & AttrCanNotBeReentrant) == 0;
  414. }
  415. bool TempObjectProducing(IR::JnHelperMethod helper)
  416. {
  417. return (JnHelperMethodAttributes[helper] & AttrTempObjectProducing) != 0;
  418. }
  419. #ifdef DBG_DUMP
  420. struct ValidateHelperHeaders
  421. {
  422. ValidateHelperHeaders()
  423. {
  424. #define HELPERCALL(Name, Address, Attributes)
  425. #define HELPERCALLCHK(Name, Address, Attributes) \
  426. Assert(JitHelperUtils::helper##Name##_implemented);
  427. #include "../Backend/JnHelperMethodList.h"
  428. }
  429. };
  430. ValidateHelperHeaders validateHelperHeaders;
  431. #endif
  432. } //namespace HelperMethodAttributes