5 * Copyright (C) 2006 Dan Carpenter.
7 * Licensed under the Open Software License version 1.1
16 char *option_project_str
= (char *)"";
17 enum project_type option_project
= PROJ_NONE
;
19 int option_no_data
= 0;
20 int option_spammy
= 0;
23 int option_full_path
= 0;
24 int option_param_mapper
= 0;
25 int option_call_tree
= 0;
27 typedef void (*reg_func
) (int id
);
28 void register_smatch_extra(int id
);
29 void register_smatch_ignore(int id
);
30 void register_implications(int id
);
31 void register_function_hooks(int id
);
32 void register_modification_hooks(int id
);
33 void register_containers(int id
);
34 void check_debug(int id
);
35 void check_assigned_expr(int id
);
36 void check_null_deref(int id
);
37 void check_overflow(int id
);
38 void register_check_overflow_again(int id
);
39 void check_locking(int id
);
40 void check_memory(int id
);
41 void check_frees_argument(int id
);
42 void check_puts_argument(int id
);
43 void check_leaks(int id
);
44 void check_type(int id
);
45 void check_allocation_funcs(int id
);
46 void check_err_ptr(int id
);
47 void check_err_ptr_deref(int id
);
48 void check_balanced(int id
);
49 void check_deref_check(int id
);
50 void check_hold_dev(int id
);
51 void check_redundant_null_check(int id
);
52 void check_signed(int id
);
53 void check_precedence(int id
);
54 void check_format_string(int id
);
55 void check_unused_ret(int id
);
56 void check_dma_on_stack(int id
);
57 void check_param_mapper(int id
);
58 void check_call_tree(int id
);
59 void check_dev_queue_xmit(int id
);
60 /* <- your test goes here */
62 /* may as well put wine scripts all together */
63 void check_wine(int id
);
64 void check_wine_filehandles(int id
);
65 void check_wine_WtoA(int id
);
67 /* void register_template(int id); */
69 #define CK(_x) {.name = #_x, .func = &_x}
70 static struct reg_func_info
{
74 CK(register_smatch_extra
), /* smatch_extra always has to be first */
75 CK(register_modification_hooks
),
77 CK(register_smatch_ignore
),
79 CK(check_assigned_expr
),
83 CK(register_check_overflow_again
),
86 CK(check_allocation_funcs
),
88 CK(check_frees_argument
),
90 CK(check_deref_check
),
91 CK(check_redundant_null_check
),
94 CK(check_format_string
),
96 CK(check_dma_on_stack
),
97 CK(check_param_mapper
),
99 CK(check_dev_queue_xmit
),
101 /* <- your test goes here */
102 /* CK(register_template), */
104 /* kernel specific */
106 CK(check_puts_argument
),
108 CK(check_err_ptr_deref
),
111 /* wine specific stuff */
113 CK(check_wine_filehandles
),
116 CK(register_containers
),
117 CK(register_implications
), /* implications always has to be last */
120 const char *check_name(unsigned short id
)
122 if (id
> ARRAY_SIZE(reg_funcs
)) {
125 return reg_funcs
[id
- 1].name
;
128 int id_from_name(const char *name
)
132 for(i
= 0; i
< ARRAY_SIZE(reg_funcs
); i
++){
133 if (!strcmp(name
, reg_funcs
[i
].name
))
139 struct smatch_state
*default_state
[ARRAY_SIZE(reg_funcs
)];
141 static void help(void)
143 printf("Usage: smatch [smatch arguments][sparse arguments] file.c\n");
144 printf("--project=<name> or -p=<name>: project specific tests\n");
145 printf("--spammy: print superfluous crap.\n");
146 printf("--rare: do checks for rare bugs.\n");
147 printf("--info: print info used to fill smatch_data/.\n");
148 printf("--debug: print lots of debug output.\n");
149 printf("--param-mapper: enable param_mapper output.\n");
150 printf("--no-data: do not use the /smatch_data/ directory.\n");
151 printf("--full-path: print the full pathname.\n");
152 printf("--debug-implied: print debug output about implications.\n");
153 printf("--oom <num>: number of mB memory to use before giving up.\n");
154 printf("--no-implied: ignore implications.\n");
155 printf("--assume-loops: assume loops always go through at least once.\n");
156 printf("--known-conditions: don't branch for known conditions.\n");
157 printf("--two-passes: use a two pass system for each function.\n");
158 printf("--help: print this helpfull message.\n");
162 static int match_option(const char *arg
, const char *option
)
168 str
= malloc(strlen(option
) + 3);
169 sprintf(str
, "--%s", option
);
176 if (!strcmp(arg
, str
))
182 #define OPTION(_x) do { \
183 if (!found && match_option((*argvp)[1], #_x)) { \
186 (*argvp)[1] = (*argvp)[0]; \
190 void parse_args(int *argcp
, char ***argvp
)
194 if (!strcmp((*argvp
)[1], "--help")) {
197 if (!found
&& !strncmp((*argvp
)[1], "--project=", 10)) {
198 option_project_str
= (*argvp
)[1] + 10;
199 (*argvp
)[1] = (*argvp
)[0];
202 if (!found
&& !strncmp((*argvp
)[1], "-p=", 3)) {
203 option_project_str
= (*argvp
)[1] + 3;
204 (*argvp
)[1] = (*argvp
)[0];
207 if (!found
&& !strcmp((*argvp
)[1], "--oom")) {
208 option_oom_kb
= atoi((*argvp
)[2]) * 1000;
209 (*argvp
)[2] = (*argvp
)[0];
217 OPTION(debug_implied
);
219 OPTION(assume_loops
);
220 OPTION(known_conditions
);
224 OPTION(param_mapper
);
232 if (!strcmp(option_project_str
, "kernel"))
233 option_project
= PROJ_KERNEL
;
234 if (!strcmp(option_project_str
, "wine"))
235 option_project
= PROJ_WINE
;
238 static char *get_data_dir(char *arg0
)
244 if (option_no_data
) {
247 bin_dir
= dirname(alloc_string(arg0
));
248 strncpy(buf
, bin_dir
, 254);
250 strncat(buf
, "/smatch_data/", 254 - strlen(buf
));
251 dir
= alloc_string(buf
);
252 if (!access(dir
, R_OK
))
254 printf("Warning: %s is not accessible.\n", dir
);
255 printf("Use --no-data to suppress this message.\n");
259 int main(int argc
, char **argv
)
264 parse_args(&argc
, &argv
);
266 data_dir
= get_data_dir(argv
[0]);
268 create_function_hash();
269 for(i
= 0; i
< ARRAY_SIZE(reg_funcs
); i
++){
270 func
= reg_funcs
[i
].func
;
271 /* The script IDs start at 1.
272 0 is used for internal stuff. */
275 register_function_hooks(-1);
278 free_string(data_dir
);