Skip to content

Commit 5cc004a

Browse files
committed
fix use-after-free in inplace_stop_source::request_stop()
If a stop callback destroys the stop source (for example when it completes a when_all and the receiver destroys the operation state), request_stop() still calls __lock_() and stores to __state_ on the freed object. Make the destructor work like __remove_callback_. On the notifying thread it sets a flag that lives on request_stop()'s stack, so request_stop() returns without touching *this again. On any other thread it waits until request_stop() has finished. Sources that never had stop requested skip all of this. Fixes #1889
1 parent f4c123f commit 5cc004a

2 files changed

Lines changed: 120 additions & 0 deletions

File tree

‎include/stdexec/stop_token.hpp‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,15 @@ STDEXEC_P2300_NAMESPACE_BEGIN()
157157

158158
void __remove_callback_(STDEXEC::__stok::__inplace_stop_callback_base*) const noexcept;
159159

160+
void __sync_with_request_stop_() noexcept;
161+
160162
static constexpr uint8_t __stop_requested_flag_ = 1;
161163
static constexpr uint8_t __locked_flag_ = 2;
162164

163165
mutable STDEXEC::__std::atomic<uint8_t> __state_{0};
164166
mutable STDEXEC::__stok::__inplace_stop_callback_base* __callbacks_ = nullptr;
165167
std::thread::id __notifying_thread_;
168+
bool* __destroyed_ = nullptr;
166169
};
167170

168171
// [stoptoken.inplace], class inplace_stop_token
@@ -266,17 +269,51 @@ STDEXEC_P2300_NAMESPACE_BEGIN()
266269

267270
inline inplace_stop_source::~inplace_stop_source()
268271
{
272+
if (stop_requested())
273+
__sync_with_request_stop_();
274+
269275
STDEXEC_ASSERT((__state_.load(STDEXEC::__std::memory_order_relaxed) & __locked_flag_) == 0);
270276
STDEXEC_ASSERT(__callbacks_ == nullptr);
271277
}
272278

279+
// Kept separate so the destructor's no-stop path stays cheap to inline.
280+
inline void inplace_stop_source::__sync_with_request_stop_() noexcept
281+
{
282+
auto __old_state = __lock_();
283+
auto __destroyed = __destroyed_;
284+
auto __notifying_thread = __notifying_thread_;
285+
__unlock_(__old_state);
286+
287+
if (__destroyed != nullptr && std::this_thread::get_id() == __notifying_thread)
288+
{
289+
// A stop callback is destroying us from inside request_stop().
290+
*__destroyed = true;
291+
}
292+
else
293+
{
294+
// Wait for request_stop() to finish on the other thread.
295+
STDEXEC::__stok::__spin_wait __spin;
296+
while (__destroyed != nullptr)
297+
{
298+
__spin.__wait();
299+
__old_state = __lock_();
300+
__destroyed = __destroyed_;
301+
__unlock_(__old_state);
302+
}
303+
}
304+
}
305+
273306
inline auto inplace_stop_source::request_stop() noexcept -> bool
274307
{
275308
if (!__try_lock_unless_stop_requested_(true))
276309
return false;
277310

278311
__notifying_thread_ = std::this_thread::get_id();
279312

313+
// ~inplace_stop_source() sets this if a callback destroys us.
314+
bool __destroyed = false;
315+
__destroyed_ = &__destroyed;
316+
280317
// We are responsible for executing callbacks.
281318
while (__callbacks_ != nullptr)
282319
{
@@ -299,9 +336,14 @@ STDEXEC_P2300_NAMESPACE_BEGIN()
299336
__callbk->__callback_completed_.store(true, STDEXEC::__std::memory_order_release);
300337
}
301338

339+
// The callback destroyed this stop source, so we can't touch *this again.
340+
if (__destroyed)
341+
return true;
342+
302343
__lock_();
303344
}
304345

346+
__destroyed_ = nullptr;
305347
__state_.store(__stop_requested_flag_, STDEXEC::__std::memory_order_release);
306348
return true;
307349
}

‎test/stdexec/concepts/test_concepts_stop_tokens.cpp‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
#if STDEXEC_USE_MODULES()
2323
import std;
2424
#else
25+
# include <atomic>
26+
# include <chrono>
27+
# include <memory>
28+
# include <optional>
29+
# include <thread>
2530
# include <type_traits>
2631
#endif
2732

@@ -63,4 +68,77 @@ namespace
6368
::STDEXEC::inplace_stop_callback cb{source.get_token(), on_stop_request{}};
6469
STATIC_REQUIRE(std::is_same_v<decltype(cb), ::STDEXEC::inplace_stop_callback<on_stop_request>>);
6570
}
71+
72+
struct stop_state;
73+
74+
struct destroy_on_stop
75+
{
76+
std::unique_ptr<stop_state>* state;
77+
78+
void operator()() noexcept;
79+
};
80+
81+
struct stop_state
82+
{
83+
::STDEXEC::inplace_stop_source source;
84+
std::optional<::STDEXEC::inplace_stop_callback<on_stop_request>> other;
85+
std::optional<::STDEXEC::inplace_stop_callback<destroy_on_stop>> callback;
86+
};
87+
88+
void destroy_on_stop::operator()() noexcept
89+
{
90+
state->reset();
91+
}
92+
93+
TEST_CASE("a stop callback can destroy the inplace_stop_source", "[stop_token]")
94+
{
95+
auto state = std::make_unique<stop_state>();
96+
state->other.emplace(state->source.get_token(), on_stop_request{});
97+
state->callback.emplace(state->source.get_token(), destroy_on_stop{&state});
98+
99+
CHECK(state->source.request_stop());
100+
CHECK(state == nullptr);
101+
}
102+
103+
struct slow_state;
104+
105+
struct remove_and_sleep
106+
{
107+
slow_state* state;
108+
std::atomic<bool>* removed;
109+
110+
void operator()() noexcept;
111+
};
112+
113+
struct slow_state
114+
{
115+
::STDEXEC::inplace_stop_source source;
116+
std::optional<::STDEXEC::inplace_stop_callback<remove_and_sleep>> callback;
117+
};
118+
119+
void remove_and_sleep::operator()() noexcept
120+
{
121+
// Like an operation that completes from inside its stop callback.
122+
auto* removed_flag = removed;
123+
state->callback.reset();
124+
removed_flag->store(true);
125+
126+
// Keep request_stop() running while the main thread destroys the source.
127+
std::this_thread::sleep_for(std::chrono::milliseconds(10));
128+
}
129+
130+
TEST_CASE("inplace_stop_source destructor waits for request_stop on another thread",
131+
"[stop_token]")
132+
{
133+
std::atomic<bool> removed{false};
134+
auto state = std::make_unique<slow_state>();
135+
state->callback.emplace(state->source.get_token(), remove_and_sleep{state.get(), &removed});
136+
137+
std::thread thread([source = &state->source] { source->request_stop(); });
138+
while (!removed.load())
139+
std::this_thread::yield();
140+
141+
state.reset(); // request_stop() is still running on the other thread
142+
thread.join();
143+
}
66144
} // namespace

0 commit comments

Comments
 (0)