Skip to content
← All posts
· Delta1 Labs GlassDecompilationIL

How a decompiler recovers SkipLocalsInit

SkipLocalsInit leaves no custom attribute in the assembly — the C# compiler consumes it and clears a single bit in the IL method header. Here is how Glass reads that bit back and reconstructs the attribute, and why the attribute moves to the method level on the way.

A method you are decompiling uses stackalloc int[256], loops over it, and never clears it first — yet the code is clearly correct in production. You check the IL: the locals are not zero-initialized. Somewhere, someone told the compiler to skip that. But there is no attribute anywhere in the source Glass reconstructed to say so. Where did it come from?

The real question

[SkipLocalsInit] is one of a small set of C# attributes the compiler consumes rather than emits. You write it, Roslyn reads it, and then it is gone — it never reaches the output assembly as metadata. What it leaves behind is a single bit cleared in each affected method’s IL header. So the question a decompiler must answer is not “which attributes are on this method” — that list is empty — but “does this method header zero its locals, and if not, what attribute would reproduce that?”

What the CLR normally does

By default, the CLR zero-initializes every local variable and every byte of a stackalloc buffer before your code runs. That guarantee is not free: for a method with a large stack frame or a big stackalloc, it is a memset on entry. C# requests it by setting the CorILMethod_InitLocals flag (0x0010) in the method body header, which ILAsm spells as the init keyword on the locals directive:

.locals init (valuetype [System.Runtime]System.Span`1<int32> V_0,
              int32 V_1,
              int32 V_2)

[SkipLocalsInit] tells Roslyn to clear that flag. The locals directive loses its init:

.locals (valuetype [System.Runtime]System.Span`1<int32> V_0,
         int32 V_1,
         int32 V_2)

That is the entire footprint of the feature. No SkipLocalsInitAttribute row in the custom-attribute table, no marker method, nothing — just the absence of one bit.

The method header, byte by byte

A method body with locals always uses the fat header format (the tiny format has no slot for a local-variable signature token). The fat header is 12 bytes, and its first two bytes are a little-endian word that packs the format, a few flags, and the header size in dwords:

  • CorILMethod_FatFormat = 0x0003
  • CorILMethod_MoreSects = 0x0008 (the method has extra sections, e.g. exception handlers)
  • CorILMethod_InitLocals = 0x0010
  • the header size (3 dwords) sits in the high nibble: 3 << 12 = 0x3000

A standard fat method that initializes its locals therefore opens with 0x3013 — bytes 13 30 on disk. Turn off locals-init and the same method opens with 0x3003 — bytes 03 30. One flipped bit, 0x10, is all that distinguishes “the CLR zeroes my frame” from “it does not.”

Source[SkipLocalsInit]static int Sum(...)stackalloc int[64]→Compilerattribute consumed,not emittedclears header bit→Method header word0x3003InitLocals (0x10)= 0 (cleared)The one bit0x3013 → .locals init (...) CLR zeroes the frame0x3003 → .locals (...) frame left as-is (bit 0x10 off)Glass reconstructsif ((flags & InitLocals) == 0 && LocalVarSigTok != 0) emit [SkipLocalsInit] on the method
The attribute never reaches the assembly; a single header bit is its whole trace, and the decompiler reconstructs the attribute from it.

A concrete method

Compile this with <AllowUnsafeBlocks>true</AllowUnsafeBlocks> (the compiler ignores [SkipLocalsInit] in a safe-only build):

using System;
using System.Runtime.CompilerServices;

static class Hash
{
    [SkipLocalsInit]
    public static int Sum(ReadOnlySpan<byte> data)
    {
        Span<int> scratch = stackalloc int[64];
        for (int i = 0; i < 64; i++) scratch[i] = 0;   // written before read
        int n = 0;
        foreach (byte b in data) scratch[n++ & 63] += b;
        int total = 0;
        for (int i = 0; i < 64; i++) total += scratch[i];
        return total;
    }
}

The stackalloc int[64] is 256 bytes. With the default init, the CLR zeroes those 256 bytes on every call before the first instruction runs. With [SkipLocalsInit], it does not — which is why the source explicitly clears scratch in a loop. That explicit loop is the tell: in the decompiled output it looks redundant until you notice the locals are no longer zeroed for you.

Now decompile Hash without the attribute present anywhere. Glass’s IL view shows the locals directive with no init:

.method public hidebysig static int32 Sum(
        valuetype [System.Runtime]System.ReadOnlySpan`1<uint8> data) cil managed
{
  .maxstack 3
  .locals (valuetype [System.Runtime]System.Span`1<int32> V_0,
           int32 V_1,
           int32 V_2,
           uint8 V_3,
           int32 V_4)
  // ... body ...
}

and the reconstructed C# carries the attribute back, placed on the method:

[SkipLocalsInit]
public static int Sum(ReadOnlySpan<byte> data) { /* ... */ }

How Glass decides

The rule is exactly the inference in the figure. For every method Glass reads the body header; if it is the fat format, it pulls the flags word and the local-variable signature token. When the InitLocals bit is clear and the method actually has locals (LocalVarSigTok != 0), the method was compiled with locals-init suppressed, so Glass attaches [SkipLocalsInit]. A method with no locals never carries the attribute, because the bit is meaningless there and there is nothing to reconstruct.

From the command line you can go straight to it:

glass decompile Hash.dll --type Hash --member Sum
glass decompile Hash.dll --type Hash --member Sum --il   # see the .locals directive

The IL view is where the mechanism is visible: the presence or absence of init is the ground truth, and the C# view is Glass’s reconstruction of it. If you drive Glass through its MCP server, asking an assistant “does Sum zero-initialize its locals?” resolves to the same header read — useful when you are auditing performance-tuned code you did not write, or recovering your own lost source and want to know which methods were tuned this way.

When it breaks, and what you lose

The reconstruction is faithful about behaviour but not about authorship. [SkipLocalsInit] can be written once at assembly, module, or type scope, and the compiler propagates the effect to every method underneath it. By the time Glass sees the assembly, there is no record of that broad declaration — only N methods each missing the bit. So the round trip turns one module-level attribute into one attribute per method. The code still behaves identically; the source no longer reads the way the author wrote it.

Two more honest caveats. First, a method with no locals carries no signal at all, so if you wrote a type-level [SkipLocalsInit] and one of its methods happens to have no locals, that method comes back without the attribute — correctly, since the flag would do nothing there. Second, the bit tells you the frame is not zeroed, not that it is safe that it is not. Reading a local or a stackalloc slot before writing it now yields whatever was on the stack. That is the point for performance, and it is exactly the shape a security reviewer looks for when hunting uninitialized-memory disclosure, so the recovered attribute is a flag worth reading carefully.

Takeaway

[SkipLocalsInit] is the clearest example of an attribute that exists only in source. The compiler turns it into the absence of one bit in each method header, and a decompiler earns the attribute back by reading that bit, not by reading metadata. Glass surfaces both halves — the raw .locals directive in the IL view and the reconstructed attribute in C# — so you can see that what looks like a declared annotation is really an inference from the shape of the compiled method. Glass is a free .NET decompiler; see Glass and the docs.

Try Nebula.NET

Harden your .NET code in minutes — start with the free edition.