Klee: a poor man’s QuickCheck…

Michael Stone, January 1, 2012, , (src)

Recently, I’ve been working on packaging the Klee symbolic interpreter for Debian and Ubuntu because I’d really like to see more folks using it to test their software. To that end, I’ve also begun working through some simple example tests to demonstrate how you might use it to test your own software. Thus, without further ado:

Suppose that you’ve been given the following reference implementation of the Fibonacci function:

and that you’ve been asked to speed it up, say, by replacing the naive recursion in the reference implementation with iteration, like so:

Wouldn’t it be nice to know whether the reference implementation and the iteration-based implementation compute the same function?

Well, conveniently, here’s a test harness, fib.c, that we might use:

with the following Makefile:

all: fib.bc
    klee --max-time=5 --optimize --use-random-search --emit-all-errors --only-output-states-covering-new $<
    for f in $$(find klee-last/ -name '*.err'); do ktest-tool --write-ints $$(echo $$f | cut -f1 -d.).ktest; done

fib.bc: fib.c
    clang -O3 -o $@ -emit-llvm -c $<

.PHONY: all

to check some small random inputs.

(P.S. - Care to guess what the output is?)