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_msg("internal smatch bug. 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_msg("internal smatch bug. 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
;
160 if (!left_rl
|| !right_rl
)
163 left_min
= rl_min(left_rl
);
164 left_max
= rl_max(left_rl
);
165 right_min
= rl_min(right_rl
);
166 right_max
= rl_max(right_rl
);
168 if (left_min
.value
== left_max
.value
&&
169 right_min
.value
== right_max
.value
&&
170 left_min
.value
== right_min
.value
)
171 return SPECIAL_EQUAL
;
173 if (sval_cmp(left_max
, right_min
) < 0)
175 if (sval_cmp(left_max
, right_min
) == 0)
177 if (sval_cmp(left_min
, right_max
) > 0)
179 if (sval_cmp(left_min
, right_max
) == 0)
185 static int comparison_from_extra(struct expression
*a
, struct expression
*b
)
187 struct range_list
*left
, *right
;
189 if (!get_implied_rl(a
, &left
))
191 if (!get_implied_rl(b
, &right
))
194 return rl_comparison(left
, right
);
197 static struct range_list
*get_orig_rl(struct var_sym_list
*vsl
)
200 struct smatch_state
*state
;
204 sym
= vsl_to_sym(vsl
);
205 if (!sym
|| !sym
->ident
)
207 state
= get_orig_estate(sym
->ident
->name
, sym
);
208 return estate_rl(state
);
211 static struct smatch_state
*unmatched_comparison(struct sm_state
*sm
)
213 struct compare_data
*data
= sm
->state
->data
;
214 struct range_list
*left_rl
, *right_rl
;
220 if (strstr(data
->left_var
, " orig"))
221 left_rl
= get_orig_rl(data
->left_vsl
);
222 else if (!get_implied_rl_var_sym(data
->left_var
, vsl_to_sym(data
->left_vsl
), &left_rl
))
225 if (strstr(data
->right_var
, " orig"))
226 right_rl
= get_orig_rl(data
->right_vsl
);
227 else if (!get_implied_rl_var_sym(data
->right_var
, vsl_to_sym(data
->right_vsl
), &right_rl
))
230 op
= rl_comparison(left_rl
, right_rl
);
232 return alloc_compare_state(
233 data
->left
, data
->left_var
, data
->left_vsl
,
235 data
->right
, data
->right_var
, data
->right_vsl
);
240 /* remove_unsigned_from_comparison() is obviously a hack. */
241 int remove_unsigned_from_comparison(int op
)
244 case SPECIAL_UNSIGNED_LT
:
246 case SPECIAL_UNSIGNED_LTE
:
248 case SPECIAL_UNSIGNED_GTE
:
250 case SPECIAL_UNSIGNED_GT
:
258 * This is for when you merge states "a < b" and "a == b", the result is that
259 * we can say for sure, "a <= b" after the merge.
261 int merge_comparisons(int one
, int two
)
268 one
= remove_unsigned_from_comparison(one
);
269 two
= remove_unsigned_from_comparison(two
);
319 return SPECIAL_NOTEQUAL
;
330 * This is for if you have "a < b" and "b <= c". Or in other words,
331 * "a < b <= c". You would call this like get_combined_comparison('<', '<=').
332 * The return comparison would be '<'.
334 * This function is different from merge_comparisons(), for example:
335 * merge_comparison('<', '==') returns '<='
336 * get_combined_comparison('<', '==') returns '<'
338 int combine_comparisons(int left_compare
, int right_compare
)
342 left_compare
= remove_unsigned_from_comparison(left_compare
);
343 right_compare
= remove_unsigned_from_comparison(right_compare
);
347 switch (left_compare
) {
356 return right_compare
;
365 switch (right_compare
) {
397 int filter_comparison(int orig
, int op
)
409 case SPECIAL_NOTEQUAL
:
419 case SPECIAL_NOTEQUAL
:
428 case SPECIAL_UNSIGNED_LTE
:
429 case SPECIAL_UNSIGNED_GTE
:
430 return SPECIAL_EQUAL
;
433 case SPECIAL_NOTEQUAL
:
438 case SPECIAL_UNSIGNED_LT
:
439 case SPECIAL_UNSIGNED_LTE
:
440 return SPECIAL_UNSIGNED_LT
;
441 case SPECIAL_NOTEQUAL
:
446 case SPECIAL_UNSIGNED_GT
:
447 case SPECIAL_UNSIGNED_GTE
:
448 return SPECIAL_UNSIGNED_GT
;
454 return SPECIAL_EQUAL
;
459 case SPECIAL_NOTEQUAL
:
467 case SPECIAL_NOTEQUAL
:
471 case SPECIAL_UNSIGNED_LT
:
473 case SPECIAL_UNSIGNED_LT
:
474 case SPECIAL_UNSIGNED_LTE
:
475 case SPECIAL_NOTEQUAL
:
476 return SPECIAL_UNSIGNED_LT
;
479 case SPECIAL_UNSIGNED_LTE
:
481 case SPECIAL_UNSIGNED_LT
:
482 case SPECIAL_UNSIGNED_LTE
:
485 case SPECIAL_NOTEQUAL
:
486 return SPECIAL_UNSIGNED_LT
;
487 case SPECIAL_UNSIGNED_GTE
:
488 return SPECIAL_EQUAL
;
491 case SPECIAL_UNSIGNED_GTE
:
493 case SPECIAL_UNSIGNED_LTE
:
494 return SPECIAL_EQUAL
;
495 case SPECIAL_NOTEQUAL
:
496 return SPECIAL_UNSIGNED_GT
;
498 case SPECIAL_UNSIGNED_GTE
:
499 case SPECIAL_UNSIGNED_GT
:
503 case SPECIAL_UNSIGNED_GT
:
505 case SPECIAL_UNSIGNED_GT
:
506 case SPECIAL_UNSIGNED_GTE
:
507 case SPECIAL_NOTEQUAL
:
508 return SPECIAL_UNSIGNED_GT
;
515 struct smatch_state
*merge_compare_states(struct smatch_state
*s1
, struct smatch_state
*s2
)
517 struct compare_data
*data
= s1
->data
;
520 op
= merge_comparisons(state_to_comparison(s1
), state_to_comparison(s2
));
522 return alloc_compare_state(
523 data
->left
, data
->left_var
, data
->left_vsl
,
525 data
->right
, data
->right_var
, data
->right_vsl
);
529 static struct smatch_state
*alloc_link_state(struct string_list
*links
)
531 struct smatch_state
*state
;
532 static char buf
[256];
536 state
= __alloc_smatch_state(0);
539 FOR_EACH_PTR(links
, tmp
) {
541 snprintf(buf
, sizeof(buf
), "%s", tmp
);
543 append(buf
, ", ", sizeof(buf
));
544 append(buf
, tmp
, sizeof(buf
));
546 } END_FOR_EACH_PTR(tmp
);
548 state
->name
= alloc_sname(buf
);
553 static void save_start_states(struct statement
*stmt
)
555 struct symbol
*param
;
557 char state_name
[128];
558 struct smatch_state
*state
;
559 struct string_list
*links
;
562 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, param
) {
563 struct var_sym_list
*left_vsl
= NULL
;
564 struct var_sym_list
*right_vsl
= NULL
;
568 snprintf(orig
, sizeof(orig
), "%s orig", param
->ident
->name
);
569 snprintf(state_name
, sizeof(state_name
), "%s vs %s", param
->ident
->name
, orig
);
570 add_var_sym(&left_vsl
, param
->ident
->name
, param
);
571 add_var_sym(&right_vsl
, orig
, param
);
572 state
= alloc_compare_state(
573 NULL
, param
->ident
->name
, left_vsl
,
575 NULL
, alloc_sname(orig
), right_vsl
);
576 set_state(compare_id
, state_name
, NULL
, state
);
578 link
= alloc_sname(state_name
);
580 insert_string(&links
, link
);
581 state
= alloc_link_state(links
);
582 set_state(link_id
, param
->ident
->name
, param
, state
);
583 } END_FOR_EACH_PTR(param
);
586 static struct smatch_state
*merge_links(struct smatch_state
*s1
, struct smatch_state
*s2
)
588 struct smatch_state
*ret
;
589 struct string_list
*links
;
591 links
= combine_string_lists(s1
->data
, s2
->data
);
592 ret
= alloc_link_state(links
);
596 static void save_link_var_sym(const char *var
, struct symbol
*sym
, const char *link
)
598 struct smatch_state
*old_state
, *new_state
;
599 struct string_list
*links
;
602 old_state
= get_state(link_id
, var
, sym
);
604 links
= clone_str_list(old_state
->data
);
608 new = alloc_sname(link
);
609 insert_string(&links
, new);
611 new_state
= alloc_link_state(links
);
612 set_state(link_id
, var
, sym
, new_state
);
615 static void match_inc(struct sm_state
*sm
)
617 struct string_list
*links
;
618 struct smatch_state
*state
, *new;
619 struct compare_data
*data
;
624 links
= sm
->state
->data
;
626 FOR_EACH_PTR(links
, tmp
) {
627 state
= get_state(compare_id
, tmp
, NULL
);
635 if (strncmp(sm
->name
, tmp
, strlen(sm
->name
)) != 0 ||
636 tmp
[strlen(sm
->name
)] != ' ')
639 op
= state_to_comparison(state
);
641 switch (flip
? flip_comparison(op
) : op
) {
644 case SPECIAL_UNSIGNED_GTE
:
646 case SPECIAL_UNSIGNED_GT
:
647 new = alloc_compare_state(
648 data
->left
, data
->left_var
, data
->left_vsl
,
650 data
->right
, data
->right_var
, data
->right_vsl
);
651 set_state(compare_id
, tmp
, NULL
, new);
654 case SPECIAL_UNSIGNED_LT
:
655 new = alloc_compare_state(
656 data
->left
, data
->left_var
, data
->left_vsl
,
657 flip
? SPECIAL_GTE
: SPECIAL_LTE
,
658 data
->right
, data
->right_var
, data
->right_vsl
);
659 set_state(compare_id
, tmp
, NULL
, new);
662 set_state(compare_id
, tmp
, NULL
, &undefined
);
664 } END_FOR_EACH_PTR(tmp
);
667 static void match_dec(struct sm_state
*sm
)
669 struct string_list
*links
;
670 struct smatch_state
*state
;
673 links
= sm
->state
->data
;
675 FOR_EACH_PTR(links
, tmp
) {
676 state
= get_state(compare_id
, tmp
, NULL
);
678 switch (state_to_comparison(state
)) {
681 case SPECIAL_UNSIGNED_LTE
:
683 case SPECIAL_UNSIGNED_LT
: {
684 struct compare_data
*data
= state
->data
;
685 struct smatch_state
*new;
687 new = alloc_compare_state(
688 data
->left
, data
->left_var
, data
->left_vsl
,
690 data
->right
, data
->right_var
, data
->right_vsl
);
691 set_state(compare_id
, tmp
, NULL
, new);
695 set_state(compare_id
, tmp
, NULL
, &undefined
);
697 } END_FOR_EACH_PTR(tmp
);
700 static void match_inc_dec(struct sm_state
*sm
, struct expression
*mod_expr
)
703 * if (foo > bar) then ++foo is also > bar.
707 if (mod_expr
->type
!= EXPR_PREOP
&& mod_expr
->type
!= EXPR_POSTOP
)
710 if (mod_expr
->op
== SPECIAL_INCREMENT
)
712 else if (mod_expr
->op
== SPECIAL_DECREMENT
)
716 static void match_modify(struct sm_state
*sm
, struct expression
*mod_expr
)
718 struct string_list
*links
;
721 /* handled by match_inc_dec() */
723 ((mod_expr
->type
== EXPR_PREOP
|| mod_expr
->type
== EXPR_POSTOP
) &&
724 (mod_expr
->op
== SPECIAL_INCREMENT
|| mod_expr
->op
== SPECIAL_DECREMENT
)))
727 links
= sm
->state
->data
;
729 FOR_EACH_PTR(links
, tmp
) {
730 set_state(compare_id
, tmp
, NULL
, &undefined
);
731 } END_FOR_EACH_PTR(tmp
);
732 set_state(link_id
, sm
->name
, sm
->sym
, &undefined
);
735 static void match_preop(struct expression
*expr
)
737 struct expression
*parent
;
738 struct range_list
*left
, *right
;
742 * This is an important special case. Say you have:
746 * Assume that we know the range of limit is higher than the start
747 * value for "j". Then the first thing that we process is the ++j. We
748 * have not comparison states set up so it doesn't get caught by the
749 * modification hook. But it does get caught by smatch_extra which sets
750 * j to unknown then we parse the "j == limit" and sets false to != but
751 * really we want false to be <.
753 * So what we do is we set j < limit here, then the match_modify catches
754 * it and we do a match_inc_dec().
758 if (expr
->type
!= EXPR_PREOP
||
759 (expr
->op
!= SPECIAL_INCREMENT
&& expr
->op
!= SPECIAL_DECREMENT
))
762 parent
= expr_get_parent_expr(expr
);
765 if (parent
->type
!= EXPR_COMPARE
|| parent
->op
!= SPECIAL_EQUAL
)
767 if (parent
->left
!= expr
)
770 if (!get_implied_rl(expr
->unop
, &left
) ||
771 !get_implied_rl(parent
->right
, &right
))
774 op
= rl_comparison(left
, right
);
778 add_comparison(expr
->unop
, op
, parent
->right
);
781 static char *chunk_to_var_sym(struct expression
*expr
, struct symbol
**sym
)
783 expr
= strip_expr(expr
);
789 if (expr
->type
== EXPR_PREOP
&&
790 (expr
->op
== SPECIAL_INCREMENT
||
791 expr
->op
== SPECIAL_DECREMENT
))
792 expr
= strip_expr(expr
->unop
);
794 if (expr
->type
== EXPR_CALL
) {
797 snprintf(buf
, sizeof(buf
), "return %p", expr
);
798 return alloc_string(buf
);
801 return expr_to_chunk_sym_vsl(expr
, sym
, NULL
);
804 static char *chunk_to_var(struct expression
*expr
)
806 return chunk_to_var_sym(expr
, NULL
);
809 static struct smatch_state
*get_state_chunk(int owner
, struct expression
*expr
)
813 struct smatch_state
*ret
;
815 name
= chunk_to_var_sym(expr
, &sym
);
819 ret
= get_state(owner
, name
, sym
);
824 static void save_link(struct expression
*expr
, char *link
)
829 expr
= strip_expr(expr
);
830 if (expr
->type
== EXPR_BINOP
) {
833 chunk
= chunk_to_var(expr
);
837 save_link(expr
->left
, link
);
838 save_link(expr
->right
, link
);
839 save_link_var_sym(chunk
, NULL
, link
);
843 var
= chunk_to_var_sym(expr
, &sym
);
847 save_link_var_sym(var
, sym
, link
);
851 static int get_orig_comparison(struct stree
*pre_stree
, const char *left
, const char *right
)
853 struct smatch_state
*state
;
854 struct compare_data
*data
;
856 char state_name
[256];
858 if (strcmp(left
, right
) > 0) {
859 const char *tmp
= right
;
866 snprintf(state_name
, sizeof(state_name
), "%s vs %s", left
, right
);
867 state
= get_state_stree(pre_stree
, compare_id
, state_name
, NULL
);
868 if (!state
|| !state
->data
)
872 return flip_comparison(data
->comparison
);
873 return data
->comparison
;
877 static int have_common_var_sym(struct var_sym_list
*left_vsl
, struct var_sym_list
*right_vsl
)
881 FOR_EACH_PTR(left_vsl
, tmp
) {
882 if (in_var_sym_list(right_vsl
, tmp
->var
, tmp
->sym
))
884 } END_FOR_EACH_PTR(tmp
);
890 * The idea here is that we take a comparison "a < b" and then we look at all
891 * the things which "b" is compared against "b < c" and we say that that implies
892 * a relationship "a < c".
894 * The names here about because the comparisons are organized like this
898 static void update_tf_links(struct stree
*pre_stree
,
899 struct expression
*left_expr
,
900 const char *left_var
, struct var_sym_list
*left_vsl
,
901 int left_comparison
, int left_false_comparison
,
902 const char *mid_var
, struct var_sym_list
*mid_vsl
,
903 struct string_list
*links
)
905 struct smatch_state
*state
;
906 struct smatch_state
*true_state
, *false_state
;
907 struct compare_data
*data
;
908 struct expression
*right_expr
;
909 const char *right_var
;
910 struct var_sym_list
*right_vsl
;
912 int right_comparison
;
914 int false_comparison
;
916 char state_name
[256];
919 FOR_EACH_PTR(links
, tmp
) {
920 state
= get_state_stree(pre_stree
, compare_id
, tmp
, NULL
);
921 if (!state
|| !state
->data
)
924 right_comparison
= data
->comparison
;
925 right_expr
= data
->right
;
926 right_var
= data
->right_var
;
927 right_vsl
= data
->right_vsl
;
928 if (strcmp(mid_var
, right_var
) == 0) {
929 right_expr
= data
->left
;
930 right_var
= data
->left_var
;
931 right_vsl
= data
->left_vsl
;
932 right_comparison
= flip_comparison(right_comparison
);
934 if (have_common_var_sym(left_vsl
, right_vsl
))
937 orig_comparison
= get_orig_comparison(pre_stree
, left_var
, right_var
);
939 true_comparison
= combine_comparisons(left_comparison
, right_comparison
);
940 false_comparison
= combine_comparisons(left_false_comparison
, right_comparison
);
942 true_comparison
= filter_comparison(orig_comparison
, true_comparison
);
943 false_comparison
= filter_comparison(orig_comparison
, false_comparison
);
945 if (strcmp(left_var
, right_var
) > 0) {
946 struct expression
*tmp_expr
= left_expr
;
947 const char *tmp_var
= left_var
;
948 struct var_sym_list
*tmp_vsl
= left_vsl
;
950 left_expr
= right_expr
;
951 left_var
= right_var
;
952 left_vsl
= right_vsl
;
953 right_expr
= tmp_expr
;
956 true_comparison
= flip_comparison(true_comparison
);
957 false_comparison
= flip_comparison(false_comparison
);
960 if (!true_comparison
&& !false_comparison
)
964 true_state
= alloc_compare_state(
965 left_expr
, left_var
, left_vsl
,
967 right_expr
, right_var
, right_vsl
);
970 if (false_comparison
)
971 false_state
= alloc_compare_state(
972 left_expr
, left_var
, left_vsl
,
974 right_expr
, right_var
, right_vsl
);
978 snprintf(state_name
, sizeof(state_name
), "%s vs %s", left_var
, right_var
);
979 set_true_false_states(compare_id
, state_name
, NULL
, true_state
, false_state
);
980 FOR_EACH_PTR(left_vsl
, vs
) {
981 save_link_var_sym(vs
->var
, vs
->sym
, state_name
);
982 } END_FOR_EACH_PTR(vs
);
983 FOR_EACH_PTR(right_vsl
, vs
) {
984 save_link_var_sym(vs
->var
, vs
->sym
, state_name
);
985 } END_FOR_EACH_PTR(vs
);
986 if (!vsl_to_sym(left_vsl
))
987 save_link_var_sym(left_var
, NULL
, state_name
);
988 if (!vsl_to_sym(right_vsl
))
989 save_link_var_sym(right_var
, NULL
, state_name
);
990 } END_FOR_EACH_PTR(tmp
);
993 static void update_tf_data(struct stree
*pre_stree
,
994 struct expression
*left_expr
,
995 const char *left_name
, struct var_sym_list
*left_vsl
,
996 struct expression
*right_expr
,
997 const char *right_name
, struct var_sym_list
*right_vsl
,
998 int true_comparison
, int false_comparison
)
1000 struct smatch_state
*state
;
1002 state
= get_state_stree(pre_stree
, link_id
, right_name
, vsl_to_sym(right_vsl
));
1004 update_tf_links(pre_stree
, left_expr
, left_name
, left_vsl
, true_comparison
, false_comparison
, right_name
, right_vsl
, state
->data
);
1006 state
= get_state_stree(pre_stree
, link_id
, left_name
, vsl_to_sym(left_vsl
));
1008 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
);
1011 static void iter_modify(struct sm_state
*sm
, struct expression
*mod_expr
)
1013 if (sm
->state
!= &start
||
1015 (mod_expr
->type
!= EXPR_PREOP
&& mod_expr
->type
!= EXPR_POSTOP
) ||
1016 mod_expr
->op
!= SPECIAL_INCREMENT
)
1017 set_state(inc_dec_id
, sm
->name
, sm
->sym
, &undefined
);
1019 set_state(inc_dec_id
, sm
->name
, sm
->sym
, &incremented
);
1022 static void handle_for_loops(struct expression
*expr
, char *state_name
, struct smatch_state
*false_state
)
1025 char *iter_name
, *cap_name
;
1026 struct symbol
*iter_sym
, *cap_sym
;
1027 struct compare_data
*data
;
1029 if (expr
->op
!= '<' && expr
->op
!= SPECIAL_UNSIGNED_LT
)
1032 if (!__cur_stmt
|| !__prev_stmt
)
1034 if (__cur_stmt
->type
!= STMT_ITERATOR
)
1036 if (__cur_stmt
->iterator_pre_condition
!= expr
)
1039 /* literals are handled in smatch_extra.c */
1040 if (get_value(expr
->right
, &sval
))
1043 /* First time checking the condition */
1044 if (__prev_stmt
== __cur_stmt
->iterator_pre_statement
) {
1045 if (!get_implied_value(expr
->left
, &sval
) ||
1049 iter_name
= expr_to_var_sym(expr
->left
, &iter_sym
);
1050 cap_name
= expr_to_var_sym(expr
->right
, &cap_sym
);
1051 if (!iter_name
|| !cap_name
|| !iter_sym
|| !cap_sym
) {
1052 free_string(iter_name
);
1053 free_string(cap_name
);
1057 set_state(inc_dec_id
, iter_name
, iter_sym
, &start
);
1058 store_link(inc_dec_link_id
, cap_name
, cap_sym
, iter_name
, iter_sym
);
1060 free_string(iter_name
);
1061 free_string(cap_name
);
1065 /* Second time checking the condtion */
1066 if (__prev_stmt
!= __cur_stmt
->iterator_post_statement
)
1069 if (get_state_chunk(inc_dec_id
, expr
->left
) != &incremented
)
1072 data
= false_state
->data
;
1073 false_state
= alloc_compare_state(
1074 data
->left
, data
->left_var
, data
->left_vsl
,
1076 data
->right
, data
->right_var
, data
->right_vsl
);
1078 set_true_false_states(compare_id
, state_name
, NULL
, NULL
, false_state
);
1081 static int is_plus_one(struct expression
*expr
)
1085 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '+')
1087 if (!get_implied_value(expr
->right
, &sval
) || sval
.value
!= 1)
1092 static int is_minus_one(struct expression
*expr
)
1096 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '-')
1098 if (!get_implied_value(expr
->right
, &sval
) || sval
.value
!= 1)
1103 static void move_plus_to_minus_helper(struct expression
**left_p
, struct expression
**right_p
)
1105 struct expression
*left
= *left_p
;
1106 struct expression
*right
= *right_p
;
1109 * These two are basically equivalent: "foo + 1 != bar" and
1110 * "foo != bar - 1". There are issues with signedness and integer
1111 * overflows. There are also issues with type as well. But let's
1112 * pretend we can ignore all that stuff for now.
1116 if (!is_plus_one(left
))
1119 *left_p
= left
->left
;
1120 *right_p
= binop_expression(right
, '-', left
->right
);
1123 static void move_plus_to_minus(struct expression
**left_p
, struct expression
**right_p
)
1125 if (is_plus_one(*left_p
) && is_plus_one(*right_p
))
1128 move_plus_to_minus_helper(left_p
, right_p
);
1129 move_plus_to_minus_helper(right_p
, left_p
);
1132 static void handle_comparison(struct expression
*left_expr
, int op
, struct expression
*right_expr
, char **_state_name
, struct smatch_state
**_false_state
)
1136 struct symbol
*left_sym
, *right_sym
;
1137 struct var_sym_list
*left_vsl
= NULL
;
1138 struct var_sym_list
*right_vsl
= NULL
;
1140 int orig_comparison
;
1141 struct smatch_state
*true_state
, *false_state
;
1142 static char state_name
[256];
1143 struct stree
*pre_stree
;
1146 left_expr
= strip_parens(left_expr
);
1147 right_expr
= strip_parens(right_expr
);
1149 false_op
= negate_comparison(op
);
1151 move_plus_to_minus(&left_expr
, &right_expr
);
1153 if (op
== SPECIAL_UNSIGNED_LT
&&
1154 get_implied_value(left_expr
, &sval
) &&
1156 false_op
= SPECIAL_EQUAL
;
1158 if (op
== SPECIAL_UNSIGNED_GT
&&
1159 get_implied_value(right_expr
, &sval
) &&
1161 false_op
= SPECIAL_EQUAL
;
1163 left
= chunk_to_var_sym(left_expr
, &left_sym
);
1167 add_var_sym(&left_vsl
, left
, left_sym
);
1169 left_vsl
= expr_to_vsl(left_expr
);
1170 right
= chunk_to_var_sym(right_expr
, &right_sym
);
1174 add_var_sym(&right_vsl
, right
, right_sym
);
1176 right_vsl
= expr_to_vsl(right_expr
);
1178 if (strcmp(left
, right
) > 0) {
1179 char *tmp_name
= left
;
1180 struct var_sym_list
*tmp_vsl
= left_vsl
;
1181 struct expression
*tmp_expr
= left_expr
;
1184 left_vsl
= right_vsl
;
1185 left_expr
= right_expr
;
1187 right_vsl
= tmp_vsl
;
1188 right_expr
= tmp_expr
;
1189 op
= flip_comparison(op
);
1190 false_op
= flip_comparison(false_op
);
1193 orig_comparison
= get_comparison(left_expr
, right_expr
);
1194 op
= filter_comparison(orig_comparison
, op
);
1195 false_op
= filter_comparison(orig_comparison
, false_op
);
1197 snprintf(state_name
, sizeof(state_name
), "%s vs %s", left
, right
);
1198 true_state
= alloc_compare_state(
1199 left_expr
, left
, left_vsl
,
1201 right_expr
, right
, right_vsl
);
1202 false_state
= alloc_compare_state(
1203 left_expr
, left
, left_vsl
,
1205 right_expr
, right
, right_vsl
);
1207 pre_stree
= clone_stree(__get_cur_stree());
1208 update_tf_data(pre_stree
, left_expr
, left
, left_vsl
, right_expr
, right
, right_vsl
, op
, false_op
);
1209 free_stree(&pre_stree
);
1211 set_true_false_states(compare_id
, state_name
, NULL
, true_state
, false_state
);
1212 __compare_param_limit_hook(left_expr
, right_expr
, state_name
, true_state
, false_state
);
1213 save_link(left_expr
, state_name
);
1214 save_link(right_expr
, state_name
);
1217 *_false_state
= false_state
;
1219 *_state_name
= state_name
;
1226 void __comparison_match_condition(struct expression
*expr
)
1228 struct expression
*left
, *right
, *new_left
, *new_right
, *tmp
;
1229 struct smatch_state
*false_state
= NULL
;
1230 char *state_name
= NULL
;
1233 if (expr
->type
!= EXPR_COMPARE
)
1236 handle_comparison(expr
->left
, expr
->op
, expr
->right
, &state_name
, &false_state
);
1237 if (false_state
&& state_name
)
1238 handle_for_loops(expr
, state_name
, false_state
);
1240 left
= strip_parens(expr
->left
);
1241 right
= strip_parens(expr
->right
);
1243 if (left
->type
== EXPR_BINOP
&& left
->op
== '+') {
1244 new_left
= left
->left
;
1245 new_right
= binop_expression(right
, '-', left
->right
);
1246 handle_comparison(new_left
, expr
->op
, new_right
, NULL
, NULL
);
1248 new_left
= left
->right
;
1249 new_right
= binop_expression(right
, '-', left
->left
);
1250 handle_comparison(new_left
, expr
->op
, new_right
, NULL
, NULL
);
1255 left
= strip_parens(expr
->left
);
1256 right
= strip_parens(expr
->right
);
1257 if (get_last_expr_from_expression_stmt(expr
->left
)) {
1258 left
= get_last_expr_from_expression_stmt(expr
->left
);
1261 if (get_last_expr_from_expression_stmt(expr
->right
)) {
1262 right
= get_last_expr_from_expression_stmt(expr
->right
);
1270 while ((tmp
= get_assigned_expr(left
))) {
1273 left
= strip_expr(tmp
);
1276 while ((tmp
= get_assigned_expr(right
))) {
1279 right
= strip_expr(tmp
);
1282 handle_comparison(left
, expr
->op
, right
, NULL
, NULL
);
1285 static void add_comparison_var_sym(
1286 struct expression
*left_expr
,
1287 const char *left_name
, struct var_sym_list
*left_vsl
,
1289 struct expression
*right_expr
,
1290 const char *right_name
, struct var_sym_list
*right_vsl
)
1292 struct smatch_state
*state
;
1294 char state_name
[256];
1296 if (strcmp(left_name
, right_name
) > 0) {
1297 struct expression
*tmp_expr
= left_expr
;
1298 const char *tmp_name
= left_name
;
1299 struct var_sym_list
*tmp_vsl
= left_vsl
;
1301 left_expr
= right_expr
;
1302 left_name
= right_name
;
1303 left_vsl
= right_vsl
;
1304 right_expr
= tmp_expr
;
1305 right_name
= tmp_name
;
1306 right_vsl
= tmp_vsl
;
1307 comparison
= flip_comparison(comparison
);
1309 snprintf(state_name
, sizeof(state_name
), "%s vs %s", left_name
, right_name
);
1310 state
= alloc_compare_state(
1311 left_expr
, left_name
, left_vsl
,
1313 right_expr
, right_name
, right_vsl
);
1315 set_state(compare_id
, state_name
, NULL
, state
);
1317 FOR_EACH_PTR(left_vsl
, vs
) {
1318 save_link_var_sym(vs
->var
, vs
->sym
, state_name
);
1319 } END_FOR_EACH_PTR(vs
);
1320 FOR_EACH_PTR(right_vsl
, vs
) {
1321 save_link_var_sym(vs
->var
, vs
->sym
, state_name
);
1322 } END_FOR_EACH_PTR(vs
);
1325 static void add_comparison(struct expression
*left
, int comparison
, struct expression
*right
)
1327 char *left_name
= NULL
;
1328 char *right_name
= NULL
;
1329 struct symbol
*left_sym
, *right_sym
;
1330 struct var_sym_list
*left_vsl
, *right_vsl
;
1331 struct smatch_state
*state
;
1332 char state_name
[256];
1334 left_name
= chunk_to_var_sym(left
, &left_sym
);
1337 left_vsl
= expr_to_vsl(left
);
1338 right_name
= chunk_to_var_sym(right
, &right_sym
);
1341 right_vsl
= expr_to_vsl(right
);
1343 if (strcmp(left_name
, right_name
) > 0) {
1344 struct expression
*tmp_expr
= left
;
1345 struct symbol
*tmp_sym
= left_sym
;
1346 char *tmp_name
= left_name
;
1347 struct var_sym_list
*tmp_vsl
= left_vsl
;
1350 left_name
= right_name
;
1351 left_sym
= right_sym
;
1352 left_vsl
= right_vsl
;
1354 right_name
= tmp_name
;
1355 right_sym
= tmp_sym
;
1356 right_vsl
= tmp_vsl
;
1357 comparison
= flip_comparison(comparison
);
1359 snprintf(state_name
, sizeof(state_name
), "%s vs %s", left_name
, right_name
);
1360 state
= alloc_compare_state(
1361 left
, left_name
, left_vsl
,
1363 right
, right_name
, right_vsl
);
1365 set_state(compare_id
, state_name
, NULL
, state
);
1366 save_link(left
, state_name
);
1367 save_link(right
, state_name
);
1370 free_string(left_name
);
1371 free_string(right_name
);
1374 static void match_assign_add(struct expression
*expr
)
1376 struct expression
*right
;
1377 struct expression
*r_left
, *r_right
;
1378 sval_t left_tmp
, right_tmp
;
1380 right
= strip_expr(expr
->right
);
1381 r_left
= strip_expr(right
->left
);
1382 r_right
= strip_expr(right
->right
);
1384 get_absolute_min(r_left
, &left_tmp
);
1385 get_absolute_min(r_right
, &right_tmp
);
1387 if (left_tmp
.value
> 0)
1388 add_comparison(expr
->left
, '>', r_right
);
1389 else if (left_tmp
.value
== 0)
1390 add_comparison(expr
->left
, SPECIAL_GTE
, r_right
);
1392 if (right_tmp
.value
> 0)
1393 add_comparison(expr
->left
, '>', r_left
);
1394 else if (right_tmp
.value
== 0)
1395 add_comparison(expr
->left
, SPECIAL_GTE
, r_left
);
1398 static void match_assign_sub(struct expression
*expr
)
1400 struct expression
*right
;
1401 struct expression
*r_left
, *r_right
;
1405 right
= strip_expr(expr
->right
);
1406 r_left
= strip_expr(right
->left
);
1407 r_right
= strip_expr(right
->right
);
1409 if (get_absolute_min(r_right
, &min
) && sval_is_negative(min
))
1412 comparison
= get_comparison(r_left
, r_right
);
1414 switch (comparison
) {
1417 if (implied_not_equal(r_right
, 0))
1418 add_comparison(expr
->left
, '>', r_left
);
1420 add_comparison(expr
->left
, SPECIAL_GTE
, r_left
);
1425 static void match_assign_divide(struct expression
*expr
)
1427 struct expression
*right
;
1428 struct expression
*r_left
, *r_right
;
1431 right
= strip_expr(expr
->right
);
1432 r_left
= strip_expr(right
->left
);
1433 r_right
= strip_expr(right
->right
);
1434 if (!get_implied_min(r_right
, &min
) || min
.value
<= 1)
1437 add_comparison(expr
->left
, '<', r_left
);
1440 static void match_binop_assign(struct expression
*expr
)
1442 struct expression
*right
;
1444 right
= strip_expr(expr
->right
);
1445 if (right
->op
== '+')
1446 match_assign_add(expr
);
1447 if (right
->op
== '-')
1448 match_assign_sub(expr
);
1449 if (right
->op
== '/')
1450 match_assign_divide(expr
);
1453 static void copy_comparisons(struct expression
*left
, struct expression
*right
)
1455 struct string_list
*links
;
1456 struct smatch_state
*state
;
1457 struct compare_data
*data
;
1458 struct symbol
*left_sym
, *right_sym
;
1459 char *left_var
= NULL
;
1460 char *right_var
= NULL
;
1461 struct var_sym_list
*left_vsl
;
1462 struct expression
*expr
;
1464 struct var_sym_list
*vsl
;
1468 left_var
= chunk_to_var_sym(left
, &left_sym
);
1471 left_vsl
= expr_to_vsl(left
);
1472 right_var
= chunk_to_var_sym(right
, &right_sym
);
1476 state
= get_state(link_id
, right_var
, right_sym
);
1479 links
= state
->data
;
1481 FOR_EACH_PTR(links
, tmp
) {
1482 state
= get_state(compare_id
, tmp
, NULL
);
1483 if (!state
|| !state
->data
)
1486 comparison
= data
->comparison
;
1488 var
= data
->right_var
;
1489 vsl
= data
->right_vsl
;
1490 if (strcmp(var
, right_var
) == 0) {
1492 var
= data
->left_var
;
1493 vsl
= data
->left_vsl
;
1494 comparison
= flip_comparison(comparison
);
1496 /* n = copy_from_user(dest, src, n); leads to n <= n which is nonsense */
1497 if (strcmp(left_var
, var
) == 0)
1499 add_comparison_var_sym(left
, left_var
, left_vsl
, comparison
, expr
, var
, vsl
);
1500 } END_FOR_EACH_PTR(tmp
);
1503 free_string(right_var
);
1506 static void match_assign(struct expression
*expr
)
1508 struct expression
*right
;
1510 if (expr
->op
!= '=')
1512 if (__in_fake_assign
|| outside_of_function())
1515 if (is_struct(expr
->left
))
1518 copy_comparisons(expr
->left
, expr
->right
);
1519 add_comparison(expr
->left
, SPECIAL_EQUAL
, expr
->right
);
1521 right
= strip_expr(expr
->right
);
1522 if (right
->type
== EXPR_BINOP
)
1523 match_binop_assign(expr
);
1526 int get_comparison_strings(const char *one
, const char *two
)
1529 struct smatch_state
*state
;
1536 if (strcmp(one
, two
) == 0)
1537 return SPECIAL_EQUAL
;
1539 if (strcmp(one
, two
) > 0) {
1540 const char *tmp
= one
;
1547 snprintf(buf
, sizeof(buf
), "%s vs %s", one
, two
);
1548 state
= get_state(compare_id
, buf
, NULL
);
1550 ret
= state_to_comparison(state
);
1553 ret
= flip_comparison(ret
);
1558 int get_comparison(struct expression
*a
, struct expression
*b
)
1564 a
= strip_parens(a
);
1565 b
= strip_parens(b
);
1567 move_plus_to_minus(&a
, &b
);
1569 one
= chunk_to_var(a
);
1572 two
= chunk_to_var(b
);
1576 ret
= get_comparison_strings(one
, two
);
1580 if (is_plus_one(a
) || is_minus_one(a
)) {
1582 one
= chunk_to_var(a
->left
);
1583 ret
= get_comparison_strings(one
, two
);
1584 } else if (is_plus_one(b
) || is_minus_one(b
)) {
1586 two
= chunk_to_var(b
->left
);
1587 ret
= get_comparison_strings(one
, two
);
1593 if ((is_plus_one(a
) || is_minus_one(b
)) && ret
== '<')
1595 else if ((is_minus_one(a
) || is_plus_one(b
)) && ret
== '>')
1605 return comparison_from_extra(a
, b
);
1609 int possible_comparison(struct expression
*a
, int comparison
, struct expression
*b
)
1615 struct sm_state
*sm
;
1618 one
= chunk_to_var(a
);
1621 two
= chunk_to_var(b
);
1626 if (strcmp(one
, two
) == 0 && comparison
== SPECIAL_EQUAL
) {
1631 if (strcmp(one
, two
) > 0) {
1636 comparison
= flip_comparison(comparison
);
1639 snprintf(buf
, sizeof(buf
), "%s vs %s", one
, two
);
1640 sm
= get_sm_state(compare_id
, buf
, NULL
);
1644 FOR_EACH_PTR(sm
->possible
, sm
) {
1645 if (!sm
->state
->data
)
1647 saved
= ((struct compare_data
*)sm
->state
->data
)->comparison
;
1648 if (saved
== comparison
)
1650 if (comparison
== SPECIAL_EQUAL
&&
1651 (saved
== SPECIAL_LTE
||
1652 saved
== SPECIAL_GTE
||
1653 saved
== SPECIAL_UNSIGNED_LTE
||
1654 saved
== SPECIAL_UNSIGNED_GTE
))
1658 } END_FOR_EACH_PTR(sm
);
1667 struct state_list
*get_all_comparisons(struct expression
*expr
)
1669 struct smatch_state
*state
;
1670 struct string_list
*links
;
1671 struct state_list
*ret
= NULL
;
1672 struct sm_state
*sm
;
1675 state
= get_state_chunk(link_id
, expr
);
1678 links
= state
->data
;
1680 FOR_EACH_PTR(links
, tmp
) {
1681 sm
= get_sm_state(compare_id
, tmp
, NULL
);
1684 // FIXME have to compare name with vsl
1685 add_ptr_list(&ret
, sm
);
1686 } END_FOR_EACH_PTR(tmp
);
1691 struct state_list
*get_all_possible_equal_comparisons(struct expression
*expr
)
1693 struct smatch_state
*state
;
1694 struct string_list
*links
;
1695 struct state_list
*ret
= NULL
;
1696 struct sm_state
*sm
;
1699 state
= get_state_chunk(link_id
, expr
);
1702 links
= state
->data
;
1704 FOR_EACH_PTR(links
, tmp
) {
1705 sm
= get_sm_state(compare_id
, tmp
, NULL
);
1708 if (!strchr(sm
->state
->name
, '='))
1710 if (strcmp(sm
->state
->name
, "!=") == 0)
1712 add_ptr_list(&ret
, sm
);
1713 } END_FOR_EACH_PTR(tmp
);
1718 struct state_list
*get_all_possible_not_equal_comparisons(struct expression
*expr
)
1720 struct smatch_state
*state
;
1721 struct string_list
*links
;
1722 struct state_list
*ret
= NULL
;
1723 struct sm_state
*sm
;
1724 struct sm_state
*possible
;
1729 state
= get_state_chunk(link_id
, expr
);
1732 links
= state
->data
;
1734 FOR_EACH_PTR(links
, link
) {
1735 sm
= get_sm_state(compare_id
, link
, NULL
);
1738 FOR_EACH_PTR(sm
->possible
, possible
) {
1739 if (strcmp(possible
->state
->name
, "!=") != 0)
1741 add_ptr_list(&ret
, sm
);
1743 } END_FOR_EACH_PTR(link
);
1744 } END_FOR_EACH_PTR(link
);
1749 static void update_links_from_call(struct expression
*left
,
1751 struct expression
*right
)
1753 struct string_list
*links
;
1754 struct smatch_state
*state
;
1755 struct compare_data
*data
;
1756 struct symbol
*left_sym
, *right_sym
;
1757 char *left_var
= NULL
;
1758 char *right_var
= NULL
;
1759 struct var_sym_list
*left_vsl
;
1760 struct expression
*expr
;
1762 struct var_sym_list
*vsl
;
1766 left_var
= chunk_to_var_sym(left
, &left_sym
);
1769 left_vsl
= expr_to_vsl(left
);
1770 right_var
= chunk_to_var_sym(right
, &right_sym
);
1774 state
= get_state(link_id
, right_var
, right_sym
);
1777 links
= state
->data
;
1779 FOR_EACH_PTR(links
, tmp
) {
1780 state
= get_state(compare_id
, tmp
, NULL
);
1781 if (!state
|| !state
->data
)
1784 comparison
= data
->comparison
;
1786 var
= data
->right_var
;
1787 vsl
= data
->right_vsl
;
1788 if (strcmp(var
, right_var
) == 0) {
1790 var
= data
->left_var
;
1791 vsl
= data
->left_vsl
;
1792 comparison
= flip_comparison(comparison
);
1794 comparison
= combine_comparisons(left_compare
, comparison
);
1797 add_comparison_var_sym(left
, left_var
, left_vsl
, comparison
, expr
, var
, vsl
);
1798 } END_FOR_EACH_PTR(tmp
);
1801 free_string(right_var
);
1804 void __add_return_comparison(struct expression
*call
, const char *range
)
1806 struct expression
*arg
;
1810 if (!str_to_comparison_arg(range
, call
, &comparison
, &arg
))
1812 snprintf(buf
, sizeof(buf
), "%s", show_special(comparison
));
1813 update_links_from_call(call
, comparison
, arg
);
1814 add_comparison(call
, comparison
, arg
);
1817 void __add_comparison_info(struct expression
*expr
, struct expression
*call
, const char *range
)
1819 copy_comparisons(expr
, call
);
1822 static char *get_mask_comparison(struct expression
*expr
, int ignore
)
1824 struct expression
*tmp
, *right
;
1828 /* The return value for "return foo & param;" is <= param */
1831 while ((tmp
= get_assigned_expr(expr
))) {
1832 expr
= strip_expr(tmp
);
1837 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '&')
1840 right
= strip_expr(expr
->right
);
1841 param
= get_param_num(right
);
1843 if (param
== ignore
)
1846 snprintf(buf
, sizeof(buf
), "[<=$%d]", param
);
1847 return alloc_sname(buf
);
1850 static char *range_comparison_to_param_helper(struct expression
*expr
, char starts_with
, int ignore
)
1852 struct symbol
*param
;
1855 char *ret_str
= NULL
;
1862 var
= chunk_to_var(expr
);
1867 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, param
) {
1873 snprintf(buf
, sizeof(buf
), "%s orig", param
->ident
->name
);
1874 compare
= get_comparison_strings(var
, buf
);
1877 if (show_special(compare
)[0] != starts_with
)
1879 snprintf(buf
, sizeof(buf
), "[%s$%d]", show_special(compare
), i
);
1880 ret_str
= alloc_sname(buf
);
1882 } END_FOR_EACH_PTR(param
);
1891 if (starts_with
== '<')
1892 ret_str
= get_mask_comparison(expr
, ignore
);
1896 char *name_sym_to_param_comparison(const char *name
, struct symbol
*sym
)
1898 struct symbol
*param
;
1904 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, param
) {
1908 snprintf(buf
, sizeof(buf
), "%s orig", param
->ident
->name
);
1909 compare
= get_comparison_strings(name
, buf
);
1912 snprintf(buf
, sizeof(buf
), "[%s$%d]", show_special(compare
), i
);
1913 return alloc_sname(buf
);
1914 } END_FOR_EACH_PTR(param
);
1919 char *expr_equal_to_param(struct expression
*expr
, int ignore
)
1921 return range_comparison_to_param_helper(expr
, '=', ignore
);
1924 char *expr_lte_to_param(struct expression
*expr
, int ignore
)
1926 return range_comparison_to_param_helper(expr
, '<', ignore
);
1929 char *expr_param_comparison(struct expression
*expr
, int ignore
)
1931 struct symbol
*param
;
1934 char *ret_str
= NULL
;
1938 var
= chunk_to_var(expr
);
1943 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, param
) {
1949 snprintf(buf
, sizeof(buf
), "%s orig", param
->ident
->name
);
1950 compare
= get_comparison_strings(var
, buf
);
1953 snprintf(buf
, sizeof(buf
), "[%s$%d]", show_special(compare
), i
);
1954 ret_str
= alloc_sname(buf
);
1956 } END_FOR_EACH_PTR(param
);
1963 char *get_printed_param_name(struct expression
*call
, const char *param_name
, struct symbol
*param_sym
)
1965 struct expression
*arg
;
1968 static char buf
[256];
1973 FOR_EACH_PTR(call
->args
, arg
) {
1976 name
= expr_to_var_sym(arg
, &sym
);
1979 if (sym
!= param_sym
)
1983 if (strncmp(name
, param_name
, len
) != 0)
1985 if (param_name
[len
] == '\0') {
1986 snprintf(buf
, sizeof(buf
), "$%d", i
);
1989 if (param_name
[len
] != '-')
1991 snprintf(buf
, sizeof(buf
), "$%d%s", i
, param_name
+ len
);
1993 } END_FOR_EACH_PTR(arg
);
1998 static void match_call_info(struct expression
*expr
)
2000 struct expression
*arg
;
2001 struct smatch_state
*state
;
2002 struct sm_state
*sm
;
2003 struct compare_data
*data
;
2005 struct string_list
*links
;
2007 const char *right_name
;
2013 FOR_EACH_PTR(expr
->args
, arg
) {
2016 state
= get_state_chunk(link_id
, arg
);
2020 links
= state
->data
;
2021 FOR_EACH_PTR(links
, link
) {
2022 struct var_sym_list
*right_vsl
;
2023 struct var_sym
*right_vs
;
2026 if (strstr(link
, " orig"))
2028 sm
= get_sm_state(compare_id
, link
, NULL
);
2031 data
= sm
->state
->data
;
2032 if (!data
|| !data
->comparison
)
2034 arg_name
= expr_to_var(arg
);
2039 if (strcmp(data
->left_var
, arg_name
) == 0) {
2040 comparison
= data
->comparison
;
2041 right_name
= data
->right_var
;
2042 right_vsl
= data
->right_vsl
;
2043 } else if (strcmp(data
->right_var
, arg_name
) == 0) {
2044 comparison
= flip_comparison(data
->comparison
);
2045 right_name
= data
->left_var
;
2046 right_vsl
= data
->left_vsl
;
2048 if (!right_vsl
|| ptr_list_size((struct ptr_list
*)right_vsl
) != 1)
2051 right_vs
= first_ptr_list((struct ptr_list
*)right_vsl
);
2052 if (strcmp(right_vs
->var
, right_name
) != 0)
2054 right_name
= get_printed_param_name(expr
, right_vs
->var
, right_vs
->sym
);
2057 snprintf(info_buf
, sizeof(info_buf
), "%s %s", show_special(comparison
), right_name
);
2058 sql_insert_caller_info(expr
, PARAM_COMPARE
, i
, "$", info_buf
);
2061 free_string(arg_name
);
2062 } END_FOR_EACH_PTR(link
);
2063 } END_FOR_EACH_PTR(arg
);
2066 static void struct_member_callback(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*link_sm
)
2068 struct sm_state
*compare_sm
;
2069 struct string_list
*links
;
2071 struct compare_data
*data
;
2072 struct var_sym
*left
, *right
;
2073 static char info_buf
[256];
2074 const char *right_name
;
2076 if (strstr(printed_name
, " orig"))
2079 links
= link_sm
->state
->data
;
2080 FOR_EACH_PTR(links
, link
) {
2081 compare_sm
= get_sm_state(compare_id
, link
, NULL
);
2084 data
= compare_sm
->state
->data
;
2085 if (!data
|| !data
->comparison
)
2088 if (ptr_list_size((struct ptr_list
*)data
->left_vsl
) != 1 ||
2089 ptr_list_size((struct ptr_list
*)data
->right_vsl
) != 1)
2091 left
= first_ptr_list((struct ptr_list
*)data
->left_vsl
);
2092 right
= first_ptr_list((struct ptr_list
*)data
->right_vsl
);
2093 if (left
->sym
== right
->sym
&&
2094 strcmp(left
->var
, right
->var
) == 0)
2097 * Both parameters link to this comparison so only
2098 * record the first one.
2100 if (left
->sym
!= link_sm
->sym
||
2101 strcmp(left
->var
, link_sm
->name
) != 0)
2104 right_name
= get_printed_param_name(call
, right
->var
, right
->sym
);
2107 snprintf(info_buf
, sizeof(info_buf
), "%s %s", show_special(data
->comparison
), right_name
);
2108 sql_insert_caller_info(call
, PARAM_COMPARE
, param
, printed_name
, info_buf
);
2109 } END_FOR_EACH_PTR(link
);
2112 static void print_return_value_comparison(int return_id
, char *return_ranges
, struct expression
*expr
)
2115 const char *tmp_name
;
2121 * TODO: This only prints == comparisons. That's probably the most
2122 * useful comparison because == max has lots of implications. But it
2123 * would be good to capture the rest as well.
2125 * This information is already in the DB but it's in the parameter math
2126 * bits and it's awkward to use it. This is is the simpler, possibly
2127 * cleaner way, but not necessarily the best, I don't know.
2132 name
= expr_to_var_sym(expr
, &sym
);
2136 param
= get_param_num_from_sym(sym
);
2139 if (param_was_set_var_sym(name
, sym
))
2142 tmp_name
= get_param_name_var_sym(name
, sym
);
2146 snprintf(info_buf
, sizeof(info_buf
), "== $%d%s", param
, tmp_name
+ 1);
2147 sql_insert_return_states(return_id
, return_ranges
,
2148 PARAM_COMPARE
, -1, "$", info_buf
);
2153 static void print_return_comparison(int return_id
, char *return_ranges
, struct expression
*expr
)
2155 struct sm_state
*tmp
;
2156 struct string_list
*links
;
2158 struct sm_state
*sm
;
2159 struct compare_data
*data
;
2160 struct var_sym
*left
, *right
;
2161 int left_param
, right_param
;
2163 char right_buf
[256];
2165 const char *tmp_name
;
2167 print_return_value_comparison(return_id
, return_ranges
, expr
);
2169 FOR_EACH_MY_SM(link_id
, __get_cur_stree(), tmp
) {
2170 if (get_param_num_from_sym(tmp
->sym
) < 0)
2172 links
= tmp
->state
->data
;
2173 FOR_EACH_PTR(links
, link
) {
2174 sm
= get_sm_state(compare_id
, link
, NULL
);
2177 data
= sm
->state
->data
;
2178 if (!data
|| !data
->comparison
)
2180 if (ptr_list_size((struct ptr_list
*)data
->left_vsl
) != 1 ||
2181 ptr_list_size((struct ptr_list
*)data
->right_vsl
) != 1)
2183 left
= first_ptr_list((struct ptr_list
*)data
->left_vsl
);
2184 right
= first_ptr_list((struct ptr_list
*)data
->right_vsl
);
2185 if (left
->sym
== right
->sym
&&
2186 strcmp(left
->var
, right
->var
) == 0)
2189 * Both parameters link to this comparison so only
2190 * record the first one.
2192 if (left
->sym
!= tmp
->sym
||
2193 strcmp(left
->var
, tmp
->name
) != 0)
2196 if (strstr(right
->var
, " orig"))
2199 left_param
= get_param_num_from_sym(left
->sym
);
2200 right_param
= get_param_num_from_sym(right
->sym
);
2201 if (left_param
< 0 || right_param
< 0)
2204 tmp_name
= get_param_name_var_sym(left
->var
, left
->sym
);
2207 snprintf(left_buf
, sizeof(left_buf
), "%s", tmp_name
);
2209 tmp_name
= get_param_name_var_sym(right
->var
, right
->sym
);
2210 if (!tmp_name
|| tmp_name
[0] != '$')
2212 snprintf(right_buf
, sizeof(right_buf
), "$%d%s", right_param
, tmp_name
+ 1);
2215 * FIXME: this should reject $ type variables (as
2216 * opposed to $->foo type). Those should come from
2217 * smatch_param_compare_limit.c.
2220 snprintf(info_buf
, sizeof(info_buf
), "%s %s", show_special(data
->comparison
), right_buf
);
2221 sql_insert_return_states(return_id
, return_ranges
,
2222 PARAM_COMPARE
, left_param
, left_buf
, info_buf
);
2223 } END_FOR_EACH_PTR(link
);
2225 } END_FOR_EACH_SM(tmp
);
2228 static int parse_comparison(char **value
, int *op
)
2236 if (**value
== '=') {
2244 *op
= SPECIAL_EQUAL
;
2249 *op
= SPECIAL_NOTEQUAL
;
2253 if (**value
== '=') {
2262 if (**value
!= ' ') {
2263 sm_msg("internal error parsing comparison. %s", *value
);
2271 static int split_op_param_key(char *value
, int *op
, int *param
, char **key
)
2273 static char buf
[256];
2276 if (!parse_comparison(&value
, op
))
2279 snprintf(buf
, sizeof(buf
), value
);
2286 if (*param
< 0 || *param
> 99)
2298 static void db_return_comparison(struct expression
*expr
, int left_param
, char *key
, char *value
)
2300 struct expression
*left_arg
, *right_arg
;
2301 char *left_name
= NULL
;
2302 struct symbol
*left_sym
;
2303 char *right_name
= NULL
;
2304 struct symbol
*right_sym
;
2308 struct var_sym_list
*left_vsl
= NULL
, *right_vsl
= NULL
;
2310 if (left_param
== -1) {
2311 if (expr
->type
!= EXPR_ASSIGNMENT
)
2313 left_arg
= strip_expr(expr
->left
);
2315 while (expr
->type
== EXPR_ASSIGNMENT
)
2316 expr
= strip_expr(expr
->right
);
2317 if (expr
->type
!= EXPR_CALL
)
2320 while (expr
->type
== EXPR_ASSIGNMENT
)
2321 expr
= strip_expr(expr
->right
);
2322 if (expr
->type
!= EXPR_CALL
)
2325 left_arg
= get_argument_from_call_expr(expr
->args
, left_param
);
2330 if (!split_op_param_key(value
, &op
, &right_param
, &right_key
))
2333 right_arg
= get_argument_from_call_expr(expr
->args
, right_param
);
2337 left_name
= get_variable_from_key(left_arg
, key
, &left_sym
);
2338 if (!left_name
|| !left_sym
)
2341 right_name
= get_variable_from_key(right_arg
, right_key
, &right_sym
);
2342 if (!right_name
|| !right_sym
)
2345 add_var_sym(&left_vsl
, left_name
, left_sym
);
2346 add_var_sym(&right_vsl
, right_name
, right_sym
);
2348 add_comparison_var_sym(NULL
, left_name
, left_vsl
, op
, NULL
, right_name
, right_vsl
);
2351 free_string(left_name
);
2352 free_string(right_name
);
2355 int param_compare_limit_is_impossible(struct expression
*expr
, int left_param
, char *left_key
, char *value
)
2357 struct smatch_state
*state
;
2358 char *left_name
= NULL
;
2359 char *right_name
= NULL
;
2360 struct symbol
*left_sym
, *right_sym
;
2361 struct expression
*left_arg
, *right_arg
;
2368 while (expr
->type
== EXPR_ASSIGNMENT
)
2369 expr
= strip_expr(expr
->right
);
2370 if (expr
->type
!= EXPR_CALL
)
2373 if (!split_op_param_key(value
, &op
, &right_param
, &right_key
))
2376 left_arg
= get_argument_from_call_expr(expr
->args
, left_param
);
2380 right_arg
= get_argument_from_call_expr(expr
->args
, right_param
);
2384 left_name
= get_variable_from_key(left_arg
, left_key
, &left_sym
);
2385 right_name
= get_variable_from_key(right_arg
, right_key
, &right_sym
);
2386 if (!left_name
|| !right_name
)
2389 snprintf(buf
, sizeof(buf
), "%s vs %s", left_name
, right_name
);
2390 state
= get_state(compare_id
, buf
, NULL
);
2393 state_op
= state_to_comparison(state
);
2397 if (!filter_comparison(remove_unsigned_from_comparison(state_op
), op
))
2400 free_string(left_name
);
2401 free_string(right_name
);
2405 int impossibly_high_comparison(struct expression
*expr
)
2407 struct smatch_state
*link_state
;
2408 struct sm_state
*sm
;
2409 struct string_list
*links
;
2411 struct compare_data
*data
;
2413 link_state
= get_state_expr(link_id
, expr
);
2415 if (expr
->type
== EXPR_BINOP
&&
2416 (impossibly_high_comparison(expr
->left
) ||
2417 impossibly_high_comparison(expr
->right
)))
2422 links
= link_state
->data
;
2423 FOR_EACH_PTR(links
, link
) {
2424 sm
= get_sm_state(compare_id
, link
, NULL
);
2427 data
= sm
->state
->data
;
2430 if (!possibly_true(data
->left
, data
->comparison
, data
->right
))
2432 } END_FOR_EACH_PTR(link
);
2437 static void free_data(struct symbol
*sym
)
2441 clear_compare_data_alloc();
2444 void register_comparison(int id
)
2447 add_hook(&save_start_states
, AFTER_DEF_HOOK
);
2448 add_unmatched_state_hook(compare_id
, unmatched_comparison
);
2449 add_merge_hook(compare_id
, &merge_compare_states
);
2450 add_hook(&free_data
, AFTER_FUNC_HOOK
);
2451 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
2452 add_split_return_callback(&print_return_comparison
);
2454 select_return_states_hook(PARAM_COMPARE
, &db_return_comparison
);
2455 add_hook(&match_preop
, OP_HOOK
);
2458 void register_comparison_late(int id
)
2460 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
2463 void register_comparison_links(int id
)
2466 add_merge_hook(link_id
, &merge_links
);
2467 add_modification_hook(link_id
, &match_modify
);
2468 add_modification_hook_late(link_id
, match_inc_dec
);
2470 add_member_info_callback(link_id
, struct_member_callback
);
2473 void register_comparison_inc_dec(int id
)
2476 add_modification_hook_late(inc_dec_id
, &iter_modify
);
2479 void register_comparison_inc_dec_links(int id
)
2481 inc_dec_link_id
= id
;
2482 set_up_link_functions(inc_dec_id
, inc_dec_link_id
);
2485 static void filter_by_sm(struct sm_state
*sm
, int op
,
2486 struct state_list
**true_stack
,
2487 struct state_list
**false_stack
)
2489 struct compare_data
*data
;
2495 data
= sm
->state
->data
;
2498 filter_by_sm(sm
->left
, op
, true_stack
, false_stack
);
2499 filter_by_sm(sm
->right
, op
, true_stack
, false_stack
);
2504 if (data
->comparison
&&
2505 data
->comparison
== filter_comparison(data
->comparison
, op
))
2508 if (data
->comparison
&&
2509 data
->comparison
== filter_comparison(data
->comparison
, negate_comparison(op
)))
2513 add_ptr_list(true_stack
, sm
);
2515 add_ptr_list(false_stack
, sm
);
2518 filter_by_sm(sm
->left
, op
, true_stack
, false_stack
);
2519 filter_by_sm(sm
->right
, op
, true_stack
, false_stack
);
2523 struct sm_state
*comparison_implication_hook(struct expression
*expr
,
2524 struct state_list
**true_stack
,
2525 struct state_list
**false_stack
)
2527 struct sm_state
*sm
;
2530 static char buf
[256];
2532 if (expr
->type
!= EXPR_COMPARE
)
2537 left
= expr_to_var(expr
->left
);
2538 right
= expr_to_var(expr
->right
);
2539 if (!left
|| !right
) {
2545 if (strcmp(left
, right
) > 0) {
2550 op
= flip_comparison(op
);
2553 snprintf(buf
, sizeof(buf
), "%s vs %s", left
, right
);
2554 sm
= get_sm_state(compare_id
, buf
, NULL
);
2560 filter_by_sm(sm
, op
, true_stack
, false_stack
);
2561 if (!*true_stack
&& !*false_stack
)
2565 sm_msg("implications from comparison: (%s)", show_sm(sm
));