CrossSite.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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::UnsafeFromVar(instance);
  29. if (object->GetScriptContext() == requestContext)
  30. {
  31. return FALSE;
  32. }
  33. if (DynamicType::Is(object->GetTypeId()))
  34. {
  35. return !DynamicObject::UnsafeFromVar(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::UnsafeFromVar(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. // Marshaling should not cause any re-entrancy.
  206. JS_REENTRANCY_LOCK(jsReentLock, scriptContext->GetThreadContext());
  207. #if ENABLE_COPYONACCESS_ARRAY
  208. JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(object);
  209. #endif
  210. TypeId typeId = object->GetTypeId();
  211. AssertMsg(typeId != TypeIds_Enumerator, "enumerator shouldn't be marshalled here");
  212. // At the moment the mental model for WithScopeObject Marshaling is this:
  213. // Are we trying to marshal a WithScopeObject in the Frame Display? - then 1) unwrap in MarshalFrameDisplay,
  214. // 2) marshal the wrapped object, 3) Create a new WithScopeObject in the current scriptContext and re-wrap.
  215. // We can avoid copying the WithScopeObject because it has no properties and never should.
  216. // Thus creating a new WithScopeObject per context in MarshalFrameDisplay should be kosher.
  217. // If it is not a FrameDisplay then we should not marshal. We can wrap cross context objects with a
  218. // withscopeObject in a different context. When we unwrap for property lookups and the wrapped object
  219. // is cross context, then we marshal the wrapped object into the current scriptContext, thus avoiding
  220. // the need to copy the WithScopeObject itself. Thus We don't have to handle marshaling the WithScopeObject
  221. // in non-FrameDisplay cases.
  222. AssertMsg(typeId != TypeIds_WithScopeObject, "WithScopeObject shouldn't be marshalled here");
  223. if (StaticType::Is(typeId))
  224. {
  225. TTD_XSITE_LOG(object->GetScriptContext(), "CloneToScriptContext", object);
  226. return object->CloneToScriptContext(scriptContext);
  227. }
  228. if (typeId == TypeIds_ModuleRoot)
  229. {
  230. RootObjectBase *moduleRoot = static_cast<RootObjectBase*>(object);
  231. HostObjectBase * hostObject = moduleRoot->GetHostObject();
  232. // When marshaling module root, all we need is the host object.
  233. // So, if the module root which is being marshaled has host object, marshal it.
  234. if (hostObject)
  235. {
  236. TTD_XSITE_LOG(object->GetScriptContext(), "hostObject", hostObject);
  237. Var hostDispatch = hostObject->GetHostDispatchVar();
  238. return CrossSite::MarshalVar(scriptContext, hostDispatch);
  239. }
  240. }
  241. if (typeId == TypeIds_Function)
  242. {
  243. if (object == object->GetScriptContext()->GetLibrary()->GetDefaultAccessorFunction() )
  244. {
  245. TTD_XSITE_LOG(object->GetScriptContext(), "DefaultAccessorFunction", object);
  246. return scriptContext->GetLibrary()->GetDefaultAccessorFunction();
  247. }
  248. if (DoRequestWrapper(object, fRequestWrapper))
  249. {
  250. TTD_XSITE_LOG(object->GetScriptContext(), "CreateWrappedExternalFunction", object);
  251. // Marshal as a cross-site thunk if necessary before re-wrapping in an external function thunk.
  252. MarshalVarInner(scriptContext, object, false);
  253. return scriptContext->GetLibrary()->CreateWrappedExternalFunction(static_cast<JavascriptExternalFunction*>(object));
  254. }
  255. }
  256. // We have an object marshaled, we need to keep track of the related script context
  257. // so optimization overrides can be updated as a group
  258. scriptContext->optimizationOverrides.Merge(&object->GetScriptContext()->optimizationOverrides);
  259. DynamicObject * dynamicObject = DynamicObject::FromVar(object);
  260. if (!dynamicObject->IsExternal())
  261. {
  262. if (!dynamicObject->IsCrossSiteObject())
  263. {
  264. if (JavascriptProxy::Is(dynamicObject))
  265. {
  266. // We don't need to marshal the prototype chain in the case of Proxy. Otherwise we will go to the user code.
  267. TTD_XSITE_LOG(object->GetScriptContext(), "MarshalDynamicObject", object);
  268. MarshalDynamicObject(scriptContext, dynamicObject);
  269. }
  270. else
  271. {
  272. TTD_XSITE_LOG(object->GetScriptContext(), "MarshalDynamicObjectAndPrototype", object);
  273. MarshalDynamicObjectAndPrototype(scriptContext, dynamicObject);
  274. }
  275. }
  276. }
  277. else
  278. {
  279. MarshalPrototypeChain(scriptContext, dynamicObject);
  280. if (Js::JavascriptConversion::IsCallable(dynamicObject))
  281. {
  282. TTD_XSITE_LOG(object->GetScriptContext(), "MarshalToScriptContext", object);
  283. dynamicObject->MarshalToScriptContext(scriptContext);
  284. }
  285. }
  286. return dynamicObject;
  287. }
  288. bool CrossSite::IsThunk(JavascriptMethod thunk)
  289. {
  290. #if defined(ENABLE_SCRIPT_PROFILING) || defined(ENABLE_SCRIPT_DEBUGGING)
  291. return (thunk == CrossSite::ProfileThunk || thunk == CrossSite::DefaultThunk);
  292. #else
  293. return (thunk == CrossSite::DefaultThunk);
  294. #endif
  295. }
  296. #if defined(ENABLE_SCRIPT_PROFILING) || defined(ENABLE_SCRIPT_DEBUGGING)
  297. Var CrossSite::ProfileThunk(RecyclableObject* callable, CallInfo callInfo, ...)
  298. {
  299. JavascriptFunction* function = JavascriptFunction::FromVar(callable);
  300. Assert(function->GetTypeId() == TypeIds_Function);
  301. Assert(function->GetEntryPoint() == CrossSite::ProfileThunk);
  302. RUNTIME_ARGUMENTS(args, callInfo);
  303. ScriptContext * scriptContext = function->GetScriptContext();
  304. // It is not safe to access the function body if the script context is not alive.
  305. scriptContext->VerifyAliveWithHostContext(!function->IsExternal(),
  306. scriptContext->GetThreadContext()->GetPreviousHostScriptContext());
  307. JavascriptMethod entryPoint;
  308. FunctionInfo *funcInfo = function->GetFunctionInfo();
  309. TTD_XSITE_LOG(callable->GetScriptContext(), "DefaultOrProfileThunk", callable);
  310. #ifdef ENABLE_WASM
  311. if (WasmScriptFunction::Is(function))
  312. {
  313. AsmJsFunctionInfo* asmInfo = funcInfo->GetFunctionBody()->GetAsmJsFunctionInfo();
  314. Assert(asmInfo);
  315. if (asmInfo->IsWasmDeferredParse())
  316. {
  317. entryPoint = WasmLibrary::WasmDeferredParseExternalThunk;
  318. }
  319. else
  320. {
  321. entryPoint = Js::AsmJsExternalEntryPoint;
  322. }
  323. } else
  324. #endif
  325. if (funcInfo->HasBody())
  326. {
  327. #if ENABLE_DEBUG_CONFIG_OPTIONS
  328. char16 debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  329. #endif
  330. entryPoint = ScriptFunction::FromVar(function)->GetEntryPointInfo()->jsMethod;
  331. if (funcInfo->IsDeferred() && scriptContext->IsProfiling())
  332. {
  333. // if the current entrypoint is deferred parse we need to update it appropriately for the profiler mode.
  334. entryPoint = Js::ScriptContext::GetProfileModeThunk(entryPoint);
  335. }
  336. OUTPUT_TRACE(Js::ScriptProfilerPhase, _u("CrossSite::ProfileThunk FunctionNumber : %s, Entrypoint : 0x%08X\n"), funcInfo->GetFunctionProxy()->GetDebugNumberSet(debugStringBuffer), entryPoint);
  337. }
  338. else
  339. {
  340. entryPoint = ProfileEntryThunk;
  341. }
  342. return CommonThunk(function, entryPoint, args);
  343. }
  344. #endif
  345. Var CrossSite::DefaultThunk(RecyclableObject* callable, CallInfo callInfo, ...)
  346. {
  347. JavascriptFunction* function = JavascriptFunction::FromVar(callable);
  348. Assert(function->GetTypeId() == TypeIds_Function);
  349. Assert(function->GetEntryPoint() == CrossSite::DefaultThunk);
  350. RUNTIME_ARGUMENTS(args, callInfo);
  351. // It is not safe to access the function body if the script context is not alive.
  352. function->GetScriptContext()->VerifyAliveWithHostContext(!function->IsExternal(),
  353. ThreadContext::GetContextForCurrentThread()->GetPreviousHostScriptContext());
  354. JavascriptMethod entryPoint;
  355. FunctionInfo *funcInfo = function->GetFunctionInfo();
  356. TTD_XSITE_LOG(callable->GetScriptContext(), "DefaultOrProfileThunk", callable);
  357. if (funcInfo->HasBody())
  358. {
  359. #ifdef ASMJS_PLAT
  360. if (funcInfo->GetFunctionProxy()->IsFunctionBody() &&
  361. funcInfo->GetFunctionBody()->GetIsAsmJsFunction())
  362. {
  363. #ifdef ENABLE_WASM
  364. AsmJsFunctionInfo* asmInfo = funcInfo->GetFunctionBody()->GetAsmJsFunctionInfo();
  365. if (asmInfo && asmInfo->IsWasmDeferredParse())
  366. {
  367. entryPoint = WasmLibrary::WasmDeferredParseExternalThunk;
  368. }
  369. else
  370. #endif
  371. {
  372. entryPoint = Js::AsmJsExternalEntryPoint;
  373. }
  374. }
  375. else
  376. #endif
  377. {
  378. entryPoint = ScriptFunction::FromVar(function)->GetEntryPointInfo()->jsMethod;
  379. }
  380. }
  381. else
  382. {
  383. entryPoint = funcInfo->GetOriginalEntryPoint();
  384. }
  385. return CommonThunk(function, entryPoint, args);
  386. }
  387. Var CrossSite::CrossSiteProxyCallTrap(RecyclableObject* function, CallInfo callInfo, ...)
  388. {
  389. RUNTIME_ARGUMENTS(args, callInfo);
  390. Assert(JavascriptProxy::Is(function));
  391. return CrossSite::CommonThunk(function, JavascriptProxy::FunctionCallTrap, args);
  392. }
  393. Var CrossSite::CommonThunk(RecyclableObject* recyclableObject, JavascriptMethod entryPoint, Arguments args)
  394. {
  395. DynamicObject* function = DynamicObject::FromVar(recyclableObject);
  396. FunctionInfo * functionInfo = (JavascriptFunction::Is(function) ? JavascriptFunction::FromVar(function)->GetFunctionInfo() : nullptr);
  397. AutoDisableRedeferral autoDisableRedeferral(functionInfo);
  398. ScriptContext* targetScriptContext = function->GetScriptContext();
  399. Assert(!targetScriptContext->IsClosed());
  400. Assert(function->IsExternal() || function->IsCrossSiteObject());
  401. Assert(targetScriptContext->GetThreadContext()->IsScriptActive());
  402. HostScriptContext* calleeHostScriptContext = targetScriptContext->GetHostScriptContext();
  403. HostScriptContext* callerHostScriptContext = targetScriptContext->GetThreadContext()->GetPreviousHostScriptContext();
  404. if (callerHostScriptContext == calleeHostScriptContext || (callerHostScriptContext == nullptr && !calleeHostScriptContext->HasCaller()))
  405. {
  406. return JavascriptFunction::CallFunction<true>(function, entryPoint, args);
  407. }
  408. #if DBG_DUMP || defined(PROFILE_EXEC) || defined(PROFILE_MEM)
  409. calleeHostScriptContext->EnsureParentInfo(callerHostScriptContext->GetScriptContext());
  410. #endif
  411. TTD_XSITE_LOG(recyclableObject->GetScriptContext(), "CommonThunk -- Pass Through", recyclableObject);
  412. uint i = 0;
  413. if (args.Values[0] == nullptr)
  414. {
  415. i = 1;
  416. Assert(args.IsNewCall());
  417. Assert(JavascriptProxy::Is(function) || (JavascriptFunction::Is(function) && JavascriptFunction::FromVar(function)->GetFunctionInfo()->GetAttributes() & FunctionInfo::SkipDefaultNewObject));
  418. }
  419. uint count = args.Info.Count;
  420. for (; i < count; i++)
  421. {
  422. args.Values[i] = CrossSite::MarshalVar(targetScriptContext, args.Values[i]);
  423. }
  424. if (args.HasExtraArg())
  425. {
  426. // The final eval arg is a frame display that needs to be marshaled specially.
  427. args.Values[count] = CrossSite::MarshalFrameDisplay(targetScriptContext, args.GetFrameDisplay());
  428. }
  429. #if ENABLE_NATIVE_CODEGEN
  430. CheckCodeGenFunction checkCodeGenFunction = GetCheckCodeGenFunction(entryPoint);
  431. if (checkCodeGenFunction != nullptr)
  432. {
  433. ScriptFunction* callFunc = ScriptFunction::FromVar(function);
  434. entryPoint = checkCodeGenFunction(callFunc);
  435. Assert(CrossSite::IsThunk(function->GetEntryPoint()));
  436. }
  437. #endif
  438. // We need to setup the caller chain when we go across script site boundary. Property access
  439. // is OK, and we need to let host know who the caller is when a call is from another script site.
  440. // CrossSiteObject is the natural place but it is in the target site. We build up the site
  441. // chain through PushDispatchExCaller/PopDispatchExCaller, and we call SetCaller in the target site
  442. // to indicate who the caller is. We first need to get the site from the previously pushed site
  443. // and set that as the caller for current call, and push a new DispatchExCaller for future calls
  444. // off this site. GetDispatchExCaller and ReleaseDispatchExCaller is used to get the current caller.
  445. // currentDispatchExCaller is cached to avoid multiple allocations.
  446. IUnknown* sourceCaller = nullptr, *previousSourceCaller = nullptr;
  447. HRESULT hr = NOERROR;
  448. Var result = nullptr;
  449. BOOL wasDispatchExCallerPushed = FALSE, wasCallerSet = FALSE;
  450. TryFinally([&]()
  451. {
  452. hr = callerHostScriptContext->GetDispatchExCaller((void**)&sourceCaller);
  453. if (SUCCEEDED(hr))
  454. {
  455. hr = calleeHostScriptContext->SetCaller((IUnknown*)sourceCaller, (IUnknown**)&previousSourceCaller);
  456. }
  457. if (SUCCEEDED(hr))
  458. {
  459. wasCallerSet = TRUE;
  460. hr = calleeHostScriptContext->PushHostScriptContext();
  461. }
  462. if (FAILED(hr))
  463. {
  464. // CONSIDER: Should this be callerScriptContext if we failed?
  465. JavascriptError::MapAndThrowError(targetScriptContext, hr);
  466. }
  467. wasDispatchExCallerPushed = TRUE;
  468. result = JavascriptFunction::CallFunction<true>(function, entryPoint, args);
  469. ScriptContext* callerScriptContext = callerHostScriptContext->GetScriptContext();
  470. result = CrossSite::MarshalVar(callerScriptContext, result);
  471. },
  472. [&](bool hasException)
  473. {
  474. if (sourceCaller != nullptr)
  475. {
  476. callerHostScriptContext->ReleaseDispatchExCaller(sourceCaller);
  477. }
  478. IUnknown* originalCaller = nullptr;
  479. if (wasDispatchExCallerPushed)
  480. {
  481. calleeHostScriptContext->PopHostScriptContext();
  482. }
  483. if (wasCallerSet)
  484. {
  485. calleeHostScriptContext->SetCaller(previousSourceCaller, &originalCaller);
  486. if (previousSourceCaller)
  487. {
  488. previousSourceCaller->Release();
  489. }
  490. if (originalCaller)
  491. {
  492. originalCaller->Release();
  493. }
  494. }
  495. });
  496. Assert(result != nullptr);
  497. return result;
  498. }
  499. // For prototype chain to install cross-site thunk.
  500. // When we change prototype using __proto__, those prototypes might not have cross-site thunks
  501. // installed even though the CEO is accessed from a different context. During ChangePrototype time
  502. // we don't really know where the requestContext is.
  503. // Force installing cross-site thunk for all prototype changes. It's a relatively less frequently used
  504. // scenario.
  505. void CrossSite::ForceCrossSiteThunkOnPrototypeChain(RecyclableObject* object)
  506. {
  507. if (TaggedNumber::Is(object))
  508. {
  509. return;
  510. }
  511. while (DynamicType::Is(object->GetTypeId()) && !JavascriptProxy::Is(object))
  512. {
  513. DynamicObject* dynamicObject = DynamicObject::UnsafeFromVar(object);
  514. if (!dynamicObject->IsCrossSiteObject() && !dynamicObject->IsExternal())
  515. {
  516. // force to install cross-site thunk on prototype objects.
  517. dynamicObject->MarshalToScriptContext(nullptr);
  518. }
  519. object = object->GetPrototype();
  520. }
  521. return;
  522. }
  523. };