Remove some false positives and enable the check.
[smatch.git] / README-smatch
blob3179ffed5a0d467419745d29c69c3d6ade8a029e
1     ***Compiling Smatch***
3 make
5 This compiles creates a binary called "smatch".
7     ***Using Smatch***
9 ./smatch filename.c
11 For the kernel use:  make -k CHECK=/path/to/smatch C=y bzImage | tee warns.txt
13 Most of the things that smatch prints out is not actually warnings.
14 What I was trying to do with that was catch errors where a NULL is
15 passed to a function that dereferences the parameter without checking.
16 There is a script that parses that information.
18       smatch_scripts/find_null_params.sh
20 Sorry the output is a mess right now.  This needs to be cleaned up.
22 Sometimes you it is not obvious why smatch is printing an error.
23 The --debug option is usefull here.  It prints a message on each state change.
25 Sometimes smatch segfaults.  Use valgrind to get a stack dump.  For the kernel
26 do:  make -k CHECK="valgrind /path/to/smatch" C=y failing/file.o
28     ***How Smatch Works***
30 It's basically a state machine that tracks the flow of code.
32 int a;  <- state is unitialized.
34 if (b) {
35        a = foo();  <- state is initialized. 
36        if (a) {
37               bar(a);  <- state is non zero.
38        }
40 baz(a);  <- state is undefined.  possibly unitialized, zero, or non-zero
42 The core parts of smatch understand how code flows, if conditions, loops,
43 and merging states back together.  The checker bits keep track of states
44 that they are interested in and print error messages and so on.
46     ***Coding Smatch***
48 The core smatch files start with "smatch_" the check scripts are named 
49 "check_*.c".  There is a template check script called check_template.c
50 that is designed to teach the basics.  Rename it and edit out the bits
51 you want.
54 The first file you should look at is smatch.h.  All the
55 functions for check scripts are at the top.
57 Smatch is based on sparse.  You will want to understand a couple sparse 
58 data types: 
59      struct expression from expression.h
60      struct symbol from symbol.h
61      struct statement from parse.h
63 More complicated scripts are also going to need functions from smatch_slist.h