@@ -38,7 +38,7 @@ int main(int argc, char* argv[])
3838```
3939- Alternatively, run this command:
4040` klee@cc0c26c5b84c:~$ echo "int main(int argc,char* argv[]){ char arr[10]; klee_make_symbolic(arr, sizeof(arr), \"arr\"); klee_assume(arr[sizeof(arr)-1] == 0); re_compile(arr); return 0; }" >> re.c `
41- - Compile and emit LLVM bitcode: ` klee@cc0c26c5b84c:~$ clang -c -g -O0 -emit-llvm re.c `
41+ - Compile and emit LLVM bitcode: ` klee@cc0c26c5b84c:~$ clang -emit-llvm -g -c - O0 -Xclang -disable-O0-optnone re.c ` [(NOTE: flags passed to clang are the ones "recommended" by the KLEE project)](https://klee.github.io/tutorials/testing-function/)
4242- Run KLEE and wait for 5-10 minutes: ` klee@cc0c26c5b84c:~$ klee --libc=uclibc re.bc `
4343- A positive result looks like this:
4444```
@@ -75,3 +75,65 @@ KLEE: done: generated tests = 801298
7575klee@cc0c26c5b84c:~ $
7676```
7777
78+ Similarly, the code below tests both `re_compile(...)` and `re_match(...)` which should be sufficient.
79+ Depending on your hardware, you should be able to increase the sizes of `pat` and `txt` to increase your confidence in the verification.
80+
81+
82+ ```C
83+ /*
84+ tiny-regex KLEE test driver
85+ kindly contributed by @DavidKorczynski - see https://github.com/kokke/tiny-regex-c/issues/44
86+ */
87+
88+ int main(int argc, char* argv[])
89+ {
90+ /* test input - a regex-pattern and a text string to search in */
91+ char pat[7];
92+ char txt[3];
93+
94+ /* make input symbolic, to search all paths through the code */
95+ /* i.e. the input is checked for all possible ten-char combinations */
96+ klee_make_symbolic(pat, sizeof(pat), "pat");
97+ klee_make_symbolic(txt, sizeof(txt), "txt");
98+
99+ /* assume proper NULL termination */
100+ klee_assume(pat[sizeof(pat) - 1] == 0);
101+ klee_assume(txt[sizeof(txt) - 1] == 0);
102+
103+ /* verify abscence of run-time errors - go! */
104+ int l;
105+ re_match(pat, txt, &l);
106+
107+ return 0;
108+ }
109+ ```
110+
111+ My modest hardware completes a check of a 7-char pattern and a 3-char text string in 20-30 minutes (size includes null-termination):
112+
113+ ```
114+ klee@780432c1aaae0:~$ clang -emit-llvm -g -c -O0 -Xclang -disable-O0-optnone re.c
115+ klee@780432c1aaae0:~$ time klee --libc=uclibc --optimize re.bc
116+ KLEE: NOTE: Using klee-uclibc : /tmp/klee_build90stp_z3/runtime/lib/klee-uclibc.bca
117+ KLEE: output directory is "/home/klee/klee-out-0"
118+ KLEE: Using STP solver backend
119+ warning: Linking two modules of different target triples: re.bc' is 'x86_64-unknown-linux-gnu' whereas '__uClibc_main.os' is 'x86_64-pc-linux-gnu'
120+
121+ KLEE: WARNING: undefined reference to function: fcntl
122+ KLEE: WARNING: undefined reference to function: fstat
123+ KLEE: WARNING: undefined reference to function: ioctl
124+ KLEE: WARNING: undefined reference to function: open
125+ KLEE: WARNING: undefined reference to function: write
126+ KLEE: WARNING: executable has module level assembly (ignoring)
127+ KLEE: WARNING ONCE: calling external: ioctl(0, 21505, 94248844458320) at libc/termios/tcgetattr:43 12
128+ KLEE: WARNING ONCE: calling __user_main with extra arguments.
129+ KLEE: WARNING ONCE: skipping fork (memory cap exceeded)
130+
131+ KLEE: done: total instructions = 201292178
132+ KLEE: done: completed paths = 910249
133+ KLEE: done: generated tests = 910249
134+
135+ real 29m16.633s
136+ user 19m38.438s
137+ sys 9m34.654s
138+ klee@780432c1aaae0:~$
139+ ```
0 commit comments