CallbackObject.cpp (19868B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "mozilla/dom/CallbackObject.h" 8 9 #include "WorkerPrivate.h" 10 #include "WorkerScope.h" 11 #include "js/ContextOptions.h" 12 #include "jsapi.h" 13 #include "jsfriendapi.h" 14 #include "mozilla/CycleCollectedJSContext.h" 15 #include "mozilla/dom/BindingUtils.h" 16 #include "nsContentUtils.h" 17 #include "nsGlobalWindowInner.h" 18 #include "nsIScriptContext.h" 19 #include "nsIScriptGlobalObject.h" 20 #include "nsJSPrincipals.h" 21 #include "nsJSUtils.h" 22 #include "nsPIDOMWindow.h" 23 #include "xpcprivate.h" 24 25 namespace mozilla::dom { 26 27 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(CallbackObject) 28 NS_INTERFACE_MAP_ENTRY(mozilla::dom::CallbackObject) 29 NS_INTERFACE_MAP_ENTRY(nsISupports) 30 NS_INTERFACE_MAP_END 31 32 NS_IMPL_CYCLE_COLLECTING_ADDREF(CallbackObject) 33 NS_IMPL_CYCLE_COLLECTING_RELEASE_WITH_LAST_RELEASE(CallbackObject, Reset()) 34 35 NS_IMPL_CYCLE_COLLECTION_CLASS(CallbackObject) 36 37 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(CallbackObject) 38 tmp->ClearJSReferences(); 39 NS_IMPL_CYCLE_COLLECTION_UNLINK(mIncumbentGlobal) 40 NS_IMPL_CYCLE_COLLECTION_UNLINK_END 41 42 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_BEGIN(CallbackObject) 43 JSObject* callback = tmp->CallbackPreserveColor(); 44 45 if (!aRemovingAllowed) { 46 // If our callback has been cleared, we can't be part of a garbage cycle. 47 return !callback; 48 } 49 50 // mCallback is always wrapped for the CallbackObject's incumbent global. In 51 // the case where the real callback is in a different compartment, we have a 52 // cross-compartment wrapper, and it will automatically be cut when its 53 // compartment is nuked. In the case where it is in the same compartment, we 54 // have a reference to the real function. Since that means there are no 55 // wrappers to cut, we need to check whether the compartment is still alive, 56 // and drop the references if it is not. 57 58 if (MOZ_UNLIKELY(!callback)) { 59 return true; 60 } 61 if (MOZ_LIKELY(tmp->mIncumbentGlobal) && 62 MOZ_UNLIKELY(js::NukedObjectRealm(tmp->CallbackGlobalPreserveColor()))) { 63 // It's not safe to release our global reference or drop our JS objects at 64 // this point, so defer their finalization until CC is finished. 65 AddForDeferredFinalization(new JSObjectsDropper(tmp)); 66 DeferredFinalize(tmp->mIncumbentGlobal.forget().take()); 67 return true; 68 } 69 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_END 70 71 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_BEGIN(CallbackObject) 72 return !tmp->mCallback; 73 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_END 74 75 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_BEGIN(CallbackObject) 76 return !tmp->mCallback; 77 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_END 78 79 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(CallbackObject) 80 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mIncumbentGlobal) 81 // If a new member is added here, don't forget to update IsBlackForCC. 82 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END 83 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(CallbackObject) 84 NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mCallback) 85 NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mCallbackGlobal) 86 NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mCreationStack) 87 NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mIncumbentJSGlobal) 88 // If a new member is added here, don't forget to update IsBlackForCC. 89 NS_IMPL_CYCLE_COLLECTION_TRACE_END 90 91 void CallbackObjectBase::Trace(JSTracer* aTracer) { 92 JS::TraceEdge(aTracer, &mCallback, "CallbackObject.mCallback"); 93 JS::TraceEdge(aTracer, &mCallbackGlobal, "CallbackObject.mCallbackGlobal"); 94 JS::TraceEdge(aTracer, &mCreationStack, "CallbackObject.mCreationStack"); 95 JS::TraceEdge(aTracer, &mIncumbentJSGlobal, 96 "CallbackObject.mIncumbentJSGlobal"); 97 } 98 99 void CallbackObject::FinishSlowJSInitIfMoreThanOneOwner(JSContext* aCx) { 100 MOZ_ASSERT(mRefCnt.get() > 0); 101 if (mRefCnt.get() > 1) { 102 mozilla::HoldJSObjectsWithKey(this); 103 if (JS::IsAsyncStackCaptureEnabledForRealm(aCx)) { 104 JS::Rooted<JSObject*> stack(aCx); 105 if (!JS::CaptureCurrentStack(aCx, &stack)) { 106 JS_ClearPendingException(aCx); 107 } 108 mCreationStack = stack; 109 } 110 mIncumbentGlobal = GetIncumbentGlobal(); 111 if (mIncumbentGlobal) { 112 // We don't want to expose to JS here (change the color). If someone ever 113 // reads mIncumbentJSGlobal, that will expose. If not, no need to expose 114 // here. 115 mIncumbentJSGlobal = mIncumbentGlobal->GetGlobalJSObjectPreserveColor(); 116 } 117 } else { 118 // We can just forget all our stuff. 119 ClearJSReferences(); 120 } 121 } 122 123 JSObject* CallbackObjectBase::Callback(JSContext* aCx) { 124 JSObject* callback = CallbackOrNull(); 125 if (!callback) { 126 callback = JS_NewDeadWrapper(aCx); 127 } 128 129 MOZ_DIAGNOSTIC_ASSERT(callback); 130 return callback; 131 } 132 133 void CallbackObjectBase::GetDescription(nsACString& aOutString) { 134 JSObject* wrappedCallback = CallbackOrNull(); 135 if (!wrappedCallback) { 136 aOutString.Append("<callback from a nuked compartment>"); 137 return; 138 } 139 140 JS::Rooted<JSObject*> unwrappedCallback( 141 RootingCx(), js::CheckedUnwrapStatic(wrappedCallback)); 142 if (!unwrappedCallback) { 143 aOutString.Append("<not a function>"); 144 return; 145 } 146 147 AutoJSAPI jsapi; 148 jsapi.Init(); 149 JSContext* cx = jsapi.cx(); 150 151 JS::Rooted<JSObject*> rootedCallback(cx, unwrappedCallback); 152 JSAutoRealm ar(cx, rootedCallback); 153 154 JS::Rooted<JSFunction*> rootedFunction(cx, 155 JS_GetObjectFunction(rootedCallback)); 156 if (!rootedFunction) { 157 aOutString.Append("<not a function>"); 158 return; 159 } 160 161 JS::Rooted<JSString*> displayId( 162 cx, JS_GetMaybePartialFunctionDisplayId(rootedFunction)); 163 if (displayId) { 164 nsAutoJSString funcNameStr; 165 if (funcNameStr.init(cx, displayId)) { 166 if (funcNameStr.IsEmpty()) { 167 aOutString.Append("<empty name>"); 168 } else { 169 AppendUTF16toUTF8(funcNameStr, aOutString); 170 } 171 } else { 172 aOutString.Append("<function name string failed to materialize>"); 173 jsapi.ClearException(); 174 } 175 } else { 176 aOutString.Append("<anonymous>"); 177 } 178 179 JS::Rooted<JSScript*> rootedScript(cx, 180 JS_GetFunctionScript(cx, rootedFunction)); 181 if (!rootedScript) { 182 return; 183 } 184 185 aOutString.Append(" ("); 186 aOutString.Append(JS_GetScriptFilename(rootedScript)); 187 aOutString.Append(":"); 188 aOutString.AppendInt(JS_GetScriptBaseLineNumber(cx, rootedScript)); 189 aOutString.Append(")"); 190 } 191 192 // Get the global for this callback: Note that this can return nullptr 193 // if it doesn't make sense to invoke the callback or global here . 194 // 195 // Note that for the case of JS-implemented WebIDL we never have a window here. 196 nsIGlobalObject* CallSetup::GetActiveGlobalObjectForCall( 197 JS::Handle<JSObject*> callbackOrGlobal, bool aIsMainThread, 198 bool aIsJSImplementedWebIDL, ErrorResult& aRv) { 199 nsGlobalWindowInner* win = aIsMainThread && !aIsJSImplementedWebIDL 200 ? xpc::WindowGlobalOrNull(callbackOrGlobal) 201 : nullptr; 202 if (win) { 203 // We don't want to run script in windows that have been navigated away 204 // from. 205 if (!win->HasActiveDocument()) { 206 aRv.ThrowNotSupportedError( 207 "Refusing to execute function from window whose document is no " 208 "longer active."); 209 return nullptr; 210 } 211 return win; 212 } 213 214 // No DOM Window. Store the global. 215 auto* globalObject = xpc::NativeGlobal(callbackOrGlobal); 216 MOZ_ASSERT(globalObject); 217 return globalObject; 218 } 219 220 // Check that it's OK & possible to execute script in the given global, and if 221 // not return false and fill in aRv. 222 bool CallSetup::CheckBeforeExecution(nsIGlobalObject* aGlobalObject, 223 JSObject* aCallbackOrGlobal, 224 bool aIsJSImplementedWebIDL, 225 ErrorResult& aRv) { 226 if (aGlobalObject->IsScriptForbidden(aCallbackOrGlobal, 227 aIsJSImplementedWebIDL)) { 228 aRv.ThrowNotSupportedError( 229 "Refusing to execute function from global in which script is " 230 "disabled."); 231 return false; 232 } 233 234 // Bail out if there's no useful global. 235 if (!aGlobalObject->HasJSGlobal()) { 236 aRv.ThrowNotSupportedError( 237 "Refusing to execute function from global which is being torn down."); 238 return false; 239 } 240 241 return true; 242 } 243 244 void CallSetup::SetupForExecution(nsIGlobalObject* aGlobalObject, 245 nsIGlobalObject* aIncumbentGlobal, 246 JS::Handle<JSObject*> aCallbackOrGlobal, 247 JS::Handle<JSObject*> aCallbackGlobal, 248 JS::Handle<JSObject*> aCreationStack, 249 nsIPrincipal* aWebIDLCallerPrincipal, 250 const char* aExecutionReason, 251 ErrorResult& aRv) { 252 AutoAllowLegacyScriptExecution exemption; 253 mAutoEntryScript.emplace(aGlobalObject, aExecutionReason, mIsMainThread); 254 mAutoEntryScript->SetWebIDLCallerPrincipal(aWebIDLCallerPrincipal); 255 256 if (aIncumbentGlobal) { 257 // The callback object traces its incumbent JS global, so in general it 258 // should be alive here. However, it's possible that we could run afoul 259 // of the same IPC global weirdness described above, wherein the 260 // nsIGlobalObject has severed its reference to the JS global. Let's just 261 // be safe here, so that nobody has to waste a day debugging gaia-ui tests. 262 if (!aIncumbentGlobal->HasJSGlobal()) { 263 aRv.ThrowNotSupportedError( 264 "Refusing to execute function because our incumbent global is being " 265 "torn down."); 266 return; 267 } 268 mAutoIncumbentScript.emplace(aIncumbentGlobal); 269 } 270 271 JSContext* cx = mAutoEntryScript->cx(); 272 273 // Unmark the callable (by invoking CallbackOrNull() and not the 274 // CallbackPreserveColor() variant), and stick it in a Rooted before it can 275 // go gray again. 276 // Nothing before us in this function can trigger a CC, so it's safe to wait 277 // until here it do the unmark. This allows us to construct mRootedCallable 278 // with the cx from mAutoEntryScript, avoiding the cost of finding another 279 // JSContext. (Rooted<> does not care about requests or compartments.) 280 mRootedCallable.emplace(cx, aCallbackOrGlobal); 281 282 if (aCreationStack) { 283 mAsyncStackSetter.emplace(cx, aCreationStack, aExecutionReason); 284 } 285 286 // Enter the realm of our callback, so we can actually work with it. 287 // 288 // Note that if the callback is a wrapper, this will not be the same 289 // realm that we ended up in with mAutoEntryScript above, because the 290 // entry point is based off of the unwrapped callback (realCallback). 291 mAr.emplace(cx, aCallbackGlobal); 292 293 // And now we're ready to go. 294 mCx = cx; 295 296 // We don't really have a good error message prefix to use for the 297 // BindingCallContext. 298 mCallContext.emplace(cx, nullptr); 299 } 300 301 // Private delegating constructor for common initialization 302 CallSetup::CallSetup(ErrorResult& aRv, 303 CallbackObjectBase::ExceptionHandling aExceptionHandling, 304 JS::Realm* aRealm, bool aIsMainThread, 305 CycleCollectedJSContext* aCCJS) 306 : mCx(nullptr), 307 mRealm(aRealm), 308 mErrorResult(aRv), 309 mExceptionHandling(aExceptionHandling), 310 mIsMainThread(aIsMainThread) { 311 MOZ_ASSERT(aCCJS); 312 aCCJS->EnterMicroTask(); 313 } 314 315 CallSetup::CallSetup(CallbackObjectBase* aCallback, ErrorResult& aRv, 316 const char* aExecutionReason, 317 CallbackObjectBase::ExceptionHandling aExceptionHandling, 318 JS::Realm* aRealm, bool aIsJSImplementedWebIDL) 319 : CallSetup(aCallback, aRv, aExecutionReason, aExceptionHandling, aRealm, 320 aIsJSImplementedWebIDL, CycleCollectedJSContext::Get()) {} 321 322 CallSetup::CallSetup(CallbackObjectBase* aCallback, ErrorResult& aRv, 323 const char* aExecutionReason, 324 CallbackObjectBase::ExceptionHandling aExceptionHandling, 325 JS::Realm* aRealm, bool aIsJSImplementedWebIDL, 326 CycleCollectedJSContext* aCCJS) 327 : CallSetup(aRv, aExceptionHandling, aRealm, NS_IsMainThread(), aCCJS) { 328 MOZ_ASSERT_IF( 329 aExceptionHandling == CallbackObjectBase::eReportExceptions || 330 aExceptionHandling == CallbackObjectBase::eRethrowExceptions, 331 !aRealm); 332 333 JS::RootedTuple<JSObject*, JSObject*, JSObject*> roots(aCCJS->RootingCx()); 334 335 // Compute the caller's subject principal (if necessary) early, before we 336 // do anything that might perturb the relevant state. 337 nsIPrincipal* webIDLCallerPrincipal = nullptr; 338 if (aIsJSImplementedWebIDL) { 339 webIDLCallerPrincipal = 340 nsContentUtils::SubjectPrincipalOrSystemIfNativeCaller(); 341 } 342 343 JSObject* wrappedCallback = aCallback->CallbackPreserveColor(); 344 if (!wrappedCallback) { 345 aRv.ThrowNotSupportedError( 346 "Cannot execute callback from a nuked compartment."); 347 return; 348 } 349 350 nsIGlobalObject* globalObject = nullptr; 351 352 { 353 // First, find the real underlying callback. 354 JS::RootedField<JSObject*, 0> realCallback( 355 roots, js::UncheckedUnwrap(wrappedCallback)); 356 357 globalObject = GetActiveGlobalObjectForCall(realCallback, mIsMainThread, 358 aIsJSImplementedWebIDL, aRv); 359 if (!globalObject) { 360 MOZ_ASSERT(aRv.Failed()); 361 return; 362 } 363 364 // Make sure to use realCallback to get the global of the callback 365 // object, not the wrapper. 366 if (!CheckBeforeExecution(globalObject, realCallback, 367 aIsJSImplementedWebIDL, aRv)) { 368 return; 369 } 370 } 371 372 nsIGlobalObject* incumbent = aCallback->IncumbentGlobalOrNull(); 373 374 // Start the execution setup -- if it succeeds, this will set mCx 375 JS::RootedField<JSObject*, 0> rootedCallback(roots, 376 aCallback->CallbackOrNull()); 377 JS::RootedField<JSObject*, 1> rootedCallbackGlobal( 378 roots, aCallback->CallbackGlobalOrNull()); 379 JS::RootedField<JSObject*, 2> rootedCreationStack( 380 roots, aCallback->GetCreationStack()); 381 SetupForExecution(globalObject, incumbent, rootedCallback, 382 rootedCallbackGlobal, rootedCreationStack, 383 webIDLCallerPrincipal, aExecutionReason, aRv); 384 } 385 386 CallSetup::CallSetup(JS::Handle<JSObject*> aCallbackGlobal, 387 nsIGlobalObject* aIncumbentGlobal, 388 JS::Handle<JSObject*> aCreationStack, ErrorResult& aRv, 389 const char* aExecutionReason, 390 CallbackObjectBase::ExceptionHandling aExceptionHandling, 391 JS::Realm* aRealm) 392 : CallSetup(aRv, aExceptionHandling, aRealm, NS_IsMainThread(), 393 CycleCollectedJSContext::Get()) { 394 MOZ_ASSERT_IF(aExceptionHandling == CallbackFunction::eReportExceptions || 395 aExceptionHandling == CallbackFunction::eRethrowExceptions, 396 !aRealm); 397 398 MOZ_RELEASE_ASSERT(aCallbackGlobal); 399 nsIGlobalObject* globalObject = GetActiveGlobalObjectForCall( 400 aCallbackGlobal, mIsMainThread, /*aIsJSImplementedWebIDL=*/false, aRv); 401 if (!globalObject) { 402 MOZ_ASSERT(aRv.Failed()); 403 return; 404 } 405 406 // Initial Validation Pass 407 if (!CheckBeforeExecution(globalObject, aCallbackGlobal, 408 /*aIsJSImplementedWebIDL=*/false, aRv)) { 409 return; 410 } 411 412 // Start the execution setup -- if it succeeds, this will set mCx 413 SetupForExecution(globalObject, aIncumbentGlobal, aCallbackGlobal, 414 aCallbackGlobal, aCreationStack, 415 /*aWebIDLCallerPrincipal=*/nullptr, aExecutionReason, aRv); 416 } 417 418 bool CallSetup::ShouldRethrowException(JS::Handle<JS::Value> aException) { 419 if (mExceptionHandling == CallbackObjectBase::eRethrowExceptions) { 420 MOZ_ASSERT(!mRealm); 421 return true; 422 } 423 424 MOZ_ASSERT(mRealm); 425 426 // Now we only want to throw an exception to the caller if the object that was 427 // thrown is in the caller realm (which we stored in mRealm). 428 429 if (!aException.isObject()) { 430 return false; 431 } 432 433 JS::Rooted<JSObject*> obj(mCx, &aException.toObject()); 434 obj = js::UncheckedUnwrap(obj, /* stopAtWindowProxy = */ false); 435 return js::GetNonCCWObjectRealm(obj) == mRealm; 436 } 437 438 CallSetup::~CallSetup() { 439 // To get our nesting right we have to destroy our JSAutoRealm first. 440 // In particular, we want to do this before we try reporting any exceptions, 441 // so we end up reporting them while in the realm of our entry point, 442 // not whatever cross-compartment wrappper mCallback might be. 443 // Be careful: the JSAutoRealm might not have been constructed at all! 444 mAr.reset(); 445 446 // Now, if we have a JSContext, report any pending errors on it, unless we 447 // were told to re-throw them. 448 if (mCx) { 449 bool needToDealWithException = mAutoEntryScript->HasException(); 450 if ((mRealm && 451 mExceptionHandling == CallbackObjectBase::eRethrowContentExceptions) || 452 mExceptionHandling == CallbackObjectBase::eRethrowExceptions) { 453 mErrorResult.MightThrowJSException(); 454 if (needToDealWithException) { 455 JS::Rooted<JS::Value> exn(mCx); 456 if (mAutoEntryScript->PeekException(&exn) && 457 ShouldRethrowException(exn)) { 458 mAutoEntryScript->ClearException(); 459 MOZ_ASSERT(!mAutoEntryScript->HasException()); 460 mErrorResult.ThrowJSException(mCx, exn); 461 needToDealWithException = false; 462 } 463 } 464 } 465 466 if (needToDealWithException) { 467 // Either we're supposed to report our exceptions, or we're supposed to 468 // re-throw them but we failed to get the exception value. Either way, 469 // we'll just report the pending exception, if any, once ~mAutoEntryScript 470 // runs. Note that we've already run ~mAr, effectively, so we don't have 471 // to worry about ordering here. 472 if (mErrorResult.IsJSContextException()) { 473 // XXXkhuey bug 1117269. When this is fixed, please consider fixing 474 // ThrowExceptionValueIfSafe over in Exceptions.cpp in the same way. 475 476 // IsJSContextException shouldn't be true anymore because we will report 477 // the exception on the JSContext ... so throw something else. 478 mErrorResult.Throw(NS_ERROR_UNEXPECTED); 479 } 480 } 481 } 482 483 mAutoIncumbentScript.reset(); 484 mAutoEntryScript.reset(); 485 486 // It is important that this is the last thing we do, after leaving the 487 // realm and undoing all our entry/incumbent script changes 488 CycleCollectedJSContext* ccjs = CycleCollectedJSContext::Get(); 489 if (ccjs) { 490 ccjs->LeaveMicroTask(); 491 } 492 } 493 494 already_AddRefed<nsISupports> CallbackObjectHolderBase::ToXPCOMCallback( 495 CallbackObject* aCallback, const nsIID& aIID) const { 496 MOZ_ASSERT(NS_IsMainThread()); 497 if (!aCallback) { 498 return nullptr; 499 } 500 501 // We don't init the AutoJSAPI with our callback because we don't want it 502 // reporting errors to its global's onerror handlers. 503 AutoJSAPI jsapi; 504 jsapi.Init(); 505 JSContext* cx = jsapi.cx(); 506 507 JS::Rooted<JSObject*> callback(cx, aCallback->CallbackOrNull()); 508 if (!callback) { 509 return nullptr; 510 } 511 512 JSAutoRealm ar(cx, aCallback->CallbackGlobalOrNull()); 513 514 RefPtr<nsXPCWrappedJS> wrappedJS; 515 nsresult rv = nsXPCWrappedJS::GetNewOrUsed(cx, callback, aIID, 516 getter_AddRefs(wrappedJS)); 517 if (NS_FAILED(rv) || !wrappedJS) { 518 return nullptr; 519 } 520 521 nsCOMPtr<nsISupports> retval; 522 rv = wrappedJS->QueryInterface(aIID, getter_AddRefs(retval)); 523 if (NS_FAILED(rv)) { 524 return nullptr; 525 } 526 527 return retval.forget(); 528 } 529 530 } // namespace mozilla::dom