Skip to content
← All posts
· Delta1 Labs GlassDecompilation

How Glass rebuilds multidimensional arrays

A rectangular int[,] has no dedicated IL opcodes. Element access compiles to calls on synthesized Get, Set, Address and .ctor "array methods" whose only declared home is an ARRAY TypeSpec in the blob heap. Here is how Glass.NET reads the rank-2 array signature, recognizes those runtime pseudo-methods, and turns the calls back into grid[i, j] — and how it tells a rectangular array apart from a jagged int[][].

Open a numerical-computing library in Glass.NET, step into a routine that walks a matrix, and the body is not what you expect. There is no ldelem, no stelem — the opcodes you have learned to read as “array access.” Instead every grid[i, j] is a call, and the thing being called is a method named Get or Set on a type that has no source file anywhere. That is not obfuscation. That is simply how a rectangular int[,] works, and reconstructing it cleanly is a good test of whether a decompiler actually reads the array signature or just guesses from opcode shapes.

The real question

C# gives you two things people both call “a 2D array,” and they are completely different objects. A jagged array, int[][], is an array whose elements are themselves arrays — rows that can have different lengths, each allocated on its own. A rectangular array, int[,], is a single contiguous object with a fixed shape: rows × cols, every row the same width.

The jagged form uses the ordinary array machinery. The rectangular form does not — the CLR has no opcodes for it. So the question is: when there is no newarr and no ldelem, where does a decompiler find out that this is an int[,] of rank 2, and how does it turn a pile of call Get / call Set instructions back into indexer syntax?

Where the opcodes run out

The IL array opcodes — newarr, ldelem, stelem, ldlen — are defined only for the single-dimensional, zero-based array, the shape the ECMA-335 spec calls a vector and encodes as ELEMENT_TYPE_SZARRAY. That covers int[], and because a jagged int[][] is just a vector whose elements are vectors, it covers jagged arrays too.

A rectangular int[,] is encoded differently — as ELEMENT_TYPE_ARRAY (0x14) carrying a rank and, optionally, explicit sizes and lower bounds. There is no opcode that takes a rank. So for every rank-2-or-higher array, and for the oddball single-dimensional non-zero-based int[*], the runtime synthesizes four methods on the array type itself:

  • .ctor(int32, int32) — allocate, one length per dimension.
  • Get(int32, int32) — read arr[i, j].
  • Set(int32, int32, int32) — write arr[i, j] = v (indices first, then the value).
  • Address(int32, int32) — produce a managed reference ref arr[i, j].

These methods have no IL body, no RVA, no row in the MethodDef table. The JIT provides them intrinsically. In metadata they appear only as MemberRef rows whose parent is a TypeSpec — the array type, spelled out in the blob heap.

The IL, read closely

Here is the C# that a matrix routine starts from:

static int DiagonalSum(int rows, int cols)
{
    int[,] grid = new int[rows, cols];
    for (int i = 0; i < rows; i++)
        grid[i, i % cols] = i;
    return grid[0, 0];
}

The allocation and the write compile like this:

  // int[,] grid = new int[rows, cols];
  IL_0000: ldarg.0
  IL_0001: ldarg.1
  IL_0002: newobj   instance void int32[0...,0...]::.ctor(int32, int32)
  IL_0007: stloc.0
  // grid[i, i % cols] = i;
  IL_0012: ldloc.0
  IL_0013: ldloc.1
  IL_0014: ldloc.1
  IL_0015: ldarg.1
  IL_0016: rem
  IL_0017: ldloc.1
  IL_0018: call     instance void int32[0...,0...]::Set(int32, int32, int32)
  // return grid[0, 0];
  IL_0024: ldloc.0
  IL_0025: ldc.i4.0
  IL_0026: ldc.i4.0
  IL_0027: call     instance int32 int32[0...,0...]::Get(int32, int32)

The giveaway is the type name ILDASM prints: int32[0...,0...]. That [0...,0...] is the decompiled form of the ARRAY signature — two dimensions, each with lower bound 0 and no fixed upper size. A vector would have printed int32[] and used newarr / stelem.i4 / ldelem.i4 instead of these calls.

Notice the argument order on Set: the indices come first, then the value last — the reverse of how you read grid[i, j] = v left to right. A decompiler that matched these as ordinary calls would print Set(i, j, v) and leave you to work out what it meant. Recognizing the pseudo-method is what lets Glass put the value back on the correct side of the =.

What Glass actually does

Glass resolves the call token to its MemberRef, sees that the parent is a TypeSpec, and parses that TypeSpec’s signature from the blob heap. The signature answers every question: it is ELEMENT_TYPE_ARRAY, element type I4, rank 2. From rank and the method name, the reconstruction is mechanical:

  • .ctor with rank length arguments → new int[a, b].
  • Get with rank index arguments → grid[i, j] in an expression.
  • Set with rank + 1 arguments → grid[i, j] = value, peeling the last argument off as the right-hand side.
  • Address → a ref grid[i, j], the form you see behind ref locals and in/ref argument passing.
Rectangular int[,] — no array opcodecall int32[0...,0...]::Set(int32, int32, int32)MemberRef → TypeSpecparent is an array typeARRAY sig in blob heap0x14 I4 rank=2 bounds 0,0rank=2, name Setlast arg is the valuegrid[i, j] = value;Jagged int[][] — the SZARRAY pathnewarr + ldelem.refrow, then ldelem.i4two SZARRAY layersarray of arraysgrid[i][j]The element-type signature — not the opcode — decides which shape, and which syntax, Glass emits.
A rectangular array indexes through a synthesized Set on an ARRAY TypeSpec; Glass reads the rank from the blob and rebuilds grid[i, j] = value. A jagged array takes the ordinary SZARRAY opcodes and becomes grid[i][j].

Rectangular versus jagged, side by side

The distinction matters because the two look almost identical in loose English and completely different in IL. A jagged int[][] built as new int[rows][] emits one newarr for the outer vector, then a newarr per row, and reads with ldelem.ref (load the row) followed by ldelem.i4 (load the element). Two opcodes, two allocations, grid[i][j]. A rectangular int[,] emits a single .ctor call and reads with one Get, grid[i, j]. Glass reads the element-type signature, not the surface syntax, so it never prints [i][j] where the metadata says rank 2, or [i, j] where it says array-of-array.

Doing it from the CLI

You can see both halves without the UI. Dump the method in IL to watch the Get/Set calls, then in C# to see the recovered indexer:

$ glass export ./MatrixOps.dll --member "M:MatrixOps.DiagonalSum(System.Int32,System.Int32)" --il
$ glass export ./MatrixOps.dll --member "M:MatrixOps.DiagonalSum(System.Int32,System.Int32)"

The first keeps newobj ...::.ctor and the Get/Set calls on the int32[0...,0...] type intact; the second shows new int[rows, cols] and grid[i, i % cols] = i. Over the MCP server the same recovery is available to an agent as a decompile call, which is handy when you are asking a tool to explain an unfamiliar numerical routine rather than reading it yourself.

Caveats and where it gets hard

A few honest edges. First, the Address method hands back a managed pointer, and whether the surrounding C# ends up as a plain ref local, an in argument, or just an inlined element read depends on how the original code used it — Glass recovers the access but the exact ref-ness can be ambiguous when the pointer is consumed immediately. Second, arrays with a non-zero lower bound (the rare Array.CreateInstance with explicit bounds, printed int32[1...,1...]) have no C# indexer syntax at all — C# always assumes zero-based — so Glass shows the Get/Set calls more literally rather than invent a grid[i, j] that would silently shift the indices. Third, a constant-filled rectangular array does not get the RuntimeHelpers.InitializeArray treatment that a single-dimensional constant array does; the element-by-element Set calls are what you will see, and the recovered initializer is reconstructed from them.

Takeaway

A rectangular int[,] is the clearest case in C# where the opcode is not the story. There is no array instruction for rank-2 access; the compiler emits ordinary calls to .ctor, Get, Set and Address — methods with no body, declared only as MemberRefs on an ARRAY TypeSpec in the blob heap. Glass.NET reads that signature for the rank and element type, recognizes the four pseudo-methods, puts the value back on the right side of a Set, and hands you grid[i, j] — while a jagged int[][] takes the separate newarr/ldelem path and comes back as grid[i][j]. Getting that distinction right is a small thing that a decompiler either does from the metadata or fakes from the opcodes; Glass does it from the metadata. Grab the free build from the Glass.NET page and point it at the next matrix routine you need to read.

Try Nebula.NET

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