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

Open
opened 2026-11-25 19:30:11 +00:00 by microwave805 · 2 comments
microwave805 commented 2026-11-25 19:30:11 +00:00 (Migrated from github.com)
Contributor

Imported from ivall/xcl. Originally posted by microwave805 on 2026-11-25T19:30:11Z.


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.
<!-- greyfinch-sync:ivall/xcl:issue:6612104916 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/issues/2454). Originally posted by [microwave805](https://github.com/microwave805) on 2026-11-25T19:30:11Z. --- 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.
Creaa commented 2026-11-25 20:08:39 +00:00 (Migrated from github.com)
Author
Contributor

Imported from ivall/xcl. Originally posted by Creaa on 2026-11-25T19:33:13Z.


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

<!-- greyfinch-sync:ivall/xcl:comment:7172455801 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/issues/2454#issuecomment-7172455801). Originally posted by [Creaa](https://github.com/Creaa) on 2026-11-25T19:33:13Z. --- 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).
ettehn commented 2026-11-26 13:05:38 +00:00 (Migrated from github.com)
Author
Contributor

Imported from ivall/xcl. Originally posted by ettehn on 2026-11-26T12:27:26Z.


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

<!-- greyfinch-sync:ivall/xcl:comment:7192025077 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/issues/2454#issuecomment-7192025077). Originally posted by [ettehn](https://github.com/ettehn) on 2026-11-26T12:27:26Z. --- 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).
Sign in to join this conversation.
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
xcl-dev/xcl#2444
No description provided.