expression: make expr_get_parent_expr/stmt() accept NULL arguments
[smatch.git] / smatch_extra.c
blob36151c749eaa6ba4bd1978d938f2abbcb6b487a0
1 /*
2 * Copyright (C) 2008 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
19 * smatch_extra.c is supposed to track the value of every variable.
23 #define _GNU_SOURCE
24 #include <string.h>
26 #include <stdlib.h>
27 #include <errno.h>
28 #ifndef __USE_ISOC99
29 #define __USE_ISOC99
30 #endif
31 #include <limits.h>
32 #include "parse.h"
33 #include "smatch.h"
34 #include "smatch_slist.h"
35 #include "smatch_extra.h"
37 static int my_id;
38 static int link_id;
40 static void match_link_modify(struct sm_state *sm, struct expression *mod_expr);
42 struct string_list *__ignored_macros = NULL;
43 static int in_warn_on_macro(void)
45 struct statement *stmt;
46 char *tmp;
47 char *macro;
49 stmt = get_current_statement();
50 if (!stmt)
51 return 0;
52 macro = get_macro_name(stmt->pos);
53 if (!macro)
54 return 0;
56 FOR_EACH_PTR(__ignored_macros, tmp) {
57 if (!strcmp(tmp, macro))
58 return 1;
59 } END_FOR_EACH_PTR(tmp);
60 return 0;
63 typedef void (mod_hook)(const char *name, struct symbol *sym, struct smatch_state *state);
64 DECLARE_PTR_LIST(void_fn_list, mod_hook *);
65 static struct void_fn_list *extra_mod_hooks;
66 void add_extra_mod_hook(mod_hook *fn)
68 mod_hook **p = malloc(sizeof(mod_hook *));
69 *p = fn;
70 add_ptr_list(&extra_mod_hooks, p);
73 void call_extra_mod_hooks(const char *name, struct symbol *sym, struct smatch_state *state)
75 mod_hook **fn;
77 FOR_EACH_PTR(extra_mod_hooks, fn) {
78 (*fn)(name, sym, state);
79 } END_FOR_EACH_PTR(fn);
82 static bool in_param_set;
83 static void set_extra_mod_helper(const char *name, struct symbol *sym, struct smatch_state *state)
85 remove_from_equiv(name, sym);
86 call_extra_mod_hooks(name, sym, state);
87 if ((__in_fake_assign || in_param_set) &&
88 estate_is_unknown(state) && !get_state(SMATCH_EXTRA, name, sym))
89 return;
90 set_state(SMATCH_EXTRA, name, sym, state);
93 static char *get_pointed_at(const char *name, struct symbol *sym, struct symbol **new_sym)
95 struct expression *assigned;
97 if (name[0] != '*')
98 return NULL;
99 if (strcmp(name + 1, sym->ident->name) != 0)
100 return NULL;
102 assigned = get_assigned_expr_name_sym(sym->ident->name, sym);
103 if (!assigned)
104 return NULL;
105 assigned = strip_parens(assigned);
106 if (assigned->type != EXPR_PREOP || assigned->op != '&')
107 return NULL;
109 return expr_to_var_sym(assigned->unop, new_sym);
112 char *get_other_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym)
114 struct expression *assigned;
115 char *orig_name = NULL;
116 char buf[256];
117 char *ret = NULL;
118 int skip;
120 *new_sym = NULL;
122 if (!sym || !sym->ident)
123 return NULL;
125 ret = get_pointed_at(name, sym, new_sym);
126 if (ret)
127 return ret;
129 skip = strlen(sym->ident->name);
130 if (name[skip] != '-' || name[skip + 1] != '>')
131 return NULL;
132 skip += 2;
134 assigned = get_assigned_expr_name_sym(sym->ident->name, sym);
135 if (!assigned)
136 return NULL;
137 if (assigned->type == EXPR_CALL)
138 return map_call_to_other_name_sym(name, sym, new_sym);
139 if (assigned->type == EXPR_PREOP || assigned->op == '&') {
141 orig_name = expr_to_var_sym(assigned, new_sym);
142 if (!orig_name || !*new_sym)
143 goto free;
145 snprintf(buf, sizeof(buf), "%s.%s", orig_name + 1, name + skip);
146 ret = alloc_string(buf);
147 free_string(orig_name);
148 return ret;
151 if (assigned->type != EXPR_DEREF)
152 goto free;
154 orig_name = expr_to_var_sym(assigned, new_sym);
155 if (!orig_name || !*new_sym)
156 goto free;
158 snprintf(buf, sizeof(buf), "%s->%s", orig_name, name + skip);
159 ret = alloc_string(buf);
160 free_string(orig_name);
161 return ret;
163 free:
164 free_string(orig_name);
165 return NULL;
168 void set_extra_mod(const char *name, struct symbol *sym, struct smatch_state *state)
170 char *new_name;
171 struct symbol *new_sym;
173 set_extra_mod_helper(name, sym, state);
174 new_name = get_other_name_sym(name, sym, &new_sym);
175 if (new_name && new_sym)
176 set_extra_mod_helper(new_name, new_sym, state);
177 free_string(new_name);
180 static void clear_array_states(struct expression *array)
182 struct sm_state *sm;
184 sm = get_sm_state_expr(link_id, array);
185 if (sm)
186 match_link_modify(sm, NULL);
189 static void set_extra_array_mod(struct expression *expr, struct smatch_state *state)
191 struct expression *array;
192 struct var_sym_list *vsl;
193 struct var_sym *vs;
194 char *name;
195 struct symbol *sym;
197 array = get_array_base(expr);
199 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
200 if (!name || !vsl) {
201 clear_array_states(array);
202 goto free;
205 FOR_EACH_PTR(vsl, vs) {
206 store_link(link_id, vs->var, vs->sym, name, sym);
207 } END_FOR_EACH_PTR(vs);
209 call_extra_mod_hooks(name, sym, state);
210 set_state(SMATCH_EXTRA, name, sym, state);
211 free:
212 free_string(name);
215 void set_extra_expr_mod(struct expression *expr, struct smatch_state *state)
217 struct symbol *sym;
218 char *name;
220 if (is_array(expr)) {
221 set_extra_array_mod(expr, state);
222 return;
225 expr = strip_expr(expr);
226 name = expr_to_var_sym(expr, &sym);
227 if (!name || !sym)
228 goto free;
229 set_extra_mod(name, sym, state);
230 free:
231 free_string(name);
234 void set_extra_nomod(const char *name, struct symbol *sym, struct smatch_state *state)
236 char *new_name;
237 struct symbol *new_sym;
238 struct relation *rel;
239 struct smatch_state *orig_state;
241 orig_state = get_state(SMATCH_EXTRA, name, sym);
243 /* don't save unknown states if leaving it blank is the same */
244 if (!orig_state && estate_is_unknown(state))
245 return;
247 new_name = get_other_name_sym(name, sym, &new_sym);
248 if (new_name && new_sym)
249 set_state(SMATCH_EXTRA, new_name, new_sym, state);
250 free_string(new_name);
252 if (!estate_related(orig_state)) {
253 set_state(SMATCH_EXTRA, name, sym, state);
254 return;
257 set_related(state, estate_related(orig_state));
258 FOR_EACH_PTR(estate_related(orig_state), rel) {
259 struct smatch_state *estate;
261 if (option_debug_related)
262 sm_msg("%s updating related %s to %s", name, rel->name, state->name);
263 estate = get_state(SMATCH_EXTRA, rel->name, rel->sym);
264 if (!estate)
265 continue;
266 set_state(SMATCH_EXTRA, rel->name, rel->sym, clone_estate_cast(estate_type(estate), state));
267 } END_FOR_EACH_PTR(rel);
270 void set_extra_nomod_vsl(const char *name, struct symbol *sym, struct var_sym_list *vsl, struct smatch_state *state)
272 struct var_sym *vs;
274 FOR_EACH_PTR(vsl, vs) {
275 store_link(link_id, vs->var, vs->sym, name, sym);
276 } END_FOR_EACH_PTR(vs);
278 set_extra_nomod(name, sym, state);
282 * This is for return_implies_state() hooks which modify a SMATCH_EXTRA state
284 void set_extra_expr_nomod(struct expression *expr, struct smatch_state *state)
286 struct var_sym_list *vsl;
287 struct var_sym *vs;
288 char *name;
289 struct symbol *sym;
291 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
292 if (!name || !vsl)
293 goto free;
294 FOR_EACH_PTR(vsl, vs) {
295 store_link(link_id, vs->var, vs->sym, name, sym);
296 } END_FOR_EACH_PTR(vs);
298 set_extra_nomod(name, sym, state);
299 free:
300 free_string(name);
303 static void set_extra_true_false(const char *name, struct symbol *sym,
304 struct smatch_state *true_state,
305 struct smatch_state *false_state)
307 char *new_name;
308 struct symbol *new_sym;
309 struct relation *rel;
310 struct smatch_state *orig_state;
312 if (!true_state && !false_state)
313 return;
315 if (in_warn_on_macro())
316 return;
318 new_name = get_other_name_sym(name, sym, &new_sym);
319 if (new_name && new_sym)
320 set_true_false_states(SMATCH_EXTRA, new_name, new_sym, true_state, false_state);
321 free_string(new_name);
323 orig_state = get_state(SMATCH_EXTRA, name, sym);
325 if (!estate_related(orig_state)) {
326 set_true_false_states(SMATCH_EXTRA, name, sym, true_state, false_state);
327 return;
330 if (true_state)
331 set_related(true_state, estate_related(orig_state));
332 if (false_state)
333 set_related(false_state, estate_related(orig_state));
335 FOR_EACH_PTR(estate_related(orig_state), rel) {
336 set_true_false_states(SMATCH_EXTRA, rel->name, rel->sym,
337 true_state, false_state);
338 } END_FOR_EACH_PTR(rel);
341 static void set_extra_chunk_true_false(struct expression *expr,
342 struct smatch_state *true_state,
343 struct smatch_state *false_state)
345 struct var_sym_list *vsl;
346 struct var_sym *vs;
347 struct symbol *type;
348 char *name;
349 struct symbol *sym;
351 if (in_warn_on_macro())
352 return;
354 type = get_type(expr);
355 if (!type)
356 return;
358 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
359 if (!name || !vsl)
360 goto free;
361 FOR_EACH_PTR(vsl, vs) {
362 store_link(link_id, vs->var, vs->sym, name, sym);
363 } END_FOR_EACH_PTR(vs);
365 set_true_false_states(SMATCH_EXTRA, name, sym,
366 clone_estate(true_state),
367 clone_estate(false_state));
368 free:
369 free_string(name);
372 static void set_extra_expr_true_false(struct expression *expr,
373 struct smatch_state *true_state,
374 struct smatch_state *false_state)
376 char *name;
377 struct symbol *sym;
378 sval_t sval;
380 if (!true_state && !false_state)
381 return;
383 if (get_value(expr, &sval))
384 return;
386 expr = strip_expr(expr);
387 name = expr_to_var_sym(expr, &sym);
388 if (!name || !sym) {
389 free_string(name);
390 set_extra_chunk_true_false(expr, true_state, false_state);
391 return;
393 set_extra_true_false(name, sym, true_state, false_state);
394 free_string(name);
397 static struct sm_state *handle_canonical_while_count_down(struct statement *loop)
399 struct expression *iter_var;
400 struct expression *condition;
401 struct sm_state *sm;
402 struct smatch_state *estate;
403 sval_t start;
405 condition = strip_expr(loop->iterator_pre_condition);
406 if (!condition)
407 return NULL;
408 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
409 return NULL;
410 if (condition->op != SPECIAL_DECREMENT)
411 return NULL;
413 iter_var = condition->unop;
414 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
415 if (!sm)
416 return NULL;
417 if (sval_cmp_val(estate_min(sm->state), 0) < 0)
418 return NULL;
419 start = estate_max(sm->state);
420 if (sval_cmp_val(start, 0) <= 0)
421 return NULL;
422 if (!sval_is_max(start))
423 start.value--;
425 if (condition->type == EXPR_PREOP) {
426 estate = alloc_estate_range(sval_type_val(start.type, 1), start);
427 if (estate_has_hard_max(sm->state))
428 estate_set_hard_max(estate);
429 estate_copy_fuzzy_max(estate, sm->state);
430 set_extra_expr_mod(iter_var, estate);
432 if (condition->type == EXPR_POSTOP) {
433 estate = alloc_estate_range(sval_type_val(start.type, 0), start);
434 if (estate_has_hard_max(sm->state))
435 estate_set_hard_max(estate);
436 estate_copy_fuzzy_max(estate, sm->state);
437 set_extra_expr_mod(iter_var, estate);
439 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
442 static struct sm_state *handle_canonical_for_inc(struct expression *iter_expr,
443 struct expression *condition)
445 struct expression *iter_var;
446 struct sm_state *sm;
447 struct smatch_state *estate;
448 sval_t start, end, max;
450 iter_var = iter_expr->unop;
451 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
452 if (!sm)
453 return NULL;
454 if (!estate_get_single_value(sm->state, &start))
455 return NULL;
456 if (get_implied_max(condition->right, &end))
457 end = sval_cast(get_type(iter_var), end);
458 else
459 end = sval_type_max(get_type(iter_var));
461 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
462 return NULL;
464 switch (condition->op) {
465 case SPECIAL_UNSIGNED_LT:
466 case SPECIAL_NOTEQUAL:
467 case '<':
468 if (!sval_is_min(end))
469 end.value--;
470 break;
471 case SPECIAL_UNSIGNED_LTE:
472 case SPECIAL_LTE:
473 break;
474 default:
475 return NULL;
477 if (sval_cmp(end, start) < 0)
478 return NULL;
479 estate = alloc_estate_range(start, end);
480 if (get_hard_max(condition->right, &max)) {
481 estate_set_hard_max(estate);
482 if (condition->op == '<' ||
483 condition->op == SPECIAL_UNSIGNED_LT ||
484 condition->op == SPECIAL_NOTEQUAL)
485 max.value--;
486 estate_set_fuzzy_max(estate, max);
488 set_extra_expr_mod(iter_var, estate);
489 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
492 static struct sm_state *handle_canonical_for_dec(struct expression *iter_expr,
493 struct expression *condition)
495 struct expression *iter_var;
496 struct sm_state *sm;
497 struct smatch_state *estate;
498 sval_t start, end;
500 iter_var = iter_expr->unop;
501 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
502 if (!sm)
503 return NULL;
504 if (!estate_get_single_value(sm->state, &start))
505 return NULL;
506 if (!get_implied_min(condition->right, &end))
507 end = sval_type_min(get_type(iter_var));
508 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
509 return NULL;
511 switch (condition->op) {
512 case SPECIAL_NOTEQUAL:
513 case '>':
514 if (!sval_is_min(end) && !sval_is_max(end))
515 end.value++;
516 break;
517 case SPECIAL_GTE:
518 break;
519 default:
520 return NULL;
522 if (sval_cmp(end, start) > 0)
523 return NULL;
524 estate = alloc_estate_range(end, start);
525 estate_set_hard_max(estate);
526 estate_set_fuzzy_max(estate, estate_get_fuzzy_max(estate));
527 set_extra_expr_mod(iter_var, estate);
528 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
531 static struct sm_state *handle_canonical_for_loops(struct statement *loop)
533 struct expression *iter_expr;
534 struct expression *condition;
536 if (!loop->iterator_post_statement)
537 return NULL;
538 if (loop->iterator_post_statement->type != STMT_EXPRESSION)
539 return NULL;
540 iter_expr = loop->iterator_post_statement->expression;
541 if (!loop->iterator_pre_condition)
542 return NULL;
543 if (loop->iterator_pre_condition->type != EXPR_COMPARE)
544 return NULL;
545 condition = loop->iterator_pre_condition;
547 if (iter_expr->op == SPECIAL_INCREMENT)
548 return handle_canonical_for_inc(iter_expr, condition);
549 if (iter_expr->op == SPECIAL_DECREMENT)
550 return handle_canonical_for_dec(iter_expr, condition);
551 return NULL;
554 struct sm_state *__extra_handle_canonical_loops(struct statement *loop, struct stree **stree)
556 struct sm_state *ret;
558 __push_fake_cur_stree();
559 if (!loop->iterator_post_statement)
560 ret = handle_canonical_while_count_down(loop);
561 else
562 ret = handle_canonical_for_loops(loop);
563 *stree = __pop_fake_cur_stree();
564 return ret;
567 int __iterator_unchanged(struct sm_state *sm)
569 if (!sm)
570 return 0;
571 if (get_sm_state(my_id, sm->name, sm->sym) == sm)
572 return 1;
573 return 0;
576 static void while_count_down_after(struct sm_state *sm, struct expression *condition)
578 sval_t after_value;
580 /* paranoid checking. prolly not needed */
581 condition = strip_expr(condition);
582 if (!condition)
583 return;
584 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
585 return;
586 if (condition->op != SPECIAL_DECREMENT)
587 return;
588 after_value = estate_min(sm->state);
589 after_value.value--;
590 set_extra_mod(sm->name, sm->sym, alloc_estate_sval(after_value));
593 void __extra_pre_loop_hook_after(struct sm_state *sm,
594 struct statement *iterator,
595 struct expression *condition)
597 struct expression *iter_expr;
598 sval_t limit;
599 struct smatch_state *state;
601 if (!iterator) {
602 while_count_down_after(sm, condition);
603 return;
606 iter_expr = iterator->expression;
608 if (condition->type != EXPR_COMPARE)
609 return;
610 if (iter_expr->op == SPECIAL_INCREMENT) {
611 limit = sval_binop(estate_max(sm->state), '+',
612 sval_type_val(estate_type(sm->state), 1));
613 } else {
614 limit = sval_binop(estate_min(sm->state), '-',
615 sval_type_val(estate_type(sm->state), 1));
617 if (!estate_has_hard_max(sm->state) && !__has_breaks()) {
618 if (iter_expr->op == SPECIAL_INCREMENT)
619 state = alloc_estate_range(estate_min(sm->state), limit);
620 else
621 state = alloc_estate_range(limit, estate_max(sm->state));
622 } else {
623 state = alloc_estate_sval(limit);
625 if (!estate_has_hard_max(sm->state)) {
626 estate_clear_hard_max(state);
628 if (estate_has_fuzzy_max(sm->state)) {
629 sval_t hmax = estate_get_fuzzy_max(sm->state);
630 sval_t max = estate_max(sm->state);
632 if (sval_cmp(hmax, max) != 0)
633 estate_clear_fuzzy_max(state);
634 } else if (!estate_has_fuzzy_max(sm->state)) {
635 estate_clear_fuzzy_max(state);
638 set_extra_mod(sm->name, sm->sym, state);
641 static struct stree *unmatched_stree;
642 static struct smatch_state *unmatched_state(struct sm_state *sm)
644 struct smatch_state *state;
646 if (unmatched_stree) {
647 state = get_state_stree(unmatched_stree, SMATCH_EXTRA, sm->name, sm->sym);
648 if (state)
649 return state;
651 if (parent_is_gone_var_sym(sm->name, sm->sym))
652 return alloc_estate_empty();
653 return alloc_estate_whole(estate_type(sm->state));
656 static void clear_the_pointed_at(struct expression *expr)
658 struct stree *stree;
659 char *name;
660 struct symbol *sym;
661 struct sm_state *tmp;
663 name = expr_to_var_sym(expr, &sym);
664 if (!name || !sym)
665 goto free;
667 stree = __get_cur_stree();
668 FOR_EACH_MY_SM(SMATCH_EXTRA, stree, tmp) {
669 if (tmp->name[0] != '*')
670 continue;
671 if (tmp->sym != sym)
672 continue;
673 if (strcmp(tmp->name + 1, name) != 0)
674 continue;
675 set_extra_mod(tmp->name, tmp->sym, alloc_estate_whole(estate_type(tmp->state)));
676 } END_FOR_EACH_SM(tmp);
678 free:
679 free_string(name);
682 static void match_function_call(struct expression *expr)
684 struct expression *arg;
685 struct expression *tmp;
687 /* if we have the db this is handled in smatch_function_hooks.c */
688 if (!option_no_db)
689 return;
690 if (inlinable(expr->fn))
691 return;
693 FOR_EACH_PTR(expr->args, arg) {
694 tmp = strip_expr(arg);
695 if (tmp->type == EXPR_PREOP && tmp->op == '&')
696 set_extra_expr_mod(tmp->unop, alloc_estate_whole(get_type(tmp->unop)));
697 else
698 clear_the_pointed_at(tmp);
699 } END_FOR_EACH_PTR(arg);
702 static int values_fit_type(struct expression *left, struct expression *right)
704 struct range_list *rl;
705 struct symbol *type;
707 type = get_type(left);
708 if (!type)
709 return 0;
710 get_absolute_rl(right, &rl);
711 if (type_unsigned(type) && sval_is_negative(rl_min(rl)))
712 return 0;
713 if (sval_cmp(sval_type_min(type), rl_min(rl)) > 0)
714 return 0;
715 if (sval_cmp(sval_type_max(type), rl_max(rl)) < 0)
716 return 0;
717 return 1;
720 static void save_chunk_info(struct expression *left, struct expression *right)
722 struct var_sym_list *vsl;
723 struct var_sym *vs;
724 struct expression *add_expr;
725 struct symbol *type;
726 sval_t sval;
727 char *name;
728 struct symbol *sym;
730 if (right->type != EXPR_BINOP || right->op != '-')
731 return;
732 if (!get_value(right->left, &sval))
733 return;
734 if (!expr_to_sym(right->right))
735 return;
737 add_expr = binop_expression(left, '+', right->right);
738 type = get_type(add_expr);
739 if (!type)
740 return;
741 name = expr_to_chunk_sym_vsl(add_expr, &sym, &vsl);
742 if (!name || !vsl)
743 goto free;
744 FOR_EACH_PTR(vsl, vs) {
745 store_link(link_id, vs->var, vs->sym, name, sym);
746 } END_FOR_EACH_PTR(vs);
748 set_state(SMATCH_EXTRA, name, sym, alloc_estate_sval(sval_cast(type, sval)));
749 free:
750 free_string(name);
753 static void do_array_assign(struct expression *left, int op, struct expression *right)
755 struct range_list *rl;
757 if (op == '=') {
758 get_absolute_rl(right, &rl);
759 rl = cast_rl(get_type(left), rl);
760 } else {
761 rl = alloc_whole_rl(get_type(left));
764 set_extra_array_mod(left, alloc_estate_rl(rl));
767 static void match_untracked_array(struct expression *call, int param)
769 struct expression *arg;
771 arg = get_argument_from_call_expr(call->args, param);
772 arg = strip_expr(arg);
774 clear_array_states(arg);
777 static void match_vanilla_assign(struct expression *left, struct expression *right)
779 struct range_list *orig_rl = NULL;
780 struct range_list *rl = NULL;
781 struct symbol *right_sym;
782 struct symbol *left_type;
783 struct symbol *right_type;
784 char *right_name = NULL;
785 struct symbol *sym;
786 char *name;
787 sval_t max;
788 struct smatch_state *state;
789 int comparison;
791 if (is_struct(left))
792 return;
794 save_chunk_info(left, right);
796 name = expr_to_var_sym(left, &sym);
797 if (!name) {
798 if (is_array(left))
799 do_array_assign(left, '=', right);
800 return;
803 left_type = get_type(left);
804 right_type = get_type(right);
806 right_name = expr_to_var_sym(right, &right_sym);
808 if (!__in_fake_assign &&
809 !(right->type == EXPR_PREOP && right->op == '&') &&
810 right_name && right_sym &&
811 values_fit_type(left, right) &&
812 !has_symbol(right, sym)) {
813 set_equiv(left, right);
814 goto free;
817 if (is_pointer(right) && get_address_rl(right, &rl)) {
818 state = alloc_estate_rl(rl);
819 goto done;
822 if (__in_fake_assign) {
823 struct smatch_state *right_state;
824 sval_t sval;
826 if (get_value(right, &sval)) {
827 sval = sval_cast(left_type, sval);
828 state = alloc_estate_sval(sval);
829 goto done;
832 right_state = get_state(SMATCH_EXTRA, right_name, right_sym);
833 if (right_state) {
834 /* simple assignment */
835 state = clone_estate(right_state);
836 goto done;
839 state = alloc_estate_rl(alloc_whole_rl(left_type));
840 goto done;
843 comparison = get_comparison(left, right);
844 if (comparison) {
845 comparison = flip_comparison(comparison);
846 get_implied_rl(left, &orig_rl);
849 if (get_implied_rl(right, &rl)) {
850 rl = cast_rl(left_type, rl);
851 if (orig_rl)
852 filter_by_comparison(&rl, comparison, orig_rl);
853 state = alloc_estate_rl(rl);
854 if (get_hard_max(right, &max)) {
855 estate_set_hard_max(state);
856 estate_set_fuzzy_max(state, max);
858 } else {
859 rl = alloc_whole_rl(right_type);
860 rl = cast_rl(left_type, rl);
861 if (orig_rl)
862 filter_by_comparison(&rl, comparison, orig_rl);
863 state = alloc_estate_rl(rl);
866 done:
867 set_extra_mod(name, sym, state);
868 free:
869 free_string(right_name);
872 static int op_remove_assign(int op)
874 switch (op) {
875 case SPECIAL_ADD_ASSIGN:
876 return '+';
877 case SPECIAL_SUB_ASSIGN:
878 return '-';
879 case SPECIAL_MUL_ASSIGN:
880 return '*';
881 case SPECIAL_DIV_ASSIGN:
882 return '/';
883 case SPECIAL_MOD_ASSIGN:
884 return '%';
885 case SPECIAL_AND_ASSIGN:
886 return '&';
887 case SPECIAL_OR_ASSIGN:
888 return '|';
889 case SPECIAL_XOR_ASSIGN:
890 return '^';
891 case SPECIAL_SHL_ASSIGN:
892 return SPECIAL_LEFTSHIFT;
893 case SPECIAL_SHR_ASSIGN:
894 return SPECIAL_RIGHTSHIFT;
895 default:
896 return op;
900 static void match_assign(struct expression *expr)
902 struct range_list *rl = NULL;
903 struct expression *left;
904 struct expression *right;
905 struct expression *binop_expr;
906 struct symbol *left_type;
907 struct symbol *sym;
908 char *name;
909 sval_t left_min, left_max;
910 sval_t right_min, right_max;
911 sval_t res_min, res_max;
913 left = strip_expr(expr->left);
915 right = strip_parens(expr->right);
916 if (right->type == EXPR_CALL && sym_name_is("__builtin_expect", right->fn))
917 right = get_argument_from_call_expr(right->args, 0);
918 while (right->type == EXPR_ASSIGNMENT && right->op == '=')
919 right = strip_parens(right->left);
921 if (expr->op == '=' && is_condition(expr->right))
922 return; /* handled in smatch_condition.c */
923 if (expr->op == '=' && right->type == EXPR_CALL)
924 return; /* handled in smatch_function_hooks.c */
925 if (expr->op == '=') {
926 match_vanilla_assign(left, right);
927 return;
930 name = expr_to_var_sym(left, &sym);
931 if (!name)
932 return;
934 left_type = get_type(left);
936 res_min = sval_type_min(left_type);
937 res_max = sval_type_max(left_type);
939 switch (expr->op) {
940 case SPECIAL_ADD_ASSIGN:
941 get_absolute_max(left, &left_max);
942 get_absolute_max(right, &right_max);
943 if (sval_binop_overflows(left_max, '+', sval_cast(left_type, right_max)))
944 break;
945 if (get_implied_min(left, &left_min) &&
946 !sval_is_negative_min(left_min) &&
947 get_implied_min(right, &right_min) &&
948 !sval_is_negative_min(right_min)) {
949 res_min = sval_binop(left_min, '+', right_min);
950 res_min = sval_cast(left_type, res_min);
952 if (inside_loop()) /* we are assuming loops don't lead to wrapping */
953 break;
954 res_max = sval_binop(left_max, '+', right_max);
955 res_max = sval_cast(left_type, res_max);
956 break;
957 case SPECIAL_SUB_ASSIGN:
958 if (get_implied_max(left, &left_max) &&
959 !sval_is_max(left_max) &&
960 get_implied_min(right, &right_min) &&
961 !sval_is_min(right_min)) {
962 res_max = sval_binop(left_max, '-', right_min);
963 res_max = sval_cast(left_type, res_max);
965 if (inside_loop())
966 break;
967 if (get_implied_min(left, &left_min) &&
968 !sval_is_min(left_min) &&
969 get_implied_max(right, &right_max) &&
970 !sval_is_max(right_max)) {
971 res_min = sval_binop(left_min, '-', right_max);
972 res_min = sval_cast(left_type, res_min);
974 break;
975 case SPECIAL_AND_ASSIGN:
976 case SPECIAL_MOD_ASSIGN:
977 case SPECIAL_SHL_ASSIGN:
978 case SPECIAL_SHR_ASSIGN:
979 case SPECIAL_OR_ASSIGN:
980 case SPECIAL_XOR_ASSIGN:
981 case SPECIAL_MUL_ASSIGN:
982 case SPECIAL_DIV_ASSIGN:
983 binop_expr = binop_expression(expr->left,
984 op_remove_assign(expr->op),
985 expr->right);
986 if (get_absolute_rl(binop_expr, &rl)) {
987 rl = cast_rl(left_type, rl);
988 set_extra_mod(name, sym, alloc_estate_rl(rl));
989 goto free;
991 break;
993 rl = cast_rl(left_type, alloc_rl(res_min, res_max));
994 set_extra_mod(name, sym, alloc_estate_rl(rl));
995 free:
996 free_string(name);
999 static struct smatch_state *increment_state(struct smatch_state *state)
1001 sval_t min = estate_min(state);
1002 sval_t max = estate_max(state);
1004 if (!estate_rl(state))
1005 return NULL;
1007 if (inside_loop())
1008 max = sval_type_max(max.type);
1010 if (!sval_is_min(min) && !sval_is_max(min))
1011 min.value++;
1012 if (!sval_is_min(max) && !sval_is_max(max))
1013 max.value++;
1014 return alloc_estate_range(min, max);
1017 static struct smatch_state *decrement_state(struct smatch_state *state)
1019 sval_t min = estate_min(state);
1020 sval_t max = estate_max(state);
1022 if (!estate_rl(state))
1023 return NULL;
1025 if (inside_loop())
1026 min = sval_type_min(min.type);
1028 if (!sval_is_min(min) && !sval_is_max(min))
1029 min.value--;
1030 if (!sval_is_min(max) && !sval_is_max(max))
1031 max.value--;
1032 return alloc_estate_range(min, max);
1035 static void clear_pointed_at_state(struct expression *expr)
1037 struct symbol *type;
1040 * ALERT: This is sort of a mess. If it's is a struct assigment like
1041 * "foo = bar;", then that's handled by smatch_struct_assignment.c.
1042 * the same thing for p++ where "p" is a struct. Most modifications
1043 * are handled by the assignment hook or the db. Smatch_extra.c doesn't
1044 * use smatch_modification.c because we have to get the ordering right
1045 * or something. So if you have p++ where p is a pointer to a standard
1046 * c type then we handle that here. What a mess.
1049 type = get_type(expr);
1050 if (!type || type->type != SYM_PTR)
1051 return;
1052 type = get_real_base_type(type);
1053 if (!type || type->type != SYM_BASETYPE)
1054 return;
1055 set_extra_expr_mod(deref_expression(expr), alloc_estate_whole(type));
1058 static void unop_expr(struct expression *expr)
1060 struct smatch_state *state;
1062 if (expr->smatch_flags & Handled)
1063 return;
1065 switch (expr->op) {
1066 case SPECIAL_INCREMENT:
1067 state = get_state_expr(SMATCH_EXTRA, expr->unop);
1068 state = increment_state(state);
1069 if (!state)
1070 state = alloc_estate_whole(get_type(expr));
1071 set_extra_expr_mod(expr->unop, state);
1072 clear_pointed_at_state(expr->unop);
1073 break;
1074 case SPECIAL_DECREMENT:
1075 state = get_state_expr(SMATCH_EXTRA, expr->unop);
1076 state = decrement_state(state);
1077 if (!state)
1078 state = alloc_estate_whole(get_type(expr));
1079 set_extra_expr_mod(expr->unop, state);
1080 clear_pointed_at_state(expr->unop);
1081 break;
1082 default:
1083 return;
1087 static void asm_expr(struct statement *stmt)
1090 struct expression *expr;
1091 struct symbol *type;
1092 int state = 0;
1094 FOR_EACH_PTR(stmt->asm_outputs, expr) {
1095 switch (state) {
1096 case 0: /* identifier */
1097 case 1: /* constraint */
1098 state++;
1099 continue;
1100 case 2: /* expression */
1101 state = 0;
1102 type = get_type(strip_expr(expr));
1103 set_extra_expr_mod(expr, alloc_estate_whole(type));
1104 continue;
1106 } END_FOR_EACH_PTR(expr);
1109 static void check_dereference(struct expression *expr)
1111 struct smatch_state *state;
1113 if (__in_fake_assign)
1114 return;
1115 if (outside_of_function())
1116 return;
1117 state = get_extra_state(expr);
1118 if (state) {
1119 static struct range_list *valid_rl;
1120 struct range_list *rl;
1122 if (!valid_rl) {
1123 valid_rl = alloc_rl(valid_ptr_min_sval, valid_ptr_max_sval);
1124 valid_rl = clone_rl_permanent(valid_rl);
1127 rl = rl_intersection(estate_rl(state), valid_rl);
1128 if (rl_equiv(rl, estate_rl(state)))
1129 return;
1130 set_extra_expr_nomod(expr, alloc_estate_rl(rl));
1131 } else {
1132 set_extra_expr_nomod(expr, alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval));
1136 static void match_dereferences(struct expression *expr)
1138 if (expr->type != EXPR_PREOP)
1139 return;
1140 /* it's saying that foo[1] = bar dereferences foo[1] */
1141 if (is_array(expr))
1142 return;
1143 check_dereference(expr->unop);
1146 static void match_pointer_as_array(struct expression *expr)
1148 if (!is_array(expr))
1149 return;
1150 check_dereference(get_array_base(expr));
1153 static void find_dereferences(struct expression *expr)
1155 while (expr->type == EXPR_PREOP) {
1156 if (expr->op == '*')
1157 check_dereference(expr->unop);
1158 expr = strip_expr(expr->unop);
1162 static void set_param_dereferenced(struct expression *arg, char *key, char *unused)
1164 struct symbol *sym;
1165 char *name;
1167 name = get_variable_from_key(arg, key, &sym);
1168 if (name && sym)
1169 set_extra_nomod(name, sym, alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval));
1170 free_string(name);
1172 find_dereferences(arg);
1175 static sval_t add_one(sval_t sval)
1177 sval.value++;
1178 return sval;
1181 static int handle_postop_inc(struct expression *left, int op, struct expression *right)
1183 struct statement *stmt;
1184 struct expression *cond;
1185 struct smatch_state *true_state, *false_state;
1186 sval_t start;
1187 sval_t limit;
1190 * If we're decrementing here then that's a canonical while count down
1191 * so it's handled already. We're only handling loops like:
1192 * i = 0;
1193 * do { ... } while (i++ < 3);
1196 if (left->type != EXPR_POSTOP || left->op != SPECIAL_INCREMENT)
1197 return 0;
1199 stmt = __cur_stmt->parent;
1200 if (!stmt)
1201 return 0;
1202 if (stmt->type == STMT_COMPOUND)
1203 stmt = stmt->parent;
1204 if (!stmt || stmt->type != STMT_ITERATOR || !stmt->iterator_post_condition)
1205 return 0;
1207 cond = strip_expr(stmt->iterator_post_condition);
1208 if (cond->type != EXPR_COMPARE || cond->op != op)
1209 return 0;
1210 if (left != strip_expr(cond->left) || right != strip_expr(cond->right))
1211 return 0;
1213 if (!get_implied_value(left->unop, &start))
1214 return 0;
1215 if (!get_implied_value(right, &limit))
1216 return 0;
1218 if (sval_cmp(start, limit) > 0)
1219 return 0;
1221 switch (op) {
1222 case '<':
1223 case SPECIAL_UNSIGNED_LT:
1224 break;
1225 case SPECIAL_LTE:
1226 case SPECIAL_UNSIGNED_LTE:
1227 limit = add_one(limit);
1228 default:
1229 return 0;
1233 true_state = alloc_estate_range(add_one(start), limit);
1234 false_state = alloc_estate_range(add_one(limit), add_one(limit));
1236 /* Currently we just discard the false state but when two passes is
1237 * implimented correctly then it will use it.
1240 set_extra_expr_true_false(left->unop, true_state, false_state);
1242 return 1;
1245 static void handle_comparison(struct symbol *type, struct expression *left, int op, struct expression *right)
1247 struct range_list *left_orig;
1248 struct range_list *left_true;
1249 struct range_list *left_false;
1250 struct range_list *right_orig;
1251 struct range_list *right_true;
1252 struct range_list *right_false;
1253 struct smatch_state *left_true_state;
1254 struct smatch_state *left_false_state;
1255 struct smatch_state *right_true_state;
1256 struct smatch_state *right_false_state;
1257 sval_t dummy, hard_max;
1258 int left_postop = 0;
1259 int right_postop = 0;
1261 if (left->op == SPECIAL_INCREMENT || left->op == SPECIAL_DECREMENT) {
1262 if (left->type == EXPR_POSTOP) {
1263 left->smatch_flags |= Handled;
1264 left_postop = left->op;
1265 if (handle_postop_inc(left, op, right))
1266 return;
1268 left = strip_parens(left->unop);
1270 while (left->type == EXPR_ASSIGNMENT)
1271 left = strip_parens(left->left);
1273 if (right->op == SPECIAL_INCREMENT || right->op == SPECIAL_DECREMENT) {
1274 if (right->type == EXPR_POSTOP) {
1275 right->smatch_flags |= Handled;
1276 right_postop = right->op;
1278 right = strip_parens(right->unop);
1281 get_real_absolute_rl(left, &left_orig);
1282 left_orig = cast_rl(type, left_orig);
1284 get_real_absolute_rl(right, &right_orig);
1285 right_orig = cast_rl(type, right_orig);
1287 split_comparison_rl(left_orig, op, right_orig, &left_true, &left_false, &right_true, &right_false);
1289 left_true = rl_truncate_cast(get_type(strip_expr(left)), left_true);
1290 left_false = rl_truncate_cast(get_type(strip_expr(left)), left_false);
1291 right_true = rl_truncate_cast(get_type(strip_expr(right)), right_true);
1292 right_false = rl_truncate_cast(get_type(strip_expr(right)), right_false);
1294 if (!left_true || !left_false) {
1295 struct range_list *tmp_true, *tmp_false;
1297 split_comparison_rl(alloc_whole_rl(type), op, right_orig, &tmp_true, &tmp_false, NULL, NULL);
1298 tmp_true = rl_truncate_cast(get_type(strip_expr(left)), tmp_true);
1299 tmp_false = rl_truncate_cast(get_type(strip_expr(left)), tmp_false);
1300 if (tmp_true && tmp_false)
1301 __save_imaginary_state(left, tmp_true, tmp_false);
1304 if (!right_true || !right_false) {
1305 struct range_list *tmp_true, *tmp_false;
1307 split_comparison_rl(alloc_whole_rl(type), op, right_orig, NULL, NULL, &tmp_true, &tmp_false);
1308 tmp_true = rl_truncate_cast(get_type(strip_expr(right)), tmp_true);
1309 tmp_false = rl_truncate_cast(get_type(strip_expr(right)), tmp_false);
1310 if (tmp_true && tmp_false)
1311 __save_imaginary_state(right, tmp_true, tmp_false);
1314 left_true_state = alloc_estate_rl(left_true);
1315 left_false_state = alloc_estate_rl(left_false);
1316 right_true_state = alloc_estate_rl(right_true);
1317 right_false_state = alloc_estate_rl(right_false);
1319 switch (op) {
1320 case '<':
1321 case SPECIAL_UNSIGNED_LT:
1322 case SPECIAL_UNSIGNED_LTE:
1323 case SPECIAL_LTE:
1324 if (get_hard_max(right, &dummy))
1325 estate_set_hard_max(left_true_state);
1326 if (get_hard_max(left, &dummy))
1327 estate_set_hard_max(right_false_state);
1328 break;
1329 case '>':
1330 case SPECIAL_UNSIGNED_GT:
1331 case SPECIAL_UNSIGNED_GTE:
1332 case SPECIAL_GTE:
1333 if (get_hard_max(left, &dummy))
1334 estate_set_hard_max(right_true_state);
1335 if (get_hard_max(right, &dummy))
1336 estate_set_hard_max(left_false_state);
1337 break;
1340 switch (op) {
1341 case '<':
1342 case SPECIAL_UNSIGNED_LT:
1343 case SPECIAL_UNSIGNED_LTE:
1344 case SPECIAL_LTE:
1345 if (get_hard_max(right, &hard_max)) {
1346 if (op == '<' || op == SPECIAL_UNSIGNED_LT)
1347 hard_max.value--;
1348 estate_set_fuzzy_max(left_true_state, hard_max);
1350 if (get_implied_value(right, &hard_max)) {
1351 if (op == SPECIAL_UNSIGNED_LTE ||
1352 op == SPECIAL_LTE)
1353 hard_max.value++;
1354 estate_set_fuzzy_max(left_false_state, hard_max);
1356 if (get_hard_max(left, &hard_max)) {
1357 if (op == SPECIAL_UNSIGNED_LTE ||
1358 op == SPECIAL_LTE)
1359 hard_max.value--;
1360 estate_set_fuzzy_max(right_false_state, hard_max);
1362 if (get_implied_value(left, &hard_max)) {
1363 if (op == '<' || op == SPECIAL_UNSIGNED_LT)
1364 hard_max.value++;
1365 estate_set_fuzzy_max(right_true_state, hard_max);
1367 break;
1368 case '>':
1369 case SPECIAL_UNSIGNED_GT:
1370 case SPECIAL_UNSIGNED_GTE:
1371 case SPECIAL_GTE:
1372 if (get_hard_max(left, &hard_max)) {
1373 if (op == '>' || op == SPECIAL_UNSIGNED_GT)
1374 hard_max.value--;
1375 estate_set_fuzzy_max(right_true_state, hard_max);
1377 if (get_implied_value(left, &hard_max)) {
1378 if (op == SPECIAL_UNSIGNED_GTE ||
1379 op == SPECIAL_GTE)
1380 hard_max.value++;
1381 estate_set_fuzzy_max(right_false_state, hard_max);
1383 if (get_hard_max(right, &hard_max)) {
1384 if (op == SPECIAL_UNSIGNED_LTE ||
1385 op == SPECIAL_LTE)
1386 hard_max.value--;
1387 estate_set_fuzzy_max(left_false_state, hard_max);
1389 if (get_implied_value(right, &hard_max)) {
1390 if (op == '>' ||
1391 op == SPECIAL_UNSIGNED_GT)
1392 hard_max.value++;
1393 estate_set_fuzzy_max(left_true_state, hard_max);
1395 break;
1396 case SPECIAL_EQUAL:
1397 if (get_hard_max(left, &hard_max))
1398 estate_set_fuzzy_max(right_true_state, hard_max);
1399 if (get_hard_max(right, &hard_max))
1400 estate_set_fuzzy_max(left_true_state, hard_max);
1401 break;
1404 if (get_hard_max(left, &hard_max)) {
1405 estate_set_hard_max(left_true_state);
1406 estate_set_hard_max(left_false_state);
1408 if (get_hard_max(right, &hard_max)) {
1409 estate_set_hard_max(right_true_state);
1410 estate_set_hard_max(right_false_state);
1413 if (left_postop == SPECIAL_INCREMENT) {
1414 left_true_state = increment_state(left_true_state);
1415 left_false_state = increment_state(left_false_state);
1417 if (left_postop == SPECIAL_DECREMENT) {
1418 left_true_state = decrement_state(left_true_state);
1419 left_false_state = decrement_state(left_false_state);
1421 if (right_postop == SPECIAL_INCREMENT) {
1422 right_true_state = increment_state(right_true_state);
1423 right_false_state = increment_state(right_false_state);
1425 if (right_postop == SPECIAL_DECREMENT) {
1426 right_true_state = decrement_state(right_true_state);
1427 right_false_state = decrement_state(right_false_state);
1430 if (estate_rl(left_true_state) && estates_equiv(left_true_state, left_false_state)) {
1431 left_true_state = NULL;
1432 left_false_state = NULL;
1435 if (estate_rl(right_true_state) && estates_equiv(right_true_state, right_false_state)) {
1436 right_true_state = NULL;
1437 right_false_state = NULL;
1440 set_extra_expr_true_false(left, left_true_state, left_false_state);
1441 set_extra_expr_true_false(right, right_true_state, right_false_state);
1444 static int is_simple_math(struct expression *expr)
1446 if (!expr)
1447 return 0;
1448 if (expr->type != EXPR_BINOP)
1449 return 0;
1450 switch (expr->op) {
1451 case '+':
1452 case '-':
1453 case '*':
1454 return 1;
1456 return 0;
1459 static void move_known_values(struct expression **left_p, struct expression **right_p)
1461 struct expression *left = *left_p;
1462 struct expression *right = *right_p;
1463 sval_t sval, dummy;
1465 if (get_implied_value(left, &sval)) {
1466 if (!is_simple_math(right))
1467 return;
1468 if (get_implied_value(right, &dummy))
1469 return;
1470 if (right->op == '*') {
1471 sval_t divisor;
1473 if (!get_value(right->right, &divisor))
1474 return;
1475 if (divisor.value == 0 && sval.value % divisor.value)
1476 return;
1477 *left_p = binop_expression(left, invert_op(right->op), right->right);
1478 *right_p = right->left;
1479 return;
1481 if (right->op == '+' && get_value(right->left, &sval)) {
1482 *left_p = binop_expression(left, invert_op(right->op), right->left);
1483 *right_p = right->right;
1484 return;
1486 if (get_value(right->right, &sval)) {
1487 *left_p = binop_expression(left, invert_op(right->op), right->right);
1488 *right_p = right->left;
1489 return;
1491 return;
1493 if (get_implied_value(right, &sval)) {
1494 if (!is_simple_math(left))
1495 return;
1496 if (get_implied_value(left, &dummy))
1497 return;
1498 if (left->op == '*') {
1499 sval_t divisor;
1501 if (!get_value(left->right, &divisor))
1502 return;
1503 if (divisor.value == 0 && sval.value % divisor.value)
1504 return;
1505 *right_p = binop_expression(right, invert_op(left->op), left->right);
1506 *left_p = left->left;
1507 return;
1509 if (left->op == '+' && get_value(left->left, &sval)) {
1510 *right_p = binop_expression(right, invert_op(left->op), left->left);
1511 *left_p = left->right;
1512 return;
1515 if (get_value(left->right, &sval)) {
1516 *right_p = binop_expression(right, invert_op(left->op), left->right);
1517 *left_p = left->left;
1518 return;
1520 return;
1525 * The reason for do_simple_algebra() is to solve things like:
1526 * if (foo > 66 || foo + bar > 64) {
1527 * "foo" is not really a known variable so it won't be handled by
1528 * move_known_variables() but it's a super common idiom.
1531 static int do_simple_algebra(struct expression **left_p, struct expression **right_p)
1533 struct expression *left = *left_p;
1534 struct expression *right = *right_p;
1535 struct range_list *rl;
1536 sval_t tmp;
1538 if (left->type != EXPR_BINOP || left->op != '+')
1539 return 0;
1540 if (can_integer_overflow(get_type(left), left))
1541 return 0;
1542 if (!get_implied_value(right, &tmp))
1543 return 0;
1545 if (!get_implied_value(left->left, &tmp) &&
1546 get_implied_rl(left->left, &rl) &&
1547 !is_whole_rl(rl)) {
1548 *right_p = binop_expression(right, '-', left->left);
1549 *left_p = left->right;
1550 return 1;
1552 if (!get_implied_value(left->right, &tmp) &&
1553 get_implied_rl(left->right, &rl) &&
1554 !is_whole_rl(rl)) {
1555 *right_p = binop_expression(right, '-', left->right);
1556 *left_p = left->left;
1557 return 1;
1560 return 0;
1563 static int match_func_comparison(struct expression *expr)
1565 struct expression *left = strip_expr(expr->left);
1566 struct expression *right = strip_expr(expr->right);
1567 sval_t sval;
1570 * fixme: think about this harder. We should always be trying to limit
1571 * the non-call side as well. If we can't determine the limitter does
1572 * that mean we aren't querying the database and are missing important
1573 * information?
1576 if (left->type == EXPR_CALL) {
1577 if (get_implied_value(left, &sval)) {
1578 handle_comparison(get_type(expr), left, expr->op, right);
1579 return 1;
1581 function_comparison(left, expr->op, right);
1582 return 1;
1585 if (right->type == EXPR_CALL) {
1586 if (get_implied_value(right, &sval)) {
1587 handle_comparison(get_type(expr), left, expr->op, right);
1588 return 1;
1590 function_comparison(left, expr->op, right);
1591 return 1;
1594 return 0;
1597 /* Handle conditions like "if (foo + bar < foo) {" */
1598 static int handle_integer_overflow_test(struct expression *expr)
1600 struct expression *left, *right;
1601 struct symbol *type;
1602 sval_t left_min, right_min, min, max;
1604 if (expr->op != '<' && expr->op != SPECIAL_UNSIGNED_LT)
1605 return 0;
1607 left = strip_parens(expr->left);
1608 right = strip_parens(expr->right);
1610 if (left->op != '+')
1611 return 0;
1613 type = get_type(expr);
1614 if (!type)
1615 return 0;
1616 if (type_positive_bits(type) == 32) {
1617 max.type = &uint_ctype;
1618 max.uvalue = (unsigned int)-1;
1619 } else if (type_positive_bits(type) == 64) {
1620 max.type = &ulong_ctype;
1621 max.value = (unsigned long long)-1;
1622 } else {
1623 return 0;
1626 if (!expr_equiv(left->left, right) && !expr_equiv(left->right, right))
1627 return 0;
1629 get_absolute_min(left->left, &left_min);
1630 get_absolute_min(left->right, &right_min);
1631 min = sval_binop(left_min, '+', right_min);
1633 set_extra_chunk_true_false(left, NULL, alloc_estate_range(min, max));
1634 return 1;
1637 static void match_comparison(struct expression *expr)
1639 struct expression *left_orig = strip_parens(expr->left);
1640 struct expression *right_orig = strip_parens(expr->right);
1641 struct expression *left, *right;
1642 struct expression *prev;
1643 struct symbol *type;
1645 if (match_func_comparison(expr))
1646 return;
1648 type = get_type(expr);
1649 if (!type)
1650 type = &llong_ctype;
1652 if (handle_integer_overflow_test(expr))
1653 return;
1655 left = left_orig;
1656 right = right_orig;
1657 move_known_values(&left, &right);
1658 handle_comparison(type, left, expr->op, right);
1660 left = left_orig;
1661 right = right_orig;
1662 if (do_simple_algebra(&left, &right))
1663 handle_comparison(type, left, expr->op, right);
1665 prev = get_assigned_expr(left_orig);
1666 if (is_simple_math(prev) && has_variable(prev, left_orig) == 0) {
1667 left = prev;
1668 right = right_orig;
1669 move_known_values(&left, &right);
1670 handle_comparison(type, left, expr->op, right);
1673 prev = get_assigned_expr(right_orig);
1674 if (is_simple_math(prev) && has_variable(prev, right_orig) == 0) {
1675 left = left_orig;
1676 right = prev;
1677 move_known_values(&left, &right);
1678 handle_comparison(type, left, expr->op, right);
1682 static sval_t get_high_mask(sval_t known)
1684 sval_t ret;
1685 int i;
1687 ret = known;
1688 ret.value = 0;
1690 for (i = type_bits(known.type) - 1; i >= 0; i--) {
1691 if (known.uvalue & (1ULL << i))
1692 ret.uvalue |= (1ULL << i);
1693 else
1694 return ret;
1697 return ret;
1700 static void handle_AND_condition(struct expression *expr)
1702 struct range_list *orig_rl;
1703 struct range_list *true_rl = NULL;
1704 struct range_list *false_rl = NULL;
1705 sval_t known;
1706 int bit;
1708 if (get_implied_value(expr->left, &known)) {
1709 sval_t low_mask = known;
1710 sval_t high_mask;
1712 if (known.value > 0) {
1713 bit = ffsll(known.value) - 1;
1714 low_mask.uvalue = (1ULL << bit) - 1;
1715 get_absolute_rl(expr->right, &orig_rl);
1716 true_rl = remove_range(orig_rl, sval_type_val(known.type, 0), low_mask);
1718 high_mask = get_high_mask(known);
1719 if (high_mask.value) {
1720 bit = ffsll(high_mask.value) - 1;
1721 low_mask.uvalue = (1ULL << bit) - 1;
1723 get_absolute_rl(expr->left, &orig_rl);
1724 if (sval_is_negative(rl_min(orig_rl)))
1725 orig_rl = remove_range(orig_rl, sval_type_min(known.type), sval_type_val(known.type, -1));
1726 false_rl = remove_range(orig_rl, low_mask, sval_type_max(known.type));
1727 if (type_signed(high_mask.type) && type_unsigned(rl_type(false_rl))) {
1728 false_rl = remove_range(false_rl,
1729 sval_type_val(rl_type(false_rl), sval_type_max(known.type).uvalue),
1730 sval_type_val(rl_type(false_rl), -1));
1733 set_extra_expr_true_false(expr->right,
1734 true_rl ? alloc_estate_rl(true_rl) : NULL,
1735 false_rl ? alloc_estate_rl(false_rl) : NULL);
1737 return;
1740 if (get_implied_value(expr->right, &known)) {
1741 sval_t low_mask = known;
1742 sval_t high_mask;
1744 if (known.value > 0) {
1745 bit = ffsll(known.value) - 1;
1746 low_mask.uvalue = (1ULL << bit) - 1;
1747 get_absolute_rl(expr->left, &orig_rl);
1748 true_rl = remove_range(orig_rl, sval_type_val(known.type, 0), low_mask);
1750 high_mask = get_high_mask(known);
1751 if (high_mask.value) {
1752 bit = ffsll(high_mask.value) - 1;
1753 low_mask.uvalue = (1ULL << bit) - 1;
1755 get_absolute_rl(expr->left, &orig_rl);
1756 if (sval_is_negative(rl_min(orig_rl)))
1757 orig_rl = remove_range(orig_rl, sval_type_min(known.type), sval_type_val(known.type, -1));
1758 false_rl = remove_range(orig_rl, low_mask, sval_type_max(known.type));
1759 if (type_signed(high_mask.type) && type_unsigned(rl_type(false_rl))) {
1760 false_rl = remove_range(false_rl,
1761 sval_type_val(rl_type(false_rl), sval_type_max(known.type).uvalue),
1762 sval_type_val(rl_type(false_rl), -1));
1765 set_extra_expr_true_false(expr->left,
1766 true_rl ? alloc_estate_rl(true_rl) : NULL,
1767 false_rl ? alloc_estate_rl(false_rl) : NULL);
1768 return;
1772 static void handle_MOD_condition(struct expression *expr)
1774 struct range_list *orig_rl;
1775 struct range_list *true_rl;
1776 struct range_list *false_rl = NULL;
1777 sval_t right;
1778 sval_t zero = {
1779 .value = 0,
1782 if (!get_implied_value(expr->right, &right) || right.value == 0)
1783 return;
1784 get_absolute_rl(expr->left, &orig_rl);
1786 zero.type = rl_type(orig_rl);
1788 /* We're basically dorking around the min and max here */
1789 true_rl = remove_range(orig_rl, zero, zero);
1790 if (!sval_is_max(rl_max(true_rl)) &&
1791 !(rl_max(true_rl).value % right.value))
1792 true_rl = remove_range(true_rl, rl_max(true_rl), rl_max(true_rl));
1794 if (rl_equiv(true_rl, orig_rl))
1795 true_rl = NULL;
1797 if (sval_is_positive(rl_min(orig_rl)) &&
1798 (rl_max(orig_rl).value - rl_min(orig_rl).value) / right.value < 5) {
1799 sval_t add;
1800 int i;
1802 add = rl_min(orig_rl);
1803 add.value += right.value - (add.value % right.value);
1804 add.value -= right.value;
1806 for (i = 0; i < 5; i++) {
1807 add.value += right.value;
1808 if (add.value > rl_max(orig_rl).value)
1809 break;
1810 add_range(&false_rl, add, add);
1812 } else {
1813 if (rl_min(orig_rl).uvalue != 0 &&
1814 rl_min(orig_rl).uvalue < right.uvalue) {
1815 sval_t chop = right;
1816 chop.value--;
1817 false_rl = remove_range(orig_rl, zero, chop);
1820 if (!sval_is_max(rl_max(orig_rl)) &&
1821 (rl_max(orig_rl).value % right.value)) {
1822 sval_t chop = rl_max(orig_rl);
1823 chop.value -= chop.value % right.value;
1824 chop.value++;
1825 if (!false_rl)
1826 false_rl = clone_rl(orig_rl);
1827 false_rl = remove_range(false_rl, chop, rl_max(orig_rl));
1831 set_extra_expr_true_false(expr->left,
1832 true_rl ? alloc_estate_rl(true_rl) : NULL,
1833 false_rl ? alloc_estate_rl(false_rl) : NULL);
1836 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
1837 void __extra_match_condition(struct expression *expr)
1839 struct smatch_state *pre_state;
1840 struct smatch_state *true_state;
1841 struct smatch_state *false_state;
1843 expr = strip_expr(expr);
1844 switch (expr->type) {
1845 case EXPR_CALL:
1846 function_comparison(expr, SPECIAL_NOTEQUAL, zero_expr());
1847 return;
1848 case EXPR_PREOP:
1849 case EXPR_SYMBOL:
1850 case EXPR_DEREF: {
1851 sval_t zero;
1853 zero = sval_blank(expr);
1854 zero.value = 0;
1856 pre_state = get_extra_state(expr);
1857 true_state = estate_filter_sval(pre_state, zero);
1858 if (possibly_true(expr, SPECIAL_EQUAL, zero_expr()))
1859 false_state = alloc_estate_sval(zero);
1860 else
1861 false_state = alloc_estate_empty();
1862 set_extra_expr_true_false(expr, true_state, false_state);
1863 return;
1865 case EXPR_COMPARE:
1866 match_comparison(expr);
1867 return;
1868 case EXPR_ASSIGNMENT:
1869 __extra_match_condition(expr->left);
1870 return;
1871 case EXPR_BINOP:
1872 if (expr->op == '&')
1873 handle_AND_condition(expr);
1874 if (expr->op == '%')
1875 handle_MOD_condition(expr);
1876 return;
1880 static void assume_indexes_are_valid(struct expression *expr)
1882 struct expression *array_expr;
1883 int array_size;
1884 struct expression *offset;
1885 struct symbol *offset_type;
1886 struct range_list *rl_before;
1887 struct range_list *rl_after;
1888 struct range_list *filter = NULL;
1889 sval_t size;
1891 expr = strip_expr(expr);
1892 if (!is_array(expr))
1893 return;
1895 offset = get_array_offset(expr);
1896 offset_type = get_type(offset);
1897 if (offset_type && type_signed(offset_type)) {
1898 filter = alloc_rl(sval_type_min(offset_type),
1899 sval_type_val(offset_type, -1));
1902 array_expr = get_array_base(expr);
1903 array_size = get_real_array_size(array_expr);
1904 if (array_size > 1) {
1905 size = sval_type_val(offset_type, array_size);
1906 add_range(&filter, size, sval_type_max(offset_type));
1909 if (!filter)
1910 return;
1911 get_absolute_rl(offset, &rl_before);
1912 rl_after = rl_filter(rl_before, filter);
1913 if (rl_equiv(rl_before, rl_after))
1914 return;
1915 set_extra_expr_nomod(offset, alloc_estate_rl(rl_after));
1918 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
1919 int implied_not_equal(struct expression *expr, long long val)
1921 return !possibly_false(expr, SPECIAL_NOTEQUAL, value_expr(val));
1924 int implied_not_equal_name_sym(char *name, struct symbol *sym, long long val)
1926 struct smatch_state *estate;
1928 estate = get_state(SMATCH_EXTRA, name, sym);
1929 if (!estate)
1930 return 0;
1931 if (!rl_has_sval(estate_rl(estate), sval_type_val(estate_type(estate), 0)))
1932 return 1;
1933 return 0;
1936 int parent_is_null_var_sym(const char *name, struct symbol *sym)
1938 char buf[256];
1939 char *start;
1940 char *end;
1941 struct smatch_state *state;
1943 strncpy(buf, name, sizeof(buf) - 1);
1944 buf[sizeof(buf) - 1] = '\0';
1946 start = &buf[0];
1947 while (*start == '*') {
1948 start++;
1949 state = get_state(SMATCH_EXTRA, start, sym);
1950 if (!state)
1951 continue;
1952 if (!estate_rl(state))
1953 return 1;
1954 if (estate_min(state).value == 0 &&
1955 estate_max(state).value == 0)
1956 return 1;
1959 start = &buf[0];
1960 while (*start == '&')
1961 start++;
1963 while ((end = strrchr(start, '-'))) {
1964 *end = '\0';
1965 state = get_state(SMATCH_EXTRA, start, sym);
1966 if (!state)
1967 continue;
1968 if (estate_min(state).value == 0 &&
1969 estate_max(state).value == 0)
1970 return 1;
1972 return 0;
1975 int parent_is_null(struct expression *expr)
1977 struct symbol *sym;
1978 char *var;
1979 int ret = 0;
1981 expr = strip_expr(expr);
1982 var = expr_to_var_sym(expr, &sym);
1983 if (!var || !sym)
1984 goto free;
1985 ret = parent_is_null_var_sym(var, sym);
1986 free:
1987 free_string(var);
1988 return ret;
1991 static int param_used_callback(void *found, int argc, char **argv, char **azColName)
1993 *(int *)found = 1;
1994 return 0;
1997 static int filter_unused_kzalloc_info(struct expression *call, int param, char *printed_name, struct sm_state *sm)
1999 sval_t sval;
2000 int found = 0;
2002 /* for function pointers assume everything is used */
2003 if (call->fn->type != EXPR_SYMBOL)
2004 return 0;
2007 * kzalloc() information is treated as special because so there is just
2008 * a lot of stuff initialized to zero and it makes building the database
2009 * take hours and hours.
2011 * In theory, we should just remove this line and not pass any unused
2012 * information, but I'm not sure enough that this code works so I want
2013 * to hold off on that for now.
2015 if (!estate_get_single_value(sm->state, &sval) || sval.value != 0)
2016 return 0;
2018 run_sql(&param_used_callback, &found,
2019 "select * from call_implies where %s and type = %d and parameter = %d and key = '%s';",
2020 get_static_filter(call->fn->symbol), PARAM_USED, param, printed_name);
2021 if (found)
2022 return 0;
2024 /* If the database is not built yet, then assume everything is used */
2025 run_sql(&param_used_callback, &found,
2026 "select * from call_implies where %s and type = %d;",
2027 get_static_filter(call->fn->symbol), PARAM_USED);
2028 if (!found)
2029 return 0;
2031 return 1;
2034 struct range_list *intersect_with_real_abs_var_sym(const char *name, struct symbol *sym, struct range_list *start)
2036 struct smatch_state *state;
2039 * Here is the difference between implied value and real absolute, say
2040 * you have:
2042 * int a = (u8)x;
2044 * Then you know that a is 0-255. That's real absolute. But you don't
2045 * know for sure that it actually goes up to 255. So it's not implied.
2046 * Implied indicates a degree of certainty.
2048 * But then say you cap "a" at 8. That means you know it goes up to
2049 * 8. So now the implied value is s32min-8. But you can combine it
2050 * with the real absolute to say that actually it's 0-8.
2052 * We are combining it here. But now that I think about it, this is
2053 * probably not the ideal place to combine it because it should proably
2054 * be done earlier. Oh well, this is an improvement on what was there
2055 * before so I'm going to commit this code.
2059 state = get_real_absolute_state_var_sym(name, sym);
2060 if (!state || !estate_rl(state))
2061 return start;
2063 return rl_intersection(estate_rl(state), start);
2066 struct range_list *intersect_with_real_abs_expr(struct expression *expr, struct range_list *start)
2068 struct smatch_state *state;
2070 state = get_real_absolute_state(expr);
2071 if (!state || !estate_rl(state))
2072 return start;
2074 return rl_intersection(estate_rl(state), start);
2077 static void struct_member_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
2079 struct range_list *rl;
2081 if (estate_is_whole(sm->state))
2082 return;
2083 if (filter_unused_kzalloc_info(call, param, printed_name, sm))
2084 return;
2085 rl = estate_rl(sm->state);
2086 rl = intersect_with_real_abs_var_sym(sm->name, sm->sym, rl);
2087 sql_insert_caller_info(call, PARAM_VALUE, param, printed_name, show_rl(rl));
2088 if (estate_has_fuzzy_max(sm->state))
2089 sql_insert_caller_info(call, FUZZY_MAX, param, printed_name,
2090 sval_to_str(estate_get_fuzzy_max(sm->state)));
2093 static void returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
2095 struct symbol *returned_sym;
2096 struct sm_state *sm;
2097 const char *param_name;
2098 char *compare_str;
2099 char buf[256];
2101 returned_sym = expr_to_sym(expr);
2102 if (!returned_sym)
2103 return;
2105 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
2106 if (!estate_rl(sm->state))
2107 continue;
2108 if (returned_sym != sm->sym)
2109 continue;
2111 param_name = get_param_name(sm);
2112 if (!param_name)
2113 continue;
2114 if (strcmp(param_name, "$") == 0)
2115 continue;
2116 compare_str = name_sym_to_param_comparison(sm->name, sm->sym);
2117 if (!compare_str && estate_is_whole(sm->state))
2118 continue;
2119 snprintf(buf, sizeof(buf), "%s%s", sm->state->name, compare_str ?: "");
2121 sql_insert_return_states(return_id, return_ranges, PARAM_VALUE,
2122 -1, param_name, buf);
2123 } END_FOR_EACH_SM(sm);
2126 static void db_limited_before(void)
2128 unmatched_stree = clone_stree(__get_cur_stree());
2131 static void db_limited_after(void)
2133 free_stree(&unmatched_stree);
2136 static int rl_fits_in_type(struct range_list *rl, struct symbol *type)
2138 if (type_bits(rl_type(rl)) <= type_bits(type))
2139 return 1;
2140 if (sval_cmp(rl_max(rl), sval_type_max(type)) > 0)
2141 return 0;
2142 if (sval_is_negative(rl_min(rl)) &&
2143 sval_cmp(rl_min(rl), sval_type_min(type)) < 0)
2144 return 0;
2145 return 1;
2148 static int basically_the_same(struct range_list *orig, struct range_list *new)
2150 if (rl_equiv(orig, new))
2151 return 1;
2154 * The whole range is essentially the same as 0,4096-27777777777 so
2155 * don't overwrite the implications just to store that.
2158 if (rl_type(orig)->type == SYM_PTR &&
2159 is_whole_rl(orig) &&
2160 rl_min(new).value == 0 &&
2161 rl_max(new).value == valid_ptr_max)
2162 return 1;
2163 return 0;
2166 static void db_param_limit_filter(struct expression *expr, int param, char *key, char *value, enum info_type op)
2168 struct expression *arg;
2169 char *name;
2170 struct symbol *sym;
2171 struct var_sym_list *vsl = NULL;
2172 struct sm_state *sm;
2173 struct symbol *compare_type, *var_type;
2174 struct range_list *rl;
2175 struct range_list *limit;
2176 struct range_list *new;
2178 while (expr->type == EXPR_ASSIGNMENT)
2179 expr = strip_expr(expr->right);
2180 if (expr->type != EXPR_CALL)
2181 return;
2183 arg = get_argument_from_call_expr(expr->args, param);
2184 if (!arg)
2185 return;
2187 name = get_chunk_from_key(arg, key, &sym, &vsl);
2188 if (!name)
2189 return;
2190 if (op != PARAM_LIMIT && !sym)
2191 goto free;
2193 if (strcmp(key, "$") == 0)
2194 compare_type = get_arg_type(expr->fn, param);
2195 else
2196 compare_type = get_member_type_from_key(arg, key);
2198 sm = get_sm_state(SMATCH_EXTRA, name, sym);
2199 if (sm)
2200 rl = estate_rl(sm->state);
2201 else
2202 rl = alloc_whole_rl(compare_type);
2204 if (op == PARAM_LIMIT && !rl_fits_in_type(rl, compare_type))
2205 goto free;
2207 call_results_to_rl(expr, compare_type, value, &limit);
2208 new = rl_intersection(rl, limit);
2210 var_type = get_member_type_from_key(arg, key);
2211 new = cast_rl(var_type, new);
2213 /* We want to preserve the implications here */
2214 if (sm && basically_the_same(estate_rl(sm->state), new))
2215 __set_sm(sm); /* FIXME: Is this really necessary? */
2216 else {
2217 char *tmp_name;
2218 struct symbol *tmp_sym;
2220 tmp_name = map_long_to_short_name_sym(name, sym, &tmp_sym);
2221 if (tmp_name && tmp_sym) {
2222 free_string(name);
2223 name = tmp_name;
2224 sym = tmp_sym;
2227 if (op == PARAM_LIMIT)
2228 set_extra_nomod_vsl(name, sym, vsl, alloc_estate_rl(new));
2229 else
2230 set_extra_mod(name, sym, alloc_estate_rl(new));
2233 free:
2234 free_string(name);
2237 static void db_param_limit(struct expression *expr, int param, char *key, char *value)
2239 db_param_limit_filter(expr, param, key, value, PARAM_LIMIT);
2242 static void db_param_filter(struct expression *expr, int param, char *key, char *value)
2244 db_param_limit_filter(expr, param, key, value, PARAM_FILTER);
2247 static void db_param_add_set(struct expression *expr, int param, char *key, char *value, enum info_type op)
2249 struct expression *arg;
2250 char *name, *tmp_name;
2251 struct symbol *sym, *tmp_sym;
2252 struct symbol *type;
2253 struct smatch_state *state;
2254 struct range_list *new = NULL;
2255 struct range_list *added = NULL;
2257 while (expr->type == EXPR_ASSIGNMENT)
2258 expr = strip_expr(expr->right);
2259 if (expr->type != EXPR_CALL)
2260 return;
2262 arg = get_argument_from_call_expr(expr->args, param);
2263 if (!arg)
2264 return;
2265 type = get_member_type_from_key(arg, key);
2266 name = get_variable_from_key(arg, key, &sym);
2267 if (!name || !sym)
2268 goto free;
2270 state = get_state(SMATCH_EXTRA, name, sym);
2271 if (state)
2272 new = estate_rl(state);
2274 call_results_to_rl(expr, type, value, &added);
2276 if (op == PARAM_SET)
2277 new = added;
2278 else
2279 new = rl_union(new, added);
2281 tmp_name = map_long_to_short_name_sym_nostack(name, sym, &tmp_sym);
2282 if (tmp_name && tmp_sym) {
2283 free_string(name);
2284 name = tmp_name;
2285 sym = tmp_sym;
2287 set_extra_mod(name, sym, alloc_estate_rl(new));
2288 free:
2289 free_string(name);
2292 static void db_param_add(struct expression *expr, int param, char *key, char *value)
2294 in_param_set = true;
2295 db_param_add_set(expr, param, key, value, PARAM_ADD);
2296 in_param_set = false;
2299 static void db_param_set(struct expression *expr, int param, char *key, char *value)
2301 in_param_set = true;
2302 db_param_add_set(expr, param, key, value, PARAM_SET);
2303 in_param_set = false;
2306 static void db_param_value(struct expression *expr, int param, char *key, char *value)
2308 struct expression *call;
2309 char *name;
2310 struct symbol *sym;
2311 struct symbol *type;
2312 struct range_list *rl = NULL;
2314 if (param != -1)
2315 return;
2317 call = expr;
2318 while (call->type == EXPR_ASSIGNMENT)
2319 call = strip_expr(call->right);
2320 if (call->type != EXPR_CALL)
2321 return;
2323 type = get_member_type_from_key(expr->left, key);
2324 name = get_variable_from_key(expr->left, key, &sym);
2325 if (!name || !sym)
2326 goto free;
2328 call_results_to_rl(call, type, value, &rl);
2330 set_extra_mod(name, sym, alloc_estate_rl(rl));
2331 free:
2332 free_string(name);
2335 static void match_call_info(struct expression *expr)
2337 struct smatch_state *state;
2338 struct range_list *rl = NULL;
2339 struct expression *arg;
2340 struct symbol *type;
2341 int i = 0;
2343 FOR_EACH_PTR(expr->args, arg) {
2344 type = get_arg_type(expr->fn, i);
2346 if (get_implied_rl(arg, &rl))
2347 rl = cast_rl(type, rl);
2348 else
2349 rl = cast_rl(type, alloc_whole_rl(get_type(arg)));
2351 if (!is_whole_rl(rl)) {
2352 rl = intersect_with_real_abs_expr(arg, rl);
2353 sql_insert_caller_info(expr, PARAM_VALUE, i, "$", show_rl(rl));
2355 state = get_state_expr(SMATCH_EXTRA, arg);
2356 if (estate_has_fuzzy_max(state)) {
2357 sql_insert_caller_info(expr, FUZZY_MAX, i, "$",
2358 sval_to_str(estate_get_fuzzy_max(state)));
2360 i++;
2361 } END_FOR_EACH_PTR(arg);
2364 static void set_param_value(const char *name, struct symbol *sym, char *key, char *value)
2366 struct range_list *rl = NULL;
2367 struct smatch_state *state;
2368 struct symbol *type;
2369 char fullname[256];
2370 sval_t dummy;
2372 if (strcmp(key, "*$") == 0)
2373 snprintf(fullname, sizeof(fullname), "*%s", name);
2374 else if (strncmp(key, "$", 1) == 0)
2375 snprintf(fullname, 256, "%s%s", name, key + 1);
2376 else
2377 return;
2379 type = get_member_type_from_key(symbol_expression(sym), key);
2380 str_to_rl(type, value, &rl);
2381 state = alloc_estate_rl(rl);
2382 if (estate_get_single_value(state, &dummy))
2383 estate_set_hard_max(state);
2384 set_state(SMATCH_EXTRA, fullname, sym, state);
2387 static void set_param_hard_max(const char *name, struct symbol *sym, char *key, char *value)
2389 struct range_list *rl = NULL;
2390 struct smatch_state *state;
2391 struct symbol *type;
2392 char fullname[256];
2393 sval_t max;
2395 if (strcmp(key, "*$") == 0)
2396 snprintf(fullname, sizeof(fullname), "*%s", name);
2397 else if (strncmp(key, "$", 1) == 0)
2398 snprintf(fullname, 256, "%s%s", name, key + 1);
2399 else
2400 return;
2402 state = get_state(SMATCH_EXTRA, fullname, sym);
2403 if (!state)
2404 return;
2405 type = get_member_type_from_key(symbol_expression(sym), key);
2406 str_to_rl(type, value, &rl);
2407 if (!rl_to_sval(rl, &max))
2408 return;
2409 estate_set_fuzzy_max(state, max);
2412 struct smatch_state *get_extra_state(struct expression *expr)
2414 char *name;
2415 struct symbol *sym;
2416 struct smatch_state *ret = NULL;
2417 struct range_list *rl;
2419 if (is_pointer(expr) && get_address_rl(expr, &rl))
2420 return alloc_estate_rl(rl);
2422 name = expr_to_known_chunk_sym(expr, &sym);
2423 if (!name)
2424 goto free;
2426 ret = get_state(SMATCH_EXTRA, name, sym);
2427 free:
2428 free_string(name);
2429 return ret;
2432 void register_smatch_extra(int id)
2434 my_id = id;
2436 add_merge_hook(my_id, &merge_estates);
2437 add_unmatched_state_hook(my_id, &unmatched_state);
2438 select_caller_info_hook(set_param_value, PARAM_VALUE);
2439 select_caller_info_hook(set_param_hard_max, FUZZY_MAX);
2440 select_return_states_before(&db_limited_before);
2441 select_return_states_hook(PARAM_LIMIT, &db_param_limit);
2442 select_return_states_hook(PARAM_FILTER, &db_param_filter);
2443 select_return_states_hook(PARAM_ADD, &db_param_add);
2444 select_return_states_hook(PARAM_SET, &db_param_set);
2445 select_return_states_hook(PARAM_VALUE, &db_param_value);
2446 select_return_states_after(&db_limited_after);
2449 static void match_link_modify(struct sm_state *sm, struct expression *mod_expr)
2451 struct var_sym_list *links;
2452 struct var_sym *tmp;
2453 struct smatch_state *state;
2455 links = sm->state->data;
2457 FOR_EACH_PTR(links, tmp) {
2458 if (sm->sym == tmp->sym &&
2459 strcmp(sm->name, tmp->var) == 0)
2460 continue;
2461 state = get_state(SMATCH_EXTRA, tmp->var, tmp->sym);
2462 if (!state)
2463 continue;
2464 set_state(SMATCH_EXTRA, tmp->var, tmp->sym, alloc_estate_whole(estate_type(state)));
2465 } END_FOR_EACH_PTR(tmp);
2466 set_state(link_id, sm->name, sm->sym, &undefined);
2469 void register_smatch_extra_links(int id)
2471 link_id = id;
2474 void register_smatch_extra_late(int id)
2476 add_merge_hook(link_id, &merge_link_states);
2477 add_modification_hook(link_id, &match_link_modify);
2478 add_hook(&match_dereferences, DEREF_HOOK);
2479 add_hook(&match_pointer_as_array, OP_HOOK);
2480 select_call_implies_hook(DEREFERENCE, &set_param_dereferenced);
2481 add_hook(&match_function_call, FUNCTION_CALL_HOOK);
2482 add_hook(&match_assign, ASSIGNMENT_HOOK);
2483 add_hook(&match_assign, GLOBAL_ASSIGNMENT_HOOK);
2484 add_hook(&unop_expr, OP_HOOK);
2485 add_hook(&asm_expr, ASM_HOOK);
2486 add_untracked_param_hook(&match_untracked_array);
2488 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
2489 add_member_info_callback(my_id, struct_member_callback);
2490 add_split_return_callback(&returned_struct_members);
2492 add_hook(&assume_indexes_are_valid, OP_HOOK);