db/fixup_kernel.sh: fix clear_user() handling
[smatch.git] / smatch.c
blob4f45f290f8bafa92078c9123427c001dd0220864
1 /*
2 * Copyright (C) 2006 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
18 #include <stdio.h>
19 #include <unistd.h>
20 #include <libgen.h>
21 #include "smatch.h"
22 #include "smatch_slist.h"
23 #include "check_list.h"
25 char *option_debug_check;
26 char *option_debug_var;
27 char *option_process_function;
28 char *option_project_str = (char *)"smatch_generic";
29 static char *option_db_file = (char *)"smatch_db.sqlite";
30 enum project_type option_project = PROJ_NONE;
31 char *bin_dir;
32 char *data_dir;
33 int option_no_data = 0;
34 int option_spammy = 0;
35 int option_pedantic;
36 int option_print_names;
37 int option_info = 0;
38 int option_full_path = 0;
39 int option_call_tree = 0;
40 int option_no_db = 0;
41 int option_enable = 0;
42 int option_disable = 0;
43 int option_file_output;
44 int option_time;
45 int option_time_stmt;
46 int option_mem;
47 char *option_datadir_str;
48 int option_fatal_checks;
49 int option_succeed;
50 int SMATCH_EXTRA;
52 FILE *sm_outfd;
53 FILE *sql_outfd;
54 FILE *caller_info_fd;
56 int sm_nr_errors;
57 int sm_nr_checks;
58 int __cur_check_id;
60 bool __silence_warnings_for_stmt;
62 typedef void (*reg_func) (int id);
63 #define CK(_x) {.name = #_x, .func = &_x, .enabled = 0},
64 static struct reg_func_info {
65 const char *name;
66 reg_func func;
67 int enabled;
68 } reg_funcs[] = {
69 {"internal", NULL},
70 #include "check_list.h"
72 #undef CK
73 int num_checks = ARRAY_SIZE(reg_funcs);
75 const char *check_name(unsigned short id)
77 if (id >= ARRAY_SIZE(reg_funcs))
78 return "internal";
80 return reg_funcs[id].name;
83 int id_from_name(const char *name)
85 int i;
87 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
88 if (!strcmp(name, reg_funcs[i].name))
89 return i;
91 return 0;
94 static void show_checks(void)
96 int i;
98 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
99 if (!strncmp(reg_funcs[i].name, "check_", 6))
100 printf("%3d. %s\n", i, reg_funcs[i].name);
102 exit(0);
105 static void enable_disable_checks(char *s, bool enable)
107 char buf[128];
108 char *next;
109 int i;
111 do {
112 next = strchr(s, ',');
113 if (next) {
114 *next = '\0';
115 next++;
117 if (*s == '\0')
118 return;
119 if (strncmp(s, "check_", 6) == 0)
120 snprintf(buf, sizeof(buf), "%s", s);
121 else
122 snprintf(buf, sizeof(buf), "check_%s", s);
125 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
126 if (strcmp(reg_funcs[i].name, buf) == 0) {
127 reg_funcs[i].enabled = (enable == true) ? 1 : -1;
128 break;
132 if (i == ARRAY_SIZE(reg_funcs))
133 sm_fatal("'%s' not found", s);
135 } while ((s = next));
138 static void help(void)
140 printf("Usage: smatch [smatch arguments][sparse arguments] file.c\n");
141 printf("--project=<name> or -p=<name>: project specific tests\n");
142 printf("--succeed: don't exit with an error\n");
143 printf("--spammy: print superfluous crap.\n");
144 printf("--pedantic: intended for reviewing new drivers.\n");
145 printf("--info: print info used to fill smatch_data/.\n");
146 printf("--debug: print lots of debug output.\n");
147 printf("--no-data: do not use the /smatch_data/ directory.\n");
148 printf("--data=<dir>: overwrite path to default smatch data directory.\n");
149 printf("--full-path: print the full pathname.\n");
150 printf("--debug-implied: print debug output about implications.\n");
151 printf("--assume-loops: assume loops always go through at least once.\n");
152 printf("--two-passes: use a two pass system for each function.\n");
153 printf("--file-output: instead of printing stdout, print to \"file.c.smatch_out\".\n");
154 printf("--fatal-checks: check output is treated as an error.\n");
155 printf("--help: print this helpful message.\n");
156 exit(1);
159 static int match_option(const char *arg, const char *option)
161 char *str;
162 char *tmp;
163 int ret = 0;
165 str = malloc(strlen(option) + 3);
166 snprintf(str, strlen(option) + 3, "--%s", option);
167 tmp = str;
168 while (*tmp) {
169 if (*tmp == '_')
170 *tmp = '-';
171 tmp++;
173 if (!strcmp(arg, str))
174 ret = 1;
175 free(str);
176 return ret;
179 #define OPTION(_x) do { \
180 if (!found && match_option((*argvp)[1], #_x)) { \
181 found = 1; \
182 option_##_x = 1; \
183 (*argvp)[1] = (*argvp)[0]; \
185 } while (0)
187 void parse_args(int *argcp, char ***argvp)
189 while (*argcp >= 2) {
190 int found = 0;
191 if (!strcmp((*argvp)[1], "--help"))
192 help();
194 if (!strcmp((*argvp)[1], "--show-checks"))
195 show_checks();
197 if (!found && !strncmp((*argvp)[1], "--project=", 10)) {
198 option_project_str = (*argvp)[1] + 10;
199 (*argvp)[1] = (*argvp)[0];
200 found = 1;
202 if (!found && !strncmp((*argvp)[1], "-p=", 3)) {
203 option_project_str = (*argvp)[1] + 3;
204 (*argvp)[1] = (*argvp)[0];
205 found = 1;
207 if (!found && !strncmp((*argvp)[1], "--db-file=", 10)) {
208 option_db_file = (*argvp)[1] + 10;
209 (*argvp)[1] = (*argvp)[0];
210 found = 1;
212 if (!found && !strncmp((*argvp)[1], "--data=", 7)) {
213 option_datadir_str = (*argvp)[1] + 7;
214 (*argvp)[1] = (*argvp)[0];
215 found = 1;
217 if (!found && !strncmp((*argvp)[1], "--debug=", 8)) {
218 option_debug_check = (*argvp)[1] + 8;
219 (*argvp)[1] = (*argvp)[0];
220 found = 1;
222 if (!found && strncmp((*argvp)[1], "--trace=", 8) == 0) {
223 trace_variable = (*argvp)[1] + 8;
224 (*argvp)[1] = (*argvp)[0];
225 found = 1;
227 if (!found && strncmp((*argvp)[1], "--enable=", 9) == 0) {
228 enable_disable_checks((*argvp)[1] + 9, 1);
229 option_enable = 1;
230 (*argvp)[1] = (*argvp)[0];
231 found = 1;
233 if (!found && strncmp((*argvp)[1], "--disable=", 10) == 0) {
234 enable_disable_checks((*argvp)[1] + 10, 0);
235 option_enable = 1;
236 option_disable = 1;
237 (*argvp)[1] = (*argvp)[0];
238 found = 1;
240 if (!found && strncmp((*argvp)[1], "--function=", 11) == 0) {
241 option_process_function = (*argvp)[1] + 11;
242 (*argvp)[1] = (*argvp)[0];
243 found = 1;
246 OPTION(fatal_checks);
247 OPTION(spammy);
248 OPTION(pedantic);
249 OPTION(info);
250 OPTION(debug);
251 OPTION(assume_loops);
252 OPTION(no_data);
253 OPTION(two_passes);
254 OPTION(full_path);
255 OPTION(call_tree);
256 OPTION(file_output);
257 OPTION(time);
258 OPTION(time_stmt);
259 OPTION(mem);
260 OPTION(no_db);
261 OPTION(succeed);
262 OPTION(print_names);
263 if (!found)
264 break;
265 (*argcp)--;
266 (*argvp)++;
269 if (strcmp(option_project_str, "smatch_generic") != 0)
270 option_project = PROJ_UNKNOWN;
272 if (strcmp(option_project_str, "kernel") == 0)
273 option_project = PROJ_KERNEL;
274 else if (strcmp(option_project_str, "wine") == 0)
275 option_project = PROJ_WINE;
276 else if (strcmp(option_project_str, "illumos_kernel") == 0)
277 option_project = PROJ_ILLUMOS_KERNEL;
278 else if (strcmp(option_project_str, "illumos_user") == 0)
279 option_project = PROJ_ILLUMOS_USER;
282 static char *read_bin_filename(void)
284 char filename[PATH_MAX] = {};
285 char proc[PATH_MAX];
287 pid_t pid = getpid();
288 sprintf(proc, "/proc/%d/exe", pid);
289 if (readlink(proc, filename, PATH_MAX) < 0)
290 return NULL;
291 return alloc_string(filename);
294 static char *get_bin_dir(char *arg0)
296 char *orig;
298 orig = read_bin_filename();
299 if (!orig)
300 orig = alloc_string(arg0);
301 return dirname(orig);
304 static char *get_data_dir(char *arg0)
306 char buf[256];
307 char *dir;
309 if (option_no_data)
310 return NULL;
312 if (option_datadir_str) {
313 if (access(option_datadir_str, R_OK))
314 sm_warning("%s is not accessible -- ignored.",
315 option_datadir_str);
316 else
317 return alloc_string(option_datadir_str);
320 strncpy(buf, "smatch_data/", sizeof(buf));
321 dir = alloc_string(buf);
322 if (!access(dir, R_OK))
323 return dir;
325 strncpy(buf, bin_dir, 254);
327 buf[255] = '\0';
328 strncat(buf, "/smatch_data/", 254 - strlen(buf));
329 dir = alloc_string(buf);
330 if (!access(dir, R_OK))
331 return dir;
332 free_string(dir);
333 snprintf(buf, 254, "%s/smatch_data/", SMATCHDATADIR);
334 dir = alloc_string(buf);
335 if (!access(dir, R_OK))
336 return dir;
338 sm_warning("%s is not accessible.", dir);
339 sm_warning("Use --no-data or --data to suppress this message.");
340 return NULL;
343 int main(int argc, char **argv)
345 struct string_list *filelist = NULL;
346 int i;
347 reg_func func;
349 sm_outfd = stdout;
350 sql_outfd = stdout;
351 caller_info_fd = stdout;
353 parse_args(&argc, &argv);
355 if (argc < 2)
356 help();
358 /* this gets set back to zero when we parse the first function */
359 final_pass = 1;
361 bin_dir = get_bin_dir(argv[0]);
362 data_dir = get_data_dir(argv[0]);
364 allocate_hook_memory();
365 allocate_dynamic_states_array(num_checks);
366 allocate_tracker_array(num_checks);
367 create_function_hook_hash();
368 open_smatch_db(option_db_file);
369 sparse_initialize(argc, argv, &filelist);
370 alloc_ptr_constants();
371 SMATCH_EXTRA = id_from_name("register_smatch_extra");
372 allocate_modification_hooks();
374 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
375 __cur_check_id = i;
376 func = reg_funcs[i].func;
377 /* The script IDs start at 1.
378 0 is used for internal stuff. */
379 if (!option_enable || reg_funcs[i].enabled == 1 ||
380 (option_disable && reg_funcs[i].enabled != -1) ||
381 strncmp(reg_funcs[i].name, "register_", 9) == 0)
382 func(i);
384 __cur_check_id = 0;
386 smatch(filelist);
387 free_string(data_dir);
389 if (option_succeed)
390 return 0;
391 if (sm_nr_errors > 0)
392 return 1;
393 if (sm_nr_checks > 0 && option_fatal_checks)
394 return 1;
395 return 0;