estate: estate_is_empty() should return false if the state is NULL
[smatch.git] / smatch_comparison.c
blobbb5da9f53d4da75182af9f6cd4f196403db853ad
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 if (!one || !two)
245 return 0;
247 one = remove_unsigned_from_comparison(one);
248 two = remove_unsigned_from_comparison(two);
250 if (one == two)
251 return one;
253 LT = EQ = GT = 0;
255 switch (one) {
256 case '<':
257 LT = 1;
258 break;
259 case SPECIAL_LTE:
260 LT = 1;
261 EQ = 1;
262 break;
263 case SPECIAL_EQUAL:
264 EQ = 1;
265 break;
266 case SPECIAL_GTE:
267 GT = 1;
268 EQ = 1;
269 break;
270 case '>':
271 GT = 1;
274 switch (two) {
275 case '<':
276 LT = 1;
277 break;
278 case SPECIAL_LTE:
279 LT = 1;
280 EQ = 1;
281 break;
282 case SPECIAL_EQUAL:
283 EQ = 1;
284 break;
285 case SPECIAL_GTE:
286 GT = 1;
287 EQ = 1;
288 break;
289 case '>':
290 GT = 1;
293 if (LT && EQ && GT)
294 return 0;
295 if (LT && EQ)
296 return SPECIAL_LTE;
297 if (LT && GT)
298 return SPECIAL_NOTEQUAL;
299 if (LT)
300 return '<';
301 if (EQ && GT)
302 return SPECIAL_GTE;
303 if (GT)
304 return '>';
305 return 0;
309 * This is for if you have "a < b" and "b <= c". Or in other words,
310 * "a < b <= c". You would call this like get_combined_comparison('<', '<=').
311 * The return comparison would be '<'.
313 * This function is different from merge_comparisons(), for example:
314 * merge_comparison('<', '==') returns '<='
315 * get_combined_comparison('<', '==') returns '<'
317 int combine_comparisons(int left_compare, int right_compare)
319 int LT, EQ, GT;
321 left_compare = remove_unsigned_from_comparison(left_compare);
322 right_compare = remove_unsigned_from_comparison(right_compare);
324 LT = EQ = GT = 0;
326 switch (left_compare) {
327 case '<':
328 LT++;
329 break;
330 case SPECIAL_LTE:
331 LT++;
332 EQ++;
333 break;
334 case SPECIAL_EQUAL:
335 return right_compare;
336 case SPECIAL_GTE:
337 GT++;
338 EQ++;
339 break;
340 case '>':
341 GT++;
344 switch (right_compare) {
345 case '<':
346 LT++;
347 break;
348 case SPECIAL_LTE:
349 LT++;
350 EQ++;
351 break;
352 case SPECIAL_EQUAL:
353 return left_compare;
354 case SPECIAL_GTE:
355 GT++;
356 EQ++;
357 break;
358 case '>':
359 GT++;
362 if (LT == 2) {
363 if (EQ == 2)
364 return SPECIAL_LTE;
365 return '<';
368 if (GT == 2) {
369 if (EQ == 2)
370 return SPECIAL_GTE;
371 return '>';
373 return 0;
376 static int filter_comparison(int orig, int op)
378 if (orig == op)
379 return orig;
381 switch (orig) {
382 case 0:
383 return op;
384 case '<':
385 switch (op) {
386 case '<':
387 case SPECIAL_LTE:
388 case SPECIAL_NOTEQUAL:
389 return '<';
391 return 0;
392 case SPECIAL_LTE:
393 switch (op) {
394 case '<':
395 case SPECIAL_LTE:
396 case SPECIAL_EQUAL:
397 return op;
398 case SPECIAL_NOTEQUAL:
399 return '<';
401 return 0;
402 case SPECIAL_EQUAL:
403 switch (op) {
404 case SPECIAL_LTE:
405 case SPECIAL_EQUAL:
406 case SPECIAL_GTE:
407 case SPECIAL_UNSIGNED_LTE:
408 case SPECIAL_UNSIGNED_GTE:
409 return SPECIAL_EQUAL;
411 return 0;
412 case SPECIAL_NOTEQUAL:
413 switch (op) {
414 case '<':
415 case SPECIAL_LTE:
416 return '<';
417 case SPECIAL_UNSIGNED_LT:
418 case SPECIAL_UNSIGNED_LTE:
419 return SPECIAL_UNSIGNED_LT;
420 case SPECIAL_NOTEQUAL:
421 return op;
422 case '>':
423 case SPECIAL_GTE:
424 return '>';
425 case SPECIAL_UNSIGNED_GT:
426 case SPECIAL_UNSIGNED_GTE:
427 return SPECIAL_UNSIGNED_GT;
429 return 0;
430 case SPECIAL_GTE:
431 switch (op) {
432 case SPECIAL_LTE:
433 return SPECIAL_EQUAL;
434 case '>':
435 case SPECIAL_GTE:
436 case SPECIAL_EQUAL:
437 return op;
438 case SPECIAL_NOTEQUAL:
439 return '>';
441 return 0;
442 case '>':
443 switch (op) {
444 case '>':
445 case SPECIAL_GTE:
446 case SPECIAL_NOTEQUAL:
447 return '>';
449 return 0;
450 case SPECIAL_UNSIGNED_LT:
451 switch (op) {
452 case SPECIAL_UNSIGNED_LT:
453 case SPECIAL_UNSIGNED_LTE:
454 case SPECIAL_NOTEQUAL:
455 return SPECIAL_UNSIGNED_LT;
457 return 0;
458 case SPECIAL_UNSIGNED_LTE:
459 switch (op) {
460 case SPECIAL_UNSIGNED_LT:
461 case SPECIAL_UNSIGNED_LTE:
462 case SPECIAL_EQUAL:
463 return op;
464 case SPECIAL_NOTEQUAL:
465 return SPECIAL_UNSIGNED_LT;
466 case SPECIAL_UNSIGNED_GTE:
467 return SPECIAL_EQUAL;
469 return 0;
470 case SPECIAL_UNSIGNED_GTE:
471 switch (op) {
472 case SPECIAL_UNSIGNED_LTE:
473 return SPECIAL_EQUAL;
474 case SPECIAL_NOTEQUAL:
475 return SPECIAL_UNSIGNED_GT;
476 case SPECIAL_EQUAL:
477 case SPECIAL_UNSIGNED_GTE:
478 case SPECIAL_UNSIGNED_GT:
479 return op;
481 return 0;
482 case SPECIAL_UNSIGNED_GT:
483 switch (op) {
484 case SPECIAL_UNSIGNED_GT:
485 case SPECIAL_UNSIGNED_GTE:
486 case SPECIAL_NOTEQUAL:
487 return SPECIAL_UNSIGNED_GT;
489 return 0;
491 return 0;
494 struct smatch_state *merge_compare_states(struct smatch_state *s1, struct smatch_state *s2)
496 struct compare_data *data = s1->data;
497 int op;
499 op = merge_comparisons(state_to_comparison(s1), state_to_comparison(s2));
500 if (op)
501 return alloc_compare_state(data->var1, data->vsl1, op, data->var2, data->vsl2);
502 return &undefined;
505 static struct smatch_state *alloc_link_state(struct string_list *links)
507 struct smatch_state *state;
508 static char buf[256];
509 char *tmp;
510 int i;
512 state = __alloc_smatch_state(0);
514 i = 0;
515 FOR_EACH_PTR(links, tmp) {
516 if (!i++) {
517 snprintf(buf, sizeof(buf), "%s", tmp);
518 } else {
519 append(buf, ", ", sizeof(buf));
520 append(buf, tmp, sizeof(buf));
522 } END_FOR_EACH_PTR(tmp);
524 state->name = alloc_sname(buf);
525 state->data = links;
526 return state;
529 static void save_start_states(struct statement *stmt)
531 struct symbol *param;
532 char orig[64];
533 char state_name[128];
534 struct smatch_state *state;
535 struct string_list *links;
536 char *link;
538 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, param) {
539 struct var_sym_list *vsl1 = NULL;
540 struct var_sym_list *vsl2 = NULL;
542 if (!param->ident)
543 continue;
544 snprintf(orig, sizeof(orig), "%s orig", param->ident->name);
545 snprintf(state_name, sizeof(state_name), "%s vs %s", param->ident->name, orig);
546 add_var_sym(&vsl1, param->ident->name, param);
547 add_var_sym(&vsl2, orig, param);
548 state = alloc_compare_state(param->ident->name, vsl1, SPECIAL_EQUAL, alloc_sname(orig), vsl2);
549 set_state(compare_id, state_name, NULL, state);
551 link = alloc_sname(state_name);
552 links = NULL;
553 insert_string(&links, link);
554 state = alloc_link_state(links);
555 set_state(link_id, param->ident->name, param, state);
556 } END_FOR_EACH_PTR(param);
559 static struct smatch_state *merge_links(struct smatch_state *s1, struct smatch_state *s2)
561 struct smatch_state *ret;
562 struct string_list *links;
564 links = combine_string_lists(s1->data, s2->data);
565 ret = alloc_link_state(links);
566 return ret;
569 static void save_link_var_sym(const char *var, struct symbol *sym, const char *link)
571 struct smatch_state *old_state, *new_state;
572 struct string_list *links;
573 char *new;
575 old_state = get_state(link_id, var, sym);
576 if (old_state)
577 links = clone_str_list(old_state->data);
578 else
579 links = NULL;
581 new = alloc_sname(link);
582 insert_string(&links, new);
584 new_state = alloc_link_state(links);
585 set_state(link_id, var, sym, new_state);
588 static void match_inc(struct sm_state *sm)
590 struct string_list *links;
591 struct smatch_state *state;
592 char *tmp;
593 int flip;
594 int op;
596 links = sm->state->data;
598 FOR_EACH_PTR(links, tmp) {
599 state = get_state(compare_id, tmp, NULL);
601 flip = 0;
602 if (strncmp(sm->name, tmp, strlen(sm->name)) != 0 ||
603 tmp[strlen(sm->name)] != ' ')
604 flip = 1;
606 op = state_to_comparison(state);
608 switch (flip ? flip_comparison(op) : op) {
609 case SPECIAL_EQUAL:
610 case SPECIAL_GTE:
611 case SPECIAL_UNSIGNED_GTE:
612 case '>':
613 case SPECIAL_UNSIGNED_GT: {
614 struct compare_data *data = state->data;
615 struct smatch_state *new;
617 new = alloc_compare_state(data->var1, data->vsl1,
618 flip ? '<' : '>',
619 data->var2, data->vsl2);
620 set_state(compare_id, tmp, NULL, new);
621 break;
623 default:
624 set_state(compare_id, tmp, NULL, &undefined);
626 } END_FOR_EACH_PTR(tmp);
629 static void match_dec(struct sm_state *sm)
631 struct string_list *links;
632 struct smatch_state *state;
633 char *tmp;
635 links = sm->state->data;
637 FOR_EACH_PTR(links, tmp) {
638 state = get_state(compare_id, tmp, NULL);
640 switch (state_to_comparison(state)) {
641 case SPECIAL_EQUAL:
642 case SPECIAL_LTE:
643 case SPECIAL_UNSIGNED_LTE:
644 case '<':
645 case SPECIAL_UNSIGNED_LT: {
646 struct compare_data *data = state->data;
647 struct smatch_state *new;
649 new = alloc_compare_state(data->var1, data->vsl1, '<', data->var2, data->vsl2);
650 set_state(compare_id, tmp, NULL, new);
651 break;
653 default:
654 set_state(compare_id, tmp, NULL, &undefined);
656 } END_FOR_EACH_PTR(tmp);
659 static int match_inc_dec(struct sm_state *sm, struct expression *mod_expr)
661 if (!mod_expr)
662 return 0;
663 if (mod_expr->type != EXPR_PREOP && mod_expr->type != EXPR_POSTOP)
664 return 0;
666 if (mod_expr->op == SPECIAL_INCREMENT) {
667 match_inc(sm);
668 return 1;
670 if (mod_expr->op == SPECIAL_DECREMENT) {
671 match_dec(sm);
672 return 1;
674 return 0;
677 static void match_modify(struct sm_state *sm, struct expression *mod_expr)
679 struct string_list *links;
680 char *tmp;
683 * if (foo > bar) then ++foo is also > bar.
685 if (match_inc_dec(sm, mod_expr))
686 return;
688 links = sm->state->data;
690 FOR_EACH_PTR(links, tmp) {
691 set_state(compare_id, tmp, NULL, &undefined);
692 } END_FOR_EACH_PTR(tmp);
693 set_state(link_id, sm->name, sm->sym, &undefined);
696 static char *chunk_to_var_sym(struct expression *expr, struct symbol **sym)
698 expr = strip_expr(expr);
699 if (!expr)
700 return NULL;
701 if (sym)
702 *sym = NULL;
704 if (expr->type == EXPR_PREOP &&
705 (expr->op == SPECIAL_INCREMENT ||
706 expr->op == SPECIAL_DECREMENT))
707 expr = strip_expr(expr->unop);
709 if (expr->type == EXPR_CALL) {
710 char buf[64];
712 snprintf(buf, sizeof(buf), "return %p", expr);
713 return alloc_string(buf);
716 return expr_to_chunk_sym_vsl(expr, sym, NULL);
719 static char *chunk_to_var(struct expression *expr)
721 return chunk_to_var_sym(expr, NULL);
724 static struct smatch_state *get_state_chunk(int owner, struct expression *expr)
726 char *name;
727 struct symbol *sym;
728 struct smatch_state *ret;
730 name = chunk_to_var_sym(expr, &sym);
731 if (!name)
732 return NULL;
734 ret = get_state(owner, name, sym);
735 free_string(name);
736 return ret;
739 static void save_link(struct expression *expr, char *link)
741 char *var;
742 struct symbol *sym;
744 expr = strip_expr(expr);
745 if (expr->type == EXPR_BINOP) {
746 char *chunk;
748 chunk = chunk_to_var(expr);
749 if (!chunk)
750 return;
752 save_link(expr->left, link);
753 save_link(expr->right, link);
754 save_link_var_sym(chunk, NULL, link);
755 return;
758 var = chunk_to_var_sym(expr, &sym);
759 if (!var)
760 return;
762 save_link_var_sym(var, sym, link);
763 free_string(var);
766 static int get_orig_comparison(struct stree *pre_stree, const char *left, const char *right)
768 struct smatch_state *state;
769 struct compare_data *data;
770 int flip = 0;
771 char state_name[256];
773 if (strcmp(left, right) > 0) {
774 const char *tmp = right;
776 flip = 1;
777 right = left;
778 left = tmp;
781 snprintf(state_name, sizeof(state_name), "%s vs %s", left, right);
782 state = get_state_stree(pre_stree, compare_id, state_name, NULL);
783 if (!state || !state->data)
784 return 0;
785 data = state->data;
786 if (flip)
787 return flip_comparison(data->comparison);
788 return data->comparison;
792 static int have_common_var_sym(struct var_sym_list *left_vsl, struct var_sym_list *right_vsl)
794 struct var_sym *tmp;
796 FOR_EACH_PTR(left_vsl, tmp) {
797 if (in_var_sym_list(right_vsl, tmp->var, tmp->sym))
798 return 1;
799 } END_FOR_EACH_PTR(tmp);
801 return 0;
805 * The idea here is that we take a comparison "a < b" and then we look at all
806 * the things which "b" is compared against "b < c" and we say that that implies
807 * a relationship "a < c".
809 * The names here about because the comparisons are organized like this
810 * "a < b < c".
813 static void update_tf_links(struct stree *pre_stree,
814 const char *left_var, struct var_sym_list *left_vsl,
815 int left_comparison, int left_false_comparison,
816 const char *mid_var, struct var_sym_list *mid_vsl,
817 struct string_list *links)
819 struct smatch_state *state;
820 struct smatch_state *true_state, *false_state;
821 struct compare_data *data;
822 const char *right_var;
823 struct var_sym_list *right_vsl;
824 int orig_comparison;
825 int right_comparison;
826 int true_comparison;
827 int false_comparison;
828 char *tmp;
829 char state_name[256];
830 struct var_sym *vs;
832 FOR_EACH_PTR(links, tmp) {
833 state = get_state_stree(pre_stree, compare_id, tmp, NULL);
834 if (!state || !state->data)
835 continue;
836 data = state->data;
837 right_comparison = data->comparison;
838 right_var = data->var2;
839 right_vsl = data->vsl2;
840 if (strcmp(mid_var, right_var) == 0) {
841 right_var = data->var1;
842 right_vsl = data->vsl1;
843 right_comparison = flip_comparison(right_comparison);
845 if (have_common_var_sym(left_vsl, right_vsl))
846 continue;
848 orig_comparison = get_orig_comparison(pre_stree, left_var, right_var);
850 true_comparison = combine_comparisons(left_comparison, right_comparison);
851 false_comparison = combine_comparisons(left_false_comparison, right_comparison);
853 true_comparison = filter_comparison(orig_comparison, true_comparison);
854 false_comparison = filter_comparison(orig_comparison, false_comparison);
856 if (strcmp(left_var, right_var) > 0) {
857 const char *tmp_var = left_var;
858 struct var_sym_list *tmp_vsl = left_vsl;
860 left_var = right_var;
861 left_vsl = right_vsl;
862 right_var = tmp_var;
863 right_vsl = tmp_vsl;
864 true_comparison = flip_comparison(true_comparison);
865 false_comparison = flip_comparison(false_comparison);
868 if (!true_comparison && !false_comparison)
869 continue;
871 if (true_comparison)
872 true_state = alloc_compare_state(left_var, left_vsl, true_comparison, right_var, right_vsl);
873 else
874 true_state = NULL;
875 if (false_comparison)
876 false_state = alloc_compare_state(left_var, left_vsl, false_comparison, right_var, right_vsl);
877 else
878 false_state = NULL;
880 snprintf(state_name, sizeof(state_name), "%s vs %s", left_var, right_var);
881 set_true_false_states(compare_id, state_name, NULL, true_state, false_state);
882 FOR_EACH_PTR(left_vsl, vs) {
883 save_link_var_sym(vs->var, vs->sym, state_name);
884 } END_FOR_EACH_PTR(vs);
885 FOR_EACH_PTR(right_vsl, vs) {
886 save_link_var_sym(vs->var, vs->sym, state_name);
887 } END_FOR_EACH_PTR(vs);
888 if (!vsl_to_sym(left_vsl))
889 save_link_var_sym(left_var, NULL, state_name);
890 if (!vsl_to_sym(right_vsl))
891 save_link_var_sym(right_var, NULL, state_name);
892 } END_FOR_EACH_PTR(tmp);
895 static void update_tf_data(struct stree *pre_stree,
896 const char *left_name, struct var_sym_list *left_vsl,
897 const char *right_name, struct var_sym_list *right_vsl,
898 int true_comparison, int false_comparison)
900 struct smatch_state *state;
902 state = get_state_stree(pre_stree, link_id, right_name, vsl_to_sym(right_vsl));
903 if (state)
904 update_tf_links(pre_stree, left_name, left_vsl, true_comparison, false_comparison, right_name, right_vsl, state->data);
906 state = get_state_stree(pre_stree, link_id, left_name, vsl_to_sym(left_vsl));
907 if (state)
908 update_tf_links(pre_stree, right_name, right_vsl, flip_comparison(true_comparison), flip_comparison(false_comparison), left_name, left_vsl, state->data);
911 static void iter_modify(struct sm_state *sm, struct expression *mod_expr)
913 if (sm->state != &start ||
914 !mod_expr ||
915 (mod_expr->type != EXPR_PREOP && mod_expr->type != EXPR_POSTOP) ||
916 mod_expr->op != SPECIAL_INCREMENT)
917 set_state(inc_dec_id, sm->name, sm->sym, &undefined);
918 else
919 set_state(inc_dec_id, sm->name, sm->sym, &incremented);
922 static void handle_for_loops(struct expression *expr, char *state_name, struct smatch_state *false_state)
924 sval_t sval;
925 char *iter_name, *cap_name;
926 struct symbol *iter_sym, *cap_sym;
927 struct compare_data *data;
929 if (expr->op != '<' && expr->op != SPECIAL_UNSIGNED_LT)
930 return;
932 if (!__cur_stmt || !__prev_stmt)
933 return;
934 if (__cur_stmt->type != STMT_ITERATOR)
935 return;
936 if (__cur_stmt->iterator_pre_condition != expr)
937 return;
939 /* literals are handled in smatch_extra.c */
940 if (get_value(expr->right, &sval))
941 return;
943 /* First time checking the condition */
944 if (__prev_stmt == __cur_stmt->iterator_pre_statement) {
945 if (!get_implied_value(expr->left, &sval) ||
946 sval.value != 0)
947 return;
949 iter_name = expr_to_var_sym(expr->left, &iter_sym);
950 cap_name = expr_to_var_sym(expr->right, &cap_sym);
951 if (!iter_name || !cap_name || !iter_sym || !cap_sym) {
952 free_string(iter_name);
953 free_string(cap_name);
954 return;
957 set_state(inc_dec_id, iter_name, iter_sym, &start);
958 store_link(inc_dec_link_id, cap_name, cap_sym, iter_name, iter_sym);
960 free_string(iter_name);
961 free_string(cap_name);
962 return;
965 /* Second time checking the condtion */
966 if (__prev_stmt != __cur_stmt->iterator_post_statement)
967 return;
969 if (get_state_chunk(inc_dec_id, expr->left) != &incremented)
970 return;
972 data = false_state->data;
973 false_state = alloc_compare_state(data->var1, data->vsl1, SPECIAL_EQUAL, data->var2, data->vsl2);
975 set_true_false_states(compare_id, state_name, NULL, NULL, false_state);
978 static void handle_comparison(struct expression *left_expr, int op, struct expression *right_expr, char **_state_name, struct smatch_state **_false_state)
980 char *left = NULL;
981 char *right = NULL;
982 struct symbol *left_sym, *right_sym;
983 struct var_sym_list *left_vsl, *right_vsl;
984 int false_op;
985 int orig_comparison;
986 struct smatch_state *true_state, *false_state;
987 static char state_name[256];
988 struct stree *pre_stree;
989 sval_t sval;
991 false_op = negate_comparison(op);
993 if (op == SPECIAL_UNSIGNED_LT &&
994 get_implied_value(left_expr, &sval) &&
995 sval.value == 0)
996 false_op = SPECIAL_EQUAL;
998 if (op == SPECIAL_UNSIGNED_GT &&
999 get_implied_value(right_expr, &sval) &&
1000 sval.value == 0)
1001 false_op = SPECIAL_EQUAL;
1003 left = chunk_to_var_sym(left_expr, &left_sym);
1004 if (!left)
1005 goto free;
1006 left_vsl = expr_to_vsl(left_expr);
1007 right = chunk_to_var_sym(right_expr, &right_sym);
1008 if (!right)
1009 goto free;
1010 right_vsl = expr_to_vsl(right_expr);
1012 if (strcmp(left, right) > 0) {
1013 struct symbol *tmp_sym = left_sym;
1014 char *tmp_name = left;
1015 struct var_sym_list *tmp_vsl = left_vsl;
1016 struct expression *tmp_expr = left_expr;
1018 left = right;
1019 left_sym = right_sym;
1020 left_vsl = right_vsl;
1021 left_expr = right_expr;
1022 right = tmp_name;
1023 right_sym = tmp_sym;
1024 right_vsl = tmp_vsl;
1025 right_expr = tmp_expr;
1026 op = flip_comparison(op);
1027 false_op = flip_comparison(false_op);
1030 orig_comparison = get_comparison_strings(left, right);
1031 op = filter_comparison(orig_comparison, op);
1032 false_op = filter_comparison(orig_comparison, false_op);
1034 snprintf(state_name, sizeof(state_name), "%s vs %s", left, right);
1035 true_state = alloc_compare_state(left, left_vsl, op, right, right_vsl);
1036 false_state = alloc_compare_state(left, left_vsl, false_op, right, right_vsl);
1038 pre_stree = clone_stree(__get_cur_stree());
1039 update_tf_data(pre_stree, left, left_vsl, right, right_vsl, op, false_op);
1040 free_stree(&pre_stree);
1042 set_true_false_states(compare_id, state_name, NULL, true_state, false_state);
1043 __compare_param_limit_hook(left_expr, right_expr, state_name, true_state, false_state);
1044 save_link(left_expr, state_name);
1045 save_link(right_expr, state_name);
1047 if (_false_state)
1048 *_false_state = false_state;
1049 if (_state_name)
1050 *_state_name = state_name;
1051 free:
1052 free_string(left);
1053 free_string(right);
1057 void __comparison_match_condition(struct expression *expr)
1059 struct expression *left, *right, *new_left, *new_right;
1060 struct smatch_state *false_state = NULL;
1061 char *state_name = NULL;
1063 if (expr->type != EXPR_COMPARE)
1064 return;
1066 handle_comparison(expr->left, expr->op, expr->right, &state_name, &false_state);
1067 if (false_state && state_name)
1068 handle_for_loops(expr, state_name, false_state);
1071 left = strip_parens(expr->left);
1072 right = strip_parens(expr->right);
1074 if (left->type == EXPR_BINOP && left->op == '+') {
1075 new_left = left->left;
1076 new_right = binop_expression(right, '-', left->right);
1077 handle_comparison(new_left, expr->op, new_right, NULL, NULL);
1079 new_left = left->right;
1080 new_right = binop_expression(right, '-', left->left);
1081 handle_comparison(new_left, expr->op, new_right, NULL, NULL);
1085 static void add_comparison_var_sym(const char *left_name,
1086 struct var_sym_list *left_vsl,
1087 int comparison,
1088 const char *right_name, struct var_sym_list *right_vsl)
1090 struct smatch_state *state;
1091 struct var_sym *vs;
1092 char state_name[256];
1094 if (strcmp(left_name, right_name) > 0) {
1095 const char *tmp_name = left_name;
1096 struct var_sym_list *tmp_vsl = left_vsl;
1098 left_name = right_name;
1099 left_vsl = right_vsl;
1100 right_name = tmp_name;
1101 right_vsl = tmp_vsl;
1102 comparison = flip_comparison(comparison);
1104 snprintf(state_name, sizeof(state_name), "%s vs %s", left_name, right_name);
1105 state = alloc_compare_state(left_name, left_vsl, comparison, right_name, right_vsl);
1107 set_state(compare_id, state_name, NULL, state);
1109 FOR_EACH_PTR(left_vsl, vs) {
1110 save_link_var_sym(vs->var, vs->sym, state_name);
1111 } END_FOR_EACH_PTR(vs);
1112 FOR_EACH_PTR(right_vsl, vs) {
1113 save_link_var_sym(vs->var, vs->sym, state_name);
1114 } END_FOR_EACH_PTR(vs);
1117 static void add_comparison(struct expression *left, int comparison, struct expression *right)
1119 char *left_name = NULL;
1120 char *right_name = NULL;
1121 struct symbol *left_sym, *right_sym;
1122 struct var_sym_list *left_vsl, *right_vsl;
1123 struct smatch_state *state;
1124 char state_name[256];
1126 left_name = chunk_to_var_sym(left, &left_sym);
1127 if (!left_name)
1128 goto free;
1129 left_vsl = expr_to_vsl(left);
1130 right_name = chunk_to_var_sym(right, &right_sym);
1131 if (!right_name)
1132 goto free;
1133 right_vsl = expr_to_vsl(right);
1135 if (strcmp(left_name, right_name) > 0) {
1136 struct symbol *tmp_sym = left_sym;
1137 char *tmp_name = left_name;
1138 struct var_sym_list *tmp_vsl = left_vsl;
1140 left_name = right_name;
1141 left_sym = right_sym;
1142 left_vsl = right_vsl;
1143 right_name = tmp_name;
1144 right_sym = tmp_sym;
1145 right_vsl = tmp_vsl;
1146 comparison = flip_comparison(comparison);
1148 snprintf(state_name, sizeof(state_name), "%s vs %s", left_name, right_name);
1149 state = alloc_compare_state(left_name, left_vsl, comparison, right_name, right_vsl);
1151 set_state(compare_id, state_name, NULL, state);
1152 save_link(left, state_name);
1153 save_link(right, state_name);
1155 free:
1156 free_string(left_name);
1157 free_string(right_name);
1160 static void match_assign_add(struct expression *expr)
1162 struct expression *right;
1163 struct expression *r_left, *r_right;
1164 sval_t left_tmp, right_tmp;
1166 right = strip_expr(expr->right);
1167 r_left = strip_expr(right->left);
1168 r_right = strip_expr(right->right);
1170 get_absolute_min(r_left, &left_tmp);
1171 get_absolute_min(r_right, &right_tmp);
1173 if (left_tmp.value > 0)
1174 add_comparison(expr->left, '>', r_right);
1175 else if (left_tmp.value == 0)
1176 add_comparison(expr->left, SPECIAL_GTE, r_right);
1178 if (right_tmp.value > 0)
1179 add_comparison(expr->left, '>', r_left);
1180 else if (right_tmp.value == 0)
1181 add_comparison(expr->left, SPECIAL_GTE, r_left);
1184 static void match_assign_sub(struct expression *expr)
1186 struct expression *right;
1187 struct expression *r_left, *r_right;
1188 int comparison;
1189 sval_t min;
1191 right = strip_expr(expr->right);
1192 r_left = strip_expr(right->left);
1193 r_right = strip_expr(right->right);
1195 if (get_absolute_min(r_right, &min) && sval_is_negative(min))
1196 return;
1198 comparison = get_comparison(r_left, r_right);
1200 switch (comparison) {
1201 case '>':
1202 case SPECIAL_GTE:
1203 if (implied_not_equal(r_right, 0))
1204 add_comparison(expr->left, '>', r_left);
1205 else
1206 add_comparison(expr->left, SPECIAL_GTE, r_left);
1207 return;
1211 static void match_assign_divide(struct expression *expr)
1213 struct expression *right;
1214 struct expression *r_left, *r_right;
1215 sval_t min;
1217 right = strip_expr(expr->right);
1218 r_left = strip_expr(right->left);
1219 r_right = strip_expr(right->right);
1220 if (!get_implied_min(r_right, &min) || min.value <= 1)
1221 return;
1223 add_comparison(expr->left, '<', r_left);
1226 static void match_binop_assign(struct expression *expr)
1228 struct expression *right;
1230 right = strip_expr(expr->right);
1231 if (right->op == '+')
1232 match_assign_add(expr);
1233 if (right->op == '-')
1234 match_assign_sub(expr);
1235 if (right->op == '/')
1236 match_assign_divide(expr);
1239 static void copy_comparisons(struct expression *left, struct expression *right)
1241 struct string_list *links;
1242 struct smatch_state *state;
1243 struct compare_data *data;
1244 struct symbol *left_sym, *right_sym;
1245 char *left_var = NULL;
1246 char *right_var = NULL;
1247 struct var_sym_list *left_vsl;
1248 const char *var;
1249 struct var_sym_list *vsl;
1250 int comparison;
1251 char *tmp;
1253 left_var = chunk_to_var_sym(left, &left_sym);
1254 if (!left_var)
1255 goto done;
1256 left_vsl = expr_to_vsl(left);
1257 right_var = chunk_to_var_sym(right, &right_sym);
1258 if (!right_var)
1259 goto done;
1261 state = get_state(link_id, right_var, right_sym);
1262 if (!state)
1263 return;
1264 links = state->data;
1266 FOR_EACH_PTR(links, tmp) {
1267 state = get_state(compare_id, tmp, NULL);
1268 if (!state || !state->data)
1269 continue;
1270 data = state->data;
1271 comparison = data->comparison;
1272 var = data->var2;
1273 vsl = data->vsl2;
1274 if (strcmp(var, right_var) == 0) {
1275 var = data->var1;
1276 vsl = data->vsl1;
1277 comparison = flip_comparison(comparison);
1279 add_comparison_var_sym(left_var, left_vsl, comparison, var, vsl);
1280 } END_FOR_EACH_PTR(tmp);
1282 done:
1283 free_string(right_var);
1286 static void match_assign(struct expression *expr)
1288 struct expression *right;
1290 if (expr->op != '=')
1291 return;
1292 if (__in_fake_assign || outside_of_function())
1293 return;
1295 if (is_struct(expr->left))
1296 return;
1298 copy_comparisons(expr->left, expr->right);
1299 add_comparison(expr->left, SPECIAL_EQUAL, expr->right);
1301 right = strip_expr(expr->right);
1302 if (right->type == EXPR_BINOP)
1303 match_binop_assign(expr);
1306 int get_comparison_strings(const char *one, const char *two)
1308 char buf[256];
1309 struct smatch_state *state;
1310 int invert = 0;
1311 int ret = 0;
1313 if (strcmp(one, two) == 0)
1314 return SPECIAL_EQUAL;
1316 if (strcmp(one, two) > 0) {
1317 const char *tmp = one;
1319 one = two;
1320 two = tmp;
1321 invert = 1;
1324 snprintf(buf, sizeof(buf), "%s vs %s", one, two);
1325 state = get_state(compare_id, buf, NULL);
1326 if (state)
1327 ret = state_to_comparison(state);
1329 if (invert)
1330 ret = flip_comparison(ret);
1332 return ret;
1335 int get_comparison(struct expression *a, struct expression *b)
1337 char *one = NULL;
1338 char *two = NULL;
1339 int ret = 0;
1341 one = chunk_to_var(a);
1342 if (!one)
1343 goto free;
1344 two = chunk_to_var(b);
1345 if (!two)
1346 goto free;
1348 ret = get_comparison_strings(one, two);
1349 free:
1350 free_string(one);
1351 free_string(two);
1352 return ret;
1355 int possible_comparison(struct expression *a, int comparison, struct expression *b)
1357 char *one = NULL;
1358 char *two = NULL;
1359 int ret = 0;
1360 char buf[256];
1361 struct sm_state *sm;
1362 int saved;
1364 one = chunk_to_var(a);
1365 if (!one)
1366 goto free;
1367 two = chunk_to_var(b);
1368 if (!two)
1369 goto free;
1372 if (strcmp(one, two) == 0 && comparison == SPECIAL_EQUAL) {
1373 ret = 1;
1374 goto free;
1377 if (strcmp(one, two) > 0) {
1378 char *tmp = one;
1380 one = two;
1381 two = tmp;
1382 comparison = flip_comparison(comparison);
1385 snprintf(buf, sizeof(buf), "%s vs %s", one, two);
1386 sm = get_sm_state(compare_id, buf, NULL);
1387 if (!sm)
1388 goto free;
1390 FOR_EACH_PTR(sm->possible, sm) {
1391 if (!sm->state->data)
1392 continue;
1393 saved = ((struct compare_data *)sm->state->data)->comparison;
1394 if (saved == comparison)
1395 ret = 1;
1396 if (comparison == SPECIAL_EQUAL &&
1397 (saved == SPECIAL_LTE ||
1398 saved == SPECIAL_GTE ||
1399 saved == SPECIAL_UNSIGNED_LTE ||
1400 saved == SPECIAL_UNSIGNED_GTE))
1401 ret = 1;
1402 if (ret == 1)
1403 goto free;
1404 } END_FOR_EACH_PTR(sm);
1406 return ret;
1407 free:
1408 free_string(one);
1409 free_string(two);
1410 return ret;
1413 struct state_list *get_all_comparisons(struct expression *expr)
1415 struct smatch_state *state;
1416 struct string_list *links;
1417 struct state_list *ret = NULL;
1418 struct sm_state *sm;
1419 char *tmp;
1421 state = get_state_chunk(link_id, expr);
1422 if (!state)
1423 return NULL;
1424 links = state->data;
1426 FOR_EACH_PTR(links, tmp) {
1427 sm = get_sm_state(compare_id, tmp, NULL);
1428 if (!sm)
1429 continue;
1430 // FIXME have to compare name with vsl
1431 add_ptr_list(&ret, sm);
1432 } END_FOR_EACH_PTR(tmp);
1434 return ret;
1437 struct state_list *get_all_possible_equal_comparisons(struct expression *expr)
1439 struct smatch_state *state;
1440 struct string_list *links;
1441 struct state_list *ret = NULL;
1442 struct sm_state *sm;
1443 char *tmp;
1445 state = get_state_chunk(link_id, expr);
1446 if (!state)
1447 return NULL;
1448 links = state->data;
1450 FOR_EACH_PTR(links, tmp) {
1451 sm = get_sm_state(compare_id, tmp, NULL);
1452 if (!sm)
1453 continue;
1454 if (!strchr(sm->state->name, '='))
1455 continue;
1456 if (strcmp(sm->state->name, "!=") == 0)
1457 continue;
1458 add_ptr_list(&ret, sm);
1459 } END_FOR_EACH_PTR(tmp);
1461 return ret;
1464 static void update_links_from_call(struct expression *left,
1465 int left_compare,
1466 struct expression *right)
1468 struct string_list *links;
1469 struct smatch_state *state;
1470 struct compare_data *data;
1471 struct symbol *left_sym, *right_sym;
1472 char *left_var = NULL;
1473 char *right_var = NULL;
1474 struct var_sym_list *left_vsl;
1475 const char *var;
1476 struct var_sym_list *vsl;
1477 int comparison;
1478 char *tmp;
1480 left_var = chunk_to_var_sym(left, &left_sym);
1481 if (!left_var)
1482 goto done;
1483 left_vsl = expr_to_vsl(left);
1484 right_var = chunk_to_var_sym(right, &right_sym);
1485 if (!right_var)
1486 goto done;
1488 state = get_state(link_id, right_var, right_sym);
1489 if (!state)
1490 return;
1491 links = state->data;
1493 FOR_EACH_PTR(links, tmp) {
1494 state = get_state(compare_id, tmp, NULL);
1495 if (!state || !state->data)
1496 continue;
1497 data = state->data;
1498 comparison = data->comparison;
1499 var = data->var2;
1500 vsl = data->vsl2;
1501 if (strcmp(var, right_var) == 0) {
1502 var = data->var1;
1503 vsl = data->vsl1;
1504 comparison = flip_comparison(comparison);
1506 comparison = combine_comparisons(left_compare, comparison);
1507 if (!comparison)
1508 continue;
1509 add_comparison_var_sym(left_var, left_vsl, comparison, var, vsl);
1510 } END_FOR_EACH_PTR(tmp);
1512 done:
1513 free_string(right_var);
1516 void __add_return_comparison(struct expression *call, const char *range)
1518 struct expression *arg;
1519 int comparison;
1520 char buf[4];
1522 if (!str_to_comparison_arg(range, call, &comparison, &arg))
1523 return;
1524 snprintf(buf, sizeof(buf), "%s", show_special(comparison));
1525 if (local_debug)
1526 sm_msg("setting %s to %s", expr_to_str(call), buf);
1527 update_links_from_call(call, comparison, arg);
1528 add_comparison(call, comparison, arg);
1531 void __add_comparison_info(struct expression *expr, struct expression *call, const char *range)
1533 copy_comparisons(expr, call);
1536 static char *range_comparison_to_param_helper(struct expression *expr, char starts_with, int ignore)
1538 struct symbol *param;
1539 char *var = NULL;
1540 char buf[256];
1541 char *ret_str = NULL;
1542 int compare;
1543 int i;
1545 var = chunk_to_var(expr);
1546 if (!var)
1547 goto free;
1549 i = -1;
1550 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, param) {
1551 i++;
1552 if (i == ignore)
1553 continue;
1554 if (!param->ident)
1555 continue;
1556 snprintf(buf, sizeof(buf), "%s orig", param->ident->name);
1557 compare = get_comparison_strings(var, buf);
1558 if (!compare)
1559 continue;
1560 if (show_special(compare)[0] != starts_with)
1561 continue;
1562 snprintf(buf, sizeof(buf), "[%s$%d]", show_special(compare), i);
1563 ret_str = alloc_sname(buf);
1564 break;
1565 } END_FOR_EACH_PTR(param);
1567 free:
1568 free_string(var);
1569 return ret_str;
1572 char *name_sym_to_param_comparison(const char *name, struct symbol *sym)
1574 struct symbol *param;
1575 char buf[256];
1576 int compare;
1577 int i;
1579 i = -1;
1580 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, param) {
1581 i++;
1582 if (!param->ident)
1583 continue;
1584 snprintf(buf, sizeof(buf), "%s orig", param->ident->name);
1585 compare = get_comparison_strings(name, buf);
1586 if (!compare)
1587 continue;
1588 snprintf(buf, sizeof(buf), "[%s$%d]", show_special(compare), i);
1589 return alloc_sname(buf);
1590 } END_FOR_EACH_PTR(param);
1592 return NULL;
1595 char *expr_equal_to_param(struct expression *expr, int ignore)
1597 return range_comparison_to_param_helper(expr, '=', ignore);
1600 char *expr_lte_to_param(struct expression *expr, int ignore)
1602 return range_comparison_to_param_helper(expr, '<', ignore);
1605 char *expr_param_comparison(struct expression *expr, int ignore)
1607 struct symbol *param;
1608 char *var = NULL;
1609 char buf[256];
1610 char *ret_str = NULL;
1611 int compare;
1612 int i;
1614 var = chunk_to_var(expr);
1615 if (!var)
1616 goto free;
1618 i = -1;
1619 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, param) {
1620 i++;
1621 if (i == ignore)
1622 continue;
1623 if (!param->ident)
1624 continue;
1625 snprintf(buf, sizeof(buf), "%s orig", param->ident->name);
1626 compare = get_comparison_strings(var, buf);
1627 if (!compare)
1628 continue;
1629 snprintf(buf, sizeof(buf), "[%s$%d]", show_special(compare), i);
1630 ret_str = alloc_sname(buf);
1631 break;
1632 } END_FOR_EACH_PTR(param);
1634 free:
1635 free_string(var);
1636 return ret_str;
1639 char *get_printed_param_name(struct expression *call, const char *param_name, struct symbol *param_sym)
1641 struct expression *arg;
1642 char *name;
1643 struct symbol *sym;
1644 static char buf[256];
1645 int len;
1646 int i;
1648 i = -1;
1649 FOR_EACH_PTR(call->args, arg) {
1650 i++;
1652 name = expr_to_var_sym(arg, &sym);
1653 if (!name || !sym)
1654 continue;
1655 if (sym != param_sym)
1656 continue;
1658 len = strlen(name);
1659 if (strncmp(name, param_name, len) != 0)
1660 continue;
1661 if (param_name[len] == '\0') {
1662 snprintf(buf, sizeof(buf), "$%d", i);
1663 return buf;
1665 if (param_name[len] != '-')
1666 continue;
1667 snprintf(buf, sizeof(buf), "$%d%s", i, param_name + len);
1668 return buf;
1669 } END_FOR_EACH_PTR(arg);
1671 return NULL;
1674 static void match_call_info(struct expression *expr)
1676 struct expression *arg;
1677 struct smatch_state *state;
1678 struct sm_state *sm;
1679 struct compare_data *data;
1680 int comparison;
1681 struct string_list *links;
1682 char *arg_name;
1683 const char *right_name;
1684 char *link;
1685 char info_buf[256];
1686 int i;
1688 i = -1;
1689 FOR_EACH_PTR(expr->args, arg) {
1690 i++;
1692 state = get_state_chunk(link_id, arg);
1693 if (!state)
1694 continue;
1696 links = state->data;
1697 FOR_EACH_PTR(links, link) {
1698 struct var_sym_list *right_vsl;
1699 struct var_sym *right_vs;
1702 if (strstr(link, " orig"))
1703 continue;
1704 sm = get_sm_state(compare_id, link, NULL);
1705 if (!sm)
1706 continue;
1707 data = sm->state->data;
1708 if (!data || !data->comparison)
1709 continue;
1710 arg_name = expr_to_var(arg);
1711 if (!arg_name)
1712 continue;
1714 right_vsl = NULL;
1715 if (strcmp(data->var1, arg_name) == 0) {
1716 comparison = data->comparison;
1717 right_name = data->var2;
1718 right_vsl = data->vsl2;
1719 } else if (strcmp(data->var2, arg_name) == 0) {
1720 comparison = flip_comparison(data->comparison);
1721 right_name = data->var1;
1722 right_vsl = data->vsl1;
1724 if (!right_vsl || ptr_list_size((struct ptr_list *)right_vsl) != 1)
1725 goto free;
1727 right_vs = first_ptr_list((struct ptr_list *)right_vsl);
1728 if (strcmp(right_vs->var, right_name) != 0)
1729 goto free;
1730 right_name = get_printed_param_name(expr, right_vs->var, right_vs->sym);
1731 if (!right_name)
1732 goto free;
1733 snprintf(info_buf, sizeof(info_buf), "%s %s", show_special(comparison), right_name);
1734 sql_insert_caller_info(expr, PARAM_COMPARE, i, "$", info_buf);
1736 free:
1737 free_string(arg_name);
1738 } END_FOR_EACH_PTR(link);
1739 } END_FOR_EACH_PTR(arg);
1742 static void struct_member_callback(struct expression *call, int param, char *printed_name, struct sm_state *link_sm)
1744 struct sm_state *compare_sm;
1745 struct string_list *links;
1746 char *link;
1747 struct compare_data *data;
1748 struct var_sym *left, *right;
1749 static char info_buf[256];
1750 const char *right_name;
1752 if (strstr(printed_name, " orig"))
1753 return;
1755 links = link_sm->state->data;
1756 FOR_EACH_PTR(links, link) {
1757 compare_sm = get_sm_state(compare_id, link, NULL);
1758 if (!compare_sm)
1759 continue;
1760 data = compare_sm->state->data;
1761 if (!data || !data->comparison)
1762 continue;
1764 if (ptr_list_size((struct ptr_list *)data->vsl1) != 1 ||
1765 ptr_list_size((struct ptr_list *)data->vsl2) != 1)
1766 continue;
1767 left = first_ptr_list((struct ptr_list *)data->vsl1);
1768 right = first_ptr_list((struct ptr_list *)data->vsl2);
1769 if (left->sym == right->sym &&
1770 strcmp(left->var, right->var) == 0)
1771 continue;
1773 * Both parameters link to this comparison so only
1774 * record the first one.
1776 if (left->sym != link_sm->sym ||
1777 strcmp(left->var, link_sm->name) != 0)
1778 continue;
1780 right_name = get_printed_param_name(call, right->var, right->sym);
1781 if (!right_name)
1782 continue;
1783 snprintf(info_buf, sizeof(info_buf), "%s %s", show_special(data->comparison), right_name);
1784 sql_insert_caller_info(call, PARAM_COMPARE, param, printed_name, info_buf);
1785 } END_FOR_EACH_PTR(link);
1788 static void print_return_value_comparison(int return_id, char *return_ranges, struct expression *expr)
1790 char *name;
1791 const char *tmp_name;
1792 struct symbol *sym;
1793 int param;
1794 char info_buf[256];
1797 * TODO: This only prints == comparisons. That's probably the most
1798 * useful comparison because == max has lots of implications. But it
1799 * would be good to capture the rest as well.
1801 * This information is already in the DB but it's in the parameter math
1802 * bits and it's awkward to use it. This is is the simpler, possibly
1803 * cleaner way, but not necessarily the best, I don't know.
1806 if (!expr)
1807 return;
1808 name = expr_to_var_sym(expr, &sym);
1809 if (!name || !sym)
1810 goto free;
1812 param = get_param_num_from_sym(sym);
1813 if (param < 0)
1814 goto free;
1815 if (param_was_set_var_sym(name, sym))
1816 goto free;
1818 tmp_name = get_param_name_var_sym(name, sym);
1819 if (!tmp_name)
1820 goto free;
1822 snprintf(info_buf, sizeof(info_buf), "== $%d%s", param, tmp_name + 1);
1823 sql_insert_return_states(return_id, return_ranges,
1824 PARAM_COMPARE, -1, "$", info_buf);
1825 free:
1826 free_string(name);
1829 static void print_return_comparison(int return_id, char *return_ranges, struct expression *expr)
1831 struct sm_state *tmp;
1832 struct string_list *links;
1833 char *link;
1834 struct sm_state *sm;
1835 struct compare_data *data;
1836 struct var_sym *left, *right;
1837 int left_param, right_param;
1838 char left_buf[256];
1839 char right_buf[256];
1840 char info_buf[256];
1841 const char *tmp_name;
1843 print_return_value_comparison(return_id, return_ranges, expr);
1845 FOR_EACH_MY_SM(link_id, __get_cur_stree(), tmp) {
1846 if (get_param_num_from_sym(tmp->sym) < 0)
1847 continue;
1848 links = tmp->state->data;
1849 FOR_EACH_PTR(links, link) {
1850 sm = get_sm_state(compare_id, link, NULL);
1851 if (!sm)
1852 continue;
1853 data = sm->state->data;
1854 if (!data || !data->comparison)
1855 continue;
1856 if (ptr_list_size((struct ptr_list *)data->vsl1) != 1 ||
1857 ptr_list_size((struct ptr_list *)data->vsl2) != 1)
1858 continue;
1859 left = first_ptr_list((struct ptr_list *)data->vsl1);
1860 right = first_ptr_list((struct ptr_list *)data->vsl2);
1861 if (left->sym == right->sym &&
1862 strcmp(left->var, right->var) == 0)
1863 continue;
1865 * Both parameters link to this comparison so only
1866 * record the first one.
1868 if (left->sym != tmp->sym ||
1869 strcmp(left->var, tmp->name) != 0)
1870 continue;
1872 if (strstr(right->var, " orig"))
1873 continue;
1875 left_param = get_param_num_from_sym(left->sym);
1876 right_param = get_param_num_from_sym(right->sym);
1877 if (left_param < 0 || right_param < 0)
1878 continue;
1880 tmp_name = get_param_name_var_sym(left->var, left->sym);
1881 if (!tmp_name)
1882 continue;
1883 snprintf(left_buf, sizeof(left_buf), "%s", tmp_name);
1885 tmp_name = get_param_name_var_sym(right->var, right->sym);
1886 if (!tmp_name || tmp_name[0] != '$')
1887 continue;
1888 snprintf(right_buf, sizeof(right_buf), "$%d%s", right_param, tmp_name + 1);
1891 * FIXME: this should reject $ type variables (as
1892 * opposed to $->foo type). Those should come from
1893 * smatch_param_compare_limit.c.
1896 snprintf(info_buf, sizeof(info_buf), "%s %s", show_special(data->comparison), right_buf);
1897 sql_insert_return_states(return_id, return_ranges,
1898 PARAM_COMPARE, left_param, left_buf, info_buf);
1899 } END_FOR_EACH_PTR(link);
1901 } END_FOR_EACH_SM(tmp);
1904 static int parse_comparison(char **value, int *op)
1907 *op = **value;
1909 switch (*op) {
1910 case '<':
1911 (*value)++;
1912 if (**value == '=') {
1913 (*value)++;
1914 *op = SPECIAL_LTE;
1916 break;
1917 case '=':
1918 (*value)++;
1919 (*value)++;
1920 *op = SPECIAL_EQUAL;
1921 break;
1922 case '!':
1923 (*value)++;
1924 (*value)++;
1925 *op = SPECIAL_NOTEQUAL;
1926 break;
1927 case '>':
1928 (*value)++;
1929 if (**value == '=') {
1930 (*value)++;
1931 *op = SPECIAL_GTE;
1933 break;
1934 default:
1935 return 0;
1938 if (**value != ' ') {
1939 sm_msg("internal error parsing comparison. %s", *value);
1940 return 0;
1943 (*value)++;
1944 return 1;
1947 static int split_op_param_key(char *value, int *op, int *param, char **key)
1949 static char buf[256];
1950 char *p;
1952 if (!parse_comparison(&value, op))
1953 return 0;
1955 snprintf(buf, sizeof(buf), value);
1957 p = buf;
1958 if (*p++ != '$')
1959 return 0;
1961 *param = atoi(p);
1962 if (*param < 0 || *param > 99)
1963 return 0;
1964 p++;
1965 if (*param > 9)
1966 p++;
1967 p--;
1968 *p = '$';
1969 *key = p;
1971 return 1;
1974 static void db_return_comparison(struct expression *expr, int left_param, char *key, char *value)
1976 struct expression *left_arg, *right_arg;
1977 char *left_name = NULL;
1978 struct symbol *left_sym;
1979 char *right_name = NULL;
1980 struct symbol *right_sym;
1981 int op;
1982 int right_param;
1983 char *right_key;
1984 struct var_sym_list *left_vsl = NULL, *right_vsl = NULL;
1986 if (left_param == -1) {
1987 if (expr->type != EXPR_ASSIGNMENT)
1988 return;
1989 left_arg = strip_expr(expr->left);
1991 while (expr->type == EXPR_ASSIGNMENT)
1992 expr = strip_expr(expr->right);
1993 if (expr->type != EXPR_CALL)
1994 return;
1995 } else {
1996 while (expr->type == EXPR_ASSIGNMENT)
1997 expr = strip_expr(expr->right);
1998 if (expr->type != EXPR_CALL)
1999 return;
2001 left_arg = get_argument_from_call_expr(expr->args, left_param);
2002 if (!left_arg)
2003 return;
2006 if (!split_op_param_key(value, &op, &right_param, &right_key))
2007 return;
2009 right_arg = get_argument_from_call_expr(expr->args, right_param);
2010 if (!right_arg)
2011 return;
2013 left_name = get_variable_from_key(left_arg, key, &left_sym);
2014 if (!left_name || !left_sym)
2015 goto free;
2017 right_name = get_variable_from_key(right_arg, right_key, &right_sym);
2018 if (!right_name || !right_sym)
2019 goto free;
2021 add_var_sym(&left_vsl, left_name, left_sym);
2022 add_var_sym(&right_vsl, right_name, right_sym);
2024 add_comparison_var_sym(left_name, left_vsl, op, right_name, right_vsl);
2026 free:
2027 free_string(left_name);
2028 free_string(right_name);
2031 int param_compare_limit_is_impossible(struct expression *expr, int left_param, char *left_key, char *value)
2033 struct smatch_state *state;
2034 char *left_name = NULL;
2035 char *right_name = NULL;
2036 struct symbol *left_sym, *right_sym;
2037 struct expression *left_arg, *right_arg;
2038 int op;
2039 int right_param;
2040 char *right_key;
2041 int ret = 0;
2042 char buf[256];
2044 while (expr->type == EXPR_ASSIGNMENT)
2045 expr = strip_expr(expr->right);
2046 if (expr->type != EXPR_CALL)
2047 return 0;
2049 if (!split_op_param_key(value, &op, &right_param, &right_key))
2050 return 0;
2052 left_arg = get_argument_from_call_expr(expr->args, left_param);
2053 if (!left_arg)
2054 return 0;
2056 right_arg = get_argument_from_call_expr(expr->args, right_param);
2057 if (!right_arg)
2058 return 0;
2060 left_name = get_variable_from_key(left_arg, left_key, &left_sym);
2061 right_name = get_variable_from_key(right_arg, right_key, &right_sym);
2062 if (!left_name || !right_name)
2063 goto free;
2065 snprintf(buf, sizeof(buf), "%s vs %s", left_name, right_name);
2066 state = get_state(compare_id, buf, NULL);
2067 if (!state)
2068 goto free;
2069 if (!state_to_comparison(state))
2070 goto free;
2072 if (!filter_comparison(state_to_comparison(state), op))
2073 ret = 1;
2074 free:
2075 free_string(left_name);
2076 free_string(right_name);
2077 return ret;
2080 static void free_data(struct symbol *sym)
2082 if (__inline_fn)
2083 return;
2084 clear_compare_data_alloc();
2087 void register_comparison(int id)
2089 compare_id = id;
2090 add_hook(&save_start_states, AFTER_DEF_HOOK);
2091 add_unmatched_state_hook(compare_id, unmatched_comparison);
2092 add_merge_hook(compare_id, &merge_compare_states);
2093 add_hook(&free_data, AFTER_FUNC_HOOK);
2094 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
2095 add_split_return_callback(&print_return_comparison);
2097 select_return_states_hook(PARAM_COMPARE, &db_return_comparison);
2100 void register_comparison_late(int id)
2102 add_hook(&match_assign, ASSIGNMENT_HOOK);
2105 void register_comparison_links(int id)
2107 link_id = id;
2108 add_merge_hook(link_id, &merge_links);
2109 add_modification_hook(link_id, &match_modify);
2111 add_member_info_callback(link_id, struct_member_callback);
2114 void register_comparison_inc_dec(int id)
2116 inc_dec_id = id;
2117 add_modification_hook_late(inc_dec_id, &iter_modify);
2120 void register_comparison_inc_dec_links(int id)
2122 inc_dec_link_id = id;
2123 set_up_link_functions(inc_dec_id, inc_dec_link_id);
2126 static void filter_by_sm(struct sm_state *sm, int op,
2127 struct state_list **true_stack,
2128 struct state_list **false_stack)
2130 struct compare_data *data;
2131 int istrue = 0;
2132 int isfalse = 0;
2134 if (!sm)
2135 return;
2136 data = sm->state->data;
2137 if (!data) {
2138 if (sm->merged) {
2139 filter_by_sm(sm->left, op, true_stack, false_stack);
2140 filter_by_sm(sm->right, op, true_stack, false_stack);
2142 return;
2145 if (data->comparison &&
2146 data->comparison == filter_comparison(data->comparison, op))
2147 istrue = 1;
2149 if (data->comparison &&
2150 data->comparison == filter_comparison(data->comparison, negate_comparison(op)))
2151 isfalse = 1;
2153 if (istrue)
2154 add_ptr_list(true_stack, sm);
2155 if (isfalse)
2156 add_ptr_list(false_stack, sm);
2158 if (sm->merged) {
2159 filter_by_sm(sm->left, op, true_stack, false_stack);
2160 filter_by_sm(sm->right, op, true_stack, false_stack);
2164 struct sm_state *comparison_implication_hook(struct expression *expr,
2165 struct state_list **true_stack,
2166 struct state_list **false_stack)
2168 struct sm_state *sm;
2169 char *left, *right;
2170 int op;
2171 static char buf[256];
2173 if (expr->type != EXPR_COMPARE)
2174 return NULL;
2176 op = expr->op;
2178 left = expr_to_var(expr->left);
2179 right = expr_to_var(expr->right);
2180 if (!left || !right) {
2181 free_string(left);
2182 free_string(right);
2183 return NULL;
2186 if (strcmp(left, right) > 0) {
2187 char *tmp = left;
2189 left = right;
2190 right = tmp;
2191 op = flip_comparison(op);
2194 snprintf(buf, sizeof(buf), "%s vs %s", left, right);
2195 sm = get_sm_state(compare_id, buf, NULL);
2196 if (!sm)
2197 return NULL;
2198 if (!sm->merged)
2199 return NULL;
2201 filter_by_sm(sm, op, true_stack, false_stack);
2202 if (!*true_stack && !*false_stack)
2203 return NULL;
2205 return sm;