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 , 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 and , notes that , and writes down what reaching error() needs: and . Substituting the first condition into the second gives , so and 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:
Location: the instruction the path has reached.
Symbolic store: for each variable, an expression over the input symbols, such as .
Path condition: the conditions collected so far. It starts as “true”, or as the known limits on the inputs.
Execution proceeds as follows.
Give each input a fresh symbol.
At an assignment, substitute the current expressions into the right-hand side and store the result for the assigned variable.
At a branch on a condition , translate into an expression and ask the solver whether “path condition and ” and “path condition and not ” can each be met. Continue every side that can, with its condition added; drop a side that cannot, an infeasible path.
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.
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.
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 and , with and , and means “and”.
z = 2 * ystores .The branch
z == xbecomes . The false side gets the path condition ; the solver returns, for example, , since .On the true side, the branch
x > y + 10becomes . Its false side has the path condition , met by .Its true side has . Substituting gives , so ; the solver returns, for example, .
Running the program on gives and , 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.
Model. With
z = read_sensor(), where the sensor’s datasheet promises a value from 0 to 1023, return a fresh symbol with . The error path needs , met by and : a sensor reading of 11 together with reacheserror().Fixed stub. If instead
read_sensor()is replaced by a stub that always returns 0, the error path needs , which no meets. The tool reports the error as unreachable, which is false for the real sensor.Concretize. With
z = h(y), wherehis compiled code the executor cannot interpret, choose , runh(0)for real, and suppose it returns 57. Continue with and add to the path condition. The error path needs , met by . Without the added , the solver could return , for whichh(5)need not be 57, and the input would not follow the predicted path. With it, every path that needs another value ofyis lost.
Stated precisely
A symbolic state is a triple : is the program location, the symbolic store, which maps each variable to an expression over the input symbols, and the path condition, a formula over the input symbols. Let path have path condition . 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 exactly when it satisfies .
Two guarantees follow from that condition of exactness.
Bugs found are real. If the solver returns a model of , running the program on that model triggers the violation; replaying the input on the real program confirms it.
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.
Model: replace the call by a summary that returns fresh symbols limited by the call’s documented contract, for example a sensor reading from 0 to 1023. The model is exact only if it matches the real call.
Concretize: choose concrete arguments, run the real call, continue with its result, and add “argument equals the chosen value” to the path condition. Paths that need other argument values are lost; without the added condition, generated inputs can stray from the predicted path.
Havoc, meaning make arbitrary: return an unconstrained symbol and treat all memory the call may write as unknown. No real behavior of the call is missed, but paths that no real run follows appear, and with them false bug reports.
Refuse: stop the path and report it as not explored.
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].