Skip to main content
Be Bitwise

Symbolic Execution in Practice

Emulation & Symbolic Execution · VR Analysis & Discovery track

6 min read

In this module

From Concrete to Symbolic

When you test a program normally, you feed it concrete inputs and observe one execution path. To find bugs, you'd ideally want to explore all of them. Symbolic execution approaches this by replacing concrete inputs with symbols representing all possible values, then working through the paths that different values would trigger — systematically, rather than by luck.

The angr Framework

angr is the most widely used binary symbolic execution framework. It can analyse binaries for x86, ARM, MIPS, and other architectures without source code. angr provides a complete analysis pipeline: binary loading, control flow recovery, symbolic execution, and constraint solving.

python
import angr
# Load the binary
proj = angr.Project("./target", auto_load_libs=False)
# Create an initial state at the program entry point
state = proj.factory.entry_state()
# Create a simulation manager to explore paths
simgr = proj.factory.simulation_manager(state)
# Explore until we find a state that reaches the "success" address
simgr.explore(find=0x401234, avoid=0x401300)
if simgr.found:
found_state = simgr.found[0]
# Get the concrete input that reaches the target
solution = found_state.posix.dumps(0) # stdin
print(f"Input to reach target: {solution}")

This simple script answers the question: "What input makes the program reach address 0x401234?" angr explores paths through the program, building constraints at each branch, and uses Z3 to find an input satisfying all constraints along a path to the target.

Symbolic Variables

A symbolic variable in angr is a bitvector of a specified width that has no concrete value - it represents all possible values simultaneously. When the program branches on a condition involving a symbolic variable, angr forks the execution state: one copy follows the true branch (with the constraint added), and the other follows the false branch (with the negated constraint added).

python
# Create a symbolic bitvector of 32 bits
sym_input = state.solver.BVS("user_input", 32)
# Store it in memory where the program reads its input
state.memory.store(input_addr, sym_input)
# After exploration, ask the solver for a concrete value
if state.solver.satisfiable():
concrete_value = state.solver.eval(sym_input)

Each execution state carries a set of constraints. At a branch if (x > 10), the true-branch state adds x > 10 and the false-branch state adds x <= 10. These constraints accumulate as execution proceeds deeper into the program.

Constraint Solving with Z3

Z3 is an SMT (Satisfiability Modulo Theories) solver that handles constraints over bitvectors, integers, arrays, and more. When angr needs to determine if a path is feasible or generate a concrete input, it passes the accumulated constraints to Z3. Z3 either returns "satisfiable" with a model (concrete values for all symbolic variables) or "unsatisfiable" (the path is impossible). Complex constraints with many variables can take significant time to solve - this is another source of scalability challenges alongside path explosion.

You can't run Z3 here, but for a 16-bit input you can afford to do its job the slow way: try all 65,536 values and keep the ones that satisfy a path's constraints. The function below has one winning path. Before you run it, predict how many inputs satisfy x*4 + 7 == 23 once the arithmetic wraps at 16 bits.

Try itPredict the count first, then change the signed comparison to an unsigned one (drop the int16_t cast) and predict how many inputs win.
C (GCC 9.2.0)

The "obvious" answer, x = 4, is only one of four, and it doesn't reach the win block. Solvers work on bit-vectors with exactly this wrap-around and signedness, which is why angr has separate signed and unsigned comparison operators. At 64 bits, brute force is hopeless and the solver's reasoning is the only way through.

The Path Explosion Problem

The core problem with symbolic execution is path explosion. Every branch doubles the path count. A loop whose body has no branch of its own, running anywhere from 0 to N times, creates N+1 paths (one per iteration count). Put a branch inside the body and each iteration doubles them instead, giving 2^N. Nested loops and conditionals compound this fast — real programs hit astronomical numbers quickly.

Consider a simple example:

c
for (int i = 0; i < strlen(input); i++) {
if (input[i] == 'A') { /* path 1 */ }
else { /* path 2 */ }
}

Here's the tree for just the first two characters. Each split adds one constraint to every path below it, and each leaf carries the full conjunction the solver would need to satisfy:

The solver's answers are one possible model each; any input that satisfies the leaf's constraints will do. Add a third character and every leaf splits again.

For a 10-character input, this creates 2^10 = 1024 paths. For a 100-character input, it creates 2^100 paths, about 10^30: no machine will ever walk them all. Real programs are far more complex.

Mitigation strategies include:

  • Directed search: prioritise paths likely to reach the target (using heuristics based on distance in the control flow graph)
  • Path merging: combine states that converge at the same program point, merging their constraints with disjunction
  • Loop bounding: limit the number of loop iterations to explore
  • Function summaries: replace complex library functions with simplified symbolic models
  • Veritesting: combine static symbolic execution (analysing basic blocks as formulas) with dynamic exploration

SimProcedures: Modelling the Environment

Real programs call library functions, make system calls, and interact with the environment. angr handles this through SimProcedures - Python functions that model external behaviour symbolically.

angr ships with SimProcedures for common libc functions: strlen returns a symbolic length, strcmp generates character-by-character equality constraints, malloc returns a symbolic pointer to freshly allocated symbolic memory. For functions without built-in models, you can write custom SimProcedures:

python
class MyCustomFunc(angr.SimProcedure):
def run(self, arg1, arg2):
# Model the function's behaviour symbolically
result = self.state.solver.If(
arg1 > arg2,
arg1 - arg2,
arg2 - arg1
)
return result
# Hook the function address with our model
proj.hook(0x401500, MyCustomFunc())

Watch the comparison in that model. On claripy bitvectors, > is an unsigned comparison, so if the real function compares signed ints, write arg1.SGT(arg2) instead. Getting this wrong gives you a model that disagrees with the binary for every negative input.

Practical Tips

When using angr in practice: identify the target address (the "win" condition) and addresses to avoid (error handlers, exit calls). Use auto_load_libs=False — library code is too complex to symbolise and usually irrelevant. Hook library functions with SimProcedures. Cap exploration time and memory. Start symbolic execution close to the target using blank_state or call_state rather than entry_state. The closer your starting point, the fewer irrelevant paths angr has to grind through.