Print this page
11462 enable smatch by default
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/tools/smatch/src/smatch.c
+++ new/usr/src/tools/smatch/src/smatch.c
1 1 /*
2 2 * Copyright (C) 2006 Dan Carpenter.
3 3 *
4 4 * This program is free software; you can redistribute it and/or
5 5 * modify it under the terms of the GNU General Public License
↓ open down ↓ |
5 lines elided |
↑ open up ↑ |
6 6 * as published by the Free Software Foundation; either version 2
7 7 * of the License, or (at your option) any later version.
8 8 *
9 9 * This program is distributed in the hope that it will be useful,
10 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 * GNU General Public License for more details.
13 13 *
14 14 * You should have received a copy of the GNU General Public License
15 15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
16 + *
17 + * Copyright 2019 Joyent, Inc.
16 18 */
17 19
18 20 #include <stdio.h>
19 21 #include <unistd.h>
20 22 #include <libgen.h>
21 23 #include "smatch.h"
22 24 #include "check_list.h"
23 25
24 26 char *option_debug_check = (char *)"";
25 27 char *option_project_str = (char *)"smatch_generic";
26 28 static char *option_db_file = (char *)"smatch_db.sqlite";
27 29 enum project_type option_project = PROJ_NONE;
28 30 char *bin_dir;
29 31 char *data_dir;
30 32 int option_no_data = 0;
31 33 int option_spammy = 0;
32 34 int option_info = 0;
33 35 int option_full_path = 0;
34 36 int option_param_mapper = 0;
35 37 int option_call_tree = 0;
↓ open down ↓ |
10 lines elided |
↑ open up ↑ |
36 38 int option_no_db = 0;
37 39 int option_enable = 0;
38 40 int option_disable = 0;
39 41 int option_debug_related;
40 42 int option_file_output;
41 43 int option_time;
42 44 int option_mem;
43 45 char *option_datadir_str;
44 46 int option_fatal_checks;
45 47 int option_succeed;
48 +int option_timeout = 60;
46 49
47 50 FILE *sm_outfd;
48 51 FILE *sql_outfd;
49 52 FILE *caller_info_fd;
50 53
51 54 int sm_nr_errors;
52 55 int sm_nr_checks;
53 56
54 57 bool __silence_warnings_for_stmt;
55 58
56 59 const char *progname;
57 60
58 61 typedef void (*reg_func) (int id);
59 62 #define CK(_x) {.name = #_x, .func = &_x, .enabled = 0},
60 63 static struct reg_func_info {
61 64 const char *name;
62 65 reg_func func;
63 66 int enabled;
64 67 } reg_funcs[] = {
65 68 {NULL, NULL},
66 69 #include "check_list.h"
67 70 };
68 71 #undef CK
69 72 int num_checks = ARRAY_SIZE(reg_funcs) - 1;
70 73
71 74 const char *check_name(unsigned short id)
72 75 {
73 76 if (id >= ARRAY_SIZE(reg_funcs))
74 77 return "internal";
75 78
76 79 return reg_funcs[id].name;
77 80 }
78 81
79 82 int id_from_name(const char *name)
80 83 {
81 84 int i;
82 85
83 86 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
84 87 if (!strcmp(name, reg_funcs[i].name))
85 88 return i;
86 89 }
87 90 return 0;
88 91 }
89 92
90 93 static void show_checks(void)
91 94 {
92 95 int i;
93 96
94 97 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
95 98 if (!strncmp(reg_funcs[i].name, "check_", 6))
96 99 printf("%3d. %s\n", i, reg_funcs[i].name);
97 100 }
98 101 exit(0);
99 102 }
100 103
101 104 static void enable_disable_checks(char *s, bool enable)
102 105 {
103 106 char buf[128];
104 107 char *next;
105 108 int i;
106 109
107 110 do {
108 111 next = strchr(s, ',');
109 112 if (next) {
110 113 *next = '\0';
111 114 next++;
112 115 }
113 116 if (*s == '\0')
114 117 return;
115 118 if (strncmp(s, "check_", 6) == 0)
116 119 snprintf(buf, sizeof(buf), "%s", s);
117 120 else
118 121 snprintf(buf, sizeof(buf), "check_%s", s);
119 122
120 123
121 124 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
122 125 if (strcmp(reg_funcs[i].name, buf) == 0) {
123 126 reg_funcs[i].enabled = (enable == true) ? 1 : -1;
124 127 break;
125 128 }
126 129 }
127 130
128 131 if (i == ARRAY_SIZE(reg_funcs))
129 132 sm_fatal("'%s' not found", s);
130 133
131 134 } while ((s = next));
132 135 }
133 136
134 137 static void help(void)
135 138 {
136 139 printf("Usage: smatch [smatch arguments][sparse arguments] file.c\n");
137 140 printf("--project=<name> or -p=<name>: project specific tests\n");
138 141 printf("--succeed: don't exit with an error\n");
139 142 printf("--spammy: print superfluous crap.\n");
140 143 printf("--info: print info used to fill smatch_data/.\n");
141 144 printf("--debug: print lots of debug output.\n");
142 145 printf("--param-mapper: enable param_mapper output.\n");
143 146 printf("--no-data: do not use the /smatch_data/ directory.\n");
144 147 printf("--data=<dir>: overwrite path to default smatch data directory.\n");
145 148 printf("--full-path: print the full pathname.\n");
146 149 printf("--debug-implied: print debug output about implications.\n");
147 150 printf("--assume-loops: assume loops always go through at least once.\n");
148 151 printf("--two-passes: use a two pass system for each function.\n");
149 152 printf("--file-output: instead of printing stdout, print to \"file.c.smatch_out\".\n");
150 153 printf("--fatal-checks: check output is treated as an error.\n");
151 154 printf("--help: print this helpful message.\n");
152 155 exit(1);
153 156 }
154 157
155 158 static int match_option(const char *arg, const char *option)
156 159 {
157 160 char *str;
158 161 char *tmp;
159 162 int ret = 0;
160 163
161 164 str = malloc(strlen(option) + 3);
162 165 snprintf(str, strlen(option) + 3, "--%s", option);
163 166 tmp = str;
164 167 while (*tmp) {
165 168 if (*tmp == '_')
↓ open down ↓ |
110 lines elided |
↑ open up ↑ |
166 169 *tmp = '-';
167 170 tmp++;
168 171 }
169 172 if (!strcmp(arg, str))
170 173 ret = 1;
171 174 free(str);
172 175 return ret;
173 176 }
174 177
175 178 #define OPTION(_x) do { \
176 - if (match_option((*argvp)[1], #_x)) { \
179 + if (match_option((*argvp)[i], #_x)) { \
177 180 option_##_x = 1; \
178 181 } \
179 182 } while (0)
180 183
181 184 void parse_args(int *argcp, char ***argvp)
182 185 {
183 186 int i;
184 187
185 188 for (i = 1 ; i < *argcp; i++) {
186 189 if (!strcmp((*argvp)[i], "--help"))
187 190 help();
188 191
189 192 if (!strcmp((*argvp)[i], "--show-checks"))
190 193 show_checks();
191 194
192 195 if (!strncmp((*argvp)[i], "--project=", 10))
193 196 option_project_str = (*argvp)[i] + 10;
194 197
195 198 if (!strncmp((*argvp)[i], "-p=", 3))
196 199 option_project_str = (*argvp)[i] + 3;
197 200
198 201 if (!strncmp((*argvp)[i], "--db-file=", 10))
199 202 option_db_file = (*argvp)[i] + 10;
200 203
201 204 if (!strncmp((*argvp)[i], "--data=", 7))
202 205 option_datadir_str = (*argvp)[i] + 7;
203 206
204 207 if (!strncmp((*argvp)[i], "--debug=", 8))
205 208 option_debug_check = (*argvp)[i] + 8;
206 209
207 210 if (strncmp((*argvp)[i], "--trace=", 8) == 0)
208 211 trace_variable = (*argvp)[i] + 8;
209 212
210 213 if (strncmp((*argvp)[i], "--enable=", 9) == 0) {
↓ open down ↓ |
24 lines elided |
↑ open up ↑ |
211 214 enable_disable_checks((*argvp)[i] + 9, 1);
212 215 option_enable = 1;
213 216 }
214 217
215 218 if (strncmp((*argvp)[i], "--disable=", 10) == 0) {
216 219 enable_disable_checks((*argvp)[i] + 10, 0);
217 220 option_enable = 1;
218 221 option_disable = 1;
219 222 }
220 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 +
221 230 OPTION(fatal_checks);
222 231 OPTION(spammy);
223 232 OPTION(info);
224 233 OPTION(debug);
225 234 OPTION(debug_implied);
226 235 OPTION(debug_related);
227 236 OPTION(assume_loops);
228 237 OPTION(no_data);
229 238 OPTION(two_passes);
230 239 OPTION(full_path);
231 240 OPTION(param_mapper);
232 241 OPTION(call_tree);
233 242 OPTION(file_output);
234 243 OPTION(time);
235 244 OPTION(mem);
236 245 OPTION(no_db);
237 246 OPTION(succeed);
238 247 }
239 248
240 249 if (strcmp(option_project_str, "smatch_generic") != 0)
241 250 option_project = PROJ_UNKNOWN;
242 251
243 252 if (strcmp(option_project_str, "kernel") == 0)
244 253 option_project = PROJ_KERNEL;
245 254 else if (strcmp(option_project_str, "wine") == 0)
246 255 option_project = PROJ_WINE;
247 256 else if (strcmp(option_project_str, "illumos_kernel") == 0)
248 257 option_project = PROJ_ILLUMOS_KERNEL;
249 258 else if (strcmp(option_project_str, "illumos_user") == 0)
250 259 option_project = PROJ_ILLUMOS_USER;
251 260 }
252 261
253 262 static char *read_bin_filename(void)
254 263 {
255 264 char filename[PATH_MAX] = {};
256 265 char proc[PATH_MAX];
257 266
258 267 pid_t pid = getpid();
259 268 sprintf(proc, "/proc/%d/exe", pid);
260 269 if (readlink(proc, filename, PATH_MAX) < 0)
261 270 return NULL;
262 271 return alloc_string(filename);
263 272 }
264 273
265 274 static char *get_bin_dir(char *arg0)
266 275 {
267 276 char *orig;
268 277
269 278 orig = read_bin_filename();
270 279 if (!orig)
271 280 orig = alloc_string(arg0);
272 281 return dirname(orig);
273 282 }
274 283
275 284 static char *get_data_dir(char *arg0)
276 285 {
277 286 char buf[256];
278 287 char *dir;
279 288
280 289 if (option_no_data)
281 290 return NULL;
282 291
283 292 if (option_datadir_str) {
284 293 if (access(option_datadir_str, R_OK))
285 294 sm_warning("%s is not accessible -- ignored.",
286 295 option_datadir_str);
287 296 else
288 297 return alloc_string(option_datadir_str);
289 298 }
290 299
291 300 strncpy(buf, "smatch_data/", sizeof(buf));
292 301 dir = alloc_string(buf);
293 302 if (!access(dir, R_OK))
294 303 return dir;
295 304
296 305 strncpy(buf, bin_dir, 254);
297 306
298 307 buf[255] = '\0';
299 308 strncat(buf, "/smatch_data/", 254 - strlen(buf));
300 309 dir = alloc_string(buf);
301 310 if (!access(dir, R_OK))
302 311 return dir;
303 312 free_string(dir);
304 313 snprintf(buf, 254, "%s/smatch_data/", SMATCHDATADIR);
305 314 dir = alloc_string(buf);
306 315 if (!access(dir, R_OK))
307 316 return dir;
308 317
309 318 sm_warning("%s is not accessible.", dir);
310 319 sm_warning("Use --no-data or --data to suppress this message.");
311 320 return NULL;
312 321 }
313 322
314 323 int main(int argc, char **argv)
315 324 {
316 325 int i;
317 326 reg_func func;
318 327
319 328 sm_outfd = stdout;
320 329 sql_outfd = stdout;
321 330 caller_info_fd = stdout;
322 331
323 332 progname = argv[0];
324 333
325 334 parse_args(&argc, &argv);
326 335
327 336 if (argc < 2)
328 337 help();
329 338
330 339 /* this gets set back to zero when we parse the first function */
331 340 final_pass = 1;
332 341
333 342 bin_dir = get_bin_dir(argv[0]);
334 343 data_dir = get_data_dir(argv[0]);
335 344
336 345 allocate_hook_memory();
337 346 create_function_hook_hash();
338 347 open_smatch_db(option_db_file);
339 348 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
340 349 func = reg_funcs[i].func;
341 350 /* The script IDs start at 1.
342 351 0 is used for internal stuff. */
343 352 if (!option_enable || reg_funcs[i].enabled == 1 ||
344 353 (option_disable && reg_funcs[i].enabled != -1) ||
345 354 strncmp(reg_funcs[i].name, "register_", 9) == 0)
346 355 func(i);
347 356 }
348 357
349 358 smatch(argc, argv);
350 359 free_string(data_dir);
351 360
352 361 if (option_succeed)
353 362 return 0;
354 363 if (sm_nr_errors > 0)
355 364 return 1;
356 365 if (sm_nr_checks > 0 && option_fatal_checks)
357 366 return 1;
358 367 return 0;
359 368 }
↓ open down ↓ |
129 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX