https://github.com/ojhunt created https://github.com/llvm/llvm-project/pull/242924
I encountered this issue while working on EricWF's contracts implementation. We were storing the exception slots inside the coroutine's frame, but it is possible for an exception to occur before the frame is actually allocated or constructed so that is unsound. >From 0a9abf0193f435f9934aaaaf0e871d13a81ccb24 Mon Sep 17 00:00:00 2001 From: Oliver Hunt <[email protected]> Date: Sat, 14 Nov 2026 22:22:20 -0800 Subject: [PATCH] [clang][CodeGen] Coroutine wxception slots can't be stored in the frame I encountered this issue while working on EricWF's contracts implementation. We were storing the exception slots inside the coroutine's frame, but it is possible for an exception to occur before the frame is actually allocated or constructed so that is unsound. --- clang/lib/CodeGen/CodeGenFunction.cpp | 11 +++++++ .../coro-exception-slots.cpp | 29 +++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 clang/test/CodeGenCoroutines/coro-exception-slots.cpp diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp index 33434526d86325..39de4faa1dbe52 100644 --- a/clang/lib/CodeGen/CodeGenFunction.cpp +++ b/clang/lib/CodeGen/CodeGenFunction.cpp @@ -480,6 +480,17 @@ void CodeGenFunction::FinishFunction(SourceLocation EndLoc) { } } + // The exception slots of a coroutine need to be stored outside of the + // coroutine frame because they can be triggered before the coroutine frame + // exists. Notable examples are failing during the construction of the promise + // itself, and in future through contract assertion failures. + if (isCoroutine()) { + for (llvm::Value *Slot : {ExceptionSlot, (llvm::Value *)EHSelectorSlot}) + if (auto *AI = dyn_cast_or_null<llvm::AllocaInst>(Slot)) + AI->setMetadata(llvm::LLVMContext::MD_coro_outside_frame, + llvm::MDNode::get(getLLVMContext(), {})); + } + EmitIfUsed(*this, EHResumeBlock); EmitIfUsed(*this, TerminateLandingPad); EmitIfUsed(*this, TerminateHandler); diff --git a/clang/test/CodeGenCoroutines/coro-exception-slots.cpp b/clang/test/CodeGenCoroutines/coro-exception-slots.cpp new file mode 100644 index 00000000000000..8788f1e1e7aa22 --- /dev/null +++ b/clang/test/CodeGenCoroutines/coro-exception-slots.cpp @@ -0,0 +1,29 @@ +// RUN: %clang_cc1 -std=c++20 -triple arm64-apple-macos -fcxx-exceptions \ +// RUN: -fexceptions -I %S/Inputs -disable-llvm-passes -emit-llvm \ +// RUN: -o - %s | FileCheck %s + + +#include "coroutine.h" + +struct Task { + struct promise_type { + promise_type(); + Task get_return_object(); + struct InitialAwaiter { + bool await_ready(); + void await_suspend(std::coroutine_handle<>); + void await_resume(); + }; + InitialAwaiter initial_suspend(); + std::suspend_always final_suspend() noexcept; + void return_void(); + void unhandled_exception(); + }; +}; + +Task coro() { + co_await std::suspend_always{}; +} +// CHECK-LABEL: define {{.*}} @_Z4corov( +// CHECK: %exn.slot = alloca ptr, align 8, !coro.outside.frame +// CHECK: %ehselector.slot = alloca i32, align 4, !coro.outside.frame _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
