Skip to content

fix(resource_tiling): guard integer overflows, shape divide-by-zero, and mip shift UB - #1980

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
totonicpu:pr/fix-resource-tiling-overflow
Nov 22, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
totonicpu:pr/fix-resource-tiling-overflow

Conversation

@totonicpu

Copy link
Copy Markdown
Contributor

Summary

Fixes three arithmetic bugs in resource_tiling() (runtime/src/resource_tiling.h):

  1. 64-bit tile/byte count overflow bypassing the > 0xffffffffu guard: standard_tiles += tw * th * td, packed_bytes += ..., and per_slice * layers were computed in unchecked uint64_t arithmetic, allowing large dimensions to wrap modulo 2^64 (e.g., 2^25 * 2^25 * 2^14 == 2^64 == 0) and return true with total_tiles == 0. Also replaced (value + by - 1) / by with overflow-safe value ? 1 + (value - 1) / by : 0 and ensured out stays clean on failure.
  2. 32-bit shape overflow leading to integer divide-by-zero: elements.width * desc.block and elements.height * desc.block multiplied in uint32_t, overflowing to 0 for large desc.block (e.g. 1u << 26 with 16-byte elements) and crashing in up(w, out.shape.width).
  3. Shift undefined behavior for mip >= 32: desc.height >> mip and depth >> mip shifted uint32_t by >= 32 (clamped to 1u for mip >= 32, and w clamped to 1 for mip >= 64).

Regression Test (Fail-First)

Added regression cases in runtime/test/resource_tiling_test.cpp:

  • Before fix (Debug): failed on {TiledKind::texture2d, 1ULL << 31, 1u << 31, 16384, 1, 16, 1} (line 201, EXIT=1; and {..., block = 1u << 26} divides by zero).
  • After fix (Debug + RelWithDebInfo): resource_tiling and d3d12_vulkan_device (RTX 4070 Ti + LEONIDA_VK_VALIDATION=1) pass 2/2 in both configs with zero warnings and zero Vulkan validation errors.

@Flyander Flyander left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the changed arithmetic and bounds checks at the current head. I found no concrete correctness issue; the guards keep the division and shifts defined, and the added regression cases cover the affected input classes. CI is still pending.

@kapitche

Copy link
Copy Markdown
Contributor

Tested on Windows 11 with two GPUs on one machine (runtime built from this PR's head, MSVC 14.51): no regression from this PR. RTX 4080 SUPER: 111/113 pass. On current main three tests fail the same way on every PR I built, so they're not from this PR: d3d12_draw (bundle blend, bisected to #1660's merge, see #1902), d3d12_compute on both GPUs (line 515: device_proxy_ref_count(device.p) == 1), and d3d12_subgroup on the Intel UHD 770 (line 611: fragment_default). I'm bisecting the last two. Other Intel failures are the known d3d12_present and d3d12_m3_frame.

@ItsNotRexed
ItsNotRexed merged commit 7e49e1b into ItsNotRexed:main Nov 22, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants