flow: don't fake an impossible default
[smatch.git] / smatch_extra.c
blob94fdad657fcf42f1b90af13ad2479b6ee26b5701
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 static 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 static 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(const char *name, struct symbol *sym, struct symbol **new_sym)
139 struct expression *assigned;
140 char *orig_name = NULL;
141 char buf[256];
142 char *ret = NULL;
143 int skip;
145 *new_sym = NULL;
147 if (!sym || !sym->ident)
148 return NULL;
150 ret = get_pointed_at(name, sym, new_sym);
151 if (ret)
152 return ret;
154 skip = strlen(sym->ident->name);
155 if (name[skip] != '-' || name[skip + 1] != '>')
156 return NULL;
157 skip += 2;
159 assigned = get_assigned_expr_name_sym(sym->ident->name, sym);
160 if (!assigned)
161 return NULL;
162 if (assigned->type == EXPR_CALL)
163 return map_call_to_other_name_sym(name, sym, new_sym);
164 if (assigned->type == EXPR_PREOP || assigned->op == '&') {
166 orig_name = expr_to_var_sym(assigned, new_sym);
167 if (!orig_name || !*new_sym)
168 goto free;
170 snprintf(buf, sizeof(buf), "%s.%s", orig_name + 1, name + skip);
171 ret = alloc_string(buf);
172 free_string(orig_name);
173 return ret;
176 if (assigned->type != EXPR_DEREF)
177 goto free;
179 orig_name = expr_to_var_sym(assigned, new_sym);
180 if (!orig_name || !*new_sym)
181 goto free;
183 snprintf(buf, sizeof(buf), "%s->%s", orig_name, name + skip);
184 ret = alloc_string(buf);
185 free_string(orig_name);
186 return ret;
188 free:
189 free_string(orig_name);
190 return NULL;
193 void set_extra_mod(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
195 char *new_name;
196 struct symbol *new_sym;
198 set_extra_mod_helper(name, sym, expr, state);
199 new_name = get_other_name_sym(name, sym, &new_sym);
200 if (new_name && new_sym)
201 set_extra_mod_helper(new_name, new_sym, expr, state);
202 free_string(new_name);
205 static struct expression *chunk_get_array_base(struct expression *expr)
208 * The problem with is_array() is that it only returns true for things
209 * like foo[1] but not for foo[1].bar.
212 expr = strip_expr(expr);
213 while (expr && expr->type == EXPR_DEREF)
214 expr = strip_expr(expr->deref);
215 return get_array_base(expr);
218 static int chunk_has_array(struct expression *expr)
220 return !!chunk_get_array_base(expr);
223 static void clear_array_states(struct expression *array)
225 struct sm_state *sm;
227 sm = get_sm_state_expr(link_id, array);
228 if (sm)
229 match_link_modify(sm, NULL);
232 static void set_extra_array_mod(struct expression *expr, struct smatch_state *state)
234 struct expression *array;
235 struct var_sym_list *vsl;
236 struct var_sym *vs;
237 char *name;
238 struct symbol *sym;
240 array = chunk_get_array_base(expr);
242 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
243 if (!name || !vsl) {
244 clear_array_states(array);
245 goto free;
248 FOR_EACH_PTR(vsl, vs) {
249 store_link(link_id, vs->var, vs->sym, name, sym);
250 } END_FOR_EACH_PTR(vs);
252 call_extra_mod_hooks(name, sym, expr, state);
253 set_state(SMATCH_EXTRA, name, sym, state);
254 free:
255 free_string(name);
258 void set_extra_expr_mod(struct expression *expr, struct smatch_state *state)
260 struct symbol *sym;
261 char *name;
263 if (chunk_has_array(expr)) {
264 set_extra_array_mod(expr, state);
265 return;
268 expr = strip_expr(expr);
269 name = expr_to_var_sym(expr, &sym);
270 if (!name || !sym)
271 goto free;
272 set_extra_mod(name, sym, expr, state);
273 free:
274 free_string(name);
277 void set_extra_nomod(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
279 char *new_name;
280 struct symbol *new_sym;
281 struct relation *rel;
282 struct smatch_state *orig_state;
284 orig_state = get_state(SMATCH_EXTRA, name, sym);
286 /* don't save unknown states if leaving it blank is the same */
287 if (!orig_state && estate_is_unknown(state))
288 return;
290 new_name = get_other_name_sym(name, sym, &new_sym);
291 if (new_name && new_sym)
292 set_extra_nomod_helper(new_name, new_sym, expr, state);
293 free_string(new_name);
295 if (!estate_related(orig_state)) {
296 set_extra_nomod_helper(name, sym, expr, state);
297 return;
300 set_related(state, estate_related(orig_state));
301 FOR_EACH_PTR(estate_related(orig_state), rel) {
302 struct smatch_state *estate;
304 if (option_debug_related)
305 sm_msg("%s updating related %s to %s", name, rel->name, state->name);
306 estate = get_state(SMATCH_EXTRA, rel->name, rel->sym);
307 if (!estate)
308 continue;
309 set_extra_nomod_helper(rel->name, rel->sym, expr, clone_estate_cast(estate_type(estate), state));
310 } END_FOR_EACH_PTR(rel);
313 void set_extra_nomod_vsl(const char *name, struct symbol *sym, struct var_sym_list *vsl, struct expression *expr, struct smatch_state *state)
315 struct var_sym *vs;
317 FOR_EACH_PTR(vsl, vs) {
318 store_link(link_id, vs->var, vs->sym, name, sym);
319 } END_FOR_EACH_PTR(vs);
321 set_extra_nomod(name, sym, expr, state);
325 * This is for return_implies_state() hooks which modify a SMATCH_EXTRA state
327 void set_extra_expr_nomod(struct expression *expr, struct smatch_state *state)
329 struct var_sym_list *vsl;
330 struct var_sym *vs;
331 char *name;
332 struct symbol *sym;
334 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
335 if (!name || !vsl)
336 goto free;
337 FOR_EACH_PTR(vsl, vs) {
338 store_link(link_id, vs->var, vs->sym, name, sym);
339 } END_FOR_EACH_PTR(vs);
341 set_extra_nomod(name, sym, expr, state);
342 free:
343 free_string(name);
346 static void set_extra_true_false(const char *name, struct symbol *sym,
347 struct smatch_state *true_state,
348 struct smatch_state *false_state)
350 char *new_name;
351 struct symbol *new_sym;
352 struct relation *rel;
353 struct smatch_state *orig_state;
355 if (!true_state && !false_state)
356 return;
358 if (in_warn_on_macro())
359 return;
361 new_name = get_other_name_sym(name, sym, &new_sym);
362 if (new_name && new_sym)
363 set_true_false_states(SMATCH_EXTRA, new_name, new_sym, true_state, false_state);
364 free_string(new_name);
366 orig_state = get_state(SMATCH_EXTRA, name, sym);
368 if (!estate_related(orig_state)) {
369 set_true_false_states(SMATCH_EXTRA, name, sym, true_state, false_state);
370 return;
373 if (true_state)
374 set_related(true_state, estate_related(orig_state));
375 if (false_state)
376 set_related(false_state, estate_related(orig_state));
378 FOR_EACH_PTR(estate_related(orig_state), rel) {
379 set_true_false_states(SMATCH_EXTRA, rel->name, rel->sym,
380 true_state, false_state);
381 } END_FOR_EACH_PTR(rel);
384 static void set_extra_chunk_true_false(struct expression *expr,
385 struct smatch_state *true_state,
386 struct smatch_state *false_state)
388 struct var_sym_list *vsl;
389 struct var_sym *vs;
390 struct symbol *type;
391 char *name;
392 struct symbol *sym;
394 if (in_warn_on_macro())
395 return;
397 type = get_type(expr);
398 if (!type)
399 return;
401 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
402 if (!name || !vsl)
403 goto free;
404 FOR_EACH_PTR(vsl, vs) {
405 store_link(link_id, vs->var, vs->sym, name, sym);
406 } END_FOR_EACH_PTR(vs);
408 set_true_false_states(SMATCH_EXTRA, name, sym,
409 clone_estate(true_state),
410 clone_estate(false_state));
411 free:
412 free_string(name);
415 static void set_extra_expr_true_false(struct expression *expr,
416 struct smatch_state *true_state,
417 struct smatch_state *false_state)
419 char *name;
420 struct symbol *sym;
421 sval_t sval;
423 if (!true_state && !false_state)
424 return;
426 if (get_value(expr, &sval))
427 return;
429 expr = strip_expr(expr);
430 name = expr_to_var_sym(expr, &sym);
431 if (!name || !sym) {
432 free_string(name);
433 set_extra_chunk_true_false(expr, true_state, false_state);
434 return;
436 set_extra_true_false(name, sym, true_state, false_state);
437 free_string(name);
440 static int get_countdown_info(struct expression *condition, struct expression **unop, int *op, sval_t *right)
442 struct expression *unop_expr;
443 int comparison;
444 sval_t limit;
446 right->type = &int_ctype;
447 right->value = 0;
449 condition = strip_expr(condition);
451 if (condition->type == EXPR_COMPARE) {
452 comparison = remove_unsigned_from_comparison(condition->op);
454 if (comparison != SPECIAL_GTE && comparison != '>')
455 return 0;
456 if (!get_value(condition->right, &limit))
457 return 0;
459 unop_expr = condition->left;
460 if (unop_expr->type != EXPR_PREOP && unop_expr->type != EXPR_POSTOP)
461 return 0;
462 if (unop_expr->op != SPECIAL_DECREMENT)
463 return 0;
465 *unop = unop_expr;
466 *op = comparison;
467 *right = limit;
469 return 1;
472 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
473 return 0;
474 if (condition->op != SPECIAL_DECREMENT)
475 return 0;
477 *unop = condition;
478 *op = '>';
480 return 1;
483 static struct sm_state *handle_canonical_while_count_down(struct statement *loop)
485 struct expression *iter_var;
486 struct expression *condition, *unop;
487 struct sm_state *sm;
488 struct smatch_state *estate;
489 int op;
490 sval_t start, right;
492 right.type = &int_ctype;
493 right.value = 0;
495 condition = strip_expr(loop->iterator_pre_condition);
496 if (!condition)
497 return NULL;
499 if (!get_countdown_info(condition, &unop, &op, &right))
500 return NULL;
502 iter_var = unop->unop;
504 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
505 if (!sm)
506 return NULL;
507 if (sval_cmp(estate_min(sm->state), right) < 0)
508 return NULL;
509 start = estate_max(sm->state);
510 if (sval_cmp(start, right) <= 0)
511 return NULL;
512 if (!sval_is_max(start))
513 start.value--;
515 if (op == SPECIAL_GTE)
516 right.value--;
518 if (unop->type == EXPR_PREOP) {
519 right.value++;
520 estate = alloc_estate_range(right, start);
521 if (estate_has_hard_max(sm->state))
522 estate_set_hard_max(estate);
523 estate_copy_fuzzy_max(estate, sm->state);
524 set_extra_expr_mod(iter_var, estate);
526 if (unop->type == EXPR_POSTOP) {
527 estate = alloc_estate_range(right, start);
528 if (estate_has_hard_max(sm->state))
529 estate_set_hard_max(estate);
530 estate_copy_fuzzy_max(estate, sm->state);
531 set_extra_expr_mod(iter_var, estate);
533 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
536 static struct sm_state *handle_canonical_for_inc(struct expression *iter_expr,
537 struct expression *condition)
539 struct expression *iter_var;
540 struct sm_state *sm;
541 struct smatch_state *estate;
542 sval_t start, end, max;
544 iter_var = iter_expr->unop;
545 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
546 if (!sm)
547 return NULL;
548 if (!estate_get_single_value(sm->state, &start))
549 return NULL;
550 if (get_implied_max(condition->right, &end))
551 end = sval_cast(get_type(iter_var), end);
552 else
553 end = sval_type_max(get_type(iter_var));
555 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
556 return NULL;
558 switch (condition->op) {
559 case SPECIAL_UNSIGNED_LT:
560 case SPECIAL_NOTEQUAL:
561 case '<':
562 if (!sval_is_min(end))
563 end.value--;
564 break;
565 case SPECIAL_UNSIGNED_LTE:
566 case SPECIAL_LTE:
567 break;
568 default:
569 return NULL;
571 if (sval_cmp(end, start) < 0)
572 return NULL;
573 estate = alloc_estate_range(start, end);
574 if (get_hard_max(condition->right, &max)) {
575 estate_set_hard_max(estate);
576 if (condition->op == '<' ||
577 condition->op == SPECIAL_UNSIGNED_LT ||
578 condition->op == SPECIAL_NOTEQUAL)
579 max.value--;
580 estate_set_fuzzy_max(estate, max);
582 set_extra_expr_mod(iter_var, estate);
583 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
586 static struct sm_state *handle_canonical_for_dec(struct expression *iter_expr,
587 struct expression *condition)
589 struct expression *iter_var;
590 struct sm_state *sm;
591 struct smatch_state *estate;
592 sval_t start, end;
594 iter_var = iter_expr->unop;
595 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
596 if (!sm)
597 return NULL;
598 if (!estate_get_single_value(sm->state, &start))
599 return NULL;
600 if (!get_implied_min(condition->right, &end))
601 end = sval_type_min(get_type(iter_var));
602 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
603 return NULL;
605 switch (condition->op) {
606 case SPECIAL_NOTEQUAL:
607 case '>':
608 if (!sval_is_min(end) && !sval_is_max(end))
609 end.value++;
610 break;
611 case SPECIAL_GTE:
612 break;
613 default:
614 return NULL;
616 if (sval_cmp(end, start) > 0)
617 return NULL;
618 estate = alloc_estate_range(end, start);
619 estate_set_hard_max(estate);
620 estate_set_fuzzy_max(estate, estate_get_fuzzy_max(estate));
621 set_extra_expr_mod(iter_var, estate);
622 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
625 static struct sm_state *handle_canonical_for_loops(struct statement *loop)
627 struct expression *iter_expr;
628 struct expression *condition;
630 if (!loop->iterator_post_statement)
631 return NULL;
632 if (loop->iterator_post_statement->type != STMT_EXPRESSION)
633 return NULL;
634 iter_expr = loop->iterator_post_statement->expression;
635 if (!loop->iterator_pre_condition)
636 return NULL;
637 if (loop->iterator_pre_condition->type != EXPR_COMPARE)
638 return NULL;
639 condition = loop->iterator_pre_condition;
641 if (iter_expr->op == SPECIAL_INCREMENT)
642 return handle_canonical_for_inc(iter_expr, condition);
643 if (iter_expr->op == SPECIAL_DECREMENT)
644 return handle_canonical_for_dec(iter_expr, condition);
645 return NULL;
648 struct sm_state *__extra_handle_canonical_loops(struct statement *loop, struct stree **stree)
650 struct sm_state *ret;
653 * Canonical loops are a hack. The proper way to handle this is to
654 * use two passes, but unfortunately, doing two passes makes parsing
655 * code twice as slow.
657 * What we do is we set the inside state here, which overwrites whatever
658 * __extra_match_condition() does. Then we set the outside state in
659 * __extra_pre_loop_hook_after().
662 __push_fake_cur_stree();
663 if (!loop->iterator_post_statement)
664 ret = handle_canonical_while_count_down(loop);
665 else
666 ret = handle_canonical_for_loops(loop);
667 *stree = __pop_fake_cur_stree();
668 return ret;
671 int __iterator_unchanged(struct sm_state *sm)
673 if (!sm)
674 return 0;
675 if (get_sm_state(my_id, sm->name, sm->sym) == sm)
676 return 1;
677 return 0;
680 static void while_count_down_after(struct sm_state *sm, struct expression *condition)
682 struct expression *unop;
683 int op;
684 sval_t limit, after_value;
686 if (!get_countdown_info(condition, &unop, &op, &limit))
687 return;
688 after_value = estate_min(sm->state);
689 after_value.value--;
690 set_extra_mod(sm->name, sm->sym, condition->unop, alloc_estate_sval(after_value));
693 void __extra_pre_loop_hook_after(struct sm_state *sm,
694 struct statement *iterator,
695 struct expression *condition)
697 struct expression *iter_expr;
698 sval_t limit;
699 struct smatch_state *state;
701 if (!iterator) {
702 while_count_down_after(sm, condition);
703 return;
706 iter_expr = iterator->expression;
708 if (condition->type != EXPR_COMPARE)
709 return;
710 if (iter_expr->op == SPECIAL_INCREMENT) {
711 limit = sval_binop(estate_max(sm->state), '+',
712 sval_type_val(estate_type(sm->state), 1));
713 } else {
714 limit = sval_binop(estate_min(sm->state), '-',
715 sval_type_val(estate_type(sm->state), 1));
717 if (!estate_has_hard_max(sm->state) && !__has_breaks()) {
718 if (iter_expr->op == SPECIAL_INCREMENT)
719 state = alloc_estate_range(estate_min(sm->state), limit);
720 else
721 state = alloc_estate_range(limit, estate_max(sm->state));
722 } else {
723 state = alloc_estate_sval(limit);
725 if (!estate_has_hard_max(sm->state)) {
726 estate_clear_hard_max(state);
728 if (estate_has_fuzzy_max(sm->state)) {
729 sval_t hmax = estate_get_fuzzy_max(sm->state);
730 sval_t max = estate_max(sm->state);
732 if (sval_cmp(hmax, max) != 0)
733 estate_clear_fuzzy_max(state);
734 } else if (!estate_has_fuzzy_max(sm->state)) {
735 estate_clear_fuzzy_max(state);
738 set_extra_mod(sm->name, sm->sym, iter_expr, state);
741 static struct stree *unmatched_stree;
742 static struct smatch_state *unmatched_state(struct sm_state *sm)
744 struct smatch_state *state;
746 if (unmatched_stree) {
747 state = get_state_stree(unmatched_stree, SMATCH_EXTRA, sm->name, sm->sym);
748 if (state)
749 return state;
751 if (parent_is_gone_var_sym(sm->name, sm->sym))
752 return alloc_estate_empty();
753 return alloc_estate_whole(estate_type(sm->state));
756 static void clear_the_pointed_at(struct expression *expr)
758 struct stree *stree;
759 char *name;
760 struct symbol *sym;
761 struct sm_state *tmp;
763 name = expr_to_var_sym(expr, &sym);
764 if (!name || !sym)
765 goto free;
767 stree = __get_cur_stree();
768 FOR_EACH_MY_SM(SMATCH_EXTRA, stree, tmp) {
769 if (tmp->name[0] != '*')
770 continue;
771 if (tmp->sym != sym)
772 continue;
773 if (strcmp(tmp->name + 1, name) != 0)
774 continue;
775 set_extra_mod(tmp->name, tmp->sym, expr, alloc_estate_whole(estate_type(tmp->state)));
776 } END_FOR_EACH_SM(tmp);
778 free:
779 free_string(name);
782 static int is_const_param(struct expression *expr, int param)
784 struct symbol *type;
786 type = get_arg_type(expr, param);
787 if (!type)
788 return 0;
789 if (type->ctype.modifiers & MOD_CONST)
790 return 1;
791 return 0;
794 static void match_function_call(struct expression *expr)
796 struct expression *arg;
797 struct expression *tmp;
798 int param = -1;
800 /* if we have the db this is handled in smatch_function_hooks.c */
801 if (!option_no_db)
802 return;
803 if (inlinable(expr->fn))
804 return;
806 FOR_EACH_PTR(expr->args, arg) {
807 param++;
808 if (is_const_param(expr->fn, param))
809 continue;
810 tmp = strip_expr(arg);
811 if (tmp->type == EXPR_PREOP && tmp->op == '&')
812 set_extra_expr_mod(tmp->unop, alloc_estate_whole(get_type(tmp->unop)));
813 else
814 clear_the_pointed_at(tmp);
815 } END_FOR_EACH_PTR(arg);
818 static int values_fit_type(struct expression *left, struct expression *right)
820 struct range_list *rl;
821 struct symbol *type;
823 type = get_type(left);
824 if (!type)
825 return 0;
826 get_absolute_rl(right, &rl);
827 if (type_unsigned(type) && sval_is_negative(rl_min(rl)))
828 return 0;
829 if (sval_cmp(sval_type_min(type), rl_min(rl)) > 0)
830 return 0;
831 if (sval_cmp(sval_type_max(type), rl_max(rl)) < 0)
832 return 0;
833 return 1;
836 static void save_chunk_info(struct expression *left, struct expression *right)
838 struct var_sym_list *vsl;
839 struct var_sym *vs;
840 struct expression *add_expr;
841 struct symbol *type;
842 sval_t sval;
843 char *name;
844 struct symbol *sym;
846 if (right->type != EXPR_BINOP || right->op != '-')
847 return;
848 if (!get_value(right->left, &sval))
849 return;
850 if (!expr_to_sym(right->right))
851 return;
853 add_expr = binop_expression(left, '+', right->right);
854 type = get_type(add_expr);
855 if (!type)
856 return;
857 name = expr_to_chunk_sym_vsl(add_expr, &sym, &vsl);
858 if (!name || !vsl)
859 goto free;
860 FOR_EACH_PTR(vsl, vs) {
861 store_link(link_id, vs->var, vs->sym, name, sym);
862 } END_FOR_EACH_PTR(vs);
864 set_state(SMATCH_EXTRA, name, sym, alloc_estate_sval(sval_cast(type, sval)));
865 free:
866 free_string(name);
869 static void do_array_assign(struct expression *left, int op, struct expression *right)
871 struct range_list *rl;
873 if (op == '=') {
874 get_absolute_rl(right, &rl);
875 rl = cast_rl(get_type(left), rl);
876 } else {
877 rl = alloc_whole_rl(get_type(left));
880 set_extra_array_mod(left, alloc_estate_rl(rl));
883 static void match_vanilla_assign(struct expression *left, struct expression *right)
885 struct range_list *orig_rl = NULL;
886 struct range_list *rl = NULL;
887 struct symbol *right_sym;
888 struct symbol *left_type;
889 struct symbol *right_type;
890 char *right_name = NULL;
891 struct symbol *sym;
892 char *name;
893 sval_t max;
894 struct smatch_state *state;
895 int comparison;
897 if (is_struct(left))
898 return;
900 save_chunk_info(left, right);
902 name = expr_to_var_sym(left, &sym);
903 if (!name) {
904 if (chunk_has_array(left))
905 do_array_assign(left, '=', right);
906 return;
909 left_type = get_type(left);
910 right_type = get_type(right);
912 right_name = expr_to_var_sym(right, &right_sym);
914 if (!__in_fake_assign &&
915 !(right->type == EXPR_PREOP && right->op == '&') &&
916 right_name && right_sym &&
917 values_fit_type(left, right) &&
918 !has_symbol(right, sym)) {
919 set_equiv(left, right);
920 goto free;
923 if (is_pointer(right) && get_address_rl(right, &rl)) {
924 state = alloc_estate_rl(rl);
925 goto done;
928 if (__in_fake_assign) {
929 struct smatch_state *right_state;
930 sval_t sval;
932 if (get_value(right, &sval)) {
933 sval = sval_cast(left_type, sval);
934 state = alloc_estate_sval(sval);
935 goto done;
938 right_state = get_state(SMATCH_EXTRA, right_name, right_sym);
939 if (right_state) {
940 /* simple assignment */
941 state = clone_estate(right_state);
942 goto done;
945 state = alloc_estate_rl(alloc_whole_rl(left_type));
946 goto done;
949 comparison = get_comparison(left, right);
950 if (comparison) {
951 comparison = flip_comparison(comparison);
952 get_implied_rl(left, &orig_rl);
955 if (get_implied_rl(right, &rl)) {
956 rl = cast_rl(left_type, rl);
957 if (orig_rl)
958 filter_by_comparison(&rl, comparison, orig_rl);
959 state = alloc_estate_rl(rl);
960 if (get_hard_max(right, &max)) {
961 estate_set_hard_max(state);
962 estate_set_fuzzy_max(state, max);
964 } else {
965 rl = alloc_whole_rl(right_type);
966 rl = cast_rl(left_type, rl);
967 if (orig_rl)
968 filter_by_comparison(&rl, comparison, orig_rl);
969 state = alloc_estate_rl(rl);
972 done:
973 set_extra_mod(name, sym, left, state);
974 free:
975 free_string(right_name);
978 static int op_remove_assign(int op)
980 switch (op) {
981 case SPECIAL_ADD_ASSIGN:
982 return '+';
983 case SPECIAL_SUB_ASSIGN:
984 return '-';
985 case SPECIAL_MUL_ASSIGN:
986 return '*';
987 case SPECIAL_DIV_ASSIGN:
988 return '/';
989 case SPECIAL_MOD_ASSIGN:
990 return '%';
991 case SPECIAL_AND_ASSIGN:
992 return '&';
993 case SPECIAL_OR_ASSIGN:
994 return '|';
995 case SPECIAL_XOR_ASSIGN:
996 return '^';
997 case SPECIAL_SHL_ASSIGN:
998 return SPECIAL_LEFTSHIFT;
999 case SPECIAL_SHR_ASSIGN:
1000 return SPECIAL_RIGHTSHIFT;
1001 default:
1002 return op;
1006 static void match_assign(struct expression *expr)
1008 struct range_list *rl = NULL;
1009 struct expression *left;
1010 struct expression *right;
1011 struct expression *binop_expr;
1012 struct symbol *left_type;
1013 struct symbol *sym;
1014 char *name;
1015 sval_t left_min, left_max;
1016 sval_t right_min, right_max;
1017 sval_t res_min, res_max;
1019 left = strip_expr(expr->left);
1021 right = strip_parens(expr->right);
1022 if (right->type == EXPR_CALL && sym_name_is("__builtin_expect", right->fn))
1023 right = get_argument_from_call_expr(right->args, 0);
1024 while (right->type == EXPR_ASSIGNMENT && right->op == '=')
1025 right = strip_parens(right->left);
1027 if (expr->op == '=' && is_condition(expr->right))
1028 return; /* handled in smatch_condition.c */
1029 if (expr->op == '=' && right->type == EXPR_CALL)
1030 return; /* handled in smatch_function_hooks.c */
1031 if (expr->op == '=') {
1032 match_vanilla_assign(left, right);
1033 return;
1036 name = expr_to_var_sym(left, &sym);
1037 if (!name)
1038 return;
1040 left_type = get_type(left);
1042 res_min = sval_type_min(left_type);
1043 res_max = sval_type_max(left_type);
1045 switch (expr->op) {
1046 case SPECIAL_ADD_ASSIGN:
1047 get_absolute_max(left, &left_max);
1048 get_absolute_max(right, &right_max);
1049 if (sval_binop_overflows(left_max, '+', sval_cast(left_type, right_max)))
1050 break;
1051 if (get_implied_min(left, &left_min) &&
1052 !sval_is_negative_min(left_min) &&
1053 get_implied_min(right, &right_min) &&
1054 !sval_is_negative_min(right_min)) {
1055 res_min = sval_binop(left_min, '+', right_min);
1056 res_min = sval_cast(left_type, res_min);
1058 if (inside_loop()) /* we are assuming loops don't lead to wrapping */
1059 break;
1060 res_max = sval_binop(left_max, '+', right_max);
1061 res_max = sval_cast(left_type, res_max);
1062 break;
1063 case SPECIAL_SUB_ASSIGN:
1064 if (get_implied_max(left, &left_max) &&
1065 !sval_is_max(left_max) &&
1066 get_implied_min(right, &right_min) &&
1067 !sval_is_min(right_min)) {
1068 res_max = sval_binop(left_max, '-', right_min);
1069 res_max = sval_cast(left_type, res_max);
1071 if (inside_loop())
1072 break;
1073 if (get_implied_min(left, &left_min) &&
1074 !sval_is_min(left_min) &&
1075 get_implied_max(right, &right_max) &&
1076 !sval_is_max(right_max)) {
1077 res_min = sval_binop(left_min, '-', right_max);
1078 res_min = sval_cast(left_type, res_min);
1080 break;
1081 case SPECIAL_AND_ASSIGN:
1082 case SPECIAL_MOD_ASSIGN:
1083 case SPECIAL_SHL_ASSIGN:
1084 case SPECIAL_SHR_ASSIGN:
1085 case SPECIAL_OR_ASSIGN:
1086 case SPECIAL_XOR_ASSIGN:
1087 case SPECIAL_MUL_ASSIGN:
1088 case SPECIAL_DIV_ASSIGN:
1089 binop_expr = binop_expression(expr->left,
1090 op_remove_assign(expr->op),
1091 expr->right);
1092 if (get_absolute_rl(binop_expr, &rl)) {
1093 rl = cast_rl(left_type, rl);
1094 set_extra_mod(name, sym, left, alloc_estate_rl(rl));
1095 goto free;
1097 break;
1099 rl = cast_rl(left_type, alloc_rl(res_min, res_max));
1100 set_extra_mod(name, sym, left, alloc_estate_rl(rl));
1101 free:
1102 free_string(name);
1105 static struct smatch_state *increment_state(struct smatch_state *state)
1107 sval_t min = estate_min(state);
1108 sval_t max = estate_max(state);
1110 if (!estate_rl(state))
1111 return NULL;
1113 if (inside_loop())
1114 max = sval_type_max(max.type);
1116 if (!sval_is_min(min) && !sval_is_max(min))
1117 min.value++;
1118 if (!sval_is_min(max) && !sval_is_max(max))
1119 max.value++;
1120 return alloc_estate_range(min, max);
1123 static struct smatch_state *decrement_state(struct smatch_state *state)
1125 sval_t min = estate_min(state);
1126 sval_t max = estate_max(state);
1128 if (!estate_rl(state))
1129 return NULL;
1131 if (inside_loop())
1132 min = sval_type_min(min.type);
1134 if (!sval_is_min(min) && !sval_is_max(min))
1135 min.value--;
1136 if (!sval_is_min(max) && !sval_is_max(max))
1137 max.value--;
1138 return alloc_estate_range(min, max);
1141 static void clear_pointed_at_state(struct expression *expr)
1143 struct symbol *type;
1146 * ALERT: This is sort of a mess. If it's is a struct assigment like
1147 * "foo = bar;", then that's handled by smatch_struct_assignment.c.
1148 * the same thing for p++ where "p" is a struct. Most modifications
1149 * are handled by the assignment hook or the db. Smatch_extra.c doesn't
1150 * use smatch_modification.c because we have to get the ordering right
1151 * or something. So if you have p++ where p is a pointer to a standard
1152 * c type then we handle that here. What a mess.
1155 type = get_type(expr);
1156 if (!type || type->type != SYM_PTR)
1157 return;
1158 type = get_real_base_type(type);
1159 if (!type || type->type != SYM_BASETYPE)
1160 return;
1161 set_extra_expr_mod(deref_expression(expr), alloc_estate_whole(type));
1164 static void unop_expr(struct expression *expr)
1166 struct smatch_state *state;
1168 if (expr->smatch_flags & Handled)
1169 return;
1171 switch (expr->op) {
1172 case SPECIAL_INCREMENT:
1173 state = get_state_expr(SMATCH_EXTRA, expr->unop);
1174 state = increment_state(state);
1175 if (!state)
1176 state = alloc_estate_whole(get_type(expr));
1177 set_extra_expr_mod(expr->unop, state);
1178 clear_pointed_at_state(expr->unop);
1179 break;
1180 case SPECIAL_DECREMENT:
1181 state = get_state_expr(SMATCH_EXTRA, expr->unop);
1182 state = decrement_state(state);
1183 if (!state)
1184 state = alloc_estate_whole(get_type(expr));
1185 set_extra_expr_mod(expr->unop, state);
1186 clear_pointed_at_state(expr->unop);
1187 break;
1188 default:
1189 return;
1193 static void asm_expr(struct statement *stmt)
1196 struct expression *expr;
1197 struct symbol *type;
1198 int state = 0;
1200 FOR_EACH_PTR(stmt->asm_outputs, expr) {
1201 switch (state) {
1202 case 0: /* identifier */
1203 case 1: /* constraint */
1204 state++;
1205 continue;
1206 case 2: /* expression */
1207 state = 0;
1208 type = get_type(strip_expr(expr));
1209 set_extra_expr_mod(expr, alloc_estate_whole(type));
1210 continue;
1212 } END_FOR_EACH_PTR(expr);
1215 static void check_dereference(struct expression *expr)
1217 struct smatch_state *state;
1219 if (__in_fake_assign)
1220 return;
1221 if (outside_of_function())
1222 return;
1223 state = get_extra_state(expr);
1224 if (state) {
1225 static struct range_list *valid_rl;
1226 struct range_list *rl;
1228 if (!valid_rl) {
1229 valid_rl = alloc_rl(valid_ptr_min_sval, valid_ptr_max_sval);
1230 valid_rl = clone_rl_permanent(valid_rl);
1233 rl = rl_intersection(estate_rl(state), valid_rl);
1234 if (rl_equiv(rl, estate_rl(state)))
1235 return;
1236 set_extra_expr_nomod(expr, alloc_estate_rl(rl));
1237 } else {
1238 set_extra_expr_nomod(expr, alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval));
1242 static void match_dereferences(struct expression *expr)
1244 if (expr->type != EXPR_PREOP)
1245 return;
1246 /* it's saying that foo[1] = bar dereferences foo[1] */
1247 if (is_array(expr))
1248 return;
1249 check_dereference(expr->unop);
1252 static void match_pointer_as_array(struct expression *expr)
1254 if (!is_array(expr))
1255 return;
1256 check_dereference(get_array_base(expr));
1259 static void find_dereferences(struct expression *expr)
1261 while (expr->type == EXPR_PREOP) {
1262 if (expr->op == '*')
1263 check_dereference(expr->unop);
1264 expr = strip_expr(expr->unop);
1268 static void set_param_dereferenced(struct expression *call, struct expression *arg, char *key, char *unused)
1270 struct symbol *sym;
1271 char *name;
1273 name = get_variable_from_key(arg, key, &sym);
1274 if (name && sym) {
1275 struct smatch_state *orig, *new;
1276 struct range_list *rl;
1278 orig = get_state(SMATCH_EXTRA, name, sym);
1279 if (orig) {
1280 rl = rl_intersection(estate_rl(orig),
1281 alloc_rl(valid_ptr_min_sval,
1282 valid_ptr_max_sval));
1283 new = alloc_estate_rl(rl);
1284 } else {
1285 new = alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval);
1288 set_extra_nomod(name, sym, NULL, new);
1290 free_string(name);
1292 find_dereferences(arg);
1295 static sval_t add_one(sval_t sval)
1297 sval.value++;
1298 return sval;
1301 static int handle_postop_inc(struct expression *left, int op, struct expression *right)
1303 struct statement *stmt;
1304 struct expression *cond;
1305 struct smatch_state *true_state, *false_state;
1306 sval_t start;
1307 sval_t limit;
1310 * If we're decrementing here then that's a canonical while count down
1311 * so it's handled already. We're only handling loops like:
1312 * i = 0;
1313 * do { ... } while (i++ < 3);
1316 if (left->type != EXPR_POSTOP || left->op != SPECIAL_INCREMENT)
1317 return 0;
1319 stmt = __cur_stmt->parent;
1320 if (!stmt)
1321 return 0;
1322 if (stmt->type == STMT_COMPOUND)
1323 stmt = stmt->parent;
1324 if (!stmt || stmt->type != STMT_ITERATOR || !stmt->iterator_post_condition)
1325 return 0;
1327 cond = strip_expr(stmt->iterator_post_condition);
1328 if (cond->type != EXPR_COMPARE || cond->op != op)
1329 return 0;
1330 if (left != strip_expr(cond->left) || right != strip_expr(cond->right))
1331 return 0;
1333 if (!get_implied_value(left->unop, &start))
1334 return 0;
1335 if (!get_implied_value(right, &limit))
1336 return 0;
1338 if (sval_cmp(start, limit) > 0)
1339 return 0;
1341 switch (op) {
1342 case '<':
1343 case SPECIAL_UNSIGNED_LT:
1344 break;
1345 case SPECIAL_LTE:
1346 case SPECIAL_UNSIGNED_LTE:
1347 limit = add_one(limit);
1348 default:
1349 return 0;
1353 true_state = alloc_estate_range(add_one(start), limit);
1354 false_state = alloc_estate_range(add_one(limit), add_one(limit));
1356 /* Currently we just discard the false state but when two passes is
1357 * implimented correctly then it will use it.
1360 set_extra_expr_true_false(left->unop, true_state, false_state);
1362 return 1;
1365 static void handle_comparison(struct symbol *type, struct expression *left, int op, struct expression *right)
1367 struct range_list *left_orig;
1368 struct range_list *left_true;
1369 struct range_list *left_false;
1370 struct range_list *right_orig;
1371 struct range_list *right_true;
1372 struct range_list *right_false;
1373 struct smatch_state *left_true_state;
1374 struct smatch_state *left_false_state;
1375 struct smatch_state *right_true_state;
1376 struct smatch_state *right_false_state;
1377 sval_t dummy, hard_max;
1378 int left_postop = 0;
1379 int right_postop = 0;
1381 if (left->op == SPECIAL_INCREMENT || left->op == SPECIAL_DECREMENT) {
1382 if (left->type == EXPR_POSTOP) {
1383 left->smatch_flags |= Handled;
1384 left_postop = left->op;
1385 if (handle_postop_inc(left, op, right))
1386 return;
1388 left = strip_parens(left->unop);
1390 while (left->type == EXPR_ASSIGNMENT)
1391 left = strip_parens(left->left);
1393 if (right->op == SPECIAL_INCREMENT || right->op == SPECIAL_DECREMENT) {
1394 if (right->type == EXPR_POSTOP) {
1395 right->smatch_flags |= Handled;
1396 right_postop = right->op;
1398 right = strip_parens(right->unop);
1401 get_real_absolute_rl(left, &left_orig);
1402 left_orig = cast_rl(type, left_orig);
1404 get_real_absolute_rl(right, &right_orig);
1405 right_orig = cast_rl(type, right_orig);
1407 split_comparison_rl(left_orig, op, right_orig, &left_true, &left_false, &right_true, &right_false);
1409 left_true = rl_truncate_cast(get_type(strip_expr(left)), left_true);
1410 left_false = rl_truncate_cast(get_type(strip_expr(left)), left_false);
1411 right_true = rl_truncate_cast(get_type(strip_expr(right)), right_true);
1412 right_false = rl_truncate_cast(get_type(strip_expr(right)), right_false);
1414 if (!left_true || !left_false) {
1415 struct range_list *tmp_true, *tmp_false;
1417 split_comparison_rl(alloc_whole_rl(type), op, right_orig, &tmp_true, &tmp_false, NULL, NULL);
1418 tmp_true = rl_truncate_cast(get_type(strip_expr(left)), tmp_true);
1419 tmp_false = rl_truncate_cast(get_type(strip_expr(left)), tmp_false);
1420 if (tmp_true && tmp_false)
1421 __save_imaginary_state(left, tmp_true, tmp_false);
1424 if (!right_true || !right_false) {
1425 struct range_list *tmp_true, *tmp_false;
1427 split_comparison_rl(alloc_whole_rl(type), op, right_orig, NULL, NULL, &tmp_true, &tmp_false);
1428 tmp_true = rl_truncate_cast(get_type(strip_expr(right)), tmp_true);
1429 tmp_false = rl_truncate_cast(get_type(strip_expr(right)), tmp_false);
1430 if (tmp_true && tmp_false)
1431 __save_imaginary_state(right, tmp_true, tmp_false);
1434 left_true_state = alloc_estate_rl(left_true);
1435 left_false_state = alloc_estate_rl(left_false);
1436 right_true_state = alloc_estate_rl(right_true);
1437 right_false_state = alloc_estate_rl(right_false);
1439 switch (op) {
1440 case '<':
1441 case SPECIAL_UNSIGNED_LT:
1442 case SPECIAL_UNSIGNED_LTE:
1443 case SPECIAL_LTE:
1444 if (get_hard_max(right, &dummy))
1445 estate_set_hard_max(left_true_state);
1446 if (get_hard_max(left, &dummy))
1447 estate_set_hard_max(right_false_state);
1448 break;
1449 case '>':
1450 case SPECIAL_UNSIGNED_GT:
1451 case SPECIAL_UNSIGNED_GTE:
1452 case SPECIAL_GTE:
1453 if (get_hard_max(left, &dummy))
1454 estate_set_hard_max(right_true_state);
1455 if (get_hard_max(right, &dummy))
1456 estate_set_hard_max(left_false_state);
1457 break;
1460 switch (op) {
1461 case '<':
1462 case SPECIAL_UNSIGNED_LT:
1463 case SPECIAL_UNSIGNED_LTE:
1464 case SPECIAL_LTE:
1465 if (get_hard_max(right, &hard_max)) {
1466 if (op == '<' || op == SPECIAL_UNSIGNED_LT)
1467 hard_max.value--;
1468 estate_set_fuzzy_max(left_true_state, hard_max);
1470 if (get_implied_value(right, &hard_max)) {
1471 if (op == SPECIAL_UNSIGNED_LTE ||
1472 op == SPECIAL_LTE)
1473 hard_max.value++;
1474 estate_set_fuzzy_max(left_false_state, hard_max);
1476 if (get_hard_max(left, &hard_max)) {
1477 if (op == SPECIAL_UNSIGNED_LTE ||
1478 op == SPECIAL_LTE)
1479 hard_max.value--;
1480 estate_set_fuzzy_max(right_false_state, hard_max);
1482 if (get_implied_value(left, &hard_max)) {
1483 if (op == '<' || op == SPECIAL_UNSIGNED_LT)
1484 hard_max.value++;
1485 estate_set_fuzzy_max(right_true_state, hard_max);
1487 break;
1488 case '>':
1489 case SPECIAL_UNSIGNED_GT:
1490 case SPECIAL_UNSIGNED_GTE:
1491 case SPECIAL_GTE:
1492 if (get_hard_max(left, &hard_max)) {
1493 if (op == '>' || op == SPECIAL_UNSIGNED_GT)
1494 hard_max.value--;
1495 estate_set_fuzzy_max(right_true_state, hard_max);
1497 if (get_implied_value(left, &hard_max)) {
1498 if (op == SPECIAL_UNSIGNED_GTE ||
1499 op == SPECIAL_GTE)
1500 hard_max.value++;
1501 estate_set_fuzzy_max(right_false_state, hard_max);
1503 if (get_hard_max(right, &hard_max)) {
1504 if (op == SPECIAL_UNSIGNED_LTE ||
1505 op == SPECIAL_LTE)
1506 hard_max.value--;
1507 estate_set_fuzzy_max(left_false_state, hard_max);
1509 if (get_implied_value(right, &hard_max)) {
1510 if (op == '>' ||
1511 op == SPECIAL_UNSIGNED_GT)
1512 hard_max.value++;
1513 estate_set_fuzzy_max(left_true_state, hard_max);
1515 break;
1516 case SPECIAL_EQUAL:
1517 if (get_hard_max(left, &hard_max))
1518 estate_set_fuzzy_max(right_true_state, hard_max);
1519 if (get_hard_max(right, &hard_max))
1520 estate_set_fuzzy_max(left_true_state, hard_max);
1521 break;
1524 if (get_hard_max(left, &hard_max)) {
1525 estate_set_hard_max(left_true_state);
1526 estate_set_hard_max(left_false_state);
1528 if (get_hard_max(right, &hard_max)) {
1529 estate_set_hard_max(right_true_state);
1530 estate_set_hard_max(right_false_state);
1533 if (left_postop == SPECIAL_INCREMENT) {
1534 left_true_state = increment_state(left_true_state);
1535 left_false_state = increment_state(left_false_state);
1537 if (left_postop == SPECIAL_DECREMENT) {
1538 left_true_state = decrement_state(left_true_state);
1539 left_false_state = decrement_state(left_false_state);
1541 if (right_postop == SPECIAL_INCREMENT) {
1542 right_true_state = increment_state(right_true_state);
1543 right_false_state = increment_state(right_false_state);
1545 if (right_postop == SPECIAL_DECREMENT) {
1546 right_true_state = decrement_state(right_true_state);
1547 right_false_state = decrement_state(right_false_state);
1550 if (estate_rl(left_true_state) && estates_equiv(left_true_state, left_false_state)) {
1551 left_true_state = NULL;
1552 left_false_state = NULL;
1555 if (estate_rl(right_true_state) && estates_equiv(right_true_state, right_false_state)) {
1556 right_true_state = NULL;
1557 right_false_state = NULL;
1560 set_extra_expr_true_false(left, left_true_state, left_false_state);
1561 set_extra_expr_true_false(right, right_true_state, right_false_state);
1564 static int is_simple_math(struct expression *expr)
1566 if (!expr)
1567 return 0;
1568 if (expr->type != EXPR_BINOP)
1569 return 0;
1570 switch (expr->op) {
1571 case '+':
1572 case '-':
1573 case '*':
1574 return 1;
1576 return 0;
1579 static void move_known_values(struct expression **left_p, struct expression **right_p)
1581 struct expression *left = *left_p;
1582 struct expression *right = *right_p;
1583 sval_t sval, dummy;
1585 if (get_implied_value(left, &sval)) {
1586 if (!is_simple_math(right))
1587 return;
1588 if (get_implied_value(right, &dummy))
1589 return;
1590 if (right->op == '*') {
1591 sval_t divisor;
1593 if (!get_value(right->right, &divisor))
1594 return;
1595 if (divisor.value == 0 && sval.value % divisor.value)
1596 return;
1597 *left_p = binop_expression(left, invert_op(right->op), right->right);
1598 *right_p = right->left;
1599 return;
1601 if (right->op == '+' && get_value(right->left, &sval)) {
1602 *left_p = binop_expression(left, invert_op(right->op), right->left);
1603 *right_p = right->right;
1604 return;
1606 if (get_value(right->right, &sval)) {
1607 *left_p = binop_expression(left, invert_op(right->op), right->right);
1608 *right_p = right->left;
1609 return;
1611 return;
1613 if (get_implied_value(right, &sval)) {
1614 if (!is_simple_math(left))
1615 return;
1616 if (get_implied_value(left, &dummy))
1617 return;
1618 if (left->op == '*') {
1619 sval_t divisor;
1621 if (!get_value(left->right, &divisor))
1622 return;
1623 if (divisor.value == 0 && sval.value % divisor.value)
1624 return;
1625 *right_p = binop_expression(right, invert_op(left->op), left->right);
1626 *left_p = left->left;
1627 return;
1629 if (left->op == '+' && get_value(left->left, &sval)) {
1630 *right_p = binop_expression(right, invert_op(left->op), left->left);
1631 *left_p = left->right;
1632 return;
1635 if (get_value(left->right, &sval)) {
1636 *right_p = binop_expression(right, invert_op(left->op), left->right);
1637 *left_p = left->left;
1638 return;
1640 return;
1645 * The reason for do_simple_algebra() is to solve things like:
1646 * if (foo > 66 || foo + bar > 64) {
1647 * "foo" is not really a known variable so it won't be handled by
1648 * move_known_variables() but it's a super common idiom.
1651 static int do_simple_algebra(struct expression **left_p, struct expression **right_p)
1653 struct expression *left = *left_p;
1654 struct expression *right = *right_p;
1655 struct range_list *rl;
1656 sval_t tmp;
1658 if (left->type != EXPR_BINOP || left->op != '+')
1659 return 0;
1660 if (can_integer_overflow(get_type(left), left))
1661 return 0;
1662 if (!get_implied_value(right, &tmp))
1663 return 0;
1665 if (!get_implied_value(left->left, &tmp) &&
1666 get_implied_rl(left->left, &rl) &&
1667 !is_whole_rl(rl)) {
1668 *right_p = binop_expression(right, '-', left->left);
1669 *left_p = left->right;
1670 return 1;
1672 if (!get_implied_value(left->right, &tmp) &&
1673 get_implied_rl(left->right, &rl) &&
1674 !is_whole_rl(rl)) {
1675 *right_p = binop_expression(right, '-', left->right);
1676 *left_p = left->left;
1677 return 1;
1680 return 0;
1683 static int match_func_comparison(struct expression *expr)
1685 struct expression *left = strip_expr(expr->left);
1686 struct expression *right = strip_expr(expr->right);
1687 sval_t sval;
1690 * fixme: think about this harder. We should always be trying to limit
1691 * the non-call side as well. If we can't determine the limitter does
1692 * that mean we aren't querying the database and are missing important
1693 * information?
1696 if (left->type == EXPR_CALL) {
1697 if (get_implied_value(left, &sval)) {
1698 handle_comparison(get_type(expr), left, expr->op, right);
1699 return 1;
1701 function_comparison(left, expr->op, right);
1702 return 1;
1705 if (right->type == EXPR_CALL) {
1706 if (get_implied_value(right, &sval)) {
1707 handle_comparison(get_type(expr), left, expr->op, right);
1708 return 1;
1710 function_comparison(left, expr->op, right);
1711 return 1;
1714 return 0;
1717 /* Handle conditions like "if (foo + bar < foo) {" */
1718 static int handle_integer_overflow_test(struct expression *expr)
1720 struct expression *left, *right;
1721 struct symbol *type;
1722 sval_t left_min, right_min, min, max;
1724 if (expr->op != '<' && expr->op != SPECIAL_UNSIGNED_LT)
1725 return 0;
1727 left = strip_parens(expr->left);
1728 right = strip_parens(expr->right);
1730 if (left->op != '+')
1731 return 0;
1733 type = get_type(expr);
1734 if (!type)
1735 return 0;
1736 if (type_positive_bits(type) == 32) {
1737 max.type = &uint_ctype;
1738 max.uvalue = (unsigned int)-1;
1739 } else if (type_positive_bits(type) == 64) {
1740 max.type = &ulong_ctype;
1741 max.value = (unsigned long long)-1;
1742 } else {
1743 return 0;
1746 if (!expr_equiv(left->left, right) && !expr_equiv(left->right, right))
1747 return 0;
1749 get_absolute_min(left->left, &left_min);
1750 get_absolute_min(left->right, &right_min);
1751 min = sval_binop(left_min, '+', right_min);
1753 set_extra_chunk_true_false(left, NULL, alloc_estate_range(min, max));
1754 return 1;
1757 static void match_comparison(struct expression *expr)
1759 struct expression *left_orig = strip_parens(expr->left);
1760 struct expression *right_orig = strip_parens(expr->right);
1761 struct expression *left, *right;
1762 struct expression *prev;
1763 struct symbol *type;
1765 if (match_func_comparison(expr))
1766 return;
1768 type = get_type(expr);
1769 if (!type)
1770 type = &llong_ctype;
1772 if (handle_integer_overflow_test(expr))
1773 return;
1775 left = left_orig;
1776 right = right_orig;
1777 move_known_values(&left, &right);
1778 handle_comparison(type, left, expr->op, right);
1780 left = left_orig;
1781 right = right_orig;
1782 if (do_simple_algebra(&left, &right))
1783 handle_comparison(type, left, expr->op, right);
1785 prev = get_assigned_expr(left_orig);
1786 if (is_simple_math(prev) && has_variable(prev, left_orig) == 0) {
1787 left = prev;
1788 right = right_orig;
1789 move_known_values(&left, &right);
1790 handle_comparison(type, left, expr->op, right);
1793 prev = get_assigned_expr(right_orig);
1794 if (is_simple_math(prev) && has_variable(prev, right_orig) == 0) {
1795 left = left_orig;
1796 right = prev;
1797 move_known_values(&left, &right);
1798 handle_comparison(type, left, expr->op, right);
1802 static sval_t get_high_mask(sval_t known)
1804 sval_t ret;
1805 int i;
1807 ret = known;
1808 ret.value = 0;
1810 for (i = type_bits(known.type) - 1; i >= 0; i--) {
1811 if (known.uvalue & (1ULL << i))
1812 ret.uvalue |= (1ULL << i);
1813 else
1814 return ret;
1817 return ret;
1820 static void handle_AND_condition(struct expression *expr)
1822 struct range_list *orig_rl;
1823 struct range_list *true_rl = NULL;
1824 struct range_list *false_rl = NULL;
1825 sval_t known;
1826 int bit;
1828 if (get_implied_value(expr->left, &known)) {
1829 sval_t low_mask = known;
1830 sval_t high_mask;
1832 if (known.value > 0) {
1833 bit = ffsll(known.value) - 1;
1834 low_mask.uvalue = (1ULL << bit) - 1;
1835 get_absolute_rl(expr->right, &orig_rl);
1836 true_rl = remove_range(orig_rl, sval_type_val(known.type, 0), low_mask);
1838 high_mask = get_high_mask(known);
1839 if (high_mask.value) {
1840 bit = ffsll(high_mask.value) - 1;
1841 low_mask.uvalue = (1ULL << bit) - 1;
1843 get_absolute_rl(expr->left, &orig_rl);
1844 if (sval_is_negative(rl_min(orig_rl)))
1845 orig_rl = remove_range(orig_rl, sval_type_min(known.type), sval_type_val(known.type, -1));
1846 false_rl = remove_range(orig_rl, low_mask, sval_type_max(known.type));
1847 if (type_signed(high_mask.type) && type_unsigned(rl_type(false_rl))) {
1848 false_rl = remove_range(false_rl,
1849 sval_type_val(rl_type(false_rl), sval_type_max(known.type).uvalue),
1850 sval_type_val(rl_type(false_rl), -1));
1853 set_extra_expr_true_false(expr->right,
1854 true_rl ? alloc_estate_rl(true_rl) : NULL,
1855 false_rl ? alloc_estate_rl(false_rl) : NULL);
1857 return;
1860 if (get_implied_value(expr->right, &known)) {
1861 sval_t low_mask = known;
1862 sval_t high_mask;
1864 if (known.value > 0) {
1865 bit = ffsll(known.value) - 1;
1866 low_mask.uvalue = (1ULL << bit) - 1;
1867 get_absolute_rl(expr->left, &orig_rl);
1868 true_rl = remove_range(orig_rl, sval_type_val(known.type, 0), low_mask);
1870 high_mask = get_high_mask(known);
1871 if (high_mask.value) {
1872 bit = ffsll(high_mask.value) - 1;
1873 low_mask.uvalue = (1ULL << bit) - 1;
1875 get_absolute_rl(expr->left, &orig_rl);
1876 if (sval_is_negative(rl_min(orig_rl)))
1877 orig_rl = remove_range(orig_rl, sval_type_min(known.type), sval_type_val(known.type, -1));
1878 false_rl = remove_range(orig_rl, low_mask, sval_type_max(known.type));
1879 if (type_signed(high_mask.type) && type_unsigned(rl_type(false_rl))) {
1880 false_rl = remove_range(false_rl,
1881 sval_type_val(rl_type(false_rl), sval_type_max(known.type).uvalue),
1882 sval_type_val(rl_type(false_rl), -1));
1885 set_extra_expr_true_false(expr->left,
1886 true_rl ? alloc_estate_rl(true_rl) : NULL,
1887 false_rl ? alloc_estate_rl(false_rl) : NULL);
1888 return;
1892 static void handle_MOD_condition(struct expression *expr)
1894 struct range_list *orig_rl;
1895 struct range_list *true_rl;
1896 struct range_list *false_rl = NULL;
1897 sval_t right;
1898 sval_t zero = {
1899 .value = 0,
1902 if (!get_implied_value(expr->right, &right) || right.value == 0)
1903 return;
1904 get_absolute_rl(expr->left, &orig_rl);
1906 zero.type = rl_type(orig_rl);
1908 /* We're basically dorking around the min and max here */
1909 true_rl = remove_range(orig_rl, zero, zero);
1910 if (!sval_is_max(rl_max(true_rl)) &&
1911 !(rl_max(true_rl).value % right.value))
1912 true_rl = remove_range(true_rl, rl_max(true_rl), rl_max(true_rl));
1914 if (rl_equiv(true_rl, orig_rl))
1915 true_rl = NULL;
1917 if (sval_is_positive(rl_min(orig_rl)) &&
1918 (rl_max(orig_rl).value - rl_min(orig_rl).value) / right.value < 5) {
1919 sval_t add;
1920 int i;
1922 add = rl_min(orig_rl);
1923 add.value += right.value - (add.value % right.value);
1924 add.value -= right.value;
1926 for (i = 0; i < 5; i++) {
1927 add.value += right.value;
1928 if (add.value > rl_max(orig_rl).value)
1929 break;
1930 add_range(&false_rl, add, add);
1932 } else {
1933 if (rl_min(orig_rl).uvalue != 0 &&
1934 rl_min(orig_rl).uvalue < right.uvalue) {
1935 sval_t chop = right;
1936 chop.value--;
1937 false_rl = remove_range(orig_rl, zero, chop);
1940 if (!sval_is_max(rl_max(orig_rl)) &&
1941 (rl_max(orig_rl).value % right.value)) {
1942 sval_t chop = rl_max(orig_rl);
1943 chop.value -= chop.value % right.value;
1944 chop.value++;
1945 if (!false_rl)
1946 false_rl = clone_rl(orig_rl);
1947 false_rl = remove_range(false_rl, chop, rl_max(orig_rl));
1951 set_extra_expr_true_false(expr->left,
1952 true_rl ? alloc_estate_rl(true_rl) : NULL,
1953 false_rl ? alloc_estate_rl(false_rl) : NULL);
1956 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
1957 void __extra_match_condition(struct expression *expr)
1959 struct smatch_state *pre_state;
1960 struct smatch_state *true_state;
1961 struct smatch_state *false_state;
1962 struct range_list *pre_rl;
1964 expr = strip_expr(expr);
1965 switch (expr->type) {
1966 case EXPR_CALL:
1967 function_comparison(expr, SPECIAL_NOTEQUAL, zero_expr());
1968 return;
1969 case EXPR_PREOP:
1970 case EXPR_SYMBOL:
1971 case EXPR_DEREF: {
1972 sval_t zero;
1974 zero = sval_blank(expr);
1975 zero.value = 0;
1977 pre_state = get_extra_state(expr);
1978 if (estate_is_empty(pre_state))
1979 return;
1980 if (pre_state)
1981 pre_rl = estate_rl(pre_state);
1982 else
1983 get_absolute_rl(expr, &pre_rl);
1984 if (possibly_true_rl(pre_rl, SPECIAL_EQUAL, rl_zero()))
1985 false_state = alloc_estate_sval(zero);
1986 else
1987 false_state = alloc_estate_empty();
1988 true_state = alloc_estate_rl(remove_range(pre_rl, zero, zero));
1989 set_extra_expr_true_false(expr, true_state, false_state);
1990 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 filter_unused_kzalloc_info(struct expression *call, int param, char *printed_name, struct sm_state *sm)
2126 sval_t sval;
2127 int found = 0;
2129 /* for function pointers assume everything is used */
2130 if (call->fn->type != EXPR_SYMBOL)
2131 return 0;
2134 * kzalloc() information is treated as special because so there is just
2135 * a lot of stuff initialized to zero and it makes building the database
2136 * take hours and hours.
2138 * In theory, we should just remove this line and not pass any unused
2139 * information, but I'm not sure enough that this code works so I want
2140 * to hold off on that for now.
2142 if (!estate_get_single_value(sm->state, &sval) || sval.value != 0)
2143 return 0;
2145 run_sql(&param_used_callback, &found,
2146 "select * from call_implies where %s and type = %d and parameter = %d and key = '%s';",
2147 get_static_filter(call->fn->symbol), PARAM_USED, param, printed_name);
2148 if (found)
2149 return 0;
2151 /* If the database is not built yet, then assume everything is used */
2152 run_sql(&param_used_callback, &found,
2153 "select * from call_implies where %s and type = %d;",
2154 get_static_filter(call->fn->symbol), PARAM_USED);
2155 if (!found)
2156 return 0;
2158 return 1;
2161 struct range_list *intersect_with_real_abs_var_sym(const char *name, struct symbol *sym, struct range_list *start)
2163 struct smatch_state *state;
2166 * Here is the difference between implied value and real absolute, say
2167 * you have:
2169 * int a = (u8)x;
2171 * Then you know that a is 0-255. That's real absolute. But you don't
2172 * know for sure that it actually goes up to 255. So it's not implied.
2173 * Implied indicates a degree of certainty.
2175 * But then say you cap "a" at 8. That means you know it goes up to
2176 * 8. So now the implied value is s32min-8. But you can combine it
2177 * with the real absolute to say that actually it's 0-8.
2179 * We are combining it here. But now that I think about it, this is
2180 * probably not the ideal place to combine it because it should proably
2181 * be done earlier. Oh well, this is an improvement on what was there
2182 * before so I'm going to commit this code.
2186 state = get_real_absolute_state_var_sym(name, sym);
2187 if (!state || !estate_rl(state))
2188 return start;
2190 return rl_intersection(estate_rl(state), start);
2193 struct range_list *intersect_with_real_abs_expr(struct expression *expr, struct range_list *start)
2195 struct smatch_state *state;
2197 state = get_real_absolute_state(expr);
2198 if (!state || !estate_rl(state))
2199 return start;
2201 return rl_intersection(estate_rl(state), start);
2204 static void struct_member_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
2206 struct range_list *rl;
2208 if (estate_is_whole(sm->state))
2209 return;
2210 if (filter_unused_kzalloc_info(call, param, printed_name, sm))
2211 return;
2212 rl = estate_rl(sm->state);
2213 rl = intersect_with_real_abs_var_sym(sm->name, sm->sym, rl);
2214 sql_insert_caller_info(call, PARAM_VALUE, param, printed_name, show_rl(rl));
2215 if (estate_has_fuzzy_max(sm->state))
2216 sql_insert_caller_info(call, FUZZY_MAX, param, printed_name,
2217 sval_to_str(estate_get_fuzzy_max(sm->state)));
2220 static void returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
2222 struct symbol *returned_sym;
2223 struct sm_state *sm;
2224 const char *param_name;
2225 char *compare_str;
2226 char buf[256];
2228 returned_sym = expr_to_sym(expr);
2229 if (!returned_sym)
2230 return;
2232 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
2233 if (!estate_rl(sm->state))
2234 continue;
2235 if (returned_sym != sm->sym)
2236 continue;
2238 param_name = get_param_name(sm);
2239 if (!param_name)
2240 continue;
2241 if (strcmp(param_name, "$") == 0)
2242 continue;
2243 compare_str = name_sym_to_param_comparison(sm->name, sm->sym);
2244 if (!compare_str && estate_is_whole(sm->state))
2245 continue;
2246 snprintf(buf, sizeof(buf), "%s%s", sm->state->name, compare_str ?: "");
2248 sql_insert_return_states(return_id, return_ranges, PARAM_VALUE,
2249 -1, param_name, buf);
2250 } END_FOR_EACH_SM(sm);
2253 static void db_limited_before(void)
2255 unmatched_stree = clone_stree(__get_cur_stree());
2258 static void db_limited_after(void)
2260 free_stree(&unmatched_stree);
2263 static int rl_fits_in_type(struct range_list *rl, struct symbol *type)
2265 if (type_bits(rl_type(rl)) <= type_bits(type))
2266 return 1;
2267 if (sval_cmp(rl_max(rl), sval_type_max(type)) > 0)
2268 return 0;
2269 if (sval_is_negative(rl_min(rl)) &&
2270 sval_cmp(rl_min(rl), sval_type_min(type)) < 0)
2271 return 0;
2272 return 1;
2275 static int basically_the_same(struct range_list *orig, struct range_list *new)
2277 if (rl_equiv(orig, new))
2278 return 1;
2281 * The whole range is essentially the same as 0,4096-27777777777 so
2282 * don't overwrite the implications just to store that.
2285 if (rl_type(orig)->type == SYM_PTR &&
2286 is_whole_rl(orig) &&
2287 rl_min(new).value == 0 &&
2288 rl_max(new).value == valid_ptr_max)
2289 return 1;
2290 return 0;
2293 static void db_param_limit_filter(struct expression *expr, int param, char *key, char *value, enum info_type op)
2295 struct expression *arg;
2296 char *name;
2297 struct symbol *sym;
2298 struct var_sym_list *vsl = NULL;
2299 struct sm_state *sm;
2300 struct symbol *compare_type, *var_type;
2301 struct range_list *rl;
2302 struct range_list *limit;
2303 struct range_list *new;
2305 while (expr->type == EXPR_ASSIGNMENT)
2306 expr = strip_expr(expr->right);
2307 if (expr->type != EXPR_CALL)
2308 return;
2310 arg = get_argument_from_call_expr(expr->args, param);
2311 if (!arg)
2312 return;
2314 name = get_chunk_from_key(arg, key, &sym, &vsl);
2315 if (!name)
2316 return;
2317 if (op != PARAM_LIMIT && !sym)
2318 goto free;
2320 if (strcmp(key, "$") == 0)
2321 compare_type = get_arg_type(expr->fn, param);
2322 else
2323 compare_type = get_member_type_from_key(arg, key);
2325 sm = get_sm_state(SMATCH_EXTRA, name, sym);
2326 if (sm)
2327 rl = estate_rl(sm->state);
2328 else
2329 rl = alloc_whole_rl(compare_type);
2331 if (op == PARAM_LIMIT && !rl_fits_in_type(rl, compare_type))
2332 goto free;
2334 call_results_to_rl(expr, compare_type, value, &limit);
2335 new = rl_intersection(rl, limit);
2337 var_type = get_member_type_from_key(arg, key);
2338 new = cast_rl(var_type, new);
2340 /* We want to preserve the implications here */
2341 if (sm && basically_the_same(estate_rl(sm->state), new))
2342 __set_sm(sm); /* FIXME: Is this really necessary? */
2343 else {
2344 char *tmp_name;
2345 struct symbol *tmp_sym;
2347 tmp_name = map_long_to_short_name_sym(name, sym, &tmp_sym);
2348 if (tmp_name && tmp_sym) {
2349 free_string(name);
2350 name = tmp_name;
2351 sym = tmp_sym;
2354 if (op == PARAM_LIMIT)
2355 set_extra_nomod_vsl(name, sym, vsl, NULL, alloc_estate_rl(new));
2356 else
2357 set_extra_mod(name, sym, NULL, alloc_estate_rl(new));
2360 free:
2361 free_string(name);
2364 static void db_param_limit(struct expression *expr, int param, char *key, char *value)
2366 db_param_limit_filter(expr, param, key, value, PARAM_LIMIT);
2369 static void db_param_filter(struct expression *expr, int param, char *key, char *value)
2371 db_param_limit_filter(expr, param, key, value, PARAM_FILTER);
2374 static void db_param_add_set(struct expression *expr, int param, char *key, char *value, enum info_type op)
2376 struct expression *arg;
2377 char *name, *tmp_name;
2378 struct symbol *sym, *tmp_sym;
2379 struct symbol *type;
2380 struct smatch_state *state;
2381 struct range_list *new = NULL;
2382 struct range_list *added = NULL;
2384 while (expr->type == EXPR_ASSIGNMENT)
2385 expr = strip_expr(expr->right);
2386 if (expr->type != EXPR_CALL)
2387 return;
2389 arg = get_argument_from_call_expr(expr->args, param);
2390 if (!arg)
2391 return;
2392 type = get_member_type_from_key(arg, key);
2393 name = get_variable_from_key(arg, key, &sym);
2394 if (!name || !sym)
2395 goto free;
2397 state = get_state(SMATCH_EXTRA, name, sym);
2398 if (state)
2399 new = estate_rl(state);
2401 call_results_to_rl(expr, type, value, &added);
2403 if (op == PARAM_SET)
2404 new = added;
2405 else
2406 new = rl_union(new, added);
2408 tmp_name = map_long_to_short_name_sym_nostack(name, sym, &tmp_sym);
2409 if (tmp_name && tmp_sym) {
2410 free_string(name);
2411 name = tmp_name;
2412 sym = tmp_sym;
2414 set_extra_mod(name, sym, NULL, alloc_estate_rl(new));
2415 free:
2416 free_string(name);
2419 static void db_param_add(struct expression *expr, int param, char *key, char *value)
2421 in_param_set = true;
2422 db_param_add_set(expr, param, key, value, PARAM_ADD);
2423 in_param_set = false;
2426 static void db_param_set(struct expression *expr, int param, char *key, char *value)
2428 in_param_set = true;
2429 db_param_add_set(expr, param, key, value, PARAM_SET);
2430 in_param_set = false;
2433 static void db_param_value(struct expression *expr, int param, char *key, char *value)
2435 struct expression *call;
2436 char *name;
2437 struct symbol *sym;
2438 struct symbol *type;
2439 struct range_list *rl = NULL;
2441 if (param != -1)
2442 return;
2444 call = expr;
2445 while (call->type == EXPR_ASSIGNMENT)
2446 call = strip_expr(call->right);
2447 if (call->type != EXPR_CALL)
2448 return;
2450 type = get_member_type_from_key(expr->left, key);
2451 name = get_variable_from_key(expr->left, key, &sym);
2452 if (!name || !sym)
2453 goto free;
2455 call_results_to_rl(call, type, value, &rl);
2457 set_extra_mod(name, sym, NULL, alloc_estate_rl(rl));
2458 free:
2459 free_string(name);
2462 static void match_call_info(struct expression *expr)
2464 struct smatch_state *state;
2465 struct range_list *rl = NULL;
2466 struct expression *arg;
2467 struct symbol *type;
2468 int i = 0;
2470 FOR_EACH_PTR(expr->args, arg) {
2471 type = get_arg_type(expr->fn, i);
2473 if (get_implied_rl(arg, &rl))
2474 rl = cast_rl(type, rl);
2475 else
2476 rl = cast_rl(type, alloc_whole_rl(get_type(arg)));
2478 if (!is_whole_rl(rl)) {
2479 rl = intersect_with_real_abs_expr(arg, rl);
2480 sql_insert_caller_info(expr, PARAM_VALUE, i, "$", show_rl(rl));
2482 state = get_state_expr(SMATCH_EXTRA, arg);
2483 if (estate_has_fuzzy_max(state)) {
2484 sql_insert_caller_info(expr, FUZZY_MAX, i, "$",
2485 sval_to_str(estate_get_fuzzy_max(state)));
2487 i++;
2488 } END_FOR_EACH_PTR(arg);
2491 static void set_param_value(const char *name, struct symbol *sym, char *key, char *value)
2493 struct range_list *rl = NULL;
2494 struct smatch_state *state;
2495 struct symbol *type;
2496 char fullname[256];
2497 sval_t dummy;
2499 if (strcmp(key, "*$") == 0)
2500 snprintf(fullname, sizeof(fullname), "*%s", name);
2501 else if (strncmp(key, "$", 1) == 0)
2502 snprintf(fullname, 256, "%s%s", name, key + 1);
2503 else
2504 return;
2506 type = get_member_type_from_key(symbol_expression(sym), key);
2507 str_to_rl(type, value, &rl);
2508 state = alloc_estate_rl(rl);
2509 if (estate_get_single_value(state, &dummy))
2510 estate_set_hard_max(state);
2511 set_state(SMATCH_EXTRA, fullname, sym, state);
2514 static void set_param_hard_max(const char *name, struct symbol *sym, char *key, char *value)
2516 struct range_list *rl = NULL;
2517 struct smatch_state *state;
2518 struct symbol *type;
2519 char fullname[256];
2520 sval_t max;
2522 if (strcmp(key, "*$") == 0)
2523 snprintf(fullname, sizeof(fullname), "*%s", name);
2524 else if (strncmp(key, "$", 1) == 0)
2525 snprintf(fullname, 256, "%s%s", name, key + 1);
2526 else
2527 return;
2529 state = get_state(SMATCH_EXTRA, fullname, sym);
2530 if (!state)
2531 return;
2532 type = get_member_type_from_key(symbol_expression(sym), key);
2533 str_to_rl(type, value, &rl);
2534 if (!rl_to_sval(rl, &max))
2535 return;
2536 estate_set_fuzzy_max(state, max);
2539 struct smatch_state *get_extra_state(struct expression *expr)
2541 char *name;
2542 struct symbol *sym;
2543 struct smatch_state *ret = NULL;
2544 struct range_list *rl;
2546 if (is_pointer(expr) && get_address_rl(expr, &rl))
2547 return alloc_estate_rl(rl);
2549 name = expr_to_known_chunk_sym(expr, &sym);
2550 if (!name)
2551 goto free;
2553 ret = get_state(SMATCH_EXTRA, name, sym);
2554 free:
2555 free_string(name);
2556 return ret;
2559 void register_smatch_extra(int id)
2561 my_id = id;
2563 add_merge_hook(my_id, &merge_estates);
2564 add_unmatched_state_hook(my_id, &unmatched_state);
2565 select_caller_info_hook(set_param_value, PARAM_VALUE);
2566 select_caller_info_hook(set_param_hard_max, FUZZY_MAX);
2567 select_return_states_before(&db_limited_before);
2568 select_return_states_hook(PARAM_LIMIT, &db_param_limit);
2569 select_return_states_hook(PARAM_FILTER, &db_param_filter);
2570 select_return_states_hook(PARAM_ADD, &db_param_add);
2571 select_return_states_hook(PARAM_SET, &db_param_set);
2572 select_return_states_hook(PARAM_VALUE, &db_param_value);
2573 select_return_states_after(&db_limited_after);
2576 static void match_link_modify(struct sm_state *sm, struct expression *mod_expr)
2578 struct var_sym_list *links;
2579 struct var_sym *tmp;
2580 struct smatch_state *state;
2582 links = sm->state->data;
2584 FOR_EACH_PTR(links, tmp) {
2585 if (sm->sym == tmp->sym &&
2586 strcmp(sm->name, tmp->var) == 0)
2587 continue;
2588 state = get_state(SMATCH_EXTRA, tmp->var, tmp->sym);
2589 if (!state)
2590 continue;
2591 set_state(SMATCH_EXTRA, tmp->var, tmp->sym, alloc_estate_whole(estate_type(state)));
2592 } END_FOR_EACH_PTR(tmp);
2593 set_state(link_id, sm->name, sm->sym, &undefined);
2596 void register_smatch_extra_links(int id)
2598 link_id = id;
2601 void register_smatch_extra_late(int id)
2603 add_merge_hook(link_id, &merge_link_states);
2604 add_modification_hook(link_id, &match_link_modify);
2605 add_hook(&match_dereferences, DEREF_HOOK);
2606 add_hook(&match_pointer_as_array, OP_HOOK);
2607 select_call_implies_hook(DEREFERENCE, &set_param_dereferenced);
2608 add_hook(&match_function_call, FUNCTION_CALL_HOOK);
2609 add_hook(&match_assign, ASSIGNMENT_HOOK);
2610 add_hook(&match_assign, GLOBAL_ASSIGNMENT_HOOK);
2611 add_hook(&unop_expr, OP_HOOK);
2612 add_hook(&asm_expr, ASM_HOOK);
2614 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
2615 add_member_info_callback(my_id, struct_member_callback);
2616 add_split_return_callback(&returned_struct_members);
2618 add_hook(&assume_indexes_are_valid, OP_HOOK);