Skip to content

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

Backlog

Description

@microwave805

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.

Activity

  1. Creaa commented on Nov 25, 2026

    @Creaa
    Contributor

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

  2. ettehn commented on Nov 26, 2026

    @ettehn
    Contributor

    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.

    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 up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions