[PR #520] [MERGED] VULKAN-SCRATCH-ALIGN-OVERFLOW: avoid uint64 overflow when aligning chunk.used #568

Closed
opened 2026-11-26 20:18:19 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/520
Author: @xroneyaa
Created: 11/24/2026
Status: ✅ Merged
Merged: 11/24/2026
Merged by: @ubiquitarianism

Base: main ← Head: fix/vulkan-scratch-align-overflow


📝 Commits (1)

  • aab57e1 vulkan_scratch: avoid uint64 overflow when aligning chunk.used

📊 Changes

2 files changed (+31 additions, -6 deletions)

View changed files

📝 runtime/src/vulkan_scratch.h (+10 -6)
📝 runtime/test/vulkan_scratch_test.cpp (+21 -0)

📄 Description

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

🔄 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/520 **Author:** [@xroneyaa](https://github.com/xroneyaa) **Created:** 11/24/2026 **Status:** ✅ Merged **Merged:** 11/24/2026 **Merged by:** [@ubiquitarianism](https://github.com/ubiquitarianism) **Base:** `main` ← **Head:** `fix/vulkan-scratch-align-overflow` --- ### 📝 Commits (1) - [`aab57e1`](https://github.com/ubiquitarianism/leonida/commit/aab57e15b96a7b7f717cf9b43030c490ac98ba71) vulkan_scratch: avoid uint64 overflow when aligning chunk.used ### 📊 Changes **2 files changed** (+31 additions, -6 deletions) <details> <summary>View changed files</summary> 📝 `runtime/src/vulkan_scratch.h` (+10 -6) 📝 `runtime/test/vulkan_scratch_test.cpp` (+21 -0) </details> ### 📄 Description 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). --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
4ld0 2026-11-26 20:18:19 +01:00
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#568
No description provided.