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 move_plus_to_minus(&a
, &b
);
1804 simplify_binops(&a
, &b
);
1806 one
= chunk_to_var(a
);
1809 two
= chunk_to_var(b
);
1813 ret
= get_comparison_strings(one
, two
);
1817 if (is_plus_one(a
) || is_minus_one(a
)) {
1819 one
= chunk_to_var(a
->left
);
1820 ret
= get_comparison_strings(one
, two
);
1821 } else if (is_plus_one(b
) || is_minus_one(b
)) {
1823 two
= chunk_to_var(b
->left
);
1824 ret
= get_comparison_strings(one
, two
);
1827 if (ret
== UNKNOWN_COMPARISON
)
1830 if ((is_plus_one(a
) || is_minus_one(b
)) && ret
== '<')
1832 else if ((is_minus_one(a
) || is_plus_one(b
)) && ret
== '>')
1835 ret
= UNKNOWN_COMPARISON
;
1842 extra
= comparison_from_extra(a
, b
);
1843 return comparison_intersection(ret
, extra
);
1846 int get_comparison(struct expression
*a
, struct expression
*b
)
1848 return get_comparison_helper(a
, b
, true);
1851 int get_comparison_no_extra(struct expression
*a
, struct expression
*b
)
1853 return get_comparison_helper(a
, b
, false);
1856 int possible_comparison(struct expression
*a
, int comparison
, struct expression
*b
)
1862 struct sm_state
*sm
;
1865 one
= chunk_to_var(a
);
1868 two
= chunk_to_var(b
);
1873 if (strcmp(one
, two
) == 0 && comparison
== SPECIAL_EQUAL
) {
1878 if (strcmp(one
, two
) > 0) {
1883 comparison
= flip_comparison(comparison
);
1886 snprintf(buf
, sizeof(buf
), "%s vs %s", one
, two
);
1887 sm
= get_sm_state(comparison_id
, buf
, NULL
);
1891 FOR_EACH_PTR(sm
->possible
, sm
) {
1892 if (!sm
->state
->data
)
1894 saved
= ((struct compare_data
*)sm
->state
->data
)->comparison
;
1895 if (saved
== comparison
)
1897 if (comparison
== SPECIAL_EQUAL
&&
1898 (saved
== SPECIAL_LTE
||
1899 saved
== SPECIAL_GTE
||
1900 saved
== SPECIAL_UNSIGNED_LTE
||
1901 saved
== SPECIAL_UNSIGNED_GTE
))
1905 } END_FOR_EACH_PTR(sm
);
1914 struct state_list
*get_all_comparisons(struct expression
*expr
)
1916 struct smatch_state
*state
;
1917 struct string_list
*links
;
1918 struct state_list
*ret
= NULL
;
1919 struct sm_state
*sm
;
1922 state
= get_state_chunk(link_id
, expr
);
1925 links
= state
->data
;
1927 FOR_EACH_PTR(links
, tmp
) {
1928 sm
= get_sm_state(comparison_id
, tmp
, NULL
);
1931 // FIXME have to compare name with vsl
1932 add_ptr_list(&ret
, sm
);
1933 } END_FOR_EACH_PTR(tmp
);
1938 struct state_list
*get_all_possible_equal_comparisons(struct expression
*expr
)
1940 struct smatch_state
*state
;
1941 struct string_list
*links
;
1942 struct state_list
*ret
= NULL
;
1943 struct sm_state
*sm
;
1946 state
= get_state_chunk(link_id
, expr
);
1949 links
= state
->data
;
1951 FOR_EACH_PTR(links
, tmp
) {
1952 sm
= get_sm_state(comparison_id
, tmp
, NULL
);
1955 if (!strchr(sm
->state
->name
, '='))
1957 if (strcmp(sm
->state
->name
, "!=") == 0)
1959 add_ptr_list(&ret
, sm
);
1960 } END_FOR_EACH_PTR(tmp
);
1965 struct state_list
*get_all_possible_not_equal_comparisons(struct expression
*expr
)
1967 struct smatch_state
*state
;
1968 struct string_list
*links
;
1969 struct state_list
*ret
= NULL
;
1970 struct sm_state
*sm
;
1971 struct sm_state
*possible
;
1976 state
= get_state_chunk(link_id
, expr
);
1979 links
= state
->data
;
1981 FOR_EACH_PTR(links
, link
) {
1982 sm
= get_sm_state(comparison_id
, link
, NULL
);
1985 FOR_EACH_PTR(sm
->possible
, possible
) {
1986 if (strcmp(possible
->state
->name
, "!=") != 0)
1988 add_ptr_list(&ret
, sm
);
1990 } END_FOR_EACH_PTR(possible
);
1991 } END_FOR_EACH_PTR(link
);
1996 static void update_links_from_call(struct expression
*left
,
1998 struct expression
*right
)
2000 struct string_list
*links
;
2001 struct smatch_state
*state
;
2002 struct compare_data
*data
;
2003 struct symbol
*left_sym
, *right_sym
;
2004 char *left_var
= NULL
;
2005 char *right_var
= NULL
;
2006 struct var_sym_list
*left_vsl
;
2007 struct expression
*expr
;
2009 struct var_sym_list
*vsl
;
2013 left_var
= chunk_to_var_sym(left
, &left_sym
);
2016 left_vsl
= expr_to_vsl(left
);
2017 right_var
= chunk_to_var_sym(right
, &right_sym
);
2021 state
= get_state(link_id
, right_var
, right_sym
);
2024 links
= state
->data
;
2026 FOR_EACH_PTR(links
, tmp
) {
2027 state
= get_state(comparison_id
, tmp
, NULL
);
2028 if (!state
|| !state
->data
)
2031 comparison
= data
->comparison
;
2033 var
= data
->right_var
;
2034 vsl
= data
->right_vsl
;
2035 if (strcmp(var
, right_var
) == 0) {
2037 var
= data
->left_var
;
2038 vsl
= data
->left_vsl
;
2039 comparison
= flip_comparison(comparison
);
2041 comparison
= combine_comparisons(left_compare
, comparison
);
2044 add_comparison_var_sym(left
, left_var
, left_vsl
, comparison
, expr
, var
, vsl
, NULL
);
2045 } END_FOR_EACH_PTR(tmp
);
2048 free_string(right_var
);
2051 static void return_str_comparison(struct expression
*expr
, const char *range
)
2053 struct expression
*arg
;
2057 while (expr
->type
== EXPR_ASSIGNMENT
)
2058 expr
= strip_expr(expr
->right
);
2059 if (expr
->type
!= EXPR_CALL
)
2061 if (!str_to_comparison_arg(range
, expr
, &comparison
, &arg
))
2063 snprintf(buf
, sizeof(buf
), "%s", show_comparison(comparison
));
2064 update_links_from_call(expr
, comparison
, arg
);
2065 add_comparison(expr
, comparison
, arg
);
2068 void __add_comparison_info(struct expression
*expr
, struct expression
*call
, const char *range
)
2070 copy_comparisons(expr
, call
);
2073 static char *get_mask_comparison(struct expression
*expr
, int ignore
)
2075 struct expression
*tmp
, *right
;
2079 /* The return value for "return foo & param;" is <= param */
2082 while ((tmp
= get_assigned_expr(expr
))) {
2083 expr
= strip_expr(tmp
);
2088 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '&')
2091 right
= strip_expr(expr
->right
);
2092 param
= get_param_num(right
);
2093 if (param
< 0 || param
== ignore
)
2096 snprintf(buf
, sizeof(buf
), "[<=$%d]", param
);
2097 return alloc_sname(buf
);
2100 static char *range_comparison_to_param_helper(struct expression
*expr
, char starts_with
, int ignore
)
2102 struct symbol
*param
;
2105 char *ret_str
= NULL
;
2112 var
= chunk_to_var(expr
);
2117 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, param
) {
2123 snprintf(buf
, sizeof(buf
), "%s orig", param
->ident
->name
);
2124 compare
= get_comparison_strings(var
, buf
);
2125 if (compare
== UNKNOWN_COMPARISON
||
2126 compare
== IMPOSSIBLE_COMPARISON
)
2128 if (show_comparison(compare
)[0] != starts_with
)
2130 snprintf(buf
, sizeof(buf
), "[%s$%d]", show_comparison(compare
), i
);
2131 ret_str
= alloc_sname(buf
);
2133 } END_FOR_EACH_PTR(param
);
2142 if (starts_with
== '<')
2143 ret_str
= get_mask_comparison(expr
, ignore
);
2147 char *name_sym_to_param_comparison(const char *name
, struct symbol
*sym
)
2149 struct symbol
*param
;
2155 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, param
) {
2159 snprintf(buf
, sizeof(buf
), "%s orig", param
->ident
->name
);
2160 compare
= get_comparison_strings(name
, buf
);
2161 if (compare
== UNKNOWN_COMPARISON
||
2162 compare
== IMPOSSIBLE_COMPARISON
)
2164 snprintf(buf
, sizeof(buf
), "[%s$%d]", show_comparison(compare
), i
);
2165 return alloc_sname(buf
);
2166 } END_FOR_EACH_PTR(param
);
2171 char *expr_equal_to_param(struct expression
*expr
, int ignore
)
2173 return range_comparison_to_param_helper(expr
, '=', ignore
);
2176 char *expr_lte_to_param(struct expression
*expr
, int ignore
)
2178 return range_comparison_to_param_helper(expr
, '<', ignore
);
2181 char *expr_param_comparison(struct expression
*expr
, int ignore
)
2183 struct symbol
*param
;
2186 char *ret_str
= NULL
;
2190 var
= chunk_to_var(expr
);
2195 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, param
) {
2201 snprintf(buf
, sizeof(buf
), "%s orig", param
->ident
->name
);
2202 compare
= get_comparison_strings(var
, buf
);
2205 snprintf(buf
, sizeof(buf
), "[%s$%d]", show_comparison(compare
), i
);
2206 ret_str
= alloc_sname(buf
);
2208 } END_FOR_EACH_PTR(param
);
2215 char *get_printed_param_name(struct expression
*call
, const char *param_name
, struct symbol
*param_sym
)
2217 struct expression
*arg
;
2220 static char buf
[256];
2225 FOR_EACH_PTR(call
->args
, arg
) {
2228 name
= expr_to_var_sym(arg
, &sym
);
2231 if (sym
!= param_sym
)
2235 if (strncmp(name
, param_name
, len
) != 0)
2237 if (param_name
[len
] == '\0') {
2238 snprintf(buf
, sizeof(buf
), "$%d", i
);
2241 if (param_name
[len
] != '-')
2243 snprintf(buf
, sizeof(buf
), "$%d%s", i
, param_name
+ len
);
2245 } END_FOR_EACH_PTR(arg
);
2250 static void match_call_info(struct expression
*expr
)
2252 struct expression
*arg
;
2253 struct smatch_state
*state
;
2254 struct sm_state
*sm
;
2255 struct compare_data
*data
;
2257 struct string_list
*links
;
2259 const char *right_name
;
2265 FOR_EACH_PTR(expr
->args
, arg
) {
2268 state
= get_state_chunk(link_id
, arg
);
2272 links
= state
->data
;
2273 FOR_EACH_PTR(links
, link
) {
2274 struct var_sym_list
*right_vsl
;
2275 struct var_sym
*right_vs
;
2278 if (strstr(link
, " orig"))
2280 sm
= get_sm_state(comparison_id
, link
, NULL
);
2283 data
= sm
->state
->data
;
2285 data
->comparison
== UNKNOWN_COMPARISON
||
2286 data
->comparison
== IMPOSSIBLE_COMPARISON
)
2288 arg_name
= expr_to_var(arg
);
2293 if (strcmp(data
->left_var
, arg_name
) == 0) {
2294 comparison
= data
->comparison
;
2295 right_name
= data
->right_var
;
2296 right_vsl
= data
->right_vsl
;
2297 } else if (strcmp(data
->right_var
, arg_name
) == 0) {
2298 comparison
= flip_comparison(data
->comparison
);
2299 right_name
= data
->left_var
;
2300 right_vsl
= data
->left_vsl
;
2302 if (!right_vsl
|| ptr_list_size((struct ptr_list
*)right_vsl
) != 1)
2305 right_vs
= first_ptr_list((struct ptr_list
*)right_vsl
);
2306 if (strcmp(right_vs
->var
, right_name
) != 0)
2308 right_name
= get_printed_param_name(expr
, right_vs
->var
, right_vs
->sym
);
2311 snprintf(info_buf
, sizeof(info_buf
), "%s %s", show_comparison(comparison
), right_name
);
2312 sql_insert_caller_info(expr
, PARAM_COMPARE
, i
, "$", info_buf
);
2315 free_string(arg_name
);
2316 } END_FOR_EACH_PTR(link
);
2317 } END_FOR_EACH_PTR(arg
);
2320 static void struct_member_callback(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*link_sm
)
2322 struct sm_state
*compare_sm
;
2323 struct string_list
*links
;
2325 struct compare_data
*data
;
2326 struct var_sym
*left
, *right
;
2327 static char info_buf
[256];
2328 const char *right_name
;
2330 if (strstr(printed_name
, " orig"))
2333 links
= link_sm
->state
->data
;
2334 FOR_EACH_PTR(links
, link
) {
2335 compare_sm
= get_sm_state(comparison_id
, link
, NULL
);
2338 data
= compare_sm
->state
->data
;
2339 if (!data
|| !data
->comparison
)
2342 if (ptr_list_size((struct ptr_list
*)data
->left_vsl
) != 1 ||
2343 ptr_list_size((struct ptr_list
*)data
->right_vsl
) != 1)
2345 left
= first_ptr_list((struct ptr_list
*)data
->left_vsl
);
2346 right
= first_ptr_list((struct ptr_list
*)data
->right_vsl
);
2347 if (left
->sym
== right
->sym
&&
2348 strcmp(left
->var
, right
->var
) == 0)
2351 * Both parameters link to this comparison so only
2352 * record the first one.
2354 if (left
->sym
!= link_sm
->sym
||
2355 strcmp(left
->var
, link_sm
->name
) != 0)
2358 right_name
= get_printed_param_name(call
, right
->var
, right
->sym
);
2361 snprintf(info_buf
, sizeof(info_buf
), "%s %s", show_comparison(data
->comparison
), right_name
);
2362 sql_insert_caller_info(call
, PARAM_COMPARE
, param
, printed_name
, info_buf
);
2363 } END_FOR_EACH_PTR(link
);
2366 static void print_return_value_comparison(int return_id
, char *return_ranges
, struct expression
*expr
)
2369 const char *tmp_name
;
2373 bool is_addr
= false;
2376 * TODO: This only prints == comparisons. That's probably the most
2377 * useful comparison because == max has lots of implications. But it
2378 * would be good to capture the rest as well.
2380 * This information is already in the DB but it's in the parameter math
2381 * bits and it's awkward to use it. This is is the simpler, possibly
2382 * cleaner way, but not necessarily the best, I don't know.
2387 name
= expr_to_var_sym(expr
, &sym
);
2391 param
= get_param_num_from_sym(sym
);
2394 if (param_was_set_var_sym(name
, sym
))
2397 tmp_name
= get_param_name_var_sym(name
, sym
);
2401 if (tmp_name
[0] == '&') {
2408 snprintf(info_buf
, sizeof(info_buf
), "== %s$%d%s", is_addr
? "&" : "", param
, tmp_name
);
2409 sql_insert_return_states(return_id
, return_ranges
,
2410 PARAM_COMPARE
, -1, "$", info_buf
);
2415 static void print_return_comparison(int return_id
, char *return_ranges
, struct expression
*expr
)
2417 struct sm_state
*tmp
;
2418 struct string_list
*links
;
2420 struct sm_state
*sm
;
2421 struct compare_data
*data
;
2422 struct var_sym
*left
, *right
;
2423 int left_param
, right_param
;
2424 const char *left_key
, *right_key
;
2427 print_return_value_comparison(return_id
, return_ranges
, expr
);
2429 FOR_EACH_MY_SM(link_id
, __get_cur_stree(), tmp
) {
2430 left_param
= get_param_key_from_var_sym(tmp
->name
, tmp
->sym
, expr
, &left_key
);
2431 if (left_param
< -1 || !left_key
)
2434 links
= tmp
->state
->data
;
2435 FOR_EACH_PTR(links
, link
) {
2436 if (strncmp(link
, "$size", 5) == 0)
2438 sm
= get_sm_state(comparison_id
, link
, NULL
);
2441 data
= sm
->state
->data
;
2443 data
->comparison
== UNKNOWN_COMPARISON
||
2444 data
->comparison
== IMPOSSIBLE_COMPARISON
)
2446 if (ptr_list_size((struct ptr_list
*)data
->left_vsl
) != 1 ||
2447 ptr_list_size((struct ptr_list
*)data
->right_vsl
) != 1)
2449 left
= first_ptr_list((struct ptr_list
*)data
->left_vsl
);
2450 right
= first_ptr_list((struct ptr_list
*)data
->right_vsl
);
2451 if (left
->sym
== right
->sym
&&
2452 strcmp(left
->var
, right
->var
) == 0)
2455 * The both foo and bar in the "foo == bar" comparison
2456 * have link states. To avoid duplicates only parse
2457 * the comparison if the link state is on the left.
2459 * And actually let's move getting the left_name outside
2463 if (left
->sym
!= tmp
->sym
||
2464 strcmp(left
->var
, tmp
->name
) != 0)
2467 if (strstr(right
->var
, " orig"))
2470 right_param
= get_param_key_from_var_sym(right
->var
, right
->sym
, expr
, &right_key
);
2471 if (right_param
< 0 || !right_key
|| right_key
[0] != '$')
2475 * The same param can have multiple names so we can get
2478 if (left_param
== right_param
&&
2479 strcmp(left_key
, right_key
) == 0)
2482 snprintf(info_buf
, sizeof(info_buf
), "%s $%d%s",
2483 show_comparison(data
->comparison
), right_param
, right_key
+ 1);
2484 sql_insert_return_states(return_id
, return_ranges
,
2485 PARAM_COMPARE
, left_param
, left_key
, info_buf
);
2486 } END_FOR_EACH_PTR(link
);
2488 } END_FOR_EACH_SM(tmp
);
2491 static int parse_comparison(char **value
, int *op
)
2499 if (**value
== '=') {
2507 *op
= SPECIAL_EQUAL
;
2512 *op
= SPECIAL_NOTEQUAL
;
2516 if (**value
== '=') {
2525 if (**value
!= ' ') {
2526 sm_perror("parsing comparison. %s", *value
);
2534 static int split_op_param_key(char *value
, int *op
, int *param
, char **key
)
2536 static char buf
[256];
2539 if (!parse_comparison(&value
, op
))
2542 snprintf(buf
, sizeof(buf
), "%s", value
);
2549 if (*param
< 0 || *param
> 99)
2561 void select_caller_info(const char *name
, struct symbol
*sym
, char *value
)
2563 struct var_sym_list
*left_vsl
= NULL
;
2564 struct var_sym_list
*right_vsl
= NULL
;
2565 char *right_key
, *p
, *right_name
;
2566 struct symbol
*right_sym
;
2567 char comparison_name
[128];
2568 char right_buf
[128];
2569 int op
, right_param
;
2571 if (!split_op_param_key(value
, &op
, &right_param
, &right_key
))
2574 right_sym
= get_param_sym_from_num(right_param
);
2575 if (!right_sym
|| !right_sym
->ident
)
2578 p
= strchr(right_key
, '$');
2582 snprintf(right_buf
, sizeof(right_buf
), "%.*s%s%s", (int)(p
- right_key
),
2583 right_key
, right_sym
->ident
->name
, p
+ 1);
2584 right_name
= right_buf
;
2586 if (strcmp(name
, right_name
) > 0) {
2587 struct symbol
*tmp_sym
= sym
;
2588 char *tmp_name
= (char *)name
;
2592 right_name
= tmp_name
;
2593 right_sym
= tmp_sym
;
2594 op
= flip_comparison(op
);
2597 add_var_sym(&left_vsl
, name
, sym
);
2598 add_var_sym(&right_vsl
, right_name
, right_sym
);
2600 snprintf(comparison_name
, sizeof(comparison_name
), "%s vs %s", name
, right_name
);
2602 set_state(comparison_id
, comparison_name
, NULL
,
2603 alloc_compare_state(NULL
, name
, left_vsl
,
2605 NULL
, right_name
, right_vsl
));
2608 static void db_return_comparison(struct expression
*expr
, int left_param
, char *key
, char *value
)
2610 struct expression
*left_arg
, *right_arg
;
2611 char *left_name
= NULL
;
2612 struct symbol
*left_sym
;
2613 char *right_name
= NULL
;
2614 struct symbol
*right_sym
;
2618 struct var_sym_list
*left_vsl
= NULL
, *right_vsl
= NULL
;
2620 if (left_param
== -1) {
2621 if (expr
->type
!= EXPR_ASSIGNMENT
)
2623 left_arg
= strip_expr(expr
->left
);
2625 while (expr
->type
== EXPR_ASSIGNMENT
)
2626 expr
= strip_expr(expr
->right
);
2627 if (expr
->type
!= EXPR_CALL
)
2630 while (expr
->type
== EXPR_ASSIGNMENT
)
2631 expr
= strip_expr(expr
->right
);
2632 if (expr
->type
!= EXPR_CALL
)
2635 left_arg
= get_argument_from_call_expr(expr
->args
, left_param
);
2640 if (!split_op_param_key(value
, &op
, &right_param
, &right_key
))
2643 right_arg
= get_argument_from_call_expr(expr
->args
, right_param
);
2647 left_name
= get_variable_from_key(left_arg
, key
, &left_sym
);
2648 if (!left_name
|| !left_sym
)
2651 right_name
= get_variable_from_key(right_arg
, right_key
, &right_sym
);
2652 if (!right_name
|| !right_sym
)
2655 add_var_sym(&left_vsl
, left_name
, left_sym
);
2656 add_var_sym(&right_vsl
, right_name
, right_sym
);
2658 add_comparison_var_sym(NULL
, left_name
, left_vsl
, op
, NULL
, right_name
, right_vsl
, NULL
);
2661 free_string(left_name
);
2662 free_string(right_name
);
2665 int param_compare_limit_is_impossible(struct expression
*expr
, int left_param
, char *left_key
, char *value
)
2667 char *left_name
= NULL
;
2668 char *right_name
= NULL
;
2669 struct symbol
*left_sym
, *right_sym
;
2670 struct expression
*left_arg
, *right_arg
;
2676 while (expr
->type
== EXPR_ASSIGNMENT
)
2677 expr
= strip_expr(expr
->right
);
2678 if (expr
->type
!= EXPR_CALL
)
2681 if (!split_op_param_key(value
, &op
, &right_param
, &right_key
))
2684 left_arg
= get_argument_from_call_expr(expr
->args
, left_param
);
2688 right_arg
= get_argument_from_call_expr(expr
->args
, right_param
);
2692 left_name
= get_variable_from_key(left_arg
, left_key
, &left_sym
);
2693 right_name
= get_variable_from_key(right_arg
, right_key
, &right_sym
);
2694 if (!left_name
|| !right_name
)
2697 state_op
= get_comparison_strings(left_name
, right_name
);
2701 if (comparison_intersection(remove_unsigned_from_comparison(state_op
), op
)
2702 == IMPOSSIBLE_COMPARISON
)
2705 free_string(left_name
);
2706 free_string(right_name
);
2710 int impossibly_high_comparison(struct expression
*expr
)
2712 struct smatch_state
*link_state
;
2713 struct sm_state
*sm
;
2714 struct string_list
*links
;
2716 struct compare_data
*data
;
2718 link_state
= get_state_expr(link_id
, expr
);
2720 if (expr
->type
== EXPR_BINOP
&&
2721 (impossibly_high_comparison(expr
->left
) ||
2722 impossibly_high_comparison(expr
->right
)))
2727 links
= link_state
->data
;
2728 FOR_EACH_PTR(links
, link
) {
2729 sm
= get_sm_state(comparison_id
, link
, NULL
);
2732 data
= sm
->state
->data
;
2735 if (!possibly_true(data
->left
, data
->comparison
, data
->right
))
2737 } END_FOR_EACH_PTR(link
);
2742 static void free_data(struct symbol
*sym
)
2746 clear_compare_data_alloc();
2749 void register_comparison(int id
)
2752 set_dynamic_states(comparison_id
);
2753 add_hook(&save_start_states
, AFTER_DEF_HOOK
);
2754 add_unmatched_state_hook(comparison_id
, unmatched_comparison
);
2755 add_pre_merge_hook(comparison_id
, &pre_merge_hook
);
2756 add_merge_hook(comparison_id
, &merge_compare_states
);
2757 add_hook(&free_data
, AFTER_FUNC_HOOK
);
2759 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
2760 select_caller_name_sym(&select_caller_info
, PARAM_COMPARE
);
2762 add_split_return_callback(&print_return_comparison
);
2763 select_return_states_hook(PARAM_COMPARE
, &db_return_comparison
);
2765 add_hook(&match_preop
, OP_HOOK
);
2768 void register_comparison_late(int id
)
2770 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
2771 add_return_string_hook(return_str_comparison
);
2774 void register_comparison_links(int id
)
2777 db_ignore_states(link_id
);
2778 set_dynamic_states(link_id
);
2779 add_merge_hook(link_id
, &merge_links
);
2780 add_modification_hook(link_id
, &match_modify
);
2781 add_modification_hook_late(link_id
, match_inc_dec
);
2783 add_member_info_callback(link_id
, struct_member_callback
);
2786 void register_comparison_inc_dec(int id
)
2789 add_modification_hook_late(inc_dec_id
, &iter_modify
);
2792 void register_comparison_inc_dec_links(int id
)
2794 inc_dec_link_id
= id
;
2795 set_dynamic_states(inc_dec_link_id
);
2796 set_up_link_functions(inc_dec_id
, inc_dec_link_id
);
2799 static struct sm_state
*clone_partial_sm(struct sm_state
*sm
, int comparison
)
2801 struct compare_data
*data
;
2802 struct sm_state
*clone
;
2803 struct stree
*stree
;
2805 data
= sm
->state
->data
;
2807 clone
= clone_sm(sm
);
2808 clone
->state
= alloc_compare_state(data
->left
, data
->left_var
, data
->left_vsl
,
2810 data
->right
, data
->right_var
, data
->right_vsl
);
2811 free_slist(&clone
->possible
);
2812 add_possible_sm(clone
, clone
);
2814 stree
= clone_stree(sm
->pool
);
2815 overwrite_sm_state_stree(&stree
, clone
);
2816 clone
->pool
= stree
;
2821 static void create_fake_history(struct sm_state
*sm
, int op
,
2822 struct state_list
**true_stack
,
2823 struct state_list
**false_stack
)
2825 struct sm_state
*true_sm
, *false_sm
;
2826 struct compare_data
*data
;
2827 int true_comparison
;
2828 int false_comparison
;
2830 data
= sm
->state
->data
;
2832 if (is_merged(sm
) || sm
->left
|| sm
->right
)
2835 true_comparison
= comparison_intersection(data
->comparison
, op
);
2836 false_comparison
= comparison_intersection(data
->comparison
, negate_comparison(op
));
2838 true_sm
= clone_partial_sm(sm
, true_comparison
);
2839 false_sm
= clone_partial_sm(sm
, false_comparison
);
2843 sm
->right
= false_sm
;
2845 add_ptr_list(true_stack
, true_sm
);
2846 add_ptr_list(false_stack
, false_sm
);
2849 static void filter_by_sm(struct sm_state
*sm
, int op
,
2850 struct state_list
**true_stack
,
2851 struct state_list
**false_stack
,
2854 struct compare_data
*data
;
2860 data
= sm
->state
->data
;
2863 if (data
->comparison
== IMPOSSIBLE_COMPARISON
)
2867 * We want to check that "data->comparison" is totally inside "op". So
2868 * if data->comparison is < and op is <= then that's true. Or if
2869 * data->comparison is == and op is <= then that's true. But if
2870 * data->comparison is <= and op is < than that's neither true nor
2873 if (data
->comparison
== comparison_intersection(data
->comparison
, op
))
2875 if (data
->comparison
== comparison_intersection(data
->comparison
, negate_comparison(op
)))
2878 if (!is_true
&& !is_false
&& !is_merged(sm
)) {
2879 create_fake_history(sm
, op
, true_stack
, false_stack
);
2883 if (debug_implied()) {
2884 sm_msg("%s: %s: op = '%s' negated '%s'. true_intersect = '%s' false_insersect = '%s' sm = '%s'",
2887 alloc_sname(show_comparison(op
)),
2888 alloc_sname(show_comparison(negate_comparison(op
))),
2889 alloc_sname(show_comparison(comparison_intersection(data
->comparison
, op
))),
2890 alloc_sname(show_comparison(comparison_intersection(data
->comparison
, negate_comparison(op
)))),
2896 add_ptr_list(true_stack
, sm
);
2898 add_ptr_list(false_stack
, sm
);
2900 filter_by_sm(sm
->left
, op
, true_stack
, false_stack
, useful
);
2901 filter_by_sm(sm
->right
, op
, true_stack
, false_stack
, useful
);
2904 struct sm_state
*comparison_implication_hook(struct expression
*expr
,
2905 struct state_list
**true_stack
,
2906 struct state_list
**false_stack
)
2908 struct sm_state
*sm
;
2911 static char buf
[256];
2912 bool useful
= false;
2914 if (expr
->type
!= EXPR_COMPARE
)
2919 left
= expr_to_var(expr
->left
);
2920 right
= expr_to_var(expr
->right
);
2921 if (!left
|| !right
) {
2927 if (strcmp(left
, right
) > 0) {
2932 op
= flip_comparison(op
);
2935 snprintf(buf
, sizeof(buf
), "%s vs %s", left
, right
);
2936 sm
= get_sm_state(comparison_id
, buf
, NULL
);
2942 filter_by_sm(sm
, op
, true_stack
, false_stack
, &useful
);
2946 if (debug_implied())
2947 sm_msg("implications from comparison: (%s)", show_sm(sm
));