locking: add new down_timeout() function
[smatch.git] / smatch_extra.c
blob5c26262818aad8e1ad768aa1ec4f216bc3960b1e
1 /*
2 * Copyright (C) 2008 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
19 * smatch_extra.c is supposed to track the value of every variable.
23 #include <stdlib.h>
24 #include <errno.h>
25 #ifndef __USE_ISOC99
26 #define __USE_ISOC99
27 #endif
28 #include <limits.h>
29 #include "parse.h"
30 #include "smatch.h"
31 #include "smatch_slist.h"
32 #include "smatch_extra.h"
34 static int my_id;
36 struct string_list *__ignored_macros = NULL;
37 static int in_warn_on_macro(void)
39 struct statement *stmt;
40 char *tmp;
41 char *macro;
43 stmt = get_current_statement();
44 if (!stmt)
45 return 0;
46 macro = get_macro_name(stmt->pos);
47 if (!macro)
48 return 0;
50 FOR_EACH_PTR(__ignored_macros, tmp) {
51 if (!strcmp(tmp, macro))
52 return 1;
53 } END_FOR_EACH_PTR(tmp);
54 return 0;
57 typedef void (mod_hook)(const char *name, struct symbol *sym, struct smatch_state *state);
58 DECLARE_PTR_LIST(void_fn_list, mod_hook *);
59 static struct void_fn_list *extra_mod_hooks;
60 void add_extra_mod_hook(mod_hook *fn)
62 mod_hook **p = malloc(sizeof(mod_hook *));
63 *p = fn;
64 add_ptr_list(&extra_mod_hooks, p);
67 void call_extra_mod_hooks(const char *name, struct symbol *sym, struct smatch_state *state)
69 mod_hook **fn;
71 FOR_EACH_PTR(extra_mod_hooks, fn) {
72 (*fn)(name, sym, state);
73 } END_FOR_EACH_PTR(fn);
76 struct sm_state *set_extra_mod_helper(const char *name, struct symbol *sym, struct smatch_state *state)
78 remove_from_equiv(name, sym);
79 call_extra_mod_hooks(name, sym, state);
80 if (__in_fake_assign && estate_is_unknown(state) && !get_state(SMATCH_EXTRA, name, sym))
81 return NULL;
82 return set_state(SMATCH_EXTRA, name, sym, state);
85 static char *get_other_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym)
87 struct expression *assigned;
88 char *orig_name = NULL;
89 char buf[256];
90 char *ret = NULL;
91 int skip;
93 *new_sym = NULL;
95 if (!sym->ident)
96 return NULL;
98 skip = strlen(sym->ident->name);
99 if (name[skip] != '-' || name[skip + 1] != '>')
100 return NULL;
101 skip += 2;
103 assigned = get_assigned_expr_name_sym(sym->ident->name, sym);
104 if (!assigned)
105 return NULL;
106 if (assigned->type == EXPR_PREOP || assigned->op == '&') {
108 orig_name = expr_to_var_sym(assigned, new_sym);
109 if (!orig_name || !*new_sym)
110 goto free;
112 snprintf(buf, sizeof(buf), "%s.%s", orig_name + 1, name + skip);
113 ret = alloc_string(buf);
114 free_string(orig_name);
115 return ret;
118 if (assigned->type != EXPR_DEREF)
119 goto free;
121 orig_name = expr_to_var_sym(assigned, new_sym);
122 if (!orig_name || !*new_sym)
123 goto free;
125 snprintf(buf, sizeof(buf), "%s->%s", orig_name, name + skip);
126 ret = alloc_string(buf);
127 free_string(orig_name);
128 return ret;
130 free:
131 free_string(orig_name);
132 return NULL;
135 struct sm_state *set_extra_mod(const char *name, struct symbol *sym, struct smatch_state *state)
137 char *new_name;
138 struct symbol *new_sym;
139 struct sm_state *sm;
141 if (in_warn_on_macro())
142 return NULL;
143 sm = set_extra_mod_helper(name, sym, state);
144 new_name = get_other_name_sym(name, sym, &new_sym);
145 if (new_name && new_sym)
146 set_extra_mod_helper(new_name, new_sym, state);
147 free_string(new_name);
148 return sm;
151 struct sm_state *set_extra_expr_mod(struct expression *expr, struct smatch_state *state)
153 struct symbol *sym;
154 char *name;
155 struct sm_state *ret = NULL;
157 expr = strip_expr(expr);
158 name = expr_to_var_sym(expr, &sym);
159 if (!name || !sym)
160 goto free;
161 ret = set_extra_mod(name, sym, state);
162 free:
163 free_string(name);
164 return ret;
167 void set_extra_nomod(const char *name, struct symbol *sym, struct smatch_state *state)
169 char *new_name;
170 struct symbol *new_sym;
171 struct relation *rel;
172 struct smatch_state *orig_state;
174 orig_state = get_state(SMATCH_EXTRA, name, sym);
176 new_name = get_other_name_sym(name, sym, &new_sym);
177 if (new_name && new_sym)
178 set_state(SMATCH_EXTRA, new_name, new_sym, state);
179 free_string(new_name);
181 if (!estate_related(orig_state)) {
182 set_state(SMATCH_EXTRA, name, sym, state);
183 return;
186 set_related(state, estate_related(orig_state));
187 FOR_EACH_PTR(estate_related(orig_state), rel) {
188 if (option_debug_related)
189 sm_msg("%s updating related %s to %s", name, rel->name, state->name);
190 set_state(SMATCH_EXTRA, rel->name, rel->sym, state);
191 } END_FOR_EACH_PTR(rel);
195 * This is for return_implies_state() hooks which modify a SMATCH_EXTRA state
197 void set_extra_expr_nomod(struct expression *expr, struct smatch_state *state)
199 char *name;
200 struct symbol *sym;
202 name = expr_to_var_sym(expr, &sym);
203 if (!name || !sym)
204 goto free;
205 set_extra_nomod(name, sym, state);
206 free:
207 free_string(name);
211 static void set_extra_true_false(const char *name, struct symbol *sym,
212 struct smatch_state *true_state,
213 struct smatch_state *false_state)
215 char *new_name;
216 struct symbol *new_sym;
217 struct relation *rel;
218 struct smatch_state *orig_state;
220 if (in_warn_on_macro())
221 return;
223 new_name = get_other_name_sym(name, sym, &new_sym);
224 if (new_name && new_sym)
225 set_true_false_states(SMATCH_EXTRA, new_name, new_sym, true_state, false_state);
226 free_string(new_name);
228 orig_state = get_state(SMATCH_EXTRA, name, sym);
230 if (!estate_related(orig_state)) {
231 set_true_false_states(SMATCH_EXTRA, name, sym, true_state, false_state);
232 return;
235 if (true_state)
236 set_related(true_state, estate_related(orig_state));
237 if (false_state)
238 set_related(false_state, estate_related(orig_state));
240 FOR_EACH_PTR(estate_related(orig_state), rel) {
241 set_true_false_states(SMATCH_EXTRA, rel->name, rel->sym,
242 true_state, false_state);
243 } END_FOR_EACH_PTR(rel);
246 static void set_extra_expr_true_false(struct expression *expr,
247 struct smatch_state *true_state,
248 struct smatch_state *false_state)
250 char *name;
251 struct symbol *sym;
253 expr = strip_expr(expr);
254 name = expr_to_var_sym(expr, &sym);
255 if (!name || !sym)
256 goto free;
257 set_extra_true_false(name, sym, true_state, false_state);
258 free:
259 free_string(name);
262 static struct sm_state *handle_canonical_while_count_down(struct statement *loop)
264 struct expression *iter_var;
265 struct expression *condition;
266 struct sm_state *sm;
267 struct smatch_state *estate;
268 sval_t start;
270 condition = strip_expr(loop->iterator_pre_condition);
271 if (!condition)
272 return NULL;
273 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
274 return NULL;
275 if (condition->op != SPECIAL_DECREMENT)
276 return NULL;
278 iter_var = condition->unop;
279 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
280 if (!sm)
281 return NULL;
282 if (sval_cmp_val(estate_min(sm->state), 0) < 0)
283 return NULL;
284 start = estate_max(sm->state);
285 if (sval_cmp_val(start, 0) <= 0)
286 return NULL;
287 if (!sval_is_max(start))
288 start.value--;
290 if (condition->type == EXPR_PREOP) {
291 estate = alloc_estate_range(sval_type_val(start.type, 1), start);
292 if (estate_has_hard_max(sm->state))
293 estate_set_hard_max(estate);
294 estate_copy_fuzzy_max(estate, sm->state);
295 set_extra_expr_mod(iter_var, estate);
297 if (condition->type == EXPR_POSTOP) {
298 estate = alloc_estate_range(sval_type_val(start.type, 0), start);
299 if (estate_has_hard_max(sm->state))
300 estate_set_hard_max(estate);
301 estate_copy_fuzzy_max(estate, sm->state);
302 set_extra_expr_mod(iter_var, estate);
304 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
307 static struct sm_state *handle_canonical_for_inc(struct expression *iter_expr,
308 struct expression *condition)
310 struct expression *iter_var;
311 struct sm_state *sm;
312 struct smatch_state *estate;
313 sval_t start, end, max;
315 iter_var = iter_expr->unop;
316 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
317 if (!sm)
318 return NULL;
319 if (!estate_get_single_value(sm->state, &start))
320 return NULL;
321 if (get_implied_max(condition->right, &end))
322 end = sval_cast(get_type(iter_var), end);
323 else
324 end = sval_type_max(get_type(iter_var));
326 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
327 return NULL;
329 switch (condition->op) {
330 case SPECIAL_UNSIGNED_LT:
331 case SPECIAL_NOTEQUAL:
332 case '<':
333 if (!sval_is_min(end))
334 end.value--;
335 break;
336 case SPECIAL_UNSIGNED_LTE:
337 case SPECIAL_LTE:
338 break;
339 default:
340 return NULL;
342 if (sval_cmp(end, start) < 0)
343 return NULL;
344 estate = alloc_estate_range(start, end);
345 if (get_hard_max(condition->right, &max)) {
346 estate_set_hard_max(estate);
347 if (condition->op == '<' ||
348 condition->op == SPECIAL_UNSIGNED_LT ||
349 condition->op == SPECIAL_NOTEQUAL)
350 max.value--;
351 estate_set_fuzzy_max(estate, max);
353 set_extra_expr_mod(iter_var, estate);
354 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
357 static struct sm_state *handle_canonical_for_dec(struct expression *iter_expr,
358 struct expression *condition)
360 struct expression *iter_var;
361 struct sm_state *sm;
362 struct smatch_state *estate;
363 sval_t start, end;
365 iter_var = iter_expr->unop;
366 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
367 if (!sm)
368 return NULL;
369 if (!estate_get_single_value(sm->state, &start))
370 return NULL;
371 if (!get_implied_min(condition->right, &end))
372 end = sval_type_min(get_type(iter_var));
373 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
374 return NULL;
376 switch (condition->op) {
377 case SPECIAL_NOTEQUAL:
378 case '>':
379 if (!sval_is_min(end) && !sval_is_max(end))
380 end.value++;
381 break;
382 case SPECIAL_GTE:
383 break;
384 default:
385 return NULL;
387 if (sval_cmp(end, start) > 0)
388 return NULL;
389 estate = alloc_estate_range(end, start);
390 estate_set_hard_max(estate);
391 estate_set_fuzzy_max(estate, estate_get_fuzzy_max(estate));
392 set_extra_expr_mod(iter_var, estate);
393 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
396 static struct sm_state *handle_canonical_for_loops(struct statement *loop)
398 struct expression *iter_expr;
399 struct expression *condition;
401 if (!loop->iterator_post_statement)
402 return NULL;
403 if (loop->iterator_post_statement->type != STMT_EXPRESSION)
404 return NULL;
405 iter_expr = loop->iterator_post_statement->expression;
406 if (!loop->iterator_pre_condition)
407 return NULL;
408 if (loop->iterator_pre_condition->type != EXPR_COMPARE)
409 return NULL;
410 condition = loop->iterator_pre_condition;
412 if (iter_expr->op == SPECIAL_INCREMENT)
413 return handle_canonical_for_inc(iter_expr, condition);
414 if (iter_expr->op == SPECIAL_DECREMENT)
415 return handle_canonical_for_dec(iter_expr, condition);
416 return NULL;
419 struct sm_state *__extra_handle_canonical_loops(struct statement *loop, struct stree **stree)
421 struct sm_state *ret;
423 __push_fake_cur_stree();
424 if (!loop->iterator_post_statement)
425 ret = handle_canonical_while_count_down(loop);
426 else
427 ret = handle_canonical_for_loops(loop);
428 *stree = __pop_fake_cur_stree();
429 return ret;
432 int __iterator_unchanged(struct sm_state *sm)
434 if (!sm)
435 return 0;
436 if (get_sm_state(my_id, sm->name, sm->sym) == sm)
437 return 1;
438 return 0;
441 static void while_count_down_after(struct sm_state *sm, struct expression *condition)
443 sval_t after_value;
445 /* paranoid checking. prolly not needed */
446 condition = strip_expr(condition);
447 if (!condition)
448 return;
449 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
450 return;
451 if (condition->op != SPECIAL_DECREMENT)
452 return;
453 after_value = estate_min(sm->state);
454 after_value.value--;
455 set_extra_mod(sm->name, sm->sym, alloc_estate_sval(after_value));
458 void __extra_pre_loop_hook_after(struct sm_state *sm,
459 struct statement *iterator,
460 struct expression *condition)
462 struct expression *iter_expr;
463 sval_t limit;
464 struct smatch_state *state;
466 if (!iterator) {
467 while_count_down_after(sm, condition);
468 return;
471 iter_expr = iterator->expression;
473 if (condition->type != EXPR_COMPARE)
474 return;
475 if (iter_expr->op == SPECIAL_INCREMENT) {
476 limit = sval_binop(estate_max(sm->state), '+',
477 sval_type_val(estate_type(sm->state), 1));
478 } else {
479 limit = sval_binop(estate_min(sm->state), '-',
480 sval_type_val(estate_type(sm->state), 1));
482 if (!estate_has_hard_max(sm->state) && !__has_breaks()) {
483 if (iter_expr->op == SPECIAL_INCREMENT)
484 state = alloc_estate_range(estate_min(sm->state), limit);
485 else
486 state = alloc_estate_range(limit, estate_max(sm->state));
487 } else {
488 state = alloc_estate_sval(limit);
490 if (!estate_has_hard_max(sm->state)) {
491 estate_clear_hard_max(state);
493 if (estate_has_fuzzy_max(sm->state)) {
494 sval_t hmax = estate_get_fuzzy_max(sm->state);
495 sval_t max = estate_max(sm->state);
497 if (sval_cmp(hmax, max) != 0)
498 estate_clear_fuzzy_max(state);
499 } else if (!estate_has_fuzzy_max(sm->state)) {
500 estate_clear_fuzzy_max(state);
503 set_extra_mod(sm->name, sm->sym, state);
506 static struct stree *unmatched_stree;
507 static struct smatch_state *unmatched_state(struct sm_state *sm)
509 struct smatch_state *state;
511 if (unmatched_stree) {
512 state = get_state_stree(unmatched_stree, SMATCH_EXTRA, sm->name, sm->sym);
513 if (state)
514 return state;
516 if (parent_is_gone_var_sym(sm->name, sm->sym))
517 return alloc_estate_empty();
518 return alloc_estate_whole(estate_type(sm->state));
521 static void clear_the_pointed_at(struct expression *expr)
523 struct stree *stree;
524 char *name;
525 struct symbol *sym;
526 struct sm_state *tmp;
528 name = expr_to_var_sym(expr, &sym);
529 if (!name || !sym)
530 goto free;
532 stree = __get_cur_stree();
533 FOR_EACH_MY_SM(SMATCH_EXTRA, stree, tmp) {
534 if (tmp->name[0] != '*')
535 continue;
536 if (tmp->sym != sym)
537 continue;
538 if (strcmp(tmp->name + 1, name) != 0)
539 continue;
540 set_extra_mod(tmp->name, tmp->sym, alloc_estate_whole(estate_type(tmp->state)));
541 } END_FOR_EACH_SM(tmp);
543 free:
544 free_string(name);
547 static void match_function_call(struct expression *expr)
549 struct expression *arg;
550 struct expression *tmp;
552 /* if we have the db this is handled in smatch_function_hooks.c */
553 if (!option_no_db)
554 return;
555 if (inlinable(expr->fn))
556 return;
558 FOR_EACH_PTR(expr->args, arg) {
559 tmp = strip_expr(arg);
560 if (tmp->type == EXPR_PREOP && tmp->op == '&')
561 set_extra_expr_mod(tmp->unop, alloc_estate_whole(get_type(tmp->unop)));
562 else
563 clear_the_pointed_at(tmp);
564 } END_FOR_EACH_PTR(arg);
567 static int types_equiv_or_pointer(struct symbol *one, struct symbol *two)
569 if (!one || !two)
570 return 0;
571 if (one->type == SYM_PTR && two->type == SYM_PTR)
572 return 1;
573 return types_equiv(one, two);
576 static void match_vanilla_assign(struct expression *left, struct expression *right)
578 struct range_list *rl = NULL;
579 struct symbol *right_sym;
580 struct symbol *left_type;
581 struct symbol *right_type;
582 char *right_name = NULL;
583 struct symbol *sym;
584 char *name;
585 sval_t max;
586 struct smatch_state *state;
588 if (is_struct(left))
589 return;
591 name = expr_to_var_sym(left, &sym);
592 if (!name)
593 return;
595 left_type = get_type(left);
596 right_type = get_type(right);
598 right_name = expr_to_var_sym(right, &right_sym);
600 if (!__in_fake_assign &&
601 !(right->type == EXPR_PREOP && right->op == '&') &&
602 right_name && right_sym &&
603 types_equiv_or_pointer(left_type, right_type) &&
604 !has_symbol(right, sym)) {
605 set_equiv(left, right);
606 goto free;
609 if (is_pointer(right) && get_address_rl(right, &rl)) {
610 state = alloc_estate_rl(rl);
611 } else if (get_implied_rl(right, &rl)) {
612 rl = cast_rl(left_type, rl);
613 state = alloc_estate_rl(rl);
614 if (get_hard_max(right, &max)) {
615 estate_set_hard_max(state);
616 estate_set_fuzzy_max(state, max);
618 } else {
619 rl = alloc_whole_rl(right_type);
620 rl = cast_rl(left_type, rl);
621 state = alloc_estate_rl(rl);
623 set_extra_mod(name, sym, state);
624 free:
625 free_string(right_name);
628 static int op_remove_assign(int op)
630 switch (op) {
631 case SPECIAL_ADD_ASSIGN:
632 return '+';
633 case SPECIAL_SUB_ASSIGN:
634 return '-';
635 case SPECIAL_MUL_ASSIGN:
636 return '*';
637 case SPECIAL_DIV_ASSIGN:
638 return '/';
639 case SPECIAL_MOD_ASSIGN:
640 return '%';
641 case SPECIAL_AND_ASSIGN:
642 return '&';
643 case SPECIAL_OR_ASSIGN:
644 return '|';
645 case SPECIAL_XOR_ASSIGN:
646 return '^';
647 case SPECIAL_SHL_ASSIGN:
648 return SPECIAL_LEFTSHIFT;
649 case SPECIAL_SHR_ASSIGN:
650 return SPECIAL_RIGHTSHIFT;
651 default:
652 return op;
656 static void match_assign(struct expression *expr)
658 struct range_list *rl = NULL;
659 struct expression *left;
660 struct expression *right;
661 struct expression *binop_expr;
662 struct symbol *left_type;
663 struct symbol *right_type;
664 struct symbol *sym;
665 char *name;
666 sval_t left_min, left_max;
667 sval_t right_min, right_max;
668 sval_t res_min, res_max;
670 left = strip_expr(expr->left);
672 right = strip_expr(expr->right);
673 while (right->type == EXPR_ASSIGNMENT && right->op == '=')
674 right = strip_expr(right->left);
676 if (expr->op == '=' && is_condition(expr->right))
677 return; /* handled in smatch_condition.c */
678 if (expr->op == '=' && right->type == EXPR_CALL)
679 return; /* handled in smatch_function_hooks.c */
680 if (expr->op == '=') {
681 match_vanilla_assign(left, right);
682 return;
685 name = expr_to_var_sym(left, &sym);
686 if (!name)
687 return;
689 left_type = get_type(left);
690 right_type = get_type(right);
692 res_min = sval_type_min(left_type);
693 res_max = sval_type_max(left_type);
695 switch (expr->op) {
696 case SPECIAL_ADD_ASSIGN:
697 get_absolute_max(left, &left_max);
698 get_absolute_max(right, &right_max);
699 if (sval_binop_overflows(left_max, '+', right_max))
700 break;
701 if (get_implied_min(left, &left_min) &&
702 !sval_is_negative_min(left_min) &&
703 get_implied_min(right, &right_min) &&
704 !sval_is_negative_min(right_min)) {
705 res_min = sval_binop(left_min, '+', right_min);
706 res_min = sval_cast(right_type, res_min);
708 if (inside_loop()) /* we are assuming loops don't lead to wrapping */
709 break;
710 res_max = sval_binop(left_max, '+', right_max);
711 res_max = sval_cast(right_type, res_max);
712 break;
713 case SPECIAL_SUB_ASSIGN:
714 if (get_implied_max(left, &left_max) &&
715 !sval_is_max(left_max) &&
716 get_implied_min(right, &right_min) &&
717 !sval_is_min(right_min)) {
718 res_max = sval_binop(left_max, '-', right_min);
719 res_max = sval_cast(right_type, res_max);
721 if (inside_loop())
722 break;
723 if (get_implied_min(left, &left_min) &&
724 !sval_is_min(left_min) &&
725 get_implied_max(right, &right_max) &&
726 !sval_is_max(right_max)) {
727 res_min = sval_binop(left_min, '-', right_max);
728 res_min = sval_cast(right_type, res_min);
730 break;
731 case SPECIAL_AND_ASSIGN:
732 case SPECIAL_MOD_ASSIGN:
733 case SPECIAL_SHL_ASSIGN:
734 case SPECIAL_SHR_ASSIGN:
735 case SPECIAL_OR_ASSIGN:
736 case SPECIAL_XOR_ASSIGN:
737 case SPECIAL_MUL_ASSIGN:
738 case SPECIAL_DIV_ASSIGN:
739 binop_expr = binop_expression(expr->left,
740 op_remove_assign(expr->op),
741 expr->right);
742 if (get_absolute_rl(binop_expr, &rl)) {
743 rl = cast_rl(left_type, rl);
744 set_extra_mod(name, sym, alloc_estate_rl(rl));
745 goto free;
747 break;
749 rl = cast_rl(left_type, alloc_rl(res_min, res_max));
750 set_extra_mod(name, sym, alloc_estate_rl(rl));
751 free:
752 free_string(name);
755 static struct smatch_state *increment_state(struct smatch_state *state)
757 sval_t min = estate_min(state);
758 sval_t max = estate_max(state);
760 if (!estate_rl(state))
761 return NULL;
763 if (inside_loop())
764 max = sval_type_max(max.type);
766 if (!sval_is_min(min) && !sval_is_max(min))
767 min.value++;
768 if (!sval_is_min(max) && !sval_is_max(max))
769 max.value++;
770 return alloc_estate_range(min, max);
773 static struct smatch_state *decrement_state(struct smatch_state *state)
775 sval_t min = estate_min(state);
776 sval_t max = estate_max(state);
778 if (!estate_rl(state))
779 return NULL;
781 if (inside_loop())
782 min = sval_type_min(min.type);
784 if (!sval_is_min(min) && !sval_is_max(min))
785 min.value--;
786 if (!sval_is_min(max) && !sval_is_max(max))
787 max.value--;
788 return alloc_estate_range(min, max);
791 static void unop_expr(struct expression *expr)
793 struct smatch_state *state;
795 if (expr->smatch_flags & Handled)
796 return;
798 switch (expr->op) {
799 case SPECIAL_INCREMENT:
800 state = get_state_expr(SMATCH_EXTRA, expr->unop);
801 state = increment_state(state);
802 if (!state)
803 state = alloc_estate_whole(get_type(expr));
804 set_extra_expr_mod(expr->unop, state);
805 break;
806 case SPECIAL_DECREMENT:
807 state = get_state_expr(SMATCH_EXTRA, expr->unop);
808 state = decrement_state(state);
809 if (!state)
810 state = alloc_estate_whole(get_type(expr));
811 set_extra_expr_mod(expr->unop, state);
812 break;
813 default:
814 return;
818 static void asm_expr(struct statement *stmt)
821 struct expression *expr;
822 struct symbol *type;
823 int state = 0;
825 FOR_EACH_PTR(stmt->asm_outputs, expr) {
826 switch (state) {
827 case 0: /* identifier */
828 case 1: /* constraint */
829 state++;
830 continue;
831 case 2: /* expression */
832 state = 0;
833 type = get_type(strip_expr(expr));
834 set_extra_expr_mod(expr, alloc_estate_whole(type));
835 continue;
837 } END_FOR_EACH_PTR(expr);
840 static void check_dereference(struct expression *expr)
842 if (outside_of_function())
843 return;
844 set_extra_expr_nomod(expr, alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval));
847 static void match_dereferences(struct expression *expr)
849 if (expr->type != EXPR_PREOP)
850 return;
851 check_dereference(expr->unop);
854 static void match_pointer_as_array(struct expression *expr)
856 if (!is_array(expr))
857 return;
858 check_dereference(get_array_base(expr));
861 static void set_param_dereferenced(struct expression *arg, char *key, char *unused)
863 struct symbol *sym;
864 char *name;
866 name = get_variable_from_key(arg, key, &sym);
867 if (!name || !sym)
868 goto free;
870 set_extra_nomod(name, sym, alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval));
872 free:
873 free_string(name);
876 static sval_t add_one(sval_t sval)
878 sval.value++;
879 return sval;
882 static sval_t sub_one(sval_t sval)
884 sval.value--;
885 return sval;
888 static void handle_comparison(struct symbol *type, struct expression *left, int op, struct expression *right)
890 struct range_list *left_orig;
891 struct range_list *left_true;
892 struct range_list *left_false;
893 struct range_list *right_orig;
894 struct range_list *right_true;
895 struct range_list *right_false;
896 struct smatch_state *left_true_state;
897 struct smatch_state *left_false_state;
898 struct smatch_state *right_true_state;
899 struct smatch_state *right_false_state;
900 sval_t min, max, dummy, hard_max;
901 int left_postop = 0;
902 int right_postop = 0;
904 if (left->op == SPECIAL_INCREMENT || left->op == SPECIAL_DECREMENT) {
905 if (left->type == EXPR_POSTOP) {
906 left->smatch_flags |= Handled;
907 left_postop = left->op;
909 left = strip_expr(left->unop);
911 while (left->type == EXPR_ASSIGNMENT)
912 left = strip_expr(left->left);
914 if (right->op == SPECIAL_INCREMENT || right->op == SPECIAL_DECREMENT) {
915 if (right->type == EXPR_POSTOP) {
916 right->smatch_flags |= Handled;
917 right_postop = right->op;
919 right = strip_expr(right->unop);
922 if (get_implied_rl(left, &left_orig)) {
923 left_orig = cast_rl(type, left_orig);
924 } else {
925 min = sval_type_min(get_type(left));
926 max = sval_type_max(get_type(left));
927 left_orig = cast_rl(type, alloc_rl(min, max));
930 if (get_implied_rl(right, &right_orig)) {
931 right_orig = cast_rl(type, right_orig);
932 } else {
933 min = sval_type_min(get_type(right));
934 max = sval_type_max(get_type(right));
935 right_orig = cast_rl(type, alloc_rl(min, max));
937 min = sval_type_min(type);
938 max = sval_type_max(type);
940 left_true = clone_rl(left_orig);
941 left_false = clone_rl(left_orig);
942 right_true = clone_rl(right_orig);
943 right_false = clone_rl(right_orig);
945 switch (op) {
946 case '<':
947 case SPECIAL_UNSIGNED_LT:
948 left_true = remove_range(left_orig, rl_max(right_orig), max);
949 if (!sval_is_min(rl_min(right_orig))) {
950 left_false = remove_range(left_orig, min, sub_one(rl_min(right_orig)));
953 right_true = remove_range(right_orig, min, rl_min(left_orig));
954 if (!sval_is_max(rl_max(left_orig)))
955 right_false = remove_range(right_orig, add_one(rl_max(left_orig)), max);
956 break;
957 case SPECIAL_UNSIGNED_LTE:
958 case SPECIAL_LTE:
959 if (!sval_is_max(rl_max(right_orig)))
960 left_true = remove_range(left_orig, add_one(rl_max(right_orig)), max);
961 left_false = remove_range(left_orig, min, rl_min(right_orig));
963 if (!sval_is_min(rl_min(left_orig)))
964 right_true = remove_range(right_orig, min, sub_one(rl_min(left_orig)));
965 right_false = remove_range(right_orig, rl_max(left_orig), max);
967 if (sval_cmp(rl_min(left_orig), rl_min(right_orig)) == 0)
968 left_false = remove_range(left_false, rl_min(left_orig), rl_min(left_orig));
969 if (sval_cmp(rl_max(left_orig), rl_max(right_orig)) == 0)
970 right_false = remove_range(right_false, rl_max(left_orig), rl_max(left_orig));
971 break;
972 case SPECIAL_EQUAL:
973 if (!sval_is_max(rl_max(right_orig))) {
974 left_true = remove_range(left_true, add_one(rl_max(right_orig)), max);
976 if (!sval_is_min(rl_min(right_orig))) {
977 left_true = remove_range(left_true, min, sub_one(rl_min(right_orig)));
979 if (sval_cmp(rl_min(right_orig), rl_max(right_orig)) == 0)
980 left_false = remove_range(left_orig, rl_min(right_orig), rl_min(right_orig));
982 if (!sval_is_max(rl_max(left_orig)))
983 right_true = remove_range(right_true, add_one(rl_max(left_orig)), max);
984 if (!sval_is_min(rl_min(left_orig)))
985 right_true = remove_range(right_true, min, sub_one(rl_min(left_orig)));
986 if (sval_cmp(rl_min(left_orig), rl_max(left_orig)) == 0)
987 right_false = remove_range(right_orig, rl_min(left_orig), rl_min(left_orig));
988 break;
989 case SPECIAL_UNSIGNED_GTE:
990 case SPECIAL_GTE:
991 if (!sval_is_min(rl_min(right_orig)))
992 left_true = remove_range(left_orig, min, sub_one(rl_min(right_orig)));
993 left_false = remove_range(left_orig, rl_max(right_orig), max);
995 if (!sval_is_max(rl_max(left_orig)))
996 right_true = remove_range(right_orig, add_one(rl_max(left_orig)), max);
997 right_false = remove_range(right_orig, min, rl_min(left_orig));
999 if (sval_cmp(rl_min(left_orig), rl_min(right_orig)) == 0)
1000 right_false = remove_range(right_false, rl_min(left_orig), rl_min(left_orig));
1001 if (sval_cmp(rl_max(left_orig), rl_max(right_orig)) == 0)
1002 left_false = remove_range(left_false, rl_max(left_orig), rl_max(left_orig));
1003 break;
1004 case '>':
1005 case SPECIAL_UNSIGNED_GT:
1006 left_true = remove_range(left_orig, min, rl_min(right_orig));
1007 if (!sval_is_max(rl_max(right_orig)))
1008 left_false = remove_range(left_orig, add_one(rl_max(right_orig)), max);
1010 right_true = remove_range(right_orig, rl_max(left_orig), max);
1011 if (!sval_is_min(rl_min(left_orig)))
1012 right_false = remove_range(right_orig, min, sub_one(rl_min(left_orig)));
1013 break;
1014 case SPECIAL_NOTEQUAL:
1015 if (!sval_is_max(rl_max(right_orig)))
1016 left_false = remove_range(left_false, add_one(rl_max(right_orig)), max);
1017 if (!sval_is_min(rl_min(right_orig)))
1018 left_false = remove_range(left_false, min, sub_one(rl_min(right_orig)));
1019 if (sval_cmp(rl_min(right_orig), rl_max(right_orig)) == 0)
1020 left_true = remove_range(left_orig, rl_min(right_orig), rl_min(right_orig));
1022 if (!sval_is_max(rl_max(left_orig)))
1023 right_false = remove_range(right_false, add_one(rl_max(left_orig)), max);
1024 if (!sval_is_min(rl_min(left_orig)))
1025 right_false = remove_range(right_false, min, sub_one(rl_min(left_orig)));
1026 if (sval_cmp(rl_min(left_orig), rl_max(left_orig)) == 0)
1027 right_true = remove_range(right_orig, rl_min(left_orig), rl_min(left_orig));
1028 break;
1029 default:
1030 return;
1033 left_true = rl_truncate_cast(get_type(left), left_true);
1034 left_false = rl_truncate_cast(get_type(left), left_false);
1035 right_true = rl_truncate_cast(get_type(right), right_true);
1036 right_false = rl_truncate_cast(get_type(right), right_false);
1038 left_true_state = alloc_estate_rl(left_true);
1039 left_false_state = alloc_estate_rl(left_false);
1040 right_true_state = alloc_estate_rl(right_true);
1041 right_false_state = alloc_estate_rl(right_false);
1043 switch (op) {
1044 case '<':
1045 case SPECIAL_UNSIGNED_LT:
1046 case SPECIAL_UNSIGNED_LTE:
1047 case SPECIAL_LTE:
1048 if (get_hard_max(right, &dummy))
1049 estate_set_hard_max(left_true_state);
1050 if (get_hard_max(left, &dummy))
1051 estate_set_hard_max(right_false_state);
1052 break;
1053 case '>':
1054 case SPECIAL_UNSIGNED_GT:
1055 case SPECIAL_UNSIGNED_GTE:
1056 case SPECIAL_GTE:
1057 if (get_hard_max(left, &dummy))
1058 estate_set_hard_max(right_true_state);
1059 if (get_hard_max(right, &dummy))
1060 estate_set_hard_max(left_false_state);
1061 break;
1064 switch (op) {
1065 case '<':
1066 case SPECIAL_UNSIGNED_LT:
1067 case SPECIAL_UNSIGNED_LTE:
1068 case SPECIAL_LTE:
1069 if (get_hard_max(right, &hard_max)) {
1070 if (op == '<' || op == SPECIAL_UNSIGNED_LT)
1071 hard_max.value--;
1072 estate_set_fuzzy_max(left_true_state, hard_max);
1074 if (get_implied_value(right, &hard_max)) {
1075 if (op == SPECIAL_UNSIGNED_LTE ||
1076 op == SPECIAL_LTE)
1077 hard_max.value++;
1078 estate_set_fuzzy_max(left_false_state, hard_max);
1080 if (get_hard_max(left, &hard_max)) {
1081 if (op == SPECIAL_UNSIGNED_LTE ||
1082 op == SPECIAL_LTE)
1083 hard_max.value--;
1084 estate_set_fuzzy_max(right_false_state, hard_max);
1086 if (get_implied_value(left, &hard_max)) {
1087 if (op == '<' || op == SPECIAL_UNSIGNED_LT)
1088 hard_max.value++;
1089 estate_set_fuzzy_max(right_true_state, hard_max);
1091 break;
1092 case '>':
1093 case SPECIAL_UNSIGNED_GT:
1094 case SPECIAL_UNSIGNED_GTE:
1095 case SPECIAL_GTE:
1096 if (get_hard_max(left, &hard_max)) {
1097 if (op == '>' || op == SPECIAL_UNSIGNED_GT)
1098 hard_max.value--;
1099 estate_set_fuzzy_max(right_true_state, hard_max);
1101 if (get_implied_value(left, &hard_max)) {
1102 if (op == SPECIAL_UNSIGNED_GTE ||
1103 op == SPECIAL_GTE)
1104 hard_max.value++;
1105 estate_set_fuzzy_max(right_false_state, hard_max);
1107 if (get_hard_max(right, &hard_max)) {
1108 if (op == SPECIAL_UNSIGNED_LTE ||
1109 op == SPECIAL_LTE)
1110 hard_max.value--;
1111 estate_set_fuzzy_max(left_false_state, hard_max);
1113 if (get_implied_value(right, &hard_max)) {
1114 if (op == '>' ||
1115 op == SPECIAL_UNSIGNED_GT)
1116 hard_max.value++;
1117 estate_set_fuzzy_max(left_true_state, hard_max);
1119 break;
1120 case SPECIAL_EQUAL:
1121 if (get_hard_max(left, &hard_max))
1122 estate_set_fuzzy_max(right_true_state, hard_max);
1123 if (get_hard_max(right, &hard_max))
1124 estate_set_fuzzy_max(left_true_state, hard_max);
1125 break;
1128 if (get_hard_max(left, &hard_max)) {
1129 estate_set_hard_max(left_true_state);
1130 estate_set_hard_max(left_false_state);
1132 if (get_hard_max(right, &hard_max)) {
1133 estate_set_hard_max(right_true_state);
1134 estate_set_hard_max(right_false_state);
1137 if (left_postop == SPECIAL_INCREMENT) {
1138 left_true_state = increment_state(left_true_state);
1139 left_false_state = increment_state(left_false_state);
1141 if (left_postop == SPECIAL_DECREMENT) {
1142 left_true_state = decrement_state(left_true_state);
1143 left_false_state = decrement_state(left_false_state);
1145 if (right_postop == SPECIAL_INCREMENT) {
1146 right_true_state = increment_state(right_true_state);
1147 right_false_state = increment_state(right_false_state);
1149 if (right_postop == SPECIAL_DECREMENT) {
1150 right_true_state = decrement_state(right_true_state);
1151 right_false_state = decrement_state(right_false_state);
1154 if (estate_rl(left_true_state) && estates_equiv(left_true_state, left_false_state)) {
1155 left_true_state = NULL;
1156 left_false_state = NULL;
1159 if (estate_rl(right_true_state) && estates_equiv(right_true_state, right_false_state)) {
1160 right_true_state = NULL;
1161 right_false_state = NULL;
1164 set_extra_expr_true_false(left, left_true_state, left_false_state);
1165 set_extra_expr_true_false(right, right_true_state, right_false_state);
1168 static int is_simple_math(struct expression *expr)
1170 if (!expr)
1171 return 0;
1172 if (expr->type != EXPR_BINOP)
1173 return 0;
1174 switch (expr->op) {
1175 case '+':
1176 case '-':
1177 case '*':
1178 return 1;
1180 return 0;
1183 static void move_known_values(struct expression **left_p, struct expression **right_p)
1185 struct expression *left = *left_p;
1186 struct expression *right = *right_p;
1187 sval_t sval;
1189 if (get_implied_value(left, &sval)) {
1190 if (!is_simple_math(right))
1191 return;
1192 if (right->op == '*') {
1193 sval_t divisor;
1195 if (!get_value(right->right, &divisor))
1196 return;
1197 if (divisor.value == 0 && sval.value % divisor.value)
1198 return;
1199 *left_p = binop_expression(left, invert_op(right->op), right->right);
1200 *right_p = right->left;
1201 return;
1203 if (right->op == '+' && get_value(right->left, &sval)) {
1204 *left_p = binop_expression(left, invert_op(right->op), right->left);
1205 *right_p = right->right;
1206 return;
1208 if (get_value(right->right, &sval)) {
1209 *left_p = binop_expression(left, invert_op(right->op), right->right);
1210 *right_p = right->left;
1211 return;
1213 return;
1215 if (get_implied_value(right, &sval)) {
1216 if (!is_simple_math(left))
1217 return;
1218 if (left->op == '*') {
1219 sval_t divisor;
1221 if (!get_value(left->right, &divisor))
1222 return;
1223 if (divisor.value == 0 && sval.value % divisor.value)
1224 return;
1225 *right_p = binop_expression(right, invert_op(left->op), left->right);
1226 *left_p = left->left;
1227 return;
1229 if (left->op == '+' && get_value(left->left, &sval)) {
1230 *right_p = binop_expression(right, invert_op(left->op), left->left);
1231 *left_p = left->right;
1232 return;
1235 if (get_value(left->right, &sval)) {
1236 *right_p = binop_expression(right, invert_op(left->op), left->right);
1237 *left_p = left->left;
1238 return;
1240 return;
1244 static int match_func_comparison(struct expression *expr)
1246 struct expression *left = strip_expr(expr->left);
1247 struct expression *right = strip_expr(expr->right);
1248 sval_t sval;
1251 * fixme: think about this harder. We should always be trying to limit
1252 * the non-call side as well. If we can't determine the limitter does
1253 * that mean we aren't querying the database and are missing important
1254 * information?
1257 if (left->type == EXPR_CALL) {
1258 if (get_implied_value(left, &sval)) {
1259 handle_comparison(get_type(expr), left, expr->op, right);
1260 return 1;
1262 function_comparison(left, expr->op, right);
1263 return 1;
1266 if (right->type == EXPR_CALL) {
1267 if (get_implied_value(right, &sval)) {
1268 handle_comparison(get_type(expr), left, expr->op, right);
1269 return 1;
1271 function_comparison(left, expr->op, right);
1272 return 1;
1275 return 0;
1278 static void match_comparison(struct expression *expr)
1280 struct expression *left_orig = strip_expr(expr->left);
1281 struct expression *right_orig = strip_expr(expr->right);
1282 struct expression *left, *right;
1283 struct expression *prev;
1284 struct symbol *type;
1286 if (match_func_comparison(expr))
1287 return;
1289 type = get_type(expr);
1290 if (!type)
1291 type = &llong_ctype;
1293 left = left_orig;
1294 right = right_orig;
1295 move_known_values(&left, &right);
1296 handle_comparison(type, left, expr->op, right);
1298 prev = get_assigned_expr(left_orig);
1299 if (is_simple_math(prev) && has_variable(prev, left_orig) == 0) {
1300 left = prev;
1301 right = right_orig;
1302 move_known_values(&left, &right);
1303 handle_comparison(type, left, expr->op, right);
1306 prev = get_assigned_expr(right_orig);
1307 if (is_simple_math(prev) && has_variable(prev, right_orig) == 0) {
1308 left = left_orig;
1309 right = prev;
1310 move_known_values(&left, &right);
1311 handle_comparison(type, left, expr->op, right);
1315 static void handle_AND_condition(struct expression *expr)
1317 struct range_list *rl = NULL;
1318 sval_t known;
1320 if (get_implied_value(expr->left, &known) && known.value > 0) {
1321 known.value--;
1322 get_absolute_rl(expr->right, &rl);
1323 rl = remove_range(rl, sval_type_val(known.type, 0), known);
1324 set_extra_expr_true_false(expr->right, alloc_estate_rl(rl), NULL);
1325 return;
1328 if (get_implied_value(expr->right, &known) && known.value > 0) {
1329 known.value--;
1330 get_absolute_rl(expr->left, &rl);
1331 rl = remove_range(rl, sval_type_val(known.type, 0), known);
1332 set_extra_expr_true_false(expr->left, alloc_estate_rl(rl), NULL);
1333 return;
1337 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
1338 void __extra_match_condition(struct expression *expr)
1340 struct symbol *sym;
1341 char *name;
1342 struct smatch_state *pre_state;
1343 struct smatch_state *true_state;
1344 struct smatch_state *false_state;
1346 expr = strip_expr(expr);
1347 switch (expr->type) {
1348 case EXPR_CALL:
1349 function_comparison(expr, SPECIAL_NOTEQUAL, zero_expr());
1350 return;
1351 case EXPR_PREOP:
1352 case EXPR_SYMBOL:
1353 case EXPR_DEREF: {
1354 sval_t zero;
1356 zero = sval_blank(expr);
1357 zero.value = 0;
1359 name = expr_to_var_sym(expr, &sym);
1360 if (!name)
1361 return;
1362 pre_state = get_state(my_id, name, sym);
1363 true_state = estate_filter_sval(pre_state, zero);
1364 if (possibly_true(expr, SPECIAL_EQUAL, zero_expr()))
1365 false_state = alloc_estate_sval(zero);
1366 else
1367 false_state = alloc_estate_empty();
1368 set_extra_true_false(name, sym, true_state, false_state);
1369 free_string(name);
1370 return;
1372 case EXPR_COMPARE:
1373 match_comparison(expr);
1374 return;
1375 case EXPR_ASSIGNMENT:
1376 __extra_match_condition(expr->left);
1377 return;
1378 case EXPR_BINOP:
1379 if (expr->op == '&')
1380 handle_AND_condition(expr);
1381 return;
1385 static void assume_indexes_are_valid(struct expression *expr)
1387 struct expression *array_expr;
1388 int array_size;
1389 struct expression *offset;
1390 struct symbol *offset_type;
1391 struct range_list *rl_before;
1392 struct range_list *rl_after;
1393 struct range_list *filter = NULL;
1394 sval_t size;
1396 expr = strip_expr(expr);
1397 if (!is_array(expr))
1398 return;
1400 offset = get_array_offset(expr);
1401 offset_type = get_type(offset);
1402 if (offset_type && type_signed(offset_type)) {
1403 filter = alloc_rl(sval_type_min(offset_type),
1404 sval_type_val(offset_type, -1));
1407 array_expr = get_array_base(expr);
1408 array_size = get_real_array_size(array_expr);
1409 if (array_size > 1) {
1410 size = sval_type_val(offset_type, array_size);
1411 add_range(&filter, size, sval_type_max(offset_type));
1414 if (!filter)
1415 return;
1416 get_absolute_rl(offset, &rl_before);
1417 rl_after = rl_filter(rl_before, filter);
1418 if (rl_equiv(rl_before, rl_after))
1419 return;
1420 set_extra_expr_nomod(offset, alloc_estate_rl(rl_after));
1423 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
1424 int implied_not_equal(struct expression *expr, long long val)
1426 return !possibly_false(expr, SPECIAL_NOTEQUAL, value_expr(val));
1429 int implied_not_equal_name_sym(char *name, struct symbol *sym, long long val)
1431 struct smatch_state *estate;
1433 estate = get_state(SMATCH_EXTRA, name, sym);
1434 if (!estate)
1435 return 0;
1436 if (!rl_has_sval(estate_rl(estate), sval_type_val(estate_type(estate), 0)))
1437 return 1;
1438 return 0;
1441 int parent_is_null_var_sym(const char *name, struct symbol *sym)
1443 char buf[256];
1444 char *start;
1445 char *end;
1446 struct smatch_state *state;
1448 strncpy(buf, name, sizeof(buf) - 1);
1449 buf[sizeof(buf) - 1] = '\0';
1451 start = &buf[0];
1452 while (*start == '*') {
1453 start++;
1454 state = get_state(SMATCH_EXTRA, start, sym);
1455 if (!state)
1456 continue;
1457 if (!estate_rl(state))
1458 return 1;
1459 if (estate_min(state).value == 0 &&
1460 estate_max(state).value == 0)
1461 return 1;
1464 start = &buf[0];
1465 while (*start == '&')
1466 start++;
1468 while ((end = strrchr(start, '-'))) {
1469 *end = '\0';
1470 state = get_state(SMATCH_EXTRA, start, sym);
1471 if (!state)
1472 continue;
1473 if (estate_min(state).value == 0 &&
1474 estate_max(state).value == 0)
1475 return 1;
1477 return 0;
1480 int parent_is_null(struct expression *expr)
1482 struct symbol *sym;
1483 char *var;
1484 int ret = 0;
1486 expr = strip_expr(expr);
1487 var = expr_to_var_sym(expr, &sym);
1488 if (!var || !sym)
1489 goto free;
1490 ret = parent_is_null_var_sym(var, sym);
1491 free:
1492 free_string(var);
1493 return ret;
1496 static int param_used_callback(void *found, int argc, char **argv, char **azColName)
1498 *(int *)found = 1;
1499 return 0;
1502 static int filter_unused_kzalloc_info(struct expression *call, int param, char *printed_name, struct sm_state *sm)
1504 sval_t sval;
1505 int found = 0;
1507 /* for function pointers assume everything is used */
1508 if (call->fn->type != EXPR_SYMBOL)
1509 return 0;
1512 * kzalloc() information is treated as special because so there is just
1513 * a lot of stuff initialized to zero and it makes building the database
1514 * take hours and hours.
1516 * In theory, we should just remove this line and not pass any unused
1517 * information, but I'm not sure enough that this code works so I want
1518 * to hold off on that for now.
1520 if (!estate_get_single_value(sm->state, &sval) || sval.value != 0)
1521 return 0;
1523 run_sql(&param_used_callback, &found,
1524 "select * from call_implies where %s and type = %d and parameter = %d and key = '%s';",
1525 get_static_filter(call->fn->symbol), PARAM_USED, param, printed_name);
1526 if (found)
1527 return 0;
1529 /* If the database is not built yet, then assume everything is used */
1530 run_sql(&param_used_callback, &found,
1531 "select * from call_implies where %s and type = %d;",
1532 get_static_filter(call->fn->symbol), PARAM_USED);
1533 if (!found)
1534 return 0;
1536 return 1;
1539 static void struct_member_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
1541 if (estate_is_whole(sm->state))
1542 return;
1543 if (filter_unused_kzalloc_info(call, param, printed_name, sm))
1544 return;
1545 sql_insert_caller_info(call, PARAM_VALUE, param, printed_name, sm->state->name);
1546 if (estate_has_fuzzy_max(sm->state))
1547 sql_insert_caller_info(call, FUZZY_MAX, param, printed_name,
1548 sval_to_str(estate_get_fuzzy_max(sm->state)));
1551 static void db_limited_before(void)
1553 unmatched_stree = clone_stree(__get_cur_stree());
1556 static void db_limited_after(void)
1558 free_stree(&unmatched_stree);
1561 static void db_param_limit_filter(struct expression *expr, int param, char *key, char *value, enum info_type op)
1563 struct expression *arg;
1564 char *name;
1565 struct symbol *sym;
1566 struct sm_state *sm;
1567 struct symbol *compare_type, *var_type;
1568 struct range_list *rl;
1569 struct range_list *limit;
1570 struct range_list *new;
1572 while (expr->type == EXPR_ASSIGNMENT)
1573 expr = strip_expr(expr->right);
1574 if (expr->type != EXPR_CALL)
1575 return;
1577 arg = get_argument_from_call_expr(expr->args, param);
1578 if (!arg)
1579 return;
1581 name = get_variable_from_key(arg, key, &sym);
1582 if (!name || !sym)
1583 goto free;
1585 if (strcmp(key, "$") == 0)
1586 compare_type = get_arg_type(expr->fn, param);
1587 else
1588 compare_type = get_member_type_from_key(arg, key);
1590 sm = get_sm_state(SMATCH_EXTRA, name, sym);
1591 if (sm)
1592 rl = estate_rl(sm->state);
1593 else
1594 rl = alloc_whole_rl(compare_type);
1596 call_results_to_rl(expr, compare_type, value, &limit);
1597 new = rl_intersection(rl, limit);
1599 var_type = get_member_type_from_key(arg, key);
1600 new = cast_rl(var_type, new);
1602 /* We want to preserve the implications here */
1603 if (sm && rl_equiv(estate_rl(sm->state), new))
1604 __set_sm(sm);
1605 else {
1606 if (op == PARAM_LIMIT)
1607 set_extra_nomod(name, sym, alloc_estate_rl(new));
1608 else
1609 set_extra_mod(name, sym, alloc_estate_rl(new));
1612 free:
1613 free_string(name);
1616 static void db_param_limit(struct expression *expr, int param, char *key, char *value)
1618 db_param_limit_filter(expr, param, key, value, PARAM_LIMIT);
1621 static void db_param_filter(struct expression *expr, int param, char *key, char *value)
1623 db_param_limit_filter(expr, param, key, value, PARAM_FILTER);
1626 static void db_param_add_set(struct expression *expr, int param, char *key, char *value, enum info_type op)
1628 struct expression *arg;
1629 char *name;
1630 struct symbol *sym;
1631 struct symbol *type;
1632 struct smatch_state *state;
1633 struct range_list *new = NULL;
1634 struct range_list *added = NULL;
1636 while (expr->type == EXPR_ASSIGNMENT)
1637 expr = strip_expr(expr->right);
1638 if (expr->type != EXPR_CALL)
1639 return;
1641 arg = get_argument_from_call_expr(expr->args, param);
1642 if (!arg)
1643 return;
1644 type = get_member_type_from_key(arg, key);
1645 name = get_variable_from_key(arg, key, &sym);
1646 if (!name || !sym)
1647 goto free;
1649 state = get_state(SMATCH_EXTRA, name, sym);
1650 if (state)
1651 new = estate_rl(state);
1653 call_results_to_rl(expr, type, value, &added);
1655 if (op == PARAM_SET)
1656 new = added;
1657 else
1658 new = rl_union(new, added);
1660 set_extra_mod(name, sym, alloc_estate_rl(new));
1661 free:
1662 free_string(name);
1665 static void db_param_add(struct expression *expr, int param, char *key, char *value)
1667 db_param_add_set(expr, param, key, value, PARAM_ADD);
1670 static void db_param_set(struct expression *expr, int param, char *key, char *value)
1672 db_param_add_set(expr, param, key, value, PARAM_SET);
1675 static void db_returned_member_info(struct expression *expr, int param, char *key, char *value)
1677 struct symbol *type;
1678 struct symbol *sym;
1679 char *name;
1680 struct range_list *rl;
1682 if (expr->type != EXPR_ASSIGNMENT)
1683 return;
1685 name = return_state_to_var_sym(expr, param, key, &sym);
1686 if (!name || !sym)
1687 goto free;
1688 type = get_member_type_from_key(expr->left, key);
1689 if (!type)
1690 return;
1691 call_results_to_rl(expr->right, type, value, &rl);
1692 set_extra_mod(name, sym, alloc_estate_rl(rl));
1694 free:
1695 free_string(name);
1698 static char *in_terms_of_param_math(struct expression *expr, char *printed_name)
1700 struct symbol *sym;
1701 char *var;
1702 char buf[256];
1703 char *ret = NULL;
1705 var = expr_to_var_sym(expr, &sym);
1706 if (!var || !sym)
1707 goto free;
1708 snprintf(buf, sizeof(buf), "%s%s", var, printed_name + 2);
1710 ret = get_value_in_terms_of_parameter_math_var_sym(buf, sym);
1712 free:
1713 free_string(var);
1714 return ret;
1717 static void returned_member_callback(int return_id, char *return_ranges, struct expression *expr, char *printed_name, struct smatch_state *state)
1719 char buf[256];
1720 char *math_str;
1721 sval_t sval;
1723 /* these are handled in smatch_param_filter/set/limit.c */
1724 if (printed_name[0] != '*' &&
1725 !strchr(printed_name, '.') && !strchr(printed_name, '-'))
1726 return;
1728 if (estate_get_single_value(state, &sval)) {
1729 sql_insert_return_states(return_id, return_ranges, RETURN_VALUE, -1,
1730 printed_name, sval_to_str(sval));
1731 return;
1734 math_str = in_terms_of_param_math(expr, printed_name);
1735 if (math_str) {
1736 snprintf(buf, sizeof(buf), "%s[%s]", state->name, math_str);
1737 sql_insert_return_states(return_id, return_ranges, RETURN_VALUE, -1,
1738 printed_name, buf);
1739 return;
1742 if (estate_is_whole(state))
1743 return;
1745 sql_insert_return_states(return_id, return_ranges, RETURN_VALUE, -1,
1746 printed_name, state->name);
1749 static void match_call_info(struct expression *expr)
1751 struct smatch_state *state;
1752 struct range_list *rl = NULL;
1753 struct expression *arg;
1754 struct symbol *type;
1755 int i = 0;
1757 FOR_EACH_PTR(expr->args, arg) {
1758 type = get_arg_type(expr->fn, i);
1760 if (get_implied_rl(arg, &rl))
1761 rl = cast_rl(type, rl);
1762 else
1763 rl = cast_rl(type, alloc_whole_rl(get_type(arg)));
1765 if (!is_whole_rl(rl))
1766 sql_insert_caller_info(expr, PARAM_VALUE, i, "$", show_rl(rl));
1767 state = get_state_expr(SMATCH_EXTRA, arg);
1768 if (estate_has_fuzzy_max(state)) {
1769 sql_insert_caller_info(expr, FUZZY_MAX, i, "$",
1770 sval_to_str(estate_get_fuzzy_max(state)));
1772 i++;
1773 } END_FOR_EACH_PTR(arg);
1776 static void set_param_value(const char *name, struct symbol *sym, char *key, char *value)
1778 struct range_list *rl = NULL;
1779 struct smatch_state *state;
1780 struct symbol *type;
1781 char fullname[256];
1783 if (strcmp(key, "*$") == 0)
1784 snprintf(fullname, sizeof(fullname), "*%s", name);
1785 else if (strncmp(key, "$", 1) == 0)
1786 snprintf(fullname, 256, "%s%s", name, key + 1);
1787 else
1788 return;
1790 type = get_member_type_from_key(symbol_expression(sym), key);
1791 str_to_rl(type, value, &rl);
1792 state = alloc_estate_rl(rl);
1793 set_state(SMATCH_EXTRA, fullname, sym, state);
1796 static void set_param_hard_max(const char *name, struct symbol *sym, char *key, char *value)
1798 struct range_list *rl = NULL;
1799 struct smatch_state *state;
1800 struct symbol *type;
1801 char fullname[256];
1802 sval_t max;
1804 if (strcmp(key, "*$") == 0)
1805 snprintf(fullname, sizeof(fullname), "*%s", name);
1806 else if (strncmp(key, "$", 1) == 0)
1807 snprintf(fullname, 256, "%s%s", name, key + 1);
1808 else
1809 return;
1811 state = get_state(SMATCH_EXTRA, fullname, sym);
1812 if (!state)
1813 return;
1814 type = get_member_type_from_key(symbol_expression(sym), key);
1815 str_to_rl(type, value, &rl);
1816 if (!rl_to_sval(rl, &max))
1817 return;
1818 estate_set_fuzzy_max(state, max);
1821 void register_smatch_extra(int id)
1823 my_id = id;
1825 add_merge_hook(my_id, &merge_estates);
1826 add_unmatched_state_hook(my_id, &unmatched_state);
1827 select_caller_info_hook(set_param_value, PARAM_VALUE);
1828 select_caller_info_hook(set_param_hard_max, FUZZY_MAX);
1829 select_return_states_hook(RETURN_VALUE, &db_returned_member_info);
1830 select_return_states_before(&db_limited_before);
1831 select_return_states_hook(PARAM_LIMIT, &db_param_limit);
1832 select_return_states_hook(PARAM_FILTER, &db_param_filter);
1833 select_return_states_hook(PARAM_ADD, &db_param_add);
1834 select_return_states_hook(PARAM_SET, &db_param_set);
1835 select_return_states_after(&db_limited_after);
1838 void register_smatch_extra_late(int id)
1840 add_hook(&match_dereferences, DEREF_HOOK);
1841 add_hook(&match_pointer_as_array, OP_HOOK);
1842 select_call_implies_hook(DEREFERENCE, &set_param_dereferenced);
1843 add_hook(&match_function_call, FUNCTION_CALL_HOOK);
1844 add_hook(&match_assign, ASSIGNMENT_HOOK);
1845 add_hook(&match_assign, GLOBAL_ASSIGNMENT_HOOK);
1846 add_hook(&unop_expr, OP_HOOK);
1847 add_hook(&asm_expr, ASM_HOOK);
1849 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
1850 add_member_info_callback(my_id, struct_member_callback);
1851 add_returned_member_callback(my_id, returned_member_callback);
1853 add_hook(&assume_indexes_are_valid, OP_HOOK);