capped: don't record single values as capped in the database
[smatch.git] / smatch_extra.c
blobc9129c5ccdcc35298a7fcfa8e995877a576c5e99
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 return set_state(SMATCH_EXTRA, name, sym, state);
83 static char *get_other_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym)
85 struct expression *assigned;
86 char *orig_name;
87 char buf[256];
88 char *ret = NULL;
89 int skip;
91 *new_sym = NULL;
93 if (!sym->ident)
94 return NULL;
96 skip = strlen(sym->ident->name);
97 if (name[skip] != '-' || name[skip + 1] != '>')
98 return NULL;
99 skip += 2;
101 assigned = get_assigned_expr_name_sym(sym->ident->name, sym);
102 if (!assigned)
103 return NULL;
104 if (assigned->type != EXPR_PREOP || assigned->op != '&')
105 return NULL;
107 orig_name = expr_to_var_sym(assigned, new_sym);
108 if (!orig_name || !*new_sym)
109 goto free;
111 snprintf(buf, sizeof(buf), "%s.%s", orig_name + 1, name + skip);
112 ret = alloc_string(buf);
113 free:
114 free_string(orig_name);
115 return ret;
118 struct sm_state *set_extra_mod(const char *name, struct symbol *sym, struct smatch_state *state)
120 char *new_name;
121 struct symbol *new_sym;
122 struct sm_state *sm;
124 if (in_warn_on_macro())
125 return NULL;
126 sm = set_extra_mod_helper(name, sym, state);
127 new_name = get_other_name_sym(name, sym, &new_sym);
128 if (new_name && new_sym)
129 set_extra_mod_helper(new_name, new_sym, state);
130 free_string(new_name);
131 return sm;
134 struct sm_state *set_extra_expr_mod(struct expression *expr, struct smatch_state *state)
136 struct symbol *sym;
137 char *name;
138 struct sm_state *ret = NULL;
140 expr = strip_expr(expr);
141 name = expr_to_var_sym(expr, &sym);
142 if (!name || !sym)
143 goto free;
144 ret = set_extra_mod(name, sym, state);
145 free:
146 free_string(name);
147 return ret;
150 void set_extra_nomod(const char *name, struct symbol *sym, struct smatch_state *state)
152 char *new_name;
153 struct symbol *new_sym;
154 struct relation *rel;
155 struct smatch_state *orig_state;
157 orig_state = get_state(SMATCH_EXTRA, name, sym);
159 new_name = get_other_name_sym(name, sym, &new_sym);
160 if (new_name && new_sym)
161 set_state(SMATCH_EXTRA, new_name, new_sym, state);
162 free_string(new_name);
164 if (!estate_related(orig_state)) {
165 set_state(SMATCH_EXTRA, name, sym, state);
166 return;
169 set_related(state, estate_related(orig_state));
170 FOR_EACH_PTR(estate_related(orig_state), rel) {
171 if (option_debug_related)
172 sm_msg("%s updating related %s to %s", name, rel->name, state->name);
173 set_state(SMATCH_EXTRA, rel->name, rel->sym, state);
174 } END_FOR_EACH_PTR(rel);
178 * This is for return_implies_state() hooks which modify a SMATCH_EXTRA state
180 void set_extra_expr_nomod(struct expression *expr, struct smatch_state *state)
182 char *name;
183 struct symbol *sym;
185 name = expr_to_var_sym(expr, &sym);
186 if (!name || !sym)
187 goto free;
188 set_extra_nomod(name, sym, state);
189 free:
190 free_string(name);
194 static void set_extra_true_false(const char *name, struct symbol *sym,
195 struct smatch_state *true_state,
196 struct smatch_state *false_state)
198 char *new_name;
199 struct symbol *new_sym;
200 struct relation *rel;
201 struct smatch_state *orig_state;
203 if (in_warn_on_macro())
204 return;
206 new_name = get_other_name_sym(name, sym, &new_sym);
207 if (new_name && new_sym)
208 set_true_false_states(SMATCH_EXTRA, new_name, new_sym, true_state, false_state);
209 free_string(new_name);
211 orig_state = get_state(SMATCH_EXTRA, name, sym);
213 if (!estate_related(orig_state)) {
214 set_true_false_states(SMATCH_EXTRA, name, sym, true_state, false_state);
215 return;
218 if (true_state)
219 set_related(true_state, estate_related(orig_state));
220 if (false_state)
221 set_related(false_state, estate_related(orig_state));
223 FOR_EACH_PTR(estate_related(orig_state), rel) {
224 set_true_false_states(SMATCH_EXTRA, rel->name, rel->sym,
225 true_state, false_state);
226 } END_FOR_EACH_PTR(rel);
229 static void set_extra_expr_true_false(struct expression *expr,
230 struct smatch_state *true_state,
231 struct smatch_state *false_state)
233 char *name;
234 struct symbol *sym;
236 expr = strip_expr(expr);
237 name = expr_to_var_sym(expr, &sym);
238 if (!name || !sym)
239 goto free;
240 set_extra_true_false(name, sym, true_state, false_state);
241 free:
242 free_string(name);
245 static struct sm_state *handle_canonical_while_count_down(struct statement *loop)
247 struct expression *iter_var;
248 struct expression *condition;
249 struct sm_state *sm;
250 struct smatch_state *estate;
251 sval_t start;
253 condition = strip_expr(loop->iterator_pre_condition);
254 if (!condition)
255 return NULL;
256 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
257 return NULL;
258 if (condition->op != SPECIAL_DECREMENT)
259 return NULL;
261 iter_var = condition->unop;
262 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
263 if (!sm)
264 return NULL;
265 if (sval_cmp_val(estate_min(sm->state), 0) < 0)
266 return NULL;
267 start = estate_max(sm->state);
268 if (sval_cmp_val(start, 0) <= 0)
269 return NULL;
270 if (!sval_is_max(start))
271 start.value--;
273 if (condition->type == EXPR_PREOP) {
274 estate = alloc_estate_range(sval_type_val(start.type, 1), start);
275 if (estate_has_hard_max(sm->state))
276 estate_set_hard_max(estate);
277 estate_copy_fuzzy_max(estate, sm->state);
278 set_extra_expr_mod(iter_var, estate);
280 if (condition->type == EXPR_POSTOP) {
281 estate = alloc_estate_range(sval_type_val(start.type, 0), start);
282 if (estate_has_hard_max(sm->state))
283 estate_set_hard_max(estate);
284 estate_copy_fuzzy_max(estate, sm->state);
285 set_extra_expr_mod(iter_var, estate);
287 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
290 static struct sm_state *handle_canonical_for_inc(struct expression *iter_expr,
291 struct expression *condition)
293 struct expression *iter_var;
294 struct sm_state *sm;
295 struct smatch_state *estate;
296 sval_t start, end, max;
298 iter_var = iter_expr->unop;
299 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
300 if (!sm)
301 return NULL;
302 if (!estate_get_single_value(sm->state, &start))
303 return NULL;
304 if (get_implied_max(condition->right, &end))
305 end = sval_cast(get_type(iter_var), end);
306 else
307 end = sval_type_max(get_type(iter_var));
309 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
310 return NULL;
312 switch (condition->op) {
313 case SPECIAL_UNSIGNED_LT:
314 case SPECIAL_NOTEQUAL:
315 case '<':
316 if (!sval_is_min(end))
317 end.value--;
318 break;
319 case SPECIAL_UNSIGNED_LTE:
320 case SPECIAL_LTE:
321 break;
322 default:
323 return NULL;
325 if (sval_cmp(end, start) < 0)
326 return NULL;
327 estate = alloc_estate_range(start, end);
328 if (get_hard_max(condition->right, &max)) {
329 estate_set_hard_max(estate);
330 if (condition->op == '<' ||
331 condition->op == SPECIAL_UNSIGNED_LT ||
332 condition->op == SPECIAL_NOTEQUAL)
333 max.value--;
334 estate_set_fuzzy_max(estate, max);
336 set_extra_expr_mod(iter_var, estate);
337 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
340 static struct sm_state *handle_canonical_for_dec(struct expression *iter_expr,
341 struct expression *condition)
343 struct expression *iter_var;
344 struct sm_state *sm;
345 struct smatch_state *estate;
346 sval_t start, end;
348 iter_var = iter_expr->unop;
349 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
350 if (!sm)
351 return NULL;
352 if (!estate_get_single_value(sm->state, &start))
353 return NULL;
354 if (!get_implied_min(condition->right, &end))
355 end = sval_type_min(get_type(iter_var));
356 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
357 return NULL;
359 switch (condition->op) {
360 case SPECIAL_NOTEQUAL:
361 case '>':
362 if (!sval_is_min(end) && !sval_is_max(end))
363 end.value++;
364 break;
365 case SPECIAL_GTE:
366 break;
367 default:
368 return NULL;
370 if (sval_cmp(end, start) > 0)
371 return NULL;
372 estate = alloc_estate_range(end, start);
373 estate_set_hard_max(estate);
374 estate_set_fuzzy_max(estate, estate_get_fuzzy_max(estate));
375 set_extra_expr_mod(iter_var, estate);
376 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
379 static struct sm_state *handle_canonical_for_loops(struct statement *loop)
381 struct expression *iter_expr;
382 struct expression *condition;
384 if (!loop->iterator_post_statement)
385 return NULL;
386 if (loop->iterator_post_statement->type != STMT_EXPRESSION)
387 return NULL;
388 iter_expr = loop->iterator_post_statement->expression;
389 if (!loop->iterator_pre_condition)
390 return NULL;
391 if (loop->iterator_pre_condition->type != EXPR_COMPARE)
392 return NULL;
393 condition = loop->iterator_pre_condition;
395 if (iter_expr->op == SPECIAL_INCREMENT)
396 return handle_canonical_for_inc(iter_expr, condition);
397 if (iter_expr->op == SPECIAL_DECREMENT)
398 return handle_canonical_for_dec(iter_expr, condition);
399 return NULL;
402 struct sm_state *__extra_handle_canonical_loops(struct statement *loop, struct stree **stree)
404 struct sm_state *ret;
406 __push_fake_cur_stree();
407 if (!loop->iterator_post_statement)
408 ret = handle_canonical_while_count_down(loop);
409 else
410 ret = handle_canonical_for_loops(loop);
411 *stree = __pop_fake_cur_stree();
412 return ret;
415 int __iterator_unchanged(struct sm_state *sm)
417 if (!sm)
418 return 0;
419 if (get_sm_state(my_id, sm->name, sm->sym) == sm)
420 return 1;
421 return 0;
424 static void while_count_down_after(struct sm_state *sm, struct expression *condition)
426 sval_t after_value;
428 /* paranoid checking. prolly not needed */
429 condition = strip_expr(condition);
430 if (!condition)
431 return;
432 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
433 return;
434 if (condition->op != SPECIAL_DECREMENT)
435 return;
436 after_value = estate_min(sm->state);
437 after_value.value--;
438 set_extra_mod(sm->name, sm->sym, alloc_estate_sval(after_value));
441 void __extra_pre_loop_hook_after(struct sm_state *sm,
442 struct statement *iterator,
443 struct expression *condition)
445 struct expression *iter_expr;
446 sval_t limit;
447 struct smatch_state *state;
449 if (!iterator) {
450 while_count_down_after(sm, condition);
451 return;
454 iter_expr = iterator->expression;
456 if (condition->type != EXPR_COMPARE)
457 return;
458 if (iter_expr->op == SPECIAL_INCREMENT) {
459 limit = sval_binop(estate_max(sm->state), '+',
460 sval_type_val(estate_type(sm->state), 1));
461 } else {
462 limit = sval_binop(estate_min(sm->state), '-',
463 sval_type_val(estate_type(sm->state), 1));
465 if (!estate_has_hard_max(sm->state) && !__has_breaks()) {
466 if (iter_expr->op == SPECIAL_INCREMENT)
467 state = alloc_estate_range(estate_min(sm->state), limit);
468 else
469 state = alloc_estate_range(limit, estate_max(sm->state));
470 } else {
471 state = alloc_estate_sval(limit);
473 if (!estate_has_hard_max(sm->state)) {
474 estate_clear_hard_max(state);
476 if (estate_has_fuzzy_max(sm->state)) {
477 sval_t hmax = estate_get_fuzzy_max(sm->state);
478 sval_t max = estate_max(sm->state);
480 if (sval_cmp(hmax, max) != 0)
481 estate_clear_fuzzy_max(state);
482 } else if (!estate_has_fuzzy_max(sm->state)) {
483 estate_clear_fuzzy_max(state);
486 set_extra_mod(sm->name, sm->sym, state);
489 static struct stree *unmatched_stree;
490 static struct smatch_state *unmatched_state(struct sm_state *sm)
492 struct smatch_state *state;
494 if (unmatched_stree) {
495 state = get_state_stree(unmatched_stree, SMATCH_EXTRA, sm->name, sm->sym);
496 if (state)
497 return state;
499 if (parent_is_gone_var_sym(sm->name, sm->sym))
500 return alloc_estate_empty();
501 return alloc_estate_whole(estate_type(sm->state));
504 static void clear_the_pointed_at(struct expression *expr)
506 struct stree *stree;
507 char *name;
508 struct symbol *sym;
509 struct sm_state *tmp;
511 name = expr_to_var_sym(expr, &sym);
512 if (!name || !sym)
513 goto free;
515 stree = __get_cur_stree();
516 FOR_EACH_MY_SM(SMATCH_EXTRA, stree, tmp) {
517 if (tmp->name[0] != '*')
518 continue;
519 if (tmp->sym != sym)
520 continue;
521 if (strcmp(tmp->name + 1, name) != 0)
522 continue;
523 set_extra_mod(tmp->name, tmp->sym, alloc_estate_whole(estate_type(tmp->state)));
524 } END_FOR_EACH_SM(tmp);
526 free:
527 free_string(name);
530 static void match_function_call(struct expression *expr)
532 struct expression *arg;
533 struct expression *tmp;
535 /* if we have the db this is handled in smatch_function_hooks.c */
536 if (!option_no_db)
537 return;
538 if (inlinable(expr->fn))
539 return;
541 FOR_EACH_PTR(expr->args, arg) {
542 tmp = strip_expr(arg);
543 if (tmp->type == EXPR_PREOP && tmp->op == '&')
544 set_extra_expr_mod(tmp->unop, alloc_estate_whole(get_type(tmp->unop)));
545 else
546 clear_the_pointed_at(tmp);
547 } END_FOR_EACH_PTR(arg);
550 static int types_equiv_or_pointer(struct symbol *one, struct symbol *two)
552 if (!one || !two)
553 return 0;
554 if (one->type == SYM_PTR && two->type == SYM_PTR)
555 return 1;
556 return types_equiv(one, two);
559 static bool is_non_null_array(struct expression *expr)
561 struct symbol *type;
562 struct symbol *sym;
563 struct symbol *tmp;
564 char *name;
565 int i;
567 type = get_type(expr);
568 if (!type || type->type != SYM_ARRAY)
569 return 0;
570 if (expr->type == EXPR_SYMBOL)
571 return 1;
572 if (implied_not_equal(expr, 0))
573 return 1;
575 /* verify that it's not the first member of the struct */
576 if (expr->type != EXPR_DEREF || !expr->member)
577 return 0;
578 name = expr_to_var_sym(expr, &sym);
579 free_string(name);
580 if (!name || !sym)
581 return 0;
582 type = get_real_base_type(sym);
583 if (!type || type->type != SYM_PTR)
584 return 0;
585 type = get_real_base_type(type);
586 if (type->type != SYM_STRUCT)
587 return 0;
589 i = 0;
590 FOR_EACH_PTR(type->symbol_list, tmp) {
591 i++;
592 if (!tmp->ident)
593 continue;
594 if (strcmp(expr->member->name, tmp->ident->name) == 0) {
595 if (i == 1)
596 return 0;
597 return 1;
599 } END_FOR_EACH_PTR(tmp);
601 return 0;
604 static void match_vanilla_assign(struct expression *left, struct expression *right)
606 struct range_list *rl = NULL;
607 struct symbol *right_sym;
608 struct symbol *left_type;
609 struct symbol *right_type;
610 char *right_name = NULL;
611 struct symbol *sym;
612 char *name;
613 sval_t max;
614 struct smatch_state *state;
616 if (is_struct(left))
617 return;
619 name = expr_to_var_sym(left, &sym);
620 if (!name)
621 return;
623 left_type = get_type(left);
624 right_type = get_type(right);
626 right_name = expr_to_var_sym(right, &right_sym);
628 if (!(right->type == EXPR_PREOP && right->op == '&') &&
629 right_name && right_sym &&
630 types_equiv_or_pointer(left_type, right_type) &&
631 !has_symbol(right, sym)) {
632 set_equiv(left, right);
633 goto free;
636 if (is_non_null_array(right)) {
637 state = alloc_estate_range(array_min_sval, array_max_sval);
638 } else if (get_implied_rl(right, &rl)) {
639 rl = cast_rl(left_type, rl);
640 state = alloc_estate_rl(rl);
641 if (get_hard_max(right, &max)) {
642 estate_set_hard_max(state);
643 estate_set_fuzzy_max(state, max);
645 } else {
646 rl = alloc_whole_rl(right_type);
647 rl = cast_rl(left_type, rl);
648 state = alloc_estate_rl(rl);
650 set_extra_mod(name, sym, state);
651 free:
652 free_string(right_name);
655 static int op_remove_assign(int op)
657 switch (op) {
658 case SPECIAL_ADD_ASSIGN:
659 return '+';
660 case SPECIAL_SUB_ASSIGN:
661 return '-';
662 case SPECIAL_MUL_ASSIGN:
663 return '*';
664 case SPECIAL_DIV_ASSIGN:
665 return '/';
666 case SPECIAL_MOD_ASSIGN:
667 return '%';
668 case SPECIAL_AND_ASSIGN:
669 return '&';
670 case SPECIAL_OR_ASSIGN:
671 return '|';
672 case SPECIAL_XOR_ASSIGN:
673 return '^';
674 case SPECIAL_SHL_ASSIGN:
675 return SPECIAL_LEFTSHIFT;
676 case SPECIAL_SHR_ASSIGN:
677 return SPECIAL_RIGHTSHIFT;
678 default:
679 return op;
683 static void match_assign(struct expression *expr)
685 struct range_list *rl = NULL;
686 struct expression *left;
687 struct expression *right;
688 struct expression *binop_expr;
689 struct symbol *left_type;
690 struct symbol *right_type;
691 struct symbol *sym;
692 char *name;
693 sval_t left_min, left_max;
694 sval_t right_min, right_max;
695 sval_t res_min, res_max;
697 left = strip_expr(expr->left);
699 right = strip_expr(expr->right);
700 while (right->type == EXPR_ASSIGNMENT && right->op == '=')
701 right = strip_expr(right->left);
703 if (is_condition(expr->right))
704 return; /* handled in smatch_condition.c */
705 if (expr->op == '=' && right->type == EXPR_CALL)
706 return; /* handled in smatch_function_hooks.c */
707 if (expr->op == '=') {
708 match_vanilla_assign(left, right);
709 return;
712 name = expr_to_var_sym(left, &sym);
713 if (!name)
714 return;
716 left_type = get_type(left);
717 right_type = get_type(right);
719 res_min = sval_type_min(left_type);
720 res_max = sval_type_max(left_type);
722 switch (expr->op) {
723 case SPECIAL_ADD_ASSIGN:
724 get_absolute_max(left, &left_max);
725 get_absolute_max(right, &right_max);
726 if (sval_binop_overflows(left_max, '+', right_max))
727 break;
728 if (get_implied_min(left, &left_min) &&
729 !sval_is_negative_min(left_min) &&
730 get_implied_min(right, &right_min) &&
731 !sval_is_negative_min(right_min)) {
732 res_min = sval_binop(left_min, '+', right_min);
733 res_min = sval_cast(right_type, res_min);
735 if (inside_loop()) /* we are assuming loops don't lead to wrapping */
736 break;
737 res_max = sval_binop(left_max, '+', right_max);
738 res_max = sval_cast(right_type, res_max);
739 break;
740 case SPECIAL_SUB_ASSIGN:
741 if (get_implied_max(left, &left_max) &&
742 !sval_is_max(left_max) &&
743 get_implied_min(right, &right_min) &&
744 !sval_is_min(right_min)) {
745 res_max = sval_binop(left_max, '-', right_min);
746 res_max = sval_cast(right_type, res_max);
748 if (inside_loop())
749 break;
750 if (get_implied_min(left, &left_min) &&
751 !sval_is_min(left_min) &&
752 get_implied_max(right, &right_max) &&
753 !sval_is_max(right_max)) {
754 res_min = sval_binop(left_min, '-', right_max);
755 res_min = sval_cast(right_type, res_min);
757 break;
758 case SPECIAL_AND_ASSIGN:
759 case SPECIAL_MOD_ASSIGN:
760 case SPECIAL_SHL_ASSIGN:
761 case SPECIAL_SHR_ASSIGN:
762 case SPECIAL_OR_ASSIGN:
763 case SPECIAL_XOR_ASSIGN:
764 case SPECIAL_MUL_ASSIGN:
765 case SPECIAL_DIV_ASSIGN:
766 binop_expr = binop_expression(expr->left,
767 op_remove_assign(expr->op),
768 expr->right);
769 if (get_absolute_rl(binop_expr, &rl)) {
770 rl = cast_rl(left_type, rl);
771 set_extra_mod(name, sym, alloc_estate_rl(rl));
772 goto free;
774 break;
776 rl = cast_rl(left_type, alloc_rl(res_min, res_max));
777 set_extra_mod(name, sym, alloc_estate_rl(rl));
778 free:
779 free_string(name);
782 static struct smatch_state *increment_state(struct smatch_state *state)
784 sval_t min = estate_min(state);
785 sval_t max = estate_max(state);
787 if (!estate_rl(state))
788 return NULL;
790 if (inside_loop())
791 max = sval_type_max(max.type);
793 if (!sval_is_min(min) && !sval_is_max(min))
794 min.value++;
795 if (!sval_is_min(max) && !sval_is_max(max))
796 max.value++;
797 return alloc_estate_range(min, max);
800 static struct smatch_state *decrement_state(struct smatch_state *state)
802 sval_t min = estate_min(state);
803 sval_t max = estate_max(state);
805 if (!estate_rl(state))
806 return NULL;
808 if (inside_loop())
809 min = sval_type_min(min.type);
811 if (!sval_is_min(min) && !sval_is_max(min))
812 min.value--;
813 if (!sval_is_min(max) && !sval_is_max(max))
814 max.value--;
815 return alloc_estate_range(min, max);
818 static void unop_expr(struct expression *expr)
820 struct smatch_state *state;
822 if (expr->smatch_flags & Handled)
823 return;
825 switch (expr->op) {
826 case SPECIAL_INCREMENT:
827 state = get_state_expr(SMATCH_EXTRA, expr->unop);
828 state = increment_state(state);
829 if (!state)
830 state = alloc_estate_whole(get_type(expr));
831 set_extra_expr_mod(expr->unop, state);
832 break;
833 case SPECIAL_DECREMENT:
834 state = get_state_expr(SMATCH_EXTRA, expr->unop);
835 state = decrement_state(state);
836 if (!state)
837 state = alloc_estate_whole(get_type(expr));
838 set_extra_expr_mod(expr->unop, state);
839 break;
840 default:
841 return;
845 static void asm_expr(struct statement *stmt)
848 struct expression *expr;
849 struct symbol *type;
850 int state = 0;
852 FOR_EACH_PTR(stmt->asm_outputs, expr) {
853 switch (state) {
854 case 0: /* identifier */
855 case 1: /* constraint */
856 state++;
857 continue;
858 case 2: /* expression */
859 state = 0;
860 type = get_type(strip_expr(expr));
861 set_extra_expr_mod(expr, alloc_estate_whole(type));
862 continue;
864 } END_FOR_EACH_PTR(expr);
867 static void delete_state_tracker(struct tracker *t)
869 remove_from_equiv(t->name, t->sym);
870 delete_state(t->owner, t->name, t->sym);
873 static void scoped_state_extra(const char *name, struct symbol *sym)
875 struct tracker *t;
877 t = alloc_tracker(SMATCH_EXTRA, name, sym);
878 add_scope_hook((scope_hook *)&delete_state_tracker, t);
881 static void match_declarations(struct symbol *sym)
883 const char *name;
885 if (sym->ident) {
886 name = sym->ident->name;
887 if (!sym->initializer) {
888 set_state(SMATCH_EXTRA, name, sym, alloc_estate_whole(get_real_base_type(sym)));
889 scoped_state_extra(name, sym);
894 static void check_dereference(struct expression *expr)
896 if (outside_of_function())
897 return;
898 set_extra_expr_nomod(expr, alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval));
901 static void match_dereferences(struct expression *expr)
903 if (expr->type != EXPR_PREOP)
904 return;
905 check_dereference(expr->unop);
908 static void match_pointer_as_array(struct expression *expr)
910 if (!is_array(expr))
911 return;
912 check_dereference(get_array_base(expr));
915 static void set_param_dereferenced(struct expression *arg, char *key, char *unused)
917 struct symbol *sym;
918 char *name;
920 name = get_variable_from_key(arg, key, &sym);
921 if (!name || !sym)
922 goto free;
924 set_extra_nomod(name, sym, alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval));
926 free:
927 free_string(name);
930 static void match_function_def(struct symbol *sym)
932 struct symbol *arg;
934 FOR_EACH_PTR(sym->ctype.base_type->arguments, arg) {
935 if (!arg->ident)
936 continue;
937 set_state(my_id, arg->ident->name, arg, alloc_estate_whole(get_real_base_type(arg)));
938 } END_FOR_EACH_PTR(arg);
941 static sval_t add_one(sval_t sval)
943 sval.value++;
944 return sval;
947 static sval_t sub_one(sval_t sval)
949 sval.value--;
950 return sval;
953 static void handle_comparison(struct symbol *type, struct expression *left, int op, struct expression *right)
955 struct range_list *left_orig;
956 struct range_list *left_true;
957 struct range_list *left_false;
958 struct range_list *right_orig;
959 struct range_list *right_true;
960 struct range_list *right_false;
961 struct smatch_state *left_true_state;
962 struct smatch_state *left_false_state;
963 struct smatch_state *right_true_state;
964 struct smatch_state *right_false_state;
965 sval_t min, max, dummy, hard_max;
966 int left_postop = 0;
967 int right_postop = 0;
969 if (left->op == SPECIAL_INCREMENT || left->op == SPECIAL_DECREMENT) {
970 if (left->type == EXPR_POSTOP) {
971 left->smatch_flags |= Handled;
972 left_postop = left->op;
974 left = strip_expr(left->unop);
976 while (left->type == EXPR_ASSIGNMENT)
977 left = strip_expr(left->left);
979 if (right->op == SPECIAL_INCREMENT || right->op == SPECIAL_DECREMENT) {
980 if (right->type == EXPR_POSTOP) {
981 right->smatch_flags |= Handled;
982 right_postop = right->op;
984 right = strip_expr(right->unop);
987 if (get_implied_rl(left, &left_orig)) {
988 left_orig = cast_rl(type, left_orig);
989 } else {
990 min = sval_type_min(get_type(left));
991 max = sval_type_max(get_type(left));
992 left_orig = cast_rl(type, alloc_rl(min, max));
995 if (get_implied_rl(right, &right_orig)) {
996 right_orig = cast_rl(type, right_orig);
997 } else {
998 min = sval_type_min(get_type(right));
999 max = sval_type_max(get_type(right));
1000 right_orig = cast_rl(type, alloc_rl(min, max));
1002 min = sval_type_min(type);
1003 max = sval_type_max(type);
1005 left_true = clone_rl(left_orig);
1006 left_false = clone_rl(left_orig);
1007 right_true = clone_rl(right_orig);
1008 right_false = clone_rl(right_orig);
1010 switch (op) {
1011 case '<':
1012 case SPECIAL_UNSIGNED_LT:
1013 left_true = remove_range(left_orig, rl_max(right_orig), max);
1014 if (!sval_is_min(rl_min(right_orig))) {
1015 left_false = remove_range(left_orig, min, sub_one(rl_min(right_orig)));
1018 right_true = remove_range(right_orig, min, rl_min(left_orig));
1019 if (!sval_is_max(rl_max(left_orig)))
1020 right_false = remove_range(right_orig, add_one(rl_max(left_orig)), max);
1021 break;
1022 case SPECIAL_UNSIGNED_LTE:
1023 case SPECIAL_LTE:
1024 if (!sval_is_max(rl_max(right_orig)))
1025 left_true = remove_range(left_orig, add_one(rl_max(right_orig)), max);
1026 left_false = remove_range(left_orig, min, rl_min(right_orig));
1028 if (!sval_is_min(rl_min(left_orig)))
1029 right_true = remove_range(right_orig, min, sub_one(rl_min(left_orig)));
1030 right_false = remove_range(right_orig, rl_max(left_orig), max);
1032 if (sval_cmp(rl_min(left_orig), rl_min(right_orig)) == 0)
1033 left_false = remove_range(left_false, rl_min(left_orig), rl_min(left_orig));
1034 if (sval_cmp(rl_max(left_orig), rl_max(right_orig)) == 0)
1035 right_false = remove_range(right_false, rl_max(left_orig), rl_max(left_orig));
1036 break;
1037 case SPECIAL_EQUAL:
1038 if (!sval_is_max(rl_max(right_orig))) {
1039 left_true = remove_range(left_true, add_one(rl_max(right_orig)), max);
1041 if (!sval_is_min(rl_min(right_orig))) {
1042 left_true = remove_range(left_true, min, sub_one(rl_min(right_orig)));
1044 if (sval_cmp(rl_min(right_orig), rl_max(right_orig)) == 0)
1045 left_false = remove_range(left_orig, rl_min(right_orig), rl_min(right_orig));
1047 if (!sval_is_max(rl_max(left_orig)))
1048 right_true = remove_range(right_true, add_one(rl_max(left_orig)), max);
1049 if (!sval_is_min(rl_min(left_orig)))
1050 right_true = remove_range(right_true, min, sub_one(rl_min(left_orig)));
1051 if (sval_cmp(rl_min(left_orig), rl_max(left_orig)) == 0)
1052 right_false = remove_range(right_orig, rl_min(left_orig), rl_min(left_orig));
1053 break;
1054 case SPECIAL_UNSIGNED_GTE:
1055 case SPECIAL_GTE:
1056 if (!sval_is_min(rl_min(right_orig)))
1057 left_true = remove_range(left_orig, min, sub_one(rl_min(right_orig)));
1058 left_false = remove_range(left_orig, rl_max(right_orig), max);
1060 if (!sval_is_max(rl_max(left_orig)))
1061 right_true = remove_range(right_orig, add_one(rl_max(left_orig)), max);
1062 right_false = remove_range(right_orig, min, rl_min(left_orig));
1064 if (sval_cmp(rl_min(left_orig), rl_min(right_orig)) == 0)
1065 right_false = remove_range(right_false, rl_min(left_orig), rl_min(left_orig));
1066 if (sval_cmp(rl_max(left_orig), rl_max(right_orig)) == 0)
1067 left_false = remove_range(left_false, rl_max(left_orig), rl_max(left_orig));
1068 break;
1069 case '>':
1070 case SPECIAL_UNSIGNED_GT:
1071 left_true = remove_range(left_orig, min, rl_min(right_orig));
1072 if (!sval_is_max(rl_max(right_orig)))
1073 left_false = remove_range(left_orig, add_one(rl_max(right_orig)), max);
1075 right_true = remove_range(right_orig, rl_max(left_orig), max);
1076 if (!sval_is_min(rl_min(left_orig)))
1077 right_false = remove_range(right_orig, min, sub_one(rl_min(left_orig)));
1078 break;
1079 case SPECIAL_NOTEQUAL:
1080 if (!sval_is_max(rl_max(right_orig)))
1081 left_false = remove_range(left_false, add_one(rl_max(right_orig)), max);
1082 if (!sval_is_min(rl_min(right_orig)))
1083 left_false = remove_range(left_false, min, sub_one(rl_min(right_orig)));
1084 if (sval_cmp(rl_min(right_orig), rl_max(right_orig)) == 0)
1085 left_true = remove_range(left_orig, rl_min(right_orig), rl_min(right_orig));
1087 if (!sval_is_max(rl_max(left_orig)))
1088 right_false = remove_range(right_false, add_one(rl_max(left_orig)), max);
1089 if (!sval_is_min(rl_min(left_orig)))
1090 right_false = remove_range(right_false, min, sub_one(rl_min(left_orig)));
1091 if (sval_cmp(rl_min(left_orig), rl_max(left_orig)) == 0)
1092 right_true = remove_range(right_orig, rl_min(left_orig), rl_min(left_orig));
1093 break;
1094 default:
1095 return;
1098 left_true = rl_truncate_cast(get_type(left), left_true);
1099 left_false = rl_truncate_cast(get_type(left), left_false);
1100 right_true = rl_truncate_cast(get_type(right), right_true);
1101 right_false = rl_truncate_cast(get_type(right), right_false);
1103 left_true_state = alloc_estate_rl(left_true);
1104 left_false_state = alloc_estate_rl(left_false);
1105 right_true_state = alloc_estate_rl(right_true);
1106 right_false_state = alloc_estate_rl(right_false);
1108 switch (op) {
1109 case '<':
1110 case SPECIAL_UNSIGNED_LT:
1111 case SPECIAL_UNSIGNED_LTE:
1112 case SPECIAL_LTE:
1113 if (get_hard_max(right, &dummy))
1114 estate_set_hard_max(left_true_state);
1115 if (get_hard_max(left, &dummy))
1116 estate_set_hard_max(right_false_state);
1117 break;
1118 case '>':
1119 case SPECIAL_UNSIGNED_GT:
1120 case SPECIAL_UNSIGNED_GTE:
1121 case SPECIAL_GTE:
1122 if (get_hard_max(left, &dummy))
1123 estate_set_hard_max(right_true_state);
1124 if (get_hard_max(right, &dummy))
1125 estate_set_hard_max(left_false_state);
1126 break;
1129 switch (op) {
1130 case '<':
1131 case SPECIAL_UNSIGNED_LT:
1132 case SPECIAL_UNSIGNED_LTE:
1133 case SPECIAL_LTE:
1134 if (get_hard_max(right, &hard_max)) {
1135 if (op == '<' || op == SPECIAL_UNSIGNED_LT)
1136 hard_max.value--;
1137 estate_set_fuzzy_max(left_true_state, hard_max);
1139 if (get_implied_value(right, &hard_max)) {
1140 if (op == SPECIAL_UNSIGNED_LTE ||
1141 op == SPECIAL_LTE)
1142 hard_max.value++;
1143 estate_set_fuzzy_max(left_false_state, hard_max);
1145 if (get_hard_max(left, &hard_max)) {
1146 if (op == SPECIAL_UNSIGNED_LTE ||
1147 op == SPECIAL_LTE)
1148 hard_max.value--;
1149 estate_set_fuzzy_max(right_false_state, hard_max);
1151 if (get_implied_value(left, &hard_max)) {
1152 if (op == '<' || op == SPECIAL_UNSIGNED_LT)
1153 hard_max.value++;
1154 estate_set_fuzzy_max(right_true_state, hard_max);
1156 break;
1157 case '>':
1158 case SPECIAL_UNSIGNED_GT:
1159 case SPECIAL_UNSIGNED_GTE:
1160 case SPECIAL_GTE:
1161 if (get_hard_max(left, &hard_max)) {
1162 if (op == '>' || op == SPECIAL_UNSIGNED_GT)
1163 hard_max.value--;
1164 estate_set_fuzzy_max(right_true_state, hard_max);
1166 if (get_implied_value(left, &hard_max)) {
1167 if (op == SPECIAL_UNSIGNED_GTE ||
1168 op == SPECIAL_GTE)
1169 hard_max.value++;
1170 estate_set_fuzzy_max(right_false_state, hard_max);
1172 if (get_hard_max(right, &hard_max)) {
1173 if (op == SPECIAL_UNSIGNED_LTE ||
1174 op == SPECIAL_LTE)
1175 hard_max.value--;
1176 estate_set_fuzzy_max(left_false_state, hard_max);
1178 if (get_implied_value(right, &hard_max)) {
1179 if (op == '>' ||
1180 op == SPECIAL_UNSIGNED_GT)
1181 hard_max.value++;
1182 estate_set_fuzzy_max(left_true_state, hard_max);
1184 break;
1185 case SPECIAL_EQUAL:
1186 if (get_hard_max(left, &hard_max))
1187 estate_set_fuzzy_max(right_true_state, hard_max);
1188 if (get_hard_max(right, &hard_max))
1189 estate_set_fuzzy_max(left_true_state, hard_max);
1190 break;
1193 if (get_hard_max(left, &hard_max)) {
1194 estate_set_hard_max(left_true_state);
1195 estate_set_hard_max(left_false_state);
1197 if (get_hard_max(right, &hard_max)) {
1198 estate_set_hard_max(right_true_state);
1199 estate_set_hard_max(right_false_state);
1202 if (left_postop == SPECIAL_INCREMENT) {
1203 left_true_state = increment_state(left_true_state);
1204 left_false_state = increment_state(left_false_state);
1206 if (left_postop == SPECIAL_DECREMENT) {
1207 left_true_state = decrement_state(left_true_state);
1208 left_false_state = decrement_state(left_false_state);
1210 if (right_postop == SPECIAL_INCREMENT) {
1211 right_true_state = increment_state(right_true_state);
1212 right_false_state = increment_state(right_false_state);
1214 if (right_postop == SPECIAL_DECREMENT) {
1215 right_true_state = decrement_state(right_true_state);
1216 right_false_state = decrement_state(right_false_state);
1219 set_extra_expr_true_false(left, left_true_state, left_false_state);
1220 set_extra_expr_true_false(right, right_true_state, right_false_state);
1223 static int is_simple_math(struct expression *expr)
1225 if (!expr)
1226 return 0;
1227 if (expr->type != EXPR_BINOP)
1228 return 0;
1229 switch (expr->op) {
1230 case '+':
1231 case '-':
1232 case '*':
1233 return 1;
1235 return 0;
1238 static void move_known_values(struct expression **left_p, struct expression **right_p)
1240 struct expression *left = *left_p;
1241 struct expression *right = *right_p;
1242 sval_t sval;
1244 if (get_implied_value(left, &sval)) {
1245 if (!is_simple_math(right))
1246 return;
1247 if (right->op == '*') {
1248 sval_t divisor;
1250 if (!get_value(right->right, &divisor))
1251 return;
1252 if (divisor.value == 0 && sval.value % divisor.value)
1253 return;
1254 *left_p = binop_expression(left, invert_op(right->op), right->right);
1255 *right_p = right->left;
1256 return;
1258 if (right->op == '+' && get_value(right->left, &sval)) {
1259 *left_p = binop_expression(left, invert_op(right->op), right->left);
1260 *right_p = right->right;
1261 return;
1263 if (get_value(right->right, &sval)) {
1264 *left_p = binop_expression(left, invert_op(right->op), right->right);
1265 *right_p = right->left;
1266 return;
1268 return;
1270 if (get_implied_value(right, &sval)) {
1271 if (!is_simple_math(left))
1272 return;
1273 if (left->op == '*') {
1274 sval_t divisor;
1276 if (!get_value(left->right, &divisor))
1277 return;
1278 if (divisor.value == 0 && sval.value % divisor.value)
1279 return;
1280 *right_p = binop_expression(right, invert_op(left->op), left->right);
1281 *left_p = left->left;
1282 return;
1284 if (left->op == '+' && get_value(left->left, &sval)) {
1285 *right_p = binop_expression(right, invert_op(left->op), left->left);
1286 *left_p = left->right;
1287 return;
1290 if (get_value(left->right, &sval)) {
1291 *right_p = binop_expression(right, invert_op(left->op), left->right);
1292 *left_p = left->left;
1293 return;
1295 return;
1299 static int match_func_comparison(struct expression *expr)
1301 struct expression *left = strip_expr(expr->left);
1302 struct expression *right = strip_expr(expr->right);
1303 sval_t sval;
1306 * fixme: think about this harder. We should always be trying to limit
1307 * the non-call side as well. If we can't determine the limitter does
1308 * that mean we aren't querying the database and are missing important
1309 * information?
1312 if (left->type == EXPR_CALL) {
1313 if (get_implied_value(left, &sval)) {
1314 handle_comparison(get_type(expr), left, expr->op, right);
1315 return 1;
1317 function_comparison(left, expr->op, right);
1318 return 1;
1321 if (right->type == EXPR_CALL) {
1322 if (get_implied_value(right, &sval)) {
1323 handle_comparison(get_type(expr), left, expr->op, right);
1324 return 1;
1326 function_comparison(left, expr->op, right);
1327 return 1;
1330 return 0;
1333 static void match_comparison(struct expression *expr)
1335 struct expression *left_orig = strip_expr(expr->left);
1336 struct expression *right_orig = strip_expr(expr->right);
1337 struct expression *left, *right;
1338 struct expression *prev;
1339 struct symbol *type;
1341 if (match_func_comparison(expr))
1342 return;
1344 type = get_type(expr);
1345 if (!type)
1346 type = &llong_ctype;
1348 left = left_orig;
1349 right = right_orig;
1350 move_known_values(&left, &right);
1351 handle_comparison(type, left, expr->op, right);
1353 prev = get_assigned_expr(left_orig);
1354 if (is_simple_math(prev) && has_variable(prev, left_orig) == 0) {
1355 left = prev;
1356 right = right_orig;
1357 move_known_values(&left, &right);
1358 handle_comparison(type, left, expr->op, right);
1361 prev = get_assigned_expr(right_orig);
1362 if (is_simple_math(prev) && has_variable(prev, right_orig) == 0) {
1363 left = left_orig;
1364 right = prev;
1365 move_known_values(&left, &right);
1366 handle_comparison(type, left, expr->op, right);
1370 static void handle_AND_condition(struct expression *expr)
1372 struct range_list *rl = NULL;
1373 sval_t known;
1375 if (get_implied_value(expr->left, &known) && known.value > 0) {
1376 known.value--;
1377 get_absolute_rl(expr->right, &rl);
1378 rl = remove_range(rl, sval_type_val(known.type, 0), known);
1379 set_extra_expr_true_false(expr->right, alloc_estate_rl(rl), NULL);
1380 return;
1383 if (get_implied_value(expr->right, &known) && known.value > 0) {
1384 known.value--;
1385 get_absolute_rl(expr->left, &rl);
1386 rl = remove_range(rl, sval_type_val(known.type, 0), known);
1387 set_extra_expr_true_false(expr->left, alloc_estate_rl(rl), NULL);
1388 return;
1392 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
1393 void __extra_match_condition(struct expression *expr)
1395 struct symbol *sym;
1396 char *name;
1397 struct smatch_state *pre_state;
1398 struct smatch_state *true_state;
1399 struct smatch_state *false_state;
1401 expr = strip_expr(expr);
1402 switch (expr->type) {
1403 case EXPR_CALL:
1404 function_comparison(expr, SPECIAL_NOTEQUAL, zero_expr());
1405 return;
1406 case EXPR_PREOP:
1407 case EXPR_SYMBOL:
1408 case EXPR_DEREF: {
1409 sval_t zero;
1411 zero = sval_blank(expr);
1412 zero.value = 0;
1414 name = expr_to_var_sym(expr, &sym);
1415 if (!name)
1416 return;
1417 pre_state = get_state(my_id, name, sym);
1418 true_state = estate_filter_sval(pre_state, zero);
1419 if (possibly_true(expr, SPECIAL_EQUAL, zero_expr()))
1420 false_state = alloc_estate_sval(zero);
1421 else
1422 false_state = alloc_estate_empty();
1423 set_extra_true_false(name, sym, true_state, false_state);
1424 free_string(name);
1425 return;
1427 case EXPR_COMPARE:
1428 match_comparison(expr);
1429 return;
1430 case EXPR_ASSIGNMENT:
1431 __extra_match_condition(expr->left);
1432 return;
1433 case EXPR_BINOP:
1434 if (expr->op == '&')
1435 handle_AND_condition(expr);
1436 return;
1440 static void assume_indexes_are_valid(struct expression *expr)
1442 struct expression *array_expr;
1443 int array_size;
1444 struct expression *offset;
1445 struct symbol *offset_type;
1446 struct range_list *rl_before;
1447 struct range_list *rl_after;
1448 struct range_list *filter = NULL;
1449 sval_t size;
1451 expr = strip_expr(expr);
1452 if (!is_array(expr))
1453 return;
1455 offset = get_array_offset(expr);
1456 offset_type = get_type(offset);
1457 if (offset_type && type_signed(offset_type)) {
1458 filter = alloc_rl(sval_type_min(offset_type),
1459 sval_type_val(offset_type, -1));
1462 array_expr = get_array_base(expr);
1463 array_size = get_real_array_size(array_expr);
1464 if (array_size > 1) {
1465 size = sval_type_val(offset_type, array_size);
1466 add_range(&filter, size, sval_type_max(offset_type));
1469 if (!filter)
1470 return;
1471 get_absolute_rl(offset, &rl_before);
1472 rl_after = rl_filter(rl_before, filter);
1473 if (rl_equiv(rl_before, rl_after))
1474 return;
1475 set_extra_expr_nomod(offset, alloc_estate_rl(rl_after));
1478 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
1479 int implied_not_equal(struct expression *expr, long long val)
1481 return !possibly_false(expr, SPECIAL_NOTEQUAL, value_expr(val));
1484 int implied_not_equal_name_sym(char *name, struct symbol *sym, long long val)
1486 struct smatch_state *estate;
1488 estate = get_state(SMATCH_EXTRA, name, sym);
1489 if (!estate)
1490 return 0;
1491 if (!rl_has_sval(estate_rl(estate), sval_type_val(estate_type(estate), 0)))
1492 return 1;
1493 return 0;
1496 int parent_is_null_var_sym(const char *name, struct symbol *sym)
1498 char buf[256];
1499 char *start;
1500 char *end;
1501 struct smatch_state *state;
1503 strncpy(buf, name, sizeof(buf) - 1);
1504 buf[sizeof(buf) - 1] = '\0';
1506 start = &buf[0];
1507 while (*start == '*') {
1508 start++;
1509 state = get_state(SMATCH_EXTRA, start, sym);
1510 if (!state)
1511 continue;
1512 if (!estate_rl(state))
1513 return 1;
1514 if (estate_min(state).value == 0 &&
1515 estate_max(state).value == 0)
1516 return 1;
1519 start = &buf[0];
1520 while (*start == '&')
1521 start++;
1523 while ((end = strrchr(start, '-'))) {
1524 *end = '\0';
1525 state = get_state(SMATCH_EXTRA, start, sym);
1526 if (!state)
1527 continue;
1528 if (estate_min(state).value == 0 &&
1529 estate_max(state).value == 0)
1530 return 1;
1532 return 0;
1535 int parent_is_null(struct expression *expr)
1537 struct symbol *sym;
1538 char *var;
1539 int ret = 0;
1541 expr = strip_expr(expr);
1542 var = expr_to_var_sym(expr, &sym);
1543 if (!var || !sym)
1544 goto free;
1545 ret = parent_is_null_var_sym(var, sym);
1546 free:
1547 free_string(var);
1548 return ret;
1551 static void struct_member_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
1553 if (estate_is_whole(sm->state))
1554 return;
1555 sql_insert_caller_info(call, PARAM_VALUE, param, printed_name, sm->state->name);
1556 if (estate_has_fuzzy_max(sm->state))
1557 sql_insert_caller_info(call, FUZZY_MAX, param, printed_name,
1558 sval_to_str(estate_get_fuzzy_max(sm->state)));
1561 static void db_limited_before(void)
1563 unmatched_stree = clone_stree(__get_cur_stree());
1566 static void db_limited_after(void)
1568 free_stree(&unmatched_stree);
1571 static void db_param_limit_filter(struct expression *expr, int param, char *key, char *value)
1573 struct expression *arg;
1574 char *name;
1575 struct symbol *sym;
1576 struct sm_state *sm;
1577 struct symbol *compare_type, *var_type;
1578 struct range_list *rl;
1579 struct range_list *limit;
1580 struct range_list *new;
1582 while (expr->type == EXPR_ASSIGNMENT)
1583 expr = strip_expr(expr->right);
1584 if (expr->type != EXPR_CALL)
1585 return;
1587 arg = get_argument_from_call_expr(expr->args, param);
1588 if (!arg)
1589 return;
1591 name = get_variable_from_key(arg, key, &sym);
1592 if (!name || !sym)
1593 goto free;
1595 if (strcmp(key, "$") == 0)
1596 compare_type = get_arg_type(expr->fn, param);
1597 else
1598 compare_type = get_member_type_from_key(arg, key);
1600 sm = get_sm_state(SMATCH_EXTRA, name, sym);
1601 if (sm)
1602 rl = estate_rl(sm->state);
1603 else
1604 rl = alloc_whole_rl(compare_type);
1606 call_results_to_rl(expr, compare_type, value, &limit);
1607 new = rl_intersection(rl, limit);
1609 var_type = get_member_type_from_key(arg, key);
1610 new = cast_rl(var_type, new);
1612 /* We want to preserve the implications here */
1613 if (sm && rl_equiv(estate_rl(sm->state), new))
1614 __set_sm(sm);
1615 else
1616 set_extra_nomod(name, sym, alloc_estate_rl(new));
1618 free:
1619 free_string(name);
1622 static void db_param_add_set(struct expression *expr, int param, char *key, char *value, enum info_type op)
1624 struct expression *arg;
1625 char *name;
1626 struct symbol *sym;
1627 struct symbol *type;
1628 struct smatch_state *state;
1629 struct range_list *added = NULL;
1630 struct range_list *new;
1632 while (expr->type == EXPR_ASSIGNMENT)
1633 expr = strip_expr(expr->right);
1634 if (expr->type != EXPR_CALL)
1635 return;
1637 arg = get_argument_from_call_expr(expr->args, param);
1638 if (!arg)
1639 return;
1640 name = get_variable_from_key(arg, key, &sym);
1641 if (!name || !sym)
1642 goto free;
1644 type = get_member_type_from_key(arg, key);
1645 call_results_to_rl(expr, type, value, &added);
1647 if (op == PARAM_SET) {
1648 new = added;
1649 } else {
1650 state = get_state(SMATCH_EXTRA, name, sym);
1651 if (state)
1652 new = rl_union(estate_rl(state), added);
1653 else
1654 new = alloc_whole_rl(type);
1657 set_extra_mod(name, sym, alloc_estate_rl(new));
1658 free:
1659 free_string(name);
1662 static void db_param_add(struct expression *expr, int param, char *key, char *value)
1664 db_param_add_set(expr, param, key, value, ADDED_VALUE);
1667 static void db_param_set(struct expression *expr, int param, char *key, char *value)
1669 db_param_add_set(expr, param, key, value, PARAM_SET);
1672 static void db_returned_member_info(struct expression *expr, int param, char *key, char *value)
1674 struct symbol *type;
1675 struct symbol *sym;
1676 char *name;
1677 struct range_list *rl;
1679 if (expr->type != EXPR_ASSIGNMENT)
1680 return;
1682 name = return_state_to_var_sym(expr, param, key, &sym);
1683 if (!name || !sym)
1684 goto free;
1685 type = get_member_type_from_key(expr->left, key);
1686 if (!type)
1687 return;
1688 call_results_to_rl(expr->right, type, value, &rl);
1689 set_extra_mod(name, sym, alloc_estate_rl(rl));
1691 free:
1692 free_string(name);
1695 static char *in_terms_of_param_math(struct expression *expr, char *printed_name)
1697 struct symbol *sym;
1698 char *var;
1699 char buf[256];
1700 char *ret = NULL;
1702 var = expr_to_var_sym(expr, &sym);
1703 if (!var || !sym)
1704 goto free;
1705 snprintf(buf, sizeof(buf), "%s%s", var, printed_name + 2);
1707 ret = get_value_in_terms_of_parameter_math_var_sym(buf, sym);
1709 free:
1710 free_string(var);
1711 return ret;
1714 static void returned_member_callback(int return_id, char *return_ranges, struct expression *expr, char *printed_name, struct smatch_state *state)
1716 char buf[256];
1717 char *math_str;
1718 sval_t sval;
1720 /* these are handled in smatch_param_filter/set/limit.c */
1721 if (printed_name[0] != '*' &&
1722 !strchr(printed_name, '.') && !strchr(printed_name, '-'))
1723 return;
1725 if (estate_get_single_value(state, &sval)) {
1726 sql_insert_return_states(return_id, return_ranges, RETURN_VALUE, -1,
1727 printed_name, sval_to_str(sval));
1728 return;
1731 math_str = in_terms_of_param_math(expr, printed_name);
1732 if (math_str) {
1733 snprintf(buf, sizeof(buf), "%s[%s]", state->name, math_str);
1734 sql_insert_return_states(return_id, return_ranges, RETURN_VALUE, -1,
1735 printed_name, buf);
1736 return;
1739 if (estate_is_whole(state))
1740 return;
1742 sql_insert_return_states(return_id, return_ranges, RETURN_VALUE, -1,
1743 printed_name, state->name);
1746 static void match_call_info(struct expression *expr)
1748 struct smatch_state *state;
1749 struct range_list *rl = NULL;
1750 struct expression *arg;
1751 struct symbol *type;
1752 int i = 0;
1754 FOR_EACH_PTR(expr->args, arg) {
1755 type = get_arg_type(expr->fn, i);
1757 if (get_implied_rl(arg, &rl))
1758 rl = cast_rl(type, rl);
1759 else
1760 rl = cast_rl(type, alloc_whole_rl(get_type(arg)));
1762 if (!is_whole_rl(rl))
1763 sql_insert_caller_info(expr, PARAM_VALUE, i, "$", show_rl(rl));
1764 state = get_state_expr(SMATCH_EXTRA, arg);
1765 if (estate_has_fuzzy_max(state)) {
1766 sql_insert_caller_info(expr, FUZZY_MAX, i, "$",
1767 sval_to_str(estate_get_fuzzy_max(state)));
1769 i++;
1770 } END_FOR_EACH_PTR(arg);
1773 static void set_param_value(const char *name, struct symbol *sym, char *key, char *value)
1775 struct range_list *rl = NULL;
1776 struct smatch_state *state;
1777 struct symbol *type;
1778 char fullname[256];
1780 if (strcmp(key, "*$") == 0)
1781 snprintf(fullname, sizeof(fullname), "*%s", name);
1782 else if (strncmp(key, "$", 1) == 0)
1783 snprintf(fullname, 256, "%s%s", name, key + 1);
1784 else
1785 return;
1787 type = get_member_type_from_key(symbol_expression(sym), key);
1788 str_to_rl(type, value, &rl);
1789 state = alloc_estate_rl(rl);
1790 set_state(SMATCH_EXTRA, fullname, sym, state);
1793 static void set_param_hard_max(const char *name, struct symbol *sym, char *key, char *value)
1795 struct range_list *rl = NULL;
1796 struct smatch_state *state;
1797 struct symbol *type;
1798 char fullname[256];
1799 sval_t max;
1801 if (strcmp(key, "*$") == 0)
1802 snprintf(fullname, sizeof(fullname), "*%s", name);
1803 else if (strncmp(key, "$", 1) == 0)
1804 snprintf(fullname, 256, "%s%s", name, key + 1);
1805 else
1806 return;
1808 state = get_state(SMATCH_EXTRA, fullname, sym);
1809 if (!state)
1810 return;
1811 type = get_member_type_from_key(symbol_expression(sym), key);
1812 str_to_rl(type, value, &rl);
1813 if (!rl_to_sval(rl, &max))
1814 return;
1815 estate_set_fuzzy_max(state, max);
1818 void register_smatch_extra(int id)
1820 my_id = id;
1822 add_merge_hook(my_id, &merge_estates);
1823 add_unmatched_state_hook(my_id, &unmatched_state);
1824 add_hook(&match_function_def, FUNC_DEF_HOOK);
1825 add_hook(&match_declarations, DECLARATION_HOOK);
1826 select_caller_info_hook(set_param_value, PARAM_VALUE);
1827 select_caller_info_hook(set_param_hard_max, FUZZY_MAX);
1828 select_return_states_hook(RETURN_VALUE, &db_returned_member_info);
1829 select_return_states_before(&db_limited_before);
1830 select_return_states_hook(LIMITED_VALUE, &db_param_limit_filter);
1831 select_return_states_hook(FILTER_VALUE, &db_param_limit_filter);
1832 select_return_states_hook(ADDED_VALUE, &db_param_add);
1833 select_return_states_hook(PARAM_SET, &db_param_set);
1834 select_return_states_after(&db_limited_after);
1837 void register_smatch_extra_late(int id)
1839 add_hook(&match_dereferences, DEREF_HOOK);
1840 add_hook(&match_pointer_as_array, OP_HOOK);
1841 select_call_implies_hook(DEREFERENCE, &set_param_dereferenced);
1842 add_hook(&match_function_call, FUNCTION_CALL_HOOK);
1843 add_hook(&match_assign, ASSIGNMENT_HOOK);
1844 add_hook(&match_assign, GLOBAL_ASSIGNMENT_HOOK);
1845 add_hook(&unop_expr, OP_HOOK);
1846 add_hook(&asm_expr, ASM_HOOK);
1848 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
1849 add_member_info_callback(my_id, struct_member_callback);
1850 add_returned_member_callback(my_id, returned_member_callback);
1852 add_hook(&assume_indexes_are_valid, OP_HOOK);