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_db.c
+++ new/usr/src/tools/smatch/src/smatch_db.c
1 1 /*
2 2 * Copyright (C) 2010 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
18 18 #include <string.h>
19 19 #include <errno.h>
↓ open down ↓ |
19 lines elided |
↑ open up ↑ |
20 20 #include <unistd.h>
21 21 #include <ctype.h>
22 22 #include "smatch.h"
23 23 #include "smatch_slist.h"
24 24 #include "smatch_extra.h"
25 25
26 26 struct sqlite3 *smatch_db;
27 27 struct sqlite3 *mem_db;
28 28 struct sqlite3 *cache_db;
29 29
30 +int debug_db;
31 +
30 32 static int return_id;
31 33
34 +static void call_return_state_hooks(struct expression *expr);
35 +
32 36 #define SQLITE_CACHE_PAGES 1000
33 37
34 38 struct def_callback {
35 39 int hook_type;
36 40 void (*callback)(const char *name, struct symbol *sym, char *key, char *value);
37 41 };
38 42 ALLOCATOR(def_callback, "definition db hook callbacks");
39 43 DECLARE_PTR_LIST(callback_list, struct def_callback);
40 44 static struct callback_list *select_caller_info_callbacks;
41 45
42 46 struct member_info_callback {
43 47 int owner;
44 48 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm);
45 49 };
46 50 ALLOCATOR(member_info_callback, "caller_info callbacks");
47 51 DECLARE_PTR_LIST(member_info_cb_list, struct member_info_callback);
48 52 static struct member_info_cb_list *member_callbacks;
49 53
50 54 struct returned_state_callback {
51 55 void (*callback)(int return_id, char *return_ranges, struct expression *return_expr);
52 56 };
53 57 ALLOCATOR(returned_state_callback, "returned state callbacks");
54 58 DECLARE_PTR_LIST(returned_state_cb_list, struct returned_state_callback);
55 59 static struct returned_state_cb_list *returned_state_callbacks;
56 60
57 61 struct returned_member_callback {
58 62 int owner;
59 63 void (*callback)(int return_id, char *return_ranges, struct expression *expr, char *printed_name, struct smatch_state *state);
60 64 };
61 65 ALLOCATOR(returned_member_callback, "returned member callbacks");
62 66 DECLARE_PTR_LIST(returned_member_cb_list, struct returned_member_callback);
63 67 static struct returned_member_cb_list *returned_member_callbacks;
64 68
65 69 struct db_implies_callback {
66 70 int type;
67 71 void (*callback)(struct expression *call, struct expression *arg, char *key, char *value);
68 72 };
69 73 ALLOCATOR(db_implies_callback, "return_implies callbacks");
70 74 DECLARE_PTR_LIST(db_implies_cb_list, struct db_implies_callback);
71 75 static struct db_implies_cb_list *return_implies_cb_list;
72 76 static struct db_implies_cb_list *call_implies_cb_list;
73 77
74 78 /* silently truncates if needed. */
75 79 char *escape_newlines(const char *str)
76 80 {
77 81 char buf[1024] = "";
78 82 bool found = false;
79 83 int i, j;
80 84
81 85 for (i = 0, j = 0; str[i] != '\0' && j != sizeof(buf); i++, j++) {
82 86 if (str[i] != '\r' && str[i] != '\n') {
83 87 buf[j] = str[i];
84 88 continue;
85 89 }
86 90
87 91 found = true;
88 92 buf[j++] = '\\';
89 93 if (j == sizeof(buf))
90 94 break;
91 95 buf[j] = 'n';
92 96 }
93 97
94 98 if (!found)
95 99 return alloc_sname(str);
96 100
97 101 if (j == sizeof(buf))
↓ open down ↓ |
56 lines elided |
↑ open up ↑ |
98 102 buf[j - 1] = '\0';
99 103 return alloc_sname(buf);
100 104 }
101 105
102 106 static int print_sql_output(void *unused, int argc, char **argv, char **azColName)
103 107 {
104 108 int i;
105 109
106 110 for (i = 0; i < argc; i++) {
107 111 if (i != 0)
108 - printf(", ");
112 + sm_printf(", ");
109 113 sm_printf("%s", argv[i]);
110 114 }
111 115 sm_printf("\n");
112 116 return 0;
113 117 }
114 118
115 119 void sql_exec(struct sqlite3 *db, int (*callback)(void*, int, char**, char**), void *data, const char *sql)
116 120 {
117 121 char *err = NULL;
118 122 int rc;
119 123
120 124 if (!db)
121 125 return;
122 126
123 - if (option_debug) {
127 + if (option_debug || debug_db) {
124 128 sm_msg("%s", sql);
125 129 if (strncasecmp(sql, "select", strlen("select")) == 0)
126 130 sqlite3_exec(db, sql, print_sql_output, NULL, NULL);
127 131 }
128 132
129 133 rc = sqlite3_exec(db, sql, callback, data, &err);
130 134 if (rc != SQLITE_OK && !parse_error) {
131 135 sm_ierror("%s:%d SQL error #2: %s\n", get_filename(), get_lineno(), err);
132 136 sm_ierror("%s:%d SQL: '%s'\n", get_filename(), get_lineno(), sql);
133 137 parse_error = 1;
134 138 }
135 139 }
136 140
137 141 static int replace_count;
138 142 static char **replace_table;
139 143 static const char *replace_return_ranges(const char *return_ranges)
140 144 {
141 145 int i;
142 146
143 147 if (!get_function()) {
144 148 /* I have no idea why EXPORT_SYMBOL() is here */
145 149 return return_ranges;
146 150 }
147 151 for (i = 0; i < replace_count; i += 3) {
148 152 if (strcmp(replace_table[i + 0], get_function()) == 0) {
149 153 if (strcmp(replace_table[i + 1], return_ranges) == 0)
150 154 return replace_table[i + 2];
151 155 }
152 156 }
153 157 return return_ranges;
154 158 }
155 159
156 160
157 161 static char *use_states;
158 162 static int get_db_state_count(void)
159 163 {
160 164 struct sm_state *sm;
161 165 int count = 0;
162 166
163 167 FOR_EACH_SM(__get_cur_stree(), sm) {
164 168 if (sm->owner == USHRT_MAX)
165 169 continue;
166 170 if (use_states[sm->owner])
167 171 count++;
168 172 } END_FOR_EACH_SM(sm);
169 173 return count;
170 174 }
171 175
172 176 void db_ignore_states(int id)
173 177 {
174 178 use_states[id] = 0;
175 179 }
176 180
177 181 void sql_insert_return_states(int return_id, const char *return_ranges,
178 182 int type, int param, const char *key, const char *value)
179 183 {
180 184 if (key && strlen(key) >= 80)
181 185 return;
182 186 return_ranges = replace_return_ranges(return_ranges);
183 187 sql_insert(return_states, "'%s', '%s', %lu, %d, '%s', %d, %d, %d, '%s', '%s'",
184 188 get_base_file(), get_function(), (unsigned long)__inline_fn,
185 189 return_id, return_ranges, fn_static(), type, param, key, value);
186 190 }
187 191
188 192 static struct string_list *common_funcs;
189 193 static int is_common_function(const char *fn)
190 194 {
191 195 char *tmp;
192 196
193 197 if (!fn)
194 198 return 0;
195 199
196 200 if (strncmp(fn, "__builtin_", 10) == 0)
197 201 return 1;
198 202
199 203 FOR_EACH_PTR(common_funcs, tmp) {
200 204 if (strcmp(tmp, fn) == 0)
201 205 return 1;
202 206 } END_FOR_EACH_PTR(tmp);
203 207
204 208 return 0;
205 209 }
206 210
207 211 static char *function_signature(void)
208 212 {
209 213 return type_to_str(get_real_base_type(cur_func_sym));
210 214 }
211 215
212 216 void sql_insert_caller_info(struct expression *call, int type,
213 217 int param, const char *key, const char *value)
214 218 {
215 219 FILE *tmp_fd = sm_outfd;
216 220 char *fn;
217 221
218 222 if (!option_info && !__inline_call)
219 223 return;
220 224
221 225 if (key && strlen(key) >= 80)
222 226 return;
223 227
224 228 fn = get_fnptr_name(call->fn);
225 229 if (!fn)
226 230 return;
227 231
228 232 if (__inline_call) {
229 233 mem_sql(NULL, NULL,
230 234 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
231 235 get_base_file(), get_function(), fn, (unsigned long)call,
232 236 is_static(call->fn), type, param, key, value);
233 237 }
234 238
235 239 if (!option_info)
236 240 return;
237 241
238 242 if (strncmp(fn, "__builtin_", 10) == 0)
239 243 return;
240 244 if (type != INTERNAL && is_common_function(fn))
241 245 return;
242 246
243 247 sm_outfd = caller_info_fd;
244 248 sm_msg("SQL_caller_info: insert into caller_info values ("
245 249 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
246 250 get_base_file(), get_function(), fn, is_static(call->fn),
247 251 type, param, key, value);
248 252 sm_outfd = tmp_fd;
249 253
250 254 free_string(fn);
251 255 }
252 256
253 257 void sql_insert_function_ptr(const char *fn, const char *struct_name)
254 258 {
255 259 sql_insert_or_ignore(function_ptr, "'%s', '%s', '%s', 0",
256 260 get_base_file(), fn, struct_name);
257 261 }
258 262
259 263 void sql_insert_return_implies(int type, int param, const char *key, const char *value)
260 264 {
261 265 sql_insert_or_ignore(return_implies, "'%s', '%s', %lu, %d, %d, %d, '%s', '%s'",
262 266 get_base_file(), get_function(), (unsigned long)__inline_fn,
263 267 fn_static(), type, param, key, value);
264 268 }
265 269
266 270 void sql_insert_call_implies(int type, int param, const char *key, const char *value)
267 271 {
268 272 sql_insert_or_ignore(call_implies, "'%s', '%s', %lu, %d, %d, %d, '%s', '%s'",
269 273 get_base_file(), get_function(), (unsigned long)__inline_fn,
270 274 fn_static(), type, param, key, value);
271 275 }
272 276
273 277 void sql_insert_function_type_size(const char *member, const char *ranges)
274 278 {
275 279 sql_insert(function_type_size, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), member, ranges);
276 280 }
277 281
278 282 void sql_insert_function_type_info(int type, const char *struct_type, const char *member, const char *value)
279 283 {
280 284 sql_insert(function_type_info, "'%s', '%s', %d, '%s', '%s', '%s'", get_base_file(), get_function(), type, struct_type, member, value);
281 285 }
282 286
283 287 void sql_insert_type_info(int type, const char *member, const char *value)
284 288 {
285 289 sql_insert_cache(type_info, "'%s', %d, '%s', '%s'", get_base_file(), type, member, value);
286 290 }
287 291
288 292 void sql_insert_local_values(const char *name, const char *value)
289 293 {
290 294 sql_insert(local_values, "'%s', '%s', '%s'", get_base_file(), name, value);
291 295 }
292 296
293 297 void sql_insert_function_type_value(const char *type, const char *value)
294 298 {
295 299 sql_insert(function_type_value, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), type, value);
296 300 }
297 301
298 302 void sql_insert_function_type(int param, const char *value)
299 303 {
300 304 sql_insert(function_type, "'%s', '%s', %d, %d, '%s'",
301 305 get_base_file(), get_function(), fn_static(), param, value);
302 306 }
303 307
304 308 void sql_insert_parameter_name(int param, const char *value)
305 309 {
306 310 sql_insert(parameter_name, "'%s', '%s', %d, %d, '%s'",
307 311 get_base_file(), get_function(), fn_static(), param, value);
308 312 }
309 313
310 314 void sql_insert_data_info(struct expression *data, int type, const char *value)
311 315 {
312 316 char *data_name;
313 317
314 318 data_name = get_data_info_name(data);
315 319 if (!data_name)
316 320 return;
317 321 sql_insert(data_info, "'%s', '%s', %d, '%s'",
318 322 is_static(data) ? get_base_file() : "extern",
319 323 data_name, type, value);
320 324 }
321 325
322 326 void sql_insert_data_info_var_sym(const char *var, struct symbol *sym, int type, const char *value)
323 327 {
324 328 sql_insert(data_info, "'%s', '%s', %d, '%s'",
325 329 (sym->ctype.modifiers & MOD_STATIC) ? get_base_file() : "extern",
326 330 var, type, value);
327 331 }
328 332
329 333 void sql_save_constraint(const char *con)
330 334 {
331 335 if (!option_info)
332 336 return;
333 337
334 338 sm_msg("SQL: insert or ignore into constraints (str) values('%s');", escape_newlines(con));
335 339 }
336 340
337 341 void sql_save_constraint_required(const char *data, int op, const char *limit)
338 342 {
339 343 sql_insert_or_ignore(constraints_required, "'%s', '%s', '%s'", data, show_special(op), limit);
340 344 }
341 345
342 346 void sql_copy_constraint_required(const char *new_limit, const char *old_limit)
343 347 {
344 348 if (!option_info)
345 349 return;
346 350
347 351 sm_msg("SQL_late: insert or ignore into constraints_required (data, op, bound) "
348 352 "select constraints_required.data, constraints_required.op, '%s' from "
349 353 "constraints_required where bound = '%s';", new_limit, old_limit);
350 354 }
351 355
352 356 void sql_insert_fn_ptr_data_link(const char *ptr, const char *data)
353 357 {
354 358 sql_insert_or_ignore(fn_ptr_data_link, "'%s', '%s'", ptr, data);
355 359 }
356 360
357 361 void sql_insert_fn_data_link(struct expression *fn, int type, int param, const char *key, const char *value)
358 362 {
359 363 if (fn->type != EXPR_SYMBOL || !fn->symbol->ident)
360 364 return;
361 365
362 366 sql_insert(fn_data_link, "'%s', '%s', %d, %d, %d, '%s', '%s'",
363 367 (fn->symbol->ctype.modifiers & MOD_STATIC) ? get_base_file() : "extern",
364 368 fn->symbol->ident->name,
365 369 !!(fn->symbol->ctype.modifiers & MOD_STATIC),
366 370 type, param, key, value);
367 371 }
368 372
369 373 void sql_insert_mtag_about(mtag_t tag, const char *left_name, const char *right_name)
370 374 {
371 375 sql_insert(mtag_about, "%lld, '%s', '%s', %d, '%s', '%s'",
372 376 tag, get_filename(), get_function(), get_lineno(), left_name, right_name);
373 377 }
374 378
375 379 void sql_insert_mtag_map(mtag_t tag, int offset, mtag_t container)
376 380 {
377 381 sql_insert(mtag_map, "%lld, %d, %lld", tag, offset, container);
378 382 }
379 383
380 384 void sql_insert_mtag_alias(mtag_t orig, mtag_t alias)
381 385 {
382 386 sql_insert(mtag_alias, "%lld, %lld", orig, alias);
383 387 }
384 388
385 389 static int save_mtag(void *_tag, int argc, char **argv, char **azColName)
386 390 {
387 391 mtag_t *saved_tag = _tag;
388 392 mtag_t new_tag;
389 393
390 394 new_tag = strtoll(argv[0], NULL, 10);
391 395
392 396 if (!*saved_tag)
393 397 *saved_tag = new_tag;
394 398 else if (*saved_tag != new_tag)
395 399 *saved_tag = -1ULL;
396 400
397 401 return 0;
398 402 }
399 403
400 404 int mtag_map_select_container(mtag_t tag, int offset, mtag_t *container)
401 405 {
402 406 mtag_t tmp = 0;
403 407
404 408 run_sql(save_mtag, &tmp,
405 409 "select container from mtag_map where tag = %lld and offset = %d;",
406 410 tag, offset);
407 411
408 412 if (tmp == 0 || tmp == -1ULL)
409 413 return 0;
410 414 *container = tmp;
411 415 return 1;
412 416 }
413 417
414 418 int mtag_map_select_tag(mtag_t container, int offset, mtag_t *tag)
415 419 {
416 420 mtag_t tmp = 0;
417 421
418 422 run_sql(save_mtag, &tmp,
419 423 "select tag from mtag_map where container = %lld and offset = %d;",
420 424 container, offset);
421 425
422 426 if (tmp == 0 || tmp == -1ULL)
423 427 return 0;
424 428 *tag = tmp;
425 429 return 1;
426 430 }
427 431
428 432 char *get_static_filter(struct symbol *sym)
429 433 {
430 434 static char sql_filter[1024];
431 435
432 436 /* This can only happen on buggy code. Return invalid SQL. */
433 437 if (!sym) {
434 438 sql_filter[0] = '\0';
435 439 return sql_filter;
436 440 }
437 441
438 442 if (sym->ctype.modifiers & MOD_STATIC) {
439 443 snprintf(sql_filter, sizeof(sql_filter),
440 444 "file = '%s' and function = '%s' and static = '1'",
441 445 get_base_file(), sym->ident->name);
442 446 } else {
443 447 snprintf(sql_filter, sizeof(sql_filter),
444 448 "function = '%s' and static = '0'", sym->ident->name);
445 449 }
446 450
447 451 return sql_filter;
448 452 }
449 453
450 454 static int get_row_count(void *_row_count, int argc, char **argv, char **azColName)
451 455 {
452 456 int *row_count = _row_count;
453 457
454 458 *row_count = 0;
455 459 if (argc != 1)
456 460 return 0;
457 461 *row_count = atoi(argv[0]);
458 462 return 0;
459 463 }
460 464
461 465 static void mark_call_params_untracked(struct expression *call)
462 466 {
463 467 struct expression *arg;
464 468 int i = 0;
465 469
466 470 FOR_EACH_PTR(call->args, arg) {
467 471 mark_untracked(call, i++, "$", NULL);
468 472 } END_FOR_EACH_PTR(arg);
469 473 }
470 474
471 475 static void sql_select_return_states_pointer(const char *cols,
472 476 struct expression *call, int (*callback)(void*, int, char**, char**), void *info)
473 477 {
474 478 char *ptr;
475 479 int return_count = 0;
476 480
477 481 ptr = get_fnptr_name(call->fn);
478 482 if (!ptr)
479 483 return;
480 484
481 485 run_sql(get_row_count, &return_count,
482 486 "select count(*) from return_states join function_ptr "
483 487 "where return_states.function == function_ptr.function and "
484 488 "ptr = '%s' and searchable = 1 and type = %d;", ptr, INTERNAL);
485 489 /* The magic number 100 is just from testing on the kernel. */
486 490 if (return_count > 100) {
487 491 mark_call_params_untracked(call);
488 492 return;
489 493 }
490 494
491 495 run_sql(callback, info,
492 496 "select %s from return_states join function_ptr where "
493 497 "return_states.function == function_ptr.function and ptr = '%s' "
494 498 "and searchable = 1 "
495 499 "order by function_ptr.file, return_states.file, return_id, type;",
496 500 cols, ptr);
497 501 }
498 502
499 503 static int is_local_symbol(struct expression *expr)
500 504 {
501 505 if (expr->type != EXPR_SYMBOL)
502 506 return 0;
503 507 if (expr->symbol->ctype.modifiers & (MOD_NONLOCAL | MOD_STATIC | MOD_ADDRESSABLE))
504 508 return 0;
505 509 return 1;
506 510 }
507 511
508 512 void sql_select_return_states(const char *cols, struct expression *call,
509 513 int (*callback)(void*, int, char**, char**), void *info)
510 514 {
511 515 struct expression *fn;
512 516 int row_count = 0;
513 517
514 518 if (is_fake_call(call))
515 519 return;
516 520
517 521 fn = strip_expr(call->fn);
518 522 if (fn->type != EXPR_SYMBOL || !fn->symbol || is_local_symbol(fn)) {
519 523 sql_select_return_states_pointer(cols, call, callback, info);
520 524 return;
521 525 }
522 526
523 527 if (inlinable(fn)) {
524 528 mem_sql(callback, info,
525 529 "select %s from return_states where call_id = '%lu' order by return_id, type;",
526 530 cols, (unsigned long)call);
527 531 return;
528 532 }
529 533
530 534 run_sql(get_row_count, &row_count, "select count(*) from return_states where %s;",
531 535 get_static_filter(fn->symbol));
532 536 if (row_count > 3000)
533 537 return;
534 538
535 539 run_sql(callback, info, "select %s from return_states where %s order by file, return_id, type;",
536 540 cols, get_static_filter(fn->symbol));
537 541 }
538 542
539 543 #define CALL_IMPLIES 0
540 544 #define RETURN_IMPLIES 1
541 545
542 546 struct implies_info {
543 547 int type;
544 548 struct db_implies_cb_list *cb_list;
545 549 struct expression *expr;
546 550 struct symbol *sym;
547 551 };
548 552
549 553 void sql_select_implies(const char *cols, struct implies_info *info,
550 554 int (*callback)(void*, int, char**, char**))
551 555 {
552 556 if (info->type == RETURN_IMPLIES && inlinable(info->expr->fn)) {
553 557 mem_sql(callback, info,
554 558 "select %s from return_implies where call_id = '%lu';",
555 559 cols, (unsigned long)info->expr);
556 560 return;
557 561 }
558 562
559 563 run_sql(callback, info, "select %s from %s_implies where %s;",
560 564 cols,
561 565 info->type == CALL_IMPLIES ? "call" : "return",
562 566 get_static_filter(info->sym));
563 567 }
564 568
565 569 struct select_caller_info_data {
566 570 struct stree *final_states;
567 571 struct timeval start_time;
568 572 int prev_func_id;
569 573 int ignore;
570 574 int results;
571 575 };
572 576
573 577 static int caller_info_callback(void *_data, int argc, char **argv, char **azColName);
574 578
575 579 static void sql_select_caller_info(struct select_caller_info_data *data,
576 580 const char *cols, struct symbol *sym)
577 581 {
578 582 if (__inline_fn) {
579 583 mem_sql(caller_info_callback, data,
580 584 "select %s from caller_info where call_id = %lu;",
581 585 cols, (unsigned long)__inline_fn);
582 586 return;
583 587 }
584 588
585 589 if (sym->ident->name && is_common_function(sym->ident->name))
586 590 return;
587 591 run_sql(caller_info_callback, data,
588 592 "select %s from common_caller_info where %s order by call_id;",
589 593 cols, get_static_filter(sym));
590 594 if (data->results)
591 595 return;
592 596
593 597 run_sql(caller_info_callback, data,
594 598 "select %s from caller_info where %s order by call_id;",
595 599 cols, get_static_filter(sym));
596 600 }
597 601
598 602 void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
599 603 {
600 604 struct def_callback *def_callback = __alloc_def_callback(0);
601 605
602 606 def_callback->hook_type = type;
603 607 def_callback->callback = callback;
604 608 add_ptr_list(&select_caller_info_callbacks, def_callback);
605 609 }
606 610
607 611 /*
608 612 * These call backs are used when the --info option is turned on to print struct
609 613 * member information. For example foo->bar could have a state in
610 614 * smatch_extra.c and also check_user.c.
611 615 */
612 616 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
613 617 {
614 618 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
615 619
616 620 member_callback->owner = owner;
617 621 member_callback->callback = callback;
618 622 add_ptr_list(&member_callbacks, member_callback);
619 623 }
620 624
621 625 void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr))
622 626 {
623 627 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
624 628
625 629 callback->callback = fn;
626 630 add_ptr_list(&returned_state_callbacks, callback);
627 631 }
628 632
629 633 void add_returned_member_callback(int owner, void (*callback)(int return_id, char *return_ranges, struct expression *expr, char *printed_name, struct smatch_state *state))
630 634 {
631 635 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
632 636
633 637 member_callback->owner = owner;
634 638 member_callback->callback = callback;
635 639 add_ptr_list(&returned_member_callbacks, member_callback);
636 640 }
637 641
638 642 void select_call_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value))
639 643 {
640 644 struct db_implies_callback *cb = __alloc_db_implies_callback(0);
641 645
642 646 cb->type = type;
643 647 cb->callback = callback;
644 648 add_ptr_list(&call_implies_cb_list, cb);
645 649 }
646 650
647 651 void select_return_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value))
648 652 {
649 653 struct db_implies_callback *cb = __alloc_db_implies_callback(0);
650 654
651 655 cb->type = type;
652 656 cb->callback = callback;
653 657 add_ptr_list(&return_implies_cb_list, cb);
654 658 }
655 659
656 660 struct return_info {
657 661 struct expression *static_returns_call;
658 662 struct symbol *return_type;
659 663 struct range_list *return_range_list;
660 664 };
661 665
662 666 static int db_return_callback(void *_ret_info, int argc, char **argv, char **azColName)
663 667 {
664 668 struct return_info *ret_info = _ret_info;
665 669 struct range_list *rl;
666 670 struct expression *call_expr = ret_info->static_returns_call;
667 671
668 672 if (argc != 1)
669 673 return 0;
670 674 call_results_to_rl(call_expr, ret_info->return_type, argv[0], &rl);
671 675 ret_info->return_range_list = rl_union(ret_info->return_range_list, rl);
672 676 return 0;
673 677 }
674 678
675 679 struct range_list *db_return_vals(struct expression *expr)
676 680 {
677 681 struct return_info ret_info = {};
678 682 char buf[64];
679 683 struct sm_state *sm;
680 684
681 685 if (is_fake_call(expr))
682 686 return NULL;
683 687
684 688 snprintf(buf, sizeof(buf), "return %p", expr);
685 689 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
686 690 if (sm)
687 691 return clone_rl(estate_rl(sm->state));
688 692 ret_info.static_returns_call = expr;
689 693 ret_info.return_type = get_type(expr);
690 694 if (!ret_info.return_type)
691 695 return NULL;
692 696
693 697 if (expr->fn->type != EXPR_SYMBOL || !expr->fn->symbol)
694 698 return NULL;
695 699
696 700 ret_info.return_range_list = NULL;
697 701 if (inlinable(expr->fn)) {
698 702 mem_sql(db_return_callback, &ret_info,
699 703 "select distinct return from return_states where call_id = '%lu';",
700 704 (unsigned long)expr);
701 705 } else {
702 706 run_sql(db_return_callback, &ret_info,
703 707 "select distinct return from return_states where %s;",
704 708 get_static_filter(expr->fn->symbol));
705 709 }
706 710 return ret_info.return_range_list;
707 711 }
708 712
709 713 struct range_list *db_return_vals_from_str(const char *fn_name)
710 714 {
711 715 struct return_info ret_info;
712 716
713 717 ret_info.static_returns_call = NULL;
714 718 ret_info.return_type = &llong_ctype;
715 719 ret_info.return_range_list = NULL;
716 720
717 721 run_sql(db_return_callback, &ret_info,
718 722 "select distinct return from return_states where function = '%s';",
719 723 fn_name);
720 724 return ret_info.return_range_list;
721 725 }
722 726
723 727 /*
724 728 * This is used when we have a function that takes a function pointer as a
725 729 * parameter. "frob(blah, blah, my_function);" We know that the return values
726 730 * from frob() come from my_funcion() so we want to find the possible returns
727 731 * of my_function(), but we don't know which arguments are passed to it.
728 732 *
729 733 */
730 734 struct range_list *db_return_vals_no_args(struct expression *expr)
731 735 {
732 736 struct return_info ret_info = {};
733 737
734 738 if (!expr || expr->type != EXPR_SYMBOL)
735 739 return NULL;
736 740
737 741 ret_info.static_returns_call = expr;
738 742 ret_info.return_type = get_type(expr);
739 743 ret_info.return_type = get_real_base_type(ret_info.return_type);
740 744 if (!ret_info.return_type)
741 745 return NULL;
742 746
743 747 run_sql(db_return_callback, &ret_info,
744 748 "select distinct return from return_states where %s;",
745 749 get_static_filter(expr->symbol));
746 750
747 751 return ret_info.return_range_list;
748 752 }
749 753
750 754 static void match_call_marker(struct expression *expr)
751 755 {
752 756 struct symbol *type;
753 757
754 758 type = get_type(expr->fn);
755 759 if (type && type->type == SYM_PTR)
756 760 type = get_real_base_type(type);
757 761
758 762 /*
759 763 * we just want to record something in the database so that if we have
760 764 * two calls like: frob(4); frob(some_unkown); then on the receiving
761 765 * side we know that sometimes frob is called with unknown parameters.
762 766 */
763 767
764 768 sql_insert_caller_info(expr, INTERNAL, -1, "%call_marker%", type_to_str(type));
765 769 }
766 770
767 771 static char *show_offset(int offset)
768 772 {
769 773 static char buf[64];
770 774
771 775 buf[0] = '\0';
772 776 if (offset != -1)
773 777 snprintf(buf, sizeof(buf), "(-%d)", offset);
774 778 return buf;
775 779 }
776 780
777 781 int is_recursive_member(const char *name)
778 782 {
779 783 char buf[256];
780 784 const char *p, *next;
781 785 int size;
782 786
783 787 p = strchr(name, '>');
784 788 if (!p)
785 789 return 0;
786 790 p++;
787 791 while (true) {
788 792 next = strchr(p, '>');
789 793 if (!next)
790 794 return 0;
791 795 next++;
792 796
793 797 size = next - p;
↓ open down ↓ |
660 lines elided |
↑ open up ↑ |
794 798 if (size >= sizeof(buf))
795 799 return 0;
796 800 memcpy(buf, p, size);
797 801 buf[size] = '\0';
798 802 if (strstr(next, buf))
799 803 return 1;
800 804 p = next;
801 805 }
802 806 }
803 807
808 +char *sm_to_arg_name(struct expression *expr, struct sm_state *sm)
809 +{
810 + struct symbol *sym;
811 + const char *sm_name;
812 + char *name;
813 + bool is_address = false;
814 + bool add_star = false;
815 + char buf[256];
816 + char *ret = NULL;
817 + int len;
818 +
819 + expr = strip_expr(expr);
820 + if (!expr)
821 + return NULL;
822 +
823 + if (expr->type == EXPR_PREOP && expr->op == '&') {
824 + expr = strip_expr(expr->unop);
825 + is_address = true;
826 + }
827 +
828 + name = expr_to_var_sym(expr, &sym);
829 + if (!name || !sym)
830 + goto free;
831 + if (sym != sm->sym)
832 + goto free;
833 +
834 + sm_name = sm->name;
835 + add_star = false;
836 + if (sm_name[0] == '*') {
837 + add_star = true;
838 + sm_name++;
839 + }
840 +
841 + len = strlen(name);
842 + if (strncmp(name, sm_name, len) != 0)
843 + goto free;
844 + if (sm_name[len] == '\0') {
845 + snprintf(buf, sizeof(buf), "%s%s$",
846 + add_star ? "*" : "", is_address ? "*" : "");
847 + } else {
848 + if (sm_name[len] != '.' && sm_name[len] != '-')
849 + goto free;
850 + if (sm_name[len] == '-')
851 + len++;
852 + // FIXME does is_address really imply that sm_name[len] == '-'
853 + snprintf(buf, sizeof(buf), "%s$->%s", add_star ? "*" : "",
854 + sm_name + len);
855 + }
856 +
857 + ret = alloc_sname(buf);
858 +free:
859 + free_string(name);
860 + return ret;
861 +}
862 +
804 863 static void print_struct_members(struct expression *call, struct expression *expr, int param, int offset, struct stree *stree,
805 864 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
806 865 {
807 866 struct sm_state *sm;
808 867 const char *sm_name;
809 868 char *name;
810 869 struct symbol *sym;
811 870 int len;
812 871 char printed_name[256];
813 872 int is_address = 0;
814 873 bool add_star;
815 874 struct symbol *type;
816 875
817 876 expr = strip_expr(expr);
818 877 if (!expr)
819 878 return;
820 879 type = get_type(expr);
821 880 if (type && type_bits(type) < type_bits(&ulong_ctype))
822 881 return;
823 882
824 883 if (expr->type == EXPR_PREOP && expr->op == '&') {
825 884 expr = strip_expr(expr->unop);
826 885 is_address = 1;
827 886 }
828 887
829 888 name = expr_to_var_sym(expr, &sym);
830 889 if (!name || !sym)
831 890 goto free;
832 891
833 892 len = strlen(name);
834 893 FOR_EACH_SM(stree, sm) {
835 894 if (sm->sym != sym)
836 895 continue;
837 896 sm_name = sm->name;
838 897 add_star = false;
839 898 if (sm_name[0] == '*') {
840 899 add_star = true;
841 900 sm_name++;
842 901 }
843 902 // FIXME: simplify?
844 903 if (!add_star && strcmp(name, sm_name) == 0) {
845 904 if (is_address)
846 905 snprintf(printed_name, sizeof(printed_name), "*$%s", show_offset(offset));
847 906 else /* these are already handled. fixme: handle them here */
848 907 continue;
849 908 } else if (add_star && strcmp(name, sm_name) == 0) {
850 909 snprintf(printed_name, sizeof(printed_name), "%s*$%s",
851 910 is_address ? "*" : "", show_offset(offset));
852 911 } else if (strncmp(name, sm_name, len) == 0) {
853 912 if (sm_name[len] != '.' && sm_name[len] != '-')
854 913 continue;
855 914 if (is_address)
856 915 snprintf(printed_name, sizeof(printed_name),
857 916 "%s$%s->%s", add_star ? "*" : "",
858 917 show_offset(offset), sm_name + len + 1);
859 918 else
860 919 snprintf(printed_name, sizeof(printed_name),
861 920 "%s$%s%s", add_star ? "*" : "",
862 921 show_offset(offset), sm_name + len);
863 922 } else {
864 923 continue;
865 924 }
866 925 if (is_recursive_member(printed_name))
867 926 continue;
868 927 callback(call, param, printed_name, sm);
869 928 } END_FOR_EACH_SM(sm);
870 929 free:
871 930 free_string(name);
872 931 }
873 932
874 933 static int param_used_callback(void *_container, int argc, char **argv, char **azColName)
875 934 {
876 935 char **container = _container;
877 936 static char buf[256];
878 937
879 938 snprintf(buf, sizeof(buf), "%s", argv[0]);
880 939 *container = buf;
881 940 return 0;
882 941 }
883 942
884 943 static void print_container_struct_members(struct expression *call, struct expression *expr, int param, struct stree *stree,
885 944 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
886 945 {
887 946 struct expression *tmp;
888 947 char *container = NULL;
889 948 int offset;
890 949 int holder_offset;
891 950 char *p;
892 951
893 952 if (!call->fn || call->fn->type != EXPR_SYMBOL || !call->fn->symbol)
894 953 return;
895 954
896 955 /*
897 956 * We can't use the in-mem DB because we have to parse the function
898 957 * first, then we know if it takes a container, then we know to pass it
899 958 * the container data.
900 959 *
901 960 */
902 961 run_sql(¶m_used_callback, &container,
903 962 "select key from return_implies where %s and type = %d and key like '%%$(%%' and parameter = %d limit 1;",
904 963 get_static_filter(call->fn->symbol), CONTAINER, param);
905 964 if (!container)
906 965 return;
907 966
908 967 p = strchr(container, '-');
909 968 if (!p)
910 969 return;
911 970 offset = atoi(p);
912 971 p = strchr(p, ')');
913 972 if (!p)
914 973 return;
915 974 p++;
916 975
917 976 tmp = get_assigned_expr(expr);
918 977 if (tmp)
919 978 expr = tmp;
920 979
921 980 if (expr->type != EXPR_PREOP || expr->op != '&')
922 981 return;
923 982 expr = strip_expr(expr->unop);
924 983 holder_offset = get_member_offset_from_deref(expr);
925 984 if (-holder_offset != offset)
926 985 return;
927 986
928 987 expr = strip_expr(expr->deref);
929 988 if (expr->type == EXPR_PREOP && expr->op == '*')
930 989 expr = strip_expr(expr->unop);
931 990
932 991 print_struct_members(call, expr, param, holder_offset, stree, callback);
933 992 }
934 993
935 994 static void match_call_info(struct expression *call)
936 995 {
937 996 struct member_info_callback *cb;
938 997 struct expression *arg;
939 998 struct stree *stree;
940 999 char *name;
941 1000 int i;
942 1001
943 1002 name = get_fnptr_name(call->fn);
944 1003 if (!name)
945 1004 return;
946 1005
947 1006 FOR_EACH_PTR(member_callbacks, cb) {
948 1007 stree = get_all_states_stree(cb->owner);
949 1008 i = 0;
950 1009 FOR_EACH_PTR(call->args, arg) {
951 1010 print_struct_members(call, arg, i, -1, stree, cb->callback);
952 1011 print_container_struct_members(call, arg, i, stree, cb->callback);
953 1012 i++;
954 1013 } END_FOR_EACH_PTR(arg);
955 1014 free_stree(&stree);
956 1015 } END_FOR_EACH_PTR(cb);
957 1016
958 1017 free_string(name);
959 1018 }
960 1019
961 1020 static int get_param(int param, char **name, struct symbol **sym)
962 1021 {
963 1022 struct symbol *arg;
964 1023 int i;
965 1024
966 1025 i = 0;
967 1026 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
968 1027 /*
969 1028 * this is a temporary hack to work around a bug (I think in sparse?)
970 1029 * 2.6.37-rc1:fs/reiserfs/journal.o
971 1030 * If there is a function definition without parameter name found
972 1031 * after a function implementation then it causes a crash.
973 1032 * int foo() {}
974 1033 * int bar(char *);
975 1034 */
976 1035 if (arg->ident->name < (char *)100)
977 1036 continue;
978 1037 if (i == param) {
979 1038 *name = arg->ident->name;
980 1039 *sym = arg;
981 1040 return TRUE;
982 1041 }
983 1042 i++;
984 1043 } END_FOR_EACH_PTR(arg);
985 1044
986 1045 return FALSE;
987 1046 }
988 1047
989 1048 static int function_signature_matches(const char *sig)
990 1049 {
991 1050 char *my_sig;
992 1051
993 1052 my_sig = function_signature();
994 1053 if (!sig || !my_sig)
995 1054 return 1; /* default to matching */
996 1055 if (strcmp(my_sig, sig) == 0)
997 1056 return 1;
998 1057 return 0;
999 1058 }
1000 1059
1001 1060 static int caller_info_callback(void *_data, int argc, char **argv, char **azColName)
1002 1061 {
1003 1062 struct select_caller_info_data *data = _data;
1004 1063 int func_id;
1005 1064 long type;
1006 1065 long param;
1007 1066 char *key;
1008 1067 char *value;
1009 1068 char *name = NULL;
1010 1069 struct symbol *sym = NULL;
1011 1070 struct def_callback *def_callback;
1012 1071 struct stree *stree;
1013 1072 struct timeval cur_time;
1014 1073
1015 1074 data->results = 1;
1016 1075
1017 1076 if (argc != 5)
1018 1077 return 0;
1019 1078
1020 1079 gettimeofday(&cur_time, NULL);
1021 1080 if (cur_time.tv_sec - data->start_time.tv_sec > 10)
1022 1081 return 0;
1023 1082
1024 1083 func_id = atoi(argv[0]);
1025 1084 errno = 0;
1026 1085 type = strtol(argv[1], NULL, 10);
1027 1086 param = strtol(argv[2], NULL, 10);
1028 1087 if (errno)
1029 1088 return 0;
1030 1089 key = argv[3];
1031 1090 value = argv[4];
1032 1091
1033 1092 if (data->prev_func_id == -1)
1034 1093 data->prev_func_id = func_id;
1035 1094 if (func_id != data->prev_func_id) {
1036 1095 stree = __pop_fake_cur_stree();
1037 1096 if (!data->ignore)
1038 1097 merge_stree(&data->final_states, stree);
1039 1098 free_stree(&stree);
1040 1099 __push_fake_cur_stree();
1041 1100 __unnullify_path();
1042 1101 data->prev_func_id = func_id;
1043 1102 data->ignore = 0;
1044 1103 }
1045 1104
1046 1105 if (data->ignore)
1047 1106 return 0;
1048 1107 if (type == INTERNAL &&
1049 1108 !function_signature_matches(value)) {
1050 1109 data->ignore = 1;
1051 1110 return 0;
1052 1111 }
1053 1112
1054 1113 if (param >= 0 && !get_param(param, &name, &sym))
1055 1114 return 0;
1056 1115
1057 1116 FOR_EACH_PTR(select_caller_info_callbacks, def_callback) {
1058 1117 if (def_callback->hook_type == type)
1059 1118 def_callback->callback(name, sym, key, value);
1060 1119 } END_FOR_EACH_PTR(def_callback);
1061 1120
1062 1121 return 0;
1063 1122 }
1064 1123
1065 1124 static struct string_list *ptr_names_done;
1066 1125 static struct string_list *ptr_names;
1067 1126
1068 1127 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
1069 1128 {
1070 1129 insert_string(&ptr_names, alloc_string(argv[0]));
1071 1130 return 0;
1072 1131 }
1073 1132
1074 1133 static char *get_next_ptr_name(void)
1075 1134 {
1076 1135 char *ptr;
1077 1136
1078 1137 FOR_EACH_PTR(ptr_names, ptr) {
1079 1138 if (!insert_string(&ptr_names_done, ptr))
1080 1139 continue;
1081 1140 return ptr;
1082 1141 } END_FOR_EACH_PTR(ptr);
1083 1142 return NULL;
1084 1143 }
1085 1144
1086 1145 static void get_ptr_names(const char *file, const char *name)
1087 1146 {
1088 1147 char sql_filter[1024];
1089 1148 int before, after;
1090 1149
1091 1150 if (file) {
1092 1151 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
1093 1152 file, name);
1094 1153 } else {
1095 1154 snprintf(sql_filter, 1024, "function = '%s';", name);
1096 1155 }
1097 1156
1098 1157 before = ptr_list_size((struct ptr_list *)ptr_names);
1099 1158
1100 1159 run_sql(get_ptr_name, NULL,
1101 1160 "select distinct ptr from function_ptr where %s",
1102 1161 sql_filter);
1103 1162
1104 1163 after = ptr_list_size((struct ptr_list *)ptr_names);
1105 1164 if (before == after)
1106 1165 return;
1107 1166
1108 1167 while ((name = get_next_ptr_name()))
1109 1168 get_ptr_names(NULL, name);
1110 1169 }
1111 1170
1112 1171 static void match_data_from_db(struct symbol *sym)
1113 1172 {
1114 1173 struct select_caller_info_data data = { .prev_func_id = -1 };
1115 1174 struct sm_state *sm;
1116 1175 struct stree *stree;
1117 1176 struct timeval end_time;
1118 1177
1119 1178 if (!sym || !sym->ident)
1120 1179 return;
1121 1180
1122 1181 gettimeofday(&data.start_time, NULL);
1123 1182
1124 1183 __push_fake_cur_stree();
1125 1184 __unnullify_path();
1126 1185
1127 1186 if (!__inline_fn) {
1128 1187 char *ptr;
1129 1188
1130 1189 if (sym->ctype.modifiers & MOD_STATIC)
1131 1190 get_ptr_names(get_base_file(), sym->ident->name);
1132 1191 else
1133 1192 get_ptr_names(NULL, sym->ident->name);
1134 1193
1135 1194 if (ptr_list_size((struct ptr_list *)ptr_names) > 20) {
1136 1195 __free_ptr_list((struct ptr_list **)&ptr_names);
1137 1196 __free_ptr_list((struct ptr_list **)&ptr_names_done);
1138 1197 __free_fake_cur_stree();
1139 1198 return;
1140 1199 }
1141 1200
1142 1201 sql_select_caller_info(&data,
1143 1202 "call_id, type, parameter, key, value",
1144 1203 sym);
1145 1204
1146 1205
1147 1206 stree = __pop_fake_cur_stree();
1148 1207 if (!data.ignore)
1149 1208 merge_stree(&data.final_states, stree);
1150 1209 free_stree(&stree);
1151 1210 __push_fake_cur_stree();
1152 1211 __unnullify_path();
1153 1212 data.prev_func_id = -1;
1154 1213 data.ignore = 0;
1155 1214 data.results = 0;
1156 1215
1157 1216 FOR_EACH_PTR(ptr_names, ptr) {
1158 1217 run_sql(caller_info_callback, &data,
1159 1218 "select call_id, type, parameter, key, value"
1160 1219 " from common_caller_info where function = '%s' order by call_id",
1161 1220 ptr);
1162 1221 } END_FOR_EACH_PTR(ptr);
1163 1222
1164 1223 if (data.results) {
1165 1224 FOR_EACH_PTR(ptr_names, ptr) {
1166 1225 free_string(ptr);
1167 1226 } END_FOR_EACH_PTR(ptr);
1168 1227 goto free_ptr_names;
1169 1228 }
1170 1229
1171 1230 FOR_EACH_PTR(ptr_names, ptr) {
1172 1231 run_sql(caller_info_callback, &data,
1173 1232 "select call_id, type, parameter, key, value"
1174 1233 " from caller_info where function = '%s' order by call_id",
1175 1234 ptr);
1176 1235 free_string(ptr);
1177 1236 } END_FOR_EACH_PTR(ptr);
1178 1237
1179 1238 free_ptr_names:
1180 1239 __free_ptr_list((struct ptr_list **)&ptr_names);
1181 1240 __free_ptr_list((struct ptr_list **)&ptr_names_done);
1182 1241 } else {
1183 1242 sql_select_caller_info(&data,
1184 1243 "call_id, type, parameter, key, value",
1185 1244 sym);
1186 1245 }
1187 1246
1188 1247 stree = __pop_fake_cur_stree();
1189 1248 if (!data.ignore)
1190 1249 merge_stree(&data.final_states, stree);
1191 1250 free_stree(&stree);
1192 1251
1193 1252 gettimeofday(&end_time, NULL);
1194 1253 if (end_time.tv_sec - data.start_time.tv_sec <= 10) {
1195 1254 FOR_EACH_SM(data.final_states, sm) {
1196 1255 __set_sm(sm);
1197 1256 } END_FOR_EACH_SM(sm);
1198 1257 }
1199 1258
1200 1259 free_stree(&data.final_states);
1201 1260 }
1202 1261
1203 1262 static int return_implies_callbacks(void *_info, int argc, char **argv, char **azColName)
1204 1263 {
1205 1264 struct implies_info *info = _info;
1206 1265 struct db_implies_callback *cb;
1207 1266 struct expression *arg = NULL;
1208 1267 int type;
1209 1268 int param;
1210 1269
1211 1270 if (argc != 5)
1212 1271 return 0;
1213 1272
1214 1273 type = atoi(argv[1]);
1215 1274 param = atoi(argv[2]);
1216 1275
1217 1276 FOR_EACH_PTR(info->cb_list, cb) {
1218 1277 if (cb->type != type)
1219 1278 continue;
1220 1279 if (param != -1) {
1221 1280 arg = get_argument_from_call_expr(info->expr->args, param);
1222 1281 if (!arg)
1223 1282 continue;
1224 1283 }
1225 1284 cb->callback(info->expr, arg, argv[3], argv[4]);
1226 1285 } END_FOR_EACH_PTR(cb);
1227 1286
1228 1287 return 0;
1229 1288 }
1230 1289
1231 1290 static int call_implies_callbacks(void *_info, int argc, char **argv, char **azColName)
1232 1291 {
1233 1292 struct implies_info *info = _info;
1234 1293 struct db_implies_callback *cb;
1235 1294 struct expression *arg;
1236 1295 struct symbol *sym;
1237 1296 char *name;
1238 1297 int type;
1239 1298 int param;
1240 1299
1241 1300 if (argc != 5)
1242 1301 return 0;
1243 1302
1244 1303 type = atoi(argv[1]);
1245 1304 param = atoi(argv[2]);
1246 1305
1247 1306 if (!get_param(param, &name, &sym))
1248 1307 return 0;
1249 1308 arg = symbol_expression(sym);
1250 1309 if (!arg)
1251 1310 return 0;
1252 1311
1253 1312 FOR_EACH_PTR(info->cb_list, cb) {
1254 1313 if (cb->type != type)
1255 1314 continue;
1256 1315 cb->callback(info->expr, arg, argv[3], argv[4]);
1257 1316 } END_FOR_EACH_PTR(cb);
1258 1317
1259 1318 return 0;
1260 1319 }
1261 1320
1262 1321 static void match_return_implies(struct expression *expr)
1263 1322 {
1264 1323 struct implies_info info = {
1265 1324 .type = RETURN_IMPLIES,
1266 1325 .cb_list = return_implies_cb_list,
1267 1326 };
1268 1327
1269 1328 if (expr->fn->type != EXPR_SYMBOL ||
1270 1329 !expr->fn->symbol)
1271 1330 return;
1272 1331 info.expr = expr;
1273 1332 info.sym = expr->fn->symbol;
1274 1333 sql_select_implies("function, type, parameter, key, value", &info,
1275 1334 return_implies_callbacks);
1276 1335 }
1277 1336
1278 1337 static void match_call_implies(struct symbol *sym)
1279 1338 {
1280 1339 struct implies_info info = {
1281 1340 .type = CALL_IMPLIES,
1282 1341 .cb_list = call_implies_cb_list,
1283 1342 };
1284 1343
1285 1344 if (!sym || !sym->ident)
1286 1345 return;
1287 1346
1288 1347 info.sym = sym;
1289 1348 sql_select_implies("function, type, parameter, key, value", &info,
1290 1349 call_implies_callbacks);
1291 1350 }
1292 1351
1293 1352 static char *get_fn_param_str(struct expression *expr)
1294 1353 {
1295 1354 struct expression *tmp;
1296 1355 int param;
1297 1356 char buf[32];
1298 1357
1299 1358 tmp = get_assigned_expr(expr);
1300 1359 if (tmp)
1301 1360 expr = tmp;
1302 1361 expr = strip_expr(expr);
1303 1362 if (!expr || expr->type != EXPR_CALL)
1304 1363 return NULL;
1305 1364 expr = strip_expr(expr->fn);
1306 1365 if (!expr || expr->type != EXPR_SYMBOL)
1307 1366 return NULL;
1308 1367 param = get_param_num(expr);
1309 1368 if (param < 0)
1310 1369 return NULL;
1311 1370
1312 1371 snprintf(buf, sizeof(buf), "[r $%d]", param);
1313 1372 return alloc_sname(buf);
1314 1373 }
1315 1374
1316 1375 static char *get_return_compare_is_param(struct expression *expr)
1317 1376 {
1318 1377 char *var;
1319 1378 char buf[256];
1320 1379 int comparison;
1321 1380 int param;
1322 1381
1323 1382 param = get_param_num(expr);
1324 1383 if (param < 0)
1325 1384 return NULL;
1326 1385
1327 1386 var = expr_to_var(expr);
1328 1387 if (!var)
1329 1388 return NULL;
1330 1389 snprintf(buf, sizeof(buf), "%s orig", var);
1331 1390 comparison = get_comparison_strings(var, buf);
1332 1391 free_string(var);
1333 1392
1334 1393 if (!comparison)
1335 1394 return NULL;
1336 1395
1337 1396 snprintf(buf, sizeof(buf), "[%s$%d]", show_special(comparison), param);
1338 1397 return alloc_sname(buf);
1339 1398 }
1340 1399
1341 1400 static char *get_return_compare_str(struct expression *expr)
1342 1401 {
1343 1402 char *compare_str;
1344 1403
1345 1404 compare_str = get_return_compare_is_param(expr);
1346 1405 if (compare_str)
1347 1406 return compare_str;
1348 1407
1349 1408 compare_str = expr_lte_to_param(expr, -1);
1350 1409 if (compare_str)
1351 1410 return compare_str;
1352 1411
1353 1412 return expr_param_comparison(expr, -1);
1354 1413 }
1355 1414
1356 1415 static const char *get_return_ranges_str(struct expression *expr, struct range_list **rl_p)
1357 1416 {
1358 1417 struct range_list *rl;
1359 1418 char *return_ranges;
1360 1419 sval_t sval;
1361 1420 char *fn_param_str;
1362 1421 char *compare_str;
1363 1422 char *math_str;
1364 1423 char buf[128];
1365 1424
1366 1425 *rl_p = NULL;
1367 1426
1368 1427 if (!expr)
1369 1428 return alloc_sname("");
1370 1429
1371 1430 if (get_implied_value(expr, &sval)) {
1372 1431 sval = sval_cast(cur_func_return_type(), sval);
1373 1432 *rl_p = alloc_rl(sval, sval);
1374 1433 return sval_to_str_or_err_ptr(sval);
1375 1434 }
1376 1435
1377 1436 fn_param_str = get_fn_param_str(expr);
1378 1437 compare_str = expr_equal_to_param(expr, -1);
1379 1438 math_str = get_value_in_terms_of_parameter_math(expr);
1380 1439
1381 1440 if (get_implied_rl(expr, &rl) && !is_whole_rl(rl)) {
1382 1441 rl = cast_rl(cur_func_return_type(), rl);
1383 1442 return_ranges = show_rl(rl);
1384 1443 } else if (get_imaginary_absolute(expr, &rl)){
1385 1444 rl = cast_rl(cur_func_return_type(), rl);
1386 1445 return alloc_sname(show_rl(rl));
1387 1446 } else {
1388 1447 get_absolute_rl(expr, &rl);
1389 1448 rl = cast_rl(cur_func_return_type(), rl);
1390 1449 return_ranges = show_rl(rl);
1391 1450 }
1392 1451 *rl_p = rl;
1393 1452
1394 1453 if (fn_param_str) {
1395 1454 snprintf(buf, sizeof(buf), "%s%s", return_ranges, fn_param_str);
1396 1455 return alloc_sname(buf);
1397 1456 }
1398 1457 if (compare_str) {
1399 1458 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1400 1459 return alloc_sname(buf);
1401 1460 }
1402 1461 if (math_str) {
1403 1462 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, math_str);
1404 1463 return alloc_sname(buf);
1405 1464 }
1406 1465 compare_str = get_return_compare_str(expr);
1407 1466 if (compare_str) {
1408 1467 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1409 1468 return alloc_sname(buf);
↓ open down ↓ |
596 lines elided |
↑ open up ↑ |
1410 1469 }
1411 1470
1412 1471 return return_ranges;
1413 1472 }
1414 1473
1415 1474 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
1416 1475 {
1417 1476 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", function_signature());
1418 1477 }
1419 1478
1420 -static void call_return_state_hooks_conditional(struct expression *expr)
1479 +static bool call_return_state_hooks_conditional(struct expression *expr)
1421 1480 {
1422 - struct returned_state_callback *cb;
1423 - struct range_list *rl;
1424 - const char *return_ranges;
1425 1481 int final_pass_orig = final_pass;
1482 + static int recurse;
1426 1483
1484 + if (recurse >= 2)
1485 + return false;
1486 + if (!expr ||
1487 + (expr->type != EXPR_CONDITIONAL && expr->type != EXPR_SELECT))
1488 + return false;
1489 +
1490 + recurse++;
1491 +
1427 1492 __push_fake_cur_stree();
1428 1493
1429 1494 final_pass = 0;
1430 1495 __split_whole_condition(expr->conditional);
1431 1496 final_pass = final_pass_orig;
1432 1497
1433 - return_ranges = get_return_ranges_str(expr->cond_true ?: expr->conditional, &rl);
1498 + call_return_state_hooks(expr->cond_true ?: expr->conditional);
1434 1499
1435 - set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(rl));
1436 -
1437 - return_id++;
1438 - FOR_EACH_PTR(returned_state_callbacks, cb) {
1439 - cb->callback(return_id, (char *)return_ranges, expr->cond_true);
1440 - } END_FOR_EACH_PTR(cb);
1441 -
1442 1500 __push_true_states();
1443 1501 __use_false_states();
1444 1502
1445 - return_ranges = get_return_ranges_str(expr->cond_false, &rl);
1446 - set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(rl));
1503 + call_return_state_hooks(expr->cond_false);
1447 1504
1448 - return_id++;
1449 - FOR_EACH_PTR(returned_state_callbacks, cb) {
1450 - cb->callback(return_id, (char *)return_ranges, expr->cond_false);
1451 - } END_FOR_EACH_PTR(cb);
1452 -
1453 1505 __merge_true_states();
1454 1506 __free_fake_cur_stree();
1507 +
1508 + recurse--;
1509 + return true;
1455 1510 }
1456 1511
1457 1512 static void call_return_state_hooks_compare(struct expression *expr)
1458 1513 {
1459 1514 struct returned_state_callback *cb;
1460 1515 char *return_ranges;
1461 1516 int final_pass_orig = final_pass;
1462 1517 sval_t sval = { .type = &int_ctype };
1463 1518 sval_t ret;
1464 1519
1465 1520 if (!get_implied_value(expr, &ret))
1466 1521 ret.value = -1;
1467 1522
1468 1523 __push_fake_cur_stree();
1469 1524
1470 1525 final_pass = 0;
1471 1526 __split_whole_condition(expr);
1472 1527 final_pass = final_pass_orig;
1473 1528
1474 1529 if (ret.value != 0) {
1475 1530 return_ranges = alloc_sname("1");
1476 1531 sval.value = 1;
1477 1532 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1478 1533
1479 1534 return_id++;
1480 1535 FOR_EACH_PTR(returned_state_callbacks, cb) {
1481 1536 cb->callback(return_id, return_ranges, expr);
1482 1537 } END_FOR_EACH_PTR(cb);
1483 1538 }
1484 1539
1485 1540 __push_true_states();
1486 1541 __use_false_states();
1487 1542
1488 1543 if (ret.value != 1) {
1489 1544 return_ranges = alloc_sname("0");
1490 1545 sval.value = 0;
1491 1546 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1492 1547
1493 1548 return_id++;
1494 1549 FOR_EACH_PTR(returned_state_callbacks, cb) {
1495 1550 cb->callback(return_id, return_ranges, expr);
1496 1551 } END_FOR_EACH_PTR(cb);
1497 1552 }
1498 1553
1499 1554 __merge_true_states();
1500 1555 __free_fake_cur_stree();
1501 1556 }
1502 1557
1503 1558 static int ptr_in_list(struct sm_state *sm, struct state_list *slist)
1504 1559 {
1505 1560 struct sm_state *tmp;
1506 1561
1507 1562 FOR_EACH_PTR(slist, tmp) {
1508 1563 if (strcmp(tmp->state->name, sm->state->name) == 0)
1509 1564 return 1;
1510 1565 } END_FOR_EACH_PTR(tmp);
1511 1566
1512 1567 return 0;
1513 1568 }
1514 1569
1515 1570 static int split_possible_helper(struct sm_state *sm, struct expression *expr)
1516 1571 {
1517 1572 struct returned_state_callback *cb;
1518 1573 struct range_list *rl;
1519 1574 char *return_ranges;
1520 1575 struct sm_state *tmp;
1521 1576 int ret = 0;
1522 1577 int nr_possible, nr_states;
1523 1578 char *compare_str;
1524 1579 char buf[128];
1525 1580 struct state_list *already_handled = NULL;
1526 1581 sval_t sval;
1527 1582
1528 1583 if (!sm || !sm->merged)
↓ open down ↓ |
64 lines elided |
↑ open up ↑ |
1529 1584 return 0;
1530 1585
1531 1586 if (too_many_possible(sm))
1532 1587 return 0;
1533 1588
1534 1589 /* bail if it gets too complicated */
1535 1590 nr_possible = 0;
1536 1591 FOR_EACH_PTR(sm->possible, tmp) {
1537 1592 if (tmp->merged)
1538 1593 continue;
1594 + if (ptr_in_list(tmp, already_handled))
1595 + continue;
1596 + add_ptr_list(&already_handled, tmp);
1539 1597 nr_possible++;
1540 1598 } END_FOR_EACH_PTR(tmp);
1599 + free_slist(&already_handled);
1541 1600 nr_states = get_db_state_count();
1542 1601 if (nr_states * nr_possible >= 2000)
1543 1602 return 0;
1544 1603
1545 1604 FOR_EACH_PTR(sm->possible, tmp) {
1546 1605 if (tmp->merged)
1547 1606 continue;
1548 1607 if (ptr_in_list(tmp, already_handled))
1549 1608 continue;
1550 1609 add_ptr_list(&already_handled, tmp);
1551 1610
1552 1611 ret = 1;
1553 1612 __push_fake_cur_stree();
1554 1613
1555 1614 overwrite_states_using_pool(sm, tmp);
1556 1615
1557 1616 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
1558 1617 return_ranges = show_rl(rl);
1559 1618 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(clone_rl(rl)));
1560 1619 if (!rl_to_sval(rl, &sval)) {
1561 1620 compare_str = get_return_compare_str(expr);
1562 1621 if (compare_str) {
1563 1622 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1564 1623 return_ranges = alloc_sname(buf);
1565 1624 }
1566 1625 }
1567 1626
1568 1627 return_id++;
1569 1628 FOR_EACH_PTR(returned_state_callbacks, cb) {
1570 1629 cb->callback(return_id, return_ranges, expr);
1571 1630 } END_FOR_EACH_PTR(cb);
1572 1631
1573 1632 __free_fake_cur_stree();
1574 1633 } END_FOR_EACH_PTR(tmp);
1575 1634
1576 1635 free_slist(&already_handled);
1577 1636
1578 1637 return ret;
1579 1638 }
1580 1639
1581 1640 static int call_return_state_hooks_split_possible(struct expression *expr)
1582 1641 {
1583 1642 struct sm_state *sm;
1584 1643
1585 1644 if (!expr || expr_equal_to_param(expr, -1))
↓ open down ↓ |
35 lines elided |
↑ open up ↑ |
1586 1645 return 0;
1587 1646
1588 1647 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1589 1648 return split_possible_helper(sm, expr);
1590 1649 }
1591 1650
1592 1651 static bool has_possible_negative(struct sm_state *sm)
1593 1652 {
1594 1653 struct sm_state *tmp;
1595 1654
1655 + if (!type_signed(estate_type(sm->state)))
1656 + return false;
1657 +
1596 1658 FOR_EACH_PTR(sm->possible, tmp) {
1597 1659 if (!estate_rl(tmp->state))
1598 1660 continue;
1599 1661 if (sval_is_negative(estate_min(tmp->state)) &&
1600 1662 sval_is_negative(estate_max(tmp->state)))
1601 1663 return true;
1602 1664 } END_FOR_EACH_PTR(tmp);
1603 1665
1604 1666 return false;
1605 1667 }
1606 1668
1607 -static bool has_possible_zero_null(struct sm_state *sm)
1669 +static bool has_separate_zero_null(struct sm_state *sm)
1608 1670 {
1609 1671 struct sm_state *tmp;
1610 1672 sval_t sval;
1611 1673
1612 1674 FOR_EACH_PTR(sm->possible, tmp) {
1613 1675 if (!estate_get_single_value(tmp->state, &sval))
1614 1676 continue;
1615 1677 if (sval.value == 0)
1616 1678 return true;
1617 1679 } END_FOR_EACH_PTR(tmp);
1618 1680
↓ open down ↓ |
1 lines elided |
↑ open up ↑ |
1619 1681 return false;
1620 1682 }
1621 1683
1622 1684 static int split_positive_from_negative(struct expression *expr)
1623 1685 {
1624 1686 struct sm_state *sm;
1625 1687 struct returned_state_callback *cb;
1626 1688 struct range_list *rl;
1627 1689 const char *return_ranges;
1628 1690 struct range_list *ret_rl;
1691 + bool separate_zero;
1629 1692 int undo;
1630 - bool has_zero;
1631 1693
1632 1694 /* We're going to print the states 3 times */
1633 1695 if (get_db_state_count() > 10000 / 3)
1634 1696 return 0;
1635 1697
1636 1698 if (!get_implied_rl(expr, &rl) || !rl)
1637 1699 return 0;
1638 - if (is_whole_rl(rl) || is_whole_rl_non_zero(rl))
1639 - return 0;
1640 1700 /* Forget about INT_MAX and larger */
1641 1701 if (rl_max(rl).value <= 0)
1642 1702 return 0;
1643 1703 if (!sval_is_negative(rl_min(rl)))
1644 1704 return 0;
1645 1705
1646 1706 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1647 1707 if (!sm)
1648 1708 return 0;
1649 1709 if (!has_possible_negative(sm))
1650 1710 return 0;
1651 - has_zero = has_possible_zero_null(sm);
1711 + separate_zero = has_separate_zero_null(sm);
1652 1712
1653 - if (!assume(compare_expression(expr, has_zero ? '>' : SPECIAL_GTE, zero_expr())))
1713 + if (!assume(compare_expression(expr, separate_zero ? '>' : SPECIAL_GTE, zero_expr())))
1654 1714 return 0;
1655 1715
1656 1716 return_id++;
1657 1717 return_ranges = get_return_ranges_str(expr, &ret_rl);
1658 1718 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1659 1719 FOR_EACH_PTR(returned_state_callbacks, cb) {
1660 1720 cb->callback(return_id, (char *)return_ranges, expr);
1661 1721 } END_FOR_EACH_PTR(cb);
1662 1722
1663 1723 end_assume();
1664 1724
1665 - if (has_zero) {
1725 + if (separate_zero) {
1666 1726 undo = assume(compare_expression(expr, SPECIAL_EQUAL, zero_expr()));
1667 1727
1668 1728 return_id++;
1669 1729 return_ranges = get_return_ranges_str(expr, &ret_rl);
1670 1730 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1671 1731 FOR_EACH_PTR(returned_state_callbacks, cb) {
1672 1732 cb->callback(return_id, (char *)return_ranges, expr);
1673 1733 } END_FOR_EACH_PTR(cb);
1674 1734
1675 1735 if (undo)
1676 1736 end_assume();
1677 1737 }
1678 1738
1679 1739 undo = assume(compare_expression(expr, '<', zero_expr()));
1680 1740
1681 1741 return_id++;
1682 1742 return_ranges = get_return_ranges_str(expr, &ret_rl);
1683 1743 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1684 1744 FOR_EACH_PTR(returned_state_callbacks, cb) {
1685 1745 cb->callback(return_id, (char *)return_ranges, expr);
1686 1746 } END_FOR_EACH_PTR(cb);
1687 1747
1688 1748 if (undo)
1689 1749 end_assume();
1690 1750
1691 1751 return 1;
1692 1752 }
1693 1753
1694 1754 static int call_return_state_hooks_split_null_non_null_zero(struct expression *expr)
1695 1755 {
1696 1756 struct returned_state_callback *cb;
1697 1757 struct range_list *rl;
1698 1758 struct range_list *nonnull_rl;
1699 1759 sval_t null_sval;
1700 1760 struct range_list *null_rl = NULL;
1701 1761 char *return_ranges;
1702 1762 struct sm_state *sm;
1703 1763 struct smatch_state *state;
1704 1764 int nr_states;
1705 1765 int final_pass_orig = final_pass;
1706 1766
1707 1767 if (!expr || expr_equal_to_param(expr, -1))
1708 1768 return 0;
1709 1769 if (expr->type == EXPR_CALL)
1710 1770 return 0;
1711 1771
↓ open down ↓ |
36 lines elided |
↑ open up ↑ |
1712 1772 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1713 1773 if (!sm)
1714 1774 return 0;
1715 1775 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
1716 1776 return 0;
1717 1777 state = sm->state;
1718 1778 if (!estate_rl(state))
1719 1779 return 0;
1720 1780 if (estate_min(state).value == 0 && estate_max(state).value == 0)
1721 1781 return 0;
1722 - if (!has_possible_zero_null(sm))
1782 + if (!has_separate_zero_null(sm))
1723 1783 return 0;
1724 1784
1725 1785 nr_states = get_db_state_count();
1726 1786 if (option_info && nr_states >= 1500)
1727 1787 return 0;
1728 1788
1729 1789 rl = estate_rl(state);
1730 1790
1731 1791 __push_fake_cur_stree();
1732 1792
1733 1793 final_pass = 0;
1734 1794 __split_whole_condition(expr);
1735 1795 final_pass = final_pass_orig;
1736 1796
1737 1797 nonnull_rl = rl_filter(rl, rl_zero());
1738 1798 return_ranges = show_rl(nonnull_rl);
1739 1799 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonnull_rl));
1740 1800
1741 1801 return_id++;
1742 1802 FOR_EACH_PTR(returned_state_callbacks, cb) {
1743 1803 cb->callback(return_id, return_ranges, expr);
1744 1804 } END_FOR_EACH_PTR(cb);
1745 1805
1746 1806 __push_true_states();
1747 1807 __use_false_states();
1748 1808
1749 1809 return_ranges = alloc_sname("0");
1750 1810 null_sval = sval_type_val(rl_type(rl), 0);
1751 1811 add_range(&null_rl, null_sval, null_sval);
1752 1812 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(null_rl));
1753 1813 return_id++;
↓ open down ↓ |
21 lines elided |
↑ open up ↑ |
1754 1814 FOR_EACH_PTR(returned_state_callbacks, cb) {
1755 1815 cb->callback(return_id, return_ranges, expr);
1756 1816 } END_FOR_EACH_PTR(cb);
1757 1817
1758 1818 __merge_true_states();
1759 1819 __free_fake_cur_stree();
1760 1820
1761 1821 return 1;
1762 1822 }
1763 1823
1824 +static bool is_kernel_success_fail(struct sm_state *sm)
1825 +{
1826 + struct sm_state *tmp;
1827 + struct range_list *rl;
1828 + bool has_zero = false;
1829 + bool has_neg = false;
1830 +
1831 + if (!type_signed(estate_type(sm->state)))
1832 + return false;
1833 +
1834 + FOR_EACH_PTR(sm->possible, tmp) {
1835 + rl = estate_rl(tmp->state);
1836 + if (!rl)
1837 + return false;
1838 + if (rl_min(rl).value == 0 && rl_max(rl).value == 0) {
1839 + has_zero = true;
1840 + continue;
1841 + }
1842 + has_neg = true;
1843 + if (rl_min(rl).value >= -4095 && rl_max(rl).value < 0)
1844 + continue;
1845 + if (strcmp(tmp->state->name, "s32min-(-1)") == 0)
1846 + continue;
1847 + if (strcmp(tmp->state->name, "s32min-(-1),1-s32max") == 0)
1848 + continue;
1849 + return false;
1850 + } END_FOR_EACH_PTR(tmp);
1851 +
1852 + return has_zero && has_neg;
1853 +}
1854 +
1764 1855 static int call_return_state_hooks_split_success_fail(struct expression *expr)
1765 1856 {
1766 1857 struct sm_state *sm;
1767 1858 struct range_list *rl;
1768 1859 struct range_list *nonzero_rl;
1769 1860 sval_t zero_sval;
1770 1861 struct range_list *zero_rl = NULL;
1771 1862 int nr_states;
1772 1863 struct returned_state_callback *cb;
1773 1864 char *return_ranges;
1774 1865 int final_pass_orig = final_pass;
1775 1866
1776 1867 if (option_project != PROJ_KERNEL)
1777 1868 return 0;
1778 1869
1779 1870 nr_states = get_db_state_count();
1780 - if (nr_states > 1500)
1871 + if (nr_states > 2000)
1781 1872 return 0;
1782 1873
1783 1874 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1784 1875 if (!sm)
1785 1876 return 0;
1786 1877 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
1787 1878 return 0;
1879 + if (!is_kernel_success_fail(sm))
1880 + return 0;
1788 1881
1789 1882 rl = estate_rl(sm->state);
1790 1883 if (!rl)
1791 1884 return 0;
1792 1885
1793 - if (rl_min(rl).value < -4095 || rl_min(rl).value >= 0)
1794 - return 0;
1795 - if (rl_max(rl).value != 0)
1796 - return 0;
1797 - if (!has_possible_zero_null(sm))
1798 - return 0;
1799 -
1800 1886 __push_fake_cur_stree();
1801 1887
1802 1888 final_pass = 0;
1803 1889 __split_whole_condition(expr);
1804 1890 final_pass = final_pass_orig;
1805 1891
1806 1892 nonzero_rl = rl_filter(rl, rl_zero());
1807 1893 nonzero_rl = cast_rl(cur_func_return_type(), nonzero_rl);
1808 1894 return_ranges = show_rl(nonzero_rl);
1809 1895 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonzero_rl));
1810 1896
1811 1897 return_id++;
1812 1898 FOR_EACH_PTR(returned_state_callbacks, cb) {
1813 1899 cb->callback(return_id, return_ranges, expr);
1814 1900 } END_FOR_EACH_PTR(cb);
1815 1901
1816 1902 __push_true_states();
1817 1903 __use_false_states();
1818 1904
1819 1905 return_ranges = alloc_sname("0");
1820 1906 zero_sval = sval_type_val(rl_type(rl), 0);
1821 1907 add_range(&zero_rl, zero_sval, zero_sval);
1822 1908 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(zero_rl));
1823 1909 return_id++;
1824 1910 FOR_EACH_PTR(returned_state_callbacks, cb) {
1825 1911 cb->callback(return_id, return_ranges, expr);
1826 1912 } END_FOR_EACH_PTR(cb);
1827 1913
1828 1914 __merge_true_states();
1829 1915 __free_fake_cur_stree();
1830 1916
1831 1917 return 1;
1832 1918 }
1833 1919
1834 1920 static int is_boolean(struct expression *expr)
↓ open down ↓ |
25 lines elided |
↑ open up ↑ |
1835 1921 {
1836 1922 struct range_list *rl;
1837 1923
1838 1924 if (!get_implied_rl(expr, &rl))
1839 1925 return 0;
1840 1926 if (rl_min(rl).value == 0 && rl_max(rl).value == 1)
1841 1927 return 1;
1842 1928 return 0;
1843 1929 }
1844 1930
1845 -static int is_conditional(struct expression *expr)
1846 -{
1847 - if (!expr)
1848 - return 0;
1849 - if (expr->type == EXPR_CONDITIONAL || expr->type == EXPR_SELECT)
1850 - return 1;
1851 - return 0;
1852 -}
1853 -
1854 1931 static int splitable_function_call(struct expression *expr)
1855 1932 {
1856 1933 struct sm_state *sm;
1857 1934 char buf[64];
1858 1935
1859 1936 if (!expr || expr->type != EXPR_CALL)
1860 1937 return 0;
1861 1938 snprintf(buf, sizeof(buf), "return %p", expr);
1862 1939 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
1863 1940 return split_possible_helper(sm, expr);
1864 1941 }
1865 1942
1866 1943 static struct sm_state *find_bool_param(void)
1867 1944 {
1868 1945 struct stree *start_states;
1869 1946 struct symbol *arg;
1870 1947 struct sm_state *sm, *tmp;
1871 1948 sval_t sval;
1872 1949
1873 1950 start_states = get_start_states();
1874 1951
1875 1952 FOR_EACH_PTR_REVERSE(cur_func_sym->ctype.base_type->arguments, arg) {
1876 1953 if (!arg->ident)
1877 1954 continue;
1878 1955 sm = get_sm_state_stree(start_states, SMATCH_EXTRA, arg->ident->name, arg);
1879 1956 if (!sm)
1880 1957 continue;
1881 1958 if (rl_min(estate_rl(sm->state)).value != 0 ||
1882 1959 rl_max(estate_rl(sm->state)).value != 1)
1883 1960 continue;
1884 1961 goto found;
1885 1962 } END_FOR_EACH_PTR_REVERSE(arg);
1886 1963
1887 1964 return NULL;
1888 1965
1889 1966 found:
1890 1967 /*
1891 1968 * Check if it's splitable. If not, then splitting it up is likely not
1892 1969 * useful for the callers.
1893 1970 */
1894 1971 FOR_EACH_PTR(sm->possible, tmp) {
1895 1972 if (is_merged(tmp))
1896 1973 continue;
1897 1974 if (!estate_get_single_value(tmp->state, &sval))
1898 1975 return NULL;
1899 1976 } END_FOR_EACH_PTR(tmp);
1900 1977
↓ open down ↓ |
37 lines elided |
↑ open up ↑ |
1901 1978 return sm;
1902 1979 }
1903 1980
1904 1981 static int split_on_bool_sm(struct sm_state *sm, struct expression *expr)
1905 1982 {
1906 1983 struct returned_state_callback *cb;
1907 1984 struct range_list *ret_rl;
1908 1985 const char *return_ranges;
1909 1986 struct sm_state *tmp;
1910 1987 int ret = 0;
1911 - int nr_possible, nr_states;
1912 1988 struct state_list *already_handled = NULL;
1913 1989
1914 1990 if (!sm || !sm->merged)
1915 1991 return 0;
1916 1992
1917 1993 if (too_many_possible(sm))
1918 1994 return 0;
1919 1995
1920 - /* bail if it gets too complicated */
1921 - nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
1922 - nr_states = get_db_state_count();
1923 - if (nr_states * nr_possible >= 2000)
1924 - return 0;
1925 -
1926 1996 FOR_EACH_PTR(sm->possible, tmp) {
1927 1997 if (tmp->merged)
1928 1998 continue;
1929 1999 if (ptr_in_list(tmp, already_handled))
1930 2000 continue;
1931 2001 add_ptr_list(&already_handled, tmp);
1932 2002
1933 2003 ret = 1;
1934 2004 __push_fake_cur_stree();
1935 2005
1936 2006 overwrite_states_using_pool(sm, tmp);
1937 2007
1938 2008 return_ranges = get_return_ranges_str(expr, &ret_rl);
1939 2009 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1940 2010 return_id++;
1941 2011 FOR_EACH_PTR(returned_state_callbacks, cb) {
1942 2012 cb->callback(return_id, (char *)return_ranges, expr);
1943 2013 } END_FOR_EACH_PTR(cb);
1944 2014
1945 2015 __free_fake_cur_stree();
1946 2016 } END_FOR_EACH_PTR(tmp);
1947 2017
1948 2018 free_slist(&already_handled);
1949 2019
1950 2020 return ret;
1951 2021 }
1952 2022
1953 2023 static int split_by_bool_param(struct expression *expr)
↓ open down ↓ |
18 lines elided |
↑ open up ↑ |
1954 2024 {
1955 2025 struct sm_state *start_sm, *sm;
1956 2026 sval_t sval;
1957 2027
1958 2028 start_sm = find_bool_param();
1959 2029 if (!start_sm)
1960 2030 return 0;
1961 2031 sm = get_sm_state(SMATCH_EXTRA, start_sm->name, start_sm->sym);
1962 2032 if (!sm || estate_get_single_value(sm->state, &sval))
1963 2033 return 0;
2034 +
2035 + if (get_db_state_count() * 2 >= 2000)
2036 + return 0;
2037 +
1964 2038 return split_on_bool_sm(sm, expr);
1965 2039 }
1966 2040
1967 2041 static int split_by_null_nonnull_param(struct expression *expr)
1968 2042 {
1969 2043 struct symbol *arg;
1970 2044 struct sm_state *sm;
1971 - sval_t zero = {
1972 - .type = &ulong_ctype,
1973 - };
2045 + int nr_possible;
1974 2046
1975 2047 /* function must only take one pointer */
1976 2048 if (ptr_list_size((struct ptr_list *)cur_func_sym->ctype.base_type->arguments) != 1)
1977 2049 return 0;
1978 2050 arg = first_ptr_list((struct ptr_list *)cur_func_sym->ctype.base_type->arguments);
1979 2051 if (!arg->ident)
1980 2052 return 0;
1981 2053 if (get_real_base_type(arg)->type != SYM_PTR)
1982 2054 return 0;
1983 2055
1984 2056 if (param_was_set_var_sym(arg->ident->name, arg))
1985 2057 return 0;
1986 2058 sm = get_sm_state(SMATCH_EXTRA, arg->ident->name, arg);
1987 2059 if (!sm)
1988 2060 return 0;
1989 2061
1990 - if (!rl_has_sval(estate_rl(sm->state), zero))
2062 + if (!has_separate_zero_null(sm))
1991 2063 return 0;
1992 2064
2065 + nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
2066 + if (get_db_state_count() * nr_possible >= 2000)
2067 + return 0;
2068 +
1993 2069 return split_on_bool_sm(sm, expr);
1994 2070 }
1995 2071
1996 2072 struct expression *strip_expr_statement(struct expression *expr)
1997 2073 {
1998 2074 struct expression *orig = expr;
1999 2075 struct statement *stmt, *last_stmt;
2000 2076
2001 2077 if (!expr)
2002 2078 return NULL;
2003 2079 if (expr->type == EXPR_PREOP && expr->op == '(')
2004 2080 expr = expr->unop;
2005 2081 if (expr->type != EXPR_STATEMENT)
2006 2082 return orig;
2007 2083 stmt = expr->statement;
2008 2084 if (!stmt || stmt->type != STMT_COMPOUND)
2009 2085 return orig;
2010 2086
2011 2087 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
2012 2088 if (!last_stmt || last_stmt->type == STMT_LABEL)
2013 2089 last_stmt = last_stmt->label_statement;
2014 2090 if (!last_stmt || last_stmt->type != STMT_EXPRESSION)
2015 2091 return orig;
2016 2092 return strip_expr(last_stmt->expression);
2017 2093 }
2018 2094
2019 2095 static void call_return_state_hooks(struct expression *expr)
2020 2096 {
2021 2097 struct returned_state_callback *cb;
2022 2098 struct range_list *ret_rl;
2023 2099 const char *return_ranges;
2024 2100 int nr_states;
2025 2101 sval_t sval;
2026 2102
2027 2103 if (__path_is_null())
2028 2104 return;
2029 2105
2030 2106 expr = strip_expr(expr);
↓ open down ↓ |
28 lines elided |
↑ open up ↑ |
2031 2107 expr = strip_expr_statement(expr);
2032 2108
2033 2109 if (is_impossible_path())
2034 2110 goto vanilla;
2035 2111
2036 2112 if (expr && (expr->type == EXPR_COMPARE ||
2037 2113 !get_implied_value(expr, &sval)) &&
2038 2114 (is_condition(expr) || is_boolean(expr))) {
2039 2115 call_return_state_hooks_compare(expr);
2040 2116 return;
2041 - } else if (is_conditional(expr)) {
2042 - call_return_state_hooks_conditional(expr);
2117 + } else if (call_return_state_hooks_conditional(expr)) {
2043 2118 return;
2044 2119 } else if (call_return_state_hooks_split_possible(expr)) {
2045 2120 return;
2046 2121 } else if (split_positive_from_negative(expr)) {
2047 2122 return;
2048 2123 } else if (call_return_state_hooks_split_null_non_null_zero(expr)) {
2049 2124 return;
2050 2125 } else if (call_return_state_hooks_split_success_fail(expr)) {
2051 2126 return;
2052 2127 } else if (splitable_function_call(expr)) {
2053 2128 return;
2054 2129 } else if (split_by_bool_param(expr)) {
2055 2130 } else if (split_by_null_nonnull_param(expr)) {
2056 2131 return;
2057 2132 }
2058 2133
2059 2134 vanilla:
2060 2135 return_ranges = get_return_ranges_str(expr, &ret_rl);
2061 2136 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
2062 2137
2063 2138 return_id++;
2064 2139 nr_states = get_db_state_count();
2065 2140 if (nr_states >= 10000) {
2066 2141 match_return_info(return_id, (char *)return_ranges, expr);
2067 2142 print_limited_param_set(return_id, (char *)return_ranges, expr);
2068 2143 mark_all_params_untracked(return_id, (char *)return_ranges, expr);
2069 2144 return;
2070 2145 }
2071 2146 FOR_EACH_PTR(returned_state_callbacks, cb) {
2072 2147 cb->callback(return_id, (char *)return_ranges, expr);
2073 2148 } END_FOR_EACH_PTR(cb);
2074 2149 }
2075 2150
2076 2151 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
2077 2152 {
2078 2153 struct returned_member_callback *cb;
2079 2154 struct stree *stree;
2080 2155 struct sm_state *sm;
2081 2156 struct symbol *type;
2082 2157 char *name;
2083 2158 char member_name[256];
2084 2159 int len;
2085 2160
2086 2161 type = get_type(expr);
2087 2162 if (!type || type->type != SYM_PTR)
2088 2163 return;
2089 2164 name = expr_to_var(expr);
2090 2165 if (!name)
2091 2166 return;
2092 2167
2093 2168 member_name[sizeof(member_name) - 1] = '\0';
2094 2169 strcpy(member_name, "$");
2095 2170
2096 2171 len = strlen(name);
2097 2172 FOR_EACH_PTR(returned_member_callbacks, cb) {
2098 2173 stree = __get_cur_stree();
2099 2174 FOR_EACH_MY_SM(cb->owner, stree, sm) {
2100 2175 if (sm->name[0] == '*' && strcmp(sm->name + 1, name) == 0) {
2101 2176 strcpy(member_name, "*$");
2102 2177 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
2103 2178 continue;
2104 2179 }
2105 2180 if (strncmp(sm->name, name, len) != 0)
2106 2181 continue;
2107 2182 if (strncmp(sm->name + len, "->", 2) != 0)
2108 2183 continue;
2109 2184 snprintf(member_name, sizeof(member_name), "$%s", sm->name + len);
2110 2185 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
2111 2186 } END_FOR_EACH_SM(sm);
2112 2187 } END_FOR_EACH_PTR(cb);
2113 2188
2114 2189 free_string(name);
2115 2190 }
2116 2191
2117 2192 static void reset_memdb(struct symbol *sym)
2118 2193 {
2119 2194 mem_sql(NULL, NULL, "delete from caller_info;");
2120 2195 mem_sql(NULL, NULL, "delete from return_states;");
2121 2196 mem_sql(NULL, NULL, "delete from call_implies;");
2122 2197 mem_sql(NULL, NULL, "delete from return_implies;");
2123 2198 }
2124 2199
2125 2200 static void match_end_func_info(struct symbol *sym)
2126 2201 {
2127 2202 if (__path_is_null())
2128 2203 return;
2129 2204 call_return_state_hooks(NULL);
2130 2205 }
2131 2206
2132 2207 static void match_after_func(struct symbol *sym)
2133 2208 {
2134 2209 if (!__inline_fn)
2135 2210 reset_memdb(sym);
2136 2211 }
2137 2212
2138 2213 static void init_memdb(void)
2139 2214 {
2140 2215 char *err = NULL;
2141 2216 int rc;
2142 2217 const char *schema_files[] = {
2143 2218 "db/db.schema",
2144 2219 "db/caller_info.schema",
2145 2220 "db/common_caller_info.schema",
2146 2221 "db/return_states.schema",
2147 2222 "db/function_type_size.schema",
2148 2223 "db/type_size.schema",
2149 2224 "db/function_type_info.schema",
2150 2225 "db/type_info.schema",
2151 2226 "db/call_implies.schema",
2152 2227 "db/return_implies.schema",
2153 2228 "db/function_ptr.schema",
2154 2229 "db/local_values.schema",
2155 2230 "db/function_type_value.schema",
2156 2231 "db/type_value.schema",
2157 2232 "db/function_type.schema",
2158 2233 "db/data_info.schema",
2159 2234 "db/parameter_name.schema",
2160 2235 "db/constraints.schema",
2161 2236 "db/constraints_required.schema",
2162 2237 "db/fn_ptr_data_link.schema",
2163 2238 "db/fn_data_link.schema",
2164 2239 "db/mtag_about.schema",
2165 2240 "db/mtag_map.schema",
2166 2241 "db/mtag_data.schema",
2167 2242 "db/mtag_alias.schema",
2168 2243 };
2169 2244 static char buf[4096];
2170 2245 int fd;
2171 2246 int ret;
2172 2247 int i;
2173 2248
2174 2249 rc = sqlite3_open(":memory:", &mem_db);
2175 2250 if (rc != SQLITE_OK) {
2176 2251 sm_ierror("starting In-Memory database.");
2177 2252 return;
2178 2253 }
2179 2254
2180 2255 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
2181 2256 fd = open_schema_file(schema_files[i]);
2182 2257 if (fd < 0)
2183 2258 continue;
2184 2259 ret = read(fd, buf, sizeof(buf));
2185 2260 if (ret < 0) {
2186 2261 sm_ierror("failed to read: %s", schema_files[i]);
2187 2262 continue;
2188 2263 }
2189 2264 close(fd);
2190 2265 if (ret == sizeof(buf)) {
2191 2266 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2192 2267 schema_files[i], sizeof(buf));
2193 2268 continue;
2194 2269 }
2195 2270 buf[ret] = '\0';
2196 2271 rc = sqlite3_exec(mem_db, buf, NULL, NULL, &err);
2197 2272 if (rc != SQLITE_OK) {
2198 2273 sm_ierror("SQL error #2: %s", err);
2199 2274 sm_ierror("%s", buf);
2200 2275 }
2201 2276 }
2202 2277 }
2203 2278
2204 2279 static void init_cachedb(void)
2205 2280 {
2206 2281 char *err = NULL;
2207 2282 int rc;
2208 2283 const char *schema_files[] = {
2209 2284 "db/call_implies.schema",
2210 2285 "db/return_implies.schema",
2211 2286 "db/type_info.schema",
2212 2287 "db/mtag_data.schema",
2213 2288 "db/sink_info.schema",
2214 2289 };
2215 2290 static char buf[4096];
2216 2291 int fd;
2217 2292 int ret;
2218 2293 int i;
2219 2294
2220 2295 rc = sqlite3_open(":memory:", &cache_db);
2221 2296 if (rc != SQLITE_OK) {
2222 2297 sm_ierror("starting In-Memory database.");
2223 2298 return;
2224 2299 }
2225 2300
2226 2301 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
2227 2302 fd = open_schema_file(schema_files[i]);
2228 2303 if (fd < 0)
2229 2304 continue;
2230 2305 ret = read(fd, buf, sizeof(buf));
2231 2306 if (ret < 0) {
2232 2307 sm_ierror("failed to read: %s", schema_files[i]);
2233 2308 continue;
2234 2309 }
2235 2310 close(fd);
2236 2311 if (ret == sizeof(buf)) {
2237 2312 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2238 2313 schema_files[i], sizeof(buf));
2239 2314 continue;
2240 2315 }
2241 2316 buf[ret] = '\0';
2242 2317 rc = sqlite3_exec(cache_db, buf, NULL, NULL, &err);
2243 2318 if (rc != SQLITE_OK) {
2244 2319 sm_ierror("SQL error #2: %s", err);
2245 2320 sm_ierror("%s", buf);
2246 2321 }
2247 2322 }
2248 2323 }
2249 2324
2250 2325 static int save_cache_data(void *_table, int argc, char **argv, char **azColName)
2251 2326 {
2252 2327 static char buf[4096];
2253 2328 char tmp[256];
2254 2329 char *p = buf;
2255 2330 char *table = _table;
2256 2331 int i;
2257 2332
2258 2333
2259 2334 p += snprintf(p, 4096 - (p - buf), "insert or ignore into %s values (", table);
2260 2335 for (i = 0; i < argc; i++) {
2261 2336 if (i)
2262 2337 p += snprintf(p, 4096 - (p - buf), ", ");
2263 2338 sqlite3_snprintf(sizeof(tmp), tmp, "%q", escape_newlines(argv[i]));
2264 2339 p += snprintf(p, 4096 - (p - buf), "'%s'", tmp);
2265 2340
2266 2341 }
2267 2342 p += snprintf(p, 4096 - (p - buf), ");");
2268 2343 if (p - buf > 4096)
2269 2344 return 0;
2270 2345
2271 2346 sm_msg("SQL: %s", buf);
2272 2347 return 0;
2273 2348 }
2274 2349
2275 2350 static void dump_cache(struct symbol_list *sym_list)
2276 2351 {
2277 2352 if (!option_info)
2278 2353 return;
2279 2354 cache_sql(&save_cache_data, (char *)"type_info", "select * from type_info;");
2280 2355 cache_sql(&save_cache_data, (char *)"return_implies", "select * from return_implies;");
2281 2356 cache_sql(&save_cache_data, (char *)"call_implies", "select * from call_implies;");
2282 2357 cache_sql(&save_cache_data, (char *)"mtag_data", "select * from mtag_data;");
2283 2358 cache_sql(&save_cache_data, (char *)"sink_info", "select * from sink_info;");
2284 2359 }
2285 2360
2286 2361 void open_smatch_db(char *db_file)
2287 2362 {
2288 2363 int rc;
2289 2364
2290 2365 if (option_no_db)
2291 2366 return;
2292 2367
2293 2368 use_states = malloc(num_checks + 1);
2294 2369 memset(use_states, 0xff, num_checks + 1);
2295 2370
2296 2371 init_memdb();
2297 2372 init_cachedb();
2298 2373
2299 2374 rc = sqlite3_open_v2(db_file, &smatch_db, SQLITE_OPEN_READONLY, NULL);
2300 2375 if (rc != SQLITE_OK) {
2301 2376 option_no_db = 1;
2302 2377 return;
2303 2378 }
2304 2379 run_sql(NULL, NULL,
2305 2380 "PRAGMA cache_size = %d;", SQLITE_CACHE_PAGES);
2306 2381 return;
2307 2382 }
2308 2383
2309 2384 static void register_common_funcs(void)
2310 2385 {
2311 2386 struct token *token;
2312 2387 char *func;
2313 2388 char filename[256];
2314 2389
2315 2390 if (option_project == PROJ_NONE)
2316 2391 strcpy(filename, "common_functions");
2317 2392 else
2318 2393 snprintf(filename, 256, "%s.common_functions", option_project_str);
2319 2394
2320 2395 token = get_tokens_file(filename);
2321 2396 if (!token)
2322 2397 return;
2323 2398 if (token_type(token) != TOKEN_STREAMBEGIN)
2324 2399 return;
2325 2400 token = token->next;
2326 2401 while (token_type(token) != TOKEN_STREAMEND) {
2327 2402 if (token_type(token) != TOKEN_IDENT)
2328 2403 return;
2329 2404 func = alloc_string(show_ident(token->ident));
2330 2405 add_ptr_list(&common_funcs, func);
2331 2406 token = token->next;
2332 2407 }
2333 2408 clear_token_alloc();
2334 2409 }
2335 2410
2336 2411 static char *get_next_string(char **str)
2337 2412 {
2338 2413 static char string[256];
2339 2414 char *start;
2340 2415 char *p = *str;
2341 2416 int len, i, j;
2342 2417
2343 2418 if (*p == '\0')
2344 2419 return NULL;
2345 2420 start = p;
2346 2421
2347 2422 while (*p != '\0' && *p != '\n') {
2348 2423 if (*p == '\\' && *(p + 1) == ' ') {
2349 2424 p += 2;
2350 2425 continue;
2351 2426 }
2352 2427 if (*p == ' ')
2353 2428 break;
2354 2429 p++;
2355 2430 }
2356 2431
2357 2432 len = p - start;
2358 2433 if (len >= sizeof(string)) {
2359 2434 memcpy(string, start, sizeof(string));
2360 2435 string[sizeof(string) - 1] = '\0';
2361 2436 sm_ierror("return_fix: '%s' too long", string);
2362 2437 **str = '\0';
2363 2438 return NULL;
2364 2439 }
2365 2440 memcpy(string, start, len);
2366 2441 string[len] = '\0';
2367 2442 for (i = 0; i < sizeof(string) - 1; i++) {
2368 2443 if (string[i] == '\\' && string[i + 1] == ' ') {
2369 2444 for (j = i; string[j] != '\0'; j++)
2370 2445 string[j] = string[j + 1];
2371 2446 }
2372 2447 }
2373 2448 if (*p != '\0')
2374 2449 p++;
2375 2450 *str = p;
2376 2451 return string;
2377 2452 }
2378 2453
2379 2454 static void register_return_replacements(void)
2380 2455 {
2381 2456 char *func, *orig, *new;
2382 2457 char filename[256];
2383 2458 char buf[4096];
2384 2459 int fd, ret, i;
2385 2460 char *p;
2386 2461
2387 2462 snprintf(filename, 256, "db/%s.return_fixes", option_project_str);
2388 2463 fd = open_schema_file(filename);
2389 2464 if (fd < 0)
2390 2465 return;
2391 2466 ret = read(fd, buf, sizeof(buf));
2392 2467 close(fd);
2393 2468 if (ret < 0)
2394 2469 return;
2395 2470 if (ret == sizeof(buf)) {
2396 2471 sm_ierror("file too large: %s (limit %zd bytes)",
2397 2472 filename, sizeof(buf));
2398 2473 return;
2399 2474 }
2400 2475 buf[ret] = '\0';
2401 2476
2402 2477 p = buf;
2403 2478 while (*p) {
2404 2479 get_next_string(&p);
2405 2480 replace_count++;
2406 2481 }
2407 2482 if (replace_count == 0 || replace_count % 3 != 0) {
2408 2483 replace_count = 0;
2409 2484 return;
2410 2485 }
2411 2486 replace_table = malloc(replace_count * sizeof(char *));
2412 2487
2413 2488 p = buf;
2414 2489 i = 0;
2415 2490 while (*p) {
2416 2491 func = alloc_string(get_next_string(&p));
2417 2492 orig = alloc_string(get_next_string(&p));
2418 2493 new = alloc_string(get_next_string(&p));
2419 2494
2420 2495 replace_table[i++] = func;
2421 2496 replace_table[i++] = orig;
2422 2497 replace_table[i++] = new;
2423 2498 }
2424 2499 }
2425 2500
2426 2501 void register_definition_db_callbacks(int id)
2427 2502 {
2428 2503 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
2429 2504 add_split_return_callback(match_return_info);
2430 2505 add_split_return_callback(print_returned_struct_members);
2431 2506 add_hook(&call_return_state_hooks, RETURN_HOOK);
2432 2507 add_hook(&match_end_func_info, END_FUNC_HOOK);
2433 2508 add_hook(&match_after_func, AFTER_FUNC_HOOK);
2434 2509
2435 2510 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
2436 2511 add_hook(&match_call_implies, FUNC_DEF_HOOK);
2437 2512 add_hook(&match_return_implies, CALL_HOOK_AFTER_INLINE);
2438 2513
2439 2514 register_common_funcs();
2440 2515 register_return_replacements();
2441 2516
2442 2517 add_hook(&dump_cache, END_FILE_HOOK);
2443 2518 }
2444 2519
2445 2520 void register_db_call_marker(int id)
2446 2521 {
2447 2522 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
2448 2523 }
2449 2524
2450 2525 char *return_state_to_var_sym(struct expression *expr, int param, const char *key, struct symbol **sym)
2451 2526 {
2452 2527 struct expression *arg;
2453 2528 char *name = NULL;
2454 2529 char member_name[256];
2455 2530
2456 2531 *sym = NULL;
2457 2532
2458 2533 if (param == -1) {
2459 2534 const char *star = "";
2460 2535
2461 2536 if (expr->type != EXPR_ASSIGNMENT)
2462 2537 return NULL;
2463 2538 if (get_type(expr->left) == &int_ctype && strcmp(key, "$") != 0)
2464 2539 return NULL;
2465 2540 name = expr_to_var_sym(expr->left, sym);
2466 2541 if (!name)
2467 2542 return NULL;
2468 2543 if (key[0] == '*') {
2469 2544 star = "*";
2470 2545 key++;
2471 2546 }
2472 2547 if (strncmp(key, "$", 1) != 0)
2473 2548 return name;
2474 2549 snprintf(member_name, sizeof(member_name), "%s%s%s", star, name, key + 1);
2475 2550 free_string(name);
2476 2551 return alloc_string(member_name);
2477 2552 }
2478 2553
2479 2554 while (expr->type == EXPR_ASSIGNMENT)
2480 2555 expr = strip_expr(expr->right);
2481 2556 if (expr->type != EXPR_CALL)
2482 2557 return NULL;
2483 2558
2484 2559 arg = get_argument_from_call_expr(expr->args, param);
↓ open down ↓ |
432 lines elided |
↑ open up ↑ |
2485 2560 if (!arg)
2486 2561 return NULL;
2487 2562
2488 2563 return get_variable_from_key(arg, key, sym);
2489 2564 }
2490 2565
2491 2566 char *get_variable_from_key(struct expression *arg, const char *key, struct symbol **sym)
2492 2567 {
2493 2568 char buf[256];
2494 2569 char *tmp;
2495 - bool add_star = false;
2570 + int star_cnt = 0;
2496 2571
2497 2572 if (!arg)
2498 2573 return NULL;
2499 2574
2500 2575 arg = strip_expr(arg);
2501 2576
2502 2577 if (strcmp(key, "$") == 0)
2503 2578 return expr_to_var_sym(arg, sym);
2504 2579
2505 2580 if (strcmp(key, "*$") == 0) {
2506 2581 if (arg->type == EXPR_PREOP && arg->op == '&') {
2507 2582 arg = strip_expr(arg->unop);
2508 2583 return expr_to_var_sym(arg, sym);
↓ open down ↓ |
3 lines elided |
↑ open up ↑ |
2509 2584 } else {
2510 2585 tmp = expr_to_var_sym(arg, sym);
2511 2586 if (!tmp)
2512 2587 return NULL;
2513 2588 snprintf(buf, sizeof(buf), "*%s", tmp);
2514 2589 free_string(tmp);
2515 2590 return alloc_string(buf);
2516 2591 }
2517 2592 }
2518 2593
2519 - if (key[0] == '*') {
2520 - add_star = true;
2594 + while (key[0] == '*') {
2595 + star_cnt++;
2521 2596 key++;
2522 2597 }
2523 2598
2599 + if (arg->type == EXPR_PREOP && arg->op == '&' && star_cnt) {
2600 + arg = strip_expr(arg->unop);
2601 + star_cnt--;
2602 + }
2603 +
2524 2604 if (arg->type == EXPR_PREOP && arg->op == '&') {
2525 2605 arg = strip_expr(arg->unop);
2526 2606 tmp = expr_to_var_sym(arg, sym);
2527 2607 if (!tmp)
2528 2608 return NULL;
2529 - snprintf(buf, sizeof(buf), "%s%s.%s",
2530 - add_star ? "*" : "", tmp, key + 3);
2609 + snprintf(buf, sizeof(buf), "%.*s%s.%s",
2610 + star_cnt, "**********", tmp, key + 3);
2531 2611 return alloc_string(buf);
2532 2612 }
2533 2613
2534 2614 tmp = expr_to_var_sym(arg, sym);
2535 2615 if (!tmp)
2536 2616 return NULL;
2537 - snprintf(buf, sizeof(buf), "%s%s%s", add_star ? "*" : "", tmp, key + 1);
2617 + snprintf(buf, sizeof(buf), "%.*s%s%s", star_cnt, "**********", tmp, key + 1);
2538 2618 free_string(tmp);
2539 2619 return alloc_string(buf);
2540 2620 }
2541 2621
2542 2622 char *get_chunk_from_key(struct expression *arg, char *key, struct symbol **sym, struct var_sym_list **vsl)
2543 2623 {
2544 2624 *vsl = NULL;
2545 2625
2546 2626 if (strcmp("$", key) == 0)
2547 2627 return expr_to_chunk_sym_vsl(arg, sym, vsl);
2548 2628 return get_variable_from_key(arg, key, sym);
2549 2629 }
2550 2630
2551 2631 const char *state_name_to_param_name(const char *state_name, const char *param_name)
2552 2632 {
2633 + int star_cnt = 0;
2553 2634 int name_len;
2554 - static char buf[256];
2555 - bool add_star = false;
2635 + char buf[256];
2556 2636
2557 2637 name_len = strlen(param_name);
2558 2638
2559 - if (state_name[0] == '*') {
2560 - add_star = true;
2639 + while (state_name[0] == '*') {
2640 + star_cnt++;
2561 2641 state_name++;
2562 2642 }
2563 2643
2644 + /* ten out of ten stars! */
2645 + if (star_cnt > 10)
2646 + return NULL;
2647 +
2564 2648 if (strcmp(state_name, param_name) == 0) {
2565 - snprintf(buf, sizeof(buf), "%s$", add_star ? "*" : "");
2649 + snprintf(buf, sizeof(buf), "%.*s$", star_cnt, "**********");
2566 2650 return alloc_sname(buf);
2567 2651 }
2568 2652
2569 2653 if (state_name[name_len] == '-' && /* check for '-' from "->" */
2570 2654 strncmp(state_name, param_name, name_len) == 0) {
2571 - snprintf(buf, sizeof(buf), "%s$%s",
2572 - add_star ? "*" : "", state_name + name_len);
2655 + snprintf(buf, sizeof(buf), "%.*s$%s", star_cnt, "**********", state_name + name_len);
2573 2656 return alloc_sname(buf);
2574 2657 }
2575 2658 return NULL;
2576 2659 }
2577 2660
2578 2661 const char *get_param_name_var_sym(const char *name, struct symbol *sym)
2579 2662 {
2580 2663 if (!sym || !sym->ident)
2581 2664 return NULL;
2582 2665
2583 2666 return state_name_to_param_name(name, sym->ident->name);
2584 2667 }
2585 2668
2586 2669 const char *get_mtag_name_var_sym(const char *state_name, struct symbol *sym)
2587 2670 {
2588 2671 struct symbol *type;
2589 2672 const char *sym_name;
2590 2673 int name_len;
2591 2674 static char buf[256];
2592 2675
2593 2676 /*
2594 2677 * mtag_name is different from param_name because mtags can be a struct
2595 2678 * instead of a struct pointer. But we want to treat it like a pointer
2596 2679 * because really an mtag is a pointer. Or in other words, if you pass
2597 2680 * a struct foo then you want to talk about foo.bar but with an mtag
2598 2681 * you want to refer to it as foo->bar.
2599 2682 *
2600 2683 */
2601 2684
2602 2685 if (!sym || !sym->ident)
2603 2686 return NULL;
2604 2687
2605 2688 type = get_real_base_type(sym);
2606 2689 if (type && type->type == SYM_BASETYPE)
2607 2690 return "*$";
2608 2691
2609 2692 sym_name = sym->ident->name;
2610 2693 name_len = strlen(sym_name);
2611 2694
2612 2695 if (state_name[name_len] == '.' && /* check for '-' from "->" */
2613 2696 strncmp(state_name, sym_name, name_len) == 0) {
2614 2697 snprintf(buf, sizeof(buf), "$->%s", state_name + name_len + 1);
2615 2698 return buf;
2616 2699 }
2617 2700
2618 2701 return state_name_to_param_name(state_name, sym_name);
2619 2702 }
2620 2703
2621 2704 const char *get_mtag_name_expr(struct expression *expr)
2622 2705 {
2623 2706 char *name;
2624 2707 struct symbol *sym;
2625 2708 const char *ret = NULL;
2626 2709
2627 2710 name = expr_to_var_sym(expr, &sym);
2628 2711 if (!name || !sym)
2629 2712 goto free;
2630 2713
2631 2714 ret = get_mtag_name_var_sym(name, sym);
2632 2715 free:
2633 2716 free_string(name);
2634 2717 return ret;
2635 2718 }
2636 2719
2637 2720 const char *get_param_name(struct sm_state *sm)
2638 2721 {
2639 2722 return get_param_name_var_sym(sm->name, sm->sym);
2640 2723 }
2641 2724
2642 2725 char *get_data_info_name(struct expression *expr)
2643 2726 {
2644 2727 struct symbol *sym;
2645 2728 char *name;
2646 2729 char buf[256];
2647 2730 char *ret = NULL;
2648 2731
2649 2732 expr = strip_expr(expr);
2650 2733 name = get_member_name(expr);
2651 2734 if (name)
2652 2735 return name;
2653 2736 name = expr_to_var_sym(expr, &sym);
2654 2737 if (!name || !sym)
2655 2738 goto free;
2656 2739 if (!(sym->ctype.modifiers & MOD_TOPLEVEL))
2657 2740 goto free;
2658 2741 if (sym->ctype.modifiers & MOD_STATIC)
2659 2742 snprintf(buf, sizeof(buf), "static %s", name);
2660 2743 else
2661 2744 snprintf(buf, sizeof(buf), "global %s", name);
2662 2745 ret = alloc_sname(buf);
2663 2746 free:
2664 2747 free_string(name);
2665 2748 return ret;
2666 2749 }
↓ open down ↓ |
84 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX