diff options
| author | Felix Croasdale <205819305+etcetera-yt@users.noreply.github.com> | 2026-11-22 17:26:51 -0600 |
|---|---|---|
| committer | Felix Croasdale <205819305+etcetera-yt@users.noreply.github.com> | 2026-11-22 17:26:51 -0600 |
| commit | c5c9ff002ac564a687a3f3f4a418079e9881c8e0 (patch) | |
| tree | 5a70e5e1e1faca36c1d0847cccc254bec3785289 /runtime/shims/gameinput.cpp | |
| parent | aa2988cc555bad7ecd508b48a702adbb32fe7c95 (diff) | |
gameinput: the poll thread no longer revives a force-feedback effect being released (RT-262)
ForceFeedbackEffect::Release() drops the count to 0 and deletes the
effect; its destructor then waits for g().mu to unlink it from the
device. In that window apply_effects (holding g().mu) could AddRef the
dying effect from 0 to 1, sync it after it was freed, and delete it a
second time with its own Release (@AAyberkU's finding).
apply_effects now takes a reference only on a live effect
(add_ref_if_live: a compare-exchange that never moves a count up from 0),
so an effect whose last Release ran is skipped.
gameinput_test: add_ref_if_live itself, and 20,000 effects created and
released while a second thread runs the poll step. With the old AddRef
that run ends in STATUS_HEAP_CORRUPTION (0xC0000374) every time here.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/shims/gameinput.cpp')
| -rw-r--r-- | runtime/shims/gameinput.cpp | 12 |
1 files changed, 8 insertions, 4 deletions
diff --git a/runtime/shims/gameinput.cpp b/runtime/shims/gameinput.cpp index 69707f75a..440337d61 100644 --- a/runtime/shims/gameinput.cpp +++ b/runtime/shims/gameinput.cpp | |||
| @@ -763,6 +763,9 @@ public: | |||
| 763 | void sync(Device* device, uint64_t now, bool muted); | 763 | void sync(Device* device, uint64_t now, bool muted); |
| 764 | // Poll thread, on unplug: the SDL device went with the pad, so forget its effect id without touching SDL. | 764 | // Poll thread, on unplug: the SDL device went with the pad, so forget its effect id without touching SDL. |
| 765 | void orphan(); | 765 | void orphan(); |
| 766 | // Poll thread, under g().mu: a reference unless the title's last Release already ran (the destructor then waits | ||
| 767 | // for g().mu to unlink the effect, so it is still in the device's list but must not be used or revived). | ||
| 768 | bool add_ref_if_live() { return rt::gameinput::add_ref_if_live(refs_); } | ||
| 766 | 769 | ||
| 767 | private: | 770 | private: |
| 768 | std::atomic<ULONG> refs_{1}; | 771 | std::atomic<ULONG> refs_{1}; |
| @@ -1399,10 +1402,11 @@ void apply_effects(Device* d, uint64_t now) { | |||
| 1399 | { | 1402 | { |
| 1400 | std::lock_guard lock(s.mu); | 1403 | std::lock_guard lock(s.mu); |
| 1401 | live.reserve(d->effects.size()); | 1404 | live.reserve(d->effects.size()); |
| 1402 | for (ForceFeedbackEffect* e : d->effects) { // referenced: the title may release one while this runs | 1405 | // Referenced: the title may release one while this runs. An effect whose count already reached 0 is being |
| 1403 | e->AddRef(); | 1406 | // destroyed (its destructor waits for this lock to unlink it); a plain AddRef revived it, and this loop's own |
| 1404 | live.push_back(e); | 1407 | // Release below freed it a second time. |
| 1405 | } | 1408 | for (ForceFeedbackEffect* e : d->effects) |
| 1409 | if (e->add_ref_if_live()) live.push_back(e); | ||
| 1406 | retire.swap(d->retired); | 1410 | retire.swap(d->retired); |
| 1407 | motor_gain = d->motor_gain; | 1411 | motor_gain = d->motor_gain; |
| 1408 | muted = !s.config.rumble; | 1412 | muted = !s.config.rumble; |