JnHelperMethod.cpp 15 KB

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