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 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 struct smatch_state
*alloc_compare_state(
61 struct expression
*left
,
62 const char *left_var
, struct var_sym_list
*left_vsl
,
64 struct expression
*right
,
65 const char *right_var
, struct var_sym_list
*right_vsl
)
67 struct smatch_state
*state
;
68 struct compare_data
*data
;
70 state
= __alloc_smatch_state(0);
71 state
->name
= alloc_sname(show_special(comparison
));
72 data
= __alloc_compare_data(0);
74 data
->left_var
= alloc_sname(left_var
);
75 data
->left_vsl
= clone_var_sym_list(left_vsl
);
76 data
->comparison
= comparison
;
78 data
->right_var
= alloc_sname(right_var
);
79 data
->right_vsl
= clone_var_sym_list(right_vsl
);
84 int state_to_comparison(struct smatch_state
*state
)
86 if (!state
|| !state
->data
)
88 return ((struct compare_data
*)state
->data
)->comparison
;
92 * flip_comparison() reverses the op left and right. So "x >= y" becomes "y <= x".
94 int flip_comparison(int op
)
101 case SPECIAL_UNSIGNED_LT
:
102 return SPECIAL_UNSIGNED_GT
;
105 case SPECIAL_UNSIGNED_LTE
:
106 return SPECIAL_UNSIGNED_GTE
;
108 return SPECIAL_EQUAL
;
109 case SPECIAL_NOTEQUAL
:
110 return SPECIAL_NOTEQUAL
;
113 case SPECIAL_UNSIGNED_GTE
:
114 return SPECIAL_UNSIGNED_LTE
;
117 case SPECIAL_UNSIGNED_GT
:
118 return SPECIAL_UNSIGNED_LT
;
120 sm_perror("unhandled comparison %d", op
);
125 int negate_comparison(int op
)
132 case SPECIAL_UNSIGNED_LT
:
133 return SPECIAL_UNSIGNED_GTE
;
136 case SPECIAL_UNSIGNED_LTE
:
137 return SPECIAL_UNSIGNED_GT
;
139 return SPECIAL_NOTEQUAL
;
140 case SPECIAL_NOTEQUAL
:
141 return SPECIAL_EQUAL
;
144 case SPECIAL_UNSIGNED_GTE
:
145 return SPECIAL_UNSIGNED_LT
;
148 case SPECIAL_UNSIGNED_GT
:
149 return SPECIAL_UNSIGNED_LTE
;
151 sm_perror("unhandled comparison %d", op
);
156 static int rl_comparison(struct range_list
*left_rl
, struct range_list
*right_rl
)
158 sval_t left_min
, left_max
, right_min
, right_max
;
159 struct symbol
*type
= &int_ctype
;
161 if (!left_rl
|| !right_rl
)
164 if (type_positive_bits(rl_type(left_rl
)) > type_positive_bits(type
))
165 type
= rl_type(left_rl
);
166 if (type_positive_bits(rl_type(right_rl
)) > type_positive_bits(type
))
167 type
= rl_type(right_rl
);
169 left_rl
= cast_rl(type
, left_rl
);
170 right_rl
= cast_rl(type
, right_rl
);
172 left_min
= rl_min(left_rl
);
173 left_max
= rl_max(left_rl
);
174 right_min
= rl_min(right_rl
);
175 right_max
= rl_max(right_rl
);
177 if (left_min
.value
== left_max
.value
&&
178 right_min
.value
== right_max
.value
&&
179 left_min
.value
== right_min
.value
)
180 return SPECIAL_EQUAL
;
182 if (sval_cmp(left_max
, right_min
) < 0)
184 if (sval_cmp(left_max
, right_min
) == 0)
186 if (sval_cmp(left_min
, right_max
) > 0)
188 if (sval_cmp(left_min
, right_max
) == 0)
194 static int comparison_from_extra(struct expression
*a
, struct expression
*b
)
196 struct range_list
*left
, *right
;
198 if (!get_implied_rl(a
, &left
))
200 if (!get_implied_rl(b
, &right
))
203 return rl_comparison(left
, right
);
206 static struct range_list
*get_orig_rl(struct var_sym_list
*vsl
)
209 struct smatch_state
*state
;
213 sym
= vsl_to_sym(vsl
);
214 if (!sym
|| !sym
->ident
)
216 state
= get_orig_estate(sym
->ident
->name
, sym
);
217 return estate_rl(state
);
220 static struct smatch_state
*unmatched_comparison(struct sm_state
*sm
)
222 struct compare_data
*data
= sm
->state
->data
;
223 struct range_list
*left_rl
, *right_rl
;
229 if (strstr(data
->left_var
, " orig"))
230 left_rl
= get_orig_rl(data
->left_vsl
);
231 else if (!get_implied_rl_var_sym(data
->left_var
, vsl_to_sym(data
->left_vsl
), &left_rl
))
234 if (strstr(data
->right_var
, " orig"))
235 right_rl
= get_orig_rl(data
->right_vsl
);
236 else if (!get_implied_rl_var_sym(data
->right_var
, vsl_to_sym(data
->right_vsl
), &right_rl
))
239 op
= rl_comparison(left_rl
, right_rl
);
241 return alloc_compare_state(
242 data
->left
, data
->left_var
, data
->left_vsl
,
244 data
->right
, data
->right_var
, data
->right_vsl
);
249 /* remove_unsigned_from_comparison() is obviously a hack. */
250 int remove_unsigned_from_comparison(int op
)
253 case SPECIAL_UNSIGNED_LT
:
255 case SPECIAL_UNSIGNED_LTE
:
257 case SPECIAL_UNSIGNED_GTE
:
259 case SPECIAL_UNSIGNED_GT
:
267 * This is for when you merge states "a < b" and "a == b", the result is that
268 * we can say for sure, "a <= b" after the merge.
270 int merge_comparisons(int one
, int two
)
277 one
= remove_unsigned_from_comparison(one
);
278 two
= remove_unsigned_from_comparison(two
);
328 return SPECIAL_NOTEQUAL
;
339 * This is for if you have "a < b" and "b <= c". Or in other words,
340 * "a < b <= c". You would call this like get_combined_comparison('<', '<=').
341 * The return comparison would be '<'.
343 * This function is different from merge_comparisons(), for example:
344 * merge_comparison('<', '==') returns '<='
345 * get_combined_comparison('<', '==') returns '<'
347 int combine_comparisons(int left_compare
, int right_compare
)
351 left_compare
= remove_unsigned_from_comparison(left_compare
);
352 right_compare
= remove_unsigned_from_comparison(right_compare
);
356 switch (left_compare
) {
365 return right_compare
;
374 switch (right_compare
) {
406 int filter_comparison(int orig
, int op
)
411 orig
= remove_unsigned_from_comparison(orig
);
412 op
= remove_unsigned_from_comparison(op
);
421 case SPECIAL_NOTEQUAL
:
431 case SPECIAL_NOTEQUAL
:
440 case SPECIAL_UNSIGNED_LTE
:
441 case SPECIAL_UNSIGNED_GTE
:
442 return SPECIAL_EQUAL
;
445 case SPECIAL_NOTEQUAL
:
450 case SPECIAL_UNSIGNED_LT
:
451 case SPECIAL_UNSIGNED_LTE
:
452 return SPECIAL_UNSIGNED_LT
;
453 case SPECIAL_NOTEQUAL
:
458 case SPECIAL_UNSIGNED_GT
:
459 case SPECIAL_UNSIGNED_GTE
:
460 return SPECIAL_UNSIGNED_GT
;
466 return SPECIAL_EQUAL
;
471 case SPECIAL_NOTEQUAL
:
479 case SPECIAL_NOTEQUAL
:
483 case SPECIAL_UNSIGNED_LT
:
485 case SPECIAL_UNSIGNED_LT
:
486 case SPECIAL_UNSIGNED_LTE
:
487 case SPECIAL_NOTEQUAL
:
488 return SPECIAL_UNSIGNED_LT
;
491 case SPECIAL_UNSIGNED_LTE
:
493 case SPECIAL_UNSIGNED_LT
:
494 case SPECIAL_UNSIGNED_LTE
:
497 case SPECIAL_NOTEQUAL
:
498 return SPECIAL_UNSIGNED_LT
;
499 case SPECIAL_UNSIGNED_GTE
:
500 return SPECIAL_EQUAL
;
503 case SPECIAL_UNSIGNED_GTE
:
505 case SPECIAL_UNSIGNED_LTE
:
506 return SPECIAL_EQUAL
;
507 case SPECIAL_NOTEQUAL
:
508 return SPECIAL_UNSIGNED_GT
;
510 case SPECIAL_UNSIGNED_GTE
:
511 case SPECIAL_UNSIGNED_GT
:
515 case SPECIAL_UNSIGNED_GT
:
517 case SPECIAL_UNSIGNED_GT
:
518 case SPECIAL_UNSIGNED_GTE
:
519 case SPECIAL_NOTEQUAL
:
520 return SPECIAL_UNSIGNED_GT
;
527 static void pre_merge_hook(struct sm_state
*sm
)
529 struct compare_data
*data
= sm
->state
->data
;
534 other
= get_comparison(data
->left
, data
->right
);
538 set_state(compare_id
, sm
->name
, NULL
,
539 alloc_compare_state(data
->left
, data
->left_var
, data
->left_vsl
,
541 data
->right
, data
->right_var
, data
->right_vsl
));
544 struct smatch_state
*merge_compare_states(struct smatch_state
*s1
, struct smatch_state
*s2
)
546 struct compare_data
*data
= s1
->data
;
549 op
= merge_comparisons(state_to_comparison(s1
), state_to_comparison(s2
));
551 return alloc_compare_state(
552 data
->left
, data
->left_var
, data
->left_vsl
,
554 data
->right
, data
->right_var
, data
->right_vsl
);
558 static struct smatch_state
*alloc_link_state(struct string_list
*links
)
560 struct smatch_state
*state
;
561 static char buf
[256];
565 state
= __alloc_smatch_state(0);
568 FOR_EACH_PTR(links
, tmp
) {
570 snprintf(buf
, sizeof(buf
), "%s", tmp
);
572 append(buf
, ", ", sizeof(buf
));
573 append(buf
, tmp
, sizeof(buf
));
575 } END_FOR_EACH_PTR(tmp
);
577 state
->name
= alloc_sname(buf
);
582 static void save_start_states(struct statement
*stmt
)
584 struct symbol
*param
;
586 char state_name
[128];
587 struct smatch_state
*state
;
588 struct string_list
*links
;
591 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, param
) {
592 struct var_sym_list
*left_vsl
= NULL
;
593 struct var_sym_list
*right_vsl
= NULL
;
597 snprintf(orig
, sizeof(orig
), "%s orig", param
->ident
->name
);
598 snprintf(state_name
, sizeof(state_name
), "%s vs %s", param
->ident
->name
, orig
);
599 add_var_sym(&left_vsl
, param
->ident
->name
, param
);
600 add_var_sym(&right_vsl
, orig
, param
);
601 state
= alloc_compare_state(
602 NULL
, param
->ident
->name
, left_vsl
,
604 NULL
, alloc_sname(orig
), right_vsl
);
605 set_state(compare_id
, state_name
, NULL
, state
);
607 link
= alloc_sname(state_name
);
609 insert_string(&links
, link
);
610 state
= alloc_link_state(links
);
611 set_state(link_id
, param
->ident
->name
, param
, state
);
612 } END_FOR_EACH_PTR(param
);
615 static struct smatch_state
*merge_links(struct smatch_state
*s1
, struct smatch_state
*s2
)
617 struct smatch_state
*ret
;
618 struct string_list
*links
;
620 links
= combine_string_lists(s1
->data
, s2
->data
);
621 ret
= alloc_link_state(links
);
625 static void save_link_var_sym(const char *var
, struct symbol
*sym
, const char *link
)
627 struct smatch_state
*old_state
, *new_state
;
628 struct string_list
*links
;
631 old_state
= get_state(link_id
, var
, sym
);
633 links
= clone_str_list(old_state
->data
);
637 new = alloc_sname(link
);
638 insert_string(&links
, new);
640 new_state
= alloc_link_state(links
);
641 set_state(link_id
, var
, sym
, new_state
);
644 static void match_inc(struct sm_state
*sm
)
646 struct string_list
*links
;
647 struct smatch_state
*state
, *new;
648 struct compare_data
*data
;
653 links
= sm
->state
->data
;
655 FOR_EACH_PTR(links
, tmp
) {
656 state
= get_state(compare_id
, tmp
, NULL
);
664 if (strncmp(sm
->name
, tmp
, strlen(sm
->name
)) != 0 ||
665 tmp
[strlen(sm
->name
)] != ' ')
668 op
= state_to_comparison(state
);
670 switch (flip
? flip_comparison(op
) : op
) {
673 case SPECIAL_UNSIGNED_GTE
:
675 case SPECIAL_UNSIGNED_GT
:
676 new = alloc_compare_state(
677 data
->left
, data
->left_var
, data
->left_vsl
,
679 data
->right
, data
->right_var
, data
->right_vsl
);
680 set_state(compare_id
, tmp
, NULL
, new);
683 case SPECIAL_UNSIGNED_LT
:
684 new = alloc_compare_state(
685 data
->left
, data
->left_var
, data
->left_vsl
,
686 flip
? SPECIAL_GTE
: SPECIAL_LTE
,
687 data
->right
, data
->right_var
, data
->right_vsl
);
688 set_state(compare_id
, tmp
, NULL
, new);
691 set_state(compare_id
, tmp
, NULL
, &undefined
);
693 } END_FOR_EACH_PTR(tmp
);
696 static void match_dec(struct sm_state
*sm
)
698 struct string_list
*links
;
699 struct smatch_state
*state
;
702 links
= sm
->state
->data
;
704 FOR_EACH_PTR(links
, tmp
) {
705 state
= get_state(compare_id
, tmp
, NULL
);
707 switch (state_to_comparison(state
)) {
710 case SPECIAL_UNSIGNED_LTE
:
712 case SPECIAL_UNSIGNED_LT
: {
713 struct compare_data
*data
= state
->data
;
714 struct smatch_state
*new;
716 new = alloc_compare_state(
717 data
->left
, data
->left_var
, data
->left_vsl
,
719 data
->right
, data
->right_var
, data
->right_vsl
);
720 set_state(compare_id
, tmp
, NULL
, new);
724 set_state(compare_id
, tmp
, NULL
, &undefined
);
726 } END_FOR_EACH_PTR(tmp
);
729 static void match_inc_dec(struct sm_state
*sm
, struct expression
*mod_expr
)
732 * if (foo > bar) then ++foo is also > bar.
736 if (mod_expr
->type
!= EXPR_PREOP
&& mod_expr
->type
!= EXPR_POSTOP
)
739 if (mod_expr
->op
== SPECIAL_INCREMENT
)
741 else if (mod_expr
->op
== SPECIAL_DECREMENT
)
745 static int is_self_assign(struct expression
*expr
)
747 if (!expr
|| expr
->type
!= EXPR_ASSIGNMENT
|| expr
->op
!= '=')
749 return expr_equiv(expr
->left
, expr
->right
);
752 static void match_modify(struct sm_state
*sm
, struct expression
*mod_expr
)
754 struct string_list
*links
;
757 if (mod_expr
&& is_self_assign(mod_expr
))
760 /* handled by match_inc_dec() */
762 ((mod_expr
->type
== EXPR_PREOP
|| mod_expr
->type
== EXPR_POSTOP
) &&
763 (mod_expr
->op
== SPECIAL_INCREMENT
|| mod_expr
->op
== SPECIAL_DECREMENT
)))
766 links
= sm
->state
->data
;
768 FOR_EACH_PTR(links
, tmp
) {
769 set_state(compare_id
, tmp
, NULL
, &undefined
);
770 } END_FOR_EACH_PTR(tmp
);
771 set_state(link_id
, sm
->name
, sm
->sym
, &undefined
);
774 static void match_preop(struct expression
*expr
)
776 struct expression
*parent
;
777 struct range_list
*left
, *right
;
781 * This is an important special case. Say you have:
785 * Assume that we know the range of limit is higher than the start
786 * value for "j". Then the first thing that we process is the ++j. We
787 * have not comparison states set up so it doesn't get caught by the
788 * modification hook. But it does get caught by smatch_extra which sets
789 * j to unknown then we parse the "j == limit" and sets false to != but
790 * really we want false to be <.
792 * So what we do is we set j < limit here, then the match_modify catches
793 * it and we do a match_inc_dec().
797 if (expr
->type
!= EXPR_PREOP
||
798 (expr
->op
!= SPECIAL_INCREMENT
&& expr
->op
!= SPECIAL_DECREMENT
))
801 parent
= expr_get_parent_expr(expr
);
804 if (parent
->type
!= EXPR_COMPARE
|| parent
->op
!= SPECIAL_EQUAL
)
806 if (parent
->left
!= expr
)
809 if (!get_implied_rl(expr
->unop
, &left
) ||
810 !get_implied_rl(parent
->right
, &right
))
813 op
= rl_comparison(left
, right
);
817 add_comparison(expr
->unop
, op
, parent
->right
);
820 static char *chunk_to_var_sym(struct expression
*expr
, struct symbol
**sym
)
822 expr
= strip_expr(expr
);
828 if (expr
->type
== EXPR_PREOP
&&
829 (expr
->op
== SPECIAL_INCREMENT
||
830 expr
->op
== SPECIAL_DECREMENT
))
831 expr
= strip_expr(expr
->unop
);
833 if (expr
->type
== EXPR_CALL
) {
836 snprintf(buf
, sizeof(buf
), "return %p", expr
);
837 return alloc_string(buf
);
840 return expr_to_chunk_sym_vsl(expr
, sym
, NULL
);
843 static char *chunk_to_var(struct expression
*expr
)
845 return chunk_to_var_sym(expr
, NULL
);
848 static struct smatch_state
*get_state_chunk(int owner
, struct expression
*expr
)
852 struct smatch_state
*ret
;
854 name
= chunk_to_var_sym(expr
, &sym
);
858 ret
= get_state(owner
, name
, sym
);
863 static void save_link(struct expression
*expr
, char *link
)
868 expr
= strip_expr(expr
);
869 if (expr
->type
== EXPR_BINOP
) {
872 chunk
= chunk_to_var(expr
);
876 save_link(expr
->left
, link
);
877 save_link(expr
->right
, link
);
878 save_link_var_sym(chunk
, NULL
, link
);
882 var
= chunk_to_var_sym(expr
, &sym
);
886 save_link_var_sym(var
, sym
, link
);
890 static int get_orig_comparison(struct stree
*pre_stree
, const char *left
, const char *right
)
892 struct smatch_state
*state
;
893 struct compare_data
*data
;
895 char state_name
[256];
897 if (strcmp(left
, right
) > 0) {
898 const char *tmp
= right
;
905 snprintf(state_name
, sizeof(state_name
), "%s vs %s", left
, right
);
906 state
= get_state_stree(pre_stree
, compare_id
, state_name
, NULL
);
907 if (!state
|| !state
->data
)
911 return flip_comparison(data
->comparison
);
912 return data
->comparison
;
916 static int have_common_var_sym(struct var_sym_list
*left_vsl
, struct var_sym_list
*right_vsl
)
920 FOR_EACH_PTR(left_vsl
, tmp
) {
921 if (in_var_sym_list(right_vsl
, tmp
->var
, tmp
->sym
))
923 } END_FOR_EACH_PTR(tmp
);
929 * The idea here is that we take a comparison "a < b" and then we look at all
930 * the things which "b" is compared against "b < c" and we say that that implies
931 * a relationship "a < c".
933 * The names here about because the comparisons are organized like this
937 static void update_tf_links(struct stree
*pre_stree
,
938 struct expression
*left_expr
,
939 const char *left_var
, struct var_sym_list
*left_vsl
,
940 int left_comparison
, int left_false_comparison
,
941 const char *mid_var
, struct var_sym_list
*mid_vsl
,
942 struct string_list
*links
)
944 struct smatch_state
*state
;
945 struct smatch_state
*true_state
, *false_state
;
946 struct compare_data
*data
;
947 struct expression
*right_expr
;
948 const char *right_var
;
949 struct var_sym_list
*right_vsl
;
951 int right_comparison
;
953 int false_comparison
;
955 char state_name
[256];
958 FOR_EACH_PTR(links
, tmp
) {
959 state
= get_state_stree(pre_stree
, compare_id
, tmp
, NULL
);
960 if (!state
|| !state
->data
)
963 right_comparison
= data
->comparison
;
964 right_expr
= data
->right
;
965 right_var
= data
->right_var
;
966 right_vsl
= data
->right_vsl
;
967 if (strcmp(mid_var
, right_var
) == 0) {
968 right_expr
= data
->left
;
969 right_var
= data
->left_var
;
970 right_vsl
= data
->left_vsl
;
971 right_comparison
= flip_comparison(right_comparison
);
973 if (have_common_var_sym(left_vsl
, right_vsl
))
976 orig_comparison
= get_orig_comparison(pre_stree
, left_var
, right_var
);
978 true_comparison
= combine_comparisons(left_comparison
, right_comparison
);
979 false_comparison
= combine_comparisons(left_false_comparison
, right_comparison
);
981 true_comparison
= filter_comparison(orig_comparison
, true_comparison
);
982 false_comparison
= filter_comparison(orig_comparison
, false_comparison
);
984 if (strcmp(left_var
, right_var
) > 0) {
985 struct expression
*tmp_expr
= left_expr
;
986 const char *tmp_var
= left_var
;
987 struct var_sym_list
*tmp_vsl
= left_vsl
;
989 left_expr
= right_expr
;
990 left_var
= right_var
;
991 left_vsl
= right_vsl
;
992 right_expr
= tmp_expr
;
995 true_comparison
= flip_comparison(true_comparison
);
996 false_comparison
= flip_comparison(false_comparison
);
999 if (!true_comparison
&& !false_comparison
)
1002 if (true_comparison
)
1003 true_state
= alloc_compare_state(
1004 left_expr
, left_var
, left_vsl
,
1006 right_expr
, right_var
, right_vsl
);
1009 if (false_comparison
)
1010 false_state
= alloc_compare_state(
1011 left_expr
, left_var
, left_vsl
,
1013 right_expr
, right_var
, right_vsl
);
1017 snprintf(state_name
, sizeof(state_name
), "%s vs %s", left_var
, right_var
);
1018 set_true_false_states(compare_id
, state_name
, NULL
, true_state
, false_state
);
1019 FOR_EACH_PTR(left_vsl
, vs
) {
1020 save_link_var_sym(vs
->var
, vs
->sym
, state_name
);
1021 } END_FOR_EACH_PTR(vs
);
1022 FOR_EACH_PTR(right_vsl
, vs
) {
1023 save_link_var_sym(vs
->var
, vs
->sym
, state_name
);
1024 } END_FOR_EACH_PTR(vs
);
1025 if (!vsl_to_sym(left_vsl
))
1026 save_link_var_sym(left_var
, NULL
, state_name
);
1027 if (!vsl_to_sym(right_vsl
))
1028 save_link_var_sym(right_var
, NULL
, state_name
);
1029 } END_FOR_EACH_PTR(tmp
);
1032 static void update_tf_data(struct stree
*pre_stree
,
1033 struct expression
*left_expr
,
1034 const char *left_name
, struct var_sym_list
*left_vsl
,
1035 struct expression
*right_expr
,
1036 const char *right_name
, struct var_sym_list
*right_vsl
,
1037 int true_comparison
, int false_comparison
)
1039 struct smatch_state
*state
;
1041 state
= get_state_stree(pre_stree
, link_id
, right_name
, vsl_to_sym(right_vsl
));
1043 update_tf_links(pre_stree
, left_expr
, left_name
, left_vsl
, true_comparison
, false_comparison
, right_name
, right_vsl
, state
->data
);
1045 state
= get_state_stree(pre_stree
, link_id
, left_name
, vsl_to_sym(left_vsl
));
1047 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
);
1050 static void iter_modify(struct sm_state
*sm
, struct expression
*mod_expr
)
1052 if (sm
->state
!= &start
||
1054 (mod_expr
->type
!= EXPR_PREOP
&& mod_expr
->type
!= EXPR_POSTOP
) ||
1055 mod_expr
->op
!= SPECIAL_INCREMENT
)
1056 set_state(inc_dec_id
, sm
->name
, sm
->sym
, &undefined
);
1058 set_state(inc_dec_id
, sm
->name
, sm
->sym
, &incremented
);
1061 static void handle_for_loops(struct expression
*expr
, char *state_name
, struct smatch_state
*false_state
)
1064 char *iter_name
, *cap_name
;
1065 struct symbol
*iter_sym
, *cap_sym
;
1066 struct compare_data
*data
;
1068 if (expr
->op
!= '<' && expr
->op
!= SPECIAL_UNSIGNED_LT
)
1071 if (!__cur_stmt
|| !__prev_stmt
)
1073 if (__cur_stmt
->type
!= STMT_ITERATOR
)
1075 if (__cur_stmt
->iterator_pre_condition
!= expr
)
1078 /* literals are handled in smatch_extra.c */
1079 if (get_value(expr
->right
, &sval
))
1082 /* First time checking the condition */
1083 if (__prev_stmt
== __cur_stmt
->iterator_pre_statement
) {
1084 if (!get_implied_value(expr
->left
, &sval
) ||
1088 iter_name
= expr_to_var_sym(expr
->left
, &iter_sym
);
1089 cap_name
= expr_to_var_sym(expr
->right
, &cap_sym
);
1090 if (!iter_name
|| !cap_name
|| !iter_sym
|| !cap_sym
) {
1091 free_string(iter_name
);
1092 free_string(cap_name
);
1096 set_state(inc_dec_id
, iter_name
, iter_sym
, &start
);
1097 store_link(inc_dec_link_id
, cap_name
, cap_sym
, iter_name
, iter_sym
);
1099 free_string(iter_name
);
1100 free_string(cap_name
);
1104 /* Second time checking the condtion */
1105 if (__prev_stmt
!= __cur_stmt
->iterator_post_statement
)
1108 if (get_state_chunk(inc_dec_id
, expr
->left
) != &incremented
)
1111 data
= false_state
->data
;
1112 false_state
= alloc_compare_state(
1113 data
->left
, data
->left_var
, data
->left_vsl
,
1115 data
->right
, data
->right_var
, data
->right_vsl
);
1117 set_true_false_states(compare_id
, state_name
, NULL
, NULL
, false_state
);
1120 static int is_plus_one(struct expression
*expr
)
1124 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '+')
1126 if (!get_implied_value(expr
->right
, &sval
) || sval
.value
!= 1)
1131 static int is_minus_one(struct expression
*expr
)
1135 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '-')
1137 if (!get_implied_value(expr
->right
, &sval
) || sval
.value
!= 1)
1142 static void move_plus_to_minus_helper(struct expression
**left_p
, struct expression
**right_p
)
1144 struct expression
*left
= *left_p
;
1145 struct expression
*right
= *right_p
;
1148 * These two are basically equivalent: "foo + 1 != bar" and
1149 * "foo != bar - 1". There are issues with signedness and integer
1150 * overflows. There are also issues with type as well. But let's
1151 * pretend we can ignore all that stuff for now.
1155 if (!is_plus_one(left
))
1158 *left_p
= left
->left
;
1159 *right_p
= binop_expression(right
, '-', left
->right
);
1162 static void move_plus_to_minus(struct expression
**left_p
, struct expression
**right_p
)
1164 if (is_plus_one(*left_p
) && is_plus_one(*right_p
))
1167 move_plus_to_minus_helper(left_p
, right_p
);
1168 move_plus_to_minus_helper(right_p
, left_p
);
1171 static void handle_comparison(struct expression
*left_expr
, int op
, struct expression
*right_expr
, char **_state_name
, struct smatch_state
**_false_state
)
1175 struct symbol
*left_sym
, *right_sym
;
1176 struct var_sym_list
*left_vsl
= NULL
;
1177 struct var_sym_list
*right_vsl
= NULL
;
1179 int orig_comparison
;
1180 struct smatch_state
*true_state
, *false_state
;
1181 static char state_name
[256];
1182 struct stree
*pre_stree
;
1185 if (!left_expr
|| !right_expr
)
1188 left_expr
= strip_parens(left_expr
);
1189 right_expr
= strip_parens(right_expr
);
1191 while (left_expr
->type
== EXPR_ASSIGNMENT
)
1192 left_expr
= strip_parens(left_expr
->left
);
1193 while (right_expr
->type
== EXPR_ASSIGNMENT
)
1194 right_expr
= strip_parens(right_expr
->left
);
1196 false_op
= negate_comparison(op
);
1198 move_plus_to_minus(&left_expr
, &right_expr
);
1200 if (op
== SPECIAL_UNSIGNED_LT
&&
1201 get_implied_value(left_expr
, &sval
) &&
1203 false_op
= SPECIAL_EQUAL
;
1205 if (op
== SPECIAL_UNSIGNED_GT
&&
1206 get_implied_value(right_expr
, &sval
) &&
1208 false_op
= SPECIAL_EQUAL
;
1210 left
= chunk_to_var_sym(left_expr
, &left_sym
);
1214 add_var_sym(&left_vsl
, left
, left_sym
);
1216 left_vsl
= expr_to_vsl(left_expr
);
1217 right
= chunk_to_var_sym(right_expr
, &right_sym
);
1221 add_var_sym(&right_vsl
, right
, right_sym
);
1223 right_vsl
= expr_to_vsl(right_expr
);
1225 if (strcmp(left
, right
) > 0) {
1226 char *tmp_name
= left
;
1227 struct var_sym_list
*tmp_vsl
= left_vsl
;
1228 struct expression
*tmp_expr
= left_expr
;
1231 left_vsl
= right_vsl
;
1232 left_expr
= right_expr
;
1234 right_vsl
= tmp_vsl
;
1235 right_expr
= tmp_expr
;
1236 op
= flip_comparison(op
);
1237 false_op
= flip_comparison(false_op
);
1240 orig_comparison
= get_comparison(left_expr
, right_expr
);
1241 op
= filter_comparison(orig_comparison
, op
);
1242 false_op
= filter_comparison(orig_comparison
, false_op
);
1244 snprintf(state_name
, sizeof(state_name
), "%s vs %s", left
, right
);
1245 true_state
= alloc_compare_state(
1246 left_expr
, left
, left_vsl
,
1248 right_expr
, right
, right_vsl
);
1249 false_state
= alloc_compare_state(
1250 left_expr
, left
, left_vsl
,
1252 right_expr
, right
, right_vsl
);
1254 pre_stree
= clone_stree(__get_cur_stree());
1255 update_tf_data(pre_stree
, left_expr
, left
, left_vsl
, right_expr
, right
, right_vsl
, op
, false_op
);
1256 free_stree(&pre_stree
);
1258 set_true_false_states(compare_id
, state_name
, NULL
, true_state
, false_state
);
1259 __compare_param_limit_hook(left_expr
, right_expr
, state_name
, true_state
, false_state
);
1260 save_link(left_expr
, state_name
);
1261 save_link(right_expr
, state_name
);
1264 *_false_state
= false_state
;
1266 *_state_name
= state_name
;
1272 void __comparison_match_condition(struct expression
*expr
)
1274 struct expression
*left
, *right
, *new_left
, *new_right
, *tmp
;
1275 struct smatch_state
*false_state
= NULL
;
1276 char *state_name
= NULL
;
1279 if (expr
->type
!= EXPR_COMPARE
)
1282 handle_comparison(expr
->left
, expr
->op
, expr
->right
, &state_name
, &false_state
);
1283 if (false_state
&& state_name
)
1284 handle_for_loops(expr
, state_name
, false_state
);
1286 left
= strip_parens(expr
->left
);
1287 right
= strip_parens(expr
->right
);
1289 if (left
->type
== EXPR_BINOP
&& left
->op
== '+') {
1290 new_left
= left
->left
;
1291 new_right
= binop_expression(right
, '-', left
->right
);
1292 handle_comparison(new_left
, expr
->op
, new_right
, NULL
, NULL
);
1294 new_left
= left
->right
;
1295 new_right
= binop_expression(right
, '-', left
->left
);
1296 handle_comparison(new_left
, expr
->op
, new_right
, NULL
, NULL
);
1301 left
= strip_parens(expr
->left
);
1302 right
= strip_parens(expr
->right
);
1303 if (get_last_expr_from_expression_stmt(expr
->left
)) {
1304 left
= get_last_expr_from_expression_stmt(expr
->left
);
1307 if (get_last_expr_from_expression_stmt(expr
->right
)) {
1308 right
= get_last_expr_from_expression_stmt(expr
->right
);
1316 while ((tmp
= get_assigned_expr(left
))) {
1319 left
= strip_expr(tmp
);
1322 while ((tmp
= get_assigned_expr(right
))) {
1325 right
= strip_expr(tmp
);
1328 handle_comparison(left
, expr
->op
, right
, NULL
, NULL
);
1331 static void add_comparison_var_sym(
1332 struct expression
*left_expr
,
1333 const char *left_name
, struct var_sym_list
*left_vsl
,
1335 struct expression
*right_expr
,
1336 const char *right_name
, struct var_sym_list
*right_vsl
)
1338 struct smatch_state
*state
;
1340 char state_name
[256];
1342 if (strcmp(left_name
, right_name
) > 0) {
1343 struct expression
*tmp_expr
= left_expr
;
1344 const char *tmp_name
= left_name
;
1345 struct var_sym_list
*tmp_vsl
= left_vsl
;
1347 left_expr
= right_expr
;
1348 left_name
= right_name
;
1349 left_vsl
= right_vsl
;
1350 right_expr
= tmp_expr
;
1351 right_name
= tmp_name
;
1352 right_vsl
= tmp_vsl
;
1353 comparison
= flip_comparison(comparison
);
1355 snprintf(state_name
, sizeof(state_name
), "%s vs %s", left_name
, right_name
);
1356 state
= alloc_compare_state(
1357 left_expr
, left_name
, left_vsl
,
1359 right_expr
, right_name
, right_vsl
);
1361 set_state(compare_id
, state_name
, NULL
, state
);
1363 FOR_EACH_PTR(left_vsl
, vs
) {
1364 save_link_var_sym(vs
->var
, vs
->sym
, state_name
);
1365 } END_FOR_EACH_PTR(vs
);
1366 FOR_EACH_PTR(right_vsl
, vs
) {
1367 save_link_var_sym(vs
->var
, vs
->sym
, state_name
);
1368 } END_FOR_EACH_PTR(vs
);
1371 static void add_comparison(struct expression
*left
, int comparison
, struct expression
*right
)
1373 char *left_name
= NULL
;
1374 char *right_name
= NULL
;
1375 struct symbol
*left_sym
, *right_sym
;
1376 struct var_sym_list
*left_vsl
, *right_vsl
;
1377 struct smatch_state
*state
;
1378 char state_name
[256];
1380 left_name
= chunk_to_var_sym(left
, &left_sym
);
1383 left_vsl
= expr_to_vsl(left
);
1384 right_name
= chunk_to_var_sym(right
, &right_sym
);
1387 right_vsl
= expr_to_vsl(right
);
1389 if (strcmp(left_name
, right_name
) > 0) {
1390 struct expression
*tmp_expr
= left
;
1391 struct symbol
*tmp_sym
= left_sym
;
1392 char *tmp_name
= left_name
;
1393 struct var_sym_list
*tmp_vsl
= left_vsl
;
1396 left_name
= right_name
;
1397 left_sym
= right_sym
;
1398 left_vsl
= right_vsl
;
1400 right_name
= tmp_name
;
1401 right_sym
= tmp_sym
;
1402 right_vsl
= tmp_vsl
;
1403 comparison
= flip_comparison(comparison
);
1405 snprintf(state_name
, sizeof(state_name
), "%s vs %s", left_name
, right_name
);
1406 state
= alloc_compare_state(
1407 left
, left_name
, left_vsl
,
1409 right
, right_name
, right_vsl
);
1411 set_state(compare_id
, state_name
, NULL
, state
);
1412 save_link(left
, state_name
);
1413 save_link(right
, state_name
);
1416 free_string(left_name
);
1417 free_string(right_name
);
1420 static void match_assign_add(struct expression
*expr
)
1422 struct expression
*right
;
1423 struct expression
*r_left
, *r_right
;
1424 sval_t left_tmp
, right_tmp
;
1426 right
= strip_expr(expr
->right
);
1427 r_left
= strip_expr(right
->left
);
1428 r_right
= strip_expr(right
->right
);
1430 get_absolute_min(r_left
, &left_tmp
);
1431 get_absolute_min(r_right
, &right_tmp
);
1433 if (left_tmp
.value
> 0)
1434 add_comparison(expr
->left
, '>', r_right
);
1435 else if (left_tmp
.value
== 0)
1436 add_comparison(expr
->left
, SPECIAL_GTE
, r_right
);
1438 if (right_tmp
.value
> 0)
1439 add_comparison(expr
->left
, '>', r_left
);
1440 else if (right_tmp
.value
== 0)
1441 add_comparison(expr
->left
, SPECIAL_GTE
, r_left
);
1444 static void match_assign_sub(struct expression
*expr
)
1446 struct expression
*right
;
1447 struct expression
*r_left
, *r_right
;
1451 right
= strip_expr(expr
->right
);
1452 r_left
= strip_expr(right
->left
);
1453 r_right
= strip_expr(right
->right
);
1455 if (get_absolute_min(r_right
, &min
) && sval_is_negative(min
))
1458 comparison
= get_comparison(r_left
, r_right
);
1460 switch (comparison
) {
1463 if (implied_not_equal(r_right
, 0))
1464 add_comparison(expr
->left
, '>', r_left
);
1466 add_comparison(expr
->left
, SPECIAL_GTE
, r_left
);
1471 static void match_assign_divide(struct expression
*expr
)
1473 struct expression
*right
;
1474 struct expression
*r_left
, *r_right
;
1477 right
= strip_expr(expr
->right
);
1478 r_left
= strip_expr(right
->left
);
1479 r_right
= strip_expr(right
->right
);
1480 if (!get_implied_min(r_right
, &min
) || min
.value
<= 1)
1483 add_comparison(expr
->left
, '<', r_left
);
1486 static void match_binop_assign(struct expression
*expr
)
1488 struct expression
*right
;
1490 right
= strip_expr(expr
->right
);
1491 if (right
->op
== '+')
1492 match_assign_add(expr
);
1493 if (right
->op
== '-')
1494 match_assign_sub(expr
);
1495 if (right
->op
== '/')
1496 match_assign_divide(expr
);
1499 static void copy_comparisons(struct expression
*left
, struct expression
*right
)
1501 struct string_list
*links
;
1502 struct smatch_state
*state
;
1503 struct compare_data
*data
;
1504 struct symbol
*left_sym
, *right_sym
;
1505 char *left_var
= NULL
;
1506 char *right_var
= NULL
;
1507 struct var_sym_list
*left_vsl
;
1508 struct expression
*expr
;
1510 struct var_sym_list
*vsl
;
1514 left_var
= chunk_to_var_sym(left
, &left_sym
);
1517 left_vsl
= expr_to_vsl(left
);
1518 right_var
= chunk_to_var_sym(right
, &right_sym
);
1522 state
= get_state(link_id
, right_var
, right_sym
);
1525 links
= state
->data
;
1527 FOR_EACH_PTR(links
, tmp
) {
1528 state
= get_state(compare_id
, tmp
, NULL
);
1529 if (!state
|| !state
->data
)
1532 comparison
= data
->comparison
;
1534 var
= data
->right_var
;
1535 vsl
= data
->right_vsl
;
1536 if (strcmp(var
, right_var
) == 0) {
1538 var
= data
->left_var
;
1539 vsl
= data
->left_vsl
;
1540 comparison
= flip_comparison(comparison
);
1542 /* n = copy_from_user(dest, src, n); leads to n <= n which is nonsense */
1543 if (strcmp(left_var
, var
) == 0)
1545 add_comparison_var_sym(left
, left_var
, left_vsl
, comparison
, expr
, var
, vsl
);
1546 } END_FOR_EACH_PTR(tmp
);
1549 free_string(right_var
);
1552 static void match_assign(struct expression
*expr
)
1554 struct expression
*right
;
1556 if (expr
->op
!= '=')
1558 if (__in_fake_assign
|| outside_of_function())
1561 if (is_struct(expr
->left
))
1564 if (is_self_assign(expr
))
1567 copy_comparisons(expr
->left
, expr
->right
);
1568 add_comparison(expr
->left
, SPECIAL_EQUAL
, expr
->right
);
1570 right
= strip_expr(expr
->right
);
1571 if (right
->type
== EXPR_BINOP
)
1572 match_binop_assign(expr
);
1575 int get_comparison_strings(const char *one
, const char *two
)
1578 struct smatch_state
*state
;
1585 if (strcmp(one
, two
) == 0)
1586 return SPECIAL_EQUAL
;
1588 if (strcmp(one
, two
) > 0) {
1589 const char *tmp
= one
;
1596 snprintf(buf
, sizeof(buf
), "%s vs %s", one
, two
);
1597 state
= get_state(compare_id
, buf
, NULL
);
1599 ret
= state_to_comparison(state
);
1602 ret
= flip_comparison(ret
);
1607 int get_comparison(struct expression
*a
, struct expression
*b
)
1616 a
= strip_parens(a
);
1617 b
= strip_parens(b
);
1619 move_plus_to_minus(&a
, &b
);
1621 one
= chunk_to_var(a
);
1624 two
= chunk_to_var(b
);
1628 ret
= get_comparison_strings(one
, two
);
1632 if (is_plus_one(a
) || is_minus_one(a
)) {
1634 one
= chunk_to_var(a
->left
);
1635 ret
= get_comparison_strings(one
, two
);
1636 } else if (is_plus_one(b
) || is_minus_one(b
)) {
1638 two
= chunk_to_var(b
->left
);
1639 ret
= get_comparison_strings(one
, two
);
1645 if ((is_plus_one(a
) || is_minus_one(b
)) && ret
== '<')
1647 else if ((is_minus_one(a
) || is_plus_one(b
)) && ret
== '>')
1657 return comparison_from_extra(a
, b
);
1661 int possible_comparison(struct expression
*a
, int comparison
, struct expression
*b
)
1667 struct sm_state
*sm
;
1670 one
= chunk_to_var(a
);
1673 two
= chunk_to_var(b
);
1678 if (strcmp(one
, two
) == 0 && comparison
== SPECIAL_EQUAL
) {
1683 if (strcmp(one
, two
) > 0) {
1688 comparison
= flip_comparison(comparison
);
1691 snprintf(buf
, sizeof(buf
), "%s vs %s", one
, two
);
1692 sm
= get_sm_state(compare_id
, buf
, NULL
);
1696 FOR_EACH_PTR(sm
->possible
, sm
) {
1697 if (!sm
->state
->data
)
1699 saved
= ((struct compare_data
*)sm
->state
->data
)->comparison
;
1700 if (saved
== comparison
)
1702 if (comparison
== SPECIAL_EQUAL
&&
1703 (saved
== SPECIAL_LTE
||
1704 saved
== SPECIAL_GTE
||
1705 saved
== SPECIAL_UNSIGNED_LTE
||
1706 saved
== SPECIAL_UNSIGNED_GTE
))
1710 } END_FOR_EACH_PTR(sm
);
1719 struct state_list
*get_all_comparisons(struct expression
*expr
)
1721 struct smatch_state
*state
;
1722 struct string_list
*links
;
1723 struct state_list
*ret
= NULL
;
1724 struct sm_state
*sm
;
1727 state
= get_state_chunk(link_id
, expr
);
1730 links
= state
->data
;
1732 FOR_EACH_PTR(links
, tmp
) {
1733 sm
= get_sm_state(compare_id
, tmp
, NULL
);
1736 // FIXME have to compare name with vsl
1737 add_ptr_list(&ret
, sm
);
1738 } END_FOR_EACH_PTR(tmp
);
1743 struct state_list
*get_all_possible_equal_comparisons(struct expression
*expr
)
1745 struct smatch_state
*state
;
1746 struct string_list
*links
;
1747 struct state_list
*ret
= NULL
;
1748 struct sm_state
*sm
;
1751 state
= get_state_chunk(link_id
, expr
);
1754 links
= state
->data
;
1756 FOR_EACH_PTR(links
, tmp
) {
1757 sm
= get_sm_state(compare_id
, tmp
, NULL
);
1760 if (!strchr(sm
->state
->name
, '='))
1762 if (strcmp(sm
->state
->name
, "!=") == 0)
1764 add_ptr_list(&ret
, sm
);
1765 } END_FOR_EACH_PTR(tmp
);
1770 struct state_list
*get_all_possible_not_equal_comparisons(struct expression
*expr
)
1772 struct smatch_state
*state
;
1773 struct string_list
*links
;
1774 struct state_list
*ret
= NULL
;
1775 struct sm_state
*sm
;
1776 struct sm_state
*possible
;
1781 state
= get_state_chunk(link_id
, expr
);
1784 links
= state
->data
;
1786 FOR_EACH_PTR(links
, link
) {
1787 sm
= get_sm_state(compare_id
, link
, NULL
);
1790 FOR_EACH_PTR(sm
->possible
, possible
) {
1791 if (strcmp(possible
->state
->name
, "!=") != 0)
1793 add_ptr_list(&ret
, sm
);
1795 } END_FOR_EACH_PTR(link
);
1796 } END_FOR_EACH_PTR(link
);
1801 static void update_links_from_call(struct expression
*left
,
1803 struct expression
*right
)
1805 struct string_list
*links
;
1806 struct smatch_state
*state
;
1807 struct compare_data
*data
;
1808 struct symbol
*left_sym
, *right_sym
;
1809 char *left_var
= NULL
;
1810 char *right_var
= NULL
;
1811 struct var_sym_list
*left_vsl
;
1812 struct expression
*expr
;
1814 struct var_sym_list
*vsl
;
1818 left_var
= chunk_to_var_sym(left
, &left_sym
);
1821 left_vsl
= expr_to_vsl(left
);
1822 right_var
= chunk_to_var_sym(right
, &right_sym
);
1826 state
= get_state(link_id
, right_var
, right_sym
);
1829 links
= state
->data
;
1831 FOR_EACH_PTR(links
, tmp
) {
1832 state
= get_state(compare_id
, tmp
, NULL
);
1833 if (!state
|| !state
->data
)
1836 comparison
= data
->comparison
;
1838 var
= data
->right_var
;
1839 vsl
= data
->right_vsl
;
1840 if (strcmp(var
, right_var
) == 0) {
1842 var
= data
->left_var
;
1843 vsl
= data
->left_vsl
;
1844 comparison
= flip_comparison(comparison
);
1846 comparison
= combine_comparisons(left_compare
, comparison
);
1849 add_comparison_var_sym(left
, left_var
, left_vsl
, comparison
, expr
, var
, vsl
);
1850 } END_FOR_EACH_PTR(tmp
);
1853 free_string(right_var
);
1856 void __add_return_comparison(struct expression
*call
, const char *range
)
1858 struct expression
*arg
;
1862 if (!str_to_comparison_arg(range
, call
, &comparison
, &arg
))
1864 snprintf(buf
, sizeof(buf
), "%s", show_special(comparison
));
1865 update_links_from_call(call
, comparison
, arg
);
1866 add_comparison(call
, comparison
, arg
);
1869 void __add_comparison_info(struct expression
*expr
, struct expression
*call
, const char *range
)
1871 copy_comparisons(expr
, call
);
1874 static char *get_mask_comparison(struct expression
*expr
, int ignore
)
1876 struct expression
*tmp
, *right
;
1880 /* The return value for "return foo & param;" is <= param */
1883 while ((tmp
= get_assigned_expr(expr
))) {
1884 expr
= strip_expr(tmp
);
1889 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '&')
1892 right
= strip_expr(expr
->right
);
1893 param
= get_param_num(right
);
1894 if (param
< 0 || param
== ignore
)
1897 snprintf(buf
, sizeof(buf
), "[<=$%d]", param
);
1898 return alloc_sname(buf
);
1901 static char *range_comparison_to_param_helper(struct expression
*expr
, char starts_with
, int ignore
)
1903 struct symbol
*param
;
1906 char *ret_str
= NULL
;
1913 var
= chunk_to_var(expr
);
1918 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, param
) {
1924 snprintf(buf
, sizeof(buf
), "%s orig", param
->ident
->name
);
1925 compare
= get_comparison_strings(var
, buf
);
1928 if (show_special(compare
)[0] != starts_with
)
1930 snprintf(buf
, sizeof(buf
), "[%s$%d]", show_special(compare
), i
);
1931 ret_str
= alloc_sname(buf
);
1933 } END_FOR_EACH_PTR(param
);
1942 if (starts_with
== '<')
1943 ret_str
= get_mask_comparison(expr
, ignore
);
1947 char *name_sym_to_param_comparison(const char *name
, struct symbol
*sym
)
1949 struct symbol
*param
;
1955 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, param
) {
1959 snprintf(buf
, sizeof(buf
), "%s orig", param
->ident
->name
);
1960 compare
= get_comparison_strings(name
, buf
);
1963 snprintf(buf
, sizeof(buf
), "[%s$%d]", show_special(compare
), i
);
1964 return alloc_sname(buf
);
1965 } END_FOR_EACH_PTR(param
);
1970 char *expr_equal_to_param(struct expression
*expr
, int ignore
)
1972 return range_comparison_to_param_helper(expr
, '=', ignore
);
1975 char *expr_lte_to_param(struct expression
*expr
, int ignore
)
1977 return range_comparison_to_param_helper(expr
, '<', ignore
);
1980 char *expr_param_comparison(struct expression
*expr
, int ignore
)
1982 struct symbol
*param
;
1985 char *ret_str
= NULL
;
1989 var
= chunk_to_var(expr
);
1994 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, param
) {
2000 snprintf(buf
, sizeof(buf
), "%s orig", param
->ident
->name
);
2001 compare
= get_comparison_strings(var
, buf
);
2004 snprintf(buf
, sizeof(buf
), "[%s$%d]", show_special(compare
), i
);
2005 ret_str
= alloc_sname(buf
);
2007 } END_FOR_EACH_PTR(param
);
2014 char *get_printed_param_name(struct expression
*call
, const char *param_name
, struct symbol
*param_sym
)
2016 struct expression
*arg
;
2019 static char buf
[256];
2024 FOR_EACH_PTR(call
->args
, arg
) {
2027 name
= expr_to_var_sym(arg
, &sym
);
2030 if (sym
!= param_sym
)
2034 if (strncmp(name
, param_name
, len
) != 0)
2036 if (param_name
[len
] == '\0') {
2037 snprintf(buf
, sizeof(buf
), "$%d", i
);
2040 if (param_name
[len
] != '-')
2042 snprintf(buf
, sizeof(buf
), "$%d%s", i
, param_name
+ len
);
2044 } END_FOR_EACH_PTR(arg
);
2049 static void match_call_info(struct expression
*expr
)
2051 struct expression
*arg
;
2052 struct smatch_state
*state
;
2053 struct sm_state
*sm
;
2054 struct compare_data
*data
;
2056 struct string_list
*links
;
2058 const char *right_name
;
2064 FOR_EACH_PTR(expr
->args
, arg
) {
2067 state
= get_state_chunk(link_id
, arg
);
2071 links
= state
->data
;
2072 FOR_EACH_PTR(links
, link
) {
2073 struct var_sym_list
*right_vsl
;
2074 struct var_sym
*right_vs
;
2077 if (strstr(link
, " orig"))
2079 sm
= get_sm_state(compare_id
, link
, NULL
);
2082 data
= sm
->state
->data
;
2083 if (!data
|| !data
->comparison
)
2085 arg_name
= expr_to_var(arg
);
2090 if (strcmp(data
->left_var
, arg_name
) == 0) {
2091 comparison
= data
->comparison
;
2092 right_name
= data
->right_var
;
2093 right_vsl
= data
->right_vsl
;
2094 } else if (strcmp(data
->right_var
, arg_name
) == 0) {
2095 comparison
= flip_comparison(data
->comparison
);
2096 right_name
= data
->left_var
;
2097 right_vsl
= data
->left_vsl
;
2099 if (!right_vsl
|| ptr_list_size((struct ptr_list
*)right_vsl
) != 1)
2102 right_vs
= first_ptr_list((struct ptr_list
*)right_vsl
);
2103 if (strcmp(right_vs
->var
, right_name
) != 0)
2105 right_name
= get_printed_param_name(expr
, right_vs
->var
, right_vs
->sym
);
2108 snprintf(info_buf
, sizeof(info_buf
), "%s %s", show_special(comparison
), right_name
);
2109 sql_insert_caller_info(expr
, PARAM_COMPARE
, i
, "$", info_buf
);
2112 free_string(arg_name
);
2113 } END_FOR_EACH_PTR(link
);
2114 } END_FOR_EACH_PTR(arg
);
2117 static void struct_member_callback(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*link_sm
)
2119 struct sm_state
*compare_sm
;
2120 struct string_list
*links
;
2122 struct compare_data
*data
;
2123 struct var_sym
*left
, *right
;
2124 static char info_buf
[256];
2125 const char *right_name
;
2127 if (strstr(printed_name
, " orig"))
2130 links
= link_sm
->state
->data
;
2131 FOR_EACH_PTR(links
, link
) {
2132 compare_sm
= get_sm_state(compare_id
, link
, NULL
);
2135 data
= compare_sm
->state
->data
;
2136 if (!data
|| !data
->comparison
)
2139 if (ptr_list_size((struct ptr_list
*)data
->left_vsl
) != 1 ||
2140 ptr_list_size((struct ptr_list
*)data
->right_vsl
) != 1)
2142 left
= first_ptr_list((struct ptr_list
*)data
->left_vsl
);
2143 right
= first_ptr_list((struct ptr_list
*)data
->right_vsl
);
2144 if (left
->sym
== right
->sym
&&
2145 strcmp(left
->var
, right
->var
) == 0)
2148 * Both parameters link to this comparison so only
2149 * record the first one.
2151 if (left
->sym
!= link_sm
->sym
||
2152 strcmp(left
->var
, link_sm
->name
) != 0)
2155 right_name
= get_printed_param_name(call
, right
->var
, right
->sym
);
2158 snprintf(info_buf
, sizeof(info_buf
), "%s %s", show_special(data
->comparison
), right_name
);
2159 sql_insert_caller_info(call
, PARAM_COMPARE
, param
, printed_name
, info_buf
);
2160 } END_FOR_EACH_PTR(link
);
2163 static void print_return_value_comparison(int return_id
, char *return_ranges
, struct expression
*expr
)
2166 const char *tmp_name
;
2172 * TODO: This only prints == comparisons. That's probably the most
2173 * useful comparison because == max has lots of implications. But it
2174 * would be good to capture the rest as well.
2176 * This information is already in the DB but it's in the parameter math
2177 * bits and it's awkward to use it. This is is the simpler, possibly
2178 * cleaner way, but not necessarily the best, I don't know.
2183 name
= expr_to_var_sym(expr
, &sym
);
2187 param
= get_param_num_from_sym(sym
);
2190 if (param_was_set_var_sym(name
, sym
))
2193 tmp_name
= get_param_name_var_sym(name
, sym
);
2197 snprintf(info_buf
, sizeof(info_buf
), "== $%d%s", param
, tmp_name
+ 1);
2198 sql_insert_return_states(return_id
, return_ranges
,
2199 PARAM_COMPARE
, -1, "$", info_buf
);
2204 static void print_return_comparison(int return_id
, char *return_ranges
, struct expression
*expr
)
2206 struct sm_state
*tmp
;
2207 struct string_list
*links
;
2209 struct sm_state
*sm
;
2210 struct compare_data
*data
;
2211 struct var_sym
*left
, *right
;
2212 int left_param
, right_param
;
2214 char right_buf
[256];
2216 const char *tmp_name
;
2218 print_return_value_comparison(return_id
, return_ranges
, expr
);
2220 FOR_EACH_MY_SM(link_id
, __get_cur_stree(), tmp
) {
2221 if (get_param_num_from_sym(tmp
->sym
) < 0)
2223 links
= tmp
->state
->data
;
2224 FOR_EACH_PTR(links
, link
) {
2225 sm
= get_sm_state(compare_id
, link
, NULL
);
2228 data
= sm
->state
->data
;
2229 if (!data
|| !data
->comparison
)
2231 if (ptr_list_size((struct ptr_list
*)data
->left_vsl
) != 1 ||
2232 ptr_list_size((struct ptr_list
*)data
->right_vsl
) != 1)
2234 left
= first_ptr_list((struct ptr_list
*)data
->left_vsl
);
2235 right
= first_ptr_list((struct ptr_list
*)data
->right_vsl
);
2236 if (left
->sym
== right
->sym
&&
2237 strcmp(left
->var
, right
->var
) == 0)
2240 * Both parameters link to this comparison so only
2241 * record the first one.
2243 if (left
->sym
!= tmp
->sym
||
2244 strcmp(left
->var
, tmp
->name
) != 0)
2247 if (strstr(right
->var
, " orig"))
2250 left_param
= get_param_num_from_sym(left
->sym
);
2251 right_param
= get_param_num_from_sym(right
->sym
);
2252 if (left_param
< 0 || right_param
< 0)
2255 tmp_name
= get_param_name_var_sym(left
->var
, left
->sym
);
2258 snprintf(left_buf
, sizeof(left_buf
), "%s", tmp_name
);
2260 tmp_name
= get_param_name_var_sym(right
->var
, right
->sym
);
2261 if (!tmp_name
|| tmp_name
[0] != '$')
2263 snprintf(right_buf
, sizeof(right_buf
), "$%d%s", right_param
, tmp_name
+ 1);
2266 * FIXME: this should reject $ type variables (as
2267 * opposed to $->foo type). Those should come from
2268 * smatch_param_compare_limit.c.
2271 snprintf(info_buf
, sizeof(info_buf
), "%s %s", show_special(data
->comparison
), right_buf
);
2272 sql_insert_return_states(return_id
, return_ranges
,
2273 PARAM_COMPARE
, left_param
, left_buf
, info_buf
);
2274 } END_FOR_EACH_PTR(link
);
2276 } END_FOR_EACH_SM(tmp
);
2279 static int parse_comparison(char **value
, int *op
)
2287 if (**value
== '=') {
2295 *op
= SPECIAL_EQUAL
;
2300 *op
= SPECIAL_NOTEQUAL
;
2304 if (**value
== '=') {
2313 if (**value
!= ' ') {
2314 sm_perror("parsing comparison. %s", *value
);
2322 static int split_op_param_key(char *value
, int *op
, int *param
, char **key
)
2324 static char buf
[256];
2327 if (!parse_comparison(&value
, op
))
2330 snprintf(buf
, sizeof(buf
), "%s", value
);
2337 if (*param
< 0 || *param
> 99)
2349 static void db_return_comparison(struct expression
*expr
, int left_param
, char *key
, char *value
)
2351 struct expression
*left_arg
, *right_arg
;
2352 char *left_name
= NULL
;
2353 struct symbol
*left_sym
;
2354 char *right_name
= NULL
;
2355 struct symbol
*right_sym
;
2359 struct var_sym_list
*left_vsl
= NULL
, *right_vsl
= NULL
;
2361 if (left_param
== -1) {
2362 if (expr
->type
!= EXPR_ASSIGNMENT
)
2364 left_arg
= strip_expr(expr
->left
);
2366 while (expr
->type
== EXPR_ASSIGNMENT
)
2367 expr
= strip_expr(expr
->right
);
2368 if (expr
->type
!= EXPR_CALL
)
2371 while (expr
->type
== EXPR_ASSIGNMENT
)
2372 expr
= strip_expr(expr
->right
);
2373 if (expr
->type
!= EXPR_CALL
)
2376 left_arg
= get_argument_from_call_expr(expr
->args
, left_param
);
2381 if (!split_op_param_key(value
, &op
, &right_param
, &right_key
))
2384 right_arg
= get_argument_from_call_expr(expr
->args
, right_param
);
2388 left_name
= get_variable_from_key(left_arg
, key
, &left_sym
);
2389 if (!left_name
|| !left_sym
)
2392 right_name
= get_variable_from_key(right_arg
, right_key
, &right_sym
);
2393 if (!right_name
|| !right_sym
)
2396 add_var_sym(&left_vsl
, left_name
, left_sym
);
2397 add_var_sym(&right_vsl
, right_name
, right_sym
);
2399 add_comparison_var_sym(NULL
, left_name
, left_vsl
, op
, NULL
, right_name
, right_vsl
);
2402 free_string(left_name
);
2403 free_string(right_name
);
2406 int param_compare_limit_is_impossible(struct expression
*expr
, int left_param
, char *left_key
, char *value
)
2408 struct smatch_state
*state
;
2409 char *left_name
= NULL
;
2410 char *right_name
= NULL
;
2411 struct symbol
*left_sym
, *right_sym
;
2412 struct expression
*left_arg
, *right_arg
;
2419 while (expr
->type
== EXPR_ASSIGNMENT
)
2420 expr
= strip_expr(expr
->right
);
2421 if (expr
->type
!= EXPR_CALL
)
2424 if (!split_op_param_key(value
, &op
, &right_param
, &right_key
))
2427 left_arg
= get_argument_from_call_expr(expr
->args
, left_param
);
2431 right_arg
= get_argument_from_call_expr(expr
->args
, right_param
);
2435 left_name
= get_variable_from_key(left_arg
, left_key
, &left_sym
);
2436 right_name
= get_variable_from_key(right_arg
, right_key
, &right_sym
);
2437 if (!left_name
|| !right_name
)
2440 snprintf(buf
, sizeof(buf
), "%s vs %s", left_name
, right_name
);
2441 state
= get_state(compare_id
, buf
, NULL
);
2444 state_op
= state_to_comparison(state
);
2448 if (!filter_comparison(remove_unsigned_from_comparison(state_op
), op
))
2451 free_string(left_name
);
2452 free_string(right_name
);
2456 int impossibly_high_comparison(struct expression
*expr
)
2458 struct smatch_state
*link_state
;
2459 struct sm_state
*sm
;
2460 struct string_list
*links
;
2462 struct compare_data
*data
;
2464 link_state
= get_state_expr(link_id
, expr
);
2466 if (expr
->type
== EXPR_BINOP
&&
2467 (impossibly_high_comparison(expr
->left
) ||
2468 impossibly_high_comparison(expr
->right
)))
2473 links
= link_state
->data
;
2474 FOR_EACH_PTR(links
, link
) {
2475 sm
= get_sm_state(compare_id
, link
, NULL
);
2478 data
= sm
->state
->data
;
2481 if (!possibly_true(data
->left
, data
->comparison
, data
->right
))
2483 } END_FOR_EACH_PTR(link
);
2488 static void free_data(struct symbol
*sym
)
2492 clear_compare_data_alloc();
2495 void register_comparison(int id
)
2498 add_hook(&save_start_states
, AFTER_DEF_HOOK
);
2499 add_unmatched_state_hook(compare_id
, unmatched_comparison
);
2500 add_pre_merge_hook(compare_id
, &pre_merge_hook
);
2501 add_merge_hook(compare_id
, &merge_compare_states
);
2502 add_hook(&free_data
, AFTER_FUNC_HOOK
);
2503 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
2504 add_split_return_callback(&print_return_comparison
);
2506 select_return_states_hook(PARAM_COMPARE
, &db_return_comparison
);
2507 add_hook(&match_preop
, OP_HOOK
);
2510 void register_comparison_late(int id
)
2512 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
2515 void register_comparison_links(int id
)
2518 add_merge_hook(link_id
, &merge_links
);
2519 add_modification_hook(link_id
, &match_modify
);
2520 add_modification_hook_late(link_id
, match_inc_dec
);
2522 add_member_info_callback(link_id
, struct_member_callback
);
2525 void register_comparison_inc_dec(int id
)
2528 add_modification_hook_late(inc_dec_id
, &iter_modify
);
2531 void register_comparison_inc_dec_links(int id
)
2533 inc_dec_link_id
= id
;
2534 set_up_link_functions(inc_dec_id
, inc_dec_link_id
);
2537 static void filter_by_sm(struct sm_state
*sm
, int op
,
2538 struct state_list
**true_stack
,
2539 struct state_list
**false_stack
)
2541 struct compare_data
*data
;
2547 data
= sm
->state
->data
;
2550 filter_by_sm(sm
->left
, op
, true_stack
, false_stack
);
2551 filter_by_sm(sm
->right
, op
, true_stack
, false_stack
);
2556 if (data
->comparison
&&
2557 data
->comparison
== filter_comparison(data
->comparison
, op
))
2560 if (data
->comparison
&&
2561 data
->comparison
== filter_comparison(data
->comparison
, negate_comparison(op
)))
2565 add_ptr_list(true_stack
, sm
);
2567 add_ptr_list(false_stack
, sm
);
2570 filter_by_sm(sm
->left
, op
, true_stack
, false_stack
);
2571 filter_by_sm(sm
->right
, op
, true_stack
, false_stack
);
2575 struct sm_state
*comparison_implication_hook(struct expression
*expr
,
2576 struct state_list
**true_stack
,
2577 struct state_list
**false_stack
)
2579 struct sm_state
*sm
;
2582 static char buf
[256];
2584 if (expr
->type
!= EXPR_COMPARE
)
2589 left
= expr_to_var(expr
->left
);
2590 right
= expr_to_var(expr
->right
);
2591 if (!left
|| !right
) {
2597 if (strcmp(left
, right
) > 0) {
2602 op
= flip_comparison(op
);
2605 snprintf(buf
, sizeof(buf
), "%s vs %s", left
, right
);
2606 sm
= get_sm_state(compare_id
, buf
, NULL
);
2612 filter_by_sm(sm
, op
, true_stack
, false_stack
);
2613 if (!*true_stack
&& !*false_stack
)
2617 sm_msg("implications from comparison: (%s)", show_sm(sm
));