JnHelperMethod.cpp 15 KB

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