avl: rename struct AVL to struct stree
[smatch.git] / smatch_extra.c
blob185ce3d1f6500502c8dd31d6ba39662fa4d359f6
1 /*
2 * Copyright (C) 2008 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
19 * smatch_extra.c is supposed to track the value of every variable.
23 #include <stdlib.h>
24 #include <errno.h>
25 #ifndef __USE_ISOC99
26 #define __USE_ISOC99
27 #endif
28 #include <limits.h>
29 #include "parse.h"
30 #include "smatch.h"
31 #include "smatch_slist.h"
32 #include "smatch_extra.h"
34 static int my_id;
36 struct string_list *__ignored_macros = NULL;
37 static int in_warn_on_macro()
39 struct statement *stmt;
40 char *tmp;
41 char *macro;
43 stmt = get_current_statement();
44 if (!stmt)
45 return 0;
46 macro = get_macro_name(stmt->pos);
47 if (!macro)
48 return 0;
50 FOR_EACH_PTR(__ignored_macros, tmp) {
51 if (!strcmp(tmp, macro))
52 return 1;
53 } END_FOR_EACH_PTR(tmp);
54 return 0;
57 typedef void (mod_hook)(const char *name, struct symbol *sym, struct smatch_state *state);
58 DECLARE_PTR_LIST(void_fn_list, mod_hook *);
59 static struct void_fn_list *extra_mod_hooks;
60 void add_extra_mod_hook(mod_hook *fn)
62 mod_hook **p = malloc(sizeof(mod_hook *));
63 *p = fn;
64 add_ptr_list(&extra_mod_hooks, p);
67 void call_extra_mod_hooks(const char *name, struct symbol *sym, struct smatch_state *state)
69 mod_hook **fn;
71 FOR_EACH_PTR(extra_mod_hooks, fn) {
72 (*fn)(name, sym, state);
73 } END_FOR_EACH_PTR(fn);
76 struct sm_state *set_extra_mod(const char *name, struct symbol *sym, struct smatch_state *state)
78 if (in_warn_on_macro())
79 return NULL;
80 remove_from_equiv(name, sym);
81 call_extra_mod_hooks(name, sym, state);
82 return set_state(SMATCH_EXTRA, name, sym, state);
85 struct sm_state *set_extra_expr_mod(struct expression *expr, struct smatch_state *state)
87 struct symbol *sym;
88 char *name;
89 struct sm_state *ret = NULL;
91 expr = strip_expr(expr);
92 name = expr_to_var_sym(expr, &sym);
93 if (!name || !sym)
94 goto free;
95 ret = set_extra_mod(name, sym, state);
96 free:
97 free_string(name);
98 return ret;
101 void set_extra_nomod(const char *name, struct symbol *sym, struct smatch_state *state)
103 struct relation *rel;
104 struct smatch_state *orig_state;
106 orig_state = get_state(SMATCH_EXTRA, name, sym);
108 if (!estate_related(orig_state)) {
109 set_state(SMATCH_EXTRA, name, sym, state);
110 return;
113 set_related(state, estate_related(orig_state));
114 FOR_EACH_PTR(estate_related(orig_state), rel) {
115 if (option_debug_related)
116 sm_msg("updating related %s to %s", rel->name, state->name);
117 set_state(SMATCH_EXTRA, rel->name, rel->sym, state);
118 } END_FOR_EACH_PTR(rel);
122 * This is for return_implies_state() hooks which modify a SMATCH_EXTRA state
124 void set_extra_expr_nomod(struct expression *expr, struct smatch_state *state)
126 char *name;
127 struct symbol *sym;
129 name = expr_to_var_sym(expr, &sym);
130 if (!name || !sym)
131 goto free;
132 set_extra_nomod(name, sym, state);
133 free:
134 free_string(name);
138 static void set_extra_true_false(const char *name, struct symbol *sym,
139 struct smatch_state *true_state,
140 struct smatch_state *false_state)
142 struct relation *rel;
143 struct smatch_state *orig_state;
145 if (in_warn_on_macro())
146 return;
148 orig_state = get_state(SMATCH_EXTRA, name, sym);
150 if (!estate_related(orig_state)) {
151 set_true_false_states(SMATCH_EXTRA, name, sym, true_state, false_state);
152 return;
155 if (true_state)
156 set_related(true_state, estate_related(orig_state));
157 if (false_state)
158 set_related(false_state, estate_related(orig_state));
160 FOR_EACH_PTR(estate_related(orig_state), rel) {
161 set_true_false_states(SMATCH_EXTRA, rel->name, rel->sym,
162 true_state, false_state);
163 } END_FOR_EACH_PTR(rel);
166 static void set_extra_expr_true_false(struct expression *expr,
167 struct smatch_state *true_state,
168 struct smatch_state *false_state)
170 char *name;
171 struct symbol *sym;
173 expr = strip_expr(expr);
174 name = expr_to_var_sym(expr, &sym);
175 if (!name || !sym)
176 goto free;
177 set_extra_true_false(name, sym, true_state, false_state);
178 free:
179 free_string(name);
182 static struct sm_state *handle_canonical_while_count_down(struct statement *loop)
184 struct expression *iter_var;
185 struct expression *condition;
186 struct sm_state *sm;
187 struct smatch_state *estate;
188 sval_t start;
190 condition = strip_expr(loop->iterator_pre_condition);
191 if (!condition)
192 return NULL;
193 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
194 return NULL;
195 if (condition->op != SPECIAL_DECREMENT)
196 return NULL;
198 iter_var = condition->unop;
199 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
200 if (!sm)
201 return NULL;
202 if (sval_cmp_val(estate_min(sm->state), 0) < 0)
203 return NULL;
204 start = estate_max(sm->state);
205 if (sval_cmp_val(start, 0) <= 0)
206 return NULL;
207 if (!sval_is_max(start))
208 start.value--;
210 if (condition->type == EXPR_PREOP) {
211 estate = alloc_estate_range(sval_type_val(start.type, 1), start);
212 if (estate_has_hard_max(sm->state))
213 estate_set_hard_max(estate);
214 estate_copy_fuzzy_max(estate, sm->state);
215 set_extra_expr_mod(iter_var, estate);
217 if (condition->type == EXPR_POSTOP) {
218 estate = alloc_estate_range(sval_type_val(start.type, 0), start);
219 if (estate_has_hard_max(sm->state))
220 estate_set_hard_max(estate);
221 estate_copy_fuzzy_max(estate, sm->state);
222 set_extra_expr_mod(iter_var, estate);
224 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
227 static struct sm_state *handle_canonical_for_inc(struct expression *iter_expr,
228 struct expression *condition)
230 struct expression *iter_var;
231 struct sm_state *sm;
232 struct smatch_state *estate;
233 sval_t start, end, max;
235 iter_var = iter_expr->unop;
236 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
237 if (!sm)
238 return NULL;
239 if (!estate_get_single_value(sm->state, &start))
240 return NULL;
241 if (get_implied_max(condition->right, &end))
242 end = sval_cast(get_type(iter_var), end);
243 else
244 end = sval_type_max(get_type(iter_var));
246 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
247 return NULL;
249 switch (condition->op) {
250 case SPECIAL_UNSIGNED_LT:
251 case SPECIAL_NOTEQUAL:
252 case '<':
253 if (!sval_is_min(end))
254 end.value--;
255 break;
256 case SPECIAL_UNSIGNED_LTE:
257 case SPECIAL_LTE:
258 break;
259 default:
260 return NULL;
262 if (sval_cmp(end, start) < 0)
263 return NULL;
264 estate = alloc_estate_range(start, end);
265 if (get_hard_max(condition->right, &max)) {
266 estate_set_hard_max(estate);
267 if (condition->op == '<' ||
268 condition->op == SPECIAL_UNSIGNED_LT ||
269 condition->op == SPECIAL_NOTEQUAL)
270 max.value--;
271 estate_set_fuzzy_max(estate, max);
273 set_extra_expr_mod(iter_var, estate);
274 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
277 static struct sm_state *handle_canonical_for_dec(struct expression *iter_expr,
278 struct expression *condition)
280 struct expression *iter_var;
281 struct sm_state *sm;
282 struct smatch_state *estate;
283 sval_t start, end;
285 iter_var = iter_expr->unop;
286 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
287 if (!sm)
288 return NULL;
289 if (!estate_get_single_value(sm->state, &start))
290 return NULL;
291 if (!get_implied_min(condition->right, &end))
292 end = sval_type_min(get_type(iter_var));
293 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
294 return NULL;
296 switch (condition->op) {
297 case SPECIAL_NOTEQUAL:
298 case '>':
299 if (!sval_is_min(end) && !sval_is_max(end))
300 end.value++;
301 break;
302 case SPECIAL_GTE:
303 break;
304 default:
305 return NULL;
307 if (sval_cmp(end, start) > 0)
308 return NULL;
309 estate = alloc_estate_range(end, start);
310 estate_set_hard_max(estate);
311 estate_set_fuzzy_max(estate, estate_get_fuzzy_max(estate));
312 set_extra_expr_mod(iter_var, estate);
313 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
316 static struct sm_state *handle_canonical_for_loops(struct statement *loop)
318 struct expression *iter_expr;
319 struct expression *condition;
321 if (!loop->iterator_post_statement)
322 return NULL;
323 if (loop->iterator_post_statement->type != STMT_EXPRESSION)
324 return NULL;
325 iter_expr = loop->iterator_post_statement->expression;
326 if (!loop->iterator_pre_condition)
327 return NULL;
328 if (loop->iterator_pre_condition->type != EXPR_COMPARE)
329 return NULL;
330 condition = loop->iterator_pre_condition;
332 if (iter_expr->op == SPECIAL_INCREMENT)
333 return handle_canonical_for_inc(iter_expr, condition);
334 if (iter_expr->op == SPECIAL_DECREMENT)
335 return handle_canonical_for_dec(iter_expr, condition);
336 return NULL;
339 struct sm_state *__extra_handle_canonical_loops(struct statement *loop, struct stree **stree)
341 struct sm_state *ret;
343 __push_fake_cur_slist();
344 if (!loop->iterator_post_statement)
345 ret = handle_canonical_while_count_down(loop);
346 else
347 ret = handle_canonical_for_loops(loop);
348 *stree = __pop_fake_cur_slist();
349 return ret;
352 int __iterator_unchanged(struct sm_state *sm)
354 if (!sm)
355 return 0;
356 if (get_sm_state(my_id, sm->name, sm->sym) == sm)
357 return 1;
358 return 0;
361 static void while_count_down_after(struct sm_state *sm, struct expression *condition)
363 sval_t after_value;
365 /* paranoid checking. prolly not needed */
366 condition = strip_expr(condition);
367 if (!condition)
368 return;
369 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
370 return;
371 if (condition->op != SPECIAL_DECREMENT)
372 return;
373 after_value = estate_min(sm->state);
374 after_value.value--;
375 set_extra_mod(sm->name, sm->sym, alloc_estate_sval(after_value));
378 void __extra_pre_loop_hook_after(struct sm_state *sm,
379 struct statement *iterator,
380 struct expression *condition)
382 struct expression *iter_expr;
383 sval_t limit;
384 struct smatch_state *state;
386 if (!iterator) {
387 while_count_down_after(sm, condition);
388 return;
391 iter_expr = iterator->expression;
393 if (condition->type != EXPR_COMPARE)
394 return;
395 if (iter_expr->op == SPECIAL_INCREMENT) {
396 limit = sval_binop(estate_max(sm->state), '+',
397 sval_type_val(estate_type(sm->state), 1));
398 } else {
399 limit = sval_binop(estate_min(sm->state), '-',
400 sval_type_val(estate_type(sm->state), 1));
402 if (!estate_has_hard_max(sm->state) && !__has_breaks()) {
403 if (iter_expr->op == SPECIAL_INCREMENT)
404 state = alloc_estate_range(estate_min(sm->state), limit);
405 else
406 state = alloc_estate_range(limit, estate_max(sm->state));
407 } else {
408 state = alloc_estate_sval(limit);
410 if (!estate_has_hard_max(sm->state)) {
411 estate_clear_hard_max(state);
413 if (estate_has_fuzzy_max(sm->state)) {
414 sval_t hmax = estate_get_fuzzy_max(sm->state);
415 sval_t max = estate_max(sm->state);
417 if (sval_cmp(hmax, max) != 0)
418 estate_clear_fuzzy_max(state);
419 } else if (!estate_has_fuzzy_max(sm->state)) {
420 estate_clear_fuzzy_max(state);
423 set_extra_mod(sm->name, sm->sym, state);
426 static struct stree *unmatched_stree;
427 static struct smatch_state *unmatched_state(struct sm_state *sm)
429 struct smatch_state *state;
431 if (unmatched_stree) {
432 state = get_state_stree(unmatched_stree, SMATCH_EXTRA, sm->name, sm->sym);
433 if (state)
434 return state;
436 return alloc_estate_whole(estate_type(sm->state));
439 static void clear_the_pointed_at(struct expression *expr, struct stree *stree)
441 char *name;
442 struct symbol *sym;
443 struct sm_state *tmp;
445 name = expr_to_var_sym(expr, &sym);
446 if (!name || !sym)
447 goto free;
449 FOR_EACH_SM(stree, tmp) {
450 if (tmp->name[0] != '*')
451 continue;
452 if (tmp->sym != sym)
453 continue;
454 if (strcmp(tmp->name + 1, name) != 0)
455 continue;
456 set_extra_mod(tmp->name, tmp->sym, alloc_estate_whole(estate_type(tmp->state)));
457 } END_FOR_EACH_SM(tmp);
459 free:
460 free_string(name);
463 static void match_function_call(struct expression *expr)
465 struct expression *arg;
466 struct expression *tmp;
467 struct stree *stree;
469 /* if we have the db this is handled in smatch_function_hooks.c */
470 if (!option_no_db)
471 return;
472 if (inlinable(expr->fn))
473 return;
475 stree = get_all_states_stree(SMATCH_EXTRA);
477 FOR_EACH_PTR(expr->args, arg) {
478 tmp = strip_expr(arg);
479 if (tmp->type == EXPR_PREOP && tmp->op == '&')
480 set_extra_expr_mod(tmp->unop, alloc_estate_whole(get_type(tmp->unop)));
481 else
482 clear_the_pointed_at(tmp, stree);
483 } END_FOR_EACH_PTR(arg);
485 free_stree(&stree);
488 static int types_equiv_or_pointer(struct symbol *one, struct symbol *two)
490 if (!one || !two)
491 return 0;
492 if (one->type == SYM_PTR && two->type == SYM_PTR)
493 return 1;
494 return types_equiv(one, two);
497 static void match_vanilla_assign(struct expression *left, struct expression *right)
499 struct range_list *rl = NULL;
500 struct symbol *right_sym;
501 struct symbol *left_type;
502 struct symbol *right_type;
503 char *right_name = NULL;
504 struct symbol *sym;
505 char *name;
506 sval_t max;
507 struct smatch_state *state;
509 name = expr_to_var_sym(left, &sym);
510 if (!name)
511 return;
513 left_type = get_type(left);
514 right_type = get_type(right);
516 right_name = expr_to_var_sym(right, &right_sym);
517 if (right_name && right_sym &&
518 types_equiv_or_pointer(left_type, right_type)) {
519 set_equiv(left, right);
520 goto free;
523 if (get_implied_rl(right, &rl)) {
524 rl = cast_rl(left_type, rl);
525 state = alloc_estate_rl(rl);
526 if (get_hard_max(right, &max)) {
527 estate_set_hard_max(state);
528 estate_set_fuzzy_max(state, max);
530 } else {
531 rl = alloc_whole_rl(right_type);
532 rl = cast_rl(left_type, rl);
533 state = alloc_estate_rl(rl);
535 set_extra_mod(name, sym, state);
536 free:
537 free_string(right_name);
540 static int op_remove_assign(int op)
542 switch (op) {
543 case SPECIAL_ADD_ASSIGN:
544 return '+';
545 case SPECIAL_SUB_ASSIGN:
546 return '-';
547 case SPECIAL_MUL_ASSIGN:
548 return '*';
549 case SPECIAL_DIV_ASSIGN:
550 return '/';
551 case SPECIAL_MOD_ASSIGN:
552 return '%';
553 case SPECIAL_AND_ASSIGN:
554 return '&';
555 case SPECIAL_OR_ASSIGN:
556 return '|';
557 case SPECIAL_XOR_ASSIGN:
558 return '^';
559 case SPECIAL_SHL_ASSIGN:
560 return SPECIAL_LEFTSHIFT;
561 case SPECIAL_SHR_ASSIGN:
562 return SPECIAL_RIGHTSHIFT;
563 default:
564 return op;
568 static void match_assign(struct expression *expr)
570 struct range_list *rl = NULL;
571 struct expression *left;
572 struct expression *right;
573 struct expression *binop_expr;
574 struct symbol *left_type;
575 struct symbol *right_type;
576 struct symbol *sym;
577 char *name;
578 sval_t left_min, left_max;
579 sval_t right_min, right_max;
580 sval_t res_min, res_max;
582 left = strip_expr(expr->left);
584 right = strip_expr(expr->right);
585 while (right->type == EXPR_ASSIGNMENT && right->op == '=')
586 right = strip_expr(right->left);
588 if (is_condition(expr->right))
589 return; /* handled in smatch_condition.c */
590 if (expr->op == '=' && right->type == EXPR_CALL)
591 return; /* handled in smatch_function_hooks.c */
592 if (expr->op == '=') {
593 match_vanilla_assign(left, right);
594 return;
597 name = expr_to_var_sym(left, &sym);
598 if (!name)
599 return;
601 left_type = get_type(left);
602 right_type = get_type(right);
604 res_min = sval_type_min(right_type);
605 res_max = sval_type_max(right_type);
607 switch (expr->op) {
608 case SPECIAL_ADD_ASSIGN:
609 get_absolute_max(left, &left_max);
610 get_absolute_max(right, &right_max);
611 if (sval_binop_overflows(left_max, '+', right_max))
612 break;
613 if (get_implied_min(left, &left_min) &&
614 !sval_is_negative_min(left_min) &&
615 get_implied_min(right, &right_min) &&
616 !sval_is_negative_min(right_min)) {
617 res_min = sval_binop(left_min, '+', right_min);
618 res_min = sval_cast(right_type, res_min);
620 if (inside_loop()) /* we are assuming loops don't lead to wrapping */
621 break;
622 res_max = sval_binop(left_max, '+', right_max);
623 res_max = sval_cast(right_type, res_max);
624 break;
625 case SPECIAL_SUB_ASSIGN:
626 if (get_implied_max(left, &left_max) &&
627 !sval_is_max(left_max) &&
628 get_implied_min(right, &right_min) &&
629 !sval_is_min(right_min)) {
630 res_max = sval_binop(left_max, '-', right_min);
631 res_max = sval_cast(right_type, res_max);
633 if (inside_loop())
634 break;
635 if (get_implied_min(left, &left_min) &&
636 !sval_is_min(left_min) &&
637 get_implied_max(right, &right_max) &&
638 !sval_is_max(right_max)) {
639 res_min = sval_binop(left_min, '-', right_max);
640 res_min = sval_cast(right_type, res_min);
642 break;
643 case SPECIAL_AND_ASSIGN:
644 case SPECIAL_MOD_ASSIGN:
645 case SPECIAL_SHL_ASSIGN:
646 case SPECIAL_SHR_ASSIGN:
647 case SPECIAL_OR_ASSIGN:
648 case SPECIAL_XOR_ASSIGN:
649 case SPECIAL_MUL_ASSIGN:
650 case SPECIAL_DIV_ASSIGN:
651 binop_expr = binop_expression(expr->left,
652 op_remove_assign(expr->op),
653 expr->right);
654 if (get_implied_rl(binop_expr, &rl)) {
655 rl = cast_rl(left_type, rl);
656 set_extra_mod(name, sym, alloc_estate_rl(rl));
657 goto free;
659 break;
661 rl = cast_rl(left_type, alloc_rl(res_min, res_max));
662 set_extra_mod(name, sym, alloc_estate_rl(rl));
663 free:
664 free_string(name);
667 static struct smatch_state *increment_state(struct smatch_state *state)
669 sval_t min = estate_min(state);
670 sval_t max = estate_max(state);
672 if (!estate_rl(state))
673 return NULL;
675 if (inside_loop())
676 max = sval_type_max(max.type);
678 if (!sval_is_min(min) && !sval_is_max(min))
679 min.value++;
680 if (!sval_is_min(max) && !sval_is_max(max))
681 max.value++;
682 return alloc_estate_range(min, max);
685 static struct smatch_state *decrement_state(struct smatch_state *state)
687 sval_t min = estate_min(state);
688 sval_t max = estate_max(state);
690 if (!estate_rl(state))
691 return NULL;
693 if (inside_loop())
694 min = sval_type_min(min.type);
696 if (!sval_is_min(min) && !sval_is_max(min))
697 min.value--;
698 if (!sval_is_min(max) && !sval_is_max(max))
699 max.value--;
700 return alloc_estate_range(min, max);
703 static void unop_expr(struct expression *expr)
705 struct smatch_state *state;
707 if (expr->smatch_flags & Handled)
708 return;
710 switch (expr->op) {
711 case SPECIAL_INCREMENT:
712 state = get_state_expr(SMATCH_EXTRA, expr->unop);
713 state = increment_state(state);
714 if (!state)
715 state = alloc_estate_whole(get_type(expr));
716 set_extra_expr_mod(expr->unop, state);
717 break;
718 case SPECIAL_DECREMENT:
719 state = get_state_expr(SMATCH_EXTRA, expr->unop);
720 state = decrement_state(state);
721 if (!state)
722 state = alloc_estate_whole(get_type(expr));
723 set_extra_expr_mod(expr->unop, state);
724 break;
725 default:
726 return;
730 static void asm_expr(struct statement *stmt)
733 struct expression *expr;
734 struct symbol *type;
735 int state = 0;
737 FOR_EACH_PTR(stmt->asm_outputs, expr) {
738 switch (state) {
739 case 0: /* identifier */
740 case 1: /* constraint */
741 state++;
742 continue;
743 case 2: /* expression */
744 state = 0;
745 type = get_type(strip_expr(expr));
746 set_extra_expr_mod(expr, alloc_estate_whole(type));
747 continue;
749 } END_FOR_EACH_PTR(expr);
752 static void delete_state_tracker(struct tracker *t)
754 remove_from_equiv(t->name, t->sym);
755 delete_state(t->owner, t->name, t->sym);
758 static void scoped_state_extra(const char *name, struct symbol *sym)
760 struct tracker *t;
762 t = alloc_tracker(SMATCH_EXTRA, name, sym);
763 add_scope_hook((scope_hook *)&delete_state_tracker, t);
766 static void match_declarations(struct symbol *sym)
768 const char *name;
770 if (sym->ident) {
771 name = sym->ident->name;
772 if (!sym->initializer) {
773 set_state(SMATCH_EXTRA, name, sym, alloc_estate_whole(get_real_base_type(sym)));
774 scoped_state_extra(name, sym);
779 static void check_dereference(struct expression *expr)
781 if (outside_of_function())
782 return;
783 set_extra_expr_nomod(expr, alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval));
786 static void match_dereferences(struct expression *expr)
788 if (expr->type != EXPR_PREOP)
789 return;
790 check_dereference(expr->unop);
793 static void match_pointer_as_array(struct expression *expr)
795 if (!is_array(expr))
796 return;
797 check_dereference(expr->unop->left);
800 static void set_param_dereferenced(struct expression *arg, char *unused)
802 check_dereference(arg);
805 static void match_function_def(struct symbol *sym)
807 struct symbol *arg;
809 FOR_EACH_PTR(sym->ctype.base_type->arguments, arg) {
810 if (!arg->ident)
811 continue;
812 set_state(my_id, arg->ident->name, arg, alloc_estate_whole(get_real_base_type(arg)));
813 } END_FOR_EACH_PTR(arg);
816 static sval_t add_one(sval_t sval)
818 sval.value++;
819 return sval;
822 static sval_t sub_one(sval_t sval)
824 sval.value--;
825 return sval;
828 static void handle_comparison(struct symbol *type, struct expression *left, int op, struct expression *right)
830 struct range_list *left_orig;
831 struct range_list *left_true;
832 struct range_list *left_false;
833 struct range_list *right_orig;
834 struct range_list *right_true;
835 struct range_list *right_false;
836 struct smatch_state *left_true_state;
837 struct smatch_state *left_false_state;
838 struct smatch_state *right_true_state;
839 struct smatch_state *right_false_state;
840 sval_t min, max, dummy, hard_max;
841 int left_postop = 0;
842 int right_postop = 0;
844 if (left->op == SPECIAL_INCREMENT || left->op == SPECIAL_DECREMENT) {
845 if (left->type == EXPR_POSTOP) {
846 left->smatch_flags |= Handled;
847 left_postop = left->op;
849 left = strip_expr(left->unop);
851 while (left->type == EXPR_ASSIGNMENT)
852 left = strip_expr(left->left);
854 if (right->op == SPECIAL_INCREMENT || right->op == SPECIAL_DECREMENT) {
855 if (right->type == EXPR_POSTOP) {
856 right->smatch_flags |= Handled;
857 right_postop = right->op;
859 right = strip_expr(right->unop);
862 if (get_implied_rl(left, &left_orig)) {
863 left_orig = cast_rl(type, left_orig);
864 } else {
865 min = sval_type_min(get_type(left));
866 max = sval_type_max(get_type(left));
867 left_orig = cast_rl(type, alloc_rl(min, max));
870 if (get_implied_rl(right, &right_orig)) {
871 right_orig = cast_rl(type, right_orig);
872 } else {
873 min = sval_type_min(get_type(right));
874 max = sval_type_max(get_type(right));
875 right_orig = cast_rl(type, alloc_rl(min, max));
877 min = sval_type_min(type);
878 max = sval_type_max(type);
880 left_true = clone_rl(left_orig);
881 left_false = clone_rl(left_orig);
882 right_true = clone_rl(right_orig);
883 right_false = clone_rl(right_orig);
885 switch (op) {
886 case '<':
887 case SPECIAL_UNSIGNED_LT:
888 left_true = remove_range(left_orig, rl_max(right_orig), max);
889 if (!sval_is_min(rl_min(right_orig))) {
890 left_false = remove_range(left_orig, min, sub_one(rl_min(right_orig)));
893 right_true = remove_range(right_orig, min, rl_min(left_orig));
894 if (!sval_is_max(rl_max(left_orig)))
895 right_false = remove_range(right_orig, add_one(rl_max(left_orig)), max);
896 break;
897 case SPECIAL_UNSIGNED_LTE:
898 case SPECIAL_LTE:
899 if (!sval_is_max(rl_max(right_orig)))
900 left_true = remove_range(left_orig, add_one(rl_max(right_orig)), max);
901 left_false = remove_range(left_orig, min, rl_min(right_orig));
903 if (!sval_is_min(rl_min(left_orig)))
904 right_true = remove_range(right_orig, min, sub_one(rl_min(left_orig)));
905 right_false = remove_range(right_orig, rl_max(left_orig), max);
907 if (sval_cmp(rl_min(left_orig), rl_min(right_orig)) == 0)
908 left_false = remove_range(left_false, rl_min(left_orig), rl_min(left_orig));
909 if (sval_cmp(rl_max(left_orig), rl_max(right_orig)) == 0)
910 right_false = remove_range(right_false, rl_max(left_orig), rl_max(left_orig));
911 break;
912 case SPECIAL_EQUAL:
913 if (!sval_is_max(rl_max(right_orig))) {
914 left_true = remove_range(left_true, add_one(rl_max(right_orig)), max);
916 if (!sval_is_min(rl_min(right_orig))) {
917 left_true = remove_range(left_true, min, sub_one(rl_min(right_orig)));
919 if (sval_cmp(rl_min(right_orig), rl_max(right_orig)) == 0)
920 left_false = remove_range(left_orig, rl_min(right_orig), rl_min(right_orig));
922 if (!sval_is_max(rl_max(left_orig)))
923 right_true = remove_range(right_true, add_one(rl_max(left_orig)), max);
924 if (!sval_is_min(rl_min(left_orig)))
925 right_true = remove_range(right_true, min, sub_one(rl_min(left_orig)));
926 if (sval_cmp(rl_min(left_orig), rl_max(left_orig)) == 0)
927 right_false = remove_range(right_orig, rl_min(left_orig), rl_min(left_orig));
928 break;
929 case SPECIAL_UNSIGNED_GTE:
930 case SPECIAL_GTE:
931 if (!sval_is_min(rl_min(right_orig)))
932 left_true = remove_range(left_orig, min, sub_one(rl_min(right_orig)));
933 left_false = remove_range(left_orig, rl_max(right_orig), max);
935 if (!sval_is_max(rl_max(left_orig)))
936 right_true = remove_range(right_orig, add_one(rl_max(left_orig)), max);
937 right_false = remove_range(right_orig, min, rl_min(left_orig));
939 if (sval_cmp(rl_min(left_orig), rl_min(right_orig)) == 0)
940 right_false = remove_range(right_false, rl_min(left_orig), rl_min(left_orig));
941 if (sval_cmp(rl_max(left_orig), rl_max(right_orig)) == 0)
942 left_false = remove_range(left_false, rl_max(left_orig), rl_max(left_orig));
943 break;
944 case '>':
945 case SPECIAL_UNSIGNED_GT:
946 left_true = remove_range(left_orig, min, rl_min(right_orig));
947 if (!sval_is_max(rl_max(right_orig)))
948 left_false = remove_range(left_orig, add_one(rl_max(right_orig)), max);
950 right_true = remove_range(right_orig, rl_max(left_orig), max);
951 if (!sval_is_min(rl_min(left_orig)))
952 right_false = remove_range(right_orig, min, sub_one(rl_min(left_orig)));
953 break;
954 case SPECIAL_NOTEQUAL:
955 if (!sval_is_max(rl_max(right_orig)))
956 left_false = remove_range(left_false, add_one(rl_max(right_orig)), max);
957 if (!sval_is_min(rl_min(right_orig)))
958 left_false = remove_range(left_false, min, sub_one(rl_min(right_orig)));
959 if (sval_cmp(rl_min(right_orig), rl_max(right_orig)) == 0)
960 left_true = remove_range(left_orig, rl_min(right_orig), rl_min(right_orig));
962 if (!sval_is_max(rl_max(left_orig)))
963 right_false = remove_range(right_false, add_one(rl_max(left_orig)), max);
964 if (!sval_is_min(rl_min(left_orig)))
965 right_false = remove_range(right_false, min, sub_one(rl_min(left_orig)));
966 if (sval_cmp(rl_min(left_orig), rl_max(left_orig)) == 0)
967 right_true = remove_range(right_orig, rl_min(left_orig), rl_min(left_orig));
968 break;
969 default:
970 return;
973 left_true = rl_truncate_cast(get_type(left), left_true);
974 left_false = rl_truncate_cast(get_type(left), left_false);
975 right_true = rl_truncate_cast(get_type(right), right_true);
976 right_false = rl_truncate_cast(get_type(right), right_false);
978 left_true_state = alloc_estate_rl(left_true);
979 left_false_state = alloc_estate_rl(left_false);
980 right_true_state = alloc_estate_rl(right_true);
981 right_false_state = alloc_estate_rl(right_false);
983 switch (op) {
984 case '<':
985 case SPECIAL_UNSIGNED_LT:
986 case SPECIAL_UNSIGNED_LTE:
987 case SPECIAL_LTE:
988 if (get_hard_max(right, &dummy))
989 estate_set_hard_max(left_true_state);
990 if (get_hard_max(left, &dummy))
991 estate_set_hard_max(right_false_state);
992 break;
993 case '>':
994 case SPECIAL_UNSIGNED_GT:
995 case SPECIAL_UNSIGNED_GTE:
996 case SPECIAL_GTE:
997 if (get_hard_max(left, &dummy))
998 estate_set_hard_max(right_true_state);
999 if (get_hard_max(right, &dummy))
1000 estate_set_hard_max(left_false_state);
1001 break;
1004 switch (op) {
1005 case '<':
1006 case SPECIAL_UNSIGNED_LT:
1007 case SPECIAL_UNSIGNED_LTE:
1008 case SPECIAL_LTE:
1009 if (get_hard_max(right, &hard_max)) {
1010 if (op == '<' || op == SPECIAL_UNSIGNED_LT)
1011 hard_max.value--;
1012 estate_set_fuzzy_max(left_true_state, hard_max);
1014 if (get_implied_value(right, &hard_max)) {
1015 if (op == SPECIAL_UNSIGNED_LTE ||
1016 op == SPECIAL_LTE)
1017 hard_max.value++;
1018 estate_set_fuzzy_max(left_false_state, hard_max);
1020 if (get_hard_max(left, &hard_max)) {
1021 if (op == SPECIAL_UNSIGNED_LTE ||
1022 op == SPECIAL_LTE)
1023 hard_max.value--;
1024 estate_set_fuzzy_max(right_false_state, hard_max);
1026 if (get_implied_value(left, &hard_max)) {
1027 if (op == '<' || op == SPECIAL_UNSIGNED_LT)
1028 hard_max.value++;
1029 estate_set_fuzzy_max(right_true_state, hard_max);
1031 break;
1032 case '>':
1033 case SPECIAL_UNSIGNED_GT:
1034 case SPECIAL_UNSIGNED_GTE:
1035 case SPECIAL_GTE:
1036 if (get_hard_max(left, &hard_max)) {
1037 if (op == '>' || op == SPECIAL_UNSIGNED_GT)
1038 hard_max.value--;
1039 estate_set_fuzzy_max(right_true_state, hard_max);
1041 if (get_implied_value(left, &hard_max)) {
1042 if (op == SPECIAL_UNSIGNED_GTE ||
1043 op == SPECIAL_GTE)
1044 hard_max.value++;
1045 estate_set_fuzzy_max(right_false_state, hard_max);
1047 if (get_hard_max(right, &hard_max)) {
1048 if (op == SPECIAL_UNSIGNED_LTE ||
1049 op == SPECIAL_LTE)
1050 hard_max.value--;
1051 estate_set_fuzzy_max(left_false_state, hard_max);
1053 if (get_implied_value(right, &hard_max)) {
1054 if (op == '>' ||
1055 op == SPECIAL_UNSIGNED_GT)
1056 hard_max.value++;
1057 estate_set_fuzzy_max(left_true_state, hard_max);
1059 break;
1060 case SPECIAL_EQUAL:
1061 if (get_hard_max(left, &hard_max))
1062 estate_set_fuzzy_max(right_true_state, hard_max);
1063 if (get_hard_max(right, &hard_max))
1064 estate_set_fuzzy_max(left_true_state, hard_max);
1065 break;
1068 if (get_hard_max(left, &hard_max)) {
1069 estate_set_hard_max(left_true_state);
1070 estate_set_hard_max(left_false_state);
1072 if (get_hard_max(right, &hard_max)) {
1073 estate_set_hard_max(right_true_state);
1074 estate_set_hard_max(right_false_state);
1077 if (left_postop == SPECIAL_INCREMENT) {
1078 left_true_state = increment_state(left_true_state);
1079 left_false_state = increment_state(left_false_state);
1081 if (left_postop == SPECIAL_DECREMENT) {
1082 left_true_state = decrement_state(left_true_state);
1083 left_false_state = decrement_state(left_false_state);
1085 if (right_postop == SPECIAL_INCREMENT) {
1086 right_true_state = increment_state(right_true_state);
1087 right_false_state = increment_state(right_false_state);
1089 if (right_postop == SPECIAL_DECREMENT) {
1090 right_true_state = decrement_state(right_true_state);
1091 right_false_state = decrement_state(right_false_state);
1094 set_extra_expr_true_false(left, left_true_state, left_false_state);
1095 set_extra_expr_true_false(right, right_true_state, right_false_state);
1098 static int opposite_op(int op)
1100 switch (op) {
1101 case '+':
1102 return '-';
1103 case '-':
1104 return '+';
1106 return 0;
1109 static int is_simple_math(struct expression *expr)
1111 if (!expr)
1112 return 0;
1113 if (expr->type != EXPR_BINOP)
1114 return 0;
1115 if (!opposite_op(expr->op))
1116 return 0;
1117 return 1;
1120 static void move_known_values(struct expression **left_p, struct expression **right_p)
1122 struct expression *left = *left_p;
1123 struct expression *right = *right_p;
1124 sval_t sval;
1126 if (get_implied_value(left, &sval)) {
1127 if (!is_simple_math(right))
1128 return;
1129 if (right->op == '+' && get_value(right->left, &sval)) {
1130 *left_p = binop_expression(left, opposite_op(right->op), right->left);
1131 *right_p = right->right;
1132 return;
1134 if (get_value(right->right, &sval)) {
1135 *left_p = binop_expression(left, opposite_op(right->op), right->right);
1136 *right_p = right->left;
1137 return;
1139 return;
1141 if (get_implied_value(right, &sval)) {
1142 if (!is_simple_math(left))
1143 return;
1144 if (left->op == '+' && get_value(left->left, &sval)) {
1145 *right_p = binop_expression(right, opposite_op(left->op), left->left);
1146 *left_p = left->right;
1147 return;
1150 if (get_value(left->right, &sval)) {
1151 *right_p = binop_expression(right, opposite_op(left->op), left->right);
1152 *left_p = left->left;
1153 return;
1155 return;
1159 static int match_func_comparison(struct expression *expr)
1161 struct expression *left = strip_expr(expr->left);
1162 struct expression *right = strip_expr(expr->right);
1163 sval_t sval;
1166 * fixme: think about this harder. We should always be trying to limit
1167 * the non-call side as well. If we can't determine the limitter does
1168 * that mean we aren't querying the database and are missing important
1169 * information?
1172 if (left->type == EXPR_CALL) {
1173 if (get_implied_value(left, &sval)) {
1174 handle_comparison(get_type(expr), left, expr->op, right);
1175 return 1;
1177 if (!get_implied_value(right, &sval))
1178 return 1;
1179 function_comparison(expr->op, left, sval, 1);
1180 return 1;
1183 if (right->type == EXPR_CALL) {
1184 if (get_implied_value(right, &sval)) {
1185 handle_comparison(get_type(expr), left, expr->op, right);
1186 return 1;
1188 if (!get_implied_value(left, &sval))
1189 return 1;
1190 function_comparison(expr->op, right, sval, 0);
1191 return 1;
1194 return 0;
1197 static void match_comparison(struct expression *expr)
1199 struct expression *left_orig = strip_expr(expr->left);
1200 struct expression *right_orig = strip_expr(expr->right);
1201 struct expression *left, *right;
1202 struct expression *prev;
1203 struct symbol *type;
1205 if (match_func_comparison(expr))
1206 return;
1208 type = get_type(expr);
1209 if (!type)
1210 type = &llong_ctype;
1212 left = left_orig;
1213 right = right_orig;
1214 move_known_values(&left, &right);
1215 handle_comparison(type, left, expr->op, right);
1217 prev = get_assigned_expr(left_orig);
1218 if (is_simple_math(prev) && has_variable(prev, left_orig) == 0) {
1219 left = prev;
1220 right = right_orig;
1221 move_known_values(&left, &right);
1222 handle_comparison(type, left, expr->op, right);
1225 prev = get_assigned_expr(right_orig);
1226 if (is_simple_math(prev) && has_variable(prev, right_orig) == 0) {
1227 left = left_orig;
1228 right = prev;
1229 move_known_values(&left, &right);
1230 handle_comparison(type, left, expr->op, right);
1234 static void handle_AND_condition(struct expression *expr)
1236 struct range_list *rl = NULL;
1237 sval_t known;
1239 if (get_implied_value(expr->left, &known) && known.value > 0) {
1240 known.value--;
1241 get_absolute_rl(expr->right, &rl);
1242 rl = remove_range(rl, sval_type_val(known.type, 0), known);
1243 set_extra_expr_true_false(expr->right, alloc_estate_rl(rl), NULL);
1244 return;
1247 if (get_implied_value(expr->right, &known) && known.value > 0) {
1248 known.value--;
1249 get_absolute_rl(expr->left, &rl);
1250 rl = remove_range(rl, sval_type_val(known.type, 0), known);
1251 set_extra_expr_true_false(expr->left, alloc_estate_rl(rl), NULL);
1252 return;
1256 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
1257 void __extra_match_condition(struct expression *expr)
1259 struct symbol *sym;
1260 char *name;
1261 struct smatch_state *pre_state;
1262 struct smatch_state *true_state;
1263 struct smatch_state *false_state;
1265 expr = strip_expr(expr);
1266 switch (expr->type) {
1267 case EXPR_CALL:
1268 function_comparison(SPECIAL_NOTEQUAL, expr, ll_to_sval(0), 1);
1269 return;
1270 case EXPR_PREOP:
1271 case EXPR_SYMBOL:
1272 case EXPR_DEREF: {
1273 sval_t zero;
1275 zero = sval_blank(expr);
1276 zero.value = 0;
1278 name = expr_to_var_sym(expr, &sym);
1279 if (!name)
1280 return;
1281 pre_state = get_state(my_id, name, sym);
1282 true_state = estate_filter_sval(pre_state, zero);
1283 if (possibly_true(expr, SPECIAL_EQUAL, zero_expr()))
1284 false_state = alloc_estate_sval(zero);
1285 else
1286 false_state = alloc_estate_empty();
1287 set_extra_true_false(name, sym, true_state, false_state);
1288 free_string(name);
1289 return;
1291 case EXPR_COMPARE:
1292 match_comparison(expr);
1293 return;
1294 case EXPR_ASSIGNMENT:
1295 __extra_match_condition(expr->left);
1296 return;
1297 case EXPR_BINOP:
1298 if (expr->op == '&')
1299 handle_AND_condition(expr);
1300 return;
1304 static void assume_indexes_are_valid(struct expression *expr)
1306 struct expression *array_expr;
1307 int array_size;
1308 struct expression *offset;
1309 struct symbol *offset_type;
1310 struct range_list *rl_before;
1311 struct range_list *rl_after;
1312 struct range_list *filter = NULL;
1313 sval_t size;
1315 expr = strip_expr(expr);
1316 if (!is_array(expr))
1317 return;
1319 offset = get_array_offset(expr);
1320 offset_type = get_type(offset);
1321 if (offset_type && type_signed(offset_type)) {
1322 filter = alloc_rl(sval_type_min(offset_type),
1323 sval_type_val(offset_type, -1));
1326 array_expr = strip_parens(expr->unop->left);
1327 array_size = get_real_array_size(array_expr);
1328 if (array_size > 1) {
1329 size = sval_type_val(offset_type, array_size);
1330 add_range(&filter, size, sval_type_max(offset_type));
1333 if (!filter)
1334 return;
1335 get_absolute_rl(offset, &rl_before);
1336 rl_after = rl_filter(rl_before, filter);
1337 if (rl_equiv(rl_before, rl_after))
1338 return;
1339 set_extra_expr_nomod(offset, alloc_estate_rl(rl_after));
1342 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
1343 int implied_not_equal(struct expression *expr, long long val)
1345 return !possibly_false(expr, SPECIAL_NOTEQUAL, value_expr(val));
1348 static void struct_member_callback(struct expression *call, int param, char *printed_name, struct smatch_state *state)
1350 if (estate_is_whole(state))
1351 return;
1352 sql_insert_caller_info(call, PARAM_VALUE, param, printed_name, state->name);
1353 if (estate_has_fuzzy_max(state))
1354 sql_insert_caller_info(call, FUZZY_MAX, param, printed_name,
1355 sval_to_str(estate_get_fuzzy_max(state)));
1358 static void db_limited_before(void)
1360 unmatched_stree = clone_stree(__get_cur_stree());
1363 static void db_limited_after(void)
1365 free_stree(&unmatched_stree);
1368 static void db_param_limit_filter(struct expression *expr, int param, char *key, char *value, int mod)
1370 struct expression *arg;
1371 char *name;
1372 struct symbol *sym;
1373 struct sm_state *sm;
1374 struct symbol *type;
1375 struct range_list *rl;
1376 struct range_list *limit;
1377 struct range_list *new;
1379 while (expr->type == EXPR_ASSIGNMENT)
1380 expr = strip_expr(expr->right);
1381 if (expr->type != EXPR_CALL)
1382 return;
1384 arg = get_argument_from_call_expr(expr->args, param);
1385 if (!arg)
1386 return;
1388 name = get_variable_from_key(arg, key, &sym);
1389 if (!name || !sym)
1390 goto free;
1392 sm = get_sm_state(SMATCH_EXTRA, name, sym);
1393 type = get_member_type_from_key(arg, key);
1394 if (sm)
1395 rl = estate_rl(sm->state);
1396 else
1397 rl = alloc_whole_rl(type);
1399 call_results_to_rl(expr, type, value, &limit);
1400 new = rl_intersection(rl, limit);
1402 /* We want to preserve the implications here */
1403 if (sm && rl_equiv(estate_rl(sm->state), new)) {
1404 __set_sm(sm);
1405 } else {
1406 if (mod)
1407 set_extra_mod(name, sym, alloc_estate_rl(new));
1408 else
1409 set_extra_nomod(name, sym, alloc_estate_rl(new));
1412 free:
1413 free_string(name);
1416 static void db_param_limit(struct expression *expr, int param, char *key, char *value)
1418 db_param_limit_filter(expr, param, key, value, 0);
1421 static void db_param_filter(struct expression *expr, int param, char *key, char *value)
1423 db_param_limit_filter(expr, param, key, value, 1);
1426 static void db_param_add(struct expression *expr, int param, char *key, char *value)
1428 struct expression *arg;
1429 char *name;
1430 struct symbol *sym;
1431 struct symbol *type;
1432 struct smatch_state *state;
1433 struct range_list *added = NULL;
1434 struct range_list *new;
1436 while (expr->type == EXPR_ASSIGNMENT)
1437 expr = strip_expr(expr->right);
1438 if (expr->type != EXPR_CALL)
1439 return;
1441 arg = get_argument_from_call_expr(expr->args, param);
1442 if (!arg)
1443 return;
1444 name = get_variable_from_key(arg, key, &sym);
1445 if (!name || !sym)
1446 goto free;
1448 type = get_member_type_from_key(arg, key);
1449 state = get_state(SMATCH_EXTRA, name, sym);
1450 if (state) {
1451 call_results_to_rl(expr, type, value, &added);
1452 new = rl_union(estate_rl(state), added);
1453 } else {
1454 new = alloc_whole_rl(type);
1457 set_extra_mod(name, sym, alloc_estate_rl(new));
1458 free:
1459 free_string(name);
1462 static void db_returned_member_info(struct expression *expr, int param, char *key, char *value)
1464 struct symbol *type;
1465 struct symbol *sym;
1466 char *name;
1467 struct range_list *rl;
1469 if (expr->type != EXPR_ASSIGNMENT)
1470 return;
1472 name = return_state_to_var_sym(expr, param, key, &sym);
1473 if (!name || !sym)
1474 goto free;
1475 type = get_member_type_from_key(expr->left, key);
1476 if (!type)
1477 return;
1478 call_results_to_rl(expr->right, type, value, &rl);
1479 set_extra_mod(name, sym, alloc_estate_rl(rl));
1481 free:
1482 free_string(name);
1485 static void returned_member_callback(int return_id, char *return_ranges, char *printed_name, struct smatch_state *state)
1487 if (estate_is_whole(state))
1488 return;
1490 /* these are handled in smatch_param_filter/set/limit.c */
1491 if (printed_name[0] != '*' &&
1492 !strchr(printed_name, '.') && !strchr(printed_name, '-'))
1493 return;
1495 sql_insert_return_states(return_id, return_ranges, RETURN_VALUE, -1,
1496 printed_name, state->name);
1499 static void match_call_info(struct expression *expr)
1501 struct smatch_state *state;
1502 struct range_list *rl = NULL;
1503 struct expression *arg;
1504 struct symbol *type;
1505 int i = 0;
1507 FOR_EACH_PTR(expr->args, arg) {
1508 type = get_arg_type(expr->fn, i);
1510 if (get_implied_rl(arg, &rl))
1511 rl = cast_rl(type, rl);
1512 else
1513 rl = cast_rl(type, alloc_whole_rl(get_type(arg)));
1515 if (!is_whole_rl(rl))
1516 sql_insert_caller_info(expr, PARAM_VALUE, i, "$$", show_rl(rl));
1517 state = get_state_expr(SMATCH_EXTRA, arg);
1518 if (estate_has_fuzzy_max(state)) {
1519 sql_insert_caller_info(expr, FUZZY_MAX, i, "$$",
1520 sval_to_str(estate_get_fuzzy_max(state)));
1522 i++;
1523 } END_FOR_EACH_PTR(arg);
1526 static void set_param_value(const char *name, struct symbol *sym, char *key, char *value)
1528 struct range_list *rl = NULL;
1529 struct smatch_state *state;
1530 struct symbol *type;
1531 char fullname[256];
1533 if (strcmp(key, "*$$") == 0)
1534 snprintf(fullname, sizeof(fullname), "*%s", name);
1535 else if (strncmp(key, "$$", 2) == 0)
1536 snprintf(fullname, 256, "%s%s", name, key + 2);
1537 else
1538 return;
1540 type = get_member_type_from_key(symbol_expression(sym), key);
1541 str_to_rl(type, value, &rl);
1542 state = alloc_estate_rl(rl);
1543 set_state(SMATCH_EXTRA, fullname, sym, state);
1546 static void set_param_hard_max(const char *name, struct symbol *sym, char *key, char *value)
1548 struct range_list *rl = NULL;
1549 struct smatch_state *state;
1550 struct symbol *type;
1551 char fullname[256];
1552 sval_t max;
1554 if (strcmp(key, "*$$") == 0)
1555 snprintf(fullname, sizeof(fullname), "*%s", name);
1556 else if (strncmp(key, "$$", 2) == 0)
1557 snprintf(fullname, 256, "%s%s", name, key + 2);
1558 else
1559 return;
1561 state = get_state(SMATCH_EXTRA, fullname, sym);
1562 if (!state)
1563 return;
1564 type = get_member_type_from_key(symbol_expression(sym), key);
1565 str_to_rl(type, value, &rl);
1566 if (!rl_to_sval(rl, &max))
1567 return;
1568 estate_set_fuzzy_max(state, max);
1571 void register_smatch_extra(int id)
1573 my_id = id;
1575 add_merge_hook(my_id, &merge_estates);
1576 add_unmatched_state_hook(my_id, &unmatched_state);
1577 add_hook(&match_function_def, FUNC_DEF_HOOK);
1578 add_hook(&match_declarations, DECLARATION_HOOK);
1579 select_caller_info_hook(set_param_value, PARAM_VALUE);
1580 select_caller_info_hook(set_param_hard_max, FUZZY_MAX);
1581 select_return_states_hook(RETURN_VALUE, &db_returned_member_info);
1582 select_return_states_before(&db_limited_before);
1583 select_return_states_hook(LIMITED_VALUE, &db_param_limit);
1584 select_return_states_hook(FILTER_VALUE, &db_param_filter);
1585 select_return_states_hook(ADDED_VALUE, &db_param_add);
1586 select_return_states_after(&db_limited_after);
1589 void register_smatch_extra_late(int id)
1591 add_hook(&match_dereferences, DEREF_HOOK);
1592 add_hook(&match_pointer_as_array, OP_HOOK);
1593 select_call_implies_hook(DEREFERENCE, &set_param_dereferenced);
1594 add_hook(&match_function_call, FUNCTION_CALL_HOOK);
1595 add_hook(&match_assign, ASSIGNMENT_HOOK);
1596 add_hook(&unop_expr, OP_HOOK);
1597 add_hook(&asm_expr, ASM_HOOK);
1599 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
1600 add_member_info_callback(my_id, struct_member_callback);
1601 add_returned_member_callback(my_id, returned_member_callback);
1603 add_hook(&assume_indexes_are_valid, OP_HOOK);