40 static void add_comparison(struct expression *left, int comparison, struct expression *right);
41
42 /* for handling for loops */
43 STATE(start);
44 STATE(incremented);
45
46 ALLOCATOR(compare_data, "compare data");
47
48 static struct symbol *vsl_to_sym(struct var_sym_list *vsl)
49 {
50 struct var_sym *vs;
51
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;
58 }
59
60 struct smatch_state *alloc_compare_state(
61 struct expression *left,
62 const char *left_var, struct var_sym_list *left_vsl,
63 int comparison,
64 struct expression *right,
65 const char *right_var, struct var_sym_list *right_vsl)
66 {
67 struct smatch_state *state;
68 struct compare_data *data;
69
70 state = __alloc_smatch_state(0);
71 state->name = alloc_sname(show_special(comparison));
72 data = __alloc_compare_data(0);
73 data->left = left;
74 data->left_var = alloc_sname(left_var);
75 data->left_vsl = clone_var_sym_list(left_vsl);
76 data->comparison = comparison;
77 data->right = right;
78 data->right_var = alloc_sname(right_var);
79 data->right_vsl = clone_var_sym_list(right_vsl);
80 state->data = data;
81 return state;
82 }
83
84 int state_to_comparison(struct smatch_state *state)
85 {
86 if (!state || !state->data)
87 return 0;
88 return ((struct compare_data *)state->data)->comparison;
89 }
90
91 /*
92 * flip_comparison() reverses the op left and right. So "x >= y" becomes "y <= x".
93 */
94 int flip_comparison(int op)
95 {
96 switch (op) {
97 case 0:
98 return 0;
99 case '<':
100 return '>';
101 case SPECIAL_UNSIGNED_LT:
102 return SPECIAL_UNSIGNED_GT;
103 case SPECIAL_LTE:
104 return SPECIAL_GTE;
105 case SPECIAL_UNSIGNED_LTE:
106 return SPECIAL_UNSIGNED_GTE;
107 case SPECIAL_EQUAL:
108 return SPECIAL_EQUAL;
109 case SPECIAL_NOTEQUAL:
110 return SPECIAL_NOTEQUAL;
111 case SPECIAL_GTE:
112 return SPECIAL_LTE;
113 case SPECIAL_UNSIGNED_GTE:
114 return SPECIAL_UNSIGNED_LTE;
115 case '>':
116 return '<';
117 case SPECIAL_UNSIGNED_GT:
118 return SPECIAL_UNSIGNED_LT;
119 default:
120 sm_perror("unhandled comparison %d", op);
121 return op;
122 }
123 }
124
125 int negate_comparison(int op)
126 {
127 switch (op) {
128 case 0:
129 return 0;
130 case '<':
131 return SPECIAL_GTE;
132 case SPECIAL_UNSIGNED_LT:
133 return SPECIAL_UNSIGNED_GTE;
134 case SPECIAL_LTE:
135 return '>';
136 case SPECIAL_UNSIGNED_LTE:
137 return SPECIAL_UNSIGNED_GT;
138 case SPECIAL_EQUAL:
139 return SPECIAL_NOTEQUAL;
140 case SPECIAL_NOTEQUAL:
141 return SPECIAL_EQUAL;
142 case SPECIAL_GTE:
143 return '<';
144 case SPECIAL_UNSIGNED_GTE:
145 return SPECIAL_UNSIGNED_LT;
146 case '>':
147 return SPECIAL_LTE;
148 case SPECIAL_UNSIGNED_GT:
149 return SPECIAL_UNSIGNED_LTE;
150 default:
151 sm_perror("unhandled comparison %d", op);
152 return op;
153 }
154 }
155
156 static int rl_comparison(struct range_list *left_rl, struct range_list *right_rl)
157 {
158 sval_t left_min, left_max, right_min, right_max;
159 struct symbol *type = &int_ctype;
160
161 if (!left_rl || !right_rl)
162 return 0;
163
164 if (type_positive_bits(rl_type(left_rl)) > type_positive_bits(type))
165 type = rl_type(left_rl);
166 if (type_positive_bits(rl_type(right_rl)) > type_positive_bits(type))
167 type = rl_type(right_rl);
168
169 left_rl = cast_rl(type, left_rl);
170 right_rl = cast_rl(type, right_rl);
171
172 left_min = rl_min(left_rl);
173 left_max = rl_max(left_rl);
174 right_min = rl_min(right_rl);
175 right_max = rl_max(right_rl);
176
177 if (left_min.value == left_max.value &&
178 right_min.value == right_max.value &&
179 left_min.value == right_min.value)
180 return SPECIAL_EQUAL;
181
182 if (sval_cmp(left_max, right_min) < 0)
183 return '<';
184 if (sval_cmp(left_max, right_min) == 0)
185 return SPECIAL_LTE;
186 if (sval_cmp(left_min, right_max) > 0)
187 return '>';
188 if (sval_cmp(left_min, right_max) == 0)
189 return SPECIAL_GTE;
190
191 return 0;
192 }
193
194 static int comparison_from_extra(struct expression *a, struct expression *b)
195 {
196 struct range_list *left, *right;
197
198 if (!get_implied_rl(a, &left))
199 return 0;
200 if (!get_implied_rl(b, &right))
201 return 0;
202
203 return rl_comparison(left, right);
204 }
205
206 static struct range_list *get_orig_rl(struct var_sym_list *vsl)
207 {
208 struct symbol *sym;
209 struct smatch_state *state;
210
211 if (!vsl)
212 return NULL;
213 sym = vsl_to_sym(vsl);
214 if (!sym || !sym->ident)
215 return NULL;
216 state = get_orig_estate(sym->ident->name, sym);
217 return estate_rl(state);
218 }
219
220 static struct smatch_state *unmatched_comparison(struct sm_state *sm)
221 {
222 struct compare_data *data = sm->state->data;
223 struct range_list *left_rl, *right_rl;
224 int op;
225
226 if (!data)
227 return &undefined;
228
229 if (strstr(data->left_var, " orig"))
230 left_rl = get_orig_rl(data->left_vsl);
231 else if (!get_implied_rl_var_sym(data->left_var, vsl_to_sym(data->left_vsl), &left_rl))
232 return &undefined;
233
234 if (strstr(data->right_var, " orig"))
235 right_rl = get_orig_rl(data->right_vsl);
236 else if (!get_implied_rl_var_sym(data->right_var, vsl_to_sym(data->right_vsl), &right_rl))
237 return &undefined;
238
239 op = rl_comparison(left_rl, right_rl);
240 if (op)
241 return alloc_compare_state(
242 data->left, data->left_var, data->left_vsl,
243 op,
244 data->right, data->right_var, data->right_vsl);
245
246 return &undefined;
247 }
248
249 /* remove_unsigned_from_comparison() is obviously a hack. */
250 int remove_unsigned_from_comparison(int op)
251 {
252 switch (op) {
253 case SPECIAL_UNSIGNED_LT:
254 return '<';
255 case SPECIAL_UNSIGNED_LTE:
256 return SPECIAL_LTE;
257 case SPECIAL_UNSIGNED_GTE:
258 return SPECIAL_GTE;
259 case SPECIAL_UNSIGNED_GT:
260 return '>';
261 default:
262 return op;
263 }
264 }
265
266 /*
267 * This is for when you merge states "a < b" and "a == b", the result is that
268 * we can say for sure, "a <= b" after the merge.
269 */
270 int merge_comparisons(int one, int two)
271 {
272 int LT, EQ, GT;
273
274 if (!one || !two)
275 return 0;
276
277 one = remove_unsigned_from_comparison(one);
278 two = remove_unsigned_from_comparison(two);
279
280 if (one == two)
281 return one;
282
283 LT = EQ = GT = 0;
284
285 switch (one) {
286 case '<':
287 LT = 1;
288 break;
289 case SPECIAL_LTE:
290 LT = 1;
291 EQ = 1;
292 break;
293 case SPECIAL_EQUAL:
294 EQ = 1;
295 break;
296 case SPECIAL_GTE:
304 switch (two) {
305 case '<':
306 LT = 1;
307 break;
308 case SPECIAL_LTE:
309 LT = 1;
310 EQ = 1;
311 break;
312 case SPECIAL_EQUAL:
313 EQ = 1;
314 break;
315 case SPECIAL_GTE:
316 GT = 1;
317 EQ = 1;
318 break;
319 case '>':
320 GT = 1;
321 }
322
323 if (LT && EQ && GT)
324 return 0;
325 if (LT && EQ)
326 return SPECIAL_LTE;
327 if (LT && GT)
328 return SPECIAL_NOTEQUAL;
329 if (LT)
330 return '<';
331 if (EQ && GT)
332 return SPECIAL_GTE;
333 if (GT)
334 return '>';
335 return 0;
336 }
337
338 /*
339 * This is for if you have "a < b" and "b <= c". Or in other words,
340 * "a < b <= c". You would call this like get_combined_comparison('<', '<=').
341 * The return comparison would be '<'.
342 *
343 * This function is different from merge_comparisons(), for example:
344 * merge_comparison('<', '==') returns '<='
345 * get_combined_comparison('<', '==') returns '<'
346 */
347 int combine_comparisons(int left_compare, int right_compare)
348 {
349 int LT, EQ, GT;
350
351 left_compare = remove_unsigned_from_comparison(left_compare);
352 right_compare = remove_unsigned_from_comparison(right_compare);
353
354 LT = EQ = GT = 0;
355
356 switch (left_compare) {
357 case '<':
358 LT++;
359 break;
360 case SPECIAL_LTE:
361 LT++;
362 EQ++;
363 break;
364 case SPECIAL_EQUAL:
365 return right_compare;
383 return left_compare;
384 case SPECIAL_GTE:
385 GT++;
386 EQ++;
387 break;
388 case '>':
389 GT++;
390 }
391
392 if (LT == 2) {
393 if (EQ == 2)
394 return SPECIAL_LTE;
395 return '<';
396 }
397
398 if (GT == 2) {
399 if (EQ == 2)
400 return SPECIAL_GTE;
401 return '>';
402 }
403 return 0;
404 }
405
406 int filter_comparison(int orig, int op)
407 {
408 if (orig == op)
409 return orig;
410
411 orig = remove_unsigned_from_comparison(orig);
412 op = remove_unsigned_from_comparison(op);
413
414 switch (orig) {
415 case 0:
416 return op;
417 case '<':
418 switch (op) {
419 case '<':
420 case SPECIAL_LTE:
421 case SPECIAL_NOTEQUAL:
422 return '<';
423 }
424 return 0;
425 case SPECIAL_LTE:
426 switch (op) {
427 case '<':
428 case SPECIAL_LTE:
429 case SPECIAL_EQUAL:
430 return op;
431 case SPECIAL_NOTEQUAL:
432 return '<';
433 }
434 return 0;
435 case SPECIAL_EQUAL:
436 switch (op) {
437 case SPECIAL_LTE:
438 case SPECIAL_EQUAL:
439 case SPECIAL_GTE:
440 case SPECIAL_UNSIGNED_LTE:
441 case SPECIAL_UNSIGNED_GTE:
442 return SPECIAL_EQUAL;
443 }
444 return 0;
445 case SPECIAL_NOTEQUAL:
446 switch (op) {
447 case '<':
448 case SPECIAL_LTE:
449 return '<';
450 case SPECIAL_UNSIGNED_LT:
451 case SPECIAL_UNSIGNED_LTE:
452 return SPECIAL_UNSIGNED_LT;
453 case SPECIAL_NOTEQUAL:
454 return op;
455 case '>':
456 case SPECIAL_GTE:
457 return '>';
458 case SPECIAL_UNSIGNED_GT:
459 case SPECIAL_UNSIGNED_GTE:
460 return SPECIAL_UNSIGNED_GT;
461 }
462 return 0;
463 case SPECIAL_GTE:
464 switch (op) {
465 case SPECIAL_LTE:
466 return SPECIAL_EQUAL;
467 case '>':
468 case SPECIAL_GTE:
469 case SPECIAL_EQUAL:
470 return op;
471 case SPECIAL_NOTEQUAL:
472 return '>';
473 }
474 return 0;
475 case '>':
476 switch (op) {
477 case '>':
478 case SPECIAL_GTE:
479 case SPECIAL_NOTEQUAL:
480 return '>';
481 }
482 return 0;
483 case SPECIAL_UNSIGNED_LT:
484 switch (op) {
485 case SPECIAL_UNSIGNED_LT:
486 case SPECIAL_UNSIGNED_LTE:
487 case SPECIAL_NOTEQUAL:
488 return SPECIAL_UNSIGNED_LT;
489 }
490 return 0;
491 case SPECIAL_UNSIGNED_LTE:
492 switch (op) {
493 case SPECIAL_UNSIGNED_LT:
494 case SPECIAL_UNSIGNED_LTE:
495 case SPECIAL_EQUAL:
496 return op;
497 case SPECIAL_NOTEQUAL:
498 return SPECIAL_UNSIGNED_LT;
499 case SPECIAL_UNSIGNED_GTE:
500 return SPECIAL_EQUAL;
501 }
502 return 0;
503 case SPECIAL_UNSIGNED_GTE:
504 switch (op) {
505 case SPECIAL_UNSIGNED_LTE:
506 return SPECIAL_EQUAL;
507 case SPECIAL_NOTEQUAL:
508 return SPECIAL_UNSIGNED_GT;
509 case SPECIAL_EQUAL:
510 case SPECIAL_UNSIGNED_GTE:
511 case SPECIAL_UNSIGNED_GT:
512 return op;
513 }
514 return 0;
515 case SPECIAL_UNSIGNED_GT:
516 switch (op) {
517 case SPECIAL_UNSIGNED_GT:
518 case SPECIAL_UNSIGNED_GTE:
519 case SPECIAL_NOTEQUAL:
520 return SPECIAL_UNSIGNED_GT;
521 }
522 return 0;
523 }
524 return 0;
525 }
526
527 static void pre_merge_hook(struct sm_state *sm)
528 {
529 struct compare_data *data = sm->state->data;
530 int other;
531
532 if (!data)
533 return;
534 other = get_comparison(data->left, data->right);
535 if (!other)
536 return;
537
538 set_state(compare_id, sm->name, NULL,
539 alloc_compare_state(data->left, data->left_var, data->left_vsl,
540 other,
541 data->right, data->right_var, data->right_vsl));
542 }
543
544 struct smatch_state *merge_compare_states(struct smatch_state *s1, struct smatch_state *s2)
545 {
546 struct compare_data *data = s1->data;
547 int op;
548
549 op = merge_comparisons(state_to_comparison(s1), state_to_comparison(s2));
550 if (op)
551 return alloc_compare_state(
552 data->left, data->left_var, data->left_vsl,
553 op,
554 data->right, data->right_var, data->right_vsl);
555 return &undefined;
556 }
557
558 static struct smatch_state *alloc_link_state(struct string_list *links)
559 {
560 struct smatch_state *state;
561 static char buf[256];
562 char *tmp;
563 int i;
564
565 state = __alloc_smatch_state(0);
566
567 i = 0;
568 FOR_EACH_PTR(links, tmp) {
569 if (!i++) {
570 snprintf(buf, sizeof(buf), "%s", tmp);
571 } else {
572 append(buf, ", ", sizeof(buf));
573 append(buf, tmp, sizeof(buf));
574 }
575 } END_FOR_EACH_PTR(tmp);
673 case SPECIAL_UNSIGNED_GTE:
674 case '>':
675 case SPECIAL_UNSIGNED_GT:
676 if (preserve)
677 break;
678 new = alloc_compare_state(
679 data->left, data->left_var, data->left_vsl,
680 flip ? '<' : '>',
681 data->right, data->right_var, data->right_vsl);
682 set_state(compare_id, tmp, NULL, new);
683 break;
684 case '<':
685 case SPECIAL_UNSIGNED_LT:
686 new = alloc_compare_state(
687 data->left, data->left_var, data->left_vsl,
688 flip ? SPECIAL_GTE : SPECIAL_LTE,
689 data->right, data->right_var, data->right_vsl);
690 set_state(compare_id, tmp, NULL, new);
691 break;
692 default:
693 set_state(compare_id, tmp, NULL, &undefined);
694 }
695 } END_FOR_EACH_PTR(tmp);
696 }
697
698 static void match_dec(struct sm_state *sm, bool preserve)
699 {
700 struct string_list *links;
701 struct smatch_state *state;
702 char *tmp;
703
704 links = sm->state->data;
705
706 FOR_EACH_PTR(links, tmp) {
707 state = get_state(compare_id, tmp, NULL);
708
709 switch (state_to_comparison(state)) {
710 case SPECIAL_EQUAL:
711 case SPECIAL_LTE:
712 case SPECIAL_UNSIGNED_LTE:
713 case '<':
714 case SPECIAL_UNSIGNED_LT: {
715 struct compare_data *data = state->data;
716 struct smatch_state *new;
717
718 if (preserve)
719 break;
720
721 new = alloc_compare_state(
722 data->left, data->left_var, data->left_vsl,
723 '<',
724 data->right, data->right_var, data->right_vsl);
725 set_state(compare_id, tmp, NULL, new);
726 break;
727 }
728 default:
729 set_state(compare_id, tmp, NULL, &undefined);
730 }
731 } END_FOR_EACH_PTR(tmp);
732 }
733
734 static void reset_sm(struct sm_state *sm)
735 {
736 struct string_list *links;
737 char *tmp;
738
739 links = sm->state->data;
740
741 FOR_EACH_PTR(links, tmp) {
742 set_state(compare_id, tmp, NULL, &undefined);
743 } END_FOR_EACH_PTR(tmp);
744 set_state(link_id, sm->name, sm->sym, &undefined);
745 }
746
747 static bool match_add_sub_assign(struct sm_state *sm, struct expression *expr)
748 {
749 struct range_list *rl;
750 sval_t zero = { .type = &int_ctype };
751
752 if (!expr || expr->type != EXPR_ASSIGNMENT)
753 return false;
754 if (expr->op != SPECIAL_ADD_ASSIGN && expr->op != SPECIAL_SUB_ASSIGN)
755 return false;
756
757 get_absolute_rl(expr->right, &rl);
758 if (sval_is_negative(rl_min(rl))) {
759 reset_sm(sm);
760 return false;
761 }
762
832 *
833 */
834
835 if (expr->type != EXPR_PREOP ||
836 (expr->op != SPECIAL_INCREMENT && expr->op != SPECIAL_DECREMENT))
837 return;
838
839 parent = expr_get_parent_expr(expr);
840 if (!parent)
841 return;
842 if (parent->type != EXPR_COMPARE || parent->op != SPECIAL_EQUAL)
843 return;
844 if (parent->left != expr)
845 return;
846
847 if (!get_implied_rl(expr->unop, &left) ||
848 !get_implied_rl(parent->right, &right))
849 return;
850
851 op = rl_comparison(left, right);
852 if (!op)
853 return;
854
855 add_comparison(expr->unop, op, parent->right);
856 }
857
858 static char *chunk_to_var_sym(struct expression *expr, struct symbol **sym)
859 {
860 expr = strip_expr(expr);
861 if (!expr)
862 return NULL;
863 if (sym)
864 *sym = NULL;
865
866 if (expr->type == EXPR_PREOP &&
867 (expr->op == SPECIAL_INCREMENT ||
868 expr->op == SPECIAL_DECREMENT))
869 expr = strip_expr(expr->unop);
870
871 if (expr->type == EXPR_CALL) {
872 char buf[64];
999 continue;
1000 data = state->data;
1001 right_comparison = data->comparison;
1002 right_expr = data->right;
1003 right_var = data->right_var;
1004 right_vsl = data->right_vsl;
1005 if (strcmp(mid_var, right_var) == 0) {
1006 right_expr = data->left;
1007 right_var = data->left_var;
1008 right_vsl = data->left_vsl;
1009 right_comparison = flip_comparison(right_comparison);
1010 }
1011 if (have_common_var_sym(left_vsl, right_vsl))
1012 continue;
1013
1014 orig_comparison = get_orig_comparison(pre_stree, left_var, right_var);
1015
1016 true_comparison = combine_comparisons(left_comparison, right_comparison);
1017 false_comparison = combine_comparisons(left_false_comparison, right_comparison);
1018
1019 true_comparison = filter_comparison(orig_comparison, true_comparison);
1020 false_comparison = filter_comparison(orig_comparison, false_comparison);
1021
1022 if (strcmp(left_var, right_var) > 0) {
1023 struct expression *tmp_expr = left_expr;
1024 const char *tmp_var = left_var;
1025 struct var_sym_list *tmp_vsl = left_vsl;
1026
1027 left_expr = right_expr;
1028 left_var = right_var;
1029 left_vsl = right_vsl;
1030 right_expr = tmp_expr;
1031 right_var = tmp_var;
1032 right_vsl = tmp_vsl;
1033 true_comparison = flip_comparison(true_comparison);
1034 false_comparison = flip_comparison(false_comparison);
1035 }
1036
1037 if (!true_comparison && !false_comparison)
1038 continue;
1039
1040 if (true_comparison)
1259 add_var_sym(&right_vsl, right, right_sym);
1260 else
1261 right_vsl = expr_to_vsl(right_expr);
1262
1263 if (strcmp(left, right) > 0) {
1264 char *tmp_name = left;
1265 struct var_sym_list *tmp_vsl = left_vsl;
1266 struct expression *tmp_expr = left_expr;
1267
1268 left = right;
1269 left_vsl = right_vsl;
1270 left_expr = right_expr;
1271 right = tmp_name;
1272 right_vsl = tmp_vsl;
1273 right_expr = tmp_expr;
1274 op = flip_comparison(op);
1275 false_op = flip_comparison(false_op);
1276 }
1277
1278 orig_comparison = get_comparison(left_expr, right_expr);
1279 op = filter_comparison(orig_comparison, op);
1280 false_op = filter_comparison(orig_comparison, false_op);
1281
1282 snprintf(state_name, sizeof(state_name), "%s vs %s", left, right);
1283 true_state = alloc_compare_state(
1284 left_expr, left, left_vsl,
1285 op,
1286 right_expr, right, right_vsl);
1287 false_state = alloc_compare_state(
1288 left_expr, left, left_vsl,
1289 false_op,
1290 right_expr, right, right_vsl);
1291
1292 pre_stree = clone_stree(__get_cur_stree());
1293 update_tf_data(pre_stree, left_expr, left, left_vsl, right_expr, right, right_vsl, op, false_op);
1294 free_stree(&pre_stree);
1295
1296 set_true_false_states(compare_id, state_name, NULL, true_state, false_state);
1297 __compare_param_limit_hook(left_expr, right_expr, state_name, true_state, false_state);
1298 save_link(left_expr, state_name);
1299 save_link(right_expr, state_name);
1300
1317 if (expr->type != EXPR_COMPARE)
1318 return;
1319
1320 handle_comparison(expr->left, expr->op, expr->right, &state_name, &false_state);
1321 if (false_state && state_name)
1322 handle_for_loops(expr, state_name, false_state);
1323
1324 left = strip_parens(expr->left);
1325 right = strip_parens(expr->right);
1326
1327 if (left->type == EXPR_BINOP && left->op == '+') {
1328 new_left = left->left;
1329 new_right = binop_expression(right, '-', left->right);
1330 handle_comparison(new_left, expr->op, new_right, NULL, NULL);
1331
1332 new_left = left->right;
1333 new_right = binop_expression(right, '-', left->left);
1334 handle_comparison(new_left, expr->op, new_right, NULL, NULL);
1335 }
1336
1337
1338 redo = 0;
1339 left = strip_parens(expr->left);
1340 right = strip_parens(expr->right);
1341 if (get_last_expr_from_expression_stmt(expr->left)) {
1342 left = get_last_expr_from_expression_stmt(expr->left);
1343 redo = 1;
1344 }
1345 if (get_last_expr_from_expression_stmt(expr->right)) {
1346 right = get_last_expr_from_expression_stmt(expr->right);
1347 redo = 1;
1348 }
1349
1350 if (!redo)
1351 return;
1352
1353 count = 0;
1354 while ((tmp = get_assigned_expr(left))) {
1355 if (count++ > 3)
1356 break;
1357 left = strip_expr(tmp);
1601
1602 if (is_self_assign(expr))
1603 return;
1604
1605 copy_comparisons(expr->left, expr->right);
1606 add_comparison(expr->left, SPECIAL_EQUAL, expr->right);
1607
1608 right = strip_expr(expr->right);
1609 if (right->type == EXPR_BINOP)
1610 match_binop_assign(expr);
1611 }
1612
1613 int get_comparison_strings(const char *one, const char *two)
1614 {
1615 char buf[256];
1616 struct smatch_state *state;
1617 int invert = 0;
1618 int ret = 0;
1619
1620 if (!one || !two)
1621 return 0;
1622
1623 if (strcmp(one, two) == 0)
1624 return SPECIAL_EQUAL;
1625
1626 if (strcmp(one, two) > 0) {
1627 const char *tmp = one;
1628
1629 one = two;
1630 two = tmp;
1631 invert = 1;
1632 }
1633
1634 snprintf(buf, sizeof(buf), "%s vs %s", one, two);
1635 state = get_state(compare_id, buf, NULL);
1636 if (state)
1637 ret = state_to_comparison(state);
1638
1639 if (invert)
1640 ret = flip_comparison(ret);
1641
1642 return ret;
1643 }
1644
1645 static int get_comparison_helper(struct expression *a, struct expression *b, bool use_extra)
1646 {
1647 char *one = NULL;
1648 char *two = NULL;
1649 int ret = 0;
1650
1651 if (!a || !b)
1652 return 0;
1653
1654 a = strip_parens(a);
1655 b = strip_parens(b);
1656
1657 move_plus_to_minus(&a, &b);
1658
1659 one = chunk_to_var(a);
1660 if (!one)
1661 goto free;
1662 two = chunk_to_var(b);
1663 if (!two)
1664 goto free;
1665
1666 ret = get_comparison_strings(one, two);
1667 if (ret)
1668 goto free;
1669
1670 if (is_plus_one(a) || is_minus_one(a)) {
1671 free_string(one);
1672 one = chunk_to_var(a->left);
1673 ret = get_comparison_strings(one, two);
1674 } else if (is_plus_one(b) || is_minus_one(b)) {
1675 free_string(two);
1676 two = chunk_to_var(b->left);
1677 ret = get_comparison_strings(one, two);
1678 }
1679
1680 if (!ret)
1681 goto free;
1682
1683 if ((is_plus_one(a) || is_minus_one(b)) && ret == '<')
1684 ret = SPECIAL_LTE;
1685 else if ((is_minus_one(a) || is_plus_one(b)) && ret == '>')
1686 ret = SPECIAL_GTE;
1687 else
1688 ret = 0;
1689
1690 free:
1691 free_string(one);
1692 free_string(two);
1693
1694 if (!ret && use_extra)
1695 return comparison_from_extra(a, b);
1696 return ret;
1697 }
1698
1699 int get_comparison(struct expression *a, struct expression *b)
1700 {
1701 return get_comparison_helper(a, b, true);
1702 }
1703
1704 int get_comparison_no_extra(struct expression *a, struct expression *b)
1705 {
1706 return get_comparison_helper(a, b, false);
1707 }
1708
1709 int possible_comparison(struct expression *a, int comparison, struct expression *b)
1710 {
1711 char *one = NULL;
1712 char *two = NULL;
1713 int ret = 0;
1714 char buf[256];
1715 struct sm_state *sm;
1716 int saved;
1888 if (strcmp(var, right_var) == 0) {
1889 expr = data->left;
1890 var = data->left_var;
1891 vsl = data->left_vsl;
1892 comparison = flip_comparison(comparison);
1893 }
1894 comparison = combine_comparisons(left_compare, comparison);
1895 if (!comparison)
1896 continue;
1897 add_comparison_var_sym(left, left_var, left_vsl, comparison, expr, var, vsl);
1898 } END_FOR_EACH_PTR(tmp);
1899
1900 done:
1901 free_string(right_var);
1902 }
1903
1904 void __add_return_comparison(struct expression *call, const char *range)
1905 {
1906 struct expression *arg;
1907 int comparison;
1908 char buf[4];
1909
1910 if (!str_to_comparison_arg(range, call, &comparison, &arg))
1911 return;
1912 snprintf(buf, sizeof(buf), "%s", show_special(comparison));
1913 update_links_from_call(call, comparison, arg);
1914 add_comparison(call, comparison, arg);
1915 }
1916
1917 void __add_comparison_info(struct expression *expr, struct expression *call, const char *range)
1918 {
1919 copy_comparisons(expr, call);
1920 }
1921
1922 static char *get_mask_comparison(struct expression *expr, int ignore)
1923 {
1924 struct expression *tmp, *right;
1925 int count, param;
1926 char buf[256];
1927
1928 /* The return value for "return foo & param;" is <= param */
1929
1930 count = 0;
1931 while ((tmp = get_assigned_expr(expr))) {
1932 expr = strip_expr(tmp);
1954 char *ret_str = NULL;
1955 int compare;
1956 int i;
1957
1958 if (!expr)
1959 return NULL;
1960
1961 var = chunk_to_var(expr);
1962 if (!var)
1963 goto try_mask;
1964
1965 i = -1;
1966 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, param) {
1967 i++;
1968 if (i == ignore)
1969 continue;
1970 if (!param->ident)
1971 continue;
1972 snprintf(buf, sizeof(buf), "%s orig", param->ident->name);
1973 compare = get_comparison_strings(var, buf);
1974 if (!compare)
1975 continue;
1976 if (show_special(compare)[0] != starts_with)
1977 continue;
1978 snprintf(buf, sizeof(buf), "[%s$%d]", show_special(compare), i);
1979 ret_str = alloc_sname(buf);
1980 break;
1981 } END_FOR_EACH_PTR(param);
1982
1983 free_string(var);
1984 if (!ret_str)
1985 goto try_mask;
1986
1987 return ret_str;
1988
1989 try_mask:
1990 if (starts_with == '<')
1991 ret_str = get_mask_comparison(expr, ignore);
1992 return ret_str;
1993 }
1994
1995 char *name_sym_to_param_comparison(const char *name, struct symbol *sym)
1996 {
1997 struct symbol *param;
1998 char buf[256];
1999 int compare;
2000 int i;
2001
2002 i = -1;
2003 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, param) {
2004 i++;
2005 if (!param->ident)
2006 continue;
2007 snprintf(buf, sizeof(buf), "%s orig", param->ident->name);
2008 compare = get_comparison_strings(name, buf);
2009 if (!compare)
2010 continue;
2011 snprintf(buf, sizeof(buf), "[%s$%d]", show_special(compare), i);
2012 return alloc_sname(buf);
2013 } END_FOR_EACH_PTR(param);
2014
2015 return NULL;
2016 }
2017
2018 char *expr_equal_to_param(struct expression *expr, int ignore)
2019 {
2020 return range_comparison_to_param_helper(expr, '=', ignore);
2021 }
2022
2023 char *expr_lte_to_param(struct expression *expr, int ignore)
2024 {
2025 return range_comparison_to_param_helper(expr, '<', ignore);
2026 }
2027
2028 char *expr_param_comparison(struct expression *expr, int ignore)
2029 {
2030 struct symbol *param;
2031 char *var = NULL;
2032 char buf[256];
2033 char *ret_str = NULL;
2034 int compare;
2035 int i;
2036
2037 var = chunk_to_var(expr);
2038 if (!var)
2039 goto free;
2040
2041 i = -1;
2042 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, param) {
2043 i++;
2044 if (i == ignore)
2045 continue;
2046 if (!param->ident)
2047 continue;
2048 snprintf(buf, sizeof(buf), "%s orig", param->ident->name);
2049 compare = get_comparison_strings(var, buf);
2050 if (!compare)
2051 continue;
2052 snprintf(buf, sizeof(buf), "[%s$%d]", show_special(compare), i);
2053 ret_str = alloc_sname(buf);
2054 break;
2055 } END_FOR_EACH_PTR(param);
2056
2057 free:
2058 free_string(var);
2059 return ret_str;
2060 }
2061
2062 char *get_printed_param_name(struct expression *call, const char *param_name, struct symbol *param_sym)
2063 {
2064 struct expression *arg;
2065 char *name;
2066 struct symbol *sym;
2067 static char buf[256];
2068 int len;
2069 int i;
2070
2071 i = -1;
2072 FOR_EACH_PTR(call->args, arg) {
2111 i = -1;
2112 FOR_EACH_PTR(expr->args, arg) {
2113 i++;
2114
2115 state = get_state_chunk(link_id, arg);
2116 if (!state)
2117 continue;
2118
2119 links = state->data;
2120 FOR_EACH_PTR(links, link) {
2121 struct var_sym_list *right_vsl;
2122 struct var_sym *right_vs;
2123
2124
2125 if (strstr(link, " orig"))
2126 continue;
2127 sm = get_sm_state(compare_id, link, NULL);
2128 if (!sm)
2129 continue;
2130 data = sm->state->data;
2131 if (!data || !data->comparison)
2132 continue;
2133 arg_name = expr_to_var(arg);
2134 if (!arg_name)
2135 continue;
2136
2137 right_vsl = NULL;
2138 if (strcmp(data->left_var, arg_name) == 0) {
2139 comparison = data->comparison;
2140 right_name = data->right_var;
2141 right_vsl = data->right_vsl;
2142 } else if (strcmp(data->right_var, arg_name) == 0) {
2143 comparison = flip_comparison(data->comparison);
2144 right_name = data->left_var;
2145 right_vsl = data->left_vsl;
2146 }
2147 if (!right_vsl || ptr_list_size((struct ptr_list *)right_vsl) != 1)
2148 goto free;
2149
2150 right_vs = first_ptr_list((struct ptr_list *)right_vsl);
2151 if (strcmp(right_vs->var, right_name) != 0)
2152 goto free;
2153 right_name = get_printed_param_name(expr, right_vs->var, right_vs->sym);
2154 if (!right_name)
2155 goto free;
2156 snprintf(info_buf, sizeof(info_buf), "%s %s", show_special(comparison), right_name);
2157 sql_insert_caller_info(expr, PARAM_COMPARE, i, "$", info_buf);
2158
2159 free:
2160 free_string(arg_name);
2161 } END_FOR_EACH_PTR(link);
2162 } END_FOR_EACH_PTR(arg);
2163 }
2164
2165 static void struct_member_callback(struct expression *call, int param, char *printed_name, struct sm_state *link_sm)
2166 {
2167 struct sm_state *compare_sm;
2168 struct string_list *links;
2169 char *link;
2170 struct compare_data *data;
2171 struct var_sym *left, *right;
2172 static char info_buf[256];
2173 const char *right_name;
2174
2175 if (strstr(printed_name, " orig"))
2176 return;
2186
2187 if (ptr_list_size((struct ptr_list *)data->left_vsl) != 1 ||
2188 ptr_list_size((struct ptr_list *)data->right_vsl) != 1)
2189 continue;
2190 left = first_ptr_list((struct ptr_list *)data->left_vsl);
2191 right = first_ptr_list((struct ptr_list *)data->right_vsl);
2192 if (left->sym == right->sym &&
2193 strcmp(left->var, right->var) == 0)
2194 continue;
2195 /*
2196 * Both parameters link to this comparison so only
2197 * record the first one.
2198 */
2199 if (left->sym != link_sm->sym ||
2200 strcmp(left->var, link_sm->name) != 0)
2201 continue;
2202
2203 right_name = get_printed_param_name(call, right->var, right->sym);
2204 if (!right_name)
2205 continue;
2206 snprintf(info_buf, sizeof(info_buf), "%s %s", show_special(data->comparison), right_name);
2207 sql_insert_caller_info(call, PARAM_COMPARE, param, printed_name, info_buf);
2208 } END_FOR_EACH_PTR(link);
2209 }
2210
2211 static void print_return_value_comparison(int return_id, char *return_ranges, struct expression *expr)
2212 {
2213 char *name;
2214 const char *tmp_name;
2215 struct symbol *sym;
2216 int param;
2217 char info_buf[256];
2218
2219 /*
2220 * TODO: This only prints == comparisons. That's probably the most
2221 * useful comparison because == max has lots of implications. But it
2222 * would be good to capture the rest as well.
2223 *
2224 * This information is already in the DB but it's in the parameter math
2225 * bits and it's awkward to use it. This is is the simpler, possibly
2226 * cleaner way, but not necessarily the best, I don't know.
2257 struct sm_state *sm;
2258 struct compare_data *data;
2259 struct var_sym *left, *right;
2260 int left_param, right_param;
2261 char left_buf[256];
2262 char right_buf[256];
2263 char info_buf[258];
2264 const char *tmp_name;
2265
2266 print_return_value_comparison(return_id, return_ranges, expr);
2267
2268 FOR_EACH_MY_SM(link_id, __get_cur_stree(), tmp) {
2269 if (get_param_num_from_sym(tmp->sym) < 0)
2270 continue;
2271 links = tmp->state->data;
2272 FOR_EACH_PTR(links, link) {
2273 sm = get_sm_state(compare_id, link, NULL);
2274 if (!sm)
2275 continue;
2276 data = sm->state->data;
2277 if (!data || !data->comparison)
2278 continue;
2279 if (ptr_list_size((struct ptr_list *)data->left_vsl) != 1 ||
2280 ptr_list_size((struct ptr_list *)data->right_vsl) != 1)
2281 continue;
2282 left = first_ptr_list((struct ptr_list *)data->left_vsl);
2283 right = first_ptr_list((struct ptr_list *)data->right_vsl);
2284 if (left->sym == right->sym &&
2285 strcmp(left->var, right->var) == 0)
2286 continue;
2287 /*
2288 * Both parameters link to this comparison so only
2289 * record the first one.
2290 */
2291 if (left->sym != tmp->sym ||
2292 strcmp(left->var, tmp->name) != 0)
2293 continue;
2294
2295 if (strstr(right->var, " orig"))
2296 continue;
2297
2299 right_param = get_param_num_from_sym(right->sym);
2300 if (left_param < 0 || right_param < 0)
2301 continue;
2302
2303 tmp_name = get_param_name_var_sym(left->var, left->sym);
2304 if (!tmp_name)
2305 continue;
2306 snprintf(left_buf, sizeof(left_buf), "%s", tmp_name);
2307
2308 tmp_name = get_param_name_var_sym(right->var, right->sym);
2309 if (!tmp_name || tmp_name[0] != '$')
2310 continue;
2311 snprintf(right_buf, sizeof(right_buf), "$%d%s", right_param, tmp_name + 1);
2312
2313 /*
2314 * FIXME: this should reject $ type variables (as
2315 * opposed to $->foo type). Those should come from
2316 * smatch_param_compare_limit.c.
2317 */
2318
2319 snprintf(info_buf, sizeof(info_buf), "%s %s", show_special(data->comparison), right_buf);
2320 sql_insert_return_states(return_id, return_ranges,
2321 PARAM_COMPARE, left_param, left_buf, info_buf);
2322 } END_FOR_EACH_PTR(link);
2323
2324 } END_FOR_EACH_SM(tmp);
2325 }
2326
2327 static int parse_comparison(char **value, int *op)
2328 {
2329
2330 *op = **value;
2331
2332 switch (*op) {
2333 case '<':
2334 (*value)++;
2335 if (**value == '=') {
2336 (*value)++;
2337 *op = SPECIAL_LTE;
2338 }
2339 break;
2476 if (!left_arg)
2477 return 0;
2478
2479 right_arg = get_argument_from_call_expr(expr->args, right_param);
2480 if (!right_arg)
2481 return 0;
2482
2483 left_name = get_variable_from_key(left_arg, left_key, &left_sym);
2484 right_name = get_variable_from_key(right_arg, right_key, &right_sym);
2485 if (!left_name || !right_name)
2486 goto free;
2487
2488 snprintf(buf, sizeof(buf), "%s vs %s", left_name, right_name);
2489 state = get_state(compare_id, buf, NULL);
2490 if (!state)
2491 goto free;
2492 state_op = state_to_comparison(state);
2493 if (!state_op)
2494 goto free;
2495
2496 if (!filter_comparison(remove_unsigned_from_comparison(state_op), op))
2497 ret = 1;
2498 free:
2499 free_string(left_name);
2500 free_string(right_name);
2501 return ret;
2502 }
2503
2504 int impossibly_high_comparison(struct expression *expr)
2505 {
2506 struct smatch_state *link_state;
2507 struct sm_state *sm;
2508 struct string_list *links;
2509 char *link;
2510 struct compare_data *data;
2511
2512 link_state = get_state_expr(link_id, expr);
2513 if (!link_state) {
2514 if (expr->type == EXPR_BINOP &&
2515 (impossibly_high_comparison(expr->left) ||
2516 impossibly_high_comparison(expr->right)))
2574 }
2575
2576 void register_comparison_inc_dec(int id)
2577 {
2578 inc_dec_id = id;
2579 add_modification_hook_late(inc_dec_id, &iter_modify);
2580 }
2581
2582 void register_comparison_inc_dec_links(int id)
2583 {
2584 inc_dec_link_id = id;
2585 set_dynamic_states(inc_dec_link_id);
2586 set_up_link_functions(inc_dec_id, inc_dec_link_id);
2587 }
2588
2589 static void filter_by_sm(struct sm_state *sm, int op,
2590 struct state_list **true_stack,
2591 struct state_list **false_stack)
2592 {
2593 struct compare_data *data;
2594 int istrue = 0;
2595 int isfalse = 0;
2596
2597 if (!sm)
2598 return;
2599 data = sm->state->data;
2600 if (!data) {
2601 if (sm->merged) {
2602 filter_by_sm(sm->left, op, true_stack, false_stack);
2603 filter_by_sm(sm->right, op, true_stack, false_stack);
2604 }
2605 return;
2606 }
2607
2608 if (data->comparison &&
2609 data->comparison == filter_comparison(data->comparison, op))
2610 istrue = 1;
2611
2612 if (data->comparison &&
2613 data->comparison == filter_comparison(data->comparison, negate_comparison(op)))
2614 isfalse = 1;
2615
2616 if (istrue)
2617 add_ptr_list(true_stack, sm);
2618 if (isfalse)
2619 add_ptr_list(false_stack, sm);
2620
2621 if (sm->merged) {
2622 filter_by_sm(sm->left, op, true_stack, false_stack);
2623 filter_by_sm(sm->right, op, true_stack, false_stack);
2624 }
2625 }
2626
2627 struct sm_state *comparison_implication_hook(struct expression *expr,
2628 struct state_list **true_stack,
2629 struct state_list **false_stack)
2630 {
2631 struct sm_state *sm;
2632 char *left, *right;
2633 int op;
2634 static char buf[256];
2635
2636 if (expr->type != EXPR_COMPARE)
2637 return NULL;
2638
2639 op = expr->op;
2640
2641 left = expr_to_var(expr->left);
2642 right = expr_to_var(expr->right);
2643 if (!left || !right) {
2644 free_string(left);
2648
2649 if (strcmp(left, right) > 0) {
2650 char *tmp = left;
2651
2652 left = right;
2653 right = tmp;
2654 op = flip_comparison(op);
2655 }
2656
2657 snprintf(buf, sizeof(buf), "%s vs %s", left, right);
2658 sm = get_sm_state(compare_id, buf, NULL);
2659 if (!sm)
2660 return NULL;
2661 if (!sm->merged)
2662 return NULL;
2663
2664 filter_by_sm(sm, op, true_stack, false_stack);
2665 if (!*true_stack && !*false_stack)
2666 return NULL;
2667
2668 if (option_debug)
2669 sm_msg("implications from comparison: (%s)", show_sm(sm));
2670
2671 return sm;
2672 }
|
40 static void add_comparison(struct expression *left, int comparison, struct expression *right);
41
42 /* for handling for loops */
43 STATE(start);
44 STATE(incremented);
45
46 ALLOCATOR(compare_data, "compare data");
47
48 static struct symbol *vsl_to_sym(struct var_sym_list *vsl)
49 {
50 struct var_sym *vs;
51
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;
58 }
59
60 static const char *show_comparison(int comparison)
61 {
62 if (comparison == IMPOSSIBLE_COMPARISON)
63 return "impossible";
64 if (comparison == UNKNOWN_COMPARISON)
65 return "unknown";
66 return show_special(comparison);
67 }
68
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)
75 {
76 struct smatch_state *state;
77 struct compare_data *data;
78
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;
91 }
92
93 int state_to_comparison(struct smatch_state *state)
94 {
95 if (!state || !state->data)
96 return UNKNOWN_COMPARISON;
97 return ((struct compare_data *)state->data)->comparison;
98 }
99
100 /*
101 * flip_comparison() reverses the op left and right. So "x >= y" becomes "y <= x".
102 */
103 int flip_comparison(int op)
104 {
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;
133 }
134 }
135
136 int negate_comparison(int op)
137 {
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;
166 }
167 }
168
169 static int rl_comparison(struct range_list *left_rl, struct range_list *right_rl)
170 {
171 sval_t left_min, left_max, right_min, right_max;
172 struct symbol *type = &int_ctype;
173
174 if (!left_rl || !right_rl)
175 return UNKNOWN_COMPARISON;
176
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);
181
182 left_rl = cast_rl(type, left_rl);
183 right_rl = cast_rl(type, right_rl);
184
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);
189
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;
194
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;
203
204 return UNKNOWN_COMPARISON;
205 }
206
207 static int comparison_from_extra(struct expression *a, struct expression *b)
208 {
209 struct range_list *left, *right;
210
211 if (!get_implied_rl(a, &left))
212 return UNKNOWN_COMPARISON;
213 if (!get_implied_rl(b, &right))
214 return UNKNOWN_COMPARISON;
215
216 return rl_comparison(left, right);
217 }
218
219 static struct range_list *get_orig_rl(struct var_sym_list *vsl)
220 {
221 struct symbol *sym;
222 struct smatch_state *state;
223
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);
231 }
232
233 static struct smatch_state *unmatched_comparison(struct sm_state *sm)
234 {
235 struct compare_data *data = sm->state->data;
236 struct range_list *left_rl, *right_rl;
237 int op = UNKNOWN_COMPARISON;
238
239 if (!data)
240 return &undefined;
241
242 if (is_impossible_path()) {
243 op = IMPOSSIBLE_COMPARISON;
244 goto alloc;
245 }
246
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;
251
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;
256
257 op = rl_comparison(left_rl, right_rl);
258
259 alloc:
260 return alloc_compare_state(data->left, data->left_var, data->left_vsl,
261 op,
262 data->right, data->right_var, data->right_vsl);
263 }
264
265 /* remove_unsigned_from_comparison() is obviously a hack. */
266 int remove_unsigned_from_comparison(int op)
267 {
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;
279 }
280 }
281
282 /*
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.
285 */
286 int merge_comparisons(int one, int two)
287 {
288 int LT, EQ, GT;
289
290 if (one == UNKNOWN_COMPARISON || two == UNKNOWN_COMPARISON)
291 return UNKNOWN_COMPARISON;
292
293 if (one == IMPOSSIBLE_COMPARISON)
294 return two;
295 if (two == IMPOSSIBLE_COMPARISON)
296 return one;
297
298 one = remove_unsigned_from_comparison(one);
299 two = remove_unsigned_from_comparison(two);
300
301 if (one == two)
302 return one;
303
304 LT = EQ = GT = 0;
305
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:
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;
342 }
343
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;
357 }
358
359 /*
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 '<'.
363 */
364 int combine_comparisons(int left_compare, int right_compare)
365 {
366 int LT, EQ, GT;
367
368 left_compare = remove_unsigned_from_comparison(left_compare);
369 right_compare = remove_unsigned_from_comparison(right_compare);
370
371 LT = EQ = GT = 0;
372
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;
400 return left_compare;
401 case SPECIAL_GTE:
402 GT++;
403 EQ++;
404 break;
405 case '>':
406 GT++;
407 }
408
409 if (LT == 2) {
410 if (EQ == 2)
411 return SPECIAL_LTE;
412 return '<';
413 }
414
415 if (GT == 2) {
416 if (EQ == 2)
417 return SPECIAL_GTE;
418 return '>';
419 }
420 return UNKNOWN_COMPARISON;
421 }
422
423 /*
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 <.
428 */
429 int comparison_intersection(int left_compare, int right_compare)
430 {
431 int LT, GT, EQ, NE, total;
432
433 if (left_compare == IMPOSSIBLE_COMPARISON ||
434 right_compare == IMPOSSIBLE_COMPARISON)
435 return IMPOSSIBLE_COMPARISON;
436
437 left_compare = remove_unsigned_from_comparison(left_compare);
438 right_compare = remove_unsigned_from_comparison(right_compare);
439
440 LT = GT = EQ = NE = total = 0;
441
442 /* Only one side is known. */
443 if (!left_compare)
444 return right_compare;
445 if (!right_compare)
446 return left_compare;
447
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;
477 }
478
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;
508 }
509
510 if (LT == 2) {
511 if (EQ == 2)
512 return SPECIAL_LTE;
513 return '<';
514 }
515
516 if (GT == 2) {
517 if (EQ == 2)
518 return SPECIAL_GTE;
519 return '>';
520 }
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;
535
536 return UNKNOWN_COMPARISON;
537 }
538
539 static void pre_merge_hook(struct sm_state *cur, struct sm_state *other)
540 {
541 struct compare_data *data = cur->state->data;
542 int extra, new;
543 static bool in_recurse;
544
545 if (!data)
546 return;
547
548 if (in_recurse)
549 return;
550 in_recurse = true;
551 extra = comparison_from_extra(data->left, data->right);
552 in_recurse = false;
553 if (!extra)
554 return;
555 new = comparison_intersection(extra, data->comparison);
556 if (new == data->comparison)
557 return;
558
559 set_state(compare_id, cur->name, NULL,
560 alloc_compare_state(data->left, data->left_var, data->left_vsl,
561 new,
562 data->right, data->right_var, data->right_vsl));
563 }
564
565 struct smatch_state *merge_compare_states(struct smatch_state *s1, struct smatch_state *s2)
566 {
567 struct compare_data *data = s1->data;
568 int op;
569
570 if (!data)
571 return &undefined;
572
573 op = merge_comparisons(state_to_comparison(s1), state_to_comparison(s2));
574 return alloc_compare_state(
575 data->left, data->left_var, data->left_vsl,
576 op,
577 data->right, data->right_var, data->right_vsl);
578 }
579
580 static struct smatch_state *alloc_link_state(struct string_list *links)
581 {
582 struct smatch_state *state;
583 static char buf[256];
584 char *tmp;
585 int i;
586
587 state = __alloc_smatch_state(0);
588
589 i = 0;
590 FOR_EACH_PTR(links, tmp) {
591 if (!i++) {
592 snprintf(buf, sizeof(buf), "%s", tmp);
593 } else {
594 append(buf, ", ", sizeof(buf));
595 append(buf, tmp, sizeof(buf));
596 }
597 } END_FOR_EACH_PTR(tmp);
695 case SPECIAL_UNSIGNED_GTE:
696 case '>':
697 case SPECIAL_UNSIGNED_GT:
698 if (preserve)
699 break;
700 new = alloc_compare_state(
701 data->left, data->left_var, data->left_vsl,
702 flip ? '<' : '>',
703 data->right, data->right_var, data->right_vsl);
704 set_state(compare_id, tmp, NULL, new);
705 break;
706 case '<':
707 case SPECIAL_UNSIGNED_LT:
708 new = alloc_compare_state(
709 data->left, data->left_var, data->left_vsl,
710 flip ? SPECIAL_GTE : SPECIAL_LTE,
711 data->right, data->right_var, data->right_vsl);
712 set_state(compare_id, tmp, NULL, new);
713 break;
714 default:
715 new = alloc_compare_state(
716 data->left, data->left_var, data->left_vsl,
717 UNKNOWN_COMPARISON,
718 data->right, data->right_var, data->right_vsl);
719 set_state(compare_id, tmp, NULL, new);
720 }
721 } END_FOR_EACH_PTR(tmp);
722 }
723
724 static void match_dec(struct sm_state *sm, bool preserve)
725 {
726 struct string_list *links;
727 struct smatch_state *state;
728 char *tmp;
729
730 links = sm->state->data;
731
732 FOR_EACH_PTR(links, tmp) {
733 struct compare_data *data;
734 struct smatch_state *new;
735
736 state = get_state(compare_id, tmp, NULL);
737 if (!state || !state->data)
738 continue;
739
740 data = state->data;
741
742 switch (state_to_comparison(state)) {
743 case SPECIAL_EQUAL:
744 case SPECIAL_LTE:
745 case SPECIAL_UNSIGNED_LTE:
746 case '<':
747 case SPECIAL_UNSIGNED_LT: {
748 if (preserve)
749 break;
750
751 new = alloc_compare_state(
752 data->left, data->left_var, data->left_vsl,
753 '<',
754 data->right, data->right_var, data->right_vsl);
755 set_state(compare_id, tmp, NULL, new);
756 break;
757 }
758 default:
759 new = alloc_compare_state(
760 data->left, data->left_var, data->left_vsl,
761 UNKNOWN_COMPARISON,
762 data->right, data->right_var, data->right_vsl);
763 set_state(compare_id, tmp, NULL, new);
764 }
765 } END_FOR_EACH_PTR(tmp);
766 }
767
768 static void reset_sm(struct sm_state *sm)
769 {
770 struct string_list *links;
771 char *tmp;
772
773 links = sm->state->data;
774
775 FOR_EACH_PTR(links, tmp) {
776 struct smatch_state *old, *new;
777
778 old = get_state(compare_id, tmp, NULL);
779 if (!old || !old->data) {
780 new = &undefined;
781 } else {
782 struct compare_data *data = old->data;
783
784 new = alloc_compare_state(
785 data->left, data->left_var, data->left_vsl,
786 UNKNOWN_COMPARISON,
787 data->right, data->right_var, data->right_vsl);
788 }
789 set_state(compare_id, tmp, NULL, new);
790 } END_FOR_EACH_PTR(tmp);
791 set_state(link_id, sm->name, sm->sym, &undefined);
792 }
793
794 static bool match_add_sub_assign(struct sm_state *sm, struct expression *expr)
795 {
796 struct range_list *rl;
797 sval_t zero = { .type = &int_ctype };
798
799 if (!expr || expr->type != EXPR_ASSIGNMENT)
800 return false;
801 if (expr->op != SPECIAL_ADD_ASSIGN && expr->op != SPECIAL_SUB_ASSIGN)
802 return false;
803
804 get_absolute_rl(expr->right, &rl);
805 if (sval_is_negative(rl_min(rl))) {
806 reset_sm(sm);
807 return false;
808 }
809
879 *
880 */
881
882 if (expr->type != EXPR_PREOP ||
883 (expr->op != SPECIAL_INCREMENT && expr->op != SPECIAL_DECREMENT))
884 return;
885
886 parent = expr_get_parent_expr(expr);
887 if (!parent)
888 return;
889 if (parent->type != EXPR_COMPARE || parent->op != SPECIAL_EQUAL)
890 return;
891 if (parent->left != expr)
892 return;
893
894 if (!get_implied_rl(expr->unop, &left) ||
895 !get_implied_rl(parent->right, &right))
896 return;
897
898 op = rl_comparison(left, right);
899 if (op == UNKNOWN_COMPARISON)
900 return;
901
902 add_comparison(expr->unop, op, parent->right);
903 }
904
905 static char *chunk_to_var_sym(struct expression *expr, struct symbol **sym)
906 {
907 expr = strip_expr(expr);
908 if (!expr)
909 return NULL;
910 if (sym)
911 *sym = NULL;
912
913 if (expr->type == EXPR_PREOP &&
914 (expr->op == SPECIAL_INCREMENT ||
915 expr->op == SPECIAL_DECREMENT))
916 expr = strip_expr(expr->unop);
917
918 if (expr->type == EXPR_CALL) {
919 char buf[64];
1046 continue;
1047 data = state->data;
1048 right_comparison = data->comparison;
1049 right_expr = data->right;
1050 right_var = data->right_var;
1051 right_vsl = data->right_vsl;
1052 if (strcmp(mid_var, right_var) == 0) {
1053 right_expr = data->left;
1054 right_var = data->left_var;
1055 right_vsl = data->left_vsl;
1056 right_comparison = flip_comparison(right_comparison);
1057 }
1058 if (have_common_var_sym(left_vsl, right_vsl))
1059 continue;
1060
1061 orig_comparison = get_orig_comparison(pre_stree, left_var, right_var);
1062
1063 true_comparison = combine_comparisons(left_comparison, right_comparison);
1064 false_comparison = combine_comparisons(left_false_comparison, right_comparison);
1065
1066 true_comparison = comparison_intersection(orig_comparison, true_comparison);
1067 false_comparison = comparison_intersection(orig_comparison, false_comparison);
1068
1069 if (strcmp(left_var, right_var) > 0) {
1070 struct expression *tmp_expr = left_expr;
1071 const char *tmp_var = left_var;
1072 struct var_sym_list *tmp_vsl = left_vsl;
1073
1074 left_expr = right_expr;
1075 left_var = right_var;
1076 left_vsl = right_vsl;
1077 right_expr = tmp_expr;
1078 right_var = tmp_var;
1079 right_vsl = tmp_vsl;
1080 true_comparison = flip_comparison(true_comparison);
1081 false_comparison = flip_comparison(false_comparison);
1082 }
1083
1084 if (!true_comparison && !false_comparison)
1085 continue;
1086
1087 if (true_comparison)
1306 add_var_sym(&right_vsl, right, right_sym);
1307 else
1308 right_vsl = expr_to_vsl(right_expr);
1309
1310 if (strcmp(left, right) > 0) {
1311 char *tmp_name = left;
1312 struct var_sym_list *tmp_vsl = left_vsl;
1313 struct expression *tmp_expr = left_expr;
1314
1315 left = right;
1316 left_vsl = right_vsl;
1317 left_expr = right_expr;
1318 right = tmp_name;
1319 right_vsl = tmp_vsl;
1320 right_expr = tmp_expr;
1321 op = flip_comparison(op);
1322 false_op = flip_comparison(false_op);
1323 }
1324
1325 orig_comparison = get_comparison(left_expr, right_expr);
1326 op = comparison_intersection(orig_comparison, op);
1327 false_op = comparison_intersection(orig_comparison, false_op);
1328
1329 snprintf(state_name, sizeof(state_name), "%s vs %s", left, right);
1330 true_state = alloc_compare_state(
1331 left_expr, left, left_vsl,
1332 op,
1333 right_expr, right, right_vsl);
1334 false_state = alloc_compare_state(
1335 left_expr, left, left_vsl,
1336 false_op,
1337 right_expr, right, right_vsl);
1338
1339 pre_stree = clone_stree(__get_cur_stree());
1340 update_tf_data(pre_stree, left_expr, left, left_vsl, right_expr, right, right_vsl, op, false_op);
1341 free_stree(&pre_stree);
1342
1343 set_true_false_states(compare_id, state_name, NULL, true_state, false_state);
1344 __compare_param_limit_hook(left_expr, right_expr, state_name, true_state, false_state);
1345 save_link(left_expr, state_name);
1346 save_link(right_expr, state_name);
1347
1364 if (expr->type != EXPR_COMPARE)
1365 return;
1366
1367 handle_comparison(expr->left, expr->op, expr->right, &state_name, &false_state);
1368 if (false_state && state_name)
1369 handle_for_loops(expr, state_name, false_state);
1370
1371 left = strip_parens(expr->left);
1372 right = strip_parens(expr->right);
1373
1374 if (left->type == EXPR_BINOP && left->op == '+') {
1375 new_left = left->left;
1376 new_right = binop_expression(right, '-', left->right);
1377 handle_comparison(new_left, expr->op, new_right, NULL, NULL);
1378
1379 new_left = left->right;
1380 new_right = binop_expression(right, '-', left->left);
1381 handle_comparison(new_left, expr->op, new_right, NULL, NULL);
1382 }
1383
1384 redo = 0;
1385 left = strip_parens(expr->left);
1386 right = strip_parens(expr->right);
1387 if (get_last_expr_from_expression_stmt(expr->left)) {
1388 left = get_last_expr_from_expression_stmt(expr->left);
1389 redo = 1;
1390 }
1391 if (get_last_expr_from_expression_stmt(expr->right)) {
1392 right = get_last_expr_from_expression_stmt(expr->right);
1393 redo = 1;
1394 }
1395
1396 if (!redo)
1397 return;
1398
1399 count = 0;
1400 while ((tmp = get_assigned_expr(left))) {
1401 if (count++ > 3)
1402 break;
1403 left = strip_expr(tmp);
1647
1648 if (is_self_assign(expr))
1649 return;
1650
1651 copy_comparisons(expr->left, expr->right);
1652 add_comparison(expr->left, SPECIAL_EQUAL, expr->right);
1653
1654 right = strip_expr(expr->right);
1655 if (right->type == EXPR_BINOP)
1656 match_binop_assign(expr);
1657 }
1658
1659 int get_comparison_strings(const char *one, const char *two)
1660 {
1661 char buf[256];
1662 struct smatch_state *state;
1663 int invert = 0;
1664 int ret = 0;
1665
1666 if (!one || !two)
1667 return UNKNOWN_COMPARISON;
1668
1669 if (strcmp(one, two) == 0)
1670 return SPECIAL_EQUAL;
1671
1672 if (strcmp(one, two) > 0) {
1673 const char *tmp = one;
1674
1675 one = two;
1676 two = tmp;
1677 invert = 1;
1678 }
1679
1680 snprintf(buf, sizeof(buf), "%s vs %s", one, two);
1681 state = get_state(compare_id, buf, NULL);
1682 if (state)
1683 ret = state_to_comparison(state);
1684
1685 if (invert)
1686 ret = flip_comparison(ret);
1687
1688 return ret;
1689 }
1690
1691 static int get_comparison_helper(struct expression *a, struct expression *b, bool use_extra)
1692 {
1693 char *one = NULL;
1694 char *two = NULL;
1695 int ret = UNKNOWN_COMPARISON;
1696 int extra = UNKNOWN_COMPARISON;
1697
1698 if (a == UNKNOWN_COMPARISON ||
1699 b == UNKNOWN_COMPARISON)
1700 return UNKNOWN_COMPARISON;
1701
1702 a = strip_parens(a);
1703 b = strip_parens(b);
1704
1705 move_plus_to_minus(&a, &b);
1706
1707 one = chunk_to_var(a);
1708 if (!one)
1709 goto free;
1710 two = chunk_to_var(b);
1711 if (!two)
1712 goto free;
1713
1714 ret = get_comparison_strings(one, two);
1715 if (ret)
1716 goto free;
1717
1718 if (is_plus_one(a) || is_minus_one(a)) {
1719 free_string(one);
1720 one = chunk_to_var(a->left);
1721 ret = get_comparison_strings(one, two);
1722 } else if (is_plus_one(b) || is_minus_one(b)) {
1723 free_string(two);
1724 two = chunk_to_var(b->left);
1725 ret = get_comparison_strings(one, two);
1726 }
1727
1728 if (ret == UNKNOWN_COMPARISON)
1729 goto free;
1730
1731 if ((is_plus_one(a) || is_minus_one(b)) && ret == '<')
1732 ret = SPECIAL_LTE;
1733 else if ((is_minus_one(a) || is_plus_one(b)) && ret == '>')
1734 ret = SPECIAL_GTE;
1735 else
1736 ret = UNKNOWN_COMPARISON;
1737
1738 free:
1739 free_string(one);
1740 free_string(two);
1741
1742 extra = comparison_from_extra(a, b);
1743 return comparison_intersection(ret, extra);
1744 }
1745
1746 int get_comparison(struct expression *a, struct expression *b)
1747 {
1748 return get_comparison_helper(a, b, true);
1749 }
1750
1751 int get_comparison_no_extra(struct expression *a, struct expression *b)
1752 {
1753 return get_comparison_helper(a, b, false);
1754 }
1755
1756 int possible_comparison(struct expression *a, int comparison, struct expression *b)
1757 {
1758 char *one = NULL;
1759 char *two = NULL;
1760 int ret = 0;
1761 char buf[256];
1762 struct sm_state *sm;
1763 int saved;
1935 if (strcmp(var, right_var) == 0) {
1936 expr = data->left;
1937 var = data->left_var;
1938 vsl = data->left_vsl;
1939 comparison = flip_comparison(comparison);
1940 }
1941 comparison = combine_comparisons(left_compare, comparison);
1942 if (!comparison)
1943 continue;
1944 add_comparison_var_sym(left, left_var, left_vsl, comparison, expr, var, vsl);
1945 } END_FOR_EACH_PTR(tmp);
1946
1947 done:
1948 free_string(right_var);
1949 }
1950
1951 void __add_return_comparison(struct expression *call, const char *range)
1952 {
1953 struct expression *arg;
1954 int comparison;
1955 char buf[16];
1956
1957 if (!str_to_comparison_arg(range, call, &comparison, &arg))
1958 return;
1959 snprintf(buf, sizeof(buf), "%s", show_comparison(comparison));
1960 update_links_from_call(call, comparison, arg);
1961 add_comparison(call, comparison, arg);
1962 }
1963
1964 void __add_comparison_info(struct expression *expr, struct expression *call, const char *range)
1965 {
1966 copy_comparisons(expr, call);
1967 }
1968
1969 static char *get_mask_comparison(struct expression *expr, int ignore)
1970 {
1971 struct expression *tmp, *right;
1972 int count, param;
1973 char buf[256];
1974
1975 /* The return value for "return foo & param;" is <= param */
1976
1977 count = 0;
1978 while ((tmp = get_assigned_expr(expr))) {
1979 expr = strip_expr(tmp);
2001 char *ret_str = NULL;
2002 int compare;
2003 int i;
2004
2005 if (!expr)
2006 return NULL;
2007
2008 var = chunk_to_var(expr);
2009 if (!var)
2010 goto try_mask;
2011
2012 i = -1;
2013 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, param) {
2014 i++;
2015 if (i == ignore)
2016 continue;
2017 if (!param->ident)
2018 continue;
2019 snprintf(buf, sizeof(buf), "%s orig", param->ident->name);
2020 compare = get_comparison_strings(var, buf);
2021 if (compare == UNKNOWN_COMPARISON ||
2022 compare == IMPOSSIBLE_COMPARISON)
2023 continue;
2024 if (show_comparison(compare)[0] != starts_with)
2025 continue;
2026 snprintf(buf, sizeof(buf), "[%s$%d]", show_comparison(compare), i);
2027 ret_str = alloc_sname(buf);
2028 break;
2029 } END_FOR_EACH_PTR(param);
2030
2031 free_string(var);
2032 if (!ret_str)
2033 goto try_mask;
2034
2035 return ret_str;
2036
2037 try_mask:
2038 if (starts_with == '<')
2039 ret_str = get_mask_comparison(expr, ignore);
2040 return ret_str;
2041 }
2042
2043 char *name_sym_to_param_comparison(const char *name, struct symbol *sym)
2044 {
2045 struct symbol *param;
2046 char buf[256];
2047 int compare;
2048 int i;
2049
2050 i = -1;
2051 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, param) {
2052 i++;
2053 if (!param->ident)
2054 continue;
2055 snprintf(buf, sizeof(buf), "%s orig", param->ident->name);
2056 compare = get_comparison_strings(name, buf);
2057 if (compare == UNKNOWN_COMPARISON ||
2058 compare == IMPOSSIBLE_COMPARISON)
2059 continue;
2060 snprintf(buf, sizeof(buf), "[%s$%d]", show_comparison(compare), i);
2061 return alloc_sname(buf);
2062 } END_FOR_EACH_PTR(param);
2063
2064 return NULL;
2065 }
2066
2067 char *expr_equal_to_param(struct expression *expr, int ignore)
2068 {
2069 return range_comparison_to_param_helper(expr, '=', ignore);
2070 }
2071
2072 char *expr_lte_to_param(struct expression *expr, int ignore)
2073 {
2074 return range_comparison_to_param_helper(expr, '<', ignore);
2075 }
2076
2077 char *expr_param_comparison(struct expression *expr, int ignore)
2078 {
2079 struct symbol *param;
2080 char *var = NULL;
2081 char buf[256];
2082 char *ret_str = NULL;
2083 int compare;
2084 int i;
2085
2086 var = chunk_to_var(expr);
2087 if (!var)
2088 goto free;
2089
2090 i = -1;
2091 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, param) {
2092 i++;
2093 if (i == ignore)
2094 continue;
2095 if (!param->ident)
2096 continue;
2097 snprintf(buf, sizeof(buf), "%s orig", param->ident->name);
2098 compare = get_comparison_strings(var, buf);
2099 if (!compare)
2100 continue;
2101 snprintf(buf, sizeof(buf), "[%s$%d]", show_comparison(compare), i);
2102 ret_str = alloc_sname(buf);
2103 break;
2104 } END_FOR_EACH_PTR(param);
2105
2106 free:
2107 free_string(var);
2108 return ret_str;
2109 }
2110
2111 char *get_printed_param_name(struct expression *call, const char *param_name, struct symbol *param_sym)
2112 {
2113 struct expression *arg;
2114 char *name;
2115 struct symbol *sym;
2116 static char buf[256];
2117 int len;
2118 int i;
2119
2120 i = -1;
2121 FOR_EACH_PTR(call->args, arg) {
2160 i = -1;
2161 FOR_EACH_PTR(expr->args, arg) {
2162 i++;
2163
2164 state = get_state_chunk(link_id, arg);
2165 if (!state)
2166 continue;
2167
2168 links = state->data;
2169 FOR_EACH_PTR(links, link) {
2170 struct var_sym_list *right_vsl;
2171 struct var_sym *right_vs;
2172
2173
2174 if (strstr(link, " orig"))
2175 continue;
2176 sm = get_sm_state(compare_id, link, NULL);
2177 if (!sm)
2178 continue;
2179 data = sm->state->data;
2180 if (!data ||
2181 data->comparison == UNKNOWN_COMPARISON ||
2182 data->comparison == IMPOSSIBLE_COMPARISON)
2183 continue;
2184 arg_name = expr_to_var(arg);
2185 if (!arg_name)
2186 continue;
2187
2188 right_vsl = NULL;
2189 if (strcmp(data->left_var, arg_name) == 0) {
2190 comparison = data->comparison;
2191 right_name = data->right_var;
2192 right_vsl = data->right_vsl;
2193 } else if (strcmp(data->right_var, arg_name) == 0) {
2194 comparison = flip_comparison(data->comparison);
2195 right_name = data->left_var;
2196 right_vsl = data->left_vsl;
2197 }
2198 if (!right_vsl || ptr_list_size((struct ptr_list *)right_vsl) != 1)
2199 goto free;
2200
2201 right_vs = first_ptr_list((struct ptr_list *)right_vsl);
2202 if (strcmp(right_vs->var, right_name) != 0)
2203 goto free;
2204 right_name = get_printed_param_name(expr, right_vs->var, right_vs->sym);
2205 if (!right_name)
2206 goto free;
2207 snprintf(info_buf, sizeof(info_buf), "%s %s", show_comparison(comparison), right_name);
2208 sql_insert_caller_info(expr, PARAM_COMPARE, i, "$", info_buf);
2209
2210 free:
2211 free_string(arg_name);
2212 } END_FOR_EACH_PTR(link);
2213 } END_FOR_EACH_PTR(arg);
2214 }
2215
2216 static void struct_member_callback(struct expression *call, int param, char *printed_name, struct sm_state *link_sm)
2217 {
2218 struct sm_state *compare_sm;
2219 struct string_list *links;
2220 char *link;
2221 struct compare_data *data;
2222 struct var_sym *left, *right;
2223 static char info_buf[256];
2224 const char *right_name;
2225
2226 if (strstr(printed_name, " orig"))
2227 return;
2237
2238 if (ptr_list_size((struct ptr_list *)data->left_vsl) != 1 ||
2239 ptr_list_size((struct ptr_list *)data->right_vsl) != 1)
2240 continue;
2241 left = first_ptr_list((struct ptr_list *)data->left_vsl);
2242 right = first_ptr_list((struct ptr_list *)data->right_vsl);
2243 if (left->sym == right->sym &&
2244 strcmp(left->var, right->var) == 0)
2245 continue;
2246 /*
2247 * Both parameters link to this comparison so only
2248 * record the first one.
2249 */
2250 if (left->sym != link_sm->sym ||
2251 strcmp(left->var, link_sm->name) != 0)
2252 continue;
2253
2254 right_name = get_printed_param_name(call, right->var, right->sym);
2255 if (!right_name)
2256 continue;
2257 snprintf(info_buf, sizeof(info_buf), "%s %s", show_comparison(data->comparison), right_name);
2258 sql_insert_caller_info(call, PARAM_COMPARE, param, printed_name, info_buf);
2259 } END_FOR_EACH_PTR(link);
2260 }
2261
2262 static void print_return_value_comparison(int return_id, char *return_ranges, struct expression *expr)
2263 {
2264 char *name;
2265 const char *tmp_name;
2266 struct symbol *sym;
2267 int param;
2268 char info_buf[256];
2269
2270 /*
2271 * TODO: This only prints == comparisons. That's probably the most
2272 * useful comparison because == max has lots of implications. But it
2273 * would be good to capture the rest as well.
2274 *
2275 * This information is already in the DB but it's in the parameter math
2276 * bits and it's awkward to use it. This is is the simpler, possibly
2277 * cleaner way, but not necessarily the best, I don't know.
2308 struct sm_state *sm;
2309 struct compare_data *data;
2310 struct var_sym *left, *right;
2311 int left_param, right_param;
2312 char left_buf[256];
2313 char right_buf[256];
2314 char info_buf[258];
2315 const char *tmp_name;
2316
2317 print_return_value_comparison(return_id, return_ranges, expr);
2318
2319 FOR_EACH_MY_SM(link_id, __get_cur_stree(), tmp) {
2320 if (get_param_num_from_sym(tmp->sym) < 0)
2321 continue;
2322 links = tmp->state->data;
2323 FOR_EACH_PTR(links, link) {
2324 sm = get_sm_state(compare_id, link, NULL);
2325 if (!sm)
2326 continue;
2327 data = sm->state->data;
2328 if (!data ||
2329 data->comparison == UNKNOWN_COMPARISON ||
2330 data->comparison == IMPOSSIBLE_COMPARISON)
2331 continue;
2332 if (ptr_list_size((struct ptr_list *)data->left_vsl) != 1 ||
2333 ptr_list_size((struct ptr_list *)data->right_vsl) != 1)
2334 continue;
2335 left = first_ptr_list((struct ptr_list *)data->left_vsl);
2336 right = first_ptr_list((struct ptr_list *)data->right_vsl);
2337 if (left->sym == right->sym &&
2338 strcmp(left->var, right->var) == 0)
2339 continue;
2340 /*
2341 * Both parameters link to this comparison so only
2342 * record the first one.
2343 */
2344 if (left->sym != tmp->sym ||
2345 strcmp(left->var, tmp->name) != 0)
2346 continue;
2347
2348 if (strstr(right->var, " orig"))
2349 continue;
2350
2352 right_param = get_param_num_from_sym(right->sym);
2353 if (left_param < 0 || right_param < 0)
2354 continue;
2355
2356 tmp_name = get_param_name_var_sym(left->var, left->sym);
2357 if (!tmp_name)
2358 continue;
2359 snprintf(left_buf, sizeof(left_buf), "%s", tmp_name);
2360
2361 tmp_name = get_param_name_var_sym(right->var, right->sym);
2362 if (!tmp_name || tmp_name[0] != '$')
2363 continue;
2364 snprintf(right_buf, sizeof(right_buf), "$%d%s", right_param, tmp_name + 1);
2365
2366 /*
2367 * FIXME: this should reject $ type variables (as
2368 * opposed to $->foo type). Those should come from
2369 * smatch_param_compare_limit.c.
2370 */
2371
2372 snprintf(info_buf, sizeof(info_buf), "%s %s", show_comparison(data->comparison), right_buf);
2373 sql_insert_return_states(return_id, return_ranges,
2374 PARAM_COMPARE, left_param, left_buf, info_buf);
2375 } END_FOR_EACH_PTR(link);
2376
2377 } END_FOR_EACH_SM(tmp);
2378 }
2379
2380 static int parse_comparison(char **value, int *op)
2381 {
2382
2383 *op = **value;
2384
2385 switch (*op) {
2386 case '<':
2387 (*value)++;
2388 if (**value == '=') {
2389 (*value)++;
2390 *op = SPECIAL_LTE;
2391 }
2392 break;
2529 if (!left_arg)
2530 return 0;
2531
2532 right_arg = get_argument_from_call_expr(expr->args, right_param);
2533 if (!right_arg)
2534 return 0;
2535
2536 left_name = get_variable_from_key(left_arg, left_key, &left_sym);
2537 right_name = get_variable_from_key(right_arg, right_key, &right_sym);
2538 if (!left_name || !right_name)
2539 goto free;
2540
2541 snprintf(buf, sizeof(buf), "%s vs %s", left_name, right_name);
2542 state = get_state(compare_id, buf, NULL);
2543 if (!state)
2544 goto free;
2545 state_op = state_to_comparison(state);
2546 if (!state_op)
2547 goto free;
2548
2549 if (!comparison_intersection(remove_unsigned_from_comparison(state_op), op))
2550 ret = 1;
2551 free:
2552 free_string(left_name);
2553 free_string(right_name);
2554 return ret;
2555 }
2556
2557 int impossibly_high_comparison(struct expression *expr)
2558 {
2559 struct smatch_state *link_state;
2560 struct sm_state *sm;
2561 struct string_list *links;
2562 char *link;
2563 struct compare_data *data;
2564
2565 link_state = get_state_expr(link_id, expr);
2566 if (!link_state) {
2567 if (expr->type == EXPR_BINOP &&
2568 (impossibly_high_comparison(expr->left) ||
2569 impossibly_high_comparison(expr->right)))
2627 }
2628
2629 void register_comparison_inc_dec(int id)
2630 {
2631 inc_dec_id = id;
2632 add_modification_hook_late(inc_dec_id, &iter_modify);
2633 }
2634
2635 void register_comparison_inc_dec_links(int id)
2636 {
2637 inc_dec_link_id = id;
2638 set_dynamic_states(inc_dec_link_id);
2639 set_up_link_functions(inc_dec_id, inc_dec_link_id);
2640 }
2641
2642 static void filter_by_sm(struct sm_state *sm, int op,
2643 struct state_list **true_stack,
2644 struct state_list **false_stack)
2645 {
2646 struct compare_data *data;
2647 int is_true = 0;
2648 int is_false = 0;
2649
2650 if (!sm)
2651 return;
2652 data = sm->state->data;
2653 if (!data || data->comparison == UNKNOWN_COMPARISON)
2654 goto split;
2655 if (data->comparison == IMPOSSIBLE_COMPARISON)
2656 return;
2657
2658 /*
2659 * We want to check that "data->comparison" is totally inside "op". So
2660 * if data->comparison is < and op is <= then that's true. Or if
2661 * data->comparison is == and op is <= then that's true. But if
2662 * data->comparison is <= and op is < than that's neither true nor
2663 * false.
2664 */
2665 if (data->comparison == comparison_intersection(data->comparison, op))
2666 is_true = 1;
2667 if (data->comparison == comparison_intersection(data->comparison, negate_comparison(op)))
2668 is_false = 1;
2669
2670 if (debug_implied()) {
2671 sm_msg("%s: %s: op = '%s' negated '%s'. true_intersect = '%s' false_insersect = '%s' sm = '%s'",
2672 __func__,
2673 sm->state->name,
2674 alloc_sname(show_comparison(op)),
2675 alloc_sname(show_comparison(negate_comparison(op))),
2676 alloc_sname(show_comparison(comparison_intersection(data->comparison, op))),
2677 alloc_sname(show_comparison(comparison_intersection(data->comparison, negate_comparison(op)))),
2678 show_sm(sm));
2679 }
2680
2681 if (is_true)
2682 add_ptr_list(true_stack, sm);
2683 if (is_false)
2684 add_ptr_list(false_stack, sm);
2685 split:
2686 filter_by_sm(sm->left, op, true_stack, false_stack);
2687 filter_by_sm(sm->right, op, true_stack, false_stack);
2688 }
2689
2690 struct sm_state *comparison_implication_hook(struct expression *expr,
2691 struct state_list **true_stack,
2692 struct state_list **false_stack)
2693 {
2694 struct sm_state *sm;
2695 char *left, *right;
2696 int op;
2697 static char buf[256];
2698
2699 if (expr->type != EXPR_COMPARE)
2700 return NULL;
2701
2702 op = expr->op;
2703
2704 left = expr_to_var(expr->left);
2705 right = expr_to_var(expr->right);
2706 if (!left || !right) {
2707 free_string(left);
2711
2712 if (strcmp(left, right) > 0) {
2713 char *tmp = left;
2714
2715 left = right;
2716 right = tmp;
2717 op = flip_comparison(op);
2718 }
2719
2720 snprintf(buf, sizeof(buf), "%s vs %s", left, right);
2721 sm = get_sm_state(compare_id, buf, NULL);
2722 if (!sm)
2723 return NULL;
2724 if (!sm->merged)
2725 return NULL;
2726
2727 filter_by_sm(sm, op, true_stack, false_stack);
2728 if (!*true_stack && !*false_stack)
2729 return NULL;
2730
2731 if (debug_implied())
2732 sm_msg("implications from comparison: (%s)", show_sm(sm));
2733
2734 return sm;
2735 }
|