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