d3d12: decline file-backed/multi-VMA host imports under Wine and deduplicate import memory types (RT-WINE-HOST-IMPORT-POLICY) #2126

Open
github-bot wants to merge 1 commit from github-bot/gh-2126 into main AGit
Owner

@⁠djrolfes on GitHub · 2026-11-25 21:43 UTC · original
djrolfes:djrolfes/rt-wine-mapped-host-import → main · +339 −73 · 5 files · 1 commits · draft

Claimed in the coordination issue as: RT-WINE-HOST-IMPORT-POLICY (#1608)

What and why

Under Wine on Linux, nvidia.ko rejects long-term pinning (pin_user_pages / FOLL_LONGTERM) of file-backed MAP_SHARED views (such as the sparse temp files created under LEONIDA_XMEM_BACKING_DIR) with kernel Cannot map memory with base addr ... errors and also rejects host-pointer imports spanning multiple VMAs. As d3d12_vulkan_device.cpp notes, bursts of those kernel rejections on NVIDIA 620.73 under Wine coincide with spurious VK_ERROR_OUT_OF_HOST_MEMORY (-1) responses on concurrent normal vkAllocateMemory calls, causing E_OUTOFMEMORY (0x8007000e) resource-creation failures even with gigabytes of VRAM and RAM free.

This change fixes four related issues in the host-import path without altering native Windows host-import eligibility or anonymous pagefile section imports (the four runtime-path invariants below — post-hold TOCTOU re-check, memory_mutex() around import vkAllocateMemory, single-pass memory-type deduplication, and stopping early on VK_ERROR_INVALID_EXTERNAL_HANDLE — apply on native Windows as well as Wine):

  1. Adds portable runtime/src/host_import_policy.h (HostImportRangeCheck and host_import_memory_types) and CPU unit test runtime/test/host_import_policy_test.cpp:
    • Under Wine (ntdll!wine_get_version), declines MEM_MAPPED regions backed by a named file (K32GetMappedFileNameW > 0) and ranges spanning multiple AllocationBase values before calling Vulkan, while keeping anonymous VirtualAlloc (MEM_PRIVATE) and anonymous pagefile sections (CreateFileMappingW(INVALID_HANDLE_VALUE, ...)) importable.
    • Deduplicates Vulkan memory types across the preferred (HOST_VISIBLE | HOST_COHERENT) and fallback passes so a failing import never probes the same memory type twice.
  2. Re-verifies host_range_importable after leonida::kernel_writes::hold in create_host_buffer and import_console_heap (closing the TOCTOU window between the writability check and the pin) and stops probing further memory types on VK_ERROR_INVALID_EXTERNAL_HANDLE.
  3. Holds DeviceState::memory_mutex() consistently around vkAllocateMemory in import_console_heap, OpenExistingHeapFromAddress, and the ImageMemory block allocator (matching DeviceState::memory_mutex()'s documented invariant).
  4. Adds a synthetic sparse disk-file-backed section view regression case to runtime/test/d3d12_existing_heap_test.cpp verifying shadow fallback without host import under Wine.

Opened as a draft because prior pull requests are currently open (one-open-PR limit).

Tested

  • python3 -I scripts/check_no_game_data.py on all 5 touched/added files: passed.
  • Portable native CTest (runtime/build/native, Linux / Clang 23.1.5): 100/100 passed (98 passed, 2 conditional skips: shader_library_corpus, vk_sync_validation), including new host_import_policy.
  • Synthetic Windows/Wine tests (clang-cl + xwin, Wine, network-isolated, no retail files):
    • host_import_policy_test: passed
    • d3d12_console_descriptor_heap_test: passed
    • d3d12_xmem_placed_test: passed
    • d3d12_existing_heap_test (including new sparse disk-file-backed section case): passed, 0 kernel Cannot map memory errors.
  • Offline --first-run-ui A/B check (no input, 300 s bounded, read-only package/shaders/BCPack predictor, network-isolated, counts/timings only):
    • Clean main (490f5b9ec): exit 126 at 28.42 s (slot 133 depth texture refused, hr=0x8007000e), 16 kernel Cannot map memory errors across 7 MEM_MAPPED addresses, 2 vkAllocateMemory failed (-1), 347 PresentX ok.
    • This branch (f5bc25cdc + fix): ran full 300.88 s time limit (exit 124), 0 kernel Cannot map memory errors, 0 vkAllocateMemory failed (-1), 0 depth-texture refusals, 7,990 PresentX ok, 200 MEM_PRIVATE slot-133 imports succeeded. Rebased cleanly onto 1fe5072af and re-verified host_import_policy_test + d3d12_existing_heap_test.

GPUs tested: NVIDIA GeForce RTX 3070 Ti (8 GB), driver 620.73.04, Linux (Arch/Omarchy) under Wine.

AI-assisted: yes.

Checklist

  • Based on current main (1fe5072af) and merges cleanly.
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a: runtime-only change)
**@⁠djrolfes** on GitHub · 2026-11-25 21:43 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2126) `djrolfes:djrolfes/rt-wine-mapped-host-import` → `main` · +339 −73 · 5 files · 1 commits · draft Claimed in the coordination issue as: RT-WINE-HOST-IMPORT-POLICY (#1608) ### What and why Under Wine on Linux, `nvidia.ko` rejects long-term pinning (`pin_user_pages` / `FOLL_LONGTERM`) of file-backed `MAP_SHARED` views (such as the sparse temp files created under `LEONIDA_XMEM_BACKING_DIR`) with kernel `Cannot map memory with base addr ...` errors and also rejects host-pointer imports spanning multiple VMAs. As `d3d12_vulkan_device.cpp` notes, bursts of those kernel rejections on NVIDIA 620.73 under Wine coincide with spurious `VK_ERROR_OUT_OF_HOST_MEMORY` (`-1`) responses on concurrent normal `vkAllocateMemory` calls, causing `E_OUTOFMEMORY` (`0x8007000e`) resource-creation failures even with gigabytes of VRAM and RAM free. This change fixes four related issues in the host-import path without altering native Windows host-import eligibility or anonymous pagefile section imports (the four runtime-path invariants below — post-`hold` TOCTOU re-check, `memory_mutex()` around import `vkAllocateMemory`, single-pass memory-type deduplication, and stopping early on `VK_ERROR_INVALID_EXTERNAL_HANDLE` — apply on native Windows as well as Wine): 1. Adds portable `runtime/src/host_import_policy.h` (`HostImportRangeCheck` and `host_import_memory_types`) and CPU unit test `runtime/test/host_import_policy_test.cpp`: - Under Wine (`ntdll!wine_get_version`), declines `MEM_MAPPED` regions backed by a named file (`K32GetMappedFileNameW > 0`) and ranges spanning multiple `AllocationBase` values before calling Vulkan, while keeping anonymous `VirtualAlloc` (`MEM_PRIVATE`) and anonymous pagefile sections (`CreateFileMappingW(INVALID_HANDLE_VALUE, ...)`) importable. - Deduplicates Vulkan memory types across the preferred (`HOST_VISIBLE | HOST_COHERENT`) and fallback passes so a failing import never probes the same memory type twice. 2. Re-verifies `host_range_importable` after `leonida::kernel_writes::hold` in `create_host_buffer` and `import_console_heap` (closing the TOCTOU window between the writability check and the pin) and stops probing further memory types on `VK_ERROR_INVALID_EXTERNAL_HANDLE`. 3. Holds `DeviceState::memory_mutex()` consistently around `vkAllocateMemory` in `import_console_heap`, `OpenExistingHeapFromAddress`, and the `ImageMemory` block allocator (matching `DeviceState::memory_mutex()`'s documented invariant). 4. Adds a synthetic sparse disk-file-backed section view regression case to `runtime/test/d3d12_existing_heap_test.cpp` verifying shadow fallback without host import under Wine. Opened as a draft because prior pull requests are currently open (one-open-PR limit). ### Tested - `python3 -I scripts/check_no_game_data.py` on all 5 touched/added files: passed. - Portable native CTest (`runtime/build/native`, Linux / Clang 23.1.5): **100/100 passed** (98 passed, 2 conditional skips: `shader_library_corpus`, `vk_sync_validation`), including new `host_import_policy`. - Synthetic Windows/Wine tests (`clang-cl` + `xwin`, Wine, network-isolated, no retail files): - `host_import_policy_test`: passed - `d3d12_console_descriptor_heap_test`: passed - `d3d12_xmem_placed_test`: passed - `d3d12_existing_heap_test` (including new sparse disk-file-backed section case): passed, **0** kernel `Cannot map memory` errors. - Offline `--first-run-ui` A/B check (no input, 300 s bounded, read-only package/shaders/BCPack predictor, network-isolated, counts/timings only): - Clean `main` (`490f5b9ec`): exit 126 at 28.42 s (`slot 133 depth texture refused`, `hr=0x8007000e`), 16 kernel `Cannot map memory` errors across 7 `MEM_MAPPED` addresses, 2 `vkAllocateMemory failed (-1)`, 347 `PresentX ok`. - This branch (`f5bc25cdc` + fix): ran full 300.88 s time limit (exit 124), **0** kernel `Cannot map memory` errors, **0** `vkAllocateMemory failed (-1)`, **0** depth-texture refusals, **7,990** `PresentX ok`, 200 `MEM_PRIVATE` slot-133 imports succeeded. Rebased cleanly onto `1fe5072af` and re-verified `host_import_policy_test` + `d3d12_existing_heap_test`. GPUs tested: NVIDIA GeForce RTX 3070 Ti (8 GB), driver 620.73.04, Linux (Arch/Omarchy) under Wine. AI-assisted: yes. ### Checklist - [x] Based on current `main` (`1fe5072af`) and merges cleanly. - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (n/a: runtime-only change)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:18:52 +00:00
Author
Owner

@⁠qhukz13 on GitHub · 2026-11-26 00:32 UTC · original

Review at 1431ac4fb: code reading and the policy test (Linux). My Windows test PC is down right now, so no native run in this comment; I can add one on a GTX 1050 Ti / Windows 10 later.

  • The import rules are unchanged outside Wine, as the description says: HostImportRangeCheck with wine == false still requires a committed, writable page without PAGE_WRITECOMBINE | PAGE_NOCACHE | PAGE_GUARD, which is what the removed loops in create_host_buffer / import_console_heap checked. The file-backed and multi-AllocationBase refusals only apply under Wine (wine_get_version).
  • Four things do change on native Windows too, so "without altering native Windows behavior" is a little broad; all four look harmless to me, but worth naming:
    1. the range is checked again after kernel_writes::hold (closes the TOCTOU, as described);
    2. memory_mutex() is now held around the import vkAllocateMemory calls (the invariant its comment states; no caller of these paths holds it already, so no self-deadlock on the plain std::mutex);
    3. a memory type is probed once, not once per pass;
    4. the probe stops at VK_ERROR_INVALID_EXTERNAL_HANDLE. That one I agree with: the error says the pointer is not importable for the handle type, which another memory type does not change.
  • host_import_policy_test builds with plain g++ 20 on Linux (-fsanitize=address,undefined) and passes. It catches both Wine rules: with if (region.mapped_file) return false; removed it fails, and so it does with the allocation_base comparison removed.

The branch merges into current main without conflicts (134 commits behind).

AI-assisted (Claude Code).

**@⁠qhukz13** on GitHub · 2026-11-26 00:32 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2126#issuecomment-7178857330) Review at `1431ac4fb`: code reading and the policy test (Linux). My Windows test PC is down right now, so no native run in this comment; I can add one on a GTX 1050 Ti / Windows 10 later. - **The import rules are unchanged outside Wine,** as the description says: `HostImportRangeCheck` with `wine == false` still requires a committed, writable page without `PAGE_WRITECOMBINE | PAGE_NOCACHE | PAGE_GUARD`, which is what the removed loops in `create_host_buffer` / `import_console_heap` checked. The file-backed and multi-`AllocationBase` refusals only apply under Wine (`wine_get_version`). - **Four things do change on native Windows too,** so "without altering native Windows behavior" is a little broad; all four look harmless to me, but worth naming: 1. the range is checked again after `kernel_writes::hold` (closes the TOCTOU, as described); 2. `memory_mutex()` is now held around the import `vkAllocateMemory` calls (the invariant its comment states; no caller of these paths holds it already, so no self-deadlock on the plain `std::mutex`); 3. a memory type is probed once, not once per pass; 4. the probe stops at `VK_ERROR_INVALID_EXTERNAL_HANDLE`. That one I agree with: the error says the pointer is not importable for the handle type, which another memory type does not change. - **`host_import_policy_test`** builds with plain g++ 20 on Linux (`-fsanitize=address,undefined`) and passes. It catches both Wine rules: with `if (region.mapped_file) return false;` removed it fails, and so it does with the `allocation_base` comparison removed. The branch merges into current main without conflicts (134 commits behind). AI-assisted (Claude Code).
Author
Owner

@⁠djrolfes on GitHub · 2026-11-26 02:05 UTC · original

Thanks @⁠qhukz13 for the review at 1431ac4fb and for checking host_import_policy_test under ASan/UBSan!

Verified all four scope notes against 1431ac4fb by code inspection (and left 1431ac4fb unchanged so existing Wine/retail/Linux reviews remain valid; git merge-tree --write-tree origin/main HEAD is still clean with no conflicts):

  1. Post-hold TOCTOU re-check: host_range_importable runs again after leonida::kernel_writes::hold in both DeviceState::import_console_heap and Resource::create_host_buffer regardless of running_under_wine().
  2. DeviceState::memory_mutex() around vkAllocateMemory: Held strictly around vkAllocateMemory in DeviceState::import_console_heap, Resource::create_host_buffer, Device::OpenExistingHeapFromAddress, and the state_.image_memory block-allocator callback. Auditing all callers (add_console_heap / DescriptorHeap, create_at_host_address / make_title_buffer, OpenExistingHeapFromAddress, and allocate_image / ImageMemoryPool::allocate) confirms none already hold memory_mutex() on entry and none of the critical sections call back into any other mutex or runtime function, so self-deadlock or lock-order inversion cannot occur.
  3. Single-pass memory-type deduplication: host_import_types_for / leonida::host_import_memory_types probes each allowed memory type at most once across the preferred and fallback passes on all platforms.
  4. Early stop on VK_ERROR_INVALID_EXTERNAL_HANDLE: Applied in import_console_heap, create_host_buffer, and OpenExistingHeapFromAddress.

Updated the PR description accordingly to clarify "without altering native Windows host-import eligibility" and explicitly name those four cross-platform runtime-path invariants.

CPU-only verification re-run in this follow-up (no Windows, Wine, GPU, or game-data run):

  • python3 -I scripts/check_no_game_data.py on all 5 touched/added files: passed.
  • cmake -S runtime -B runtime/build && cmake --build runtime/build --target host_import_policy_test --config RelWithDebInfo -j 2 && ctest --test-dir runtime/build -R '^host_import_policy$' --output-on-failure -j 2 (Linux / GCC 16.2.1): 1/1 passed.
  • g++ -std=c++20 -O2 -Wall -Wextra -Werror -fsanitize=address,undefined runtime/test/host_import_policy_test.cpp: passed (ok).

AI-assisted: yes; no game data.

**@⁠djrolfes** on GitHub · 2026-11-26 02:05 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2126#issuecomment-7180442948) Thanks @⁠qhukz13 for the review at `1431ac4fb` and for checking `host_import_policy_test` under ASan/UBSan! Verified all four scope notes against `1431ac4fb` by code inspection (and left `1431ac4fb` unchanged so existing Wine/retail/Linux reviews remain valid; `git merge-tree --write-tree origin/main HEAD` is still clean with no conflicts): 1. **Post-`hold` TOCTOU re-check:** `host_range_importable` runs again after `leonida::kernel_writes::hold` in both `DeviceState::import_console_heap` and `Resource::create_host_buffer` regardless of `running_under_wine()`. 2. **`DeviceState::memory_mutex()` around `vkAllocateMemory`:** Held strictly around `vkAllocateMemory` in `DeviceState::import_console_heap`, `Resource::create_host_buffer`, `Device::OpenExistingHeapFromAddress`, and the `state_.image_memory` block-allocator callback. Auditing all callers (`add_console_heap` / `DescriptorHeap`, `create_at_host_address` / `make_title_buffer`, `OpenExistingHeapFromAddress`, and `allocate_image` / `ImageMemoryPool::allocate`) confirms none already hold `memory_mutex()` on entry and none of the critical sections call back into any other mutex or runtime function, so self-deadlock or lock-order inversion cannot occur. 3. **Single-pass memory-type deduplication:** `host_import_types_for` / `leonida::host_import_memory_types` probes each allowed memory type at most once across the preferred and fallback passes on all platforms. 4. **Early stop on `VK_ERROR_INVALID_EXTERNAL_HANDLE`:** Applied in `import_console_heap`, `create_host_buffer`, and `OpenExistingHeapFromAddress`. Updated the PR description accordingly to clarify `"without altering native Windows host-import eligibility"` and explicitly name those four cross-platform runtime-path invariants. CPU-only verification re-run in this follow-up (no Windows, Wine, GPU, or game-data run): - `python3 -I scripts/check_no_game_data.py` on all 5 touched/added files: passed. - `cmake -S runtime -B runtime/build && cmake --build runtime/build --target host_import_policy_test --config RelWithDebInfo -j 2 && ctest --test-dir runtime/build -R '^host_import_policy$' --output-on-failure -j 2` (Linux / GCC 16.2.1): 1/1 passed. - `g++ -std=c++20 -O2 -Wall -Wextra -Werror -fsanitize=address,undefined runtime/test/host_import_policy_test.cpp`: passed (`ok`). AI-assisted: yes; no game data.
This pull request has changes conflicting with the target branch.
  • runtime/shims/d3d12_vulkan_device.cpp
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2126/head:github-bot/gh-2126'
git switch 'github-bot/gh-2126'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2126'
git switch 'github-bot/gh-2126'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2126'
git switch 'github-bot/gh-2126'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2126'
git switch 'main'
git merge --squash 'github-bot/gh-2126'
git switch 'main'
git merge --ff-only 'github-bot/gh-2126'
git switch 'main'
git merge 'github-bot/gh-2126'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.