math: remove the get_implied_value_low_overhead() function
[smatch.git] / smatch_extra.c
blob1d4f44849da509d8d786e48e7f03a146ec54e975
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;
207 void set_extra_mod(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
209 char *new_name;
210 struct symbol *new_sym;
212 set_extra_mod_helper(name, sym, expr, state);
213 new_name = get_other_name_sym(name, sym, &new_sym);
214 if (new_name && new_sym)
215 set_extra_mod_helper(new_name, new_sym, expr, state);
216 free_string(new_name);
219 static struct expression *chunk_get_array_base(struct expression *expr)
222 * The problem with is_array() is that it only returns true for things
223 * like foo[1] but not for foo[1].bar.
226 expr = strip_expr(expr);
227 while (expr && expr->type == EXPR_DEREF)
228 expr = strip_expr(expr->deref);
229 return get_array_base(expr);
232 static int chunk_has_array(struct expression *expr)
234 return !!chunk_get_array_base(expr);
237 static void clear_array_states(struct expression *array)
239 struct sm_state *sm;
241 sm = get_sm_state_expr(link_id, array);
242 if (sm)
243 match_link_modify(sm, NULL);
246 static void set_extra_array_mod(struct expression *expr, struct smatch_state *state)
248 struct expression *array;
249 struct var_sym_list *vsl;
250 struct var_sym *vs;
251 char *name;
252 struct symbol *sym;
254 array = chunk_get_array_base(expr);
256 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
257 if (!name || !vsl) {
258 clear_array_states(array);
259 goto free;
262 FOR_EACH_PTR(vsl, vs) {
263 store_link(link_id, vs->var, vs->sym, name, sym);
264 } END_FOR_EACH_PTR(vs);
266 call_extra_mod_hooks(name, sym, expr, state);
267 set_state(SMATCH_EXTRA, name, sym, state);
268 free:
269 free_string(name);
272 void set_extra_expr_mod(struct expression *expr, struct smatch_state *state)
274 struct symbol *sym;
275 char *name;
277 if (chunk_has_array(expr)) {
278 set_extra_array_mod(expr, state);
279 return;
282 expr = strip_expr(expr);
283 name = expr_to_var_sym(expr, &sym);
284 if (!name || !sym)
285 goto free;
286 set_extra_mod(name, sym, expr, state);
287 free:
288 free_string(name);
291 void set_extra_nomod(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
293 char *new_name;
294 struct symbol *new_sym;
295 struct relation *rel;
296 struct smatch_state *orig_state;
298 orig_state = get_state(SMATCH_EXTRA, name, sym);
300 /* don't save unknown states if leaving it blank is the same */
301 if (!orig_state && estate_is_unknown(state))
302 return;
304 new_name = get_other_name_sym(name, sym, &new_sym);
305 if (new_name && new_sym)
306 set_extra_nomod_helper(new_name, new_sym, expr, state);
307 free_string(new_name);
309 if (!estate_related(orig_state)) {
310 set_extra_nomod_helper(name, sym, expr, state);
311 return;
314 set_related(state, estate_related(orig_state));
315 FOR_EACH_PTR(estate_related(orig_state), rel) {
316 struct smatch_state *estate;
318 if (option_debug_related)
319 sm_msg("%s updating related %s to %s", name, rel->name, state->name);
320 estate = get_state(SMATCH_EXTRA, rel->name, rel->sym);
321 if (!estate)
322 continue;
323 set_extra_nomod_helper(rel->name, rel->sym, expr, clone_estate_cast(estate_type(estate), state));
324 } END_FOR_EACH_PTR(rel);
327 void set_extra_nomod_vsl(const char *name, struct symbol *sym, struct var_sym_list *vsl, struct expression *expr, struct smatch_state *state)
329 struct var_sym *vs;
331 FOR_EACH_PTR(vsl, vs) {
332 store_link(link_id, vs->var, vs->sym, name, sym);
333 } END_FOR_EACH_PTR(vs);
335 set_extra_nomod(name, sym, expr, state);
339 * This is for return_implies_state() hooks which modify a SMATCH_EXTRA state
341 void set_extra_expr_nomod(struct expression *expr, struct smatch_state *state)
343 struct var_sym_list *vsl;
344 struct var_sym *vs;
345 char *name;
346 struct symbol *sym;
348 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
349 if (!name || !vsl)
350 goto free;
351 FOR_EACH_PTR(vsl, vs) {
352 store_link(link_id, vs->var, vs->sym, name, sym);
353 } END_FOR_EACH_PTR(vs);
355 set_extra_nomod(name, sym, expr, state);
356 free:
357 free_string(name);
360 static void set_extra_true_false(const char *name, struct symbol *sym,
361 struct smatch_state *true_state,
362 struct smatch_state *false_state)
364 char *new_name;
365 struct symbol *new_sym;
366 struct relation *rel;
367 struct smatch_state *orig_state;
369 if (!true_state && !false_state)
370 return;
372 if (in_warn_on_macro())
373 return;
375 new_name = get_other_name_sym(name, sym, &new_sym);
376 if (new_name && new_sym)
377 set_true_false_states(SMATCH_EXTRA, new_name, new_sym, true_state, false_state);
378 free_string(new_name);
380 orig_state = get_state(SMATCH_EXTRA, name, sym);
382 if (!estate_related(orig_state)) {
383 set_true_false_states(SMATCH_EXTRA, name, sym, true_state, false_state);
384 return;
387 if (true_state)
388 set_related(true_state, estate_related(orig_state));
389 if (false_state)
390 set_related(false_state, estate_related(orig_state));
392 FOR_EACH_PTR(estate_related(orig_state), rel) {
393 set_true_false_states(SMATCH_EXTRA, rel->name, rel->sym,
394 true_state, false_state);
395 } END_FOR_EACH_PTR(rel);
398 static void set_extra_chunk_true_false(struct expression *expr,
399 struct smatch_state *true_state,
400 struct smatch_state *false_state)
402 struct var_sym_list *vsl;
403 struct var_sym *vs;
404 struct symbol *type;
405 char *name;
406 struct symbol *sym;
408 if (in_warn_on_macro())
409 return;
411 type = get_type(expr);
412 if (!type)
413 return;
415 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
416 if (!name || !vsl)
417 goto free;
418 FOR_EACH_PTR(vsl, vs) {
419 store_link(link_id, vs->var, vs->sym, name, sym);
420 } END_FOR_EACH_PTR(vs);
422 set_true_false_states(SMATCH_EXTRA, name, sym,
423 clone_estate(true_state),
424 clone_estate(false_state));
425 free:
426 free_string(name);
429 static void set_extra_expr_true_false(struct expression *expr,
430 struct smatch_state *true_state,
431 struct smatch_state *false_state)
433 char *name;
434 struct symbol *sym;
435 sval_t sval;
437 if (!true_state && !false_state)
438 return;
440 if (get_value(expr, &sval))
441 return;
443 expr = strip_expr(expr);
444 name = expr_to_var_sym(expr, &sym);
445 if (!name || !sym) {
446 free_string(name);
447 set_extra_chunk_true_false(expr, true_state, false_state);
448 return;
450 set_extra_true_false(name, sym, true_state, false_state);
451 free_string(name);
454 static int get_countdown_info(struct expression *condition, struct expression **unop, int *op, sval_t *right)
456 struct expression *unop_expr;
457 int comparison;
458 sval_t limit;
460 right->type = &int_ctype;
461 right->value = 0;
463 condition = strip_expr(condition);
465 if (condition->type == EXPR_COMPARE) {
466 comparison = remove_unsigned_from_comparison(condition->op);
468 if (comparison != SPECIAL_GTE && comparison != '>')
469 return 0;
470 if (!get_value(condition->right, &limit))
471 return 0;
473 unop_expr = condition->left;
474 if (unop_expr->type != EXPR_PREOP && unop_expr->type != EXPR_POSTOP)
475 return 0;
476 if (unop_expr->op != SPECIAL_DECREMENT)
477 return 0;
479 *unop = unop_expr;
480 *op = comparison;
481 *right = limit;
483 return 1;
486 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
487 return 0;
488 if (condition->op != SPECIAL_DECREMENT)
489 return 0;
491 *unop = condition;
492 *op = '>';
494 return 1;
497 static struct sm_state *handle_canonical_while_count_down(struct statement *loop)
499 struct expression *iter_var;
500 struct expression *condition, *unop;
501 struct symbol *type;
502 struct sm_state *sm;
503 struct smatch_state *estate;
504 int op;
505 sval_t start, right;
507 right.type = &int_ctype;
508 right.value = 0;
510 condition = strip_expr(loop->iterator_pre_condition);
511 if (!condition)
512 return NULL;
514 if (!get_countdown_info(condition, &unop, &op, &right))
515 return NULL;
517 iter_var = unop->unop;
519 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
520 if (!sm)
521 return NULL;
522 if (sval_cmp(estate_min(sm->state), right) < 0)
523 return NULL;
524 start = estate_max(sm->state);
526 type = get_type(iter_var);
527 right = sval_cast(type, right);
528 start = sval_cast(type, start);
530 if (sval_cmp(start, right) <= 0)
531 return NULL;
532 if (!sval_is_max(start))
533 start.value--;
535 if (op == SPECIAL_GTE)
536 right.value--;
538 if (unop->type == EXPR_PREOP) {
539 right.value++;
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 if (unop->type == EXPR_POSTOP) {
547 estate = alloc_estate_range(right, start);
548 if (estate_has_hard_max(sm->state))
549 estate_set_hard_max(estate);
550 estate_copy_fuzzy_max(estate, sm->state);
551 set_extra_expr_mod(iter_var, estate);
553 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
556 static struct sm_state *handle_canonical_for_inc(struct expression *iter_expr,
557 struct expression *condition)
559 struct expression *iter_var;
560 struct sm_state *sm;
561 struct smatch_state *estate;
562 sval_t start, end, max;
563 struct symbol *type;
565 iter_var = iter_expr->unop;
566 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
567 if (!sm)
568 return NULL;
569 if (!estate_get_single_value(sm->state, &start))
570 return NULL;
571 if (!get_implied_value(condition->right, &end))
572 return NULL;
574 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
575 return NULL;
577 switch (condition->op) {
578 case SPECIAL_UNSIGNED_LT:
579 case SPECIAL_NOTEQUAL:
580 case '<':
581 if (!sval_is_min(end))
582 end.value--;
583 break;
584 case SPECIAL_UNSIGNED_LTE:
585 case SPECIAL_LTE:
586 break;
587 default:
588 return NULL;
590 if (sval_cmp(end, start) < 0)
591 return NULL;
592 type = get_type(iter_var);
593 start = sval_cast(type, start);
594 end = sval_cast(type, end);
595 estate = alloc_estate_range(start, end);
596 if (get_hard_max(condition->right, &max)) {
597 if (!get_macro_name(condition->pos))
598 estate_set_hard_max(estate);
599 if (condition->op == '<' ||
600 condition->op == SPECIAL_UNSIGNED_LT ||
601 condition->op == SPECIAL_NOTEQUAL)
602 max.value--;
603 max = sval_cast(type, max);
604 estate_set_fuzzy_max(estate, max);
606 set_extra_expr_mod(iter_var, estate);
607 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
610 static struct sm_state *handle_canonical_for_dec(struct expression *iter_expr,
611 struct expression *condition)
613 struct expression *iter_var;
614 struct sm_state *sm;
615 struct smatch_state *estate;
616 sval_t start, end;
618 iter_var = iter_expr->unop;
619 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
620 if (!sm)
621 return NULL;
622 if (!estate_get_single_value(sm->state, &start))
623 return NULL;
624 if (!get_implied_min(condition->right, &end))
625 end = sval_type_min(get_type(iter_var));
626 end = sval_cast(estate_type(sm->state), end);
627 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
628 return NULL;
630 switch (condition->op) {
631 case SPECIAL_NOTEQUAL:
632 case '>':
633 if (!sval_is_max(end))
634 end.value++;
635 break;
636 case SPECIAL_GTE:
637 break;
638 default:
639 return NULL;
641 if (sval_cmp(end, start) > 0)
642 return NULL;
643 estate = alloc_estate_range(end, start);
644 estate_set_hard_max(estate);
645 estate_set_fuzzy_max(estate, estate_get_fuzzy_max(estate));
646 set_extra_expr_mod(iter_var, estate);
647 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
650 static struct sm_state *handle_canonical_for_loops(struct statement *loop)
652 struct expression *iter_expr;
653 struct expression *condition;
655 if (!loop->iterator_post_statement)
656 return NULL;
657 if (loop->iterator_post_statement->type != STMT_EXPRESSION)
658 return NULL;
659 iter_expr = loop->iterator_post_statement->expression;
660 if (!loop->iterator_pre_condition)
661 return NULL;
662 if (loop->iterator_pre_condition->type != EXPR_COMPARE)
663 return NULL;
664 condition = loop->iterator_pre_condition;
666 if (iter_expr->op == SPECIAL_INCREMENT)
667 return handle_canonical_for_inc(iter_expr, condition);
668 if (iter_expr->op == SPECIAL_DECREMENT)
669 return handle_canonical_for_dec(iter_expr, condition);
670 return NULL;
673 struct sm_state *__extra_handle_canonical_loops(struct statement *loop, struct stree **stree)
675 struct sm_state *ret;
678 * Canonical loops are a hack. The proper way to handle this is to
679 * use two passes, but unfortunately, doing two passes makes parsing
680 * code twice as slow.
682 * What we do is we set the inside state here, which overwrites whatever
683 * __extra_match_condition() does. Then we set the outside state in
684 * __extra_pre_loop_hook_after().
687 __push_fake_cur_stree();
688 if (!loop->iterator_post_statement)
689 ret = handle_canonical_while_count_down(loop);
690 else
691 ret = handle_canonical_for_loops(loop);
692 *stree = __pop_fake_cur_stree();
693 return ret;
696 int __iterator_unchanged(struct sm_state *sm)
698 if (!sm)
699 return 0;
700 if (get_sm_state(my_id, sm->name, sm->sym) == sm)
701 return 1;
702 return 0;
705 static void while_count_down_after(struct sm_state *sm, struct expression *condition)
707 struct expression *unop;
708 int op;
709 sval_t limit, after_value;
711 if (!get_countdown_info(condition, &unop, &op, &limit))
712 return;
713 after_value = estate_min(sm->state);
714 after_value.value--;
715 set_extra_mod(sm->name, sm->sym, condition->unop, alloc_estate_sval(after_value));
718 void __extra_pre_loop_hook_after(struct sm_state *sm,
719 struct statement *iterator,
720 struct expression *condition)
722 struct expression *iter_expr;
723 sval_t limit;
724 struct smatch_state *state;
726 if (!iterator) {
727 while_count_down_after(sm, condition);
728 return;
731 iter_expr = iterator->expression;
733 if (condition->type != EXPR_COMPARE)
734 return;
735 if (iter_expr->op == SPECIAL_INCREMENT) {
736 limit = sval_binop(estate_max(sm->state), '+',
737 sval_type_val(estate_type(sm->state), 1));
738 } else {
739 limit = sval_binop(estate_min(sm->state), '-',
740 sval_type_val(estate_type(sm->state), 1));
742 if (!estate_has_hard_max(sm->state) && !__has_breaks()) {
743 if (iter_expr->op == SPECIAL_INCREMENT)
744 state = alloc_estate_range(estate_min(sm->state), limit);
745 else
746 state = alloc_estate_range(limit, estate_max(sm->state));
747 } else {
748 state = alloc_estate_sval(limit);
750 if (!estate_has_hard_max(sm->state)) {
751 estate_clear_hard_max(state);
753 if (estate_has_fuzzy_max(sm->state)) {
754 sval_t hmax = estate_get_fuzzy_max(sm->state);
755 sval_t max = estate_max(sm->state);
757 if (sval_cmp(hmax, max) != 0)
758 estate_clear_fuzzy_max(state);
759 } else if (!estate_has_fuzzy_max(sm->state)) {
760 estate_clear_fuzzy_max(state);
763 set_extra_mod(sm->name, sm->sym, iter_expr, state);
766 static bool get_global_rl(const char *name, struct symbol *sym, struct range_list **rl)
768 struct expression *expr;
770 if (!sym || !(sym->ctype.modifiers & MOD_TOPLEVEL) || !sym->ident)
771 return false;
772 if (strcmp(sym->ident->name, name) != 0)
773 return false;
775 expr = symbol_expression(sym);
776 return get_implied_rl(expr, rl);
779 static struct stree *unmatched_stree;
780 static struct smatch_state *unmatched_state(struct sm_state *sm)
782 struct smatch_state *state;
783 struct range_list *rl;
785 if (unmatched_stree) {
786 state = get_state_stree(unmatched_stree, SMATCH_EXTRA, sm->name, sm->sym);
787 if (state)
788 return state;
790 if (parent_is_gone_var_sym(sm->name, sm->sym))
791 return alloc_estate_empty();
792 if (get_global_rl(sm->name, sm->sym, &rl))
793 return alloc_estate_rl(rl);
794 return alloc_estate_whole(estate_type(sm->state));
797 static void clear_the_pointed_at(struct expression *expr)
799 struct stree *stree;
800 char *name;
801 struct symbol *sym;
802 struct sm_state *tmp;
804 name = expr_to_var_sym(expr, &sym);
805 if (!name || !sym)
806 goto free;
808 stree = __get_cur_stree();
809 FOR_EACH_MY_SM(SMATCH_EXTRA, stree, tmp) {
810 if (tmp->name[0] != '*')
811 continue;
812 if (tmp->sym != sym)
813 continue;
814 if (strcmp(tmp->name + 1, name) != 0)
815 continue;
816 set_extra_mod(tmp->name, tmp->sym, expr, alloc_estate_whole(estate_type(tmp->state)));
817 } END_FOR_EACH_SM(tmp);
819 free:
820 free_string(name);
823 static int is_const_param(struct expression *expr, int param)
825 struct symbol *type;
827 type = get_arg_type(expr, param);
828 if (!type)
829 return 0;
830 if (type->ctype.modifiers & MOD_CONST)
831 return 1;
832 return 0;
835 static void match_function_call(struct expression *expr)
837 struct expression *arg;
838 struct expression *tmp;
839 int param = -1;
841 /* if we have the db this is handled in smatch_function_hooks.c */
842 if (!option_no_db)
843 return;
844 if (inlinable(expr->fn))
845 return;
847 FOR_EACH_PTR(expr->args, arg) {
848 param++;
849 if (is_const_param(expr->fn, param))
850 continue;
851 tmp = strip_expr(arg);
852 if (tmp->type == EXPR_PREOP && tmp->op == '&')
853 set_extra_expr_mod(tmp->unop, alloc_estate_whole(get_type(tmp->unop)));
854 else
855 clear_the_pointed_at(tmp);
856 } END_FOR_EACH_PTR(arg);
859 static int values_fit_type(struct expression *left, struct expression *right)
861 struct range_list *rl;
862 struct symbol *type;
864 type = get_type(left);
865 if (!type)
866 return 0;
867 get_absolute_rl(right, &rl);
868 if (type == rl_type(rl))
869 return 1;
870 if (type_unsigned(type) && sval_is_negative(rl_min(rl)))
871 return 0;
872 if (sval_cmp(sval_type_min(type), rl_min(rl)) > 0)
873 return 0;
874 if (sval_cmp(sval_type_max(type), rl_max(rl)) < 0)
875 return 0;
876 return 1;
879 static void save_chunk_info(struct expression *left, struct expression *right)
881 struct var_sym_list *vsl;
882 struct var_sym *vs;
883 struct expression *add_expr;
884 struct symbol *type;
885 sval_t sval;
886 char *name;
887 struct symbol *sym;
889 if (right->type != EXPR_BINOP || right->op != '-')
890 return;
891 if (!get_value(right->left, &sval))
892 return;
893 if (!expr_to_sym(right->right))
894 return;
896 add_expr = binop_expression(left, '+', right->right);
897 type = get_type(add_expr);
898 if (!type)
899 return;
900 name = expr_to_chunk_sym_vsl(add_expr, &sym, &vsl);
901 if (!name || !vsl)
902 goto free;
903 FOR_EACH_PTR(vsl, vs) {
904 store_link(link_id, vs->var, vs->sym, name, sym);
905 } END_FOR_EACH_PTR(vs);
907 set_state(SMATCH_EXTRA, name, sym, alloc_estate_sval(sval_cast(type, sval)));
908 free:
909 free_string(name);
912 static void do_array_assign(struct expression *left, int op, struct expression *right)
914 struct range_list *rl;
916 if (op == '=') {
917 get_absolute_rl(right, &rl);
918 rl = cast_rl(get_type(left), rl);
919 } else {
920 rl = alloc_whole_rl(get_type(left));
923 set_extra_array_mod(left, alloc_estate_rl(rl));
926 static void match_vanilla_assign(struct expression *left, struct expression *right)
928 struct range_list *orig_rl = NULL;
929 struct range_list *rl = NULL;
930 struct symbol *right_sym;
931 struct symbol *left_type;
932 struct symbol *right_type;
933 char *right_name = NULL;
934 struct symbol *sym;
935 char *name;
936 sval_t sval, max;
937 struct smatch_state *state;
938 int comparison;
940 if (is_struct(left))
941 return;
943 save_chunk_info(left, right);
945 name = expr_to_var_sym(left, &sym);
946 if (!name) {
947 if (chunk_has_array(left))
948 do_array_assign(left, '=', right);
949 return;
952 left_type = get_type(left);
953 right_type = get_type(right);
955 right_name = expr_to_var_sym(right, &right_sym);
957 if (!__in_fake_assign &&
958 !(right->type == EXPR_PREOP && right->op == '&') &&
959 right_name && right_sym &&
960 values_fit_type(left, strip_expr(right)) &&
961 !has_symbol(right, sym)) {
962 set_equiv(left, right);
963 goto free;
966 if (is_pointer(right) && get_address_rl(right, &rl)) {
967 state = alloc_estate_rl(rl);
968 goto done;
971 if (get_implied_value(right, &sval)) {
972 state = alloc_estate_sval(sval_cast(left_type, sval));
973 goto done;
976 if (__in_fake_assign) {
977 struct smatch_state *right_state;
978 sval_t sval;
980 if (get_value(right, &sval)) {
981 sval = sval_cast(left_type, sval);
982 state = alloc_estate_sval(sval);
983 goto done;
986 right_state = get_state(SMATCH_EXTRA, right_name, right_sym);
987 if (right_state) {
988 /* simple assignment */
989 state = clone_estate(right_state);
990 goto done;
993 state = alloc_estate_rl(alloc_whole_rl(left_type));
994 goto done;
997 comparison = get_comparison(left, right);
998 if (comparison) {
999 comparison = flip_comparison(comparison);
1000 get_implied_rl(left, &orig_rl);
1003 if (get_implied_rl(right, &rl)) {
1004 rl = cast_rl(left_type, rl);
1005 if (orig_rl)
1006 filter_by_comparison(&rl, comparison, orig_rl);
1007 state = alloc_estate_rl(rl);
1008 if (get_hard_max(right, &max)) {
1009 estate_set_hard_max(state);
1010 estate_set_fuzzy_max(state, max);
1012 } else {
1013 rl = alloc_whole_rl(right_type);
1014 rl = cast_rl(left_type, rl);
1015 if (orig_rl)
1016 filter_by_comparison(&rl, comparison, orig_rl);
1017 state = alloc_estate_rl(rl);
1020 done:
1021 set_extra_mod(name, sym, left, state);
1022 free:
1023 free_string(right_name);
1026 static int op_remove_assign(int op)
1028 switch (op) {
1029 case SPECIAL_ADD_ASSIGN:
1030 return '+';
1031 case SPECIAL_SUB_ASSIGN:
1032 return '-';
1033 case SPECIAL_MUL_ASSIGN:
1034 return '*';
1035 case SPECIAL_DIV_ASSIGN:
1036 return '/';
1037 case SPECIAL_MOD_ASSIGN:
1038 return '%';
1039 case SPECIAL_AND_ASSIGN:
1040 return '&';
1041 case SPECIAL_OR_ASSIGN:
1042 return '|';
1043 case SPECIAL_XOR_ASSIGN:
1044 return '^';
1045 case SPECIAL_SHL_ASSIGN:
1046 return SPECIAL_LEFTSHIFT;
1047 case SPECIAL_SHR_ASSIGN:
1048 return SPECIAL_RIGHTSHIFT;
1049 default:
1050 return op;
1054 static void match_assign(struct expression *expr)
1056 struct range_list *rl = NULL;
1057 struct expression *left;
1058 struct expression *right;
1059 struct expression *binop_expr;
1060 struct symbol *left_type;
1061 struct symbol *sym;
1062 char *name;
1063 sval_t left_min, left_max;
1064 sval_t right_min, right_max;
1065 sval_t res_min, res_max;
1067 left = strip_expr(expr->left);
1069 right = strip_parens(expr->right);
1070 if (right->type == EXPR_CALL && sym_name_is("__builtin_expect", right->fn))
1071 right = get_argument_from_call_expr(right->args, 0);
1072 while (right->type == EXPR_ASSIGNMENT && right->op == '=')
1073 right = strip_parens(right->left);
1075 if (expr->op == '=' && is_condition(expr->right))
1076 return; /* handled in smatch_condition.c */
1077 if (expr->op == '=' && right->type == EXPR_CALL)
1078 return; /* handled in smatch_function_hooks.c */
1079 if (expr->op == '=') {
1080 match_vanilla_assign(left, right);
1081 return;
1084 name = expr_to_var_sym(left, &sym);
1085 if (!name)
1086 return;
1088 left_type = get_type(left);
1090 res_min = sval_type_min(left_type);
1091 res_max = sval_type_max(left_type);
1093 switch (expr->op) {
1094 case SPECIAL_ADD_ASSIGN:
1095 get_absolute_max(left, &left_max);
1096 get_absolute_max(right, &right_max);
1097 if (sval_binop_overflows(left_max, '+', sval_cast(left_type, right_max)))
1098 break;
1099 if (get_implied_min(left, &left_min) &&
1100 !sval_is_negative_min(left_min) &&
1101 get_implied_min(right, &right_min) &&
1102 !sval_is_negative_min(right_min)) {
1103 res_min = sval_binop(left_min, '+', right_min);
1104 res_min = sval_cast(left_type, res_min);
1106 if (inside_loop()) /* we are assuming loops don't lead to wrapping */
1107 break;
1108 res_max = sval_binop(left_max, '+', right_max);
1109 res_max = sval_cast(left_type, res_max);
1110 break;
1111 case SPECIAL_SUB_ASSIGN:
1112 if (get_implied_max(left, &left_max) &&
1113 !sval_is_max(left_max) &&
1114 get_implied_min(right, &right_min) &&
1115 !sval_is_min(right_min)) {
1116 res_max = sval_binop(left_max, '-', right_min);
1117 res_max = sval_cast(left_type, res_max);
1119 if (inside_loop())
1120 break;
1121 if (get_implied_min(left, &left_min) &&
1122 !sval_is_min(left_min) &&
1123 get_implied_max(right, &right_max) &&
1124 !sval_is_max(right_max)) {
1125 res_min = sval_binop(left_min, '-', right_max);
1126 res_min = sval_cast(left_type, res_min);
1128 break;
1129 case SPECIAL_AND_ASSIGN:
1130 case SPECIAL_MOD_ASSIGN:
1131 case SPECIAL_SHL_ASSIGN:
1132 case SPECIAL_SHR_ASSIGN:
1133 case SPECIAL_OR_ASSIGN:
1134 case SPECIAL_XOR_ASSIGN:
1135 case SPECIAL_MUL_ASSIGN:
1136 case SPECIAL_DIV_ASSIGN:
1137 binop_expr = binop_expression(expr->left,
1138 op_remove_assign(expr->op),
1139 expr->right);
1140 if (get_absolute_rl(binop_expr, &rl)) {
1141 rl = cast_rl(left_type, rl);
1142 set_extra_mod(name, sym, left, alloc_estate_rl(rl));
1143 goto free;
1145 break;
1147 rl = cast_rl(left_type, alloc_rl(res_min, res_max));
1148 set_extra_mod(name, sym, left, alloc_estate_rl(rl));
1149 free:
1150 free_string(name);
1153 static struct smatch_state *increment_state(struct smatch_state *state)
1155 sval_t min = estate_min(state);
1156 sval_t max = estate_max(state);
1158 if (!estate_rl(state))
1159 return NULL;
1161 if (inside_loop())
1162 max = sval_type_max(max.type);
1164 if (!sval_is_min(min) && !sval_is_max(min))
1165 min.value++;
1166 if (!sval_is_min(max) && !sval_is_max(max))
1167 max.value++;
1168 return alloc_estate_range(min, max);
1171 static struct smatch_state *decrement_state(struct smatch_state *state)
1173 sval_t min = estate_min(state);
1174 sval_t max = estate_max(state);
1176 if (!estate_rl(state))
1177 return NULL;
1179 if (inside_loop())
1180 min = sval_type_min(min.type);
1182 if (!sval_is_min(min) && !sval_is_max(min))
1183 min.value--;
1184 if (!sval_is_min(max) && !sval_is_max(max))
1185 max.value--;
1186 return alloc_estate_range(min, max);
1189 static void clear_pointed_at_state(struct expression *expr)
1191 struct symbol *type;
1194 * ALERT: This is sort of a mess. If it's is a struct assigment like
1195 * "foo = bar;", then that's handled by smatch_struct_assignment.c.
1196 * the same thing for p++ where "p" is a struct. Most modifications
1197 * are handled by the assignment hook or the db. Smatch_extra.c doesn't
1198 * use smatch_modification.c because we have to get the ordering right
1199 * or something. So if you have p++ where p is a pointer to a standard
1200 * c type then we handle that here. What a mess.
1202 expr = strip_expr(expr);
1203 type = get_type(expr);
1204 if (!type || type->type != SYM_PTR)
1205 return;
1206 type = get_real_base_type(type);
1207 if (!type || type->type != SYM_BASETYPE)
1208 return;
1209 set_extra_expr_nomod(deref_expression(expr), alloc_estate_whole(type));
1212 static void unop_expr(struct expression *expr)
1214 struct smatch_state *state;
1216 if (expr->smatch_flags & Handled)
1217 return;
1219 switch (expr->op) {
1220 case SPECIAL_INCREMENT:
1221 state = get_state_expr(SMATCH_EXTRA, expr->unop);
1222 state = increment_state(state);
1223 if (!state)
1224 state = alloc_estate_whole(get_type(expr));
1225 set_extra_expr_mod(expr->unop, state);
1226 clear_pointed_at_state(expr->unop);
1227 break;
1228 case SPECIAL_DECREMENT:
1229 state = get_state_expr(SMATCH_EXTRA, expr->unop);
1230 state = decrement_state(state);
1231 if (!state)
1232 state = alloc_estate_whole(get_type(expr));
1233 set_extra_expr_mod(expr->unop, state);
1234 clear_pointed_at_state(expr->unop);
1235 break;
1236 default:
1237 return;
1241 static void asm_expr(struct statement *stmt)
1244 struct expression *expr;
1245 struct symbol *type;
1246 int state = 0;
1248 FOR_EACH_PTR(stmt->asm_outputs, expr) {
1249 switch (state) {
1250 case 0: /* identifier */
1251 case 1: /* constraint */
1252 state++;
1253 continue;
1254 case 2: /* expression */
1255 state = 0;
1256 type = get_type(strip_expr(expr));
1257 set_extra_expr_mod(expr, alloc_estate_whole(type));
1258 continue;
1260 } END_FOR_EACH_PTR(expr);
1263 static void check_dereference(struct expression *expr)
1265 struct smatch_state *state;
1267 if (__in_fake_assign)
1268 return;
1269 if (outside_of_function())
1270 return;
1271 state = get_extra_state(expr);
1272 if (state) {
1273 struct range_list *rl;
1275 rl = rl_intersection(estate_rl(state), valid_ptr_rl);
1276 if (rl_equiv(rl, estate_rl(state)))
1277 return;
1278 set_extra_expr_nomod(expr, alloc_estate_rl(rl));
1279 } else {
1280 set_extra_expr_nomod(expr, alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval));
1284 static void match_dereferences(struct expression *expr)
1286 if (expr->type != EXPR_PREOP)
1287 return;
1288 /* it's saying that foo[1] = bar dereferences foo[1] */
1289 if (is_array(expr))
1290 return;
1291 check_dereference(expr->unop);
1294 static void match_pointer_as_array(struct expression *expr)
1296 if (!is_array(expr))
1297 return;
1298 check_dereference(get_array_base(expr));
1301 static void find_dereferences(struct expression *expr)
1303 while (expr->type == EXPR_PREOP) {
1304 if (expr->op == '*')
1305 check_dereference(expr->unop);
1306 expr = strip_expr(expr->unop);
1310 static void set_param_dereferenced(struct expression *call, struct expression *arg, char *key, char *unused)
1312 struct symbol *sym;
1313 char *name;
1315 name = get_variable_from_key(arg, key, &sym);
1316 if (name && sym) {
1317 struct smatch_state *orig, *new;
1318 struct range_list *rl;
1320 orig = get_state(SMATCH_EXTRA, name, sym);
1321 if (orig) {
1322 rl = rl_intersection(estate_rl(orig),
1323 alloc_rl(valid_ptr_min_sval,
1324 valid_ptr_max_sval));
1325 new = alloc_estate_rl(rl);
1326 } else {
1327 new = alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval);
1330 set_extra_nomod(name, sym, NULL, new);
1332 free_string(name);
1334 find_dereferences(arg);
1337 static sval_t add_one(sval_t sval)
1339 sval.value++;
1340 return sval;
1343 static int handle_postop_inc(struct expression *left, int op, struct expression *right)
1345 struct statement *stmt;
1346 struct expression *cond;
1347 struct smatch_state *true_state, *false_state;
1348 struct symbol *type;
1349 sval_t start;
1350 sval_t limit;
1353 * If we're decrementing here then that's a canonical while count down
1354 * so it's handled already. We're only handling loops like:
1355 * i = 0;
1356 * do { ... } while (i++ < 3);
1359 if (left->type != EXPR_POSTOP || left->op != SPECIAL_INCREMENT)
1360 return 0;
1362 stmt = __cur_stmt->parent;
1363 if (!stmt)
1364 return 0;
1365 if (stmt->type == STMT_COMPOUND)
1366 stmt = stmt->parent;
1367 if (!stmt || stmt->type != STMT_ITERATOR || !stmt->iterator_post_condition)
1368 return 0;
1370 cond = strip_expr(stmt->iterator_post_condition);
1371 if (cond->type != EXPR_COMPARE || cond->op != op)
1372 return 0;
1373 if (left != strip_expr(cond->left) || right != strip_expr(cond->right))
1374 return 0;
1376 if (!get_implied_value(left->unop, &start))
1377 return 0;
1378 if (!get_implied_value(right, &limit))
1379 return 0;
1380 type = get_type(left->unop);
1381 limit = sval_cast(type, limit);
1382 if (sval_cmp(start, limit) > 0)
1383 return 0;
1385 switch (op) {
1386 case '<':
1387 case SPECIAL_UNSIGNED_LT:
1388 break;
1389 case SPECIAL_LTE:
1390 case SPECIAL_UNSIGNED_LTE:
1391 limit = add_one(limit);
1392 default:
1393 return 0;
1397 true_state = alloc_estate_range(add_one(start), limit);
1398 false_state = alloc_estate_range(add_one(limit), add_one(limit));
1400 /* Currently we just discard the false state but when two passes is
1401 * implimented correctly then it will use it.
1404 set_extra_expr_true_false(left->unop, true_state, false_state);
1406 return 1;
1409 bool is_impossible_variable(struct expression *expr)
1411 struct smatch_state *state;
1413 state = get_extra_state(expr);
1414 if (state && !estate_rl(state))
1415 return true;
1416 return false;
1419 static bool in_macro(struct expression *left, struct expression *right)
1421 if (!left || !right)
1422 return 0;
1423 if (left->pos.line != right->pos.line || left->pos.pos != right->pos.pos)
1424 return 0;
1425 if (get_macro_name(left->pos))
1426 return 1;
1427 return 0;
1430 static void handle_comparison(struct symbol *type, struct expression *left, int op, struct expression *right)
1432 struct range_list *left_orig;
1433 struct range_list *left_true;
1434 struct range_list *left_false;
1435 struct range_list *right_orig;
1436 struct range_list *right_true;
1437 struct range_list *right_false;
1438 struct smatch_state *left_true_state;
1439 struct smatch_state *left_false_state;
1440 struct smatch_state *right_true_state;
1441 struct smatch_state *right_false_state;
1442 sval_t dummy, hard_max;
1443 int left_postop = 0;
1444 int right_postop = 0;
1446 if (left->op == SPECIAL_INCREMENT || left->op == SPECIAL_DECREMENT) {
1447 if (left->type == EXPR_POSTOP) {
1448 left->smatch_flags |= Handled;
1449 left_postop = left->op;
1450 if (handle_postop_inc(left, op, right))
1451 return;
1453 left = strip_parens(left->unop);
1455 while (left->type == EXPR_ASSIGNMENT)
1456 left = strip_parens(left->left);
1458 if (right->op == SPECIAL_INCREMENT || right->op == SPECIAL_DECREMENT) {
1459 if (right->type == EXPR_POSTOP) {
1460 right->smatch_flags |= Handled;
1461 right_postop = right->op;
1463 right = strip_parens(right->unop);
1466 if (is_impossible_variable(left) || is_impossible_variable(right))
1467 return;
1469 get_real_absolute_rl(left, &left_orig);
1470 left_orig = cast_rl(type, left_orig);
1472 get_real_absolute_rl(right, &right_orig);
1473 right_orig = cast_rl(type, right_orig);
1475 split_comparison_rl(left_orig, op, right_orig, &left_true, &left_false, &right_true, &right_false);
1477 left_true = rl_truncate_cast(get_type(strip_expr(left)), left_true);
1478 left_false = rl_truncate_cast(get_type(strip_expr(left)), left_false);
1479 right_true = rl_truncate_cast(get_type(strip_expr(right)), right_true);
1480 right_false = rl_truncate_cast(get_type(strip_expr(right)), right_false);
1482 if (!left_true || !left_false) {
1483 struct range_list *tmp_true, *tmp_false;
1485 split_comparison_rl(alloc_whole_rl(type), op, right_orig, &tmp_true, &tmp_false, NULL, NULL);
1486 tmp_true = rl_truncate_cast(get_type(strip_expr(left)), tmp_true);
1487 tmp_false = rl_truncate_cast(get_type(strip_expr(left)), tmp_false);
1488 if (tmp_true && tmp_false)
1489 __save_imaginary_state(left, tmp_true, tmp_false);
1492 if (!right_true || !right_false) {
1493 struct range_list *tmp_true, *tmp_false;
1495 split_comparison_rl(alloc_whole_rl(type), op, right_orig, NULL, NULL, &tmp_true, &tmp_false);
1496 tmp_true = rl_truncate_cast(get_type(strip_expr(right)), tmp_true);
1497 tmp_false = rl_truncate_cast(get_type(strip_expr(right)), tmp_false);
1498 if (tmp_true && tmp_false)
1499 __save_imaginary_state(right, tmp_true, tmp_false);
1502 left_true_state = alloc_estate_rl(left_true);
1503 left_false_state = alloc_estate_rl(left_false);
1504 right_true_state = alloc_estate_rl(right_true);
1505 right_false_state = alloc_estate_rl(right_false);
1507 switch (op) {
1508 case '<':
1509 case SPECIAL_UNSIGNED_LT:
1510 case SPECIAL_UNSIGNED_LTE:
1511 case SPECIAL_LTE:
1512 if (get_implied_value(right, &dummy) && !in_macro(left, right))
1513 estate_set_hard_max(left_true_state);
1514 if (get_implied_value(left, &dummy) && !in_macro(left, right))
1515 estate_set_hard_max(right_false_state);
1516 break;
1517 case '>':
1518 case SPECIAL_UNSIGNED_GT:
1519 case SPECIAL_UNSIGNED_GTE:
1520 case SPECIAL_GTE:
1521 if (get_implied_value(left, &dummy) && !in_macro(left, right))
1522 estate_set_hard_max(right_true_state);
1523 if (get_implied_value(right, &dummy) && !in_macro(left, right))
1524 estate_set_hard_max(left_false_state);
1525 break;
1528 switch (op) {
1529 case '<':
1530 case SPECIAL_UNSIGNED_LT:
1531 case SPECIAL_UNSIGNED_LTE:
1532 case SPECIAL_LTE:
1533 if (get_hard_max(right, &hard_max)) {
1534 if (op == '<' || op == SPECIAL_UNSIGNED_LT)
1535 hard_max.value--;
1536 estate_set_fuzzy_max(left_true_state, hard_max);
1538 if (get_implied_value(right, &hard_max)) {
1539 if (op == SPECIAL_UNSIGNED_LTE ||
1540 op == SPECIAL_LTE)
1541 hard_max.value++;
1542 estate_set_fuzzy_max(left_false_state, hard_max);
1544 if (get_hard_max(left, &hard_max)) {
1545 if (op == SPECIAL_UNSIGNED_LTE ||
1546 op == SPECIAL_LTE)
1547 hard_max.value--;
1548 estate_set_fuzzy_max(right_false_state, hard_max);
1550 if (get_implied_value(left, &hard_max)) {
1551 if (op == '<' || op == SPECIAL_UNSIGNED_LT)
1552 hard_max.value++;
1553 estate_set_fuzzy_max(right_true_state, hard_max);
1555 break;
1556 case '>':
1557 case SPECIAL_UNSIGNED_GT:
1558 case SPECIAL_UNSIGNED_GTE:
1559 case SPECIAL_GTE:
1560 if (get_hard_max(left, &hard_max)) {
1561 if (op == '>' || op == SPECIAL_UNSIGNED_GT)
1562 hard_max.value--;
1563 estate_set_fuzzy_max(right_true_state, hard_max);
1565 if (get_implied_value(left, &hard_max)) {
1566 if (op == SPECIAL_UNSIGNED_GTE ||
1567 op == SPECIAL_GTE)
1568 hard_max.value++;
1569 estate_set_fuzzy_max(right_false_state, hard_max);
1571 if (get_hard_max(right, &hard_max)) {
1572 if (op == SPECIAL_UNSIGNED_LTE ||
1573 op == SPECIAL_LTE)
1574 hard_max.value--;
1575 estate_set_fuzzy_max(left_false_state, hard_max);
1577 if (get_implied_value(right, &hard_max)) {
1578 if (op == '>' ||
1579 op == SPECIAL_UNSIGNED_GT)
1580 hard_max.value++;
1581 estate_set_fuzzy_max(left_true_state, hard_max);
1583 break;
1584 case SPECIAL_EQUAL:
1585 if (get_hard_max(left, &hard_max))
1586 estate_set_fuzzy_max(right_true_state, hard_max);
1587 if (get_hard_max(right, &hard_max))
1588 estate_set_fuzzy_max(left_true_state, hard_max);
1589 break;
1592 if (get_hard_max(left, &hard_max)) {
1593 estate_set_hard_max(left_true_state);
1594 estate_set_hard_max(left_false_state);
1596 if (get_hard_max(right, &hard_max)) {
1597 estate_set_hard_max(right_true_state);
1598 estate_set_hard_max(right_false_state);
1601 if (left_postop == SPECIAL_INCREMENT) {
1602 left_true_state = increment_state(left_true_state);
1603 left_false_state = increment_state(left_false_state);
1605 if (left_postop == SPECIAL_DECREMENT) {
1606 left_true_state = decrement_state(left_true_state);
1607 left_false_state = decrement_state(left_false_state);
1609 if (right_postop == SPECIAL_INCREMENT) {
1610 right_true_state = increment_state(right_true_state);
1611 right_false_state = increment_state(right_false_state);
1613 if (right_postop == SPECIAL_DECREMENT) {
1614 right_true_state = decrement_state(right_true_state);
1615 right_false_state = decrement_state(right_false_state);
1618 if (estate_rl(left_true_state) && estates_equiv(left_true_state, left_false_state)) {
1619 left_true_state = NULL;
1620 left_false_state = NULL;
1623 if (estate_rl(right_true_state) && estates_equiv(right_true_state, right_false_state)) {
1624 right_true_state = NULL;
1625 right_false_state = NULL;
1628 /* Don't introduce new states for known true/false conditions */
1629 if (rl_equiv(left_orig, estate_rl(left_true_state)))
1630 left_true_state = NULL;
1631 if (rl_equiv(left_orig, estate_rl(left_false_state)))
1632 left_false_state = NULL;
1633 if (rl_equiv(right_orig, estate_rl(right_true_state)))
1634 right_true_state = NULL;
1635 if (rl_equiv(right_orig, estate_rl(right_false_state)))
1636 right_false_state = NULL;
1638 set_extra_expr_true_false(left, left_true_state, left_false_state);
1639 set_extra_expr_true_false(right, right_true_state, right_false_state);
1642 static int is_simple_math(struct expression *expr)
1644 if (!expr)
1645 return 0;
1646 if (expr->type != EXPR_BINOP)
1647 return 0;
1648 switch (expr->op) {
1649 case '+':
1650 case '-':
1651 case '*':
1652 return 1;
1654 return 0;
1657 static void move_known_values(struct expression **left_p, struct expression **right_p)
1659 struct expression *left = *left_p;
1660 struct expression *right = *right_p;
1661 sval_t sval, dummy;
1663 if (get_implied_value(left, &sval)) {
1664 if (!is_simple_math(right))
1665 return;
1666 if (get_implied_value(right, &dummy))
1667 return;
1668 if (right->op == '*') {
1669 sval_t divisor;
1671 if (!get_value(right->right, &divisor))
1672 return;
1673 if (divisor.value == 0)
1674 return;
1675 *left_p = binop_expression(left, invert_op(right->op), right->right);
1676 *right_p = right->left;
1677 return;
1679 if (right->op == '+' && get_value(right->left, &sval)) {
1680 *left_p = binop_expression(left, invert_op(right->op), right->left);
1681 *right_p = right->right;
1682 return;
1684 if (get_value(right->right, &sval)) {
1685 *left_p = binop_expression(left, invert_op(right->op), right->right);
1686 *right_p = right->left;
1687 return;
1689 return;
1691 if (get_implied_value(right, &sval)) {
1692 if (!is_simple_math(left))
1693 return;
1694 if (get_implied_value(left, &dummy))
1695 return;
1696 if (left->op == '*') {
1697 sval_t divisor;
1699 if (!get_value(left->right, &divisor))
1700 return;
1701 if (divisor.value == 0)
1702 return;
1703 *right_p = binop_expression(right, invert_op(left->op), left->right);
1704 *left_p = left->left;
1705 return;
1707 if (left->op == '+' && get_value(left->left, &sval)) {
1708 *right_p = binop_expression(right, invert_op(left->op), left->left);
1709 *left_p = left->right;
1710 return;
1713 if (get_value(left->right, &sval)) {
1714 *right_p = binop_expression(right, invert_op(left->op), left->right);
1715 *left_p = left->left;
1716 return;
1718 return;
1723 * The reason for do_simple_algebra() is to solve things like:
1724 * if (foo > 66 || foo + bar > 64) {
1725 * "foo" is not really a known variable so it won't be handled by
1726 * move_known_variables() but it's a super common idiom.
1729 static int do_simple_algebra(struct expression **left_p, struct expression **right_p)
1731 struct expression *left = *left_p;
1732 struct expression *right = *right_p;
1733 struct range_list *rl;
1734 sval_t tmp;
1736 if (left->type != EXPR_BINOP || left->op != '+')
1737 return 0;
1738 if (can_integer_overflow(get_type(left), left))
1739 return 0;
1740 if (!get_implied_value(right, &tmp))
1741 return 0;
1743 if (!get_implied_value(left->left, &tmp) &&
1744 get_implied_rl(left->left, &rl) &&
1745 !is_whole_rl(rl)) {
1746 *right_p = binop_expression(right, '-', left->left);
1747 *left_p = left->right;
1748 return 1;
1750 if (!get_implied_value(left->right, &tmp) &&
1751 get_implied_rl(left->right, &rl) &&
1752 !is_whole_rl(rl)) {
1753 *right_p = binop_expression(right, '-', left->right);
1754 *left_p = left->left;
1755 return 1;
1758 return 0;
1761 static int match_func_comparison(struct expression *expr)
1763 struct expression *left = strip_expr(expr->left);
1764 struct expression *right = strip_expr(expr->right);
1766 if (left->type == EXPR_CALL || right->type == EXPR_CALL) {
1767 function_comparison(left, expr->op, right);
1768 return 1;
1771 return 0;
1774 /* Handle conditions like "if (foo + bar < foo) {" */
1775 static int handle_integer_overflow_test(struct expression *expr)
1777 struct expression *left, *right;
1778 struct symbol *type;
1779 sval_t left_min, right_min, min, max;
1781 if (expr->op != '<' && expr->op != SPECIAL_UNSIGNED_LT)
1782 return 0;
1784 left = strip_parens(expr->left);
1785 right = strip_parens(expr->right);
1787 if (left->op != '+')
1788 return 0;
1790 type = get_type(expr);
1791 if (!type)
1792 return 0;
1793 if (type_positive_bits(type) == 32) {
1794 max.type = &uint_ctype;
1795 max.uvalue = (unsigned int)-1;
1796 } else if (type_positive_bits(type) == 64) {
1797 max.type = &ulong_ctype;
1798 max.value = (unsigned long long)-1;
1799 } else {
1800 return 0;
1803 if (!expr_equiv(left->left, right) && !expr_equiv(left->right, right))
1804 return 0;
1806 get_absolute_min(left->left, &left_min);
1807 get_absolute_min(left->right, &right_min);
1808 min = sval_binop(left_min, '+', right_min);
1810 type = get_type(left);
1811 min = sval_cast(type, min);
1812 max = sval_cast(type, max);
1814 set_extra_chunk_true_false(left, NULL, alloc_estate_range(min, max));
1815 return 1;
1818 static void match_comparison(struct expression *expr)
1820 struct expression *left_orig = strip_parens(expr->left);
1821 struct expression *right_orig = strip_parens(expr->right);
1822 struct expression *left, *right, *tmp;
1823 struct expression *prev;
1824 struct symbol *type;
1825 int redo, count;
1827 if (match_func_comparison(expr))
1828 return;
1830 type = get_type(expr);
1831 if (!type)
1832 type = &llong_ctype;
1834 if (handle_integer_overflow_test(expr))
1835 return;
1837 left = left_orig;
1838 right = right_orig;
1839 move_known_values(&left, &right);
1840 handle_comparison(type, left, expr->op, right);
1842 left = left_orig;
1843 right = right_orig;
1844 if (do_simple_algebra(&left, &right))
1845 handle_comparison(type, left, expr->op, right);
1847 prev = get_assigned_expr(left_orig);
1848 if (is_simple_math(prev) && has_variable(prev, left_orig) == 0) {
1849 left = prev;
1850 right = right_orig;
1851 move_known_values(&left, &right);
1852 handle_comparison(type, left, expr->op, right);
1855 prev = get_assigned_expr(right_orig);
1856 if (is_simple_math(prev) && has_variable(prev, right_orig) == 0) {
1857 left = left_orig;
1858 right = prev;
1859 move_known_values(&left, &right);
1860 handle_comparison(type, left, expr->op, right);
1863 redo = 0;
1864 left = left_orig;
1865 right = right_orig;
1866 if (get_last_expr_from_expression_stmt(left_orig)) {
1867 left = get_last_expr_from_expression_stmt(left_orig);
1868 redo = 1;
1870 if (get_last_expr_from_expression_stmt(right_orig)) {
1871 right = get_last_expr_from_expression_stmt(right_orig);
1872 redo = 1;
1875 if (!redo)
1876 return;
1878 count = 0;
1879 while ((tmp = get_assigned_expr(left))) {
1880 if (count++ > 3)
1881 break;
1882 left = strip_expr(tmp);
1884 count = 0;
1885 while ((tmp = get_assigned_expr(right))) {
1886 if (count++ > 3)
1887 break;
1888 right = strip_expr(tmp);
1891 handle_comparison(type, left, expr->op, right);
1894 static sval_t get_high_mask(sval_t known)
1896 sval_t ret;
1897 int i;
1899 ret = known;
1900 ret.value = 0;
1902 for (i = type_bits(known.type) - 1; i >= 0; i--) {
1903 if (known.uvalue & (1ULL << i))
1904 ret.uvalue |= (1ULL << i);
1905 else
1906 return ret;
1909 return ret;
1912 static void handle_AND_op(struct expression *var, sval_t known)
1914 struct range_list *orig_rl;
1915 struct range_list *true_rl = NULL;
1916 struct range_list *false_rl = NULL;
1917 int bit;
1918 sval_t low_mask = known;
1919 sval_t high_mask;
1920 sval_t max;
1922 get_absolute_rl(var, &orig_rl);
1924 if (known.value > 0) {
1925 bit = ffsll(known.value) - 1;
1926 low_mask.uvalue = (1ULL << bit) - 1;
1927 true_rl = remove_range(orig_rl, sval_type_val(known.type, 0), low_mask);
1929 high_mask = get_high_mask(known);
1930 if (high_mask.value) {
1931 bit = ffsll(high_mask.value) - 1;
1932 low_mask.uvalue = (1ULL << bit) - 1;
1934 false_rl = orig_rl;
1935 if (sval_is_negative(rl_min(orig_rl)))
1936 false_rl = remove_range(false_rl, sval_type_min(known.type), sval_type_val(known.type, -1));
1937 false_rl = remove_range(false_rl, low_mask, sval_type_max(known.type));
1938 if (type_signed(high_mask.type) && type_unsigned(rl_type(false_rl))) {
1939 false_rl = remove_range(false_rl,
1940 sval_type_val(rl_type(false_rl), sval_type_max(known.type).uvalue),
1941 sval_type_val(rl_type(false_rl), -1));
1943 } else if (known.value == 1 &&
1944 get_hard_max(var, &max) &&
1945 sval_cmp(max, rl_max(orig_rl)) == 0 &&
1946 max.value & 1) {
1947 false_rl = remove_range(orig_rl, max, max);
1949 set_extra_expr_true_false(var,
1950 true_rl ? alloc_estate_rl(true_rl) : NULL,
1951 false_rl ? alloc_estate_rl(false_rl) : NULL);
1954 static void handle_AND_condition(struct expression *expr)
1956 sval_t known;
1958 if (get_implied_value(expr->left, &known))
1959 handle_AND_op(expr->right, known);
1960 else if (get_implied_value(expr->right, &known))
1961 handle_AND_op(expr->left, known);
1964 static void handle_MOD_condition(struct expression *expr)
1966 struct range_list *orig_rl;
1967 struct range_list *true_rl;
1968 struct range_list *false_rl = NULL;
1969 sval_t right;
1970 sval_t zero = {
1971 .value = 0,
1974 if (!get_implied_value(expr->right, &right) || right.value == 0)
1975 return;
1976 get_absolute_rl(expr->left, &orig_rl);
1978 zero.type = rl_type(orig_rl);
1980 /* We're basically dorking around the min and max here */
1981 true_rl = remove_range(orig_rl, zero, zero);
1982 if (!sval_is_max(rl_max(true_rl)) &&
1983 !(rl_max(true_rl).value % right.value))
1984 true_rl = remove_range(true_rl, rl_max(true_rl), rl_max(true_rl));
1986 if (rl_equiv(true_rl, orig_rl))
1987 true_rl = NULL;
1989 if (sval_is_positive(rl_min(orig_rl)) &&
1990 (rl_max(orig_rl).value - rl_min(orig_rl).value) / right.value < 5) {
1991 sval_t add;
1992 int i;
1994 add = rl_min(orig_rl);
1995 add.value += right.value - (add.value % right.value);
1996 add.value -= right.value;
1998 for (i = 0; i < 5; i++) {
1999 add.value += right.value;
2000 if (add.value > rl_max(orig_rl).value)
2001 break;
2002 add_range(&false_rl, add, add);
2004 } else {
2005 if (rl_min(orig_rl).uvalue != 0 &&
2006 rl_min(orig_rl).uvalue < right.uvalue) {
2007 sval_t chop = right;
2008 chop.value--;
2009 false_rl = remove_range(orig_rl, zero, chop);
2012 if (!sval_is_max(rl_max(orig_rl)) &&
2013 (rl_max(orig_rl).value % right.value)) {
2014 sval_t chop = rl_max(orig_rl);
2015 chop.value -= chop.value % right.value;
2016 chop.value++;
2017 if (!false_rl)
2018 false_rl = clone_rl(orig_rl);
2019 false_rl = remove_range(false_rl, chop, rl_max(orig_rl));
2023 set_extra_expr_true_false(expr->left,
2024 true_rl ? alloc_estate_rl(true_rl) : NULL,
2025 false_rl ? alloc_estate_rl(false_rl) : NULL);
2028 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
2029 void __extra_match_condition(struct expression *expr)
2031 expr = strip_expr(expr);
2032 switch (expr->type) {
2033 case EXPR_CALL:
2034 function_comparison(expr, SPECIAL_NOTEQUAL, zero_expr());
2035 return;
2036 case EXPR_PREOP:
2037 case EXPR_SYMBOL:
2038 case EXPR_DEREF:
2039 handle_comparison(get_type(expr), expr, SPECIAL_NOTEQUAL, zero_expr());
2040 return;
2041 case EXPR_COMPARE:
2042 match_comparison(expr);
2043 return;
2044 case EXPR_ASSIGNMENT:
2045 __extra_match_condition(expr->left);
2046 return;
2047 case EXPR_BINOP:
2048 if (expr->op == '&')
2049 handle_AND_condition(expr);
2050 if (expr->op == '%')
2051 handle_MOD_condition(expr);
2052 return;
2056 static void assume_indexes_are_valid(struct expression *expr)
2058 struct expression *array_expr;
2059 int array_size;
2060 struct expression *offset;
2061 struct symbol *offset_type;
2062 struct range_list *rl_before;
2063 struct range_list *rl_after;
2064 struct range_list *filter = NULL;
2065 sval_t size;
2067 expr = strip_expr(expr);
2068 if (!is_array(expr))
2069 return;
2071 offset = get_array_offset(expr);
2072 offset_type = get_type(offset);
2073 if (offset_type && type_signed(offset_type)) {
2074 filter = alloc_rl(sval_type_min(offset_type),
2075 sval_type_val(offset_type, -1));
2078 array_expr = get_array_base(expr);
2079 array_size = get_real_array_size(array_expr);
2080 if (array_size > 1) {
2081 size = sval_type_val(offset_type, array_size);
2082 add_range(&filter, size, sval_type_max(offset_type));
2085 if (!filter)
2086 return;
2087 get_absolute_rl(offset, &rl_before);
2088 rl_after = rl_filter(rl_before, filter);
2089 if (rl_equiv(rl_before, rl_after))
2090 return;
2091 set_extra_expr_nomod(offset, alloc_estate_rl(rl_after));
2094 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
2095 int implied_not_equal(struct expression *expr, long long val)
2097 return !possibly_false(expr, SPECIAL_NOTEQUAL, value_expr(val));
2100 int implied_not_equal_name_sym(char *name, struct symbol *sym, long long val)
2102 struct smatch_state *estate;
2104 estate = get_state(SMATCH_EXTRA, name, sym);
2105 if (!estate)
2106 return 0;
2107 if (!rl_has_sval(estate_rl(estate), sval_type_val(estate_type(estate), 0)))
2108 return 1;
2109 return 0;
2112 int parent_is_null_var_sym(const char *name, struct symbol *sym)
2114 char buf[256];
2115 char *start;
2116 char *end;
2117 struct smatch_state *state;
2119 strncpy(buf, name, sizeof(buf) - 1);
2120 buf[sizeof(buf) - 1] = '\0';
2122 start = &buf[0];
2123 while (*start == '*') {
2124 start++;
2125 state = get_state(SMATCH_EXTRA, start, sym);
2126 if (!state)
2127 continue;
2128 if (!estate_rl(state))
2129 return 1;
2130 if (estate_min(state).value == 0 &&
2131 estate_max(state).value == 0)
2132 return 1;
2135 start = &buf[0];
2136 while (*start == '&')
2137 start++;
2139 while ((end = strrchr(start, '-'))) {
2140 *end = '\0';
2141 state = __get_state(SMATCH_EXTRA, start, sym);
2142 if (!state)
2143 continue;
2144 if (estate_min(state).value == 0 &&
2145 estate_max(state).value == 0)
2146 return 1;
2148 return 0;
2151 int parent_is_null(struct expression *expr)
2153 struct symbol *sym;
2154 char *var;
2155 int ret = 0;
2157 expr = strip_expr(expr);
2158 var = expr_to_var_sym(expr, &sym);
2159 if (!var || !sym)
2160 goto free;
2161 ret = parent_is_null_var_sym(var, sym);
2162 free:
2163 free_string(var);
2164 return ret;
2167 static int param_used_callback(void *found, int argc, char **argv, char **azColName)
2169 *(int *)found = 1;
2170 return 0;
2173 static int is_kzalloc_info(struct sm_state *sm)
2175 sval_t sval;
2178 * kzalloc() information is treated as special because so there is just
2179 * a lot of stuff initialized to zero and it makes building the database
2180 * take hours and hours.
2182 * In theory, we should just remove this line and not pass any unused
2183 * information, but I'm not sure enough that this code works so I want
2184 * to hold off on that for now.
2186 if (!estate_get_single_value(sm->state, &sval))
2187 return 0;
2188 if (sval.value != 0)
2189 return 0;
2190 return 1;
2193 static int is_really_long(struct sm_state *sm)
2195 const char *p;
2196 int cnt = 0;
2198 p = sm->name;
2199 while ((p = strstr(p, "->"))) {
2200 p += 2;
2201 cnt++;
2204 if (cnt < 3 ||
2205 strlen(sm->name) < 40)
2206 return 0;
2207 return 1;
2210 static int filter_unused_param_value_info(struct expression *call, int param, char *printed_name, struct sm_state *sm)
2212 int found = 0;
2214 /* for function pointers assume everything is used */
2215 if (call->fn->type != EXPR_SYMBOL)
2216 return 0;
2219 * This is to handle __builtin_mul_overflow(). In an ideal world we
2220 * would only need this for invalid code.
2223 if (!call->fn->symbol)
2224 return 0;
2226 if (!is_kzalloc_info(sm) && !is_really_long(sm))
2227 return 0;
2229 run_sql(&param_used_callback, &found,
2230 "select * from return_implies where %s and type = %d and parameter = %d and key = '%s';",
2231 get_static_filter(call->fn->symbol), PARAM_USED, param, printed_name);
2232 if (found)
2233 return 0;
2235 /* If the database is not built yet, then assume everything is used */
2236 run_sql(&param_used_callback, &found,
2237 "select * from return_implies where %s and type = %d;",
2238 get_static_filter(call->fn->symbol), PARAM_USED);
2239 if (!found)
2240 return 0;
2242 return 1;
2245 struct range_list *intersect_with_real_abs_var_sym(const char *name, struct symbol *sym, struct range_list *start)
2247 struct smatch_state *state;
2250 * Here is the difference between implied value and real absolute, say
2251 * you have:
2253 * int a = (u8)x;
2255 * Then you know that a is 0-255. That's real absolute. But you don't
2256 * know for sure that it actually goes up to 255. So it's not implied.
2257 * Implied indicates a degree of certainty.
2259 * But then say you cap "a" at 8. That means you know it goes up to
2260 * 8. So now the implied value is s32min-8. But you can combine it
2261 * with the real absolute to say that actually it's 0-8.
2263 * We are combining it here. But now that I think about it, this is
2264 * probably not the ideal place to combine it because it should proably
2265 * be done earlier. Oh well, this is an improvement on what was there
2266 * before so I'm going to commit this code.
2270 state = get_real_absolute_state_var_sym(name, sym);
2271 if (!state || !estate_rl(state))
2272 return start;
2274 return rl_intersection(estate_rl(state), start);
2277 struct range_list *intersect_with_real_abs_expr(struct expression *expr, struct range_list *start)
2279 struct smatch_state *state;
2280 struct range_list *abs_rl;
2282 state = get_real_absolute_state(expr);
2283 if (!state || !estate_rl(state))
2284 return start;
2286 abs_rl = cast_rl(rl_type(start), estate_rl(state));
2287 return rl_intersection(abs_rl, start);
2290 static void struct_member_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
2292 struct range_list *rl;
2293 sval_t dummy;
2295 if (estate_is_whole(sm->state))
2296 return;
2297 if (filter_unused_param_value_info(call, param, printed_name, sm))
2298 return;
2299 rl = estate_rl(sm->state);
2300 rl = intersect_with_real_abs_var_sym(sm->name, sm->sym, rl);
2301 sql_insert_caller_info(call, PARAM_VALUE, param, printed_name, show_rl(rl));
2302 if (!estate_get_single_value(sm->state, &dummy) && estate_has_hard_max(sm->state))
2303 sql_insert_caller_info(call, HARD_MAX, param, printed_name,
2304 sval_to_str(estate_max(sm->state)));
2305 if (estate_has_fuzzy_max(sm->state))
2306 sql_insert_caller_info(call, FUZZY_MAX, param, printed_name,
2307 sval_to_str(estate_get_fuzzy_max(sm->state)));
2310 static void returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
2312 struct symbol *returned_sym;
2313 char *returned_name;
2314 struct sm_state *sm;
2315 char *compare_str;
2316 char name_buf[256];
2317 char val_buf[256];
2318 int len;
2320 // FIXME handle *$
2322 if (!is_pointer(expr))
2323 return;
2325 returned_name = expr_to_var_sym(expr, &returned_sym);
2326 if (!returned_name || !returned_sym)
2327 goto free;
2328 len = strlen(returned_name);
2330 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
2331 if (!estate_rl(sm->state))
2332 continue;
2333 if (returned_sym != sm->sym)
2334 continue;
2335 if (strncmp(returned_name, sm->name, len) != 0)
2336 continue;
2337 if (sm->name[len] != '-')
2338 continue;
2340 snprintf(name_buf, sizeof(name_buf), "$%s", sm->name + len);
2342 compare_str = name_sym_to_param_comparison(sm->name, sm->sym);
2343 if (!compare_str && estate_is_whole(sm->state))
2344 continue;
2345 snprintf(val_buf, sizeof(val_buf), "%s%s", sm->state->name, compare_str ?: "");
2347 sql_insert_return_states(return_id, return_ranges, PARAM_VALUE,
2348 -1, name_buf, val_buf);
2349 } END_FOR_EACH_SM(sm);
2351 free:
2352 free_string(returned_name);
2355 static void db_limited_before(void)
2357 unmatched_stree = clone_stree(__get_cur_stree());
2360 static void db_limited_after(void)
2362 free_stree(&unmatched_stree);
2365 static int rl_fits_in_type(struct range_list *rl, struct symbol *type)
2367 if (type_bits(rl_type(rl)) <= type_bits(type))
2368 return 1;
2369 if (sval_cmp(rl_max(rl), sval_type_max(type)) > 0)
2370 return 0;
2371 if (sval_is_negative(rl_min(rl)) &&
2372 sval_cmp(rl_min(rl), sval_type_min(type)) < 0)
2373 return 0;
2374 return 1;
2377 static int basically_the_same(struct range_list *orig, struct range_list *new)
2379 if (rl_equiv(orig, new))
2380 return 1;
2383 * The whole range is essentially the same as 0,4096-27777777777 so
2384 * don't overwrite the implications just to store that.
2387 if (rl_type(orig)->type == SYM_PTR &&
2388 is_whole_rl(orig) &&
2389 rl_min(new).value == 0 &&
2390 rl_max(new).value == valid_ptr_max)
2391 return 1;
2392 return 0;
2395 static void db_param_limit_binops(struct expression *arg, char *key, struct range_list *rl)
2397 struct range_list *left_rl;
2398 sval_t zero = { .type = rl_type(rl), };
2399 sval_t sval;
2401 if (arg->op != '*')
2402 return;
2403 if (!get_implied_value(arg->right, &sval))
2404 return;
2405 if (can_integer_overflow(get_type(arg), arg))
2406 return;
2408 left_rl = rl_binop(rl, '/', alloc_rl(sval, sval));
2409 if (!rl_has_sval(rl, zero))
2410 left_rl = remove_range(left_rl, zero, zero);
2412 set_extra_expr_nomod(arg->left, alloc_estate_rl(left_rl));
2415 static void db_param_limit_filter(struct expression *expr, int param, char *key, char *value, enum info_type op)
2417 struct expression *arg;
2418 char *name;
2419 struct symbol *sym;
2420 struct var_sym_list *vsl = NULL;
2421 struct sm_state *sm;
2422 struct symbol *compare_type, *var_type;
2423 struct range_list *rl;
2424 struct range_list *limit;
2425 struct range_list *new;
2426 char *other_name;
2427 struct symbol *other_sym;
2429 while (expr->type == EXPR_ASSIGNMENT)
2430 expr = strip_expr(expr->right);
2431 if (expr->type != EXPR_CALL)
2432 return;
2434 arg = get_argument_from_call_expr(expr->args, param);
2435 if (!arg)
2436 return;
2438 name = get_chunk_from_key(arg, key, &sym, &vsl);
2439 if (!name)
2440 return;
2441 if (op != PARAM_LIMIT && !sym)
2442 goto free;
2444 if (strcmp(key, "$") == 0)
2445 compare_type = get_arg_type(expr->fn, param);
2446 else
2447 compare_type = get_member_type_from_key(arg, key);
2449 sm = get_sm_state(SMATCH_EXTRA, name, sym);
2450 if (sm)
2451 rl = estate_rl(sm->state);
2452 else
2453 rl = alloc_whole_rl(compare_type);
2455 if (op == PARAM_LIMIT && !rl_fits_in_type(rl, compare_type))
2456 goto free;
2458 call_results_to_rl(expr, compare_type, value, &limit);
2459 new = rl_intersection(rl, limit);
2461 var_type = get_member_type_from_key(arg, key);
2462 new = cast_rl(var_type, new);
2464 /* We want to preserve the implications here */
2465 if (sm && basically_the_same(estate_rl(sm->state), new))
2466 goto free;
2467 other_name = map_long_to_short_name_sym(name, sym, &other_sym);
2469 if (op == PARAM_LIMIT)
2470 set_extra_nomod_vsl(name, sym, vsl, NULL, alloc_estate_rl(new));
2471 else
2472 set_extra_mod(name, sym, NULL, alloc_estate_rl(new));
2474 if (other_name && other_sym) {
2475 if (op == PARAM_LIMIT)
2476 set_extra_nomod_vsl(other_name, other_sym, vsl, NULL, alloc_estate_rl(new));
2477 else
2478 set_extra_mod(other_name, other_sym, NULL, alloc_estate_rl(new));
2481 if (op == PARAM_LIMIT && arg->type == EXPR_BINOP)
2482 db_param_limit_binops(arg, key, new);
2483 free:
2484 free_string(name);
2487 static void db_param_limit(struct expression *expr, int param, char *key, char *value)
2489 db_param_limit_filter(expr, param, key, value, PARAM_LIMIT);
2492 static void db_param_filter(struct expression *expr, int param, char *key, char *value)
2494 db_param_limit_filter(expr, param, key, value, PARAM_FILTER);
2497 static void db_param_add_set(struct expression *expr, int param, char *key, char *value, enum info_type op)
2499 struct expression *arg;
2500 char *name;
2501 char *other_name = NULL;
2502 struct symbol *sym, *other_sym;
2503 struct symbol *param_type, *arg_type;
2504 struct smatch_state *state;
2505 struct range_list *new = NULL;
2506 struct range_list *added = NULL;
2508 while (expr->type == EXPR_ASSIGNMENT)
2509 expr = strip_expr(expr->right);
2510 if (expr->type != EXPR_CALL)
2511 return;
2513 arg = get_argument_from_call_expr(expr->args, param);
2514 if (!arg)
2515 return;
2517 arg_type = get_arg_type_from_key(expr->fn, param, arg, key);
2518 param_type = get_member_type_from_key(arg, key);
2519 name = get_variable_from_key(arg, key, &sym);
2520 if (!name || !sym)
2521 goto free;
2523 state = get_state(SMATCH_EXTRA, name, sym);
2524 if (state)
2525 new = estate_rl(state);
2527 call_results_to_rl(expr, arg_type, value, &added);
2528 added = cast_rl(param_type, added);
2529 if (op == PARAM_SET)
2530 new = added;
2531 else
2532 new = rl_union(new, added);
2534 other_name = map_long_to_short_name_sym_nostack(name, sym, &other_sym);
2535 set_extra_mod(name, sym, NULL, alloc_estate_rl(new));
2536 if (other_name && other_sym)
2537 set_extra_mod(other_name, other_sym, NULL, alloc_estate_rl(new));
2538 free:
2539 free_string(other_name);
2540 free_string(name);
2543 static void db_param_add(struct expression *expr, int param, char *key, char *value)
2545 in_param_set = true;
2546 db_param_add_set(expr, param, key, value, PARAM_ADD);
2547 in_param_set = false;
2550 static void db_param_set(struct expression *expr, int param, char *key, char *value)
2552 in_param_set = true;
2553 db_param_add_set(expr, param, key, value, PARAM_SET);
2554 in_param_set = false;
2557 static void match_lost_param(struct expression *call, int param)
2559 struct expression *arg;
2561 if (is_const_param(call->fn, param))
2562 return;
2564 arg = get_argument_from_call_expr(call->args, param);
2565 if (!arg)
2566 return;
2568 arg = strip_expr(arg);
2569 if (arg->type == EXPR_PREOP && arg->op == '&')
2570 set_extra_expr_mod(arg->unop, alloc_estate_whole(get_type(arg->unop)));
2571 else
2572 ; /* if pointer then set struct members, maybe?*/
2575 static void db_param_value(struct expression *expr, int param, char *key, char *value)
2577 struct expression *call;
2578 char *name;
2579 struct symbol *sym;
2580 struct symbol *type;
2581 struct range_list *rl = NULL;
2583 if (param != -1)
2584 return;
2586 call = expr;
2587 while (call->type == EXPR_ASSIGNMENT)
2588 call = strip_expr(call->right);
2589 if (call->type != EXPR_CALL)
2590 return;
2592 type = get_member_type_from_key(expr->left, key);
2593 name = get_variable_from_key(expr->left, key, &sym);
2594 if (!name || !sym)
2595 goto free;
2597 call_results_to_rl(call, type, value, &rl);
2599 set_extra_mod(name, sym, NULL, alloc_estate_rl(rl));
2600 free:
2601 free_string(name);
2604 static void match_call_info(struct expression *expr)
2606 struct smatch_state *state;
2607 struct range_list *rl = NULL;
2608 struct expression *arg;
2609 struct symbol *type;
2610 sval_t dummy;
2611 int i = 0;
2613 FOR_EACH_PTR(expr->args, arg) {
2614 type = get_arg_type(expr->fn, i);
2616 get_absolute_rl(arg, &rl);
2617 rl = cast_rl(type, rl);
2619 if (!is_whole_rl(rl)) {
2620 rl = intersect_with_real_abs_expr(arg, rl);
2621 sql_insert_caller_info(expr, PARAM_VALUE, i, "$", show_rl(rl));
2623 state = get_state_expr(SMATCH_EXTRA, arg);
2624 if (!estate_get_single_value(state, &dummy) && estate_has_hard_max(state)) {
2625 sql_insert_caller_info(expr, HARD_MAX, i, "$",
2626 sval_to_str(estate_max(state)));
2628 if (estate_has_fuzzy_max(state)) {
2629 sql_insert_caller_info(expr, FUZZY_MAX, i, "$",
2630 sval_to_str(estate_get_fuzzy_max(state)));
2632 i++;
2633 } END_FOR_EACH_PTR(arg);
2636 static void set_param_value(const char *name, struct symbol *sym, char *key, char *value)
2638 struct range_list *rl = NULL;
2639 struct smatch_state *state;
2640 struct symbol *type;
2641 char fullname[256];
2642 sval_t dummy;
2644 if (strcmp(key, "*$") == 0)
2645 snprintf(fullname, sizeof(fullname), "*%s", name);
2646 else if (strncmp(key, "$", 1) == 0)
2647 snprintf(fullname, 256, "%s%s", name, key + 1);
2648 else
2649 return;
2651 type = get_member_type_from_key(symbol_expression(sym), key);
2652 str_to_rl(type, value, &rl);
2653 state = alloc_estate_rl(rl);
2654 if (estate_get_single_value(state, &dummy))
2655 estate_set_hard_max(state);
2656 set_state(SMATCH_EXTRA, fullname, sym, state);
2659 static void set_param_fuzzy_max(const char *name, struct symbol *sym, char *key, char *value)
2661 struct range_list *rl = NULL;
2662 struct smatch_state *state;
2663 struct symbol *type;
2664 char fullname[256];
2665 sval_t max;
2667 if (strcmp(key, "*$") == 0)
2668 snprintf(fullname, sizeof(fullname), "*%s", name);
2669 else if (strncmp(key, "$", 1) == 0)
2670 snprintf(fullname, 256, "%s%s", name, key + 1);
2671 else
2672 return;
2674 state = get_state(SMATCH_EXTRA, fullname, sym);
2675 if (!state)
2676 return;
2677 type = estate_type(state);
2678 str_to_rl(type, value, &rl);
2679 if (!rl_to_sval(rl, &max))
2680 return;
2681 estate_set_fuzzy_max(state, max);
2684 static void set_param_hard_max(const char *name, struct symbol *sym, char *key, char *value)
2686 struct smatch_state *state;
2687 char fullname[256];
2689 if (strcmp(key, "*$") == 0)
2690 snprintf(fullname, sizeof(fullname), "*%s", name);
2691 else if (strncmp(key, "$", 1) == 0)
2692 snprintf(fullname, 256, "%s%s", name, key + 1);
2693 else
2694 return;
2696 state = get_state(SMATCH_EXTRA, fullname, sym);
2697 if (!state)
2698 return;
2699 estate_set_hard_max(state);
2702 struct sm_state *get_extra_sm_state(struct expression *expr)
2704 char *name;
2705 struct symbol *sym;
2706 struct sm_state *ret = NULL;
2708 name = expr_to_known_chunk_sym(expr, &sym);
2709 if (!name)
2710 goto free;
2712 ret = get_sm_state(SMATCH_EXTRA, name, sym);
2713 free:
2714 free_string(name);
2715 return ret;
2718 struct smatch_state *get_extra_state(struct expression *expr)
2720 struct sm_state *sm;
2722 sm = get_extra_sm_state(expr);
2723 if (!sm)
2724 return NULL;
2725 return sm->state;
2728 void register_smatch_extra(int id)
2730 my_id = id;
2732 set_dynamic_states(my_id);
2733 add_merge_hook(my_id, &merge_estates);
2734 add_unmatched_state_hook(my_id, &unmatched_state);
2735 select_caller_info_hook(set_param_value, PARAM_VALUE);
2736 select_caller_info_hook(set_param_fuzzy_max, FUZZY_MAX);
2737 select_caller_info_hook(set_param_hard_max, HARD_MAX);
2738 select_return_states_before(&db_limited_before);
2739 select_return_states_hook(PARAM_LIMIT, &db_param_limit);
2740 select_return_states_hook(PARAM_FILTER, &db_param_filter);
2741 select_return_states_hook(PARAM_ADD, &db_param_add);
2742 select_return_states_hook(PARAM_SET, &db_param_set);
2743 add_lost_param_hook(&match_lost_param);
2744 select_return_states_hook(PARAM_VALUE, &db_param_value);
2745 select_return_states_after(&db_limited_after);
2748 static void match_link_modify(struct sm_state *sm, struct expression *mod_expr)
2750 struct var_sym_list *links;
2751 struct var_sym *tmp;
2752 struct smatch_state *state;
2754 links = sm->state->data;
2756 FOR_EACH_PTR(links, tmp) {
2757 if (sm->sym == tmp->sym &&
2758 strcmp(sm->name, tmp->var) == 0)
2759 continue;
2760 state = get_state(SMATCH_EXTRA, tmp->var, tmp->sym);
2761 if (!state)
2762 continue;
2763 set_state(SMATCH_EXTRA, tmp->var, tmp->sym, alloc_estate_whole(estate_type(state)));
2764 } END_FOR_EACH_PTR(tmp);
2765 set_state(link_id, sm->name, sm->sym, &undefined);
2768 void register_smatch_extra_links(int id)
2770 link_id = id;
2771 set_dynamic_states(link_id);
2774 void register_smatch_extra_late(int id)
2776 add_merge_hook(link_id, &merge_link_states);
2777 add_modification_hook(link_id, &match_link_modify);
2778 add_hook(&match_dereferences, DEREF_HOOK);
2779 add_hook(&match_pointer_as_array, OP_HOOK);
2780 select_return_implies_hook(DEREFERENCE, &set_param_dereferenced);
2781 add_hook(&match_function_call, FUNCTION_CALL_HOOK);
2782 add_hook(&match_assign, ASSIGNMENT_HOOK);
2783 add_hook(&match_assign, GLOBAL_ASSIGNMENT_HOOK);
2784 add_hook(&unop_expr, OP_HOOK);
2785 add_hook(&asm_expr, ASM_HOOK);
2787 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
2788 add_member_info_callback(my_id, struct_member_callback);
2789 add_split_return_callback(&returned_struct_members);
2791 // add_hook(&assume_indexes_are_valid, OP_HOOK);