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

Merged
xroneyaa merged 1 commit from upstream-pr/520 into main 2026-11-24 14:37:55 +00:00
xroneyaa commented 2026-11-24 06:52:19 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by xroneyaa on 2026-11-24T06:52:19Z.


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).
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5467542657 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/520). Originally posted by [xroneyaa](https://github.com/xroneyaa) on 2026-11-24T06:52:19Z. --- 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).
kawaii-shakal commented 2026-11-24 07:47:31 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by kawaii-shakal on 2026-11-24T07:47:31Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7125375682 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/520#issuecomment-7125375682). Originally posted by [kawaii-shakal](https://github.com/kawaii-shakal) on 2026-11-24T07:47:31Z. --- 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._
Sign in to join this conversation.
No reviewers
No labels
No milestone
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.

Reference
ProjectLeonida/mandy!520
No description provided.