Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix UB in cancelled tasks #578

Merged
merged 2 commits into from
Oct 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions include/unifex/task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ struct _result_and_unhandled_exception final {
if (result_.state_ == _state::exception) {
std::rethrow_exception(std::move(result_.exception_).get());
}

UNIFEX_ASSERT(result_.state_ == _state::value);
return std::move(result_.value_).get();
}
}
Expand Down Expand Up @@ -389,6 +391,7 @@ struct _sr_thunk_promise_base : _promise_base {
if (refCount_.fetch_sub(1, std::memory_order_acq_rel) == 1) {
return continuation_.done();
} else {
handleToResume_ = continuation_.done();
return coro::noop_coroutine();
}
}
Expand Down Expand Up @@ -420,7 +423,8 @@ struct _sr_thunk_promise_base : _promise_base {

void set_value(bool) noexcept {
if (self->refCount_.fetch_sub(1, std::memory_order_acq_rel) == 1) {
self->continuation_.handle().resume();
UNIFEX_ASSERT(self->handleToResume_ != coro::coroutine_handle<>{});
self->handleToResume_.resume();
}
}
void set_error(std::exception_ptr) noexcept { std::terminate(); }
Expand All @@ -447,6 +451,7 @@ struct _sr_thunk_promise_base : _promise_base {
using stop_callback_t =
typename inplace_stop_token::callback_type<stop_callback>;

coro::coroutine_handle<> handleToResume_{};
manual_lifetime<stop_callback_t> callback_;

std::atomic<uint8_t> refCount_{1};
Expand Down Expand Up @@ -498,8 +503,11 @@ struct _sr_thunk_promise final {
// nothing and wait for the async stop request to do it
if (p.refCount_.fetch_sub(1, std::memory_order_acq_rel) == 1) {
return h.promise().continuation_.handle().resume();
} else {
p.handleToResume_ = h.promise().continuation_.handle();
// don't resume anything here; wait for the deferred stop request
// to resume our continuation
}
// nothing
}
#else
coro::coroutine_handle<>
Expand All @@ -513,6 +521,7 @@ struct _sr_thunk_promise final {
if (p.refCount_.fetch_sub(1, std::memory_order_acq_rel) == 1) {
return h.promise().continuation_.handle();
} else {
p.handleToResume_ = h.promise().continuation_.handle();
return coro::noop_coroutine();
}
}
Expand Down
Loading