Type Recovery & Data Flow Analysis
How Decompilers Work · Reverse Engineering track
5 min read
In this module
Giving Meaning to Raw Bytes
Machine code is typeless. To the CPU, everything is bytes — no integers, floats, pointers, strings, or structs. The same register holds an integer in one instruction and a pointer in the next without any distinction. The decompiler has to reconstruct types by watching how values are used.
Clues from Instructions
Different types are manipulated by different instructions:
- Integer operations (ADD, SUB, IMUL) suggest integer types
- Floating-point operations (ADDSS, MULSD, CVTSI2SD) reveal float/double types
- Memory dereferences (MOV EAX, [ECX]) suggest that ECX is a pointer
- Comparison with specific constants (CMP EAX, 0x41 where 0x41 = 'A') hint at character types
- Signedness pairs: the compiler picks a signed or unsigned version of several operations, so SAR vs SHR (arithmetic vs logical right shift), MOVSX vs MOVZX, JL/JG vs JB/JA after a compare, and IMUL/IDIV vs MUL/DIV all hint at whether a value was signed. (SHL tells you nothing, since a left shift is the same either way.)
MOVSS XMM0, [EBP-8] ; loads a float from the stackADDSS XMM0, XMM1 ; float additionMOVSS [EBP-8], XMM0 ; stores a float back
Inference: variable at EBP-8 is a 32-bit floatType recovery infers data types from usage patterns. If a value is used in floating-point instructions, it is a float. If it is dereferenced as a memory address, it is a pointer. If it is compared with small constants in branch conditions, it may be an enum or boolean. The decompiler combines many such clues to assign types to every variable.
Stack Variable Recovery
Local variables live on the stack, accessed through offsets from the stack pointer or frame pointer. The decompiler analyses all accesses to the stack frame to identify individual variables:
MOV DWORD [EBP-4], 10 ; 4-byte write at offset -4MOV DWORD [EBP-8], 20 ; 4-byte write at offset -8LEA EAX, [EBP-24] ; address of 16-byte region at offset -24
Inference: int var_4; // at EBP-4 int var_8; // at EBP-8 char buf[16]; // at EBP-24 (the LEA suggests it is passed as a buffer)Overlapping accesses (accessing the same stack offset with different sizes) complicate this analysis and may indicate union types or type punning.
Struct Recovery
When code accesses memory at regular offsets from a base pointer, the decompiler can infer a struct:
MOV EAX, [ECX] ; offset 0: first fieldMOV EBX, [ECX+4] ; offset 4: second fieldMOVSS XMM0, [ECX+8] ; offset 8: third field (float)MOV EDX, [ECX+12] ; offset 12: fourth field
Inference:struct { int field_0; // 4 bytes at offset 0 int field_4; // 4 bytes at offset 4 float field_8; // 4 bytes at offset 8 int field_c; // 4 bytes at offset 12};More sophisticated analysis can detect arrays within structs (regular access patterns with computed indices), vtable pointers (function pointer tables accessed through the first field), and inheritance (child structs that extend parent layouts).
Propagation and Unification
Type recovery is not just local - types propagate through assignments, function calls, and data flow:
- If a function returns its value in XMM0 rather than RAX/EAX (in the System V and Windows x64 conventions), and callers use it in floating-point operations, the return type is very likely
floatordouble - If a value is passed to
strlen(), it's being treated as a string pointer - If a value is passed to
free(), it's treated as a heap-allocated pointer
The decompiler builds a system of type constraints and solves them through type unification - finding the simplest type assignment that satisfies all constraints simultaneously. For example, if p = malloc(16) and later strlen(p) is called, the constraints p : void * and p : char * unify to char *, and that type then flows back to every other use of p. When constraints conflict, it indicates a type cast, union, or type confusion in the original code.
Debug Information: The Shortcut
When available, debug information (like DWARF on Linux or PDB on Windows) provides exact type and variable information embedded in the binary. This includes:
- Variable names and types
- Function signatures
- Struct/class definitions
- Source file and line number mappings
Debug builds include this information; release builds strip it for size and to make reverse engineering harder. When debug info is present, the decompiler's output is dramatically more readable.
The Limits of Type Recovery
Type recovery is fundamentally heuristic. The decompiler can determine that a variable is a 4-byte integer, but it cannot know whether it was originally declared as int, unsigned int, long, or even an enum. It cannot recover typedefs, type aliases, or the semantic meaning of types.
The output is functional pseudo-code that captures behaviour, not a faithful reconstruction of the programmer's type design. Usually that's enough — understanding what the program does is the goal.
Helping the Decompiler
You can sharpen the decompiler's output significantly by feeding it type information. Define a struct you've recovered and the decompiler propagates correct field names and sizes everywhere that type appears. Apply a function signature from documentation and every call site improves. The best workflow here is collaborative: the decompiler grinds through register tracking and memory accesses; you supply the semantic context — what these types mean, what this struct represents. That combination is why modern decompilation is genuinely useful rather than merely academic.