Skip to content

Commit fd97553

Browse files
ChuanqiXu9razmser
authored andcommitted
[Coroutines] [CoroElide] Don't think exceptional terminator don't leak coro handle unconditionally any more
Close llvm#59723. The fundamental cause of the above issue is that we assumed the memory of coroutine frame can be released by stack unwinding automatically if the allocation of the coroutine frame is elided. But we missed one point: the stack unwinding has different semantics with the explicit coroutine_handle<>::destroy(). Since the latter is explicit so it shows the intention of the user. So we can blame the user to destroy the coroutine frame incorrectly in case of use-after-free happens. But we can't do so with stack unwinding. So after this patch, we won't think the exceptional terminator don't leak the coroutine handle unconditionally. Instead, we think the exceptional terminator will leak the coroutine handle too if the coroutine is leaked somewhere along the search path. Concretely for C++, we can think the exceptional terminator is not special any more. Maybe this may cause some performance regressions. But I've tested the motivating example (std::generator). And on the other side, the coroutine elision is a middle end opitmization and not a language feature. So we don't think we should blame such regressions especially we are correcting the miscompilations.
1 parent bed34e9 commit fd97553

File tree

3 files changed

+299
-23
lines changed

3 files changed

+299
-23
lines changed

Diff for: clang/test/CodeGenCoroutines/coro-halo.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// This tests that the coroutine heap allocation elision optimization could happen succesfully.
22
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 -O2 -emit-llvm %s -o - | FileCheck %s
3+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 -O2 -emit-llvm %s \
4+
// RUN: -fcxx-exceptions -fexceptions -o - | FileCheck %s
35

46
#include "Inputs/coroutine.h"
57
#include "Inputs/numeric.h"

Diff for: clang/test/CodeGenCoroutines/pr59723.cpp

+237
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
// This is reduced test case from https://github.com/llvm/llvm-project/issues/59723.
2+
// This is not a minimal reproducer intentionally to check the compiler's ability.
3+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 -fcxx-exceptions\
4+
// RUN: -fexceptions -O2 -emit-llvm %s -o - | FileCheck %s
5+
6+
#include "Inputs/coroutine.h"
7+
8+
// executor and operation base
9+
10+
class bug_any_executor;
11+
12+
struct bug_async_op_base
13+
{
14+
void invoke();
15+
16+
protected:
17+
18+
~bug_async_op_base() = default;
19+
};
20+
21+
class bug_any_executor
22+
{
23+
using op_type = bug_async_op_base;
24+
25+
public:
26+
27+
virtual ~bug_any_executor() = default;
28+
29+
// removing noexcept enables clang to find that the pointer has escaped
30+
virtual void post(op_type& op) noexcept = 0;
31+
32+
virtual void wait() noexcept = 0;
33+
};
34+
35+
class bug_thread_executor : public bug_any_executor
36+
{
37+
38+
public:
39+
40+
void start()
41+
{
42+
43+
}
44+
45+
~bug_thread_executor()
46+
{
47+
}
48+
49+
// although this implementation is not realy noexcept due to allocation but I have a real one that is and required to be noexcept
50+
virtual void post(bug_async_op_base& op) noexcept override;
51+
52+
virtual void wait() noexcept override
53+
{
54+
55+
}
56+
};
57+
58+
// task and promise
59+
60+
struct bug_final_suspend_notification
61+
{
62+
virtual std::coroutine_handle<> get_waiter() = 0;
63+
};
64+
65+
class bug_task;
66+
67+
class bug_task_promise
68+
{
69+
friend bug_task;
70+
public:
71+
72+
bug_task get_return_object() noexcept;
73+
74+
constexpr std::suspend_always initial_suspend() noexcept { return {}; }
75+
76+
std::suspend_always final_suspend() noexcept
77+
{
78+
return {};
79+
}
80+
81+
void unhandled_exception() noexcept;
82+
83+
constexpr void return_void() const noexcept {}
84+
85+
void get_result() const
86+
{
87+
88+
}
89+
};
90+
91+
template <class T, class U>
92+
T exchange(T &&t, U &&u) {
93+
T ret = t;
94+
t = u;
95+
return ret;
96+
}
97+
98+
class bug_task
99+
{
100+
friend bug_task_promise;
101+
using handle = std::coroutine_handle<>;
102+
using promise_t = bug_task_promise;
103+
104+
bug_task(handle coro, promise_t* p) noexcept : this_coro{ coro }, this_promise{ p }
105+
{
106+
107+
}
108+
109+
public:
110+
using promise_type = bug_task_promise;
111+
112+
bug_task(bug_task&& other) noexcept
113+
: this_coro{ exchange(other.this_coro, nullptr) }, this_promise{ exchange(other.this_promise, nullptr) } {
114+
115+
}
116+
117+
~bug_task()
118+
{
119+
if (this_coro)
120+
this_coro.destroy();
121+
}
122+
123+
constexpr bool await_ready() const noexcept
124+
{
125+
return false;
126+
}
127+
128+
handle await_suspend(handle waiter) noexcept
129+
{
130+
return this_coro;
131+
}
132+
133+
void await_resume()
134+
{
135+
return this_promise->get_result();
136+
}
137+
138+
handle this_coro;
139+
promise_t* this_promise;
140+
};
141+
142+
bug_task bug_task_promise::get_return_object() noexcept
143+
{
144+
return { std::coroutine_handle<bug_task_promise>::from_promise(*this), this };
145+
}
146+
147+
// spawn operation and spawner
148+
149+
template<class Handler>
150+
class bug_spawn_op final : public bug_async_op_base, bug_final_suspend_notification
151+
{
152+
Handler handler;
153+
bug_task task_;
154+
155+
public:
156+
157+
bug_spawn_op(Handler handler, bug_task&& t)
158+
: handler { handler }, task_{ static_cast<bug_task&&>(t) } {}
159+
160+
virtual std::coroutine_handle<> get_waiter() override
161+
{
162+
handler();
163+
return std::noop_coroutine();
164+
}
165+
};
166+
167+
class bug_spawner;
168+
169+
struct bug_spawner_awaiter
170+
{
171+
bug_spawner& s;
172+
std::coroutine_handle<> waiter;
173+
174+
bug_spawner_awaiter(bug_spawner& s) : s{ s } {}
175+
176+
bool await_ready() const noexcept;
177+
178+
void await_suspend(std::coroutine_handle<> coro);
179+
180+
void await_resume() {}
181+
};
182+
183+
class bug_spawner
184+
{
185+
friend bug_spawner_awaiter;
186+
187+
struct final_handler_t
188+
{
189+
bug_spawner& s;
190+
191+
void operator()()
192+
{
193+
s.awaiter_->waiter.resume();
194+
}
195+
};
196+
197+
public:
198+
199+
bug_spawner(bug_any_executor& ex) : ex_{ ex } {}
200+
201+
void spawn(bug_task&& t) {
202+
using op_t = bug_spawn_op<final_handler_t>;
203+
// move task into ptr
204+
op_t* ptr = new op_t(final_handler_t{ *this }, static_cast<bug_task&&>(t));
205+
++count_;
206+
ex_.post(*ptr); // ptr escapes here thus task escapes but clang can't deduce that unless post() is not noexcept
207+
}
208+
209+
bug_spawner_awaiter wait() noexcept { return { *this }; }
210+
211+
private:
212+
bug_any_executor& ex_; // if bug_thread_executor& is used instead enables clang to detect the escape of the promise
213+
bug_spawner_awaiter* awaiter_ = nullptr;
214+
unsigned count_ = 0;
215+
};
216+
217+
// test case
218+
219+
bug_task bug_spawned_task(int id, int inc)
220+
{
221+
co_return;
222+
}
223+
224+
struct A {
225+
A();
226+
};
227+
228+
void throwing_fn(bug_spawner& s) {
229+
s.spawn(bug_spawned_task(1, 2));
230+
throw A{};
231+
}
232+
233+
// Check that the coroutine frame of bug_spawned_task are allocated from operator new.
234+
// CHECK: define{{.*}}@_Z11throwing_fnR11bug_spawner
235+
// CHECK-NOT: alloc
236+
// CHECK: %[[CALL:.+]] = {{.*}}@_Znwm(i64{{.*}} 24)
237+
// CHECK: store ptr @_Z16bug_spawned_taskii.resume, ptr %[[CALL]]

Diff for: llvm/lib/Transforms/Coroutines/CoroElide.cpp

+60-23
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,49 @@ bool Lowerer::hasEscapePath(const CoroBeginInst *CB,
194194
for (auto *DA : It->second)
195195
Visited.insert(DA->getParent());
196196

197+
SmallPtrSet<const BasicBlock *, 32> EscapingBBs;
198+
for (auto *U : CB->users()) {
199+
// The use from coroutine intrinsics are not a problem.
200+
if (isa<CoroFreeInst, CoroSubFnInst, CoroSaveInst>(U))
201+
continue;
202+
203+
// Think all other usages may be an escaping candidate conservatively.
204+
//
205+
// Note that the major user of switch ABI coroutine (the C++) will store
206+
// resume.fn, destroy.fn and the index to the coroutine frame immediately.
207+
// So the parent of the coro.begin in C++ will be always escaping.
208+
// Then we can't get any performance benefits for C++ by improving the
209+
// precision of the method.
210+
//
211+
// The reason why we still judge it is we want to make LLVM Coroutine in
212+
// switch ABIs to be self contained as much as possible instead of a
213+
// by-product of C++20 Coroutines.
214+
EscapingBBs.insert(cast<Instruction>(U)->getParent());
215+
}
216+
217+
bool PotentiallyEscaped = false;
218+
197219
do {
198220
const auto *BB = Worklist.pop_back_val();
199221
if (!Visited.insert(BB).second)
200222
continue;
201-
if (TIs.count(BB))
202-
return true;
223+
224+
// A Path insensitive marker to test whether the coro.begin escapes.
225+
// It is intentional to make it path insensitive while it may not be
226+
// precise since we don't want the process to be too slow.
227+
PotentiallyEscaped |= EscapingBBs.count(BB);
228+
229+
if (TIs.count(BB)) {
230+
if (!BB->getTerminator()->isExceptionalTerminator() || PotentiallyEscaped)
231+
return true;
232+
233+
// If the function ends with the exceptional terminator, the memory used
234+
// by the coroutine frame can be released by stack unwinding
235+
// automatically. So we can think the coro.begin doesn't escape if it
236+
// exits the function by exceptional terminator.
237+
238+
continue;
239+
}
203240

204241
// Conservatively say that there is potentially a path.
205242
if (!--Limit)
@@ -236,36 +273,36 @@ bool Lowerer::shouldElide(Function *F, DominatorTree &DT) const {
236273
// memory location storing that value and not the virtual register.
237274

238275
SmallPtrSet<BasicBlock *, 8> Terminators;
239-
// First gather all of the non-exceptional terminators for the function.
276+
// First gather all of the terminators for the function.
240277
// Consider the final coro.suspend as the real terminator when the current
241278
// function is a coroutine.
242-
for (BasicBlock &B : *F) {
243-
auto *TI = B.getTerminator();
244-
if (TI->getNumSuccessors() == 0 && !TI->isExceptionalTerminator() &&
245-
!isa<UnreachableInst>(TI))
246-
Terminators.insert(&B);
247-
}
279+
for (BasicBlock &B : *F) {
280+
auto *TI = B.getTerminator();
281+
282+
if (TI->getNumSuccessors() != 0 || isa<UnreachableInst>(TI))
283+
continue;
284+
285+
Terminators.insert(&B);
286+
}
248287

249288
// Filter out the coro.destroy that lie along exceptional paths.
250289
SmallPtrSet<CoroBeginInst *, 8> ReferencedCoroBegins;
251290
for (const auto &It : DestroyAddr) {
252-
// If there is any coro.destroy dominates all of the terminators for the
253-
// coro.begin, we could know the corresponding coro.begin wouldn't escape.
254-
for (Instruction *DA : It.second) {
255-
if (llvm::all_of(Terminators, [&](auto *TI) {
256-
return DT.dominates(DA, TI->getTerminator());
257-
})) {
258-
ReferencedCoroBegins.insert(It.first);
259-
break;
260-
}
261-
}
262-
263-
// Whether there is any paths from coro.begin to Terminators which not pass
264-
// through any of the coro.destroys.
291+
// If every terminators is dominated by coro.destroy, we could know the
292+
// corresponding coro.begin wouldn't escape.
293+
//
294+
// Otherwise hasEscapePath would decide whether there is any paths from
295+
// coro.begin to Terminators which not pass through any of the
296+
// coro.destroys.
265297
//
266298
// hasEscapePath is relatively slow, so we avoid to run it as much as
267299
// possible.
268-
if (!ReferencedCoroBegins.count(It.first) &&
300+
if (llvm::all_of(Terminators,
301+
[&](auto *TI) {
302+
return llvm::any_of(It.second, [&](auto *DA) {
303+
return DT.dominates(DA, TI->getTerminator());
304+
});
305+
}) ||
269306
!hasEscapePath(It.first, Terminators))
270307
ReferencedCoroBegins.insert(It.first);
271308
}

0 commit comments

Comments
 (0)