unwind: add release_and_free_resource() and release_resource()
[smatch.git] / smatch_extra.c
blobf032d18fb196682d429c3011de92004202a00ab6
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 smatch_state *state)
173 struct sm_state *sm;
175 if (__in_fake_assign || __in_fake_var_assign)
176 return;
178 if (!estate_type(state) || estate_type(state)->type != SYM_PTR)
179 return;
180 if (!is_noderef_ptr_rl(estate_rl(state)))
181 return;
183 FOR_EACH_MY_SM(SMATCH_EXTRA, __get_cur_stree(), sm) {
184 if (sm->sym != sym)
185 continue;
186 if (strcmp(name, sm->name) == 0)
187 continue;
188 if (!is_sub_member(name, sym, sm))
189 continue;
190 set_extra_nomod(sm->name, sm->sym, NULL, alloc_estate_empty());
191 } END_FOR_EACH_SM(sm);
194 static void call_update_mtag_data(struct expression *expr,
195 struct smatch_state *state)
197 struct expression *faked;
199 if (__in_fake_var_assign)
200 return;
202 faked = get_faked_expression();
203 if (!faked)
204 goto update;
205 if (faked->type == EXPR_ASSIGNMENT && is_fresh_alloc(faked->right))
206 goto update;
207 return;
209 update:
210 update_mtag_data(expr, state);
213 static bool in_param_set;
214 void set_extra_mod_helper(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
216 if (!expr)
217 expr = gen_expression_from_name_sym(name, sym);
218 remove_from_equiv(name, sym);
219 set_union_info(name, sym, expr, state);
220 mark_sub_members_gone(name, sym, state);
221 call_extra_mod_hooks(name, sym, expr, state);
222 call_update_mtag_data(expr, state);
223 if ((__in_fake_assign || in_param_set) &&
224 estate_is_unknown(state) && !get_state(SMATCH_EXTRA, name, sym))
225 return;
226 set_state(SMATCH_EXTRA, name, sym, state);
229 static void set_extra_nomod_helper(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
231 call_extra_nomod_hooks(name, sym, expr, state);
232 set_state(SMATCH_EXTRA, name, sym, state);
235 static char *get_pointed_at(const char *name, struct symbol *sym, struct symbol **new_sym)
237 struct expression *assigned;
240 * Imagine we have an assignment: "foo = &addr;" then the other name
241 * of "*foo" is addr.
244 if (name[0] != '*')
245 return NULL;
246 if (strcmp(name + 1, sym->ident->name) != 0)
247 return NULL;
249 assigned = get_assigned_expr_name_sym(sym->ident->name, sym);
250 if (!assigned)
251 return NULL;
252 assigned = strip_parens(assigned);
253 if (assigned->type != EXPR_PREOP || assigned->op != '&')
254 return NULL;
256 return expr_to_var_sym(assigned->unop, new_sym);
259 char *get_other_name_sym_from_chunk(const char *name, const char *chunk, int len, struct symbol *sym, struct symbol **new_sym)
261 struct expression *assigned;
262 char *orig_name = NULL;
263 char buf[256];
264 char *ret;
266 assigned = get_assigned_expr_name_sym(chunk, sym);
267 if (!assigned)
268 return NULL;
269 if (assigned->type == EXPR_CALL)
270 return map_call_to_other_name_sym(name, sym, new_sym);
271 if (assigned->type == EXPR_PREOP && assigned->op == '&') {
273 orig_name = expr_to_var_sym(assigned, new_sym);
274 if (!orig_name || !*new_sym)
275 goto free;
277 snprintf(buf, sizeof(buf), "%s.%s", orig_name + 1, name + len);
278 ret = alloc_string(buf);
279 free_string(orig_name);
280 return ret;
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, name + len);
288 ret = alloc_string(buf);
289 free_string(orig_name);
290 return ret;
291 free:
292 free_string(orig_name);
293 return NULL;
296 static char *get_long_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym, bool use_stack)
298 struct expression *orig;
299 struct symbol *orig_sym;
300 char *orig_name, *ret;
302 if (!sym || !sym->ident)
303 return NULL;
305 orig = get_assigned_expr_name_sym(sym->ident->name, sym);
306 if (orig) {
307 orig_name = expr_to_var_sym(orig, &orig_sym);
308 if (!orig_name)
309 return NULL;
311 ret = swap_names(name, sym->ident->name, orig_name);
312 free_string(orig_name);
313 if (ret)
314 *new_sym = orig_sym;
315 return ret;
318 return NULL;
321 char *get_other_name_sym_helper(const char *name, struct symbol *sym, struct symbol **new_sym, bool use_stack)
323 char buf[256];
324 char *ret;
325 int len;
327 *new_sym = NULL;
329 if (!sym || !sym->ident)
330 return NULL;
332 ret = get_pointed_at(name, sym, new_sym);
333 if (ret)
334 return ret;
336 ret = map_long_to_short_name_sym(name, sym, new_sym, use_stack);
337 if (ret)
338 return ret;
340 len = snprintf(buf, sizeof(buf), "%s", name);
341 if (len >= sizeof(buf) - 2)
342 return NULL;
344 while (use_stack && len >= 1) {
345 if (buf[len] == '>' && buf[len - 1] == '-') {
346 len--;
347 buf[len] = '\0';
348 ret = get_other_name_sym_from_chunk(name, buf, len + 2, sym, new_sym);
349 if (ret)
350 return ret;
352 len--;
355 ret = get_long_name_sym(name, sym, new_sym, use_stack);
356 if (ret)
357 return ret;
359 return NULL;
362 char *get_other_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym)
364 return get_other_name_sym_helper(name, sym, new_sym, true);
367 char *get_other_name_sym_nostack(const char *name, struct symbol *sym, struct symbol **new_sym)
369 return get_other_name_sym_helper(name, sym, new_sym, false);
372 void set_extra_mod(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
374 char *new_name;
375 struct symbol *new_sym;
377 set_extra_mod_helper(name, sym, expr, state);
378 new_name = get_other_name_sym_nostack(name, sym, &new_sym);
379 if (new_name && new_sym)
380 set_extra_mod_helper(new_name, new_sym, NULL, state);
381 free_string(new_name);
384 static struct expression *chunk_get_array_base(struct expression *expr)
387 * The problem with is_array() is that it only returns true for things
388 * like foo[1] but not for foo[1].bar.
391 expr = strip_expr(expr);
392 while (expr && expr->type == EXPR_DEREF)
393 expr = strip_expr(expr->deref);
394 return get_array_base(expr);
397 static int chunk_has_array(struct expression *expr)
399 return !!chunk_get_array_base(expr);
402 static void clear_array_states(struct expression *array)
404 struct sm_state *sm;
406 sm = get_sm_state_expr(link_id, array);
407 if (sm)
408 match_link_modify(sm, NULL);
411 static void set_extra_array_mod(struct expression *expr, struct smatch_state *state)
413 struct expression *array;
414 struct var_sym_list *vsl;
415 struct var_sym *vs;
416 char *name;
417 struct symbol *sym;
419 array = chunk_get_array_base(expr);
421 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
422 if (!name || !vsl) {
423 clear_array_states(array);
424 goto free;
427 FOR_EACH_PTR(vsl, vs) {
428 store_link(link_id, vs->var, vs->sym, name, sym);
429 } END_FOR_EACH_PTR(vs);
431 call_extra_mod_hooks(name, sym, expr, state);
432 set_state(SMATCH_EXTRA, name, sym, state);
433 free:
434 free_string(name);
437 void set_extra_expr_mod(struct expression *expr, struct smatch_state *state)
439 struct symbol *sym;
440 char *name;
442 if (chunk_has_array(expr)) {
443 set_extra_array_mod(expr, state);
444 return;
447 expr = strip_expr(expr);
448 name = expr_to_var_sym(expr, &sym);
449 if (!name || !sym)
450 goto free;
451 set_extra_mod(name, sym, expr, state);
452 free:
453 free_string(name);
456 void set_extra_nomod(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
458 char *new_name;
459 struct symbol *new_sym;
460 struct relation *rel;
461 struct smatch_state *orig_state;
463 orig_state = get_state(SMATCH_EXTRA, name, sym);
465 /* don't save unknown states if leaving it blank is the same */
466 if (!orig_state && estate_is_unknown(state))
467 return;
469 new_name = get_other_name_sym(name, sym, &new_sym);
470 if (new_name && new_sym)
471 set_extra_nomod_helper(new_name, new_sym, expr, state);
472 free_string(new_name);
474 if (!estate_related(orig_state)) {
475 set_extra_nomod_helper(name, sym, expr, state);
476 return;
479 set_related(state, estate_related(orig_state));
480 FOR_EACH_PTR(estate_related(orig_state), rel) {
481 struct smatch_state *estate;
483 estate = get_state(SMATCH_EXTRA, rel->name, rel->sym);
484 if (!estate)
485 continue;
486 set_extra_nomod_helper(rel->name, rel->sym, expr, clone_estate_cast(estate_type(estate), state));
487 } END_FOR_EACH_PTR(rel);
490 void set_extra_nomod_vsl(const char *name, struct symbol *sym, struct var_sym_list *vsl, struct expression *expr, struct smatch_state *state)
492 struct var_sym *vs;
494 FOR_EACH_PTR(vsl, vs) {
495 store_link(link_id, vs->var, vs->sym, name, sym);
496 } END_FOR_EACH_PTR(vs);
498 set_extra_nomod(name, sym, expr, state);
502 * This is for return_implies_state() hooks which modify a SMATCH_EXTRA state
504 void set_extra_expr_nomod(struct expression *expr, struct smatch_state *state)
506 struct var_sym_list *vsl;
507 struct var_sym *vs;
508 char *name;
509 struct symbol *sym;
511 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
512 if (!name || !vsl)
513 goto free;
514 FOR_EACH_PTR(vsl, vs) {
515 store_link(link_id, vs->var, vs->sym, name, sym);
516 } END_FOR_EACH_PTR(vs);
518 set_extra_nomod(name, sym, expr, state);
519 free:
520 free_string(name);
523 static void set_extra_true_false(const char *name, struct symbol *sym,
524 struct smatch_state *true_state,
525 struct smatch_state *false_state)
527 char *new_name;
528 struct symbol *new_sym;
529 struct relation *rel;
530 struct smatch_state *orig_state;
532 if (!true_state && !false_state)
533 return;
535 if (in_warn_on_macro())
536 return;
538 new_name = get_other_name_sym(name, sym, &new_sym);
539 if (new_name && new_sym)
540 set_true_false_states(SMATCH_EXTRA, new_name, new_sym, true_state, false_state);
541 free_string(new_name);
543 orig_state = get_state(SMATCH_EXTRA, name, sym);
545 if (!estate_related(orig_state)) {
546 set_true_false_states(SMATCH_EXTRA, name, sym, true_state, false_state);
547 return;
550 if (true_state)
551 set_related(true_state, estate_related(orig_state));
552 if (false_state)
553 set_related(false_state, estate_related(orig_state));
555 FOR_EACH_PTR(estate_related(orig_state), rel) {
556 set_true_false_states(SMATCH_EXTRA, rel->name, rel->sym,
557 true_state, false_state);
558 } END_FOR_EACH_PTR(rel);
561 static void set_extra_chunk_true_false(struct expression *expr,
562 struct smatch_state *true_state,
563 struct smatch_state *false_state)
565 struct var_sym_list *vsl;
566 struct var_sym *vs;
567 struct symbol *type;
568 char *name;
569 struct symbol *sym;
571 if (in_warn_on_macro())
572 return;
574 type = get_type(expr);
575 if (!type)
576 return;
578 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
579 if (!name || !vsl)
580 goto free;
581 FOR_EACH_PTR(vsl, vs) {
582 store_link(link_id, vs->var, vs->sym, name, sym);
583 } END_FOR_EACH_PTR(vs);
585 set_true_false_states(SMATCH_EXTRA, name, sym,
586 clone_estate(true_state),
587 clone_estate(false_state));
588 free:
589 free_string(name);
592 static void set_extra_expr_true_false(struct expression *expr,
593 struct smatch_state *true_state,
594 struct smatch_state *false_state)
596 char *name;
597 struct symbol *sym;
598 sval_t sval;
600 if (!true_state && !false_state)
601 return;
603 if (get_value(expr, &sval))
604 return;
606 expr = strip_expr(expr);
607 name = expr_to_var_sym(expr, &sym);
608 if (!name || !sym) {
609 free_string(name);
610 set_extra_chunk_true_false(expr, true_state, false_state);
611 return;
613 set_extra_true_false(name, sym, true_state, false_state);
614 free_string(name);
617 static int get_countdown_info(struct expression *condition, struct expression **unop, int *op, sval_t *right)
619 struct expression *unop_expr;
620 int comparison;
621 sval_t limit;
623 right->type = &int_ctype;
624 right->value = 0;
626 condition = strip_expr(condition);
628 if (condition->type == EXPR_COMPARE) {
629 comparison = remove_unsigned_from_comparison(condition->op);
631 if (comparison != SPECIAL_GTE && comparison != '>')
632 return 0;
633 if (!get_value(condition->right, &limit))
634 return 0;
636 unop_expr = condition->left;
637 if (unop_expr->type != EXPR_PREOP && unop_expr->type != EXPR_POSTOP)
638 return 0;
639 if (unop_expr->op != SPECIAL_DECREMENT)
640 return 0;
642 *unop = unop_expr;
643 *op = comparison;
644 *right = limit;
646 return 1;
649 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
650 return 0;
651 if (condition->op != SPECIAL_DECREMENT)
652 return 0;
654 *unop = condition;
655 *op = '>';
657 return 1;
660 static struct sm_state *handle_canonical_while_count_down(struct statement *loop)
662 struct expression *iter_var;
663 struct expression *condition, *unop;
664 struct symbol *type;
665 struct sm_state *sm;
666 struct smatch_state *estate;
667 int op;
668 sval_t start, right;
670 right.type = &int_ctype;
671 right.value = 0;
673 condition = strip_expr(loop->iterator_pre_condition);
674 if (!condition)
675 return NULL;
677 if (!get_countdown_info(condition, &unop, &op, &right))
678 return NULL;
680 iter_var = unop->unop;
682 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
683 if (!sm)
684 return NULL;
685 if (sval_cmp(estate_min(sm->state), right) < 0)
686 return NULL;
687 start = estate_max(sm->state);
689 type = get_type(iter_var);
690 right = sval_cast(type, right);
691 start = sval_cast(type, start);
693 if (sval_cmp(start, right) <= 0)
694 return NULL;
695 if (!sval_is_max(start))
696 start.value--;
698 if (op == SPECIAL_GTE)
699 right.value--;
701 if (unop->type == EXPR_PREOP) {
702 right.value++;
703 estate = alloc_estate_range(right, start);
704 if (estate_has_hard_max(sm->state))
705 estate_set_hard_max(estate);
706 estate_copy_fuzzy_max(estate, sm->state);
707 set_extra_expr_mod(iter_var, estate);
709 if (unop->type == EXPR_POSTOP) {
710 estate = alloc_estate_range(right, start);
711 if (estate_has_hard_max(sm->state))
712 estate_set_hard_max(estate);
713 estate_copy_fuzzy_max(estate, sm->state);
714 set_extra_expr_mod(iter_var, estate);
716 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
719 static struct sm_state *handle_canonical_for_inc(struct expression *iter_expr,
720 struct expression *condition)
722 struct expression *iter_var;
723 struct sm_state *sm;
724 struct smatch_state *estate;
725 sval_t start, end, max;
726 bool unknown_end = false;
728 iter_var = iter_expr->unop;
729 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
730 if (!sm)
731 return NULL;
732 if (!estate_get_single_value(sm->state, &start))
733 return NULL;
734 if (!get_implied_max(condition->right, &end)) {
735 end = sval_type_max(get_type(condition->right));
736 end = sval_cast(start.type, end);
737 if (sval_is_max(end))
738 unknown_end = true;
741 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
742 return NULL;
744 switch (condition->op) {
745 case SPECIAL_UNSIGNED_LT:
746 case SPECIAL_NOTEQUAL:
747 case '<':
748 if (!sval_is_min(end) && !unknown_end)
749 end.value--;
750 break;
751 case SPECIAL_UNSIGNED_LTE:
752 case SPECIAL_LTE:
753 break;
754 default:
755 return NULL;
757 if (sval_cmp(end, start) < 0)
758 return NULL;
759 end = sval_cast(start.type, end);
760 estate = alloc_estate_range(start, end);
761 if (get_hard_max(condition->right, &max)) {
762 if (!get_macro_name(condition->pos))
763 estate_set_hard_max(estate);
764 if (condition->op == '<' ||
765 condition->op == SPECIAL_UNSIGNED_LT ||
766 condition->op == SPECIAL_NOTEQUAL)
767 max.value--;
768 max = sval_cast(start.type, max);
769 estate_set_fuzzy_max(estate, max);
771 set_extra_expr_mod(iter_var, estate);
772 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
775 static struct sm_state *handle_canonical_for_dec(struct expression *iter_expr,
776 struct expression *condition)
778 struct expression *iter_var;
779 struct sm_state *sm;
780 struct smatch_state *estate;
781 sval_t start, end;
783 iter_var = iter_expr->unop;
784 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
785 if (!sm)
786 return NULL;
787 if (!estate_get_single_value(sm->state, &start))
788 return NULL;
789 if (!get_implied_min(condition->right, &end))
790 end = sval_type_min(get_type(iter_var));
791 end = sval_cast(estate_type(sm->state), end);
792 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
793 return NULL;
795 switch (condition->op) {
796 case SPECIAL_NOTEQUAL:
797 case '>':
798 if (!sval_is_max(end))
799 end.value++;
800 break;
801 case SPECIAL_GTE:
802 break;
803 default:
804 return NULL;
806 if (sval_cmp(end, start) > 0)
807 return NULL;
808 estate = alloc_estate_range(end, start);
809 estate_set_hard_max(estate);
810 estate_set_fuzzy_max(estate, estate_get_fuzzy_max(estate));
811 set_extra_expr_mod(iter_var, estate);
812 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
815 static struct sm_state *handle_canonical_for_loops(struct statement *loop)
817 struct expression *iter_expr;
818 struct expression *condition;
820 if (!loop->iterator_post_statement)
821 return NULL;
822 if (loop->iterator_post_statement->type != STMT_EXPRESSION)
823 return NULL;
824 iter_expr = loop->iterator_post_statement->expression;
825 if (!loop->iterator_pre_condition)
826 return NULL;
827 if (loop->iterator_pre_condition->type != EXPR_COMPARE)
828 return NULL;
829 condition = loop->iterator_pre_condition;
831 if (iter_expr->op == SPECIAL_INCREMENT)
832 return handle_canonical_for_inc(iter_expr, condition);
833 if (iter_expr->op == SPECIAL_DECREMENT)
834 return handle_canonical_for_dec(iter_expr, condition);
835 return NULL;
838 struct sm_state *__extra_handle_canonical_loops(struct statement *loop, struct stree **stree)
840 struct sm_state *ret;
843 * Canonical loops are a hack. The proper way to handle this is to
844 * use two passes, but unfortunately, doing two passes makes parsing
845 * code twice as slow.
847 * What we do is we set the inside state here, which overwrites whatever
848 * __extra_match_condition() does. Then we set the outside state in
849 * __extra_pre_loop_hook_after().
852 __push_fake_cur_stree();
853 if (!loop->iterator_post_statement)
854 ret = handle_canonical_while_count_down(loop);
855 else
856 ret = handle_canonical_for_loops(loop);
857 *stree = __pop_fake_cur_stree();
858 return ret;
861 int __iterator_unchanged(struct sm_state *sm)
863 if (!sm)
864 return 0;
865 if (get_sm_state(my_id, sm->name, sm->sym) == sm)
866 return 1;
867 return 0;
870 static void while_count_down_after(struct sm_state *sm, struct expression *condition)
872 struct expression *unop;
873 int op;
874 sval_t limit, after_value;
876 if (!get_countdown_info(condition, &unop, &op, &limit))
877 return;
878 after_value = estate_min(sm->state);
879 after_value.value--;
880 set_extra_mod(sm->name, sm->sym, condition->unop, alloc_estate_sval(after_value));
883 void __extra_pre_loop_hook_after(struct sm_state *sm,
884 struct statement *iterator,
885 struct expression *condition)
887 struct expression *iter_expr;
888 sval_t limit;
889 struct smatch_state *state;
890 sval_t end;
892 if (!iterator) {
893 while_count_down_after(sm, condition);
894 return;
897 iter_expr = iterator->expression;
899 if (condition->type != EXPR_COMPARE)
900 return;
902 if (iter_expr->op == SPECIAL_INCREMENT) {
903 if (!get_implied_value(condition->right, &end) &&
904 sval_is_max(estate_max(sm->state)))
905 limit = estate_max(sm->state);
906 else
907 limit = sval_binop(estate_max(sm->state), '+',
908 sval_type_val(estate_type(sm->state), 1));
909 } else {
910 limit = sval_binop(estate_min(sm->state), '-',
911 sval_type_val(estate_type(sm->state), 1));
913 limit = sval_cast(estate_type(sm->state), limit);
914 if (!estate_has_hard_max(sm->state) && !__has_breaks()) {
915 if (iter_expr->op == SPECIAL_INCREMENT)
916 state = alloc_estate_range(estate_min(sm->state), limit);
917 else
918 state = alloc_estate_range(limit, estate_max(sm->state));
919 } else {
920 state = alloc_estate_sval(limit);
922 if (!estate_has_hard_max(sm->state)) {
923 estate_clear_hard_max(state);
925 if (estate_has_fuzzy_max(sm->state)) {
926 sval_t hmax = estate_get_fuzzy_max(sm->state);
927 sval_t max = estate_max(sm->state);
929 if (sval_cmp(hmax, max) != 0)
930 estate_clear_fuzzy_max(state);
931 } else if (!estate_has_fuzzy_max(sm->state)) {
932 estate_clear_fuzzy_max(state);
935 set_extra_mod(sm->name, sm->sym, iter_expr, state);
938 static bool get_global_rl(const char *name, struct symbol *sym, struct range_list **rl)
940 struct expression *expr;
942 if (!sym || !(sym->ctype.modifiers & MOD_TOPLEVEL) || !sym->ident)
943 return false;
944 if (strcmp(sym->ident->name, name) != 0)
945 return false;
947 expr = symbol_expression(sym);
948 return get_implied_rl(expr, rl);
951 static struct stree *unmatched_stree;
952 static struct smatch_state *unmatched_state(struct sm_state *sm)
954 struct smatch_state *state;
955 struct range_list *rl;
957 if (unmatched_stree) {
958 state = get_state_stree(unmatched_stree, SMATCH_EXTRA, sm->name, sm->sym);
959 if (state)
960 return state;
962 if (parent_is_gone_var_sym(sm->name, sm->sym))
963 return alloc_estate_empty();
964 if (get_global_rl(sm->name, sm->sym, &rl))
965 return alloc_estate_rl(rl);
966 return alloc_estate_whole(estate_type(sm->state));
969 static void clear_the_pointed_at(struct expression *expr)
971 struct stree *stree;
972 char *name;
973 struct symbol *sym;
974 struct sm_state *tmp;
976 name = expr_to_var_sym(expr, &sym);
977 if (!name || !sym)
978 goto free;
980 stree = __get_cur_stree();
981 FOR_EACH_MY_SM(SMATCH_EXTRA, stree, tmp) {
982 if (tmp->name[0] != '*')
983 continue;
984 if (tmp->sym != sym)
985 continue;
986 if (strcmp(tmp->name + 1, name) != 0)
987 continue;
988 set_extra_mod(tmp->name, tmp->sym, expr, alloc_estate_whole(estate_type(tmp->state)));
989 } END_FOR_EACH_SM(tmp);
991 free:
992 free_string(name);
995 static int is_const_param(struct expression *expr, int param)
997 struct symbol *type;
999 type = get_arg_type(expr, param);
1000 if (!type)
1001 return 0;
1002 if (type->ctype.modifiers & MOD_CONST)
1003 return 1;
1004 return 0;
1007 static void match_function_call(struct expression *expr)
1009 struct expression *arg;
1010 struct expression *tmp;
1011 int param = -1;
1013 /* if we have the db this is handled in smatch_function_hooks.c */
1014 if (!option_no_db)
1015 return;
1016 if (inlinable(expr->fn))
1017 return;
1019 FOR_EACH_PTR(expr->args, arg) {
1020 param++;
1021 if (is_const_param(expr->fn, param))
1022 continue;
1023 tmp = strip_expr(arg);
1024 if (tmp->type == EXPR_PREOP && tmp->op == '&')
1025 set_extra_expr_mod(tmp->unop, alloc_estate_whole(get_type(tmp->unop)));
1026 else
1027 clear_the_pointed_at(tmp);
1028 } END_FOR_EACH_PTR(arg);
1031 int values_fit_type(struct expression *left, struct expression *right)
1033 struct range_list *rl;
1034 struct symbol *type;
1036 type = get_type(left);
1037 if (!type)
1038 return 0;
1039 get_absolute_rl(right, &rl);
1040 if (type == rl_type(rl))
1041 return 1;
1042 if (type_unsigned(type) && sval_is_negative(rl_min(rl)))
1043 return 0;
1044 if (sval_cmp(sval_type_min(type), rl_min(rl)) > 0)
1045 return 0;
1046 if (sval_cmp(sval_type_max(type), rl_max(rl)) < 0)
1047 return 0;
1048 return 1;
1051 static void save_chunk_info(struct expression *left, struct expression *right)
1053 struct var_sym_list *vsl;
1054 struct var_sym *vs;
1055 struct expression *add_expr;
1056 struct symbol *type;
1057 sval_t sval;
1058 char *name;
1059 struct symbol *sym;
1061 if (right->type != EXPR_BINOP || right->op != '-')
1062 return;
1063 if (!get_value(right->left, &sval))
1064 return;
1065 if (!expr_to_sym(right->right))
1066 return;
1068 add_expr = binop_expression(left, '+', right->right);
1069 type = get_type(add_expr);
1070 if (!type)
1071 return;
1072 name = expr_to_chunk_sym_vsl(add_expr, &sym, &vsl);
1073 if (!name || !vsl)
1074 goto free;
1075 FOR_EACH_PTR(vsl, vs) {
1076 store_link(link_id, vs->var, vs->sym, name, sym);
1077 } END_FOR_EACH_PTR(vs);
1079 set_state(SMATCH_EXTRA, name, sym, alloc_estate_sval(sval_cast(type, sval)));
1080 free:
1081 free_string(name);
1084 static void do_array_assign(struct expression *left, int op, struct expression *right)
1086 struct range_list *rl;
1088 if (op == '=') {
1089 get_absolute_rl(right, &rl);
1090 rl = cast_rl(get_type(left), rl);
1091 } else {
1092 rl = alloc_whole_rl(get_type(left));
1095 set_extra_array_mod(left, alloc_estate_rl(rl));
1098 static void match_vanilla_assign(struct expression *left, struct expression *right)
1100 struct range_list *orig_rl = NULL;
1101 struct range_list *rl = NULL;
1102 struct symbol *right_sym;
1103 struct symbol *left_type;
1104 struct symbol *right_type;
1105 char *right_name = NULL;
1106 struct symbol *sym;
1107 char *name;
1108 sval_t sval, max;
1109 struct smatch_state *state;
1110 int comparison;
1112 if (is_struct(left))
1113 return;
1115 if (expr_equiv(left, right))
1116 return;
1118 save_chunk_info(left, right);
1120 name = expr_to_var_sym(left, &sym);
1121 if (!name) {
1122 if (chunk_has_array(left))
1123 do_array_assign(left, '=', right);
1124 return;
1127 left_type = get_type(left);
1128 right_type = get_type(right);
1130 right_name = expr_to_var_sym(right, &right_sym);
1132 if (!__in_fake_assign &&
1133 !(right->type == EXPR_PREOP && right->op == '&') &&
1134 right_name && right_sym &&
1135 values_fit_type(left, strip_expr(right)) &&
1136 !has_symbol(right, sym)) {
1137 set_equiv(left, right);
1138 goto free;
1141 if (get_implied_value(right, &sval)) {
1142 state = alloc_estate_sval(sval_cast(left_type, sval));
1143 goto done;
1146 if (__in_fake_assign || is_fake_var(left)) {
1147 struct smatch_state *right_state;
1148 struct range_list *rl;
1150 right_state = get_state(SMATCH_EXTRA, right_name, right_sym);
1151 if (right_state) {
1152 /* simple assignment */
1153 state = clone_estate(right_state);
1154 goto done;
1157 if (get_implied_rl(right, &rl)) {
1158 rl = cast_rl(left_type, rl);
1159 state = alloc_estate_rl(rl);
1160 goto done;
1163 rl = alloc_whole_rl(right_type);
1164 rl = cast_rl(left_type, rl);
1165 state = alloc_estate_rl(rl);
1166 goto done;
1169 comparison = get_comparison_no_extra(left, right);
1170 if (comparison) {
1171 comparison = flip_comparison(comparison);
1172 get_implied_rl(left, &orig_rl);
1175 if (get_implied_rl(right, &rl)) {
1176 rl = cast_rl(left_type, rl);
1177 if (orig_rl)
1178 filter_by_comparison(&rl, comparison, orig_rl);
1179 state = alloc_estate_rl(rl);
1180 if (get_hard_max(right, &max)) {
1181 estate_set_hard_max(state);
1182 estate_set_fuzzy_max(state, max);
1184 } else {
1185 rl = alloc_whole_rl(right_type);
1186 rl = cast_rl(left_type, rl);
1187 if (orig_rl)
1188 filter_by_comparison(&rl, comparison, orig_rl);
1189 state = alloc_estate_rl(rl);
1192 done:
1193 set_extra_mod(name, sym, left, state);
1194 free:
1195 free_string(right_name);
1198 static void match_assign(struct expression *expr)
1200 struct range_list *rl = NULL;
1201 struct expression *left;
1202 struct expression *right;
1203 struct expression *binop_expr;
1204 struct symbol *left_type;
1205 struct symbol *sym;
1206 char *name;
1208 left = strip_expr(expr->left);
1210 right = strip_parens(expr->right);
1211 if (right->type == EXPR_CALL && sym_name_is("__builtin_expect", right->fn))
1212 right = get_argument_from_call_expr(right->args, 0);
1213 while (right->type == EXPR_ASSIGNMENT && right->op == '=')
1214 right = strip_parens(right->left);
1216 if (expr->op == '=' && is_condition(expr->right))
1217 return; /* handled in smatch_condition.c */
1218 if (expr->op == '=' && right->type == EXPR_CALL &&
1219 !is_fake_call(right))
1220 return; /* handled in smatch_function_hooks.c */
1221 if (expr->op == '=') {
1222 match_vanilla_assign(left, right);
1223 return;
1226 name = expr_to_var_sym(left, &sym);
1227 if (!name)
1228 return;
1230 left_type = get_type(left);
1232 switch (expr->op) {
1233 case SPECIAL_ADD_ASSIGN:
1234 case SPECIAL_SUB_ASSIGN:
1235 case SPECIAL_AND_ASSIGN:
1236 case SPECIAL_MOD_ASSIGN:
1237 case SPECIAL_SHL_ASSIGN:
1238 case SPECIAL_SHR_ASSIGN:
1239 case SPECIAL_OR_ASSIGN:
1240 case SPECIAL_XOR_ASSIGN:
1241 case SPECIAL_MUL_ASSIGN:
1242 case SPECIAL_DIV_ASSIGN:
1243 binop_expr = binop_expression(expr->left,
1244 op_remove_assign(expr->op),
1245 expr->right);
1246 get_absolute_rl(binop_expr, &rl);
1247 rl = cast_rl(left_type, rl);
1248 if (inside_loop()) {
1249 if (expr->op == SPECIAL_ADD_ASSIGN)
1250 add_range(&rl, rl_max(rl), sval_type_max(rl_type(rl)));
1252 if (expr->op == SPECIAL_SUB_ASSIGN &&
1253 !sval_is_negative(rl_min(rl))) {
1254 sval_t zero = { .type = rl_type(rl) };
1256 add_range(&rl, rl_min(rl), zero);
1259 set_extra_mod(name, sym, left, alloc_estate_rl(rl));
1260 goto free;
1262 set_extra_mod(name, sym, left, alloc_estate_whole(left_type));
1263 free:
1264 free_string(name);
1267 static struct smatch_state *increment_state(struct smatch_state *state)
1269 sval_t min = estate_min(state);
1270 sval_t max = estate_max(state);
1272 if (!estate_rl(state))
1273 return NULL;
1275 if (inside_loop())
1276 max = sval_type_max(max.type);
1278 if (!sval_is_min(min) && !sval_is_max(min))
1279 min.value++;
1280 if (!sval_is_min(max) && !sval_is_max(max))
1281 max.value++;
1282 return alloc_estate_range(min, max);
1285 static struct smatch_state *decrement_state(struct smatch_state *state)
1287 sval_t min = estate_min(state);
1288 sval_t max = estate_max(state);
1290 if (!estate_rl(state))
1291 return NULL;
1293 if (inside_loop())
1294 min = sval_type_min(min.type);
1296 if (!sval_is_min(min) && !sval_is_max(min))
1297 min.value--;
1298 if (!sval_is_min(max) && !sval_is_max(max))
1299 max.value--;
1300 return alloc_estate_range(min, max);
1303 static void clear_pointed_at_state(struct expression *expr)
1305 struct symbol *type;
1308 * ALERT: This is sort of a mess. If it's is a struct assigment like
1309 * "foo = bar;", then that's handled by smatch_struct_assignment.c.
1310 * the same thing for p++ where "p" is a struct. Most modifications
1311 * are handled by the assignment hook or the db. Smatch_extra.c doesn't
1312 * use smatch_modification.c because we have to get the ordering right
1313 * or something. So if you have p++ where p is a pointer to a standard
1314 * c type then we handle that here. What a mess.
1316 expr = strip_expr(expr);
1317 type = get_type(expr);
1318 if (!type || type->type != SYM_PTR)
1319 return;
1320 type = get_real_base_type(type);
1321 if (!type || type->type != SYM_BASETYPE)
1322 return;
1323 set_extra_expr_nomod(deref_expression(expr), alloc_estate_whole(type));
1326 static void unop_expr(struct expression *expr)
1328 struct smatch_state *state;
1330 if (expr->smatch_flags & Handled)
1331 return;
1333 switch (expr->op) {
1334 case SPECIAL_INCREMENT:
1335 state = get_state_expr(SMATCH_EXTRA, expr->unop);
1336 state = increment_state(state);
1337 if (!state)
1338 state = alloc_estate_whole(get_type(expr));
1339 set_extra_expr_mod(expr->unop, state);
1340 clear_pointed_at_state(expr->unop);
1341 break;
1342 case SPECIAL_DECREMENT:
1343 state = get_state_expr(SMATCH_EXTRA, expr->unop);
1344 state = decrement_state(state);
1345 if (!state)
1346 state = alloc_estate_whole(get_type(expr));
1347 set_extra_expr_mod(expr->unop, state);
1348 clear_pointed_at_state(expr->unop);
1349 break;
1350 default:
1351 return;
1355 static void asm_expr(struct statement *stmt)
1357 struct asm_operand *op;
1358 struct symbol *type;
1360 FOR_EACH_PTR(stmt->asm_outputs, op) {
1361 type = get_type(strip_expr(op->expr));
1362 set_extra_expr_mod(op->expr, alloc_estate_whole(type));
1363 } END_FOR_EACH_PTR(op);
1366 static void check_dereference(struct expression *expr)
1368 struct smatch_state *state;
1370 if (__in_fake_assign)
1371 return;
1372 if (outside_of_function())
1373 return;
1374 state = get_extra_state(expr);
1375 if (state) {
1376 struct range_list *rl;
1378 rl = rl_intersection(estate_rl(state), valid_ptr_rl);
1379 if (rl_equiv(rl, estate_rl(state)))
1380 return;
1381 set_extra_expr_nomod(expr, alloc_estate_rl(rl));
1382 } else {
1383 struct range_list *rl;
1385 if (get_mtag_rl(expr, &rl))
1386 rl = rl_intersection(rl, valid_ptr_rl);
1387 else
1388 rl = clone_rl(valid_ptr_rl);
1390 set_extra_expr_nomod(expr, alloc_estate_rl(rl));
1394 static void match_dereferences(struct expression *expr)
1396 if (expr->type != EXPR_PREOP)
1397 return;
1398 if (getting_address(expr))
1399 return;
1400 /* it's saying that foo[1] = bar dereferences foo[1] */
1401 if (is_array(expr))
1402 return;
1403 check_dereference(expr->unop);
1406 static void match_pointer_as_array(struct expression *expr)
1408 if (!is_array(expr))
1409 return;
1410 check_dereference(get_array_base(expr));
1413 static void find_dereferences(struct expression *expr)
1415 while (expr->type == EXPR_PREOP) {
1416 if (expr->op == '*')
1417 check_dereference(expr->unop);
1418 expr = strip_expr(expr->unop);
1422 static void set_param_dereferenced(struct expression *call, struct expression *arg, char *key, char *unused)
1424 struct symbol *sym;
1425 char *name;
1427 name = get_variable_from_key(arg, key, &sym);
1428 if (name && sym) {
1429 struct smatch_state *orig, *new;
1430 struct range_list *rl;
1432 orig = get_state(SMATCH_EXTRA, name, sym);
1433 if (orig) {
1434 rl = rl_intersection(estate_rl(orig),
1435 alloc_rl(valid_ptr_min_sval,
1436 valid_ptr_max_sval));
1437 new = alloc_estate_rl(rl);
1438 } else {
1439 new = alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval);
1442 set_extra_nomod(name, sym, NULL, new);
1444 free_string(name);
1446 find_dereferences(arg);
1449 static sval_t add_one(sval_t sval)
1451 sval.value++;
1452 return sval;
1455 static int handle_postop_inc(struct expression *left, int op, struct expression *right)
1457 struct statement *stmt;
1458 struct expression *cond;
1459 struct smatch_state *true_state, *false_state;
1460 struct symbol *type;
1461 sval_t start;
1462 sval_t limit;
1465 * If we're decrementing here then that's a canonical while count down
1466 * so it's handled already. We're only handling loops like:
1467 * i = 0;
1468 * do { ... } while (i++ < 3);
1471 if (left->type != EXPR_POSTOP || left->op != SPECIAL_INCREMENT)
1472 return 0;
1474 stmt = __cur_stmt->parent;
1475 if (!stmt)
1476 return 0;
1477 if (stmt->type == STMT_COMPOUND)
1478 stmt = stmt->parent;
1479 if (!stmt || stmt->type != STMT_ITERATOR || !stmt->iterator_post_condition)
1480 return 0;
1482 cond = strip_expr(stmt->iterator_post_condition);
1483 if (cond->type != EXPR_COMPARE || cond->op != op)
1484 return 0;
1485 if (left != strip_expr(cond->left) || right != strip_expr(cond->right))
1486 return 0;
1488 if (!get_implied_value(left->unop, &start))
1489 return 0;
1490 if (!get_implied_value(right, &limit))
1491 return 0;
1492 type = get_type(left->unop);
1493 limit = sval_cast(type, limit);
1494 if (sval_cmp(start, limit) > 0)
1495 return 0;
1497 switch (op) {
1498 case '<':
1499 case SPECIAL_UNSIGNED_LT:
1500 break;
1501 case SPECIAL_LTE:
1502 case SPECIAL_UNSIGNED_LTE:
1503 limit = add_one(limit);
1504 default:
1505 return 0;
1509 true_state = alloc_estate_range(add_one(start), limit);
1510 false_state = alloc_estate_range(add_one(limit), add_one(limit));
1512 /* Currently we just discard the false state but when two passes is
1513 * implimented correctly then it will use it.
1516 set_extra_expr_true_false(left->unop, true_state, false_state);
1518 return 1;
1521 bool is_impossible_variable(struct expression *expr)
1523 struct smatch_state *state;
1525 state = get_extra_state(expr);
1526 if (state && !estate_rl(state))
1527 return true;
1528 return false;
1531 static bool in_macro(struct expression *left, struct expression *right)
1533 if (!left || !right)
1534 return 0;
1535 if (left->pos.line != right->pos.line || left->pos.pos != right->pos.pos)
1536 return 0;
1537 if (get_macro_name(left->pos))
1538 return 1;
1539 return 0;
1542 static void handle_comparison(struct symbol *type, struct expression *left, int op, struct expression *right)
1544 struct range_list *left_orig;
1545 struct range_list *left_true;
1546 struct range_list *left_false;
1547 struct range_list *right_orig;
1548 struct range_list *right_true;
1549 struct range_list *right_false;
1550 struct smatch_state *left_true_state;
1551 struct smatch_state *left_false_state;
1552 struct smatch_state *right_true_state;
1553 struct smatch_state *right_false_state;
1554 sval_t dummy, hard_max;
1555 int left_postop = 0;
1556 int right_postop = 0;
1558 if (left->op == SPECIAL_INCREMENT || left->op == SPECIAL_DECREMENT) {
1559 if (left->type == EXPR_POSTOP) {
1560 left->smatch_flags |= Handled;
1561 left_postop = left->op;
1562 if (handle_postop_inc(left, op, right))
1563 return;
1565 left = strip_parens(left->unop);
1567 while (left->type == EXPR_ASSIGNMENT)
1568 left = strip_parens(left->left);
1570 if (right->op == SPECIAL_INCREMENT || right->op == SPECIAL_DECREMENT) {
1571 if (right->type == EXPR_POSTOP) {
1572 right->smatch_flags |= Handled;
1573 right_postop = right->op;
1575 right = strip_parens(right->unop);
1578 if (is_impossible_variable(left) || is_impossible_variable(right))
1579 return;
1581 get_real_absolute_rl(left, &left_orig);
1582 left_orig = cast_rl(type, left_orig);
1584 get_real_absolute_rl(right, &right_orig);
1585 right_orig = cast_rl(type, right_orig);
1587 split_comparison_rl(left_orig, op, right_orig, &left_true, &left_false, &right_true, &right_false);
1589 left_true = rl_truncate_cast(get_type(strip_expr(left)), left_true);
1590 left_false = rl_truncate_cast(get_type(strip_expr(left)), left_false);
1591 right_true = rl_truncate_cast(get_type(strip_expr(right)), right_true);
1592 right_false = rl_truncate_cast(get_type(strip_expr(right)), right_false);
1594 if (!left_true || !left_false) {
1595 struct range_list *tmp_true, *tmp_false;
1597 split_comparison_rl(alloc_whole_rl(type), op, right_orig, &tmp_true, &tmp_false, NULL, NULL);
1598 tmp_true = rl_truncate_cast(get_type(strip_expr(left)), tmp_true);
1599 tmp_false = rl_truncate_cast(get_type(strip_expr(left)), tmp_false);
1600 if (tmp_true && tmp_false)
1601 __save_imaginary_state(left, tmp_true, tmp_false);
1604 if (!right_true || !right_false) {
1605 struct range_list *tmp_true, *tmp_false;
1607 split_comparison_rl(alloc_whole_rl(type), op, right_orig, NULL, NULL, &tmp_true, &tmp_false);
1608 tmp_true = rl_truncate_cast(get_type(strip_expr(right)), tmp_true);
1609 tmp_false = rl_truncate_cast(get_type(strip_expr(right)), tmp_false);
1610 if (tmp_true && tmp_false)
1611 __save_imaginary_state(right, tmp_true, tmp_false);
1614 left_true_state = alloc_estate_rl(left_true);
1615 left_false_state = alloc_estate_rl(left_false);
1616 right_true_state = alloc_estate_rl(right_true);
1617 right_false_state = alloc_estate_rl(right_false);
1619 switch (op) {
1620 case '<':
1621 case SPECIAL_UNSIGNED_LT:
1622 case SPECIAL_UNSIGNED_LTE:
1623 case SPECIAL_LTE:
1624 if (get_hard_max(right, &dummy) && !in_macro(left, right))
1625 estate_set_hard_max(left_true_state);
1626 if (get_hard_max(left, &dummy) && !in_macro(left, right))
1627 estate_set_hard_max(right_false_state);
1628 break;
1629 case '>':
1630 case SPECIAL_UNSIGNED_GT:
1631 case SPECIAL_UNSIGNED_GTE:
1632 case SPECIAL_GTE:
1633 if (get_hard_max(left, &dummy) && !in_macro(left, right))
1634 estate_set_hard_max(right_true_state);
1635 if (get_hard_max(right, &dummy) && !in_macro(left, right))
1636 estate_set_hard_max(left_false_state);
1637 break;
1640 switch (op) {
1641 case '<':
1642 case SPECIAL_UNSIGNED_LT:
1643 case SPECIAL_UNSIGNED_LTE:
1644 case SPECIAL_LTE:
1645 if (get_hard_max(right, &hard_max)) {
1646 if (op == '<' || op == SPECIAL_UNSIGNED_LT)
1647 hard_max.value--;
1648 estate_set_fuzzy_max(left_true_state, hard_max);
1650 if (get_implied_value(right, &hard_max)) {
1651 if (op == SPECIAL_UNSIGNED_LTE ||
1652 op == SPECIAL_LTE)
1653 hard_max.value++;
1654 estate_set_fuzzy_max(left_false_state, hard_max);
1656 if (get_hard_max(left, &hard_max)) {
1657 if (op == SPECIAL_UNSIGNED_LTE ||
1658 op == SPECIAL_LTE)
1659 hard_max.value--;
1660 estate_set_fuzzy_max(right_false_state, hard_max);
1662 if (get_implied_value(left, &hard_max)) {
1663 if (op == '<' || op == SPECIAL_UNSIGNED_LT)
1664 hard_max.value++;
1665 estate_set_fuzzy_max(right_true_state, hard_max);
1667 break;
1668 case '>':
1669 case SPECIAL_UNSIGNED_GT:
1670 case SPECIAL_UNSIGNED_GTE:
1671 case SPECIAL_GTE:
1672 if (get_hard_max(left, &hard_max)) {
1673 if (op == '>' || op == SPECIAL_UNSIGNED_GT)
1674 hard_max.value--;
1675 estate_set_fuzzy_max(right_true_state, hard_max);
1677 if (get_implied_value(left, &hard_max)) {
1678 if (op == SPECIAL_UNSIGNED_GTE ||
1679 op == SPECIAL_GTE)
1680 hard_max.value++;
1681 estate_set_fuzzy_max(right_false_state, hard_max);
1683 if (get_hard_max(right, &hard_max)) {
1684 if (op == SPECIAL_UNSIGNED_LTE ||
1685 op == SPECIAL_LTE)
1686 hard_max.value--;
1687 estate_set_fuzzy_max(left_false_state, hard_max);
1689 if (get_implied_value(right, &hard_max)) {
1690 if (op == '>' ||
1691 op == SPECIAL_UNSIGNED_GT)
1692 hard_max.value++;
1693 estate_set_fuzzy_max(left_true_state, hard_max);
1695 break;
1696 case SPECIAL_EQUAL:
1697 if (get_hard_max(left, &hard_max))
1698 estate_set_fuzzy_max(right_true_state, hard_max);
1699 if (get_hard_max(right, &hard_max))
1700 estate_set_fuzzy_max(left_true_state, hard_max);
1701 break;
1704 if (get_hard_max(left, &hard_max)) {
1705 estate_set_hard_max(left_true_state);
1706 estate_set_hard_max(left_false_state);
1708 if (get_hard_max(right, &hard_max)) {
1709 estate_set_hard_max(right_true_state);
1710 estate_set_hard_max(right_false_state);
1713 if (left_postop == SPECIAL_INCREMENT) {
1714 left_true_state = increment_state(left_true_state);
1715 left_false_state = increment_state(left_false_state);
1717 if (left_postop == SPECIAL_DECREMENT) {
1718 left_true_state = decrement_state(left_true_state);
1719 left_false_state = decrement_state(left_false_state);
1721 if (right_postop == SPECIAL_INCREMENT) {
1722 right_true_state = increment_state(right_true_state);
1723 right_false_state = increment_state(right_false_state);
1725 if (right_postop == SPECIAL_DECREMENT) {
1726 right_true_state = decrement_state(right_true_state);
1727 right_false_state = decrement_state(right_false_state);
1730 if (estate_rl(left_true_state) && estates_equiv(left_true_state, left_false_state)) {
1731 left_true_state = NULL;
1732 left_false_state = NULL;
1735 if (estate_rl(right_true_state) && estates_equiv(right_true_state, right_false_state)) {
1736 right_true_state = NULL;
1737 right_false_state = NULL;
1740 /* Don't introduce new states for known true/false conditions */
1741 if (rl_equiv(left_orig, estate_rl(left_true_state)))
1742 left_true_state = NULL;
1743 if (rl_equiv(left_orig, estate_rl(left_false_state)))
1744 left_false_state = NULL;
1745 if (rl_equiv(right_orig, estate_rl(right_true_state)))
1746 right_true_state = NULL;
1747 if (rl_equiv(right_orig, estate_rl(right_false_state)))
1748 right_false_state = NULL;
1750 set_extra_expr_true_false(left, left_true_state, left_false_state);
1751 set_extra_expr_true_false(right, right_true_state, right_false_state);
1754 static int is_simple_math(struct expression *expr)
1756 if (!expr)
1757 return 0;
1758 if (expr->type != EXPR_BINOP)
1759 return 0;
1760 switch (expr->op) {
1761 case '+':
1762 case '-':
1763 case '*':
1764 return 1;
1766 return 0;
1769 static int flip_op(int op)
1771 /* We only care about simple math */
1772 switch (op) {
1773 case '+':
1774 return '-';
1775 case '-':
1776 return '+';
1777 case '*':
1778 return '/';
1780 return 0;
1783 static void move_known_to_rl(struct expression **expr_p, struct range_list **rl_p)
1785 struct expression *expr = *expr_p;
1786 struct range_list *rl = *rl_p;
1787 sval_t sval;
1789 if (!is_simple_math(expr))
1790 return;
1792 if (get_implied_value(expr->right, &sval)) {
1793 *expr_p = expr->left;
1794 *rl_p = rl_binop(rl, flip_op(expr->op), alloc_rl(sval, sval));
1795 move_known_to_rl(expr_p, rl_p);
1796 return;
1798 if (expr->op == '-')
1799 return;
1800 if (get_implied_value(expr->left, &sval)) {
1801 *expr_p = expr->right;
1802 *rl_p = rl_binop(rl, flip_op(expr->op), alloc_rl(sval, sval));
1803 move_known_to_rl(expr_p, rl_p);
1804 return;
1808 static void move_known_values(struct expression **left_p, struct expression **right_p)
1810 struct expression *left = *left_p;
1811 struct expression *right = *right_p;
1812 sval_t sval, dummy;
1814 if (get_implied_value(left, &sval)) {
1815 if (!is_simple_math(right))
1816 return;
1817 if (get_implied_value(right, &dummy))
1818 return;
1819 if (right->op == '*') {
1820 sval_t divisor;
1822 if (!get_value(right->right, &divisor))
1823 return;
1824 if (divisor.value == 0)
1825 return;
1826 *left_p = binop_expression(left, invert_op(right->op), right->right);
1827 *right_p = right->left;
1828 return;
1830 if (right->op == '+' && get_value(right->left, &sval)) {
1831 *left_p = binop_expression(left, invert_op(right->op), right->left);
1832 *right_p = right->right;
1833 return;
1835 if (get_value(right->right, &sval)) {
1836 *left_p = binop_expression(left, invert_op(right->op), right->right);
1837 *right_p = right->left;
1838 return;
1840 return;
1842 if (get_implied_value(right, &sval)) {
1843 if (!is_simple_math(left))
1844 return;
1845 if (get_implied_value(left, &dummy))
1846 return;
1847 if (left->op == '*') {
1848 sval_t divisor;
1850 if (!get_value(left->right, &divisor))
1851 return;
1852 if (divisor.value == 0)
1853 return;
1854 *right_p = binop_expression(right, invert_op(left->op), left->right);
1855 *left_p = left->left;
1856 return;
1858 if (left->op == '+' && get_value(left->left, &sval)) {
1859 *right_p = binop_expression(right, invert_op(left->op), left->left);
1860 *left_p = left->right;
1861 return;
1864 if (get_value(left->right, &sval)) {
1865 *right_p = binop_expression(right, invert_op(left->op), left->right);
1866 *left_p = left->left;
1867 return;
1869 return;
1874 * The reason for do_simple_algebra() is to solve things like:
1875 * if (foo > 66 || foo + bar > 64) {
1876 * "foo" is not really a known variable so it won't be handled by
1877 * move_known_variables() but it's a super common idiom.
1880 static int do_simple_algebra(struct expression **left_p, struct expression **right_p)
1882 struct expression *left = *left_p;
1883 struct expression *right = *right_p;
1884 struct range_list *rl;
1885 sval_t tmp;
1887 if (left->type != EXPR_BINOP || left->op != '+')
1888 return 0;
1889 if (can_integer_overflow(get_type(left), left))
1890 return 0;
1891 if (!get_implied_value(right, &tmp))
1892 return 0;
1894 if (!get_implied_value(left->left, &tmp) &&
1895 get_implied_rl(left->left, &rl) &&
1896 !is_whole_rl(rl)) {
1897 *right_p = binop_expression(right, '-', left->left);
1898 *left_p = left->right;
1899 return 1;
1901 if (!get_implied_value(left->right, &tmp) &&
1902 get_implied_rl(left->right, &rl) &&
1903 !is_whole_rl(rl)) {
1904 *right_p = binop_expression(right, '-', left->right);
1905 *left_p = left->left;
1906 return 1;
1909 return 0;
1912 static int match_func_comparison(struct expression *expr)
1914 struct expression *left = strip_expr(expr->left);
1915 struct expression *right = strip_expr(expr->right);
1917 if (left->type == EXPR_CALL || right->type == EXPR_CALL) {
1918 function_comparison(left, expr->op, right);
1919 return 1;
1922 return 0;
1925 /* Handle conditions like "if (foo + bar < foo) {" */
1926 static int handle_integer_overflow_test(struct expression *expr)
1928 struct expression *left, *right;
1929 struct symbol *type;
1930 sval_t left_min, right_min, min, max;
1932 if (expr->op != '<' && expr->op != SPECIAL_UNSIGNED_LT)
1933 return 0;
1935 left = strip_parens(expr->left);
1936 right = strip_parens(expr->right);
1938 if (left->op != '+')
1939 return 0;
1941 type = get_type(expr);
1942 if (!type)
1943 return 0;
1944 if (type_positive_bits(type) == 32) {
1945 max.type = &uint_ctype;
1946 max.uvalue = (unsigned int)-1;
1947 } else if (type_positive_bits(type) == 64) {
1948 max.type = &ulong_ctype;
1949 max.value = (unsigned long long)-1;
1950 } else {
1951 return 0;
1954 if (!expr_equiv(left->left, right) && !expr_equiv(left->right, right))
1955 return 0;
1957 get_absolute_min(left->left, &left_min);
1958 get_absolute_min(left->right, &right_min);
1959 min = sval_binop(left_min, '+', right_min);
1961 type = get_type(left);
1962 min = sval_cast(type, min);
1963 max = sval_cast(type, max);
1965 set_extra_chunk_true_false(left, NULL, alloc_estate_range(min, max));
1966 return 1;
1969 static void match_comparison(struct expression *expr)
1971 struct expression *left_orig = strip_parens(expr->left);
1972 struct expression *right_orig = strip_parens(expr->right);
1973 struct expression *left, *right, *tmp;
1974 struct expression *prev;
1975 struct symbol *type;
1976 int redo, count;
1978 if (match_func_comparison(expr))
1979 return;
1981 type = get_type(expr);
1982 if (!type)
1983 type = &llong_ctype;
1985 if (handle_integer_overflow_test(expr))
1986 return;
1988 left = left_orig;
1989 right = right_orig;
1990 move_known_values(&left, &right);
1991 handle_comparison(type, left, expr->op, right);
1993 left = left_orig;
1994 right = right_orig;
1995 if (do_simple_algebra(&left, &right))
1996 handle_comparison(type, left, expr->op, right);
1998 prev = get_assigned_expr(left_orig);
1999 if (is_simple_math(prev) && !has_variable(prev, left_orig)) {
2000 left = prev;
2001 right = right_orig;
2002 move_known_values(&left, &right);
2003 handle_comparison(type, left, expr->op, right);
2006 prev = get_assigned_expr(right_orig);
2007 if (is_simple_math(prev) && !has_variable(prev, right_orig)) {
2008 left = left_orig;
2009 right = prev;
2010 move_known_values(&left, &right);
2011 handle_comparison(type, left, expr->op, right);
2014 redo = 0;
2015 left = left_orig;
2016 right = right_orig;
2017 if (get_last_expr_from_expression_stmt(left_orig)) {
2018 left = get_last_expr_from_expression_stmt(left_orig);
2019 redo = 1;
2021 if (get_last_expr_from_expression_stmt(right_orig)) {
2022 right = get_last_expr_from_expression_stmt(right_orig);
2023 redo = 1;
2026 if (!redo)
2027 return;
2029 count = 0;
2030 while ((tmp = get_assigned_expr(left))) {
2031 if (count++ > 3)
2032 break;
2033 left = strip_expr(tmp);
2035 count = 0;
2036 while ((tmp = get_assigned_expr(right))) {
2037 if (count++ > 3)
2038 break;
2039 right = strip_expr(tmp);
2042 handle_comparison(type, left, expr->op, right);
2045 static sval_t get_high_mask(sval_t known)
2047 sval_t ret;
2048 int i;
2050 ret = known;
2051 ret.value = 0;
2053 for (i = type_bits(known.type) - 1; i >= 0; i--) {
2054 if (known.uvalue & (1ULL << i))
2055 ret.uvalue |= (1ULL << i);
2056 else
2057 return ret;
2060 return ret;
2063 static bool handle_bit_test(struct expression *expr)
2065 struct range_list *orig_rl, *rlt, *rlf, *true_rl, *false_rl;
2066 struct expression *shift, *mask, *var;
2067 struct bit_info *bit_info;
2068 sval_t sval;
2069 sval_t high = { .type = &int_ctype };
2070 sval_t low = { .type = &int_ctype };
2072 shift = strip_expr(expr->right);
2073 mask = strip_expr(expr->left);
2074 if (shift->type != EXPR_BINOP || shift->op != SPECIAL_LEFTSHIFT) {
2075 shift = strip_expr(expr->left);
2076 mask = strip_expr(expr->right);
2077 if (shift->type != EXPR_BINOP || shift->op != SPECIAL_LEFTSHIFT)
2078 return false;
2080 if (!get_implied_value(shift->left, &sval) || sval.value != 1)
2081 return false;
2082 var = strip_expr(shift->right);
2084 bit_info = get_bit_info(mask);
2085 if (!bit_info)
2086 return false;
2087 if (!bit_info->possible){
2088 set_true_false_states_expr(my_id, var, alloc_estate_empty(), NULL);
2089 return false;
2092 get_absolute_rl(var, &orig_rl);
2093 if (sval_is_negative(rl_min(orig_rl)) ||
2094 rl_max(orig_rl).uvalue > type_bits(get_type(shift->left)))
2095 return false;
2097 low.value = ffsll(bit_info->possible) - 1;
2098 high.value = sm_fls64(bit_info->possible) - 1;
2099 rlt = alloc_rl(low, high);
2100 rlt = cast_rl(get_type(var), rlt);
2101 true_rl = rl_intersection(orig_rl, rlt);
2103 low.value = ffsll(bit_info->set) - 1;
2104 high.value = sm_fls64(bit_info->set) - 1;
2105 rlf = alloc_rl(low, high);
2106 rlf = cast_rl(get_type(var), rlf);
2107 false_rl = rl_filter(orig_rl, rlf);
2109 set_extra_expr_true_false(var, alloc_estate_rl(true_rl), alloc_estate_rl(false_rl));
2111 return true;
2114 static void handle_AND_op(struct expression *var, sval_t known)
2116 struct range_list *orig_rl;
2117 struct range_list *true_rl = NULL;
2118 struct range_list *false_rl = NULL;
2119 int bit;
2120 sval_t low_mask = known;
2121 sval_t high_mask;
2122 sval_t max;
2124 get_absolute_rl(var, &orig_rl);
2126 if (known.value > 0) {
2127 bit = ffsll(known.value) - 1;
2128 low_mask.uvalue = (1ULL << bit) - 1;
2129 true_rl = remove_range(orig_rl, sval_type_val(known.type, 0), low_mask);
2131 high_mask = get_high_mask(known);
2132 if (high_mask.value) {
2133 bit = ffsll(high_mask.value) - 1;
2134 low_mask.uvalue = (1ULL << bit) - 1;
2136 false_rl = orig_rl;
2137 if (sval_is_negative(rl_min(orig_rl)))
2138 false_rl = remove_range(false_rl, sval_type_min(known.type), sval_type_val(known.type, -1));
2139 false_rl = remove_range(false_rl, low_mask, sval_type_max(known.type));
2140 if (type_signed(high_mask.type) && type_unsigned(rl_type(false_rl))) {
2141 false_rl = remove_range(false_rl,
2142 sval_type_val(rl_type(false_rl), sval_type_max(known.type).uvalue),
2143 sval_type_val(rl_type(false_rl), -1));
2145 } else if (known.value == 1 &&
2146 get_hard_max(var, &max) &&
2147 sval_cmp(max, rl_max(orig_rl)) == 0 &&
2148 max.value & 1) {
2149 false_rl = remove_range(orig_rl, max, max);
2151 set_extra_expr_true_false(var,
2152 true_rl ? alloc_estate_rl(true_rl) : NULL,
2153 false_rl ? alloc_estate_rl(false_rl) : NULL);
2156 static void handle_AND_condition(struct expression *expr)
2158 sval_t known;
2160 if (handle_bit_test(expr))
2161 return;
2163 if (get_implied_value(expr->left, &known))
2164 handle_AND_op(expr->right, known);
2165 else if (get_implied_value(expr->right, &known))
2166 handle_AND_op(expr->left, known);
2169 static void handle_MOD_condition(struct expression *expr)
2171 struct range_list *orig_rl;
2172 struct range_list *true_rl;
2173 struct range_list *false_rl = NULL;
2174 sval_t right;
2175 sval_t zero = {
2176 .value = 0,
2179 if (!get_implied_value(expr->right, &right) || right.value == 0)
2180 return;
2181 get_absolute_rl(expr->left, &orig_rl);
2183 zero.type = rl_type(orig_rl);
2185 /* We're basically dorking around the min and max here */
2186 true_rl = remove_range(orig_rl, zero, zero);
2187 if (!sval_is_max(rl_max(true_rl)) &&
2188 !(rl_max(true_rl).value % right.value))
2189 true_rl = remove_range(true_rl, rl_max(true_rl), rl_max(true_rl));
2191 if (rl_equiv(true_rl, orig_rl))
2192 true_rl = NULL;
2194 if (sval_is_positive(rl_min(orig_rl)) &&
2195 (rl_max(orig_rl).value - rl_min(orig_rl).value) / right.value < 5) {
2196 sval_t add;
2197 int i;
2199 add = rl_min(orig_rl);
2200 add.value += right.value - (add.value % right.value);
2201 add.value -= right.value;
2203 for (i = 0; i < 5; i++) {
2204 add.value += right.value;
2205 if (add.value > rl_max(orig_rl).value)
2206 break;
2207 add_range(&false_rl, add, add);
2209 } else {
2210 if (rl_min(orig_rl).uvalue != 0 &&
2211 rl_min(orig_rl).uvalue < right.uvalue) {
2212 sval_t chop = right;
2213 chop.value--;
2214 false_rl = remove_range(orig_rl, zero, chop);
2217 if (!sval_is_max(rl_max(orig_rl)) &&
2218 (rl_max(orig_rl).value % right.value)) {
2219 sval_t chop = rl_max(orig_rl);
2220 chop.value -= chop.value % right.value;
2221 chop.value++;
2222 if (!false_rl)
2223 false_rl = clone_rl(orig_rl);
2224 false_rl = remove_range(false_rl, chop, rl_max(orig_rl));
2228 set_extra_expr_true_false(expr->left,
2229 true_rl ? alloc_estate_rl(true_rl) : NULL,
2230 false_rl ? alloc_estate_rl(false_rl) : NULL);
2233 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
2234 void __extra_match_condition(struct expression *expr)
2236 expr = strip_expr(expr);
2237 switch (expr->type) {
2238 case EXPR_CALL:
2239 function_comparison(expr, SPECIAL_NOTEQUAL, zero_expr());
2240 return;
2241 case EXPR_PREOP:
2242 case EXPR_SYMBOL:
2243 case EXPR_DEREF:
2244 handle_comparison(get_type(expr), expr, SPECIAL_NOTEQUAL, zero_expr());
2245 return;
2246 case EXPR_COMPARE:
2247 match_comparison(expr);
2248 return;
2249 case EXPR_ASSIGNMENT:
2250 __extra_match_condition(expr->left);
2251 return;
2252 case EXPR_BINOP:
2253 if (expr->op == '&')
2254 handle_AND_condition(expr);
2255 if (expr->op == '%')
2256 handle_MOD_condition(expr);
2257 return;
2261 static void assume_indexes_are_valid(struct expression *expr)
2263 struct expression *array_expr;
2264 int array_size;
2265 struct expression *offset;
2266 struct symbol *offset_type;
2267 struct range_list *rl_before;
2268 struct range_list *rl_after;
2269 struct range_list *filter = NULL;
2270 sval_t size;
2272 expr = strip_expr(expr);
2273 if (!is_array(expr))
2274 return;
2276 offset = get_array_offset(expr);
2277 offset_type = get_type(offset);
2278 if (offset_type && type_signed(offset_type)) {
2279 filter = alloc_rl(sval_type_min(offset_type),
2280 sval_type_val(offset_type, -1));
2283 array_expr = get_array_base(expr);
2284 array_size = get_real_array_size(array_expr);
2285 if (array_size > 1) {
2286 size = sval_type_val(offset_type, array_size);
2287 add_range(&filter, size, sval_type_max(offset_type));
2290 if (!filter)
2291 return;
2292 get_absolute_rl(offset, &rl_before);
2293 rl_after = rl_filter(rl_before, filter);
2294 if (rl_equiv(rl_before, rl_after))
2295 return;
2296 set_extra_expr_nomod(offset, alloc_estate_rl(rl_after));
2299 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
2300 int implied_not_equal(struct expression *expr, long long val)
2302 return !possibly_false(expr, SPECIAL_NOTEQUAL, value_expr(val));
2305 int implied_not_equal_name_sym(char *name, struct symbol *sym, long long val)
2307 struct smatch_state *estate;
2309 estate = get_state(SMATCH_EXTRA, name, sym);
2310 if (!estate)
2311 return 0;
2312 if (!rl_has_sval(estate_rl(estate), sval_type_val(estate_type(estate), 0)))
2313 return 1;
2314 return 0;
2317 bool is_noderef_ptr(struct expression *expr)
2319 struct range_list *rl;
2321 if (!get_implied_rl(expr, &rl))
2322 return false;
2323 return is_noderef_ptr_rl(rl);
2326 static int parent_is_err_or_null_var_sym_helper(const char *name, struct symbol *sym, bool check_err_ptr)
2328 struct smatch_state *state;
2329 char buf[256];
2330 char *start;
2331 int len;
2333 strncpy(buf, name, sizeof(buf) - 1);
2334 buf[sizeof(buf) - 1] = '\0';
2336 start = &buf[0];
2337 while (*start == '*') {
2338 start++;
2339 state = __get_state(SMATCH_EXTRA, start, sym);
2340 if (!state)
2341 continue;
2342 if (!estate_rl(state))
2343 return 1;
2344 if (is_noderef_ptr_rl(estate_rl(state)))
2345 return 1;
2348 start = &buf[0];
2349 while (*start == '&')
2350 start++;
2352 len = strlen(start);
2353 while (true) {
2354 while (len > 0) {
2355 len--;
2356 if (start[len] == '-' ||
2357 start[len] == '.') {
2358 start[len] = '\0';
2359 break;
2362 if (len == 0)
2363 return 0;
2364 state = __get_state(SMATCH_EXTRA, start, sym);
2365 if (!state)
2366 continue;
2367 if (is_noderef_ptr_rl(estate_rl(state)))
2368 return 1;
2372 int parent_is_null_var_sym(const char *name, struct symbol *sym)
2374 return parent_is_err_or_null_var_sym_helper(name, sym, false);
2377 int parent_is_err_or_null_var_sym(const char *name, struct symbol *sym)
2379 return parent_is_err_or_null_var_sym_helper(name, sym, (option_project == PROJ_KERNEL));
2382 int parent_is_null(struct expression *expr)
2384 struct symbol *sym;
2385 char *var;
2386 int ret = 0;
2388 expr = strip_expr(expr);
2389 var = expr_to_var_sym(expr, &sym);
2390 if (!var || !sym)
2391 goto free;
2392 ret = parent_is_null_var_sym(var, sym);
2393 free:
2394 free_string(var);
2395 return ret;
2398 static int param_used_callback(void *found, int argc, char **argv, char **azColName)
2400 *(int *)found = 1;
2401 return 0;
2404 static int is_kzalloc_info(struct sm_state *sm)
2406 sval_t sval;
2409 * kzalloc() information is treated as special because so there is just
2410 * a lot of stuff initialized to zero and it makes building the database
2411 * take hours and hours.
2413 * In theory, we should just remove this line and not pass any unused
2414 * information, but I'm not sure enough that this code works so I want
2415 * to hold off on that for now.
2417 if (!estate_get_single_value(sm->state, &sval))
2418 return 0;
2419 if (sval.value != 0)
2420 return 0;
2421 return 1;
2424 static int is_really_long(struct sm_state *sm)
2426 const char *p;
2427 int cnt = 0;
2429 p = sm->name;
2430 while ((p = strstr(p, "->"))) {
2431 p += 2;
2432 cnt++;
2435 if (cnt < 3 ||
2436 strlen(sm->name) < 40)
2437 return 0;
2438 return 1;
2441 static int filter_unused_param_value_info(struct expression *call, int param, char *printed_name, struct sm_state *sm)
2443 int found = 0;
2445 /* for function pointers assume everything is used */
2446 if (call->fn->type != EXPR_SYMBOL)
2447 return 0;
2449 if (strcmp(printed_name, "$") == 0 ||
2450 strcmp(printed_name, "*$") == 0)
2451 return 0;
2454 * This is to handle __builtin_mul_overflow(). In an ideal world we
2455 * would only need this for invalid code.
2458 if (!call->fn->symbol)
2459 return 0;
2461 if (!is_kzalloc_info(sm) && !is_really_long(sm))
2462 return 0;
2464 run_sql(&param_used_callback, &found,
2465 "select * from return_implies where %s and type = %d and parameter = %d and key = '%s';",
2466 get_static_filter(call->fn->symbol), PARAM_USED, param, printed_name);
2467 if (found)
2468 return 0;
2470 /* If the database is not built yet, then assume everything is used */
2471 run_sql(&param_used_callback, &found,
2472 "select * from return_implies where %s and type = %d;",
2473 get_static_filter(call->fn->symbol), PARAM_USED);
2474 if (!found)
2475 return 0;
2477 return 1;
2480 struct range_list *intersect_with_real_abs_var_sym(const char *name, struct symbol *sym, struct range_list *start)
2482 struct smatch_state *state;
2485 * Here is the difference between implied value and real absolute, say
2486 * you have:
2488 * int a = (u8)x;
2490 * Then you know that a is 0-255. That's real absolute. But you don't
2491 * know for sure that it actually goes up to 255. So it's not implied.
2492 * Implied indicates a degree of certainty.
2494 * But then say you cap "a" at 8. That means you know it goes up to
2495 * 8. So now the implied value is s32min-8. But you can combine it
2496 * with the real absolute to say that actually it's 0-8.
2498 * We are combining it here. But now that I think about it, this is
2499 * probably not the ideal place to combine it because it should proably
2500 * be done earlier. Oh well, this is an improvement on what was there
2501 * before so I'm going to commit this code.
2505 state = get_real_absolute_state_var_sym(name, sym);
2506 if (!state || !estate_rl(state))
2507 return start;
2509 return rl_intersection(estate_rl(state), start);
2512 struct range_list *intersect_with_real_abs_expr(struct expression *expr, struct range_list *start)
2514 struct smatch_state *state;
2515 struct range_list *abs_rl;
2517 state = get_real_absolute_state(expr);
2518 if (!state || !estate_rl(state))
2519 return start;
2521 abs_rl = cast_rl(rl_type(start), estate_rl(state));
2522 return rl_intersection(abs_rl, start);
2525 static void caller_info_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
2527 struct range_list *rl;
2528 sval_t dummy;
2530 if (estate_is_whole(sm->state) || !estate_rl(sm->state))
2531 return;
2532 if (filter_unused_param_value_info(call, param, printed_name, sm))
2533 return;
2534 rl = estate_rl(sm->state);
2535 rl = intersect_with_real_abs_var_sym(sm->name, sm->sym, rl);
2536 if (!rl)
2537 return;
2538 sql_insert_caller_info(call, PARAM_VALUE, param, printed_name, show_rl(rl));
2539 if (!estate_get_single_value(sm->state, &dummy)) {
2540 if (estate_has_hard_max(sm->state))
2541 sql_insert_caller_info(call, HARD_MAX, param, printed_name,
2542 sval_to_str(estate_max(sm->state)));
2543 if (estate_has_fuzzy_max(sm->state))
2544 sql_insert_caller_info(call, FUZZY_MAX, param, printed_name,
2545 sval_to_str(estate_get_fuzzy_max(sm->state)));
2549 static void returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
2551 struct symbol *returned_sym;
2552 char *returned_name;
2553 struct sm_state *sm;
2554 char *compare_str;
2555 char name_buf[256];
2556 char val_buf[256];
2557 int len;
2559 // FIXME handle *$
2561 if (!is_pointer(expr))
2562 return;
2563 if (return_ranges && strstr(return_ranges, "[==$"))
2564 return;
2566 returned_name = expr_to_var_sym(expr, &returned_sym);
2567 if (!returned_name || !returned_sym)
2568 goto free;
2569 len = strlen(returned_name);
2571 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
2572 if (!estate_rl(sm->state))
2573 continue;
2574 if (returned_sym != sm->sym)
2575 continue;
2576 if (strncmp(returned_name, sm->name, len) != 0)
2577 continue;
2578 if (sm->name[len] != '-')
2579 continue;
2581 snprintf(name_buf, sizeof(name_buf), "$%s", sm->name + len);
2583 compare_str = name_sym_to_param_comparison(sm->name, sm->sym);
2584 if (!compare_str && estate_is_whole(sm->state))
2585 continue;
2586 snprintf(val_buf, sizeof(val_buf), "%s%s", sm->state->name, compare_str ?: "");
2588 sql_insert_return_states(return_id, return_ranges, PARAM_VALUE,
2589 -1, name_buf, val_buf);
2590 } END_FOR_EACH_SM(sm);
2592 free:
2593 free_string(returned_name);
2596 static void db_limited_before(void)
2598 unmatched_stree = clone_stree(__get_cur_stree());
2601 static void db_limited_after(void)
2603 free_stree(&unmatched_stree);
2606 static int basically_the_same(struct range_list *orig, struct range_list *new)
2608 if (type_is_ptr(rl_type(orig)) &&
2609 is_whole_ptr_rl(orig) &&
2610 is_whole_ptr_rl(new))
2611 return true;
2613 return rl_equiv(orig, new);
2616 static void db_param_limit_binops(struct expression *arg, char *key, struct range_list *rl)
2618 struct range_list *left_rl;
2619 sval_t zero = { .type = rl_type(rl), };
2620 sval_t sval;
2622 if (strcmp(key, "$") != 0)
2623 return;
2624 if (arg->op != '*')
2625 return;
2626 if (!get_implied_value(arg->right, &sval))
2627 return;
2628 if (can_integer_overflow(get_type(arg), arg))
2629 return;
2631 left_rl = rl_binop(rl, '/', alloc_rl(sval, sval));
2632 if (!rl_has_sval(rl, zero))
2633 left_rl = remove_range(left_rl, zero, zero);
2635 set_extra_expr_nomod(arg->left, alloc_estate_rl(left_rl));
2638 static void db_param_limit_filter(struct expression *expr, int param, char *key, char *value, enum info_type op)
2640 struct smatch_state *state;
2641 struct expression *arg;
2642 char *name;
2643 struct symbol *sym;
2644 struct var_sym_list *vsl = NULL;
2645 struct sm_state *sm;
2646 struct symbol *compare_type, *var_type;
2647 struct range_list *rl;
2648 struct range_list *limit;
2649 struct range_list *new;
2650 char *other_name;
2651 struct symbol *other_sym;
2653 while (expr->type == EXPR_ASSIGNMENT)
2654 expr = strip_expr(expr->right);
2655 if (expr->type != EXPR_CALL)
2656 return;
2658 arg = get_argument_from_call_expr(expr->args, param);
2659 if (!arg)
2660 return;
2662 if (strcmp(key, "$") == 0)
2663 compare_type = get_arg_type(expr->fn, param);
2664 else
2665 compare_type = get_member_type_from_key(arg, key);
2667 call_results_to_rl(expr, compare_type, value, &limit);
2668 if (strcmp(key, "$") == 0)
2669 move_known_to_rl(&arg, &limit);
2670 name = get_chunk_from_key(arg, key, &sym, &vsl);
2671 if (!name)
2672 return;
2673 if (op != PARAM_LIMIT && !sym)
2674 goto free;
2676 sm = get_sm_state(SMATCH_EXTRA, name, sym);
2677 if (sm)
2678 rl = estate_rl(sm->state);
2679 else
2680 rl = alloc_whole_rl(compare_type);
2682 if (op == PARAM_LIMIT && !rl_fits_in_type(rl, compare_type))
2683 goto free;
2685 new = rl_intersection(rl, limit);
2687 var_type = get_member_type_from_key(arg, key);
2688 new = cast_rl(var_type, new);
2690 /* We want to preserve the implications here */
2691 if (sm && basically_the_same(rl, new))
2692 goto free;
2693 other_name = get_other_name_sym(name, sym, &other_sym);
2695 state = alloc_estate_rl(new);
2696 if (sm && estate_has_hard_max(sm->state))
2697 estate_set_hard_max(state);
2699 if (op == PARAM_LIMIT) {
2700 set_extra_nomod_vsl(name, sym, vsl, NULL, state);
2701 } else
2702 set_extra_mod(name, sym, NULL, state);
2704 if (other_name && other_sym) {
2705 state = clone_estate(state);
2706 if (op == PARAM_LIMIT)
2707 set_extra_nomod_vsl(other_name, other_sym, vsl, NULL, state);
2708 else
2709 set_extra_mod(other_name, other_sym, NULL, state);
2712 if (op == PARAM_LIMIT && arg->type == EXPR_BINOP)
2713 db_param_limit_binops(arg, key, new);
2714 free:
2715 free_string(name);
2718 static void db_param_limit(struct expression *expr, int param, char *key, char *value)
2720 db_param_limit_filter(expr, param, key, value, PARAM_LIMIT);
2723 static void db_param_filter(struct expression *expr, int param, char *key, char *value)
2725 db_param_limit_filter(expr, param, key, value, PARAM_FILTER);
2728 static void db_param_add_set(struct expression *expr, int param, char *key, char *value, enum info_type op)
2730 struct expression *arg, *gen_expr;
2731 char *name;
2732 char *other_name = NULL;
2733 struct symbol *sym, *other_sym;
2734 struct symbol *param_type, *arg_type;
2735 struct smatch_state *state;
2736 struct range_list *new = NULL;
2737 struct range_list *added = NULL;
2739 while (expr->type == EXPR_ASSIGNMENT)
2740 expr = strip_expr(expr->right);
2741 if (expr->type != EXPR_CALL)
2742 return;
2744 arg = get_argument_from_call_expr(expr->args, param);
2745 if (!arg)
2746 return;
2748 arg_type = get_arg_type_from_key(expr->fn, param, arg, key);
2749 param_type = get_member_type_from_key(arg, key);
2750 if (param_type && param_type->type == SYM_STRUCT)
2751 return;
2752 name = get_variable_from_key(arg, key, &sym);
2753 if (!name || !sym)
2754 goto free;
2755 gen_expr = gen_expression_from_key(arg, key);
2757 state = get_state(SMATCH_EXTRA, name, sym);
2758 if (state)
2759 new = estate_rl(state);
2761 call_results_to_rl(expr, arg_type, value, &added);
2762 added = cast_rl(param_type, added);
2763 if (op == PARAM_SET)
2764 new = added;
2765 else
2766 new = rl_union(new, added);
2768 other_name = get_other_name_sym_nostack(name, sym, &other_sym);
2769 set_extra_mod(name, sym, gen_expr, alloc_estate_rl(new));
2770 if (other_name && other_sym)
2771 set_extra_mod(other_name, other_sym, gen_expr, alloc_estate_rl(new));
2772 free:
2773 free_string(other_name);
2774 free_string(name);
2777 static void db_param_add(struct expression *expr, int param, char *key, char *value)
2779 in_param_set = true;
2780 db_param_add_set(expr, param, key, value, PARAM_ADD);
2781 in_param_set = false;
2784 static void db_param_set(struct expression *expr, int param, char *key, char *value)
2786 in_param_set = true;
2787 db_param_add_set(expr, param, key, value, PARAM_SET);
2788 in_param_set = false;
2791 static void match_lost_param(struct expression *call, int param)
2793 struct expression *arg;
2795 if (is_const_param(call->fn, param))
2796 return;
2798 arg = get_argument_from_call_expr(call->args, param);
2799 if (!arg)
2800 return;
2802 arg = strip_expr(arg);
2803 if (arg->type == EXPR_PREOP && arg->op == '&')
2804 set_extra_expr_mod(arg->unop, alloc_estate_whole(get_type(arg->unop)));
2805 else
2806 ; /* if pointer then set struct members, maybe?*/
2809 static void db_param_value(struct expression *expr, int param, char *key, char *value)
2811 struct expression *call;
2812 char *name;
2813 struct symbol *sym;
2814 struct symbol *type;
2815 struct range_list *rl = NULL;
2817 if (param != -1)
2818 return;
2820 call = expr;
2821 while (call->type == EXPR_ASSIGNMENT)
2822 call = strip_expr(call->right);
2823 if (call->type != EXPR_CALL)
2824 return;
2826 type = get_member_type_from_key(expr->left, key);
2827 name = get_variable_from_key(expr->left, key, &sym);
2828 if (!name || !sym)
2829 goto free;
2831 call_results_to_rl(call, type, value, &rl);
2833 set_extra_mod(name, sym, NULL, alloc_estate_rl(rl));
2834 free:
2835 free_string(name);
2838 static void set_param_value(const char *name, struct symbol *sym, char *key, char *value)
2840 struct expression *expr;
2841 struct range_list *rl = NULL;
2842 struct smatch_state *state;
2843 struct symbol *type;
2844 char *key_orig = key;
2845 char *fullname;
2846 sval_t dummy;
2848 expr = symbol_expression(sym);
2849 fullname = get_variable_from_key(expr, key, NULL);
2850 if (!fullname)
2851 return;
2853 type = get_member_type_from_key(expr, key_orig);
2854 str_to_rl(type, value, &rl);
2855 state = alloc_estate_rl(rl);
2856 if (estate_get_single_value(state, &dummy))
2857 estate_set_hard_max(state);
2858 set_state(SMATCH_EXTRA, fullname, sym, state);
2861 static void set_param_fuzzy_max(const char *name, struct symbol *sym, char *key, char *value)
2863 struct expression *expr;
2864 struct range_list *rl = NULL;
2865 struct smatch_state *state;
2866 struct symbol *type;
2867 char *fullname;
2868 sval_t max;
2870 expr = symbol_expression(sym);
2871 fullname = get_variable_from_key(expr, key, NULL);
2872 if (!fullname)
2873 return;
2875 state = get_state(SMATCH_EXTRA, fullname, sym);
2876 if (!state)
2877 return;
2878 type = estate_type(state);
2879 str_to_rl(type, value, &rl);
2880 if (!rl_to_sval(rl, &max))
2881 return;
2882 estate_set_fuzzy_max(state, max);
2885 static void set_param_hard_max(const char *name, struct symbol *sym, char *key, char *value)
2887 struct smatch_state *state;
2888 struct expression *expr;
2889 char *fullname;
2891 expr = symbol_expression(sym);
2892 fullname = get_variable_from_key(expr, key, NULL);
2893 if (!fullname)
2894 return;
2896 state = get_state(SMATCH_EXTRA, fullname, sym);
2897 if (!state)
2898 return;
2899 estate_set_hard_max(state);
2902 static struct sm_state *get_sm_from_call(struct expression *expr)
2904 char buf[32];
2906 if (is_fake_call(expr))
2907 return NULL;
2909 snprintf(buf, sizeof(buf), "return %p", expr);
2910 return get_sm_state(SMATCH_EXTRA, buf, NULL);
2913 struct sm_state *get_extra_sm_state(struct expression *expr)
2915 char *name;
2916 struct symbol *sym;
2917 struct sm_state *ret = NULL;
2919 expr = strip_expr(expr);
2920 if (!expr)
2921 return NULL;
2923 if (expr->type == EXPR_CALL)
2924 return get_sm_from_call(expr);
2926 name = expr_to_known_chunk_sym(expr, &sym);
2927 if (!name)
2928 goto free;
2930 ret = get_sm_state(SMATCH_EXTRA, name, sym);
2931 free:
2932 free_string(name);
2933 return ret;
2936 struct smatch_state *get_extra_state(struct expression *expr)
2938 struct sm_state *sm;
2940 sm = get_extra_sm_state(expr);
2941 if (!sm)
2942 return NULL;
2943 return sm->state;
2946 void register_smatch_extra(int id)
2948 my_id = id;
2950 set_dynamic_states(my_id);
2951 add_merge_hook(my_id, &merge_estates);
2952 add_unmatched_state_hook(my_id, &unmatched_state);
2953 select_caller_info_hook(set_param_value, PARAM_VALUE);
2954 select_caller_info_hook(set_param_fuzzy_max, FUZZY_MAX);
2955 select_caller_info_hook(set_param_hard_max, HARD_MAX);
2956 select_return_states_before(&db_limited_before);
2957 select_return_states_hook(PARAM_LIMIT, &db_param_limit);
2958 select_return_states_hook(PARAM_FILTER, &db_param_filter);
2959 select_return_states_hook(PARAM_ADD, &db_param_add);
2960 select_return_states_hook(PARAM_SET, &db_param_set);
2961 add_lost_param_hook(&match_lost_param);
2962 select_return_states_hook(PARAM_VALUE, &db_param_value);
2963 select_return_states_after(&db_limited_after);
2966 static void match_link_modify(struct sm_state *sm, struct expression *mod_expr)
2968 struct var_sym_list *links;
2969 struct var_sym *tmp;
2970 struct smatch_state *state;
2972 links = sm->state->data;
2974 FOR_EACH_PTR(links, tmp) {
2975 if (sm->sym == tmp->sym &&
2976 strcmp(sm->name, tmp->var) == 0)
2977 continue;
2978 state = get_state(SMATCH_EXTRA, tmp->var, tmp->sym);
2979 if (!state)
2980 continue;
2981 set_state(SMATCH_EXTRA, tmp->var, tmp->sym, alloc_estate_whole(estate_type(state)));
2982 } END_FOR_EACH_PTR(tmp);
2983 set_state(link_id, sm->name, sm->sym, &undefined);
2986 void register_smatch_extra_links(int id)
2988 link_id = id;
2989 set_dynamic_states(link_id);
2992 void register_smatch_extra_late(int id)
2994 add_merge_hook(link_id, &merge_link_states);
2995 add_modification_hook(link_id, &match_link_modify);
2996 add_hook(&match_dereferences, DEREF_HOOK);
2997 add_hook(&match_pointer_as_array, OP_HOOK);
2998 select_return_implies_hook(DEREFERENCE, &set_param_dereferenced);
2999 add_hook(&match_function_call, FUNCTION_CALL_HOOK);
3000 add_hook(&match_assign, ASSIGNMENT_HOOK);
3001 add_hook(&match_assign, GLOBAL_ASSIGNMENT_HOOK);
3002 add_hook(&unop_expr, OP_HOOK);
3003 add_hook(&asm_expr, ASM_HOOK);
3005 add_caller_info_callback(my_id, caller_info_callback);
3006 add_split_return_callback(&returned_struct_members);
3008 // add_hook(&assume_indexes_are_valid, OP_HOOK);