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

Closed
morodirule wants to merge 3 commits from upstream-pr/903 into main
morodirule commented 2026-11-24 15:08:50 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by morodirule on 2026-11-24T15:08:50Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5474806135 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/903). Originally posted by [morodirule](https://github.com/morodirule) on 2026-11-24T15:08:50Z. --- 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 commented 2026-11-24 15:26:44 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by morodirule on 2026-11-24T15:26:44Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7136037828 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/903#issuecomment-7136037828). Originally posted by [morodirule](https://github.com/morodirule) on 2026-11-24T15:26:44Z. --- 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 commented 2026-11-24 15:27:00 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by morodirule on 2026-11-24T15:27:00Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7136045261 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/903#issuecomment-7136045261). Originally posted by [morodirule](https://github.com/morodirule) on 2026-11-24T15:27:00Z. --- 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 commented 2026-11-24 15:27:11 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by morodirule on 2026-11-24T15:27:11Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7136050585 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/903#issuecomment-7136050585). Originally posted by [morodirule](https://github.com/morodirule) on 2026-11-24T15:27:11Z. --- 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.
moffington commented 2026-11-24 15:50:09 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by moffington on 2026-11-24T15:50:09Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7136693110 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/903#issuecomment-7136693110). Originally posted by [moffington](https://github.com/moffington) on 2026-11-24T15:50:09Z. --- 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.
morodirule commented 2026-11-24 16:16:32 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by morodirule on 2026-11-24T16:16:32Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7137396764 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/903#issuecomment-7137396764). Originally posted by [morodirule](https://github.com/morodirule) on 2026-11-24T16:16:32Z. --- 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

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
ProjectLeonida/mandy!903
No description provided.