startup: return the failed call's error from the stand-in's File::GetFileInformation #1516

Merged
RenzoWit35 merged 3 commits from upstream-pr/1523 into main 2026-11-25 02:02:07 +00:00
RenzoWit35 commented 2026-11-24 13:37:43 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by RenzoWit35 on 2026-11-24T13:37:43Z.


Change

file_information in src/startup_stubs/xcrdapi.cpp (the stand-in's IDStorageFile::GetFileInformation) called log(...) and only then built its result with HRESULT_FROM_WIN32(GetLastError()). log() calls GetEnvironmentVariableW (and CreateFileW when X2W_TRAP_LOG is set), which overwrite the thread's last error. A failed GetFileInformationByHandle therefore returned the logger's error instead of its own.

The error is now captured right after the failing call and before log(), the same pattern factory_open_file uses. This was the only place in the file that read GetLastError() after a log() call; factory_open_file already captures first. Passing GetLastError() as a log() argument (as the queue-worker message in #1410 does) is safe, because arguments are evaluated before the call.

I did not change log() itself, so the change stays three lines and cannot interfere with open PRs that touch logging. No open PR changes these lines: #1429 touches xcrdapi.cpp only as diff context near file_information, and #1482 only adds a logf check.

Verification

Environment: Windows 11, MSVC 14.51, cmake --preset dev, base origin/main 60c72330.

New scenario dstorage-file-information in tests/startup_stub_consumer.cpp, run from tests/startup_stub_check.py (CTest startup_stubs). It opens a real temporary file through the stand-in factory, calls File slot 4 (S_OK, size 1234), calls File slot 3 (Close; the object stays valid and holds INVALID_HANDLE_VALUE, so there is no use-after-free), calls slot 4 again and expects HRESULT_FROM_WIN32(ERROR_INVALID_HANDLE) = 0x80070006. It does this with X2W_TRAP_LOG unset and set.

Negative control, without the fix (startup_stub_consumer.exe dstorage-file-information, exit 10):

file-information log=unset open: hr=0x00000000 size=1234 ok; closed: hr=0x800700cb expected=0x80070006 MISMATCH
file-information log=set open: hr=0x00000000 size=1234 ok; closed: hr=0x800700b7 expected=0x80070006 MISMATCH

0xcb is ERROR_ENVVAR_NOT_FOUND (203); 0xb7 is ERROR_ALREADY_EXISTS (183) from the log file's CreateFileW.

With the fix, same command, exit 0:

file-information log=unset open: hr=0x00000000 size=1234 ok; closed: hr=0x80070006 expected=0x80070006 ok
file-information log=set open: hr=0x00000000 size=1234 ok; closed: hr=0x80070006 expected=0x80070006 ok
dstorage-file-information ok

Build: cmake --build out/build/dev --target startup_stub_consumer startup_stub_xcrdapi startup_stub_chad startup_stub_xg_xs startup_stub_acphal startup_stub_xapu startup_stub_gameinput xmem_shim d3d12_xs_shim

Run: ctest --test-dir out/build/dev --no-tests=error --timeout 150 -R '^(startup_stubs|startup_storage_directory_batch)$' -V : 2/2 passed.

Not run: the full CTest suite, GPU tests, and the CPU/PE tests (they do not touch this file).

Evidence and follow-up

The fixture is a 1234-byte synthetic temporary file written by the test; no game data.

Out of scope, noticed while reading: when info is null, file_information skips the Win32 call, so GetLastError() is whatever the caller left (possibly 0, which maps to S_OK). factory_open_file returns E_POINTER for a null out pointer; the same could be done here in a separate change.

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 (none needed).

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6583042663 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1523). Originally posted by [RenzoWit35](https://github.com/RenzoWit35) on 2026-11-24T13:37:43Z. --- ## Change `file_information` in `src/startup_stubs/xcrdapi.cpp` (the stand-in's `IDStorageFile::GetFileInformation`) called `log(...)` and only then built its result with `HRESULT_FROM_WIN32(GetLastError())`. `log()` calls `GetEnvironmentVariableW` (and `CreateFileW` when `X2W_TRAP_LOG` is set), which overwrite the thread's last error. A failed `GetFileInformationByHandle` therefore returned the logger's error instead of its own. The error is now captured right after the failing call and before `log()`, the same pattern `factory_open_file` uses. This was the only place in the file that read `GetLastError()` after a `log()` call; `factory_open_file` already captures first. Passing `GetLastError()` as a `log()` argument (as the queue-worker message in #1410 does) is safe, because arguments are evaluated before the call. I did not change `log()` itself, so the change stays three lines and cannot interfere with open PRs that touch logging. No open PR changes these lines: #1429 touches `xcrdapi.cpp` only as diff context near `file_information`, and #1482 only adds a `logf` check. ## Verification Environment: Windows 11, MSVC 14.51, `cmake --preset dev`, base `origin/main` 60c72330. New scenario `dstorage-file-information` in `tests/startup_stub_consumer.cpp`, run from `tests/startup_stub_check.py` (CTest `startup_stubs`). It opens a real temporary file through the stand-in factory, calls File slot 4 (S_OK, size 1234), calls File slot 3 (Close; the object stays valid and holds `INVALID_HANDLE_VALUE`, so there is no use-after-free), calls slot 4 again and expects `HRESULT_FROM_WIN32(ERROR_INVALID_HANDLE)` = 0x80070006. It does this with `X2W_TRAP_LOG` unset and set. Negative control, without the fix (`startup_stub_consumer.exe dstorage-file-information`, exit 10): ``` file-information log=unset open: hr=0x00000000 size=1234 ok; closed: hr=0x800700cb expected=0x80070006 MISMATCH file-information log=set open: hr=0x00000000 size=1234 ok; closed: hr=0x800700b7 expected=0x80070006 MISMATCH ``` 0xcb is ERROR_ENVVAR_NOT_FOUND (203); 0xb7 is ERROR_ALREADY_EXISTS (183) from the log file's `CreateFileW`. With the fix, same command, exit 0: ``` file-information log=unset open: hr=0x00000000 size=1234 ok; closed: hr=0x80070006 expected=0x80070006 ok file-information log=set open: hr=0x00000000 size=1234 ok; closed: hr=0x80070006 expected=0x80070006 ok dstorage-file-information ok ``` Build: `cmake --build out/build/dev --target startup_stub_consumer startup_stub_xcrdapi startup_stub_chad startup_stub_xg_xs startup_stub_acphal startup_stub_xapu startup_stub_gameinput xmem_shim d3d12_xs_shim` Run: `ctest --test-dir out/build/dev --no-tests=error --timeout 150 -R '^(startup_stubs|startup_storage_directory_batch)$' -V` : 2/2 passed. Not run: the full CTest suite, GPU tests, and the CPU/PE tests (they do not touch this file). ## Evidence and follow-up The fixture is a 1234-byte synthetic temporary file written by the test; no game data. Out of scope, noticed while reading: when `info` is null, `file_information` skips the Win32 call, so `GetLastError()` is whatever the caller left (possibly 0, which maps to S_OK). `factory_open_file` returns `E_POINTER` for a null out pointer; the same could be done here in a separate change. ## 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 (none needed). AI-assisted.
tedin7 commented 2026-11-24 16:03:00 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T15:52:58Z.

Review: COMMENTED


Review

No blocking problem found. The change does what the title says, and the added test detects the original defect. Verification is Linux only (cross-compiled, run under Wine); see "Not verified".

Verified

  • Error capture order. In file_information (src/startup_stubs/xcrdapi.cpp) the error is now read right after GetFileInformationByHandle and before log(). Nothing runs in between. log() calls GetEnvironmentVariableW and, when X2W_TRAP_LOG is set, CreateFileW (OPEN_ALWAYS) and CloseHandle, all of which overwrite the thread's last error.
  • Error domain. The return value is HRESULT_FROM_WIN32(error), the same domain as factory_open_file (line 249), which uses the same capture-then-log-then-return pattern. A closed file object yields 0x80070006 (ERROR_INVALID_HANDLE as an HRESULT).
  • Success path. Unchanged: ok gives S_OK, and the size fields are read from info as before.
  • Thread safety. GetLastError is per-thread and the value is held in a local, so there is no cross-thread issue.
  • Siblings. grep -rn GetLastError src/startup_stubs: no other stand-in method reads the error after a log call. factory_open_file already captures it first.
  • Build and run. I cross-compiled xcrdapi.cpp as a DLL with x86_64-w64-mingw32-g++ -std=c++17. I ran the new test body (copied verbatim into a small standalone driver) under Wine64, with a 120 s timeout. Result: both the X2W_TRAP_LOG unset and set passes print closed: hr=0x80070006 expected=0x80070006 ok, and the open case returns S_OK with size 1234. tests/startup_stub_consumer.cpp also passes -fsyntax-only with the same compiler.
  • Does the test catch the old code? Mutations applied to the PR's file_information, then the same driver:
Mutation Result
Restore the old code (GetLastError() read after log()) FAIL. unset: 0x800700cb (ERROR_ENVVAR_NOT_FOUND); set: 0x800700b7 (ERROR_ALREADY_EXISTS)
Keep the structure but move the read after log() FAIL, same codes as above
Return the raw Win32 code instead of the HRESULT FAIL, 0x00000006
Hard-code HRESULT_FROM_WIN32(ERROR_INVALID_HANDLE) PASS (see finding 2)

Running both the unset and set variants is what makes the test strong: each one exposes a different overwriting call.

Findings

  1. P2 (not in the diff, same function): a null info can return S_OK. src/startup_stubs/xcrdapi.cpp:307-311. ok = info && ... is false when info == nullptr, but no Win32 call has failed, so GetLastError() is whatever the thread had before. Repro: call slot 4 with info = nullptr. With last error 0 the result is 0x00000000 (S_OK, although nothing was filled in). With last error 5 it is 0x80070005. Expected: a deterministic failure, e.g. if (!info) return E_POINTER; as the factory methods do. This is pre-existing and is made slightly more visible by the new "capture the real error" logic, since a stale value is now the returned value. The same applies if the API ever fails with last error 0: HRESULT_FROM_WIN32(0) is S_OK. A error ? HRESULT_FROM_WIN32(error) : E_FAIL guard would cover both.
  2. nit: the test cannot tell "captured the real error" from "always returns ERROR_INVALID_HANDLE". tests/startup_stub_consumer.cpp, dstorage_file_information_checks. The only injected failure is the closed handle, so a constant return passes (mutation 4 above). A second failure with a different code would close the gap, for example a null info once finding 1 is decided, or a handle that fails with another error. Not required for this fix: the ordering bug itself is caught.

Not verified

  • Only Linux was used: MinGW cross-compile and Wine64 (no 32-bit Wine). The driver was my own harness around the PR's test function, not the project's CMake/CTest target, so tests/startup_stub_check.py was not run end to end, nor the existing startup_stubs CTest wiring with its other cases.
  • To build the DLL with MinGW I used a scratch copy of the sources with the SEH __try/__except in local_log.h neutralised and a stub for _ReturnAddress. file_information itself was compiled unchanged. This does not affect the logic under test.
  • Not checked: MSVC build/warnings, a real Windows run, and behaviour of GetFileInformationByHandle on handles to non-regular files (pipes, devices).

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6042669229 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1523#pullrequestreview-6042669229). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T15:52:58Z. **Review:** COMMENTED --- ## Review No blocking problem found. The change does what the title says, and the added test detects the original defect. Verification is Linux only (cross-compiled, run under Wine); see "Not verified". ### Verified - Error capture order. In `file_information` (`src/startup_stubs/xcrdapi.cpp`) the error is now read right after `GetFileInformationByHandle` and before `log()`. Nothing runs in between. `log()` calls `GetEnvironmentVariableW` and, when `X2W_TRAP_LOG` is set, `CreateFileW` (OPEN_ALWAYS) and `CloseHandle`, all of which overwrite the thread's last error. - Error domain. The return value is `HRESULT_FROM_WIN32(error)`, the same domain as `factory_open_file` (line 249), which uses the same capture-then-log-then-return pattern. A closed file object yields `0x80070006` (ERROR_INVALID_HANDLE as an HRESULT). - Success path. Unchanged: `ok` gives `S_OK`, and the size fields are read from `info` as before. - Thread safety. `GetLastError` is per-thread and the value is held in a local, so there is no cross-thread issue. - Siblings. `grep -rn GetLastError src/startup_stubs`: no other stand-in method reads the error after a log call. `factory_open_file` already captures it first. - Build and run. I cross-compiled `xcrdapi.cpp` as a DLL with `x86_64-w64-mingw32-g++ -std=c++17`. I ran the new test body (copied verbatim into a small standalone driver) under Wine64, with a 120 s timeout. Result: both the `X2W_TRAP_LOG` unset and set passes print `closed: hr=0x80070006 expected=0x80070006 ok`, and the open case returns `S_OK` with size 1234. `tests/startup_stub_consumer.cpp` also passes `-fsyntax-only` with the same compiler. - Does the test catch the old code? Mutations applied to the PR's `file_information`, then the same driver: | Mutation | Result | |---|---| | Restore the old code (`GetLastError()` read after `log()`) | FAIL. unset: `0x800700cb` (ERROR_ENVVAR_NOT_FOUND); set: `0x800700b7` (ERROR_ALREADY_EXISTS) | | Keep the structure but move the read after `log()` | FAIL, same codes as above | | Return the raw Win32 code instead of the HRESULT | FAIL, `0x00000006` | | Hard-code `HRESULT_FROM_WIN32(ERROR_INVALID_HANDLE)` | PASS (see finding 2) | Running both the unset and set variants is what makes the test strong: each one exposes a different overwriting call. ### Findings 1. **P2 (not in the diff, same function): a null `info` can return `S_OK`.** `src/startup_stubs/xcrdapi.cpp:307-311`. `ok = info && ...` is false when `info == nullptr`, but no Win32 call has failed, so `GetLastError()` is whatever the thread had before. Repro: call slot 4 with `info = nullptr`. With last error 0 the result is `0x00000000` (S_OK, although nothing was filled in). With last error 5 it is `0x80070005`. Expected: a deterministic failure, e.g. `if (!info) return E_POINTER;` as the factory methods do. This is pre-existing and is made slightly more visible by the new "capture the real error" logic, since a stale value is now the returned value. The same applies if the API ever fails with last error 0: `HRESULT_FROM_WIN32(0)` is `S_OK`. A `error ? HRESULT_FROM_WIN32(error) : E_FAIL` guard would cover both. 2. **nit: the test cannot tell "captured the real error" from "always returns ERROR_INVALID_HANDLE".** `tests/startup_stub_consumer.cpp`, `dstorage_file_information_checks`. The only injected failure is the closed handle, so a constant return passes (mutation 4 above). A second failure with a different code would close the gap, for example a null `info` once finding 1 is decided, or a handle that fails with another error. Not required for this fix: the ordering bug itself is caught. ### Not verified - Only Linux was used: MinGW cross-compile and Wine64 (no 32-bit Wine). The driver was my own harness around the PR's test function, not the project's CMake/CTest target, so `tests/startup_stub_check.py` was not run end to end, nor the existing `startup_stubs` CTest wiring with its other cases. - To build the DLL with MinGW I used a scratch copy of the sources with the SEH `__try/__except` in `local_log.h` neutralised and a stub for `_ReturnAddress`. `file_information` itself was compiled unchanged. This does not affect the logic under test. - Not checked: MSVC build/warnings, a real Windows run, and behaviour of `GetFileInformationByHandle` on handles to non-regular files (pipes, devices). AI-assisted; commands listed were executed locally.
upstream-sync closed this pull request 2026-11-25 03:00:42 +00:00
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!1516
No description provided.