JnHelperMethod.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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 HelperMemCmp:
  180. return SHIFT_CRT_ADDR(context, (int(*)(void *, void *, size_t))memcmp);
  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. //
  238. // Methods that we don't want to get marked as CFG targets as they dump all registers to a controlled address
  239. //
  240. case HelperSaveAllRegistersAndBailOut:
  241. return SHIFT_ADDR(context, LinearScanMD::SaveAllRegistersAndBailOut);
  242. case HelperSaveAllRegistersAndBranchBailOut:
  243. return SHIFT_ADDR(context, LinearScanMD::SaveAllRegistersAndBranchBailOut);
  244. #ifdef _M_IX86
  245. case HelperSaveAllRegistersNoSse2AndBailOut:
  246. return SHIFT_ADDR(context, LinearScanMD::SaveAllRegistersNoSse2AndBailOut);
  247. case HelperSaveAllRegistersNoSse2AndBranchBailOut:
  248. return SHIFT_ADDR(context, LinearScanMD::SaveAllRegistersNoSse2AndBranchBailOut);
  249. #endif
  250. }
  251. Assume(UNREACHED);
  252. return 0;
  253. }
  254. ///----------------------------------------------------------------------------
  255. ///
  256. /// GetMethodOriginalAddress
  257. ///
  258. /// returns the memory address of the helperMethod,
  259. /// this one is never the intercepted by debugger helper.
  260. ///
  261. ///----------------------------------------------------------------------------
  262. intptr_t GetMethodOriginalAddress(ThreadContextInfo * context, JnHelperMethod helperMethod)
  263. {
  264. intptr_t address = GetHelperMethods()[static_cast<WORD>(helperMethod)];
  265. if (address == 0)
  266. {
  267. return GetNonTableMethodAddress(context, helperMethod);
  268. }
  269. return SHIFT_ADDR(context, address);
  270. }
  271. #if DBG_DUMP || defined(ENABLE_IR_VIEWER)
  272. char16 const * const JnHelperMethodNames[] =
  273. {
  274. #define HELPERCALL(Name, Address, Attributes) _u("") STRINGIZEW(Name) _u(""),
  275. #include "JnHelperMethodList.h"
  276. #undef HELPERCALL
  277. NULL
  278. };
  279. ///----------------------------------------------------------------------------
  280. ///
  281. /// GetMethodName
  282. ///
  283. /// returns the string representing the name of the helperMethod.
  284. ///
  285. ///----------------------------------------------------------------------------
  286. char16 const*
  287. GetMethodName(JnHelperMethod helperMethod)
  288. {
  289. return JnHelperMethodNames[static_cast<WORD>(helperMethod)];
  290. }
  291. #endif //#if DBG_DUMP
  292. } //namespace IR
  293. #if DBG_DUMP || defined(ENABLE_IR_VIEWER)
  294. const char16 *GetVtableName(VTableValue value)
  295. {
  296. switch (value)
  297. {
  298. #if !defined(_M_X64)
  299. case VtableJavascriptNumber:
  300. return _u("vtable JavascriptNumber");
  301. break;
  302. #endif
  303. case VtableDynamicObject:
  304. return _u("vtable DynamicObject");
  305. break;
  306. case VtableInvalid:
  307. return _u("vtable Invalid");
  308. break;
  309. case VtablePropertyString:
  310. return _u("vtable PropertyString");
  311. break;
  312. case VtableJavascriptBoolean:
  313. return _u("vtable JavascriptBoolean");
  314. break;
  315. case VtableJavascriptArray:
  316. return _u("vtable JavascriptArray");
  317. break;
  318. case VtableInt8Array:
  319. return _u("vtable Int8Array");
  320. break;
  321. case VtableUint8Array:
  322. return _u("vtable Uint8Array");
  323. break;
  324. case VtableUint8ClampedArray:
  325. return _u("vtable Uint8ClampedArray");
  326. break;
  327. case VtableInt16Array:
  328. return _u("vtable Int16Array");
  329. break;
  330. case VtableUint16Array:
  331. return _u("vtable Uint16Array");
  332. break;
  333. case VtableInt32Array:
  334. return _u("vtable Int32Array");
  335. break;
  336. case VtableUint32Array:
  337. return _u("vtable Uint32Array");
  338. break;
  339. case VtableFloat32Array:
  340. return _u("vtable Float32Array");
  341. break;
  342. case VtableFloat64Array:
  343. return _u("vtable Float64Array");
  344. break;
  345. case VtableJavascriptPixelArray:
  346. return _u("vtable JavascriptPixelArray");
  347. break;
  348. case VtableInt64Array:
  349. return _u("vtable Int64Array");
  350. break;
  351. case VtableUint64Array:
  352. return _u("vtable Uint64Array");
  353. break;
  354. case VtableInt8VirtualArray:
  355. return _u("vtable Int8VirtualArray");
  356. break;
  357. case VtableUint8VirtualArray:
  358. return _u("vtable Uint8VirtualArray");
  359. break;
  360. case VtableUint8ClampedVirtualArray:
  361. return _u("vtable Uint8ClampedVirtualArray");
  362. break;
  363. case VtableInt16VirtualArray:
  364. return _u("vtable Int16VirtualArray");
  365. break;
  366. case VtableUint16VirtualArray:
  367. return _u("vtable Uint16VirtualArray");
  368. break;
  369. case VtableInt32VirtualArray:
  370. return _u("vtable Int32VirtualArray");
  371. break;
  372. case VtableUint32VirtualArray:
  373. return _u("vtable Uint32VirtualArray");
  374. break;
  375. case VtableFloat32VirtualArray:
  376. return _u("vtable Float32VirtualArray");
  377. break;
  378. case VtableFloat64VirtualArray:
  379. return _u("vtable Float64VirtualArray");
  380. break;
  381. case VtableBoolArray:
  382. return _u("vtable BoolArray");
  383. break;
  384. case VtableCharArray:
  385. return _u("vtable CharArray");
  386. break;
  387. case VtableNativeIntArray:
  388. return _u("vtable NativeIntArray");
  389. break;
  390. case VtableNativeFloatArray:
  391. return _u("vtable NativeFloatArray");
  392. break;
  393. case VtableJavascriptNativeIntArray:
  394. return _u("vtable JavascriptNativeIntArray");
  395. break;
  396. case VtableJavascriptRegExp:
  397. return _u("vtable JavascriptRegExp");
  398. break;
  399. case VtableStackScriptFunction:
  400. return _u("vtable StackScriptFunction");
  401. break;
  402. case VtableConcatStringMulti:
  403. return _u("vtable ConcatStringMulti");
  404. break;
  405. case VtableCompoundString:
  406. return _u("vtable CompoundString");
  407. break;
  408. default:
  409. Assert(false);
  410. break;
  411. }
  412. return _u("vtable unknown");
  413. }
  414. #endif
  415. namespace HelperMethodAttributes
  416. {
  417. // Position: same as in JnHelperMethod enum.
  418. // Value: one or more of OR'ed HelperMethodAttribute values.
  419. static const BYTE JnHelperMethodAttributes[] =
  420. {
  421. #define HELPERCALL(Name, Address, Attributes) Attributes,
  422. #include "JnHelperMethodList.h"
  423. #undef HELPERCALL
  424. };
  425. // Returns true if the helper can throw non-OOM / non-SO exception.
  426. bool CanThrow(IR::JnHelperMethod helper)
  427. {
  428. return (JnHelperMethodAttributes[helper] & AttrCanThrow) != 0;
  429. }
  430. bool IsInVariant(IR::JnHelperMethod helper)
  431. {
  432. return (JnHelperMethodAttributes[helper] & AttrInVariant) != 0;
  433. }
  434. } //namespace HelperMethodAttributes