data_source: rename a variable "tmp" => "arg"
[smatch.git] / smatch_comparison.c
blob70410d3f47169f6b066ee760eeb0ec30aa12bf16
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 return expr_to_chunk_sym_vsl(expr, sym, NULL);
712 static char *chunk_to_var(struct expression *expr)
714 return chunk_to_var_sym(expr, NULL);
717 static void save_link(struct expression *expr, char *link)
719 char *var;
720 struct symbol *sym;
722 expr = strip_expr(expr);
723 if (expr->type == EXPR_BINOP) {
724 char *chunk;
726 chunk = chunk_to_var(expr);
727 if (!chunk)
728 return;
730 save_link(expr->left, link);
731 save_link(expr->right, link);
732 save_link_var_sym(chunk, NULL, link);
733 return;
736 var = expr_to_var_sym(expr, &sym);
737 if (!var || !sym)
738 goto done;
740 save_link_var_sym(var, sym, link);
741 done:
742 free_string(var);
745 static int get_orig_comparison(struct stree *pre_stree, const char *left, const char *right)
747 struct smatch_state *state;
748 struct compare_data *data;
749 int flip = 0;
750 char state_name[256];
752 if (strcmp(left, right) > 0) {
753 const char *tmp = right;
755 flip = 1;
756 right = left;
757 left = tmp;
760 snprintf(state_name, sizeof(state_name), "%s vs %s", left, right);
761 state = get_state_stree(pre_stree, compare_id, state_name, NULL);
762 if (!state || !state->data)
763 return 0;
764 data = state->data;
765 if (flip)
766 return flip_comparison(data->comparison);
767 return data->comparison;
771 static int have_common_var_sym(struct var_sym_list *left_vsl, struct var_sym_list *right_vsl)
773 struct var_sym *tmp;
775 FOR_EACH_PTR(left_vsl, tmp) {
776 if (in_var_sym_list(right_vsl, tmp->var, tmp->sym))
777 return 1;
778 } END_FOR_EACH_PTR(tmp);
780 return 0;
784 * The idea here is that we take a comparison "a < b" and then we look at all
785 * the things which "b" is compared against "b < c" and we say that that implies
786 * a relationship "a < c".
788 * The names here about because the comparisons are organized like this
789 * "a < b < c".
792 static void update_tf_links(struct stree *pre_stree,
793 const char *left_var, struct var_sym_list *left_vsl,
794 int left_comparison, int left_false_comparison,
795 const char *mid_var, struct var_sym_list *mid_vsl,
796 struct string_list *links)
798 struct smatch_state *state;
799 struct smatch_state *true_state, *false_state;
800 struct compare_data *data;
801 const char *right_var;
802 struct var_sym_list *right_vsl;
803 int orig_comparison;
804 int right_comparison;
805 int true_comparison;
806 int false_comparison;
807 char *tmp;
808 char state_name[256];
809 struct var_sym *vs;
811 FOR_EACH_PTR(links, tmp) {
812 state = get_state_stree(pre_stree, compare_id, tmp, NULL);
813 if (!state || !state->data)
814 continue;
815 data = state->data;
816 right_comparison = data->comparison;
817 right_var = data->var2;
818 right_vsl = data->vsl2;
819 if (strcmp(mid_var, right_var) == 0) {
820 right_var = data->var1;
821 right_vsl = data->vsl1;
822 right_comparison = flip_comparison(right_comparison);
824 if (have_common_var_sym(left_vsl, right_vsl))
825 continue;
827 orig_comparison = get_orig_comparison(pre_stree, left_var, right_var);
829 true_comparison = combine_comparisons(left_comparison, right_comparison);
830 false_comparison = combine_comparisons(left_false_comparison, right_comparison);
832 true_comparison = filter_comparison(orig_comparison, true_comparison);
833 false_comparison = filter_comparison(orig_comparison, false_comparison);
835 if (strcmp(left_var, right_var) > 0) {
836 const char *tmp_var = left_var;
837 struct var_sym_list *tmp_vsl = left_vsl;
839 left_var = right_var;
840 left_vsl = right_vsl;
841 right_var = tmp_var;
842 right_vsl = tmp_vsl;
843 true_comparison = flip_comparison(true_comparison);
844 false_comparison = flip_comparison(false_comparison);
847 if (!true_comparison && !false_comparison)
848 continue;
850 if (true_comparison)
851 true_state = alloc_compare_state(left_var, left_vsl, true_comparison, right_var, right_vsl);
852 else
853 true_state = NULL;
854 if (false_comparison)
855 false_state = alloc_compare_state(left_var, left_vsl, false_comparison, right_var, right_vsl);
856 else
857 false_state = NULL;
859 snprintf(state_name, sizeof(state_name), "%s vs %s", left_var, right_var);
860 set_true_false_states(compare_id, state_name, NULL, true_state, false_state);
861 FOR_EACH_PTR(left_vsl, vs) {
862 save_link_var_sym(vs->var, vs->sym, state_name);
863 } END_FOR_EACH_PTR(vs);
864 FOR_EACH_PTR(right_vsl, vs) {
865 save_link_var_sym(vs->var, vs->sym, state_name);
866 } END_FOR_EACH_PTR(vs);
867 if (!vsl_to_sym(left_vsl))
868 save_link_var_sym(left_var, NULL, state_name);
869 if (!vsl_to_sym(right_vsl))
870 save_link_var_sym(right_var, NULL, state_name);
871 } END_FOR_EACH_PTR(tmp);
874 static void update_tf_data(struct stree *pre_stree,
875 const char *left_name, struct var_sym_list *left_vsl,
876 const char *right_name, struct var_sym_list *right_vsl,
877 int true_comparison, int false_comparison)
879 struct smatch_state *state;
881 state = get_state_stree(pre_stree, link_id, right_name, vsl_to_sym(right_vsl));
882 if (state)
883 update_tf_links(pre_stree, left_name, left_vsl, true_comparison, false_comparison, right_name, right_vsl, state->data);
885 state = get_state_stree(pre_stree, link_id, left_name, vsl_to_sym(left_vsl));
886 if (state)
887 update_tf_links(pre_stree, right_name, right_vsl, flip_comparison(true_comparison), flip_comparison(false_comparison), left_name, left_vsl, state->data);
890 static void iter_modify(struct sm_state *sm, struct expression *mod_expr)
892 if (sm->state != &start ||
893 !mod_expr ||
894 (mod_expr->type != EXPR_PREOP && mod_expr->type != EXPR_POSTOP) ||
895 mod_expr->op != SPECIAL_INCREMENT)
896 set_state(inc_dec_id, sm->name, sm->sym, &undefined);
897 else
898 set_state(inc_dec_id, sm->name, sm->sym, &incremented);
901 static void handle_for_loops(struct expression *expr, char *state_name, struct smatch_state *false_state)
903 sval_t sval;
904 char *iter_name, *cap_name;
905 struct symbol *iter_sym, *cap_sym;
906 struct compare_data *data;
908 if (expr->op != '<' && expr->op != SPECIAL_UNSIGNED_LT)
909 return;
911 if (!__cur_stmt || !__prev_stmt)
912 return;
913 if (__cur_stmt->type != STMT_ITERATOR)
914 return;
915 if (__cur_stmt->iterator_pre_condition != expr)
916 return;
918 /* literals are handled in smatch_extra.c */
919 if (get_value(expr->right, &sval))
920 return;
922 /* First time checking the condition */
923 if (__prev_stmt == __cur_stmt->iterator_pre_statement) {
924 if (!get_implied_value(expr->left, &sval) ||
925 sval.value != 0)
926 return;
928 iter_name = expr_to_var_sym(expr->left, &iter_sym);
929 cap_name = expr_to_var_sym(expr->right, &cap_sym);
930 if (!iter_name || !cap_name || !iter_sym || !cap_sym) {
931 free_string(iter_name);
932 free_string(cap_name);
933 return;
936 set_state(inc_dec_id, iter_name, iter_sym, &start);
937 store_link(inc_dec_link_id, cap_name, cap_sym, iter_name, iter_sym);
939 free_string(iter_name);
940 free_string(cap_name);
941 return;
944 /* Second time checking the condtion */
945 if (__prev_stmt != __cur_stmt->iterator_post_statement)
946 return;
948 if (get_state_expr(inc_dec_id, expr->left) != &incremented)
949 return;
951 data = false_state->data;
952 false_state = alloc_compare_state(data->var1, data->vsl1, SPECIAL_EQUAL, data->var2, data->vsl2);
954 set_true_false_states(compare_id, state_name, NULL, NULL, false_state);
957 static void handle_comparison(struct expression *left_expr, int op, struct expression *right_expr, char **_state_name, struct smatch_state **_false_state)
959 char *left = NULL;
960 char *right = NULL;
961 struct symbol *left_sym, *right_sym;
962 struct var_sym_list *left_vsl, *right_vsl;
963 int false_op;
964 int orig_comparison;
965 struct smatch_state *true_state, *false_state;
966 static char state_name[256];
967 struct stree *pre_stree;
968 sval_t sval;
970 false_op = negate_comparison(op);
972 if (op == SPECIAL_UNSIGNED_LT &&
973 get_implied_value(left_expr, &sval) &&
974 sval.value == 0)
975 false_op = SPECIAL_EQUAL;
977 if (op == SPECIAL_UNSIGNED_GT &&
978 get_implied_value(right_expr, &sval) &&
979 sval.value == 0)
980 false_op = SPECIAL_EQUAL;
982 left = chunk_to_var_sym(left_expr, &left_sym);
983 if (!left)
984 goto free;
985 left_vsl = expr_to_vsl(left_expr);
986 right = chunk_to_var_sym(right_expr, &right_sym);
987 if (!right)
988 goto free;
989 right_vsl = expr_to_vsl(right_expr);
991 if (strcmp(left, right) > 0) {
992 struct symbol *tmp_sym = left_sym;
993 char *tmp_name = left;
994 struct var_sym_list *tmp_vsl = left_vsl;
995 struct expression *tmp_expr = left_expr;
997 left = right;
998 left_sym = right_sym;
999 left_vsl = right_vsl;
1000 left_expr = right_expr;
1001 right = tmp_name;
1002 right_sym = tmp_sym;
1003 right_vsl = tmp_vsl;
1004 right_expr = tmp_expr;
1005 op = flip_comparison(op);
1006 false_op = flip_comparison(false_op);
1009 orig_comparison = get_comparison_strings(left, right);
1010 op = filter_comparison(orig_comparison, op);
1011 false_op = filter_comparison(orig_comparison, false_op);
1013 snprintf(state_name, sizeof(state_name), "%s vs %s", left, right);
1014 true_state = alloc_compare_state(left, left_vsl, op, right, right_vsl);
1015 false_state = alloc_compare_state(left, left_vsl, false_op, right, right_vsl);
1017 pre_stree = clone_stree(__get_cur_stree());
1018 update_tf_data(pre_stree, left, left_vsl, right, right_vsl, op, false_op);
1019 free_stree(&pre_stree);
1021 set_true_false_states(compare_id, state_name, NULL, true_state, false_state);
1022 __compare_param_limit_hook(left_expr, right_expr, state_name, true_state, false_state);
1023 save_link(left_expr, state_name);
1024 save_link(right_expr, state_name);
1026 if (_false_state)
1027 *_false_state = false_state;
1028 if (_state_name)
1029 *_state_name = state_name;
1030 free:
1031 free_string(left);
1032 free_string(right);
1036 void __comparison_match_condition(struct expression *expr)
1038 struct expression *left, *right, *new_left, *new_right;
1039 struct smatch_state *false_state = NULL;
1040 char *state_name = NULL;
1042 if (expr->type != EXPR_COMPARE)
1043 return;
1045 handle_comparison(expr->left, expr->op, expr->right, &state_name, &false_state);
1046 if (false_state && state_name)
1047 handle_for_loops(expr, state_name, false_state);
1050 left = strip_parens(expr->left);
1051 right = strip_parens(expr->right);
1053 if (left->type == EXPR_BINOP && left->op == '+') {
1054 new_left = left->left;
1055 new_right = binop_expression(right, '-', left->right);
1056 handle_comparison(new_left, expr->op, new_right, NULL, NULL);
1058 new_left = left->right;
1059 new_right = binop_expression(right, '-', left->left);
1060 handle_comparison(new_left, expr->op, new_right, NULL, NULL);
1064 static void add_comparison_var_sym(const char *left_name,
1065 struct var_sym_list *left_vsl,
1066 int comparison,
1067 const char *right_name, struct var_sym_list *right_vsl)
1069 struct smatch_state *state;
1070 struct var_sym *vs;
1071 char state_name[256];
1073 if (strcmp(left_name, right_name) > 0) {
1074 const char *tmp_name = left_name;
1075 struct var_sym_list *tmp_vsl = left_vsl;
1077 left_name = right_name;
1078 left_vsl = right_vsl;
1079 right_name = tmp_name;
1080 right_vsl = tmp_vsl;
1081 comparison = flip_comparison(comparison);
1083 snprintf(state_name, sizeof(state_name), "%s vs %s", left_name, right_name);
1084 state = alloc_compare_state(left_name, left_vsl, comparison, right_name, right_vsl);
1086 set_state(compare_id, state_name, NULL, state);
1088 FOR_EACH_PTR(left_vsl, vs) {
1089 save_link_var_sym(vs->var, vs->sym, state_name);
1090 } END_FOR_EACH_PTR(vs);
1091 FOR_EACH_PTR(right_vsl, vs) {
1092 save_link_var_sym(vs->var, vs->sym, state_name);
1093 } END_FOR_EACH_PTR(vs);
1096 static void add_comparison(struct expression *left, int comparison, struct expression *right)
1098 char *left_name = NULL;
1099 char *right_name = NULL;
1100 struct symbol *left_sym, *right_sym;
1101 struct var_sym_list *left_vsl, *right_vsl;
1102 struct smatch_state *state;
1103 char state_name[256];
1105 left_name = chunk_to_var_sym(left, &left_sym);
1106 if (!left_name)
1107 goto free;
1108 left_vsl = expr_to_vsl(left);
1109 right_name = chunk_to_var_sym(right, &right_sym);
1110 if (!right_name)
1111 goto free;
1112 right_vsl = expr_to_vsl(right);
1114 if (strcmp(left_name, right_name) > 0) {
1115 struct symbol *tmp_sym = left_sym;
1116 char *tmp_name = left_name;
1117 struct var_sym_list *tmp_vsl = left_vsl;
1119 left_name = right_name;
1120 left_sym = right_sym;
1121 left_vsl = right_vsl;
1122 right_name = tmp_name;
1123 right_sym = tmp_sym;
1124 right_vsl = tmp_vsl;
1125 comparison = flip_comparison(comparison);
1127 snprintf(state_name, sizeof(state_name), "%s vs %s", left_name, right_name);
1128 state = alloc_compare_state(left_name, left_vsl, comparison, right_name, right_vsl);
1130 set_state(compare_id, state_name, NULL, state);
1131 save_link(left, state_name);
1132 save_link(right, state_name);
1134 free:
1135 free_string(left_name);
1136 free_string(right_name);
1139 static void match_assign_add(struct expression *expr)
1141 struct expression *right;
1142 struct expression *r_left, *r_right;
1143 sval_t left_tmp, right_tmp;
1145 right = strip_expr(expr->right);
1146 r_left = strip_expr(right->left);
1147 r_right = strip_expr(right->right);
1149 get_absolute_min(r_left, &left_tmp);
1150 get_absolute_min(r_right, &right_tmp);
1152 if (left_tmp.value > 0)
1153 add_comparison(expr->left, '>', r_right);
1154 else if (left_tmp.value == 0)
1155 add_comparison(expr->left, SPECIAL_GTE, r_right);
1157 if (right_tmp.value > 0)
1158 add_comparison(expr->left, '>', r_left);
1159 else if (right_tmp.value == 0)
1160 add_comparison(expr->left, SPECIAL_GTE, r_left);
1163 static void match_assign_sub(struct expression *expr)
1165 struct expression *right;
1166 struct expression *r_left, *r_right;
1167 int comparison;
1168 sval_t min;
1170 right = strip_expr(expr->right);
1171 r_left = strip_expr(right->left);
1172 r_right = strip_expr(right->right);
1174 if (get_absolute_min(r_right, &min) && sval_is_negative(min))
1175 return;
1177 comparison = get_comparison(r_left, r_right);
1179 switch (comparison) {
1180 case '>':
1181 case SPECIAL_GTE:
1182 if (implied_not_equal(r_right, 0))
1183 add_comparison(expr->left, '>', r_left);
1184 else
1185 add_comparison(expr->left, SPECIAL_GTE, r_left);
1186 return;
1190 static void match_assign_divide(struct expression *expr)
1192 struct expression *right;
1193 struct expression *r_left, *r_right;
1194 sval_t min;
1196 right = strip_expr(expr->right);
1197 r_left = strip_expr(right->left);
1198 r_right = strip_expr(right->right);
1199 if (!get_implied_min(r_right, &min) || min.value <= 1)
1200 return;
1202 add_comparison(expr->left, '<', r_left);
1205 static void match_binop_assign(struct expression *expr)
1207 struct expression *right;
1209 right = strip_expr(expr->right);
1210 if (right->op == '+')
1211 match_assign_add(expr);
1212 if (right->op == '-')
1213 match_assign_sub(expr);
1214 if (right->op == '/')
1215 match_assign_divide(expr);
1218 static void copy_comparisons(struct expression *left, struct expression *right)
1220 struct string_list *links;
1221 struct smatch_state *state;
1222 struct compare_data *data;
1223 struct symbol *left_sym, *right_sym;
1224 char *left_var = NULL;
1225 char *right_var = NULL;
1226 struct var_sym_list *left_vsl;
1227 const char *var;
1228 struct var_sym_list *vsl;
1229 int comparison;
1230 char *tmp;
1232 left_var = chunk_to_var_sym(left, &left_sym);
1233 if (!left_var)
1234 goto done;
1235 left_vsl = expr_to_vsl(left);
1236 right_var = chunk_to_var_sym(right, &right_sym);
1237 if (!right_var)
1238 goto done;
1240 state = get_state(link_id, right_var, right_sym);
1241 if (!state)
1242 return;
1243 links = state->data;
1245 FOR_EACH_PTR(links, tmp) {
1246 state = get_state(compare_id, tmp, NULL);
1247 if (!state || !state->data)
1248 continue;
1249 data = state->data;
1250 comparison = data->comparison;
1251 var = data->var2;
1252 vsl = data->vsl2;
1253 if (strcmp(var, right_var) == 0) {
1254 var = data->var1;
1255 vsl = data->vsl1;
1256 comparison = flip_comparison(comparison);
1258 add_comparison_var_sym(left_var, left_vsl, comparison, var, vsl);
1259 } END_FOR_EACH_PTR(tmp);
1261 done:
1262 free_string(right_var);
1265 static void match_assign(struct expression *expr)
1267 struct expression *right;
1269 if (expr->op != '=')
1270 return;
1271 if (__in_fake_assign || outside_of_function())
1272 return;
1274 if (is_struct(expr->left))
1275 return;
1277 copy_comparisons(expr->left, expr->right);
1278 add_comparison(expr->left, SPECIAL_EQUAL, expr->right);
1280 right = strip_expr(expr->right);
1281 if (right->type == EXPR_BINOP)
1282 match_binop_assign(expr);
1285 int get_comparison_strings(const char *one, const char *two)
1287 char buf[256];
1288 struct smatch_state *state;
1289 int invert = 0;
1290 int ret = 0;
1292 if (strcmp(one, two) == 0)
1293 return SPECIAL_EQUAL;
1295 if (strcmp(one, two) > 0) {
1296 const char *tmp = one;
1298 one = two;
1299 two = tmp;
1300 invert = 1;
1303 snprintf(buf, sizeof(buf), "%s vs %s", one, two);
1304 state = get_state(compare_id, buf, NULL);
1305 if (state)
1306 ret = state_to_comparison(state);
1308 if (invert)
1309 ret = flip_comparison(ret);
1311 return ret;
1314 int get_comparison(struct expression *a, struct expression *b)
1316 char *one = NULL;
1317 char *two = NULL;
1318 int ret = 0;
1320 one = chunk_to_var(a);
1321 if (!one)
1322 goto free;
1323 two = chunk_to_var(b);
1324 if (!two)
1325 goto free;
1327 ret = get_comparison_strings(one, two);
1328 free:
1329 free_string(one);
1330 free_string(two);
1331 return ret;
1334 int possible_comparison(struct expression *a, int comparison, struct expression *b)
1336 char *one = NULL;
1337 char *two = NULL;
1338 int ret = 0;
1339 char buf[256];
1340 struct sm_state *sm;
1341 int saved;
1343 one = chunk_to_var(a);
1344 if (!one)
1345 goto free;
1346 two = chunk_to_var(b);
1347 if (!two)
1348 goto free;
1351 if (strcmp(one, two) == 0 && comparison == SPECIAL_EQUAL) {
1352 ret = 1;
1353 goto free;
1356 if (strcmp(one, two) > 0) {
1357 char *tmp = one;
1359 one = two;
1360 two = tmp;
1361 comparison = flip_comparison(comparison);
1364 snprintf(buf, sizeof(buf), "%s vs %s", one, two);
1365 sm = get_sm_state(compare_id, buf, NULL);
1366 if (!sm)
1367 goto free;
1369 FOR_EACH_PTR(sm->possible, sm) {
1370 if (!sm->state->data)
1371 continue;
1372 saved = ((struct compare_data *)sm->state->data)->comparison;
1373 if (saved == comparison)
1374 ret = 1;
1375 if (comparison == SPECIAL_EQUAL &&
1376 (saved == SPECIAL_LTE ||
1377 saved == SPECIAL_GTE ||
1378 saved == SPECIAL_UNSIGNED_LTE ||
1379 saved == SPECIAL_UNSIGNED_GTE))
1380 ret = 1;
1381 if (ret == 1)
1382 goto free;
1383 } END_FOR_EACH_PTR(sm);
1385 return ret;
1386 free:
1387 free_string(one);
1388 free_string(two);
1389 return ret;
1392 struct state_list *get_all_comparisons(struct expression *expr)
1394 struct smatch_state *state;
1395 struct string_list *links;
1396 struct state_list *ret = NULL;
1397 struct sm_state *sm;
1398 char *tmp;
1400 state = get_state_expr(link_id, expr);
1401 if (!state)
1402 return NULL;
1403 links = state->data;
1405 FOR_EACH_PTR(links, tmp) {
1406 sm = get_sm_state(compare_id, tmp, NULL);
1407 if (!sm)
1408 continue;
1409 // FIXME have to compare name with vsl
1410 add_ptr_list(&ret, sm);
1411 } END_FOR_EACH_PTR(tmp);
1413 return ret;
1416 struct state_list *get_all_possible_equal_comparisons(struct expression *expr)
1418 struct smatch_state *state;
1419 struct string_list *links;
1420 struct state_list *ret = NULL;
1421 struct sm_state *sm;
1422 char *tmp;
1424 state = get_state_expr(link_id, expr);
1425 if (!state)
1426 return NULL;
1427 links = state->data;
1429 FOR_EACH_PTR(links, tmp) {
1430 sm = get_sm_state(compare_id, tmp, NULL);
1431 if (!sm)
1432 continue;
1433 if (!strchr(sm->state->name, '='))
1434 continue;
1435 if (strcmp(sm->state->name, "!=") == 0)
1436 continue;
1437 add_ptr_list(&ret, sm);
1438 } END_FOR_EACH_PTR(tmp);
1440 return ret;
1443 static void update_links_from_call(struct expression *left,
1444 int left_compare,
1445 struct expression *right)
1447 struct string_list *links;
1448 struct smatch_state *state;
1449 struct compare_data *data;
1450 struct symbol *left_sym, *right_sym;
1451 char *left_var = NULL;
1452 char *right_var = NULL;
1453 struct var_sym_list *left_vsl;
1454 const char *var;
1455 struct var_sym_list *vsl;
1456 int comparison;
1457 char *tmp;
1459 left_var = chunk_to_var_sym(left, &left_sym);
1460 if (!left_var)
1461 goto done;
1462 left_vsl = expr_to_vsl(left);
1463 right_var = chunk_to_var_sym(right, &right_sym);
1464 if (!right_var)
1465 goto done;
1467 state = get_state(link_id, right_var, right_sym);
1468 if (!state)
1469 return;
1470 links = state->data;
1472 FOR_EACH_PTR(links, tmp) {
1473 state = get_state(compare_id, tmp, NULL);
1474 if (!state || !state->data)
1475 continue;
1476 data = state->data;
1477 comparison = data->comparison;
1478 var = data->var2;
1479 vsl = data->vsl2;
1480 if (strcmp(var, right_var) == 0) {
1481 var = data->var1;
1482 vsl = data->vsl1;
1483 comparison = flip_comparison(comparison);
1485 comparison = combine_comparisons(left_compare, comparison);
1486 if (!comparison)
1487 continue;
1488 add_comparison_var_sym(left_var, left_vsl, comparison, var, vsl);
1489 } END_FOR_EACH_PTR(tmp);
1491 done:
1492 free_string(right_var);
1495 void __add_comparison_info(struct expression *expr, struct expression *call, const char *range)
1497 struct expression *arg;
1498 int comparison;
1499 const char *c = range;
1501 if (!str_to_comparison_arg(c, call, &comparison, &arg))
1502 return;
1503 update_links_from_call(expr, comparison, arg);
1504 add_comparison(expr, comparison, arg);
1507 static char *range_comparison_to_param_helper(struct expression *expr, char starts_with, int ignore)
1509 struct symbol *param;
1510 char *var = NULL;
1511 char buf[256];
1512 char *ret_str = NULL;
1513 int compare;
1514 int i;
1516 var = chunk_to_var(expr);
1517 if (!var)
1518 goto free;
1520 i = -1;
1521 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, param) {
1522 i++;
1523 if (i == ignore)
1524 continue;
1525 if (!param->ident)
1526 continue;
1527 snprintf(buf, sizeof(buf), "%s orig", param->ident->name);
1528 compare = get_comparison_strings(var, buf);
1529 if (!compare)
1530 continue;
1531 if (show_special(compare)[0] != starts_with)
1532 continue;
1533 snprintf(buf, sizeof(buf), "[%s$%d]", show_special(compare), i);
1534 ret_str = alloc_sname(buf);
1535 break;
1536 } END_FOR_EACH_PTR(param);
1538 free:
1539 free_string(var);
1540 return ret_str;
1543 char *name_sym_to_param_comparison(const char *name, struct symbol *sym)
1545 struct symbol *param;
1546 char buf[256];
1547 int compare;
1548 int i;
1550 i = -1;
1551 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, param) {
1552 i++;
1553 if (!param->ident)
1554 continue;
1555 snprintf(buf, sizeof(buf), "%s orig", param->ident->name);
1556 compare = get_comparison_strings(name, buf);
1557 if (!compare)
1558 continue;
1559 snprintf(buf, sizeof(buf), "[%s$%d]", show_special(compare), i);
1560 return alloc_sname(buf);
1561 } END_FOR_EACH_PTR(param);
1563 return NULL;
1566 char *expr_equal_to_param(struct expression *expr, int ignore)
1568 return range_comparison_to_param_helper(expr, '=', ignore);
1571 char *expr_lte_to_param(struct expression *expr, int ignore)
1573 return range_comparison_to_param_helper(expr, '<', ignore);
1576 char *expr_param_comparison(struct expression *expr, int ignore)
1578 struct symbol *param;
1579 char *var = NULL;
1580 char buf[256];
1581 char *ret_str = NULL;
1582 int compare;
1583 int i;
1585 var = chunk_to_var(expr);
1586 if (!var)
1587 goto free;
1589 i = -1;
1590 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, param) {
1591 i++;
1592 if (i == ignore)
1593 continue;
1594 if (!param->ident)
1595 continue;
1596 snprintf(buf, sizeof(buf), "%s orig", param->ident->name);
1597 compare = get_comparison_strings(var, buf);
1598 if (!compare)
1599 continue;
1600 snprintf(buf, sizeof(buf), "[%s$%d]", show_special(compare), i);
1601 ret_str = alloc_sname(buf);
1602 break;
1603 } END_FOR_EACH_PTR(param);
1605 free:
1606 free_string(var);
1607 return ret_str;
1610 char *get_printed_param_name(struct expression *call, const char *param_name, struct symbol *param_sym)
1612 struct expression *arg;
1613 char *name;
1614 struct symbol *sym;
1615 static char buf[256];
1616 int len;
1617 int i;
1619 i = -1;
1620 FOR_EACH_PTR(call->args, arg) {
1621 i++;
1623 name = expr_to_var_sym(arg, &sym);
1624 if (!name || !sym)
1625 continue;
1626 if (sym != param_sym)
1627 continue;
1629 len = strlen(name);
1630 if (strncmp(name, param_name, len) != 0)
1631 continue;
1632 if (param_name[len] == '\0') {
1633 snprintf(buf, sizeof(buf), "$%d", i);
1634 return buf;
1636 if (param_name[len] != '-')
1637 continue;
1638 snprintf(buf, sizeof(buf), "$%d%s", i, param_name + len);
1639 return buf;
1640 } END_FOR_EACH_PTR(arg);
1642 return NULL;
1645 static void match_call_info(struct expression *expr)
1647 struct expression *arg;
1648 struct smatch_state *state;
1649 struct sm_state *sm;
1650 struct compare_data *data;
1651 int comparison;
1652 struct string_list *links;
1653 char *arg_name;
1654 const char *right_name;
1655 char *link;
1656 char info_buf[256];
1657 int i;
1659 i = -1;
1660 FOR_EACH_PTR(expr->args, arg) {
1661 i++;
1663 state = get_state_expr(link_id, arg);
1664 if (!state)
1665 continue;
1667 links = state->data;
1668 FOR_EACH_PTR(links, link) {
1669 struct var_sym_list *right_vsl;
1670 struct var_sym *right_vs;
1673 if (strstr(link, " orig"))
1674 continue;
1675 sm = get_sm_state(compare_id, link, NULL);
1676 if (!sm)
1677 continue;
1678 data = sm->state->data;
1679 if (!data || !data->comparison)
1680 continue;
1681 arg_name = expr_to_var(arg);
1682 if (!arg_name)
1683 continue;
1685 right_vsl = NULL;
1686 if (strcmp(data->var1, arg_name) == 0) {
1687 comparison = data->comparison;
1688 right_name = data->var2;
1689 right_vsl = data->vsl2;
1690 } else if (strcmp(data->var2, arg_name) == 0) {
1691 comparison = flip_comparison(data->comparison);
1692 right_name = data->var1;
1693 right_vsl = data->vsl1;
1695 if (!right_vsl || ptr_list_size((struct ptr_list *)right_vsl) != 1)
1696 goto free;
1698 right_vs = first_ptr_list((struct ptr_list *)right_vsl);
1699 if (strcmp(right_vs->var, right_name) != 0)
1700 goto free;
1701 right_name = get_printed_param_name(expr, right_vs->var, right_vs->sym);
1702 if (!right_name)
1703 goto free;
1704 snprintf(info_buf, sizeof(info_buf), "%s %s", show_special(comparison), right_name);
1705 sql_insert_caller_info(expr, PARAM_COMPARE, i, "$", info_buf);
1707 free:
1708 free_string(arg_name);
1709 } END_FOR_EACH_PTR(link);
1710 } END_FOR_EACH_PTR(arg);
1713 static void struct_member_callback(struct expression *call, int param, char *printed_name, struct sm_state *link_sm)
1715 struct sm_state *compare_sm;
1716 struct string_list *links;
1717 char *link;
1718 struct compare_data *data;
1719 struct var_sym *left, *right;
1720 static char info_buf[256];
1721 const char *right_name;
1723 if (strstr(printed_name, " orig"))
1724 return;
1726 links = link_sm->state->data;
1727 FOR_EACH_PTR(links, link) {
1728 compare_sm = get_sm_state(compare_id, link, NULL);
1729 if (!compare_sm)
1730 continue;
1731 data = compare_sm->state->data;
1732 if (!data || !data->comparison)
1733 continue;
1735 if (ptr_list_size((struct ptr_list *)data->vsl1) != 1 ||
1736 ptr_list_size((struct ptr_list *)data->vsl2) != 1)
1737 continue;
1738 left = first_ptr_list((struct ptr_list *)data->vsl1);
1739 right = first_ptr_list((struct ptr_list *)data->vsl2);
1740 if (left->sym == right->sym &&
1741 strcmp(left->var, right->var) == 0)
1742 continue;
1744 * Both parameters link to this comparison so only
1745 * record the first one.
1747 if (left->sym != link_sm->sym ||
1748 strcmp(left->var, link_sm->name) != 0)
1749 continue;
1751 right_name = get_printed_param_name(call, right->var, right->sym);
1752 if (!right_name)
1753 continue;
1754 snprintf(info_buf, sizeof(info_buf), "%s %s", show_special(data->comparison), right_name);
1755 sql_insert_caller_info(call, PARAM_COMPARE, param, printed_name, info_buf);
1756 } END_FOR_EACH_PTR(link);
1759 static void print_return_value_comparison(int return_id, char *return_ranges, struct expression *expr)
1761 char *name;
1762 const char *tmp_name;
1763 struct symbol *sym;
1764 int param;
1765 char info_buf[256];
1768 * TODO: This only prints == comparisons. That's probably the most
1769 * useful comparison because == max has lots of implications. But it
1770 * would be good to capture the rest as well.
1772 * This information is already in the DB but it's in the parameter math
1773 * bits and it's awkward to use it. This is is the simpler, possibly
1774 * cleaner way, but not necessarily the best, I don't know.
1777 if (!expr)
1778 return;
1779 name = expr_to_var_sym(expr, &sym);
1780 if (!name || !sym)
1781 goto free;
1783 param = get_param_num_from_sym(sym);
1784 if (param < 0)
1785 goto free;
1786 if (param_was_set_var_sym(name, sym))
1787 goto free;
1789 tmp_name = get_param_name_var_sym(name, sym);
1790 if (!tmp_name)
1791 goto free;
1793 snprintf(info_buf, sizeof(info_buf), "== $%d%s", param, tmp_name + 1);
1794 sql_insert_return_states(return_id, return_ranges,
1795 PARAM_COMPARE, -1, "$", info_buf);
1796 free:
1797 free_string(name);
1800 static void print_return_comparison(int return_id, char *return_ranges, struct expression *expr)
1802 struct sm_state *tmp;
1803 struct string_list *links;
1804 char *link;
1805 struct sm_state *sm;
1806 struct compare_data *data;
1807 struct var_sym *left, *right;
1808 int left_param, right_param;
1809 char left_buf[256];
1810 char right_buf[256];
1811 char info_buf[256];
1812 const char *tmp_name;
1814 print_return_value_comparison(return_id, return_ranges, expr);
1816 FOR_EACH_MY_SM(link_id, __get_cur_stree(), tmp) {
1817 if (get_param_num_from_sym(tmp->sym) < 0)
1818 continue;
1819 links = tmp->state->data;
1820 FOR_EACH_PTR(links, link) {
1821 sm = get_sm_state(compare_id, link, NULL);
1822 if (!sm)
1823 continue;
1824 data = sm->state->data;
1825 if (!data || !data->comparison)
1826 continue;
1827 if (ptr_list_size((struct ptr_list *)data->vsl1) != 1 ||
1828 ptr_list_size((struct ptr_list *)data->vsl2) != 1)
1829 continue;
1830 left = first_ptr_list((struct ptr_list *)data->vsl1);
1831 right = first_ptr_list((struct ptr_list *)data->vsl2);
1832 if (left->sym == right->sym &&
1833 strcmp(left->var, right->var) == 0)
1834 continue;
1836 * Both parameters link to this comparison so only
1837 * record the first one.
1839 if (left->sym != tmp->sym ||
1840 strcmp(left->var, tmp->name) != 0)
1841 continue;
1843 if (strstr(right->var, " orig"))
1844 continue;
1846 left_param = get_param_num_from_sym(left->sym);
1847 right_param = get_param_num_from_sym(right->sym);
1848 if (left_param < 0 || right_param < 0)
1849 continue;
1851 tmp_name = get_param_name_var_sym(left->var, left->sym);
1852 if (!tmp_name)
1853 continue;
1854 snprintf(left_buf, sizeof(left_buf), "%s", tmp_name);
1856 tmp_name = get_param_name_var_sym(right->var, right->sym);
1857 if (!tmp_name || tmp_name[0] != '$')
1858 continue;
1859 snprintf(right_buf, sizeof(right_buf), "$%d%s", right_param, tmp_name + 1);
1862 * FIXME: this should reject $ type variables (as
1863 * opposed to $->foo type). Those should come from
1864 * smatch_param_compare_limit.c.
1867 snprintf(info_buf, sizeof(info_buf), "%s %s", show_special(data->comparison), right_buf);
1868 sql_insert_return_states(return_id, return_ranges,
1869 PARAM_COMPARE, left_param, left_buf, info_buf);
1870 } END_FOR_EACH_PTR(link);
1872 } END_FOR_EACH_SM(tmp);
1875 static int parse_comparison(char **value, int *op)
1878 *op = **value;
1880 switch (*op) {
1881 case '<':
1882 (*value)++;
1883 if (**value == '=') {
1884 (*value)++;
1885 *op = SPECIAL_LTE;
1887 break;
1888 case '=':
1889 (*value)++;
1890 (*value)++;
1891 *op = SPECIAL_EQUAL;
1892 break;
1893 case '!':
1894 (*value)++;
1895 (*value)++;
1896 *op = SPECIAL_NOTEQUAL;
1897 break;
1898 case '>':
1899 (*value)++;
1900 if (**value == '=') {
1901 (*value)++;
1902 *op = SPECIAL_GTE;
1904 break;
1905 default:
1906 return 0;
1909 if (**value != ' ') {
1910 sm_msg("internal error parsing comparison. %s", *value);
1911 return 0;
1914 (*value)++;
1915 return 1;
1918 static int split_op_param_key(char *value, int *op, int *param, char **key)
1920 static char buf[256];
1921 char *p;
1923 if (!parse_comparison(&value, op))
1924 return 0;
1926 snprintf(buf, sizeof(buf), value);
1928 p = buf;
1929 if (*p++ != '$')
1930 return 0;
1932 *param = atoi(p);
1933 if (*param < 0 || *param > 99)
1934 return 0;
1935 p++;
1936 if (*param > 9)
1937 p++;
1938 p--;
1939 *p = '$';
1940 *key = p;
1942 return 1;
1945 static void db_return_comparison(struct expression *expr, int left_param, char *key, char *value)
1947 struct expression *left_arg, *right_arg;
1948 char *left_name = NULL;
1949 struct symbol *left_sym;
1950 char *right_name = NULL;
1951 struct symbol *right_sym;
1952 int op;
1953 int right_param;
1954 char *right_key;
1955 struct var_sym_list *left_vsl = NULL, *right_vsl = NULL;
1957 if (left_param == -1) {
1958 if (expr->type != EXPR_ASSIGNMENT)
1959 return;
1960 left_arg = strip_expr(expr->left);
1962 while (expr->type == EXPR_ASSIGNMENT)
1963 expr = strip_expr(expr->right);
1964 if (expr->type != EXPR_CALL)
1965 return;
1966 } else {
1967 while (expr->type == EXPR_ASSIGNMENT)
1968 expr = strip_expr(expr->right);
1969 if (expr->type != EXPR_CALL)
1970 return;
1972 left_arg = get_argument_from_call_expr(expr->args, left_param);
1973 if (!left_arg)
1974 return;
1977 if (!split_op_param_key(value, &op, &right_param, &right_key))
1978 return;
1980 right_arg = get_argument_from_call_expr(expr->args, right_param);
1981 if (!right_arg)
1982 return;
1984 left_name = get_variable_from_key(left_arg, key, &left_sym);
1985 if (!left_name || !left_sym)
1986 goto free;
1988 right_name = get_variable_from_key(right_arg, right_key, &right_sym);
1989 if (!right_name || !right_sym)
1990 goto free;
1992 add_var_sym(&left_vsl, left_name, left_sym);
1993 add_var_sym(&right_vsl, right_name, right_sym);
1995 add_comparison_var_sym(left_name, left_vsl, op, right_name, right_vsl);
1997 free:
1998 free_string(left_name);
1999 free_string(right_name);
2002 int param_compare_limit_is_impossible(struct expression *expr, int left_param, char *left_key, char *value)
2004 struct smatch_state *state;
2005 char *left_name = NULL;
2006 char *right_name = NULL;
2007 struct symbol *left_sym, *right_sym;
2008 struct expression *left_arg, *right_arg;
2009 int op;
2010 int right_param;
2011 char *right_key;
2012 int ret = 0;
2013 char buf[256];
2015 while (expr->type == EXPR_ASSIGNMENT)
2016 expr = strip_expr(expr->right);
2017 if (expr->type != EXPR_CALL)
2018 return 0;
2020 if (!split_op_param_key(value, &op, &right_param, &right_key))
2021 return 0;
2023 left_arg = get_argument_from_call_expr(expr->args, left_param);
2024 if (!left_arg)
2025 return 0;
2027 right_arg = get_argument_from_call_expr(expr->args, right_param);
2028 if (!right_arg)
2029 return 0;
2031 left_name = get_variable_from_key(left_arg, left_key, &left_sym);
2032 right_name = get_variable_from_key(right_arg, right_key, &right_sym);
2033 if (!left_name || !right_name)
2034 goto free;
2036 snprintf(buf, sizeof(buf), "%s vs %s", left_name, right_name);
2037 state = get_state(compare_id, buf, NULL);
2038 if (!state)
2039 goto free;
2040 if (!state_to_comparison(state))
2041 goto free;
2043 if (!filter_comparison(state_to_comparison(state), op))
2044 ret = 1;
2045 free:
2046 free_string(left_name);
2047 free_string(right_name);
2048 return ret;
2051 static void free_data(struct symbol *sym)
2053 if (__inline_fn)
2054 return;
2055 clear_compare_data_alloc();
2058 void register_comparison(int id)
2060 compare_id = id;
2061 add_hook(&save_start_states, AFTER_DEF_HOOK);
2062 add_unmatched_state_hook(compare_id, unmatched_comparison);
2063 add_merge_hook(compare_id, &merge_compare_states);
2064 add_hook(&free_data, AFTER_FUNC_HOOK);
2065 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
2066 add_split_return_callback(&print_return_comparison);
2068 select_return_states_hook(PARAM_COMPARE, &db_return_comparison);
2071 void register_comparison_late(int id)
2073 add_hook(&match_assign, ASSIGNMENT_HOOK);
2076 void register_comparison_links(int id)
2078 link_id = id;
2079 add_merge_hook(link_id, &merge_links);
2080 add_modification_hook(link_id, &match_modify);
2082 add_member_info_callback(link_id, struct_member_callback);
2085 void register_comparison_inc_dec(int id)
2087 inc_dec_id = id;
2088 add_modification_hook_late(inc_dec_id, &iter_modify);
2091 void register_comparison_inc_dec_links(int id)
2093 inc_dec_link_id = id;
2094 set_up_link_functions(inc_dec_id, inc_dec_link_id);
2097 static void filter_by_sm(struct sm_state *sm, int op,
2098 struct state_list **true_stack,
2099 struct state_list **false_stack)
2101 struct compare_data *data;
2102 int istrue = 0;
2103 int isfalse = 0;
2105 if (!sm)
2106 return;
2107 data = sm->state->data;
2108 if (!data) {
2109 if (sm->merged) {
2110 filter_by_sm(sm->left, op, true_stack, false_stack);
2111 filter_by_sm(sm->right, op, true_stack, false_stack);
2113 return;
2116 if (data->comparison &&
2117 data->comparison == filter_comparison(data->comparison, op))
2118 istrue = 1;
2120 if (data->comparison &&
2121 data->comparison == filter_comparison(data->comparison, negate_comparison(op)))
2122 isfalse = 1;
2124 if (istrue)
2125 add_ptr_list(true_stack, sm);
2126 if (isfalse)
2127 add_ptr_list(false_stack, sm);
2129 if (sm->merged) {
2130 filter_by_sm(sm->left, op, true_stack, false_stack);
2131 filter_by_sm(sm->right, op, true_stack, false_stack);
2135 struct sm_state *comparison_implication_hook(struct expression *expr,
2136 struct state_list **true_stack,
2137 struct state_list **false_stack)
2139 struct sm_state *sm;
2140 char *left, *right;
2141 int op;
2142 static char buf[256];
2144 if (expr->type != EXPR_COMPARE)
2145 return NULL;
2147 op = expr->op;
2149 left = expr_to_var(expr->left);
2150 right = expr_to_var(expr->right);
2151 if (!left || !right) {
2152 free_string(left);
2153 free_string(right);
2154 return NULL;
2157 if (strcmp(left, right) > 0) {
2158 char *tmp = left;
2160 left = right;
2161 right = tmp;
2162 op = flip_comparison(op);
2165 snprintf(buf, sizeof(buf), "%s vs %s", left, right);
2166 sm = get_sm_state(compare_id, buf, NULL);
2167 if (!sm)
2168 return NULL;
2169 if (!sm->merged)
2170 return NULL;
2172 filter_by_sm(sm, op, true_stack, false_stack);
2173 if (!*true_stack && !*false_stack)
2174 return NULL;
2176 return sm;