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_link_var_sym(const char *var
, struct symbol
*sym
, const char *link
)
605 struct smatch_state
*old_state
, *new_state
;
606 struct string_list
*links
;
609 old_state
= get_state(link_id
, var
, sym
);
611 links
= clone_str_list(old_state
->data
);
615 new = alloc_sname(link
);
616 insert_string(&links
, new);
618 new_state
= alloc_link_state(links
);
619 set_state(link_id
, var
, sym
, new_state
);
622 static void save_start_states(struct statement
*stmt
)
624 struct symbol
*param
;
626 char state_name
[128];
627 struct smatch_state
*state
;
629 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, param
) {
630 struct var_sym_list
*left_vsl
= NULL
;
631 struct var_sym_list
*right_vsl
= NULL
;
635 snprintf(orig
, sizeof(orig
), "%s orig", param
->ident
->name
);
636 snprintf(state_name
, sizeof(state_name
), "%s vs %s", param
->ident
->name
, orig
);
637 add_var_sym(&left_vsl
, param
->ident
->name
, param
);
638 add_var_sym(&right_vsl
, orig
, param
);
639 state
= alloc_compare_state(
640 NULL
, param
->ident
->name
, left_vsl
,
642 NULL
, alloc_sname(orig
), right_vsl
);
643 set_state(comparison_id
, state_name
, NULL
, state
);
644 save_link_var_sym(param
->ident
->name
, param
, state_name
);
645 } END_FOR_EACH_PTR(param
);
648 static struct smatch_state
*merge_links(struct smatch_state
*s1
, struct smatch_state
*s2
)
650 struct smatch_state
*ret
;
651 struct string_list
*links
;
653 links
= combine_string_lists(s1
->data
, s2
->data
);
654 ret
= alloc_link_state(links
);
658 static void match_inc(struct sm_state
*sm
, bool preserve
)
660 struct string_list
*links
;
661 struct smatch_state
*state
, *new;
662 struct compare_data
*data
;
667 links
= sm
->state
->data
;
669 FOR_EACH_PTR(links
, tmp
) {
670 state
= get_state(comparison_id
, tmp
, NULL
);
678 if (strncmp(sm
->name
, tmp
, strlen(sm
->name
)) != 0 ||
679 tmp
[strlen(sm
->name
)] != ' ')
682 op
= state_to_comparison(state
);
684 switch (flip
? flip_comparison(op
) : op
) {
687 case SPECIAL_UNSIGNED_GTE
:
689 case SPECIAL_UNSIGNED_GT
:
692 new = alloc_compare_state(
693 data
->left
, data
->left_var
, data
->left_vsl
,
695 data
->right
, data
->right_var
, data
->right_vsl
);
696 set_state(comparison_id
, tmp
, NULL
, new);
699 case SPECIAL_UNSIGNED_LT
:
700 new = alloc_compare_state(
701 data
->left
, data
->left_var
, data
->left_vsl
,
702 flip
? SPECIAL_GTE
: SPECIAL_LTE
,
703 data
->right
, data
->right_var
, data
->right_vsl
);
704 set_state(comparison_id
, tmp
, NULL
, new);
707 new = alloc_compare_state(
708 data
->left
, data
->left_var
, data
->left_vsl
,
710 data
->right
, data
->right_var
, data
->right_vsl
);
711 set_state(comparison_id
, tmp
, NULL
, new);
713 } END_FOR_EACH_PTR(tmp
);
716 static void match_dec(struct sm_state
*sm
, bool preserve
)
718 struct string_list
*links
;
719 struct smatch_state
*state
;
722 links
= sm
->state
->data
;
724 FOR_EACH_PTR(links
, tmp
) {
725 struct compare_data
*data
;
726 struct smatch_state
*new;
728 state
= get_state(comparison_id
, tmp
, NULL
);
729 if (!state
|| !state
->data
)
734 switch (state_to_comparison(state
)) {
737 case SPECIAL_UNSIGNED_LTE
:
739 case SPECIAL_UNSIGNED_LT
: {
743 new = alloc_compare_state(
744 data
->left
, data
->left_var
, data
->left_vsl
,
746 data
->right
, data
->right_var
, data
->right_vsl
);
747 set_state(comparison_id
, tmp
, NULL
, new);
751 new = alloc_compare_state(
752 data
->left
, data
->left_var
, data
->left_vsl
,
754 data
->right
, data
->right_var
, data
->right_vsl
);
755 set_state(comparison_id
, tmp
, NULL
, new);
757 } END_FOR_EACH_PTR(tmp
);
760 static void reset_sm(struct sm_state
*sm
)
762 struct string_list
*links
;
765 links
= sm
->state
->data
;
767 FOR_EACH_PTR(links
, tmp
) {
768 struct smatch_state
*old
, *new;
770 old
= get_state(comparison_id
, tmp
, NULL
);
771 if (!old
|| !old
->data
) {
774 struct compare_data
*data
= old
->data
;
776 new = alloc_compare_state(
777 data
->left
, data
->left_var
, data
->left_vsl
,
779 data
->right
, data
->right_var
, data
->right_vsl
);
781 set_state(comparison_id
, tmp
, NULL
, new);
782 } END_FOR_EACH_PTR(tmp
);
783 set_state(link_id
, sm
->name
, sm
->sym
, &undefined
);
786 static bool match_add_sub_assign(struct sm_state
*sm
, struct expression
*expr
)
788 struct range_list
*rl
;
789 sval_t zero
= { .type
= &int_ctype
};
791 if (!expr
|| expr
->type
!= EXPR_ASSIGNMENT
)
793 if (expr
->op
!= SPECIAL_ADD_ASSIGN
&& expr
->op
!= SPECIAL_SUB_ASSIGN
)
796 get_absolute_rl(expr
->right
, &rl
);
797 if (sval_is_negative(rl_min(rl
))) {
802 if (expr
->op
== SPECIAL_ADD_ASSIGN
)
803 match_inc(sm
, rl_has_sval(rl
, zero
));
805 match_dec(sm
, rl_has_sval(rl
, zero
));
809 static void match_inc_dec(struct sm_state
*sm
, struct expression
*mod_expr
)
812 * if (foo > bar) then ++foo is also > bar.
816 if (match_add_sub_assign(sm
, mod_expr
))
818 if (mod_expr
->type
!= EXPR_PREOP
&& mod_expr
->type
!= EXPR_POSTOP
)
821 if (mod_expr
->op
== SPECIAL_INCREMENT
)
822 match_inc(sm
, false);
823 else if (mod_expr
->op
== SPECIAL_DECREMENT
)
824 match_dec(sm
, false);
827 static int is_self_assign(struct expression
*expr
)
829 if (!expr
|| expr
->type
!= EXPR_ASSIGNMENT
|| expr
->op
!= '=')
831 return expr_equiv(expr
->left
, expr
->right
);
834 static struct expression
*ignore_mod_expr
;
835 static void match_modify(struct sm_state
*sm
, struct expression
*mod_expr
)
837 if (mod_expr
&& is_self_assign(mod_expr
))
839 if (mod_expr
&& mod_expr
== ignore_mod_expr
)
842 /* handled by match_inc_dec() */
844 ((mod_expr
->type
== EXPR_PREOP
|| mod_expr
->type
== EXPR_POSTOP
) &&
845 (mod_expr
->op
== SPECIAL_INCREMENT
|| mod_expr
->op
== SPECIAL_DECREMENT
)))
847 if (mod_expr
&& mod_expr
->type
== EXPR_ASSIGNMENT
&&
848 (mod_expr
->op
== SPECIAL_ADD_ASSIGN
|| mod_expr
->op
== SPECIAL_SUB_ASSIGN
))
854 static void match_preop(struct expression
*expr
)
856 struct expression
*parent
;
857 struct range_list
*left
, *right
;
861 * This is an important special case. Say you have:
865 * Assume that we know the range of limit is higher than the start
866 * value for "j". Then the first thing that we process is the ++j. We
867 * have not comparison states set up so it doesn't get caught by the
868 * modification hook. But it does get caught by smatch_extra which sets
869 * j to unknown then we parse the "j == limit" and sets false to != but
870 * really we want false to be <.
872 * So what we do is we set j < limit here, then the match_modify catches
873 * it and we do a match_inc_dec().
877 if (expr
->type
!= EXPR_PREOP
||
878 (expr
->op
!= SPECIAL_INCREMENT
&& expr
->op
!= SPECIAL_DECREMENT
))
881 parent
= expr_get_parent_expr(expr
);
884 if (parent
->type
!= EXPR_COMPARE
|| parent
->op
!= SPECIAL_EQUAL
)
886 if (parent
->left
!= expr
)
889 if (!get_implied_rl(expr
->unop
, &left
) ||
890 !get_implied_rl(parent
->right
, &right
))
893 op
= rl_comparison(left
, right
);
894 if (op
== UNKNOWN_COMPARISON
)
897 add_comparison(expr
->unop
, op
, parent
->right
);
900 static char *chunk_to_var_sym(struct expression
*expr
, struct symbol
**sym
)
902 expr
= strip_expr(expr
);
908 if (expr
->type
== EXPR_PREOP
&&
909 (expr
->op
== SPECIAL_INCREMENT
||
910 expr
->op
== SPECIAL_DECREMENT
))
911 expr
= strip_expr(expr
->unop
);
913 if (expr
->type
== EXPR_CALL
) {
916 snprintf(buf
, sizeof(buf
), "return %p", expr
);
917 return alloc_string(buf
);
920 return expr_to_chunk_sym_vsl(expr
, sym
, NULL
);
923 static char *chunk_to_var(struct expression
*expr
)
925 return chunk_to_var_sym(expr
, NULL
);
928 static struct smatch_state
*get_state_chunk(int owner
, struct expression
*expr
)
932 struct smatch_state
*ret
;
934 name
= chunk_to_var_sym(expr
, &sym
);
938 ret
= get_state(owner
, name
, sym
);
943 static void save_link(struct expression
*expr
, char *link
)
948 expr
= strip_expr(expr
);
949 if (expr
->type
== EXPR_BINOP
) {
952 chunk
= chunk_to_var(expr
);
956 save_link(expr
->left
, link
);
957 save_link(expr
->right
, link
);
958 save_link_var_sym(chunk
, NULL
, link
);
962 var
= chunk_to_var_sym(expr
, &sym
);
966 save_link_var_sym(var
, sym
, link
);
970 static int get_orig_comparison(struct stree
*pre_stree
, const char *left
, const char *right
)
972 struct smatch_state
*state
;
973 struct compare_data
*data
;
975 char state_name
[256];
977 if (strcmp(left
, right
) > 0) {
978 const char *tmp
= right
;
985 snprintf(state_name
, sizeof(state_name
), "%s vs %s", left
, right
);
986 state
= get_state_stree(pre_stree
, comparison_id
, state_name
, NULL
);
987 if (!state
|| !state
->data
)
991 return flip_comparison(data
->comparison
);
992 return data
->comparison
;
996 static int have_common_var_sym(struct var_sym_list
*left_vsl
, struct var_sym_list
*right_vsl
)
1000 FOR_EACH_PTR(left_vsl
, tmp
) {
1001 if (in_var_sym_list(right_vsl
, tmp
->var
, tmp
->sym
))
1003 } END_FOR_EACH_PTR(tmp
);
1009 * The idea here is that we take a comparison "a < b" and then we look at all
1010 * the things which "b" is compared against "b < c" and we say that that implies
1011 * a relationship "a < c".
1013 * The names here about because the comparisons are organized like this
1017 static void update_tf_links(struct stree
*pre_stree
,
1018 struct expression
*left_expr
,
1019 const char *left_var
, struct var_sym_list
*left_vsl
,
1020 int left_comparison
, int left_false_comparison
,
1021 const char *mid_var
, struct var_sym_list
*mid_vsl
,
1022 struct string_list
*links
)
1024 struct smatch_state
*state
;
1025 struct smatch_state
*true_state
, *false_state
;
1026 struct compare_data
*data
;
1027 struct expression
*right_expr
;
1028 const char *left_var_orig
= left_var
;
1029 const char *right_var
;
1030 struct var_sym_list
*left_vsl_orig
= left_vsl
;
1031 struct var_sym_list
*right_vsl
;
1032 int orig_comparison
;
1033 int right_comparison
;
1034 int true_comparison
;
1035 int false_comparison
;
1037 char state_name
[256];
1040 FOR_EACH_PTR(links
, tmp
) {
1041 state
= get_state_stree(pre_stree
, comparison_id
, tmp
, NULL
);
1042 if (!state
|| !state
->data
)
1044 left_var
= left_var_orig
;
1045 left_vsl
= left_vsl_orig
;
1047 right_comparison
= data
->comparison
;
1048 right_expr
= data
->right
;
1049 right_var
= data
->right_var
;
1050 right_vsl
= data
->right_vsl
;
1051 if (strcmp(mid_var
, right_var
) == 0) {
1052 right_expr
= data
->left
;
1053 right_var
= data
->left_var
;
1054 right_vsl
= data
->left_vsl
;
1055 right_comparison
= flip_comparison(right_comparison
);
1057 if (have_common_var_sym(left_vsl
, right_vsl
))
1060 orig_comparison
= get_orig_comparison(pre_stree
, left_var
, right_var
);
1062 true_comparison
= combine_comparisons(left_comparison
, right_comparison
);
1063 false_comparison
= combine_comparisons(left_false_comparison
, right_comparison
);
1065 true_comparison
= comparison_intersection(orig_comparison
, true_comparison
);
1066 false_comparison
= comparison_intersection(orig_comparison
, false_comparison
);
1068 if (strcmp(left_var
, right_var
) > 0) {
1069 struct expression
*tmp_expr
= left_expr
;
1070 const char *tmp_var
= left_var
;
1071 struct var_sym_list
*tmp_vsl
= left_vsl
;
1073 left_expr
= right_expr
;
1074 left_var
= right_var
;
1075 left_vsl
= right_vsl
;
1076 right_expr
= tmp_expr
;
1077 right_var
= tmp_var
;
1078 right_vsl
= tmp_vsl
;
1079 true_comparison
= flip_comparison(true_comparison
);
1080 false_comparison
= flip_comparison(false_comparison
);
1083 if (!true_comparison
&& !false_comparison
)
1086 if (true_comparison
)
1087 true_state
= alloc_compare_state(
1088 left_expr
, left_var
, left_vsl
,
1090 right_expr
, right_var
, right_vsl
);
1093 if (false_comparison
)
1094 false_state
= alloc_compare_state(
1095 left_expr
, left_var
, left_vsl
,
1097 right_expr
, right_var
, right_vsl
);
1101 snprintf(state_name
, sizeof(state_name
), "%s vs %s", left_var
, right_var
);
1102 set_true_false_states(comparison_id
, state_name
, NULL
, true_state
, false_state
);
1103 FOR_EACH_PTR(left_vsl
, vs
) {
1104 save_link_var_sym(vs
->var
, vs
->sym
, state_name
);
1105 } END_FOR_EACH_PTR(vs
);
1106 FOR_EACH_PTR(right_vsl
, vs
) {
1107 save_link_var_sym(vs
->var
, vs
->sym
, state_name
);
1108 } END_FOR_EACH_PTR(vs
);
1109 if (!vsl_to_sym(left_vsl
))
1110 save_link_var_sym(left_var
, NULL
, state_name
);
1111 if (!vsl_to_sym(right_vsl
))
1112 save_link_var_sym(right_var
, NULL
, state_name
);
1113 } END_FOR_EACH_PTR(tmp
);
1116 static void update_tf_data(struct stree
*pre_stree
,
1117 struct expression
*left_expr
,
1118 const char *left_name
, struct var_sym_list
*left_vsl
,
1119 struct expression
*right_expr
,
1120 const char *right_name
, struct var_sym_list
*right_vsl
,
1121 int true_comparison
, int false_comparison
)
1123 struct smatch_state
*state
;
1125 state
= get_state_stree(pre_stree
, link_id
, right_name
, vsl_to_sym(right_vsl
));
1127 update_tf_links(pre_stree
, left_expr
, left_name
, left_vsl
, true_comparison
, false_comparison
, right_name
, right_vsl
, state
->data
);
1129 state
= get_state_stree(pre_stree
, link_id
, left_name
, vsl_to_sym(left_vsl
));
1131 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
);
1134 static void iter_modify(struct sm_state
*sm
, struct expression
*mod_expr
)
1136 if (sm
->state
!= &start
||
1138 (mod_expr
->type
!= EXPR_PREOP
&& mod_expr
->type
!= EXPR_POSTOP
) ||
1139 mod_expr
->op
!= SPECIAL_INCREMENT
)
1140 set_state(inc_dec_id
, sm
->name
, sm
->sym
, &undefined
);
1142 set_state(inc_dec_id
, sm
->name
, sm
->sym
, &incremented
);
1145 static void handle_for_loops(struct expression
*expr
, char *state_name
, struct smatch_state
*false_state
)
1148 char *iter_name
, *cap_name
;
1149 struct symbol
*iter_sym
, *cap_sym
;
1150 struct compare_data
*data
;
1152 if (expr
->op
!= '<' && expr
->op
!= SPECIAL_UNSIGNED_LT
)
1155 if (!__cur_stmt
|| !__prev_stmt
)
1157 if (__cur_stmt
->type
!= STMT_ITERATOR
)
1159 if (__cur_stmt
->iterator_pre_condition
!= expr
)
1162 /* literals are handled in smatch_extra.c */
1163 if (get_value(expr
->right
, &sval
))
1166 /* First time checking the condition */
1167 if (__prev_stmt
== __cur_stmt
->iterator_pre_statement
) {
1168 if (!get_implied_value(expr
->left
, &sval
) ||
1172 iter_name
= expr_to_var_sym(expr
->left
, &iter_sym
);
1173 cap_name
= expr_to_var_sym(expr
->right
, &cap_sym
);
1174 if (!iter_name
|| !cap_name
|| !iter_sym
|| !cap_sym
) {
1175 free_string(iter_name
);
1176 free_string(cap_name
);
1180 set_state(inc_dec_id
, iter_name
, iter_sym
, &start
);
1181 store_link(inc_dec_link_id
, cap_name
, cap_sym
, iter_name
, iter_sym
);
1183 free_string(iter_name
);
1184 free_string(cap_name
);
1188 /* Second time checking the condtion */
1189 if (__prev_stmt
!= __cur_stmt
->iterator_post_statement
)
1192 if (get_state_chunk(inc_dec_id
, expr
->left
) != &incremented
)
1195 data
= false_state
->data
;
1196 false_state
= alloc_compare_state(
1197 data
->left
, data
->left_var
, data
->left_vsl
,
1199 data
->right
, data
->right_var
, data
->right_vsl
);
1201 // FIXME: This doesn't handle links correct so it doesn't set "param orig"
1202 set_true_false_states(comparison_id
, state_name
, NULL
, NULL
, false_state
);
1205 static int is_plus_one(struct expression
*expr
)
1209 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '+')
1211 if (!get_implied_value(expr
->right
, &sval
) || sval
.value
!= 1)
1216 static int is_minus_one(struct expression
*expr
)
1220 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '-')
1222 if (!get_implied_value(expr
->right
, &sval
) || sval
.value
!= 1)
1227 static void remove_plus_minus_zero(struct expression
**expr_p
)
1229 struct expression
*expr
= *expr_p
;
1232 if (!expr
|| expr
->type
!= EXPR_BINOP
)
1234 if (expr
->op
== '+') {
1235 if (get_implied_value(expr
->left
, &sval
) && sval
.value
== 0)
1236 *expr_p
= expr
->right
;
1237 else if (get_implied_value(expr
->right
, &sval
) && sval
.value
== 0)
1238 *expr_p
= expr
->left
;
1241 return remove_plus_minus_zero(expr_p
);
1243 if (expr
->op
== '-') {
1244 if (get_implied_value(expr
->right
, &sval
) && sval
.value
== 0) {
1245 *expr_p
= expr
->left
;
1246 return remove_plus_minus_zero(expr_p
);
1251 static void move_plus_to_minus_helper(struct expression
**left_p
, struct expression
**right_p
)
1253 struct expression
*left
= *left_p
;
1254 struct expression
*right
= *right_p
;
1257 * These two are basically equivalent: "foo + 1 != bar" and
1258 * "foo != bar - 1". There are issues with signedness and integer
1259 * overflows. There are also issues with type as well. But let's
1260 * pretend we can ignore all that stuff for now.
1264 if (!is_plus_one(left
))
1267 *left_p
= left
->left
;
1268 *right_p
= binop_expression(right
, '-', left
->right
);
1271 static void move_plus_to_minus(struct expression
**left_p
, struct expression
**right_p
)
1273 if (is_plus_one(*left_p
) && is_plus_one(*right_p
))
1276 move_plus_to_minus_helper(left_p
, right_p
);
1277 move_plus_to_minus_helper(right_p
, left_p
);
1280 static void simplify_binops(struct expression
**left_p
, struct expression
**right_p
)
1282 remove_plus_minus_zero(left_p
);
1283 remove_plus_minus_zero(right_p
);
1285 move_plus_to_minus(left_p
, right_p
);
1288 static void handle_comparison(struct expression
*left_expr
, int op
, struct expression
*right_expr
, char **_state_name
, struct smatch_state
**_false_state
)
1292 struct symbol
*left_sym
, *right_sym
;
1293 struct var_sym_list
*left_vsl
= NULL
;
1294 struct var_sym_list
*right_vsl
= NULL
;
1296 int orig_comparison
;
1297 struct smatch_state
*true_state
, *false_state
;
1298 static char state_name
[256];
1299 struct stree
*pre_stree
;
1302 if (!left_expr
|| !right_expr
)
1305 left_expr
= strip_parens(left_expr
);
1306 right_expr
= strip_parens(right_expr
);
1308 while (left_expr
->type
== EXPR_ASSIGNMENT
)
1309 left_expr
= strip_parens(left_expr
->left
);
1310 while (right_expr
->type
== EXPR_ASSIGNMENT
)
1311 right_expr
= strip_parens(right_expr
->left
);
1313 false_op
= negate_comparison(op
);
1315 simplify_binops(&left_expr
, &right_expr
);
1317 if (op
== SPECIAL_UNSIGNED_LT
&&
1318 get_implied_value(left_expr
, &sval
) &&
1320 false_op
= SPECIAL_EQUAL
;
1322 if (op
== SPECIAL_UNSIGNED_GT
&&
1323 get_implied_value(right_expr
, &sval
) &&
1325 false_op
= SPECIAL_EQUAL
;
1327 left
= chunk_to_var_sym(left_expr
, &left_sym
);
1331 add_var_sym(&left_vsl
, left
, left_sym
);
1333 left_vsl
= expr_to_vsl(left_expr
);
1334 right
= chunk_to_var_sym(right_expr
, &right_sym
);
1338 add_var_sym(&right_vsl
, right
, right_sym
);
1340 right_vsl
= expr_to_vsl(right_expr
);
1342 if (strcmp(left
, right
) > 0) {
1343 char *tmp_name
= left
;
1344 struct var_sym_list
*tmp_vsl
= left_vsl
;
1345 struct expression
*tmp_expr
= left_expr
;
1348 left_vsl
= right_vsl
;
1349 left_expr
= right_expr
;
1351 right_vsl
= tmp_vsl
;
1352 right_expr
= tmp_expr
;
1353 op
= flip_comparison(op
);
1354 false_op
= flip_comparison(false_op
);
1357 orig_comparison
= get_comparison(left_expr
, right_expr
);
1358 op
= comparison_intersection(orig_comparison
, op
);
1359 false_op
= comparison_intersection(orig_comparison
, false_op
);
1361 snprintf(state_name
, sizeof(state_name
), "%s vs %s", left
, right
);
1362 true_state
= alloc_compare_state(
1363 left_expr
, left
, left_vsl
,
1365 right_expr
, right
, right_vsl
);
1366 false_state
= alloc_compare_state(
1367 left_expr
, left
, left_vsl
,
1369 right_expr
, right
, right_vsl
);
1371 pre_stree
= clone_stree(__get_cur_stree());
1372 update_tf_data(pre_stree
, left_expr
, left
, left_vsl
, right_expr
, right
, right_vsl
, op
, false_op
);
1373 free_stree(&pre_stree
);
1375 if (op
== IMPOSSIBLE_COMPARISON
&&
1376 false_op
!= IMPOSSIBLE_COMPARISON
&&
1377 false_op
!= UNKNOWN_COMPARISON
)
1378 set_true_path_impossible();
1379 if (op
!= IMPOSSIBLE_COMPARISON
&&
1380 op
!= UNKNOWN_COMPARISON
&&
1381 false_op
== IMPOSSIBLE_COMPARISON
)
1382 set_false_path_impossible();
1384 set_true_false_states(comparison_id
, state_name
, NULL
, true_state
, false_state
);
1385 __compare_param_limit_hook(left_expr
, right_expr
, state_name
, true_state
, false_state
);
1386 save_link(left_expr
, state_name
);
1387 save_link(right_expr
, state_name
);
1390 *_false_state
= false_state
;
1392 *_state_name
= state_name
;
1398 void __comparison_match_condition(struct expression
*expr
)
1400 struct expression
*left
, *right
, *new_left
, *new_right
, *tmp
;
1401 struct smatch_state
*false_state
= NULL
;
1402 char *state_name
= NULL
;
1405 if (expr
->type
!= EXPR_COMPARE
)
1408 handle_comparison(expr
->left
, expr
->op
, expr
->right
, &state_name
, &false_state
);
1409 if (false_state
&& state_name
)
1410 handle_for_loops(expr
, state_name
, false_state
);
1412 left
= strip_parens(expr
->left
);
1413 right
= strip_parens(expr
->right
);
1415 if (left
->type
== EXPR_BINOP
&& left
->op
== '+') {
1416 new_left
= left
->left
;
1417 new_right
= binop_expression(right
, '-', left
->right
);
1418 handle_comparison(new_left
, expr
->op
, new_right
, NULL
, NULL
);
1420 new_left
= left
->right
;
1421 new_right
= binop_expression(right
, '-', left
->left
);
1422 handle_comparison(new_left
, expr
->op
, new_right
, NULL
, NULL
);
1425 if ((expr
->op
== SPECIAL_EQUAL
|| expr
->op
== SPECIAL_NOTEQUAL
) &&
1426 expr_is_zero(expr
->right
) &&
1427 left
->type
== EXPR_BINOP
&& left
->op
== '-') {
1428 new_left
= left
->left
;
1429 new_right
= left
->right
;
1430 handle_comparison(new_left
, expr
->op
, new_right
, NULL
, NULL
);
1434 left
= strip_parens(expr
->left
);
1435 right
= strip_parens(expr
->right
);
1436 if (get_last_expr_from_expression_stmt(expr
->left
)) {
1437 left
= get_last_expr_from_expression_stmt(expr
->left
);
1440 if (get_last_expr_from_expression_stmt(expr
->right
)) {
1441 right
= get_last_expr_from_expression_stmt(expr
->right
);
1449 while ((tmp
= get_assigned_expr(left
))) {
1452 left
= strip_expr(tmp
);
1455 while ((tmp
= get_assigned_expr(right
))) {
1458 right
= strip_expr(tmp
);
1461 handle_comparison(left
, expr
->op
, right
, NULL
, NULL
);
1464 void add_comparison_var_sym(
1465 struct expression
*left_expr
,
1466 const char *left_name
, struct var_sym_list
*left_vsl
,
1468 struct expression
*right_expr
,
1469 const char *right_name
, struct var_sym_list
*right_vsl
,
1470 struct expression
*mod_expr
)
1472 struct smatch_state
*state
;
1474 char state_name
[256];
1476 ignore_mod_expr
= mod_expr
;
1478 if (strcmp(left_name
, right_name
) > 0) {
1479 struct expression
*tmp_expr
= left_expr
;
1480 const char *tmp_name
= left_name
;
1481 struct var_sym_list
*tmp_vsl
= left_vsl
;
1483 left_expr
= right_expr
;
1484 left_name
= right_name
;
1485 left_vsl
= right_vsl
;
1486 right_expr
= tmp_expr
;
1487 right_name
= tmp_name
;
1488 right_vsl
= tmp_vsl
;
1489 comparison
= flip_comparison(comparison
);
1491 snprintf(state_name
, sizeof(state_name
), "%s vs %s", left_name
, right_name
);
1492 state
= alloc_compare_state(
1493 left_expr
, left_name
, left_vsl
,
1495 right_expr
, right_name
, right_vsl
);
1497 set_state(comparison_id
, state_name
, NULL
, state
);
1499 FOR_EACH_PTR(left_vsl
, vs
) {
1500 save_link_var_sym(vs
->var
, vs
->sym
, state_name
);
1501 } END_FOR_EACH_PTR(vs
);
1502 FOR_EACH_PTR(right_vsl
, vs
) {
1503 save_link_var_sym(vs
->var
, vs
->sym
, state_name
);
1504 } END_FOR_EACH_PTR(vs
);
1507 static void add_comparison(struct expression
*left
, int comparison
, struct expression
*right
)
1509 char *left_name
= NULL
;
1510 char *right_name
= NULL
;
1511 struct symbol
*left_sym
, *right_sym
;
1512 struct var_sym_list
*left_vsl
, *right_vsl
;
1513 struct smatch_state
*state
;
1514 char state_name
[256];
1516 left_name
= chunk_to_var_sym(left
, &left_sym
);
1519 left_vsl
= expr_to_vsl(left
);
1520 right_name
= chunk_to_var_sym(right
, &right_sym
);
1523 right_vsl
= expr_to_vsl(right
);
1525 if (strcmp(left_name
, right_name
) > 0) {
1526 struct expression
*tmp_expr
= left
;
1527 struct symbol
*tmp_sym
= left_sym
;
1528 char *tmp_name
= left_name
;
1529 struct var_sym_list
*tmp_vsl
= left_vsl
;
1532 left_name
= right_name
;
1533 left_sym
= right_sym
;
1534 left_vsl
= right_vsl
;
1536 right_name
= tmp_name
;
1537 right_sym
= tmp_sym
;
1538 right_vsl
= tmp_vsl
;
1539 comparison
= flip_comparison(comparison
);
1541 snprintf(state_name
, sizeof(state_name
), "%s vs %s", left_name
, right_name
);
1542 state
= alloc_compare_state(
1543 left
, left_name
, left_vsl
,
1545 right
, right_name
, right_vsl
);
1547 set_state(comparison_id
, state_name
, NULL
, state
);
1548 save_link(left
, state_name
);
1549 save_link(right
, state_name
);
1552 free_string(left_name
);
1553 free_string(right_name
);
1556 static void match_assign_add(struct expression
*expr
)
1558 struct expression
*right
;
1559 struct expression
*r_left
, *r_right
;
1560 sval_t left_tmp
, right_tmp
;
1562 right
= strip_expr(expr
->right
);
1563 r_left
= strip_expr(right
->left
);
1564 r_right
= strip_expr(right
->right
);
1566 get_absolute_min(r_left
, &left_tmp
);
1567 get_absolute_min(r_right
, &right_tmp
);
1569 if (left_tmp
.value
> 0)
1570 add_comparison(expr
->left
, '>', r_right
);
1571 else if (left_tmp
.value
== 0)
1572 add_comparison(expr
->left
, SPECIAL_GTE
, r_right
);
1574 if (right_tmp
.value
> 0)
1575 add_comparison(expr
->left
, '>', r_left
);
1576 else if (right_tmp
.value
== 0)
1577 add_comparison(expr
->left
, SPECIAL_GTE
, r_left
);
1580 static void match_assign_sub(struct expression
*expr
)
1582 struct expression
*right
;
1583 struct expression
*r_left
, *r_right
;
1587 right
= strip_expr(expr
->right
);
1588 r_left
= strip_expr(right
->left
);
1589 r_right
= strip_expr(right
->right
);
1591 if (get_absolute_min(r_right
, &min
) && sval_is_negative(min
))
1594 comparison
= get_comparison(r_left
, r_right
);
1596 switch (comparison
) {
1599 if (implied_not_equal(r_right
, 0))
1600 add_comparison(expr
->left
, '>', r_left
);
1602 add_comparison(expr
->left
, SPECIAL_GTE
, r_left
);
1607 static void match_assign_divide(struct expression
*expr
)
1609 struct expression
*right
;
1610 struct expression
*r_left
, *r_right
;
1613 right
= strip_expr(expr
->right
);
1614 r_left
= strip_expr(right
->left
);
1615 r_right
= strip_expr(right
->right
);
1616 if (!get_implied_min(r_right
, &min
) || min
.value
<= 1)
1619 add_comparison(expr
->left
, '<', r_left
);
1622 static void match_binop_assign(struct expression
*expr
)
1624 struct expression
*right
;
1626 right
= strip_expr(expr
->right
);
1627 if (right
->op
== '+')
1628 match_assign_add(expr
);
1629 if (right
->op
== '-')
1630 match_assign_sub(expr
);
1631 if (right
->op
== '/')
1632 match_assign_divide(expr
);
1635 static void copy_comparisons(struct expression
*left
, struct expression
*right
)
1637 struct string_list
*links
;
1638 struct smatch_state
*state
;
1639 struct compare_data
*data
;
1640 struct symbol
*left_sym
, *right_sym
;
1641 char *left_var
= NULL
;
1642 char *right_var
= NULL
;
1643 struct var_sym_list
*left_vsl
;
1644 struct expression
*expr
;
1646 struct var_sym_list
*vsl
;
1651 * Say we have y < foo and we assign x = y; then that means x < foo.
1652 * Internally, smatch has links from "y" and "foo" to the comparison
1653 * "foo vs y". There could also be a comparison with an array:
1654 * "x[idx] vs limit", so all three variables, "x", "idx" and "limit"
1657 * Also another thing which can happen is that you have an assignment
1658 * "x = y" and there is a comparison: "x vs y". And clearly we don't
1659 * want to store "x < x".
1661 * So what this function does is it takes each link "foo vs y" and it
1662 * swaps in "foo vs x". Except it moves the "x" to the left so it's
1663 * "x vs foo" (or whatever) and the add_comparison_var_sym() will flip
1664 * it into the correct order.
1668 left_var
= chunk_to_var_sym(left
, &left_sym
);
1671 left_vsl
= expr_to_vsl(left
);
1672 right_var
= chunk_to_var_sym(right
, &right_sym
);
1676 state
= get_state(link_id
, right_var
, right_sym
);
1679 links
= state
->data
;
1681 FOR_EACH_PTR(links
, tmp
) {
1682 state
= get_state(comparison_id
, tmp
, NULL
);
1683 if (!state
|| !state
->data
)
1686 /* if there isn't a comparison then skip it. (can this happen?) */
1687 if (!data
->comparison
)
1690 /* right_var is from "left = right_var;". "var" is the variable
1691 * bit from "right_var < var".
1693 if (strcmp(right_var
, data
->left_var
) == 0) {
1695 var
= data
->right_var
;
1696 vsl
= data
->right_vsl
;
1697 comparison
= data
->comparison
;
1698 } else if (strcmp(right_var
, data
->right_var
) == 0) {
1700 var
= data
->left_var
;
1701 vsl
= data
->left_vsl
;
1702 comparison
= flip_comparison(data
->comparison
);
1704 /* This means the right side is only part of the
1705 * comparison. As in "x = i;" when "foo[i] < bar".
1710 /* n = copy_from_user(dest, src, n); leads to n <= n which is nonsense */
1711 if (strcmp(left_var
, var
) == 0)
1714 add_comparison_var_sym(left
, left_var
, left_vsl
, comparison
, expr
, var
, vsl
, NULL
);
1715 } END_FOR_EACH_PTR(tmp
);
1718 free_string(right_var
);
1721 static bool is_empty_fake_assign(struct expression
*expr
)
1723 if (!is_fake_var_assign(expr
))
1725 if (get_state_chunk(link_id
, expr
->right
))
1730 static void match_assign(struct expression
*expr
)
1732 struct expression
*right
;
1734 if (expr
->op
!= '=')
1736 if (__in_fake_assign
|| outside_of_function())
1739 if (is_struct(expr
->left
))
1742 if (is_self_assign(expr
))
1745 if (is_empty_fake_assign(expr
))
1748 copy_comparisons(expr
->left
, expr
->right
);
1749 add_comparison(expr
->left
, SPECIAL_EQUAL
, expr
->right
);
1751 right
= strip_expr(expr
->right
);
1752 if (right
->type
== EXPR_BINOP
)
1753 match_binop_assign(expr
);
1756 int get_comparison_strings(const char *one
, const char *two
)
1759 struct smatch_state
*state
;
1764 return UNKNOWN_COMPARISON
;
1766 if (strcmp(one
, two
) == 0)
1767 return SPECIAL_EQUAL
;
1769 if (strcmp(one
, two
) > 0) {
1770 const char *tmp
= one
;
1777 snprintf(buf
, sizeof(buf
), "%s vs %s", one
, two
);
1778 state
= get_state(comparison_id
, buf
, NULL
);
1780 ret
= state_to_comparison(state
);
1783 ret
= flip_comparison(ret
);
1788 static int get_comparison_helper(struct expression
*a
, struct expression
*b
, bool use_extra
)
1792 int ret
= UNKNOWN_COMPARISON
;
1793 int extra
= UNKNOWN_COMPARISON
;
1795 if (a
== UNKNOWN_COMPARISON
||
1796 b
== UNKNOWN_COMPARISON
)
1797 return UNKNOWN_COMPARISON
;
1799 a
= strip_parens(a
);
1800 b
= strip_parens(b
);
1802 simplify_binops(&a
, &b
);
1804 one
= chunk_to_var(a
);
1807 two
= chunk_to_var(b
);
1811 ret
= get_comparison_strings(one
, two
);
1815 if (is_plus_one(a
) || is_minus_one(a
)) {
1817 one
= chunk_to_var(a
->left
);
1818 ret
= get_comparison_strings(one
, two
);
1819 } else if (is_plus_one(b
) || is_minus_one(b
)) {
1821 two
= chunk_to_var(b
->left
);
1822 ret
= get_comparison_strings(one
, two
);
1825 if (ret
== UNKNOWN_COMPARISON
)
1828 if (is_plus_one(a
) || is_minus_one(b
)) {
1831 else if (ret
== SPECIAL_EQUAL
)
1833 else if (ret
== SPECIAL_GTE
)
1836 ret
= UNKNOWN_COMPARISON
;
1837 } else if ((is_minus_one(a
) || is_plus_one(b
))) {
1840 else if (ret
== SPECIAL_EQUAL
)
1842 else if (ret
== SPECIAL_LTE
)
1845 ret
= UNKNOWN_COMPARISON
;
1847 ret
= UNKNOWN_COMPARISON
;
1855 extra
= comparison_from_extra(a
, b
);
1856 return comparison_intersection(ret
, extra
);
1859 int get_comparison(struct expression
*a
, struct expression
*b
)
1861 return get_comparison_helper(a
, b
, true);
1864 int get_comparison_no_extra(struct expression
*a
, struct expression
*b
)
1866 return get_comparison_helper(a
, b
, false);
1869 int possible_comparison(struct expression
*a
, int comparison
, struct expression
*b
)
1875 struct sm_state
*sm
;
1878 one
= chunk_to_var(a
);
1881 two
= chunk_to_var(b
);
1886 if (strcmp(one
, two
) == 0 && comparison
== SPECIAL_EQUAL
) {
1891 if (strcmp(one
, two
) > 0) {
1896 comparison
= flip_comparison(comparison
);
1899 snprintf(buf
, sizeof(buf
), "%s vs %s", one
, two
);
1900 sm
= get_sm_state(comparison_id
, buf
, NULL
);
1904 FOR_EACH_PTR(sm
->possible
, sm
) {
1905 if (!sm
->state
->data
)
1907 saved
= ((struct compare_data
*)sm
->state
->data
)->comparison
;
1908 if (saved
== comparison
)
1910 if (comparison
== SPECIAL_EQUAL
&&
1911 (saved
== SPECIAL_LTE
||
1912 saved
== SPECIAL_GTE
||
1913 saved
== SPECIAL_UNSIGNED_LTE
||
1914 saved
== SPECIAL_UNSIGNED_GTE
))
1918 } END_FOR_EACH_PTR(sm
);
1927 struct state_list
*get_all_comparisons(struct expression
*expr
)
1929 struct smatch_state
*state
;
1930 struct string_list
*links
;
1931 struct state_list
*ret
= NULL
;
1932 struct sm_state
*sm
;
1935 state
= get_state_chunk(link_id
, expr
);
1938 links
= state
->data
;
1940 FOR_EACH_PTR(links
, tmp
) {
1941 sm
= get_sm_state(comparison_id
, tmp
, NULL
);
1944 // FIXME have to compare name with vsl
1945 add_ptr_list(&ret
, sm
);
1946 } END_FOR_EACH_PTR(tmp
);
1951 struct state_list
*get_all_possible_equal_comparisons(struct expression
*expr
)
1953 struct smatch_state
*state
;
1954 struct string_list
*links
;
1955 struct state_list
*ret
= NULL
;
1956 struct sm_state
*sm
;
1959 state
= get_state_chunk(link_id
, expr
);
1962 links
= state
->data
;
1964 FOR_EACH_PTR(links
, tmp
) {
1965 sm
= get_sm_state(comparison_id
, tmp
, NULL
);
1968 if (!strchr(sm
->state
->name
, '='))
1970 if (strcmp(sm
->state
->name
, "!=") == 0)
1972 add_ptr_list(&ret
, sm
);
1973 } END_FOR_EACH_PTR(tmp
);
1978 struct state_list
*get_all_possible_not_equal_comparisons(struct expression
*expr
)
1980 struct smatch_state
*state
;
1981 struct string_list
*links
;
1982 struct state_list
*ret
= NULL
;
1983 struct sm_state
*sm
;
1984 struct sm_state
*possible
;
1989 state
= get_state_chunk(link_id
, expr
);
1992 links
= state
->data
;
1994 FOR_EACH_PTR(links
, link
) {
1995 sm
= get_sm_state(comparison_id
, link
, NULL
);
1998 FOR_EACH_PTR(sm
->possible
, possible
) {
1999 if (strcmp(possible
->state
->name
, "!=") != 0)
2001 add_ptr_list(&ret
, sm
);
2003 } END_FOR_EACH_PTR(possible
);
2004 } END_FOR_EACH_PTR(link
);
2009 static void update_links_from_call(struct expression
*left
,
2011 struct expression
*right
)
2013 struct string_list
*links
;
2014 struct smatch_state
*state
;
2015 struct compare_data
*data
;
2016 struct symbol
*left_sym
, *right_sym
;
2017 char *left_var
= NULL
;
2018 char *right_var
= NULL
;
2019 struct var_sym_list
*left_vsl
;
2020 struct expression
*expr
;
2022 struct var_sym_list
*vsl
;
2026 left_var
= chunk_to_var_sym(left
, &left_sym
);
2029 left_vsl
= expr_to_vsl(left
);
2030 right_var
= chunk_to_var_sym(right
, &right_sym
);
2034 state
= get_state(link_id
, right_var
, right_sym
);
2037 links
= state
->data
;
2039 FOR_EACH_PTR(links
, tmp
) {
2040 state
= get_state(comparison_id
, tmp
, NULL
);
2041 if (!state
|| !state
->data
)
2044 comparison
= data
->comparison
;
2046 var
= data
->right_var
;
2047 vsl
= data
->right_vsl
;
2048 if (strcmp(var
, right_var
) == 0) {
2050 var
= data
->left_var
;
2051 vsl
= data
->left_vsl
;
2052 comparison
= flip_comparison(comparison
);
2054 comparison
= combine_comparisons(left_compare
, comparison
);
2057 add_comparison_var_sym(left
, left_var
, left_vsl
, comparison
, expr
, var
, vsl
, NULL
);
2058 } END_FOR_EACH_PTR(tmp
);
2061 free_string(right_var
);
2064 static void return_str_comparison(struct expression
*expr
, const char *range
)
2066 struct expression
*arg
;
2070 while (expr
->type
== EXPR_ASSIGNMENT
)
2071 expr
= strip_expr(expr
->right
);
2072 if (expr
->type
!= EXPR_CALL
)
2074 if (!str_to_comparison_arg(range
, expr
, &comparison
, &arg
))
2076 snprintf(buf
, sizeof(buf
), "%s", show_comparison(comparison
));
2077 update_links_from_call(expr
, comparison
, arg
);
2078 add_comparison(expr
, comparison
, arg
);
2081 void __add_comparison_info(struct expression
*expr
, struct expression
*call
, const char *range
)
2083 copy_comparisons(expr
, call
);
2086 static char *get_mask_comparison(struct expression
*expr
, int ignore
)
2088 struct expression
*tmp
, *right
;
2092 /* The return value for "return foo & param;" is <= param */
2095 while ((tmp
= get_assigned_expr(expr
))) {
2096 expr
= strip_expr(tmp
);
2101 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '&')
2104 right
= strip_expr(expr
->right
);
2105 param
= get_param_num(right
);
2106 if (param
< 0 || param
== ignore
)
2109 snprintf(buf
, sizeof(buf
), "[<=$%d]", param
);
2110 return alloc_sname(buf
);
2113 static char *range_comparison_to_param_helper(struct expression
*expr
, char starts_with
, int ignore
)
2115 struct symbol
*param
;
2118 char *ret_str
= NULL
;
2125 var
= chunk_to_var(expr
);
2130 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, param
) {
2136 snprintf(buf
, sizeof(buf
), "%s orig", param
->ident
->name
);
2137 compare
= get_comparison_strings(var
, buf
);
2138 if (compare
== UNKNOWN_COMPARISON
||
2139 compare
== IMPOSSIBLE_COMPARISON
)
2141 if (show_comparison(compare
)[0] != starts_with
)
2143 snprintf(buf
, sizeof(buf
), "[%s$%d]", show_comparison(compare
), i
);
2144 ret_str
= alloc_sname(buf
);
2146 } END_FOR_EACH_PTR(param
);
2155 if (starts_with
== '<')
2156 ret_str
= get_mask_comparison(expr
, ignore
);
2160 char *name_sym_to_param_comparison(const char *name
, struct symbol
*sym
)
2162 struct symbol
*param
;
2168 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, param
) {
2172 snprintf(buf
, sizeof(buf
), "%s orig", param
->ident
->name
);
2173 compare
= get_comparison_strings(name
, buf
);
2174 if (compare
== UNKNOWN_COMPARISON
||
2175 compare
== IMPOSSIBLE_COMPARISON
)
2177 snprintf(buf
, sizeof(buf
), "[%s$%d]", show_comparison(compare
), i
);
2178 return alloc_sname(buf
);
2179 } END_FOR_EACH_PTR(param
);
2184 char *expr_equal_to_param(struct expression
*expr
, int ignore
)
2186 return range_comparison_to_param_helper(expr
, '=', ignore
);
2189 char *expr_lte_to_param(struct expression
*expr
, int ignore
)
2191 return range_comparison_to_param_helper(expr
, '<', ignore
);
2194 char *expr_param_comparison(struct expression
*expr
, int ignore
)
2196 struct symbol
*param
;
2199 char *ret_str
= NULL
;
2203 var
= chunk_to_var(expr
);
2208 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, param
) {
2214 snprintf(buf
, sizeof(buf
), "%s orig", param
->ident
->name
);
2215 compare
= get_comparison_strings(var
, buf
);
2218 snprintf(buf
, sizeof(buf
), "[%s$%d]", show_comparison(compare
), i
);
2219 ret_str
= alloc_sname(buf
);
2221 } END_FOR_EACH_PTR(param
);
2228 char *get_printed_param_name(struct expression
*call
, const char *param_name
, struct symbol
*param_sym
)
2230 struct expression
*arg
;
2233 static char buf
[256];
2238 FOR_EACH_PTR(call
->args
, arg
) {
2241 name
= expr_to_var_sym(arg
, &sym
);
2244 if (sym
!= param_sym
)
2248 if (strncmp(name
, param_name
, len
) != 0)
2250 if (param_name
[len
] == '\0') {
2251 snprintf(buf
, sizeof(buf
), "$%d", i
);
2254 if (param_name
[len
] != '-')
2256 snprintf(buf
, sizeof(buf
), "$%d%s", i
, param_name
+ len
);
2258 } END_FOR_EACH_PTR(arg
);
2263 static void match_call_info(struct expression
*expr
)
2265 struct expression
*arg
;
2266 struct smatch_state
*state
;
2267 struct sm_state
*sm
;
2268 struct compare_data
*data
;
2270 struct string_list
*links
;
2272 const char *right_name
;
2278 FOR_EACH_PTR(expr
->args
, arg
) {
2281 state
= get_state_chunk(link_id
, arg
);
2285 links
= state
->data
;
2286 FOR_EACH_PTR(links
, link
) {
2287 struct var_sym_list
*right_vsl
;
2288 struct var_sym
*right_vs
;
2290 if (strstr(link
, " orig"))
2292 sm
= get_sm_state(comparison_id
, link
, NULL
);
2295 data
= sm
->state
->data
;
2297 data
->comparison
== UNKNOWN_COMPARISON
||
2298 data
->comparison
== IMPOSSIBLE_COMPARISON
)
2300 arg_name
= expr_to_var(arg
);
2305 if (strcmp(data
->left_var
, arg_name
) == 0) {
2306 comparison
= data
->comparison
;
2307 right_name
= data
->right_var
;
2308 right_vsl
= data
->right_vsl
;
2309 } else if (strcmp(data
->right_var
, arg_name
) == 0) {
2310 comparison
= flip_comparison(data
->comparison
);
2311 right_name
= data
->left_var
;
2312 right_vsl
= data
->left_vsl
;
2314 if (!right_vsl
|| ptr_list_size((struct ptr_list
*)right_vsl
) != 1)
2317 right_vs
= first_ptr_list((struct ptr_list
*)right_vsl
);
2318 if (strcmp(right_vs
->var
, right_name
) != 0)
2320 right_name
= get_printed_param_name(expr
, right_vs
->var
, right_vs
->sym
);
2323 snprintf(info_buf
, sizeof(info_buf
), "%s %s", show_comparison(comparison
), right_name
);
2324 sql_insert_caller_info(expr
, PARAM_COMPARE
, i
, "$", info_buf
);
2327 free_string(arg_name
);
2328 } END_FOR_EACH_PTR(link
);
2329 } END_FOR_EACH_PTR(arg
);
2332 static void struct_member_callback(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*link_sm
)
2334 struct sm_state
*compare_sm
;
2335 struct string_list
*links
;
2337 struct compare_data
*data
;
2338 struct var_sym
*left
, *right
;
2339 static char info_buf
[256];
2340 const char *right_name
;
2342 if (strstr(printed_name
, " orig"))
2345 links
= link_sm
->state
->data
;
2346 FOR_EACH_PTR(links
, link
) {
2347 compare_sm
= get_sm_state(comparison_id
, link
, NULL
);
2350 data
= compare_sm
->state
->data
;
2351 if (!data
|| !data
->comparison
)
2354 if (ptr_list_size((struct ptr_list
*)data
->left_vsl
) != 1 ||
2355 ptr_list_size((struct ptr_list
*)data
->right_vsl
) != 1)
2357 left
= first_ptr_list((struct ptr_list
*)data
->left_vsl
);
2358 right
= first_ptr_list((struct ptr_list
*)data
->right_vsl
);
2359 if (left
->sym
== right
->sym
&&
2360 strcmp(left
->var
, right
->var
) == 0)
2363 * Both parameters link to this comparison so only
2364 * record the first one.
2366 if (left
->sym
!= link_sm
->sym
||
2367 strcmp(left
->var
, link_sm
->name
) != 0)
2370 right_name
= get_printed_param_name(call
, right
->var
, right
->sym
);
2373 snprintf(info_buf
, sizeof(info_buf
), "%s %s", show_comparison(data
->comparison
), right_name
);
2374 sql_insert_caller_info(call
, PARAM_COMPARE
, param
, printed_name
, info_buf
);
2375 } END_FOR_EACH_PTR(link
);
2378 static void print_return_value_comparison(int return_id
, char *return_ranges
, struct expression
*expr
)
2381 const char *tmp_name
;
2385 bool is_addr
= false;
2388 * TODO: This only prints == comparisons. That's probably the most
2389 * useful comparison because == max has lots of implications. But it
2390 * would be good to capture the rest as well.
2392 * This information is already in the DB but it's in the parameter math
2393 * bits and it's awkward to use it. This is is the simpler, possibly
2394 * cleaner way, but not necessarily the best, I don't know.
2399 name
= expr_to_var_sym(expr
, &sym
);
2403 param
= get_param_num_from_sym(sym
);
2406 if (param_was_set_var_sym(name
, sym
))
2409 tmp_name
= get_param_name_var_sym(name
, sym
);
2413 if (tmp_name
[0] == '&') {
2420 snprintf(info_buf
, sizeof(info_buf
), "== %s$%d%s", is_addr
? "&" : "", param
, tmp_name
);
2421 sql_insert_return_states(return_id
, return_ranges
,
2422 PARAM_COMPARE
, -1, "$", info_buf
);
2427 static void print_return_comparison(int return_id
, char *return_ranges
, struct expression
*expr
)
2429 struct sm_state
*tmp
;
2430 struct string_list
*links
;
2432 struct sm_state
*sm
;
2433 struct compare_data
*data
;
2434 struct var_sym
*left
, *right
;
2435 int left_param
, right_param
;
2436 const char *left_key
, *right_key
;
2440 print_return_value_comparison(return_id
, return_ranges
, expr
);
2442 FOR_EACH_MY_SM(link_id
, __get_cur_stree(), tmp
) {
2443 left_param
= get_param_key_from_var_sym(tmp
->name
, tmp
->sym
, expr
, &left_key
);
2444 if (left_param
< -1 || !left_key
)
2447 links
= tmp
->state
->data
;
2448 FOR_EACH_PTR(links
, link
) {
2449 if (strncmp(link
, "$size", 5) == 0)
2451 sm
= get_sm_state(comparison_id
, link
, NULL
);
2454 data
= sm
->state
->data
;
2456 data
->comparison
== UNKNOWN_COMPARISON
||
2457 data
->comparison
== IMPOSSIBLE_COMPARISON
)
2459 if (ptr_list_size((struct ptr_list
*)data
->left_vsl
) != 1 ||
2460 ptr_list_size((struct ptr_list
*)data
->right_vsl
) != 1)
2462 left
= first_ptr_list((struct ptr_list
*)data
->left_vsl
);
2463 right
= first_ptr_list((struct ptr_list
*)data
->right_vsl
);
2464 if (left
->sym
== right
->sym
&&
2465 strcmp(left
->var
, right
->var
) == 0)
2468 * The both foo and bar in the "foo == bar" comparison
2469 * have link states. To avoid duplicates only parse
2470 * the comparison if the link state is on the left.
2472 * And actually let's move getting the left_name outside
2476 if (left
->sym
!= tmp
->sym
||
2477 strcmp(left
->var
, tmp
->name
) != 0)
2480 if (strstr(right
->var
, " orig"))
2483 right_param
= get_param_key_from_var_sym(right
->var
, right
->sym
, expr
, &right_key
);
2484 if (right_param
< 0 || !right_key
|| right_key
[0] != '$')
2488 * The same param can have multiple names so we can get
2491 if (left_param
== right_param
&&
2492 strcmp(left_key
, right_key
) == 0)
2495 compare_type
= PARAM_COMPARE
;
2496 if (!param_was_set_var_sym(right
->var
, right
->sym
) &&
2497 !param_was_set_var_sym(left
->var
, left
->sym
))
2498 compare_type
= COMPARE_LIMIT
;
2500 snprintf(info_buf
, sizeof(info_buf
), "%s $%d%s",
2501 show_comparison(data
->comparison
), right_param
, right_key
+ 1);
2502 sql_insert_return_states(return_id
, return_ranges
,
2503 compare_type
, left_param
, left_key
, info_buf
);
2504 } END_FOR_EACH_PTR(link
);
2506 } END_FOR_EACH_SM(tmp
);
2509 static int parse_comparison(char **value
, int *op
)
2517 if (**value
== '=') {
2525 *op
= SPECIAL_EQUAL
;
2530 *op
= SPECIAL_NOTEQUAL
;
2534 if (**value
== '=') {
2543 if (**value
!= ' ') {
2544 sm_perror("parsing comparison. %s", *value
);
2552 static int split_op_param_key(char *value
, int *op
, int *param
, char **key
)
2554 static char buf
[256];
2557 if (!parse_comparison(&value
, op
))
2560 snprintf(buf
, sizeof(buf
), "%s", value
);
2567 if (*param
< 0 || *param
> 99)
2579 void select_caller_info(const char *name
, struct symbol
*sym
, char *value
)
2581 struct var_sym_list
*left_vsl
= NULL
;
2582 struct var_sym_list
*right_vsl
= NULL
;
2583 char *right_key
, *p
, *right_name
;
2584 struct symbol
*right_sym
;
2585 char comparison_name
[128];
2586 char right_buf
[128];
2587 int op
, right_param
;
2589 if (!split_op_param_key(value
, &op
, &right_param
, &right_key
))
2592 right_sym
= get_param_sym_from_num(right_param
);
2593 if (!right_sym
|| !right_sym
->ident
)
2596 p
= strchr(right_key
, '$');
2600 snprintf(right_buf
, sizeof(right_buf
), "%.*s%s%s", (int)(p
- right_key
),
2601 right_key
, right_sym
->ident
->name
, p
+ 1);
2602 right_name
= right_buf
;
2604 if (strcmp(name
, right_name
) > 0) {
2605 struct symbol
*tmp_sym
= sym
;
2606 char *tmp_name
= (char *)name
;
2610 right_name
= tmp_name
;
2611 right_sym
= tmp_sym
;
2612 op
= flip_comparison(op
);
2615 add_var_sym(&left_vsl
, name
, sym
);
2616 add_var_sym(&right_vsl
, right_name
, right_sym
);
2618 snprintf(comparison_name
, sizeof(comparison_name
), "%s vs %s", name
, right_name
);
2620 set_state(comparison_id
, comparison_name
, NULL
,
2621 alloc_compare_state(NULL
, name
, left_vsl
,
2623 NULL
, right_name
, right_vsl
));
2626 static void db_return_comparison(struct expression
*expr
, int left_param
, char *key
, char *value
)
2628 struct expression
*left_arg
, *right_arg
;
2629 char *left_name
= NULL
;
2630 struct symbol
*left_sym
;
2631 char *right_name
= NULL
;
2632 struct symbol
*right_sym
;
2636 struct var_sym_list
*left_vsl
= NULL
, *right_vsl
= NULL
;
2638 if (left_param
== -1) {
2639 if (expr
->type
!= EXPR_ASSIGNMENT
)
2641 left_arg
= strip_expr(expr
->left
);
2643 while (expr
->type
== EXPR_ASSIGNMENT
)
2644 expr
= strip_expr(expr
->right
);
2645 if (expr
->type
!= EXPR_CALL
)
2648 while (expr
->type
== EXPR_ASSIGNMENT
)
2649 expr
= strip_expr(expr
->right
);
2650 if (expr
->type
!= EXPR_CALL
)
2653 left_arg
= get_argument_from_call_expr(expr
->args
, left_param
);
2658 if (!split_op_param_key(value
, &op
, &right_param
, &right_key
))
2661 right_arg
= get_argument_from_call_expr(expr
->args
, right_param
);
2665 left_name
= get_variable_from_key(left_arg
, key
, &left_sym
);
2666 if (!left_name
|| !left_sym
)
2669 right_name
= get_variable_from_key(right_arg
, right_key
, &right_sym
);
2670 if (!right_name
|| !right_sym
)
2673 add_var_sym(&left_vsl
, left_name
, left_sym
);
2674 add_var_sym(&right_vsl
, right_name
, right_sym
);
2676 add_comparison_var_sym(NULL
, left_name
, left_vsl
, op
, NULL
, right_name
, right_vsl
, NULL
);
2679 free_string(left_name
);
2680 free_string(right_name
);
2683 int param_compare_limit_is_impossible(struct expression
*expr
, int left_param
, char *left_key
, char *value
)
2685 char *left_name
= NULL
;
2686 char *right_name
= NULL
;
2687 struct symbol
*left_sym
, *right_sym
;
2688 struct expression
*left_arg
, *right_arg
;
2694 while (expr
->type
== EXPR_ASSIGNMENT
)
2695 expr
= strip_expr(expr
->right
);
2696 if (expr
->type
!= EXPR_CALL
)
2699 if (!split_op_param_key(value
, &op
, &right_param
, &right_key
))
2702 left_arg
= get_argument_from_call_expr(expr
->args
, left_param
);
2706 right_arg
= get_argument_from_call_expr(expr
->args
, right_param
);
2710 left_name
= get_variable_from_key(left_arg
, left_key
, &left_sym
);
2711 right_name
= get_variable_from_key(right_arg
, right_key
, &right_sym
);
2712 if (!left_name
|| !right_name
)
2715 state_op
= get_comparison_strings(left_name
, right_name
);
2719 if (comparison_intersection(remove_unsigned_from_comparison(state_op
), op
)
2720 == IMPOSSIBLE_COMPARISON
)
2723 free_string(left_name
);
2724 free_string(right_name
);
2728 int impossibly_high_comparison(struct expression
*expr
)
2730 struct smatch_state
*link_state
;
2731 struct sm_state
*sm
;
2732 struct string_list
*links
;
2734 struct compare_data
*data
;
2736 link_state
= get_state_expr(link_id
, expr
);
2738 if (expr
->type
== EXPR_BINOP
&&
2739 (impossibly_high_comparison(expr
->left
) ||
2740 impossibly_high_comparison(expr
->right
)))
2745 links
= link_state
->data
;
2746 FOR_EACH_PTR(links
, link
) {
2747 sm
= get_sm_state(comparison_id
, link
, NULL
);
2750 data
= sm
->state
->data
;
2753 if (!possibly_true(data
->left
, data
->comparison
, data
->right
))
2755 } END_FOR_EACH_PTR(link
);
2760 static void free_data(struct symbol
*sym
)
2764 clear_compare_data_alloc();
2767 void register_comparison(int id
)
2770 set_dynamic_states(comparison_id
);
2771 add_hook(&save_start_states
, AFTER_DEF_HOOK
);
2772 add_unmatched_state_hook(comparison_id
, unmatched_comparison
);
2773 add_pre_merge_hook(comparison_id
, &pre_merge_hook
);
2774 add_merge_hook(comparison_id
, &merge_compare_states
);
2775 add_hook(&free_data
, AFTER_FUNC_HOOK
);
2777 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
2778 select_caller_name_sym(&select_caller_info
, PARAM_COMPARE
);
2780 add_split_return_callback(&print_return_comparison
);
2781 select_return_states_hook(PARAM_COMPARE
, &db_return_comparison
);
2783 add_hook(&match_preop
, OP_HOOK
);
2786 void register_comparison_late(int id
)
2788 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
2789 add_return_string_hook(return_str_comparison
);
2792 void register_comparison_links(int id
)
2795 db_ignore_states(link_id
);
2796 set_dynamic_states(link_id
);
2797 add_merge_hook(link_id
, &merge_links
);
2798 add_modification_hook(link_id
, &match_modify
);
2799 add_modification_hook_late(link_id
, match_inc_dec
);
2801 add_member_info_callback(link_id
, struct_member_callback
);
2804 void register_comparison_inc_dec(int id
)
2807 add_modification_hook_late(inc_dec_id
, &iter_modify
);
2810 void register_comparison_inc_dec_links(int id
)
2812 inc_dec_link_id
= id
;
2813 set_dynamic_states(inc_dec_link_id
);
2814 set_up_link_functions(inc_dec_id
, inc_dec_link_id
);
2817 static struct sm_state
*clone_partial_sm(struct sm_state
*sm
, int comparison
)
2819 struct compare_data
*data
;
2820 struct sm_state
*clone
;
2821 struct stree
*stree
;
2823 data
= sm
->state
->data
;
2825 clone
= clone_sm(sm
);
2826 clone
->state
= alloc_compare_state(data
->left
, data
->left_var
, data
->left_vsl
,
2828 data
->right
, data
->right_var
, data
->right_vsl
);
2829 free_slist(&clone
->possible
);
2830 add_possible_sm(clone
, clone
);
2832 stree
= clone_stree(sm
->pool
);
2833 overwrite_sm_state_stree(&stree
, clone
);
2834 clone
->pool
= stree
;
2839 static void create_fake_history(struct sm_state
*sm
, int op
,
2840 struct state_list
**true_stack
,
2841 struct state_list
**false_stack
)
2843 struct sm_state
*true_sm
, *false_sm
;
2844 struct compare_data
*data
;
2845 int true_comparison
;
2846 int false_comparison
;
2848 data
= sm
->state
->data
;
2850 if (is_merged(sm
) || sm
->left
|| sm
->right
)
2853 true_comparison
= comparison_intersection(data
->comparison
, op
);
2854 false_comparison
= comparison_intersection(data
->comparison
, negate_comparison(op
));
2856 true_sm
= clone_partial_sm(sm
, true_comparison
);
2857 false_sm
= clone_partial_sm(sm
, false_comparison
);
2861 sm
->right
= false_sm
;
2863 add_ptr_list(true_stack
, true_sm
);
2864 add_ptr_list(false_stack
, false_sm
);
2867 static void filter_by_sm(struct sm_state
*sm
, int op
,
2868 struct state_list
**true_stack
,
2869 struct state_list
**false_stack
,
2872 struct compare_data
*data
;
2878 data
= sm
->state
->data
;
2881 if (data
->comparison
== IMPOSSIBLE_COMPARISON
)
2885 * We want to check that "data->comparison" is totally inside "op". So
2886 * if data->comparison is < and op is <= then that's true. Or if
2887 * data->comparison is == and op is <= then that's true. But if
2888 * data->comparison is <= and op is < than that's neither true nor
2891 if (data
->comparison
== comparison_intersection(data
->comparison
, op
))
2893 if (data
->comparison
== comparison_intersection(data
->comparison
, negate_comparison(op
)))
2896 if (!is_true
&& !is_false
&& !is_merged(sm
)) {
2897 create_fake_history(sm
, op
, true_stack
, false_stack
);
2901 if (debug_implied()) {
2902 sm_msg("%s: %s: op = '%s' negated '%s'. true_intersect = '%s' false_insersect = '%s' sm = '%s'",
2905 alloc_sname(show_comparison(op
)),
2906 alloc_sname(show_comparison(negate_comparison(op
))),
2907 alloc_sname(show_comparison(comparison_intersection(data
->comparison
, op
))),
2908 alloc_sname(show_comparison(comparison_intersection(data
->comparison
, negate_comparison(op
)))),
2914 add_ptr_list(true_stack
, sm
);
2916 add_ptr_list(false_stack
, sm
);
2918 filter_by_sm(sm
->left
, op
, true_stack
, false_stack
, useful
);
2919 filter_by_sm(sm
->right
, op
, true_stack
, false_stack
, useful
);
2922 struct sm_state
*comparison_implication_hook(struct expression
*expr
,
2923 struct state_list
**true_stack
,
2924 struct state_list
**false_stack
)
2926 struct sm_state
*sm
;
2929 static char buf
[256];
2930 bool useful
= false;
2932 if (expr
->type
!= EXPR_COMPARE
)
2937 left
= expr_to_var(expr
->left
);
2938 right
= expr_to_var(expr
->right
);
2939 if (!left
|| !right
) {
2945 if (strcmp(left
, right
) > 0) {
2950 op
= flip_comparison(op
);
2953 snprintf(buf
, sizeof(buf
), "%s vs %s", left
, right
);
2954 sm
= get_sm_state(comparison_id
, buf
, NULL
);
2960 filter_by_sm(sm
, op
, true_stack
, false_stack
, &useful
);
2964 if (debug_implied())
2965 sm_msg("implications from comparison: (%s)", show_sm(sm
));