flow: strip out parentheses before faking a return
[smatch.git] / smatch_extra.c
blobbb13ca6389a8b71e7aaa429c7757a362cd2a63b3
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(mod_hook_list, mod_hook *);
65 static struct mod_hook_list *extra_mod_hooks;
66 static struct mod_hook_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 mod_hook_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 void set_union_info(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
103 struct symbol *type, *tmp, *inner_type, *inner, *new_type;
104 struct expression *deref, *member_expr;
105 struct smatch_state *new;
106 int offset, inner_offset;
107 static bool in_recurse;
108 char *member_name;
110 if (__in_fake_assign)
111 return;
113 if (in_recurse)
114 return;
115 in_recurse = true;
117 if (!expr || expr->type != EXPR_DEREF || !expr->member)
118 goto done;
119 offset = get_member_offset_from_deref(expr);
120 if (offset < 0)
121 goto done;
123 deref = strip_expr(expr->deref);
124 type = get_type(deref);
125 if (type_is_ptr(type))
126 type = get_real_base_type(type);
127 if (!type || type->type != SYM_STRUCT)
128 goto done;
130 FOR_EACH_PTR(type->symbol_list, tmp) {
131 inner_type = get_real_base_type(tmp);
132 if (!inner_type || inner_type->type != SYM_UNION)
133 continue;
135 inner = first_ptr_list((struct ptr_list *)inner_type->symbol_list);
136 if (!inner || !inner->ident)
137 continue;
139 inner_offset = get_member_offset(type, inner->ident->name);
140 if (inner_offset < offset)
141 continue;
142 if (inner_offset > offset)
143 goto done;
145 FOR_EACH_PTR(inner_type->symbol_list, inner) {
146 struct symbol *tmp_type;
148 if (!inner->ident || inner->ident == expr->member)
149 continue;
150 tmp_type = get_real_base_type(inner);
151 if (tmp_type && tmp_type->type == SYM_STRUCT)
152 continue;
153 member_expr = deref;
154 if (tmp->ident)
155 member_expr = member_expression(member_expr, '.', tmp->ident);
156 member_expr = member_expression(member_expr, expr->op, inner->ident);
157 member_name = expr_to_var(member_expr);
158 if (!member_name)
159 continue;
160 new_type = get_real_base_type(inner);
161 new = alloc_estate_rl(cast_rl(new_type, estate_rl(state)));
162 set_extra_mod_helper(member_name, sym, member_expr, new);
163 free_string(member_name);
164 } END_FOR_EACH_PTR(inner);
165 } END_FOR_EACH_PTR(tmp);
167 done:
168 in_recurse = false;
171 static void mark_sub_members_gone(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
173 struct sm_state *sm;
175 if (__in_fake_assign)
176 return;
177 if (is_fake_var_assign(expr))
178 return;
180 if (!estate_type(state) || estate_type(state)->type != SYM_PTR)
181 return;
182 if (!is_noderef_ptr_rl(estate_rl(state)))
183 return;
185 FOR_EACH_MY_SM(SMATCH_EXTRA, __get_cur_stree(), sm) {
186 if (sm->sym != sym)
187 continue;
188 if (strcmp(name, sm->name) == 0)
189 continue;
190 if (!is_sub_member(name, sym, sm))
191 continue;
192 set_extra_nomod(sm->name, sm->sym, NULL, alloc_estate_empty());
193 } END_FOR_EACH_SM(sm);
196 static bool is_fake_assign(struct expression *expr)
198 struct expression *faked;
200 if (is_fake_var_assign(expr))
201 return true;
203 faked = get_faked_expression();
204 if (!faked || faked->type != EXPR_ASSIGNMENT || faked->op != '=')
205 return false;
207 faked = strip_expr(faked->right);
208 if (faked->type == EXPR_PREOP && faked->op == '&')
209 return true;
211 return false;
214 static void call_update_mtag_data(struct expression *expr,
215 struct smatch_state *state)
217 if (is_fake_assign(expr))
218 return;
220 update_mtag_data(expr, state);
223 static bool in_param_set;
224 void set_extra_mod_helper(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
226 if (!expr)
227 expr = gen_expression_from_name_sym(name, sym);
228 remove_from_equiv(name, sym);
229 set_union_info(name, sym, expr, state);
230 mark_sub_members_gone(name, sym, expr, state);
231 call_extra_mod_hooks(name, sym, expr, state);
232 call_update_mtag_data(expr, state);
233 if ((__in_fake_assign || in_param_set) &&
234 estate_is_unknown(state) && !get_state(SMATCH_EXTRA, name, sym))
235 return;
236 set_state(SMATCH_EXTRA, name, sym, state);
239 static void set_extra_nomod_helper(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
241 call_extra_nomod_hooks(name, sym, expr, state);
242 set_state(SMATCH_EXTRA, name, sym, state);
245 static char *get_pointed_at(const char *name, struct symbol *sym, struct symbol **new_sym)
247 struct expression *assigned;
250 * Imagine we have an assignment: "foo = &addr;" then the other name
251 * of "*foo" is addr.
254 if (name[0] != '*')
255 return NULL;
256 if (strcmp(name + 1, sym->ident->name) != 0)
257 return NULL;
259 assigned = get_assigned_expr_name_sym(sym->ident->name, sym);
260 if (!assigned)
261 return NULL;
262 assigned = strip_parens(assigned);
263 if (assigned->type != EXPR_PREOP || assigned->op != '&')
264 return NULL;
266 return expr_to_var_sym(assigned->unop, new_sym);
269 char *get_other_name_sym_from_chunk(const char *name, const char *chunk, int len, struct symbol *sym, struct symbol **new_sym)
271 struct expression *assigned;
272 char *orig_name = NULL;
273 char buf[256];
274 char *ret;
276 assigned = get_assigned_expr_name_sym(chunk, sym);
277 if (!assigned)
278 return NULL;
279 if (assigned->type == EXPR_CALL)
280 return map_call_to_other_name_sym(name, sym, new_sym);
281 if (assigned->type == EXPR_PREOP && assigned->op == '&') {
283 orig_name = expr_to_var_sym(assigned, new_sym);
284 if (!orig_name || !*new_sym)
285 goto free;
287 snprintf(buf, sizeof(buf), "%s.%s", orig_name + 1, name + len);
288 ret = alloc_string(buf);
289 free_string(orig_name);
290 return ret;
293 orig_name = expr_to_var_sym(assigned, new_sym);
294 if (!orig_name || !*new_sym)
295 goto free;
297 snprintf(buf, sizeof(buf), "%s->%s", orig_name, name + len);
298 ret = alloc_string(buf);
299 free_string(orig_name);
300 return ret;
301 free:
302 free_string(orig_name);
303 return NULL;
306 static char *get_long_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym, bool use_stack)
308 struct expression *orig;
309 struct symbol *orig_sym;
310 char *orig_name, *ret;
312 if (!sym || !sym->ident)
313 return NULL;
315 orig = get_assigned_expr_name_sym(sym->ident->name, sym);
316 if (orig) {
317 orig_name = expr_to_var_sym(orig, &orig_sym);
318 if (!orig_name)
319 return NULL;
321 ret = swap_names(name, sym->ident->name, orig_name);
322 free_string(orig_name);
323 if (ret)
324 *new_sym = orig_sym;
325 return ret;
328 return NULL;
331 char *get_other_name_sym_helper(const char *name, struct symbol *sym, struct symbol **new_sym, bool use_stack)
333 char buf[256];
334 char *ret;
335 int len;
337 *new_sym = NULL;
339 if (!sym || !sym->ident)
340 return NULL;
342 ret = get_pointed_at(name, sym, new_sym);
343 if (ret)
344 return ret;
346 ret = map_long_to_short_name_sym(name, sym, new_sym, use_stack);
347 if (ret)
348 return ret;
350 len = snprintf(buf, sizeof(buf), "%s", name);
351 if (len >= sizeof(buf) - 2)
352 return NULL;
354 while (use_stack && len >= 1) {
355 if (buf[len] == '>' && buf[len - 1] == '-') {
356 len--;
357 buf[len] = '\0';
358 ret = get_other_name_sym_from_chunk(name, buf, len + 2, sym, new_sym);
359 if (ret)
360 return ret;
362 len--;
365 ret = get_long_name_sym(name, sym, new_sym, use_stack);
366 if (ret)
367 return ret;
369 return NULL;
372 char *get_other_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym)
374 return get_other_name_sym_helper(name, sym, new_sym, true);
377 char *get_other_name_sym_nostack(const char *name, struct symbol *sym, struct symbol **new_sym)
379 return get_other_name_sym_helper(name, sym, new_sym, false);
382 void set_extra_mod(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
384 char *new_name;
385 struct symbol *new_sym;
387 set_extra_mod_helper(name, sym, expr, state);
388 new_name = get_other_name_sym_nostack(name, sym, &new_sym);
389 if (new_name && new_sym)
390 set_extra_mod_helper(new_name, new_sym, NULL, state);
391 free_string(new_name);
394 static struct expression *chunk_get_array_base(struct expression *expr)
397 * The problem with is_array() is that it only returns true for things
398 * like foo[1] but not for foo[1].bar.
401 expr = strip_expr(expr);
402 while (expr && expr->type == EXPR_DEREF)
403 expr = strip_expr(expr->deref);
404 return get_array_base(expr);
407 static int chunk_has_array(struct expression *expr)
409 return !!chunk_get_array_base(expr);
412 static void clear_array_states(struct expression *array)
414 struct sm_state *sm;
416 sm = get_sm_state_expr(link_id, array);
417 if (sm)
418 match_link_modify(sm, NULL);
421 static void set_extra_array_mod(struct expression *expr, struct smatch_state *state)
423 struct expression *array;
424 struct var_sym_list *vsl;
425 struct var_sym *vs;
426 char *name;
427 struct symbol *sym;
429 array = chunk_get_array_base(expr);
431 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
432 if (!name || !vsl) {
433 clear_array_states(array);
434 goto free;
437 FOR_EACH_PTR(vsl, vs) {
438 store_link(link_id, vs->var, vs->sym, name, sym);
439 } END_FOR_EACH_PTR(vs);
441 call_extra_mod_hooks(name, sym, expr, state);
442 set_state(SMATCH_EXTRA, name, sym, state);
443 free:
444 free_string(name);
447 void set_extra_expr_mod(struct expression *expr, struct smatch_state *state)
449 struct symbol *sym;
450 char *name;
452 if (chunk_has_array(expr)) {
453 set_extra_array_mod(expr, state);
454 return;
457 expr = strip_expr(expr);
458 name = expr_to_var_sym(expr, &sym);
459 if (!name || !sym)
460 goto free;
461 set_extra_mod(name, sym, expr, state);
462 free:
463 free_string(name);
466 void set_extra_nomod(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
468 char *new_name;
469 struct symbol *new_sym;
470 struct relation *rel;
471 struct smatch_state *orig_state;
473 orig_state = get_state(SMATCH_EXTRA, name, sym);
475 /* don't save unknown states if leaving it blank is the same */
476 if (!orig_state && estate_is_unknown(state))
477 return;
479 new_name = get_other_name_sym(name, sym, &new_sym);
480 if (new_name && new_sym)
481 set_extra_nomod_helper(new_name, new_sym, expr, state);
482 free_string(new_name);
484 if (!estate_related(orig_state)) {
485 set_extra_nomod_helper(name, sym, expr, state);
486 return;
489 set_related(state, estate_related(orig_state));
490 FOR_EACH_PTR(estate_related(orig_state), rel) {
491 struct smatch_state *estate;
493 estate = get_state(SMATCH_EXTRA, rel->name, rel->sym);
494 if (!estate)
495 continue;
496 set_extra_nomod_helper(rel->name, rel->sym, expr, clone_estate_cast(estate_type(estate), state));
497 } END_FOR_EACH_PTR(rel);
500 void set_extra_nomod_vsl(const char *name, struct symbol *sym, struct var_sym_list *vsl, struct expression *expr, struct smatch_state *state)
502 struct var_sym *vs;
504 FOR_EACH_PTR(vsl, vs) {
505 store_link(link_id, vs->var, vs->sym, name, sym);
506 } END_FOR_EACH_PTR(vs);
508 set_extra_nomod(name, sym, expr, state);
512 * This is for return_implies_state() hooks which modify a SMATCH_EXTRA state
514 void set_extra_expr_nomod(struct expression *expr, struct smatch_state *state)
516 struct var_sym_list *vsl;
517 struct var_sym *vs;
518 char *name;
519 struct symbol *sym;
521 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
522 if (!name || !vsl)
523 goto free;
524 FOR_EACH_PTR(vsl, vs) {
525 store_link(link_id, vs->var, vs->sym, name, sym);
526 } END_FOR_EACH_PTR(vs);
528 set_extra_nomod(name, sym, expr, state);
529 free:
530 free_string(name);
533 static void set_extra_true_false(const char *name, struct symbol *sym,
534 struct smatch_state *true_state,
535 struct smatch_state *false_state)
537 char *new_name;
538 struct symbol *new_sym;
539 struct relation *rel;
540 struct smatch_state *orig_state;
542 if (!true_state && !false_state)
543 return;
545 if (in_warn_on_macro())
546 return;
548 new_name = get_other_name_sym(name, sym, &new_sym);
549 if (new_name && new_sym)
550 set_true_false_states(SMATCH_EXTRA, new_name, new_sym, true_state, false_state);
551 free_string(new_name);
553 orig_state = get_state(SMATCH_EXTRA, name, sym);
555 if (!estate_related(orig_state)) {
556 set_true_false_states(SMATCH_EXTRA, name, sym, true_state, false_state);
557 return;
560 if (true_state)
561 set_related(true_state, estate_related(orig_state));
562 if (false_state)
563 set_related(false_state, estate_related(orig_state));
565 FOR_EACH_PTR(estate_related(orig_state), rel) {
566 set_true_false_states(SMATCH_EXTRA, rel->name, rel->sym,
567 true_state, false_state);
568 } END_FOR_EACH_PTR(rel);
571 static void set_extra_chunk_true_false(struct expression *expr,
572 struct smatch_state *true_state,
573 struct smatch_state *false_state)
575 struct var_sym_list *vsl;
576 struct var_sym *vs;
577 struct symbol *type;
578 char *name;
579 struct symbol *sym;
581 if (in_warn_on_macro())
582 return;
584 type = get_type(expr);
585 if (!type)
586 return;
588 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
589 if (!name || !vsl)
590 goto free;
591 FOR_EACH_PTR(vsl, vs) {
592 store_link(link_id, vs->var, vs->sym, name, sym);
593 } END_FOR_EACH_PTR(vs);
595 set_true_false_states(SMATCH_EXTRA, name, sym,
596 clone_estate(true_state),
597 clone_estate(false_state));
598 free:
599 free_string(name);
602 static void set_extra_expr_true_false(struct expression *expr,
603 struct smatch_state *true_state,
604 struct smatch_state *false_state)
606 char *name;
607 struct symbol *sym;
608 sval_t sval;
610 if (!true_state && !false_state)
611 return;
613 if (get_value(expr, &sval))
614 return;
616 expr = strip_expr(expr);
617 name = expr_to_var_sym(expr, &sym);
618 if (!name || !sym) {
619 free_string(name);
620 set_extra_chunk_true_false(expr, true_state, false_state);
621 return;
623 set_extra_true_false(name, sym, true_state, false_state);
624 free_string(name);
627 static int get_countdown_info(struct expression *condition, struct expression **unop, int *op, sval_t *right)
629 struct expression *unop_expr;
630 int comparison;
631 sval_t limit;
633 right->type = &int_ctype;
634 right->value = 0;
636 condition = strip_expr(condition);
638 if (condition->type == EXPR_COMPARE) {
639 comparison = remove_unsigned_from_comparison(condition->op);
641 if (comparison != SPECIAL_GTE && comparison != '>')
642 return 0;
643 if (!get_value(condition->right, &limit))
644 return 0;
646 unop_expr = condition->left;
647 if (unop_expr->type != EXPR_PREOP && unop_expr->type != EXPR_POSTOP)
648 return 0;
649 if (unop_expr->op != SPECIAL_DECREMENT)
650 return 0;
652 *unop = unop_expr;
653 *op = comparison;
654 *right = limit;
656 return 1;
659 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
660 return 0;
661 if (condition->op != SPECIAL_DECREMENT)
662 return 0;
664 *unop = condition;
665 *op = '>';
667 return 1;
670 static struct sm_state *handle_canonical_while_count_down(struct statement *loop)
672 struct expression *iter_var;
673 struct expression *condition, *unop;
674 struct symbol *type;
675 struct sm_state *sm;
676 struct smatch_state *estate;
677 int op;
678 sval_t start, right;
680 right.type = &int_ctype;
681 right.value = 0;
683 condition = strip_expr(loop->iterator_pre_condition);
684 if (!condition)
685 return NULL;
687 if (!get_countdown_info(condition, &unop, &op, &right))
688 return NULL;
690 iter_var = unop->unop;
692 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
693 if (!sm)
694 return NULL;
695 if (sval_cmp(estate_min(sm->state), right) < 0)
696 return NULL;
697 start = estate_max(sm->state);
699 type = get_type(iter_var);
700 right = sval_cast(type, right);
701 start = sval_cast(type, start);
703 if (sval_cmp(start, right) <= 0)
704 return NULL;
705 if (!sval_is_max(start))
706 start.value--;
708 if (op == SPECIAL_GTE)
709 right.value--;
711 if (unop->type == EXPR_PREOP) {
712 right.value++;
713 estate = alloc_estate_range(right, start);
714 if (estate_has_hard_max(sm->state))
715 estate_set_hard_max(estate);
716 estate_copy_fuzzy_max(estate, sm->state);
717 set_extra_expr_mod(iter_var, estate);
719 if (unop->type == EXPR_POSTOP) {
720 estate = alloc_estate_range(right, start);
721 if (estate_has_hard_max(sm->state))
722 estate_set_hard_max(estate);
723 estate_copy_fuzzy_max(estate, sm->state);
724 set_extra_expr_mod(iter_var, estate);
726 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
729 static struct sm_state *handle_canonical_for_inc(struct expression *iter_expr,
730 struct expression *condition)
732 struct expression *iter_var;
733 struct sm_state *sm;
734 struct smatch_state *estate;
735 sval_t start, end, max;
736 bool unknown_end = false;
738 iter_var = iter_expr->unop;
739 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
740 if (!sm)
741 return NULL;
742 if (!estate_get_single_value(sm->state, &start))
743 return NULL;
744 if (!get_implied_max(condition->right, &end)) {
745 end = sval_type_max(get_type(condition->right));
746 end = sval_cast(start.type, end);
747 if (sval_is_max(end))
748 unknown_end = true;
751 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
752 return NULL;
754 switch (condition->op) {
755 case SPECIAL_UNSIGNED_LT:
756 case SPECIAL_NOTEQUAL:
757 case '<':
758 if (!sval_is_min(end) && !unknown_end)
759 end.value--;
760 break;
761 case SPECIAL_UNSIGNED_LTE:
762 case SPECIAL_LTE:
763 break;
764 default:
765 return NULL;
767 if (sval_cmp(end, start) < 0)
768 return NULL;
769 end = sval_cast(start.type, end);
770 estate = alloc_estate_range(start, end);
771 if (get_hard_max(condition->right, &max)) {
772 if (!get_macro_name(condition->pos))
773 estate_set_hard_max(estate);
774 if (condition->op == '<' ||
775 condition->op == SPECIAL_UNSIGNED_LT ||
776 condition->op == SPECIAL_NOTEQUAL)
777 max.value--;
778 max = sval_cast(start.type, max);
779 estate_set_fuzzy_max(estate, max);
781 set_extra_expr_mod(iter_var, estate);
782 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
785 static struct sm_state *handle_canonical_for_dec(struct expression *iter_expr,
786 struct expression *condition)
788 struct expression *iter_var;
789 struct sm_state *sm;
790 struct smatch_state *estate;
791 sval_t start, end;
793 iter_var = iter_expr->unop;
794 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
795 if (!sm)
796 return NULL;
797 if (!estate_get_single_value(sm->state, &start))
798 return NULL;
799 if (!get_implied_min(condition->right, &end))
800 end = sval_type_min(get_type(iter_var));
801 end = sval_cast(estate_type(sm->state), end);
802 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
803 return NULL;
805 switch (condition->op) {
806 case SPECIAL_NOTEQUAL:
807 case '>':
808 if (!sval_is_max(end))
809 end.value++;
810 break;
811 case SPECIAL_GTE:
812 break;
813 default:
814 return NULL;
816 if (sval_cmp(end, start) > 0)
817 return NULL;
818 estate = alloc_estate_range(end, start);
819 estate_set_hard_max(estate);
820 estate_set_fuzzy_max(estate, estate_get_fuzzy_max(estate));
821 set_extra_expr_mod(iter_var, estate);
822 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
825 static struct sm_state *handle_canonical_for_loops(struct statement *loop)
827 struct expression *iter_expr;
828 struct expression *condition;
830 if (!loop->iterator_post_statement)
831 return NULL;
832 if (loop->iterator_post_statement->type != STMT_EXPRESSION)
833 return NULL;
834 iter_expr = loop->iterator_post_statement->expression;
835 if (!loop->iterator_pre_condition)
836 return NULL;
837 if (loop->iterator_pre_condition->type != EXPR_COMPARE)
838 return NULL;
839 condition = loop->iterator_pre_condition;
841 if (iter_expr->op == SPECIAL_INCREMENT)
842 return handle_canonical_for_inc(iter_expr, condition);
843 if (iter_expr->op == SPECIAL_DECREMENT)
844 return handle_canonical_for_dec(iter_expr, condition);
845 return NULL;
848 struct sm_state *__extra_handle_canonical_loops(struct statement *loop, struct stree **stree)
850 struct sm_state *ret;
853 * Canonical loops are a hack. The proper way to handle this is to
854 * use two passes, but unfortunately, doing two passes makes parsing
855 * code twice as slow.
857 * What we do is we set the inside state here, which overwrites whatever
858 * __extra_match_condition() does. Then we set the outside state in
859 * __extra_pre_loop_hook_after().
862 __push_fake_cur_stree();
863 if (!loop->iterator_post_statement)
864 ret = handle_canonical_while_count_down(loop);
865 else
866 ret = handle_canonical_for_loops(loop);
867 *stree = __pop_fake_cur_stree();
868 return ret;
871 int __iterator_unchanged(struct sm_state *sm)
873 if (!sm)
874 return 0;
875 if (get_sm_state(my_id, sm->name, sm->sym) == sm)
876 return 1;
877 return 0;
880 static void while_count_down_after(struct sm_state *sm, struct expression *condition)
882 struct expression *unop;
883 int op;
884 sval_t limit, after_value;
886 if (!get_countdown_info(condition, &unop, &op, &limit))
887 return;
888 after_value = estate_min(sm->state);
889 after_value.value--;
890 set_extra_mod(sm->name, sm->sym, condition->unop, alloc_estate_sval(after_value));
893 void __extra_pre_loop_hook_after(struct sm_state *sm,
894 struct statement *iterator,
895 struct expression *condition)
897 struct expression *iter_expr;
898 sval_t limit;
899 struct smatch_state *state;
900 sval_t end;
902 if (!iterator) {
903 while_count_down_after(sm, condition);
904 return;
907 iter_expr = iterator->expression;
909 if (condition->type != EXPR_COMPARE)
910 return;
912 if (iter_expr->op == SPECIAL_INCREMENT) {
913 if (!get_implied_value(condition->right, &end) &&
914 sval_is_max(estate_max(sm->state)))
915 limit = estate_max(sm->state);
916 else
917 limit = sval_binop(estate_max(sm->state), '+',
918 sval_type_val(estate_type(sm->state), 1));
919 } else {
920 limit = sval_binop(estate_min(sm->state), '-',
921 sval_type_val(estate_type(sm->state), 1));
923 limit = sval_cast(estate_type(sm->state), limit);
924 if (!estate_has_hard_max(sm->state) && !__has_breaks()) {
925 if (iter_expr->op == SPECIAL_INCREMENT)
926 state = alloc_estate_range(estate_min(sm->state), limit);
927 else
928 state = alloc_estate_range(limit, estate_max(sm->state));
929 } else {
930 state = alloc_estate_sval(limit);
932 if (!estate_has_hard_max(sm->state)) {
933 estate_clear_hard_max(state);
935 if (estate_has_fuzzy_max(sm->state)) {
936 sval_t hmax = estate_get_fuzzy_max(sm->state);
937 sval_t max = estate_max(sm->state);
939 if (sval_cmp(hmax, max) != 0)
940 estate_clear_fuzzy_max(state);
941 } else if (!estate_has_fuzzy_max(sm->state)) {
942 estate_clear_fuzzy_max(state);
945 set_extra_mod(sm->name, sm->sym, iter_expr, state);
948 static bool get_global_rl(const char *name, struct symbol *sym, struct range_list **rl)
950 struct expression *expr;
952 if (!sym || !(sym->ctype.modifiers & MOD_TOPLEVEL) || !sym->ident)
953 return false;
954 if (strcmp(sym->ident->name, name) != 0)
955 return false;
957 expr = symbol_expression(sym);
958 return get_implied_rl(expr, rl);
961 static struct stree *unmatched_stree;
962 static struct smatch_state *unmatched_state(struct sm_state *sm)
964 struct smatch_state *state;
965 struct expression *expr;
966 struct range_list *rl;
968 if (unmatched_stree) {
969 state = get_state_stree(unmatched_stree, SMATCH_EXTRA, sm->name, sm->sym);
970 if (state)
971 return state;
973 if (parent_is_gone_var_sym(sm->name, sm->sym))
974 return alloc_estate_empty();
975 if (get_global_rl(sm->name, sm->sym, &rl))
976 return alloc_estate_rl(rl);
978 expr = gen_expression_from_name_sym(sm->name, sm->sym);
979 if (!expr)
980 return alloc_estate_whole(estate_type(sm->state));
981 get_absolute_rl(expr, &rl);
982 return alloc_estate_rl(rl);
985 static void clear_the_pointed_at(struct expression *expr)
987 struct stree *stree;
988 char *name;
989 struct symbol *sym;
990 struct sm_state *tmp;
992 name = expr_to_var_sym(expr, &sym);
993 if (!name || !sym)
994 goto free;
996 stree = __get_cur_stree();
997 FOR_EACH_MY_SM(SMATCH_EXTRA, stree, tmp) {
998 if (tmp->name[0] != '*')
999 continue;
1000 if (tmp->sym != sym)
1001 continue;
1002 if (strcmp(tmp->name + 1, name) != 0)
1003 continue;
1004 set_extra_mod(tmp->name, tmp->sym, expr, alloc_estate_whole(estate_type(tmp->state)));
1005 } END_FOR_EACH_SM(tmp);
1007 free:
1008 free_string(name);
1011 static int is_const_param(struct expression *expr, int param)
1013 struct symbol *type;
1015 type = get_arg_type(expr, param);
1016 if (!type)
1017 return 0;
1018 if (type->ctype.modifiers & MOD_CONST)
1019 return 1;
1020 return 0;
1023 static void match_function_call(struct expression *expr)
1025 struct expression *arg;
1026 struct expression *tmp;
1027 int param = -1;
1029 /* if we have the db this is handled in smatch_function_hooks.c */
1030 if (!option_no_db)
1031 return;
1032 if (inlinable(expr->fn))
1033 return;
1035 FOR_EACH_PTR(expr->args, arg) {
1036 param++;
1037 if (is_const_param(expr->fn, param))
1038 continue;
1039 tmp = strip_expr(arg);
1040 if (tmp->type == EXPR_PREOP && tmp->op == '&')
1041 set_extra_expr_mod(tmp->unop, alloc_estate_whole(get_type(tmp->unop)));
1042 else
1043 clear_the_pointed_at(tmp);
1044 } END_FOR_EACH_PTR(arg);
1047 int values_fit_type(struct expression *left, struct expression *right)
1049 struct range_list *rl;
1050 struct symbol *type;
1052 type = get_type(left);
1053 if (!type)
1054 return 0;
1055 get_absolute_rl(right, &rl);
1056 if (type == rl_type(rl))
1057 return 1;
1058 if (sval_is_negative(rl_min(rl))) {
1059 if (type_unsigned(type))
1060 return 0;
1061 if (sval_cmp(sval_type_min(type), rl_min(rl)) > 0)
1062 return 0;
1064 if (sval_cmp(sval_type_max(type), rl_max(rl)) < 0)
1065 return 0;
1066 return 1;
1069 static void save_chunk_info(struct expression *left, struct expression *right)
1071 struct var_sym_list *vsl;
1072 struct var_sym *vs;
1073 struct expression *add_expr;
1074 struct symbol *type;
1075 sval_t sval;
1076 char *name;
1077 struct symbol *sym;
1079 if (right->type != EXPR_BINOP || right->op != '-')
1080 return;
1081 if (!get_value(right->left, &sval))
1082 return;
1083 if (!expr_to_sym(right->right))
1084 return;
1086 add_expr = binop_expression(left, '+', right->right);
1087 type = get_type(add_expr);
1088 if (!type)
1089 return;
1090 name = expr_to_chunk_sym_vsl(add_expr, &sym, &vsl);
1091 if (!name || !vsl)
1092 goto free;
1093 FOR_EACH_PTR(vsl, vs) {
1094 store_link(link_id, vs->var, vs->sym, name, sym);
1095 } END_FOR_EACH_PTR(vs);
1097 set_state(SMATCH_EXTRA, name, sym, alloc_estate_sval(sval_cast(type, sval)));
1098 free:
1099 free_string(name);
1102 static void do_array_assign(struct expression *left, int op, struct expression *right)
1104 struct range_list *rl;
1106 if (op == '=') {
1107 get_absolute_rl(right, &rl);
1108 rl = cast_rl(get_type(left), rl);
1109 } else {
1110 rl = alloc_whole_rl(get_type(left));
1113 set_extra_array_mod(left, alloc_estate_rl(rl));
1116 static void match_vanilla_assign(struct expression *left, struct expression *right)
1118 struct range_list *orig_rl = NULL;
1119 struct range_list *rl = NULL;
1120 struct symbol *right_sym;
1121 struct symbol *left_type;
1122 struct symbol *right_type;
1123 char *right_name = NULL;
1124 struct symbol *sym;
1125 char *name;
1126 sval_t sval, max;
1127 struct smatch_state *state;
1128 int comparison;
1130 if (is_struct(left))
1131 return;
1133 if (expr_equiv(left, right))
1134 return;
1136 save_chunk_info(left, right);
1138 name = expr_to_var_sym(left, &sym);
1139 if (!name) {
1140 if (chunk_has_array(left))
1141 do_array_assign(left, '=', right);
1142 return;
1145 left_type = get_type(left);
1146 right_type = get_type(right);
1148 right_name = expr_to_var_sym(right, &right_sym);
1150 if (!__in_fake_assign &&
1151 !(right->type == EXPR_PREOP && right->op == '&') &&
1152 right_name && right_sym &&
1153 values_fit_type(left, strip_expr(right)) &&
1154 !has_symbol(right, sym)) {
1155 set_equiv(left, right);
1156 goto free;
1159 if (get_implied_value(right, &sval)) {
1160 state = alloc_estate_sval(sval_cast(left_type, sval));
1161 goto done;
1164 if (__in_fake_assign || is_fake_var(left)) {
1165 struct smatch_state *right_state;
1166 struct range_list *rl;
1168 right_state = get_state(SMATCH_EXTRA, right_name, right_sym);
1169 if (right_state) {
1170 state = clone_estate_cast(left_type, right_state);
1171 goto done;
1174 if (get_implied_rl(right, &rl)) {
1175 rl = cast_rl(left_type, rl);
1176 state = alloc_estate_rl(rl);
1177 goto done;
1180 rl = alloc_whole_rl(right_type);
1181 rl = cast_rl(left_type, rl);
1182 state = alloc_estate_rl(rl);
1183 goto done;
1186 comparison = get_comparison_no_extra(left, right);
1187 if (comparison) {
1188 comparison = flip_comparison(comparison);
1189 get_implied_rl(left, &orig_rl);
1192 if (get_implied_rl(right, &rl)) {
1193 rl = cast_rl(left_type, rl);
1194 if (orig_rl)
1195 filter_by_comparison(&rl, comparison, orig_rl);
1196 state = alloc_estate_rl(rl);
1197 if (get_hard_max(right, &max)) {
1198 estate_set_hard_max(state);
1199 estate_set_fuzzy_max(state, max);
1201 } else {
1202 rl = alloc_whole_rl(right_type);
1203 rl = cast_rl(left_type, rl);
1204 if (orig_rl)
1205 filter_by_comparison(&rl, comparison, orig_rl);
1206 state = alloc_estate_rl(rl);
1209 done:
1210 set_extra_mod(name, sym, left, state);
1211 free:
1212 free_string(right_name);
1215 static void match_assign(struct expression *expr)
1217 struct range_list *rl = NULL;
1218 struct expression *left;
1219 struct expression *right;
1220 struct expression *binop_expr;
1221 struct symbol *left_type;
1222 struct symbol *sym;
1223 char *name;
1225 left = strip_expr(expr->left);
1227 right = strip_parens(expr->right);
1228 if (right->type == EXPR_CALL && sym_name_is("__builtin_expect", right->fn))
1229 right = get_argument_from_call_expr(right->args, 0);
1230 while (right->type == EXPR_ASSIGNMENT && right->op == '=')
1231 right = strip_parens(right->left);
1233 if (expr->op == '=' && is_condition(expr->right))
1234 return; /* handled in smatch_condition.c */
1235 if (expr->op == '=' && right->type == EXPR_CALL &&
1236 !is_fake_call(right))
1237 return; /* handled in smatch_function_hooks.c */
1238 if (expr->op == '=') {
1239 match_vanilla_assign(left, right);
1240 return;
1243 name = expr_to_var_sym(left, &sym);
1244 if (!name)
1245 return;
1247 left_type = get_type(left);
1249 switch (expr->op) {
1250 case SPECIAL_ADD_ASSIGN:
1251 case SPECIAL_SUB_ASSIGN:
1252 case SPECIAL_AND_ASSIGN:
1253 case SPECIAL_MOD_ASSIGN:
1254 case SPECIAL_SHL_ASSIGN:
1255 case SPECIAL_SHR_ASSIGN:
1256 case SPECIAL_OR_ASSIGN:
1257 case SPECIAL_XOR_ASSIGN:
1258 case SPECIAL_MUL_ASSIGN:
1259 case SPECIAL_DIV_ASSIGN:
1260 binop_expr = binop_expression(expr->left,
1261 op_remove_assign(expr->op),
1262 expr->right);
1263 get_absolute_rl(binop_expr, &rl);
1264 rl = cast_rl(left_type, rl);
1265 if (inside_loop()) {
1266 if (expr->op == SPECIAL_ADD_ASSIGN)
1267 add_range(&rl, rl_max(rl), sval_type_max(rl_type(rl)));
1269 if (expr->op == SPECIAL_SUB_ASSIGN &&
1270 !sval_is_negative(rl_min(rl))) {
1271 sval_t zero = { .type = rl_type(rl) };
1273 add_range(&rl, rl_min(rl), zero);
1276 set_extra_mod(name, sym, left, alloc_estate_rl(rl));
1277 goto free;
1279 set_extra_mod(name, sym, left, alloc_estate_whole(left_type));
1280 free:
1281 free_string(name);
1284 static struct smatch_state *increment_state(struct smatch_state *state)
1286 sval_t min = estate_min(state);
1287 sval_t max = estate_max(state);
1289 if (!estate_rl(state))
1290 return NULL;
1292 if (inside_loop())
1293 max = sval_type_max(max.type);
1295 if (!sval_is_min(min) && !sval_is_max(min))
1296 min.value++;
1297 if (!sval_is_min(max) && !sval_is_max(max))
1298 max.value++;
1299 return alloc_estate_range(min, max);
1302 static struct smatch_state *decrement_state(struct smatch_state *state)
1304 sval_t min = estate_min(state);
1305 sval_t max = estate_max(state);
1307 if (!estate_rl(state))
1308 return NULL;
1310 if (inside_loop())
1311 min = sval_type_min(min.type);
1313 if (!sval_is_min(min) && !sval_is_max(min))
1314 min.value--;
1315 if (!sval_is_min(max) && !sval_is_max(max))
1316 max.value--;
1317 return alloc_estate_range(min, max);
1320 static void clear_pointed_at_state(struct expression *expr)
1322 struct symbol *type;
1325 * ALERT: This is sort of a mess. If it's is a struct assigment like
1326 * "foo = bar;", then that's handled by smatch_struct_assignment.c.
1327 * the same thing for p++ where "p" is a struct. Most modifications
1328 * are handled by the assignment hook or the db. Smatch_extra.c doesn't
1329 * use smatch_modification.c because we have to get the ordering right
1330 * or something. So if you have p++ where p is a pointer to a standard
1331 * c type then we handle that here. What a mess.
1333 expr = strip_expr(expr);
1334 type = get_type(expr);
1335 if (!type || type->type != SYM_PTR)
1336 return;
1337 type = get_real_base_type(type);
1338 if (!type || type->type != SYM_BASETYPE)
1339 return;
1340 set_extra_expr_nomod(deref_expression(expr), alloc_estate_whole(type));
1343 static void unop_expr(struct expression *expr)
1345 struct smatch_state *state;
1347 if (expr->smatch_flags & Handled)
1348 return;
1350 switch (expr->op) {
1351 case SPECIAL_INCREMENT:
1352 state = get_state_expr(SMATCH_EXTRA, expr->unop);
1353 state = increment_state(state);
1354 if (!state)
1355 state = alloc_estate_whole(get_type(expr));
1356 set_extra_expr_mod(expr->unop, state);
1357 clear_pointed_at_state(expr->unop);
1358 break;
1359 case SPECIAL_DECREMENT:
1360 state = get_state_expr(SMATCH_EXTRA, expr->unop);
1361 state = decrement_state(state);
1362 if (!state)
1363 state = alloc_estate_whole(get_type(expr));
1364 set_extra_expr_mod(expr->unop, state);
1365 clear_pointed_at_state(expr->unop);
1366 break;
1367 default:
1368 return;
1372 static void asm_expr(struct statement *stmt)
1374 struct asm_operand *op;
1375 struct symbol *type;
1377 FOR_EACH_PTR(stmt->asm_outputs, op) {
1378 type = get_type(strip_expr(op->expr));
1379 set_extra_expr_mod(op->expr, alloc_estate_whole(type));
1380 } END_FOR_EACH_PTR(op);
1383 extern int __no_limits;
1384 static void check_dereference(struct expression *expr)
1386 struct smatch_state *state;
1388 if (__in_fake_assign)
1389 return;
1390 if (outside_of_function())
1391 return;
1392 state = get_extra_state(expr);
1393 if (state) {
1394 struct range_list *rl;
1396 rl = rl_intersection(estate_rl(state), valid_ptr_rl);
1397 if (rl_equiv(rl, estate_rl(state)))
1398 return;
1399 __no_limits++;
1400 set_extra_expr_nomod(expr, alloc_estate_rl(rl));
1401 __no_limits--;
1402 } else {
1403 struct range_list *rl;
1405 if (get_mtag_rl(expr, &rl))
1406 rl = rl_intersection(rl, valid_ptr_rl);
1407 else
1408 rl = clone_rl(valid_ptr_rl);
1410 __no_limits++;
1411 set_extra_expr_nomod(expr, alloc_estate_rl(rl));
1412 __no_limits--;
1416 static void match_dereferences(struct expression *expr)
1418 if (expr->type != EXPR_PREOP)
1419 return;
1420 if (getting_address(expr))
1421 return;
1422 /* it's saying that foo[1] = bar dereferences foo[1] */
1423 if (is_array(expr))
1424 return;
1425 check_dereference(expr->unop);
1428 static void match_pointer_as_array(struct expression *expr)
1430 if (!is_array(expr))
1431 return;
1432 check_dereference(get_array_base(expr));
1435 static void find_dereferences(struct expression *expr)
1437 while (expr->type == EXPR_PREOP) {
1438 if (expr->op == '*')
1439 check_dereference(expr->unop);
1440 expr = strip_expr(expr->unop);
1444 static void set_param_dereferenced(struct expression *call, struct expression *arg, char *key, char *unused)
1446 struct symbol *sym;
1447 char *name;
1449 if (strcmp(key, "$") == 0 && arg->type == EXPR_PREOP && arg->op == '&') {
1450 struct expression *tmp;
1452 tmp = strip_expr(arg->unop);
1453 if (tmp->type == EXPR_DEREF) {
1454 tmp = strip_expr(tmp->deref);
1455 if (tmp->type == EXPR_PREOP && tmp->op == '*')
1456 arg = strip_expr(tmp->unop);
1460 name = get_variable_from_key(arg, key, &sym);
1461 if (name && sym) {
1462 struct smatch_state *orig, *new;
1463 struct range_list *rl;
1465 orig = get_state(SMATCH_EXTRA, name, sym);
1466 if (orig) {
1467 rl = rl_intersection(estate_rl(orig),
1468 alloc_rl(valid_ptr_min_sval,
1469 valid_ptr_max_sval));
1470 new = alloc_estate_rl(rl);
1471 } else {
1472 new = alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval);
1475 __no_limits++;
1476 set_extra_nomod(name, sym, NULL, new);
1477 __no_limits--;
1479 free_string(name);
1481 find_dereferences(arg);
1484 static sval_t add_one(sval_t sval)
1486 sval.value++;
1487 return sval;
1490 static int handle_postop_inc(struct expression *left, int op, struct expression *right)
1492 struct statement *stmt;
1493 struct expression *cond;
1494 struct smatch_state *true_state, *false_state;
1495 struct symbol *type;
1496 sval_t start;
1497 sval_t limit;
1500 * If we're decrementing here then that's a canonical while count down
1501 * so it's handled already. We're only handling loops like:
1502 * i = 0;
1503 * do { ... } while (i++ < 3);
1506 if (left->type != EXPR_POSTOP || left->op != SPECIAL_INCREMENT)
1507 return 0;
1509 stmt = __cur_stmt->parent;
1510 if (!stmt)
1511 return 0;
1512 if (stmt->type == STMT_COMPOUND)
1513 stmt = stmt->parent;
1514 if (!stmt || stmt->type != STMT_ITERATOR || !stmt->iterator_post_condition)
1515 return 0;
1517 cond = strip_expr(stmt->iterator_post_condition);
1518 if (cond->type != EXPR_COMPARE || cond->op != op)
1519 return 0;
1520 if (left != strip_expr(cond->left) || right != strip_expr(cond->right))
1521 return 0;
1523 if (!get_implied_value(left->unop, &start))
1524 return 0;
1525 if (!get_implied_value(right, &limit))
1526 return 0;
1527 type = get_type(left->unop);
1528 limit = sval_cast(type, limit);
1529 if (sval_cmp(start, limit) > 0)
1530 return 0;
1532 switch (op) {
1533 case '<':
1534 case SPECIAL_UNSIGNED_LT:
1535 break;
1536 case SPECIAL_LTE:
1537 case SPECIAL_UNSIGNED_LTE:
1538 limit = add_one(limit);
1539 default:
1540 return 0;
1544 true_state = alloc_estate_range(add_one(start), limit);
1545 false_state = alloc_estate_range(add_one(limit), add_one(limit));
1547 /* Currently we just discard the false state but when two passes is
1548 * implimented correctly then it will use it.
1551 set_extra_expr_true_false(left->unop, true_state, false_state);
1553 return 1;
1556 bool is_impossible_variable(struct expression *expr)
1558 struct smatch_state *state;
1560 state = get_extra_state(expr);
1561 if (state && !estate_rl(state))
1562 return true;
1563 return false;
1566 static bool in_macro(struct expression *left, struct expression *right)
1568 if (!left || !right)
1569 return 0;
1570 if (left->pos.line != right->pos.line || left->pos.pos != right->pos.pos)
1571 return 0;
1572 if (get_macro_name(left->pos))
1573 return 1;
1574 return 0;
1577 static void handle_comparison(struct symbol *type, struct expression *left, int op, struct expression *right)
1579 struct smatch_state *left_state_orig, *right_state_orig;
1580 struct range_list *left_orig;
1581 struct range_list *left_true;
1582 struct range_list *left_false;
1583 struct range_list *right_orig;
1584 struct range_list *right_true;
1585 struct range_list *right_false;
1586 struct smatch_state *left_true_state;
1587 struct smatch_state *left_false_state;
1588 struct smatch_state *right_true_state;
1589 struct smatch_state *right_false_state;
1590 sval_t dummy, hard_max;
1591 int left_postop = 0;
1592 int right_postop = 0;
1594 if (left->op == SPECIAL_INCREMENT || left->op == SPECIAL_DECREMENT) {
1595 if (left->type == EXPR_POSTOP) {
1596 left->smatch_flags |= Handled;
1597 left_postop = left->op;
1598 if (handle_postop_inc(left, op, right))
1599 return;
1601 left = strip_parens(left->unop);
1603 while (left->type == EXPR_ASSIGNMENT)
1604 left = strip_parens(left->left);
1606 if (right->op == SPECIAL_INCREMENT || right->op == SPECIAL_DECREMENT) {
1607 if (right->type == EXPR_POSTOP) {
1608 right->smatch_flags |= Handled;
1609 right_postop = right->op;
1611 right = strip_parens(right->unop);
1614 if (is_impossible_variable(left) || is_impossible_variable(right))
1615 return;
1617 left_state_orig = get_extra_state(left);
1618 get_real_absolute_rl(left, &left_orig);
1619 left_orig = cast_rl(type, left_orig);
1621 right_state_orig = get_extra_state(right);
1622 get_real_absolute_rl(right, &right_orig);
1623 right_orig = cast_rl(type, right_orig);
1625 split_comparison_rl(left_orig, op, right_orig, &left_true, &left_false, &right_true, &right_false);
1627 left_true = rl_truncate_cast(get_type(strip_expr(left)), left_true);
1628 left_false = rl_truncate_cast(get_type(strip_expr(left)), left_false);
1629 right_true = rl_truncate_cast(get_type(strip_expr(right)), right_true);
1630 right_false = rl_truncate_cast(get_type(strip_expr(right)), right_false);
1632 if (!left_true || !left_false) {
1633 struct range_list *tmp_true, *tmp_false;
1635 split_comparison_rl(alloc_whole_rl(type), op, right_orig, &tmp_true, &tmp_false, NULL, NULL);
1636 tmp_true = rl_truncate_cast(get_type(strip_expr(left)), tmp_true);
1637 tmp_false = rl_truncate_cast(get_type(strip_expr(left)), tmp_false);
1638 if (tmp_true && tmp_false)
1639 __save_imaginary_state(left, tmp_true, tmp_false);
1642 if (!right_true || !right_false) {
1643 struct range_list *tmp_true, *tmp_false;
1645 split_comparison_rl(alloc_whole_rl(type), op, right_orig, NULL, NULL, &tmp_true, &tmp_false);
1646 tmp_true = rl_truncate_cast(get_type(strip_expr(right)), tmp_true);
1647 tmp_false = rl_truncate_cast(get_type(strip_expr(right)), tmp_false);
1648 if (tmp_true && tmp_false)
1649 __save_imaginary_state(right, tmp_true, tmp_false);
1652 left_true_state = alloc_estate_rl(left_true);
1653 left_false_state = alloc_estate_rl(left_false);
1654 right_true_state = alloc_estate_rl(right_true);
1655 right_false_state = alloc_estate_rl(right_false);
1657 switch (op) {
1658 case '<':
1659 case SPECIAL_UNSIGNED_LT:
1660 case SPECIAL_UNSIGNED_LTE:
1661 case SPECIAL_LTE:
1662 if (get_hard_max(right, &dummy) && !in_macro(left, right))
1663 estate_set_hard_max(left_true_state);
1664 if (get_hard_max(left, &dummy) && !in_macro(left, right))
1665 estate_set_hard_max(right_false_state);
1666 break;
1667 case '>':
1668 case SPECIAL_UNSIGNED_GT:
1669 case SPECIAL_UNSIGNED_GTE:
1670 case SPECIAL_GTE:
1671 if (get_hard_max(left, &dummy) && !in_macro(left, right))
1672 estate_set_hard_max(right_true_state);
1673 if (get_hard_max(right, &dummy) && !in_macro(left, right))
1674 estate_set_hard_max(left_false_state);
1675 break;
1678 switch (op) {
1679 case '<':
1680 case SPECIAL_UNSIGNED_LT:
1681 case SPECIAL_UNSIGNED_LTE:
1682 case SPECIAL_LTE:
1683 if (get_hard_max(right, &hard_max)) {
1684 if (op == '<' || op == SPECIAL_UNSIGNED_LT)
1685 hard_max.value--;
1686 estate_set_fuzzy_max(left_true_state, hard_max);
1688 if (get_implied_value(right, &hard_max)) {
1689 if (op == SPECIAL_UNSIGNED_LTE ||
1690 op == SPECIAL_LTE)
1691 hard_max.value++;
1692 estate_set_fuzzy_max(left_false_state, hard_max);
1694 if (get_hard_max(left, &hard_max)) {
1695 if (op == SPECIAL_UNSIGNED_LTE ||
1696 op == SPECIAL_LTE)
1697 hard_max.value--;
1698 estate_set_fuzzy_max(right_false_state, hard_max);
1700 if (get_implied_value(left, &hard_max)) {
1701 if (op == '<' || op == SPECIAL_UNSIGNED_LT)
1702 hard_max.value++;
1703 estate_set_fuzzy_max(right_true_state, hard_max);
1705 break;
1706 case '>':
1707 case SPECIAL_UNSIGNED_GT:
1708 case SPECIAL_UNSIGNED_GTE:
1709 case SPECIAL_GTE:
1710 if (get_hard_max(left, &hard_max)) {
1711 if (op == '>' || op == SPECIAL_UNSIGNED_GT)
1712 hard_max.value--;
1713 estate_set_fuzzy_max(right_true_state, hard_max);
1715 if (get_implied_value(left, &hard_max)) {
1716 if (op == SPECIAL_UNSIGNED_GTE ||
1717 op == SPECIAL_GTE)
1718 hard_max.value++;
1719 estate_set_fuzzy_max(right_false_state, hard_max);
1721 if (get_hard_max(right, &hard_max)) {
1722 if (op == SPECIAL_UNSIGNED_LTE ||
1723 op == SPECIAL_LTE)
1724 hard_max.value--;
1725 estate_set_fuzzy_max(left_false_state, hard_max);
1727 if (get_implied_value(right, &hard_max)) {
1728 if (op == '>' ||
1729 op == SPECIAL_UNSIGNED_GT)
1730 hard_max.value++;
1731 estate_set_fuzzy_max(left_true_state, hard_max);
1733 break;
1734 case SPECIAL_EQUAL:
1735 if (get_hard_max(left, &hard_max))
1736 estate_set_fuzzy_max(right_true_state, hard_max);
1737 if (get_hard_max(right, &hard_max))
1738 estate_set_fuzzy_max(left_true_state, hard_max);
1739 break;
1742 if (get_hard_max(left, &hard_max)) {
1743 estate_set_hard_max(left_true_state);
1744 estate_set_hard_max(left_false_state);
1746 if (get_hard_max(right, &hard_max)) {
1747 estate_set_hard_max(right_true_state);
1748 estate_set_hard_max(right_false_state);
1751 if (left_postop == SPECIAL_INCREMENT) {
1752 left_true_state = increment_state(left_true_state);
1753 left_false_state = increment_state(left_false_state);
1755 if (left_postop == SPECIAL_DECREMENT) {
1756 left_true_state = decrement_state(left_true_state);
1757 left_false_state = decrement_state(left_false_state);
1759 if (right_postop == SPECIAL_INCREMENT) {
1760 right_true_state = increment_state(right_true_state);
1761 right_false_state = increment_state(right_false_state);
1763 if (right_postop == SPECIAL_DECREMENT) {
1764 right_true_state = decrement_state(right_true_state);
1765 right_false_state = decrement_state(right_false_state);
1768 if (estate_rl(left_true_state) && estates_equiv(left_true_state, left_false_state)) {
1769 left_true_state = NULL;
1770 left_false_state = NULL;
1773 if (estate_rl(right_true_state) && estates_equiv(right_true_state, right_false_state)) {
1774 right_true_state = NULL;
1775 right_false_state = NULL;
1778 /* Don't introduce new states for known true/false conditions */
1779 if (rl_equiv(estate_rl(left_state_orig), estate_rl(left_true_state)))
1780 left_true_state = NULL;
1781 if (rl_equiv(estate_rl(left_state_orig), estate_rl(left_false_state)))
1782 left_false_state = NULL;
1783 if (rl_equiv(estate_rl(right_state_orig), estate_rl(right_true_state)))
1784 right_true_state = NULL;
1785 if (rl_equiv(estate_rl(right_state_orig), estate_rl(right_false_state)))
1786 right_false_state = NULL;
1788 set_extra_expr_true_false(left, left_true_state, left_false_state);
1789 set_extra_expr_true_false(right, right_true_state, right_false_state);
1792 static int is_simple_math(struct expression *expr)
1794 if (!expr)
1795 return 0;
1796 if (expr->type != EXPR_BINOP)
1797 return 0;
1798 switch (expr->op) {
1799 case '+':
1800 case '-':
1801 case '*':
1802 return 1;
1804 return 0;
1807 static int flip_op(int op)
1809 /* We only care about simple math */
1810 switch (op) {
1811 case '+':
1812 return '-';
1813 case '-':
1814 return '+';
1815 case '*':
1816 return '/';
1818 return 0;
1821 static void move_known_to_rl(struct expression **expr_p, struct range_list **rl_p)
1823 struct expression *expr = *expr_p;
1824 struct range_list *rl = *rl_p;
1825 sval_t sval;
1827 if (!is_simple_math(expr))
1828 return;
1830 if (get_implied_value(expr->right, &sval)) {
1831 *expr_p = expr->left;
1832 *rl_p = rl_binop(rl, flip_op(expr->op), alloc_rl(sval, sval));
1833 move_known_to_rl(expr_p, rl_p);
1834 return;
1836 if (expr->op == '-')
1837 return;
1838 if (get_implied_value(expr->left, &sval)) {
1839 *expr_p = expr->right;
1840 *rl_p = rl_binop(rl, flip_op(expr->op), alloc_rl(sval, sval));
1841 move_known_to_rl(expr_p, rl_p);
1842 return;
1846 static void move_known_values(struct expression **left_p, struct expression **right_p)
1848 struct expression *left = *left_p;
1849 struct expression *right = *right_p;
1850 sval_t sval, dummy;
1852 if (get_implied_value(left, &sval)) {
1853 if (!is_simple_math(right))
1854 return;
1855 if (get_implied_value(right, &dummy))
1856 return;
1857 if (right->op == '*') {
1858 sval_t divisor;
1860 if (!get_value(right->right, &divisor))
1861 return;
1862 if (divisor.value == 0)
1863 return;
1864 *left_p = binop_expression(left, invert_op(right->op), right->right);
1865 *right_p = right->left;
1866 return;
1868 if (right->op == '+' && get_value(right->left, &sval)) {
1869 *left_p = binop_expression(left, invert_op(right->op), right->left);
1870 *right_p = right->right;
1871 return;
1873 if (get_value(right->right, &sval)) {
1874 *left_p = binop_expression(left, invert_op(right->op), right->right);
1875 *right_p = right->left;
1876 return;
1878 return;
1880 if (get_implied_value(right, &sval)) {
1881 if (!is_simple_math(left))
1882 return;
1883 if (get_implied_value(left, &dummy))
1884 return;
1885 if (left->op == '*') {
1886 sval_t divisor;
1888 if (!get_value(left->right, &divisor))
1889 return;
1890 if (divisor.value == 0)
1891 return;
1892 *right_p = binop_expression(right, invert_op(left->op), left->right);
1893 *left_p = left->left;
1894 return;
1896 if (left->op == '+' && get_value(left->left, &sval)) {
1897 *right_p = binop_expression(right, invert_op(left->op), left->left);
1898 *left_p = left->right;
1899 return;
1902 if (get_value(left->right, &sval)) {
1903 *right_p = binop_expression(right, invert_op(left->op), left->right);
1904 *left_p = left->left;
1905 return;
1907 return;
1912 * The reason for do_simple_algebra() is to solve things like:
1913 * if (foo > 66 || foo + bar > 64) {
1914 * "foo" is not really a known variable so it won't be handled by
1915 * move_known_variables() but it's a super common idiom.
1918 static int do_simple_algebra(struct expression **left_p, struct expression **right_p)
1920 struct expression *left = *left_p;
1921 struct expression *right = *right_p;
1922 struct range_list *rl;
1923 sval_t tmp;
1925 if (left->type != EXPR_BINOP || left->op != '+')
1926 return 0;
1927 if (can_integer_overflow(get_type(left), left))
1928 return 0;
1929 if (!get_implied_value(right, &tmp))
1930 return 0;
1932 if (!get_implied_value(left->left, &tmp) &&
1933 get_implied_rl(left->left, &rl) &&
1934 !is_whole_rl(rl)) {
1935 *right_p = binop_expression(right, '-', left->left);
1936 *left_p = left->right;
1937 return 1;
1939 if (!get_implied_value(left->right, &tmp) &&
1940 get_implied_rl(left->right, &rl) &&
1941 !is_whole_rl(rl)) {
1942 *right_p = binop_expression(right, '-', left->right);
1943 *left_p = left->left;
1944 return 1;
1947 return 0;
1950 static int match_func_comparison(struct expression *expr)
1952 struct expression *left = strip_expr(expr->left);
1953 struct expression *right = strip_expr(expr->right);
1955 if (left->type == EXPR_CALL || right->type == EXPR_CALL) {
1956 // TODO: faked_assign this should be handled as a fake assignment instead
1957 function_comparison(left, expr->op, right);
1958 return 1;
1961 return 0;
1964 /* Handle conditions like "if (foo + bar < foo) {" */
1965 static int handle_integer_overflow_test(struct expression *expr)
1967 struct expression *left, *right;
1968 struct symbol *type;
1969 sval_t left_min, right_min, min, max;
1971 if (expr->op != '<' && expr->op != SPECIAL_UNSIGNED_LT)
1972 return 0;
1974 left = strip_parens(expr->left);
1975 right = strip_parens(expr->right);
1977 if (left->op != '+')
1978 return 0;
1980 type = get_type(expr);
1981 if (!type)
1982 return 0;
1983 if (type_positive_bits(type) == 32) {
1984 max.type = &uint_ctype;
1985 max.uvalue = (unsigned int)-1;
1986 } else if (type_positive_bits(type) == 64) {
1987 max.type = &ulong_ctype;
1988 max.value = (unsigned long long)-1;
1989 } else {
1990 return 0;
1993 if (!expr_equiv(left->left, right) && !expr_equiv(left->right, right))
1994 return 0;
1996 get_absolute_min(left->left, &left_min);
1997 get_absolute_min(left->right, &right_min);
1998 min = sval_binop(left_min, '+', right_min);
2000 type = get_type(left);
2001 min = sval_cast(type, min);
2002 max = sval_cast(type, max);
2004 set_extra_chunk_true_false(left, NULL, alloc_estate_range(min, max));
2005 return 1;
2008 static void match_comparison(struct expression *expr)
2010 struct expression *left_orig = strip_parens(expr->left);
2011 struct expression *right_orig = strip_parens(expr->right);
2012 struct expression *left, *right, *tmp;
2013 struct expression *prev;
2014 struct symbol *type;
2015 int redo, count;
2017 if (match_func_comparison(expr))
2018 return;
2020 type = get_type(expr);
2021 if (!type)
2022 type = &llong_ctype;
2024 if (handle_integer_overflow_test(expr))
2025 return;
2027 left = left_orig;
2028 right = right_orig;
2029 move_known_values(&left, &right);
2030 handle_comparison(type, left, expr->op, right);
2032 left = left_orig;
2033 right = right_orig;
2034 if (do_simple_algebra(&left, &right))
2035 handle_comparison(type, left, expr->op, right);
2037 prev = get_assigned_expr(left_orig);
2038 if (is_simple_math(prev) && !has_variable(prev, left_orig)) {
2039 left = prev;
2040 right = right_orig;
2041 move_known_values(&left, &right);
2042 handle_comparison(type, left, expr->op, right);
2045 prev = get_assigned_expr(right_orig);
2046 if (is_simple_math(prev) && !has_variable(prev, right_orig)) {
2047 left = left_orig;
2048 right = prev;
2049 move_known_values(&left, &right);
2050 handle_comparison(type, left, expr->op, right);
2053 redo = 0;
2054 left = left_orig;
2055 right = right_orig;
2056 if (get_last_expr_from_expression_stmt(left_orig)) {
2057 left = get_last_expr_from_expression_stmt(left_orig);
2058 redo = 1;
2060 if (get_last_expr_from_expression_stmt(right_orig)) {
2061 right = get_last_expr_from_expression_stmt(right_orig);
2062 redo = 1;
2065 if (!redo)
2066 return;
2068 count = 0;
2069 while ((tmp = get_assigned_expr(left))) {
2070 if (count++ > 3)
2071 break;
2072 left = strip_expr(tmp);
2074 count = 0;
2075 while ((tmp = get_assigned_expr(right))) {
2076 if (count++ > 3)
2077 break;
2078 right = strip_expr(tmp);
2081 handle_comparison(type, left, expr->op, right);
2084 static sval_t get_high_mask(sval_t known)
2086 sval_t ret;
2087 int i;
2089 ret = known;
2090 ret.value = 0;
2092 for (i = type_bits(known.type) - 1; i >= 0; i--) {
2093 if (known.uvalue & (1ULL << i))
2094 ret.uvalue |= (1ULL << i);
2095 else
2096 return ret;
2099 return ret;
2102 static bool handle_bit_test(struct expression *expr)
2104 struct range_list *orig_rl, *rlt, *rlf, *true_rl, *false_rl;
2105 struct expression *shift, *mask, *var;
2106 struct bit_info *bit_info;
2107 sval_t sval;
2108 sval_t high = { .type = &int_ctype };
2109 sval_t low = { .type = &int_ctype };
2111 shift = strip_expr(expr->right);
2112 mask = strip_expr(expr->left);
2113 if (shift->type != EXPR_BINOP || shift->op != SPECIAL_LEFTSHIFT) {
2114 shift = strip_expr(expr->left);
2115 mask = strip_expr(expr->right);
2116 if (shift->type != EXPR_BINOP || shift->op != SPECIAL_LEFTSHIFT)
2117 return false;
2119 if (!get_implied_value(shift->left, &sval) || sval.value != 1)
2120 return false;
2121 var = strip_expr(shift->right);
2123 bit_info = get_bit_info(mask);
2124 if (!bit_info)
2125 return false;
2126 if (!bit_info->possible){
2127 set_true_false_states_expr(my_id, var, alloc_estate_empty(), NULL);
2128 return false;
2131 get_absolute_rl(var, &orig_rl);
2132 if (sval_is_negative(rl_min(orig_rl)) ||
2133 rl_max(orig_rl).uvalue > type_bits(get_type(shift->left)))
2134 return false;
2136 low.value = ffsll(bit_info->possible) - 1;
2137 high.value = sm_fls64(bit_info->possible) - 1;
2138 rlt = alloc_rl(low, high);
2139 rlt = cast_rl(get_type(var), rlt);
2140 true_rl = rl_intersection(orig_rl, rlt);
2142 low.value = ffsll(bit_info->set) - 1;
2143 high.value = sm_fls64(bit_info->set) - 1;
2144 rlf = alloc_rl(low, high);
2145 rlf = cast_rl(get_type(var), rlf);
2146 false_rl = rl_filter(orig_rl, rlf);
2148 set_extra_expr_true_false(var, alloc_estate_rl(true_rl), alloc_estate_rl(false_rl));
2150 return true;
2153 static void handle_AND_op(struct expression *var, sval_t known)
2155 struct range_list *orig_rl;
2156 struct range_list *true_rl = NULL;
2157 struct range_list *false_rl = NULL;
2158 int bit;
2159 sval_t low_mask = known;
2160 sval_t high_mask;
2161 sval_t max;
2163 get_absolute_rl(var, &orig_rl);
2165 if (known.value > 0) {
2166 bit = ffsll(known.value) - 1;
2167 low_mask.uvalue = (1ULL << bit) - 1;
2168 true_rl = remove_range(orig_rl, sval_type_val(known.type, 0), low_mask);
2170 high_mask = get_high_mask(known);
2171 if (high_mask.value) {
2172 bit = ffsll(high_mask.value) - 1;
2173 low_mask.uvalue = (1ULL << bit) - 1;
2175 false_rl = orig_rl;
2176 if (sval_is_negative(rl_min(orig_rl)))
2177 false_rl = remove_range(false_rl, sval_type_min(known.type), sval_type_val(known.type, -1));
2178 false_rl = remove_range(false_rl, low_mask, sval_type_max(known.type));
2179 if (type_signed(high_mask.type) && type_unsigned(rl_type(false_rl))) {
2180 false_rl = remove_range(false_rl,
2181 sval_type_val(rl_type(false_rl), sval_type_max(known.type).uvalue),
2182 sval_type_val(rl_type(false_rl), -1));
2184 } else if (known.value == 1 &&
2185 get_hard_max(var, &max) &&
2186 sval_cmp(max, rl_max(orig_rl)) == 0 &&
2187 max.value & 1) {
2188 false_rl = remove_range(orig_rl, max, max);
2190 set_extra_expr_true_false(var,
2191 true_rl ? alloc_estate_rl(true_rl) : NULL,
2192 false_rl ? alloc_estate_rl(false_rl) : NULL);
2195 static void handle_AND_condition(struct expression *expr)
2197 sval_t known;
2199 if (handle_bit_test(expr))
2200 return;
2202 if (get_implied_value(expr->left, &known))
2203 handle_AND_op(expr->right, known);
2204 else if (get_implied_value(expr->right, &known))
2205 handle_AND_op(expr->left, known);
2208 static void handle_MOD_condition(struct expression *expr)
2210 struct range_list *orig_rl;
2211 struct range_list *true_rl;
2212 struct range_list *false_rl = NULL;
2213 sval_t right;
2214 sval_t zero = {
2215 .value = 0,
2218 if (!get_implied_value(expr->right, &right) || right.value == 0)
2219 return;
2220 get_absolute_rl(expr->left, &orig_rl);
2222 zero.type = rl_type(orig_rl);
2224 /* We're basically dorking around the min and max here */
2225 true_rl = remove_range(orig_rl, zero, zero);
2226 if (!sval_is_max(rl_max(true_rl)) &&
2227 !(rl_max(true_rl).value % right.value))
2228 true_rl = remove_range(true_rl, rl_max(true_rl), rl_max(true_rl));
2230 if (rl_equiv(true_rl, orig_rl))
2231 true_rl = NULL;
2233 if (sval_is_positive(rl_min(orig_rl)) &&
2234 (rl_max(orig_rl).value - rl_min(orig_rl).value) / right.value < 5) {
2235 sval_t add;
2236 int i;
2238 add = rl_min(orig_rl);
2239 add.value += right.value - (add.value % right.value);
2240 add.value -= right.value;
2242 for (i = 0; i < 5; i++) {
2243 add.value += right.value;
2244 if (add.value > rl_max(orig_rl).value)
2245 break;
2246 add_range(&false_rl, add, add);
2248 } else {
2249 if (rl_min(orig_rl).uvalue != 0 &&
2250 rl_min(orig_rl).uvalue < right.uvalue) {
2251 sval_t chop = right;
2252 chop.value--;
2253 false_rl = remove_range(orig_rl, zero, chop);
2256 if (!sval_is_max(rl_max(orig_rl)) &&
2257 (rl_max(orig_rl).value % right.value)) {
2258 sval_t chop = rl_max(orig_rl);
2259 chop.value -= chop.value % right.value;
2260 chop.value++;
2261 if (!false_rl)
2262 false_rl = clone_rl(orig_rl);
2263 false_rl = remove_range(false_rl, chop, rl_max(orig_rl));
2267 set_extra_expr_true_false(expr->left,
2268 true_rl ? alloc_estate_rl(true_rl) : NULL,
2269 false_rl ? alloc_estate_rl(false_rl) : NULL);
2272 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
2273 void __extra_match_condition(struct expression *expr)
2275 expr = strip_expr(expr);
2276 switch (expr->type) {
2277 case EXPR_CALL:
2278 function_comparison(expr, SPECIAL_NOTEQUAL, zero_expr());
2279 return;
2280 case EXPR_PREOP:
2281 case EXPR_SYMBOL:
2282 case EXPR_DEREF:
2283 handle_comparison(get_type(expr), expr, SPECIAL_NOTEQUAL, zero_expr());
2284 return;
2285 case EXPR_COMPARE:
2286 match_comparison(expr);
2287 return;
2288 case EXPR_ASSIGNMENT:
2289 __extra_match_condition(expr->left);
2290 return;
2291 case EXPR_BINOP:
2292 if (expr->op == '&')
2293 handle_AND_condition(expr);
2294 if (expr->op == '%')
2295 handle_MOD_condition(expr);
2296 return;
2300 static void assume_indexes_are_valid(struct expression *expr)
2302 struct expression *array_expr;
2303 int array_size;
2304 struct expression *offset;
2305 struct symbol *offset_type;
2306 struct range_list *rl_before;
2307 struct range_list *rl_after;
2308 struct range_list *filter = NULL;
2309 sval_t size;
2311 expr = strip_expr(expr);
2312 if (!is_array(expr))
2313 return;
2315 offset = get_array_offset(expr);
2316 offset_type = get_type(offset);
2317 if (offset_type && type_signed(offset_type)) {
2318 filter = alloc_rl(sval_type_min(offset_type),
2319 sval_type_val(offset_type, -1));
2322 array_expr = get_array_base(expr);
2323 array_size = get_real_array_size(array_expr);
2324 if (array_size > 1) {
2325 size = sval_type_val(offset_type, array_size);
2326 add_range(&filter, size, sval_type_max(offset_type));
2329 if (!filter)
2330 return;
2331 get_absolute_rl(offset, &rl_before);
2332 rl_after = rl_filter(rl_before, filter);
2333 if (rl_equiv(rl_before, rl_after))
2334 return;
2335 set_extra_expr_nomod(offset, alloc_estate_rl(rl_after));
2338 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
2339 int implied_not_equal(struct expression *expr, long long val)
2341 return !possibly_false(expr, SPECIAL_NOTEQUAL, value_expr(val));
2344 int implied_not_equal_name_sym(char *name, struct symbol *sym, long long val)
2346 struct smatch_state *estate;
2348 estate = get_state(SMATCH_EXTRA, name, sym);
2349 if (!estate)
2350 return 0;
2351 if (!rl_has_sval(estate_rl(estate), sval_type_val(estate_type(estate), 0)))
2352 return 1;
2353 return 0;
2356 bool is_noderef_ptr(struct expression *expr)
2358 struct range_list *rl;
2360 if (!get_implied_rl(expr, &rl))
2361 return false;
2362 return is_noderef_ptr_rl(rl);
2365 static int parent_is_err_or_null_var_sym_helper(const char *name, struct symbol *sym, bool check_err_ptr)
2367 struct smatch_state *state;
2368 char buf[256];
2369 char *start;
2370 int len;
2372 strncpy(buf, name, sizeof(buf) - 1);
2373 buf[sizeof(buf) - 1] = '\0';
2375 start = &buf[0];
2376 while (*start == '*') {
2377 start++;
2378 state = __get_state(SMATCH_EXTRA, start, sym);
2379 if (!state)
2380 continue;
2381 if (!estate_rl(state))
2382 return 1;
2383 if (is_noderef_ptr_rl(estate_rl(state)))
2384 return 1;
2387 start = &buf[0];
2388 while (*start == '&')
2389 start++;
2391 len = strlen(start);
2392 while (true) {
2393 while (len > 0) {
2394 len--;
2395 if (start[len] == '-' ||
2396 start[len] == '.') {
2397 start[len] = '\0';
2398 break;
2401 if (len == 0)
2402 return 0;
2403 state = __get_state(SMATCH_EXTRA, start, sym);
2404 if (!state)
2405 continue;
2406 if (is_noderef_ptr_rl(estate_rl(state)))
2407 return 1;
2411 int parent_is_null_var_sym(const char *name, struct symbol *sym)
2413 return parent_is_err_or_null_var_sym_helper(name, sym, false);
2416 int parent_is_err_or_null_var_sym(const char *name, struct symbol *sym)
2418 return parent_is_err_or_null_var_sym_helper(name, sym, (option_project == PROJ_KERNEL));
2421 int parent_is_null(struct expression *expr)
2423 struct symbol *sym;
2424 char *var;
2425 int ret = 0;
2427 expr = strip_expr(expr);
2428 var = expr_to_var_sym(expr, &sym);
2429 if (!var || !sym)
2430 goto free;
2431 ret = parent_is_null_var_sym(var, sym);
2432 free:
2433 free_string(var);
2434 return ret;
2437 static int param_used_callback(void *found, int argc, char **argv, char **azColName)
2439 *(int *)found = 1;
2440 return 0;
2443 static int is_kzalloc_info(struct sm_state *sm)
2445 sval_t sval;
2448 * kzalloc() information is treated as special because so there is just
2449 * a lot of stuff initialized to zero and it makes building the database
2450 * take hours and hours.
2452 * In theory, we should just remove this line and not pass any unused
2453 * information, but I'm not sure enough that this code works so I want
2454 * to hold off on that for now.
2456 if (!estate_get_single_value(sm->state, &sval))
2457 return 0;
2458 if (sval.value != 0)
2459 return 0;
2460 return 1;
2463 static int is_really_long(struct sm_state *sm)
2465 const char *p;
2466 int cnt = 0;
2468 p = sm->name;
2469 while ((p = strstr(p, "->"))) {
2470 p += 2;
2471 cnt++;
2474 if (cnt < 3 ||
2475 strlen(sm->name) < 40)
2476 return 0;
2477 return 1;
2480 static int filter_unused_param_value_info(struct expression *call, int param, char *printed_name, struct sm_state *sm)
2482 int found = 0;
2484 /* for function pointers assume everything is used */
2485 if (call->fn->type != EXPR_SYMBOL)
2486 return 0;
2488 if (strcmp(printed_name, "$") == 0 ||
2489 strcmp(printed_name, "*$") == 0)
2490 return 0;
2493 * This is to handle __builtin_mul_overflow(). In an ideal world we
2494 * would only need this for invalid code.
2497 if (!call->fn->symbol)
2498 return 0;
2500 if (!is_kzalloc_info(sm) && !is_really_long(sm))
2501 return 0;
2503 run_sql(&param_used_callback, &found,
2504 "select * from return_implies where %s and type = %d and parameter = %d and key = '%s';",
2505 get_static_filter(call->fn->symbol), PARAM_USED, param, printed_name);
2506 if (found)
2507 return 0;
2509 /* If the database is not built yet, then assume everything is used */
2510 run_sql(&param_used_callback, &found,
2511 "select * from return_implies where %s and type = %d;",
2512 get_static_filter(call->fn->symbol), PARAM_USED);
2513 if (!found)
2514 return 0;
2516 return 1;
2519 struct range_list *intersect_with_real_abs_var_sym(const char *name, struct symbol *sym, struct range_list *start)
2521 struct smatch_state *state;
2524 * Here is the difference between implied value and real absolute, say
2525 * you have:
2527 * int a = (u8)x;
2529 * Then you know that a is 0-255. That's real absolute. But you don't
2530 * know for sure that it actually goes up to 255. So it's not implied.
2531 * Implied indicates a degree of certainty.
2533 * But then say you cap "a" at 8. That means you know it goes up to
2534 * 8. So now the implied value is s32min-8. But you can combine it
2535 * with the real absolute to say that actually it's 0-8.
2537 * We are combining it here. But now that I think about it, this is
2538 * probably not the ideal place to combine it because it should proably
2539 * be done earlier. Oh well, this is an improvement on what was there
2540 * before so I'm going to commit this code.
2544 state = get_real_absolute_state_var_sym(name, sym);
2545 if (!state || !estate_rl(state))
2546 return start;
2548 return rl_intersection(estate_rl(state), start);
2551 struct range_list *intersect_with_real_abs_expr(struct expression *expr, struct range_list *start)
2553 struct smatch_state *state;
2554 struct range_list *abs_rl;
2556 state = get_real_absolute_state(expr);
2557 if (!state || !estate_rl(state))
2558 return start;
2560 abs_rl = cast_rl(rl_type(start), estate_rl(state));
2561 return rl_intersection(abs_rl, start);
2564 static void caller_info_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
2566 struct range_list *rl;
2567 sval_t dummy;
2569 if (printed_name[0] == '&')
2570 return;
2571 if (estate_is_whole(sm->state) || !estate_rl(sm->state))
2572 return;
2573 if (filter_unused_param_value_info(call, param, printed_name, sm))
2574 return;
2575 rl = estate_rl(sm->state);
2576 rl = intersect_with_real_abs_var_sym(sm->name, sm->sym, rl);
2577 if (!rl)
2578 return;
2579 sql_insert_caller_info(call, PARAM_VALUE, param, printed_name, show_rl(rl));
2580 if (!estate_get_single_value(sm->state, &dummy)) {
2581 if (estate_has_hard_max(sm->state))
2582 sql_insert_caller_info(call, HARD_MAX, param, printed_name,
2583 sval_to_str(estate_max(sm->state)));
2584 if (estate_has_fuzzy_max(sm->state))
2585 sql_insert_caller_info(call, FUZZY_MAX, param, printed_name,
2586 sval_to_str(estate_get_fuzzy_max(sm->state)));
2590 static void returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
2592 struct symbol *returned_sym;
2593 char *returned_name;
2594 struct sm_state *sm;
2595 char *compare_str;
2596 char name_buf[256];
2597 char val_buf[256];
2598 int len;
2600 // FIXME handle *$
2602 if (!is_pointer(expr))
2603 return;
2604 if (return_ranges && strstr(return_ranges, "[==$"))
2605 return;
2607 returned_name = expr_to_var_sym(expr, &returned_sym);
2608 if (!returned_name || !returned_sym)
2609 goto free;
2610 len = strlen(returned_name);
2612 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
2613 if (!estate_rl(sm->state))
2614 continue;
2615 if (returned_sym != sm->sym)
2616 continue;
2617 if (strncmp(returned_name, sm->name, len) != 0)
2618 continue;
2619 if (sm->name[len] != '-')
2620 continue;
2622 snprintf(name_buf, sizeof(name_buf), "$%s", sm->name + len);
2624 compare_str = name_sym_to_param_comparison(sm->name, sm->sym);
2625 if (!compare_str && estate_is_whole(sm->state))
2626 continue;
2627 snprintf(val_buf, sizeof(val_buf), "%s%s", sm->state->name, compare_str ?: "");
2629 sql_insert_return_states(return_id, return_ranges, PARAM_VALUE,
2630 -1, name_buf, val_buf);
2631 } END_FOR_EACH_SM(sm);
2633 free:
2634 free_string(returned_name);
2637 static void db_limited_before(void)
2639 unmatched_stree = clone_stree(__get_cur_stree());
2642 static void db_limited_after(void)
2644 free_stree(&unmatched_stree);
2647 static int basically_the_same(struct range_list *orig, struct range_list *new)
2649 if (type_is_ptr(rl_type(orig)) &&
2650 is_whole_ptr_rl(orig) &&
2651 is_whole_ptr_rl(new))
2652 return true;
2654 return rl_equiv(orig, new);
2657 static void db_param_limit_binops(struct expression *arg, char *key, struct range_list *rl)
2659 struct range_list *left_rl;
2660 sval_t zero = { .type = rl_type(rl), };
2661 sval_t sval;
2663 if (strcmp(key, "$") != 0)
2664 return;
2665 if (arg->op != '*')
2666 return;
2667 if (!get_implied_value(arg->right, &sval))
2668 return;
2669 if (can_integer_overflow(get_type(arg), arg))
2670 return;
2672 left_rl = rl_binop(rl, '/', alloc_rl(sval, sval));
2673 if (!rl_has_sval(rl, zero))
2674 left_rl = remove_range(left_rl, zero, zero);
2676 set_extra_expr_nomod(arg->left, alloc_estate_rl(left_rl));
2679 static void db_param_limit_filter(struct expression *expr, int param, char *key, char *value, enum info_type op)
2681 struct smatch_state *state;
2682 struct expression *arg;
2683 char *name;
2684 struct symbol *sym;
2685 struct var_sym_list *vsl = NULL;
2686 struct sm_state *sm;
2687 struct symbol *compare_type, *var_type;
2688 struct range_list *rl;
2689 struct range_list *limit;
2690 struct range_list *new;
2691 char *other_name;
2692 struct symbol *other_sym;
2694 while (expr->type == EXPR_ASSIGNMENT)
2695 expr = strip_expr(expr->right);
2696 if (expr->type != EXPR_CALL)
2697 return;
2699 arg = get_argument_from_call_expr(expr->args, param);
2700 if (!arg)
2701 return;
2703 if (strcmp(key, "$") == 0)
2704 compare_type = get_arg_type(expr->fn, param);
2705 else
2706 compare_type = get_member_type_from_key(arg, key);
2708 call_results_to_rl(expr, compare_type, value, &limit);
2709 if (strcmp(key, "$") == 0)
2710 move_known_to_rl(&arg, &limit);
2711 name = get_chunk_from_key(arg, key, &sym, &vsl);
2712 if (!name)
2713 return;
2714 if (op != PARAM_LIMIT && !sym)
2715 goto free;
2717 sm = get_sm_state(SMATCH_EXTRA, name, sym);
2718 if (sm)
2719 rl = estate_rl(sm->state);
2720 else
2721 rl = alloc_whole_rl(compare_type);
2723 if (op == PARAM_LIMIT && !rl_fits_in_type(rl, compare_type))
2724 goto free;
2726 new = rl_intersection(rl, limit);
2728 var_type = get_member_type_from_key(arg, key);
2729 new = cast_rl(var_type, new);
2731 /* We want to preserve the implications here */
2732 if (sm && basically_the_same(rl, new))
2733 goto free;
2734 other_name = get_other_name_sym(name, sym, &other_sym);
2736 state = alloc_estate_rl(new);
2737 if (sm && estate_has_hard_max(sm->state))
2738 estate_set_hard_max(state);
2740 if (op == PARAM_LIMIT) {
2741 set_extra_nomod_vsl(name, sym, vsl, NULL, state);
2742 } else
2743 set_extra_mod(name, sym, NULL, state);
2745 if (other_name && other_sym) {
2746 state = clone_estate(state);
2747 if (op == PARAM_LIMIT)
2748 set_extra_nomod_vsl(other_name, other_sym, vsl, NULL, state);
2749 else
2750 set_extra_mod(other_name, other_sym, NULL, state);
2753 if (op == PARAM_LIMIT && arg->type == EXPR_BINOP)
2754 db_param_limit_binops(arg, key, new);
2755 free:
2756 free_string(name);
2759 static void db_param_limit(struct expression *expr, int param, char *key, char *value)
2761 db_param_limit_filter(expr, param, key, value, PARAM_LIMIT);
2764 static void db_param_filter(struct expression *expr, int param, char *key, char *value)
2766 db_param_limit_filter(expr, param, key, value, PARAM_FILTER);
2769 static struct expression *get_star_pointer_hack(struct expression *arg, const char *key)
2771 /* if we are setting *(p + offset) then we are setting *p */
2772 if (strcmp(key, "*$") != 0)
2773 return NULL;
2775 arg = strip_expr(arg);
2776 if (arg->op != '+')
2777 return NULL;
2778 arg = strip_expr(arg->left);
2779 if (!is_pointer(arg))
2780 return NULL;
2781 return arg;
2784 static void db_param_add_set(struct expression *expr, int param, char *key, char *value, enum info_type op)
2786 struct expression *arg, *gen_expr;
2787 char *name;
2788 char *other_name = NULL;
2789 struct symbol *sym, *other_sym;
2790 struct symbol *param_type, *arg_type;
2791 struct smatch_state *state;
2792 struct range_list *new = NULL;
2793 struct range_list *added = NULL;
2795 while (expr->type == EXPR_ASSIGNMENT)
2796 expr = strip_expr(expr->right);
2797 if (expr->type != EXPR_CALL)
2798 return;
2800 arg = get_argument_from_call_expr(expr->args, param);
2801 if (!arg)
2802 return;
2804 arg_type = get_arg_type_from_key(expr->fn, param, arg, key);
2805 param_type = get_member_type_from_key(arg, key);
2806 if (param_type && param_type->type == SYM_STRUCT)
2807 return;
2808 name = get_variable_from_key(arg, key, &sym);
2809 if (!name || !sym) {
2810 arg = get_star_pointer_hack(arg, key);
2811 if (!arg)
2812 goto free;
2813 name = get_variable_from_key(arg, key, &sym);
2814 if (!name || !sym)
2815 goto free;
2817 gen_expr = gen_expression_from_key(arg, key);
2819 state = get_state(SMATCH_EXTRA, name, sym);
2820 if (state)
2821 new = estate_rl(state);
2823 call_results_to_rl(expr, arg_type, value, &added);
2824 added = cast_rl(param_type, added);
2825 if (op == PARAM_SET)
2826 new = added;
2827 else
2828 new = rl_union(new, added);
2830 other_name = get_other_name_sym_nostack(name, sym, &other_sym);
2831 set_extra_mod(name, sym, gen_expr, alloc_estate_rl(new));
2832 if (other_name && other_sym)
2833 set_extra_mod(other_name, other_sym, gen_expr, alloc_estate_rl(new));
2834 free:
2835 free_string(other_name);
2836 free_string(name);
2839 static void db_param_add(struct expression *expr, int param, char *key, char *value)
2841 in_param_set = true;
2842 db_param_add_set(expr, param, key, value, PARAM_ADD);
2843 in_param_set = false;
2846 static void db_param_set(struct expression *expr, int param, char *key, char *value)
2848 in_param_set = true;
2849 db_param_add_set(expr, param, key, value, PARAM_SET);
2850 in_param_set = false;
2853 static void match_lost_param(struct expression *call, int param)
2855 struct expression *arg;
2857 if (is_const_param(call->fn, param))
2858 return;
2860 arg = get_argument_from_call_expr(call->args, param);
2861 if (!arg)
2862 return;
2864 arg = strip_expr(arg);
2865 if (arg->type == EXPR_PREOP && arg->op == '&')
2866 set_extra_expr_mod(arg->unop, alloc_estate_whole(get_type(arg->unop)));
2867 else
2868 ; /* if pointer then set struct members, maybe?*/
2871 static void db_param_value(struct expression *expr, int param, char *key, char *value)
2873 struct expression *call;
2874 char *name;
2875 struct symbol *sym;
2876 struct symbol *type;
2877 struct range_list *rl = NULL;
2879 if (param != -1)
2880 return;
2882 call = expr;
2883 while (call->type == EXPR_ASSIGNMENT)
2884 call = strip_expr(call->right);
2885 if (call->type != EXPR_CALL)
2886 return;
2888 type = get_member_type_from_key(expr->left, key);
2889 name = get_variable_from_key(expr->left, key, &sym);
2890 if (!name || !sym)
2891 goto free;
2893 call_results_to_rl(call, type, value, &rl);
2895 set_extra_mod(name, sym, NULL, alloc_estate_rl(rl));
2896 free:
2897 free_string(name);
2900 static void set_param_value(const char *name, struct symbol *sym, char *key, char *value)
2902 struct expression *expr;
2903 struct range_list *rl = NULL;
2904 struct smatch_state *state;
2905 struct symbol *type;
2906 char *key_orig = key;
2907 char *fullname;
2908 sval_t dummy;
2910 expr = symbol_expression(sym);
2911 fullname = get_variable_from_key(expr, key, NULL);
2912 if (!fullname)
2913 return;
2915 type = get_member_type_from_key(expr, key_orig);
2916 str_to_rl(type, value, &rl);
2917 state = alloc_estate_rl(rl);
2918 if (estate_get_single_value(state, &dummy))
2919 estate_set_hard_max(state);
2920 set_state(SMATCH_EXTRA, fullname, sym, state);
2923 static void set_param_fuzzy_max(const char *name, struct symbol *sym, char *key, char *value)
2925 struct expression *expr;
2926 struct range_list *rl = NULL;
2927 struct smatch_state *state;
2928 struct symbol *type;
2929 char *fullname;
2930 sval_t max;
2932 expr = symbol_expression(sym);
2933 fullname = get_variable_from_key(expr, key, NULL);
2934 if (!fullname)
2935 return;
2937 state = get_state(SMATCH_EXTRA, fullname, sym);
2938 if (!state)
2939 return;
2940 type = estate_type(state);
2941 str_to_rl(type, value, &rl);
2942 if (!rl_to_sval(rl, &max))
2943 return;
2944 estate_set_fuzzy_max(state, max);
2947 static void set_param_hard_max(const char *name, struct symbol *sym, char *key, char *value)
2949 struct smatch_state *state;
2950 struct expression *expr;
2951 char *fullname;
2953 expr = symbol_expression(sym);
2954 fullname = get_variable_from_key(expr, key, NULL);
2955 if (!fullname)
2956 return;
2958 state = get_state(SMATCH_EXTRA, fullname, sym);
2959 if (!state)
2960 return;
2961 estate_set_hard_max(state);
2964 static struct sm_state *get_sm_from_call(struct expression *expr)
2966 struct expression *fake;
2967 struct sm_state *ret;
2968 char buf[32];
2970 if (is_fake_call(expr))
2971 return NULL;
2973 fake = expr_get_fake_parent_expr(expr);
2974 if (fake && fake->type == EXPR_ASSIGNMENT) {
2975 ret = get_sm_state_expr(SMATCH_EXTRA, fake->left);
2976 if (ret)
2977 return ret;
2980 snprintf(buf, sizeof(buf), "return %p", expr);
2981 return get_sm_state(SMATCH_EXTRA, buf, NULL);
2984 struct sm_state *get_extra_sm_state(struct expression *expr)
2986 char *name;
2987 struct symbol *sym;
2988 struct sm_state *ret = NULL;
2990 expr = strip_expr(expr);
2991 if (!expr)
2992 return NULL;
2994 if (expr->type == EXPR_CALL)
2995 return get_sm_from_call(expr);
2997 name = expr_to_known_chunk_sym(expr, &sym);
2998 if (!name)
2999 goto free;
3001 ret = get_sm_state(SMATCH_EXTRA, name, sym);
3002 free:
3003 free_string(name);
3004 return ret;
3007 struct smatch_state *get_extra_state(struct expression *expr)
3009 struct sm_state *sm;
3011 sm = get_extra_sm_state(expr);
3012 if (!sm)
3013 return NULL;
3014 return sm->state;
3017 void register_smatch_extra(int id)
3019 my_id = id;
3021 set_dynamic_states(my_id);
3022 add_merge_hook(my_id, &merge_estates);
3023 add_unmatched_state_hook(my_id, &unmatched_state);
3024 select_caller_info_hook(set_param_value, PARAM_VALUE);
3025 select_caller_info_hook(set_param_fuzzy_max, FUZZY_MAX);
3026 select_caller_info_hook(set_param_hard_max, HARD_MAX);
3027 select_return_states_before(&db_limited_before);
3028 select_return_states_hook(PARAM_LIMIT, &db_param_limit);
3029 select_return_states_hook(PARAM_FILTER, &db_param_filter);
3030 select_return_states_hook(PARAM_ADD, &db_param_add);
3031 select_return_states_hook(PARAM_SET, &db_param_set);
3032 add_lost_param_hook(&match_lost_param);
3033 select_return_states_hook(PARAM_VALUE, &db_param_value);
3034 select_return_states_after(&db_limited_after);
3037 static void match_link_modify(struct sm_state *sm, struct expression *mod_expr)
3039 struct var_sym_list *links;
3040 struct var_sym *tmp;
3041 struct smatch_state *state;
3043 links = sm->state->data;
3045 FOR_EACH_PTR(links, tmp) {
3046 if (sm->sym == tmp->sym &&
3047 strcmp(sm->name, tmp->var) == 0)
3048 continue;
3049 state = get_state(SMATCH_EXTRA, tmp->var, tmp->sym);
3050 if (!state)
3051 continue;
3052 set_state(SMATCH_EXTRA, tmp->var, tmp->sym, alloc_estate_whole(estate_type(state)));
3053 } END_FOR_EACH_PTR(tmp);
3054 set_state(link_id, sm->name, sm->sym, &undefined);
3057 void register_smatch_extra_links(int id)
3059 link_id = id;
3060 set_dynamic_states(link_id);
3063 void register_smatch_extra_late(int id)
3065 add_merge_hook(link_id, &merge_link_states);
3066 add_modification_hook(link_id, &match_link_modify);
3067 add_hook(&match_dereferences, DEREF_HOOK);
3068 add_hook(&match_pointer_as_array, OP_HOOK);
3069 select_return_implies_hook_early(DEREFERENCE, &set_param_dereferenced);
3070 add_hook(&match_function_call, FUNCTION_CALL_HOOK);
3071 add_hook(&match_assign, ASSIGNMENT_HOOK);
3072 add_hook(&match_assign, GLOBAL_ASSIGNMENT_HOOK);
3073 add_hook(&unop_expr, OP_HOOK);
3074 add_hook(&asm_expr, ASM_HOOK);
3076 add_caller_info_callback(my_id, caller_info_callback);
3077 add_split_return_callback(&returned_struct_members);
3079 // add_hook(&assume_indexes_are_valid, OP_HOOK);