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
;
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 expr
= strip_expr(expr
);
690 if (expr
->type
== EXPR_PREOP
&&
691 (expr
->op
== SPECIAL_INCREMENT
||
692 expr
->op
== SPECIAL_DECREMENT
))
693 expr
= strip_expr(expr
->unop
);
695 return expr_to_chunk_sym_vsl(expr
, sym
, NULL
);
698 static char *chunk_to_var(struct expression
*expr
)
700 return chunk_to_var_sym(expr
, NULL
);
703 static void save_link(struct expression
*expr
, char *link
)
708 expr
= strip_expr(expr
);
709 if (expr
->type
== EXPR_BINOP
) {
712 chunk
= chunk_to_var(expr
);
716 save_link(expr
->left
, link
);
717 save_link(expr
->right
, link
);
718 save_link_var_sym(chunk
, NULL
, link
);
722 var
= expr_to_var_sym(expr
, &sym
);
726 save_link_var_sym(var
, sym
, link
);
731 static int get_orig_comparison(struct stree
*pre_stree
, const char *left
, const char *right
)
733 struct smatch_state
*state
;
734 struct compare_data
*data
;
736 char state_name
[256];
738 if (strcmp(left
, right
) > 0) {
739 const char *tmp
= right
;
746 snprintf(state_name
, sizeof(state_name
), "%s vs %s", left
, right
);
747 state
= get_state_stree(pre_stree
, compare_id
, state_name
, NULL
);
748 if (!state
|| !state
->data
)
752 return flip_comparison(data
->comparison
);
753 return data
->comparison
;
757 static int have_common_var_sym(struct var_sym_list
*left_vsl
, struct var_sym_list
*right_vsl
)
761 FOR_EACH_PTR(left_vsl
, tmp
) {
762 if (in_var_sym_list(right_vsl
, tmp
->var
, tmp
->sym
))
764 } END_FOR_EACH_PTR(tmp
);
770 * The idea here is that we take a comparison "a < b" and then we look at all
771 * the things which "b" is compared against "b < c" and we say that that implies
772 * a relationship "a < c".
774 * The names here about because the comparisons are organized like this
778 static void update_tf_links(struct stree
*pre_stree
,
779 const char *left_var
, struct var_sym_list
*left_vsl
,
780 int left_comparison
, int left_false_comparison
,
781 const char *mid_var
, struct var_sym_list
*mid_vsl
,
782 struct string_list
*links
)
784 struct smatch_state
*state
;
785 struct smatch_state
*true_state
, *false_state
;
786 struct compare_data
*data
;
787 const char *right_var
;
788 struct var_sym_list
*right_vsl
;
790 int right_comparison
;
792 int false_comparison
;
794 char state_name
[256];
797 FOR_EACH_PTR(links
, tmp
) {
798 state
= get_state_stree(pre_stree
, compare_id
, tmp
, NULL
);
799 if (!state
|| !state
->data
)
802 right_comparison
= data
->comparison
;
803 right_var
= data
->var2
;
804 right_vsl
= data
->vsl2
;
805 if (strcmp(mid_var
, right_var
) == 0) {
806 right_var
= data
->var1
;
807 right_vsl
= data
->vsl1
;
808 right_comparison
= flip_comparison(right_comparison
);
810 if (have_common_var_sym(left_vsl
, right_vsl
))
813 orig_comparison
= get_orig_comparison(pre_stree
, left_var
, right_var
);
815 true_comparison
= combine_comparisons(left_comparison
, right_comparison
);
816 false_comparison
= combine_comparisons(left_false_comparison
, right_comparison
);
818 true_comparison
= filter_comparison(orig_comparison
, true_comparison
);
819 false_comparison
= filter_comparison(orig_comparison
, false_comparison
);
821 if (strcmp(left_var
, right_var
) > 0) {
822 const char *tmp_var
= left_var
;
823 struct var_sym_list
*tmp_vsl
= left_vsl
;
825 left_var
= right_var
;
826 left_vsl
= right_vsl
;
829 true_comparison
= flip_comparison(true_comparison
);
830 false_comparison
= flip_comparison(false_comparison
);
833 if (!true_comparison
&& !false_comparison
)
837 true_state
= alloc_compare_state(left_var
, left_vsl
, true_comparison
, right_var
, right_vsl
);
840 if (false_comparison
)
841 false_state
= alloc_compare_state(left_var
, left_vsl
, false_comparison
, right_var
, right_vsl
);
845 snprintf(state_name
, sizeof(state_name
), "%s vs %s", left_var
, right_var
);
846 set_true_false_states(compare_id
, state_name
, NULL
, true_state
, false_state
);
847 FOR_EACH_PTR(left_vsl
, vs
) {
848 save_link_var_sym(vs
->var
, vs
->sym
, state_name
);
849 } END_FOR_EACH_PTR(vs
);
850 FOR_EACH_PTR(right_vsl
, vs
) {
851 save_link_var_sym(vs
->var
, vs
->sym
, state_name
);
852 } END_FOR_EACH_PTR(vs
);
853 if (!vsl_to_sym(left_vsl
))
854 save_link_var_sym(left_var
, NULL
, state_name
);
855 if (!vsl_to_sym(right_vsl
))
856 save_link_var_sym(right_var
, NULL
, state_name
);
857 } END_FOR_EACH_PTR(tmp
);
860 static void update_tf_data(struct stree
*pre_stree
,
861 const char *left_name
, struct var_sym_list
*left_vsl
,
862 const char *right_name
, struct var_sym_list
*right_vsl
,
863 int true_comparison
, int false_comparison
)
865 struct smatch_state
*state
;
867 state
= get_state_stree(pre_stree
, link_id
, right_name
, vsl_to_sym(right_vsl
));
869 update_tf_links(pre_stree
, left_name
, left_vsl
, true_comparison
, false_comparison
, right_name
, right_vsl
, state
->data
);
871 state
= get_state_stree(pre_stree
, link_id
, left_name
, vsl_to_sym(left_vsl
));
873 update_tf_links(pre_stree
, right_name
, right_vsl
, flip_comparison(true_comparison
), flip_comparison(false_comparison
), left_name
, left_vsl
, state
->data
);
876 static void iter_modify(struct sm_state
*sm
, struct expression
*mod_expr
)
878 if (sm
->state
!= &start
||
880 (mod_expr
->type
!= EXPR_PREOP
&& mod_expr
->type
!= EXPR_POSTOP
) ||
881 mod_expr
->op
!= SPECIAL_INCREMENT
)
882 set_state(inc_dec_id
, sm
->name
, sm
->sym
, &undefined
);
884 set_state(inc_dec_id
, sm
->name
, sm
->sym
, &incremented
);
887 static void handle_for_loops(struct expression
*expr
, char *state_name
, struct smatch_state
*false_state
)
890 char *iter_name
, *cap_name
;
891 struct symbol
*iter_sym
, *cap_sym
;
892 struct compare_data
*data
;
894 if (expr
->op
!= '<' && expr
->op
!= SPECIAL_UNSIGNED_LT
)
897 if (!__cur_stmt
|| !__prev_stmt
)
899 if (__cur_stmt
->type
!= STMT_ITERATOR
)
901 if (__cur_stmt
->iterator_pre_condition
!= expr
)
904 /* literals are handled in smatch_extra.c */
905 if (get_value(expr
->right
, &sval
))
908 /* First time checking the condition */
909 if (__prev_stmt
== __cur_stmt
->iterator_pre_statement
) {
910 if (!get_implied_value(expr
->left
, &sval
) ||
914 iter_name
= expr_to_var_sym(expr
->left
, &iter_sym
);
915 cap_name
= expr_to_var_sym(expr
->right
, &cap_sym
);
916 if (!iter_name
|| !cap_name
|| !iter_sym
|| !cap_sym
) {
917 free_string(iter_name
);
918 free_string(cap_name
);
922 set_state(inc_dec_id
, iter_name
, iter_sym
, &start
);
923 store_link(inc_dec_link_id
, cap_name
, cap_sym
, iter_name
, iter_sym
);
925 free_string(iter_name
);
926 free_string(cap_name
);
930 /* Second time checking the condtion */
931 if (__prev_stmt
!= __cur_stmt
->iterator_post_statement
)
934 if (get_state_expr(inc_dec_id
, expr
->left
) != &incremented
)
937 data
= false_state
->data
;
938 false_state
= alloc_compare_state(data
->var1
, data
->vsl1
, SPECIAL_EQUAL
, data
->var2
, data
->vsl2
);
940 set_true_false_states(compare_id
, state_name
, NULL
, NULL
, false_state
);
943 static void handle_comparison(struct expression
*expr_left
, int op
, struct expression
*expr_right
, char **_state_name
, struct smatch_state
**_false_state
)
947 struct symbol
*left_sym
, *right_sym
;
948 struct var_sym_list
*left_vsl
, *right_vsl
;
951 struct smatch_state
*true_state
, *false_state
;
952 static char state_name
[256];
953 struct stree
*pre_stree
;
956 false_op
= negate_comparison(op
);
958 if (op
== SPECIAL_UNSIGNED_LT
&&
959 get_implied_value(expr_left
, &sval
) &&
961 false_op
= SPECIAL_EQUAL
;
963 if (op
== SPECIAL_UNSIGNED_GT
&&
964 get_implied_value(expr_right
, &sval
) &&
966 false_op
= SPECIAL_EQUAL
;
968 left
= chunk_to_var_sym(expr_left
, &left_sym
);
971 left_vsl
= expr_to_vsl(expr_left
);
972 right
= chunk_to_var_sym(expr_right
, &right_sym
);
975 right_vsl
= expr_to_vsl(expr_right
);
977 if (strcmp(left
, right
) > 0) {
978 struct symbol
*tmp_sym
= left_sym
;
979 char *tmp_name
= left
;
980 struct var_sym_list
*tmp_vsl
= left_vsl
;
983 left_sym
= right_sym
;
984 left_vsl
= right_vsl
;
988 op
= flip_comparison(op
);
989 false_op
= flip_comparison(false_op
);
992 orig_comparison
= get_comparison_strings(left
, right
);
993 op
= filter_comparison(orig_comparison
, op
);
994 false_op
= filter_comparison(orig_comparison
, false_op
);
996 snprintf(state_name
, sizeof(state_name
), "%s vs %s", left
, right
);
997 true_state
= alloc_compare_state(left
, left_vsl
, op
, right
, right_vsl
);
998 false_state
= alloc_compare_state(left
, left_vsl
, false_op
, right
, right_vsl
);
1000 pre_stree
= clone_stree(__get_cur_stree());
1001 update_tf_data(pre_stree
, left
, left_vsl
, right
, right_vsl
, op
, false_op
);
1002 free_stree(&pre_stree
);
1004 set_true_false_states(compare_id
, state_name
, NULL
, true_state
, false_state
);
1005 save_link(expr_left
, state_name
);
1006 save_link(expr_right
, state_name
);
1009 *_false_state
= false_state
;
1011 *_state_name
= state_name
;
1018 void __comparison_match_condition(struct expression
*expr
)
1020 struct expression
*left
, *right
, *new_left
, *new_right
;
1021 struct smatch_state
*false_state
= NULL
;
1022 char *state_name
= NULL
;
1024 if (expr
->type
!= EXPR_COMPARE
)
1027 handle_comparison(expr
->left
, expr
->op
, expr
->right
, &state_name
, &false_state
);
1028 if (false_state
&& state_name
)
1029 handle_for_loops(expr
, state_name
, false_state
);
1032 left
= strip_parens(expr
->left
);
1033 right
= strip_parens(expr
->right
);
1035 if (left
->type
== EXPR_BINOP
&& left
->op
== '+') {
1036 new_left
= left
->left
;
1037 new_right
= binop_expression(right
, '-', left
->right
);
1038 handle_comparison(new_left
, expr
->op
, new_right
, NULL
, NULL
);
1040 new_left
= left
->right
;
1041 new_right
= binop_expression(right
, '-', left
->left
);
1042 handle_comparison(new_left
, expr
->op
, new_right
, NULL
, NULL
);
1046 static void add_comparison_var_sym(const char *left_name
,
1047 struct var_sym_list
*left_vsl
,
1049 const char *right_name
, struct var_sym_list
*right_vsl
)
1051 struct smatch_state
*state
;
1053 char state_name
[256];
1055 if (strcmp(left_name
, right_name
) > 0) {
1056 const char *tmp_name
= left_name
;
1057 struct var_sym_list
*tmp_vsl
= left_vsl
;
1059 left_name
= right_name
;
1060 left_vsl
= right_vsl
;
1061 right_name
= tmp_name
;
1062 right_vsl
= tmp_vsl
;
1063 comparison
= flip_comparison(comparison
);
1065 snprintf(state_name
, sizeof(state_name
), "%s vs %s", left_name
, right_name
);
1066 state
= alloc_compare_state(left_name
, left_vsl
, comparison
, right_name
, right_vsl
);
1068 set_state(compare_id
, state_name
, NULL
, state
);
1070 FOR_EACH_PTR(left_vsl
, vs
) {
1071 save_link_var_sym(vs
->var
, vs
->sym
, state_name
);
1072 } END_FOR_EACH_PTR(vs
);
1073 FOR_EACH_PTR(right_vsl
, vs
) {
1074 save_link_var_sym(vs
->var
, vs
->sym
, state_name
);
1075 } END_FOR_EACH_PTR(vs
);
1078 static void add_comparison(struct expression
*left
, int comparison
, struct expression
*right
)
1080 char *left_name
= NULL
;
1081 char *right_name
= NULL
;
1082 struct symbol
*left_sym
, *right_sym
;
1083 struct var_sym_list
*left_vsl
, *right_vsl
;
1084 struct smatch_state
*state
;
1085 char state_name
[256];
1087 left_name
= chunk_to_var_sym(left
, &left_sym
);
1090 left_vsl
= expr_to_vsl(left
);
1091 right_name
= chunk_to_var_sym(right
, &right_sym
);
1094 right_vsl
= expr_to_vsl(right
);
1096 if (strcmp(left_name
, right_name
) > 0) {
1097 struct symbol
*tmp_sym
= left_sym
;
1098 char *tmp_name
= left_name
;
1099 struct var_sym_list
*tmp_vsl
= left_vsl
;
1101 left_name
= right_name
;
1102 left_sym
= right_sym
;
1103 left_vsl
= right_vsl
;
1104 right_name
= tmp_name
;
1105 right_sym
= tmp_sym
;
1106 right_vsl
= tmp_vsl
;
1107 comparison
= flip_comparison(comparison
);
1109 snprintf(state_name
, sizeof(state_name
), "%s vs %s", left_name
, right_name
);
1110 state
= alloc_compare_state(left_name
, left_vsl
, comparison
, right_name
, right_vsl
);
1112 set_state(compare_id
, state_name
, NULL
, state
);
1113 save_link(left
, state_name
);
1114 save_link(right
, state_name
);
1117 free_string(left_name
);
1118 free_string(right_name
);
1121 static void match_assign_add(struct expression
*expr
)
1123 struct expression
*right
;
1124 struct expression
*r_left
, *r_right
;
1125 sval_t left_tmp
, right_tmp
;
1127 right
= strip_expr(expr
->right
);
1128 r_left
= strip_expr(right
->left
);
1129 r_right
= strip_expr(right
->right
);
1131 get_absolute_min(r_left
, &left_tmp
);
1132 get_absolute_min(r_right
, &right_tmp
);
1134 if (left_tmp
.value
> 0)
1135 add_comparison(expr
->left
, '>', r_right
);
1136 else if (left_tmp
.value
== 0)
1137 add_comparison(expr
->left
, SPECIAL_GTE
, r_right
);
1139 if (right_tmp
.value
> 0)
1140 add_comparison(expr
->left
, '>', r_left
);
1141 else if (right_tmp
.value
== 0)
1142 add_comparison(expr
->left
, SPECIAL_GTE
, r_left
);
1145 static void match_assign_sub(struct expression
*expr
)
1147 struct expression
*right
;
1148 struct expression
*r_left
, *r_right
;
1152 right
= strip_expr(expr
->right
);
1153 r_left
= strip_expr(right
->left
);
1154 r_right
= strip_expr(right
->right
);
1156 if (get_absolute_min(r_right
, &min
) && sval_is_negative(min
))
1159 comparison
= get_comparison(r_left
, r_right
);
1161 switch (comparison
) {
1164 if (implied_not_equal(r_right
, 0))
1165 add_comparison(expr
->left
, '>', r_left
);
1167 add_comparison(expr
->left
, SPECIAL_GTE
, r_left
);
1172 static void match_assign_divide(struct expression
*expr
)
1174 struct expression
*right
;
1175 struct expression
*r_left
, *r_right
;
1178 right
= strip_expr(expr
->right
);
1179 r_left
= strip_expr(right
->left
);
1180 r_right
= strip_expr(right
->right
);
1181 if (!get_implied_min(r_right
, &min
) || min
.value
<= 1)
1184 add_comparison(expr
->left
, '<', r_left
);
1187 static void match_binop_assign(struct expression
*expr
)
1189 struct expression
*right
;
1191 right
= strip_expr(expr
->right
);
1192 if (right
->op
== '+')
1193 match_assign_add(expr
);
1194 if (right
->op
== '-')
1195 match_assign_sub(expr
);
1196 if (right
->op
== '/')
1197 match_assign_divide(expr
);
1200 static void copy_comparisons(struct expression
*left
, struct expression
*right
)
1202 struct string_list
*links
;
1203 struct smatch_state
*state
;
1204 struct compare_data
*data
;
1205 struct symbol
*left_sym
, *right_sym
;
1206 char *left_var
= NULL
;
1207 char *right_var
= NULL
;
1208 struct var_sym_list
*left_vsl
;
1210 struct var_sym_list
*vsl
;
1214 left_var
= chunk_to_var_sym(left
, &left_sym
);
1217 left_vsl
= expr_to_vsl(left
);
1218 right_var
= chunk_to_var_sym(right
, &right_sym
);
1222 state
= get_state(link_id
, right_var
, right_sym
);
1225 links
= state
->data
;
1227 FOR_EACH_PTR(links
, tmp
) {
1228 state
= get_state(compare_id
, tmp
, NULL
);
1229 if (!state
|| !state
->data
)
1232 comparison
= data
->comparison
;
1235 if (strcmp(var
, right_var
) == 0) {
1238 comparison
= flip_comparison(comparison
);
1240 add_comparison_var_sym(left_var
, left_vsl
, comparison
, var
, vsl
);
1241 } END_FOR_EACH_PTR(tmp
);
1244 free_string(right_var
);
1247 static void match_assign(struct expression
*expr
)
1249 struct expression
*right
;
1251 if (expr
->op
!= '=')
1253 if (__in_fake_assign
|| outside_of_function())
1256 if (is_struct(expr
->left
))
1259 copy_comparisons(expr
->left
, expr
->right
);
1260 add_comparison(expr
->left
, SPECIAL_EQUAL
, expr
->right
);
1262 right
= strip_expr(expr
->right
);
1263 if (right
->type
== EXPR_BINOP
)
1264 match_binop_assign(expr
);
1267 int get_comparison_strings(const char *one
, const char *two
)
1270 struct smatch_state
*state
;
1274 if (strcmp(one
, two
) == 0)
1275 return SPECIAL_EQUAL
;
1277 if (strcmp(one
, two
) > 0) {
1278 const char *tmp
= one
;
1285 snprintf(buf
, sizeof(buf
), "%s vs %s", one
, two
);
1286 state
= get_state(compare_id
, buf
, NULL
);
1288 ret
= state_to_comparison(state
);
1291 ret
= flip_comparison(ret
);
1296 int get_comparison(struct expression
*a
, struct expression
*b
)
1302 one
= chunk_to_var(a
);
1305 two
= chunk_to_var(b
);
1309 ret
= get_comparison_strings(one
, two
);
1316 int possible_comparison(struct expression
*a
, int comparison
, struct expression
*b
)
1322 struct sm_state
*sm
;
1325 one
= chunk_to_var(a
);
1328 two
= chunk_to_var(b
);
1333 if (strcmp(one
, two
) == 0 && comparison
== SPECIAL_EQUAL
) {
1338 if (strcmp(one
, two
) > 0) {
1343 comparison
= flip_comparison(comparison
);
1346 snprintf(buf
, sizeof(buf
), "%s vs %s", one
, two
);
1347 sm
= get_sm_state(compare_id
, buf
, NULL
);
1351 FOR_EACH_PTR(sm
->possible
, sm
) {
1352 if (!sm
->state
->data
)
1354 saved
= ((struct compare_data
*)sm
->state
->data
)->comparison
;
1355 if (saved
== comparison
)
1357 if (comparison
== SPECIAL_EQUAL
&&
1358 (saved
== SPECIAL_LTE
||
1359 saved
== SPECIAL_GTE
||
1360 saved
== SPECIAL_UNSIGNED_LTE
||
1361 saved
== SPECIAL_UNSIGNED_GTE
))
1365 } END_FOR_EACH_PTR(sm
);
1374 struct state_list
*get_all_comparisons(struct expression
*expr
)
1376 struct smatch_state
*state
;
1377 struct string_list
*links
;
1378 struct state_list
*ret
= NULL
;
1379 struct sm_state
*sm
;
1382 state
= get_state_expr(link_id
, expr
);
1385 links
= state
->data
;
1387 FOR_EACH_PTR(links
, tmp
) {
1388 sm
= get_sm_state(compare_id
, tmp
, NULL
);
1391 // FIXME have to compare name with vsl
1392 add_ptr_list(&ret
, sm
);
1393 } END_FOR_EACH_PTR(tmp
);
1398 struct state_list
*get_all_possible_equal_comparisons(struct expression
*expr
)
1400 struct smatch_state
*state
;
1401 struct string_list
*links
;
1402 struct state_list
*ret
= NULL
;
1403 struct sm_state
*sm
;
1406 state
= get_state_expr(link_id
, expr
);
1409 links
= state
->data
;
1411 FOR_EACH_PTR(links
, tmp
) {
1412 sm
= get_sm_state(compare_id
, tmp
, NULL
);
1415 if (!strchr(sm
->state
->name
, '='))
1417 if (strcmp(sm
->state
->name
, "!=") == 0)
1419 add_ptr_list(&ret
, sm
);
1420 } END_FOR_EACH_PTR(tmp
);
1425 static void update_links_from_call(struct expression
*left
,
1427 struct expression
*right
)
1429 struct string_list
*links
;
1430 struct smatch_state
*state
;
1431 struct compare_data
*data
;
1432 struct symbol
*left_sym
, *right_sym
;
1433 char *left_var
= NULL
;
1434 char *right_var
= NULL
;
1435 struct var_sym_list
*left_vsl
;
1437 struct var_sym_list
*vsl
;
1441 left_var
= chunk_to_var_sym(left
, &left_sym
);
1444 left_vsl
= expr_to_vsl(left
);
1445 right_var
= chunk_to_var_sym(right
, &right_sym
);
1449 state
= get_state(link_id
, right_var
, right_sym
);
1452 links
= state
->data
;
1454 FOR_EACH_PTR(links
, tmp
) {
1455 state
= get_state(compare_id
, tmp
, NULL
);
1456 if (!state
|| !state
->data
)
1459 comparison
= data
->comparison
;
1462 if (strcmp(var
, right_var
) == 0) {
1465 comparison
= flip_comparison(comparison
);
1467 comparison
= combine_comparisons(left_compare
, comparison
);
1470 add_comparison_var_sym(left_var
, left_vsl
, comparison
, var
, vsl
);
1471 } END_FOR_EACH_PTR(tmp
);
1474 free_string(right_var
);
1477 void __add_comparison_info(struct expression
*expr
, struct expression
*call
, const char *range
)
1479 struct expression
*arg
;
1481 const char *c
= range
;
1483 if (!str_to_comparison_arg(c
, call
, &comparison
, &arg
))
1485 update_links_from_call(expr
, comparison
, arg
);
1486 add_comparison(expr
, comparison
, arg
);
1489 static char *range_comparison_to_param_helper(struct expression
*expr
, char starts_with
, int ignore
)
1491 struct symbol
*param
;
1494 char *ret_str
= NULL
;
1498 var
= chunk_to_var(expr
);
1503 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, param
) {
1509 snprintf(buf
, sizeof(buf
), "%s orig", param
->ident
->name
);
1510 compare
= get_comparison_strings(var
, buf
);
1513 if (show_special(compare
)[0] != starts_with
)
1515 snprintf(buf
, sizeof(buf
), "[%s$%d]", show_special(compare
), i
);
1516 ret_str
= alloc_sname(buf
);
1518 } END_FOR_EACH_PTR(param
);
1525 char *name_sym_to_param_comparison(const char *name
, struct symbol
*sym
)
1527 struct symbol
*param
;
1533 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, param
) {
1537 snprintf(buf
, sizeof(buf
), "%s orig", param
->ident
->name
);
1538 compare
= get_comparison_strings(name
, buf
);
1541 snprintf(buf
, sizeof(buf
), "[%s$%d]", show_special(compare
), i
);
1542 return alloc_sname(buf
);
1543 } END_FOR_EACH_PTR(param
);
1548 char *expr_equal_to_param(struct expression
*expr
, int ignore
)
1550 return range_comparison_to_param_helper(expr
, '=', ignore
);
1553 char *expr_lte_to_param(struct expression
*expr
, int ignore
)
1555 return range_comparison_to_param_helper(expr
, '<', ignore
);
1558 char *expr_param_comparison(struct expression
*expr
, int ignore
)
1560 struct symbol
*param
;
1563 char *ret_str
= NULL
;
1567 var
= chunk_to_var(expr
);
1572 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, param
) {
1578 snprintf(buf
, sizeof(buf
), "%s orig", param
->ident
->name
);
1579 compare
= get_comparison_strings(var
, buf
);
1582 snprintf(buf
, sizeof(buf
), "[%s$%d]", show_special(compare
), i
);
1583 ret_str
= alloc_sname(buf
);
1585 } END_FOR_EACH_PTR(param
);
1592 static void match_call_info(struct expression
*expr
)
1594 struct expression
*arg
;
1595 struct smatch_state
*state
;
1596 struct sm_state
*sm
;
1597 struct compare_data
*data
;
1599 struct string_list
*links
;
1601 const char *right_name
;
1607 FOR_EACH_PTR(expr
->args
, arg
) {
1610 state
= get_state_expr(link_id
, arg
);
1614 links
= state
->data
;
1615 FOR_EACH_PTR(links
, link
) {
1616 if (strstr(link
, " orig"))
1618 sm
= get_sm_state(compare_id
, link
, NULL
);
1621 data
= sm
->state
->data
;
1622 if (!data
|| !data
->comparison
)
1624 arg_name
= expr_to_var(arg
);
1628 if (strcmp(data
->var1
, arg_name
) == 0) {
1629 comparison
= data
->comparison
;
1630 right_name
= data
->var2
;
1631 } else if (strcmp(data
->var2
, arg_name
) == 0) {
1632 comparison
= flip_comparison(data
->comparison
);
1633 right_name
= data
->var1
;
1638 snprintf(info_buf
, sizeof(info_buf
), "%s %s", show_special(comparison
), right_name
);
1639 sql_insert_caller_info(expr
, PARAM_COMPARE
, i
, "$", info_buf
);
1642 free_string(arg_name
);
1643 } END_FOR_EACH_PTR(link
);
1644 } END_FOR_EACH_PTR(arg
);
1647 static void free_data(struct symbol
*sym
)
1651 clear_compare_data_alloc();
1654 void register_comparison(int id
)
1657 add_hook(&save_start_states
, AFTER_DEF_HOOK
);
1658 add_unmatched_state_hook(compare_id
, unmatched_comparison
);
1659 add_merge_hook(compare_id
, &merge_compare_states
);
1660 add_hook(&free_data
, AFTER_FUNC_HOOK
);
1661 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
1664 void register_comparison_late(int id
)
1666 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
1669 void register_comparison_links(int id
)
1672 add_merge_hook(link_id
, &merge_links
);
1673 add_modification_hook_late(link_id
, &match_modify
);
1676 void register_comparison_inc_dec(int id
)
1679 add_modification_hook_late(inc_dec_id
, &iter_modify
);
1682 void register_comparison_inc_dec_links(int id
)
1684 inc_dec_link_id
= id
;
1685 set_up_link_functions(inc_dec_id
, inc_dec_link_id
);
1688 static void filter_by_sm(struct sm_state
*sm
, int op
,
1689 struct stree_stack
**true_stack
,
1690 struct stree_stack
**false_stack
)
1692 struct compare_data
*data
;
1698 data
= sm
->state
->data
;
1701 filter_by_sm(sm
->left
, op
, true_stack
, false_stack
);
1702 filter_by_sm(sm
->right
, op
, true_stack
, false_stack
);
1707 if (data
->comparison
&&
1708 data
->comparison
== filter_comparison(data
->comparison
, op
))
1711 if (data
->comparison
&&
1712 data
->comparison
== filter_comparison(data
->comparison
, negate_comparison(op
)))
1716 add_pool(true_stack
, sm
->pool
);
1718 add_pool(false_stack
, sm
->pool
);
1721 filter_by_sm(sm
->left
, op
, true_stack
, false_stack
);
1722 filter_by_sm(sm
->right
, op
, true_stack
, false_stack
);
1726 struct sm_state
*comparison_implication_hook(struct expression
*expr
,
1727 struct stree_stack
**true_stack
,
1728 struct stree_stack
**false_stack
)
1730 struct sm_state
*sm
;
1733 static char buf
[256];
1735 if (expr
->type
!= EXPR_COMPARE
)
1740 left
= expr_to_var(expr
->left
);
1741 right
= expr_to_var(expr
->right
);
1742 if (!left
|| !right
) {
1748 if (strcmp(left
, right
) > 0) {
1753 op
= flip_comparison(op
);
1756 snprintf(buf
, sizeof(buf
), "%s vs %s", left
, right
);
1757 sm
= get_sm_state(compare_id
, buf
, NULL
);
1763 filter_by_sm(sm
, op
, true_stack
, false_stack
);
1764 if (!*true_stack
&& !*false_stack
)