kernel_user_data: handle struct assignments
[smatch.git] / smatch_extra.c
blob275a79d18ea47819b7e27de349d18e9e6d6cadba
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;
207 void set_extra_mod(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
209 char *new_name;
210 struct symbol *new_sym;
212 set_extra_mod_helper(name, sym, expr, state);
213 new_name = get_other_name_sym(name, sym, &new_sym);
214 if (new_name && new_sym)
215 set_extra_mod_helper(new_name, new_sym, expr, state);
216 free_string(new_name);
219 static struct expression *chunk_get_array_base(struct expression *expr)
222 * The problem with is_array() is that it only returns true for things
223 * like foo[1] but not for foo[1].bar.
226 expr = strip_expr(expr);
227 while (expr && expr->type == EXPR_DEREF)
228 expr = strip_expr(expr->deref);
229 return get_array_base(expr);
232 static int chunk_has_array(struct expression *expr)
234 return !!chunk_get_array_base(expr);
237 static void clear_array_states(struct expression *array)
239 struct sm_state *sm;
241 sm = get_sm_state_expr(link_id, array);
242 if (sm)
243 match_link_modify(sm, NULL);
246 static void set_extra_array_mod(struct expression *expr, struct smatch_state *state)
248 struct expression *array;
249 struct var_sym_list *vsl;
250 struct var_sym *vs;
251 char *name;
252 struct symbol *sym;
254 array = chunk_get_array_base(expr);
256 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
257 if (!name || !vsl) {
258 clear_array_states(array);
259 goto free;
262 FOR_EACH_PTR(vsl, vs) {
263 store_link(link_id, vs->var, vs->sym, name, sym);
264 } END_FOR_EACH_PTR(vs);
266 call_extra_mod_hooks(name, sym, expr, state);
267 set_state(SMATCH_EXTRA, name, sym, state);
268 free:
269 free_string(name);
272 void set_extra_expr_mod(struct expression *expr, struct smatch_state *state)
274 struct symbol *sym;
275 char *name;
277 if (chunk_has_array(expr)) {
278 set_extra_array_mod(expr, state);
279 return;
282 expr = strip_expr(expr);
283 name = expr_to_var_sym(expr, &sym);
284 if (!name || !sym)
285 goto free;
286 set_extra_mod(name, sym, expr, state);
287 free:
288 free_string(name);
291 void set_extra_nomod(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
293 char *new_name;
294 struct symbol *new_sym;
295 struct relation *rel;
296 struct smatch_state *orig_state;
298 orig_state = get_state(SMATCH_EXTRA, name, sym);
300 /* don't save unknown states if leaving it blank is the same */
301 if (!orig_state && estate_is_unknown(state))
302 return;
304 new_name = get_other_name_sym(name, sym, &new_sym);
305 if (new_name && new_sym)
306 set_extra_nomod_helper(new_name, new_sym, expr, state);
307 free_string(new_name);
309 if (!estate_related(orig_state)) {
310 set_extra_nomod_helper(name, sym, expr, state);
311 return;
314 set_related(state, estate_related(orig_state));
315 FOR_EACH_PTR(estate_related(orig_state), rel) {
316 struct smatch_state *estate;
318 if (option_debug_related)
319 sm_msg("%s updating related %s to %s", name, rel->name, state->name);
320 estate = get_state(SMATCH_EXTRA, rel->name, rel->sym);
321 if (!estate)
322 continue;
323 set_extra_nomod_helper(rel->name, rel->sym, expr, clone_estate_cast(estate_type(estate), state));
324 } END_FOR_EACH_PTR(rel);
327 void set_extra_nomod_vsl(const char *name, struct symbol *sym, struct var_sym_list *vsl, struct expression *expr, struct smatch_state *state)
329 struct var_sym *vs;
331 FOR_EACH_PTR(vsl, vs) {
332 store_link(link_id, vs->var, vs->sym, name, sym);
333 } END_FOR_EACH_PTR(vs);
335 set_extra_nomod(name, sym, expr, state);
339 * This is for return_implies_state() hooks which modify a SMATCH_EXTRA state
341 void set_extra_expr_nomod(struct expression *expr, struct smatch_state *state)
343 struct var_sym_list *vsl;
344 struct var_sym *vs;
345 char *name;
346 struct symbol *sym;
348 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
349 if (!name || !vsl)
350 goto free;
351 FOR_EACH_PTR(vsl, vs) {
352 store_link(link_id, vs->var, vs->sym, name, sym);
353 } END_FOR_EACH_PTR(vs);
355 set_extra_nomod(name, sym, expr, state);
356 free:
357 free_string(name);
360 static void set_extra_true_false(const char *name, struct symbol *sym,
361 struct smatch_state *true_state,
362 struct smatch_state *false_state)
364 char *new_name;
365 struct symbol *new_sym;
366 struct relation *rel;
367 struct smatch_state *orig_state;
369 if (!true_state && !false_state)
370 return;
372 if (in_warn_on_macro())
373 return;
375 new_name = get_other_name_sym(name, sym, &new_sym);
376 if (new_name && new_sym)
377 set_true_false_states(SMATCH_EXTRA, new_name, new_sym, true_state, false_state);
378 free_string(new_name);
380 orig_state = get_state(SMATCH_EXTRA, name, sym);
382 if (!estate_related(orig_state)) {
383 set_true_false_states(SMATCH_EXTRA, name, sym, true_state, false_state);
384 return;
387 if (true_state)
388 set_related(true_state, estate_related(orig_state));
389 if (false_state)
390 set_related(false_state, estate_related(orig_state));
392 FOR_EACH_PTR(estate_related(orig_state), rel) {
393 set_true_false_states(SMATCH_EXTRA, rel->name, rel->sym,
394 true_state, false_state);
395 } END_FOR_EACH_PTR(rel);
398 static void set_extra_chunk_true_false(struct expression *expr,
399 struct smatch_state *true_state,
400 struct smatch_state *false_state)
402 struct var_sym_list *vsl;
403 struct var_sym *vs;
404 struct symbol *type;
405 char *name;
406 struct symbol *sym;
408 if (in_warn_on_macro())
409 return;
411 type = get_type(expr);
412 if (!type)
413 return;
415 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
416 if (!name || !vsl)
417 goto free;
418 FOR_EACH_PTR(vsl, vs) {
419 store_link(link_id, vs->var, vs->sym, name, sym);
420 } END_FOR_EACH_PTR(vs);
422 set_true_false_states(SMATCH_EXTRA, name, sym,
423 clone_estate(true_state),
424 clone_estate(false_state));
425 free:
426 free_string(name);
429 static void set_extra_expr_true_false(struct expression *expr,
430 struct smatch_state *true_state,
431 struct smatch_state *false_state)
433 char *name;
434 struct symbol *sym;
435 sval_t sval;
437 if (!true_state && !false_state)
438 return;
440 if (get_value(expr, &sval))
441 return;
443 expr = strip_expr(expr);
444 name = expr_to_var_sym(expr, &sym);
445 if (!name || !sym) {
446 free_string(name);
447 set_extra_chunk_true_false(expr, true_state, false_state);
448 return;
450 set_extra_true_false(name, sym, true_state, false_state);
451 free_string(name);
454 static int get_countdown_info(struct expression *condition, struct expression **unop, int *op, sval_t *right)
456 struct expression *unop_expr;
457 int comparison;
458 sval_t limit;
460 right->type = &int_ctype;
461 right->value = 0;
463 condition = strip_expr(condition);
465 if (condition->type == EXPR_COMPARE) {
466 comparison = remove_unsigned_from_comparison(condition->op);
468 if (comparison != SPECIAL_GTE && comparison != '>')
469 return 0;
470 if (!get_value(condition->right, &limit))
471 return 0;
473 unop_expr = condition->left;
474 if (unop_expr->type != EXPR_PREOP && unop_expr->type != EXPR_POSTOP)
475 return 0;
476 if (unop_expr->op != SPECIAL_DECREMENT)
477 return 0;
479 *unop = unop_expr;
480 *op = comparison;
481 *right = limit;
483 return 1;
486 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
487 return 0;
488 if (condition->op != SPECIAL_DECREMENT)
489 return 0;
491 *unop = condition;
492 *op = '>';
494 return 1;
497 static struct sm_state *handle_canonical_while_count_down(struct statement *loop)
499 struct expression *iter_var;
500 struct expression *condition, *unop;
501 struct sm_state *sm;
502 struct smatch_state *estate;
503 int op;
504 sval_t start, right;
506 right.type = &int_ctype;
507 right.value = 0;
509 condition = strip_expr(loop->iterator_pre_condition);
510 if (!condition)
511 return NULL;
513 if (!get_countdown_info(condition, &unop, &op, &right))
514 return NULL;
516 iter_var = unop->unop;
518 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
519 if (!sm)
520 return NULL;
521 if (sval_cmp(estate_min(sm->state), right) < 0)
522 return NULL;
523 start = estate_max(sm->state);
524 if (sval_cmp(start, right) <= 0)
525 return NULL;
526 if (!sval_is_max(start))
527 start.value--;
529 if (op == SPECIAL_GTE)
530 right.value--;
532 if (unop->type == EXPR_PREOP) {
533 right.value++;
534 estate = alloc_estate_range(right, start);
535 if (estate_has_hard_max(sm->state))
536 estate_set_hard_max(estate);
537 estate_copy_fuzzy_max(estate, sm->state);
538 set_extra_expr_mod(iter_var, estate);
540 if (unop->type == EXPR_POSTOP) {
541 estate = alloc_estate_range(right, start);
542 if (estate_has_hard_max(sm->state))
543 estate_set_hard_max(estate);
544 estate_copy_fuzzy_max(estate, sm->state);
545 set_extra_expr_mod(iter_var, estate);
547 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
550 static struct sm_state *handle_canonical_for_inc(struct expression *iter_expr,
551 struct expression *condition)
553 struct expression *iter_var;
554 struct sm_state *sm;
555 struct smatch_state *estate;
556 sval_t start, end, max;
558 iter_var = iter_expr->unop;
559 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
560 if (!sm)
561 return NULL;
562 if (!estate_get_single_value(sm->state, &start))
563 return NULL;
564 if (!get_implied_value(condition->right, &end))
565 return NULL;
567 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
568 return NULL;
570 switch (condition->op) {
571 case SPECIAL_UNSIGNED_LT:
572 case SPECIAL_NOTEQUAL:
573 case '<':
574 if (!sval_is_min(end))
575 end.value--;
576 break;
577 case SPECIAL_UNSIGNED_LTE:
578 case SPECIAL_LTE:
579 break;
580 default:
581 return NULL;
583 if (sval_cmp(end, start) < 0)
584 return NULL;
585 estate = alloc_estate_range(start, end);
586 if (get_hard_max(condition->right, &max)) {
587 if (!get_macro_name(condition->pos))
588 estate_set_hard_max(estate);
589 if (condition->op == '<' ||
590 condition->op == SPECIAL_UNSIGNED_LT ||
591 condition->op == SPECIAL_NOTEQUAL)
592 max.value--;
593 estate_set_fuzzy_max(estate, max);
595 set_extra_expr_mod(iter_var, estate);
596 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
599 static struct sm_state *handle_canonical_for_dec(struct expression *iter_expr,
600 struct expression *condition)
602 struct expression *iter_var;
603 struct sm_state *sm;
604 struct smatch_state *estate;
605 sval_t start, end;
607 iter_var = iter_expr->unop;
608 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
609 if (!sm)
610 return NULL;
611 if (!estate_get_single_value(sm->state, &start))
612 return NULL;
613 if (!get_implied_min(condition->right, &end))
614 end = sval_type_min(get_type(iter_var));
615 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
616 return NULL;
618 switch (condition->op) {
619 case SPECIAL_NOTEQUAL:
620 case '>':
621 if (!sval_is_min(end) && !sval_is_max(end))
622 end.value++;
623 break;
624 case SPECIAL_GTE:
625 break;
626 default:
627 return NULL;
629 if (sval_cmp(end, start) > 0)
630 return NULL;
631 estate = alloc_estate_range(end, start);
632 estate_set_hard_max(estate);
633 estate_set_fuzzy_max(estate, estate_get_fuzzy_max(estate));
634 set_extra_expr_mod(iter_var, estate);
635 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
638 static struct sm_state *handle_canonical_for_loops(struct statement *loop)
640 struct expression *iter_expr;
641 struct expression *condition;
643 if (!loop->iterator_post_statement)
644 return NULL;
645 if (loop->iterator_post_statement->type != STMT_EXPRESSION)
646 return NULL;
647 iter_expr = loop->iterator_post_statement->expression;
648 if (!loop->iterator_pre_condition)
649 return NULL;
650 if (loop->iterator_pre_condition->type != EXPR_COMPARE)
651 return NULL;
652 condition = loop->iterator_pre_condition;
654 if (iter_expr->op == SPECIAL_INCREMENT)
655 return handle_canonical_for_inc(iter_expr, condition);
656 if (iter_expr->op == SPECIAL_DECREMENT)
657 return handle_canonical_for_dec(iter_expr, condition);
658 return NULL;
661 struct sm_state *__extra_handle_canonical_loops(struct statement *loop, struct stree **stree)
663 struct sm_state *ret;
666 * Canonical loops are a hack. The proper way to handle this is to
667 * use two passes, but unfortunately, doing two passes makes parsing
668 * code twice as slow.
670 * What we do is we set the inside state here, which overwrites whatever
671 * __extra_match_condition() does. Then we set the outside state in
672 * __extra_pre_loop_hook_after().
675 __push_fake_cur_stree();
676 if (!loop->iterator_post_statement)
677 ret = handle_canonical_while_count_down(loop);
678 else
679 ret = handle_canonical_for_loops(loop);
680 *stree = __pop_fake_cur_stree();
681 return ret;
684 int __iterator_unchanged(struct sm_state *sm)
686 if (!sm)
687 return 0;
688 if (get_sm_state(my_id, sm->name, sm->sym) == sm)
689 return 1;
690 return 0;
693 static void while_count_down_after(struct sm_state *sm, struct expression *condition)
695 struct expression *unop;
696 int op;
697 sval_t limit, after_value;
699 if (!get_countdown_info(condition, &unop, &op, &limit))
700 return;
701 after_value = estate_min(sm->state);
702 after_value.value--;
703 set_extra_mod(sm->name, sm->sym, condition->unop, alloc_estate_sval(after_value));
706 void __extra_pre_loop_hook_after(struct sm_state *sm,
707 struct statement *iterator,
708 struct expression *condition)
710 struct expression *iter_expr;
711 sval_t limit;
712 struct smatch_state *state;
714 if (!iterator) {
715 while_count_down_after(sm, condition);
716 return;
719 iter_expr = iterator->expression;
721 if (condition->type != EXPR_COMPARE)
722 return;
723 if (iter_expr->op == SPECIAL_INCREMENT) {
724 limit = sval_binop(estate_max(sm->state), '+',
725 sval_type_val(estate_type(sm->state), 1));
726 } else {
727 limit = sval_binop(estate_min(sm->state), '-',
728 sval_type_val(estate_type(sm->state), 1));
730 if (!estate_has_hard_max(sm->state) && !__has_breaks()) {
731 if (iter_expr->op == SPECIAL_INCREMENT)
732 state = alloc_estate_range(estate_min(sm->state), limit);
733 else
734 state = alloc_estate_range(limit, estate_max(sm->state));
735 } else {
736 state = alloc_estate_sval(limit);
738 if (!estate_has_hard_max(sm->state)) {
739 estate_clear_hard_max(state);
741 if (estate_has_fuzzy_max(sm->state)) {
742 sval_t hmax = estate_get_fuzzy_max(sm->state);
743 sval_t max = estate_max(sm->state);
745 if (sval_cmp(hmax, max) != 0)
746 estate_clear_fuzzy_max(state);
747 } else if (!estate_has_fuzzy_max(sm->state)) {
748 estate_clear_fuzzy_max(state);
751 set_extra_mod(sm->name, sm->sym, iter_expr, state);
754 static struct stree *unmatched_stree;
755 static struct smatch_state *unmatched_state(struct sm_state *sm)
757 struct smatch_state *state;
759 if (unmatched_stree) {
760 state = get_state_stree(unmatched_stree, SMATCH_EXTRA, sm->name, sm->sym);
761 if (state)
762 return state;
764 if (parent_is_gone_var_sym(sm->name, sm->sym))
765 return alloc_estate_empty();
766 return alloc_estate_whole(estate_type(sm->state));
769 static void clear_the_pointed_at(struct expression *expr)
771 struct stree *stree;
772 char *name;
773 struct symbol *sym;
774 struct sm_state *tmp;
776 name = expr_to_var_sym(expr, &sym);
777 if (!name || !sym)
778 goto free;
780 stree = __get_cur_stree();
781 FOR_EACH_MY_SM(SMATCH_EXTRA, stree, tmp) {
782 if (tmp->name[0] != '*')
783 continue;
784 if (tmp->sym != sym)
785 continue;
786 if (strcmp(tmp->name + 1, name) != 0)
787 continue;
788 set_extra_mod(tmp->name, tmp->sym, expr, alloc_estate_whole(estate_type(tmp->state)));
789 } END_FOR_EACH_SM(tmp);
791 free:
792 free_string(name);
795 static int is_const_param(struct expression *expr, int param)
797 struct symbol *type;
799 type = get_arg_type(expr, param);
800 if (!type)
801 return 0;
802 if (type->ctype.modifiers & MOD_CONST)
803 return 1;
804 return 0;
807 static void match_function_call(struct expression *expr)
809 struct expression *arg;
810 struct expression *tmp;
811 int param = -1;
813 /* if we have the db this is handled in smatch_function_hooks.c */
814 if (!option_no_db)
815 return;
816 if (inlinable(expr->fn))
817 return;
819 FOR_EACH_PTR(expr->args, arg) {
820 param++;
821 if (is_const_param(expr->fn, param))
822 continue;
823 tmp = strip_expr(arg);
824 if (tmp->type == EXPR_PREOP && tmp->op == '&')
825 set_extra_expr_mod(tmp->unop, alloc_estate_whole(get_type(tmp->unop)));
826 else
827 clear_the_pointed_at(tmp);
828 } END_FOR_EACH_PTR(arg);
831 static int values_fit_type(struct expression *left, struct expression *right)
833 struct range_list *rl;
834 struct symbol *type;
836 type = get_type(left);
837 if (!type)
838 return 0;
839 get_absolute_rl(right, &rl);
840 if (type == rl_type(rl))
841 return 1;
842 if (type_unsigned(type) && sval_is_negative(rl_min(rl)))
843 return 0;
844 if (sval_cmp(sval_type_min(type), rl_min(rl)) > 0)
845 return 0;
846 if (sval_cmp(sval_type_max(type), rl_max(rl)) < 0)
847 return 0;
848 return 1;
851 static void save_chunk_info(struct expression *left, struct expression *right)
853 struct var_sym_list *vsl;
854 struct var_sym *vs;
855 struct expression *add_expr;
856 struct symbol *type;
857 sval_t sval;
858 char *name;
859 struct symbol *sym;
861 if (right->type != EXPR_BINOP || right->op != '-')
862 return;
863 if (!get_value(right->left, &sval))
864 return;
865 if (!expr_to_sym(right->right))
866 return;
868 add_expr = binop_expression(left, '+', right->right);
869 type = get_type(add_expr);
870 if (!type)
871 return;
872 name = expr_to_chunk_sym_vsl(add_expr, &sym, &vsl);
873 if (!name || !vsl)
874 goto free;
875 FOR_EACH_PTR(vsl, vs) {
876 store_link(link_id, vs->var, vs->sym, name, sym);
877 } END_FOR_EACH_PTR(vs);
879 set_state(SMATCH_EXTRA, name, sym, alloc_estate_sval(sval_cast(type, sval)));
880 free:
881 free_string(name);
884 static void do_array_assign(struct expression *left, int op, struct expression *right)
886 struct range_list *rl;
888 if (op == '=') {
889 get_absolute_rl(right, &rl);
890 rl = cast_rl(get_type(left), rl);
891 } else {
892 rl = alloc_whole_rl(get_type(left));
895 set_extra_array_mod(left, alloc_estate_rl(rl));
898 static void match_vanilla_assign(struct expression *left, struct expression *right)
900 struct range_list *orig_rl = NULL;
901 struct range_list *rl = NULL;
902 struct symbol *right_sym;
903 struct symbol *left_type;
904 struct symbol *right_type;
905 char *right_name = NULL;
906 struct symbol *sym;
907 char *name;
908 sval_t sval, max;
909 struct smatch_state *state;
910 int comparison;
912 if (is_struct(left))
913 return;
915 save_chunk_info(left, right);
917 name = expr_to_var_sym(left, &sym);
918 if (!name) {
919 if (chunk_has_array(left))
920 do_array_assign(left, '=', right);
921 return;
924 left_type = get_type(left);
925 right_type = get_type(right);
927 right_name = expr_to_var_sym(right, &right_sym);
929 if (!__in_fake_assign &&
930 !(right->type == EXPR_PREOP && right->op == '&') &&
931 right_name && right_sym &&
932 values_fit_type(left, strip_expr(right)) &&
933 !has_symbol(right, sym)) {
934 set_equiv(left, right);
935 goto free;
938 if (is_pointer(right) && get_address_rl(right, &rl)) {
939 state = alloc_estate_rl(rl);
940 goto done;
943 if (get_implied_value(right, &sval)) {
944 state = alloc_estate_sval(sval_cast(left_type, sval));
945 goto done;
948 if (__in_fake_assign) {
949 struct smatch_state *right_state;
950 sval_t sval;
952 if (get_value(right, &sval)) {
953 sval = sval_cast(left_type, sval);
954 state = alloc_estate_sval(sval);
955 goto done;
958 right_state = get_state(SMATCH_EXTRA, right_name, right_sym);
959 if (right_state) {
960 /* simple assignment */
961 state = clone_estate(right_state);
962 goto done;
965 state = alloc_estate_rl(alloc_whole_rl(left_type));
966 goto done;
969 comparison = get_comparison(left, right);
970 if (comparison) {
971 comparison = flip_comparison(comparison);
972 get_implied_rl(left, &orig_rl);
975 if (get_implied_rl(right, &rl)) {
976 rl = cast_rl(left_type, rl);
977 if (orig_rl)
978 filter_by_comparison(&rl, comparison, orig_rl);
979 state = alloc_estate_rl(rl);
980 if (get_hard_max(right, &max)) {
981 estate_set_hard_max(state);
982 estate_set_fuzzy_max(state, max);
984 } else {
985 rl = alloc_whole_rl(right_type);
986 rl = cast_rl(left_type, rl);
987 if (orig_rl)
988 filter_by_comparison(&rl, comparison, orig_rl);
989 state = alloc_estate_rl(rl);
992 done:
993 set_extra_mod(name, sym, left, state);
994 free:
995 free_string(right_name);
998 static int op_remove_assign(int op)
1000 switch (op) {
1001 case SPECIAL_ADD_ASSIGN:
1002 return '+';
1003 case SPECIAL_SUB_ASSIGN:
1004 return '-';
1005 case SPECIAL_MUL_ASSIGN:
1006 return '*';
1007 case SPECIAL_DIV_ASSIGN:
1008 return '/';
1009 case SPECIAL_MOD_ASSIGN:
1010 return '%';
1011 case SPECIAL_AND_ASSIGN:
1012 return '&';
1013 case SPECIAL_OR_ASSIGN:
1014 return '|';
1015 case SPECIAL_XOR_ASSIGN:
1016 return '^';
1017 case SPECIAL_SHL_ASSIGN:
1018 return SPECIAL_LEFTSHIFT;
1019 case SPECIAL_SHR_ASSIGN:
1020 return SPECIAL_RIGHTSHIFT;
1021 default:
1022 return op;
1026 static void match_assign(struct expression *expr)
1028 struct range_list *rl = NULL;
1029 struct expression *left;
1030 struct expression *right;
1031 struct expression *binop_expr;
1032 struct symbol *left_type;
1033 struct symbol *sym;
1034 char *name;
1035 sval_t left_min, left_max;
1036 sval_t right_min, right_max;
1037 sval_t res_min, res_max;
1039 left = strip_expr(expr->left);
1041 right = strip_parens(expr->right);
1042 if (right->type == EXPR_CALL && sym_name_is("__builtin_expect", right->fn))
1043 right = get_argument_from_call_expr(right->args, 0);
1044 while (right->type == EXPR_ASSIGNMENT && right->op == '=')
1045 right = strip_parens(right->left);
1047 if (expr->op == '=' && is_condition(expr->right))
1048 return; /* handled in smatch_condition.c */
1049 if (expr->op == '=' && right->type == EXPR_CALL)
1050 return; /* handled in smatch_function_hooks.c */
1051 if (expr->op == '=') {
1052 match_vanilla_assign(left, right);
1053 return;
1056 name = expr_to_var_sym(left, &sym);
1057 if (!name)
1058 return;
1060 left_type = get_type(left);
1062 res_min = sval_type_min(left_type);
1063 res_max = sval_type_max(left_type);
1065 switch (expr->op) {
1066 case SPECIAL_ADD_ASSIGN:
1067 get_absolute_max(left, &left_max);
1068 get_absolute_max(right, &right_max);
1069 if (sval_binop_overflows(left_max, '+', sval_cast(left_type, right_max)))
1070 break;
1071 if (get_implied_min(left, &left_min) &&
1072 !sval_is_negative_min(left_min) &&
1073 get_implied_min(right, &right_min) &&
1074 !sval_is_negative_min(right_min)) {
1075 res_min = sval_binop(left_min, '+', right_min);
1076 res_min = sval_cast(left_type, res_min);
1078 if (inside_loop()) /* we are assuming loops don't lead to wrapping */
1079 break;
1080 res_max = sval_binop(left_max, '+', right_max);
1081 res_max = sval_cast(left_type, res_max);
1082 break;
1083 case SPECIAL_SUB_ASSIGN:
1084 if (get_implied_max(left, &left_max) &&
1085 !sval_is_max(left_max) &&
1086 get_implied_min(right, &right_min) &&
1087 !sval_is_min(right_min)) {
1088 res_max = sval_binop(left_max, '-', right_min);
1089 res_max = sval_cast(left_type, res_max);
1091 if (inside_loop())
1092 break;
1093 if (get_implied_min(left, &left_min) &&
1094 !sval_is_min(left_min) &&
1095 get_implied_max(right, &right_max) &&
1096 !sval_is_max(right_max)) {
1097 res_min = sval_binop(left_min, '-', right_max);
1098 res_min = sval_cast(left_type, res_min);
1100 break;
1101 case SPECIAL_AND_ASSIGN:
1102 case SPECIAL_MOD_ASSIGN:
1103 case SPECIAL_SHL_ASSIGN:
1104 case SPECIAL_SHR_ASSIGN:
1105 case SPECIAL_OR_ASSIGN:
1106 case SPECIAL_XOR_ASSIGN:
1107 case SPECIAL_MUL_ASSIGN:
1108 case SPECIAL_DIV_ASSIGN:
1109 binop_expr = binop_expression(expr->left,
1110 op_remove_assign(expr->op),
1111 expr->right);
1112 if (get_absolute_rl(binop_expr, &rl)) {
1113 rl = cast_rl(left_type, rl);
1114 set_extra_mod(name, sym, left, alloc_estate_rl(rl));
1115 goto free;
1117 break;
1119 rl = cast_rl(left_type, alloc_rl(res_min, res_max));
1120 set_extra_mod(name, sym, left, alloc_estate_rl(rl));
1121 free:
1122 free_string(name);
1125 static struct smatch_state *increment_state(struct smatch_state *state)
1127 sval_t min = estate_min(state);
1128 sval_t max = estate_max(state);
1130 if (!estate_rl(state))
1131 return NULL;
1133 if (inside_loop())
1134 max = sval_type_max(max.type);
1136 if (!sval_is_min(min) && !sval_is_max(min))
1137 min.value++;
1138 if (!sval_is_min(max) && !sval_is_max(max))
1139 max.value++;
1140 return alloc_estate_range(min, max);
1143 static struct smatch_state *decrement_state(struct smatch_state *state)
1145 sval_t min = estate_min(state);
1146 sval_t max = estate_max(state);
1148 if (!estate_rl(state))
1149 return NULL;
1151 if (inside_loop())
1152 min = sval_type_min(min.type);
1154 if (!sval_is_min(min) && !sval_is_max(min))
1155 min.value--;
1156 if (!sval_is_min(max) && !sval_is_max(max))
1157 max.value--;
1158 return alloc_estate_range(min, max);
1161 static void clear_pointed_at_state(struct expression *expr)
1163 struct symbol *type;
1166 * ALERT: This is sort of a mess. If it's is a struct assigment like
1167 * "foo = bar;", then that's handled by smatch_struct_assignment.c.
1168 * the same thing for p++ where "p" is a struct. Most modifications
1169 * are handled by the assignment hook or the db. Smatch_extra.c doesn't
1170 * use smatch_modification.c because we have to get the ordering right
1171 * or something. So if you have p++ where p is a pointer to a standard
1172 * c type then we handle that here. What a mess.
1174 expr = strip_expr(expr);
1175 type = get_type(expr);
1176 if (!type || type->type != SYM_PTR)
1177 return;
1178 type = get_real_base_type(type);
1179 if (!type || type->type != SYM_BASETYPE)
1180 return;
1181 set_extra_expr_nomod(deref_expression(expr), alloc_estate_whole(type));
1184 static void unop_expr(struct expression *expr)
1186 struct smatch_state *state;
1188 if (expr->smatch_flags & Handled)
1189 return;
1191 switch (expr->op) {
1192 case SPECIAL_INCREMENT:
1193 state = get_state_expr(SMATCH_EXTRA, expr->unop);
1194 state = increment_state(state);
1195 if (!state)
1196 state = alloc_estate_whole(get_type(expr));
1197 set_extra_expr_mod(expr->unop, state);
1198 clear_pointed_at_state(expr->unop);
1199 break;
1200 case SPECIAL_DECREMENT:
1201 state = get_state_expr(SMATCH_EXTRA, expr->unop);
1202 state = decrement_state(state);
1203 if (!state)
1204 state = alloc_estate_whole(get_type(expr));
1205 set_extra_expr_mod(expr->unop, state);
1206 clear_pointed_at_state(expr->unop);
1207 break;
1208 default:
1209 return;
1213 static void asm_expr(struct statement *stmt)
1216 struct expression *expr;
1217 struct symbol *type;
1218 int state = 0;
1220 FOR_EACH_PTR(stmt->asm_outputs, expr) {
1221 switch (state) {
1222 case 0: /* identifier */
1223 case 1: /* constraint */
1224 state++;
1225 continue;
1226 case 2: /* expression */
1227 state = 0;
1228 type = get_type(strip_expr(expr));
1229 set_extra_expr_mod(expr, alloc_estate_whole(type));
1230 continue;
1232 } END_FOR_EACH_PTR(expr);
1235 static void check_dereference(struct expression *expr)
1237 struct smatch_state *state;
1239 if (__in_fake_assign)
1240 return;
1241 if (outside_of_function())
1242 return;
1243 state = get_extra_state(expr);
1244 if (state) {
1245 struct range_list *rl;
1247 rl = rl_intersection(estate_rl(state), valid_ptr_rl);
1248 if (rl_equiv(rl, estate_rl(state)))
1249 return;
1250 set_extra_expr_nomod(expr, alloc_estate_rl(rl));
1251 } else {
1252 set_extra_expr_nomod(expr, alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval));
1256 static void match_dereferences(struct expression *expr)
1258 if (expr->type != EXPR_PREOP)
1259 return;
1260 /* it's saying that foo[1] = bar dereferences foo[1] */
1261 if (is_array(expr))
1262 return;
1263 check_dereference(expr->unop);
1266 static void match_pointer_as_array(struct expression *expr)
1268 if (!is_array(expr))
1269 return;
1270 check_dereference(get_array_base(expr));
1273 static void find_dereferences(struct expression *expr)
1275 while (expr->type == EXPR_PREOP) {
1276 if (expr->op == '*')
1277 check_dereference(expr->unop);
1278 expr = strip_expr(expr->unop);
1282 static void set_param_dereferenced(struct expression *call, struct expression *arg, char *key, char *unused)
1284 struct symbol *sym;
1285 char *name;
1287 name = get_variable_from_key(arg, key, &sym);
1288 if (name && sym) {
1289 struct smatch_state *orig, *new;
1290 struct range_list *rl;
1292 orig = get_state(SMATCH_EXTRA, name, sym);
1293 if (orig) {
1294 rl = rl_intersection(estate_rl(orig),
1295 alloc_rl(valid_ptr_min_sval,
1296 valid_ptr_max_sval));
1297 new = alloc_estate_rl(rl);
1298 } else {
1299 new = alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval);
1302 set_extra_nomod(name, sym, NULL, new);
1304 free_string(name);
1306 find_dereferences(arg);
1309 static sval_t add_one(sval_t sval)
1311 sval.value++;
1312 return sval;
1315 static int handle_postop_inc(struct expression *left, int op, struct expression *right)
1317 struct statement *stmt;
1318 struct expression *cond;
1319 struct smatch_state *true_state, *false_state;
1320 sval_t start;
1321 sval_t limit;
1324 * If we're decrementing here then that's a canonical while count down
1325 * so it's handled already. We're only handling loops like:
1326 * i = 0;
1327 * do { ... } while (i++ < 3);
1330 if (left->type != EXPR_POSTOP || left->op != SPECIAL_INCREMENT)
1331 return 0;
1333 stmt = __cur_stmt->parent;
1334 if (!stmt)
1335 return 0;
1336 if (stmt->type == STMT_COMPOUND)
1337 stmt = stmt->parent;
1338 if (!stmt || stmt->type != STMT_ITERATOR || !stmt->iterator_post_condition)
1339 return 0;
1341 cond = strip_expr(stmt->iterator_post_condition);
1342 if (cond->type != EXPR_COMPARE || cond->op != op)
1343 return 0;
1344 if (left != strip_expr(cond->left) || right != strip_expr(cond->right))
1345 return 0;
1347 if (!get_implied_value(left->unop, &start))
1348 return 0;
1349 if (!get_implied_value(right, &limit))
1350 return 0;
1352 if (sval_cmp(start, limit) > 0)
1353 return 0;
1355 switch (op) {
1356 case '<':
1357 case SPECIAL_UNSIGNED_LT:
1358 break;
1359 case SPECIAL_LTE:
1360 case SPECIAL_UNSIGNED_LTE:
1361 limit = add_one(limit);
1362 default:
1363 return 0;
1367 true_state = alloc_estate_range(add_one(start), limit);
1368 false_state = alloc_estate_range(add_one(limit), add_one(limit));
1370 /* Currently we just discard the false state but when two passes is
1371 * implimented correctly then it will use it.
1374 set_extra_expr_true_false(left->unop, true_state, false_state);
1376 return 1;
1379 bool is_impossible_variable(struct expression *expr)
1381 struct smatch_state *state;
1383 state = get_extra_state(expr);
1384 if (state && !estate_rl(state))
1385 return true;
1386 return false;
1389 static bool in_macro(struct expression *left, struct expression *right)
1391 if (!left || !right)
1392 return 0;
1393 if (left->pos.line != right->pos.line || left->pos.pos != right->pos.pos)
1394 return 0;
1395 if (get_macro_name(left->pos))
1396 return 1;
1397 return 0;
1400 static void handle_comparison(struct symbol *type, struct expression *left, int op, struct expression *right)
1402 struct range_list *left_orig;
1403 struct range_list *left_true;
1404 struct range_list *left_false;
1405 struct range_list *right_orig;
1406 struct range_list *right_true;
1407 struct range_list *right_false;
1408 struct smatch_state *left_true_state;
1409 struct smatch_state *left_false_state;
1410 struct smatch_state *right_true_state;
1411 struct smatch_state *right_false_state;
1412 sval_t dummy, hard_max;
1413 int left_postop = 0;
1414 int right_postop = 0;
1416 if (left->op == SPECIAL_INCREMENT || left->op == SPECIAL_DECREMENT) {
1417 if (left->type == EXPR_POSTOP) {
1418 left->smatch_flags |= Handled;
1419 left_postop = left->op;
1420 if (handle_postop_inc(left, op, right))
1421 return;
1423 left = strip_parens(left->unop);
1425 while (left->type == EXPR_ASSIGNMENT)
1426 left = strip_parens(left->left);
1428 if (right->op == SPECIAL_INCREMENT || right->op == SPECIAL_DECREMENT) {
1429 if (right->type == EXPR_POSTOP) {
1430 right->smatch_flags |= Handled;
1431 right_postop = right->op;
1433 right = strip_parens(right->unop);
1436 if (is_impossible_variable(left) || is_impossible_variable(right))
1437 return;
1439 get_real_absolute_rl(left, &left_orig);
1440 left_orig = cast_rl(type, left_orig);
1442 get_real_absolute_rl(right, &right_orig);
1443 right_orig = cast_rl(type, right_orig);
1445 split_comparison_rl(left_orig, op, right_orig, &left_true, &left_false, &right_true, &right_false);
1447 left_true = rl_truncate_cast(get_type(strip_expr(left)), left_true);
1448 left_false = rl_truncate_cast(get_type(strip_expr(left)), left_false);
1449 right_true = rl_truncate_cast(get_type(strip_expr(right)), right_true);
1450 right_false = rl_truncate_cast(get_type(strip_expr(right)), right_false);
1452 if (!left_true || !left_false) {
1453 struct range_list *tmp_true, *tmp_false;
1455 split_comparison_rl(alloc_whole_rl(type), op, right_orig, &tmp_true, &tmp_false, NULL, NULL);
1456 tmp_true = rl_truncate_cast(get_type(strip_expr(left)), tmp_true);
1457 tmp_false = rl_truncate_cast(get_type(strip_expr(left)), tmp_false);
1458 if (tmp_true && tmp_false)
1459 __save_imaginary_state(left, tmp_true, tmp_false);
1462 if (!right_true || !right_false) {
1463 struct range_list *tmp_true, *tmp_false;
1465 split_comparison_rl(alloc_whole_rl(type), op, right_orig, NULL, NULL, &tmp_true, &tmp_false);
1466 tmp_true = rl_truncate_cast(get_type(strip_expr(right)), tmp_true);
1467 tmp_false = rl_truncate_cast(get_type(strip_expr(right)), tmp_false);
1468 if (tmp_true && tmp_false)
1469 __save_imaginary_state(right, tmp_true, tmp_false);
1472 left_true_state = alloc_estate_rl(left_true);
1473 left_false_state = alloc_estate_rl(left_false);
1474 right_true_state = alloc_estate_rl(right_true);
1475 right_false_state = alloc_estate_rl(right_false);
1477 switch (op) {
1478 case '<':
1479 case SPECIAL_UNSIGNED_LT:
1480 case SPECIAL_UNSIGNED_LTE:
1481 case SPECIAL_LTE:
1482 if (get_implied_value(right, &dummy) && !in_macro(left, right))
1483 estate_set_hard_max(left_true_state);
1484 if (get_implied_value(left, &dummy) && !in_macro(left, right))
1485 estate_set_hard_max(right_false_state);
1486 break;
1487 case '>':
1488 case SPECIAL_UNSIGNED_GT:
1489 case SPECIAL_UNSIGNED_GTE:
1490 case SPECIAL_GTE:
1491 if (get_implied_value(left, &dummy) && !in_macro(left, right))
1492 estate_set_hard_max(right_true_state);
1493 if (get_implied_value(right, &dummy) && !in_macro(left, right))
1494 estate_set_hard_max(left_false_state);
1495 break;
1498 switch (op) {
1499 case '<':
1500 case SPECIAL_UNSIGNED_LT:
1501 case SPECIAL_UNSIGNED_LTE:
1502 case SPECIAL_LTE:
1503 if (get_hard_max(right, &hard_max)) {
1504 if (op == '<' || op == SPECIAL_UNSIGNED_LT)
1505 hard_max.value--;
1506 estate_set_fuzzy_max(left_true_state, hard_max);
1508 if (get_implied_value(right, &hard_max)) {
1509 if (op == SPECIAL_UNSIGNED_LTE ||
1510 op == SPECIAL_LTE)
1511 hard_max.value++;
1512 estate_set_fuzzy_max(left_false_state, hard_max);
1514 if (get_hard_max(left, &hard_max)) {
1515 if (op == SPECIAL_UNSIGNED_LTE ||
1516 op == SPECIAL_LTE)
1517 hard_max.value--;
1518 estate_set_fuzzy_max(right_false_state, hard_max);
1520 if (get_implied_value(left, &hard_max)) {
1521 if (op == '<' || op == SPECIAL_UNSIGNED_LT)
1522 hard_max.value++;
1523 estate_set_fuzzy_max(right_true_state, hard_max);
1525 break;
1526 case '>':
1527 case SPECIAL_UNSIGNED_GT:
1528 case SPECIAL_UNSIGNED_GTE:
1529 case SPECIAL_GTE:
1530 if (get_hard_max(left, &hard_max)) {
1531 if (op == '>' || op == SPECIAL_UNSIGNED_GT)
1532 hard_max.value--;
1533 estate_set_fuzzy_max(right_true_state, hard_max);
1535 if (get_implied_value(left, &hard_max)) {
1536 if (op == SPECIAL_UNSIGNED_GTE ||
1537 op == SPECIAL_GTE)
1538 hard_max.value++;
1539 estate_set_fuzzy_max(right_false_state, hard_max);
1541 if (get_hard_max(right, &hard_max)) {
1542 if (op == SPECIAL_UNSIGNED_LTE ||
1543 op == SPECIAL_LTE)
1544 hard_max.value--;
1545 estate_set_fuzzy_max(left_false_state, hard_max);
1547 if (get_implied_value(right, &hard_max)) {
1548 if (op == '>' ||
1549 op == SPECIAL_UNSIGNED_GT)
1550 hard_max.value++;
1551 estate_set_fuzzy_max(left_true_state, hard_max);
1553 break;
1554 case SPECIAL_EQUAL:
1555 if (get_hard_max(left, &hard_max))
1556 estate_set_fuzzy_max(right_true_state, hard_max);
1557 if (get_hard_max(right, &hard_max))
1558 estate_set_fuzzy_max(left_true_state, hard_max);
1559 break;
1562 if (get_hard_max(left, &hard_max)) {
1563 estate_set_hard_max(left_true_state);
1564 estate_set_hard_max(left_false_state);
1566 if (get_hard_max(right, &hard_max)) {
1567 estate_set_hard_max(right_true_state);
1568 estate_set_hard_max(right_false_state);
1571 if (left_postop == SPECIAL_INCREMENT) {
1572 left_true_state = increment_state(left_true_state);
1573 left_false_state = increment_state(left_false_state);
1575 if (left_postop == SPECIAL_DECREMENT) {
1576 left_true_state = decrement_state(left_true_state);
1577 left_false_state = decrement_state(left_false_state);
1579 if (right_postop == SPECIAL_INCREMENT) {
1580 right_true_state = increment_state(right_true_state);
1581 right_false_state = increment_state(right_false_state);
1583 if (right_postop == SPECIAL_DECREMENT) {
1584 right_true_state = decrement_state(right_true_state);
1585 right_false_state = decrement_state(right_false_state);
1588 if (estate_rl(left_true_state) && estates_equiv(left_true_state, left_false_state)) {
1589 left_true_state = NULL;
1590 left_false_state = NULL;
1593 if (estate_rl(right_true_state) && estates_equiv(right_true_state, right_false_state)) {
1594 right_true_state = NULL;
1595 right_false_state = NULL;
1598 /* Don't introduce new states for known true/false conditions */
1599 if (rl_equiv(left_orig, estate_rl(left_true_state)))
1600 left_true_state = NULL;
1601 if (rl_equiv(left_orig, estate_rl(left_false_state)))
1602 left_false_state = NULL;
1603 if (rl_equiv(right_orig, estate_rl(right_true_state)))
1604 right_true_state = NULL;
1605 if (rl_equiv(right_orig, estate_rl(right_false_state)))
1606 right_false_state = NULL;
1608 set_extra_expr_true_false(left, left_true_state, left_false_state);
1609 set_extra_expr_true_false(right, right_true_state, right_false_state);
1612 static int is_simple_math(struct expression *expr)
1614 if (!expr)
1615 return 0;
1616 if (expr->type != EXPR_BINOP)
1617 return 0;
1618 switch (expr->op) {
1619 case '+':
1620 case '-':
1621 case '*':
1622 return 1;
1624 return 0;
1627 static void move_known_values(struct expression **left_p, struct expression **right_p)
1629 struct expression *left = *left_p;
1630 struct expression *right = *right_p;
1631 sval_t sval, dummy;
1633 if (get_implied_value(left, &sval)) {
1634 if (!is_simple_math(right))
1635 return;
1636 if (get_implied_value(right, &dummy))
1637 return;
1638 if (right->op == '*') {
1639 sval_t divisor;
1641 if (!get_value(right->right, &divisor))
1642 return;
1643 if (divisor.value == 0)
1644 return;
1645 *left_p = binop_expression(left, invert_op(right->op), right->right);
1646 *right_p = right->left;
1647 return;
1649 if (right->op == '+' && get_value(right->left, &sval)) {
1650 *left_p = binop_expression(left, invert_op(right->op), right->left);
1651 *right_p = right->right;
1652 return;
1654 if (get_value(right->right, &sval)) {
1655 *left_p = binop_expression(left, invert_op(right->op), right->right);
1656 *right_p = right->left;
1657 return;
1659 return;
1661 if (get_implied_value(right, &sval)) {
1662 if (!is_simple_math(left))
1663 return;
1664 if (get_implied_value(left, &dummy))
1665 return;
1666 if (left->op == '*') {
1667 sval_t divisor;
1669 if (!get_value(left->right, &divisor))
1670 return;
1671 if (divisor.value == 0)
1672 return;
1673 *right_p = binop_expression(right, invert_op(left->op), left->right);
1674 *left_p = left->left;
1675 return;
1677 if (left->op == '+' && get_value(left->left, &sval)) {
1678 *right_p = binop_expression(right, invert_op(left->op), left->left);
1679 *left_p = left->right;
1680 return;
1683 if (get_value(left->right, &sval)) {
1684 *right_p = binop_expression(right, invert_op(left->op), left->right);
1685 *left_p = left->left;
1686 return;
1688 return;
1693 * The reason for do_simple_algebra() is to solve things like:
1694 * if (foo > 66 || foo + bar > 64) {
1695 * "foo" is not really a known variable so it won't be handled by
1696 * move_known_variables() but it's a super common idiom.
1699 static int do_simple_algebra(struct expression **left_p, struct expression **right_p)
1701 struct expression *left = *left_p;
1702 struct expression *right = *right_p;
1703 struct range_list *rl;
1704 sval_t tmp;
1706 if (left->type != EXPR_BINOP || left->op != '+')
1707 return 0;
1708 if (can_integer_overflow(get_type(left), left))
1709 return 0;
1710 if (!get_implied_value(right, &tmp))
1711 return 0;
1713 if (!get_implied_value(left->left, &tmp) &&
1714 get_implied_rl(left->left, &rl) &&
1715 !is_whole_rl(rl)) {
1716 *right_p = binop_expression(right, '-', left->left);
1717 *left_p = left->right;
1718 return 1;
1720 if (!get_implied_value(left->right, &tmp) &&
1721 get_implied_rl(left->right, &rl) &&
1722 !is_whole_rl(rl)) {
1723 *right_p = binop_expression(right, '-', left->right);
1724 *left_p = left->left;
1725 return 1;
1728 return 0;
1731 static int match_func_comparison(struct expression *expr)
1733 struct expression *left = strip_expr(expr->left);
1734 struct expression *right = strip_expr(expr->right);
1736 if (left->type == EXPR_CALL || right->type == EXPR_CALL) {
1737 function_comparison(left, expr->op, right);
1738 return 1;
1741 return 0;
1744 /* Handle conditions like "if (foo + bar < foo) {" */
1745 static int handle_integer_overflow_test(struct expression *expr)
1747 struct expression *left, *right;
1748 struct symbol *type;
1749 sval_t left_min, right_min, min, max;
1751 if (expr->op != '<' && expr->op != SPECIAL_UNSIGNED_LT)
1752 return 0;
1754 left = strip_parens(expr->left);
1755 right = strip_parens(expr->right);
1757 if (left->op != '+')
1758 return 0;
1760 type = get_type(expr);
1761 if (!type)
1762 return 0;
1763 if (type_positive_bits(type) == 32) {
1764 max.type = &uint_ctype;
1765 max.uvalue = (unsigned int)-1;
1766 } else if (type_positive_bits(type) == 64) {
1767 max.type = &ulong_ctype;
1768 max.value = (unsigned long long)-1;
1769 } else {
1770 return 0;
1773 if (!expr_equiv(left->left, right) && !expr_equiv(left->right, right))
1774 return 0;
1776 get_absolute_min(left->left, &left_min);
1777 get_absolute_min(left->right, &right_min);
1778 min = sval_binop(left_min, '+', right_min);
1780 set_extra_chunk_true_false(left, NULL, alloc_estate_range(min, max));
1781 return 1;
1784 static void match_comparison(struct expression *expr)
1786 struct expression *left_orig = strip_parens(expr->left);
1787 struct expression *right_orig = strip_parens(expr->right);
1788 struct expression *left, *right, *tmp;
1789 struct expression *prev;
1790 struct symbol *type;
1791 int redo, count;
1793 if (match_func_comparison(expr))
1794 return;
1796 type = get_type(expr);
1797 if (!type)
1798 type = &llong_ctype;
1800 if (handle_integer_overflow_test(expr))
1801 return;
1803 left = left_orig;
1804 right = right_orig;
1805 move_known_values(&left, &right);
1806 handle_comparison(type, left, expr->op, right);
1808 left = left_orig;
1809 right = right_orig;
1810 if (do_simple_algebra(&left, &right))
1811 handle_comparison(type, left, expr->op, right);
1813 prev = get_assigned_expr(left_orig);
1814 if (is_simple_math(prev) && has_variable(prev, left_orig) == 0) {
1815 left = prev;
1816 right = right_orig;
1817 move_known_values(&left, &right);
1818 handle_comparison(type, left, expr->op, right);
1821 prev = get_assigned_expr(right_orig);
1822 if (is_simple_math(prev) && has_variable(prev, right_orig) == 0) {
1823 left = left_orig;
1824 right = prev;
1825 move_known_values(&left, &right);
1826 handle_comparison(type, left, expr->op, right);
1829 redo = 0;
1830 left = left_orig;
1831 right = right_orig;
1832 if (get_last_expr_from_expression_stmt(left_orig)) {
1833 left = get_last_expr_from_expression_stmt(left_orig);
1834 redo = 1;
1836 if (get_last_expr_from_expression_stmt(right_orig)) {
1837 right = get_last_expr_from_expression_stmt(right_orig);
1838 redo = 1;
1841 if (!redo)
1842 return;
1844 count = 0;
1845 while ((tmp = get_assigned_expr(left))) {
1846 if (count++ > 3)
1847 break;
1848 left = strip_expr(tmp);
1850 count = 0;
1851 while ((tmp = get_assigned_expr(right))) {
1852 if (count++ > 3)
1853 break;
1854 right = strip_expr(tmp);
1857 handle_comparison(type, left, expr->op, right);
1860 static sval_t get_high_mask(sval_t known)
1862 sval_t ret;
1863 int i;
1865 ret = known;
1866 ret.value = 0;
1868 for (i = type_bits(known.type) - 1; i >= 0; i--) {
1869 if (known.uvalue & (1ULL << i))
1870 ret.uvalue |= (1ULL << i);
1871 else
1872 return ret;
1875 return ret;
1878 static void handle_AND_op(struct expression *var, sval_t known)
1880 struct range_list *orig_rl;
1881 struct range_list *true_rl = NULL;
1882 struct range_list *false_rl = NULL;
1883 int bit;
1884 sval_t low_mask = known;
1885 sval_t high_mask;
1886 sval_t max;
1888 get_absolute_rl(var, &orig_rl);
1890 if (known.value > 0) {
1891 bit = ffsll(known.value) - 1;
1892 low_mask.uvalue = (1ULL << bit) - 1;
1893 true_rl = remove_range(orig_rl, sval_type_val(known.type, 0), low_mask);
1895 high_mask = get_high_mask(known);
1896 if (high_mask.value) {
1897 bit = ffsll(high_mask.value) - 1;
1898 low_mask.uvalue = (1ULL << bit) - 1;
1900 false_rl = orig_rl;
1901 if (sval_is_negative(rl_min(orig_rl)))
1902 false_rl = remove_range(false_rl, sval_type_min(known.type), sval_type_val(known.type, -1));
1903 false_rl = remove_range(false_rl, low_mask, sval_type_max(known.type));
1904 if (type_signed(high_mask.type) && type_unsigned(rl_type(false_rl))) {
1905 false_rl = remove_range(false_rl,
1906 sval_type_val(rl_type(false_rl), sval_type_max(known.type).uvalue),
1907 sval_type_val(rl_type(false_rl), -1));
1909 } else if (known.value == 1 &&
1910 get_hard_max(var, &max) &&
1911 sval_cmp(max, rl_max(orig_rl)) == 0 &&
1912 max.value & 1) {
1913 false_rl = remove_range(orig_rl, max, max);
1915 set_extra_expr_true_false(var,
1916 true_rl ? alloc_estate_rl(true_rl) : NULL,
1917 false_rl ? alloc_estate_rl(false_rl) : NULL);
1920 static void handle_AND_condition(struct expression *expr)
1922 sval_t known;
1924 if (get_implied_value(expr->left, &known))
1925 handle_AND_op(expr->right, known);
1926 else if (get_implied_value(expr->right, &known))
1927 handle_AND_op(expr->left, known);
1930 static void handle_MOD_condition(struct expression *expr)
1932 struct range_list *orig_rl;
1933 struct range_list *true_rl;
1934 struct range_list *false_rl = NULL;
1935 sval_t right;
1936 sval_t zero = {
1937 .value = 0,
1940 if (!get_implied_value(expr->right, &right) || right.value == 0)
1941 return;
1942 get_absolute_rl(expr->left, &orig_rl);
1944 zero.type = rl_type(orig_rl);
1946 /* We're basically dorking around the min and max here */
1947 true_rl = remove_range(orig_rl, zero, zero);
1948 if (!sval_is_max(rl_max(true_rl)) &&
1949 !(rl_max(true_rl).value % right.value))
1950 true_rl = remove_range(true_rl, rl_max(true_rl), rl_max(true_rl));
1952 if (rl_equiv(true_rl, orig_rl))
1953 true_rl = NULL;
1955 if (sval_is_positive(rl_min(orig_rl)) &&
1956 (rl_max(orig_rl).value - rl_min(orig_rl).value) / right.value < 5) {
1957 sval_t add;
1958 int i;
1960 add = rl_min(orig_rl);
1961 add.value += right.value - (add.value % right.value);
1962 add.value -= right.value;
1964 for (i = 0; i < 5; i++) {
1965 add.value += right.value;
1966 if (add.value > rl_max(orig_rl).value)
1967 break;
1968 add_range(&false_rl, add, add);
1970 } else {
1971 if (rl_min(orig_rl).uvalue != 0 &&
1972 rl_min(orig_rl).uvalue < right.uvalue) {
1973 sval_t chop = right;
1974 chop.value--;
1975 false_rl = remove_range(orig_rl, zero, chop);
1978 if (!sval_is_max(rl_max(orig_rl)) &&
1979 (rl_max(orig_rl).value % right.value)) {
1980 sval_t chop = rl_max(orig_rl);
1981 chop.value -= chop.value % right.value;
1982 chop.value++;
1983 if (!false_rl)
1984 false_rl = clone_rl(orig_rl);
1985 false_rl = remove_range(false_rl, chop, rl_max(orig_rl));
1989 set_extra_expr_true_false(expr->left,
1990 true_rl ? alloc_estate_rl(true_rl) : NULL,
1991 false_rl ? alloc_estate_rl(false_rl) : NULL);
1994 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
1995 void __extra_match_condition(struct expression *expr)
1997 expr = strip_expr(expr);
1998 switch (expr->type) {
1999 case EXPR_CALL:
2000 function_comparison(expr, SPECIAL_NOTEQUAL, zero_expr());
2001 return;
2002 case EXPR_PREOP:
2003 case EXPR_SYMBOL:
2004 case EXPR_DEREF:
2005 handle_comparison(get_type(expr), expr, SPECIAL_NOTEQUAL, zero_expr());
2006 return;
2007 case EXPR_COMPARE:
2008 match_comparison(expr);
2009 return;
2010 case EXPR_ASSIGNMENT:
2011 __extra_match_condition(expr->left);
2012 return;
2013 case EXPR_BINOP:
2014 if (expr->op == '&')
2015 handle_AND_condition(expr);
2016 if (expr->op == '%')
2017 handle_MOD_condition(expr);
2018 return;
2022 static void assume_indexes_are_valid(struct expression *expr)
2024 struct expression *array_expr;
2025 int array_size;
2026 struct expression *offset;
2027 struct symbol *offset_type;
2028 struct range_list *rl_before;
2029 struct range_list *rl_after;
2030 struct range_list *filter = NULL;
2031 sval_t size;
2033 expr = strip_expr(expr);
2034 if (!is_array(expr))
2035 return;
2037 offset = get_array_offset(expr);
2038 offset_type = get_type(offset);
2039 if (offset_type && type_signed(offset_type)) {
2040 filter = alloc_rl(sval_type_min(offset_type),
2041 sval_type_val(offset_type, -1));
2044 array_expr = get_array_base(expr);
2045 array_size = get_real_array_size(array_expr);
2046 if (array_size > 1) {
2047 size = sval_type_val(offset_type, array_size);
2048 add_range(&filter, size, sval_type_max(offset_type));
2051 if (!filter)
2052 return;
2053 get_absolute_rl(offset, &rl_before);
2054 rl_after = rl_filter(rl_before, filter);
2055 if (rl_equiv(rl_before, rl_after))
2056 return;
2057 set_extra_expr_nomod(offset, alloc_estate_rl(rl_after));
2060 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
2061 int implied_not_equal(struct expression *expr, long long val)
2063 return !possibly_false(expr, SPECIAL_NOTEQUAL, value_expr(val));
2066 int implied_not_equal_name_sym(char *name, struct symbol *sym, long long val)
2068 struct smatch_state *estate;
2070 estate = get_state(SMATCH_EXTRA, name, sym);
2071 if (!estate)
2072 return 0;
2073 if (!rl_has_sval(estate_rl(estate), sval_type_val(estate_type(estate), 0)))
2074 return 1;
2075 return 0;
2078 int parent_is_null_var_sym(const char *name, struct symbol *sym)
2080 char buf[256];
2081 char *start;
2082 char *end;
2083 struct smatch_state *state;
2085 strncpy(buf, name, sizeof(buf) - 1);
2086 buf[sizeof(buf) - 1] = '\0';
2088 start = &buf[0];
2089 while (*start == '*') {
2090 start++;
2091 state = get_state(SMATCH_EXTRA, start, sym);
2092 if (!state)
2093 continue;
2094 if (!estate_rl(state))
2095 return 1;
2096 if (estate_min(state).value == 0 &&
2097 estate_max(state).value == 0)
2098 return 1;
2101 start = &buf[0];
2102 while (*start == '&')
2103 start++;
2105 while ((end = strrchr(start, '-'))) {
2106 *end = '\0';
2107 state = __get_state(SMATCH_EXTRA, start, sym);
2108 if (!state)
2109 continue;
2110 if (estate_min(state).value == 0 &&
2111 estate_max(state).value == 0)
2112 return 1;
2114 return 0;
2117 int parent_is_null(struct expression *expr)
2119 struct symbol *sym;
2120 char *var;
2121 int ret = 0;
2123 expr = strip_expr(expr);
2124 var = expr_to_var_sym(expr, &sym);
2125 if (!var || !sym)
2126 goto free;
2127 ret = parent_is_null_var_sym(var, sym);
2128 free:
2129 free_string(var);
2130 return ret;
2133 static int param_used_callback(void *found, int argc, char **argv, char **azColName)
2135 *(int *)found = 1;
2136 return 0;
2139 static int is_kzalloc_info(struct sm_state *sm)
2141 sval_t sval;
2144 * kzalloc() information is treated as special because so there is just
2145 * a lot of stuff initialized to zero and it makes building the database
2146 * take hours and hours.
2148 * In theory, we should just remove this line and not pass any unused
2149 * information, but I'm not sure enough that this code works so I want
2150 * to hold off on that for now.
2152 if (!estate_get_single_value(sm->state, &sval))
2153 return 0;
2154 if (sval.value != 0)
2155 return 0;
2156 return 1;
2159 static int is_really_long(struct sm_state *sm)
2161 const char *p;
2162 int cnt = 0;
2164 p = sm->name;
2165 while ((p = strstr(p, "->"))) {
2166 p += 2;
2167 cnt++;
2170 if (cnt < 3 ||
2171 strlen(sm->name) < 40)
2172 return 0;
2173 return 1;
2176 static int filter_unused_param_value_info(struct expression *call, int param, char *printed_name, struct sm_state *sm)
2178 int found = 0;
2180 /* for function pointers assume everything is used */
2181 if (call->fn->type != EXPR_SYMBOL)
2182 return 0;
2185 * This is to handle __builtin_mul_overflow(). In an ideal world we
2186 * would only need this for invalid code.
2189 if (!call->fn->symbol)
2190 return 0;
2192 if (!is_kzalloc_info(sm) && !is_really_long(sm))
2193 return 0;
2195 run_sql(&param_used_callback, &found,
2196 "select * from return_implies where %s and type = %d and parameter = %d and key = '%s';",
2197 get_static_filter(call->fn->symbol), PARAM_USED, param, printed_name);
2198 if (found)
2199 return 0;
2201 /* If the database is not built yet, then assume everything is used */
2202 run_sql(&param_used_callback, &found,
2203 "select * from return_implies where %s and type = %d;",
2204 get_static_filter(call->fn->symbol), PARAM_USED);
2205 if (!found)
2206 return 0;
2208 return 1;
2211 struct range_list *intersect_with_real_abs_var_sym(const char *name, struct symbol *sym, struct range_list *start)
2213 struct smatch_state *state;
2216 * Here is the difference between implied value and real absolute, say
2217 * you have:
2219 * int a = (u8)x;
2221 * Then you know that a is 0-255. That's real absolute. But you don't
2222 * know for sure that it actually goes up to 255. So it's not implied.
2223 * Implied indicates a degree of certainty.
2225 * But then say you cap "a" at 8. That means you know it goes up to
2226 * 8. So now the implied value is s32min-8. But you can combine it
2227 * with the real absolute to say that actually it's 0-8.
2229 * We are combining it here. But now that I think about it, this is
2230 * probably not the ideal place to combine it because it should proably
2231 * be done earlier. Oh well, this is an improvement on what was there
2232 * before so I'm going to commit this code.
2236 state = get_real_absolute_state_var_sym(name, sym);
2237 if (!state || !estate_rl(state))
2238 return start;
2240 return rl_intersection(estate_rl(state), start);
2243 struct range_list *intersect_with_real_abs_expr(struct expression *expr, struct range_list *start)
2245 struct smatch_state *state;
2246 struct range_list *abs_rl;
2248 state = get_real_absolute_state(expr);
2249 if (!state || !estate_rl(state))
2250 return start;
2252 abs_rl = cast_rl(rl_type(start), estate_rl(state));
2253 return rl_intersection(abs_rl, start);
2256 static void struct_member_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
2258 struct range_list *rl;
2260 if (estate_is_whole(sm->state))
2261 return;
2262 if (filter_unused_param_value_info(call, param, printed_name, sm))
2263 return;
2264 rl = estate_rl(sm->state);
2265 rl = intersect_with_real_abs_var_sym(sm->name, sm->sym, rl);
2266 sql_insert_caller_info(call, PARAM_VALUE, param, printed_name, show_rl(rl));
2267 if (estate_has_hard_max(sm->state))
2268 sql_insert_caller_info(call, HARD_MAX, param, printed_name,
2269 sval_to_str(estate_max(sm->state)));
2270 if (estate_has_fuzzy_max(sm->state))
2271 sql_insert_caller_info(call, FUZZY_MAX, param, printed_name,
2272 sval_to_str(estate_get_fuzzy_max(sm->state)));
2275 static void returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
2277 struct symbol *returned_sym;
2278 char *returned_name;
2279 struct sm_state *sm;
2280 char *compare_str;
2281 char name_buf[256];
2282 char val_buf[256];
2283 int len;
2285 // FIXME handle *$
2287 if (!is_pointer(expr))
2288 return;
2290 returned_name = expr_to_var_sym(expr, &returned_sym);
2291 if (!returned_name || !returned_sym)
2292 goto free;
2293 len = strlen(returned_name);
2295 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
2296 if (!estate_rl(sm->state))
2297 continue;
2298 if (returned_sym != sm->sym)
2299 continue;
2300 if (strncmp(returned_name, sm->name, len) != 0)
2301 continue;
2302 if (sm->name[len] != '-')
2303 continue;
2305 snprintf(name_buf, sizeof(name_buf), "$%s", sm->name + len);
2307 compare_str = name_sym_to_param_comparison(sm->name, sm->sym);
2308 if (!compare_str && estate_is_whole(sm->state))
2309 continue;
2310 snprintf(val_buf, sizeof(val_buf), "%s%s", sm->state->name, compare_str ?: "");
2312 sql_insert_return_states(return_id, return_ranges, PARAM_VALUE,
2313 -1, name_buf, val_buf);
2314 } END_FOR_EACH_SM(sm);
2316 free:
2317 free_string(returned_name);
2320 static void db_limited_before(void)
2322 unmatched_stree = clone_stree(__get_cur_stree());
2325 static void db_limited_after(void)
2327 free_stree(&unmatched_stree);
2330 static int rl_fits_in_type(struct range_list *rl, struct symbol *type)
2332 if (type_bits(rl_type(rl)) <= type_bits(type))
2333 return 1;
2334 if (sval_cmp(rl_max(rl), sval_type_max(type)) > 0)
2335 return 0;
2336 if (sval_is_negative(rl_min(rl)) &&
2337 sval_cmp(rl_min(rl), sval_type_min(type)) < 0)
2338 return 0;
2339 return 1;
2342 static int basically_the_same(struct range_list *orig, struct range_list *new)
2344 if (rl_equiv(orig, new))
2345 return 1;
2348 * The whole range is essentially the same as 0,4096-27777777777 so
2349 * don't overwrite the implications just to store that.
2352 if (rl_type(orig)->type == SYM_PTR &&
2353 is_whole_rl(orig) &&
2354 rl_min(new).value == 0 &&
2355 rl_max(new).value == valid_ptr_max)
2356 return 1;
2357 return 0;
2360 static void db_param_limit_binops(struct expression *arg, char *key, struct range_list *rl)
2362 struct range_list *left_rl;
2363 sval_t zero = { .type = rl_type(rl), };
2364 sval_t sval;
2366 if (arg->op != '*')
2367 return;
2368 if (!get_implied_value(arg->right, &sval))
2369 return;
2370 if (can_integer_overflow(get_type(arg), arg))
2371 return;
2373 left_rl = rl_binop(rl, '/', alloc_rl(sval, sval));
2374 if (!rl_has_sval(rl, zero))
2375 left_rl = remove_range(left_rl, zero, zero);
2377 set_extra_expr_nomod(arg->left, alloc_estate_rl(left_rl));
2380 static void db_param_limit_filter(struct expression *expr, int param, char *key, char *value, enum info_type op)
2382 struct expression *arg;
2383 char *name;
2384 struct symbol *sym;
2385 struct var_sym_list *vsl = NULL;
2386 struct sm_state *sm;
2387 struct symbol *compare_type, *var_type;
2388 struct range_list *rl;
2389 struct range_list *limit;
2390 struct range_list *new;
2391 char *other_name;
2392 struct symbol *other_sym;
2394 while (expr->type == EXPR_ASSIGNMENT)
2395 expr = strip_expr(expr->right);
2396 if (expr->type != EXPR_CALL)
2397 return;
2399 arg = get_argument_from_call_expr(expr->args, param);
2400 if (!arg)
2401 return;
2403 name = get_chunk_from_key(arg, key, &sym, &vsl);
2404 if (!name)
2405 return;
2406 if (op != PARAM_LIMIT && !sym)
2407 goto free;
2409 if (strcmp(key, "$") == 0)
2410 compare_type = get_arg_type(expr->fn, param);
2411 else
2412 compare_type = get_member_type_from_key(arg, key);
2414 sm = get_sm_state(SMATCH_EXTRA, name, sym);
2415 if (sm)
2416 rl = estate_rl(sm->state);
2417 else
2418 rl = alloc_whole_rl(compare_type);
2420 if (op == PARAM_LIMIT && !rl_fits_in_type(rl, compare_type))
2421 goto free;
2423 call_results_to_rl(expr, compare_type, value, &limit);
2424 new = rl_intersection(rl, limit);
2426 var_type = get_member_type_from_key(arg, key);
2427 new = cast_rl(var_type, new);
2429 /* We want to preserve the implications here */
2430 if (sm && basically_the_same(estate_rl(sm->state), new))
2431 goto free;
2432 other_name = map_long_to_short_name_sym(name, sym, &other_sym);
2434 if (op == PARAM_LIMIT)
2435 set_extra_nomod_vsl(name, sym, vsl, NULL, alloc_estate_rl(new));
2436 else
2437 set_extra_mod(name, sym, NULL, alloc_estate_rl(new));
2439 if (other_name && other_sym) {
2440 if (op == PARAM_LIMIT)
2441 set_extra_nomod_vsl(other_name, other_sym, vsl, NULL, alloc_estate_rl(new));
2442 else
2443 set_extra_mod(other_name, other_sym, NULL, alloc_estate_rl(new));
2446 if (op == PARAM_LIMIT && arg->type == EXPR_BINOP)
2447 db_param_limit_binops(arg, key, new);
2448 free:
2449 free_string(name);
2452 static void db_param_limit(struct expression *expr, int param, char *key, char *value)
2454 db_param_limit_filter(expr, param, key, value, PARAM_LIMIT);
2457 static void db_param_filter(struct expression *expr, int param, char *key, char *value)
2459 db_param_limit_filter(expr, param, key, value, PARAM_FILTER);
2462 static void db_param_add_set(struct expression *expr, int param, char *key, char *value, enum info_type op)
2464 struct expression *arg;
2465 char *name, *tmp_name;
2466 struct symbol *sym, *tmp_sym;
2467 struct symbol *param_type, *arg_type;
2468 struct smatch_state *state;
2469 struct range_list *new = NULL;
2470 struct range_list *added = NULL;
2472 while (expr->type == EXPR_ASSIGNMENT)
2473 expr = strip_expr(expr->right);
2474 if (expr->type != EXPR_CALL)
2475 return;
2477 arg = get_argument_from_call_expr(expr->args, param);
2478 if (!arg)
2479 return;
2481 arg_type = get_arg_type_from_key(expr->fn, param, arg, key);
2482 param_type = get_member_type_from_key(arg, key);
2483 name = get_variable_from_key(arg, key, &sym);
2484 if (!name || !sym)
2485 goto free;
2487 state = get_state(SMATCH_EXTRA, name, sym);
2488 if (state)
2489 new = estate_rl(state);
2491 call_results_to_rl(expr, arg_type, value, &added);
2492 added = cast_rl(param_type, added);
2493 if (op == PARAM_SET)
2494 new = added;
2495 else
2496 new = rl_union(new, added);
2498 tmp_name = map_long_to_short_name_sym_nostack(name, sym, &tmp_sym);
2499 if (tmp_name && tmp_sym) {
2500 free_string(name);
2501 name = tmp_name;
2502 sym = tmp_sym;
2504 set_extra_mod(name, sym, NULL, alloc_estate_rl(new));
2505 free:
2506 free_string(name);
2509 static void db_param_add(struct expression *expr, int param, char *key, char *value)
2511 in_param_set = true;
2512 db_param_add_set(expr, param, key, value, PARAM_ADD);
2513 in_param_set = false;
2516 static void db_param_set(struct expression *expr, int param, char *key, char *value)
2518 in_param_set = true;
2519 db_param_add_set(expr, param, key, value, PARAM_SET);
2520 in_param_set = false;
2523 static void match_lost_param(struct expression *call, int param)
2525 struct expression *arg;
2527 if (is_const_param(call->fn, param))
2528 return;
2530 arg = get_argument_from_call_expr(call->args, param);
2531 if (!arg)
2532 return;
2534 arg = strip_expr(arg);
2535 if (arg->type == EXPR_PREOP && arg->op == '&')
2536 set_extra_expr_mod(arg->unop, alloc_estate_whole(get_type(arg->unop)));
2537 else
2538 ; /* if pointer then set struct members, maybe?*/
2541 static void db_param_value(struct expression *expr, int param, char *key, char *value)
2543 struct expression *call;
2544 char *name;
2545 struct symbol *sym;
2546 struct symbol *type;
2547 struct range_list *rl = NULL;
2549 if (param != -1)
2550 return;
2552 call = expr;
2553 while (call->type == EXPR_ASSIGNMENT)
2554 call = strip_expr(call->right);
2555 if (call->type != EXPR_CALL)
2556 return;
2558 type = get_member_type_from_key(expr->left, key);
2559 name = get_variable_from_key(expr->left, key, &sym);
2560 if (!name || !sym)
2561 goto free;
2563 call_results_to_rl(call, type, value, &rl);
2565 set_extra_mod(name, sym, NULL, alloc_estate_rl(rl));
2566 free:
2567 free_string(name);
2570 static void match_call_info(struct expression *expr)
2572 struct smatch_state *state;
2573 struct range_list *rl = NULL;
2574 struct expression *arg;
2575 struct symbol *type;
2576 int i = 0;
2578 FOR_EACH_PTR(expr->args, arg) {
2579 type = get_arg_type(expr->fn, i);
2581 get_absolute_rl(arg, &rl);
2582 rl = cast_rl(type, rl);
2584 if (!is_whole_rl(rl)) {
2585 rl = intersect_with_real_abs_expr(arg, rl);
2586 sql_insert_caller_info(expr, PARAM_VALUE, i, "$", show_rl(rl));
2588 state = get_state_expr(SMATCH_EXTRA, arg);
2589 if (estate_has_hard_max(state)) {
2590 sql_insert_caller_info(expr, HARD_MAX, i, "$",
2591 sval_to_str(estate_max(state)));
2593 if (estate_has_fuzzy_max(state)) {
2594 sql_insert_caller_info(expr, FUZZY_MAX, i, "$",
2595 sval_to_str(estate_get_fuzzy_max(state)));
2597 i++;
2598 } END_FOR_EACH_PTR(arg);
2601 static void set_param_value(const char *name, struct symbol *sym, char *key, char *value)
2603 struct range_list *rl = NULL;
2604 struct smatch_state *state;
2605 struct symbol *type;
2606 char fullname[256];
2607 sval_t dummy;
2609 if (strcmp(key, "*$") == 0)
2610 snprintf(fullname, sizeof(fullname), "*%s", name);
2611 else if (strncmp(key, "$", 1) == 0)
2612 snprintf(fullname, 256, "%s%s", name, key + 1);
2613 else
2614 return;
2616 type = get_member_type_from_key(symbol_expression(sym), key);
2617 str_to_rl(type, value, &rl);
2618 state = alloc_estate_rl(rl);
2619 if (estate_get_single_value(state, &dummy))
2620 estate_set_hard_max(state);
2621 set_state(SMATCH_EXTRA, fullname, sym, state);
2624 static void set_param_fuzzy_max(const char *name, struct symbol *sym, char *key, char *value)
2626 struct range_list *rl = NULL;
2627 struct smatch_state *state;
2628 struct symbol *type;
2629 char fullname[256];
2630 sval_t max;
2632 if (strcmp(key, "*$") == 0)
2633 snprintf(fullname, sizeof(fullname), "*%s", name);
2634 else if (strncmp(key, "$", 1) == 0)
2635 snprintf(fullname, 256, "%s%s", name, key + 1);
2636 else
2637 return;
2639 state = get_state(SMATCH_EXTRA, fullname, sym);
2640 if (!state)
2641 return;
2642 type = estate_type(state);
2643 str_to_rl(type, value, &rl);
2644 if (!rl_to_sval(rl, &max))
2645 return;
2646 estate_set_fuzzy_max(state, max);
2649 static void set_param_hard_max(const char *name, struct symbol *sym, char *key, char *value)
2651 struct smatch_state *state;
2652 char fullname[256];
2654 if (strcmp(key, "*$") == 0)
2655 snprintf(fullname, sizeof(fullname), "*%s", name);
2656 else if (strncmp(key, "$", 1) == 0)
2657 snprintf(fullname, 256, "%s%s", name, key + 1);
2658 else
2659 return;
2661 state = get_state(SMATCH_EXTRA, fullname, sym);
2662 if (!state)
2663 return;
2664 estate_set_hard_max(state);
2667 struct sm_state *get_extra_sm_state(struct expression *expr)
2669 char *name;
2670 struct symbol *sym;
2671 struct sm_state *ret = NULL;
2673 name = expr_to_known_chunk_sym(expr, &sym);
2674 if (!name)
2675 goto free;
2677 ret = get_sm_state(SMATCH_EXTRA, name, sym);
2678 free:
2679 free_string(name);
2680 return ret;
2683 struct smatch_state *get_extra_state(struct expression *expr)
2685 struct sm_state *sm;
2687 sm = get_extra_sm_state(expr);
2688 if (!sm)
2689 return NULL;
2690 return sm->state;
2693 void register_smatch_extra(int id)
2695 my_id = id;
2697 add_merge_hook(my_id, &merge_estates);
2698 add_unmatched_state_hook(my_id, &unmatched_state);
2699 select_caller_info_hook(set_param_value, PARAM_VALUE);
2700 select_caller_info_hook(set_param_fuzzy_max, FUZZY_MAX);
2701 select_caller_info_hook(set_param_hard_max, HARD_MAX);
2702 select_return_states_before(&db_limited_before);
2703 select_return_states_hook(PARAM_LIMIT, &db_param_limit);
2704 select_return_states_hook(PARAM_FILTER, &db_param_filter);
2705 select_return_states_hook(PARAM_ADD, &db_param_add);
2706 select_return_states_hook(PARAM_SET, &db_param_set);
2707 add_lost_param_hook(&match_lost_param);
2708 select_return_states_hook(PARAM_VALUE, &db_param_value);
2709 select_return_states_after(&db_limited_after);
2712 static void match_link_modify(struct sm_state *sm, struct expression *mod_expr)
2714 struct var_sym_list *links;
2715 struct var_sym *tmp;
2716 struct smatch_state *state;
2718 links = sm->state->data;
2720 FOR_EACH_PTR(links, tmp) {
2721 if (sm->sym == tmp->sym &&
2722 strcmp(sm->name, tmp->var) == 0)
2723 continue;
2724 state = get_state(SMATCH_EXTRA, tmp->var, tmp->sym);
2725 if (!state)
2726 continue;
2727 set_state(SMATCH_EXTRA, tmp->var, tmp->sym, alloc_estate_whole(estate_type(state)));
2728 } END_FOR_EACH_PTR(tmp);
2729 set_state(link_id, sm->name, sm->sym, &undefined);
2732 void register_smatch_extra_links(int id)
2734 link_id = id;
2737 void register_smatch_extra_late(int id)
2739 add_merge_hook(link_id, &merge_link_states);
2740 add_modification_hook(link_id, &match_link_modify);
2741 add_hook(&match_dereferences, DEREF_HOOK);
2742 add_hook(&match_pointer_as_array, OP_HOOK);
2743 select_return_implies_hook(DEREFERENCE, &set_param_dereferenced);
2744 add_hook(&match_function_call, FUNCTION_CALL_HOOK);
2745 add_hook(&match_assign, ASSIGNMENT_HOOK);
2746 add_hook(&match_assign, GLOBAL_ASSIGNMENT_HOOK);
2747 add_hook(&unop_expr, OP_HOOK);
2748 add_hook(&asm_expr, ASM_HOOK);
2750 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
2751 add_member_info_callback(my_id, struct_member_callback);
2752 add_split_return_callback(&returned_struct_members);
2754 // add_hook(&assume_indexes_are_valid, OP_HOOK);