startup: return the failed call's error from the stand-in's File::GetFileInformation #1516
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1523"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from R0Y00/Xbox2Windows. Originally posted by RenzoWit35 on 2026-11-24T13:37:43Z.
Change
file_informationinsrc/startup_stubs/xcrdapi.cpp(the stand-in'sIDStorageFile::GetFileInformation) calledlog(...)and only then built its result withHRESULT_FROM_WIN32(GetLastError()).log()callsGetEnvironmentVariableW(andCreateFileWwhenX2W_TRAP_LOGis set), which overwrite the thread's last error. A failedGetFileInformationByHandletherefore returned the logger's error instead of its own.The error is now captured right after the failing call and before
log(), the same patternfactory_open_fileuses. This was the only place in the file that readGetLastError()after alog()call;factory_open_filealready captures first. PassingGetLastError()as alog()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 touchesxcrdapi.cpponly as diff context nearfile_information, and #1482 only adds alogfcheck.Verification
Environment: Windows 11, MSVC 14.51,
cmake --preset dev, baseorigin/main60c72330.New scenario
dstorage-file-informationintests/startup_stub_consumer.cpp, run fromtests/startup_stub_check.py(CTeststartup_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 holdsINVALID_HANDLE_VALUE, so there is no use-after-free), calls slot 4 again and expectsHRESULT_FROM_WIN32(ERROR_INVALID_HANDLE)= 0x80070006. It does this withX2W_TRAP_LOGunset and set.Negative control, without the fix (
startup_stub_consumer.exe dstorage-file-information, exit 10):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:
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_shimRun:
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
infois null,file_informationskips the Win32 call, soGetLastError()is whatever the caller left (possibly 0, which maps to S_OK).factory_open_filereturnsE_POINTERfor a null out pointer; the same could be done here in a separate change.Checklist
AI-assisted.
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
file_information(src/startup_stubs/xcrdapi.cpp) the error is now read right afterGetFileInformationByHandleand beforelog(). Nothing runs in between.log()callsGetEnvironmentVariableWand, whenX2W_TRAP_LOGis set,CreateFileW(OPEN_ALWAYS) andCloseHandle, all of which overwrite the thread's last error.HRESULT_FROM_WIN32(error), the same domain asfactory_open_file(line 249), which uses the same capture-then-log-then-return pattern. A closed file object yields0x80070006(ERROR_INVALID_HANDLE as an HRESULT).okgivesS_OK, and the size fields are read frominfoas before.GetLastErroris per-thread and the value is held in a local, so there is no cross-thread issue.grep -rn GetLastError src/startup_stubs: no other stand-in method reads the error after a log call.factory_open_filealready captures it first.xcrdapi.cppas a DLL withx86_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 theX2W_TRAP_LOGunset and set passes printclosed: hr=0x80070006 expected=0x80070006 ok, and the open case returnsS_OKwith size 1234.tests/startup_stub_consumer.cppalso passes-fsyntax-onlywith the same compiler.file_information, then the same driver:GetLastError()read afterlog())0x800700cb(ERROR_ENVVAR_NOT_FOUND); set:0x800700b7(ERROR_ALREADY_EXISTS)log()0x00000006HRESULT_FROM_WIN32(ERROR_INVALID_HANDLE)Running both the unset and set variants is what makes the test strong: each one exposes a different overwriting call.
Findings
infocan returnS_OK.src/startup_stubs/xcrdapi.cpp:307-311.ok = info && ...is false wheninfo == nullptr, but no Win32 call has failed, soGetLastError()is whatever the thread had before. Repro: call slot 4 withinfo = nullptr. With last error 0 the result is0x00000000(S_OK, although nothing was filled in). With last error 5 it is0x80070005. 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)isS_OK. Aerror ? HRESULT_FROM_WIN32(error) : E_FAILguard would cover both.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 nullinfoonce 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
tests/startup_stub_check.pywas not run end to end, nor the existingstartup_stubsCTest wiring with its other cases.__try/__exceptinlocal_log.hneutralised and a stub for_ReturnAddress.file_informationitself was compiled unchanged. This does not affect the logic under test.GetFileInformationByHandleon handles to non-regular files (pipes, devices).AI-assisted; commands listed were executed locally.