mtag_data: cache DB results
[smatch.git] / smatch_comparison.c
blob8575c13fcd8ec40ef100b3f77dba88e4d46d85d7
1 /*
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.
20 * Ie: y > x.
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
23 * would be ">=".
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.
31 #include "smatch.h"
32 #include "smatch_extra.h"
33 #include "smatch_slist.h"
35 static int compare_id;
36 static int link_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 */
43 STATE(start);
44 STATE(incremented);
46 ALLOCATOR(compare_data, "compare data");
48 static struct symbol *vsl_to_sym(struct var_sym_list *vsl)
50 struct var_sym *vs;
52 if (!vsl)
53 return NULL;
54 if (ptr_list_size((struct ptr_list *)vsl) != 1)
55 return NULL;
56 vs = first_ptr_list((struct ptr_list *)vsl);
57 return vs->sym;
60 struct smatch_state *alloc_compare_state(
61 struct expression *left,
62 const char *left_var, struct var_sym_list *left_vsl,
63 int comparison,
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);
73 data->left = left;
74 data->left_var = alloc_sname(left_var);
75 data->left_vsl = clone_var_sym_list(left_vsl);
76 data->comparison = comparison;
77 data->right = right;
78 data->right_var = alloc_sname(right_var);
79 data->right_vsl = clone_var_sym_list(right_vsl);
80 state->data = data;
81 return state;
84 int state_to_comparison(struct smatch_state *state)
86 if (!state || !state->data)
87 return 0;
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)
96 switch (op) {
97 case 0:
98 return 0;
99 case '<':
100 return '>';
101 case SPECIAL_UNSIGNED_LT:
102 return SPECIAL_UNSIGNED_GT;
103 case SPECIAL_LTE:
104 return SPECIAL_GTE;
105 case SPECIAL_UNSIGNED_LTE:
106 return SPECIAL_UNSIGNED_GTE;
107 case SPECIAL_EQUAL:
108 return SPECIAL_EQUAL;
109 case SPECIAL_NOTEQUAL:
110 return SPECIAL_NOTEQUAL;
111 case SPECIAL_GTE:
112 return SPECIAL_LTE;
113 case SPECIAL_UNSIGNED_GTE:
114 return SPECIAL_UNSIGNED_LTE;
115 case '>':
116 return '<';
117 case SPECIAL_UNSIGNED_GT:
118 return SPECIAL_UNSIGNED_LT;
119 default:
120 sm_msg("internal smatch bug. unhandled comparison %d", op);
121 return op;
125 int negate_comparison(int op)
127 switch (op) {
128 case 0:
129 return 0;
130 case '<':
131 return SPECIAL_GTE;
132 case SPECIAL_UNSIGNED_LT:
133 return SPECIAL_UNSIGNED_GTE;
134 case SPECIAL_LTE:
135 return '>';
136 case SPECIAL_UNSIGNED_LTE:
137 return SPECIAL_UNSIGNED_GT;
138 case SPECIAL_EQUAL:
139 return SPECIAL_NOTEQUAL;
140 case SPECIAL_NOTEQUAL:
141 return SPECIAL_EQUAL;
142 case SPECIAL_GTE:
143 return '<';
144 case SPECIAL_UNSIGNED_GTE:
145 return SPECIAL_UNSIGNED_LT;
146 case '>':
147 return SPECIAL_LTE;
148 case SPECIAL_UNSIGNED_GT:
149 return SPECIAL_UNSIGNED_LTE;
150 default:
151 sm_msg("internal smatch bug. unhandled comparison %d", op);
152 return 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)
161 return 0;
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)
174 return '<';
175 if (sval_cmp(left_max, right_min) == 0)
176 return SPECIAL_LTE;
177 if (sval_cmp(left_min, right_max) > 0)
178 return '>';
179 if (sval_cmp(left_min, right_max) == 0)
180 return SPECIAL_GTE;
182 return 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))
190 return 0;
191 if (!get_implied_rl(b, &right))
192 return 0;
194 return rl_comparison(left, right);
197 static struct range_list *get_orig_rl(struct var_sym_list *vsl)
199 struct symbol *sym;
200 struct smatch_state *state;
202 if (!vsl)
203 return NULL;
204 sym = vsl_to_sym(vsl);
205 if (!sym || !sym->ident)
206 return NULL;
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;
215 int op;
217 if (!data)
218 return &undefined;
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))
223 return &undefined;
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))
228 return &undefined;
230 op = rl_comparison(left_rl, right_rl);
231 if (op)
232 return alloc_compare_state(
233 data->left, data->left_var, data->left_vsl,
235 data->right, data->right_var, data->right_vsl);
237 return &undefined;
240 /* remove_unsigned_from_comparison() is obviously a hack. */
241 int remove_unsigned_from_comparison(int op)
243 switch (op) {
244 case SPECIAL_UNSIGNED_LT:
245 return '<';
246 case SPECIAL_UNSIGNED_LTE:
247 return SPECIAL_LTE;
248 case SPECIAL_UNSIGNED_GTE:
249 return SPECIAL_GTE;
250 case SPECIAL_UNSIGNED_GT:
251 return '>';
252 default:
253 return op;
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)
263 int LT, EQ, GT;
265 if (!one || !two)
266 return 0;
268 one = remove_unsigned_from_comparison(one);
269 two = remove_unsigned_from_comparison(two);
271 if (one == two)
272 return one;
274 LT = EQ = GT = 0;
276 switch (one) {
277 case '<':
278 LT = 1;
279 break;
280 case SPECIAL_LTE:
281 LT = 1;
282 EQ = 1;
283 break;
284 case SPECIAL_EQUAL:
285 EQ = 1;
286 break;
287 case SPECIAL_GTE:
288 GT = 1;
289 EQ = 1;
290 break;
291 case '>':
292 GT = 1;
295 switch (two) {
296 case '<':
297 LT = 1;
298 break;
299 case SPECIAL_LTE:
300 LT = 1;
301 EQ = 1;
302 break;
303 case SPECIAL_EQUAL:
304 EQ = 1;
305 break;
306 case SPECIAL_GTE:
307 GT = 1;
308 EQ = 1;
309 break;
310 case '>':
311 GT = 1;
314 if (LT && EQ && GT)
315 return 0;
316 if (LT && EQ)
317 return SPECIAL_LTE;
318 if (LT && GT)
319 return SPECIAL_NOTEQUAL;
320 if (LT)
321 return '<';
322 if (EQ && GT)
323 return SPECIAL_GTE;
324 if (GT)
325 return '>';
326 return 0;
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)
340 int LT, EQ, GT;
342 left_compare = remove_unsigned_from_comparison(left_compare);
343 right_compare = remove_unsigned_from_comparison(right_compare);
345 LT = EQ = GT = 0;
347 switch (left_compare) {
348 case '<':
349 LT++;
350 break;
351 case SPECIAL_LTE:
352 LT++;
353 EQ++;
354 break;
355 case SPECIAL_EQUAL:
356 return right_compare;
357 case SPECIAL_GTE:
358 GT++;
359 EQ++;
360 break;
361 case '>':
362 GT++;
365 switch (right_compare) {
366 case '<':
367 LT++;
368 break;
369 case SPECIAL_LTE:
370 LT++;
371 EQ++;
372 break;
373 case SPECIAL_EQUAL:
374 return left_compare;
375 case SPECIAL_GTE:
376 GT++;
377 EQ++;
378 break;
379 case '>':
380 GT++;
383 if (LT == 2) {
384 if (EQ == 2)
385 return SPECIAL_LTE;
386 return '<';
389 if (GT == 2) {
390 if (EQ == 2)
391 return SPECIAL_GTE;
392 return '>';
394 return 0;
397 int filter_comparison(int orig, int op)
399 if (orig == op)
400 return orig;
402 switch (orig) {
403 case 0:
404 return op;
405 case '<':
406 switch (op) {
407 case '<':
408 case SPECIAL_LTE:
409 case SPECIAL_NOTEQUAL:
410 return '<';
412 return 0;
413 case SPECIAL_LTE:
414 switch (op) {
415 case '<':
416 case SPECIAL_LTE:
417 case SPECIAL_EQUAL:
418 return op;
419 case SPECIAL_NOTEQUAL:
420 return '<';
422 return 0;
423 case SPECIAL_EQUAL:
424 switch (op) {
425 case SPECIAL_LTE:
426 case SPECIAL_EQUAL:
427 case SPECIAL_GTE:
428 case SPECIAL_UNSIGNED_LTE:
429 case SPECIAL_UNSIGNED_GTE:
430 return SPECIAL_EQUAL;
432 return 0;
433 case SPECIAL_NOTEQUAL:
434 switch (op) {
435 case '<':
436 case SPECIAL_LTE:
437 return '<';
438 case SPECIAL_UNSIGNED_LT:
439 case SPECIAL_UNSIGNED_LTE:
440 return SPECIAL_UNSIGNED_LT;
441 case SPECIAL_NOTEQUAL:
442 return op;
443 case '>':
444 case SPECIAL_GTE:
445 return '>';
446 case SPECIAL_UNSIGNED_GT:
447 case SPECIAL_UNSIGNED_GTE:
448 return SPECIAL_UNSIGNED_GT;
450 return 0;
451 case SPECIAL_GTE:
452 switch (op) {
453 case SPECIAL_LTE:
454 return SPECIAL_EQUAL;
455 case '>':
456 case SPECIAL_GTE:
457 case SPECIAL_EQUAL:
458 return op;
459 case SPECIAL_NOTEQUAL:
460 return '>';
462 return 0;
463 case '>':
464 switch (op) {
465 case '>':
466 case SPECIAL_GTE:
467 case SPECIAL_NOTEQUAL:
468 return '>';
470 return 0;
471 case SPECIAL_UNSIGNED_LT:
472 switch (op) {
473 case SPECIAL_UNSIGNED_LT:
474 case SPECIAL_UNSIGNED_LTE:
475 case SPECIAL_NOTEQUAL:
476 return SPECIAL_UNSIGNED_LT;
478 return 0;
479 case SPECIAL_UNSIGNED_LTE:
480 switch (op) {
481 case SPECIAL_UNSIGNED_LT:
482 case SPECIAL_UNSIGNED_LTE:
483 case SPECIAL_EQUAL:
484 return op;
485 case SPECIAL_NOTEQUAL:
486 return SPECIAL_UNSIGNED_LT;
487 case SPECIAL_UNSIGNED_GTE:
488 return SPECIAL_EQUAL;
490 return 0;
491 case SPECIAL_UNSIGNED_GTE:
492 switch (op) {
493 case SPECIAL_UNSIGNED_LTE:
494 return SPECIAL_EQUAL;
495 case SPECIAL_NOTEQUAL:
496 return SPECIAL_UNSIGNED_GT;
497 case SPECIAL_EQUAL:
498 case SPECIAL_UNSIGNED_GTE:
499 case SPECIAL_UNSIGNED_GT:
500 return op;
502 return 0;
503 case SPECIAL_UNSIGNED_GT:
504 switch (op) {
505 case SPECIAL_UNSIGNED_GT:
506 case SPECIAL_UNSIGNED_GTE:
507 case SPECIAL_NOTEQUAL:
508 return SPECIAL_UNSIGNED_GT;
510 return 0;
512 return 0;
515 struct smatch_state *merge_compare_states(struct smatch_state *s1, struct smatch_state *s2)
517 struct compare_data *data = s1->data;
518 int op;
520 op = merge_comparisons(state_to_comparison(s1), state_to_comparison(s2));
521 if (op)
522 return alloc_compare_state(
523 data->left, data->left_var, data->left_vsl,
525 data->right, data->right_var, data->right_vsl);
526 return &undefined;
529 static struct smatch_state *alloc_link_state(struct string_list *links)
531 struct smatch_state *state;
532 static char buf[256];
533 char *tmp;
534 int i;
536 state = __alloc_smatch_state(0);
538 i = 0;
539 FOR_EACH_PTR(links, tmp) {
540 if (!i++) {
541 snprintf(buf, sizeof(buf), "%s", tmp);
542 } else {
543 append(buf, ", ", sizeof(buf));
544 append(buf, tmp, sizeof(buf));
546 } END_FOR_EACH_PTR(tmp);
548 state->name = alloc_sname(buf);
549 state->data = links;
550 return state;
553 static void save_start_states(struct statement *stmt)
555 struct symbol *param;
556 char orig[64];
557 char state_name[128];
558 struct smatch_state *state;
559 struct string_list *links;
560 char *link;
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;
566 if (!param->ident)
567 continue;
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,
574 SPECIAL_EQUAL,
575 NULL, alloc_sname(orig), right_vsl);
576 set_state(compare_id, state_name, NULL, state);
578 link = alloc_sname(state_name);
579 links = NULL;
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);
593 return ret;
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;
600 char *new;
602 old_state = get_state(link_id, var, sym);
603 if (old_state)
604 links = clone_str_list(old_state->data);
605 else
606 links = NULL;
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;
620 char *tmp;
621 int flip;
622 int op;
624 links = sm->state->data;
626 FOR_EACH_PTR(links, tmp) {
627 state = get_state(compare_id, tmp, NULL);
628 if (!state)
629 continue;
630 data = state->data;
631 if (!data)
632 continue;
634 flip = 0;
635 if (strncmp(sm->name, tmp, strlen(sm->name)) != 0 ||
636 tmp[strlen(sm->name)] != ' ')
637 flip = 1;
639 op = state_to_comparison(state);
641 switch (flip ? flip_comparison(op) : op) {
642 case SPECIAL_EQUAL:
643 case SPECIAL_GTE:
644 case SPECIAL_UNSIGNED_GTE:
645 case '>':
646 case SPECIAL_UNSIGNED_GT:
647 new = alloc_compare_state(
648 data->left, data->left_var, data->left_vsl,
649 flip ? '<' : '>',
650 data->right, data->right_var, data->right_vsl);
651 set_state(compare_id, tmp, NULL, new);
652 break;
653 case '<':
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);
660 break;
661 default:
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;
671 char *tmp;
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)) {
679 case SPECIAL_EQUAL:
680 case SPECIAL_LTE:
681 case SPECIAL_UNSIGNED_LTE:
682 case '<':
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,
689 '<',
690 data->right, data->right_var, data->right_vsl);
691 set_state(compare_id, tmp, NULL, new);
692 break;
694 default:
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.
705 if (!mod_expr)
706 return;
707 if (mod_expr->type != EXPR_PREOP && mod_expr->type != EXPR_POSTOP)
708 return;
710 if (mod_expr->op == SPECIAL_INCREMENT)
711 match_inc(sm);
712 else if (mod_expr->op == SPECIAL_DECREMENT)
713 match_dec(sm);
716 static void match_modify(struct sm_state *sm, struct expression *mod_expr)
718 struct string_list *links;
719 char *tmp;
721 /* handled by match_inc_dec() */
722 if (mod_expr &&
723 ((mod_expr->type == EXPR_PREOP || mod_expr->type == EXPR_POSTOP) &&
724 (mod_expr->op == SPECIAL_INCREMENT || mod_expr->op == SPECIAL_DECREMENT)))
725 return;
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;
739 int op;
742 * This is an important special case. Say you have:
744 * if (++j == limit)
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))
760 return;
762 parent = expr_get_parent_expr(expr);
763 if (!parent)
764 return;
765 if (parent->type != EXPR_COMPARE || parent->op != SPECIAL_EQUAL)
766 return;
767 if (parent->left != expr)
768 return;
770 if (!get_implied_rl(expr->unop, &left) ||
771 !get_implied_rl(parent->right, &right))
772 return;
774 op = rl_comparison(left, right);
775 if (!op)
776 return;
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);
784 if (!expr)
785 return NULL;
786 if (sym)
787 *sym = NULL;
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) {
795 char buf[64];
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)
811 char *name;
812 struct symbol *sym;
813 struct smatch_state *ret;
815 name = chunk_to_var_sym(expr, &sym);
816 if (!name)
817 return NULL;
819 ret = get_state(owner, name, sym);
820 free_string(name);
821 return ret;
824 static void save_link(struct expression *expr, char *link)
826 char *var;
827 struct symbol *sym;
829 expr = strip_expr(expr);
830 if (expr->type == EXPR_BINOP) {
831 char *chunk;
833 chunk = chunk_to_var(expr);
834 if (!chunk)
835 return;
837 save_link(expr->left, link);
838 save_link(expr->right, link);
839 save_link_var_sym(chunk, NULL, link);
840 return;
843 var = chunk_to_var_sym(expr, &sym);
844 if (!var)
845 return;
847 save_link_var_sym(var, sym, link);
848 free_string(var);
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;
855 int flip = 0;
856 char state_name[256];
858 if (strcmp(left, right) > 0) {
859 const char *tmp = right;
861 flip = 1;
862 right = left;
863 left = tmp;
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)
869 return 0;
870 data = state->data;
871 if (flip)
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)
879 struct var_sym *tmp;
881 FOR_EACH_PTR(left_vsl, tmp) {
882 if (in_var_sym_list(right_vsl, tmp->var, tmp->sym))
883 return 1;
884 } END_FOR_EACH_PTR(tmp);
886 return 0;
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
895 * "a < b < c".
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;
911 int orig_comparison;
912 int right_comparison;
913 int true_comparison;
914 int false_comparison;
915 char *tmp;
916 char state_name[256];
917 struct var_sym *vs;
919 FOR_EACH_PTR(links, tmp) {
920 state = get_state_stree(pre_stree, compare_id, tmp, NULL);
921 if (!state || !state->data)
922 continue;
923 data = 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))
935 continue;
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;
954 right_var = tmp_var;
955 right_vsl = tmp_vsl;
956 true_comparison = flip_comparison(true_comparison);
957 false_comparison = flip_comparison(false_comparison);
960 if (!true_comparison && !false_comparison)
961 continue;
963 if (true_comparison)
964 true_state = alloc_compare_state(
965 left_expr, left_var, left_vsl,
966 true_comparison,
967 right_expr, right_var, right_vsl);
968 else
969 true_state = NULL;
970 if (false_comparison)
971 false_state = alloc_compare_state(
972 left_expr, left_var, left_vsl,
973 false_comparison,
974 right_expr, right_var, right_vsl);
975 else
976 false_state = NULL;
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));
1003 if (state)
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));
1007 if (state)
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 ||
1014 !mod_expr ||
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);
1018 else
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)
1024 sval_t sval;
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)
1030 return;
1032 if (!__cur_stmt || !__prev_stmt)
1033 return;
1034 if (__cur_stmt->type != STMT_ITERATOR)
1035 return;
1036 if (__cur_stmt->iterator_pre_condition != expr)
1037 return;
1039 /* literals are handled in smatch_extra.c */
1040 if (get_value(expr->right, &sval))
1041 return;
1043 /* First time checking the condition */
1044 if (__prev_stmt == __cur_stmt->iterator_pre_statement) {
1045 if (!get_implied_value(expr->left, &sval) ||
1046 sval.value != 0)
1047 return;
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);
1054 return;
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);
1062 return;
1065 /* Second time checking the condtion */
1066 if (__prev_stmt != __cur_stmt->iterator_post_statement)
1067 return;
1069 if (get_state_chunk(inc_dec_id, expr->left) != &incremented)
1070 return;
1072 data = false_state->data;
1073 false_state = alloc_compare_state(
1074 data->left, data->left_var, data->left_vsl,
1075 SPECIAL_EQUAL,
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)
1083 sval_t sval;
1085 if (expr->type != EXPR_BINOP || expr->op != '+')
1086 return 0;
1087 if (!get_implied_value(expr->right, &sval) || sval.value != 1)
1088 return 0;
1089 return 1;
1092 static int is_minus_one(struct expression *expr)
1094 sval_t sval;
1096 if (expr->type != EXPR_BINOP || expr->op != '-')
1097 return 0;
1098 if (!get_implied_value(expr->right, &sval) || sval.value != 1)
1099 return 0;
1100 return 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))
1117 return;
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))
1126 return;
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)
1134 char *left = NULL;
1135 char *right = NULL;
1136 struct symbol *left_sym, *right_sym;
1137 struct var_sym_list *left_vsl = NULL;
1138 struct var_sym_list *right_vsl = NULL;
1139 int false_op;
1140 int orig_comparison;
1141 struct smatch_state *true_state, *false_state;
1142 static char state_name[256];
1143 struct stree *pre_stree;
1144 sval_t sval;
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) &&
1155 sval.value == 0)
1156 false_op = SPECIAL_EQUAL;
1158 if (op == SPECIAL_UNSIGNED_GT &&
1159 get_implied_value(right_expr, &sval) &&
1160 sval.value == 0)
1161 false_op = SPECIAL_EQUAL;
1163 left = chunk_to_var_sym(left_expr, &left_sym);
1164 if (!left)
1165 goto free;
1166 if (left_sym)
1167 add_var_sym(&left_vsl, left, left_sym);
1168 else
1169 left_vsl = expr_to_vsl(left_expr);
1170 right = chunk_to_var_sym(right_expr, &right_sym);
1171 if (!right)
1172 goto free;
1173 if (right_sym)
1174 add_var_sym(&right_vsl, right, right_sym);
1175 else
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;
1183 left = right;
1184 left_vsl = right_vsl;
1185 left_expr = right_expr;
1186 right = tmp_name;
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,
1204 false_op,
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);
1216 if (_false_state)
1217 *_false_state = false_state;
1218 if (_state_name)
1219 *_state_name = state_name;
1220 free:
1221 free_string(left);
1222 free_string(right);
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;
1231 int redo, count;
1233 if (expr->type != EXPR_COMPARE)
1234 return;
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);
1254 redo = 0;
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);
1259 redo = 1;
1261 if (get_last_expr_from_expression_stmt(expr->right)) {
1262 right = get_last_expr_from_expression_stmt(expr->right);
1263 redo = 1;
1266 if (!redo)
1267 return;
1269 count = 0;
1270 while ((tmp = get_assigned_expr(left))) {
1271 if (count++ > 3)
1272 break;
1273 left = strip_expr(tmp);
1275 count = 0;
1276 while ((tmp = get_assigned_expr(right))) {
1277 if (count++ > 3)
1278 break;
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,
1288 int comparison,
1289 struct expression *right_expr,
1290 const char *right_name, struct var_sym_list *right_vsl)
1292 struct smatch_state *state;
1293 struct var_sym *vs;
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,
1312 comparison,
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);
1335 if (!left_name)
1336 goto free;
1337 left_vsl = expr_to_vsl(left);
1338 right_name = chunk_to_var_sym(right, &right_sym);
1339 if (!right_name)
1340 goto free;
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;
1349 left = right;
1350 left_name = right_name;
1351 left_sym = right_sym;
1352 left_vsl = right_vsl;
1353 right = tmp_expr;
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,
1362 comparison,
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);
1369 free:
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;
1402 int comparison;
1403 sval_t min;
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))
1410 return;
1412 comparison = get_comparison(r_left, r_right);
1414 switch (comparison) {
1415 case '>':
1416 case SPECIAL_GTE:
1417 if (implied_not_equal(r_right, 0))
1418 add_comparison(expr->left, '>', r_left);
1419 else
1420 add_comparison(expr->left, SPECIAL_GTE, r_left);
1421 return;
1425 static void match_assign_divide(struct expression *expr)
1427 struct expression *right;
1428 struct expression *r_left, *r_right;
1429 sval_t min;
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)
1435 return;
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;
1463 const char *var;
1464 struct var_sym_list *vsl;
1465 int comparison;
1466 char *tmp;
1468 left_var = chunk_to_var_sym(left, &left_sym);
1469 if (!left_var)
1470 goto done;
1471 left_vsl = expr_to_vsl(left);
1472 right_var = chunk_to_var_sym(right, &right_sym);
1473 if (!right_var)
1474 goto done;
1476 state = get_state(link_id, right_var, right_sym);
1477 if (!state)
1478 return;
1479 links = state->data;
1481 FOR_EACH_PTR(links, tmp) {
1482 state = get_state(compare_id, tmp, NULL);
1483 if (!state || !state->data)
1484 continue;
1485 data = state->data;
1486 comparison = data->comparison;
1487 expr = data->right;
1488 var = data->right_var;
1489 vsl = data->right_vsl;
1490 if (strcmp(var, right_var) == 0) {
1491 expr = data->left;
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)
1498 continue;
1499 add_comparison_var_sym(left, left_var, left_vsl, comparison, expr, var, vsl);
1500 } END_FOR_EACH_PTR(tmp);
1502 done:
1503 free_string(right_var);
1506 static void match_assign(struct expression *expr)
1508 struct expression *right;
1510 if (expr->op != '=')
1511 return;
1512 if (__in_fake_assign || outside_of_function())
1513 return;
1515 if (is_struct(expr->left))
1516 return;
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)
1528 char buf[256];
1529 struct smatch_state *state;
1530 int invert = 0;
1531 int ret = 0;
1533 if (!one || !two)
1534 return 0;
1536 if (strcmp(one, two) == 0)
1537 return SPECIAL_EQUAL;
1539 if (strcmp(one, two) > 0) {
1540 const char *tmp = one;
1542 one = two;
1543 two = tmp;
1544 invert = 1;
1547 snprintf(buf, sizeof(buf), "%s vs %s", one, two);
1548 state = get_state(compare_id, buf, NULL);
1549 if (state)
1550 ret = state_to_comparison(state);
1552 if (invert)
1553 ret = flip_comparison(ret);
1555 return ret;
1558 int get_comparison(struct expression *a, struct expression *b)
1560 char *one = NULL;
1561 char *two = NULL;
1562 int ret = 0;
1564 a = strip_parens(a);
1565 b = strip_parens(b);
1567 move_plus_to_minus(&a, &b);
1569 one = chunk_to_var(a);
1570 if (!one)
1571 goto free;
1572 two = chunk_to_var(b);
1573 if (!two)
1574 goto free;
1576 ret = get_comparison_strings(one, two);
1577 if (ret)
1578 goto free;
1580 if (is_plus_one(a) || is_minus_one(a)) {
1581 free_string(one);
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)) {
1585 free_string(two);
1586 two = chunk_to_var(b->left);
1587 ret = get_comparison_strings(one, two);
1590 if (!ret)
1591 goto free;
1593 if ((is_plus_one(a) || is_minus_one(b)) && ret == '<')
1594 ret = SPECIAL_LTE;
1595 else if ((is_minus_one(a) || is_plus_one(b)) && ret == '>')
1596 ret = SPECIAL_GTE;
1597 else
1598 ret = 0;
1600 free:
1601 free_string(one);
1602 free_string(two);
1604 if (!ret)
1605 return comparison_from_extra(a, b);
1606 return ret;
1609 int possible_comparison(struct expression *a, int comparison, struct expression *b)
1611 char *one = NULL;
1612 char *two = NULL;
1613 int ret = 0;
1614 char buf[256];
1615 struct sm_state *sm;
1616 int saved;
1618 one = chunk_to_var(a);
1619 if (!one)
1620 goto free;
1621 two = chunk_to_var(b);
1622 if (!two)
1623 goto free;
1626 if (strcmp(one, two) == 0 && comparison == SPECIAL_EQUAL) {
1627 ret = 1;
1628 goto free;
1631 if (strcmp(one, two) > 0) {
1632 char *tmp = one;
1634 one = two;
1635 two = tmp;
1636 comparison = flip_comparison(comparison);
1639 snprintf(buf, sizeof(buf), "%s vs %s", one, two);
1640 sm = get_sm_state(compare_id, buf, NULL);
1641 if (!sm)
1642 goto free;
1644 FOR_EACH_PTR(sm->possible, sm) {
1645 if (!sm->state->data)
1646 continue;
1647 saved = ((struct compare_data *)sm->state->data)->comparison;
1648 if (saved == comparison)
1649 ret = 1;
1650 if (comparison == SPECIAL_EQUAL &&
1651 (saved == SPECIAL_LTE ||
1652 saved == SPECIAL_GTE ||
1653 saved == SPECIAL_UNSIGNED_LTE ||
1654 saved == SPECIAL_UNSIGNED_GTE))
1655 ret = 1;
1656 if (ret == 1)
1657 goto free;
1658 } END_FOR_EACH_PTR(sm);
1660 return ret;
1661 free:
1662 free_string(one);
1663 free_string(two);
1664 return ret;
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;
1673 char *tmp;
1675 state = get_state_chunk(link_id, expr);
1676 if (!state)
1677 return NULL;
1678 links = state->data;
1680 FOR_EACH_PTR(links, tmp) {
1681 sm = get_sm_state(compare_id, tmp, NULL);
1682 if (!sm)
1683 continue;
1684 // FIXME have to compare name with vsl
1685 add_ptr_list(&ret, sm);
1686 } END_FOR_EACH_PTR(tmp);
1688 return ret;
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;
1697 char *tmp;
1699 state = get_state_chunk(link_id, expr);
1700 if (!state)
1701 return NULL;
1702 links = state->data;
1704 FOR_EACH_PTR(links, tmp) {
1705 sm = get_sm_state(compare_id, tmp, NULL);
1706 if (!sm)
1707 continue;
1708 if (!strchr(sm->state->name, '='))
1709 continue;
1710 if (strcmp(sm->state->name, "!=") == 0)
1711 continue;
1712 add_ptr_list(&ret, sm);
1713 } END_FOR_EACH_PTR(tmp);
1715 return ret;
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;
1725 char *link;
1727 return NULL;
1729 state = get_state_chunk(link_id, expr);
1730 if (!state)
1731 return NULL;
1732 links = state->data;
1734 FOR_EACH_PTR(links, link) {
1735 sm = get_sm_state(compare_id, link, NULL);
1736 if (!sm)
1737 continue;
1738 FOR_EACH_PTR(sm->possible, possible) {
1739 if (strcmp(possible->state->name, "!=") != 0)
1740 continue;
1741 add_ptr_list(&ret, sm);
1742 break;
1743 } END_FOR_EACH_PTR(link);
1744 } END_FOR_EACH_PTR(link);
1746 return ret;
1749 static void update_links_from_call(struct expression *left,
1750 int left_compare,
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;
1761 const char *var;
1762 struct var_sym_list *vsl;
1763 int comparison;
1764 char *tmp;
1766 left_var = chunk_to_var_sym(left, &left_sym);
1767 if (!left_var)
1768 goto done;
1769 left_vsl = expr_to_vsl(left);
1770 right_var = chunk_to_var_sym(right, &right_sym);
1771 if (!right_var)
1772 goto done;
1774 state = get_state(link_id, right_var, right_sym);
1775 if (!state)
1776 return;
1777 links = state->data;
1779 FOR_EACH_PTR(links, tmp) {
1780 state = get_state(compare_id, tmp, NULL);
1781 if (!state || !state->data)
1782 continue;
1783 data = state->data;
1784 comparison = data->comparison;
1785 expr = data->right;
1786 var = data->right_var;
1787 vsl = data->right_vsl;
1788 if (strcmp(var, right_var) == 0) {
1789 expr = data->left;
1790 var = data->left_var;
1791 vsl = data->left_vsl;
1792 comparison = flip_comparison(comparison);
1794 comparison = combine_comparisons(left_compare, comparison);
1795 if (!comparison)
1796 continue;
1797 add_comparison_var_sym(left, left_var, left_vsl, comparison, expr, var, vsl);
1798 } END_FOR_EACH_PTR(tmp);
1800 done:
1801 free_string(right_var);
1804 void __add_return_comparison(struct expression *call, const char *range)
1806 struct expression *arg;
1807 int comparison;
1808 char buf[4];
1810 if (!str_to_comparison_arg(range, call, &comparison, &arg))
1811 return;
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;
1825 int count, param;
1826 char buf[256];
1828 /* The return value for "return foo & param;" is <= param */
1830 count = 0;
1831 while ((tmp = get_assigned_expr(expr))) {
1832 expr = strip_expr(tmp);
1833 if (count++ > 4)
1834 break;
1837 if (expr->type != EXPR_BINOP || expr->op != '&')
1838 return NULL;
1840 right = strip_expr(expr->right);
1841 param = get_param_num(right);
1843 if (param == ignore)
1844 return NULL;
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;
1853 char *var = NULL;
1854 char buf[256];
1855 char *ret_str = NULL;
1856 int compare;
1857 int i;
1859 if (!expr)
1860 return NULL;
1862 var = chunk_to_var(expr);
1863 if (!var)
1864 goto try_mask;
1866 i = -1;
1867 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, param) {
1868 i++;
1869 if (i == ignore)
1870 continue;
1871 if (!param->ident)
1872 continue;
1873 snprintf(buf, sizeof(buf), "%s orig", param->ident->name);
1874 compare = get_comparison_strings(var, buf);
1875 if (!compare)
1876 continue;
1877 if (show_special(compare)[0] != starts_with)
1878 continue;
1879 snprintf(buf, sizeof(buf), "[%s$%d]", show_special(compare), i);
1880 ret_str = alloc_sname(buf);
1881 break;
1882 } END_FOR_EACH_PTR(param);
1884 free_string(var);
1885 if (!ret_str)
1886 goto try_mask;
1888 return ret_str;
1890 try_mask:
1891 if (starts_with == '<')
1892 ret_str = get_mask_comparison(expr, ignore);
1893 return ret_str;
1896 char *name_sym_to_param_comparison(const char *name, struct symbol *sym)
1898 struct symbol *param;
1899 char buf[256];
1900 int compare;
1901 int i;
1903 i = -1;
1904 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, param) {
1905 i++;
1906 if (!param->ident)
1907 continue;
1908 snprintf(buf, sizeof(buf), "%s orig", param->ident->name);
1909 compare = get_comparison_strings(name, buf);
1910 if (!compare)
1911 continue;
1912 snprintf(buf, sizeof(buf), "[%s$%d]", show_special(compare), i);
1913 return alloc_sname(buf);
1914 } END_FOR_EACH_PTR(param);
1916 return NULL;
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;
1932 char *var = NULL;
1933 char buf[256];
1934 char *ret_str = NULL;
1935 int compare;
1936 int i;
1938 var = chunk_to_var(expr);
1939 if (!var)
1940 goto free;
1942 i = -1;
1943 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, param) {
1944 i++;
1945 if (i == ignore)
1946 continue;
1947 if (!param->ident)
1948 continue;
1949 snprintf(buf, sizeof(buf), "%s orig", param->ident->name);
1950 compare = get_comparison_strings(var, buf);
1951 if (!compare)
1952 continue;
1953 snprintf(buf, sizeof(buf), "[%s$%d]", show_special(compare), i);
1954 ret_str = alloc_sname(buf);
1955 break;
1956 } END_FOR_EACH_PTR(param);
1958 free:
1959 free_string(var);
1960 return ret_str;
1963 char *get_printed_param_name(struct expression *call, const char *param_name, struct symbol *param_sym)
1965 struct expression *arg;
1966 char *name;
1967 struct symbol *sym;
1968 static char buf[256];
1969 int len;
1970 int i;
1972 i = -1;
1973 FOR_EACH_PTR(call->args, arg) {
1974 i++;
1976 name = expr_to_var_sym(arg, &sym);
1977 if (!name || !sym)
1978 continue;
1979 if (sym != param_sym)
1980 continue;
1982 len = strlen(name);
1983 if (strncmp(name, param_name, len) != 0)
1984 continue;
1985 if (param_name[len] == '\0') {
1986 snprintf(buf, sizeof(buf), "$%d", i);
1987 return buf;
1989 if (param_name[len] != '-')
1990 continue;
1991 snprintf(buf, sizeof(buf), "$%d%s", i, param_name + len);
1992 return buf;
1993 } END_FOR_EACH_PTR(arg);
1995 return NULL;
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;
2004 int comparison;
2005 struct string_list *links;
2006 char *arg_name;
2007 const char *right_name;
2008 char *link;
2009 char info_buf[256];
2010 int i;
2012 i = -1;
2013 FOR_EACH_PTR(expr->args, arg) {
2014 i++;
2016 state = get_state_chunk(link_id, arg);
2017 if (!state)
2018 continue;
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"))
2027 continue;
2028 sm = get_sm_state(compare_id, link, NULL);
2029 if (!sm)
2030 continue;
2031 data = sm->state->data;
2032 if (!data || !data->comparison)
2033 continue;
2034 arg_name = expr_to_var(arg);
2035 if (!arg_name)
2036 continue;
2038 right_vsl = NULL;
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)
2049 goto free;
2051 right_vs = first_ptr_list((struct ptr_list *)right_vsl);
2052 if (strcmp(right_vs->var, right_name) != 0)
2053 goto free;
2054 right_name = get_printed_param_name(expr, right_vs->var, right_vs->sym);
2055 if (!right_name)
2056 goto free;
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);
2060 free:
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;
2070 char *link;
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"))
2077 return;
2079 links = link_sm->state->data;
2080 FOR_EACH_PTR(links, link) {
2081 compare_sm = get_sm_state(compare_id, link, NULL);
2082 if (!compare_sm)
2083 continue;
2084 data = compare_sm->state->data;
2085 if (!data || !data->comparison)
2086 continue;
2088 if (ptr_list_size((struct ptr_list *)data->left_vsl) != 1 ||
2089 ptr_list_size((struct ptr_list *)data->right_vsl) != 1)
2090 continue;
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)
2095 continue;
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)
2102 continue;
2104 right_name = get_printed_param_name(call, right->var, right->sym);
2105 if (!right_name)
2106 continue;
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)
2114 char *name;
2115 const char *tmp_name;
2116 struct symbol *sym;
2117 int param;
2118 char info_buf[256];
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.
2130 if (!expr)
2131 return;
2132 name = expr_to_var_sym(expr, &sym);
2133 if (!name || !sym)
2134 goto free;
2136 param = get_param_num_from_sym(sym);
2137 if (param < 0)
2138 goto free;
2139 if (param_was_set_var_sym(name, sym))
2140 goto free;
2142 tmp_name = get_param_name_var_sym(name, sym);
2143 if (!tmp_name)
2144 goto free;
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);
2149 free:
2150 free_string(name);
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;
2157 char *link;
2158 struct sm_state *sm;
2159 struct compare_data *data;
2160 struct var_sym *left, *right;
2161 int left_param, right_param;
2162 char left_buf[256];
2163 char right_buf[256];
2164 char info_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)
2171 continue;
2172 links = tmp->state->data;
2173 FOR_EACH_PTR(links, link) {
2174 sm = get_sm_state(compare_id, link, NULL);
2175 if (!sm)
2176 continue;
2177 data = sm->state->data;
2178 if (!data || !data->comparison)
2179 continue;
2180 if (ptr_list_size((struct ptr_list *)data->left_vsl) != 1 ||
2181 ptr_list_size((struct ptr_list *)data->right_vsl) != 1)
2182 continue;
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)
2187 continue;
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)
2194 continue;
2196 if (strstr(right->var, " orig"))
2197 continue;
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)
2202 continue;
2204 tmp_name = get_param_name_var_sym(left->var, left->sym);
2205 if (!tmp_name)
2206 continue;
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] != '$')
2211 continue;
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)
2231 *op = **value;
2233 switch (*op) {
2234 case '<':
2235 (*value)++;
2236 if (**value == '=') {
2237 (*value)++;
2238 *op = SPECIAL_LTE;
2240 break;
2241 case '=':
2242 (*value)++;
2243 (*value)++;
2244 *op = SPECIAL_EQUAL;
2245 break;
2246 case '!':
2247 (*value)++;
2248 (*value)++;
2249 *op = SPECIAL_NOTEQUAL;
2250 break;
2251 case '>':
2252 (*value)++;
2253 if (**value == '=') {
2254 (*value)++;
2255 *op = SPECIAL_GTE;
2257 break;
2258 default:
2259 return 0;
2262 if (**value != ' ') {
2263 sm_msg("internal error parsing comparison. %s", *value);
2264 return 0;
2267 (*value)++;
2268 return 1;
2271 static int split_op_param_key(char *value, int *op, int *param, char **key)
2273 static char buf[256];
2274 char *p;
2276 if (!parse_comparison(&value, op))
2277 return 0;
2279 snprintf(buf, sizeof(buf), value);
2281 p = buf;
2282 if (*p++ != '$')
2283 return 0;
2285 *param = atoi(p);
2286 if (*param < 0 || *param > 99)
2287 return 0;
2288 p++;
2289 if (*param > 9)
2290 p++;
2291 p--;
2292 *p = '$';
2293 *key = p;
2295 return 1;
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;
2305 int op;
2306 int right_param;
2307 char *right_key;
2308 struct var_sym_list *left_vsl = NULL, *right_vsl = NULL;
2310 if (left_param == -1) {
2311 if (expr->type != EXPR_ASSIGNMENT)
2312 return;
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)
2318 return;
2319 } else {
2320 while (expr->type == EXPR_ASSIGNMENT)
2321 expr = strip_expr(expr->right);
2322 if (expr->type != EXPR_CALL)
2323 return;
2325 left_arg = get_argument_from_call_expr(expr->args, left_param);
2326 if (!left_arg)
2327 return;
2330 if (!split_op_param_key(value, &op, &right_param, &right_key))
2331 return;
2333 right_arg = get_argument_from_call_expr(expr->args, right_param);
2334 if (!right_arg)
2335 return;
2337 left_name = get_variable_from_key(left_arg, key, &left_sym);
2338 if (!left_name || !left_sym)
2339 goto free;
2341 right_name = get_variable_from_key(right_arg, right_key, &right_sym);
2342 if (!right_name || !right_sym)
2343 goto free;
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);
2350 free:
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;
2362 int op, state_op;
2363 int right_param;
2364 char *right_key;
2365 int ret = 0;
2366 char buf[256];
2368 while (expr->type == EXPR_ASSIGNMENT)
2369 expr = strip_expr(expr->right);
2370 if (expr->type != EXPR_CALL)
2371 return 0;
2373 if (!split_op_param_key(value, &op, &right_param, &right_key))
2374 return 0;
2376 left_arg = get_argument_from_call_expr(expr->args, left_param);
2377 if (!left_arg)
2378 return 0;
2380 right_arg = get_argument_from_call_expr(expr->args, right_param);
2381 if (!right_arg)
2382 return 0;
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)
2387 goto free;
2389 snprintf(buf, sizeof(buf), "%s vs %s", left_name, right_name);
2390 state = get_state(compare_id, buf, NULL);
2391 if (!state)
2392 goto free;
2393 state_op = state_to_comparison(state);
2394 if (!state_op)
2395 goto free;
2397 if (!filter_comparison(remove_unsigned_from_comparison(state_op), op))
2398 ret = 1;
2399 free:
2400 free_string(left_name);
2401 free_string(right_name);
2402 return ret;
2405 int impossibly_high_comparison(struct expression *expr)
2407 struct smatch_state *link_state;
2408 struct sm_state *sm;
2409 struct string_list *links;
2410 char *link;
2411 struct compare_data *data;
2413 link_state = get_state_expr(link_id, expr);
2414 if (!link_state) {
2415 if (expr->type == EXPR_BINOP &&
2416 (impossibly_high_comparison(expr->left) ||
2417 impossibly_high_comparison(expr->right)))
2418 return 1;
2419 return 0;
2422 links = link_state->data;
2423 FOR_EACH_PTR(links, link) {
2424 sm = get_sm_state(compare_id, link, NULL);
2425 if (!sm)
2426 continue;
2427 data = sm->state->data;
2428 if (!data)
2429 continue;
2430 if (!possibly_true(data->left, data->comparison, data->right))
2431 return 1;
2432 } END_FOR_EACH_PTR(link);
2434 return 0;
2437 static void free_data(struct symbol *sym)
2439 if (__inline_fn)
2440 return;
2441 clear_compare_data_alloc();
2444 void register_comparison(int id)
2446 compare_id = 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)
2465 link_id = 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)
2475 inc_dec_id = 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;
2490 int istrue = 0;
2491 int isfalse = 0;
2493 if (!sm)
2494 return;
2495 data = sm->state->data;
2496 if (!data) {
2497 if (sm->merged) {
2498 filter_by_sm(sm->left, op, true_stack, false_stack);
2499 filter_by_sm(sm->right, op, true_stack, false_stack);
2501 return;
2504 if (data->comparison &&
2505 data->comparison == filter_comparison(data->comparison, op))
2506 istrue = 1;
2508 if (data->comparison &&
2509 data->comparison == filter_comparison(data->comparison, negate_comparison(op)))
2510 isfalse = 1;
2512 if (istrue)
2513 add_ptr_list(true_stack, sm);
2514 if (isfalse)
2515 add_ptr_list(false_stack, sm);
2517 if (sm->merged) {
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;
2528 char *left, *right;
2529 int op;
2530 static char buf[256];
2532 if (expr->type != EXPR_COMPARE)
2533 return NULL;
2535 op = expr->op;
2537 left = expr_to_var(expr->left);
2538 right = expr_to_var(expr->right);
2539 if (!left || !right) {
2540 free_string(left);
2541 free_string(right);
2542 return NULL;
2545 if (strcmp(left, right) > 0) {
2546 char *tmp = left;
2548 left = right;
2549 right = tmp;
2550 op = flip_comparison(op);
2553 snprintf(buf, sizeof(buf), "%s vs %s", left, right);
2554 sm = get_sm_state(compare_id, buf, NULL);
2555 if (!sm)
2556 return NULL;
2557 if (!sm->merged)
2558 return NULL;
2560 filter_by_sm(sm, op, true_stack, false_stack);
2561 if (!*true_stack && !*false_stack)
2562 return NULL;
2564 if (option_debug)
2565 sm_msg("implications from comparison: (%s)", show_sm(sm));
2567 return sm;