smatch: remove the "loop could be replaced with if statement" messages
[smatch.git] / smatch_extra.c
blob87c75544e0df5e6701f6e7f5741c2c646493f5ed
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 #include <stdlib.h>
24 #include <errno.h>
25 #ifndef __USE_ISOC99
26 #define __USE_ISOC99
27 #endif
28 #include <limits.h>
29 #include "parse.h"
30 #include "smatch.h"
31 #include "smatch_slist.h"
32 #include "smatch_extra.h"
34 static int my_id;
35 static int link_id;
37 static void match_link_modify(struct sm_state *sm, struct expression *mod_expr);
39 struct string_list *__ignored_macros = NULL;
40 static int in_warn_on_macro(void)
42 struct statement *stmt;
43 char *tmp;
44 char *macro;
46 stmt = get_current_statement();
47 if (!stmt)
48 return 0;
49 macro = get_macro_name(stmt->pos);
50 if (!macro)
51 return 0;
53 FOR_EACH_PTR(__ignored_macros, tmp) {
54 if (!strcmp(tmp, macro))
55 return 1;
56 } END_FOR_EACH_PTR(tmp);
57 return 0;
60 typedef void (mod_hook)(const char *name, struct symbol *sym, struct smatch_state *state);
61 DECLARE_PTR_LIST(void_fn_list, mod_hook *);
62 static struct void_fn_list *extra_mod_hooks;
63 void add_extra_mod_hook(mod_hook *fn)
65 mod_hook **p = malloc(sizeof(mod_hook *));
66 *p = fn;
67 add_ptr_list(&extra_mod_hooks, p);
70 void call_extra_mod_hooks(const char *name, struct symbol *sym, struct smatch_state *state)
72 mod_hook **fn;
74 FOR_EACH_PTR(extra_mod_hooks, fn) {
75 (*fn)(name, sym, state);
76 } END_FOR_EACH_PTR(fn);
79 struct sm_state *set_extra_mod_helper(const char *name, struct symbol *sym, struct smatch_state *state)
81 remove_from_equiv(name, sym);
82 call_extra_mod_hooks(name, sym, state);
83 if (__in_fake_assign && estate_is_unknown(state) && !get_state(SMATCH_EXTRA, name, sym))
84 return NULL;
85 return set_state(SMATCH_EXTRA, name, sym, state);
88 static char *get_other_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym)
90 struct expression *assigned;
91 char *orig_name = NULL;
92 char buf[256];
93 char *ret = NULL;
94 int skip;
96 *new_sym = NULL;
98 if (!sym->ident)
99 return NULL;
101 skip = strlen(sym->ident->name);
102 if (name[skip] != '-' || name[skip + 1] != '>')
103 return NULL;
104 skip += 2;
106 assigned = get_assigned_expr_name_sym(sym->ident->name, sym);
107 if (!assigned)
108 return NULL;
109 if (assigned->type == EXPR_PREOP || assigned->op == '&') {
111 orig_name = expr_to_var_sym(assigned, new_sym);
112 if (!orig_name || !*new_sym)
113 goto free;
115 snprintf(buf, sizeof(buf), "%s.%s", orig_name + 1, name + skip);
116 ret = alloc_string(buf);
117 free_string(orig_name);
118 return ret;
121 if (assigned->type != EXPR_DEREF)
122 goto free;
124 orig_name = expr_to_var_sym(assigned, new_sym);
125 if (!orig_name || !*new_sym)
126 goto free;
128 snprintf(buf, sizeof(buf), "%s->%s", orig_name, name + skip);
129 ret = alloc_string(buf);
130 free_string(orig_name);
131 return ret;
133 free:
134 free_string(orig_name);
135 return NULL;
138 struct sm_state *set_extra_mod(const char *name, struct symbol *sym, struct smatch_state *state)
140 char *new_name;
141 struct symbol *new_sym;
142 struct sm_state *sm;
144 if (in_warn_on_macro())
145 return NULL;
146 sm = set_extra_mod_helper(name, sym, state);
147 new_name = get_other_name_sym(name, sym, &new_sym);
148 if (new_name && new_sym)
149 set_extra_mod_helper(new_name, new_sym, state);
150 free_string(new_name);
151 return sm;
154 struct sm_state *set_extra_expr_mod(struct expression *expr, struct smatch_state *state)
156 struct symbol *sym;
157 char *name;
158 struct sm_state *ret = NULL;
160 expr = strip_expr(expr);
161 name = expr_to_var_sym(expr, &sym);
162 if (!name || !sym)
163 goto free;
164 ret = set_extra_mod(name, sym, state);
165 free:
166 free_string(name);
167 return ret;
170 void set_extra_nomod(const char *name, struct symbol *sym, struct smatch_state *state)
172 char *new_name;
173 struct symbol *new_sym;
174 struct relation *rel;
175 struct smatch_state *orig_state;
177 orig_state = get_state(SMATCH_EXTRA, name, sym);
179 new_name = get_other_name_sym(name, sym, &new_sym);
180 if (new_name && new_sym)
181 set_state(SMATCH_EXTRA, new_name, new_sym, state);
182 free_string(new_name);
184 if (!estate_related(orig_state)) {
185 set_state(SMATCH_EXTRA, name, sym, state);
186 return;
189 set_related(state, estate_related(orig_state));
190 FOR_EACH_PTR(estate_related(orig_state), rel) {
191 if (option_debug_related)
192 sm_msg("%s updating related %s to %s", name, rel->name, state->name);
193 set_state(SMATCH_EXTRA, rel->name, rel->sym, state);
194 } END_FOR_EACH_PTR(rel);
198 * This is for return_implies_state() hooks which modify a SMATCH_EXTRA state
200 void set_extra_expr_nomod(struct expression *expr, struct smatch_state *state)
202 char *name;
203 struct symbol *sym;
205 name = expr_to_var_sym(expr, &sym);
206 if (!name || !sym)
207 goto free;
208 set_extra_nomod(name, sym, state);
209 free:
210 free_string(name);
214 static void set_extra_true_false(const char *name, struct symbol *sym,
215 struct smatch_state *true_state,
216 struct smatch_state *false_state)
218 char *new_name;
219 struct symbol *new_sym;
220 struct relation *rel;
221 struct smatch_state *orig_state;
223 if (in_warn_on_macro())
224 return;
226 new_name = get_other_name_sym(name, sym, &new_sym);
227 if (new_name && new_sym)
228 set_true_false_states(SMATCH_EXTRA, new_name, new_sym, true_state, false_state);
229 free_string(new_name);
231 orig_state = get_state(SMATCH_EXTRA, name, sym);
233 if (!estate_related(orig_state)) {
234 set_true_false_states(SMATCH_EXTRA, name, sym, true_state, false_state);
235 return;
238 if (true_state)
239 set_related(true_state, estate_related(orig_state));
240 if (false_state)
241 set_related(false_state, estate_related(orig_state));
243 FOR_EACH_PTR(estate_related(orig_state), rel) {
244 set_true_false_states(SMATCH_EXTRA, rel->name, rel->sym,
245 true_state, false_state);
246 } END_FOR_EACH_PTR(rel);
249 static void set_extra_chunk_true_false(struct expression *expr,
250 struct smatch_state *true_state,
251 struct smatch_state *false_state)
253 struct var_sym_list *vsl;
254 struct var_sym *vs;
255 struct symbol *type;
256 char *name;
257 struct symbol *sym;
259 if (in_warn_on_macro())
260 return;
262 type = get_type(expr);
263 if (!type)
264 return;
266 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
267 if (!name || !vsl)
268 goto free;
269 FOR_EACH_PTR(vsl, vs) {
270 store_link(link_id, vs->var, vs->sym, name, sym);
271 } END_FOR_EACH_PTR(vs);
273 set_true_false_states(SMATCH_EXTRA, name, sym,
274 clone_estate(true_state),
275 clone_estate(false_state));
276 free:
277 free_string(name);
280 static void set_extra_expr_true_false(struct expression *expr,
281 struct smatch_state *true_state,
282 struct smatch_state *false_state)
284 char *name;
285 struct symbol *sym;
286 sval_t sval;
288 if (get_value(expr, &sval))
289 return;
291 expr = strip_expr(expr);
292 name = expr_to_var_sym(expr, &sym);
293 if (!name || !sym) {
294 free_string(name);
295 set_extra_chunk_true_false(expr, true_state, false_state);
296 return;
298 set_extra_true_false(name, sym, true_state, false_state);
299 free_string(name);
302 static struct sm_state *handle_canonical_while_count_down(struct statement *loop)
304 struct expression *iter_var;
305 struct expression *condition;
306 struct sm_state *sm;
307 struct smatch_state *estate;
308 sval_t start;
310 condition = strip_expr(loop->iterator_pre_condition);
311 if (!condition)
312 return NULL;
313 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
314 return NULL;
315 if (condition->op != SPECIAL_DECREMENT)
316 return NULL;
318 iter_var = condition->unop;
319 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
320 if (!sm)
321 return NULL;
322 if (sval_cmp_val(estate_min(sm->state), 0) < 0)
323 return NULL;
324 start = estate_max(sm->state);
325 if (sval_cmp_val(start, 0) <= 0)
326 return NULL;
327 if (!sval_is_max(start))
328 start.value--;
330 if (condition->type == EXPR_PREOP) {
331 estate = alloc_estate_range(sval_type_val(start.type, 1), start);
332 if (estate_has_hard_max(sm->state))
333 estate_set_hard_max(estate);
334 estate_copy_fuzzy_max(estate, sm->state);
335 set_extra_expr_mod(iter_var, estate);
337 if (condition->type == EXPR_POSTOP) {
338 estate = alloc_estate_range(sval_type_val(start.type, 0), start);
339 if (estate_has_hard_max(sm->state))
340 estate_set_hard_max(estate);
341 estate_copy_fuzzy_max(estate, sm->state);
342 set_extra_expr_mod(iter_var, estate);
344 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
347 static struct sm_state *handle_canonical_for_inc(struct expression *iter_expr,
348 struct expression *condition)
350 struct expression *iter_var;
351 struct sm_state *sm;
352 struct smatch_state *estate;
353 sval_t start, end, max;
355 iter_var = iter_expr->unop;
356 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
357 if (!sm)
358 return NULL;
359 if (!estate_get_single_value(sm->state, &start))
360 return NULL;
361 if (get_implied_max(condition->right, &end))
362 end = sval_cast(get_type(iter_var), end);
363 else
364 end = sval_type_max(get_type(iter_var));
366 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
367 return NULL;
369 switch (condition->op) {
370 case SPECIAL_UNSIGNED_LT:
371 case SPECIAL_NOTEQUAL:
372 case '<':
373 if (!sval_is_min(end))
374 end.value--;
375 break;
376 case SPECIAL_UNSIGNED_LTE:
377 case SPECIAL_LTE:
378 break;
379 default:
380 return NULL;
382 if (sval_cmp(end, start) < 0)
383 return NULL;
384 estate = alloc_estate_range(start, end);
385 if (get_hard_max(condition->right, &max)) {
386 estate_set_hard_max(estate);
387 if (condition->op == '<' ||
388 condition->op == SPECIAL_UNSIGNED_LT ||
389 condition->op == SPECIAL_NOTEQUAL)
390 max.value--;
391 estate_set_fuzzy_max(estate, max);
393 set_extra_expr_mod(iter_var, estate);
394 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
397 static struct sm_state *handle_canonical_for_dec(struct expression *iter_expr,
398 struct expression *condition)
400 struct expression *iter_var;
401 struct sm_state *sm;
402 struct smatch_state *estate;
403 sval_t start, end;
405 iter_var = iter_expr->unop;
406 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
407 if (!sm)
408 return NULL;
409 if (!estate_get_single_value(sm->state, &start))
410 return NULL;
411 if (!get_implied_min(condition->right, &end))
412 end = sval_type_min(get_type(iter_var));
413 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
414 return NULL;
416 switch (condition->op) {
417 case SPECIAL_NOTEQUAL:
418 case '>':
419 if (!sval_is_min(end) && !sval_is_max(end))
420 end.value++;
421 break;
422 case SPECIAL_GTE:
423 break;
424 default:
425 return NULL;
427 if (sval_cmp(end, start) > 0)
428 return NULL;
429 estate = alloc_estate_range(end, start);
430 estate_set_hard_max(estate);
431 estate_set_fuzzy_max(estate, estate_get_fuzzy_max(estate));
432 set_extra_expr_mod(iter_var, estate);
433 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
436 static struct sm_state *handle_canonical_for_loops(struct statement *loop)
438 struct expression *iter_expr;
439 struct expression *condition;
441 if (!loop->iterator_post_statement)
442 return NULL;
443 if (loop->iterator_post_statement->type != STMT_EXPRESSION)
444 return NULL;
445 iter_expr = loop->iterator_post_statement->expression;
446 if (!loop->iterator_pre_condition)
447 return NULL;
448 if (loop->iterator_pre_condition->type != EXPR_COMPARE)
449 return NULL;
450 condition = loop->iterator_pre_condition;
452 if (iter_expr->op == SPECIAL_INCREMENT)
453 return handle_canonical_for_inc(iter_expr, condition);
454 if (iter_expr->op == SPECIAL_DECREMENT)
455 return handle_canonical_for_dec(iter_expr, condition);
456 return NULL;
459 struct sm_state *__extra_handle_canonical_loops(struct statement *loop, struct stree **stree)
461 struct sm_state *ret;
463 __push_fake_cur_stree();
464 if (!loop->iterator_post_statement)
465 ret = handle_canonical_while_count_down(loop);
466 else
467 ret = handle_canonical_for_loops(loop);
468 *stree = __pop_fake_cur_stree();
469 return ret;
472 int __iterator_unchanged(struct sm_state *sm)
474 if (!sm)
475 return 0;
476 if (get_sm_state(my_id, sm->name, sm->sym) == sm)
477 return 1;
478 return 0;
481 static void while_count_down_after(struct sm_state *sm, struct expression *condition)
483 sval_t after_value;
485 /* paranoid checking. prolly not needed */
486 condition = strip_expr(condition);
487 if (!condition)
488 return;
489 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
490 return;
491 if (condition->op != SPECIAL_DECREMENT)
492 return;
493 after_value = estate_min(sm->state);
494 after_value.value--;
495 set_extra_mod(sm->name, sm->sym, alloc_estate_sval(after_value));
498 void __extra_pre_loop_hook_after(struct sm_state *sm,
499 struct statement *iterator,
500 struct expression *condition)
502 struct expression *iter_expr;
503 sval_t limit;
504 struct smatch_state *state;
506 if (!iterator) {
507 while_count_down_after(sm, condition);
508 return;
511 iter_expr = iterator->expression;
513 if (condition->type != EXPR_COMPARE)
514 return;
515 if (iter_expr->op == SPECIAL_INCREMENT) {
516 limit = sval_binop(estate_max(sm->state), '+',
517 sval_type_val(estate_type(sm->state), 1));
518 } else {
519 limit = sval_binop(estate_min(sm->state), '-',
520 sval_type_val(estate_type(sm->state), 1));
522 if (!estate_has_hard_max(sm->state) && !__has_breaks()) {
523 if (iter_expr->op == SPECIAL_INCREMENT)
524 state = alloc_estate_range(estate_min(sm->state), limit);
525 else
526 state = alloc_estate_range(limit, estate_max(sm->state));
527 } else {
528 state = alloc_estate_sval(limit);
530 if (!estate_has_hard_max(sm->state)) {
531 estate_clear_hard_max(state);
533 if (estate_has_fuzzy_max(sm->state)) {
534 sval_t hmax = estate_get_fuzzy_max(sm->state);
535 sval_t max = estate_max(sm->state);
537 if (sval_cmp(hmax, max) != 0)
538 estate_clear_fuzzy_max(state);
539 } else if (!estate_has_fuzzy_max(sm->state)) {
540 estate_clear_fuzzy_max(state);
543 set_extra_mod(sm->name, sm->sym, state);
546 static struct stree *unmatched_stree;
547 static struct smatch_state *unmatched_state(struct sm_state *sm)
549 struct smatch_state *state;
551 if (unmatched_stree) {
552 state = get_state_stree(unmatched_stree, SMATCH_EXTRA, sm->name, sm->sym);
553 if (state)
554 return state;
556 if (parent_is_gone_var_sym(sm->name, sm->sym))
557 return alloc_estate_empty();
558 return alloc_estate_whole(estate_type(sm->state));
561 static void clear_the_pointed_at(struct expression *expr)
563 struct stree *stree;
564 char *name;
565 struct symbol *sym;
566 struct sm_state *tmp;
568 name = expr_to_var_sym(expr, &sym);
569 if (!name || !sym)
570 goto free;
572 stree = __get_cur_stree();
573 FOR_EACH_MY_SM(SMATCH_EXTRA, stree, tmp) {
574 if (tmp->name[0] != '*')
575 continue;
576 if (tmp->sym != sym)
577 continue;
578 if (strcmp(tmp->name + 1, name) != 0)
579 continue;
580 set_extra_mod(tmp->name, tmp->sym, alloc_estate_whole(estate_type(tmp->state)));
581 } END_FOR_EACH_SM(tmp);
583 free:
584 free_string(name);
587 static void match_function_call(struct expression *expr)
589 struct expression *arg;
590 struct expression *tmp;
592 /* if we have the db this is handled in smatch_function_hooks.c */
593 if (!option_no_db)
594 return;
595 if (inlinable(expr->fn))
596 return;
598 FOR_EACH_PTR(expr->args, arg) {
599 tmp = strip_expr(arg);
600 if (tmp->type == EXPR_PREOP && tmp->op == '&')
601 set_extra_expr_mod(tmp->unop, alloc_estate_whole(get_type(tmp->unop)));
602 else
603 clear_the_pointed_at(tmp);
604 } END_FOR_EACH_PTR(arg);
607 static int types_equiv_or_pointer(struct symbol *one, struct symbol *two)
609 if (!one || !two)
610 return 0;
611 if (one->type == SYM_PTR && two->type == SYM_PTR)
612 return 1;
613 return types_equiv(one, two);
616 static void save_chunk_info(struct expression *left, struct expression *right)
618 struct var_sym_list *vsl;
619 struct var_sym *vs;
620 struct expression *add_expr;
621 struct symbol *type;
622 sval_t sval;
623 char *name;
624 struct symbol *sym;
626 if (right->type != EXPR_BINOP || right->op != '-')
627 return;
628 if (!get_value(right->left, &sval))
629 return;
630 if (!expr_to_sym(right->right))
631 return;
633 add_expr = binop_expression(left, '+', right->right);
634 type = get_type(add_expr);
635 if (!type)
636 return;
637 name = expr_to_chunk_sym_vsl(add_expr, &sym, &vsl);
638 if (!name || !vsl)
639 goto free;
640 FOR_EACH_PTR(vsl, vs) {
641 store_link(link_id, vs->var, vs->sym, name, sym);
642 } END_FOR_EACH_PTR(vs);
644 set_state(SMATCH_EXTRA, name, sym, alloc_estate_sval(sval_cast(type, sval)));
645 free:
646 free_string(name);
649 static void do_array_assign(struct expression *left, int op, struct expression *right)
651 struct expression *array;
652 struct expression *offset;
653 struct var_sym_list *vsl;
654 struct var_sym *vs;
655 char *name;
656 struct symbol *sym;
657 struct range_list *rl;
658 sval_t sval;
660 array = get_array_base(left);
661 offset = get_array_offset(left);
662 get_absolute_rl(right, &rl);
663 rl = cast_rl(get_type(left), rl);
665 name = expr_to_chunk_sym_vsl(left, &sym, &vsl);
667 if (!name || !vsl || op != '=' || !get_value(offset, &sval) || is_whole_rl(rl)) {
668 struct sm_state *sm;
670 sm = get_sm_state_expr(link_id, array);
671 if (sm)
672 match_link_modify(sm, NULL);
673 goto free;
676 FOR_EACH_PTR(vsl, vs) {
677 store_link(link_id, vs->var, vs->sym, name, sym);
678 } END_FOR_EACH_PTR(vs);
680 set_state(SMATCH_EXTRA, name, sym, alloc_estate_rl(rl));
681 free:
682 free_string(name);
685 static void match_vanilla_assign(struct expression *left, struct expression *right)
687 struct range_list *rl = NULL;
688 struct symbol *right_sym;
689 struct symbol *left_type;
690 struct symbol *right_type;
691 char *right_name = NULL;
692 struct symbol *sym;
693 char *name;
694 sval_t max;
695 struct smatch_state *state;
697 if (is_struct(left))
698 return;
700 save_chunk_info(left, right);
702 name = expr_to_var_sym(left, &sym);
703 if (!name) {
704 if (is_array(left))
705 do_array_assign(left, '=', right);
706 return;
709 left_type = get_type(left);
710 right_type = get_type(right);
712 right_name = expr_to_var_sym(right, &right_sym);
714 if (!__in_fake_assign &&
715 !(right->type == EXPR_PREOP && right->op == '&') &&
716 right_name && right_sym &&
717 types_equiv_or_pointer(left_type, right_type) &&
718 !has_symbol(right, sym)) {
719 set_equiv(left, right);
720 goto free;
723 if (is_pointer(right) && get_address_rl(right, &rl)) {
724 state = alloc_estate_rl(rl);
725 } else if (get_implied_rl(right, &rl)) {
726 rl = cast_rl(left_type, rl);
727 state = alloc_estate_rl(rl);
728 if (get_hard_max(right, &max)) {
729 estate_set_hard_max(state);
730 estate_set_fuzzy_max(state, max);
732 } else {
733 rl = alloc_whole_rl(right_type);
734 rl = cast_rl(left_type, rl);
735 state = alloc_estate_rl(rl);
737 set_extra_mod(name, sym, state);
738 free:
739 free_string(right_name);
742 static int op_remove_assign(int op)
744 switch (op) {
745 case SPECIAL_ADD_ASSIGN:
746 return '+';
747 case SPECIAL_SUB_ASSIGN:
748 return '-';
749 case SPECIAL_MUL_ASSIGN:
750 return '*';
751 case SPECIAL_DIV_ASSIGN:
752 return '/';
753 case SPECIAL_MOD_ASSIGN:
754 return '%';
755 case SPECIAL_AND_ASSIGN:
756 return '&';
757 case SPECIAL_OR_ASSIGN:
758 return '|';
759 case SPECIAL_XOR_ASSIGN:
760 return '^';
761 case SPECIAL_SHL_ASSIGN:
762 return SPECIAL_LEFTSHIFT;
763 case SPECIAL_SHR_ASSIGN:
764 return SPECIAL_RIGHTSHIFT;
765 default:
766 return op;
770 static void match_assign(struct expression *expr)
772 struct range_list *rl = NULL;
773 struct expression *left;
774 struct expression *right;
775 struct expression *binop_expr;
776 struct symbol *left_type;
777 struct symbol *right_type;
778 struct symbol *sym;
779 char *name;
780 sval_t left_min, left_max;
781 sval_t right_min, right_max;
782 sval_t res_min, res_max;
784 left = strip_expr(expr->left);
786 right = strip_parens(expr->right);
787 while (right->type == EXPR_ASSIGNMENT && right->op == '=')
788 right = strip_parens(right->left);
790 if (expr->op == '=' && is_condition(expr->right))
791 return; /* handled in smatch_condition.c */
792 if (expr->op == '=' && right->type == EXPR_CALL)
793 return; /* handled in smatch_function_hooks.c */
794 if (expr->op == '=') {
795 match_vanilla_assign(left, right);
796 return;
799 name = expr_to_var_sym(left, &sym);
800 if (!name)
801 return;
803 left_type = get_type(left);
804 right_type = get_type(right);
806 res_min = sval_type_min(left_type);
807 res_max = sval_type_max(left_type);
809 switch (expr->op) {
810 case SPECIAL_ADD_ASSIGN:
811 get_absolute_max(left, &left_max);
812 get_absolute_max(right, &right_max);
813 if (sval_binop_overflows(left_max, '+', right_max))
814 break;
815 if (get_implied_min(left, &left_min) &&
816 !sval_is_negative_min(left_min) &&
817 get_implied_min(right, &right_min) &&
818 !sval_is_negative_min(right_min)) {
819 res_min = sval_binop(left_min, '+', right_min);
820 res_min = sval_cast(right_type, res_min);
822 if (inside_loop()) /* we are assuming loops don't lead to wrapping */
823 break;
824 res_max = sval_binop(left_max, '+', right_max);
825 res_max = sval_cast(right_type, res_max);
826 break;
827 case SPECIAL_SUB_ASSIGN:
828 if (get_implied_max(left, &left_max) &&
829 !sval_is_max(left_max) &&
830 get_implied_min(right, &right_min) &&
831 !sval_is_min(right_min)) {
832 res_max = sval_binop(left_max, '-', right_min);
833 res_max = sval_cast(right_type, res_max);
835 if (inside_loop())
836 break;
837 if (get_implied_min(left, &left_min) &&
838 !sval_is_min(left_min) &&
839 get_implied_max(right, &right_max) &&
840 !sval_is_max(right_max)) {
841 res_min = sval_binop(left_min, '-', right_max);
842 res_min = sval_cast(right_type, res_min);
844 break;
845 case SPECIAL_AND_ASSIGN:
846 case SPECIAL_MOD_ASSIGN:
847 case SPECIAL_SHL_ASSIGN:
848 case SPECIAL_SHR_ASSIGN:
849 case SPECIAL_OR_ASSIGN:
850 case SPECIAL_XOR_ASSIGN:
851 case SPECIAL_MUL_ASSIGN:
852 case SPECIAL_DIV_ASSIGN:
853 binop_expr = binop_expression(expr->left,
854 op_remove_assign(expr->op),
855 expr->right);
856 if (get_absolute_rl(binop_expr, &rl)) {
857 rl = cast_rl(left_type, rl);
858 set_extra_mod(name, sym, alloc_estate_rl(rl));
859 goto free;
861 break;
863 rl = cast_rl(left_type, alloc_rl(res_min, res_max));
864 set_extra_mod(name, sym, alloc_estate_rl(rl));
865 free:
866 free_string(name);
869 static struct smatch_state *increment_state(struct smatch_state *state)
871 sval_t min = estate_min(state);
872 sval_t max = estate_max(state);
874 if (!estate_rl(state))
875 return NULL;
877 if (inside_loop())
878 max = sval_type_max(max.type);
880 if (!sval_is_min(min) && !sval_is_max(min))
881 min.value++;
882 if (!sval_is_min(max) && !sval_is_max(max))
883 max.value++;
884 return alloc_estate_range(min, max);
887 static struct smatch_state *decrement_state(struct smatch_state *state)
889 sval_t min = estate_min(state);
890 sval_t max = estate_max(state);
892 if (!estate_rl(state))
893 return NULL;
895 if (inside_loop())
896 min = sval_type_min(min.type);
898 if (!sval_is_min(min) && !sval_is_max(min))
899 min.value--;
900 if (!sval_is_min(max) && !sval_is_max(max))
901 max.value--;
902 return alloc_estate_range(min, max);
905 static void unop_expr(struct expression *expr)
907 struct smatch_state *state;
909 if (expr->smatch_flags & Handled)
910 return;
912 switch (expr->op) {
913 case SPECIAL_INCREMENT:
914 state = get_state_expr(SMATCH_EXTRA, expr->unop);
915 state = increment_state(state);
916 if (!state)
917 state = alloc_estate_whole(get_type(expr));
918 set_extra_expr_mod(expr->unop, state);
919 break;
920 case SPECIAL_DECREMENT:
921 state = get_state_expr(SMATCH_EXTRA, expr->unop);
922 state = decrement_state(state);
923 if (!state)
924 state = alloc_estate_whole(get_type(expr));
925 set_extra_expr_mod(expr->unop, state);
926 break;
927 default:
928 return;
932 static void asm_expr(struct statement *stmt)
935 struct expression *expr;
936 struct symbol *type;
937 int state = 0;
939 FOR_EACH_PTR(stmt->asm_outputs, expr) {
940 switch (state) {
941 case 0: /* identifier */
942 case 1: /* constraint */
943 state++;
944 continue;
945 case 2: /* expression */
946 state = 0;
947 type = get_type(strip_expr(expr));
948 set_extra_expr_mod(expr, alloc_estate_whole(type));
949 continue;
951 } END_FOR_EACH_PTR(expr);
954 static void check_dereference(struct expression *expr)
956 if (outside_of_function())
957 return;
958 set_extra_expr_nomod(expr, alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval));
961 static void match_dereferences(struct expression *expr)
963 if (expr->type != EXPR_PREOP)
964 return;
965 check_dereference(expr->unop);
968 static void match_pointer_as_array(struct expression *expr)
970 if (!is_array(expr))
971 return;
972 check_dereference(get_array_base(expr));
975 static void set_param_dereferenced(struct expression *arg, char *key, char *unused)
977 struct symbol *sym;
978 char *name;
980 name = get_variable_from_key(arg, key, &sym);
981 if (!name || !sym)
982 goto free;
984 set_extra_nomod(name, sym, alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval));
986 free:
987 free_string(name);
990 static sval_t add_one(sval_t sval)
992 sval.value++;
993 return sval;
996 static sval_t sub_one(sval_t sval)
998 sval.value--;
999 return sval;
1002 static void handle_comparison(struct symbol *type, struct expression *left, int op, struct expression *right)
1004 struct range_list *left_orig;
1005 struct range_list *left_true;
1006 struct range_list *left_false;
1007 struct range_list *right_orig;
1008 struct range_list *right_true;
1009 struct range_list *right_false;
1010 struct smatch_state *left_true_state;
1011 struct smatch_state *left_false_state;
1012 struct smatch_state *right_true_state;
1013 struct smatch_state *right_false_state;
1014 sval_t min, max, dummy, hard_max;
1015 int left_postop = 0;
1016 int right_postop = 0;
1018 if (left->op == SPECIAL_INCREMENT || left->op == SPECIAL_DECREMENT) {
1019 if (left->type == EXPR_POSTOP) {
1020 left->smatch_flags |= Handled;
1021 left_postop = left->op;
1023 left = strip_expr(left->unop);
1025 while (left->type == EXPR_ASSIGNMENT)
1026 left = strip_expr(left->left);
1028 if (right->op == SPECIAL_INCREMENT || right->op == SPECIAL_DECREMENT) {
1029 if (right->type == EXPR_POSTOP) {
1030 right->smatch_flags |= Handled;
1031 right_postop = right->op;
1033 right = strip_expr(right->unop);
1036 if (get_implied_rl(left, &left_orig)) {
1037 left_orig = cast_rl(type, left_orig);
1038 } else {
1039 min = sval_type_min(get_type(left));
1040 max = sval_type_max(get_type(left));
1041 left_orig = cast_rl(type, alloc_rl(min, max));
1044 if (get_implied_rl(right, &right_orig)) {
1045 right_orig = cast_rl(type, right_orig);
1046 } else {
1047 min = sval_type_min(get_type(right));
1048 max = sval_type_max(get_type(right));
1049 right_orig = cast_rl(type, alloc_rl(min, max));
1051 min = sval_type_min(type);
1052 max = sval_type_max(type);
1054 left_true = clone_rl(left_orig);
1055 left_false = clone_rl(left_orig);
1056 right_true = clone_rl(right_orig);
1057 right_false = clone_rl(right_orig);
1059 switch (op) {
1060 case '<':
1061 case SPECIAL_UNSIGNED_LT:
1062 left_true = remove_range(left_orig, rl_max(right_orig), max);
1063 if (!sval_is_min(rl_min(right_orig))) {
1064 left_false = remove_range(left_orig, min, sub_one(rl_min(right_orig)));
1067 right_true = remove_range(right_orig, min, rl_min(left_orig));
1068 if (!sval_is_max(rl_max(left_orig)))
1069 right_false = remove_range(right_orig, add_one(rl_max(left_orig)), max);
1070 break;
1071 case SPECIAL_UNSIGNED_LTE:
1072 case SPECIAL_LTE:
1073 if (!sval_is_max(rl_max(right_orig)))
1074 left_true = remove_range(left_orig, add_one(rl_max(right_orig)), max);
1075 left_false = remove_range(left_orig, min, rl_min(right_orig));
1077 if (!sval_is_min(rl_min(left_orig)))
1078 right_true = remove_range(right_orig, min, sub_one(rl_min(left_orig)));
1079 right_false = remove_range(right_orig, rl_max(left_orig), max);
1081 if (sval_cmp(rl_min(left_orig), rl_min(right_orig)) == 0)
1082 left_false = remove_range(left_false, rl_min(left_orig), rl_min(left_orig));
1083 if (sval_cmp(rl_max(left_orig), rl_max(right_orig)) == 0)
1084 right_false = remove_range(right_false, rl_max(left_orig), rl_max(left_orig));
1085 break;
1086 case SPECIAL_EQUAL:
1087 if (!sval_is_max(rl_max(right_orig))) {
1088 left_true = remove_range(left_true, add_one(rl_max(right_orig)), max);
1090 if (!sval_is_min(rl_min(right_orig))) {
1091 left_true = remove_range(left_true, min, sub_one(rl_min(right_orig)));
1093 if (sval_cmp(rl_min(right_orig), rl_max(right_orig)) == 0)
1094 left_false = remove_range(left_orig, rl_min(right_orig), rl_min(right_orig));
1096 if (!sval_is_max(rl_max(left_orig)))
1097 right_true = remove_range(right_true, add_one(rl_max(left_orig)), max);
1098 if (!sval_is_min(rl_min(left_orig)))
1099 right_true = remove_range(right_true, min, sub_one(rl_min(left_orig)));
1100 if (sval_cmp(rl_min(left_orig), rl_max(left_orig)) == 0)
1101 right_false = remove_range(right_orig, rl_min(left_orig), rl_min(left_orig));
1102 break;
1103 case SPECIAL_UNSIGNED_GTE:
1104 case SPECIAL_GTE:
1105 if (!sval_is_min(rl_min(right_orig)))
1106 left_true = remove_range(left_orig, min, sub_one(rl_min(right_orig)));
1107 left_false = remove_range(left_orig, rl_max(right_orig), max);
1109 if (!sval_is_max(rl_max(left_orig)))
1110 right_true = remove_range(right_orig, add_one(rl_max(left_orig)), max);
1111 right_false = remove_range(right_orig, min, rl_min(left_orig));
1113 if (sval_cmp(rl_min(left_orig), rl_min(right_orig)) == 0)
1114 right_false = remove_range(right_false, rl_min(left_orig), rl_min(left_orig));
1115 if (sval_cmp(rl_max(left_orig), rl_max(right_orig)) == 0)
1116 left_false = remove_range(left_false, rl_max(left_orig), rl_max(left_orig));
1117 break;
1118 case '>':
1119 case SPECIAL_UNSIGNED_GT:
1120 left_true = remove_range(left_orig, min, rl_min(right_orig));
1121 if (!sval_is_max(rl_max(right_orig)))
1122 left_false = remove_range(left_orig, add_one(rl_max(right_orig)), max);
1124 right_true = remove_range(right_orig, rl_max(left_orig), max);
1125 if (!sval_is_min(rl_min(left_orig)))
1126 right_false = remove_range(right_orig, min, sub_one(rl_min(left_orig)));
1127 break;
1128 case SPECIAL_NOTEQUAL:
1129 if (!sval_is_max(rl_max(right_orig)))
1130 left_false = remove_range(left_false, add_one(rl_max(right_orig)), max);
1131 if (!sval_is_min(rl_min(right_orig)))
1132 left_false = remove_range(left_false, min, sub_one(rl_min(right_orig)));
1133 if (sval_cmp(rl_min(right_orig), rl_max(right_orig)) == 0)
1134 left_true = remove_range(left_orig, rl_min(right_orig), rl_min(right_orig));
1136 if (!sval_is_max(rl_max(left_orig)))
1137 right_false = remove_range(right_false, add_one(rl_max(left_orig)), max);
1138 if (!sval_is_min(rl_min(left_orig)))
1139 right_false = remove_range(right_false, min, sub_one(rl_min(left_orig)));
1140 if (sval_cmp(rl_min(left_orig), rl_max(left_orig)) == 0)
1141 right_true = remove_range(right_orig, rl_min(left_orig), rl_min(left_orig));
1142 break;
1143 default:
1144 return;
1147 left_true = rl_truncate_cast(get_type(left), left_true);
1148 left_false = rl_truncate_cast(get_type(left), left_false);
1149 right_true = rl_truncate_cast(get_type(right), right_true);
1150 right_false = rl_truncate_cast(get_type(right), right_false);
1152 left_true_state = alloc_estate_rl(left_true);
1153 left_false_state = alloc_estate_rl(left_false);
1154 right_true_state = alloc_estate_rl(right_true);
1155 right_false_state = alloc_estate_rl(right_false);
1157 switch (op) {
1158 case '<':
1159 case SPECIAL_UNSIGNED_LT:
1160 case SPECIAL_UNSIGNED_LTE:
1161 case SPECIAL_LTE:
1162 if (get_hard_max(right, &dummy))
1163 estate_set_hard_max(left_true_state);
1164 if (get_hard_max(left, &dummy))
1165 estate_set_hard_max(right_false_state);
1166 break;
1167 case '>':
1168 case SPECIAL_UNSIGNED_GT:
1169 case SPECIAL_UNSIGNED_GTE:
1170 case SPECIAL_GTE:
1171 if (get_hard_max(left, &dummy))
1172 estate_set_hard_max(right_true_state);
1173 if (get_hard_max(right, &dummy))
1174 estate_set_hard_max(left_false_state);
1175 break;
1178 switch (op) {
1179 case '<':
1180 case SPECIAL_UNSIGNED_LT:
1181 case SPECIAL_UNSIGNED_LTE:
1182 case SPECIAL_LTE:
1183 if (get_hard_max(right, &hard_max)) {
1184 if (op == '<' || op == SPECIAL_UNSIGNED_LT)
1185 hard_max.value--;
1186 estate_set_fuzzy_max(left_true_state, hard_max);
1188 if (get_implied_value(right, &hard_max)) {
1189 if (op == SPECIAL_UNSIGNED_LTE ||
1190 op == SPECIAL_LTE)
1191 hard_max.value++;
1192 estate_set_fuzzy_max(left_false_state, hard_max);
1194 if (get_hard_max(left, &hard_max)) {
1195 if (op == SPECIAL_UNSIGNED_LTE ||
1196 op == SPECIAL_LTE)
1197 hard_max.value--;
1198 estate_set_fuzzy_max(right_false_state, hard_max);
1200 if (get_implied_value(left, &hard_max)) {
1201 if (op == '<' || op == SPECIAL_UNSIGNED_LT)
1202 hard_max.value++;
1203 estate_set_fuzzy_max(right_true_state, hard_max);
1205 break;
1206 case '>':
1207 case SPECIAL_UNSIGNED_GT:
1208 case SPECIAL_UNSIGNED_GTE:
1209 case SPECIAL_GTE:
1210 if (get_hard_max(left, &hard_max)) {
1211 if (op == '>' || op == SPECIAL_UNSIGNED_GT)
1212 hard_max.value--;
1213 estate_set_fuzzy_max(right_true_state, hard_max);
1215 if (get_implied_value(left, &hard_max)) {
1216 if (op == SPECIAL_UNSIGNED_GTE ||
1217 op == SPECIAL_GTE)
1218 hard_max.value++;
1219 estate_set_fuzzy_max(right_false_state, hard_max);
1221 if (get_hard_max(right, &hard_max)) {
1222 if (op == SPECIAL_UNSIGNED_LTE ||
1223 op == SPECIAL_LTE)
1224 hard_max.value--;
1225 estate_set_fuzzy_max(left_false_state, hard_max);
1227 if (get_implied_value(right, &hard_max)) {
1228 if (op == '>' ||
1229 op == SPECIAL_UNSIGNED_GT)
1230 hard_max.value++;
1231 estate_set_fuzzy_max(left_true_state, hard_max);
1233 break;
1234 case SPECIAL_EQUAL:
1235 if (get_hard_max(left, &hard_max))
1236 estate_set_fuzzy_max(right_true_state, hard_max);
1237 if (get_hard_max(right, &hard_max))
1238 estate_set_fuzzy_max(left_true_state, hard_max);
1239 break;
1242 if (get_hard_max(left, &hard_max)) {
1243 estate_set_hard_max(left_true_state);
1244 estate_set_hard_max(left_false_state);
1246 if (get_hard_max(right, &hard_max)) {
1247 estate_set_hard_max(right_true_state);
1248 estate_set_hard_max(right_false_state);
1251 if (left_postop == SPECIAL_INCREMENT) {
1252 left_true_state = increment_state(left_true_state);
1253 left_false_state = increment_state(left_false_state);
1255 if (left_postop == SPECIAL_DECREMENT) {
1256 left_true_state = decrement_state(left_true_state);
1257 left_false_state = decrement_state(left_false_state);
1259 if (right_postop == SPECIAL_INCREMENT) {
1260 right_true_state = increment_state(right_true_state);
1261 right_false_state = increment_state(right_false_state);
1263 if (right_postop == SPECIAL_DECREMENT) {
1264 right_true_state = decrement_state(right_true_state);
1265 right_false_state = decrement_state(right_false_state);
1268 if (estate_rl(left_true_state) && estates_equiv(left_true_state, left_false_state)) {
1269 left_true_state = NULL;
1270 left_false_state = NULL;
1273 if (estate_rl(right_true_state) && estates_equiv(right_true_state, right_false_state)) {
1274 right_true_state = NULL;
1275 right_false_state = NULL;
1278 set_extra_expr_true_false(left, left_true_state, left_false_state);
1279 set_extra_expr_true_false(right, right_true_state, right_false_state);
1282 static int is_simple_math(struct expression *expr)
1284 if (!expr)
1285 return 0;
1286 if (expr->type != EXPR_BINOP)
1287 return 0;
1288 switch (expr->op) {
1289 case '+':
1290 case '-':
1291 case '*':
1292 return 1;
1294 return 0;
1297 static void move_known_values(struct expression **left_p, struct expression **right_p)
1299 struct expression *left = *left_p;
1300 struct expression *right = *right_p;
1301 sval_t sval;
1303 if (get_implied_value(left, &sval)) {
1304 if (!is_simple_math(right))
1305 return;
1306 if (right->op == '*') {
1307 sval_t divisor;
1309 if (!get_value(right->right, &divisor))
1310 return;
1311 if (divisor.value == 0 && sval.value % divisor.value)
1312 return;
1313 *left_p = binop_expression(left, invert_op(right->op), right->right);
1314 *right_p = right->left;
1315 return;
1317 if (right->op == '+' && get_value(right->left, &sval)) {
1318 *left_p = binop_expression(left, invert_op(right->op), right->left);
1319 *right_p = right->right;
1320 return;
1322 if (get_value(right->right, &sval)) {
1323 *left_p = binop_expression(left, invert_op(right->op), right->right);
1324 *right_p = right->left;
1325 return;
1327 return;
1329 if (get_implied_value(right, &sval)) {
1330 if (!is_simple_math(left))
1331 return;
1332 if (left->op == '*') {
1333 sval_t divisor;
1335 if (!get_value(left->right, &divisor))
1336 return;
1337 if (divisor.value == 0 && sval.value % divisor.value)
1338 return;
1339 *right_p = binop_expression(right, invert_op(left->op), left->right);
1340 *left_p = left->left;
1341 return;
1343 if (left->op == '+' && get_value(left->left, &sval)) {
1344 *right_p = binop_expression(right, invert_op(left->op), left->left);
1345 *left_p = left->right;
1346 return;
1349 if (get_value(left->right, &sval)) {
1350 *right_p = binop_expression(right, invert_op(left->op), left->right);
1351 *left_p = left->left;
1352 return;
1354 return;
1358 static int match_func_comparison(struct expression *expr)
1360 struct expression *left = strip_expr(expr->left);
1361 struct expression *right = strip_expr(expr->right);
1362 sval_t sval;
1365 * fixme: think about this harder. We should always be trying to limit
1366 * the non-call side as well. If we can't determine the limitter does
1367 * that mean we aren't querying the database and are missing important
1368 * information?
1371 if (left->type == EXPR_CALL) {
1372 if (get_implied_value(left, &sval)) {
1373 handle_comparison(get_type(expr), left, expr->op, right);
1374 return 1;
1376 function_comparison(left, expr->op, right);
1377 return 1;
1380 if (right->type == EXPR_CALL) {
1381 if (get_implied_value(right, &sval)) {
1382 handle_comparison(get_type(expr), left, expr->op, right);
1383 return 1;
1385 function_comparison(left, expr->op, right);
1386 return 1;
1389 return 0;
1392 static void match_comparison(struct expression *expr)
1394 struct expression *left_orig = strip_expr(expr->left);
1395 struct expression *right_orig = strip_expr(expr->right);
1396 struct expression *left, *right;
1397 struct expression *prev;
1398 struct symbol *type;
1400 if (match_func_comparison(expr))
1401 return;
1403 type = get_type(expr);
1404 if (!type)
1405 type = &llong_ctype;
1407 left = left_orig;
1408 right = right_orig;
1409 move_known_values(&left, &right);
1410 handle_comparison(type, left, expr->op, right);
1412 prev = get_assigned_expr(left_orig);
1413 if (is_simple_math(prev) && has_variable(prev, left_orig) == 0) {
1414 left = prev;
1415 right = right_orig;
1416 move_known_values(&left, &right);
1417 handle_comparison(type, left, expr->op, right);
1420 prev = get_assigned_expr(right_orig);
1421 if (is_simple_math(prev) && has_variable(prev, right_orig) == 0) {
1422 left = left_orig;
1423 right = prev;
1424 move_known_values(&left, &right);
1425 handle_comparison(type, left, expr->op, right);
1429 static void handle_AND_condition(struct expression *expr)
1431 struct range_list *rl = NULL;
1432 sval_t known;
1434 if (get_implied_value(expr->left, &known) && known.value > 0) {
1435 known.value--;
1436 get_absolute_rl(expr->right, &rl);
1437 rl = remove_range(rl, sval_type_val(known.type, 0), known);
1438 set_extra_expr_true_false(expr->right, alloc_estate_rl(rl), NULL);
1439 return;
1442 if (get_implied_value(expr->right, &known) && known.value > 0) {
1443 known.value--;
1444 get_absolute_rl(expr->left, &rl);
1445 rl = remove_range(rl, sval_type_val(known.type, 0), known);
1446 set_extra_expr_true_false(expr->left, alloc_estate_rl(rl), NULL);
1447 return;
1451 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
1452 void __extra_match_condition(struct expression *expr)
1454 struct smatch_state *pre_state;
1455 struct smatch_state *true_state;
1456 struct smatch_state *false_state;
1458 expr = strip_expr(expr);
1459 switch (expr->type) {
1460 case EXPR_CALL:
1461 function_comparison(expr, SPECIAL_NOTEQUAL, zero_expr());
1462 return;
1463 case EXPR_PREOP:
1464 case EXPR_SYMBOL:
1465 case EXPR_DEREF: {
1466 sval_t zero;
1468 zero = sval_blank(expr);
1469 zero.value = 0;
1471 pre_state = get_extra_state(expr);
1472 true_state = estate_filter_sval(pre_state, zero);
1473 if (possibly_true(expr, SPECIAL_EQUAL, zero_expr()))
1474 false_state = alloc_estate_sval(zero);
1475 else
1476 false_state = alloc_estate_empty();
1477 set_extra_expr_true_false(expr, true_state, false_state);
1478 return;
1480 case EXPR_COMPARE:
1481 match_comparison(expr);
1482 return;
1483 case EXPR_ASSIGNMENT:
1484 __extra_match_condition(expr->left);
1485 return;
1486 case EXPR_BINOP:
1487 if (expr->op == '&')
1488 handle_AND_condition(expr);
1489 return;
1493 static void assume_indexes_are_valid(struct expression *expr)
1495 struct expression *array_expr;
1496 int array_size;
1497 struct expression *offset;
1498 struct symbol *offset_type;
1499 struct range_list *rl_before;
1500 struct range_list *rl_after;
1501 struct range_list *filter = NULL;
1502 sval_t size;
1504 expr = strip_expr(expr);
1505 if (!is_array(expr))
1506 return;
1508 offset = get_array_offset(expr);
1509 offset_type = get_type(offset);
1510 if (offset_type && type_signed(offset_type)) {
1511 filter = alloc_rl(sval_type_min(offset_type),
1512 sval_type_val(offset_type, -1));
1515 array_expr = get_array_base(expr);
1516 array_size = get_real_array_size(array_expr);
1517 if (array_size > 1) {
1518 size = sval_type_val(offset_type, array_size);
1519 add_range(&filter, size, sval_type_max(offset_type));
1522 if (!filter)
1523 return;
1524 get_absolute_rl(offset, &rl_before);
1525 rl_after = rl_filter(rl_before, filter);
1526 if (rl_equiv(rl_before, rl_after))
1527 return;
1528 set_extra_expr_nomod(offset, alloc_estate_rl(rl_after));
1531 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
1532 int implied_not_equal(struct expression *expr, long long val)
1534 return !possibly_false(expr, SPECIAL_NOTEQUAL, value_expr(val));
1537 int implied_not_equal_name_sym(char *name, struct symbol *sym, long long val)
1539 struct smatch_state *estate;
1541 estate = get_state(SMATCH_EXTRA, name, sym);
1542 if (!estate)
1543 return 0;
1544 if (!rl_has_sval(estate_rl(estate), sval_type_val(estate_type(estate), 0)))
1545 return 1;
1546 return 0;
1549 int parent_is_null_var_sym(const char *name, struct symbol *sym)
1551 char buf[256];
1552 char *start;
1553 char *end;
1554 struct smatch_state *state;
1556 strncpy(buf, name, sizeof(buf) - 1);
1557 buf[sizeof(buf) - 1] = '\0';
1559 start = &buf[0];
1560 while (*start == '*') {
1561 start++;
1562 state = get_state(SMATCH_EXTRA, start, sym);
1563 if (!state)
1564 continue;
1565 if (!estate_rl(state))
1566 return 1;
1567 if (estate_min(state).value == 0 &&
1568 estate_max(state).value == 0)
1569 return 1;
1572 start = &buf[0];
1573 while (*start == '&')
1574 start++;
1576 while ((end = strrchr(start, '-'))) {
1577 *end = '\0';
1578 state = get_state(SMATCH_EXTRA, start, sym);
1579 if (!state)
1580 continue;
1581 if (estate_min(state).value == 0 &&
1582 estate_max(state).value == 0)
1583 return 1;
1585 return 0;
1588 int parent_is_null(struct expression *expr)
1590 struct symbol *sym;
1591 char *var;
1592 int ret = 0;
1594 expr = strip_expr(expr);
1595 var = expr_to_var_sym(expr, &sym);
1596 if (!var || !sym)
1597 goto free;
1598 ret = parent_is_null_var_sym(var, sym);
1599 free:
1600 free_string(var);
1601 return ret;
1604 static int param_used_callback(void *found, int argc, char **argv, char **azColName)
1606 *(int *)found = 1;
1607 return 0;
1610 static int filter_unused_kzalloc_info(struct expression *call, int param, char *printed_name, struct sm_state *sm)
1612 sval_t sval;
1613 int found = 0;
1615 /* for function pointers assume everything is used */
1616 if (call->fn->type != EXPR_SYMBOL)
1617 return 0;
1620 * kzalloc() information is treated as special because so there is just
1621 * a lot of stuff initialized to zero and it makes building the database
1622 * take hours and hours.
1624 * In theory, we should just remove this line and not pass any unused
1625 * information, but I'm not sure enough that this code works so I want
1626 * to hold off on that for now.
1628 if (!estate_get_single_value(sm->state, &sval) || sval.value != 0)
1629 return 0;
1631 run_sql(&param_used_callback, &found,
1632 "select * from call_implies where %s and type = %d and parameter = %d and key = '%s';",
1633 get_static_filter(call->fn->symbol), PARAM_USED, param, printed_name);
1634 if (found)
1635 return 0;
1637 /* If the database is not built yet, then assume everything is used */
1638 run_sql(&param_used_callback, &found,
1639 "select * from call_implies where %s and type = %d;",
1640 get_static_filter(call->fn->symbol), PARAM_USED);
1641 if (!found)
1642 return 0;
1644 return 1;
1647 static void struct_member_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
1649 if (estate_is_whole(sm->state))
1650 return;
1651 if (filter_unused_kzalloc_info(call, param, printed_name, sm))
1652 return;
1653 sql_insert_caller_info(call, PARAM_VALUE, param, printed_name, sm->state->name);
1654 if (estate_has_fuzzy_max(sm->state))
1655 sql_insert_caller_info(call, FUZZY_MAX, param, printed_name,
1656 sval_to_str(estate_get_fuzzy_max(sm->state)));
1659 static void db_limited_before(void)
1661 unmatched_stree = clone_stree(__get_cur_stree());
1664 static void db_limited_after(void)
1666 free_stree(&unmatched_stree);
1669 static void db_param_limit_filter(struct expression *expr, int param, char *key, char *value, enum info_type op)
1671 struct expression *arg;
1672 char *name;
1673 struct symbol *sym;
1674 struct sm_state *sm;
1675 struct symbol *compare_type, *var_type;
1676 struct range_list *rl;
1677 struct range_list *limit;
1678 struct range_list *new;
1680 while (expr->type == EXPR_ASSIGNMENT)
1681 expr = strip_expr(expr->right);
1682 if (expr->type != EXPR_CALL)
1683 return;
1685 arg = get_argument_from_call_expr(expr->args, param);
1686 if (!arg)
1687 return;
1689 name = get_variable_from_key(arg, key, &sym);
1690 if (!name || !sym)
1691 goto free;
1693 if (strcmp(key, "$") == 0)
1694 compare_type = get_arg_type(expr->fn, param);
1695 else
1696 compare_type = get_member_type_from_key(arg, key);
1698 sm = get_sm_state(SMATCH_EXTRA, name, sym);
1699 if (sm)
1700 rl = estate_rl(sm->state);
1701 else
1702 rl = alloc_whole_rl(compare_type);
1704 call_results_to_rl(expr, compare_type, value, &limit);
1705 new = rl_intersection(rl, limit);
1707 var_type = get_member_type_from_key(arg, key);
1708 new = cast_rl(var_type, new);
1710 /* We want to preserve the implications here */
1711 if (sm && rl_equiv(estate_rl(sm->state), new))
1712 __set_sm(sm);
1713 else {
1714 if (op == PARAM_LIMIT)
1715 set_extra_nomod(name, sym, alloc_estate_rl(new));
1716 else
1717 set_extra_mod(name, sym, alloc_estate_rl(new));
1720 free:
1721 free_string(name);
1724 static void db_param_limit(struct expression *expr, int param, char *key, char *value)
1726 db_param_limit_filter(expr, param, key, value, PARAM_LIMIT);
1729 static void db_param_filter(struct expression *expr, int param, char *key, char *value)
1731 db_param_limit_filter(expr, param, key, value, PARAM_FILTER);
1734 static void db_param_add_set(struct expression *expr, int param, char *key, char *value, enum info_type op)
1736 struct expression *arg;
1737 char *name;
1738 struct symbol *sym;
1739 struct symbol *type;
1740 struct smatch_state *state;
1741 struct range_list *new = NULL;
1742 struct range_list *added = NULL;
1744 while (expr->type == EXPR_ASSIGNMENT)
1745 expr = strip_expr(expr->right);
1746 if (expr->type != EXPR_CALL)
1747 return;
1749 arg = get_argument_from_call_expr(expr->args, param);
1750 if (!arg)
1751 return;
1752 type = get_member_type_from_key(arg, key);
1753 name = get_variable_from_key(arg, key, &sym);
1754 if (!name || !sym)
1755 goto free;
1757 state = get_state(SMATCH_EXTRA, name, sym);
1758 if (state)
1759 new = estate_rl(state);
1761 call_results_to_rl(expr, type, value, &added);
1763 if (op == PARAM_SET)
1764 new = added;
1765 else
1766 new = rl_union(new, added);
1768 set_extra_mod(name, sym, alloc_estate_rl(new));
1769 free:
1770 free_string(name);
1773 static void db_param_add(struct expression *expr, int param, char *key, char *value)
1775 db_param_add_set(expr, param, key, value, PARAM_ADD);
1778 static void db_param_set(struct expression *expr, int param, char *key, char *value)
1780 db_param_add_set(expr, param, key, value, PARAM_SET);
1783 static void db_returned_member_info(struct expression *expr, int param, char *key, char *value)
1785 struct symbol *type;
1786 struct symbol *sym;
1787 char *name;
1788 struct range_list *rl;
1790 if (expr->type != EXPR_ASSIGNMENT)
1791 return;
1793 name = return_state_to_var_sym(expr, param, key, &sym);
1794 if (!name || !sym)
1795 goto free;
1796 type = get_member_type_from_key(expr->left, key);
1797 if (!type)
1798 return;
1799 call_results_to_rl(expr->right, type, value, &rl);
1800 set_extra_mod(name, sym, alloc_estate_rl(rl));
1802 free:
1803 free_string(name);
1806 static char *in_terms_of_param_math(struct expression *expr, char *printed_name)
1808 struct symbol *sym;
1809 char *var;
1810 char buf[256];
1811 char *ret = NULL;
1813 var = expr_to_var_sym(expr, &sym);
1814 if (!var || !sym)
1815 goto free;
1816 snprintf(buf, sizeof(buf), "%s%s", var, printed_name + 2);
1818 ret = get_value_in_terms_of_parameter_math_var_sym(buf, sym);
1820 free:
1821 free_string(var);
1822 return ret;
1825 static void returned_member_callback(int return_id, char *return_ranges, struct expression *expr, char *printed_name, struct smatch_state *state)
1827 char buf[256];
1828 char *math_str;
1829 sval_t sval;
1831 /* these are handled in smatch_param_filter/set/limit.c */
1832 if (printed_name[0] != '*' &&
1833 !strchr(printed_name, '.') && !strchr(printed_name, '-'))
1834 return;
1836 if (estate_get_single_value(state, &sval)) {
1837 sql_insert_return_states(return_id, return_ranges, RETURN_VALUE, -1,
1838 printed_name, sval_to_str(sval));
1839 return;
1842 math_str = in_terms_of_param_math(expr, printed_name);
1843 if (math_str) {
1844 snprintf(buf, sizeof(buf), "%s[%s]", state->name, math_str);
1845 sql_insert_return_states(return_id, return_ranges, RETURN_VALUE, -1,
1846 printed_name, buf);
1847 return;
1850 if (estate_is_whole(state))
1851 return;
1853 sql_insert_return_states(return_id, return_ranges, RETURN_VALUE, -1,
1854 printed_name, state->name);
1857 static void match_call_info(struct expression *expr)
1859 struct smatch_state *state;
1860 struct range_list *rl = NULL;
1861 struct expression *arg;
1862 struct symbol *type;
1863 int i = 0;
1865 FOR_EACH_PTR(expr->args, arg) {
1866 type = get_arg_type(expr->fn, i);
1868 if (get_implied_rl(arg, &rl))
1869 rl = cast_rl(type, rl);
1870 else
1871 rl = cast_rl(type, alloc_whole_rl(get_type(arg)));
1873 if (!is_whole_rl(rl))
1874 sql_insert_caller_info(expr, PARAM_VALUE, i, "$", show_rl(rl));
1875 state = get_state_expr(SMATCH_EXTRA, arg);
1876 if (estate_has_fuzzy_max(state)) {
1877 sql_insert_caller_info(expr, FUZZY_MAX, i, "$",
1878 sval_to_str(estate_get_fuzzy_max(state)));
1880 i++;
1881 } END_FOR_EACH_PTR(arg);
1884 static void set_param_value(const char *name, struct symbol *sym, char *key, char *value)
1886 struct range_list *rl = NULL;
1887 struct smatch_state *state;
1888 struct symbol *type;
1889 char fullname[256];
1891 if (strcmp(key, "*$") == 0)
1892 snprintf(fullname, sizeof(fullname), "*%s", name);
1893 else if (strncmp(key, "$", 1) == 0)
1894 snprintf(fullname, 256, "%s%s", name, key + 1);
1895 else
1896 return;
1898 type = get_member_type_from_key(symbol_expression(sym), key);
1899 str_to_rl(type, value, &rl);
1900 state = alloc_estate_rl(rl);
1901 set_state(SMATCH_EXTRA, fullname, sym, state);
1904 static void set_param_hard_max(const char *name, struct symbol *sym, char *key, char *value)
1906 struct range_list *rl = NULL;
1907 struct smatch_state *state;
1908 struct symbol *type;
1909 char fullname[256];
1910 sval_t max;
1912 if (strcmp(key, "*$") == 0)
1913 snprintf(fullname, sizeof(fullname), "*%s", name);
1914 else if (strncmp(key, "$", 1) == 0)
1915 snprintf(fullname, 256, "%s%s", name, key + 1);
1916 else
1917 return;
1919 state = get_state(SMATCH_EXTRA, fullname, sym);
1920 if (!state)
1921 return;
1922 type = get_member_type_from_key(symbol_expression(sym), key);
1923 str_to_rl(type, value, &rl);
1924 if (!rl_to_sval(rl, &max))
1925 return;
1926 estate_set_fuzzy_max(state, max);
1929 struct smatch_state *get_extra_state(struct expression *expr)
1931 char *name;
1932 struct symbol *sym;
1933 struct smatch_state *ret = NULL;
1934 struct range_list *rl;
1936 if (is_pointer(expr) && get_address_rl(expr, &rl))
1937 return alloc_estate_rl(rl);
1939 name = expr_to_known_chunk_sym(expr, &sym);
1940 if (!name)
1941 goto free;
1943 ret = get_state(SMATCH_EXTRA, name, sym);
1944 free:
1945 free_string(name);
1946 return ret;
1949 void register_smatch_extra(int id)
1951 my_id = id;
1953 add_merge_hook(my_id, &merge_estates);
1954 add_unmatched_state_hook(my_id, &unmatched_state);
1955 select_caller_info_hook(set_param_value, PARAM_VALUE);
1956 select_caller_info_hook(set_param_hard_max, FUZZY_MAX);
1957 select_return_states_hook(RETURN_VALUE, &db_returned_member_info);
1958 select_return_states_before(&db_limited_before);
1959 select_return_states_hook(PARAM_LIMIT, &db_param_limit);
1960 select_return_states_hook(PARAM_FILTER, &db_param_filter);
1961 select_return_states_hook(PARAM_ADD, &db_param_add);
1962 select_return_states_hook(PARAM_SET, &db_param_set);
1963 select_return_states_after(&db_limited_after);
1966 static void match_link_modify(struct sm_state *sm, struct expression *mod_expr)
1968 struct var_sym_list *links;
1969 struct var_sym *tmp;
1970 struct smatch_state *state;
1972 links = sm->state->data;
1974 FOR_EACH_PTR(links, tmp) {
1975 state = get_state(SMATCH_EXTRA, tmp->var, tmp->sym);
1976 if (!state)
1977 continue;
1978 set_state(SMATCH_EXTRA, tmp->var, tmp->sym, alloc_estate_whole(estate_type(state)));
1979 } END_FOR_EACH_PTR(tmp);
1980 set_state(link_id, sm->name, sm->sym, &undefined);
1983 void register_smatch_extra_links(int id)
1985 link_id = id;
1988 void register_smatch_extra_late(int id)
1990 add_merge_hook(link_id, &merge_link_states);
1991 add_modification_hook(link_id, &match_link_modify);
1992 add_hook(&match_dereferences, DEREF_HOOK);
1993 add_hook(&match_pointer_as_array, OP_HOOK);
1994 select_call_implies_hook(DEREFERENCE, &set_param_dereferenced);
1995 add_hook(&match_function_call, FUNCTION_CALL_HOOK);
1996 add_hook(&match_assign, ASSIGNMENT_HOOK);
1997 add_hook(&match_assign, GLOBAL_ASSIGNMENT_HOOK);
1998 add_hook(&unop_expr, OP_HOOK);
1999 add_hook(&asm_expr, ASM_HOOK);
2001 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
2002 add_member_info_callback(my_id, struct_member_callback);
2003 add_returned_member_callback(my_id, returned_member_callback);
2005 add_hook(&assume_indexes_are_valid, OP_HOOK);