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

Open
opened 2026-11-26 21:19:53 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/2052
Author: @IlyassRS3
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: rt/xg-xs-copy-scratch-and-port-validation


📝 Commits (1)

  • d16029d runtime: reuse thread-local scratch and harden port-model copy validation in xg_xs

📊 Changes

2 files changed (+59 additions, -8 deletions)

View changed files

📝 runtime/shims/xg_xs.cpp (+28 -8)
📝 runtime/test/xg_xs_test.cpp (+31 -0)

📄 Description

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).

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/2052 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `rt/xg-xs-copy-scratch-and-port-validation` --- ### 📝 Commits (1) - [`d16029d`](https://github.com/ubiquitarianism/leonida/commit/d16029daf1f27a8784f70ea89b20d04c6f3f0f18) runtime: reuse thread-local scratch and harden port-model copy validation in xg_xs ### 📊 Changes **2 files changed** (+59 additions, -8 deletions) <details> <summary>View changed files</summary> 📝 `runtime/shims/xg_xs.cpp` (+28 -8) 📝 `runtime/test/xg_xs_test.cpp` (+31 -0) </details> ### 📄 Description 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). --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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.

Dependencies

No dependencies set

Reference
starred/leonida#2056
No description provided.