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 static 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 add_comparison_var_sym(left
, left_var
, left_vsl
, comparison
, expr
, var
, vsl
);
1497 } END_FOR_EACH_PTR(tmp
);
1500 free_string(right_var
);
1503 static void match_assign(struct expression
*expr
)
1505 struct expression
*right
;
1507 if (expr
->op
!= '=')
1509 if (__in_fake_assign
|| outside_of_function())
1512 if (is_struct(expr
->left
))
1515 copy_comparisons(expr
->left
, expr
->right
);
1516 add_comparison(expr
->left
, SPECIAL_EQUAL
, expr
->right
);
1518 right
= strip_expr(expr
->right
);
1519 if (right
->type
== EXPR_BINOP
)
1520 match_binop_assign(expr
);
1523 int get_comparison_strings(const char *one
, const char *two
)
1526 struct smatch_state
*state
;
1533 if (strcmp(one
, two
) == 0)
1534 return SPECIAL_EQUAL
;
1536 if (strcmp(one
, two
) > 0) {
1537 const char *tmp
= one
;
1544 snprintf(buf
, sizeof(buf
), "%s vs %s", one
, two
);
1545 state
= get_state(compare_id
, buf
, NULL
);
1547 ret
= state_to_comparison(state
);
1550 ret
= flip_comparison(ret
);
1555 int get_comparison(struct expression
*a
, struct expression
*b
)
1561 a
= strip_parens(a
);
1562 b
= strip_parens(b
);
1564 move_plus_to_minus(&a
, &b
);
1566 one
= chunk_to_var(a
);
1569 two
= chunk_to_var(b
);
1573 ret
= get_comparison_strings(one
, two
);
1577 if (is_plus_one(a
) || is_minus_one(a
)) {
1579 one
= chunk_to_var(a
->left
);
1580 ret
= get_comparison_strings(one
, two
);
1581 } else if (is_plus_one(b
) || is_minus_one(b
)) {
1583 two
= chunk_to_var(b
->left
);
1584 ret
= get_comparison_strings(one
, two
);
1590 if ((is_plus_one(a
) || is_minus_one(b
)) && ret
== '<')
1592 else if ((is_minus_one(a
) || is_plus_one(b
)) && ret
== '>')
1602 return comparison_from_extra(a
, b
);
1606 int possible_comparison(struct expression
*a
, int comparison
, struct expression
*b
)
1612 struct sm_state
*sm
;
1615 one
= chunk_to_var(a
);
1618 two
= chunk_to_var(b
);
1623 if (strcmp(one
, two
) == 0 && comparison
== SPECIAL_EQUAL
) {
1628 if (strcmp(one
, two
) > 0) {
1633 comparison
= flip_comparison(comparison
);
1636 snprintf(buf
, sizeof(buf
), "%s vs %s", one
, two
);
1637 sm
= get_sm_state(compare_id
, buf
, NULL
);
1641 FOR_EACH_PTR(sm
->possible
, sm
) {
1642 if (!sm
->state
->data
)
1644 saved
= ((struct compare_data
*)sm
->state
->data
)->comparison
;
1645 if (saved
== comparison
)
1647 if (comparison
== SPECIAL_EQUAL
&&
1648 (saved
== SPECIAL_LTE
||
1649 saved
== SPECIAL_GTE
||
1650 saved
== SPECIAL_UNSIGNED_LTE
||
1651 saved
== SPECIAL_UNSIGNED_GTE
))
1655 } END_FOR_EACH_PTR(sm
);
1664 struct state_list
*get_all_comparisons(struct expression
*expr
)
1666 struct smatch_state
*state
;
1667 struct string_list
*links
;
1668 struct state_list
*ret
= NULL
;
1669 struct sm_state
*sm
;
1672 state
= get_state_chunk(link_id
, expr
);
1675 links
= state
->data
;
1677 FOR_EACH_PTR(links
, tmp
) {
1678 sm
= get_sm_state(compare_id
, tmp
, NULL
);
1681 // FIXME have to compare name with vsl
1682 add_ptr_list(&ret
, sm
);
1683 } END_FOR_EACH_PTR(tmp
);
1688 struct state_list
*get_all_possible_equal_comparisons(struct expression
*expr
)
1690 struct smatch_state
*state
;
1691 struct string_list
*links
;
1692 struct state_list
*ret
= NULL
;
1693 struct sm_state
*sm
;
1696 state
= get_state_chunk(link_id
, expr
);
1699 links
= state
->data
;
1701 FOR_EACH_PTR(links
, tmp
) {
1702 sm
= get_sm_state(compare_id
, tmp
, NULL
);
1705 if (!strchr(sm
->state
->name
, '='))
1707 if (strcmp(sm
->state
->name
, "!=") == 0)
1709 add_ptr_list(&ret
, sm
);
1710 } END_FOR_EACH_PTR(tmp
);
1715 struct state_list
*get_all_possible_not_equal_comparisons(struct expression
*expr
)
1717 struct smatch_state
*state
;
1718 struct string_list
*links
;
1719 struct state_list
*ret
= NULL
;
1720 struct sm_state
*sm
;
1721 struct sm_state
*possible
;
1726 state
= get_state_chunk(link_id
, expr
);
1729 links
= state
->data
;
1731 FOR_EACH_PTR(links
, link
) {
1732 sm
= get_sm_state(compare_id
, link
, NULL
);
1735 FOR_EACH_PTR(sm
->possible
, possible
) {
1736 if (strcmp(possible
->state
->name
, "!=") != 0)
1738 add_ptr_list(&ret
, sm
);
1740 } END_FOR_EACH_PTR(link
);
1741 } END_FOR_EACH_PTR(link
);
1746 static void update_links_from_call(struct expression
*left
,
1748 struct expression
*right
)
1750 struct string_list
*links
;
1751 struct smatch_state
*state
;
1752 struct compare_data
*data
;
1753 struct symbol
*left_sym
, *right_sym
;
1754 char *left_var
= NULL
;
1755 char *right_var
= NULL
;
1756 struct var_sym_list
*left_vsl
;
1757 struct expression
*expr
;
1759 struct var_sym_list
*vsl
;
1763 left_var
= chunk_to_var_sym(left
, &left_sym
);
1766 left_vsl
= expr_to_vsl(left
);
1767 right_var
= chunk_to_var_sym(right
, &right_sym
);
1771 state
= get_state(link_id
, right_var
, right_sym
);
1774 links
= state
->data
;
1776 FOR_EACH_PTR(links
, tmp
) {
1777 state
= get_state(compare_id
, tmp
, NULL
);
1778 if (!state
|| !state
->data
)
1781 comparison
= data
->comparison
;
1783 var
= data
->right_var
;
1784 vsl
= data
->right_vsl
;
1785 if (strcmp(var
, right_var
) == 0) {
1787 var
= data
->left_var
;
1788 vsl
= data
->left_vsl
;
1789 comparison
= flip_comparison(comparison
);
1791 comparison
= combine_comparisons(left_compare
, comparison
);
1794 add_comparison_var_sym(left
, left_var
, left_vsl
, comparison
, expr
, var
, vsl
);
1795 } END_FOR_EACH_PTR(tmp
);
1798 free_string(right_var
);
1801 void __add_return_comparison(struct expression
*call
, const char *range
)
1803 struct expression
*arg
;
1807 if (!str_to_comparison_arg(range
, call
, &comparison
, &arg
))
1809 snprintf(buf
, sizeof(buf
), "%s", show_special(comparison
));
1810 update_links_from_call(call
, comparison
, arg
);
1811 add_comparison(call
, comparison
, arg
);
1814 void __add_comparison_info(struct expression
*expr
, struct expression
*call
, const char *range
)
1816 copy_comparisons(expr
, call
);
1819 static char *get_mask_comparison(struct expression
*expr
, int ignore
)
1821 struct expression
*tmp
, *right
;
1825 /* The return value for "return foo & param;" is <= param */
1828 while ((tmp
= get_assigned_expr(expr
))) {
1829 expr
= strip_expr(tmp
);
1834 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '&')
1837 right
= strip_expr(expr
->right
);
1838 param
= get_param_num(right
);
1840 if (param
== ignore
)
1843 snprintf(buf
, sizeof(buf
), "[<=$%d]", param
);
1844 return alloc_sname(buf
);
1847 static char *range_comparison_to_param_helper(struct expression
*expr
, char starts_with
, int ignore
)
1849 struct symbol
*param
;
1852 char *ret_str
= NULL
;
1859 var
= chunk_to_var(expr
);
1864 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, param
) {
1870 snprintf(buf
, sizeof(buf
), "%s orig", param
->ident
->name
);
1871 compare
= get_comparison_strings(var
, buf
);
1874 if (show_special(compare
)[0] != starts_with
)
1876 snprintf(buf
, sizeof(buf
), "[%s$%d]", show_special(compare
), i
);
1877 ret_str
= alloc_sname(buf
);
1879 } END_FOR_EACH_PTR(param
);
1888 if (starts_with
== '<')
1889 ret_str
= get_mask_comparison(expr
, ignore
);
1893 char *name_sym_to_param_comparison(const char *name
, struct symbol
*sym
)
1895 struct symbol
*param
;
1901 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, param
) {
1905 snprintf(buf
, sizeof(buf
), "%s orig", param
->ident
->name
);
1906 compare
= get_comparison_strings(name
, buf
);
1909 snprintf(buf
, sizeof(buf
), "[%s$%d]", show_special(compare
), i
);
1910 return alloc_sname(buf
);
1911 } END_FOR_EACH_PTR(param
);
1916 char *expr_equal_to_param(struct expression
*expr
, int ignore
)
1918 return range_comparison_to_param_helper(expr
, '=', ignore
);
1921 char *expr_lte_to_param(struct expression
*expr
, int ignore
)
1923 return range_comparison_to_param_helper(expr
, '<', ignore
);
1926 char *expr_param_comparison(struct expression
*expr
, int ignore
)
1928 struct symbol
*param
;
1931 char *ret_str
= NULL
;
1935 var
= chunk_to_var(expr
);
1940 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, param
) {
1946 snprintf(buf
, sizeof(buf
), "%s orig", param
->ident
->name
);
1947 compare
= get_comparison_strings(var
, buf
);
1950 snprintf(buf
, sizeof(buf
), "[%s$%d]", show_special(compare
), i
);
1951 ret_str
= alloc_sname(buf
);
1953 } END_FOR_EACH_PTR(param
);
1960 char *get_printed_param_name(struct expression
*call
, const char *param_name
, struct symbol
*param_sym
)
1962 struct expression
*arg
;
1965 static char buf
[256];
1970 FOR_EACH_PTR(call
->args
, arg
) {
1973 name
= expr_to_var_sym(arg
, &sym
);
1976 if (sym
!= param_sym
)
1980 if (strncmp(name
, param_name
, len
) != 0)
1982 if (param_name
[len
] == '\0') {
1983 snprintf(buf
, sizeof(buf
), "$%d", i
);
1986 if (param_name
[len
] != '-')
1988 snprintf(buf
, sizeof(buf
), "$%d%s", i
, param_name
+ len
);
1990 } END_FOR_EACH_PTR(arg
);
1995 static void match_call_info(struct expression
*expr
)
1997 struct expression
*arg
;
1998 struct smatch_state
*state
;
1999 struct sm_state
*sm
;
2000 struct compare_data
*data
;
2002 struct string_list
*links
;
2004 const char *right_name
;
2010 FOR_EACH_PTR(expr
->args
, arg
) {
2013 state
= get_state_chunk(link_id
, arg
);
2017 links
= state
->data
;
2018 FOR_EACH_PTR(links
, link
) {
2019 struct var_sym_list
*right_vsl
;
2020 struct var_sym
*right_vs
;
2023 if (strstr(link
, " orig"))
2025 sm
= get_sm_state(compare_id
, link
, NULL
);
2028 data
= sm
->state
->data
;
2029 if (!data
|| !data
->comparison
)
2031 arg_name
= expr_to_var(arg
);
2036 if (strcmp(data
->left_var
, arg_name
) == 0) {
2037 comparison
= data
->comparison
;
2038 right_name
= data
->right_var
;
2039 right_vsl
= data
->right_vsl
;
2040 } else if (strcmp(data
->right_var
, arg_name
) == 0) {
2041 comparison
= flip_comparison(data
->comparison
);
2042 right_name
= data
->left_var
;
2043 right_vsl
= data
->left_vsl
;
2045 if (!right_vsl
|| ptr_list_size((struct ptr_list
*)right_vsl
) != 1)
2048 right_vs
= first_ptr_list((struct ptr_list
*)right_vsl
);
2049 if (strcmp(right_vs
->var
, right_name
) != 0)
2051 right_name
= get_printed_param_name(expr
, right_vs
->var
, right_vs
->sym
);
2054 snprintf(info_buf
, sizeof(info_buf
), "%s %s", show_special(comparison
), right_name
);
2055 sql_insert_caller_info(expr
, PARAM_COMPARE
, i
, "$", info_buf
);
2058 free_string(arg_name
);
2059 } END_FOR_EACH_PTR(link
);
2060 } END_FOR_EACH_PTR(arg
);
2063 static void struct_member_callback(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*link_sm
)
2065 struct sm_state
*compare_sm
;
2066 struct string_list
*links
;
2068 struct compare_data
*data
;
2069 struct var_sym
*left
, *right
;
2070 static char info_buf
[256];
2071 const char *right_name
;
2073 if (strstr(printed_name
, " orig"))
2076 links
= link_sm
->state
->data
;
2077 FOR_EACH_PTR(links
, link
) {
2078 compare_sm
= get_sm_state(compare_id
, link
, NULL
);
2081 data
= compare_sm
->state
->data
;
2082 if (!data
|| !data
->comparison
)
2085 if (ptr_list_size((struct ptr_list
*)data
->left_vsl
) != 1 ||
2086 ptr_list_size((struct ptr_list
*)data
->right_vsl
) != 1)
2088 left
= first_ptr_list((struct ptr_list
*)data
->left_vsl
);
2089 right
= first_ptr_list((struct ptr_list
*)data
->right_vsl
);
2090 if (left
->sym
== right
->sym
&&
2091 strcmp(left
->var
, right
->var
) == 0)
2094 * Both parameters link to this comparison so only
2095 * record the first one.
2097 if (left
->sym
!= link_sm
->sym
||
2098 strcmp(left
->var
, link_sm
->name
) != 0)
2101 right_name
= get_printed_param_name(call
, right
->var
, right
->sym
);
2104 snprintf(info_buf
, sizeof(info_buf
), "%s %s", show_special(data
->comparison
), right_name
);
2105 sql_insert_caller_info(call
, PARAM_COMPARE
, param
, printed_name
, info_buf
);
2106 } END_FOR_EACH_PTR(link
);
2109 static void print_return_value_comparison(int return_id
, char *return_ranges
, struct expression
*expr
)
2112 const char *tmp_name
;
2118 * TODO: This only prints == comparisons. That's probably the most
2119 * useful comparison because == max has lots of implications. But it
2120 * would be good to capture the rest as well.
2122 * This information is already in the DB but it's in the parameter math
2123 * bits and it's awkward to use it. This is is the simpler, possibly
2124 * cleaner way, but not necessarily the best, I don't know.
2129 name
= expr_to_var_sym(expr
, &sym
);
2133 param
= get_param_num_from_sym(sym
);
2136 if (param_was_set_var_sym(name
, sym
))
2139 tmp_name
= get_param_name_var_sym(name
, sym
);
2143 snprintf(info_buf
, sizeof(info_buf
), "== $%d%s", param
, tmp_name
+ 1);
2144 sql_insert_return_states(return_id
, return_ranges
,
2145 PARAM_COMPARE
, -1, "$", info_buf
);
2150 static void print_return_comparison(int return_id
, char *return_ranges
, struct expression
*expr
)
2152 struct sm_state
*tmp
;
2153 struct string_list
*links
;
2155 struct sm_state
*sm
;
2156 struct compare_data
*data
;
2157 struct var_sym
*left
, *right
;
2158 int left_param
, right_param
;
2160 char right_buf
[256];
2162 const char *tmp_name
;
2164 print_return_value_comparison(return_id
, return_ranges
, expr
);
2166 FOR_EACH_MY_SM(link_id
, __get_cur_stree(), tmp
) {
2167 if (get_param_num_from_sym(tmp
->sym
) < 0)
2169 links
= tmp
->state
->data
;
2170 FOR_EACH_PTR(links
, link
) {
2171 sm
= get_sm_state(compare_id
, link
, NULL
);
2174 data
= sm
->state
->data
;
2175 if (!data
|| !data
->comparison
)
2177 if (ptr_list_size((struct ptr_list
*)data
->left_vsl
) != 1 ||
2178 ptr_list_size((struct ptr_list
*)data
->right_vsl
) != 1)
2180 left
= first_ptr_list((struct ptr_list
*)data
->left_vsl
);
2181 right
= first_ptr_list((struct ptr_list
*)data
->right_vsl
);
2182 if (left
->sym
== right
->sym
&&
2183 strcmp(left
->var
, right
->var
) == 0)
2186 * Both parameters link to this comparison so only
2187 * record the first one.
2189 if (left
->sym
!= tmp
->sym
||
2190 strcmp(left
->var
, tmp
->name
) != 0)
2193 if (strstr(right
->var
, " orig"))
2196 left_param
= get_param_num_from_sym(left
->sym
);
2197 right_param
= get_param_num_from_sym(right
->sym
);
2198 if (left_param
< 0 || right_param
< 0)
2201 tmp_name
= get_param_name_var_sym(left
->var
, left
->sym
);
2204 snprintf(left_buf
, sizeof(left_buf
), "%s", tmp_name
);
2206 tmp_name
= get_param_name_var_sym(right
->var
, right
->sym
);
2207 if (!tmp_name
|| tmp_name
[0] != '$')
2209 snprintf(right_buf
, sizeof(right_buf
), "$%d%s", right_param
, tmp_name
+ 1);
2212 * FIXME: this should reject $ type variables (as
2213 * opposed to $->foo type). Those should come from
2214 * smatch_param_compare_limit.c.
2217 snprintf(info_buf
, sizeof(info_buf
), "%s %s", show_special(data
->comparison
), right_buf
);
2218 sql_insert_return_states(return_id
, return_ranges
,
2219 PARAM_COMPARE
, left_param
, left_buf
, info_buf
);
2220 } END_FOR_EACH_PTR(link
);
2222 } END_FOR_EACH_SM(tmp
);
2225 static int parse_comparison(char **value
, int *op
)
2233 if (**value
== '=') {
2241 *op
= SPECIAL_EQUAL
;
2246 *op
= SPECIAL_NOTEQUAL
;
2250 if (**value
== '=') {
2259 if (**value
!= ' ') {
2260 sm_msg("internal error parsing comparison. %s", *value
);
2268 static int split_op_param_key(char *value
, int *op
, int *param
, char **key
)
2270 static char buf
[256];
2273 if (!parse_comparison(&value
, op
))
2276 snprintf(buf
, sizeof(buf
), value
);
2283 if (*param
< 0 || *param
> 99)
2295 static void db_return_comparison(struct expression
*expr
, int left_param
, char *key
, char *value
)
2297 struct expression
*left_arg
, *right_arg
;
2298 char *left_name
= NULL
;
2299 struct symbol
*left_sym
;
2300 char *right_name
= NULL
;
2301 struct symbol
*right_sym
;
2305 struct var_sym_list
*left_vsl
= NULL
, *right_vsl
= NULL
;
2307 if (left_param
== -1) {
2308 if (expr
->type
!= EXPR_ASSIGNMENT
)
2310 left_arg
= strip_expr(expr
->left
);
2312 while (expr
->type
== EXPR_ASSIGNMENT
)
2313 expr
= strip_expr(expr
->right
);
2314 if (expr
->type
!= EXPR_CALL
)
2317 while (expr
->type
== EXPR_ASSIGNMENT
)
2318 expr
= strip_expr(expr
->right
);
2319 if (expr
->type
!= EXPR_CALL
)
2322 left_arg
= get_argument_from_call_expr(expr
->args
, left_param
);
2327 if (!split_op_param_key(value
, &op
, &right_param
, &right_key
))
2330 right_arg
= get_argument_from_call_expr(expr
->args
, right_param
);
2334 left_name
= get_variable_from_key(left_arg
, key
, &left_sym
);
2335 if (!left_name
|| !left_sym
)
2338 right_name
= get_variable_from_key(right_arg
, right_key
, &right_sym
);
2339 if (!right_name
|| !right_sym
)
2342 add_var_sym(&left_vsl
, left_name
, left_sym
);
2343 add_var_sym(&right_vsl
, right_name
, right_sym
);
2345 add_comparison_var_sym(NULL
, left_name
, left_vsl
, op
, NULL
, right_name
, right_vsl
);
2348 free_string(left_name
);
2349 free_string(right_name
);
2352 int param_compare_limit_is_impossible(struct expression
*expr
, int left_param
, char *left_key
, char *value
)
2354 struct smatch_state
*state
;
2355 char *left_name
= NULL
;
2356 char *right_name
= NULL
;
2357 struct symbol
*left_sym
, *right_sym
;
2358 struct expression
*left_arg
, *right_arg
;
2365 while (expr
->type
== EXPR_ASSIGNMENT
)
2366 expr
= strip_expr(expr
->right
);
2367 if (expr
->type
!= EXPR_CALL
)
2370 if (!split_op_param_key(value
, &op
, &right_param
, &right_key
))
2373 left_arg
= get_argument_from_call_expr(expr
->args
, left_param
);
2377 right_arg
= get_argument_from_call_expr(expr
->args
, right_param
);
2381 left_name
= get_variable_from_key(left_arg
, left_key
, &left_sym
);
2382 right_name
= get_variable_from_key(right_arg
, right_key
, &right_sym
);
2383 if (!left_name
|| !right_name
)
2386 snprintf(buf
, sizeof(buf
), "%s vs %s", left_name
, right_name
);
2387 state
= get_state(compare_id
, buf
, NULL
);
2390 state_op
= state_to_comparison(state
);
2394 if (!filter_comparison(remove_unsigned_from_comparison(state_op
), op
))
2397 free_string(left_name
);
2398 free_string(right_name
);
2402 int impossibly_high_comparison(struct expression
*expr
)
2404 struct smatch_state
*link_state
;
2405 struct sm_state
*sm
;
2406 struct string_list
*links
;
2408 struct compare_data
*data
;
2410 link_state
= get_state_expr(link_id
, expr
);
2412 if (expr
->type
== EXPR_BINOP
&&
2413 (impossibly_high_comparison(expr
->left
) ||
2414 impossibly_high_comparison(expr
->right
)))
2419 links
= link_state
->data
;
2420 FOR_EACH_PTR(links
, link
) {
2421 sm
= get_sm_state(compare_id
, link
, NULL
);
2424 data
= sm
->state
->data
;
2427 if (!possibly_true(data
->left
, data
->comparison
, data
->right
))
2429 } END_FOR_EACH_PTR(link
);
2434 static void free_data(struct symbol
*sym
)
2438 clear_compare_data_alloc();
2441 void register_comparison(int id
)
2444 add_hook(&save_start_states
, AFTER_DEF_HOOK
);
2445 add_unmatched_state_hook(compare_id
, unmatched_comparison
);
2446 add_merge_hook(compare_id
, &merge_compare_states
);
2447 add_hook(&free_data
, AFTER_FUNC_HOOK
);
2448 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
2449 add_split_return_callback(&print_return_comparison
);
2451 select_return_states_hook(PARAM_COMPARE
, &db_return_comparison
);
2452 add_hook(&match_preop
, OP_HOOK
);
2455 void register_comparison_late(int id
)
2457 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
2460 void register_comparison_links(int id
)
2463 add_merge_hook(link_id
, &merge_links
);
2464 add_modification_hook(link_id
, &match_modify
);
2465 add_modification_hook_late(link_id
, match_inc_dec
);
2467 add_member_info_callback(link_id
, struct_member_callback
);
2470 void register_comparison_inc_dec(int id
)
2473 add_modification_hook_late(inc_dec_id
, &iter_modify
);
2476 void register_comparison_inc_dec_links(int id
)
2478 inc_dec_link_id
= id
;
2479 set_up_link_functions(inc_dec_id
, inc_dec_link_id
);
2482 static void filter_by_sm(struct sm_state
*sm
, int op
,
2483 struct state_list
**true_stack
,
2484 struct state_list
**false_stack
)
2486 struct compare_data
*data
;
2492 data
= sm
->state
->data
;
2495 filter_by_sm(sm
->left
, op
, true_stack
, false_stack
);
2496 filter_by_sm(sm
->right
, op
, true_stack
, false_stack
);
2501 if (data
->comparison
&&
2502 data
->comparison
== filter_comparison(data
->comparison
, op
))
2505 if (data
->comparison
&&
2506 data
->comparison
== filter_comparison(data
->comparison
, negate_comparison(op
)))
2510 add_ptr_list(true_stack
, sm
);
2512 add_ptr_list(false_stack
, sm
);
2515 filter_by_sm(sm
->left
, op
, true_stack
, false_stack
);
2516 filter_by_sm(sm
->right
, op
, true_stack
, false_stack
);
2520 struct sm_state
*comparison_implication_hook(struct expression
*expr
,
2521 struct state_list
**true_stack
,
2522 struct state_list
**false_stack
)
2524 struct sm_state
*sm
;
2527 static char buf
[256];
2529 if (expr
->type
!= EXPR_COMPARE
)
2534 left
= expr_to_var(expr
->left
);
2535 right
= expr_to_var(expr
->right
);
2536 if (!left
|| !right
) {
2542 if (strcmp(left
, right
) > 0) {
2547 op
= flip_comparison(op
);
2550 snprintf(buf
, sizeof(buf
), "%s vs %s", left
, right
);
2551 sm
= get_sm_state(compare_id
, buf
, NULL
);
2557 filter_by_sm(sm
, op
, true_stack
, false_stack
);
2558 if (!*true_stack
&& !*false_stack
)