runtime: reuse thread-local scratch and harden port-model copy validation in xg_xs #2052

Closed
leonida/mandy
quietfold wants to merge 0 commits from rt/xg-xs-copy-scratch-and-port-validation into main

Pull request closed

This pull request cannot be reopened because the branch was deleted.

quietfold commented 2026-11-25 19:03:42 +00:00

Claim ID: RT-XG-XS-COPY-SCRATCH-AND-PORT-VALIDATION

Summary

Eliminates per-copy heap allocation on the primary xg_xs.dll copy path and brings the second (port) layout model's copy and offset validation into parity with the primary model (runtime/shims/xg_xs.cpp):

  1. Reuse thread-local column-offset scratch in copy_subresource: copy_elements already resizes and overwrites the caller-supplied std::vector<uint64_t>& columns, but copy_subresource previously allocated and freed a fresh vector on every XGCopyIntoSubresource / XGCopyFromSubresource call. Reusing static thread_local std::vector<uint64_t> columns avoids per-call heap churn (and catches std::bad_alloc -> E_OUTOFMEMORY instead of unwinding across __stdcall).
  2. Validate 3D depth_pitch and overflow in port::copy_args: Reject row < bytes, zero height/depth, depths > 1 && depth < one_slice (previously allowed undersized 3D depth pitch to silently read/overwrite slice 0), and p.offset + l.size uint64 wraparound.
  3. Reject overlapping buffers and nonzero sample in port::copy_into / port::copy_from / port::element_offset: Check leonida::gfx10::tiling_detail::overlap before calling tile()/detile() (preventing std::memcpy overlap UB, matching primary copy_subresource), and reject sample != 0 in port::copy_from (E_INVALIDARG) and port::element_offset (0).
  4. Regression tests: Adds synthetic 3D render-target second-model checks in runtime/test/xg_xs_test.cpp covering round-trip copy, undersized 3D depth pitch, nonzero sample, buffer overlap, and element_offset sample rejection.

Verification

  • AI-assisted: yes (Antigravity agent by Google DeepMind)
  • GPUs tested: none (verified locally on macOS arm64 compiling real runtime/shims/xg_xs.cpp + xg_xs_layout.h + console_texture_layout.h + gfx10_tiling.h with minimal Win32/shim stubs; Windows MSVC CI run of xg_xs pending)
  • Sanitizers & tests:
    • clang++ -std=c++20 -O1 -g -fsanitize=address,undefined -Wall -Wextra -Werror: PASS (failures=0)
    • clang++ -std=c++20 -O1 -g -fsanitize=thread -Wall -Wextra -Werror (4 threads x 25 alternating 128x64 RGBA8 / 16x16 R8 copy_into/copy_from round-trips + all second-model validation checks): PASS (failures=0)
    • -O2 A/B check (200,000 64x4 RGBA8 SW_4KB_S copy_into calls): warm 105.655 ms -> 101.226 ms (cold 130.911 ms -> 123.630 ms), zero per-call heap allocations, bit-exact checksum=130620.
    • Negative control on main: failed all 5 new port validation assertions (main_exit=1).
  • Repository checks: python3 -I scripts/check_no_game_data.py clean; python3 -I tests/test_text_write_line_endings.py (3/3 OK).
Claim ID: `RT-XG-XS-COPY-SCRATCH-AND-PORT-VALIDATION` ### Summary Eliminates per-copy heap allocation on the primary `xg_xs.dll` copy path and brings the second (`port`) layout model's copy and offset validation into parity with the primary model (`runtime/shims/xg_xs.cpp`): 1. **Reuse thread-local column-offset scratch in `copy_subresource`:** `copy_elements` already resizes and overwrites the caller-supplied `std::vector<uint64_t>& columns`, but `copy_subresource` previously allocated and freed a fresh vector on every `XGCopyIntoSubresource` / `XGCopyFromSubresource` call. Reusing `static thread_local std::vector<uint64_t> columns` avoids per-call heap churn (and catches `std::bad_alloc` -> `E_OUTOFMEMORY` instead of unwinding across `__stdcall`). 2. **Validate 3D `depth_pitch` and overflow in `port::copy_args`:** Reject `row < bytes`, zero height/depth, `depths > 1 && depth < one_slice` (previously allowed undersized 3D depth pitch to silently read/overwrite slice 0), and `p.offset + l.size` uint64 wraparound. 3. **Reject overlapping buffers and nonzero `sample` in `port::copy_into` / `port::copy_from` / `port::element_offset`:** Check `leonida::gfx10::tiling_detail::overlap` before calling `tile()`/`detile()` (preventing `std::memcpy` overlap UB, matching primary `copy_subresource`), and reject `sample != 0` in `port::copy_from` (`E_INVALIDARG`) and `port::element_offset` (`0`). 4. **Regression tests:** Adds synthetic 3D render-target second-model checks in `runtime/test/xg_xs_test.cpp` covering round-trip copy, undersized 3D depth pitch, nonzero sample, buffer overlap, and `element_offset` sample rejection. ### Verification - **AI-assisted:** yes (Antigravity agent by Google DeepMind) - **GPUs tested:** none (verified locally on macOS arm64 compiling real `runtime/shims/xg_xs.cpp` + `xg_xs_layout.h` + `console_texture_layout.h` + `gfx10_tiling.h` with minimal Win32/shim stubs; Windows MSVC CI run of `xg_xs` pending) - **Sanitizers & tests:** - `clang++ -std=c++20 -O1 -g -fsanitize=address,undefined -Wall -Wextra -Werror`: PASS (`failures=0`) - `clang++ -std=c++20 -O1 -g -fsanitize=thread -Wall -Wextra -Werror` (4 threads x 25 alternating `128x64 RGBA8` / `16x16 R8` `copy_into`/`copy_from` round-trips + all second-model validation checks): PASS (`failures=0`) - `-O2` A/B check (200,000 `64x4 RGBA8 SW_4KB_S` `copy_into` calls): warm `105.655 ms -> 101.226 ms` (cold `130.911 ms -> 123.630 ms`), zero per-call heap allocations, bit-exact `checksum=130620`. - Negative control on `main`: failed all 5 new `port` validation assertions (`main_exit=1`). - **Repository checks:** `python3 -I scripts/check_no_game_data.py` clean; `python3 -I tests/test_text_write_line_endings.py` (3/3 OK).
copilot-pull-request-reviewer[bot] reviewed 2026-11-25 19:03:55 +00:00
copilot-pull-request-reviewer[bot] left a comment
No description provided.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
MoonEyes commented 2026-11-28 02:51:58 +00:00

Hi @IlyassRS3, thanks for this pull request. It currently conflicts with main (conflicting file(s): runtime/test/xg_xs_test.cpp), so it carries the needs rebase label. CONTRIBUTING.md asks authors to rebase their branch on main; once it merges cleanly the label goes within the hour, and reviews start with the pull requests that do. Could you update the branch when you have a moment? If you no longer plan to continue with it, please say so here.

AI-assisted (Claude Code, Claude Opus 5.5).

Hi @IlyassRS3, thanks for this pull request. It currently conflicts with `main` (conflicting file(s): `runtime/test/xg_xs_test.cpp`), so it carries the `needs rebase` label. CONTRIBUTING.md asks authors to rebase their branch on `main`; once it merges cleanly the label goes within the hour, and reviews start with the pull requests that do. Could you update the branch when you have a moment? If you no longer plan to continue with it, please say so here. AI-assisted (Claude Code, Claude Opus 5.5).
OreoFR commented 2026-11-28 21:59:05 +00:00

Verified by OreoFR

  • Base: main c1ecf04ed + PR head d16029daf
  • Test: not run (merge conflict with main)
  • Red on main: not run
  • Green with the PR: not run
  • Verdict: CONFLICT (runtime/test/xg_xs_test.cpp conflicts with main; needs a rebase)
  • Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11

AI-assisted (Claude Code, Claude Opus 5.5).

**Verified by OreoFR** - Base: main `c1ecf04ed` + PR head `d16029daf` - Test: not run (merge conflict with main) - Red on main: not run - Green with the PR: not run - Verdict: CONFLICT (runtime/test/xg_xs_test.cpp conflicts with main; needs a rebase) - Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11 AI-assisted (Claude Code, Claude Opus 5.5).
aslan2142 commented 2026-12-01 16:41:50 +00:00

Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (d3d12/) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, d3d12/, or general docs), please feel free to reopen it.

Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (`d3d12/`) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, `d3d12/`, or general docs), please feel free to reopen it.
Sign in to join this conversation.
No description provided.