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