type: handle pointer type correctly
[smatch.git] / smatch_extra.c
bloba6fab5520aeb716f3dcb33a032117a439349a985
1 /*
2 * Copyright (C) 2008 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
19 * smatch_extra.c is supposed to track the value of every variable.
23 #define _GNU_SOURCE
24 #include <string.h>
26 #include <stdlib.h>
27 #include <errno.h>
28 #ifndef __USE_ISOC99
29 #define __USE_ISOC99
30 #endif
31 #include <limits.h>
32 #include "parse.h"
33 #include "smatch.h"
34 #include "smatch_slist.h"
35 #include "smatch_extra.h"
37 static int my_id;
38 static int link_id;
40 static void match_link_modify(struct sm_state *sm, struct expression *mod_expr);
42 struct string_list *__ignored_macros = NULL;
43 int in_warn_on_macro(void)
45 struct statement *stmt;
46 char *tmp;
47 char *macro;
49 stmt = get_current_statement();
50 if (!stmt)
51 return 0;
52 macro = get_macro_name(stmt->pos);
53 if (!macro)
54 return 0;
56 FOR_EACH_PTR(__ignored_macros, tmp) {
57 if (!strcmp(tmp, macro))
58 return 1;
59 } END_FOR_EACH_PTR(tmp);
60 return 0;
63 typedef void (mod_hook)(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state);
64 DECLARE_PTR_LIST(void_fn_list, mod_hook *);
65 static struct void_fn_list *extra_mod_hooks;
66 static struct void_fn_list *extra_nomod_hooks;
68 void add_extra_mod_hook(mod_hook *fn)
70 mod_hook **p = malloc(sizeof(mod_hook *));
71 *p = fn;
72 add_ptr_list(&extra_mod_hooks, p);
75 void add_extra_nomod_hook(mod_hook *fn)
77 mod_hook **p = malloc(sizeof(mod_hook *));
78 *p = fn;
79 add_ptr_list(&extra_nomod_hooks, p);
82 void call_extra_hooks(struct void_fn_list *hooks, const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
84 mod_hook **fn;
86 FOR_EACH_PTR(hooks, fn) {
87 (*fn)(name, sym, expr, state);
88 } END_FOR_EACH_PTR(fn);
91 void call_extra_mod_hooks(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
93 call_extra_hooks(extra_mod_hooks, name, sym, expr, state);
96 void call_extra_nomod_hooks(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
98 call_extra_hooks(extra_nomod_hooks, name, sym, expr, state);
101 static bool in_param_set;
102 void set_extra_mod_helper(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
104 remove_from_equiv(name, sym);
105 call_extra_mod_hooks(name, sym, expr, state);
106 if ((__in_fake_assign || in_param_set) &&
107 estate_is_unknown(state) && !get_state(SMATCH_EXTRA, name, sym))
108 return;
109 set_state(SMATCH_EXTRA, name, sym, state);
112 static void set_extra_nomod_helper(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
114 call_extra_nomod_hooks(name, sym, expr, state);
115 set_state(SMATCH_EXTRA, name, sym, state);
118 static char *get_pointed_at(const char *name, struct symbol *sym, struct symbol **new_sym)
120 struct expression *assigned;
122 if (name[0] != '*')
123 return NULL;
124 if (strcmp(name + 1, sym->ident->name) != 0)
125 return NULL;
127 assigned = get_assigned_expr_name_sym(sym->ident->name, sym);
128 if (!assigned)
129 return NULL;
130 assigned = strip_parens(assigned);
131 if (assigned->type != EXPR_PREOP || assigned->op != '&')
132 return NULL;
134 return expr_to_var_sym(assigned->unop, new_sym);
137 char *get_other_name_sym_helper(const char *name, const char *chunk, int len, struct symbol *sym, struct symbol **new_sym)
139 struct expression *assigned;
140 char *orig_name = NULL;
141 char buf[256];
142 char *ret = NULL;
144 assigned = get_assigned_expr_name_sym(chunk, sym);
145 if (!assigned)
146 return NULL;
147 if (assigned->type == EXPR_CALL)
148 return map_call_to_other_name_sym(name, sym, new_sym);
149 if (assigned->type == EXPR_PREOP && assigned->op == '&') {
151 orig_name = expr_to_var_sym(assigned, new_sym);
152 if (!orig_name || !*new_sym)
153 goto free;
155 snprintf(buf, sizeof(buf), "%s.%s", orig_name + 1, name + len);
156 ret = alloc_string(buf);
157 free_string(orig_name);
158 return ret;
161 orig_name = expr_to_var_sym(assigned, new_sym);
162 if (!orig_name || !*new_sym)
163 goto free;
165 snprintf(buf, sizeof(buf), "%s->%s", orig_name, name + len);
166 ret = alloc_string(buf);
167 free_string(orig_name);
168 return ret;
169 free:
170 free_string(orig_name);
171 return NULL;
174 char *get_other_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym)
176 char buf[256];
177 char *ret;
178 int len;
180 *new_sym = NULL;
182 if (!sym || !sym->ident)
183 return NULL;
185 ret = get_pointed_at(name, sym, new_sym);
186 if (ret)
187 return ret;
189 len = snprintf(buf, sizeof(buf), "%s", name);
190 if (len >= sizeof(buf) - 2)
191 return NULL;
193 while (len >= 1) {
194 if (buf[len] == '>' && buf[len - 1] == '-') {
195 len--;
196 buf[len] = '\0';
197 ret = get_other_name_sym_helper(name, buf, len + 2, sym, new_sym);
198 if (ret)
199 return ret;
201 len--;
204 return NULL;
206 void set_extra_mod(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
208 char *new_name;
209 struct symbol *new_sym;
211 set_extra_mod_helper(name, sym, expr, state);
212 new_name = get_other_name_sym(name, sym, &new_sym);
213 if (new_name && new_sym)
214 set_extra_mod_helper(new_name, new_sym, expr, state);
215 free_string(new_name);
218 static struct expression *chunk_get_array_base(struct expression *expr)
221 * The problem with is_array() is that it only returns true for things
222 * like foo[1] but not for foo[1].bar.
225 expr = strip_expr(expr);
226 while (expr && expr->type == EXPR_DEREF)
227 expr = strip_expr(expr->deref);
228 return get_array_base(expr);
231 static int chunk_has_array(struct expression *expr)
233 return !!chunk_get_array_base(expr);
236 static void clear_array_states(struct expression *array)
238 struct sm_state *sm;
240 sm = get_sm_state_expr(link_id, array);
241 if (sm)
242 match_link_modify(sm, NULL);
245 static void set_extra_array_mod(struct expression *expr, struct smatch_state *state)
247 struct expression *array;
248 struct var_sym_list *vsl;
249 struct var_sym *vs;
250 char *name;
251 struct symbol *sym;
253 array = chunk_get_array_base(expr);
255 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
256 if (!name || !vsl) {
257 clear_array_states(array);
258 goto free;
261 FOR_EACH_PTR(vsl, vs) {
262 store_link(link_id, vs->var, vs->sym, name, sym);
263 } END_FOR_EACH_PTR(vs);
265 call_extra_mod_hooks(name, sym, expr, state);
266 set_state(SMATCH_EXTRA, name, sym, state);
267 free:
268 free_string(name);
271 void set_extra_expr_mod(struct expression *expr, struct smatch_state *state)
273 struct symbol *sym;
274 char *name;
276 if (chunk_has_array(expr)) {
277 set_extra_array_mod(expr, state);
278 return;
281 expr = strip_expr(expr);
282 name = expr_to_var_sym(expr, &sym);
283 if (!name || !sym)
284 goto free;
285 set_extra_mod(name, sym, expr, state);
286 free:
287 free_string(name);
290 void set_extra_nomod(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
292 char *new_name;
293 struct symbol *new_sym;
294 struct relation *rel;
295 struct smatch_state *orig_state;
297 orig_state = get_state(SMATCH_EXTRA, name, sym);
299 /* don't save unknown states if leaving it blank is the same */
300 if (!orig_state && estate_is_unknown(state))
301 return;
303 new_name = get_other_name_sym(name, sym, &new_sym);
304 if (new_name && new_sym)
305 set_extra_nomod_helper(new_name, new_sym, expr, state);
306 free_string(new_name);
308 if (!estate_related(orig_state)) {
309 set_extra_nomod_helper(name, sym, expr, state);
310 return;
313 set_related(state, estate_related(orig_state));
314 FOR_EACH_PTR(estate_related(orig_state), rel) {
315 struct smatch_state *estate;
317 if (option_debug_related)
318 sm_msg("%s updating related %s to %s", name, rel->name, state->name);
319 estate = get_state(SMATCH_EXTRA, rel->name, rel->sym);
320 if (!estate)
321 continue;
322 set_extra_nomod_helper(rel->name, rel->sym, expr, clone_estate_cast(estate_type(estate), state));
323 } END_FOR_EACH_PTR(rel);
326 void set_extra_nomod_vsl(const char *name, struct symbol *sym, struct var_sym_list *vsl, struct expression *expr, struct smatch_state *state)
328 struct var_sym *vs;
330 FOR_EACH_PTR(vsl, vs) {
331 store_link(link_id, vs->var, vs->sym, name, sym);
332 } END_FOR_EACH_PTR(vs);
334 set_extra_nomod(name, sym, expr, state);
338 * This is for return_implies_state() hooks which modify a SMATCH_EXTRA state
340 void set_extra_expr_nomod(struct expression *expr, struct smatch_state *state)
342 struct var_sym_list *vsl;
343 struct var_sym *vs;
344 char *name;
345 struct symbol *sym;
347 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
348 if (!name || !vsl)
349 goto free;
350 FOR_EACH_PTR(vsl, vs) {
351 store_link(link_id, vs->var, vs->sym, name, sym);
352 } END_FOR_EACH_PTR(vs);
354 set_extra_nomod(name, sym, expr, state);
355 free:
356 free_string(name);
359 static void set_extra_true_false(const char *name, struct symbol *sym,
360 struct smatch_state *true_state,
361 struct smatch_state *false_state)
363 char *new_name;
364 struct symbol *new_sym;
365 struct relation *rel;
366 struct smatch_state *orig_state;
368 if (!true_state && !false_state)
369 return;
371 if (in_warn_on_macro())
372 return;
374 new_name = get_other_name_sym(name, sym, &new_sym);
375 if (new_name && new_sym)
376 set_true_false_states(SMATCH_EXTRA, new_name, new_sym, true_state, false_state);
377 free_string(new_name);
379 orig_state = get_state(SMATCH_EXTRA, name, sym);
381 if (!estate_related(orig_state)) {
382 set_true_false_states(SMATCH_EXTRA, name, sym, true_state, false_state);
383 return;
386 if (true_state)
387 set_related(true_state, estate_related(orig_state));
388 if (false_state)
389 set_related(false_state, estate_related(orig_state));
391 FOR_EACH_PTR(estate_related(orig_state), rel) {
392 set_true_false_states(SMATCH_EXTRA, rel->name, rel->sym,
393 true_state, false_state);
394 } END_FOR_EACH_PTR(rel);
397 static void set_extra_chunk_true_false(struct expression *expr,
398 struct smatch_state *true_state,
399 struct smatch_state *false_state)
401 struct var_sym_list *vsl;
402 struct var_sym *vs;
403 struct symbol *type;
404 char *name;
405 struct symbol *sym;
407 if (in_warn_on_macro())
408 return;
410 type = get_type(expr);
411 if (!type)
412 return;
414 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
415 if (!name || !vsl)
416 goto free;
417 FOR_EACH_PTR(vsl, vs) {
418 store_link(link_id, vs->var, vs->sym, name, sym);
419 } END_FOR_EACH_PTR(vs);
421 set_true_false_states(SMATCH_EXTRA, name, sym,
422 clone_estate(true_state),
423 clone_estate(false_state));
424 free:
425 free_string(name);
428 static void set_extra_expr_true_false(struct expression *expr,
429 struct smatch_state *true_state,
430 struct smatch_state *false_state)
432 char *name;
433 struct symbol *sym;
434 sval_t sval;
436 if (!true_state && !false_state)
437 return;
439 if (get_value(expr, &sval))
440 return;
442 expr = strip_expr(expr);
443 name = expr_to_var_sym(expr, &sym);
444 if (!name || !sym) {
445 free_string(name);
446 set_extra_chunk_true_false(expr, true_state, false_state);
447 return;
449 set_extra_true_false(name, sym, true_state, false_state);
450 free_string(name);
453 static int get_countdown_info(struct expression *condition, struct expression **unop, int *op, sval_t *right)
455 struct expression *unop_expr;
456 int comparison;
457 sval_t limit;
459 right->type = &int_ctype;
460 right->value = 0;
462 condition = strip_expr(condition);
464 if (condition->type == EXPR_COMPARE) {
465 comparison = remove_unsigned_from_comparison(condition->op);
467 if (comparison != SPECIAL_GTE && comparison != '>')
468 return 0;
469 if (!get_value(condition->right, &limit))
470 return 0;
472 unop_expr = condition->left;
473 if (unop_expr->type != EXPR_PREOP && unop_expr->type != EXPR_POSTOP)
474 return 0;
475 if (unop_expr->op != SPECIAL_DECREMENT)
476 return 0;
478 *unop = unop_expr;
479 *op = comparison;
480 *right = limit;
482 return 1;
485 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
486 return 0;
487 if (condition->op != SPECIAL_DECREMENT)
488 return 0;
490 *unop = condition;
491 *op = '>';
493 return 1;
496 static struct sm_state *handle_canonical_while_count_down(struct statement *loop)
498 struct expression *iter_var;
499 struct expression *condition, *unop;
500 struct sm_state *sm;
501 struct smatch_state *estate;
502 int op;
503 sval_t start, right;
505 right.type = &int_ctype;
506 right.value = 0;
508 condition = strip_expr(loop->iterator_pre_condition);
509 if (!condition)
510 return NULL;
512 if (!get_countdown_info(condition, &unop, &op, &right))
513 return NULL;
515 iter_var = unop->unop;
517 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
518 if (!sm)
519 return NULL;
520 if (sval_cmp(estate_min(sm->state), right) < 0)
521 return NULL;
522 start = estate_max(sm->state);
523 if (sval_cmp(start, right) <= 0)
524 return NULL;
525 if (!sval_is_max(start))
526 start.value--;
528 if (op == SPECIAL_GTE)
529 right.value--;
531 if (unop->type == EXPR_PREOP) {
532 right.value++;
533 estate = alloc_estate_range(right, start);
534 if (estate_has_hard_max(sm->state))
535 estate_set_hard_max(estate);
536 estate_copy_fuzzy_max(estate, sm->state);
537 set_extra_expr_mod(iter_var, estate);
539 if (unop->type == EXPR_POSTOP) {
540 estate = alloc_estate_range(right, start);
541 if (estate_has_hard_max(sm->state))
542 estate_set_hard_max(estate);
543 estate_copy_fuzzy_max(estate, sm->state);
544 set_extra_expr_mod(iter_var, estate);
546 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
549 static struct sm_state *handle_canonical_for_inc(struct expression *iter_expr,
550 struct expression *condition)
552 struct expression *iter_var;
553 struct sm_state *sm;
554 struct smatch_state *estate;
555 sval_t start, end, max;
557 iter_var = iter_expr->unop;
558 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
559 if (!sm)
560 return NULL;
561 if (!estate_get_single_value(sm->state, &start))
562 return NULL;
563 if (get_implied_max(condition->right, &end))
564 end = sval_cast(get_type(iter_var), end);
565 else
566 end = sval_type_max(get_type(iter_var));
568 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
569 return NULL;
571 switch (condition->op) {
572 case SPECIAL_UNSIGNED_LT:
573 case SPECIAL_NOTEQUAL:
574 case '<':
575 if (!sval_is_min(end))
576 end.value--;
577 break;
578 case SPECIAL_UNSIGNED_LTE:
579 case SPECIAL_LTE:
580 break;
581 default:
582 return NULL;
584 if (sval_cmp(end, start) < 0)
585 return NULL;
586 estate = alloc_estate_range(start, end);
587 if (get_hard_max(condition->right, &max)) {
588 estate_set_hard_max(estate);
589 if (condition->op == '<' ||
590 condition->op == SPECIAL_UNSIGNED_LT ||
591 condition->op == SPECIAL_NOTEQUAL)
592 max.value--;
593 estate_set_fuzzy_max(estate, max);
595 set_extra_expr_mod(iter_var, estate);
596 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
599 static struct sm_state *handle_canonical_for_dec(struct expression *iter_expr,
600 struct expression *condition)
602 struct expression *iter_var;
603 struct sm_state *sm;
604 struct smatch_state *estate;
605 sval_t start, end;
607 iter_var = iter_expr->unop;
608 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
609 if (!sm)
610 return NULL;
611 if (!estate_get_single_value(sm->state, &start))
612 return NULL;
613 if (!get_implied_min(condition->right, &end))
614 end = sval_type_min(get_type(iter_var));
615 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
616 return NULL;
618 switch (condition->op) {
619 case SPECIAL_NOTEQUAL:
620 case '>':
621 if (!sval_is_min(end) && !sval_is_max(end))
622 end.value++;
623 break;
624 case SPECIAL_GTE:
625 break;
626 default:
627 return NULL;
629 if (sval_cmp(end, start) > 0)
630 return NULL;
631 estate = alloc_estate_range(end, start);
632 estate_set_hard_max(estate);
633 estate_set_fuzzy_max(estate, estate_get_fuzzy_max(estate));
634 set_extra_expr_mod(iter_var, estate);
635 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
638 static struct sm_state *handle_canonical_for_loops(struct statement *loop)
640 struct expression *iter_expr;
641 struct expression *condition;
643 if (!loop->iterator_post_statement)
644 return NULL;
645 if (loop->iterator_post_statement->type != STMT_EXPRESSION)
646 return NULL;
647 iter_expr = loop->iterator_post_statement->expression;
648 if (!loop->iterator_pre_condition)
649 return NULL;
650 if (loop->iterator_pre_condition->type != EXPR_COMPARE)
651 return NULL;
652 condition = loop->iterator_pre_condition;
654 if (iter_expr->op == SPECIAL_INCREMENT)
655 return handle_canonical_for_inc(iter_expr, condition);
656 if (iter_expr->op == SPECIAL_DECREMENT)
657 return handle_canonical_for_dec(iter_expr, condition);
658 return NULL;
661 struct sm_state *__extra_handle_canonical_loops(struct statement *loop, struct stree **stree)
663 struct sm_state *ret;
666 * Canonical loops are a hack. The proper way to handle this is to
667 * use two passes, but unfortunately, doing two passes makes parsing
668 * code twice as slow.
670 * What we do is we set the inside state here, which overwrites whatever
671 * __extra_match_condition() does. Then we set the outside state in
672 * __extra_pre_loop_hook_after().
675 __push_fake_cur_stree();
676 if (!loop->iterator_post_statement)
677 ret = handle_canonical_while_count_down(loop);
678 else
679 ret = handle_canonical_for_loops(loop);
680 *stree = __pop_fake_cur_stree();
681 return ret;
684 int __iterator_unchanged(struct sm_state *sm)
686 if (!sm)
687 return 0;
688 if (get_sm_state(my_id, sm->name, sm->sym) == sm)
689 return 1;
690 return 0;
693 static void while_count_down_after(struct sm_state *sm, struct expression *condition)
695 struct expression *unop;
696 int op;
697 sval_t limit, after_value;
699 if (!get_countdown_info(condition, &unop, &op, &limit))
700 return;
701 after_value = estate_min(sm->state);
702 after_value.value--;
703 set_extra_mod(sm->name, sm->sym, condition->unop, alloc_estate_sval(after_value));
706 void __extra_pre_loop_hook_after(struct sm_state *sm,
707 struct statement *iterator,
708 struct expression *condition)
710 struct expression *iter_expr;
711 sval_t limit;
712 struct smatch_state *state;
714 if (!iterator) {
715 while_count_down_after(sm, condition);
716 return;
719 iter_expr = iterator->expression;
721 if (condition->type != EXPR_COMPARE)
722 return;
723 if (iter_expr->op == SPECIAL_INCREMENT) {
724 limit = sval_binop(estate_max(sm->state), '+',
725 sval_type_val(estate_type(sm->state), 1));
726 } else {
727 limit = sval_binop(estate_min(sm->state), '-',
728 sval_type_val(estate_type(sm->state), 1));
730 if (!estate_has_hard_max(sm->state) && !__has_breaks()) {
731 if (iter_expr->op == SPECIAL_INCREMENT)
732 state = alloc_estate_range(estate_min(sm->state), limit);
733 else
734 state = alloc_estate_range(limit, estate_max(sm->state));
735 } else {
736 state = alloc_estate_sval(limit);
738 if (!estate_has_hard_max(sm->state)) {
739 estate_clear_hard_max(state);
741 if (estate_has_fuzzy_max(sm->state)) {
742 sval_t hmax = estate_get_fuzzy_max(sm->state);
743 sval_t max = estate_max(sm->state);
745 if (sval_cmp(hmax, max) != 0)
746 estate_clear_fuzzy_max(state);
747 } else if (!estate_has_fuzzy_max(sm->state)) {
748 estate_clear_fuzzy_max(state);
751 set_extra_mod(sm->name, sm->sym, iter_expr, state);
754 static struct stree *unmatched_stree;
755 static struct smatch_state *unmatched_state(struct sm_state *sm)
757 struct smatch_state *state;
759 if (unmatched_stree) {
760 state = get_state_stree(unmatched_stree, SMATCH_EXTRA, sm->name, sm->sym);
761 if (state)
762 return state;
764 if (parent_is_gone_var_sym(sm->name, sm->sym))
765 return alloc_estate_empty();
766 return alloc_estate_whole(estate_type(sm->state));
769 static void clear_the_pointed_at(struct expression *expr)
771 struct stree *stree;
772 char *name;
773 struct symbol *sym;
774 struct sm_state *tmp;
776 name = expr_to_var_sym(expr, &sym);
777 if (!name || !sym)
778 goto free;
780 stree = __get_cur_stree();
781 FOR_EACH_MY_SM(SMATCH_EXTRA, stree, tmp) {
782 if (tmp->name[0] != '*')
783 continue;
784 if (tmp->sym != sym)
785 continue;
786 if (strcmp(tmp->name + 1, name) != 0)
787 continue;
788 set_extra_mod(tmp->name, tmp->sym, expr, alloc_estate_whole(estate_type(tmp->state)));
789 } END_FOR_EACH_SM(tmp);
791 free:
792 free_string(name);
795 static int is_const_param(struct expression *expr, int param)
797 struct symbol *type;
799 type = get_arg_type(expr, param);
800 if (!type)
801 return 0;
802 if (type->ctype.modifiers & MOD_CONST)
803 return 1;
804 return 0;
807 static void match_function_call(struct expression *expr)
809 struct expression *arg;
810 struct expression *tmp;
811 int param = -1;
813 /* if we have the db this is handled in smatch_function_hooks.c */
814 if (!option_no_db)
815 return;
816 if (inlinable(expr->fn))
817 return;
819 FOR_EACH_PTR(expr->args, arg) {
820 param++;
821 if (is_const_param(expr->fn, param))
822 continue;
823 tmp = strip_expr(arg);
824 if (tmp->type == EXPR_PREOP && tmp->op == '&')
825 set_extra_expr_mod(tmp->unop, alloc_estate_whole(get_type(tmp->unop)));
826 else
827 clear_the_pointed_at(tmp);
828 } END_FOR_EACH_PTR(arg);
831 static int values_fit_type(struct expression *left, struct expression *right)
833 struct range_list *rl;
834 struct symbol *type;
836 type = get_type(left);
837 if (!type)
838 return 0;
839 get_absolute_rl(right, &rl);
840 if (type_unsigned(type) && sval_is_negative(rl_min(rl)))
841 return 0;
842 if (sval_cmp(sval_type_min(type), rl_min(rl)) > 0)
843 return 0;
844 if (sval_cmp(sval_type_max(type), rl_max(rl)) < 0)
845 return 0;
846 return 1;
849 static void save_chunk_info(struct expression *left, struct expression *right)
851 struct var_sym_list *vsl;
852 struct var_sym *vs;
853 struct expression *add_expr;
854 struct symbol *type;
855 sval_t sval;
856 char *name;
857 struct symbol *sym;
859 if (right->type != EXPR_BINOP || right->op != '-')
860 return;
861 if (!get_value(right->left, &sval))
862 return;
863 if (!expr_to_sym(right->right))
864 return;
866 add_expr = binop_expression(left, '+', right->right);
867 type = get_type(add_expr);
868 if (!type)
869 return;
870 name = expr_to_chunk_sym_vsl(add_expr, &sym, &vsl);
871 if (!name || !vsl)
872 goto free;
873 FOR_EACH_PTR(vsl, vs) {
874 store_link(link_id, vs->var, vs->sym, name, sym);
875 } END_FOR_EACH_PTR(vs);
877 set_state(SMATCH_EXTRA, name, sym, alloc_estate_sval(sval_cast(type, sval)));
878 free:
879 free_string(name);
882 static void do_array_assign(struct expression *left, int op, struct expression *right)
884 struct range_list *rl;
886 if (op == '=') {
887 get_absolute_rl(right, &rl);
888 rl = cast_rl(get_type(left), rl);
889 } else {
890 rl = alloc_whole_rl(get_type(left));
893 set_extra_array_mod(left, alloc_estate_rl(rl));
896 static void match_vanilla_assign(struct expression *left, struct expression *right)
898 struct range_list *orig_rl = NULL;
899 struct range_list *rl = NULL;
900 struct symbol *right_sym;
901 struct symbol *left_type;
902 struct symbol *right_type;
903 char *right_name = NULL;
904 struct symbol *sym;
905 char *name;
906 sval_t sval, max;
907 struct smatch_state *state;
908 int comparison;
910 if (is_struct(left))
911 return;
913 save_chunk_info(left, right);
915 name = expr_to_var_sym(left, &sym);
916 if (!name) {
917 if (chunk_has_array(left))
918 do_array_assign(left, '=', right);
919 return;
922 left_type = get_type(left);
923 right_type = get_type(right);
925 right_name = expr_to_var_sym(right, &right_sym);
927 if (!__in_fake_assign &&
928 !(right->type == EXPR_PREOP && right->op == '&') &&
929 right_name && right_sym &&
930 values_fit_type(left, strip_expr(right)) &&
931 !has_symbol(right, sym)) {
932 set_equiv(left, right);
933 goto free;
936 if (is_pointer(right) && get_address_rl(right, &rl)) {
937 state = alloc_estate_rl(rl);
938 goto done;
941 if (get_implied_value(right, &sval)) {
942 state = alloc_estate_sval(sval_cast(left_type, sval));
943 goto done;
946 if (__in_fake_assign) {
947 struct smatch_state *right_state;
948 sval_t sval;
950 if (get_value(right, &sval)) {
951 sval = sval_cast(left_type, sval);
952 state = alloc_estate_sval(sval);
953 goto done;
956 right_state = get_state(SMATCH_EXTRA, right_name, right_sym);
957 if (right_state) {
958 /* simple assignment */
959 state = clone_estate(right_state);
960 goto done;
963 state = alloc_estate_rl(alloc_whole_rl(left_type));
964 goto done;
967 comparison = get_comparison(left, right);
968 if (comparison) {
969 comparison = flip_comparison(comparison);
970 get_implied_rl(left, &orig_rl);
973 if (get_implied_rl(right, &rl)) {
974 rl = cast_rl(left_type, rl);
975 if (orig_rl)
976 filter_by_comparison(&rl, comparison, orig_rl);
977 state = alloc_estate_rl(rl);
978 if (get_hard_max(right, &max)) {
979 estate_set_hard_max(state);
980 estate_set_fuzzy_max(state, max);
982 } else {
983 rl = alloc_whole_rl(right_type);
984 rl = cast_rl(left_type, rl);
985 if (orig_rl)
986 filter_by_comparison(&rl, comparison, orig_rl);
987 state = alloc_estate_rl(rl);
990 done:
991 set_extra_mod(name, sym, left, state);
992 free:
993 free_string(right_name);
996 static int op_remove_assign(int op)
998 switch (op) {
999 case SPECIAL_ADD_ASSIGN:
1000 return '+';
1001 case SPECIAL_SUB_ASSIGN:
1002 return '-';
1003 case SPECIAL_MUL_ASSIGN:
1004 return '*';
1005 case SPECIAL_DIV_ASSIGN:
1006 return '/';
1007 case SPECIAL_MOD_ASSIGN:
1008 return '%';
1009 case SPECIAL_AND_ASSIGN:
1010 return '&';
1011 case SPECIAL_OR_ASSIGN:
1012 return '|';
1013 case SPECIAL_XOR_ASSIGN:
1014 return '^';
1015 case SPECIAL_SHL_ASSIGN:
1016 return SPECIAL_LEFTSHIFT;
1017 case SPECIAL_SHR_ASSIGN:
1018 return SPECIAL_RIGHTSHIFT;
1019 default:
1020 return op;
1024 static void match_assign(struct expression *expr)
1026 struct range_list *rl = NULL;
1027 struct expression *left;
1028 struct expression *right;
1029 struct expression *binop_expr;
1030 struct symbol *left_type;
1031 struct symbol *sym;
1032 char *name;
1033 sval_t left_min, left_max;
1034 sval_t right_min, right_max;
1035 sval_t res_min, res_max;
1037 left = strip_expr(expr->left);
1039 right = strip_parens(expr->right);
1040 if (right->type == EXPR_CALL && sym_name_is("__builtin_expect", right->fn))
1041 right = get_argument_from_call_expr(right->args, 0);
1042 while (right->type == EXPR_ASSIGNMENT && right->op == '=')
1043 right = strip_parens(right->left);
1045 if (expr->op == '=' && is_condition(expr->right))
1046 return; /* handled in smatch_condition.c */
1047 if (expr->op == '=' && right->type == EXPR_CALL)
1048 return; /* handled in smatch_function_hooks.c */
1049 if (expr->op == '=') {
1050 match_vanilla_assign(left, right);
1051 return;
1054 name = expr_to_var_sym(left, &sym);
1055 if (!name)
1056 return;
1058 left_type = get_type(left);
1060 res_min = sval_type_min(left_type);
1061 res_max = sval_type_max(left_type);
1063 switch (expr->op) {
1064 case SPECIAL_ADD_ASSIGN:
1065 get_absolute_max(left, &left_max);
1066 get_absolute_max(right, &right_max);
1067 if (sval_binop_overflows(left_max, '+', sval_cast(left_type, right_max)))
1068 break;
1069 if (get_implied_min(left, &left_min) &&
1070 !sval_is_negative_min(left_min) &&
1071 get_implied_min(right, &right_min) &&
1072 !sval_is_negative_min(right_min)) {
1073 res_min = sval_binop(left_min, '+', right_min);
1074 res_min = sval_cast(left_type, res_min);
1076 if (inside_loop()) /* we are assuming loops don't lead to wrapping */
1077 break;
1078 res_max = sval_binop(left_max, '+', right_max);
1079 res_max = sval_cast(left_type, res_max);
1080 break;
1081 case SPECIAL_SUB_ASSIGN:
1082 if (get_implied_max(left, &left_max) &&
1083 !sval_is_max(left_max) &&
1084 get_implied_min(right, &right_min) &&
1085 !sval_is_min(right_min)) {
1086 res_max = sval_binop(left_max, '-', right_min);
1087 res_max = sval_cast(left_type, res_max);
1089 if (inside_loop())
1090 break;
1091 if (get_implied_min(left, &left_min) &&
1092 !sval_is_min(left_min) &&
1093 get_implied_max(right, &right_max) &&
1094 !sval_is_max(right_max)) {
1095 res_min = sval_binop(left_min, '-', right_max);
1096 res_min = sval_cast(left_type, res_min);
1098 break;
1099 case SPECIAL_AND_ASSIGN:
1100 case SPECIAL_MOD_ASSIGN:
1101 case SPECIAL_SHL_ASSIGN:
1102 case SPECIAL_SHR_ASSIGN:
1103 case SPECIAL_OR_ASSIGN:
1104 case SPECIAL_XOR_ASSIGN:
1105 case SPECIAL_MUL_ASSIGN:
1106 case SPECIAL_DIV_ASSIGN:
1107 binop_expr = binop_expression(expr->left,
1108 op_remove_assign(expr->op),
1109 expr->right);
1110 if (get_absolute_rl(binop_expr, &rl)) {
1111 rl = cast_rl(left_type, rl);
1112 set_extra_mod(name, sym, left, alloc_estate_rl(rl));
1113 goto free;
1115 break;
1117 rl = cast_rl(left_type, alloc_rl(res_min, res_max));
1118 set_extra_mod(name, sym, left, alloc_estate_rl(rl));
1119 free:
1120 free_string(name);
1123 static struct smatch_state *increment_state(struct smatch_state *state)
1125 sval_t min = estate_min(state);
1126 sval_t max = estate_max(state);
1128 if (!estate_rl(state))
1129 return NULL;
1131 if (inside_loop())
1132 max = sval_type_max(max.type);
1134 if (!sval_is_min(min) && !sval_is_max(min))
1135 min.value++;
1136 if (!sval_is_min(max) && !sval_is_max(max))
1137 max.value++;
1138 return alloc_estate_range(min, max);
1141 static struct smatch_state *decrement_state(struct smatch_state *state)
1143 sval_t min = estate_min(state);
1144 sval_t max = estate_max(state);
1146 if (!estate_rl(state))
1147 return NULL;
1149 if (inside_loop())
1150 min = sval_type_min(min.type);
1152 if (!sval_is_min(min) && !sval_is_max(min))
1153 min.value--;
1154 if (!sval_is_min(max) && !sval_is_max(max))
1155 max.value--;
1156 return alloc_estate_range(min, max);
1159 static void clear_pointed_at_state(struct expression *expr)
1161 struct symbol *type;
1164 * ALERT: This is sort of a mess. If it's is a struct assigment like
1165 * "foo = bar;", then that's handled by smatch_struct_assignment.c.
1166 * the same thing for p++ where "p" is a struct. Most modifications
1167 * are handled by the assignment hook or the db. Smatch_extra.c doesn't
1168 * use smatch_modification.c because we have to get the ordering right
1169 * or something. So if you have p++ where p is a pointer to a standard
1170 * c type then we handle that here. What a mess.
1172 expr = strip_expr(expr);
1173 type = get_type(expr);
1174 if (!type || type->type != SYM_PTR)
1175 return;
1176 type = get_real_base_type(type);
1177 if (!type || type->type != SYM_BASETYPE)
1178 return;
1179 set_extra_expr_nomod(deref_expression(expr), alloc_estate_whole(type));
1182 static void unop_expr(struct expression *expr)
1184 struct smatch_state *state;
1186 if (expr->smatch_flags & Handled)
1187 return;
1189 switch (expr->op) {
1190 case SPECIAL_INCREMENT:
1191 state = get_state_expr(SMATCH_EXTRA, expr->unop);
1192 state = increment_state(state);
1193 if (!state)
1194 state = alloc_estate_whole(get_type(expr));
1195 set_extra_expr_mod(expr->unop, state);
1196 clear_pointed_at_state(expr->unop);
1197 break;
1198 case SPECIAL_DECREMENT:
1199 state = get_state_expr(SMATCH_EXTRA, expr->unop);
1200 state = decrement_state(state);
1201 if (!state)
1202 state = alloc_estate_whole(get_type(expr));
1203 set_extra_expr_mod(expr->unop, state);
1204 clear_pointed_at_state(expr->unop);
1205 break;
1206 default:
1207 return;
1211 static void asm_expr(struct statement *stmt)
1214 struct expression *expr;
1215 struct symbol *type;
1216 int state = 0;
1218 FOR_EACH_PTR(stmt->asm_outputs, expr) {
1219 switch (state) {
1220 case 0: /* identifier */
1221 case 1: /* constraint */
1222 state++;
1223 continue;
1224 case 2: /* expression */
1225 state = 0;
1226 type = get_type(strip_expr(expr));
1227 set_extra_expr_mod(expr, alloc_estate_whole(type));
1228 continue;
1230 } END_FOR_EACH_PTR(expr);
1233 static void check_dereference(struct expression *expr)
1235 struct smatch_state *state;
1237 if (__in_fake_assign)
1238 return;
1239 if (outside_of_function())
1240 return;
1241 state = get_extra_state(expr);
1242 if (state) {
1243 struct range_list *rl;
1245 rl = rl_intersection(estate_rl(state), valid_ptr_rl);
1246 if (rl_equiv(rl, estate_rl(state)))
1247 return;
1248 set_extra_expr_nomod(expr, alloc_estate_rl(rl));
1249 } else {
1250 set_extra_expr_nomod(expr, alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval));
1254 static void match_dereferences(struct expression *expr)
1256 if (expr->type != EXPR_PREOP)
1257 return;
1258 /* it's saying that foo[1] = bar dereferences foo[1] */
1259 if (is_array(expr))
1260 return;
1261 check_dereference(expr->unop);
1264 static void match_pointer_as_array(struct expression *expr)
1266 if (!is_array(expr))
1267 return;
1268 check_dereference(get_array_base(expr));
1271 static void find_dereferences(struct expression *expr)
1273 while (expr->type == EXPR_PREOP) {
1274 if (expr->op == '*')
1275 check_dereference(expr->unop);
1276 expr = strip_expr(expr->unop);
1280 static void set_param_dereferenced(struct expression *call, struct expression *arg, char *key, char *unused)
1282 struct symbol *sym;
1283 char *name;
1285 name = get_variable_from_key(arg, key, &sym);
1286 if (name && sym) {
1287 struct smatch_state *orig, *new;
1288 struct range_list *rl;
1290 orig = get_state(SMATCH_EXTRA, name, sym);
1291 if (orig) {
1292 rl = rl_intersection(estate_rl(orig),
1293 alloc_rl(valid_ptr_min_sval,
1294 valid_ptr_max_sval));
1295 new = alloc_estate_rl(rl);
1296 } else {
1297 new = alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval);
1300 set_extra_nomod(name, sym, NULL, new);
1302 free_string(name);
1304 find_dereferences(arg);
1307 static sval_t add_one(sval_t sval)
1309 sval.value++;
1310 return sval;
1313 static int handle_postop_inc(struct expression *left, int op, struct expression *right)
1315 struct statement *stmt;
1316 struct expression *cond;
1317 struct smatch_state *true_state, *false_state;
1318 sval_t start;
1319 sval_t limit;
1322 * If we're decrementing here then that's a canonical while count down
1323 * so it's handled already. We're only handling loops like:
1324 * i = 0;
1325 * do { ... } while (i++ < 3);
1328 if (left->type != EXPR_POSTOP || left->op != SPECIAL_INCREMENT)
1329 return 0;
1331 stmt = __cur_stmt->parent;
1332 if (!stmt)
1333 return 0;
1334 if (stmt->type == STMT_COMPOUND)
1335 stmt = stmt->parent;
1336 if (!stmt || stmt->type != STMT_ITERATOR || !stmt->iterator_post_condition)
1337 return 0;
1339 cond = strip_expr(stmt->iterator_post_condition);
1340 if (cond->type != EXPR_COMPARE || cond->op != op)
1341 return 0;
1342 if (left != strip_expr(cond->left) || right != strip_expr(cond->right))
1343 return 0;
1345 if (!get_implied_value(left->unop, &start))
1346 return 0;
1347 if (!get_implied_value(right, &limit))
1348 return 0;
1350 if (sval_cmp(start, limit) > 0)
1351 return 0;
1353 switch (op) {
1354 case '<':
1355 case SPECIAL_UNSIGNED_LT:
1356 break;
1357 case SPECIAL_LTE:
1358 case SPECIAL_UNSIGNED_LTE:
1359 limit = add_one(limit);
1360 default:
1361 return 0;
1365 true_state = alloc_estate_range(add_one(start), limit);
1366 false_state = alloc_estate_range(add_one(limit), add_one(limit));
1368 /* Currently we just discard the false state but when two passes is
1369 * implimented correctly then it will use it.
1372 set_extra_expr_true_false(left->unop, true_state, false_state);
1374 return 1;
1377 bool is_impossible_variable(struct expression *expr)
1379 struct smatch_state *state;
1381 state = get_extra_state(expr);
1382 if (state && !estate_rl(state))
1383 return true;
1384 return false;
1387 static void handle_comparison(struct symbol *type, struct expression *left, int op, struct expression *right)
1389 struct range_list *left_orig;
1390 struct range_list *left_true;
1391 struct range_list *left_false;
1392 struct range_list *right_orig;
1393 struct range_list *right_true;
1394 struct range_list *right_false;
1395 struct smatch_state *left_true_state;
1396 struct smatch_state *left_false_state;
1397 struct smatch_state *right_true_state;
1398 struct smatch_state *right_false_state;
1399 sval_t dummy, hard_max;
1400 int left_postop = 0;
1401 int right_postop = 0;
1403 if (left->op == SPECIAL_INCREMENT || left->op == SPECIAL_DECREMENT) {
1404 if (left->type == EXPR_POSTOP) {
1405 left->smatch_flags |= Handled;
1406 left_postop = left->op;
1407 if (handle_postop_inc(left, op, right))
1408 return;
1410 left = strip_parens(left->unop);
1412 while (left->type == EXPR_ASSIGNMENT)
1413 left = strip_parens(left->left);
1415 if (right->op == SPECIAL_INCREMENT || right->op == SPECIAL_DECREMENT) {
1416 if (right->type == EXPR_POSTOP) {
1417 right->smatch_flags |= Handled;
1418 right_postop = right->op;
1420 right = strip_parens(right->unop);
1423 if (is_impossible_variable(left) || is_impossible_variable(right))
1424 return;
1426 get_real_absolute_rl(left, &left_orig);
1427 left_orig = cast_rl(type, left_orig);
1429 get_real_absolute_rl(right, &right_orig);
1430 right_orig = cast_rl(type, right_orig);
1432 split_comparison_rl(left_orig, op, right_orig, &left_true, &left_false, &right_true, &right_false);
1434 left_true = rl_truncate_cast(get_type(strip_expr(left)), left_true);
1435 left_false = rl_truncate_cast(get_type(strip_expr(left)), left_false);
1436 right_true = rl_truncate_cast(get_type(strip_expr(right)), right_true);
1437 right_false = rl_truncate_cast(get_type(strip_expr(right)), right_false);
1439 if (!left_true || !left_false) {
1440 struct range_list *tmp_true, *tmp_false;
1442 split_comparison_rl(alloc_whole_rl(type), op, right_orig, &tmp_true, &tmp_false, NULL, NULL);
1443 tmp_true = rl_truncate_cast(get_type(strip_expr(left)), tmp_true);
1444 tmp_false = rl_truncate_cast(get_type(strip_expr(left)), tmp_false);
1445 if (tmp_true && tmp_false)
1446 __save_imaginary_state(left, tmp_true, tmp_false);
1449 if (!right_true || !right_false) {
1450 struct range_list *tmp_true, *tmp_false;
1452 split_comparison_rl(alloc_whole_rl(type), op, right_orig, NULL, NULL, &tmp_true, &tmp_false);
1453 tmp_true = rl_truncate_cast(get_type(strip_expr(right)), tmp_true);
1454 tmp_false = rl_truncate_cast(get_type(strip_expr(right)), tmp_false);
1455 if (tmp_true && tmp_false)
1456 __save_imaginary_state(right, tmp_true, tmp_false);
1459 left_true_state = alloc_estate_rl(left_true);
1460 left_false_state = alloc_estate_rl(left_false);
1461 right_true_state = alloc_estate_rl(right_true);
1462 right_false_state = alloc_estate_rl(right_false);
1464 switch (op) {
1465 case '<':
1466 case SPECIAL_UNSIGNED_LT:
1467 case SPECIAL_UNSIGNED_LTE:
1468 case SPECIAL_LTE:
1469 if (get_hard_max(right, &dummy))
1470 estate_set_hard_max(left_true_state);
1471 if (get_hard_max(left, &dummy))
1472 estate_set_hard_max(right_false_state);
1473 break;
1474 case '>':
1475 case SPECIAL_UNSIGNED_GT:
1476 case SPECIAL_UNSIGNED_GTE:
1477 case SPECIAL_GTE:
1478 if (get_hard_max(left, &dummy))
1479 estate_set_hard_max(right_true_state);
1480 if (get_hard_max(right, &dummy))
1481 estate_set_hard_max(left_false_state);
1482 break;
1485 switch (op) {
1486 case '<':
1487 case SPECIAL_UNSIGNED_LT:
1488 case SPECIAL_UNSIGNED_LTE:
1489 case SPECIAL_LTE:
1490 if (get_hard_max(right, &hard_max)) {
1491 if (op == '<' || op == SPECIAL_UNSIGNED_LT)
1492 hard_max.value--;
1493 estate_set_fuzzy_max(left_true_state, hard_max);
1495 if (get_implied_value(right, &hard_max)) {
1496 if (op == SPECIAL_UNSIGNED_LTE ||
1497 op == SPECIAL_LTE)
1498 hard_max.value++;
1499 estate_set_fuzzy_max(left_false_state, hard_max);
1501 if (get_hard_max(left, &hard_max)) {
1502 if (op == SPECIAL_UNSIGNED_LTE ||
1503 op == SPECIAL_LTE)
1504 hard_max.value--;
1505 estate_set_fuzzy_max(right_false_state, hard_max);
1507 if (get_implied_value(left, &hard_max)) {
1508 if (op == '<' || op == SPECIAL_UNSIGNED_LT)
1509 hard_max.value++;
1510 estate_set_fuzzy_max(right_true_state, hard_max);
1512 break;
1513 case '>':
1514 case SPECIAL_UNSIGNED_GT:
1515 case SPECIAL_UNSIGNED_GTE:
1516 case SPECIAL_GTE:
1517 if (get_hard_max(left, &hard_max)) {
1518 if (op == '>' || op == SPECIAL_UNSIGNED_GT)
1519 hard_max.value--;
1520 estate_set_fuzzy_max(right_true_state, hard_max);
1522 if (get_implied_value(left, &hard_max)) {
1523 if (op == SPECIAL_UNSIGNED_GTE ||
1524 op == SPECIAL_GTE)
1525 hard_max.value++;
1526 estate_set_fuzzy_max(right_false_state, hard_max);
1528 if (get_hard_max(right, &hard_max)) {
1529 if (op == SPECIAL_UNSIGNED_LTE ||
1530 op == SPECIAL_LTE)
1531 hard_max.value--;
1532 estate_set_fuzzy_max(left_false_state, hard_max);
1534 if (get_implied_value(right, &hard_max)) {
1535 if (op == '>' ||
1536 op == SPECIAL_UNSIGNED_GT)
1537 hard_max.value++;
1538 estate_set_fuzzy_max(left_true_state, hard_max);
1540 break;
1541 case SPECIAL_EQUAL:
1542 if (get_hard_max(left, &hard_max))
1543 estate_set_fuzzy_max(right_true_state, hard_max);
1544 if (get_hard_max(right, &hard_max))
1545 estate_set_fuzzy_max(left_true_state, hard_max);
1546 break;
1549 if (get_hard_max(left, &hard_max)) {
1550 estate_set_hard_max(left_true_state);
1551 estate_set_hard_max(left_false_state);
1553 if (get_hard_max(right, &hard_max)) {
1554 estate_set_hard_max(right_true_state);
1555 estate_set_hard_max(right_false_state);
1558 if (left_postop == SPECIAL_INCREMENT) {
1559 left_true_state = increment_state(left_true_state);
1560 left_false_state = increment_state(left_false_state);
1562 if (left_postop == SPECIAL_DECREMENT) {
1563 left_true_state = decrement_state(left_true_state);
1564 left_false_state = decrement_state(left_false_state);
1566 if (right_postop == SPECIAL_INCREMENT) {
1567 right_true_state = increment_state(right_true_state);
1568 right_false_state = increment_state(right_false_state);
1570 if (right_postop == SPECIAL_DECREMENT) {
1571 right_true_state = decrement_state(right_true_state);
1572 right_false_state = decrement_state(right_false_state);
1575 if (estate_rl(left_true_state) && estates_equiv(left_true_state, left_false_state)) {
1576 left_true_state = NULL;
1577 left_false_state = NULL;
1580 if (estate_rl(right_true_state) && estates_equiv(right_true_state, right_false_state)) {
1581 right_true_state = NULL;
1582 right_false_state = NULL;
1585 /* Don't introduce new states for known true/false conditions */
1586 if (rl_equiv(left_orig, estate_rl(left_true_state)))
1587 left_true_state = NULL;
1588 if (rl_equiv(left_orig, estate_rl(left_false_state)))
1589 left_false_state = NULL;
1590 if (rl_equiv(right_orig, estate_rl(right_true_state)))
1591 right_true_state = NULL;
1592 if (rl_equiv(right_orig, estate_rl(right_false_state)))
1593 right_false_state = NULL;
1595 set_extra_expr_true_false(left, left_true_state, left_false_state);
1596 set_extra_expr_true_false(right, right_true_state, right_false_state);
1599 static int is_simple_math(struct expression *expr)
1601 if (!expr)
1602 return 0;
1603 if (expr->type != EXPR_BINOP)
1604 return 0;
1605 switch (expr->op) {
1606 case '+':
1607 case '-':
1608 case '*':
1609 return 1;
1611 return 0;
1614 static void move_known_values(struct expression **left_p, struct expression **right_p)
1616 struct expression *left = *left_p;
1617 struct expression *right = *right_p;
1618 sval_t sval, dummy;
1620 if (get_implied_value(left, &sval)) {
1621 if (!is_simple_math(right))
1622 return;
1623 if (get_implied_value(right, &dummy))
1624 return;
1625 if (right->op == '*') {
1626 sval_t divisor;
1628 if (!get_value(right->right, &divisor))
1629 return;
1630 if (divisor.value == 0)
1631 return;
1632 *left_p = binop_expression(left, invert_op(right->op), right->right);
1633 *right_p = right->left;
1634 return;
1636 if (right->op == '+' && get_value(right->left, &sval)) {
1637 *left_p = binop_expression(left, invert_op(right->op), right->left);
1638 *right_p = right->right;
1639 return;
1641 if (get_value(right->right, &sval)) {
1642 *left_p = binop_expression(left, invert_op(right->op), right->right);
1643 *right_p = right->left;
1644 return;
1646 return;
1648 if (get_implied_value(right, &sval)) {
1649 if (!is_simple_math(left))
1650 return;
1651 if (get_implied_value(left, &dummy))
1652 return;
1653 if (left->op == '*') {
1654 sval_t divisor;
1656 if (!get_value(left->right, &divisor))
1657 return;
1658 if (divisor.value == 0)
1659 return;
1660 *right_p = binop_expression(right, invert_op(left->op), left->right);
1661 *left_p = left->left;
1662 return;
1664 if (left->op == '+' && get_value(left->left, &sval)) {
1665 *right_p = binop_expression(right, invert_op(left->op), left->left);
1666 *left_p = left->right;
1667 return;
1670 if (get_value(left->right, &sval)) {
1671 *right_p = binop_expression(right, invert_op(left->op), left->right);
1672 *left_p = left->left;
1673 return;
1675 return;
1680 * The reason for do_simple_algebra() is to solve things like:
1681 * if (foo > 66 || foo + bar > 64) {
1682 * "foo" is not really a known variable so it won't be handled by
1683 * move_known_variables() but it's a super common idiom.
1686 static int do_simple_algebra(struct expression **left_p, struct expression **right_p)
1688 struct expression *left = *left_p;
1689 struct expression *right = *right_p;
1690 struct range_list *rl;
1691 sval_t tmp;
1693 if (left->type != EXPR_BINOP || left->op != '+')
1694 return 0;
1695 if (can_integer_overflow(get_type(left), left))
1696 return 0;
1697 if (!get_implied_value(right, &tmp))
1698 return 0;
1700 if (!get_implied_value(left->left, &tmp) &&
1701 get_implied_rl(left->left, &rl) &&
1702 !is_whole_rl(rl)) {
1703 *right_p = binop_expression(right, '-', left->left);
1704 *left_p = left->right;
1705 return 1;
1707 if (!get_implied_value(left->right, &tmp) &&
1708 get_implied_rl(left->right, &rl) &&
1709 !is_whole_rl(rl)) {
1710 *right_p = binop_expression(right, '-', left->right);
1711 *left_p = left->left;
1712 return 1;
1715 return 0;
1718 static int match_func_comparison(struct expression *expr)
1720 struct expression *left = strip_expr(expr->left);
1721 struct expression *right = strip_expr(expr->right);
1722 sval_t sval;
1725 * fixme: think about this harder. We should always be trying to limit
1726 * the non-call side as well. If we can't determine the limitter does
1727 * that mean we aren't querying the database and are missing important
1728 * information?
1731 if (left->type == EXPR_CALL) {
1732 if (get_implied_value(left, &sval)) {
1733 handle_comparison(get_type(expr), left, expr->op, right);
1734 return 1;
1736 function_comparison(left, expr->op, right);
1737 return 1;
1740 if (right->type == EXPR_CALL) {
1741 if (get_implied_value(right, &sval)) {
1742 handle_comparison(get_type(expr), left, expr->op, right);
1743 return 1;
1745 function_comparison(left, expr->op, right);
1746 return 1;
1749 return 0;
1752 /* Handle conditions like "if (foo + bar < foo) {" */
1753 static int handle_integer_overflow_test(struct expression *expr)
1755 struct expression *left, *right;
1756 struct symbol *type;
1757 sval_t left_min, right_min, min, max;
1759 if (expr->op != '<' && expr->op != SPECIAL_UNSIGNED_LT)
1760 return 0;
1762 left = strip_parens(expr->left);
1763 right = strip_parens(expr->right);
1765 if (left->op != '+')
1766 return 0;
1768 type = get_type(expr);
1769 if (!type)
1770 return 0;
1771 if (type_positive_bits(type) == 32) {
1772 max.type = &uint_ctype;
1773 max.uvalue = (unsigned int)-1;
1774 } else if (type_positive_bits(type) == 64) {
1775 max.type = &ulong_ctype;
1776 max.value = (unsigned long long)-1;
1777 } else {
1778 return 0;
1781 if (!expr_equiv(left->left, right) && !expr_equiv(left->right, right))
1782 return 0;
1784 get_absolute_min(left->left, &left_min);
1785 get_absolute_min(left->right, &right_min);
1786 min = sval_binop(left_min, '+', right_min);
1788 set_extra_chunk_true_false(left, NULL, alloc_estate_range(min, max));
1789 return 1;
1792 static void match_comparison(struct expression *expr)
1794 struct expression *left_orig = strip_parens(expr->left);
1795 struct expression *right_orig = strip_parens(expr->right);
1796 struct expression *left, *right, *tmp;
1797 struct expression *prev;
1798 struct symbol *type;
1799 int redo, count;
1801 if (match_func_comparison(expr))
1802 return;
1804 type = get_type(expr);
1805 if (!type)
1806 type = &llong_ctype;
1808 if (handle_integer_overflow_test(expr))
1809 return;
1811 left = left_orig;
1812 right = right_orig;
1813 move_known_values(&left, &right);
1814 handle_comparison(type, left, expr->op, right);
1816 left = left_orig;
1817 right = right_orig;
1818 if (do_simple_algebra(&left, &right))
1819 handle_comparison(type, left, expr->op, right);
1821 prev = get_assigned_expr(left_orig);
1822 if (is_simple_math(prev) && has_variable(prev, left_orig) == 0) {
1823 left = prev;
1824 right = right_orig;
1825 move_known_values(&left, &right);
1826 handle_comparison(type, left, expr->op, right);
1829 prev = get_assigned_expr(right_orig);
1830 if (is_simple_math(prev) && has_variable(prev, right_orig) == 0) {
1831 left = left_orig;
1832 right = prev;
1833 move_known_values(&left, &right);
1834 handle_comparison(type, left, expr->op, right);
1837 redo = 0;
1838 left = left_orig;
1839 right = right_orig;
1840 if (get_last_expr_from_expression_stmt(left_orig)) {
1841 left = get_last_expr_from_expression_stmt(left_orig);
1842 redo = 1;
1844 if (get_last_expr_from_expression_stmt(right_orig)) {
1845 right = get_last_expr_from_expression_stmt(right_orig);
1846 redo = 1;
1849 if (!redo)
1850 return;
1852 count = 0;
1853 while ((tmp = get_assigned_expr(left))) {
1854 if (count++ > 3)
1855 break;
1856 left = strip_expr(tmp);
1858 count = 0;
1859 while ((tmp = get_assigned_expr(right))) {
1860 if (count++ > 3)
1861 break;
1862 right = strip_expr(tmp);
1865 handle_comparison(type, left, expr->op, right);
1868 static sval_t get_high_mask(sval_t known)
1870 sval_t ret;
1871 int i;
1873 ret = known;
1874 ret.value = 0;
1876 for (i = type_bits(known.type) - 1; i >= 0; i--) {
1877 if (known.uvalue & (1ULL << i))
1878 ret.uvalue |= (1ULL << i);
1879 else
1880 return ret;
1883 return ret;
1886 static void handle_AND_op(struct expression *var, sval_t known)
1888 struct range_list *orig_rl;
1889 struct range_list *true_rl = NULL;
1890 struct range_list *false_rl = NULL;
1891 int bit;
1892 sval_t low_mask = known;
1893 sval_t high_mask;
1894 sval_t max;
1896 get_absolute_rl(var, &orig_rl);
1898 if (known.value > 0) {
1899 bit = ffsll(known.value) - 1;
1900 low_mask.uvalue = (1ULL << bit) - 1;
1901 true_rl = remove_range(orig_rl, sval_type_val(known.type, 0), low_mask);
1903 high_mask = get_high_mask(known);
1904 if (high_mask.value) {
1905 bit = ffsll(high_mask.value) - 1;
1906 low_mask.uvalue = (1ULL << bit) - 1;
1908 false_rl = orig_rl;
1909 if (sval_is_negative(rl_min(orig_rl)))
1910 false_rl = remove_range(false_rl, sval_type_min(known.type), sval_type_val(known.type, -1));
1911 false_rl = remove_range(false_rl, low_mask, sval_type_max(known.type));
1912 if (type_signed(high_mask.type) && type_unsigned(rl_type(false_rl))) {
1913 false_rl = remove_range(false_rl,
1914 sval_type_val(rl_type(false_rl), sval_type_max(known.type).uvalue),
1915 sval_type_val(rl_type(false_rl), -1));
1917 } else if (known.value == 1 &&
1918 get_hard_max(var, &max) &&
1919 sval_cmp(max, rl_max(orig_rl)) == 0 &&
1920 max.value & 1) {
1921 false_rl = remove_range(orig_rl, max, max);
1923 set_extra_expr_true_false(var,
1924 true_rl ? alloc_estate_rl(true_rl) : NULL,
1925 false_rl ? alloc_estate_rl(false_rl) : NULL);
1928 static void handle_AND_condition(struct expression *expr)
1930 sval_t known;
1932 if (get_implied_value(expr->left, &known))
1933 handle_AND_op(expr->right, known);
1934 else if (get_implied_value(expr->right, &known))
1935 handle_AND_op(expr->left, known);
1938 static void handle_MOD_condition(struct expression *expr)
1940 struct range_list *orig_rl;
1941 struct range_list *true_rl;
1942 struct range_list *false_rl = NULL;
1943 sval_t right;
1944 sval_t zero = {
1945 .value = 0,
1948 if (!get_implied_value(expr->right, &right) || right.value == 0)
1949 return;
1950 get_absolute_rl(expr->left, &orig_rl);
1952 zero.type = rl_type(orig_rl);
1954 /* We're basically dorking around the min and max here */
1955 true_rl = remove_range(orig_rl, zero, zero);
1956 if (!sval_is_max(rl_max(true_rl)) &&
1957 !(rl_max(true_rl).value % right.value))
1958 true_rl = remove_range(true_rl, rl_max(true_rl), rl_max(true_rl));
1960 if (rl_equiv(true_rl, orig_rl))
1961 true_rl = NULL;
1963 if (sval_is_positive(rl_min(orig_rl)) &&
1964 (rl_max(orig_rl).value - rl_min(orig_rl).value) / right.value < 5) {
1965 sval_t add;
1966 int i;
1968 add = rl_min(orig_rl);
1969 add.value += right.value - (add.value % right.value);
1970 add.value -= right.value;
1972 for (i = 0; i < 5; i++) {
1973 add.value += right.value;
1974 if (add.value > rl_max(orig_rl).value)
1975 break;
1976 add_range(&false_rl, add, add);
1978 } else {
1979 if (rl_min(orig_rl).uvalue != 0 &&
1980 rl_min(orig_rl).uvalue < right.uvalue) {
1981 sval_t chop = right;
1982 chop.value--;
1983 false_rl = remove_range(orig_rl, zero, chop);
1986 if (!sval_is_max(rl_max(orig_rl)) &&
1987 (rl_max(orig_rl).value % right.value)) {
1988 sval_t chop = rl_max(orig_rl);
1989 chop.value -= chop.value % right.value;
1990 chop.value++;
1991 if (!false_rl)
1992 false_rl = clone_rl(orig_rl);
1993 false_rl = remove_range(false_rl, chop, rl_max(orig_rl));
1997 set_extra_expr_true_false(expr->left,
1998 true_rl ? alloc_estate_rl(true_rl) : NULL,
1999 false_rl ? alloc_estate_rl(false_rl) : NULL);
2002 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
2003 void __extra_match_condition(struct expression *expr)
2005 expr = strip_expr(expr);
2006 switch (expr->type) {
2007 case EXPR_CALL:
2008 function_comparison(expr, SPECIAL_NOTEQUAL, zero_expr());
2009 return;
2010 case EXPR_PREOP:
2011 case EXPR_SYMBOL:
2012 case EXPR_DEREF:
2013 handle_comparison(get_type(expr), expr, SPECIAL_NOTEQUAL, zero_expr());
2014 return;
2015 case EXPR_COMPARE:
2016 match_comparison(expr);
2017 return;
2018 case EXPR_ASSIGNMENT:
2019 __extra_match_condition(expr->left);
2020 return;
2021 case EXPR_BINOP:
2022 if (expr->op == '&')
2023 handle_AND_condition(expr);
2024 if (expr->op == '%')
2025 handle_MOD_condition(expr);
2026 return;
2030 static void assume_indexes_are_valid(struct expression *expr)
2032 struct expression *array_expr;
2033 int array_size;
2034 struct expression *offset;
2035 struct symbol *offset_type;
2036 struct range_list *rl_before;
2037 struct range_list *rl_after;
2038 struct range_list *filter = NULL;
2039 sval_t size;
2041 expr = strip_expr(expr);
2042 if (!is_array(expr))
2043 return;
2045 offset = get_array_offset(expr);
2046 offset_type = get_type(offset);
2047 if (offset_type && type_signed(offset_type)) {
2048 filter = alloc_rl(sval_type_min(offset_type),
2049 sval_type_val(offset_type, -1));
2052 array_expr = get_array_base(expr);
2053 array_size = get_real_array_size(array_expr);
2054 if (array_size > 1) {
2055 size = sval_type_val(offset_type, array_size);
2056 add_range(&filter, size, sval_type_max(offset_type));
2059 if (!filter)
2060 return;
2061 get_absolute_rl(offset, &rl_before);
2062 rl_after = rl_filter(rl_before, filter);
2063 if (rl_equiv(rl_before, rl_after))
2064 return;
2065 set_extra_expr_nomod(offset, alloc_estate_rl(rl_after));
2068 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
2069 int implied_not_equal(struct expression *expr, long long val)
2071 return !possibly_false(expr, SPECIAL_NOTEQUAL, value_expr(val));
2074 int implied_not_equal_name_sym(char *name, struct symbol *sym, long long val)
2076 struct smatch_state *estate;
2078 estate = get_state(SMATCH_EXTRA, name, sym);
2079 if (!estate)
2080 return 0;
2081 if (!rl_has_sval(estate_rl(estate), sval_type_val(estate_type(estate), 0)))
2082 return 1;
2083 return 0;
2086 int parent_is_null_var_sym(const char *name, struct symbol *sym)
2088 char buf[256];
2089 char *start;
2090 char *end;
2091 struct smatch_state *state;
2093 strncpy(buf, name, sizeof(buf) - 1);
2094 buf[sizeof(buf) - 1] = '\0';
2096 start = &buf[0];
2097 while (*start == '*') {
2098 start++;
2099 state = get_state(SMATCH_EXTRA, start, sym);
2100 if (!state)
2101 continue;
2102 if (!estate_rl(state))
2103 return 1;
2104 if (estate_min(state).value == 0 &&
2105 estate_max(state).value == 0)
2106 return 1;
2109 start = &buf[0];
2110 while (*start == '&')
2111 start++;
2113 while ((end = strrchr(start, '-'))) {
2114 *end = '\0';
2115 state = __get_state(SMATCH_EXTRA, start, sym);
2116 if (!state)
2117 continue;
2118 if (estate_min(state).value == 0 &&
2119 estate_max(state).value == 0)
2120 return 1;
2122 return 0;
2125 int parent_is_null(struct expression *expr)
2127 struct symbol *sym;
2128 char *var;
2129 int ret = 0;
2131 expr = strip_expr(expr);
2132 var = expr_to_var_sym(expr, &sym);
2133 if (!var || !sym)
2134 goto free;
2135 ret = parent_is_null_var_sym(var, sym);
2136 free:
2137 free_string(var);
2138 return ret;
2141 static int param_used_callback(void *found, int argc, char **argv, char **azColName)
2143 *(int *)found = 1;
2144 return 0;
2147 static int is_kzalloc_info(struct sm_state *sm)
2149 sval_t sval;
2152 * kzalloc() information is treated as special because so there is just
2153 * a lot of stuff initialized to zero and it makes building the database
2154 * take hours and hours.
2156 * In theory, we should just remove this line and not pass any unused
2157 * information, but I'm not sure enough that this code works so I want
2158 * to hold off on that for now.
2160 if (!estate_get_single_value(sm->state, &sval))
2161 return 0;
2162 if (sval.value != 0)
2163 return 0;
2164 return 1;
2167 static int is_really_long(struct sm_state *sm)
2169 const char *p;
2170 int cnt = 0;
2172 p = sm->name;
2173 while ((p = strstr(p, "->"))) {
2174 p += 2;
2175 cnt++;
2178 if (cnt < 3 ||
2179 strlen(sm->name) < 40)
2180 return 0;
2181 return 1;
2184 static int filter_unused_param_value_info(struct expression *call, int param, char *printed_name, struct sm_state *sm)
2186 int found = 0;
2188 /* for function pointers assume everything is used */
2189 if (call->fn->type != EXPR_SYMBOL)
2190 return 0;
2193 * This is to handle __builtin_mul_overflow(). In an ideal world we
2194 * would only need this for invalid code.
2197 if (!call->fn->symbol)
2198 return 0;
2200 if (!is_kzalloc_info(sm) && !is_really_long(sm))
2201 return 0;
2203 run_sql(&param_used_callback, &found,
2204 "select * from return_implies where %s and type = %d and parameter = %d and key = '%s';",
2205 get_static_filter(call->fn->symbol), PARAM_USED, param, printed_name);
2206 if (found)
2207 return 0;
2209 /* If the database is not built yet, then assume everything is used */
2210 run_sql(&param_used_callback, &found,
2211 "select * from return_implies where %s and type = %d;",
2212 get_static_filter(call->fn->symbol), PARAM_USED);
2213 if (!found)
2214 return 0;
2216 return 1;
2219 struct range_list *intersect_with_real_abs_var_sym(const char *name, struct symbol *sym, struct range_list *start)
2221 struct smatch_state *state;
2224 * Here is the difference between implied value and real absolute, say
2225 * you have:
2227 * int a = (u8)x;
2229 * Then you know that a is 0-255. That's real absolute. But you don't
2230 * know for sure that it actually goes up to 255. So it's not implied.
2231 * Implied indicates a degree of certainty.
2233 * But then say you cap "a" at 8. That means you know it goes up to
2234 * 8. So now the implied value is s32min-8. But you can combine it
2235 * with the real absolute to say that actually it's 0-8.
2237 * We are combining it here. But now that I think about it, this is
2238 * probably not the ideal place to combine it because it should proably
2239 * be done earlier. Oh well, this is an improvement on what was there
2240 * before so I'm going to commit this code.
2244 state = get_real_absolute_state_var_sym(name, sym);
2245 if (!state || !estate_rl(state))
2246 return start;
2248 return rl_intersection(estate_rl(state), start);
2251 struct range_list *intersect_with_real_abs_expr(struct expression *expr, struct range_list *start)
2253 struct smatch_state *state;
2254 struct range_list *abs_rl;
2256 state = get_real_absolute_state(expr);
2257 if (!state || !estate_rl(state))
2258 return start;
2260 abs_rl = cast_rl(rl_type(start), estate_rl(state));
2261 return rl_intersection(abs_rl, start);
2264 static void struct_member_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
2266 struct range_list *rl;
2268 if (estate_is_whole(sm->state))
2269 return;
2270 if (filter_unused_param_value_info(call, param, printed_name, sm))
2271 return;
2272 rl = estate_rl(sm->state);
2273 rl = intersect_with_real_abs_var_sym(sm->name, sm->sym, rl);
2274 sql_insert_caller_info(call, PARAM_VALUE, param, printed_name, show_rl(rl));
2275 if (estate_has_fuzzy_max(sm->state))
2276 sql_insert_caller_info(call, FUZZY_MAX, param, printed_name,
2277 sval_to_str(estate_get_fuzzy_max(sm->state)));
2280 static void returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
2282 struct symbol *returned_sym;
2283 char *returned_name;
2284 struct sm_state *sm;
2285 char *compare_str;
2286 char name_buf[256];
2287 char val_buf[256];
2288 int len;
2290 // FIXME handle *$
2292 if (!is_pointer(expr))
2293 return;
2295 returned_name = expr_to_var_sym(expr, &returned_sym);
2296 if (!returned_name || !returned_sym)
2297 goto free;
2298 len = strlen(returned_name);
2300 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
2301 if (!estate_rl(sm->state))
2302 continue;
2303 if (returned_sym != sm->sym)
2304 continue;
2305 if (strncmp(returned_name, sm->name, len) != 0)
2306 continue;
2307 if (sm->name[len] != '-')
2308 continue;
2310 snprintf(name_buf, sizeof(name_buf), "$%s", sm->name + len);
2312 compare_str = name_sym_to_param_comparison(sm->name, sm->sym);
2313 if (!compare_str && estate_is_whole(sm->state))
2314 continue;
2315 snprintf(val_buf, sizeof(val_buf), "%s%s", sm->state->name, compare_str ?: "");
2317 sql_insert_return_states(return_id, return_ranges, PARAM_VALUE,
2318 -1, name_buf, val_buf);
2319 } END_FOR_EACH_SM(sm);
2321 free:
2322 free_string(returned_name);
2325 static void db_limited_before(void)
2327 unmatched_stree = clone_stree(__get_cur_stree());
2330 static void db_limited_after(void)
2332 free_stree(&unmatched_stree);
2335 static int rl_fits_in_type(struct range_list *rl, struct symbol *type)
2337 if (type_bits(rl_type(rl)) <= type_bits(type))
2338 return 1;
2339 if (sval_cmp(rl_max(rl), sval_type_max(type)) > 0)
2340 return 0;
2341 if (sval_is_negative(rl_min(rl)) &&
2342 sval_cmp(rl_min(rl), sval_type_min(type)) < 0)
2343 return 0;
2344 return 1;
2347 static int basically_the_same(struct range_list *orig, struct range_list *new)
2349 if (rl_equiv(orig, new))
2350 return 1;
2353 * The whole range is essentially the same as 0,4096-27777777777 so
2354 * don't overwrite the implications just to store that.
2357 if (rl_type(orig)->type == SYM_PTR &&
2358 is_whole_rl(orig) &&
2359 rl_min(new).value == 0 &&
2360 rl_max(new).value == valid_ptr_max)
2361 return 1;
2362 return 0;
2365 static void db_param_limit_binops(struct expression *arg, char *key, struct range_list *rl)
2367 struct range_list *left_rl;
2368 sval_t zero = { .type = rl_type(rl), };
2369 sval_t sval;
2371 if (arg->op != '*')
2372 return;
2373 if (!get_implied_value(arg->right, &sval))
2374 return;
2375 if (can_integer_overflow(get_type(arg), arg))
2376 return;
2378 left_rl = rl_binop(rl, '/', alloc_rl(sval, sval));
2379 if (!rl_has_sval(rl, zero))
2380 left_rl = remove_range(left_rl, zero, zero);
2382 set_extra_expr_nomod(arg->left, alloc_estate_rl(left_rl));
2385 static void db_param_limit_filter(struct expression *expr, int param, char *key, char *value, enum info_type op)
2387 struct expression *arg;
2388 char *name;
2389 struct symbol *sym;
2390 struct var_sym_list *vsl = NULL;
2391 struct sm_state *sm;
2392 struct symbol *compare_type, *var_type;
2393 struct range_list *rl;
2394 struct range_list *limit;
2395 struct range_list *new;
2396 char *tmp_name;
2397 struct symbol *tmp_sym;
2399 while (expr->type == EXPR_ASSIGNMENT)
2400 expr = strip_expr(expr->right);
2401 if (expr->type != EXPR_CALL)
2402 return;
2404 arg = get_argument_from_call_expr(expr->args, param);
2405 if (!arg)
2406 return;
2408 name = get_chunk_from_key(arg, key, &sym, &vsl);
2409 if (!name)
2410 return;
2411 if (op != PARAM_LIMIT && !sym)
2412 goto free;
2414 if (strcmp(key, "$") == 0)
2415 compare_type = get_arg_type(expr->fn, param);
2416 else
2417 compare_type = get_member_type_from_key(arg, key);
2419 sm = get_sm_state(SMATCH_EXTRA, name, sym);
2420 if (sm)
2421 rl = estate_rl(sm->state);
2422 else
2423 rl = alloc_whole_rl(compare_type);
2425 if (op == PARAM_LIMIT && !rl_fits_in_type(rl, compare_type))
2426 goto free;
2428 call_results_to_rl(expr, compare_type, value, &limit);
2429 new = rl_intersection(rl, limit);
2431 var_type = get_member_type_from_key(arg, key);
2432 new = cast_rl(var_type, new);
2434 /* We want to preserve the implications here */
2435 if (sm && basically_the_same(estate_rl(sm->state), new))
2436 goto free;
2437 tmp_name = map_long_to_short_name_sym(name, sym, &tmp_sym);
2438 if (tmp_name && tmp_sym) {
2439 free_string(name);
2440 name = tmp_name;
2441 sym = tmp_sym;
2444 if (op == PARAM_LIMIT)
2445 set_extra_nomod_vsl(name, sym, vsl, NULL, alloc_estate_rl(new));
2446 else
2447 set_extra_mod(name, sym, NULL, alloc_estate_rl(new));
2449 if (op == PARAM_LIMIT && arg->type == EXPR_BINOP)
2450 db_param_limit_binops(arg, key, new);
2451 free:
2452 free_string(name);
2455 static void db_param_limit(struct expression *expr, int param, char *key, char *value)
2457 db_param_limit_filter(expr, param, key, value, PARAM_LIMIT);
2460 static void db_param_filter(struct expression *expr, int param, char *key, char *value)
2462 db_param_limit_filter(expr, param, key, value, PARAM_FILTER);
2465 static void db_param_add_set(struct expression *expr, int param, char *key, char *value, enum info_type op)
2467 struct expression *arg;
2468 char *name, *tmp_name;
2469 struct symbol *sym, *tmp_sym;
2470 struct symbol *param_type, *arg_type;
2471 struct smatch_state *state;
2472 struct range_list *new = NULL;
2473 struct range_list *added = NULL;
2475 while (expr->type == EXPR_ASSIGNMENT)
2476 expr = strip_expr(expr->right);
2477 if (expr->type != EXPR_CALL)
2478 return;
2480 arg = get_argument_from_call_expr(expr->args, param);
2481 if (!arg)
2482 return;
2484 arg_type = get_arg_type_from_key(expr->fn, param, arg, key);
2485 param_type = get_member_type_from_key(arg, key);
2486 name = get_variable_from_key(arg, key, &sym);
2487 if (!name || !sym)
2488 goto free;
2490 state = get_state(SMATCH_EXTRA, name, sym);
2491 if (state)
2492 new = estate_rl(state);
2494 call_results_to_rl(expr, arg_type, value, &added);
2495 added = cast_rl(param_type, added);
2496 if (op == PARAM_SET)
2497 new = added;
2498 else
2499 new = rl_union(new, added);
2501 tmp_name = map_long_to_short_name_sym_nostack(name, sym, &tmp_sym);
2502 if (tmp_name && tmp_sym) {
2503 free_string(name);
2504 name = tmp_name;
2505 sym = tmp_sym;
2507 set_extra_mod(name, sym, NULL, alloc_estate_rl(new));
2508 free:
2509 free_string(name);
2512 static void db_param_add(struct expression *expr, int param, char *key, char *value)
2514 in_param_set = true;
2515 db_param_add_set(expr, param, key, value, PARAM_ADD);
2516 in_param_set = false;
2519 static void db_param_set(struct expression *expr, int param, char *key, char *value)
2521 in_param_set = true;
2522 db_param_add_set(expr, param, key, value, PARAM_SET);
2523 in_param_set = false;
2526 static void match_lost_param(struct expression *call, int param)
2528 struct expression *arg;
2530 if (is_const_param(call->fn, param))
2531 return;
2533 arg = get_argument_from_call_expr(call->args, param);
2534 if (!arg)
2535 return;
2537 arg = strip_expr(arg);
2538 if (arg->type == EXPR_PREOP && arg->op == '&')
2539 set_extra_expr_mod(arg->unop, alloc_estate_whole(get_type(arg->unop)));
2540 else
2541 ; /* if pointer then set struct members, maybe?*/
2544 static void db_param_value(struct expression *expr, int param, char *key, char *value)
2546 struct expression *call;
2547 char *name;
2548 struct symbol *sym;
2549 struct symbol *type;
2550 struct range_list *rl = NULL;
2552 if (param != -1)
2553 return;
2555 call = expr;
2556 while (call->type == EXPR_ASSIGNMENT)
2557 call = strip_expr(call->right);
2558 if (call->type != EXPR_CALL)
2559 return;
2561 type = get_member_type_from_key(expr->left, key);
2562 name = get_variable_from_key(expr->left, key, &sym);
2563 if (!name || !sym)
2564 goto free;
2566 call_results_to_rl(call, type, value, &rl);
2568 set_extra_mod(name, sym, NULL, alloc_estate_rl(rl));
2569 free:
2570 free_string(name);
2573 static void match_call_info(struct expression *expr)
2575 struct smatch_state *state;
2576 struct range_list *rl = NULL;
2577 struct expression *arg;
2578 struct symbol *type;
2579 int i = 0;
2581 FOR_EACH_PTR(expr->args, arg) {
2582 type = get_arg_type(expr->fn, i);
2584 get_absolute_rl(arg, &rl);
2585 rl = cast_rl(type, rl);
2587 if (!is_whole_rl(rl)) {
2588 rl = intersect_with_real_abs_expr(arg, rl);
2589 sql_insert_caller_info(expr, PARAM_VALUE, i, "$", show_rl(rl));
2591 state = get_state_expr(SMATCH_EXTRA, arg);
2592 if (estate_has_fuzzy_max(state)) {
2593 sql_insert_caller_info(expr, FUZZY_MAX, i, "$",
2594 sval_to_str(estate_get_fuzzy_max(state)));
2596 i++;
2597 } END_FOR_EACH_PTR(arg);
2600 static void set_param_value(const char *name, struct symbol *sym, char *key, char *value)
2602 struct range_list *rl = NULL;
2603 struct smatch_state *state;
2604 struct symbol *type;
2605 char fullname[256];
2606 sval_t dummy;
2608 if (strcmp(key, "*$") == 0)
2609 snprintf(fullname, sizeof(fullname), "*%s", name);
2610 else if (strncmp(key, "$", 1) == 0)
2611 snprintf(fullname, 256, "%s%s", name, key + 1);
2612 else
2613 return;
2615 type = get_member_type_from_key(symbol_expression(sym), key);
2616 str_to_rl(type, value, &rl);
2617 state = alloc_estate_rl(rl);
2618 if (estate_get_single_value(state, &dummy))
2619 estate_set_hard_max(state);
2620 set_state(SMATCH_EXTRA, fullname, sym, state);
2623 static void set_param_hard_max(const char *name, struct symbol *sym, char *key, char *value)
2625 struct range_list *rl = NULL;
2626 struct smatch_state *state;
2627 struct symbol *type;
2628 char fullname[256];
2629 sval_t max;
2631 if (strcmp(key, "*$") == 0)
2632 snprintf(fullname, sizeof(fullname), "*%s", name);
2633 else if (strncmp(key, "$", 1) == 0)
2634 snprintf(fullname, 256, "%s%s", name, key + 1);
2635 else
2636 return;
2638 state = get_state(SMATCH_EXTRA, fullname, sym);
2639 if (!state)
2640 return;
2641 type = get_member_type_from_key(symbol_expression(sym), key);
2642 str_to_rl(type, value, &rl);
2643 if (!rl_to_sval(rl, &max))
2644 return;
2645 estate_set_fuzzy_max(state, max);
2648 struct sm_state *get_extra_sm_state(struct expression *expr)
2650 char *name;
2651 struct symbol *sym;
2652 struct sm_state *ret = NULL;
2654 name = expr_to_known_chunk_sym(expr, &sym);
2655 if (!name)
2656 goto free;
2658 ret = get_sm_state(SMATCH_EXTRA, name, sym);
2659 free:
2660 free_string(name);
2661 return ret;
2664 struct smatch_state *get_extra_state(struct expression *expr)
2666 struct sm_state *sm;
2668 sm = get_extra_sm_state(expr);
2669 if (!sm)
2670 return NULL;
2671 return sm->state;
2674 void register_smatch_extra(int id)
2676 my_id = id;
2678 add_merge_hook(my_id, &merge_estates);
2679 add_unmatched_state_hook(my_id, &unmatched_state);
2680 select_caller_info_hook(set_param_value, PARAM_VALUE);
2681 select_caller_info_hook(set_param_hard_max, FUZZY_MAX);
2682 select_return_states_before(&db_limited_before);
2683 select_return_states_hook(PARAM_LIMIT, &db_param_limit);
2684 select_return_states_hook(PARAM_FILTER, &db_param_filter);
2685 select_return_states_hook(PARAM_ADD, &db_param_add);
2686 select_return_states_hook(PARAM_SET, &db_param_set);
2687 add_lost_param_hook(&match_lost_param);
2688 select_return_states_hook(PARAM_VALUE, &db_param_value);
2689 select_return_states_after(&db_limited_after);
2692 static void match_link_modify(struct sm_state *sm, struct expression *mod_expr)
2694 struct var_sym_list *links;
2695 struct var_sym *tmp;
2696 struct smatch_state *state;
2698 links = sm->state->data;
2700 FOR_EACH_PTR(links, tmp) {
2701 if (sm->sym == tmp->sym &&
2702 strcmp(sm->name, tmp->var) == 0)
2703 continue;
2704 state = get_state(SMATCH_EXTRA, tmp->var, tmp->sym);
2705 if (!state)
2706 continue;
2707 set_state(SMATCH_EXTRA, tmp->var, tmp->sym, alloc_estate_whole(estate_type(state)));
2708 } END_FOR_EACH_PTR(tmp);
2709 set_state(link_id, sm->name, sm->sym, &undefined);
2712 void register_smatch_extra_links(int id)
2714 link_id = id;
2717 void register_smatch_extra_late(int id)
2719 add_merge_hook(link_id, &merge_link_states);
2720 add_modification_hook(link_id, &match_link_modify);
2721 add_hook(&match_dereferences, DEREF_HOOK);
2722 add_hook(&match_pointer_as_array, OP_HOOK);
2723 select_return_implies_hook(DEREFERENCE, &set_param_dereferenced);
2724 add_hook(&match_function_call, FUNCTION_CALL_HOOK);
2725 add_hook(&match_assign, ASSIGNMENT_HOOK);
2726 add_hook(&match_assign, GLOBAL_ASSIGNMENT_HOOK);
2727 add_hook(&unop_expr, OP_HOOK);
2728 add_hook(&asm_expr, ASM_HOOK);
2730 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
2731 add_member_info_callback(my_id, struct_member_callback);
2732 add_split_return_callback(&returned_struct_members);
2734 // add_hook(&assume_indexes_are_valid, OP_HOOK);