2 * Copyright (C) 2012 Oracle.
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 * The point here is to store the relationships between two variables.
21 * To do that we create a state with the two variables in alphabetical order:
22 * ->name = "x vs y" and the state would be "<". On the false path the state
25 * Part of the trick of it is that if x or y is modified then we need to reset
26 * the state. We need to keep a list of all the states which depend on x and
27 * all the states which depend on y. The link_id code handles this.
32 #include "smatch_extra.h"
33 #include "smatch_slist.h"
37 static int inc_dec_id
;
38 static int inc_dec_link_id
;
40 static void add_comparison(struct expression
*left
, int comparison
, struct expression
*right
);
42 /* for handling for loops */
46 ALLOCATOR(compare_data
, "compare data");
48 static struct symbol
*vsl_to_sym(struct var_sym_list
*vsl
)
54 if (ptr_list_size((struct ptr_list
*)vsl
) != 1)
56 vs
= first_ptr_list((struct ptr_list
*)vsl
);
60 static const char *show_comparison(int comparison
)
62 if (comparison
== IMPOSSIBLE_COMPARISON
)
64 if (comparison
== UNKNOWN_COMPARISON
)
66 return show_special(comparison
);
69 struct smatch_state
*alloc_compare_state(
70 struct expression
*left
,
71 const char *left_var
, struct var_sym_list
*left_vsl
,
73 struct expression
*right
,
74 const char *right_var
, struct var_sym_list
*right_vsl
)
76 struct smatch_state
*state
;
77 struct compare_data
*data
;
79 state
= __alloc_smatch_state(0);
80 state
->name
= alloc_sname(show_comparison(comparison
));
81 data
= __alloc_compare_data(0);
83 data
->left_var
= alloc_sname(left_var
);
84 data
->left_vsl
= clone_var_sym_list(left_vsl
);
85 data
->comparison
= comparison
;
87 data
->right_var
= alloc_sname(right_var
);
88 data
->right_vsl
= clone_var_sym_list(right_vsl
);
93 int state_to_comparison(struct smatch_state
*state
)
95 if (!state
|| !state
->data
)
96 return UNKNOWN_COMPARISON
;
97 return ((struct compare_data
*)state
->data
)->comparison
;
101 * flip_comparison() reverses the op left and right. So "x >= y" becomes "y <= x".
103 int flip_comparison(int op
)
106 case UNKNOWN_COMPARISON
:
107 return UNKNOWN_COMPARISON
;
110 case SPECIAL_UNSIGNED_LT
:
111 return SPECIAL_UNSIGNED_GT
;
114 case SPECIAL_UNSIGNED_LTE
:
115 return SPECIAL_UNSIGNED_GTE
;
117 return SPECIAL_EQUAL
;
118 case SPECIAL_NOTEQUAL
:
119 return SPECIAL_NOTEQUAL
;
122 case SPECIAL_UNSIGNED_GTE
:
123 return SPECIAL_UNSIGNED_LTE
;
126 case SPECIAL_UNSIGNED_GT
:
127 return SPECIAL_UNSIGNED_LT
;
128 case IMPOSSIBLE_COMPARISON
:
129 return UNKNOWN_COMPARISON
;
131 sm_perror("unhandled comparison %d", op
);
136 int negate_comparison(int op
)
139 case UNKNOWN_COMPARISON
:
140 return UNKNOWN_COMPARISON
;
143 case SPECIAL_UNSIGNED_LT
:
144 return SPECIAL_UNSIGNED_GTE
;
147 case SPECIAL_UNSIGNED_LTE
:
148 return SPECIAL_UNSIGNED_GT
;
150 return SPECIAL_NOTEQUAL
;
151 case SPECIAL_NOTEQUAL
:
152 return SPECIAL_EQUAL
;
155 case SPECIAL_UNSIGNED_GTE
:
156 return SPECIAL_UNSIGNED_LT
;
159 case SPECIAL_UNSIGNED_GT
:
160 return SPECIAL_UNSIGNED_LTE
;
161 case IMPOSSIBLE_COMPARISON
:
162 return UNKNOWN_COMPARISON
;
164 sm_perror("unhandled comparison %d", op
);
169 static int rl_comparison(struct range_list
*left_rl
, struct range_list
*right_rl
)
171 sval_t left_min
, left_max
, right_min
, right_max
;
172 struct symbol
*type
= &int_ctype
;
174 if (!left_rl
|| !right_rl
)
175 return UNKNOWN_COMPARISON
;
177 if (type_positive_bits(rl_type(left_rl
)) > type_positive_bits(type
))
178 type
= rl_type(left_rl
);
179 if (type_positive_bits(rl_type(right_rl
)) > type_positive_bits(type
))
180 type
= rl_type(right_rl
);
182 left_rl
= cast_rl(type
, left_rl
);
183 right_rl
= cast_rl(type
, right_rl
);
185 left_min
= rl_min(left_rl
);
186 left_max
= rl_max(left_rl
);
187 right_min
= rl_min(right_rl
);
188 right_max
= rl_max(right_rl
);
190 if (left_min
.value
== left_max
.value
&&
191 right_min
.value
== right_max
.value
&&
192 left_min
.value
== right_min
.value
)
193 return SPECIAL_EQUAL
;
195 if (sval_cmp(left_max
, right_min
) < 0)
197 if (sval_cmp(left_max
, right_min
) == 0)
199 if (sval_cmp(left_min
, right_max
) > 0)
201 if (sval_cmp(left_min
, right_max
) == 0)
204 return UNKNOWN_COMPARISON
;
207 static int comparison_from_extra(struct expression
*a
, struct expression
*b
)
209 struct range_list
*left
, *right
;
211 if (!get_implied_rl(a
, &left
))
212 return UNKNOWN_COMPARISON
;
213 if (!get_implied_rl(b
, &right
))
214 return UNKNOWN_COMPARISON
;
216 return rl_comparison(left
, right
);
219 static struct range_list
*get_orig_rl(struct var_sym_list
*vsl
)
222 struct smatch_state
*state
;
226 sym
= vsl_to_sym(vsl
);
227 if (!sym
|| !sym
->ident
)
229 state
= get_orig_estate(sym
->ident
->name
, sym
);
230 return estate_rl(state
);
233 static struct smatch_state
*unmatched_comparison(struct sm_state
*sm
)
235 struct compare_data
*data
= sm
->state
->data
;
236 struct range_list
*left_rl
, *right_rl
;
237 int op
= UNKNOWN_COMPARISON
;
242 if (is_impossible_path()) {
243 op
= IMPOSSIBLE_COMPARISON
;
247 if (strstr(data
->left_var
, " orig"))
248 left_rl
= get_orig_rl(data
->left_vsl
);
249 else if (!get_implied_rl_var_sym(data
->left_var
, vsl_to_sym(data
->left_vsl
), &left_rl
))
252 if (strstr(data
->right_var
, " orig"))
253 right_rl
= get_orig_rl(data
->right_vsl
);
254 else if (!get_implied_rl_var_sym(data
->right_var
, vsl_to_sym(data
->right_vsl
), &right_rl
))
257 op
= rl_comparison(left_rl
, right_rl
);
260 return alloc_compare_state(data
->left
, data
->left_var
, data
->left_vsl
,
262 data
->right
, data
->right_var
, data
->right_vsl
);
265 /* remove_unsigned_from_comparison() is obviously a hack. */
266 int remove_unsigned_from_comparison(int op
)
269 case SPECIAL_UNSIGNED_LT
:
271 case SPECIAL_UNSIGNED_LTE
:
273 case SPECIAL_UNSIGNED_GTE
:
275 case SPECIAL_UNSIGNED_GT
:
283 * This is for when you merge states "a < b" and "a == b", the result is that
284 * we can say for sure, "a <= b" after the merge.
286 int merge_comparisons(int one
, int two
)
290 if (one
== UNKNOWN_COMPARISON
|| two
== UNKNOWN_COMPARISON
)
291 return UNKNOWN_COMPARISON
;
293 if (one
== IMPOSSIBLE_COMPARISON
)
295 if (two
== IMPOSSIBLE_COMPARISON
)
298 one
= remove_unsigned_from_comparison(one
);
299 two
= remove_unsigned_from_comparison(two
);
345 return UNKNOWN_COMPARISON
;
349 return SPECIAL_NOTEQUAL
;
356 return UNKNOWN_COMPARISON
;
360 * This is for if you have "a < b" and "b <= c" and you want to see how "a
361 * compares to c". You would call this like get_combined_comparison('<', '<=').
362 * The return comparison would be '<'.
364 int combine_comparisons(int left_compare
, int right_compare
)
368 left_compare
= remove_unsigned_from_comparison(left_compare
);
369 right_compare
= remove_unsigned_from_comparison(right_compare
);
373 switch (left_compare
) {
382 return right_compare
;
391 switch (right_compare
) {
420 return UNKNOWN_COMPARISON
;
424 * This is mostly used when you know from extra state that a <= b but you
425 * know from comparisons that a != b so then if take the intersection then
426 * we know that a < b. The name is taken from the fact that the intersection
429 int comparison_intersection(int left_compare
, int right_compare
)
431 int LT
, GT
, EQ
, NE
, total
;
433 if (left_compare
== IMPOSSIBLE_COMPARISON
||
434 right_compare
== IMPOSSIBLE_COMPARISON
)
435 return IMPOSSIBLE_COMPARISON
;
437 left_compare
= remove_unsigned_from_comparison(left_compare
);
438 right_compare
= remove_unsigned_from_comparison(right_compare
);
440 LT
= GT
= EQ
= NE
= total
= 0;
442 /* Only one side is known. */
444 return right_compare
;
448 switch (left_compare
) {
462 case SPECIAL_NOTEQUAL
:
476 return UNKNOWN_COMPARISON
;
479 switch (right_compare
) {
493 case SPECIAL_NOTEQUAL
:
507 return UNKNOWN_COMPARISON
;
522 return SPECIAL_EQUAL
;
523 if (total
== 2 && EQ
&& NE
)
524 return IMPOSSIBLE_COMPARISON
;
526 return IMPOSSIBLE_COMPARISON
;
532 return SPECIAL_NOTEQUAL
;
533 if (total
== 2 && (LT
|| GT
) && EQ
)
534 return IMPOSSIBLE_COMPARISON
;
536 return UNKNOWN_COMPARISON
;
539 static void pre_merge_hook(struct sm_state
*cur
, struct sm_state
*other
)
541 struct compare_data
*data
= cur
->state
->data
;
543 static bool in_recurse
;
545 // FIXME. No data is useless
552 extra
= comparison_from_extra(data
->left
, data
->right
);
556 new = comparison_intersection(extra
, data
->comparison
);
557 if (new == data
->comparison
)
560 // FIXME: we should always preserve implications
561 set_state(comparison_id
, cur
->name
, NULL
,
562 alloc_compare_state(data
->left
, data
->left_var
, data
->left_vsl
,
564 data
->right
, data
->right_var
, data
->right_vsl
));
567 struct smatch_state
*merge_compare_states(struct smatch_state
*s1
, struct smatch_state
*s2
)
569 struct compare_data
*data
= s1
->data
;
575 op
= merge_comparisons(state_to_comparison(s1
), state_to_comparison(s2
));
576 return alloc_compare_state(
577 data
->left
, data
->left_var
, data
->left_vsl
,
579 data
->right
, data
->right_var
, data
->right_vsl
);
582 static struct smatch_state
*alloc_link_state(struct string_list
*links
)
584 struct smatch_state
*state
;
585 static char buf
[256] = "";
589 state
= __alloc_smatch_state(0);
591 FOR_EACH_PTR(links
, tmp
) {
592 cnt
+= snprintf(buf
+ cnt
, sizeof(buf
) - cnt
, "%s%s", cnt
? ", " : "", tmp
);
593 if (cnt
>= sizeof(buf
))
595 } END_FOR_EACH_PTR(tmp
);
598 state
->name
= alloc_sname(buf
);
603 static void save_start_states(struct statement
*stmt
)
605 struct symbol
*param
;
607 char state_name
[128];
608 struct smatch_state
*state
;
609 struct string_list
*links
;
612 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, param
) {
613 struct var_sym_list
*left_vsl
= NULL
;
614 struct var_sym_list
*right_vsl
= NULL
;
618 snprintf(orig
, sizeof(orig
), "%s orig", param
->ident
->name
);
619 snprintf(state_name
, sizeof(state_name
), "%s vs %s", param
->ident
->name
, orig
);
620 add_var_sym(&left_vsl
, param
->ident
->name
, param
);
621 add_var_sym(&right_vsl
, orig
, param
);
622 state
= alloc_compare_state(
623 NULL
, param
->ident
->name
, left_vsl
,
625 NULL
, alloc_sname(orig
), right_vsl
);
626 set_state(comparison_id
, state_name
, NULL
, state
);
628 link
= alloc_sname(state_name
);
630 insert_string(&links
, link
);
631 state
= alloc_link_state(links
);
632 set_state(link_id
, param
->ident
->name
, param
, state
);
633 } END_FOR_EACH_PTR(param
);
636 static struct smatch_state
*merge_links(struct smatch_state
*s1
, struct smatch_state
*s2
)
638 struct smatch_state
*ret
;
639 struct string_list
*links
;
641 links
= combine_string_lists(s1
->data
, s2
->data
);
642 ret
= alloc_link_state(links
);
646 static void save_link_var_sym(const char *var
, struct symbol
*sym
, const char *link
)
648 struct smatch_state
*old_state
, *new_state
;
649 struct string_list
*links
;
652 old_state
= get_state(link_id
, var
, sym
);
654 links
= clone_str_list(old_state
->data
);
658 new = alloc_sname(link
);
659 insert_string(&links
, new);
661 new_state
= alloc_link_state(links
);
662 set_state(link_id
, var
, sym
, new_state
);
665 static void match_inc(struct sm_state
*sm
, bool preserve
)
667 struct string_list
*links
;
668 struct smatch_state
*state
, *new;
669 struct compare_data
*data
;
674 links
= sm
->state
->data
;
676 FOR_EACH_PTR(links
, tmp
) {
677 state
= get_state(comparison_id
, tmp
, NULL
);
685 if (strncmp(sm
->name
, tmp
, strlen(sm
->name
)) != 0 ||
686 tmp
[strlen(sm
->name
)] != ' ')
689 op
= state_to_comparison(state
);
691 switch (flip
? flip_comparison(op
) : op
) {
694 case SPECIAL_UNSIGNED_GTE
:
696 case SPECIAL_UNSIGNED_GT
:
699 new = alloc_compare_state(
700 data
->left
, data
->left_var
, data
->left_vsl
,
702 data
->right
, data
->right_var
, data
->right_vsl
);
703 set_state(comparison_id
, tmp
, NULL
, new);
706 case SPECIAL_UNSIGNED_LT
:
707 new = alloc_compare_state(
708 data
->left
, data
->left_var
, data
->left_vsl
,
709 flip
? SPECIAL_GTE
: SPECIAL_LTE
,
710 data
->right
, data
->right_var
, data
->right_vsl
);
711 set_state(comparison_id
, tmp
, NULL
, new);
714 new = alloc_compare_state(
715 data
->left
, data
->left_var
, data
->left_vsl
,
717 data
->right
, data
->right_var
, data
->right_vsl
);
718 set_state(comparison_id
, tmp
, NULL
, new);
720 } END_FOR_EACH_PTR(tmp
);
723 static void match_dec(struct sm_state
*sm
, bool preserve
)
725 struct string_list
*links
;
726 struct smatch_state
*state
;
729 links
= sm
->state
->data
;
731 FOR_EACH_PTR(links
, tmp
) {
732 struct compare_data
*data
;
733 struct smatch_state
*new;
735 state
= get_state(comparison_id
, tmp
, NULL
);
736 if (!state
|| !state
->data
)
741 switch (state_to_comparison(state
)) {
744 case SPECIAL_UNSIGNED_LTE
:
746 case SPECIAL_UNSIGNED_LT
: {
750 new = alloc_compare_state(
751 data
->left
, data
->left_var
, data
->left_vsl
,
753 data
->right
, data
->right_var
, data
->right_vsl
);
754 set_state(comparison_id
, tmp
, NULL
, new);
758 new = alloc_compare_state(
759 data
->left
, data
->left_var
, data
->left_vsl
,
761 data
->right
, data
->right_var
, data
->right_vsl
);
762 set_state(comparison_id
, tmp
, NULL
, new);
764 } END_FOR_EACH_PTR(tmp
);
767 static void reset_sm(struct sm_state
*sm
)
769 struct string_list
*links
;
772 links
= sm
->state
->data
;
774 FOR_EACH_PTR(links
, tmp
) {
775 struct smatch_state
*old
, *new;
777 old
= get_state(comparison_id
, tmp
, NULL
);
778 if (!old
|| !old
->data
) {
781 struct compare_data
*data
= old
->data
;
783 new = alloc_compare_state(
784 data
->left
, data
->left_var
, data
->left_vsl
,
786 data
->right
, data
->right_var
, data
->right_vsl
);
788 set_state(comparison_id
, tmp
, NULL
, new);
789 } END_FOR_EACH_PTR(tmp
);
790 set_state(link_id
, sm
->name
, sm
->sym
, &undefined
);
793 static bool match_add_sub_assign(struct sm_state
*sm
, struct expression
*expr
)
795 struct range_list
*rl
;
796 sval_t zero
= { .type
= &int_ctype
};
798 if (!expr
|| expr
->type
!= EXPR_ASSIGNMENT
)
800 if (expr
->op
!= SPECIAL_ADD_ASSIGN
&& expr
->op
!= SPECIAL_SUB_ASSIGN
)
803 get_absolute_rl(expr
->right
, &rl
);
804 if (sval_is_negative(rl_min(rl
))) {
809 if (expr
->op
== SPECIAL_ADD_ASSIGN
)
810 match_inc(sm
, rl_has_sval(rl
, zero
));
812 match_dec(sm
, rl_has_sval(rl
, zero
));
816 static void match_inc_dec(struct sm_state
*sm
, struct expression
*mod_expr
)
819 * if (foo > bar) then ++foo is also > bar.
823 if (match_add_sub_assign(sm
, mod_expr
))
825 if (mod_expr
->type
!= EXPR_PREOP
&& mod_expr
->type
!= EXPR_POSTOP
)
828 if (mod_expr
->op
== SPECIAL_INCREMENT
)
829 match_inc(sm
, false);
830 else if (mod_expr
->op
== SPECIAL_DECREMENT
)
831 match_dec(sm
, false);
834 static int is_self_assign(struct expression
*expr
)
836 if (!expr
|| expr
->type
!= EXPR_ASSIGNMENT
|| expr
->op
!= '=')
838 return expr_equiv(expr
->left
, expr
->right
);
841 static void match_modify(struct sm_state
*sm
, struct expression
*mod_expr
)
843 if (mod_expr
&& is_self_assign(mod_expr
))
846 /* handled by match_inc_dec() */
848 ((mod_expr
->type
== EXPR_PREOP
|| mod_expr
->type
== EXPR_POSTOP
) &&
849 (mod_expr
->op
== SPECIAL_INCREMENT
|| mod_expr
->op
== SPECIAL_DECREMENT
)))
851 if (mod_expr
&& mod_expr
->type
== EXPR_ASSIGNMENT
&&
852 (mod_expr
->op
== SPECIAL_ADD_ASSIGN
|| mod_expr
->op
== SPECIAL_SUB_ASSIGN
))
858 static void match_preop(struct expression
*expr
)
860 struct expression
*parent
;
861 struct range_list
*left
, *right
;
865 * This is an important special case. Say you have:
869 * Assume that we know the range of limit is higher than the start
870 * value for "j". Then the first thing that we process is the ++j. We
871 * have not comparison states set up so it doesn't get caught by the
872 * modification hook. But it does get caught by smatch_extra which sets
873 * j to unknown then we parse the "j == limit" and sets false to != but
874 * really we want false to be <.
876 * So what we do is we set j < limit here, then the match_modify catches
877 * it and we do a match_inc_dec().
881 if (expr
->type
!= EXPR_PREOP
||
882 (expr
->op
!= SPECIAL_INCREMENT
&& expr
->op
!= SPECIAL_DECREMENT
))
885 parent
= expr_get_parent_expr(expr
);
888 if (parent
->type
!= EXPR_COMPARE
|| parent
->op
!= SPECIAL_EQUAL
)
890 if (parent
->left
!= expr
)
893 if (!get_implied_rl(expr
->unop
, &left
) ||
894 !get_implied_rl(parent
->right
, &right
))
897 op
= rl_comparison(left
, right
);
898 if (op
== UNKNOWN_COMPARISON
)
901 add_comparison(expr
->unop
, op
, parent
->right
);
904 static char *chunk_to_var_sym(struct expression
*expr
, struct symbol
**sym
)
906 expr
= strip_expr(expr
);
912 if (expr
->type
== EXPR_PREOP
&&
913 (expr
->op
== SPECIAL_INCREMENT
||
914 expr
->op
== SPECIAL_DECREMENT
))
915 expr
= strip_expr(expr
->unop
);
917 if (expr
->type
== EXPR_CALL
) {
920 snprintf(buf
, sizeof(buf
), "return %p", expr
);
921 return alloc_string(buf
);
924 return expr_to_chunk_sym_vsl(expr
, sym
, NULL
);
927 static char *chunk_to_var(struct expression
*expr
)
929 return chunk_to_var_sym(expr
, NULL
);
932 static struct smatch_state
*get_state_chunk(int owner
, struct expression
*expr
)
936 struct smatch_state
*ret
;
938 name
= chunk_to_var_sym(expr
, &sym
);
942 ret
= get_state(owner
, name
, sym
);
947 static void save_link(struct expression
*expr
, char *link
)
952 expr
= strip_expr(expr
);
953 if (expr
->type
== EXPR_BINOP
) {
956 chunk
= chunk_to_var(expr
);
960 save_link(expr
->left
, link
);
961 save_link(expr
->right
, link
);
962 save_link_var_sym(chunk
, NULL
, link
);
966 var
= chunk_to_var_sym(expr
, &sym
);
970 save_link_var_sym(var
, sym
, link
);
974 static int get_orig_comparison(struct stree
*pre_stree
, const char *left
, const char *right
)
976 struct smatch_state
*state
;
977 struct compare_data
*data
;
979 char state_name
[256];
981 if (strcmp(left
, right
) > 0) {
982 const char *tmp
= right
;
989 snprintf(state_name
, sizeof(state_name
), "%s vs %s", left
, right
);
990 state
= get_state_stree(pre_stree
, comparison_id
, state_name
, NULL
);
991 if (!state
|| !state
->data
)
995 return flip_comparison(data
->comparison
);
996 return data
->comparison
;
1000 static int have_common_var_sym(struct var_sym_list
*left_vsl
, struct var_sym_list
*right_vsl
)
1002 struct var_sym
*tmp
;
1004 FOR_EACH_PTR(left_vsl
, tmp
) {
1005 if (in_var_sym_list(right_vsl
, tmp
->var
, tmp
->sym
))
1007 } END_FOR_EACH_PTR(tmp
);
1013 * The idea here is that we take a comparison "a < b" and then we look at all
1014 * the things which "b" is compared against "b < c" and we say that that implies
1015 * a relationship "a < c".
1017 * The names here about because the comparisons are organized like this
1021 static void update_tf_links(struct stree
*pre_stree
,
1022 struct expression
*left_expr
,
1023 const char *left_var
, struct var_sym_list
*left_vsl
,
1024 int left_comparison
, int left_false_comparison
,
1025 const char *mid_var
, struct var_sym_list
*mid_vsl
,
1026 struct string_list
*links
)
1028 struct smatch_state
*state
;
1029 struct smatch_state
*true_state
, *false_state
;
1030 struct compare_data
*data
;
1031 struct expression
*right_expr
;
1032 const char *left_var_orig
= left_var
;
1033 const char *right_var
;
1034 struct var_sym_list
*left_vsl_orig
= left_vsl
;
1035 struct var_sym_list
*right_vsl
;
1036 int orig_comparison
;
1037 int right_comparison
;
1038 int true_comparison
;
1039 int false_comparison
;
1041 char state_name
[256];
1044 FOR_EACH_PTR(links
, tmp
) {
1045 state
= get_state_stree(pre_stree
, comparison_id
, tmp
, NULL
);
1046 if (!state
|| !state
->data
)
1048 left_var
= left_var_orig
;
1049 left_vsl
= left_vsl_orig
;
1051 right_comparison
= data
->comparison
;
1052 right_expr
= data
->right
;
1053 right_var
= data
->right_var
;
1054 right_vsl
= data
->right_vsl
;
1055 if (strcmp(mid_var
, right_var
) == 0) {
1056 right_expr
= data
->left
;
1057 right_var
= data
->left_var
;
1058 right_vsl
= data
->left_vsl
;
1059 right_comparison
= flip_comparison(right_comparison
);
1061 if (have_common_var_sym(left_vsl
, right_vsl
))
1064 orig_comparison
= get_orig_comparison(pre_stree
, left_var
, right_var
);
1066 true_comparison
= combine_comparisons(left_comparison
, right_comparison
);
1067 false_comparison
= combine_comparisons(left_false_comparison
, right_comparison
);
1069 true_comparison
= comparison_intersection(orig_comparison
, true_comparison
);
1070 false_comparison
= comparison_intersection(orig_comparison
, false_comparison
);
1072 if (strcmp(left_var
, right_var
) > 0) {
1073 struct expression
*tmp_expr
= left_expr
;
1074 const char *tmp_var
= left_var
;
1075 struct var_sym_list
*tmp_vsl
= left_vsl
;
1077 left_expr
= right_expr
;
1078 left_var
= right_var
;
1079 left_vsl
= right_vsl
;
1080 right_expr
= tmp_expr
;
1081 right_var
= tmp_var
;
1082 right_vsl
= tmp_vsl
;
1083 true_comparison
= flip_comparison(true_comparison
);
1084 false_comparison
= flip_comparison(false_comparison
);
1087 if (!true_comparison
&& !false_comparison
)
1090 if (true_comparison
)
1091 true_state
= alloc_compare_state(
1092 left_expr
, left_var
, left_vsl
,
1094 right_expr
, right_var
, right_vsl
);
1097 if (false_comparison
)
1098 false_state
= alloc_compare_state(
1099 left_expr
, left_var
, left_vsl
,
1101 right_expr
, right_var
, right_vsl
);
1105 snprintf(state_name
, sizeof(state_name
), "%s vs %s", left_var
, right_var
);
1106 set_true_false_states(comparison_id
, state_name
, NULL
, true_state
, false_state
);
1107 FOR_EACH_PTR(left_vsl
, vs
) {
1108 save_link_var_sym(vs
->var
, vs
->sym
, state_name
);
1109 } END_FOR_EACH_PTR(vs
);
1110 FOR_EACH_PTR(right_vsl
, vs
) {
1111 save_link_var_sym(vs
->var
, vs
->sym
, state_name
);
1112 } END_FOR_EACH_PTR(vs
);
1113 if (!vsl_to_sym(left_vsl
))
1114 save_link_var_sym(left_var
, NULL
, state_name
);
1115 if (!vsl_to_sym(right_vsl
))
1116 save_link_var_sym(right_var
, NULL
, state_name
);
1117 } END_FOR_EACH_PTR(tmp
);
1120 static void update_tf_data(struct stree
*pre_stree
,
1121 struct expression
*left_expr
,
1122 const char *left_name
, struct var_sym_list
*left_vsl
,
1123 struct expression
*right_expr
,
1124 const char *right_name
, struct var_sym_list
*right_vsl
,
1125 int true_comparison
, int false_comparison
)
1127 struct smatch_state
*state
;
1129 state
= get_state_stree(pre_stree
, link_id
, right_name
, vsl_to_sym(right_vsl
));
1131 update_tf_links(pre_stree
, left_expr
, left_name
, left_vsl
, true_comparison
, false_comparison
, right_name
, right_vsl
, state
->data
);
1133 state
= get_state_stree(pre_stree
, link_id
, left_name
, vsl_to_sym(left_vsl
));
1135 update_tf_links(pre_stree
, right_expr
, right_name
, right_vsl
, flip_comparison(true_comparison
), flip_comparison(false_comparison
), left_name
, left_vsl
, state
->data
);
1138 static void iter_modify(struct sm_state
*sm
, struct expression
*mod_expr
)
1140 if (sm
->state
!= &start
||
1142 (mod_expr
->type
!= EXPR_PREOP
&& mod_expr
->type
!= EXPR_POSTOP
) ||
1143 mod_expr
->op
!= SPECIAL_INCREMENT
)
1144 set_state(inc_dec_id
, sm
->name
, sm
->sym
, &undefined
);
1146 set_state(inc_dec_id
, sm
->name
, sm
->sym
, &incremented
);
1149 static void handle_for_loops(struct expression
*expr
, char *state_name
, struct smatch_state
*false_state
)
1152 char *iter_name
, *cap_name
;
1153 struct symbol
*iter_sym
, *cap_sym
;
1154 struct compare_data
*data
;
1156 if (expr
->op
!= '<' && expr
->op
!= SPECIAL_UNSIGNED_LT
)
1159 if (!__cur_stmt
|| !__prev_stmt
)
1161 if (__cur_stmt
->type
!= STMT_ITERATOR
)
1163 if (__cur_stmt
->iterator_pre_condition
!= expr
)
1166 /* literals are handled in smatch_extra.c */
1167 if (get_value(expr
->right
, &sval
))
1170 /* First time checking the condition */
1171 if (__prev_stmt
== __cur_stmt
->iterator_pre_statement
) {
1172 if (!get_implied_value(expr
->left
, &sval
) ||
1176 iter_name
= expr_to_var_sym(expr
->left
, &iter_sym
);
1177 cap_name
= expr_to_var_sym(expr
->right
, &cap_sym
);
1178 if (!iter_name
|| !cap_name
|| !iter_sym
|| !cap_sym
) {
1179 free_string(iter_name
);
1180 free_string(cap_name
);
1184 set_state(inc_dec_id
, iter_name
, iter_sym
, &start
);
1185 store_link(inc_dec_link_id
, cap_name
, cap_sym
, iter_name
, iter_sym
);
1187 free_string(iter_name
);
1188 free_string(cap_name
);
1192 /* Second time checking the condtion */
1193 if (__prev_stmt
!= __cur_stmt
->iterator_post_statement
)
1196 if (get_state_chunk(inc_dec_id
, expr
->left
) != &incremented
)
1199 data
= false_state
->data
;
1200 false_state
= alloc_compare_state(
1201 data
->left
, data
->left_var
, data
->left_vsl
,
1203 data
->right
, data
->right_var
, data
->right_vsl
);
1205 // FIXME: This doesn't handle links correct so it doesn't set "param orig"
1206 set_true_false_states(comparison_id
, state_name
, NULL
, NULL
, false_state
);
1209 static int is_plus_one(struct expression
*expr
)
1213 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '+')
1215 if (!get_implied_value(expr
->right
, &sval
) || sval
.value
!= 1)
1220 static int is_minus_one(struct expression
*expr
)
1224 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '-')
1226 if (!get_implied_value(expr
->right
, &sval
) || sval
.value
!= 1)
1231 static void remove_plus_minus_zero(struct expression
**expr_p
)
1233 struct expression
*expr
= *expr_p
;
1236 if (!expr
|| expr
->type
!= EXPR_BINOP
)
1238 if (expr
->op
== '+') {
1239 if (get_implied_value(expr
->left
, &sval
) && sval
.value
== 0)
1240 *expr_p
= expr
->right
;
1241 else if (get_implied_value(expr
->right
, &sval
) && sval
.value
== 0)
1242 *expr_p
= expr
->left
;
1245 return remove_plus_minus_zero(expr_p
);
1247 if (expr
->op
== '-') {
1248 if (get_implied_value(expr
->right
, &sval
) && sval
.value
== 0) {
1249 *expr_p
= expr
->left
;
1250 return remove_plus_minus_zero(expr_p
);
1255 static void move_plus_to_minus_helper(struct expression
**left_p
, struct expression
**right_p
)
1257 struct expression
*left
= *left_p
;
1258 struct expression
*right
= *right_p
;
1261 * These two are basically equivalent: "foo + 1 != bar" and
1262 * "foo != bar - 1". There are issues with signedness and integer
1263 * overflows. There are also issues with type as well. But let's
1264 * pretend we can ignore all that stuff for now.
1268 if (!is_plus_one(left
))
1271 *left_p
= left
->left
;
1272 *right_p
= binop_expression(right
, '-', left
->right
);
1275 static void move_plus_to_minus(struct expression
**left_p
, struct expression
**right_p
)
1277 if (is_plus_one(*left_p
) && is_plus_one(*right_p
))
1280 move_plus_to_minus_helper(left_p
, right_p
);
1281 move_plus_to_minus_helper(right_p
, left_p
);
1284 static void simplify_binops(struct expression
**left_p
, struct expression
**right_p
)
1286 remove_plus_minus_zero(left_p
);
1287 remove_plus_minus_zero(right_p
);
1289 move_plus_to_minus(left_p
, right_p
);
1292 static void handle_comparison(struct expression
*left_expr
, int op
, struct expression
*right_expr
, char **_state_name
, struct smatch_state
**_false_state
)
1296 struct symbol
*left_sym
, *right_sym
;
1297 struct var_sym_list
*left_vsl
= NULL
;
1298 struct var_sym_list
*right_vsl
= NULL
;
1300 int orig_comparison
;
1301 struct smatch_state
*true_state
, *false_state
;
1302 static char state_name
[256];
1303 struct stree
*pre_stree
;
1306 if (!left_expr
|| !right_expr
)
1309 left_expr
= strip_parens(left_expr
);
1310 right_expr
= strip_parens(right_expr
);
1312 while (left_expr
->type
== EXPR_ASSIGNMENT
)
1313 left_expr
= strip_parens(left_expr
->left
);
1314 while (right_expr
->type
== EXPR_ASSIGNMENT
)
1315 right_expr
= strip_parens(right_expr
->left
);
1317 false_op
= negate_comparison(op
);
1319 simplify_binops(&left_expr
, &right_expr
);
1321 if (op
== SPECIAL_UNSIGNED_LT
&&
1322 get_implied_value(left_expr
, &sval
) &&
1324 false_op
= SPECIAL_EQUAL
;
1326 if (op
== SPECIAL_UNSIGNED_GT
&&
1327 get_implied_value(right_expr
, &sval
) &&
1329 false_op
= SPECIAL_EQUAL
;
1331 left
= chunk_to_var_sym(left_expr
, &left_sym
);
1335 add_var_sym(&left_vsl
, left
, left_sym
);
1337 left_vsl
= expr_to_vsl(left_expr
);
1338 right
= chunk_to_var_sym(right_expr
, &right_sym
);
1342 add_var_sym(&right_vsl
, right
, right_sym
);
1344 right_vsl
= expr_to_vsl(right_expr
);
1346 if (strcmp(left
, right
) > 0) {
1347 char *tmp_name
= left
;
1348 struct var_sym_list
*tmp_vsl
= left_vsl
;
1349 struct expression
*tmp_expr
= left_expr
;
1352 left_vsl
= right_vsl
;
1353 left_expr
= right_expr
;
1355 right_vsl
= tmp_vsl
;
1356 right_expr
= tmp_expr
;
1357 op
= flip_comparison(op
);
1358 false_op
= flip_comparison(false_op
);
1361 orig_comparison
= get_comparison(left_expr
, right_expr
);
1362 op
= comparison_intersection(orig_comparison
, op
);
1363 false_op
= comparison_intersection(orig_comparison
, false_op
);
1365 snprintf(state_name
, sizeof(state_name
), "%s vs %s", left
, right
);
1366 true_state
= alloc_compare_state(
1367 left_expr
, left
, left_vsl
,
1369 right_expr
, right
, right_vsl
);
1370 false_state
= alloc_compare_state(
1371 left_expr
, left
, left_vsl
,
1373 right_expr
, right
, right_vsl
);
1375 pre_stree
= clone_stree(__get_cur_stree());
1376 update_tf_data(pre_stree
, left_expr
, left
, left_vsl
, right_expr
, right
, right_vsl
, op
, false_op
);
1377 free_stree(&pre_stree
);
1379 set_true_false_states(comparison_id
, state_name
, NULL
, true_state
, false_state
);
1380 __compare_param_limit_hook(left_expr
, right_expr
, state_name
, true_state
, false_state
);
1381 save_link(left_expr
, state_name
);
1382 save_link(right_expr
, state_name
);
1385 *_false_state
= false_state
;
1387 *_state_name
= state_name
;
1393 void __comparison_match_condition(struct expression
*expr
)
1395 struct expression
*left
, *right
, *new_left
, *new_right
, *tmp
;
1396 struct smatch_state
*false_state
= NULL
;
1397 char *state_name
= NULL
;
1400 if (expr
->type
!= EXPR_COMPARE
)
1403 handle_comparison(expr
->left
, expr
->op
, expr
->right
, &state_name
, &false_state
);
1404 if (false_state
&& state_name
)
1405 handle_for_loops(expr
, state_name
, false_state
);
1407 left
= strip_parens(expr
->left
);
1408 right
= strip_parens(expr
->right
);
1410 if (left
->type
== EXPR_BINOP
&& left
->op
== '+') {
1411 new_left
= left
->left
;
1412 new_right
= binop_expression(right
, '-', left
->right
);
1413 handle_comparison(new_left
, expr
->op
, new_right
, NULL
, NULL
);
1415 new_left
= left
->right
;
1416 new_right
= binop_expression(right
, '-', left
->left
);
1417 handle_comparison(new_left
, expr
->op
, new_right
, NULL
, NULL
);
1420 if ((expr
->op
== SPECIAL_EQUAL
|| expr
->op
== SPECIAL_NOTEQUAL
) &&
1421 expr_is_zero(expr
->right
) &&
1422 left
->type
== EXPR_BINOP
&& left
->op
== '-') {
1423 new_left
= left
->left
;
1424 new_right
= left
->right
;
1425 handle_comparison(new_left
, expr
->op
, new_right
, NULL
, NULL
);
1429 left
= strip_parens(expr
->left
);
1430 right
= strip_parens(expr
->right
);
1431 if (get_last_expr_from_expression_stmt(expr
->left
)) {
1432 left
= get_last_expr_from_expression_stmt(expr
->left
);
1435 if (get_last_expr_from_expression_stmt(expr
->right
)) {
1436 right
= get_last_expr_from_expression_stmt(expr
->right
);
1444 while ((tmp
= get_assigned_expr(left
))) {
1447 left
= strip_expr(tmp
);
1450 while ((tmp
= get_assigned_expr(right
))) {
1453 right
= strip_expr(tmp
);
1456 handle_comparison(left
, expr
->op
, right
, NULL
, NULL
);
1459 static void add_comparison_var_sym(
1460 struct expression
*left_expr
,
1461 const char *left_name
, struct var_sym_list
*left_vsl
,
1463 struct expression
*right_expr
,
1464 const char *right_name
, struct var_sym_list
*right_vsl
)
1466 struct smatch_state
*state
;
1468 char state_name
[256];
1470 if (strcmp(left_name
, right_name
) > 0) {
1471 struct expression
*tmp_expr
= left_expr
;
1472 const char *tmp_name
= left_name
;
1473 struct var_sym_list
*tmp_vsl
= left_vsl
;
1475 left_expr
= right_expr
;
1476 left_name
= right_name
;
1477 left_vsl
= right_vsl
;
1478 right_expr
= tmp_expr
;
1479 right_name
= tmp_name
;
1480 right_vsl
= tmp_vsl
;
1481 comparison
= flip_comparison(comparison
);
1483 snprintf(state_name
, sizeof(state_name
), "%s vs %s", left_name
, right_name
);
1484 state
= alloc_compare_state(
1485 left_expr
, left_name
, left_vsl
,
1487 right_expr
, right_name
, right_vsl
);
1489 set_state(comparison_id
, state_name
, NULL
, state
);
1491 FOR_EACH_PTR(left_vsl
, vs
) {
1492 save_link_var_sym(vs
->var
, vs
->sym
, state_name
);
1493 } END_FOR_EACH_PTR(vs
);
1494 FOR_EACH_PTR(right_vsl
, vs
) {
1495 save_link_var_sym(vs
->var
, vs
->sym
, state_name
);
1496 } END_FOR_EACH_PTR(vs
);
1499 static void add_comparison(struct expression
*left
, int comparison
, struct expression
*right
)
1501 char *left_name
= NULL
;
1502 char *right_name
= NULL
;
1503 struct symbol
*left_sym
, *right_sym
;
1504 struct var_sym_list
*left_vsl
, *right_vsl
;
1505 struct smatch_state
*state
;
1506 char state_name
[256];
1508 left_name
= chunk_to_var_sym(left
, &left_sym
);
1511 left_vsl
= expr_to_vsl(left
);
1512 right_name
= chunk_to_var_sym(right
, &right_sym
);
1515 right_vsl
= expr_to_vsl(right
);
1517 if (strcmp(left_name
, right_name
) > 0) {
1518 struct expression
*tmp_expr
= left
;
1519 struct symbol
*tmp_sym
= left_sym
;
1520 char *tmp_name
= left_name
;
1521 struct var_sym_list
*tmp_vsl
= left_vsl
;
1524 left_name
= right_name
;
1525 left_sym
= right_sym
;
1526 left_vsl
= right_vsl
;
1528 right_name
= tmp_name
;
1529 right_sym
= tmp_sym
;
1530 right_vsl
= tmp_vsl
;
1531 comparison
= flip_comparison(comparison
);
1533 snprintf(state_name
, sizeof(state_name
), "%s vs %s", left_name
, right_name
);
1534 state
= alloc_compare_state(
1535 left
, left_name
, left_vsl
,
1537 right
, right_name
, right_vsl
);
1539 set_state(comparison_id
, state_name
, NULL
, state
);
1540 save_link(left
, state_name
);
1541 save_link(right
, state_name
);
1544 free_string(left_name
);
1545 free_string(right_name
);
1548 static void match_assign_add(struct expression
*expr
)
1550 struct expression
*right
;
1551 struct expression
*r_left
, *r_right
;
1552 sval_t left_tmp
, right_tmp
;
1554 right
= strip_expr(expr
->right
);
1555 r_left
= strip_expr(right
->left
);
1556 r_right
= strip_expr(right
->right
);
1558 get_absolute_min(r_left
, &left_tmp
);
1559 get_absolute_min(r_right
, &right_tmp
);
1561 if (left_tmp
.value
> 0)
1562 add_comparison(expr
->left
, '>', r_right
);
1563 else if (left_tmp
.value
== 0)
1564 add_comparison(expr
->left
, SPECIAL_GTE
, r_right
);
1566 if (right_tmp
.value
> 0)
1567 add_comparison(expr
->left
, '>', r_left
);
1568 else if (right_tmp
.value
== 0)
1569 add_comparison(expr
->left
, SPECIAL_GTE
, r_left
);
1572 static void match_assign_sub(struct expression
*expr
)
1574 struct expression
*right
;
1575 struct expression
*r_left
, *r_right
;
1579 right
= strip_expr(expr
->right
);
1580 r_left
= strip_expr(right
->left
);
1581 r_right
= strip_expr(right
->right
);
1583 if (get_absolute_min(r_right
, &min
) && sval_is_negative(min
))
1586 comparison
= get_comparison(r_left
, r_right
);
1588 switch (comparison
) {
1591 if (implied_not_equal(r_right
, 0))
1592 add_comparison(expr
->left
, '>', r_left
);
1594 add_comparison(expr
->left
, SPECIAL_GTE
, r_left
);
1599 static void match_assign_divide(struct expression
*expr
)
1601 struct expression
*right
;
1602 struct expression
*r_left
, *r_right
;
1605 right
= strip_expr(expr
->right
);
1606 r_left
= strip_expr(right
->left
);
1607 r_right
= strip_expr(right
->right
);
1608 if (!get_implied_min(r_right
, &min
) || min
.value
<= 1)
1611 add_comparison(expr
->left
, '<', r_left
);
1614 static void match_binop_assign(struct expression
*expr
)
1616 struct expression
*right
;
1618 right
= strip_expr(expr
->right
);
1619 if (right
->op
== '+')
1620 match_assign_add(expr
);
1621 if (right
->op
== '-')
1622 match_assign_sub(expr
);
1623 if (right
->op
== '/')
1624 match_assign_divide(expr
);
1627 static void copy_comparisons(struct expression
*left
, struct expression
*right
)
1629 struct string_list
*links
;
1630 struct smatch_state
*state
;
1631 struct compare_data
*data
;
1632 struct symbol
*left_sym
, *right_sym
;
1633 char *left_var
= NULL
;
1634 char *right_var
= NULL
;
1635 struct var_sym_list
*left_vsl
;
1636 struct expression
*expr
;
1638 struct var_sym_list
*vsl
;
1643 * Say we have y < foo and we assign x = y; then that means x < foo.
1644 * Internally, smatch has links from "y" and "foo" to the comparison
1645 * "foo vs y". There could also be a comparison with an array:
1646 * "x[idx] vs limit", so all three variables, "x", "idx" and "limit"
1649 * Also another thing which can happen is that you have an assignment
1650 * "x = y" and there is a comparison: "x vs y". And clearly we don't
1651 * want to store "x < x".
1653 * So what this function does is it takes each link "foo vs y" and it
1654 * swaps in "foo vs x". Except it moves the "x" to the left so it's
1655 * "x vs foo" (or whatever) and the add_comparison_var_sym() will flip
1656 * it into the correct order.
1660 left_var
= chunk_to_var_sym(left
, &left_sym
);
1663 left_vsl
= expr_to_vsl(left
);
1664 right_var
= chunk_to_var_sym(right
, &right_sym
);
1668 state
= get_state(link_id
, right_var
, right_sym
);
1671 links
= state
->data
;
1673 FOR_EACH_PTR(links
, tmp
) {
1674 state
= get_state(comparison_id
, tmp
, NULL
);
1675 if (!state
|| !state
->data
)
1678 /* if there isn't a comparison then skip it. (can this happen?) */
1679 if (!data
->comparison
)
1682 /* right_var is from "left = right_var;". "var" is the variable
1683 * bit from "right_var < var".
1685 if (strcmp(right_var
, data
->left_var
) == 0) {
1687 var
= data
->right_var
;
1688 vsl
= data
->right_vsl
;
1689 comparison
= data
->comparison
;
1690 } else if (strcmp(right_var
, data
->right_var
) == 0) {
1692 var
= data
->left_var
;
1693 vsl
= data
->left_vsl
;
1694 comparison
= flip_comparison(data
->comparison
);
1696 /* This means the right side is only part of the
1697 * comparison. As in "x = i;" when "foo[i] < bar".
1702 /* n = copy_from_user(dest, src, n); leads to n <= n which is nonsense */
1703 if (strcmp(left_var
, var
) == 0)
1706 add_comparison_var_sym(left
, left_var
, left_vsl
, comparison
, expr
, var
, vsl
);
1707 } END_FOR_EACH_PTR(tmp
);
1710 free_string(right_var
);
1713 static bool is_empty_fake_assign(struct expression
*expr
)
1715 if (!is_fake_var_assign(expr
))
1717 if (get_state_chunk(link_id
, expr
->right
))
1722 static void match_assign(struct expression
*expr
)
1724 struct expression
*right
;
1726 if (expr
->op
!= '=')
1728 if (__in_fake_assign
|| outside_of_function())
1731 if (is_struct(expr
->left
))
1734 if (is_self_assign(expr
))
1737 if (is_empty_fake_assign(expr
))
1740 copy_comparisons(expr
->left
, expr
->right
);
1741 add_comparison(expr
->left
, SPECIAL_EQUAL
, expr
->right
);
1743 right
= strip_expr(expr
->right
);
1744 if (right
->type
== EXPR_BINOP
)
1745 match_binop_assign(expr
);
1748 int get_comparison_strings(const char *one
, const char *two
)
1751 struct smatch_state
*state
;
1756 return UNKNOWN_COMPARISON
;
1758 if (strcmp(one
, two
) == 0)
1759 return SPECIAL_EQUAL
;
1761 if (strcmp(one
, two
) > 0) {
1762 const char *tmp
= one
;
1769 snprintf(buf
, sizeof(buf
), "%s vs %s", one
, two
);
1770 state
= get_state(comparison_id
, buf
, NULL
);
1772 ret
= state_to_comparison(state
);
1775 ret
= flip_comparison(ret
);
1780 static int get_comparison_helper(struct expression
*a
, struct expression
*b
, bool use_extra
)
1784 int ret
= UNKNOWN_COMPARISON
;
1785 int extra
= UNKNOWN_COMPARISON
;
1787 if (a
== UNKNOWN_COMPARISON
||
1788 b
== UNKNOWN_COMPARISON
)
1789 return UNKNOWN_COMPARISON
;
1791 a
= strip_parens(a
);
1792 b
= strip_parens(b
);
1794 move_plus_to_minus(&a
, &b
);
1796 simplify_binops(&a
, &b
);
1798 one
= chunk_to_var(a
);
1801 two
= chunk_to_var(b
);
1805 ret
= get_comparison_strings(one
, two
);
1809 if (is_plus_one(a
) || is_minus_one(a
)) {
1811 one
= chunk_to_var(a
->left
);
1812 ret
= get_comparison_strings(one
, two
);
1813 } else if (is_plus_one(b
) || is_minus_one(b
)) {
1815 two
= chunk_to_var(b
->left
);
1816 ret
= get_comparison_strings(one
, two
);
1819 if (ret
== UNKNOWN_COMPARISON
)
1822 if ((is_plus_one(a
) || is_minus_one(b
)) && ret
== '<')
1824 else if ((is_minus_one(a
) || is_plus_one(b
)) && ret
== '>')
1827 ret
= UNKNOWN_COMPARISON
;
1834 extra
= comparison_from_extra(a
, b
);
1835 return comparison_intersection(ret
, extra
);
1838 int get_comparison(struct expression
*a
, struct expression
*b
)
1840 return get_comparison_helper(a
, b
, true);
1843 int get_comparison_no_extra(struct expression
*a
, struct expression
*b
)
1845 return get_comparison_helper(a
, b
, false);
1848 int possible_comparison(struct expression
*a
, int comparison
, struct expression
*b
)
1854 struct sm_state
*sm
;
1857 one
= chunk_to_var(a
);
1860 two
= chunk_to_var(b
);
1865 if (strcmp(one
, two
) == 0 && comparison
== SPECIAL_EQUAL
) {
1870 if (strcmp(one
, two
) > 0) {
1875 comparison
= flip_comparison(comparison
);
1878 snprintf(buf
, sizeof(buf
), "%s vs %s", one
, two
);
1879 sm
= get_sm_state(comparison_id
, buf
, NULL
);
1883 FOR_EACH_PTR(sm
->possible
, sm
) {
1884 if (!sm
->state
->data
)
1886 saved
= ((struct compare_data
*)sm
->state
->data
)->comparison
;
1887 if (saved
== comparison
)
1889 if (comparison
== SPECIAL_EQUAL
&&
1890 (saved
== SPECIAL_LTE
||
1891 saved
== SPECIAL_GTE
||
1892 saved
== SPECIAL_UNSIGNED_LTE
||
1893 saved
== SPECIAL_UNSIGNED_GTE
))
1897 } END_FOR_EACH_PTR(sm
);
1906 struct state_list
*get_all_comparisons(struct expression
*expr
)
1908 struct smatch_state
*state
;
1909 struct string_list
*links
;
1910 struct state_list
*ret
= NULL
;
1911 struct sm_state
*sm
;
1914 state
= get_state_chunk(link_id
, expr
);
1917 links
= state
->data
;
1919 FOR_EACH_PTR(links
, tmp
) {
1920 sm
= get_sm_state(comparison_id
, tmp
, NULL
);
1923 // FIXME have to compare name with vsl
1924 add_ptr_list(&ret
, sm
);
1925 } END_FOR_EACH_PTR(tmp
);
1930 struct state_list
*get_all_possible_equal_comparisons(struct expression
*expr
)
1932 struct smatch_state
*state
;
1933 struct string_list
*links
;
1934 struct state_list
*ret
= NULL
;
1935 struct sm_state
*sm
;
1938 state
= get_state_chunk(link_id
, expr
);
1941 links
= state
->data
;
1943 FOR_EACH_PTR(links
, tmp
) {
1944 sm
= get_sm_state(comparison_id
, tmp
, NULL
);
1947 if (!strchr(sm
->state
->name
, '='))
1949 if (strcmp(sm
->state
->name
, "!=") == 0)
1951 add_ptr_list(&ret
, sm
);
1952 } END_FOR_EACH_PTR(tmp
);
1957 struct state_list
*get_all_possible_not_equal_comparisons(struct expression
*expr
)
1959 struct smatch_state
*state
;
1960 struct string_list
*links
;
1961 struct state_list
*ret
= NULL
;
1962 struct sm_state
*sm
;
1963 struct sm_state
*possible
;
1968 state
= get_state_chunk(link_id
, expr
);
1971 links
= state
->data
;
1973 FOR_EACH_PTR(links
, link
) {
1974 sm
= get_sm_state(comparison_id
, link
, NULL
);
1977 FOR_EACH_PTR(sm
->possible
, possible
) {
1978 if (strcmp(possible
->state
->name
, "!=") != 0)
1980 add_ptr_list(&ret
, sm
);
1982 } END_FOR_EACH_PTR(possible
);
1983 } END_FOR_EACH_PTR(link
);
1988 static void update_links_from_call(struct expression
*left
,
1990 struct expression
*right
)
1992 struct string_list
*links
;
1993 struct smatch_state
*state
;
1994 struct compare_data
*data
;
1995 struct symbol
*left_sym
, *right_sym
;
1996 char *left_var
= NULL
;
1997 char *right_var
= NULL
;
1998 struct var_sym_list
*left_vsl
;
1999 struct expression
*expr
;
2001 struct var_sym_list
*vsl
;
2005 left_var
= chunk_to_var_sym(left
, &left_sym
);
2008 left_vsl
= expr_to_vsl(left
);
2009 right_var
= chunk_to_var_sym(right
, &right_sym
);
2013 state
= get_state(link_id
, right_var
, right_sym
);
2016 links
= state
->data
;
2018 FOR_EACH_PTR(links
, tmp
) {
2019 state
= get_state(comparison_id
, tmp
, NULL
);
2020 if (!state
|| !state
->data
)
2023 comparison
= data
->comparison
;
2025 var
= data
->right_var
;
2026 vsl
= data
->right_vsl
;
2027 if (strcmp(var
, right_var
) == 0) {
2029 var
= data
->left_var
;
2030 vsl
= data
->left_vsl
;
2031 comparison
= flip_comparison(comparison
);
2033 comparison
= combine_comparisons(left_compare
, comparison
);
2036 add_comparison_var_sym(left
, left_var
, left_vsl
, comparison
, expr
, var
, vsl
);
2037 } END_FOR_EACH_PTR(tmp
);
2040 free_string(right_var
);
2043 static void return_str_comparison(struct expression
*expr
, const char *range
)
2045 struct expression
*arg
;
2049 while (expr
->type
== EXPR_ASSIGNMENT
)
2050 expr
= strip_expr(expr
->right
);
2051 if (expr
->type
!= EXPR_CALL
)
2053 if (!str_to_comparison_arg(range
, expr
, &comparison
, &arg
))
2055 snprintf(buf
, sizeof(buf
), "%s", show_comparison(comparison
));
2056 update_links_from_call(expr
, comparison
, arg
);
2057 add_comparison(expr
, comparison
, arg
);
2060 void __add_comparison_info(struct expression
*expr
, struct expression
*call
, const char *range
)
2062 copy_comparisons(expr
, call
);
2065 static char *get_mask_comparison(struct expression
*expr
, int ignore
)
2067 struct expression
*tmp
, *right
;
2071 /* The return value for "return foo & param;" is <= param */
2074 while ((tmp
= get_assigned_expr(expr
))) {
2075 expr
= strip_expr(tmp
);
2080 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '&')
2083 right
= strip_expr(expr
->right
);
2084 param
= get_param_num(right
);
2085 if (param
< 0 || param
== ignore
)
2088 snprintf(buf
, sizeof(buf
), "[<=$%d]", param
);
2089 return alloc_sname(buf
);
2092 static char *range_comparison_to_param_helper(struct expression
*expr
, char starts_with
, int ignore
)
2094 struct symbol
*param
;
2097 char *ret_str
= NULL
;
2104 var
= chunk_to_var(expr
);
2109 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, param
) {
2115 snprintf(buf
, sizeof(buf
), "%s orig", param
->ident
->name
);
2116 compare
= get_comparison_strings(var
, buf
);
2117 if (compare
== UNKNOWN_COMPARISON
||
2118 compare
== IMPOSSIBLE_COMPARISON
)
2120 if (show_comparison(compare
)[0] != starts_with
)
2122 snprintf(buf
, sizeof(buf
), "[%s$%d]", show_comparison(compare
), i
);
2123 ret_str
= alloc_sname(buf
);
2125 } END_FOR_EACH_PTR(param
);
2134 if (starts_with
== '<')
2135 ret_str
= get_mask_comparison(expr
, ignore
);
2139 char *name_sym_to_param_comparison(const char *name
, struct symbol
*sym
)
2141 struct symbol
*param
;
2147 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, param
) {
2151 snprintf(buf
, sizeof(buf
), "%s orig", param
->ident
->name
);
2152 compare
= get_comparison_strings(name
, buf
);
2153 if (compare
== UNKNOWN_COMPARISON
||
2154 compare
== IMPOSSIBLE_COMPARISON
)
2156 snprintf(buf
, sizeof(buf
), "[%s$%d]", show_comparison(compare
), i
);
2157 return alloc_sname(buf
);
2158 } END_FOR_EACH_PTR(param
);
2163 char *expr_equal_to_param(struct expression
*expr
, int ignore
)
2165 return range_comparison_to_param_helper(expr
, '=', ignore
);
2168 char *expr_lte_to_param(struct expression
*expr
, int ignore
)
2170 return range_comparison_to_param_helper(expr
, '<', ignore
);
2173 char *expr_param_comparison(struct expression
*expr
, int ignore
)
2175 struct symbol
*param
;
2178 char *ret_str
= NULL
;
2182 var
= chunk_to_var(expr
);
2187 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, param
) {
2193 snprintf(buf
, sizeof(buf
), "%s orig", param
->ident
->name
);
2194 compare
= get_comparison_strings(var
, buf
);
2197 snprintf(buf
, sizeof(buf
), "[%s$%d]", show_comparison(compare
), i
);
2198 ret_str
= alloc_sname(buf
);
2200 } END_FOR_EACH_PTR(param
);
2207 char *get_printed_param_name(struct expression
*call
, const char *param_name
, struct symbol
*param_sym
)
2209 struct expression
*arg
;
2212 static char buf
[256];
2217 FOR_EACH_PTR(call
->args
, arg
) {
2220 name
= expr_to_var_sym(arg
, &sym
);
2223 if (sym
!= param_sym
)
2227 if (strncmp(name
, param_name
, len
) != 0)
2229 if (param_name
[len
] == '\0') {
2230 snprintf(buf
, sizeof(buf
), "$%d", i
);
2233 if (param_name
[len
] != '-')
2235 snprintf(buf
, sizeof(buf
), "$%d%s", i
, param_name
+ len
);
2237 } END_FOR_EACH_PTR(arg
);
2242 static void match_call_info(struct expression
*expr
)
2244 struct expression
*arg
;
2245 struct smatch_state
*state
;
2246 struct sm_state
*sm
;
2247 struct compare_data
*data
;
2249 struct string_list
*links
;
2251 const char *right_name
;
2257 FOR_EACH_PTR(expr
->args
, arg
) {
2260 state
= get_state_chunk(link_id
, arg
);
2264 links
= state
->data
;
2265 FOR_EACH_PTR(links
, link
) {
2266 struct var_sym_list
*right_vsl
;
2267 struct var_sym
*right_vs
;
2270 if (strstr(link
, " orig"))
2272 sm
= get_sm_state(comparison_id
, link
, NULL
);
2275 data
= sm
->state
->data
;
2277 data
->comparison
== UNKNOWN_COMPARISON
||
2278 data
->comparison
== IMPOSSIBLE_COMPARISON
)
2280 arg_name
= expr_to_var(arg
);
2285 if (strcmp(data
->left_var
, arg_name
) == 0) {
2286 comparison
= data
->comparison
;
2287 right_name
= data
->right_var
;
2288 right_vsl
= data
->right_vsl
;
2289 } else if (strcmp(data
->right_var
, arg_name
) == 0) {
2290 comparison
= flip_comparison(data
->comparison
);
2291 right_name
= data
->left_var
;
2292 right_vsl
= data
->left_vsl
;
2294 if (!right_vsl
|| ptr_list_size((struct ptr_list
*)right_vsl
) != 1)
2297 right_vs
= first_ptr_list((struct ptr_list
*)right_vsl
);
2298 if (strcmp(right_vs
->var
, right_name
) != 0)
2300 right_name
= get_printed_param_name(expr
, right_vs
->var
, right_vs
->sym
);
2303 snprintf(info_buf
, sizeof(info_buf
), "%s %s", show_comparison(comparison
), right_name
);
2304 sql_insert_caller_info(expr
, PARAM_COMPARE
, i
, "$", info_buf
);
2307 free_string(arg_name
);
2308 } END_FOR_EACH_PTR(link
);
2309 } END_FOR_EACH_PTR(arg
);
2312 static void struct_member_callback(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*link_sm
)
2314 struct sm_state
*compare_sm
;
2315 struct string_list
*links
;
2317 struct compare_data
*data
;
2318 struct var_sym
*left
, *right
;
2319 static char info_buf
[256];
2320 const char *right_name
;
2322 if (strstr(printed_name
, " orig"))
2325 links
= link_sm
->state
->data
;
2326 FOR_EACH_PTR(links
, link
) {
2327 compare_sm
= get_sm_state(comparison_id
, link
, NULL
);
2330 data
= compare_sm
->state
->data
;
2331 if (!data
|| !data
->comparison
)
2334 if (ptr_list_size((struct ptr_list
*)data
->left_vsl
) != 1 ||
2335 ptr_list_size((struct ptr_list
*)data
->right_vsl
) != 1)
2337 left
= first_ptr_list((struct ptr_list
*)data
->left_vsl
);
2338 right
= first_ptr_list((struct ptr_list
*)data
->right_vsl
);
2339 if (left
->sym
== right
->sym
&&
2340 strcmp(left
->var
, right
->var
) == 0)
2343 * Both parameters link to this comparison so only
2344 * record the first one.
2346 if (left
->sym
!= link_sm
->sym
||
2347 strcmp(left
->var
, link_sm
->name
) != 0)
2350 right_name
= get_printed_param_name(call
, right
->var
, right
->sym
);
2353 snprintf(info_buf
, sizeof(info_buf
), "%s %s", show_comparison(data
->comparison
), right_name
);
2354 sql_insert_caller_info(call
, PARAM_COMPARE
, param
, printed_name
, info_buf
);
2355 } END_FOR_EACH_PTR(link
);
2358 static void print_return_value_comparison(int return_id
, char *return_ranges
, struct expression
*expr
)
2361 const char *tmp_name
;
2365 bool is_addr
= false;
2368 * TODO: This only prints == comparisons. That's probably the most
2369 * useful comparison because == max has lots of implications. But it
2370 * would be good to capture the rest as well.
2372 * This information is already in the DB but it's in the parameter math
2373 * bits and it's awkward to use it. This is is the simpler, possibly
2374 * cleaner way, but not necessarily the best, I don't know.
2379 name
= expr_to_var_sym(expr
, &sym
);
2383 param
= get_param_num_from_sym(sym
);
2386 if (param_was_set_var_sym(name
, sym
))
2389 tmp_name
= get_param_name_var_sym(name
, sym
);
2393 if (tmp_name
[0] == '&') {
2400 snprintf(info_buf
, sizeof(info_buf
), "== %s$%d%s", is_addr
? "&" : "", param
, tmp_name
);
2401 sql_insert_return_states(return_id
, return_ranges
,
2402 PARAM_COMPARE
, -1, "$", info_buf
);
2407 static void print_return_comparison(int return_id
, char *return_ranges
, struct expression
*expr
)
2409 struct sm_state
*tmp
;
2410 struct string_list
*links
;
2412 struct sm_state
*sm
;
2413 struct compare_data
*data
;
2414 struct var_sym
*left
, *right
;
2415 int left_param
, right_param
;
2416 const char *left_key
, *right_key
;
2419 print_return_value_comparison(return_id
, return_ranges
, expr
);
2421 FOR_EACH_MY_SM(link_id
, __get_cur_stree(), tmp
) {
2422 left_param
= get_param_key_from_var_sym(tmp
->name
, tmp
->sym
, expr
, &left_key
);
2423 if (left_param
< -1 || !left_key
)
2426 links
= tmp
->state
->data
;
2427 FOR_EACH_PTR(links
, link
) {
2428 sm
= get_sm_state(comparison_id
, link
, NULL
);
2431 data
= sm
->state
->data
;
2433 data
->comparison
== UNKNOWN_COMPARISON
||
2434 data
->comparison
== IMPOSSIBLE_COMPARISON
)
2436 if (ptr_list_size((struct ptr_list
*)data
->left_vsl
) != 1 ||
2437 ptr_list_size((struct ptr_list
*)data
->right_vsl
) != 1)
2439 left
= first_ptr_list((struct ptr_list
*)data
->left_vsl
);
2440 right
= first_ptr_list((struct ptr_list
*)data
->right_vsl
);
2441 if (left
->sym
== right
->sym
&&
2442 strcmp(left
->var
, right
->var
) == 0)
2445 * The both foo and bar in the "foo == bar" comparison
2446 * have link states. To avoid duplicates only parse
2447 * the comparison if the link state is on the left.
2449 * And actually let's move getting the left_name outside
2453 if (left
->sym
!= tmp
->sym
||
2454 strcmp(left
->var
, tmp
->name
) != 0)
2457 if (strstr(right
->var
, " orig"))
2460 right_param
= get_param_key_from_var_sym(right
->var
, right
->sym
, expr
, &right_key
);
2461 if (right_param
< 0 || !right_key
|| right_key
[0] != '$')
2465 * The same param can have multiple names so we can get
2468 if (left_param
== right_param
&&
2469 strcmp(left_key
, right_key
) == 0)
2472 snprintf(info_buf
, sizeof(info_buf
), "%s $%d%s",
2473 show_comparison(data
->comparison
), right_param
, right_key
+ 1);
2474 sql_insert_return_states(return_id
, return_ranges
,
2475 PARAM_COMPARE
, left_param
, left_key
, info_buf
);
2476 } END_FOR_EACH_PTR(link
);
2478 } END_FOR_EACH_SM(tmp
);
2481 static int parse_comparison(char **value
, int *op
)
2489 if (**value
== '=') {
2497 *op
= SPECIAL_EQUAL
;
2502 *op
= SPECIAL_NOTEQUAL
;
2506 if (**value
== '=') {
2515 if (**value
!= ' ') {
2516 sm_perror("parsing comparison. %s", *value
);
2524 static int split_op_param_key(char *value
, int *op
, int *param
, char **key
)
2526 static char buf
[256];
2529 if (!parse_comparison(&value
, op
))
2532 snprintf(buf
, sizeof(buf
), "%s", value
);
2539 if (*param
< 0 || *param
> 99)
2551 void select_caller_info(const char *name
, struct symbol
*sym
, char *value
)
2553 struct var_sym_list
*left_vsl
= NULL
;
2554 struct var_sym_list
*right_vsl
= NULL
;
2555 char *right_key
, *p
, *right_name
;
2556 struct symbol
*right_sym
;
2557 char comparison_name
[128];
2558 char right_buf
[128];
2559 int op
, right_param
;
2561 if (!split_op_param_key(value
, &op
, &right_param
, &right_key
))
2564 right_sym
= get_param_sym_from_num(right_param
);
2565 if (!right_sym
|| !right_sym
->ident
)
2568 p
= strchr(right_key
, '$');
2572 snprintf(right_buf
, sizeof(right_buf
), "%.*s%s%s", (int)(p
- right_key
),
2573 right_key
, right_sym
->ident
->name
, p
+ 1);
2574 right_name
= right_buf
;
2576 if (strcmp(name
, right_name
) > 0) {
2577 struct symbol
*tmp_sym
= sym
;
2578 char *tmp_name
= (char *)name
;
2582 right_name
= tmp_name
;
2583 right_sym
= tmp_sym
;
2584 op
= flip_comparison(op
);
2587 add_var_sym(&left_vsl
, name
, sym
);
2588 add_var_sym(&right_vsl
, right_name
, right_sym
);
2590 snprintf(comparison_name
, sizeof(comparison_name
), "%s vs %s", name
, right_name
);
2592 set_state(comparison_id
, comparison_name
, NULL
,
2593 alloc_compare_state(NULL
, name
, left_vsl
,
2595 NULL
, right_name
, right_vsl
));
2598 static void db_return_comparison(struct expression
*expr
, int left_param
, char *key
, char *value
)
2600 struct expression
*left_arg
, *right_arg
;
2601 char *left_name
= NULL
;
2602 struct symbol
*left_sym
;
2603 char *right_name
= NULL
;
2604 struct symbol
*right_sym
;
2608 struct var_sym_list
*left_vsl
= NULL
, *right_vsl
= NULL
;
2610 if (left_param
== -1) {
2611 if (expr
->type
!= EXPR_ASSIGNMENT
)
2613 left_arg
= strip_expr(expr
->left
);
2615 while (expr
->type
== EXPR_ASSIGNMENT
)
2616 expr
= strip_expr(expr
->right
);
2617 if (expr
->type
!= EXPR_CALL
)
2620 while (expr
->type
== EXPR_ASSIGNMENT
)
2621 expr
= strip_expr(expr
->right
);
2622 if (expr
->type
!= EXPR_CALL
)
2625 left_arg
= get_argument_from_call_expr(expr
->args
, left_param
);
2630 if (!split_op_param_key(value
, &op
, &right_param
, &right_key
))
2633 right_arg
= get_argument_from_call_expr(expr
->args
, right_param
);
2637 left_name
= get_variable_from_key(left_arg
, key
, &left_sym
);
2638 if (!left_name
|| !left_sym
)
2641 right_name
= get_variable_from_key(right_arg
, right_key
, &right_sym
);
2642 if (!right_name
|| !right_sym
)
2645 add_var_sym(&left_vsl
, left_name
, left_sym
);
2646 add_var_sym(&right_vsl
, right_name
, right_sym
);
2648 add_comparison_var_sym(NULL
, left_name
, left_vsl
, op
, NULL
, right_name
, right_vsl
);
2651 free_string(left_name
);
2652 free_string(right_name
);
2655 int param_compare_limit_is_impossible(struct expression
*expr
, int left_param
, char *left_key
, char *value
)
2657 char *left_name
= NULL
;
2658 char *right_name
= NULL
;
2659 struct symbol
*left_sym
, *right_sym
;
2660 struct expression
*left_arg
, *right_arg
;
2666 while (expr
->type
== EXPR_ASSIGNMENT
)
2667 expr
= strip_expr(expr
->right
);
2668 if (expr
->type
!= EXPR_CALL
)
2671 if (!split_op_param_key(value
, &op
, &right_param
, &right_key
))
2674 left_arg
= get_argument_from_call_expr(expr
->args
, left_param
);
2678 right_arg
= get_argument_from_call_expr(expr
->args
, right_param
);
2682 left_name
= get_variable_from_key(left_arg
, left_key
, &left_sym
);
2683 right_name
= get_variable_from_key(right_arg
, right_key
, &right_sym
);
2684 if (!left_name
|| !right_name
)
2687 state_op
= get_comparison_strings(left_name
, right_name
);
2691 if (comparison_intersection(remove_unsigned_from_comparison(state_op
), op
)
2692 == IMPOSSIBLE_COMPARISON
)
2695 free_string(left_name
);
2696 free_string(right_name
);
2700 int impossibly_high_comparison(struct expression
*expr
)
2702 struct smatch_state
*link_state
;
2703 struct sm_state
*sm
;
2704 struct string_list
*links
;
2706 struct compare_data
*data
;
2708 link_state
= get_state_expr(link_id
, expr
);
2710 if (expr
->type
== EXPR_BINOP
&&
2711 (impossibly_high_comparison(expr
->left
) ||
2712 impossibly_high_comparison(expr
->right
)))
2717 links
= link_state
->data
;
2718 FOR_EACH_PTR(links
, link
) {
2719 sm
= get_sm_state(comparison_id
, link
, NULL
);
2722 data
= sm
->state
->data
;
2725 if (!possibly_true(data
->left
, data
->comparison
, data
->right
))
2727 } END_FOR_EACH_PTR(link
);
2732 static void free_data(struct symbol
*sym
)
2736 clear_compare_data_alloc();
2739 void register_comparison(int id
)
2742 set_dynamic_states(comparison_id
);
2743 add_hook(&save_start_states
, AFTER_DEF_HOOK
);
2744 add_unmatched_state_hook(comparison_id
, unmatched_comparison
);
2745 add_pre_merge_hook(comparison_id
, &pre_merge_hook
);
2746 add_merge_hook(comparison_id
, &merge_compare_states
);
2747 add_hook(&free_data
, AFTER_FUNC_HOOK
);
2749 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
2750 select_caller_name_sym(&select_caller_info
, PARAM_COMPARE
);
2752 add_split_return_callback(&print_return_comparison
);
2753 select_return_states_hook(PARAM_COMPARE
, &db_return_comparison
);
2755 add_hook(&match_preop
, OP_HOOK
);
2758 void register_comparison_late(int id
)
2760 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
2761 add_return_string_hook(return_str_comparison
);
2764 void register_comparison_links(int id
)
2767 db_ignore_states(link_id
);
2768 set_dynamic_states(link_id
);
2769 add_merge_hook(link_id
, &merge_links
);
2770 add_modification_hook(link_id
, &match_modify
);
2771 add_modification_hook_late(link_id
, match_inc_dec
);
2773 add_member_info_callback(link_id
, struct_member_callback
);
2776 void register_comparison_inc_dec(int id
)
2779 add_modification_hook_late(inc_dec_id
, &iter_modify
);
2782 void register_comparison_inc_dec_links(int id
)
2784 inc_dec_link_id
= id
;
2785 set_dynamic_states(inc_dec_link_id
);
2786 set_up_link_functions(inc_dec_id
, inc_dec_link_id
);
2789 static struct sm_state
*clone_partial_sm(struct sm_state
*sm
, int comparison
)
2791 struct compare_data
*data
;
2792 struct sm_state
*clone
;
2793 struct stree
*stree
;
2795 data
= sm
->state
->data
;
2797 clone
= clone_sm(sm
);
2798 clone
->state
= alloc_compare_state(data
->left
, data
->left_var
, data
->left_vsl
,
2800 data
->right
, data
->right_var
, data
->right_vsl
);
2801 free_slist(&clone
->possible
);
2802 add_possible_sm(clone
, clone
);
2804 stree
= clone_stree(sm
->pool
);
2805 overwrite_sm_state_stree(&stree
, clone
);
2806 clone
->pool
= stree
;
2811 static void create_fake_history(struct sm_state
*sm
, int op
,
2812 struct state_list
**true_stack
,
2813 struct state_list
**false_stack
)
2815 struct sm_state
*true_sm
, *false_sm
;
2816 struct compare_data
*data
;
2817 int true_comparison
;
2818 int false_comparison
;
2820 data
= sm
->state
->data
;
2822 if (is_merged(sm
) || sm
->left
|| sm
->right
)
2825 true_comparison
= comparison_intersection(data
->comparison
, op
);
2826 false_comparison
= comparison_intersection(data
->comparison
, negate_comparison(op
));
2828 true_sm
= clone_partial_sm(sm
, true_comparison
);
2829 false_sm
= clone_partial_sm(sm
, false_comparison
);
2833 sm
->right
= false_sm
;
2835 add_ptr_list(true_stack
, true_sm
);
2836 add_ptr_list(false_stack
, false_sm
);
2839 static void filter_by_sm(struct sm_state
*sm
, int op
,
2840 struct state_list
**true_stack
,
2841 struct state_list
**false_stack
,
2844 struct compare_data
*data
;
2850 data
= sm
->state
->data
;
2853 if (data
->comparison
== IMPOSSIBLE_COMPARISON
)
2857 * We want to check that "data->comparison" is totally inside "op". So
2858 * if data->comparison is < and op is <= then that's true. Or if
2859 * data->comparison is == and op is <= then that's true. But if
2860 * data->comparison is <= and op is < than that's neither true nor
2863 if (data
->comparison
== comparison_intersection(data
->comparison
, op
))
2865 if (data
->comparison
== comparison_intersection(data
->comparison
, negate_comparison(op
)))
2868 if (!is_true
&& !is_false
&& !is_merged(sm
)) {
2869 create_fake_history(sm
, op
, true_stack
, false_stack
);
2873 if (debug_implied()) {
2874 sm_msg("%s: %s: op = '%s' negated '%s'. true_intersect = '%s' false_insersect = '%s' sm = '%s'",
2877 alloc_sname(show_comparison(op
)),
2878 alloc_sname(show_comparison(negate_comparison(op
))),
2879 alloc_sname(show_comparison(comparison_intersection(data
->comparison
, op
))),
2880 alloc_sname(show_comparison(comparison_intersection(data
->comparison
, negate_comparison(op
)))),
2886 add_ptr_list(true_stack
, sm
);
2888 add_ptr_list(false_stack
, sm
);
2890 filter_by_sm(sm
->left
, op
, true_stack
, false_stack
, useful
);
2891 filter_by_sm(sm
->right
, op
, true_stack
, false_stack
, useful
);
2894 struct sm_state
*comparison_implication_hook(struct expression
*expr
,
2895 struct state_list
**true_stack
,
2896 struct state_list
**false_stack
)
2898 struct sm_state
*sm
;
2901 static char buf
[256];
2902 bool useful
= false;
2904 if (expr
->type
!= EXPR_COMPARE
)
2909 left
= expr_to_var(expr
->left
);
2910 right
= expr_to_var(expr
->right
);
2911 if (!left
|| !right
) {
2917 if (strcmp(left
, right
) > 0) {
2922 op
= flip_comparison(op
);
2925 snprintf(buf
, sizeof(buf
), "%s vs %s", left
, right
);
2926 sm
= get_sm_state(comparison_id
, buf
, NULL
);
2932 filter_by_sm(sm
, op
, true_stack
, false_stack
, &useful
);
2936 if (debug_implied())
2937 sm_msg("implications from comparison: (%s)", show_sm(sm
));