gfx-rs/wgpu · error

byte slice is empty, not SPIR-V

Error message

byte slice is empty, not SPIR-V

What it means

Panic from the SPIR-V input sanity check: the byte slice is empty (or fails the length/magic check), so it cannot be valid SPIR-V. This guard is load-bearing for soundness — make_spirv_const dereferences words only after it passes; the faulty input is the empty data slice.

Source

Thrown at wgpu/src/util/spirv.rs:43

pub fn make_spirv(data: &[u8]) -> ShaderSource<'_> {
    ShaderSource::SpirV(make_spirv_raw(data))
}

/// Check whether the byte slice has the SPIR-V magic number (in either byte order) and of an
/// appropriate size, and panic with a suitable message when it is not.
///
/// Returns whether the endianness is opposite of native endianness (i.e. whether
/// [`u32::swap_bytes()`] should be called.)
///
/// Note: this function’s checks are relied upon for the soundness of [`make_spirv_const()`].
/// Undefined behavior will result if it does not panic when `bytes.len()` is not a multiple of 4.
#[track_caller]
const fn assert_has_spirv_magic_number_and_length(bytes: &[u8]) -> bool {
    // First, check the magic number.
    // This way we give the best error for wrong formats.
    // (Plus a special case for the empty slice.)
    let found_magic_number: Option<bool> = match *bytes {
        [] => panic!("byte slice is empty, not SPIR-V"),
        // This would be simpler as slice::starts_with(), but that isn't a const fn yet.
        [b1, b2, b3, b4, ..] => {
            let prefix = u32::from_ne_bytes([b1, b2, b3, b4]);
            if prefix == SPIRV_MAGIC_NUMBER {
                Some(false)
            } else if prefix == const { SPIRV_MAGIC_NUMBER.swap_bytes() } {
                // needs swapping
                Some(true)
            } else {
                None
            }
        }
        _ => None, // fallthrough case = between 1 and 3 bytes
    };

    match found_magic_number {
        Some(needs_byte_swap) => {
            // Note: this assertion is relied upon for the soundness of `make_spirv_const()`.

View on GitHub (pinned to 3e11ff59bf)

Solutions

  1. Check that the SPIR-V byte slice is non-empty and its length is a multiple of 4 before calling make_spirv
  2. Verify the file was actually read (not zero-length) before constructing the shader source
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at wgpu/src/util/spirv.rs:43 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of gfx-rs/wgpu@3e11ff59bf (2026-09-03). Data as JSON: /api/errors/489f77646ac4ac36. Report an issue: GitHub.