constraints_required: ignore fake assignments
[smatch.git] / smatch_extra.c
blobfddf8475a081297a445bfeb75cd755aacb8718c6
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 bool in_param_set;
195 void set_extra_mod_helper(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
197 struct expression *faked;
199 if (!expr)
200 expr = gen_expression_from_name_sym(name, sym);
201 remove_from_equiv(name, sym);
202 set_union_info(name, sym, expr, state);
203 mark_sub_members_gone(name, sym, state);
204 call_extra_mod_hooks(name, sym, expr, state);
205 faked = get_faked_expression();
206 if (!faked ||
207 (faked->type == EXPR_ASSIGNMENT && is_fresh_alloc(faked->right)))
208 update_mtag_data(expr, state);
209 if ((__in_fake_assign || in_param_set) &&
210 estate_is_unknown(state) && !get_state(SMATCH_EXTRA, name, sym))
211 return;
212 set_state(SMATCH_EXTRA, name, sym, state);
215 static void set_extra_nomod_helper(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
217 call_extra_nomod_hooks(name, sym, expr, state);
218 set_state(SMATCH_EXTRA, name, sym, state);
221 static char *get_pointed_at(const char *name, struct symbol *sym, struct symbol **new_sym)
223 struct expression *assigned;
226 * Imagine we have an assignment: "foo = &addr;" then the other name
227 * of "*foo" is addr.
230 if (name[0] != '*')
231 return NULL;
232 if (strcmp(name + 1, sym->ident->name) != 0)
233 return NULL;
235 assigned = get_assigned_expr_name_sym(sym->ident->name, sym);
236 if (!assigned)
237 return NULL;
238 assigned = strip_parens(assigned);
239 if (assigned->type != EXPR_PREOP || assigned->op != '&')
240 return NULL;
242 return expr_to_var_sym(assigned->unop, new_sym);
245 char *get_other_name_sym_from_chunk(const char *name, const char *chunk, int len, struct symbol *sym, struct symbol **new_sym)
247 struct expression *assigned;
248 char *orig_name = NULL;
249 char buf[256];
250 char *ret;
252 assigned = get_assigned_expr_name_sym(chunk, sym);
253 if (!assigned)
254 return NULL;
255 if (assigned->type == EXPR_CALL)
256 return map_call_to_other_name_sym(name, sym, new_sym);
257 if (assigned->type == EXPR_PREOP && assigned->op == '&') {
259 orig_name = expr_to_var_sym(assigned, new_sym);
260 if (!orig_name || !*new_sym)
261 goto free;
263 snprintf(buf, sizeof(buf), "%s.%s", orig_name + 1, name + len);
264 ret = alloc_string(buf);
265 free_string(orig_name);
266 return ret;
269 orig_name = expr_to_var_sym(assigned, new_sym);
270 if (!orig_name || !*new_sym)
271 goto free;
273 snprintf(buf, sizeof(buf), "%s->%s", orig_name, name + len);
274 ret = alloc_string(buf);
275 free_string(orig_name);
276 return ret;
277 free:
278 free_string(orig_name);
279 return NULL;
282 static char *get_long_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym, bool use_stack)
284 struct expression *tmp;
285 struct sm_state *sm;
286 char buf[256];
289 * Just prepend the name with a different name/sym and return that.
290 * For example, if we set "foo->bar = bar;" then the other name
291 * for "bar->baz" is "foo->bar->baz". Or if we have "foo = bar;" then
292 * the other name for "bar" is "foo". A third option is if we have
293 * "foo = bar;" then another name for "*bar" is "*foo".
296 FOR_EACH_MY_SM(check_assigned_expr_id, __get_cur_stree(), sm) {
297 tmp = sm->state->data;
298 if (!tmp || tmp->type != EXPR_SYMBOL)
299 continue;
300 if (tmp->symbol == sym)
301 goto found;
302 } END_FOR_EACH_SM(sm);
304 return NULL;
306 found:
307 if (!use_stack && name[tmp->symbol->ident->len] != '-')
308 return NULL;
310 if (name[0] == '*' && strcmp(name + 1, tmp->symbol_name->name) == 0)
311 snprintf(buf, sizeof(buf), "*%s", sm->name);
312 else if (name[tmp->symbol->ident->len] == '-' ||
313 name[tmp->symbol->ident->len] == '.')
314 snprintf(buf, sizeof(buf), "%s%s", sm->name, name + tmp->symbol->ident->len);
315 else if (strcmp(name, tmp->symbol_name->name) == 0)
316 snprintf(buf, sizeof(buf), "%s", sm->name);
317 else
318 return NULL;
320 *new_sym = sm->sym;
321 return alloc_string(buf);
324 char *get_other_name_sym_helper(const char *name, struct symbol *sym, struct symbol **new_sym, bool use_stack)
326 char buf[256];
327 char *ret;
328 int len;
330 *new_sym = NULL;
332 if (!sym || !sym->ident)
333 return NULL;
335 ret = get_pointed_at(name, sym, new_sym);
336 if (ret)
337 return ret;
339 ret = map_long_to_short_name_sym(name, sym, new_sym, use_stack);
340 if (ret)
341 return ret;
343 len = snprintf(buf, sizeof(buf), "%s", name);
344 if (len >= sizeof(buf) - 2)
345 return NULL;
347 while (use_stack && len >= 1) {
348 if (buf[len] == '>' && buf[len - 1] == '-') {
349 len--;
350 buf[len] = '\0';
351 ret = get_other_name_sym_from_chunk(name, buf, len + 2, sym, new_sym);
352 if (ret)
353 return ret;
355 len--;
358 ret = get_long_name_sym(name, sym, new_sym, use_stack);
359 if (ret)
360 return ret;
362 return NULL;
365 char *get_other_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym)
367 return get_other_name_sym_helper(name, sym, new_sym, true);
370 char *get_other_name_sym_nostack(const char *name, struct symbol *sym, struct symbol **new_sym)
372 return get_other_name_sym_helper(name, sym, new_sym, false);
375 void set_extra_mod(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
377 char *new_name;
378 struct symbol *new_sym;
380 set_extra_mod_helper(name, sym, expr, state);
381 new_name = get_other_name_sym_nostack(name, sym, &new_sym);
382 if (new_name && new_sym)
383 set_extra_mod_helper(new_name, new_sym, NULL, state);
384 free_string(new_name);
387 static struct expression *chunk_get_array_base(struct expression *expr)
390 * The problem with is_array() is that it only returns true for things
391 * like foo[1] but not for foo[1].bar.
394 expr = strip_expr(expr);
395 while (expr && expr->type == EXPR_DEREF)
396 expr = strip_expr(expr->deref);
397 return get_array_base(expr);
400 static int chunk_has_array(struct expression *expr)
402 return !!chunk_get_array_base(expr);
405 static void clear_array_states(struct expression *array)
407 struct sm_state *sm;
409 sm = get_sm_state_expr(link_id, array);
410 if (sm)
411 match_link_modify(sm, NULL);
414 static void set_extra_array_mod(struct expression *expr, struct smatch_state *state)
416 struct expression *array;
417 struct var_sym_list *vsl;
418 struct var_sym *vs;
419 char *name;
420 struct symbol *sym;
422 array = chunk_get_array_base(expr);
424 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
425 if (!name || !vsl) {
426 clear_array_states(array);
427 goto free;
430 FOR_EACH_PTR(vsl, vs) {
431 store_link(link_id, vs->var, vs->sym, name, sym);
432 } END_FOR_EACH_PTR(vs);
434 call_extra_mod_hooks(name, sym, expr, state);
435 set_state(SMATCH_EXTRA, name, sym, state);
436 free:
437 free_string(name);
440 void set_extra_expr_mod(struct expression *expr, struct smatch_state *state)
442 struct symbol *sym;
443 char *name;
445 if (chunk_has_array(expr)) {
446 set_extra_array_mod(expr, state);
447 return;
450 expr = strip_expr(expr);
451 name = expr_to_var_sym(expr, &sym);
452 if (!name || !sym)
453 goto free;
454 set_extra_mod(name, sym, expr, state);
455 free:
456 free_string(name);
459 void set_extra_nomod(const char *name, struct symbol *sym, struct expression *expr, struct smatch_state *state)
461 char *new_name;
462 struct symbol *new_sym;
463 struct relation *rel;
464 struct smatch_state *orig_state;
466 orig_state = get_state(SMATCH_EXTRA, name, sym);
468 /* don't save unknown states if leaving it blank is the same */
469 if (!orig_state && estate_is_unknown(state))
470 return;
472 new_name = get_other_name_sym(name, sym, &new_sym);
473 if (new_name && new_sym)
474 set_extra_nomod_helper(new_name, new_sym, expr, state);
475 free_string(new_name);
477 if (!estate_related(orig_state)) {
478 set_extra_nomod_helper(name, sym, expr, state);
479 return;
482 set_related(state, estate_related(orig_state));
483 FOR_EACH_PTR(estate_related(orig_state), rel) {
484 struct smatch_state *estate;
486 estate = get_state(SMATCH_EXTRA, rel->name, rel->sym);
487 if (!estate)
488 continue;
489 set_extra_nomod_helper(rel->name, rel->sym, expr, clone_estate_cast(estate_type(estate), state));
490 } END_FOR_EACH_PTR(rel);
493 void set_extra_nomod_vsl(const char *name, struct symbol *sym, struct var_sym_list *vsl, struct expression *expr, struct smatch_state *state)
495 struct var_sym *vs;
497 FOR_EACH_PTR(vsl, vs) {
498 store_link(link_id, vs->var, vs->sym, name, sym);
499 } END_FOR_EACH_PTR(vs);
501 set_extra_nomod(name, sym, expr, state);
505 * This is for return_implies_state() hooks which modify a SMATCH_EXTRA state
507 void set_extra_expr_nomod(struct expression *expr, struct smatch_state *state)
509 struct var_sym_list *vsl;
510 struct var_sym *vs;
511 char *name;
512 struct symbol *sym;
514 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
515 if (!name || !vsl)
516 goto free;
517 FOR_EACH_PTR(vsl, vs) {
518 store_link(link_id, vs->var, vs->sym, name, sym);
519 } END_FOR_EACH_PTR(vs);
521 set_extra_nomod(name, sym, expr, state);
522 free:
523 free_string(name);
526 static void set_extra_true_false(const char *name, struct symbol *sym,
527 struct smatch_state *true_state,
528 struct smatch_state *false_state)
530 char *new_name;
531 struct symbol *new_sym;
532 struct relation *rel;
533 struct smatch_state *orig_state;
535 if (!true_state && !false_state)
536 return;
538 if (in_warn_on_macro())
539 return;
541 new_name = get_other_name_sym(name, sym, &new_sym);
542 if (new_name && new_sym)
543 set_true_false_states(SMATCH_EXTRA, new_name, new_sym, true_state, false_state);
544 free_string(new_name);
546 orig_state = get_state(SMATCH_EXTRA, name, sym);
548 if (!estate_related(orig_state)) {
549 set_true_false_states(SMATCH_EXTRA, name, sym, true_state, false_state);
550 return;
553 if (true_state)
554 set_related(true_state, estate_related(orig_state));
555 if (false_state)
556 set_related(false_state, estate_related(orig_state));
558 FOR_EACH_PTR(estate_related(orig_state), rel) {
559 set_true_false_states(SMATCH_EXTRA, rel->name, rel->sym,
560 true_state, false_state);
561 } END_FOR_EACH_PTR(rel);
564 static void set_extra_chunk_true_false(struct expression *expr,
565 struct smatch_state *true_state,
566 struct smatch_state *false_state)
568 struct var_sym_list *vsl;
569 struct var_sym *vs;
570 struct symbol *type;
571 char *name;
572 struct symbol *sym;
574 if (in_warn_on_macro())
575 return;
577 type = get_type(expr);
578 if (!type)
579 return;
581 name = expr_to_chunk_sym_vsl(expr, &sym, &vsl);
582 if (!name || !vsl)
583 goto free;
584 FOR_EACH_PTR(vsl, vs) {
585 store_link(link_id, vs->var, vs->sym, name, sym);
586 } END_FOR_EACH_PTR(vs);
588 set_true_false_states(SMATCH_EXTRA, name, sym,
589 clone_estate(true_state),
590 clone_estate(false_state));
591 free:
592 free_string(name);
595 static void set_extra_expr_true_false(struct expression *expr,
596 struct smatch_state *true_state,
597 struct smatch_state *false_state)
599 char *name;
600 struct symbol *sym;
601 sval_t sval;
603 if (!true_state && !false_state)
604 return;
606 if (get_value(expr, &sval))
607 return;
609 expr = strip_expr(expr);
610 name = expr_to_var_sym(expr, &sym);
611 if (!name || !sym) {
612 free_string(name);
613 set_extra_chunk_true_false(expr, true_state, false_state);
614 return;
616 set_extra_true_false(name, sym, true_state, false_state);
617 free_string(name);
620 static int get_countdown_info(struct expression *condition, struct expression **unop, int *op, sval_t *right)
622 struct expression *unop_expr;
623 int comparison;
624 sval_t limit;
626 right->type = &int_ctype;
627 right->value = 0;
629 condition = strip_expr(condition);
631 if (condition->type == EXPR_COMPARE) {
632 comparison = remove_unsigned_from_comparison(condition->op);
634 if (comparison != SPECIAL_GTE && comparison != '>')
635 return 0;
636 if (!get_value(condition->right, &limit))
637 return 0;
639 unop_expr = condition->left;
640 if (unop_expr->type != EXPR_PREOP && unop_expr->type != EXPR_POSTOP)
641 return 0;
642 if (unop_expr->op != SPECIAL_DECREMENT)
643 return 0;
645 *unop = unop_expr;
646 *op = comparison;
647 *right = limit;
649 return 1;
652 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
653 return 0;
654 if (condition->op != SPECIAL_DECREMENT)
655 return 0;
657 *unop = condition;
658 *op = '>';
660 return 1;
663 static struct sm_state *handle_canonical_while_count_down(struct statement *loop)
665 struct expression *iter_var;
666 struct expression *condition, *unop;
667 struct symbol *type;
668 struct sm_state *sm;
669 struct smatch_state *estate;
670 int op;
671 sval_t start, right;
673 right.type = &int_ctype;
674 right.value = 0;
676 condition = strip_expr(loop->iterator_pre_condition);
677 if (!condition)
678 return NULL;
680 if (!get_countdown_info(condition, &unop, &op, &right))
681 return NULL;
683 iter_var = unop->unop;
685 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
686 if (!sm)
687 return NULL;
688 if (sval_cmp(estate_min(sm->state), right) < 0)
689 return NULL;
690 start = estate_max(sm->state);
692 type = get_type(iter_var);
693 right = sval_cast(type, right);
694 start = sval_cast(type, start);
696 if (sval_cmp(start, right) <= 0)
697 return NULL;
698 if (!sval_is_max(start))
699 start.value--;
701 if (op == SPECIAL_GTE)
702 right.value--;
704 if (unop->type == EXPR_PREOP) {
705 right.value++;
706 estate = alloc_estate_range(right, start);
707 if (estate_has_hard_max(sm->state))
708 estate_set_hard_max(estate);
709 estate_copy_fuzzy_max(estate, sm->state);
710 set_extra_expr_mod(iter_var, estate);
712 if (unop->type == EXPR_POSTOP) {
713 estate = alloc_estate_range(right, start);
714 if (estate_has_hard_max(sm->state))
715 estate_set_hard_max(estate);
716 estate_copy_fuzzy_max(estate, sm->state);
717 set_extra_expr_mod(iter_var, estate);
719 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
722 static struct sm_state *handle_canonical_for_inc(struct expression *iter_expr,
723 struct expression *condition)
725 struct expression *iter_var;
726 struct sm_state *sm;
727 struct smatch_state *estate;
728 sval_t start, end, max;
729 struct symbol *type;
730 bool unknown_end = false;
732 iter_var = iter_expr->unop;
733 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
734 if (!sm)
735 return NULL;
736 if (!estate_get_single_value(sm->state, &start))
737 return NULL;
738 if (!get_implied_max(condition->right, &end)) {
739 end = sval_type_max(start.type);
740 unknown_end = true;
743 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
744 return NULL;
746 switch (condition->op) {
747 case SPECIAL_UNSIGNED_LT:
748 case SPECIAL_NOTEQUAL:
749 case '<':
750 if (!sval_is_min(end) && !unknown_end)
751 end.value--;
752 break;
753 case SPECIAL_UNSIGNED_LTE:
754 case SPECIAL_LTE:
755 break;
756 default:
757 return NULL;
759 if (sval_cmp(end, start) < 0)
760 return NULL;
761 type = get_type(iter_var);
762 start = sval_cast(type, start);
763 end = sval_cast(type, end);
764 estate = alloc_estate_range(start, end);
765 if (get_hard_max(condition->right, &max)) {
766 if (!get_macro_name(condition->pos))
767 estate_set_hard_max(estate);
768 if (condition->op == '<' ||
769 condition->op == SPECIAL_UNSIGNED_LT ||
770 condition->op == SPECIAL_NOTEQUAL)
771 max.value--;
772 max = sval_cast(type, max);
773 estate_set_fuzzy_max(estate, max);
775 set_extra_expr_mod(iter_var, estate);
776 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
779 static struct sm_state *handle_canonical_for_dec(struct expression *iter_expr,
780 struct expression *condition)
782 struct expression *iter_var;
783 struct sm_state *sm;
784 struct smatch_state *estate;
785 sval_t start, end;
787 iter_var = iter_expr->unop;
788 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
789 if (!sm)
790 return NULL;
791 if (!estate_get_single_value(sm->state, &start))
792 return NULL;
793 if (!get_implied_min(condition->right, &end))
794 end = sval_type_min(get_type(iter_var));
795 end = sval_cast(estate_type(sm->state), end);
796 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
797 return NULL;
799 switch (condition->op) {
800 case SPECIAL_NOTEQUAL:
801 case '>':
802 if (!sval_is_max(end))
803 end.value++;
804 break;
805 case SPECIAL_GTE:
806 break;
807 default:
808 return NULL;
810 if (sval_cmp(end, start) > 0)
811 return NULL;
812 estate = alloc_estate_range(end, start);
813 estate_set_hard_max(estate);
814 estate_set_fuzzy_max(estate, estate_get_fuzzy_max(estate));
815 set_extra_expr_mod(iter_var, estate);
816 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
819 static struct sm_state *handle_canonical_for_loops(struct statement *loop)
821 struct expression *iter_expr;
822 struct expression *condition;
824 if (!loop->iterator_post_statement)
825 return NULL;
826 if (loop->iterator_post_statement->type != STMT_EXPRESSION)
827 return NULL;
828 iter_expr = loop->iterator_post_statement->expression;
829 if (!loop->iterator_pre_condition)
830 return NULL;
831 if (loop->iterator_pre_condition->type != EXPR_COMPARE)
832 return NULL;
833 condition = loop->iterator_pre_condition;
835 if (iter_expr->op == SPECIAL_INCREMENT)
836 return handle_canonical_for_inc(iter_expr, condition);
837 if (iter_expr->op == SPECIAL_DECREMENT)
838 return handle_canonical_for_dec(iter_expr, condition);
839 return NULL;
842 struct sm_state *__extra_handle_canonical_loops(struct statement *loop, struct stree **stree)
844 struct sm_state *ret;
847 * Canonical loops are a hack. The proper way to handle this is to
848 * use two passes, but unfortunately, doing two passes makes parsing
849 * code twice as slow.
851 * What we do is we set the inside state here, which overwrites whatever
852 * __extra_match_condition() does. Then we set the outside state in
853 * __extra_pre_loop_hook_after().
856 __push_fake_cur_stree();
857 if (!loop->iterator_post_statement)
858 ret = handle_canonical_while_count_down(loop);
859 else
860 ret = handle_canonical_for_loops(loop);
861 *stree = __pop_fake_cur_stree();
862 return ret;
865 int __iterator_unchanged(struct sm_state *sm)
867 if (!sm)
868 return 0;
869 if (get_sm_state(my_id, sm->name, sm->sym) == sm)
870 return 1;
871 return 0;
874 static void while_count_down_after(struct sm_state *sm, struct expression *condition)
876 struct expression *unop;
877 int op;
878 sval_t limit, after_value;
880 if (!get_countdown_info(condition, &unop, &op, &limit))
881 return;
882 after_value = estate_min(sm->state);
883 after_value.value--;
884 set_extra_mod(sm->name, sm->sym, condition->unop, alloc_estate_sval(after_value));
887 void __extra_pre_loop_hook_after(struct sm_state *sm,
888 struct statement *iterator,
889 struct expression *condition)
891 struct expression *iter_expr;
892 sval_t limit;
893 struct smatch_state *state;
894 sval_t end;
896 if (!iterator) {
897 while_count_down_after(sm, condition);
898 return;
901 iter_expr = iterator->expression;
903 if (condition->type != EXPR_COMPARE)
904 return;
906 if (iter_expr->op == SPECIAL_INCREMENT) {
907 if (!get_implied_value(condition->right, &end) &&
908 sval_is_max(estate_max(sm->state)))
909 limit = estate_max(sm->state);
910 else
911 limit = sval_binop(estate_max(sm->state), '+',
912 sval_type_val(estate_type(sm->state), 1));
913 } else {
914 limit = sval_binop(estate_min(sm->state), '-',
915 sval_type_val(estate_type(sm->state), 1));
917 limit = sval_cast(estate_type(sm->state), limit);
918 if (!estate_has_hard_max(sm->state) && !__has_breaks()) {
919 if (iter_expr->op == SPECIAL_INCREMENT)
920 state = alloc_estate_range(estate_min(sm->state), limit);
921 else
922 state = alloc_estate_range(limit, estate_max(sm->state));
923 } else {
924 state = alloc_estate_sval(limit);
926 if (!estate_has_hard_max(sm->state)) {
927 estate_clear_hard_max(state);
929 if (estate_has_fuzzy_max(sm->state)) {
930 sval_t hmax = estate_get_fuzzy_max(sm->state);
931 sval_t max = estate_max(sm->state);
933 if (sval_cmp(hmax, max) != 0)
934 estate_clear_fuzzy_max(state);
935 } else if (!estate_has_fuzzy_max(sm->state)) {
936 estate_clear_fuzzy_max(state);
939 set_extra_mod(sm->name, sm->sym, iter_expr, state);
942 static bool get_global_rl(const char *name, struct symbol *sym, struct range_list **rl)
944 struct expression *expr;
946 if (!sym || !(sym->ctype.modifiers & MOD_TOPLEVEL) || !sym->ident)
947 return false;
948 if (strcmp(sym->ident->name, name) != 0)
949 return false;
951 expr = symbol_expression(sym);
952 return get_implied_rl(expr, rl);
955 static struct stree *unmatched_stree;
956 static struct smatch_state *unmatched_state(struct sm_state *sm)
958 struct smatch_state *state;
959 struct range_list *rl;
961 if (unmatched_stree) {
962 state = get_state_stree(unmatched_stree, SMATCH_EXTRA, sm->name, sm->sym);
963 if (state)
964 return state;
966 if (parent_is_gone_var_sym(sm->name, sm->sym))
967 return alloc_estate_empty();
968 if (get_global_rl(sm->name, sm->sym, &rl))
969 return alloc_estate_rl(rl);
970 return alloc_estate_whole(estate_type(sm->state));
973 static void clear_the_pointed_at(struct expression *expr)
975 struct stree *stree;
976 char *name;
977 struct symbol *sym;
978 struct sm_state *tmp;
980 name = expr_to_var_sym(expr, &sym);
981 if (!name || !sym)
982 goto free;
984 stree = __get_cur_stree();
985 FOR_EACH_MY_SM(SMATCH_EXTRA, stree, tmp) {
986 if (tmp->name[0] != '*')
987 continue;
988 if (tmp->sym != sym)
989 continue;
990 if (strcmp(tmp->name + 1, name) != 0)
991 continue;
992 set_extra_mod(tmp->name, tmp->sym, expr, alloc_estate_whole(estate_type(tmp->state)));
993 } END_FOR_EACH_SM(tmp);
995 free:
996 free_string(name);
999 static int is_const_param(struct expression *expr, int param)
1001 struct symbol *type;
1003 type = get_arg_type(expr, param);
1004 if (!type)
1005 return 0;
1006 if (type->ctype.modifiers & MOD_CONST)
1007 return 1;
1008 return 0;
1011 static void match_function_call(struct expression *expr)
1013 struct expression *arg;
1014 struct expression *tmp;
1015 int param = -1;
1017 /* if we have the db this is handled in smatch_function_hooks.c */
1018 if (!option_no_db)
1019 return;
1020 if (inlinable(expr->fn))
1021 return;
1023 FOR_EACH_PTR(expr->args, arg) {
1024 param++;
1025 if (is_const_param(expr->fn, param))
1026 continue;
1027 tmp = strip_expr(arg);
1028 if (tmp->type == EXPR_PREOP && tmp->op == '&')
1029 set_extra_expr_mod(tmp->unop, alloc_estate_whole(get_type(tmp->unop)));
1030 else
1031 clear_the_pointed_at(tmp);
1032 } END_FOR_EACH_PTR(arg);
1035 int values_fit_type(struct expression *left, struct expression *right)
1037 struct range_list *rl;
1038 struct symbol *type;
1040 type = get_type(left);
1041 if (!type)
1042 return 0;
1043 get_absolute_rl(right, &rl);
1044 if (type == rl_type(rl))
1045 return 1;
1046 if (type_unsigned(type) && sval_is_negative(rl_min(rl)))
1047 return 0;
1048 if (sval_cmp(sval_type_min(type), rl_min(rl)) > 0)
1049 return 0;
1050 if (sval_cmp(sval_type_max(type), rl_max(rl)) < 0)
1051 return 0;
1052 return 1;
1055 static void save_chunk_info(struct expression *left, struct expression *right)
1057 struct var_sym_list *vsl;
1058 struct var_sym *vs;
1059 struct expression *add_expr;
1060 struct symbol *type;
1061 sval_t sval;
1062 char *name;
1063 struct symbol *sym;
1065 if (right->type != EXPR_BINOP || right->op != '-')
1066 return;
1067 if (!get_value(right->left, &sval))
1068 return;
1069 if (!expr_to_sym(right->right))
1070 return;
1072 add_expr = binop_expression(left, '+', right->right);
1073 type = get_type(add_expr);
1074 if (!type)
1075 return;
1076 name = expr_to_chunk_sym_vsl(add_expr, &sym, &vsl);
1077 if (!name || !vsl)
1078 goto free;
1079 FOR_EACH_PTR(vsl, vs) {
1080 store_link(link_id, vs->var, vs->sym, name, sym);
1081 } END_FOR_EACH_PTR(vs);
1083 set_state(SMATCH_EXTRA, name, sym, alloc_estate_sval(sval_cast(type, sval)));
1084 free:
1085 free_string(name);
1088 static void do_array_assign(struct expression *left, int op, struct expression *right)
1090 struct range_list *rl;
1092 if (op == '=') {
1093 get_absolute_rl(right, &rl);
1094 rl = cast_rl(get_type(left), rl);
1095 } else {
1096 rl = alloc_whole_rl(get_type(left));
1099 set_extra_array_mod(left, alloc_estate_rl(rl));
1102 static void match_vanilla_assign(struct expression *left, struct expression *right)
1104 struct range_list *orig_rl = NULL;
1105 struct range_list *rl = NULL;
1106 struct symbol *right_sym;
1107 struct symbol *left_type;
1108 struct symbol *right_type;
1109 char *right_name = NULL;
1110 struct symbol *sym;
1111 char *name;
1112 sval_t sval, max;
1113 struct smatch_state *state;
1114 int comparison;
1116 if (is_struct(left))
1117 return;
1119 save_chunk_info(left, right);
1121 name = expr_to_var_sym(left, &sym);
1122 if (!name) {
1123 if (chunk_has_array(left))
1124 do_array_assign(left, '=', right);
1125 return;
1128 left_type = get_type(left);
1129 right_type = get_type(right);
1131 right_name = expr_to_var_sym(right, &right_sym);
1133 if (!__in_fake_assign && !__in_fake_var_assign &&
1134 !(right->type == EXPR_PREOP && right->op == '&') &&
1135 right_name && right_sym &&
1136 values_fit_type(left, strip_expr(right)) &&
1137 !has_symbol(right, sym)) {
1138 set_equiv(left, right);
1139 goto free;
1142 if (get_implied_value(right, &sval)) {
1143 state = alloc_estate_sval(sval_cast(left_type, sval));
1144 goto done;
1147 if (__in_fake_assign || __in_fake_var_assign) {
1148 struct smatch_state *right_state;
1149 struct range_list *rl;
1151 right_state = get_state(SMATCH_EXTRA, right_name, right_sym);
1152 if (right_state) {
1153 /* simple assignment */
1154 state = clone_estate(right_state);
1155 goto done;
1158 if (get_implied_rl(right, &rl)) {
1159 rl = cast_rl(left_type, rl);
1160 state = alloc_estate_rl(rl);
1161 goto done;
1164 state = alloc_estate_rl(alloc_whole_rl(left_type));
1165 goto done;
1168 comparison = get_comparison_no_extra(left, right);
1169 if (comparison) {
1170 comparison = flip_comparison(comparison);
1171 get_implied_rl(left, &orig_rl);
1174 if (get_implied_rl(right, &rl)) {
1175 rl = cast_rl(left_type, rl);
1176 if (orig_rl)
1177 filter_by_comparison(&rl, comparison, orig_rl);
1178 state = alloc_estate_rl(rl);
1179 if (get_hard_max(right, &max)) {
1180 estate_set_hard_max(state);
1181 estate_set_fuzzy_max(state, max);
1183 } else {
1184 rl = alloc_whole_rl(right_type);
1185 rl = cast_rl(left_type, rl);
1186 if (orig_rl)
1187 filter_by_comparison(&rl, comparison, orig_rl);
1188 state = alloc_estate_rl(rl);
1191 done:
1192 set_extra_mod(name, sym, left, state);
1193 free:
1194 free_string(right_name);
1197 static void match_assign(struct expression *expr)
1199 struct range_list *rl = NULL;
1200 struct expression *left;
1201 struct expression *right;
1202 struct expression *binop_expr;
1203 struct symbol *left_type;
1204 struct symbol *sym;
1205 char *name;
1207 left = strip_expr(expr->left);
1209 right = strip_parens(expr->right);
1210 if (right->type == EXPR_CALL && sym_name_is("__builtin_expect", right->fn))
1211 right = get_argument_from_call_expr(right->args, 0);
1212 while (right->type == EXPR_ASSIGNMENT && right->op == '=')
1213 right = strip_parens(right->left);
1215 if (expr->op == '=' && is_condition(expr->right))
1216 return; /* handled in smatch_condition.c */
1217 if (expr->op == '=' && right->type == EXPR_CALL)
1218 return; /* handled in smatch_function_hooks.c */
1219 if (expr->op == '=') {
1220 match_vanilla_assign(left, right);
1221 return;
1224 name = expr_to_var_sym(left, &sym);
1225 if (!name)
1226 return;
1228 left_type = get_type(left);
1230 switch (expr->op) {
1231 case SPECIAL_ADD_ASSIGN:
1232 case SPECIAL_SUB_ASSIGN:
1233 case SPECIAL_AND_ASSIGN:
1234 case SPECIAL_MOD_ASSIGN:
1235 case SPECIAL_SHL_ASSIGN:
1236 case SPECIAL_SHR_ASSIGN:
1237 case SPECIAL_OR_ASSIGN:
1238 case SPECIAL_XOR_ASSIGN:
1239 case SPECIAL_MUL_ASSIGN:
1240 case SPECIAL_DIV_ASSIGN:
1241 binop_expr = binop_expression(expr->left,
1242 op_remove_assign(expr->op),
1243 expr->right);
1244 get_absolute_rl(binop_expr, &rl);
1245 rl = cast_rl(left_type, rl);
1246 if (inside_loop()) {
1247 if (expr->op == SPECIAL_ADD_ASSIGN)
1248 add_range(&rl, rl_max(rl), sval_type_max(rl_type(rl)));
1250 if (expr->op == SPECIAL_SUB_ASSIGN &&
1251 !sval_is_negative(rl_min(rl))) {
1252 sval_t zero = { .type = rl_type(rl) };
1254 add_range(&rl, rl_min(rl), zero);
1257 set_extra_mod(name, sym, left, alloc_estate_rl(rl));
1258 goto free;
1260 set_extra_mod(name, sym, left, alloc_estate_whole(left_type));
1261 free:
1262 free_string(name);
1265 static struct smatch_state *increment_state(struct smatch_state *state)
1267 sval_t min = estate_min(state);
1268 sval_t max = estate_max(state);
1270 if (!estate_rl(state))
1271 return NULL;
1273 if (inside_loop())
1274 max = sval_type_max(max.type);
1276 if (!sval_is_min(min) && !sval_is_max(min))
1277 min.value++;
1278 if (!sval_is_min(max) && !sval_is_max(max))
1279 max.value++;
1280 return alloc_estate_range(min, max);
1283 static struct smatch_state *decrement_state(struct smatch_state *state)
1285 sval_t min = estate_min(state);
1286 sval_t max = estate_max(state);
1288 if (!estate_rl(state))
1289 return NULL;
1291 if (inside_loop())
1292 min = sval_type_min(min.type);
1294 if (!sval_is_min(min) && !sval_is_max(min))
1295 min.value--;
1296 if (!sval_is_min(max) && !sval_is_max(max))
1297 max.value--;
1298 return alloc_estate_range(min, max);
1301 static void clear_pointed_at_state(struct expression *expr)
1303 struct symbol *type;
1306 * ALERT: This is sort of a mess. If it's is a struct assigment like
1307 * "foo = bar;", then that's handled by smatch_struct_assignment.c.
1308 * the same thing for p++ where "p" is a struct. Most modifications
1309 * are handled by the assignment hook or the db. Smatch_extra.c doesn't
1310 * use smatch_modification.c because we have to get the ordering right
1311 * or something. So if you have p++ where p is a pointer to a standard
1312 * c type then we handle that here. What a mess.
1314 expr = strip_expr(expr);
1315 type = get_type(expr);
1316 if (!type || type->type != SYM_PTR)
1317 return;
1318 type = get_real_base_type(type);
1319 if (!type || type->type != SYM_BASETYPE)
1320 return;
1321 set_extra_expr_nomod(deref_expression(expr), alloc_estate_whole(type));
1324 static void unop_expr(struct expression *expr)
1326 struct smatch_state *state;
1328 if (expr->smatch_flags & Handled)
1329 return;
1331 switch (expr->op) {
1332 case SPECIAL_INCREMENT:
1333 state = get_state_expr(SMATCH_EXTRA, expr->unop);
1334 state = increment_state(state);
1335 if (!state)
1336 state = alloc_estate_whole(get_type(expr));
1337 set_extra_expr_mod(expr->unop, state);
1338 clear_pointed_at_state(expr->unop);
1339 break;
1340 case SPECIAL_DECREMENT:
1341 state = get_state_expr(SMATCH_EXTRA, expr->unop);
1342 state = decrement_state(state);
1343 if (!state)
1344 state = alloc_estate_whole(get_type(expr));
1345 set_extra_expr_mod(expr->unop, state);
1346 clear_pointed_at_state(expr->unop);
1347 break;
1348 default:
1349 return;
1353 static void asm_expr(struct statement *stmt)
1355 struct asm_operand *op;
1356 struct symbol *type;
1358 FOR_EACH_PTR(stmt->asm_outputs, op) {
1359 type = get_type(strip_expr(op->expr));
1360 set_extra_expr_mod(op->expr, alloc_estate_whole(type));
1361 } END_FOR_EACH_PTR(op);
1364 static void check_dereference(struct expression *expr)
1366 struct smatch_state *state;
1368 if (__in_fake_assign)
1369 return;
1370 if (outside_of_function())
1371 return;
1372 state = get_extra_state(expr);
1373 if (state) {
1374 struct range_list *rl;
1376 rl = rl_intersection(estate_rl(state), valid_ptr_rl);
1377 if (rl_equiv(rl, estate_rl(state)))
1378 return;
1379 set_extra_expr_nomod(expr, alloc_estate_rl(rl));
1380 } else {
1381 struct range_list *rl;
1383 if (get_mtag_rl(expr, &rl))
1384 rl = rl_intersection(rl, valid_ptr_rl);
1385 else
1386 rl = clone_rl(valid_ptr_rl);
1388 set_extra_expr_nomod(expr, alloc_estate_rl(rl));
1392 static void match_dereferences(struct expression *expr)
1394 if (expr->type != EXPR_PREOP)
1395 return;
1396 if (getting_address(expr))
1397 return;
1398 /* it's saying that foo[1] = bar dereferences foo[1] */
1399 if (is_array(expr))
1400 return;
1401 check_dereference(expr->unop);
1404 static void match_pointer_as_array(struct expression *expr)
1406 if (!is_array(expr))
1407 return;
1408 check_dereference(get_array_base(expr));
1411 static void find_dereferences(struct expression *expr)
1413 while (expr->type == EXPR_PREOP) {
1414 if (expr->op == '*')
1415 check_dereference(expr->unop);
1416 expr = strip_expr(expr->unop);
1420 static void set_param_dereferenced(struct expression *call, struct expression *arg, char *key, char *unused)
1422 struct symbol *sym;
1423 char *name;
1425 name = get_variable_from_key(arg, key, &sym);
1426 if (name && sym) {
1427 struct smatch_state *orig, *new;
1428 struct range_list *rl;
1430 orig = get_state(SMATCH_EXTRA, name, sym);
1431 if (orig) {
1432 rl = rl_intersection(estate_rl(orig),
1433 alloc_rl(valid_ptr_min_sval,
1434 valid_ptr_max_sval));
1435 new = alloc_estate_rl(rl);
1436 } else {
1437 new = alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval);
1440 set_extra_nomod(name, sym, NULL, new);
1442 free_string(name);
1444 find_dereferences(arg);
1447 static sval_t add_one(sval_t sval)
1449 sval.value++;
1450 return sval;
1453 static int handle_postop_inc(struct expression *left, int op, struct expression *right)
1455 struct statement *stmt;
1456 struct expression *cond;
1457 struct smatch_state *true_state, *false_state;
1458 struct symbol *type;
1459 sval_t start;
1460 sval_t limit;
1463 * If we're decrementing here then that's a canonical while count down
1464 * so it's handled already. We're only handling loops like:
1465 * i = 0;
1466 * do { ... } while (i++ < 3);
1469 if (left->type != EXPR_POSTOP || left->op != SPECIAL_INCREMENT)
1470 return 0;
1472 stmt = __cur_stmt->parent;
1473 if (!stmt)
1474 return 0;
1475 if (stmt->type == STMT_COMPOUND)
1476 stmt = stmt->parent;
1477 if (!stmt || stmt->type != STMT_ITERATOR || !stmt->iterator_post_condition)
1478 return 0;
1480 cond = strip_expr(stmt->iterator_post_condition);
1481 if (cond->type != EXPR_COMPARE || cond->op != op)
1482 return 0;
1483 if (left != strip_expr(cond->left) || right != strip_expr(cond->right))
1484 return 0;
1486 if (!get_implied_value(left->unop, &start))
1487 return 0;
1488 if (!get_implied_value(right, &limit))
1489 return 0;
1490 type = get_type(left->unop);
1491 limit = sval_cast(type, limit);
1492 if (sval_cmp(start, limit) > 0)
1493 return 0;
1495 switch (op) {
1496 case '<':
1497 case SPECIAL_UNSIGNED_LT:
1498 break;
1499 case SPECIAL_LTE:
1500 case SPECIAL_UNSIGNED_LTE:
1501 limit = add_one(limit);
1502 default:
1503 return 0;
1507 true_state = alloc_estate_range(add_one(start), limit);
1508 false_state = alloc_estate_range(add_one(limit), add_one(limit));
1510 /* Currently we just discard the false state but when two passes is
1511 * implimented correctly then it will use it.
1514 set_extra_expr_true_false(left->unop, true_state, false_state);
1516 return 1;
1519 bool is_impossible_variable(struct expression *expr)
1521 struct smatch_state *state;
1523 state = get_extra_state(expr);
1524 if (state && !estate_rl(state))
1525 return true;
1526 return false;
1529 static bool in_macro(struct expression *left, struct expression *right)
1531 if (!left || !right)
1532 return 0;
1533 if (left->pos.line != right->pos.line || left->pos.pos != right->pos.pos)
1534 return 0;
1535 if (get_macro_name(left->pos))
1536 return 1;
1537 return 0;
1540 static void handle_comparison(struct symbol *type, struct expression *left, int op, struct expression *right)
1542 struct range_list *left_orig;
1543 struct range_list *left_true;
1544 struct range_list *left_false;
1545 struct range_list *right_orig;
1546 struct range_list *right_true;
1547 struct range_list *right_false;
1548 struct smatch_state *left_true_state;
1549 struct smatch_state *left_false_state;
1550 struct smatch_state *right_true_state;
1551 struct smatch_state *right_false_state;
1552 sval_t dummy, hard_max;
1553 int left_postop = 0;
1554 int right_postop = 0;
1556 if (left->op == SPECIAL_INCREMENT || left->op == SPECIAL_DECREMENT) {
1557 if (left->type == EXPR_POSTOP) {
1558 left->smatch_flags |= Handled;
1559 left_postop = left->op;
1560 if (handle_postop_inc(left, op, right))
1561 return;
1563 left = strip_parens(left->unop);
1565 while (left->type == EXPR_ASSIGNMENT)
1566 left = strip_parens(left->left);
1568 if (right->op == SPECIAL_INCREMENT || right->op == SPECIAL_DECREMENT) {
1569 if (right->type == EXPR_POSTOP) {
1570 right->smatch_flags |= Handled;
1571 right_postop = right->op;
1573 right = strip_parens(right->unop);
1576 if (is_impossible_variable(left) || is_impossible_variable(right))
1577 return;
1579 get_real_absolute_rl(left, &left_orig);
1580 left_orig = cast_rl(type, left_orig);
1582 get_real_absolute_rl(right, &right_orig);
1583 right_orig = cast_rl(type, right_orig);
1585 split_comparison_rl(left_orig, op, right_orig, &left_true, &left_false, &right_true, &right_false);
1587 left_true = rl_truncate_cast(get_type(strip_expr(left)), left_true);
1588 left_false = rl_truncate_cast(get_type(strip_expr(left)), left_false);
1589 right_true = rl_truncate_cast(get_type(strip_expr(right)), right_true);
1590 right_false = rl_truncate_cast(get_type(strip_expr(right)), right_false);
1592 if (!left_true || !left_false) {
1593 struct range_list *tmp_true, *tmp_false;
1595 split_comparison_rl(alloc_whole_rl(type), op, right_orig, &tmp_true, &tmp_false, NULL, NULL);
1596 tmp_true = rl_truncate_cast(get_type(strip_expr(left)), tmp_true);
1597 tmp_false = rl_truncate_cast(get_type(strip_expr(left)), tmp_false);
1598 if (tmp_true && tmp_false)
1599 __save_imaginary_state(left, tmp_true, tmp_false);
1602 if (!right_true || !right_false) {
1603 struct range_list *tmp_true, *tmp_false;
1605 split_comparison_rl(alloc_whole_rl(type), op, right_orig, NULL, NULL, &tmp_true, &tmp_false);
1606 tmp_true = rl_truncate_cast(get_type(strip_expr(right)), tmp_true);
1607 tmp_false = rl_truncate_cast(get_type(strip_expr(right)), tmp_false);
1608 if (tmp_true && tmp_false)
1609 __save_imaginary_state(right, tmp_true, tmp_false);
1612 left_true_state = alloc_estate_rl(left_true);
1613 left_false_state = alloc_estate_rl(left_false);
1614 right_true_state = alloc_estate_rl(right_true);
1615 right_false_state = alloc_estate_rl(right_false);
1617 switch (op) {
1618 case '<':
1619 case SPECIAL_UNSIGNED_LT:
1620 case SPECIAL_UNSIGNED_LTE:
1621 case SPECIAL_LTE:
1622 if (get_implied_value(right, &dummy) && !in_macro(left, right))
1623 estate_set_hard_max(left_true_state);
1624 if (get_implied_value(left, &dummy) && !in_macro(left, right))
1625 estate_set_hard_max(right_false_state);
1626 break;
1627 case '>':
1628 case SPECIAL_UNSIGNED_GT:
1629 case SPECIAL_UNSIGNED_GTE:
1630 case SPECIAL_GTE:
1631 if (get_implied_value(left, &dummy) && !in_macro(left, right))
1632 estate_set_hard_max(right_true_state);
1633 if (get_implied_value(right, &dummy) && !in_macro(left, right))
1634 estate_set_hard_max(left_false_state);
1635 break;
1638 switch (op) {
1639 case '<':
1640 case SPECIAL_UNSIGNED_LT:
1641 case SPECIAL_UNSIGNED_LTE:
1642 case SPECIAL_LTE:
1643 if (get_hard_max(right, &hard_max)) {
1644 if (op == '<' || op == SPECIAL_UNSIGNED_LT)
1645 hard_max.value--;
1646 estate_set_fuzzy_max(left_true_state, hard_max);
1648 if (get_implied_value(right, &hard_max)) {
1649 if (op == SPECIAL_UNSIGNED_LTE ||
1650 op == SPECIAL_LTE)
1651 hard_max.value++;
1652 estate_set_fuzzy_max(left_false_state, hard_max);
1654 if (get_hard_max(left, &hard_max)) {
1655 if (op == SPECIAL_UNSIGNED_LTE ||
1656 op == SPECIAL_LTE)
1657 hard_max.value--;
1658 estate_set_fuzzy_max(right_false_state, hard_max);
1660 if (get_implied_value(left, &hard_max)) {
1661 if (op == '<' || op == SPECIAL_UNSIGNED_LT)
1662 hard_max.value++;
1663 estate_set_fuzzy_max(right_true_state, hard_max);
1665 break;
1666 case '>':
1667 case SPECIAL_UNSIGNED_GT:
1668 case SPECIAL_UNSIGNED_GTE:
1669 case SPECIAL_GTE:
1670 if (get_hard_max(left, &hard_max)) {
1671 if (op == '>' || op == SPECIAL_UNSIGNED_GT)
1672 hard_max.value--;
1673 estate_set_fuzzy_max(right_true_state, hard_max);
1675 if (get_implied_value(left, &hard_max)) {
1676 if (op == SPECIAL_UNSIGNED_GTE ||
1677 op == SPECIAL_GTE)
1678 hard_max.value++;
1679 estate_set_fuzzy_max(right_false_state, hard_max);
1681 if (get_hard_max(right, &hard_max)) {
1682 if (op == SPECIAL_UNSIGNED_LTE ||
1683 op == SPECIAL_LTE)
1684 hard_max.value--;
1685 estate_set_fuzzy_max(left_false_state, hard_max);
1687 if (get_implied_value(right, &hard_max)) {
1688 if (op == '>' ||
1689 op == SPECIAL_UNSIGNED_GT)
1690 hard_max.value++;
1691 estate_set_fuzzy_max(left_true_state, hard_max);
1693 break;
1694 case SPECIAL_EQUAL:
1695 if (get_hard_max(left, &hard_max))
1696 estate_set_fuzzy_max(right_true_state, hard_max);
1697 if (get_hard_max(right, &hard_max))
1698 estate_set_fuzzy_max(left_true_state, hard_max);
1699 break;
1702 if (get_hard_max(left, &hard_max)) {
1703 estate_set_hard_max(left_true_state);
1704 estate_set_hard_max(left_false_state);
1706 if (get_hard_max(right, &hard_max)) {
1707 estate_set_hard_max(right_true_state);
1708 estate_set_hard_max(right_false_state);
1711 if (left_postop == SPECIAL_INCREMENT) {
1712 left_true_state = increment_state(left_true_state);
1713 left_false_state = increment_state(left_false_state);
1715 if (left_postop == SPECIAL_DECREMENT) {
1716 left_true_state = decrement_state(left_true_state);
1717 left_false_state = decrement_state(left_false_state);
1719 if (right_postop == SPECIAL_INCREMENT) {
1720 right_true_state = increment_state(right_true_state);
1721 right_false_state = increment_state(right_false_state);
1723 if (right_postop == SPECIAL_DECREMENT) {
1724 right_true_state = decrement_state(right_true_state);
1725 right_false_state = decrement_state(right_false_state);
1728 if (estate_rl(left_true_state) && estates_equiv(left_true_state, left_false_state)) {
1729 left_true_state = NULL;
1730 left_false_state = NULL;
1733 if (estate_rl(right_true_state) && estates_equiv(right_true_state, right_false_state)) {
1734 right_true_state = NULL;
1735 right_false_state = NULL;
1738 /* Don't introduce new states for known true/false conditions */
1739 if (rl_equiv(left_orig, estate_rl(left_true_state)))
1740 left_true_state = NULL;
1741 if (rl_equiv(left_orig, estate_rl(left_false_state)))
1742 left_false_state = NULL;
1743 if (rl_equiv(right_orig, estate_rl(right_true_state)))
1744 right_true_state = NULL;
1745 if (rl_equiv(right_orig, estate_rl(right_false_state)))
1746 right_false_state = NULL;
1748 set_extra_expr_true_false(left, left_true_state, left_false_state);
1749 set_extra_expr_true_false(right, right_true_state, right_false_state);
1752 static int is_simple_math(struct expression *expr)
1754 if (!expr)
1755 return 0;
1756 if (expr->type != EXPR_BINOP)
1757 return 0;
1758 switch (expr->op) {
1759 case '+':
1760 case '-':
1761 case '*':
1762 return 1;
1764 return 0;
1767 static int flip_op(int op)
1769 /* We only care about simple math */
1770 switch (op) {
1771 case '+':
1772 return '-';
1773 case '-':
1774 return '+';
1775 case '*':
1776 return '/';
1778 return 0;
1781 static void move_known_to_rl(struct expression **expr_p, struct range_list **rl_p)
1783 struct expression *expr = *expr_p;
1784 struct range_list *rl = *rl_p;
1785 sval_t sval;
1787 if (!is_simple_math(expr))
1788 return;
1790 if (get_implied_value(expr->right, &sval)) {
1791 *expr_p = expr->left;
1792 *rl_p = rl_binop(rl, flip_op(expr->op), alloc_rl(sval, sval));
1793 move_known_to_rl(expr_p, rl_p);
1794 return;
1796 if (expr->op == '-')
1797 return;
1798 if (get_implied_value(expr->left, &sval)) {
1799 *expr_p = expr->right;
1800 *rl_p = rl_binop(rl, flip_op(expr->op), alloc_rl(sval, sval));
1801 move_known_to_rl(expr_p, rl_p);
1802 return;
1806 static void move_known_values(struct expression **left_p, struct expression **right_p)
1808 struct expression *left = *left_p;
1809 struct expression *right = *right_p;
1810 sval_t sval, dummy;
1812 if (get_implied_value(left, &sval)) {
1813 if (!is_simple_math(right))
1814 return;
1815 if (get_implied_value(right, &dummy))
1816 return;
1817 if (right->op == '*') {
1818 sval_t divisor;
1820 if (!get_value(right->right, &divisor))
1821 return;
1822 if (divisor.value == 0)
1823 return;
1824 *left_p = binop_expression(left, invert_op(right->op), right->right);
1825 *right_p = right->left;
1826 return;
1828 if (right->op == '+' && get_value(right->left, &sval)) {
1829 *left_p = binop_expression(left, invert_op(right->op), right->left);
1830 *right_p = right->right;
1831 return;
1833 if (get_value(right->right, &sval)) {
1834 *left_p = binop_expression(left, invert_op(right->op), right->right);
1835 *right_p = right->left;
1836 return;
1838 return;
1840 if (get_implied_value(right, &sval)) {
1841 if (!is_simple_math(left))
1842 return;
1843 if (get_implied_value(left, &dummy))
1844 return;
1845 if (left->op == '*') {
1846 sval_t divisor;
1848 if (!get_value(left->right, &divisor))
1849 return;
1850 if (divisor.value == 0)
1851 return;
1852 *right_p = binop_expression(right, invert_op(left->op), left->right);
1853 *left_p = left->left;
1854 return;
1856 if (left->op == '+' && get_value(left->left, &sval)) {
1857 *right_p = binop_expression(right, invert_op(left->op), left->left);
1858 *left_p = left->right;
1859 return;
1862 if (get_value(left->right, &sval)) {
1863 *right_p = binop_expression(right, invert_op(left->op), left->right);
1864 *left_p = left->left;
1865 return;
1867 return;
1872 * The reason for do_simple_algebra() is to solve things like:
1873 * if (foo > 66 || foo + bar > 64) {
1874 * "foo" is not really a known variable so it won't be handled by
1875 * move_known_variables() but it's a super common idiom.
1878 static int do_simple_algebra(struct expression **left_p, struct expression **right_p)
1880 struct expression *left = *left_p;
1881 struct expression *right = *right_p;
1882 struct range_list *rl;
1883 sval_t tmp;
1885 if (left->type != EXPR_BINOP || left->op != '+')
1886 return 0;
1887 if (can_integer_overflow(get_type(left), left))
1888 return 0;
1889 if (!get_implied_value(right, &tmp))
1890 return 0;
1892 if (!get_implied_value(left->left, &tmp) &&
1893 get_implied_rl(left->left, &rl) &&
1894 !is_whole_rl(rl)) {
1895 *right_p = binop_expression(right, '-', left->left);
1896 *left_p = left->right;
1897 return 1;
1899 if (!get_implied_value(left->right, &tmp) &&
1900 get_implied_rl(left->right, &rl) &&
1901 !is_whole_rl(rl)) {
1902 *right_p = binop_expression(right, '-', left->right);
1903 *left_p = left->left;
1904 return 1;
1907 return 0;
1910 static int match_func_comparison(struct expression *expr)
1912 struct expression *left = strip_expr(expr->left);
1913 struct expression *right = strip_expr(expr->right);
1915 if (left->type == EXPR_CALL || right->type == EXPR_CALL) {
1916 function_comparison(left, expr->op, right);
1917 return 1;
1920 return 0;
1923 /* Handle conditions like "if (foo + bar < foo) {" */
1924 static int handle_integer_overflow_test(struct expression *expr)
1926 struct expression *left, *right;
1927 struct symbol *type;
1928 sval_t left_min, right_min, min, max;
1930 if (expr->op != '<' && expr->op != SPECIAL_UNSIGNED_LT)
1931 return 0;
1933 left = strip_parens(expr->left);
1934 right = strip_parens(expr->right);
1936 if (left->op != '+')
1937 return 0;
1939 type = get_type(expr);
1940 if (!type)
1941 return 0;
1942 if (type_positive_bits(type) == 32) {
1943 max.type = &uint_ctype;
1944 max.uvalue = (unsigned int)-1;
1945 } else if (type_positive_bits(type) == 64) {
1946 max.type = &ulong_ctype;
1947 max.value = (unsigned long long)-1;
1948 } else {
1949 return 0;
1952 if (!expr_equiv(left->left, right) && !expr_equiv(left->right, right))
1953 return 0;
1955 get_absolute_min(left->left, &left_min);
1956 get_absolute_min(left->right, &right_min);
1957 min = sval_binop(left_min, '+', right_min);
1959 type = get_type(left);
1960 min = sval_cast(type, min);
1961 max = sval_cast(type, max);
1963 set_extra_chunk_true_false(left, NULL, alloc_estate_range(min, max));
1964 return 1;
1967 static void match_comparison(struct expression *expr)
1969 struct expression *left_orig = strip_parens(expr->left);
1970 struct expression *right_orig = strip_parens(expr->right);
1971 struct expression *left, *right, *tmp;
1972 struct expression *prev;
1973 struct symbol *type;
1974 int redo, count;
1976 if (match_func_comparison(expr))
1977 return;
1979 type = get_type(expr);
1980 if (!type)
1981 type = &llong_ctype;
1983 if (handle_integer_overflow_test(expr))
1984 return;
1986 left = left_orig;
1987 right = right_orig;
1988 move_known_values(&left, &right);
1989 handle_comparison(type, left, expr->op, right);
1991 left = left_orig;
1992 right = right_orig;
1993 if (do_simple_algebra(&left, &right))
1994 handle_comparison(type, left, expr->op, right);
1996 prev = get_assigned_expr(left_orig);
1997 if (is_simple_math(prev) && !has_variable(prev, left_orig)) {
1998 left = prev;
1999 right = right_orig;
2000 move_known_values(&left, &right);
2001 handle_comparison(type, left, expr->op, right);
2004 prev = get_assigned_expr(right_orig);
2005 if (is_simple_math(prev) && !has_variable(prev, right_orig)) {
2006 left = left_orig;
2007 right = prev;
2008 move_known_values(&left, &right);
2009 handle_comparison(type, left, expr->op, right);
2012 redo = 0;
2013 left = left_orig;
2014 right = right_orig;
2015 if (get_last_expr_from_expression_stmt(left_orig)) {
2016 left = get_last_expr_from_expression_stmt(left_orig);
2017 redo = 1;
2019 if (get_last_expr_from_expression_stmt(right_orig)) {
2020 right = get_last_expr_from_expression_stmt(right_orig);
2021 redo = 1;
2024 if (!redo)
2025 return;
2027 count = 0;
2028 while ((tmp = get_assigned_expr(left))) {
2029 if (count++ > 3)
2030 break;
2031 left = strip_expr(tmp);
2033 count = 0;
2034 while ((tmp = get_assigned_expr(right))) {
2035 if (count++ > 3)
2036 break;
2037 right = strip_expr(tmp);
2040 handle_comparison(type, left, expr->op, right);
2043 static sval_t get_high_mask(sval_t known)
2045 sval_t ret;
2046 int i;
2048 ret = known;
2049 ret.value = 0;
2051 for (i = type_bits(known.type) - 1; i >= 0; i--) {
2052 if (known.uvalue & (1ULL << i))
2053 ret.uvalue |= (1ULL << i);
2054 else
2055 return ret;
2058 return ret;
2061 static bool handle_bit_test(struct expression *expr)
2063 struct range_list *orig_rl, *rl;
2064 struct expression *shift, *mask, *var;
2065 struct bit_info *bit_info;
2066 sval_t sval;
2067 sval_t high = { .type = &int_ctype };
2068 sval_t low = { .type = &int_ctype };
2070 shift = strip_expr(expr->right);
2071 mask = strip_expr(expr->left);
2072 if (shift->type != EXPR_BINOP || shift->op != SPECIAL_LEFTSHIFT) {
2073 shift = strip_expr(expr->left);
2074 mask = strip_expr(expr->right);
2075 if (shift->type != EXPR_BINOP || shift->op != SPECIAL_LEFTSHIFT)
2076 return false;
2078 if (!get_implied_value(shift->left, &sval) || sval.value != 1)
2079 return false;
2080 var = strip_expr(shift->right);
2082 bit_info = get_bit_info(mask);
2083 if (!bit_info)
2084 return false;
2085 if (!bit_info->possible)
2086 return false;
2088 get_absolute_rl(var, &orig_rl);
2089 if (sval_is_negative(rl_min(orig_rl)) ||
2090 rl_max(orig_rl).uvalue > type_bits(get_type(shift->left)))
2091 return false;
2093 low.value = ffsll(bit_info->possible);
2094 high.value = sm_fls64(bit_info->possible);
2095 rl = alloc_rl(low, high);
2096 rl = cast_rl(get_type(var), rl);
2097 rl = rl_intersection(orig_rl, rl);
2098 if (!rl)
2099 return false;
2101 set_extra_expr_true_false(shift->right, alloc_estate_rl(rl), NULL);
2103 return true;
2106 static void handle_AND_op(struct expression *var, sval_t known)
2108 struct range_list *orig_rl;
2109 struct range_list *true_rl = NULL;
2110 struct range_list *false_rl = NULL;
2111 int bit;
2112 sval_t low_mask = known;
2113 sval_t high_mask;
2114 sval_t max;
2116 get_absolute_rl(var, &orig_rl);
2118 if (known.value > 0) {
2119 bit = ffsll(known.value) - 1;
2120 low_mask.uvalue = (1ULL << bit) - 1;
2121 true_rl = remove_range(orig_rl, sval_type_val(known.type, 0), low_mask);
2123 high_mask = get_high_mask(known);
2124 if (high_mask.value) {
2125 bit = ffsll(high_mask.value) - 1;
2126 low_mask.uvalue = (1ULL << bit) - 1;
2128 false_rl = orig_rl;
2129 if (sval_is_negative(rl_min(orig_rl)))
2130 false_rl = remove_range(false_rl, sval_type_min(known.type), sval_type_val(known.type, -1));
2131 false_rl = remove_range(false_rl, low_mask, sval_type_max(known.type));
2132 if (type_signed(high_mask.type) && type_unsigned(rl_type(false_rl))) {
2133 false_rl = remove_range(false_rl,
2134 sval_type_val(rl_type(false_rl), sval_type_max(known.type).uvalue),
2135 sval_type_val(rl_type(false_rl), -1));
2137 } else if (known.value == 1 &&
2138 get_hard_max(var, &max) &&
2139 sval_cmp(max, rl_max(orig_rl)) == 0 &&
2140 max.value & 1) {
2141 false_rl = remove_range(orig_rl, max, max);
2143 set_extra_expr_true_false(var,
2144 true_rl ? alloc_estate_rl(true_rl) : NULL,
2145 false_rl ? alloc_estate_rl(false_rl) : NULL);
2148 static void handle_AND_condition(struct expression *expr)
2150 sval_t known;
2152 if (handle_bit_test(expr))
2153 return;
2155 if (get_implied_value(expr->left, &known))
2156 handle_AND_op(expr->right, known);
2157 else if (get_implied_value(expr->right, &known))
2158 handle_AND_op(expr->left, known);
2161 static void handle_MOD_condition(struct expression *expr)
2163 struct range_list *orig_rl;
2164 struct range_list *true_rl;
2165 struct range_list *false_rl = NULL;
2166 sval_t right;
2167 sval_t zero = {
2168 .value = 0,
2171 if (!get_implied_value(expr->right, &right) || right.value == 0)
2172 return;
2173 get_absolute_rl(expr->left, &orig_rl);
2175 zero.type = rl_type(orig_rl);
2177 /* We're basically dorking around the min and max here */
2178 true_rl = remove_range(orig_rl, zero, zero);
2179 if (!sval_is_max(rl_max(true_rl)) &&
2180 !(rl_max(true_rl).value % right.value))
2181 true_rl = remove_range(true_rl, rl_max(true_rl), rl_max(true_rl));
2183 if (rl_equiv(true_rl, orig_rl))
2184 true_rl = NULL;
2186 if (sval_is_positive(rl_min(orig_rl)) &&
2187 (rl_max(orig_rl).value - rl_min(orig_rl).value) / right.value < 5) {
2188 sval_t add;
2189 int i;
2191 add = rl_min(orig_rl);
2192 add.value += right.value - (add.value % right.value);
2193 add.value -= right.value;
2195 for (i = 0; i < 5; i++) {
2196 add.value += right.value;
2197 if (add.value > rl_max(orig_rl).value)
2198 break;
2199 add_range(&false_rl, add, add);
2201 } else {
2202 if (rl_min(orig_rl).uvalue != 0 &&
2203 rl_min(orig_rl).uvalue < right.uvalue) {
2204 sval_t chop = right;
2205 chop.value--;
2206 false_rl = remove_range(orig_rl, zero, chop);
2209 if (!sval_is_max(rl_max(orig_rl)) &&
2210 (rl_max(orig_rl).value % right.value)) {
2211 sval_t chop = rl_max(orig_rl);
2212 chop.value -= chop.value % right.value;
2213 chop.value++;
2214 if (!false_rl)
2215 false_rl = clone_rl(orig_rl);
2216 false_rl = remove_range(false_rl, chop, rl_max(orig_rl));
2220 set_extra_expr_true_false(expr->left,
2221 true_rl ? alloc_estate_rl(true_rl) : NULL,
2222 false_rl ? alloc_estate_rl(false_rl) : NULL);
2225 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
2226 void __extra_match_condition(struct expression *expr)
2228 expr = strip_expr(expr);
2229 switch (expr->type) {
2230 case EXPR_CALL:
2231 function_comparison(expr, SPECIAL_NOTEQUAL, zero_expr());
2232 return;
2233 case EXPR_PREOP:
2234 case EXPR_SYMBOL:
2235 case EXPR_DEREF:
2236 handle_comparison(get_type(expr), expr, SPECIAL_NOTEQUAL, zero_expr());
2237 return;
2238 case EXPR_COMPARE:
2239 match_comparison(expr);
2240 return;
2241 case EXPR_ASSIGNMENT:
2242 __extra_match_condition(expr->left);
2243 return;
2244 case EXPR_BINOP:
2245 if (expr->op == '&')
2246 handle_AND_condition(expr);
2247 if (expr->op == '%')
2248 handle_MOD_condition(expr);
2249 return;
2253 static void assume_indexes_are_valid(struct expression *expr)
2255 struct expression *array_expr;
2256 int array_size;
2257 struct expression *offset;
2258 struct symbol *offset_type;
2259 struct range_list *rl_before;
2260 struct range_list *rl_after;
2261 struct range_list *filter = NULL;
2262 sval_t size;
2264 expr = strip_expr(expr);
2265 if (!is_array(expr))
2266 return;
2268 offset = get_array_offset(expr);
2269 offset_type = get_type(offset);
2270 if (offset_type && type_signed(offset_type)) {
2271 filter = alloc_rl(sval_type_min(offset_type),
2272 sval_type_val(offset_type, -1));
2275 array_expr = get_array_base(expr);
2276 array_size = get_real_array_size(array_expr);
2277 if (array_size > 1) {
2278 size = sval_type_val(offset_type, array_size);
2279 add_range(&filter, size, sval_type_max(offset_type));
2282 if (!filter)
2283 return;
2284 get_absolute_rl(offset, &rl_before);
2285 rl_after = rl_filter(rl_before, filter);
2286 if (rl_equiv(rl_before, rl_after))
2287 return;
2288 set_extra_expr_nomod(offset, alloc_estate_rl(rl_after));
2291 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
2292 int implied_not_equal(struct expression *expr, long long val)
2294 return !possibly_false(expr, SPECIAL_NOTEQUAL, value_expr(val));
2297 int implied_not_equal_name_sym(char *name, struct symbol *sym, long long val)
2299 struct smatch_state *estate;
2301 estate = get_state(SMATCH_EXTRA, name, sym);
2302 if (!estate)
2303 return 0;
2304 if (!rl_has_sval(estate_rl(estate), sval_type_val(estate_type(estate), 0)))
2305 return 1;
2306 return 0;
2309 bool is_noderef_ptr(struct expression *expr)
2311 struct range_list *rl;
2313 if (!get_implied_rl(expr, &rl))
2314 return false;
2315 return is_noderef_ptr_rl(rl);
2318 static int parent_is_err_or_null_var_sym_helper(const char *name, struct symbol *sym, bool check_err_ptr)
2320 struct smatch_state *state;
2321 char buf[256];
2322 char *start;
2323 int len;
2325 strncpy(buf, name, sizeof(buf) - 1);
2326 buf[sizeof(buf) - 1] = '\0';
2328 start = &buf[0];
2329 while (*start == '*') {
2330 start++;
2331 state = __get_state(SMATCH_EXTRA, start, sym);
2332 if (!state)
2333 continue;
2334 if (!estate_rl(state))
2335 return 1;
2336 if (is_noderef_ptr_rl(estate_rl(state)))
2337 return 1;
2340 start = &buf[0];
2341 while (*start == '&')
2342 start++;
2344 len = strlen(start);
2345 while (true) {
2346 while (len > 0) {
2347 len--;
2348 if (start[len] == '-' ||
2349 start[len] == '.') {
2350 start[len] = '\0';
2351 break;
2354 if (len == 0)
2355 return 0;
2356 state = __get_state(SMATCH_EXTRA, start, sym);
2357 if (!state)
2358 continue;
2359 if (is_noderef_ptr_rl(estate_rl(state)))
2360 return 1;
2364 int parent_is_null_var_sym(const char *name, struct symbol *sym)
2366 return parent_is_err_or_null_var_sym_helper(name, sym, false);
2369 int parent_is_err_or_null_var_sym(const char *name, struct symbol *sym)
2371 return parent_is_err_or_null_var_sym_helper(name, sym, (option_project == PROJ_KERNEL));
2374 int parent_is_null(struct expression *expr)
2376 struct symbol *sym;
2377 char *var;
2378 int ret = 0;
2380 expr = strip_expr(expr);
2381 var = expr_to_var_sym(expr, &sym);
2382 if (!var || !sym)
2383 goto free;
2384 ret = parent_is_null_var_sym(var, sym);
2385 free:
2386 free_string(var);
2387 return ret;
2390 static int param_used_callback(void *found, int argc, char **argv, char **azColName)
2392 *(int *)found = 1;
2393 return 0;
2396 static int is_kzalloc_info(struct sm_state *sm)
2398 sval_t sval;
2401 * kzalloc() information is treated as special because so there is just
2402 * a lot of stuff initialized to zero and it makes building the database
2403 * take hours and hours.
2405 * In theory, we should just remove this line and not pass any unused
2406 * information, but I'm not sure enough that this code works so I want
2407 * to hold off on that for now.
2409 if (!estate_get_single_value(sm->state, &sval))
2410 return 0;
2411 if (sval.value != 0)
2412 return 0;
2413 return 1;
2416 static int is_really_long(struct sm_state *sm)
2418 const char *p;
2419 int cnt = 0;
2421 p = sm->name;
2422 while ((p = strstr(p, "->"))) {
2423 p += 2;
2424 cnt++;
2427 if (cnt < 3 ||
2428 strlen(sm->name) < 40)
2429 return 0;
2430 return 1;
2433 static int filter_unused_param_value_info(struct expression *call, int param, char *printed_name, struct sm_state *sm)
2435 int found = 0;
2437 /* for function pointers assume everything is used */
2438 if (call->fn->type != EXPR_SYMBOL)
2439 return 0;
2441 if (strcmp(printed_name, "$") == 0 ||
2442 strcmp(printed_name, "*$") == 0)
2443 return 0;
2446 * This is to handle __builtin_mul_overflow(). In an ideal world we
2447 * would only need this for invalid code.
2450 if (!call->fn->symbol)
2451 return 0;
2453 if (!is_kzalloc_info(sm) && !is_really_long(sm))
2454 return 0;
2456 run_sql(&param_used_callback, &found,
2457 "select * from return_implies where %s and type = %d and parameter = %d and key = '%s';",
2458 get_static_filter(call->fn->symbol), PARAM_USED, param, printed_name);
2459 if (found)
2460 return 0;
2462 /* If the database is not built yet, then assume everything is used */
2463 run_sql(&param_used_callback, &found,
2464 "select * from return_implies where %s and type = %d;",
2465 get_static_filter(call->fn->symbol), PARAM_USED);
2466 if (!found)
2467 return 0;
2469 return 1;
2472 struct range_list *intersect_with_real_abs_var_sym(const char *name, struct symbol *sym, struct range_list *start)
2474 struct smatch_state *state;
2477 * Here is the difference between implied value and real absolute, say
2478 * you have:
2480 * int a = (u8)x;
2482 * Then you know that a is 0-255. That's real absolute. But you don't
2483 * know for sure that it actually goes up to 255. So it's not implied.
2484 * Implied indicates a degree of certainty.
2486 * But then say you cap "a" at 8. That means you know it goes up to
2487 * 8. So now the implied value is s32min-8. But you can combine it
2488 * with the real absolute to say that actually it's 0-8.
2490 * We are combining it here. But now that I think about it, this is
2491 * probably not the ideal place to combine it because it should proably
2492 * be done earlier. Oh well, this is an improvement on what was there
2493 * before so I'm going to commit this code.
2497 state = get_real_absolute_state_var_sym(name, sym);
2498 if (!state || !estate_rl(state))
2499 return start;
2501 return rl_intersection(estate_rl(state), start);
2504 struct range_list *intersect_with_real_abs_expr(struct expression *expr, struct range_list *start)
2506 struct smatch_state *state;
2507 struct range_list *abs_rl;
2509 state = get_real_absolute_state(expr);
2510 if (!state || !estate_rl(state))
2511 return start;
2513 abs_rl = cast_rl(rl_type(start), estate_rl(state));
2514 return rl_intersection(abs_rl, start);
2517 static void struct_member_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
2519 struct range_list *rl;
2520 sval_t dummy;
2522 if (estate_is_whole(sm->state) || !estate_rl(sm->state))
2523 return;
2524 if (filter_unused_param_value_info(call, param, printed_name, sm))
2525 return;
2526 rl = estate_rl(sm->state);
2527 rl = intersect_with_real_abs_var_sym(sm->name, sm->sym, rl);
2528 if (!rl)
2529 return;
2530 sql_insert_caller_info(call, PARAM_VALUE, param, printed_name, show_rl(rl));
2531 if (!estate_get_single_value(sm->state, &dummy)) {
2532 if (estate_has_hard_max(sm->state))
2533 sql_insert_caller_info(call, HARD_MAX, param, printed_name,
2534 sval_to_str(estate_max(sm->state)));
2535 if (estate_has_fuzzy_max(sm->state))
2536 sql_insert_caller_info(call, FUZZY_MAX, param, printed_name,
2537 sval_to_str(estate_get_fuzzy_max(sm->state)));
2541 static void returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
2543 struct symbol *returned_sym;
2544 char *returned_name;
2545 struct sm_state *sm;
2546 char *compare_str;
2547 char name_buf[256];
2548 char val_buf[256];
2549 int len;
2551 // FIXME handle *$
2553 if (!is_pointer(expr))
2554 return;
2555 if (return_ranges && strstr(return_ranges, "[==$"))
2556 return;
2558 returned_name = expr_to_var_sym(expr, &returned_sym);
2559 if (!returned_name || !returned_sym)
2560 goto free;
2561 len = strlen(returned_name);
2563 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
2564 if (!estate_rl(sm->state))
2565 continue;
2566 if (returned_sym != sm->sym)
2567 continue;
2568 if (strncmp(returned_name, sm->name, len) != 0)
2569 continue;
2570 if (sm->name[len] != '-')
2571 continue;
2573 snprintf(name_buf, sizeof(name_buf), "$%s", sm->name + len);
2575 compare_str = name_sym_to_param_comparison(sm->name, sm->sym);
2576 if (!compare_str && estate_is_whole(sm->state))
2577 continue;
2578 snprintf(val_buf, sizeof(val_buf), "%s%s", sm->state->name, compare_str ?: "");
2580 sql_insert_return_states(return_id, return_ranges, PARAM_VALUE,
2581 -1, name_buf, val_buf);
2582 } END_FOR_EACH_SM(sm);
2584 free:
2585 free_string(returned_name);
2588 static void db_limited_before(void)
2590 unmatched_stree = clone_stree(__get_cur_stree());
2593 static void db_limited_after(void)
2595 free_stree(&unmatched_stree);
2598 static int basically_the_same(struct range_list *orig, struct range_list *new)
2600 return rl_equiv(orig, new);
2603 static void db_param_limit_binops(struct expression *arg, char *key, struct range_list *rl)
2605 struct range_list *left_rl;
2606 sval_t zero = { .type = rl_type(rl), };
2607 sval_t sval;
2609 if (arg->op != '*')
2610 return;
2611 if (!get_implied_value(arg->right, &sval))
2612 return;
2613 if (can_integer_overflow(get_type(arg), arg))
2614 return;
2616 left_rl = rl_binop(rl, '/', alloc_rl(sval, sval));
2617 if (!rl_has_sval(rl, zero))
2618 left_rl = remove_range(left_rl, zero, zero);
2620 set_extra_expr_nomod(arg->left, alloc_estate_rl(left_rl));
2623 static void db_param_limit_filter(struct expression *expr, int param, char *key, char *value, enum info_type op)
2625 struct expression *arg;
2626 char *name;
2627 struct symbol *sym;
2628 struct var_sym_list *vsl = NULL;
2629 struct sm_state *sm;
2630 struct symbol *compare_type, *var_type;
2631 struct range_list *rl;
2632 struct range_list *limit;
2633 struct range_list *new;
2634 char *other_name;
2635 struct symbol *other_sym;
2637 while (expr->type == EXPR_ASSIGNMENT)
2638 expr = strip_expr(expr->right);
2639 if (expr->type != EXPR_CALL)
2640 return;
2642 arg = get_argument_from_call_expr(expr->args, param);
2643 if (!arg)
2644 return;
2646 if (strcmp(key, "$") == 0)
2647 compare_type = get_arg_type(expr->fn, param);
2648 else
2649 compare_type = get_member_type_from_key(arg, key);
2651 call_results_to_rl(expr, compare_type, value, &limit);
2652 if (strcmp(key, "$") == 0)
2653 move_known_to_rl(&arg, &limit);
2654 name = get_chunk_from_key(arg, key, &sym, &vsl);
2655 if (!name)
2656 return;
2657 if (op != PARAM_LIMIT && !sym)
2658 goto free;
2660 sm = get_sm_state(SMATCH_EXTRA, name, sym);
2661 if (sm)
2662 rl = estate_rl(sm->state);
2663 else
2664 rl = alloc_whole_rl(compare_type);
2666 if (op == PARAM_LIMIT && !rl_fits_in_type(rl, compare_type))
2667 goto free;
2669 new = rl_intersection(rl, limit);
2671 var_type = get_member_type_from_key(arg, key);
2672 new = cast_rl(var_type, new);
2674 /* We want to preserve the implications here */
2675 if (sm && basically_the_same(rl, new))
2676 goto free;
2677 other_name = get_other_name_sym(name, sym, &other_sym);
2679 if (op == PARAM_LIMIT)
2680 set_extra_nomod_vsl(name, sym, vsl, NULL, alloc_estate_rl(new));
2681 else
2682 set_extra_mod(name, sym, NULL, alloc_estate_rl(new));
2684 if (other_name && other_sym) {
2685 if (op == PARAM_LIMIT)
2686 set_extra_nomod_vsl(other_name, other_sym, vsl, NULL, alloc_estate_rl(new));
2687 else
2688 set_extra_mod(other_name, other_sym, NULL, alloc_estate_rl(new));
2691 if (op == PARAM_LIMIT && arg->type == EXPR_BINOP)
2692 db_param_limit_binops(arg, key, new);
2693 free:
2694 free_string(name);
2697 static void db_param_limit(struct expression *expr, int param, char *key, char *value)
2699 db_param_limit_filter(expr, param, key, value, PARAM_LIMIT);
2702 static void db_param_filter(struct expression *expr, int param, char *key, char *value)
2704 db_param_limit_filter(expr, param, key, value, PARAM_FILTER);
2707 static void db_param_add_set(struct expression *expr, int param, char *key, char *value, enum info_type op)
2709 struct expression *arg, *gen_expr;
2710 char *name;
2711 char *other_name = NULL;
2712 struct symbol *sym, *other_sym;
2713 struct symbol *param_type, *arg_type;
2714 struct smatch_state *state;
2715 struct range_list *new = NULL;
2716 struct range_list *added = NULL;
2718 while (expr->type == EXPR_ASSIGNMENT)
2719 expr = strip_expr(expr->right);
2720 if (expr->type != EXPR_CALL)
2721 return;
2723 arg = get_argument_from_call_expr(expr->args, param);
2724 if (!arg)
2725 return;
2727 arg_type = get_arg_type_from_key(expr->fn, param, arg, key);
2728 param_type = get_member_type_from_key(arg, key);
2729 if (param_type && param_type->type == SYM_STRUCT)
2730 return;
2731 name = get_variable_from_key(arg, key, &sym);
2732 if (!name || !sym)
2733 goto free;
2734 gen_expr = gen_expression_from_key(arg, key);
2736 state = get_state(SMATCH_EXTRA, name, sym);
2737 if (state)
2738 new = estate_rl(state);
2740 call_results_to_rl(expr, arg_type, value, &added);
2741 added = cast_rl(param_type, added);
2742 if (op == PARAM_SET)
2743 new = added;
2744 else
2745 new = rl_union(new, added);
2747 other_name = get_other_name_sym_nostack(name, sym, &other_sym);
2748 set_extra_mod(name, sym, gen_expr, alloc_estate_rl(new));
2749 if (other_name && other_sym)
2750 set_extra_mod(other_name, other_sym, gen_expr, alloc_estate_rl(new));
2751 free:
2752 free_string(other_name);
2753 free_string(name);
2756 static void db_param_add(struct expression *expr, int param, char *key, char *value)
2758 in_param_set = true;
2759 db_param_add_set(expr, param, key, value, PARAM_ADD);
2760 in_param_set = false;
2763 static void db_param_set(struct expression *expr, int param, char *key, char *value)
2765 in_param_set = true;
2766 db_param_add_set(expr, param, key, value, PARAM_SET);
2767 in_param_set = false;
2770 static void match_lost_param(struct expression *call, int param)
2772 struct expression *arg;
2774 if (is_const_param(call->fn, param))
2775 return;
2777 arg = get_argument_from_call_expr(call->args, param);
2778 if (!arg)
2779 return;
2781 arg = strip_expr(arg);
2782 if (arg->type == EXPR_PREOP && arg->op == '&')
2783 set_extra_expr_mod(arg->unop, alloc_estate_whole(get_type(arg->unop)));
2784 else
2785 ; /* if pointer then set struct members, maybe?*/
2788 static void db_param_value(struct expression *expr, int param, char *key, char *value)
2790 struct expression *call;
2791 char *name;
2792 struct symbol *sym;
2793 struct symbol *type;
2794 struct range_list *rl = NULL;
2796 if (param != -1)
2797 return;
2799 call = expr;
2800 while (call->type == EXPR_ASSIGNMENT)
2801 call = strip_expr(call->right);
2802 if (call->type != EXPR_CALL)
2803 return;
2805 type = get_member_type_from_key(expr->left, key);
2806 name = get_variable_from_key(expr->left, key, &sym);
2807 if (!name || !sym)
2808 goto free;
2810 call_results_to_rl(call, type, value, &rl);
2812 set_extra_mod(name, sym, NULL, alloc_estate_rl(rl));
2813 free:
2814 free_string(name);
2817 static void match_call_info(struct expression *expr)
2819 struct smatch_state *state;
2820 struct range_list *rl = NULL;
2821 struct expression *arg;
2822 struct symbol *type;
2823 sval_t dummy;
2824 int i = 0;
2826 FOR_EACH_PTR(expr->args, arg) {
2827 type = get_arg_type(expr->fn, i);
2829 get_absolute_rl(arg, &rl);
2830 rl = cast_rl(type, rl);
2832 if (!is_whole_rl(rl)) {
2833 rl = intersect_with_real_abs_expr(arg, rl);
2834 sql_insert_caller_info(expr, PARAM_VALUE, i, "$", show_rl(rl));
2836 state = get_state_expr(SMATCH_EXTRA, arg);
2837 if (!estate_get_single_value(state, &dummy) && estate_has_hard_max(state)) {
2838 sql_insert_caller_info(expr, HARD_MAX, i, "$",
2839 sval_to_str(estate_max(state)));
2841 if (estate_has_fuzzy_max(state)) {
2842 sql_insert_caller_info(expr, FUZZY_MAX, i, "$",
2843 sval_to_str(estate_get_fuzzy_max(state)));
2845 i++;
2846 } END_FOR_EACH_PTR(arg);
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_hook(&match_call_info, FUNCTION_CALL_HOOK);
3017 add_member_info_callback(my_id, struct_member_callback);
3018 add_split_return_callback(&returned_struct_members);
3020 // add_hook(&assume_indexes_are_valid, OP_HOOK);