DEV Community

Zero Heartbeat
Zero Heartbeat

Posted on Edited on Originally published at delta1labs.com

How Glass spots a custom async method builder

Write a one-line async ValueTask<int> method, slap [AsyncMethodBuilder(typeof(PoolingAsyncValueTaskMethodBuilder))] on it, and compile. Open the result in a decompiler and you will usually see a plain async ValueTask<int> method come back — the attribute gone, the pooling erased. That is not a cosmetic loss. The whole reason the attribute was there was to change how the method allocates, and a decompiler that silently normalises it to the stock builder has told you the method does something it does not.

The real question

Every async method you write is a lie the compiler tells on your behalf. What actually ships is a generated struct and a builder — a small type that constructs the struct, starts it, parks awaiter continuations, and finally publishes a result or an exception. For a Task-returning method that builder is AsyncTaskMethodBuilder; for ValueTask<T> it is AsyncValueTaskMethodBuilder<T>. The compiler picks it from the return type.

But that choice is overridable. [AsyncMethodBuilder(typeof(...))] on the method — or baked into the return type — tells Roslyn to wire the state machine to a different builder, with the same Create/Start/AwaitOnCompleted/SetResult/SetException contract but different behaviour. So the real question for a decompiler is: when you read a state machine back, do you assume the builder from the return type, or do you read the builder the compiler actually used? Those answers diverge exactly when it matters most.

The builder is a field, and its type is the evidence

Here is the source. One method, one override:

using System.Runtime.CompilerServices;
using System.Threading.Tasks;

public static class Cache
{
    [AsyncMethodBuilder(typeof(PoolingAsyncValueTaskMethodBuilder<int>))]
    public static async ValueTask<int> LoadAsync(string key)
    {
        var bytes = await ReadAsync(key);
        return bytes.Length;
    }
}
Enter fullscreen mode Exit fullscreen mode

The attribute does not change the shape of the lowered method. The compiler still emits a [CompilerGenerated] state-machine struct, still stamps the method with [AsyncStateMachine], still produces a MoveNext. What changes is one field's type:

[CompilerGenerated]
private struct <LoadAsync>d__0 : IAsyncStateMachine
{
    public int <>1__state;
    public PoolingAsyncValueTaskMethodBuilder<int> <>t__builder;
    public string key;
    private ValueTaskAwaiter<byte[]> <>u__1;
    // ... MoveNext, SetStateMachine
}
Enter fullscreen mode Exit fullscreen mode

That <>t__builder field is the entire tell. In a stock async ValueTask<int> method the field is typed AsyncValueTaskMethodBuilder<int>. Here it is PoolingAsyncValueTaskMethodBuilder<int>. A decompiler that reads the field's declared type — rather than inferring a builder from the return type — knows immediately that a non-default builder was in play, because the metadata says so in one place that cannot be faked away without also breaking the IL.

The <>t__builder field type and the [AsyncMethodBuilder] attribute, not the return type, are what tell Glass which builder the compiler actually wired in.

What the IL confirms

Reading the field type is enough to flag the override, but the IL backs it up. The generated stub that replaced your method body calls Create on the builder type, and MoveNext calls AwaitUnsafeOnCompleted and SetResult on the same type. Those call targets are fully-qualified in the IL — they name the builder, not the return type:

// the LoadAsync stub that replaced the body
call    valuetype [System.Threading.Tasks]...PoolingAsyncValueTaskMethodBuilder`1<int32>
          ...PoolingAsyncValueTaskMethodBuilder`1<int32>::Create()
stfld   ...PoolingAsyncValueTaskMethodBuilder`1<int32> <LoadAsync>d__0::'<>t__builder'
ldloca.s state
call    instance void ...PoolingAsyncValueTaskMethodBuilder`1<int32>::Start<...>(!!0&)
Enter fullscreen mode Exit fullscreen mode
// inside MoveNext, suspending on the awaiter
ldloca.s builder
ldloca.s awaiter
ldarg.0
call    instance void ...PoolingAsyncValueTaskMethodBuilder`1<int32>
          ::AwaitUnsafeOnCompleted<...>(!!0&, !!1&)
Enter fullscreen mode Exit fullscreen mode

The builder contract is fixed — Create, Start<TSM>, SetStateMachine, AwaitOnCompleted/AwaitUnsafeOnCompleted, SetResult, SetException — which is exactly why a custom builder slots in without changing the MoveNext skeleton. Glass reads those call targets as corroboration: the method whose body is the Create/Start stub, over the struct named in [AsyncStateMachine], with a builder field typed PoolingAsyncValueTaskMethodBuilder<int>, is an async method using that builder. All three signals agree.

How Glass maps it back

The reconstruction is a fixed sequence, all static:

  1. Find the struct. Read [AsyncStateMachine(typeof(<LoadAsync>d__0))] on the method to locate the generated type. The method whose body became a builder-start stub is the original.
  2. Read the builder field's type — do not assume it. Inside the struct, the single builder field (<>t__builder) has a declared type. Glass takes that type verbatim. If it is AsyncTaskMethodBuilder, AsyncValueTaskMethodBuilder, or the void builder, it is stock; anything else is custom.
  3. Confirm the attribute. Glass looks for [AsyncMethodBuilder(typeof(...))] on the method, then on the return type, then its assembly/module-level forms. Found, it reattaches it. The return type is recovered from the builder's result type and the method signature, not guessed from the builder alone.
  4. Reconstruct the signature. Out comes [AsyncMethodBuilder(typeof(PoolingAsyncValueTaskMethodBuilder<int>))] async ValueTask<int> LoadAsync(string key) with the await and the body intact — and Glass notes in the output that a non-default builder was in use, so the pooling is visible, not erased.

From the CLI it is one command:

$ glass export ./App.dll --member "M:Cache.LoadAsync(System.String)"
Enter fullscreen mode Exit fullscreen mode

You get the reconstructed async method with the builder attribute in place. Add --il and Glass shows the honest other side: the <LoadAsync>d__0 struct, the <>t__builder field typed with the custom builder, and the Create/Start/AwaitUnsafeOnCompleted calls that name it.

Caveats and where it breaks

Three honest limits. First, if the [AsyncMethodBuilder] attribute was stripped — some obfuscators drop custom attributes they do not recognise — Glass can still read the builder field's type and tell you a non-stock builder was used, but it cannot always reproduce the exact attribute placement; it falls back to presenting the builder it found, or, if even the field type is ambiguous, the raw state machine. Second, pooled builders like PoolingAsyncValueTaskMethodBuilder reuse state-machine boxes across invocations, which complicates a naive step-through debugger — the same box services different calls — but it does not affect Glass's static reconstruction, which reads the type once and never executes it. Third, the async keyword is a C# surface form: Glass reconstructs the equivalent awaitable method and surfaces the builder, but when a builder is a third-party awaitable with unusual completion semantics, you should verify behaviour against the IL rather than assume the reconstructed C# is semantically identical in every edge case.

This is the same discipline Glass applies to async iterators and to await using over IAsyncDisposable: read the fingerprint the compiler actually emitted, not the one convention predicts.

Takeaway

A custom async method builder is invisible to any decompiler that infers the builder from the return type — and inferring it is the common shortcut. The override lives in two places the compiler cannot hide: the [AsyncMethodBuilder(typeof(...))] attribute, and the declared type of the state machine's single builder field, confirmed by the fully-qualified Create/Start/AwaitUnsafeOnCompleted call targets in the IL. Glass.NET reads all three, reconstructs the async signature with the correct return type, and surfaces that a non-default builder — pooled or otherwise — was in play, rather than normalising it into a stock-builder method that allocates differently than the one that shipped. Grab the free build from the Glass.NET download page and open the IL view on the next pooled ValueTask you need to trust.

Top comments (0)