JnHelperMethod.cpp 16 KB

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