stride3d/stride · error · ArgumentException

contains invalid Effect bytecode. Could not find magic…

Error message

{nameof(buffer)} contains invalid Effect bytecode. Could not find magic header 0x{MagicHeader:X}.

What it means

EffectBytecode.FromBytesSafe parses a byte buffer into an EffectBytecode and validates the presence of the magic header. If parsing yields null (the magic header 0x... was not found at the expected position), it throws ArgumentException indicating the buffer does not contain valid Effect bytecode.

Solutions

  1. Verify you are loading a compiled EffectBytecode file, not source or another asset type.
  2. Dump the first bytes and compare against the MagicHeader to detect offset/corruption.
  3. Re-export/recompile the effect bytecode and redeploy the bundle.
  4. Use FromStream/FromBytes with null handling if you want to detect invalid data without throwing.

Example fix

// before
var bytecode = EffectBytecode.FromBytesSafe(File.ReadAllBytes(path)); // path actually holds a .sdsl source
// after
var bytes = File.ReadAllBytes(path);
if (bytes.Length < 4 || BitConverter.ToUInt32(bytes, 0) != EffectBytecode.MagicHeader)
    throw new InvalidDataException($"{path} is not compiled effect bytecode");
var bytecode = EffectBytecode.FromBytesSafe(bytes);
Defensive patterns

Strategy: validation

Validate before calling

if (buffer == null || buffer.Length < sizeof(uint) || BitConverter.ToUInt32(buffer, 0) != EffectBytecode.MagicHeader) throw new InvalidDataException("Buffer is not valid Effect bytecode");

Type guard

bool LooksLikeEffectBytecode(byte[] b) => b != null && b.Length >= sizeof(uint) && BitConverter.ToUInt32(b, 0) == EffectBytecode.MagicHeader;

Try / catch

try { bytecode = EffectBytecode.FromBytesSafe(buffer); } catch (ArgumentException ex) when (ex.Message.Contains("invalid Effect bytecode")) { /* re-fetch/recompile the bytecode asset */ }

Prevention

When it happens

Trigger: Calling EffectBytecode.FromBytesSafe on bytes that are not effect bytecode: wrong file loaded, truncated/offset data, corrupted download, or a resource whose first bytes are not the expected magic header.

Common situations: Loading effect (.efd/bytecode) files from a mis-assembled bundle; treating shader source text as compiled bytecode; byte-order/prefix mismatches after custom serialization changes.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/88d9b009065a1934. Report an issue: GitHub.

Appendix: source

Thrown at sources/shaders/Stride.Shaders.Effects/EffectBytecode.cs:109

    /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is <see langword="null"/>.</exception>
    public static EffectBytecode FromBytes(byte[] buffer)
    {
        ArgumentNullException.ThrowIfNull(buffer);

        return FromStream(new MemoryStream(buffer));
    }

    /// <summary>
    ///   Loads an <see cref="EffectBytecode"/> from a buffer.
    /// </summary>
    /// <param name="buffer">The buffer to read from.</param>
    /// <returns>The loaded Effect bytecode.</returns>
    /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is <see langword="null"/>.</exception>
    /// <exception cref="ArgumentException"><paramref name="buffer"/> contains invalid Effect bytecode.</exception>
    public static EffectBytecode FromBytesSafe(byte[] buffer)
    {
        var result = FromBytes(buffer);
        return result ?? throw new ArgumentException($"{nameof(buffer)} contains invalid Effect bytecode. Could not find magic header 0x{MagicHeader:X}.");
    }

    /// <summary>
    ///   Loads an <see cref="EffectBytecode"/> from a stream.
    /// </summary>
    /// <param name="stream">The stream to read from.</param>
    /// <returns>The loaded Effect bytecode, or <see langword="null"/> if the magic header is not matching.</returns>
    /// <exception cref="ArgumentNullException"><paramref name="stream"/> is <see langword="null"/>.</exception>
    public static EffectBytecode FromStream(Stream stream)
    {
        ArgumentNullException.ThrowIfNull(stream);

        var reader = new BinarySerializationReader(stream);
        var version = reader.Read<uint>();

        // Version is not matching, return null
        if (version != MagicHeader)
            return null;

View on GitHub (pinned to 96fad776d2)