extra: small speedup in set_param_hard_max()
[smatch.git] / smatch_extra.c
blob3c433f0bfd156919b5614ec32ac37e3067c3313b
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 #define _GNU_SOURCE
24 #include <string.h>
26 #include <stdlib.h>
27 #include <errno.h>
28 #ifndef __USE_ISOC99
29 #define __USE_ISOC99
30 #endif
31 #include <limits.h>
32 #include "parse.h"
33 #include "smatch.h"
34 #include "smatch_slist.h"
35 #include "smatch_extra.h"
37 static int my_id;
38 static int link_id;
40 static void match_link_modify(struct sm_state *sm, struct expression *mod_expr);
42 struct string_list *__ignored_macros = NULL;
43 int in_warn_on_macro(void)
45 struct statement *stmt;
46 char *tmp;
47 char *macro;
49 stmt = get_current_statement();
50 if (!stmt)
51 return 0;
52 macro = get_macro_name(stmt->pos);
53 if (!macro)
54 return 0;
56 FOR_EACH_PTR(__ignored_macros, tmp) {
57 if (!strcmp(tmp, macro))
58 return 1;
59 } END_FOR_EACH_PTR(tmp);
60 return 0;
63 typedef void (mod_hook)(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state);
64 DECLARE_PTR_LIST(void_fn_list, mod_hook *);
65 static struct void_fn_list *extra_mod_hooks;
66 static struct void_fn_list *extra_nomod_hooks;
68 void add_extra_mod_hook(mod_hook *fn)
70 mod_hook **p = malloc(sizeof(mod_hook *));
71 *p = fn;
72 add_ptr_list(&extra_mod_hooks, p);
75 void add_extra_nomod_hook(mod_hook *fn)
77 mod_hook **p = malloc(sizeof(mod_hook *));
78 *p = fn;
79 add_ptr_list(&extra_nomod_hooks, p);
82 void call_extra_hooks(struct void_fn_list *hooks, const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
84 mod_hook **fn;
86 FOR_EACH_PTR(hooks, fn) {
87 (*fn)(name, sym, expr, state);
88 } END_FOR_EACH_PTR(fn);
91 void call_extra_mod_hooks(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
93 call_extra_hooks(extra_mod_hooks, name, sym, expr, state);
96 void call_extra_nomod_hooks(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
98 call_extra_hooks(extra_nomod_hooks, name, sym, expr, state);
101 static bool in_param_set;
102 void set_extra_mod_helper(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
104 remove_from_equiv(name, sym);
105 call_extra_mod_hooks(name, sym, expr, state);
106 if ((__in_fake_assign || in_param_set) &&
107 estate_is_unknown(state) && !get_state(SMATCH_EXTRA, name, sym))
108 return;
109 set_state(SMATCH_EXTRA, name, sym, state);
112 static void set_extra_nomod_helper(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
114 call_extra_nomod_hooks(name, sym, expr, state);
115 set_state(SMATCH_EXTRA, name, sym, state);
118 static char *get_pointed_at(const char *name, struct symbol *sym, struct symbol **new_sym)
120 struct expression *assigned;
122 if (name[0] != '*')
123 return NULL;
124 if (strcmp(name + 1, sym->ident->name) != 0)
125 return NULL;
127 assigned = get_assigned_expr_name_sym(sym->ident->name, sym);
128 if (!assigned)
129 return NULL;
130 assigned = strip_parens(assigned);
131 if (assigned->type != EXPR_PREOP || assigned->op != '&')
132 return NULL;
134 return expr_to_var_sym(assigned->unop, new_sym);
137 char *get_other_name_sym_helper(const char *name, const char *chunk, int len, struct symbol *sym, struct symbol **new_sym)
139 struct expression *assigned;
140 char *orig_name = NULL;
141 char buf[256];
142 char *ret = NULL;
144 assigned = get_assigned_expr_name_sym(chunk, sym);
145 if (!assigned)
146 return NULL;
147 if (assigned->type == EXPR_CALL)
148 return map_call_to_other_name_sym(name, sym, new_sym);
149 if (assigned->type == EXPR_PREOP && assigned->op == '&') {
151 orig_name = expr_to_var_sym(assigned, new_sym);
152 if (!orig_name || !*new_sym)
153 goto free;
155 snprintf(buf, sizeof(buf), "%s.%s", orig_name + 1, name + len);
156 ret = alloc_string(buf);
157 free_string(orig_name);
158 return ret;
161 orig_name = expr_to_var_sym(assigned, new_sym);
162 if (!orig_name || !*new_sym)
163 goto free;
165 snprintf(buf, sizeof(buf), "%s->%s", orig_name, name + len);
166 ret = alloc_string(buf);
167 free_string(orig_name);
168 return ret;
169 free:
170 free_string(orig_name);
171 return NULL;
174 char *get_other_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym)
176 char buf[256];
177 char *ret;
178 int len;
180 *new_sym = NULL;
182 if (!sym || !sym->ident)
183 return NULL;
185 ret = get_pointed_at(name, sym, new_sym);
186 if (ret)
187 return ret;
189 len = snprintf(buf, sizeof(buf), "%s", name);
190 if (len >= sizeof(buf) - 2)
191 return NULL;
193 while (len >= 1) {
194 if (buf[len] == '>' && buf[len - 1] == '-') {
195 len--;
196 buf[len] = '\0';
197 ret = get_other_name_sym_helper(name, buf, len + 2, sym, new_sym);
198 if (ret)
199 return ret;
201 len--;
204 return NULL;
206 void set_extra_mod(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
208 char *new_name;
209 struct symbol *new_sym;
211 set_extra_mod_helper(name, sym, expr, state);
212 new_name = get_other_name_sym(name, sym, &new_sym);
213 if (new_name && new_sym)
214 set_extra_mod_helper(new_name, new_sym, expr, state);
215 free_string(new_name);
218 static struct expression *chunk_get_array_base(struct expression *expr)
221 * The problem with is_array() is that it only returns true for things
222 * like foo[1] but not for foo[1].bar.
225 expr = strip_expr(expr);
226 while (expr && expr->type == EXPR_DEREF)
227 expr = strip_expr(expr->deref);
228 return get_array_base(expr);
231 static int chunk_has_array(struct expression *expr)
233 return !!chunk_get_array_base(expr);
236 static void clear_array_states(struct expression *array)
238 struct sm_state *sm;
240 sm = get_sm_state_expr(link_id, array);
241 if (sm)
242 match_link_modify(sm, NULL);
245 static void set_extra_array_mod(struct expression *expr, struct smatch_state *state)
247 struct expression *array;
248 struct var_sym_list *vsl;
249 struct var_sym *vs;
250 char *name;
251 struct symbol *sym;
253 array = chunk_get_array_base(expr);
255 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
256 if (!name || !vsl) {
257 clear_array_states(array);
258 goto free;
261 FOR_EACH_PTR(vsl, vs) {
262 store_link(link_id, vs->var, vs->sym, name, sym);
263 } END_FOR_EACH_PTR(vs);
265 call_extra_mod_hooks(name, sym, expr, state);
266 set_state(SMATCH_EXTRA, name, sym, state);
267 free:
268 free_string(name);
271 void set_extra_expr_mod(struct expression *expr, struct smatch_state *state)
273 struct symbol *sym;
274 char *name;
276 if (chunk_has_array(expr)) {
277 set_extra_array_mod(expr, state);
278 return;
281 expr = strip_expr(expr);
282 name = expr_to_var_sym(expr, &sym);
283 if (!name || !sym)
284 goto free;
285 set_extra_mod(name, sym, expr, state);
286 free:
287 free_string(name);
290 void set_extra_nomod(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
292 char *new_name;
293 struct symbol *new_sym;
294 struct relation *rel;
295 struct smatch_state *orig_state;
297 orig_state = get_state(SMATCH_EXTRA, name, sym);
299 /* don't save unknown states if leaving it blank is the same */
300 if (!orig_state && estate_is_unknown(state))
301 return;
303 new_name = get_other_name_sym(name, sym, &new_sym);
304 if (new_name && new_sym)
305 set_extra_nomod_helper(new_name, new_sym, expr, state);
306 free_string(new_name);
308 if (!estate_related(orig_state)) {
309 set_extra_nomod_helper(name, sym, expr, state);
310 return;
313 set_related(state, estate_related(orig_state));
314 FOR_EACH_PTR(estate_related(orig_state), rel) {
315 struct smatch_state *estate;
317 if (option_debug_related)
318 sm_msg("%s updating related %s to %s", name, rel->name, state->name);
319 estate = get_state(SMATCH_EXTRA, rel->name, rel->sym);
320 if (!estate)
321 continue;
322 set_extra_nomod_helper(rel->name, rel->sym, expr, clone_estate_cast(estate_type(estate), state));
323 } END_FOR_EACH_PTR(rel);
326 void set_extra_nomod_vsl(const char *name, struct symbol *sym, struct var_sym_list *vsl, struct expression *expr, struct smatch_state *state)
328 struct var_sym *vs;
330 FOR_EACH_PTR(vsl, vs) {
331 store_link(link_id, vs->var, vs->sym, name, sym);
332 } END_FOR_EACH_PTR(vs);
334 set_extra_nomod(name, sym, expr, state);
338 * This is for return_implies_state() hooks which modify a SMATCH_EXTRA state
340 void set_extra_expr_nomod(struct expression *expr, struct smatch_state *state)
342 struct var_sym_list *vsl;
343 struct var_sym *vs;
344 char *name;
345 struct symbol *sym;
347 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
348 if (!name || !vsl)
349 goto free;
350 FOR_EACH_PTR(vsl, vs) {
351 store_link(link_id, vs->var, vs->sym, name, sym);
352 } END_FOR_EACH_PTR(vs);
354 set_extra_nomod(name, sym, expr, state);
355 free:
356 free_string(name);
359 static void set_extra_true_false(const char *name, struct symbol *sym,
360 struct smatch_state *true_state,
361 struct smatch_state *false_state)
363 char *new_name;
364 struct symbol *new_sym;
365 struct relation *rel;
366 struct smatch_state *orig_state;
368 if (!true_state && !false_state)
369 return;
371 if (in_warn_on_macro())
372 return;
374 new_name = get_other_name_sym(name, sym, &new_sym);
375 if (new_name && new_sym)
376 set_true_false_states(SMATCH_EXTRA, new_name, new_sym, true_state, false_state);
377 free_string(new_name);
379 orig_state = get_state(SMATCH_EXTRA, name, sym);
381 if (!estate_related(orig_state)) {
382 set_true_false_states(SMATCH_EXTRA, name, sym, true_state, false_state);
383 return;
386 if (true_state)
387 set_related(true_state, estate_related(orig_state));
388 if (false_state)
389 set_related(false_state, estate_related(orig_state));
391 FOR_EACH_PTR(estate_related(orig_state), rel) {
392 set_true_false_states(SMATCH_EXTRA, rel->name, rel->sym,
393 true_state, false_state);
394 } END_FOR_EACH_PTR(rel);
397 static void set_extra_chunk_true_false(struct expression *expr,
398 struct smatch_state *true_state,
399 struct smatch_state *false_state)
401 struct var_sym_list *vsl;
402 struct var_sym *vs;
403 struct symbol *type;
404 char *name;
405 struct symbol *sym;
407 if (in_warn_on_macro())
408 return;
410 type = get_type(expr);
411 if (!type)
412 return;
414 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
415 if (!name || !vsl)
416 goto free;
417 FOR_EACH_PTR(vsl, vs) {
418 store_link(link_id, vs->var, vs->sym, name, sym);
419 } END_FOR_EACH_PTR(vs);
421 set_true_false_states(SMATCH_EXTRA, name, sym,
422 clone_estate(true_state),
423 clone_estate(false_state));
424 free:
425 free_string(name);
428 static void set_extra_expr_true_false(struct expression *expr,
429 struct smatch_state *true_state,
430 struct smatch_state *false_state)
432 char *name;
433 struct symbol *sym;
434 sval_t sval;
436 if (!true_state && !false_state)
437 return;
439 if (get_value(expr, &sval))
440 return;
442 expr = strip_expr(expr);
443 name = expr_to_var_sym(expr, &sym);
444 if (!name || !sym) {
445 free_string(name);
446 set_extra_chunk_true_false(expr, true_state, false_state);
447 return;
449 set_extra_true_false(name, sym, true_state, false_state);
450 free_string(name);
453 static int get_countdown_info(struct expression *condition, struct expression **unop, int *op, sval_t *right)
455 struct expression *unop_expr;
456 int comparison;
457 sval_t limit;
459 right->type = &int_ctype;
460 right->value = 0;
462 condition = strip_expr(condition);
464 if (condition->type == EXPR_COMPARE) {
465 comparison = remove_unsigned_from_comparison(condition->op);
467 if (comparison != SPECIAL_GTE && comparison != '>')
468 return 0;
469 if (!get_value(condition->right, &limit))
470 return 0;
472 unop_expr = condition->left;
473 if (unop_expr->type != EXPR_PREOP && unop_expr->type != EXPR_POSTOP)
474 return 0;
475 if (unop_expr->op != SPECIAL_DECREMENT)
476 return 0;
478 *unop = unop_expr;
479 *op = comparison;
480 *right = limit;
482 return 1;
485 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
486 return 0;
487 if (condition->op != SPECIAL_DECREMENT)
488 return 0;
490 *unop = condition;
491 *op = '>';
493 return 1;
496 static struct sm_state *handle_canonical_while_count_down(struct statement *loop)
498 struct expression *iter_var;
499 struct expression *condition, *unop;
500 struct sm_state *sm;
501 struct smatch_state *estate;
502 int op;
503 sval_t start, right;
505 right.type = &int_ctype;
506 right.value = 0;
508 condition = strip_expr(loop->iterator_pre_condition);
509 if (!condition)
510 return NULL;
512 if (!get_countdown_info(condition, &unop, &op, &right))
513 return NULL;
515 iter_var = unop->unop;
517 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
518 if (!sm)
519 return NULL;
520 if (sval_cmp(estate_min(sm->state), right) < 0)
521 return NULL;
522 start = estate_max(sm->state);
523 if (sval_cmp(start, right) <= 0)
524 return NULL;
525 if (!sval_is_max(start))
526 start.value--;
528 if (op == SPECIAL_GTE)
529 right.value--;
531 if (unop->type == EXPR_PREOP) {
532 right.value++;
533 estate = alloc_estate_range(right, start);
534 if (estate_has_hard_max(sm->state))
535 estate_set_hard_max(estate);
536 estate_copy_fuzzy_max(estate, sm->state);
537 set_extra_expr_mod(iter_var, estate);
539 if (unop->type == EXPR_POSTOP) {
540 estate = alloc_estate_range(right, start);
541 if (estate_has_hard_max(sm->state))
542 estate_set_hard_max(estate);
543 estate_copy_fuzzy_max(estate, sm->state);
544 set_extra_expr_mod(iter_var, estate);
546 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
549 static struct sm_state *handle_canonical_for_inc(struct expression *iter_expr,
550 struct expression *condition)
552 struct expression *iter_var;
553 struct sm_state *sm;
554 struct smatch_state *estate;
555 sval_t start, end, max;
557 iter_var = iter_expr->unop;
558 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
559 if (!sm)
560 return NULL;
561 if (!estate_get_single_value(sm->state, &start))
562 return NULL;
563 if (!get_implied_value(condition->right, &end))
564 return NULL;
566 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
567 return NULL;
569 switch (condition->op) {
570 case SPECIAL_UNSIGNED_LT:
571 case SPECIAL_NOTEQUAL:
572 case '<':
573 if (!sval_is_min(end))
574 end.value--;
575 break;
576 case SPECIAL_UNSIGNED_LTE:
577 case SPECIAL_LTE:
578 break;
579 default:
580 return NULL;
582 if (sval_cmp(end, start) < 0)
583 return NULL;
584 estate = alloc_estate_range(start, end);
585 if (get_hard_max(condition->right, &max)) {
586 estate_set_hard_max(estate);
587 if (condition->op == '<' ||
588 condition->op == SPECIAL_UNSIGNED_LT ||
589 condition->op == SPECIAL_NOTEQUAL)
590 max.value--;
591 estate_set_fuzzy_max(estate, max);
593 set_extra_expr_mod(iter_var, estate);
594 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
597 static struct sm_state *handle_canonical_for_dec(struct expression *iter_expr,
598 struct expression *condition)
600 struct expression *iter_var;
601 struct sm_state *sm;
602 struct smatch_state *estate;
603 sval_t start, end;
605 iter_var = iter_expr->unop;
606 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
607 if (!sm)
608 return NULL;
609 if (!estate_get_single_value(sm->state, &start))
610 return NULL;
611 if (!get_implied_min(condition->right, &end))
612 end = sval_type_min(get_type(iter_var));
613 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
614 return NULL;
616 switch (condition->op) {
617 case SPECIAL_NOTEQUAL:
618 case '>':
619 if (!sval_is_min(end) && !sval_is_max(end))
620 end.value++;
621 break;
622 case SPECIAL_GTE:
623 break;
624 default:
625 return NULL;
627 if (sval_cmp(end, start) > 0)
628 return NULL;
629 estate = alloc_estate_range(end, start);
630 estate_set_hard_max(estate);
631 estate_set_fuzzy_max(estate, estate_get_fuzzy_max(estate));
632 set_extra_expr_mod(iter_var, estate);
633 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
636 static struct sm_state *handle_canonical_for_loops(struct statement *loop)
638 struct expression *iter_expr;
639 struct expression *condition;
641 if (!loop->iterator_post_statement)
642 return NULL;
643 if (loop->iterator_post_statement->type != STMT_EXPRESSION)
644 return NULL;
645 iter_expr = loop->iterator_post_statement->expression;
646 if (!loop->iterator_pre_condition)
647 return NULL;
648 if (loop->iterator_pre_condition->type != EXPR_COMPARE)
649 return NULL;
650 condition = loop->iterator_pre_condition;
652 if (iter_expr->op == SPECIAL_INCREMENT)
653 return handle_canonical_for_inc(iter_expr, condition);
654 if (iter_expr->op == SPECIAL_DECREMENT)
655 return handle_canonical_for_dec(iter_expr, condition);
656 return NULL;
659 struct sm_state *__extra_handle_canonical_loops(struct statement *loop, struct stree **stree)
661 struct sm_state *ret;
664 * Canonical loops are a hack. The proper way to handle this is to
665 * use two passes, but unfortunately, doing two passes makes parsing
666 * code twice as slow.
668 * What we do is we set the inside state here, which overwrites whatever
669 * __extra_match_condition() does. Then we set the outside state in
670 * __extra_pre_loop_hook_after().
673 __push_fake_cur_stree();
674 if (!loop->iterator_post_statement)
675 ret = handle_canonical_while_count_down(loop);
676 else
677 ret = handle_canonical_for_loops(loop);
678 *stree = __pop_fake_cur_stree();
679 return ret;
682 int __iterator_unchanged(struct sm_state *sm)
684 if (!sm)
685 return 0;
686 if (get_sm_state(my_id, sm->name, sm->sym) == sm)
687 return 1;
688 return 0;
691 static void while_count_down_after(struct sm_state *sm, struct expression *condition)
693 struct expression *unop;
694 int op;
695 sval_t limit, after_value;
697 if (!get_countdown_info(condition, &unop, &op, &limit))
698 return;
699 after_value = estate_min(sm->state);
700 after_value.value--;
701 set_extra_mod(sm->name, sm->sym, condition->unop, alloc_estate_sval(after_value));
704 void __extra_pre_loop_hook_after(struct sm_state *sm,
705 struct statement *iterator,
706 struct expression *condition)
708 struct expression *iter_expr;
709 sval_t limit;
710 struct smatch_state *state;
712 if (!iterator) {
713 while_count_down_after(sm, condition);
714 return;
717 iter_expr = iterator->expression;
719 if (condition->type != EXPR_COMPARE)
720 return;
721 if (iter_expr->op == SPECIAL_INCREMENT) {
722 limit = sval_binop(estate_max(sm->state), '+',
723 sval_type_val(estate_type(sm->state), 1));
724 } else {
725 limit = sval_binop(estate_min(sm->state), '-',
726 sval_type_val(estate_type(sm->state), 1));
728 if (!estate_has_hard_max(sm->state) && !__has_breaks()) {
729 if (iter_expr->op == SPECIAL_INCREMENT)
730 state = alloc_estate_range(estate_min(sm->state), limit);
731 else
732 state = alloc_estate_range(limit, estate_max(sm->state));
733 } else {
734 state = alloc_estate_sval(limit);
736 if (!estate_has_hard_max(sm->state)) {
737 estate_clear_hard_max(state);
739 if (estate_has_fuzzy_max(sm->state)) {
740 sval_t hmax = estate_get_fuzzy_max(sm->state);
741 sval_t max = estate_max(sm->state);
743 if (sval_cmp(hmax, max) != 0)
744 estate_clear_fuzzy_max(state);
745 } else if (!estate_has_fuzzy_max(sm->state)) {
746 estate_clear_fuzzy_max(state);
749 set_extra_mod(sm->name, sm->sym, iter_expr, state);
752 static struct stree *unmatched_stree;
753 static struct smatch_state *unmatched_state(struct sm_state *sm)
755 struct smatch_state *state;
757 if (unmatched_stree) {
758 state = get_state_stree(unmatched_stree, SMATCH_EXTRA, sm->name, sm->sym);
759 if (state)
760 return state;
762 if (parent_is_gone_var_sym(sm->name, sm->sym))
763 return alloc_estate_empty();
764 return alloc_estate_whole(estate_type(sm->state));
767 static void clear_the_pointed_at(struct expression *expr)
769 struct stree *stree;
770 char *name;
771 struct symbol *sym;
772 struct sm_state *tmp;
774 name = expr_to_var_sym(expr, &sym);
775 if (!name || !sym)
776 goto free;
778 stree = __get_cur_stree();
779 FOR_EACH_MY_SM(SMATCH_EXTRA, stree, tmp) {
780 if (tmp->name[0] != '*')
781 continue;
782 if (tmp->sym != sym)
783 continue;
784 if (strcmp(tmp->name + 1, name) != 0)
785 continue;
786 set_extra_mod(tmp->name, tmp->sym, expr, alloc_estate_whole(estate_type(tmp->state)));
787 } END_FOR_EACH_SM(tmp);
789 free:
790 free_string(name);
793 static int is_const_param(struct expression *expr, int param)
795 struct symbol *type;
797 type = get_arg_type(expr, param);
798 if (!type)
799 return 0;
800 if (type->ctype.modifiers & MOD_CONST)
801 return 1;
802 return 0;
805 static void match_function_call(struct expression *expr)
807 struct expression *arg;
808 struct expression *tmp;
809 int param = -1;
811 /* if we have the db this is handled in smatch_function_hooks.c */
812 if (!option_no_db)
813 return;
814 if (inlinable(expr->fn))
815 return;
817 FOR_EACH_PTR(expr->args, arg) {
818 param++;
819 if (is_const_param(expr->fn, param))
820 continue;
821 tmp = strip_expr(arg);
822 if (tmp->type == EXPR_PREOP && tmp->op == '&')
823 set_extra_expr_mod(tmp->unop, alloc_estate_whole(get_type(tmp->unop)));
824 else
825 clear_the_pointed_at(tmp);
826 } END_FOR_EACH_PTR(arg);
829 static int values_fit_type(struct expression *left, struct expression *right)
831 struct range_list *rl;
832 struct symbol *type;
834 type = get_type(left);
835 if (!type)
836 return 0;
837 get_absolute_rl(right, &rl);
838 if (type_unsigned(type) && sval_is_negative(rl_min(rl)))
839 return 0;
840 if (sval_cmp(sval_type_min(type), rl_min(rl)) > 0)
841 return 0;
842 if (sval_cmp(sval_type_max(type), rl_max(rl)) < 0)
843 return 0;
844 return 1;
847 static void save_chunk_info(struct expression *left, struct expression *right)
849 struct var_sym_list *vsl;
850 struct var_sym *vs;
851 struct expression *add_expr;
852 struct symbol *type;
853 sval_t sval;
854 char *name;
855 struct symbol *sym;
857 if (right->type != EXPR_BINOP || right->op != '-')
858 return;
859 if (!get_value(right->left, &sval))
860 return;
861 if (!expr_to_sym(right->right))
862 return;
864 add_expr = binop_expression(left, '+', right->right);
865 type = get_type(add_expr);
866 if (!type)
867 return;
868 name = expr_to_chunk_sym_vsl(add_expr, &sym, &vsl);
869 if (!name || !vsl)
870 goto free;
871 FOR_EACH_PTR(vsl, vs) {
872 store_link(link_id, vs->var, vs->sym, name, sym);
873 } END_FOR_EACH_PTR(vs);
875 set_state(SMATCH_EXTRA, name, sym, alloc_estate_sval(sval_cast(type, sval)));
876 free:
877 free_string(name);
880 static void do_array_assign(struct expression *left, int op, struct expression *right)
882 struct range_list *rl;
884 if (op == '=') {
885 get_absolute_rl(right, &rl);
886 rl = cast_rl(get_type(left), rl);
887 } else {
888 rl = alloc_whole_rl(get_type(left));
891 set_extra_array_mod(left, alloc_estate_rl(rl));
894 static void match_vanilla_assign(struct expression *left, struct expression *right)
896 struct range_list *orig_rl = NULL;
897 struct range_list *rl = NULL;
898 struct symbol *right_sym;
899 struct symbol *left_type;
900 struct symbol *right_type;
901 char *right_name = NULL;
902 struct symbol *sym;
903 char *name;
904 sval_t sval, max;
905 struct smatch_state *state;
906 int comparison;
908 if (is_struct(left))
909 return;
911 save_chunk_info(left, right);
913 name = expr_to_var_sym(left, &sym);
914 if (!name) {
915 if (chunk_has_array(left))
916 do_array_assign(left, '=', right);
917 return;
920 left_type = get_type(left);
921 right_type = get_type(right);
923 right_name = expr_to_var_sym(right, &right_sym);
925 if (!__in_fake_assign &&
926 !(right->type == EXPR_PREOP && right->op == '&') &&
927 right_name && right_sym &&
928 values_fit_type(left, strip_expr(right)) &&
929 !has_symbol(right, sym)) {
930 set_equiv(left, right);
931 goto free;
934 if (is_pointer(right) && get_address_rl(right, &rl)) {
935 state = alloc_estate_rl(rl);
936 goto done;
939 if (get_implied_value(right, &sval)) {
940 state = alloc_estate_sval(sval_cast(left_type, sval));
941 goto done;
944 if (__in_fake_assign) {
945 struct smatch_state *right_state;
946 sval_t sval;
948 if (get_value(right, &sval)) {
949 sval = sval_cast(left_type, sval);
950 state = alloc_estate_sval(sval);
951 goto done;
954 right_state = get_state(SMATCH_EXTRA, right_name, right_sym);
955 if (right_state) {
956 /* simple assignment */
957 state = clone_estate(right_state);
958 goto done;
961 state = alloc_estate_rl(alloc_whole_rl(left_type));
962 goto done;
965 comparison = get_comparison(left, right);
966 if (comparison) {
967 comparison = flip_comparison(comparison);
968 get_implied_rl(left, &orig_rl);
971 if (get_implied_rl(right, &rl)) {
972 rl = cast_rl(left_type, rl);
973 if (orig_rl)
974 filter_by_comparison(&rl, comparison, orig_rl);
975 state = alloc_estate_rl(rl);
976 if (get_hard_max(right, &max)) {
977 estate_set_hard_max(state);
978 estate_set_fuzzy_max(state, max);
980 } else {
981 rl = alloc_whole_rl(right_type);
982 rl = cast_rl(left_type, rl);
983 if (orig_rl)
984 filter_by_comparison(&rl, comparison, orig_rl);
985 state = alloc_estate_rl(rl);
988 done:
989 set_extra_mod(name, sym, left, state);
990 free:
991 free_string(right_name);
994 static int op_remove_assign(int op)
996 switch (op) {
997 case SPECIAL_ADD_ASSIGN:
998 return '+';
999 case SPECIAL_SUB_ASSIGN:
1000 return '-';
1001 case SPECIAL_MUL_ASSIGN:
1002 return '*';
1003 case SPECIAL_DIV_ASSIGN:
1004 return '/';
1005 case SPECIAL_MOD_ASSIGN:
1006 return '%';
1007 case SPECIAL_AND_ASSIGN:
1008 return '&';
1009 case SPECIAL_OR_ASSIGN:
1010 return '|';
1011 case SPECIAL_XOR_ASSIGN:
1012 return '^';
1013 case SPECIAL_SHL_ASSIGN:
1014 return SPECIAL_LEFTSHIFT;
1015 case SPECIAL_SHR_ASSIGN:
1016 return SPECIAL_RIGHTSHIFT;
1017 default:
1018 return op;
1022 static void match_assign(struct expression *expr)
1024 struct range_list *rl = NULL;
1025 struct expression *left;
1026 struct expression *right;
1027 struct expression *binop_expr;
1028 struct symbol *left_type;
1029 struct symbol *sym;
1030 char *name;
1031 sval_t left_min, left_max;
1032 sval_t right_min, right_max;
1033 sval_t res_min, res_max;
1035 left = strip_expr(expr->left);
1037 right = strip_parens(expr->right);
1038 if (right->type == EXPR_CALL && sym_name_is("__builtin_expect", right->fn))
1039 right = get_argument_from_call_expr(right->args, 0);
1040 while (right->type == EXPR_ASSIGNMENT && right->op == '=')
1041 right = strip_parens(right->left);
1043 if (expr->op == '=' && is_condition(expr->right))
1044 return; /* handled in smatch_condition.c */
1045 if (expr->op == '=' && right->type == EXPR_CALL)
1046 return; /* handled in smatch_function_hooks.c */
1047 if (expr->op == '=') {
1048 match_vanilla_assign(left, right);
1049 return;
1052 name = expr_to_var_sym(left, &sym);
1053 if (!name)
1054 return;
1056 left_type = get_type(left);
1058 res_min = sval_type_min(left_type);
1059 res_max = sval_type_max(left_type);
1061 switch (expr->op) {
1062 case SPECIAL_ADD_ASSIGN:
1063 get_absolute_max(left, &left_max);
1064 get_absolute_max(right, &right_max);
1065 if (sval_binop_overflows(left_max, '+', sval_cast(left_type, right_max)))
1066 break;
1067 if (get_implied_min(left, &left_min) &&
1068 !sval_is_negative_min(left_min) &&
1069 get_implied_min(right, &right_min) &&
1070 !sval_is_negative_min(right_min)) {
1071 res_min = sval_binop(left_min, '+', right_min);
1072 res_min = sval_cast(left_type, res_min);
1074 if (inside_loop()) /* we are assuming loops don't lead to wrapping */
1075 break;
1076 res_max = sval_binop(left_max, '+', right_max);
1077 res_max = sval_cast(left_type, res_max);
1078 break;
1079 case SPECIAL_SUB_ASSIGN:
1080 if (get_implied_max(left, &left_max) &&
1081 !sval_is_max(left_max) &&
1082 get_implied_min(right, &right_min) &&
1083 !sval_is_min(right_min)) {
1084 res_max = sval_binop(left_max, '-', right_min);
1085 res_max = sval_cast(left_type, res_max);
1087 if (inside_loop())
1088 break;
1089 if (get_implied_min(left, &left_min) &&
1090 !sval_is_min(left_min) &&
1091 get_implied_max(right, &right_max) &&
1092 !sval_is_max(right_max)) {
1093 res_min = sval_binop(left_min, '-', right_max);
1094 res_min = sval_cast(left_type, res_min);
1096 break;
1097 case SPECIAL_AND_ASSIGN:
1098 case SPECIAL_MOD_ASSIGN:
1099 case SPECIAL_SHL_ASSIGN:
1100 case SPECIAL_SHR_ASSIGN:
1101 case SPECIAL_OR_ASSIGN:
1102 case SPECIAL_XOR_ASSIGN:
1103 case SPECIAL_MUL_ASSIGN:
1104 case SPECIAL_DIV_ASSIGN:
1105 binop_expr = binop_expression(expr->left,
1106 op_remove_assign(expr->op),
1107 expr->right);
1108 if (get_absolute_rl(binop_expr, &rl)) {
1109 rl = cast_rl(left_type, rl);
1110 set_extra_mod(name, sym, left, alloc_estate_rl(rl));
1111 goto free;
1113 break;
1115 rl = cast_rl(left_type, alloc_rl(res_min, res_max));
1116 set_extra_mod(name, sym, left, alloc_estate_rl(rl));
1117 free:
1118 free_string(name);
1121 static struct smatch_state *increment_state(struct smatch_state *state)
1123 sval_t min = estate_min(state);
1124 sval_t max = estate_max(state);
1126 if (!estate_rl(state))
1127 return NULL;
1129 if (inside_loop())
1130 max = sval_type_max(max.type);
1132 if (!sval_is_min(min) && !sval_is_max(min))
1133 min.value++;
1134 if (!sval_is_min(max) && !sval_is_max(max))
1135 max.value++;
1136 return alloc_estate_range(min, max);
1139 static struct smatch_state *decrement_state(struct smatch_state *state)
1141 sval_t min = estate_min(state);
1142 sval_t max = estate_max(state);
1144 if (!estate_rl(state))
1145 return NULL;
1147 if (inside_loop())
1148 min = sval_type_min(min.type);
1150 if (!sval_is_min(min) && !sval_is_max(min))
1151 min.value--;
1152 if (!sval_is_min(max) && !sval_is_max(max))
1153 max.value--;
1154 return alloc_estate_range(min, max);
1157 static void clear_pointed_at_state(struct expression *expr)
1159 struct symbol *type;
1162 * ALERT: This is sort of a mess. If it's is a struct assigment like
1163 * "foo = bar;", then that's handled by smatch_struct_assignment.c.
1164 * the same thing for p++ where "p" is a struct. Most modifications
1165 * are handled by the assignment hook or the db. Smatch_extra.c doesn't
1166 * use smatch_modification.c because we have to get the ordering right
1167 * or something. So if you have p++ where p is a pointer to a standard
1168 * c type then we handle that here. What a mess.
1170 expr = strip_expr(expr);
1171 type = get_type(expr);
1172 if (!type || type->type != SYM_PTR)
1173 return;
1174 type = get_real_base_type(type);
1175 if (!type || type->type != SYM_BASETYPE)
1176 return;
1177 set_extra_expr_nomod(deref_expression(expr), alloc_estate_whole(type));
1180 static void unop_expr(struct expression *expr)
1182 struct smatch_state *state;
1184 if (expr->smatch_flags & Handled)
1185 return;
1187 switch (expr->op) {
1188 case SPECIAL_INCREMENT:
1189 state = get_state_expr(SMATCH_EXTRA, expr->unop);
1190 state = increment_state(state);
1191 if (!state)
1192 state = alloc_estate_whole(get_type(expr));
1193 set_extra_expr_mod(expr->unop, state);
1194 clear_pointed_at_state(expr->unop);
1195 break;
1196 case SPECIAL_DECREMENT:
1197 state = get_state_expr(SMATCH_EXTRA, expr->unop);
1198 state = decrement_state(state);
1199 if (!state)
1200 state = alloc_estate_whole(get_type(expr));
1201 set_extra_expr_mod(expr->unop, state);
1202 clear_pointed_at_state(expr->unop);
1203 break;
1204 default:
1205 return;
1209 static void asm_expr(struct statement *stmt)
1212 struct expression *expr;
1213 struct symbol *type;
1214 int state = 0;
1216 FOR_EACH_PTR(stmt->asm_outputs, expr) {
1217 switch (state) {
1218 case 0: /* identifier */
1219 case 1: /* constraint */
1220 state++;
1221 continue;
1222 case 2: /* expression */
1223 state = 0;
1224 type = get_type(strip_expr(expr));
1225 set_extra_expr_mod(expr, alloc_estate_whole(type));
1226 continue;
1228 } END_FOR_EACH_PTR(expr);
1231 static void check_dereference(struct expression *expr)
1233 struct smatch_state *state;
1235 if (__in_fake_assign)
1236 return;
1237 if (outside_of_function())
1238 return;
1239 state = get_extra_state(expr);
1240 if (state) {
1241 struct range_list *rl;
1243 rl = rl_intersection(estate_rl(state), valid_ptr_rl);
1244 if (rl_equiv(rl, estate_rl(state)))
1245 return;
1246 set_extra_expr_nomod(expr, alloc_estate_rl(rl));
1247 } else {
1248 set_extra_expr_nomod(expr, alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval));
1252 static void match_dereferences(struct expression *expr)
1254 if (expr->type != EXPR_PREOP)
1255 return;
1256 /* it's saying that foo[1] = bar dereferences foo[1] */
1257 if (is_array(expr))
1258 return;
1259 check_dereference(expr->unop);
1262 static void match_pointer_as_array(struct expression *expr)
1264 if (!is_array(expr))
1265 return;
1266 check_dereference(get_array_base(expr));
1269 static void find_dereferences(struct expression *expr)
1271 while (expr->type == EXPR_PREOP) {
1272 if (expr->op == '*')
1273 check_dereference(expr->unop);
1274 expr = strip_expr(expr->unop);
1278 static void set_param_dereferenced(struct expression *call, struct expression *arg, char *key, char *unused)
1280 struct symbol *sym;
1281 char *name;
1283 name = get_variable_from_key(arg, key, &sym);
1284 if (name && sym) {
1285 struct smatch_state *orig, *new;
1286 struct range_list *rl;
1288 orig = get_state(SMATCH_EXTRA, name, sym);
1289 if (orig) {
1290 rl = rl_intersection(estate_rl(orig),
1291 alloc_rl(valid_ptr_min_sval,
1292 valid_ptr_max_sval));
1293 new = alloc_estate_rl(rl);
1294 } else {
1295 new = alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval);
1298 set_extra_nomod(name, sym, NULL, new);
1300 free_string(name);
1302 find_dereferences(arg);
1305 static sval_t add_one(sval_t sval)
1307 sval.value++;
1308 return sval;
1311 static int handle_postop_inc(struct expression *left, int op, struct expression *right)
1313 struct statement *stmt;
1314 struct expression *cond;
1315 struct smatch_state *true_state, *false_state;
1316 sval_t start;
1317 sval_t limit;
1320 * If we're decrementing here then that's a canonical while count down
1321 * so it's handled already. We're only handling loops like:
1322 * i = 0;
1323 * do { ... } while (i++ < 3);
1326 if (left->type != EXPR_POSTOP || left->op != SPECIAL_INCREMENT)
1327 return 0;
1329 stmt = __cur_stmt->parent;
1330 if (!stmt)
1331 return 0;
1332 if (stmt->type == STMT_COMPOUND)
1333 stmt = stmt->parent;
1334 if (!stmt || stmt->type != STMT_ITERATOR || !stmt->iterator_post_condition)
1335 return 0;
1337 cond = strip_expr(stmt->iterator_post_condition);
1338 if (cond->type != EXPR_COMPARE || cond->op != op)
1339 return 0;
1340 if (left != strip_expr(cond->left) || right != strip_expr(cond->right))
1341 return 0;
1343 if (!get_implied_value(left->unop, &start))
1344 return 0;
1345 if (!get_implied_value(right, &limit))
1346 return 0;
1348 if (sval_cmp(start, limit) > 0)
1349 return 0;
1351 switch (op) {
1352 case '<':
1353 case SPECIAL_UNSIGNED_LT:
1354 break;
1355 case SPECIAL_LTE:
1356 case SPECIAL_UNSIGNED_LTE:
1357 limit = add_one(limit);
1358 default:
1359 return 0;
1363 true_state = alloc_estate_range(add_one(start), limit);
1364 false_state = alloc_estate_range(add_one(limit), add_one(limit));
1366 /* Currently we just discard the false state but when two passes is
1367 * implimented correctly then it will use it.
1370 set_extra_expr_true_false(left->unop, true_state, false_state);
1372 return 1;
1375 bool is_impossible_variable(struct expression *expr)
1377 struct smatch_state *state;
1379 state = get_extra_state(expr);
1380 if (state && !estate_rl(state))
1381 return true;
1382 return false;
1385 static void handle_comparison(struct symbol *type, struct expression *left, int op, struct expression *right)
1387 struct range_list *left_orig;
1388 struct range_list *left_true;
1389 struct range_list *left_false;
1390 struct range_list *right_orig;
1391 struct range_list *right_true;
1392 struct range_list *right_false;
1393 struct smatch_state *left_true_state;
1394 struct smatch_state *left_false_state;
1395 struct smatch_state *right_true_state;
1396 struct smatch_state *right_false_state;
1397 sval_t dummy, hard_max;
1398 int left_postop = 0;
1399 int right_postop = 0;
1401 if (left->op == SPECIAL_INCREMENT || left->op == SPECIAL_DECREMENT) {
1402 if (left->type == EXPR_POSTOP) {
1403 left->smatch_flags |= Handled;
1404 left_postop = left->op;
1405 if (handle_postop_inc(left, op, right))
1406 return;
1408 left = strip_parens(left->unop);
1410 while (left->type == EXPR_ASSIGNMENT)
1411 left = strip_parens(left->left);
1413 if (right->op == SPECIAL_INCREMENT || right->op == SPECIAL_DECREMENT) {
1414 if (right->type == EXPR_POSTOP) {
1415 right->smatch_flags |= Handled;
1416 right_postop = right->op;
1418 right = strip_parens(right->unop);
1421 if (is_impossible_variable(left) || is_impossible_variable(right))
1422 return;
1424 get_real_absolute_rl(left, &left_orig);
1425 left_orig = cast_rl(type, left_orig);
1427 get_real_absolute_rl(right, &right_orig);
1428 right_orig = cast_rl(type, right_orig);
1430 split_comparison_rl(left_orig, op, right_orig, &left_true, &left_false, &right_true, &right_false);
1432 left_true = rl_truncate_cast(get_type(strip_expr(left)), left_true);
1433 left_false = rl_truncate_cast(get_type(strip_expr(left)), left_false);
1434 right_true = rl_truncate_cast(get_type(strip_expr(right)), right_true);
1435 right_false = rl_truncate_cast(get_type(strip_expr(right)), right_false);
1437 if (!left_true || !left_false) {
1438 struct range_list *tmp_true, *tmp_false;
1440 split_comparison_rl(alloc_whole_rl(type), op, right_orig, &tmp_true, &tmp_false, NULL, NULL);
1441 tmp_true = rl_truncate_cast(get_type(strip_expr(left)), tmp_true);
1442 tmp_false = rl_truncate_cast(get_type(strip_expr(left)), tmp_false);
1443 if (tmp_true && tmp_false)
1444 __save_imaginary_state(left, tmp_true, tmp_false);
1447 if (!right_true || !right_false) {
1448 struct range_list *tmp_true, *tmp_false;
1450 split_comparison_rl(alloc_whole_rl(type), op, right_orig, NULL, NULL, &tmp_true, &tmp_false);
1451 tmp_true = rl_truncate_cast(get_type(strip_expr(right)), tmp_true);
1452 tmp_false = rl_truncate_cast(get_type(strip_expr(right)), tmp_false);
1453 if (tmp_true && tmp_false)
1454 __save_imaginary_state(right, tmp_true, tmp_false);
1457 left_true_state = alloc_estate_rl(left_true);
1458 left_false_state = alloc_estate_rl(left_false);
1459 right_true_state = alloc_estate_rl(right_true);
1460 right_false_state = alloc_estate_rl(right_false);
1462 switch (op) {
1463 case '<':
1464 case SPECIAL_UNSIGNED_LT:
1465 case SPECIAL_UNSIGNED_LTE:
1466 case SPECIAL_LTE:
1467 if (get_hard_max(right, &dummy))
1468 estate_set_hard_max(left_true_state);
1469 if (get_hard_max(left, &dummy))
1470 estate_set_hard_max(right_false_state);
1471 break;
1472 case '>':
1473 case SPECIAL_UNSIGNED_GT:
1474 case SPECIAL_UNSIGNED_GTE:
1475 case SPECIAL_GTE:
1476 if (get_hard_max(left, &dummy))
1477 estate_set_hard_max(right_true_state);
1478 if (get_hard_max(right, &dummy))
1479 estate_set_hard_max(left_false_state);
1480 break;
1483 switch (op) {
1484 case '<':
1485 case SPECIAL_UNSIGNED_LT:
1486 case SPECIAL_UNSIGNED_LTE:
1487 case SPECIAL_LTE:
1488 if (get_hard_max(right, &hard_max)) {
1489 if (op == '<' || op == SPECIAL_UNSIGNED_LT)
1490 hard_max.value--;
1491 estate_set_fuzzy_max(left_true_state, hard_max);
1493 if (get_implied_value(right, &hard_max)) {
1494 if (op == SPECIAL_UNSIGNED_LTE ||
1495 op == SPECIAL_LTE)
1496 hard_max.value++;
1497 estate_set_fuzzy_max(left_false_state, hard_max);
1499 if (get_hard_max(left, &hard_max)) {
1500 if (op == SPECIAL_UNSIGNED_LTE ||
1501 op == SPECIAL_LTE)
1502 hard_max.value--;
1503 estate_set_fuzzy_max(right_false_state, hard_max);
1505 if (get_implied_value(left, &hard_max)) {
1506 if (op == '<' || op == SPECIAL_UNSIGNED_LT)
1507 hard_max.value++;
1508 estate_set_fuzzy_max(right_true_state, hard_max);
1510 break;
1511 case '>':
1512 case SPECIAL_UNSIGNED_GT:
1513 case SPECIAL_UNSIGNED_GTE:
1514 case SPECIAL_GTE:
1515 if (get_hard_max(left, &hard_max)) {
1516 if (op == '>' || op == SPECIAL_UNSIGNED_GT)
1517 hard_max.value--;
1518 estate_set_fuzzy_max(right_true_state, hard_max);
1520 if (get_implied_value(left, &hard_max)) {
1521 if (op == SPECIAL_UNSIGNED_GTE ||
1522 op == SPECIAL_GTE)
1523 hard_max.value++;
1524 estate_set_fuzzy_max(right_false_state, hard_max);
1526 if (get_hard_max(right, &hard_max)) {
1527 if (op == SPECIAL_UNSIGNED_LTE ||
1528 op == SPECIAL_LTE)
1529 hard_max.value--;
1530 estate_set_fuzzy_max(left_false_state, hard_max);
1532 if (get_implied_value(right, &hard_max)) {
1533 if (op == '>' ||
1534 op == SPECIAL_UNSIGNED_GT)
1535 hard_max.value++;
1536 estate_set_fuzzy_max(left_true_state, hard_max);
1538 break;
1539 case SPECIAL_EQUAL:
1540 if (get_hard_max(left, &hard_max))
1541 estate_set_fuzzy_max(right_true_state, hard_max);
1542 if (get_hard_max(right, &hard_max))
1543 estate_set_fuzzy_max(left_true_state, hard_max);
1544 break;
1547 if (get_hard_max(left, &hard_max)) {
1548 estate_set_hard_max(left_true_state);
1549 estate_set_hard_max(left_false_state);
1551 if (get_hard_max(right, &hard_max)) {
1552 estate_set_hard_max(right_true_state);
1553 estate_set_hard_max(right_false_state);
1556 if (left_postop == SPECIAL_INCREMENT) {
1557 left_true_state = increment_state(left_true_state);
1558 left_false_state = increment_state(left_false_state);
1560 if (left_postop == SPECIAL_DECREMENT) {
1561 left_true_state = decrement_state(left_true_state);
1562 left_false_state = decrement_state(left_false_state);
1564 if (right_postop == SPECIAL_INCREMENT) {
1565 right_true_state = increment_state(right_true_state);
1566 right_false_state = increment_state(right_false_state);
1568 if (right_postop == SPECIAL_DECREMENT) {
1569 right_true_state = decrement_state(right_true_state);
1570 right_false_state = decrement_state(right_false_state);
1573 if (estate_rl(left_true_state) && estates_equiv(left_true_state, left_false_state)) {
1574 left_true_state = NULL;
1575 left_false_state = NULL;
1578 if (estate_rl(right_true_state) && estates_equiv(right_true_state, right_false_state)) {
1579 right_true_state = NULL;
1580 right_false_state = NULL;
1583 /* Don't introduce new states for known true/false conditions */
1584 if (rl_equiv(left_orig, estate_rl(left_true_state)))
1585 left_true_state = NULL;
1586 if (rl_equiv(left_orig, estate_rl(left_false_state)))
1587 left_false_state = NULL;
1588 if (rl_equiv(right_orig, estate_rl(right_true_state)))
1589 right_true_state = NULL;
1590 if (rl_equiv(right_orig, estate_rl(right_false_state)))
1591 right_false_state = NULL;
1593 set_extra_expr_true_false(left, left_true_state, left_false_state);
1594 set_extra_expr_true_false(right, right_true_state, right_false_state);
1597 static int is_simple_math(struct expression *expr)
1599 if (!expr)
1600 return 0;
1601 if (expr->type != EXPR_BINOP)
1602 return 0;
1603 switch (expr->op) {
1604 case '+':
1605 case '-':
1606 case '*':
1607 return 1;
1609 return 0;
1612 static void move_known_values(struct expression **left_p, struct expression **right_p)
1614 struct expression *left = *left_p;
1615 struct expression *right = *right_p;
1616 sval_t sval, dummy;
1618 if (get_implied_value(left, &sval)) {
1619 if (!is_simple_math(right))
1620 return;
1621 if (get_implied_value(right, &dummy))
1622 return;
1623 if (right->op == '*') {
1624 sval_t divisor;
1626 if (!get_value(right->right, &divisor))
1627 return;
1628 if (divisor.value == 0)
1629 return;
1630 *left_p = binop_expression(left, invert_op(right->op), right->right);
1631 *right_p = right->left;
1632 return;
1634 if (right->op == '+' && get_value(right->left, &sval)) {
1635 *left_p = binop_expression(left, invert_op(right->op), right->left);
1636 *right_p = right->right;
1637 return;
1639 if (get_value(right->right, &sval)) {
1640 *left_p = binop_expression(left, invert_op(right->op), right->right);
1641 *right_p = right->left;
1642 return;
1644 return;
1646 if (get_implied_value(right, &sval)) {
1647 if (!is_simple_math(left))
1648 return;
1649 if (get_implied_value(left, &dummy))
1650 return;
1651 if (left->op == '*') {
1652 sval_t divisor;
1654 if (!get_value(left->right, &divisor))
1655 return;
1656 if (divisor.value == 0)
1657 return;
1658 *right_p = binop_expression(right, invert_op(left->op), left->right);
1659 *left_p = left->left;
1660 return;
1662 if (left->op == '+' && get_value(left->left, &sval)) {
1663 *right_p = binop_expression(right, invert_op(left->op), left->left);
1664 *left_p = left->right;
1665 return;
1668 if (get_value(left->right, &sval)) {
1669 *right_p = binop_expression(right, invert_op(left->op), left->right);
1670 *left_p = left->left;
1671 return;
1673 return;
1678 * The reason for do_simple_algebra() is to solve things like:
1679 * if (foo > 66 || foo + bar > 64) {
1680 * "foo" is not really a known variable so it won't be handled by
1681 * move_known_variables() but it's a super common idiom.
1684 static int do_simple_algebra(struct expression **left_p, struct expression **right_p)
1686 struct expression *left = *left_p;
1687 struct expression *right = *right_p;
1688 struct range_list *rl;
1689 sval_t tmp;
1691 if (left->type != EXPR_BINOP || left->op != '+')
1692 return 0;
1693 if (can_integer_overflow(get_type(left), left))
1694 return 0;
1695 if (!get_implied_value(right, &tmp))
1696 return 0;
1698 if (!get_implied_value(left->left, &tmp) &&
1699 get_implied_rl(left->left, &rl) &&
1700 !is_whole_rl(rl)) {
1701 *right_p = binop_expression(right, '-', left->left);
1702 *left_p = left->right;
1703 return 1;
1705 if (!get_implied_value(left->right, &tmp) &&
1706 get_implied_rl(left->right, &rl) &&
1707 !is_whole_rl(rl)) {
1708 *right_p = binop_expression(right, '-', left->right);
1709 *left_p = left->left;
1710 return 1;
1713 return 0;
1716 static int match_func_comparison(struct expression *expr)
1718 struct expression *left = strip_expr(expr->left);
1719 struct expression *right = strip_expr(expr->right);
1721 if (left->type == EXPR_CALL || right->type == EXPR_CALL) {
1722 function_comparison(left, expr->op, right);
1723 return 1;
1726 return 0;
1729 /* Handle conditions like "if (foo + bar < foo) {" */
1730 static int handle_integer_overflow_test(struct expression *expr)
1732 struct expression *left, *right;
1733 struct symbol *type;
1734 sval_t left_min, right_min, min, max;
1736 if (expr->op != '<' && expr->op != SPECIAL_UNSIGNED_LT)
1737 return 0;
1739 left = strip_parens(expr->left);
1740 right = strip_parens(expr->right);
1742 if (left->op != '+')
1743 return 0;
1745 type = get_type(expr);
1746 if (!type)
1747 return 0;
1748 if (type_positive_bits(type) == 32) {
1749 max.type = &uint_ctype;
1750 max.uvalue = (unsigned int)-1;
1751 } else if (type_positive_bits(type) == 64) {
1752 max.type = &ulong_ctype;
1753 max.value = (unsigned long long)-1;
1754 } else {
1755 return 0;
1758 if (!expr_equiv(left->left, right) && !expr_equiv(left->right, right))
1759 return 0;
1761 get_absolute_min(left->left, &left_min);
1762 get_absolute_min(left->right, &right_min);
1763 min = sval_binop(left_min, '+', right_min);
1765 set_extra_chunk_true_false(left, NULL, alloc_estate_range(min, max));
1766 return 1;
1769 static void match_comparison(struct expression *expr)
1771 struct expression *left_orig = strip_parens(expr->left);
1772 struct expression *right_orig = strip_parens(expr->right);
1773 struct expression *left, *right, *tmp;
1774 struct expression *prev;
1775 struct symbol *type;
1776 int redo, count;
1778 if (match_func_comparison(expr))
1779 return;
1781 type = get_type(expr);
1782 if (!type)
1783 type = &llong_ctype;
1785 if (handle_integer_overflow_test(expr))
1786 return;
1788 left = left_orig;
1789 right = right_orig;
1790 move_known_values(&left, &right);
1791 handle_comparison(type, left, expr->op, right);
1793 left = left_orig;
1794 right = right_orig;
1795 if (do_simple_algebra(&left, &right))
1796 handle_comparison(type, left, expr->op, right);
1798 prev = get_assigned_expr(left_orig);
1799 if (is_simple_math(prev) && has_variable(prev, left_orig) == 0) {
1800 left = prev;
1801 right = right_orig;
1802 move_known_values(&left, &right);
1803 handle_comparison(type, left, expr->op, right);
1806 prev = get_assigned_expr(right_orig);
1807 if (is_simple_math(prev) && has_variable(prev, right_orig) == 0) {
1808 left = left_orig;
1809 right = prev;
1810 move_known_values(&left, &right);
1811 handle_comparison(type, left, expr->op, right);
1814 redo = 0;
1815 left = left_orig;
1816 right = right_orig;
1817 if (get_last_expr_from_expression_stmt(left_orig)) {
1818 left = get_last_expr_from_expression_stmt(left_orig);
1819 redo = 1;
1821 if (get_last_expr_from_expression_stmt(right_orig)) {
1822 right = get_last_expr_from_expression_stmt(right_orig);
1823 redo = 1;
1826 if (!redo)
1827 return;
1829 count = 0;
1830 while ((tmp = get_assigned_expr(left))) {
1831 if (count++ > 3)
1832 break;
1833 left = strip_expr(tmp);
1835 count = 0;
1836 while ((tmp = get_assigned_expr(right))) {
1837 if (count++ > 3)
1838 break;
1839 right = strip_expr(tmp);
1842 handle_comparison(type, left, expr->op, right);
1845 static sval_t get_high_mask(sval_t known)
1847 sval_t ret;
1848 int i;
1850 ret = known;
1851 ret.value = 0;
1853 for (i = type_bits(known.type) - 1; i >= 0; i--) {
1854 if (known.uvalue & (1ULL << i))
1855 ret.uvalue |= (1ULL << i);
1856 else
1857 return ret;
1860 return ret;
1863 static void handle_AND_op(struct expression *var, sval_t known)
1865 struct range_list *orig_rl;
1866 struct range_list *true_rl = NULL;
1867 struct range_list *false_rl = NULL;
1868 int bit;
1869 sval_t low_mask = known;
1870 sval_t high_mask;
1871 sval_t max;
1873 get_absolute_rl(var, &orig_rl);
1875 if (known.value > 0) {
1876 bit = ffsll(known.value) - 1;
1877 low_mask.uvalue = (1ULL << bit) - 1;
1878 true_rl = remove_range(orig_rl, sval_type_val(known.type, 0), low_mask);
1880 high_mask = get_high_mask(known);
1881 if (high_mask.value) {
1882 bit = ffsll(high_mask.value) - 1;
1883 low_mask.uvalue = (1ULL << bit) - 1;
1885 false_rl = orig_rl;
1886 if (sval_is_negative(rl_min(orig_rl)))
1887 false_rl = remove_range(false_rl, sval_type_min(known.type), sval_type_val(known.type, -1));
1888 false_rl = remove_range(false_rl, low_mask, sval_type_max(known.type));
1889 if (type_signed(high_mask.type) && type_unsigned(rl_type(false_rl))) {
1890 false_rl = remove_range(false_rl,
1891 sval_type_val(rl_type(false_rl), sval_type_max(known.type).uvalue),
1892 sval_type_val(rl_type(false_rl), -1));
1894 } else if (known.value == 1 &&
1895 get_hard_max(var, &max) &&
1896 sval_cmp(max, rl_max(orig_rl)) == 0 &&
1897 max.value & 1) {
1898 false_rl = remove_range(orig_rl, max, max);
1900 set_extra_expr_true_false(var,
1901 true_rl ? alloc_estate_rl(true_rl) : NULL,
1902 false_rl ? alloc_estate_rl(false_rl) : NULL);
1905 static void handle_AND_condition(struct expression *expr)
1907 sval_t known;
1909 if (get_implied_value(expr->left, &known))
1910 handle_AND_op(expr->right, known);
1911 else if (get_implied_value(expr->right, &known))
1912 handle_AND_op(expr->left, known);
1915 static void handle_MOD_condition(struct expression *expr)
1917 struct range_list *orig_rl;
1918 struct range_list *true_rl;
1919 struct range_list *false_rl = NULL;
1920 sval_t right;
1921 sval_t zero = {
1922 .value = 0,
1925 if (!get_implied_value(expr->right, &right) || right.value == 0)
1926 return;
1927 get_absolute_rl(expr->left, &orig_rl);
1929 zero.type = rl_type(orig_rl);
1931 /* We're basically dorking around the min and max here */
1932 true_rl = remove_range(orig_rl, zero, zero);
1933 if (!sval_is_max(rl_max(true_rl)) &&
1934 !(rl_max(true_rl).value % right.value))
1935 true_rl = remove_range(true_rl, rl_max(true_rl), rl_max(true_rl));
1937 if (rl_equiv(true_rl, orig_rl))
1938 true_rl = NULL;
1940 if (sval_is_positive(rl_min(orig_rl)) &&
1941 (rl_max(orig_rl).value - rl_min(orig_rl).value) / right.value < 5) {
1942 sval_t add;
1943 int i;
1945 add = rl_min(orig_rl);
1946 add.value += right.value - (add.value % right.value);
1947 add.value -= right.value;
1949 for (i = 0; i < 5; i++) {
1950 add.value += right.value;
1951 if (add.value > rl_max(orig_rl).value)
1952 break;
1953 add_range(&false_rl, add, add);
1955 } else {
1956 if (rl_min(orig_rl).uvalue != 0 &&
1957 rl_min(orig_rl).uvalue < right.uvalue) {
1958 sval_t chop = right;
1959 chop.value--;
1960 false_rl = remove_range(orig_rl, zero, chop);
1963 if (!sval_is_max(rl_max(orig_rl)) &&
1964 (rl_max(orig_rl).value % right.value)) {
1965 sval_t chop = rl_max(orig_rl);
1966 chop.value -= chop.value % right.value;
1967 chop.value++;
1968 if (!false_rl)
1969 false_rl = clone_rl(orig_rl);
1970 false_rl = remove_range(false_rl, chop, rl_max(orig_rl));
1974 set_extra_expr_true_false(expr->left,
1975 true_rl ? alloc_estate_rl(true_rl) : NULL,
1976 false_rl ? alloc_estate_rl(false_rl) : NULL);
1979 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
1980 void __extra_match_condition(struct expression *expr)
1982 expr = strip_expr(expr);
1983 switch (expr->type) {
1984 case EXPR_CALL:
1985 function_comparison(expr, SPECIAL_NOTEQUAL, zero_expr());
1986 return;
1987 case EXPR_PREOP:
1988 case EXPR_SYMBOL:
1989 case EXPR_DEREF:
1990 handle_comparison(get_type(expr), expr, SPECIAL_NOTEQUAL, zero_expr());
1991 return;
1992 case EXPR_COMPARE:
1993 match_comparison(expr);
1994 return;
1995 case EXPR_ASSIGNMENT:
1996 __extra_match_condition(expr->left);
1997 return;
1998 case EXPR_BINOP:
1999 if (expr->op == '&')
2000 handle_AND_condition(expr);
2001 if (expr->op == '%')
2002 handle_MOD_condition(expr);
2003 return;
2007 static void assume_indexes_are_valid(struct expression *expr)
2009 struct expression *array_expr;
2010 int array_size;
2011 struct expression *offset;
2012 struct symbol *offset_type;
2013 struct range_list *rl_before;
2014 struct range_list *rl_after;
2015 struct range_list *filter = NULL;
2016 sval_t size;
2018 expr = strip_expr(expr);
2019 if (!is_array(expr))
2020 return;
2022 offset = get_array_offset(expr);
2023 offset_type = get_type(offset);
2024 if (offset_type && type_signed(offset_type)) {
2025 filter = alloc_rl(sval_type_min(offset_type),
2026 sval_type_val(offset_type, -1));
2029 array_expr = get_array_base(expr);
2030 array_size = get_real_array_size(array_expr);
2031 if (array_size > 1) {
2032 size = sval_type_val(offset_type, array_size);
2033 add_range(&filter, size, sval_type_max(offset_type));
2036 if (!filter)
2037 return;
2038 get_absolute_rl(offset, &rl_before);
2039 rl_after = rl_filter(rl_before, filter);
2040 if (rl_equiv(rl_before, rl_after))
2041 return;
2042 set_extra_expr_nomod(offset, alloc_estate_rl(rl_after));
2045 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
2046 int implied_not_equal(struct expression *expr, long long val)
2048 return !possibly_false(expr, SPECIAL_NOTEQUAL, value_expr(val));
2051 int implied_not_equal_name_sym(char *name, struct symbol *sym, long long val)
2053 struct smatch_state *estate;
2055 estate = get_state(SMATCH_EXTRA, name, sym);
2056 if (!estate)
2057 return 0;
2058 if (!rl_has_sval(estate_rl(estate), sval_type_val(estate_type(estate), 0)))
2059 return 1;
2060 return 0;
2063 int parent_is_null_var_sym(const char *name, struct symbol *sym)
2065 char buf[256];
2066 char *start;
2067 char *end;
2068 struct smatch_state *state;
2070 strncpy(buf, name, sizeof(buf) - 1);
2071 buf[sizeof(buf) - 1] = '\0';
2073 start = &buf[0];
2074 while (*start == '*') {
2075 start++;
2076 state = get_state(SMATCH_EXTRA, start, sym);
2077 if (!state)
2078 continue;
2079 if (!estate_rl(state))
2080 return 1;
2081 if (estate_min(state).value == 0 &&
2082 estate_max(state).value == 0)
2083 return 1;
2086 start = &buf[0];
2087 while (*start == '&')
2088 start++;
2090 while ((end = strrchr(start, '-'))) {
2091 *end = '\0';
2092 state = __get_state(SMATCH_EXTRA, start, sym);
2093 if (!state)
2094 continue;
2095 if (estate_min(state).value == 0 &&
2096 estate_max(state).value == 0)
2097 return 1;
2099 return 0;
2102 int parent_is_null(struct expression *expr)
2104 struct symbol *sym;
2105 char *var;
2106 int ret = 0;
2108 expr = strip_expr(expr);
2109 var = expr_to_var_sym(expr, &sym);
2110 if (!var || !sym)
2111 goto free;
2112 ret = parent_is_null_var_sym(var, sym);
2113 free:
2114 free_string(var);
2115 return ret;
2118 static int param_used_callback(void *found, int argc, char **argv, char **azColName)
2120 *(int *)found = 1;
2121 return 0;
2124 static int is_kzalloc_info(struct sm_state *sm)
2126 sval_t sval;
2129 * kzalloc() information is treated as special because so there is just
2130 * a lot of stuff initialized to zero and it makes building the database
2131 * take hours and hours.
2133 * In theory, we should just remove this line and not pass any unused
2134 * information, but I'm not sure enough that this code works so I want
2135 * to hold off on that for now.
2137 if (!estate_get_single_value(sm->state, &sval))
2138 return 0;
2139 if (sval.value != 0)
2140 return 0;
2141 return 1;
2144 static int is_really_long(struct sm_state *sm)
2146 const char *p;
2147 int cnt = 0;
2149 p = sm->name;
2150 while ((p = strstr(p, "->"))) {
2151 p += 2;
2152 cnt++;
2155 if (cnt < 3 ||
2156 strlen(sm->name) < 40)
2157 return 0;
2158 return 1;
2161 static int filter_unused_param_value_info(struct expression *call, int param, char *printed_name, struct sm_state *sm)
2163 int found = 0;
2165 /* for function pointers assume everything is used */
2166 if (call->fn->type != EXPR_SYMBOL)
2167 return 0;
2170 * This is to handle __builtin_mul_overflow(). In an ideal world we
2171 * would only need this for invalid code.
2174 if (!call->fn->symbol)
2175 return 0;
2177 if (!is_kzalloc_info(sm) && !is_really_long(sm))
2178 return 0;
2180 run_sql(&param_used_callback, &found,
2181 "select * from return_implies where %s and type = %d and parameter = %d and key = '%s';",
2182 get_static_filter(call->fn->symbol), PARAM_USED, param, printed_name);
2183 if (found)
2184 return 0;
2186 /* If the database is not built yet, then assume everything is used */
2187 run_sql(&param_used_callback, &found,
2188 "select * from return_implies where %s and type = %d;",
2189 get_static_filter(call->fn->symbol), PARAM_USED);
2190 if (!found)
2191 return 0;
2193 return 1;
2196 struct range_list *intersect_with_real_abs_var_sym(const char *name, struct symbol *sym, struct range_list *start)
2198 struct smatch_state *state;
2201 * Here is the difference between implied value and real absolute, say
2202 * you have:
2204 * int a = (u8)x;
2206 * Then you know that a is 0-255. That's real absolute. But you don't
2207 * know for sure that it actually goes up to 255. So it's not implied.
2208 * Implied indicates a degree of certainty.
2210 * But then say you cap "a" at 8. That means you know it goes up to
2211 * 8. So now the implied value is s32min-8. But you can combine it
2212 * with the real absolute to say that actually it's 0-8.
2214 * We are combining it here. But now that I think about it, this is
2215 * probably not the ideal place to combine it because it should proably
2216 * be done earlier. Oh well, this is an improvement on what was there
2217 * before so I'm going to commit this code.
2221 state = get_real_absolute_state_var_sym(name, sym);
2222 if (!state || !estate_rl(state))
2223 return start;
2225 return rl_intersection(estate_rl(state), start);
2228 struct range_list *intersect_with_real_abs_expr(struct expression *expr, struct range_list *start)
2230 struct smatch_state *state;
2231 struct range_list *abs_rl;
2233 state = get_real_absolute_state(expr);
2234 if (!state || !estate_rl(state))
2235 return start;
2237 abs_rl = cast_rl(rl_type(start), estate_rl(state));
2238 return rl_intersection(abs_rl, start);
2241 static void struct_member_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
2243 struct range_list *rl;
2245 if (estate_is_whole(sm->state))
2246 return;
2247 if (filter_unused_param_value_info(call, param, printed_name, sm))
2248 return;
2249 rl = estate_rl(sm->state);
2250 rl = intersect_with_real_abs_var_sym(sm->name, sm->sym, rl);
2251 sql_insert_caller_info(call, PARAM_VALUE, param, printed_name, show_rl(rl));
2252 if (estate_has_fuzzy_max(sm->state))
2253 sql_insert_caller_info(call, FUZZY_MAX, param, printed_name,
2254 sval_to_str(estate_get_fuzzy_max(sm->state)));
2257 static void returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
2259 struct symbol *returned_sym;
2260 char *returned_name;
2261 struct sm_state *sm;
2262 char *compare_str;
2263 char name_buf[256];
2264 char val_buf[256];
2265 int len;
2267 // FIXME handle *$
2269 if (!is_pointer(expr))
2270 return;
2272 returned_name = expr_to_var_sym(expr, &returned_sym);
2273 if (!returned_name || !returned_sym)
2274 goto free;
2275 len = strlen(returned_name);
2277 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
2278 if (!estate_rl(sm->state))
2279 continue;
2280 if (returned_sym != sm->sym)
2281 continue;
2282 if (strncmp(returned_name, sm->name, len) != 0)
2283 continue;
2284 if (sm->name[len] != '-')
2285 continue;
2287 snprintf(name_buf, sizeof(name_buf), "$%s", sm->name + len);
2289 compare_str = name_sym_to_param_comparison(sm->name, sm->sym);
2290 if (!compare_str && estate_is_whole(sm->state))
2291 continue;
2292 snprintf(val_buf, sizeof(val_buf), "%s%s", sm->state->name, compare_str ?: "");
2294 sql_insert_return_states(return_id, return_ranges, PARAM_VALUE,
2295 -1, name_buf, val_buf);
2296 } END_FOR_EACH_SM(sm);
2298 free:
2299 free_string(returned_name);
2302 static void db_limited_before(void)
2304 unmatched_stree = clone_stree(__get_cur_stree());
2307 static void db_limited_after(void)
2309 free_stree(&unmatched_stree);
2312 static int rl_fits_in_type(struct range_list *rl, struct symbol *type)
2314 if (type_bits(rl_type(rl)) <= type_bits(type))
2315 return 1;
2316 if (sval_cmp(rl_max(rl), sval_type_max(type)) > 0)
2317 return 0;
2318 if (sval_is_negative(rl_min(rl)) &&
2319 sval_cmp(rl_min(rl), sval_type_min(type)) < 0)
2320 return 0;
2321 return 1;
2324 static int basically_the_same(struct range_list *orig, struct range_list *new)
2326 if (rl_equiv(orig, new))
2327 return 1;
2330 * The whole range is essentially the same as 0,4096-27777777777 so
2331 * don't overwrite the implications just to store that.
2334 if (rl_type(orig)->type == SYM_PTR &&
2335 is_whole_rl(orig) &&
2336 rl_min(new).value == 0 &&
2337 rl_max(new).value == valid_ptr_max)
2338 return 1;
2339 return 0;
2342 static void db_param_limit_binops(struct expression *arg, char *key, struct range_list *rl)
2344 struct range_list *left_rl;
2345 sval_t zero = { .type = rl_type(rl), };
2346 sval_t sval;
2348 if (arg->op != '*')
2349 return;
2350 if (!get_implied_value(arg->right, &sval))
2351 return;
2352 if (can_integer_overflow(get_type(arg), arg))
2353 return;
2355 left_rl = rl_binop(rl, '/', alloc_rl(sval, sval));
2356 if (!rl_has_sval(rl, zero))
2357 left_rl = remove_range(left_rl, zero, zero);
2359 set_extra_expr_nomod(arg->left, alloc_estate_rl(left_rl));
2362 static void db_param_limit_filter(struct expression *expr, int param, char *key, char *value, enum info_type op)
2364 struct expression *arg;
2365 char *name;
2366 struct symbol *sym;
2367 struct var_sym_list *vsl = NULL;
2368 struct sm_state *sm;
2369 struct symbol *compare_type, *var_type;
2370 struct range_list *rl;
2371 struct range_list *limit;
2372 struct range_list *new;
2373 char *tmp_name;
2374 struct symbol *tmp_sym;
2376 while (expr->type == EXPR_ASSIGNMENT)
2377 expr = strip_expr(expr->right);
2378 if (expr->type != EXPR_CALL)
2379 return;
2381 arg = get_argument_from_call_expr(expr->args, param);
2382 if (!arg)
2383 return;
2385 name = get_chunk_from_key(arg, key, &sym, &vsl);
2386 if (!name)
2387 return;
2388 if (op != PARAM_LIMIT && !sym)
2389 goto free;
2391 if (strcmp(key, "$") == 0)
2392 compare_type = get_arg_type(expr->fn, param);
2393 else
2394 compare_type = get_member_type_from_key(arg, key);
2396 sm = get_sm_state(SMATCH_EXTRA, name, sym);
2397 if (sm)
2398 rl = estate_rl(sm->state);
2399 else
2400 rl = alloc_whole_rl(compare_type);
2402 if (op == PARAM_LIMIT && !rl_fits_in_type(rl, compare_type))
2403 goto free;
2405 call_results_to_rl(expr, compare_type, value, &limit);
2406 new = rl_intersection(rl, limit);
2408 var_type = get_member_type_from_key(arg, key);
2409 new = cast_rl(var_type, new);
2411 /* We want to preserve the implications here */
2412 if (sm && basically_the_same(estate_rl(sm->state), new))
2413 goto free;
2414 tmp_name = map_long_to_short_name_sym(name, sym, &tmp_sym);
2415 if (tmp_name && tmp_sym) {
2416 free_string(name);
2417 name = tmp_name;
2418 sym = tmp_sym;
2421 if (op == PARAM_LIMIT)
2422 set_extra_nomod_vsl(name, sym, vsl, NULL, alloc_estate_rl(new));
2423 else
2424 set_extra_mod(name, sym, NULL, alloc_estate_rl(new));
2426 if (op == PARAM_LIMIT && arg->type == EXPR_BINOP)
2427 db_param_limit_binops(arg, key, new);
2428 free:
2429 free_string(name);
2432 static void db_param_limit(struct expression *expr, int param, char *key, char *value)
2434 db_param_limit_filter(expr, param, key, value, PARAM_LIMIT);
2437 static void db_param_filter(struct expression *expr, int param, char *key, char *value)
2439 db_param_limit_filter(expr, param, key, value, PARAM_FILTER);
2442 static void db_param_add_set(struct expression *expr, int param, char *key, char *value, enum info_type op)
2444 struct expression *arg;
2445 char *name, *tmp_name;
2446 struct symbol *sym, *tmp_sym;
2447 struct symbol *param_type, *arg_type;
2448 struct smatch_state *state;
2449 struct range_list *new = NULL;
2450 struct range_list *added = NULL;
2452 while (expr->type == EXPR_ASSIGNMENT)
2453 expr = strip_expr(expr->right);
2454 if (expr->type != EXPR_CALL)
2455 return;
2457 arg = get_argument_from_call_expr(expr->args, param);
2458 if (!arg)
2459 return;
2461 arg_type = get_arg_type_from_key(expr->fn, param, arg, key);
2462 param_type = get_member_type_from_key(arg, key);
2463 name = get_variable_from_key(arg, key, &sym);
2464 if (!name || !sym)
2465 goto free;
2467 state = get_state(SMATCH_EXTRA, name, sym);
2468 if (state)
2469 new = estate_rl(state);
2471 call_results_to_rl(expr, arg_type, value, &added);
2472 added = cast_rl(param_type, added);
2473 if (op == PARAM_SET)
2474 new = added;
2475 else
2476 new = rl_union(new, added);
2478 tmp_name = map_long_to_short_name_sym_nostack(name, sym, &tmp_sym);
2479 if (tmp_name && tmp_sym) {
2480 free_string(name);
2481 name = tmp_name;
2482 sym = tmp_sym;
2484 set_extra_mod(name, sym, NULL, alloc_estate_rl(new));
2485 free:
2486 free_string(name);
2489 static void db_param_add(struct expression *expr, int param, char *key, char *value)
2491 in_param_set = true;
2492 db_param_add_set(expr, param, key, value, PARAM_ADD);
2493 in_param_set = false;
2496 static void db_param_set(struct expression *expr, int param, char *key, char *value)
2498 in_param_set = true;
2499 db_param_add_set(expr, param, key, value, PARAM_SET);
2500 in_param_set = false;
2503 static void match_lost_param(struct expression *call, int param)
2505 struct expression *arg;
2507 if (is_const_param(call->fn, param))
2508 return;
2510 arg = get_argument_from_call_expr(call->args, param);
2511 if (!arg)
2512 return;
2514 arg = strip_expr(arg);
2515 if (arg->type == EXPR_PREOP && arg->op == '&')
2516 set_extra_expr_mod(arg->unop, alloc_estate_whole(get_type(arg->unop)));
2517 else
2518 ; /* if pointer then set struct members, maybe?*/
2521 static void db_param_value(struct expression *expr, int param, char *key, char *value)
2523 struct expression *call;
2524 char *name;
2525 struct symbol *sym;
2526 struct symbol *type;
2527 struct range_list *rl = NULL;
2529 if (param != -1)
2530 return;
2532 call = expr;
2533 while (call->type == EXPR_ASSIGNMENT)
2534 call = strip_expr(call->right);
2535 if (call->type != EXPR_CALL)
2536 return;
2538 type = get_member_type_from_key(expr->left, key);
2539 name = get_variable_from_key(expr->left, key, &sym);
2540 if (!name || !sym)
2541 goto free;
2543 call_results_to_rl(call, type, value, &rl);
2545 set_extra_mod(name, sym, NULL, alloc_estate_rl(rl));
2546 free:
2547 free_string(name);
2550 static void match_call_info(struct expression *expr)
2552 struct smatch_state *state;
2553 struct range_list *rl = NULL;
2554 struct expression *arg;
2555 struct symbol *type;
2556 int i = 0;
2558 FOR_EACH_PTR(expr->args, arg) {
2559 type = get_arg_type(expr->fn, i);
2561 get_absolute_rl(arg, &rl);
2562 rl = cast_rl(type, rl);
2564 if (!is_whole_rl(rl)) {
2565 rl = intersect_with_real_abs_expr(arg, rl);
2566 sql_insert_caller_info(expr, PARAM_VALUE, i, "$", show_rl(rl));
2568 state = get_state_expr(SMATCH_EXTRA, arg);
2569 if (estate_has_fuzzy_max(state)) {
2570 sql_insert_caller_info(expr, FUZZY_MAX, i, "$",
2571 sval_to_str(estate_get_fuzzy_max(state)));
2573 i++;
2574 } END_FOR_EACH_PTR(arg);
2577 static void set_param_value(const char *name, struct symbol *sym, char *key, char *value)
2579 struct range_list *rl = NULL;
2580 struct smatch_state *state;
2581 struct symbol *type;
2582 char fullname[256];
2583 sval_t dummy;
2585 if (strcmp(key, "*$") == 0)
2586 snprintf(fullname, sizeof(fullname), "*%s", name);
2587 else if (strncmp(key, "$", 1) == 0)
2588 snprintf(fullname, 256, "%s%s", name, key + 1);
2589 else
2590 return;
2592 type = get_member_type_from_key(symbol_expression(sym), key);
2593 str_to_rl(type, value, &rl);
2594 state = alloc_estate_rl(rl);
2595 if (estate_get_single_value(state, &dummy))
2596 estate_set_hard_max(state);
2597 set_state(SMATCH_EXTRA, fullname, sym, state);
2600 static void set_param_hard_max(const char *name, struct symbol *sym, char *key, char *value)
2602 struct range_list *rl = NULL;
2603 struct smatch_state *state;
2604 struct symbol *type;
2605 char fullname[256];
2606 sval_t max;
2608 if (strcmp(key, "*$") == 0)
2609 snprintf(fullname, sizeof(fullname), "*%s", name);
2610 else if (strncmp(key, "$", 1) == 0)
2611 snprintf(fullname, 256, "%s%s", name, key + 1);
2612 else
2613 return;
2615 state = get_state(SMATCH_EXTRA, fullname, sym);
2616 if (!state)
2617 return;
2618 type = estate_type(state);
2619 str_to_rl(type, value, &rl);
2620 if (!rl_to_sval(rl, &max))
2621 return;
2622 estate_set_fuzzy_max(state, max);
2625 struct sm_state *get_extra_sm_state(struct expression *expr)
2627 char *name;
2628 struct symbol *sym;
2629 struct sm_state *ret = NULL;
2631 name = expr_to_known_chunk_sym(expr, &sym);
2632 if (!name)
2633 goto free;
2635 ret = get_sm_state(SMATCH_EXTRA, name, sym);
2636 free:
2637 free_string(name);
2638 return ret;
2641 struct smatch_state *get_extra_state(struct expression *expr)
2643 struct sm_state *sm;
2645 sm = get_extra_sm_state(expr);
2646 if (!sm)
2647 return NULL;
2648 return sm->state;
2651 void register_smatch_extra(int id)
2653 my_id = id;
2655 add_merge_hook(my_id, &merge_estates);
2656 add_unmatched_state_hook(my_id, &unmatched_state);
2657 select_caller_info_hook(set_param_value, PARAM_VALUE);
2658 select_caller_info_hook(set_param_hard_max, FUZZY_MAX);
2659 select_return_states_before(&db_limited_before);
2660 select_return_states_hook(PARAM_LIMIT, &db_param_limit);
2661 select_return_states_hook(PARAM_FILTER, &db_param_filter);
2662 select_return_states_hook(PARAM_ADD, &db_param_add);
2663 select_return_states_hook(PARAM_SET, &db_param_set);
2664 add_lost_param_hook(&match_lost_param);
2665 select_return_states_hook(PARAM_VALUE, &db_param_value);
2666 select_return_states_after(&db_limited_after);
2669 static void match_link_modify(struct sm_state *sm, struct expression *mod_expr)
2671 struct var_sym_list *links;
2672 struct var_sym *tmp;
2673 struct smatch_state *state;
2675 links = sm->state->data;
2677 FOR_EACH_PTR(links, tmp) {
2678 if (sm->sym == tmp->sym &&
2679 strcmp(sm->name, tmp->var) == 0)
2680 continue;
2681 state = get_state(SMATCH_EXTRA, tmp->var, tmp->sym);
2682 if (!state)
2683 continue;
2684 set_state(SMATCH_EXTRA, tmp->var, tmp->sym, alloc_estate_whole(estate_type(state)));
2685 } END_FOR_EACH_PTR(tmp);
2686 set_state(link_id, sm->name, sm->sym, &undefined);
2689 void register_smatch_extra_links(int id)
2691 link_id = id;
2694 void register_smatch_extra_late(int id)
2696 add_merge_hook(link_id, &merge_link_states);
2697 add_modification_hook(link_id, &match_link_modify);
2698 add_hook(&match_dereferences, DEREF_HOOK);
2699 add_hook(&match_pointer_as_array, OP_HOOK);
2700 select_return_implies_hook(DEREFERENCE, &set_param_dereferenced);
2701 add_hook(&match_function_call, FUNCTION_CALL_HOOK);
2702 add_hook(&match_assign, ASSIGNMENT_HOOK);
2703 add_hook(&match_assign, GLOBAL_ASSIGNMENT_HOOK);
2704 add_hook(&unop_expr, OP_HOOK);
2705 add_hook(&asm_expr, ASM_HOOK);
2707 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
2708 add_member_info_callback(my_id, struct_member_callback);
2709 add_split_return_callback(&returned_struct_members);
2711 // add_hook(&assume_indexes_are_valid, OP_HOOK);