allocations: set the ->total_size, ->nr_elem and ->elem_size expressions
[smatch.git] / smatch_comparison.c
blob153c45aa8ffa43b9e5e4d56db4b9709d8527b941
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 int comparison_id;
36 static int link_id;
37 static int inc_dec_id;
38 static int inc_dec_link_id;
40 static void add_comparison(struct expression *left, int comparison, struct expression *right);
42 /* for handling for loops */
43 STATE(start);
44 STATE(incremented);
46 ALLOCATOR(compare_data, "compare data");
48 static struct symbol *vsl_to_sym(struct var_sym_list *vsl)
50 struct var_sym *vs;
52 if (!vsl)
53 return NULL;
54 if (ptr_list_size((struct ptr_list *)vsl) != 1)
55 return NULL;
56 vs = first_ptr_list((struct ptr_list *)vsl);
57 return vs->sym;
60 static const char *show_comparison(int comparison)
62 if (comparison == IMPOSSIBLE_COMPARISON)
63 return "impossible";
64 if (comparison == UNKNOWN_COMPARISON)
65 return "unknown";
66 return show_special(comparison);
69 struct smatch_state *alloc_compare_state(
70 struct expression *left,
71 const char *left_var, struct var_sym_list *left_vsl,
72 int comparison,
73 struct expression *right,
74 const char *right_var, struct var_sym_list *right_vsl)
76 struct smatch_state *state;
77 struct compare_data *data;
79 state = __alloc_smatch_state(0);
80 state->name = alloc_sname(show_comparison(comparison));
81 data = __alloc_compare_data(0);
82 data->left = left;
83 data->left_var = alloc_sname(left_var);
84 data->left_vsl = clone_var_sym_list(left_vsl);
85 data->comparison = comparison;
86 data->right = right;
87 data->right_var = alloc_sname(right_var);
88 data->right_vsl = clone_var_sym_list(right_vsl);
89 state->data = data;
90 return state;
93 int state_to_comparison(struct smatch_state *state)
95 if (!state || !state->data)
96 return UNKNOWN_COMPARISON;
97 return ((struct compare_data *)state->data)->comparison;
101 * flip_comparison() reverses the op left and right. So "x >= y" becomes "y <= x".
103 int flip_comparison(int op)
105 switch (op) {
106 case UNKNOWN_COMPARISON:
107 return UNKNOWN_COMPARISON;
108 case '<':
109 return '>';
110 case SPECIAL_UNSIGNED_LT:
111 return SPECIAL_UNSIGNED_GT;
112 case SPECIAL_LTE:
113 return SPECIAL_GTE;
114 case SPECIAL_UNSIGNED_LTE:
115 return SPECIAL_UNSIGNED_GTE;
116 case SPECIAL_EQUAL:
117 return SPECIAL_EQUAL;
118 case SPECIAL_NOTEQUAL:
119 return SPECIAL_NOTEQUAL;
120 case SPECIAL_GTE:
121 return SPECIAL_LTE;
122 case SPECIAL_UNSIGNED_GTE:
123 return SPECIAL_UNSIGNED_LTE;
124 case '>':
125 return '<';
126 case SPECIAL_UNSIGNED_GT:
127 return SPECIAL_UNSIGNED_LT;
128 case IMPOSSIBLE_COMPARISON:
129 return UNKNOWN_COMPARISON;
130 default:
131 sm_perror("unhandled comparison %d", op);
132 return op;
136 int negate_comparison(int op)
138 switch (op) {
139 case UNKNOWN_COMPARISON:
140 return UNKNOWN_COMPARISON;
141 case '<':
142 return SPECIAL_GTE;
143 case SPECIAL_UNSIGNED_LT:
144 return SPECIAL_UNSIGNED_GTE;
145 case SPECIAL_LTE:
146 return '>';
147 case SPECIAL_UNSIGNED_LTE:
148 return SPECIAL_UNSIGNED_GT;
149 case SPECIAL_EQUAL:
150 return SPECIAL_NOTEQUAL;
151 case SPECIAL_NOTEQUAL:
152 return SPECIAL_EQUAL;
153 case SPECIAL_GTE:
154 return '<';
155 case SPECIAL_UNSIGNED_GTE:
156 return SPECIAL_UNSIGNED_LT;
157 case '>':
158 return SPECIAL_LTE;
159 case SPECIAL_UNSIGNED_GT:
160 return SPECIAL_UNSIGNED_LTE;
161 case IMPOSSIBLE_COMPARISON:
162 return UNKNOWN_COMPARISON;
163 default:
164 sm_perror("unhandled comparison %d", op);
165 return op;
169 static int rl_comparison(struct range_list *left_rl, struct range_list *right_rl)
171 sval_t left_min, left_max, right_min, right_max;
172 struct symbol *type = &int_ctype;
174 if (!left_rl || !right_rl)
175 return UNKNOWN_COMPARISON;
177 if (type_positive_bits(rl_type(left_rl)) > type_positive_bits(type))
178 type = rl_type(left_rl);
179 if (type_positive_bits(rl_type(right_rl)) > type_positive_bits(type))
180 type = rl_type(right_rl);
182 left_rl = cast_rl(type, left_rl);
183 right_rl = cast_rl(type, right_rl);
185 left_min = rl_min(left_rl);
186 left_max = rl_max(left_rl);
187 right_min = rl_min(right_rl);
188 right_max = rl_max(right_rl);
190 if (left_min.value == left_max.value &&
191 right_min.value == right_max.value &&
192 left_min.value == right_min.value)
193 return SPECIAL_EQUAL;
195 if (sval_cmp(left_max, right_min) < 0)
196 return '<';
197 if (sval_cmp(left_max, right_min) == 0)
198 return SPECIAL_LTE;
199 if (sval_cmp(left_min, right_max) > 0)
200 return '>';
201 if (sval_cmp(left_min, right_max) == 0)
202 return SPECIAL_GTE;
204 return UNKNOWN_COMPARISON;
207 static int comparison_from_extra(struct expression *a, struct expression *b)
209 struct range_list *left, *right;
211 if (!get_implied_rl(a, &left))
212 return UNKNOWN_COMPARISON;
213 if (!get_implied_rl(b, &right))
214 return UNKNOWN_COMPARISON;
216 return rl_comparison(left, right);
219 static struct range_list *get_orig_rl(struct var_sym_list *vsl)
221 struct symbol *sym;
222 struct smatch_state *state;
224 if (!vsl)
225 return NULL;
226 sym = vsl_to_sym(vsl);
227 if (!sym || !sym->ident)
228 return NULL;
229 state = get_orig_estate(sym->ident->name, sym);
230 return estate_rl(state);
233 static struct smatch_state *unmatched_comparison(struct sm_state *sm)
235 struct compare_data *data = sm->state->data;
236 struct range_list *left_rl, *right_rl;
237 int op = UNKNOWN_COMPARISON;
239 if (!data)
240 return &undefined;
242 if (is_impossible_path()) {
243 op = IMPOSSIBLE_COMPARISON;
244 goto alloc;
247 if (strstr(data->left_var, " orig"))
248 left_rl = get_orig_rl(data->left_vsl);
249 else if (!get_implied_rl_var_sym(data->left_var, vsl_to_sym(data->left_vsl), &left_rl))
250 goto alloc;
252 if (strstr(data->right_var, " orig"))
253 right_rl = get_orig_rl(data->right_vsl);
254 else if (!get_implied_rl_var_sym(data->right_var, vsl_to_sym(data->right_vsl), &right_rl))
255 goto alloc;
257 op = rl_comparison(left_rl, right_rl);
259 alloc:
260 return alloc_compare_state(data->left, data->left_var, data->left_vsl,
262 data->right, data->right_var, data->right_vsl);
265 /* remove_unsigned_from_comparison() is obviously a hack. */
266 int remove_unsigned_from_comparison(int op)
268 switch (op) {
269 case SPECIAL_UNSIGNED_LT:
270 return '<';
271 case SPECIAL_UNSIGNED_LTE:
272 return SPECIAL_LTE;
273 case SPECIAL_UNSIGNED_GTE:
274 return SPECIAL_GTE;
275 case SPECIAL_UNSIGNED_GT:
276 return '>';
277 default:
278 return op;
283 * This is for when you merge states "a < b" and "a == b", the result is that
284 * we can say for sure, "a <= b" after the merge.
286 int merge_comparisons(int one, int two)
288 int LT, EQ, GT;
290 if (one == UNKNOWN_COMPARISON || two == UNKNOWN_COMPARISON)
291 return UNKNOWN_COMPARISON;
293 if (one == IMPOSSIBLE_COMPARISON)
294 return two;
295 if (two == IMPOSSIBLE_COMPARISON)
296 return one;
298 one = remove_unsigned_from_comparison(one);
299 two = remove_unsigned_from_comparison(two);
301 if (one == two)
302 return one;
304 LT = EQ = GT = 0;
306 switch (one) {
307 case '<':
308 LT = 1;
309 break;
310 case SPECIAL_LTE:
311 LT = 1;
312 EQ = 1;
313 break;
314 case SPECIAL_EQUAL:
315 EQ = 1;
316 break;
317 case SPECIAL_GTE:
318 GT = 1;
319 EQ = 1;
320 break;
321 case '>':
322 GT = 1;
325 switch (two) {
326 case '<':
327 LT = 1;
328 break;
329 case SPECIAL_LTE:
330 LT = 1;
331 EQ = 1;
332 break;
333 case SPECIAL_EQUAL:
334 EQ = 1;
335 break;
336 case SPECIAL_GTE:
337 GT = 1;
338 EQ = 1;
339 break;
340 case '>':
341 GT = 1;
344 if (LT && EQ && GT)
345 return UNKNOWN_COMPARISON;
346 if (LT && EQ)
347 return SPECIAL_LTE;
348 if (LT && GT)
349 return SPECIAL_NOTEQUAL;
350 if (LT)
351 return '<';
352 if (EQ && GT)
353 return SPECIAL_GTE;
354 if (GT)
355 return '>';
356 return UNKNOWN_COMPARISON;
360 * This is for if you have "a < b" and "b <= c" and you want to see how "a
361 * compares to c". You would call this like get_combined_comparison('<', '<=').
362 * The return comparison would be '<'.
364 int combine_comparisons(int left_compare, int right_compare)
366 int LT, EQ, GT;
368 left_compare = remove_unsigned_from_comparison(left_compare);
369 right_compare = remove_unsigned_from_comparison(right_compare);
371 LT = EQ = GT = 0;
373 switch (left_compare) {
374 case '<':
375 LT++;
376 break;
377 case SPECIAL_LTE:
378 LT++;
379 EQ++;
380 break;
381 case SPECIAL_EQUAL:
382 return right_compare;
383 case SPECIAL_GTE:
384 GT++;
385 EQ++;
386 break;
387 case '>':
388 GT++;
391 switch (right_compare) {
392 case '<':
393 LT++;
394 break;
395 case SPECIAL_LTE:
396 LT++;
397 EQ++;
398 break;
399 case SPECIAL_EQUAL:
400 return left_compare;
401 case SPECIAL_GTE:
402 GT++;
403 EQ++;
404 break;
405 case '>':
406 GT++;
409 if (LT == 2) {
410 if (EQ == 2)
411 return SPECIAL_LTE;
412 return '<';
415 if (GT == 2) {
416 if (EQ == 2)
417 return SPECIAL_GTE;
418 return '>';
420 return UNKNOWN_COMPARISON;
424 * This is mostly used when you know from extra state that a <= b but you
425 * know from comparisons that a != b so then if take the intersection then
426 * we know that a < b. The name is taken from the fact that the intersection
427 * of < and <= is <.
429 int comparison_intersection(int left_compare, int right_compare)
431 int LT, GT, EQ, NE, total;
433 if (left_compare == IMPOSSIBLE_COMPARISON ||
434 right_compare == IMPOSSIBLE_COMPARISON)
435 return IMPOSSIBLE_COMPARISON;
437 left_compare = remove_unsigned_from_comparison(left_compare);
438 right_compare = remove_unsigned_from_comparison(right_compare);
440 LT = GT = EQ = NE = total = 0;
442 /* Only one side is known. */
443 if (!left_compare)
444 return right_compare;
445 if (!right_compare)
446 return left_compare;
448 switch (left_compare) {
449 case '<':
450 LT++;
451 total += 1;
452 break;
453 case SPECIAL_LTE:
454 LT++;
455 EQ++;
456 total += 2;
457 break;
458 case SPECIAL_EQUAL:
459 EQ++;
460 total += 1;
461 break;
462 case SPECIAL_NOTEQUAL:
463 NE++;
464 total += 1;
465 break;
466 case SPECIAL_GTE:
467 GT++;
468 EQ++;
469 total += 2;
470 break;
471 case '>':
472 GT++;
473 total += 1;
474 break;
475 default:
476 return UNKNOWN_COMPARISON;
479 switch (right_compare) {
480 case '<':
481 LT++;
482 total += 1;
483 break;
484 case SPECIAL_LTE:
485 LT++;
486 EQ++;
487 total += 2;
488 break;
489 case SPECIAL_EQUAL:
490 EQ++;
491 total += 1;
492 break;
493 case SPECIAL_NOTEQUAL:
494 NE++;
495 total += 1;
496 break;
497 case SPECIAL_GTE:
498 GT++;
499 EQ++;
500 total += 2;
501 break;
502 case '>':
503 GT++;
504 total += 1;
505 break;
506 default:
507 return UNKNOWN_COMPARISON;
510 if (LT == 2) {
511 if (EQ == 2)
512 return SPECIAL_LTE;
513 return '<';
516 if (GT == 2) {
517 if (EQ == 2)
518 return SPECIAL_GTE;
519 return '>';
521 if (EQ == 2)
522 return SPECIAL_EQUAL;
523 if (total == 2 && EQ && NE)
524 return IMPOSSIBLE_COMPARISON;
525 if (GT && LT)
526 return IMPOSSIBLE_COMPARISON;
527 if (GT && NE)
528 return '>';
529 if (LT && NE)
530 return '<';
531 if (NE == 2)
532 return SPECIAL_NOTEQUAL;
533 if (total == 2 && (LT || GT) && EQ)
534 return IMPOSSIBLE_COMPARISON;
536 return UNKNOWN_COMPARISON;
539 static void pre_merge_hook(struct sm_state *cur, struct sm_state *other)
541 struct compare_data *data = cur->state->data;
542 int extra, new;
543 static bool in_recurse;
545 // FIXME. No data is useless
546 if (!data)
547 return;
549 if (in_recurse)
550 return;
551 in_recurse = true;
552 extra = comparison_from_extra(data->left, data->right);
553 in_recurse = false;
554 if (!extra)
555 return;
556 new = comparison_intersection(extra, data->comparison);
557 if (new == data->comparison)
558 return;
560 // FIXME: we should always preserve implications
561 set_state(comparison_id, cur->name, NULL,
562 alloc_compare_state(data->left, data->left_var, data->left_vsl,
563 new,
564 data->right, data->right_var, data->right_vsl));
567 struct smatch_state *merge_compare_states(struct smatch_state *s1, struct smatch_state *s2)
569 struct compare_data *data = s1->data;
570 int op;
572 if (!data)
573 return &undefined;
575 op = merge_comparisons(state_to_comparison(s1), state_to_comparison(s2));
576 return alloc_compare_state(
577 data->left, data->left_var, data->left_vsl,
579 data->right, data->right_var, data->right_vsl);
582 static struct smatch_state *alloc_link_state(struct string_list *links)
584 struct smatch_state *state;
585 static char buf[256] = "";
586 int cnt = 0;
587 char *tmp;
589 state = __alloc_smatch_state(0);
591 FOR_EACH_PTR(links, tmp) {
592 cnt += snprintf(buf + cnt, sizeof(buf) - cnt, "%s%s", cnt ? ", " : "", tmp);
593 if (cnt >= sizeof(buf))
594 goto done;
595 } END_FOR_EACH_PTR(tmp);
597 done:
598 state->name = alloc_sname(buf);
599 state->data = links;
600 return state;
603 static void save_link_var_sym(const char *var, struct symbol *sym, const char *link)
605 struct smatch_state *old_state, *new_state;
606 struct string_list *links;
607 char *new;
609 old_state = get_state(link_id, var, sym);
610 if (old_state)
611 links = clone_str_list(old_state->data);
612 else
613 links = NULL;
615 new = alloc_sname(link);
616 insert_string(&links, new);
618 new_state = alloc_link_state(links);
619 set_state(link_id, var, sym, new_state);
622 static void save_start_states(struct statement *stmt)
624 struct symbol *param;
625 char orig[64];
626 char state_name[128];
627 struct smatch_state *state;
629 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, param) {
630 struct var_sym_list *left_vsl = NULL;
631 struct var_sym_list *right_vsl = NULL;
633 if (!param->ident)
634 continue;
635 snprintf(orig, sizeof(orig), "%s orig", param->ident->name);
636 snprintf(state_name, sizeof(state_name), "%s vs %s", param->ident->name, orig);
637 add_var_sym(&left_vsl, param->ident->name, param);
638 add_var_sym(&right_vsl, orig, param);
639 state = alloc_compare_state(
640 NULL, param->ident->name, left_vsl,
641 SPECIAL_EQUAL,
642 NULL, alloc_sname(orig), right_vsl);
643 set_state(comparison_id, state_name, NULL, state);
644 save_link_var_sym(param->ident->name, param, state_name);
645 } END_FOR_EACH_PTR(param);
648 static struct smatch_state *merge_links(struct smatch_state *s1, struct smatch_state *s2)
650 struct smatch_state *ret;
651 struct string_list *links;
653 links = combine_string_lists(s1->data, s2->data);
654 ret = alloc_link_state(links);
655 return ret;
658 static void match_inc(struct sm_state *sm, bool preserve)
660 struct string_list *links;
661 struct smatch_state *state, *new;
662 struct compare_data *data;
663 char *tmp;
664 int flip;
665 int op;
667 links = sm->state->data;
669 FOR_EACH_PTR(links, tmp) {
670 state = get_state(comparison_id, tmp, NULL);
671 if (!state)
672 continue;
673 data = state->data;
674 if (!data)
675 continue;
677 flip = 0;
678 if (strncmp(sm->name, tmp, strlen(sm->name)) != 0 ||
679 tmp[strlen(sm->name)] != ' ')
680 flip = 1;
682 op = state_to_comparison(state);
684 switch (flip ? flip_comparison(op) : op) {
685 case SPECIAL_EQUAL:
686 case SPECIAL_GTE:
687 case SPECIAL_UNSIGNED_GTE:
688 case '>':
689 case SPECIAL_UNSIGNED_GT:
690 if (preserve)
691 break;
692 new = alloc_compare_state(
693 data->left, data->left_var, data->left_vsl,
694 flip ? '<' : '>',
695 data->right, data->right_var, data->right_vsl);
696 set_state(comparison_id, tmp, NULL, new);
697 break;
698 case '<':
699 case SPECIAL_UNSIGNED_LT:
700 new = alloc_compare_state(
701 data->left, data->left_var, data->left_vsl,
702 flip ? SPECIAL_GTE : SPECIAL_LTE,
703 data->right, data->right_var, data->right_vsl);
704 set_state(comparison_id, tmp, NULL, new);
705 break;
706 default:
707 new = alloc_compare_state(
708 data->left, data->left_var, data->left_vsl,
709 UNKNOWN_COMPARISON,
710 data->right, data->right_var, data->right_vsl);
711 set_state(comparison_id, tmp, NULL, new);
713 } END_FOR_EACH_PTR(tmp);
716 static void match_dec(struct sm_state *sm, bool preserve)
718 struct string_list *links;
719 struct smatch_state *state;
720 char *tmp;
722 links = sm->state->data;
724 FOR_EACH_PTR(links, tmp) {
725 struct compare_data *data;
726 struct smatch_state *new;
728 state = get_state(comparison_id, tmp, NULL);
729 if (!state || !state->data)
730 continue;
732 data = state->data;
734 switch (state_to_comparison(state)) {
735 case SPECIAL_EQUAL:
736 case SPECIAL_LTE:
737 case SPECIAL_UNSIGNED_LTE:
738 case '<':
739 case SPECIAL_UNSIGNED_LT: {
740 if (preserve)
741 break;
743 new = alloc_compare_state(
744 data->left, data->left_var, data->left_vsl,
745 '<',
746 data->right, data->right_var, data->right_vsl);
747 set_state(comparison_id, tmp, NULL, new);
748 break;
750 default:
751 new = alloc_compare_state(
752 data->left, data->left_var, data->left_vsl,
753 UNKNOWN_COMPARISON,
754 data->right, data->right_var, data->right_vsl);
755 set_state(comparison_id, tmp, NULL, new);
757 } END_FOR_EACH_PTR(tmp);
760 static void reset_sm(struct sm_state *sm)
762 struct string_list *links;
763 char *tmp;
765 links = sm->state->data;
767 FOR_EACH_PTR(links, tmp) {
768 struct smatch_state *old, *new;
770 old = get_state(comparison_id, tmp, NULL);
771 if (!old || !old->data) {
772 new = &undefined;
773 } else {
774 struct compare_data *data = old->data;
776 new = alloc_compare_state(
777 data->left, data->left_var, data->left_vsl,
778 UNKNOWN_COMPARISON,
779 data->right, data->right_var, data->right_vsl);
781 set_state(comparison_id, tmp, NULL, new);
782 } END_FOR_EACH_PTR(tmp);
783 set_state(link_id, sm->name, sm->sym, &undefined);
786 static bool match_add_sub_assign(struct sm_state *sm, struct expression *expr)
788 struct range_list *rl;
789 sval_t zero = { .type = &int_ctype };
791 if (!expr || expr->type != EXPR_ASSIGNMENT)
792 return false;
793 if (expr->op != SPECIAL_ADD_ASSIGN && expr->op != SPECIAL_SUB_ASSIGN)
794 return false;
796 get_absolute_rl(expr->right, &rl);
797 if (sval_is_negative(rl_min(rl))) {
798 reset_sm(sm);
799 return false;
802 if (expr->op == SPECIAL_ADD_ASSIGN)
803 match_inc(sm, rl_has_sval(rl, zero));
804 else
805 match_dec(sm, rl_has_sval(rl, zero));
806 return true;
809 static void match_inc_dec(struct sm_state *sm, struct expression *mod_expr)
812 * if (foo > bar) then ++foo is also > bar.
814 if (!mod_expr)
815 return;
816 if (match_add_sub_assign(sm, mod_expr))
817 return;
818 if (mod_expr->type != EXPR_PREOP && mod_expr->type != EXPR_POSTOP)
819 return;
821 if (mod_expr->op == SPECIAL_INCREMENT)
822 match_inc(sm, false);
823 else if (mod_expr->op == SPECIAL_DECREMENT)
824 match_dec(sm, false);
827 static int is_self_assign(struct expression *expr)
829 if (!expr || expr->type != EXPR_ASSIGNMENT || expr->op != '=')
830 return 0;
831 return expr_equiv(expr->left, expr->right);
834 static struct expression *ignore_mod_expr;
835 static void match_modify(struct sm_state *sm, struct expression *mod_expr)
837 if (mod_expr && is_self_assign(mod_expr))
838 return;
839 if (mod_expr && mod_expr == ignore_mod_expr)
840 return;
842 /* handled by match_inc_dec() */
843 if (mod_expr &&
844 ((mod_expr->type == EXPR_PREOP || mod_expr->type == EXPR_POSTOP) &&
845 (mod_expr->op == SPECIAL_INCREMENT || mod_expr->op == SPECIAL_DECREMENT)))
846 return;
847 if (mod_expr && mod_expr->type == EXPR_ASSIGNMENT &&
848 (mod_expr->op == SPECIAL_ADD_ASSIGN || mod_expr->op == SPECIAL_SUB_ASSIGN))
849 return;
851 reset_sm(sm);
854 static void match_preop(struct expression *expr)
856 struct expression *parent;
857 struct range_list *left, *right;
858 int op;
861 * This is an important special case. Say you have:
863 * if (++j == limit)
865 * Assume that we know the range of limit is higher than the start
866 * value for "j". Then the first thing that we process is the ++j. We
867 * have not comparison states set up so it doesn't get caught by the
868 * modification hook. But it does get caught by smatch_extra which sets
869 * j to unknown then we parse the "j == limit" and sets false to != but
870 * really we want false to be <.
872 * So what we do is we set j < limit here, then the match_modify catches
873 * it and we do a match_inc_dec().
877 if (expr->type != EXPR_PREOP ||
878 (expr->op != SPECIAL_INCREMENT && expr->op != SPECIAL_DECREMENT))
879 return;
881 parent = expr_get_parent_expr(expr);
882 if (!parent)
883 return;
884 if (parent->type != EXPR_COMPARE || parent->op != SPECIAL_EQUAL)
885 return;
886 if (parent->left != expr)
887 return;
889 if (!get_implied_rl(expr->unop, &left) ||
890 !get_implied_rl(parent->right, &right))
891 return;
893 op = rl_comparison(left, right);
894 if (op == UNKNOWN_COMPARISON)
895 return;
897 add_comparison(expr->unop, op, parent->right);
900 static char *chunk_to_var_sym(struct expression *expr, struct symbol **sym)
902 expr = strip_expr(expr);
903 if (!expr)
904 return NULL;
905 if (sym)
906 *sym = NULL;
908 if (expr->type == EXPR_PREOP &&
909 (expr->op == SPECIAL_INCREMENT ||
910 expr->op == SPECIAL_DECREMENT))
911 expr = strip_expr(expr->unop);
913 if (expr->type == EXPR_CALL) {
914 char buf[64];
916 snprintf(buf, sizeof(buf), "return %p", expr);
917 return alloc_string(buf);
920 return expr_to_chunk_sym_vsl(expr, sym, NULL);
923 static char *chunk_to_var(struct expression *expr)
925 return chunk_to_var_sym(expr, NULL);
928 static struct smatch_state *get_state_chunk(int owner, struct expression *expr)
930 char *name;
931 struct symbol *sym;
932 struct smatch_state *ret;
934 name = chunk_to_var_sym(expr, &sym);
935 if (!name)
936 return NULL;
938 ret = get_state(owner, name, sym);
939 free_string(name);
940 return ret;
943 static void save_link(struct expression *expr, char *link)
945 char *var;
946 struct symbol *sym;
948 expr = strip_expr(expr);
949 if (expr->type == EXPR_BINOP) {
950 char *chunk;
952 chunk = chunk_to_var(expr);
953 if (!chunk)
954 return;
956 save_link(expr->left, link);
957 save_link(expr->right, link);
958 save_link_var_sym(chunk, NULL, link);
959 return;
962 var = chunk_to_var_sym(expr, &sym);
963 if (!var)
964 return;
966 save_link_var_sym(var, sym, link);
967 free_string(var);
970 static int get_orig_comparison(struct stree *pre_stree, const char *left, const char *right)
972 struct smatch_state *state;
973 struct compare_data *data;
974 int flip = 0;
975 char state_name[256];
977 if (strcmp(left, right) > 0) {
978 const char *tmp = right;
980 flip = 1;
981 right = left;
982 left = tmp;
985 snprintf(state_name, sizeof(state_name), "%s vs %s", left, right);
986 state = get_state_stree(pre_stree, comparison_id, state_name, NULL);
987 if (!state || !state->data)
988 return 0;
989 data = state->data;
990 if (flip)
991 return flip_comparison(data->comparison);
992 return data->comparison;
996 static int have_common_var_sym(struct var_sym_list *left_vsl, struct var_sym_list *right_vsl)
998 struct var_sym *tmp;
1000 FOR_EACH_PTR(left_vsl, tmp) {
1001 if (in_var_sym_list(right_vsl, tmp->var, tmp->sym))
1002 return 1;
1003 } END_FOR_EACH_PTR(tmp);
1005 return 0;
1009 * The idea here is that we take a comparison "a < b" and then we look at all
1010 * the things which "b" is compared against "b < c" and we say that that implies
1011 * a relationship "a < c".
1013 * The names here about because the comparisons are organized like this
1014 * "a < b < c".
1017 static void update_tf_links(struct stree *pre_stree,
1018 struct expression *left_expr,
1019 const char *left_var, struct var_sym_list *left_vsl,
1020 int left_comparison, int left_false_comparison,
1021 const char *mid_var, struct var_sym_list *mid_vsl,
1022 struct string_list *links)
1024 struct smatch_state *state;
1025 struct smatch_state *true_state, *false_state;
1026 struct compare_data *data;
1027 struct expression *right_expr;
1028 const char *left_var_orig = left_var;
1029 const char *right_var;
1030 struct var_sym_list *left_vsl_orig = left_vsl;
1031 struct var_sym_list *right_vsl;
1032 int orig_comparison;
1033 int right_comparison;
1034 int true_comparison;
1035 int false_comparison;
1036 char *tmp;
1037 char state_name[256];
1038 struct var_sym *vs;
1040 FOR_EACH_PTR(links, tmp) {
1041 state = get_state_stree(pre_stree, comparison_id, tmp, NULL);
1042 if (!state || !state->data)
1043 continue;
1044 left_var = left_var_orig;
1045 left_vsl = left_vsl_orig;
1046 data = state->data;
1047 right_comparison = data->comparison;
1048 right_expr = data->right;
1049 right_var = data->right_var;
1050 right_vsl = data->right_vsl;
1051 if (strcmp(mid_var, right_var) == 0) {
1052 right_expr = data->left;
1053 right_var = data->left_var;
1054 right_vsl = data->left_vsl;
1055 right_comparison = flip_comparison(right_comparison);
1057 if (have_common_var_sym(left_vsl, right_vsl))
1058 continue;
1060 orig_comparison = get_orig_comparison(pre_stree, left_var, right_var);
1062 true_comparison = combine_comparisons(left_comparison, right_comparison);
1063 false_comparison = combine_comparisons(left_false_comparison, right_comparison);
1065 true_comparison = comparison_intersection(orig_comparison, true_comparison);
1066 false_comparison = comparison_intersection(orig_comparison, false_comparison);
1068 if (strcmp(left_var, right_var) > 0) {
1069 struct expression *tmp_expr = left_expr;
1070 const char *tmp_var = left_var;
1071 struct var_sym_list *tmp_vsl = left_vsl;
1073 left_expr = right_expr;
1074 left_var = right_var;
1075 left_vsl = right_vsl;
1076 right_expr = tmp_expr;
1077 right_var = tmp_var;
1078 right_vsl = tmp_vsl;
1079 true_comparison = flip_comparison(true_comparison);
1080 false_comparison = flip_comparison(false_comparison);
1083 if (!true_comparison && !false_comparison)
1084 continue;
1086 if (true_comparison)
1087 true_state = alloc_compare_state(
1088 left_expr, left_var, left_vsl,
1089 true_comparison,
1090 right_expr, right_var, right_vsl);
1091 else
1092 true_state = NULL;
1093 if (false_comparison)
1094 false_state = alloc_compare_state(
1095 left_expr, left_var, left_vsl,
1096 false_comparison,
1097 right_expr, right_var, right_vsl);
1098 else
1099 false_state = NULL;
1101 snprintf(state_name, sizeof(state_name), "%s vs %s", left_var, right_var);
1102 set_true_false_states(comparison_id, state_name, NULL, true_state, false_state);
1103 FOR_EACH_PTR(left_vsl, vs) {
1104 save_link_var_sym(vs->var, vs->sym, state_name);
1105 } END_FOR_EACH_PTR(vs);
1106 FOR_EACH_PTR(right_vsl, vs) {
1107 save_link_var_sym(vs->var, vs->sym, state_name);
1108 } END_FOR_EACH_PTR(vs);
1109 if (!vsl_to_sym(left_vsl))
1110 save_link_var_sym(left_var, NULL, state_name);
1111 if (!vsl_to_sym(right_vsl))
1112 save_link_var_sym(right_var, NULL, state_name);
1113 } END_FOR_EACH_PTR(tmp);
1116 static void update_tf_data(struct stree *pre_stree,
1117 struct expression *left_expr,
1118 const char *left_name, struct var_sym_list *left_vsl,
1119 struct expression *right_expr,
1120 const char *right_name, struct var_sym_list *right_vsl,
1121 int true_comparison, int false_comparison)
1123 struct smatch_state *state;
1125 state = get_state_stree(pre_stree, link_id, right_name, vsl_to_sym(right_vsl));
1126 if (state)
1127 update_tf_links(pre_stree, left_expr, left_name, left_vsl, true_comparison, false_comparison, right_name, right_vsl, state->data);
1129 state = get_state_stree(pre_stree, link_id, left_name, vsl_to_sym(left_vsl));
1130 if (state)
1131 update_tf_links(pre_stree, right_expr, right_name, right_vsl, flip_comparison(true_comparison), flip_comparison(false_comparison), left_name, left_vsl, state->data);
1134 static void iter_modify(struct sm_state *sm, struct expression *mod_expr)
1136 if (sm->state != &start ||
1137 !mod_expr ||
1138 (mod_expr->type != EXPR_PREOP && mod_expr->type != EXPR_POSTOP) ||
1139 mod_expr->op != SPECIAL_INCREMENT)
1140 set_state(inc_dec_id, sm->name, sm->sym, &undefined);
1141 else
1142 set_state(inc_dec_id, sm->name, sm->sym, &incremented);
1145 static void handle_for_loops(struct expression *expr, char *state_name, struct smatch_state *false_state)
1147 sval_t sval;
1148 char *iter_name, *cap_name;
1149 struct symbol *iter_sym, *cap_sym;
1150 struct compare_data *data;
1152 if (expr->op != '<' && expr->op != SPECIAL_UNSIGNED_LT)
1153 return;
1155 if (!__cur_stmt || !__prev_stmt)
1156 return;
1157 if (__cur_stmt->type != STMT_ITERATOR)
1158 return;
1159 if (__cur_stmt->iterator_pre_condition != expr)
1160 return;
1162 /* literals are handled in smatch_extra.c */
1163 if (get_value(expr->right, &sval))
1164 return;
1166 /* First time checking the condition */
1167 if (__prev_stmt == __cur_stmt->iterator_pre_statement) {
1168 if (!get_implied_value(expr->left, &sval) ||
1169 sval.value != 0)
1170 return;
1172 iter_name = expr_to_var_sym(expr->left, &iter_sym);
1173 cap_name = expr_to_var_sym(expr->right, &cap_sym);
1174 if (!iter_name || !cap_name || !iter_sym || !cap_sym) {
1175 free_string(iter_name);
1176 free_string(cap_name);
1177 return;
1180 set_state(inc_dec_id, iter_name, iter_sym, &start);
1181 store_link(inc_dec_link_id, cap_name, cap_sym, iter_name, iter_sym);
1183 free_string(iter_name);
1184 free_string(cap_name);
1185 return;
1188 /* Second time checking the condtion */
1189 if (__prev_stmt != __cur_stmt->iterator_post_statement)
1190 return;
1192 if (get_state_chunk(inc_dec_id, expr->left) != &incremented)
1193 return;
1195 data = false_state->data;
1196 false_state = alloc_compare_state(
1197 data->left, data->left_var, data->left_vsl,
1198 SPECIAL_EQUAL,
1199 data->right, data->right_var, data->right_vsl);
1201 // FIXME: This doesn't handle links correct so it doesn't set "param orig"
1202 set_true_false_states(comparison_id, state_name, NULL, NULL, false_state);
1205 static int is_plus_one(struct expression *expr)
1207 sval_t sval;
1209 if (expr->type != EXPR_BINOP || expr->op != '+')
1210 return 0;
1211 if (!get_implied_value(expr->right, &sval) || sval.value != 1)
1212 return 0;
1213 return 1;
1216 static int is_minus_one(struct expression *expr)
1218 sval_t sval;
1220 if (expr->type != EXPR_BINOP || expr->op != '-')
1221 return 0;
1222 if (!get_implied_value(expr->right, &sval) || sval.value != 1)
1223 return 0;
1224 return 1;
1227 static void remove_plus_minus_zero(struct expression **expr_p)
1229 struct expression *expr = *expr_p;
1230 sval_t sval;
1232 if (!expr || expr->type != EXPR_BINOP)
1233 return;
1234 if (expr->op == '+') {
1235 if (get_implied_value(expr->left, &sval) && sval.value == 0)
1236 *expr_p = expr->right;
1237 else if (get_implied_value(expr->right, &sval) && sval.value == 0)
1238 *expr_p = expr->left;
1239 else
1240 return;
1241 return remove_plus_minus_zero(expr_p);
1243 if (expr->op == '-') {
1244 if (get_implied_value(expr->right, &sval) && sval.value == 0) {
1245 *expr_p = expr->left;
1246 return remove_plus_minus_zero(expr_p);
1251 static void move_plus_to_minus_helper(struct expression **left_p, struct expression **right_p)
1253 struct expression *left = *left_p;
1254 struct expression *right = *right_p;
1257 * These two are basically equivalent: "foo + 1 != bar" and
1258 * "foo != bar - 1". There are issues with signedness and integer
1259 * overflows. There are also issues with type as well. But let's
1260 * pretend we can ignore all that stuff for now.
1264 if (!is_plus_one(left))
1265 return;
1267 *left_p = left->left;
1268 *right_p = binop_expression(right, '-', left->right);
1271 static void move_plus_to_minus(struct expression **left_p, struct expression **right_p)
1273 if (is_plus_one(*left_p) && is_plus_one(*right_p))
1274 return;
1276 move_plus_to_minus_helper(left_p, right_p);
1277 move_plus_to_minus_helper(right_p, left_p);
1280 static void simplify_binops(struct expression **left_p, struct expression **right_p)
1282 remove_plus_minus_zero(left_p);
1283 remove_plus_minus_zero(right_p);
1285 move_plus_to_minus(left_p, right_p);
1288 static void handle_comparison(struct expression *left_expr, int op, struct expression *right_expr, char **_state_name, struct smatch_state **_false_state)
1290 char *left = NULL;
1291 char *right = NULL;
1292 struct symbol *left_sym, *right_sym;
1293 struct var_sym_list *left_vsl = NULL;
1294 struct var_sym_list *right_vsl = NULL;
1295 int false_op;
1296 int orig_comparison;
1297 struct smatch_state *true_state, *false_state;
1298 static char state_name[256];
1299 struct stree *pre_stree;
1300 sval_t sval;
1302 if (!left_expr || !right_expr)
1303 return;
1305 left_expr = strip_parens(left_expr);
1306 right_expr = strip_parens(right_expr);
1308 while (left_expr->type == EXPR_ASSIGNMENT)
1309 left_expr = strip_parens(left_expr->left);
1310 while (right_expr->type == EXPR_ASSIGNMENT)
1311 right_expr = strip_parens(right_expr->left);
1313 false_op = negate_comparison(op);
1315 simplify_binops(&left_expr, &right_expr);
1317 if (op == SPECIAL_UNSIGNED_LT &&
1318 get_implied_value(left_expr, &sval) &&
1319 sval.value == 0)
1320 false_op = SPECIAL_EQUAL;
1322 if (op == SPECIAL_UNSIGNED_GT &&
1323 get_implied_value(right_expr, &sval) &&
1324 sval.value == 0)
1325 false_op = SPECIAL_EQUAL;
1327 left = chunk_to_var_sym(left_expr, &left_sym);
1328 if (!left)
1329 goto free;
1330 if (left_sym)
1331 add_var_sym(&left_vsl, left, left_sym);
1332 else
1333 left_vsl = expr_to_vsl(left_expr);
1334 right = chunk_to_var_sym(right_expr, &right_sym);
1335 if (!right)
1336 goto free;
1337 if (right_sym)
1338 add_var_sym(&right_vsl, right, right_sym);
1339 else
1340 right_vsl = expr_to_vsl(right_expr);
1342 if (strcmp(left, right) > 0) {
1343 char *tmp_name = left;
1344 struct var_sym_list *tmp_vsl = left_vsl;
1345 struct expression *tmp_expr = left_expr;
1347 left = right;
1348 left_vsl = right_vsl;
1349 left_expr = right_expr;
1350 right = tmp_name;
1351 right_vsl = tmp_vsl;
1352 right_expr = tmp_expr;
1353 op = flip_comparison(op);
1354 false_op = flip_comparison(false_op);
1357 orig_comparison = get_comparison(left_expr, right_expr);
1358 op = comparison_intersection(orig_comparison, op);
1359 false_op = comparison_intersection(orig_comparison, false_op);
1361 snprintf(state_name, sizeof(state_name), "%s vs %s", left, right);
1362 true_state = alloc_compare_state(
1363 left_expr, left, left_vsl,
1365 right_expr, right, right_vsl);
1366 false_state = alloc_compare_state(
1367 left_expr, left, left_vsl,
1368 false_op,
1369 right_expr, right, right_vsl);
1371 pre_stree = clone_stree(__get_cur_stree());
1372 update_tf_data(pre_stree, left_expr, left, left_vsl, right_expr, right, right_vsl, op, false_op);
1373 free_stree(&pre_stree);
1375 if (op == IMPOSSIBLE_COMPARISON &&
1376 false_op != IMPOSSIBLE_COMPARISON &&
1377 false_op != UNKNOWN_COMPARISON)
1378 set_true_path_impossible();
1379 if (op != IMPOSSIBLE_COMPARISON &&
1380 op != UNKNOWN_COMPARISON &&
1381 false_op == IMPOSSIBLE_COMPARISON)
1382 set_false_path_impossible();
1384 set_true_false_states(comparison_id, state_name, NULL, true_state, false_state);
1385 __compare_param_limit_hook(left_expr, right_expr, state_name, true_state, false_state);
1386 save_link(left_expr, state_name);
1387 save_link(right_expr, state_name);
1389 if (_false_state)
1390 *_false_state = false_state;
1391 if (_state_name)
1392 *_state_name = state_name;
1393 free:
1394 free_string(left);
1395 free_string(right);
1398 void __comparison_match_condition(struct expression *expr)
1400 struct expression *left, *right, *new_left, *new_right, *tmp;
1401 struct smatch_state *false_state = NULL;
1402 char *state_name = NULL;
1403 int redo, count;
1405 if (expr->type != EXPR_COMPARE)
1406 return;
1408 handle_comparison(expr->left, expr->op, expr->right, &state_name, &false_state);
1409 if (false_state && state_name)
1410 handle_for_loops(expr, state_name, false_state);
1412 left = strip_parens(expr->left);
1413 right = strip_parens(expr->right);
1415 if (left->type == EXPR_BINOP && left->op == '+') {
1416 new_left = left->left;
1417 new_right = binop_expression(right, '-', left->right);
1418 handle_comparison(new_left, expr->op, new_right, NULL, NULL);
1420 new_left = left->right;
1421 new_right = binop_expression(right, '-', left->left);
1422 handle_comparison(new_left, expr->op, new_right, NULL, NULL);
1425 if ((expr->op == SPECIAL_EQUAL || expr->op == SPECIAL_NOTEQUAL) &&
1426 expr_is_zero(expr->right) &&
1427 left->type == EXPR_BINOP && left->op == '-') {
1428 new_left = left->left;
1429 new_right = left->right;
1430 handle_comparison(new_left, expr->op, new_right, NULL, NULL);
1433 redo = 0;
1434 left = strip_parens(expr->left);
1435 right = strip_parens(expr->right);
1436 if (get_last_expr_from_expression_stmt(expr->left)) {
1437 left = get_last_expr_from_expression_stmt(expr->left);
1438 redo = 1;
1440 if (get_last_expr_from_expression_stmt(expr->right)) {
1441 right = get_last_expr_from_expression_stmt(expr->right);
1442 redo = 1;
1445 if (!redo)
1446 return;
1448 count = 0;
1449 while ((tmp = get_assigned_expr(left))) {
1450 if (count++ > 3)
1451 break;
1452 left = strip_expr(tmp);
1454 count = 0;
1455 while ((tmp = get_assigned_expr(right))) {
1456 if (count++ > 3)
1457 break;
1458 right = strip_expr(tmp);
1461 handle_comparison(left, expr->op, right, NULL, NULL);
1464 void add_comparison_var_sym(
1465 struct expression *left_expr,
1466 const char *left_name, struct var_sym_list *left_vsl,
1467 int comparison,
1468 struct expression *right_expr,
1469 const char *right_name, struct var_sym_list *right_vsl,
1470 struct expression *mod_expr)
1472 struct smatch_state *state;
1473 struct var_sym *vs;
1474 char state_name[256];
1476 ignore_mod_expr = mod_expr;
1478 if (strcmp(left_name, right_name) > 0) {
1479 struct expression *tmp_expr = left_expr;
1480 const char *tmp_name = left_name;
1481 struct var_sym_list *tmp_vsl = left_vsl;
1483 left_expr = right_expr;
1484 left_name = right_name;
1485 left_vsl = right_vsl;
1486 right_expr = tmp_expr;
1487 right_name = tmp_name;
1488 right_vsl = tmp_vsl;
1489 comparison = flip_comparison(comparison);
1491 snprintf(state_name, sizeof(state_name), "%s vs %s", left_name, right_name);
1492 state = alloc_compare_state(
1493 left_expr, left_name, left_vsl,
1494 comparison,
1495 right_expr, right_name, right_vsl);
1497 set_state(comparison_id, state_name, NULL, state);
1499 FOR_EACH_PTR(left_vsl, vs) {
1500 save_link_var_sym(vs->var, vs->sym, state_name);
1501 } END_FOR_EACH_PTR(vs);
1502 FOR_EACH_PTR(right_vsl, vs) {
1503 save_link_var_sym(vs->var, vs->sym, state_name);
1504 } END_FOR_EACH_PTR(vs);
1507 static void add_comparison(struct expression *left, int comparison, struct expression *right)
1509 char *left_name = NULL;
1510 char *right_name = NULL;
1511 struct symbol *left_sym, *right_sym;
1512 struct var_sym_list *left_vsl, *right_vsl;
1513 struct smatch_state *state;
1514 char state_name[256];
1516 left_name = chunk_to_var_sym(left, &left_sym);
1517 if (!left_name)
1518 goto free;
1519 left_vsl = expr_to_vsl(left);
1520 right_name = chunk_to_var_sym(right, &right_sym);
1521 if (!right_name)
1522 goto free;
1523 right_vsl = expr_to_vsl(right);
1525 if (strcmp(left_name, right_name) > 0) {
1526 struct expression *tmp_expr = left;
1527 struct symbol *tmp_sym = left_sym;
1528 char *tmp_name = left_name;
1529 struct var_sym_list *tmp_vsl = left_vsl;
1531 left = right;
1532 left_name = right_name;
1533 left_sym = right_sym;
1534 left_vsl = right_vsl;
1535 right = tmp_expr;
1536 right_name = tmp_name;
1537 right_sym = tmp_sym;
1538 right_vsl = tmp_vsl;
1539 comparison = flip_comparison(comparison);
1541 snprintf(state_name, sizeof(state_name), "%s vs %s", left_name, right_name);
1542 state = alloc_compare_state(
1543 left, left_name, left_vsl,
1544 comparison,
1545 right, right_name, right_vsl);
1547 set_state(comparison_id, state_name, NULL, state);
1548 save_link(left, state_name);
1549 save_link(right, state_name);
1551 free:
1552 free_string(left_name);
1553 free_string(right_name);
1556 static void match_assign_add(struct expression *expr)
1558 struct expression *right;
1559 struct expression *r_left, *r_right;
1560 sval_t left_tmp, right_tmp;
1562 right = strip_expr(expr->right);
1563 r_left = strip_expr(right->left);
1564 r_right = strip_expr(right->right);
1566 get_absolute_min(r_left, &left_tmp);
1567 get_absolute_min(r_right, &right_tmp);
1569 if (left_tmp.value > 0)
1570 add_comparison(expr->left, '>', r_right);
1571 else if (left_tmp.value == 0)
1572 add_comparison(expr->left, SPECIAL_GTE, r_right);
1574 if (right_tmp.value > 0)
1575 add_comparison(expr->left, '>', r_left);
1576 else if (right_tmp.value == 0)
1577 add_comparison(expr->left, SPECIAL_GTE, r_left);
1580 static void match_assign_sub(struct expression *expr)
1582 struct expression *right;
1583 struct expression *r_left, *r_right;
1584 int comparison;
1585 sval_t min;
1587 right = strip_expr(expr->right);
1588 r_left = strip_expr(right->left);
1589 r_right = strip_expr(right->right);
1591 if (get_absolute_min(r_right, &min) && sval_is_negative(min))
1592 return;
1594 comparison = get_comparison(r_left, r_right);
1596 switch (comparison) {
1597 case '>':
1598 case SPECIAL_GTE:
1599 if (implied_not_equal(r_right, 0))
1600 add_comparison(expr->left, '>', r_left);
1601 else
1602 add_comparison(expr->left, SPECIAL_GTE, r_left);
1603 return;
1607 static void match_assign_divide(struct expression *expr)
1609 struct expression *right;
1610 struct expression *r_left, *r_right;
1611 sval_t min;
1613 right = strip_expr(expr->right);
1614 r_left = strip_expr(right->left);
1615 r_right = strip_expr(right->right);
1616 if (!get_implied_min(r_right, &min) || min.value <= 1)
1617 return;
1619 add_comparison(expr->left, '<', r_left);
1622 static void match_binop_assign(struct expression *expr)
1624 struct expression *right;
1626 right = strip_expr(expr->right);
1627 if (right->op == '+')
1628 match_assign_add(expr);
1629 if (right->op == '-')
1630 match_assign_sub(expr);
1631 if (right->op == '/')
1632 match_assign_divide(expr);
1635 static void copy_comparisons(struct expression *left, struct expression *right)
1637 struct string_list *links;
1638 struct smatch_state *state;
1639 struct compare_data *data;
1640 struct symbol *left_sym, *right_sym;
1641 char *left_var = NULL;
1642 char *right_var = NULL;
1643 struct var_sym_list *left_vsl;
1644 struct expression *expr;
1645 const char *var;
1646 struct var_sym_list *vsl;
1647 int comparison;
1648 char *tmp;
1651 * Say we have y < foo and we assign x = y; then that means x < foo.
1652 * Internally, smatch has links from "y" and "foo" to the comparison
1653 * "foo vs y". There could also be a comparison with an array:
1654 * "x[idx] vs limit", so all three variables, "x", "idx" and "limit"
1655 * have a link.
1657 * Also another thing which can happen is that you have an assignment
1658 * "x = y" and there is a comparison: "x vs y". And clearly we don't
1659 * want to store "x < x".
1661 * So what this function does is it takes each link "foo vs y" and it
1662 * swaps in "foo vs x". Except it moves the "x" to the left so it's
1663 * "x vs foo" (or whatever) and the add_comparison_var_sym() will flip
1664 * it into the correct order.
1668 left_var = chunk_to_var_sym(left, &left_sym);
1669 if (!left_var)
1670 goto done;
1671 left_vsl = expr_to_vsl(left);
1672 right_var = chunk_to_var_sym(right, &right_sym);
1673 if (!right_var)
1674 goto done;
1676 state = get_state(link_id, right_var, right_sym);
1677 if (!state)
1678 return;
1679 links = state->data;
1681 FOR_EACH_PTR(links, tmp) {
1682 state = get_state(comparison_id, tmp, NULL);
1683 if (!state || !state->data)
1684 continue;
1685 data = state->data;
1686 /* if there isn't a comparison then skip it. (can this happen?) */
1687 if (!data->comparison)
1688 continue;
1690 /* right_var is from "left = right_var;". "var" is the variable
1691 * bit from "right_var < var".
1693 if (strcmp(right_var, data->left_var) == 0) {
1694 expr = data->right;
1695 var = data->right_var;
1696 vsl = data->right_vsl;
1697 comparison = data->comparison;
1698 } else if (strcmp(right_var, data->right_var) == 0) {
1699 expr = data->left;
1700 var = data->left_var;
1701 vsl = data->left_vsl;
1702 comparison = flip_comparison(data->comparison);
1703 } else {
1704 /* This means the right side is only part of the
1705 * comparison. As in "x = i;" when "foo[i] < bar".
1707 continue;
1710 /* n = copy_from_user(dest, src, n); leads to n <= n which is nonsense */
1711 if (strcmp(left_var, var) == 0)
1712 continue;
1714 add_comparison_var_sym(left, left_var, left_vsl, comparison, expr, var, vsl, NULL);
1715 } END_FOR_EACH_PTR(tmp);
1717 done:
1718 free_string(right_var);
1721 static bool is_empty_fake_assign(struct expression *expr)
1723 if (!is_fake_var_assign(expr))
1724 return false;
1725 if (get_state_chunk(link_id, expr->right))
1726 return false;
1727 return true;
1730 static void match_assign(struct expression *expr)
1732 struct expression *right;
1734 if (expr->op != '=')
1735 return;
1736 if (__in_fake_assign || outside_of_function())
1737 return;
1739 if (is_struct(expr->left))
1740 return;
1742 if (is_self_assign(expr))
1743 return;
1745 if (is_empty_fake_assign(expr))
1746 return;
1748 copy_comparisons(expr->left, expr->right);
1749 add_comparison(expr->left, SPECIAL_EQUAL, expr->right);
1751 right = strip_expr(expr->right);
1752 if (right->type == EXPR_BINOP)
1753 match_binop_assign(expr);
1756 int get_comparison_strings(const char *one, const char *two)
1758 char buf[256];
1759 struct smatch_state *state;
1760 int invert = 0;
1761 int ret = 0;
1763 if (!one || !two)
1764 return UNKNOWN_COMPARISON;
1766 if (strcmp(one, two) == 0)
1767 return SPECIAL_EQUAL;
1769 if (strcmp(one, two) > 0) {
1770 const char *tmp = one;
1772 one = two;
1773 two = tmp;
1774 invert = 1;
1777 snprintf(buf, sizeof(buf), "%s vs %s", one, two);
1778 state = get_state(comparison_id, buf, NULL);
1779 if (state)
1780 ret = state_to_comparison(state);
1782 if (invert)
1783 ret = flip_comparison(ret);
1785 return ret;
1788 static int get_comparison_helper(struct expression *a, struct expression *b, bool use_extra)
1790 char *one = NULL;
1791 char *two = NULL;
1792 int ret = UNKNOWN_COMPARISON;
1793 int extra = UNKNOWN_COMPARISON;
1795 if (a == UNKNOWN_COMPARISON ||
1796 b == UNKNOWN_COMPARISON)
1797 return UNKNOWN_COMPARISON;
1799 a = strip_parens(a);
1800 b = strip_parens(b);
1802 move_plus_to_minus(&a, &b);
1804 simplify_binops(&a, &b);
1806 one = chunk_to_var(a);
1807 if (!one)
1808 goto free;
1809 two = chunk_to_var(b);
1810 if (!two)
1811 goto free;
1813 ret = get_comparison_strings(one, two);
1814 if (ret)
1815 goto free;
1817 if (is_plus_one(a) || is_minus_one(a)) {
1818 free_string(one);
1819 one = chunk_to_var(a->left);
1820 ret = get_comparison_strings(one, two);
1821 } else if (is_plus_one(b) || is_minus_one(b)) {
1822 free_string(two);
1823 two = chunk_to_var(b->left);
1824 ret = get_comparison_strings(one, two);
1827 if (ret == UNKNOWN_COMPARISON)
1828 goto free;
1830 if ((is_plus_one(a) || is_minus_one(b)) && ret == '<')
1831 ret = SPECIAL_LTE;
1832 else if ((is_minus_one(a) || is_plus_one(b)) && ret == '>')
1833 ret = SPECIAL_GTE;
1834 else
1835 ret = UNKNOWN_COMPARISON;
1837 free:
1838 free_string(one);
1839 free_string(two);
1841 if (use_extra)
1842 extra = comparison_from_extra(a, b);
1843 return comparison_intersection(ret, extra);
1846 int get_comparison(struct expression *a, struct expression *b)
1848 return get_comparison_helper(a, b, true);
1851 int get_comparison_no_extra(struct expression *a, struct expression *b)
1853 return get_comparison_helper(a, b, false);
1856 int possible_comparison(struct expression *a, int comparison, struct expression *b)
1858 char *one = NULL;
1859 char *two = NULL;
1860 int ret = 0;
1861 char buf[256];
1862 struct sm_state *sm;
1863 int saved;
1865 one = chunk_to_var(a);
1866 if (!one)
1867 goto free;
1868 two = chunk_to_var(b);
1869 if (!two)
1870 goto free;
1873 if (strcmp(one, two) == 0 && comparison == SPECIAL_EQUAL) {
1874 ret = 1;
1875 goto free;
1878 if (strcmp(one, two) > 0) {
1879 char *tmp = one;
1881 one = two;
1882 two = tmp;
1883 comparison = flip_comparison(comparison);
1886 snprintf(buf, sizeof(buf), "%s vs %s", one, two);
1887 sm = get_sm_state(comparison_id, buf, NULL);
1888 if (!sm)
1889 goto free;
1891 FOR_EACH_PTR(sm->possible, sm) {
1892 if (!sm->state->data)
1893 continue;
1894 saved = ((struct compare_data *)sm->state->data)->comparison;
1895 if (saved == comparison)
1896 ret = 1;
1897 if (comparison == SPECIAL_EQUAL &&
1898 (saved == SPECIAL_LTE ||
1899 saved == SPECIAL_GTE ||
1900 saved == SPECIAL_UNSIGNED_LTE ||
1901 saved == SPECIAL_UNSIGNED_GTE))
1902 ret = 1;
1903 if (ret == 1)
1904 goto free;
1905 } END_FOR_EACH_PTR(sm);
1907 return ret;
1908 free:
1909 free_string(one);
1910 free_string(two);
1911 return ret;
1914 struct state_list *get_all_comparisons(struct expression *expr)
1916 struct smatch_state *state;
1917 struct string_list *links;
1918 struct state_list *ret = NULL;
1919 struct sm_state *sm;
1920 char *tmp;
1922 state = get_state_chunk(link_id, expr);
1923 if (!state)
1924 return NULL;
1925 links = state->data;
1927 FOR_EACH_PTR(links, tmp) {
1928 sm = get_sm_state(comparison_id, tmp, NULL);
1929 if (!sm)
1930 continue;
1931 // FIXME have to compare name with vsl
1932 add_ptr_list(&ret, sm);
1933 } END_FOR_EACH_PTR(tmp);
1935 return ret;
1938 struct state_list *get_all_possible_equal_comparisons(struct expression *expr)
1940 struct smatch_state *state;
1941 struct string_list *links;
1942 struct state_list *ret = NULL;
1943 struct sm_state *sm;
1944 char *tmp;
1946 state = get_state_chunk(link_id, expr);
1947 if (!state)
1948 return NULL;
1949 links = state->data;
1951 FOR_EACH_PTR(links, tmp) {
1952 sm = get_sm_state(comparison_id, tmp, NULL);
1953 if (!sm)
1954 continue;
1955 if (!strchr(sm->state->name, '='))
1956 continue;
1957 if (strcmp(sm->state->name, "!=") == 0)
1958 continue;
1959 add_ptr_list(&ret, sm);
1960 } END_FOR_EACH_PTR(tmp);
1962 return ret;
1965 struct state_list *get_all_possible_not_equal_comparisons(struct expression *expr)
1967 struct smatch_state *state;
1968 struct string_list *links;
1969 struct state_list *ret = NULL;
1970 struct sm_state *sm;
1971 struct sm_state *possible;
1972 char *link;
1974 return NULL;
1976 state = get_state_chunk(link_id, expr);
1977 if (!state)
1978 return NULL;
1979 links = state->data;
1981 FOR_EACH_PTR(links, link) {
1982 sm = get_sm_state(comparison_id, link, NULL);
1983 if (!sm)
1984 continue;
1985 FOR_EACH_PTR(sm->possible, possible) {
1986 if (strcmp(possible->state->name, "!=") != 0)
1987 continue;
1988 add_ptr_list(&ret, sm);
1989 break;
1990 } END_FOR_EACH_PTR(possible);
1991 } END_FOR_EACH_PTR(link);
1993 return ret;
1996 static void update_links_from_call(struct expression *left,
1997 int left_compare,
1998 struct expression *right)
2000 struct string_list *links;
2001 struct smatch_state *state;
2002 struct compare_data *data;
2003 struct symbol *left_sym, *right_sym;
2004 char *left_var = NULL;
2005 char *right_var = NULL;
2006 struct var_sym_list *left_vsl;
2007 struct expression *expr;
2008 const char *var;
2009 struct var_sym_list *vsl;
2010 int comparison;
2011 char *tmp;
2013 left_var = chunk_to_var_sym(left, &left_sym);
2014 if (!left_var)
2015 goto done;
2016 left_vsl = expr_to_vsl(left);
2017 right_var = chunk_to_var_sym(right, &right_sym);
2018 if (!right_var)
2019 goto done;
2021 state = get_state(link_id, right_var, right_sym);
2022 if (!state)
2023 return;
2024 links = state->data;
2026 FOR_EACH_PTR(links, tmp) {
2027 state = get_state(comparison_id, tmp, NULL);
2028 if (!state || !state->data)
2029 continue;
2030 data = state->data;
2031 comparison = data->comparison;
2032 expr = data->right;
2033 var = data->right_var;
2034 vsl = data->right_vsl;
2035 if (strcmp(var, right_var) == 0) {
2036 expr = data->left;
2037 var = data->left_var;
2038 vsl = data->left_vsl;
2039 comparison = flip_comparison(comparison);
2041 comparison = combine_comparisons(left_compare, comparison);
2042 if (!comparison)
2043 continue;
2044 add_comparison_var_sym(left, left_var, left_vsl, comparison, expr, var, vsl, NULL);
2045 } END_FOR_EACH_PTR(tmp);
2047 done:
2048 free_string(right_var);
2051 static void return_str_comparison(struct expression *expr, const char *range)
2053 struct expression *arg;
2054 int comparison;
2055 char buf[16];
2057 while (expr->type == EXPR_ASSIGNMENT)
2058 expr = strip_expr(expr->right);
2059 if (expr->type != EXPR_CALL)
2060 return;
2061 if (!str_to_comparison_arg(range, expr, &comparison, &arg))
2062 return;
2063 snprintf(buf, sizeof(buf), "%s", show_comparison(comparison));
2064 update_links_from_call(expr, comparison, arg);
2065 add_comparison(expr, comparison, arg);
2068 void __add_comparison_info(struct expression *expr, struct expression *call, const char *range)
2070 copy_comparisons(expr, call);
2073 static char *get_mask_comparison(struct expression *expr, int ignore)
2075 struct expression *tmp, *right;
2076 int count, param;
2077 char buf[256];
2079 /* The return value for "return foo & param;" is <= param */
2081 count = 0;
2082 while ((tmp = get_assigned_expr(expr))) {
2083 expr = strip_expr(tmp);
2084 if (count++ > 4)
2085 break;
2088 if (expr->type != EXPR_BINOP || expr->op != '&')
2089 return NULL;
2091 right = strip_expr(expr->right);
2092 param = get_param_num(right);
2093 if (param < 0 || param == ignore)
2094 return NULL;
2096 snprintf(buf, sizeof(buf), "[<=$%d]", param);
2097 return alloc_sname(buf);
2100 static char *range_comparison_to_param_helper(struct expression *expr, char starts_with, int ignore)
2102 struct symbol *param;
2103 char *var = NULL;
2104 char buf[256];
2105 char *ret_str = NULL;
2106 int compare;
2107 int i;
2109 if (!expr)
2110 return NULL;
2112 var = chunk_to_var(expr);
2113 if (!var)
2114 goto try_mask;
2116 i = -1;
2117 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, param) {
2118 i++;
2119 if (i == ignore)
2120 continue;
2121 if (!param->ident)
2122 continue;
2123 snprintf(buf, sizeof(buf), "%s orig", param->ident->name);
2124 compare = get_comparison_strings(var, buf);
2125 if (compare == UNKNOWN_COMPARISON ||
2126 compare == IMPOSSIBLE_COMPARISON)
2127 continue;
2128 if (show_comparison(compare)[0] != starts_with)
2129 continue;
2130 snprintf(buf, sizeof(buf), "[%s$%d]", show_comparison(compare), i);
2131 ret_str = alloc_sname(buf);
2132 break;
2133 } END_FOR_EACH_PTR(param);
2135 free_string(var);
2136 if (!ret_str)
2137 goto try_mask;
2139 return ret_str;
2141 try_mask:
2142 if (starts_with == '<')
2143 ret_str = get_mask_comparison(expr, ignore);
2144 return ret_str;
2147 char *name_sym_to_param_comparison(const char *name, struct symbol *sym)
2149 struct symbol *param;
2150 char buf[256];
2151 int compare;
2152 int i;
2154 i = -1;
2155 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, param) {
2156 i++;
2157 if (!param->ident)
2158 continue;
2159 snprintf(buf, sizeof(buf), "%s orig", param->ident->name);
2160 compare = get_comparison_strings(name, buf);
2161 if (compare == UNKNOWN_COMPARISON ||
2162 compare == IMPOSSIBLE_COMPARISON)
2163 continue;
2164 snprintf(buf, sizeof(buf), "[%s$%d]", show_comparison(compare), i);
2165 return alloc_sname(buf);
2166 } END_FOR_EACH_PTR(param);
2168 return NULL;
2171 char *expr_equal_to_param(struct expression *expr, int ignore)
2173 return range_comparison_to_param_helper(expr, '=', ignore);
2176 char *expr_lte_to_param(struct expression *expr, int ignore)
2178 return range_comparison_to_param_helper(expr, '<', ignore);
2181 char *expr_param_comparison(struct expression *expr, int ignore)
2183 struct symbol *param;
2184 char *var = NULL;
2185 char buf[256];
2186 char *ret_str = NULL;
2187 int compare;
2188 int i;
2190 var = chunk_to_var(expr);
2191 if (!var)
2192 goto free;
2194 i = -1;
2195 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, param) {
2196 i++;
2197 if (i == ignore)
2198 continue;
2199 if (!param->ident)
2200 continue;
2201 snprintf(buf, sizeof(buf), "%s orig", param->ident->name);
2202 compare = get_comparison_strings(var, buf);
2203 if (!compare)
2204 continue;
2205 snprintf(buf, sizeof(buf), "[%s$%d]", show_comparison(compare), i);
2206 ret_str = alloc_sname(buf);
2207 break;
2208 } END_FOR_EACH_PTR(param);
2210 free:
2211 free_string(var);
2212 return ret_str;
2215 char *get_printed_param_name(struct expression *call, const char *param_name, struct symbol *param_sym)
2217 struct expression *arg;
2218 char *name;
2219 struct symbol *sym;
2220 static char buf[256];
2221 int len;
2222 int i;
2224 i = -1;
2225 FOR_EACH_PTR(call->args, arg) {
2226 i++;
2228 name = expr_to_var_sym(arg, &sym);
2229 if (!name || !sym)
2230 continue;
2231 if (sym != param_sym)
2232 continue;
2234 len = strlen(name);
2235 if (strncmp(name, param_name, len) != 0)
2236 continue;
2237 if (param_name[len] == '\0') {
2238 snprintf(buf, sizeof(buf), "$%d", i);
2239 return buf;
2241 if (param_name[len] != '-')
2242 continue;
2243 snprintf(buf, sizeof(buf), "$%d%s", i, param_name + len);
2244 return buf;
2245 } END_FOR_EACH_PTR(arg);
2247 return NULL;
2250 static void match_call_info(struct expression *expr)
2252 struct expression *arg;
2253 struct smatch_state *state;
2254 struct sm_state *sm;
2255 struct compare_data *data;
2256 int comparison;
2257 struct string_list *links;
2258 char *arg_name;
2259 const char *right_name;
2260 char *link;
2261 char info_buf[256];
2262 int i;
2264 i = -1;
2265 FOR_EACH_PTR(expr->args, arg) {
2266 i++;
2268 state = get_state_chunk(link_id, arg);
2269 if (!state)
2270 continue;
2272 links = state->data;
2273 FOR_EACH_PTR(links, link) {
2274 struct var_sym_list *right_vsl;
2275 struct var_sym *right_vs;
2278 if (strstr(link, " orig"))
2279 continue;
2280 sm = get_sm_state(comparison_id, link, NULL);
2281 if (!sm)
2282 continue;
2283 data = sm->state->data;
2284 if (!data ||
2285 data->comparison == UNKNOWN_COMPARISON ||
2286 data->comparison == IMPOSSIBLE_COMPARISON)
2287 continue;
2288 arg_name = expr_to_var(arg);
2289 if (!arg_name)
2290 continue;
2292 right_vsl = NULL;
2293 if (strcmp(data->left_var, arg_name) == 0) {
2294 comparison = data->comparison;
2295 right_name = data->right_var;
2296 right_vsl = data->right_vsl;
2297 } else if (strcmp(data->right_var, arg_name) == 0) {
2298 comparison = flip_comparison(data->comparison);
2299 right_name = data->left_var;
2300 right_vsl = data->left_vsl;
2302 if (!right_vsl || ptr_list_size((struct ptr_list *)right_vsl) != 1)
2303 goto free;
2305 right_vs = first_ptr_list((struct ptr_list *)right_vsl);
2306 if (strcmp(right_vs->var, right_name) != 0)
2307 goto free;
2308 right_name = get_printed_param_name(expr, right_vs->var, right_vs->sym);
2309 if (!right_name)
2310 goto free;
2311 snprintf(info_buf, sizeof(info_buf), "%s %s", show_comparison(comparison), right_name);
2312 sql_insert_caller_info(expr, PARAM_COMPARE, i, "$", info_buf);
2314 free:
2315 free_string(arg_name);
2316 } END_FOR_EACH_PTR(link);
2317 } END_FOR_EACH_PTR(arg);
2320 static void struct_member_callback(struct expression *call, int param, char *printed_name, struct sm_state *link_sm)
2322 struct sm_state *compare_sm;
2323 struct string_list *links;
2324 char *link;
2325 struct compare_data *data;
2326 struct var_sym *left, *right;
2327 static char info_buf[256];
2328 const char *right_name;
2330 if (strstr(printed_name, " orig"))
2331 return;
2333 links = link_sm->state->data;
2334 FOR_EACH_PTR(links, link) {
2335 compare_sm = get_sm_state(comparison_id, link, NULL);
2336 if (!compare_sm)
2337 continue;
2338 data = compare_sm->state->data;
2339 if (!data || !data->comparison)
2340 continue;
2342 if (ptr_list_size((struct ptr_list *)data->left_vsl) != 1 ||
2343 ptr_list_size((struct ptr_list *)data->right_vsl) != 1)
2344 continue;
2345 left = first_ptr_list((struct ptr_list *)data->left_vsl);
2346 right = first_ptr_list((struct ptr_list *)data->right_vsl);
2347 if (left->sym == right->sym &&
2348 strcmp(left->var, right->var) == 0)
2349 continue;
2351 * Both parameters link to this comparison so only
2352 * record the first one.
2354 if (left->sym != link_sm->sym ||
2355 strcmp(left->var, link_sm->name) != 0)
2356 continue;
2358 right_name = get_printed_param_name(call, right->var, right->sym);
2359 if (!right_name)
2360 continue;
2361 snprintf(info_buf, sizeof(info_buf), "%s %s", show_comparison(data->comparison), right_name);
2362 sql_insert_caller_info(call, PARAM_COMPARE, param, printed_name, info_buf);
2363 } END_FOR_EACH_PTR(link);
2366 static void print_return_value_comparison(int return_id, char *return_ranges, struct expression *expr)
2368 char *name;
2369 const char *tmp_name;
2370 struct symbol *sym;
2371 int param;
2372 char info_buf[256];
2373 bool is_addr = false;
2376 * TODO: This only prints == comparisons. That's probably the most
2377 * useful comparison because == max has lots of implications. But it
2378 * would be good to capture the rest as well.
2380 * This information is already in the DB but it's in the parameter math
2381 * bits and it's awkward to use it. This is is the simpler, possibly
2382 * cleaner way, but not necessarily the best, I don't know.
2385 if (!expr)
2386 return;
2387 name = expr_to_var_sym(expr, &sym);
2388 if (!name || !sym)
2389 goto free;
2391 param = get_param_num_from_sym(sym);
2392 if (param < 0)
2393 goto free;
2394 if (param_was_set_var_sym(name, sym))
2395 goto free;
2397 tmp_name = get_param_name_var_sym(name, sym);
2398 if (!tmp_name)
2399 goto free;
2401 if (tmp_name[0] == '&') {
2402 tmp_name += 2;
2403 is_addr = true;
2404 } else {
2405 tmp_name += 1;
2408 snprintf(info_buf, sizeof(info_buf), "== %s$%d%s", is_addr ? "&" : "", param, tmp_name);
2409 sql_insert_return_states(return_id, return_ranges,
2410 PARAM_COMPARE, -1, "$", info_buf);
2411 free:
2412 free_string(name);
2415 static void print_return_comparison(int return_id, char *return_ranges, struct expression *expr)
2417 struct sm_state *tmp;
2418 struct string_list *links;
2419 char *link;
2420 struct sm_state *sm;
2421 struct compare_data *data;
2422 struct var_sym *left, *right;
2423 int left_param, right_param;
2424 const char *left_key, *right_key;
2425 char info_buf[258];
2427 print_return_value_comparison(return_id, return_ranges, expr);
2429 FOR_EACH_MY_SM(link_id, __get_cur_stree(), tmp) {
2430 left_param = get_param_key_from_var_sym(tmp->name, tmp->sym, expr, &left_key);
2431 if (left_param < -1 || !left_key)
2432 continue;
2434 links = tmp->state->data;
2435 FOR_EACH_PTR(links, link) {
2436 if (strncmp(link, "$size", 5) == 0)
2437 continue;
2438 sm = get_sm_state(comparison_id, link, NULL);
2439 if (!sm)
2440 continue;
2441 data = sm->state->data;
2442 if (!data ||
2443 data->comparison == UNKNOWN_COMPARISON ||
2444 data->comparison == IMPOSSIBLE_COMPARISON)
2445 continue;
2446 if (ptr_list_size((struct ptr_list *)data->left_vsl) != 1 ||
2447 ptr_list_size((struct ptr_list *)data->right_vsl) != 1)
2448 continue;
2449 left = first_ptr_list((struct ptr_list *)data->left_vsl);
2450 right = first_ptr_list((struct ptr_list *)data->right_vsl);
2451 if (left->sym == right->sym &&
2452 strcmp(left->var, right->var) == 0)
2453 continue;
2455 * The both foo and bar in the "foo == bar" comparison
2456 * have link states. To avoid duplicates only parse
2457 * the comparison if the link state is on the left.
2459 * And actually let's move getting the left_name outside
2460 * the loop.
2463 if (left->sym != tmp->sym ||
2464 strcmp(left->var, tmp->name) != 0)
2465 continue;
2467 if (strstr(right->var, " orig"))
2468 continue;
2470 right_param = get_param_key_from_var_sym(right->var, right->sym, expr, &right_key);
2471 if (right_param < 0 || !right_key || right_key[0] != '$')
2472 continue;
2475 * The same param can have multiple names so we can get
2476 * duplicates.
2478 if (left_param == right_param &&
2479 strcmp(left_key, right_key) == 0)
2480 continue;
2482 snprintf(info_buf, sizeof(info_buf), "%s $%d%s",
2483 show_comparison(data->comparison), right_param, right_key + 1);
2484 sql_insert_return_states(return_id, return_ranges,
2485 PARAM_COMPARE, left_param, left_key, info_buf);
2486 } END_FOR_EACH_PTR(link);
2488 } END_FOR_EACH_SM(tmp);
2491 static int parse_comparison(char **value, int *op)
2494 *op = **value;
2496 switch (*op) {
2497 case '<':
2498 (*value)++;
2499 if (**value == '=') {
2500 (*value)++;
2501 *op = SPECIAL_LTE;
2503 break;
2504 case '=':
2505 (*value)++;
2506 (*value)++;
2507 *op = SPECIAL_EQUAL;
2508 break;
2509 case '!':
2510 (*value)++;
2511 (*value)++;
2512 *op = SPECIAL_NOTEQUAL;
2513 break;
2514 case '>':
2515 (*value)++;
2516 if (**value == '=') {
2517 (*value)++;
2518 *op = SPECIAL_GTE;
2520 break;
2521 default:
2522 return 0;
2525 if (**value != ' ') {
2526 sm_perror("parsing comparison. %s", *value);
2527 return 0;
2530 (*value)++;
2531 return 1;
2534 static int split_op_param_key(char *value, int *op, int *param, char **key)
2536 static char buf[256];
2537 char *p;
2539 if (!parse_comparison(&value, op))
2540 return 0;
2542 snprintf(buf, sizeof(buf), "%s", value);
2544 p = buf;
2545 if (*p++ != '$')
2546 return 0;
2548 *param = atoi(p);
2549 if (*param < 0 || *param > 99)
2550 return 0;
2551 p++;
2552 if (*param > 9)
2553 p++;
2554 p--;
2555 *p = '$';
2556 *key = p;
2558 return 1;
2561 void select_caller_info(const char *name, struct symbol *sym, char *value)
2563 struct var_sym_list *left_vsl = NULL;
2564 struct var_sym_list *right_vsl = NULL;
2565 char *right_key, *p, *right_name;
2566 struct symbol *right_sym;
2567 char comparison_name[128];
2568 char right_buf[128];
2569 int op, right_param;
2571 if (!split_op_param_key(value, &op, &right_param, &right_key))
2572 return;
2574 right_sym = get_param_sym_from_num(right_param);
2575 if (!right_sym || !right_sym->ident)
2576 return;
2578 p = strchr(right_key, '$');
2579 if (!p)
2580 return;
2582 snprintf(right_buf, sizeof(right_buf), "%.*s%s%s", (int)(p - right_key),
2583 right_key, right_sym->ident->name, p + 1);
2584 right_name = right_buf;
2586 if (strcmp(name, right_name) > 0) {
2587 struct symbol *tmp_sym = sym;
2588 char *tmp_name = (char *)name;
2590 name = right_name;
2591 sym = right_sym;
2592 right_name = tmp_name;
2593 right_sym = tmp_sym;
2594 op = flip_comparison(op);
2597 add_var_sym(&left_vsl, name, sym);
2598 add_var_sym(&right_vsl, right_name, right_sym);
2600 snprintf(comparison_name, sizeof(comparison_name), "%s vs %s", name, right_name);
2602 set_state(comparison_id, comparison_name, NULL,
2603 alloc_compare_state(NULL, name, left_vsl,
2605 NULL, right_name, right_vsl));
2608 static void db_return_comparison(struct expression *expr, int left_param, char *key, char *value)
2610 struct expression *left_arg, *right_arg;
2611 char *left_name = NULL;
2612 struct symbol *left_sym;
2613 char *right_name = NULL;
2614 struct symbol *right_sym;
2615 int op;
2616 int right_param;
2617 char *right_key;
2618 struct var_sym_list *left_vsl = NULL, *right_vsl = NULL;
2620 if (left_param == -1) {
2621 if (expr->type != EXPR_ASSIGNMENT)
2622 return;
2623 left_arg = strip_expr(expr->left);
2625 while (expr->type == EXPR_ASSIGNMENT)
2626 expr = strip_expr(expr->right);
2627 if (expr->type != EXPR_CALL)
2628 return;
2629 } else {
2630 while (expr->type == EXPR_ASSIGNMENT)
2631 expr = strip_expr(expr->right);
2632 if (expr->type != EXPR_CALL)
2633 return;
2635 left_arg = get_argument_from_call_expr(expr->args, left_param);
2636 if (!left_arg)
2637 return;
2640 if (!split_op_param_key(value, &op, &right_param, &right_key))
2641 return;
2643 right_arg = get_argument_from_call_expr(expr->args, right_param);
2644 if (!right_arg)
2645 return;
2647 left_name = get_variable_from_key(left_arg, key, &left_sym);
2648 if (!left_name || !left_sym)
2649 goto free;
2651 right_name = get_variable_from_key(right_arg, right_key, &right_sym);
2652 if (!right_name || !right_sym)
2653 goto free;
2655 add_var_sym(&left_vsl, left_name, left_sym);
2656 add_var_sym(&right_vsl, right_name, right_sym);
2658 add_comparison_var_sym(NULL, left_name, left_vsl, op, NULL, right_name, right_vsl, NULL);
2660 free:
2661 free_string(left_name);
2662 free_string(right_name);
2665 int param_compare_limit_is_impossible(struct expression *expr, int left_param, char *left_key, char *value)
2667 char *left_name = NULL;
2668 char *right_name = NULL;
2669 struct symbol *left_sym, *right_sym;
2670 struct expression *left_arg, *right_arg;
2671 int op, state_op;
2672 int right_param;
2673 char *right_key;
2674 int ret = 0;
2676 while (expr->type == EXPR_ASSIGNMENT)
2677 expr = strip_expr(expr->right);
2678 if (expr->type != EXPR_CALL)
2679 return 0;
2681 if (!split_op_param_key(value, &op, &right_param, &right_key))
2682 return 0;
2684 left_arg = get_argument_from_call_expr(expr->args, left_param);
2685 if (!left_arg)
2686 return 0;
2688 right_arg = get_argument_from_call_expr(expr->args, right_param);
2689 if (!right_arg)
2690 return 0;
2692 left_name = get_variable_from_key(left_arg, left_key, &left_sym);
2693 right_name = get_variable_from_key(right_arg, right_key, &right_sym);
2694 if (!left_name || !right_name)
2695 goto free;
2697 state_op = get_comparison_strings(left_name, right_name);
2698 if (!state_op)
2699 goto free;
2701 if (comparison_intersection(remove_unsigned_from_comparison(state_op), op)
2702 == IMPOSSIBLE_COMPARISON)
2703 ret = 1;
2704 free:
2705 free_string(left_name);
2706 free_string(right_name);
2707 return ret;
2710 int impossibly_high_comparison(struct expression *expr)
2712 struct smatch_state *link_state;
2713 struct sm_state *sm;
2714 struct string_list *links;
2715 char *link;
2716 struct compare_data *data;
2718 link_state = get_state_expr(link_id, expr);
2719 if (!link_state) {
2720 if (expr->type == EXPR_BINOP &&
2721 (impossibly_high_comparison(expr->left) ||
2722 impossibly_high_comparison(expr->right)))
2723 return 1;
2724 return 0;
2727 links = link_state->data;
2728 FOR_EACH_PTR(links, link) {
2729 sm = get_sm_state(comparison_id, link, NULL);
2730 if (!sm)
2731 continue;
2732 data = sm->state->data;
2733 if (!data)
2734 continue;
2735 if (!possibly_true(data->left, data->comparison, data->right))
2736 return 1;
2737 } END_FOR_EACH_PTR(link);
2739 return 0;
2742 static void free_data(struct symbol *sym)
2744 if (__inline_fn)
2745 return;
2746 clear_compare_data_alloc();
2749 void register_comparison(int id)
2751 comparison_id = id;
2752 set_dynamic_states(comparison_id);
2753 add_hook(&save_start_states, AFTER_DEF_HOOK);
2754 add_unmatched_state_hook(comparison_id, unmatched_comparison);
2755 add_pre_merge_hook(comparison_id, &pre_merge_hook);
2756 add_merge_hook(comparison_id, &merge_compare_states);
2757 add_hook(&free_data, AFTER_FUNC_HOOK);
2759 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
2760 select_caller_name_sym(&select_caller_info, PARAM_COMPARE);
2762 add_split_return_callback(&print_return_comparison);
2763 select_return_states_hook(PARAM_COMPARE, &db_return_comparison);
2765 add_hook(&match_preop, OP_HOOK);
2768 void register_comparison_late(int id)
2770 add_hook(&match_assign, ASSIGNMENT_HOOK);
2771 add_return_string_hook(return_str_comparison);
2774 void register_comparison_links(int id)
2776 link_id = id;
2777 db_ignore_states(link_id);
2778 set_dynamic_states(link_id);
2779 add_merge_hook(link_id, &merge_links);
2780 add_modification_hook(link_id, &match_modify);
2781 add_modification_hook_late(link_id, match_inc_dec);
2783 add_member_info_callback(link_id, struct_member_callback);
2786 void register_comparison_inc_dec(int id)
2788 inc_dec_id = id;
2789 add_modification_hook_late(inc_dec_id, &iter_modify);
2792 void register_comparison_inc_dec_links(int id)
2794 inc_dec_link_id = id;
2795 set_dynamic_states(inc_dec_link_id);
2796 set_up_link_functions(inc_dec_id, inc_dec_link_id);
2799 static struct sm_state *clone_partial_sm(struct sm_state *sm, int comparison)
2801 struct compare_data *data;
2802 struct sm_state *clone;
2803 struct stree *stree;
2805 data = sm->state->data;
2807 clone = clone_sm(sm);
2808 clone->state = alloc_compare_state(data->left, data->left_var, data->left_vsl,
2809 comparison,
2810 data->right, data->right_var, data->right_vsl);
2811 free_slist(&clone->possible);
2812 add_possible_sm(clone, clone);
2814 stree = clone_stree(sm->pool);
2815 overwrite_sm_state_stree(&stree, clone);
2816 clone->pool = stree;
2818 return clone;
2821 static void create_fake_history(struct sm_state *sm, int op,
2822 struct state_list **true_stack,
2823 struct state_list **false_stack)
2825 struct sm_state *true_sm, *false_sm;
2826 struct compare_data *data;
2827 int true_comparison;
2828 int false_comparison;
2830 data = sm->state->data;
2832 if (is_merged(sm) || sm->left || sm->right)
2833 return;
2835 true_comparison = comparison_intersection(data->comparison, op);
2836 false_comparison = comparison_intersection(data->comparison, negate_comparison(op));
2838 true_sm = clone_partial_sm(sm, true_comparison);
2839 false_sm = clone_partial_sm(sm, false_comparison);
2841 sm->merged = 1;
2842 sm->left = true_sm;
2843 sm->right = false_sm;
2845 add_ptr_list(true_stack, true_sm);
2846 add_ptr_list(false_stack, false_sm);
2849 static void filter_by_sm(struct sm_state *sm, int op,
2850 struct state_list **true_stack,
2851 struct state_list **false_stack,
2852 bool *useful)
2854 struct compare_data *data;
2855 int is_true = 0;
2856 int is_false = 0;
2858 if (!sm)
2859 return;
2860 data = sm->state->data;
2861 if (!data)
2862 goto split;
2863 if (data->comparison == IMPOSSIBLE_COMPARISON)
2864 return;
2867 * We want to check that "data->comparison" is totally inside "op". So
2868 * if data->comparison is < and op is <= then that's true. Or if
2869 * data->comparison is == and op is <= then that's true. But if
2870 * data->comparison is <= and op is < than that's neither true nor
2871 * false.
2873 if (data->comparison == comparison_intersection(data->comparison, op))
2874 is_true = 1;
2875 if (data->comparison == comparison_intersection(data->comparison, negate_comparison(op)))
2876 is_false = 1;
2878 if (!is_true && !is_false && !is_merged(sm)) {
2879 create_fake_history(sm, op, true_stack, false_stack);
2880 return;
2883 if (debug_implied()) {
2884 sm_msg("%s: %s: op = '%s' negated '%s'. true_intersect = '%s' false_insersect = '%s' sm = '%s'",
2885 __func__,
2886 sm->state->name,
2887 alloc_sname(show_comparison(op)),
2888 alloc_sname(show_comparison(negate_comparison(op))),
2889 alloc_sname(show_comparison(comparison_intersection(data->comparison, op))),
2890 alloc_sname(show_comparison(comparison_intersection(data->comparison, negate_comparison(op)))),
2891 show_sm(sm));
2894 *useful = true;
2895 if (is_true)
2896 add_ptr_list(true_stack, sm);
2897 if (is_false)
2898 add_ptr_list(false_stack, sm);
2899 split:
2900 filter_by_sm(sm->left, op, true_stack, false_stack, useful);
2901 filter_by_sm(sm->right, op, true_stack, false_stack, useful);
2904 struct sm_state *comparison_implication_hook(struct expression *expr,
2905 struct state_list **true_stack,
2906 struct state_list **false_stack)
2908 struct sm_state *sm;
2909 char *left, *right;
2910 int op;
2911 static char buf[256];
2912 bool useful = false;
2914 if (expr->type != EXPR_COMPARE)
2915 return NULL;
2917 op = expr->op;
2919 left = expr_to_var(expr->left);
2920 right = expr_to_var(expr->right);
2921 if (!left || !right) {
2922 free_string(left);
2923 free_string(right);
2924 return NULL;
2927 if (strcmp(left, right) > 0) {
2928 char *tmp = left;
2930 left = right;
2931 right = tmp;
2932 op = flip_comparison(op);
2935 snprintf(buf, sizeof(buf), "%s vs %s", left, right);
2936 sm = get_sm_state(comparison_id, buf, NULL);
2937 if (!sm)
2938 return NULL;
2939 if (!sm->merged)
2940 return NULL;
2942 filter_by_sm(sm, op, true_stack, false_stack, &useful);
2943 if (!useful)
2944 return NULL;
2946 if (debug_implied())
2947 sm_msg("implications from comparison: (%s)", show_sm(sm));
2949 return sm;