JsrtInternal.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. #pragma once
  6. #include "JsrtExceptionBase.h"
  7. #include "Exceptions/EvalDisabledException.h"
  8. //A class to ensure that even when exceptions are thrown we update any event recording info we were in the middle of
  9. #if ENABLE_TTD
  10. typedef TTD::TTDJsRTActionResultAutoRecorder TTDRecorder;
  11. #else
  12. typedef struct {} TTDRecorder;
  13. #endif
  14. #define PARAM_NOT_NULL(p) \
  15. if (p == nullptr) \
  16. { \
  17. return JsErrorNullArgument; \
  18. }
  19. #define VALIDATE_JSREF(p) \
  20. if (p == JS_INVALID_REFERENCE) \
  21. { \
  22. return JsErrorInvalidArgument; \
  23. } \
  24. #define MARSHAL_OBJECT(p, scriptContext) \
  25. Js::RecyclableObject* __obj = Js::RecyclableObject::FromVar(p); \
  26. if (__obj->GetScriptContext() != scriptContext) \
  27. { \
  28. if(__obj->GetScriptContext()->GetThreadContext() != scriptContext->GetThreadContext()) \
  29. { \
  30. return JsErrorWrongRuntime; \
  31. } \
  32. p = Js::CrossSite::MarshalVar(scriptContext, __obj); \
  33. }
  34. #define VALIDATE_INCOMING_RUNTIME_HANDLE(p) \
  35. { \
  36. if (p == JS_INVALID_RUNTIME_HANDLE) \
  37. { \
  38. return JsErrorInvalidArgument; \
  39. } \
  40. }
  41. #define VALIDATE_INCOMING_PROPERTYID(p) \
  42. { \
  43. if (p == JS_INVALID_REFERENCE || \
  44. Js::IsInternalPropertyId(((Js::PropertyRecord *)p)->GetPropertyId())) \
  45. { \
  46. return JsErrorInvalidArgument; \
  47. } \
  48. }
  49. #define VALIDATE_INCOMING_REFERENCE(p, scriptContext) \
  50. { \
  51. VALIDATE_JSREF(p); \
  52. if (Js::RecyclableObject::Is(p)) \
  53. { \
  54. MARSHAL_OBJECT(p, scriptContext) \
  55. } \
  56. }
  57. #define VALIDATE_INCOMING_OBJECT(p, scriptContext) \
  58. { \
  59. VALIDATE_JSREF(p); \
  60. if (!Js::JavascriptOperators::IsObject(p)) \
  61. { \
  62. return JsErrorArgumentNotObject; \
  63. } \
  64. MARSHAL_OBJECT(p, scriptContext) \
  65. }
  66. #define VALIDATE_INCOMING_OBJECT_OR_NULL(p, scriptContext) \
  67. { \
  68. VALIDATE_JSREF(p); \
  69. if (!Js::JavascriptOperators::IsObjectOrNull(p)) \
  70. { \
  71. return JsErrorArgumentNotObject; \
  72. } \
  73. MARSHAL_OBJECT(p, scriptContext) \
  74. }
  75. #define VALIDATE_INCOMING_FUNCTION(p, scriptContext) \
  76. { \
  77. VALIDATE_JSREF(p); \
  78. if (!Js::JavascriptFunction::Is(p)) \
  79. { \
  80. return JsErrorInvalidArgument; \
  81. } \
  82. MARSHAL_OBJECT(p, scriptContext) \
  83. }
  84. template <class Fn>
  85. JsErrorCode GlobalAPIWrapper_Core(Fn fn)
  86. {
  87. JsErrorCode errCode = JsNoError;
  88. try
  89. {
  90. // For now, treat this like an out of memory; consider if we should do something else here.
  91. AUTO_NESTED_HANDLED_EXCEPTION_TYPE((ExceptionType)(ExceptionType_OutOfMemory | ExceptionType_StackOverflow));
  92. errCode = fn();
  93. // These are error codes that should only be produced by the wrappers and should never
  94. // be produced by the internal calls.
  95. Assert(errCode != JsErrorFatal &&
  96. errCode != JsErrorNoCurrentContext &&
  97. errCode != JsErrorInExceptionState &&
  98. errCode != JsErrorInDisabledState &&
  99. errCode != JsErrorOutOfMemory &&
  100. errCode != JsErrorScriptException &&
  101. errCode != JsErrorScriptTerminated);
  102. }
  103. CATCH_STATIC_JAVASCRIPT_EXCEPTION_OBJECT(errCode)
  104. CATCH_OTHER_EXCEPTIONS(errCode)
  105. return errCode;
  106. }
  107. template <class Fn>
  108. JsErrorCode GlobalAPIWrapper(Fn fn)
  109. {
  110. TTDRecorder _actionEntryPopper;
  111. JsErrorCode errCode = GlobalAPIWrapper_Core([&fn, &_actionEntryPopper]() -> JsErrorCode
  112. {
  113. return fn(_actionEntryPopper);
  114. });
  115. #if ENABLE_TTD
  116. _actionEntryPopper.CompleteWithStatusCode(errCode);
  117. #endif
  118. return errCode;
  119. }
  120. template <class Fn>
  121. JsErrorCode GlobalAPIWrapper_NoRecord(Fn fn)
  122. {
  123. return GlobalAPIWrapper_Core([&fn]() -> JsErrorCode
  124. {
  125. return fn();
  126. });
  127. }
  128. JsErrorCode CheckContext(JsrtContext *currentContext, bool verifyRuntimeState, bool allowInObjectBeforeCollectCallback = false);
  129. template <bool verifyRuntimeState, class Fn>
  130. JsErrorCode ContextAPIWrapper_Core(Fn fn)
  131. {
  132. JsrtContext *currentContext = JsrtContext::GetCurrent();
  133. JsErrorCode errCode = CheckContext(currentContext, verifyRuntimeState);
  134. if(errCode != JsNoError)
  135. {
  136. return errCode;
  137. }
  138. Js::ScriptContext *scriptContext = currentContext->GetScriptContext();
  139. try
  140. {
  141. AUTO_NESTED_HANDLED_EXCEPTION_TYPE((ExceptionType)(ExceptionType_OutOfMemory | ExceptionType_JavascriptException));
  142. // Enter script
  143. BEGIN_ENTER_SCRIPT(scriptContext, true, true, true)
  144. {
  145. errCode = fn(scriptContext);
  146. }
  147. END_ENTER_SCRIPT
  148. // These are error codes that should only be produced by the wrappers and should never
  149. // be produced by the internal calls.
  150. Assert(errCode != JsErrorFatal &&
  151. errCode != JsErrorNoCurrentContext &&
  152. errCode != JsErrorInExceptionState &&
  153. errCode != JsErrorInDisabledState &&
  154. errCode != JsErrorOutOfMemory &&
  155. errCode != JsErrorScriptException &&
  156. errCode != JsErrorScriptTerminated);
  157. }
  158. catch(Js::OutOfMemoryException)
  159. {
  160. errCode = JsErrorOutOfMemory;
  161. }
  162. catch(const Js::JavascriptException& err)
  163. {
  164. scriptContext->GetThreadContext()->SetRecordedException(err.GetAndClear());
  165. errCode = JsErrorScriptException;
  166. }
  167. catch(Js::ScriptAbortException)
  168. {
  169. Assert(scriptContext->GetThreadContext()->GetRecordedException() == nullptr);
  170. scriptContext->GetThreadContext()->SetRecordedException(scriptContext->GetThreadContext()->GetPendingTerminatedErrorObject());
  171. errCode = JsErrorScriptTerminated;
  172. }
  173. catch(Js::EvalDisabledException)
  174. {
  175. errCode = JsErrorScriptEvalDisabled;
  176. }
  177. CATCH_OTHER_EXCEPTIONS(errCode)
  178. return errCode;
  179. }
  180. template <bool verifyRuntimeState, class Fn>
  181. JsErrorCode ContextAPIWrapper(Fn fn)
  182. {
  183. TTDRecorder _actionEntryPopper;
  184. JsErrorCode errCode = ContextAPIWrapper_Core<verifyRuntimeState>([&fn, &_actionEntryPopper](Js::ScriptContext* scriptContext) -> JsErrorCode
  185. {
  186. return fn(scriptContext, _actionEntryPopper);
  187. });
  188. #if ENABLE_TTD
  189. _actionEntryPopper.CompleteWithStatusCode(errCode);
  190. #endif
  191. return errCode;
  192. }
  193. template <bool verifyRuntimeState, class Fn>
  194. JsErrorCode ContextAPIWrapper_NoRecord(Fn fn)
  195. {
  196. return ContextAPIWrapper_Core<verifyRuntimeState>([&fn](Js::ScriptContext* scriptContext) -> JsErrorCode
  197. {
  198. return fn(scriptContext);
  199. });
  200. }
  201. // allowInObjectBeforeCollectCallback only when current API is guaranteed not to do recycler allocation.
  202. template <class Fn>
  203. JsErrorCode ContextAPINoScriptWrapper_Core(Fn fn, bool allowInObjectBeforeCollectCallback = false, bool scriptExceptionAllowed = false)
  204. {
  205. JsrtContext *currentContext = JsrtContext::GetCurrent();
  206. JsErrorCode errCode = CheckContext(currentContext, /*verifyRuntimeState*/true, allowInObjectBeforeCollectCallback);
  207. if(errCode != JsNoError)
  208. {
  209. return errCode;
  210. }
  211. Js::ScriptContext *scriptContext = currentContext->GetScriptContext();
  212. try
  213. {
  214. // For now, treat this like an out of memory; consider if we should do something else here.
  215. AUTO_NESTED_HANDLED_EXCEPTION_TYPE((ExceptionType)(ExceptionType_OutOfMemory | ExceptionType_StackOverflow));
  216. errCode = fn(scriptContext);
  217. // These are error codes that should only be produced by the wrappers and should never
  218. // be produced by the internal calls.
  219. Assert(errCode != JsErrorFatal &&
  220. errCode != JsErrorNoCurrentContext &&
  221. errCode != JsErrorInExceptionState &&
  222. errCode != JsErrorInDisabledState &&
  223. errCode != JsErrorOutOfMemory &&
  224. (scriptExceptionAllowed || errCode != JsErrorScriptException) &&
  225. errCode != JsErrorScriptTerminated);
  226. }
  227. CATCH_STATIC_JAVASCRIPT_EXCEPTION_OBJECT(errCode)
  228. catch(const Js::JavascriptException& err)
  229. {
  230. AssertMsg(false, "Should never get JavascriptExceptionObject for ContextAPINoScriptWrapper.");
  231. scriptContext->GetThreadContext()->SetRecordedException(err.GetAndClear());
  232. errCode = JsErrorScriptException;
  233. }
  234. catch(Js::ScriptAbortException)
  235. {
  236. Assert(scriptContext->GetThreadContext()->GetRecordedException() == nullptr);
  237. scriptContext->GetThreadContext()->SetRecordedException(scriptContext->GetThreadContext()->GetPendingTerminatedErrorObject());
  238. errCode = JsErrorScriptTerminated;
  239. }
  240. CATCH_OTHER_EXCEPTIONS(errCode)
  241. return errCode;
  242. }
  243. template <class Fn>
  244. JsErrorCode ContextAPINoScriptWrapper(Fn fn, bool allowInObjectBeforeCollectCallback = false, bool scriptExceptionAllowed = false)
  245. {
  246. TTDRecorder _actionEntryPopper;
  247. JsErrorCode errCode = ContextAPINoScriptWrapper_Core([&fn, &_actionEntryPopper](Js::ScriptContext* scriptContext) -> JsErrorCode
  248. {
  249. return fn(scriptContext, _actionEntryPopper);
  250. }, allowInObjectBeforeCollectCallback, scriptExceptionAllowed);
  251. #if ENABLE_TTD
  252. _actionEntryPopper.CompleteWithStatusCode(errCode);
  253. #endif
  254. return errCode;
  255. }
  256. template <class Fn>
  257. JsErrorCode ContextAPINoScriptWrapper_NoRecord(Fn fn, bool allowInObjectBeforeCollectCallback = false, bool scriptExceptionAllowed = false)
  258. {
  259. return ContextAPINoScriptWrapper_Core([&fn](Js::ScriptContext* scriptContext) -> JsErrorCode
  260. {
  261. return fn(scriptContext);
  262. }, allowInObjectBeforeCollectCallback, scriptExceptionAllowed);
  263. }
  264. template <class Fn>
  265. JsErrorCode SetContextAPIWrapper(JsrtContext* newContext, Fn fn)
  266. {
  267. JsrtContext* oldContext = JsrtContext::GetCurrent();
  268. Js::ScriptContext* scriptContext = newContext->GetScriptContext();
  269. JsErrorCode errorCode = JsNoError;
  270. try
  271. {
  272. // For now, treat this like an out of memory; consider if we should do something else here.
  273. AUTO_NESTED_HANDLED_EXCEPTION_TYPE((ExceptionType)(ExceptionType_OutOfMemory | ExceptionType_StackOverflow | ExceptionType_JavascriptException));
  274. if (JsrtContext::TrySetCurrent(newContext))
  275. {
  276. // Enter script
  277. BEGIN_ENTER_SCRIPT(scriptContext, true, true, true)
  278. {
  279. errorCode = fn(scriptContext);
  280. }
  281. END_ENTER_SCRIPT
  282. }
  283. else
  284. {
  285. errorCode = JsErrorWrongThread;
  286. }
  287. // These are error codes that should only be produced by the wrappers and should never
  288. // be produced by the internal calls.
  289. Assert(errorCode != JsErrorFatal &&
  290. errorCode != JsErrorNoCurrentContext &&
  291. errorCode != JsErrorInExceptionState &&
  292. errorCode != JsErrorInDisabledState &&
  293. errorCode != JsErrorOutOfMemory &&
  294. errorCode != JsErrorScriptException &&
  295. errorCode != JsErrorScriptTerminated);
  296. }
  297. catch (Js::OutOfMemoryException)
  298. {
  299. errorCode = JsErrorOutOfMemory;
  300. }
  301. catch (const Js::JavascriptException& err)
  302. {
  303. scriptContext->GetThreadContext()->SetRecordedException(err.GetAndClear());
  304. errorCode = JsErrorScriptException;
  305. }
  306. catch (Js::ScriptAbortException)
  307. {
  308. Assert(scriptContext->GetThreadContext()->GetRecordedException() == nullptr);
  309. scriptContext->GetThreadContext()->SetRecordedException(scriptContext->GetThreadContext()->GetPendingTerminatedErrorObject());
  310. errorCode = JsErrorScriptTerminated;
  311. }
  312. catch (Js::EvalDisabledException)
  313. {
  314. errorCode = JsErrorScriptEvalDisabled;
  315. }
  316. catch (Js::StackOverflowException)
  317. {
  318. return JsErrorOutOfMemory;
  319. }
  320. CATCH_OTHER_EXCEPTIONS(errorCode)
  321. AUTO_NESTED_HANDLED_EXCEPTION_TYPE((ExceptionType)(ExceptionType_OutOfMemory | ExceptionType_StackOverflow));
  322. JsrtContext::TrySetCurrent(oldContext);
  323. return errorCode;
  324. }
  325. void HandleScriptCompileError(Js::ScriptContext * scriptContext, CompileScriptException * se, const WCHAR * sourceUrl = nullptr);
  326. #if DBG
  327. #define _PREPARE_RETURN_NO_EXCEPTION __debugCheckNoException.hasException = false;
  328. #else
  329. #define _PREPARE_RETURN_NO_EXCEPTION
  330. #endif
  331. #define BEGIN_JSRT_NO_EXCEPTION BEGIN_NO_EXCEPTION
  332. #define END_JSRT_NO_EXCEPTION END_NO_EXCEPTION return JsNoError;
  333. #define RETURN_NO_EXCEPTION(x) _PREPARE_RETURN_NO_EXCEPTION return x
  334. ////
  335. //Define compact TTD macros for use in the JSRT API's
  336. //A class to ensure that even when exceptions are thrown we update any event recording info we were in the middle of
  337. #if ENABLE_TTD
  338. #define PERFORM_JSRT_TTD_RECORD_ACTION_CHECK(CTX) (CTX)->ShouldPerformRecordAction()
  339. #define PERFORM_JSRT_TTD_RECORD_ACTION(CTX, ACTION_CODE, ...) \
  340. if(PERFORM_JSRT_TTD_RECORD_ACTION_CHECK(CTX)) \
  341. { \
  342. (CTX)->GetThreadContext()->TTDLog->##ACTION_CODE##(_actionEntryPopper, ##__VA_ARGS__); \
  343. }
  344. #define PERFORM_JSRT_TTD_RECORD_ACTION_RESULT(CTX, RESULT) if(PERFORM_JSRT_TTD_RECORD_ACTION_CHECK(CTX)) \
  345. { \
  346. _actionEntryPopper.SetResult(RESULT); \
  347. }
  348. //TODO: find and replace all of the occourences of this in jsrt.cpp
  349. #define PERFORM_JSRT_TTD_RECORD_ACTION_NOT_IMPLEMENTED(CTX) if(PERFORM_JSRT_TTD_RECORD_ACTION_CHECK(CTX)) { AssertMsg(false, "Need to implement support here!!!"); }
  350. #else
  351. #define PERFORM_JSRT_TTD_RECORD_ACTION_CHECK(CTX) false
  352. #define PERFORM_JSRT_TTD_RECORD_ACTION(CTX, ACTION_CODE, ...)
  353. #define PERFORM_JSRT_TTD_RECORD_ACTION_RESULT(CTX, RESULT)
  354. #define PERFORM_JSRT_TTD_RECORD_ACTION_NOT_IMPLEMENTED(CTX)
  355. #endif