LEVEL 5 · FRONTIER

Mini Decompiler

A decompiler for a defined x86-64 subset that turns functions in an ELF relocatable object you compiled into structured C. It recompiles its own output, runs a differential test over an input grid, compares relocation-masked bytes and writes an evidence report that lists every unknown.

8 stages~18 hoursPythoncore
Read ELF64 sections, symbols and RELA recordsDecode an x86-64 subset and check it against a disassembler listingRecover blocks, jump tables, dominators and loopsLift to an IR with explicit flags and build minimal SSAFold copies, dead code and division by a constantRecover System V parameters, pointer and struct types and signednessStructure a graph into C without gotoProve the output with recompilation, a differential grid and masked bytes
YOU END UP WITH

A decompiler that turns the functions of an x86-64 relocatable object you compiled into one C file, and an evidence report that says what was proved and what stays unknown.

USEFUL FOR
  • Run python3 -m minidec verify on your own object and its source. The command writes decompiled.c, recompiled.o and report.json; a CI job can fail on status fail or unknown.
PROJECT OUTLINE

Mini Decompiler in 8 stages

A decompiler for a defined x86-64 subset that turns functions in an object you compiled into structured C, then proves the output.

  1. Read an ELF relocatable object

    Validate the ELF64 header, read section headers, the symbol table and every RELA record, and cut each function out of .text with its relocations and its alignment padding. A PC-relative record points at S + A + (end of instruction - P), so a target is resolved only with the end of the instruction that holds the field.

    starter · ~2 hours · Python

    ElfObjectElfObject.functionrelocation_target
  2. Decode the x86-64 subset

    Decode legacy prefixes, REX, one and two byte opcodes, ModRM, SIB, displacements and immediates for the instructions the fixtures use. Record where the displacement and immediate fields sit so relocations can be matched to operands. Every instruction must agree with the captured disassembler listing by address, bytes and AT&T text.

    core · ~3 hours · Python

    decodelinear_sweepcompare_with_objdump
  3. Recover control flow

    Follow control flow by recursive descent, resolve the position-independent jump table from its relocations and bounds check, split basic blocks, then compute dominators, dominance frontiers, post-dominators, back edges and natural loops. An indirect jump with no matching idiom stays unresolved and is reported.

    core · ~2 hours · Python

    recursive_descentjump_table_resolverControlFlowGraphdominance_frontiers
  4. Lift to IR and build SSA

    Lift each instruction to a small IR in which flags are an ordinary variable, frame bookkeeping disappears and frame-pointer slots become locals. Place phi statements on iterated dominance frontiers and rename over the dominator tree so every value has one definition, with explicit casts where a narrow definition meets a wide use.

    core · ~3 hours · Python

    lift_instructionlift_functioninsert_phisrename
  5. Simplify and fold idioms

    Turn flag conditions into comparisons, propagate copies and constants, delete dead definitions, merge congruent phis and substitute single-use values without moving a load across a store. Recognize the multiply-and-shift form of signed division by a constant and fold it only when the multiplier, shift and error bound prove the divisor.

    core · ~2 hours · Python

    magic_divisoroptimizepropagate_copiesremove_dead_code
  6. Recover the signature and types

    A System V argument register read before any write is a parameter, and the highest one fixes the count. Coalesce phi webs into C variables, make a scaled element access a pointer to its element and constant offsets a struct, and choose signedness from the instructions that touch each value. Every choice carries its instruction evidence.

    core · ~2 hours · Python

    collect_paramscoalesce_websinfer_typesrecover_signature
  7. Structure the graph and emit C

    Match loops, two-way and n-way conditionals with the immediate post-dominator as the join, emit if, if-else, while, do-while and switch, and print C with recovered struct and table definitions. A region that matches no shape produces a counted goto, and every heuristic goes into the unknowns list.

    stretch · ~2 hours · Python

    Structurerdecompiledecompile_alltranslation_unit
  8. Prove the output

    Recompile the emitted C with the fixture flags, compare each function with relocation fields masked, compare the constants each relocation points at, and run original and candidate side by side over an input grid that crosses limits, zero, negatives and compare constants. The report is fail-closed and lists every unknown. Without clang the recompile and differential legs skip and say why.

    stretch · ~2 hours · Python

    mask_relocationscompare_function_bytesinput_gridverify

Lessons this project uses

All projects · Course catalog · Source on GitHub