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"
35 static int compare_id
;
37 static int inc_dec_id
;
38 static int inc_dec_link_id
;
40 /* for handling for loops */
44 ALLOCATOR(compare_data
, "compare data");
46 static struct symbol
*vsl_to_sym(struct var_sym_list
*vsl
)
52 if (ptr_list_size((struct ptr_list
*)vsl
) != 1)
54 vs
= first_ptr_list((struct ptr_list
*)vsl
);
58 static struct smatch_state
*alloc_compare_state(
59 const char *var1
, struct var_sym_list
*vsl1
,
61 const char *var2
, struct var_sym_list
*vsl2
)
63 struct smatch_state
*state
;
64 struct compare_data
*data
;
66 state
= __alloc_smatch_state(0);
67 state
->name
= alloc_sname(show_special(comparison
));
68 data
= __alloc_compare_data(0);
69 data
->var1
= alloc_sname(var1
);
70 data
->vsl1
= clone_var_sym_list(vsl1
);
71 data
->comparison
= comparison
;
72 data
->var2
= alloc_sname(var2
);
73 data
->vsl2
= clone_var_sym_list(vsl2
);
78 static int state_to_comparison(struct smatch_state
*state
)
80 if (!state
|| !state
->data
)
82 return ((struct compare_data
*)state
->data
)->comparison
;
86 * flip_comparison() reverses the op left and right. So "x >= y" becomes "y <= x".
88 int flip_comparison(int op
)
95 case SPECIAL_UNSIGNED_LT
:
96 return SPECIAL_UNSIGNED_GT
;
99 case SPECIAL_UNSIGNED_LTE
:
100 return SPECIAL_UNSIGNED_GTE
;
102 return SPECIAL_EQUAL
;
103 case SPECIAL_NOTEQUAL
:
104 return SPECIAL_NOTEQUAL
;
107 case SPECIAL_UNSIGNED_GTE
:
108 return SPECIAL_UNSIGNED_LTE
;
111 case SPECIAL_UNSIGNED_GT
:
112 return SPECIAL_UNSIGNED_LT
;
114 sm_msg("internal smatch bug. unhandled comparison %d", op
);
119 int negate_comparison(int op
)
126 case SPECIAL_UNSIGNED_LT
:
127 return SPECIAL_UNSIGNED_GTE
;
130 case SPECIAL_UNSIGNED_LTE
:
131 return SPECIAL_UNSIGNED_GT
;
133 return SPECIAL_NOTEQUAL
;
134 case SPECIAL_NOTEQUAL
:
135 return SPECIAL_EQUAL
;
138 case SPECIAL_UNSIGNED_GTE
:
139 return SPECIAL_UNSIGNED_LT
;
142 case SPECIAL_UNSIGNED_GT
:
143 return SPECIAL_UNSIGNED_LTE
;
145 sm_msg("internal smatch bug. unhandled comparison %d", op
);
150 static int rl_comparison(struct range_list
*left_rl
, struct range_list
*right_rl
)
152 sval_t left_min
, left_max
, right_min
, right_max
;
154 if (!left_rl
|| !right_rl
)
157 left_min
= rl_min(left_rl
);
158 left_max
= rl_max(left_rl
);
159 right_min
= rl_min(right_rl
);
160 right_max
= rl_max(right_rl
);
162 if (left_min
.value
== left_max
.value
&&
163 right_min
.value
== right_max
.value
&&
164 left_min
.value
== right_min
.value
)
165 return SPECIAL_EQUAL
;
167 if (sval_cmp(left_max
, right_min
) < 0)
169 if (sval_cmp(left_max
, right_min
) == 0)
171 if (sval_cmp(left_min
, right_max
) > 0)
173 if (sval_cmp(left_min
, right_max
) == 0)
179 static struct range_list
*get_orig_rl(struct var_sym_list
*vsl
)
182 struct smatch_state
*state
;
186 sym
= vsl_to_sym(vsl
);
187 if (!sym
|| !sym
->ident
)
189 state
= get_orig_estate(sym
->ident
->name
, sym
);
190 return estate_rl(state
);
193 static struct smatch_state
*unmatched_comparison(struct sm_state
*sm
)
195 struct compare_data
*data
= sm
->state
->data
;
196 struct range_list
*left_rl
, *right_rl
;
202 if (strstr(data
->var1
, " orig"))
203 left_rl
= get_orig_rl(data
->vsl1
);
204 else if (!get_implied_rl_var_sym(data
->var1
, vsl_to_sym(data
->vsl1
), &left_rl
))
206 if (strstr(data
->var2
, " orig"))
207 right_rl
= get_orig_rl(data
->vsl2
);
208 else if (!get_implied_rl_var_sym(data
->var2
, vsl_to_sym(data
->vsl2
), &right_rl
))
212 op
= rl_comparison(left_rl
, right_rl
);
214 return alloc_compare_state(data
->var1
, data
->vsl1
, op
, data
->var2
, data
->vsl2
);
219 /* remove_unsigned_from_comparison() is obviously a hack. */
220 static int remove_unsigned_from_comparison(int op
)
223 case SPECIAL_UNSIGNED_LT
:
225 case SPECIAL_UNSIGNED_LTE
:
227 case SPECIAL_UNSIGNED_GTE
:
229 case SPECIAL_UNSIGNED_GT
:
237 * This is for when you merge states "a < b" and "a == b", the result is that
238 * we can say for sure, "a <= b" after the merge.
240 static int merge_comparisons(int one
, int two
)
244 one
= remove_unsigned_from_comparison(one
);
245 two
= remove_unsigned_from_comparison(two
);
295 return SPECIAL_NOTEQUAL
;
306 * This is for if you have "a < b" and "b <= c". Or in other words,
307 * "a < b <= c". You would call this like get_combined_comparison('<', '<=').
308 * The return comparison would be '<'.
310 * This function is different from merge_comparisons(), for example:
311 * merge_comparison('<', '==') returns '<='
312 * get_combined_comparison('<', '==') returns '<'
314 static int combine_comparisons(int left_compare
, int right_compare
)
318 left_compare
= remove_unsigned_from_comparison(left_compare
);
319 right_compare
= remove_unsigned_from_comparison(right_compare
);
323 switch (left_compare
) {
332 return right_compare
;
341 switch (right_compare
) {
373 static int filter_comparison(int orig
, int op
)
385 case SPECIAL_NOTEQUAL
:
395 case SPECIAL_NOTEQUAL
:
404 case SPECIAL_UNSIGNED_LTE
:
405 case SPECIAL_UNSIGNED_GTE
:
406 return SPECIAL_EQUAL
;
409 case SPECIAL_NOTEQUAL
:
414 case SPECIAL_UNSIGNED_LT
:
415 case SPECIAL_UNSIGNED_LTE
:
416 return SPECIAL_UNSIGNED_LT
;
417 case SPECIAL_NOTEQUAL
:
422 case SPECIAL_UNSIGNED_GT
:
423 case SPECIAL_UNSIGNED_GTE
:
424 return SPECIAL_UNSIGNED_GT
;
430 return SPECIAL_EQUAL
;
435 case SPECIAL_NOTEQUAL
:
443 case SPECIAL_NOTEQUAL
:
447 case SPECIAL_UNSIGNED_LT
:
449 case SPECIAL_UNSIGNED_LT
:
450 case SPECIAL_UNSIGNED_LTE
:
451 case SPECIAL_NOTEQUAL
:
452 return SPECIAL_UNSIGNED_LT
;
455 case SPECIAL_UNSIGNED_LTE
:
457 case SPECIAL_UNSIGNED_LT
:
458 case SPECIAL_UNSIGNED_LTE
:
461 case SPECIAL_NOTEQUAL
:
462 return SPECIAL_UNSIGNED_LT
;
463 case SPECIAL_UNSIGNED_GTE
:
464 return SPECIAL_EQUAL
;
467 case SPECIAL_UNSIGNED_GTE
:
469 case SPECIAL_UNSIGNED_LTE
:
470 return SPECIAL_EQUAL
;
471 case SPECIAL_NOTEQUAL
:
472 return SPECIAL_UNSIGNED_GT
;
474 case SPECIAL_UNSIGNED_GTE
:
475 case SPECIAL_UNSIGNED_GT
:
479 case SPECIAL_UNSIGNED_GT
:
481 case SPECIAL_UNSIGNED_GT
:
482 case SPECIAL_UNSIGNED_GTE
:
483 case SPECIAL_NOTEQUAL
:
484 return SPECIAL_UNSIGNED_GT
;
491 static struct smatch_state
*merge_compare_states(struct smatch_state
*s1
, struct smatch_state
*s2
)
493 struct compare_data
*data
= s1
->data
;
496 op
= merge_comparisons(state_to_comparison(s1
), state_to_comparison(s2
));
498 return alloc_compare_state(data
->var1
, data
->vsl1
, op
, data
->var2
, data
->vsl2
);
502 static struct smatch_state
*alloc_link_state(struct string_list
*links
)
504 struct smatch_state
*state
;
505 static char buf
[256];
509 state
= __alloc_smatch_state(0);
512 FOR_EACH_PTR(links
, tmp
) {
514 snprintf(buf
, sizeof(buf
), "%s", tmp
);
516 append(buf
, ", ", sizeof(buf
));
517 append(buf
, tmp
, sizeof(buf
));
519 } END_FOR_EACH_PTR(tmp
);
521 state
->name
= alloc_sname(buf
);
526 static void save_start_states(struct statement
*stmt
)
528 struct symbol
*param
;
530 char state_name
[128];
531 struct smatch_state
*state
;
532 struct string_list
*links
;
535 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, param
) {
536 struct var_sym_list
*vsl1
= NULL
;
537 struct var_sym_list
*vsl2
= NULL
;
539 if (!param
->ident
|| !param
->ident
->name
)
541 snprintf(orig
, sizeof(orig
), "%s orig", param
->ident
->name
);
542 snprintf(state_name
, sizeof(state_name
), "%s vs %s", param
->ident
->name
, orig
);
543 add_var_sym(&vsl1
, param
->ident
->name
, param
);
544 add_var_sym(&vsl2
, orig
, param
);
545 state
= alloc_compare_state(param
->ident
->name
, vsl1
, SPECIAL_EQUAL
, alloc_sname(orig
), vsl2
);
546 set_state(compare_id
, state_name
, NULL
, state
);
548 link
= alloc_sname(state_name
);
550 insert_string(&links
, link
);
551 state
= alloc_link_state(links
);
552 set_state(link_id
, param
->ident
->name
, param
, state
);
553 } END_FOR_EACH_PTR(param
);
556 static struct smatch_state
*merge_links(struct smatch_state
*s1
, struct smatch_state
*s2
)
558 struct smatch_state
*ret
;
559 struct string_list
*links
;
561 links
= combine_string_lists(s1
->data
, s2
->data
);
562 ret
= alloc_link_state(links
);
566 static void save_link_var_sym(const char *var
, struct symbol
*sym
, const char *link
)
568 struct smatch_state
*old_state
, *new_state
;
569 struct string_list
*links
;
572 old_state
= get_state(link_id
, var
, sym
);
574 links
= clone_str_list(old_state
->data
);
578 new = alloc_sname(link
);
579 insert_string(&links
, new);
581 new_state
= alloc_link_state(links
);
582 set_state(link_id
, var
, sym
, new_state
);
585 static void match_inc(struct sm_state
*sm
)
587 struct string_list
*links
;
588 struct smatch_state
*state
;
591 links
= sm
->state
->data
;
593 FOR_EACH_PTR(links
, tmp
) {
594 state
= get_state(compare_id
, tmp
, NULL
);
596 switch (state_to_comparison(state
)) {
599 case SPECIAL_UNSIGNED_GTE
:
601 case SPECIAL_UNSIGNED_GT
: {
602 struct compare_data
*data
= state
->data
;
603 struct smatch_state
*new;
605 new = alloc_compare_state(data
->var1
, data
->vsl1
, '>', data
->var2
, data
->vsl2
);
606 set_state(compare_id
, tmp
, NULL
, new);
610 set_state(compare_id
, tmp
, NULL
, &undefined
);
612 } END_FOR_EACH_PTR(tmp
);
615 static void match_dec(struct sm_state
*sm
)
617 struct string_list
*links
;
618 struct smatch_state
*state
;
621 links
= sm
->state
->data
;
623 FOR_EACH_PTR(links
, tmp
) {
624 state
= get_state(compare_id
, tmp
, NULL
);
626 switch (state_to_comparison(state
)) {
629 case SPECIAL_UNSIGNED_LTE
:
631 case SPECIAL_UNSIGNED_LT
: {
632 struct compare_data
*data
= state
->data
;
633 struct smatch_state
*new;
635 new = alloc_compare_state(data
->var1
, data
->vsl1
, '<', data
->var2
, data
->vsl2
);
636 set_state(compare_id
, tmp
, NULL
, new);
640 set_state(compare_id
, tmp
, NULL
, &undefined
);
642 } END_FOR_EACH_PTR(tmp
);
645 static int match_inc_dec(struct sm_state
*sm
, struct expression
*mod_expr
)
649 if (mod_expr
->type
!= EXPR_PREOP
&& mod_expr
->type
!= EXPR_POSTOP
)
652 if (mod_expr
->op
== SPECIAL_INCREMENT
) {
656 if (mod_expr
->op
== SPECIAL_DECREMENT
) {
663 static void match_modify(struct sm_state
*sm
, struct expression
*mod_expr
)
665 struct string_list
*links
;
669 * if (foo > bar) then ++foo is also > bar.
671 if (match_inc_dec(sm
, mod_expr
))
674 links
= sm
->state
->data
;
676 FOR_EACH_PTR(links
, tmp
) {
677 set_state(compare_id
, tmp
, NULL
, &undefined
);
678 } END_FOR_EACH_PTR(tmp
);
679 set_state(link_id
, sm
->name
, sm
->sym
, &undefined
);
682 static char *chunk_to_var_sym(struct expression
*expr
, struct symbol
**sym
)
684 char *name
, *left_name
, *right_name
;
688 expr
= strip_expr(expr
);
694 if (expr
->type
== EXPR_PREOP
&&
695 (expr
->op
== SPECIAL_INCREMENT
||
696 expr
->op
== SPECIAL_DECREMENT
))
697 expr
= strip_expr(expr
->unop
);
699 name
= expr_to_var_sym(expr
, &tmp
);
708 if (expr
->type
!= EXPR_BINOP
)
710 if (expr
->op
!= '-' && expr
->op
!= '+')
713 left_name
= expr_to_var(expr
->left
);
716 right_name
= expr_to_var(expr
->right
);
718 free_string(left_name
);
721 snprintf(buf
, sizeof(buf
), "%s %s %s", left_name
, show_special(expr
->op
), right_name
);
722 free_string(left_name
);
723 free_string(right_name
);
724 return alloc_string(buf
);
727 static char *chunk_to_var(struct expression
*expr
)
729 return chunk_to_var_sym(expr
, NULL
);
732 static void save_link(struct expression
*expr
, char *link
)
737 expr
= strip_expr(expr
);
738 if (expr
->type
== EXPR_BINOP
) {
741 chunk
= chunk_to_var(expr
);
745 save_link(expr
->left
, link
);
746 save_link(expr
->right
, link
);
747 save_link_var_sym(chunk
, NULL
, link
);
751 var
= expr_to_var_sym(expr
, &sym
);
755 save_link_var_sym(var
, sym
, link
);
760 static int get_orig_comparison(struct stree
*pre_stree
, const char *left
, const char *right
)
762 struct smatch_state
*state
;
763 struct compare_data
*data
;
765 char state_name
[256];
767 if (strcmp(left
, right
) > 0) {
768 const char *tmp
= right
;
775 snprintf(state_name
, sizeof(state_name
), "%s vs %s", left
, right
);
776 state
= get_state_stree(pre_stree
, compare_id
, state_name
, NULL
);
777 if (!state
|| !state
->data
)
781 return flip_comparison(data
->comparison
);
782 return data
->comparison
;
787 * The idea here is that we take a comparison "a < b" and then we look at all
788 * the things which "b" is compared against "b < c" and we say that that implies
789 * a relationship "a < c".
791 * The names here about because the comparisons are organized like this
795 static void update_tf_links(struct stree
*pre_stree
,
796 const char *left_var
, struct var_sym_list
*left_vsl
,
797 int left_comparison
, int left_false_comparison
,
798 const char *mid_var
, struct var_sym_list
*mid_vsl
,
799 struct string_list
*links
)
801 struct smatch_state
*state
;
802 struct smatch_state
*true_state
, *false_state
;
803 struct compare_data
*data
;
804 const char *right_var
;
805 struct var_sym_list
*right_vsl
;
807 int right_comparison
;
809 int false_comparison
;
811 char state_name
[256];
814 FOR_EACH_PTR(links
, tmp
) {
815 state
= get_state_stree(pre_stree
, compare_id
, tmp
, NULL
);
816 if (!state
|| !state
->data
)
819 right_comparison
= data
->comparison
;
820 right_var
= data
->var2
;
821 right_vsl
= data
->vsl2
;
822 if (strcmp(mid_var
, right_var
) == 0) {
823 right_var
= data
->var1
;
824 right_vsl
= data
->vsl1
;
825 right_comparison
= flip_comparison(right_comparison
);
827 if (strcmp(left_var
, right_var
) == 0)
830 orig_comparison
= get_orig_comparison(pre_stree
, left_var
, right_var
);
832 true_comparison
= combine_comparisons(left_comparison
, right_comparison
);
833 false_comparison
= combine_comparisons(left_false_comparison
, right_comparison
);
835 true_comparison
= filter_comparison(orig_comparison
, true_comparison
);
836 false_comparison
= filter_comparison(orig_comparison
, false_comparison
);
838 if (strcmp(left_var
, right_var
) > 0) {
839 const char *tmp_var
= left_var
;
840 struct var_sym_list
*tmp_vsl
= left_vsl
;
842 left_var
= right_var
;
843 left_vsl
= right_vsl
;
846 true_comparison
= flip_comparison(true_comparison
);
847 false_comparison
= flip_comparison(false_comparison
);
850 if (!true_comparison
&& !false_comparison
)
854 true_state
= alloc_compare_state(left_var
, left_vsl
, true_comparison
, right_var
, right_vsl
);
857 if (false_comparison
)
858 false_state
= alloc_compare_state(left_var
, left_vsl
, false_comparison
, right_var
, right_vsl
);
862 snprintf(state_name
, sizeof(state_name
), "%s vs %s", left_var
, right_var
);
863 set_true_false_states(compare_id
, state_name
, NULL
, true_state
, false_state
);
864 FOR_EACH_PTR(left_vsl
, vs
) {
865 save_link_var_sym(vs
->var
, vs
->sym
, state_name
);
866 } END_FOR_EACH_PTR(vs
);
867 FOR_EACH_PTR(right_vsl
, vs
) {
868 save_link_var_sym(vs
->var
, vs
->sym
, state_name
);
869 } END_FOR_EACH_PTR(vs
);
870 if (!vsl_to_sym(left_vsl
))
871 save_link_var_sym(left_var
, NULL
, state_name
);
872 if (!vsl_to_sym(right_vsl
))
873 save_link_var_sym(right_var
, NULL
, state_name
);
874 } END_FOR_EACH_PTR(tmp
);
877 static void update_tf_data(struct stree
*pre_stree
,
878 const char *left_name
, struct var_sym_list
*left_vsl
,
879 const char *right_name
, struct var_sym_list
*right_vsl
,
880 int true_comparison
, int false_comparison
)
882 struct smatch_state
*state
;
884 state
= get_state_stree(pre_stree
, link_id
, right_name
, vsl_to_sym(right_vsl
));
886 update_tf_links(pre_stree
, left_name
, left_vsl
, true_comparison
, false_comparison
, right_name
, right_vsl
, state
->data
);
888 state
= get_state_stree(pre_stree
, link_id
, left_name
, vsl_to_sym(left_vsl
));
890 update_tf_links(pre_stree
, right_name
, right_vsl
, flip_comparison(true_comparison
), flip_comparison(false_comparison
), left_name
, left_vsl
, state
->data
);
893 static void iter_modify(struct sm_state
*sm
, struct expression
*mod_expr
)
895 if (sm
->state
!= &start
||
897 (mod_expr
->type
!= EXPR_PREOP
&& mod_expr
->type
!= EXPR_POSTOP
) ||
898 mod_expr
->op
!= SPECIAL_INCREMENT
)
899 set_state(inc_dec_id
, sm
->name
, sm
->sym
, &undefined
);
901 set_state(inc_dec_id
, sm
->name
, sm
->sym
, &incremented
);
904 static void handle_for_loops(struct expression
*expr
, char *state_name
, struct smatch_state
*false_state
)
907 char *iter_name
, *cap_name
;
908 struct symbol
*iter_sym
, *cap_sym
;
909 struct compare_data
*data
;
911 if (expr
->op
!= '<' && expr
->op
!= SPECIAL_UNSIGNED_LT
)
914 if (!__cur_stmt
|| !__prev_stmt
)
916 if (__cur_stmt
->type
!= STMT_ITERATOR
)
918 if (__cur_stmt
->iterator_pre_condition
!= expr
)
921 /* literals are handled in smatch_extra.c */
922 if (get_value(expr
->right
, &sval
))
925 /* First time checking the condition */
926 if (__prev_stmt
== __cur_stmt
->iterator_pre_statement
) {
927 if (!get_implied_value(expr
->left
, &sval
) ||
931 iter_name
= expr_to_var_sym(expr
->left
, &iter_sym
);
932 cap_name
= expr_to_var_sym(expr
->right
, &cap_sym
);
933 if (!iter_name
|| !cap_name
|| !iter_sym
|| !cap_sym
) {
934 free_string(iter_name
);
935 free_string(cap_name
);
939 set_state(inc_dec_id
, iter_name
, iter_sym
, &start
);
940 store_link(inc_dec_link_id
, cap_name
, cap_sym
, iter_name
, iter_sym
);
942 free_string(iter_name
);
943 free_string(cap_name
);
947 /* Second time checking the condtion */
948 if (__prev_stmt
!= __cur_stmt
->iterator_post_statement
)
951 if (get_state_expr(inc_dec_id
, expr
->left
) != &incremented
)
954 data
= false_state
->data
;
955 false_state
= alloc_compare_state(data
->var1
, data
->vsl1
, SPECIAL_EQUAL
, data
->var2
, data
->vsl2
);
957 set_true_false_states(compare_id
, state_name
, NULL
, NULL
, false_state
);
960 void __comparison_match_condition(struct expression
*expr
)
964 struct symbol
*left_sym
, *right_sym
;
965 struct var_sym_list
*left_vsl
, *right_vsl
;
968 struct smatch_state
*true_state
, *false_state
;
969 char state_name
[256];
970 struct stree
*pre_stree
;
973 if (expr
->type
!= EXPR_COMPARE
)
977 false_op
= negate_comparison(op
);
979 if (op
== SPECIAL_UNSIGNED_LT
&&
980 get_implied_value(expr
->left
, &sval
) &&
982 false_op
= SPECIAL_EQUAL
;
984 if (op
== SPECIAL_UNSIGNED_GT
&&
985 get_implied_value(expr
->right
, &sval
) &&
987 false_op
= SPECIAL_EQUAL
;
989 left
= chunk_to_var_sym(expr
->left
, &left_sym
);
992 left_vsl
= expr_to_vsl(expr
->left
);
993 right
= chunk_to_var_sym(expr
->right
, &right_sym
);
996 right_vsl
= expr_to_vsl(expr
->right
);
998 if (strcmp(left
, right
) > 0) {
999 struct symbol
*tmp_sym
= left_sym
;
1000 char *tmp_name
= left
;
1001 struct var_sym_list
*tmp_vsl
= left_vsl
;
1004 left_sym
= right_sym
;
1005 left_vsl
= right_vsl
;
1007 right_sym
= tmp_sym
;
1008 right_vsl
= tmp_vsl
;
1009 op
= flip_comparison(op
);
1010 false_op
= flip_comparison(false_op
);
1013 orig_comparison
= get_comparison_strings(left
, right
);
1014 op
= filter_comparison(orig_comparison
, op
);
1015 false_op
= filter_comparison(orig_comparison
, false_op
);
1017 snprintf(state_name
, sizeof(state_name
), "%s vs %s", left
, right
);
1018 true_state
= alloc_compare_state(left
, left_vsl
, op
, right
, right_vsl
);
1019 false_state
= alloc_compare_state(left
, left_vsl
, false_op
, right
, right_vsl
);
1021 pre_stree
= clone_stree(__get_cur_stree());
1022 update_tf_data(pre_stree
, left
, left_vsl
, right
, right_vsl
, op
, false_op
);
1023 free_stree(&pre_stree
);
1025 set_true_false_states(compare_id
, state_name
, NULL
, true_state
, false_state
);
1026 save_link(expr
->left
, state_name
);
1027 save_link(expr
->right
, state_name
);
1029 handle_for_loops(expr
, state_name
, false_state
);
1035 static void add_comparison_var_sym(const char *left_name
,
1036 struct var_sym_list
*left_vsl
,
1038 const char *right_name
, struct var_sym_list
*right_vsl
)
1040 struct smatch_state
*state
;
1042 char state_name
[256];
1044 if (strcmp(left_name
, right_name
) > 0) {
1045 const char *tmp_name
= left_name
;
1046 struct var_sym_list
*tmp_vsl
= left_vsl
;
1048 left_name
= right_name
;
1049 left_vsl
= right_vsl
;
1050 right_name
= tmp_name
;
1051 right_vsl
= tmp_vsl
;
1052 comparison
= flip_comparison(comparison
);
1054 snprintf(state_name
, sizeof(state_name
), "%s vs %s", left_name
, right_name
);
1055 state
= alloc_compare_state(left_name
, left_vsl
, comparison
, right_name
, right_vsl
);
1057 set_state(compare_id
, state_name
, NULL
, state
);
1059 FOR_EACH_PTR(left_vsl
, vs
) {
1060 save_link_var_sym(vs
->var
, vs
->sym
, state_name
);
1061 } END_FOR_EACH_PTR(vs
);
1062 FOR_EACH_PTR(right_vsl
, vs
) {
1063 save_link_var_sym(vs
->var
, vs
->sym
, state_name
);
1064 } END_FOR_EACH_PTR(vs
);
1067 static void add_comparison(struct expression
*left
, int comparison
, struct expression
*right
)
1069 char *left_name
= NULL
;
1070 char *right_name
= NULL
;
1071 struct symbol
*left_sym
, *right_sym
;
1072 struct var_sym_list
*left_vsl
, *right_vsl
;
1073 struct smatch_state
*state
;
1074 char state_name
[256];
1076 left_name
= chunk_to_var_sym(left
, &left_sym
);
1079 left_vsl
= expr_to_vsl(left
);
1080 right_name
= chunk_to_var_sym(right
, &right_sym
);
1083 right_vsl
= expr_to_vsl(right
);
1085 if (strcmp(left_name
, right_name
) > 0) {
1086 struct symbol
*tmp_sym
= left_sym
;
1087 char *tmp_name
= left_name
;
1088 struct var_sym_list
*tmp_vsl
= left_vsl
;
1090 left_name
= right_name
;
1091 left_sym
= right_sym
;
1092 left_vsl
= right_vsl
;
1093 right_name
= tmp_name
;
1094 right_sym
= tmp_sym
;
1095 right_vsl
= tmp_vsl
;
1096 comparison
= flip_comparison(comparison
);
1098 snprintf(state_name
, sizeof(state_name
), "%s vs %s", left_name
, right_name
);
1099 state
= alloc_compare_state(left_name
, left_vsl
, comparison
, right_name
, right_vsl
);
1101 set_state(compare_id
, state_name
, NULL
, state
);
1102 save_link(left
, state_name
);
1103 save_link(right
, state_name
);
1106 free_string(left_name
);
1107 free_string(right_name
);
1110 static void match_assign_add(struct expression
*expr
)
1112 struct expression
*right
;
1113 struct expression
*r_left
, *r_right
;
1114 sval_t left_tmp
, right_tmp
;
1116 right
= strip_expr(expr
->right
);
1117 r_left
= strip_expr(right
->left
);
1118 r_right
= strip_expr(right
->right
);
1120 get_absolute_min(r_left
, &left_tmp
);
1121 get_absolute_min(r_right
, &right_tmp
);
1123 if (left_tmp
.value
> 0)
1124 add_comparison(expr
->left
, '>', r_right
);
1125 else if (left_tmp
.value
== 0)
1126 add_comparison(expr
->left
, SPECIAL_GTE
, r_right
);
1128 if (right_tmp
.value
> 0)
1129 add_comparison(expr
->left
, '>', r_left
);
1130 else if (right_tmp
.value
== 0)
1131 add_comparison(expr
->left
, SPECIAL_GTE
, r_left
);
1134 static void match_assign_sub(struct expression
*expr
)
1136 struct expression
*right
;
1137 struct expression
*r_left
, *r_right
;
1141 right
= strip_expr(expr
->right
);
1142 r_left
= strip_expr(right
->left
);
1143 r_right
= strip_expr(right
->right
);
1145 if (get_absolute_min(r_right
, &min
) && sval_is_negative(min
))
1148 comparison
= get_comparison(r_left
, r_right
);
1150 switch (comparison
) {
1153 if (implied_not_equal(r_right
, 0))
1154 add_comparison(expr
->left
, '>', r_left
);
1156 add_comparison(expr
->left
, SPECIAL_GTE
, r_left
);
1161 static void match_assign_divide(struct expression
*expr
)
1163 struct expression
*right
;
1164 struct expression
*r_left
, *r_right
;
1167 right
= strip_expr(expr
->right
);
1168 r_left
= strip_expr(right
->left
);
1169 r_right
= strip_expr(right
->right
);
1170 if (!get_implied_min(r_right
, &min
) || min
.value
<= 1)
1173 add_comparison(expr
->left
, '<', r_left
);
1176 static void match_binop_assign(struct expression
*expr
)
1178 struct expression
*right
;
1180 right
= strip_expr(expr
->right
);
1181 if (right
->op
== '+')
1182 match_assign_add(expr
);
1183 if (right
->op
== '-')
1184 match_assign_sub(expr
);
1185 if (right
->op
== '/')
1186 match_assign_divide(expr
);
1189 static void copy_comparisons(struct expression
*left
, struct expression
*right
)
1191 struct string_list
*links
;
1192 struct smatch_state
*state
;
1193 struct compare_data
*data
;
1194 struct symbol
*left_sym
, *right_sym
;
1195 char *left_var
= NULL
;
1196 char *right_var
= NULL
;
1197 struct var_sym_list
*left_vsl
;
1199 struct var_sym_list
*vsl
;
1203 left_var
= chunk_to_var_sym(left
, &left_sym
);
1206 left_vsl
= expr_to_vsl(left
);
1207 right_var
= chunk_to_var_sym(right
, &right_sym
);
1211 state
= get_state(link_id
, right_var
, right_sym
);
1214 links
= state
->data
;
1216 FOR_EACH_PTR(links
, tmp
) {
1217 state
= get_state(compare_id
, tmp
, NULL
);
1218 if (!state
|| !state
->data
)
1221 comparison
= data
->comparison
;
1224 if (strcmp(var
, right_var
) == 0) {
1227 comparison
= flip_comparison(comparison
);
1229 add_comparison_var_sym(left_var
, left_vsl
, comparison
, var
, vsl
);
1230 } END_FOR_EACH_PTR(tmp
);
1233 free_string(right_var
);
1236 static void match_assign(struct expression
*expr
)
1238 struct expression
*right
;
1240 if (expr
->op
!= '=')
1242 if (__in_fake_assign
|| outside_of_function())
1245 if (is_struct(expr
->left
))
1248 copy_comparisons(expr
->left
, expr
->right
);
1249 add_comparison(expr
->left
, SPECIAL_EQUAL
, expr
->right
);
1251 right
= strip_expr(expr
->right
);
1252 if (right
->type
== EXPR_BINOP
)
1253 match_binop_assign(expr
);
1256 int get_comparison_strings(const char *one
, const char *two
)
1259 struct smatch_state
*state
;
1263 if (strcmp(one
, two
) == 0)
1264 return SPECIAL_EQUAL
;
1266 if (strcmp(one
, two
) > 0) {
1267 const char *tmp
= one
;
1274 snprintf(buf
, sizeof(buf
), "%s vs %s", one
, two
);
1275 state
= get_state(compare_id
, buf
, NULL
);
1277 ret
= state_to_comparison(state
);
1280 ret
= flip_comparison(ret
);
1285 int get_comparison(struct expression
*a
, struct expression
*b
)
1291 one
= chunk_to_var(a
);
1294 two
= chunk_to_var(b
);
1298 ret
= get_comparison_strings(one
, two
);
1305 int possible_comparison(struct expression
*a
, int comparison
, struct expression
*b
)
1311 struct sm_state
*sm
;
1314 one
= chunk_to_var(a
);
1317 two
= chunk_to_var(b
);
1322 if (strcmp(one
, two
) == 0 && comparison
== SPECIAL_EQUAL
) {
1327 if (strcmp(one
, two
) > 0) {
1332 comparison
= flip_comparison(comparison
);
1335 snprintf(buf
, sizeof(buf
), "%s vs %s", one
, two
);
1336 sm
= get_sm_state(compare_id
, buf
, NULL
);
1340 FOR_EACH_PTR(sm
->possible
, sm
) {
1341 if (!sm
->state
->data
)
1343 saved
= ((struct compare_data
*)sm
->state
->data
)->comparison
;
1344 if (saved
== comparison
)
1346 if (comparison
== SPECIAL_EQUAL
&&
1347 (saved
== SPECIAL_LTE
||
1348 saved
== SPECIAL_GTE
||
1349 saved
== SPECIAL_UNSIGNED_LTE
||
1350 saved
== SPECIAL_UNSIGNED_GTE
))
1354 } END_FOR_EACH_PTR(sm
);
1363 struct state_list
*get_all_comparisons(struct expression
*expr
)
1365 struct smatch_state
*state
;
1366 struct string_list
*links
;
1367 struct state_list
*ret
= NULL
;
1368 struct sm_state
*sm
;
1371 state
= get_state_expr(link_id
, expr
);
1374 links
= state
->data
;
1376 FOR_EACH_PTR(links
, tmp
) {
1377 sm
= get_sm_state(compare_id
, tmp
, NULL
);
1380 // FIXME have to compare name with vsl
1381 add_ptr_list(&ret
, sm
);
1382 } END_FOR_EACH_PTR(tmp
);
1387 struct state_list
*get_all_possible_equal_comparisons(struct expression
*expr
)
1389 struct smatch_state
*state
;
1390 struct string_list
*links
;
1391 struct state_list
*ret
= NULL
;
1392 struct sm_state
*sm
;
1395 state
= get_state_expr(link_id
, expr
);
1398 links
= state
->data
;
1400 FOR_EACH_PTR(links
, tmp
) {
1401 sm
= get_sm_state(compare_id
, tmp
, NULL
);
1404 if (!strchr(sm
->state
->name
, '='))
1406 if (strcmp(sm
->state
->name
, "!=") == 0)
1408 add_ptr_list(&ret
, sm
);
1409 } END_FOR_EACH_PTR(tmp
);
1414 static void update_links_from_call(struct expression
*left
,
1416 struct expression
*right
)
1418 struct string_list
*links
;
1419 struct smatch_state
*state
;
1420 struct compare_data
*data
;
1421 struct symbol
*left_sym
, *right_sym
;
1422 char *left_var
= NULL
;
1423 char *right_var
= NULL
;
1424 struct var_sym_list
*left_vsl
;
1426 struct var_sym_list
*vsl
;
1430 left_var
= chunk_to_var_sym(left
, &left_sym
);
1433 left_vsl
= expr_to_vsl(left
);
1434 right_var
= chunk_to_var_sym(right
, &right_sym
);
1438 state
= get_state(link_id
, right_var
, right_sym
);
1441 links
= state
->data
;
1443 FOR_EACH_PTR(links
, tmp
) {
1444 state
= get_state(compare_id
, tmp
, NULL
);
1445 if (!state
|| !state
->data
)
1448 comparison
= data
->comparison
;
1451 if (strcmp(var
, right_var
) == 0) {
1454 comparison
= flip_comparison(comparison
);
1456 comparison
= combine_comparisons(left_compare
, comparison
);
1459 add_comparison_var_sym(left_var
, left_vsl
, comparison
, var
, vsl
);
1460 } END_FOR_EACH_PTR(tmp
);
1463 free_string(right_var
);
1466 void __add_comparison_info(struct expression
*expr
, struct expression
*call
, const char *range
)
1468 struct expression
*arg
;
1470 const char *c
= range
;
1472 if (!str_to_comparison_arg(c
, call
, &comparison
, &arg
))
1474 update_links_from_call(expr
, comparison
, arg
);
1475 add_comparison(expr
, comparison
, arg
);
1478 static char *range_comparison_to_param_helper(struct expression
*expr
, char starts_with
, int ignore
)
1480 struct symbol
*param
;
1483 char *ret_str
= NULL
;
1487 var
= chunk_to_var(expr
);
1492 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, param
) {
1498 snprintf(buf
, sizeof(buf
), "%s orig", param
->ident
->name
);
1499 compare
= get_comparison_strings(var
, buf
);
1502 if (show_special(compare
)[0] != starts_with
)
1504 snprintf(buf
, sizeof(buf
), "[%s$%d]", show_special(compare
), i
);
1505 ret_str
= alloc_sname(buf
);
1507 } END_FOR_EACH_PTR(param
);
1514 char *expr_equal_to_param(struct expression
*expr
, int ignore
)
1516 return range_comparison_to_param_helper(expr
, '=', ignore
);
1519 char *expr_lte_to_param(struct expression
*expr
, int ignore
)
1521 return range_comparison_to_param_helper(expr
, '<', ignore
);
1524 char *expr_param_comparison(struct expression
*expr
, int ignore
)
1526 struct symbol
*param
;
1529 char *ret_str
= NULL
;
1533 var
= chunk_to_var(expr
);
1538 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, param
) {
1544 snprintf(buf
, sizeof(buf
), "%s orig", param
->ident
->name
);
1545 compare
= get_comparison_strings(var
, buf
);
1548 snprintf(buf
, sizeof(buf
), "[%s$%d]", show_special(compare
), i
);
1549 ret_str
= alloc_sname(buf
);
1551 } END_FOR_EACH_PTR(param
);
1558 static void free_data(struct symbol
*sym
)
1562 clear_compare_data_alloc();
1565 void register_comparison(int id
)
1568 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
1569 add_hook(&save_start_states
, AFTER_DEF_HOOK
);
1570 add_unmatched_state_hook(compare_id
, unmatched_comparison
);
1571 add_merge_hook(compare_id
, &merge_compare_states
);
1572 add_hook(&free_data
, AFTER_FUNC_HOOK
);
1575 void register_comparison_links(int id
)
1578 add_merge_hook(link_id
, &merge_links
);
1579 add_modification_hook(link_id
, &match_modify
);
1582 void register_comparison_inc_dec(int id
)
1585 add_modification_hook(inc_dec_id
, &iter_modify
);
1588 void register_comparison_inc_dec_links(int id
)
1590 inc_dec_link_id
= id
;
1591 set_up_link_functions(inc_dec_id
, inc_dec_link_id
);
1594 static void filter_by_sm(struct sm_state
*sm
, int op
,
1595 struct stree_stack
**true_stack
,
1596 struct stree_stack
**false_stack
)
1598 struct compare_data
*data
;
1604 data
= sm
->state
->data
;
1608 if (data
->comparison
&&
1609 data
->comparison
== filter_comparison(data
->comparison
, op
))
1612 if (data
->comparison
&&
1613 data
->comparison
== filter_comparison(data
->comparison
, negate_comparison(op
)))
1617 add_pool(true_stack
, sm
->pool
);
1619 add_pool(false_stack
, sm
->pool
);
1622 filter_by_sm(sm
->left
, op
, true_stack
, false_stack
);
1623 filter_by_sm(sm
->right
, op
, true_stack
, false_stack
);
1627 struct sm_state
*comparison_implication_hook(struct expression
*expr
,
1628 struct stree_stack
**true_stack
,
1629 struct stree_stack
**false_stack
)
1631 struct sm_state
*sm
;
1634 static char buf
[256];
1636 if (expr
->type
!= EXPR_COMPARE
)
1641 left
= expr_to_var(expr
->left
);
1642 right
= expr_to_var(expr
->right
);
1643 if (!left
|| !right
) {
1649 if (strcmp(left
, right
) > 0) {
1654 op
= flip_comparison(op
);
1657 snprintf(buf
, sizeof(buf
), "%s vs %s", left
, right
);
1658 sm
= get_sm_state(compare_id
, buf
, NULL
);
1664 filter_by_sm(sm
, op
, true_stack
, false_stack
);
1665 if (!*true_stack
&& !*false_stack
)