db/fixup_kernel.sh: fix clear_user() handling
[smatch.git] / smatch_extra.c
blob63708e55637b3fb595d9ece7fe9f115d7c8318d8
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 extern int __no_limits;
172 static void mark_sub_members_gone(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
174 struct sm_state *sm;
176 if (__in_fake_assign)
177 return;
178 if (is_fake_var_assign(expr))
179 return;
181 if (!estate_type(state) || estate_type(state)->type != SYM_PTR)
182 return;
183 if (!is_noderef_ptr_rl(estate_rl(state)))
184 return;
186 FOR_EACH_MY_SM(SMATCH_EXTRA, __get_cur_stree(), sm) {
187 if (sm->sym != sym)
188 continue;
189 if (strcmp(name, sm->name) == 0)
190 continue;
191 if (!is_sub_member(name, sym, sm))
192 continue;
193 __no_limits++;
194 set_extra_nomod(sm->name, sm->sym, NULL, alloc_estate_empty());
195 __no_limits--;
196 } END_FOR_EACH_SM(sm);
199 static bool is_fake_assign(struct expression *expr)
201 struct expression *faked;
203 if (is_fake_var_assign(expr))
204 return true;
206 faked = get_faked_expression();
207 if (!faked || faked->type != EXPR_ASSIGNMENT || faked->op != '=')
208 return false;
210 faked = strip_expr(faked->right);
211 if (faked->type == EXPR_PREOP && faked->op == '&')
212 return true;
214 return false;
217 static void call_update_mtag_data(struct expression *expr,
218 struct smatch_state *state)
220 if (is_fake_assign(expr))
221 return;
223 update_mtag_data(expr, state);
226 static bool in_param_set;
227 void set_extra_mod_helper(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
229 if (!expr)
230 expr = gen_expression_from_name_sym(name, sym);
231 remove_from_equiv(name, sym);
232 set_union_info(name, sym, expr, state);
233 mark_sub_members_gone(name, sym, expr, state);
234 call_extra_mod_hooks(name, sym, expr, state);
235 call_update_mtag_data(expr, state);
236 if ((__in_fake_assign || in_param_set) &&
237 estate_is_unknown(state) && !get_state(SMATCH_EXTRA, name, sym))
238 return;
239 set_state(SMATCH_EXTRA, name, sym, state);
242 static void set_extra_nomod_helper(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
244 call_extra_nomod_hooks(name, sym, expr, state);
245 set_state(SMATCH_EXTRA, name, sym, state);
248 static char *get_pointed_at(const char *name, struct symbol *sym, struct symbol **new_sym)
250 struct expression *assigned;
253 * Imagine we have an assignment: "foo = &addr;" then the other name
254 * of "*foo" is addr.
257 if (name[0] != '*')
258 return NULL;
259 if (strcmp(name + 1, sym->ident->name) != 0)
260 return NULL;
262 assigned = get_assigned_expr_name_sym(sym->ident->name, sym);
263 if (!assigned)
264 return NULL;
265 assigned = strip_parens(assigned);
266 if (assigned->type != EXPR_PREOP || assigned->op != '&')
267 return NULL;
269 return expr_to_var_sym(assigned->unop, new_sym);
272 char *get_other_name_sym_from_chunk(const char *name, const char *chunk, int len, struct symbol *sym, struct symbol **new_sym)
274 struct expression *assigned;
275 char *orig_name = NULL;
276 char buf[256];
277 char *ret;
279 assigned = get_assigned_expr_name_sym(chunk, sym);
280 if (!assigned)
281 return NULL;
282 if (assigned->type == EXPR_CALL)
283 return map_call_to_other_name_sym(name, sym, new_sym);
284 if (assigned->type == EXPR_PREOP && assigned->op == '&') {
286 orig_name = expr_to_var_sym(assigned, new_sym);
287 if (!orig_name || !*new_sym)
288 goto free;
290 snprintf(buf, sizeof(buf), "%s.%s", orig_name + 1, name + len);
291 ret = alloc_string(buf);
292 free_string(orig_name);
293 return ret;
296 orig_name = expr_to_var_sym(assigned, new_sym);
297 if (!orig_name || !*new_sym)
298 goto free;
300 snprintf(buf, sizeof(buf), "%s->%s", orig_name, name + len);
301 ret = alloc_string(buf);
302 free_string(orig_name);
303 return ret;
304 free:
305 free_string(orig_name);
306 return NULL;
309 static char *get_long_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym, bool use_stack)
311 struct expression *orig;
312 struct symbol *orig_sym;
313 char *orig_name, *ret;
315 if (!sym || !sym->ident)
316 return NULL;
318 orig = get_assigned_expr_name_sym(sym->ident->name, sym);
319 if (orig) {
320 orig_name = expr_to_var_sym(orig, &orig_sym);
321 if (!orig_name)
322 return NULL;
324 ret = swap_names(name, sym->ident->name, orig_name);
325 free_string(orig_name);
326 if (ret)
327 *new_sym = orig_sym;
328 return ret;
331 return NULL;
334 char *get_other_name_sym_helper(const char *name, struct symbol *sym, struct symbol **new_sym, bool use_stack)
336 char buf[256];
337 char *ret;
338 int len;
340 *new_sym = NULL;
342 if (!sym || !sym->ident)
343 return NULL;
345 ret = get_pointed_at(name, sym, new_sym);
346 if (ret)
347 return ret;
349 ret = map_long_to_short_name_sym(name, sym, new_sym, use_stack);
350 if (ret)
351 return ret;
353 len = snprintf(buf, sizeof(buf), "%s", name);
354 if (len >= sizeof(buf) - 2)
355 return NULL;
357 while (use_stack && len >= 1) {
358 if (buf[len] == '>' && buf[len - 1] == '-') {
359 len--;
360 buf[len] = '\0';
361 ret = get_other_name_sym_from_chunk(name, buf, len + 2, sym, new_sym);
362 if (ret)
363 return ret;
365 len--;
368 ret = get_long_name_sym(name, sym, new_sym, use_stack);
369 if (ret)
370 return ret;
372 return NULL;
375 char *get_other_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym)
377 return get_other_name_sym_helper(name, sym, new_sym, true);
380 char *get_other_name_sym_nostack(const char *name, struct symbol *sym, struct symbol **new_sym)
382 return get_other_name_sym_helper(name, sym, new_sym, false);
385 void set_extra_mod(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
387 char *new_name;
388 struct symbol *new_sym;
390 set_extra_mod_helper(name, sym, expr, state);
391 new_name = get_other_name_sym_nostack(name, sym, &new_sym);
392 if (new_name && new_sym)
393 set_extra_mod_helper(new_name, new_sym, NULL, state);
394 free_string(new_name);
397 static struct expression *chunk_get_array_base(struct expression *expr)
400 * The problem with is_array() is that it only returns true for things
401 * like foo[1] but not for foo[1].bar.
404 expr = strip_expr(expr);
405 while (expr && expr->type == EXPR_DEREF)
406 expr = strip_expr(expr->deref);
407 return get_array_base(expr);
410 static int chunk_has_array(struct expression *expr)
412 return !!chunk_get_array_base(expr);
415 static void clear_array_states(struct expression *array)
417 struct sm_state *sm;
419 sm = get_sm_state_expr(link_id, array);
420 if (sm)
421 match_link_modify(sm, NULL);
424 static void set_extra_array_mod(struct expression *expr, struct smatch_state *state)
426 struct expression *array;
427 struct var_sym_list *vsl;
428 struct var_sym *vs;
429 char *name;
430 struct symbol *sym;
432 array = chunk_get_array_base(expr);
434 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
435 if (!name || !vsl) {
436 clear_array_states(array);
437 goto free;
440 FOR_EACH_PTR(vsl, vs) {
441 store_link(link_id, vs->var, vs->sym, name, sym);
442 } END_FOR_EACH_PTR(vs);
444 call_extra_mod_hooks(name, sym, expr, state);
445 set_state(SMATCH_EXTRA, name, sym, state);
446 free:
447 free_string(name);
450 void set_extra_expr_mod(struct expression *expr, struct smatch_state *state)
452 struct symbol *sym;
453 char *name;
455 if (chunk_has_array(expr)) {
456 set_extra_array_mod(expr, state);
457 return;
460 expr = strip_expr(expr);
461 name = expr_to_var_sym(expr, &sym);
462 if (!name || !sym)
463 goto free;
464 set_extra_mod(name, sym, expr, state);
465 free:
466 free_string(name);
469 void set_extra_nomod(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
471 char *new_name;
472 struct symbol *new_sym;
473 struct relation *rel;
474 struct smatch_state *orig_state;
476 orig_state = get_state(SMATCH_EXTRA, name, sym);
478 /* don't save unknown states if leaving it blank is the same */
479 if (!orig_state && estate_is_unknown(state))
480 return;
482 new_name = get_other_name_sym(name, sym, &new_sym);
483 if (new_name && new_sym)
484 set_extra_nomod_helper(new_name, new_sym, expr, state);
485 free_string(new_name);
487 if (!estate_related(orig_state)) {
488 set_extra_nomod_helper(name, sym, expr, state);
489 return;
492 set_related(state, estate_related(orig_state));
493 FOR_EACH_PTR(estate_related(orig_state), rel) {
494 struct smatch_state *estate;
496 estate = get_state(SMATCH_EXTRA, rel->name, rel->sym);
497 if (!estate)
498 continue;
499 set_extra_nomod_helper(rel->name, rel->sym, expr, clone_estate_cast(estate_type(estate), state));
500 } END_FOR_EACH_PTR(rel);
503 void set_extra_nomod_vsl(const char *name, struct symbol *sym, struct var_sym_list *vsl, struct expression *expr, struct smatch_state *state)
505 struct var_sym *vs;
507 FOR_EACH_PTR(vsl, vs) {
508 store_link(link_id, vs->var, vs->sym, name, sym);
509 } END_FOR_EACH_PTR(vs);
511 set_extra_nomod(name, sym, expr, state);
515 * This is for return_implies_state() hooks which modify a SMATCH_EXTRA state
517 void set_extra_expr_nomod(struct expression *expr, struct smatch_state *state)
519 struct var_sym_list *vsl;
520 struct var_sym *vs;
521 char *name;
522 struct symbol *sym;
524 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
525 if (!name || !vsl)
526 goto free;
527 FOR_EACH_PTR(vsl, vs) {
528 store_link(link_id, vs->var, vs->sym, name, sym);
529 } END_FOR_EACH_PTR(vs);
531 set_extra_nomod(name, sym, expr, state);
532 free:
533 free_string(name);
536 static void set_extra_true_false(const char *name, struct symbol *sym,
537 struct smatch_state *true_state,
538 struct smatch_state *false_state)
540 char *new_name;
541 struct symbol *new_sym;
542 struct relation *rel;
543 struct smatch_state *orig_state;
545 if (!true_state && !false_state)
546 return;
548 if (in_warn_on_macro())
549 return;
551 new_name = get_other_name_sym(name, sym, &new_sym);
552 if (new_name && new_sym)
553 set_true_false_states(SMATCH_EXTRA, new_name, new_sym, true_state, false_state);
554 free_string(new_name);
556 orig_state = get_state(SMATCH_EXTRA, name, sym);
558 if (!estate_related(orig_state)) {
559 set_true_false_states(SMATCH_EXTRA, name, sym, true_state, false_state);
560 return;
563 if (true_state)
564 set_related(true_state, estate_related(orig_state));
565 if (false_state)
566 set_related(false_state, estate_related(orig_state));
568 FOR_EACH_PTR(estate_related(orig_state), rel) {
569 set_true_false_states(SMATCH_EXTRA, rel->name, rel->sym,
570 true_state, false_state);
571 } END_FOR_EACH_PTR(rel);
574 static void set_extra_chunk_true_false(struct expression *expr,
575 struct smatch_state *true_state,
576 struct smatch_state *false_state)
578 struct var_sym_list *vsl;
579 struct var_sym *vs;
580 struct symbol *type;
581 char *name;
582 struct symbol *sym;
584 if (in_warn_on_macro())
585 return;
587 type = get_type(expr);
588 if (!type)
589 return;
591 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
592 if (!name || !vsl)
593 goto free;
594 FOR_EACH_PTR(vsl, vs) {
595 store_link(link_id, vs->var, vs->sym, name, sym);
596 } END_FOR_EACH_PTR(vs);
598 set_true_false_states(SMATCH_EXTRA, name, sym,
599 clone_estate(true_state),
600 clone_estate(false_state));
601 free:
602 free_string(name);
605 static void set_extra_expr_true_false(struct expression *expr,
606 struct smatch_state *true_state,
607 struct smatch_state *false_state)
609 char *name;
610 struct symbol *sym;
611 sval_t sval;
613 if (!true_state && !false_state)
614 return;
616 if (get_value(expr, &sval))
617 return;
619 expr = strip_expr(expr);
620 name = expr_to_var_sym(expr, &sym);
621 if (!name || !sym) {
622 free_string(name);
623 set_extra_chunk_true_false(expr, true_state, false_state);
624 return;
626 set_extra_true_false(name, sym, true_state, false_state);
627 free_string(name);
630 static int get_countdown_info(struct expression *condition, struct expression **unop, int *op, sval_t *right)
632 struct expression *unop_expr;
633 int comparison;
634 sval_t limit;
636 right->type = &int_ctype;
637 right->value = 0;
639 condition = strip_expr(condition);
641 if (condition->type == EXPR_COMPARE) {
642 comparison = remove_unsigned_from_comparison(condition->op);
644 if (comparison != SPECIAL_GTE && comparison != '>')
645 return 0;
646 if (!get_value(condition->right, &limit))
647 return 0;
649 unop_expr = condition->left;
650 if (unop_expr->type != EXPR_PREOP && unop_expr->type != EXPR_POSTOP)
651 return 0;
652 if (unop_expr->op != SPECIAL_DECREMENT)
653 return 0;
655 *unop = unop_expr;
656 *op = comparison;
657 *right = limit;
659 return 1;
662 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
663 return 0;
664 if (condition->op != SPECIAL_DECREMENT)
665 return 0;
667 *unop = condition;
668 *op = '>';
670 return 1;
673 static struct sm_state *handle_canonical_while_count_down(struct statement *loop)
675 struct expression *iter_var;
676 struct expression *condition, *unop;
677 struct symbol *type;
678 struct sm_state *sm;
679 struct smatch_state *estate;
680 int op;
681 sval_t start, right;
683 right.type = &int_ctype;
684 right.value = 0;
686 condition = strip_expr(loop->iterator_pre_condition);
687 if (!condition)
688 return NULL;
690 if (!get_countdown_info(condition, &unop, &op, &right))
691 return NULL;
693 iter_var = unop->unop;
695 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
696 if (!sm)
697 return NULL;
698 if (sval_cmp(estate_min(sm->state), right) < 0)
699 return NULL;
700 start = estate_max(sm->state);
702 type = get_type(iter_var);
703 right = sval_cast(type, right);
704 start = sval_cast(type, start);
706 if (sval_cmp(start, right) <= 0)
707 return NULL;
708 if (!sval_is_max(start))
709 start.value--;
711 if (op == SPECIAL_GTE)
712 right.value--;
714 if (unop->type == EXPR_PREOP) {
715 right.value++;
716 estate = alloc_estate_range(right, start);
717 if (estate_has_hard_max(sm->state))
718 estate_set_hard_max(estate);
719 estate_copy_fuzzy_max(estate, sm->state);
720 set_extra_expr_mod(iter_var, estate);
722 if (unop->type == EXPR_POSTOP) {
723 estate = alloc_estate_range(right, start);
724 if (estate_has_hard_max(sm->state))
725 estate_set_hard_max(estate);
726 estate_copy_fuzzy_max(estate, sm->state);
727 set_extra_expr_mod(iter_var, estate);
729 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
732 static struct sm_state *handle_canonical_for_inc(struct expression *iter_expr,
733 struct expression *condition)
735 struct expression *iter_var;
736 struct sm_state *sm;
737 struct smatch_state *estate;
738 sval_t start, end, max;
739 bool unknown_end = false;
741 iter_var = iter_expr->unop;
742 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
743 if (!sm)
744 return NULL;
745 if (!estate_get_single_value(sm->state, &start))
746 return NULL;
747 if (!get_implied_max(condition->right, &end)) {
748 end = sval_type_max(get_type(condition->right));
749 end = sval_cast(start.type, end);
750 if (sval_is_max(end))
751 unknown_end = true;
754 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
755 return NULL;
757 switch (condition->op) {
758 case SPECIAL_UNSIGNED_LT:
759 case SPECIAL_NOTEQUAL:
760 case '<':
761 if (!sval_is_min(end) && !unknown_end)
762 end.value--;
763 break;
764 case SPECIAL_UNSIGNED_LTE:
765 case SPECIAL_LTE:
766 break;
767 default:
768 return NULL;
770 if (sval_cmp(end, start) < 0)
771 return NULL;
772 end = sval_cast(start.type, end);
773 estate = alloc_estate_range(start, end);
774 if (get_hard_max(condition->right, &max)) {
775 if (!get_macro_name(condition->pos))
776 estate_set_hard_max(estate);
777 if (condition->op == '<' ||
778 condition->op == SPECIAL_UNSIGNED_LT ||
779 condition->op == SPECIAL_NOTEQUAL)
780 max.value--;
781 max = sval_cast(start.type, max);
782 estate_set_fuzzy_max(estate, max);
784 set_extra_expr_mod(iter_var, estate);
785 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
788 static struct sm_state *handle_canonical_for_dec(struct expression *iter_expr,
789 struct expression *condition)
791 struct expression *iter_var;
792 struct sm_state *sm;
793 struct smatch_state *estate;
794 sval_t start, end;
796 iter_var = iter_expr->unop;
797 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
798 if (!sm)
799 return NULL;
800 if (!estate_get_single_value(sm->state, &start))
801 return NULL;
802 if (!get_implied_min(condition->right, &end))
803 end = sval_type_min(get_type(iter_var));
804 end = sval_cast(estate_type(sm->state), end);
805 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
806 return NULL;
808 switch (condition->op) {
809 case SPECIAL_NOTEQUAL:
810 case '>':
811 if (!sval_is_max(end))
812 end.value++;
813 break;
814 case SPECIAL_GTE:
815 break;
816 default:
817 return NULL;
819 if (sval_cmp(end, start) > 0)
820 return NULL;
821 estate = alloc_estate_range(end, start);
822 estate_set_hard_max(estate);
823 estate_set_fuzzy_max(estate, estate_get_fuzzy_max(estate));
824 set_extra_expr_mod(iter_var, estate);
825 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
828 static struct sm_state *handle_canonical_for_loops(struct statement *loop)
830 struct expression *iter_expr;
831 struct expression *condition;
833 if (!loop->iterator_post_statement)
834 return NULL;
835 if (loop->iterator_post_statement->type != STMT_EXPRESSION)
836 return NULL;
837 iter_expr = loop->iterator_post_statement->expression;
838 if (!loop->iterator_pre_condition)
839 return NULL;
840 if (loop->iterator_pre_condition->type != EXPR_COMPARE)
841 return NULL;
842 condition = loop->iterator_pre_condition;
844 if (iter_expr->op == SPECIAL_INCREMENT)
845 return handle_canonical_for_inc(iter_expr, condition);
846 if (iter_expr->op == SPECIAL_DECREMENT)
847 return handle_canonical_for_dec(iter_expr, condition);
848 return NULL;
851 struct sm_state *__extra_handle_canonical_loops(struct statement *loop, struct stree **stree)
853 struct sm_state *ret;
856 * Canonical loops are a hack. The proper way to handle this is to
857 * use two passes, but unfortunately, doing two passes makes parsing
858 * code twice as slow.
860 * What we do is we set the inside state here, which overwrites whatever
861 * __extra_match_condition() does. Then we set the outside state in
862 * __extra_pre_loop_hook_after().
865 __push_fake_cur_stree();
866 if (!loop->iterator_post_statement)
867 ret = handle_canonical_while_count_down(loop);
868 else
869 ret = handle_canonical_for_loops(loop);
870 *stree = __pop_fake_cur_stree();
871 return ret;
874 int __iterator_unchanged(struct sm_state *sm)
876 if (!sm)
877 return 0;
878 if (get_sm_state(my_id, sm->name, sm->sym) == sm)
879 return 1;
880 return 0;
883 static void while_count_down_after(struct sm_state *sm, struct expression *condition)
885 struct expression *unop;
886 int op;
887 sval_t limit, after_value;
889 if (!get_countdown_info(condition, &unop, &op, &limit))
890 return;
891 after_value = estate_min(sm->state);
892 after_value.value--;
893 set_extra_mod(sm->name, sm->sym, condition->unop, alloc_estate_sval(after_value));
896 void __extra_pre_loop_hook_after(struct sm_state *sm,
897 struct statement *iterator,
898 struct expression *condition)
900 struct expression *iter_expr;
901 sval_t limit;
902 struct smatch_state *state;
903 sval_t end;
905 if (!iterator) {
906 while_count_down_after(sm, condition);
907 return;
910 iter_expr = iterator->expression;
912 if (condition->type != EXPR_COMPARE)
913 return;
915 if (iter_expr->op == SPECIAL_INCREMENT) {
916 if (!get_implied_value(condition->right, &end) &&
917 sval_is_max(estate_max(sm->state)))
918 limit = estate_max(sm->state);
919 else
920 limit = sval_binop(estate_max(sm->state), '+',
921 sval_type_val(estate_type(sm->state), 1));
922 } else {
923 limit = sval_binop(estate_min(sm->state), '-',
924 sval_type_val(estate_type(sm->state), 1));
926 limit = sval_cast(estate_type(sm->state), limit);
927 if (!estate_has_hard_max(sm->state) && !__has_breaks()) {
928 if (iter_expr->op == SPECIAL_INCREMENT)
929 state = alloc_estate_range(estate_min(sm->state), limit);
930 else
931 state = alloc_estate_range(limit, estate_max(sm->state));
932 } else {
933 state = alloc_estate_sval(limit);
935 if (!estate_has_hard_max(sm->state)) {
936 estate_clear_hard_max(state);
938 if (estate_has_fuzzy_max(sm->state)) {
939 sval_t hmax = estate_get_fuzzy_max(sm->state);
940 sval_t max = estate_max(sm->state);
942 if (sval_cmp(hmax, max) != 0)
943 estate_clear_fuzzy_max(state);
944 } else if (!estate_has_fuzzy_max(sm->state)) {
945 estate_clear_fuzzy_max(state);
948 set_extra_mod(sm->name, sm->sym, iter_expr, state);
951 static bool get_global_rl(const char *name, struct symbol *sym, struct range_list **rl)
953 struct expression *expr;
955 if (!sym || !(sym->ctype.modifiers & MOD_TOPLEVEL) || !sym->ident)
956 return false;
957 if (strcmp(sym->ident->name, name) != 0)
958 return false;
960 expr = symbol_expression(sym);
961 return get_implied_rl(expr, rl);
964 static struct stree *unmatched_stree;
965 static struct smatch_state *unmatched_state(struct sm_state *sm)
967 struct smatch_state *state;
968 struct expression *expr;
969 struct range_list *rl;
971 if (unmatched_stree) {
972 state = get_state_stree(unmatched_stree, SMATCH_EXTRA, sm->name, sm->sym);
973 if (state)
974 return state;
976 if (parent_is_gone_var_sym(sm->name, sm->sym))
977 return alloc_estate_empty();
978 if (get_global_rl(sm->name, sm->sym, &rl))
979 return alloc_estate_rl(rl);
981 expr = gen_expression_from_name_sym(sm->name, sm->sym);
982 if (!expr)
983 return alloc_estate_whole(estate_type(sm->state));
984 get_absolute_rl(expr, &rl);
985 return alloc_estate_rl(rl);
988 static void clear_the_pointed_at(struct expression *expr)
990 struct stree *stree;
991 char *name;
992 struct symbol *sym;
993 struct sm_state *tmp;
995 name = expr_to_var_sym(expr, &sym);
996 if (!name || !sym)
997 goto free;
999 stree = __get_cur_stree();
1000 FOR_EACH_MY_SM(SMATCH_EXTRA, stree, tmp) {
1001 if (tmp->name[0] != '*')
1002 continue;
1003 if (tmp->sym != sym)
1004 continue;
1005 if (strcmp(tmp->name + 1, name) != 0)
1006 continue;
1007 set_extra_mod(tmp->name, tmp->sym, expr, alloc_estate_whole(estate_type(tmp->state)));
1008 } END_FOR_EACH_SM(tmp);
1010 free:
1011 free_string(name);
1014 static int is_const_param(struct expression *expr, int param)
1016 struct symbol *type;
1018 type = get_arg_type(expr, param);
1019 if (!type)
1020 return 0;
1021 if (type->ctype.modifiers & MOD_CONST)
1022 return 1;
1023 return 0;
1026 static void match_function_call(struct expression *expr)
1028 struct expression *arg;
1029 struct expression *tmp;
1030 int param = -1;
1032 /* if we have the db this is handled in smatch_function_hooks.c */
1033 if (!option_no_db)
1034 return;
1035 if (inlinable(expr->fn))
1036 return;
1038 FOR_EACH_PTR(expr->args, arg) {
1039 param++;
1040 if (is_const_param(expr->fn, param))
1041 continue;
1042 tmp = strip_expr(arg);
1043 if (tmp->type == EXPR_PREOP && tmp->op == '&')
1044 set_extra_expr_mod(tmp->unop, alloc_estate_whole(get_type(tmp->unop)));
1045 else
1046 clear_the_pointed_at(tmp);
1047 } END_FOR_EACH_PTR(arg);
1050 int values_fit_type(struct expression *left, struct expression *right)
1052 struct range_list *rl;
1053 struct symbol *type;
1055 type = get_type(left);
1056 if (!type)
1057 return 0;
1058 get_absolute_rl(right, &rl);
1059 if (type == rl_type(rl))
1060 return 1;
1061 if (sval_is_negative(rl_min(rl))) {
1062 if (type_unsigned(type))
1063 return 0;
1064 if (sval_cmp(sval_type_min(type), rl_min(rl)) > 0)
1065 return 0;
1067 if (sval_cmp(sval_type_max(type), rl_max(rl)) < 0)
1068 return 0;
1069 return 1;
1072 static void save_chunk_info(struct expression *left, struct expression *right)
1074 struct var_sym_list *vsl;
1075 struct var_sym *vs;
1076 struct expression *add_expr;
1077 struct symbol *type;
1078 sval_t sval;
1079 char *name;
1080 struct symbol *sym;
1082 if (right->type != EXPR_BINOP || right->op != '-')
1083 return;
1084 if (!get_value(right->left, &sval))
1085 return;
1086 if (!expr_to_sym(right->right))
1087 return;
1089 add_expr = binop_expression(left, '+', right->right);
1090 type = get_type(add_expr);
1091 if (!type)
1092 return;
1093 name = expr_to_chunk_sym_vsl(add_expr, &sym, &vsl);
1094 if (!name || !vsl)
1095 goto free;
1096 FOR_EACH_PTR(vsl, vs) {
1097 store_link(link_id, vs->var, vs->sym, name, sym);
1098 } END_FOR_EACH_PTR(vs);
1100 set_state(SMATCH_EXTRA, name, sym, alloc_estate_sval(sval_cast(type, sval)));
1101 free:
1102 free_string(name);
1105 static void do_array_assign(struct expression *left, int op, struct expression *right)
1107 struct range_list *rl;
1109 if (op == '=') {
1110 get_absolute_rl(right, &rl);
1111 rl = cast_rl(get_type(left), rl);
1112 } else {
1113 rl = alloc_whole_rl(get_type(left));
1116 set_extra_array_mod(left, alloc_estate_rl(rl));
1119 static void match_vanilla_assign(struct expression *left, struct expression *right)
1121 struct range_list *orig_rl = NULL;
1122 struct range_list *rl = NULL;
1123 struct symbol *right_sym;
1124 struct symbol *left_type;
1125 struct symbol *right_type;
1126 char *right_name = NULL;
1127 struct symbol *sym;
1128 char *name;
1129 sval_t sval, max;
1130 struct smatch_state *state;
1131 int comparison;
1133 if (is_struct(left))
1134 return;
1136 if (expr_equiv(left, right))
1137 return;
1139 left_type = get_type(left);
1140 right_type = get_type(right);
1141 if (left_type && left_type->type == SYM_ARRAY)
1142 return;
1144 save_chunk_info(left, right);
1146 name = expr_to_var_sym(left, &sym);
1147 if (!name) {
1148 if (chunk_has_array(left))
1149 do_array_assign(left, '=', right);
1150 return;
1153 right_name = expr_to_var_sym(right, &right_sym);
1155 if (!__in_fake_assign &&
1156 !(right->type == EXPR_PREOP && right->op == '&') &&
1157 right_name && right_sym &&
1158 values_fit_type(left, strip_expr(right)) &&
1159 !has_symbol(right, sym)) {
1160 set_equiv(left, right);
1161 goto free;
1164 if (get_implied_value(right, &sval)) {
1165 state = alloc_estate_sval(sval_cast(left_type, sval));
1166 goto done;
1169 if (__in_fake_assign || is_fake_var(left)) {
1170 struct smatch_state *right_state;
1171 struct range_list *rl;
1173 right_state = get_state(SMATCH_EXTRA, right_name, right_sym);
1174 if (right_state) {
1175 state = clone_estate_cast(left_type, right_state);
1176 goto done;
1179 if (get_implied_rl(right, &rl)) {
1180 rl = cast_rl(left_type, rl);
1181 state = alloc_estate_rl(rl);
1182 goto done;
1185 rl = alloc_whole_rl(right_type);
1186 rl = cast_rl(left_type, rl);
1187 state = alloc_estate_rl(rl);
1188 goto done;
1191 comparison = get_comparison_no_extra(left, right);
1192 if (comparison) {
1193 comparison = flip_comparison(comparison);
1194 get_implied_rl(left, &orig_rl);
1197 if (get_implied_rl(right, &rl)) {
1198 rl = cast_rl(left_type, rl);
1199 if (orig_rl)
1200 filter_by_comparison(&rl, comparison, orig_rl);
1201 state = alloc_estate_rl(rl);
1202 if (get_hard_max(right, &max)) {
1203 estate_set_hard_max(state);
1204 estate_set_fuzzy_max(state, max);
1206 } else {
1207 rl = alloc_whole_rl(right_type);
1208 rl = cast_rl(left_type, rl);
1209 if (orig_rl)
1210 filter_by_comparison(&rl, comparison, orig_rl);
1211 state = alloc_estate_rl(rl);
1214 done:
1215 set_extra_mod(name, sym, left, state);
1216 free:
1217 free_string(right_name);
1220 static void match_assign(struct expression *expr)
1222 struct range_list *rl = NULL;
1223 struct expression *left;
1224 struct expression *right;
1225 struct expression *binop_expr;
1226 struct symbol *left_type;
1227 struct symbol *sym;
1228 char *name;
1230 left = strip_expr(expr->left);
1232 right = strip_parens(expr->right);
1233 if (right->type == EXPR_CALL && sym_name_is("__builtin_expect", right->fn))
1234 right = get_argument_from_call_expr(right->args, 0);
1235 while (right->type == EXPR_ASSIGNMENT && right->op == '=')
1236 right = strip_parens(right->left);
1238 if (expr->op == '=' && is_condition(expr->right))
1239 return; /* handled in smatch_condition.c */
1240 if (expr->op == '=' && right->type == EXPR_CALL &&
1241 !is_fake_call(right))
1242 return; /* handled in smatch_function_hooks.c */
1243 if (expr->op == '=') {
1244 match_vanilla_assign(left, right);
1245 return;
1248 name = expr_to_var_sym(left, &sym);
1249 if (!name)
1250 return;
1252 left_type = get_type(left);
1254 switch (expr->op) {
1255 case SPECIAL_ADD_ASSIGN:
1256 case SPECIAL_SUB_ASSIGN:
1257 case SPECIAL_AND_ASSIGN:
1258 case SPECIAL_MOD_ASSIGN:
1259 case SPECIAL_SHL_ASSIGN:
1260 case SPECIAL_SHR_ASSIGN:
1261 case SPECIAL_OR_ASSIGN:
1262 case SPECIAL_XOR_ASSIGN:
1263 case SPECIAL_MUL_ASSIGN:
1264 case SPECIAL_DIV_ASSIGN:
1265 binop_expr = binop_expression(expr->left,
1266 op_remove_assign(expr->op),
1267 expr->right);
1268 get_absolute_rl(binop_expr, &rl);
1269 rl = cast_rl(left_type, rl);
1270 if (inside_loop()) {
1271 if (expr->op == SPECIAL_ADD_ASSIGN)
1272 add_range(&rl, rl_max(rl), sval_type_max(rl_type(rl)));
1274 if (expr->op == SPECIAL_SUB_ASSIGN &&
1275 !sval_is_negative(rl_min(rl))) {
1276 sval_t zero = { .type = rl_type(rl) };
1278 add_range(&rl, rl_min(rl), zero);
1281 set_extra_mod(name, sym, left, alloc_estate_rl(rl));
1282 goto free;
1284 set_extra_mod(name, sym, left, alloc_estate_whole(left_type));
1285 free:
1286 free_string(name);
1289 static struct smatch_state *increment_state(struct smatch_state *state)
1291 sval_t min = estate_min(state);
1292 sval_t max = estate_max(state);
1294 if (!estate_rl(state))
1295 return NULL;
1297 if (inside_loop())
1298 max = sval_type_max(max.type);
1300 if (!sval_is_min(min) && !sval_is_max(min))
1301 min.value++;
1302 if (!sval_is_min(max) && !sval_is_max(max))
1303 max.value++;
1304 return alloc_estate_range(min, max);
1307 static struct smatch_state *decrement_state(struct smatch_state *state)
1309 sval_t min = estate_min(state);
1310 sval_t max = estate_max(state);
1312 if (!estate_rl(state))
1313 return NULL;
1315 if (inside_loop())
1316 min = sval_type_min(min.type);
1318 if (!sval_is_min(min) && !sval_is_max(min))
1319 min.value--;
1320 if (!sval_is_min(max) && !sval_is_max(max))
1321 max.value--;
1322 return alloc_estate_range(min, max);
1325 static void clear_pointed_at_state(struct expression *expr)
1327 struct symbol *type;
1330 * ALERT: This is sort of a mess. If it's is a struct assigment like
1331 * "foo = bar;", then that's handled by smatch_struct_assignment.c.
1332 * the same thing for p++ where "p" is a struct. Most modifications
1333 * are handled by the assignment hook or the db. Smatch_extra.c doesn't
1334 * use smatch_modification.c because we have to get the ordering right
1335 * or something. So if you have p++ where p is a pointer to a standard
1336 * c type then we handle that here. What a mess.
1338 expr = strip_expr(expr);
1339 type = get_type(expr);
1340 if (!type || type->type != SYM_PTR)
1341 return;
1342 type = get_real_base_type(type);
1343 if (!type || type->type != SYM_BASETYPE)
1344 return;
1345 set_extra_expr_nomod(deref_expression(expr), alloc_estate_whole(type));
1348 static void unop_expr(struct expression *expr)
1350 struct smatch_state *state;
1352 if (expr->smatch_flags & Handled)
1353 return;
1355 switch (expr->op) {
1356 case SPECIAL_INCREMENT:
1357 state = get_state_expr(SMATCH_EXTRA, expr->unop);
1358 state = increment_state(state);
1359 if (!state)
1360 state = alloc_estate_whole(get_type(expr));
1361 set_extra_expr_mod(expr->unop, state);
1362 clear_pointed_at_state(expr->unop);
1363 break;
1364 case SPECIAL_DECREMENT:
1365 state = get_state_expr(SMATCH_EXTRA, expr->unop);
1366 state = decrement_state(state);
1367 if (!state)
1368 state = alloc_estate_whole(get_type(expr));
1369 set_extra_expr_mod(expr->unop, state);
1370 clear_pointed_at_state(expr->unop);
1371 break;
1372 default:
1373 return;
1377 static void asm_expr(struct statement *stmt)
1379 struct asm_operand *op;
1380 struct symbol *type;
1382 FOR_EACH_PTR(stmt->asm_outputs, op) {
1383 type = get_type(strip_expr(op->expr));
1384 set_extra_expr_mod(op->expr, alloc_estate_whole(type));
1385 } END_FOR_EACH_PTR(op);
1388 static void check_dereference(struct expression *expr)
1390 struct smatch_state *state;
1392 if (__in_fake_assign)
1393 return;
1394 if (outside_of_function())
1395 return;
1396 state = get_extra_state(expr);
1397 if (state) {
1398 struct range_list *rl;
1400 rl = rl_intersection(estate_rl(state), valid_ptr_rl);
1401 if (rl_equiv(rl, estate_rl(state)))
1402 return;
1403 set_extra_expr_nomod(expr, alloc_estate_rl(rl));
1404 } else {
1405 struct range_list *rl;
1407 if (get_mtag_rl(expr, &rl))
1408 rl = rl_intersection(rl, valid_ptr_rl);
1409 else
1410 rl = clone_rl(valid_ptr_rl);
1412 __no_limits++;
1413 set_extra_expr_nomod(expr, alloc_estate_rl(rl));
1414 __no_limits--;
1418 static void match_dereferences(struct expression *expr)
1420 if (expr->type != EXPR_PREOP)
1421 return;
1422 if (getting_address(expr))
1423 return;
1424 /* it's saying that foo[1] = bar dereferences foo[1] */
1425 if (is_array(expr))
1426 return;
1427 check_dereference(expr->unop);
1430 static void match_pointer_as_array(struct expression *expr)
1432 if (!is_array(expr))
1433 return;
1434 check_dereference(get_array_base(expr));
1437 static void find_dereferences(struct expression *expr)
1439 while (expr->type == EXPR_PREOP) {
1440 if (expr->op == '*')
1441 check_dereference(expr->unop);
1442 expr = strip_expr(expr->unop);
1446 static void set_param_dereferenced(struct expression *call, struct expression *arg, char *key, char *unused)
1448 struct symbol *sym;
1449 char *name;
1451 if (strcmp(key, "$") == 0 && arg->type == EXPR_PREOP && arg->op == '&') {
1452 struct expression *tmp;
1454 tmp = strip_expr(arg->unop);
1455 if (tmp->type == EXPR_DEREF) {
1456 tmp = strip_expr(tmp->deref);
1457 if (tmp->type == EXPR_PREOP && tmp->op == '*')
1458 arg = strip_expr(tmp->unop);
1462 name = get_variable_from_key(arg, key, &sym);
1463 if (name && sym) {
1464 struct smatch_state *orig, *new;
1465 struct range_list *rl;
1467 orig = get_state(SMATCH_EXTRA, name, sym);
1468 if (orig) {
1469 rl = rl_intersection(estate_rl(orig),
1470 alloc_rl(valid_ptr_min_sval,
1471 valid_ptr_max_sval));
1472 new = alloc_estate_rl(rl);
1473 } else {
1474 new = alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval);
1477 if (!orig)
1478 __no_limits++;
1479 set_extra_nomod(name, sym, NULL, new);
1480 if (!orig)
1481 __no_limits--;
1483 free_string(name);
1485 find_dereferences(arg);
1488 static sval_t add_one(sval_t sval)
1490 sval.value++;
1491 return sval;
1494 static int handle_postop_inc(struct expression *left, int op, struct expression *right)
1496 struct statement *stmt;
1497 struct expression *cond;
1498 struct smatch_state *true_state, *false_state;
1499 struct symbol *type;
1500 sval_t start;
1501 sval_t limit;
1504 * If we're decrementing here then that's a canonical while count down
1505 * so it's handled already. We're only handling loops like:
1506 * i = 0;
1507 * do { ... } while (i++ < 3);
1510 if (left->type != EXPR_POSTOP || left->op != SPECIAL_INCREMENT)
1511 return 0;
1513 stmt = __cur_stmt->parent;
1514 if (!stmt)
1515 return 0;
1516 if (stmt->type == STMT_COMPOUND)
1517 stmt = stmt->parent;
1518 if (!stmt || stmt->type != STMT_ITERATOR || !stmt->iterator_post_condition)
1519 return 0;
1521 cond = strip_expr(stmt->iterator_post_condition);
1522 if (cond->type != EXPR_COMPARE || cond->op != op)
1523 return 0;
1524 if (left != strip_expr(cond->left) || right != strip_expr(cond->right))
1525 return 0;
1527 if (!get_implied_value(left->unop, &start))
1528 return 0;
1529 if (!get_implied_value(right, &limit))
1530 return 0;
1531 type = get_type(left->unop);
1532 limit = sval_cast(type, limit);
1533 if (sval_cmp(start, limit) > 0)
1534 return 0;
1536 switch (op) {
1537 case '<':
1538 case SPECIAL_UNSIGNED_LT:
1539 break;
1540 case SPECIAL_LTE:
1541 case SPECIAL_UNSIGNED_LTE:
1542 limit = add_one(limit);
1543 default:
1544 return 0;
1548 true_state = alloc_estate_range(add_one(start), limit);
1549 false_state = alloc_estate_range(add_one(limit), add_one(limit));
1551 /* Currently we just discard the false state but when two passes is
1552 * implimented correctly then it will use it.
1555 set_extra_expr_true_false(left->unop, true_state, false_state);
1557 return 1;
1560 bool is_impossible_variable(struct expression *expr)
1562 struct smatch_state *state;
1564 state = get_extra_state(expr);
1565 if (state && !estate_rl(state))
1566 return true;
1567 return false;
1570 static bool in_macro(struct expression *left, struct expression *right)
1572 if (!left || !right)
1573 return 0;
1574 if (left->pos.line != right->pos.line || left->pos.pos != right->pos.pos)
1575 return 0;
1576 if (get_macro_name(left->pos))
1577 return 1;
1578 return 0;
1581 static void handle_comparison(struct symbol *type, struct expression *left, int op, struct expression *right)
1583 struct smatch_state *left_state_orig, *right_state_orig;
1584 struct range_list *left_orig;
1585 struct range_list *left_true;
1586 struct range_list *left_false;
1587 struct range_list *right_orig;
1588 struct range_list *right_true;
1589 struct range_list *right_false;
1590 struct smatch_state *left_true_state;
1591 struct smatch_state *left_false_state;
1592 struct smatch_state *right_true_state;
1593 struct smatch_state *right_false_state;
1594 sval_t dummy, hard_max;
1595 int left_postop = 0;
1596 int right_postop = 0;
1598 if (left->op == SPECIAL_INCREMENT || left->op == SPECIAL_DECREMENT) {
1599 if (left->type == EXPR_POSTOP) {
1600 left->smatch_flags |= Handled;
1601 left_postop = left->op;
1602 if (handle_postop_inc(left, op, right))
1603 return;
1605 left = strip_parens(left->unop);
1607 while (left->type == EXPR_ASSIGNMENT)
1608 left = strip_parens(left->left);
1610 if (right->op == SPECIAL_INCREMENT || right->op == SPECIAL_DECREMENT) {
1611 if (right->type == EXPR_POSTOP) {
1612 right->smatch_flags |= Handled;
1613 right_postop = right->op;
1615 right = strip_parens(right->unop);
1618 if (is_impossible_variable(left) || is_impossible_variable(right))
1619 return;
1621 left_state_orig = get_extra_state(left);
1622 get_real_absolute_rl(left, &left_orig);
1623 left_orig = cast_rl(type, left_orig);
1625 right_state_orig = get_extra_state(right);
1626 get_real_absolute_rl(right, &right_orig);
1627 right_orig = cast_rl(type, right_orig);
1629 split_comparison_rl(left_orig, op, right_orig, &left_true, &left_false, &right_true, &right_false);
1631 left_true = rl_truncate_cast(get_type(strip_expr(left)), left_true);
1632 left_false = rl_truncate_cast(get_type(strip_expr(left)), left_false);
1633 right_true = rl_truncate_cast(get_type(strip_expr(right)), right_true);
1634 right_false = rl_truncate_cast(get_type(strip_expr(right)), right_false);
1636 if (!left_true || !left_false) {
1637 struct range_list *tmp_true, *tmp_false;
1639 split_comparison_rl(alloc_whole_rl(type), op, right_orig, &tmp_true, &tmp_false, NULL, NULL);
1640 tmp_true = rl_truncate_cast(get_type(strip_expr(left)), tmp_true);
1641 tmp_false = rl_truncate_cast(get_type(strip_expr(left)), tmp_false);
1642 if (tmp_true && tmp_false)
1643 __save_imaginary_state(left, tmp_true, tmp_false);
1646 if (!right_true || !right_false) {
1647 struct range_list *tmp_true, *tmp_false;
1649 split_comparison_rl(alloc_whole_rl(type), op, right_orig, NULL, NULL, &tmp_true, &tmp_false);
1650 tmp_true = rl_truncate_cast(get_type(strip_expr(right)), tmp_true);
1651 tmp_false = rl_truncate_cast(get_type(strip_expr(right)), tmp_false);
1652 if (tmp_true && tmp_false)
1653 __save_imaginary_state(right, tmp_true, tmp_false);
1656 left_true_state = alloc_estate_rl(left_true);
1657 left_false_state = alloc_estate_rl(left_false);
1658 right_true_state = alloc_estate_rl(right_true);
1659 right_false_state = alloc_estate_rl(right_false);
1661 switch (op) {
1662 case '<':
1663 case SPECIAL_UNSIGNED_LT:
1664 case SPECIAL_UNSIGNED_LTE:
1665 case SPECIAL_LTE:
1666 if (get_hard_max(right, &dummy) && !in_macro(left, right))
1667 estate_set_hard_max(left_true_state);
1668 if (get_hard_max(left, &dummy) && !in_macro(left, right))
1669 estate_set_hard_max(right_false_state);
1670 break;
1671 case '>':
1672 case SPECIAL_UNSIGNED_GT:
1673 case SPECIAL_UNSIGNED_GTE:
1674 case SPECIAL_GTE:
1675 if (get_hard_max(left, &dummy) && !in_macro(left, right))
1676 estate_set_hard_max(right_true_state);
1677 if (get_hard_max(right, &dummy) && !in_macro(left, right))
1678 estate_set_hard_max(left_false_state);
1679 break;
1682 switch (op) {
1683 case '<':
1684 case SPECIAL_UNSIGNED_LT:
1685 case SPECIAL_UNSIGNED_LTE:
1686 case SPECIAL_LTE:
1687 if (get_hard_max(right, &hard_max)) {
1688 if (op == '<' || op == SPECIAL_UNSIGNED_LT)
1689 hard_max.value--;
1690 estate_set_fuzzy_max(left_true_state, hard_max);
1692 if (get_implied_value(right, &hard_max)) {
1693 if (op == SPECIAL_UNSIGNED_LTE ||
1694 op == SPECIAL_LTE)
1695 hard_max.value++;
1696 estate_set_fuzzy_max(left_false_state, hard_max);
1698 if (get_hard_max(left, &hard_max)) {
1699 if (op == SPECIAL_UNSIGNED_LTE ||
1700 op == SPECIAL_LTE)
1701 hard_max.value--;
1702 estate_set_fuzzy_max(right_false_state, hard_max);
1704 if (get_implied_value(left, &hard_max)) {
1705 if (op == '<' || op == SPECIAL_UNSIGNED_LT)
1706 hard_max.value++;
1707 estate_set_fuzzy_max(right_true_state, hard_max);
1709 break;
1710 case '>':
1711 case SPECIAL_UNSIGNED_GT:
1712 case SPECIAL_UNSIGNED_GTE:
1713 case SPECIAL_GTE:
1714 if (get_hard_max(left, &hard_max)) {
1715 if (op == '>' || op == SPECIAL_UNSIGNED_GT)
1716 hard_max.value--;
1717 estate_set_fuzzy_max(right_true_state, hard_max);
1719 if (get_implied_value(left, &hard_max)) {
1720 if (op == SPECIAL_UNSIGNED_GTE ||
1721 op == SPECIAL_GTE)
1722 hard_max.value++;
1723 estate_set_fuzzy_max(right_false_state, hard_max);
1725 if (get_hard_max(right, &hard_max)) {
1726 if (op == SPECIAL_UNSIGNED_LTE ||
1727 op == SPECIAL_LTE)
1728 hard_max.value--;
1729 estate_set_fuzzy_max(left_false_state, hard_max);
1731 if (get_implied_value(right, &hard_max)) {
1732 if (op == '>' ||
1733 op == SPECIAL_UNSIGNED_GT)
1734 hard_max.value++;
1735 estate_set_fuzzy_max(left_true_state, hard_max);
1737 break;
1738 case SPECIAL_EQUAL:
1739 if (get_hard_max(left, &hard_max))
1740 estate_set_fuzzy_max(right_true_state, hard_max);
1741 if (get_hard_max(right, &hard_max))
1742 estate_set_fuzzy_max(left_true_state, hard_max);
1743 break;
1746 if (get_hard_max(left, &hard_max)) {
1747 estate_set_hard_max(left_true_state);
1748 estate_set_hard_max(left_false_state);
1750 if (get_hard_max(right, &hard_max)) {
1751 estate_set_hard_max(right_true_state);
1752 estate_set_hard_max(right_false_state);
1755 if (left_postop == SPECIAL_INCREMENT) {
1756 left_true_state = increment_state(left_true_state);
1757 left_false_state = increment_state(left_false_state);
1759 if (left_postop == SPECIAL_DECREMENT) {
1760 left_true_state = decrement_state(left_true_state);
1761 left_false_state = decrement_state(left_false_state);
1763 if (right_postop == SPECIAL_INCREMENT) {
1764 right_true_state = increment_state(right_true_state);
1765 right_false_state = increment_state(right_false_state);
1767 if (right_postop == SPECIAL_DECREMENT) {
1768 right_true_state = decrement_state(right_true_state);
1769 right_false_state = decrement_state(right_false_state);
1772 if (estate_rl(left_true_state) && estates_equiv(left_true_state, left_false_state)) {
1773 left_true_state = NULL;
1774 left_false_state = NULL;
1777 if (estate_rl(right_true_state) && estates_equiv(right_true_state, right_false_state)) {
1778 right_true_state = NULL;
1779 right_false_state = NULL;
1782 /* Don't introduce new states for known true/false conditions */
1783 if (rl_equiv(estate_rl(left_state_orig), estate_rl(left_true_state)))
1784 left_true_state = NULL;
1785 if (rl_equiv(estate_rl(left_state_orig), estate_rl(left_false_state)))
1786 left_false_state = NULL;
1787 if (rl_equiv(estate_rl(right_state_orig), estate_rl(right_true_state)))
1788 right_true_state = NULL;
1789 if (rl_equiv(estate_rl(right_state_orig), estate_rl(right_false_state)))
1790 right_false_state = NULL;
1792 set_extra_expr_true_false(left, left_true_state, left_false_state);
1793 set_extra_expr_true_false(right, right_true_state, right_false_state);
1796 static int is_simple_math(struct expression *expr)
1798 if (!expr)
1799 return 0;
1800 if (expr->type != EXPR_BINOP)
1801 return 0;
1802 switch (expr->op) {
1803 case '+':
1804 case '-':
1805 case '*':
1806 return 1;
1808 return 0;
1811 static int flip_op(int op)
1813 /* We only care about simple math */
1814 switch (op) {
1815 case '+':
1816 return '-';
1817 case '-':
1818 return '+';
1819 case '*':
1820 return '/';
1822 return 0;
1825 static void move_known_to_rl(struct expression **expr_p, struct range_list **rl_p)
1827 struct expression *expr = *expr_p;
1828 struct range_list *rl = *rl_p;
1829 sval_t sval;
1831 if (!is_simple_math(expr))
1832 return;
1834 if (get_implied_value(expr->right, &sval)) {
1835 *expr_p = expr->left;
1836 *rl_p = rl_binop(rl, flip_op(expr->op), alloc_rl(sval, sval));
1837 move_known_to_rl(expr_p, rl_p);
1838 return;
1840 if (expr->op == '-')
1841 return;
1842 if (get_implied_value(expr->left, &sval)) {
1843 *expr_p = expr->right;
1844 *rl_p = rl_binop(rl, flip_op(expr->op), alloc_rl(sval, sval));
1845 move_known_to_rl(expr_p, rl_p);
1846 return;
1850 static void move_known_values(struct expression **left_p, struct expression **right_p)
1852 struct expression *left = *left_p;
1853 struct expression *right = *right_p;
1854 sval_t sval, dummy;
1856 if (get_implied_value(left, &sval)) {
1857 if (!is_simple_math(right))
1858 return;
1859 if (get_implied_value(right, &dummy))
1860 return;
1861 if (right->op == '*') {
1862 sval_t divisor;
1864 if (!get_value(right->right, &divisor))
1865 return;
1866 if (divisor.value == 0)
1867 return;
1868 *left_p = binop_expression(left, invert_op(right->op), right->right);
1869 *right_p = right->left;
1870 return;
1872 if (right->op == '+' && get_value(right->left, &sval)) {
1873 *left_p = binop_expression(left, invert_op(right->op), right->left);
1874 *right_p = right->right;
1875 return;
1877 if (get_value(right->right, &sval)) {
1878 *left_p = binop_expression(left, invert_op(right->op), right->right);
1879 *right_p = right->left;
1880 return;
1882 return;
1884 if (get_implied_value(right, &sval)) {
1885 if (!is_simple_math(left))
1886 return;
1887 if (get_implied_value(left, &dummy))
1888 return;
1889 if (left->op == '*') {
1890 sval_t divisor;
1892 if (!get_value(left->right, &divisor))
1893 return;
1894 if (divisor.value == 0)
1895 return;
1896 *right_p = binop_expression(right, invert_op(left->op), left->right);
1897 *left_p = left->left;
1898 return;
1900 if (left->op == '+' && get_value(left->left, &sval)) {
1901 *right_p = binop_expression(right, invert_op(left->op), left->left);
1902 *left_p = left->right;
1903 return;
1906 if (get_value(left->right, &sval)) {
1907 *right_p = binop_expression(right, invert_op(left->op), left->right);
1908 *left_p = left->left;
1909 return;
1911 return;
1916 * The reason for do_simple_algebra() is to solve things like:
1917 * if (foo > 66 || foo + bar > 64) {
1918 * "foo" is not really a known variable so it won't be handled by
1919 * move_known_variables() but it's a super common idiom.
1922 static int do_simple_algebra(struct expression **left_p, struct expression **right_p)
1924 struct expression *left = *left_p;
1925 struct expression *right = *right_p;
1926 struct range_list *rl;
1927 sval_t tmp;
1929 if (left->type != EXPR_BINOP || left->op != '+')
1930 return 0;
1931 if (can_integer_overflow(get_type(left), left))
1932 return 0;
1933 if (!get_implied_value(right, &tmp))
1934 return 0;
1936 if (!get_implied_value(left->left, &tmp) &&
1937 get_implied_rl(left->left, &rl) &&
1938 !is_whole_rl(rl)) {
1939 *right_p = binop_expression(right, '-', left->left);
1940 *left_p = left->right;
1941 return 1;
1943 if (!get_implied_value(left->right, &tmp) &&
1944 get_implied_rl(left->right, &rl) &&
1945 !is_whole_rl(rl)) {
1946 *right_p = binop_expression(right, '-', left->right);
1947 *left_p = left->left;
1948 return 1;
1951 return 0;
1954 static int match_func_comparison(struct expression *expr)
1956 struct expression *left = strip_expr(expr->left);
1957 struct expression *right = strip_expr(expr->right);
1959 if (left->type == EXPR_CALL || right->type == EXPR_CALL) {
1960 // TODO: faked_assign this should be handled as a fake assignment instead
1961 function_comparison(left, expr->op, right);
1962 return 1;
1965 return 0;
1968 /* Handle conditions like "if (foo + bar < foo) {" */
1969 static int handle_integer_overflow_test(struct expression *expr)
1971 struct expression *left, *right;
1972 struct symbol *type;
1973 sval_t left_min, right_min, min, max;
1975 if (expr->op != '<' && expr->op != SPECIAL_UNSIGNED_LT)
1976 return 0;
1978 left = strip_parens(expr->left);
1979 right = strip_parens(expr->right);
1981 if (left->op != '+')
1982 return 0;
1984 type = get_type(expr);
1985 if (!type)
1986 return 0;
1987 if (type_positive_bits(type) == 32) {
1988 max.type = &uint_ctype;
1989 max.uvalue = (unsigned int)-1;
1990 } else if (type_positive_bits(type) == 64) {
1991 max.type = &ulong_ctype;
1992 max.value = (unsigned long long)-1;
1993 } else {
1994 return 0;
1997 if (!expr_equiv(left->left, right) && !expr_equiv(left->right, right))
1998 return 0;
2000 get_absolute_min(left->left, &left_min);
2001 get_absolute_min(left->right, &right_min);
2002 min = sval_binop(left_min, '+', right_min);
2004 type = get_type(left);
2005 min = sval_cast(type, min);
2006 max = sval_cast(type, max);
2008 set_extra_chunk_true_false(left, NULL, alloc_estate_range(min, max));
2009 return 1;
2012 static void match_comparison(struct expression *expr)
2014 struct expression *left_orig = strip_parens(expr->left);
2015 struct expression *right_orig = strip_parens(expr->right);
2016 struct expression *left, *right, *tmp;
2017 struct expression *prev;
2018 struct symbol *type;
2019 int redo, count;
2021 if (match_func_comparison(expr))
2022 return;
2024 type = get_type(expr);
2025 if (!type)
2026 type = &llong_ctype;
2028 if (handle_integer_overflow_test(expr))
2029 return;
2031 left = left_orig;
2032 right = right_orig;
2033 move_known_values(&left, &right);
2034 handle_comparison(type, left, expr->op, right);
2036 left = left_orig;
2037 right = right_orig;
2038 if (do_simple_algebra(&left, &right))
2039 handle_comparison(type, left, expr->op, right);
2041 prev = get_assigned_expr(left_orig);
2042 if (is_simple_math(prev) && !has_variable(prev, left_orig)) {
2043 left = prev;
2044 right = right_orig;
2045 move_known_values(&left, &right);
2046 handle_comparison(type, left, expr->op, right);
2049 prev = get_assigned_expr(right_orig);
2050 if (is_simple_math(prev) && !has_variable(prev, right_orig)) {
2051 left = left_orig;
2052 right = prev;
2053 move_known_values(&left, &right);
2054 handle_comparison(type, left, expr->op, right);
2057 redo = 0;
2058 left = left_orig;
2059 right = right_orig;
2060 if (get_last_expr_from_expression_stmt(left_orig)) {
2061 left = get_last_expr_from_expression_stmt(left_orig);
2062 redo = 1;
2064 if (get_last_expr_from_expression_stmt(right_orig)) {
2065 right = get_last_expr_from_expression_stmt(right_orig);
2066 redo = 1;
2069 if (!redo)
2070 return;
2072 count = 0;
2073 while ((tmp = get_assigned_expr(left))) {
2074 if (count++ > 3)
2075 break;
2076 left = strip_expr(tmp);
2078 count = 0;
2079 while ((tmp = get_assigned_expr(right))) {
2080 if (count++ > 3)
2081 break;
2082 right = strip_expr(tmp);
2085 handle_comparison(type, left, expr->op, right);
2088 static sval_t get_high_mask(sval_t known)
2090 sval_t ret;
2091 int i;
2093 ret = known;
2094 ret.value = 0;
2096 for (i = type_bits(known.type) - 1; i >= 0; i--) {
2097 if (known.uvalue & (1ULL << i))
2098 ret.uvalue |= (1ULL << i);
2099 else
2100 return ret;
2103 return ret;
2106 static bool handle_bit_test(struct expression *expr)
2108 struct range_list *orig_rl, *rlt, *rlf, *true_rl, *false_rl;
2109 struct expression *shift, *mask, *var;
2110 struct bit_info *bit_info;
2111 sval_t sval;
2112 sval_t high = { .type = &int_ctype };
2113 sval_t low = { .type = &int_ctype };
2115 shift = strip_expr(expr->right);
2116 mask = strip_expr(expr->left);
2117 if (shift->type != EXPR_BINOP || shift->op != SPECIAL_LEFTSHIFT) {
2118 shift = strip_expr(expr->left);
2119 mask = strip_expr(expr->right);
2120 if (shift->type != EXPR_BINOP || shift->op != SPECIAL_LEFTSHIFT)
2121 return false;
2123 if (!get_implied_value(shift->left, &sval) || sval.value != 1)
2124 return false;
2125 var = strip_expr(shift->right);
2127 bit_info = get_bit_info(mask);
2128 if (!bit_info)
2129 return false;
2130 if (!bit_info->possible){
2131 set_true_false_states_expr(my_id, var, alloc_estate_empty(), NULL);
2132 return false;
2135 get_absolute_rl(var, &orig_rl);
2136 if (sval_is_negative(rl_min(orig_rl)) ||
2137 rl_max(orig_rl).uvalue > type_bits(get_type(shift->left)))
2138 return false;
2140 low.value = ffsll(bit_info->possible) - 1;
2141 high.value = sm_fls64(bit_info->possible) - 1;
2142 rlt = alloc_rl(low, high);
2143 rlt = cast_rl(get_type(var), rlt);
2144 true_rl = rl_intersection(orig_rl, rlt);
2146 low.value = ffsll(bit_info->set) - 1;
2147 high.value = sm_fls64(bit_info->set) - 1;
2148 rlf = alloc_rl(low, high);
2149 rlf = cast_rl(get_type(var), rlf);
2150 false_rl = rl_filter(orig_rl, rlf);
2152 set_extra_expr_true_false(var, alloc_estate_rl(true_rl), alloc_estate_rl(false_rl));
2154 return true;
2157 static void handle_AND_op(struct expression *var, sval_t known)
2159 struct range_list *orig_rl;
2160 struct range_list *true_rl = NULL;
2161 struct range_list *false_rl = NULL;
2162 int bit;
2163 sval_t low_mask = known;
2164 sval_t high_mask;
2165 sval_t max;
2167 get_absolute_rl(var, &orig_rl);
2169 if (known.value > 0) {
2170 bit = ffsll(known.value) - 1;
2171 low_mask.uvalue = (1ULL << bit) - 1;
2172 true_rl = remove_range(orig_rl, sval_type_val(known.type, 0), low_mask);
2174 high_mask = get_high_mask(known);
2175 if (high_mask.value) {
2176 bit = ffsll(high_mask.value) - 1;
2177 low_mask.uvalue = (1ULL << bit) - 1;
2179 false_rl = orig_rl;
2180 if (sval_is_negative(rl_min(orig_rl)))
2181 false_rl = remove_range(false_rl, sval_type_min(known.type), sval_type_val(known.type, -1));
2182 false_rl = remove_range(false_rl, low_mask, sval_type_max(known.type));
2183 if (type_signed(high_mask.type) && type_unsigned(rl_type(false_rl))) {
2184 false_rl = remove_range(false_rl,
2185 sval_type_val(rl_type(false_rl), sval_type_max(known.type).uvalue),
2186 sval_type_val(rl_type(false_rl), -1));
2188 } else if (known.value == 1 &&
2189 get_hard_max(var, &max) &&
2190 sval_cmp(max, rl_max(orig_rl)) == 0 &&
2191 max.value & 1) {
2192 false_rl = remove_range(orig_rl, max, max);
2194 set_extra_expr_true_false(var,
2195 true_rl ? alloc_estate_rl(true_rl) : NULL,
2196 false_rl ? alloc_estate_rl(false_rl) : NULL);
2199 static void handle_AND_condition(struct expression *expr)
2201 sval_t known;
2203 if (handle_bit_test(expr))
2204 return;
2206 if (get_implied_value(expr->left, &known))
2207 handle_AND_op(expr->right, known);
2208 else if (get_implied_value(expr->right, &known))
2209 handle_AND_op(expr->left, known);
2212 static void handle_MOD_condition(struct expression *expr)
2214 struct range_list *orig_rl;
2215 struct range_list *true_rl;
2216 struct range_list *false_rl = NULL;
2217 sval_t right;
2218 sval_t zero = {
2219 .value = 0,
2222 if (!get_implied_value(expr->right, &right) || right.value == 0)
2223 return;
2224 get_absolute_rl(expr->left, &orig_rl);
2226 zero.type = rl_type(orig_rl);
2228 /* We're basically dorking around the min and max here */
2229 true_rl = remove_range(orig_rl, zero, zero);
2230 if (!sval_is_max(rl_max(true_rl)) &&
2231 !(rl_max(true_rl).value % right.value))
2232 true_rl = remove_range(true_rl, rl_max(true_rl), rl_max(true_rl));
2234 if (rl_equiv(true_rl, orig_rl))
2235 true_rl = NULL;
2237 if (sval_is_positive(rl_min(orig_rl)) &&
2238 (rl_max(orig_rl).value - rl_min(orig_rl).value) / right.value < 5) {
2239 sval_t add;
2240 int i;
2242 add = rl_min(orig_rl);
2243 add.value += right.value - (add.value % right.value);
2244 add.value -= right.value;
2246 for (i = 0; i < 5; i++) {
2247 add.value += right.value;
2248 if (add.value > rl_max(orig_rl).value)
2249 break;
2250 add_range(&false_rl, add, add);
2252 } else {
2253 if (rl_min(orig_rl).uvalue != 0 &&
2254 rl_min(orig_rl).uvalue < right.uvalue) {
2255 sval_t chop = right;
2256 chop.value--;
2257 false_rl = remove_range(orig_rl, zero, chop);
2260 if (!sval_is_max(rl_max(orig_rl)) &&
2261 (rl_max(orig_rl).value % right.value)) {
2262 sval_t chop = rl_max(orig_rl);
2263 chop.value -= chop.value % right.value;
2264 chop.value++;
2265 if (!false_rl)
2266 false_rl = clone_rl(orig_rl);
2267 false_rl = remove_range(false_rl, chop, rl_max(orig_rl));
2271 set_extra_expr_true_false(expr->left,
2272 true_rl ? alloc_estate_rl(true_rl) : NULL,
2273 false_rl ? alloc_estate_rl(false_rl) : NULL);
2276 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
2277 void __extra_match_condition(struct expression *expr)
2279 expr = strip_expr(expr);
2280 switch (expr->type) {
2281 case EXPR_CALL:
2282 function_comparison(expr, SPECIAL_NOTEQUAL, zero_expr());
2283 return;
2284 case EXPR_PREOP:
2285 case EXPR_SYMBOL:
2286 case EXPR_DEREF:
2287 handle_comparison(get_type(expr), expr, SPECIAL_NOTEQUAL, zero_expr());
2288 return;
2289 case EXPR_COMPARE:
2290 match_comparison(expr);
2291 return;
2292 case EXPR_ASSIGNMENT:
2293 __extra_match_condition(expr->left);
2294 return;
2295 case EXPR_BINOP:
2296 if (expr->op == '&')
2297 handle_AND_condition(expr);
2298 if (expr->op == '%')
2299 handle_MOD_condition(expr);
2300 return;
2304 static void assume_indexes_are_valid(struct expression *expr)
2306 struct expression *array_expr;
2307 int array_size;
2308 struct expression *offset;
2309 struct symbol *offset_type;
2310 struct range_list *rl_before;
2311 struct range_list *rl_after;
2312 struct range_list *filter = NULL;
2313 sval_t size;
2315 expr = strip_expr(expr);
2316 if (!is_array(expr))
2317 return;
2319 offset = get_array_offset(expr);
2320 offset_type = get_type(offset);
2321 if (offset_type && type_signed(offset_type)) {
2322 filter = alloc_rl(sval_type_min(offset_type),
2323 sval_type_val(offset_type, -1));
2326 array_expr = get_array_base(expr);
2327 array_size = get_real_array_size(array_expr);
2328 if (array_size > 1) {
2329 size = sval_type_val(offset_type, array_size);
2330 add_range(&filter, size, sval_type_max(offset_type));
2333 if (!filter)
2334 return;
2335 get_absolute_rl(offset, &rl_before);
2336 rl_after = rl_filter(rl_before, filter);
2337 if (rl_equiv(rl_before, rl_after))
2338 return;
2339 set_extra_expr_nomod(offset, alloc_estate_rl(rl_after));
2342 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
2343 int implied_not_equal(struct expression *expr, long long val)
2345 return !possibly_false(expr, SPECIAL_NOTEQUAL, value_expr(val));
2348 int implied_not_equal_name_sym(char *name, struct symbol *sym, long long val)
2350 struct smatch_state *estate;
2352 estate = get_state(SMATCH_EXTRA, name, sym);
2353 if (!estate)
2354 return 0;
2355 if (!rl_has_sval(estate_rl(estate), sval_type_val(estate_type(estate), 0)))
2356 return 1;
2357 return 0;
2360 bool is_noderef_ptr(struct expression *expr)
2362 struct range_list *rl;
2364 if (!get_implied_rl(expr, &rl))
2365 return false;
2366 return is_noderef_ptr_rl(rl);
2369 static int parent_is_err_or_null_var_sym_helper(const char *name, struct symbol *sym, bool check_err_ptr)
2371 struct smatch_state *state;
2372 char buf[256];
2373 char *start;
2374 int len;
2376 strncpy(buf, name, sizeof(buf) - 1);
2377 buf[sizeof(buf) - 1] = '\0';
2379 start = &buf[0];
2380 while (*start == '*') {
2381 start++;
2382 state = __get_state(SMATCH_EXTRA, start, sym);
2383 if (!state)
2384 continue;
2385 if (!estate_rl(state))
2386 return 1;
2387 if (is_noderef_ptr_rl(estate_rl(state)))
2388 return 1;
2391 start = &buf[0];
2392 while (*start == '&')
2393 start++;
2395 len = strlen(start);
2396 while (true) {
2397 while (len > 0) {
2398 len--;
2399 if (start[len] == '-' ||
2400 start[len] == '.') {
2401 start[len] = '\0';
2402 break;
2405 if (len == 0)
2406 return 0;
2407 state = __get_state(SMATCH_EXTRA, start, sym);
2408 if (!state)
2409 continue;
2410 if (is_noderef_ptr_rl(estate_rl(state)))
2411 return 1;
2415 int parent_is_null_var_sym(const char *name, struct symbol *sym)
2417 return parent_is_err_or_null_var_sym_helper(name, sym, false);
2420 int parent_is_err_or_null_var_sym(const char *name, struct symbol *sym)
2422 return parent_is_err_or_null_var_sym_helper(name, sym, (option_project == PROJ_KERNEL));
2425 int parent_is_null(struct expression *expr)
2427 struct symbol *sym;
2428 char *var;
2429 int ret = 0;
2431 expr = strip_expr(expr);
2432 var = expr_to_var_sym(expr, &sym);
2433 if (!var || !sym)
2434 goto free;
2435 ret = parent_is_null_var_sym(var, sym);
2436 free:
2437 free_string(var);
2438 return ret;
2441 static int param_used_callback(void *found, int argc, char **argv, char **azColName)
2443 *(int *)found = 1;
2444 return 0;
2447 static int is_kzalloc_info(struct sm_state *sm)
2449 sval_t sval;
2452 * kzalloc() information is treated as special because so there is just
2453 * a lot of stuff initialized to zero and it makes building the database
2454 * take hours and hours.
2456 * In theory, we should just remove this line and not pass any unused
2457 * information, but I'm not sure enough that this code works so I want
2458 * to hold off on that for now.
2460 if (!estate_get_single_value(sm->state, &sval))
2461 return 0;
2462 if (sval.value != 0)
2463 return 0;
2464 return 1;
2467 static int is_really_long(struct sm_state *sm)
2469 const char *p;
2470 int cnt = 0;
2472 p = sm->name;
2473 while ((p = strstr(p, "->"))) {
2474 p += 2;
2475 cnt++;
2478 if (cnt < 3 ||
2479 strlen(sm->name) < 40)
2480 return 0;
2481 return 1;
2484 static int filter_unused_param_value_info(struct expression *call, int param, char *printed_name, struct sm_state *sm)
2486 int found = 0;
2488 /* for function pointers assume everything is used */
2489 if (call->fn->type != EXPR_SYMBOL)
2490 return 0;
2492 if (strcmp(printed_name, "$") == 0 ||
2493 strcmp(printed_name, "*$") == 0)
2494 return 0;
2497 * This is to handle __builtin_mul_overflow(). In an ideal world we
2498 * would only need this for invalid code.
2501 if (!call->fn->symbol)
2502 return 0;
2504 if (!is_kzalloc_info(sm) && !is_really_long(sm))
2505 return 0;
2507 run_sql(&param_used_callback, &found,
2508 "select * from return_implies where %s and type = %d and parameter = %d and key = '%s';",
2509 get_static_filter(call->fn->symbol), PARAM_USED, param, printed_name);
2510 if (found)
2511 return 0;
2513 /* If the database is not built yet, then assume everything is used */
2514 run_sql(&param_used_callback, &found,
2515 "select * from return_implies where %s and type = %d;",
2516 get_static_filter(call->fn->symbol), PARAM_USED);
2517 if (!found)
2518 return 0;
2520 return 1;
2523 struct range_list *intersect_with_real_abs_var_sym(const char *name, struct symbol *sym, struct range_list *start)
2525 struct smatch_state *state;
2528 * Here is the difference between implied value and real absolute, say
2529 * you have:
2531 * int a = (u8)x;
2533 * Then you know that a is 0-255. That's real absolute. But you don't
2534 * know for sure that it actually goes up to 255. So it's not implied.
2535 * Implied indicates a degree of certainty.
2537 * But then say you cap "a" at 8. That means you know it goes up to
2538 * 8. So now the implied value is s32min-8. But you can combine it
2539 * with the real absolute to say that actually it's 0-8.
2541 * We are combining it here. But now that I think about it, this is
2542 * probably not the ideal place to combine it because it should proably
2543 * be done earlier. Oh well, this is an improvement on what was there
2544 * before so I'm going to commit this code.
2548 state = get_real_absolute_state_var_sym(name, sym);
2549 if (!state || !estate_rl(state))
2550 return start;
2552 return rl_intersection(estate_rl(state), start);
2555 struct range_list *intersect_with_real_abs_expr(struct expression *expr, struct range_list *start)
2557 struct smatch_state *state;
2558 struct range_list *abs_rl;
2560 state = get_real_absolute_state(expr);
2561 if (!state || !estate_rl(state))
2562 return start;
2564 abs_rl = cast_rl(rl_type(start), estate_rl(state));
2565 return rl_intersection(abs_rl, start);
2568 static void caller_info_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
2570 struct range_list *rl;
2571 sval_t dummy;
2573 if (printed_name[0] == '&')
2574 return;
2575 if (estate_is_whole(sm->state) || !estate_rl(sm->state))
2576 return;
2577 if (filter_unused_param_value_info(call, param, printed_name, sm))
2578 return;
2579 rl = estate_rl(sm->state);
2580 rl = intersect_with_real_abs_var_sym(sm->name, sm->sym, rl);
2581 if (!rl)
2582 return;
2583 sql_insert_caller_info(call, PARAM_VALUE, param, printed_name, show_rl(rl));
2584 if (!estate_get_single_value(sm->state, &dummy)) {
2585 if (estate_has_hard_max(sm->state))
2586 sql_insert_caller_info(call, HARD_MAX, param, printed_name,
2587 sval_to_str(estate_max(sm->state)));
2588 if (estate_has_fuzzy_max(sm->state))
2589 sql_insert_caller_info(call, FUZZY_MAX, param, printed_name,
2590 sval_to_str(estate_get_fuzzy_max(sm->state)));
2594 static void returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
2596 struct symbol *returned_sym;
2597 char *returned_name;
2598 struct sm_state *sm;
2599 char *compare_str;
2600 char name_buf[256];
2601 char val_buf[256];
2602 int len;
2604 // FIXME handle *$
2606 if (!is_pointer(expr))
2607 return;
2608 if (return_ranges && strstr(return_ranges, "[==$"))
2609 return;
2611 returned_name = expr_to_var_sym(expr, &returned_sym);
2612 if (!returned_name || !returned_sym)
2613 goto free;
2614 len = strlen(returned_name);
2616 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
2617 if (!estate_rl(sm->state))
2618 continue;
2619 if (returned_sym != sm->sym)
2620 continue;
2621 if (strncmp(returned_name, sm->name, len) != 0)
2622 continue;
2623 if (sm->name[len] != '-')
2624 continue;
2626 snprintf(name_buf, sizeof(name_buf), "$%s", sm->name + len);
2628 compare_str = name_sym_to_param_comparison(sm->name, sm->sym);
2629 if (!compare_str && estate_is_whole(sm->state))
2630 continue;
2631 snprintf(val_buf, sizeof(val_buf), "%s%s", sm->state->name, compare_str ?: "");
2633 sql_insert_return_states(return_id, return_ranges, PARAM_VALUE,
2634 -1, name_buf, val_buf);
2635 } END_FOR_EACH_SM(sm);
2637 free:
2638 free_string(returned_name);
2641 static void db_limited_before(void)
2643 unmatched_stree = clone_stree(__get_cur_stree());
2646 static void db_limited_after(void)
2648 free_stree(&unmatched_stree);
2651 static int basically_the_same(struct range_list *orig, struct range_list *new)
2653 if (type_is_ptr(rl_type(orig)) &&
2654 is_whole_ptr_rl(orig) &&
2655 is_whole_ptr_rl(new))
2656 return true;
2658 return rl_equiv(orig, new);
2661 static void db_param_limit_binops(struct expression *arg, char *key, struct range_list *rl)
2663 struct range_list *left_rl;
2664 sval_t zero = { .type = rl_type(rl), };
2665 sval_t sval;
2667 if (strcmp(key, "$") != 0)
2668 return;
2669 if (arg->op != '*')
2670 return;
2671 if (!get_implied_value(arg->right, &sval))
2672 return;
2673 if (can_integer_overflow(get_type(arg), arg))
2674 return;
2676 left_rl = rl_binop(rl, '/', alloc_rl(sval, sval));
2677 if (!rl_has_sval(rl, zero))
2678 left_rl = remove_range(left_rl, zero, zero);
2680 set_extra_expr_nomod(arg->left, alloc_estate_rl(left_rl));
2683 static void db_param_limit_filter(struct expression *expr, int param, char *key, char *value, enum info_type op)
2685 struct smatch_state *state;
2686 struct expression *arg;
2687 char *name;
2688 struct symbol *sym;
2689 struct var_sym_list *vsl = NULL;
2690 struct sm_state *sm;
2691 struct symbol *compare_type, *var_type;
2692 struct range_list *rl;
2693 struct range_list *limit;
2694 struct range_list *new;
2695 char *other_name;
2696 struct symbol *other_sym;
2698 while (expr->type == EXPR_ASSIGNMENT)
2699 expr = strip_expr(expr->right);
2700 if (expr->type != EXPR_CALL)
2701 return;
2703 arg = get_argument_from_call_expr(expr->args, param);
2704 if (!arg)
2705 return;
2707 if (strcmp(key, "$") == 0)
2708 compare_type = get_arg_type(expr->fn, param);
2709 else
2710 compare_type = get_member_type_from_key(arg, key);
2712 call_results_to_rl(expr, compare_type, value, &limit);
2713 if (strcmp(key, "$") == 0)
2714 move_known_to_rl(&arg, &limit);
2715 name = get_chunk_from_key(arg, key, &sym, &vsl);
2716 if (!name)
2717 return;
2718 if (op != PARAM_LIMIT && !sym)
2719 goto free;
2721 sm = get_sm_state(SMATCH_EXTRA, name, sym);
2722 if (sm)
2723 rl = estate_rl(sm->state);
2724 else
2725 rl = alloc_whole_rl(compare_type);
2727 if (op == PARAM_LIMIT && !rl_fits_in_type(rl, compare_type))
2728 goto free;
2730 new = rl_intersection(rl, limit);
2732 var_type = get_member_type_from_key(arg, key);
2733 new = cast_rl(var_type, new);
2735 /* We want to preserve the implications here */
2736 if (sm && basically_the_same(rl, new))
2737 goto free;
2738 other_name = get_other_name_sym(name, sym, &other_sym);
2740 state = alloc_estate_rl(new);
2741 if (sm && estate_has_hard_max(sm->state))
2742 estate_set_hard_max(state);
2744 if (op == PARAM_LIMIT) {
2745 set_extra_nomod_vsl(name, sym, vsl, NULL, state);
2746 } else
2747 set_extra_mod(name, sym, NULL, state);
2749 if (other_name && other_sym) {
2750 state = clone_estate(state);
2751 if (op == PARAM_LIMIT)
2752 set_extra_nomod_vsl(other_name, other_sym, vsl, NULL, state);
2753 else
2754 set_extra_mod(other_name, other_sym, NULL, state);
2757 if (op == PARAM_LIMIT && arg->type == EXPR_BINOP)
2758 db_param_limit_binops(arg, key, new);
2759 free:
2760 free_string(name);
2763 static void db_param_limit(struct expression *expr, int param, char *key, char *value)
2765 db_param_limit_filter(expr, param, key, value, PARAM_LIMIT);
2768 static void db_param_filter(struct expression *expr, int param, char *key, char *value)
2770 db_param_limit_filter(expr, param, key, value, PARAM_FILTER);
2773 static struct expression *get_star_pointer_hack(struct expression *arg, const char *key)
2775 /* if we are setting *(p + offset) then we are setting *p */
2776 if (strcmp(key, "*$") != 0)
2777 return NULL;
2779 arg = strip_expr(arg);
2780 if (arg->op != '+')
2781 return NULL;
2782 arg = strip_expr(arg->left);
2783 if (!is_pointer(arg))
2784 return NULL;
2785 return arg;
2788 static bool handle_param_assign(struct expression *expr, int param, char *key, char *value)
2790 struct expression *arg, *left, *right, *fake_assign;
2791 struct range_list *filter, *filtered;
2792 struct symbol *arg_type, *param_type;
2793 struct smatch_state *state, *clone;
2794 char right_key[64];
2795 int right_param;
2796 char buf[64];
2797 char *p;
2799 p = strchr(value, '[');
2800 if (!p)
2801 return false;
2802 p++;
2803 if (*p != '$')
2804 return false;
2805 snprintf(buf, sizeof(buf), "%s", p);
2806 p = strchr(buf, ']');
2807 if (!p)
2808 return false;
2809 *p = '\0';
2811 if (!split_param_key(buf, &right_param, right_key, sizeof(right_key)))
2812 return false;
2814 left = gen_expr_from_param_key(expr, param, key);
2815 if (!left)
2816 return false;
2817 right = gen_expr_from_param_key(expr, right_param, right_key);
2818 if (!right)
2819 return false;
2821 fake_assign = assign_expression(left, '=', right);
2822 fake_param_assign_helper(expr, fake_assign, true);
2824 /* Assignment faked. Now filter the results */
2825 arg = get_argument_from_call_expr(expr->args, param);
2826 if (!arg)
2827 return false;
2828 state = get_extra_state(left);
2829 if (!state)
2830 return false;
2831 arg_type = get_arg_type_from_key(expr->fn, param, arg, key);
2832 param_type = get_member_type_from_key(arg, key);
2833 if (param_type && param_type->type == SYM_STRUCT)
2834 return false;
2835 call_results_to_rl(expr, arg_type, value, &filter);
2836 filter = cast_rl(param_type, filter);
2837 filtered = rl_intersection(estate_rl(state), filter);
2839 if (!rl_equiv(estate_rl(state), filtered)) {
2840 clone = clone_partial_estate(state, filtered);
2841 set_extra_expr_nomod(left, clone);
2844 return true;
2847 static void db_param_add_set(struct expression *expr, int param, char *key, char *value, enum info_type op)
2849 struct expression *arg, *gen_expr;
2850 char *name;
2851 char *other_name = NULL;
2852 struct symbol *sym, *other_sym;
2853 struct symbol *param_type, *arg_type;
2854 struct smatch_state *state;
2855 struct range_list *new = NULL;
2856 struct range_list *added = NULL;
2858 while (expr->type == EXPR_ASSIGNMENT)
2859 expr = strip_expr(expr->right);
2860 if (expr->type != EXPR_CALL)
2861 return;
2863 if (op == PARAM_SET) {
2864 if (handle_param_assign(expr, param, key, value))
2865 return;
2868 arg = get_argument_from_call_expr(expr->args, param);
2869 if (!arg)
2870 return;
2872 arg_type = get_arg_type_from_key(expr->fn, param, arg, key);
2873 param_type = get_member_type_from_key(arg, key);
2874 if (param_type && param_type->type == SYM_STRUCT)
2875 return;
2876 name = get_variable_from_key(arg, key, &sym);
2877 if (!name || !sym) {
2878 arg = get_star_pointer_hack(arg, key);
2879 if (!arg)
2880 goto free;
2881 name = get_variable_from_key(arg, key, &sym);
2882 if (!name || !sym)
2883 goto free;
2885 gen_expr = gen_expression_from_key(arg, key);
2887 state = get_state(SMATCH_EXTRA, name, sym);
2888 if (state)
2889 new = estate_rl(state);
2891 call_results_to_rl(expr, arg_type, value, &added);
2892 added = cast_rl(param_type, added);
2893 if (op == PARAM_SET)
2894 new = added;
2895 else
2896 new = rl_union(new, added);
2898 other_name = get_other_name_sym_nostack(name, sym, &other_sym);
2899 set_extra_mod(name, sym, gen_expr, alloc_estate_rl(new));
2900 if (other_name && other_sym)
2901 set_extra_mod(other_name, other_sym, gen_expr, alloc_estate_rl(new));
2902 free:
2903 free_string(other_name);
2904 free_string(name);
2907 static void db_param_add(struct expression *expr, int param, char *key, char *value)
2909 in_param_set = true;
2910 db_param_add_set(expr, param, key, value, PARAM_ADD);
2911 in_param_set = false;
2914 static void db_param_set(struct expression *expr, int param, char *key, char *value)
2916 in_param_set = true;
2917 db_param_add_set(expr, param, key, value, PARAM_SET);
2918 in_param_set = false;
2921 static void match_lost_param(struct expression *call, int param)
2923 struct expression *arg;
2925 if (is_const_param(call->fn, param))
2926 return;
2928 arg = get_argument_from_call_expr(call->args, param);
2929 if (!arg)
2930 return;
2932 arg = strip_expr(arg);
2933 if (arg->type == EXPR_PREOP && arg->op == '&')
2934 set_extra_expr_mod(arg->unop, alloc_estate_whole(get_type(arg->unop)));
2935 else
2936 ; /* if pointer then set struct members, maybe?*/
2939 static void db_param_value(struct expression *expr, int param, char *key, char *value)
2941 struct expression *call;
2942 char *name;
2943 struct symbol *sym;
2944 struct symbol *type;
2945 struct range_list *rl = NULL;
2947 if (param != -1)
2948 return;
2950 call = expr;
2951 while (call->type == EXPR_ASSIGNMENT)
2952 call = strip_expr(call->right);
2953 if (call->type != EXPR_CALL)
2954 return;
2956 type = get_member_type_from_key(expr->left, key);
2957 name = get_variable_from_key(expr->left, key, &sym);
2958 if (!name || !sym)
2959 goto free;
2961 call_results_to_rl(call, type, value, &rl);
2963 set_extra_mod(name, sym, NULL, alloc_estate_rl(rl));
2964 free:
2965 free_string(name);
2968 static void set_param_value(const char *name, struct symbol *sym, char *key, char *value)
2970 struct expression *expr;
2971 struct range_list *rl = NULL;
2972 struct smatch_state *state;
2973 struct symbol *type;
2974 char *key_orig = key;
2975 char *fullname;
2976 sval_t dummy;
2978 expr = symbol_expression(sym);
2979 fullname = get_variable_from_key(expr, key, NULL);
2980 if (!fullname)
2981 return;
2983 type = get_member_type_from_key(expr, key_orig);
2984 str_to_rl(type, value, &rl);
2985 state = alloc_estate_rl(rl);
2986 if (estate_get_single_value(state, &dummy))
2987 estate_set_hard_max(state);
2988 set_state(SMATCH_EXTRA, fullname, sym, state);
2991 static void db_buf_add_helper(struct expression *expr, void *data)
2993 struct expression *left = expr->left;
2994 struct sm_state *sm;
2996 sm = get_sm_state_expr(my_id, left);
2997 if (!sm)
2998 return;
3000 set_state(my_id, sm->name, sm->sym, alloc_estate_whole(estate_type(sm->state)));
3003 static void db_buf_add(struct expression *expr, int param, char *key, char *value)
3005 struct expression *arg;
3008 * There really isn't much we can do with a BUF_ADD. A BUF_CLEAR is
3009 * like a full modification which some stuff cares about. A no_mod
3010 * thing is useful because that's normally bounds checking etc. But
3011 * with BUF_ADD the only thing we can say is do is delete any previous
3012 * known states.
3016 arg = gen_expr_from_param_key(expr, param, key);
3017 if (!arg)
3018 return;
3020 create_recursive_fake_assignments(deref_expression(arg), &db_buf_add_helper, NULL);
3023 static void set_param_fuzzy_max(const char *name, struct symbol *sym, char *key, char *value)
3025 struct expression *expr;
3026 struct range_list *rl = NULL;
3027 struct smatch_state *state;
3028 struct symbol *type;
3029 char *fullname;
3030 sval_t max;
3032 expr = symbol_expression(sym);
3033 fullname = get_variable_from_key(expr, key, NULL);
3034 if (!fullname)
3035 return;
3037 state = get_state(SMATCH_EXTRA, fullname, sym);
3038 if (!state)
3039 return;
3040 type = estate_type(state);
3041 str_to_rl(type, value, &rl);
3042 if (!rl_to_sval(rl, &max))
3043 return;
3044 estate_set_fuzzy_max(state, max);
3047 static void set_param_hard_max(const char *name, struct symbol *sym, char *key, char *value)
3049 struct smatch_state *state;
3050 struct expression *expr;
3051 char *fullname;
3053 expr = symbol_expression(sym);
3054 fullname = get_variable_from_key(expr, key, NULL);
3055 if (!fullname)
3056 return;
3058 state = get_state(SMATCH_EXTRA, fullname, sym);
3059 if (!state)
3060 return;
3061 estate_set_hard_max(state);
3064 static struct sm_state *get_sm_from_call(struct expression *expr)
3066 struct expression *fake;
3067 struct sm_state *ret;
3068 char buf[32];
3070 if (is_fake_call(expr))
3071 return NULL;
3073 fake = expr_get_fake_parent_expr(expr);
3074 if (fake && fake->type == EXPR_ASSIGNMENT) {
3075 ret = get_sm_state_expr(SMATCH_EXTRA, fake->left);
3076 if (ret)
3077 return ret;
3080 snprintf(buf, sizeof(buf), "return %p", expr);
3081 return get_sm_state(SMATCH_EXTRA, buf, NULL);
3084 struct sm_state *get_extra_sm_state(struct expression *expr)
3086 char *name;
3087 struct symbol *sym;
3088 struct sm_state *ret = NULL;
3090 expr = strip_expr(expr);
3091 if (!expr)
3092 return NULL;
3094 if (expr->type == EXPR_CALL)
3095 return get_sm_from_call(expr);
3097 name = expr_to_known_chunk_sym(expr, &sym);
3098 if (!name)
3099 goto free;
3101 ret = get_sm_state(SMATCH_EXTRA, name, sym);
3102 free:
3103 free_string(name);
3104 return ret;
3107 struct smatch_state *get_extra_state(struct expression *expr)
3109 struct sm_state *sm;
3111 sm = get_extra_sm_state(expr);
3112 if (!sm)
3113 return NULL;
3114 return sm->state;
3117 void register_smatch_extra(int id)
3119 my_id = id;
3121 set_dynamic_states(my_id);
3122 add_merge_hook(my_id, &merge_estates);
3123 add_unmatched_state_hook(my_id, &unmatched_state);
3124 select_caller_info_hook(set_param_value, PARAM_VALUE);
3125 select_caller_info_hook(set_param_fuzzy_max, FUZZY_MAX);
3126 select_caller_info_hook(set_param_hard_max, HARD_MAX);
3127 select_return_states_before(&db_limited_before);
3128 select_return_states_hook(PARAM_LIMIT, &db_param_limit);
3129 select_return_states_hook(PARAM_FILTER, &db_param_filter);
3130 select_return_states_hook(PARAM_ADD, &db_param_add);
3131 select_return_states_hook(PARAM_SET, &db_param_set);
3132 select_return_states_hook(BUF_ADD, &db_buf_add);
3133 add_lost_param_hook(&match_lost_param);
3134 select_return_states_hook(PARAM_VALUE, &db_param_value);
3135 select_return_states_after(&db_limited_after);
3138 static void match_link_modify(struct sm_state *sm, struct expression *mod_expr)
3140 struct var_sym_list *links;
3141 struct var_sym *tmp;
3142 struct smatch_state *state;
3144 links = sm->state->data;
3146 FOR_EACH_PTR(links, tmp) {
3147 if (sm->sym == tmp->sym &&
3148 strcmp(sm->name, tmp->var) == 0)
3149 continue;
3150 state = get_state(SMATCH_EXTRA, tmp->var, tmp->sym);
3151 if (!state)
3152 continue;
3153 set_state(SMATCH_EXTRA, tmp->var, tmp->sym, alloc_estate_whole(estate_type(state)));
3154 } END_FOR_EACH_PTR(tmp);
3155 set_state(link_id, sm->name, sm->sym, &undefined);
3158 void register_smatch_extra_links(int id)
3160 link_id = id;
3161 set_dynamic_states(link_id);
3164 void register_smatch_extra_late(int id)
3166 add_merge_hook(link_id, &merge_link_states);
3167 add_modification_hook(link_id, &match_link_modify);
3168 add_hook(&match_dereferences, DEREF_HOOK);
3169 add_hook(&match_pointer_as_array, OP_HOOK);
3170 select_return_implies_hook_early(DEREFERENCE, &set_param_dereferenced);
3171 add_hook(&match_function_call, FUNCTION_CALL_HOOK);
3172 add_hook(&match_assign, ASSIGNMENT_HOOK);
3173 add_hook(&match_assign, GLOBAL_ASSIGNMENT_HOOK);
3174 add_hook(&unop_expr, OP_HOOK);
3175 add_hook(&asm_expr, ASM_HOOK);
3177 add_caller_info_callback(my_id, caller_info_callback);
3178 add_split_return_callback(&returned_struct_members);
3180 // add_hook(&assume_indexes_are_valid, OP_HOOK);