startup: check that a reused or destroyed save lock frees its lock #1684

Merged
Asimmimx merged 1 commit from upstream-pr/1691 into main 2026-11-24 17:24:16 +00:00
Asimmimx commented 2026-11-24 17:12:18 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Asimmimx on 2026-11-24T17:12:18Z.


Refs #1149.

Change

src/startup_stubs/file_lock.h is the cross-process lock of the game-save stand-in store: FileLock::acquire opens a lock file and takes LockFileEx (or flock) on it with a bounded wait, and the system drops the lock when the handle closes. release() closes the handle, and the destructor calls release().

Two tests reach the header today:

  • startup_gamesave_lock_errors checks the busy/error code mapping, the overflow-free deadline, and the error reporting with real lock files;
  • startup_gamesave_publish_locked / _unlocked run the store across processes on a generated copy of the header.

Neither test checks that a lock object gives its lock back except through an explicit release() or the end of a child process. Three single-point mutants of those paths left both tests passing:

  • acquire without its leading release(): re-acquiring with the same object leaks the earlier lock behind an open handle;
  • a destructor that does not release: Store::read_blobs, delete_blob and delete_container rely on it, because they keep a FileLock on the stack and never call release();
  • release() that keeps the stale handle value: a second release(), for example an explicit one followed by the destructor, closes whatever the system has since handed out under that value.

This PR adds check_release_paths to tests/startup_gamesave_lock_errors.cpp. Each case takes the lock from a second object with a zero timeout, so it succeeds only if the first object's handle is really closed:

Case What it pins
object re-acquires the same file it holds acquire drops its earlier hold first
object moves from file A to file B; a second object takes A, and B stays held moving to another file frees the old lock and keeps the new one
scoped object leaves scope; a second object takes the file the destructor frees the lock
object released, a second object takes the freed lock, first object released again; a third attempt must give timeout a second release() leaves alone a handle the object no longer owns

Negative control: startup_gamesave_lock_errors_reacquire_control builds the same check against a generated copy of file_lock.h without the leading release() in acquire. This follows the startup_gamesave_publish_unlocked pattern next to it. CMake makes the copy with a regex that tolerates CRLF and stops with FATAL_ERROR if the anchor is gone. The test uses PASS_REGULAR_EXPRESSION "FAIL: re-acquire" instead of WILL_FAIL, so it passes only when the check reports this specific defect. A crash would not pass it. The copy exists only in the build tree.

The change touches tests/startup_gamesave_lock_errors.cpp (one new function and one call; no existing case changed) and adds 13 lines to CMakeLists.txt directly after the startup_gamesave_lock_errors registration. src/startup_stubs/file_lock.h itself is unchanged. docs/startup-runtime.md does not list this check's coverage, so it is not changed.

Verification

Commit a9852263 on c864f138 (main), dev preset (Debug), Windows 11 Pro build 26300.9974, MSVC 19.51.36260. No GPU is involved.

cmake --preset dev
cmake --build --preset dev --target startup_gamesave_lock_errors startup_gamesave_lock_errors_keep startup_gamesave_publish_locked startup_gamesave_publish_unlocked
ctest --preset dev --no-tests=error -R '^startup_gamesave_(lock_errors|lock_errors_reacquire_control|publish_locked|publish_unlocked)$'
#   4/4 passed (lock_errors 0.21 s, reacquire_control 0.20 s, publish_locked 2.19 s, publish_unlocked 0.92 s)
ctest --preset dev --no-tests=error -R '^startup_gamesave_lock_errors(_reacquire_control)?$' --repeat until-fail:20
#   2/2 passed, 20 repetitions each

When run directly, the control binary prints FAIL: re-acquire: taking the same lock again on the same object succeeds ... and FAIL: re-acquire: moving to another file frees the lock the object held before. Four later cases fail too, because the leaked lock carries over, and it exits 1.

tests/docs_links_check.py, tests/bidi_controls_check.py and git diff --check passed.

Mutation probe

I applied each mutation to src/startup_stubs/file_lock.h alone. Then I re-ran cmake --preset dev (the publish tests compile a configure-time copy of the header), rebuilt startup_gamesave_lock_errors and startup_gamesave_publish_locked, ran both tests and reverted. None of this is committed. "Before" counts as killed if either test failed. These are Windows-branch mutants.

Mutant Before After
acquire does not release() a lock it already holds survived killed
destructor does not release() survived killed
release() keeps the stale handle value survived killed (3 of 3 runs, see note)
finite timeout becomes one attempt (return now) killed (publish) killed
saturated timeout becomes one attempt killed killed
ERROR_SHARING_VIOLATION not busy killed killed
ERROR_IO_PENDING not busy killed killed
shared/exclusive flags swapped killed killed
always exclusive killed killed
always shared killed killed
OPEN_EXISTING instead of OPEN_ALWAYS killed killed
failed open does not record GetLastError killed killed
failed lock does not record GetLastError killed killed
zero-byte lock range killed killed
timeout reported as error killed killed

Note on the stale-handle case: whether the third object's open receives the freed handle value is up to Windows (on POSIX the lowest free descriptor is reused, so it is deterministic there). On the correct header the case cannot fail either way, so it never fails falsely. At worst it misses the mutant. It killed the mutant in all three probe runs here.

Not pinned (left as is, on purpose)

  • Per-attempt last_error_ = 0 (survives). After a wait that sees busy and then locks, last_error() is 0 in the code. The comment says "the last failed open or lock attempt; 0 when none failed", which can also be read as "the busy code from earlier in this call". I did not pin either reading.
  • Lock-error paths (break on Attempt::error, the release() before returning error, "every lock error is busy"). They survive. Reaching them needs LockFileEx to fail with a code other than busy on a handle that opened, and I found no device-free way to make that happen.
  • release() before returning timeout (survives). A timed-out object keeps an unlocked handle open until it is reused or destroyed. No lock behavior can observe that.
  • FILE_SHARE_DELETE dropped and GENERIC_READ only (survive). The store never deletes or writes a lock file, so this behavior is outside the lock contract.
  • Equivalent mutants: the per-call last_error_ = 0 (the per-attempt reset or open overwrite it on every path), </<= at the zero timeout, >/>= at the deadline and saturation tests, and a fixed retry pause, which changes only timing.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed (no change needed).
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6587321967 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1691). Originally posted by [Asimmimx](https://github.com/Asimmimx) on 2026-11-24T17:12:18Z. --- Refs #1149. ## Change `src/startup_stubs/file_lock.h` is the cross-process lock of the game-save stand-in store: `FileLock::acquire` opens a lock file and takes `LockFileEx` (or `flock`) on it with a bounded wait, and the system drops the lock when the handle closes. `release()` closes the handle, and the destructor calls `release()`. Two tests reach the header today: - `startup_gamesave_lock_errors` checks the busy/error code mapping, the overflow-free deadline, and the error reporting with real lock files; - `startup_gamesave_publish_locked` / `_unlocked` run the store across processes on a generated copy of the header. Neither test checks that a lock object gives its lock back except through an explicit `release()` or the end of a child process. Three single-point mutants of those paths left both tests passing: - `acquire` without its leading `release()`: re-acquiring with the same object leaks the earlier lock behind an open handle; - a destructor that does not release: `Store::read_blobs`, `delete_blob` and `delete_container` rely on it, because they keep a `FileLock` on the stack and never call `release()`; - `release()` that keeps the stale handle value: a second `release()`, for example an explicit one followed by the destructor, closes whatever the system has since handed out under that value. This PR adds `check_release_paths` to `tests/startup_gamesave_lock_errors.cpp`. Each case takes the lock from a second object with a zero timeout, so it succeeds only if the first object's handle is really closed: | Case | What it pins | |---|---| | object re-acquires the same file it holds | `acquire` drops its earlier hold first | | object moves from file A to file B; a second object takes A, and B stays held | moving to another file frees the old lock and keeps the new one | | scoped object leaves scope; a second object takes the file | the destructor frees the lock | | object released, a second object takes the freed lock, first object released again; a third attempt must give `timeout` | a second `release()` leaves alone a handle the object no longer owns | Negative control: `startup_gamesave_lock_errors_reacquire_control` builds the same check against a generated copy of `file_lock.h` without the leading `release()` in `acquire`. This follows the `startup_gamesave_publish_unlocked` pattern next to it. CMake makes the copy with a regex that tolerates CRLF and stops with `FATAL_ERROR` if the anchor is gone. The test uses `PASS_REGULAR_EXPRESSION "FAIL: re-acquire"` instead of `WILL_FAIL`, so it passes only when the check reports this specific defect. A crash would not pass it. The copy exists only in the build tree. The change touches `tests/startup_gamesave_lock_errors.cpp` (one new function and one call; no existing case changed) and adds 13 lines to `CMakeLists.txt` directly after the `startup_gamesave_lock_errors` registration. `src/startup_stubs/file_lock.h` itself is unchanged. `docs/startup-runtime.md` does not list this check's coverage, so it is not changed. ## Verification Commit `a9852263` on `c864f138` (`main`), `dev` preset (Debug), Windows 11 Pro build 26300.9974, MSVC 19.51.36260. No GPU is involved. ```powershell cmake --preset dev cmake --build --preset dev --target startup_gamesave_lock_errors startup_gamesave_lock_errors_keep startup_gamesave_publish_locked startup_gamesave_publish_unlocked ctest --preset dev --no-tests=error -R '^startup_gamesave_(lock_errors|lock_errors_reacquire_control|publish_locked|publish_unlocked)$' # 4/4 passed (lock_errors 0.21 s, reacquire_control 0.20 s, publish_locked 2.19 s, publish_unlocked 0.92 s) ctest --preset dev --no-tests=error -R '^startup_gamesave_lock_errors(_reacquire_control)?$' --repeat until-fail:20 # 2/2 passed, 20 repetitions each ``` When run directly, the control binary prints `FAIL: re-acquire: taking the same lock again on the same object succeeds ...` and `FAIL: re-acquire: moving to another file frees the lock the object held before`. Four later cases fail too, because the leaked lock carries over, and it exits 1. `tests/docs_links_check.py`, `tests/bidi_controls_check.py` and `git diff --check` passed. ## Mutation probe I applied each mutation to `src/startup_stubs/file_lock.h` alone. Then I re-ran `cmake --preset dev` (the publish tests compile a configure-time copy of the header), rebuilt `startup_gamesave_lock_errors` and `startup_gamesave_publish_locked`, ran both tests and reverted. None of this is committed. "Before" counts as killed if either test failed. These are Windows-branch mutants. | Mutant | Before | After | |---|---|---| | `acquire` does not `release()` a lock it already holds | **survived** | killed | | destructor does not `release()` | **survived** | killed | | `release()` keeps the stale handle value | **survived** | killed (3 of 3 runs, see note) | | finite timeout becomes one attempt (`return now`) | killed (publish) | killed | | saturated timeout becomes one attempt | killed | killed | | `ERROR_SHARING_VIOLATION` not busy | killed | killed | | `ERROR_IO_PENDING` not busy | killed | killed | | shared/exclusive flags swapped | killed | killed | | always exclusive | killed | killed | | always shared | killed | killed | | `OPEN_EXISTING` instead of `OPEN_ALWAYS` | killed | killed | | failed open does not record `GetLastError` | killed | killed | | failed lock does not record `GetLastError` | killed | killed | | zero-byte lock range | killed | killed | | timeout reported as `error` | killed | killed | Note on the stale-handle case: whether the third object's open receives the freed handle value is up to Windows (on POSIX the lowest free descriptor is reused, so it is deterministic there). On the correct header the case cannot fail either way, so it never fails falsely. At worst it misses the mutant. It killed the mutant in all three probe runs here. ### Not pinned (left as is, on purpose) - **Per-attempt `last_error_ = 0`** (survives). After a wait that sees busy and then locks, `last_error()` is 0 in the code. The comment says "the last failed open or lock attempt; 0 when none failed", which can also be read as "the busy code from earlier in this call". I did not pin either reading. - **Lock-error paths** (`break` on `Attempt::error`, the `release()` before returning `error`, "every lock error is busy"). They survive. Reaching them needs `LockFileEx` to fail with a code other than busy on a handle that opened, and I found no device-free way to make that happen. - **`release()` before returning `timeout`** (survives). A timed-out object keeps an unlocked handle open until it is reused or destroyed. No lock behavior can observe that. - **`FILE_SHARE_DELETE` dropped** and **`GENERIC_READ` only** (survive). The store never deletes or writes a lock file, so this behavior is outside the lock contract. - **Equivalent mutants**: the per-call `last_error_ = 0` (the per-attempt reset or `open` overwrite it on every path), `<`/`<=` at the zero timeout, `>`/`>=` at the deadline and saturation tests, and a fixed retry pause, which changes only timing. ## Checklist - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] I considered a negative control or failure case for a new fixture. - [x] I updated README/docs when a capability or build requirement changed (no change needed). - [x] I did not include game files, dumps, distribution links or secrets. AI-assisted (Claude Code).
upstream-sync closed this pull request 2026-11-24 18:02:33 +00:00
tedin7 commented 2026-11-24 18:02:34 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T17:23:46Z.

Review: COMMENTED


Review of head a9852263c2 (comment only, not an approval).

Verdict: 0 P1 / 0 P2. Test and CMake only; no production change. No test is removed, loosened or skipped, and no expected value comes from the code under test.

Run

  • g++ -std=c++17 -O1 -Isrc tests/startup_gamesave_lock_errors.cpp -pthread: prints startup_gamesave_lock_errors: ok, exit 0.
  • The CMake negative control reproduced with cmake -P and the PR's exact regex (removes release(); at the start of acquire): compiles, exits 1 with FAIL: re-acquire, scope, destructor and double release lines, so PASS_REGULAR_EXPRESSION matches.
  • x86_64-w64-mingw32-g++ -std=c++17 -fsyntax-only -Isrc and clang++-19 -std=c++17 -fsyntax-only -Wall -Wextra: clean.
  • Mutations on the real src/startup_stubs/file_lock.h, each restored: ~FileLock() { release(); } -> ~FileLock() {} fails the destructor and double-release checks; removing fd_ = -1; from release() fails "releasing an object twice leaves the lock another object now holds" (the stale-descriptor case, caught only by this new test). Green again after restore.

Non-blocking

  • The control build removes only the release at the start of acquire; destructor and double-release are guarded by the main test alone (which the mutations show can fail).
  • The control's include directory has no src; it compiles today but would break if the test includes another header from src/.
  • The CMake edit has a FATAL_ERROR guard if the release(); / last_error_ = 0; anchor disappears, which is the right failure mode.

Not run: the real CMake configure and ctest, Windows LockFileEx behaviour (the Windows branch was only syntax-checked), MSVC. No overlap found with open PRs (#1334 touches the save provider; check only if it edits startup_gamesave_*).

Linux limits: no Windows, MSVC, DXC or D3D12 device here; LLVM 19 only. Anything that needs them was not run.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6044115165 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1691#pullrequestreview-6044115165). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T17:23:46Z. **Review:** COMMENTED --- Review of head a9852263c26e0e7556905921b064fc1439ccd52f (comment only, not an approval). Verdict: 0 P1 / 0 P2. Test and CMake only; no production change. No test is removed, loosened or skipped, and no expected value comes from the code under test. Run - `g++ -std=c++17 -O1 -Isrc tests/startup_gamesave_lock_errors.cpp -pthread`: prints `startup_gamesave_lock_errors: ok`, exit 0. - The CMake negative control reproduced with `cmake -P` and the PR's exact regex (removes `release();` at the start of `acquire`): compiles, exits 1 with `FAIL: re-acquire`, `scope`, `destructor` and `double release` lines, so `PASS_REGULAR_EXPRESSION` matches. - `x86_64-w64-mingw32-g++ -std=c++17 -fsyntax-only -Isrc` and `clang++-19 -std=c++17 -fsyntax-only -Wall -Wextra`: clean. - Mutations on the real src/startup_stubs/file_lock.h, each restored: `~FileLock() { release(); }` -> `~FileLock() {}` fails the destructor and double-release checks; removing `fd_ = -1;` from `release()` fails "releasing an object twice leaves the lock another object now holds" (the stale-descriptor case, caught only by this new test). Green again after restore. Non-blocking - The control build removes only the release at the start of `acquire`; destructor and double-release are guarded by the main test alone (which the mutations show can fail). - The control's include directory has no `src`; it compiles today but would break if the test includes another header from src/. - The CMake edit has a FATAL_ERROR guard if the `release();` / `last_error_ = 0;` anchor disappears, which is the right failure mode. Not run: the real CMake configure and ctest, Windows LockFileEx behaviour (the Windows branch was only syntax-checked), MSVC. No overlap found with open PRs (#1334 touches the save provider; check only if it edits startup_gamesave_*). Linux limits: no Windows, MSVC, DXC or D3D12 device here; LLVM 19 only. Anything that needs them was not run. AI-assisted; commands listed were executed locally.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows!1684
No description provided.