DEV Community

Zero Heartbeat
Zero Heartbeat

Posted on Edited on Originally published at delta1labs.com

How Glass recovers fixed-size buffers

A struct with an embedded array is a common shape in interop and parsers: a header plus a fixed run of bytes, all inline, no heap allocation, no separate byte[]. In C# that is a fixed-size buffer:

public unsafe struct Packet
{
    public fixed byte Payload[16];
    public int Length;
}
Enter fullscreen mode Exit fullscreen mode

Decompile a careless build and you can get a surprise. A naive view shows a nested struct you never wrote, a field of that struct type, and no Payload[16] anywhere. The array has vanished into the metadata. The interesting question is how a decompiler puts it back.

What the compiler actually emits

fixed byte Payload[16] is not one declaration — it is three coordinated pieces of metadata. The compiler synthesizes a nested value type to carry the storage, tags the field so the storage can be recognized, and fixes the layout size so the runtime reserves exactly the right number of bytes.

The nested type looks like this, as raw decompiled output before any recognition:

public struct Packet
{
    [CompilerGenerated]
    [UnsafeValueType]
    [StructLayout(LayoutKind.Sequential, Size = 16)]
    public struct <Payload>e__FixedBuffer
    {
        public byte FixedElementField;
    }

    [FixedBuffer(typeof(byte), 16)]
    public <Payload>e__FixedBuffer Payload;

    public int Length;
}
Enter fullscreen mode Exit fullscreen mode

Three signals are now in front of us:

  • The nested type is named <Payload>e__FixedBuffer. The angle-bracket prefix is the compiler's unspeakable-name convention (you cannot write <Payload> in C#), and the e__FixedBuffer suffix is the fixed-buffer marker.
  • The type carries [StructLayout(LayoutKind.Sequential, Size = 16)]. The Size is sizeof(element) * length — here 1 * 16. It has exactly one field, FixedElementField, of the element type. That single field plus the explicit Size is what reserves the 16 inline bytes.
  • The Payload field itself carries [FixedBuffer(typeof(byte), 16)]. This is the decisive signal. Its two constructor arguments are the element type and the element count — not the byte size. For fixed int x[4] the attribute reads [FixedBuffer(typeof(int), 4)] while the layout Size is 16.

A decompiler that ignores these renders the raw nested struct — technically faithful, but not code you would ever write, and not code that round-trips. Glass recognizes the pattern and collapses all three signals back into the one line of source that produced them.

Three independent metadata facts — the synthesized nested type, the [FixedBuffer] attribute, and the single-member sequential layout — collapse into one source line.

Reading the length back

The element count can be read two ways, and Glass cross-checks them. The [FixedBuffer] attribute's second argument is the count directly — 16 for byte, 4 for int. The StructLayout.Size gives the byte count, so dividing by the element's size gives the same count. When both agree, the declaration is unambiguous; the attribute is authoritative when a hand-crafted or obfuscated assembly makes them disagree. The element type comes straight from the first attribute argument, so fixed double Samples[8] reappears with the right type even though the layout Size alone (64) could not distinguish eight doubles from sixteen ints.

The indexer is pointer arithmetic

The field declaration is only half the story. Every use of the buffer lowers to a pointer, and that lowering has to be read back too. Consider a method that fills the buffer:

public void Fill(byte value)
{
    for (int i = 0; i < 16; i++)
        Payload[i] = value;
}
Enter fullscreen mode Exit fullscreen mode

Payload[i] is not an array indexer — <Payload>e__FixedBuffer has no indexer. The compiler takes the address of the buffer's single field and adds the (scaled) index. The body of the loop compiles to:

// Payload[i] = value;
ldarg.0                                              // Packet& this
ldflda   valuetype Packet/'<Payload>e__FixedBuffer' Packet::Payload
ldflda   uint8 Packet/'<Payload>e__FixedBuffer'::FixedElementField
ldloc.0                                              // i
add
ldarg.1                                              // value
stind.i1
Enter fullscreen mode Exit fullscreen mode

ldflda Payload yields the address of the nested struct; ldflda FixedElementField yields the address of its first (and only) byte — the start of the inline run. add offsets by i. For byte the element size is 1, so there is no scaling; for fixed int x[4] you would see ldc.i4.4; mul (or a sizeof) before the add, and stind.i4 instead of stind.i1. Glass matches this shape — take the address of a FixedElementField reached through a [FixedBuffer] field, add a scaled index, dereference — and renders it back as the indexer Payload[i] = value, hiding the pointer entirely.

Accessing the buffer from outside the struct needs a fixed statement to pin the storage while a pointer is live:

Packet pkt = default;
fixed (byte* p = pkt.Payload)
    p[0] = 0xAA;
Enter fullscreen mode Exit fullscreen mode

Here the fixed local is a pinned managed pointer initialized with the same ldflda Payload / ldflda FixedElementField pair, then converted to byte*. Glass reads the pinned local and the initializer back into the fixed (byte* p = pkt.Payload) form rather than leaving a raw pinned pointer. Inside the struct's own methods, as in Fill, the containing value is already addressable through this, so no fixed is required — and Glass correctly omits it.

How to use it

Point Glass at the assembly and read the struct. The CLI prints the recovered declaration with the unsafe fixed modifier intact:

glass decompile Interop.dll --type Packet
Enter fullscreen mode Exit fullscreen mode

Switch to the IL view when you want to confirm the lowering — the ldflda FixedElementField pair and the stind.i1 are the fingerprints of a fixed-buffer access, and seeing them next to the recovered C# is the fastest way to trust the recovery. When you export the type to a buildable project, Glass emits the fixed byte Payload[16] field and marks the member unsafe, so the round-tripped source compiles with /unsafe and produces the identical layout.

When it breaks

The recognition hangs on the [FixedBuffer] attribute. A renaming obfuscator that strips or rewrites it — or renames the <Payload>e__FixedBuffer type out of its recognizable shape — can force Glass to fall back to the raw nested-struct rendering. That output is still correct (it describes the exact same bytes), it is just not the fixed-buffer sugar; you would read the Size and the single field manually to recover the intent. The other limit is semantic: a fixed buffer has no bounds check, so Payload[20] compiles and reads past the inline storage into Length. The decompiler faithfully shows the access it found; whether that access was in bounds is something only the original author knew.

Takeaway

A fixed-size buffer looks like one field and lives as three pieces of metadata plus a pointer idiom. Recovering it means recognizing the synthesized <Field>e__FixedBuffer type, trusting the [FixedBuffer] attribute for the element type and count, and reading ldflda FixedElementField + scaled-index arithmetic back into an indexer. Glass does all three, so a struct built for zero-allocation interop reads back as the inline array you would have written — the honest shape, not the compiler's scaffolding.

Top comments (0)