diff --git a/patches/v8/2023-06-22-Implement-Promise-Context-Tagging.patch b/patches/v8/2023-06-22-Implement-Promise-Context-Tagging.patch index 77fe7f1646e..58f711a3044 100644 --- a/patches/v8/2023-06-22-Implement-Promise-Context-Tagging.patch +++ b/patches/v8/2023-06-22-Implement-Promise-Context-Tagging.patch @@ -3,6 +3,20 @@ From: James M Snell Date: Thu, 22 Jun 2023 15:29:26 -0700 Subject: Implement Promise Context Tagging +Every promise records the isolate's current promise context tag, the +PromiseContextTag root, which Isolate::PromiseContextScope sets. +Outside any scope the tag is the hole. Following a pending promise +whose tag is neither the hole nor the current tag calls the +embedder's PromiseCrossContextCallback, which may return another +promise to follow instead. Builtins copy and compare the tag inline, +and call the runtime only when the tags differ. + +Optimized code allocates promises inline while the PromiseHook +protector holds, without tagging them, so setting the callback +invalidates the protector. PromiseContextTagTest checks the promises +that Maglev and TurboFan would otherwise allocate inline. + +Signed-off-by: James M Snell diff --git a/include/v8-callbacks.h b/include/v8-callbacks.h index bca245b6d666d134fe55939cd8426f10bf0fd69d..61e2d8f212af5096ec2c9359766331309777071a 100644 @@ -58,7 +72,7 @@ index 26272489eb2348b1ae3960d7cbd39cc8fdc3c919..2b4eb9c3df147c96078bf619bd92a8e4 #endif // INCLUDE_V8_ISOLATE_H_ diff --git a/src/api/api.cc b/src/api/api.cc -index 07bca31a38d110cc7d49c9103223ac06d305cbe6..2b7a7508b1f58ef50e62679327eda0c596bb22b7 100644 +index 07bca31a38d110cc7d49c9103223ac06d305cbe6..7d8971c68d61bda25ec27c34a665a46032368161 100644 --- a/src/api/api.cc +++ b/src/api/api.cc @@ -12799,6 +12799,25 @@ std::string SourceLocation::ToString() const { @@ -77,7 +91,7 @@ index 07bca31a38d110cc7d49c9103223ac06d305cbe6..2b7a7508b1f58ef50e62679327eda0c5 + DCHECK(!isolate_->has_promise_context_tag()); + DCHECK(!tag.IsEmpty()); + i::Handle handle = Utils::OpenHandle(*tag); -+ isolate_->set_promise_context_tag(*handle); ++ isolate_->set_promise_context_tag(handle); +} + +Isolate::PromiseContextScope::~PromiseContextScope() { @@ -88,7 +102,7 @@ index 07bca31a38d110cc7d49c9103223ac06d305cbe6..2b7a7508b1f58ef50e62679327eda0c5 #ifdef ENABLE_SLOW_DCHECKS diff --git a/src/builtins/promise-abstract-operations.tq b/src/builtins/promise-abstract-operations.tq -index 1d8db469bad01dfaa105cf90abb2932bb9a664c3..e821fd2469f7977761f3d4bc280518fa9300a712 100644 +index 1d8db469bad01dfaa105cf90abb2932bb9a664c3..4e5429cb8f278daaf2672994a170fe2b1c01cc3c 100644 --- a/src/builtins/promise-abstract-operations.tq +++ b/src/builtins/promise-abstract-operations.tq @@ -14,6 +14,9 @@ extern transitioning runtime PromiseRevokeReject( @@ -101,11 +115,13 @@ index 1d8db469bad01dfaa105cf90abb2932bb9a664c3..e821fd2469f7977761f3d4bc280518fa } // https://tc39.es/ecma262/#sec-promise-abstract-operations -@@ -475,12 +478,14 @@ transitioning macro PerformPromiseThenImpl( +@@ -475,12 +478,16 @@ transitioning macro PerformPromiseThenImpl( // PromiseReaction holding both the onFulfilled and onRejected callbacks. // Once the {promise} is resolved we decide on the concrete handler to // push onto the microtask queue. -+ const delegate = runtime::PromiseContextCheck(promise); ++ const delegate = NeedsPromiseContextSwitch(promise) ? ++ runtime::PromiseContextCheck(promise) : ++ promise; const promiseReactions = - UnsafeCast<(Zero | PromiseReaction)>(promise.reactions_or_result); + UnsafeCast<(Zero | PromiseReaction)>(delegate.reactions_or_result); @@ -118,7 +134,7 @@ index 1d8db469bad01dfaa105cf90abb2932bb9a664c3..e821fd2469f7977761f3d4bc280518fa } else { const reactionsOrResult = promise.reactions_or_result; let microtask: PromiseReactionJobTask; -@@ -502,8 +507,8 @@ transitioning macro PerformPromiseThenImpl( +@@ -502,8 +509,8 @@ transitioning macro PerformPromiseThenImpl( } } EnqueueMicrotask(handlerContext, microtask); @@ -128,66 +144,57 @@ index 1d8db469bad01dfaa105cf90abb2932bb9a664c3..e821fd2469f7977761f3d4bc280518fa } transitioning javascript builtin PerformPromiseThenFunction( -diff --git a/src/builtins/promise-constructor.tq b/src/builtins/promise-constructor.tq -index 50677631b5399453eebc6b149272431f74b1fce6..c652bd836b27805865e0a902ef9cf7c1ff254646 100644 ---- a/src/builtins/promise-constructor.tq -+++ b/src/builtins/promise-constructor.tq -@@ -8,6 +8,9 @@ - namespace runtime { - extern transitioning runtime PromiseHookInit( - implicit context: Context)(Object, Object): JSAny; -+ -+extern transitioning runtime PromiseContextInit( -+ implicit context: Context)(JSPromise): JSAny; - } - - // https://tc39.es/ecma262/#sec-promise-constructor -@@ -62,6 +65,7 @@ transitioning javascript builtin PromiseConstructor( - result = UnsafeCast( - FastNewObject(context, promiseFun, UnsafeCast(newTarget))); - PromiseInit(result); -+ runtime::PromiseContextInit(result); - RunAnyPromiseHookInit(result, Undefined); - } - diff --git a/src/builtins/promise-misc.tq b/src/builtins/promise-misc.tq -index c85bb3b4591239a0f137dc0ed3c5cc80e169d66f..5bff2e5d7cee2e4e78c5d2c68b4c1d27c6646bd1 100644 +index c85bb3b4591239a0f137dc0ed3c5cc80e169d66f..b0b4a18e00faffe2efb7ce807e7ab609adc1ff1d 100644 --- a/src/builtins/promise-misc.tq +++ b/src/builtins/promise-misc.tq -@@ -54,6 +54,7 @@ macro PromiseInit(promise: JSPromise): void { +@@ -29,6 +29,14 @@ extern macro PromiseBuiltinsAssembler::IsIsolatePromiseHookEnabled(uint32): + bool; + + extern macro PromiseBuiltinsAssembler::PromiseHookFlags(): uint32; ++extern macro LoadPromiseContextTag(): Object; ++ ++// Whether following or settling {promise} has to go through the embedder. ++macro NeedsPromiseContextSwitch(promise: JSPromise): bool { ++ const tag = promise.context_tag; ++ return !TaggedEqual(tag, TheHole) && ++ !TaggedEqual(tag, LoadPromiseContextTag()); ++} + + namespace macros { + extern macro GetContinuationPreservedEmbedderData(): Object; +@@ -54,6 +62,7 @@ macro PromiseInit(promise: JSPromise): void { is_silent: false, async_task_id: kInvalidAsyncTaskId }); -+ promise.context_tag = kZero; ++ promise.context_tag = LoadPromiseContextTag(); promise_internal::ZeroOutEmbedderOffsets(promise); } -@@ -74,6 +75,7 @@ macro InnerNewJSPromise(implicit context: Context)(): JSPromise { +@@ -74,6 +83,7 @@ macro InnerNewJSPromise(implicit context: Context)(): JSPromise { is_silent: false, async_task_id: kInvalidAsyncTaskId }); -+ promise.context_tag = kZero; ++ promise.context_tag = LoadPromiseContextTag(); return promise; } -@@ -273,6 +275,7 @@ transitioning macro NewJSPromise(implicit context: Context)(parent: Object): - JSPromise { - const instance = InnerNewJSPromise(); - PromiseInit(instance); -+ runtime::PromiseContextInit(instance); - RunAnyPromiseHookInit(instance, parent); - return instance; - } -@@ -296,6 +299,7 @@ transitioning macro NewJSPromise( - instance.reactions_or_result = result; - instance.SetStatus(status); - promise_internal::ZeroOutEmbedderOffsets(instance); -+ runtime::PromiseContextInit(instance); - RunAnyPromiseHookInit(instance, Undefined); - return instance; - } +diff --git a/src/codegen/code-stub-assembler.h b/src/codegen/code-stub-assembler.h +index 63b3b9c92d22136a87e52584c42d7870569b36dc..94a0f719dff41a3aa33d335a693cc290a054b652 100644 +--- a/src/codegen/code-stub-assembler.h ++++ b/src/codegen/code-stub-assembler.h +@@ -4592,6 +4592,9 @@ class V8_EXPORT_PRIVATE CodeStubAssembler + + // Promise helpers + TNode PromiseHookFlags(); ++ TNode LoadPromiseContextTag() { ++ return LoadRoot(RootIndex::kPromiseContextTag); ++ } + TNode HasAsyncEventDelegate(); + #ifdef V8_ENABLE_JAVASCRIPT_PROMISE_HOOKS + TNode IsContextPromiseHookEnabled(TNode flags); diff --git a/src/compiler/js-create-lowering.cc b/src/compiler/js-create-lowering.cc -index 2b96d0eed11a5b22e17ed11f1050597936b4dea0..c1ad8ae285a1bcd1191687a5cb6c8c858bc0e60e 100644 +index 2b96d0eed11a5b22e17ed11f1050597936b4dea0..7b0ce1ff96c4a10b7ca12903bda68e0a6faf4524 100644 --- a/src/compiler/js-create-lowering.cc +++ b/src/compiler/js-create-lowering.cc @@ -1115,10 +1115,12 @@ Reduction JSCreateLowering::ReduceJSCreatePromise(Node* node) { @@ -195,7 +202,7 @@ index 2b96d0eed11a5b22e17ed11f1050597936b4dea0..c1ad8ae285a1bcd1191687a5cb6c8c85 offsetof(JSPromise, reactions_or_result_)), jsgraph()->ZeroConstant()); + a.Store(AccessBuilder::ForJSObjectOffset(JSPromise::kContextTagOffset), -+ jsgraph()->ZeroConstant()); ++ jsgraph()->TheHoleConstant()); static_assert(v8::Promise::kPending == 0); a.Store(AccessBuilder::ForJSObjectOffset(offsetof(JSPromise, flags_)), jsgraph()->ZeroConstant()); @@ -217,49 +224,48 @@ index f89b106d883569ad927d6864f1e63361a257907f..e6fa23f0640899a666776c925531bf63 } diff --git a/src/execution/isolate-inl.h b/src/execution/isolate-inl.h -index de710c8fad334d5fc29ee0d4627b4eb370e58550..fb7d83dd6fcff7aa7aba2ba5ad533fa8b877e1e1 100644 +index de710c8fad334d5fc29ee0d4627b4eb370e58550..38bbd3f3ea4031d7620541fed83c9bd05636a760 100644 --- a/src/execution/isolate-inl.h +++ b/src/execution/isolate-inl.h -@@ -129,6 +129,25 @@ bool Isolate::is_javascript_execution_allowed() const { +@@ -129,6 +129,33 @@ bool Isolate::is_javascript_execution_allowed() const { javascript_execution_dump(); } -+Tagged Isolate::promise_context_tag() { return promise_context_tag_; } ++Handle Isolate::promise_context_tag() { ++ return root_handle(RootIndex::kPromiseContextTag); ++} + +bool Isolate::has_promise_context_tag() { -+ return promise_context_tag_ != ReadOnlyRoots(this).the_hole_value(); ++ return heap()->promise_context_tag() != ReadOnlyRoots(this).the_hole_value(); +} + +void Isolate::clear_promise_context_tag() { -+ set_promise_context_tag(ReadOnlyRoots(this).the_hole_value()); ++ heap()->set_promise_context_tag(ReadOnlyRoots(this).the_hole_value()); +} + -+void Isolate::set_promise_context_tag(Tagged tag) { -+ promise_context_tag_ = tag; ++void Isolate::set_promise_context_tag(Handle tag) { ++ heap()->set_promise_context_tag(*tag); +} + +void Isolate::set_promise_cross_context_callback( + PromiseCrossContextCallback callback) { + promise_cross_context_callback_ = callback; ++ // Optimized code allocates promises inline as long as the PromiseHook ++ // protector holds, since inline allocation skips init hooks. It also leaves ++ // the promise untagged, so that its reactions would run in whichever context ++ // settles it. Context tagging is an init hook, so invalidate the protector ++ // to keep promise allocation in builtins and the runtime, which tag it. ++ if (callback != nullptr) UpdatePromiseHookProtector(); +} + #ifdef DEBUG Tagged Isolate::VerifyBuiltinsResult(Tagged result) { if (is_execution_terminating() && !v8_flags.strict_termination_checks) { diff --git a/src/execution/isolate.cc b/src/execution/isolate.cc -index 2834f8fbac67370a58e47c15882743cb0d7ff111..adb34befdad7cdc763e9f9ad2de6ddb9160245db 100644 +index 2834f8fbac67370a58e47c15882743cb0d7ff111..5553f07618687436f60fadd7fce3f2fa9a7cf81d 100644 --- a/src/execution/isolate.cc +++ b/src/execution/isolate.cc -@@ -680,6 +680,8 @@ void Isolate::Iterate(RootVisitor* v, ThreadLocalTop* thread) { - FullObjectSlot(&thread->context_)); - v->VisitRootPointer(Root::kStackRoots, nullptr, - FullObjectSlot(&thread->last_entered_context_)); -+ v->VisitRootPointer(Root::kStackRoots, nullptr, -+ FullObjectSlot(&promise_context_tag_)); - - for (v8::TryCatch* block = thread->try_catch_handler_; block != nullptr; - block = block->next_) { -@@ -6498,6 +6500,7 @@ bool Isolate::Init(SnapshotData* startup_snapshot_data, +@@ -6498,6 +6498,7 @@ bool Isolate::Init(SnapshotData* startup_snapshot_data, shared_heap_object_cache_.push_back(ReadOnlyRoots(this).undefined_value()); } @@ -267,7 +273,7 @@ index 2834f8fbac67370a58e47c15882743cb0d7ff111..adb34befdad7cdc763e9f9ad2de6ddb9 InitializeThreadLocal(); // Profiler has to be created after ThreadLocal is initialized -@@ -8716,5 +8719,40 @@ void Isolate::PrintNumberStringCacheStats(const char* comment, +@@ -8716,5 +8717,40 @@ void Isolate::PrintNumberStringCacheStats(const char* comment, PrintF("\n"); } @@ -309,17 +315,17 @@ index 2834f8fbac67370a58e47c15882743cb0d7ff111..adb34befdad7cdc763e9f9ad2de6ddb9 } // namespace internal } // namespace v8 diff --git a/src/execution/isolate.h b/src/execution/isolate.h -index c1917812619ca9141599bdd192343d7f6f6e4767..003cb03ac3f846680a47c22e7e3fcf816ce488f5 100644 +index c1917812619ca9141599bdd192343d7f6f6e4767..367f6dc9201abf8ed5fca7d6e0158d91cc8aff95 100644 --- a/src/execution/isolate.h +++ b/src/execution/isolate.h @@ -2479,6 +2479,15 @@ class V8_EXPORT_PRIVATE Isolate final : private HiddenFactory { v8::ExceptionContext callback_kind); void SetExceptionPropagationCallback(ExceptionPropagationCallback callback); -+ inline Tagged promise_context_tag(); ++ inline Handle promise_context_tag(); + inline bool has_promise_context_tag(); + inline void clear_promise_context_tag(); -+ inline void set_promise_context_tag(Tagged tag); ++ inline void set_promise_context_tag(Handle tag); + inline void set_promise_cross_context_callback( + PromiseCrossContextCallback callback); + MaybeHandle RunPromiseCrossContextCallback( @@ -328,12 +334,11 @@ index c1917812619ca9141599bdd192343d7f6f6e4767..003cb03ac3f846680a47c22e7e3fcf81 #ifdef V8_ENABLE_WASM_SIMD256_REVEC void set_wasm_revec_verifier_for_test( compiler::turboshaft::WasmRevecVerifier* verifier) { -@@ -3023,6 +3032,12 @@ class V8_EXPORT_PRIVATE Isolate final : private HiddenFactory { +@@ -3023,6 +3032,11 @@ class V8_EXPORT_PRIVATE Isolate final : private HiddenFactory { bool is_frozen_ = false; friend class HandleScopeImplementer; -+ Tagged promise_context_tag_; -+ PromiseCrossContextCallback promise_cross_context_callback_; ++ PromiseCrossContextCallback promise_cross_context_callback_ = nullptr; + bool in_promise_cross_context_callback_ = false; + + class PromiseCrossContextCallbackScope; @@ -341,7 +346,7 @@ index c1917812619ca9141599bdd192343d7f6f6e4767..003cb03ac3f846680a47c22e7e3fcf81 friend class GlobalSafepoint; friend class heap::HeapTester; friend class IsolateForPointerCompression; -@@ -3030,6 +3045,7 @@ class V8_EXPORT_PRIVATE Isolate final : private HiddenFactory { +@@ -3030,6 +3044,7 @@ class V8_EXPORT_PRIVATE Isolate final : private HiddenFactory { friend class IsolateGroup; friend class TestSerializer; friend class SharedHeapNoClientsTest; @@ -350,31 +355,27 @@ index c1917812619ca9141599bdd192343d7f6f6e4767..003cb03ac3f846680a47c22e7e3fcf81 // The current entered Isolate and its thread data. Do not access these diff --git a/src/heap/factory.cc b/src/heap/factory.cc -index 400a43dc58ae891a7f56d20335226d12d4210e72..4a738ed0eb86fd46c735fe7c84d946e6f7950b4f 100644 +index 400a43dc58ae891a7f56d20335226d12d4210e72..055690fc81af503d60661a3fb10c9e85b6abc7a1 100644 --- a/src/heap/factory.cc +++ b/src/heap/factory.cc -@@ -4991,6 +4991,12 @@ Handle Factory::NewJSPromiseWithoutHook() { +@@ -4991,6 +4991,7 @@ Handle Factory::NewJSPromiseWithoutHook() { DisallowGarbageCollection no_gc; Tagged raw = *promise; raw->set_reactions_or_result(Smi::zero(), SKIP_WRITE_BARRIER); -+ if (!isolate()->has_promise_context_tag()) { -+ raw->set_context_tag(Smi::zero(), SKIP_WRITE_BARRIER); -+ } else { -+ raw->set_context_tag(isolate()->promise_context_tag()); -+ } -+ ++ raw->set_context_tag(isolate()->heap()->promise_context_tag()); raw->set_flags(JSPromise::Flags()); // TODO(v8) remove once embedder data slots are always zero-initialized. InitEmbedderFields(*promise, Smi::zero()); diff --git a/src/maglev/maglev-reducer-inl.h b/src/maglev/maglev-reducer-inl.h -index 4bb1704d2ae9d7a31334363fa85d1b44f5dbdfa9..b2b10f80206ebf21f31b1c4b1a66585240db68ad 100644 +index 4bb1704d2ae9d7a31334363fa85d1b44f5dbdfa9..eb27f044079d7336f9fe23cd40651a673b329a42 100644 --- a/src/maglev/maglev-reducer-inl.h +++ b/src/maglev/maglev-reducer-inl.h -@@ -6263,9 +6263,10 @@ VirtualObject* MaglevReducer::CreateJSPromiseObject() { +@@ -6263,9 +6263,11 @@ VirtualObject* MaglevReducer::CreateJSPromiseObject() { vobj->set(offsetof(JSObject, elements_), GetRootConstant(RootIndex::kEmptyFixedArray)); vobj->set(offsetof(JSPromise, reactions_or_result_), GetSmiConstant(0)); -+ vobj->set(JSPromise::kContextTagOffset, GetSmiConstant(0)); ++ vobj->set(JSPromise::kContextTagOffset, ++ GetRootConstant(RootIndex::kTheHoleValue)); static_assert(v8::Promise::kPending == 0); vobj->set(offsetof(JSPromise, flags_), GetSmiConstant(0)); - static_assert(sizeof(JSPromise) == 5 * kTaggedSize); @@ -400,7 +401,7 @@ index 6526e792e6b1e515bae3d3fa4462f64d3fe67882..c2cef8c2a0d623949ef0845af43db0db void JSPromise::set_flags(Flags value) { flags_.store(value); } diff --git a/src/objects/js-promise.h b/src/objects/js-promise.h -index 9c6f404386aa1d883ebb782f57d81c466c6992e3..81f8a42bb12696508f6c43ed2eead5aaf53c3181 100644 +index 9c6f404386aa1d883ebb782f57d81c466c6992e3..86c3d78fcd4dc8d27999f73311cbb882e7b4db1e 100644 --- a/src/objects/js-promise.h +++ b/src/objects/js-promise.h @@ -38,6 +38,10 @@ V8_OBJECT class JSPromise : public JSObjectWithEmbedderSlots { @@ -418,7 +419,7 @@ index 9c6f404386aa1d883ebb782f57d81c466c6992e3..81f8a42bb12696508f6c43ed2eead5aa // Smi 0 terminated list of PromiseReaction objects in case the JSPromise // was not settled yet, otherwise the result. TaggedMember> reactions_or_result_; -+ // The context tag (workerd Promise Context Tagging extension). ++ // The hole (default) or a tag object set via Isolate::PromiseContextScope. + TaggedMember context_tag_; SmiBitFieldMember flags_; @@ -465,26 +466,28 @@ index 1403701e8d94fd0a20fded046ff75c0f98604023..2ddf8f86c16ab7c3c3f187dad8ccbea2 } void V8HeapExplorer::ExtractJSGeneratorObjectReferences( +diff --git a/src/roots/roots.h b/src/roots/roots.h +index 1abe1280bd9a33295c1728d1543dd2f7193adeb2..926135995c273e6334b81648c83dadd423fa85e6 100644 +--- a/src/roots/roots.h ++++ b/src/roots/roots.h +@@ -466,7 +466,8 @@ class RootVisitor; + V(FunctionTemplateInfo, error_stack_getter_fun_template, \ + ErrorStackGetterSharedFun) \ + V(FunctionTemplateInfo, error_stack_setter_fun_template, \ +- ErrorStackSetterSharedFun) ++ ErrorStackSetterSharedFun) \ ++ V(Object, promise_context_tag, PromiseContextTag) + + // Entries in this list are limited to Smis and are not visited during GC. + #define SMI_ROOT_LIST(V) \ diff --git a/src/runtime/runtime-promise.cc b/src/runtime/runtime-promise.cc -index af84ed30bf4e583dffbd253d732d0623bf0005c8..5c2adc6ed9663082571a8e5b88751309c54cb3bc 100644 +index af84ed30bf4e583dffbd253d732d0623bf0005c8..c595fe90bd5bf91230ca34e8200e7ca7ea94e4fa 100644 --- a/src/runtime/runtime-promise.cc +++ b/src/runtime/runtime-promise.cc -@@ -246,5 +246,41 @@ RUNTIME_FUNCTION(Runtime_ConstructSuppressedError) { +@@ -246,5 +246,29 @@ RUNTIME_FUNCTION(Runtime_ConstructSuppressedError) { return *result; } -+RUNTIME_FUNCTION(Runtime_PromiseContextInit) { -+ HandleScope scope(isolate); -+ DCHECK_EQ(1, args.length()); -+ if (!isolate->has_promise_context_tag()) { -+ args.at(0)->set_context_tag(Smi::zero()); -+ } else { -+ CHECK(!IsUndefined(isolate->promise_context_tag())); -+ args.at(0)->set_context_tag(isolate->promise_context_tag()); -+ } -+ return ReadOnlyRoots(isolate).undefined_value(); -+} -+ +RUNTIME_FUNCTION(Runtime_PromiseContextCheck) { + HandleScope scope(isolate); + DCHECK_EQ(1, args.length()); @@ -494,8 +497,8 @@ index af84ed30bf4e583dffbd253d732d0623bf0005c8..5c2adc6ed9663082571a8e5b88751309 + // If promise.context_tag() is strict equal to isolate.promise_context_tag(), + // or if the promise being checked does not have a context tag, we'll just + // return promise directly. -+ Tagged obj = promise->context_tag(); -+ if (obj == Smi::zero() || obj == isolate->promise_context_tag()) { ++ Handle obj(promise->context_tag(), isolate); ++ if (IsTheHole(*obj) || obj.is_identical_to(isolate->promise_context_tag())) { + return *promise; + } + @@ -512,10 +515,10 @@ index af84ed30bf4e583dffbd253d732d0623bf0005c8..5c2adc6ed9663082571a8e5b88751309 } // namespace internal } // namespace v8 diff --git a/src/runtime/runtime.h b/src/runtime/runtime.h -index 1b4e76d9f239918c9421048ffab4fb76502c201f..09926ac1d875e53428b1af0afbb7a3fe17434361 100644 +index 1b4e76d9f239918c9421048ffab4fb76502c201f..ce24b24abb2a4f706f3fde36ef46945984574fb1 100644 --- a/src/runtime/runtime.h +++ b/src/runtime/runtime.h -@@ -439,18 +439,20 @@ constexpr bool CanTriggerGC(T... properties) { +@@ -439,18 +439,19 @@ constexpr bool CanTriggerGC(T... properties) { F(StrictNotEqual, 2, 1) \ F(ReferenceEqual, 2, 1) @@ -543,8 +546,122 @@ index 1b4e76d9f239918c9421048ffab4fb76502c201f..09926ac1d875e53428b1af0afbb7a3fe + F(ConstructSuppressedError, 3, 1) \ + F(ConstructAggregateErrorHelper, 4, 1) \ + F(ConstructInternalAggregateErrorHelper, -1 /* <= 5*/, 1) \ -+ F(PromiseContextInit, 1, 1) \ + F(PromiseContextCheck, 1, 1) #define FOR_EACH_INTRINSIC_PROXY(F, I) \ F(CheckProxyGetSetTrapResult, 4, 1) \ +diff --git a/test/unittests/BUILD.gn b/test/unittests/BUILD.gn +index 27ae1d5c174be15aa70666f0cf3c208bac6b860a..c31c66884947ac27f886eb45d3f9549105d40ad6 100644 +--- a/test/unittests/BUILD.gn ++++ b/test/unittests/BUILD.gn +@@ -286,6 +286,7 @@ v8_cluster_source_set("v8_unittests_sources") { + "api/gc-callbacks-unittest.cc", + "api/interceptor-unittest.cc", + "api/isolate-unittest.cc", ++ "api/promise-context-tag-unittest.cc", + "api/remote-object-unittest.cc", + "api/resource-constraints-unittest.cc", + "api/smi-tagging-unittest.cc", +diff --git a/test/unittests/api/promise-context-tag-unittest.cc b/test/unittests/api/promise-context-tag-unittest.cc +new file mode 100644 +index 0000000000000000000000000000000000000000..f082201e39177f73b59af387707915eabecac64a +--- /dev/null ++++ b/test/unittests/api/promise-context-tag-unittest.cc +@@ -0,0 +1,97 @@ ++// Copyright 2026 the V8 project authors. All rights reserved. ++// Use of this source code is governed by a BSD-style license that can be ++// found in the LICENSE file. ++ ++#include ++ ++#include "include/v8-context.h" ++#include "include/v8-function.h" ++#include "include/v8-isolate.h" ++#include "include/v8-local-handle.h" ++#include "include/v8-object.h" ++#include "include/v8-promise.h" ++#include "src/flags/flags.h" ++#include "test/common/flag-utils.h" ++#include "test/unittests/test-utils.h" ++#include "testing/gtest/include/gtest/gtest.h" ++ ++namespace v8 { ++namespace { ++ ++// Once optimized, each of these allocates the promise it returns inline, unless ++// promise allocation is kept out of optimized code. ++constexpr const char* kDerivers[] = { ++ "(base) => base.then((value) => value)", ++ "(base) => base.catch(() => {})", ++ "(base) => base.finally(() => {})", ++ "(base) => Promise.all([base]).then(([value]) => value)", ++ "async (base) => { await base; }", ++ "(base) => new Promise((resolve) => { base.then(resolve); })", ++}; ++ ++// Stores the tag of the promise being followed in the global `followedTag`. ++MaybeLocal RecordFollowedTag(Local context, ++ Local promise, ++ Local tag) { ++ Isolate* isolate = Isolate::GetCurrent(); ++ CHECK(context->Global() ++ ->Set(context, String::NewFromUtf8Literal(isolate, "followedTag"), ++ tag) ++ .FromJust()); ++ return promise; ++} ++ ++class PromiseContextTagTest : public TestWithContext { ++ protected: ++ // Optimizes each deriver with the natives syntax function `optimize`, derives ++ // a promise from a pending one under one context tag, and follows the derived ++ // promise under another. The follow must see the first tag, as it does when ++ // the deriver is not optimized. ++ void ExpectOptimizedDerivedPromisesTagged(const std::string& optimize, ++ const std::string& active_tier) { ++ i::FlagScope natives_syntax(&i::v8_flags.allow_natives_syntax, true); ++ isolate()->SetPromiseCrossContextCallback(RecordFollowedTag); ++ ++ for (const char* deriver : kDerivers) { ++ SCOPED_TRACE(deriver); ++ SetGlobalProperty("derive", RunJS(deriver)); ++ RunJS(("%PrepareFunctionForOptimization(derive);" ++ "derive(Promise.resolve());" ++ "derive(Promise.resolve());" ++ "%" + ++ optimize + "(derive);") ++ .c_str()); ++ ++ Local creating_tag = Object::New(isolate()); ++ { ++ Isolate::PromiseContextScope scope(isolate(), creating_tag); ++ RunJS("var derived = derive(new Promise(() => {}));"); ++ } ++ // The deriver must have run optimized for the check below to mean ++ // anything. ++ ASSERT_TRUE(RunJS(("%" + active_tier + "(derive)").c_str())->IsTrue()); ++ ++ RunJS("var followedTag = undefined;"); ++ { ++ Isolate::PromiseContextScope scope(isolate(), Object::New(isolate())); ++ RunJS("derived.then(() => {});"); ++ } ++ EXPECT_TRUE(RunJS("followedTag")->StrictEquals(creating_tag)); ++ } ++ } ++}; ++ ++TEST_F(PromiseContextTagTest, MaglevTagsPromises) { ++ if (!i::v8_flags.maglev) GTEST_SKIP() << "Maglev is disabled"; ++ ExpectOptimizedDerivedPromisesTagged("OptimizeMaglevOnNextCall", ++ "ActiveTierIsMaglev"); ++} ++ ++TEST_F(PromiseContextTagTest, TurbofanTagsPromises) { ++ if (!i::v8_flags.turbofan) GTEST_SKIP() << "TurboFan is disabled"; ++ ExpectOptimizedDerivedPromisesTagged("OptimizeFunctionOnNextCall", ++ "ActiveTierIsTurbofan"); ++} ++ ++} // namespace ++} // namespace v8 diff --git a/patches/v8/2024-03-02-Add-ValueSerializer-SetTreatFunctionsAsHostObjects.patch b/patches/v8/2024-03-02-Add-ValueSerializer-SetTreatFunctionsAsHostObjects.patch index a5cb3429db0..af48c46a58d 100644 --- a/patches/v8/2024-03-02-Add-ValueSerializer-SetTreatFunctionsAsHostObjects.patch +++ b/patches/v8/2024-03-02-Add-ValueSerializer-SetTreatFunctionsAsHostObjects.patch @@ -30,7 +30,7 @@ index 240fd2da3b1eb2c7858b026525a963402bcb5c62..c9234089b2e663ea0ea8f495af59d64d * Write raw data in various common formats to the buffer. * Note that integer types are written in base-128 varint format, not with a diff --git a/src/api/api.cc b/src/api/api.cc -index 2b7a7508b1f58ef50e62679327eda0c596bb22b7..d402580f59e2843a6718e40bac5ba0b8e461c536 100644 +index 7d8971c68d61bda25ec27c34a665a46032368161..10efa04fe619769484ebc34918362d051efc0602 100644 --- a/src/api/api.cc +++ b/src/api/api.cc @@ -3755,6 +3755,10 @@ ValueSerializer::ReleaseSharedImmutableBackingStores() { diff --git a/patches/v8/2024-12-04-Add-ValueSerializer-SetTreatProxiesAsHostObjects.patch b/patches/v8/2024-12-04-Add-ValueSerializer-SetTreatProxiesAsHostObjects.patch index c6130a83156..6c412413597 100644 --- a/patches/v8/2024-12-04-Add-ValueSerializer-SetTreatProxiesAsHostObjects.patch +++ b/patches/v8/2024-12-04-Add-ValueSerializer-SetTreatProxiesAsHostObjects.patch @@ -30,7 +30,7 @@ index c9234089b2e663ea0ea8f495af59d64de32622a7..5be6fcf6465fcc5698f260bdfad22939 * Write raw data in various common formats to the buffer. * Note that integer types are written in base-128 varint format, not with a diff --git a/src/api/api.cc b/src/api/api.cc -index d402580f59e2843a6718e40bac5ba0b8e461c536..eea63e5e43cfb2899ea2e327e50881fe61fb0c25 100644 +index 10efa04fe619769484ebc34918362d051efc0602..b32039accffdc4f118b3e68274aa8aa7b16561e7 100644 --- a/src/api/api.cc +++ b/src/api/api.cc @@ -3759,6 +3759,10 @@ void ValueSerializer::SetTreatFunctionsAsHostObjects(bool mode) { diff --git a/patches/v8/2025-07-01-Implement-additional-Exception-construction-methods.patch b/patches/v8/2025-07-01-Implement-additional-Exception-construction-methods.patch index 6d00454a5a0..0928a2cf44f 100644 --- a/patches/v8/2025-07-01-Implement-additional-Exception-construction-methods.patch +++ b/patches/v8/2025-07-01-Implement-additional-Exception-construction-methods.patch @@ -25,7 +25,7 @@ index 74f57311a5257e8fdba3a060e9f6844a26a6dd69..88ba0737664800d8bcc1e091d9648ca7 /** * Creates an error message for the given exception. diff --git a/src/api/api.cc b/src/api/api.cc -index eea63e5e43cfb2899ea2e327e50881fe61fb0c25..8d4b0a95ac795d32a26e440783c0fdb217afe937 100644 +index b32039accffdc4f118b3e68274aa8aa7b16561e7..467e5e7d45f44c82e43c91f1a7f3dadeeab0a356 100644 --- a/src/api/api.cc +++ b/src/api/api.cc @@ -11540,6 +11540,10 @@ DEFINE_ERROR(WasmCompileError, wasm_compile_error) diff --git a/patches/v8/2025-11-06-Add-v8-String-IsFlat-API.patch b/patches/v8/2025-11-06-Add-v8-String-IsFlat-API.patch index eeecc66372f..0f95d203f85 100644 --- a/patches/v8/2025-11-06-Add-v8-String-IsFlat-API.patch +++ b/patches/v8/2025-11-06-Add-v8-String-IsFlat-API.patch @@ -24,7 +24,7 @@ index fe7a5f83a46a71ad7b8e3ea5341a0921632643e6..6f33e4c2420f3b0887537be97915f9df enum { kNone = 0, diff --git a/src/api/api.cc b/src/api/api.cc -index 8d4b0a95ac795d32a26e440783c0fdb217afe937..7861de8445ce4b4137ff946c4fc2d335096e3a4e 100644 +index 467e5e7d45f44c82e43c91f1a7f3dadeeab0a356..ff8785d4235d6d6e30d2c9868247d0093122abb0 100644 --- a/src/api/api.cc +++ b/src/api/api.cc @@ -6021,6 +6021,10 @@ bool String::IsOneByte() const { diff --git a/patches/v8/2026-06-05-Implement-cross-request-context-promise-resolve-handling.patch b/patches/v8/2026-06-05-Implement-cross-request-context-promise-resolve-handling.patch index f3664d28ea2..94ed090b8f9 100644 --- a/patches/v8/2026-06-05-Implement-cross-request-context-promise-resolve-handling.patch +++ b/patches/v8/2026-06-05-Implement-cross-request-context-promise-resolve-handling.patch @@ -49,10 +49,10 @@ index 4ab150ceb70386a84a6eefa864dda11b545ecaa1..7340344a61d2069ebffe9b329e322296 Isolate() = delete; ~Isolate() = delete; diff --git a/src/api/api.cc b/src/api/api.cc -index 7861de8445ce4b4137ff946c4fc2d335096e3a4e..38ed0abb3a65c583b06ca6105ff11fdd4c98d7bc 100644 +index ff8785d4235d6d6e30d2c9868247d0093122abb0..38ed0abb3a65c583b06ca6105ff11fdd4c98d7bc 100644 --- a/src/api/api.cc +++ b/src/api/api.cc -@@ -12821,13 +12821,19 @@ void Isolate::SetPromiseCrossContextCallback( +@@ -12821,6 +12821,12 @@ void Isolate::SetPromiseCrossContextCallback( isolate->set_promise_cross_context_callback(callback); } @@ -65,29 +65,11 @@ index 7861de8445ce4b4137ff946c4fc2d335096e3a4e..38ed0abb3a65c583b06ca6105ff11fdd Isolate::PromiseContextScope::PromiseContextScope(Isolate* isolate, v8::Local tag) : isolate_(reinterpret_cast(isolate)) { - DCHECK(!isolate_->has_promise_context_tag()); - DCHECK(!tag.IsEmpty()); - i::Handle handle = Utils::OpenHandle(*tag); -- isolate_->set_promise_context_tag(*handle); -+ isolate_->set_promise_context_tag(handle); - } - - Isolate::PromiseContextScope::~PromiseContextScope() { diff --git a/src/builtins/promise-abstract-operations.tq b/src/builtins/promise-abstract-operations.tq -index e821fd2469f7977761f3d4bc280518fa9300a712..256083d852c5c877ec5e14ef5118b79a6e295a06 100644 +index 4e5429cb8f278daaf2672994a170fe2b1c01cc3c..4f4b62c7d6b1e046a9e50f102dab3aa8e5a14cef 100644 --- a/src/builtins/promise-abstract-operations.tq +++ b/src/builtins/promise-abstract-operations.tq -@@ -17,6 +17,9 @@ extern transitioning runtime PromiseRejectEventFromStack( - - extern transitioning runtime PromiseContextCheck( - implicit context: Context)(JSPromise): JSPromise; -+ -+extern transitioning runtime PromiseResolveContextCheck( -+ implicit context: Context)(JSPromise): JSAny; - } - - // https://tc39.es/ecma262/#sec-promise-abstract-operations -@@ -244,13 +247,16 @@ extern macro PromiseBuiltinsAssembler:: +@@ -244,13 +244,16 @@ extern macro PromiseBuiltinsAssembler:: transitioning builtin RejectPromise( implicit context: Context)(promise: JSPromise, reason: JSAny, debugEvent: Boolean): JSAny { @@ -100,13 +82,13 @@ index e821fd2469f7977761f3d4bc280518fa9300a712..256083d852c5c877ec5e14ef5118b79a // the complexity here and also avoids a couple of back and // forth between JavaScript and C++ land. - if (IsIsolatePromiseHookEnabledOrDebugIsActiveOrHasAsyncEventDelegate( -+ if (ToBoolean(runtime::PromiseResolveContextCheck(promise)) || ++ if (NeedsPromiseContextSwitch(promise) || + IsIsolatePromiseHookEnabledOrDebugIsActiveOrHasAsyncEventDelegate( promiseHookFlags) || !promise.HasHandler()) { // 7. If promise.[[PromiseIsHandled]] is false, perform diff --git a/src/builtins/promise-resolve.tq b/src/builtins/promise-resolve.tq -index 202180adbbae91a689a667c40d20b4b1b9cb6edd..5e618fcc7521d6c9ba15d83cca949099b9320264 100644 +index 202180adbbae91a689a667c40d20b4b1b9cb6edd..a40e3848b99f985287e39e29afe43c0508542101 100644 --- a/src/builtins/promise-resolve.tq +++ b/src/builtins/promise-resolve.tq @@ -96,7 +96,8 @@ transitioning builtin ResolvePromise( @@ -114,43 +96,17 @@ index 202180adbbae91a689a667c40d20b4b1b9cb6edd..5e618fcc7521d6c9ba15d83cca949099 // We can use pointer comparison here, since the {promise} is guaranteed // to be a JSPromise inside this function and thus is reference comparable. - if (IsIsolatePromiseHookEnabledOrDebugIsActiveOrHasAsyncEventDelegate() || -+ if (ToBoolean(runtime::PromiseResolveContextCheck(promise)) || ++ if (NeedsPromiseContextSwitch(promise) || + IsIsolatePromiseHookEnabledOrDebugIsActiveOrHasAsyncEventDelegate() || TaggedEqual(promise, resolution)) deferred { return runtime::ResolvePromise(promise, resolution); diff --git a/src/execution/isolate-inl.h b/src/execution/isolate-inl.h -index fb7d83dd6fcff7aa7aba2ba5ad533fa8b877e1e1..46bb8d6283ea8e00c03c3c0422f1afcc09ce28df 100644 +index 38bbd3f3ea4031d7620541fed83c9bd05636a760..823b8bfde06eca234e6b96b7a73b1f7a21af4de4 100644 --- a/src/execution/isolate-inl.h +++ b/src/execution/isolate-inl.h -@@ -129,18 +129,20 @@ bool Isolate::is_javascript_execution_allowed() const { - javascript_execution_dump(); - } - --Tagged Isolate::promise_context_tag() { return promise_context_tag_; } -+Handle Isolate::promise_context_tag() { -+ return root_handle(RootIndex::kPromiseContextTag); -+} - - bool Isolate::has_promise_context_tag() { -- return promise_context_tag_ != ReadOnlyRoots(this).the_hole_value(); -+ return heap()->promise_context_tag() != ReadOnlyRoots(this).the_hole_value(); - } - - void Isolate::clear_promise_context_tag() { -- set_promise_context_tag(ReadOnlyRoots(this).the_hole_value()); -+ heap()->set_promise_context_tag(ReadOnlyRoots(this).the_hole_value()); - } - --void Isolate::set_promise_context_tag(Tagged tag) { -- promise_context_tag_ = tag; -+void Isolate::set_promise_context_tag(Handle tag) { -+ heap()->set_promise_context_tag(*tag); - } - - void Isolate::set_promise_cross_context_callback( -@@ -148,6 +150,15 @@ void Isolate::set_promise_cross_context_callback( - promise_cross_context_callback_ = callback; +@@ -156,6 +156,15 @@ void Isolate::set_promise_cross_context_callback( + if (callback != nullptr) UpdatePromiseHookProtector(); } +void Isolate::set_promise_cross_context_resolve_callback( @@ -166,19 +122,10 @@ index fb7d83dd6fcff7aa7aba2ba5ad533fa8b877e1e1..46bb8d6283ea8e00c03c3c0422f1afcc Tagged Isolate::VerifyBuiltinsResult(Tagged result) { if (is_execution_terminating() && !v8_flags.strict_termination_checks) { diff --git a/src/execution/isolate.cc b/src/execution/isolate.cc -index adb34befdad7cdc763e9f9ad2de6ddb9160245db..5b8a996907168a7c303d62d10f8733a81c950e46 100644 +index 5553f07618687436f60fadd7fce3f2fa9a7cf81d..5b8a996907168a7c303d62d10f8733a81c950e46 100644 --- a/src/execution/isolate.cc +++ b/src/execution/isolate.cc -@@ -680,8 +680,6 @@ void Isolate::Iterate(RootVisitor* v, ThreadLocalTop* thread) { - FullObjectSlot(&thread->context_)); - v->VisitRootPointer(Root::kStackRoots, nullptr, - FullObjectSlot(&thread->last_entered_context_)); -- v->VisitRootPointer(Root::kStackRoots, nullptr, -- FullObjectSlot(&promise_context_tag_)); - - for (v8::TryCatch* block = thread->try_catch_handler_; block != nullptr; - block = block->next_) { -@@ -8754,5 +8752,23 @@ MaybeHandle Isolate::RunPromiseCrossContextCallback( +@@ -8752,5 +8752,23 @@ MaybeHandle Isolate::RunPromiseCrossContextCallback( return v8::Utils::OpenHandle(*result); } @@ -203,7 +150,7 @@ index adb34befdad7cdc763e9f9ad2de6ddb9160245db..5b8a996907168a7c303d62d10f8733a8 } // namespace internal } // namespace v8 diff --git a/src/execution/isolate.h b/src/execution/isolate.h -index 003cb03ac3f846680a47c22e7e3fcf816ce488f5..cbb8d10920fabb01f10ae26d0249115d39877fcc 100644 +index 367f6dc9201abf8ed5fca7d6e0158d91cc8aff95..cbb8d10920fabb01f10ae26d0249115d39877fcc 100644 --- a/src/execution/isolate.h +++ b/src/execution/isolate.h @@ -48,6 +48,7 @@ @@ -214,16 +161,8 @@ index 003cb03ac3f846680a47c22e7e3fcf816ce488f5..cbb8d10920fabb01f10ae26d0249115d #include "src/objects/tagged.h" #include "src/runtime/runtime.h" #include "src/sandbox/external-pointer-table.h" -@@ -2479,15 +2480,24 @@ class V8_EXPORT_PRIVATE Isolate final : private HiddenFactory { - v8::ExceptionContext callback_kind); - void SetExceptionPropagationCallback(ExceptionPropagationCallback callback); - -- inline Tagged promise_context_tag(); -+ inline Handle promise_context_tag(); - inline bool has_promise_context_tag(); - inline void clear_promise_context_tag(); -- inline void set_promise_context_tag(Tagged tag); -+ inline void set_promise_context_tag(Handle tag); +@@ -2485,9 +2486,18 @@ class V8_EXPORT_PRIVATE Isolate final : private HiddenFactory { + inline void set_promise_context_tag(Handle tag); inline void set_promise_cross_context_callback( PromiseCrossContextCallback callback); + inline void set_promise_cross_context_resolve_callback( @@ -241,13 +180,10 @@ index 003cb03ac3f846680a47c22e7e3fcf816ce488f5..cbb8d10920fabb01f10ae26d0249115d #ifdef V8_ENABLE_WASM_SIMD256_REVEC void set_wasm_revec_verifier_for_test( compiler::turboshaft::WasmRevecVerifier* verifier) { -@@ -3032,9 +3042,11 @@ class V8_EXPORT_PRIVATE Isolate final : private HiddenFactory { - bool is_frozen_ = false; +@@ -3033,7 +3043,10 @@ class V8_EXPORT_PRIVATE Isolate final : private HiddenFactory { friend class HandleScopeImplementer; -- Tagged promise_context_tag_; -- PromiseCrossContextCallback promise_cross_context_callback_; -+ PromiseCrossContextCallback promise_cross_context_callback_ = nullptr; + PromiseCrossContextCallback promise_cross_context_callback_ = nullptr; + PromiseCrossContextResolveCallback promise_cross_context_resolve_callback_ = + nullptr; bool in_promise_cross_context_callback_ = false; @@ -255,36 +191,8 @@ index 003cb03ac3f846680a47c22e7e3fcf816ce488f5..cbb8d10920fabb01f10ae26d0249115d class PromiseCrossContextCallbackScope; -diff --git a/src/heap/factory.cc b/src/heap/factory.cc -index 4a738ed0eb86fd46c735fe7c84d946e6f7950b4f..a8a72cdea9510b9a1129899071315e2f1b4fcd1d 100644 ---- a/src/heap/factory.cc -+++ b/src/heap/factory.cc -@@ -4989,18 +4989,17 @@ Handle Factory::NewJSPromiseWithoutHook() { - Handle promise = - Cast(NewJSObject(isolate()->promise_function())); - DisallowGarbageCollection no_gc; -- Tagged raw = *promise; -- raw->set_reactions_or_result(Smi::zero(), SKIP_WRITE_BARRIER); -+ promise->set_reactions_or_result(Smi::zero(), SKIP_WRITE_BARRIER); - if (!isolate()->has_promise_context_tag()) { -- raw->set_context_tag(Smi::zero(), SKIP_WRITE_BARRIER); -+ promise->set_context_tag(Smi::zero(), SKIP_WRITE_BARRIER); - } else { -- raw->set_context_tag(isolate()->promise_context_tag()); -+ promise->set_context_tag(*isolate()->promise_context_tag()); - } - -- raw->set_flags(JSPromise::Flags()); -+ promise->set_flags(JSPromise::Flags()); - // TODO(v8) remove once embedder data slots are always zero-initialized. - InitEmbedderFields(*promise, Smi::zero()); -- DCHECK_EQ(raw->GetEmbedderFieldCount(), v8::Promise::kEmbedderFieldCount); -+ DCHECK_EQ(promise->GetEmbedderFieldCount(), v8::Promise::kEmbedderFieldCount); - return promise; - } - diff --git a/src/objects/js-promise.h b/src/objects/js-promise.h -index 81f8a42bb12696508f6c43ed2eead5aaf53c3181..5d604b946b2b3b383bb9becde8ddce47199031d6 100644 +index 86c3d78fcd4dc8d27999f73311cbb882e7b4db1e..ae529703b60108755ca557fbc77ffa5de9095a0a 100644 --- a/src/objects/js-promise.h +++ b/src/objects/js-promise.h @@ -124,6 +124,13 @@ V8_OBJECT class JSPromise : public JSObjectWithEmbedderSlots { @@ -302,10 +210,10 @@ index 81f8a42bb12696508f6c43ed2eead5aaf53c3181..5d604b946b2b3b383bb9becde8ddce47 // Smi 0 terminated list of PromiseReaction objects in case the JSPromise // was not settled yet, otherwise the result. diff --git a/src/objects/objects.cc b/src/objects/objects.cc -index 20327c5b70660966b8cd6a3c7ef1df7738559fa0..1bc672bb9cf37f70444e1f70cd2a30fd6d9790b5 100644 +index 20327c5b70660966b8cd6a3c7ef1df7738559fa0..c2fdde2492d4fef62f53748cdc53b3d3729a1cb4 100644 --- a/src/objects/objects.cc +++ b/src/objects/objects.cc -@@ -4700,6 +4700,28 @@ Handle JSPromise::Fulfill(DirectHandle promise, +@@ -4700,6 +4700,27 @@ Handle JSPromise::Fulfill(DirectHandle promise, } #endif @@ -316,10 +224,9 @@ index 20327c5b70660966b8cd6a3c7ef1df7738559fa0..1bc672bb9cf37f70444e1f70cd2a30fd + // CHECK(kPending) crash when a cross-context promise is settled while + // another request's microtask checkpoint is active. + Handle obj(promise->context_tag(), isolate); -+ bool needs_promise_context_switch = -+ !(*obj == Smi::zero() || -+ obj.is_identical_to(isolate->promise_context_tag()) || -+ !isolate->has_promise_context_resolve_callback()); ++ bool needs_promise_context_switch = !( ++ IsTheHole(*obj) || obj.is_identical_to(isolate->promise_context_tag()) || ++ !isolate->has_promise_context_resolve_callback()); + if (needs_promise_context_switch) { + if (isolate + ->RunPromiseCrossContextResolveCallback( @@ -334,7 +241,7 @@ index 20327c5b70660966b8cd6a3c7ef1df7738559fa0..1bc672bb9cf37f70444e1f70cd2a30fd // 1. Assert: The value of promise.[[PromiseState]] is "pending". CHECK_EQ(Promise::kPending, promise->status()); -@@ -4757,6 +4779,25 @@ Handle JSPromise::Reject(DirectHandle promise, +@@ -4757,6 +4778,24 @@ Handle JSPromise::Reject(DirectHandle promise, isolate->RunAllPromiseHooks(PromiseHookType::kResolve, promise, isolate->factory()->undefined_value()); @@ -342,10 +249,9 @@ index 20327c5b70660966b8cd6a3c7ef1df7738559fa0..1bc672bb9cf37f70444e1f70cd2a30fd + // context, defer the ENTIRE settlement (status update + reaction + // triggering) to the owning IoContext. See JSPromise::Fulfill for details. + Handle obj(promise->context_tag(), isolate); -+ bool needs_promise_context_switch = -+ !(*obj == Smi::zero() || -+ obj.is_identical_to(isolate->promise_context_tag()) || -+ !isolate->has_promise_context_resolve_callback()); ++ bool needs_promise_context_switch = !( ++ IsTheHole(*obj) || obj.is_identical_to(isolate->promise_context_tag()) || ++ !isolate->has_promise_context_resolve_callback()); + if (needs_promise_context_switch) { + if (isolate + ->RunPromiseCrossContextResolveCallback( @@ -360,7 +266,7 @@ index 20327c5b70660966b8cd6a3c7ef1df7738559fa0..1bc672bb9cf37f70444e1f70cd2a30fd // 1. Assert: The value of promise.[[PromiseState]] is "pending". CHECK_EQ(Promise::kPending, promise->status()); -@@ -4885,6 +4926,33 @@ MaybeHandle JSPromise::Resolve(DirectHandle promise, +@@ -4885,6 +4924,33 @@ MaybeHandle JSPromise::Resolve(DirectHandle promise, } // static @@ -394,22 +300,8 @@ index 20327c5b70660966b8cd6a3c7ef1df7738559fa0..1bc672bb9cf37f70444e1f70cd2a30fd Handle JSPromise::TriggerPromiseReactions( Isolate* isolate, DirectHandle reactions, DirectHandle argument, PromiseReaction::Type type) { -diff --git a/src/roots/roots.h b/src/roots/roots.h -index 1abe1280bd9a33295c1728d1543dd2f7193adeb2..926135995c273e6334b81648c83dadd423fa85e6 100644 ---- a/src/roots/roots.h -+++ b/src/roots/roots.h -@@ -466,7 +466,8 @@ class RootVisitor; - V(FunctionTemplateInfo, error_stack_getter_fun_template, \ - ErrorStackGetterSharedFun) \ - V(FunctionTemplateInfo, error_stack_setter_fun_template, \ -- ErrorStackSetterSharedFun) -+ ErrorStackSetterSharedFun) \ -+ V(Object, promise_context_tag, PromiseContextTag) - - // Entries in this list are limited to Smis and are not visited during GC. - #define SMI_ROOT_LIST(V) \ diff --git a/src/runtime/runtime-promise.cc b/src/runtime/runtime-promise.cc -index 5c2adc6ed9663082571a8e5b88751309c54cb3bc..313f8422546686201ffc47b2d70c520773db851f 100644 +index c595fe90bd5bf91230ca34e8200e7ca7ea94e4fa..ab80f4c73f80e291de1d7bce161a4c10e20d8662 100644 --- a/src/runtime/runtime-promise.cc +++ b/src/runtime/runtime-promise.cc @@ -158,6 +158,7 @@ RUNTIME_FUNCTION(Runtime_RejectPromise) { @@ -420,64 +312,3 @@ index 5c2adc6ed9663082571a8e5b88751309c54cb3bc..313f8422546686201ffc47b2d70c5207 if (isolate->debug()->IsRestartFrameScheduled()) { return isolate->TerminateExecution(); } -@@ -252,8 +253,8 @@ RUNTIME_FUNCTION(Runtime_PromiseContextInit) { - if (!isolate->has_promise_context_tag()) { - args.at(0)->set_context_tag(Smi::zero()); - } else { -- CHECK(!IsUndefined(isolate->promise_context_tag())); -- args.at(0)->set_context_tag(isolate->promise_context_tag()); -+ CHECK(!IsUndefined(*isolate->promise_context_tag())); -+ args.at(0)->set_context_tag(*isolate->promise_context_tag()); - } - return ReadOnlyRoots(isolate).undefined_value(); - } -@@ -267,8 +268,9 @@ RUNTIME_FUNCTION(Runtime_PromiseContextCheck) { - // If promise.context_tag() is strict equal to isolate.promise_context_tag(), - // or if the promise being checked does not have a context tag, we'll just - // return promise directly. -- Tagged obj = promise->context_tag(); -- if (obj == Smi::zero() || obj == isolate->promise_context_tag()) { -+ Handle obj(promise->context_tag(), isolate); -+ if (*obj == Smi::zero() || -+ obj.is_identical_to(isolate->promise_context_tag())) { - return *promise; - } - -@@ -282,5 +284,23 @@ RUNTIME_FUNCTION(Runtime_PromiseContextCheck) { - return *result; - } - -+RUNTIME_FUNCTION(Runtime_PromiseResolveContextCheck) { -+ HandleScope scope(isolate); -+ DCHECK_EQ(1, args.length()); -+ Handle promise = args.at(0); -+ // If promise.context_tag() is strict equal to isolate.promise_context_tag(), -+ // or if the promise being checked does not have a context tag, or if the -+ // resolve callback has not been set, we'll just return false here to indicate -+ // that the default handling should be used. -+ Handle obj(promise->context_tag(), isolate); -+ if (*obj == Smi::zero() || -+ obj.is_identical_to(isolate->promise_context_tag()) || -+ !isolate->has_promise_context_resolve_callback()) { -+ return isolate->heap()->ToBoolean(false); -+ } -+ -+ return isolate->heap()->ToBoolean(true); -+} -+ - } // namespace internal - } // namespace v8 -diff --git a/src/runtime/runtime.h b/src/runtime/runtime.h -index 09926ac1d875e53428b1af0afbb7a3fe17434361..48ba54feb179b333f2903dde2d14a4230fa092a5 100644 ---- a/src/runtime/runtime.h -+++ b/src/runtime/runtime.h -@@ -452,7 +452,8 @@ constexpr bool CanTriggerGC(T... properties) { - F(ConstructAggregateErrorHelper, 4, 1) \ - F(ConstructInternalAggregateErrorHelper, -1 /* <= 5*/, 1) \ - F(PromiseContextInit, 1, 1) \ -- F(PromiseContextCheck, 1, 1) -+ F(PromiseContextCheck, 1, 1) \ -+ F(PromiseResolveContextCheck, 1, 1) - - #define FOR_EACH_INTRINSIC_PROXY(F, I) \ - F(CheckProxyGetSetTrapResult, 4, 1) \