# How a decompiler recovers SkipLocalsInit


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

<figure class="my-8"><svg viewBox="0 0 760 340" role="img" aria-label="SkipLocalsInit erased at compile time and reconstructed from the method header bit"><rect x="0" y="0" width="760" height="340" fill="none"/><text x="20" y="28" fill="#6366f1" font-size="15" font-weight="bold">Source</text><rect x="20" y="40" width="200" height="70" rx="6" fill="none" stroke="#6366f1" stroke-width="1.5"/><text x="34" y="66" fill="#64748b" font-size="12">[SkipLocalsInit]</text><text x="34" y="86" fill="#64748b" font-size="12">static int Sum(...)</text><text x="34" y="102" fill="#64748b" font-size="11">stackalloc int[64]</text><text x="260" y="80" fill="#64748b" font-size="22">&#8594;</text><text x="300" y="28" fill="#6366f1" font-size="15" font-weight="bold">Compiler</text><rect x="300" y="40" width="180" height="70" rx="6" fill="#6366f1" stroke="#6366f1" stroke-width="1.5"/><text x="314" y="66" fill="#ffffff" font-size="12">attribute consumed,</text><text x="314" y="86" fill="#ffffff" font-size="12">not emitted</text><text x="314" y="102" fill="#ffffff" font-size="11">clears header bit</text><text x="520" y="80" fill="#64748b" font-size="22">&#8594;</text><text x="560" y="28" fill="#6366f1" font-size="15" font-weight="bold">Method header word</text><rect x="560" y="40" width="180" height="70" rx="6" fill="none" stroke="#0891b2" stroke-width="1.5"/><text x="574" y="66" fill="#0891b2" font-size="13" font-family="monospace">0x3003</text><text x="574" y="86" fill="#64748b" font-size="11">InitLocals (0x10)</text><text x="574" y="102" fill="#10b981" font-size="11">= 0 (cleared)</text><text x="20" y="160" fill="#6366f1" font-size="15" font-weight="bold">The one bit</text><rect x="20" y="172" width="720" height="54" rx="6" fill="none" stroke="#64748b" stroke-width="1"/><text x="34" y="196" fill="#10b981" font-size="12" font-family="monospace">0x3013  &#8594;  .locals init (...)   CLR zeroes the frame</text><text x="34" y="216" fill="#0891b2" font-size="12" font-family="monospace">0x3003  &#8594;  .locals (...)        frame left as-is  (bit 0x10 off)</text><text x="20" y="262" fill="#6366f1" font-size="15" font-weight="bold">Glass reconstructs</text><rect x="20" y="274" width="720" height="50" rx="6" fill="none" stroke="#6366f1" stroke-width="1.5"/><text x="34" y="298" fill="#64748b" font-size="12" font-family="monospace">if ((flags &amp; InitLocals) == 0 &amp;&amp; LocalVarSigTok != 0)</text><text x="34" y="316" fill="#64748b" font-size="12" font-family="monospace">    emit [SkipLocalsInit] on the method</text></svg><figcaption class="text-sm text-muted mt-2">The attribute never reaches the assembly; a single header bit is its whole trace, and the decompiler reconstructs the attribute from it.</figcaption></figure>

## A concrete method

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

```csharp
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:

```csharp
[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:

```bash
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](https://delta1labs.com/products/glass) and the [docs](https://delta1labs.com/docs/glass/).

