extra: preserve pointer ranges across parameter dereferences
[smatch.git] / smatch_extra.c
blobdfd7700c7feb55eba51e269adf71e976f2805617
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 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 smatch_state *state)
84 mod_hook **fn;
86 FOR_EACH_PTR(hooks, fn) {
87 (*fn)(name, sym, state);
88 } END_FOR_EACH_PTR(fn);
91 void call_extra_mod_hooks(const char *name, struct symbol *sym, struct smatch_state *state)
93 call_extra_hooks(extra_mod_hooks, name, sym, state);
96 void call_extra_nomod_hooks(const char *name, struct symbol *sym, struct smatch_state *state)
98 call_extra_hooks(extra_nomod_hooks, name, sym, state);
101 static bool in_param_set;
102 static void set_extra_mod_helper(const char *name, struct symbol *sym, struct smatch_state *state)
104 remove_from_equiv(name, sym);
105 call_extra_mod_hooks(name, sym, 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 smatch_state *state)
114 call_extra_nomod_hooks(name, sym, 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 smatch_state *state)
195 char *new_name;
196 struct symbol *new_sym;
198 set_extra_mod_helper(name, sym, 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, state);
202 free_string(new_name);
205 static void clear_array_states(struct expression *array)
207 struct sm_state *sm;
209 sm = get_sm_state_expr(link_id, array);
210 if (sm)
211 match_link_modify(sm, NULL);
214 static void set_extra_array_mod(struct expression *expr, struct smatch_state *state)
216 struct expression *array;
217 struct var_sym_list *vsl;
218 struct var_sym *vs;
219 char *name;
220 struct symbol *sym;
222 array = get_array_base(expr);
224 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
225 if (!name || !vsl) {
226 clear_array_states(array);
227 goto free;
230 FOR_EACH_PTR(vsl, vs) {
231 store_link(link_id, vs->var, vs->sym, name, sym);
232 } END_FOR_EACH_PTR(vs);
234 call_extra_mod_hooks(name, sym, state);
235 set_state(SMATCH_EXTRA, name, sym, state);
236 free:
237 free_string(name);
240 void set_extra_expr_mod(struct expression *expr, struct smatch_state *state)
242 struct symbol *sym;
243 char *name;
245 if (is_array(expr)) {
246 set_extra_array_mod(expr, state);
247 return;
250 expr = strip_expr(expr);
251 name = expr_to_var_sym(expr, &sym);
252 if (!name || !sym)
253 goto free;
254 set_extra_mod(name, sym, state);
255 free:
256 free_string(name);
259 void set_extra_nomod(const char *name, struct symbol *sym, struct smatch_state *state)
261 char *new_name;
262 struct symbol *new_sym;
263 struct relation *rel;
264 struct smatch_state *orig_state;
266 orig_state = get_state(SMATCH_EXTRA, name, sym);
268 /* don't save unknown states if leaving it blank is the same */
269 if (!orig_state && estate_is_unknown(state))
270 return;
272 new_name = get_other_name_sym(name, sym, &new_sym);
273 if (new_name && new_sym)
274 set_extra_nomod_helper(new_name, new_sym, state);
275 free_string(new_name);
277 if (!estate_related(orig_state)) {
278 set_extra_nomod_helper(name, sym, state);
279 return;
282 set_related(state, estate_related(orig_state));
283 FOR_EACH_PTR(estate_related(orig_state), rel) {
284 struct smatch_state *estate;
286 if (option_debug_related)
287 sm_msg("%s updating related %s to %s", name, rel->name, state->name);
288 estate = get_state(SMATCH_EXTRA, rel->name, rel->sym);
289 if (!estate)
290 continue;
291 set_extra_nomod_helper(rel->name, rel->sym, clone_estate_cast(estate_type(estate), state));
292 } END_FOR_EACH_PTR(rel);
295 void set_extra_nomod_vsl(const char *name, struct symbol *sym, struct var_sym_list *vsl, struct smatch_state *state)
297 struct var_sym *vs;
299 FOR_EACH_PTR(vsl, vs) {
300 store_link(link_id, vs->var, vs->sym, name, sym);
301 } END_FOR_EACH_PTR(vs);
303 set_extra_nomod(name, sym, state);
307 * This is for return_implies_state() hooks which modify a SMATCH_EXTRA state
309 void set_extra_expr_nomod(struct expression *expr, struct smatch_state *state)
311 struct var_sym_list *vsl;
312 struct var_sym *vs;
313 char *name;
314 struct symbol *sym;
316 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
317 if (!name || !vsl)
318 goto free;
319 FOR_EACH_PTR(vsl, vs) {
320 store_link(link_id, vs->var, vs->sym, name, sym);
321 } END_FOR_EACH_PTR(vs);
323 set_extra_nomod(name, sym, state);
324 free:
325 free_string(name);
328 static void set_extra_true_false(const char *name, struct symbol *sym,
329 struct smatch_state *true_state,
330 struct smatch_state *false_state)
332 char *new_name;
333 struct symbol *new_sym;
334 struct relation *rel;
335 struct smatch_state *orig_state;
337 if (!true_state && !false_state)
338 return;
340 if (in_warn_on_macro())
341 return;
343 new_name = get_other_name_sym(name, sym, &new_sym);
344 if (new_name && new_sym)
345 set_true_false_states(SMATCH_EXTRA, new_name, new_sym, true_state, false_state);
346 free_string(new_name);
348 orig_state = get_state(SMATCH_EXTRA, name, sym);
350 if (!estate_related(orig_state)) {
351 set_true_false_states(SMATCH_EXTRA, name, sym, true_state, false_state);
352 return;
355 if (true_state)
356 set_related(true_state, estate_related(orig_state));
357 if (false_state)
358 set_related(false_state, estate_related(orig_state));
360 FOR_EACH_PTR(estate_related(orig_state), rel) {
361 set_true_false_states(SMATCH_EXTRA, rel->name, rel->sym,
362 true_state, false_state);
363 } END_FOR_EACH_PTR(rel);
366 static void set_extra_chunk_true_false(struct expression *expr,
367 struct smatch_state *true_state,
368 struct smatch_state *false_state)
370 struct var_sym_list *vsl;
371 struct var_sym *vs;
372 struct symbol *type;
373 char *name;
374 struct symbol *sym;
376 if (in_warn_on_macro())
377 return;
379 type = get_type(expr);
380 if (!type)
381 return;
383 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
384 if (!name || !vsl)
385 goto free;
386 FOR_EACH_PTR(vsl, vs) {
387 store_link(link_id, vs->var, vs->sym, name, sym);
388 } END_FOR_EACH_PTR(vs);
390 set_true_false_states(SMATCH_EXTRA, name, sym,
391 clone_estate(true_state),
392 clone_estate(false_state));
393 free:
394 free_string(name);
397 static void set_extra_expr_true_false(struct expression *expr,
398 struct smatch_state *true_state,
399 struct smatch_state *false_state)
401 char *name;
402 struct symbol *sym;
403 sval_t sval;
405 if (!true_state && !false_state)
406 return;
408 if (get_value(expr, &sval))
409 return;
411 expr = strip_expr(expr);
412 name = expr_to_var_sym(expr, &sym);
413 if (!name || !sym) {
414 free_string(name);
415 set_extra_chunk_true_false(expr, true_state, false_state);
416 return;
418 set_extra_true_false(name, sym, true_state, false_state);
419 free_string(name);
422 static struct sm_state *handle_canonical_while_count_down(struct statement *loop)
424 struct expression *iter_var;
425 struct expression *condition;
426 struct sm_state *sm;
427 struct smatch_state *estate;
428 sval_t start;
430 condition = strip_expr(loop->iterator_pre_condition);
431 if (!condition)
432 return NULL;
433 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
434 return NULL;
435 if (condition->op != SPECIAL_DECREMENT)
436 return NULL;
438 iter_var = condition->unop;
439 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
440 if (!sm)
441 return NULL;
442 if (sval_cmp_val(estate_min(sm->state), 0) < 0)
443 return NULL;
444 start = estate_max(sm->state);
445 if (sval_cmp_val(start, 0) <= 0)
446 return NULL;
447 if (!sval_is_max(start))
448 start.value--;
450 if (condition->type == EXPR_PREOP) {
451 estate = alloc_estate_range(sval_type_val(start.type, 1), start);
452 if (estate_has_hard_max(sm->state))
453 estate_set_hard_max(estate);
454 estate_copy_fuzzy_max(estate, sm->state);
455 set_extra_expr_mod(iter_var, estate);
457 if (condition->type == EXPR_POSTOP) {
458 estate = alloc_estate_range(sval_type_val(start.type, 0), start);
459 if (estate_has_hard_max(sm->state))
460 estate_set_hard_max(estate);
461 estate_copy_fuzzy_max(estate, sm->state);
462 set_extra_expr_mod(iter_var, estate);
464 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
467 static struct sm_state *handle_canonical_for_inc(struct expression *iter_expr,
468 struct expression *condition)
470 struct expression *iter_var;
471 struct sm_state *sm;
472 struct smatch_state *estate;
473 sval_t start, end, max;
475 iter_var = iter_expr->unop;
476 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
477 if (!sm)
478 return NULL;
479 if (!estate_get_single_value(sm->state, &start))
480 return NULL;
481 if (get_implied_max(condition->right, &end))
482 end = sval_cast(get_type(iter_var), end);
483 else
484 end = sval_type_max(get_type(iter_var));
486 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
487 return NULL;
489 switch (condition->op) {
490 case SPECIAL_UNSIGNED_LT:
491 case SPECIAL_NOTEQUAL:
492 case '<':
493 if (!sval_is_min(end))
494 end.value--;
495 break;
496 case SPECIAL_UNSIGNED_LTE:
497 case SPECIAL_LTE:
498 break;
499 default:
500 return NULL;
502 if (sval_cmp(end, start) < 0)
503 return NULL;
504 estate = alloc_estate_range(start, end);
505 if (get_hard_max(condition->right, &max)) {
506 estate_set_hard_max(estate);
507 if (condition->op == '<' ||
508 condition->op == SPECIAL_UNSIGNED_LT ||
509 condition->op == SPECIAL_NOTEQUAL)
510 max.value--;
511 estate_set_fuzzy_max(estate, max);
513 set_extra_expr_mod(iter_var, estate);
514 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
517 static struct sm_state *handle_canonical_for_dec(struct expression *iter_expr,
518 struct expression *condition)
520 struct expression *iter_var;
521 struct sm_state *sm;
522 struct smatch_state *estate;
523 sval_t start, end;
525 iter_var = iter_expr->unop;
526 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
527 if (!sm)
528 return NULL;
529 if (!estate_get_single_value(sm->state, &start))
530 return NULL;
531 if (!get_implied_min(condition->right, &end))
532 end = sval_type_min(get_type(iter_var));
533 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
534 return NULL;
536 switch (condition->op) {
537 case SPECIAL_NOTEQUAL:
538 case '>':
539 if (!sval_is_min(end) && !sval_is_max(end))
540 end.value++;
541 break;
542 case SPECIAL_GTE:
543 break;
544 default:
545 return NULL;
547 if (sval_cmp(end, start) > 0)
548 return NULL;
549 estate = alloc_estate_range(end, start);
550 estate_set_hard_max(estate);
551 estate_set_fuzzy_max(estate, estate_get_fuzzy_max(estate));
552 set_extra_expr_mod(iter_var, estate);
553 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
556 static struct sm_state *handle_canonical_for_loops(struct statement *loop)
558 struct expression *iter_expr;
559 struct expression *condition;
561 if (!loop->iterator_post_statement)
562 return NULL;
563 if (loop->iterator_post_statement->type != STMT_EXPRESSION)
564 return NULL;
565 iter_expr = loop->iterator_post_statement->expression;
566 if (!loop->iterator_pre_condition)
567 return NULL;
568 if (loop->iterator_pre_condition->type != EXPR_COMPARE)
569 return NULL;
570 condition = loop->iterator_pre_condition;
572 if (iter_expr->op == SPECIAL_INCREMENT)
573 return handle_canonical_for_inc(iter_expr, condition);
574 if (iter_expr->op == SPECIAL_DECREMENT)
575 return handle_canonical_for_dec(iter_expr, condition);
576 return NULL;
579 struct sm_state *__extra_handle_canonical_loops(struct statement *loop, struct stree **stree)
581 struct sm_state *ret;
583 __push_fake_cur_stree();
584 if (!loop->iterator_post_statement)
585 ret = handle_canonical_while_count_down(loop);
586 else
587 ret = handle_canonical_for_loops(loop);
588 *stree = __pop_fake_cur_stree();
589 return ret;
592 int __iterator_unchanged(struct sm_state *sm)
594 if (!sm)
595 return 0;
596 if (get_sm_state(my_id, sm->name, sm->sym) == sm)
597 return 1;
598 return 0;
601 static void while_count_down_after(struct sm_state *sm, struct expression *condition)
603 sval_t after_value;
605 /* paranoid checking. prolly not needed */
606 condition = strip_expr(condition);
607 if (!condition)
608 return;
609 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
610 return;
611 if (condition->op != SPECIAL_DECREMENT)
612 return;
613 after_value = estate_min(sm->state);
614 after_value.value--;
615 set_extra_mod(sm->name, sm->sym, alloc_estate_sval(after_value));
618 void __extra_pre_loop_hook_after(struct sm_state *sm,
619 struct statement *iterator,
620 struct expression *condition)
622 struct expression *iter_expr;
623 sval_t limit;
624 struct smatch_state *state;
626 if (!iterator) {
627 while_count_down_after(sm, condition);
628 return;
631 iter_expr = iterator->expression;
633 if (condition->type != EXPR_COMPARE)
634 return;
635 if (iter_expr->op == SPECIAL_INCREMENT) {
636 limit = sval_binop(estate_max(sm->state), '+',
637 sval_type_val(estate_type(sm->state), 1));
638 } else {
639 limit = sval_binop(estate_min(sm->state), '-',
640 sval_type_val(estate_type(sm->state), 1));
642 if (!estate_has_hard_max(sm->state) && !__has_breaks()) {
643 if (iter_expr->op == SPECIAL_INCREMENT)
644 state = alloc_estate_range(estate_min(sm->state), limit);
645 else
646 state = alloc_estate_range(limit, estate_max(sm->state));
647 } else {
648 state = alloc_estate_sval(limit);
650 if (!estate_has_hard_max(sm->state)) {
651 estate_clear_hard_max(state);
653 if (estate_has_fuzzy_max(sm->state)) {
654 sval_t hmax = estate_get_fuzzy_max(sm->state);
655 sval_t max = estate_max(sm->state);
657 if (sval_cmp(hmax, max) != 0)
658 estate_clear_fuzzy_max(state);
659 } else if (!estate_has_fuzzy_max(sm->state)) {
660 estate_clear_fuzzy_max(state);
663 set_extra_mod(sm->name, sm->sym, state);
666 static struct stree *unmatched_stree;
667 static struct smatch_state *unmatched_state(struct sm_state *sm)
669 struct smatch_state *state;
671 if (unmatched_stree) {
672 state = get_state_stree(unmatched_stree, SMATCH_EXTRA, sm->name, sm->sym);
673 if (state)
674 return state;
676 if (parent_is_gone_var_sym(sm->name, sm->sym))
677 return alloc_estate_empty();
678 return alloc_estate_whole(estate_type(sm->state));
681 static void clear_the_pointed_at(struct expression *expr)
683 struct stree *stree;
684 char *name;
685 struct symbol *sym;
686 struct sm_state *tmp;
688 name = expr_to_var_sym(expr, &sym);
689 if (!name || !sym)
690 goto free;
692 stree = __get_cur_stree();
693 FOR_EACH_MY_SM(SMATCH_EXTRA, stree, tmp) {
694 if (tmp->name[0] != '*')
695 continue;
696 if (tmp->sym != sym)
697 continue;
698 if (strcmp(tmp->name + 1, name) != 0)
699 continue;
700 set_extra_mod(tmp->name, tmp->sym, alloc_estate_whole(estate_type(tmp->state)));
701 } END_FOR_EACH_SM(tmp);
703 free:
704 free_string(name);
707 static void match_function_call(struct expression *expr)
709 struct expression *arg;
710 struct expression *tmp;
712 /* if we have the db this is handled in smatch_function_hooks.c */
713 if (!option_no_db)
714 return;
715 if (inlinable(expr->fn))
716 return;
718 FOR_EACH_PTR(expr->args, arg) {
719 tmp = strip_expr(arg);
720 if (tmp->type == EXPR_PREOP && tmp->op == '&')
721 set_extra_expr_mod(tmp->unop, alloc_estate_whole(get_type(tmp->unop)));
722 else
723 clear_the_pointed_at(tmp);
724 } END_FOR_EACH_PTR(arg);
727 static int values_fit_type(struct expression *left, struct expression *right)
729 struct range_list *rl;
730 struct symbol *type;
732 type = get_type(left);
733 if (!type)
734 return 0;
735 get_absolute_rl(right, &rl);
736 if (type_unsigned(type) && sval_is_negative(rl_min(rl)))
737 return 0;
738 if (sval_cmp(sval_type_min(type), rl_min(rl)) > 0)
739 return 0;
740 if (sval_cmp(sval_type_max(type), rl_max(rl)) < 0)
741 return 0;
742 return 1;
745 static void save_chunk_info(struct expression *left, struct expression *right)
747 struct var_sym_list *vsl;
748 struct var_sym *vs;
749 struct expression *add_expr;
750 struct symbol *type;
751 sval_t sval;
752 char *name;
753 struct symbol *sym;
755 if (right->type != EXPR_BINOP || right->op != '-')
756 return;
757 if (!get_value(right->left, &sval))
758 return;
759 if (!expr_to_sym(right->right))
760 return;
762 add_expr = binop_expression(left, '+', right->right);
763 type = get_type(add_expr);
764 if (!type)
765 return;
766 name = expr_to_chunk_sym_vsl(add_expr, &sym, &vsl);
767 if (!name || !vsl)
768 goto free;
769 FOR_EACH_PTR(vsl, vs) {
770 store_link(link_id, vs->var, vs->sym, name, sym);
771 } END_FOR_EACH_PTR(vs);
773 set_state(SMATCH_EXTRA, name, sym, alloc_estate_sval(sval_cast(type, sval)));
774 free:
775 free_string(name);
778 static void do_array_assign(struct expression *left, int op, struct expression *right)
780 struct range_list *rl;
782 if (op == '=') {
783 get_absolute_rl(right, &rl);
784 rl = cast_rl(get_type(left), rl);
785 } else {
786 rl = alloc_whole_rl(get_type(left));
789 set_extra_array_mod(left, alloc_estate_rl(rl));
792 static void match_untracked_array(struct expression *call, int param)
794 struct expression *arg;
796 arg = get_argument_from_call_expr(call->args, param);
797 arg = strip_expr(arg);
799 clear_array_states(arg);
802 static void match_vanilla_assign(struct expression *left, struct expression *right)
804 struct range_list *orig_rl = NULL;
805 struct range_list *rl = NULL;
806 struct symbol *right_sym;
807 struct symbol *left_type;
808 struct symbol *right_type;
809 char *right_name = NULL;
810 struct symbol *sym;
811 char *name;
812 sval_t max;
813 struct smatch_state *state;
814 int comparison;
816 if (is_struct(left))
817 return;
819 save_chunk_info(left, right);
821 name = expr_to_var_sym(left, &sym);
822 if (!name) {
823 if (is_array(left))
824 do_array_assign(left, '=', right);
825 return;
828 left_type = get_type(left);
829 right_type = get_type(right);
831 right_name = expr_to_var_sym(right, &right_sym);
833 if (!__in_fake_assign &&
834 !(right->type == EXPR_PREOP && right->op == '&') &&
835 right_name && right_sym &&
836 values_fit_type(left, right) &&
837 !has_symbol(right, sym)) {
838 set_equiv(left, right);
839 goto free;
842 if (is_pointer(right) && get_address_rl(right, &rl)) {
843 state = alloc_estate_rl(rl);
844 goto done;
847 if (__in_fake_assign) {
848 struct smatch_state *right_state;
849 sval_t sval;
851 if (get_value(right, &sval)) {
852 sval = sval_cast(left_type, sval);
853 state = alloc_estate_sval(sval);
854 goto done;
857 right_state = get_state(SMATCH_EXTRA, right_name, right_sym);
858 if (right_state) {
859 /* simple assignment */
860 state = clone_estate(right_state);
861 goto done;
864 state = alloc_estate_rl(alloc_whole_rl(left_type));
865 goto done;
868 comparison = get_comparison(left, right);
869 if (comparison) {
870 comparison = flip_comparison(comparison);
871 get_implied_rl(left, &orig_rl);
874 if (get_implied_rl(right, &rl)) {
875 rl = cast_rl(left_type, rl);
876 if (orig_rl)
877 filter_by_comparison(&rl, comparison, orig_rl);
878 state = alloc_estate_rl(rl);
879 if (get_hard_max(right, &max)) {
880 estate_set_hard_max(state);
881 estate_set_fuzzy_max(state, max);
883 } else {
884 rl = alloc_whole_rl(right_type);
885 rl = cast_rl(left_type, rl);
886 if (orig_rl)
887 filter_by_comparison(&rl, comparison, orig_rl);
888 state = alloc_estate_rl(rl);
891 done:
892 set_extra_mod(name, sym, state);
893 free:
894 free_string(right_name);
897 static int op_remove_assign(int op)
899 switch (op) {
900 case SPECIAL_ADD_ASSIGN:
901 return '+';
902 case SPECIAL_SUB_ASSIGN:
903 return '-';
904 case SPECIAL_MUL_ASSIGN:
905 return '*';
906 case SPECIAL_DIV_ASSIGN:
907 return '/';
908 case SPECIAL_MOD_ASSIGN:
909 return '%';
910 case SPECIAL_AND_ASSIGN:
911 return '&';
912 case SPECIAL_OR_ASSIGN:
913 return '|';
914 case SPECIAL_XOR_ASSIGN:
915 return '^';
916 case SPECIAL_SHL_ASSIGN:
917 return SPECIAL_LEFTSHIFT;
918 case SPECIAL_SHR_ASSIGN:
919 return SPECIAL_RIGHTSHIFT;
920 default:
921 return op;
925 static void match_assign(struct expression *expr)
927 struct range_list *rl = NULL;
928 struct expression *left;
929 struct expression *right;
930 struct expression *binop_expr;
931 struct symbol *left_type;
932 struct symbol *sym;
933 char *name;
934 sval_t left_min, left_max;
935 sval_t right_min, right_max;
936 sval_t res_min, res_max;
938 left = strip_expr(expr->left);
940 right = strip_parens(expr->right);
941 if (right->type == EXPR_CALL && sym_name_is("__builtin_expect", right->fn))
942 right = get_argument_from_call_expr(right->args, 0);
943 while (right->type == EXPR_ASSIGNMENT && right->op == '=')
944 right = strip_parens(right->left);
946 if (expr->op == '=' && is_condition(expr->right))
947 return; /* handled in smatch_condition.c */
948 if (expr->op == '=' && right->type == EXPR_CALL)
949 return; /* handled in smatch_function_hooks.c */
950 if (expr->op == '=') {
951 match_vanilla_assign(left, right);
952 return;
955 name = expr_to_var_sym(left, &sym);
956 if (!name)
957 return;
959 left_type = get_type(left);
961 res_min = sval_type_min(left_type);
962 res_max = sval_type_max(left_type);
964 switch (expr->op) {
965 case SPECIAL_ADD_ASSIGN:
966 get_absolute_max(left, &left_max);
967 get_absolute_max(right, &right_max);
968 if (sval_binop_overflows(left_max, '+', sval_cast(left_type, right_max)))
969 break;
970 if (get_implied_min(left, &left_min) &&
971 !sval_is_negative_min(left_min) &&
972 get_implied_min(right, &right_min) &&
973 !sval_is_negative_min(right_min)) {
974 res_min = sval_binop(left_min, '+', right_min);
975 res_min = sval_cast(left_type, res_min);
977 if (inside_loop()) /* we are assuming loops don't lead to wrapping */
978 break;
979 res_max = sval_binop(left_max, '+', right_max);
980 res_max = sval_cast(left_type, res_max);
981 break;
982 case SPECIAL_SUB_ASSIGN:
983 if (get_implied_max(left, &left_max) &&
984 !sval_is_max(left_max) &&
985 get_implied_min(right, &right_min) &&
986 !sval_is_min(right_min)) {
987 res_max = sval_binop(left_max, '-', right_min);
988 res_max = sval_cast(left_type, res_max);
990 if (inside_loop())
991 break;
992 if (get_implied_min(left, &left_min) &&
993 !sval_is_min(left_min) &&
994 get_implied_max(right, &right_max) &&
995 !sval_is_max(right_max)) {
996 res_min = sval_binop(left_min, '-', right_max);
997 res_min = sval_cast(left_type, res_min);
999 break;
1000 case SPECIAL_AND_ASSIGN:
1001 case SPECIAL_MOD_ASSIGN:
1002 case SPECIAL_SHL_ASSIGN:
1003 case SPECIAL_SHR_ASSIGN:
1004 case SPECIAL_OR_ASSIGN:
1005 case SPECIAL_XOR_ASSIGN:
1006 case SPECIAL_MUL_ASSIGN:
1007 case SPECIAL_DIV_ASSIGN:
1008 binop_expr = binop_expression(expr->left,
1009 op_remove_assign(expr->op),
1010 expr->right);
1011 if (get_absolute_rl(binop_expr, &rl)) {
1012 rl = cast_rl(left_type, rl);
1013 set_extra_mod(name, sym, alloc_estate_rl(rl));
1014 goto free;
1016 break;
1018 rl = cast_rl(left_type, alloc_rl(res_min, res_max));
1019 set_extra_mod(name, sym, alloc_estate_rl(rl));
1020 free:
1021 free_string(name);
1024 static struct smatch_state *increment_state(struct smatch_state *state)
1026 sval_t min = estate_min(state);
1027 sval_t max = estate_max(state);
1029 if (!estate_rl(state))
1030 return NULL;
1032 if (inside_loop())
1033 max = sval_type_max(max.type);
1035 if (!sval_is_min(min) && !sval_is_max(min))
1036 min.value++;
1037 if (!sval_is_min(max) && !sval_is_max(max))
1038 max.value++;
1039 return alloc_estate_range(min, max);
1042 static struct smatch_state *decrement_state(struct smatch_state *state)
1044 sval_t min = estate_min(state);
1045 sval_t max = estate_max(state);
1047 if (!estate_rl(state))
1048 return NULL;
1050 if (inside_loop())
1051 min = sval_type_min(min.type);
1053 if (!sval_is_min(min) && !sval_is_max(min))
1054 min.value--;
1055 if (!sval_is_min(max) && !sval_is_max(max))
1056 max.value--;
1057 return alloc_estate_range(min, max);
1060 static void clear_pointed_at_state(struct expression *expr)
1062 struct symbol *type;
1065 * ALERT: This is sort of a mess. If it's is a struct assigment like
1066 * "foo = bar;", then that's handled by smatch_struct_assignment.c.
1067 * the same thing for p++ where "p" is a struct. Most modifications
1068 * are handled by the assignment hook or the db. Smatch_extra.c doesn't
1069 * use smatch_modification.c because we have to get the ordering right
1070 * or something. So if you have p++ where p is a pointer to a standard
1071 * c type then we handle that here. What a mess.
1074 type = get_type(expr);
1075 if (!type || type->type != SYM_PTR)
1076 return;
1077 type = get_real_base_type(type);
1078 if (!type || type->type != SYM_BASETYPE)
1079 return;
1080 set_extra_expr_mod(deref_expression(expr), alloc_estate_whole(type));
1083 static void unop_expr(struct expression *expr)
1085 struct smatch_state *state;
1087 if (expr->smatch_flags & Handled)
1088 return;
1090 switch (expr->op) {
1091 case SPECIAL_INCREMENT:
1092 state = get_state_expr(SMATCH_EXTRA, expr->unop);
1093 state = increment_state(state);
1094 if (!state)
1095 state = alloc_estate_whole(get_type(expr));
1096 set_extra_expr_mod(expr->unop, state);
1097 clear_pointed_at_state(expr->unop);
1098 break;
1099 case SPECIAL_DECREMENT:
1100 state = get_state_expr(SMATCH_EXTRA, expr->unop);
1101 state = decrement_state(state);
1102 if (!state)
1103 state = alloc_estate_whole(get_type(expr));
1104 set_extra_expr_mod(expr->unop, state);
1105 clear_pointed_at_state(expr->unop);
1106 break;
1107 default:
1108 return;
1112 static void asm_expr(struct statement *stmt)
1115 struct expression *expr;
1116 struct symbol *type;
1117 int state = 0;
1119 FOR_EACH_PTR(stmt->asm_outputs, expr) {
1120 switch (state) {
1121 case 0: /* identifier */
1122 case 1: /* constraint */
1123 state++;
1124 continue;
1125 case 2: /* expression */
1126 state = 0;
1127 type = get_type(strip_expr(expr));
1128 set_extra_expr_mod(expr, alloc_estate_whole(type));
1129 continue;
1131 } END_FOR_EACH_PTR(expr);
1134 static void check_dereference(struct expression *expr)
1136 struct smatch_state *state;
1138 if (__in_fake_assign)
1139 return;
1140 if (outside_of_function())
1141 return;
1142 state = get_extra_state(expr);
1143 if (state) {
1144 static struct range_list *valid_rl;
1145 struct range_list *rl;
1147 if (!valid_rl) {
1148 valid_rl = alloc_rl(valid_ptr_min_sval, valid_ptr_max_sval);
1149 valid_rl = clone_rl_permanent(valid_rl);
1152 rl = rl_intersection(estate_rl(state), valid_rl);
1153 if (rl_equiv(rl, estate_rl(state)))
1154 return;
1155 set_extra_expr_nomod(expr, alloc_estate_rl(rl));
1156 } else {
1157 set_extra_expr_nomod(expr, alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval));
1161 static void match_dereferences(struct expression *expr)
1163 if (expr->type != EXPR_PREOP)
1164 return;
1165 /* it's saying that foo[1] = bar dereferences foo[1] */
1166 if (is_array(expr))
1167 return;
1168 check_dereference(expr->unop);
1171 static void match_pointer_as_array(struct expression *expr)
1173 if (!is_array(expr))
1174 return;
1175 check_dereference(get_array_base(expr));
1178 static void find_dereferences(struct expression *expr)
1180 while (expr->type == EXPR_PREOP) {
1181 if (expr->op == '*')
1182 check_dereference(expr->unop);
1183 expr = strip_expr(expr->unop);
1187 static void set_param_dereferenced(struct expression *arg, char *key, char *unused)
1189 struct symbol *sym;
1190 char *name;
1192 name = get_variable_from_key(arg, key, &sym);
1193 if (name && sym) {
1194 struct smatch_state *orig, *new;
1195 struct range_list *rl;
1197 orig = get_state(SMATCH_EXTRA, name, sym);
1198 if (orig) {
1199 rl = rl_intersection(estate_rl(orig),
1200 alloc_rl(valid_ptr_min_sval,
1201 valid_ptr_max_sval));
1202 new = alloc_estate_rl(rl);
1203 } else {
1204 new = alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval);
1207 set_extra_nomod(name, sym, new);
1209 free_string(name);
1211 find_dereferences(arg);
1214 static sval_t add_one(sval_t sval)
1216 sval.value++;
1217 return sval;
1220 static int handle_postop_inc(struct expression *left, int op, struct expression *right)
1222 struct statement *stmt;
1223 struct expression *cond;
1224 struct smatch_state *true_state, *false_state;
1225 sval_t start;
1226 sval_t limit;
1229 * If we're decrementing here then that's a canonical while count down
1230 * so it's handled already. We're only handling loops like:
1231 * i = 0;
1232 * do { ... } while (i++ < 3);
1235 if (left->type != EXPR_POSTOP || left->op != SPECIAL_INCREMENT)
1236 return 0;
1238 stmt = __cur_stmt->parent;
1239 if (!stmt)
1240 return 0;
1241 if (stmt->type == STMT_COMPOUND)
1242 stmt = stmt->parent;
1243 if (!stmt || stmt->type != STMT_ITERATOR || !stmt->iterator_post_condition)
1244 return 0;
1246 cond = strip_expr(stmt->iterator_post_condition);
1247 if (cond->type != EXPR_COMPARE || cond->op != op)
1248 return 0;
1249 if (left != strip_expr(cond->left) || right != strip_expr(cond->right))
1250 return 0;
1252 if (!get_implied_value(left->unop, &start))
1253 return 0;
1254 if (!get_implied_value(right, &limit))
1255 return 0;
1257 if (sval_cmp(start, limit) > 0)
1258 return 0;
1260 switch (op) {
1261 case '<':
1262 case SPECIAL_UNSIGNED_LT:
1263 break;
1264 case SPECIAL_LTE:
1265 case SPECIAL_UNSIGNED_LTE:
1266 limit = add_one(limit);
1267 default:
1268 return 0;
1272 true_state = alloc_estate_range(add_one(start), limit);
1273 false_state = alloc_estate_range(add_one(limit), add_one(limit));
1275 /* Currently we just discard the false state but when two passes is
1276 * implimented correctly then it will use it.
1279 set_extra_expr_true_false(left->unop, true_state, false_state);
1281 return 1;
1284 static void handle_comparison(struct symbol *type, struct expression *left, int op, struct expression *right)
1286 struct range_list *left_orig;
1287 struct range_list *left_true;
1288 struct range_list *left_false;
1289 struct range_list *right_orig;
1290 struct range_list *right_true;
1291 struct range_list *right_false;
1292 struct smatch_state *left_true_state;
1293 struct smatch_state *left_false_state;
1294 struct smatch_state *right_true_state;
1295 struct smatch_state *right_false_state;
1296 sval_t dummy, hard_max;
1297 int left_postop = 0;
1298 int right_postop = 0;
1300 if (left->op == SPECIAL_INCREMENT || left->op == SPECIAL_DECREMENT) {
1301 if (left->type == EXPR_POSTOP) {
1302 left->smatch_flags |= Handled;
1303 left_postop = left->op;
1304 if (handle_postop_inc(left, op, right))
1305 return;
1307 left = strip_parens(left->unop);
1309 while (left->type == EXPR_ASSIGNMENT)
1310 left = strip_parens(left->left);
1312 if (right->op == SPECIAL_INCREMENT || right->op == SPECIAL_DECREMENT) {
1313 if (right->type == EXPR_POSTOP) {
1314 right->smatch_flags |= Handled;
1315 right_postop = right->op;
1317 right = strip_parens(right->unop);
1320 get_real_absolute_rl(left, &left_orig);
1321 left_orig = cast_rl(type, left_orig);
1323 get_real_absolute_rl(right, &right_orig);
1324 right_orig = cast_rl(type, right_orig);
1326 split_comparison_rl(left_orig, op, right_orig, &left_true, &left_false, &right_true, &right_false);
1328 left_true = rl_truncate_cast(get_type(strip_expr(left)), left_true);
1329 left_false = rl_truncate_cast(get_type(strip_expr(left)), left_false);
1330 right_true = rl_truncate_cast(get_type(strip_expr(right)), right_true);
1331 right_false = rl_truncate_cast(get_type(strip_expr(right)), right_false);
1333 if (!left_true || !left_false) {
1334 struct range_list *tmp_true, *tmp_false;
1336 split_comparison_rl(alloc_whole_rl(type), op, right_orig, &tmp_true, &tmp_false, NULL, NULL);
1337 tmp_true = rl_truncate_cast(get_type(strip_expr(left)), tmp_true);
1338 tmp_false = rl_truncate_cast(get_type(strip_expr(left)), tmp_false);
1339 if (tmp_true && tmp_false)
1340 __save_imaginary_state(left, tmp_true, tmp_false);
1343 if (!right_true || !right_false) {
1344 struct range_list *tmp_true, *tmp_false;
1346 split_comparison_rl(alloc_whole_rl(type), op, right_orig, NULL, NULL, &tmp_true, &tmp_false);
1347 tmp_true = rl_truncate_cast(get_type(strip_expr(right)), tmp_true);
1348 tmp_false = rl_truncate_cast(get_type(strip_expr(right)), tmp_false);
1349 if (tmp_true && tmp_false)
1350 __save_imaginary_state(right, tmp_true, tmp_false);
1353 left_true_state = alloc_estate_rl(left_true);
1354 left_false_state = alloc_estate_rl(left_false);
1355 right_true_state = alloc_estate_rl(right_true);
1356 right_false_state = alloc_estate_rl(right_false);
1358 switch (op) {
1359 case '<':
1360 case SPECIAL_UNSIGNED_LT:
1361 case SPECIAL_UNSIGNED_LTE:
1362 case SPECIAL_LTE:
1363 if (get_hard_max(right, &dummy))
1364 estate_set_hard_max(left_true_state);
1365 if (get_hard_max(left, &dummy))
1366 estate_set_hard_max(right_false_state);
1367 break;
1368 case '>':
1369 case SPECIAL_UNSIGNED_GT:
1370 case SPECIAL_UNSIGNED_GTE:
1371 case SPECIAL_GTE:
1372 if (get_hard_max(left, &dummy))
1373 estate_set_hard_max(right_true_state);
1374 if (get_hard_max(right, &dummy))
1375 estate_set_hard_max(left_false_state);
1376 break;
1379 switch (op) {
1380 case '<':
1381 case SPECIAL_UNSIGNED_LT:
1382 case SPECIAL_UNSIGNED_LTE:
1383 case SPECIAL_LTE:
1384 if (get_hard_max(right, &hard_max)) {
1385 if (op == '<' || op == SPECIAL_UNSIGNED_LT)
1386 hard_max.value--;
1387 estate_set_fuzzy_max(left_true_state, hard_max);
1389 if (get_implied_value(right, &hard_max)) {
1390 if (op == SPECIAL_UNSIGNED_LTE ||
1391 op == SPECIAL_LTE)
1392 hard_max.value++;
1393 estate_set_fuzzy_max(left_false_state, hard_max);
1395 if (get_hard_max(left, &hard_max)) {
1396 if (op == SPECIAL_UNSIGNED_LTE ||
1397 op == SPECIAL_LTE)
1398 hard_max.value--;
1399 estate_set_fuzzy_max(right_false_state, hard_max);
1401 if (get_implied_value(left, &hard_max)) {
1402 if (op == '<' || op == SPECIAL_UNSIGNED_LT)
1403 hard_max.value++;
1404 estate_set_fuzzy_max(right_true_state, hard_max);
1406 break;
1407 case '>':
1408 case SPECIAL_UNSIGNED_GT:
1409 case SPECIAL_UNSIGNED_GTE:
1410 case SPECIAL_GTE:
1411 if (get_hard_max(left, &hard_max)) {
1412 if (op == '>' || op == SPECIAL_UNSIGNED_GT)
1413 hard_max.value--;
1414 estate_set_fuzzy_max(right_true_state, hard_max);
1416 if (get_implied_value(left, &hard_max)) {
1417 if (op == SPECIAL_UNSIGNED_GTE ||
1418 op == SPECIAL_GTE)
1419 hard_max.value++;
1420 estate_set_fuzzy_max(right_false_state, hard_max);
1422 if (get_hard_max(right, &hard_max)) {
1423 if (op == SPECIAL_UNSIGNED_LTE ||
1424 op == SPECIAL_LTE)
1425 hard_max.value--;
1426 estate_set_fuzzy_max(left_false_state, hard_max);
1428 if (get_implied_value(right, &hard_max)) {
1429 if (op == '>' ||
1430 op == SPECIAL_UNSIGNED_GT)
1431 hard_max.value++;
1432 estate_set_fuzzy_max(left_true_state, hard_max);
1434 break;
1435 case SPECIAL_EQUAL:
1436 if (get_hard_max(left, &hard_max))
1437 estate_set_fuzzy_max(right_true_state, hard_max);
1438 if (get_hard_max(right, &hard_max))
1439 estate_set_fuzzy_max(left_true_state, hard_max);
1440 break;
1443 if (get_hard_max(left, &hard_max)) {
1444 estate_set_hard_max(left_true_state);
1445 estate_set_hard_max(left_false_state);
1447 if (get_hard_max(right, &hard_max)) {
1448 estate_set_hard_max(right_true_state);
1449 estate_set_hard_max(right_false_state);
1452 if (left_postop == SPECIAL_INCREMENT) {
1453 left_true_state = increment_state(left_true_state);
1454 left_false_state = increment_state(left_false_state);
1456 if (left_postop == SPECIAL_DECREMENT) {
1457 left_true_state = decrement_state(left_true_state);
1458 left_false_state = decrement_state(left_false_state);
1460 if (right_postop == SPECIAL_INCREMENT) {
1461 right_true_state = increment_state(right_true_state);
1462 right_false_state = increment_state(right_false_state);
1464 if (right_postop == SPECIAL_DECREMENT) {
1465 right_true_state = decrement_state(right_true_state);
1466 right_false_state = decrement_state(right_false_state);
1469 if (estate_rl(left_true_state) && estates_equiv(left_true_state, left_false_state)) {
1470 left_true_state = NULL;
1471 left_false_state = NULL;
1474 if (estate_rl(right_true_state) && estates_equiv(right_true_state, right_false_state)) {
1475 right_true_state = NULL;
1476 right_false_state = NULL;
1479 set_extra_expr_true_false(left, left_true_state, left_false_state);
1480 set_extra_expr_true_false(right, right_true_state, right_false_state);
1483 static int is_simple_math(struct expression *expr)
1485 if (!expr)
1486 return 0;
1487 if (expr->type != EXPR_BINOP)
1488 return 0;
1489 switch (expr->op) {
1490 case '+':
1491 case '-':
1492 case '*':
1493 return 1;
1495 return 0;
1498 static void move_known_values(struct expression **left_p, struct expression **right_p)
1500 struct expression *left = *left_p;
1501 struct expression *right = *right_p;
1502 sval_t sval, dummy;
1504 if (get_implied_value(left, &sval)) {
1505 if (!is_simple_math(right))
1506 return;
1507 if (get_implied_value(right, &dummy))
1508 return;
1509 if (right->op == '*') {
1510 sval_t divisor;
1512 if (!get_value(right->right, &divisor))
1513 return;
1514 if (divisor.value == 0 && sval.value % divisor.value)
1515 return;
1516 *left_p = binop_expression(left, invert_op(right->op), right->right);
1517 *right_p = right->left;
1518 return;
1520 if (right->op == '+' && get_value(right->left, &sval)) {
1521 *left_p = binop_expression(left, invert_op(right->op), right->left);
1522 *right_p = right->right;
1523 return;
1525 if (get_value(right->right, &sval)) {
1526 *left_p = binop_expression(left, invert_op(right->op), right->right);
1527 *right_p = right->left;
1528 return;
1530 return;
1532 if (get_implied_value(right, &sval)) {
1533 if (!is_simple_math(left))
1534 return;
1535 if (get_implied_value(left, &dummy))
1536 return;
1537 if (left->op == '*') {
1538 sval_t divisor;
1540 if (!get_value(left->right, &divisor))
1541 return;
1542 if (divisor.value == 0 && sval.value % divisor.value)
1543 return;
1544 *right_p = binop_expression(right, invert_op(left->op), left->right);
1545 *left_p = left->left;
1546 return;
1548 if (left->op == '+' && get_value(left->left, &sval)) {
1549 *right_p = binop_expression(right, invert_op(left->op), left->left);
1550 *left_p = left->right;
1551 return;
1554 if (get_value(left->right, &sval)) {
1555 *right_p = binop_expression(right, invert_op(left->op), left->right);
1556 *left_p = left->left;
1557 return;
1559 return;
1564 * The reason for do_simple_algebra() is to solve things like:
1565 * if (foo > 66 || foo + bar > 64) {
1566 * "foo" is not really a known variable so it won't be handled by
1567 * move_known_variables() but it's a super common idiom.
1570 static int do_simple_algebra(struct expression **left_p, struct expression **right_p)
1572 struct expression *left = *left_p;
1573 struct expression *right = *right_p;
1574 struct range_list *rl;
1575 sval_t tmp;
1577 if (left->type != EXPR_BINOP || left->op != '+')
1578 return 0;
1579 if (can_integer_overflow(get_type(left), left))
1580 return 0;
1581 if (!get_implied_value(right, &tmp))
1582 return 0;
1584 if (!get_implied_value(left->left, &tmp) &&
1585 get_implied_rl(left->left, &rl) &&
1586 !is_whole_rl(rl)) {
1587 *right_p = binop_expression(right, '-', left->left);
1588 *left_p = left->right;
1589 return 1;
1591 if (!get_implied_value(left->right, &tmp) &&
1592 get_implied_rl(left->right, &rl) &&
1593 !is_whole_rl(rl)) {
1594 *right_p = binop_expression(right, '-', left->right);
1595 *left_p = left->left;
1596 return 1;
1599 return 0;
1602 static int match_func_comparison(struct expression *expr)
1604 struct expression *left = strip_expr(expr->left);
1605 struct expression *right = strip_expr(expr->right);
1606 sval_t sval;
1609 * fixme: think about this harder. We should always be trying to limit
1610 * the non-call side as well. If we can't determine the limitter does
1611 * that mean we aren't querying the database and are missing important
1612 * information?
1615 if (left->type == EXPR_CALL) {
1616 if (get_implied_value(left, &sval)) {
1617 handle_comparison(get_type(expr), left, expr->op, right);
1618 return 1;
1620 function_comparison(left, expr->op, right);
1621 return 1;
1624 if (right->type == EXPR_CALL) {
1625 if (get_implied_value(right, &sval)) {
1626 handle_comparison(get_type(expr), left, expr->op, right);
1627 return 1;
1629 function_comparison(left, expr->op, right);
1630 return 1;
1633 return 0;
1636 /* Handle conditions like "if (foo + bar < foo) {" */
1637 static int handle_integer_overflow_test(struct expression *expr)
1639 struct expression *left, *right;
1640 struct symbol *type;
1641 sval_t left_min, right_min, min, max;
1643 if (expr->op != '<' && expr->op != SPECIAL_UNSIGNED_LT)
1644 return 0;
1646 left = strip_parens(expr->left);
1647 right = strip_parens(expr->right);
1649 if (left->op != '+')
1650 return 0;
1652 type = get_type(expr);
1653 if (!type)
1654 return 0;
1655 if (type_positive_bits(type) == 32) {
1656 max.type = &uint_ctype;
1657 max.uvalue = (unsigned int)-1;
1658 } else if (type_positive_bits(type) == 64) {
1659 max.type = &ulong_ctype;
1660 max.value = (unsigned long long)-1;
1661 } else {
1662 return 0;
1665 if (!expr_equiv(left->left, right) && !expr_equiv(left->right, right))
1666 return 0;
1668 get_absolute_min(left->left, &left_min);
1669 get_absolute_min(left->right, &right_min);
1670 min = sval_binop(left_min, '+', right_min);
1672 set_extra_chunk_true_false(left, NULL, alloc_estate_range(min, max));
1673 return 1;
1676 static void match_comparison(struct expression *expr)
1678 struct expression *left_orig = strip_parens(expr->left);
1679 struct expression *right_orig = strip_parens(expr->right);
1680 struct expression *left, *right;
1681 struct expression *prev;
1682 struct symbol *type;
1684 if (match_func_comparison(expr))
1685 return;
1687 type = get_type(expr);
1688 if (!type)
1689 type = &llong_ctype;
1691 if (handle_integer_overflow_test(expr))
1692 return;
1694 left = left_orig;
1695 right = right_orig;
1696 move_known_values(&left, &right);
1697 handle_comparison(type, left, expr->op, right);
1699 left = left_orig;
1700 right = right_orig;
1701 if (do_simple_algebra(&left, &right))
1702 handle_comparison(type, left, expr->op, right);
1704 prev = get_assigned_expr(left_orig);
1705 if (is_simple_math(prev) && has_variable(prev, left_orig) == 0) {
1706 left = prev;
1707 right = right_orig;
1708 move_known_values(&left, &right);
1709 handle_comparison(type, left, expr->op, right);
1712 prev = get_assigned_expr(right_orig);
1713 if (is_simple_math(prev) && has_variable(prev, right_orig) == 0) {
1714 left = left_orig;
1715 right = prev;
1716 move_known_values(&left, &right);
1717 handle_comparison(type, left, expr->op, right);
1721 static sval_t get_high_mask(sval_t known)
1723 sval_t ret;
1724 int i;
1726 ret = known;
1727 ret.value = 0;
1729 for (i = type_bits(known.type) - 1; i >= 0; i--) {
1730 if (known.uvalue & (1ULL << i))
1731 ret.uvalue |= (1ULL << i);
1732 else
1733 return ret;
1736 return ret;
1739 static void handle_AND_condition(struct expression *expr)
1741 struct range_list *orig_rl;
1742 struct range_list *true_rl = NULL;
1743 struct range_list *false_rl = NULL;
1744 sval_t known;
1745 int bit;
1747 if (get_implied_value(expr->left, &known)) {
1748 sval_t low_mask = known;
1749 sval_t high_mask;
1751 if (known.value > 0) {
1752 bit = ffsll(known.value) - 1;
1753 low_mask.uvalue = (1ULL << bit) - 1;
1754 get_absolute_rl(expr->right, &orig_rl);
1755 true_rl = remove_range(orig_rl, sval_type_val(known.type, 0), low_mask);
1757 high_mask = get_high_mask(known);
1758 if (high_mask.value) {
1759 bit = ffsll(high_mask.value) - 1;
1760 low_mask.uvalue = (1ULL << bit) - 1;
1762 get_absolute_rl(expr->left, &orig_rl);
1763 if (sval_is_negative(rl_min(orig_rl)))
1764 orig_rl = remove_range(orig_rl, sval_type_min(known.type), sval_type_val(known.type, -1));
1765 false_rl = remove_range(orig_rl, low_mask, sval_type_max(known.type));
1766 if (type_signed(high_mask.type) && type_unsigned(rl_type(false_rl))) {
1767 false_rl = remove_range(false_rl,
1768 sval_type_val(rl_type(false_rl), sval_type_max(known.type).uvalue),
1769 sval_type_val(rl_type(false_rl), -1));
1772 set_extra_expr_true_false(expr->right,
1773 true_rl ? alloc_estate_rl(true_rl) : NULL,
1774 false_rl ? alloc_estate_rl(false_rl) : NULL);
1776 return;
1779 if (get_implied_value(expr->right, &known)) {
1780 sval_t low_mask = known;
1781 sval_t high_mask;
1783 if (known.value > 0) {
1784 bit = ffsll(known.value) - 1;
1785 low_mask.uvalue = (1ULL << bit) - 1;
1786 get_absolute_rl(expr->left, &orig_rl);
1787 true_rl = remove_range(orig_rl, sval_type_val(known.type, 0), low_mask);
1789 high_mask = get_high_mask(known);
1790 if (high_mask.value) {
1791 bit = ffsll(high_mask.value) - 1;
1792 low_mask.uvalue = (1ULL << bit) - 1;
1794 get_absolute_rl(expr->left, &orig_rl);
1795 if (sval_is_negative(rl_min(orig_rl)))
1796 orig_rl = remove_range(orig_rl, sval_type_min(known.type), sval_type_val(known.type, -1));
1797 false_rl = remove_range(orig_rl, low_mask, sval_type_max(known.type));
1798 if (type_signed(high_mask.type) && type_unsigned(rl_type(false_rl))) {
1799 false_rl = remove_range(false_rl,
1800 sval_type_val(rl_type(false_rl), sval_type_max(known.type).uvalue),
1801 sval_type_val(rl_type(false_rl), -1));
1804 set_extra_expr_true_false(expr->left,
1805 true_rl ? alloc_estate_rl(true_rl) : NULL,
1806 false_rl ? alloc_estate_rl(false_rl) : NULL);
1807 return;
1811 static void handle_MOD_condition(struct expression *expr)
1813 struct range_list *orig_rl;
1814 struct range_list *true_rl;
1815 struct range_list *false_rl = NULL;
1816 sval_t right;
1817 sval_t zero = {
1818 .value = 0,
1821 if (!get_implied_value(expr->right, &right) || right.value == 0)
1822 return;
1823 get_absolute_rl(expr->left, &orig_rl);
1825 zero.type = rl_type(orig_rl);
1827 /* We're basically dorking around the min and max here */
1828 true_rl = remove_range(orig_rl, zero, zero);
1829 if (!sval_is_max(rl_max(true_rl)) &&
1830 !(rl_max(true_rl).value % right.value))
1831 true_rl = remove_range(true_rl, rl_max(true_rl), rl_max(true_rl));
1833 if (rl_equiv(true_rl, orig_rl))
1834 true_rl = NULL;
1836 if (sval_is_positive(rl_min(orig_rl)) &&
1837 (rl_max(orig_rl).value - rl_min(orig_rl).value) / right.value < 5) {
1838 sval_t add;
1839 int i;
1841 add = rl_min(orig_rl);
1842 add.value += right.value - (add.value % right.value);
1843 add.value -= right.value;
1845 for (i = 0; i < 5; i++) {
1846 add.value += right.value;
1847 if (add.value > rl_max(orig_rl).value)
1848 break;
1849 add_range(&false_rl, add, add);
1851 } else {
1852 if (rl_min(orig_rl).uvalue != 0 &&
1853 rl_min(orig_rl).uvalue < right.uvalue) {
1854 sval_t chop = right;
1855 chop.value--;
1856 false_rl = remove_range(orig_rl, zero, chop);
1859 if (!sval_is_max(rl_max(orig_rl)) &&
1860 (rl_max(orig_rl).value % right.value)) {
1861 sval_t chop = rl_max(orig_rl);
1862 chop.value -= chop.value % right.value;
1863 chop.value++;
1864 if (!false_rl)
1865 false_rl = clone_rl(orig_rl);
1866 false_rl = remove_range(false_rl, chop, rl_max(orig_rl));
1870 set_extra_expr_true_false(expr->left,
1871 true_rl ? alloc_estate_rl(true_rl) : NULL,
1872 false_rl ? alloc_estate_rl(false_rl) : NULL);
1875 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
1876 void __extra_match_condition(struct expression *expr)
1878 struct smatch_state *pre_state;
1879 struct smatch_state *true_state;
1880 struct smatch_state *false_state;
1881 struct range_list *pre_rl;
1883 expr = strip_expr(expr);
1884 switch (expr->type) {
1885 case EXPR_CALL:
1886 function_comparison(expr, SPECIAL_NOTEQUAL, zero_expr());
1887 return;
1888 case EXPR_PREOP:
1889 case EXPR_SYMBOL:
1890 case EXPR_DEREF: {
1891 sval_t zero;
1893 zero = sval_blank(expr);
1894 zero.value = 0;
1896 pre_state = get_extra_state(expr);
1897 if (estate_is_empty(pre_state))
1898 return;
1899 if (pre_state)
1900 pre_rl = estate_rl(pre_state);
1901 else
1902 get_absolute_rl(expr, &pre_rl);
1903 if (possibly_true_rl(pre_rl, SPECIAL_EQUAL, rl_zero()))
1904 false_state = alloc_estate_sval(zero);
1905 else
1906 false_state = alloc_estate_empty();
1907 true_state = alloc_estate_rl(remove_range(pre_rl, zero, zero));
1908 set_extra_expr_true_false(expr, true_state, false_state);
1909 return;
1911 case EXPR_COMPARE:
1912 match_comparison(expr);
1913 return;
1914 case EXPR_ASSIGNMENT:
1915 __extra_match_condition(expr->left);
1916 return;
1917 case EXPR_BINOP:
1918 if (expr->op == '&')
1919 handle_AND_condition(expr);
1920 if (expr->op == '%')
1921 handle_MOD_condition(expr);
1922 return;
1926 static void assume_indexes_are_valid(struct expression *expr)
1928 struct expression *array_expr;
1929 int array_size;
1930 struct expression *offset;
1931 struct symbol *offset_type;
1932 struct range_list *rl_before;
1933 struct range_list *rl_after;
1934 struct range_list *filter = NULL;
1935 sval_t size;
1937 expr = strip_expr(expr);
1938 if (!is_array(expr))
1939 return;
1941 offset = get_array_offset(expr);
1942 offset_type = get_type(offset);
1943 if (offset_type && type_signed(offset_type)) {
1944 filter = alloc_rl(sval_type_min(offset_type),
1945 sval_type_val(offset_type, -1));
1948 array_expr = get_array_base(expr);
1949 array_size = get_real_array_size(array_expr);
1950 if (array_size > 1) {
1951 size = sval_type_val(offset_type, array_size);
1952 add_range(&filter, size, sval_type_max(offset_type));
1955 if (!filter)
1956 return;
1957 get_absolute_rl(offset, &rl_before);
1958 rl_after = rl_filter(rl_before, filter);
1959 if (rl_equiv(rl_before, rl_after))
1960 return;
1961 set_extra_expr_nomod(offset, alloc_estate_rl(rl_after));
1964 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
1965 int implied_not_equal(struct expression *expr, long long val)
1967 return !possibly_false(expr, SPECIAL_NOTEQUAL, value_expr(val));
1970 int implied_not_equal_name_sym(char *name, struct symbol *sym, long long val)
1972 struct smatch_state *estate;
1974 estate = get_state(SMATCH_EXTRA, name, sym);
1975 if (!estate)
1976 return 0;
1977 if (!rl_has_sval(estate_rl(estate), sval_type_val(estate_type(estate), 0)))
1978 return 1;
1979 return 0;
1982 int parent_is_null_var_sym(const char *name, struct symbol *sym)
1984 char buf[256];
1985 char *start;
1986 char *end;
1987 struct smatch_state *state;
1989 strncpy(buf, name, sizeof(buf) - 1);
1990 buf[sizeof(buf) - 1] = '\0';
1992 start = &buf[0];
1993 while (*start == '*') {
1994 start++;
1995 state = get_state(SMATCH_EXTRA, start, sym);
1996 if (!state)
1997 continue;
1998 if (!estate_rl(state))
1999 return 1;
2000 if (estate_min(state).value == 0 &&
2001 estate_max(state).value == 0)
2002 return 1;
2005 start = &buf[0];
2006 while (*start == '&')
2007 start++;
2009 while ((end = strrchr(start, '-'))) {
2010 *end = '\0';
2011 state = get_state(SMATCH_EXTRA, start, sym);
2012 if (!state)
2013 continue;
2014 if (estate_min(state).value == 0 &&
2015 estate_max(state).value == 0)
2016 return 1;
2018 return 0;
2021 int parent_is_null(struct expression *expr)
2023 struct symbol *sym;
2024 char *var;
2025 int ret = 0;
2027 expr = strip_expr(expr);
2028 var = expr_to_var_sym(expr, &sym);
2029 if (!var || !sym)
2030 goto free;
2031 ret = parent_is_null_var_sym(var, sym);
2032 free:
2033 free_string(var);
2034 return ret;
2037 static int param_used_callback(void *found, int argc, char **argv, char **azColName)
2039 *(int *)found = 1;
2040 return 0;
2043 static int filter_unused_kzalloc_info(struct expression *call, int param, char *printed_name, struct sm_state *sm)
2045 sval_t sval;
2046 int found = 0;
2048 /* for function pointers assume everything is used */
2049 if (call->fn->type != EXPR_SYMBOL)
2050 return 0;
2053 * kzalloc() information is treated as special because so there is just
2054 * a lot of stuff initialized to zero and it makes building the database
2055 * take hours and hours.
2057 * In theory, we should just remove this line and not pass any unused
2058 * information, but I'm not sure enough that this code works so I want
2059 * to hold off on that for now.
2061 if (!estate_get_single_value(sm->state, &sval) || sval.value != 0)
2062 return 0;
2064 run_sql(&param_used_callback, &found,
2065 "select * from call_implies where %s and type = %d and parameter = %d and key = '%s';",
2066 get_static_filter(call->fn->symbol), PARAM_USED, param, printed_name);
2067 if (found)
2068 return 0;
2070 /* If the database is not built yet, then assume everything is used */
2071 run_sql(&param_used_callback, &found,
2072 "select * from call_implies where %s and type = %d;",
2073 get_static_filter(call->fn->symbol), PARAM_USED);
2074 if (!found)
2075 return 0;
2077 return 1;
2080 struct range_list *intersect_with_real_abs_var_sym(const char *name, struct symbol *sym, struct range_list *start)
2082 struct smatch_state *state;
2085 * Here is the difference between implied value and real absolute, say
2086 * you have:
2088 * int a = (u8)x;
2090 * Then you know that a is 0-255. That's real absolute. But you don't
2091 * know for sure that it actually goes up to 255. So it's not implied.
2092 * Implied indicates a degree of certainty.
2094 * But then say you cap "a" at 8. That means you know it goes up to
2095 * 8. So now the implied value is s32min-8. But you can combine it
2096 * with the real absolute to say that actually it's 0-8.
2098 * We are combining it here. But now that I think about it, this is
2099 * probably not the ideal place to combine it because it should proably
2100 * be done earlier. Oh well, this is an improvement on what was there
2101 * before so I'm going to commit this code.
2105 state = get_real_absolute_state_var_sym(name, sym);
2106 if (!state || !estate_rl(state))
2107 return start;
2109 return rl_intersection(estate_rl(state), start);
2112 struct range_list *intersect_with_real_abs_expr(struct expression *expr, struct range_list *start)
2114 struct smatch_state *state;
2116 state = get_real_absolute_state(expr);
2117 if (!state || !estate_rl(state))
2118 return start;
2120 return rl_intersection(estate_rl(state), start);
2123 static void struct_member_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
2125 struct range_list *rl;
2127 if (estate_is_whole(sm->state))
2128 return;
2129 if (filter_unused_kzalloc_info(call, param, printed_name, sm))
2130 return;
2131 rl = estate_rl(sm->state);
2132 rl = intersect_with_real_abs_var_sym(sm->name, sm->sym, rl);
2133 sql_insert_caller_info(call, PARAM_VALUE, param, printed_name, show_rl(rl));
2134 if (estate_has_fuzzy_max(sm->state))
2135 sql_insert_caller_info(call, FUZZY_MAX, param, printed_name,
2136 sval_to_str(estate_get_fuzzy_max(sm->state)));
2139 static void returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
2141 struct symbol *returned_sym;
2142 struct sm_state *sm;
2143 const char *param_name;
2144 char *compare_str;
2145 char buf[256];
2147 returned_sym = expr_to_sym(expr);
2148 if (!returned_sym)
2149 return;
2151 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
2152 if (!estate_rl(sm->state))
2153 continue;
2154 if (returned_sym != sm->sym)
2155 continue;
2157 param_name = get_param_name(sm);
2158 if (!param_name)
2159 continue;
2160 if (strcmp(param_name, "$") == 0)
2161 continue;
2162 compare_str = name_sym_to_param_comparison(sm->name, sm->sym);
2163 if (!compare_str && estate_is_whole(sm->state))
2164 continue;
2165 snprintf(buf, sizeof(buf), "%s%s", sm->state->name, compare_str ?: "");
2167 sql_insert_return_states(return_id, return_ranges, PARAM_VALUE,
2168 -1, param_name, buf);
2169 } END_FOR_EACH_SM(sm);
2172 static void db_limited_before(void)
2174 unmatched_stree = clone_stree(__get_cur_stree());
2177 static void db_limited_after(void)
2179 free_stree(&unmatched_stree);
2182 static int rl_fits_in_type(struct range_list *rl, struct symbol *type)
2184 if (type_bits(rl_type(rl)) <= type_bits(type))
2185 return 1;
2186 if (sval_cmp(rl_max(rl), sval_type_max(type)) > 0)
2187 return 0;
2188 if (sval_is_negative(rl_min(rl)) &&
2189 sval_cmp(rl_min(rl), sval_type_min(type)) < 0)
2190 return 0;
2191 return 1;
2194 static int basically_the_same(struct range_list *orig, struct range_list *new)
2196 if (rl_equiv(orig, new))
2197 return 1;
2200 * The whole range is essentially the same as 0,4096-27777777777 so
2201 * don't overwrite the implications just to store that.
2204 if (rl_type(orig)->type == SYM_PTR &&
2205 is_whole_rl(orig) &&
2206 rl_min(new).value == 0 &&
2207 rl_max(new).value == valid_ptr_max)
2208 return 1;
2209 return 0;
2212 static void db_param_limit_filter(struct expression *expr, int param, char *key, char *value, enum info_type op)
2214 struct expression *arg;
2215 char *name;
2216 struct symbol *sym;
2217 struct var_sym_list *vsl = NULL;
2218 struct sm_state *sm;
2219 struct symbol *compare_type, *var_type;
2220 struct range_list *rl;
2221 struct range_list *limit;
2222 struct range_list *new;
2224 while (expr->type == EXPR_ASSIGNMENT)
2225 expr = strip_expr(expr->right);
2226 if (expr->type != EXPR_CALL)
2227 return;
2229 arg = get_argument_from_call_expr(expr->args, param);
2230 if (!arg)
2231 return;
2233 name = get_chunk_from_key(arg, key, &sym, &vsl);
2234 if (!name)
2235 return;
2236 if (op != PARAM_LIMIT && !sym)
2237 goto free;
2239 if (strcmp(key, "$") == 0)
2240 compare_type = get_arg_type(expr->fn, param);
2241 else
2242 compare_type = get_member_type_from_key(arg, key);
2244 sm = get_sm_state(SMATCH_EXTRA, name, sym);
2245 if (sm)
2246 rl = estate_rl(sm->state);
2247 else
2248 rl = alloc_whole_rl(compare_type);
2250 if (op == PARAM_LIMIT && !rl_fits_in_type(rl, compare_type))
2251 goto free;
2253 call_results_to_rl(expr, compare_type, value, &limit);
2254 new = rl_intersection(rl, limit);
2256 var_type = get_member_type_from_key(arg, key);
2257 new = cast_rl(var_type, new);
2259 /* We want to preserve the implications here */
2260 if (sm && basically_the_same(estate_rl(sm->state), new))
2261 __set_sm(sm); /* FIXME: Is this really necessary? */
2262 else {
2263 char *tmp_name;
2264 struct symbol *tmp_sym;
2266 tmp_name = map_long_to_short_name_sym(name, sym, &tmp_sym);
2267 if (tmp_name && tmp_sym) {
2268 free_string(name);
2269 name = tmp_name;
2270 sym = tmp_sym;
2273 if (op == PARAM_LIMIT)
2274 set_extra_nomod_vsl(name, sym, vsl, alloc_estate_rl(new));
2275 else
2276 set_extra_mod(name, sym, alloc_estate_rl(new));
2279 free:
2280 free_string(name);
2283 static void db_param_limit(struct expression *expr, int param, char *key, char *value)
2285 db_param_limit_filter(expr, param, key, value, PARAM_LIMIT);
2288 static void db_param_filter(struct expression *expr, int param, char *key, char *value)
2290 db_param_limit_filter(expr, param, key, value, PARAM_FILTER);
2293 static void db_param_add_set(struct expression *expr, int param, char *key, char *value, enum info_type op)
2295 struct expression *arg;
2296 char *name, *tmp_name;
2297 struct symbol *sym, *tmp_sym;
2298 struct symbol *type;
2299 struct smatch_state *state;
2300 struct range_list *new = NULL;
2301 struct range_list *added = NULL;
2303 while (expr->type == EXPR_ASSIGNMENT)
2304 expr = strip_expr(expr->right);
2305 if (expr->type != EXPR_CALL)
2306 return;
2308 arg = get_argument_from_call_expr(expr->args, param);
2309 if (!arg)
2310 return;
2311 type = get_member_type_from_key(arg, key);
2312 name = get_variable_from_key(arg, key, &sym);
2313 if (!name || !sym)
2314 goto free;
2316 state = get_state(SMATCH_EXTRA, name, sym);
2317 if (state)
2318 new = estate_rl(state);
2320 call_results_to_rl(expr, type, value, &added);
2322 if (op == PARAM_SET)
2323 new = added;
2324 else
2325 new = rl_union(new, added);
2327 tmp_name = map_long_to_short_name_sym_nostack(name, sym, &tmp_sym);
2328 if (tmp_name && tmp_sym) {
2329 free_string(name);
2330 name = tmp_name;
2331 sym = tmp_sym;
2333 set_extra_mod(name, sym, alloc_estate_rl(new));
2334 free:
2335 free_string(name);
2338 static void db_param_add(struct expression *expr, int param, char *key, char *value)
2340 in_param_set = true;
2341 db_param_add_set(expr, param, key, value, PARAM_ADD);
2342 in_param_set = false;
2345 static void db_param_set(struct expression *expr, int param, char *key, char *value)
2347 in_param_set = true;
2348 db_param_add_set(expr, param, key, value, PARAM_SET);
2349 in_param_set = false;
2352 static void db_param_value(struct expression *expr, int param, char *key, char *value)
2354 struct expression *call;
2355 char *name;
2356 struct symbol *sym;
2357 struct symbol *type;
2358 struct range_list *rl = NULL;
2360 if (param != -1)
2361 return;
2363 call = expr;
2364 while (call->type == EXPR_ASSIGNMENT)
2365 call = strip_expr(call->right);
2366 if (call->type != EXPR_CALL)
2367 return;
2369 type = get_member_type_from_key(expr->left, key);
2370 name = get_variable_from_key(expr->left, key, &sym);
2371 if (!name || !sym)
2372 goto free;
2374 call_results_to_rl(call, type, value, &rl);
2376 set_extra_mod(name, sym, alloc_estate_rl(rl));
2377 free:
2378 free_string(name);
2381 static void match_call_info(struct expression *expr)
2383 struct smatch_state *state;
2384 struct range_list *rl = NULL;
2385 struct expression *arg;
2386 struct symbol *type;
2387 int i = 0;
2389 FOR_EACH_PTR(expr->args, arg) {
2390 type = get_arg_type(expr->fn, i);
2392 if (get_implied_rl(arg, &rl))
2393 rl = cast_rl(type, rl);
2394 else
2395 rl = cast_rl(type, alloc_whole_rl(get_type(arg)));
2397 if (!is_whole_rl(rl)) {
2398 rl = intersect_with_real_abs_expr(arg, rl);
2399 sql_insert_caller_info(expr, PARAM_VALUE, i, "$", show_rl(rl));
2401 state = get_state_expr(SMATCH_EXTRA, arg);
2402 if (estate_has_fuzzy_max(state)) {
2403 sql_insert_caller_info(expr, FUZZY_MAX, i, "$",
2404 sval_to_str(estate_get_fuzzy_max(state)));
2406 i++;
2407 } END_FOR_EACH_PTR(arg);
2410 static void set_param_value(const char *name, struct symbol *sym, char *key, char *value)
2412 struct range_list *rl = NULL;
2413 struct smatch_state *state;
2414 struct symbol *type;
2415 char fullname[256];
2416 sval_t dummy;
2418 if (strcmp(key, "*$") == 0)
2419 snprintf(fullname, sizeof(fullname), "*%s", name);
2420 else if (strncmp(key, "$", 1) == 0)
2421 snprintf(fullname, 256, "%s%s", name, key + 1);
2422 else
2423 return;
2425 type = get_member_type_from_key(symbol_expression(sym), key);
2426 str_to_rl(type, value, &rl);
2427 state = alloc_estate_rl(rl);
2428 if (estate_get_single_value(state, &dummy))
2429 estate_set_hard_max(state);
2430 set_state(SMATCH_EXTRA, fullname, sym, state);
2433 static void set_param_hard_max(const char *name, struct symbol *sym, char *key, char *value)
2435 struct range_list *rl = NULL;
2436 struct smatch_state *state;
2437 struct symbol *type;
2438 char fullname[256];
2439 sval_t max;
2441 if (strcmp(key, "*$") == 0)
2442 snprintf(fullname, sizeof(fullname), "*%s", name);
2443 else if (strncmp(key, "$", 1) == 0)
2444 snprintf(fullname, 256, "%s%s", name, key + 1);
2445 else
2446 return;
2448 state = get_state(SMATCH_EXTRA, fullname, sym);
2449 if (!state)
2450 return;
2451 type = get_member_type_from_key(symbol_expression(sym), key);
2452 str_to_rl(type, value, &rl);
2453 if (!rl_to_sval(rl, &max))
2454 return;
2455 estate_set_fuzzy_max(state, max);
2458 struct smatch_state *get_extra_state(struct expression *expr)
2460 char *name;
2461 struct symbol *sym;
2462 struct smatch_state *ret = NULL;
2463 struct range_list *rl;
2465 if (is_pointer(expr) && get_address_rl(expr, &rl))
2466 return alloc_estate_rl(rl);
2468 name = expr_to_known_chunk_sym(expr, &sym);
2469 if (!name)
2470 goto free;
2472 ret = get_state(SMATCH_EXTRA, name, sym);
2473 free:
2474 free_string(name);
2475 return ret;
2478 void register_smatch_extra(int id)
2480 my_id = id;
2482 add_merge_hook(my_id, &merge_estates);
2483 add_unmatched_state_hook(my_id, &unmatched_state);
2484 select_caller_info_hook(set_param_value, PARAM_VALUE);
2485 select_caller_info_hook(set_param_hard_max, FUZZY_MAX);
2486 select_return_states_before(&db_limited_before);
2487 select_return_states_hook(PARAM_LIMIT, &db_param_limit);
2488 select_return_states_hook(PARAM_FILTER, &db_param_filter);
2489 select_return_states_hook(PARAM_ADD, &db_param_add);
2490 select_return_states_hook(PARAM_SET, &db_param_set);
2491 select_return_states_hook(PARAM_VALUE, &db_param_value);
2492 select_return_states_after(&db_limited_after);
2495 static void match_link_modify(struct sm_state *sm, struct expression *mod_expr)
2497 struct var_sym_list *links;
2498 struct var_sym *tmp;
2499 struct smatch_state *state;
2501 links = sm->state->data;
2503 FOR_EACH_PTR(links, tmp) {
2504 if (sm->sym == tmp->sym &&
2505 strcmp(sm->name, tmp->var) == 0)
2506 continue;
2507 state = get_state(SMATCH_EXTRA, tmp->var, tmp->sym);
2508 if (!state)
2509 continue;
2510 set_state(SMATCH_EXTRA, tmp->var, tmp->sym, alloc_estate_whole(estate_type(state)));
2511 } END_FOR_EACH_PTR(tmp);
2512 set_state(link_id, sm->name, sm->sym, &undefined);
2515 void register_smatch_extra_links(int id)
2517 link_id = id;
2520 void register_smatch_extra_late(int id)
2522 add_merge_hook(link_id, &merge_link_states);
2523 add_modification_hook(link_id, &match_link_modify);
2524 add_hook(&match_dereferences, DEREF_HOOK);
2525 add_hook(&match_pointer_as_array, OP_HOOK);
2526 select_call_implies_hook(DEREFERENCE, &set_param_dereferenced);
2527 add_hook(&match_function_call, FUNCTION_CALL_HOOK);
2528 add_hook(&match_assign, ASSIGNMENT_HOOK);
2529 add_hook(&match_assign, GLOBAL_ASSIGNMENT_HOOK);
2530 add_hook(&unop_expr, OP_HOOK);
2531 add_hook(&asm_expr, ASM_HOOK);
2532 add_untracked_param_hook(&match_untracked_array);
2534 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
2535 add_member_info_callback(my_id, struct_member_callback);
2536 add_split_return_callback(&returned_struct_members);
2538 add_hook(&assume_indexes_are_valid, OP_HOOK);