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

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

@⁠IlyassRS3 on GitHub · 2026-11-25 19:03 UTC · original
IlyassRS3:rt/xg-xs-copy-scratch-and-port-validation → main · +59 −8 · 2 files · 1 commits · open

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).
**@⁠IlyassRS3** on GitHub · 2026-11-25 19:03 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2052) `IlyassRS3:rt/xg-xs-copy-scratch-and-port-validation` → `main` · +59 −8 · 2 files · 1 commits · open 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).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:16:09 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 19:03 UTC · original · reviewed

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.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-25 19:03 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2052#pullrequestreview-6063363946) · **reviewed** 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.
This pull request has changes conflicting with the target branch.
  • runtime/test/xg_xs_test.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/2052/head:github-bot/gh-2052'
git switch 'github-bot/gh-2052'

Merge

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