DEV Community

Zero Heartbeat
Zero Heartbeat

Posted on Originally published at delta1labs.com

Decoding custom attribute blobs: from #Blob bytes back to [Cache(30, ...)]

Most C# constructs lower into IL instructions. Attributes do not: an attribute application compiles into data, a packed binary record stored in the #Blob heap and pointed at by a row in the CustomAttribute metadata table. There is no code to read back, only bytes to decode — and the decode is driven by a signature that lives somewhere else entirely. Getting a named argument's field-versus-property tag wrong, or misreading an enum's embedded type name, produces an attribute that looks plausible and binds to the wrong member. This post walks the exact byte layout and shows what Glass.NET reconstructs.

The source

A single attribute, carrying two positional arguments and two named ones — one property, one field — is enough to exercise every part of the format:

using System;

[AttributeUsage(AttributeTargets.Class)]
sealed class CacheAttribute : Attribute
{
    public CacheAttribute(int seconds, string region)
    {
        Seconds = seconds;
        Region = region;
    }

    public int Seconds { get; }
    public string Region { get; }

    public bool SlidingExpiration { get; set; }  // property
    public int Priority;                           // field
}

[Cache(30, "catalog", SlidingExpiration = true, Priority = 5)]
sealed class ProductService { }
Enter fullscreen mode Exit fullscreen mode

The application on ProductService says: call the (int, string) constructor with 30 and "catalog", then set the SlidingExpiration property to true and the Priority field to 5. None of those member names or values is stored as text in source form — they are serialized into a blob whose shape is fixed by ECMA-335 §II.23.3.

What the metadata really holds

ildasm and the Glass IL view both render the application as a constructor reference followed by the raw blob:

.custom instance void CacheAttribute::.ctor(int32, string) = (
    01 00 1E 00 00 00 07 63 61 74 61 6C 6F 67 02 00
    54 02 11 53 6C 69 64 69 6E 67 45 78 70 69 72 61
    74 69 6F 6E 01 53 08 08 50 72 69 6F 72 69 74 79
    05 00 00 00 )
Enter fullscreen mode Exit fullscreen mode

Fifty-two bytes. Every field of the attribute is in there, in a strict order, with no delimiters and no names for the positional arguments. Here is the same blob, sliced:

Bytes Meaning
01 00 Prolog — always 0x0001, little-endian
1E 00 00 00 Fixed arg 1: int32 = 30
07 + 63 61 74 61 6C 6F 67 Fixed arg 2: SerString, length 7, UTF-8 "catalog"
02 00 NumNamed = 2 named arguments follow
54 Named arg 1 tag: PROPERTY
02 Element type BOOLEAN
11 + 53 6C 69 64 … Name SerString, length 17, "SlidingExpiration"
01 Value: true
53 Named arg 2 tag: FIELD
08 Element type I4 (int32)
08 + 50 72 69 6F … Name SerString, length 8, "Priority"
05 00 00 00 Value: 5

The prolog and the fixed arguments

The first two bytes are a version prolog, always 0x0001. After it come the fixed arguments — the constructor's positional parameters — serialized back to back in declaration order, with no type tags and no names. The blob stores 1E 00 00 00 and 07 catalog; it does not record that the first is seconds or the second region. The only way to know that the four bytes 1E 00 00 00 are an int32 and the next group is a string is to read the constructor signature from the MethodDef/MemberRef the CustomAttribute row points at. Fixed arguments are decoded by external signature; the blob alone is ambiguous.

Strings use the SerString encoding: a compressed unsigned length (here a single byte 0x07) followed by that many UTF-8 bytes. A compressed length is one, two, or four bytes depending on magnitude — the same scheme used throughout the metadata heaps — and a null string is the single byte 0xFF, distinct from an empty string's 0x00.

The named arguments are self-describing

After the fixed arguments comes a 2-byte count — 02 00 here — and then each named argument, which unlike the fixed ones carries its own type information. Each begins with a tag byte that is the piece of information C# syntax throws away:

  • 0x53 = FIELD
  • 0x54 = PROPERTY

In source, SlidingExpiration = true and Priority = 5 look identical — both are Name = value. In the blob they are distinguished at byte offset zero of each named argument. The tag is followed by an element-type byte (0x02 for BOOLEAN, 0x08 for I4, 0x0E for STRING, …), a SerString for the member name, and the value serialized according to that element type.


Byte layout of a custom attribute blob: prolog, fixed arguments decoded by constructor signature, a named-argument count, and self-describing named arguments

Prolog
01 00

Fixed args — no tags, no names
1E 00 00 00 · 07 "catalog"

NumNamed
02 00
decoded against the (int32, string) constructor signature

Named arg 1 — self-describing
54 PROPERTY tag
02 BOOLEAN element type
11 "SlidingExpiration"
01 value = true

Named arg 2 — self-describing
53 FIELD tag
08 I4 element type
08 "Priority"
05 00 00 00 value = 5


The asymmetry is the whole story: fixed arguments lean on an external signature, named arguments describe themselves. A decoder has to switch strategies halfway through the blob.

The awkward cases: enums, Type, and arrays

Three argument kinds complicate the "element type byte then value" rule, and they are where naive decoders drift.

Enums. An enum argument does not have a primitive element-type byte. It is encoded as 0x55 followed by a SerString naming the enum type, then the value serialized as the enum's underlying integer type. Suppose CacheAttribute also accepted a CachePriority enum:

[Cache(30, "catalog", Level = CachePriority.High)]
Enter fullscreen mode Exit fullscreen mode

The named argument's bytes would be 54 (property) 55 (enum) then "...CachePriority" as a SerString, then 02 00 00 00 for the underlying int value 2. The blob stores the number, not the member name High. To render CachePriority.High, Glass resolves the enum type by its embedded name and maps 2 back to the member — or emits (CachePriority)2 when no single member matches a flag combination.

System.Type. A typeof(...) argument is serialized as element type 0x50 followed by a SerString of the type's name (assembly-qualified when it lives elsewhere). Glass parses that name back into a typeof(T) expression rather than leaving a string literal.

Arrays. An array argument is SZARRAY (0x1D), the element type, a 4-byte element count, then the elements packed in sequence (0xFFFFFFFF marks a null array, distinct from a zero-length one). Each element is serialized by the element type — so a string[] is a count followed by that many SerStrings.

What Glass reconstructs

Glass reads the CustomAttribute row, resolves the constructor to recover the fixed-argument types, decodes the blob in two passes (signature-driven for fixed args, self-describing for named args), honours each FIELD/PROPERTY tag to bind the right member, and resolves enum and Type names. From the 52 bytes above it produces exactly the original application:

[Cache(30, "catalog", SlidingExpiration = true, Priority = 5)]
sealed class ProductService { }
Enter fullscreen mode Exit fullscreen mode

A decoder that skipped the named-argument tag byte, or that treated the enum's embedded type string as a positional value, would emit something that compiles but names the wrong member or the wrong constant — the same class of silent, on-the-page-invisible error as dropping an overflow suffix.

Reading it yourself

Open any assembly in Glass, select a decorated type or method, and switch to the IL view to see the raw .custom blob next to the reconstructed C#. Line up the prolog 01 00, count the fixed arguments against the constructor signature, find the 02 00 named-argument count, and watch the 53/54 tags pick field versus property. For a quick experiment, add an enum-typed property to an attribute and recompile — you will see the 0x55 element type and the embedded type name appear in the blob, with the numeric value stored where the member name never is.

Glass.NET is a free .NET decompiler for inspecting assemblies you have the right to inspect — your own builds, your dependencies, and third-party binaries you are licensed to analyse. Decoding a custom attribute blob faithfully, down to the field-versus-property tag, is the difference between reading metadata and guessing at it.

Top comments (0)