JnHelperMethod.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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_X64)
  152. case HelperDirectMath_FloorDb:
  153. return (double(*)(double))floor;
  154. case HelperDirectMath_FloorFlt:
  155. return (float(*)(float))floor;
  156. case HelperDirectMath_CeilDb:
  157. return (double(*)(double))ceil;
  158. case HelperDirectMath_CeilFlt:
  159. return (float(*)(float))ceil;
  160. #elif defined(_M_IX86)
  161. case HelperDirectMath_Acos:
  162. return (double(*)(double))__libm_sse2_acos;
  163. case HelperDirectMath_Asin:
  164. return (double(*)(double))__libm_sse2_asin;
  165. case HelperDirectMath_Atan:
  166. return (double(*)(double))__libm_sse2_atan;
  167. case HelperDirectMath_Atan2:
  168. return (double(*)(double, double))__libm_sse2_atan2;
  169. case HelperDirectMath_Cos:
  170. return (double(*)(double))__libm_sse2_cos;
  171. case HelperDirectMath_Exp:
  172. return (double(*)(double))__libm_sse2_exp;
  173. case HelperDirectMath_Log:
  174. return (double(*)(double))__libm_sse2_log;
  175. case HelperDirectMath_Sin:
  176. return (double(*)(double))__libm_sse2_sin;
  177. case HelperDirectMath_Tan:
  178. return (double(*)(double))__libm_sse2_tan;
  179. #endif
  180. #ifdef _CONTROL_FLOW_GUARD
  181. case HelperGuardCheckCall:
  182. return __guard_check_icall_fptr;
  183. #endif
  184. //
  185. // These are statically initialized to an import thunk, but let's keep them out of the table in case a new CRT changes this
  186. //
  187. case HelperMemCmp:
  188. return (int(*)(void *, void *, size_t))memcmp;
  189. case HelperMemCpy:
  190. return (int(*)(void *, void *, size_t))memcpy;
  191. #if defined(_M_X64)
  192. case HelperDirectMath_Acos:
  193. return (double(*)(double))acos;
  194. case HelperDirectMath_Asin:
  195. return (double(*)(double))asin;
  196. case HelperDirectMath_Atan:
  197. return (double(*)(double))atan;
  198. case HelperDirectMath_Atan2:
  199. return (double(*)(double, double))atan2;
  200. case HelperDirectMath_Cos:
  201. return (double(*)(double))cos;
  202. case HelperDirectMath_Exp:
  203. return (double(*)(double))exp;
  204. case HelperDirectMath_Log:
  205. return (double(*)(double))log;
  206. case HelperDirectMath_Sin:
  207. return (double(*)(double))sin;
  208. case HelperDirectMath_Tan:
  209. return (double(*)(double))tan;
  210. #elif defined(_M_ARM32_OR_ARM64)
  211. case HelperDirectMath_Acos:
  212. return (double(*)(double))acos;
  213. case HelperDirectMath_Asin:
  214. return (double(*)(double))asin;
  215. case HelperDirectMath_Atan:
  216. return (double(*)(double))atan;
  217. case HelperDirectMath_Atan2:
  218. return (double(*)(double, double))atan2;
  219. case HelperDirectMath_Cos:
  220. return (double(*)(double))cos;
  221. case HelperDirectMath_Exp:
  222. return (double(*)(double))exp;
  223. case HelperDirectMath_Log:
  224. return (double(*)(double))log;
  225. case HelperDirectMath_Sin:
  226. return (double(*)(double))sin;
  227. case HelperDirectMath_Tan:
  228. return (double(*)(double))tan;
  229. #endif
  230. //
  231. // Methods that we don't want to get marked as CFG targets as they make unprotected calls
  232. //
  233. case HelperOp_TryCatch:
  234. return Js::JavascriptExceptionOperators::OP_TryCatch;
  235. case HelperOp_TryFinally:
  236. return 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 LinearScanMD::SaveAllRegistersAndBailOut;
  242. case HelperSaveAllRegistersAndBranchBailOut:
  243. return LinearScanMD::SaveAllRegistersAndBranchBailOut;
  244. #ifdef _M_IX86
  245. case HelperSaveAllRegistersNoSse2AndBailOut:
  246. return LinearScanMD::SaveAllRegistersNoSse2AndBailOut;
  247. case HelperSaveAllRegistersNoSse2AndBranchBailOut:
  248. return LinearScanMD::SaveAllRegistersNoSse2AndBranchBailOut;
  249. #endif
  250. }
  251. Assume(UNREACHED);
  252. return nullptr;
  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. void const * GetMethodOriginalAddress(JnHelperMethod helperMethod)
  263. {
  264. const void *address = GetHelperMethods()[static_cast<WORD>(helperMethod)];
  265. if (address == nullptr)
  266. {
  267. return GetNonTableMethodAddress(helperMethod);
  268. }
  269. return address;
  270. }
  271. #if DBG_DUMP || defined(ENABLE_IR_VIEWER)
  272. wchar_t const * const JnHelperMethodNames[] =
  273. {
  274. #define HELPERCALL(Name, Address, Attributes) L"" STRINGIZEW(Name) L"",
  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. wchar_t 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 wchar_t *GetVtableName(VTableValue value)
  295. {
  296. switch (value)
  297. {
  298. #if !defined(_M_X64)
  299. case VtableJavascriptNumber:
  300. return L"vtable JavascriptNumber";
  301. break;
  302. #endif
  303. case VtableDynamicObject:
  304. return L"vtable DynamicObject";
  305. break;
  306. case VtableInvalid:
  307. return L"vtable Invalid";
  308. break;
  309. case VtablePropertyString:
  310. return L"vtable PropertyString";
  311. break;
  312. case VtableJavascriptBoolean:
  313. return L"vtable JavascriptBoolean";
  314. break;
  315. case VtableSmallDynamicObjectSnapshotEnumeratorWPCache:
  316. return L"vtable SmallDynamicObjectSnapshotEnumeratorWPCache";
  317. break;
  318. case VtableJavascriptArray:
  319. return L"vtable JavascriptArray";
  320. break;
  321. case VtableInt8Array:
  322. return L"vtable Int8Array";
  323. break;
  324. case VtableUint8Array:
  325. return L"vtable Uint8Array";
  326. break;
  327. case VtableUint8ClampedArray:
  328. return L"vtable Uint8ClampedArray";
  329. break;
  330. case VtableInt16Array:
  331. return L"vtable Int16Array";
  332. break;
  333. case VtableUint16Array:
  334. return L"vtable Uint16Array";
  335. break;
  336. case VtableInt32Array:
  337. return L"vtable Int32Array";
  338. break;
  339. case VtableUint32Array:
  340. return L"vtable Uint32Array";
  341. break;
  342. case VtableFloat32Array:
  343. return L"vtable Float32Array";
  344. break;
  345. case VtableFloat64Array:
  346. return L"vtable Float64Array";
  347. break;
  348. case VtableJavascriptPixelArray:
  349. return L"vtable JavascriptPixelArray";
  350. break;
  351. case VtableInt64Array:
  352. return L"vtable Int64Array";
  353. break;
  354. case VtableUint64Array:
  355. return L"vtable Uint64Array";
  356. break;
  357. case VtableInt8VirtualArray:
  358. return L"vtable Int8VirtualArray";
  359. break;
  360. case VtableUint8VirtualArray:
  361. return L"vtable Uint8VirtualArray";
  362. break;
  363. case VtableUint8ClampedVirtualArray:
  364. return L"vtable Uint8ClampedVirtualArray";
  365. break;
  366. case VtableInt16VirtualArray:
  367. return L"vtable Int16VirtualArray";
  368. break;
  369. case VtableUint16VirtualArray:
  370. return L"vtable Uint16VirtualArray";
  371. break;
  372. case VtableInt32VirtualArray:
  373. return L"vtable Int32VirtualArray";
  374. break;
  375. case VtableUint32VirtualArray:
  376. return L"vtable Uint32VirtualArray";
  377. break;
  378. case VtableFloat32VirtualArray:
  379. return L"vtable Float32VirtualArray";
  380. break;
  381. case VtableFloat64VirtualArray:
  382. return L"vtable Float64VirtualArray";
  383. break;
  384. case VtableBoolArray:
  385. return L"vtable BoolArray";
  386. break;
  387. case VtableCharArray:
  388. return L"vtable CharArray";
  389. break;
  390. case VtableNativeIntArray:
  391. return L"vtable NativeIntArray";
  392. break;
  393. case VtableNativeFloatArray:
  394. return L"vtable NativeFloatArray";
  395. break;
  396. case VtableJavascriptNativeIntArray:
  397. return L"vtable JavascriptNativeIntArray";
  398. break;
  399. case VtableJavascriptRegExp:
  400. return L"vtable JavascriptRegExp";
  401. break;
  402. case VtableStackScriptFunction:
  403. return L"vtable StackScriptFunction";
  404. break;
  405. case VtableConcatStringMulti:
  406. return L"vtable ConcatStringMulti";
  407. break;
  408. case VtableCompoundString:
  409. return L"vtable CompoundString";
  410. break;
  411. default:
  412. Assert(false);
  413. break;
  414. }
  415. return L"vtable unknown";
  416. }
  417. #endif
  418. namespace HelperMethodAttributes
  419. {
  420. // Position: same as in JnHelperMethod enum.
  421. // Value: one or more of OR'ed HelperMethodAttribute values.
  422. static const BYTE JnHelperMethodAttributes[] =
  423. {
  424. #define HELPERCALL(Name, Address, Attributes) Attributes,
  425. #include "JnHelperMethodList.h"
  426. #undef HELPERCALL
  427. };
  428. // Returns true if the helper can throw non-OOM / non-SO exception.
  429. bool CanThrow(IR::JnHelperMethod helper)
  430. {
  431. return (JnHelperMethodAttributes[helper] & AttrCanThrow) != 0;
  432. }
  433. bool IsInVariant(IR::JnHelperMethod helper)
  434. {
  435. return (JnHelperMethodAttributes[helper] & AttrInVariant) != 0;
  436. }
  437. } //namespace HelperMethodAttributes