Section 3.27

Symbolic execution

Definition

Symbolic execution is a program analysis method that runs a program on symbols standing for unknown inputs instead of concrete values. Each variable holds an expression over these symbols. At a branch it can follow both sides, recording on each side the condition the inputs must meet. The conditions collected along one path, joined by “and”, form the path condition. A constraint solver, usually an SMT solver §3.28 (satisfiability modulo theories solver), decides whether a path condition can be met and, if so, returns inputs that follow the path.

The idea

Some branches are almost impossible to reach by guessing. In z = 2 * y; if (z == x) { if (x > y + 10) error(); }, a random pair of 32-bit inputs satisfies z == x with probability 1 in 232=4,294,967,2962^{32} = 4{,}294{,}967{,}296, so random testing, and fuzzing §10.9 that mutates inputs blindly, almost never reaches error().

Symbolic execution replaces guessing with algebra. It calls the inputs α\alpha and β\beta, notes that z=2βz = 2\beta, and writes down what reaching error() needs: 2β=α2\beta = \alpha and α>β+10\alpha > \beta + 10. Substituting the first condition into the second gives β>10\beta > 10, so β=11\beta = 11 and α=22\alpha = 22 work. A solver performs this kind of substitution and search automatically. Each path gets its own system of conditions, and the number of paths is what makes the method expensive: it suffers from path explosion §3.26, the rapid growth of the number of paths with the number of branches.

How it works

James King described the method in 1976 [266]. The executor keeps one record for each path it is exploring:

Execution proceeds as follows.

  1. Give each input a fresh symbol.

  2. At an assignment, substitute the current expressions into the right-hand side and store the result for the assigned variable.

  3. At a branch on a condition cc, translate cc into an expression and ask the solver whether “path condition and cc” and “path condition and not cc” can each be met. Continue every side that can, with its condition added; drop a side that cannot, an infeasible path.

  4. At a check, such as an array §14.5 access or an assertion, ask whether the path condition can be met together with the violation. If it can, the solver’s answer is an input that triggers the bug.

  5. When a path ends, ask the solver for a model of its path condition, a concrete value for every input symbol: that is a test input for this path.

  6. Choose the next pending path, in depth-first order, by coverage goals or at random, until no paths remain or the time budget is spent.

Concolic execution (concrete plus symbolic), also called dynamic symbolic execution, runs the program on a concrete input while recording the path condition of the path it takes. It then negates one branch condition, solves, and obtains an input that takes a new path. The concrete values let it pass through code that it cannot treat symbolically.

Worked example

Use the code above with mathematical integers; the SMT solver entry explains how 32-bit arithmetic changes the formulas. The inputs are x=αx = \alpha and y=βy = \beta, with α≥0\alpha \ge 0 and β≥0\beta \ge 0, and ∧\land means “and”.

  1. z = 2 * y stores z↦2βz \mapsto 2\beta.

  2. The branch z == x becomes 2β=α2\beta = \alpha. The false side gets the path condition 2β≠α2\beta \ne \alpha; the solver returns, for example, α=1,β=0\alpha = 1, \beta = 0, since 2×0=0≠12 \times 0 = 0 \ne 1.

  3. On the true side, the branch x > y + 10 becomes α>β+10\alpha > \beta + 10. Its false side has the path condition 2β=α∧α≤β+102\beta = \alpha \land \alpha \le \beta + 10, met by α=0,β=0\alpha = 0, \beta = 0.

  4. Its true side has 2β=α∧α>β+102\beta = \alpha \land \alpha > \beta + 10. Substituting α=2β\alpha = 2\beta gives 2β>β+102\beta > \beta + 10, so β>10\beta > 10; the solver returns, for example, β=11,α=22\beta = 11, \alpha = 22.

Running the program on x=22,y=11x = 22, y = 11 gives z=22=xz = 22 = x and 22>11+10=2122 > 11 + 10 = 21, so error() is reached. Three paths gave three test inputs, one of which triggers the bug.

Now let the value of z come from outside the program.

Stated precisely

A symbolic state is a triple (ℓ,σ,φ)(\ell, \sigma, \varphi): ℓ\ell is the program location, σ\sigma the symbolic store, which maps each variable to an expression over the input symbols, and φ\varphi the path condition, a formula over the input symbols. Let path PP have path condition φP\varphi_P. If the expressions encode the program exactly, including its machine arithmetic, its memory and every function it calls, then a choice of input values follows PP exactly when it satisfies φP\varphi_P.

Two guarantees follow from that condition of exactness.

  1. Bugs found are real. If the solver returns a model of φP∧violation\varphi_P \land \text{violation}, running the program on that model triggers the violation; replaying the input on the real program confirms it.

  2. Exhaustive exploration is a proof. If every path is explored to its end, every solver query is answered (none returns “unknown”), and no violation is found, then no input triggers one.

An over-approximate encoding, one that allows more behaviors than the program has, such as an unconstrained result for a call, adds paths that no real input follows, which breaks guarantee 1. An under-approximate encoding, one that allows fewer, such as a fixed stub, a concretized value or a bound on loop iterations, leaves paths out, which breaks guarantee 2.

Limits and common mistakes

Path explosion: the number of paths grows exponentially with the number of branches, and a loop whose bound depends on the input has unboundedly many paths. Practical tools bound loop iterations or path length, so they cover only part of the program’s behavior.

Solver limits: nonlinear arithmetic, floating point and cryptographic functions produce formulas that solvers decide slowly or not at all. Finding an input with a given cryptographic hash §5.1 is infeasible by design. The executor must then concretize or give up on the path.

Symbolic memory: an address that depends on the input can refer to many objects. Forking once per object multiplies the paths; encoding memory as one array formula makes each query harder.

No report is not a proof unless exploration was exhaustive and the encoding exact. A run that stops at a time limit has shown only the bugs it found.

A generated input that does not follow its predicted path when replayed, called divergence, shows that the encoding is wrong somewhere: a missing condition after concretization §19.2.7, a wrong model, or machine arithmetic left out.

Questions answered

What happens when symbolic paths invoke external I/O or complex native helpers?

The executor cannot follow such a call symbolically, so it must replace the call by one of four treatments, and each changes what its results mean. External I/O (input and output, such as reading a sensor, a file or the network) produces values that the program text does not determine and may change the outside world. A complex native helper is compiled code, such as a library or operating-system routine, that the executor cannot interpret or whose formulas it cannot solve.

A wrong model is the dangerous case: a stub that always returns 0 can make an error path look infeasible, so the tool reports no bug where one exists. Calls with side effects need extra care, because the paths that the executor forks share one real world: a packet sent or a file written on one path cannot be undone for the others. Real I/O is therefore modeled, or performed only when one generated input is replayed. Replaying every generated input on the real program detects a wrong model; only exhaustive exploration with exact models supports the claim that no bug exists.

Related entries: State explosion (§3.26), SMT solver (§3.28), Helper contract (§2.21), Fuzzing (§10.9), Path sensitivity (§3.11). Further reading: [266].