implied: fix a type bug
[smatch.git] / smatch_extra.c
blob84f54f97a4233b02bcb5c5b074f678f3ce02ae0a
1 /*
2 * Copyright (C) 2008 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
19 * smatch_extra.c is supposed to track the value of every variable.
23 #include <stdlib.h>
24 #include <errno.h>
25 #ifndef __USE_ISOC99
26 #define __USE_ISOC99
27 #endif
28 #include <limits.h>
29 #include "parse.h"
30 #include "smatch.h"
31 #include "smatch_slist.h"
32 #include "smatch_extra.h"
34 static int my_id;
35 static int link_id;
37 static void match_link_modify(struct sm_state *sm, struct expression *mod_expr);
39 struct string_list *__ignored_macros = NULL;
40 static int in_warn_on_macro(void)
42 struct statement *stmt;
43 char *tmp;
44 char *macro;
46 stmt = get_current_statement();
47 if (!stmt)
48 return 0;
49 macro = get_macro_name(stmt->pos);
50 if (!macro)
51 return 0;
53 FOR_EACH_PTR(__ignored_macros, tmp) {
54 if (!strcmp(tmp, macro))
55 return 1;
56 } END_FOR_EACH_PTR(tmp);
57 return 0;
60 typedef void (mod_hook)(const char *name, struct symbol *sym, struct smatch_state *state);
61 DECLARE_PTR_LIST(void_fn_list, mod_hook *);
62 static struct void_fn_list *extra_mod_hooks;
63 void add_extra_mod_hook(mod_hook *fn)
65 mod_hook **p = malloc(sizeof(mod_hook *));
66 *p = fn;
67 add_ptr_list(&extra_mod_hooks, p);
70 void call_extra_mod_hooks(const char *name, struct symbol *sym, struct smatch_state *state)
72 mod_hook **fn;
74 FOR_EACH_PTR(extra_mod_hooks, fn) {
75 (*fn)(name, sym, state);
76 } END_FOR_EACH_PTR(fn);
79 struct sm_state *set_extra_mod_helper(const char *name, struct symbol *sym, struct smatch_state *state)
81 remove_from_equiv(name, sym);
82 call_extra_mod_hooks(name, sym, state);
83 if (__in_fake_assign && estate_is_unknown(state) && !get_state(SMATCH_EXTRA, name, sym))
84 return NULL;
85 return set_state(SMATCH_EXTRA, name, sym, state);
88 static char *get_other_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym)
90 struct expression *assigned;
91 char *orig_name = NULL;
92 char buf[256];
93 char *ret = NULL;
94 int skip;
96 *new_sym = NULL;
98 if (!sym->ident)
99 return NULL;
101 skip = strlen(sym->ident->name);
102 if (name[skip] != '-' || name[skip + 1] != '>')
103 return NULL;
104 skip += 2;
106 assigned = get_assigned_expr_name_sym(sym->ident->name, sym);
107 if (!assigned)
108 return NULL;
109 if (assigned->type == EXPR_PREOP || assigned->op == '&') {
111 orig_name = expr_to_var_sym(assigned, new_sym);
112 if (!orig_name || !*new_sym)
113 goto free;
115 snprintf(buf, sizeof(buf), "%s.%s", orig_name + 1, name + skip);
116 ret = alloc_string(buf);
117 free_string(orig_name);
118 return ret;
121 if (assigned->type != EXPR_DEREF)
122 goto free;
124 orig_name = expr_to_var_sym(assigned, new_sym);
125 if (!orig_name || !*new_sym)
126 goto free;
128 snprintf(buf, sizeof(buf), "%s->%s", orig_name, name + skip);
129 ret = alloc_string(buf);
130 free_string(orig_name);
131 return ret;
133 free:
134 free_string(orig_name);
135 return NULL;
138 struct sm_state *set_extra_mod(const char *name, struct symbol *sym, struct smatch_state *state)
140 char *new_name;
141 struct symbol *new_sym;
142 struct sm_state *sm;
144 sm = set_extra_mod_helper(name, sym, state);
145 new_name = get_other_name_sym(name, sym, &new_sym);
146 if (new_name && new_sym)
147 set_extra_mod_helper(new_name, new_sym, state);
148 free_string(new_name);
149 return sm;
152 static void clear_array_states(struct expression *array)
154 struct sm_state *sm;
156 sm = get_sm_state_expr(link_id, array);
157 if (sm)
158 match_link_modify(sm, NULL);
161 static struct sm_state *set_extra_array_mod(struct expression *expr, struct smatch_state *state)
163 struct expression *array;
164 struct var_sym_list *vsl;
165 struct var_sym *vs;
166 char *name;
167 struct symbol *sym;
168 struct sm_state *ret = NULL;
170 array = get_array_base(expr);
172 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
173 if (!name || !vsl) {
174 clear_array_states(array);
175 goto free;
178 FOR_EACH_PTR(vsl, vs) {
179 store_link(link_id, vs->var, vs->sym, name, sym);
180 } END_FOR_EACH_PTR(vs);
182 call_extra_mod_hooks(name, sym, state);
183 ret = set_state(SMATCH_EXTRA, name, sym, state);
184 free:
185 free_string(name);
186 return ret;
189 struct sm_state *set_extra_expr_mod(struct expression *expr, struct smatch_state *state)
191 struct symbol *sym;
192 char *name;
193 struct sm_state *ret = NULL;
195 if (is_array(expr))
196 return set_extra_array_mod(expr, state);
198 expr = strip_expr(expr);
199 name = expr_to_var_sym(expr, &sym);
200 if (!name || !sym)
201 goto free;
202 ret = set_extra_mod(name, sym, state);
203 free:
204 free_string(name);
205 return ret;
208 void set_extra_nomod(const char *name, struct symbol *sym, struct smatch_state *state)
210 char *new_name;
211 struct symbol *new_sym;
212 struct relation *rel;
213 struct smatch_state *orig_state;
215 orig_state = get_state(SMATCH_EXTRA, name, sym);
217 new_name = get_other_name_sym(name, sym, &new_sym);
218 if (new_name && new_sym)
219 set_state(SMATCH_EXTRA, new_name, new_sym, state);
220 free_string(new_name);
222 if (!estate_related(orig_state)) {
223 set_state(SMATCH_EXTRA, name, sym, state);
224 return;
227 set_related(state, estate_related(orig_state));
228 FOR_EACH_PTR(estate_related(orig_state), rel) {
229 struct smatch_state *estate;
231 if (option_debug_related)
232 sm_msg("%s updating related %s to %s", name, rel->name, state->name);
233 estate = get_state(SMATCH_EXTRA, rel->name, rel->sym);
234 if (!estate)
235 continue;
236 set_state(SMATCH_EXTRA, rel->name, rel->sym, clone_estate_cast(estate_type(estate), state));
237 } END_FOR_EACH_PTR(rel);
241 * This is for return_implies_state() hooks which modify a SMATCH_EXTRA state
243 void set_extra_expr_nomod(struct expression *expr, struct smatch_state *state)
245 char *name;
246 struct symbol *sym;
248 name = expr_to_var_sym(expr, &sym);
249 if (!name || !sym)
250 goto free;
251 set_extra_nomod(name, sym, state);
252 free:
253 free_string(name);
257 static void set_extra_true_false(const char *name, struct symbol *sym,
258 struct smatch_state *true_state,
259 struct smatch_state *false_state)
261 char *new_name;
262 struct symbol *new_sym;
263 struct relation *rel;
264 struct smatch_state *orig_state;
266 if (!true_state && !false_state)
267 return;
269 if (in_warn_on_macro())
270 return;
272 new_name = get_other_name_sym(name, sym, &new_sym);
273 if (new_name && new_sym)
274 set_true_false_states(SMATCH_EXTRA, new_name, new_sym, true_state, false_state);
275 free_string(new_name);
277 orig_state = get_state(SMATCH_EXTRA, name, sym);
279 if (!estate_related(orig_state)) {
280 set_true_false_states(SMATCH_EXTRA, name, sym, true_state, false_state);
281 return;
284 if (true_state)
285 set_related(true_state, estate_related(orig_state));
286 if (false_state)
287 set_related(false_state, estate_related(orig_state));
289 FOR_EACH_PTR(estate_related(orig_state), rel) {
290 set_true_false_states(SMATCH_EXTRA, rel->name, rel->sym,
291 true_state, false_state);
292 } END_FOR_EACH_PTR(rel);
295 static void set_extra_chunk_true_false(struct expression *expr,
296 struct smatch_state *true_state,
297 struct smatch_state *false_state)
299 struct var_sym_list *vsl;
300 struct var_sym *vs;
301 struct symbol *type;
302 char *name;
303 struct symbol *sym;
305 if (in_warn_on_macro())
306 return;
308 type = get_type(expr);
309 if (!type)
310 return;
312 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
313 if (!name || !vsl)
314 goto free;
315 FOR_EACH_PTR(vsl, vs) {
316 store_link(link_id, vs->var, vs->sym, name, sym);
317 } END_FOR_EACH_PTR(vs);
319 set_true_false_states(SMATCH_EXTRA, name, sym,
320 clone_estate(true_state),
321 clone_estate(false_state));
322 free:
323 free_string(name);
326 static void set_extra_expr_true_false(struct expression *expr,
327 struct smatch_state *true_state,
328 struct smatch_state *false_state)
330 char *name;
331 struct symbol *sym;
332 sval_t sval;
334 if (!true_state && !false_state)
335 return;
337 if (get_value(expr, &sval))
338 return;
340 expr = strip_expr(expr);
341 name = expr_to_var_sym(expr, &sym);
342 if (!name || !sym) {
343 free_string(name);
344 set_extra_chunk_true_false(expr, true_state, false_state);
345 return;
347 set_extra_true_false(name, sym, true_state, false_state);
348 free_string(name);
351 static struct sm_state *handle_canonical_while_count_down(struct statement *loop)
353 struct expression *iter_var;
354 struct expression *condition;
355 struct sm_state *sm;
356 struct smatch_state *estate;
357 sval_t start;
359 condition = strip_expr(loop->iterator_pre_condition);
360 if (!condition)
361 return NULL;
362 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
363 return NULL;
364 if (condition->op != SPECIAL_DECREMENT)
365 return NULL;
367 iter_var = condition->unop;
368 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
369 if (!sm)
370 return NULL;
371 if (sval_cmp_val(estate_min(sm->state), 0) < 0)
372 return NULL;
373 start = estate_max(sm->state);
374 if (sval_cmp_val(start, 0) <= 0)
375 return NULL;
376 if (!sval_is_max(start))
377 start.value--;
379 if (condition->type == EXPR_PREOP) {
380 estate = alloc_estate_range(sval_type_val(start.type, 1), start);
381 if (estate_has_hard_max(sm->state))
382 estate_set_hard_max(estate);
383 estate_copy_fuzzy_max(estate, sm->state);
384 set_extra_expr_mod(iter_var, estate);
386 if (condition->type == EXPR_POSTOP) {
387 estate = alloc_estate_range(sval_type_val(start.type, 0), start);
388 if (estate_has_hard_max(sm->state))
389 estate_set_hard_max(estate);
390 estate_copy_fuzzy_max(estate, sm->state);
391 set_extra_expr_mod(iter_var, estate);
393 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
396 static struct sm_state *handle_canonical_for_inc(struct expression *iter_expr,
397 struct expression *condition)
399 struct expression *iter_var;
400 struct sm_state *sm;
401 struct smatch_state *estate;
402 sval_t start, end, max;
404 iter_var = iter_expr->unop;
405 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
406 if (!sm)
407 return NULL;
408 if (!estate_get_single_value(sm->state, &start))
409 return NULL;
410 if (get_implied_max(condition->right, &end))
411 end = sval_cast(get_type(iter_var), end);
412 else
413 end = sval_type_max(get_type(iter_var));
415 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
416 return NULL;
418 switch (condition->op) {
419 case SPECIAL_UNSIGNED_LT:
420 case SPECIAL_NOTEQUAL:
421 case '<':
422 if (!sval_is_min(end))
423 end.value--;
424 break;
425 case SPECIAL_UNSIGNED_LTE:
426 case SPECIAL_LTE:
427 break;
428 default:
429 return NULL;
431 if (sval_cmp(end, start) < 0)
432 return NULL;
433 estate = alloc_estate_range(start, end);
434 if (get_hard_max(condition->right, &max)) {
435 estate_set_hard_max(estate);
436 if (condition->op == '<' ||
437 condition->op == SPECIAL_UNSIGNED_LT ||
438 condition->op == SPECIAL_NOTEQUAL)
439 max.value--;
440 estate_set_fuzzy_max(estate, max);
442 set_extra_expr_mod(iter_var, estate);
443 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
446 static struct sm_state *handle_canonical_for_dec(struct expression *iter_expr,
447 struct expression *condition)
449 struct expression *iter_var;
450 struct sm_state *sm;
451 struct smatch_state *estate;
452 sval_t start, end;
454 iter_var = iter_expr->unop;
455 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
456 if (!sm)
457 return NULL;
458 if (!estate_get_single_value(sm->state, &start))
459 return NULL;
460 if (!get_implied_min(condition->right, &end))
461 end = sval_type_min(get_type(iter_var));
462 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
463 return NULL;
465 switch (condition->op) {
466 case SPECIAL_NOTEQUAL:
467 case '>':
468 if (!sval_is_min(end) && !sval_is_max(end))
469 end.value++;
470 break;
471 case SPECIAL_GTE:
472 break;
473 default:
474 return NULL;
476 if (sval_cmp(end, start) > 0)
477 return NULL;
478 estate = alloc_estate_range(end, start);
479 estate_set_hard_max(estate);
480 estate_set_fuzzy_max(estate, estate_get_fuzzy_max(estate));
481 set_extra_expr_mod(iter_var, estate);
482 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
485 static struct sm_state *handle_canonical_for_loops(struct statement *loop)
487 struct expression *iter_expr;
488 struct expression *condition;
490 if (!loop->iterator_post_statement)
491 return NULL;
492 if (loop->iterator_post_statement->type != STMT_EXPRESSION)
493 return NULL;
494 iter_expr = loop->iterator_post_statement->expression;
495 if (!loop->iterator_pre_condition)
496 return NULL;
497 if (loop->iterator_pre_condition->type != EXPR_COMPARE)
498 return NULL;
499 condition = loop->iterator_pre_condition;
501 if (iter_expr->op == SPECIAL_INCREMENT)
502 return handle_canonical_for_inc(iter_expr, condition);
503 if (iter_expr->op == SPECIAL_DECREMENT)
504 return handle_canonical_for_dec(iter_expr, condition);
505 return NULL;
508 struct sm_state *__extra_handle_canonical_loops(struct statement *loop, struct stree **stree)
510 struct sm_state *ret;
512 __push_fake_cur_stree();
513 if (!loop->iterator_post_statement)
514 ret = handle_canonical_while_count_down(loop);
515 else
516 ret = handle_canonical_for_loops(loop);
517 *stree = __pop_fake_cur_stree();
518 return ret;
521 int __iterator_unchanged(struct sm_state *sm)
523 if (!sm)
524 return 0;
525 if (get_sm_state(my_id, sm->name, sm->sym) == sm)
526 return 1;
527 return 0;
530 static void while_count_down_after(struct sm_state *sm, struct expression *condition)
532 sval_t after_value;
534 /* paranoid checking. prolly not needed */
535 condition = strip_expr(condition);
536 if (!condition)
537 return;
538 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
539 return;
540 if (condition->op != SPECIAL_DECREMENT)
541 return;
542 after_value = estate_min(sm->state);
543 after_value.value--;
544 set_extra_mod(sm->name, sm->sym, alloc_estate_sval(after_value));
547 void __extra_pre_loop_hook_after(struct sm_state *sm,
548 struct statement *iterator,
549 struct expression *condition)
551 struct expression *iter_expr;
552 sval_t limit;
553 struct smatch_state *state;
555 if (!iterator) {
556 while_count_down_after(sm, condition);
557 return;
560 iter_expr = iterator->expression;
562 if (condition->type != EXPR_COMPARE)
563 return;
564 if (iter_expr->op == SPECIAL_INCREMENT) {
565 limit = sval_binop(estate_max(sm->state), '+',
566 sval_type_val(estate_type(sm->state), 1));
567 } else {
568 limit = sval_binop(estate_min(sm->state), '-',
569 sval_type_val(estate_type(sm->state), 1));
571 if (!estate_has_hard_max(sm->state) && !__has_breaks()) {
572 if (iter_expr->op == SPECIAL_INCREMENT)
573 state = alloc_estate_range(estate_min(sm->state), limit);
574 else
575 state = alloc_estate_range(limit, estate_max(sm->state));
576 } else {
577 state = alloc_estate_sval(limit);
579 if (!estate_has_hard_max(sm->state)) {
580 estate_clear_hard_max(state);
582 if (estate_has_fuzzy_max(sm->state)) {
583 sval_t hmax = estate_get_fuzzy_max(sm->state);
584 sval_t max = estate_max(sm->state);
586 if (sval_cmp(hmax, max) != 0)
587 estate_clear_fuzzy_max(state);
588 } else if (!estate_has_fuzzy_max(sm->state)) {
589 estate_clear_fuzzy_max(state);
592 set_extra_mod(sm->name, sm->sym, state);
595 static struct stree *unmatched_stree;
596 static struct smatch_state *unmatched_state(struct sm_state *sm)
598 struct smatch_state *state;
600 if (unmatched_stree) {
601 state = get_state_stree(unmatched_stree, SMATCH_EXTRA, sm->name, sm->sym);
602 if (state)
603 return state;
605 if (parent_is_gone_var_sym(sm->name, sm->sym))
606 return alloc_estate_empty();
607 return alloc_estate_whole(estate_type(sm->state));
610 static void clear_the_pointed_at(struct expression *expr)
612 struct stree *stree;
613 char *name;
614 struct symbol *sym;
615 struct sm_state *tmp;
617 name = expr_to_var_sym(expr, &sym);
618 if (!name || !sym)
619 goto free;
621 stree = __get_cur_stree();
622 FOR_EACH_MY_SM(SMATCH_EXTRA, stree, tmp) {
623 if (tmp->name[0] != '*')
624 continue;
625 if (tmp->sym != sym)
626 continue;
627 if (strcmp(tmp->name + 1, name) != 0)
628 continue;
629 set_extra_mod(tmp->name, tmp->sym, alloc_estate_whole(estate_type(tmp->state)));
630 } END_FOR_EACH_SM(tmp);
632 free:
633 free_string(name);
636 static void match_function_call(struct expression *expr)
638 struct expression *arg;
639 struct expression *tmp;
641 /* if we have the db this is handled in smatch_function_hooks.c */
642 if (!option_no_db)
643 return;
644 if (inlinable(expr->fn))
645 return;
647 FOR_EACH_PTR(expr->args, arg) {
648 tmp = strip_expr(arg);
649 if (tmp->type == EXPR_PREOP && tmp->op == '&')
650 set_extra_expr_mod(tmp->unop, alloc_estate_whole(get_type(tmp->unop)));
651 else
652 clear_the_pointed_at(tmp);
653 } END_FOR_EACH_PTR(arg);
656 static int values_fit_type(struct expression *left, struct expression *right)
658 struct range_list *rl;
659 struct symbol *type;
661 type = get_type(left);
662 if (!type)
663 return 0;
664 get_absolute_rl(right, &rl);
665 if (type_unsigned(type) && sval_is_negative(rl_min(rl)))
666 return 0;
667 if (sval_cmp(sval_type_min(type), rl_min(rl)) > 0)
668 return 0;
669 if (sval_cmp(sval_type_max(type), rl_max(rl)) < 0)
670 return 0;
671 return 1;
674 static void save_chunk_info(struct expression *left, struct expression *right)
676 struct var_sym_list *vsl;
677 struct var_sym *vs;
678 struct expression *add_expr;
679 struct symbol *type;
680 sval_t sval;
681 char *name;
682 struct symbol *sym;
684 if (right->type != EXPR_BINOP || right->op != '-')
685 return;
686 if (!get_value(right->left, &sval))
687 return;
688 if (!expr_to_sym(right->right))
689 return;
691 add_expr = binop_expression(left, '+', right->right);
692 type = get_type(add_expr);
693 if (!type)
694 return;
695 name = expr_to_chunk_sym_vsl(add_expr, &sym, &vsl);
696 if (!name || !vsl)
697 goto free;
698 FOR_EACH_PTR(vsl, vs) {
699 store_link(link_id, vs->var, vs->sym, name, sym);
700 } END_FOR_EACH_PTR(vs);
702 set_state(SMATCH_EXTRA, name, sym, alloc_estate_sval(sval_cast(type, sval)));
703 free:
704 free_string(name);
707 static void do_array_assign(struct expression *left, int op, struct expression *right)
709 struct range_list *rl;
711 if (op == '=') {
712 get_absolute_rl(right, &rl);
713 rl = cast_rl(get_type(left), rl);
714 } else {
715 rl = alloc_whole_rl(get_type(left));
718 set_extra_array_mod(left, alloc_estate_rl(rl));
721 static void match_untracked_array(struct expression *call, int param)
723 struct expression *arg;
725 arg = get_argument_from_call_expr(call->args, param);
726 arg = strip_expr(arg);
728 clear_array_states(arg);
731 static void match_vanilla_assign(struct expression *left, struct expression *right)
733 struct range_list *orig_rl = NULL;
734 struct range_list *rl = NULL;
735 struct symbol *right_sym;
736 struct symbol *left_type;
737 struct symbol *right_type;
738 char *right_name = NULL;
739 struct symbol *sym;
740 char *name;
741 sval_t max;
742 struct smatch_state *state;
743 int comparison;
745 if (is_struct(left))
746 return;
748 save_chunk_info(left, right);
750 name = expr_to_var_sym(left, &sym);
751 if (!name) {
752 if (is_array(left))
753 do_array_assign(left, '=', right);
754 return;
757 left_type = get_type(left);
758 right_type = get_type(right);
760 right_name = expr_to_var_sym(right, &right_sym);
762 if (!__in_fake_assign &&
763 !(right->type == EXPR_PREOP && right->op == '&') &&
764 right_name && right_sym &&
765 values_fit_type(left, right) &&
766 !has_symbol(right, sym)) {
767 set_equiv(left, right);
768 goto free;
771 if (is_pointer(right) && get_address_rl(right, &rl)) {
772 state = alloc_estate_rl(rl);
773 goto done;
776 comparison = get_comparison(left, right);
777 if (comparison) {
778 comparison = flip_comparison(comparison);
779 get_implied_rl(left, &orig_rl);
782 if (get_implied_rl(right, &rl)) {
783 rl = cast_rl(left_type, rl);
784 if (orig_rl)
785 filter_by_comparison(&rl, comparison, orig_rl);
786 state = alloc_estate_rl(rl);
787 if (get_hard_max(right, &max)) {
788 estate_set_hard_max(state);
789 estate_set_fuzzy_max(state, max);
791 } else {
792 rl = alloc_whole_rl(right_type);
793 rl = cast_rl(left_type, rl);
794 if (orig_rl)
795 filter_by_comparison(&rl, comparison, orig_rl);
796 state = alloc_estate_rl(rl);
799 done:
800 set_extra_mod(name, sym, state);
801 free:
802 free_string(right_name);
805 static int op_remove_assign(int op)
807 switch (op) {
808 case SPECIAL_ADD_ASSIGN:
809 return '+';
810 case SPECIAL_SUB_ASSIGN:
811 return '-';
812 case SPECIAL_MUL_ASSIGN:
813 return '*';
814 case SPECIAL_DIV_ASSIGN:
815 return '/';
816 case SPECIAL_MOD_ASSIGN:
817 return '%';
818 case SPECIAL_AND_ASSIGN:
819 return '&';
820 case SPECIAL_OR_ASSIGN:
821 return '|';
822 case SPECIAL_XOR_ASSIGN:
823 return '^';
824 case SPECIAL_SHL_ASSIGN:
825 return SPECIAL_LEFTSHIFT;
826 case SPECIAL_SHR_ASSIGN:
827 return SPECIAL_RIGHTSHIFT;
828 default:
829 return op;
833 static void match_assign(struct expression *expr)
835 struct range_list *rl = NULL;
836 struct expression *left;
837 struct expression *right;
838 struct expression *binop_expr;
839 struct symbol *left_type;
840 struct symbol *right_type;
841 struct symbol *sym;
842 char *name;
843 sval_t left_min, left_max;
844 sval_t right_min, right_max;
845 sval_t res_min, res_max;
847 left = strip_expr(expr->left);
849 right = strip_parens(expr->right);
850 if (right->type == EXPR_CALL && sym_name_is("__builtin_expect", right->fn))
851 right = get_argument_from_call_expr(right->args, 0);
852 while (right->type == EXPR_ASSIGNMENT && right->op == '=')
853 right = strip_parens(right->left);
855 if (expr->op == '=' && is_condition(expr->right))
856 return; /* handled in smatch_condition.c */
857 if (expr->op == '=' && right->type == EXPR_CALL)
858 return; /* handled in smatch_function_hooks.c */
859 if (expr->op == '=') {
860 match_vanilla_assign(left, right);
861 return;
864 name = expr_to_var_sym(left, &sym);
865 if (!name)
866 return;
868 left_type = get_type(left);
869 right_type = get_type(right);
871 res_min = sval_type_min(left_type);
872 res_max = sval_type_max(left_type);
874 switch (expr->op) {
875 case SPECIAL_ADD_ASSIGN:
876 get_absolute_max(left, &left_max);
877 get_absolute_max(right, &right_max);
878 if (sval_binop_overflows(left_max, '+', right_max))
879 break;
880 if (get_implied_min(left, &left_min) &&
881 !sval_is_negative_min(left_min) &&
882 get_implied_min(right, &right_min) &&
883 !sval_is_negative_min(right_min)) {
884 res_min = sval_binop(left_min, '+', right_min);
885 res_min = sval_cast(right_type, res_min);
887 if (inside_loop()) /* we are assuming loops don't lead to wrapping */
888 break;
889 res_max = sval_binop(left_max, '+', right_max);
890 res_max = sval_cast(right_type, res_max);
891 break;
892 case SPECIAL_SUB_ASSIGN:
893 if (get_implied_max(left, &left_max) &&
894 !sval_is_max(left_max) &&
895 get_implied_min(right, &right_min) &&
896 !sval_is_min(right_min)) {
897 res_max = sval_binop(left_max, '-', right_min);
898 res_max = sval_cast(right_type, res_max);
900 if (inside_loop())
901 break;
902 if (get_implied_min(left, &left_min) &&
903 !sval_is_min(left_min) &&
904 get_implied_max(right, &right_max) &&
905 !sval_is_max(right_max)) {
906 res_min = sval_binop(left_min, '-', right_max);
907 res_min = sval_cast(right_type, res_min);
909 break;
910 case SPECIAL_AND_ASSIGN:
911 case SPECIAL_MOD_ASSIGN:
912 case SPECIAL_SHL_ASSIGN:
913 case SPECIAL_SHR_ASSIGN:
914 case SPECIAL_OR_ASSIGN:
915 case SPECIAL_XOR_ASSIGN:
916 case SPECIAL_MUL_ASSIGN:
917 case SPECIAL_DIV_ASSIGN:
918 binop_expr = binop_expression(expr->left,
919 op_remove_assign(expr->op),
920 expr->right);
921 if (get_absolute_rl(binop_expr, &rl)) {
922 rl = cast_rl(left_type, rl);
923 set_extra_mod(name, sym, alloc_estate_rl(rl));
924 goto free;
926 break;
928 rl = cast_rl(left_type, alloc_rl(res_min, res_max));
929 set_extra_mod(name, sym, alloc_estate_rl(rl));
930 free:
931 free_string(name);
934 static struct smatch_state *increment_state(struct smatch_state *state)
936 sval_t min = estate_min(state);
937 sval_t max = estate_max(state);
939 if (!estate_rl(state))
940 return NULL;
942 if (inside_loop())
943 max = sval_type_max(max.type);
945 if (!sval_is_min(min) && !sval_is_max(min))
946 min.value++;
947 if (!sval_is_min(max) && !sval_is_max(max))
948 max.value++;
949 return alloc_estate_range(min, max);
952 static struct smatch_state *decrement_state(struct smatch_state *state)
954 sval_t min = estate_min(state);
955 sval_t max = estate_max(state);
957 if (!estate_rl(state))
958 return NULL;
960 if (inside_loop())
961 min = sval_type_min(min.type);
963 if (!sval_is_min(min) && !sval_is_max(min))
964 min.value--;
965 if (!sval_is_min(max) && !sval_is_max(max))
966 max.value--;
967 return alloc_estate_range(min, max);
970 static void clear_pointed_at_state(struct expression *expr)
972 struct symbol *type;
975 * ALERT: This is sort of a mess. If it's is a struct assigment like
976 * "foo = bar;", then that's handled by smatch_struct_assignment.c.
977 * the same thing for p++ where "p" is a struct. Most modifications
978 * are handled by the assignment hook or the db. Smatch_extra.c doesn't
979 * use smatch_modification.c because we have to get the ordering right
980 * or something. So if you have p++ where p is a pointer to a standard
981 * c type then we handle that here. What a mess.
984 type = get_type(expr);
985 if (!type || type->type != SYM_PTR)
986 return;
987 type = get_real_base_type(type);
988 if (!type || type->type != SYM_BASETYPE)
989 return;
990 set_extra_expr_mod(deref_expression(expr), alloc_estate_whole(type));
993 static void unop_expr(struct expression *expr)
995 struct smatch_state *state;
997 if (expr->smatch_flags & Handled)
998 return;
1000 switch (expr->op) {
1001 case SPECIAL_INCREMENT:
1002 state = get_state_expr(SMATCH_EXTRA, expr->unop);
1003 state = increment_state(state);
1004 if (!state)
1005 state = alloc_estate_whole(get_type(expr));
1006 set_extra_expr_mod(expr->unop, state);
1007 clear_pointed_at_state(expr->unop);
1008 break;
1009 case SPECIAL_DECREMENT:
1010 state = get_state_expr(SMATCH_EXTRA, expr->unop);
1011 state = decrement_state(state);
1012 if (!state)
1013 state = alloc_estate_whole(get_type(expr));
1014 set_extra_expr_mod(expr->unop, state);
1015 clear_pointed_at_state(expr->unop);
1016 break;
1017 default:
1018 return;
1022 static void asm_expr(struct statement *stmt)
1025 struct expression *expr;
1026 struct symbol *type;
1027 int state = 0;
1029 FOR_EACH_PTR(stmt->asm_outputs, expr) {
1030 switch (state) {
1031 case 0: /* identifier */
1032 case 1: /* constraint */
1033 state++;
1034 continue;
1035 case 2: /* expression */
1036 state = 0;
1037 type = get_type(strip_expr(expr));
1038 set_extra_expr_mod(expr, alloc_estate_whole(type));
1039 continue;
1041 } END_FOR_EACH_PTR(expr);
1044 static void check_dereference(struct expression *expr)
1046 if (outside_of_function())
1047 return;
1048 set_extra_expr_nomod(expr, alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval));
1051 static void match_dereferences(struct expression *expr)
1053 if (expr->type != EXPR_PREOP)
1054 return;
1055 check_dereference(expr->unop);
1058 static void match_pointer_as_array(struct expression *expr)
1060 if (!is_array(expr))
1061 return;
1062 check_dereference(get_array_base(expr));
1065 static void set_param_dereferenced(struct expression *arg, char *key, char *unused)
1067 struct symbol *sym;
1068 char *name;
1070 name = get_variable_from_key(arg, key, &sym);
1071 if (!name || !sym)
1072 goto free;
1074 set_extra_nomod(name, sym, alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval));
1076 free:
1077 free_string(name);
1080 static sval_t add_one(sval_t sval)
1082 sval.value++;
1083 return sval;
1086 static sval_t sub_one(sval_t sval)
1088 sval.value--;
1089 return sval;
1092 static int handle_postop_inc(struct expression *left, int op, struct expression *right)
1094 struct statement *stmt;
1095 struct expression *cond;
1096 struct smatch_state *true_state, *false_state;
1097 sval_t start;
1098 sval_t limit;
1101 * If we're decrementing here then that's a canonical while count down
1102 * so it's handled already. We're only handling loops like:
1103 * i = 0;
1104 * do { ... } while (i++ < 3);
1107 if (left->type != EXPR_POSTOP || left->op != SPECIAL_INCREMENT)
1108 return 0;
1110 stmt = __cur_stmt->parent;
1111 if (!stmt)
1112 return 0;
1113 if (stmt->type == STMT_COMPOUND)
1114 stmt = stmt->parent;
1115 if (!stmt || stmt->type != STMT_ITERATOR || !stmt->iterator_post_condition)
1116 return 0;
1118 cond = strip_expr(stmt->iterator_post_condition);
1119 if (cond->type != EXPR_COMPARE || cond->op != op)
1120 return 0;
1121 if (left != strip_expr(cond->left) || right != strip_expr(cond->right))
1122 return 0;
1124 if (!get_implied_value(left->unop, &start))
1125 return 0;
1126 if (!get_implied_value(right, &limit))
1127 return 0;
1129 if (sval_cmp(start, limit) > 0)
1130 return 0;
1132 switch (op) {
1133 case '<':
1134 case SPECIAL_UNSIGNED_LT:
1135 break;
1136 case SPECIAL_LTE:
1137 case SPECIAL_UNSIGNED_LTE:
1138 limit = add_one(limit);
1139 default:
1140 return 0;
1144 true_state = alloc_estate_range(add_one(start), limit);
1145 false_state = alloc_estate_range(add_one(limit), add_one(limit));
1147 /* Currently we just discard the false state but when two passes is
1148 * implimented correctly then it will use it.
1151 set_extra_expr_true_false(left->unop, true_state, false_state);
1153 return 1;
1156 static void handle_comparison(struct symbol *type, struct expression *left, int op, struct expression *right)
1158 struct range_list *left_orig;
1159 struct range_list *left_true;
1160 struct range_list *left_false;
1161 struct range_list *right_orig;
1162 struct range_list *right_true;
1163 struct range_list *right_false;
1164 struct smatch_state *left_true_state;
1165 struct smatch_state *left_false_state;
1166 struct smatch_state *right_true_state;
1167 struct smatch_state *right_false_state;
1168 sval_t dummy, hard_max;
1169 int left_postop = 0;
1170 int right_postop = 0;
1172 if (left->op == SPECIAL_INCREMENT || left->op == SPECIAL_DECREMENT) {
1173 if (left->type == EXPR_POSTOP) {
1174 left->smatch_flags |= Handled;
1175 left_postop = left->op;
1176 if (handle_postop_inc(left, op, right))
1177 return;
1179 left = strip_parens(left->unop);
1181 while (left->type == EXPR_ASSIGNMENT)
1182 left = strip_parens(left->left);
1184 if (right->op == SPECIAL_INCREMENT || right->op == SPECIAL_DECREMENT) {
1185 if (right->type == EXPR_POSTOP) {
1186 right->smatch_flags |= Handled;
1187 right_postop = right->op;
1189 right = strip_parens(right->unop);
1192 get_real_absolute_rl(left, &left_orig);
1193 left_orig = cast_rl(type, left_orig);
1195 get_real_absolute_rl(right, &right_orig);
1196 right_orig = cast_rl(type, right_orig);
1198 split_comparison_rl(left_orig, op, right_orig, &left_true, &left_false, &right_true, &right_false);
1200 left_true = rl_truncate_cast(get_type(strip_expr(left)), left_true);
1201 left_false = rl_truncate_cast(get_type(strip_expr(left)), left_false);
1202 right_true = rl_truncate_cast(get_type(strip_expr(right)), right_true);
1203 right_false = rl_truncate_cast(get_type(strip_expr(right)), right_false);
1205 if (!left_true || !left_false) {
1206 struct range_list *tmp_true, *tmp_false;
1208 split_comparison_rl(alloc_whole_rl(type), op, right_orig, &tmp_true, &tmp_false, NULL, NULL);
1209 tmp_true = rl_truncate_cast(get_type(strip_expr(left)), tmp_true);
1210 tmp_false = rl_truncate_cast(get_type(strip_expr(left)), tmp_false);
1211 if (tmp_true && tmp_false)
1212 __save_imaginary_state(left, tmp_true, tmp_false);
1215 if (!right_true || !right_false) {
1216 struct range_list *tmp_true, *tmp_false;
1218 split_comparison_rl(alloc_whole_rl(type), op, right_orig, NULL, NULL, &tmp_true, &tmp_false);
1219 tmp_true = rl_truncate_cast(get_type(strip_expr(right)), tmp_true);
1220 tmp_false = rl_truncate_cast(get_type(strip_expr(right)), tmp_false);
1221 if (tmp_true && tmp_false)
1222 __save_imaginary_state(right, tmp_true, tmp_false);
1225 left_true_state = alloc_estate_rl(left_true);
1226 left_false_state = alloc_estate_rl(left_false);
1227 right_true_state = alloc_estate_rl(right_true);
1228 right_false_state = alloc_estate_rl(right_false);
1230 switch (op) {
1231 case '<':
1232 case SPECIAL_UNSIGNED_LT:
1233 case SPECIAL_UNSIGNED_LTE:
1234 case SPECIAL_LTE:
1235 if (get_hard_max(right, &dummy))
1236 estate_set_hard_max(left_true_state);
1237 if (get_hard_max(left, &dummy))
1238 estate_set_hard_max(right_false_state);
1239 break;
1240 case '>':
1241 case SPECIAL_UNSIGNED_GT:
1242 case SPECIAL_UNSIGNED_GTE:
1243 case SPECIAL_GTE:
1244 if (get_hard_max(left, &dummy))
1245 estate_set_hard_max(right_true_state);
1246 if (get_hard_max(right, &dummy))
1247 estate_set_hard_max(left_false_state);
1248 break;
1251 switch (op) {
1252 case '<':
1253 case SPECIAL_UNSIGNED_LT:
1254 case SPECIAL_UNSIGNED_LTE:
1255 case SPECIAL_LTE:
1256 if (get_hard_max(right, &hard_max)) {
1257 if (op == '<' || op == SPECIAL_UNSIGNED_LT)
1258 hard_max.value--;
1259 estate_set_fuzzy_max(left_true_state, hard_max);
1261 if (get_implied_value(right, &hard_max)) {
1262 if (op == SPECIAL_UNSIGNED_LTE ||
1263 op == SPECIAL_LTE)
1264 hard_max.value++;
1265 estate_set_fuzzy_max(left_false_state, hard_max);
1267 if (get_hard_max(left, &hard_max)) {
1268 if (op == SPECIAL_UNSIGNED_LTE ||
1269 op == SPECIAL_LTE)
1270 hard_max.value--;
1271 estate_set_fuzzy_max(right_false_state, hard_max);
1273 if (get_implied_value(left, &hard_max)) {
1274 if (op == '<' || op == SPECIAL_UNSIGNED_LT)
1275 hard_max.value++;
1276 estate_set_fuzzy_max(right_true_state, hard_max);
1278 break;
1279 case '>':
1280 case SPECIAL_UNSIGNED_GT:
1281 case SPECIAL_UNSIGNED_GTE:
1282 case SPECIAL_GTE:
1283 if (get_hard_max(left, &hard_max)) {
1284 if (op == '>' || op == SPECIAL_UNSIGNED_GT)
1285 hard_max.value--;
1286 estate_set_fuzzy_max(right_true_state, hard_max);
1288 if (get_implied_value(left, &hard_max)) {
1289 if (op == SPECIAL_UNSIGNED_GTE ||
1290 op == SPECIAL_GTE)
1291 hard_max.value++;
1292 estate_set_fuzzy_max(right_false_state, hard_max);
1294 if (get_hard_max(right, &hard_max)) {
1295 if (op == SPECIAL_UNSIGNED_LTE ||
1296 op == SPECIAL_LTE)
1297 hard_max.value--;
1298 estate_set_fuzzy_max(left_false_state, hard_max);
1300 if (get_implied_value(right, &hard_max)) {
1301 if (op == '>' ||
1302 op == SPECIAL_UNSIGNED_GT)
1303 hard_max.value++;
1304 estate_set_fuzzy_max(left_true_state, hard_max);
1306 break;
1307 case SPECIAL_EQUAL:
1308 if (get_hard_max(left, &hard_max))
1309 estate_set_fuzzy_max(right_true_state, hard_max);
1310 if (get_hard_max(right, &hard_max))
1311 estate_set_fuzzy_max(left_true_state, hard_max);
1312 break;
1315 if (get_hard_max(left, &hard_max)) {
1316 estate_set_hard_max(left_true_state);
1317 estate_set_hard_max(left_false_state);
1319 if (get_hard_max(right, &hard_max)) {
1320 estate_set_hard_max(right_true_state);
1321 estate_set_hard_max(right_false_state);
1324 if (left_postop == SPECIAL_INCREMENT) {
1325 left_true_state = increment_state(left_true_state);
1326 left_false_state = increment_state(left_false_state);
1328 if (left_postop == SPECIAL_DECREMENT) {
1329 left_true_state = decrement_state(left_true_state);
1330 left_false_state = decrement_state(left_false_state);
1332 if (right_postop == SPECIAL_INCREMENT) {
1333 right_true_state = increment_state(right_true_state);
1334 right_false_state = increment_state(right_false_state);
1336 if (right_postop == SPECIAL_DECREMENT) {
1337 right_true_state = decrement_state(right_true_state);
1338 right_false_state = decrement_state(right_false_state);
1341 if (estate_rl(left_true_state) && estates_equiv(left_true_state, left_false_state)) {
1342 left_true_state = NULL;
1343 left_false_state = NULL;
1346 if (estate_rl(right_true_state) && estates_equiv(right_true_state, right_false_state)) {
1347 right_true_state = NULL;
1348 right_false_state = NULL;
1351 set_extra_expr_true_false(left, left_true_state, left_false_state);
1352 set_extra_expr_true_false(right, right_true_state, right_false_state);
1355 static int is_simple_math(struct expression *expr)
1357 if (!expr)
1358 return 0;
1359 if (expr->type != EXPR_BINOP)
1360 return 0;
1361 switch (expr->op) {
1362 case '+':
1363 case '-':
1364 case '*':
1365 return 1;
1367 return 0;
1370 static void move_known_values(struct expression **left_p, struct expression **right_p)
1372 struct expression *left = *left_p;
1373 struct expression *right = *right_p;
1374 sval_t sval;
1376 if (get_implied_value(left, &sval)) {
1377 if (!is_simple_math(right))
1378 return;
1379 if (right->op == '*') {
1380 sval_t divisor;
1382 if (!get_value(right->right, &divisor))
1383 return;
1384 if (divisor.value == 0 && sval.value % divisor.value)
1385 return;
1386 *left_p = binop_expression(left, invert_op(right->op), right->right);
1387 *right_p = right->left;
1388 return;
1390 if (right->op == '+' && get_value(right->left, &sval)) {
1391 *left_p = binop_expression(left, invert_op(right->op), right->left);
1392 *right_p = right->right;
1393 return;
1395 if (get_value(right->right, &sval)) {
1396 *left_p = binop_expression(left, invert_op(right->op), right->right);
1397 *right_p = right->left;
1398 return;
1400 return;
1402 if (get_implied_value(right, &sval)) {
1403 if (!is_simple_math(left))
1404 return;
1405 if (left->op == '*') {
1406 sval_t divisor;
1408 if (!get_value(left->right, &divisor))
1409 return;
1410 if (divisor.value == 0 && sval.value % divisor.value)
1411 return;
1412 *right_p = binop_expression(right, invert_op(left->op), left->right);
1413 *left_p = left->left;
1414 return;
1416 if (left->op == '+' && get_value(left->left, &sval)) {
1417 *right_p = binop_expression(right, invert_op(left->op), left->left);
1418 *left_p = left->right;
1419 return;
1422 if (get_value(left->right, &sval)) {
1423 *right_p = binop_expression(right, invert_op(left->op), left->right);
1424 *left_p = left->left;
1425 return;
1427 return;
1431 static int match_func_comparison(struct expression *expr)
1433 struct expression *left = strip_expr(expr->left);
1434 struct expression *right = strip_expr(expr->right);
1435 sval_t sval;
1438 * fixme: think about this harder. We should always be trying to limit
1439 * the non-call side as well. If we can't determine the limitter does
1440 * that mean we aren't querying the database and are missing important
1441 * information?
1444 if (left->type == EXPR_CALL) {
1445 if (get_implied_value(left, &sval)) {
1446 handle_comparison(get_type(expr), left, expr->op, right);
1447 return 1;
1449 function_comparison(left, expr->op, right);
1450 return 1;
1453 if (right->type == EXPR_CALL) {
1454 if (get_implied_value(right, &sval)) {
1455 handle_comparison(get_type(expr), left, expr->op, right);
1456 return 1;
1458 function_comparison(left, expr->op, right);
1459 return 1;
1462 return 0;
1465 static void match_comparison(struct expression *expr)
1467 struct expression *left_orig = strip_parens(expr->left);
1468 struct expression *right_orig = strip_parens(expr->right);
1469 struct expression *left, *right;
1470 struct expression *prev;
1471 struct symbol *type;
1473 if (match_func_comparison(expr))
1474 return;
1476 type = get_type(expr);
1477 if (!type)
1478 type = &llong_ctype;
1480 left = left_orig;
1481 right = right_orig;
1482 move_known_values(&left, &right);
1483 handle_comparison(type, left, expr->op, right);
1485 prev = get_assigned_expr(left_orig);
1486 if (is_simple_math(prev) && has_variable(prev, left_orig) == 0) {
1487 left = prev;
1488 right = right_orig;
1489 move_known_values(&left, &right);
1490 handle_comparison(type, left, expr->op, right);
1493 prev = get_assigned_expr(right_orig);
1494 if (is_simple_math(prev) && has_variable(prev, right_orig) == 0) {
1495 left = left_orig;
1496 right = prev;
1497 move_known_values(&left, &right);
1498 handle_comparison(type, left, expr->op, right);
1502 static void handle_AND_condition(struct expression *expr)
1504 struct range_list *rl = NULL;
1505 sval_t known;
1507 if (get_implied_value(expr->left, &known) && known.value > 0) {
1508 known.value--;
1509 get_absolute_rl(expr->right, &rl);
1510 rl = remove_range(rl, sval_type_val(known.type, 0), known);
1511 set_extra_expr_true_false(expr->right, alloc_estate_rl(rl), NULL);
1512 return;
1515 if (get_implied_value(expr->right, &known) && known.value > 0) {
1516 known.value--;
1517 get_absolute_rl(expr->left, &rl);
1518 rl = remove_range(rl, sval_type_val(known.type, 0), known);
1519 set_extra_expr_true_false(expr->left, alloc_estate_rl(rl), NULL);
1520 return;
1524 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
1525 void __extra_match_condition(struct expression *expr)
1527 struct smatch_state *pre_state;
1528 struct smatch_state *true_state;
1529 struct smatch_state *false_state;
1531 expr = strip_expr(expr);
1532 switch (expr->type) {
1533 case EXPR_CALL:
1534 function_comparison(expr, SPECIAL_NOTEQUAL, zero_expr());
1535 return;
1536 case EXPR_PREOP:
1537 case EXPR_SYMBOL:
1538 case EXPR_DEREF: {
1539 sval_t zero;
1541 zero = sval_blank(expr);
1542 zero.value = 0;
1544 pre_state = get_extra_state(expr);
1545 true_state = estate_filter_sval(pre_state, zero);
1546 if (possibly_true(expr, SPECIAL_EQUAL, zero_expr()))
1547 false_state = alloc_estate_sval(zero);
1548 else
1549 false_state = alloc_estate_empty();
1550 set_extra_expr_true_false(expr, true_state, false_state);
1551 return;
1553 case EXPR_COMPARE:
1554 match_comparison(expr);
1555 return;
1556 case EXPR_ASSIGNMENT:
1557 __extra_match_condition(expr->left);
1558 return;
1559 case EXPR_BINOP:
1560 if (expr->op == '&')
1561 handle_AND_condition(expr);
1562 return;
1566 static void assume_indexes_are_valid(struct expression *expr)
1568 struct expression *array_expr;
1569 int array_size;
1570 struct expression *offset;
1571 struct symbol *offset_type;
1572 struct range_list *rl_before;
1573 struct range_list *rl_after;
1574 struct range_list *filter = NULL;
1575 sval_t size;
1577 expr = strip_expr(expr);
1578 if (!is_array(expr))
1579 return;
1581 offset = get_array_offset(expr);
1582 offset_type = get_type(offset);
1583 if (offset_type && type_signed(offset_type)) {
1584 filter = alloc_rl(sval_type_min(offset_type),
1585 sval_type_val(offset_type, -1));
1588 array_expr = get_array_base(expr);
1589 array_size = get_real_array_size(array_expr);
1590 if (array_size > 1) {
1591 size = sval_type_val(offset_type, array_size);
1592 add_range(&filter, size, sval_type_max(offset_type));
1595 if (!filter)
1596 return;
1597 get_absolute_rl(offset, &rl_before);
1598 rl_after = rl_filter(rl_before, filter);
1599 if (rl_equiv(rl_before, rl_after))
1600 return;
1601 set_extra_expr_nomod(offset, alloc_estate_rl(rl_after));
1604 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
1605 int implied_not_equal(struct expression *expr, long long val)
1607 return !possibly_false(expr, SPECIAL_NOTEQUAL, value_expr(val));
1610 int implied_not_equal_name_sym(char *name, struct symbol *sym, long long val)
1612 struct smatch_state *estate;
1614 estate = get_state(SMATCH_EXTRA, name, sym);
1615 if (!estate)
1616 return 0;
1617 if (!rl_has_sval(estate_rl(estate), sval_type_val(estate_type(estate), 0)))
1618 return 1;
1619 return 0;
1622 int parent_is_null_var_sym(const char *name, struct symbol *sym)
1624 char buf[256];
1625 char *start;
1626 char *end;
1627 struct smatch_state *state;
1629 strncpy(buf, name, sizeof(buf) - 1);
1630 buf[sizeof(buf) - 1] = '\0';
1632 start = &buf[0];
1633 while (*start == '*') {
1634 start++;
1635 state = get_state(SMATCH_EXTRA, start, sym);
1636 if (!state)
1637 continue;
1638 if (!estate_rl(state))
1639 return 1;
1640 if (estate_min(state).value == 0 &&
1641 estate_max(state).value == 0)
1642 return 1;
1645 start = &buf[0];
1646 while (*start == '&')
1647 start++;
1649 while ((end = strrchr(start, '-'))) {
1650 *end = '\0';
1651 state = get_state(SMATCH_EXTRA, start, sym);
1652 if (!state)
1653 continue;
1654 if (estate_min(state).value == 0 &&
1655 estate_max(state).value == 0)
1656 return 1;
1658 return 0;
1661 int parent_is_null(struct expression *expr)
1663 struct symbol *sym;
1664 char *var;
1665 int ret = 0;
1667 expr = strip_expr(expr);
1668 var = expr_to_var_sym(expr, &sym);
1669 if (!var || !sym)
1670 goto free;
1671 ret = parent_is_null_var_sym(var, sym);
1672 free:
1673 free_string(var);
1674 return ret;
1677 static int param_used_callback(void *found, int argc, char **argv, char **azColName)
1679 *(int *)found = 1;
1680 return 0;
1683 static int filter_unused_kzalloc_info(struct expression *call, int param, char *printed_name, struct sm_state *sm)
1685 sval_t sval;
1686 int found = 0;
1688 /* for function pointers assume everything is used */
1689 if (call->fn->type != EXPR_SYMBOL)
1690 return 0;
1693 * kzalloc() information is treated as special because so there is just
1694 * a lot of stuff initialized to zero and it makes building the database
1695 * take hours and hours.
1697 * In theory, we should just remove this line and not pass any unused
1698 * information, but I'm not sure enough that this code works so I want
1699 * to hold off on that for now.
1701 if (!estate_get_single_value(sm->state, &sval) || sval.value != 0)
1702 return 0;
1704 run_sql(&param_used_callback, &found,
1705 "select * from call_implies where %s and type = %d and parameter = %d and key = '%s';",
1706 get_static_filter(call->fn->symbol), PARAM_USED, param, printed_name);
1707 if (found)
1708 return 0;
1710 /* If the database is not built yet, then assume everything is used */
1711 run_sql(&param_used_callback, &found,
1712 "select * from call_implies where %s and type = %d;",
1713 get_static_filter(call->fn->symbol), PARAM_USED);
1714 if (!found)
1715 return 0;
1717 return 1;
1720 static void struct_member_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
1722 if (estate_is_whole(sm->state))
1723 return;
1724 if (filter_unused_kzalloc_info(call, param, printed_name, sm))
1725 return;
1726 sql_insert_caller_info(call, PARAM_VALUE, param, printed_name, sm->state->name);
1727 if (estate_has_fuzzy_max(sm->state))
1728 sql_insert_caller_info(call, FUZZY_MAX, param, printed_name,
1729 sval_to_str(estate_get_fuzzy_max(sm->state)));
1732 static void returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
1734 struct symbol *returned_sym;
1735 struct sm_state *sm;
1736 const char *param_name;
1737 char *compare_str;
1738 char buf[256];
1740 returned_sym = expr_to_sym(expr);
1741 if (!returned_sym)
1742 return;
1744 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
1745 if (!estate_rl(sm->state))
1746 continue;
1747 if (returned_sym != sm->sym)
1748 continue;
1750 param_name = get_param_name(sm);
1751 if (!param_name)
1752 continue;
1753 if (strcmp(param_name, "$") == 0)
1754 continue;
1755 compare_str = name_sym_to_param_comparison(sm->name, sm->sym);
1756 if (!compare_str && estate_is_whole(sm->state))
1757 continue;
1758 snprintf(buf, sizeof(buf), "%s%s", sm->state->name, compare_str ?: "");
1760 sql_insert_return_states(return_id, return_ranges, PARAM_VALUE,
1761 -1, param_name, buf);
1762 } END_FOR_EACH_SM(sm);
1765 static void db_limited_before(void)
1767 unmatched_stree = clone_stree(__get_cur_stree());
1770 static void db_limited_after(void)
1772 free_stree(&unmatched_stree);
1775 static void db_param_limit_filter(struct expression *expr, int param, char *key, char *value, enum info_type op)
1777 struct expression *arg;
1778 char *name;
1779 struct symbol *sym;
1780 struct sm_state *sm;
1781 struct symbol *compare_type, *var_type;
1782 struct range_list *rl;
1783 struct range_list *limit;
1784 struct range_list *new;
1786 while (expr->type == EXPR_ASSIGNMENT)
1787 expr = strip_expr(expr->right);
1788 if (expr->type != EXPR_CALL)
1789 return;
1791 arg = get_argument_from_call_expr(expr->args, param);
1792 if (!arg)
1793 return;
1795 name = get_variable_from_key(arg, key, &sym);
1796 if (!name || !sym)
1797 goto free;
1799 if (strcmp(key, "$") == 0)
1800 compare_type = get_arg_type(expr->fn, param);
1801 else
1802 compare_type = get_member_type_from_key(arg, key);
1804 sm = get_sm_state(SMATCH_EXTRA, name, sym);
1805 if (sm)
1806 rl = estate_rl(sm->state);
1807 else
1808 rl = alloc_whole_rl(compare_type);
1810 call_results_to_rl(expr, compare_type, value, &limit);
1811 new = rl_intersection(rl, limit);
1813 var_type = get_member_type_from_key(arg, key);
1814 new = cast_rl(var_type, new);
1816 /* We want to preserve the implications here */
1817 if (sm && rl_equiv(estate_rl(sm->state), new))
1818 __set_sm(sm);
1819 else {
1820 if (op == PARAM_LIMIT)
1821 set_extra_nomod(name, sym, alloc_estate_rl(new));
1822 else
1823 set_extra_mod(name, sym, alloc_estate_rl(new));
1826 free:
1827 free_string(name);
1830 static void db_param_limit(struct expression *expr, int param, char *key, char *value)
1832 db_param_limit_filter(expr, param, key, value, PARAM_LIMIT);
1835 static void db_param_filter(struct expression *expr, int param, char *key, char *value)
1837 db_param_limit_filter(expr, param, key, value, PARAM_FILTER);
1840 static void db_param_add_set(struct expression *expr, int param, char *key, char *value, enum info_type op)
1842 struct expression *arg;
1843 char *name;
1844 struct symbol *sym;
1845 struct symbol *type;
1846 struct smatch_state *state;
1847 struct range_list *new = NULL;
1848 struct range_list *added = NULL;
1850 while (expr->type == EXPR_ASSIGNMENT)
1851 expr = strip_expr(expr->right);
1852 if (expr->type != EXPR_CALL)
1853 return;
1855 arg = get_argument_from_call_expr(expr->args, param);
1856 if (!arg)
1857 return;
1858 type = get_member_type_from_key(arg, key);
1859 name = get_variable_from_key(arg, key, &sym);
1860 if (!name || !sym)
1861 goto free;
1863 state = get_state(SMATCH_EXTRA, name, sym);
1864 if (state)
1865 new = estate_rl(state);
1867 call_results_to_rl(expr, type, value, &added);
1869 if (op == PARAM_SET)
1870 new = added;
1871 else
1872 new = rl_union(new, added);
1874 set_extra_mod(name, sym, alloc_estate_rl(new));
1875 free:
1876 free_string(name);
1879 static void db_param_add(struct expression *expr, int param, char *key, char *value)
1881 db_param_add_set(expr, param, key, value, PARAM_ADD);
1884 static void db_param_set(struct expression *expr, int param, char *key, char *value)
1886 db_param_add_set(expr, param, key, value, PARAM_SET);
1889 static void db_param_value(struct expression *expr, int param, char *key, char *value)
1891 struct expression *call;
1892 char *name;
1893 struct symbol *sym;
1894 struct symbol *type;
1895 struct range_list *rl = NULL;
1897 if (param != -1)
1898 return;
1900 call = expr;
1901 while (call->type == EXPR_ASSIGNMENT)
1902 call = strip_expr(call->right);
1903 if (call->type != EXPR_CALL)
1904 return;
1906 type = get_member_type_from_key(expr->left, key);
1907 name = get_variable_from_key(expr->left, key, &sym);
1908 if (!name || !sym)
1909 goto free;
1911 call_results_to_rl(call, type, value, &rl);
1913 set_extra_mod(name, sym, alloc_estate_rl(rl));
1914 free:
1915 free_string(name);
1918 static void match_call_info(struct expression *expr)
1920 struct smatch_state *state;
1921 struct range_list *rl = NULL;
1922 struct expression *arg;
1923 struct symbol *type;
1924 int i = 0;
1926 FOR_EACH_PTR(expr->args, arg) {
1927 type = get_arg_type(expr->fn, i);
1929 if (get_implied_rl(arg, &rl))
1930 rl = cast_rl(type, rl);
1931 else
1932 rl = cast_rl(type, alloc_whole_rl(get_type(arg)));
1934 if (!is_whole_rl(rl))
1935 sql_insert_caller_info(expr, PARAM_VALUE, i, "$", show_rl(rl));
1936 state = get_state_expr(SMATCH_EXTRA, arg);
1937 if (estate_has_fuzzy_max(state)) {
1938 sql_insert_caller_info(expr, FUZZY_MAX, i, "$",
1939 sval_to_str(estate_get_fuzzy_max(state)));
1941 i++;
1942 } END_FOR_EACH_PTR(arg);
1945 static void set_param_value(const char *name, struct symbol *sym, char *key, char *value)
1947 struct range_list *rl = NULL;
1948 struct smatch_state *state;
1949 struct symbol *type;
1950 char fullname[256];
1952 if (strcmp(key, "*$") == 0)
1953 snprintf(fullname, sizeof(fullname), "*%s", name);
1954 else if (strncmp(key, "$", 1) == 0)
1955 snprintf(fullname, 256, "%s%s", name, key + 1);
1956 else
1957 return;
1959 type = get_member_type_from_key(symbol_expression(sym), key);
1960 str_to_rl(type, value, &rl);
1961 state = alloc_estate_rl(rl);
1962 set_state(SMATCH_EXTRA, fullname, sym, state);
1965 static void set_param_hard_max(const char *name, struct symbol *sym, char *key, char *value)
1967 struct range_list *rl = NULL;
1968 struct smatch_state *state;
1969 struct symbol *type;
1970 char fullname[256];
1971 sval_t max;
1973 if (strcmp(key, "*$") == 0)
1974 snprintf(fullname, sizeof(fullname), "*%s", name);
1975 else if (strncmp(key, "$", 1) == 0)
1976 snprintf(fullname, 256, "%s%s", name, key + 1);
1977 else
1978 return;
1980 state = get_state(SMATCH_EXTRA, fullname, sym);
1981 if (!state)
1982 return;
1983 type = get_member_type_from_key(symbol_expression(sym), key);
1984 str_to_rl(type, value, &rl);
1985 if (!rl_to_sval(rl, &max))
1986 return;
1987 estate_set_fuzzy_max(state, max);
1990 struct smatch_state *get_extra_state(struct expression *expr)
1992 char *name;
1993 struct symbol *sym;
1994 struct smatch_state *ret = NULL;
1995 struct range_list *rl;
1997 if (is_pointer(expr) && get_address_rl(expr, &rl))
1998 return alloc_estate_rl(rl);
2000 name = expr_to_known_chunk_sym(expr, &sym);
2001 if (!name)
2002 goto free;
2004 ret = get_state(SMATCH_EXTRA, name, sym);
2005 free:
2006 free_string(name);
2007 return ret;
2010 void register_smatch_extra(int id)
2012 my_id = id;
2014 add_merge_hook(my_id, &merge_estates);
2015 add_unmatched_state_hook(my_id, &unmatched_state);
2016 select_caller_info_hook(set_param_value, PARAM_VALUE);
2017 select_caller_info_hook(set_param_hard_max, FUZZY_MAX);
2018 select_return_states_before(&db_limited_before);
2019 select_return_states_hook(PARAM_LIMIT, &db_param_limit);
2020 select_return_states_hook(PARAM_FILTER, &db_param_filter);
2021 select_return_states_hook(PARAM_ADD, &db_param_add);
2022 select_return_states_hook(PARAM_SET, &db_param_set);
2023 select_return_states_hook(PARAM_VALUE, &db_param_value);
2024 select_return_states_after(&db_limited_after);
2027 static void match_link_modify(struct sm_state *sm, struct expression *mod_expr)
2029 struct var_sym_list *links;
2030 struct var_sym *tmp;
2031 struct smatch_state *state;
2033 links = sm->state->data;
2035 FOR_EACH_PTR(links, tmp) {
2036 state = get_state(SMATCH_EXTRA, tmp->var, tmp->sym);
2037 if (!state)
2038 continue;
2039 set_state(SMATCH_EXTRA, tmp->var, tmp->sym, alloc_estate_whole(estate_type(state)));
2040 } END_FOR_EACH_PTR(tmp);
2041 set_state(link_id, sm->name, sm->sym, &undefined);
2044 void register_smatch_extra_links(int id)
2046 link_id = id;
2049 void register_smatch_extra_late(int id)
2051 add_merge_hook(link_id, &merge_link_states);
2052 add_modification_hook(link_id, &match_link_modify);
2053 add_hook(&match_dereferences, DEREF_HOOK);
2054 add_hook(&match_pointer_as_array, OP_HOOK);
2055 select_call_implies_hook(DEREFERENCE, &set_param_dereferenced);
2056 add_hook(&match_function_call, FUNCTION_CALL_HOOK);
2057 add_hook(&match_assign, ASSIGNMENT_HOOK);
2058 add_hook(&match_assign, GLOBAL_ASSIGNMENT_HOOK);
2059 add_hook(&unop_expr, OP_HOOK);
2060 add_hook(&asm_expr, ASM_HOOK);
2061 add_untracked_param_hook(&match_untracked_array);
2063 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
2064 add_member_info_callback(my_id, struct_member_callback);
2065 add_split_return_callback(&returned_struct_members);
2067 add_hook(&assume_indexes_are_valid, OP_HOOK);