CrossSite.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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 "RuntimeBasePch.h"
  6. #include "Library/JavascriptProxy.h"
  7. #include "Library/HostObjectBase.h"
  8. #include "Types/WithScopeObject.h"
  9. #if ENABLE_CROSSSITE_TRACE
  10. #define TTD_XSITE_LOG(CTX, MSG, VAR) if((CTX)->ShouldPerformRecordOrReplayAction()) \
  11. { \
  12. (CTX)->GetThreadContext()->TTDExecutionInfo->GetTraceLogger()->WriteLiteralMsg(" -XS- "); \
  13. (CTX)->GetThreadContext()->TTDExecutionInfo->GetTraceLogger()->WriteLiteralMsg(MSG); \
  14. (CTX)->GetThreadContext()->TTDExecutionInfo->GetTraceLogger()->WriteVar(VAR); \
  15. (CTX)->GetThreadContext()->TTDExecutionInfo->GetTraceLogger()->WriteLiteralMsg("\n"); \
  16. }
  17. #else
  18. #define TTD_XSITE_LOG(CTX, MSG, VAR)
  19. #endif
  20. namespace Js
  21. {
  22. BOOL CrossSite::NeedMarshalVar(Var instance, ScriptContext * requestContext)
  23. {
  24. if (TaggedNumber::Is(instance))
  25. {
  26. return FALSE;
  27. }
  28. RecyclableObject * object = RecyclableObject::FromVar(instance);
  29. if (object->GetScriptContext() == requestContext)
  30. {
  31. return FALSE;
  32. }
  33. if (DynamicType::Is(object->GetTypeId()))
  34. {
  35. return !DynamicObject::FromVar(object)->IsCrossSiteObject() && !object->IsExternal();
  36. }
  37. return TRUE;
  38. }
  39. void CrossSite::MarshalDynamicObject(ScriptContext * scriptContext, DynamicObject * object)
  40. {
  41. Assert(!object->IsExternal() && !object->IsCrossSiteObject());
  42. TTD_XSITE_LOG(scriptContext, "MarshalDynamicObject", object);
  43. object->MarshalToScriptContext(scriptContext);
  44. if (object->GetTypeId() == TypeIds_Function)
  45. {
  46. AssertMsg(object != object->GetScriptContext()->GetLibrary()->GetDefaultAccessorFunction(), "default accessor marshalled");
  47. JavascriptFunction * function = JavascriptFunction::FromVar(object);
  48. //TODO: this may be too aggressive and create x-site thunks that are't technically needed -- see uglify-2js test.
  49. // See if this function is one that the host needs to handle
  50. HostScriptContext * hostScriptContext = scriptContext->GetHostScriptContext();
  51. if (!hostScriptContext || !hostScriptContext->SetCrossSiteForFunctionType(function))
  52. {
  53. if (function->GetDynamicType()->GetIsShared())
  54. {
  55. TTD_XSITE_LOG(scriptContext, "SetCrossSiteForSharedFunctionType ", object);
  56. function->GetLibrary()->SetCrossSiteForSharedFunctionType(function);
  57. }
  58. else
  59. {
  60. TTD_XSITE_LOG(scriptContext, "setEntryPoint->CurrentCrossSiteThunk ", object);
  61. function->SetEntryPoint(function->GetScriptContext()->CurrentCrossSiteThunk);
  62. }
  63. }
  64. }
  65. else if (object->GetTypeId() == TypeIds_Proxy)
  66. {
  67. RecyclableObject * target = JavascriptProxy::FromVar(object)->GetTarget();
  68. if (JavascriptConversion::IsCallable(target))
  69. {
  70. Assert(JavascriptProxy::FunctionCallTrap == object->GetEntryPoint());
  71. TTD_XSITE_LOG(scriptContext, "setEntryPoint->CrossSiteProxyCallTrap ", object);
  72. object->GetDynamicType()->SetEntryPoint(CrossSite::CrossSiteProxyCallTrap);
  73. }
  74. }
  75. }
  76. void CrossSite::MarshalPrototypeChain(ScriptContext* scriptContext, DynamicObject * object)
  77. {
  78. RecyclableObject * prototype = object->GetPrototype();
  79. while (prototype->GetTypeId() != TypeIds_Null && prototype->GetTypeId() != TypeIds_HostDispatch)
  80. {
  81. // We should not see any static type or host dispatch here
  82. DynamicObject * prototypeObject = DynamicObject::FromVar(prototype);
  83. if (prototypeObject->IsCrossSiteObject())
  84. {
  85. break;
  86. }
  87. if (scriptContext != prototypeObject->GetScriptContext() && !prototypeObject->IsExternal())
  88. {
  89. MarshalDynamicObject(scriptContext, prototypeObject);
  90. }
  91. prototype = prototypeObject->GetPrototype();
  92. }
  93. }
  94. void CrossSite::MarshalDynamicObjectAndPrototype(ScriptContext* scriptContext, DynamicObject * object)
  95. {
  96. MarshalDynamicObject(scriptContext, object);
  97. MarshalPrototypeChain(scriptContext, object);
  98. }
  99. Var CrossSite::MarshalFrameDisplay(ScriptContext* scriptContext, FrameDisplay *display)
  100. {
  101. TTD_XSITE_LOG(scriptContext, "MarshalFrameDisplay", nullptr);
  102. uint16 length = display->GetLength();
  103. FrameDisplay *newDisplay =
  104. RecyclerNewPlus(scriptContext->GetRecycler(), length * sizeof(Var), FrameDisplay, length);
  105. for (uint16 i = 0; i < length; i++)
  106. {
  107. Var value = display->GetItem(i);
  108. if (WithScopeObject::Is(value))
  109. {
  110. // Here we are marshalling the wrappedObject and then ReWrapping th object in the new context.
  111. RecyclableObject* wrappedObject = WithScopeObject::FromVar(value)->GetWrappedObject();
  112. ScriptContext* wrappedObjectScriptContext = wrappedObject->GetScriptContext();
  113. value = JavascriptOperators::ToWithObject(CrossSite::MarshalVar(scriptContext,
  114. wrappedObject, wrappedObjectScriptContext), scriptContext);
  115. }
  116. else
  117. {
  118. value = CrossSite::MarshalVar(scriptContext, value);
  119. }
  120. newDisplay->SetItem(i, value);
  121. }
  122. return (Var)newDisplay;
  123. }
  124. // static
  125. Var CrossSite::MarshalVar(ScriptContext* scriptContext, Var value, ScriptContext* objectScriptContext)
  126. {
  127. if (scriptContext != objectScriptContext)
  128. {
  129. if (value == nullptr || Js::TaggedNumber::Is(value))
  130. {
  131. return value;
  132. }
  133. return MarshalVarInner(scriptContext, RecyclableObject::FromVar(value), false);
  134. }
  135. return value;
  136. }
  137. // static
  138. Var CrossSite::MarshalVar(ScriptContext* scriptContext, Var value, bool fRequestWrapper)
  139. {
  140. // value might be null from disable implicit call
  141. if (value == nullptr || Js::TaggedNumber::Is(value))
  142. {
  143. return value;
  144. }
  145. Js::RecyclableObject* object = RecyclableObject::FromVar(value);
  146. if (fRequestWrapper || scriptContext != object->GetScriptContext())
  147. {
  148. return MarshalVarInner(scriptContext, object, fRequestWrapper);
  149. }
  150. return value;
  151. }
  152. bool CrossSite::DoRequestWrapper(Js::RecyclableObject* object, bool fRequestWrapper)
  153. {
  154. return fRequestWrapper && JavascriptFunction::Is(object) && JavascriptFunction::FromVar(object)->IsExternalFunction();
  155. }
  156. #if ENABLE_TTD
  157. void CrossSite::MarshalCrossSite_TTDInflate(DynamicObject* obj)
  158. {
  159. obj->MarshalCrossSite_TTDInflate();
  160. if(obj->GetTypeId() == TypeIds_Function)
  161. {
  162. AssertMsg(obj != obj->GetScriptContext()->GetLibrary()->GetDefaultAccessorFunction(), "default accessor marshalled -- I don't think this should ever happen as it is marshalled in a special case?");
  163. JavascriptFunction * function = JavascriptFunction::FromVar(obj);
  164. //
  165. //TODO: what happens if the gaurd in marshal (MarshalDynamicObject) isn't true?
  166. //
  167. if(function->GetDynamicType()->GetIsShared())
  168. {
  169. function->GetLibrary()->SetCrossSiteForSharedFunctionType(function);
  170. }
  171. else
  172. {
  173. function->SetEntryPoint(function->GetScriptContext()->CurrentCrossSiteThunk);
  174. }
  175. }
  176. }
  177. #endif
  178. Var CrossSite::MarshalVarInner(ScriptContext* scriptContext, __in Js::RecyclableObject* object, bool fRequestWrapper)
  179. {
  180. if (scriptContext == object->GetScriptContext())
  181. {
  182. if (DoRequestWrapper(object, fRequestWrapper))
  183. {
  184. // If we get here then we need to either wrap in the caller's type system or we need to return undefined.
  185. // VBScript will pass in the scriptContext (requestContext) from the JavascriptDispatch and this will be the
  186. // same as the object's script context and so we have to safely pretend this value doesn't exist.
  187. return scriptContext->GetLibrary()->GetUndefined();
  188. }
  189. return object;
  190. }
  191. AssertMsg(scriptContext->GetThreadContext() == object->GetScriptContext()->GetThreadContext(), "ScriptContexts should belong to same threadcontext for marshalling.");
  192. // In heapenum, we are traversing through the object graph to dump out the content of recyclable objects. The content
  193. // of the objects are duplicated to the heapenum result, and we are not storing/changing the object graph during heap enum.
  194. // We don't actually need to do cross site thunk here.
  195. if (scriptContext->GetRecycler()->IsHeapEnumInProgress())
  196. {
  197. return object;
  198. }
  199. #if ENABLE_TTD
  200. if (scriptContext->IsTTDSnapshotOrInflateInProgress())
  201. {
  202. return object;
  203. }
  204. #endif
  205. #if ENABLE_COPYONACCESS_ARRAY
  206. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(object);
  207. #endif
  208. TypeId typeId = object->GetTypeId();
  209. AssertMsg(typeId != TypeIds_Enumerator, "enumerator shouldn't be marshalled here");
  210. // At the moment the mental model for WithScopeObject Marshaling is this:
  211. // Are we trying to marshal a WithScopeObject in the Frame Display? - then 1) unwrap in MarshalFrameDisplay,
  212. // 2) marshal the wrapped object, 3) Create a new WithScopeObject in the current scriptContext and re-wrap.
  213. // We can avoid copying the WithScopeObject because it has no properties and never should.
  214. // Thus creating a new WithScopeObject per context in MarshalFrameDisplay should be kosher.
  215. // If it is not a FrameDisplay then we should not marshal. We can wrap cross context objects with a
  216. // withscopeObject in a different context. When we unwrap for property lookups and the wrapped object
  217. // is cross context, then we marshal the wrapped object into the current scriptContext, thus avoiding
  218. // the need to copy the WithScopeObject itself. Thus We don't have to handle marshaling the WithScopeObject
  219. // in non-FrameDisplay cases.
  220. AssertMsg(typeId != TypeIds_WithScopeObject, "WithScopeObject shouldn't be marshalled here");
  221. if (StaticType::Is(typeId))
  222. {
  223. TTD_XSITE_LOG(object->GetScriptContext(), "CloneToScriptContext", object);
  224. return object->CloneToScriptContext(scriptContext);
  225. }
  226. if (typeId == TypeIds_ModuleRoot)
  227. {
  228. RootObjectBase *moduleRoot = static_cast<RootObjectBase*>(object);
  229. HostObjectBase * hostObject = moduleRoot->GetHostObject();
  230. // When marshaling module root, all we need is the host object.
  231. // So, if the module root which is being marshaled has host object, marshal it.
  232. if (hostObject)
  233. {
  234. TTD_XSITE_LOG(object->GetScriptContext(), "hostObject", hostObject);
  235. Var hostDispatch = hostObject->GetHostDispatchVar();
  236. return CrossSite::MarshalVar(scriptContext, hostDispatch);
  237. }
  238. }
  239. if (typeId == TypeIds_Function)
  240. {
  241. if (object == object->GetScriptContext()->GetLibrary()->GetDefaultAccessorFunction() )
  242. {
  243. TTD_XSITE_LOG(object->GetScriptContext(), "DefaultAccessorFunction", object);
  244. return scriptContext->GetLibrary()->GetDefaultAccessorFunction();
  245. }
  246. if (DoRequestWrapper(object, fRequestWrapper))
  247. {
  248. TTD_XSITE_LOG(object->GetScriptContext(), "CreateWrappedExternalFunction", object);
  249. // Marshal as a cross-site thunk if necessary before re-wrapping in an external function thunk.
  250. MarshalVarInner(scriptContext, object, false);
  251. return scriptContext->GetLibrary()->CreateWrappedExternalFunction(static_cast<JavascriptExternalFunction*>(object));
  252. }
  253. }
  254. // We have an object marshaled, we need to keep track of the related script context
  255. // so optimization overrides can be updated as a group
  256. scriptContext->optimizationOverrides.Merge(&object->GetScriptContext()->optimizationOverrides);
  257. DynamicObject * dynamicObject = DynamicObject::FromVar(object);
  258. if (!dynamicObject->IsExternal())
  259. {
  260. if (!dynamicObject->IsCrossSiteObject())
  261. {
  262. TTD_XSITE_LOG(object->GetScriptContext(), "MarshalDynamicObjectAndPrototype", object);
  263. MarshalDynamicObjectAndPrototype(scriptContext, dynamicObject);
  264. }
  265. }
  266. else
  267. {
  268. MarshalPrototypeChain(scriptContext, dynamicObject);
  269. if (Js::JavascriptConversion::IsCallable(dynamicObject))
  270. {
  271. TTD_XSITE_LOG(object->GetScriptContext(), "MarshalToScriptContext", object);
  272. dynamicObject->MarshalToScriptContext(scriptContext);
  273. }
  274. }
  275. return dynamicObject;
  276. }
  277. bool CrossSite::IsThunk(JavascriptMethod thunk)
  278. {
  279. #if defined(ENABLE_SCRIPT_PROFILING) || defined(ENABLE_SCRIPT_DEBUGGING)
  280. return (thunk == CrossSite::ProfileThunk || thunk == CrossSite::DefaultThunk);
  281. #else
  282. return (thunk == CrossSite::DefaultThunk);
  283. #endif
  284. }
  285. #if defined(ENABLE_SCRIPT_PROFILING) || defined(ENABLE_SCRIPT_DEBUGGING)
  286. Var CrossSite::ProfileThunk(RecyclableObject* callable, CallInfo callInfo, ...)
  287. {
  288. JavascriptFunction* function = JavascriptFunction::FromVar(callable);
  289. Assert(function->GetTypeId() == TypeIds_Function);
  290. Assert(function->GetEntryPoint() == CrossSite::ProfileThunk);
  291. RUNTIME_ARGUMENTS(args, callInfo);
  292. ScriptContext * scriptContext = function->GetScriptContext();
  293. // It is not safe to access the function body if the script context is not alive.
  294. scriptContext->VerifyAliveWithHostContext(!function->IsExternal(),
  295. scriptContext->GetThreadContext()->GetPreviousHostScriptContext());
  296. JavascriptMethod entryPoint;
  297. FunctionInfo *funcInfo = function->GetFunctionInfo();
  298. TTD_XSITE_LOG(callable->GetScriptContext(), "DefaultOrProfileThunk", callable);
  299. #ifdef ENABLE_WASM
  300. if (WasmScriptFunction::Is(function))
  301. {
  302. AsmJsFunctionInfo* asmInfo = funcInfo->GetFunctionBody()->GetAsmJsFunctionInfo();
  303. Assert(asmInfo);
  304. if (asmInfo->IsWasmDeferredParse())
  305. {
  306. entryPoint = WasmLibrary::WasmDeferredParseExternalThunk;
  307. }
  308. else
  309. {
  310. entryPoint = Js::AsmJsExternalEntryPoint;
  311. }
  312. } else
  313. #endif
  314. if (funcInfo->HasBody())
  315. {
  316. #if ENABLE_DEBUG_CONFIG_OPTIONS
  317. char16 debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  318. #endif
  319. entryPoint = ScriptFunction::FromVar(function)->GetEntryPointInfo()->jsMethod;
  320. if (funcInfo->IsDeferred() && scriptContext->IsProfiling())
  321. {
  322. // if the current entrypoint is deferred parse we need to update it appropriately for the profiler mode.
  323. entryPoint = Js::ScriptContext::GetProfileModeThunk(entryPoint);
  324. }
  325. OUTPUT_TRACE(Js::ScriptProfilerPhase, _u("CrossSite::ProfileThunk FunctionNumber : %s, Entrypoint : 0x%08X\n"), funcInfo->GetFunctionProxy()->GetDebugNumberSet(debugStringBuffer), entryPoint);
  326. }
  327. else
  328. {
  329. entryPoint = ProfileEntryThunk;
  330. }
  331. return CommonThunk(function, entryPoint, args);
  332. }
  333. #endif
  334. Var CrossSite::DefaultThunk(RecyclableObject* callable, CallInfo callInfo, ...)
  335. {
  336. JavascriptFunction* function = JavascriptFunction::FromVar(callable);
  337. Assert(function->GetTypeId() == TypeIds_Function);
  338. Assert(function->GetEntryPoint() == CrossSite::DefaultThunk);
  339. RUNTIME_ARGUMENTS(args, callInfo);
  340. // It is not safe to access the function body if the script context is not alive.
  341. function->GetScriptContext()->VerifyAliveWithHostContext(!function->IsExternal(),
  342. ThreadContext::GetContextForCurrentThread()->GetPreviousHostScriptContext());
  343. JavascriptMethod entryPoint;
  344. FunctionInfo *funcInfo = function->GetFunctionInfo();
  345. TTD_XSITE_LOG(callable->GetScriptContext(), "DefaultOrProfileThunk", callable);
  346. if (funcInfo->HasBody())
  347. {
  348. #ifdef ASMJS_PLAT
  349. if (funcInfo->GetFunctionProxy()->IsFunctionBody() &&
  350. funcInfo->GetFunctionBody()->GetIsAsmJsFunction())
  351. {
  352. #ifdef ENABLE_WASM
  353. AsmJsFunctionInfo* asmInfo = funcInfo->GetFunctionBody()->GetAsmJsFunctionInfo();
  354. if (asmInfo && asmInfo->IsWasmDeferredParse())
  355. {
  356. entryPoint = WasmLibrary::WasmDeferredParseExternalThunk;
  357. }
  358. else
  359. #endif
  360. {
  361. entryPoint = Js::AsmJsExternalEntryPoint;
  362. }
  363. }
  364. else
  365. #endif
  366. {
  367. entryPoint = ScriptFunction::FromVar(function)->GetEntryPointInfo()->jsMethod;
  368. }
  369. }
  370. else
  371. {
  372. entryPoint = funcInfo->GetOriginalEntryPoint();
  373. }
  374. return CommonThunk(function, entryPoint, args);
  375. }
  376. Var CrossSite::CrossSiteProxyCallTrap(RecyclableObject* function, CallInfo callInfo, ...)
  377. {
  378. RUNTIME_ARGUMENTS(args, callInfo);
  379. Assert(JavascriptProxy::Is(function));
  380. return CrossSite::CommonThunk(function, JavascriptProxy::FunctionCallTrap, args);
  381. }
  382. Var CrossSite::CommonThunk(RecyclableObject* recyclableObject, JavascriptMethod entryPoint, Arguments args)
  383. {
  384. DynamicObject* function = DynamicObject::FromVar(recyclableObject);
  385. FunctionInfo * functionInfo = (JavascriptFunction::Is(function) ? JavascriptFunction::FromVar(function)->GetFunctionInfo() : nullptr);
  386. AutoDisableRedeferral autoDisableRedeferral(functionInfo);
  387. ScriptContext* targetScriptContext = function->GetScriptContext();
  388. Assert(!targetScriptContext->IsClosed());
  389. Assert(function->IsExternal() || function->IsCrossSiteObject());
  390. Assert(targetScriptContext->GetThreadContext()->IsScriptActive());
  391. HostScriptContext* calleeHostScriptContext = targetScriptContext->GetHostScriptContext();
  392. HostScriptContext* callerHostScriptContext = targetScriptContext->GetThreadContext()->GetPreviousHostScriptContext();
  393. if (callerHostScriptContext == calleeHostScriptContext || (callerHostScriptContext == nullptr && !calleeHostScriptContext->HasCaller()))
  394. {
  395. return JavascriptFunction::CallFunction<true>(function, entryPoint, args);
  396. }
  397. #if DBG_DUMP || defined(PROFILE_EXEC) || defined(PROFILE_MEM)
  398. calleeHostScriptContext->EnsureParentInfo(callerHostScriptContext->GetScriptContext());
  399. #endif
  400. TTD_XSITE_LOG(recyclableObject->GetScriptContext(), "CommonThunk -- Pass Through", recyclableObject);
  401. uint i = 0;
  402. if (args.Values[0] == nullptr)
  403. {
  404. i = 1;
  405. Assert(args.Info.Flags & CallFlags_New);
  406. Assert(JavascriptProxy::Is(function) || (JavascriptFunction::Is(function) && JavascriptFunction::FromVar(function)->GetFunctionInfo()->GetAttributes() & FunctionInfo::SkipDefaultNewObject));
  407. }
  408. uint count = args.Info.Count;
  409. if ((args.Info.Flags & CallFlags_ExtraArg) && ((args.Info.Flags & CallFlags_NewTarget) == 0))
  410. {
  411. // The final eval arg is a frame display that needs to be marshaled specially.
  412. args.Values[count-1] = CrossSite::MarshalFrameDisplay(targetScriptContext, (FrameDisplay*)args.Values[count-1]);
  413. count--;
  414. }
  415. for (; i < count; i++)
  416. {
  417. args.Values[i] = CrossSite::MarshalVar(targetScriptContext, args.Values[i]);
  418. }
  419. #if ENABLE_NATIVE_CODEGEN
  420. CheckCodeGenFunction checkCodeGenFunction = GetCheckCodeGenFunction(entryPoint);
  421. if (checkCodeGenFunction != nullptr)
  422. {
  423. ScriptFunction* callFunc = ScriptFunction::FromVar(function);
  424. entryPoint = checkCodeGenFunction(callFunc);
  425. Assert(CrossSite::IsThunk(function->GetEntryPoint()));
  426. }
  427. #endif
  428. // We need to setup the caller chain when we go across script site boundary. Property access
  429. // is OK, and we need to let host know who the caller is when a call is from another script site.
  430. // CrossSiteObject is the natural place but it is in the target site. We build up the site
  431. // chain through PushDispatchExCaller/PopDispatchExCaller, and we call SetCaller in the target site
  432. // to indicate who the caller is. We first need to get the site from the previously pushed site
  433. // and set that as the caller for current call, and push a new DispatchExCaller for future calls
  434. // off this site. GetDispatchExCaller and ReleaseDispatchExCaller is used to get the current caller.
  435. // currentDispatchExCaller is cached to avoid multiple allocations.
  436. IUnknown* sourceCaller = nullptr, *previousSourceCaller = nullptr;
  437. HRESULT hr = NOERROR;
  438. Var result = nullptr;
  439. BOOL wasDispatchExCallerPushed = FALSE, wasCallerSet = FALSE;
  440. TryFinally([&]()
  441. {
  442. hr = callerHostScriptContext->GetDispatchExCaller((void**)&sourceCaller);
  443. if (SUCCEEDED(hr))
  444. {
  445. hr = calleeHostScriptContext->SetCaller((IUnknown*)sourceCaller, (IUnknown**)&previousSourceCaller);
  446. }
  447. if (SUCCEEDED(hr))
  448. {
  449. wasCallerSet = TRUE;
  450. hr = calleeHostScriptContext->PushHostScriptContext();
  451. }
  452. if (FAILED(hr))
  453. {
  454. // CONSIDER: Should this be callerScriptContext if we failed?
  455. JavascriptError::MapAndThrowError(targetScriptContext, hr);
  456. }
  457. wasDispatchExCallerPushed = TRUE;
  458. result = JavascriptFunction::CallFunction<true>(function, entryPoint, args);
  459. ScriptContext* callerScriptContext = callerHostScriptContext->GetScriptContext();
  460. result = CrossSite::MarshalVar(callerScriptContext, result);
  461. },
  462. [&](bool hasException)
  463. {
  464. if (sourceCaller != nullptr)
  465. {
  466. callerHostScriptContext->ReleaseDispatchExCaller(sourceCaller);
  467. }
  468. IUnknown* originalCaller = nullptr;
  469. if (wasDispatchExCallerPushed)
  470. {
  471. calleeHostScriptContext->PopHostScriptContext();
  472. }
  473. if (wasCallerSet)
  474. {
  475. calleeHostScriptContext->SetCaller(previousSourceCaller, &originalCaller);
  476. if (previousSourceCaller)
  477. {
  478. previousSourceCaller->Release();
  479. }
  480. if (originalCaller)
  481. {
  482. originalCaller->Release();
  483. }
  484. }
  485. });
  486. Assert(result != nullptr);
  487. return result;
  488. }
  489. // For prototype chain to install cross-site thunk.
  490. // When we change prototype using __proto__, those prototypes might not have cross-site thunks
  491. // installed even though the CEO is accessed from a different context. During ChangePrototype time
  492. // we don't really know where the requestContext is.
  493. // Force installing cross-site thunk for all prototype changes. It's a relatively less frequently used
  494. // scenario.
  495. void CrossSite::ForceCrossSiteThunkOnPrototypeChain(RecyclableObject* object)
  496. {
  497. if (TaggedNumber::Is(object))
  498. {
  499. return;
  500. }
  501. while (DynamicType::Is(object->GetTypeId()) && !JavascriptProxy::Is(object))
  502. {
  503. DynamicObject* dynamicObject = DynamicObject::FromVar(object);
  504. if (!dynamicObject->IsCrossSiteObject() && !dynamicObject->IsExternal())
  505. {
  506. // force to install cross-site thunk on prototype objects.
  507. dynamicObject->MarshalToScriptContext(nullptr);
  508. }
  509. object = object->GetPrototype();
  510. }
  511. return;
  512. }
  513. };