JavascriptExternalFunction.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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 "RuntimeLibraryPch.h"
  6. #include "Types/DeferredTypeHandler.h"
  7. namespace Js
  8. {
  9. // This is a wrapper class for javascript functions that are added directly to the JS engine via BuildDirectFunction
  10. // or CreateConstructor. We add a thunk before calling into user's direct C++ methods, with additional checks:
  11. // . check the script site is still alive.
  12. // . convert globalObject to hostObject
  13. // . leavescriptstart/end
  14. // . wrap the result value with potential cross site access
  15. JavascriptExternalFunction::JavascriptExternalFunction(ExternalMethod entryPoint, DynamicType* type)
  16. : RuntimeFunction(type, &EntryInfo::ExternalFunctionThunk), nativeMethod(entryPoint), signature(nullptr), callbackState(nullptr), initMethod(nullptr),
  17. oneBit(1), typeSlots(0), hasAccessors(0), flags(0), deferredLength(0)
  18. {
  19. DebugOnly(VerifyEntryPoint());
  20. }
  21. JavascriptExternalFunction::JavascriptExternalFunction(ExternalMethod entryPoint, DynamicType* type, InitializeMethod method, unsigned short deferredSlotCount, bool accessors)
  22. : RuntimeFunction(type, &EntryInfo::ExternalFunctionThunk), nativeMethod(entryPoint), signature(nullptr), callbackState(nullptr), initMethod(method),
  23. oneBit(1), typeSlots(deferredSlotCount), hasAccessors(accessors), flags(0), deferredLength(0)
  24. {
  25. DebugOnly(VerifyEntryPoint());
  26. }
  27. JavascriptExternalFunction::JavascriptExternalFunction(DynamicType* type, InitializeMethod method, unsigned short deferredSlotCount, bool accessors)
  28. : RuntimeFunction(type, &EntryInfo::DefaultExternalFunctionThunk), nativeMethod(nullptr), signature(nullptr), callbackState(nullptr), initMethod(method),
  29. oneBit(1), typeSlots(deferredSlotCount), hasAccessors(accessors), flags(0), deferredLength(0)
  30. {
  31. DebugOnly(VerifyEntryPoint());
  32. }
  33. JavascriptExternalFunction::JavascriptExternalFunction(JavascriptExternalFunction* entryPoint, DynamicType* type)
  34. : RuntimeFunction(type, &EntryInfo::WrappedFunctionThunk), wrappedMethod(entryPoint), callbackState(nullptr), initMethod(nullptr),
  35. oneBit(1), typeSlots(0), hasAccessors(0), flags(0), deferredLength(0)
  36. {
  37. DebugOnly(VerifyEntryPoint());
  38. }
  39. JavascriptExternalFunction::JavascriptExternalFunction(StdCallJavascriptMethod entryPoint, DynamicType* type)
  40. : RuntimeFunction(type, &EntryInfo::StdCallExternalFunctionThunk), stdCallNativeMethod(entryPoint), signature(nullptr), callbackState(nullptr), initMethod(nullptr),
  41. oneBit(1), typeSlots(0), hasAccessors(0), flags(0), deferredLength(0)
  42. {
  43. DebugOnly(VerifyEntryPoint());
  44. }
  45. JavascriptExternalFunction::JavascriptExternalFunction(DynamicType *type)
  46. : RuntimeFunction(type, &EntryInfo::ExternalFunctionThunk), nativeMethod(nullptr), signature(nullptr), callbackState(nullptr), initMethod(nullptr),
  47. oneBit(1), typeSlots(0), hasAccessors(0), flags(0), deferredLength(0)
  48. {
  49. DebugOnly(VerifyEntryPoint());
  50. }
  51. bool __cdecl JavascriptExternalFunction::DeferredLengthInitializer(DynamicObject * instance, DeferredTypeHandlerBase * typeHandler, DeferredInitializeMode mode)
  52. {
  53. Js::JavascriptLibrary::InitializeFunction<true>(instance, typeHandler, mode);
  54. JavascriptExternalFunction* object = static_cast<JavascriptExternalFunction*>(instance);
  55. object->UndeferLength(instance->GetScriptContext());
  56. return true;
  57. }
  58. // Note: non-constructors will probably use JavascriptFunction::InitiailizeFunction for undeferral.
  59. bool __cdecl JavascriptExternalFunction::DeferredConstructorInitializer(DynamicObject* instance, DeferredTypeHandlerBase* typeHandler, DeferredInitializeMode mode)
  60. {
  61. JavascriptExternalFunction* object = static_cast<JavascriptExternalFunction*>(instance);
  62. HRESULT hr = E_FAIL;
  63. ScriptContext* scriptContext = object->GetScriptContext();
  64. AnalysisAssert(scriptContext);
  65. // Don't call the implicit call if disable implicit call
  66. if (scriptContext->GetThreadContext()->IsDisableImplicitCall())
  67. {
  68. scriptContext->GetThreadContext()->AddImplicitCallFlags(ImplicitCall_External);
  69. //we will return if we get call further into implicitcalls.
  70. return false;
  71. }
  72. if (scriptContext->IsClosed() || scriptContext->IsInvalidatedForHostObjects())
  73. {
  74. Js::JavascriptError::MapAndThrowError(scriptContext, E_ACCESSDENIED);
  75. }
  76. ThreadContext* threadContext = scriptContext->GetThreadContext();
  77. typeHandler->Convert(instance, mode, object->typeSlots, object->hasAccessors);
  78. BEGIN_LEAVE_SCRIPT_INTERNAL(scriptContext)
  79. {
  80. ASYNC_HOST_OPERATION_START(threadContext);
  81. hr = object->initMethod(instance);
  82. ASYNC_HOST_OPERATION_END(threadContext);
  83. }
  84. END_LEAVE_SCRIPT_INTERNAL(scriptContext);
  85. if (FAILED(hr))
  86. {
  87. Js::JavascriptError::MapAndThrowError(scriptContext, hr);
  88. }
  89. JavascriptString * functionName = nullptr;
  90. if (scriptContext->GetConfig()->IsES6FunctionNameEnabled() &&
  91. object->GetFunctionName(&functionName))
  92. {
  93. object->SetPropertyWithAttributes(PropertyIds::name, functionName, PropertyConfigurable, nullptr);
  94. }
  95. return true;
  96. }
  97. void JavascriptExternalFunction::PrepareExternalCall(Js::Arguments * args)
  98. {
  99. ScriptContext * scriptContext = this->type->GetScriptContext();
  100. Assert(!scriptContext->GetThreadContext()->IsDisableImplicitException());
  101. scriptContext->VerifyAlive();
  102. Assert(scriptContext->GetThreadContext()->IsScriptActive());
  103. if (args->Info.Count == 0)
  104. {
  105. JavascriptError::ThrowTypeError(scriptContext, JSERR_This_NullOrUndefined);
  106. }
  107. Var &thisVar = args->Values[0];
  108. Js::TypeId typeId = Js::JavascriptOperators::GetTypeId(thisVar);
  109. Js::RecyclableObject* directHostObject = nullptr;
  110. switch(typeId)
  111. {
  112. case TypeIds_Integer:
  113. #if FLOATVAR
  114. case TypeIds_Number:
  115. #endif // FLOATVAR
  116. Assert(!Js::RecyclableObject::Is(thisVar));
  117. break;
  118. default:
  119. {
  120. Assert(Js::RecyclableObject::Is(thisVar));
  121. ScriptContext* scriptContextThisVar = Js::RecyclableObject::FromVar(thisVar)->GetScriptContext();
  122. // We need to verify "this" pointer is active as well. The problem is that DOM prototype functions are
  123. // the same across multiple frames, and caller can do function.call(closedthis)
  124. Assert(!scriptContext->GetThreadContext()->IsDisableImplicitException());
  125. scriptContextThisVar->VerifyAlive();
  126. // translate direct host for fastDOM.
  127. switch(typeId)
  128. {
  129. case Js::TypeIds_GlobalObject:
  130. {
  131. Js::GlobalObject* srcGlobalObject = (Js::GlobalObject*)(void*)(thisVar);
  132. directHostObject = srcGlobalObject->GetDirectHostObject();
  133. // For jsrt, direct host object can be null. If thats the case don't change it.
  134. if (directHostObject != nullptr)
  135. {
  136. thisVar = directHostObject;
  137. }
  138. }
  139. break;
  140. case Js::TypeIds_Undefined:
  141. case Js::TypeIds_Null:
  142. {
  143. // Call to DOM function with this as "undefined" or "null"
  144. // This should be converted to Global object
  145. Js::GlobalObject* srcGlobalObject = scriptContextThisVar->GetGlobalObject() ;
  146. directHostObject = srcGlobalObject->GetDirectHostObject();
  147. // For jsrt, direct host object can be null. If thats the case don't change it.
  148. if (directHostObject != nullptr)
  149. {
  150. thisVar = directHostObject;
  151. }
  152. }
  153. break;
  154. }
  155. }
  156. break;
  157. }
  158. }
  159. Var JavascriptExternalFunction::ExternalFunctionThunk(RecyclableObject* function, CallInfo callInfo, ...)
  160. {
  161. ARGUMENTS(args, callInfo);
  162. JavascriptExternalFunction* externalFunction = static_cast<JavascriptExternalFunction*>(function);
  163. ScriptContext * scriptContext = externalFunction->type->GetScriptContext();
  164. #ifdef ENABLE_DIRECTCALL_TELEMETRY
  165. DirectCallTelemetry::AutoLogger logger(scriptContext, externalFunction, &args);
  166. #endif
  167. externalFunction->PrepareExternalCall(&args);
  168. #if ENABLE_TTD
  169. Var result = nullptr;
  170. if(scriptContext->ShouldPerformRecordOrReplayAction())
  171. {
  172. result = JavascriptExternalFunction::HandleRecordReplayExternalFunction_Thunk(externalFunction, callInfo, args, scriptContext);
  173. }
  174. else
  175. {
  176. BEGIN_LEAVE_SCRIPT_WITH_EXCEPTION(scriptContext)
  177. {
  178. // Don't do stack probe since BEGIN_LEAVE_SCRIPT_WITH_EXCEPTION does that for us already
  179. result = externalFunction->nativeMethod(function, callInfo, args.Values);
  180. }
  181. END_LEAVE_SCRIPT_WITH_EXCEPTION(scriptContext);
  182. }
  183. #else
  184. Var result = nullptr;
  185. BEGIN_LEAVE_SCRIPT_WITH_EXCEPTION(scriptContext)
  186. {
  187. // Don't do stack probe since BEGIN_LEAVE_SCRIPT_WITH_EXCEPTION does that for us already
  188. result = externalFunction->nativeMethod(function, callInfo, args.Values);
  189. }
  190. END_LEAVE_SCRIPT_WITH_EXCEPTION(scriptContext);
  191. #endif
  192. if (result == nullptr)
  193. {
  194. #pragma warning(push)
  195. #pragma warning(disable:6011) // scriptContext cannot be null here
  196. result = scriptContext->GetLibrary()->GetUndefined();
  197. #pragma warning(pop)
  198. }
  199. else
  200. {
  201. result = CrossSite::MarshalVar(scriptContext, result);
  202. }
  203. return result;
  204. }
  205. Var JavascriptExternalFunction::WrappedFunctionThunk(RecyclableObject* function, CallInfo callInfo, ...)
  206. {
  207. ARGUMENTS(args, callInfo);
  208. JavascriptExternalFunction* externalFunction = static_cast<JavascriptExternalFunction*>(function);
  209. ScriptContext* scriptContext = externalFunction->type->GetScriptContext();
  210. Assert(!scriptContext->GetThreadContext()->IsDisableImplicitException());
  211. scriptContext->VerifyAlive();
  212. Assert(scriptContext->GetThreadContext()->IsScriptActive());
  213. // Make sure the callee knows we are a wrapped function thunk
  214. args.Info.Flags = (Js::CallFlags) (((int32) args.Info.Flags) | CallFlags_Wrapped);
  215. // don't need to leave script here, ExternalFunctionThunk will
  216. Assert(externalFunction->wrappedMethod->GetFunctionInfo()->GetOriginalEntryPoint() == JavascriptExternalFunction::ExternalFunctionThunk);
  217. return JavascriptFunction::CallFunction<true>(externalFunction->wrappedMethod, externalFunction->wrappedMethod->GetEntryPoint(), args);
  218. }
  219. Var JavascriptExternalFunction::DefaultExternalFunctionThunk(RecyclableObject* function, CallInfo callInfo, ...)
  220. {
  221. TypeId typeId = function->GetTypeId();
  222. rtErrors err = typeId <= TypeIds_UndefinedOrNull ? JSERR_NeedObject : JSERR_NeedFunction;
  223. JavascriptError::ThrowTypeError(function->GetScriptContext(), err);
  224. }
  225. Var JavascriptExternalFunction::StdCallExternalFunctionThunk(RecyclableObject* function, CallInfo callInfo, ...)
  226. {
  227. ARGUMENTS(args, callInfo);
  228. JavascriptExternalFunction* externalFunction = static_cast<JavascriptExternalFunction*>(function);
  229. externalFunction->PrepareExternalCall(&args);
  230. ScriptContext * scriptContext = externalFunction->type->GetScriptContext();
  231. AnalysisAssert(scriptContext);
  232. if (args.Info.Count > USHORT_MAX)
  233. {
  234. // Due to compat reasons, stdcall external functions expect a ushort count of args.
  235. // To support more than this we will need a new API.
  236. Js::JavascriptError::ThrowTypeError(scriptContext, JSERR_ArgListTooLarge);
  237. }
  238. Var result = nullptr;
  239. Assert(callInfo.Count > 0);
  240. StdCallJavascriptMethodInfo info = {
  241. args[0],
  242. args.HasNewTarget() ? args.GetNewTarget() : args.IsNewCall() ? function : scriptContext->GetLibrary()->GetUndefined(),
  243. args.IsNewCall()
  244. };
  245. #if ENABLE_TTD
  246. if(scriptContext->ShouldPerformRecordOrReplayAction())
  247. {
  248. result = JavascriptExternalFunction::HandleRecordReplayExternalFunction_StdThunk(function, callInfo, args, scriptContext);
  249. }
  250. else
  251. {
  252. BEGIN_LEAVE_SCRIPT(scriptContext)
  253. {
  254. result = externalFunction->stdCallNativeMethod(function, args.Values, static_cast<USHORT>(args.Info.Count), &info, externalFunction->callbackState);
  255. }
  256. END_LEAVE_SCRIPT(scriptContext);
  257. }
  258. #else
  259. BEGIN_LEAVE_SCRIPT(scriptContext)
  260. {
  261. result = externalFunction->stdCallNativeMethod(function, args.Values, static_cast<USHORT>(args.Info.Count), &info, externalFunction->callbackState);
  262. }
  263. END_LEAVE_SCRIPT(scriptContext);
  264. #endif
  265. bool marshallingMayBeNeeded = false;
  266. if (result != nullptr)
  267. {
  268. marshallingMayBeNeeded = Js::RecyclableObject::Is(result);
  269. if (marshallingMayBeNeeded)
  270. {
  271. Js::RecyclableObject * obj = Js::RecyclableObject::FromVar(result);
  272. // For JSRT, we could get result marshalled in different context.
  273. bool isJSRT = scriptContext->GetThreadContext()->IsJSRT();
  274. marshallingMayBeNeeded = obj->GetScriptContext() != scriptContext;
  275. if (!isJSRT && marshallingMayBeNeeded)
  276. {
  277. Js::Throw::InternalError();
  278. }
  279. }
  280. }
  281. if (scriptContext->HasRecordedException())
  282. {
  283. bool considerPassingToDebugger = false;
  284. JavascriptExceptionObject* recordedException = scriptContext->GetAndClearRecordedException(&considerPassingToDebugger);
  285. if (recordedException != nullptr)
  286. {
  287. // If this is script termination, then throw ScriptAbortExceptio, else throw normal Exception object.
  288. if (recordedException == scriptContext->GetThreadContext()->GetPendingTerminatedErrorObject())
  289. {
  290. throw Js::ScriptAbortException();
  291. }
  292. else
  293. {
  294. JavascriptExceptionOperators::RethrowExceptionObject(recordedException, scriptContext, considerPassingToDebugger);
  295. }
  296. }
  297. }
  298. if (result == nullptr)
  299. {
  300. result = scriptContext->GetLibrary()->GetUndefined();
  301. }
  302. else if (marshallingMayBeNeeded)
  303. {
  304. result = CrossSite::MarshalVar(scriptContext, result);
  305. }
  306. return result;
  307. }
  308. BOOL JavascriptExternalFunction::SetLengthProperty(Var length)
  309. {
  310. return DynamicObject::SetPropertyWithAttributes(PropertyIds::length, length, PropertyConfigurable, NULL, PropertyOperation_None, SideEffects_None);
  311. }
  312. void JavascriptExternalFunction::UndeferLength(ScriptContext *scriptContext)
  313. {
  314. if (deferredLength > 0)
  315. {
  316. SetLengthProperty(Js::JavascriptNumber::ToVar(deferredLength, scriptContext));
  317. deferredLength = 0;
  318. }
  319. }
  320. #if ENABLE_TTD
  321. TTD::NSSnapObjects::SnapObjectType JavascriptExternalFunction::GetSnapTag_TTD() const
  322. {
  323. return TTD::NSSnapObjects::SnapObjectType::SnapExternalFunctionObject;
  324. }
  325. void JavascriptExternalFunction::ExtractSnapObjectDataInto(TTD::NSSnapObjects::SnapObject* objData, TTD::SlabAllocator& alloc)
  326. {
  327. TTD::TTDVar fnameId = TTD_CONVERT_JSVAR_TO_TTDVAR(this->functionNameId);
  328. TTD::NSSnapObjects::StdExtractSetKindSpecificInfo<TTD::TTDVar, TTD::NSSnapObjects::SnapObjectType::SnapExternalFunctionObject>(objData, fnameId);
  329. }
  330. Var JavascriptExternalFunction::HandleRecordReplayExternalFunction_Thunk(Js::JavascriptFunction* function, CallInfo& callInfo, Arguments& args, ScriptContext* scriptContext)
  331. {
  332. JavascriptExternalFunction* externalFunction = static_cast<JavascriptExternalFunction*>(function);
  333. Var result = nullptr;
  334. if(scriptContext->ShouldPerformReplayAction())
  335. {
  336. TTD::TTDNestingDepthAutoAdjuster logPopper(scriptContext->GetThreadContext());
  337. scriptContext->GetThreadContext()->TTDLog->ReplayExternalCallEvent(externalFunction, args, &result);
  338. }
  339. else
  340. {
  341. TTDAssert(scriptContext->ShouldPerformRecordAction(), "Check either record/replay before calling!!!");
  342. TTD::EventLog* elog = scriptContext->GetThreadContext()->TTDLog;
  343. TTD::TTDNestingDepthAutoAdjuster logPopper(scriptContext->GetThreadContext());
  344. TTD::NSLogEvents::EventLogEntry* callEvent = elog->RecordExternalCallEvent(externalFunction, scriptContext->GetThreadContext()->TTDRootNestingCount, args, false);
  345. BEGIN_LEAVE_SCRIPT_WITH_EXCEPTION(scriptContext)
  346. {
  347. // Don't do stack probe since BEGIN_LEAVE_SCRIPT_WITH_EXCEPTION does that for us already
  348. result = externalFunction->nativeMethod(function, callInfo, args.Values);
  349. }
  350. END_LEAVE_SCRIPT_WITH_EXCEPTION(scriptContext);
  351. //Exceptions should be prohibited so no need to do extra work
  352. elog->RecordExternalCallEvent_Complete(externalFunction, callEvent, result);
  353. }
  354. return result;
  355. }
  356. Var JavascriptExternalFunction::HandleRecordReplayExternalFunction_StdThunk(Js::RecyclableObject* function, CallInfo& callInfo, Arguments& args, ScriptContext* scriptContext)
  357. {
  358. JavascriptExternalFunction* externalFunction = static_cast<JavascriptExternalFunction*>(function);
  359. Var result = nullptr;
  360. if(scriptContext->ShouldPerformReplayAction())
  361. {
  362. TTD::TTDNestingDepthAutoAdjuster logPopper(scriptContext->GetThreadContext());
  363. scriptContext->GetThreadContext()->TTDLog->ReplayExternalCallEvent(externalFunction, args, &result);
  364. }
  365. else
  366. {
  367. if (args.Info.Count > USHORT_MAX)
  368. {
  369. // Due to compat reasons, stdcall external functions expect a ushort count of args.
  370. // To support more than this we will need a new API.
  371. Js::JavascriptError::ThrowTypeError(scriptContext, JSERR_ArgListTooLarge);
  372. }
  373. TTDAssert(scriptContext->ShouldPerformRecordAction(), "Check either record/replay before calling!!!");
  374. TTD::EventLog* elog = scriptContext->GetThreadContext()->TTDLog;
  375. TTD::TTDNestingDepthAutoAdjuster logPopper(scriptContext->GetThreadContext());
  376. TTD::NSLogEvents::EventLogEntry* callEvent = elog->RecordExternalCallEvent(externalFunction, scriptContext->GetThreadContext()->TTDRootNestingCount, args, true);
  377. StdCallJavascriptMethodInfo info = {
  378. args[0],
  379. args.HasNewTarget() ? args.GetNewTarget() : args.IsNewCall() ? function : scriptContext->GetLibrary()->GetUndefined(),
  380. args.IsNewCall()
  381. };
  382. BEGIN_LEAVE_SCRIPT(scriptContext)
  383. {
  384. result = externalFunction->stdCallNativeMethod(function, args.Values, static_cast<ushort>(args.Info.Count), &info, externalFunction->callbackState);
  385. }
  386. END_LEAVE_SCRIPT(scriptContext);
  387. elog->RecordExternalCallEvent_Complete(externalFunction, callEvent, result);
  388. }
  389. return result;
  390. }
  391. Var __stdcall JavascriptExternalFunction::TTDReplayDummyExternalMethod(Var callee, Var *args, USHORT cargs, StdCallJavascriptMethodInfo *info, void *callbackState)
  392. {
  393. JavascriptExternalFunction* externalFunction = static_cast<JavascriptExternalFunction*>(callee);
  394. ScriptContext* scriptContext = externalFunction->type->GetScriptContext();
  395. TTD::EventLog* elog = scriptContext->GetThreadContext()->TTDLog;
  396. TTDAssert(elog != nullptr, "How did this get created then???");
  397. //If this flag is set then this is ok (the debugger may be evaluating this so just return undef -- otherwise this is an error
  398. if(!elog->IsDebugModeFlagSet())
  399. {
  400. TTDAssert(false, "This should never be reached in pure replay mode!!!");
  401. return nullptr;
  402. }
  403. return scriptContext->GetLibrary()->GetUndefined();
  404. }
  405. #endif
  406. }