user_data2: remove some debug printfs
[smatch.git] / smatch_comparison.c
blob84e30ca985cb6fd0135b7b4ac960febc7554c701
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 /* for handling for loops */
41 STATE(start);
42 STATE(incremented);
44 ALLOCATOR(compare_data, "compare data");
46 static struct symbol *vsl_to_sym(struct var_sym_list *vsl)
48 struct var_sym *vs;
50 if (!vsl)
51 return NULL;
52 if (ptr_list_size((struct ptr_list *)vsl) != 1)
53 return NULL;
54 vs = first_ptr_list((struct ptr_list *)vsl);
55 return vs->sym;
58 struct smatch_state *alloc_compare_state(
59 const char *var1, struct var_sym_list *vsl1,
60 int comparison,
61 const char *var2, struct var_sym_list *vsl2)
63 struct smatch_state *state;
64 struct compare_data *data;
66 state = __alloc_smatch_state(0);
67 state->name = alloc_sname(show_special(comparison));
68 data = __alloc_compare_data(0);
69 data->var1 = alloc_sname(var1);
70 data->vsl1 = clone_var_sym_list(vsl1);
71 data->comparison = comparison;
72 data->var2 = alloc_sname(var2);
73 data->vsl2 = clone_var_sym_list(vsl2);
74 state->data = data;
75 return state;
78 int state_to_comparison(struct smatch_state *state)
80 if (!state || !state->data)
81 return 0;
82 return ((struct compare_data *)state->data)->comparison;
86 * flip_comparison() reverses the op left and right. So "x >= y" becomes "y <= x".
88 int flip_comparison(int op)
90 switch (op) {
91 case 0:
92 return 0;
93 case '<':
94 return '>';
95 case SPECIAL_UNSIGNED_LT:
96 return SPECIAL_UNSIGNED_GT;
97 case SPECIAL_LTE:
98 return SPECIAL_GTE;
99 case SPECIAL_UNSIGNED_LTE:
100 return SPECIAL_UNSIGNED_GTE;
101 case SPECIAL_EQUAL:
102 return SPECIAL_EQUAL;
103 case SPECIAL_NOTEQUAL:
104 return SPECIAL_NOTEQUAL;
105 case SPECIAL_GTE:
106 return SPECIAL_LTE;
107 case SPECIAL_UNSIGNED_GTE:
108 return SPECIAL_UNSIGNED_LTE;
109 case '>':
110 return '<';
111 case SPECIAL_UNSIGNED_GT:
112 return SPECIAL_UNSIGNED_LT;
113 default:
114 sm_msg("internal smatch bug. unhandled comparison %d", op);
115 return op;
119 int negate_comparison(int op)
121 switch (op) {
122 case 0:
123 return 0;
124 case '<':
125 return SPECIAL_GTE;
126 case SPECIAL_UNSIGNED_LT:
127 return SPECIAL_UNSIGNED_GTE;
128 case SPECIAL_LTE:
129 return '>';
130 case SPECIAL_UNSIGNED_LTE:
131 return SPECIAL_UNSIGNED_GT;
132 case SPECIAL_EQUAL:
133 return SPECIAL_NOTEQUAL;
134 case SPECIAL_NOTEQUAL:
135 return SPECIAL_EQUAL;
136 case SPECIAL_GTE:
137 return '<';
138 case SPECIAL_UNSIGNED_GTE:
139 return SPECIAL_UNSIGNED_LT;
140 case '>':
141 return SPECIAL_LTE;
142 case SPECIAL_UNSIGNED_GT:
143 return SPECIAL_UNSIGNED_LTE;
144 default:
145 sm_msg("internal smatch bug. unhandled comparison %d", op);
146 return op;
150 static int rl_comparison(struct range_list *left_rl, struct range_list *right_rl)
152 sval_t left_min, left_max, right_min, right_max;
154 if (!left_rl || !right_rl)
155 return 0;
157 left_min = rl_min(left_rl);
158 left_max = rl_max(left_rl);
159 right_min = rl_min(right_rl);
160 right_max = rl_max(right_rl);
162 if (left_min.value == left_max.value &&
163 right_min.value == right_max.value &&
164 left_min.value == right_min.value)
165 return SPECIAL_EQUAL;
167 if (sval_cmp(left_max, right_min) < 0)
168 return '<';
169 if (sval_cmp(left_max, right_min) == 0)
170 return SPECIAL_LTE;
171 if (sval_cmp(left_min, right_max) > 0)
172 return '>';
173 if (sval_cmp(left_min, right_max) == 0)
174 return SPECIAL_GTE;
176 return 0;
179 static struct range_list *get_orig_rl(struct var_sym_list *vsl)
181 struct symbol *sym;
182 struct smatch_state *state;
184 if (!vsl)
185 return NULL;
186 sym = vsl_to_sym(vsl);
187 if (!sym || !sym->ident)
188 return NULL;
189 state = get_orig_estate(sym->ident->name, sym);
190 return estate_rl(state);
193 static struct smatch_state *unmatched_comparison(struct sm_state *sm)
195 struct compare_data *data = sm->state->data;
196 struct range_list *left_rl, *right_rl;
197 int op;
199 if (!data)
200 return &undefined;
202 if (strstr(data->var1, " orig"))
203 left_rl = get_orig_rl(data->vsl1);
204 else if (!get_implied_rl_var_sym(data->var1, vsl_to_sym(data->vsl1), &left_rl))
205 return &undefined;
206 if (strstr(data->var2, " orig"))
207 right_rl = get_orig_rl(data->vsl2);
208 else if (!get_implied_rl_var_sym(data->var2, vsl_to_sym(data->vsl2), &right_rl))
209 return &undefined;
212 op = rl_comparison(left_rl, right_rl);
213 if (op)
214 return alloc_compare_state(data->var1, data->vsl1, op, data->var2, data->vsl2);
216 return &undefined;
219 /* remove_unsigned_from_comparison() is obviously a hack. */
220 static int remove_unsigned_from_comparison(int op)
222 switch (op) {
223 case SPECIAL_UNSIGNED_LT:
224 return '<';
225 case SPECIAL_UNSIGNED_LTE:
226 return SPECIAL_LTE;
227 case SPECIAL_UNSIGNED_GTE:
228 return SPECIAL_GTE;
229 case SPECIAL_UNSIGNED_GT:
230 return '>';
231 default:
232 return op;
237 * This is for when you merge states "a < b" and "a == b", the result is that
238 * we can say for sure, "a <= b" after the merge.
240 int merge_comparisons(int one, int two)
242 int LT, EQ, GT;
244 one = remove_unsigned_from_comparison(one);
245 two = remove_unsigned_from_comparison(two);
247 if (one == two)
248 return one;
250 LT = EQ = GT = 0;
252 switch (one) {
253 case '<':
254 LT = 1;
255 break;
256 case SPECIAL_LTE:
257 LT = 1;
258 EQ = 1;
259 break;
260 case SPECIAL_EQUAL:
261 EQ = 1;
262 break;
263 case SPECIAL_GTE:
264 GT = 1;
265 EQ = 1;
266 break;
267 case '>':
268 GT = 1;
271 switch (two) {
272 case '<':
273 LT = 1;
274 break;
275 case SPECIAL_LTE:
276 LT = 1;
277 EQ = 1;
278 break;
279 case SPECIAL_EQUAL:
280 EQ = 1;
281 break;
282 case SPECIAL_GTE:
283 GT = 1;
284 EQ = 1;
285 break;
286 case '>':
287 GT = 1;
290 if (LT && EQ && GT)
291 return 0;
292 if (LT && EQ)
293 return SPECIAL_LTE;
294 if (LT && GT)
295 return SPECIAL_NOTEQUAL;
296 if (LT)
297 return '<';
298 if (EQ && GT)
299 return SPECIAL_GTE;
300 if (GT)
301 return '>';
302 return 0;
306 * This is for if you have "a < b" and "b <= c". Or in other words,
307 * "a < b <= c". You would call this like get_combined_comparison('<', '<=').
308 * The return comparison would be '<'.
310 * This function is different from merge_comparisons(), for example:
311 * merge_comparison('<', '==') returns '<='
312 * get_combined_comparison('<', '==') returns '<'
314 int combine_comparisons(int left_compare, int right_compare)
316 int LT, EQ, GT;
318 left_compare = remove_unsigned_from_comparison(left_compare);
319 right_compare = remove_unsigned_from_comparison(right_compare);
321 LT = EQ = GT = 0;
323 switch (left_compare) {
324 case '<':
325 LT++;
326 break;
327 case SPECIAL_LTE:
328 LT++;
329 EQ++;
330 break;
331 case SPECIAL_EQUAL:
332 return right_compare;
333 case SPECIAL_GTE:
334 GT++;
335 EQ++;
336 break;
337 case '>':
338 GT++;
341 switch (right_compare) {
342 case '<':
343 LT++;
344 break;
345 case SPECIAL_LTE:
346 LT++;
347 EQ++;
348 break;
349 case SPECIAL_EQUAL:
350 return left_compare;
351 case SPECIAL_GTE:
352 GT++;
353 EQ++;
354 break;
355 case '>':
356 GT++;
359 if (LT == 2) {
360 if (EQ == 2)
361 return SPECIAL_LTE;
362 return '<';
365 if (GT == 2) {
366 if (EQ == 2)
367 return SPECIAL_GTE;
368 return '>';
370 return 0;
373 static int filter_comparison(int orig, int op)
375 if (orig == op)
376 return orig;
378 switch (orig) {
379 case 0:
380 return op;
381 case '<':
382 switch (op) {
383 case '<':
384 case SPECIAL_LTE:
385 case SPECIAL_NOTEQUAL:
386 return '<';
388 return 0;
389 case SPECIAL_LTE:
390 switch (op) {
391 case '<':
392 case SPECIAL_LTE:
393 case SPECIAL_EQUAL:
394 return op;
395 case SPECIAL_NOTEQUAL:
396 return '<';
398 return 0;
399 case SPECIAL_EQUAL:
400 switch (op) {
401 case SPECIAL_LTE:
402 case SPECIAL_EQUAL:
403 case SPECIAL_GTE:
404 case SPECIAL_UNSIGNED_LTE:
405 case SPECIAL_UNSIGNED_GTE:
406 return SPECIAL_EQUAL;
408 return 0;
409 case SPECIAL_NOTEQUAL:
410 switch (op) {
411 case '<':
412 case SPECIAL_LTE:
413 return '<';
414 case SPECIAL_UNSIGNED_LT:
415 case SPECIAL_UNSIGNED_LTE:
416 return SPECIAL_UNSIGNED_LT;
417 case SPECIAL_NOTEQUAL:
418 return op;
419 case '>':
420 case SPECIAL_GTE:
421 return '>';
422 case SPECIAL_UNSIGNED_GT:
423 case SPECIAL_UNSIGNED_GTE:
424 return SPECIAL_UNSIGNED_GT;
426 return 0;
427 case SPECIAL_GTE:
428 switch (op) {
429 case SPECIAL_LTE:
430 return SPECIAL_EQUAL;
431 case '>':
432 case SPECIAL_GTE:
433 case SPECIAL_EQUAL:
434 return op;
435 case SPECIAL_NOTEQUAL:
436 return '>';
438 return 0;
439 case '>':
440 switch (op) {
441 case '>':
442 case SPECIAL_GTE:
443 case SPECIAL_NOTEQUAL:
444 return '>';
446 return 0;
447 case SPECIAL_UNSIGNED_LT:
448 switch (op) {
449 case SPECIAL_UNSIGNED_LT:
450 case SPECIAL_UNSIGNED_LTE:
451 case SPECIAL_NOTEQUAL:
452 return SPECIAL_UNSIGNED_LT;
454 return 0;
455 case SPECIAL_UNSIGNED_LTE:
456 switch (op) {
457 case SPECIAL_UNSIGNED_LT:
458 case SPECIAL_UNSIGNED_LTE:
459 case SPECIAL_EQUAL:
460 return op;
461 case SPECIAL_NOTEQUAL:
462 return SPECIAL_UNSIGNED_LT;
463 case SPECIAL_UNSIGNED_GTE:
464 return SPECIAL_EQUAL;
466 return 0;
467 case SPECIAL_UNSIGNED_GTE:
468 switch (op) {
469 case SPECIAL_UNSIGNED_LTE:
470 return SPECIAL_EQUAL;
471 case SPECIAL_NOTEQUAL:
472 return SPECIAL_UNSIGNED_GT;
473 case SPECIAL_EQUAL:
474 case SPECIAL_UNSIGNED_GTE:
475 case SPECIAL_UNSIGNED_GT:
476 return op;
478 return 0;
479 case SPECIAL_UNSIGNED_GT:
480 switch (op) {
481 case SPECIAL_UNSIGNED_GT:
482 case SPECIAL_UNSIGNED_GTE:
483 case SPECIAL_NOTEQUAL:
484 return SPECIAL_UNSIGNED_GT;
486 return 0;
488 return 0;
491 struct smatch_state *merge_compare_states(struct smatch_state *s1, struct smatch_state *s2)
493 struct compare_data *data = s1->data;
494 int op;
496 op = merge_comparisons(state_to_comparison(s1), state_to_comparison(s2));
497 if (op)
498 return alloc_compare_state(data->var1, data->vsl1, op, data->var2, data->vsl2);
499 return &undefined;
502 static struct smatch_state *alloc_link_state(struct string_list *links)
504 struct smatch_state *state;
505 static char buf[256];
506 char *tmp;
507 int i;
509 state = __alloc_smatch_state(0);
511 i = 0;
512 FOR_EACH_PTR(links, tmp) {
513 if (!i++) {
514 snprintf(buf, sizeof(buf), "%s", tmp);
515 } else {
516 append(buf, ", ", sizeof(buf));
517 append(buf, tmp, sizeof(buf));
519 } END_FOR_EACH_PTR(tmp);
521 state->name = alloc_sname(buf);
522 state->data = links;
523 return state;
526 static void save_start_states(struct statement *stmt)
528 struct symbol *param;
529 char orig[64];
530 char state_name[128];
531 struct smatch_state *state;
532 struct string_list *links;
533 char *link;
535 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, param) {
536 struct var_sym_list *vsl1 = NULL;
537 struct var_sym_list *vsl2 = NULL;
539 if (!param->ident)
540 continue;
541 snprintf(orig, sizeof(orig), "%s orig", param->ident->name);
542 snprintf(state_name, sizeof(state_name), "%s vs %s", param->ident->name, orig);
543 add_var_sym(&vsl1, param->ident->name, param);
544 add_var_sym(&vsl2, orig, param);
545 state = alloc_compare_state(param->ident->name, vsl1, SPECIAL_EQUAL, alloc_sname(orig), vsl2);
546 set_state(compare_id, state_name, NULL, state);
548 link = alloc_sname(state_name);
549 links = NULL;
550 insert_string(&links, link);
551 state = alloc_link_state(links);
552 set_state(link_id, param->ident->name, param, state);
553 } END_FOR_EACH_PTR(param);
556 static struct smatch_state *merge_links(struct smatch_state *s1, struct smatch_state *s2)
558 struct smatch_state *ret;
559 struct string_list *links;
561 links = combine_string_lists(s1->data, s2->data);
562 ret = alloc_link_state(links);
563 return ret;
566 static void save_link_var_sym(const char *var, struct symbol *sym, const char *link)
568 struct smatch_state *old_state, *new_state;
569 struct string_list *links;
570 char *new;
572 old_state = get_state(link_id, var, sym);
573 if (old_state)
574 links = clone_str_list(old_state->data);
575 else
576 links = NULL;
578 new = alloc_sname(link);
579 insert_string(&links, new);
581 new_state = alloc_link_state(links);
582 set_state(link_id, var, sym, new_state);
585 static void match_inc(struct sm_state *sm)
587 struct string_list *links;
588 struct smatch_state *state;
589 char *tmp;
591 links = sm->state->data;
593 FOR_EACH_PTR(links, tmp) {
594 state = get_state(compare_id, tmp, NULL);
596 switch (state_to_comparison(state)) {
597 case SPECIAL_EQUAL:
598 case SPECIAL_GTE:
599 case SPECIAL_UNSIGNED_GTE:
600 case '>':
601 case SPECIAL_UNSIGNED_GT: {
602 struct compare_data *data = state->data;
603 struct smatch_state *new;
605 new = alloc_compare_state(data->var1, data->vsl1, '>', data->var2, data->vsl2);
606 set_state(compare_id, tmp, NULL, new);
607 break;
609 default:
610 set_state(compare_id, tmp, NULL, &undefined);
612 } END_FOR_EACH_PTR(tmp);
615 static void match_dec(struct sm_state *sm)
617 struct string_list *links;
618 struct smatch_state *state;
619 char *tmp;
621 links = sm->state->data;
623 FOR_EACH_PTR(links, tmp) {
624 state = get_state(compare_id, tmp, NULL);
626 switch (state_to_comparison(state)) {
627 case SPECIAL_EQUAL:
628 case SPECIAL_LTE:
629 case SPECIAL_UNSIGNED_LTE:
630 case '<':
631 case SPECIAL_UNSIGNED_LT: {
632 struct compare_data *data = state->data;
633 struct smatch_state *new;
635 new = alloc_compare_state(data->var1, data->vsl1, '<', data->var2, data->vsl2);
636 set_state(compare_id, tmp, NULL, new);
637 break;
639 default:
640 set_state(compare_id, tmp, NULL, &undefined);
642 } END_FOR_EACH_PTR(tmp);
645 static int match_inc_dec(struct sm_state *sm, struct expression *mod_expr)
647 if (!mod_expr)
648 return 0;
649 if (mod_expr->type != EXPR_PREOP && mod_expr->type != EXPR_POSTOP)
650 return 0;
652 if (mod_expr->op == SPECIAL_INCREMENT) {
653 match_inc(sm);
654 return 1;
656 if (mod_expr->op == SPECIAL_DECREMENT) {
657 match_dec(sm);
658 return 1;
660 return 0;
663 static void match_modify(struct sm_state *sm, struct expression *mod_expr)
665 struct string_list *links;
666 char *tmp;
669 * if (foo > bar) then ++foo is also > bar.
671 if (match_inc_dec(sm, mod_expr))
672 return;
674 links = sm->state->data;
676 FOR_EACH_PTR(links, tmp) {
677 set_state(compare_id, tmp, NULL, &undefined);
678 } END_FOR_EACH_PTR(tmp);
679 set_state(link_id, sm->name, sm->sym, &undefined);
682 static char *chunk_to_var_sym(struct expression *expr, struct symbol **sym)
684 expr = strip_expr(expr);
685 if (!expr)
686 return NULL;
687 if (sym)
688 *sym = NULL;
690 if (expr->type == EXPR_PREOP &&
691 (expr->op == SPECIAL_INCREMENT ||
692 expr->op == SPECIAL_DECREMENT))
693 expr = strip_expr(expr->unop);
695 return expr_to_chunk_sym_vsl(expr, sym, NULL);
698 static char *chunk_to_var(struct expression *expr)
700 return chunk_to_var_sym(expr, NULL);
703 static void save_link(struct expression *expr, char *link)
705 char *var;
706 struct symbol *sym;
708 expr = strip_expr(expr);
709 if (expr->type == EXPR_BINOP) {
710 char *chunk;
712 chunk = chunk_to_var(expr);
713 if (!chunk)
714 return;
716 save_link(expr->left, link);
717 save_link(expr->right, link);
718 save_link_var_sym(chunk, NULL, link);
719 return;
722 var = expr_to_var_sym(expr, &sym);
723 if (!var || !sym)
724 goto done;
726 save_link_var_sym(var, sym, link);
727 done:
728 free_string(var);
731 static int get_orig_comparison(struct stree *pre_stree, const char *left, const char *right)
733 struct smatch_state *state;
734 struct compare_data *data;
735 int flip = 0;
736 char state_name[256];
738 if (strcmp(left, right) > 0) {
739 const char *tmp = right;
741 flip = 1;
742 right = left;
743 left = tmp;
746 snprintf(state_name, sizeof(state_name), "%s vs %s", left, right);
747 state = get_state_stree(pre_stree, compare_id, state_name, NULL);
748 if (!state || !state->data)
749 return 0;
750 data = state->data;
751 if (flip)
752 return flip_comparison(data->comparison);
753 return data->comparison;
757 static int have_common_var_sym(struct var_sym_list *left_vsl, struct var_sym_list *right_vsl)
759 struct var_sym *tmp;
761 FOR_EACH_PTR(left_vsl, tmp) {
762 if (in_var_sym_list(right_vsl, tmp->var, tmp->sym))
763 return 1;
764 } END_FOR_EACH_PTR(tmp);
766 return 0;
770 * The idea here is that we take a comparison "a < b" and then we look at all
771 * the things which "b" is compared against "b < c" and we say that that implies
772 * a relationship "a < c".
774 * The names here about because the comparisons are organized like this
775 * "a < b < c".
778 static void update_tf_links(struct stree *pre_stree,
779 const char *left_var, struct var_sym_list *left_vsl,
780 int left_comparison, int left_false_comparison,
781 const char *mid_var, struct var_sym_list *mid_vsl,
782 struct string_list *links)
784 struct smatch_state *state;
785 struct smatch_state *true_state, *false_state;
786 struct compare_data *data;
787 const char *right_var;
788 struct var_sym_list *right_vsl;
789 int orig_comparison;
790 int right_comparison;
791 int true_comparison;
792 int false_comparison;
793 char *tmp;
794 char state_name[256];
795 struct var_sym *vs;
797 FOR_EACH_PTR(links, tmp) {
798 state = get_state_stree(pre_stree, compare_id, tmp, NULL);
799 if (!state || !state->data)
800 continue;
801 data = state->data;
802 right_comparison = data->comparison;
803 right_var = data->var2;
804 right_vsl = data->vsl2;
805 if (strcmp(mid_var, right_var) == 0) {
806 right_var = data->var1;
807 right_vsl = data->vsl1;
808 right_comparison = flip_comparison(right_comparison);
810 if (have_common_var_sym(left_vsl, right_vsl))
811 continue;
813 orig_comparison = get_orig_comparison(pre_stree, left_var, right_var);
815 true_comparison = combine_comparisons(left_comparison, right_comparison);
816 false_comparison = combine_comparisons(left_false_comparison, right_comparison);
818 true_comparison = filter_comparison(orig_comparison, true_comparison);
819 false_comparison = filter_comparison(orig_comparison, false_comparison);
821 if (strcmp(left_var, right_var) > 0) {
822 const char *tmp_var = left_var;
823 struct var_sym_list *tmp_vsl = left_vsl;
825 left_var = right_var;
826 left_vsl = right_vsl;
827 right_var = tmp_var;
828 right_vsl = tmp_vsl;
829 true_comparison = flip_comparison(true_comparison);
830 false_comparison = flip_comparison(false_comparison);
833 if (!true_comparison && !false_comparison)
834 continue;
836 if (true_comparison)
837 true_state = alloc_compare_state(left_var, left_vsl, true_comparison, right_var, right_vsl);
838 else
839 true_state = NULL;
840 if (false_comparison)
841 false_state = alloc_compare_state(left_var, left_vsl, false_comparison, right_var, right_vsl);
842 else
843 false_state = NULL;
845 snprintf(state_name, sizeof(state_name), "%s vs %s", left_var, right_var);
846 set_true_false_states(compare_id, state_name, NULL, true_state, false_state);
847 FOR_EACH_PTR(left_vsl, vs) {
848 save_link_var_sym(vs->var, vs->sym, state_name);
849 } END_FOR_EACH_PTR(vs);
850 FOR_EACH_PTR(right_vsl, vs) {
851 save_link_var_sym(vs->var, vs->sym, state_name);
852 } END_FOR_EACH_PTR(vs);
853 if (!vsl_to_sym(left_vsl))
854 save_link_var_sym(left_var, NULL, state_name);
855 if (!vsl_to_sym(right_vsl))
856 save_link_var_sym(right_var, NULL, state_name);
857 } END_FOR_EACH_PTR(tmp);
860 static void update_tf_data(struct stree *pre_stree,
861 const char *left_name, struct var_sym_list *left_vsl,
862 const char *right_name, struct var_sym_list *right_vsl,
863 int true_comparison, int false_comparison)
865 struct smatch_state *state;
867 state = get_state_stree(pre_stree, link_id, right_name, vsl_to_sym(right_vsl));
868 if (state)
869 update_tf_links(pre_stree, left_name, left_vsl, true_comparison, false_comparison, right_name, right_vsl, state->data);
871 state = get_state_stree(pre_stree, link_id, left_name, vsl_to_sym(left_vsl));
872 if (state)
873 update_tf_links(pre_stree, right_name, right_vsl, flip_comparison(true_comparison), flip_comparison(false_comparison), left_name, left_vsl, state->data);
876 static void iter_modify(struct sm_state *sm, struct expression *mod_expr)
878 if (sm->state != &start ||
879 !mod_expr ||
880 (mod_expr->type != EXPR_PREOP && mod_expr->type != EXPR_POSTOP) ||
881 mod_expr->op != SPECIAL_INCREMENT)
882 set_state(inc_dec_id, sm->name, sm->sym, &undefined);
883 else
884 set_state(inc_dec_id, sm->name, sm->sym, &incremented);
887 static void handle_for_loops(struct expression *expr, char *state_name, struct smatch_state *false_state)
889 sval_t sval;
890 char *iter_name, *cap_name;
891 struct symbol *iter_sym, *cap_sym;
892 struct compare_data *data;
894 if (expr->op != '<' && expr->op != SPECIAL_UNSIGNED_LT)
895 return;
897 if (!__cur_stmt || !__prev_stmt)
898 return;
899 if (__cur_stmt->type != STMT_ITERATOR)
900 return;
901 if (__cur_stmt->iterator_pre_condition != expr)
902 return;
904 /* literals are handled in smatch_extra.c */
905 if (get_value(expr->right, &sval))
906 return;
908 /* First time checking the condition */
909 if (__prev_stmt == __cur_stmt->iterator_pre_statement) {
910 if (!get_implied_value(expr->left, &sval) ||
911 sval.value != 0)
912 return;
914 iter_name = expr_to_var_sym(expr->left, &iter_sym);
915 cap_name = expr_to_var_sym(expr->right, &cap_sym);
916 if (!iter_name || !cap_name || !iter_sym || !cap_sym) {
917 free_string(iter_name);
918 free_string(cap_name);
919 return;
922 set_state(inc_dec_id, iter_name, iter_sym, &start);
923 store_link(inc_dec_link_id, cap_name, cap_sym, iter_name, iter_sym);
925 free_string(iter_name);
926 free_string(cap_name);
927 return;
930 /* Second time checking the condtion */
931 if (__prev_stmt != __cur_stmt->iterator_post_statement)
932 return;
934 if (get_state_expr(inc_dec_id, expr->left) != &incremented)
935 return;
937 data = false_state->data;
938 false_state = alloc_compare_state(data->var1, data->vsl1, SPECIAL_EQUAL, data->var2, data->vsl2);
940 set_true_false_states(compare_id, state_name, NULL, NULL, false_state);
943 static void handle_comparison(struct expression *left_expr, int op, struct expression *right_expr, char **_state_name, struct smatch_state **_false_state)
945 char *left = NULL;
946 char *right = NULL;
947 struct symbol *left_sym, *right_sym;
948 struct var_sym_list *left_vsl, *right_vsl;
949 int false_op;
950 int orig_comparison;
951 struct smatch_state *true_state, *false_state;
952 static char state_name[256];
953 struct stree *pre_stree;
954 sval_t sval;
956 false_op = negate_comparison(op);
958 if (op == SPECIAL_UNSIGNED_LT &&
959 get_implied_value(left_expr, &sval) &&
960 sval.value == 0)
961 false_op = SPECIAL_EQUAL;
963 if (op == SPECIAL_UNSIGNED_GT &&
964 get_implied_value(right_expr, &sval) &&
965 sval.value == 0)
966 false_op = SPECIAL_EQUAL;
968 left = chunk_to_var_sym(left_expr, &left_sym);
969 if (!left)
970 goto free;
971 left_vsl = expr_to_vsl(left_expr);
972 right = chunk_to_var_sym(right_expr, &right_sym);
973 if (!right)
974 goto free;
975 right_vsl = expr_to_vsl(right_expr);
977 if (strcmp(left, right) > 0) {
978 struct symbol *tmp_sym = left_sym;
979 char *tmp_name = left;
980 struct var_sym_list *tmp_vsl = left_vsl;
981 struct expression *tmp_expr = left_expr;
983 left = right;
984 left_sym = right_sym;
985 left_vsl = right_vsl;
986 left_expr = right_expr;
987 right = tmp_name;
988 right_sym = tmp_sym;
989 right_vsl = tmp_vsl;
990 right_expr = tmp_expr;
991 op = flip_comparison(op);
992 false_op = flip_comparison(false_op);
995 orig_comparison = get_comparison_strings(left, right);
996 op = filter_comparison(orig_comparison, op);
997 false_op = filter_comparison(orig_comparison, false_op);
999 snprintf(state_name, sizeof(state_name), "%s vs %s", left, right);
1000 true_state = alloc_compare_state(left, left_vsl, op, right, right_vsl);
1001 false_state = alloc_compare_state(left, left_vsl, false_op, right, right_vsl);
1003 pre_stree = clone_stree(__get_cur_stree());
1004 update_tf_data(pre_stree, left, left_vsl, right, right_vsl, op, false_op);
1005 free_stree(&pre_stree);
1007 set_true_false_states(compare_id, state_name, NULL, true_state, false_state);
1008 __compare_param_limit_hook(left_expr, right_expr, state_name, true_state, false_state);
1009 save_link(left_expr, state_name);
1010 save_link(right_expr, state_name);
1012 if (_false_state)
1013 *_false_state = false_state;
1014 if (_state_name)
1015 *_state_name = state_name;
1016 free:
1017 free_string(left);
1018 free_string(right);
1022 void __comparison_match_condition(struct expression *expr)
1024 struct expression *left, *right, *new_left, *new_right;
1025 struct smatch_state *false_state = NULL;
1026 char *state_name = NULL;
1028 if (expr->type != EXPR_COMPARE)
1029 return;
1031 handle_comparison(expr->left, expr->op, expr->right, &state_name, &false_state);
1032 if (false_state && state_name)
1033 handle_for_loops(expr, state_name, false_state);
1036 left = strip_parens(expr->left);
1037 right = strip_parens(expr->right);
1039 if (left->type == EXPR_BINOP && left->op == '+') {
1040 new_left = left->left;
1041 new_right = binop_expression(right, '-', left->right);
1042 handle_comparison(new_left, expr->op, new_right, NULL, NULL);
1044 new_left = left->right;
1045 new_right = binop_expression(right, '-', left->left);
1046 handle_comparison(new_left, expr->op, new_right, NULL, NULL);
1050 static void add_comparison_var_sym(const char *left_name,
1051 struct var_sym_list *left_vsl,
1052 int comparison,
1053 const char *right_name, struct var_sym_list *right_vsl)
1055 struct smatch_state *state;
1056 struct var_sym *vs;
1057 char state_name[256];
1059 if (strcmp(left_name, right_name) > 0) {
1060 const char *tmp_name = left_name;
1061 struct var_sym_list *tmp_vsl = left_vsl;
1063 left_name = right_name;
1064 left_vsl = right_vsl;
1065 right_name = tmp_name;
1066 right_vsl = tmp_vsl;
1067 comparison = flip_comparison(comparison);
1069 snprintf(state_name, sizeof(state_name), "%s vs %s", left_name, right_name);
1070 state = alloc_compare_state(left_name, left_vsl, comparison, right_name, right_vsl);
1072 set_state(compare_id, state_name, NULL, state);
1074 FOR_EACH_PTR(left_vsl, vs) {
1075 save_link_var_sym(vs->var, vs->sym, state_name);
1076 } END_FOR_EACH_PTR(vs);
1077 FOR_EACH_PTR(right_vsl, vs) {
1078 save_link_var_sym(vs->var, vs->sym, state_name);
1079 } END_FOR_EACH_PTR(vs);
1082 static void add_comparison(struct expression *left, int comparison, struct expression *right)
1084 char *left_name = NULL;
1085 char *right_name = NULL;
1086 struct symbol *left_sym, *right_sym;
1087 struct var_sym_list *left_vsl, *right_vsl;
1088 struct smatch_state *state;
1089 char state_name[256];
1091 left_name = chunk_to_var_sym(left, &left_sym);
1092 if (!left_name)
1093 goto free;
1094 left_vsl = expr_to_vsl(left);
1095 right_name = chunk_to_var_sym(right, &right_sym);
1096 if (!right_name)
1097 goto free;
1098 right_vsl = expr_to_vsl(right);
1100 if (strcmp(left_name, right_name) > 0) {
1101 struct symbol *tmp_sym = left_sym;
1102 char *tmp_name = left_name;
1103 struct var_sym_list *tmp_vsl = left_vsl;
1105 left_name = right_name;
1106 left_sym = right_sym;
1107 left_vsl = right_vsl;
1108 right_name = tmp_name;
1109 right_sym = tmp_sym;
1110 right_vsl = tmp_vsl;
1111 comparison = flip_comparison(comparison);
1113 snprintf(state_name, sizeof(state_name), "%s vs %s", left_name, right_name);
1114 state = alloc_compare_state(left_name, left_vsl, comparison, right_name, right_vsl);
1116 set_state(compare_id, state_name, NULL, state);
1117 save_link(left, state_name);
1118 save_link(right, state_name);
1120 free:
1121 free_string(left_name);
1122 free_string(right_name);
1125 static void match_assign_add(struct expression *expr)
1127 struct expression *right;
1128 struct expression *r_left, *r_right;
1129 sval_t left_tmp, right_tmp;
1131 right = strip_expr(expr->right);
1132 r_left = strip_expr(right->left);
1133 r_right = strip_expr(right->right);
1135 get_absolute_min(r_left, &left_tmp);
1136 get_absolute_min(r_right, &right_tmp);
1138 if (left_tmp.value > 0)
1139 add_comparison(expr->left, '>', r_right);
1140 else if (left_tmp.value == 0)
1141 add_comparison(expr->left, SPECIAL_GTE, r_right);
1143 if (right_tmp.value > 0)
1144 add_comparison(expr->left, '>', r_left);
1145 else if (right_tmp.value == 0)
1146 add_comparison(expr->left, SPECIAL_GTE, r_left);
1149 static void match_assign_sub(struct expression *expr)
1151 struct expression *right;
1152 struct expression *r_left, *r_right;
1153 int comparison;
1154 sval_t min;
1156 right = strip_expr(expr->right);
1157 r_left = strip_expr(right->left);
1158 r_right = strip_expr(right->right);
1160 if (get_absolute_min(r_right, &min) && sval_is_negative(min))
1161 return;
1163 comparison = get_comparison(r_left, r_right);
1165 switch (comparison) {
1166 case '>':
1167 case SPECIAL_GTE:
1168 if (implied_not_equal(r_right, 0))
1169 add_comparison(expr->left, '>', r_left);
1170 else
1171 add_comparison(expr->left, SPECIAL_GTE, r_left);
1172 return;
1176 static void match_assign_divide(struct expression *expr)
1178 struct expression *right;
1179 struct expression *r_left, *r_right;
1180 sval_t min;
1182 right = strip_expr(expr->right);
1183 r_left = strip_expr(right->left);
1184 r_right = strip_expr(right->right);
1185 if (!get_implied_min(r_right, &min) || min.value <= 1)
1186 return;
1188 add_comparison(expr->left, '<', r_left);
1191 static void match_binop_assign(struct expression *expr)
1193 struct expression *right;
1195 right = strip_expr(expr->right);
1196 if (right->op == '+')
1197 match_assign_add(expr);
1198 if (right->op == '-')
1199 match_assign_sub(expr);
1200 if (right->op == '/')
1201 match_assign_divide(expr);
1204 static void copy_comparisons(struct expression *left, struct expression *right)
1206 struct string_list *links;
1207 struct smatch_state *state;
1208 struct compare_data *data;
1209 struct symbol *left_sym, *right_sym;
1210 char *left_var = NULL;
1211 char *right_var = NULL;
1212 struct var_sym_list *left_vsl;
1213 const char *var;
1214 struct var_sym_list *vsl;
1215 int comparison;
1216 char *tmp;
1218 left_var = chunk_to_var_sym(left, &left_sym);
1219 if (!left_var)
1220 goto done;
1221 left_vsl = expr_to_vsl(left);
1222 right_var = chunk_to_var_sym(right, &right_sym);
1223 if (!right_var)
1224 goto done;
1226 state = get_state(link_id, right_var, right_sym);
1227 if (!state)
1228 return;
1229 links = state->data;
1231 FOR_EACH_PTR(links, tmp) {
1232 state = get_state(compare_id, tmp, NULL);
1233 if (!state || !state->data)
1234 continue;
1235 data = state->data;
1236 comparison = data->comparison;
1237 var = data->var2;
1238 vsl = data->vsl2;
1239 if (strcmp(var, right_var) == 0) {
1240 var = data->var1;
1241 vsl = data->vsl1;
1242 comparison = flip_comparison(comparison);
1244 add_comparison_var_sym(left_var, left_vsl, comparison, var, vsl);
1245 } END_FOR_EACH_PTR(tmp);
1247 done:
1248 free_string(right_var);
1251 static void match_assign(struct expression *expr)
1253 struct expression *right;
1255 if (expr->op != '=')
1256 return;
1257 if (__in_fake_assign || outside_of_function())
1258 return;
1260 if (is_struct(expr->left))
1261 return;
1263 copy_comparisons(expr->left, expr->right);
1264 add_comparison(expr->left, SPECIAL_EQUAL, expr->right);
1266 right = strip_expr(expr->right);
1267 if (right->type == EXPR_BINOP)
1268 match_binop_assign(expr);
1271 int get_comparison_strings(const char *one, const char *two)
1273 char buf[256];
1274 struct smatch_state *state;
1275 int invert = 0;
1276 int ret = 0;
1278 if (strcmp(one, two) == 0)
1279 return SPECIAL_EQUAL;
1281 if (strcmp(one, two) > 0) {
1282 const char *tmp = one;
1284 one = two;
1285 two = tmp;
1286 invert = 1;
1289 snprintf(buf, sizeof(buf), "%s vs %s", one, two);
1290 state = get_state(compare_id, buf, NULL);
1291 if (state)
1292 ret = state_to_comparison(state);
1294 if (invert)
1295 ret = flip_comparison(ret);
1297 return ret;
1300 int get_comparison(struct expression *a, struct expression *b)
1302 char *one = NULL;
1303 char *two = NULL;
1304 int ret = 0;
1306 one = chunk_to_var(a);
1307 if (!one)
1308 goto free;
1309 two = chunk_to_var(b);
1310 if (!two)
1311 goto free;
1313 ret = get_comparison_strings(one, two);
1314 free:
1315 free_string(one);
1316 free_string(two);
1317 return ret;
1320 int possible_comparison(struct expression *a, int comparison, struct expression *b)
1322 char *one = NULL;
1323 char *two = NULL;
1324 int ret = 0;
1325 char buf[256];
1326 struct sm_state *sm;
1327 int saved;
1329 one = chunk_to_var(a);
1330 if (!one)
1331 goto free;
1332 two = chunk_to_var(b);
1333 if (!two)
1334 goto free;
1337 if (strcmp(one, two) == 0 && comparison == SPECIAL_EQUAL) {
1338 ret = 1;
1339 goto free;
1342 if (strcmp(one, two) > 0) {
1343 char *tmp = one;
1345 one = two;
1346 two = tmp;
1347 comparison = flip_comparison(comparison);
1350 snprintf(buf, sizeof(buf), "%s vs %s", one, two);
1351 sm = get_sm_state(compare_id, buf, NULL);
1352 if (!sm)
1353 goto free;
1355 FOR_EACH_PTR(sm->possible, sm) {
1356 if (!sm->state->data)
1357 continue;
1358 saved = ((struct compare_data *)sm->state->data)->comparison;
1359 if (saved == comparison)
1360 ret = 1;
1361 if (comparison == SPECIAL_EQUAL &&
1362 (saved == SPECIAL_LTE ||
1363 saved == SPECIAL_GTE ||
1364 saved == SPECIAL_UNSIGNED_LTE ||
1365 saved == SPECIAL_UNSIGNED_GTE))
1366 ret = 1;
1367 if (ret == 1)
1368 goto free;
1369 } END_FOR_EACH_PTR(sm);
1371 return ret;
1372 free:
1373 free_string(one);
1374 free_string(two);
1375 return ret;
1378 struct state_list *get_all_comparisons(struct expression *expr)
1380 struct smatch_state *state;
1381 struct string_list *links;
1382 struct state_list *ret = NULL;
1383 struct sm_state *sm;
1384 char *tmp;
1386 state = get_state_expr(link_id, expr);
1387 if (!state)
1388 return NULL;
1389 links = state->data;
1391 FOR_EACH_PTR(links, tmp) {
1392 sm = get_sm_state(compare_id, tmp, NULL);
1393 if (!sm)
1394 continue;
1395 // FIXME have to compare name with vsl
1396 add_ptr_list(&ret, sm);
1397 } END_FOR_EACH_PTR(tmp);
1399 return ret;
1402 struct state_list *get_all_possible_equal_comparisons(struct expression *expr)
1404 struct smatch_state *state;
1405 struct string_list *links;
1406 struct state_list *ret = NULL;
1407 struct sm_state *sm;
1408 char *tmp;
1410 state = get_state_expr(link_id, expr);
1411 if (!state)
1412 return NULL;
1413 links = state->data;
1415 FOR_EACH_PTR(links, tmp) {
1416 sm = get_sm_state(compare_id, tmp, NULL);
1417 if (!sm)
1418 continue;
1419 if (!strchr(sm->state->name, '='))
1420 continue;
1421 if (strcmp(sm->state->name, "!=") == 0)
1422 continue;
1423 add_ptr_list(&ret, sm);
1424 } END_FOR_EACH_PTR(tmp);
1426 return ret;
1429 static void update_links_from_call(struct expression *left,
1430 int left_compare,
1431 struct expression *right)
1433 struct string_list *links;
1434 struct smatch_state *state;
1435 struct compare_data *data;
1436 struct symbol *left_sym, *right_sym;
1437 char *left_var = NULL;
1438 char *right_var = NULL;
1439 struct var_sym_list *left_vsl;
1440 const char *var;
1441 struct var_sym_list *vsl;
1442 int comparison;
1443 char *tmp;
1445 left_var = chunk_to_var_sym(left, &left_sym);
1446 if (!left_var)
1447 goto done;
1448 left_vsl = expr_to_vsl(left);
1449 right_var = chunk_to_var_sym(right, &right_sym);
1450 if (!right_var)
1451 goto done;
1453 state = get_state(link_id, right_var, right_sym);
1454 if (!state)
1455 return;
1456 links = state->data;
1458 FOR_EACH_PTR(links, tmp) {
1459 state = get_state(compare_id, tmp, NULL);
1460 if (!state || !state->data)
1461 continue;
1462 data = state->data;
1463 comparison = data->comparison;
1464 var = data->var2;
1465 vsl = data->vsl2;
1466 if (strcmp(var, right_var) == 0) {
1467 var = data->var1;
1468 vsl = data->vsl1;
1469 comparison = flip_comparison(comparison);
1471 comparison = combine_comparisons(left_compare, comparison);
1472 if (!comparison)
1473 continue;
1474 add_comparison_var_sym(left_var, left_vsl, comparison, var, vsl);
1475 } END_FOR_EACH_PTR(tmp);
1477 done:
1478 free_string(right_var);
1481 void __add_comparison_info(struct expression *expr, struct expression *call, const char *range)
1483 struct expression *arg;
1484 int comparison;
1485 const char *c = range;
1487 if (!str_to_comparison_arg(c, call, &comparison, &arg))
1488 return;
1489 update_links_from_call(expr, comparison, arg);
1490 add_comparison(expr, comparison, arg);
1493 static char *range_comparison_to_param_helper(struct expression *expr, char starts_with, int ignore)
1495 struct symbol *param;
1496 char *var = NULL;
1497 char buf[256];
1498 char *ret_str = NULL;
1499 int compare;
1500 int i;
1502 var = chunk_to_var(expr);
1503 if (!var)
1504 goto free;
1506 i = -1;
1507 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, param) {
1508 i++;
1509 if (i == ignore)
1510 continue;
1511 if (!param->ident)
1512 continue;
1513 snprintf(buf, sizeof(buf), "%s orig", param->ident->name);
1514 compare = get_comparison_strings(var, buf);
1515 if (!compare)
1516 continue;
1517 if (show_special(compare)[0] != starts_with)
1518 continue;
1519 snprintf(buf, sizeof(buf), "[%s$%d]", show_special(compare), i);
1520 ret_str = alloc_sname(buf);
1521 break;
1522 } END_FOR_EACH_PTR(param);
1524 free:
1525 free_string(var);
1526 return ret_str;
1529 char *name_sym_to_param_comparison(const char *name, struct symbol *sym)
1531 struct symbol *param;
1532 char buf[256];
1533 int compare;
1534 int i;
1536 i = -1;
1537 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, param) {
1538 i++;
1539 if (!param->ident)
1540 continue;
1541 snprintf(buf, sizeof(buf), "%s orig", param->ident->name);
1542 compare = get_comparison_strings(name, buf);
1543 if (!compare)
1544 continue;
1545 snprintf(buf, sizeof(buf), "[%s$%d]", show_special(compare), i);
1546 return alloc_sname(buf);
1547 } END_FOR_EACH_PTR(param);
1549 return NULL;
1552 char *expr_equal_to_param(struct expression *expr, int ignore)
1554 return range_comparison_to_param_helper(expr, '=', ignore);
1557 char *expr_lte_to_param(struct expression *expr, int ignore)
1559 return range_comparison_to_param_helper(expr, '<', ignore);
1562 char *expr_param_comparison(struct expression *expr, int ignore)
1564 struct symbol *param;
1565 char *var = NULL;
1566 char buf[256];
1567 char *ret_str = NULL;
1568 int compare;
1569 int i;
1571 var = chunk_to_var(expr);
1572 if (!var)
1573 goto free;
1575 i = -1;
1576 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, param) {
1577 i++;
1578 if (i == ignore)
1579 continue;
1580 if (!param->ident)
1581 continue;
1582 snprintf(buf, sizeof(buf), "%s orig", param->ident->name);
1583 compare = get_comparison_strings(var, buf);
1584 if (!compare)
1585 continue;
1586 snprintf(buf, sizeof(buf), "[%s$%d]", show_special(compare), i);
1587 ret_str = alloc_sname(buf);
1588 break;
1589 } END_FOR_EACH_PTR(param);
1591 free:
1592 free_string(var);
1593 return ret_str;
1596 char *get_printed_param_name(struct expression *call, const char *param_name, struct symbol *param_sym)
1598 struct expression *arg;
1599 char *name;
1600 struct symbol *sym;
1601 static char buf[256];
1602 int len;
1603 int i;
1605 i = -1;
1606 FOR_EACH_PTR(call->args, arg) {
1607 i++;
1609 name = expr_to_var_sym(arg, &sym);
1610 if (!name || !sym)
1611 continue;
1612 if (sym != param_sym)
1613 continue;
1615 len = strlen(name);
1616 if (strncmp(name, param_name, len) != 0)
1617 continue;
1618 if (param_name[len] == '\0') {
1619 snprintf(buf, sizeof(buf), "$%d", i);
1620 return buf;
1622 if (param_name[len] != '-')
1623 continue;
1624 snprintf(buf, sizeof(buf), "$%d%s", i, param_name + len);
1625 return buf;
1626 } END_FOR_EACH_PTR(arg);
1628 return NULL;
1631 static void match_call_info(struct expression *expr)
1633 struct expression *arg;
1634 struct smatch_state *state;
1635 struct sm_state *sm;
1636 struct compare_data *data;
1637 int comparison;
1638 struct string_list *links;
1639 char *arg_name;
1640 const char *right_name;
1641 char *link;
1642 char info_buf[256];
1643 int i;
1645 i = -1;
1646 FOR_EACH_PTR(expr->args, arg) {
1647 i++;
1649 state = get_state_expr(link_id, arg);
1650 if (!state)
1651 continue;
1653 links = state->data;
1654 FOR_EACH_PTR(links, link) {
1655 struct var_sym_list *right_vsl;
1656 struct var_sym *right_vs;
1659 if (strstr(link, " orig"))
1660 continue;
1661 sm = get_sm_state(compare_id, link, NULL);
1662 if (!sm)
1663 continue;
1664 data = sm->state->data;
1665 if (!data || !data->comparison)
1666 continue;
1667 arg_name = expr_to_var(arg);
1668 if (!arg_name)
1669 continue;
1671 right_vsl = NULL;
1672 if (strcmp(data->var1, arg_name) == 0) {
1673 comparison = data->comparison;
1674 right_name = data->var2;
1675 right_vsl = data->vsl2;
1677 } else if (strcmp(data->var2, arg_name) == 0) {
1678 comparison = flip_comparison(data->comparison);
1679 right_name = data->var1;
1680 right_vsl = data->vsl1;
1682 if (!right_vsl || ptr_list_size((struct ptr_list *)right_vsl) != 1)
1683 goto free;
1684 right_vs = first_ptr_list((struct ptr_list *)right_vsl);
1685 if (strcmp(right_vs->var, right_name) != 0)
1686 continue;
1687 right_name = get_printed_param_name(expr, right_vs->var, right_vs->sym);
1688 if (!right_name)
1689 continue;
1690 snprintf(info_buf, sizeof(info_buf), "%s %s", show_special(comparison), right_name);
1691 sql_insert_caller_info(expr, PARAM_COMPARE, i, "$", info_buf);
1693 free:
1694 free_string(arg_name);
1695 } END_FOR_EACH_PTR(link);
1696 } END_FOR_EACH_PTR(arg);
1699 static void struct_member_callback(struct expression *call, int param, char *printed_name, struct sm_state *link_sm)
1701 struct sm_state *compare_sm;
1702 struct string_list *links;
1703 char *link;
1704 struct compare_data *data;
1705 struct var_sym *left, *right;
1706 static char info_buf[256];
1707 const char *right_name;
1709 if (strstr(printed_name, " orig"))
1710 return;
1712 links = link_sm->state->data;
1713 FOR_EACH_PTR(links, link) {
1714 compare_sm = get_sm_state(compare_id, link, NULL);
1715 if (!compare_sm)
1716 continue;
1717 data = compare_sm->state->data;
1718 if (!data || !data->comparison)
1719 continue;
1721 if (ptr_list_size((struct ptr_list *)data->vsl1) != 1 ||
1722 ptr_list_size((struct ptr_list *)data->vsl2) != 1)
1723 continue;
1724 left = first_ptr_list((struct ptr_list *)data->vsl1);
1725 right = first_ptr_list((struct ptr_list *)data->vsl2);
1726 if (left->sym == right->sym &&
1727 strcmp(left->var, right->var) == 0)
1728 continue;
1730 * Both parameters link to this comparison so only
1731 * record the first one.
1733 if (left->sym != link_sm->sym ||
1734 strcmp(left->var, link_sm->name) != 0)
1735 continue;
1737 right_name = get_printed_param_name(call, right->var, right->sym);
1738 if (!right_name)
1739 continue;
1740 snprintf(info_buf, sizeof(info_buf), "%s %s", show_special(data->comparison), right_name);
1741 sql_insert_caller_info(call, PARAM_COMPARE, param, printed_name, info_buf);
1742 } END_FOR_EACH_PTR(link);
1745 static void print_return_value_comparison(int return_id, char *return_ranges, struct expression *expr)
1747 char *name;
1748 const char *tmp_name;
1749 struct symbol *sym;
1750 int param;
1751 char info_buf[256];
1754 * TODO: This only prints == comparisons. That's probably the most
1755 * useful comparison because == max has lots of implications. But it
1756 * would be good to capture the rest as well.
1758 * This information is already in the DB but it's in the parameter math
1759 * bits and it's awkward to use it. This is is the simpler, possibly
1760 * cleaner way, but not necessarily the best, I don't know.
1763 if (!expr)
1764 return;
1765 name = expr_to_var_sym(expr, &sym);
1766 if (!name || !sym)
1767 goto free;
1769 param = get_param_num_from_sym(sym);
1770 if (param < 0)
1771 goto free;
1772 if (param_was_set_var_sym(name, sym))
1773 goto free;
1775 tmp_name = get_param_name_var_sym(name, sym);
1776 if (!tmp_name)
1777 goto free;
1779 snprintf(info_buf, sizeof(info_buf), "== $%d%s", param, tmp_name + 1);
1780 sql_insert_return_states(return_id, return_ranges,
1781 PARAM_COMPARE, -1, "$", info_buf);
1782 free:
1783 free_string(name);
1786 static void print_return_comparison(int return_id, char *return_ranges, struct expression *expr)
1788 struct sm_state *tmp;
1789 struct string_list *links;
1790 char *link;
1791 struct sm_state *sm;
1792 struct compare_data *data;
1793 struct var_sym *left, *right;
1794 int left_param, right_param;
1795 char left_buf[256];
1796 char right_buf[256];
1797 char info_buf[256];
1798 const char *tmp_name;
1800 print_return_value_comparison(return_id, return_ranges, expr);
1802 FOR_EACH_MY_SM(link_id, __get_cur_stree(), tmp) {
1803 if (get_param_num_from_sym(tmp->sym) < 0)
1804 continue;
1805 links = tmp->state->data;
1806 FOR_EACH_PTR(links, link) {
1807 sm = get_sm_state(compare_id, link, NULL);
1808 if (!sm)
1809 continue;
1810 data = sm->state->data;
1811 if (!data || !data->comparison)
1812 continue;
1813 if (ptr_list_size((struct ptr_list *)data->vsl1) != 1 ||
1814 ptr_list_size((struct ptr_list *)data->vsl2) != 1)
1815 continue;
1816 left = first_ptr_list((struct ptr_list *)data->vsl1);
1817 right = first_ptr_list((struct ptr_list *)data->vsl2);
1818 if (left->sym == right->sym &&
1819 strcmp(left->var, right->var) == 0)
1820 continue;
1822 * Both parameters link to this comparison so only
1823 * record the first one.
1825 if (left->sym != tmp->sym ||
1826 strcmp(left->var, tmp->name) != 0)
1827 continue;
1829 if (strstr(right->var, " orig"))
1830 continue;
1832 left_param = get_param_num_from_sym(left->sym);
1833 right_param = get_param_num_from_sym(right->sym);
1834 if (left_param < 0 || right_param < 0)
1835 continue;
1837 tmp_name = get_param_name_var_sym(left->var, left->sym);
1838 if (!tmp_name)
1839 continue;
1840 snprintf(left_buf, sizeof(left_buf), "%s", tmp_name);
1842 tmp_name = get_param_name_var_sym(right->var, right->sym);
1843 if (!tmp_name || tmp_name[0] != '$')
1844 continue;
1845 snprintf(right_buf, sizeof(right_buf), "$%d%s", right_param, tmp_name + 1);
1848 * FIXME: this should reject $ type variables (as
1849 * opposed to $->foo type). Those should come from
1850 * smatch_param_compare_limit.c.
1853 snprintf(info_buf, sizeof(info_buf), "%s %s", show_special(data->comparison), right_buf);
1854 sql_insert_return_states(return_id, return_ranges,
1855 PARAM_COMPARE, left_param, left_buf, info_buf);
1856 } END_FOR_EACH_PTR(link);
1858 } END_FOR_EACH_SM(tmp);
1861 static int parse_comparison(char **value, int *op)
1864 *op = **value;
1866 switch (*op) {
1867 case '<':
1868 (*value)++;
1869 if (**value == '=') {
1870 (*value)++;
1871 *op = SPECIAL_LTE;
1873 break;
1874 case '=':
1875 (*value)++;
1876 (*value)++;
1877 *op = SPECIAL_EQUAL;
1878 break;
1879 case '!':
1880 (*value)++;
1881 (*value)++;
1882 *op = SPECIAL_NOTEQUAL;
1883 break;
1884 case '>':
1885 (*value)++;
1886 if (**value == '=') {
1887 (*value)++;
1888 *op = SPECIAL_GTE;
1890 break;
1891 default:
1892 return 0;
1895 if (**value != ' ') {
1896 sm_msg("internal error parsing comparison. %s", *value);
1897 return 0;
1900 (*value)++;
1901 return 1;
1904 static int split_op_param_key(char *value, int *op, int *param, char **key)
1906 static char buf[256];
1907 char *p;
1909 if (!parse_comparison(&value, op))
1910 return 0;
1912 snprintf(buf, sizeof(buf), value);
1914 p = buf;
1915 if (*p++ != '$')
1916 return 0;
1918 *param = atoi(p);
1919 if (*param < 0 || *param > 99)
1920 return 0;
1921 p++;
1922 if (*param > 9)
1923 p++;
1924 p--;
1925 *p = '$';
1926 *key = p;
1928 return 1;
1931 static void db_return_comparison(struct expression *expr, int left_param, char *key, char *value)
1933 struct expression *left_arg, *right_arg;
1934 char *left_name = NULL;
1935 struct symbol *left_sym;
1936 char *right_name = NULL;
1937 struct symbol *right_sym;
1938 int op;
1939 int right_param;
1940 char *right_key;
1941 struct var_sym_list *left_vsl = NULL, *right_vsl = NULL;
1943 if (left_param == -1) {
1944 if (expr->type != EXPR_ASSIGNMENT)
1945 return;
1946 left_arg = strip_expr(expr->left);
1948 while (expr->type == EXPR_ASSIGNMENT)
1949 expr = strip_expr(expr->right);
1950 if (expr->type != EXPR_CALL)
1951 return;
1952 } else {
1953 while (expr->type == EXPR_ASSIGNMENT)
1954 expr = strip_expr(expr->right);
1955 if (expr->type != EXPR_CALL)
1956 return;
1958 left_arg = get_argument_from_call_expr(expr->args, left_param);
1959 if (!left_arg)
1960 return;
1963 if (!split_op_param_key(value, &op, &right_param, &right_key))
1964 return;
1966 right_arg = get_argument_from_call_expr(expr->args, right_param);
1967 if (!right_arg)
1968 return;
1970 left_name = get_variable_from_key(left_arg, key, &left_sym);
1971 if (!left_name || !left_sym)
1972 goto free;
1974 right_name = get_variable_from_key(right_arg, right_key, &right_sym);
1975 if (!right_name || !right_sym)
1976 goto free;
1978 add_var_sym(&left_vsl, left_name, left_sym);
1979 add_var_sym(&right_vsl, right_name, right_sym);
1981 if (local_debug)
1982 sm_msg("SET compare: %s %s %s", left_name, show_special(op), right_name);
1984 add_comparison_var_sym(left_name, left_vsl, op, right_name, right_vsl);
1986 free:
1987 free_string(left_name);
1988 free_string(right_name);
1991 int param_compare_limit_is_impossible(struct expression *expr, int left_param, char *left_key, char *value)
1993 struct smatch_state *state;
1994 char *left_name = NULL;
1995 char *right_name = NULL;
1996 struct symbol *left_sym, *right_sym;
1997 struct expression *left_arg, *right_arg;
1998 int op;
1999 int right_param;
2000 char *right_key;
2001 int ret = 0;
2002 char buf[256];
2004 while (expr->type == EXPR_ASSIGNMENT)
2005 expr = strip_expr(expr->right);
2006 if (expr->type != EXPR_CALL)
2007 return 0;
2009 if (!split_op_param_key(value, &op, &right_param, &right_key))
2010 return 0;
2012 left_arg = get_argument_from_call_expr(expr->args, left_param);
2013 if (!left_arg)
2014 return 0;
2016 right_arg = get_argument_from_call_expr(expr->args, right_param);
2017 if (!right_arg)
2018 return 0;
2020 left_name = get_variable_from_key(left_arg, left_key, &left_sym);
2021 right_name = get_variable_from_key(right_arg, right_key, &right_sym);
2022 if (!left_name || !right_name)
2023 goto free;
2025 snprintf(buf, sizeof(buf), "%s vs %s", left_name, right_name);
2026 state = get_state(compare_id, buf, NULL);
2027 if (!state)
2028 goto free;
2029 if (!state_to_comparison(state))
2030 goto free;
2032 if (!filter_comparison(state_to_comparison(state), op))
2033 ret = 1;
2034 free:
2035 free_string(left_name);
2036 free_string(right_name);
2037 return ret;
2040 static void free_data(struct symbol *sym)
2042 if (__inline_fn)
2043 return;
2044 clear_compare_data_alloc();
2047 void register_comparison(int id)
2049 compare_id = id;
2050 add_hook(&save_start_states, AFTER_DEF_HOOK);
2051 add_unmatched_state_hook(compare_id, unmatched_comparison);
2052 add_merge_hook(compare_id, &merge_compare_states);
2053 add_hook(&free_data, AFTER_FUNC_HOOK);
2054 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
2055 add_split_return_callback(&print_return_comparison);
2057 select_return_states_hook(PARAM_COMPARE, &db_return_comparison);
2060 void register_comparison_late(int id)
2062 add_hook(&match_assign, ASSIGNMENT_HOOK);
2065 void register_comparison_links(int id)
2067 link_id = id;
2068 add_merge_hook(link_id, &merge_links);
2069 add_modification_hook(link_id, &match_modify);
2071 add_member_info_callback(link_id, struct_member_callback);
2074 void register_comparison_inc_dec(int id)
2076 inc_dec_id = id;
2077 add_modification_hook_late(inc_dec_id, &iter_modify);
2080 void register_comparison_inc_dec_links(int id)
2082 inc_dec_link_id = id;
2083 set_up_link_functions(inc_dec_id, inc_dec_link_id);
2086 static void filter_by_sm(struct sm_state *sm, int op,
2087 struct state_list **true_stack,
2088 struct state_list **false_stack)
2090 struct compare_data *data;
2091 int istrue = 0;
2092 int isfalse = 0;
2094 if (!sm)
2095 return;
2096 data = sm->state->data;
2097 if (!data) {
2098 if (sm->merged) {
2099 filter_by_sm(sm->left, op, true_stack, false_stack);
2100 filter_by_sm(sm->right, op, true_stack, false_stack);
2102 return;
2105 if (data->comparison &&
2106 data->comparison == filter_comparison(data->comparison, op))
2107 istrue = 1;
2109 if (data->comparison &&
2110 data->comparison == filter_comparison(data->comparison, negate_comparison(op)))
2111 isfalse = 1;
2113 if (istrue)
2114 add_ptr_list(true_stack, sm);
2115 if (isfalse)
2116 add_ptr_list(false_stack, sm);
2118 if (sm->merged) {
2119 filter_by_sm(sm->left, op, true_stack, false_stack);
2120 filter_by_sm(sm->right, op, true_stack, false_stack);
2124 struct sm_state *comparison_implication_hook(struct expression *expr,
2125 struct state_list **true_stack,
2126 struct state_list **false_stack)
2128 struct sm_state *sm;
2129 char *left, *right;
2130 int op;
2131 static char buf[256];
2133 if (expr->type != EXPR_COMPARE)
2134 return NULL;
2136 op = expr->op;
2138 left = expr_to_var(expr->left);
2139 right = expr_to_var(expr->right);
2140 if (!left || !right) {
2141 free_string(left);
2142 free_string(right);
2143 return NULL;
2146 if (strcmp(left, right) > 0) {
2147 char *tmp = left;
2149 left = right;
2150 right = tmp;
2151 op = flip_comparison(op);
2154 snprintf(buf, sizeof(buf), "%s vs %s", left, right);
2155 sm = get_sm_state(compare_id, buf, NULL);
2156 if (!sm)
2157 return NULL;
2158 if (!sm->merged)
2159 return NULL;
2161 filter_by_sm(sm, op, true_stack, false_stack);
2162 if (!*true_stack && !*false_stack)
2163 return NULL;
2165 return sm;