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/test/gameinput_test.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/test/gameinput_test.cpp')
| -rw-r--r-- | runtime/test/gameinput_test.cpp | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/runtime/test/gameinput_test.cpp b/runtime/test/gameinput_test.cpp index 1389b8f6d..2737b9fc9 100644 --- a/runtime/test/gameinput_test.cpp +++ b/runtime/test/gameinput_test.cpp | |||
| @@ -12,12 +12,14 @@ | |||
| 12 | #include <SDL3/SDL.h> | 12 | #include <SDL3/SDL.h> |
| 13 | 13 | ||
| 14 | #include <algorithm> | 14 | #include <algorithm> |
| 15 | #include <atomic> | ||
| 15 | #include <cmath> | 16 | #include <cmath> |
| 16 | #include <cstdint> | 17 | #include <cstdint> |
| 17 | #include <cstdio> | 18 | #include <cstdio> |
| 18 | #include <cstring> | 19 | #include <cstring> |
| 19 | #include <deque> | 20 | #include <deque> |
| 20 | #include <string> | 21 | #include <string> |
| 22 | #include <thread> | ||
| 21 | #include <vector> | 23 | #include <vector> |
| 22 | 24 | ||
| 23 | #include "gameinput.h" | 25 | #include "gameinput.h" |
| @@ -345,6 +347,14 @@ std::vector<uint8_t> raw_bytes(IGameInputRawDeviceReport* r) { | |||
| 345 | return v; | 347 | return v; |
| 346 | } | 348 | } |
| 347 | 349 | ||
| 350 | // RT-262: the poll thread takes a reference on a force-feedback effect only while it is alive. | ||
| 351 | void test_add_ref_if_live() { | ||
| 352 | std::atomic<unsigned long> live{1}, dying{0}; | ||
| 353 | check(rt::gameinput::add_ref_if_live(live) && live.load() == 2, "add_ref_if_live: a live count goes up by one"); | ||
| 354 | check(!rt::gameinput::add_ref_if_live(dying) && dying.load() == 0, | ||
| 355 | "add_ref_if_live: a count of 0 stays 0, so an effect being destroyed isn't revived"); | ||
| 356 | } | ||
| 357 | |||
| 348 | void test_raw_hid(IGameInput* input) { | 358 | void test_raw_hid(IGameInput* input) { |
| 349 | test_parse_report_descriptor(); | 359 | test_parse_report_descriptor(); |
| 350 | rt::gameinput::set_hid_backend(&kFakeRawHid); | 360 | rt::gameinput::set_hid_backend(&kFakeRawHid); |
| @@ -1209,6 +1219,28 @@ void test_force_feedback(IGameInput* input) { | |||
| 1209 | rt::gameinput::poll(); | 1219 | rt::gameinput::poll(); |
| 1210 | check(g_fake.destroys == 1, "force feedback: its SDL effect is destroyed on the poll thread"); | 1220 | check(g_fake.destroys == 1, "force feedback: its SDL effect is destroyed on the poll thread"); |
| 1211 | 1221 | ||
| 1222 | // RT-262: the title releases effects while the poll thread applies them. The poll thread used to AddRef an effect | ||
| 1223 | // whose count had already reached 0 (its destructor waiting for the lock), use it after it was freed, and free it | ||
| 1224 | // a second time with its own Release. | ||
| 1225 | { | ||
| 1226 | std::atomic<bool> stop{false}; | ||
| 1227 | std::thread poller([&] { | ||
| 1228 | while (!stop.load()) rt::gameinput::poll(); | ||
| 1229 | }); | ||
| 1230 | int created = 0; | ||
| 1231 | for (int i = 0; i < 20000; ++i) { | ||
| 1232 | IGameInputForceFeedbackEffect* e = nullptr; | ||
| 1233 | if (dev->CreateForceFeedbackEffect(0, ¶ms, reinterpret_cast<void**>(&e)) == S_OK && e) { | ||
| 1234 | ++created; | ||
| 1235 | e->Release(); | ||
| 1236 | } | ||
| 1237 | } | ||
| 1238 | stop = true; | ||
| 1239 | poller.join(); | ||
| 1240 | rt::gameinput::poll(); | ||
| 1241 | check(created == 20000, "force feedback: 20,000 effects created and released while the poll thread runs"); | ||
| 1242 | } | ||
| 1243 | |||
| 1212 | detach(wheel); | 1244 | detach(wheel); |
| 1213 | rt::gameinput::poll(); | 1245 | rt::gameinput::poll(); |
| 1214 | check(g_fake.closes == 1 && !dev->IsForceFeedbackMotorPoweredOn(0), | 1246 | check(g_fake.closes == 1 && !dev->IsForceFeedbackMotorPoweredOn(0), |
| @@ -1319,6 +1351,7 @@ int main() { | |||
| 1319 | set_env("LEONIDA_DS_TRIGGERS", nullptr); | 1351 | set_env("LEONIDA_DS_TRIGGERS", nullptr); |
| 1320 | set_env("LEONIDA_DS_LIGHTBAR", nullptr); | 1352 | set_env("LEONIDA_DS_LIGHTBAR", nullptr); |
| 1321 | set_env("LEONIDA_RUMBLE", nullptr); | 1353 | set_env("LEONIDA_RUMBLE", nullptr); |
| 1354 | test_add_ref_if_live(); | ||
| 1322 | 1355 | ||
| 1323 | auto create = reinterpret_cast<GameInputCreateFn>(rt::shims::find("gameinput.dll", "GameInputCreate")); | 1356 | auto create = reinterpret_cast<GameInputCreateFn>(rt::shims::find("gameinput.dll", "GameInputCreate")); |
| 1324 | check(create != nullptr, "gameinput.dll!GameInputCreate is registered"); | 1357 | check(create != nullptr, "gameinput.dll!GameInputCreate is registered"); |