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);
592 FOR_EACH_PTR(links
, tmp
) {
594 snprintf(buf
, sizeof(buf
), "%s", tmp
);
596 append(buf
, ", ", sizeof(buf
));
597 append(buf
, tmp
, sizeof(buf
));
599 } END_FOR_EACH_PTR(tmp
);
601 state
->name
= alloc_sname(buf
);
606 static void save_start_states(struct statement
*stmt
)
608 struct symbol
*param
;
610 char state_name
[128];
611 struct smatch_state
*state
;
612 struct string_list
*links
;
615 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, param
) {
616 struct var_sym_list
*left_vsl
= NULL
;
617 struct var_sym_list
*right_vsl
= NULL
;
621 snprintf(orig
, sizeof(orig
), "%s orig", param
->ident
->name
);
622 snprintf(state_name
, sizeof(state_name
), "%s vs %s", param
->ident
->name
, orig
);
623 add_var_sym(&left_vsl
, param
->ident
->name
, param
);
624 add_var_sym(&right_vsl
, orig
, param
);
625 state
= alloc_compare_state(
626 NULL
, param
->ident
->name
, left_vsl
,
628 NULL
, alloc_sname(orig
), right_vsl
);
629 set_state(comparison_id
, state_name
, NULL
, state
);
631 link
= alloc_sname(state_name
);
633 insert_string(&links
, link
);
634 state
= alloc_link_state(links
);
635 set_state(link_id
, param
->ident
->name
, param
, state
);
636 } END_FOR_EACH_PTR(param
);
639 static struct smatch_state
*merge_links(struct smatch_state
*s1
, struct smatch_state
*s2
)
641 struct smatch_state
*ret
;
642 struct string_list
*links
;
644 links
= combine_string_lists(s1
->data
, s2
->data
);
645 ret
= alloc_link_state(links
);
649 static void save_link_var_sym(const char *var
, struct symbol
*sym
, const char *link
)
651 struct smatch_state
*old_state
, *new_state
;
652 struct string_list
*links
;
655 old_state
= get_state(link_id
, var
, sym
);
657 links
= clone_str_list(old_state
->data
);
661 new = alloc_sname(link
);
662 insert_string(&links
, new);
664 new_state
= alloc_link_state(links
);
665 set_state(link_id
, var
, sym
, new_state
);
668 static void match_inc(struct sm_state
*sm
, bool preserve
)
670 struct string_list
*links
;
671 struct smatch_state
*state
, *new;
672 struct compare_data
*data
;
677 links
= sm
->state
->data
;
679 FOR_EACH_PTR(links
, tmp
) {
680 state
= get_state(comparison_id
, tmp
, NULL
);
688 if (strncmp(sm
->name
, tmp
, strlen(sm
->name
)) != 0 ||
689 tmp
[strlen(sm
->name
)] != ' ')
692 op
= state_to_comparison(state
);
694 switch (flip
? flip_comparison(op
) : op
) {
697 case SPECIAL_UNSIGNED_GTE
:
699 case SPECIAL_UNSIGNED_GT
:
702 new = alloc_compare_state(
703 data
->left
, data
->left_var
, data
->left_vsl
,
705 data
->right
, data
->right_var
, data
->right_vsl
);
706 set_state(comparison_id
, tmp
, NULL
, new);
709 case SPECIAL_UNSIGNED_LT
:
710 new = alloc_compare_state(
711 data
->left
, data
->left_var
, data
->left_vsl
,
712 flip
? SPECIAL_GTE
: SPECIAL_LTE
,
713 data
->right
, data
->right_var
, data
->right_vsl
);
714 set_state(comparison_id
, tmp
, NULL
, new);
717 new = alloc_compare_state(
718 data
->left
, data
->left_var
, data
->left_vsl
,
720 data
->right
, data
->right_var
, data
->right_vsl
);
721 set_state(comparison_id
, tmp
, NULL
, new);
723 } END_FOR_EACH_PTR(tmp
);
726 static void match_dec(struct sm_state
*sm
, bool preserve
)
728 struct string_list
*links
;
729 struct smatch_state
*state
;
732 links
= sm
->state
->data
;
734 FOR_EACH_PTR(links
, tmp
) {
735 struct compare_data
*data
;
736 struct smatch_state
*new;
738 state
= get_state(comparison_id
, tmp
, NULL
);
739 if (!state
|| !state
->data
)
744 switch (state_to_comparison(state
)) {
747 case SPECIAL_UNSIGNED_LTE
:
749 case SPECIAL_UNSIGNED_LT
: {
753 new = alloc_compare_state(
754 data
->left
, data
->left_var
, data
->left_vsl
,
756 data
->right
, data
->right_var
, data
->right_vsl
);
757 set_state(comparison_id
, tmp
, NULL
, new);
761 new = alloc_compare_state(
762 data
->left
, data
->left_var
, data
->left_vsl
,
764 data
->right
, data
->right_var
, data
->right_vsl
);
765 set_state(comparison_id
, tmp
, NULL
, new);
767 } END_FOR_EACH_PTR(tmp
);
770 static void reset_sm(struct sm_state
*sm
)
772 struct string_list
*links
;
775 links
= sm
->state
->data
;
777 FOR_EACH_PTR(links
, tmp
) {
778 struct smatch_state
*old
, *new;
780 old
= get_state(comparison_id
, tmp
, NULL
);
781 if (!old
|| !old
->data
) {
784 struct compare_data
*data
= old
->data
;
786 new = alloc_compare_state(
787 data
->left
, data
->left_var
, data
->left_vsl
,
789 data
->right
, data
->right_var
, data
->right_vsl
);
791 set_state(comparison_id
, tmp
, NULL
, new);
792 } END_FOR_EACH_PTR(tmp
);
793 set_state(link_id
, sm
->name
, sm
->sym
, &undefined
);
796 static bool match_add_sub_assign(struct sm_state
*sm
, struct expression
*expr
)
798 struct range_list
*rl
;
799 sval_t zero
= { .type
= &int_ctype
};
801 if (!expr
|| expr
->type
!= EXPR_ASSIGNMENT
)
803 if (expr
->op
!= SPECIAL_ADD_ASSIGN
&& expr
->op
!= SPECIAL_SUB_ASSIGN
)
806 get_absolute_rl(expr
->right
, &rl
);
807 if (sval_is_negative(rl_min(rl
))) {
812 if (expr
->op
== SPECIAL_ADD_ASSIGN
)
813 match_inc(sm
, rl_has_sval(rl
, zero
));
815 match_dec(sm
, rl_has_sval(rl
, zero
));
819 static void match_inc_dec(struct sm_state
*sm
, struct expression
*mod_expr
)
822 * if (foo > bar) then ++foo is also > bar.
826 if (match_add_sub_assign(sm
, mod_expr
))
828 if (mod_expr
->type
!= EXPR_PREOP
&& mod_expr
->type
!= EXPR_POSTOP
)
831 if (mod_expr
->op
== SPECIAL_INCREMENT
)
832 match_inc(sm
, false);
833 else if (mod_expr
->op
== SPECIAL_DECREMENT
)
834 match_dec(sm
, false);
837 static int is_self_assign(struct expression
*expr
)
839 if (!expr
|| expr
->type
!= EXPR_ASSIGNMENT
|| expr
->op
!= '=')
841 return expr_equiv(expr
->left
, expr
->right
);
844 static void match_modify(struct sm_state
*sm
, struct expression
*mod_expr
)
846 if (mod_expr
&& is_self_assign(mod_expr
))
849 /* handled by match_inc_dec() */
851 ((mod_expr
->type
== EXPR_PREOP
|| mod_expr
->type
== EXPR_POSTOP
) &&
852 (mod_expr
->op
== SPECIAL_INCREMENT
|| mod_expr
->op
== SPECIAL_DECREMENT
)))
854 if (mod_expr
&& mod_expr
->type
== EXPR_ASSIGNMENT
&&
855 (mod_expr
->op
== SPECIAL_ADD_ASSIGN
|| mod_expr
->op
== SPECIAL_SUB_ASSIGN
))
861 static void match_preop(struct expression
*expr
)
863 struct expression
*parent
;
864 struct range_list
*left
, *right
;
868 * This is an important special case. Say you have:
872 * Assume that we know the range of limit is higher than the start
873 * value for "j". Then the first thing that we process is the ++j. We
874 * have not comparison states set up so it doesn't get caught by the
875 * modification hook. But it does get caught by smatch_extra which sets
876 * j to unknown then we parse the "j == limit" and sets false to != but
877 * really we want false to be <.
879 * So what we do is we set j < limit here, then the match_modify catches
880 * it and we do a match_inc_dec().
884 if (expr
->type
!= EXPR_PREOP
||
885 (expr
->op
!= SPECIAL_INCREMENT
&& expr
->op
!= SPECIAL_DECREMENT
))
888 parent
= expr_get_parent_expr(expr
);
891 if (parent
->type
!= EXPR_COMPARE
|| parent
->op
!= SPECIAL_EQUAL
)
893 if (parent
->left
!= expr
)
896 if (!get_implied_rl(expr
->unop
, &left
) ||
897 !get_implied_rl(parent
->right
, &right
))
900 op
= rl_comparison(left
, right
);
901 if (op
== UNKNOWN_COMPARISON
)
904 add_comparison(expr
->unop
, op
, parent
->right
);
907 static char *chunk_to_var_sym(struct expression
*expr
, struct symbol
**sym
)
909 expr
= strip_expr(expr
);
915 if (expr
->type
== EXPR_PREOP
&&
916 (expr
->op
== SPECIAL_INCREMENT
||
917 expr
->op
== SPECIAL_DECREMENT
))
918 expr
= strip_expr(expr
->unop
);
920 if (expr
->type
== EXPR_CALL
) {
923 snprintf(buf
, sizeof(buf
), "return %p", expr
);
924 return alloc_string(buf
);
927 return expr_to_chunk_sym_vsl(expr
, sym
, NULL
);
930 static char *chunk_to_var(struct expression
*expr
)
932 return chunk_to_var_sym(expr
, NULL
);
935 static struct smatch_state
*get_state_chunk(int owner
, struct expression
*expr
)
939 struct smatch_state
*ret
;
941 name
= chunk_to_var_sym(expr
, &sym
);
945 ret
= get_state(owner
, name
, sym
);
950 static void save_link(struct expression
*expr
, char *link
)
955 expr
= strip_expr(expr
);
956 if (expr
->type
== EXPR_BINOP
) {
959 chunk
= chunk_to_var(expr
);
963 save_link(expr
->left
, link
);
964 save_link(expr
->right
, link
);
965 save_link_var_sym(chunk
, NULL
, link
);
969 var
= chunk_to_var_sym(expr
, &sym
);
973 save_link_var_sym(var
, sym
, link
);
977 static int get_orig_comparison(struct stree
*pre_stree
, const char *left
, const char *right
)
979 struct smatch_state
*state
;
980 struct compare_data
*data
;
982 char state_name
[256];
984 if (strcmp(left
, right
) > 0) {
985 const char *tmp
= right
;
992 snprintf(state_name
, sizeof(state_name
), "%s vs %s", left
, right
);
993 state
= get_state_stree(pre_stree
, comparison_id
, state_name
, NULL
);
994 if (!state
|| !state
->data
)
998 return flip_comparison(data
->comparison
);
999 return data
->comparison
;
1003 static int have_common_var_sym(struct var_sym_list
*left_vsl
, struct var_sym_list
*right_vsl
)
1005 struct var_sym
*tmp
;
1007 FOR_EACH_PTR(left_vsl
, tmp
) {
1008 if (in_var_sym_list(right_vsl
, tmp
->var
, tmp
->sym
))
1010 } END_FOR_EACH_PTR(tmp
);
1016 * The idea here is that we take a comparison "a < b" and then we look at all
1017 * the things which "b" is compared against "b < c" and we say that that implies
1018 * a relationship "a < c".
1020 * The names here about because the comparisons are organized like this
1024 static void update_tf_links(struct stree
*pre_stree
,
1025 struct expression
*left_expr
,
1026 const char *left_var
, struct var_sym_list
*left_vsl
,
1027 int left_comparison
, int left_false_comparison
,
1028 const char *mid_var
, struct var_sym_list
*mid_vsl
,
1029 struct string_list
*links
)
1031 struct smatch_state
*state
;
1032 struct smatch_state
*true_state
, *false_state
;
1033 struct compare_data
*data
;
1034 struct expression
*right_expr
;
1035 const char *right_var
;
1036 struct var_sym_list
*right_vsl
;
1037 int orig_comparison
;
1038 int right_comparison
;
1039 int true_comparison
;
1040 int false_comparison
;
1042 char state_name
[256];
1045 FOR_EACH_PTR(links
, tmp
) {
1046 state
= get_state_stree(pre_stree
, comparison_id
, tmp
, NULL
);
1047 if (!state
|| !state
->data
)
1050 right_comparison
= data
->comparison
;
1051 right_expr
= data
->right
;
1052 right_var
= data
->right_var
;
1053 right_vsl
= data
->right_vsl
;
1054 if (strcmp(mid_var
, right_var
) == 0) {
1055 right_expr
= data
->left
;
1056 right_var
= data
->left_var
;
1057 right_vsl
= data
->left_vsl
;
1058 right_comparison
= flip_comparison(right_comparison
);
1060 if (have_common_var_sym(left_vsl
, right_vsl
))
1063 orig_comparison
= get_orig_comparison(pre_stree
, left_var
, right_var
);
1065 true_comparison
= combine_comparisons(left_comparison
, right_comparison
);
1066 false_comparison
= combine_comparisons(left_false_comparison
, right_comparison
);
1068 true_comparison
= comparison_intersection(orig_comparison
, true_comparison
);
1069 false_comparison
= comparison_intersection(orig_comparison
, false_comparison
);
1071 if (strcmp(left_var
, right_var
) > 0) {
1072 struct expression
*tmp_expr
= left_expr
;
1073 const char *tmp_var
= left_var
;
1074 struct var_sym_list
*tmp_vsl
= left_vsl
;
1076 left_expr
= right_expr
;
1077 left_var
= right_var
;
1078 left_vsl
= right_vsl
;
1079 right_expr
= tmp_expr
;
1080 right_var
= tmp_var
;
1081 right_vsl
= tmp_vsl
;
1082 true_comparison
= flip_comparison(true_comparison
);
1083 false_comparison
= flip_comparison(false_comparison
);
1086 if (!true_comparison
&& !false_comparison
)
1089 if (true_comparison
)
1090 true_state
= alloc_compare_state(
1091 left_expr
, left_var
, left_vsl
,
1093 right_expr
, right_var
, right_vsl
);
1096 if (false_comparison
)
1097 false_state
= alloc_compare_state(
1098 left_expr
, left_var
, left_vsl
,
1100 right_expr
, right_var
, right_vsl
);
1104 snprintf(state_name
, sizeof(state_name
), "%s vs %s", left_var
, right_var
);
1105 set_true_false_states(comparison_id
, state_name
, NULL
, true_state
, false_state
);
1106 FOR_EACH_PTR(left_vsl
, vs
) {
1107 save_link_var_sym(vs
->var
, vs
->sym
, state_name
);
1108 } END_FOR_EACH_PTR(vs
);
1109 FOR_EACH_PTR(right_vsl
, vs
) {
1110 save_link_var_sym(vs
->var
, vs
->sym
, state_name
);
1111 } END_FOR_EACH_PTR(vs
);
1112 if (!vsl_to_sym(left_vsl
))
1113 save_link_var_sym(left_var
, NULL
, state_name
);
1114 if (!vsl_to_sym(right_vsl
))
1115 save_link_var_sym(right_var
, NULL
, state_name
);
1116 } END_FOR_EACH_PTR(tmp
);
1119 static void update_tf_data(struct stree
*pre_stree
,
1120 struct expression
*left_expr
,
1121 const char *left_name
, struct var_sym_list
*left_vsl
,
1122 struct expression
*right_expr
,
1123 const char *right_name
, struct var_sym_list
*right_vsl
,
1124 int true_comparison
, int false_comparison
)
1126 struct smatch_state
*state
;
1128 state
= get_state_stree(pre_stree
, link_id
, right_name
, vsl_to_sym(right_vsl
));
1130 update_tf_links(pre_stree
, left_expr
, left_name
, left_vsl
, true_comparison
, false_comparison
, right_name
, right_vsl
, state
->data
);
1132 state
= get_state_stree(pre_stree
, link_id
, left_name
, vsl_to_sym(left_vsl
));
1134 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
);
1137 static void iter_modify(struct sm_state
*sm
, struct expression
*mod_expr
)
1139 if (sm
->state
!= &start
||
1141 (mod_expr
->type
!= EXPR_PREOP
&& mod_expr
->type
!= EXPR_POSTOP
) ||
1142 mod_expr
->op
!= SPECIAL_INCREMENT
)
1143 set_state(inc_dec_id
, sm
->name
, sm
->sym
, &undefined
);
1145 set_state(inc_dec_id
, sm
->name
, sm
->sym
, &incremented
);
1148 static void handle_for_loops(struct expression
*expr
, char *state_name
, struct smatch_state
*false_state
)
1151 char *iter_name
, *cap_name
;
1152 struct symbol
*iter_sym
, *cap_sym
;
1153 struct compare_data
*data
;
1155 if (expr
->op
!= '<' && expr
->op
!= SPECIAL_UNSIGNED_LT
)
1158 if (!__cur_stmt
|| !__prev_stmt
)
1160 if (__cur_stmt
->type
!= STMT_ITERATOR
)
1162 if (__cur_stmt
->iterator_pre_condition
!= expr
)
1165 /* literals are handled in smatch_extra.c */
1166 if (get_value(expr
->right
, &sval
))
1169 /* First time checking the condition */
1170 if (__prev_stmt
== __cur_stmt
->iterator_pre_statement
) {
1171 if (!get_implied_value(expr
->left
, &sval
) ||
1175 iter_name
= expr_to_var_sym(expr
->left
, &iter_sym
);
1176 cap_name
= expr_to_var_sym(expr
->right
, &cap_sym
);
1177 if (!iter_name
|| !cap_name
|| !iter_sym
|| !cap_sym
) {
1178 free_string(iter_name
);
1179 free_string(cap_name
);
1183 set_state(inc_dec_id
, iter_name
, iter_sym
, &start
);
1184 store_link(inc_dec_link_id
, cap_name
, cap_sym
, iter_name
, iter_sym
);
1186 free_string(iter_name
);
1187 free_string(cap_name
);
1191 /* Second time checking the condtion */
1192 if (__prev_stmt
!= __cur_stmt
->iterator_post_statement
)
1195 if (get_state_chunk(inc_dec_id
, expr
->left
) != &incremented
)
1198 data
= false_state
->data
;
1199 false_state
= alloc_compare_state(
1200 data
->left
, data
->left_var
, data
->left_vsl
,
1202 data
->right
, data
->right_var
, data
->right_vsl
);
1204 // FIXME: This doesn't handle links correct so it doesn't set "param orig"
1205 set_true_false_states(comparison_id
, state_name
, NULL
, NULL
, false_state
);
1208 static int is_plus_one(struct expression
*expr
)
1212 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '+')
1214 if (!get_implied_value(expr
->right
, &sval
) || sval
.value
!= 1)
1219 static int is_minus_one(struct expression
*expr
)
1223 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '-')
1225 if (!get_implied_value(expr
->right
, &sval
) || sval
.value
!= 1)
1230 static void move_plus_to_minus_helper(struct expression
**left_p
, struct expression
**right_p
)
1232 struct expression
*left
= *left_p
;
1233 struct expression
*right
= *right_p
;
1236 * These two are basically equivalent: "foo + 1 != bar" and
1237 * "foo != bar - 1". There are issues with signedness and integer
1238 * overflows. There are also issues with type as well. But let's
1239 * pretend we can ignore all that stuff for now.
1243 if (!is_plus_one(left
))
1246 *left_p
= left
->left
;
1247 *right_p
= binop_expression(right
, '-', left
->right
);
1250 static void move_plus_to_minus(struct expression
**left_p
, struct expression
**right_p
)
1252 if (is_plus_one(*left_p
) && is_plus_one(*right_p
))
1255 move_plus_to_minus_helper(left_p
, right_p
);
1256 move_plus_to_minus_helper(right_p
, left_p
);
1259 static void handle_comparison(struct expression
*left_expr
, int op
, struct expression
*right_expr
, char **_state_name
, struct smatch_state
**_false_state
)
1263 struct symbol
*left_sym
, *right_sym
;
1264 struct var_sym_list
*left_vsl
= NULL
;
1265 struct var_sym_list
*right_vsl
= NULL
;
1267 int orig_comparison
;
1268 struct smatch_state
*true_state
, *false_state
;
1269 static char state_name
[256];
1270 struct stree
*pre_stree
;
1273 if (!left_expr
|| !right_expr
)
1276 left_expr
= strip_parens(left_expr
);
1277 right_expr
= strip_parens(right_expr
);
1279 while (left_expr
->type
== EXPR_ASSIGNMENT
)
1280 left_expr
= strip_parens(left_expr
->left
);
1281 while (right_expr
->type
== EXPR_ASSIGNMENT
)
1282 right_expr
= strip_parens(right_expr
->left
);
1284 false_op
= negate_comparison(op
);
1286 move_plus_to_minus(&left_expr
, &right_expr
);
1288 if (op
== SPECIAL_UNSIGNED_LT
&&
1289 get_implied_value(left_expr
, &sval
) &&
1291 false_op
= SPECIAL_EQUAL
;
1293 if (op
== SPECIAL_UNSIGNED_GT
&&
1294 get_implied_value(right_expr
, &sval
) &&
1296 false_op
= SPECIAL_EQUAL
;
1298 left
= chunk_to_var_sym(left_expr
, &left_sym
);
1302 add_var_sym(&left_vsl
, left
, left_sym
);
1304 left_vsl
= expr_to_vsl(left_expr
);
1305 right
= chunk_to_var_sym(right_expr
, &right_sym
);
1309 add_var_sym(&right_vsl
, right
, right_sym
);
1311 right_vsl
= expr_to_vsl(right_expr
);
1313 if (strcmp(left
, right
) > 0) {
1314 char *tmp_name
= left
;
1315 struct var_sym_list
*tmp_vsl
= left_vsl
;
1316 struct expression
*tmp_expr
= left_expr
;
1319 left_vsl
= right_vsl
;
1320 left_expr
= right_expr
;
1322 right_vsl
= tmp_vsl
;
1323 right_expr
= tmp_expr
;
1324 op
= flip_comparison(op
);
1325 false_op
= flip_comparison(false_op
);
1328 orig_comparison
= get_comparison(left_expr
, right_expr
);
1329 op
= comparison_intersection(orig_comparison
, op
);
1330 false_op
= comparison_intersection(orig_comparison
, false_op
);
1332 snprintf(state_name
, sizeof(state_name
), "%s vs %s", left
, right
);
1333 true_state
= alloc_compare_state(
1334 left_expr
, left
, left_vsl
,
1336 right_expr
, right
, right_vsl
);
1337 false_state
= alloc_compare_state(
1338 left_expr
, left
, left_vsl
,
1340 right_expr
, right
, right_vsl
);
1342 pre_stree
= clone_stree(__get_cur_stree());
1343 update_tf_data(pre_stree
, left_expr
, left
, left_vsl
, right_expr
, right
, right_vsl
, op
, false_op
);
1344 free_stree(&pre_stree
);
1346 set_true_false_states(comparison_id
, state_name
, NULL
, true_state
, false_state
);
1347 __compare_param_limit_hook(left_expr
, right_expr
, state_name
, true_state
, false_state
);
1348 save_link(left_expr
, state_name
);
1349 save_link(right_expr
, state_name
);
1352 *_false_state
= false_state
;
1354 *_state_name
= state_name
;
1360 void __comparison_match_condition(struct expression
*expr
)
1362 struct expression
*left
, *right
, *new_left
, *new_right
, *tmp
;
1363 struct smatch_state
*false_state
= NULL
;
1364 char *state_name
= NULL
;
1367 if (expr
->type
!= EXPR_COMPARE
)
1370 handle_comparison(expr
->left
, expr
->op
, expr
->right
, &state_name
, &false_state
);
1371 if (false_state
&& state_name
)
1372 handle_for_loops(expr
, state_name
, false_state
);
1374 left
= strip_parens(expr
->left
);
1375 right
= strip_parens(expr
->right
);
1377 if (left
->type
== EXPR_BINOP
&& left
->op
== '+') {
1378 new_left
= left
->left
;
1379 new_right
= binop_expression(right
, '-', left
->right
);
1380 handle_comparison(new_left
, expr
->op
, new_right
, NULL
, NULL
);
1382 new_left
= left
->right
;
1383 new_right
= binop_expression(right
, '-', left
->left
);
1384 handle_comparison(new_left
, expr
->op
, new_right
, NULL
, NULL
);
1388 left
= strip_parens(expr
->left
);
1389 right
= strip_parens(expr
->right
);
1390 if (get_last_expr_from_expression_stmt(expr
->left
)) {
1391 left
= get_last_expr_from_expression_stmt(expr
->left
);
1394 if (get_last_expr_from_expression_stmt(expr
->right
)) {
1395 right
= get_last_expr_from_expression_stmt(expr
->right
);
1403 while ((tmp
= get_assigned_expr(left
))) {
1406 left
= strip_expr(tmp
);
1409 while ((tmp
= get_assigned_expr(right
))) {
1412 right
= strip_expr(tmp
);
1415 handle_comparison(left
, expr
->op
, right
, NULL
, NULL
);
1418 static void add_comparison_var_sym(
1419 struct expression
*left_expr
,
1420 const char *left_name
, struct var_sym_list
*left_vsl
,
1422 struct expression
*right_expr
,
1423 const char *right_name
, struct var_sym_list
*right_vsl
)
1425 struct smatch_state
*state
;
1427 char state_name
[256];
1429 if (strcmp(left_name
, right_name
) > 0) {
1430 struct expression
*tmp_expr
= left_expr
;
1431 const char *tmp_name
= left_name
;
1432 struct var_sym_list
*tmp_vsl
= left_vsl
;
1434 left_expr
= right_expr
;
1435 left_name
= right_name
;
1436 left_vsl
= right_vsl
;
1437 right_expr
= tmp_expr
;
1438 right_name
= tmp_name
;
1439 right_vsl
= tmp_vsl
;
1440 comparison
= flip_comparison(comparison
);
1442 snprintf(state_name
, sizeof(state_name
), "%s vs %s", left_name
, right_name
);
1443 state
= alloc_compare_state(
1444 left_expr
, left_name
, left_vsl
,
1446 right_expr
, right_name
, right_vsl
);
1448 set_state(comparison_id
, state_name
, NULL
, state
);
1450 FOR_EACH_PTR(left_vsl
, vs
) {
1451 save_link_var_sym(vs
->var
, vs
->sym
, state_name
);
1452 } END_FOR_EACH_PTR(vs
);
1453 FOR_EACH_PTR(right_vsl
, vs
) {
1454 save_link_var_sym(vs
->var
, vs
->sym
, state_name
);
1455 } END_FOR_EACH_PTR(vs
);
1458 static void add_comparison(struct expression
*left
, int comparison
, struct expression
*right
)
1460 char *left_name
= NULL
;
1461 char *right_name
= NULL
;
1462 struct symbol
*left_sym
, *right_sym
;
1463 struct var_sym_list
*left_vsl
, *right_vsl
;
1464 struct smatch_state
*state
;
1465 char state_name
[256];
1467 left_name
= chunk_to_var_sym(left
, &left_sym
);
1470 left_vsl
= expr_to_vsl(left
);
1471 right_name
= chunk_to_var_sym(right
, &right_sym
);
1474 right_vsl
= expr_to_vsl(right
);
1476 if (strcmp(left_name
, right_name
) > 0) {
1477 struct expression
*tmp_expr
= left
;
1478 struct symbol
*tmp_sym
= left_sym
;
1479 char *tmp_name
= left_name
;
1480 struct var_sym_list
*tmp_vsl
= left_vsl
;
1483 left_name
= right_name
;
1484 left_sym
= right_sym
;
1485 left_vsl
= right_vsl
;
1487 right_name
= tmp_name
;
1488 right_sym
= tmp_sym
;
1489 right_vsl
= tmp_vsl
;
1490 comparison
= flip_comparison(comparison
);
1492 snprintf(state_name
, sizeof(state_name
), "%s vs %s", left_name
, right_name
);
1493 state
= alloc_compare_state(
1494 left
, left_name
, left_vsl
,
1496 right
, right_name
, right_vsl
);
1498 set_state(comparison_id
, state_name
, NULL
, state
);
1499 save_link(left
, state_name
);
1500 save_link(right
, state_name
);
1503 free_string(left_name
);
1504 free_string(right_name
);
1507 static void match_assign_add(struct expression
*expr
)
1509 struct expression
*right
;
1510 struct expression
*r_left
, *r_right
;
1511 sval_t left_tmp
, right_tmp
;
1513 right
= strip_expr(expr
->right
);
1514 r_left
= strip_expr(right
->left
);
1515 r_right
= strip_expr(right
->right
);
1517 get_absolute_min(r_left
, &left_tmp
);
1518 get_absolute_min(r_right
, &right_tmp
);
1520 if (left_tmp
.value
> 0)
1521 add_comparison(expr
->left
, '>', r_right
);
1522 else if (left_tmp
.value
== 0)
1523 add_comparison(expr
->left
, SPECIAL_GTE
, r_right
);
1525 if (right_tmp
.value
> 0)
1526 add_comparison(expr
->left
, '>', r_left
);
1527 else if (right_tmp
.value
== 0)
1528 add_comparison(expr
->left
, SPECIAL_GTE
, r_left
);
1531 static void match_assign_sub(struct expression
*expr
)
1533 struct expression
*right
;
1534 struct expression
*r_left
, *r_right
;
1538 right
= strip_expr(expr
->right
);
1539 r_left
= strip_expr(right
->left
);
1540 r_right
= strip_expr(right
->right
);
1542 if (get_absolute_min(r_right
, &min
) && sval_is_negative(min
))
1545 comparison
= get_comparison(r_left
, r_right
);
1547 switch (comparison
) {
1550 if (implied_not_equal(r_right
, 0))
1551 add_comparison(expr
->left
, '>', r_left
);
1553 add_comparison(expr
->left
, SPECIAL_GTE
, r_left
);
1558 static void match_assign_divide(struct expression
*expr
)
1560 struct expression
*right
;
1561 struct expression
*r_left
, *r_right
;
1564 right
= strip_expr(expr
->right
);
1565 r_left
= strip_expr(right
->left
);
1566 r_right
= strip_expr(right
->right
);
1567 if (!get_implied_min(r_right
, &min
) || min
.value
<= 1)
1570 add_comparison(expr
->left
, '<', r_left
);
1573 static void match_binop_assign(struct expression
*expr
)
1575 struct expression
*right
;
1577 right
= strip_expr(expr
->right
);
1578 if (right
->op
== '+')
1579 match_assign_add(expr
);
1580 if (right
->op
== '-')
1581 match_assign_sub(expr
);
1582 if (right
->op
== '/')
1583 match_assign_divide(expr
);
1586 static void copy_comparisons(struct expression
*left
, struct expression
*right
)
1588 struct string_list
*links
;
1589 struct smatch_state
*state
;
1590 struct compare_data
*data
;
1591 struct symbol
*left_sym
, *right_sym
;
1592 char *left_var
= NULL
;
1593 char *right_var
= NULL
;
1594 struct var_sym_list
*left_vsl
;
1595 struct expression
*expr
;
1597 struct var_sym_list
*vsl
;
1601 left_var
= chunk_to_var_sym(left
, &left_sym
);
1604 left_vsl
= expr_to_vsl(left
);
1605 right_var
= chunk_to_var_sym(right
, &right_sym
);
1609 state
= get_state(link_id
, right_var
, right_sym
);
1612 links
= state
->data
;
1614 FOR_EACH_PTR(links
, tmp
) {
1615 state
= get_state(comparison_id
, tmp
, NULL
);
1616 if (!state
|| !state
->data
)
1619 comparison
= data
->comparison
;
1621 var
= data
->right_var
;
1622 vsl
= data
->right_vsl
;
1623 if (strcmp(var
, right_var
) == 0) {
1625 var
= data
->left_var
;
1626 vsl
= data
->left_vsl
;
1627 comparison
= flip_comparison(comparison
);
1629 /* n = copy_from_user(dest, src, n); leads to n <= n which is nonsense */
1630 if (strcmp(left_var
, var
) == 0)
1632 add_comparison_var_sym(left
, left_var
, left_vsl
, comparison
, expr
, var
, vsl
);
1633 } END_FOR_EACH_PTR(tmp
);
1636 free_string(right_var
);
1639 static void match_assign(struct expression
*expr
)
1641 struct expression
*right
;
1643 if (expr
->op
!= '=')
1645 if (__in_fake_assign
|| outside_of_function())
1648 if (is_struct(expr
->left
))
1651 if (is_self_assign(expr
))
1654 copy_comparisons(expr
->left
, expr
->right
);
1655 add_comparison(expr
->left
, SPECIAL_EQUAL
, expr
->right
);
1657 right
= strip_expr(expr
->right
);
1658 if (right
->type
== EXPR_BINOP
)
1659 match_binop_assign(expr
);
1662 int get_comparison_strings(const char *one
, const char *two
)
1665 struct smatch_state
*state
;
1670 return UNKNOWN_COMPARISON
;
1672 if (strcmp(one
, two
) == 0)
1673 return SPECIAL_EQUAL
;
1675 if (strcmp(one
, two
) > 0) {
1676 const char *tmp
= one
;
1683 snprintf(buf
, sizeof(buf
), "%s vs %s", one
, two
);
1684 state
= get_state(comparison_id
, buf
, NULL
);
1686 ret
= state_to_comparison(state
);
1689 ret
= flip_comparison(ret
);
1694 static int get_comparison_helper(struct expression
*a
, struct expression
*b
, bool use_extra
)
1698 int ret
= UNKNOWN_COMPARISON
;
1699 int extra
= UNKNOWN_COMPARISON
;
1701 if (a
== UNKNOWN_COMPARISON
||
1702 b
== UNKNOWN_COMPARISON
)
1703 return UNKNOWN_COMPARISON
;
1705 a
= strip_parens(a
);
1706 b
= strip_parens(b
);
1708 move_plus_to_minus(&a
, &b
);
1710 one
= chunk_to_var(a
);
1713 two
= chunk_to_var(b
);
1717 ret
= get_comparison_strings(one
, two
);
1721 if (is_plus_one(a
) || is_minus_one(a
)) {
1723 one
= chunk_to_var(a
->left
);
1724 ret
= get_comparison_strings(one
, two
);
1725 } else if (is_plus_one(b
) || is_minus_one(b
)) {
1727 two
= chunk_to_var(b
->left
);
1728 ret
= get_comparison_strings(one
, two
);
1731 if (ret
== UNKNOWN_COMPARISON
)
1734 if ((is_plus_one(a
) || is_minus_one(b
)) && ret
== '<')
1736 else if ((is_minus_one(a
) || is_plus_one(b
)) && ret
== '>')
1739 ret
= UNKNOWN_COMPARISON
;
1745 extra
= comparison_from_extra(a
, b
);
1746 return comparison_intersection(ret
, extra
);
1749 int get_comparison(struct expression
*a
, struct expression
*b
)
1751 return get_comparison_helper(a
, b
, true);
1754 int get_comparison_no_extra(struct expression
*a
, struct expression
*b
)
1756 return get_comparison_helper(a
, b
, false);
1759 int possible_comparison(struct expression
*a
, int comparison
, struct expression
*b
)
1765 struct sm_state
*sm
;
1768 one
= chunk_to_var(a
);
1771 two
= chunk_to_var(b
);
1776 if (strcmp(one
, two
) == 0 && comparison
== SPECIAL_EQUAL
) {
1781 if (strcmp(one
, two
) > 0) {
1786 comparison
= flip_comparison(comparison
);
1789 snprintf(buf
, sizeof(buf
), "%s vs %s", one
, two
);
1790 sm
= get_sm_state(comparison_id
, buf
, NULL
);
1794 FOR_EACH_PTR(sm
->possible
, sm
) {
1795 if (!sm
->state
->data
)
1797 saved
= ((struct compare_data
*)sm
->state
->data
)->comparison
;
1798 if (saved
== comparison
)
1800 if (comparison
== SPECIAL_EQUAL
&&
1801 (saved
== SPECIAL_LTE
||
1802 saved
== SPECIAL_GTE
||
1803 saved
== SPECIAL_UNSIGNED_LTE
||
1804 saved
== SPECIAL_UNSIGNED_GTE
))
1808 } END_FOR_EACH_PTR(sm
);
1817 struct state_list
*get_all_comparisons(struct expression
*expr
)
1819 struct smatch_state
*state
;
1820 struct string_list
*links
;
1821 struct state_list
*ret
= NULL
;
1822 struct sm_state
*sm
;
1825 state
= get_state_chunk(link_id
, expr
);
1828 links
= state
->data
;
1830 FOR_EACH_PTR(links
, tmp
) {
1831 sm
= get_sm_state(comparison_id
, tmp
, NULL
);
1834 // FIXME have to compare name with vsl
1835 add_ptr_list(&ret
, sm
);
1836 } END_FOR_EACH_PTR(tmp
);
1841 struct state_list
*get_all_possible_equal_comparisons(struct expression
*expr
)
1843 struct smatch_state
*state
;
1844 struct string_list
*links
;
1845 struct state_list
*ret
= NULL
;
1846 struct sm_state
*sm
;
1849 state
= get_state_chunk(link_id
, expr
);
1852 links
= state
->data
;
1854 FOR_EACH_PTR(links
, tmp
) {
1855 sm
= get_sm_state(comparison_id
, tmp
, NULL
);
1858 if (!strchr(sm
->state
->name
, '='))
1860 if (strcmp(sm
->state
->name
, "!=") == 0)
1862 add_ptr_list(&ret
, sm
);
1863 } END_FOR_EACH_PTR(tmp
);
1868 struct state_list
*get_all_possible_not_equal_comparisons(struct expression
*expr
)
1870 struct smatch_state
*state
;
1871 struct string_list
*links
;
1872 struct state_list
*ret
= NULL
;
1873 struct sm_state
*sm
;
1874 struct sm_state
*possible
;
1879 state
= get_state_chunk(link_id
, expr
);
1882 links
= state
->data
;
1884 FOR_EACH_PTR(links
, link
) {
1885 sm
= get_sm_state(comparison_id
, link
, NULL
);
1888 FOR_EACH_PTR(sm
->possible
, possible
) {
1889 if (strcmp(possible
->state
->name
, "!=") != 0)
1891 add_ptr_list(&ret
, sm
);
1893 } END_FOR_EACH_PTR(link
);
1894 } END_FOR_EACH_PTR(link
);
1899 static void update_links_from_call(struct expression
*left
,
1901 struct expression
*right
)
1903 struct string_list
*links
;
1904 struct smatch_state
*state
;
1905 struct compare_data
*data
;
1906 struct symbol
*left_sym
, *right_sym
;
1907 char *left_var
= NULL
;
1908 char *right_var
= NULL
;
1909 struct var_sym_list
*left_vsl
;
1910 struct expression
*expr
;
1912 struct var_sym_list
*vsl
;
1916 left_var
= chunk_to_var_sym(left
, &left_sym
);
1919 left_vsl
= expr_to_vsl(left
);
1920 right_var
= chunk_to_var_sym(right
, &right_sym
);
1924 state
= get_state(link_id
, right_var
, right_sym
);
1927 links
= state
->data
;
1929 FOR_EACH_PTR(links
, tmp
) {
1930 state
= get_state(comparison_id
, tmp
, NULL
);
1931 if (!state
|| !state
->data
)
1934 comparison
= data
->comparison
;
1936 var
= data
->right_var
;
1937 vsl
= data
->right_vsl
;
1938 if (strcmp(var
, right_var
) == 0) {
1940 var
= data
->left_var
;
1941 vsl
= data
->left_vsl
;
1942 comparison
= flip_comparison(comparison
);
1944 comparison
= combine_comparisons(left_compare
, comparison
);
1947 add_comparison_var_sym(left
, left_var
, left_vsl
, comparison
, expr
, var
, vsl
);
1948 } END_FOR_EACH_PTR(tmp
);
1951 free_string(right_var
);
1954 void __add_return_comparison(struct expression
*call
, const char *range
)
1956 struct expression
*arg
;
1960 if (!str_to_comparison_arg(range
, call
, &comparison
, &arg
))
1962 snprintf(buf
, sizeof(buf
), "%s", show_comparison(comparison
));
1963 update_links_from_call(call
, comparison
, arg
);
1964 add_comparison(call
, comparison
, arg
);
1967 void __add_comparison_info(struct expression
*expr
, struct expression
*call
, const char *range
)
1969 copy_comparisons(expr
, call
);
1972 static char *get_mask_comparison(struct expression
*expr
, int ignore
)
1974 struct expression
*tmp
, *right
;
1978 /* The return value for "return foo & param;" is <= param */
1981 while ((tmp
= get_assigned_expr(expr
))) {
1982 expr
= strip_expr(tmp
);
1987 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '&')
1990 right
= strip_expr(expr
->right
);
1991 param
= get_param_num(right
);
1992 if (param
< 0 || param
== ignore
)
1995 snprintf(buf
, sizeof(buf
), "[<=$%d]", param
);
1996 return alloc_sname(buf
);
1999 static char *range_comparison_to_param_helper(struct expression
*expr
, char starts_with
, int ignore
)
2001 struct symbol
*param
;
2004 char *ret_str
= NULL
;
2011 var
= chunk_to_var(expr
);
2016 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, param
) {
2022 snprintf(buf
, sizeof(buf
), "%s orig", param
->ident
->name
);
2023 compare
= get_comparison_strings(var
, buf
);
2024 if (compare
== UNKNOWN_COMPARISON
||
2025 compare
== IMPOSSIBLE_COMPARISON
)
2027 if (show_comparison(compare
)[0] != starts_with
)
2029 snprintf(buf
, sizeof(buf
), "[%s$%d]", show_comparison(compare
), i
);
2030 ret_str
= alloc_sname(buf
);
2032 } END_FOR_EACH_PTR(param
);
2041 if (starts_with
== '<')
2042 ret_str
= get_mask_comparison(expr
, ignore
);
2046 char *name_sym_to_param_comparison(const char *name
, struct symbol
*sym
)
2048 struct symbol
*param
;
2054 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, param
) {
2058 snprintf(buf
, sizeof(buf
), "%s orig", param
->ident
->name
);
2059 compare
= get_comparison_strings(name
, buf
);
2060 if (compare
== UNKNOWN_COMPARISON
||
2061 compare
== IMPOSSIBLE_COMPARISON
)
2063 snprintf(buf
, sizeof(buf
), "[%s$%d]", show_comparison(compare
), i
);
2064 return alloc_sname(buf
);
2065 } END_FOR_EACH_PTR(param
);
2070 char *expr_equal_to_param(struct expression
*expr
, int ignore
)
2072 return range_comparison_to_param_helper(expr
, '=', ignore
);
2075 char *expr_lte_to_param(struct expression
*expr
, int ignore
)
2077 return range_comparison_to_param_helper(expr
, '<', ignore
);
2080 char *expr_param_comparison(struct expression
*expr
, int ignore
)
2082 struct symbol
*param
;
2085 char *ret_str
= NULL
;
2089 var
= chunk_to_var(expr
);
2094 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, param
) {
2100 snprintf(buf
, sizeof(buf
), "%s orig", param
->ident
->name
);
2101 compare
= get_comparison_strings(var
, buf
);
2104 snprintf(buf
, sizeof(buf
), "[%s$%d]", show_comparison(compare
), i
);
2105 ret_str
= alloc_sname(buf
);
2107 } END_FOR_EACH_PTR(param
);
2114 char *get_printed_param_name(struct expression
*call
, const char *param_name
, struct symbol
*param_sym
)
2116 struct expression
*arg
;
2119 static char buf
[256];
2124 FOR_EACH_PTR(call
->args
, arg
) {
2127 name
= expr_to_var_sym(arg
, &sym
);
2130 if (sym
!= param_sym
)
2134 if (strncmp(name
, param_name
, len
) != 0)
2136 if (param_name
[len
] == '\0') {
2137 snprintf(buf
, sizeof(buf
), "$%d", i
);
2140 if (param_name
[len
] != '-')
2142 snprintf(buf
, sizeof(buf
), "$%d%s", i
, param_name
+ len
);
2144 } END_FOR_EACH_PTR(arg
);
2149 static void match_call_info(struct expression
*expr
)
2151 struct expression
*arg
;
2152 struct smatch_state
*state
;
2153 struct sm_state
*sm
;
2154 struct compare_data
*data
;
2156 struct string_list
*links
;
2158 const char *right_name
;
2164 FOR_EACH_PTR(expr
->args
, arg
) {
2167 state
= get_state_chunk(link_id
, arg
);
2171 links
= state
->data
;
2172 FOR_EACH_PTR(links
, link
) {
2173 struct var_sym_list
*right_vsl
;
2174 struct var_sym
*right_vs
;
2177 if (strstr(link
, " orig"))
2179 sm
= get_sm_state(comparison_id
, link
, NULL
);
2182 data
= sm
->state
->data
;
2184 data
->comparison
== UNKNOWN_COMPARISON
||
2185 data
->comparison
== IMPOSSIBLE_COMPARISON
)
2187 arg_name
= expr_to_var(arg
);
2192 if (strcmp(data
->left_var
, arg_name
) == 0) {
2193 comparison
= data
->comparison
;
2194 right_name
= data
->right_var
;
2195 right_vsl
= data
->right_vsl
;
2196 } else if (strcmp(data
->right_var
, arg_name
) == 0) {
2197 comparison
= flip_comparison(data
->comparison
);
2198 right_name
= data
->left_var
;
2199 right_vsl
= data
->left_vsl
;
2201 if (!right_vsl
|| ptr_list_size((struct ptr_list
*)right_vsl
) != 1)
2204 right_vs
= first_ptr_list((struct ptr_list
*)right_vsl
);
2205 if (strcmp(right_vs
->var
, right_name
) != 0)
2207 right_name
= get_printed_param_name(expr
, right_vs
->var
, right_vs
->sym
);
2210 snprintf(info_buf
, sizeof(info_buf
), "%s %s", show_comparison(comparison
), right_name
);
2211 sql_insert_caller_info(expr
, PARAM_COMPARE
, i
, "$", info_buf
);
2214 free_string(arg_name
);
2215 } END_FOR_EACH_PTR(link
);
2216 } END_FOR_EACH_PTR(arg
);
2219 static void struct_member_callback(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*link_sm
)
2221 struct sm_state
*compare_sm
;
2222 struct string_list
*links
;
2224 struct compare_data
*data
;
2225 struct var_sym
*left
, *right
;
2226 static char info_buf
[256];
2227 const char *right_name
;
2229 if (strstr(printed_name
, " orig"))
2232 links
= link_sm
->state
->data
;
2233 FOR_EACH_PTR(links
, link
) {
2234 compare_sm
= get_sm_state(comparison_id
, link
, NULL
);
2237 data
= compare_sm
->state
->data
;
2238 if (!data
|| !data
->comparison
)
2241 if (ptr_list_size((struct ptr_list
*)data
->left_vsl
) != 1 ||
2242 ptr_list_size((struct ptr_list
*)data
->right_vsl
) != 1)
2244 left
= first_ptr_list((struct ptr_list
*)data
->left_vsl
);
2245 right
= first_ptr_list((struct ptr_list
*)data
->right_vsl
);
2246 if (left
->sym
== right
->sym
&&
2247 strcmp(left
->var
, right
->var
) == 0)
2250 * Both parameters link to this comparison so only
2251 * record the first one.
2253 if (left
->sym
!= link_sm
->sym
||
2254 strcmp(left
->var
, link_sm
->name
) != 0)
2257 right_name
= get_printed_param_name(call
, right
->var
, right
->sym
);
2260 snprintf(info_buf
, sizeof(info_buf
), "%s %s", show_comparison(data
->comparison
), right_name
);
2261 sql_insert_caller_info(call
, PARAM_COMPARE
, param
, printed_name
, info_buf
);
2262 } END_FOR_EACH_PTR(link
);
2265 static void print_return_value_comparison(int return_id
, char *return_ranges
, struct expression
*expr
)
2268 const char *tmp_name
;
2274 * TODO: This only prints == comparisons. That's probably the most
2275 * useful comparison because == max has lots of implications. But it
2276 * would be good to capture the rest as well.
2278 * This information is already in the DB but it's in the parameter math
2279 * bits and it's awkward to use it. This is is the simpler, possibly
2280 * cleaner way, but not necessarily the best, I don't know.
2285 name
= expr_to_var_sym(expr
, &sym
);
2289 param
= get_param_num_from_sym(sym
);
2292 if (param_was_set_var_sym(name
, sym
))
2295 tmp_name
= get_param_name_var_sym(name
, sym
);
2299 snprintf(info_buf
, sizeof(info_buf
), "== $%d%s", param
, tmp_name
+ 1);
2300 sql_insert_return_states(return_id
, return_ranges
,
2301 PARAM_COMPARE
, -1, "$", info_buf
);
2306 static void print_return_comparison(int return_id
, char *return_ranges
, struct expression
*expr
)
2308 struct sm_state
*tmp
;
2309 struct string_list
*links
;
2311 struct sm_state
*sm
;
2312 struct compare_data
*data
;
2313 struct var_sym
*left
, *right
;
2314 int left_param
, right_param
;
2316 char right_buf
[256];
2318 const char *tmp_name
;
2320 print_return_value_comparison(return_id
, return_ranges
, expr
);
2322 FOR_EACH_MY_SM(link_id
, __get_cur_stree(), tmp
) {
2323 if (get_param_num_from_sym(tmp
->sym
) < 0)
2325 links
= tmp
->state
->data
;
2326 FOR_EACH_PTR(links
, link
) {
2327 sm
= get_sm_state(comparison_id
, link
, NULL
);
2330 data
= sm
->state
->data
;
2332 data
->comparison
== UNKNOWN_COMPARISON
||
2333 data
->comparison
== IMPOSSIBLE_COMPARISON
)
2335 if (ptr_list_size((struct ptr_list
*)data
->left_vsl
) != 1 ||
2336 ptr_list_size((struct ptr_list
*)data
->right_vsl
) != 1)
2338 left
= first_ptr_list((struct ptr_list
*)data
->left_vsl
);
2339 right
= first_ptr_list((struct ptr_list
*)data
->right_vsl
);
2340 if (left
->sym
== right
->sym
&&
2341 strcmp(left
->var
, right
->var
) == 0)
2344 * Both parameters link to this comparison so only
2345 * record the first one.
2347 if (left
->sym
!= tmp
->sym
||
2348 strcmp(left
->var
, tmp
->name
) != 0)
2351 if (strstr(right
->var
, " orig"))
2354 left_param
= get_param_num_from_sym(left
->sym
);
2355 right_param
= get_param_num_from_sym(right
->sym
);
2356 if (left_param
< 0 || right_param
< 0)
2359 tmp_name
= get_param_name_var_sym(left
->var
, left
->sym
);
2362 snprintf(left_buf
, sizeof(left_buf
), "%s", tmp_name
);
2364 tmp_name
= get_param_name_var_sym(right
->var
, right
->sym
);
2365 if (!tmp_name
|| tmp_name
[0] != '$')
2367 snprintf(right_buf
, sizeof(right_buf
), "$%d%s", right_param
, tmp_name
+ 1);
2370 * FIXME: this should reject $ type variables (as
2371 * opposed to $->foo type). Those should come from
2372 * smatch_param_compare_limit.c.
2375 snprintf(info_buf
, sizeof(info_buf
), "%s %s", show_comparison(data
->comparison
), right_buf
);
2376 sql_insert_return_states(return_id
, return_ranges
,
2377 PARAM_COMPARE
, left_param
, left_buf
, info_buf
);
2378 } END_FOR_EACH_PTR(link
);
2380 } END_FOR_EACH_SM(tmp
);
2383 static int parse_comparison(char **value
, int *op
)
2391 if (**value
== '=') {
2399 *op
= SPECIAL_EQUAL
;
2404 *op
= SPECIAL_NOTEQUAL
;
2408 if (**value
== '=') {
2417 if (**value
!= ' ') {
2418 sm_perror("parsing comparison. %s", *value
);
2426 static int split_op_param_key(char *value
, int *op
, int *param
, char **key
)
2428 static char buf
[256];
2431 if (!parse_comparison(&value
, op
))
2434 snprintf(buf
, sizeof(buf
), "%s", value
);
2441 if (*param
< 0 || *param
> 99)
2453 static void db_return_comparison(struct expression
*expr
, int left_param
, char *key
, char *value
)
2455 struct expression
*left_arg
, *right_arg
;
2456 char *left_name
= NULL
;
2457 struct symbol
*left_sym
;
2458 char *right_name
= NULL
;
2459 struct symbol
*right_sym
;
2463 struct var_sym_list
*left_vsl
= NULL
, *right_vsl
= NULL
;
2465 if (left_param
== -1) {
2466 if (expr
->type
!= EXPR_ASSIGNMENT
)
2468 left_arg
= strip_expr(expr
->left
);
2470 while (expr
->type
== EXPR_ASSIGNMENT
)
2471 expr
= strip_expr(expr
->right
);
2472 if (expr
->type
!= EXPR_CALL
)
2475 while (expr
->type
== EXPR_ASSIGNMENT
)
2476 expr
= strip_expr(expr
->right
);
2477 if (expr
->type
!= EXPR_CALL
)
2480 left_arg
= get_argument_from_call_expr(expr
->args
, left_param
);
2485 if (!split_op_param_key(value
, &op
, &right_param
, &right_key
))
2488 right_arg
= get_argument_from_call_expr(expr
->args
, right_param
);
2492 left_name
= get_variable_from_key(left_arg
, key
, &left_sym
);
2493 if (!left_name
|| !left_sym
)
2496 right_name
= get_variable_from_key(right_arg
, right_key
, &right_sym
);
2497 if (!right_name
|| !right_sym
)
2500 add_var_sym(&left_vsl
, left_name
, left_sym
);
2501 add_var_sym(&right_vsl
, right_name
, right_sym
);
2503 add_comparison_var_sym(NULL
, left_name
, left_vsl
, op
, NULL
, right_name
, right_vsl
);
2506 free_string(left_name
);
2507 free_string(right_name
);
2510 int param_compare_limit_is_impossible(struct expression
*expr
, int left_param
, char *left_key
, char *value
)
2512 struct smatch_state
*state
;
2513 char *left_name
= NULL
;
2514 char *right_name
= NULL
;
2515 struct symbol
*left_sym
, *right_sym
;
2516 struct expression
*left_arg
, *right_arg
;
2523 while (expr
->type
== EXPR_ASSIGNMENT
)
2524 expr
= strip_expr(expr
->right
);
2525 if (expr
->type
!= EXPR_CALL
)
2528 if (!split_op_param_key(value
, &op
, &right_param
, &right_key
))
2531 left_arg
= get_argument_from_call_expr(expr
->args
, left_param
);
2535 right_arg
= get_argument_from_call_expr(expr
->args
, right_param
);
2539 left_name
= get_variable_from_key(left_arg
, left_key
, &left_sym
);
2540 right_name
= get_variable_from_key(right_arg
, right_key
, &right_sym
);
2541 if (!left_name
|| !right_name
)
2544 snprintf(buf
, sizeof(buf
), "%s vs %s", left_name
, right_name
);
2545 state
= get_state(comparison_id
, buf
, NULL
);
2548 state_op
= state_to_comparison(state
);
2552 if (!comparison_intersection(remove_unsigned_from_comparison(state_op
), op
))
2555 free_string(left_name
);
2556 free_string(right_name
);
2560 int impossibly_high_comparison(struct expression
*expr
)
2562 struct smatch_state
*link_state
;
2563 struct sm_state
*sm
;
2564 struct string_list
*links
;
2566 struct compare_data
*data
;
2568 link_state
= get_state_expr(link_id
, expr
);
2570 if (expr
->type
== EXPR_BINOP
&&
2571 (impossibly_high_comparison(expr
->left
) ||
2572 impossibly_high_comparison(expr
->right
)))
2577 links
= link_state
->data
;
2578 FOR_EACH_PTR(links
, link
) {
2579 sm
= get_sm_state(comparison_id
, link
, NULL
);
2582 data
= sm
->state
->data
;
2585 if (!possibly_true(data
->left
, data
->comparison
, data
->right
))
2587 } END_FOR_EACH_PTR(link
);
2592 static void free_data(struct symbol
*sym
)
2596 clear_compare_data_alloc();
2599 void register_comparison(int id
)
2602 set_dynamic_states(comparison_id
);
2603 add_hook(&save_start_states
, AFTER_DEF_HOOK
);
2604 add_unmatched_state_hook(comparison_id
, unmatched_comparison
);
2605 add_pre_merge_hook(comparison_id
, &pre_merge_hook
);
2606 add_merge_hook(comparison_id
, &merge_compare_states
);
2607 add_hook(&free_data
, AFTER_FUNC_HOOK
);
2608 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
2609 add_split_return_callback(&print_return_comparison
);
2611 select_return_states_hook(PARAM_COMPARE
, &db_return_comparison
);
2612 add_hook(&match_preop
, OP_HOOK
);
2615 void register_comparison_late(int id
)
2617 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
2620 void register_comparison_links(int id
)
2623 db_ignore_states(link_id
);
2624 set_dynamic_states(link_id
);
2625 add_merge_hook(link_id
, &merge_links
);
2626 add_modification_hook(link_id
, &match_modify
);
2627 add_modification_hook_late(link_id
, match_inc_dec
);
2629 add_member_info_callback(link_id
, struct_member_callback
);
2632 void register_comparison_inc_dec(int id
)
2635 add_modification_hook_late(inc_dec_id
, &iter_modify
);
2638 void register_comparison_inc_dec_links(int id
)
2640 inc_dec_link_id
= id
;
2641 set_dynamic_states(inc_dec_link_id
);
2642 set_up_link_functions(inc_dec_id
, inc_dec_link_id
);
2645 static struct sm_state
*clone_partial_sm(struct sm_state
*sm
, int comparison
)
2647 struct compare_data
*data
;
2648 struct sm_state
*clone
;
2649 struct stree
*stree
;
2651 data
= sm
->state
->data
;
2653 clone
= clone_sm(sm
);
2654 clone
->state
= alloc_compare_state(data
->left
, data
->left_var
, data
->left_vsl
,
2656 data
->right
, data
->right_var
, data
->right_vsl
);
2657 free_slist(&clone
->possible
);
2658 add_possible_sm(clone
, clone
);
2660 stree
= clone_stree(sm
->pool
);
2661 overwrite_sm_state_stree(&stree
, clone
);
2662 clone
->pool
= stree
;
2667 static void create_fake_history(struct sm_state
*sm
, int op
,
2668 struct state_list
**true_stack
,
2669 struct state_list
**false_stack
)
2671 struct sm_state
*true_sm
, *false_sm
;
2672 struct compare_data
*data
;
2673 int true_comparison
;
2674 int false_comparison
;
2676 data
= sm
->state
->data
;
2678 if (is_merged(sm
) || sm
->left
|| sm
->right
)
2681 true_comparison
= comparison_intersection(data
->comparison
, op
);
2682 false_comparison
= comparison_intersection(data
->comparison
, negate_comparison(op
));
2684 true_sm
= clone_partial_sm(sm
, true_comparison
);
2685 false_sm
= clone_partial_sm(sm
, false_comparison
);
2689 sm
->right
= false_sm
;
2691 add_ptr_list(true_stack
, true_sm
);
2692 add_ptr_list(false_stack
, false_sm
);
2695 static void filter_by_sm(struct sm_state
*sm
, int op
,
2696 struct state_list
**true_stack
,
2697 struct state_list
**false_stack
,
2700 struct compare_data
*data
;
2706 data
= sm
->state
->data
;
2709 if (data
->comparison
== IMPOSSIBLE_COMPARISON
)
2713 * We want to check that "data->comparison" is totally inside "op". So
2714 * if data->comparison is < and op is <= then that's true. Or if
2715 * data->comparison is == and op is <= then that's true. But if
2716 * data->comparison is <= and op is < than that's neither true nor
2719 if (data
->comparison
== comparison_intersection(data
->comparison
, op
))
2721 if (data
->comparison
== comparison_intersection(data
->comparison
, negate_comparison(op
)))
2724 if (!is_true
&& !is_false
&& !is_merged(sm
)) {
2725 create_fake_history(sm
, op
, true_stack
, false_stack
);
2729 if (debug_implied()) {
2730 sm_msg("%s: %s: op = '%s' negated '%s'. true_intersect = '%s' false_insersect = '%s' sm = '%s'",
2733 alloc_sname(show_comparison(op
)),
2734 alloc_sname(show_comparison(negate_comparison(op
))),
2735 alloc_sname(show_comparison(comparison_intersection(data
->comparison
, op
))),
2736 alloc_sname(show_comparison(comparison_intersection(data
->comparison
, negate_comparison(op
)))),
2742 add_ptr_list(true_stack
, sm
);
2744 add_ptr_list(false_stack
, sm
);
2746 filter_by_sm(sm
->left
, op
, true_stack
, false_stack
, useful
);
2747 filter_by_sm(sm
->right
, op
, true_stack
, false_stack
, useful
);
2750 struct sm_state
*comparison_implication_hook(struct expression
*expr
,
2751 struct state_list
**true_stack
,
2752 struct state_list
**false_stack
)
2754 struct sm_state
*sm
;
2757 static char buf
[256];
2758 bool useful
= false;
2760 if (expr
->type
!= EXPR_COMPARE
)
2765 left
= expr_to_var(expr
->left
);
2766 right
= expr_to_var(expr
->right
);
2767 if (!left
|| !right
) {
2773 if (strcmp(left
, right
) > 0) {
2778 op
= flip_comparison(op
);
2781 snprintf(buf
, sizeof(buf
), "%s vs %s", left
, right
);
2782 sm
= get_sm_state(comparison_id
, buf
, NULL
);
2788 filter_by_sm(sm
, op
, true_stack
, false_stack
, &useful
);
2792 if (debug_implied())
2793 sm_msg("implications from comparison: (%s)", show_sm(sm
));