unreachable: don't complain about static declarations
[smatch.git] / smatch_extra.c
blob5d8cf7175527aea8cdb4ae29eef9de22447cebc5
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 struct sm_state *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 NULL;
88 return 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 struct sm_state *set_extra_mod(const char *name, struct symbol *sym, struct smatch_state *state)
168 char *new_name;
169 struct symbol *new_sym;
170 struct sm_state *sm;
172 sm = set_extra_mod_helper(name, sym, state);
173 new_name = get_other_name_sym(name, sym, &new_sym);
174 if (new_name && new_sym)
175 set_extra_mod_helper(new_name, new_sym, state);
176 free_string(new_name);
177 return sm;
180 static void clear_array_states(struct expression *array)
182 struct sm_state *sm;
184 sm = get_sm_state_expr(link_id, array);
185 if (sm)
186 match_link_modify(sm, NULL);
189 static struct sm_state *set_extra_array_mod(struct expression *expr, struct smatch_state *state)
191 struct expression *array;
192 struct var_sym_list *vsl;
193 struct var_sym *vs;
194 char *name;
195 struct symbol *sym;
196 struct sm_state *ret = NULL;
198 array = get_array_base(expr);
200 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
201 if (!name || !vsl) {
202 clear_array_states(array);
203 goto free;
206 FOR_EACH_PTR(vsl, vs) {
207 store_link(link_id, vs->var, vs->sym, name, sym);
208 } END_FOR_EACH_PTR(vs);
210 call_extra_mod_hooks(name, sym, state);
211 ret = set_state(SMATCH_EXTRA, name, sym, state);
212 free:
213 free_string(name);
214 return ret;
217 struct sm_state *set_extra_expr_mod(struct expression *expr, struct smatch_state *state)
219 struct symbol *sym;
220 char *name;
221 struct sm_state *ret = NULL;
223 if (is_array(expr))
224 return set_extra_array_mod(expr, state);
226 expr = strip_expr(expr);
227 name = expr_to_var_sym(expr, &sym);
228 if (!name || !sym)
229 goto free;
230 ret = set_extra_mod(name, sym, state);
231 free:
232 free_string(name);
233 return ret;
236 void set_extra_nomod(const char *name, struct symbol *sym, struct smatch_state *state)
238 char *new_name;
239 struct symbol *new_sym;
240 struct relation *rel;
241 struct smatch_state *orig_state;
243 orig_state = get_state(SMATCH_EXTRA, name, sym);
245 new_name = get_other_name_sym(name, sym, &new_sym);
246 if (new_name && new_sym)
247 set_state(SMATCH_EXTRA, new_name, new_sym, state);
248 free_string(new_name);
250 if (!estate_related(orig_state)) {
251 set_state(SMATCH_EXTRA, name, sym, state);
252 return;
255 set_related(state, estate_related(orig_state));
256 FOR_EACH_PTR(estate_related(orig_state), rel) {
257 struct smatch_state *estate;
259 if (option_debug_related)
260 sm_msg("%s updating related %s to %s", name, rel->name, state->name);
261 estate = get_state(SMATCH_EXTRA, rel->name, rel->sym);
262 if (!estate)
263 continue;
264 set_state(SMATCH_EXTRA, rel->name, rel->sym, clone_estate_cast(estate_type(estate), state));
265 } END_FOR_EACH_PTR(rel);
268 void set_extra_nomod_vsl(const char *name, struct symbol *sym, struct var_sym_list *vsl, struct smatch_state *state)
270 struct var_sym *vs;
272 FOR_EACH_PTR(vsl, vs) {
273 store_link(link_id, vs->var, vs->sym, name, sym);
274 } END_FOR_EACH_PTR(vs);
276 set_extra_nomod(name, sym, state);
280 * This is for return_implies_state() hooks which modify a SMATCH_EXTRA state
282 void set_extra_expr_nomod(struct expression *expr, struct smatch_state *state)
284 struct var_sym_list *vsl;
285 struct var_sym *vs;
286 char *name;
287 struct symbol *sym;
289 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
290 if (!name || !vsl)
291 goto free;
292 FOR_EACH_PTR(vsl, vs) {
293 store_link(link_id, vs->var, vs->sym, name, sym);
294 } END_FOR_EACH_PTR(vs);
296 set_extra_nomod(name, sym, state);
297 free:
298 free_string(name);
301 static void set_extra_true_false(const char *name, struct symbol *sym,
302 struct smatch_state *true_state,
303 struct smatch_state *false_state)
305 char *new_name;
306 struct symbol *new_sym;
307 struct relation *rel;
308 struct smatch_state *orig_state;
310 if (!true_state && !false_state)
311 return;
313 if (in_warn_on_macro())
314 return;
316 new_name = get_other_name_sym(name, sym, &new_sym);
317 if (new_name && new_sym)
318 set_true_false_states(SMATCH_EXTRA, new_name, new_sym, true_state, false_state);
319 free_string(new_name);
321 orig_state = get_state(SMATCH_EXTRA, name, sym);
323 if (!estate_related(orig_state)) {
324 set_true_false_states(SMATCH_EXTRA, name, sym, true_state, false_state);
325 return;
328 if (true_state)
329 set_related(true_state, estate_related(orig_state));
330 if (false_state)
331 set_related(false_state, estate_related(orig_state));
333 FOR_EACH_PTR(estate_related(orig_state), rel) {
334 set_true_false_states(SMATCH_EXTRA, rel->name, rel->sym,
335 true_state, false_state);
336 } END_FOR_EACH_PTR(rel);
339 static void set_extra_chunk_true_false(struct expression *expr,
340 struct smatch_state *true_state,
341 struct smatch_state *false_state)
343 struct var_sym_list *vsl;
344 struct var_sym *vs;
345 struct symbol *type;
346 char *name;
347 struct symbol *sym;
349 if (in_warn_on_macro())
350 return;
352 type = get_type(expr);
353 if (!type)
354 return;
356 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
357 if (!name || !vsl)
358 goto free;
359 FOR_EACH_PTR(vsl, vs) {
360 store_link(link_id, vs->var, vs->sym, name, sym);
361 } END_FOR_EACH_PTR(vs);
363 set_true_false_states(SMATCH_EXTRA, name, sym,
364 clone_estate(true_state),
365 clone_estate(false_state));
366 free:
367 free_string(name);
370 static void set_extra_expr_true_false(struct expression *expr,
371 struct smatch_state *true_state,
372 struct smatch_state *false_state)
374 char *name;
375 struct symbol *sym;
376 sval_t sval;
378 if (!true_state && !false_state)
379 return;
381 if (get_value(expr, &sval))
382 return;
384 expr = strip_expr(expr);
385 name = expr_to_var_sym(expr, &sym);
386 if (!name || !sym) {
387 free_string(name);
388 set_extra_chunk_true_false(expr, true_state, false_state);
389 return;
391 set_extra_true_false(name, sym, true_state, false_state);
392 free_string(name);
395 static struct sm_state *handle_canonical_while_count_down(struct statement *loop)
397 struct expression *iter_var;
398 struct expression *condition;
399 struct sm_state *sm;
400 struct smatch_state *estate;
401 sval_t start;
403 condition = strip_expr(loop->iterator_pre_condition);
404 if (!condition)
405 return NULL;
406 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
407 return NULL;
408 if (condition->op != SPECIAL_DECREMENT)
409 return NULL;
411 iter_var = condition->unop;
412 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
413 if (!sm)
414 return NULL;
415 if (sval_cmp_val(estate_min(sm->state), 0) < 0)
416 return NULL;
417 start = estate_max(sm->state);
418 if (sval_cmp_val(start, 0) <= 0)
419 return NULL;
420 if (!sval_is_max(start))
421 start.value--;
423 if (condition->type == EXPR_PREOP) {
424 estate = alloc_estate_range(sval_type_val(start.type, 1), start);
425 if (estate_has_hard_max(sm->state))
426 estate_set_hard_max(estate);
427 estate_copy_fuzzy_max(estate, sm->state);
428 set_extra_expr_mod(iter_var, estate);
430 if (condition->type == EXPR_POSTOP) {
431 estate = alloc_estate_range(sval_type_val(start.type, 0), start);
432 if (estate_has_hard_max(sm->state))
433 estate_set_hard_max(estate);
434 estate_copy_fuzzy_max(estate, sm->state);
435 set_extra_expr_mod(iter_var, estate);
437 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
440 static struct sm_state *handle_canonical_for_inc(struct expression *iter_expr,
441 struct expression *condition)
443 struct expression *iter_var;
444 struct sm_state *sm;
445 struct smatch_state *estate;
446 sval_t start, end, max;
448 iter_var = iter_expr->unop;
449 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
450 if (!sm)
451 return NULL;
452 if (!estate_get_single_value(sm->state, &start))
453 return NULL;
454 if (get_implied_max(condition->right, &end))
455 end = sval_cast(get_type(iter_var), end);
456 else
457 end = sval_type_max(get_type(iter_var));
459 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
460 return NULL;
462 switch (condition->op) {
463 case SPECIAL_UNSIGNED_LT:
464 case SPECIAL_NOTEQUAL:
465 case '<':
466 if (!sval_is_min(end))
467 end.value--;
468 break;
469 case SPECIAL_UNSIGNED_LTE:
470 case SPECIAL_LTE:
471 break;
472 default:
473 return NULL;
475 if (sval_cmp(end, start) < 0)
476 return NULL;
477 estate = alloc_estate_range(start, end);
478 if (get_hard_max(condition->right, &max)) {
479 estate_set_hard_max(estate);
480 if (condition->op == '<' ||
481 condition->op == SPECIAL_UNSIGNED_LT ||
482 condition->op == SPECIAL_NOTEQUAL)
483 max.value--;
484 estate_set_fuzzy_max(estate, max);
486 set_extra_expr_mod(iter_var, estate);
487 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
490 static struct sm_state *handle_canonical_for_dec(struct expression *iter_expr,
491 struct expression *condition)
493 struct expression *iter_var;
494 struct sm_state *sm;
495 struct smatch_state *estate;
496 sval_t start, end;
498 iter_var = iter_expr->unop;
499 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
500 if (!sm)
501 return NULL;
502 if (!estate_get_single_value(sm->state, &start))
503 return NULL;
504 if (!get_implied_min(condition->right, &end))
505 end = sval_type_min(get_type(iter_var));
506 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
507 return NULL;
509 switch (condition->op) {
510 case SPECIAL_NOTEQUAL:
511 case '>':
512 if (!sval_is_min(end) && !sval_is_max(end))
513 end.value++;
514 break;
515 case SPECIAL_GTE:
516 break;
517 default:
518 return NULL;
520 if (sval_cmp(end, start) > 0)
521 return NULL;
522 estate = alloc_estate_range(end, start);
523 estate_set_hard_max(estate);
524 estate_set_fuzzy_max(estate, estate_get_fuzzy_max(estate));
525 set_extra_expr_mod(iter_var, estate);
526 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
529 static struct sm_state *handle_canonical_for_loops(struct statement *loop)
531 struct expression *iter_expr;
532 struct expression *condition;
534 if (!loop->iterator_post_statement)
535 return NULL;
536 if (loop->iterator_post_statement->type != STMT_EXPRESSION)
537 return NULL;
538 iter_expr = loop->iterator_post_statement->expression;
539 if (!loop->iterator_pre_condition)
540 return NULL;
541 if (loop->iterator_pre_condition->type != EXPR_COMPARE)
542 return NULL;
543 condition = loop->iterator_pre_condition;
545 if (iter_expr->op == SPECIAL_INCREMENT)
546 return handle_canonical_for_inc(iter_expr, condition);
547 if (iter_expr->op == SPECIAL_DECREMENT)
548 return handle_canonical_for_dec(iter_expr, condition);
549 return NULL;
552 struct sm_state *__extra_handle_canonical_loops(struct statement *loop, struct stree **stree)
554 struct sm_state *ret;
556 __push_fake_cur_stree();
557 if (!loop->iterator_post_statement)
558 ret = handle_canonical_while_count_down(loop);
559 else
560 ret = handle_canonical_for_loops(loop);
561 *stree = __pop_fake_cur_stree();
562 return ret;
565 int __iterator_unchanged(struct sm_state *sm)
567 if (!sm)
568 return 0;
569 if (get_sm_state(my_id, sm->name, sm->sym) == sm)
570 return 1;
571 return 0;
574 static void while_count_down_after(struct sm_state *sm, struct expression *condition)
576 sval_t after_value;
578 /* paranoid checking. prolly not needed */
579 condition = strip_expr(condition);
580 if (!condition)
581 return;
582 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
583 return;
584 if (condition->op != SPECIAL_DECREMENT)
585 return;
586 after_value = estate_min(sm->state);
587 after_value.value--;
588 set_extra_mod(sm->name, sm->sym, alloc_estate_sval(after_value));
591 void __extra_pre_loop_hook_after(struct sm_state *sm,
592 struct statement *iterator,
593 struct expression *condition)
595 struct expression *iter_expr;
596 sval_t limit;
597 struct smatch_state *state;
599 if (!iterator) {
600 while_count_down_after(sm, condition);
601 return;
604 iter_expr = iterator->expression;
606 if (condition->type != EXPR_COMPARE)
607 return;
608 if (iter_expr->op == SPECIAL_INCREMENT) {
609 limit = sval_binop(estate_max(sm->state), '+',
610 sval_type_val(estate_type(sm->state), 1));
611 } else {
612 limit = sval_binop(estate_min(sm->state), '-',
613 sval_type_val(estate_type(sm->state), 1));
615 if (!estate_has_hard_max(sm->state) && !__has_breaks()) {
616 if (iter_expr->op == SPECIAL_INCREMENT)
617 state = alloc_estate_range(estate_min(sm->state), limit);
618 else
619 state = alloc_estate_range(limit, estate_max(sm->state));
620 } else {
621 state = alloc_estate_sval(limit);
623 if (!estate_has_hard_max(sm->state)) {
624 estate_clear_hard_max(state);
626 if (estate_has_fuzzy_max(sm->state)) {
627 sval_t hmax = estate_get_fuzzy_max(sm->state);
628 sval_t max = estate_max(sm->state);
630 if (sval_cmp(hmax, max) != 0)
631 estate_clear_fuzzy_max(state);
632 } else if (!estate_has_fuzzy_max(sm->state)) {
633 estate_clear_fuzzy_max(state);
636 set_extra_mod(sm->name, sm->sym, state);
639 static struct stree *unmatched_stree;
640 static struct smatch_state *unmatched_state(struct sm_state *sm)
642 struct smatch_state *state;
644 if (unmatched_stree) {
645 state = get_state_stree(unmatched_stree, SMATCH_EXTRA, sm->name, sm->sym);
646 if (state)
647 return state;
649 if (parent_is_gone_var_sym(sm->name, sm->sym))
650 return alloc_estate_empty();
651 return alloc_estate_whole(estate_type(sm->state));
654 static void clear_the_pointed_at(struct expression *expr)
656 struct stree *stree;
657 char *name;
658 struct symbol *sym;
659 struct sm_state *tmp;
661 name = expr_to_var_sym(expr, &sym);
662 if (!name || !sym)
663 goto free;
665 stree = __get_cur_stree();
666 FOR_EACH_MY_SM(SMATCH_EXTRA, stree, tmp) {
667 if (tmp->name[0] != '*')
668 continue;
669 if (tmp->sym != sym)
670 continue;
671 if (strcmp(tmp->name + 1, name) != 0)
672 continue;
673 set_extra_mod(tmp->name, tmp->sym, alloc_estate_whole(estate_type(tmp->state)));
674 } END_FOR_EACH_SM(tmp);
676 free:
677 free_string(name);
680 static void match_function_call(struct expression *expr)
682 struct expression *arg;
683 struct expression *tmp;
685 /* if we have the db this is handled in smatch_function_hooks.c */
686 if (!option_no_db)
687 return;
688 if (inlinable(expr->fn))
689 return;
691 FOR_EACH_PTR(expr->args, arg) {
692 tmp = strip_expr(arg);
693 if (tmp->type == EXPR_PREOP && tmp->op == '&')
694 set_extra_expr_mod(tmp->unop, alloc_estate_whole(get_type(tmp->unop)));
695 else
696 clear_the_pointed_at(tmp);
697 } END_FOR_EACH_PTR(arg);
700 static int values_fit_type(struct expression *left, struct expression *right)
702 struct range_list *rl;
703 struct symbol *type;
705 type = get_type(left);
706 if (!type)
707 return 0;
708 get_absolute_rl(right, &rl);
709 if (type_unsigned(type) && sval_is_negative(rl_min(rl)))
710 return 0;
711 if (sval_cmp(sval_type_min(type), rl_min(rl)) > 0)
712 return 0;
713 if (sval_cmp(sval_type_max(type), rl_max(rl)) < 0)
714 return 0;
715 return 1;
718 static void save_chunk_info(struct expression *left, struct expression *right)
720 struct var_sym_list *vsl;
721 struct var_sym *vs;
722 struct expression *add_expr;
723 struct symbol *type;
724 sval_t sval;
725 char *name;
726 struct symbol *sym;
728 if (right->type != EXPR_BINOP || right->op != '-')
729 return;
730 if (!get_value(right->left, &sval))
731 return;
732 if (!expr_to_sym(right->right))
733 return;
735 add_expr = binop_expression(left, '+', right->right);
736 type = get_type(add_expr);
737 if (!type)
738 return;
739 name = expr_to_chunk_sym_vsl(add_expr, &sym, &vsl);
740 if (!name || !vsl)
741 goto free;
742 FOR_EACH_PTR(vsl, vs) {
743 store_link(link_id, vs->var, vs->sym, name, sym);
744 } END_FOR_EACH_PTR(vs);
746 set_state(SMATCH_EXTRA, name, sym, alloc_estate_sval(sval_cast(type, sval)));
747 free:
748 free_string(name);
751 static void do_array_assign(struct expression *left, int op, struct expression *right)
753 struct range_list *rl;
755 if (op == '=') {
756 get_absolute_rl(right, &rl);
757 rl = cast_rl(get_type(left), rl);
758 } else {
759 rl = alloc_whole_rl(get_type(left));
762 set_extra_array_mod(left, alloc_estate_rl(rl));
765 static void match_untracked_array(struct expression *call, int param)
767 struct expression *arg;
769 arg = get_argument_from_call_expr(call->args, param);
770 arg = strip_expr(arg);
772 clear_array_states(arg);
775 static void match_vanilla_assign(struct expression *left, struct expression *right)
777 struct range_list *orig_rl = NULL;
778 struct range_list *rl = NULL;
779 struct symbol *right_sym;
780 struct symbol *left_type;
781 struct symbol *right_type;
782 char *right_name = NULL;
783 struct symbol *sym;
784 char *name;
785 sval_t max;
786 struct smatch_state *state;
787 int comparison;
789 if (is_struct(left))
790 return;
792 save_chunk_info(left, right);
794 name = expr_to_var_sym(left, &sym);
795 if (!name) {
796 if (is_array(left))
797 do_array_assign(left, '=', right);
798 return;
801 left_type = get_type(left);
802 right_type = get_type(right);
804 right_name = expr_to_var_sym(right, &right_sym);
806 if (!__in_fake_assign &&
807 !(right->type == EXPR_PREOP && right->op == '&') &&
808 right_name && right_sym &&
809 values_fit_type(left, right) &&
810 !has_symbol(right, sym)) {
811 set_equiv(left, right);
812 goto free;
815 if (is_pointer(right) && get_address_rl(right, &rl)) {
816 state = alloc_estate_rl(rl);
817 goto done;
820 if (__in_fake_assign) {
821 struct smatch_state *right_state;
822 sval_t sval;
824 if (get_value(right, &sval)) {
825 sval = sval_cast(left_type, sval);
826 state = alloc_estate_sval(sval);
827 goto done;
830 right_state = get_state(SMATCH_EXTRA, right_name, right_sym);
831 if (right_state) {
832 /* simple assignment */
833 state = clone_estate(right_state);
834 goto done;
837 state = alloc_estate_rl(alloc_whole_rl(left_type));
838 goto done;
841 comparison = get_comparison(left, right);
842 if (comparison) {
843 comparison = flip_comparison(comparison);
844 get_implied_rl(left, &orig_rl);
847 if (get_implied_rl(right, &rl)) {
848 rl = cast_rl(left_type, rl);
849 if (orig_rl)
850 filter_by_comparison(&rl, comparison, orig_rl);
851 state = alloc_estate_rl(rl);
852 if (get_hard_max(right, &max)) {
853 estate_set_hard_max(state);
854 estate_set_fuzzy_max(state, max);
856 } else {
857 rl = alloc_whole_rl(right_type);
858 rl = cast_rl(left_type, rl);
859 if (orig_rl)
860 filter_by_comparison(&rl, comparison, orig_rl);
861 state = alloc_estate_rl(rl);
864 done:
865 set_extra_mod(name, sym, state);
866 free:
867 free_string(right_name);
870 static int op_remove_assign(int op)
872 switch (op) {
873 case SPECIAL_ADD_ASSIGN:
874 return '+';
875 case SPECIAL_SUB_ASSIGN:
876 return '-';
877 case SPECIAL_MUL_ASSIGN:
878 return '*';
879 case SPECIAL_DIV_ASSIGN:
880 return '/';
881 case SPECIAL_MOD_ASSIGN:
882 return '%';
883 case SPECIAL_AND_ASSIGN:
884 return '&';
885 case SPECIAL_OR_ASSIGN:
886 return '|';
887 case SPECIAL_XOR_ASSIGN:
888 return '^';
889 case SPECIAL_SHL_ASSIGN:
890 return SPECIAL_LEFTSHIFT;
891 case SPECIAL_SHR_ASSIGN:
892 return SPECIAL_RIGHTSHIFT;
893 default:
894 return op;
898 static void match_assign(struct expression *expr)
900 struct range_list *rl = NULL;
901 struct expression *left;
902 struct expression *right;
903 struct expression *binop_expr;
904 struct symbol *left_type;
905 struct symbol *sym;
906 char *name;
907 sval_t left_min, left_max;
908 sval_t right_min, right_max;
909 sval_t res_min, res_max;
911 left = strip_expr(expr->left);
913 right = strip_parens(expr->right);
914 if (right->type == EXPR_CALL && sym_name_is("__builtin_expect", right->fn))
915 right = get_argument_from_call_expr(right->args, 0);
916 while (right->type == EXPR_ASSIGNMENT && right->op == '=')
917 right = strip_parens(right->left);
919 if (expr->op == '=' && is_condition(expr->right))
920 return; /* handled in smatch_condition.c */
921 if (expr->op == '=' && right->type == EXPR_CALL)
922 return; /* handled in smatch_function_hooks.c */
923 if (expr->op == '=') {
924 match_vanilla_assign(left, right);
925 return;
928 name = expr_to_var_sym(left, &sym);
929 if (!name)
930 return;
932 left_type = get_type(left);
934 res_min = sval_type_min(left_type);
935 res_max = sval_type_max(left_type);
937 switch (expr->op) {
938 case SPECIAL_ADD_ASSIGN:
939 get_absolute_max(left, &left_max);
940 get_absolute_max(right, &right_max);
941 if (sval_binop_overflows(left_max, '+', right_max))
942 break;
943 if (get_implied_min(left, &left_min) &&
944 !sval_is_negative_min(left_min) &&
945 get_implied_min(right, &right_min) &&
946 !sval_is_negative_min(right_min)) {
947 res_min = sval_binop(left_min, '+', right_min);
948 res_min = sval_cast(left_type, res_min);
950 if (inside_loop()) /* we are assuming loops don't lead to wrapping */
951 break;
952 res_max = sval_binop(left_max, '+', right_max);
953 res_max = sval_cast(left_type, res_max);
954 break;
955 case SPECIAL_SUB_ASSIGN:
956 if (get_implied_max(left, &left_max) &&
957 !sval_is_max(left_max) &&
958 get_implied_min(right, &right_min) &&
959 !sval_is_min(right_min)) {
960 res_max = sval_binop(left_max, '-', right_min);
961 res_max = sval_cast(left_type, res_max);
963 if (inside_loop())
964 break;
965 if (get_implied_min(left, &left_min) &&
966 !sval_is_min(left_min) &&
967 get_implied_max(right, &right_max) &&
968 !sval_is_max(right_max) &&
969 sval_cmp(left_min, right_max) > 0) {
970 res_min = sval_binop(left_min, '-', right_max);
971 res_min = sval_cast(left_type, res_min);
973 break;
974 case SPECIAL_AND_ASSIGN:
975 case SPECIAL_MOD_ASSIGN:
976 case SPECIAL_SHL_ASSIGN:
977 case SPECIAL_SHR_ASSIGN:
978 case SPECIAL_OR_ASSIGN:
979 case SPECIAL_XOR_ASSIGN:
980 case SPECIAL_MUL_ASSIGN:
981 case SPECIAL_DIV_ASSIGN:
982 binop_expr = binop_expression(expr->left,
983 op_remove_assign(expr->op),
984 expr->right);
985 if (get_absolute_rl(binop_expr, &rl)) {
986 rl = cast_rl(left_type, rl);
987 set_extra_mod(name, sym, alloc_estate_rl(rl));
988 goto free;
990 break;
992 rl = cast_rl(left_type, alloc_rl(res_min, res_max));
993 set_extra_mod(name, sym, alloc_estate_rl(rl));
994 free:
995 free_string(name);
998 static struct smatch_state *increment_state(struct smatch_state *state)
1000 sval_t min = estate_min(state);
1001 sval_t max = estate_max(state);
1003 if (!estate_rl(state))
1004 return NULL;
1006 if (inside_loop())
1007 max = sval_type_max(max.type);
1009 if (!sval_is_min(min) && !sval_is_max(min))
1010 min.value++;
1011 if (!sval_is_min(max) && !sval_is_max(max))
1012 max.value++;
1013 return alloc_estate_range(min, max);
1016 static struct smatch_state *decrement_state(struct smatch_state *state)
1018 sval_t min = estate_min(state);
1019 sval_t max = estate_max(state);
1021 if (!estate_rl(state))
1022 return NULL;
1024 if (inside_loop())
1025 min = sval_type_min(min.type);
1027 if (!sval_is_min(min) && !sval_is_max(min))
1028 min.value--;
1029 if (!sval_is_min(max) && !sval_is_max(max))
1030 max.value--;
1031 return alloc_estate_range(min, max);
1034 static void clear_pointed_at_state(struct expression *expr)
1036 struct symbol *type;
1039 * ALERT: This is sort of a mess. If it's is a struct assigment like
1040 * "foo = bar;", then that's handled by smatch_struct_assignment.c.
1041 * the same thing for p++ where "p" is a struct. Most modifications
1042 * are handled by the assignment hook or the db. Smatch_extra.c doesn't
1043 * use smatch_modification.c because we have to get the ordering right
1044 * or something. So if you have p++ where p is a pointer to a standard
1045 * c type then we handle that here. What a mess.
1048 type = get_type(expr);
1049 if (!type || type->type != SYM_PTR)
1050 return;
1051 type = get_real_base_type(type);
1052 if (!type || type->type != SYM_BASETYPE)
1053 return;
1054 set_extra_expr_mod(deref_expression(expr), alloc_estate_whole(type));
1057 static void unop_expr(struct expression *expr)
1059 struct smatch_state *state;
1061 if (expr->smatch_flags & Handled)
1062 return;
1064 switch (expr->op) {
1065 case SPECIAL_INCREMENT:
1066 state = get_state_expr(SMATCH_EXTRA, expr->unop);
1067 state = increment_state(state);
1068 if (!state)
1069 state = alloc_estate_whole(get_type(expr));
1070 set_extra_expr_mod(expr->unop, state);
1071 clear_pointed_at_state(expr->unop);
1072 break;
1073 case SPECIAL_DECREMENT:
1074 state = get_state_expr(SMATCH_EXTRA, expr->unop);
1075 state = decrement_state(state);
1076 if (!state)
1077 state = alloc_estate_whole(get_type(expr));
1078 set_extra_expr_mod(expr->unop, state);
1079 clear_pointed_at_state(expr->unop);
1080 break;
1081 default:
1082 return;
1086 static void asm_expr(struct statement *stmt)
1089 struct expression *expr;
1090 struct symbol *type;
1091 int state = 0;
1093 FOR_EACH_PTR(stmt->asm_outputs, expr) {
1094 switch (state) {
1095 case 0: /* identifier */
1096 case 1: /* constraint */
1097 state++;
1098 continue;
1099 case 2: /* expression */
1100 state = 0;
1101 type = get_type(strip_expr(expr));
1102 set_extra_expr_mod(expr, alloc_estate_whole(type));
1103 continue;
1105 } END_FOR_EACH_PTR(expr);
1108 static void check_dereference(struct expression *expr)
1110 if (__in_fake_assign)
1111 return;
1112 if (outside_of_function())
1113 return;
1114 if (implied_not_equal(expr, 0))
1115 return;
1116 set_extra_expr_nomod(expr, alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval));
1119 static void match_dereferences(struct expression *expr)
1121 if (expr->type != EXPR_PREOP)
1122 return;
1123 /* it's saying that foo[1] = bar dereferences foo[1] */
1124 if (is_array(expr))
1125 return;
1126 check_dereference(expr->unop);
1129 static void match_pointer_as_array(struct expression *expr)
1131 if (!is_array(expr))
1132 return;
1133 check_dereference(get_array_base(expr));
1136 static void find_dereferences(struct expression *expr)
1138 while (expr->type == EXPR_PREOP) {
1139 if (expr->op == '*')
1140 check_dereference(expr->unop);
1141 expr = strip_expr(expr->unop);
1145 static void set_param_dereferenced(struct expression *arg, char *key, char *unused)
1147 struct symbol *sym;
1148 char *name;
1150 name = get_variable_from_key(arg, key, &sym);
1151 if (name && sym)
1152 set_extra_nomod(name, sym, alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval));
1153 free_string(name);
1155 find_dereferences(arg);
1158 static sval_t add_one(sval_t sval)
1160 sval.value++;
1161 return sval;
1164 static int handle_postop_inc(struct expression *left, int op, struct expression *right)
1166 struct statement *stmt;
1167 struct expression *cond;
1168 struct smatch_state *true_state, *false_state;
1169 sval_t start;
1170 sval_t limit;
1173 * If we're decrementing here then that's a canonical while count down
1174 * so it's handled already. We're only handling loops like:
1175 * i = 0;
1176 * do { ... } while (i++ < 3);
1179 if (left->type != EXPR_POSTOP || left->op != SPECIAL_INCREMENT)
1180 return 0;
1182 stmt = __cur_stmt->parent;
1183 if (!stmt)
1184 return 0;
1185 if (stmt->type == STMT_COMPOUND)
1186 stmt = stmt->parent;
1187 if (!stmt || stmt->type != STMT_ITERATOR || !stmt->iterator_post_condition)
1188 return 0;
1190 cond = strip_expr(stmt->iterator_post_condition);
1191 if (cond->type != EXPR_COMPARE || cond->op != op)
1192 return 0;
1193 if (left != strip_expr(cond->left) || right != strip_expr(cond->right))
1194 return 0;
1196 if (!get_implied_value(left->unop, &start))
1197 return 0;
1198 if (!get_implied_value(right, &limit))
1199 return 0;
1201 if (sval_cmp(start, limit) > 0)
1202 return 0;
1204 switch (op) {
1205 case '<':
1206 case SPECIAL_UNSIGNED_LT:
1207 break;
1208 case SPECIAL_LTE:
1209 case SPECIAL_UNSIGNED_LTE:
1210 limit = add_one(limit);
1211 default:
1212 return 0;
1216 true_state = alloc_estate_range(add_one(start), limit);
1217 false_state = alloc_estate_range(add_one(limit), add_one(limit));
1219 /* Currently we just discard the false state but when two passes is
1220 * implimented correctly then it will use it.
1223 set_extra_expr_true_false(left->unop, true_state, false_state);
1225 return 1;
1228 static void handle_comparison(struct symbol *type, struct expression *left, int op, struct expression *right)
1230 struct range_list *left_orig;
1231 struct range_list *left_true;
1232 struct range_list *left_false;
1233 struct range_list *right_orig;
1234 struct range_list *right_true;
1235 struct range_list *right_false;
1236 struct smatch_state *left_true_state;
1237 struct smatch_state *left_false_state;
1238 struct smatch_state *right_true_state;
1239 struct smatch_state *right_false_state;
1240 sval_t dummy, hard_max;
1241 int left_postop = 0;
1242 int right_postop = 0;
1244 if (left->op == SPECIAL_INCREMENT || left->op == SPECIAL_DECREMENT) {
1245 if (left->type == EXPR_POSTOP) {
1246 left->smatch_flags |= Handled;
1247 left_postop = left->op;
1248 if (handle_postop_inc(left, op, right))
1249 return;
1251 left = strip_parens(left->unop);
1253 while (left->type == EXPR_ASSIGNMENT)
1254 left = strip_parens(left->left);
1256 if (right->op == SPECIAL_INCREMENT || right->op == SPECIAL_DECREMENT) {
1257 if (right->type == EXPR_POSTOP) {
1258 right->smatch_flags |= Handled;
1259 right_postop = right->op;
1261 right = strip_parens(right->unop);
1264 get_real_absolute_rl(left, &left_orig);
1265 left_orig = cast_rl(type, left_orig);
1267 get_real_absolute_rl(right, &right_orig);
1268 right_orig = cast_rl(type, right_orig);
1270 split_comparison_rl(left_orig, op, right_orig, &left_true, &left_false, &right_true, &right_false);
1272 left_true = rl_truncate_cast(get_type(strip_expr(left)), left_true);
1273 left_false = rl_truncate_cast(get_type(strip_expr(left)), left_false);
1274 right_true = rl_truncate_cast(get_type(strip_expr(right)), right_true);
1275 right_false = rl_truncate_cast(get_type(strip_expr(right)), right_false);
1277 if (!left_true || !left_false) {
1278 struct range_list *tmp_true, *tmp_false;
1280 split_comparison_rl(alloc_whole_rl(type), op, right_orig, &tmp_true, &tmp_false, NULL, NULL);
1281 tmp_true = rl_truncate_cast(get_type(strip_expr(left)), tmp_true);
1282 tmp_false = rl_truncate_cast(get_type(strip_expr(left)), tmp_false);
1283 if (tmp_true && tmp_false)
1284 __save_imaginary_state(left, tmp_true, tmp_false);
1287 if (!right_true || !right_false) {
1288 struct range_list *tmp_true, *tmp_false;
1290 split_comparison_rl(alloc_whole_rl(type), op, right_orig, NULL, NULL, &tmp_true, &tmp_false);
1291 tmp_true = rl_truncate_cast(get_type(strip_expr(right)), tmp_true);
1292 tmp_false = rl_truncate_cast(get_type(strip_expr(right)), tmp_false);
1293 if (tmp_true && tmp_false)
1294 __save_imaginary_state(right, tmp_true, tmp_false);
1297 left_true_state = alloc_estate_rl(left_true);
1298 left_false_state = alloc_estate_rl(left_false);
1299 right_true_state = alloc_estate_rl(right_true);
1300 right_false_state = alloc_estate_rl(right_false);
1302 switch (op) {
1303 case '<':
1304 case SPECIAL_UNSIGNED_LT:
1305 case SPECIAL_UNSIGNED_LTE:
1306 case SPECIAL_LTE:
1307 if (get_hard_max(right, &dummy))
1308 estate_set_hard_max(left_true_state);
1309 if (get_hard_max(left, &dummy))
1310 estate_set_hard_max(right_false_state);
1311 break;
1312 case '>':
1313 case SPECIAL_UNSIGNED_GT:
1314 case SPECIAL_UNSIGNED_GTE:
1315 case SPECIAL_GTE:
1316 if (get_hard_max(left, &dummy))
1317 estate_set_hard_max(right_true_state);
1318 if (get_hard_max(right, &dummy))
1319 estate_set_hard_max(left_false_state);
1320 break;
1323 switch (op) {
1324 case '<':
1325 case SPECIAL_UNSIGNED_LT:
1326 case SPECIAL_UNSIGNED_LTE:
1327 case SPECIAL_LTE:
1328 if (get_hard_max(right, &hard_max)) {
1329 if (op == '<' || op == SPECIAL_UNSIGNED_LT)
1330 hard_max.value--;
1331 estate_set_fuzzy_max(left_true_state, hard_max);
1333 if (get_implied_value(right, &hard_max)) {
1334 if (op == SPECIAL_UNSIGNED_LTE ||
1335 op == SPECIAL_LTE)
1336 hard_max.value++;
1337 estate_set_fuzzy_max(left_false_state, hard_max);
1339 if (get_hard_max(left, &hard_max)) {
1340 if (op == SPECIAL_UNSIGNED_LTE ||
1341 op == SPECIAL_LTE)
1342 hard_max.value--;
1343 estate_set_fuzzy_max(right_false_state, hard_max);
1345 if (get_implied_value(left, &hard_max)) {
1346 if (op == '<' || op == SPECIAL_UNSIGNED_LT)
1347 hard_max.value++;
1348 estate_set_fuzzy_max(right_true_state, hard_max);
1350 break;
1351 case '>':
1352 case SPECIAL_UNSIGNED_GT:
1353 case SPECIAL_UNSIGNED_GTE:
1354 case SPECIAL_GTE:
1355 if (get_hard_max(left, &hard_max)) {
1356 if (op == '>' || op == SPECIAL_UNSIGNED_GT)
1357 hard_max.value--;
1358 estate_set_fuzzy_max(right_true_state, hard_max);
1360 if (get_implied_value(left, &hard_max)) {
1361 if (op == SPECIAL_UNSIGNED_GTE ||
1362 op == SPECIAL_GTE)
1363 hard_max.value++;
1364 estate_set_fuzzy_max(right_false_state, hard_max);
1366 if (get_hard_max(right, &hard_max)) {
1367 if (op == SPECIAL_UNSIGNED_LTE ||
1368 op == SPECIAL_LTE)
1369 hard_max.value--;
1370 estate_set_fuzzy_max(left_false_state, hard_max);
1372 if (get_implied_value(right, &hard_max)) {
1373 if (op == '>' ||
1374 op == SPECIAL_UNSIGNED_GT)
1375 hard_max.value++;
1376 estate_set_fuzzy_max(left_true_state, hard_max);
1378 break;
1379 case SPECIAL_EQUAL:
1380 if (get_hard_max(left, &hard_max))
1381 estate_set_fuzzy_max(right_true_state, hard_max);
1382 if (get_hard_max(right, &hard_max))
1383 estate_set_fuzzy_max(left_true_state, hard_max);
1384 break;
1387 if (get_hard_max(left, &hard_max)) {
1388 estate_set_hard_max(left_true_state);
1389 estate_set_hard_max(left_false_state);
1391 if (get_hard_max(right, &hard_max)) {
1392 estate_set_hard_max(right_true_state);
1393 estate_set_hard_max(right_false_state);
1396 if (left_postop == SPECIAL_INCREMENT) {
1397 left_true_state = increment_state(left_true_state);
1398 left_false_state = increment_state(left_false_state);
1400 if (left_postop == SPECIAL_DECREMENT) {
1401 left_true_state = decrement_state(left_true_state);
1402 left_false_state = decrement_state(left_false_state);
1404 if (right_postop == SPECIAL_INCREMENT) {
1405 right_true_state = increment_state(right_true_state);
1406 right_false_state = increment_state(right_false_state);
1408 if (right_postop == SPECIAL_DECREMENT) {
1409 right_true_state = decrement_state(right_true_state);
1410 right_false_state = decrement_state(right_false_state);
1413 if (estate_rl(left_true_state) && estates_equiv(left_true_state, left_false_state)) {
1414 left_true_state = NULL;
1415 left_false_state = NULL;
1418 if (estate_rl(right_true_state) && estates_equiv(right_true_state, right_false_state)) {
1419 right_true_state = NULL;
1420 right_false_state = NULL;
1423 set_extra_expr_true_false(left, left_true_state, left_false_state);
1424 set_extra_expr_true_false(right, right_true_state, right_false_state);
1427 static int is_simple_math(struct expression *expr)
1429 if (!expr)
1430 return 0;
1431 if (expr->type != EXPR_BINOP)
1432 return 0;
1433 switch (expr->op) {
1434 case '+':
1435 case '-':
1436 case '*':
1437 return 1;
1439 return 0;
1442 static void move_known_values(struct expression **left_p, struct expression **right_p)
1444 struct expression *left = *left_p;
1445 struct expression *right = *right_p;
1446 sval_t sval, dummy;
1448 if (get_implied_value(left, &sval)) {
1449 if (!is_simple_math(right))
1450 return;
1451 if (get_implied_value(right, &dummy))
1452 return;
1453 if (right->op == '*') {
1454 sval_t divisor;
1456 if (!get_value(right->right, &divisor))
1457 return;
1458 if (divisor.value == 0 && sval.value % divisor.value)
1459 return;
1460 *left_p = binop_expression(left, invert_op(right->op), right->right);
1461 *right_p = right->left;
1462 return;
1464 if (right->op == '+' && get_value(right->left, &sval)) {
1465 *left_p = binop_expression(left, invert_op(right->op), right->left);
1466 *right_p = right->right;
1467 return;
1469 if (get_value(right->right, &sval)) {
1470 *left_p = binop_expression(left, invert_op(right->op), right->right);
1471 *right_p = right->left;
1472 return;
1474 return;
1476 if (get_implied_value(right, &sval)) {
1477 if (!is_simple_math(left))
1478 return;
1479 if (get_implied_value(left, &dummy))
1480 return;
1481 if (left->op == '*') {
1482 sval_t divisor;
1484 if (!get_value(left->right, &divisor))
1485 return;
1486 if (divisor.value == 0 && sval.value % divisor.value)
1487 return;
1488 *right_p = binop_expression(right, invert_op(left->op), left->right);
1489 *left_p = left->left;
1490 return;
1492 if (left->op == '+' && get_value(left->left, &sval)) {
1493 *right_p = binop_expression(right, invert_op(left->op), left->left);
1494 *left_p = left->right;
1495 return;
1498 if (get_value(left->right, &sval)) {
1499 *right_p = binop_expression(right, invert_op(left->op), left->right);
1500 *left_p = left->left;
1501 return;
1503 return;
1507 static int match_func_comparison(struct expression *expr)
1509 struct expression *left = strip_expr(expr->left);
1510 struct expression *right = strip_expr(expr->right);
1511 sval_t sval;
1514 * fixme: think about this harder. We should always be trying to limit
1515 * the non-call side as well. If we can't determine the limitter does
1516 * that mean we aren't querying the database and are missing important
1517 * information?
1520 if (left->type == EXPR_CALL) {
1521 if (get_implied_value(left, &sval)) {
1522 handle_comparison(get_type(expr), left, expr->op, right);
1523 return 1;
1525 function_comparison(left, expr->op, right);
1526 return 1;
1529 if (right->type == EXPR_CALL) {
1530 if (get_implied_value(right, &sval)) {
1531 handle_comparison(get_type(expr), left, expr->op, right);
1532 return 1;
1534 function_comparison(left, expr->op, right);
1535 return 1;
1538 return 0;
1541 /* Handle conditions like "if (foo + bar < foo) {" */
1542 static int handle_integer_overflow_test(struct expression *expr)
1544 struct expression *left, *right;
1545 struct symbol *type;
1546 sval_t left_min, right_min, min, max;
1548 if (expr->op != '<' && expr->op != SPECIAL_UNSIGNED_LT)
1549 return 0;
1551 left = strip_parens(expr->left);
1552 right = strip_parens(expr->right);
1554 if (left->op != '+')
1555 return 0;
1557 type = get_type(expr);
1558 if (!type)
1559 return 0;
1560 if (type_positive_bits(type) == 32) {
1561 max.type = &uint_ctype;
1562 max.uvalue = (unsigned int)-1;
1563 } else if (type_positive_bits(type) == 64) {
1564 max.type = &ulong_ctype;
1565 max.value = (unsigned long long)-1;
1566 } else {
1567 return 0;
1570 if (!expr_equiv(left->left, right) && !expr_equiv(left->right, right))
1571 return 0;
1573 get_absolute_min(left->left, &left_min);
1574 get_absolute_min(left->right, &right_min);
1575 min = sval_binop(left_min, '+', right_min);
1577 set_extra_chunk_true_false(left, NULL, alloc_estate_range(min, max));
1578 return 1;
1581 static void match_comparison(struct expression *expr)
1583 struct expression *left_orig = strip_parens(expr->left);
1584 struct expression *right_orig = strip_parens(expr->right);
1585 struct expression *left, *right;
1586 struct expression *prev;
1587 struct symbol *type;
1589 if (match_func_comparison(expr))
1590 return;
1592 type = get_type(expr);
1593 if (!type)
1594 type = &llong_ctype;
1596 if (handle_integer_overflow_test(expr))
1597 return;
1599 left = left_orig;
1600 right = right_orig;
1601 move_known_values(&left, &right);
1602 handle_comparison(type, left, expr->op, right);
1604 prev = get_assigned_expr(left_orig);
1605 if (is_simple_math(prev) && has_variable(prev, left_orig) == 0) {
1606 left = prev;
1607 right = right_orig;
1608 move_known_values(&left, &right);
1609 handle_comparison(type, left, expr->op, right);
1612 prev = get_assigned_expr(right_orig);
1613 if (is_simple_math(prev) && has_variable(prev, right_orig) == 0) {
1614 left = left_orig;
1615 right = prev;
1616 move_known_values(&left, &right);
1617 handle_comparison(type, left, expr->op, right);
1621 static sval_t get_high_mask(sval_t known)
1623 sval_t ret;
1624 int i;
1626 ret = known;
1627 ret.value = 0;
1629 for (i = type_bits(known.type) - 1; i >= 0; i--) {
1630 if (known.uvalue & (1ULL << i))
1631 ret.uvalue |= (1ULL << i);
1632 else
1633 return ret;
1636 return ret;
1639 static void handle_AND_condition(struct expression *expr)
1641 struct range_list *orig_rl;
1642 struct range_list *true_rl = NULL;
1643 struct range_list *false_rl = NULL;
1644 sval_t known;
1645 int bit;
1647 if (get_implied_value(expr->left, &known)) {
1648 sval_t low_mask = known;
1649 sval_t high_mask;
1651 if (known.value > 0) {
1652 bit = ffsll(known.value) - 1;
1653 low_mask.uvalue = (1ULL << bit) - 1;
1654 get_absolute_rl(expr->right, &orig_rl);
1655 true_rl = remove_range(orig_rl, sval_type_val(known.type, 0), low_mask);
1657 high_mask = get_high_mask(known);
1658 if (high_mask.value) {
1659 bit = ffsll(high_mask.value) - 1;
1660 low_mask.uvalue = (1ULL << bit) - 1;
1662 get_absolute_rl(expr->left, &orig_rl);
1663 if (sval_is_negative(rl_min(orig_rl)))
1664 orig_rl = remove_range(orig_rl, sval_type_min(known.type), sval_type_val(known.type, -1));
1665 false_rl = remove_range(orig_rl, low_mask, sval_type_max(known.type));
1666 if (type_signed(high_mask.type) && type_unsigned(rl_type(false_rl))) {
1667 false_rl = remove_range(false_rl,
1668 sval_type_val(rl_type(false_rl), sval_type_max(known.type).uvalue),
1669 sval_type_val(rl_type(false_rl), -1));
1672 set_extra_expr_true_false(expr->right,
1673 true_rl ? alloc_estate_rl(true_rl) : NULL,
1674 false_rl ? alloc_estate_rl(false_rl) : NULL);
1676 return;
1679 if (get_implied_value(expr->right, &known)) {
1680 sval_t low_mask = known;
1681 sval_t high_mask;
1683 if (known.value > 0) {
1684 bit = ffsll(known.value) - 1;
1685 low_mask.uvalue = (1ULL << bit) - 1;
1686 get_absolute_rl(expr->left, &orig_rl);
1687 true_rl = remove_range(orig_rl, sval_type_val(known.type, 0), low_mask);
1689 high_mask = get_high_mask(known);
1690 if (high_mask.value) {
1691 bit = ffsll(high_mask.value) - 1;
1692 low_mask.uvalue = (1ULL << bit) - 1;
1694 get_absolute_rl(expr->left, &orig_rl);
1695 if (sval_is_negative(rl_min(orig_rl)))
1696 orig_rl = remove_range(orig_rl, sval_type_min(known.type), sval_type_val(known.type, -1));
1697 false_rl = remove_range(orig_rl, low_mask, sval_type_max(known.type));
1698 if (type_signed(high_mask.type) && type_unsigned(rl_type(false_rl))) {
1699 false_rl = remove_range(false_rl,
1700 sval_type_val(rl_type(false_rl), sval_type_max(known.type).uvalue),
1701 sval_type_val(rl_type(false_rl), -1));
1704 set_extra_expr_true_false(expr->left,
1705 true_rl ? alloc_estate_rl(true_rl) : NULL,
1706 false_rl ? alloc_estate_rl(false_rl) : NULL);
1707 return;
1711 static void handle_MOD_condition(struct expression *expr)
1713 struct range_list *orig_rl;
1714 struct range_list *true_rl;
1715 struct range_list *false_rl = NULL;
1716 sval_t right;
1717 sval_t zero = {
1718 .value = 0,
1721 if (!get_implied_value(expr->right, &right) || right.value == 0)
1722 return;
1723 get_absolute_rl(expr->left, &orig_rl);
1725 zero.type = rl_type(orig_rl);
1727 /* We're basically dorking around the min and max here */
1728 true_rl = remove_range(orig_rl, zero, zero);
1729 if (!sval_is_max(rl_max(true_rl)) &&
1730 !(rl_max(true_rl).value % right.value))
1731 true_rl = remove_range(true_rl, rl_max(true_rl), rl_max(true_rl));
1733 if (rl_equiv(true_rl, orig_rl))
1734 true_rl = NULL;
1736 if (sval_is_positive(rl_min(orig_rl)) &&
1737 (rl_max(orig_rl).value - rl_min(orig_rl).value) / right.value < 5) {
1738 sval_t add;
1739 int i;
1741 add = rl_min(orig_rl);
1742 add.value += right.value - (add.value % right.value);
1743 add.value -= right.value;
1745 for (i = 0; i < 5; i++) {
1746 add.value += right.value;
1747 if (add.value > rl_max(orig_rl).value)
1748 break;
1749 add_range(&false_rl, add, add);
1751 } else {
1752 if (rl_min(orig_rl).uvalue != 0 &&
1753 rl_min(orig_rl).uvalue < right.uvalue) {
1754 sval_t chop = right;
1755 chop.value--;
1756 false_rl = remove_range(orig_rl, zero, chop);
1759 if (!sval_is_max(rl_max(orig_rl)) &&
1760 (rl_max(orig_rl).value % right.value)) {
1761 sval_t chop = rl_max(orig_rl);
1762 chop.value -= chop.value % right.value;
1763 chop.value++;
1764 if (!false_rl)
1765 false_rl = clone_rl(orig_rl);
1766 false_rl = remove_range(false_rl, chop, rl_max(orig_rl));
1770 set_extra_expr_true_false(expr->left,
1771 true_rl ? alloc_estate_rl(true_rl) : NULL,
1772 false_rl ? alloc_estate_rl(false_rl) : NULL);
1775 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
1776 void __extra_match_condition(struct expression *expr)
1778 struct smatch_state *pre_state;
1779 struct smatch_state *true_state;
1780 struct smatch_state *false_state;
1782 expr = strip_expr(expr);
1783 switch (expr->type) {
1784 case EXPR_CALL:
1785 function_comparison(expr, SPECIAL_NOTEQUAL, zero_expr());
1786 return;
1787 case EXPR_PREOP:
1788 case EXPR_SYMBOL:
1789 case EXPR_DEREF: {
1790 sval_t zero;
1792 zero = sval_blank(expr);
1793 zero.value = 0;
1795 pre_state = get_extra_state(expr);
1796 true_state = estate_filter_sval(pre_state, zero);
1797 if (possibly_true(expr, SPECIAL_EQUAL, zero_expr()))
1798 false_state = alloc_estate_sval(zero);
1799 else
1800 false_state = alloc_estate_empty();
1801 set_extra_expr_true_false(expr, true_state, false_state);
1802 return;
1804 case EXPR_COMPARE:
1805 match_comparison(expr);
1806 return;
1807 case EXPR_ASSIGNMENT:
1808 __extra_match_condition(expr->left);
1809 return;
1810 case EXPR_BINOP:
1811 if (expr->op == '&')
1812 handle_AND_condition(expr);
1813 if (expr->op == '%')
1814 handle_MOD_condition(expr);
1815 return;
1819 static void assume_indexes_are_valid(struct expression *expr)
1821 struct expression *array_expr;
1822 int array_size;
1823 struct expression *offset;
1824 struct symbol *offset_type;
1825 struct range_list *rl_before;
1826 struct range_list *rl_after;
1827 struct range_list *filter = NULL;
1828 sval_t size;
1830 expr = strip_expr(expr);
1831 if (!is_array(expr))
1832 return;
1834 offset = get_array_offset(expr);
1835 offset_type = get_type(offset);
1836 if (offset_type && type_signed(offset_type)) {
1837 filter = alloc_rl(sval_type_min(offset_type),
1838 sval_type_val(offset_type, -1));
1841 array_expr = get_array_base(expr);
1842 array_size = get_real_array_size(array_expr);
1843 if (array_size > 1) {
1844 size = sval_type_val(offset_type, array_size);
1845 add_range(&filter, size, sval_type_max(offset_type));
1848 if (!filter)
1849 return;
1850 get_absolute_rl(offset, &rl_before);
1851 rl_after = rl_filter(rl_before, filter);
1852 if (rl_equiv(rl_before, rl_after))
1853 return;
1854 set_extra_expr_nomod(offset, alloc_estate_rl(rl_after));
1857 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
1858 int implied_not_equal(struct expression *expr, long long val)
1860 return !possibly_false(expr, SPECIAL_NOTEQUAL, value_expr(val));
1863 int implied_not_equal_name_sym(char *name, struct symbol *sym, long long val)
1865 struct smatch_state *estate;
1867 estate = get_state(SMATCH_EXTRA, name, sym);
1868 if (!estate)
1869 return 0;
1870 if (!rl_has_sval(estate_rl(estate), sval_type_val(estate_type(estate), 0)))
1871 return 1;
1872 return 0;
1875 int parent_is_null_var_sym(const char *name, struct symbol *sym)
1877 char buf[256];
1878 char *start;
1879 char *end;
1880 struct smatch_state *state;
1882 strncpy(buf, name, sizeof(buf) - 1);
1883 buf[sizeof(buf) - 1] = '\0';
1885 start = &buf[0];
1886 while (*start == '*') {
1887 start++;
1888 state = get_state(SMATCH_EXTRA, start, sym);
1889 if (!state)
1890 continue;
1891 if (!estate_rl(state))
1892 return 1;
1893 if (estate_min(state).value == 0 &&
1894 estate_max(state).value == 0)
1895 return 1;
1898 start = &buf[0];
1899 while (*start == '&')
1900 start++;
1902 while ((end = strrchr(start, '-'))) {
1903 *end = '\0';
1904 state = get_state(SMATCH_EXTRA, start, sym);
1905 if (!state)
1906 continue;
1907 if (estate_min(state).value == 0 &&
1908 estate_max(state).value == 0)
1909 return 1;
1911 return 0;
1914 int parent_is_null(struct expression *expr)
1916 struct symbol *sym;
1917 char *var;
1918 int ret = 0;
1920 expr = strip_expr(expr);
1921 var = expr_to_var_sym(expr, &sym);
1922 if (!var || !sym)
1923 goto free;
1924 ret = parent_is_null_var_sym(var, sym);
1925 free:
1926 free_string(var);
1927 return ret;
1930 static int param_used_callback(void *found, int argc, char **argv, char **azColName)
1932 *(int *)found = 1;
1933 return 0;
1936 static int filter_unused_kzalloc_info(struct expression *call, int param, char *printed_name, struct sm_state *sm)
1938 sval_t sval;
1939 int found = 0;
1941 /* for function pointers assume everything is used */
1942 if (call->fn->type != EXPR_SYMBOL)
1943 return 0;
1946 * kzalloc() information is treated as special because so there is just
1947 * a lot of stuff initialized to zero and it makes building the database
1948 * take hours and hours.
1950 * In theory, we should just remove this line and not pass any unused
1951 * information, but I'm not sure enough that this code works so I want
1952 * to hold off on that for now.
1954 if (!estate_get_single_value(sm->state, &sval) || sval.value != 0)
1955 return 0;
1957 run_sql(&param_used_callback, &found,
1958 "select * from call_implies where %s and type = %d and parameter = %d and key = '%s';",
1959 get_static_filter(call->fn->symbol), PARAM_USED, param, printed_name);
1960 if (found)
1961 return 0;
1963 /* If the database is not built yet, then assume everything is used */
1964 run_sql(&param_used_callback, &found,
1965 "select * from call_implies where %s and type = %d;",
1966 get_static_filter(call->fn->symbol), PARAM_USED);
1967 if (!found)
1968 return 0;
1970 return 1;
1973 struct range_list *intersect_with_real_abs_var_sym(const char *name, struct symbol *sym, struct range_list *start)
1975 struct smatch_state *state;
1978 * Here is the difference between implied value and real absolute, say
1979 * you have:
1981 * int a = (u8)x;
1983 * Then you know that a is 0-255. That's real absolute. But you don't
1984 * know for sure that it actually goes up to 255. So it's not implied.
1985 * Implied indicates a degree of certainty.
1987 * But then say you cap "a" at 8. That means you know it goes up to
1988 * 8. So now the implied value is s32min-8. But you can combine it
1989 * with the real absolute to say that actually it's 0-8.
1991 * We are combining it here. But now that I think about it, this is
1992 * probably not the ideal place to combine it because it should proably
1993 * be done earlier. Oh well, this is an improvement on what was there
1994 * before so I'm going to commit this code.
1998 state = get_real_absolute_state_var_sym(name, sym);
1999 if (!state || !estate_rl(state))
2000 return start;
2002 return rl_intersection(estate_rl(state), start);
2005 struct range_list *intersect_with_real_abs_expr(struct expression *expr, struct range_list *start)
2007 struct smatch_state *state;
2009 state = get_real_absolute_state(expr);
2010 if (!state || !estate_rl(state))
2011 return start;
2013 return rl_intersection(estate_rl(state), start);
2016 static void struct_member_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
2018 struct range_list *rl;
2020 if (estate_is_whole(sm->state))
2021 return;
2022 if (filter_unused_kzalloc_info(call, param, printed_name, sm))
2023 return;
2024 rl = estate_rl(sm->state);
2025 rl = intersect_with_real_abs_var_sym(sm->name, sm->sym, rl);
2026 sql_insert_caller_info(call, PARAM_VALUE, param, printed_name, show_rl(rl));
2027 if (estate_has_fuzzy_max(sm->state))
2028 sql_insert_caller_info(call, FUZZY_MAX, param, printed_name,
2029 sval_to_str(estate_get_fuzzy_max(sm->state)));
2032 static void returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
2034 struct symbol *returned_sym;
2035 struct sm_state *sm;
2036 const char *param_name;
2037 char *compare_str;
2038 char buf[256];
2040 returned_sym = expr_to_sym(expr);
2041 if (!returned_sym)
2042 return;
2044 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
2045 if (!estate_rl(sm->state))
2046 continue;
2047 if (returned_sym != sm->sym)
2048 continue;
2050 param_name = get_param_name(sm);
2051 if (!param_name)
2052 continue;
2053 if (strcmp(param_name, "$") == 0)
2054 continue;
2055 compare_str = name_sym_to_param_comparison(sm->name, sm->sym);
2056 if (!compare_str && estate_is_whole(sm->state))
2057 continue;
2058 snprintf(buf, sizeof(buf), "%s%s", sm->state->name, compare_str ?: "");
2060 sql_insert_return_states(return_id, return_ranges, PARAM_VALUE,
2061 -1, param_name, buf);
2062 } END_FOR_EACH_SM(sm);
2065 static void db_limited_before(void)
2067 unmatched_stree = clone_stree(__get_cur_stree());
2070 static void db_limited_after(void)
2072 free_stree(&unmatched_stree);
2075 static int rl_fits_in_type(struct range_list *rl, struct symbol *type)
2077 if (type_bits(rl_type(rl)) <= type_bits(type))
2078 return 1;
2079 if (sval_cmp(rl_max(rl), sval_type_max(type)) > 0)
2080 return 0;
2081 if (sval_is_negative(rl_min(rl)) &&
2082 sval_cmp(rl_min(rl), sval_type_min(type)) < 0)
2083 return 0;
2084 return 1;
2087 static void db_param_limit_filter(struct expression *expr, int param, char *key, char *value, enum info_type op)
2089 struct expression *arg;
2090 char *name;
2091 struct symbol *sym;
2092 struct var_sym_list *vsl = NULL;
2093 struct sm_state *sm;
2094 struct symbol *compare_type, *var_type;
2095 struct range_list *rl;
2096 struct range_list *limit;
2097 struct range_list *new;
2099 while (expr->type == EXPR_ASSIGNMENT)
2100 expr = strip_expr(expr->right);
2101 if (expr->type != EXPR_CALL)
2102 return;
2104 arg = get_argument_from_call_expr(expr->args, param);
2105 if (!arg)
2106 return;
2108 name = get_chunk_from_key(arg, key, &sym, &vsl);
2109 if (!name)
2110 return;
2111 if (op != PARAM_LIMIT && !sym)
2112 goto free;
2114 if (strcmp(key, "$") == 0)
2115 compare_type = get_arg_type(expr->fn, param);
2116 else
2117 compare_type = get_member_type_from_key(arg, key);
2119 sm = get_sm_state(SMATCH_EXTRA, name, sym);
2120 if (sm)
2121 rl = estate_rl(sm->state);
2122 else
2123 rl = alloc_whole_rl(compare_type);
2125 if (op == PARAM_LIMIT && !rl_fits_in_type(rl, compare_type))
2126 goto free;
2128 call_results_to_rl(expr, compare_type, value, &limit);
2129 new = rl_intersection(rl, limit);
2131 var_type = get_member_type_from_key(arg, key);
2132 new = cast_rl(var_type, new);
2134 /* We want to preserve the implications here */
2135 if (sm && rl_equiv(estate_rl(sm->state), new))
2136 __set_sm(sm);
2137 else {
2138 char *tmp_name;
2139 struct symbol *tmp_sym;
2141 tmp_name = map_long_to_short_name_sym(name, sym, &tmp_sym);
2142 if (tmp_name && tmp_sym) {
2143 free_string(name);
2144 name = tmp_name;
2145 sym = tmp_sym;
2148 if (op == PARAM_LIMIT)
2149 set_extra_nomod_vsl(name, sym, vsl, alloc_estate_rl(new));
2150 else
2151 set_extra_mod(name, sym, alloc_estate_rl(new));
2154 free:
2155 free_string(name);
2158 static void db_param_limit(struct expression *expr, int param, char *key, char *value)
2160 db_param_limit_filter(expr, param, key, value, PARAM_LIMIT);
2163 static void db_param_filter(struct expression *expr, int param, char *key, char *value)
2165 db_param_limit_filter(expr, param, key, value, PARAM_FILTER);
2168 static void db_param_add_set(struct expression *expr, int param, char *key, char *value, enum info_type op)
2170 struct expression *arg;
2171 char *name, *tmp_name;
2172 struct symbol *sym, *tmp_sym;
2173 struct symbol *type;
2174 struct smatch_state *state;
2175 struct range_list *new = NULL;
2176 struct range_list *added = NULL;
2178 while (expr->type == EXPR_ASSIGNMENT)
2179 expr = strip_expr(expr->right);
2180 if (expr->type != EXPR_CALL)
2181 return;
2183 arg = get_argument_from_call_expr(expr->args, param);
2184 if (!arg)
2185 return;
2186 type = get_member_type_from_key(arg, key);
2187 name = get_variable_from_key(arg, key, &sym);
2188 if (!name || !sym)
2189 goto free;
2191 state = get_state(SMATCH_EXTRA, name, sym);
2192 if (state)
2193 new = estate_rl(state);
2195 call_results_to_rl(expr, type, value, &added);
2197 if (op == PARAM_SET)
2198 new = added;
2199 else
2200 new = rl_union(new, added);
2202 tmp_name = map_long_to_short_name_sym(name, sym, &tmp_sym);
2203 if (tmp_name && tmp_sym) {
2204 free_string(name);
2205 name = tmp_name;
2206 sym = tmp_sym;
2208 set_extra_mod(name, sym, alloc_estate_rl(new));
2209 free:
2210 free_string(name);
2213 static void db_param_add(struct expression *expr, int param, char *key, char *value)
2215 db_param_add_set(expr, param, key, value, PARAM_ADD);
2218 static void db_param_set(struct expression *expr, int param, char *key, char *value)
2220 db_param_add_set(expr, param, key, value, PARAM_SET);
2223 static void db_param_value(struct expression *expr, int param, char *key, char *value)
2225 struct expression *call;
2226 char *name;
2227 struct symbol *sym;
2228 struct symbol *type;
2229 struct range_list *rl = NULL;
2231 if (param != -1)
2232 return;
2234 call = expr;
2235 while (call->type == EXPR_ASSIGNMENT)
2236 call = strip_expr(call->right);
2237 if (call->type != EXPR_CALL)
2238 return;
2240 type = get_member_type_from_key(expr->left, key);
2241 name = get_variable_from_key(expr->left, key, &sym);
2242 if (!name || !sym)
2243 goto free;
2245 call_results_to_rl(call, type, value, &rl);
2247 set_extra_mod(name, sym, alloc_estate_rl(rl));
2248 free:
2249 free_string(name);
2252 static void match_call_info(struct expression *expr)
2254 struct smatch_state *state;
2255 struct range_list *rl = NULL;
2256 struct expression *arg;
2257 struct symbol *type;
2258 int i = 0;
2260 FOR_EACH_PTR(expr->args, arg) {
2261 type = get_arg_type(expr->fn, i);
2263 if (get_implied_rl(arg, &rl))
2264 rl = cast_rl(type, rl);
2265 else
2266 rl = cast_rl(type, alloc_whole_rl(get_type(arg)));
2268 if (!is_whole_rl(rl)) {
2269 rl = intersect_with_real_abs_expr(arg, rl);
2270 sql_insert_caller_info(expr, PARAM_VALUE, i, "$", show_rl(rl));
2272 state = get_state_expr(SMATCH_EXTRA, arg);
2273 if (estate_has_fuzzy_max(state)) {
2274 sql_insert_caller_info(expr, FUZZY_MAX, i, "$",
2275 sval_to_str(estate_get_fuzzy_max(state)));
2277 i++;
2278 } END_FOR_EACH_PTR(arg);
2281 static void set_param_value(const char *name, struct symbol *sym, char *key, char *value)
2283 struct range_list *rl = NULL;
2284 struct smatch_state *state;
2285 struct symbol *type;
2286 char fullname[256];
2287 sval_t dummy;
2289 if (strcmp(key, "*$") == 0)
2290 snprintf(fullname, sizeof(fullname), "*%s", name);
2291 else if (strncmp(key, "$", 1) == 0)
2292 snprintf(fullname, 256, "%s%s", name, key + 1);
2293 else
2294 return;
2296 type = get_member_type_from_key(symbol_expression(sym), key);
2297 str_to_rl(type, value, &rl);
2298 state = alloc_estate_rl(rl);
2299 if (estate_get_single_value(state, &dummy))
2300 estate_set_hard_max(state);
2301 set_state(SMATCH_EXTRA, fullname, sym, state);
2304 static void set_param_hard_max(const char *name, struct symbol *sym, char *key, char *value)
2306 struct range_list *rl = NULL;
2307 struct smatch_state *state;
2308 struct symbol *type;
2309 char fullname[256];
2310 sval_t max;
2312 if (strcmp(key, "*$") == 0)
2313 snprintf(fullname, sizeof(fullname), "*%s", name);
2314 else if (strncmp(key, "$", 1) == 0)
2315 snprintf(fullname, 256, "%s%s", name, key + 1);
2316 else
2317 return;
2319 state = get_state(SMATCH_EXTRA, fullname, sym);
2320 if (!state)
2321 return;
2322 type = get_member_type_from_key(symbol_expression(sym), key);
2323 str_to_rl(type, value, &rl);
2324 if (!rl_to_sval(rl, &max))
2325 return;
2326 estate_set_fuzzy_max(state, max);
2329 struct smatch_state *get_extra_state(struct expression *expr)
2331 char *name;
2332 struct symbol *sym;
2333 struct smatch_state *ret = NULL;
2334 struct range_list *rl;
2336 if (is_pointer(expr) && get_address_rl(expr, &rl))
2337 return alloc_estate_rl(rl);
2339 name = expr_to_known_chunk_sym(expr, &sym);
2340 if (!name)
2341 goto free;
2343 ret = get_state(SMATCH_EXTRA, name, sym);
2344 free:
2345 free_string(name);
2346 return ret;
2349 void register_smatch_extra(int id)
2351 my_id = id;
2353 add_merge_hook(my_id, &merge_estates);
2354 add_unmatched_state_hook(my_id, &unmatched_state);
2355 select_caller_info_hook(set_param_value, PARAM_VALUE);
2356 select_caller_info_hook(set_param_hard_max, FUZZY_MAX);
2357 select_return_states_before(&db_limited_before);
2358 select_return_states_hook(PARAM_LIMIT, &db_param_limit);
2359 select_return_states_hook(PARAM_FILTER, &db_param_filter);
2360 select_return_states_hook(PARAM_ADD, &db_param_add);
2361 select_return_states_hook(PARAM_SET, &db_param_set);
2362 select_return_states_hook(PARAM_VALUE, &db_param_value);
2363 select_return_states_after(&db_limited_after);
2366 static void match_link_modify(struct sm_state *sm, struct expression *mod_expr)
2368 struct var_sym_list *links;
2369 struct var_sym *tmp;
2370 struct smatch_state *state;
2372 links = sm->state->data;
2374 FOR_EACH_PTR(links, tmp) {
2375 if (sm->sym == tmp->sym &&
2376 strcmp(sm->name, tmp->var) == 0)
2377 continue;
2378 state = get_state(SMATCH_EXTRA, tmp->var, tmp->sym);
2379 if (!state)
2380 continue;
2381 set_state(SMATCH_EXTRA, tmp->var, tmp->sym, alloc_estate_whole(estate_type(state)));
2382 } END_FOR_EACH_PTR(tmp);
2383 set_state(link_id, sm->name, sm->sym, &undefined);
2386 void register_smatch_extra_links(int id)
2388 link_id = id;
2391 void register_smatch_extra_late(int id)
2393 add_merge_hook(link_id, &merge_link_states);
2394 add_modification_hook(link_id, &match_link_modify);
2395 add_hook(&match_dereferences, DEREF_HOOK);
2396 add_hook(&match_pointer_as_array, OP_HOOK);
2397 select_call_implies_hook(DEREFERENCE, &set_param_dereferenced);
2398 add_hook(&match_function_call, FUNCTION_CALL_HOOK);
2399 add_hook(&match_assign, ASSIGNMENT_HOOK);
2400 add_hook(&match_assign, GLOBAL_ASSIGNMENT_HOOK);
2401 add_hook(&unop_expr, OP_HOOK);
2402 add_hook(&asm_expr, ASM_HOOK);
2403 add_untracked_param_hook(&match_untracked_array);
2405 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
2406 add_member_info_callback(my_id, struct_member_callback);
2407 add_split_return_callback(&returned_struct_members);
2409 add_hook(&assume_indexes_are_valid, OP_HOOK);