DEPTH24-SCRATCH-BOUNDS-OVERFLOW: guard integer overflows in Depth24Converter #524

Closed
xroneyaa wants to merge 1 commit from upstream-pr/524 into main
xroneyaa commented 2026-11-24 06:55:50 +00:00 (Migrated from github.com)

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


Claimed in the coordination issue as: DEPTH24-SCRATCH-BOUNDS-OVERFLOW

What and why

Depth24Converter in runtime/src/vulkan_depth24.h had several integer-overflow and pre-validation gaps:

  1. prepare():
    • whole.app_offset > whole.app_size was not checked before whole.app_offset + span_words * 4 > whole.app_size, so an offset near UINT64_MAX could wrap past 0 and pass bounds validation.
    • (s - 1) * whole.app_slice_words + (h - 1) * whole.app_row_words + aw, span_words * 4, sw * h * s * 4, and max_range_ part sizing (* 4 + slack) used unchecked 64-bit arithmetic.
    • make_pipeline() and scratch(...) ran before checking max_range_, so an oversized row or max_range_ <= 256 left an unused scratch buffer allocation in current_.chunks.
    • Caller-provided whole.scratch did not validate scratch_offset % 4 == 0 or overflow of whole.scratch_offset + total_scratch_bytes.
  2. scratch():
    • (chunk.used + alignment - 1) / alignment * alignment wrapped uint64_t near UINT64_MAX, and start + bytes <= chunk.size wrapped when bytes was near UINT64_MAX.
  3. make_part():
    • window + range and part.app_offset - window + span were unchecked.
  4. run():
    • (job.width + 7) / 8 and (job.height + 7) / 8 wrapped uint32_t near UINT32_MAX.

This adds checked_add / checked_mul guards and O(1) overflow-free part sizing in prepare(), pre-validates everything before pipeline creation or scratch allocation, uses overflow-free alignment padding in scratch(), guards make_part() range checks, promotes job.width/job.height in run(), and adds the CPU-only mock-Vulkan test runtime/test/depth24_bounds_test.cpp (depth24_bounds).

Tested

  • Rebuilt depth24_bounds_test and depth24_convert_test with MSVC 19.50 / Ninja (0 warnings under /W4)
  • ctest --test-dir runtime/build --output-on-failure -R "^(depth24_bounds|depth24_convert)$" (VK_LOADER_LAYERS_DISABLE=~implicit~) -> 2/2 Passed (depth24_convert 5.00 s on RTX 3060 Ti, depth24_bounds 0.01 s CPU-only)
  • python -I scripts/check_no_game_data.py runtime/src/vulkan_depth24.h runtime/test/depth24_bounds_test.cpp runtime/CMakeLists.txt -> exit 0

GPUs tested: NVIDIA GeForce RTX 3060 Ti (depth24_convert 1/1 Passed; depth24_bounds CPU-only)

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:5467584715 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/524). Originally posted by [xroneyaa](https://github.com/xroneyaa) on 2026-11-24T06:55:50Z. --- Claimed in the coordination issue as: DEPTH24-SCRATCH-BOUNDS-OVERFLOW ### What and why `Depth24Converter` in `runtime/src/vulkan_depth24.h` had several integer-overflow and pre-validation gaps: 1. `prepare()`: - `whole.app_offset > whole.app_size` was not checked before `whole.app_offset + span_words * 4 > whole.app_size`, so an offset near `UINT64_MAX` could wrap past `0` and pass bounds validation. - `(s - 1) * whole.app_slice_words + (h - 1) * whole.app_row_words + aw`, `span_words * 4`, `sw * h * s * 4`, and `max_range_` part sizing (`* 4 + slack`) used unchecked 64-bit arithmetic. - `make_pipeline()` and `scratch(...)` ran before checking `max_range_`, so an oversized row or `max_range_ <= 256` left an unused scratch buffer allocation in `current_.chunks`. - Caller-provided `whole.scratch` did not validate `scratch_offset % 4 == 0` or overflow of `whole.scratch_offset + total_scratch_bytes`. 2. `scratch()`: - `(chunk.used + alignment - 1) / alignment * alignment` wrapped `uint64_t` near `UINT64_MAX`, and `start + bytes <= chunk.size` wrapped when `bytes` was near `UINT64_MAX`. 3. `make_part()`: - `window + range` and `part.app_offset - window + span` were unchecked. 4. `run()`: - `(job.width + 7) / 8` and `(job.height + 7) / 8` wrapped `uint32_t` near `UINT32_MAX`. This adds `checked_add` / `checked_mul` guards and O(1) overflow-free part sizing in `prepare()`, pre-validates everything before pipeline creation or scratch allocation, uses overflow-free alignment padding in `scratch()`, guards `make_part()` range checks, promotes `job.width`/`job.height` in `run()`, and adds the CPU-only mock-Vulkan test `runtime/test/depth24_bounds_test.cpp` (`depth24_bounds`). ### Tested - Rebuilt `depth24_bounds_test` and `depth24_convert_test` with MSVC 19.50 / Ninja (0 warnings under `/W4`) - `ctest --test-dir runtime/build --output-on-failure -R "^(depth24_bounds|depth24_convert)$"` (`VK_LOADER_LAYERS_DISABLE=~implicit~`) -> 2/2 Passed (`depth24_convert` 5.00 s on RTX 3060 Ti, `depth24_bounds` 0.01 s CPU-only) - `python -I scripts/check_no_game_data.py runtime/src/vulkan_depth24.h runtime/test/depth24_bounds_test.cpp runtime/CMakeLists.txt` -> exit 0 GPUs tested: NVIDIA GeForce RTX 3060 Ti (`depth24_convert` 1/1 Passed; `depth24_bounds` CPU-only) 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).
Sangonapou commented 2026-11-24 19:07:38 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Sangonapou on 2026-11-24T19:07:38Z.


Tested 00523443 on NVIDIA GeForce RTX 3080 Ti Laptop (Ampere), NVIDIA 621.24, Vulkan 1.4.357, Windows 11 Pro (build 26300), native Windows: pass

Built at this head with scripts\build_runtime.ps1 (MSVC 19.51, VS Build Tools 2026, Windows SDK 10.0.26100, Vulkan SDK 1.4.363.0). No game files.

ctest -R ^depth24_bounds$ -V: passed, depth24_bounds: all checks passed.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7141639433 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/524#issuecomment-7141639433). Originally posted by [Sangonapou](https://github.com/Sangonapou) on 2026-11-24T19:07:38Z. --- Tested 00523443 on NVIDIA GeForce RTX 3080 Ti Laptop (Ampere), NVIDIA 621.24, Vulkan 1.4.357, Windows 11 Pro (build 26300), native Windows: pass Built at this head with `scripts\build_runtime.ps1` (MSVC 19.51, VS Build Tools 2026, Windows SDK 10.0.26100, Vulkan SDK 1.4.363.0). No game files. `ctest -R ^depth24_bounds$ -V`: passed, `depth24_bounds: all checks passed`.

Pull request closed

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!524
No description provided.