VULKAN-SCRATCH-ALIGN-OVERFLOW: avoid uint64 overflow when aligning chunk.used #520

Manually merged
github merged 1 commit from gh-pr/520/head into gh-pr/520/base 2026-11-26 09:05:45 +00:00
Owner

@⁠xroneyaa on GitHub · 2026-11-24 06:52 UTC · original
xroneyaa:fix/vulkan-scratch-align-overflow → main · +31 −6 · 2 files · 1 commits · merged 2026-11-24 14:37 UTC

Claimed in the coordination issue as: VULKAN-SCRATCH-ALIGN-OVERFLOW

What and why

In runtime/src/vulkan_scratch.h, ScratchBuffers::get rounded chunk.used up to a 256-byte boundary with:

const VkDeviceSize start = (chunk.used + alignment - 1) / alignment * alignment;

When chunk.used > UINT64_MAX - 255, chunk.used + alignment - 1 wraps around uint64_t and start becomes 0, passing start <= chunk.size && bytes <= chunk.size - start and returning offset 0 inside an already occupied chunk.

This computes the alignment padding cleanly (rem = chunk.used % alignment; pad = rem ? alignment - rem : 0) and checks pad <= chunk.size - chunk.used before forming start = chunk.used + pad and checking bytes <= chunk.size - start. It also adds a regression case to runtime/test/vulkan_scratch_test.cpp verifying a UINT64_MAX - 100 byte allocation followed by a 16 byte allocation spills to a second chunk rather than wrapping to offset 0 in chunk 1.

Tested

  • Rebuilt vulkan_scratch_test with MSVC 19.50 / Ninja (0 warnings under /W4)
  • ctest --test-dir runtime/build --output-on-failure -R "^vulkan_scratch$" -> 1/1 Passed (0.01 s)
  • python -I scripts/check_no_game_data.py runtime/src/vulkan_scratch.h runtime/test/vulkan_scratch_test.cpp -> exit 0

GPUs tested: n/a (vulkan_scratch runs CPU-only with fake device procs)

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • 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 — no translator rule added).
**@⁠xroneyaa** on GitHub · 2026-11-24 06:52 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/520) `xroneyaa:fix/vulkan-scratch-align-overflow` → `main` · +31 −6 · 2 files · 1 commits · merged 2026-11-24 14:37 UTC Claimed in the coordination issue as: VULKAN-SCRATCH-ALIGN-OVERFLOW ### What and why In `runtime/src/vulkan_scratch.h`, `ScratchBuffers::get` rounded `chunk.used` up to a 256-byte boundary with: ```cpp const VkDeviceSize start = (chunk.used + alignment - 1) / alignment * alignment; ``` When `chunk.used > UINT64_MAX - 255`, `chunk.used + alignment - 1` wraps around `uint64_t` and `start` becomes `0`, passing `start <= chunk.size && bytes <= chunk.size - start` and returning offset `0` inside an already occupied chunk. This computes the alignment padding cleanly (`rem = chunk.used % alignment; pad = rem ? alignment - rem : 0`) and checks `pad <= chunk.size - chunk.used` before forming `start = chunk.used + pad` and checking `bytes <= chunk.size - start`. It also adds a regression case to `runtime/test/vulkan_scratch_test.cpp` verifying a `UINT64_MAX - 100` byte allocation followed by a `16` byte allocation spills to a second chunk rather than wrapping to offset `0` in chunk 1. ### Tested - Rebuilt `vulkan_scratch_test` with MSVC 19.50 / Ninja (0 warnings under `/W4`) - `ctest --test-dir runtime/build --output-on-failure -R "^vulkan_scratch$"` -> 1/1 Passed (`0.01 s`) - `python -I scripts/check_no_game_data.py runtime/src/vulkan_scratch.h runtime/test/vulkan_scratch_test.cpp` -> exit 0 GPUs tested: n/a (`vulkan_scratch` runs CPU-only with fake device procs) AI-assisted: yes ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [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 — no translator rule added).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:05:43 +00:00
github manually merged commit aab57e15b9 into gh-pr/520/base 2026-11-26 09:05:45 +00:00
Author
Owner

@⁠kawaii-shakal on GitHub · 2026-11-24 07:47 UTC · original

Tested aab57e15b on Linux (g++ 16.2, CPU only, public Vulkan-Headers v1.4.313). No defect found.

  • runtime/test/vulkan_scratch_test.cpp built with ASan and UBSan passes. With the PR's base vulkan_scratch.h it fails at the new check (buf2 != buf1 && off2 == 0), so the test catches the wrap.
  • I compared the old and new alignment math directly over 630 combinations of used, size and bytes around 0, 256, 2^32 and UINT64_MAX. In the 60 where the old used + alignment - 1 wrapped, the new code never returns an offset below used. In every other case both give the same accept/refuse result and the same offset.

Not tested: Windows, MSVC, a real Vulkan device.


Review written by Claude (Claude Code, Opus 5.5), an AI agent, posted from a contributor's account.

**@⁠kawaii-shakal** on GitHub · 2026-11-24 07:47 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/520#issuecomment-7125375682) Tested `aab57e15b` on Linux (g++ 16.2, CPU only, public Vulkan-Headers v1.4.313). No defect found. - `runtime/test/vulkan_scratch_test.cpp` built with ASan and UBSan passes. With the PR's base `vulkan_scratch.h` it fails at the new check (`buf2 != buf1 && off2 == 0`), so the test catches the wrap. - I compared the old and new alignment math directly over 630 combinations of `used`, `size` and `bytes` around 0, 256, 2^32 and `UINT64_MAX`. In the 60 where the old `used + alignment - 1` wrapped, the new code never returns an offset below `used`. In every other case both give the same accept/refuse result and the same offset. Not tested: Windows, MSVC, a real Vulkan device. --- _Review written by Claude (Claude Code, Opus 5.5), an AI agent, posted from a contributor's account._
This discussion has been locked. Commenting is limited to contributors.
No description provided.