JnHelperMethod.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. 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. const void * const JnHelperMethodAddresses[] =
  18. {
  19. #define HELPERCALL(Name, Address, Attributes) static_cast<void *>(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. const void * const JnHelperMethodAddresses_SSE2[] =
  28. {
  29. #define SSE2MATH
  30. #define HELPERCALL(Name, Address, Attributes) static_cast<void *>(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. const void * const*GetHelperMethods()
  39. {
  40. if (AutoSystemInfo::Data.SSE2Available())
  41. {
  42. return JnHelperMethodAddresses_SSE2;
  43. }
  44. return JnHelperMethodAddresses;
  45. }
  46. #else
  47. const void *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(const void * 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 void const* const helperMethodWrappers[] = {
  81. &Js::HelperMethodWrapper0,
  82. &Js::HelperMethodWrapper1,
  83. &Js::HelperMethodWrapper2,
  84. &Js::HelperMethodWrapper3,
  85. &Js::HelperMethodWrapper4,
  86. &Js::HelperMethodWrapper5,
  87. &Js::HelperMethodWrapper6,
  88. &Js::HelperMethodWrapper7,
  89. &Js::HelperMethodWrapper8,
  90. &Js::HelperMethodWrapper9,
  91. &Js::HelperMethodWrapper10,
  92. &Js::HelperMethodWrapper11,
  93. &Js::HelperMethodWrapper12,
  94. &Js::HelperMethodWrapper13,
  95. &Js::HelperMethodWrapper14,
  96. &Js::HelperMethodWrapper15,
  97. &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. void const*
  108. GetMethodAddress(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 helperMethodWrappers[diagOpnd->m_argCount];
  124. }
  125. else
  126. {
  127. AssertMsg(FALSE, "Unsupported arg count (need to implement).");
  128. }
  129. }
  130. return GetMethodOriginalAddress(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)
  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 __declspec(noinline) void * const GetNonTableMethodAddress(JnHelperMethod helperMethod)
  145. {
  146. switch (helperMethod)
  147. {
  148. //
  149. // DllImport methods
  150. //
  151. #if defined(_M_IX86)
  152. case HelperDirectMath_Acos:
  153. return (double(*)(double))__libm_sse2_acos;
  154. case HelperDirectMath_Asin:
  155. return (double(*)(double))__libm_sse2_asin;
  156. case HelperDirectMath_Atan:
  157. return (double(*)(double))__libm_sse2_atan;
  158. case HelperDirectMath_Atan2:
  159. return (double(*)(double, double))__libm_sse2_atan2;
  160. case HelperDirectMath_Cos:
  161. return (double(*)(double))__libm_sse2_cos;
  162. case HelperDirectMath_Exp:
  163. return (double(*)(double))__libm_sse2_exp;
  164. case HelperDirectMath_Log:
  165. return (double(*)(double))__libm_sse2_log;
  166. case HelperDirectMath_Sin:
  167. return (double(*)(double))__libm_sse2_sin;
  168. case HelperDirectMath_Tan:
  169. return (double(*)(double))__libm_sse2_tan;
  170. #endif
  171. #ifdef _CONTROL_FLOW_GUARD
  172. case HelperGuardCheckCall:
  173. return __guard_check_icall_fptr;
  174. #endif
  175. case HelperDirectMath_FloorDb:
  176. return (double(*)(double))floor;
  177. case HelperDirectMath_FloorFlt:
  178. return (float(*)(float))floor;
  179. case HelperDirectMath_CeilDb:
  180. return (double(*)(double))ceil;
  181. case HelperDirectMath_CeilFlt:
  182. return (float(*)(float))ceil;
  183. //
  184. // These are statically initialized to an import thunk, but let's keep them out of the table in case a new CRT changes this
  185. //
  186. case HelperMemCmp:
  187. return (int(*)(void *, void *, size_t))memcmp;
  188. case HelperMemCpy:
  189. return (int(*)(void *, void *, size_t))memcpy;
  190. #if defined(_M_X64)
  191. case HelperDirectMath_Acos:
  192. return (double(*)(double))acos;
  193. case HelperDirectMath_Asin:
  194. return (double(*)(double))asin;
  195. case HelperDirectMath_Atan:
  196. return (double(*)(double))atan;
  197. case HelperDirectMath_Atan2:
  198. return (double(*)(double, double))atan2;
  199. case HelperDirectMath_Cos:
  200. return (double(*)(double))cos;
  201. case HelperDirectMath_Exp:
  202. return (double(*)(double))exp;
  203. case HelperDirectMath_Log:
  204. return (double(*)(double))log;
  205. case HelperDirectMath_Sin:
  206. return (double(*)(double))sin;
  207. case HelperDirectMath_Tan:
  208. return (double(*)(double))tan;
  209. #elif defined(_M_ARM32_OR_ARM64)
  210. case HelperDirectMath_Acos:
  211. return (double(*)(double))acos;
  212. case HelperDirectMath_Asin:
  213. return (double(*)(double))asin;
  214. case HelperDirectMath_Atan:
  215. return (double(*)(double))atan;
  216. case HelperDirectMath_Atan2:
  217. return (double(*)(double, double))atan2;
  218. case HelperDirectMath_Cos:
  219. return (double(*)(double))cos;
  220. case HelperDirectMath_Exp:
  221. return (double(*)(double))exp;
  222. case HelperDirectMath_Log:
  223. return (double(*)(double))log;
  224. case HelperDirectMath_Sin:
  225. return (double(*)(double))sin;
  226. case HelperDirectMath_Tan:
  227. return (double(*)(double))tan;
  228. #endif
  229. //
  230. // Methods that we don't want to get marked as CFG targets as they make unprotected calls
  231. //
  232. case HelperOp_TryCatch:
  233. return Js::JavascriptExceptionOperators::OP_TryCatch;
  234. case HelperOp_TryFinally:
  235. return Js::JavascriptExceptionOperators::OP_TryFinally;
  236. //
  237. // Methods that we don't want to get marked as CFG targets as they dump all registers to a controlled address
  238. //
  239. case HelperSaveAllRegistersAndBailOut:
  240. return LinearScanMD::SaveAllRegistersAndBailOut;
  241. case HelperSaveAllRegistersAndBranchBailOut:
  242. return LinearScanMD::SaveAllRegistersAndBranchBailOut;
  243. #ifdef _M_IX86
  244. case HelperSaveAllRegistersNoSse2AndBailOut:
  245. return LinearScanMD::SaveAllRegistersNoSse2AndBailOut;
  246. case HelperSaveAllRegistersNoSse2AndBranchBailOut:
  247. return LinearScanMD::SaveAllRegistersNoSse2AndBranchBailOut;
  248. #endif
  249. }
  250. Assume(UNREACHED);
  251. return nullptr;
  252. }
  253. ///----------------------------------------------------------------------------
  254. ///
  255. /// GetMethodOriginalAddress
  256. ///
  257. /// returns the memory address of the helperMethod,
  258. /// this one is never the intercepted by debugger helper.
  259. ///
  260. ///----------------------------------------------------------------------------
  261. void const * GetMethodOriginalAddress(JnHelperMethod helperMethod)
  262. {
  263. const void *address = GetHelperMethods()[static_cast<WORD>(helperMethod)];
  264. if (address == nullptr)
  265. {
  266. return GetNonTableMethodAddress(helperMethod);
  267. }
  268. return address;
  269. }
  270. #if DBG_DUMP || defined(ENABLE_IR_VIEWER)
  271. wchar_t const * const JnHelperMethodNames[] =
  272. {
  273. #define HELPERCALL(Name, Address, Attributes) L"" STRINGIZEW(Name) L"",
  274. #include "JnHelperMethodList.h"
  275. #undef HELPERCALL
  276. NULL
  277. };
  278. ///----------------------------------------------------------------------------
  279. ///
  280. /// GetMethodName
  281. ///
  282. /// returns the string representing the name of the helperMethod.
  283. ///
  284. ///----------------------------------------------------------------------------
  285. wchar_t const*
  286. GetMethodName(JnHelperMethod helperMethod)
  287. {
  288. return JnHelperMethodNames[static_cast<WORD>(helperMethod)];
  289. }
  290. #endif //#if DBG_DUMP
  291. } //namespace IR
  292. #if DBG_DUMP || defined(ENABLE_IR_VIEWER)
  293. const wchar_t *GetVtableName(VTableValue value)
  294. {
  295. switch (value)
  296. {
  297. #if !defined(_M_X64)
  298. case VtableJavascriptNumber:
  299. return L"vtable JavascriptNumber";
  300. break;
  301. #endif
  302. case VtableDynamicObject:
  303. return L"vtable DynamicObject";
  304. break;
  305. case VtableInvalid:
  306. return L"vtable Invalid";
  307. break;
  308. case VtablePropertyString:
  309. return L"vtable PropertyString";
  310. break;
  311. case VtableJavascriptBoolean:
  312. return L"vtable JavascriptBoolean";
  313. break;
  314. case VtableSmallDynamicObjectSnapshotEnumeratorWPCache:
  315. return L"vtable SmallDynamicObjectSnapshotEnumeratorWPCache";
  316. break;
  317. case VtableJavascriptArray:
  318. return L"vtable JavascriptArray";
  319. break;
  320. case VtableInt8Array:
  321. return L"vtable Int8Array";
  322. break;
  323. case VtableUint8Array:
  324. return L"vtable Uint8Array";
  325. break;
  326. case VtableUint8ClampedArray:
  327. return L"vtable Uint8ClampedArray";
  328. break;
  329. case VtableInt16Array:
  330. return L"vtable Int16Array";
  331. break;
  332. case VtableUint16Array:
  333. return L"vtable Uint16Array";
  334. break;
  335. case VtableInt32Array:
  336. return L"vtable Int32Array";
  337. break;
  338. case VtableUint32Array:
  339. return L"vtable Uint32Array";
  340. break;
  341. case VtableFloat32Array:
  342. return L"vtable Float32Array";
  343. break;
  344. case VtableFloat64Array:
  345. return L"vtable Float64Array";
  346. break;
  347. case VtableJavascriptPixelArray:
  348. return L"vtable JavascriptPixelArray";
  349. break;
  350. case VtableInt64Array:
  351. return L"vtable Int64Array";
  352. break;
  353. case VtableUint64Array:
  354. return L"vtable Uint64Array";
  355. break;
  356. case VtableInt8VirtualArray:
  357. return L"vtable Int8VirtualArray";
  358. break;
  359. case VtableUint8VirtualArray:
  360. return L"vtable Uint8VirtualArray";
  361. break;
  362. case VtableUint8ClampedVirtualArray:
  363. return L"vtable Uint8ClampedVirtualArray";
  364. break;
  365. case VtableInt16VirtualArray:
  366. return L"vtable Int16VirtualArray";
  367. break;
  368. case VtableUint16VirtualArray:
  369. return L"vtable Uint16VirtualArray";
  370. break;
  371. case VtableInt32VirtualArray:
  372. return L"vtable Int32VirtualArray";
  373. break;
  374. case VtableUint32VirtualArray:
  375. return L"vtable Uint32VirtualArray";
  376. break;
  377. case VtableFloat32VirtualArray:
  378. return L"vtable Float32VirtualArray";
  379. break;
  380. case VtableFloat64VirtualArray:
  381. return L"vtable Float64VirtualArray";
  382. break;
  383. case VtableBoolArray:
  384. return L"vtable BoolArray";
  385. break;
  386. case VtableCharArray:
  387. return L"vtable CharArray";
  388. break;
  389. case VtableNativeIntArray:
  390. return L"vtable NativeIntArray";
  391. break;
  392. case VtableNativeFloatArray:
  393. return L"vtable NativeFloatArray";
  394. break;
  395. case VtableJavascriptNativeIntArray:
  396. return L"vtable JavascriptNativeIntArray";
  397. break;
  398. case VtableJavascriptRegExp:
  399. return L"vtable JavascriptRegExp";
  400. break;
  401. case VtableStackScriptFunction:
  402. return L"vtable StackScriptFunction";
  403. break;
  404. case VtableConcatStringMulti:
  405. return L"vtable ConcatStringMulti";
  406. break;
  407. case VtableCompoundString:
  408. return L"vtable CompoundString";
  409. break;
  410. default:
  411. Assert(false);
  412. break;
  413. }
  414. return L"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