free_strict: print the line number where the pointer was freed
[smatch.git] / smatch.c
blobf1e2b3e521fc3416a8c76f41b86a086424746c6d
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_state_cnt;
28 char *option_process_function;
29 char *option_project_str = (char *)"smatch_generic";
30 static char *option_db_file = (char *)"smatch_db.sqlite";
31 enum project_type option_project = PROJ_NONE;
32 char *bin_dir;
33 char *data_dir;
34 int option_no_data = 0;
35 int option_spammy = 0;
36 int option_pedantic;
37 int option_print_names;
38 int option_info = 0;
39 int option_full_path = 0;
40 int option_call_tree = 0;
41 int option_no_db = 0;
42 int option_enable = 0;
43 int option_disable = 0;
44 int option_file_output;
45 int option_time;
46 int option_time_stmt;
47 int option_mem;
48 char *option_datadir_str;
49 int option_fatal_checks;
50 int option_succeed;
51 int SMATCH_EXTRA;
53 FILE *sm_outfd;
54 FILE *sql_outfd;
55 FILE *caller_info_fd;
57 int sm_nr_errors;
58 int sm_nr_checks;
59 int __cur_check_id;
61 bool __silence_warnings_for_stmt;
63 typedef void (*reg_func) (int id);
64 #define CK(_x) {.name = #_x, .func = &_x, .enabled = 0},
65 static struct reg_func_info {
66 const char *name;
67 reg_func func;
68 int enabled;
69 } reg_funcs[] = {
70 {"internal", NULL},
71 #include "check_list.h"
73 #undef CK
74 int num_checks = ARRAY_SIZE(reg_funcs);
76 const char *check_name(unsigned short id)
78 if (id >= ARRAY_SIZE(reg_funcs))
79 return "internal";
81 return reg_funcs[id].name;
84 int id_from_name(const char *name)
86 int i;
88 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
89 if (!strcmp(name, reg_funcs[i].name))
90 return i;
92 return 0;
95 static void show_checks(void)
97 int i;
99 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
100 if (!strncmp(reg_funcs[i].name, "check_", 6))
101 printf("%3d. %s\n", i, reg_funcs[i].name);
103 exit(0);
106 static void enable_disable_checks(char *s, bool enable)
108 char buf[128];
109 char *next;
110 int i;
112 do {
113 next = strchr(s, ',');
114 if (next) {
115 *next = '\0';
116 next++;
118 if (*s == '\0')
119 return;
120 if (strncmp(s, "check_", 6) == 0)
121 snprintf(buf, sizeof(buf), "%s", s);
122 else
123 snprintf(buf, sizeof(buf), "check_%s", s);
126 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
127 if (strcmp(reg_funcs[i].name, buf) == 0) {
128 reg_funcs[i].enabled = (enable == true) ? 1 : -1;
129 break;
133 if (i == ARRAY_SIZE(reg_funcs))
134 sm_fatal("'%s' not found", s);
136 } while ((s = next));
139 static void help(void)
141 printf("Usage: smatch [smatch arguments][sparse arguments] file.c\n");
142 printf("--project=<name> or -p=<name>: project specific tests\n");
143 printf("--succeed: don't exit with an error\n");
144 printf("--spammy: print superfluous crap.\n");
145 printf("--pedantic: intended for reviewing new drivers.\n");
146 printf("--info: print info used to fill smatch_data/.\n");
147 printf("--debug: print lots of debug output.\n");
148 printf("--no-data: do not use the /smatch_data/ directory.\n");
149 printf("--data=<dir>: overwrite path to default smatch data directory.\n");
150 printf("--full-path: print the full pathname.\n");
151 printf("--debug-implied: print debug output about implications.\n");
152 printf("--assume-loops: assume loops always go through at least once.\n");
153 printf("--two-passes: use a two pass system for each function.\n");
154 printf("--file-output: instead of printing stdout, print to \"file.c.smatch_out\".\n");
155 printf("--fatal-checks: check output is treated as an error.\n");
156 printf("--help: print this helpful message.\n");
157 exit(1);
160 static int match_option(const char *arg, const char *option)
162 char *str;
163 char *tmp;
164 int ret = 0;
166 str = malloc(strlen(option) + 3);
167 snprintf(str, strlen(option) + 3, "--%s", option);
168 tmp = str;
169 while (*tmp) {
170 if (*tmp == '_')
171 *tmp = '-';
172 tmp++;
174 if (!strcmp(arg, str))
175 ret = 1;
176 free(str);
177 return ret;
180 #define OPTION(_x) do { \
181 if (!found && match_option((*argvp)[1], #_x)) { \
182 found = 1; \
183 option_##_x = 1; \
184 (*argvp)[1] = (*argvp)[0]; \
186 } while (0)
188 void parse_args(int *argcp, char ***argvp)
190 while (*argcp >= 2) {
191 int found = 0;
192 if (!strcmp((*argvp)[1], "--help"))
193 help();
195 if (!strcmp((*argvp)[1], "--show-checks"))
196 show_checks();
198 if (!found && !strncmp((*argvp)[1], "--project=", 10)) {
199 option_project_str = (*argvp)[1] + 10;
200 (*argvp)[1] = (*argvp)[0];
201 found = 1;
203 if (!found && !strncmp((*argvp)[1], "-p=", 3)) {
204 option_project_str = (*argvp)[1] + 3;
205 (*argvp)[1] = (*argvp)[0];
206 found = 1;
208 if (!found && !strncmp((*argvp)[1], "--db-file=", 10)) {
209 option_db_file = (*argvp)[1] + 10;
210 (*argvp)[1] = (*argvp)[0];
211 found = 1;
213 if (!found && !strncmp((*argvp)[1], "--data=", 7)) {
214 option_datadir_str = (*argvp)[1] + 7;
215 (*argvp)[1] = (*argvp)[0];
216 found = 1;
218 if (!found && !strncmp((*argvp)[1], "--debug=", 8)) {
219 option_debug_check = (*argvp)[1] + 8;
220 (*argvp)[1] = (*argvp)[0];
221 found = 1;
223 if (!found && !strncmp((*argvp)[1], "--state-cnt=", 12)) {
224 option_state_cnt = (*argvp)[1] + 12;
225 (*argvp)[1] = (*argvp)[0];
226 found = 1;
228 if (!found && strncmp((*argvp)[1], "--trace=", 8) == 0) {
229 trace_variable = (*argvp)[1] + 8;
230 (*argvp)[1] = (*argvp)[0];
231 found = 1;
233 if (!found && strncmp((*argvp)[1], "--enable=", 9) == 0) {
234 enable_disable_checks((*argvp)[1] + 9, 1);
235 option_enable = 1;
236 (*argvp)[1] = (*argvp)[0];
237 found = 1;
239 if (!found && strncmp((*argvp)[1], "--disable=", 10) == 0) {
240 enable_disable_checks((*argvp)[1] + 10, 0);
241 option_enable = 1;
242 option_disable = 1;
243 (*argvp)[1] = (*argvp)[0];
244 found = 1;
246 if (!found && strncmp((*argvp)[1], "--function=", 11) == 0) {
247 option_process_function = (*argvp)[1] + 11;
248 (*argvp)[1] = (*argvp)[0];
249 found = 1;
252 OPTION(fatal_checks);
253 OPTION(spammy);
254 OPTION(pedantic);
255 OPTION(info);
256 OPTION(debug);
257 OPTION(assume_loops);
258 OPTION(no_data);
259 OPTION(two_passes);
260 OPTION(full_path);
261 OPTION(call_tree);
262 OPTION(file_output);
263 OPTION(time);
264 OPTION(time_stmt);
265 OPTION(mem);
266 OPTION(no_db);
267 OPTION(succeed);
268 OPTION(print_names);
269 if (!found)
270 break;
271 (*argcp)--;
272 (*argvp)++;
275 if (strcmp(option_project_str, "smatch_generic") != 0)
276 option_project = PROJ_UNKNOWN;
278 if (strcmp(option_project_str, "kernel") == 0)
279 option_project = PROJ_KERNEL;
280 else if (strcmp(option_project_str, "wine") == 0)
281 option_project = PROJ_WINE;
282 else if (strcmp(option_project_str, "illumos_kernel") == 0)
283 option_project = PROJ_ILLUMOS_KERNEL;
284 else if (strcmp(option_project_str, "illumos_user") == 0)
285 option_project = PROJ_ILLUMOS_USER;
288 static char *read_bin_filename(void)
290 char filename[PATH_MAX] = {};
291 char proc[PATH_MAX];
293 pid_t pid = getpid();
294 sprintf(proc, "/proc/%d/exe", pid);
295 if (readlink(proc, filename, PATH_MAX) < 0)
296 return NULL;
297 return alloc_string(filename);
300 static char *get_bin_dir(char *arg0)
302 char *orig;
304 orig = read_bin_filename();
305 if (!orig)
306 orig = alloc_string(arg0);
307 return dirname(orig);
310 static char *get_data_dir(char *arg0)
312 char buf[256];
313 char *dir;
315 if (option_no_data)
316 return NULL;
318 if (option_datadir_str) {
319 if (access(option_datadir_str, R_OK))
320 sm_warning("%s is not accessible -- ignored.",
321 option_datadir_str);
322 else
323 return alloc_string(option_datadir_str);
326 strncpy(buf, "smatch_data/", sizeof(buf));
327 dir = alloc_string(buf);
328 if (!access(dir, R_OK))
329 return dir;
331 strncpy(buf, bin_dir, 254);
333 buf[255] = '\0';
334 strncat(buf, "/smatch_data/", 254 - strlen(buf));
335 dir = alloc_string(buf);
336 if (!access(dir, R_OK))
337 return dir;
338 free_string(dir);
339 snprintf(buf, 254, "%s/smatch_data/", SMATCHDATADIR);
340 dir = alloc_string(buf);
341 if (!access(dir, R_OK))
342 return dir;
344 sm_warning("%s is not accessible.", dir);
345 sm_warning("Use --no-data or --data to suppress this message.");
346 return NULL;
349 int main(int argc, char **argv)
351 struct string_list *filelist = NULL;
352 int i;
353 reg_func func;
355 sm_outfd = stdout;
356 sql_outfd = stdout;
357 caller_info_fd = stdout;
359 parse_args(&argc, &argv);
361 if (argc < 2)
362 help();
364 /* this gets set back to zero when we parse the first function */
365 final_pass = 1;
367 bin_dir = get_bin_dir(argv[0]);
368 data_dir = get_data_dir(argv[0]);
370 allocate_hook_memory();
371 allocate_dynamic_states_array(num_checks);
372 allocate_tracker_array(num_checks);
373 create_function_hook_hash();
374 open_smatch_db(option_db_file);
375 sparse_initialize(argc, argv, &filelist);
376 alloc_ptr_constants();
377 SMATCH_EXTRA = id_from_name("register_smatch_extra");
378 allocate_modification_hooks();
380 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
381 __cur_check_id = i;
382 func = reg_funcs[i].func;
383 /* The script IDs start at 1.
384 0 is used for internal stuff. */
385 if (!option_enable || reg_funcs[i].enabled == 1 ||
386 (option_disable && reg_funcs[i].enabled != -1) ||
387 strncmp(reg_funcs[i].name, "register_", 9) == 0)
388 func(i);
390 __cur_check_id = 0;
392 smatch(filelist);
393 free_string(data_dir);
395 if (option_succeed)
396 return 0;
397 if (sm_nr_errors > 0)
398 return 1;
399 if (sm_nr_checks > 0 && option_fatal_checks)
400 return 1;
401 return 0;