KLEE (Cadar, Dunbar and Engler, OSDI 2008) is a symbolic execution engine that automatically generates high-coverage tests for systems programs, including ones that read files and make system calls. Symbolic execution runs a program on symbolic inputs, which stand for every possible value, and records the conditions each path places on them. Solving those conditions yields concrete inputs that drive the real program down each path.

Citation

Problem

Manual tests are time-consuming to write and still miss edge cases. Random testing makes no use of the branch conditions it must satisfy; as the paper puts it, satisfying a single 32-bit equality requires guessing one value out of four billion. Earlier symbolic execution tools mostly could not handle a program’s environment (files, system calls and their failures), which drives much of a systems program’s behavior.

Main idea

KLEE runs a program on symbolic inputs and keeps, for each path, a path condition: the conjunction of branch conditions taken so far. At a branch whose condition depends on a symbolic value, it asks a constraint solver (STP) whether each side is feasible and forks the state if both are, adding the condition to one side and its negation to the other. When a path ends, solving its path condition gives a concrete test.

Take a function of one 32-bit input x:

int f(int x) {
  if (x > 10) {
    if (x == 42) abort();
    return 1;
  }
  return 0;
}

KLEE explores three paths, one test each:

flowchart TD
    S0["x symbolic, path condition: true"]
    S0 -->|"x > 10 false"| S1["x ≤ 10"]
    S0 -->|"x > 10 true"| S2["x > 10"]
    S2 -->|"x == 42 false"| S3["x > 10 and x ≠ 42"]
    S2 -->|"x == 42 true"| S4["x > 10 and x = 42"]
    S1 --> T1["Solver picks x = 0, returns 0"]
    S3 --> T3["Solver picks x = 11, returns 1"]
    S4 --> E4["x = 42 reaches abort: bug report with its test"]

The solver finds x = 42 in one query, while a uniformly random 32-bit input reaches the abort with probability .

Bug finding also covers operations that are not branches. At every dangerous operation (a pointer dereference, a division, an assert), KLEE asks whether any input allowed by the path condition makes it fail, and if so emits that input. Functional equivalence works the same way: for two implementations f and g, KLEE runs assert(f(x) == g(x)) on a symbolic x, and any violating input shows where they differ.

Mechanism

KLEE interprets LLVM bitcode. Four design decisions account for most of its practicality:

  • State representation. Each path is an explicit state inside the engine (stack, heap, globals, path condition), so forking never reruns the path prefix. Object-level copy-on-write keeps thousands of concurrent states affordable.
  • Constraint solving. Solver queries dominate running time, so KLEE simplifies expressions, splits each query into independent subsets that share no variables, and caches solutions in a counter-example cache that also answers subset and superset queries. These optimizations cut the average number of STP queries to 5% of the unoptimized count.
  • Search heuristics. A run has far more paths than it can finish, so KLEE alternates two state-selection strategies: random path selection, which walks the fork tree from the root and so favors shallow states, and coverage-optimized search, which favors states near uncovered code.
  • Environment models. About 2,500 lines of C model roughly 40 system calls (open, read, stat and others), much as mocks replace dependencies in unit tests. Symbolic files live in a per-state symbolic file system, and KLEE can optionally make system calls fail, which exercises error-handling code.

Evidence

Line coverage reported in the paper, library code excluded:

  • GNU Coreutils (89 tools): 84.5% overall, against 67.7% for the developers’ test suite; 100% on 16 tools
  • BusyBox (75 tools): 90.5% overall, against 44.8% for the developers’ test suite; 100% on 31 tools
  • 56 serious bugs across 452 applications (about 430K lines), including MINIX utilities and the HiStar kernel
  • Cross-checking 67 Coreutils tools against their BusyBox counterparts found functional errors and many inconsistencies

On 15 Coreutils tools, random testing covered less than both the manual tests and KLEE, and adding more random tests barely raised its coverage.

Assumptions and limits

Path explosion remains the limiting factor: paths grow exponentially with input-dependent branches, so coverage depends on the search heuristics and on bounded input sizes. Equivalence results hold only on the explored paths. The 2008 version did not support symbolic floating point, longjmp, threads or assembly, and every memory object needs a concrete size. Nondeterminism and bugs in the models produced false positives in practice. Compiling a large system to LLVM bitcode is itself substantial work (wllvm helps).

Open questions

  • Can symbolic execution handle non-deterministic programs? Could KLEE’s constraints capture the theoretical bounds of randomized algorithms?
  • Is symbolic execution possible at a lower level, like the instruction set, or do the semantics stop being interpretable there? If so, the result would be a language-agnostic engine. S2E (ASPLOS 2011) does this for x86 binaries by translating machine code to LLVM inside QEMU and running KLEE on it.
  • Can the same constraint techniques simplify program logic by detecting redundant constraints?

Sources