Skip to content

Lesson 3 of 13 · 22m

Control Flow: Finding the Shape of the Code

if/while/switch collapse into compare-and-jump; learn the five CFG shapes, what -O2 does to them, and why linear sweep and recursive descent disassemblers each go blind.

Assembly is a flat list of instructions; programs are trees, loops, and dispatch tables. The wrong mental model is that the compiler preserved your structure — it preserved your behavior, using three primitives: compare-and-jump, call, and return. Most of static analysis is pattern-matching those primitives back into the ifs, whiles, and switches the programmer wrote. Learn five shapes and the control-flow graph stops being noise.

Basic blocks and the CFG

A basic block is a run of instructions with one way in and one way out — it ends at a jump, a call, or a ret. The control-flow graph is basic blocks as nodes and jumps as edges. Everything you'll ever recognize is a shape in that graph.

Control flow shapes

The five shapes

; if/else — a diamond: compare, jump over one arm, both arms rejoin
cmp  eax, 5
jle  ELSE
; ... then-arm ...
jmp  JOIN
ELSE:
; ... else-arm ...
JOIN:

; while — a back-edge: a jump that goes *up* the listing
LOOP:
cmp  ecx, edx
jge  DONE
; ... body ...
inc  ecx
jmp  LOOP                ; backward jump = loop
DONE:

; switch — a bounds check, then one indirect jump through a table
mov  eax, DWORD [rbp-0x4]   ; the switch variable
cmp  eax, 6
ja   DEFAULT             ; out of range -> default case
movsxd rax, eax          ; the same variable, sign-extended to index the table
jmp  QWORD [rax*8 + 0x402000]   ; rax indexes a case-address table

Two impostors to recognize. cmov folds a small if/else into a conditional move with no branch at all — when you expect a diamond and find straight-line code, that's why; ternaries compile this way constantly. And setcc (setz, setne) materializes a comparison as a 0/1 byte — that's bool ok = (x == 5) with no branch either.

Two details in that switch shape pay rent. The ja (unsigned-above) bounds check does double duty: a negative case value wraps to a huge unsigned and lands in DEFAULT too, so one compare guards both ends of the range. And the table only exists because the cases were dense — switch (x) { case 1: ... case 90000: ... } compiles back to compare-chains or a binary-search branch tree; the indirect jump is the compiler's payoff for a contiguous range.

How -O2 rearranges the shapes

  • Loop rotation. while (cond) becomes a guard test plus a do { ... } while (cond) — one compare jumps into the loop body, a second jumps backward from the bottom. The for/while/do-while distinction you wrote is gone; only the trip count and exit condition survive.
  • Tail calls. return f(x) compiles to jmp f — reusing the current stack frame. It looks like a jump into the middle of nowhere until you notice the call-site call is missing.
  • No-return exits. Calls to exit, abort, or __stack_chk_fail end a path — the compiler may drop ud2 after them as a fencepost. Don't look for the block to rejoin; it doesn't.

How disassemblers find the code

  • Linear sweep (objdump -d): decode every byte of .text in order. Fast, and fooled the moment data sits between functions — those bytes decode as instructions and the stream desyncs for everything after.
  • Recursive descent (Ghidra, IDA): follow jmp/call targets, stop at ret and unconditional jumps. Handles inline data, but indirect jumps — jmp rax, jump tables — leave blocks undiscovered until analysis proves where they point.
  • Emulation (angr, some IDA plugins): execute blocks over a fake machine to discover the targets the other two can't prove. Slower, and reserved for the jump tables recursive descent gave up on.

That's what Ghidra's minute of auto-analysis is doing: resolving every indirect target it can, including switch tables. Where it fails you get ?? bytes and unreachable regions — which is itself information: usually a jump table sitting in .rodata that you can define by hand.

One more shape hiding in plain sight: call printf@plt is a jump through the GOT — jmp [rip+0x2fe2] in the .plt that the dynamic linker filled in. In the listing it looks like a function; in reality it's a two-hop trampoline to libc. The call graph knows the difference.

Field notes

  • Function boundaries are inferred: prologues, call targets, and the int3/nop padding compilers insert between functions. Strip the symbols and boundary detection is a heuristic — occasionally wrong.
  • Bytes right after a ret that no jump reaches are almost always padding or data, not dead logic. Don't spend an hour reversing an alignment sled.
  • A call graph is the zoomed-out CFG: functions as nodes, calls as edges. Ghidra's function call graph view answers "who could ever reach this" faster than the listing does.

Keep reading

Related articles from the SudoSecurity blog.