assigned_expr: remove debug code
[smatch.git] / smatch_extra.c
blob46b242572dccf899a0b4a7f171c0f1798033c3aa
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 expression *expr, struct smatch_state *state);
64 DECLARE_PTR_LIST(void_fn_list, mod_hook *);
65 static struct void_fn_list *extra_mod_hooks;
66 static struct void_fn_list *extra_nomod_hooks;
68 void add_extra_mod_hook(mod_hook *fn)
70 mod_hook **p = malloc(sizeof(mod_hook *));
71 *p = fn;
72 add_ptr_list(&extra_mod_hooks, p);
75 void add_extra_nomod_hook(mod_hook *fn)
77 mod_hook **p = malloc(sizeof(mod_hook *));
78 *p = fn;
79 add_ptr_list(&extra_nomod_hooks, p);
82 void call_extra_hooks(struct void_fn_list *hooks, const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
84 mod_hook **fn;
86 FOR_EACH_PTR(hooks, fn) {
87 (*fn)(name, sym, expr, state);
88 } END_FOR_EACH_PTR(fn);
91 void call_extra_mod_hooks(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
93 call_extra_hooks(extra_mod_hooks, name, sym, expr, state);
96 void call_extra_nomod_hooks(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
98 call_extra_hooks(extra_nomod_hooks, name, sym, expr, state);
101 static bool in_param_set;
102 static void set_extra_mod_helper(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
104 remove_from_equiv(name, sym);
105 call_extra_mod_hooks(name, sym, expr, state);
106 if ((__in_fake_assign || in_param_set) &&
107 estate_is_unknown(state) && !get_state(SMATCH_EXTRA, name, sym))
108 return;
109 set_state(SMATCH_EXTRA, name, sym, state);
112 static void set_extra_nomod_helper(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
114 call_extra_nomod_hooks(name, sym, expr, state);
115 set_state(SMATCH_EXTRA, name, sym, state);
118 static char *get_pointed_at(const char *name, struct symbol *sym, struct symbol **new_sym)
120 struct expression *assigned;
122 if (name[0] != '*')
123 return NULL;
124 if (strcmp(name + 1, sym->ident->name) != 0)
125 return NULL;
127 assigned = get_assigned_expr_name_sym(sym->ident->name, sym);
128 if (!assigned)
129 return NULL;
130 assigned = strip_parens(assigned);
131 if (assigned->type != EXPR_PREOP || assigned->op != '&')
132 return NULL;
134 return expr_to_var_sym(assigned->unop, new_sym);
137 char *get_other_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym)
139 struct expression *assigned;
140 char *orig_name = NULL;
141 char buf[256];
142 char *ret = NULL;
143 int skip;
145 *new_sym = NULL;
147 if (!sym || !sym->ident)
148 return NULL;
150 ret = get_pointed_at(name, sym, new_sym);
151 if (ret)
152 return ret;
154 skip = strlen(sym->ident->name);
155 if (name[skip] != '-' || name[skip + 1] != '>')
156 return NULL;
157 skip += 2;
159 assigned = get_assigned_expr_name_sym(sym->ident->name, sym);
160 if (!assigned)
161 return NULL;
162 if (assigned->type == EXPR_CALL)
163 return map_call_to_other_name_sym(name, sym, new_sym);
164 if (assigned->type == EXPR_PREOP || assigned->op == '&') {
166 orig_name = expr_to_var_sym(assigned, new_sym);
167 if (!orig_name || !*new_sym)
168 goto free;
170 snprintf(buf, sizeof(buf), "%s.%s", orig_name + 1, name + skip);
171 ret = alloc_string(buf);
172 free_string(orig_name);
173 return ret;
176 if (assigned->type != EXPR_DEREF)
177 goto free;
179 orig_name = expr_to_var_sym(assigned, new_sym);
180 if (!orig_name || !*new_sym)
181 goto free;
183 snprintf(buf, sizeof(buf), "%s->%s", orig_name, name + skip);
184 ret = alloc_string(buf);
185 free_string(orig_name);
186 return ret;
188 free:
189 free_string(orig_name);
190 return NULL;
193 void set_extra_mod(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
195 char *new_name;
196 struct symbol *new_sym;
198 set_extra_mod_helper(name, sym, expr, state);
199 new_name = get_other_name_sym(name, sym, &new_sym);
200 if (new_name && new_sym)
201 set_extra_mod_helper(new_name, new_sym, expr, state);
202 free_string(new_name);
205 static struct expression *chunk_get_array_base(struct expression *expr)
208 * The problem with is_array() is that it only returns true for things
209 * like foo[1] but not for foo[1].bar.
212 expr = strip_expr(expr);
213 while (expr && expr->type == EXPR_DEREF)
214 expr = strip_expr(expr->deref);
215 return get_array_base(expr);
218 static int chunk_has_array(struct expression *expr)
220 return !!chunk_get_array_base(expr);
223 static void clear_array_states(struct expression *array)
225 struct sm_state *sm;
227 sm = get_sm_state_expr(link_id, array);
228 if (sm)
229 match_link_modify(sm, NULL);
232 static void set_extra_array_mod(struct expression *expr, struct smatch_state *state)
234 struct expression *array;
235 struct var_sym_list *vsl;
236 struct var_sym *vs;
237 char *name;
238 struct symbol *sym;
240 array = chunk_get_array_base(expr);
242 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
243 if (!name || !vsl) {
244 clear_array_states(array);
245 goto free;
248 FOR_EACH_PTR(vsl, vs) {
249 store_link(link_id, vs->var, vs->sym, name, sym);
250 } END_FOR_EACH_PTR(vs);
252 call_extra_mod_hooks(name, sym, expr, state);
253 set_state(SMATCH_EXTRA, name, sym, state);
254 free:
255 free_string(name);
258 void set_extra_expr_mod(struct expression *expr, struct smatch_state *state)
260 struct symbol *sym;
261 char *name;
263 if (chunk_has_array(expr)) {
264 set_extra_array_mod(expr, state);
265 return;
268 expr = strip_expr(expr);
269 name = expr_to_var_sym(expr, &sym);
270 if (!name || !sym)
271 goto free;
272 set_extra_mod(name, sym, expr, state);
273 free:
274 free_string(name);
277 void set_extra_nomod(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
279 char *new_name;
280 struct symbol *new_sym;
281 struct relation *rel;
282 struct smatch_state *orig_state;
284 orig_state = get_state(SMATCH_EXTRA, name, sym);
286 /* don't save unknown states if leaving it blank is the same */
287 if (!orig_state && estate_is_unknown(state))
288 return;
290 new_name = get_other_name_sym(name, sym, &new_sym);
291 if (new_name && new_sym)
292 set_extra_nomod_helper(new_name, new_sym, expr, state);
293 free_string(new_name);
295 if (!estate_related(orig_state)) {
296 set_extra_nomod_helper(name, sym, expr, state);
297 return;
300 set_related(state, estate_related(orig_state));
301 FOR_EACH_PTR(estate_related(orig_state), rel) {
302 struct smatch_state *estate;
304 if (option_debug_related)
305 sm_msg("%s updating related %s to %s", name, rel->name, state->name);
306 estate = get_state(SMATCH_EXTRA, rel->name, rel->sym);
307 if (!estate)
308 continue;
309 set_extra_nomod_helper(rel->name, rel->sym, expr, clone_estate_cast(estate_type(estate), state));
310 } END_FOR_EACH_PTR(rel);
313 void set_extra_nomod_vsl(const char *name, struct symbol *sym, struct var_sym_list *vsl, struct expression *expr, struct smatch_state *state)
315 struct var_sym *vs;
317 FOR_EACH_PTR(vsl, vs) {
318 store_link(link_id, vs->var, vs->sym, name, sym);
319 } END_FOR_EACH_PTR(vs);
321 set_extra_nomod(name, sym, expr, state);
325 * This is for return_implies_state() hooks which modify a SMATCH_EXTRA state
327 void set_extra_expr_nomod(struct expression *expr, struct smatch_state *state)
329 struct var_sym_list *vsl;
330 struct var_sym *vs;
331 char *name;
332 struct symbol *sym;
334 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
335 if (!name || !vsl)
336 goto free;
337 FOR_EACH_PTR(vsl, vs) {
338 store_link(link_id, vs->var, vs->sym, name, sym);
339 } END_FOR_EACH_PTR(vs);
341 set_extra_nomod(name, sym, expr, state);
342 free:
343 free_string(name);
346 static void set_extra_true_false(const char *name, struct symbol *sym,
347 struct smatch_state *true_state,
348 struct smatch_state *false_state)
350 char *new_name;
351 struct symbol *new_sym;
352 struct relation *rel;
353 struct smatch_state *orig_state;
355 if (!true_state && !false_state)
356 return;
358 if (in_warn_on_macro())
359 return;
361 new_name = get_other_name_sym(name, sym, &new_sym);
362 if (new_name && new_sym)
363 set_true_false_states(SMATCH_EXTRA, new_name, new_sym, true_state, false_state);
364 free_string(new_name);
366 orig_state = get_state(SMATCH_EXTRA, name, sym);
368 if (!estate_related(orig_state)) {
369 set_true_false_states(SMATCH_EXTRA, name, sym, true_state, false_state);
370 return;
373 if (true_state)
374 set_related(true_state, estate_related(orig_state));
375 if (false_state)
376 set_related(false_state, estate_related(orig_state));
378 FOR_EACH_PTR(estate_related(orig_state), rel) {
379 set_true_false_states(SMATCH_EXTRA, rel->name, rel->sym,
380 true_state, false_state);
381 } END_FOR_EACH_PTR(rel);
384 static void set_extra_chunk_true_false(struct expression *expr,
385 struct smatch_state *true_state,
386 struct smatch_state *false_state)
388 struct var_sym_list *vsl;
389 struct var_sym *vs;
390 struct symbol *type;
391 char *name;
392 struct symbol *sym;
394 if (in_warn_on_macro())
395 return;
397 type = get_type(expr);
398 if (!type)
399 return;
401 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
402 if (!name || !vsl)
403 goto free;
404 FOR_EACH_PTR(vsl, vs) {
405 store_link(link_id, vs->var, vs->sym, name, sym);
406 } END_FOR_EACH_PTR(vs);
408 set_true_false_states(SMATCH_EXTRA, name, sym,
409 clone_estate(true_state),
410 clone_estate(false_state));
411 free:
412 free_string(name);
415 static void set_extra_expr_true_false(struct expression *expr,
416 struct smatch_state *true_state,
417 struct smatch_state *false_state)
419 char *name;
420 struct symbol *sym;
421 sval_t sval;
423 if (!true_state && !false_state)
424 return;
426 if (get_value(expr, &sval))
427 return;
429 expr = strip_expr(expr);
430 name = expr_to_var_sym(expr, &sym);
431 if (!name || !sym) {
432 free_string(name);
433 set_extra_chunk_true_false(expr, true_state, false_state);
434 return;
436 set_extra_true_false(name, sym, true_state, false_state);
437 free_string(name);
440 static struct sm_state *handle_canonical_while_count_down(struct statement *loop)
442 struct expression *iter_var;
443 struct expression *condition;
444 struct sm_state *sm;
445 struct smatch_state *estate;
446 sval_t start;
448 condition = strip_expr(loop->iterator_pre_condition);
449 if (!condition)
450 return NULL;
451 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
452 return NULL;
453 if (condition->op != SPECIAL_DECREMENT)
454 return NULL;
456 iter_var = condition->unop;
457 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
458 if (!sm)
459 return NULL;
460 if (sval_cmp_val(estate_min(sm->state), 0) < 0)
461 return NULL;
462 start = estate_max(sm->state);
463 if (sval_cmp_val(start, 0) <= 0)
464 return NULL;
465 if (!sval_is_max(start))
466 start.value--;
468 if (condition->type == EXPR_PREOP) {
469 estate = alloc_estate_range(sval_type_val(start.type, 1), start);
470 if (estate_has_hard_max(sm->state))
471 estate_set_hard_max(estate);
472 estate_copy_fuzzy_max(estate, sm->state);
473 set_extra_expr_mod(iter_var, estate);
475 if (condition->type == EXPR_POSTOP) {
476 estate = alloc_estate_range(sval_type_val(start.type, 0), start);
477 if (estate_has_hard_max(sm->state))
478 estate_set_hard_max(estate);
479 estate_copy_fuzzy_max(estate, sm->state);
480 set_extra_expr_mod(iter_var, estate);
482 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
485 static struct sm_state *handle_canonical_for_inc(struct expression *iter_expr,
486 struct expression *condition)
488 struct expression *iter_var;
489 struct sm_state *sm;
490 struct smatch_state *estate;
491 sval_t start, end, max;
493 iter_var = iter_expr->unop;
494 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
495 if (!sm)
496 return NULL;
497 if (!estate_get_single_value(sm->state, &start))
498 return NULL;
499 if (get_implied_max(condition->right, &end))
500 end = sval_cast(get_type(iter_var), end);
501 else
502 end = sval_type_max(get_type(iter_var));
504 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
505 return NULL;
507 switch (condition->op) {
508 case SPECIAL_UNSIGNED_LT:
509 case SPECIAL_NOTEQUAL:
510 case '<':
511 if (!sval_is_min(end))
512 end.value--;
513 break;
514 case SPECIAL_UNSIGNED_LTE:
515 case SPECIAL_LTE:
516 break;
517 default:
518 return NULL;
520 if (sval_cmp(end, start) < 0)
521 return NULL;
522 estate = alloc_estate_range(start, end);
523 if (get_hard_max(condition->right, &max)) {
524 estate_set_hard_max(estate);
525 if (condition->op == '<' ||
526 condition->op == SPECIAL_UNSIGNED_LT ||
527 condition->op == SPECIAL_NOTEQUAL)
528 max.value--;
529 estate_set_fuzzy_max(estate, max);
531 set_extra_expr_mod(iter_var, estate);
532 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
535 static struct sm_state *handle_canonical_for_dec(struct expression *iter_expr,
536 struct expression *condition)
538 struct expression *iter_var;
539 struct sm_state *sm;
540 struct smatch_state *estate;
541 sval_t start, end;
543 iter_var = iter_expr->unop;
544 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
545 if (!sm)
546 return NULL;
547 if (!estate_get_single_value(sm->state, &start))
548 return NULL;
549 if (!get_implied_min(condition->right, &end))
550 end = sval_type_min(get_type(iter_var));
551 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
552 return NULL;
554 switch (condition->op) {
555 case SPECIAL_NOTEQUAL:
556 case '>':
557 if (!sval_is_min(end) && !sval_is_max(end))
558 end.value++;
559 break;
560 case SPECIAL_GTE:
561 break;
562 default:
563 return NULL;
565 if (sval_cmp(end, start) > 0)
566 return NULL;
567 estate = alloc_estate_range(end, start);
568 estate_set_hard_max(estate);
569 estate_set_fuzzy_max(estate, estate_get_fuzzy_max(estate));
570 set_extra_expr_mod(iter_var, estate);
571 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
574 static struct sm_state *handle_canonical_for_loops(struct statement *loop)
576 struct expression *iter_expr;
577 struct expression *condition;
579 if (!loop->iterator_post_statement)
580 return NULL;
581 if (loop->iterator_post_statement->type != STMT_EXPRESSION)
582 return NULL;
583 iter_expr = loop->iterator_post_statement->expression;
584 if (!loop->iterator_pre_condition)
585 return NULL;
586 if (loop->iterator_pre_condition->type != EXPR_COMPARE)
587 return NULL;
588 condition = loop->iterator_pre_condition;
590 if (iter_expr->op == SPECIAL_INCREMENT)
591 return handle_canonical_for_inc(iter_expr, condition);
592 if (iter_expr->op == SPECIAL_DECREMENT)
593 return handle_canonical_for_dec(iter_expr, condition);
594 return NULL;
597 struct sm_state *__extra_handle_canonical_loops(struct statement *loop, struct stree **stree)
599 struct sm_state *ret;
601 __push_fake_cur_stree();
602 if (!loop->iterator_post_statement)
603 ret = handle_canonical_while_count_down(loop);
604 else
605 ret = handle_canonical_for_loops(loop);
606 *stree = __pop_fake_cur_stree();
607 return ret;
610 int __iterator_unchanged(struct sm_state *sm)
612 if (!sm)
613 return 0;
614 if (get_sm_state(my_id, sm->name, sm->sym) == sm)
615 return 1;
616 return 0;
619 static void while_count_down_after(struct sm_state *sm, struct expression *condition)
621 sval_t after_value;
623 /* paranoid checking. prolly not needed */
624 condition = strip_expr(condition);
625 if (!condition)
626 return;
627 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
628 return;
629 if (condition->op != SPECIAL_DECREMENT)
630 return;
631 after_value = estate_min(sm->state);
632 after_value.value--;
633 set_extra_mod(sm->name, sm->sym, condition->unop, alloc_estate_sval(after_value));
636 void __extra_pre_loop_hook_after(struct sm_state *sm,
637 struct statement *iterator,
638 struct expression *condition)
640 struct expression *iter_expr;
641 sval_t limit;
642 struct smatch_state *state;
644 if (!iterator) {
645 while_count_down_after(sm, condition);
646 return;
649 iter_expr = iterator->expression;
651 if (condition->type != EXPR_COMPARE)
652 return;
653 if (iter_expr->op == SPECIAL_INCREMENT) {
654 limit = sval_binop(estate_max(sm->state), '+',
655 sval_type_val(estate_type(sm->state), 1));
656 } else {
657 limit = sval_binop(estate_min(sm->state), '-',
658 sval_type_val(estate_type(sm->state), 1));
660 if (!estate_has_hard_max(sm->state) && !__has_breaks()) {
661 if (iter_expr->op == SPECIAL_INCREMENT)
662 state = alloc_estate_range(estate_min(sm->state), limit);
663 else
664 state = alloc_estate_range(limit, estate_max(sm->state));
665 } else {
666 state = alloc_estate_sval(limit);
668 if (!estate_has_hard_max(sm->state)) {
669 estate_clear_hard_max(state);
671 if (estate_has_fuzzy_max(sm->state)) {
672 sval_t hmax = estate_get_fuzzy_max(sm->state);
673 sval_t max = estate_max(sm->state);
675 if (sval_cmp(hmax, max) != 0)
676 estate_clear_fuzzy_max(state);
677 } else if (!estate_has_fuzzy_max(sm->state)) {
678 estate_clear_fuzzy_max(state);
681 set_extra_mod(sm->name, sm->sym, iter_expr, state);
684 static struct stree *unmatched_stree;
685 static struct smatch_state *unmatched_state(struct sm_state *sm)
687 struct smatch_state *state;
689 if (unmatched_stree) {
690 state = get_state_stree(unmatched_stree, SMATCH_EXTRA, sm->name, sm->sym);
691 if (state)
692 return state;
694 if (parent_is_gone_var_sym(sm->name, sm->sym))
695 return alloc_estate_empty();
696 return alloc_estate_whole(estate_type(sm->state));
699 static void clear_the_pointed_at(struct expression *expr)
701 struct stree *stree;
702 char *name;
703 struct symbol *sym;
704 struct sm_state *tmp;
706 name = expr_to_var_sym(expr, &sym);
707 if (!name || !sym)
708 goto free;
710 stree = __get_cur_stree();
711 FOR_EACH_MY_SM(SMATCH_EXTRA, stree, tmp) {
712 if (tmp->name[0] != '*')
713 continue;
714 if (tmp->sym != sym)
715 continue;
716 if (strcmp(tmp->name + 1, name) != 0)
717 continue;
718 set_extra_mod(tmp->name, tmp->sym, expr, alloc_estate_whole(estate_type(tmp->state)));
719 } END_FOR_EACH_SM(tmp);
721 free:
722 free_string(name);
725 static int is_const_param(struct expression *expr, int param)
727 struct symbol *type;
729 type = get_arg_type(expr, param);
730 if (!type)
731 return 0;
732 if (type->ctype.modifiers & MOD_CONST)
733 return 1;
734 return 0;
737 static void match_function_call(struct expression *expr)
739 struct expression *arg;
740 struct expression *tmp;
741 int param = -1;
743 /* if we have the db this is handled in smatch_function_hooks.c */
744 if (!option_no_db)
745 return;
746 if (inlinable(expr->fn))
747 return;
749 FOR_EACH_PTR(expr->args, arg) {
750 param++;
751 if (is_const_param(expr->fn, param))
752 continue;
753 tmp = strip_expr(arg);
754 if (tmp->type == EXPR_PREOP && tmp->op == '&')
755 set_extra_expr_mod(tmp->unop, alloc_estate_whole(get_type(tmp->unop)));
756 else
757 clear_the_pointed_at(tmp);
758 } END_FOR_EACH_PTR(arg);
761 static int values_fit_type(struct expression *left, struct expression *right)
763 struct range_list *rl;
764 struct symbol *type;
766 type = get_type(left);
767 if (!type)
768 return 0;
769 get_absolute_rl(right, &rl);
770 if (type_unsigned(type) && sval_is_negative(rl_min(rl)))
771 return 0;
772 if (sval_cmp(sval_type_min(type), rl_min(rl)) > 0)
773 return 0;
774 if (sval_cmp(sval_type_max(type), rl_max(rl)) < 0)
775 return 0;
776 return 1;
779 static void save_chunk_info(struct expression *left, struct expression *right)
781 struct var_sym_list *vsl;
782 struct var_sym *vs;
783 struct expression *add_expr;
784 struct symbol *type;
785 sval_t sval;
786 char *name;
787 struct symbol *sym;
789 if (right->type != EXPR_BINOP || right->op != '-')
790 return;
791 if (!get_value(right->left, &sval))
792 return;
793 if (!expr_to_sym(right->right))
794 return;
796 add_expr = binop_expression(left, '+', right->right);
797 type = get_type(add_expr);
798 if (!type)
799 return;
800 name = expr_to_chunk_sym_vsl(add_expr, &sym, &vsl);
801 if (!name || !vsl)
802 goto free;
803 FOR_EACH_PTR(vsl, vs) {
804 store_link(link_id, vs->var, vs->sym, name, sym);
805 } END_FOR_EACH_PTR(vs);
807 set_state(SMATCH_EXTRA, name, sym, alloc_estate_sval(sval_cast(type, sval)));
808 free:
809 free_string(name);
812 static void do_array_assign(struct expression *left, int op, struct expression *right)
814 struct range_list *rl;
816 if (op == '=') {
817 get_absolute_rl(right, &rl);
818 rl = cast_rl(get_type(left), rl);
819 } else {
820 rl = alloc_whole_rl(get_type(left));
823 set_extra_array_mod(left, alloc_estate_rl(rl));
826 static void match_vanilla_assign(struct expression *left, struct expression *right)
828 struct range_list *orig_rl = NULL;
829 struct range_list *rl = NULL;
830 struct symbol *right_sym;
831 struct symbol *left_type;
832 struct symbol *right_type;
833 char *right_name = NULL;
834 struct symbol *sym;
835 char *name;
836 sval_t max;
837 struct smatch_state *state;
838 int comparison;
840 if (is_struct(left))
841 return;
843 save_chunk_info(left, right);
845 name = expr_to_var_sym(left, &sym);
846 if (!name) {
847 if (chunk_has_array(left))
848 do_array_assign(left, '=', right);
849 return;
852 left_type = get_type(left);
853 right_type = get_type(right);
855 right_name = expr_to_var_sym(right, &right_sym);
857 if (!__in_fake_assign &&
858 !(right->type == EXPR_PREOP && right->op == '&') &&
859 right_name && right_sym &&
860 values_fit_type(left, right) &&
861 !has_symbol(right, sym)) {
862 set_equiv(left, right);
863 goto free;
866 if (is_pointer(right) && get_address_rl(right, &rl)) {
867 state = alloc_estate_rl(rl);
868 goto done;
871 if (__in_fake_assign) {
872 struct smatch_state *right_state;
873 sval_t sval;
875 if (get_value(right, &sval)) {
876 sval = sval_cast(left_type, sval);
877 state = alloc_estate_sval(sval);
878 goto done;
881 right_state = get_state(SMATCH_EXTRA, right_name, right_sym);
882 if (right_state) {
883 /* simple assignment */
884 state = clone_estate(right_state);
885 goto done;
888 state = alloc_estate_rl(alloc_whole_rl(left_type));
889 goto done;
892 comparison = get_comparison(left, right);
893 if (comparison) {
894 comparison = flip_comparison(comparison);
895 get_implied_rl(left, &orig_rl);
898 if (get_implied_rl(right, &rl)) {
899 rl = cast_rl(left_type, rl);
900 if (orig_rl)
901 filter_by_comparison(&rl, comparison, orig_rl);
902 state = alloc_estate_rl(rl);
903 if (get_hard_max(right, &max)) {
904 estate_set_hard_max(state);
905 estate_set_fuzzy_max(state, max);
907 } else {
908 rl = alloc_whole_rl(right_type);
909 rl = cast_rl(left_type, rl);
910 if (orig_rl)
911 filter_by_comparison(&rl, comparison, orig_rl);
912 state = alloc_estate_rl(rl);
915 done:
916 set_extra_mod(name, sym, left, state);
917 free:
918 free_string(right_name);
921 static int op_remove_assign(int op)
923 switch (op) {
924 case SPECIAL_ADD_ASSIGN:
925 return '+';
926 case SPECIAL_SUB_ASSIGN:
927 return '-';
928 case SPECIAL_MUL_ASSIGN:
929 return '*';
930 case SPECIAL_DIV_ASSIGN:
931 return '/';
932 case SPECIAL_MOD_ASSIGN:
933 return '%';
934 case SPECIAL_AND_ASSIGN:
935 return '&';
936 case SPECIAL_OR_ASSIGN:
937 return '|';
938 case SPECIAL_XOR_ASSIGN:
939 return '^';
940 case SPECIAL_SHL_ASSIGN:
941 return SPECIAL_LEFTSHIFT;
942 case SPECIAL_SHR_ASSIGN:
943 return SPECIAL_RIGHTSHIFT;
944 default:
945 return op;
949 static void match_assign(struct expression *expr)
951 struct range_list *rl = NULL;
952 struct expression *left;
953 struct expression *right;
954 struct expression *binop_expr;
955 struct symbol *left_type;
956 struct symbol *sym;
957 char *name;
958 sval_t left_min, left_max;
959 sval_t right_min, right_max;
960 sval_t res_min, res_max;
962 left = strip_expr(expr->left);
964 right = strip_parens(expr->right);
965 if (right->type == EXPR_CALL && sym_name_is("__builtin_expect", right->fn))
966 right = get_argument_from_call_expr(right->args, 0);
967 while (right->type == EXPR_ASSIGNMENT && right->op == '=')
968 right = strip_parens(right->left);
970 if (expr->op == '=' && is_condition(expr->right))
971 return; /* handled in smatch_condition.c */
972 if (expr->op == '=' && right->type == EXPR_CALL)
973 return; /* handled in smatch_function_hooks.c */
974 if (expr->op == '=') {
975 match_vanilla_assign(left, right);
976 return;
979 name = expr_to_var_sym(left, &sym);
980 if (!name)
981 return;
983 left_type = get_type(left);
985 res_min = sval_type_min(left_type);
986 res_max = sval_type_max(left_type);
988 switch (expr->op) {
989 case SPECIAL_ADD_ASSIGN:
990 get_absolute_max(left, &left_max);
991 get_absolute_max(right, &right_max);
992 if (sval_binop_overflows(left_max, '+', sval_cast(left_type, right_max)))
993 break;
994 if (get_implied_min(left, &left_min) &&
995 !sval_is_negative_min(left_min) &&
996 get_implied_min(right, &right_min) &&
997 !sval_is_negative_min(right_min)) {
998 res_min = sval_binop(left_min, '+', right_min);
999 res_min = sval_cast(left_type, res_min);
1001 if (inside_loop()) /* we are assuming loops don't lead to wrapping */
1002 break;
1003 res_max = sval_binop(left_max, '+', right_max);
1004 res_max = sval_cast(left_type, res_max);
1005 break;
1006 case SPECIAL_SUB_ASSIGN:
1007 if (get_implied_max(left, &left_max) &&
1008 !sval_is_max(left_max) &&
1009 get_implied_min(right, &right_min) &&
1010 !sval_is_min(right_min)) {
1011 res_max = sval_binop(left_max, '-', right_min);
1012 res_max = sval_cast(left_type, res_max);
1014 if (inside_loop())
1015 break;
1016 if (get_implied_min(left, &left_min) &&
1017 !sval_is_min(left_min) &&
1018 get_implied_max(right, &right_max) &&
1019 !sval_is_max(right_max)) {
1020 res_min = sval_binop(left_min, '-', right_max);
1021 res_min = sval_cast(left_type, res_min);
1023 break;
1024 case SPECIAL_AND_ASSIGN:
1025 case SPECIAL_MOD_ASSIGN:
1026 case SPECIAL_SHL_ASSIGN:
1027 case SPECIAL_SHR_ASSIGN:
1028 case SPECIAL_OR_ASSIGN:
1029 case SPECIAL_XOR_ASSIGN:
1030 case SPECIAL_MUL_ASSIGN:
1031 case SPECIAL_DIV_ASSIGN:
1032 binop_expr = binop_expression(expr->left,
1033 op_remove_assign(expr->op),
1034 expr->right);
1035 if (get_absolute_rl(binop_expr, &rl)) {
1036 rl = cast_rl(left_type, rl);
1037 set_extra_mod(name, sym, left, alloc_estate_rl(rl));
1038 goto free;
1040 break;
1042 rl = cast_rl(left_type, alloc_rl(res_min, res_max));
1043 set_extra_mod(name, sym, left, alloc_estate_rl(rl));
1044 free:
1045 free_string(name);
1048 static struct smatch_state *increment_state(struct smatch_state *state)
1050 sval_t min = estate_min(state);
1051 sval_t max = estate_max(state);
1053 if (!estate_rl(state))
1054 return NULL;
1056 if (inside_loop())
1057 max = sval_type_max(max.type);
1059 if (!sval_is_min(min) && !sval_is_max(min))
1060 min.value++;
1061 if (!sval_is_min(max) && !sval_is_max(max))
1062 max.value++;
1063 return alloc_estate_range(min, max);
1066 static struct smatch_state *decrement_state(struct smatch_state *state)
1068 sval_t min = estate_min(state);
1069 sval_t max = estate_max(state);
1071 if (!estate_rl(state))
1072 return NULL;
1074 if (inside_loop())
1075 min = sval_type_min(min.type);
1077 if (!sval_is_min(min) && !sval_is_max(min))
1078 min.value--;
1079 if (!sval_is_min(max) && !sval_is_max(max))
1080 max.value--;
1081 return alloc_estate_range(min, max);
1084 static void clear_pointed_at_state(struct expression *expr)
1086 struct symbol *type;
1089 * ALERT: This is sort of a mess. If it's is a struct assigment like
1090 * "foo = bar;", then that's handled by smatch_struct_assignment.c.
1091 * the same thing for p++ where "p" is a struct. Most modifications
1092 * are handled by the assignment hook or the db. Smatch_extra.c doesn't
1093 * use smatch_modification.c because we have to get the ordering right
1094 * or something. So if you have p++ where p is a pointer to a standard
1095 * c type then we handle that here. What a mess.
1098 type = get_type(expr);
1099 if (!type || type->type != SYM_PTR)
1100 return;
1101 type = get_real_base_type(type);
1102 if (!type || type->type != SYM_BASETYPE)
1103 return;
1104 set_extra_expr_mod(deref_expression(expr), alloc_estate_whole(type));
1107 static void unop_expr(struct expression *expr)
1109 struct smatch_state *state;
1111 if (expr->smatch_flags & Handled)
1112 return;
1114 switch (expr->op) {
1115 case SPECIAL_INCREMENT:
1116 state = get_state_expr(SMATCH_EXTRA, expr->unop);
1117 state = increment_state(state);
1118 if (!state)
1119 state = alloc_estate_whole(get_type(expr));
1120 set_extra_expr_mod(expr->unop, state);
1121 clear_pointed_at_state(expr->unop);
1122 break;
1123 case SPECIAL_DECREMENT:
1124 state = get_state_expr(SMATCH_EXTRA, expr->unop);
1125 state = decrement_state(state);
1126 if (!state)
1127 state = alloc_estate_whole(get_type(expr));
1128 set_extra_expr_mod(expr->unop, state);
1129 clear_pointed_at_state(expr->unop);
1130 break;
1131 default:
1132 return;
1136 static void asm_expr(struct statement *stmt)
1139 struct expression *expr;
1140 struct symbol *type;
1141 int state = 0;
1143 FOR_EACH_PTR(stmt->asm_outputs, expr) {
1144 switch (state) {
1145 case 0: /* identifier */
1146 case 1: /* constraint */
1147 state++;
1148 continue;
1149 case 2: /* expression */
1150 state = 0;
1151 type = get_type(strip_expr(expr));
1152 set_extra_expr_mod(expr, alloc_estate_whole(type));
1153 continue;
1155 } END_FOR_EACH_PTR(expr);
1158 static void check_dereference(struct expression *expr)
1160 struct smatch_state *state;
1162 if (__in_fake_assign)
1163 return;
1164 if (outside_of_function())
1165 return;
1166 state = get_extra_state(expr);
1167 if (state) {
1168 static struct range_list *valid_rl;
1169 struct range_list *rl;
1171 if (!valid_rl) {
1172 valid_rl = alloc_rl(valid_ptr_min_sval, valid_ptr_max_sval);
1173 valid_rl = clone_rl_permanent(valid_rl);
1176 rl = rl_intersection(estate_rl(state), valid_rl);
1177 if (rl_equiv(rl, estate_rl(state)))
1178 return;
1179 set_extra_expr_nomod(expr, alloc_estate_rl(rl));
1180 } else {
1181 set_extra_expr_nomod(expr, alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval));
1185 static void match_dereferences(struct expression *expr)
1187 if (expr->type != EXPR_PREOP)
1188 return;
1189 /* it's saying that foo[1] = bar dereferences foo[1] */
1190 if (is_array(expr))
1191 return;
1192 check_dereference(expr->unop);
1195 static void match_pointer_as_array(struct expression *expr)
1197 if (!is_array(expr))
1198 return;
1199 check_dereference(get_array_base(expr));
1202 static void find_dereferences(struct expression *expr)
1204 while (expr->type == EXPR_PREOP) {
1205 if (expr->op == '*')
1206 check_dereference(expr->unop);
1207 expr = strip_expr(expr->unop);
1211 static void set_param_dereferenced(struct expression *call, struct expression *arg, char *key, char *unused)
1213 struct symbol *sym;
1214 char *name;
1216 name = get_variable_from_key(arg, key, &sym);
1217 if (name && sym) {
1218 struct smatch_state *orig, *new;
1219 struct range_list *rl;
1221 orig = get_state(SMATCH_EXTRA, name, sym);
1222 if (orig) {
1223 rl = rl_intersection(estate_rl(orig),
1224 alloc_rl(valid_ptr_min_sval,
1225 valid_ptr_max_sval));
1226 new = alloc_estate_rl(rl);
1227 } else {
1228 new = alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval);
1231 set_extra_nomod(name, sym, NULL, new);
1233 free_string(name);
1235 find_dereferences(arg);
1238 static sval_t add_one(sval_t sval)
1240 sval.value++;
1241 return sval;
1244 static int handle_postop_inc(struct expression *left, int op, struct expression *right)
1246 struct statement *stmt;
1247 struct expression *cond;
1248 struct smatch_state *true_state, *false_state;
1249 sval_t start;
1250 sval_t limit;
1253 * If we're decrementing here then that's a canonical while count down
1254 * so it's handled already. We're only handling loops like:
1255 * i = 0;
1256 * do { ... } while (i++ < 3);
1259 if (left->type != EXPR_POSTOP || left->op != SPECIAL_INCREMENT)
1260 return 0;
1262 stmt = __cur_stmt->parent;
1263 if (!stmt)
1264 return 0;
1265 if (stmt->type == STMT_COMPOUND)
1266 stmt = stmt->parent;
1267 if (!stmt || stmt->type != STMT_ITERATOR || !stmt->iterator_post_condition)
1268 return 0;
1270 cond = strip_expr(stmt->iterator_post_condition);
1271 if (cond->type != EXPR_COMPARE || cond->op != op)
1272 return 0;
1273 if (left != strip_expr(cond->left) || right != strip_expr(cond->right))
1274 return 0;
1276 if (!get_implied_value(left->unop, &start))
1277 return 0;
1278 if (!get_implied_value(right, &limit))
1279 return 0;
1281 if (sval_cmp(start, limit) > 0)
1282 return 0;
1284 switch (op) {
1285 case '<':
1286 case SPECIAL_UNSIGNED_LT:
1287 break;
1288 case SPECIAL_LTE:
1289 case SPECIAL_UNSIGNED_LTE:
1290 limit = add_one(limit);
1291 default:
1292 return 0;
1296 true_state = alloc_estate_range(add_one(start), limit);
1297 false_state = alloc_estate_range(add_one(limit), add_one(limit));
1299 /* Currently we just discard the false state but when two passes is
1300 * implimented correctly then it will use it.
1303 set_extra_expr_true_false(left->unop, true_state, false_state);
1305 return 1;
1308 static void handle_comparison(struct symbol *type, struct expression *left, int op, struct expression *right)
1310 struct range_list *left_orig;
1311 struct range_list *left_true;
1312 struct range_list *left_false;
1313 struct range_list *right_orig;
1314 struct range_list *right_true;
1315 struct range_list *right_false;
1316 struct smatch_state *left_true_state;
1317 struct smatch_state *left_false_state;
1318 struct smatch_state *right_true_state;
1319 struct smatch_state *right_false_state;
1320 sval_t dummy, hard_max;
1321 int left_postop = 0;
1322 int right_postop = 0;
1324 if (left->op == SPECIAL_INCREMENT || left->op == SPECIAL_DECREMENT) {
1325 if (left->type == EXPR_POSTOP) {
1326 left->smatch_flags |= Handled;
1327 left_postop = left->op;
1328 if (handle_postop_inc(left, op, right))
1329 return;
1331 left = strip_parens(left->unop);
1333 while (left->type == EXPR_ASSIGNMENT)
1334 left = strip_parens(left->left);
1336 if (right->op == SPECIAL_INCREMENT || right->op == SPECIAL_DECREMENT) {
1337 if (right->type == EXPR_POSTOP) {
1338 right->smatch_flags |= Handled;
1339 right_postop = right->op;
1341 right = strip_parens(right->unop);
1344 get_real_absolute_rl(left, &left_orig);
1345 left_orig = cast_rl(type, left_orig);
1347 get_real_absolute_rl(right, &right_orig);
1348 right_orig = cast_rl(type, right_orig);
1350 split_comparison_rl(left_orig, op, right_orig, &left_true, &left_false, &right_true, &right_false);
1352 left_true = rl_truncate_cast(get_type(strip_expr(left)), left_true);
1353 left_false = rl_truncate_cast(get_type(strip_expr(left)), left_false);
1354 right_true = rl_truncate_cast(get_type(strip_expr(right)), right_true);
1355 right_false = rl_truncate_cast(get_type(strip_expr(right)), right_false);
1357 if (!left_true || !left_false) {
1358 struct range_list *tmp_true, *tmp_false;
1360 split_comparison_rl(alloc_whole_rl(type), op, right_orig, &tmp_true, &tmp_false, NULL, NULL);
1361 tmp_true = rl_truncate_cast(get_type(strip_expr(left)), tmp_true);
1362 tmp_false = rl_truncate_cast(get_type(strip_expr(left)), tmp_false);
1363 if (tmp_true && tmp_false)
1364 __save_imaginary_state(left, tmp_true, tmp_false);
1367 if (!right_true || !right_false) {
1368 struct range_list *tmp_true, *tmp_false;
1370 split_comparison_rl(alloc_whole_rl(type), op, right_orig, NULL, NULL, &tmp_true, &tmp_false);
1371 tmp_true = rl_truncate_cast(get_type(strip_expr(right)), tmp_true);
1372 tmp_false = rl_truncate_cast(get_type(strip_expr(right)), tmp_false);
1373 if (tmp_true && tmp_false)
1374 __save_imaginary_state(right, tmp_true, tmp_false);
1377 left_true_state = alloc_estate_rl(left_true);
1378 left_false_state = alloc_estate_rl(left_false);
1379 right_true_state = alloc_estate_rl(right_true);
1380 right_false_state = alloc_estate_rl(right_false);
1382 switch (op) {
1383 case '<':
1384 case SPECIAL_UNSIGNED_LT:
1385 case SPECIAL_UNSIGNED_LTE:
1386 case SPECIAL_LTE:
1387 if (get_hard_max(right, &dummy))
1388 estate_set_hard_max(left_true_state);
1389 if (get_hard_max(left, &dummy))
1390 estate_set_hard_max(right_false_state);
1391 break;
1392 case '>':
1393 case SPECIAL_UNSIGNED_GT:
1394 case SPECIAL_UNSIGNED_GTE:
1395 case SPECIAL_GTE:
1396 if (get_hard_max(left, &dummy))
1397 estate_set_hard_max(right_true_state);
1398 if (get_hard_max(right, &dummy))
1399 estate_set_hard_max(left_false_state);
1400 break;
1403 switch (op) {
1404 case '<':
1405 case SPECIAL_UNSIGNED_LT:
1406 case SPECIAL_UNSIGNED_LTE:
1407 case SPECIAL_LTE:
1408 if (get_hard_max(right, &hard_max)) {
1409 if (op == '<' || op == SPECIAL_UNSIGNED_LT)
1410 hard_max.value--;
1411 estate_set_fuzzy_max(left_true_state, hard_max);
1413 if (get_implied_value(right, &hard_max)) {
1414 if (op == SPECIAL_UNSIGNED_LTE ||
1415 op == SPECIAL_LTE)
1416 hard_max.value++;
1417 estate_set_fuzzy_max(left_false_state, hard_max);
1419 if (get_hard_max(left, &hard_max)) {
1420 if (op == SPECIAL_UNSIGNED_LTE ||
1421 op == SPECIAL_LTE)
1422 hard_max.value--;
1423 estate_set_fuzzy_max(right_false_state, hard_max);
1425 if (get_implied_value(left, &hard_max)) {
1426 if (op == '<' || op == SPECIAL_UNSIGNED_LT)
1427 hard_max.value++;
1428 estate_set_fuzzy_max(right_true_state, hard_max);
1430 break;
1431 case '>':
1432 case SPECIAL_UNSIGNED_GT:
1433 case SPECIAL_UNSIGNED_GTE:
1434 case SPECIAL_GTE:
1435 if (get_hard_max(left, &hard_max)) {
1436 if (op == '>' || op == SPECIAL_UNSIGNED_GT)
1437 hard_max.value--;
1438 estate_set_fuzzy_max(right_true_state, hard_max);
1440 if (get_implied_value(left, &hard_max)) {
1441 if (op == SPECIAL_UNSIGNED_GTE ||
1442 op == SPECIAL_GTE)
1443 hard_max.value++;
1444 estate_set_fuzzy_max(right_false_state, hard_max);
1446 if (get_hard_max(right, &hard_max)) {
1447 if (op == SPECIAL_UNSIGNED_LTE ||
1448 op == SPECIAL_LTE)
1449 hard_max.value--;
1450 estate_set_fuzzy_max(left_false_state, hard_max);
1452 if (get_implied_value(right, &hard_max)) {
1453 if (op == '>' ||
1454 op == SPECIAL_UNSIGNED_GT)
1455 hard_max.value++;
1456 estate_set_fuzzy_max(left_true_state, hard_max);
1458 break;
1459 case SPECIAL_EQUAL:
1460 if (get_hard_max(left, &hard_max))
1461 estate_set_fuzzy_max(right_true_state, hard_max);
1462 if (get_hard_max(right, &hard_max))
1463 estate_set_fuzzy_max(left_true_state, hard_max);
1464 break;
1467 if (get_hard_max(left, &hard_max)) {
1468 estate_set_hard_max(left_true_state);
1469 estate_set_hard_max(left_false_state);
1471 if (get_hard_max(right, &hard_max)) {
1472 estate_set_hard_max(right_true_state);
1473 estate_set_hard_max(right_false_state);
1476 if (left_postop == SPECIAL_INCREMENT) {
1477 left_true_state = increment_state(left_true_state);
1478 left_false_state = increment_state(left_false_state);
1480 if (left_postop == SPECIAL_DECREMENT) {
1481 left_true_state = decrement_state(left_true_state);
1482 left_false_state = decrement_state(left_false_state);
1484 if (right_postop == SPECIAL_INCREMENT) {
1485 right_true_state = increment_state(right_true_state);
1486 right_false_state = increment_state(right_false_state);
1488 if (right_postop == SPECIAL_DECREMENT) {
1489 right_true_state = decrement_state(right_true_state);
1490 right_false_state = decrement_state(right_false_state);
1493 if (estate_rl(left_true_state) && estates_equiv(left_true_state, left_false_state)) {
1494 left_true_state = NULL;
1495 left_false_state = NULL;
1498 if (estate_rl(right_true_state) && estates_equiv(right_true_state, right_false_state)) {
1499 right_true_state = NULL;
1500 right_false_state = NULL;
1503 set_extra_expr_true_false(left, left_true_state, left_false_state);
1504 set_extra_expr_true_false(right, right_true_state, right_false_state);
1507 static int is_simple_math(struct expression *expr)
1509 if (!expr)
1510 return 0;
1511 if (expr->type != EXPR_BINOP)
1512 return 0;
1513 switch (expr->op) {
1514 case '+':
1515 case '-':
1516 case '*':
1517 return 1;
1519 return 0;
1522 static void move_known_values(struct expression **left_p, struct expression **right_p)
1524 struct expression *left = *left_p;
1525 struct expression *right = *right_p;
1526 sval_t sval, dummy;
1528 if (get_implied_value(left, &sval)) {
1529 if (!is_simple_math(right))
1530 return;
1531 if (get_implied_value(right, &dummy))
1532 return;
1533 if (right->op == '*') {
1534 sval_t divisor;
1536 if (!get_value(right->right, &divisor))
1537 return;
1538 if (divisor.value == 0 && sval.value % divisor.value)
1539 return;
1540 *left_p = binop_expression(left, invert_op(right->op), right->right);
1541 *right_p = right->left;
1542 return;
1544 if (right->op == '+' && get_value(right->left, &sval)) {
1545 *left_p = binop_expression(left, invert_op(right->op), right->left);
1546 *right_p = right->right;
1547 return;
1549 if (get_value(right->right, &sval)) {
1550 *left_p = binop_expression(left, invert_op(right->op), right->right);
1551 *right_p = right->left;
1552 return;
1554 return;
1556 if (get_implied_value(right, &sval)) {
1557 if (!is_simple_math(left))
1558 return;
1559 if (get_implied_value(left, &dummy))
1560 return;
1561 if (left->op == '*') {
1562 sval_t divisor;
1564 if (!get_value(left->right, &divisor))
1565 return;
1566 if (divisor.value == 0 && sval.value % divisor.value)
1567 return;
1568 *right_p = binop_expression(right, invert_op(left->op), left->right);
1569 *left_p = left->left;
1570 return;
1572 if (left->op == '+' && get_value(left->left, &sval)) {
1573 *right_p = binop_expression(right, invert_op(left->op), left->left);
1574 *left_p = left->right;
1575 return;
1578 if (get_value(left->right, &sval)) {
1579 *right_p = binop_expression(right, invert_op(left->op), left->right);
1580 *left_p = left->left;
1581 return;
1583 return;
1588 * The reason for do_simple_algebra() is to solve things like:
1589 * if (foo > 66 || foo + bar > 64) {
1590 * "foo" is not really a known variable so it won't be handled by
1591 * move_known_variables() but it's a super common idiom.
1594 static int do_simple_algebra(struct expression **left_p, struct expression **right_p)
1596 struct expression *left = *left_p;
1597 struct expression *right = *right_p;
1598 struct range_list *rl;
1599 sval_t tmp;
1601 if (left->type != EXPR_BINOP || left->op != '+')
1602 return 0;
1603 if (can_integer_overflow(get_type(left), left))
1604 return 0;
1605 if (!get_implied_value(right, &tmp))
1606 return 0;
1608 if (!get_implied_value(left->left, &tmp) &&
1609 get_implied_rl(left->left, &rl) &&
1610 !is_whole_rl(rl)) {
1611 *right_p = binop_expression(right, '-', left->left);
1612 *left_p = left->right;
1613 return 1;
1615 if (!get_implied_value(left->right, &tmp) &&
1616 get_implied_rl(left->right, &rl) &&
1617 !is_whole_rl(rl)) {
1618 *right_p = binop_expression(right, '-', left->right);
1619 *left_p = left->left;
1620 return 1;
1623 return 0;
1626 static int match_func_comparison(struct expression *expr)
1628 struct expression *left = strip_expr(expr->left);
1629 struct expression *right = strip_expr(expr->right);
1630 sval_t sval;
1633 * fixme: think about this harder. We should always be trying to limit
1634 * the non-call side as well. If we can't determine the limitter does
1635 * that mean we aren't querying the database and are missing important
1636 * information?
1639 if (left->type == EXPR_CALL) {
1640 if (get_implied_value(left, &sval)) {
1641 handle_comparison(get_type(expr), left, expr->op, right);
1642 return 1;
1644 function_comparison(left, expr->op, right);
1645 return 1;
1648 if (right->type == EXPR_CALL) {
1649 if (get_implied_value(right, &sval)) {
1650 handle_comparison(get_type(expr), left, expr->op, right);
1651 return 1;
1653 function_comparison(left, expr->op, right);
1654 return 1;
1657 return 0;
1660 /* Handle conditions like "if (foo + bar < foo) {" */
1661 static int handle_integer_overflow_test(struct expression *expr)
1663 struct expression *left, *right;
1664 struct symbol *type;
1665 sval_t left_min, right_min, min, max;
1667 if (expr->op != '<' && expr->op != SPECIAL_UNSIGNED_LT)
1668 return 0;
1670 left = strip_parens(expr->left);
1671 right = strip_parens(expr->right);
1673 if (left->op != '+')
1674 return 0;
1676 type = get_type(expr);
1677 if (!type)
1678 return 0;
1679 if (type_positive_bits(type) == 32) {
1680 max.type = &uint_ctype;
1681 max.uvalue = (unsigned int)-1;
1682 } else if (type_positive_bits(type) == 64) {
1683 max.type = &ulong_ctype;
1684 max.value = (unsigned long long)-1;
1685 } else {
1686 return 0;
1689 if (!expr_equiv(left->left, right) && !expr_equiv(left->right, right))
1690 return 0;
1692 get_absolute_min(left->left, &left_min);
1693 get_absolute_min(left->right, &right_min);
1694 min = sval_binop(left_min, '+', right_min);
1696 set_extra_chunk_true_false(left, NULL, alloc_estate_range(min, max));
1697 return 1;
1700 static void match_comparison(struct expression *expr)
1702 struct expression *left_orig = strip_parens(expr->left);
1703 struct expression *right_orig = strip_parens(expr->right);
1704 struct expression *left, *right;
1705 struct expression *prev;
1706 struct symbol *type;
1708 if (match_func_comparison(expr))
1709 return;
1711 type = get_type(expr);
1712 if (!type)
1713 type = &llong_ctype;
1715 if (handle_integer_overflow_test(expr))
1716 return;
1718 left = left_orig;
1719 right = right_orig;
1720 move_known_values(&left, &right);
1721 handle_comparison(type, left, expr->op, right);
1723 left = left_orig;
1724 right = right_orig;
1725 if (do_simple_algebra(&left, &right))
1726 handle_comparison(type, left, expr->op, right);
1728 prev = get_assigned_expr(left_orig);
1729 if (is_simple_math(prev) && has_variable(prev, left_orig) == 0) {
1730 left = prev;
1731 right = right_orig;
1732 move_known_values(&left, &right);
1733 handle_comparison(type, left, expr->op, right);
1736 prev = get_assigned_expr(right_orig);
1737 if (is_simple_math(prev) && has_variable(prev, right_orig) == 0) {
1738 left = left_orig;
1739 right = prev;
1740 move_known_values(&left, &right);
1741 handle_comparison(type, left, expr->op, right);
1745 static sval_t get_high_mask(sval_t known)
1747 sval_t ret;
1748 int i;
1750 ret = known;
1751 ret.value = 0;
1753 for (i = type_bits(known.type) - 1; i >= 0; i--) {
1754 if (known.uvalue & (1ULL << i))
1755 ret.uvalue |= (1ULL << i);
1756 else
1757 return ret;
1760 return ret;
1763 static void handle_AND_condition(struct expression *expr)
1765 struct range_list *orig_rl;
1766 struct range_list *true_rl = NULL;
1767 struct range_list *false_rl = NULL;
1768 sval_t known;
1769 int bit;
1771 if (get_implied_value(expr->left, &known)) {
1772 sval_t low_mask = known;
1773 sval_t high_mask;
1775 if (known.value > 0) {
1776 bit = ffsll(known.value) - 1;
1777 low_mask.uvalue = (1ULL << bit) - 1;
1778 get_absolute_rl(expr->right, &orig_rl);
1779 true_rl = remove_range(orig_rl, sval_type_val(known.type, 0), low_mask);
1781 high_mask = get_high_mask(known);
1782 if (high_mask.value) {
1783 bit = ffsll(high_mask.value) - 1;
1784 low_mask.uvalue = (1ULL << bit) - 1;
1786 get_absolute_rl(expr->left, &orig_rl);
1787 if (sval_is_negative(rl_min(orig_rl)))
1788 orig_rl = remove_range(orig_rl, sval_type_min(known.type), sval_type_val(known.type, -1));
1789 false_rl = remove_range(orig_rl, low_mask, sval_type_max(known.type));
1790 if (type_signed(high_mask.type) && type_unsigned(rl_type(false_rl))) {
1791 false_rl = remove_range(false_rl,
1792 sval_type_val(rl_type(false_rl), sval_type_max(known.type).uvalue),
1793 sval_type_val(rl_type(false_rl), -1));
1796 set_extra_expr_true_false(expr->right,
1797 true_rl ? alloc_estate_rl(true_rl) : NULL,
1798 false_rl ? alloc_estate_rl(false_rl) : NULL);
1800 return;
1803 if (get_implied_value(expr->right, &known)) {
1804 sval_t low_mask = known;
1805 sval_t high_mask;
1807 if (known.value > 0) {
1808 bit = ffsll(known.value) - 1;
1809 low_mask.uvalue = (1ULL << bit) - 1;
1810 get_absolute_rl(expr->left, &orig_rl);
1811 true_rl = remove_range(orig_rl, sval_type_val(known.type, 0), low_mask);
1813 high_mask = get_high_mask(known);
1814 if (high_mask.value) {
1815 bit = ffsll(high_mask.value) - 1;
1816 low_mask.uvalue = (1ULL << bit) - 1;
1818 get_absolute_rl(expr->left, &orig_rl);
1819 if (sval_is_negative(rl_min(orig_rl)))
1820 orig_rl = remove_range(orig_rl, sval_type_min(known.type), sval_type_val(known.type, -1));
1821 false_rl = remove_range(orig_rl, low_mask, sval_type_max(known.type));
1822 if (type_signed(high_mask.type) && type_unsigned(rl_type(false_rl))) {
1823 false_rl = remove_range(false_rl,
1824 sval_type_val(rl_type(false_rl), sval_type_max(known.type).uvalue),
1825 sval_type_val(rl_type(false_rl), -1));
1828 set_extra_expr_true_false(expr->left,
1829 true_rl ? alloc_estate_rl(true_rl) : NULL,
1830 false_rl ? alloc_estate_rl(false_rl) : NULL);
1831 return;
1835 static void handle_MOD_condition(struct expression *expr)
1837 struct range_list *orig_rl;
1838 struct range_list *true_rl;
1839 struct range_list *false_rl = NULL;
1840 sval_t right;
1841 sval_t zero = {
1842 .value = 0,
1845 if (!get_implied_value(expr->right, &right) || right.value == 0)
1846 return;
1847 get_absolute_rl(expr->left, &orig_rl);
1849 zero.type = rl_type(orig_rl);
1851 /* We're basically dorking around the min and max here */
1852 true_rl = remove_range(orig_rl, zero, zero);
1853 if (!sval_is_max(rl_max(true_rl)) &&
1854 !(rl_max(true_rl).value % right.value))
1855 true_rl = remove_range(true_rl, rl_max(true_rl), rl_max(true_rl));
1857 if (rl_equiv(true_rl, orig_rl))
1858 true_rl = NULL;
1860 if (sval_is_positive(rl_min(orig_rl)) &&
1861 (rl_max(orig_rl).value - rl_min(orig_rl).value) / right.value < 5) {
1862 sval_t add;
1863 int i;
1865 add = rl_min(orig_rl);
1866 add.value += right.value - (add.value % right.value);
1867 add.value -= right.value;
1869 for (i = 0; i < 5; i++) {
1870 add.value += right.value;
1871 if (add.value > rl_max(orig_rl).value)
1872 break;
1873 add_range(&false_rl, add, add);
1875 } else {
1876 if (rl_min(orig_rl).uvalue != 0 &&
1877 rl_min(orig_rl).uvalue < right.uvalue) {
1878 sval_t chop = right;
1879 chop.value--;
1880 false_rl = remove_range(orig_rl, zero, chop);
1883 if (!sval_is_max(rl_max(orig_rl)) &&
1884 (rl_max(orig_rl).value % right.value)) {
1885 sval_t chop = rl_max(orig_rl);
1886 chop.value -= chop.value % right.value;
1887 chop.value++;
1888 if (!false_rl)
1889 false_rl = clone_rl(orig_rl);
1890 false_rl = remove_range(false_rl, chop, rl_max(orig_rl));
1894 set_extra_expr_true_false(expr->left,
1895 true_rl ? alloc_estate_rl(true_rl) : NULL,
1896 false_rl ? alloc_estate_rl(false_rl) : NULL);
1899 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
1900 void __extra_match_condition(struct expression *expr)
1902 struct smatch_state *pre_state;
1903 struct smatch_state *true_state;
1904 struct smatch_state *false_state;
1905 struct range_list *pre_rl;
1907 expr = strip_expr(expr);
1908 switch (expr->type) {
1909 case EXPR_CALL:
1910 function_comparison(expr, SPECIAL_NOTEQUAL, zero_expr());
1911 return;
1912 case EXPR_PREOP:
1913 case EXPR_SYMBOL:
1914 case EXPR_DEREF: {
1915 sval_t zero;
1917 zero = sval_blank(expr);
1918 zero.value = 0;
1920 pre_state = get_extra_state(expr);
1921 if (estate_is_empty(pre_state))
1922 return;
1923 if (pre_state)
1924 pre_rl = estate_rl(pre_state);
1925 else
1926 get_absolute_rl(expr, &pre_rl);
1927 if (possibly_true_rl(pre_rl, SPECIAL_EQUAL, rl_zero()))
1928 false_state = alloc_estate_sval(zero);
1929 else
1930 false_state = alloc_estate_empty();
1931 true_state = alloc_estate_rl(remove_range(pre_rl, zero, zero));
1932 set_extra_expr_true_false(expr, true_state, false_state);
1933 return;
1935 case EXPR_COMPARE:
1936 match_comparison(expr);
1937 return;
1938 case EXPR_ASSIGNMENT:
1939 __extra_match_condition(expr->left);
1940 return;
1941 case EXPR_BINOP:
1942 if (expr->op == '&')
1943 handle_AND_condition(expr);
1944 if (expr->op == '%')
1945 handle_MOD_condition(expr);
1946 return;
1950 static void assume_indexes_are_valid(struct expression *expr)
1952 struct expression *array_expr;
1953 int array_size;
1954 struct expression *offset;
1955 struct symbol *offset_type;
1956 struct range_list *rl_before;
1957 struct range_list *rl_after;
1958 struct range_list *filter = NULL;
1959 sval_t size;
1961 expr = strip_expr(expr);
1962 if (!is_array(expr))
1963 return;
1965 offset = get_array_offset(expr);
1966 offset_type = get_type(offset);
1967 if (offset_type && type_signed(offset_type)) {
1968 filter = alloc_rl(sval_type_min(offset_type),
1969 sval_type_val(offset_type, -1));
1972 array_expr = get_array_base(expr);
1973 array_size = get_real_array_size(array_expr);
1974 if (array_size > 1) {
1975 size = sval_type_val(offset_type, array_size);
1976 add_range(&filter, size, sval_type_max(offset_type));
1979 if (!filter)
1980 return;
1981 get_absolute_rl(offset, &rl_before);
1982 rl_after = rl_filter(rl_before, filter);
1983 if (rl_equiv(rl_before, rl_after))
1984 return;
1985 set_extra_expr_nomod(offset, alloc_estate_rl(rl_after));
1988 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
1989 int implied_not_equal(struct expression *expr, long long val)
1991 return !possibly_false(expr, SPECIAL_NOTEQUAL, value_expr(val));
1994 int implied_not_equal_name_sym(char *name, struct symbol *sym, long long val)
1996 struct smatch_state *estate;
1998 estate = get_state(SMATCH_EXTRA, name, sym);
1999 if (!estate)
2000 return 0;
2001 if (!rl_has_sval(estate_rl(estate), sval_type_val(estate_type(estate), 0)))
2002 return 1;
2003 return 0;
2006 int parent_is_null_var_sym(const char *name, struct symbol *sym)
2008 char buf[256];
2009 char *start;
2010 char *end;
2011 struct smatch_state *state;
2013 strncpy(buf, name, sizeof(buf) - 1);
2014 buf[sizeof(buf) - 1] = '\0';
2016 start = &buf[0];
2017 while (*start == '*') {
2018 start++;
2019 state = get_state(SMATCH_EXTRA, start, sym);
2020 if (!state)
2021 continue;
2022 if (!estate_rl(state))
2023 return 1;
2024 if (estate_min(state).value == 0 &&
2025 estate_max(state).value == 0)
2026 return 1;
2029 start = &buf[0];
2030 while (*start == '&')
2031 start++;
2033 while ((end = strrchr(start, '-'))) {
2034 *end = '\0';
2035 state = __get_state(SMATCH_EXTRA, start, sym);
2036 if (!state)
2037 continue;
2038 if (estate_min(state).value == 0 &&
2039 estate_max(state).value == 0)
2040 return 1;
2042 return 0;
2045 int parent_is_null(struct expression *expr)
2047 struct symbol *sym;
2048 char *var;
2049 int ret = 0;
2051 expr = strip_expr(expr);
2052 var = expr_to_var_sym(expr, &sym);
2053 if (!var || !sym)
2054 goto free;
2055 ret = parent_is_null_var_sym(var, sym);
2056 free:
2057 free_string(var);
2058 return ret;
2061 static int param_used_callback(void *found, int argc, char **argv, char **azColName)
2063 *(int *)found = 1;
2064 return 0;
2067 static int filter_unused_kzalloc_info(struct expression *call, int param, char *printed_name, struct sm_state *sm)
2069 sval_t sval;
2070 int found = 0;
2072 /* for function pointers assume everything is used */
2073 if (call->fn->type != EXPR_SYMBOL)
2074 return 0;
2077 * kzalloc() information is treated as special because so there is just
2078 * a lot of stuff initialized to zero and it makes building the database
2079 * take hours and hours.
2081 * In theory, we should just remove this line and not pass any unused
2082 * information, but I'm not sure enough that this code works so I want
2083 * to hold off on that for now.
2085 if (!estate_get_single_value(sm->state, &sval) || sval.value != 0)
2086 return 0;
2088 run_sql(&param_used_callback, &found,
2089 "select * from call_implies where %s and type = %d and parameter = %d and key = '%s';",
2090 get_static_filter(call->fn->symbol), PARAM_USED, param, printed_name);
2091 if (found)
2092 return 0;
2094 /* If the database is not built yet, then assume everything is used */
2095 run_sql(&param_used_callback, &found,
2096 "select * from call_implies where %s and type = %d;",
2097 get_static_filter(call->fn->symbol), PARAM_USED);
2098 if (!found)
2099 return 0;
2101 return 1;
2104 struct range_list *intersect_with_real_abs_var_sym(const char *name, struct symbol *sym, struct range_list *start)
2106 struct smatch_state *state;
2109 * Here is the difference between implied value and real absolute, say
2110 * you have:
2112 * int a = (u8)x;
2114 * Then you know that a is 0-255. That's real absolute. But you don't
2115 * know for sure that it actually goes up to 255. So it's not implied.
2116 * Implied indicates a degree of certainty.
2118 * But then say you cap "a" at 8. That means you know it goes up to
2119 * 8. So now the implied value is s32min-8. But you can combine it
2120 * with the real absolute to say that actually it's 0-8.
2122 * We are combining it here. But now that I think about it, this is
2123 * probably not the ideal place to combine it because it should proably
2124 * be done earlier. Oh well, this is an improvement on what was there
2125 * before so I'm going to commit this code.
2129 state = get_real_absolute_state_var_sym(name, sym);
2130 if (!state || !estate_rl(state))
2131 return start;
2133 return rl_intersection(estate_rl(state), start);
2136 struct range_list *intersect_with_real_abs_expr(struct expression *expr, struct range_list *start)
2138 struct smatch_state *state;
2140 state = get_real_absolute_state(expr);
2141 if (!state || !estate_rl(state))
2142 return start;
2144 return rl_intersection(estate_rl(state), start);
2147 static void struct_member_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
2149 struct range_list *rl;
2151 if (estate_is_whole(sm->state))
2152 return;
2153 if (filter_unused_kzalloc_info(call, param, printed_name, sm))
2154 return;
2155 rl = estate_rl(sm->state);
2156 rl = intersect_with_real_abs_var_sym(sm->name, sm->sym, rl);
2157 sql_insert_caller_info(call, PARAM_VALUE, param, printed_name, show_rl(rl));
2158 if (estate_has_fuzzy_max(sm->state))
2159 sql_insert_caller_info(call, FUZZY_MAX, param, printed_name,
2160 sval_to_str(estate_get_fuzzy_max(sm->state)));
2163 static void returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
2165 struct symbol *returned_sym;
2166 struct sm_state *sm;
2167 const char *param_name;
2168 char *compare_str;
2169 char buf[256];
2171 returned_sym = expr_to_sym(expr);
2172 if (!returned_sym)
2173 return;
2175 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
2176 if (!estate_rl(sm->state))
2177 continue;
2178 if (returned_sym != sm->sym)
2179 continue;
2181 param_name = get_param_name(sm);
2182 if (!param_name)
2183 continue;
2184 if (strcmp(param_name, "$") == 0)
2185 continue;
2186 compare_str = name_sym_to_param_comparison(sm->name, sm->sym);
2187 if (!compare_str && estate_is_whole(sm->state))
2188 continue;
2189 snprintf(buf, sizeof(buf), "%s%s", sm->state->name, compare_str ?: "");
2191 sql_insert_return_states(return_id, return_ranges, PARAM_VALUE,
2192 -1, param_name, buf);
2193 } END_FOR_EACH_SM(sm);
2196 static void db_limited_before(void)
2198 unmatched_stree = clone_stree(__get_cur_stree());
2201 static void db_limited_after(void)
2203 free_stree(&unmatched_stree);
2206 static int rl_fits_in_type(struct range_list *rl, struct symbol *type)
2208 if (type_bits(rl_type(rl)) <= type_bits(type))
2209 return 1;
2210 if (sval_cmp(rl_max(rl), sval_type_max(type)) > 0)
2211 return 0;
2212 if (sval_is_negative(rl_min(rl)) &&
2213 sval_cmp(rl_min(rl), sval_type_min(type)) < 0)
2214 return 0;
2215 return 1;
2218 static int basically_the_same(struct range_list *orig, struct range_list *new)
2220 if (rl_equiv(orig, new))
2221 return 1;
2224 * The whole range is essentially the same as 0,4096-27777777777 so
2225 * don't overwrite the implications just to store that.
2228 if (rl_type(orig)->type == SYM_PTR &&
2229 is_whole_rl(orig) &&
2230 rl_min(new).value == 0 &&
2231 rl_max(new).value == valid_ptr_max)
2232 return 1;
2233 return 0;
2236 static void db_param_limit_filter(struct expression *expr, int param, char *key, char *value, enum info_type op)
2238 struct expression *arg;
2239 char *name;
2240 struct symbol *sym;
2241 struct var_sym_list *vsl = NULL;
2242 struct sm_state *sm;
2243 struct symbol *compare_type, *var_type;
2244 struct range_list *rl;
2245 struct range_list *limit;
2246 struct range_list *new;
2248 while (expr->type == EXPR_ASSIGNMENT)
2249 expr = strip_expr(expr->right);
2250 if (expr->type != EXPR_CALL)
2251 return;
2253 arg = get_argument_from_call_expr(expr->args, param);
2254 if (!arg)
2255 return;
2257 name = get_chunk_from_key(arg, key, &sym, &vsl);
2258 if (!name)
2259 return;
2260 if (op != PARAM_LIMIT && !sym)
2261 goto free;
2263 if (strcmp(key, "$") == 0)
2264 compare_type = get_arg_type(expr->fn, param);
2265 else
2266 compare_type = get_member_type_from_key(arg, key);
2268 sm = get_sm_state(SMATCH_EXTRA, name, sym);
2269 if (sm)
2270 rl = estate_rl(sm->state);
2271 else
2272 rl = alloc_whole_rl(compare_type);
2274 if (op == PARAM_LIMIT && !rl_fits_in_type(rl, compare_type))
2275 goto free;
2277 call_results_to_rl(expr, compare_type, value, &limit);
2278 new = rl_intersection(rl, limit);
2280 var_type = get_member_type_from_key(arg, key);
2281 new = cast_rl(var_type, new);
2283 /* We want to preserve the implications here */
2284 if (sm && basically_the_same(estate_rl(sm->state), new))
2285 __set_sm(sm); /* FIXME: Is this really necessary? */
2286 else {
2287 char *tmp_name;
2288 struct symbol *tmp_sym;
2290 tmp_name = map_long_to_short_name_sym(name, sym, &tmp_sym);
2291 if (tmp_name && tmp_sym) {
2292 free_string(name);
2293 name = tmp_name;
2294 sym = tmp_sym;
2297 if (op == PARAM_LIMIT)
2298 set_extra_nomod_vsl(name, sym, vsl, NULL, alloc_estate_rl(new));
2299 else
2300 set_extra_mod(name, sym, NULL, alloc_estate_rl(new));
2303 free:
2304 free_string(name);
2307 static void db_param_limit(struct expression *expr, int param, char *key, char *value)
2309 db_param_limit_filter(expr, param, key, value, PARAM_LIMIT);
2312 static void db_param_filter(struct expression *expr, int param, char *key, char *value)
2314 db_param_limit_filter(expr, param, key, value, PARAM_FILTER);
2317 static void db_param_add_set(struct expression *expr, int param, char *key, char *value, enum info_type op)
2319 struct expression *arg;
2320 char *name, *tmp_name;
2321 struct symbol *sym, *tmp_sym;
2322 struct symbol *type;
2323 struct smatch_state *state;
2324 struct range_list *new = NULL;
2325 struct range_list *added = NULL;
2327 while (expr->type == EXPR_ASSIGNMENT)
2328 expr = strip_expr(expr->right);
2329 if (expr->type != EXPR_CALL)
2330 return;
2332 arg = get_argument_from_call_expr(expr->args, param);
2333 if (!arg)
2334 return;
2335 type = get_member_type_from_key(arg, key);
2336 name = get_variable_from_key(arg, key, &sym);
2337 if (!name || !sym)
2338 goto free;
2340 state = get_state(SMATCH_EXTRA, name, sym);
2341 if (state)
2342 new = estate_rl(state);
2344 call_results_to_rl(expr, type, value, &added);
2346 if (op == PARAM_SET)
2347 new = added;
2348 else
2349 new = rl_union(new, added);
2351 tmp_name = map_long_to_short_name_sym_nostack(name, sym, &tmp_sym);
2352 if (tmp_name && tmp_sym) {
2353 free_string(name);
2354 name = tmp_name;
2355 sym = tmp_sym;
2357 set_extra_mod(name, sym, NULL, alloc_estate_rl(new));
2358 free:
2359 free_string(name);
2362 static void db_param_add(struct expression *expr, int param, char *key, char *value)
2364 in_param_set = true;
2365 db_param_add_set(expr, param, key, value, PARAM_ADD);
2366 in_param_set = false;
2369 static void db_param_set(struct expression *expr, int param, char *key, char *value)
2371 in_param_set = true;
2372 db_param_add_set(expr, param, key, value, PARAM_SET);
2373 in_param_set = false;
2376 static void db_param_value(struct expression *expr, int param, char *key, char *value)
2378 struct expression *call;
2379 char *name;
2380 struct symbol *sym;
2381 struct symbol *type;
2382 struct range_list *rl = NULL;
2384 if (param != -1)
2385 return;
2387 call = expr;
2388 while (call->type == EXPR_ASSIGNMENT)
2389 call = strip_expr(call->right);
2390 if (call->type != EXPR_CALL)
2391 return;
2393 type = get_member_type_from_key(expr->left, key);
2394 name = get_variable_from_key(expr->left, key, &sym);
2395 if (!name || !sym)
2396 goto free;
2398 call_results_to_rl(call, type, value, &rl);
2400 set_extra_mod(name, sym, NULL, alloc_estate_rl(rl));
2401 free:
2402 free_string(name);
2405 static void match_call_info(struct expression *expr)
2407 struct smatch_state *state;
2408 struct range_list *rl = NULL;
2409 struct expression *arg;
2410 struct symbol *type;
2411 int i = 0;
2413 FOR_EACH_PTR(expr->args, arg) {
2414 type = get_arg_type(expr->fn, i);
2416 if (get_implied_rl(arg, &rl))
2417 rl = cast_rl(type, rl);
2418 else
2419 rl = cast_rl(type, alloc_whole_rl(get_type(arg)));
2421 if (!is_whole_rl(rl)) {
2422 rl = intersect_with_real_abs_expr(arg, rl);
2423 sql_insert_caller_info(expr, PARAM_VALUE, i, "$", show_rl(rl));
2425 state = get_state_expr(SMATCH_EXTRA, arg);
2426 if (estate_has_fuzzy_max(state)) {
2427 sql_insert_caller_info(expr, FUZZY_MAX, i, "$",
2428 sval_to_str(estate_get_fuzzy_max(state)));
2430 i++;
2431 } END_FOR_EACH_PTR(arg);
2434 static void set_param_value(const char *name, struct symbol *sym, char *key, char *value)
2436 struct range_list *rl = NULL;
2437 struct smatch_state *state;
2438 struct symbol *type;
2439 char fullname[256];
2440 sval_t dummy;
2442 if (strcmp(key, "*$") == 0)
2443 snprintf(fullname, sizeof(fullname), "*%s", name);
2444 else if (strncmp(key, "$", 1) == 0)
2445 snprintf(fullname, 256, "%s%s", name, key + 1);
2446 else
2447 return;
2449 type = get_member_type_from_key(symbol_expression(sym), key);
2450 str_to_rl(type, value, &rl);
2451 state = alloc_estate_rl(rl);
2452 if (estate_get_single_value(state, &dummy))
2453 estate_set_hard_max(state);
2454 set_state(SMATCH_EXTRA, fullname, sym, state);
2457 static void set_param_hard_max(const char *name, struct symbol *sym, char *key, char *value)
2459 struct range_list *rl = NULL;
2460 struct smatch_state *state;
2461 struct symbol *type;
2462 char fullname[256];
2463 sval_t max;
2465 if (strcmp(key, "*$") == 0)
2466 snprintf(fullname, sizeof(fullname), "*%s", name);
2467 else if (strncmp(key, "$", 1) == 0)
2468 snprintf(fullname, 256, "%s%s", name, key + 1);
2469 else
2470 return;
2472 state = get_state(SMATCH_EXTRA, fullname, sym);
2473 if (!state)
2474 return;
2475 type = get_member_type_from_key(symbol_expression(sym), key);
2476 str_to_rl(type, value, &rl);
2477 if (!rl_to_sval(rl, &max))
2478 return;
2479 estate_set_fuzzy_max(state, max);
2482 struct smatch_state *get_extra_state(struct expression *expr)
2484 char *name;
2485 struct symbol *sym;
2486 struct smatch_state *ret = NULL;
2487 struct range_list *rl;
2489 if (is_pointer(expr) && get_address_rl(expr, &rl))
2490 return alloc_estate_rl(rl);
2492 name = expr_to_known_chunk_sym(expr, &sym);
2493 if (!name)
2494 goto free;
2496 ret = get_state(SMATCH_EXTRA, name, sym);
2497 free:
2498 free_string(name);
2499 return ret;
2502 void register_smatch_extra(int id)
2504 my_id = id;
2506 add_merge_hook(my_id, &merge_estates);
2507 add_unmatched_state_hook(my_id, &unmatched_state);
2508 select_caller_info_hook(set_param_value, PARAM_VALUE);
2509 select_caller_info_hook(set_param_hard_max, FUZZY_MAX);
2510 select_return_states_before(&db_limited_before);
2511 select_return_states_hook(PARAM_LIMIT, &db_param_limit);
2512 select_return_states_hook(PARAM_FILTER, &db_param_filter);
2513 select_return_states_hook(PARAM_ADD, &db_param_add);
2514 select_return_states_hook(PARAM_SET, &db_param_set);
2515 select_return_states_hook(PARAM_VALUE, &db_param_value);
2516 select_return_states_after(&db_limited_after);
2519 static void match_link_modify(struct sm_state *sm, struct expression *mod_expr)
2521 struct var_sym_list *links;
2522 struct var_sym *tmp;
2523 struct smatch_state *state;
2525 links = sm->state->data;
2527 FOR_EACH_PTR(links, tmp) {
2528 if (sm->sym == tmp->sym &&
2529 strcmp(sm->name, tmp->var) == 0)
2530 continue;
2531 state = get_state(SMATCH_EXTRA, tmp->var, tmp->sym);
2532 if (!state)
2533 continue;
2534 set_state(SMATCH_EXTRA, tmp->var, tmp->sym, alloc_estate_whole(estate_type(state)));
2535 } END_FOR_EACH_PTR(tmp);
2536 set_state(link_id, sm->name, sm->sym, &undefined);
2539 void register_smatch_extra_links(int id)
2541 link_id = id;
2544 void register_smatch_extra_late(int id)
2546 add_merge_hook(link_id, &merge_link_states);
2547 add_modification_hook(link_id, &match_link_modify);
2548 add_hook(&match_dereferences, DEREF_HOOK);
2549 add_hook(&match_pointer_as_array, OP_HOOK);
2550 select_call_implies_hook(DEREFERENCE, &set_param_dereferenced);
2551 add_hook(&match_function_call, FUNCTION_CALL_HOOK);
2552 add_hook(&match_assign, ASSIGNMENT_HOOK);
2553 add_hook(&match_assign, GLOBAL_ASSIGNMENT_HOOK);
2554 add_hook(&unop_expr, OP_HOOK);
2555 add_hook(&asm_expr, ASM_HOOK);
2557 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
2558 add_member_info_callback(my_id, struct_member_callback);
2559 add_split_return_callback(&returned_struct_members);
2561 add_hook(&assume_indexes_are_valid, OP_HOOK);