Recompiler: ShaderCache::bytes integer underflow / usize::MAX wraparound when evicting split compute (dispatch_chain) entries #2454

Open
opened 2026-11-26 05:31:20 +00:00 by github-bot · 2 comments
Owner

@⁠microwave805 on GitHub · 2026-11-25 19:30 UTC · original

Static audit on main @ c7e1799 found an integer underflow / usize::MAX wraparound in ShaderCache::evict_one() whenever a split compute (dispatch_chain) shader is evicted. Part of #1269.

Where

  • crates/xcl/src/gpu/recomp/pso.rs:731-739 (ShaderEntry::bytes)
  • crates/xcl/src/gpu/recomp/pso.rs:770-775 (ShaderCache::evict_one)
  • crates/xcl/src/gpu/recomp/pso.rs:813-840 (ShaderCache::insert)

Root cause

ShaderEntry::bytes() (pso.rs:736) accounts for dispatch_chain piece capacities:

+ self.shader.dispatch_chain.as_ref().map_or(0, |chain| chain.pieces.iter().map(Vec::capacity).sum())

However, ShaderCache::insert() (pso.rs:813-816, 839) omits dispatch_chain pieces when computing bytes for self.limit and self.bytes += bytes:

let bytes = blob.len()
    + key.cache_dir.capacity()
    + shader.dxil.capacity()
    + shader.samplers.capacity() * std::mem::size_of::<StaticSampler>();
...
self.bytes += bytes;

When that entry is later evicted at pso.rs:774 (self.bytes -= old.bytes();), old.bytes() exceeds the amount added on insert by the sum of chain.pieces capacities:

  • Debug/test builds: panics with subtract-with-underflow and permanently poisons the global shader_cache() Mutex.
  • Release builds: self.bytes wraps to ~usize::MAX, making while self.bytes + bytes > self.limit (pso.rs:820) evict every entry on every subsequent insert (and risking addition wraparound).

Trigger

Insert any split direct-DXIL compute shader (shader.dispatch_chain.is_some() with non-empty pieces) into ShaderCache, then insert additional entries past the byte or entry limit so the split entry is evicted.

Fix direction

Compute entry size through one shared helper (or construct ShaderEntry first and use entry.bytes() in both insert() and evict_one()), and add a unit test in pso.rs that inserts a Compiled with a non-empty dispatch_chain and triggers eviction.

AI use

  • Antigravity (Gemini 4 Neon): static code audit on main @ c7e1799 and issue drafting.
**@⁠microwave805** on GitHub · 2026-11-25 19:30 UTC · [original](https://github.com/xcl-dev/xcl/issues/2454) Static audit on `main @ c7e1799` found an integer underflow / `usize::MAX` wraparound in `ShaderCache::evict_one()` whenever a split compute (`dispatch_chain`) shader is evicted. Part of #1269. ### Where - `crates/xcl/src/gpu/recomp/pso.rs:731-739` (`ShaderEntry::bytes`) - `crates/xcl/src/gpu/recomp/pso.rs:770-775` (`ShaderCache::evict_one`) - `crates/xcl/src/gpu/recomp/pso.rs:813-840` (`ShaderCache::insert`) ### Root cause `ShaderEntry::bytes()` (`pso.rs:736`) accounts for `dispatch_chain` piece capacities: ```rust + self.shader.dispatch_chain.as_ref().map_or(0, |chain| chain.pieces.iter().map(Vec::capacity).sum()) ``` However, `ShaderCache::insert()` (`pso.rs:813-816, 839`) omits `dispatch_chain` pieces when computing `bytes` for `self.limit` and `self.bytes += bytes`: ```rust let bytes = blob.len() + key.cache_dir.capacity() + shader.dxil.capacity() + shader.samplers.capacity() * std::mem::size_of::<StaticSampler>(); ... self.bytes += bytes; ``` When that entry is later evicted at `pso.rs:774` (`self.bytes -= old.bytes();`), `old.bytes()` exceeds the amount added on insert by the sum of `chain.pieces` capacities: - **Debug/test builds:** panics with subtract-with-underflow and permanently poisons the global `shader_cache()` `Mutex`. - **Release builds:** `self.bytes` wraps to `~usize::MAX`, making `while self.bytes + bytes > self.limit` (`pso.rs:820`) evict every entry on every subsequent insert (and risking addition wraparound). ### Trigger Insert any split direct-DXIL compute shader (`shader.dispatch_chain.is_some()` with non-empty `pieces`) into `ShaderCache`, then insert additional entries past the byte or entry limit so the split entry is evicted. ### Fix direction Compute entry size through one shared helper (or construct `ShaderEntry` first and use `entry.bytes()` in both `insert()` and `evict_one()`), and add a unit test in `pso.rs` that inserts a `Compiled` with a non-empty `dispatch_chain` and triggers eviction. ### AI use - Antigravity (Gemini 4 Neon): static code audit on `main @ c7e1799` and issue drafting.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:31:20 +00:00
Author
Owner

@⁠Creaa on GitHub · 2026-11-25 19:33 UTC · original

Taking this. I will use the same size calculation for insertion and eviction, and add a unit test for the cache budget with a split compute shader. AI use: OpenAI Codex (GPT-6).

**@⁠Creaa** on GitHub · 2026-11-25 19:33 UTC · [original](https://github.com/xcl-dev/xcl/issues/2454#issuecomment-7172455801) Taking this. I will use the same size calculation for insertion and eviction, and add a unit test for the cache budget with a split compute shader. AI use: OpenAI Codex (GPT-6).
Author
Owner

@⁠ettehn on GitHub · 2026-11-26 12:27 UTC · original

Confirmed the insertion-driven eviction on Windows 11 (Rust 1.97.1 MSVC, debug) with one synthetic test and no GPU. With a split entry already cached, insert a plain entry sized to fill the whole byte budget. This exercises the eviction from inside ShaderCache::insert.

  • On #2475's parent 06055a7, the second insertion reaches evict_one and panics at pso.rs:785 with attempt to subtract with overflow.
  • On #2475 at 398cefb, the split entry is evicted, the replacement remains readable, and cache.bytes equals the budget. The same test passes.

This verifies the debug panic and the fix at the insertion boundary. Release overflow behavior and retail runs were outside this check.

AI-assisted test and verification (OpenAI Codex).

**@⁠ettehn** on GitHub · 2026-11-26 12:27 UTC · [original](https://github.com/xcl-dev/xcl/issues/2454#issuecomment-7192025077) Confirmed the insertion-driven eviction on Windows 11 (Rust 1.97.1 MSVC, debug) with one synthetic test and no GPU. With a split entry already cached, insert a plain entry sized to fill the whole byte budget. This exercises the eviction from inside `ShaderCache::insert`. - On #2475's parent `06055a7`, the second insertion reaches `evict_one` and panics at `pso.rs:785` with `attempt to subtract with overflow`. - On #2475 at `398cefb`, the split entry is evicted, the replacement remains readable, and `cache.bytes` equals the budget. The same test passes. This verifies the debug panic and the fix at the insertion boundary. Release overflow behavior and retail runs were outside this check. AI-assisted test and verification (OpenAI Codex).
This discussion has been locked. Commenting is limited to contributors.
No description provided.