smatch_data/kernel.clears_argument: add more functions
[smatch.git] / smatch_extra.c
blobb29235fcfca33b8f95cd94bde0f88e906945e9fe
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 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 expression *expr, struct smatch_state *state);
64 DECLARE_PTR_LIST(void_fn_list, mod_hook *);
65 static struct void_fn_list *extra_mod_hooks;
66 static struct void_fn_list *extra_nomod_hooks;
68 void add_extra_mod_hook(mod_hook *fn)
70 mod_hook **p = malloc(sizeof(mod_hook *));
71 *p = fn;
72 add_ptr_list(&extra_mod_hooks, p);
75 void add_extra_nomod_hook(mod_hook *fn)
77 mod_hook **p = malloc(sizeof(mod_hook *));
78 *p = fn;
79 add_ptr_list(&extra_nomod_hooks, p);
82 void call_extra_hooks(struct void_fn_list *hooks, const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
84 mod_hook **fn;
86 FOR_EACH_PTR(hooks, fn) {
87 (*fn)(name, sym, expr, state);
88 } END_FOR_EACH_PTR(fn);
91 void call_extra_mod_hooks(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
93 call_extra_hooks(extra_mod_hooks, name, sym, expr, state);
96 void call_extra_nomod_hooks(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
98 call_extra_hooks(extra_nomod_hooks, name, sym, expr, state);
101 static void set_union_info(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
103 struct symbol *type, *tmp, *inner_type, *inner, *new_type;
104 struct expression *deref, *member_expr;
105 struct smatch_state *new;
106 int offset, inner_offset;
107 static bool in_recurse;
108 char *member_name;
110 if (__in_fake_assign)
111 return;
113 if (in_recurse)
114 return;
115 in_recurse = true;
117 if (!expr || expr->type != EXPR_DEREF || !expr->member)
118 goto done;
119 offset = get_member_offset_from_deref(expr);
120 if (offset < 0)
121 goto done;
123 deref = strip_expr(expr->deref);
124 type = get_type(deref);
125 if (type_is_ptr(type))
126 type = get_real_base_type(type);
127 if (!type || type->type != SYM_STRUCT)
128 goto done;
130 FOR_EACH_PTR(type->symbol_list, tmp) {
131 inner_type = get_real_base_type(tmp);
132 if (!inner_type || inner_type->type != SYM_UNION)
133 continue;
135 inner = first_ptr_list((struct ptr_list *)inner_type->symbol_list);
136 if (!inner || !inner->ident)
137 continue;
139 inner_offset = get_member_offset(type, inner->ident->name);
140 if (inner_offset < offset)
141 continue;
142 if (inner_offset > offset)
143 goto done;
145 FOR_EACH_PTR(inner_type->symbol_list, inner) {
146 struct symbol *tmp_type;
148 if (!inner->ident || inner->ident == expr->member)
149 continue;
150 tmp_type = get_real_base_type(inner);
151 if (tmp_type && tmp_type->type == SYM_STRUCT)
152 continue;
153 member_expr = deref;
154 if (tmp->ident)
155 member_expr = member_expression(member_expr, '.', tmp->ident);
156 member_expr = member_expression(member_expr, expr->op, inner->ident);
157 member_name = expr_to_var(member_expr);
158 if (!member_name)
159 continue;
160 new_type = get_real_base_type(inner);
161 new = alloc_estate_rl(cast_rl(new_type, estate_rl(state)));
162 set_extra_mod_helper(member_name, sym, member_expr, new);
163 free_string(member_name);
164 } END_FOR_EACH_PTR(inner);
165 } END_FOR_EACH_PTR(tmp);
167 done:
168 in_recurse = false;
171 static void mark_sub_members_gone(const char *name, struct symbol *sym, struct smatch_state *state)
173 struct sm_state *sm;
175 if (__in_fake_assign || __in_fake_var_assign)
176 return;
178 if (!estate_type(state) || estate_type(state)->type != SYM_PTR)
179 return;
180 if (!is_noderef_ptr_rl(estate_rl(state)))
181 return;
183 FOR_EACH_MY_SM(SMATCH_EXTRA, __get_cur_stree(), sm) {
184 if (sm->sym != sym)
185 continue;
186 if (strcmp(name, sm->name) == 0)
187 continue;
188 if (!is_sub_member(name, sym, sm))
189 continue;
190 set_extra_nomod(sm->name, sm->sym, NULL, alloc_estate_empty());
191 } END_FOR_EACH_SM(sm);
194 static void call_update_mtag_data(struct expression *expr,
195 struct smatch_state *state)
197 struct expression *faked;
199 if (__in_fake_var_assign)
200 return;
202 faked = get_faked_expression();
203 if (!faked)
204 goto update;
205 if (faked->type == EXPR_ASSIGNMENT && is_fresh_alloc(faked->right))
206 goto update;
207 return;
209 update:
210 update_mtag_data(expr, state);
213 static bool in_param_set;
214 void set_extra_mod_helper(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
216 if (!expr)
217 expr = gen_expression_from_name_sym(name, sym);
218 remove_from_equiv(name, sym);
219 set_union_info(name, sym, expr, state);
220 mark_sub_members_gone(name, sym, state);
221 call_extra_mod_hooks(name, sym, expr, state);
222 call_update_mtag_data(expr, state);
223 if ((__in_fake_assign || in_param_set) &&
224 estate_is_unknown(state) && !get_state(SMATCH_EXTRA, name, sym))
225 return;
226 set_state(SMATCH_EXTRA, name, sym, state);
229 static void set_extra_nomod_helper(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
231 call_extra_nomod_hooks(name, sym, expr, state);
232 set_state(SMATCH_EXTRA, name, sym, state);
235 static char *get_pointed_at(const char *name, struct symbol *sym, struct symbol **new_sym)
237 struct expression *assigned;
240 * Imagine we have an assignment: "foo = &addr;" then the other name
241 * of "*foo" is addr.
244 if (name[0] != '*')
245 return NULL;
246 if (strcmp(name + 1, sym->ident->name) != 0)
247 return NULL;
249 assigned = get_assigned_expr_name_sym(sym->ident->name, sym);
250 if (!assigned)
251 return NULL;
252 assigned = strip_parens(assigned);
253 if (assigned->type != EXPR_PREOP || assigned->op != '&')
254 return NULL;
256 return expr_to_var_sym(assigned->unop, new_sym);
259 char *get_other_name_sym_from_chunk(const char *name, const char *chunk, int len, struct symbol *sym, struct symbol **new_sym)
261 struct expression *assigned;
262 char *orig_name = NULL;
263 char buf[256];
264 char *ret;
266 assigned = get_assigned_expr_name_sym(chunk, sym);
267 if (!assigned)
268 return NULL;
269 if (assigned->type == EXPR_CALL)
270 return map_call_to_other_name_sym(name, sym, new_sym);
271 if (assigned->type == EXPR_PREOP && assigned->op == '&') {
273 orig_name = expr_to_var_sym(assigned, new_sym);
274 if (!orig_name || !*new_sym)
275 goto free;
277 snprintf(buf, sizeof(buf), "%s.%s", orig_name + 1, name + len);
278 ret = alloc_string(buf);
279 free_string(orig_name);
280 return ret;
283 orig_name = expr_to_var_sym(assigned, new_sym);
284 if (!orig_name || !*new_sym)
285 goto free;
287 snprintf(buf, sizeof(buf), "%s->%s", orig_name, name + len);
288 ret = alloc_string(buf);
289 free_string(orig_name);
290 return ret;
291 free:
292 free_string(orig_name);
293 return NULL;
296 static char *get_long_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym, bool use_stack)
298 struct expression *tmp;
299 struct sm_state *sm;
300 char buf[256];
301 int len, sym_len;
304 * Just prepend the name with a different name/sym and return that.
305 * For example, if we set "foo->bar = bar;" then the other name
306 * for "bar->baz" is "foo->bar->baz". Or if we have "foo = bar;" then
307 * the other name for "bar" is "foo". A third option is if we have
308 * "foo = bar;" then another name for "*bar" is "*foo".
311 FOR_EACH_MY_SM(check_assigned_expr_id, __get_cur_stree(), sm) {
312 tmp = sm->state->data;
313 if (!tmp || tmp->type != EXPR_SYMBOL)
314 continue;
315 if (tmp->symbol == sym)
316 goto found;
317 } END_FOR_EACH_SM(sm);
319 return NULL;
321 found:
322 len = strlen(name);
323 sym_len = tmp->symbol->ident->len;
324 if (!use_stack && sym_len < len && name[sym_len] != '-')
325 return NULL;
327 if (name[0] == '*' && strcmp(name + 1, tmp->symbol_name->name) == 0)
328 snprintf(buf, sizeof(buf), "*%s", sm->name);
329 else if (sym_len < len && (name[tmp->symbol->ident->len] == '-' ||
330 name[tmp->symbol->ident->len] == '.'))
331 snprintf(buf, sizeof(buf), "%s%s", sm->name, name + tmp->symbol->ident->len);
332 else if (strcmp(name, tmp->symbol_name->name) == 0)
333 snprintf(buf, sizeof(buf), "%s", sm->name);
334 else
335 return NULL;
337 *new_sym = sm->sym;
338 return alloc_string(buf);
341 char *get_other_name_sym_helper(const char *name, struct symbol *sym, struct symbol **new_sym, bool use_stack)
343 char buf[256];
344 char *ret;
345 int len;
347 *new_sym = NULL;
349 if (!sym || !sym->ident)
350 return NULL;
352 ret = get_pointed_at(name, sym, new_sym);
353 if (ret)
354 return ret;
356 ret = map_long_to_short_name_sym(name, sym, new_sym, use_stack);
357 if (ret)
358 return ret;
360 len = snprintf(buf, sizeof(buf), "%s", name);
361 if (len >= sizeof(buf) - 2)
362 return NULL;
364 while (use_stack && len >= 1) {
365 if (buf[len] == '>' && buf[len - 1] == '-') {
366 len--;
367 buf[len] = '\0';
368 ret = get_other_name_sym_from_chunk(name, buf, len + 2, sym, new_sym);
369 if (ret)
370 return ret;
372 len--;
375 ret = get_long_name_sym(name, sym, new_sym, use_stack);
376 if (ret)
377 return ret;
379 return NULL;
382 char *get_other_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym)
384 return get_other_name_sym_helper(name, sym, new_sym, true);
387 char *get_other_name_sym_nostack(const char *name, struct symbol *sym, struct symbol **new_sym)
389 return get_other_name_sym_helper(name, sym, new_sym, false);
392 void set_extra_mod(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
394 char *new_name;
395 struct symbol *new_sym;
397 set_extra_mod_helper(name, sym, expr, state);
398 new_name = get_other_name_sym_nostack(name, sym, &new_sym);
399 if (new_name && new_sym)
400 set_extra_mod_helper(new_name, new_sym, NULL, state);
401 free_string(new_name);
404 static struct expression *chunk_get_array_base(struct expression *expr)
407 * The problem with is_array() is that it only returns true for things
408 * like foo[1] but not for foo[1].bar.
411 expr = strip_expr(expr);
412 while (expr && expr->type == EXPR_DEREF)
413 expr = strip_expr(expr->deref);
414 return get_array_base(expr);
417 static int chunk_has_array(struct expression *expr)
419 return !!chunk_get_array_base(expr);
422 static void clear_array_states(struct expression *array)
424 struct sm_state *sm;
426 sm = get_sm_state_expr(link_id, array);
427 if (sm)
428 match_link_modify(sm, NULL);
431 static void set_extra_array_mod(struct expression *expr, struct smatch_state *state)
433 struct expression *array;
434 struct var_sym_list *vsl;
435 struct var_sym *vs;
436 char *name;
437 struct symbol *sym;
439 array = chunk_get_array_base(expr);
441 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
442 if (!name || !vsl) {
443 clear_array_states(array);
444 goto free;
447 FOR_EACH_PTR(vsl, vs) {
448 store_link(link_id, vs->var, vs->sym, name, sym);
449 } END_FOR_EACH_PTR(vs);
451 call_extra_mod_hooks(name, sym, expr, state);
452 set_state(SMATCH_EXTRA, name, sym, state);
453 free:
454 free_string(name);
457 void set_extra_expr_mod(struct expression *expr, struct smatch_state *state)
459 struct symbol *sym;
460 char *name;
462 if (chunk_has_array(expr)) {
463 set_extra_array_mod(expr, state);
464 return;
467 expr = strip_expr(expr);
468 name = expr_to_var_sym(expr, &sym);
469 if (!name || !sym)
470 goto free;
471 set_extra_mod(name, sym, expr, state);
472 free:
473 free_string(name);
476 void set_extra_nomod(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
478 char *new_name;
479 struct symbol *new_sym;
480 struct relation *rel;
481 struct smatch_state *orig_state;
483 orig_state = get_state(SMATCH_EXTRA, name, sym);
485 /* don't save unknown states if leaving it blank is the same */
486 if (!orig_state && estate_is_unknown(state))
487 return;
489 new_name = get_other_name_sym(name, sym, &new_sym);
490 if (new_name && new_sym)
491 set_extra_nomod_helper(new_name, new_sym, expr, state);
492 free_string(new_name);
494 if (!estate_related(orig_state)) {
495 set_extra_nomod_helper(name, sym, expr, state);
496 return;
499 set_related(state, estate_related(orig_state));
500 FOR_EACH_PTR(estate_related(orig_state), rel) {
501 struct smatch_state *estate;
503 estate = get_state(SMATCH_EXTRA, rel->name, rel->sym);
504 if (!estate)
505 continue;
506 set_extra_nomod_helper(rel->name, rel->sym, expr, clone_estate_cast(estate_type(estate), state));
507 } END_FOR_EACH_PTR(rel);
510 void set_extra_nomod_vsl(const char *name, struct symbol *sym, struct var_sym_list *vsl, struct expression *expr, struct smatch_state *state)
512 struct var_sym *vs;
514 FOR_EACH_PTR(vsl, vs) {
515 store_link(link_id, vs->var, vs->sym, name, sym);
516 } END_FOR_EACH_PTR(vs);
518 set_extra_nomod(name, sym, expr, state);
522 * This is for return_implies_state() hooks which modify a SMATCH_EXTRA state
524 void set_extra_expr_nomod(struct expression *expr, struct smatch_state *state)
526 struct var_sym_list *vsl;
527 struct var_sym *vs;
528 char *name;
529 struct symbol *sym;
531 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
532 if (!name || !vsl)
533 goto free;
534 FOR_EACH_PTR(vsl, vs) {
535 store_link(link_id, vs->var, vs->sym, name, sym);
536 } END_FOR_EACH_PTR(vs);
538 set_extra_nomod(name, sym, expr, state);
539 free:
540 free_string(name);
543 static void set_extra_true_false(const char *name, struct symbol *sym,
544 struct smatch_state *true_state,
545 struct smatch_state *false_state)
547 char *new_name;
548 struct symbol *new_sym;
549 struct relation *rel;
550 struct smatch_state *orig_state;
552 if (!true_state && !false_state)
553 return;
555 if (in_warn_on_macro())
556 return;
558 new_name = get_other_name_sym(name, sym, &new_sym);
559 if (new_name && new_sym)
560 set_true_false_states(SMATCH_EXTRA, new_name, new_sym, true_state, false_state);
561 free_string(new_name);
563 orig_state = get_state(SMATCH_EXTRA, name, sym);
565 if (!estate_related(orig_state)) {
566 set_true_false_states(SMATCH_EXTRA, name, sym, true_state, false_state);
567 return;
570 if (true_state)
571 set_related(true_state, estate_related(orig_state));
572 if (false_state)
573 set_related(false_state, estate_related(orig_state));
575 FOR_EACH_PTR(estate_related(orig_state), rel) {
576 set_true_false_states(SMATCH_EXTRA, rel->name, rel->sym,
577 true_state, false_state);
578 } END_FOR_EACH_PTR(rel);
581 static void set_extra_chunk_true_false(struct expression *expr,
582 struct smatch_state *true_state,
583 struct smatch_state *false_state)
585 struct var_sym_list *vsl;
586 struct var_sym *vs;
587 struct symbol *type;
588 char *name;
589 struct symbol *sym;
591 if (in_warn_on_macro())
592 return;
594 type = get_type(expr);
595 if (!type)
596 return;
598 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
599 if (!name || !vsl)
600 goto free;
601 FOR_EACH_PTR(vsl, vs) {
602 store_link(link_id, vs->var, vs->sym, name, sym);
603 } END_FOR_EACH_PTR(vs);
605 set_true_false_states(SMATCH_EXTRA, name, sym,
606 clone_estate(true_state),
607 clone_estate(false_state));
608 free:
609 free_string(name);
612 static void set_extra_expr_true_false(struct expression *expr,
613 struct smatch_state *true_state,
614 struct smatch_state *false_state)
616 char *name;
617 struct symbol *sym;
618 sval_t sval;
620 if (!true_state && !false_state)
621 return;
623 if (get_value(expr, &sval))
624 return;
626 expr = strip_expr(expr);
627 name = expr_to_var_sym(expr, &sym);
628 if (!name || !sym) {
629 free_string(name);
630 set_extra_chunk_true_false(expr, true_state, false_state);
631 return;
633 set_extra_true_false(name, sym, true_state, false_state);
634 free_string(name);
637 static int get_countdown_info(struct expression *condition, struct expression **unop, int *op, sval_t *right)
639 struct expression *unop_expr;
640 int comparison;
641 sval_t limit;
643 right->type = &int_ctype;
644 right->value = 0;
646 condition = strip_expr(condition);
648 if (condition->type == EXPR_COMPARE) {
649 comparison = remove_unsigned_from_comparison(condition->op);
651 if (comparison != SPECIAL_GTE && comparison != '>')
652 return 0;
653 if (!get_value(condition->right, &limit))
654 return 0;
656 unop_expr = condition->left;
657 if (unop_expr->type != EXPR_PREOP && unop_expr->type != EXPR_POSTOP)
658 return 0;
659 if (unop_expr->op != SPECIAL_DECREMENT)
660 return 0;
662 *unop = unop_expr;
663 *op = comparison;
664 *right = limit;
666 return 1;
669 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
670 return 0;
671 if (condition->op != SPECIAL_DECREMENT)
672 return 0;
674 *unop = condition;
675 *op = '>';
677 return 1;
680 static struct sm_state *handle_canonical_while_count_down(struct statement *loop)
682 struct expression *iter_var;
683 struct expression *condition, *unop;
684 struct symbol *type;
685 struct sm_state *sm;
686 struct smatch_state *estate;
687 int op;
688 sval_t start, right;
690 right.type = &int_ctype;
691 right.value = 0;
693 condition = strip_expr(loop->iterator_pre_condition);
694 if (!condition)
695 return NULL;
697 if (!get_countdown_info(condition, &unop, &op, &right))
698 return NULL;
700 iter_var = unop->unop;
702 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
703 if (!sm)
704 return NULL;
705 if (sval_cmp(estate_min(sm->state), right) < 0)
706 return NULL;
707 start = estate_max(sm->state);
709 type = get_type(iter_var);
710 right = sval_cast(type, right);
711 start = sval_cast(type, start);
713 if (sval_cmp(start, right) <= 0)
714 return NULL;
715 if (!sval_is_max(start))
716 start.value--;
718 if (op == SPECIAL_GTE)
719 right.value--;
721 if (unop->type == EXPR_PREOP) {
722 right.value++;
723 estate = alloc_estate_range(right, start);
724 if (estate_has_hard_max(sm->state))
725 estate_set_hard_max(estate);
726 estate_copy_fuzzy_max(estate, sm->state);
727 set_extra_expr_mod(iter_var, estate);
729 if (unop->type == EXPR_POSTOP) {
730 estate = alloc_estate_range(right, start);
731 if (estate_has_hard_max(sm->state))
732 estate_set_hard_max(estate);
733 estate_copy_fuzzy_max(estate, sm->state);
734 set_extra_expr_mod(iter_var, estate);
736 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
739 static struct sm_state *handle_canonical_for_inc(struct expression *iter_expr,
740 struct expression *condition)
742 struct expression *iter_var;
743 struct sm_state *sm;
744 struct smatch_state *estate;
745 sval_t start, end, max;
746 bool unknown_end = false;
748 iter_var = iter_expr->unop;
749 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
750 if (!sm)
751 return NULL;
752 if (!estate_get_single_value(sm->state, &start))
753 return NULL;
754 if (!get_implied_max(condition->right, &end)) {
755 end = sval_type_max(get_type(condition->right));
756 end = sval_cast(start.type, end);
757 if (sval_is_max(end))
758 unknown_end = true;
761 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
762 return NULL;
764 switch (condition->op) {
765 case SPECIAL_UNSIGNED_LT:
766 case SPECIAL_NOTEQUAL:
767 case '<':
768 if (!sval_is_min(end) && !unknown_end)
769 end.value--;
770 break;
771 case SPECIAL_UNSIGNED_LTE:
772 case SPECIAL_LTE:
773 break;
774 default:
775 return NULL;
777 if (sval_cmp(end, start) < 0)
778 return NULL;
779 end = sval_cast(start.type, end);
780 estate = alloc_estate_range(start, end);
781 if (get_hard_max(condition->right, &max)) {
782 if (!get_macro_name(condition->pos))
783 estate_set_hard_max(estate);
784 if (condition->op == '<' ||
785 condition->op == SPECIAL_UNSIGNED_LT ||
786 condition->op == SPECIAL_NOTEQUAL)
787 max.value--;
788 max = sval_cast(start.type, max);
789 estate_set_fuzzy_max(estate, max);
791 set_extra_expr_mod(iter_var, estate);
792 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
795 static struct sm_state *handle_canonical_for_dec(struct expression *iter_expr,
796 struct expression *condition)
798 struct expression *iter_var;
799 struct sm_state *sm;
800 struct smatch_state *estate;
801 sval_t start, end;
803 iter_var = iter_expr->unop;
804 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
805 if (!sm)
806 return NULL;
807 if (!estate_get_single_value(sm->state, &start))
808 return NULL;
809 if (!get_implied_min(condition->right, &end))
810 end = sval_type_min(get_type(iter_var));
811 end = sval_cast(estate_type(sm->state), end);
812 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
813 return NULL;
815 switch (condition->op) {
816 case SPECIAL_NOTEQUAL:
817 case '>':
818 if (!sval_is_max(end))
819 end.value++;
820 break;
821 case SPECIAL_GTE:
822 break;
823 default:
824 return NULL;
826 if (sval_cmp(end, start) > 0)
827 return NULL;
828 estate = alloc_estate_range(end, start);
829 estate_set_hard_max(estate);
830 estate_set_fuzzy_max(estate, estate_get_fuzzy_max(estate));
831 set_extra_expr_mod(iter_var, estate);
832 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
835 static struct sm_state *handle_canonical_for_loops(struct statement *loop)
837 struct expression *iter_expr;
838 struct expression *condition;
840 if (!loop->iterator_post_statement)
841 return NULL;
842 if (loop->iterator_post_statement->type != STMT_EXPRESSION)
843 return NULL;
844 iter_expr = loop->iterator_post_statement->expression;
845 if (!loop->iterator_pre_condition)
846 return NULL;
847 if (loop->iterator_pre_condition->type != EXPR_COMPARE)
848 return NULL;
849 condition = loop->iterator_pre_condition;
851 if (iter_expr->op == SPECIAL_INCREMENT)
852 return handle_canonical_for_inc(iter_expr, condition);
853 if (iter_expr->op == SPECIAL_DECREMENT)
854 return handle_canonical_for_dec(iter_expr, condition);
855 return NULL;
858 struct sm_state *__extra_handle_canonical_loops(struct statement *loop, struct stree **stree)
860 struct sm_state *ret;
863 * Canonical loops are a hack. The proper way to handle this is to
864 * use two passes, but unfortunately, doing two passes makes parsing
865 * code twice as slow.
867 * What we do is we set the inside state here, which overwrites whatever
868 * __extra_match_condition() does. Then we set the outside state in
869 * __extra_pre_loop_hook_after().
872 __push_fake_cur_stree();
873 if (!loop->iterator_post_statement)
874 ret = handle_canonical_while_count_down(loop);
875 else
876 ret = handle_canonical_for_loops(loop);
877 *stree = __pop_fake_cur_stree();
878 return ret;
881 int __iterator_unchanged(struct sm_state *sm)
883 if (!sm)
884 return 0;
885 if (get_sm_state(my_id, sm->name, sm->sym) == sm)
886 return 1;
887 return 0;
890 static void while_count_down_after(struct sm_state *sm, struct expression *condition)
892 struct expression *unop;
893 int op;
894 sval_t limit, after_value;
896 if (!get_countdown_info(condition, &unop, &op, &limit))
897 return;
898 after_value = estate_min(sm->state);
899 after_value.value--;
900 set_extra_mod(sm->name, sm->sym, condition->unop, alloc_estate_sval(after_value));
903 void __extra_pre_loop_hook_after(struct sm_state *sm,
904 struct statement *iterator,
905 struct expression *condition)
907 struct expression *iter_expr;
908 sval_t limit;
909 struct smatch_state *state;
910 sval_t end;
912 if (!iterator) {
913 while_count_down_after(sm, condition);
914 return;
917 iter_expr = iterator->expression;
919 if (condition->type != EXPR_COMPARE)
920 return;
922 if (iter_expr->op == SPECIAL_INCREMENT) {
923 if (!get_implied_value(condition->right, &end) &&
924 sval_is_max(estate_max(sm->state)))
925 limit = estate_max(sm->state);
926 else
927 limit = sval_binop(estate_max(sm->state), '+',
928 sval_type_val(estate_type(sm->state), 1));
929 } else {
930 limit = sval_binop(estate_min(sm->state), '-',
931 sval_type_val(estate_type(sm->state), 1));
933 limit = sval_cast(estate_type(sm->state), limit);
934 if (!estate_has_hard_max(sm->state) && !__has_breaks()) {
935 if (iter_expr->op == SPECIAL_INCREMENT)
936 state = alloc_estate_range(estate_min(sm->state), limit);
937 else
938 state = alloc_estate_range(limit, estate_max(sm->state));
939 } else {
940 state = alloc_estate_sval(limit);
942 if (!estate_has_hard_max(sm->state)) {
943 estate_clear_hard_max(state);
945 if (estate_has_fuzzy_max(sm->state)) {
946 sval_t hmax = estate_get_fuzzy_max(sm->state);
947 sval_t max = estate_max(sm->state);
949 if (sval_cmp(hmax, max) != 0)
950 estate_clear_fuzzy_max(state);
951 } else if (!estate_has_fuzzy_max(sm->state)) {
952 estate_clear_fuzzy_max(state);
955 set_extra_mod(sm->name, sm->sym, iter_expr, state);
958 static bool get_global_rl(const char *name, struct symbol *sym, struct range_list **rl)
960 struct expression *expr;
962 if (!sym || !(sym->ctype.modifiers & MOD_TOPLEVEL) || !sym->ident)
963 return false;
964 if (strcmp(sym->ident->name, name) != 0)
965 return false;
967 expr = symbol_expression(sym);
968 return get_implied_rl(expr, rl);
971 static struct stree *unmatched_stree;
972 static struct smatch_state *unmatched_state(struct sm_state *sm)
974 struct smatch_state *state;
975 struct range_list *rl;
977 if (unmatched_stree) {
978 state = get_state_stree(unmatched_stree, SMATCH_EXTRA, sm->name, sm->sym);
979 if (state)
980 return state;
982 if (parent_is_gone_var_sym(sm->name, sm->sym))
983 return alloc_estate_empty();
984 if (get_global_rl(sm->name, sm->sym, &rl))
985 return alloc_estate_rl(rl);
986 return alloc_estate_whole(estate_type(sm->state));
989 static void clear_the_pointed_at(struct expression *expr)
991 struct stree *stree;
992 char *name;
993 struct symbol *sym;
994 struct sm_state *tmp;
996 name = expr_to_var_sym(expr, &sym);
997 if (!name || !sym)
998 goto free;
1000 stree = __get_cur_stree();
1001 FOR_EACH_MY_SM(SMATCH_EXTRA, stree, tmp) {
1002 if (tmp->name[0] != '*')
1003 continue;
1004 if (tmp->sym != sym)
1005 continue;
1006 if (strcmp(tmp->name + 1, name) != 0)
1007 continue;
1008 set_extra_mod(tmp->name, tmp->sym, expr, alloc_estate_whole(estate_type(tmp->state)));
1009 } END_FOR_EACH_SM(tmp);
1011 free:
1012 free_string(name);
1015 static int is_const_param(struct expression *expr, int param)
1017 struct symbol *type;
1019 type = get_arg_type(expr, param);
1020 if (!type)
1021 return 0;
1022 if (type->ctype.modifiers & MOD_CONST)
1023 return 1;
1024 return 0;
1027 static void match_function_call(struct expression *expr)
1029 struct expression *arg;
1030 struct expression *tmp;
1031 int param = -1;
1033 /* if we have the db this is handled in smatch_function_hooks.c */
1034 if (!option_no_db)
1035 return;
1036 if (inlinable(expr->fn))
1037 return;
1039 FOR_EACH_PTR(expr->args, arg) {
1040 param++;
1041 if (is_const_param(expr->fn, param))
1042 continue;
1043 tmp = strip_expr(arg);
1044 if (tmp->type == EXPR_PREOP && tmp->op == '&')
1045 set_extra_expr_mod(tmp->unop, alloc_estate_whole(get_type(tmp->unop)));
1046 else
1047 clear_the_pointed_at(tmp);
1048 } END_FOR_EACH_PTR(arg);
1051 int values_fit_type(struct expression *left, struct expression *right)
1053 struct range_list *rl;
1054 struct symbol *type;
1056 type = get_type(left);
1057 if (!type)
1058 return 0;
1059 get_absolute_rl(right, &rl);
1060 if (type == rl_type(rl))
1061 return 1;
1062 if (type_unsigned(type) && sval_is_negative(rl_min(rl)))
1063 return 0;
1064 if (sval_cmp(sval_type_min(type), rl_min(rl)) > 0)
1065 return 0;
1066 if (sval_cmp(sval_type_max(type), rl_max(rl)) < 0)
1067 return 0;
1068 return 1;
1071 static void save_chunk_info(struct expression *left, struct expression *right)
1073 struct var_sym_list *vsl;
1074 struct var_sym *vs;
1075 struct expression *add_expr;
1076 struct symbol *type;
1077 sval_t sval;
1078 char *name;
1079 struct symbol *sym;
1081 if (right->type != EXPR_BINOP || right->op != '-')
1082 return;
1083 if (!get_value(right->left, &sval))
1084 return;
1085 if (!expr_to_sym(right->right))
1086 return;
1088 add_expr = binop_expression(left, '+', right->right);
1089 type = get_type(add_expr);
1090 if (!type)
1091 return;
1092 name = expr_to_chunk_sym_vsl(add_expr, &sym, &vsl);
1093 if (!name || !vsl)
1094 goto free;
1095 FOR_EACH_PTR(vsl, vs) {
1096 store_link(link_id, vs->var, vs->sym, name, sym);
1097 } END_FOR_EACH_PTR(vs);
1099 set_state(SMATCH_EXTRA, name, sym, alloc_estate_sval(sval_cast(type, sval)));
1100 free:
1101 free_string(name);
1104 static void do_array_assign(struct expression *left, int op, struct expression *right)
1106 struct range_list *rl;
1108 if (op == '=') {
1109 get_absolute_rl(right, &rl);
1110 rl = cast_rl(get_type(left), rl);
1111 } else {
1112 rl = alloc_whole_rl(get_type(left));
1115 set_extra_array_mod(left, alloc_estate_rl(rl));
1118 static void match_vanilla_assign(struct expression *left, struct expression *right)
1120 struct range_list *orig_rl = NULL;
1121 struct range_list *rl = NULL;
1122 struct symbol *right_sym;
1123 struct symbol *left_type;
1124 struct symbol *right_type;
1125 char *right_name = NULL;
1126 struct symbol *sym;
1127 char *name;
1128 sval_t sval, max;
1129 struct smatch_state *state;
1130 int comparison;
1132 if (is_struct(left))
1133 return;
1135 save_chunk_info(left, right);
1137 name = expr_to_var_sym(left, &sym);
1138 if (!name) {
1139 if (chunk_has_array(left))
1140 do_array_assign(left, '=', right);
1141 return;
1144 left_type = get_type(left);
1145 right_type = get_type(right);
1147 right_name = expr_to_var_sym(right, &right_sym);
1149 if (!__in_fake_assign &&
1150 !(right->type == EXPR_PREOP && right->op == '&') &&
1151 right_name && right_sym &&
1152 values_fit_type(left, strip_expr(right)) &&
1153 !has_symbol(right, sym)) {
1154 set_equiv(left, right);
1155 goto free;
1158 if (get_implied_value(right, &sval)) {
1159 state = alloc_estate_sval(sval_cast(left_type, sval));
1160 goto done;
1163 if (__in_fake_assign || is_fake_var(left)) {
1164 struct smatch_state *right_state;
1165 struct range_list *rl;
1167 right_state = get_state(SMATCH_EXTRA, right_name, right_sym);
1168 if (right_state) {
1169 /* simple assignment */
1170 state = clone_estate(right_state);
1171 goto done;
1174 if (get_implied_rl(right, &rl)) {
1175 rl = cast_rl(left_type, rl);
1176 state = alloc_estate_rl(rl);
1177 goto done;
1180 rl = alloc_whole_rl(right_type);
1181 rl = cast_rl(left_type, rl);
1182 state = alloc_estate_rl(rl);
1183 goto done;
1186 comparison = get_comparison_no_extra(left, right);
1187 if (comparison) {
1188 comparison = flip_comparison(comparison);
1189 get_implied_rl(left, &orig_rl);
1192 if (get_implied_rl(right, &rl)) {
1193 rl = cast_rl(left_type, rl);
1194 if (orig_rl)
1195 filter_by_comparison(&rl, comparison, orig_rl);
1196 state = alloc_estate_rl(rl);
1197 if (get_hard_max(right, &max)) {
1198 estate_set_hard_max(state);
1199 estate_set_fuzzy_max(state, max);
1201 } else {
1202 rl = alloc_whole_rl(right_type);
1203 rl = cast_rl(left_type, rl);
1204 if (orig_rl)
1205 filter_by_comparison(&rl, comparison, orig_rl);
1206 state = alloc_estate_rl(rl);
1209 done:
1210 set_extra_mod(name, sym, left, state);
1211 free:
1212 free_string(right_name);
1215 static void match_assign(struct expression *expr)
1217 struct range_list *rl = NULL;
1218 struct expression *left;
1219 struct expression *right;
1220 struct expression *binop_expr;
1221 struct symbol *left_type;
1222 struct symbol *sym;
1223 char *name;
1225 left = strip_expr(expr->left);
1227 right = strip_parens(expr->right);
1228 if (right->type == EXPR_CALL && sym_name_is("__builtin_expect", right->fn))
1229 right = get_argument_from_call_expr(right->args, 0);
1230 while (right->type == EXPR_ASSIGNMENT && right->op == '=')
1231 right = strip_parens(right->left);
1233 if (expr->op == '=' && is_condition(expr->right))
1234 return; /* handled in smatch_condition.c */
1235 if (expr->op == '=' && right->type == EXPR_CALL &&
1236 !is_fake_call(right))
1237 return; /* handled in smatch_function_hooks.c */
1238 if (expr->op == '=') {
1239 match_vanilla_assign(left, right);
1240 return;
1243 name = expr_to_var_sym(left, &sym);
1244 if (!name)
1245 return;
1247 left_type = get_type(left);
1249 switch (expr->op) {
1250 case SPECIAL_ADD_ASSIGN:
1251 case SPECIAL_SUB_ASSIGN:
1252 case SPECIAL_AND_ASSIGN:
1253 case SPECIAL_MOD_ASSIGN:
1254 case SPECIAL_SHL_ASSIGN:
1255 case SPECIAL_SHR_ASSIGN:
1256 case SPECIAL_OR_ASSIGN:
1257 case SPECIAL_XOR_ASSIGN:
1258 case SPECIAL_MUL_ASSIGN:
1259 case SPECIAL_DIV_ASSIGN:
1260 binop_expr = binop_expression(expr->left,
1261 op_remove_assign(expr->op),
1262 expr->right);
1263 get_absolute_rl(binop_expr, &rl);
1264 rl = cast_rl(left_type, rl);
1265 if (inside_loop()) {
1266 if (expr->op == SPECIAL_ADD_ASSIGN)
1267 add_range(&rl, rl_max(rl), sval_type_max(rl_type(rl)));
1269 if (expr->op == SPECIAL_SUB_ASSIGN &&
1270 !sval_is_negative(rl_min(rl))) {
1271 sval_t zero = { .type = rl_type(rl) };
1273 add_range(&rl, rl_min(rl), zero);
1276 set_extra_mod(name, sym, left, alloc_estate_rl(rl));
1277 goto free;
1279 set_extra_mod(name, sym, left, alloc_estate_whole(left_type));
1280 free:
1281 free_string(name);
1284 static struct smatch_state *increment_state(struct smatch_state *state)
1286 sval_t min = estate_min(state);
1287 sval_t max = estate_max(state);
1289 if (!estate_rl(state))
1290 return NULL;
1292 if (inside_loop())
1293 max = sval_type_max(max.type);
1295 if (!sval_is_min(min) && !sval_is_max(min))
1296 min.value++;
1297 if (!sval_is_min(max) && !sval_is_max(max))
1298 max.value++;
1299 return alloc_estate_range(min, max);
1302 static struct smatch_state *decrement_state(struct smatch_state *state)
1304 sval_t min = estate_min(state);
1305 sval_t max = estate_max(state);
1307 if (!estate_rl(state))
1308 return NULL;
1310 if (inside_loop())
1311 min = sval_type_min(min.type);
1313 if (!sval_is_min(min) && !sval_is_max(min))
1314 min.value--;
1315 if (!sval_is_min(max) && !sval_is_max(max))
1316 max.value--;
1317 return alloc_estate_range(min, max);
1320 static void clear_pointed_at_state(struct expression *expr)
1322 struct symbol *type;
1325 * ALERT: This is sort of a mess. If it's is a struct assigment like
1326 * "foo = bar;", then that's handled by smatch_struct_assignment.c.
1327 * the same thing for p++ where "p" is a struct. Most modifications
1328 * are handled by the assignment hook or the db. Smatch_extra.c doesn't
1329 * use smatch_modification.c because we have to get the ordering right
1330 * or something. So if you have p++ where p is a pointer to a standard
1331 * c type then we handle that here. What a mess.
1333 expr = strip_expr(expr);
1334 type = get_type(expr);
1335 if (!type || type->type != SYM_PTR)
1336 return;
1337 type = get_real_base_type(type);
1338 if (!type || type->type != SYM_BASETYPE)
1339 return;
1340 set_extra_expr_nomod(deref_expression(expr), alloc_estate_whole(type));
1343 static void unop_expr(struct expression *expr)
1345 struct smatch_state *state;
1347 if (expr->smatch_flags & Handled)
1348 return;
1350 switch (expr->op) {
1351 case SPECIAL_INCREMENT:
1352 state = get_state_expr(SMATCH_EXTRA, expr->unop);
1353 state = increment_state(state);
1354 if (!state)
1355 state = alloc_estate_whole(get_type(expr));
1356 set_extra_expr_mod(expr->unop, state);
1357 clear_pointed_at_state(expr->unop);
1358 break;
1359 case SPECIAL_DECREMENT:
1360 state = get_state_expr(SMATCH_EXTRA, expr->unop);
1361 state = decrement_state(state);
1362 if (!state)
1363 state = alloc_estate_whole(get_type(expr));
1364 set_extra_expr_mod(expr->unop, state);
1365 clear_pointed_at_state(expr->unop);
1366 break;
1367 default:
1368 return;
1372 static void asm_expr(struct statement *stmt)
1374 struct asm_operand *op;
1375 struct symbol *type;
1377 FOR_EACH_PTR(stmt->asm_outputs, op) {
1378 type = get_type(strip_expr(op->expr));
1379 set_extra_expr_mod(op->expr, alloc_estate_whole(type));
1380 } END_FOR_EACH_PTR(op);
1383 static void check_dereference(struct expression *expr)
1385 struct smatch_state *state;
1387 if (__in_fake_assign)
1388 return;
1389 if (outside_of_function())
1390 return;
1391 state = get_extra_state(expr);
1392 if (state) {
1393 struct range_list *rl;
1395 rl = rl_intersection(estate_rl(state), valid_ptr_rl);
1396 if (rl_equiv(rl, estate_rl(state)))
1397 return;
1398 set_extra_expr_nomod(expr, alloc_estate_rl(rl));
1399 } else {
1400 struct range_list *rl;
1402 if (get_mtag_rl(expr, &rl))
1403 rl = rl_intersection(rl, valid_ptr_rl);
1404 else
1405 rl = clone_rl(valid_ptr_rl);
1407 set_extra_expr_nomod(expr, alloc_estate_rl(rl));
1411 static void match_dereferences(struct expression *expr)
1413 if (expr->type != EXPR_PREOP)
1414 return;
1415 if (getting_address(expr))
1416 return;
1417 /* it's saying that foo[1] = bar dereferences foo[1] */
1418 if (is_array(expr))
1419 return;
1420 check_dereference(expr->unop);
1423 static void match_pointer_as_array(struct expression *expr)
1425 if (!is_array(expr))
1426 return;
1427 check_dereference(get_array_base(expr));
1430 static void find_dereferences(struct expression *expr)
1432 while (expr->type == EXPR_PREOP) {
1433 if (expr->op == '*')
1434 check_dereference(expr->unop);
1435 expr = strip_expr(expr->unop);
1439 static void set_param_dereferenced(struct expression *call, struct expression *arg, char *key, char *unused)
1441 struct symbol *sym;
1442 char *name;
1444 name = get_variable_from_key(arg, key, &sym);
1445 if (name && sym) {
1446 struct smatch_state *orig, *new;
1447 struct range_list *rl;
1449 orig = get_state(SMATCH_EXTRA, name, sym);
1450 if (orig) {
1451 rl = rl_intersection(estate_rl(orig),
1452 alloc_rl(valid_ptr_min_sval,
1453 valid_ptr_max_sval));
1454 new = alloc_estate_rl(rl);
1455 } else {
1456 new = alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval);
1459 set_extra_nomod(name, sym, NULL, new);
1461 free_string(name);
1463 find_dereferences(arg);
1466 static sval_t add_one(sval_t sval)
1468 sval.value++;
1469 return sval;
1472 static int handle_postop_inc(struct expression *left, int op, struct expression *right)
1474 struct statement *stmt;
1475 struct expression *cond;
1476 struct smatch_state *true_state, *false_state;
1477 struct symbol *type;
1478 sval_t start;
1479 sval_t limit;
1482 * If we're decrementing here then that's a canonical while count down
1483 * so it's handled already. We're only handling loops like:
1484 * i = 0;
1485 * do { ... } while (i++ < 3);
1488 if (left->type != EXPR_POSTOP || left->op != SPECIAL_INCREMENT)
1489 return 0;
1491 stmt = __cur_stmt->parent;
1492 if (!stmt)
1493 return 0;
1494 if (stmt->type == STMT_COMPOUND)
1495 stmt = stmt->parent;
1496 if (!stmt || stmt->type != STMT_ITERATOR || !stmt->iterator_post_condition)
1497 return 0;
1499 cond = strip_expr(stmt->iterator_post_condition);
1500 if (cond->type != EXPR_COMPARE || cond->op != op)
1501 return 0;
1502 if (left != strip_expr(cond->left) || right != strip_expr(cond->right))
1503 return 0;
1505 if (!get_implied_value(left->unop, &start))
1506 return 0;
1507 if (!get_implied_value(right, &limit))
1508 return 0;
1509 type = get_type(left->unop);
1510 limit = sval_cast(type, limit);
1511 if (sval_cmp(start, limit) > 0)
1512 return 0;
1514 switch (op) {
1515 case '<':
1516 case SPECIAL_UNSIGNED_LT:
1517 break;
1518 case SPECIAL_LTE:
1519 case SPECIAL_UNSIGNED_LTE:
1520 limit = add_one(limit);
1521 default:
1522 return 0;
1526 true_state = alloc_estate_range(add_one(start), limit);
1527 false_state = alloc_estate_range(add_one(limit), add_one(limit));
1529 /* Currently we just discard the false state but when two passes is
1530 * implimented correctly then it will use it.
1533 set_extra_expr_true_false(left->unop, true_state, false_state);
1535 return 1;
1538 bool is_impossible_variable(struct expression *expr)
1540 struct smatch_state *state;
1542 state = get_extra_state(expr);
1543 if (state && !estate_rl(state))
1544 return true;
1545 return false;
1548 static bool in_macro(struct expression *left, struct expression *right)
1550 if (!left || !right)
1551 return 0;
1552 if (left->pos.line != right->pos.line || left->pos.pos != right->pos.pos)
1553 return 0;
1554 if (get_macro_name(left->pos))
1555 return 1;
1556 return 0;
1559 static void handle_comparison(struct symbol *type, struct expression *left, int op, struct expression *right)
1561 struct range_list *left_orig;
1562 struct range_list *left_true;
1563 struct range_list *left_false;
1564 struct range_list *right_orig;
1565 struct range_list *right_true;
1566 struct range_list *right_false;
1567 struct smatch_state *left_true_state;
1568 struct smatch_state *left_false_state;
1569 struct smatch_state *right_true_state;
1570 struct smatch_state *right_false_state;
1571 sval_t dummy, hard_max;
1572 int left_postop = 0;
1573 int right_postop = 0;
1575 if (left->op == SPECIAL_INCREMENT || left->op == SPECIAL_DECREMENT) {
1576 if (left->type == EXPR_POSTOP) {
1577 left->smatch_flags |= Handled;
1578 left_postop = left->op;
1579 if (handle_postop_inc(left, op, right))
1580 return;
1582 left = strip_parens(left->unop);
1584 while (left->type == EXPR_ASSIGNMENT)
1585 left = strip_parens(left->left);
1587 if (right->op == SPECIAL_INCREMENT || right->op == SPECIAL_DECREMENT) {
1588 if (right->type == EXPR_POSTOP) {
1589 right->smatch_flags |= Handled;
1590 right_postop = right->op;
1592 right = strip_parens(right->unop);
1595 if (is_impossible_variable(left) || is_impossible_variable(right))
1596 return;
1598 get_real_absolute_rl(left, &left_orig);
1599 left_orig = cast_rl(type, left_orig);
1601 get_real_absolute_rl(right, &right_orig);
1602 right_orig = cast_rl(type, right_orig);
1604 split_comparison_rl(left_orig, op, right_orig, &left_true, &left_false, &right_true, &right_false);
1606 left_true = rl_truncate_cast(get_type(strip_expr(left)), left_true);
1607 left_false = rl_truncate_cast(get_type(strip_expr(left)), left_false);
1608 right_true = rl_truncate_cast(get_type(strip_expr(right)), right_true);
1609 right_false = rl_truncate_cast(get_type(strip_expr(right)), right_false);
1611 if (!left_true || !left_false) {
1612 struct range_list *tmp_true, *tmp_false;
1614 split_comparison_rl(alloc_whole_rl(type), op, right_orig, &tmp_true, &tmp_false, NULL, NULL);
1615 tmp_true = rl_truncate_cast(get_type(strip_expr(left)), tmp_true);
1616 tmp_false = rl_truncate_cast(get_type(strip_expr(left)), tmp_false);
1617 if (tmp_true && tmp_false)
1618 __save_imaginary_state(left, tmp_true, tmp_false);
1621 if (!right_true || !right_false) {
1622 struct range_list *tmp_true, *tmp_false;
1624 split_comparison_rl(alloc_whole_rl(type), op, right_orig, NULL, NULL, &tmp_true, &tmp_false);
1625 tmp_true = rl_truncate_cast(get_type(strip_expr(right)), tmp_true);
1626 tmp_false = rl_truncate_cast(get_type(strip_expr(right)), tmp_false);
1627 if (tmp_true && tmp_false)
1628 __save_imaginary_state(right, tmp_true, tmp_false);
1631 left_true_state = alloc_estate_rl(left_true);
1632 left_false_state = alloc_estate_rl(left_false);
1633 right_true_state = alloc_estate_rl(right_true);
1634 right_false_state = alloc_estate_rl(right_false);
1636 switch (op) {
1637 case '<':
1638 case SPECIAL_UNSIGNED_LT:
1639 case SPECIAL_UNSIGNED_LTE:
1640 case SPECIAL_LTE:
1641 if (get_hard_max(right, &dummy) && !in_macro(left, right))
1642 estate_set_hard_max(left_true_state);
1643 if (get_hard_max(left, &dummy) && !in_macro(left, right))
1644 estate_set_hard_max(right_false_state);
1645 break;
1646 case '>':
1647 case SPECIAL_UNSIGNED_GT:
1648 case SPECIAL_UNSIGNED_GTE:
1649 case SPECIAL_GTE:
1650 if (get_hard_max(left, &dummy) && !in_macro(left, right))
1651 estate_set_hard_max(right_true_state);
1652 if (get_hard_max(right, &dummy) && !in_macro(left, right))
1653 estate_set_hard_max(left_false_state);
1654 break;
1657 switch (op) {
1658 case '<':
1659 case SPECIAL_UNSIGNED_LT:
1660 case SPECIAL_UNSIGNED_LTE:
1661 case SPECIAL_LTE:
1662 if (get_hard_max(right, &hard_max)) {
1663 if (op == '<' || op == SPECIAL_UNSIGNED_LT)
1664 hard_max.value--;
1665 estate_set_fuzzy_max(left_true_state, hard_max);
1667 if (get_implied_value(right, &hard_max)) {
1668 if (op == SPECIAL_UNSIGNED_LTE ||
1669 op == SPECIAL_LTE)
1670 hard_max.value++;
1671 estate_set_fuzzy_max(left_false_state, hard_max);
1673 if (get_hard_max(left, &hard_max)) {
1674 if (op == SPECIAL_UNSIGNED_LTE ||
1675 op == SPECIAL_LTE)
1676 hard_max.value--;
1677 estate_set_fuzzy_max(right_false_state, hard_max);
1679 if (get_implied_value(left, &hard_max)) {
1680 if (op == '<' || op == SPECIAL_UNSIGNED_LT)
1681 hard_max.value++;
1682 estate_set_fuzzy_max(right_true_state, hard_max);
1684 break;
1685 case '>':
1686 case SPECIAL_UNSIGNED_GT:
1687 case SPECIAL_UNSIGNED_GTE:
1688 case SPECIAL_GTE:
1689 if (get_hard_max(left, &hard_max)) {
1690 if (op == '>' || op == SPECIAL_UNSIGNED_GT)
1691 hard_max.value--;
1692 estate_set_fuzzy_max(right_true_state, hard_max);
1694 if (get_implied_value(left, &hard_max)) {
1695 if (op == SPECIAL_UNSIGNED_GTE ||
1696 op == SPECIAL_GTE)
1697 hard_max.value++;
1698 estate_set_fuzzy_max(right_false_state, hard_max);
1700 if (get_hard_max(right, &hard_max)) {
1701 if (op == SPECIAL_UNSIGNED_LTE ||
1702 op == SPECIAL_LTE)
1703 hard_max.value--;
1704 estate_set_fuzzy_max(left_false_state, hard_max);
1706 if (get_implied_value(right, &hard_max)) {
1707 if (op == '>' ||
1708 op == SPECIAL_UNSIGNED_GT)
1709 hard_max.value++;
1710 estate_set_fuzzy_max(left_true_state, hard_max);
1712 break;
1713 case SPECIAL_EQUAL:
1714 if (get_hard_max(left, &hard_max))
1715 estate_set_fuzzy_max(right_true_state, hard_max);
1716 if (get_hard_max(right, &hard_max))
1717 estate_set_fuzzy_max(left_true_state, hard_max);
1718 break;
1721 if (get_hard_max(left, &hard_max)) {
1722 estate_set_hard_max(left_true_state);
1723 estate_set_hard_max(left_false_state);
1725 if (get_hard_max(right, &hard_max)) {
1726 estate_set_hard_max(right_true_state);
1727 estate_set_hard_max(right_false_state);
1730 if (left_postop == SPECIAL_INCREMENT) {
1731 left_true_state = increment_state(left_true_state);
1732 left_false_state = increment_state(left_false_state);
1734 if (left_postop == SPECIAL_DECREMENT) {
1735 left_true_state = decrement_state(left_true_state);
1736 left_false_state = decrement_state(left_false_state);
1738 if (right_postop == SPECIAL_INCREMENT) {
1739 right_true_state = increment_state(right_true_state);
1740 right_false_state = increment_state(right_false_state);
1742 if (right_postop == SPECIAL_DECREMENT) {
1743 right_true_state = decrement_state(right_true_state);
1744 right_false_state = decrement_state(right_false_state);
1747 if (estate_rl(left_true_state) && estates_equiv(left_true_state, left_false_state)) {
1748 left_true_state = NULL;
1749 left_false_state = NULL;
1752 if (estate_rl(right_true_state) && estates_equiv(right_true_state, right_false_state)) {
1753 right_true_state = NULL;
1754 right_false_state = NULL;
1757 /* Don't introduce new states for known true/false conditions */
1758 if (rl_equiv(left_orig, estate_rl(left_true_state)))
1759 left_true_state = NULL;
1760 if (rl_equiv(left_orig, estate_rl(left_false_state)))
1761 left_false_state = NULL;
1762 if (rl_equiv(right_orig, estate_rl(right_true_state)))
1763 right_true_state = NULL;
1764 if (rl_equiv(right_orig, estate_rl(right_false_state)))
1765 right_false_state = NULL;
1767 set_extra_expr_true_false(left, left_true_state, left_false_state);
1768 set_extra_expr_true_false(right, right_true_state, right_false_state);
1771 static int is_simple_math(struct expression *expr)
1773 if (!expr)
1774 return 0;
1775 if (expr->type != EXPR_BINOP)
1776 return 0;
1777 switch (expr->op) {
1778 case '+':
1779 case '-':
1780 case '*':
1781 return 1;
1783 return 0;
1786 static int flip_op(int op)
1788 /* We only care about simple math */
1789 switch (op) {
1790 case '+':
1791 return '-';
1792 case '-':
1793 return '+';
1794 case '*':
1795 return '/';
1797 return 0;
1800 static void move_known_to_rl(struct expression **expr_p, struct range_list **rl_p)
1802 struct expression *expr = *expr_p;
1803 struct range_list *rl = *rl_p;
1804 sval_t sval;
1806 if (!is_simple_math(expr))
1807 return;
1809 if (get_implied_value(expr->right, &sval)) {
1810 *expr_p = expr->left;
1811 *rl_p = rl_binop(rl, flip_op(expr->op), alloc_rl(sval, sval));
1812 move_known_to_rl(expr_p, rl_p);
1813 return;
1815 if (expr->op == '-')
1816 return;
1817 if (get_implied_value(expr->left, &sval)) {
1818 *expr_p = expr->right;
1819 *rl_p = rl_binop(rl, flip_op(expr->op), alloc_rl(sval, sval));
1820 move_known_to_rl(expr_p, rl_p);
1821 return;
1825 static void move_known_values(struct expression **left_p, struct expression **right_p)
1827 struct expression *left = *left_p;
1828 struct expression *right = *right_p;
1829 sval_t sval, dummy;
1831 if (get_implied_value(left, &sval)) {
1832 if (!is_simple_math(right))
1833 return;
1834 if (get_implied_value(right, &dummy))
1835 return;
1836 if (right->op == '*') {
1837 sval_t divisor;
1839 if (!get_value(right->right, &divisor))
1840 return;
1841 if (divisor.value == 0)
1842 return;
1843 *left_p = binop_expression(left, invert_op(right->op), right->right);
1844 *right_p = right->left;
1845 return;
1847 if (right->op == '+' && get_value(right->left, &sval)) {
1848 *left_p = binop_expression(left, invert_op(right->op), right->left);
1849 *right_p = right->right;
1850 return;
1852 if (get_value(right->right, &sval)) {
1853 *left_p = binop_expression(left, invert_op(right->op), right->right);
1854 *right_p = right->left;
1855 return;
1857 return;
1859 if (get_implied_value(right, &sval)) {
1860 if (!is_simple_math(left))
1861 return;
1862 if (get_implied_value(left, &dummy))
1863 return;
1864 if (left->op == '*') {
1865 sval_t divisor;
1867 if (!get_value(left->right, &divisor))
1868 return;
1869 if (divisor.value == 0)
1870 return;
1871 *right_p = binop_expression(right, invert_op(left->op), left->right);
1872 *left_p = left->left;
1873 return;
1875 if (left->op == '+' && get_value(left->left, &sval)) {
1876 *right_p = binop_expression(right, invert_op(left->op), left->left);
1877 *left_p = left->right;
1878 return;
1881 if (get_value(left->right, &sval)) {
1882 *right_p = binop_expression(right, invert_op(left->op), left->right);
1883 *left_p = left->left;
1884 return;
1886 return;
1891 * The reason for do_simple_algebra() is to solve things like:
1892 * if (foo > 66 || foo + bar > 64) {
1893 * "foo" is not really a known variable so it won't be handled by
1894 * move_known_variables() but it's a super common idiom.
1897 static int do_simple_algebra(struct expression **left_p, struct expression **right_p)
1899 struct expression *left = *left_p;
1900 struct expression *right = *right_p;
1901 struct range_list *rl;
1902 sval_t tmp;
1904 if (left->type != EXPR_BINOP || left->op != '+')
1905 return 0;
1906 if (can_integer_overflow(get_type(left), left))
1907 return 0;
1908 if (!get_implied_value(right, &tmp))
1909 return 0;
1911 if (!get_implied_value(left->left, &tmp) &&
1912 get_implied_rl(left->left, &rl) &&
1913 !is_whole_rl(rl)) {
1914 *right_p = binop_expression(right, '-', left->left);
1915 *left_p = left->right;
1916 return 1;
1918 if (!get_implied_value(left->right, &tmp) &&
1919 get_implied_rl(left->right, &rl) &&
1920 !is_whole_rl(rl)) {
1921 *right_p = binop_expression(right, '-', left->right);
1922 *left_p = left->left;
1923 return 1;
1926 return 0;
1929 static int match_func_comparison(struct expression *expr)
1931 struct expression *left = strip_expr(expr->left);
1932 struct expression *right = strip_expr(expr->right);
1934 if (left->type == EXPR_CALL || right->type == EXPR_CALL) {
1935 function_comparison(left, expr->op, right);
1936 return 1;
1939 return 0;
1942 /* Handle conditions like "if (foo + bar < foo) {" */
1943 static int handle_integer_overflow_test(struct expression *expr)
1945 struct expression *left, *right;
1946 struct symbol *type;
1947 sval_t left_min, right_min, min, max;
1949 if (expr->op != '<' && expr->op != SPECIAL_UNSIGNED_LT)
1950 return 0;
1952 left = strip_parens(expr->left);
1953 right = strip_parens(expr->right);
1955 if (left->op != '+')
1956 return 0;
1958 type = get_type(expr);
1959 if (!type)
1960 return 0;
1961 if (type_positive_bits(type) == 32) {
1962 max.type = &uint_ctype;
1963 max.uvalue = (unsigned int)-1;
1964 } else if (type_positive_bits(type) == 64) {
1965 max.type = &ulong_ctype;
1966 max.value = (unsigned long long)-1;
1967 } else {
1968 return 0;
1971 if (!expr_equiv(left->left, right) && !expr_equiv(left->right, right))
1972 return 0;
1974 get_absolute_min(left->left, &left_min);
1975 get_absolute_min(left->right, &right_min);
1976 min = sval_binop(left_min, '+', right_min);
1978 type = get_type(left);
1979 min = sval_cast(type, min);
1980 max = sval_cast(type, max);
1982 set_extra_chunk_true_false(left, NULL, alloc_estate_range(min, max));
1983 return 1;
1986 static void match_comparison(struct expression *expr)
1988 struct expression *left_orig = strip_parens(expr->left);
1989 struct expression *right_orig = strip_parens(expr->right);
1990 struct expression *left, *right, *tmp;
1991 struct expression *prev;
1992 struct symbol *type;
1993 int redo, count;
1995 if (match_func_comparison(expr))
1996 return;
1998 type = get_type(expr);
1999 if (!type)
2000 type = &llong_ctype;
2002 if (handle_integer_overflow_test(expr))
2003 return;
2005 left = left_orig;
2006 right = right_orig;
2007 move_known_values(&left, &right);
2008 handle_comparison(type, left, expr->op, right);
2010 left = left_orig;
2011 right = right_orig;
2012 if (do_simple_algebra(&left, &right))
2013 handle_comparison(type, left, expr->op, right);
2015 prev = get_assigned_expr(left_orig);
2016 if (is_simple_math(prev) && !has_variable(prev, left_orig)) {
2017 left = prev;
2018 right = right_orig;
2019 move_known_values(&left, &right);
2020 handle_comparison(type, left, expr->op, right);
2023 prev = get_assigned_expr(right_orig);
2024 if (is_simple_math(prev) && !has_variable(prev, right_orig)) {
2025 left = left_orig;
2026 right = prev;
2027 move_known_values(&left, &right);
2028 handle_comparison(type, left, expr->op, right);
2031 redo = 0;
2032 left = left_orig;
2033 right = right_orig;
2034 if (get_last_expr_from_expression_stmt(left_orig)) {
2035 left = get_last_expr_from_expression_stmt(left_orig);
2036 redo = 1;
2038 if (get_last_expr_from_expression_stmt(right_orig)) {
2039 right = get_last_expr_from_expression_stmt(right_orig);
2040 redo = 1;
2043 if (!redo)
2044 return;
2046 count = 0;
2047 while ((tmp = get_assigned_expr(left))) {
2048 if (count++ > 3)
2049 break;
2050 left = strip_expr(tmp);
2052 count = 0;
2053 while ((tmp = get_assigned_expr(right))) {
2054 if (count++ > 3)
2055 break;
2056 right = strip_expr(tmp);
2059 handle_comparison(type, left, expr->op, right);
2062 static sval_t get_high_mask(sval_t known)
2064 sval_t ret;
2065 int i;
2067 ret = known;
2068 ret.value = 0;
2070 for (i = type_bits(known.type) - 1; i >= 0; i--) {
2071 if (known.uvalue & (1ULL << i))
2072 ret.uvalue |= (1ULL << i);
2073 else
2074 return ret;
2077 return ret;
2080 static bool handle_bit_test(struct expression *expr)
2082 struct range_list *orig_rl, *rl;
2083 struct expression *shift, *mask, *var;
2084 struct bit_info *bit_info;
2085 sval_t sval;
2086 sval_t high = { .type = &int_ctype };
2087 sval_t low = { .type = &int_ctype };
2089 shift = strip_expr(expr->right);
2090 mask = strip_expr(expr->left);
2091 if (shift->type != EXPR_BINOP || shift->op != SPECIAL_LEFTSHIFT) {
2092 shift = strip_expr(expr->left);
2093 mask = strip_expr(expr->right);
2094 if (shift->type != EXPR_BINOP || shift->op != SPECIAL_LEFTSHIFT)
2095 return false;
2097 if (!get_implied_value(shift->left, &sval) || sval.value != 1)
2098 return false;
2099 var = strip_expr(shift->right);
2101 bit_info = get_bit_info(mask);
2102 if (!bit_info)
2103 return false;
2104 if (!bit_info->possible)
2105 return false;
2107 get_absolute_rl(var, &orig_rl);
2108 if (sval_is_negative(rl_min(orig_rl)) ||
2109 rl_max(orig_rl).uvalue > type_bits(get_type(shift->left)))
2110 return false;
2112 low.value = ffsll(bit_info->possible);
2113 high.value = sm_fls64(bit_info->possible);
2114 rl = alloc_rl(low, high);
2115 rl = cast_rl(get_type(var), rl);
2116 rl = rl_intersection(orig_rl, rl);
2117 if (!rl)
2118 return false;
2120 set_extra_expr_true_false(shift->right, alloc_estate_rl(rl), NULL);
2122 return true;
2125 static void handle_AND_op(struct expression *var, sval_t known)
2127 struct range_list *orig_rl;
2128 struct range_list *true_rl = NULL;
2129 struct range_list *false_rl = NULL;
2130 int bit;
2131 sval_t low_mask = known;
2132 sval_t high_mask;
2133 sval_t max;
2135 get_absolute_rl(var, &orig_rl);
2137 if (known.value > 0) {
2138 bit = ffsll(known.value) - 1;
2139 low_mask.uvalue = (1ULL << bit) - 1;
2140 true_rl = remove_range(orig_rl, sval_type_val(known.type, 0), low_mask);
2142 high_mask = get_high_mask(known);
2143 if (high_mask.value) {
2144 bit = ffsll(high_mask.value) - 1;
2145 low_mask.uvalue = (1ULL << bit) - 1;
2147 false_rl = orig_rl;
2148 if (sval_is_negative(rl_min(orig_rl)))
2149 false_rl = remove_range(false_rl, sval_type_min(known.type), sval_type_val(known.type, -1));
2150 false_rl = remove_range(false_rl, low_mask, sval_type_max(known.type));
2151 if (type_signed(high_mask.type) && type_unsigned(rl_type(false_rl))) {
2152 false_rl = remove_range(false_rl,
2153 sval_type_val(rl_type(false_rl), sval_type_max(known.type).uvalue),
2154 sval_type_val(rl_type(false_rl), -1));
2156 } else if (known.value == 1 &&
2157 get_hard_max(var, &max) &&
2158 sval_cmp(max, rl_max(orig_rl)) == 0 &&
2159 max.value & 1) {
2160 false_rl = remove_range(orig_rl, max, max);
2162 set_extra_expr_true_false(var,
2163 true_rl ? alloc_estate_rl(true_rl) : NULL,
2164 false_rl ? alloc_estate_rl(false_rl) : NULL);
2167 static void handle_AND_condition(struct expression *expr)
2169 sval_t known;
2171 if (handle_bit_test(expr))
2172 return;
2174 if (get_implied_value(expr->left, &known))
2175 handle_AND_op(expr->right, known);
2176 else if (get_implied_value(expr->right, &known))
2177 handle_AND_op(expr->left, known);
2180 static void handle_MOD_condition(struct expression *expr)
2182 struct range_list *orig_rl;
2183 struct range_list *true_rl;
2184 struct range_list *false_rl = NULL;
2185 sval_t right;
2186 sval_t zero = {
2187 .value = 0,
2190 if (!get_implied_value(expr->right, &right) || right.value == 0)
2191 return;
2192 get_absolute_rl(expr->left, &orig_rl);
2194 zero.type = rl_type(orig_rl);
2196 /* We're basically dorking around the min and max here */
2197 true_rl = remove_range(orig_rl, zero, zero);
2198 if (!sval_is_max(rl_max(true_rl)) &&
2199 !(rl_max(true_rl).value % right.value))
2200 true_rl = remove_range(true_rl, rl_max(true_rl), rl_max(true_rl));
2202 if (rl_equiv(true_rl, orig_rl))
2203 true_rl = NULL;
2205 if (sval_is_positive(rl_min(orig_rl)) &&
2206 (rl_max(orig_rl).value - rl_min(orig_rl).value) / right.value < 5) {
2207 sval_t add;
2208 int i;
2210 add = rl_min(orig_rl);
2211 add.value += right.value - (add.value % right.value);
2212 add.value -= right.value;
2214 for (i = 0; i < 5; i++) {
2215 add.value += right.value;
2216 if (add.value > rl_max(orig_rl).value)
2217 break;
2218 add_range(&false_rl, add, add);
2220 } else {
2221 if (rl_min(orig_rl).uvalue != 0 &&
2222 rl_min(orig_rl).uvalue < right.uvalue) {
2223 sval_t chop = right;
2224 chop.value--;
2225 false_rl = remove_range(orig_rl, zero, chop);
2228 if (!sval_is_max(rl_max(orig_rl)) &&
2229 (rl_max(orig_rl).value % right.value)) {
2230 sval_t chop = rl_max(orig_rl);
2231 chop.value -= chop.value % right.value;
2232 chop.value++;
2233 if (!false_rl)
2234 false_rl = clone_rl(orig_rl);
2235 false_rl = remove_range(false_rl, chop, rl_max(orig_rl));
2239 set_extra_expr_true_false(expr->left,
2240 true_rl ? alloc_estate_rl(true_rl) : NULL,
2241 false_rl ? alloc_estate_rl(false_rl) : NULL);
2244 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
2245 void __extra_match_condition(struct expression *expr)
2247 expr = strip_expr(expr);
2248 switch (expr->type) {
2249 case EXPR_CALL:
2250 function_comparison(expr, SPECIAL_NOTEQUAL, zero_expr());
2251 return;
2252 case EXPR_PREOP:
2253 case EXPR_SYMBOL:
2254 case EXPR_DEREF:
2255 handle_comparison(get_type(expr), expr, SPECIAL_NOTEQUAL, zero_expr());
2256 return;
2257 case EXPR_COMPARE:
2258 match_comparison(expr);
2259 return;
2260 case EXPR_ASSIGNMENT:
2261 __extra_match_condition(expr->left);
2262 return;
2263 case EXPR_BINOP:
2264 if (expr->op == '&')
2265 handle_AND_condition(expr);
2266 if (expr->op == '%')
2267 handle_MOD_condition(expr);
2268 return;
2272 static void assume_indexes_are_valid(struct expression *expr)
2274 struct expression *array_expr;
2275 int array_size;
2276 struct expression *offset;
2277 struct symbol *offset_type;
2278 struct range_list *rl_before;
2279 struct range_list *rl_after;
2280 struct range_list *filter = NULL;
2281 sval_t size;
2283 expr = strip_expr(expr);
2284 if (!is_array(expr))
2285 return;
2287 offset = get_array_offset(expr);
2288 offset_type = get_type(offset);
2289 if (offset_type && type_signed(offset_type)) {
2290 filter = alloc_rl(sval_type_min(offset_type),
2291 sval_type_val(offset_type, -1));
2294 array_expr = get_array_base(expr);
2295 array_size = get_real_array_size(array_expr);
2296 if (array_size > 1) {
2297 size = sval_type_val(offset_type, array_size);
2298 add_range(&filter, size, sval_type_max(offset_type));
2301 if (!filter)
2302 return;
2303 get_absolute_rl(offset, &rl_before);
2304 rl_after = rl_filter(rl_before, filter);
2305 if (rl_equiv(rl_before, rl_after))
2306 return;
2307 set_extra_expr_nomod(offset, alloc_estate_rl(rl_after));
2310 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
2311 int implied_not_equal(struct expression *expr, long long val)
2313 return !possibly_false(expr, SPECIAL_NOTEQUAL, value_expr(val));
2316 int implied_not_equal_name_sym(char *name, struct symbol *sym, long long val)
2318 struct smatch_state *estate;
2320 estate = get_state(SMATCH_EXTRA, name, sym);
2321 if (!estate)
2322 return 0;
2323 if (!rl_has_sval(estate_rl(estate), sval_type_val(estate_type(estate), 0)))
2324 return 1;
2325 return 0;
2328 bool is_noderef_ptr(struct expression *expr)
2330 struct range_list *rl;
2332 if (!get_implied_rl(expr, &rl))
2333 return false;
2334 return is_noderef_ptr_rl(rl);
2337 static int parent_is_err_or_null_var_sym_helper(const char *name, struct symbol *sym, bool check_err_ptr)
2339 struct smatch_state *state;
2340 char buf[256];
2341 char *start;
2342 int len;
2344 strncpy(buf, name, sizeof(buf) - 1);
2345 buf[sizeof(buf) - 1] = '\0';
2347 start = &buf[0];
2348 while (*start == '*') {
2349 start++;
2350 state = __get_state(SMATCH_EXTRA, start, sym);
2351 if (!state)
2352 continue;
2353 if (!estate_rl(state))
2354 return 1;
2355 if (is_noderef_ptr_rl(estate_rl(state)))
2356 return 1;
2359 start = &buf[0];
2360 while (*start == '&')
2361 start++;
2363 len = strlen(start);
2364 while (true) {
2365 while (len > 0) {
2366 len--;
2367 if (start[len] == '-' ||
2368 start[len] == '.') {
2369 start[len] = '\0';
2370 break;
2373 if (len == 0)
2374 return 0;
2375 state = __get_state(SMATCH_EXTRA, start, sym);
2376 if (!state)
2377 continue;
2378 if (is_noderef_ptr_rl(estate_rl(state)))
2379 return 1;
2383 int parent_is_null_var_sym(const char *name, struct symbol *sym)
2385 return parent_is_err_or_null_var_sym_helper(name, sym, false);
2388 int parent_is_err_or_null_var_sym(const char *name, struct symbol *sym)
2390 return parent_is_err_or_null_var_sym_helper(name, sym, (option_project == PROJ_KERNEL));
2393 int parent_is_null(struct expression *expr)
2395 struct symbol *sym;
2396 char *var;
2397 int ret = 0;
2399 expr = strip_expr(expr);
2400 var = expr_to_var_sym(expr, &sym);
2401 if (!var || !sym)
2402 goto free;
2403 ret = parent_is_null_var_sym(var, sym);
2404 free:
2405 free_string(var);
2406 return ret;
2409 static int param_used_callback(void *found, int argc, char **argv, char **azColName)
2411 *(int *)found = 1;
2412 return 0;
2415 static int is_kzalloc_info(struct sm_state *sm)
2417 sval_t sval;
2420 * kzalloc() information is treated as special because so there is just
2421 * a lot of stuff initialized to zero and it makes building the database
2422 * take hours and hours.
2424 * In theory, we should just remove this line and not pass any unused
2425 * information, but I'm not sure enough that this code works so I want
2426 * to hold off on that for now.
2428 if (!estate_get_single_value(sm->state, &sval))
2429 return 0;
2430 if (sval.value != 0)
2431 return 0;
2432 return 1;
2435 static int is_really_long(struct sm_state *sm)
2437 const char *p;
2438 int cnt = 0;
2440 p = sm->name;
2441 while ((p = strstr(p, "->"))) {
2442 p += 2;
2443 cnt++;
2446 if (cnt < 3 ||
2447 strlen(sm->name) < 40)
2448 return 0;
2449 return 1;
2452 static int filter_unused_param_value_info(struct expression *call, int param, char *printed_name, struct sm_state *sm)
2454 int found = 0;
2456 /* for function pointers assume everything is used */
2457 if (call->fn->type != EXPR_SYMBOL)
2458 return 0;
2460 if (strcmp(printed_name, "$") == 0 ||
2461 strcmp(printed_name, "*$") == 0)
2462 return 0;
2465 * This is to handle __builtin_mul_overflow(). In an ideal world we
2466 * would only need this for invalid code.
2469 if (!call->fn->symbol)
2470 return 0;
2472 if (!is_kzalloc_info(sm) && !is_really_long(sm))
2473 return 0;
2475 run_sql(&param_used_callback, &found,
2476 "select * from return_implies where %s and type = %d and parameter = %d and key = '%s';",
2477 get_static_filter(call->fn->symbol), PARAM_USED, param, printed_name);
2478 if (found)
2479 return 0;
2481 /* If the database is not built yet, then assume everything is used */
2482 run_sql(&param_used_callback, &found,
2483 "select * from return_implies where %s and type = %d;",
2484 get_static_filter(call->fn->symbol), PARAM_USED);
2485 if (!found)
2486 return 0;
2488 return 1;
2491 struct range_list *intersect_with_real_abs_var_sym(const char *name, struct symbol *sym, struct range_list *start)
2493 struct smatch_state *state;
2496 * Here is the difference between implied value and real absolute, say
2497 * you have:
2499 * int a = (u8)x;
2501 * Then you know that a is 0-255. That's real absolute. But you don't
2502 * know for sure that it actually goes up to 255. So it's not implied.
2503 * Implied indicates a degree of certainty.
2505 * But then say you cap "a" at 8. That means you know it goes up to
2506 * 8. So now the implied value is s32min-8. But you can combine it
2507 * with the real absolute to say that actually it's 0-8.
2509 * We are combining it here. But now that I think about it, this is
2510 * probably not the ideal place to combine it because it should proably
2511 * be done earlier. Oh well, this is an improvement on what was there
2512 * before so I'm going to commit this code.
2516 state = get_real_absolute_state_var_sym(name, sym);
2517 if (!state || !estate_rl(state))
2518 return start;
2520 return rl_intersection(estate_rl(state), start);
2523 struct range_list *intersect_with_real_abs_expr(struct expression *expr, struct range_list *start)
2525 struct smatch_state *state;
2526 struct range_list *abs_rl;
2528 state = get_real_absolute_state(expr);
2529 if (!state || !estate_rl(state))
2530 return start;
2532 abs_rl = cast_rl(rl_type(start), estate_rl(state));
2533 return rl_intersection(abs_rl, start);
2536 static void caller_info_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
2538 struct range_list *rl;
2539 sval_t dummy;
2541 if (estate_is_whole(sm->state) || !estate_rl(sm->state))
2542 return;
2543 if (filter_unused_param_value_info(call, param, printed_name, sm))
2544 return;
2545 rl = estate_rl(sm->state);
2546 rl = intersect_with_real_abs_var_sym(sm->name, sm->sym, rl);
2547 if (!rl)
2548 return;
2549 sql_insert_caller_info(call, PARAM_VALUE, param, printed_name, show_rl(rl));
2550 if (!estate_get_single_value(sm->state, &dummy)) {
2551 if (estate_has_hard_max(sm->state))
2552 sql_insert_caller_info(call, HARD_MAX, param, printed_name,
2553 sval_to_str(estate_max(sm->state)));
2554 if (estate_has_fuzzy_max(sm->state))
2555 sql_insert_caller_info(call, FUZZY_MAX, param, printed_name,
2556 sval_to_str(estate_get_fuzzy_max(sm->state)));
2560 static void returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
2562 struct symbol *returned_sym;
2563 char *returned_name;
2564 struct sm_state *sm;
2565 char *compare_str;
2566 char name_buf[256];
2567 char val_buf[256];
2568 int len;
2570 // FIXME handle *$
2572 if (!is_pointer(expr))
2573 return;
2574 if (return_ranges && strstr(return_ranges, "[==$"))
2575 return;
2577 returned_name = expr_to_var_sym(expr, &returned_sym);
2578 if (!returned_name || !returned_sym)
2579 goto free;
2580 len = strlen(returned_name);
2582 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
2583 if (!estate_rl(sm->state))
2584 continue;
2585 if (returned_sym != sm->sym)
2586 continue;
2587 if (strncmp(returned_name, sm->name, len) != 0)
2588 continue;
2589 if (sm->name[len] != '-')
2590 continue;
2592 snprintf(name_buf, sizeof(name_buf), "$%s", sm->name + len);
2594 compare_str = name_sym_to_param_comparison(sm->name, sm->sym);
2595 if (!compare_str && estate_is_whole(sm->state))
2596 continue;
2597 snprintf(val_buf, sizeof(val_buf), "%s%s", sm->state->name, compare_str ?: "");
2599 sql_insert_return_states(return_id, return_ranges, PARAM_VALUE,
2600 -1, name_buf, val_buf);
2601 } END_FOR_EACH_SM(sm);
2603 free:
2604 free_string(returned_name);
2607 static void db_limited_before(void)
2609 unmatched_stree = clone_stree(__get_cur_stree());
2612 static void db_limited_after(void)
2614 free_stree(&unmatched_stree);
2617 static int basically_the_same(struct range_list *orig, struct range_list *new)
2619 if (type_is_ptr(rl_type(orig)) &&
2620 is_whole_ptr_rl(orig) &&
2621 is_whole_ptr_rl(new))
2622 return true;
2624 return rl_equiv(orig, new);
2627 static void db_param_limit_binops(struct expression *arg, char *key, struct range_list *rl)
2629 struct range_list *left_rl;
2630 sval_t zero = { .type = rl_type(rl), };
2631 sval_t sval;
2633 if (strcmp(key, "$") != 0)
2634 return;
2635 if (arg->op != '*')
2636 return;
2637 if (!get_implied_value(arg->right, &sval))
2638 return;
2639 if (can_integer_overflow(get_type(arg), arg))
2640 return;
2642 left_rl = rl_binop(rl, '/', alloc_rl(sval, sval));
2643 if (!rl_has_sval(rl, zero))
2644 left_rl = remove_range(left_rl, zero, zero);
2646 set_extra_expr_nomod(arg->left, alloc_estate_rl(left_rl));
2649 static void db_param_limit_filter(struct expression *expr, int param, char *key, char *value, enum info_type op)
2651 struct smatch_state *state;
2652 struct expression *arg;
2653 char *name;
2654 struct symbol *sym;
2655 struct var_sym_list *vsl = NULL;
2656 struct sm_state *sm;
2657 struct symbol *compare_type, *var_type;
2658 struct range_list *rl;
2659 struct range_list *limit;
2660 struct range_list *new;
2661 char *other_name;
2662 struct symbol *other_sym;
2664 while (expr->type == EXPR_ASSIGNMENT)
2665 expr = strip_expr(expr->right);
2666 if (expr->type != EXPR_CALL)
2667 return;
2669 arg = get_argument_from_call_expr(expr->args, param);
2670 if (!arg)
2671 return;
2673 if (strcmp(key, "$") == 0)
2674 compare_type = get_arg_type(expr->fn, param);
2675 else
2676 compare_type = get_member_type_from_key(arg, key);
2678 call_results_to_rl(expr, compare_type, value, &limit);
2679 if (strcmp(key, "$") == 0)
2680 move_known_to_rl(&arg, &limit);
2681 name = get_chunk_from_key(arg, key, &sym, &vsl);
2682 if (!name)
2683 return;
2684 if (op != PARAM_LIMIT && !sym)
2685 goto free;
2687 sm = get_sm_state(SMATCH_EXTRA, name, sym);
2688 if (sm)
2689 rl = estate_rl(sm->state);
2690 else
2691 rl = alloc_whole_rl(compare_type);
2693 if (op == PARAM_LIMIT && !rl_fits_in_type(rl, compare_type))
2694 goto free;
2696 new = rl_intersection(rl, limit);
2698 var_type = get_member_type_from_key(arg, key);
2699 new = cast_rl(var_type, new);
2701 /* We want to preserve the implications here */
2702 if (sm && basically_the_same(rl, new))
2703 goto free;
2704 other_name = get_other_name_sym(name, sym, &other_sym);
2706 state = alloc_estate_rl(new);
2707 if (sm && estate_has_hard_max(sm->state))
2708 estate_set_hard_max(state);
2710 if (op == PARAM_LIMIT) {
2711 set_extra_nomod_vsl(name, sym, vsl, NULL, state);
2712 } else
2713 set_extra_mod(name, sym, NULL, state);
2715 if (other_name && other_sym) {
2716 state = clone_estate(state);
2717 if (op == PARAM_LIMIT)
2718 set_extra_nomod_vsl(other_name, other_sym, vsl, NULL, state);
2719 else
2720 set_extra_mod(other_name, other_sym, NULL, state);
2723 if (op == PARAM_LIMIT && arg->type == EXPR_BINOP)
2724 db_param_limit_binops(arg, key, new);
2725 free:
2726 free_string(name);
2729 static void db_param_limit(struct expression *expr, int param, char *key, char *value)
2731 db_param_limit_filter(expr, param, key, value, PARAM_LIMIT);
2734 static void db_param_filter(struct expression *expr, int param, char *key, char *value)
2736 db_param_limit_filter(expr, param, key, value, PARAM_FILTER);
2739 static void db_param_add_set(struct expression *expr, int param, char *key, char *value, enum info_type op)
2741 struct expression *arg, *gen_expr;
2742 char *name;
2743 char *other_name = NULL;
2744 struct symbol *sym, *other_sym;
2745 struct symbol *param_type, *arg_type;
2746 struct smatch_state *state;
2747 struct range_list *new = NULL;
2748 struct range_list *added = NULL;
2750 while (expr->type == EXPR_ASSIGNMENT)
2751 expr = strip_expr(expr->right);
2752 if (expr->type != EXPR_CALL)
2753 return;
2755 arg = get_argument_from_call_expr(expr->args, param);
2756 if (!arg)
2757 return;
2759 arg_type = get_arg_type_from_key(expr->fn, param, arg, key);
2760 param_type = get_member_type_from_key(arg, key);
2761 if (param_type && param_type->type == SYM_STRUCT)
2762 return;
2763 name = get_variable_from_key(arg, key, &sym);
2764 if (!name || !sym)
2765 goto free;
2766 gen_expr = gen_expression_from_key(arg, key);
2768 state = get_state(SMATCH_EXTRA, name, sym);
2769 if (state)
2770 new = estate_rl(state);
2772 call_results_to_rl(expr, arg_type, value, &added);
2773 added = cast_rl(param_type, added);
2774 if (op == PARAM_SET)
2775 new = added;
2776 else
2777 new = rl_union(new, added);
2779 other_name = get_other_name_sym_nostack(name, sym, &other_sym);
2780 set_extra_mod(name, sym, gen_expr, alloc_estate_rl(new));
2781 if (other_name && other_sym)
2782 set_extra_mod(other_name, other_sym, gen_expr, alloc_estate_rl(new));
2783 free:
2784 free_string(other_name);
2785 free_string(name);
2788 static void db_param_add(struct expression *expr, int param, char *key, char *value)
2790 in_param_set = true;
2791 db_param_add_set(expr, param, key, value, PARAM_ADD);
2792 in_param_set = false;
2795 static void db_param_set(struct expression *expr, int param, char *key, char *value)
2797 in_param_set = true;
2798 db_param_add_set(expr, param, key, value, PARAM_SET);
2799 in_param_set = false;
2802 static void match_lost_param(struct expression *call, int param)
2804 struct expression *arg;
2806 if (is_const_param(call->fn, param))
2807 return;
2809 arg = get_argument_from_call_expr(call->args, param);
2810 if (!arg)
2811 return;
2813 arg = strip_expr(arg);
2814 if (arg->type == EXPR_PREOP && arg->op == '&')
2815 set_extra_expr_mod(arg->unop, alloc_estate_whole(get_type(arg->unop)));
2816 else
2817 ; /* if pointer then set struct members, maybe?*/
2820 static void db_param_value(struct expression *expr, int param, char *key, char *value)
2822 struct expression *call;
2823 char *name;
2824 struct symbol *sym;
2825 struct symbol *type;
2826 struct range_list *rl = NULL;
2828 if (param != -1)
2829 return;
2831 call = expr;
2832 while (call->type == EXPR_ASSIGNMENT)
2833 call = strip_expr(call->right);
2834 if (call->type != EXPR_CALL)
2835 return;
2837 type = get_member_type_from_key(expr->left, key);
2838 name = get_variable_from_key(expr->left, key, &sym);
2839 if (!name || !sym)
2840 goto free;
2842 call_results_to_rl(call, type, value, &rl);
2844 set_extra_mod(name, sym, NULL, alloc_estate_rl(rl));
2845 free:
2846 free_string(name);
2849 static void set_param_value(const char *name, struct symbol *sym, char *key, char *value)
2851 struct expression *expr;
2852 struct range_list *rl = NULL;
2853 struct smatch_state *state;
2854 struct symbol *type;
2855 char *key_orig = key;
2856 char *fullname;
2857 sval_t dummy;
2859 expr = symbol_expression(sym);
2860 fullname = get_variable_from_key(expr, key, NULL);
2861 if (!fullname)
2862 return;
2864 type = get_member_type_from_key(expr, key_orig);
2865 str_to_rl(type, value, &rl);
2866 state = alloc_estate_rl(rl);
2867 if (estate_get_single_value(state, &dummy))
2868 estate_set_hard_max(state);
2869 set_state(SMATCH_EXTRA, fullname, sym, state);
2872 static void set_param_fuzzy_max(const char *name, struct symbol *sym, char *key, char *value)
2874 struct expression *expr;
2875 struct range_list *rl = NULL;
2876 struct smatch_state *state;
2877 struct symbol *type;
2878 char *fullname;
2879 sval_t max;
2881 expr = symbol_expression(sym);
2882 fullname = get_variable_from_key(expr, key, NULL);
2883 if (!fullname)
2884 return;
2886 state = get_state(SMATCH_EXTRA, fullname, sym);
2887 if (!state)
2888 return;
2889 type = estate_type(state);
2890 str_to_rl(type, value, &rl);
2891 if (!rl_to_sval(rl, &max))
2892 return;
2893 estate_set_fuzzy_max(state, max);
2896 static void set_param_hard_max(const char *name, struct symbol *sym, char *key, char *value)
2898 struct smatch_state *state;
2899 struct expression *expr;
2900 char *fullname;
2902 expr = symbol_expression(sym);
2903 fullname = get_variable_from_key(expr, key, NULL);
2904 if (!fullname)
2905 return;
2907 state = get_state(SMATCH_EXTRA, fullname, sym);
2908 if (!state)
2909 return;
2910 estate_set_hard_max(state);
2913 static struct sm_state *get_sm_from_call(struct expression *expr)
2915 char buf[32];
2917 if (is_fake_call(expr))
2918 return NULL;
2920 snprintf(buf, sizeof(buf), "return %p", expr);
2921 return get_sm_state(SMATCH_EXTRA, buf, NULL);
2924 struct sm_state *get_extra_sm_state(struct expression *expr)
2926 char *name;
2927 struct symbol *sym;
2928 struct sm_state *ret = NULL;
2930 expr = strip_expr(expr);
2931 if (!expr)
2932 return NULL;
2934 if (expr->type == EXPR_CALL)
2935 return get_sm_from_call(expr);
2937 name = expr_to_known_chunk_sym(expr, &sym);
2938 if (!name)
2939 goto free;
2941 ret = get_sm_state(SMATCH_EXTRA, name, sym);
2942 free:
2943 free_string(name);
2944 return ret;
2947 struct smatch_state *get_extra_state(struct expression *expr)
2949 struct sm_state *sm;
2951 sm = get_extra_sm_state(expr);
2952 if (!sm)
2953 return NULL;
2954 return sm->state;
2957 void register_smatch_extra(int id)
2959 my_id = id;
2961 set_dynamic_states(my_id);
2962 add_merge_hook(my_id, &merge_estates);
2963 add_unmatched_state_hook(my_id, &unmatched_state);
2964 select_caller_info_hook(set_param_value, PARAM_VALUE);
2965 select_caller_info_hook(set_param_fuzzy_max, FUZZY_MAX);
2966 select_caller_info_hook(set_param_hard_max, HARD_MAX);
2967 select_return_states_before(&db_limited_before);
2968 select_return_states_hook(PARAM_LIMIT, &db_param_limit);
2969 select_return_states_hook(PARAM_FILTER, &db_param_filter);
2970 select_return_states_hook(PARAM_ADD, &db_param_add);
2971 select_return_states_hook(PARAM_SET, &db_param_set);
2972 add_lost_param_hook(&match_lost_param);
2973 select_return_states_hook(PARAM_VALUE, &db_param_value);
2974 select_return_states_after(&db_limited_after);
2977 static void match_link_modify(struct sm_state *sm, struct expression *mod_expr)
2979 struct var_sym_list *links;
2980 struct var_sym *tmp;
2981 struct smatch_state *state;
2983 links = sm->state->data;
2985 FOR_EACH_PTR(links, tmp) {
2986 if (sm->sym == tmp->sym &&
2987 strcmp(sm->name, tmp->var) == 0)
2988 continue;
2989 state = get_state(SMATCH_EXTRA, tmp->var, tmp->sym);
2990 if (!state)
2991 continue;
2992 set_state(SMATCH_EXTRA, tmp->var, tmp->sym, alloc_estate_whole(estate_type(state)));
2993 } END_FOR_EACH_PTR(tmp);
2994 set_state(link_id, sm->name, sm->sym, &undefined);
2997 void register_smatch_extra_links(int id)
2999 link_id = id;
3000 set_dynamic_states(link_id);
3003 void register_smatch_extra_late(int id)
3005 add_merge_hook(link_id, &merge_link_states);
3006 add_modification_hook(link_id, &match_link_modify);
3007 add_hook(&match_dereferences, DEREF_HOOK);
3008 add_hook(&match_pointer_as_array, OP_HOOK);
3009 select_return_implies_hook(DEREFERENCE, &set_param_dereferenced);
3010 add_hook(&match_function_call, FUNCTION_CALL_HOOK);
3011 add_hook(&match_assign, ASSIGNMENT_HOOK);
3012 add_hook(&match_assign, GLOBAL_ASSIGNMENT_HOOK);
3013 add_hook(&unop_expr, OP_HOOK);
3014 add_hook(&asm_expr, ASM_HOOK);
3016 add_caller_info_callback(my_id, caller_info_callback);
3017 add_split_return_callback(&returned_struct_members);
3019 // add_hook(&assume_indexes_are_valid, OP_HOOK);