leaks: don't complain if we overwrite NULL pointers
[smatch.git] / smatch_extra.c
blobb5fc720a71f457f1df3beea0c57232a79d14705d
1 /*
2 * Copyright (C) 2008 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
19 * smatch_extra.c is supposed to track the value of every variable.
23 #define _GNU_SOURCE
24 #include <string.h>
26 #include <stdlib.h>
27 #include <errno.h>
28 #ifndef __USE_ISOC99
29 #define __USE_ISOC99
30 #endif
31 #include <limits.h>
32 #include "parse.h"
33 #include "smatch.h"
34 #include "smatch_slist.h"
35 #include "smatch_extra.h"
37 static int my_id;
38 static int link_id;
40 static void match_link_modify(struct sm_state *sm, struct expression *mod_expr);
42 struct string_list *__ignored_macros = NULL;
43 static int in_warn_on_macro(void)
45 struct statement *stmt;
46 char *tmp;
47 char *macro;
49 stmt = get_current_statement();
50 if (!stmt)
51 return 0;
52 macro = get_macro_name(stmt->pos);
53 if (!macro)
54 return 0;
56 FOR_EACH_PTR(__ignored_macros, tmp) {
57 if (!strcmp(tmp, macro))
58 return 1;
59 } END_FOR_EACH_PTR(tmp);
60 return 0;
63 typedef void (mod_hook)(const char *name, struct symbol *sym, struct smatch_state *state);
64 DECLARE_PTR_LIST(void_fn_list, mod_hook *);
65 static struct void_fn_list *extra_mod_hooks;
66 void add_extra_mod_hook(mod_hook *fn)
68 mod_hook **p = malloc(sizeof(mod_hook *));
69 *p = fn;
70 add_ptr_list(&extra_mod_hooks, p);
73 void call_extra_mod_hooks(const char *name, struct symbol *sym, struct smatch_state *state)
75 mod_hook **fn;
77 FOR_EACH_PTR(extra_mod_hooks, fn) {
78 (*fn)(name, sym, state);
79 } END_FOR_EACH_PTR(fn);
82 struct sm_state *set_extra_mod_helper(const char *name, struct symbol *sym, struct smatch_state *state)
84 remove_from_equiv(name, sym);
85 call_extra_mod_hooks(name, sym, state);
86 if (__in_fake_assign && estate_is_unknown(state) && !get_state(SMATCH_EXTRA, name, sym))
87 return NULL;
88 return set_state(SMATCH_EXTRA, name, sym, state);
91 char *get_other_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym)
93 struct expression *assigned;
94 char *orig_name = NULL;
95 char buf[256];
96 char *ret = NULL;
97 int skip;
99 *new_sym = NULL;
101 if (!sym || !sym->ident)
102 return NULL;
104 skip = strlen(sym->ident->name);
105 if (name[skip] != '-' || name[skip + 1] != '>')
106 return NULL;
107 skip += 2;
109 assigned = get_assigned_expr_name_sym(sym->ident->name, sym);
110 if (!assigned)
111 return NULL;
112 if (assigned->type == EXPR_PREOP || assigned->op == '&') {
114 orig_name = expr_to_var_sym(assigned, new_sym);
115 if (!orig_name || !*new_sym)
116 goto free;
118 snprintf(buf, sizeof(buf), "%s.%s", orig_name + 1, name + skip);
119 ret = alloc_string(buf);
120 free_string(orig_name);
121 return ret;
124 if (assigned->type != EXPR_DEREF)
125 goto free;
127 orig_name = expr_to_var_sym(assigned, new_sym);
128 if (!orig_name || !*new_sym)
129 goto free;
131 snprintf(buf, sizeof(buf), "%s->%s", orig_name, name + skip);
132 ret = alloc_string(buf);
133 free_string(orig_name);
134 return ret;
136 free:
137 free_string(orig_name);
138 return NULL;
141 struct sm_state *set_extra_mod(const char *name, struct symbol *sym, struct smatch_state *state)
143 char *new_name;
144 struct symbol *new_sym;
145 struct sm_state *sm;
147 sm = set_extra_mod_helper(name, sym, state);
148 new_name = get_other_name_sym(name, sym, &new_sym);
149 if (new_name && new_sym)
150 set_extra_mod_helper(new_name, new_sym, state);
151 free_string(new_name);
152 return sm;
155 static void clear_array_states(struct expression *array)
157 struct sm_state *sm;
159 sm = get_sm_state_expr(link_id, array);
160 if (sm)
161 match_link_modify(sm, NULL);
164 static struct sm_state *set_extra_array_mod(struct expression *expr, struct smatch_state *state)
166 struct expression *array;
167 struct var_sym_list *vsl;
168 struct var_sym *vs;
169 char *name;
170 struct symbol *sym;
171 struct sm_state *ret = NULL;
173 array = get_array_base(expr);
175 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
176 if (!name || !vsl) {
177 clear_array_states(array);
178 goto free;
181 FOR_EACH_PTR(vsl, vs) {
182 store_link(link_id, vs->var, vs->sym, name, sym);
183 } END_FOR_EACH_PTR(vs);
185 call_extra_mod_hooks(name, sym, state);
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);
243 void set_extra_nomod_vsl(const char *name, struct symbol *sym, struct var_sym_list *vsl, struct smatch_state *state)
245 struct var_sym *vs;
247 FOR_EACH_PTR(vsl, vs) {
248 store_link(link_id, vs->var, vs->sym, name, sym);
249 } END_FOR_EACH_PTR(vs);
251 set_extra_nomod(name, sym, state);
255 * This is for return_implies_state() hooks which modify a SMATCH_EXTRA state
257 void set_extra_expr_nomod(struct expression *expr, struct smatch_state *state)
259 struct var_sym_list *vsl;
260 struct var_sym *vs;
261 char *name;
262 struct symbol *sym;
264 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
265 if (!name || !vsl)
266 goto free;
267 FOR_EACH_PTR(vsl, vs) {
268 store_link(link_id, vs->var, vs->sym, name, sym);
269 } END_FOR_EACH_PTR(vs);
271 set_extra_nomod(name, sym, state);
272 free:
273 free_string(name);
276 static void set_extra_true_false(const char *name, struct symbol *sym,
277 struct smatch_state *true_state,
278 struct smatch_state *false_state)
280 char *new_name;
281 struct symbol *new_sym;
282 struct relation *rel;
283 struct smatch_state *orig_state;
285 if (!true_state && !false_state)
286 return;
288 if (in_warn_on_macro())
289 return;
291 new_name = get_other_name_sym(name, sym, &new_sym);
292 if (new_name && new_sym)
293 set_true_false_states(SMATCH_EXTRA, new_name, new_sym, true_state, false_state);
294 free_string(new_name);
296 orig_state = get_state(SMATCH_EXTRA, name, sym);
298 if (!estate_related(orig_state)) {
299 set_true_false_states(SMATCH_EXTRA, name, sym, true_state, false_state);
300 return;
303 if (true_state)
304 set_related(true_state, estate_related(orig_state));
305 if (false_state)
306 set_related(false_state, estate_related(orig_state));
308 FOR_EACH_PTR(estate_related(orig_state), rel) {
309 set_true_false_states(SMATCH_EXTRA, rel->name, rel->sym,
310 true_state, false_state);
311 } END_FOR_EACH_PTR(rel);
314 static void set_extra_chunk_true_false(struct expression *expr,
315 struct smatch_state *true_state,
316 struct smatch_state *false_state)
318 struct var_sym_list *vsl;
319 struct var_sym *vs;
320 struct symbol *type;
321 char *name;
322 struct symbol *sym;
324 if (in_warn_on_macro())
325 return;
327 type = get_type(expr);
328 if (!type)
329 return;
331 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
332 if (!name || !vsl)
333 goto free;
334 FOR_EACH_PTR(vsl, vs) {
335 store_link(link_id, vs->var, vs->sym, name, sym);
336 } END_FOR_EACH_PTR(vs);
338 set_true_false_states(SMATCH_EXTRA, name, sym,
339 clone_estate(true_state),
340 clone_estate(false_state));
341 free:
342 free_string(name);
345 static void set_extra_expr_true_false(struct expression *expr,
346 struct smatch_state *true_state,
347 struct smatch_state *false_state)
349 char *name;
350 struct symbol *sym;
351 sval_t sval;
353 if (!true_state && !false_state)
354 return;
356 if (get_value(expr, &sval))
357 return;
359 expr = strip_expr(expr);
360 name = expr_to_var_sym(expr, &sym);
361 if (!name || !sym) {
362 free_string(name);
363 set_extra_chunk_true_false(expr, true_state, false_state);
364 return;
366 set_extra_true_false(name, sym, true_state, false_state);
367 free_string(name);
370 static struct sm_state *handle_canonical_while_count_down(struct statement *loop)
372 struct expression *iter_var;
373 struct expression *condition;
374 struct sm_state *sm;
375 struct smatch_state *estate;
376 sval_t start;
378 condition = strip_expr(loop->iterator_pre_condition);
379 if (!condition)
380 return NULL;
381 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
382 return NULL;
383 if (condition->op != SPECIAL_DECREMENT)
384 return NULL;
386 iter_var = condition->unop;
387 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
388 if (!sm)
389 return NULL;
390 if (sval_cmp_val(estate_min(sm->state), 0) < 0)
391 return NULL;
392 start = estate_max(sm->state);
393 if (sval_cmp_val(start, 0) <= 0)
394 return NULL;
395 if (!sval_is_max(start))
396 start.value--;
398 if (condition->type == EXPR_PREOP) {
399 estate = alloc_estate_range(sval_type_val(start.type, 1), start);
400 if (estate_has_hard_max(sm->state))
401 estate_set_hard_max(estate);
402 estate_copy_fuzzy_max(estate, sm->state);
403 set_extra_expr_mod(iter_var, estate);
405 if (condition->type == EXPR_POSTOP) {
406 estate = alloc_estate_range(sval_type_val(start.type, 0), start);
407 if (estate_has_hard_max(sm->state))
408 estate_set_hard_max(estate);
409 estate_copy_fuzzy_max(estate, sm->state);
410 set_extra_expr_mod(iter_var, estate);
412 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
415 static struct sm_state *handle_canonical_for_inc(struct expression *iter_expr,
416 struct expression *condition)
418 struct expression *iter_var;
419 struct sm_state *sm;
420 struct smatch_state *estate;
421 sval_t start, end, max;
423 iter_var = iter_expr->unop;
424 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
425 if (!sm)
426 return NULL;
427 if (!estate_get_single_value(sm->state, &start))
428 return NULL;
429 if (get_implied_max(condition->right, &end))
430 end = sval_cast(get_type(iter_var), end);
431 else
432 end = sval_type_max(get_type(iter_var));
434 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
435 return NULL;
437 switch (condition->op) {
438 case SPECIAL_UNSIGNED_LT:
439 case SPECIAL_NOTEQUAL:
440 case '<':
441 if (!sval_is_min(end))
442 end.value--;
443 break;
444 case SPECIAL_UNSIGNED_LTE:
445 case SPECIAL_LTE:
446 break;
447 default:
448 return NULL;
450 if (sval_cmp(end, start) < 0)
451 return NULL;
452 estate = alloc_estate_range(start, end);
453 if (get_hard_max(condition->right, &max)) {
454 estate_set_hard_max(estate);
455 if (condition->op == '<' ||
456 condition->op == SPECIAL_UNSIGNED_LT ||
457 condition->op == SPECIAL_NOTEQUAL)
458 max.value--;
459 estate_set_fuzzy_max(estate, max);
461 set_extra_expr_mod(iter_var, estate);
462 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
465 static struct sm_state *handle_canonical_for_dec(struct expression *iter_expr,
466 struct expression *condition)
468 struct expression *iter_var;
469 struct sm_state *sm;
470 struct smatch_state *estate;
471 sval_t start, end;
473 iter_var = iter_expr->unop;
474 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
475 if (!sm)
476 return NULL;
477 if (!estate_get_single_value(sm->state, &start))
478 return NULL;
479 if (!get_implied_min(condition->right, &end))
480 end = sval_type_min(get_type(iter_var));
481 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
482 return NULL;
484 switch (condition->op) {
485 case SPECIAL_NOTEQUAL:
486 case '>':
487 if (!sval_is_min(end) && !sval_is_max(end))
488 end.value++;
489 break;
490 case SPECIAL_GTE:
491 break;
492 default:
493 return NULL;
495 if (sval_cmp(end, start) > 0)
496 return NULL;
497 estate = alloc_estate_range(end, start);
498 estate_set_hard_max(estate);
499 estate_set_fuzzy_max(estate, estate_get_fuzzy_max(estate));
500 set_extra_expr_mod(iter_var, estate);
501 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
504 static struct sm_state *handle_canonical_for_loops(struct statement *loop)
506 struct expression *iter_expr;
507 struct expression *condition;
509 if (!loop->iterator_post_statement)
510 return NULL;
511 if (loop->iterator_post_statement->type != STMT_EXPRESSION)
512 return NULL;
513 iter_expr = loop->iterator_post_statement->expression;
514 if (!loop->iterator_pre_condition)
515 return NULL;
516 if (loop->iterator_pre_condition->type != EXPR_COMPARE)
517 return NULL;
518 condition = loop->iterator_pre_condition;
520 if (iter_expr->op == SPECIAL_INCREMENT)
521 return handle_canonical_for_inc(iter_expr, condition);
522 if (iter_expr->op == SPECIAL_DECREMENT)
523 return handle_canonical_for_dec(iter_expr, condition);
524 return NULL;
527 struct sm_state *__extra_handle_canonical_loops(struct statement *loop, struct stree **stree)
529 struct sm_state *ret;
531 __push_fake_cur_stree();
532 if (!loop->iterator_post_statement)
533 ret = handle_canonical_while_count_down(loop);
534 else
535 ret = handle_canonical_for_loops(loop);
536 *stree = __pop_fake_cur_stree();
537 return ret;
540 int __iterator_unchanged(struct sm_state *sm)
542 if (!sm)
543 return 0;
544 if (get_sm_state(my_id, sm->name, sm->sym) == sm)
545 return 1;
546 return 0;
549 static void while_count_down_after(struct sm_state *sm, struct expression *condition)
551 sval_t after_value;
553 /* paranoid checking. prolly not needed */
554 condition = strip_expr(condition);
555 if (!condition)
556 return;
557 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
558 return;
559 if (condition->op != SPECIAL_DECREMENT)
560 return;
561 after_value = estate_min(sm->state);
562 after_value.value--;
563 set_extra_mod(sm->name, sm->sym, alloc_estate_sval(after_value));
566 void __extra_pre_loop_hook_after(struct sm_state *sm,
567 struct statement *iterator,
568 struct expression *condition)
570 struct expression *iter_expr;
571 sval_t limit;
572 struct smatch_state *state;
574 if (!iterator) {
575 while_count_down_after(sm, condition);
576 return;
579 iter_expr = iterator->expression;
581 if (condition->type != EXPR_COMPARE)
582 return;
583 if (iter_expr->op == SPECIAL_INCREMENT) {
584 limit = sval_binop(estate_max(sm->state), '+',
585 sval_type_val(estate_type(sm->state), 1));
586 } else {
587 limit = sval_binop(estate_min(sm->state), '-',
588 sval_type_val(estate_type(sm->state), 1));
590 if (!estate_has_hard_max(sm->state) && !__has_breaks()) {
591 if (iter_expr->op == SPECIAL_INCREMENT)
592 state = alloc_estate_range(estate_min(sm->state), limit);
593 else
594 state = alloc_estate_range(limit, estate_max(sm->state));
595 } else {
596 state = alloc_estate_sval(limit);
598 if (!estate_has_hard_max(sm->state)) {
599 estate_clear_hard_max(state);
601 if (estate_has_fuzzy_max(sm->state)) {
602 sval_t hmax = estate_get_fuzzy_max(sm->state);
603 sval_t max = estate_max(sm->state);
605 if (sval_cmp(hmax, max) != 0)
606 estate_clear_fuzzy_max(state);
607 } else if (!estate_has_fuzzy_max(sm->state)) {
608 estate_clear_fuzzy_max(state);
611 set_extra_mod(sm->name, sm->sym, state);
614 static struct stree *unmatched_stree;
615 static struct smatch_state *unmatched_state(struct sm_state *sm)
617 struct smatch_state *state;
619 if (unmatched_stree) {
620 state = get_state_stree(unmatched_stree, SMATCH_EXTRA, sm->name, sm->sym);
621 if (state)
622 return state;
624 if (parent_is_gone_var_sym(sm->name, sm->sym))
625 return alloc_estate_empty();
626 return alloc_estate_whole(estate_type(sm->state));
629 static void clear_the_pointed_at(struct expression *expr)
631 struct stree *stree;
632 char *name;
633 struct symbol *sym;
634 struct sm_state *tmp;
636 name = expr_to_var_sym(expr, &sym);
637 if (!name || !sym)
638 goto free;
640 stree = __get_cur_stree();
641 FOR_EACH_MY_SM(SMATCH_EXTRA, stree, tmp) {
642 if (tmp->name[0] != '*')
643 continue;
644 if (tmp->sym != sym)
645 continue;
646 if (strcmp(tmp->name + 1, name) != 0)
647 continue;
648 set_extra_mod(tmp->name, tmp->sym, alloc_estate_whole(estate_type(tmp->state)));
649 } END_FOR_EACH_SM(tmp);
651 free:
652 free_string(name);
655 static void match_function_call(struct expression *expr)
657 struct expression *arg;
658 struct expression *tmp;
660 /* if we have the db this is handled in smatch_function_hooks.c */
661 if (!option_no_db)
662 return;
663 if (inlinable(expr->fn))
664 return;
666 FOR_EACH_PTR(expr->args, arg) {
667 tmp = strip_expr(arg);
668 if (tmp->type == EXPR_PREOP && tmp->op == '&')
669 set_extra_expr_mod(tmp->unop, alloc_estate_whole(get_type(tmp->unop)));
670 else
671 clear_the_pointed_at(tmp);
672 } END_FOR_EACH_PTR(arg);
675 static int values_fit_type(struct expression *left, struct expression *right)
677 struct range_list *rl;
678 struct symbol *type;
680 type = get_type(left);
681 if (!type)
682 return 0;
683 get_absolute_rl(right, &rl);
684 if (type_unsigned(type) && sval_is_negative(rl_min(rl)))
685 return 0;
686 if (sval_cmp(sval_type_min(type), rl_min(rl)) > 0)
687 return 0;
688 if (sval_cmp(sval_type_max(type), rl_max(rl)) < 0)
689 return 0;
690 return 1;
693 static void save_chunk_info(struct expression *left, struct expression *right)
695 struct var_sym_list *vsl;
696 struct var_sym *vs;
697 struct expression *add_expr;
698 struct symbol *type;
699 sval_t sval;
700 char *name;
701 struct symbol *sym;
703 if (right->type != EXPR_BINOP || right->op != '-')
704 return;
705 if (!get_value(right->left, &sval))
706 return;
707 if (!expr_to_sym(right->right))
708 return;
710 add_expr = binop_expression(left, '+', right->right);
711 type = get_type(add_expr);
712 if (!type)
713 return;
714 name = expr_to_chunk_sym_vsl(add_expr, &sym, &vsl);
715 if (!name || !vsl)
716 goto free;
717 FOR_EACH_PTR(vsl, vs) {
718 store_link(link_id, vs->var, vs->sym, name, sym);
719 } END_FOR_EACH_PTR(vs);
721 set_state(SMATCH_EXTRA, name, sym, alloc_estate_sval(sval_cast(type, sval)));
722 free:
723 free_string(name);
726 static void do_array_assign(struct expression *left, int op, struct expression *right)
728 struct range_list *rl;
730 if (op == '=') {
731 get_absolute_rl(right, &rl);
732 rl = cast_rl(get_type(left), rl);
733 } else {
734 rl = alloc_whole_rl(get_type(left));
737 set_extra_array_mod(left, alloc_estate_rl(rl));
740 static void match_untracked_array(struct expression *call, int param)
742 struct expression *arg;
744 arg = get_argument_from_call_expr(call->args, param);
745 arg = strip_expr(arg);
747 clear_array_states(arg);
750 static void match_vanilla_assign(struct expression *left, struct expression *right)
752 struct range_list *orig_rl = NULL;
753 struct range_list *rl = NULL;
754 struct symbol *right_sym;
755 struct symbol *left_type;
756 struct symbol *right_type;
757 char *right_name = NULL;
758 struct symbol *sym;
759 char *name;
760 sval_t max;
761 struct smatch_state *state;
762 int comparison;
764 if (is_struct(left))
765 return;
767 save_chunk_info(left, right);
769 name = expr_to_var_sym(left, &sym);
770 if (!name) {
771 if (is_array(left))
772 do_array_assign(left, '=', right);
773 return;
776 left_type = get_type(left);
777 right_type = get_type(right);
779 right_name = expr_to_var_sym(right, &right_sym);
781 if (!__in_fake_assign &&
782 !(right->type == EXPR_PREOP && right->op == '&') &&
783 right_name && right_sym &&
784 values_fit_type(left, right) &&
785 !has_symbol(right, sym)) {
786 set_equiv(left, right);
787 goto free;
790 if (is_pointer(right) && get_address_rl(right, &rl)) {
791 state = alloc_estate_rl(rl);
792 goto done;
795 if (__in_fake_assign) {
796 struct smatch_state *right_state;
797 sval_t sval;
799 if (get_value(right, &sval)) {
800 sval = sval_cast(left_type, sval);
801 state = alloc_estate_sval(sval);
802 goto done;
805 right_state = get_state(SMATCH_EXTRA, right_name, right_sym);
806 if (right_state) {
807 /* simple assignment */
808 state = clone_estate(right_state);
809 goto done;
812 state = alloc_estate_rl(alloc_whole_rl(left_type));
813 goto done;
816 comparison = get_comparison(left, right);
817 if (comparison) {
818 comparison = flip_comparison(comparison);
819 get_implied_rl(left, &orig_rl);
822 if (get_implied_rl(right, &rl)) {
823 rl = cast_rl(left_type, rl);
824 if (orig_rl)
825 filter_by_comparison(&rl, comparison, orig_rl);
826 state = alloc_estate_rl(rl);
827 if (get_hard_max(right, &max)) {
828 estate_set_hard_max(state);
829 estate_set_fuzzy_max(state, max);
831 } else {
832 rl = alloc_whole_rl(right_type);
833 rl = cast_rl(left_type, rl);
834 if (orig_rl)
835 filter_by_comparison(&rl, comparison, orig_rl);
836 state = alloc_estate_rl(rl);
839 done:
840 set_extra_mod(name, sym, state);
841 free:
842 free_string(right_name);
845 static int op_remove_assign(int op)
847 switch (op) {
848 case SPECIAL_ADD_ASSIGN:
849 return '+';
850 case SPECIAL_SUB_ASSIGN:
851 return '-';
852 case SPECIAL_MUL_ASSIGN:
853 return '*';
854 case SPECIAL_DIV_ASSIGN:
855 return '/';
856 case SPECIAL_MOD_ASSIGN:
857 return '%';
858 case SPECIAL_AND_ASSIGN:
859 return '&';
860 case SPECIAL_OR_ASSIGN:
861 return '|';
862 case SPECIAL_XOR_ASSIGN:
863 return '^';
864 case SPECIAL_SHL_ASSIGN:
865 return SPECIAL_LEFTSHIFT;
866 case SPECIAL_SHR_ASSIGN:
867 return SPECIAL_RIGHTSHIFT;
868 default:
869 return op;
873 static void match_assign(struct expression *expr)
875 struct range_list *rl = NULL;
876 struct expression *left;
877 struct expression *right;
878 struct expression *binop_expr;
879 struct symbol *left_type;
880 struct symbol *sym;
881 char *name;
882 sval_t left_min, left_max;
883 sval_t right_min, right_max;
884 sval_t res_min, res_max;
886 left = strip_expr(expr->left);
888 right = strip_parens(expr->right);
889 if (right->type == EXPR_CALL && sym_name_is("__builtin_expect", right->fn))
890 right = get_argument_from_call_expr(right->args, 0);
891 while (right->type == EXPR_ASSIGNMENT && right->op == '=')
892 right = strip_parens(right->left);
894 if (expr->op == '=' && is_condition(expr->right))
895 return; /* handled in smatch_condition.c */
896 if (expr->op == '=' && right->type == EXPR_CALL)
897 return; /* handled in smatch_function_hooks.c */
898 if (expr->op == '=') {
899 match_vanilla_assign(left, right);
900 return;
903 name = expr_to_var_sym(left, &sym);
904 if (!name)
905 return;
907 left_type = get_type(left);
909 res_min = sval_type_min(left_type);
910 res_max = sval_type_max(left_type);
912 switch (expr->op) {
913 case SPECIAL_ADD_ASSIGN:
914 get_absolute_max(left, &left_max);
915 get_absolute_max(right, &right_max);
916 if (sval_binop_overflows(left_max, '+', right_max))
917 break;
918 if (get_implied_min(left, &left_min) &&
919 !sval_is_negative_min(left_min) &&
920 get_implied_min(right, &right_min) &&
921 !sval_is_negative_min(right_min)) {
922 res_min = sval_binop(left_min, '+', right_min);
923 res_min = sval_cast(left_type, res_min);
925 if (inside_loop()) /* we are assuming loops don't lead to wrapping */
926 break;
927 res_max = sval_binop(left_max, '+', right_max);
928 res_max = sval_cast(left_type, res_max);
929 break;
930 case SPECIAL_SUB_ASSIGN:
931 if (get_implied_max(left, &left_max) &&
932 !sval_is_max(left_max) &&
933 get_implied_min(right, &right_min) &&
934 !sval_is_min(right_min)) {
935 res_max = sval_binop(left_max, '-', right_min);
936 res_max = sval_cast(left_type, res_max);
938 if (inside_loop())
939 break;
940 if (get_implied_min(left, &left_min) &&
941 !sval_is_min(left_min) &&
942 get_implied_max(right, &right_max) &&
943 !sval_is_max(right_max) &&
944 sval_cmp(left_min, right_max) > 0) {
945 res_min = sval_binop(left_min, '-', right_max);
946 res_min = sval_cast(left_type, res_min);
948 break;
949 case SPECIAL_AND_ASSIGN:
950 case SPECIAL_MOD_ASSIGN:
951 case SPECIAL_SHL_ASSIGN:
952 case SPECIAL_SHR_ASSIGN:
953 case SPECIAL_OR_ASSIGN:
954 case SPECIAL_XOR_ASSIGN:
955 case SPECIAL_MUL_ASSIGN:
956 case SPECIAL_DIV_ASSIGN:
957 binop_expr = binop_expression(expr->left,
958 op_remove_assign(expr->op),
959 expr->right);
960 if (get_absolute_rl(binop_expr, &rl)) {
961 rl = cast_rl(left_type, rl);
962 set_extra_mod(name, sym, alloc_estate_rl(rl));
963 goto free;
965 break;
967 rl = cast_rl(left_type, alloc_rl(res_min, res_max));
968 set_extra_mod(name, sym, alloc_estate_rl(rl));
969 free:
970 free_string(name);
973 static struct smatch_state *increment_state(struct smatch_state *state)
975 sval_t min = estate_min(state);
976 sval_t max = estate_max(state);
978 if (!estate_rl(state))
979 return NULL;
981 if (inside_loop())
982 max = sval_type_max(max.type);
984 if (!sval_is_min(min) && !sval_is_max(min))
985 min.value++;
986 if (!sval_is_min(max) && !sval_is_max(max))
987 max.value++;
988 return alloc_estate_range(min, max);
991 static struct smatch_state *decrement_state(struct smatch_state *state)
993 sval_t min = estate_min(state);
994 sval_t max = estate_max(state);
996 if (!estate_rl(state))
997 return NULL;
999 if (inside_loop())
1000 min = sval_type_min(min.type);
1002 if (!sval_is_min(min) && !sval_is_max(min))
1003 min.value--;
1004 if (!sval_is_min(max) && !sval_is_max(max))
1005 max.value--;
1006 return alloc_estate_range(min, max);
1009 static void clear_pointed_at_state(struct expression *expr)
1011 struct symbol *type;
1014 * ALERT: This is sort of a mess. If it's is a struct assigment like
1015 * "foo = bar;", then that's handled by smatch_struct_assignment.c.
1016 * the same thing for p++ where "p" is a struct. Most modifications
1017 * are handled by the assignment hook or the db. Smatch_extra.c doesn't
1018 * use smatch_modification.c because we have to get the ordering right
1019 * or something. So if you have p++ where p is a pointer to a standard
1020 * c type then we handle that here. What a mess.
1023 type = get_type(expr);
1024 if (!type || type->type != SYM_PTR)
1025 return;
1026 type = get_real_base_type(type);
1027 if (!type || type->type != SYM_BASETYPE)
1028 return;
1029 set_extra_expr_mod(deref_expression(expr), alloc_estate_whole(type));
1032 static void unop_expr(struct expression *expr)
1034 struct smatch_state *state;
1036 if (expr->smatch_flags & Handled)
1037 return;
1039 switch (expr->op) {
1040 case SPECIAL_INCREMENT:
1041 state = get_state_expr(SMATCH_EXTRA, expr->unop);
1042 state = increment_state(state);
1043 if (!state)
1044 state = alloc_estate_whole(get_type(expr));
1045 set_extra_expr_mod(expr->unop, state);
1046 clear_pointed_at_state(expr->unop);
1047 break;
1048 case SPECIAL_DECREMENT:
1049 state = get_state_expr(SMATCH_EXTRA, expr->unop);
1050 state = decrement_state(state);
1051 if (!state)
1052 state = alloc_estate_whole(get_type(expr));
1053 set_extra_expr_mod(expr->unop, state);
1054 clear_pointed_at_state(expr->unop);
1055 break;
1056 default:
1057 return;
1061 static void asm_expr(struct statement *stmt)
1064 struct expression *expr;
1065 struct symbol *type;
1066 int state = 0;
1068 FOR_EACH_PTR(stmt->asm_outputs, expr) {
1069 switch (state) {
1070 case 0: /* identifier */
1071 case 1: /* constraint */
1072 state++;
1073 continue;
1074 case 2: /* expression */
1075 state = 0;
1076 type = get_type(strip_expr(expr));
1077 set_extra_expr_mod(expr, alloc_estate_whole(type));
1078 continue;
1080 } END_FOR_EACH_PTR(expr);
1083 static void check_dereference(struct expression *expr)
1085 if (__in_fake_assign)
1086 return;
1087 if (outside_of_function())
1088 return;
1089 if (implied_not_equal(expr, 0))
1090 return;
1091 set_extra_expr_nomod(expr, alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval));
1094 static void match_dereferences(struct expression *expr)
1096 if (expr->type != EXPR_PREOP)
1097 return;
1098 /* it's saying that foo[1] = bar dereferences foo[1] */
1099 if (is_array(expr))
1100 return;
1101 check_dereference(expr->unop);
1104 static void match_pointer_as_array(struct expression *expr)
1106 if (!is_array(expr))
1107 return;
1108 check_dereference(get_array_base(expr));
1111 static void find_dereferences(struct expression *expr)
1113 while (expr->type == EXPR_PREOP) {
1114 if (expr->op == '*')
1115 check_dereference(expr->unop);
1116 expr = strip_expr(expr->unop);
1120 static void set_param_dereferenced(struct expression *arg, char *key, char *unused)
1122 struct symbol *sym;
1123 char *name;
1125 name = get_variable_from_key(arg, key, &sym);
1126 if (name && sym)
1127 set_extra_nomod(name, sym, alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval));
1128 free_string(name);
1130 find_dereferences(arg);
1133 static sval_t add_one(sval_t sval)
1135 sval.value++;
1136 return sval;
1139 static int handle_postop_inc(struct expression *left, int op, struct expression *right)
1141 struct statement *stmt;
1142 struct expression *cond;
1143 struct smatch_state *true_state, *false_state;
1144 sval_t start;
1145 sval_t limit;
1148 * If we're decrementing here then that's a canonical while count down
1149 * so it's handled already. We're only handling loops like:
1150 * i = 0;
1151 * do { ... } while (i++ < 3);
1154 if (left->type != EXPR_POSTOP || left->op != SPECIAL_INCREMENT)
1155 return 0;
1157 stmt = __cur_stmt->parent;
1158 if (!stmt)
1159 return 0;
1160 if (stmt->type == STMT_COMPOUND)
1161 stmt = stmt->parent;
1162 if (!stmt || stmt->type != STMT_ITERATOR || !stmt->iterator_post_condition)
1163 return 0;
1165 cond = strip_expr(stmt->iterator_post_condition);
1166 if (cond->type != EXPR_COMPARE || cond->op != op)
1167 return 0;
1168 if (left != strip_expr(cond->left) || right != strip_expr(cond->right))
1169 return 0;
1171 if (!get_implied_value(left->unop, &start))
1172 return 0;
1173 if (!get_implied_value(right, &limit))
1174 return 0;
1176 if (sval_cmp(start, limit) > 0)
1177 return 0;
1179 switch (op) {
1180 case '<':
1181 case SPECIAL_UNSIGNED_LT:
1182 break;
1183 case SPECIAL_LTE:
1184 case SPECIAL_UNSIGNED_LTE:
1185 limit = add_one(limit);
1186 default:
1187 return 0;
1191 true_state = alloc_estate_range(add_one(start), limit);
1192 false_state = alloc_estate_range(add_one(limit), add_one(limit));
1194 /* Currently we just discard the false state but when two passes is
1195 * implimented correctly then it will use it.
1198 set_extra_expr_true_false(left->unop, true_state, false_state);
1200 return 1;
1203 static void handle_comparison(struct symbol *type, struct expression *left, int op, struct expression *right)
1205 struct range_list *left_orig;
1206 struct range_list *left_true;
1207 struct range_list *left_false;
1208 struct range_list *right_orig;
1209 struct range_list *right_true;
1210 struct range_list *right_false;
1211 struct smatch_state *left_true_state;
1212 struct smatch_state *left_false_state;
1213 struct smatch_state *right_true_state;
1214 struct smatch_state *right_false_state;
1215 sval_t dummy, hard_max;
1216 int left_postop = 0;
1217 int right_postop = 0;
1219 if (left->op == SPECIAL_INCREMENT || left->op == SPECIAL_DECREMENT) {
1220 if (left->type == EXPR_POSTOP) {
1221 left->smatch_flags |= Handled;
1222 left_postop = left->op;
1223 if (handle_postop_inc(left, op, right))
1224 return;
1226 left = strip_parens(left->unop);
1228 while (left->type == EXPR_ASSIGNMENT)
1229 left = strip_parens(left->left);
1231 if (right->op == SPECIAL_INCREMENT || right->op == SPECIAL_DECREMENT) {
1232 if (right->type == EXPR_POSTOP) {
1233 right->smatch_flags |= Handled;
1234 right_postop = right->op;
1236 right = strip_parens(right->unop);
1239 get_real_absolute_rl(left, &left_orig);
1240 left_orig = cast_rl(type, left_orig);
1242 get_real_absolute_rl(right, &right_orig);
1243 right_orig = cast_rl(type, right_orig);
1245 split_comparison_rl(left_orig, op, right_orig, &left_true, &left_false, &right_true, &right_false);
1247 left_true = rl_truncate_cast(get_type(strip_expr(left)), left_true);
1248 left_false = rl_truncate_cast(get_type(strip_expr(left)), left_false);
1249 right_true = rl_truncate_cast(get_type(strip_expr(right)), right_true);
1250 right_false = rl_truncate_cast(get_type(strip_expr(right)), right_false);
1252 if (!left_true || !left_false) {
1253 struct range_list *tmp_true, *tmp_false;
1255 split_comparison_rl(alloc_whole_rl(type), op, right_orig, &tmp_true, &tmp_false, NULL, NULL);
1256 tmp_true = rl_truncate_cast(get_type(strip_expr(left)), tmp_true);
1257 tmp_false = rl_truncate_cast(get_type(strip_expr(left)), tmp_false);
1258 if (tmp_true && tmp_false)
1259 __save_imaginary_state(left, tmp_true, tmp_false);
1262 if (!right_true || !right_false) {
1263 struct range_list *tmp_true, *tmp_false;
1265 split_comparison_rl(alloc_whole_rl(type), op, right_orig, NULL, NULL, &tmp_true, &tmp_false);
1266 tmp_true = rl_truncate_cast(get_type(strip_expr(right)), tmp_true);
1267 tmp_false = rl_truncate_cast(get_type(strip_expr(right)), tmp_false);
1268 if (tmp_true && tmp_false)
1269 __save_imaginary_state(right, tmp_true, tmp_false);
1272 left_true_state = alloc_estate_rl(left_true);
1273 left_false_state = alloc_estate_rl(left_false);
1274 right_true_state = alloc_estate_rl(right_true);
1275 right_false_state = alloc_estate_rl(right_false);
1277 switch (op) {
1278 case '<':
1279 case SPECIAL_UNSIGNED_LT:
1280 case SPECIAL_UNSIGNED_LTE:
1281 case SPECIAL_LTE:
1282 if (get_hard_max(right, &dummy))
1283 estate_set_hard_max(left_true_state);
1284 if (get_hard_max(left, &dummy))
1285 estate_set_hard_max(right_false_state);
1286 break;
1287 case '>':
1288 case SPECIAL_UNSIGNED_GT:
1289 case SPECIAL_UNSIGNED_GTE:
1290 case SPECIAL_GTE:
1291 if (get_hard_max(left, &dummy))
1292 estate_set_hard_max(right_true_state);
1293 if (get_hard_max(right, &dummy))
1294 estate_set_hard_max(left_false_state);
1295 break;
1298 switch (op) {
1299 case '<':
1300 case SPECIAL_UNSIGNED_LT:
1301 case SPECIAL_UNSIGNED_LTE:
1302 case SPECIAL_LTE:
1303 if (get_hard_max(right, &hard_max)) {
1304 if (op == '<' || op == SPECIAL_UNSIGNED_LT)
1305 hard_max.value--;
1306 estate_set_fuzzy_max(left_true_state, hard_max);
1308 if (get_implied_value(right, &hard_max)) {
1309 if (op == SPECIAL_UNSIGNED_LTE ||
1310 op == SPECIAL_LTE)
1311 hard_max.value++;
1312 estate_set_fuzzy_max(left_false_state, hard_max);
1314 if (get_hard_max(left, &hard_max)) {
1315 if (op == SPECIAL_UNSIGNED_LTE ||
1316 op == SPECIAL_LTE)
1317 hard_max.value--;
1318 estate_set_fuzzy_max(right_false_state, hard_max);
1320 if (get_implied_value(left, &hard_max)) {
1321 if (op == '<' || op == SPECIAL_UNSIGNED_LT)
1322 hard_max.value++;
1323 estate_set_fuzzy_max(right_true_state, hard_max);
1325 break;
1326 case '>':
1327 case SPECIAL_UNSIGNED_GT:
1328 case SPECIAL_UNSIGNED_GTE:
1329 case SPECIAL_GTE:
1330 if (get_hard_max(left, &hard_max)) {
1331 if (op == '>' || op == SPECIAL_UNSIGNED_GT)
1332 hard_max.value--;
1333 estate_set_fuzzy_max(right_true_state, hard_max);
1335 if (get_implied_value(left, &hard_max)) {
1336 if (op == SPECIAL_UNSIGNED_GTE ||
1337 op == SPECIAL_GTE)
1338 hard_max.value++;
1339 estate_set_fuzzy_max(right_false_state, hard_max);
1341 if (get_hard_max(right, &hard_max)) {
1342 if (op == SPECIAL_UNSIGNED_LTE ||
1343 op == SPECIAL_LTE)
1344 hard_max.value--;
1345 estate_set_fuzzy_max(left_false_state, hard_max);
1347 if (get_implied_value(right, &hard_max)) {
1348 if (op == '>' ||
1349 op == SPECIAL_UNSIGNED_GT)
1350 hard_max.value++;
1351 estate_set_fuzzy_max(left_true_state, hard_max);
1353 break;
1354 case SPECIAL_EQUAL:
1355 if (get_hard_max(left, &hard_max))
1356 estate_set_fuzzy_max(right_true_state, hard_max);
1357 if (get_hard_max(right, &hard_max))
1358 estate_set_fuzzy_max(left_true_state, hard_max);
1359 break;
1362 if (get_hard_max(left, &hard_max)) {
1363 estate_set_hard_max(left_true_state);
1364 estate_set_hard_max(left_false_state);
1366 if (get_hard_max(right, &hard_max)) {
1367 estate_set_hard_max(right_true_state);
1368 estate_set_hard_max(right_false_state);
1371 if (left_postop == SPECIAL_INCREMENT) {
1372 left_true_state = increment_state(left_true_state);
1373 left_false_state = increment_state(left_false_state);
1375 if (left_postop == SPECIAL_DECREMENT) {
1376 left_true_state = decrement_state(left_true_state);
1377 left_false_state = decrement_state(left_false_state);
1379 if (right_postop == SPECIAL_INCREMENT) {
1380 right_true_state = increment_state(right_true_state);
1381 right_false_state = increment_state(right_false_state);
1383 if (right_postop == SPECIAL_DECREMENT) {
1384 right_true_state = decrement_state(right_true_state);
1385 right_false_state = decrement_state(right_false_state);
1388 if (estate_rl(left_true_state) && estates_equiv(left_true_state, left_false_state)) {
1389 left_true_state = NULL;
1390 left_false_state = NULL;
1393 if (estate_rl(right_true_state) && estates_equiv(right_true_state, right_false_state)) {
1394 right_true_state = NULL;
1395 right_false_state = NULL;
1398 set_extra_expr_true_false(left, left_true_state, left_false_state);
1399 set_extra_expr_true_false(right, right_true_state, right_false_state);
1402 static int is_simple_math(struct expression *expr)
1404 if (!expr)
1405 return 0;
1406 if (expr->type != EXPR_BINOP)
1407 return 0;
1408 switch (expr->op) {
1409 case '+':
1410 case '-':
1411 case '*':
1412 return 1;
1414 return 0;
1417 static void move_known_values(struct expression **left_p, struct expression **right_p)
1419 struct expression *left = *left_p;
1420 struct expression *right = *right_p;
1421 sval_t sval, dummy;
1423 if (get_implied_value(left, &sval)) {
1424 if (!is_simple_math(right))
1425 return;
1426 if (get_implied_value(right, &dummy))
1427 return;
1428 if (right->op == '*') {
1429 sval_t divisor;
1431 if (!get_value(right->right, &divisor))
1432 return;
1433 if (divisor.value == 0 && sval.value % divisor.value)
1434 return;
1435 *left_p = binop_expression(left, invert_op(right->op), right->right);
1436 *right_p = right->left;
1437 return;
1439 if (right->op == '+' && get_value(right->left, &sval)) {
1440 *left_p = binop_expression(left, invert_op(right->op), right->left);
1441 *right_p = right->right;
1442 return;
1444 if (get_value(right->right, &sval)) {
1445 *left_p = binop_expression(left, invert_op(right->op), right->right);
1446 *right_p = right->left;
1447 return;
1449 return;
1451 if (get_implied_value(right, &sval)) {
1452 if (!is_simple_math(left))
1453 return;
1454 if (get_implied_value(left, &dummy))
1455 return;
1456 if (left->op == '*') {
1457 sval_t divisor;
1459 if (!get_value(left->right, &divisor))
1460 return;
1461 if (divisor.value == 0 && sval.value % divisor.value)
1462 return;
1463 *right_p = binop_expression(right, invert_op(left->op), left->right);
1464 *left_p = left->left;
1465 return;
1467 if (left->op == '+' && get_value(left->left, &sval)) {
1468 *right_p = binop_expression(right, invert_op(left->op), left->left);
1469 *left_p = left->right;
1470 return;
1473 if (get_value(left->right, &sval)) {
1474 *right_p = binop_expression(right, invert_op(left->op), left->right);
1475 *left_p = left->left;
1476 return;
1478 return;
1482 static int match_func_comparison(struct expression *expr)
1484 struct expression *left = strip_expr(expr->left);
1485 struct expression *right = strip_expr(expr->right);
1486 sval_t sval;
1489 * fixme: think about this harder. We should always be trying to limit
1490 * the non-call side as well. If we can't determine the limitter does
1491 * that mean we aren't querying the database and are missing important
1492 * information?
1495 if (left->type == EXPR_CALL) {
1496 if (get_implied_value(left, &sval)) {
1497 handle_comparison(get_type(expr), left, expr->op, right);
1498 return 1;
1500 function_comparison(left, expr->op, right);
1501 return 1;
1504 if (right->type == EXPR_CALL) {
1505 if (get_implied_value(right, &sval)) {
1506 handle_comparison(get_type(expr), left, expr->op, right);
1507 return 1;
1509 function_comparison(left, expr->op, right);
1510 return 1;
1513 return 0;
1516 /* Handle conditions like "if (foo + bar < foo) {" */
1517 static int handle_integer_overflow_test(struct expression *expr)
1519 struct expression *left, *right;
1520 struct symbol *type;
1521 sval_t left_min, right_min, min, max;
1523 if (expr->op != '<' && expr->op != SPECIAL_UNSIGNED_LT)
1524 return 0;
1526 left = strip_parens(expr->left);
1527 right = strip_parens(expr->right);
1529 if (left->op != '+')
1530 return 0;
1532 type = get_type(expr);
1533 if (!type)
1534 return 0;
1535 if (type_positive_bits(type) == 32) {
1536 max.type = &uint_ctype;
1537 max.uvalue = (unsigned int)-1;
1538 } else if (type_positive_bits(type) == 64) {
1539 max.type = &ulong_ctype;
1540 max.value = (unsigned long long)-1;
1541 } else {
1542 return 0;
1545 if (!expr_equiv(left->left, right) && !expr_equiv(left->right, right))
1546 return 0;
1548 get_absolute_min(left->left, &left_min);
1549 get_absolute_min(left->right, &right_min);
1550 min = sval_binop(left_min, '+', right_min);
1552 set_extra_chunk_true_false(left, NULL, alloc_estate_range(min, max));
1553 return 1;
1556 static void match_comparison(struct expression *expr)
1558 struct expression *left_orig = strip_parens(expr->left);
1559 struct expression *right_orig = strip_parens(expr->right);
1560 struct expression *left, *right;
1561 struct expression *prev;
1562 struct symbol *type;
1564 if (match_func_comparison(expr))
1565 return;
1567 type = get_type(expr);
1568 if (!type)
1569 type = &llong_ctype;
1571 if (handle_integer_overflow_test(expr))
1572 return;
1574 left = left_orig;
1575 right = right_orig;
1576 move_known_values(&left, &right);
1577 handle_comparison(type, left, expr->op, right);
1579 prev = get_assigned_expr(left_orig);
1580 if (is_simple_math(prev) && has_variable(prev, left_orig) == 0) {
1581 left = prev;
1582 right = right_orig;
1583 move_known_values(&left, &right);
1584 handle_comparison(type, left, expr->op, right);
1587 prev = get_assigned_expr(right_orig);
1588 if (is_simple_math(prev) && has_variable(prev, right_orig) == 0) {
1589 left = left_orig;
1590 right = prev;
1591 move_known_values(&left, &right);
1592 handle_comparison(type, left, expr->op, right);
1596 static sval_t get_high_mask(sval_t known)
1598 sval_t ret;
1599 int i;
1601 ret = known;
1602 ret.value = 0;
1604 for (i = type_bits(known.type) - 1; i >= 0; i--) {
1605 if (known.uvalue & (1ULL << i))
1606 ret.uvalue |= (1ULL << i);
1607 else
1608 return ret;
1611 return ret;
1614 static void handle_AND_condition(struct expression *expr)
1616 struct range_list *orig_rl;
1617 struct range_list *true_rl = NULL;
1618 struct range_list *false_rl = NULL;
1619 sval_t known;
1620 int bit;
1622 if (get_implied_value(expr->left, &known)) {
1623 sval_t low_mask = known;
1624 sval_t high_mask;
1626 if (known.value > 0) {
1627 bit = ffsll(known.value) - 1;
1628 low_mask.uvalue = (1ULL << bit) - 1;
1629 get_absolute_rl(expr->right, &orig_rl);
1630 true_rl = remove_range(orig_rl, sval_type_val(known.type, 0), low_mask);
1632 high_mask = get_high_mask(known);
1633 if (high_mask.value) {
1634 bit = ffsll(high_mask.value) - 1;
1635 low_mask.uvalue = (1ULL << bit) - 1;
1637 get_absolute_rl(expr->left, &orig_rl);
1638 if (sval_is_negative(rl_min(orig_rl)))
1639 orig_rl = remove_range(orig_rl, sval_type_min(known.type), sval_type_val(known.type, -1));
1640 false_rl = remove_range(orig_rl, low_mask, sval_type_max(known.type));
1641 if (type_signed(high_mask.type) && type_unsigned(rl_type(false_rl))) {
1642 false_rl = remove_range(false_rl,
1643 sval_type_val(rl_type(false_rl), sval_type_max(known.type).uvalue),
1644 sval_type_val(rl_type(false_rl), -1));
1647 set_extra_expr_true_false(expr->right,
1648 true_rl ? alloc_estate_rl(true_rl) : NULL,
1649 false_rl ? alloc_estate_rl(false_rl) : NULL);
1651 return;
1654 if (get_implied_value(expr->right, &known)) {
1655 sval_t low_mask = known;
1656 sval_t high_mask;
1658 if (known.value > 0) {
1659 bit = ffsll(known.value) - 1;
1660 low_mask.uvalue = (1ULL << bit) - 1;
1661 get_absolute_rl(expr->left, &orig_rl);
1662 true_rl = remove_range(orig_rl, sval_type_val(known.type, 0), low_mask);
1664 high_mask = get_high_mask(known);
1665 if (high_mask.value) {
1666 bit = ffsll(high_mask.value) - 1;
1667 low_mask.uvalue = (1ULL << bit) - 1;
1669 get_absolute_rl(expr->left, &orig_rl);
1670 if (sval_is_negative(rl_min(orig_rl)))
1671 orig_rl = remove_range(orig_rl, sval_type_min(known.type), sval_type_val(known.type, -1));
1672 false_rl = remove_range(orig_rl, low_mask, sval_type_max(known.type));
1673 if (type_signed(high_mask.type) && type_unsigned(rl_type(false_rl))) {
1674 false_rl = remove_range(false_rl,
1675 sval_type_val(rl_type(false_rl), sval_type_max(known.type).uvalue),
1676 sval_type_val(rl_type(false_rl), -1));
1679 set_extra_expr_true_false(expr->left,
1680 true_rl ? alloc_estate_rl(true_rl) : NULL,
1681 false_rl ? alloc_estate_rl(false_rl) : NULL);
1682 return;
1686 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
1687 void __extra_match_condition(struct expression *expr)
1689 struct smatch_state *pre_state;
1690 struct smatch_state *true_state;
1691 struct smatch_state *false_state;
1693 expr = strip_expr(expr);
1694 switch (expr->type) {
1695 case EXPR_CALL:
1696 function_comparison(expr, SPECIAL_NOTEQUAL, zero_expr());
1697 return;
1698 case EXPR_PREOP:
1699 case EXPR_SYMBOL:
1700 case EXPR_DEREF: {
1701 sval_t zero;
1703 zero = sval_blank(expr);
1704 zero.value = 0;
1706 pre_state = get_extra_state(expr);
1707 true_state = estate_filter_sval(pre_state, zero);
1708 if (possibly_true(expr, SPECIAL_EQUAL, zero_expr()))
1709 false_state = alloc_estate_sval(zero);
1710 else
1711 false_state = alloc_estate_empty();
1712 set_extra_expr_true_false(expr, true_state, false_state);
1713 return;
1715 case EXPR_COMPARE:
1716 match_comparison(expr);
1717 return;
1718 case EXPR_ASSIGNMENT:
1719 __extra_match_condition(expr->left);
1720 return;
1721 case EXPR_BINOP:
1722 if (expr->op == '&')
1723 handle_AND_condition(expr);
1724 return;
1728 static void assume_indexes_are_valid(struct expression *expr)
1730 struct expression *array_expr;
1731 int array_size;
1732 struct expression *offset;
1733 struct symbol *offset_type;
1734 struct range_list *rl_before;
1735 struct range_list *rl_after;
1736 struct range_list *filter = NULL;
1737 sval_t size;
1739 expr = strip_expr(expr);
1740 if (!is_array(expr))
1741 return;
1743 offset = get_array_offset(expr);
1744 offset_type = get_type(offset);
1745 if (offset_type && type_signed(offset_type)) {
1746 filter = alloc_rl(sval_type_min(offset_type),
1747 sval_type_val(offset_type, -1));
1750 array_expr = get_array_base(expr);
1751 array_size = get_real_array_size(array_expr);
1752 if (array_size > 1) {
1753 size = sval_type_val(offset_type, array_size);
1754 add_range(&filter, size, sval_type_max(offset_type));
1757 if (!filter)
1758 return;
1759 get_absolute_rl(offset, &rl_before);
1760 rl_after = rl_filter(rl_before, filter);
1761 if (rl_equiv(rl_before, rl_after))
1762 return;
1763 set_extra_expr_nomod(offset, alloc_estate_rl(rl_after));
1766 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
1767 int implied_not_equal(struct expression *expr, long long val)
1769 return !possibly_false(expr, SPECIAL_NOTEQUAL, value_expr(val));
1772 int implied_not_equal_name_sym(char *name, struct symbol *sym, long long val)
1774 struct smatch_state *estate;
1776 estate = get_state(SMATCH_EXTRA, name, sym);
1777 if (!estate)
1778 return 0;
1779 if (!rl_has_sval(estate_rl(estate), sval_type_val(estate_type(estate), 0)))
1780 return 1;
1781 return 0;
1784 int parent_is_null_var_sym(const char *name, struct symbol *sym)
1786 char buf[256];
1787 char *start;
1788 char *end;
1789 struct smatch_state *state;
1791 strncpy(buf, name, sizeof(buf) - 1);
1792 buf[sizeof(buf) - 1] = '\0';
1794 start = &buf[0];
1795 while (*start == '*') {
1796 start++;
1797 state = get_state(SMATCH_EXTRA, start, sym);
1798 if (!state)
1799 continue;
1800 if (!estate_rl(state))
1801 return 1;
1802 if (estate_min(state).value == 0 &&
1803 estate_max(state).value == 0)
1804 return 1;
1807 start = &buf[0];
1808 while (*start == '&')
1809 start++;
1811 while ((end = strrchr(start, '-'))) {
1812 *end = '\0';
1813 state = get_state(SMATCH_EXTRA, start, sym);
1814 if (!state)
1815 continue;
1816 if (estate_min(state).value == 0 &&
1817 estate_max(state).value == 0)
1818 return 1;
1820 return 0;
1823 int parent_is_null(struct expression *expr)
1825 struct symbol *sym;
1826 char *var;
1827 int ret = 0;
1829 expr = strip_expr(expr);
1830 var = expr_to_var_sym(expr, &sym);
1831 if (!var || !sym)
1832 goto free;
1833 ret = parent_is_null_var_sym(var, sym);
1834 free:
1835 free_string(var);
1836 return ret;
1839 static int param_used_callback(void *found, int argc, char **argv, char **azColName)
1841 *(int *)found = 1;
1842 return 0;
1845 static int filter_unused_kzalloc_info(struct expression *call, int param, char *printed_name, struct sm_state *sm)
1847 sval_t sval;
1848 int found = 0;
1850 /* for function pointers assume everything is used */
1851 if (call->fn->type != EXPR_SYMBOL)
1852 return 0;
1855 * kzalloc() information is treated as special because so there is just
1856 * a lot of stuff initialized to zero and it makes building the database
1857 * take hours and hours.
1859 * In theory, we should just remove this line and not pass any unused
1860 * information, but I'm not sure enough that this code works so I want
1861 * to hold off on that for now.
1863 if (!estate_get_single_value(sm->state, &sval) || sval.value != 0)
1864 return 0;
1866 run_sql(&param_used_callback, &found,
1867 "select * from call_implies where %s and type = %d and parameter = %d and key = '%s';",
1868 get_static_filter(call->fn->symbol), PARAM_USED, param, printed_name);
1869 if (found)
1870 return 0;
1872 /* If the database is not built yet, then assume everything is used */
1873 run_sql(&param_used_callback, &found,
1874 "select * from call_implies where %s and type = %d;",
1875 get_static_filter(call->fn->symbol), PARAM_USED);
1876 if (!found)
1877 return 0;
1879 return 1;
1882 struct range_list *intersect_with_real_abs_var_sym(const char *name, struct symbol *sym, struct range_list *start)
1884 struct smatch_state *state;
1887 * Here is the difference between implied value and real absolute, say
1888 * you have:
1890 * int a = (u8)x;
1892 * Then you know that a is 0-255. That's real absolute. But you don't
1893 * know for sure that it actually goes up to 255. So it's not implied.
1894 * Implied indicates a degree of certainty.
1896 * But then say you cap "a" at 8. That means you know it goes up to
1897 * 8. So now the implied value is s32min-8. But you can combine it
1898 * with the real absolute to say that actually it's 0-8.
1900 * We are combining it here. But now that I think about it, this is
1901 * probably not the ideal place to combine it because it should proably
1902 * be done earlier. Oh well, this is an improvement on what was there
1903 * before so I'm going to commit this code.
1907 state = get_real_absolute_state_var_sym(name, sym);
1908 if (!state || !estate_rl(state))
1909 return start;
1911 return rl_intersection(estate_rl(state), start);
1914 struct range_list *intersect_with_real_abs_expr(struct expression *expr, struct range_list *start)
1916 struct smatch_state *state;
1918 state = get_real_absolute_state(expr);
1919 if (!state || !estate_rl(state))
1920 return start;
1922 return rl_intersection(estate_rl(state), start);
1925 static void struct_member_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
1927 struct range_list *rl;
1929 if (estate_is_whole(sm->state))
1930 return;
1931 if (filter_unused_kzalloc_info(call, param, printed_name, sm))
1932 return;
1933 rl = estate_rl(sm->state);
1934 rl = intersect_with_real_abs_var_sym(sm->name, sm->sym, rl);
1935 sql_insert_caller_info(call, PARAM_VALUE, param, printed_name, show_rl(rl));
1936 if (estate_has_fuzzy_max(sm->state))
1937 sql_insert_caller_info(call, FUZZY_MAX, param, printed_name,
1938 sval_to_str(estate_get_fuzzy_max(sm->state)));
1941 static void returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
1943 struct symbol *returned_sym;
1944 struct sm_state *sm;
1945 const char *param_name;
1946 char *compare_str;
1947 char buf[256];
1949 returned_sym = expr_to_sym(expr);
1950 if (!returned_sym)
1951 return;
1953 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
1954 if (!estate_rl(sm->state))
1955 continue;
1956 if (returned_sym != sm->sym)
1957 continue;
1959 param_name = get_param_name(sm);
1960 if (!param_name)
1961 continue;
1962 if (strcmp(param_name, "$") == 0)
1963 continue;
1964 compare_str = name_sym_to_param_comparison(sm->name, sm->sym);
1965 if (!compare_str && estate_is_whole(sm->state))
1966 continue;
1967 snprintf(buf, sizeof(buf), "%s%s", sm->state->name, compare_str ?: "");
1969 sql_insert_return_states(return_id, return_ranges, PARAM_VALUE,
1970 -1, param_name, buf);
1971 } END_FOR_EACH_SM(sm);
1974 static void db_limited_before(void)
1976 unmatched_stree = clone_stree(__get_cur_stree());
1979 static void db_limited_after(void)
1981 free_stree(&unmatched_stree);
1984 static int rl_fits_in_type(struct range_list *rl, struct symbol *type)
1986 if (type_bits(rl_type(rl)) <= type_bits(type))
1987 return 1;
1988 if (sval_cmp(rl_max(rl), sval_type_max(type)) > 0)
1989 return 0;
1990 if (sval_is_negative(rl_min(rl)) &&
1991 sval_cmp(rl_min(rl), sval_type_min(type)) < 0)
1992 return 0;
1993 return 1;
1996 static void db_param_limit_filter(struct expression *expr, int param, char *key, char *value, enum info_type op)
1998 struct expression *arg;
1999 char *name;
2000 struct symbol *sym;
2001 struct var_sym_list *vsl = NULL;
2002 struct sm_state *sm;
2003 struct symbol *compare_type, *var_type;
2004 struct range_list *rl;
2005 struct range_list *limit;
2006 struct range_list *new;
2008 while (expr->type == EXPR_ASSIGNMENT)
2009 expr = strip_expr(expr->right);
2010 if (expr->type != EXPR_CALL)
2011 return;
2013 arg = get_argument_from_call_expr(expr->args, param);
2014 if (!arg)
2015 return;
2017 name = get_chunk_from_key(arg, key, &sym, &vsl);
2018 if (!name)
2019 return;
2020 if (op != PARAM_LIMIT && !sym)
2021 goto free;
2023 if (strcmp(key, "$") == 0)
2024 compare_type = get_arg_type(expr->fn, param);
2025 else
2026 compare_type = get_member_type_from_key(arg, key);
2028 sm = get_sm_state(SMATCH_EXTRA, name, sym);
2029 if (sm)
2030 rl = estate_rl(sm->state);
2031 else
2032 rl = alloc_whole_rl(compare_type);
2034 if (op == PARAM_LIMIT && !rl_fits_in_type(rl, compare_type))
2035 goto free;
2037 call_results_to_rl(expr, compare_type, value, &limit);
2038 new = rl_intersection(rl, limit);
2040 var_type = get_member_type_from_key(arg, key);
2041 new = cast_rl(var_type, new);
2043 /* We want to preserve the implications here */
2044 if (sm && rl_equiv(estate_rl(sm->state), new))
2045 __set_sm(sm);
2046 else {
2047 if (op == PARAM_LIMIT)
2048 set_extra_nomod_vsl(name, sym, vsl, alloc_estate_rl(new));
2049 else
2050 set_extra_mod(name, sym, alloc_estate_rl(new));
2053 free:
2054 free_string(name);
2057 static void db_param_limit(struct expression *expr, int param, char *key, char *value)
2059 db_param_limit_filter(expr, param, key, value, PARAM_LIMIT);
2062 static void db_param_filter(struct expression *expr, int param, char *key, char *value)
2064 db_param_limit_filter(expr, param, key, value, PARAM_FILTER);
2067 static void db_param_add_set(struct expression *expr, int param, char *key, char *value, enum info_type op)
2069 struct expression *arg;
2070 char *name;
2071 struct symbol *sym;
2072 struct symbol *type;
2073 struct smatch_state *state;
2074 struct range_list *new = NULL;
2075 struct range_list *added = NULL;
2077 while (expr->type == EXPR_ASSIGNMENT)
2078 expr = strip_expr(expr->right);
2079 if (expr->type != EXPR_CALL)
2080 return;
2082 arg = get_argument_from_call_expr(expr->args, param);
2083 if (!arg)
2084 return;
2085 type = get_member_type_from_key(arg, key);
2086 name = get_variable_from_key(arg, key, &sym);
2087 if (!name || !sym)
2088 goto free;
2090 state = get_state(SMATCH_EXTRA, name, sym);
2091 if (state)
2092 new = estate_rl(state);
2094 call_results_to_rl(expr, type, value, &added);
2096 if (op == PARAM_SET)
2097 new = added;
2098 else
2099 new = rl_union(new, added);
2101 set_extra_mod(name, sym, alloc_estate_rl(new));
2102 free:
2103 free_string(name);
2106 static void db_param_add(struct expression *expr, int param, char *key, char *value)
2108 db_param_add_set(expr, param, key, value, PARAM_ADD);
2111 static void db_param_set(struct expression *expr, int param, char *key, char *value)
2113 db_param_add_set(expr, param, key, value, PARAM_SET);
2116 static void db_param_value(struct expression *expr, int param, char *key, char *value)
2118 struct expression *call;
2119 char *name;
2120 struct symbol *sym;
2121 struct symbol *type;
2122 struct range_list *rl = NULL;
2124 if (param != -1)
2125 return;
2127 call = expr;
2128 while (call->type == EXPR_ASSIGNMENT)
2129 call = strip_expr(call->right);
2130 if (call->type != EXPR_CALL)
2131 return;
2133 type = get_member_type_from_key(expr->left, key);
2134 name = get_variable_from_key(expr->left, key, &sym);
2135 if (!name || !sym)
2136 goto free;
2138 call_results_to_rl(call, type, value, &rl);
2140 set_extra_mod(name, sym, alloc_estate_rl(rl));
2141 free:
2142 free_string(name);
2145 static void match_call_info(struct expression *expr)
2147 struct smatch_state *state;
2148 struct range_list *rl = NULL;
2149 struct expression *arg;
2150 struct symbol *type;
2151 int i = 0;
2153 FOR_EACH_PTR(expr->args, arg) {
2154 type = get_arg_type(expr->fn, i);
2156 if (get_implied_rl(arg, &rl))
2157 rl = cast_rl(type, rl);
2158 else
2159 rl = cast_rl(type, alloc_whole_rl(get_type(arg)));
2161 if (!is_whole_rl(rl)) {
2162 rl = intersect_with_real_abs_expr(arg, rl);
2163 sql_insert_caller_info(expr, PARAM_VALUE, i, "$", show_rl(rl));
2165 state = get_state_expr(SMATCH_EXTRA, arg);
2166 if (estate_has_fuzzy_max(state)) {
2167 sql_insert_caller_info(expr, FUZZY_MAX, i, "$",
2168 sval_to_str(estate_get_fuzzy_max(state)));
2170 i++;
2171 } END_FOR_EACH_PTR(arg);
2174 static void set_param_value(const char *name, struct symbol *sym, char *key, char *value)
2176 struct range_list *rl = NULL;
2177 struct smatch_state *state;
2178 struct symbol *type;
2179 char fullname[256];
2180 sval_t dummy;
2182 if (strcmp(key, "*$") == 0)
2183 snprintf(fullname, sizeof(fullname), "*%s", name);
2184 else if (strncmp(key, "$", 1) == 0)
2185 snprintf(fullname, 256, "%s%s", name, key + 1);
2186 else
2187 return;
2189 type = get_member_type_from_key(symbol_expression(sym), key);
2190 str_to_rl(type, value, &rl);
2191 state = alloc_estate_rl(rl);
2192 if (estate_get_single_value(state, &dummy))
2193 estate_set_hard_max(state);
2194 set_state(SMATCH_EXTRA, fullname, sym, state);
2197 static void set_param_hard_max(const char *name, struct symbol *sym, char *key, char *value)
2199 struct range_list *rl = NULL;
2200 struct smatch_state *state;
2201 struct symbol *type;
2202 char fullname[256];
2203 sval_t max;
2205 if (strcmp(key, "*$") == 0)
2206 snprintf(fullname, sizeof(fullname), "*%s", name);
2207 else if (strncmp(key, "$", 1) == 0)
2208 snprintf(fullname, 256, "%s%s", name, key + 1);
2209 else
2210 return;
2212 state = get_state(SMATCH_EXTRA, fullname, sym);
2213 if (!state)
2214 return;
2215 type = get_member_type_from_key(symbol_expression(sym), key);
2216 str_to_rl(type, value, &rl);
2217 if (!rl_to_sval(rl, &max))
2218 return;
2219 estate_set_fuzzy_max(state, max);
2222 struct smatch_state *get_extra_state(struct expression *expr)
2224 char *name;
2225 struct symbol *sym;
2226 struct smatch_state *ret = NULL;
2227 struct range_list *rl;
2229 if (is_pointer(expr) && get_address_rl(expr, &rl))
2230 return alloc_estate_rl(rl);
2232 name = expr_to_known_chunk_sym(expr, &sym);
2233 if (!name)
2234 goto free;
2236 ret = get_state(SMATCH_EXTRA, name, sym);
2237 free:
2238 free_string(name);
2239 return ret;
2242 void register_smatch_extra(int id)
2244 my_id = id;
2246 add_merge_hook(my_id, &merge_estates);
2247 add_unmatched_state_hook(my_id, &unmatched_state);
2248 select_caller_info_hook(set_param_value, PARAM_VALUE);
2249 select_caller_info_hook(set_param_hard_max, FUZZY_MAX);
2250 select_return_states_before(&db_limited_before);
2251 select_return_states_hook(PARAM_LIMIT, &db_param_limit);
2252 select_return_states_hook(PARAM_FILTER, &db_param_filter);
2253 select_return_states_hook(PARAM_ADD, &db_param_add);
2254 select_return_states_hook(PARAM_SET, &db_param_set);
2255 select_return_states_hook(PARAM_VALUE, &db_param_value);
2256 select_return_states_after(&db_limited_after);
2259 static void match_link_modify(struct sm_state *sm, struct expression *mod_expr)
2261 struct var_sym_list *links;
2262 struct var_sym *tmp;
2263 struct smatch_state *state;
2265 links = sm->state->data;
2267 FOR_EACH_PTR(links, tmp) {
2268 if (sm->sym == tmp->sym &&
2269 strcmp(sm->name, tmp->var) == 0)
2270 continue;
2271 state = get_state(SMATCH_EXTRA, tmp->var, tmp->sym);
2272 if (!state)
2273 continue;
2274 set_state(SMATCH_EXTRA, tmp->var, tmp->sym, alloc_estate_whole(estate_type(state)));
2275 } END_FOR_EACH_PTR(tmp);
2276 set_state(link_id, sm->name, sm->sym, &undefined);
2279 void register_smatch_extra_links(int id)
2281 link_id = id;
2284 void register_smatch_extra_late(int id)
2286 add_merge_hook(link_id, &merge_link_states);
2287 add_modification_hook(link_id, &match_link_modify);
2288 add_hook(&match_dereferences, DEREF_HOOK);
2289 add_hook(&match_pointer_as_array, OP_HOOK);
2290 select_call_implies_hook(DEREFERENCE, &set_param_dereferenced);
2291 add_hook(&match_function_call, FUNCTION_CALL_HOOK);
2292 add_hook(&match_assign, ASSIGNMENT_HOOK);
2293 add_hook(&match_assign, GLOBAL_ASSIGNMENT_HOOK);
2294 add_hook(&unop_expr, OP_HOOK);
2295 add_hook(&asm_expr, ASM_HOOK);
2296 add_untracked_param_hook(&match_untracked_array);
2298 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
2299 add_member_info_callback(my_id, struct_member_callback);
2300 add_split_return_callback(&returned_struct_members);
2302 add_hook(&assume_indexes_are_valid, OP_HOOK);