Lecture 14 · Applications: Symbolic Execution

Introduction to Automated Reasoning · Lecture 14。个人学习讲义与配套实验,按主题整理并持续修订。

课程源码 ZIP ↓

个人整理的学习笔记,非官方讲义;课程来源与整理说明见课程介绍。配套示例未逐一运行验证。

本篇目录

Maps to: Stanford CS 257 Introduction to Automated Reasoning (Winter 2026), Lecture 14 · Thu 02/19/2026 · Applications: symbolic execution (Rachel Cleaveland). Reading: none listed on the course page. Basis: The Stanford slides are on Canvas. The public Iowa deck Theory Solvers III opens with the symbolic-execution pipeline that motivates string solving (Lecture 13, §1). The rest is written from:

  • King (1976), symbolic execution;
  • Godefroid, Klarlund and Sen (2005), DART;
  • Sen, Marinov and Agha (2005), CUTE;
  • Cadar et al. (2006), EXE, and Cadar, Dunbar and Engler (2008), KLEE;
  • Godefroid, Levin and Molnar (2008), SAGE;
  • Kuznetsov et al. (2012), state merging; Avgerinos et al. (2014), veritesting;
  • the surveys by Cadar and Sen (2013) and Baldoni et al. (2018).

This lecture has no code module. The examples are worked by hand and checked with earlier lectures’ code:

  • every path condition shown was solved with Lecture 12’s solve_lia, and every model was certified;
  • the example programs were run in Python on the generated inputs, to confirm that each input takes the predicted path;
  • the overflow example was solved with Lecture 16’s bitvec, and the aliasing example comes from Lecture 13’s script 01.

The examples and their presentation were designed for these notes and do not reflect what was said in class. Reading time: 1.5 hours


0. How to read this lecture

0.1 Learning goals

After this lecture you should be able to:

  1. Execute a small program symbolically by hand: keep a symbolic store and a path condition, fork at branches, and draw the execution tree.
  2. Turn feasible path conditions into test inputs, and explain why every such input really follows its path.
  3. Say which solver queries symbolic execution makes, which theories it needs (integers or bit-vectors, arrays, strings), and how tools reduce the number and cost of queries.
  4. Explain path explosion, and compare search strategies, bounded loop unrolling and state merging. Relate full merging to bounded model checking.
  5. Run concolic testing (DART) by hand, including concretization and divergence, and say what it gains and loses compared with pure symbolic execution.

0.2 Prerequisites and scope

AssumedIntroduced hereDeveloped later
CDCL(T) and linear integer arithmetic (Lectures 10, 12)symbolic states, path conditions, execution trees—
bounded model checking (Lecture 9)state merging as BMC of programs—
arrays, strings (Lecture 13); bit-vectors (Lecture 16)the theories of program statestheory combination (Lecture 18)
—concolic testing, search strategiesprogram synthesis (Lecture 20)

0.3 Map

§1  why: which input reaches this line?
§2  symbolic states and path conditions (King)
§3  test generation
§4  the solver's role: queries, theories, optimizations
§5  loops and path explosion; search strategies
§6  state merging, and bounded model checking of programs
§7  concolic testing (DART, CUTE, SAGE)
§8  memory: symbolic pointers and arrays
§9  symbolic execution in practice
§10 summary   §11 common pitfalls   §12 exercises   §13 further reading   §14 next lecture

0.4 Notation

SymbolMeaning
X,Y,…X, Y, \ldotssymbolic inputs: fresh constants standing for “any value”
σ\sigmathe symbolic store: program variable → term over the inputs
π\pithe path condition: a formula over the inputs
⟨ℓ,σ,π⟩\langle \ell, \sigma, \pi \ranglea symbolic state at program location ℓ\ell
feasibleπ\pi is satisfiable

1. Why symbolic execution

Testing runs a program on chosen inputs and finds bugs only on those inputs. The question that matters is the reverse one:

Which input makes the program reach this line, or violate this assertion?

Random testing struggles with it whenever the line hides behind a narrow condition. Take if x == 4660 and y == x - 17: a random 32-bit input satisfies it with probability 2−642^{-64}.

Symbolic execution (King 1976) answers the question with a solver. It runs the program on symbols instead of numbers, collects the conditions under which each path is taken, and asks the solver for inputs that satisfy them. It is the left half of the pipeline of Lecture 13, §1:

code (+ assertions)  →  symbolic execution  →  one formula per path  →  SMT solver  →  a test input, or "infeasible"

Its main uses are test generation (inputs that cover every feasible path) and bug finding (inputs that violate assertions or crash the program). Every bug report comes with a concrete input that reproduces it.


2. Symbolic states and path conditions

2.1 King’s idea

Give every input a fresh symbol: xx gets XX, yy gets YY. A symbolic state has three parts:

  • the current location;
  • a store σ\sigma mapping each variable to a term over the symbols, such as z↦2Yz \mapsto 2Y;
  • a path condition π\pi: the conjunction of the branch conditions taken so far, as formulas over the symbols.

Execution rules:

statementeffect on ⟨σ,π⟩\langle \sigma, \pi \rangle
v = eσ:=σ[v↦σ(e)]\sigma := \sigma[v \mapsto \sigma(e)], where σ(e)\sigma(e) substitutes the current terms into ee
if c: A else: Bfork: continue with π∧σ(c)\pi \wedge \sigma(c) into A, and with π∧¬σ(c)\pi \wedge \neg\sigma(c) into B; drop a branch whose condition is unsatisfiable
while c: Alike if c: (A; while c: A), so every iteration forks
assert cif π∧¬σ(c)\pi \wedge \neg\sigma(c) is satisfiable, report a bug with its model; continue with π∧σ(c)\pi \wedge \sigma(c)
return ethe path ends; its model is a test, and σ(e)\sigma(e) is the predicted result

2.2 A worked example

This is DART’s running example, in Python:

def h(x, y):
    if x != y:
        if 2 * x == x + 10:
            ...            # the error branch
    return 0

Start with σ={x↦X,y↦Y}\sigma = \{x \mapsto X, y \mapsto Y\} and π=⊤\pi = \top. The execution tree has three leaves:

                       π = ⊤
              X ≠ Y  /       \  X = Y
                    /         \
     2X = X + 10   /  \ 2X ≠ X + 10      path 3: X = Y
                  /    \
 path 1: X ≠ Y ∧ 2X = X + 10     path 2: X ≠ Y ∧ 2X ≠ X + 10
pathpath conditionmodel (Lecture 12’s solve_lia)running h on it
1X≠Y∧2X=X+10X \neq Y \wedge 2X = X + 10x=10, y=0x = 10,\ y = 0reaches the error branch
2X≠Y∧2X≠X+10X \neq Y \wedge 2X \neq X + 10x=11, y=0x = 11,\ y = 0returns 0
3X=YX = Yx=0, y=0x = 0,\ y = 0returns 0

Path conditions are quantifier-free formulas. Here they are in linear integer arithmetic, exactly the input language of Lectures 10–12. Each model certificate was checked by evaluation, and running h on the three inputs follows the predicted paths.

2.3 What a path condition guarantees

Soundness. Every model of π\pi drives the real program down the path. Executing the program concretely on that input takes, at each branch, the direction recorded in π\pi, and computes the values σ\sigma predicted. So a reported bug is a real bug, with a reproducing input. Symbolic execution is an under-approximation: it reports only behaviours that really happen.

Completeness, in principle. If every path is explored, every reachable assertion violation is found. A program has finitely many paths only without unbounded loops. So in practice symbolic execution is a bounded search, and “no bug found” means “no bug on the paths explored”. Compare model checking (Lecture 9), whose proofs (k-induction, IC3) cover all executions.

Exactness depends on the theory. Python’s int is unbounded, so linear integer arithmetic is the exact theory for +, -, and multiplication by constants. In C or Java, int is a 32-bit word, and the exact theory is bit-vectors (§4.2).


3. Test generation

One test per feasible path. Collect a model of every path condition. The classic example is triangle classification:

def triangle(a, b, c):
    if a <= 0 or b <= 0 or c <= 0:
        return "invalid"
    if a + b <= c or a + c <= b or b + c <= a:
        return "not a triangle"
    if a == b and b == c:
        return "equilateral"
    if a == b or b == c or a == c:
        return "isosceles"
    return "scalene"

Each if forks once, with its whole condition as the branch formula. That gives five paths:

pathmodel (solve_lia)triangle returns
some side ≤ 0(1, 1, 0)invalid
sides positive ∧ some sum ≤ third side(2, 1, 1)not a triangle
… ∧ a = b ∧ b = c(1, 1, 1)equilateral
… ∧ ¬(all equal) ∧ two equal(2, 1, 2)isosceles
… ∧ no two equal(4, 3, 2)scalene

Five inputs cover every statement and every branch direction.

Finer coverage. A tool may fork on each disjunct of a <= 0 or b <= 0 or c <= 0 separately. Python’s or is short-circuiting, so each disjunct really is a separate branch. That gives more paths and more tests, one per way of reaching each return. That is condition coverage rather than branch coverage.

The oracle problem. A test input is only half a test: someone must say whether the output is right. Symbolic execution finds inputs; the checks come from:

  • assertions in the code;
  • crashes (division by zero, out-of-bounds access, failed sanitizer checks);
  • comparing two implementations, as in Lecture 16’s popcount and Lecture 2’s adder miters.

4. The solver’s role

4.1 Which queries

Symbolic execution is a client that makes very many small queries:

  • feasibility at a fork: is π∧c\pi \wedge c satisfiable? Is π∧¬c\pi \wedge \neg c?
  • at an assertion: is π∧¬assert\pi \wedge \neg \text{assert} satisfiable? If so, a model.
  • at the end of a path: a model of π\pi, the test.

Most queries are conjunctions of literals over the inputs, sharing long prefixes with the previous query. That is the incremental setting that CDCL(T) supports with push/pop and learned lemmas.

4.2 Which theories

The program’s data decides the theory:

program datatheorylecture
unbounded integers (Python)linear integer arithmetic (nonlinear: undecidable)12
machine integers (C, Java, binaries)bit-vectors16
memory, arrays, mapsarrays13
stringsstrings and regular expressions13
floating pointfloating-point arithmetic (SMT-LIB FP)—

The theory changes the answers. Consider

if (x > 0) { if (x + 1 < 0) { /* error branch */ } }

Over Z\mathbb{Z} the path condition X>0∧X+1<0X > 0 \wedge X + 1 < 0 is unsatisfiable, so the error branch is unreachable. On 32-bit words it is reachable: Lecture 16’s bitvec solves X>s0∧X+1<s0X >_s 0 \wedge X + 1 <_s 0 with x=2147483647x = 2147483647 (and x=127x = 127 on 8 bits), and the model is certified by evaluation. A symbolic executor that models C integers as mathematical integers would miss the overflow and report a false “unreachable”. KLEE and SAGE therefore reason in QF_BV and QF_ABV (bit-vectors plus arrays).

4.3 Making queries cheap

The KLEE paper describes optimizations that most tools now use:

  • Reuse the current model. A state keeps a model of its π\pi. At a fork on cc, evaluate cc in that model: whichever side it satisfies is feasible with the same model, so only the other side needs a query. That halves the queries at forks.
  • Constraint independence. Split π∧c\pi \wedge c into groups that share no variables, and send only the group that mentions cc‘s variables. The others are already known to be satisfiable.
  • Counterexample caching. Cache models by constraint set:
    • if a cached unsatisfiable set is a subset of the query, the query is unsatisfiable;
    • if a cached model satisfies the query, it is satisfiable.
  • Simplification. Rewrite constraints with the known equalities (for instance X=5X = 5 substitutes XX everywhere), and use the word-level rewriting of Lecture 16.

These make the solver fast enough to be called at every branch. Even so, solving typically dominates the running time of symbolic executors.


5. Loops and path explosion

5.1 Loops

A loop whose bound depends on an input has one path per number of iterations:

def count(n):
    i = 0
    while i < n:
        i = i + 1
    return i
iterations kkpath conditionmodelcount returns
0¬(0<N)\neg(0 < N)n=0n = 00
10<N∧¬(1<N)0 < N \wedge \neg(1 < N)n=1n = 11
20<N∧1<N∧¬(2<N)0 < N \wedge 1 < N \wedge \neg(2 < N)n=2n = 22
3…∧¬(3<N)\ldots \wedge \neg(3 < N)n=3n = 33
4…∧¬(4<N)\ldots \wedge \neg(4 < N)n=4n = 44

There are infinitely many paths. Tools bound the unrolling (at most kk iterations per loop per path), and the guarantee becomes “no bug on paths with at most kk iterations”. Loop summaries go beyond the bound. A summary is a formula for the effect of any number of iterations, such as i=max⁡(0,n)i = \max(0, n), found by pattern matching or by invariant inference, which is Lecture 9’s territory.

5.2 Path explosion

Branches multiply. Consider a sequence of nn independent branches:

c = 0
if a0 > 0: c += 1
if a1 > 0: c += 1
...
if a(n-1) > 0: c += 1

It has 2n2^n paths, every one feasible. A loop over an array with a branch in its body behaves the same way. Path explosion is the problem of symbolic execution: even a modest function has more paths than any tool can explore.

5.3 Search strategies

Since not all paths can be explored, the order matters. A symbolic executor keeps a set of pending states and chooses which to advance:

strategypicksstrengthweakness
depth-firstthe newest statelittle memory; reaches deep codedives into one loop forever
breadth-firstthe oldest statefair; short paths firsthuge frontier; slow to go deep
random path (KLEE)a random walk from the root of the execution treefavours states with few forks above themno notion of progress
coverage-guided (KLEE)states near uncovered codemaximizes new coverageheuristic distance estimates
generational (SAGE)negate every branch of one execution, then pick the best childmany new tests per expensive rundesigned for long concolic runs

Tools usually interleave several strategies (KLEE round-robins between random path and coverage-guided). The analogy with SAT is close: the search order is a heuristic, and like VSIDS it decides what is found in practice.


6. State merging

6.1 The idea

Two states that reach the same location can be merged into one. The merged path condition is the disjunction, and each variable’s value becomes a conditional term. After if a0 > 0: c += 1 the two states ⟨c↦1,π∧A0>0⟩\langle c \mapsto 1, \pi \wedge A_0 > 0\rangle and ⟨c↦0,π∧¬(A0>0)⟩\langle c \mapsto 0, \pi \wedge \neg(A_0 > 0)\rangle merge into

⟨ c↦c1,  π∧((A0>0∧c1=1)∨(¬(A0>0)∧c1=0)) ⟩\langle\, c \mapsto c_1,\ \ \pi \wedge \big((A_0 > 0 \wedge c_1 = 1) \vee (\neg(A_0 > 0) \wedge c_1 = 0)\big) \,\rangle

with a fresh c1c_1. That is the ite encoding of the merge, c1=ite(A0>0,1,0)c_1 = \text{ite}(A_0 > 0, 1, 0).

Merging at every join of §5.2’s program gives one state and one query instead of 2n2^n paths. For n=3n = 3 with the assertion c != 3, the merged formula is

(0<A0∧c1=1∨¬(0<A0)∧c1=0)∧(0<A1∧c2=c1+1∨¬(0<A1)∧c2=c1)∧(…c3…)∧c3=3.(0 < A_0 \wedge c_1 = 1 \vee \neg(0 < A_0) \wedge c_1 = 0) \wedge (0 < A_1 \wedge c_2 = c_1 + 1 \vee \neg(0 < A_1) \wedge c_2 = c_1) \wedge (\ldots c_3 \ldots) \wedge c_3 = 3.

solve_lia finds a0=a1=a2=1a_0 = a_1 = a_2 = 1 (c1=1,c2=2,c3=3c_1 = 1, c_2 = 2, c_3 = 3) in one call.

6.2 The trade-off

Merging moves the case splits from the executor into the solver. The SAT solver now explores the 2n2^n combinations, but with conflict learning, propagation and non-chronological backtracking (Lecture 5) instead of explicit enumeration. That is usually a large win, but not always:

  • merged formulas are bigger and harder;
  • the model-reuse and caching tricks of §4.3 work per path, and become less effective;
  • values that were concrete on each path, such as c=1c = 1, become symbolic (c1c_1).

Kuznetsov et al. (2012) merge only when an estimate of future queries says it pays. Veritesting (Avgerinos et al. 2014) merges over short, loop-free regions and executes path by path elsewhere.

6.3 Full merging is bounded model checking

Merge at every join point, unroll every loop kk times, and the whole program becomes one formula:

  • one fresh variable per assignment (static single assignment);
  • a conditional at each join;
  • the assertion negated.

This is bounded model checking of programs (CBMC, ESBMC), Lecture 9’s BMC with the program’s control flow as the transition relation. Symbolic execution and BMC are the two ends of one spectrum: one path at a time, or all paths at once.


7. Concolic testing

7.1 Concrete and symbolic together

Pure symbolic execution gets stuck when the program does something the solver cannot model:

  • nonlinear arithmetic;
  • a hash function;
  • a library or system call whose code is not available.

Concolic testing (concrete + symbolic; DART 2005, CUTE 2005) runs the program on a concrete input and keeps the symbolic state alongside:

input := an initial input (e.g. all zeros)
repeat:
    run the program concretely on input, recording at each branch the symbolic condition and the direction taken
    pick a branch of this path that has not been flipped yet, and solve
        (conditions before it) ∧ ¬(its condition)
    the model is the next input (variables the query does not mention keep their old values)

Each run follows one concrete path, so every operation has a concrete value. When an operation is beyond the solver, concolic testing concretizes: it replaces the symbolic term by its concrete value and continues.

7.2 A worked example: concretization and divergence

def hashed(y):
    return (y * y) % 50 + 7

def g(x, y):
    if x == hashed(y):
        if y > 100:
            ...            # the error branch
        return 1
    return 0

y * y is nonlinear, so the executor concretizes hashed(y) to its value on the current input. Four runs:

runinput (x,y)(x, y)hashed(y)path observednext query (solved by solve_lia)its model
1(0, 0)7¬(X=7)\neg(X = 7)X=7X = 7x=7x = 7 (y stays 0)
2(7, 0)7X=7∧¬(Y>100)X = 7 \wedge \neg(Y > 100)X=7∧Y>100X = 7 \wedge Y > 100x=7, y=101x = 7,\ y = 101
3(7, 101)8¬(X=8)\neg(X = 8): divergenceX=8X = 8x=8x = 8 (y stays 101)
4(8, 101)8X=8∧Y>100X = 8 \wedge Y > 100—reaches the error branch

Running g on the four inputs gives 0, 1, 0, and the error branch.

Divergence. Run 3 was meant to take the inner branch, but the concretized value 7 was only valid for y=0y = 0. With y=101y = 101, hashed(y) is 8 and the run goes elsewhere. Concolic testing recovers here by negating again with the new concrete value. In general, concretization makes the method incomplete: some reachable paths may never be generated. It also makes it robust: it never stops at an unsupported operation, and every input it generates is a real execution.

What pure symbolic execution does here. It must either give up on the path (the product is not linear), or treat hashed as an uninterpreted function, which is sound for path feasibility but cannot produce a concrete xx without knowing f(101)f(101). Concolic execution gets that value by running the code.

7.3 Generational search and whitebox fuzzing

SAGE (Godefroid, Levin and Molnar 2008) applies concolic testing to large x86 programs that parse files. One run is expensive (millions of instructions, long path conditions), so SAGE negates every branch of the run, not just the last. This generational search yields a whole generation of new inputs per run, ranked by how much new code they cover. Microsoft called it whitebox fuzzing and used it on file parsers. Modern hybrid fuzzers combine a fast random fuzzer with a concolic engine that is called only when the fuzzer gets stuck at a narrow condition such as the x == 4660 of §1.


8. Memory: symbolic pointers and arrays

Programs read and write memory through addresses, and an address can itself depend on the input. Lecture 13 gave the tools:

  • memory is an array from addresses to values;
  • *p = v is a write;
  • *q is a read.

Lecture 13’s script 01 asks whether *p = c1; *q = c2; assert(*p == c1) can fail. The answer is SAT, with the model p = q: the assertion fails exactly when the pointers alias. With p≠qp \neq q assumed, it is UNSAT. Both answers are certified.

How tools handle symbolic addresses.

  • Fork on the target. EXE and KLEE resolve a symbolic pointer to the memory objects it may point to and fork one state per object, which is precise but multiplies paths. Each object is then its own array, so the arrays stay small.
  • One big array. Give all of memory to the solver as a single array with symbolic indices. There are fewer forks, but harder array queries, the eager-vs-lazy trade-off of Lecture 13, §4.5.
  • Concretize the address (concolic): fast, but it misses the aliasing bugs above.

Out-of-bounds accesses become queries too: is π∧(i<0∨i≥length)\pi \wedge (i < 0 \vee i \ge \text{length}) satisfiable? A model is a reproducing input for the memory error.


9. Symbolic execution in practice

Tools.

  • KLEE (on LLVM bitcode; it generated high-coverage tests for the GNU Coreutils and found bugs in them);
  • SAGE (x86 binaries, at Microsoft);
  • angr (binary analysis);
  • Symbolic PathFinder (Java);
  • Pex / IntelliTest (.NET).

Concolic engines also sit inside hybrid fuzzers.

What limits them, roughly in order:

  1. Path explosion (§5), managed by strategies, merging and summaries, never solved.
  2. The environment. Files, network, system calls and libraries must be modelled, or executed concretely (concolic). KLEE ships simplified models of the POSIX environment.
  3. The solver. Nonlinear arithmetic, floating point, hash functions and long string constraints are slow or undecidable, and the solver is called at every fork.
  4. Precision vs scale. Exact bit-level memory models are expensive; tools concretize, simplify and cache, each time trading completeness for speed.

Where it shines. Inputs guarded by narrow conditions: magic numbers, checksums of a few bytes, parsers with many exact cases, and equivalence checks between two implementations. Random testing and fuzzing almost never hit these; one solver query does.

The pattern of the whole course. Here too an answer comes with a certificate. A bug report is an input that reproduces the bug when the program runs, the most convincing certificate there is. What symbolic execution cannot give is the other answer: a proof that no bug exists needs the induction-style reasoning of model checking (Lecture 9) or of program verification (Lecture 15’s kernel and Lean).


10. Summary

  1. Symbolic execution runs a program on symbols. A state is a store of terms plus a path condition; branches fork, and an infeasible branch (unsatisfiable condition) is dropped.
  2. Every model of a path condition drives the real program down that path: tests and bug reports with reproducing inputs. It is an under-approximation, complete only for the paths it explores.
  3. The solver answers feasibility and model queries at every fork. The theory must match the program’s data: bit-vectors for machine integers (otherwise overflow paths are missed), arrays for memory, strings for text.
  4. Tools make queries cheap by reusing the current model, splitting independent constraints, caching and simplifying.
  5. Path explosion (2n2^n paths for nn branches; one path per loop iteration count) forces bounds, search strategies and summaries.
  6. State merging moves case splits into the solver. Full merging is bounded model checking of programs.
  7. Concolic testing runs concretely and solves for the next input, concretizing what the solver cannot handle. It is robust and practical, but incomplete (divergence).

11. Common pitfalls

  • Mathematical integers for machine code. x>0⇒x+1>0x > 0 \Rightarrow x + 1 > 0 is false on 32 bits; use bit-vectors for C, Java and binaries.
  • “No bug found” as a proof. It only covers the paths explored, under the loop bound.
  • Forgetting short-circuit semantics. a or b is two branches in Python, C and Java; a single fork on a∨ba \vee b misses condition coverage.
  • Ignoring the environment. Unmodelled library calls make paths infeasible in the model that are feasible in reality, or the reverse.
  • Concretizing too early. Every concretized value is a constraint the solver never sees; paths that depend on it are lost.
  • Merging everything. Merged formulas can be far harder than the paths they replace.

12. Exercises

1. (by hand) Execute symbolically:

def f(x, y):
    z = x + y
    if z > 10:
        if x < 3:
            return 1
        return 2
    return 3

Write each path condition and a model.

Answer

The store. σ(z)=X+Y\sigma(z) = X + Y.

The paths. Models are from solve_lia; running f on them returns 1, 2, 3.

  • X+Y>10∧X<3X + Y > 10 \wedge X < 3 returns 1, e.g. x=2,y=9x = 2, y = 9.
  • X+Y>10∧X≥3X + Y > 10 \wedge X \ge 3 returns 2, e.g. x=11,y=0x = 11, y = 0.
  • X+Y≤10X + Y \le 10 returns 3, e.g. x=0,y=0x = 0, y = 0.

All three are feasible, so there are three tests.

2. (infeasible paths) In if x > 5: if x < 3: ..., how many paths does the execution tree have, and which query proves one infeasible?

Answer

The forks. The outer fork gives X>5X > 5 and X≤5X \le 5. The inner if sits only on the true side, where it forks into X>5∧X<3X > 5 \wedge X < 3 and X>5∧X≥3X > 5 \wedge X \ge 3.

The infeasible path. The query X>5∧X<3X > 5 \wedge X < 3 is unsatisfiable, so that branch is dropped and the inner body is dead code.

The count. Two feasible paths remain: X>5∧X≥3X > 5 \wedge X \ge 3 (e.g. x=6x = 6) and X≤5X \le 5 (e.g. x=0x = 0).

3. (model reuse) A state with π=X>0\pi = X > 0 has the model X=1X = 1. At a fork on X mod 2=0X \bmod 2 = 0, which side needs a solver query?

Answer

Evaluating the model. In the model, 1 mod 2=1≠01 \bmod 2 = 1 \neq 0, so the false side is feasible with the same model.

The query. Only X>0∧X mod 2=0X > 0 \wedge X \bmod 2 = 0 needs one. Encoded with X=2Q+RX = 2Q + R, 0≤R≤10 \le R \le 1, R=0R = 0, it has the model X=2X = 2.

4. (bit-vectors) Is the error branch of if x < 200: if x + 100 < 50: ... reachable when x is an 8-bit unsigned word? And when x is an integer with 0≤x0 \le x?

Answer

On 8-bit words: reachable. x+100x + 100 wraps around for x≥156x \ge 156, and then equals x+100−256x + 100 - 256, which is below 50 exactly when x<206x < 206.

  • So 156≤x<200156 \le x < 200 reaches the branch.
  • Lecture 16’s bitvec finds x=192x = 192 (292−256=36<50292 - 256 = 36 < 50).
  • With the extra constraint x<156x < 156 it reports UNSAT, certified by DRUP.

Over the integers: unreachable. 0≤x<200∧x+100<500 \le x < 200 \wedge x + 100 < 50 is unsatisfiable (solve_lia), since x≥0x \ge 0 forces x+100≥100x + 100 \ge 100. An executor that models the machine word as a mathematical integer would declare the branch dead.

5. (path explosion) A loop for i in range(8): if a[i] > 0: s += 1 over 8 symbolic integers, followed by assert s != 8. How many paths does pure symbolic execution explore? How many queries does full merging need?

Answer

Path by path. 28=2562^8 = 256 paths, all feasible. The tree has 255 forks, so up to 510 feasibility queries, about half of them with the model-reuse trick of §4.3.

Full merging. One formula with 8 conditional increments and s8=8s_8 = 8, which is one query, satisfied by all ai>0a_i > 0.

6. (concolic) Redo the run table of §7.2 starting from the input (x,y)=(0,5)(x, y) = (0, 5).

Answer

Run 1. (0,5)(0, 5): hashed(5) =25 mod 50+7=32= 25 \bmod 50 + 7 = 32. The path is ¬(X=32)\neg(X = 32); solve X=32X = 32.

Run 2. (32,5)(32, 5): hashed =32= 32. The path is X=32∧¬(Y>100)X = 32 \wedge \neg(Y > 100); solve X=32∧Y>100X = 32 \wedge Y > 100, e.g. y=101y = 101.

Run 3. (32,101)(32, 101): hashed(101) =10201 mod 50+7=8= 10201 \bmod 50 + 7 = 8. The run diverges; solve X=8X = 8.

Run 4. (8,101)(8, 101) reaches the error branch.

7. (aliasing) Write the array formula for a[i] = 1; a[j] = 2; assert a[i] == 1 and say which model violates it.

Answer

The formula. m1=m⟨i◃1⟩∧m2=m1⟨j◃2⟩∧m2[i]≠1m_1 = m\langle i \triangleleft 1\rangle \wedge m_2 = m_1\langle j \triangleleft 2\rangle \wedge m_2[i] \neq 1.

The violating model. It is satisfiable exactly when i=ji = j: then m2[i]=2m_2[i] = 2. It is Lecture 13’s aliasing example with indices instead of pointers.

8. (merging vs BMC) Explain why merging at every join point of a loop-free program produces the same formula as BMC with one time step per statement, up to renaming.

Answer

Merging. It introduces one variable per assignment along all paths, chosen by conditionals at the joins: static single assignment with φ-functions.

BMC. It introduces one copy of each variable per step, constrained by the transition relation, which is the statement’s effect under the control location.

Why they coincide. For loop-free code, both encode “some execution reaches the assertion with its negation true” as one quantifier-free formula over the inputs. The variable copies correspond one to one, and the conditionals correspond to the control-flow choices.


13. Further reading

  • J. C. King, “Symbolic Execution and Program Testing”, CACM 1976.
  • P. Godefroid, N. Klarlund, K. Sen, “DART: Directed Automated Random Testing”, PLDI 2005.
  • K. Sen, D. Marinov, G. Agha, “CUTE: A Concolic Unit Testing Engine for C”, ESEC/FSE 2005.
  • C. Cadar, V. Ganesh, P. Pawlowski, D. Dill, D. Engler, “EXE: Automatically Generating Inputs of Death”, CCS 2006.
  • C. Cadar, D. Dunbar, D. Engler, “KLEE: Unassisted and Automatic Generation of High-Coverage Tests for Complex Systems Programs”, OSDI 2008.
  • P. Godefroid, M. Y. Levin, D. Molnar, “Automated Whitebox Fuzz Testing”, NDSS 2008 (SAGE).
  • V. Kuznetsov, J. Kinder, S. Bucur, G. Candea, “Efficient State Merging in Symbolic Execution”, PLDI 2012.
  • T. Avgerinos, A. Rebert, S. K. Cha, D. Brumley, “Enhancing Symbolic Execution with Veritesting”, ICSE 2014.
  • C. Cadar, K. Sen, “Symbolic Execution for Software Testing: Three Decades Later”, CACM 2013.
  • R. Baldoni, E. Coppa, D. C. D’Elia, C. Demetrescu, I. Finocchi, “A Survey of Symbolic Execution Techniques”, ACM Computing Surveys 2018.
  • E. Clarke, D. Kroening, F. Lerda, “A Tool for Checking ANSI-C Programs”, TACAS 2004 (CBMC).

14. Next lecture

Lecture 15: from decision procedures to proof automation in Lean (guest lecture by Leo de Moura).

  • The other half of the picture. Symbolic execution finds bugs with reproducing inputs. Proof assistants prove their absence, with a small trusted kernel checking every step.
  • How the course’s decision procedures fit in. SAT, congruence closure, the simplex and branch and bound become tactics that explain themselves to that kernel.
← 返回课程目录