shims: guard SEH __try behind _MSC_VER for MinGW (MINGW-SEH-GUARD) #903

Closed
github-bot wants to merge 3 commits from gh-pr/903/head into gh-pr/903/base
Owner

@⁠morodirule on GitHub · 2026-11-24 15:08 UTC · original
fix/mingw-seh-guard → main · +57 −0 · 2 files · 3 commits · closed without merging

Claimed in the coordination issue as: MINGW-SEH-GUARD

Carries abnalstlll-byte's finding (file_system.cpp touch_pages); the same unguarded __try also blocked dstorage.cpp guarded_move and touch_pages, so all three sites are guarded.

Change: the three __try/__except blocks are now #ifdef _MSC_VER, following the existing startup_trace.h pattern. Other compilers probe with VirtualQuery: touch_pages treats a non-committed/guarded page as untouched (ReadFile refuses it as before); guarded_move returns false when a source page is unreadable or a destination page unwritable, keeping its E_DSTORAGE_ACCESS_VIOLATION contract. The MSVC branch is unchanged (diff is +45/-0).

Tests: x86_64-w64-mingw32-g++ -fsyntax-only and -c -O1 of both translation units pass (baseline: error: expected 'catch' before '__except' in both files). Linux native build unaffected (Windows-only shims; not in the portable subset). check_no_game_data.py PASS. No game files, no GPU. MSVC compile not available on this machine.

AI-assisted.

**@⁠morodirule** on GitHub · 2026-11-24 15:08 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/903) `fix/mingw-seh-guard` → `main` · +57 −0 · 2 files · 3 commits · closed without merging Claimed in the coordination issue as: MINGW-SEH-GUARD Carries abnalstlll-byte's finding (file_system.cpp touch_pages); the same unguarded __try also blocked dstorage.cpp guarded_move and touch_pages, so all three sites are guarded. Change: the three __try/__except blocks are now #ifdef _MSC_VER, following the existing startup_trace.h pattern. Other compilers probe with VirtualQuery: touch_pages treats a non-committed/guarded page as untouched (ReadFile refuses it as before); guarded_move returns false when a source page is unreadable or a destination page unwritable, keeping its E_DSTORAGE_ACCESS_VIOLATION contract. The MSVC branch is unchanged (diff is +45/-0). Tests: x86_64-w64-mingw32-g++ -fsyntax-only and -c -O1 of both translation units pass (baseline: error: expected 'catch' before '__except' in both files). Linux native build unaffected (Windows-only shims; not in the portable subset). check_no_game_data.py PASS. No game files, no GPU. MSVC compile not available on this machine. AI-assisted.
file_system touch_pages plus dstorage guarded_move and touch_pages used MSVC __try/__except unguarded, so MinGW stops at the first __except. Non-MSVC builds now probe pages with VirtualQuery instead (same startup_trace.h convention); guarded_move pre-validates source readability and destination writability to keep its false-on-inaccessible contract.
The VirtualQuery fallback rejected PAGE_GUARD outright, but the MSVC
__except filter catches only EXCEPTION_ACCESS_VIOLATION, so a guard-page
touch propagates to vectored handlers there. dstorage_test's hold_on_guard
VEH depends on the copy reaching the guard page; rejecting it turned the
held copy into E_DSTORAGE_ACCESS_VIOLATION (2 extra dstorage_test failures
vs the clang-cl build under Wine: 4/329 instead of 2/329).

Verified with a MinGW build of dstorage_test under Wine 11.22 on Linux:
before 4 failures (guard hold + pending + held copy + moved bytes), after
2 failures (guard hold + pending only), matching the clang-cl baseline.
The remaining two are a Wine artifact: the guard touch completes without
dispatching the VEH in the full test binary under Wine, for both
toolchains.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:27:19 +00:00
github-bot closed this pull request 2026-11-26 09:27:19 +00:00
Author
Owner

@⁠morodirule on GitHub · 2026-11-24 15:26 UTC · original

Self-review hardening: the MinGW guarded_move VirtualQuery loop now rejects a range whose size wraps uintptr_t and uses an inclusive last-page loop so stepping the final address cannot wrap/infinite-loop. MinGW -fsyntax-only and -c of dstorage.cpp pass.

**@⁠morodirule** on GitHub · 2026-11-24 15:26 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/903#issuecomment-7136037828) Self-review hardening: the MinGW guarded_move VirtualQuery loop now rejects a range whose size wraps uintptr_t and uses an inclusive last-page loop so stepping the final address cannot wrap/infinite-loop. MinGW -fsyntax-only and -c of dstorage.cpp pass.
Author
Owner

@⁠morodirule on GitHub · 2026-11-24 15:27 UTC · original

Review caveat: the non-MSVC fallback queries page state but cannot itself trigger xmem's first-touch VEH, unlike the MSVC volatile read. This follows the original finding's VirtualQuery fallback recommendation and keeps invalid pointers from crashing a MinGW build, but MinGW may not get the same first-touch commit behavior for xmem-backed ReadFile/DStorage destinations. Wine's current file_system_test reports the two new T: checks passing; its shimmed ReadFile first-touch check also passes there, but that does not establish native Windows behavior. Please treat MinGW first-touch parity as unverified; if MinGW runtime support requires it, it needs a separate safe mechanism/test.

**@⁠morodirule** on GitHub · 2026-11-24 15:27 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/903#issuecomment-7136045261) Review caveat: the non-MSVC fallback queries page state but cannot itself trigger xmem's first-touch VEH, unlike the MSVC volatile read. This follows the original finding's VirtualQuery fallback recommendation and keeps invalid pointers from crashing a MinGW build, but MinGW may not get the same first-touch commit behavior for xmem-backed ReadFile/DStorage destinations. Wine's current file_system_test reports the two new T: checks passing; its shimmed ReadFile first-touch check also passes there, but that does not establish native Windows behavior. Please treat MinGW first-touch parity as unverified; if MinGW runtime support requires it, it needs a separate safe mechanism/test.
Author
Owner

@⁠morodirule on GitHub · 2026-11-24 15:27 UTC · original

Correction to my previous comment: I omitted the function name while shell quoting. The caveat is specifically about touch_pages: the non-MSVC branch only queries page state and cannot itself trigger xmem’s first-touch VEH, unlike the MSVC volatile read. The VirtualQuery fallback matches the original finding’s recommendation and avoids unsafe reads, but native Windows MinGW first-touch behavior is unverified. Wine’s test passing does not establish native behavior; if MinGW runtime support requires parity, this needs a separate safe mechanism and test.

**@⁠morodirule** on GitHub · 2026-11-24 15:27 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/903#issuecomment-7136050585) Correction to my previous comment: I omitted the function name while shell quoting. The caveat is specifically about `touch_pages`: the non-MSVC branch only queries page state and cannot itself trigger xmem’s first-touch VEH, unlike the MSVC volatile read. The VirtualQuery fallback matches the original finding’s recommendation and avoids unsafe reads, but native Windows MinGW first-touch behavior is unverified. Wine’s test passing does not establish native behavior; if MinGW runtime support requires parity, this needs a separate safe mechanism and test.
Author
Owner

@⁠moffington on GitHub · 2026-11-24 15:50 UTC · original

Reviewed 7e47b210d using MinGW-w64 and clang-cl/xwin on Linux with Wine 11.20.

Confirmed both file_system.cpp and dstorage.cpp fail MinGW compilation on baseline and compile successfully on HEAD.

One additional behavioral difference worth noting: the MinGW guarded_move fallback rejects PAGE_GUARD before accessing the buffer. This produces two additional failures in dstorage_test compared with the clang-cl build (4/329 failures vs. 2/329), specifically the held copy succeeding and moving every byte.

Synthetic probes also confirmed that reserved buffers with first-touch VEH handlers return E_DSTORAGE_ACCESS_VIOLATION without invoking the handler. Ordinary committed buffers work correctly.

I saw the existing discussion about the first-touch limitation in touch_pages, but wanted to flag the DirectStorage test impact as well.

Clean merge-tree against main and #929. Note that #929 contains the first commit of this PR but not the subsequent wrap-around hardening commit.

Native MSVC was not tested, and the clang-cl synthetic exception probes could not complete under Wine.

**@⁠moffington** on GitHub · 2026-11-24 15:50 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/903#issuecomment-7136693110) Reviewed `7e47b210d` using MinGW-w64 and clang-cl/xwin on Linux with Wine 11.20. Confirmed both `file_system.cpp` and `dstorage.cpp` fail MinGW compilation on baseline and compile successfully on HEAD. One additional behavioral difference worth noting: the MinGW `guarded_move` fallback rejects PAGE_GUARD before accessing the buffer. This produces two additional failures in `dstorage_test` compared with the clang-cl build (4/329 failures vs. 2/329), specifically the held copy succeeding and moving every byte. Synthetic probes also confirmed that reserved buffers with first-touch VEH handlers return `E_DSTORAGE_ACCESS_VIOLATION` without invoking the handler. Ordinary committed buffers work correctly. I saw the existing discussion about the first-touch limitation in `touch_pages`, but wanted to flag the DirectStorage test impact as well. Clean merge-tree against main and #929. Note that #929 contains the first commit of this PR but not the subsequent wrap-around hardening commit. Native MSVC was not tested, and the clang-cl synthetic exception probes could not complete under Wine.
Author
Owner

@⁠morodirule on GitHub · 2026-11-24 16:16 UTC · original

Thank you for the thorough review — all four points acted on:

  1. PAGE_GUARD divergence fixed (commit 85c78e20f): the MinGW range_ok no longer rejects guard pages. You were right about the semantics — the MSVC filter catches only AV, so a guard touch propagates to VEHs. I reproduced your numbers exactly with a MinGW build of dstorage_test under Wine 11.22 on Linux (MinGW-w64 + static link, test-only _ReturnAddress shims for linking): before, the same 4 failures including the held copy succeeding/moving bytes; after, 2/329 with only "the copy reached the guard page" and "a running request is pending" remaining. The held copy now succeeds and moves every byte.

  2. The 2 remaining match your clang-cl Wine baseline, and look like a Wine artifact rather than a toolchain gap: the guard touch completes without dispatching hold_on_guard in the full test binary under Wine (also true for the MSVC-syntax build, whose AV-only filter likewise never sees it). Minimal VEH + guard probes on the same setup dispatch fine, so I could not pin the full-binary difference down; either way it affects both toolchains equally, and native Windows is where the VEH path actually matters.

  3. Reserve/first-touch limitation acknowledged for guarded_move as well: a reserve-only xmem range still returns E_DSTORAGE_ACCESS_VIOLATION without invoking the handler, same class as the touch_pages caveat. Only a real fault can commit those pages, which needs SEH — beyond what a VirtualQuery fallback can do. Noted in the code comment.

  4. #929 rebased onto the new head (5a0d521c2): it now contains both the wrap-around hardening and the guard fix. Rebuilt file_system_test from the rebased branch under Wine: 496 checks, 11 failures, identical to the reported baseline (same Wine-env MAXIMUM_ALLOWED/long-path set, no scratch failures).

Not covered here: native MSVC testing (no Windows/MSVC on this machine). MinGW -fsyntax-only/-c plus the Wine runs above were done on Arch Linux, x86_64-w64-mingw32-g++ and Wine 11.22.

**@⁠morodirule** on GitHub · 2026-11-24 16:16 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/903#issuecomment-7137396764) Thank you for the thorough review — all four points acted on: 1. PAGE_GUARD divergence fixed (commit 85c78e20f): the MinGW range_ok no longer rejects guard pages. You were right about the semantics — the MSVC filter catches only AV, so a guard touch propagates to VEHs. I reproduced your numbers exactly with a MinGW build of dstorage_test under Wine 11.22 on Linux (MinGW-w64 + static link, test-only _ReturnAddress shims for linking): before, the same 4 failures including the held copy succeeding/moving bytes; after, 2/329 with only "the copy reached the guard page" and "a running request is pending" remaining. The held copy now succeeds and moves every byte. 2. The 2 remaining match your clang-cl Wine baseline, and look like a Wine artifact rather than a toolchain gap: the guard touch completes without dispatching hold_on_guard in the full test binary under Wine (also true for the MSVC-syntax build, whose AV-only filter likewise never sees it). Minimal VEH + guard probes on the same setup dispatch fine, so I could not pin the full-binary difference down; either way it affects both toolchains equally, and native Windows is where the VEH path actually matters. 3. Reserve/first-touch limitation acknowledged for guarded_move as well: a reserve-only xmem range still returns E_DSTORAGE_ACCESS_VIOLATION without invoking the handler, same class as the touch_pages caveat. Only a real fault can commit those pages, which needs SEH — beyond what a VirtualQuery fallback can do. Noted in the code comment. 4. #929 rebased onto the new head (5a0d521c2): it now contains both the wrap-around hardening and the guard fix. Rebuilt file_system_test from the rebased branch under Wine: 496 checks, 11 failures, identical to the reported baseline (same Wine-env MAXIMUM_ALLOWED/long-path set, no scratch failures). Not covered here: native MSVC testing (no Windows/MSVC on this machine). MinGW -fsyntax-only/-c plus the Wine runs above were done on Arch Linux, x86_64-w64-mingw32-g++ and Wine 11.22.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.