JavascriptExternalFunction.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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), prototypeTypeId(-1), flags(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),prototypeTypeId(-1), flags(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), prototypeTypeId(-1), flags(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), prototypeTypeId(-1), flags(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), prototypeTypeId(-1), flags(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), prototypeTypeId(-1), flags(0)
  48. {
  49. DebugOnly(VerifyEntryPoint());
  50. }
  51. void __cdecl JavascriptExternalFunction::DeferredInitializer(DynamicObject* instance, DeferredTypeHandlerBase* typeHandler, DeferredInitializeMode mode)
  52. {
  53. JavascriptExternalFunction* object = static_cast<JavascriptExternalFunction*>(instance);
  54. HRESULT hr = E_FAIL;
  55. ScriptContext* scriptContext = object->GetScriptContext();
  56. AnalysisAssert(scriptContext);
  57. // Don't call the implicit call if disable implicit call
  58. if (scriptContext->GetThreadContext()->IsDisableImplicitCall())
  59. {
  60. scriptContext->GetThreadContext()->AddImplicitCallFlags(ImplicitCall_External);
  61. //we will return if we get call further into implicitcalls.
  62. return;
  63. }
  64. if (scriptContext->IsClosed() || scriptContext->IsInvalidatedForHostObjects())
  65. {
  66. Js::JavascriptError::MapAndThrowError(scriptContext, E_ACCESSDENIED);
  67. }
  68. ThreadContext* threadContext = scriptContext->GetThreadContext();
  69. typeHandler->Convert(instance, mode, object->typeSlots, object->hasAccessors);
  70. BEGIN_LEAVE_SCRIPT_INTERNAL(scriptContext)
  71. {
  72. ASYNC_HOST_OPERATION_START(threadContext);
  73. hr = object->initMethod(instance);
  74. ASYNC_HOST_OPERATION_END(threadContext);
  75. }
  76. END_LEAVE_SCRIPT_INTERNAL(scriptContext);
  77. if (FAILED(hr))
  78. {
  79. Js::JavascriptError::MapAndThrowError(scriptContext, hr);
  80. }
  81. JavascriptString * functionName = nullptr;
  82. if (scriptContext->GetConfig()->IsES6FunctionNameEnabled() &&
  83. object->GetFunctionName(&functionName))
  84. {
  85. object->SetPropertyWithAttributes(PropertyIds::name, functionName, PropertyConfigurable, nullptr);
  86. }
  87. }
  88. void JavascriptExternalFunction::PrepareExternalCall(Js::Arguments * args)
  89. {
  90. ScriptContext * scriptContext = this->type->GetScriptContext();
  91. Assert(!scriptContext->GetThreadContext()->IsDisableImplicitException());
  92. scriptContext->VerifyAlive();
  93. Assert(scriptContext->GetThreadContext()->IsScriptActive());
  94. if (args->Info.Count == 0)
  95. {
  96. JavascriptError::ThrowTypeError(scriptContext, JSERR_This_NullOrUndefined);
  97. }
  98. Var &thisVar = args->Values[0];
  99. Js::TypeId typeId = Js::JavascriptOperators::GetTypeId(thisVar);
  100. Js::RecyclableObject* directHostObject = nullptr;
  101. switch(typeId)
  102. {
  103. case TypeIds_Integer:
  104. #if FLOATVAR
  105. case TypeIds_Number:
  106. #endif // FLOATVAR
  107. Assert(!Js::RecyclableObject::Is(thisVar));
  108. break;
  109. default:
  110. {
  111. Assert(Js::RecyclableObject::Is(thisVar));
  112. ScriptContext* scriptContextThisVar = Js::RecyclableObject::FromVar(thisVar)->GetScriptContext();
  113. // We need to verify "this" pointer is active as well. The problem is that DOM prototype functions are
  114. // the same across multiple frames, and caller can do function.call(closedthis)
  115. Assert(!scriptContext->GetThreadContext()->IsDisableImplicitException());
  116. scriptContextThisVar->VerifyAlive();
  117. // translate direct host for fastDOM.
  118. switch(typeId)
  119. {
  120. case Js::TypeIds_GlobalObject:
  121. {
  122. Js::GlobalObject* srcGlobalObject = static_cast<Js::GlobalObject*>(thisVar);
  123. directHostObject = srcGlobalObject->GetDirectHostObject();
  124. // For jsrt, direct host object can be null. If thats the case don't change it.
  125. if (directHostObject != nullptr)
  126. {
  127. thisVar = directHostObject;
  128. }
  129. }
  130. break;
  131. case Js::TypeIds_Undefined:
  132. case Js::TypeIds_Null:
  133. {
  134. // Call to DOM function with this as "undefined" or "null"
  135. // This should be converted to Global object
  136. Js::GlobalObject* srcGlobalObject = scriptContextThisVar->GetGlobalObject() ;
  137. directHostObject = srcGlobalObject->GetDirectHostObject();
  138. // For jsrt, direct host object can be null. If thats the case don't change it.
  139. if (directHostObject != nullptr)
  140. {
  141. thisVar = directHostObject;
  142. }
  143. }
  144. break;
  145. }
  146. }
  147. break;
  148. }
  149. }
  150. Var JavascriptExternalFunction::ExternalFunctionThunk(RecyclableObject* function, CallInfo callInfo, ...)
  151. {
  152. ARGUMENTS(args, callInfo);
  153. JavascriptExternalFunction* externalFunction = static_cast<JavascriptExternalFunction*>(function);
  154. ScriptContext * scriptContext = externalFunction->type->GetScriptContext();
  155. #ifdef ENABLE_DIRECTCALL_TELEMETRY
  156. DirectCallTelemetry::AutoLogger logger(scriptContext, externalFunction, &args);
  157. #endif
  158. externalFunction->PrepareExternalCall(&args);
  159. #if ENABLE_TTD
  160. Var result = nullptr;
  161. //
  162. //TODO: This may be a hot path so we may want to reduce the number of checks here and perhaps create a special TTD external function so only record code is needed here (see also in StdCallExternalFunctionThunk below).
  163. //
  164. if(scriptContext->ShouldPerformDebugAction())
  165. {
  166. TTD::TTDReplayExternalFunctionCallActionPopper logPopper(externalFunction);
  167. scriptContext->GetThreadContext()->TTDLog->ReplayExternalCallEvent(externalFunction, args.Info.Count, args.Values, &result);
  168. }
  169. else if(scriptContext->ShouldPerformRecordAction())
  170. {
  171. //Root nesting depth handled in logPopper constructor, destructor, and Normal return paths -- the increment of nesting is handled by the popper but we need to add 1 to the value we record (so it matches)
  172. TTD::NSLogEvents::EventLogEntry* callEvent = scriptContext->GetThreadContext()->TTDLog->RecordExternalCallEvent(externalFunction, scriptContext->TTDRootNestingCount + 1, args.Info.Count, args.Values);
  173. TTD::TTDRecordExternalFunctionCallActionPopper logPopper(externalFunction, callEvent);
  174. BEGIN_LEAVE_SCRIPT_WITH_EXCEPTION(scriptContext)
  175. {
  176. // Don't do stack probe since BEGIN_LEAVE_SCRIPT_WITH_EXCEPTION does that for us already
  177. result = externalFunction->nativeMethod(function, callInfo, args.Values);
  178. }
  179. END_LEAVE_SCRIPT_WITH_EXCEPTION(scriptContext);
  180. //no exception check below so I assume the external call cannot have an exception registered
  181. logPopper.NormalReturn(false, result);
  182. }
  183. else
  184. {
  185. if(externalFunction->nativeMethod == nullptr)
  186. {
  187. //The only way this should happen is if the debugger is requesting a value to display that is an external accessor
  188. //or the debugger is running something that can fail (and it is ok with that).
  189. result = function->GetScriptContext()->GetLibrary()->GetUndefined();
  190. }
  191. else
  192. {
  193. BEGIN_LEAVE_SCRIPT_WITH_EXCEPTION(scriptContext)
  194. {
  195. // Don't do stack probe since BEGIN_LEAVE_SCRIPT_WITH_EXCEPTION does that for us already
  196. result = externalFunction->nativeMethod(function, callInfo, args.Values);
  197. }
  198. END_LEAVE_SCRIPT_WITH_EXCEPTION(scriptContext);
  199. }
  200. }
  201. #else
  202. Var result = nullptr;
  203. BEGIN_LEAVE_SCRIPT_WITH_EXCEPTION(scriptContext)
  204. {
  205. // Don't do stack probe since BEGIN_LEAVE_SCRIPT_WITH_EXCEPTION does that for us already
  206. result = externalFunction->nativeMethod(function, callInfo, args.Values);
  207. }
  208. END_LEAVE_SCRIPT_WITH_EXCEPTION(scriptContext);
  209. #endif
  210. if (result == nullptr)
  211. {
  212. #pragma warning(push)
  213. #pragma warning(disable:6011) // scriptContext cannot be null here
  214. result = scriptContext->GetLibrary()->GetUndefined();
  215. #pragma warning(pop)
  216. }
  217. else
  218. {
  219. result = CrossSite::MarshalVar(scriptContext, result);
  220. }
  221. return result;
  222. }
  223. Var JavascriptExternalFunction::WrappedFunctionThunk(RecyclableObject* function, CallInfo callInfo, ...)
  224. {
  225. ARGUMENTS(args, callInfo);
  226. JavascriptExternalFunction* externalFunction = static_cast<JavascriptExternalFunction*>(function);
  227. ScriptContext* scriptContext = externalFunction->type->GetScriptContext();
  228. Assert(!scriptContext->GetThreadContext()->IsDisableImplicitException());
  229. scriptContext->VerifyAlive();
  230. Assert(scriptContext->GetThreadContext()->IsScriptActive());
  231. // Make sure the callee knows we are a wrapped function thunk
  232. args.Info.Flags = (Js::CallFlags) (((int32) args.Info.Flags) | CallFlags_Wrapped);
  233. // don't need to leave script here, ExternalFunctionThunk will
  234. Assert(externalFunction->wrappedMethod->GetFunctionInfo()->GetOriginalEntryPoint() == JavascriptExternalFunction::ExternalFunctionThunk);
  235. return JavascriptFunction::CallFunction<true>(externalFunction->wrappedMethod, externalFunction->wrappedMethod->GetEntryPoint(), args);
  236. }
  237. Var JavascriptExternalFunction::DefaultExternalFunctionThunk(RecyclableObject* function, CallInfo callInfo, ...)
  238. {
  239. TypeId typeId = function->GetTypeId();
  240. rtErrors err = typeId == TypeIds_Undefined || typeId == TypeIds_Null ? JSERR_NeedObject : JSERR_NeedFunction;
  241. JavascriptError::ThrowTypeError(function->GetScriptContext(), err);
  242. }
  243. Var JavascriptExternalFunction::StdCallExternalFunctionThunk(RecyclableObject* function, CallInfo callInfo, ...)
  244. {
  245. ARGUMENTS(args, callInfo);
  246. JavascriptExternalFunction* externalFunction = static_cast<JavascriptExternalFunction*>(function);
  247. externalFunction->PrepareExternalCall(&args);
  248. ScriptContext * scriptContext = externalFunction->type->GetScriptContext();
  249. AnalysisAssert(scriptContext);
  250. Var result = NULL;
  251. #if ENABLE_TTD
  252. if(scriptContext->ShouldPerformDebugAction())
  253. {
  254. TTD::TTDReplayExternalFunctionCallActionPopper logPopper(externalFunction);
  255. scriptContext->GetThreadContext()->TTDLog->ReplayExternalCallEvent(externalFunction, args.Info.Count, args.Values, &result);
  256. }
  257. else if(scriptContext->ShouldPerformRecordAction())
  258. {
  259. //Root nesting depth handled in logPopper constructor, destructor, and Normal return paths -- the increment of nesting is handled by the popper but we need to add 1 to the value we record (so it matches)
  260. TTD::NSLogEvents::EventLogEntry* callEvent = scriptContext->GetThreadContext()->TTDLog->RecordExternalCallEvent(externalFunction, scriptContext->TTDRootNestingCount + 1, args.Info.Count, args.Values);
  261. TTD::TTDRecordExternalFunctionCallActionPopper logPopper(externalFunction, callEvent);
  262. BEGIN_LEAVE_SCRIPT(scriptContext)
  263. {
  264. result = externalFunction->stdCallNativeMethod(function, ((callInfo.Flags & CallFlags_New) != 0), args.Values, args.Info.Count, externalFunction->callbackState);
  265. }
  266. END_LEAVE_SCRIPT(scriptContext);
  267. //exception check is done explicitly below call can have an exception registered
  268. logPopper.NormalReturn(true, result);
  269. }
  270. else
  271. {
  272. BEGIN_LEAVE_SCRIPT(scriptContext)
  273. {
  274. result = externalFunction->stdCallNativeMethod(function, ((callInfo.Flags & CallFlags_New) != 0), args.Values, args.Info.Count, externalFunction->callbackState);
  275. }
  276. END_LEAVE_SCRIPT(scriptContext);
  277. }
  278. #else
  279. BEGIN_LEAVE_SCRIPT(scriptContext)
  280. {
  281. result = externalFunction->stdCallNativeMethod(function, ((callInfo.Flags & CallFlags_New) != 0), args.Values, args.Info.Count, externalFunction->callbackState);
  282. }
  283. END_LEAVE_SCRIPT(scriptContext);
  284. #endif
  285. if (result != nullptr && !Js::TaggedNumber::Is(result))
  286. {
  287. if (!Js::RecyclableObject::Is(result))
  288. {
  289. Js::Throw::InternalError();
  290. }
  291. Js::RecyclableObject * obj = Js::RecyclableObject::FromVar(result);
  292. // For JSRT, we could get result marshalled in different context.
  293. bool isJSRT = scriptContext->GetThreadContext()->IsJSRT();
  294. if (!isJSRT && obj->GetScriptContext() != scriptContext)
  295. {
  296. Js::Throw::InternalError();
  297. }
  298. }
  299. if (scriptContext->HasRecordedException())
  300. {
  301. bool considerPassingToDebugger = false;
  302. JavascriptExceptionObject* recordedException = scriptContext->GetAndClearRecordedException(&considerPassingToDebugger);
  303. if (recordedException != nullptr)
  304. {
  305. // If this is script termination, then throw ScriptAbortExceptio, else throw normal Exception object.
  306. if (recordedException == scriptContext->GetThreadContext()->GetPendingTerminatedErrorObject())
  307. {
  308. throw Js::ScriptAbortException();
  309. }
  310. else
  311. {
  312. JavascriptExceptionOperators::RethrowExceptionObject(recordedException, scriptContext, considerPassingToDebugger);
  313. }
  314. }
  315. }
  316. if (result == nullptr)
  317. {
  318. result = scriptContext->GetLibrary()->GetUndefined();
  319. }
  320. else
  321. {
  322. result = CrossSite::MarshalVar(scriptContext, result);
  323. }
  324. return result;
  325. }
  326. BOOL JavascriptExternalFunction::SetLengthProperty(Var length)
  327. {
  328. return DynamicObject::SetPropertyWithAttributes(PropertyIds::length, length, PropertyConfigurable, NULL, PropertyOperation_None, SideEffects_None);
  329. }
  330. #if ENABLE_TTD
  331. TTD::NSSnapObjects::SnapObjectType JavascriptExternalFunction::GetSnapTag_TTD() const
  332. {
  333. return TTD::NSSnapObjects::SnapObjectType::SnapExternalFunctionObject;
  334. }
  335. void JavascriptExternalFunction::ExtractSnapObjectDataInto(TTD::NSSnapObjects::SnapObject* objData, TTD::SlabAllocator& alloc)
  336. {
  337. Js::JavascriptString* nameString = this->GetDisplayName();
  338. TTD::TTString* snapName = alloc.SlabAllocateStruct<TTD::TTString>();
  339. alloc.CopyStringIntoWLength(nameString->GetSz(), nameString->GetLength(), *snapName);
  340. TTD::NSSnapObjects::StdExtractSetKindSpecificInfo<TTD::TTString*, TTD::NSSnapObjects::SnapObjectType::SnapExternalFunctionObject>(objData, snapName);
  341. }
  342. #endif
  343. }