How C# Checked Arithmetic Compiles to IL
Resumo
Pixel’s DEV Community announcement explores how C# checked and unchecked blocks are represented in IL without a direct metadata marker. It compares operations such as add.ovf and checked integer conversions, showing how Glass.NET reconstructs source blocks—a useful guide for understanding decompilation and preserving program behavior.
DEV Community
Decompiling checked arithmetic: add.ovf, conv.ovf, and rebuilding the checked block
A checked or unchecked keyword is one of the few C# constructs that leaves no direct fingerprint in metadata. There is no attribute, no flag, no scope record that says "overflow checking was on here." The information survives in a completely different form: it is smeared across the individual arithmetic instructions, each of which comes in a checking and a non-checking variant. Reconstructing the keyword means reading those instructions back and working out the smallest block of source that would have produced exactly that pattern — and getting it wrong changes what the program does. This post walks the lowering in both directions and shows what Glass.NET rebuilds. What the source looks like Overflow checking controls one thing: what happens when an integer operation produces a result too large for its type. Under unchecked (the C# default) the result wraps around silently. Under checked the runtime throws OverflowException. The keyword can scope a block or a single expression: using System;
static class Money
{
public static int Total(int a, int b)
{
checked
{
return a + b;
}
}
public static byte ToByte(int n) => checked((byte)n);
public static int Fast(int a, int b) => unchecked(a * b);
}
Three methods, three decisions. Total adds with overflow checking on. ToByte narrows an int to a byte with a range check. Fast multiplies with checking explicitly off. Nothing about these intentions is stored as such — each becomes a choice of opcode. What the compiler really emits Here are the three method bodies as Glass shows them in the IL view. Watch the suffixes. .method public hidebysig static int32 Total (int32 a, int32 b) cil managed
{
IL_0000: ldarg.0
IL_0001: ldarg.1
IL_0002: add.ovf
IL_0003: ret
}
.method public hidebysig static uint8 ToByte (int32 n) cil managed
{
IL_0000: ldarg.0
IL_0001: conv.ovf.u1
IL_0002: ret
}
.method public hidebysig static int32 Fast (int32 a, int32 b) cil managed
{
IL_0000: ldarg.0
IL_0001: ldarg.1
IL_0002: mul
IL_0003: ret
}
The entire difference between checked and unchecked is three characters of opcode mnemonic. add.ovf is add with a trap on overflow. conv.ovf.u1 is the range-checking narrowing conversion to unsigned 8-bit; its unchecked twin is the bare conv.u1, which truncates. mul is the plain multiply with no trap. The compiler made one checking decision per instruction and threw the block structure away. The conversion opcodes are a family Narrowing conversions are where overflow checking matters most, because every assignment to a smaller integer type is a potential loss. The CLR gives each target type a matched pair: conv.u1 // int -> byte, truncating (unchecked)
conv.ovf.u1 // int -> byte, range-checked (checked)
conv.i2 // int -> short, truncating (unchecked)
conv.ovf.i2 // int -> short, range-checked (checked)
conv.ovf.i4.un // uint -> int, range-checked, source treated as unsigned
The .un suffix on the last one is the second axis: it tells the conversion to treat its source as unsigned when deciding whether the value fits. That is how checked((int)someUInt) is distinguished from checked((int)someInt) — same target, different interpretation of the bits already on the stack. A decompiler has to carry both the .ovf and the .un flags through, because dropping either one silently changes the conversion's semantics. The asymmetry: where checked does nothing Not every arithmetic operation has an overflow variant, and this is the part naive reconstruction gets wrong. There is no div.ovf and no rem.ovf. Integer division has exactly one overflow case — int.MinValue / -1, whose true result is 2147483648 and does not fit in an int — and ECMA-335 makes that case throw unconditionally, in checked and unchecked contexts alike. Since the behaviour is identical either way, the compiler emits plain div whether or not you wrote checked. The practical consequence fo...

Powered by Readybot.io