flow: cleanup state backup for inline functions
[smatch.git] / smatch_extra.c
blob27d445fc26df8286290a798ed5c83e8376bd5689
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 static 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 smatch_state *state);
64 DECLARE_PTR_LIST(void_fn_list, mod_hook *);
65 static struct void_fn_list *extra_mod_hooks;
66 void add_extra_mod_hook(mod_hook *fn)
68 mod_hook **p = malloc(sizeof(mod_hook *));
69 *p = fn;
70 add_ptr_list(&extra_mod_hooks, p);
73 void call_extra_mod_hooks(const char *name, struct symbol *sym, struct smatch_state *state)
75 mod_hook **fn;
77 FOR_EACH_PTR(extra_mod_hooks, fn) {
78 (*fn)(name, sym, state);
79 } END_FOR_EACH_PTR(fn);
82 static void set_extra_mod_helper(const char *name, struct symbol *sym, struct smatch_state *state)
84 remove_from_equiv(name, sym);
85 call_extra_mod_hooks(name, sym, state);
86 if (__in_fake_assign && estate_is_unknown(state) && !get_state(SMATCH_EXTRA, name, sym))
87 return;
88 set_state(SMATCH_EXTRA, name, sym, state);
91 static char *get_pointed_at(const char *name, struct symbol *sym, struct symbol **new_sym)
93 struct expression *assigned;
95 if (name[0] != '*')
96 return NULL;
97 if (strcmp(name + 1, sym->ident->name) != 0)
98 return NULL;
100 assigned = get_assigned_expr_name_sym(sym->ident->name, sym);
101 if (!assigned)
102 return NULL;
103 assigned = strip_parens(assigned);
104 if (assigned->type != EXPR_PREOP || assigned->op != '&')
105 return NULL;
107 return expr_to_var_sym(assigned->unop, new_sym);
110 char *get_other_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym)
112 struct expression *assigned;
113 char *orig_name = NULL;
114 char buf[256];
115 char *ret = NULL;
116 int skip;
118 *new_sym = NULL;
120 if (!sym || !sym->ident)
121 return NULL;
123 ret = get_pointed_at(name, sym, new_sym);
124 if (ret)
125 return ret;
127 skip = strlen(sym->ident->name);
128 if (name[skip] != '-' || name[skip + 1] != '>')
129 return NULL;
130 skip += 2;
132 assigned = get_assigned_expr_name_sym(sym->ident->name, sym);
133 if (!assigned)
134 return NULL;
135 if (assigned->type == EXPR_CALL)
136 return map_call_to_other_name_sym(name, sym, new_sym);
137 if (assigned->type == EXPR_PREOP || assigned->op == '&') {
139 orig_name = expr_to_var_sym(assigned, new_sym);
140 if (!orig_name || !*new_sym)
141 goto free;
143 snprintf(buf, sizeof(buf), "%s.%s", orig_name + 1, name + skip);
144 ret = alloc_string(buf);
145 free_string(orig_name);
146 return ret;
149 if (assigned->type != EXPR_DEREF)
150 goto free;
152 orig_name = expr_to_var_sym(assigned, new_sym);
153 if (!orig_name || !*new_sym)
154 goto free;
156 snprintf(buf, sizeof(buf), "%s->%s", orig_name, name + skip);
157 ret = alloc_string(buf);
158 free_string(orig_name);
159 return ret;
161 free:
162 free_string(orig_name);
163 return NULL;
166 void set_extra_mod(const char *name, struct symbol *sym, struct smatch_state *state)
168 char *new_name;
169 struct symbol *new_sym;
171 set_extra_mod_helper(name, sym, state);
172 new_name = get_other_name_sym(name, sym, &new_sym);
173 if (new_name && new_sym)
174 set_extra_mod_helper(new_name, new_sym, state);
175 free_string(new_name);
178 static void clear_array_states(struct expression *array)
180 struct sm_state *sm;
182 sm = get_sm_state_expr(link_id, array);
183 if (sm)
184 match_link_modify(sm, NULL);
187 static void set_extra_array_mod(struct expression *expr, struct smatch_state *state)
189 struct expression *array;
190 struct var_sym_list *vsl;
191 struct var_sym *vs;
192 char *name;
193 struct symbol *sym;
195 array = get_array_base(expr);
197 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
198 if (!name || !vsl) {
199 clear_array_states(array);
200 goto free;
203 FOR_EACH_PTR(vsl, vs) {
204 store_link(link_id, vs->var, vs->sym, name, sym);
205 } END_FOR_EACH_PTR(vs);
207 call_extra_mod_hooks(name, sym, state);
208 set_state(SMATCH_EXTRA, name, sym, state);
209 free:
210 free_string(name);
213 void set_extra_expr_mod(struct expression *expr, struct smatch_state *state)
215 struct symbol *sym;
216 char *name;
218 if (is_array(expr)) {
219 set_extra_array_mod(expr, state);
220 return;
223 expr = strip_expr(expr);
224 name = expr_to_var_sym(expr, &sym);
225 if (!name || !sym)
226 goto free;
227 set_extra_mod(name, sym, state);
228 free:
229 free_string(name);
232 void set_extra_nomod(const char *name, struct symbol *sym, struct smatch_state *state)
234 char *new_name;
235 struct symbol *new_sym;
236 struct relation *rel;
237 struct smatch_state *orig_state;
239 orig_state = get_state(SMATCH_EXTRA, name, sym);
241 new_name = get_other_name_sym(name, sym, &new_sym);
242 if (new_name && new_sym)
243 set_state(SMATCH_EXTRA, new_name, new_sym, state);
244 free_string(new_name);
246 if (!estate_related(orig_state)) {
247 set_state(SMATCH_EXTRA, name, sym, state);
248 return;
251 set_related(state, estate_related(orig_state));
252 FOR_EACH_PTR(estate_related(orig_state), rel) {
253 struct smatch_state *estate;
255 if (option_debug_related)
256 sm_msg("%s updating related %s to %s", name, rel->name, state->name);
257 estate = get_state(SMATCH_EXTRA, rel->name, rel->sym);
258 if (!estate)
259 continue;
260 set_state(SMATCH_EXTRA, rel->name, rel->sym, clone_estate_cast(estate_type(estate), state));
261 } END_FOR_EACH_PTR(rel);
264 void set_extra_nomod_vsl(const char *name, struct symbol *sym, struct var_sym_list *vsl, struct smatch_state *state)
266 struct var_sym *vs;
268 FOR_EACH_PTR(vsl, vs) {
269 store_link(link_id, vs->var, vs->sym, name, sym);
270 } END_FOR_EACH_PTR(vs);
272 set_extra_nomod(name, sym, state);
276 * This is for return_implies_state() hooks which modify a SMATCH_EXTRA state
278 void set_extra_expr_nomod(struct expression *expr, struct smatch_state *state)
280 struct var_sym_list *vsl;
281 struct var_sym *vs;
282 char *name;
283 struct symbol *sym;
285 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
286 if (!name || !vsl)
287 goto free;
288 FOR_EACH_PTR(vsl, vs) {
289 store_link(link_id, vs->var, vs->sym, name, sym);
290 } END_FOR_EACH_PTR(vs);
292 set_extra_nomod(name, sym, state);
293 free:
294 free_string(name);
297 static void set_extra_true_false(const char *name, struct symbol *sym,
298 struct smatch_state *true_state,
299 struct smatch_state *false_state)
301 char *new_name;
302 struct symbol *new_sym;
303 struct relation *rel;
304 struct smatch_state *orig_state;
306 if (!true_state && !false_state)
307 return;
309 if (in_warn_on_macro())
310 return;
312 new_name = get_other_name_sym(name, sym, &new_sym);
313 if (new_name && new_sym)
314 set_true_false_states(SMATCH_EXTRA, new_name, new_sym, true_state, false_state);
315 free_string(new_name);
317 orig_state = get_state(SMATCH_EXTRA, name, sym);
319 if (!estate_related(orig_state)) {
320 set_true_false_states(SMATCH_EXTRA, name, sym, true_state, false_state);
321 return;
324 if (true_state)
325 set_related(true_state, estate_related(orig_state));
326 if (false_state)
327 set_related(false_state, estate_related(orig_state));
329 FOR_EACH_PTR(estate_related(orig_state), rel) {
330 set_true_false_states(SMATCH_EXTRA, rel->name, rel->sym,
331 true_state, false_state);
332 } END_FOR_EACH_PTR(rel);
335 static void set_extra_chunk_true_false(struct expression *expr,
336 struct smatch_state *true_state,
337 struct smatch_state *false_state)
339 struct var_sym_list *vsl;
340 struct var_sym *vs;
341 struct symbol *type;
342 char *name;
343 struct symbol *sym;
345 if (in_warn_on_macro())
346 return;
348 type = get_type(expr);
349 if (!type)
350 return;
352 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
353 if (!name || !vsl)
354 goto free;
355 FOR_EACH_PTR(vsl, vs) {
356 store_link(link_id, vs->var, vs->sym, name, sym);
357 } END_FOR_EACH_PTR(vs);
359 set_true_false_states(SMATCH_EXTRA, name, sym,
360 clone_estate(true_state),
361 clone_estate(false_state));
362 free:
363 free_string(name);
366 static void set_extra_expr_true_false(struct expression *expr,
367 struct smatch_state *true_state,
368 struct smatch_state *false_state)
370 char *name;
371 struct symbol *sym;
372 sval_t sval;
374 if (!true_state && !false_state)
375 return;
377 if (get_value(expr, &sval))
378 return;
380 expr = strip_expr(expr);
381 name = expr_to_var_sym(expr, &sym);
382 if (!name || !sym) {
383 free_string(name);
384 set_extra_chunk_true_false(expr, true_state, false_state);
385 return;
387 set_extra_true_false(name, sym, true_state, false_state);
388 free_string(name);
391 static struct sm_state *handle_canonical_while_count_down(struct statement *loop)
393 struct expression *iter_var;
394 struct expression *condition;
395 struct sm_state *sm;
396 struct smatch_state *estate;
397 sval_t start;
399 condition = strip_expr(loop->iterator_pre_condition);
400 if (!condition)
401 return NULL;
402 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
403 return NULL;
404 if (condition->op != SPECIAL_DECREMENT)
405 return NULL;
407 iter_var = condition->unop;
408 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
409 if (!sm)
410 return NULL;
411 if (sval_cmp_val(estate_min(sm->state), 0) < 0)
412 return NULL;
413 start = estate_max(sm->state);
414 if (sval_cmp_val(start, 0) <= 0)
415 return NULL;
416 if (!sval_is_max(start))
417 start.value--;
419 if (condition->type == EXPR_PREOP) {
420 estate = alloc_estate_range(sval_type_val(start.type, 1), start);
421 if (estate_has_hard_max(sm->state))
422 estate_set_hard_max(estate);
423 estate_copy_fuzzy_max(estate, sm->state);
424 set_extra_expr_mod(iter_var, estate);
426 if (condition->type == EXPR_POSTOP) {
427 estate = alloc_estate_range(sval_type_val(start.type, 0), start);
428 if (estate_has_hard_max(sm->state))
429 estate_set_hard_max(estate);
430 estate_copy_fuzzy_max(estate, sm->state);
431 set_extra_expr_mod(iter_var, estate);
433 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
436 static struct sm_state *handle_canonical_for_inc(struct expression *iter_expr,
437 struct expression *condition)
439 struct expression *iter_var;
440 struct sm_state *sm;
441 struct smatch_state *estate;
442 sval_t start, end, max;
444 iter_var = iter_expr->unop;
445 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
446 if (!sm)
447 return NULL;
448 if (!estate_get_single_value(sm->state, &start))
449 return NULL;
450 if (get_implied_max(condition->right, &end))
451 end = sval_cast(get_type(iter_var), end);
452 else
453 end = sval_type_max(get_type(iter_var));
455 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
456 return NULL;
458 switch (condition->op) {
459 case SPECIAL_UNSIGNED_LT:
460 case SPECIAL_NOTEQUAL:
461 case '<':
462 if (!sval_is_min(end))
463 end.value--;
464 break;
465 case SPECIAL_UNSIGNED_LTE:
466 case SPECIAL_LTE:
467 break;
468 default:
469 return NULL;
471 if (sval_cmp(end, start) < 0)
472 return NULL;
473 estate = alloc_estate_range(start, end);
474 if (get_hard_max(condition->right, &max)) {
475 estate_set_hard_max(estate);
476 if (condition->op == '<' ||
477 condition->op == SPECIAL_UNSIGNED_LT ||
478 condition->op == SPECIAL_NOTEQUAL)
479 max.value--;
480 estate_set_fuzzy_max(estate, max);
482 set_extra_expr_mod(iter_var, estate);
483 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
486 static struct sm_state *handle_canonical_for_dec(struct expression *iter_expr,
487 struct expression *condition)
489 struct expression *iter_var;
490 struct sm_state *sm;
491 struct smatch_state *estate;
492 sval_t start, end;
494 iter_var = iter_expr->unop;
495 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
496 if (!sm)
497 return NULL;
498 if (!estate_get_single_value(sm->state, &start))
499 return NULL;
500 if (!get_implied_min(condition->right, &end))
501 end = sval_type_min(get_type(iter_var));
502 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
503 return NULL;
505 switch (condition->op) {
506 case SPECIAL_NOTEQUAL:
507 case '>':
508 if (!sval_is_min(end) && !sval_is_max(end))
509 end.value++;
510 break;
511 case SPECIAL_GTE:
512 break;
513 default:
514 return NULL;
516 if (sval_cmp(end, start) > 0)
517 return NULL;
518 estate = alloc_estate_range(end, start);
519 estate_set_hard_max(estate);
520 estate_set_fuzzy_max(estate, estate_get_fuzzy_max(estate));
521 set_extra_expr_mod(iter_var, estate);
522 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
525 static struct sm_state *handle_canonical_for_loops(struct statement *loop)
527 struct expression *iter_expr;
528 struct expression *condition;
530 if (!loop->iterator_post_statement)
531 return NULL;
532 if (loop->iterator_post_statement->type != STMT_EXPRESSION)
533 return NULL;
534 iter_expr = loop->iterator_post_statement->expression;
535 if (!loop->iterator_pre_condition)
536 return NULL;
537 if (loop->iterator_pre_condition->type != EXPR_COMPARE)
538 return NULL;
539 condition = loop->iterator_pre_condition;
541 if (iter_expr->op == SPECIAL_INCREMENT)
542 return handle_canonical_for_inc(iter_expr, condition);
543 if (iter_expr->op == SPECIAL_DECREMENT)
544 return handle_canonical_for_dec(iter_expr, condition);
545 return NULL;
548 struct sm_state *__extra_handle_canonical_loops(struct statement *loop, struct stree **stree)
550 struct sm_state *ret;
552 __push_fake_cur_stree();
553 if (!loop->iterator_post_statement)
554 ret = handle_canonical_while_count_down(loop);
555 else
556 ret = handle_canonical_for_loops(loop);
557 *stree = __pop_fake_cur_stree();
558 return ret;
561 int __iterator_unchanged(struct sm_state *sm)
563 if (!sm)
564 return 0;
565 if (get_sm_state(my_id, sm->name, sm->sym) == sm)
566 return 1;
567 return 0;
570 static void while_count_down_after(struct sm_state *sm, struct expression *condition)
572 sval_t after_value;
574 /* paranoid checking. prolly not needed */
575 condition = strip_expr(condition);
576 if (!condition)
577 return;
578 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
579 return;
580 if (condition->op != SPECIAL_DECREMENT)
581 return;
582 after_value = estate_min(sm->state);
583 after_value.value--;
584 set_extra_mod(sm->name, sm->sym, alloc_estate_sval(after_value));
587 void __extra_pre_loop_hook_after(struct sm_state *sm,
588 struct statement *iterator,
589 struct expression *condition)
591 struct expression *iter_expr;
592 sval_t limit;
593 struct smatch_state *state;
595 if (!iterator) {
596 while_count_down_after(sm, condition);
597 return;
600 iter_expr = iterator->expression;
602 if (condition->type != EXPR_COMPARE)
603 return;
604 if (iter_expr->op == SPECIAL_INCREMENT) {
605 limit = sval_binop(estate_max(sm->state), '+',
606 sval_type_val(estate_type(sm->state), 1));
607 } else {
608 limit = sval_binop(estate_min(sm->state), '-',
609 sval_type_val(estate_type(sm->state), 1));
611 if (!estate_has_hard_max(sm->state) && !__has_breaks()) {
612 if (iter_expr->op == SPECIAL_INCREMENT)
613 state = alloc_estate_range(estate_min(sm->state), limit);
614 else
615 state = alloc_estate_range(limit, estate_max(sm->state));
616 } else {
617 state = alloc_estate_sval(limit);
619 if (!estate_has_hard_max(sm->state)) {
620 estate_clear_hard_max(state);
622 if (estate_has_fuzzy_max(sm->state)) {
623 sval_t hmax = estate_get_fuzzy_max(sm->state);
624 sval_t max = estate_max(sm->state);
626 if (sval_cmp(hmax, max) != 0)
627 estate_clear_fuzzy_max(state);
628 } else if (!estate_has_fuzzy_max(sm->state)) {
629 estate_clear_fuzzy_max(state);
632 set_extra_mod(sm->name, sm->sym, state);
635 static struct stree *unmatched_stree;
636 static struct smatch_state *unmatched_state(struct sm_state *sm)
638 struct smatch_state *state;
640 if (unmatched_stree) {
641 state = get_state_stree(unmatched_stree, SMATCH_EXTRA, sm->name, sm->sym);
642 if (state)
643 return state;
645 if (parent_is_gone_var_sym(sm->name, sm->sym))
646 return alloc_estate_empty();
647 return alloc_estate_whole(estate_type(sm->state));
650 static void clear_the_pointed_at(struct expression *expr)
652 struct stree *stree;
653 char *name;
654 struct symbol *sym;
655 struct sm_state *tmp;
657 name = expr_to_var_sym(expr, &sym);
658 if (!name || !sym)
659 goto free;
661 stree = __get_cur_stree();
662 FOR_EACH_MY_SM(SMATCH_EXTRA, stree, tmp) {
663 if (tmp->name[0] != '*')
664 continue;
665 if (tmp->sym != sym)
666 continue;
667 if (strcmp(tmp->name + 1, name) != 0)
668 continue;
669 set_extra_mod(tmp->name, tmp->sym, alloc_estate_whole(estate_type(tmp->state)));
670 } END_FOR_EACH_SM(tmp);
672 free:
673 free_string(name);
676 static void match_function_call(struct expression *expr)
678 struct expression *arg;
679 struct expression *tmp;
681 /* if we have the db this is handled in smatch_function_hooks.c */
682 if (!option_no_db)
683 return;
684 if (inlinable(expr->fn))
685 return;
687 FOR_EACH_PTR(expr->args, arg) {
688 tmp = strip_expr(arg);
689 if (tmp->type == EXPR_PREOP && tmp->op == '&')
690 set_extra_expr_mod(tmp->unop, alloc_estate_whole(get_type(tmp->unop)));
691 else
692 clear_the_pointed_at(tmp);
693 } END_FOR_EACH_PTR(arg);
696 static int values_fit_type(struct expression *left, struct expression *right)
698 struct range_list *rl;
699 struct symbol *type;
701 type = get_type(left);
702 if (!type)
703 return 0;
704 get_absolute_rl(right, &rl);
705 if (type_unsigned(type) && sval_is_negative(rl_min(rl)))
706 return 0;
707 if (sval_cmp(sval_type_min(type), rl_min(rl)) > 0)
708 return 0;
709 if (sval_cmp(sval_type_max(type), rl_max(rl)) < 0)
710 return 0;
711 return 1;
714 static void save_chunk_info(struct expression *left, struct expression *right)
716 struct var_sym_list *vsl;
717 struct var_sym *vs;
718 struct expression *add_expr;
719 struct symbol *type;
720 sval_t sval;
721 char *name;
722 struct symbol *sym;
724 if (right->type != EXPR_BINOP || right->op != '-')
725 return;
726 if (!get_value(right->left, &sval))
727 return;
728 if (!expr_to_sym(right->right))
729 return;
731 add_expr = binop_expression(left, '+', right->right);
732 type = get_type(add_expr);
733 if (!type)
734 return;
735 name = expr_to_chunk_sym_vsl(add_expr, &sym, &vsl);
736 if (!name || !vsl)
737 goto free;
738 FOR_EACH_PTR(vsl, vs) {
739 store_link(link_id, vs->var, vs->sym, name, sym);
740 } END_FOR_EACH_PTR(vs);
742 set_state(SMATCH_EXTRA, name, sym, alloc_estate_sval(sval_cast(type, sval)));
743 free:
744 free_string(name);
747 static void do_array_assign(struct expression *left, int op, struct expression *right)
749 struct range_list *rl;
751 if (op == '=') {
752 get_absolute_rl(right, &rl);
753 rl = cast_rl(get_type(left), rl);
754 } else {
755 rl = alloc_whole_rl(get_type(left));
758 set_extra_array_mod(left, alloc_estate_rl(rl));
761 static void match_untracked_array(struct expression *call, int param)
763 struct expression *arg;
765 arg = get_argument_from_call_expr(call->args, param);
766 arg = strip_expr(arg);
768 clear_array_states(arg);
771 static void match_vanilla_assign(struct expression *left, struct expression *right)
773 struct range_list *orig_rl = NULL;
774 struct range_list *rl = NULL;
775 struct symbol *right_sym;
776 struct symbol *left_type;
777 struct symbol *right_type;
778 char *right_name = NULL;
779 struct symbol *sym;
780 char *name;
781 sval_t max;
782 struct smatch_state *state;
783 int comparison;
785 if (is_struct(left))
786 return;
788 save_chunk_info(left, right);
790 name = expr_to_var_sym(left, &sym);
791 if (!name) {
792 if (is_array(left))
793 do_array_assign(left, '=', right);
794 return;
797 left_type = get_type(left);
798 right_type = get_type(right);
800 right_name = expr_to_var_sym(right, &right_sym);
802 if (!__in_fake_assign &&
803 !(right->type == EXPR_PREOP && right->op == '&') &&
804 right_name && right_sym &&
805 values_fit_type(left, right) &&
806 !has_symbol(right, sym)) {
807 set_equiv(left, right);
808 goto free;
811 if (is_pointer(right) && get_address_rl(right, &rl)) {
812 state = alloc_estate_rl(rl);
813 goto done;
816 if (__in_fake_assign) {
817 struct smatch_state *right_state;
818 sval_t sval;
820 if (get_value(right, &sval)) {
821 sval = sval_cast(left_type, sval);
822 state = alloc_estate_sval(sval);
823 goto done;
826 right_state = get_state(SMATCH_EXTRA, right_name, right_sym);
827 if (right_state) {
828 /* simple assignment */
829 state = clone_estate(right_state);
830 goto done;
833 state = alloc_estate_rl(alloc_whole_rl(left_type));
834 goto done;
837 comparison = get_comparison(left, right);
838 if (comparison) {
839 comparison = flip_comparison(comparison);
840 get_implied_rl(left, &orig_rl);
843 if (get_implied_rl(right, &rl)) {
844 rl = cast_rl(left_type, rl);
845 if (orig_rl)
846 filter_by_comparison(&rl, comparison, orig_rl);
847 state = alloc_estate_rl(rl);
848 if (get_hard_max(right, &max)) {
849 estate_set_hard_max(state);
850 estate_set_fuzzy_max(state, max);
852 } else {
853 rl = alloc_whole_rl(right_type);
854 rl = cast_rl(left_type, rl);
855 if (orig_rl)
856 filter_by_comparison(&rl, comparison, orig_rl);
857 state = alloc_estate_rl(rl);
860 done:
861 set_extra_mod(name, sym, state);
862 free:
863 free_string(right_name);
866 static int op_remove_assign(int op)
868 switch (op) {
869 case SPECIAL_ADD_ASSIGN:
870 return '+';
871 case SPECIAL_SUB_ASSIGN:
872 return '-';
873 case SPECIAL_MUL_ASSIGN:
874 return '*';
875 case SPECIAL_DIV_ASSIGN:
876 return '/';
877 case SPECIAL_MOD_ASSIGN:
878 return '%';
879 case SPECIAL_AND_ASSIGN:
880 return '&';
881 case SPECIAL_OR_ASSIGN:
882 return '|';
883 case SPECIAL_XOR_ASSIGN:
884 return '^';
885 case SPECIAL_SHL_ASSIGN:
886 return SPECIAL_LEFTSHIFT;
887 case SPECIAL_SHR_ASSIGN:
888 return SPECIAL_RIGHTSHIFT;
889 default:
890 return op;
894 static void match_assign(struct expression *expr)
896 struct range_list *rl = NULL;
897 struct expression *left;
898 struct expression *right;
899 struct expression *binop_expr;
900 struct symbol *left_type;
901 struct symbol *sym;
902 char *name;
903 sval_t left_min, left_max;
904 sval_t right_min, right_max;
905 sval_t res_min, res_max;
907 left = strip_expr(expr->left);
909 right = strip_parens(expr->right);
910 if (right->type == EXPR_CALL && sym_name_is("__builtin_expect", right->fn))
911 right = get_argument_from_call_expr(right->args, 0);
912 while (right->type == EXPR_ASSIGNMENT && right->op == '=')
913 right = strip_parens(right->left);
915 if (expr->op == '=' && is_condition(expr->right))
916 return; /* handled in smatch_condition.c */
917 if (expr->op == '=' && right->type == EXPR_CALL)
918 return; /* handled in smatch_function_hooks.c */
919 if (expr->op == '=') {
920 match_vanilla_assign(left, right);
921 return;
924 name = expr_to_var_sym(left, &sym);
925 if (!name)
926 return;
928 left_type = get_type(left);
930 res_min = sval_type_min(left_type);
931 res_max = sval_type_max(left_type);
933 switch (expr->op) {
934 case SPECIAL_ADD_ASSIGN:
935 get_absolute_max(left, &left_max);
936 get_absolute_max(right, &right_max);
937 if (sval_binop_overflows(left_max, '+', sval_cast(left_type, right_max)))
938 break;
939 if (get_implied_min(left, &left_min) &&
940 !sval_is_negative_min(left_min) &&
941 get_implied_min(right, &right_min) &&
942 !sval_is_negative_min(right_min)) {
943 res_min = sval_binop(left_min, '+', right_min);
944 res_min = sval_cast(left_type, res_min);
946 if (inside_loop()) /* we are assuming loops don't lead to wrapping */
947 break;
948 res_max = sval_binop(left_max, '+', right_max);
949 res_max = sval_cast(left_type, res_max);
950 break;
951 case SPECIAL_SUB_ASSIGN:
952 if (get_implied_max(left, &left_max) &&
953 !sval_is_max(left_max) &&
954 get_implied_min(right, &right_min) &&
955 !sval_is_min(right_min)) {
956 res_max = sval_binop(left_max, '-', right_min);
957 res_max = sval_cast(left_type, res_max);
959 if (inside_loop())
960 break;
961 if (get_implied_min(left, &left_min) &&
962 !sval_is_min(left_min) &&
963 get_implied_max(right, &right_max) &&
964 !sval_is_max(right_max) &&
965 sval_cmp(left_min, right_max) > 0) {
966 res_min = sval_binop(left_min, '-', right_max);
967 res_min = sval_cast(left_type, res_min);
969 break;
970 case SPECIAL_AND_ASSIGN:
971 case SPECIAL_MOD_ASSIGN:
972 case SPECIAL_SHL_ASSIGN:
973 case SPECIAL_SHR_ASSIGN:
974 case SPECIAL_OR_ASSIGN:
975 case SPECIAL_XOR_ASSIGN:
976 case SPECIAL_MUL_ASSIGN:
977 case SPECIAL_DIV_ASSIGN:
978 binop_expr = binop_expression(expr->left,
979 op_remove_assign(expr->op),
980 expr->right);
981 if (get_absolute_rl(binop_expr, &rl)) {
982 rl = cast_rl(left_type, rl);
983 set_extra_mod(name, sym, alloc_estate_rl(rl));
984 goto free;
986 break;
988 rl = cast_rl(left_type, alloc_rl(res_min, res_max));
989 set_extra_mod(name, sym, alloc_estate_rl(rl));
990 free:
991 free_string(name);
994 static struct smatch_state *increment_state(struct smatch_state *state)
996 sval_t min = estate_min(state);
997 sval_t max = estate_max(state);
999 if (!estate_rl(state))
1000 return NULL;
1002 if (inside_loop())
1003 max = sval_type_max(max.type);
1005 if (!sval_is_min(min) && !sval_is_max(min))
1006 min.value++;
1007 if (!sval_is_min(max) && !sval_is_max(max))
1008 max.value++;
1009 return alloc_estate_range(min, max);
1012 static struct smatch_state *decrement_state(struct smatch_state *state)
1014 sval_t min = estate_min(state);
1015 sval_t max = estate_max(state);
1017 if (!estate_rl(state))
1018 return NULL;
1020 if (inside_loop())
1021 min = sval_type_min(min.type);
1023 if (!sval_is_min(min) && !sval_is_max(min))
1024 min.value--;
1025 if (!sval_is_min(max) && !sval_is_max(max))
1026 max.value--;
1027 return alloc_estate_range(min, max);
1030 static void clear_pointed_at_state(struct expression *expr)
1032 struct symbol *type;
1035 * ALERT: This is sort of a mess. If it's is a struct assigment like
1036 * "foo = bar;", then that's handled by smatch_struct_assignment.c.
1037 * the same thing for p++ where "p" is a struct. Most modifications
1038 * are handled by the assignment hook or the db. Smatch_extra.c doesn't
1039 * use smatch_modification.c because we have to get the ordering right
1040 * or something. So if you have p++ where p is a pointer to a standard
1041 * c type then we handle that here. What a mess.
1044 type = get_type(expr);
1045 if (!type || type->type != SYM_PTR)
1046 return;
1047 type = get_real_base_type(type);
1048 if (!type || type->type != SYM_BASETYPE)
1049 return;
1050 set_extra_expr_mod(deref_expression(expr), alloc_estate_whole(type));
1053 static void unop_expr(struct expression *expr)
1055 struct smatch_state *state;
1057 if (expr->smatch_flags & Handled)
1058 return;
1060 switch (expr->op) {
1061 case SPECIAL_INCREMENT:
1062 state = get_state_expr(SMATCH_EXTRA, expr->unop);
1063 state = increment_state(state);
1064 if (!state)
1065 state = alloc_estate_whole(get_type(expr));
1066 set_extra_expr_mod(expr->unop, state);
1067 clear_pointed_at_state(expr->unop);
1068 break;
1069 case SPECIAL_DECREMENT:
1070 state = get_state_expr(SMATCH_EXTRA, expr->unop);
1071 state = decrement_state(state);
1072 if (!state)
1073 state = alloc_estate_whole(get_type(expr));
1074 set_extra_expr_mod(expr->unop, state);
1075 clear_pointed_at_state(expr->unop);
1076 break;
1077 default:
1078 return;
1082 static void asm_expr(struct statement *stmt)
1085 struct expression *expr;
1086 struct symbol *type;
1087 int state = 0;
1089 FOR_EACH_PTR(stmt->asm_outputs, expr) {
1090 switch (state) {
1091 case 0: /* identifier */
1092 case 1: /* constraint */
1093 state++;
1094 continue;
1095 case 2: /* expression */
1096 state = 0;
1097 type = get_type(strip_expr(expr));
1098 set_extra_expr_mod(expr, alloc_estate_whole(type));
1099 continue;
1101 } END_FOR_EACH_PTR(expr);
1104 static void check_dereference(struct expression *expr)
1106 if (__in_fake_assign)
1107 return;
1108 if (outside_of_function())
1109 return;
1110 if (implied_not_equal(expr, 0))
1111 return;
1112 set_extra_expr_nomod(expr, alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval));
1115 static void match_dereferences(struct expression *expr)
1117 if (expr->type != EXPR_PREOP)
1118 return;
1119 /* it's saying that foo[1] = bar dereferences foo[1] */
1120 if (is_array(expr))
1121 return;
1122 check_dereference(expr->unop);
1125 static void match_pointer_as_array(struct expression *expr)
1127 if (!is_array(expr))
1128 return;
1129 check_dereference(get_array_base(expr));
1132 static void find_dereferences(struct expression *expr)
1134 while (expr->type == EXPR_PREOP) {
1135 if (expr->op == '*')
1136 check_dereference(expr->unop);
1137 expr = strip_expr(expr->unop);
1141 static void set_param_dereferenced(struct expression *arg, char *key, char *unused)
1143 struct symbol *sym;
1144 char *name;
1146 name = get_variable_from_key(arg, key, &sym);
1147 if (name && sym)
1148 set_extra_nomod(name, sym, alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval));
1149 free_string(name);
1151 find_dereferences(arg);
1154 static sval_t add_one(sval_t sval)
1156 sval.value++;
1157 return sval;
1160 static int handle_postop_inc(struct expression *left, int op, struct expression *right)
1162 struct statement *stmt;
1163 struct expression *cond;
1164 struct smatch_state *true_state, *false_state;
1165 sval_t start;
1166 sval_t limit;
1169 * If we're decrementing here then that's a canonical while count down
1170 * so it's handled already. We're only handling loops like:
1171 * i = 0;
1172 * do { ... } while (i++ < 3);
1175 if (left->type != EXPR_POSTOP || left->op != SPECIAL_INCREMENT)
1176 return 0;
1178 stmt = __cur_stmt->parent;
1179 if (!stmt)
1180 return 0;
1181 if (stmt->type == STMT_COMPOUND)
1182 stmt = stmt->parent;
1183 if (!stmt || stmt->type != STMT_ITERATOR || !stmt->iterator_post_condition)
1184 return 0;
1186 cond = strip_expr(stmt->iterator_post_condition);
1187 if (cond->type != EXPR_COMPARE || cond->op != op)
1188 return 0;
1189 if (left != strip_expr(cond->left) || right != strip_expr(cond->right))
1190 return 0;
1192 if (!get_implied_value(left->unop, &start))
1193 return 0;
1194 if (!get_implied_value(right, &limit))
1195 return 0;
1197 if (sval_cmp(start, limit) > 0)
1198 return 0;
1200 switch (op) {
1201 case '<':
1202 case SPECIAL_UNSIGNED_LT:
1203 break;
1204 case SPECIAL_LTE:
1205 case SPECIAL_UNSIGNED_LTE:
1206 limit = add_one(limit);
1207 default:
1208 return 0;
1212 true_state = alloc_estate_range(add_one(start), limit);
1213 false_state = alloc_estate_range(add_one(limit), add_one(limit));
1215 /* Currently we just discard the false state but when two passes is
1216 * implimented correctly then it will use it.
1219 set_extra_expr_true_false(left->unop, true_state, false_state);
1221 return 1;
1224 static void handle_comparison(struct symbol *type, struct expression *left, int op, struct expression *right)
1226 struct range_list *left_orig;
1227 struct range_list *left_true;
1228 struct range_list *left_false;
1229 struct range_list *right_orig;
1230 struct range_list *right_true;
1231 struct range_list *right_false;
1232 struct smatch_state *left_true_state;
1233 struct smatch_state *left_false_state;
1234 struct smatch_state *right_true_state;
1235 struct smatch_state *right_false_state;
1236 sval_t dummy, hard_max;
1237 int left_postop = 0;
1238 int right_postop = 0;
1240 if (left->op == SPECIAL_INCREMENT || left->op == SPECIAL_DECREMENT) {
1241 if (left->type == EXPR_POSTOP) {
1242 left->smatch_flags |= Handled;
1243 left_postop = left->op;
1244 if (handle_postop_inc(left, op, right))
1245 return;
1247 left = strip_parens(left->unop);
1249 while (left->type == EXPR_ASSIGNMENT)
1250 left = strip_parens(left->left);
1252 if (right->op == SPECIAL_INCREMENT || right->op == SPECIAL_DECREMENT) {
1253 if (right->type == EXPR_POSTOP) {
1254 right->smatch_flags |= Handled;
1255 right_postop = right->op;
1257 right = strip_parens(right->unop);
1260 get_real_absolute_rl(left, &left_orig);
1261 left_orig = cast_rl(type, left_orig);
1263 get_real_absolute_rl(right, &right_orig);
1264 right_orig = cast_rl(type, right_orig);
1266 split_comparison_rl(left_orig, op, right_orig, &left_true, &left_false, &right_true, &right_false);
1268 left_true = rl_truncate_cast(get_type(strip_expr(left)), left_true);
1269 left_false = rl_truncate_cast(get_type(strip_expr(left)), left_false);
1270 right_true = rl_truncate_cast(get_type(strip_expr(right)), right_true);
1271 right_false = rl_truncate_cast(get_type(strip_expr(right)), right_false);
1273 if (!left_true || !left_false) {
1274 struct range_list *tmp_true, *tmp_false;
1276 split_comparison_rl(alloc_whole_rl(type), op, right_orig, &tmp_true, &tmp_false, NULL, NULL);
1277 tmp_true = rl_truncate_cast(get_type(strip_expr(left)), tmp_true);
1278 tmp_false = rl_truncate_cast(get_type(strip_expr(left)), tmp_false);
1279 if (tmp_true && tmp_false)
1280 __save_imaginary_state(left, tmp_true, tmp_false);
1283 if (!right_true || !right_false) {
1284 struct range_list *tmp_true, *tmp_false;
1286 split_comparison_rl(alloc_whole_rl(type), op, right_orig, NULL, NULL, &tmp_true, &tmp_false);
1287 tmp_true = rl_truncate_cast(get_type(strip_expr(right)), tmp_true);
1288 tmp_false = rl_truncate_cast(get_type(strip_expr(right)), tmp_false);
1289 if (tmp_true && tmp_false)
1290 __save_imaginary_state(right, tmp_true, tmp_false);
1293 left_true_state = alloc_estate_rl(left_true);
1294 left_false_state = alloc_estate_rl(left_false);
1295 right_true_state = alloc_estate_rl(right_true);
1296 right_false_state = alloc_estate_rl(right_false);
1298 switch (op) {
1299 case '<':
1300 case SPECIAL_UNSIGNED_LT:
1301 case SPECIAL_UNSIGNED_LTE:
1302 case SPECIAL_LTE:
1303 if (get_hard_max(right, &dummy))
1304 estate_set_hard_max(left_true_state);
1305 if (get_hard_max(left, &dummy))
1306 estate_set_hard_max(right_false_state);
1307 break;
1308 case '>':
1309 case SPECIAL_UNSIGNED_GT:
1310 case SPECIAL_UNSIGNED_GTE:
1311 case SPECIAL_GTE:
1312 if (get_hard_max(left, &dummy))
1313 estate_set_hard_max(right_true_state);
1314 if (get_hard_max(right, &dummy))
1315 estate_set_hard_max(left_false_state);
1316 break;
1319 switch (op) {
1320 case '<':
1321 case SPECIAL_UNSIGNED_LT:
1322 case SPECIAL_UNSIGNED_LTE:
1323 case SPECIAL_LTE:
1324 if (get_hard_max(right, &hard_max)) {
1325 if (op == '<' || op == SPECIAL_UNSIGNED_LT)
1326 hard_max.value--;
1327 estate_set_fuzzy_max(left_true_state, hard_max);
1329 if (get_implied_value(right, &hard_max)) {
1330 if (op == SPECIAL_UNSIGNED_LTE ||
1331 op == SPECIAL_LTE)
1332 hard_max.value++;
1333 estate_set_fuzzy_max(left_false_state, hard_max);
1335 if (get_hard_max(left, &hard_max)) {
1336 if (op == SPECIAL_UNSIGNED_LTE ||
1337 op == SPECIAL_LTE)
1338 hard_max.value--;
1339 estate_set_fuzzy_max(right_false_state, hard_max);
1341 if (get_implied_value(left, &hard_max)) {
1342 if (op == '<' || op == SPECIAL_UNSIGNED_LT)
1343 hard_max.value++;
1344 estate_set_fuzzy_max(right_true_state, hard_max);
1346 break;
1347 case '>':
1348 case SPECIAL_UNSIGNED_GT:
1349 case SPECIAL_UNSIGNED_GTE:
1350 case SPECIAL_GTE:
1351 if (get_hard_max(left, &hard_max)) {
1352 if (op == '>' || op == SPECIAL_UNSIGNED_GT)
1353 hard_max.value--;
1354 estate_set_fuzzy_max(right_true_state, hard_max);
1356 if (get_implied_value(left, &hard_max)) {
1357 if (op == SPECIAL_UNSIGNED_GTE ||
1358 op == SPECIAL_GTE)
1359 hard_max.value++;
1360 estate_set_fuzzy_max(right_false_state, hard_max);
1362 if (get_hard_max(right, &hard_max)) {
1363 if (op == SPECIAL_UNSIGNED_LTE ||
1364 op == SPECIAL_LTE)
1365 hard_max.value--;
1366 estate_set_fuzzy_max(left_false_state, hard_max);
1368 if (get_implied_value(right, &hard_max)) {
1369 if (op == '>' ||
1370 op == SPECIAL_UNSIGNED_GT)
1371 hard_max.value++;
1372 estate_set_fuzzy_max(left_true_state, hard_max);
1374 break;
1375 case SPECIAL_EQUAL:
1376 if (get_hard_max(left, &hard_max))
1377 estate_set_fuzzy_max(right_true_state, hard_max);
1378 if (get_hard_max(right, &hard_max))
1379 estate_set_fuzzy_max(left_true_state, hard_max);
1380 break;
1383 if (get_hard_max(left, &hard_max)) {
1384 estate_set_hard_max(left_true_state);
1385 estate_set_hard_max(left_false_state);
1387 if (get_hard_max(right, &hard_max)) {
1388 estate_set_hard_max(right_true_state);
1389 estate_set_hard_max(right_false_state);
1392 if (left_postop == SPECIAL_INCREMENT) {
1393 left_true_state = increment_state(left_true_state);
1394 left_false_state = increment_state(left_false_state);
1396 if (left_postop == SPECIAL_DECREMENT) {
1397 left_true_state = decrement_state(left_true_state);
1398 left_false_state = decrement_state(left_false_state);
1400 if (right_postop == SPECIAL_INCREMENT) {
1401 right_true_state = increment_state(right_true_state);
1402 right_false_state = increment_state(right_false_state);
1404 if (right_postop == SPECIAL_DECREMENT) {
1405 right_true_state = decrement_state(right_true_state);
1406 right_false_state = decrement_state(right_false_state);
1409 if (estate_rl(left_true_state) && estates_equiv(left_true_state, left_false_state)) {
1410 left_true_state = NULL;
1411 left_false_state = NULL;
1414 if (estate_rl(right_true_state) && estates_equiv(right_true_state, right_false_state)) {
1415 right_true_state = NULL;
1416 right_false_state = NULL;
1419 set_extra_expr_true_false(left, left_true_state, left_false_state);
1420 set_extra_expr_true_false(right, right_true_state, right_false_state);
1423 static int is_simple_math(struct expression *expr)
1425 if (!expr)
1426 return 0;
1427 if (expr->type != EXPR_BINOP)
1428 return 0;
1429 switch (expr->op) {
1430 case '+':
1431 case '-':
1432 case '*':
1433 return 1;
1435 return 0;
1438 static void move_known_values(struct expression **left_p, struct expression **right_p)
1440 struct expression *left = *left_p;
1441 struct expression *right = *right_p;
1442 sval_t sval, dummy;
1444 if (get_implied_value(left, &sval)) {
1445 if (!is_simple_math(right))
1446 return;
1447 if (get_implied_value(right, &dummy))
1448 return;
1449 if (right->op == '*') {
1450 sval_t divisor;
1452 if (!get_value(right->right, &divisor))
1453 return;
1454 if (divisor.value == 0 && sval.value % divisor.value)
1455 return;
1456 *left_p = binop_expression(left, invert_op(right->op), right->right);
1457 *right_p = right->left;
1458 return;
1460 if (right->op == '+' && get_value(right->left, &sval)) {
1461 *left_p = binop_expression(left, invert_op(right->op), right->left);
1462 *right_p = right->right;
1463 return;
1465 if (get_value(right->right, &sval)) {
1466 *left_p = binop_expression(left, invert_op(right->op), right->right);
1467 *right_p = right->left;
1468 return;
1470 return;
1472 if (get_implied_value(right, &sval)) {
1473 if (!is_simple_math(left))
1474 return;
1475 if (get_implied_value(left, &dummy))
1476 return;
1477 if (left->op == '*') {
1478 sval_t divisor;
1480 if (!get_value(left->right, &divisor))
1481 return;
1482 if (divisor.value == 0 && sval.value % divisor.value)
1483 return;
1484 *right_p = binop_expression(right, invert_op(left->op), left->right);
1485 *left_p = left->left;
1486 return;
1488 if (left->op == '+' && get_value(left->left, &sval)) {
1489 *right_p = binop_expression(right, invert_op(left->op), left->left);
1490 *left_p = left->right;
1491 return;
1494 if (get_value(left->right, &sval)) {
1495 *right_p = binop_expression(right, invert_op(left->op), left->right);
1496 *left_p = left->left;
1497 return;
1499 return;
1503 static int match_func_comparison(struct expression *expr)
1505 struct expression *left = strip_expr(expr->left);
1506 struct expression *right = strip_expr(expr->right);
1507 sval_t sval;
1510 * fixme: think about this harder. We should always be trying to limit
1511 * the non-call side as well. If we can't determine the limitter does
1512 * that mean we aren't querying the database and are missing important
1513 * information?
1516 if (left->type == EXPR_CALL) {
1517 if (get_implied_value(left, &sval)) {
1518 handle_comparison(get_type(expr), left, expr->op, right);
1519 return 1;
1521 function_comparison(left, expr->op, right);
1522 return 1;
1525 if (right->type == EXPR_CALL) {
1526 if (get_implied_value(right, &sval)) {
1527 handle_comparison(get_type(expr), left, expr->op, right);
1528 return 1;
1530 function_comparison(left, expr->op, right);
1531 return 1;
1534 return 0;
1537 /* Handle conditions like "if (foo + bar < foo) {" */
1538 static int handle_integer_overflow_test(struct expression *expr)
1540 struct expression *left, *right;
1541 struct symbol *type;
1542 sval_t left_min, right_min, min, max;
1544 if (expr->op != '<' && expr->op != SPECIAL_UNSIGNED_LT)
1545 return 0;
1547 left = strip_parens(expr->left);
1548 right = strip_parens(expr->right);
1550 if (left->op != '+')
1551 return 0;
1553 type = get_type(expr);
1554 if (!type)
1555 return 0;
1556 if (type_positive_bits(type) == 32) {
1557 max.type = &uint_ctype;
1558 max.uvalue = (unsigned int)-1;
1559 } else if (type_positive_bits(type) == 64) {
1560 max.type = &ulong_ctype;
1561 max.value = (unsigned long long)-1;
1562 } else {
1563 return 0;
1566 if (!expr_equiv(left->left, right) && !expr_equiv(left->right, right))
1567 return 0;
1569 get_absolute_min(left->left, &left_min);
1570 get_absolute_min(left->right, &right_min);
1571 min = sval_binop(left_min, '+', right_min);
1573 set_extra_chunk_true_false(left, NULL, alloc_estate_range(min, max));
1574 return 1;
1577 static void match_comparison(struct expression *expr)
1579 struct expression *left_orig = strip_parens(expr->left);
1580 struct expression *right_orig = strip_parens(expr->right);
1581 struct expression *left, *right;
1582 struct expression *prev;
1583 struct symbol *type;
1585 if (match_func_comparison(expr))
1586 return;
1588 type = get_type(expr);
1589 if (!type)
1590 type = &llong_ctype;
1592 if (handle_integer_overflow_test(expr))
1593 return;
1595 left = left_orig;
1596 right = right_orig;
1597 move_known_values(&left, &right);
1598 handle_comparison(type, left, expr->op, right);
1600 prev = get_assigned_expr(left_orig);
1601 if (is_simple_math(prev) && has_variable(prev, left_orig) == 0) {
1602 left = prev;
1603 right = right_orig;
1604 move_known_values(&left, &right);
1605 handle_comparison(type, left, expr->op, right);
1608 prev = get_assigned_expr(right_orig);
1609 if (is_simple_math(prev) && has_variable(prev, right_orig) == 0) {
1610 left = left_orig;
1611 right = prev;
1612 move_known_values(&left, &right);
1613 handle_comparison(type, left, expr->op, right);
1617 static sval_t get_high_mask(sval_t known)
1619 sval_t ret;
1620 int i;
1622 ret = known;
1623 ret.value = 0;
1625 for (i = type_bits(known.type) - 1; i >= 0; i--) {
1626 if (known.uvalue & (1ULL << i))
1627 ret.uvalue |= (1ULL << i);
1628 else
1629 return ret;
1632 return ret;
1635 static void handle_AND_condition(struct expression *expr)
1637 struct range_list *orig_rl;
1638 struct range_list *true_rl = NULL;
1639 struct range_list *false_rl = NULL;
1640 sval_t known;
1641 int bit;
1643 if (get_implied_value(expr->left, &known)) {
1644 sval_t low_mask = known;
1645 sval_t high_mask;
1647 if (known.value > 0) {
1648 bit = ffsll(known.value) - 1;
1649 low_mask.uvalue = (1ULL << bit) - 1;
1650 get_absolute_rl(expr->right, &orig_rl);
1651 true_rl = remove_range(orig_rl, sval_type_val(known.type, 0), low_mask);
1653 high_mask = get_high_mask(known);
1654 if (high_mask.value) {
1655 bit = ffsll(high_mask.value) - 1;
1656 low_mask.uvalue = (1ULL << bit) - 1;
1658 get_absolute_rl(expr->left, &orig_rl);
1659 if (sval_is_negative(rl_min(orig_rl)))
1660 orig_rl = remove_range(orig_rl, sval_type_min(known.type), sval_type_val(known.type, -1));
1661 false_rl = remove_range(orig_rl, low_mask, sval_type_max(known.type));
1662 if (type_signed(high_mask.type) && type_unsigned(rl_type(false_rl))) {
1663 false_rl = remove_range(false_rl,
1664 sval_type_val(rl_type(false_rl), sval_type_max(known.type).uvalue),
1665 sval_type_val(rl_type(false_rl), -1));
1668 set_extra_expr_true_false(expr->right,
1669 true_rl ? alloc_estate_rl(true_rl) : NULL,
1670 false_rl ? alloc_estate_rl(false_rl) : NULL);
1672 return;
1675 if (get_implied_value(expr->right, &known)) {
1676 sval_t low_mask = known;
1677 sval_t high_mask;
1679 if (known.value > 0) {
1680 bit = ffsll(known.value) - 1;
1681 low_mask.uvalue = (1ULL << bit) - 1;
1682 get_absolute_rl(expr->left, &orig_rl);
1683 true_rl = remove_range(orig_rl, sval_type_val(known.type, 0), low_mask);
1685 high_mask = get_high_mask(known);
1686 if (high_mask.value) {
1687 bit = ffsll(high_mask.value) - 1;
1688 low_mask.uvalue = (1ULL << bit) - 1;
1690 get_absolute_rl(expr->left, &orig_rl);
1691 if (sval_is_negative(rl_min(orig_rl)))
1692 orig_rl = remove_range(orig_rl, sval_type_min(known.type), sval_type_val(known.type, -1));
1693 false_rl = remove_range(orig_rl, low_mask, sval_type_max(known.type));
1694 if (type_signed(high_mask.type) && type_unsigned(rl_type(false_rl))) {
1695 false_rl = remove_range(false_rl,
1696 sval_type_val(rl_type(false_rl), sval_type_max(known.type).uvalue),
1697 sval_type_val(rl_type(false_rl), -1));
1700 set_extra_expr_true_false(expr->left,
1701 true_rl ? alloc_estate_rl(true_rl) : NULL,
1702 false_rl ? alloc_estate_rl(false_rl) : NULL);
1703 return;
1707 static void handle_MOD_condition(struct expression *expr)
1709 struct range_list *orig_rl;
1710 struct range_list *true_rl;
1711 struct range_list *false_rl = NULL;
1712 sval_t right;
1713 sval_t zero = {
1714 .value = 0,
1717 if (!get_implied_value(expr->right, &right) || right.value == 0)
1718 return;
1719 get_absolute_rl(expr->left, &orig_rl);
1721 zero.type = rl_type(orig_rl);
1723 /* We're basically dorking around the min and max here */
1724 true_rl = remove_range(orig_rl, zero, zero);
1725 if (!sval_is_max(rl_max(true_rl)) &&
1726 !(rl_max(true_rl).value % right.value))
1727 true_rl = remove_range(true_rl, rl_max(true_rl), rl_max(true_rl));
1729 if (rl_equiv(true_rl, orig_rl))
1730 true_rl = NULL;
1732 if (sval_is_positive(rl_min(orig_rl)) &&
1733 (rl_max(orig_rl).value - rl_min(orig_rl).value) / right.value < 5) {
1734 sval_t add;
1735 int i;
1737 add = rl_min(orig_rl);
1738 add.value += right.value - (add.value % right.value);
1739 add.value -= right.value;
1741 for (i = 0; i < 5; i++) {
1742 add.value += right.value;
1743 if (add.value > rl_max(orig_rl).value)
1744 break;
1745 add_range(&false_rl, add, add);
1747 } else {
1748 if (rl_min(orig_rl).uvalue != 0 &&
1749 rl_min(orig_rl).uvalue < right.uvalue) {
1750 sval_t chop = right;
1751 chop.value--;
1752 false_rl = remove_range(orig_rl, zero, chop);
1755 if (!sval_is_max(rl_max(orig_rl)) &&
1756 (rl_max(orig_rl).value % right.value)) {
1757 sval_t chop = rl_max(orig_rl);
1758 chop.value -= chop.value % right.value;
1759 chop.value++;
1760 if (!false_rl)
1761 false_rl = clone_rl(orig_rl);
1762 false_rl = remove_range(false_rl, chop, rl_max(orig_rl));
1766 set_extra_expr_true_false(expr->left,
1767 true_rl ? alloc_estate_rl(true_rl) : NULL,
1768 false_rl ? alloc_estate_rl(false_rl) : NULL);
1771 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
1772 void __extra_match_condition(struct expression *expr)
1774 struct smatch_state *pre_state;
1775 struct smatch_state *true_state;
1776 struct smatch_state *false_state;
1778 expr = strip_expr(expr);
1779 switch (expr->type) {
1780 case EXPR_CALL:
1781 function_comparison(expr, SPECIAL_NOTEQUAL, zero_expr());
1782 return;
1783 case EXPR_PREOP:
1784 case EXPR_SYMBOL:
1785 case EXPR_DEREF: {
1786 sval_t zero;
1788 zero = sval_blank(expr);
1789 zero.value = 0;
1791 pre_state = get_extra_state(expr);
1792 true_state = estate_filter_sval(pre_state, zero);
1793 if (possibly_true(expr, SPECIAL_EQUAL, zero_expr()))
1794 false_state = alloc_estate_sval(zero);
1795 else
1796 false_state = alloc_estate_empty();
1797 set_extra_expr_true_false(expr, true_state, false_state);
1798 return;
1800 case EXPR_COMPARE:
1801 match_comparison(expr);
1802 return;
1803 case EXPR_ASSIGNMENT:
1804 __extra_match_condition(expr->left);
1805 return;
1806 case EXPR_BINOP:
1807 if (expr->op == '&')
1808 handle_AND_condition(expr);
1809 if (expr->op == '%')
1810 handle_MOD_condition(expr);
1811 return;
1815 static void assume_indexes_are_valid(struct expression *expr)
1817 struct expression *array_expr;
1818 int array_size;
1819 struct expression *offset;
1820 struct symbol *offset_type;
1821 struct range_list *rl_before;
1822 struct range_list *rl_after;
1823 struct range_list *filter = NULL;
1824 sval_t size;
1826 expr = strip_expr(expr);
1827 if (!is_array(expr))
1828 return;
1830 offset = get_array_offset(expr);
1831 offset_type = get_type(offset);
1832 if (offset_type && type_signed(offset_type)) {
1833 filter = alloc_rl(sval_type_min(offset_type),
1834 sval_type_val(offset_type, -1));
1837 array_expr = get_array_base(expr);
1838 array_size = get_real_array_size(array_expr);
1839 if (array_size > 1) {
1840 size = sval_type_val(offset_type, array_size);
1841 add_range(&filter, size, sval_type_max(offset_type));
1844 if (!filter)
1845 return;
1846 get_absolute_rl(offset, &rl_before);
1847 rl_after = rl_filter(rl_before, filter);
1848 if (rl_equiv(rl_before, rl_after))
1849 return;
1850 set_extra_expr_nomod(offset, alloc_estate_rl(rl_after));
1853 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
1854 int implied_not_equal(struct expression *expr, long long val)
1856 return !possibly_false(expr, SPECIAL_NOTEQUAL, value_expr(val));
1859 int implied_not_equal_name_sym(char *name, struct symbol *sym, long long val)
1861 struct smatch_state *estate;
1863 estate = get_state(SMATCH_EXTRA, name, sym);
1864 if (!estate)
1865 return 0;
1866 if (!rl_has_sval(estate_rl(estate), sval_type_val(estate_type(estate), 0)))
1867 return 1;
1868 return 0;
1871 int parent_is_null_var_sym(const char *name, struct symbol *sym)
1873 char buf[256];
1874 char *start;
1875 char *end;
1876 struct smatch_state *state;
1878 strncpy(buf, name, sizeof(buf) - 1);
1879 buf[sizeof(buf) - 1] = '\0';
1881 start = &buf[0];
1882 while (*start == '*') {
1883 start++;
1884 state = get_state(SMATCH_EXTRA, start, sym);
1885 if (!state)
1886 continue;
1887 if (!estate_rl(state))
1888 return 1;
1889 if (estate_min(state).value == 0 &&
1890 estate_max(state).value == 0)
1891 return 1;
1894 start = &buf[0];
1895 while (*start == '&')
1896 start++;
1898 while ((end = strrchr(start, '-'))) {
1899 *end = '\0';
1900 state = get_state(SMATCH_EXTRA, start, sym);
1901 if (!state)
1902 continue;
1903 if (estate_min(state).value == 0 &&
1904 estate_max(state).value == 0)
1905 return 1;
1907 return 0;
1910 int parent_is_null(struct expression *expr)
1912 struct symbol *sym;
1913 char *var;
1914 int ret = 0;
1916 expr = strip_expr(expr);
1917 var = expr_to_var_sym(expr, &sym);
1918 if (!var || !sym)
1919 goto free;
1920 ret = parent_is_null_var_sym(var, sym);
1921 free:
1922 free_string(var);
1923 return ret;
1926 static int param_used_callback(void *found, int argc, char **argv, char **azColName)
1928 *(int *)found = 1;
1929 return 0;
1932 static int filter_unused_kzalloc_info(struct expression *call, int param, char *printed_name, struct sm_state *sm)
1934 sval_t sval;
1935 int found = 0;
1937 /* for function pointers assume everything is used */
1938 if (call->fn->type != EXPR_SYMBOL)
1939 return 0;
1942 * kzalloc() information is treated as special because so there is just
1943 * a lot of stuff initialized to zero and it makes building the database
1944 * take hours and hours.
1946 * In theory, we should just remove this line and not pass any unused
1947 * information, but I'm not sure enough that this code works so I want
1948 * to hold off on that for now.
1950 if (!estate_get_single_value(sm->state, &sval) || sval.value != 0)
1951 return 0;
1953 run_sql(&param_used_callback, &found,
1954 "select * from call_implies where %s and type = %d and parameter = %d and key = '%s';",
1955 get_static_filter(call->fn->symbol), PARAM_USED, param, printed_name);
1956 if (found)
1957 return 0;
1959 /* If the database is not built yet, then assume everything is used */
1960 run_sql(&param_used_callback, &found,
1961 "select * from call_implies where %s and type = %d;",
1962 get_static_filter(call->fn->symbol), PARAM_USED);
1963 if (!found)
1964 return 0;
1966 return 1;
1969 struct range_list *intersect_with_real_abs_var_sym(const char *name, struct symbol *sym, struct range_list *start)
1971 struct smatch_state *state;
1974 * Here is the difference between implied value and real absolute, say
1975 * you have:
1977 * int a = (u8)x;
1979 * Then you know that a is 0-255. That's real absolute. But you don't
1980 * know for sure that it actually goes up to 255. So it's not implied.
1981 * Implied indicates a degree of certainty.
1983 * But then say you cap "a" at 8. That means you know it goes up to
1984 * 8. So now the implied value is s32min-8. But you can combine it
1985 * with the real absolute to say that actually it's 0-8.
1987 * We are combining it here. But now that I think about it, this is
1988 * probably not the ideal place to combine it because it should proably
1989 * be done earlier. Oh well, this is an improvement on what was there
1990 * before so I'm going to commit this code.
1994 state = get_real_absolute_state_var_sym(name, sym);
1995 if (!state || !estate_rl(state))
1996 return start;
1998 return rl_intersection(estate_rl(state), start);
2001 struct range_list *intersect_with_real_abs_expr(struct expression *expr, struct range_list *start)
2003 struct smatch_state *state;
2005 state = get_real_absolute_state(expr);
2006 if (!state || !estate_rl(state))
2007 return start;
2009 return rl_intersection(estate_rl(state), start);
2012 static void struct_member_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
2014 struct range_list *rl;
2016 if (estate_is_whole(sm->state))
2017 return;
2018 if (filter_unused_kzalloc_info(call, param, printed_name, sm))
2019 return;
2020 rl = estate_rl(sm->state);
2021 rl = intersect_with_real_abs_var_sym(sm->name, sm->sym, rl);
2022 sql_insert_caller_info(call, PARAM_VALUE, param, printed_name, show_rl(rl));
2023 if (estate_has_fuzzy_max(sm->state))
2024 sql_insert_caller_info(call, FUZZY_MAX, param, printed_name,
2025 sval_to_str(estate_get_fuzzy_max(sm->state)));
2028 static void returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
2030 struct symbol *returned_sym;
2031 struct sm_state *sm;
2032 const char *param_name;
2033 char *compare_str;
2034 char buf[256];
2036 returned_sym = expr_to_sym(expr);
2037 if (!returned_sym)
2038 return;
2040 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
2041 if (!estate_rl(sm->state))
2042 continue;
2043 if (returned_sym != sm->sym)
2044 continue;
2046 param_name = get_param_name(sm);
2047 if (!param_name)
2048 continue;
2049 if (strcmp(param_name, "$") == 0)
2050 continue;
2051 compare_str = name_sym_to_param_comparison(sm->name, sm->sym);
2052 if (!compare_str && estate_is_whole(sm->state))
2053 continue;
2054 snprintf(buf, sizeof(buf), "%s%s", sm->state->name, compare_str ?: "");
2056 sql_insert_return_states(return_id, return_ranges, PARAM_VALUE,
2057 -1, param_name, buf);
2058 } END_FOR_EACH_SM(sm);
2061 static void db_limited_before(void)
2063 unmatched_stree = clone_stree(__get_cur_stree());
2066 static void db_limited_after(void)
2068 free_stree(&unmatched_stree);
2071 static int rl_fits_in_type(struct range_list *rl, struct symbol *type)
2073 if (type_bits(rl_type(rl)) <= type_bits(type))
2074 return 1;
2075 if (sval_cmp(rl_max(rl), sval_type_max(type)) > 0)
2076 return 0;
2077 if (sval_is_negative(rl_min(rl)) &&
2078 sval_cmp(rl_min(rl), sval_type_min(type)) < 0)
2079 return 0;
2080 return 1;
2083 static int basically_the_same(struct range_list *orig, struct range_list *new)
2085 if (rl_equiv(orig, new))
2086 return 1;
2089 * The whole range is essentially the same as 0,4096-27777777777 so
2090 * don't overwrite the implications just to store that.
2093 if (rl_type(orig)->type == SYM_PTR &&
2094 is_whole_rl(orig) &&
2095 rl_min(new).value == 0 &&
2096 rl_max(new).value == valid_ptr_max)
2097 return 1;
2098 return 0;
2101 static void db_param_limit_filter(struct expression *expr, int param, char *key, char *value, enum info_type op)
2103 struct expression *arg;
2104 char *name;
2105 struct symbol *sym;
2106 struct var_sym_list *vsl = NULL;
2107 struct sm_state *sm;
2108 struct symbol *compare_type, *var_type;
2109 struct range_list *rl;
2110 struct range_list *limit;
2111 struct range_list *new;
2113 while (expr->type == EXPR_ASSIGNMENT)
2114 expr = strip_expr(expr->right);
2115 if (expr->type != EXPR_CALL)
2116 return;
2118 arg = get_argument_from_call_expr(expr->args, param);
2119 if (!arg)
2120 return;
2122 name = get_chunk_from_key(arg, key, &sym, &vsl);
2123 if (!name)
2124 return;
2125 if (op != PARAM_LIMIT && !sym)
2126 goto free;
2128 if (strcmp(key, "$") == 0)
2129 compare_type = get_arg_type(expr->fn, param);
2130 else
2131 compare_type = get_member_type_from_key(arg, key);
2133 sm = get_sm_state(SMATCH_EXTRA, name, sym);
2134 if (sm)
2135 rl = estate_rl(sm->state);
2136 else
2137 rl = alloc_whole_rl(compare_type);
2139 if (op == PARAM_LIMIT && !rl_fits_in_type(rl, compare_type))
2140 goto free;
2142 call_results_to_rl(expr, compare_type, value, &limit);
2143 new = rl_intersection(rl, limit);
2145 var_type = get_member_type_from_key(arg, key);
2146 new = cast_rl(var_type, new);
2148 /* We want to preserve the implications here */
2149 if (sm && basically_the_same(estate_rl(sm->state), new))
2150 __set_sm(sm); /* FIXME: Is this really necessary? */
2151 else {
2152 char *tmp_name;
2153 struct symbol *tmp_sym;
2155 tmp_name = map_long_to_short_name_sym(name, sym, &tmp_sym);
2156 if (tmp_name && tmp_sym) {
2157 free_string(name);
2158 name = tmp_name;
2159 sym = tmp_sym;
2162 if (op == PARAM_LIMIT)
2163 set_extra_nomod_vsl(name, sym, vsl, alloc_estate_rl(new));
2164 else
2165 set_extra_mod(name, sym, alloc_estate_rl(new));
2168 free:
2169 free_string(name);
2172 static void db_param_limit(struct expression *expr, int param, char *key, char *value)
2174 db_param_limit_filter(expr, param, key, value, PARAM_LIMIT);
2177 static void db_param_filter(struct expression *expr, int param, char *key, char *value)
2179 db_param_limit_filter(expr, param, key, value, PARAM_FILTER);
2182 static void db_param_add_set(struct expression *expr, int param, char *key, char *value, enum info_type op)
2184 struct expression *arg;
2185 char *name, *tmp_name;
2186 struct symbol *sym, *tmp_sym;
2187 struct symbol *type;
2188 struct smatch_state *state;
2189 struct range_list *new = NULL;
2190 struct range_list *added = NULL;
2192 while (expr->type == EXPR_ASSIGNMENT)
2193 expr = strip_expr(expr->right);
2194 if (expr->type != EXPR_CALL)
2195 return;
2197 arg = get_argument_from_call_expr(expr->args, param);
2198 if (!arg)
2199 return;
2200 type = get_member_type_from_key(arg, key);
2201 name = get_variable_from_key(arg, key, &sym);
2202 if (!name || !sym)
2203 goto free;
2205 state = get_state(SMATCH_EXTRA, name, sym);
2206 if (state)
2207 new = estate_rl(state);
2209 call_results_to_rl(expr, type, value, &added);
2211 if (op == PARAM_SET)
2212 new = added;
2213 else
2214 new = rl_union(new, added);
2216 tmp_name = map_long_to_short_name_sym_nostack(name, sym, &tmp_sym);
2217 if (tmp_name && tmp_sym) {
2218 free_string(name);
2219 name = tmp_name;
2220 sym = tmp_sym;
2222 set_extra_mod(name, sym, alloc_estate_rl(new));
2223 free:
2224 free_string(name);
2227 static void db_param_add(struct expression *expr, int param, char *key, char *value)
2229 db_param_add_set(expr, param, key, value, PARAM_ADD);
2232 static void db_param_set(struct expression *expr, int param, char *key, char *value)
2234 db_param_add_set(expr, param, key, value, PARAM_SET);
2237 static void db_param_value(struct expression *expr, int param, char *key, char *value)
2239 struct expression *call;
2240 char *name;
2241 struct symbol *sym;
2242 struct symbol *type;
2243 struct range_list *rl = NULL;
2245 if (param != -1)
2246 return;
2248 call = expr;
2249 while (call->type == EXPR_ASSIGNMENT)
2250 call = strip_expr(call->right);
2251 if (call->type != EXPR_CALL)
2252 return;
2254 type = get_member_type_from_key(expr->left, key);
2255 name = get_variable_from_key(expr->left, key, &sym);
2256 if (!name || !sym)
2257 goto free;
2259 call_results_to_rl(call, type, value, &rl);
2261 set_extra_mod(name, sym, alloc_estate_rl(rl));
2262 free:
2263 free_string(name);
2266 static void match_call_info(struct expression *expr)
2268 struct smatch_state *state;
2269 struct range_list *rl = NULL;
2270 struct expression *arg;
2271 struct symbol *type;
2272 int i = 0;
2274 FOR_EACH_PTR(expr->args, arg) {
2275 type = get_arg_type(expr->fn, i);
2277 if (get_implied_rl(arg, &rl))
2278 rl = cast_rl(type, rl);
2279 else
2280 rl = cast_rl(type, alloc_whole_rl(get_type(arg)));
2282 if (!is_whole_rl(rl)) {
2283 rl = intersect_with_real_abs_expr(arg, rl);
2284 sql_insert_caller_info(expr, PARAM_VALUE, i, "$", show_rl(rl));
2286 state = get_state_expr(SMATCH_EXTRA, arg);
2287 if (estate_has_fuzzy_max(state)) {
2288 sql_insert_caller_info(expr, FUZZY_MAX, i, "$",
2289 sval_to_str(estate_get_fuzzy_max(state)));
2291 i++;
2292 } END_FOR_EACH_PTR(arg);
2295 static void set_param_value(const char *name, struct symbol *sym, char *key, char *value)
2297 struct range_list *rl = NULL;
2298 struct smatch_state *state;
2299 struct symbol *type;
2300 char fullname[256];
2301 sval_t dummy;
2303 if (strcmp(key, "*$") == 0)
2304 snprintf(fullname, sizeof(fullname), "*%s", name);
2305 else if (strncmp(key, "$", 1) == 0)
2306 snprintf(fullname, 256, "%s%s", name, key + 1);
2307 else
2308 return;
2310 type = get_member_type_from_key(symbol_expression(sym), key);
2311 str_to_rl(type, value, &rl);
2312 state = alloc_estate_rl(rl);
2313 if (estate_get_single_value(state, &dummy))
2314 estate_set_hard_max(state);
2315 set_state(SMATCH_EXTRA, fullname, sym, state);
2318 static void set_param_hard_max(const char *name, struct symbol *sym, char *key, char *value)
2320 struct range_list *rl = NULL;
2321 struct smatch_state *state;
2322 struct symbol *type;
2323 char fullname[256];
2324 sval_t max;
2326 if (strcmp(key, "*$") == 0)
2327 snprintf(fullname, sizeof(fullname), "*%s", name);
2328 else if (strncmp(key, "$", 1) == 0)
2329 snprintf(fullname, 256, "%s%s", name, key + 1);
2330 else
2331 return;
2333 state = get_state(SMATCH_EXTRA, fullname, sym);
2334 if (!state)
2335 return;
2336 type = get_member_type_from_key(symbol_expression(sym), key);
2337 str_to_rl(type, value, &rl);
2338 if (!rl_to_sval(rl, &max))
2339 return;
2340 estate_set_fuzzy_max(state, max);
2343 struct smatch_state *get_extra_state(struct expression *expr)
2345 char *name;
2346 struct symbol *sym;
2347 struct smatch_state *ret = NULL;
2348 struct range_list *rl;
2350 if (is_pointer(expr) && get_address_rl(expr, &rl))
2351 return alloc_estate_rl(rl);
2353 name = expr_to_known_chunk_sym(expr, &sym);
2354 if (!name)
2355 goto free;
2357 ret = get_state(SMATCH_EXTRA, name, sym);
2358 free:
2359 free_string(name);
2360 return ret;
2363 void register_smatch_extra(int id)
2365 my_id = id;
2367 add_merge_hook(my_id, &merge_estates);
2368 add_unmatched_state_hook(my_id, &unmatched_state);
2369 select_caller_info_hook(set_param_value, PARAM_VALUE);
2370 select_caller_info_hook(set_param_hard_max, FUZZY_MAX);
2371 select_return_states_before(&db_limited_before);
2372 select_return_states_hook(PARAM_LIMIT, &db_param_limit);
2373 select_return_states_hook(PARAM_FILTER, &db_param_filter);
2374 select_return_states_hook(PARAM_ADD, &db_param_add);
2375 select_return_states_hook(PARAM_SET, &db_param_set);
2376 select_return_states_hook(PARAM_VALUE, &db_param_value);
2377 select_return_states_after(&db_limited_after);
2380 static void match_link_modify(struct sm_state *sm, struct expression *mod_expr)
2382 struct var_sym_list *links;
2383 struct var_sym *tmp;
2384 struct smatch_state *state;
2386 links = sm->state->data;
2388 FOR_EACH_PTR(links, tmp) {
2389 if (sm->sym == tmp->sym &&
2390 strcmp(sm->name, tmp->var) == 0)
2391 continue;
2392 state = get_state(SMATCH_EXTRA, tmp->var, tmp->sym);
2393 if (!state)
2394 continue;
2395 set_state(SMATCH_EXTRA, tmp->var, tmp->sym, alloc_estate_whole(estate_type(state)));
2396 } END_FOR_EACH_PTR(tmp);
2397 set_state(link_id, sm->name, sm->sym, &undefined);
2400 void register_smatch_extra_links(int id)
2402 link_id = id;
2405 void register_smatch_extra_late(int id)
2407 add_merge_hook(link_id, &merge_link_states);
2408 add_modification_hook(link_id, &match_link_modify);
2409 add_hook(&match_dereferences, DEREF_HOOK);
2410 add_hook(&match_pointer_as_array, OP_HOOK);
2411 select_call_implies_hook(DEREFERENCE, &set_param_dereferenced);
2412 add_hook(&match_function_call, FUNCTION_CALL_HOOK);
2413 add_hook(&match_assign, ASSIGNMENT_HOOK);
2414 add_hook(&match_assign, GLOBAL_ASSIGNMENT_HOOK);
2415 add_hook(&unop_expr, OP_HOOK);
2416 add_hook(&asm_expr, ASM_HOOK);
2417 add_untracked_param_hook(&match_untracked_array);
2419 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
2420 add_member_info_callback(my_id, struct_member_callback);
2421 add_split_return_callback(&returned_struct_members);
2423 add_hook(&assume_indexes_are_valid, OP_HOOK);