llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-codegen

@llvm/pr-subscribers-coroutines

Author: Oliver Hunt (ojhunt)

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/242924.diff


2 Files Affected:

- (modified) clang/lib/CodeGen/CodeGenFunction.cpp (+11) 
- (added) clang/test/CodeGenCoroutines/coro-exception-slots.cpp (+29) 


``````````diff
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

``````````

</details>


https://github.com/llvm/llvm-project/pull/242924
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to