memcpy_overflow: improve struct member boundary calculation for anon unions
[smatch.git] / smatch_extra.c
blob86e2e0692e7ee65970399ba548e8465a6e7e4fc3
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 expression *offset;
165 struct var_sym_list *vsl;
166 struct var_sym *vs;
167 char *name;
168 struct symbol *sym;
169 sval_t sval;
170 struct sm_state *ret = NULL;
172 array = get_array_base(expr);
173 offset = get_array_offset(expr);
175 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
177 if (!name || !vsl || !get_value(offset, &sval)) {
178 clear_array_states(array);
179 goto free;
182 FOR_EACH_PTR(vsl, vs) {
183 store_link(link_id, vs->var, vs->sym, name, sym);
184 } END_FOR_EACH_PTR(vs);
186 ret = set_state(SMATCH_EXTRA, name, sym, state);
187 free:
188 free_string(name);
189 return ret;
192 struct sm_state *set_extra_expr_mod(struct expression *expr, struct smatch_state *state)
194 struct symbol *sym;
195 char *name;
196 struct sm_state *ret = NULL;
198 if (is_array(expr))
199 return set_extra_array_mod(expr, state);
201 expr = strip_expr(expr);
202 name = expr_to_var_sym(expr, &sym);
203 if (!name || !sym)
204 goto free;
205 ret = set_extra_mod(name, sym, state);
206 free:
207 free_string(name);
208 return ret;
211 void set_extra_nomod(const char *name, struct symbol *sym, struct smatch_state *state)
213 char *new_name;
214 struct symbol *new_sym;
215 struct relation *rel;
216 struct smatch_state *orig_state;
218 orig_state = get_state(SMATCH_EXTRA, name, sym);
220 new_name = get_other_name_sym(name, sym, &new_sym);
221 if (new_name && new_sym)
222 set_state(SMATCH_EXTRA, new_name, new_sym, state);
223 free_string(new_name);
225 if (!estate_related(orig_state)) {
226 set_state(SMATCH_EXTRA, name, sym, state);
227 return;
230 set_related(state, estate_related(orig_state));
231 FOR_EACH_PTR(estate_related(orig_state), rel) {
232 struct smatch_state *estate;
234 if (option_debug_related)
235 sm_msg("%s updating related %s to %s", name, rel->name, state->name);
236 estate = get_state(SMATCH_EXTRA, rel->name, rel->sym);
237 if (!estate)
238 continue;
239 set_state(SMATCH_EXTRA, rel->name, rel->sym, clone_estate_cast(estate_type(estate), state));
240 } END_FOR_EACH_PTR(rel);
244 * This is for return_implies_state() hooks which modify a SMATCH_EXTRA state
246 void set_extra_expr_nomod(struct expression *expr, struct smatch_state *state)
248 char *name;
249 struct symbol *sym;
251 name = expr_to_var_sym(expr, &sym);
252 if (!name || !sym)
253 goto free;
254 set_extra_nomod(name, sym, state);
255 free:
256 free_string(name);
260 static void set_extra_true_false(const char *name, struct symbol *sym,
261 struct smatch_state *true_state,
262 struct smatch_state *false_state)
264 char *new_name;
265 struct symbol *new_sym;
266 struct relation *rel;
267 struct smatch_state *orig_state;
269 if (!true_state && !false_state)
270 return;
272 if (in_warn_on_macro())
273 return;
275 new_name = get_other_name_sym(name, sym, &new_sym);
276 if (new_name && new_sym)
277 set_true_false_states(SMATCH_EXTRA, new_name, new_sym, true_state, false_state);
278 free_string(new_name);
280 orig_state = get_state(SMATCH_EXTRA, name, sym);
282 if (!estate_related(orig_state)) {
283 set_true_false_states(SMATCH_EXTRA, name, sym, true_state, false_state);
284 return;
287 if (true_state)
288 set_related(true_state, estate_related(orig_state));
289 if (false_state)
290 set_related(false_state, estate_related(orig_state));
292 FOR_EACH_PTR(estate_related(orig_state), rel) {
293 set_true_false_states(SMATCH_EXTRA, rel->name, rel->sym,
294 true_state, false_state);
295 } END_FOR_EACH_PTR(rel);
298 static void set_extra_chunk_true_false(struct expression *expr,
299 struct smatch_state *true_state,
300 struct smatch_state *false_state)
302 struct var_sym_list *vsl;
303 struct var_sym *vs;
304 struct symbol *type;
305 char *name;
306 struct symbol *sym;
308 if (in_warn_on_macro())
309 return;
311 type = get_type(expr);
312 if (!type)
313 return;
315 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
316 if (!name || !vsl)
317 goto free;
318 FOR_EACH_PTR(vsl, vs) {
319 store_link(link_id, vs->var, vs->sym, name, sym);
320 } END_FOR_EACH_PTR(vs);
322 set_true_false_states(SMATCH_EXTRA, name, sym,
323 clone_estate(true_state),
324 clone_estate(false_state));
325 free:
326 free_string(name);
329 static void set_extra_expr_true_false(struct expression *expr,
330 struct smatch_state *true_state,
331 struct smatch_state *false_state)
333 char *name;
334 struct symbol *sym;
335 sval_t sval;
337 if (!true_state && !false_state)
338 return;
340 if (get_value(expr, &sval))
341 return;
343 expr = strip_expr(expr);
344 name = expr_to_var_sym(expr, &sym);
345 if (!name || !sym) {
346 free_string(name);
347 set_extra_chunk_true_false(expr, true_state, false_state);
348 return;
350 set_extra_true_false(name, sym, true_state, false_state);
351 free_string(name);
354 static struct sm_state *handle_canonical_while_count_down(struct statement *loop)
356 struct expression *iter_var;
357 struct expression *condition;
358 struct sm_state *sm;
359 struct smatch_state *estate;
360 sval_t start;
362 condition = strip_expr(loop->iterator_pre_condition);
363 if (!condition)
364 return NULL;
365 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
366 return NULL;
367 if (condition->op != SPECIAL_DECREMENT)
368 return NULL;
370 iter_var = condition->unop;
371 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
372 if (!sm)
373 return NULL;
374 if (sval_cmp_val(estate_min(sm->state), 0) < 0)
375 return NULL;
376 start = estate_max(sm->state);
377 if (sval_cmp_val(start, 0) <= 0)
378 return NULL;
379 if (!sval_is_max(start))
380 start.value--;
382 if (condition->type == EXPR_PREOP) {
383 estate = alloc_estate_range(sval_type_val(start.type, 1), start);
384 if (estate_has_hard_max(sm->state))
385 estate_set_hard_max(estate);
386 estate_copy_fuzzy_max(estate, sm->state);
387 set_extra_expr_mod(iter_var, estate);
389 if (condition->type == EXPR_POSTOP) {
390 estate = alloc_estate_range(sval_type_val(start.type, 0), start);
391 if (estate_has_hard_max(sm->state))
392 estate_set_hard_max(estate);
393 estate_copy_fuzzy_max(estate, sm->state);
394 set_extra_expr_mod(iter_var, estate);
396 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
399 static struct sm_state *handle_canonical_for_inc(struct expression *iter_expr,
400 struct expression *condition)
402 struct expression *iter_var;
403 struct sm_state *sm;
404 struct smatch_state *estate;
405 sval_t start, end, max;
407 iter_var = iter_expr->unop;
408 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
409 if (!sm)
410 return NULL;
411 if (!estate_get_single_value(sm->state, &start))
412 return NULL;
413 if (get_implied_max(condition->right, &end))
414 end = sval_cast(get_type(iter_var), end);
415 else
416 end = sval_type_max(get_type(iter_var));
418 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
419 return NULL;
421 switch (condition->op) {
422 case SPECIAL_UNSIGNED_LT:
423 case SPECIAL_NOTEQUAL:
424 case '<':
425 if (!sval_is_min(end))
426 end.value--;
427 break;
428 case SPECIAL_UNSIGNED_LTE:
429 case SPECIAL_LTE:
430 break;
431 default:
432 return NULL;
434 if (sval_cmp(end, start) < 0)
435 return NULL;
436 estate = alloc_estate_range(start, end);
437 if (get_hard_max(condition->right, &max)) {
438 estate_set_hard_max(estate);
439 if (condition->op == '<' ||
440 condition->op == SPECIAL_UNSIGNED_LT ||
441 condition->op == SPECIAL_NOTEQUAL)
442 max.value--;
443 estate_set_fuzzy_max(estate, max);
445 set_extra_expr_mod(iter_var, estate);
446 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
449 static struct sm_state *handle_canonical_for_dec(struct expression *iter_expr,
450 struct expression *condition)
452 struct expression *iter_var;
453 struct sm_state *sm;
454 struct smatch_state *estate;
455 sval_t start, end;
457 iter_var = iter_expr->unop;
458 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
459 if (!sm)
460 return NULL;
461 if (!estate_get_single_value(sm->state, &start))
462 return NULL;
463 if (!get_implied_min(condition->right, &end))
464 end = sval_type_min(get_type(iter_var));
465 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
466 return NULL;
468 switch (condition->op) {
469 case SPECIAL_NOTEQUAL:
470 case '>':
471 if (!sval_is_min(end) && !sval_is_max(end))
472 end.value++;
473 break;
474 case SPECIAL_GTE:
475 break;
476 default:
477 return NULL;
479 if (sval_cmp(end, start) > 0)
480 return NULL;
481 estate = alloc_estate_range(end, start);
482 estate_set_hard_max(estate);
483 estate_set_fuzzy_max(estate, estate_get_fuzzy_max(estate));
484 set_extra_expr_mod(iter_var, estate);
485 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
488 static struct sm_state *handle_canonical_for_loops(struct statement *loop)
490 struct expression *iter_expr;
491 struct expression *condition;
493 if (!loop->iterator_post_statement)
494 return NULL;
495 if (loop->iterator_post_statement->type != STMT_EXPRESSION)
496 return NULL;
497 iter_expr = loop->iterator_post_statement->expression;
498 if (!loop->iterator_pre_condition)
499 return NULL;
500 if (loop->iterator_pre_condition->type != EXPR_COMPARE)
501 return NULL;
502 condition = loop->iterator_pre_condition;
504 if (iter_expr->op == SPECIAL_INCREMENT)
505 return handle_canonical_for_inc(iter_expr, condition);
506 if (iter_expr->op == SPECIAL_DECREMENT)
507 return handle_canonical_for_dec(iter_expr, condition);
508 return NULL;
511 struct sm_state *__extra_handle_canonical_loops(struct statement *loop, struct stree **stree)
513 struct sm_state *ret;
515 __push_fake_cur_stree();
516 if (!loop->iterator_post_statement)
517 ret = handle_canonical_while_count_down(loop);
518 else
519 ret = handle_canonical_for_loops(loop);
520 *stree = __pop_fake_cur_stree();
521 return ret;
524 int __iterator_unchanged(struct sm_state *sm)
526 if (!sm)
527 return 0;
528 if (get_sm_state(my_id, sm->name, sm->sym) == sm)
529 return 1;
530 return 0;
533 static void while_count_down_after(struct sm_state *sm, struct expression *condition)
535 sval_t after_value;
537 /* paranoid checking. prolly not needed */
538 condition = strip_expr(condition);
539 if (!condition)
540 return;
541 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
542 return;
543 if (condition->op != SPECIAL_DECREMENT)
544 return;
545 after_value = estate_min(sm->state);
546 after_value.value--;
547 set_extra_mod(sm->name, sm->sym, alloc_estate_sval(after_value));
550 void __extra_pre_loop_hook_after(struct sm_state *sm,
551 struct statement *iterator,
552 struct expression *condition)
554 struct expression *iter_expr;
555 sval_t limit;
556 struct smatch_state *state;
558 if (!iterator) {
559 while_count_down_after(sm, condition);
560 return;
563 iter_expr = iterator->expression;
565 if (condition->type != EXPR_COMPARE)
566 return;
567 if (iter_expr->op == SPECIAL_INCREMENT) {
568 limit = sval_binop(estate_max(sm->state), '+',
569 sval_type_val(estate_type(sm->state), 1));
570 } else {
571 limit = sval_binop(estate_min(sm->state), '-',
572 sval_type_val(estate_type(sm->state), 1));
574 if (!estate_has_hard_max(sm->state) && !__has_breaks()) {
575 if (iter_expr->op == SPECIAL_INCREMENT)
576 state = alloc_estate_range(estate_min(sm->state), limit);
577 else
578 state = alloc_estate_range(limit, estate_max(sm->state));
579 } else {
580 state = alloc_estate_sval(limit);
582 if (!estate_has_hard_max(sm->state)) {
583 estate_clear_hard_max(state);
585 if (estate_has_fuzzy_max(sm->state)) {
586 sval_t hmax = estate_get_fuzzy_max(sm->state);
587 sval_t max = estate_max(sm->state);
589 if (sval_cmp(hmax, max) != 0)
590 estate_clear_fuzzy_max(state);
591 } else if (!estate_has_fuzzy_max(sm->state)) {
592 estate_clear_fuzzy_max(state);
595 set_extra_mod(sm->name, sm->sym, state);
598 static struct stree *unmatched_stree;
599 static struct smatch_state *unmatched_state(struct sm_state *sm)
601 struct smatch_state *state;
603 if (unmatched_stree) {
604 state = get_state_stree(unmatched_stree, SMATCH_EXTRA, sm->name, sm->sym);
605 if (state)
606 return state;
608 if (parent_is_gone_var_sym(sm->name, sm->sym))
609 return alloc_estate_empty();
610 return alloc_estate_whole(estate_type(sm->state));
613 static void clear_the_pointed_at(struct expression *expr)
615 struct stree *stree;
616 char *name;
617 struct symbol *sym;
618 struct sm_state *tmp;
620 name = expr_to_var_sym(expr, &sym);
621 if (!name || !sym)
622 goto free;
624 stree = __get_cur_stree();
625 FOR_EACH_MY_SM(SMATCH_EXTRA, stree, tmp) {
626 if (tmp->name[0] != '*')
627 continue;
628 if (tmp->sym != sym)
629 continue;
630 if (strcmp(tmp->name + 1, name) != 0)
631 continue;
632 set_extra_mod(tmp->name, tmp->sym, alloc_estate_whole(estate_type(tmp->state)));
633 } END_FOR_EACH_SM(tmp);
635 free:
636 free_string(name);
639 static void match_function_call(struct expression *expr)
641 struct expression *arg;
642 struct expression *tmp;
644 /* if we have the db this is handled in smatch_function_hooks.c */
645 if (!option_no_db)
646 return;
647 if (inlinable(expr->fn))
648 return;
650 FOR_EACH_PTR(expr->args, arg) {
651 tmp = strip_expr(arg);
652 if (tmp->type == EXPR_PREOP && tmp->op == '&')
653 set_extra_expr_mod(tmp->unop, alloc_estate_whole(get_type(tmp->unop)));
654 else
655 clear_the_pointed_at(tmp);
656 } END_FOR_EACH_PTR(arg);
659 static int values_fit_type(struct expression *left, struct expression *right)
661 struct range_list *rl;
662 struct symbol *type;
664 type = get_type(left);
665 if (!type)
666 return 0;
667 get_absolute_rl(right, &rl);
668 if (sval_cmp(sval_type_min(type), rl_min(rl)) > 0)
669 return 0;
670 if (sval_cmp(sval_type_max(type), rl_max(rl)) < 0)
671 return 0;
672 return 1;
675 static void save_chunk_info(struct expression *left, struct expression *right)
677 struct var_sym_list *vsl;
678 struct var_sym *vs;
679 struct expression *add_expr;
680 struct symbol *type;
681 sval_t sval;
682 char *name;
683 struct symbol *sym;
685 if (right->type != EXPR_BINOP || right->op != '-')
686 return;
687 if (!get_value(right->left, &sval))
688 return;
689 if (!expr_to_sym(right->right))
690 return;
692 add_expr = binop_expression(left, '+', right->right);
693 type = get_type(add_expr);
694 if (!type)
695 return;
696 name = expr_to_chunk_sym_vsl(add_expr, &sym, &vsl);
697 if (!name || !vsl)
698 goto free;
699 FOR_EACH_PTR(vsl, vs) {
700 store_link(link_id, vs->var, vs->sym, name, sym);
701 } END_FOR_EACH_PTR(vs);
703 set_state(SMATCH_EXTRA, name, sym, alloc_estate_sval(sval_cast(type, sval)));
704 free:
705 free_string(name);
708 static void do_array_assign(struct expression *left, int op, struct expression *right)
710 struct range_list *rl;
712 if (op == '=') {
713 get_absolute_rl(right, &rl);
714 rl = cast_rl(get_type(left), rl);
715 } else {
716 rl = alloc_whole_rl(get_type(left));
719 set_extra_array_mod(left, alloc_estate_rl(rl));
722 static void match_untracked_array(struct expression *call, int param)
724 struct expression *arg;
726 arg = get_argument_from_call_expr(call->args, param);
727 arg = strip_expr(arg);
729 clear_array_states(arg);
732 static void match_vanilla_assign(struct expression *left, struct expression *right)
734 struct range_list *orig_rl = NULL;
735 struct range_list *rl = NULL;
736 struct symbol *right_sym;
737 struct symbol *left_type;
738 struct symbol *right_type;
739 char *right_name = NULL;
740 struct symbol *sym;
741 char *name;
742 sval_t max;
743 struct smatch_state *state;
744 int comparison;
746 if (is_struct(left))
747 return;
749 save_chunk_info(left, right);
751 name = expr_to_var_sym(left, &sym);
752 if (!name) {
753 if (is_array(left))
754 do_array_assign(left, '=', right);
755 return;
758 left_type = get_type(left);
759 right_type = get_type(right);
761 right_name = expr_to_var_sym(right, &right_sym);
763 if (!__in_fake_assign &&
764 !(right->type == EXPR_PREOP && right->op == '&') &&
765 right_name && right_sym &&
766 values_fit_type(left, right) &&
767 !has_symbol(right, sym)) {
768 set_equiv(left, right);
769 goto free;
772 if (is_pointer(right) && get_address_rl(right, &rl)) {
773 state = alloc_estate_rl(rl);
774 goto done;
777 comparison = get_comparison(left, right);
778 if (comparison) {
779 comparison = flip_comparison(comparison);
780 get_implied_rl(left, &orig_rl);
783 if (get_implied_rl(right, &rl)) {
784 rl = cast_rl(left_type, rl);
785 if (orig_rl)
786 filter_by_comparison(&rl, comparison, orig_rl);
787 state = alloc_estate_rl(rl);
788 if (get_hard_max(right, &max)) {
789 estate_set_hard_max(state);
790 estate_set_fuzzy_max(state, max);
792 } else {
793 rl = alloc_whole_rl(right_type);
794 rl = cast_rl(left_type, rl);
795 if (orig_rl)
796 filter_by_comparison(&rl, comparison, orig_rl);
797 state = alloc_estate_rl(rl);
800 done:
801 set_extra_mod(name, sym, state);
802 free:
803 free_string(right_name);
806 static int op_remove_assign(int op)
808 switch (op) {
809 case SPECIAL_ADD_ASSIGN:
810 return '+';
811 case SPECIAL_SUB_ASSIGN:
812 return '-';
813 case SPECIAL_MUL_ASSIGN:
814 return '*';
815 case SPECIAL_DIV_ASSIGN:
816 return '/';
817 case SPECIAL_MOD_ASSIGN:
818 return '%';
819 case SPECIAL_AND_ASSIGN:
820 return '&';
821 case SPECIAL_OR_ASSIGN:
822 return '|';
823 case SPECIAL_XOR_ASSIGN:
824 return '^';
825 case SPECIAL_SHL_ASSIGN:
826 return SPECIAL_LEFTSHIFT;
827 case SPECIAL_SHR_ASSIGN:
828 return SPECIAL_RIGHTSHIFT;
829 default:
830 return op;
834 static void match_assign(struct expression *expr)
836 struct range_list *rl = NULL;
837 struct expression *left;
838 struct expression *right;
839 struct expression *binop_expr;
840 struct symbol *left_type;
841 struct symbol *right_type;
842 struct symbol *sym;
843 char *name;
844 sval_t left_min, left_max;
845 sval_t right_min, right_max;
846 sval_t res_min, res_max;
848 left = strip_expr(expr->left);
850 right = strip_parens(expr->right);
851 if (right->type == EXPR_CALL && sym_name_is("__builtin_expect", right->fn))
852 right = get_argument_from_call_expr(right->args, 0);
853 while (right->type == EXPR_ASSIGNMENT && right->op == '=')
854 right = strip_parens(right->left);
856 if (expr->op == '=' && is_condition(expr->right))
857 return; /* handled in smatch_condition.c */
858 if (expr->op == '=' && right->type == EXPR_CALL)
859 return; /* handled in smatch_function_hooks.c */
860 if (expr->op == '=') {
861 match_vanilla_assign(left, right);
862 return;
865 name = expr_to_var_sym(left, &sym);
866 if (!name)
867 return;
869 left_type = get_type(left);
870 right_type = get_type(right);
872 res_min = sval_type_min(left_type);
873 res_max = sval_type_max(left_type);
875 switch (expr->op) {
876 case SPECIAL_ADD_ASSIGN:
877 get_absolute_max(left, &left_max);
878 get_absolute_max(right, &right_max);
879 if (sval_binop_overflows(left_max, '+', right_max))
880 break;
881 if (get_implied_min(left, &left_min) &&
882 !sval_is_negative_min(left_min) &&
883 get_implied_min(right, &right_min) &&
884 !sval_is_negative_min(right_min)) {
885 res_min = sval_binop(left_min, '+', right_min);
886 res_min = sval_cast(right_type, res_min);
888 if (inside_loop()) /* we are assuming loops don't lead to wrapping */
889 break;
890 res_max = sval_binop(left_max, '+', right_max);
891 res_max = sval_cast(right_type, res_max);
892 break;
893 case SPECIAL_SUB_ASSIGN:
894 if (get_implied_max(left, &left_max) &&
895 !sval_is_max(left_max) &&
896 get_implied_min(right, &right_min) &&
897 !sval_is_min(right_min)) {
898 res_max = sval_binop(left_max, '-', right_min);
899 res_max = sval_cast(right_type, res_max);
901 if (inside_loop())
902 break;
903 if (get_implied_min(left, &left_min) &&
904 !sval_is_min(left_min) &&
905 get_implied_max(right, &right_max) &&
906 !sval_is_max(right_max)) {
907 res_min = sval_binop(left_min, '-', right_max);
908 res_min = sval_cast(right_type, res_min);
910 break;
911 case SPECIAL_AND_ASSIGN:
912 case SPECIAL_MOD_ASSIGN:
913 case SPECIAL_SHL_ASSIGN:
914 case SPECIAL_SHR_ASSIGN:
915 case SPECIAL_OR_ASSIGN:
916 case SPECIAL_XOR_ASSIGN:
917 case SPECIAL_MUL_ASSIGN:
918 case SPECIAL_DIV_ASSIGN:
919 binop_expr = binop_expression(expr->left,
920 op_remove_assign(expr->op),
921 expr->right);
922 if (get_absolute_rl(binop_expr, &rl)) {
923 rl = cast_rl(left_type, rl);
924 set_extra_mod(name, sym, alloc_estate_rl(rl));
925 goto free;
927 break;
929 rl = cast_rl(left_type, alloc_rl(res_min, res_max));
930 set_extra_mod(name, sym, alloc_estate_rl(rl));
931 free:
932 free_string(name);
935 static struct smatch_state *increment_state(struct smatch_state *state)
937 sval_t min = estate_min(state);
938 sval_t max = estate_max(state);
940 if (!estate_rl(state))
941 return NULL;
943 if (inside_loop())
944 max = sval_type_max(max.type);
946 if (!sval_is_min(min) && !sval_is_max(min))
947 min.value++;
948 if (!sval_is_min(max) && !sval_is_max(max))
949 max.value++;
950 return alloc_estate_range(min, max);
953 static struct smatch_state *decrement_state(struct smatch_state *state)
955 sval_t min = estate_min(state);
956 sval_t max = estate_max(state);
958 if (!estate_rl(state))
959 return NULL;
961 if (inside_loop())
962 min = sval_type_min(min.type);
964 if (!sval_is_min(min) && !sval_is_max(min))
965 min.value--;
966 if (!sval_is_min(max) && !sval_is_max(max))
967 max.value--;
968 return alloc_estate_range(min, max);
971 static void unop_expr(struct expression *expr)
973 struct smatch_state *state;
975 if (expr->smatch_flags & Handled)
976 return;
978 switch (expr->op) {
979 case SPECIAL_INCREMENT:
980 state = get_state_expr(SMATCH_EXTRA, expr->unop);
981 state = increment_state(state);
982 if (!state)
983 state = alloc_estate_whole(get_type(expr));
984 set_extra_expr_mod(expr->unop, state);
985 break;
986 case SPECIAL_DECREMENT:
987 state = get_state_expr(SMATCH_EXTRA, expr->unop);
988 state = decrement_state(state);
989 if (!state)
990 state = alloc_estate_whole(get_type(expr));
991 set_extra_expr_mod(expr->unop, state);
992 break;
993 default:
994 return;
998 static void asm_expr(struct statement *stmt)
1001 struct expression *expr;
1002 struct symbol *type;
1003 int state = 0;
1005 FOR_EACH_PTR(stmt->asm_outputs, expr) {
1006 switch (state) {
1007 case 0: /* identifier */
1008 case 1: /* constraint */
1009 state++;
1010 continue;
1011 case 2: /* expression */
1012 state = 0;
1013 type = get_type(strip_expr(expr));
1014 set_extra_expr_mod(expr, alloc_estate_whole(type));
1015 continue;
1017 } END_FOR_EACH_PTR(expr);
1020 static void check_dereference(struct expression *expr)
1022 if (outside_of_function())
1023 return;
1024 set_extra_expr_nomod(expr, alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval));
1027 static void match_dereferences(struct expression *expr)
1029 if (expr->type != EXPR_PREOP)
1030 return;
1031 check_dereference(expr->unop);
1034 static void match_pointer_as_array(struct expression *expr)
1036 if (!is_array(expr))
1037 return;
1038 check_dereference(get_array_base(expr));
1041 static void set_param_dereferenced(struct expression *arg, char *key, char *unused)
1043 struct symbol *sym;
1044 char *name;
1046 name = get_variable_from_key(arg, key, &sym);
1047 if (!name || !sym)
1048 goto free;
1050 set_extra_nomod(name, sym, alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval));
1052 free:
1053 free_string(name);
1056 static sval_t add_one(sval_t sval)
1058 sval.value++;
1059 return sval;
1062 static sval_t sub_one(sval_t sval)
1064 sval.value--;
1065 return sval;
1068 static void handle_comparison(struct symbol *type, struct expression *left, int op, struct expression *right)
1070 struct range_list *left_orig;
1071 struct range_list *left_true;
1072 struct range_list *left_false;
1073 struct range_list *right_orig;
1074 struct range_list *right_true;
1075 struct range_list *right_false;
1076 struct smatch_state *left_true_state;
1077 struct smatch_state *left_false_state;
1078 struct smatch_state *right_true_state;
1079 struct smatch_state *right_false_state;
1080 sval_t min, max, dummy, hard_max;
1081 int left_postop = 0;
1082 int right_postop = 0;
1084 if (left->op == SPECIAL_INCREMENT || left->op == SPECIAL_DECREMENT) {
1085 if (left->type == EXPR_POSTOP) {
1086 left->smatch_flags |= Handled;
1087 left_postop = left->op;
1089 left = strip_parens(left->unop);
1091 while (left->type == EXPR_ASSIGNMENT)
1092 left = strip_parens(left->left);
1094 if (right->op == SPECIAL_INCREMENT || right->op == SPECIAL_DECREMENT) {
1095 if (right->type == EXPR_POSTOP) {
1096 right->smatch_flags |= Handled;
1097 right_postop = right->op;
1099 right = strip_parens(right->unop);
1102 /* FIXME: we should be able to use get_real_absolute_rl() here but
1103 * apparently that is buggy.
1105 get_absolute_rl(left, &left_orig);
1106 left_orig = cast_rl(type, left_orig);
1108 get_absolute_rl(right, &right_orig);
1109 right_orig = cast_rl(type, right_orig);
1111 min = sval_type_min(type);
1112 max = sval_type_max(type);
1114 left_true = clone_rl(left_orig);
1115 left_false = clone_rl(left_orig);
1116 right_true = clone_rl(right_orig);
1117 right_false = clone_rl(right_orig);
1119 switch (op) {
1120 case '<':
1121 case SPECIAL_UNSIGNED_LT:
1122 left_true = remove_range(left_orig, rl_max(right_orig), max);
1123 if (!sval_is_min(rl_min(right_orig))) {
1124 left_false = remove_range(left_orig, min, sub_one(rl_min(right_orig)));
1127 right_true = remove_range(right_orig, min, rl_min(left_orig));
1128 if (!sval_is_max(rl_max(left_orig)))
1129 right_false = remove_range(right_orig, add_one(rl_max(left_orig)), max);
1130 break;
1131 case SPECIAL_UNSIGNED_LTE:
1132 case SPECIAL_LTE:
1133 if (!sval_is_max(rl_max(right_orig)))
1134 left_true = remove_range(left_orig, add_one(rl_max(right_orig)), max);
1135 left_false = remove_range(left_orig, min, rl_min(right_orig));
1137 if (!sval_is_min(rl_min(left_orig)))
1138 right_true = remove_range(right_orig, min, sub_one(rl_min(left_orig)));
1139 right_false = remove_range(right_orig, rl_max(left_orig), max);
1141 if (sval_cmp(rl_min(left_orig), rl_min(right_orig)) == 0)
1142 left_false = remove_range(left_false, rl_min(left_orig), rl_min(left_orig));
1143 if (sval_cmp(rl_max(left_orig), rl_max(right_orig)) == 0)
1144 right_false = remove_range(right_false, rl_max(left_orig), rl_max(left_orig));
1145 break;
1146 case SPECIAL_EQUAL:
1147 if (!sval_is_max(rl_max(right_orig))) {
1148 left_true = remove_range(left_true, add_one(rl_max(right_orig)), max);
1150 if (!sval_is_min(rl_min(right_orig))) {
1151 left_true = remove_range(left_true, min, sub_one(rl_min(right_orig)));
1153 if (sval_cmp(rl_min(right_orig), rl_max(right_orig)) == 0)
1154 left_false = remove_range(left_orig, rl_min(right_orig), rl_min(right_orig));
1156 if (!sval_is_max(rl_max(left_orig)))
1157 right_true = remove_range(right_true, add_one(rl_max(left_orig)), max);
1158 if (!sval_is_min(rl_min(left_orig)))
1159 right_true = remove_range(right_true, min, sub_one(rl_min(left_orig)));
1160 if (sval_cmp(rl_min(left_orig), rl_max(left_orig)) == 0)
1161 right_false = remove_range(right_orig, rl_min(left_orig), rl_min(left_orig));
1162 break;
1163 case SPECIAL_UNSIGNED_GTE:
1164 case SPECIAL_GTE:
1165 if (!sval_is_min(rl_min(right_orig)))
1166 left_true = remove_range(left_orig, min, sub_one(rl_min(right_orig)));
1167 left_false = remove_range(left_orig, rl_max(right_orig), max);
1169 if (!sval_is_max(rl_max(left_orig)))
1170 right_true = remove_range(right_orig, add_one(rl_max(left_orig)), max);
1171 right_false = remove_range(right_orig, min, rl_min(left_orig));
1173 if (sval_cmp(rl_min(left_orig), rl_min(right_orig)) == 0)
1174 right_false = remove_range(right_false, rl_min(left_orig), rl_min(left_orig));
1175 if (sval_cmp(rl_max(left_orig), rl_max(right_orig)) == 0)
1176 left_false = remove_range(left_false, rl_max(left_orig), rl_max(left_orig));
1177 break;
1178 case '>':
1179 case SPECIAL_UNSIGNED_GT:
1180 left_true = remove_range(left_orig, min, rl_min(right_orig));
1181 if (!sval_is_max(rl_max(right_orig)))
1182 left_false = remove_range(left_orig, add_one(rl_max(right_orig)), max);
1184 right_true = remove_range(right_orig, rl_max(left_orig), max);
1185 if (!sval_is_min(rl_min(left_orig)))
1186 right_false = remove_range(right_orig, min, sub_one(rl_min(left_orig)));
1187 break;
1188 case SPECIAL_NOTEQUAL:
1189 if (!sval_is_max(rl_max(right_orig)))
1190 left_false = remove_range(left_false, add_one(rl_max(right_orig)), max);
1191 if (!sval_is_min(rl_min(right_orig)))
1192 left_false = remove_range(left_false, min, sub_one(rl_min(right_orig)));
1193 if (sval_cmp(rl_min(right_orig), rl_max(right_orig)) == 0)
1194 left_true = remove_range(left_orig, rl_min(right_orig), rl_min(right_orig));
1196 if (!sval_is_max(rl_max(left_orig)))
1197 right_false = remove_range(right_false, add_one(rl_max(left_orig)), max);
1198 if (!sval_is_min(rl_min(left_orig)))
1199 right_false = remove_range(right_false, min, sub_one(rl_min(left_orig)));
1200 if (sval_cmp(rl_min(left_orig), rl_max(left_orig)) == 0)
1201 right_true = remove_range(right_orig, rl_min(left_orig), rl_min(left_orig));
1202 break;
1203 default:
1204 return;
1207 left_true = rl_truncate_cast(get_type(strip_expr(left)), left_true);
1208 left_false = rl_truncate_cast(get_type(strip_expr(left)), left_false);
1209 right_true = rl_truncate_cast(get_type(strip_expr(right)), right_true);
1210 right_false = rl_truncate_cast(get_type(strip_expr(right)), right_false);
1212 left_true_state = alloc_estate_rl(left_true);
1213 left_false_state = alloc_estate_rl(left_false);
1214 right_true_state = alloc_estate_rl(right_true);
1215 right_false_state = alloc_estate_rl(right_false);
1217 switch (op) {
1218 case '<':
1219 case SPECIAL_UNSIGNED_LT:
1220 case SPECIAL_UNSIGNED_LTE:
1221 case SPECIAL_LTE:
1222 if (get_hard_max(right, &dummy))
1223 estate_set_hard_max(left_true_state);
1224 if (get_hard_max(left, &dummy))
1225 estate_set_hard_max(right_false_state);
1226 break;
1227 case '>':
1228 case SPECIAL_UNSIGNED_GT:
1229 case SPECIAL_UNSIGNED_GTE:
1230 case SPECIAL_GTE:
1231 if (get_hard_max(left, &dummy))
1232 estate_set_hard_max(right_true_state);
1233 if (get_hard_max(right, &dummy))
1234 estate_set_hard_max(left_false_state);
1235 break;
1238 switch (op) {
1239 case '<':
1240 case SPECIAL_UNSIGNED_LT:
1241 case SPECIAL_UNSIGNED_LTE:
1242 case SPECIAL_LTE:
1243 if (get_hard_max(right, &hard_max)) {
1244 if (op == '<' || op == SPECIAL_UNSIGNED_LT)
1245 hard_max.value--;
1246 estate_set_fuzzy_max(left_true_state, hard_max);
1248 if (get_implied_value(right, &hard_max)) {
1249 if (op == SPECIAL_UNSIGNED_LTE ||
1250 op == SPECIAL_LTE)
1251 hard_max.value++;
1252 estate_set_fuzzy_max(left_false_state, hard_max);
1254 if (get_hard_max(left, &hard_max)) {
1255 if (op == SPECIAL_UNSIGNED_LTE ||
1256 op == SPECIAL_LTE)
1257 hard_max.value--;
1258 estate_set_fuzzy_max(right_false_state, hard_max);
1260 if (get_implied_value(left, &hard_max)) {
1261 if (op == '<' || op == SPECIAL_UNSIGNED_LT)
1262 hard_max.value++;
1263 estate_set_fuzzy_max(right_true_state, hard_max);
1265 break;
1266 case '>':
1267 case SPECIAL_UNSIGNED_GT:
1268 case SPECIAL_UNSIGNED_GTE:
1269 case SPECIAL_GTE:
1270 if (get_hard_max(left, &hard_max)) {
1271 if (op == '>' || op == SPECIAL_UNSIGNED_GT)
1272 hard_max.value--;
1273 estate_set_fuzzy_max(right_true_state, hard_max);
1275 if (get_implied_value(left, &hard_max)) {
1276 if (op == SPECIAL_UNSIGNED_GTE ||
1277 op == SPECIAL_GTE)
1278 hard_max.value++;
1279 estate_set_fuzzy_max(right_false_state, hard_max);
1281 if (get_hard_max(right, &hard_max)) {
1282 if (op == SPECIAL_UNSIGNED_LTE ||
1283 op == SPECIAL_LTE)
1284 hard_max.value--;
1285 estate_set_fuzzy_max(left_false_state, hard_max);
1287 if (get_implied_value(right, &hard_max)) {
1288 if (op == '>' ||
1289 op == SPECIAL_UNSIGNED_GT)
1290 hard_max.value++;
1291 estate_set_fuzzy_max(left_true_state, hard_max);
1293 break;
1294 case SPECIAL_EQUAL:
1295 if (get_hard_max(left, &hard_max))
1296 estate_set_fuzzy_max(right_true_state, hard_max);
1297 if (get_hard_max(right, &hard_max))
1298 estate_set_fuzzy_max(left_true_state, hard_max);
1299 break;
1302 if (get_hard_max(left, &hard_max)) {
1303 estate_set_hard_max(left_true_state);
1304 estate_set_hard_max(left_false_state);
1306 if (get_hard_max(right, &hard_max)) {
1307 estate_set_hard_max(right_true_state);
1308 estate_set_hard_max(right_false_state);
1311 if (left_postop == SPECIAL_INCREMENT) {
1312 left_true_state = increment_state(left_true_state);
1313 left_false_state = increment_state(left_false_state);
1315 if (left_postop == SPECIAL_DECREMENT) {
1316 left_true_state = decrement_state(left_true_state);
1317 left_false_state = decrement_state(left_false_state);
1319 if (right_postop == SPECIAL_INCREMENT) {
1320 right_true_state = increment_state(right_true_state);
1321 right_false_state = increment_state(right_false_state);
1323 if (right_postop == SPECIAL_DECREMENT) {
1324 right_true_state = decrement_state(right_true_state);
1325 right_false_state = decrement_state(right_false_state);
1328 if (estate_rl(left_true_state) && estates_equiv(left_true_state, left_false_state)) {
1329 left_true_state = NULL;
1330 left_false_state = NULL;
1333 if (estate_rl(right_true_state) && estates_equiv(right_true_state, right_false_state)) {
1334 right_true_state = NULL;
1335 right_false_state = NULL;
1338 set_extra_expr_true_false(left, left_true_state, left_false_state);
1339 set_extra_expr_true_false(right, right_true_state, right_false_state);
1342 static int is_simple_math(struct expression *expr)
1344 if (!expr)
1345 return 0;
1346 if (expr->type != EXPR_BINOP)
1347 return 0;
1348 switch (expr->op) {
1349 case '+':
1350 case '-':
1351 case '*':
1352 return 1;
1354 return 0;
1357 static void move_known_values(struct expression **left_p, struct expression **right_p)
1359 struct expression *left = *left_p;
1360 struct expression *right = *right_p;
1361 sval_t sval;
1363 if (get_implied_value(left, &sval)) {
1364 if (!is_simple_math(right))
1365 return;
1366 if (right->op == '*') {
1367 sval_t divisor;
1369 if (!get_value(right->right, &divisor))
1370 return;
1371 if (divisor.value == 0 && sval.value % divisor.value)
1372 return;
1373 *left_p = binop_expression(left, invert_op(right->op), right->right);
1374 *right_p = right->left;
1375 return;
1377 if (right->op == '+' && get_value(right->left, &sval)) {
1378 *left_p = binop_expression(left, invert_op(right->op), right->left);
1379 *right_p = right->right;
1380 return;
1382 if (get_value(right->right, &sval)) {
1383 *left_p = binop_expression(left, invert_op(right->op), right->right);
1384 *right_p = right->left;
1385 return;
1387 return;
1389 if (get_implied_value(right, &sval)) {
1390 if (!is_simple_math(left))
1391 return;
1392 if (left->op == '*') {
1393 sval_t divisor;
1395 if (!get_value(left->right, &divisor))
1396 return;
1397 if (divisor.value == 0 && sval.value % divisor.value)
1398 return;
1399 *right_p = binop_expression(right, invert_op(left->op), left->right);
1400 *left_p = left->left;
1401 return;
1403 if (left->op == '+' && get_value(left->left, &sval)) {
1404 *right_p = binop_expression(right, invert_op(left->op), left->left);
1405 *left_p = left->right;
1406 return;
1409 if (get_value(left->right, &sval)) {
1410 *right_p = binop_expression(right, invert_op(left->op), left->right);
1411 *left_p = left->left;
1412 return;
1414 return;
1418 static int match_func_comparison(struct expression *expr)
1420 struct expression *left = strip_expr(expr->left);
1421 struct expression *right = strip_expr(expr->right);
1422 sval_t sval;
1425 * fixme: think about this harder. We should always be trying to limit
1426 * the non-call side as well. If we can't determine the limitter does
1427 * that mean we aren't querying the database and are missing important
1428 * information?
1431 if (left->type == EXPR_CALL) {
1432 if (get_implied_value(left, &sval)) {
1433 handle_comparison(get_type(expr), left, expr->op, right);
1434 return 1;
1436 function_comparison(left, expr->op, right);
1437 return 1;
1440 if (right->type == EXPR_CALL) {
1441 if (get_implied_value(right, &sval)) {
1442 handle_comparison(get_type(expr), left, expr->op, right);
1443 return 1;
1445 function_comparison(left, expr->op, right);
1446 return 1;
1449 return 0;
1452 static void match_comparison(struct expression *expr)
1454 struct expression *left_orig = strip_parens(expr->left);
1455 struct expression *right_orig = strip_parens(expr->right);
1456 struct expression *left, *right;
1457 struct expression *prev;
1458 struct symbol *type;
1460 if (match_func_comparison(expr))
1461 return;
1463 type = get_type(expr);
1464 if (!type)
1465 type = &llong_ctype;
1467 left = left_orig;
1468 right = right_orig;
1469 move_known_values(&left, &right);
1470 handle_comparison(type, left, expr->op, right);
1472 prev = get_assigned_expr(left_orig);
1473 if (is_simple_math(prev) && has_variable(prev, left_orig) == 0) {
1474 left = prev;
1475 right = right_orig;
1476 move_known_values(&left, &right);
1477 handle_comparison(type, left, expr->op, right);
1480 prev = get_assigned_expr(right_orig);
1481 if (is_simple_math(prev) && has_variable(prev, right_orig) == 0) {
1482 left = left_orig;
1483 right = prev;
1484 move_known_values(&left, &right);
1485 handle_comparison(type, left, expr->op, right);
1489 static void handle_AND_condition(struct expression *expr)
1491 struct range_list *rl = NULL;
1492 sval_t known;
1494 if (get_implied_value(expr->left, &known) && known.value > 0) {
1495 known.value--;
1496 get_absolute_rl(expr->right, &rl);
1497 rl = remove_range(rl, sval_type_val(known.type, 0), known);
1498 set_extra_expr_true_false(expr->right, alloc_estate_rl(rl), NULL);
1499 return;
1502 if (get_implied_value(expr->right, &known) && known.value > 0) {
1503 known.value--;
1504 get_absolute_rl(expr->left, &rl);
1505 rl = remove_range(rl, sval_type_val(known.type, 0), known);
1506 set_extra_expr_true_false(expr->left, alloc_estate_rl(rl), NULL);
1507 return;
1511 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
1512 void __extra_match_condition(struct expression *expr)
1514 struct smatch_state *pre_state;
1515 struct smatch_state *true_state;
1516 struct smatch_state *false_state;
1518 expr = strip_expr(expr);
1519 switch (expr->type) {
1520 case EXPR_CALL:
1521 function_comparison(expr, SPECIAL_NOTEQUAL, zero_expr());
1522 return;
1523 case EXPR_PREOP:
1524 case EXPR_SYMBOL:
1525 case EXPR_DEREF: {
1526 sval_t zero;
1528 zero = sval_blank(expr);
1529 zero.value = 0;
1531 pre_state = get_extra_state(expr);
1532 true_state = estate_filter_sval(pre_state, zero);
1533 if (possibly_true(expr, SPECIAL_EQUAL, zero_expr()))
1534 false_state = alloc_estate_sval(zero);
1535 else
1536 false_state = alloc_estate_empty();
1537 set_extra_expr_true_false(expr, true_state, false_state);
1538 return;
1540 case EXPR_COMPARE:
1541 match_comparison(expr);
1542 return;
1543 case EXPR_ASSIGNMENT:
1544 __extra_match_condition(expr->left);
1545 return;
1546 case EXPR_BINOP:
1547 if (expr->op == '&')
1548 handle_AND_condition(expr);
1549 return;
1553 static void assume_indexes_are_valid(struct expression *expr)
1555 struct expression *array_expr;
1556 int array_size;
1557 struct expression *offset;
1558 struct symbol *offset_type;
1559 struct range_list *rl_before;
1560 struct range_list *rl_after;
1561 struct range_list *filter = NULL;
1562 sval_t size;
1564 expr = strip_expr(expr);
1565 if (!is_array(expr))
1566 return;
1568 offset = get_array_offset(expr);
1569 offset_type = get_type(offset);
1570 if (offset_type && type_signed(offset_type)) {
1571 filter = alloc_rl(sval_type_min(offset_type),
1572 sval_type_val(offset_type, -1));
1575 array_expr = get_array_base(expr);
1576 array_size = get_real_array_size(array_expr);
1577 if (array_size > 1) {
1578 size = sval_type_val(offset_type, array_size);
1579 add_range(&filter, size, sval_type_max(offset_type));
1582 if (!filter)
1583 return;
1584 get_absolute_rl(offset, &rl_before);
1585 rl_after = rl_filter(rl_before, filter);
1586 if (rl_equiv(rl_before, rl_after))
1587 return;
1588 set_extra_expr_nomod(offset, alloc_estate_rl(rl_after));
1591 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
1592 int implied_not_equal(struct expression *expr, long long val)
1594 return !possibly_false(expr, SPECIAL_NOTEQUAL, value_expr(val));
1597 int implied_not_equal_name_sym(char *name, struct symbol *sym, long long val)
1599 struct smatch_state *estate;
1601 estate = get_state(SMATCH_EXTRA, name, sym);
1602 if (!estate)
1603 return 0;
1604 if (!rl_has_sval(estate_rl(estate), sval_type_val(estate_type(estate), 0)))
1605 return 1;
1606 return 0;
1609 int parent_is_null_var_sym(const char *name, struct symbol *sym)
1611 char buf[256];
1612 char *start;
1613 char *end;
1614 struct smatch_state *state;
1616 strncpy(buf, name, sizeof(buf) - 1);
1617 buf[sizeof(buf) - 1] = '\0';
1619 start = &buf[0];
1620 while (*start == '*') {
1621 start++;
1622 state = get_state(SMATCH_EXTRA, start, sym);
1623 if (!state)
1624 continue;
1625 if (!estate_rl(state))
1626 return 1;
1627 if (estate_min(state).value == 0 &&
1628 estate_max(state).value == 0)
1629 return 1;
1632 start = &buf[0];
1633 while (*start == '&')
1634 start++;
1636 while ((end = strrchr(start, '-'))) {
1637 *end = '\0';
1638 state = get_state(SMATCH_EXTRA, start, sym);
1639 if (!state)
1640 continue;
1641 if (estate_min(state).value == 0 &&
1642 estate_max(state).value == 0)
1643 return 1;
1645 return 0;
1648 int parent_is_null(struct expression *expr)
1650 struct symbol *sym;
1651 char *var;
1652 int ret = 0;
1654 expr = strip_expr(expr);
1655 var = expr_to_var_sym(expr, &sym);
1656 if (!var || !sym)
1657 goto free;
1658 ret = parent_is_null_var_sym(var, sym);
1659 free:
1660 free_string(var);
1661 return ret;
1664 static int param_used_callback(void *found, int argc, char **argv, char **azColName)
1666 *(int *)found = 1;
1667 return 0;
1670 static int filter_unused_kzalloc_info(struct expression *call, int param, char *printed_name, struct sm_state *sm)
1672 sval_t sval;
1673 int found = 0;
1675 /* for function pointers assume everything is used */
1676 if (call->fn->type != EXPR_SYMBOL)
1677 return 0;
1680 * kzalloc() information is treated as special because so there is just
1681 * a lot of stuff initialized to zero and it makes building the database
1682 * take hours and hours.
1684 * In theory, we should just remove this line and not pass any unused
1685 * information, but I'm not sure enough that this code works so I want
1686 * to hold off on that for now.
1688 if (!estate_get_single_value(sm->state, &sval) || sval.value != 0)
1689 return 0;
1691 run_sql(&param_used_callback, &found,
1692 "select * from call_implies where %s and type = %d and parameter = %d and key = '%s';",
1693 get_static_filter(call->fn->symbol), PARAM_USED, param, printed_name);
1694 if (found)
1695 return 0;
1697 /* If the database is not built yet, then assume everything is used */
1698 run_sql(&param_used_callback, &found,
1699 "select * from call_implies where %s and type = %d;",
1700 get_static_filter(call->fn->symbol), PARAM_USED);
1701 if (!found)
1702 return 0;
1704 return 1;
1707 static void struct_member_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
1709 if (estate_is_whole(sm->state))
1710 return;
1711 if (filter_unused_kzalloc_info(call, param, printed_name, sm))
1712 return;
1713 sql_insert_caller_info(call, PARAM_VALUE, param, printed_name, sm->state->name);
1714 if (estate_has_fuzzy_max(sm->state))
1715 sql_insert_caller_info(call, FUZZY_MAX, param, printed_name,
1716 sval_to_str(estate_get_fuzzy_max(sm->state)));
1719 static void returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
1721 struct symbol *returned_sym;
1722 struct sm_state *sm;
1723 const char *param_name;
1724 char *compare_str;
1725 char buf[256];
1727 returned_sym = expr_to_sym(expr);
1728 if (!returned_sym)
1729 return;
1731 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
1732 if (!estate_rl(sm->state))
1733 continue;
1734 if (returned_sym != sm->sym)
1735 continue;
1737 param_name = get_param_name(sm);
1738 if (!param_name)
1739 continue;
1740 if (strcmp(param_name, "$") == 0)
1741 continue;
1742 compare_str = name_sym_to_param_comparison(sm->name, sm->sym);
1743 if (!compare_str && estate_is_whole(sm->state))
1744 continue;
1745 snprintf(buf, sizeof(buf), "%s%s", sm->state->name, compare_str ?: "");
1747 sql_insert_return_states(return_id, return_ranges, PARAM_VALUE,
1748 -1, param_name, buf);
1749 } END_FOR_EACH_SM(sm);
1752 static void db_limited_before(void)
1754 unmatched_stree = clone_stree(__get_cur_stree());
1757 static void db_limited_after(void)
1759 free_stree(&unmatched_stree);
1762 static void db_param_limit_filter(struct expression *expr, int param, char *key, char *value, enum info_type op)
1764 struct expression *arg;
1765 char *name;
1766 struct symbol *sym;
1767 struct sm_state *sm;
1768 struct symbol *compare_type, *var_type;
1769 struct range_list *rl;
1770 struct range_list *limit;
1771 struct range_list *new;
1773 while (expr->type == EXPR_ASSIGNMENT)
1774 expr = strip_expr(expr->right);
1775 if (expr->type != EXPR_CALL)
1776 return;
1778 arg = get_argument_from_call_expr(expr->args, param);
1779 if (!arg)
1780 return;
1782 name = get_variable_from_key(arg, key, &sym);
1783 if (!name || !sym)
1784 goto free;
1786 if (strcmp(key, "$") == 0)
1787 compare_type = get_arg_type(expr->fn, param);
1788 else
1789 compare_type = get_member_type_from_key(arg, key);
1791 sm = get_sm_state(SMATCH_EXTRA, name, sym);
1792 if (sm)
1793 rl = estate_rl(sm->state);
1794 else
1795 rl = alloc_whole_rl(compare_type);
1797 call_results_to_rl(expr, compare_type, value, &limit);
1798 new = rl_intersection(rl, limit);
1800 var_type = get_member_type_from_key(arg, key);
1801 new = cast_rl(var_type, new);
1803 /* We want to preserve the implications here */
1804 if (sm && rl_equiv(estate_rl(sm->state), new))
1805 __set_sm(sm);
1806 else {
1807 if (op == PARAM_LIMIT)
1808 set_extra_nomod(name, sym, alloc_estate_rl(new));
1809 else
1810 set_extra_mod(name, sym, alloc_estate_rl(new));
1813 free:
1814 free_string(name);
1817 static void db_param_limit(struct expression *expr, int param, char *key, char *value)
1819 db_param_limit_filter(expr, param, key, value, PARAM_LIMIT);
1822 static void db_param_filter(struct expression *expr, int param, char *key, char *value)
1824 db_param_limit_filter(expr, param, key, value, PARAM_FILTER);
1827 static void db_param_add_set(struct expression *expr, int param, char *key, char *value, enum info_type op)
1829 struct expression *arg;
1830 char *name;
1831 struct symbol *sym;
1832 struct symbol *type;
1833 struct smatch_state *state;
1834 struct range_list *new = NULL;
1835 struct range_list *added = NULL;
1837 while (expr->type == EXPR_ASSIGNMENT)
1838 expr = strip_expr(expr->right);
1839 if (expr->type != EXPR_CALL)
1840 return;
1842 arg = get_argument_from_call_expr(expr->args, param);
1843 if (!arg)
1844 return;
1845 type = get_member_type_from_key(arg, key);
1846 name = get_variable_from_key(arg, key, &sym);
1847 if (!name || !sym)
1848 goto free;
1850 state = get_state(SMATCH_EXTRA, name, sym);
1851 if (state)
1852 new = estate_rl(state);
1854 call_results_to_rl(expr, type, value, &added);
1856 if (op == PARAM_SET)
1857 new = added;
1858 else
1859 new = rl_union(new, added);
1861 set_extra_mod(name, sym, alloc_estate_rl(new));
1862 free:
1863 free_string(name);
1866 static void db_param_add(struct expression *expr, int param, char *key, char *value)
1868 db_param_add_set(expr, param, key, value, PARAM_ADD);
1871 static void db_param_set(struct expression *expr, int param, char *key, char *value)
1873 db_param_add_set(expr, param, key, value, PARAM_SET);
1876 static void db_param_value(struct expression *expr, int param, char *key, char *value)
1878 struct expression *call;
1879 char *name;
1880 struct symbol *sym;
1881 struct symbol *type;
1882 struct range_list *rl = NULL;
1884 if (param != -1)
1885 return;
1887 call = expr;
1888 while (call->type == EXPR_ASSIGNMENT)
1889 call = strip_expr(call->right);
1890 if (call->type != EXPR_CALL)
1891 return;
1893 type = get_member_type_from_key(expr->left, key);
1894 name = get_variable_from_key(expr->left, key, &sym);
1895 if (!name || !sym)
1896 goto free;
1898 call_results_to_rl(call, type, value, &rl);
1900 set_extra_mod(name, sym, alloc_estate_rl(rl));
1901 free:
1902 free_string(name);
1905 static void match_call_info(struct expression *expr)
1907 struct smatch_state *state;
1908 struct range_list *rl = NULL;
1909 struct expression *arg;
1910 struct symbol *type;
1911 int i = 0;
1913 FOR_EACH_PTR(expr->args, arg) {
1914 type = get_arg_type(expr->fn, i);
1916 if (get_implied_rl(arg, &rl))
1917 rl = cast_rl(type, rl);
1918 else
1919 rl = cast_rl(type, alloc_whole_rl(get_type(arg)));
1921 if (!is_whole_rl(rl))
1922 sql_insert_caller_info(expr, PARAM_VALUE, i, "$", show_rl(rl));
1923 state = get_state_expr(SMATCH_EXTRA, arg);
1924 if (estate_has_fuzzy_max(state)) {
1925 sql_insert_caller_info(expr, FUZZY_MAX, i, "$",
1926 sval_to_str(estate_get_fuzzy_max(state)));
1928 i++;
1929 } END_FOR_EACH_PTR(arg);
1932 static void set_param_value(const char *name, struct symbol *sym, char *key, char *value)
1934 struct range_list *rl = NULL;
1935 struct smatch_state *state;
1936 struct symbol *type;
1937 char fullname[256];
1939 if (strcmp(key, "*$") == 0)
1940 snprintf(fullname, sizeof(fullname), "*%s", name);
1941 else if (strncmp(key, "$", 1) == 0)
1942 snprintf(fullname, 256, "%s%s", name, key + 1);
1943 else
1944 return;
1946 type = get_member_type_from_key(symbol_expression(sym), key);
1947 str_to_rl(type, value, &rl);
1948 state = alloc_estate_rl(rl);
1949 set_state(SMATCH_EXTRA, fullname, sym, state);
1952 static void set_param_hard_max(const char *name, struct symbol *sym, char *key, char *value)
1954 struct range_list *rl = NULL;
1955 struct smatch_state *state;
1956 struct symbol *type;
1957 char fullname[256];
1958 sval_t max;
1960 if (strcmp(key, "*$") == 0)
1961 snprintf(fullname, sizeof(fullname), "*%s", name);
1962 else if (strncmp(key, "$", 1) == 0)
1963 snprintf(fullname, 256, "%s%s", name, key + 1);
1964 else
1965 return;
1967 state = get_state(SMATCH_EXTRA, fullname, sym);
1968 if (!state)
1969 return;
1970 type = get_member_type_from_key(symbol_expression(sym), key);
1971 str_to_rl(type, value, &rl);
1972 if (!rl_to_sval(rl, &max))
1973 return;
1974 estate_set_fuzzy_max(state, max);
1977 struct smatch_state *get_extra_state(struct expression *expr)
1979 char *name;
1980 struct symbol *sym;
1981 struct smatch_state *ret = NULL;
1982 struct range_list *rl;
1984 if (is_pointer(expr) && get_address_rl(expr, &rl))
1985 return alloc_estate_rl(rl);
1987 name = expr_to_known_chunk_sym(expr, &sym);
1988 if (!name)
1989 goto free;
1991 ret = get_state(SMATCH_EXTRA, name, sym);
1992 free:
1993 free_string(name);
1994 return ret;
1997 void register_smatch_extra(int id)
1999 my_id = id;
2001 add_merge_hook(my_id, &merge_estates);
2002 add_unmatched_state_hook(my_id, &unmatched_state);
2003 select_caller_info_hook(set_param_value, PARAM_VALUE);
2004 select_caller_info_hook(set_param_hard_max, FUZZY_MAX);
2005 select_return_states_before(&db_limited_before);
2006 select_return_states_hook(PARAM_LIMIT, &db_param_limit);
2007 select_return_states_hook(PARAM_FILTER, &db_param_filter);
2008 select_return_states_hook(PARAM_ADD, &db_param_add);
2009 select_return_states_hook(PARAM_SET, &db_param_set);
2010 select_return_states_hook(PARAM_VALUE, &db_param_value);
2011 select_return_states_after(&db_limited_after);
2014 static void match_link_modify(struct sm_state *sm, struct expression *mod_expr)
2016 struct var_sym_list *links;
2017 struct var_sym *tmp;
2018 struct smatch_state *state;
2020 links = sm->state->data;
2022 FOR_EACH_PTR(links, tmp) {
2023 state = get_state(SMATCH_EXTRA, tmp->var, tmp->sym);
2024 if (!state)
2025 continue;
2026 set_state(SMATCH_EXTRA, tmp->var, tmp->sym, alloc_estate_whole(estate_type(state)));
2027 } END_FOR_EACH_PTR(tmp);
2028 set_state(link_id, sm->name, sm->sym, &undefined);
2031 void register_smatch_extra_links(int id)
2033 link_id = id;
2036 void register_smatch_extra_late(int id)
2038 add_merge_hook(link_id, &merge_link_states);
2039 add_modification_hook(link_id, &match_link_modify);
2040 add_hook(&match_dereferences, DEREF_HOOK);
2041 add_hook(&match_pointer_as_array, OP_HOOK);
2042 select_call_implies_hook(DEREFERENCE, &set_param_dereferenced);
2043 add_hook(&match_function_call, FUNCTION_CALL_HOOK);
2044 add_hook(&match_assign, ASSIGNMENT_HOOK);
2045 add_hook(&match_assign, GLOBAL_ASSIGNMENT_HOOK);
2046 add_hook(&unop_expr, OP_HOOK);
2047 add_hook(&asm_expr, ASM_HOOK);
2048 add_untracked_param_hook(&match_untracked_array);
2050 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
2051 add_member_info_callback(my_id, struct_member_callback);
2052 add_split_return_callback(&returned_struct_members);
2054 add_hook(&assume_indexes_are_valid, OP_HOOK);