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.
8 *
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.
13 *
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
16 *
17 * Copyright 2019 Joyent, Inc.
18 */
19
20 #include <stdio.h>
21 #include <unistd.h>
22 #include <libgen.h>
23 #include "smatch.h"
24 #include "check_list.h"
25
26 char *option_debug_check = (char *)"";
27 char *option_project_str = (char *)"smatch_generic";
28 static char *option_db_file = (char *)"smatch_db.sqlite";
29 enum project_type option_project = PROJ_NONE;
30 char *bin_dir;
31 char *data_dir;
32 int option_no_data = 0;
33 int option_spammy = 0;
34 int option_info = 0;
35 int option_full_path = 0;
36 int option_param_mapper = 0;
37 int option_call_tree = 0;
38 int option_no_db = 0;
39 int option_enable = 0;
40 int option_disable = 0;
41 int option_debug_related;
42 int option_file_output;
43 int option_time;
44 int option_mem;
45 char *option_datadir_str;
46 int option_fatal_checks;
47 int option_succeed;
48 int option_timeout = 60;
49
50 FILE *sm_outfd;
51 FILE *sql_outfd;
52 FILE *caller_info_fd;
53
54 int sm_nr_errors;
55 int sm_nr_checks;
56
57 bool __silence_warnings_for_stmt;
58
59 const char *progname;
60
61 typedef void (*reg_func) (int id);
159 {
160 char *str;
161 char *tmp;
162 int ret = 0;
163
164 str = malloc(strlen(option) + 3);
165 snprintf(str, strlen(option) + 3, "--%s", option);
166 tmp = str;
167 while (*tmp) {
168 if (*tmp == '_')
169 *tmp = '-';
170 tmp++;
171 }
172 if (!strcmp(arg, str))
173 ret = 1;
174 free(str);
175 return ret;
176 }
177
178 #define OPTION(_x) do { \
179 if (match_option((*argvp)[i], #_x)) { \
180 option_##_x = 1; \
181 } \
182 } while (0)
183
184 void parse_args(int *argcp, char ***argvp)
185 {
186 int i;
187
188 for (i = 1 ; i < *argcp; i++) {
189 if (!strcmp((*argvp)[i], "--help"))
190 help();
191
192 if (!strcmp((*argvp)[i], "--show-checks"))
193 show_checks();
194
195 if (!strncmp((*argvp)[i], "--project=", 10))
196 option_project_str = (*argvp)[i] + 10;
197
198 if (!strncmp((*argvp)[i], "-p=", 3))
199 option_project_str = (*argvp)[i] + 3;
214 enable_disable_checks((*argvp)[i] + 9, 1);
215 option_enable = 1;
216 }
217
218 if (strncmp((*argvp)[i], "--disable=", 10) == 0) {
219 enable_disable_checks((*argvp)[i] + 10, 0);
220 option_enable = 1;
221 option_disable = 1;
222 }
223
224 if (!strncmp((*argvp)[i], "--timeout=", 10)) {
225 if (sscanf((*argvp)[i] + 10, "%d",
226 &option_timeout) != 1)
227 sm_fatal("invalid option %s", (*argvp)[i]);
228 }
229
230 OPTION(fatal_checks);
231 OPTION(spammy);
232 OPTION(info);
233 OPTION(debug);
234 OPTION(debug_implied);
235 OPTION(debug_related);
236 OPTION(assume_loops);
237 OPTION(no_data);
238 OPTION(two_passes);
239 OPTION(full_path);
240 OPTION(param_mapper);
241 OPTION(call_tree);
242 OPTION(file_output);
243 OPTION(time);
244 OPTION(mem);
245 OPTION(no_db);
246 OPTION(succeed);
247 }
248
249 if (strcmp(option_project_str, "smatch_generic") != 0)
250 option_project = PROJ_UNKNOWN;
251
252 if (strcmp(option_project_str, "kernel") == 0)
253 option_project = PROJ_KERNEL;
254 else if (strcmp(option_project_str, "wine") == 0)
255 option_project = PROJ_WINE;
305 strncpy(buf, bin_dir, 254);
306
307 buf[255] = '\0';
308 strncat(buf, "/smatch_data/", 254 - strlen(buf));
309 dir = alloc_string(buf);
310 if (!access(dir, R_OK))
311 return dir;
312 free_string(dir);
313 snprintf(buf, 254, "%s/smatch_data/", SMATCHDATADIR);
314 dir = alloc_string(buf);
315 if (!access(dir, R_OK))
316 return dir;
317
318 sm_warning("%s is not accessible.", dir);
319 sm_warning("Use --no-data or --data to suppress this message.");
320 return NULL;
321 }
322
323 int main(int argc, char **argv)
324 {
325 int i;
326 reg_func func;
327
328 sm_outfd = stdout;
329 sql_outfd = stdout;
330 caller_info_fd = stdout;
331
332 progname = argv[0];
333
334 parse_args(&argc, &argv);
335
336 if (argc < 2)
337 help();
338
339 /* this gets set back to zero when we parse the first function */
340 final_pass = 1;
341
342 bin_dir = get_bin_dir(argv[0]);
343 data_dir = get_data_dir(argv[0]);
344
345 allocate_hook_memory();
346 create_function_hook_hash();
347 open_smatch_db(option_db_file);
348 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
349 func = reg_funcs[i].func;
350 /* The script IDs start at 1.
351 0 is used for internal stuff. */
352 if (!option_enable || reg_funcs[i].enabled == 1 ||
353 (option_disable && reg_funcs[i].enabled != -1) ||
354 strncmp(reg_funcs[i].name, "register_", 9) == 0)
355 func(i);
356 }
357
358 smatch(argc, argv);
359 free_string(data_dir);
360
361 if (option_succeed)
362 return 0;
363 if (sm_nr_errors > 0)
364 return 1;
365 if (sm_nr_checks > 0 && option_fatal_checks)
366 return 1;
367 return 0;
368 }
|
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.
8 *
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.
13 *
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
16 *
17 * Copyright 2019 Joyent, Inc.
18 */
19
20 #include <stdio.h>
21 #include <unistd.h>
22 #include <libgen.h>
23 #include "smatch.h"
24 #include "smatch_slist.h"
25 #include "check_list.h"
26
27 char *option_debug_check = (char *)"";
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_info = 0;
36 int option_full_path = 0;
37 int option_param_mapper = 0;
38 int option_call_tree = 0;
39 int option_no_db = 0;
40 int option_enable = 0;
41 int option_disable = 0;
42 int option_file_output;
43 int option_time;
44 int option_mem;
45 char *option_datadir_str;
46 int option_fatal_checks;
47 int option_succeed;
48 int option_timeout = 60;
49
50 FILE *sm_outfd;
51 FILE *sql_outfd;
52 FILE *caller_info_fd;
53
54 int sm_nr_errors;
55 int sm_nr_checks;
56
57 bool __silence_warnings_for_stmt;
58
59 const char *progname;
60
61 typedef void (*reg_func) (int id);
159 {
160 char *str;
161 char *tmp;
162 int ret = 0;
163
164 str = malloc(strlen(option) + 3);
165 snprintf(str, strlen(option) + 3, "--%s", option);
166 tmp = str;
167 while (*tmp) {
168 if (*tmp == '_')
169 *tmp = '-';
170 tmp++;
171 }
172 if (!strcmp(arg, str))
173 ret = 1;
174 free(str);
175 return ret;
176 }
177
178 #define OPTION(_x) do { \
179 if (match_option((*argvp)[1], #_x)) { \
180 option_##_x = 1; \
181 } \
182 } while (0)
183
184 void parse_args(int *argcp, char ***argvp)
185 {
186 int i;
187
188 for (i = 1 ; i < *argcp; i++) {
189 if (!strcmp((*argvp)[i], "--help"))
190 help();
191
192 if (!strcmp((*argvp)[i], "--show-checks"))
193 show_checks();
194
195 if (!strncmp((*argvp)[i], "--project=", 10))
196 option_project_str = (*argvp)[i] + 10;
197
198 if (!strncmp((*argvp)[i], "-p=", 3))
199 option_project_str = (*argvp)[i] + 3;
214 enable_disable_checks((*argvp)[i] + 9, 1);
215 option_enable = 1;
216 }
217
218 if (strncmp((*argvp)[i], "--disable=", 10) == 0) {
219 enable_disable_checks((*argvp)[i] + 10, 0);
220 option_enable = 1;
221 option_disable = 1;
222 }
223
224 if (!strncmp((*argvp)[i], "--timeout=", 10)) {
225 if (sscanf((*argvp)[i] + 10, "%d",
226 &option_timeout) != 1)
227 sm_fatal("invalid option %s", (*argvp)[i]);
228 }
229
230 OPTION(fatal_checks);
231 OPTION(spammy);
232 OPTION(info);
233 OPTION(debug);
234 OPTION(assume_loops);
235 OPTION(no_data);
236 OPTION(two_passes);
237 OPTION(full_path);
238 OPTION(param_mapper);
239 OPTION(call_tree);
240 OPTION(file_output);
241 OPTION(time);
242 OPTION(mem);
243 OPTION(no_db);
244 OPTION(succeed);
245 }
246
247 if (strcmp(option_project_str, "smatch_generic") != 0)
248 option_project = PROJ_UNKNOWN;
249
250 if (strcmp(option_project_str, "kernel") == 0)
251 option_project = PROJ_KERNEL;
252 else if (strcmp(option_project_str, "wine") == 0)
253 option_project = PROJ_WINE;
303 strncpy(buf, bin_dir, 254);
304
305 buf[255] = '\0';
306 strncat(buf, "/smatch_data/", 254 - strlen(buf));
307 dir = alloc_string(buf);
308 if (!access(dir, R_OK))
309 return dir;
310 free_string(dir);
311 snprintf(buf, 254, "%s/smatch_data/", SMATCHDATADIR);
312 dir = alloc_string(buf);
313 if (!access(dir, R_OK))
314 return dir;
315
316 sm_warning("%s is not accessible.", dir);
317 sm_warning("Use --no-data or --data to suppress this message.");
318 return NULL;
319 }
320
321 int main(int argc, char **argv)
322 {
323 struct string_list *filelist = NULL;
324 int i;
325 reg_func func;
326
327 sm_outfd = stdout;
328 sql_outfd = stdout;
329 caller_info_fd = stdout;
330
331 progname = argv[0];
332
333 parse_args(&argc, &argv);
334
335 if (argc < 2)
336 help();
337
338 /* this gets set back to zero when we parse the first function */
339 final_pass = 1;
340
341 bin_dir = get_bin_dir(argv[0]);
342 data_dir = get_data_dir(argv[0]);
343
344 allocate_hook_memory();
345 allocate_dynamic_states_array(num_checks);
346 create_function_hook_hash();
347 open_smatch_db(option_db_file);
348 sparse_initialize(argc, argv, &filelist);
349 alloc_valid_ptr_rl();
350
351 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
352 func = reg_funcs[i].func;
353 /* The script IDs start at 1.
354 0 is used for internal stuff. */
355 if (!option_enable || reg_funcs[i].enabled == 1 ||
356 (option_disable && reg_funcs[i].enabled != -1) ||
357 strncmp(reg_funcs[i].name, "register_", 9) == 0)
358 func(i);
359 }
360
361 smatch(filelist);
362 free_string(data_dir);
363
364 if (option_succeed)
365 return 0;
366 if (sm_nr_errors > 0)
367 return 1;
368 if (sm_nr_checks > 0 && option_fatal_checks)
369 return 1;
370 return 0;
371 }
|