db: Use smatch_data/db/<project>.return_fixes on the mem DB
[smatch.git] / smatch_math.c
blob7180f2a4b21207906547257ee4d6e6bb5586902b
1 /*
2 * Copyright (C) 2010 Dan Carpenter.
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
18 #include "symbol.h"
19 #include "smatch.h"
20 #include "smatch_slist.h"
21 #include "smatch_extra.h"
23 static struct range_list *_get_rl(struct expression *expr, int implied, int *recurse_cnt);
24 static struct range_list *handle_variable(struct expression *expr, int implied, int *recurse_cnt);
25 static struct range_list *(*custom_handle_variable)(struct expression *expr);
27 static int get_implied_value_internal(struct expression *expr, sval_t *sval, int *recurse_cnt);
28 static int get_absolute_rl_internal(struct expression *expr, struct range_list **rl, int *recurse_cnt);
30 static sval_t zero = {.type = &int_ctype, {.value = 0} };
31 static sval_t one = {.type = &int_ctype, {.value = 1} };
33 struct range_list *rl_zero(void)
35 return alloc_rl(zero, zero);
38 struct range_list *rl_one(void)
40 return alloc_rl(one, one);
43 enum {
44 RL_EXACT,
45 RL_HARD,
46 RL_FUZZY,
47 RL_IMPLIED,
48 RL_ABSOLUTE,
49 RL_REAL_ABSOLUTE,
52 static struct range_list *last_stmt_rl(struct statement *stmt, int implied, int *recurse_cnt)
54 struct expression *expr;
56 if (!stmt)
57 return NULL;
59 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
60 if (stmt->type == STMT_LABEL) {
61 if (stmt->label_statement &&
62 stmt->label_statement->type == STMT_EXPRESSION)
63 expr = stmt->label_statement->expression;
64 else
65 return NULL;
66 } else if (stmt->type == STMT_EXPRESSION) {
67 expr = stmt->expression;
68 } else {
69 return NULL;
71 return _get_rl(expr, implied, recurse_cnt);
74 static struct range_list *handle_expression_statement_rl(struct expression *expr, int implied, int *recurse_cnt)
76 return last_stmt_rl(get_expression_statement(expr), implied, recurse_cnt);
79 static struct range_list *handle_ampersand_rl(struct expression *expr, int implied, int *recurse_cnt)
81 struct range_list *rl;
83 if (implied == RL_EXACT || implied == RL_HARD)
84 return NULL;
85 if (get_address_rl(expr, &rl))
86 return rl;
87 return alloc_rl(valid_ptr_min_sval, valid_ptr_max_sval);
90 static struct range_list *handle_negate_rl(struct expression *expr, int implied, int *recurse_cnt)
92 if (known_condition_true(expr->unop))
93 return rl_zero();
94 if (known_condition_false(expr->unop))
95 return rl_one();
97 if (implied == RL_EXACT)
98 return NULL;
100 if (implied_condition_true(expr->unop))
101 return rl_zero();
102 if (implied_condition_false(expr->unop))
103 return rl_one();
104 return alloc_rl(zero, one);
107 static struct range_list *handle_bitwise_negate(struct expression *expr, int implied, int *recurse_cnt)
109 struct range_list *rl;
110 sval_t sval;
112 rl = _get_rl(expr->unop, implied, recurse_cnt);
113 if (!rl_to_sval(rl, &sval))
114 return NULL;
115 sval = sval_preop(sval, '~');
116 sval_cast(get_type(expr->unop), sval);
117 return alloc_rl(sval, sval);
120 static struct range_list *handle_minus_preop(struct expression *expr, int implied, int *recurse_cnt)
122 struct range_list *rl;
123 sval_t min, max;
125 rl = _get_rl(expr->unop, implied, recurse_cnt);
126 min = sval_preop(rl_max(rl), '-');
127 max = sval_preop(rl_min(rl), '-');
128 return alloc_rl(min, max);
131 static struct range_list *handle_preop_rl(struct expression *expr, int implied, int *recurse_cnt)
133 switch (expr->op) {
134 case '&':
135 return handle_ampersand_rl(expr, implied, recurse_cnt);
136 case '!':
137 return handle_negate_rl(expr, implied, recurse_cnt);
138 case '~':
139 return handle_bitwise_negate(expr, implied, recurse_cnt);
140 case '-':
141 return handle_minus_preop(expr, implied, recurse_cnt);
142 case '*':
143 return handle_variable(expr, implied, recurse_cnt);
144 case '(':
145 return handle_expression_statement_rl(expr, implied, recurse_cnt);
146 default:
147 return NULL;
151 static struct range_list *handle_divide_rl(struct expression *expr, int implied, int *recurse_cnt)
153 struct range_list *left_rl, *right_rl;
154 struct symbol *type;
156 type = get_type(expr);
158 left_rl = _get_rl(expr->left, implied, recurse_cnt);
159 left_rl = cast_rl(type, left_rl);
160 right_rl = _get_rl(expr->right, implied, recurse_cnt);
161 right_rl = cast_rl(type, right_rl);
163 if (!left_rl || !right_rl)
164 return NULL;
166 if (implied != RL_REAL_ABSOLUTE) {
167 if (is_whole_rl(left_rl) || is_whole_rl(right_rl))
168 return NULL;
171 return rl_binop(left_rl, '/', right_rl);
174 static int handle_offset_subtraction(struct expression *expr)
176 struct expression *left, *right;
177 struct symbol *left_sym, *right_sym;
178 struct symbol *type;
179 int left_offset, right_offset;
181 type = get_type(expr);
184 if (!type || type->type != SYM_PTR)
185 return -1;
186 type = get_real_base_type(type);
187 if (!type || (type_bits(type) != 8 && (type != &void_ctype)))
188 return -1;
190 left = strip_expr(expr->left);
191 right = strip_expr(expr->right);
193 if (left->type != EXPR_PREOP || left->op != '&')
194 return -1;
195 left = strip_expr(left->unop);
197 left_sym = expr_to_sym(left);
198 right_sym = expr_to_sym(right);
199 if (!left_sym || left_sym != right_sym)
200 return -1;
202 left_offset = get_member_offset_from_deref(left);
203 if (right->type == EXPR_SYMBOL)
204 right_offset = 0;
205 else {
206 if (right->type != EXPR_PREOP || right->op != '&')
207 return -1;
208 right = strip_expr(right->unop);
209 right_offset = get_member_offset_from_deref(right);
211 if (left_offset < 0 || right_offset < 0)
212 return -1;
214 return left_offset - right_offset;
217 static struct range_list *handle_subtract_rl(struct expression *expr, int implied, int *recurse_cnt)
219 struct symbol *type;
220 struct range_list *left_rl, *right_rl;
221 sval_t max, min, tmp;
222 int comparison;
223 int offset;
225 type = get_type(expr);
227 offset = handle_offset_subtraction(expr);
228 if (offset >= 0) {
229 tmp.type = type;
230 tmp.value = offset;
232 return alloc_rl(tmp, tmp);
235 comparison = get_comparison(expr->left, expr->right);
237 left_rl = _get_rl(expr->left, implied, recurse_cnt);
238 left_rl = cast_rl(type, left_rl);
239 right_rl = _get_rl(expr->right, implied, recurse_cnt);
240 right_rl = cast_rl(type, right_rl);
242 if ((!left_rl || !right_rl) &&
243 (implied == RL_EXACT || implied == RL_HARD || implied == RL_FUZZY))
244 return NULL;
246 if (!left_rl)
247 left_rl = alloc_whole_rl(type);
248 if (!right_rl)
249 right_rl = alloc_whole_rl(type);
251 /* negative values complicate everything fix this later */
252 if (sval_is_negative(rl_min(right_rl)))
253 return NULL;
254 max = rl_max(left_rl);
256 switch (comparison) {
257 case '>':
258 case SPECIAL_UNSIGNED_GT:
259 min = sval_type_val(type, 1);
260 max = rl_max(left_rl);
261 break;
262 case SPECIAL_GTE:
263 case SPECIAL_UNSIGNED_GTE:
264 min = sval_type_val(type, 0);
265 max = rl_max(left_rl);
266 break;
267 case SPECIAL_EQUAL:
268 min = sval_type_val(type, 0);
269 max = sval_type_val(type, 0);
270 break;
271 case '<':
272 case SPECIAL_UNSIGNED_LT:
273 max = sval_type_val(type, -1);
274 break;
275 case SPECIAL_LTE:
276 case SPECIAL_UNSIGNED_LTE:
277 max = sval_type_val(type, 0);
278 break;
279 default:
280 if (sval_binop_overflows(rl_min(left_rl), '-', rl_max(right_rl)))
281 return NULL;
282 min = sval_type_min(type);
285 if (!sval_binop_overflows(rl_min(left_rl), '-', rl_max(right_rl))) {
286 tmp = sval_binop(rl_min(left_rl), '-', rl_max(right_rl));
287 if (sval_cmp(tmp, min) > 0)
288 min = tmp;
291 if (!sval_is_max(rl_max(left_rl))) {
292 tmp = sval_binop(rl_max(left_rl), '-', rl_min(right_rl));
293 if (sval_cmp(tmp, max) < 0)
294 max = tmp;
297 if (sval_is_min(min) && sval_is_max(max))
298 return NULL;
300 return cast_rl(type, alloc_rl(min, max));
303 static struct range_list *handle_mod_rl(struct expression *expr, int implied, int *recurse_cnt)
305 struct range_list *rl;
306 sval_t left, right, sval;
308 if (implied == RL_EXACT) {
309 if (!get_implied_value(expr->right, &right))
310 return NULL;
311 if (!get_implied_value(expr->left, &left))
312 return NULL;
313 sval = sval_binop(left, '%', right);
314 return alloc_rl(sval, sval);
316 /* if we can't figure out the right side it's probably hopeless */
317 if (!get_implied_value_internal(expr->right, &right, recurse_cnt))
318 return NULL;
320 right = sval_cast(get_type(expr), right);
321 right.value--;
323 rl = _get_rl(expr->left, implied, recurse_cnt);
324 if (rl && rl_max(rl).uvalue < right.uvalue)
325 right.uvalue = rl_max(rl).uvalue;
327 return alloc_rl(sval_cast(right.type, zero), right);
330 static sval_t sval_lowest_set_bit(sval_t sval)
332 int i;
333 int found = 0;
335 for (i = 0; i < 64; i++) {
336 if (sval.uvalue & 1ULL << i) {
337 if (!found++)
338 continue;
339 sval.uvalue &= ~(1ULL << i);
342 return sval;
345 static struct range_list *handle_bitwise_AND(struct expression *expr, int implied, int *recurse_cnt)
347 struct symbol *type;
348 struct range_list *left_rl, *right_rl;
349 sval_t known;
350 int new_recurse;
352 if (implied != RL_IMPLIED && implied != RL_ABSOLUTE && implied != RL_REAL_ABSOLUTE)
353 return NULL;
355 type = get_type(expr);
357 if (get_implied_value_internal(expr->left, &known, recurse_cnt)) {
358 sval_t min;
360 min = sval_lowest_set_bit(known);
361 left_rl = alloc_rl(min, known);
362 left_rl = cast_rl(type, left_rl);
363 add_range(&left_rl, sval_type_val(type, 0), sval_type_val(type, 0));
364 } else {
365 left_rl = _get_rl(expr->left, implied, recurse_cnt);
366 if (left_rl) {
367 left_rl = cast_rl(type, left_rl);
368 left_rl = alloc_rl(sval_type_val(type, 0), rl_max(left_rl));
369 } else {
370 if (implied == RL_HARD)
371 return NULL;
372 left_rl = alloc_whole_rl(type);
376 new_recurse = *recurse_cnt;
377 if (*recurse_cnt >= 200)
378 new_recurse = 100; /* Let's try super hard to get the mask */
379 if (get_implied_value_internal(expr->right, &known, &new_recurse)) {
380 sval_t min, left_max, mod;
382 *recurse_cnt = new_recurse;
384 min = sval_lowest_set_bit(known);
385 right_rl = alloc_rl(min, known);
386 right_rl = cast_rl(type, right_rl);
387 add_range(&right_rl, sval_type_val(type, 0), sval_type_val(type, 0));
389 if (min.value != 0) {
390 left_max = rl_max(left_rl);
391 mod = sval_binop(left_max, '%', min);
392 if (mod.value) {
393 left_max = sval_binop(left_max, '-', mod);
394 left_max.value++;
395 if (left_max.value > 0 && sval_cmp(left_max, rl_max(left_rl)) < 0)
396 left_rl = remove_range(left_rl, left_max, rl_max(left_rl));
399 } else {
400 right_rl = _get_rl(expr->right, implied, recurse_cnt);
401 if (right_rl) {
402 right_rl = cast_rl(type, right_rl);
403 right_rl = alloc_rl(sval_type_val(type, 0), rl_max(right_rl));
404 } else {
405 if (implied == RL_HARD)
406 return NULL;
407 right_rl = alloc_whole_rl(type);
411 return rl_intersection(left_rl, right_rl);
414 static struct range_list *use_rl_binop(struct expression *expr, int implied, int *recurse_cnt)
416 struct symbol *type;
417 struct range_list *left_rl, *right_rl;
419 if (implied != RL_IMPLIED && implied != RL_ABSOLUTE && implied != RL_REAL_ABSOLUTE)
420 return NULL;
422 type = get_type(expr);
424 get_absolute_rl_internal(expr->left, &left_rl, recurse_cnt);
425 get_absolute_rl_internal(expr->right, &right_rl, recurse_cnt);
426 left_rl = cast_rl(type, left_rl);
427 right_rl = cast_rl(type, right_rl);
428 if (!left_rl || !right_rl)
429 return NULL;
431 return rl_binop(left_rl, expr->op, right_rl);
434 static struct range_list *handle_right_shift(struct expression *expr, int implied, int *recurse_cnt)
436 struct range_list *left_rl;
437 sval_t right;
438 sval_t min, max;
440 if (implied == RL_EXACT || implied == RL_HARD)
441 return NULL;
443 left_rl = _get_rl(expr->left, implied, recurse_cnt);
444 if (left_rl) {
445 max = rl_max(left_rl);
446 min = rl_min(left_rl);
447 } else {
448 if (implied == RL_FUZZY)
449 return NULL;
450 max = sval_type_max(get_type(expr->left));
451 min = sval_type_val(get_type(expr->left), 0);
454 if (get_implied_value_internal(expr->right, &right, recurse_cnt)) {
455 min = sval_binop(min, SPECIAL_RIGHTSHIFT, right);
456 max = sval_binop(max, SPECIAL_RIGHTSHIFT, right);
457 } else if (!sval_is_negative(min)) {
458 min.value = 0;
459 max = sval_type_max(max.type);
460 } else {
461 return NULL;
464 return alloc_rl(min, max);
467 static struct range_list *handle_left_shift(struct expression *expr, int implied, int *recurse_cnt)
469 struct range_list *left_rl, *res;
470 sval_t right;
471 sval_t min, max;
472 int add_zero = 0;
474 if (implied == RL_EXACT || implied == RL_HARD)
475 return NULL;
476 /* this is hopeless without the right side */
477 if (!get_implied_value_internal(expr->right, &right, recurse_cnt))
478 return NULL;
479 left_rl = _get_rl(expr->left, implied, recurse_cnt);
480 if (left_rl) {
481 max = rl_max(left_rl);
482 min = rl_min(left_rl);
483 if (min.value == 0) {
484 min.value = 1;
485 add_zero = 1;
487 } else {
488 if (implied == RL_FUZZY)
489 return NULL;
490 max = sval_type_max(get_type(expr->left));
491 min = sval_type_val(get_type(expr->left), 1);
492 add_zero = 1;
495 max = sval_binop(max, SPECIAL_LEFTSHIFT, right);
496 min = sval_binop(min, SPECIAL_LEFTSHIFT, right);
497 res = alloc_rl(min, max);
498 if (add_zero)
499 res = rl_union(res, rl_zero());
500 return res;
503 static struct range_list *handle_known_binop(struct expression *expr)
505 sval_t left, right;
507 if (!get_value(expr->left, &left))
508 return NULL;
509 if (!get_value(expr->right, &right))
510 return NULL;
511 left = sval_binop(left, expr->op, right);
512 return alloc_rl(left, left);
515 static int has_actual_ranges(struct range_list *rl)
517 struct data_range *tmp;
519 FOR_EACH_PTR(rl, tmp) {
520 if (sval_cmp(tmp->min, tmp->max) != 0)
521 return 1;
522 } END_FOR_EACH_PTR(tmp);
523 return 0;
526 static struct range_list *handle_implied_binop(struct range_list *left_rl, int op, struct range_list *right_rl)
528 struct range_list *res_rl;
529 struct data_range *left_drange, *right_drange;
530 sval_t res;
532 if (!left_rl || !right_rl)
533 return NULL;
534 if (has_actual_ranges(left_rl))
535 return NULL;
536 if (has_actual_ranges(right_rl))
537 return NULL;
539 if (ptr_list_size((struct ptr_list *)left_rl) * ptr_list_size((struct ptr_list *)right_rl) > 20)
540 return NULL;
542 res_rl = NULL;
544 FOR_EACH_PTR(left_rl, left_drange) {
545 FOR_EACH_PTR(right_rl, right_drange) {
546 if ((op == '%' || op == '/') &&
547 right_drange->min.value == 0)
548 return NULL;
549 res = sval_binop(left_drange->min, op, right_drange->min);
550 add_range(&res_rl, res, res);
551 } END_FOR_EACH_PTR(right_drange);
552 } END_FOR_EACH_PTR(left_drange);
554 return res_rl;
557 static struct range_list *handle_binop_rl(struct expression *expr, int implied, int *recurse_cnt)
559 struct smatch_state *state;
560 struct symbol *type;
561 struct range_list *left_rl, *right_rl, *rl;
562 sval_t min, max;
564 rl = handle_known_binop(expr);
565 if (rl)
566 return rl;
567 if (implied == RL_EXACT)
568 return NULL;
570 if (custom_handle_variable) {
571 rl = custom_handle_variable(expr);
572 if (rl)
573 return rl;
576 state = get_extra_state(expr);
577 if (state && !is_whole_rl(estate_rl(state)))
578 return clone_rl(estate_rl(state));
580 type = get_type(expr);
581 left_rl = _get_rl(expr->left, implied, recurse_cnt);
582 left_rl = cast_rl(type, left_rl);
583 right_rl = _get_rl(expr->right, implied, recurse_cnt);
584 right_rl = cast_rl(type, right_rl);
586 if (!left_rl && !right_rl)
587 return NULL;
589 rl = handle_implied_binop(left_rl, expr->op, right_rl);
590 if (rl)
591 return rl;
593 switch (expr->op) {
594 case '%':
595 return handle_mod_rl(expr, implied, recurse_cnt);
596 case '&':
597 return handle_bitwise_AND(expr, implied, recurse_cnt);
598 case '|':
599 case '^':
600 return use_rl_binop(expr, implied, recurse_cnt);
601 case SPECIAL_RIGHTSHIFT:
602 return handle_right_shift(expr, implied, recurse_cnt);
603 case SPECIAL_LEFTSHIFT:
604 return handle_left_shift(expr, implied, recurse_cnt);
605 case '-':
606 return handle_subtract_rl(expr, implied, recurse_cnt);
607 case '/':
608 return handle_divide_rl(expr, implied, recurse_cnt);
611 if (!left_rl || !right_rl)
612 return NULL;
614 if (sval_binop_overflows(rl_min(left_rl), expr->op, rl_min(right_rl)))
615 return NULL;
616 if (sval_binop_overflows(rl_max(left_rl), expr->op, rl_max(right_rl)))
617 return NULL;
619 min = sval_binop(rl_min(left_rl), expr->op, rl_min(right_rl));
620 max = sval_binop(rl_max(left_rl), expr->op, rl_max(right_rl));
622 return alloc_rl(min, max);
625 static int do_comparison(struct expression *expr)
627 struct range_list *left_ranges = NULL;
628 struct range_list *right_ranges = NULL;
629 int poss_true, poss_false;
630 struct symbol *type;
632 type = get_type(expr);
633 get_absolute_rl(expr->left, &left_ranges);
634 get_absolute_rl(expr->right, &right_ranges);
636 left_ranges = cast_rl(type, left_ranges);
637 right_ranges = cast_rl(type, right_ranges);
639 poss_true = possibly_true_rl(left_ranges, expr->op, right_ranges);
640 poss_false = possibly_false_rl(left_ranges, expr->op, right_ranges);
642 free_rl(&left_ranges);
643 free_rl(&right_ranges);
645 if (!poss_true && !poss_false)
646 return 0x0;
647 if (poss_true && !poss_false)
648 return 0x1;
649 if (!poss_true && poss_false)
650 return 0x2;
651 return 0x3;
654 static struct range_list *handle_comparison_rl(struct expression *expr, int implied, int *recurse_cnt)
656 sval_t left, right;
657 int res;
659 if (expr->op == SPECIAL_EQUAL && expr->left->type == EXPR_TYPE) {
660 struct symbol *left, *right;
662 left = get_real_base_type(expr->left->symbol);
663 right = get_real_base_type(expr->left->symbol);
664 if (left == right)
665 return rl_one();
666 return rl_zero();
669 if (get_value(expr->left, &left) && get_value(expr->right, &right)) {
670 struct data_range tmp_left, tmp_right;
672 tmp_left.min = left;
673 tmp_left.max = left;
674 tmp_right.min = right;
675 tmp_right.max = right;
676 if (true_comparison_range(&tmp_left, expr->op, &tmp_right))
677 return rl_one();
678 return rl_zero();
681 if (implied == RL_EXACT)
682 return NULL;
684 res = do_comparison(expr);
685 if (res == 1)
686 return rl_one();
687 if (res == 2)
688 return rl_zero();
690 return alloc_rl(zero, one);
693 static struct range_list *handle_logical_rl(struct expression *expr, int implied, int *recurse_cnt)
695 sval_t left, right;
696 int left_known = 0;
697 int right_known = 0;
699 if (implied == RL_EXACT) {
700 if (get_value(expr->left, &left))
701 left_known = 1;
702 if (get_value(expr->right, &right))
703 right_known = 1;
704 } else {
705 if (get_implied_value_internal(expr->left, &left, recurse_cnt))
706 left_known = 1;
707 if (get_implied_value_internal(expr->right, &right, recurse_cnt))
708 right_known = 1;
711 switch (expr->op) {
712 case SPECIAL_LOGICAL_OR:
713 if (left_known && left.value)
714 return rl_one();
715 if (right_known && right.value)
716 return rl_one();
717 if (left_known && right_known)
718 return rl_zero();
719 break;
720 case SPECIAL_LOGICAL_AND:
721 if (left_known && right_known) {
722 if (left.value && right.value)
723 return rl_one();
724 return rl_zero();
726 break;
727 default:
728 return NULL;
731 if (implied == RL_EXACT)
732 return NULL;
734 return alloc_rl(zero, one);
737 static struct range_list *handle_conditional_rl(struct expression *expr, int implied, int *recurse_cnt)
739 struct range_list *true_rl, *false_rl;
740 struct symbol *type;
741 int final_pass_orig = final_pass;
743 if (known_condition_true(expr->conditional))
744 return _get_rl(expr->cond_true, implied, recurse_cnt);
745 if (known_condition_false(expr->conditional))
746 return _get_rl(expr->cond_false, implied, recurse_cnt);
748 if (implied == RL_EXACT)
749 return NULL;
751 if (implied_condition_true(expr->conditional))
752 return _get_rl(expr->cond_true, implied, recurse_cnt);
753 if (implied_condition_false(expr->conditional))
754 return _get_rl(expr->cond_false, implied, recurse_cnt);
757 /* this becomes a problem with deeply nested conditional statements */
758 if (low_on_memory())
759 return NULL;
761 type = get_type(expr);
763 __push_fake_cur_stree();
764 final_pass = 0;
765 __split_whole_condition(expr->conditional);
766 true_rl = _get_rl(expr->cond_true, implied, recurse_cnt);
767 __push_true_states();
768 __use_false_states();
769 false_rl = _get_rl(expr->cond_false, implied, recurse_cnt);
770 __merge_true_states();
771 __free_fake_cur_stree();
772 final_pass = final_pass_orig;
774 if (!true_rl || !false_rl)
775 return NULL;
776 true_rl = cast_rl(type, true_rl);
777 false_rl = cast_rl(type, false_rl);
779 return rl_union(true_rl, false_rl);
782 static int get_fuzzy_max_helper(struct expression *expr, sval_t *max)
784 struct smatch_state *state;
785 sval_t sval;
787 if (get_hard_max(expr, &sval)) {
788 *max = sval;
789 return 1;
792 state = get_extra_state(expr);
793 if (!state || !estate_has_fuzzy_max(state))
794 return 0;
795 *max = sval_cast(get_type(expr), estate_get_fuzzy_max(state));
796 return 1;
799 static int get_fuzzy_min_helper(struct expression *expr, sval_t *min)
801 struct smatch_state *state;
802 sval_t sval;
804 state = get_extra_state(expr);
805 if (!state || !estate_rl(state))
806 return 0;
808 sval = estate_min(state);
809 if (sval_is_negative(sval) && sval_is_min(sval))
810 return 0;
812 if (sval_is_max(sval))
813 return 0;
815 *min = sval_cast(get_type(expr), sval);
816 return 1;
819 int get_const_value(struct expression *expr, sval_t *sval)
821 struct symbol *sym;
822 sval_t right;
824 if (expr->type != EXPR_SYMBOL || !expr->symbol)
825 return 0;
826 sym = expr->symbol;
827 if (!(sym->ctype.modifiers & MOD_CONST))
828 return 0;
829 if (get_value(sym->initializer, &right)) {
830 *sval = sval_cast(get_type(expr), right);
831 return 1;
833 return 0;
836 struct range_list *var_to_absolute_rl(struct expression *expr)
838 struct smatch_state *state;
839 struct range_list *rl;
841 state = get_extra_state(expr);
842 if (!state || is_whole_rl(estate_rl(state))) {
843 state = get_real_absolute_state(expr);
844 if (state && state->data && !estate_is_whole(state))
845 return clone_rl(estate_rl(state));
846 if (get_local_rl(expr, &rl) && !is_whole_rl(rl))
847 return rl;
848 if (get_db_type_rl(expr, &rl) && !is_whole_rl(rl))
849 return rl;
850 return alloc_whole_rl(get_type(expr));
852 /* err on the side of saying things are possible */
853 if (!estate_rl(state))
854 return alloc_whole_rl(get_type(expr));
855 return clone_rl(estate_rl(state));
858 static struct range_list *handle_variable(struct expression *expr, int implied, int *recurse_cnt)
860 struct smatch_state *state;
861 struct range_list *rl;
862 sval_t sval, min, max;
863 struct symbol *type;
865 if (get_const_value(expr, &sval))
866 return alloc_rl(sval, sval);
868 if (custom_handle_variable) {
869 rl = custom_handle_variable(expr);
870 if (!rl)
871 return var_to_absolute_rl(expr);
872 return rl;
875 if (implied == RL_EXACT)
876 return NULL;
878 type = get_type(expr);
879 if (type && type->type == SYM_FN)
880 return alloc_rl(fn_ptr_min, fn_ptr_max);
882 switch (implied) {
883 case RL_HARD:
884 case RL_IMPLIED:
885 case RL_ABSOLUTE:
886 state = get_extra_state(expr);
887 if (!state || !state->data) {
888 if (implied == RL_HARD)
889 return NULL;
890 if (get_local_rl(expr, &rl))
891 return rl;
892 if (get_db_type_rl(expr, &rl))
893 return rl;
894 return NULL;
896 if (implied == RL_HARD && !estate_has_hard_max(state))
897 return NULL;
898 return clone_rl(estate_rl(state));
899 case RL_REAL_ABSOLUTE: {
900 struct smatch_state *abs_state;
902 state = get_extra_state(expr);
903 abs_state = get_real_absolute_state(expr);
905 if (estate_rl(state) && estate_rl(abs_state)) {
906 return clone_rl(rl_intersection(estate_rl(state),
907 estate_rl(abs_state)));
908 } else if (estate_rl(state)) {
909 return clone_rl(estate_rl(state));
910 } else if (state && estate_is_empty(state)) {
912 * FIXME: we don't handle empty extra states correctly.
914 * The real abs rl is supposed to be filtered by the
915 * extra state if there is one. We don't bother keeping
916 * the abs state in sync all the time because we know it
917 * will be filtered later.
919 * It's not totally obvious to me how they should be
920 * handled. Perhaps we should take the whole rl and
921 * filter by the imaginary states. Perhaps we should
922 * just go with the empty state.
924 * Anyway what we currently do is return NULL here and
925 * that gets translated into the whole range in
926 * get_real_absolute_rl().
929 return NULL;
930 } else if (estate_rl(abs_state)) {
931 return clone_rl(estate_rl(abs_state));
934 if (get_local_rl(expr, &rl))
935 return rl;
936 if (get_db_type_rl(expr, &rl))
937 return rl;
938 return NULL;
940 case RL_FUZZY:
941 if (!get_fuzzy_min_helper(expr, &min))
942 min = sval_type_min(get_type(expr));
943 if (!get_fuzzy_max_helper(expr, &max))
944 return NULL;
945 /* fuzzy ranges are often inverted */
946 if (sval_cmp(min, max) > 0) {
947 sval = min;
948 min = max;
949 max = sval;
951 return alloc_rl(min, max);
953 return NULL;
956 static sval_t handle_sizeof(struct expression *expr)
958 struct symbol *sym;
959 sval_t ret;
961 ret = sval_blank(expr);
962 sym = expr->cast_type;
963 if (!sym) {
964 sym = evaluate_expression(expr->cast_expression);
965 if (!sym)
966 sym = &int_ctype;
967 #if 0
969 * Expressions of restricted types will possibly get
970 * promoted - check that here. I'm not sure how this works,
971 * the problem is that sizeof(le16) shouldn't be promoted and
972 * the original code did that... Let's if zero this out and
973 * see what breaks.
976 if (is_restricted_type(sym)) {
977 if (type_bits(sym) < bits_in_int)
978 sym = &int_ctype;
980 #endif
981 if (is_fouled_type(sym))
982 sym = &int_ctype;
984 examine_symbol_type(sym);
986 ret.type = size_t_ctype;
987 if (type_bits(sym) <= 0) /* sizeof(void) */ {
988 if (get_real_base_type(sym) == &void_ctype)
989 ret.value = 1;
990 else
991 ret.value = 0;
992 } else
993 ret.value = type_bytes(sym);
995 return ret;
998 static struct range_list *handle_strlen(struct expression *expr, int implied, int *recurse_cnt)
1000 struct range_list *rl;
1001 struct expression *arg;
1002 sval_t sval = { .type = &ulong_ctype };
1004 if (implied == RL_EXACT)
1005 return NULL;
1007 arg = get_argument_from_call_expr(expr->args, 0);
1008 if (!arg)
1009 return NULL;
1010 if (arg->type == EXPR_STRING) {
1011 sval.value = arg->string->length - 1;
1012 return alloc_rl(sval, sval);
1015 if (implied == RL_HARD || implied == RL_FUZZY)
1016 return NULL;
1018 if (get_implied_return(expr, &rl))
1019 return rl;
1021 return NULL;
1024 static struct range_list *handle_call_rl(struct expression *expr, int implied, int *recurse_cnt)
1026 struct range_list *rl;
1028 if (sym_name_is("__builtin_expect", expr->fn) ||
1029 sym_name_is("__builtin_bswap16", expr->fn) ||
1030 sym_name_is("__builtin_bswap32", expr->fn) ||
1031 sym_name_is("__builtin_bswap64", expr->fn)) {
1032 struct expression *arg;
1034 arg = get_argument_from_call_expr(expr->args, 0);
1035 return _get_rl(arg, implied, recurse_cnt);
1038 if (sym_name_is("strlen", expr->fn))
1039 return handle_strlen(expr, implied, recurse_cnt);
1041 if (implied == RL_EXACT || implied == RL_HARD || implied == RL_FUZZY)
1042 return NULL;
1044 if (custom_handle_variable) {
1045 rl = custom_handle_variable(expr);
1046 if (rl)
1047 return rl;
1050 if (get_implied_return(expr, &rl))
1051 return rl;
1052 return db_return_vals(expr);
1055 static struct range_list *handle_cast(struct expression *expr, int implied, int *recurse_cnt)
1057 struct range_list *rl;
1058 struct symbol *type;
1060 type = get_type(expr);
1061 rl = _get_rl(expr->cast_expression, implied, recurse_cnt);
1062 if (rl)
1063 return cast_rl(type, rl);
1064 if (implied == RL_ABSOLUTE || implied == RL_REAL_ABSOLUTE)
1065 return alloc_whole_rl(type);
1066 if (implied == RL_IMPLIED && type &&
1067 type_bits(type) > 0 && type_bits(type) < 32)
1068 return alloc_whole_rl(type);
1069 return NULL;
1072 static struct range_list *_get_rl(struct expression *expr, int implied, int *recurse_cnt)
1074 struct range_list *rl;
1075 struct symbol *type;
1076 sval_t sval;
1078 type = get_type(expr);
1079 expr = strip_parens(expr);
1080 if (!expr)
1081 return NULL;
1083 if (++(*recurse_cnt) >= 200)
1084 return NULL;
1086 switch(expr->type) {
1087 case EXPR_CAST:
1088 case EXPR_FORCE_CAST:
1089 case EXPR_IMPLIED_CAST:
1090 rl = handle_cast(expr, implied, recurse_cnt);
1091 goto out_cast;
1094 expr = strip_expr(expr);
1095 if (!expr)
1096 return NULL;
1098 switch (expr->type) {
1099 case EXPR_VALUE:
1100 sval = sval_from_val(expr, expr->value);
1101 rl = alloc_rl(sval, sval);
1102 break;
1103 case EXPR_PREOP:
1104 rl = handle_preop_rl(expr, implied, recurse_cnt);
1105 break;
1106 case EXPR_POSTOP:
1107 rl = _get_rl(expr->unop, implied, recurse_cnt);
1108 break;
1109 case EXPR_BINOP:
1110 rl = handle_binop_rl(expr, implied, recurse_cnt);
1111 break;
1112 case EXPR_COMPARE:
1113 rl = handle_comparison_rl(expr, implied, recurse_cnt);
1114 break;
1115 case EXPR_LOGICAL:
1116 rl = handle_logical_rl(expr, implied, recurse_cnt);
1117 break;
1118 case EXPR_PTRSIZEOF:
1119 case EXPR_SIZEOF:
1120 sval = handle_sizeof(expr);
1121 rl = alloc_rl(sval, sval);
1122 break;
1123 case EXPR_SELECT:
1124 case EXPR_CONDITIONAL:
1125 rl = handle_conditional_rl(expr, implied, recurse_cnt);
1126 break;
1127 case EXPR_CALL:
1128 rl = handle_call_rl(expr, implied, recurse_cnt);
1129 break;
1130 default:
1131 rl = handle_variable(expr, implied, recurse_cnt);
1134 out_cast:
1135 if (rl)
1136 return rl;
1137 if (type && (implied == RL_ABSOLUTE || implied == RL_REAL_ABSOLUTE))
1138 return alloc_whole_rl(type);
1139 return NULL;
1142 /* returns 1 if it can get a value literal or else returns 0 */
1143 int get_value(struct expression *expr, sval_t *sval)
1145 struct range_list *rl;
1146 int recurse_cnt = 0;
1148 rl = _get_rl(expr, RL_EXACT, &recurse_cnt);
1149 if (!rl_to_sval(rl, sval))
1150 return 0;
1151 return 1;
1154 static int get_implied_value_internal(struct expression *expr, sval_t *sval, int *recurse_cnt)
1156 struct range_list *rl;
1158 rl = _get_rl(expr, RL_IMPLIED, recurse_cnt);
1159 if (!rl_to_sval(rl, sval))
1160 return 0;
1161 return 1;
1164 int get_implied_value(struct expression *expr, sval_t *sval)
1166 struct range_list *rl;
1167 int recurse_cnt = 0;
1169 rl = _get_rl(expr, RL_IMPLIED, &recurse_cnt);
1170 if (!rl_to_sval(rl, sval))
1171 return 0;
1172 return 1;
1175 int get_implied_min(struct expression *expr, sval_t *sval)
1177 struct range_list *rl;
1178 int recurse_cnt = 0;
1180 rl = _get_rl(expr, RL_IMPLIED, &recurse_cnt);
1181 if (!rl)
1182 return 0;
1183 *sval = rl_min(rl);
1184 return 1;
1187 int get_implied_max(struct expression *expr, sval_t *sval)
1189 struct range_list *rl;
1190 int recurse_cnt = 0;
1192 rl = _get_rl(expr, RL_IMPLIED, &recurse_cnt);
1193 if (!rl)
1194 return 0;
1195 *sval = rl_max(rl);
1196 return 1;
1199 int get_implied_rl(struct expression *expr, struct range_list **rl)
1201 int recurse_cnt = 0;
1203 *rl = _get_rl(expr, RL_IMPLIED, &recurse_cnt);
1204 if (*rl)
1205 return 1;
1206 return 0;
1209 static int get_absolute_rl_internal(struct expression *expr, struct range_list **rl, int *recurse_cnt)
1211 *rl = _get_rl(expr, RL_ABSOLUTE, recurse_cnt);
1212 if (!*rl)
1213 *rl = alloc_whole_rl(get_type(expr));
1214 return 1;
1217 int get_absolute_rl(struct expression *expr, struct range_list **rl)
1219 int recurse_cnt = 0;
1221 *rl = _get_rl(expr, RL_ABSOLUTE, &recurse_cnt);
1222 if (!*rl)
1223 *rl = alloc_whole_rl(get_type(expr));
1224 return 1;
1227 int get_real_absolute_rl(struct expression *expr, struct range_list **rl)
1229 int recurse_cnt = 0;
1231 *rl = _get_rl(expr, RL_REAL_ABSOLUTE, &recurse_cnt);
1232 if (!*rl)
1233 *rl = alloc_whole_rl(get_type(expr));
1234 return 1;
1237 int custom_get_absolute_rl(struct expression *expr,
1238 struct range_list *(*fn)(struct expression *expr),
1239 struct range_list **rl)
1241 int recurse_cnt = 0;
1243 *rl = NULL;
1244 custom_handle_variable = fn;
1245 *rl = _get_rl(expr, RL_REAL_ABSOLUTE, &recurse_cnt);
1246 custom_handle_variable = NULL;
1247 return 1;
1250 int get_implied_rl_var_sym(const char *var, struct symbol *sym, struct range_list **rl)
1252 struct smatch_state *state;
1254 state = get_state(SMATCH_EXTRA, var, sym);
1255 *rl = estate_rl(state);
1256 if (*rl)
1257 return 1;
1258 return 0;
1261 int get_hard_max(struct expression *expr, sval_t *sval)
1263 struct range_list *rl;
1264 int recurse_cnt = 0;
1266 rl = _get_rl(expr, RL_HARD, &recurse_cnt);
1267 if (!rl)
1268 return 0;
1269 *sval = rl_max(rl);
1270 return 1;
1273 int get_fuzzy_min(struct expression *expr, sval_t *sval)
1275 struct range_list *rl;
1276 sval_t tmp;
1277 int recurse_cnt = 0;
1279 rl = _get_rl(expr, RL_FUZZY, &recurse_cnt);
1280 if (!rl)
1281 return 0;
1282 tmp = rl_min(rl);
1283 if (sval_is_negative(tmp) && sval_is_min(tmp))
1284 return 0;
1285 *sval = tmp;
1286 return 1;
1289 int get_fuzzy_max(struct expression *expr, sval_t *sval)
1291 struct range_list *rl;
1292 sval_t max;
1293 int recurse_cnt = 0;
1295 rl = _get_rl(expr, RL_FUZZY, &recurse_cnt);
1296 if (!rl)
1297 return 0;
1298 max = rl_max(rl);
1299 if (max.uvalue > INT_MAX - 10000)
1300 return 0;
1301 *sval = max;
1302 return 1;
1305 int get_absolute_min(struct expression *expr, sval_t *sval)
1307 struct range_list *rl;
1308 struct symbol *type;
1309 int recurse_cnt = 0;
1311 type = get_type(expr);
1312 if (!type)
1313 type = &llong_ctype; // FIXME: this is wrong but places assume get type can't fail.
1314 rl = _get_rl(expr, RL_REAL_ABSOLUTE, &recurse_cnt);
1315 if (rl)
1316 *sval = rl_min(rl);
1317 else
1318 *sval = sval_type_min(type);
1320 if (sval_cmp(*sval, sval_type_min(type)) < 0)
1321 *sval = sval_type_min(type);
1322 return 1;
1325 int get_absolute_max(struct expression *expr, sval_t *sval)
1327 struct range_list *rl;
1328 struct symbol *type;
1329 int recurse_cnt = 0;
1331 type = get_type(expr);
1332 if (!type)
1333 type = &llong_ctype;
1334 rl = _get_rl(expr, RL_REAL_ABSOLUTE, &recurse_cnt);
1335 if (rl)
1336 *sval = rl_max(rl);
1337 else
1338 *sval = sval_type_max(type);
1340 if (sval_cmp(sval_type_max(type), *sval) < 0)
1341 *sval = sval_type_max(type);
1342 return 1;
1345 int known_condition_true(struct expression *expr)
1347 sval_t tmp;
1349 if (!expr)
1350 return 0;
1352 if (get_value(expr, &tmp) && tmp.value)
1353 return 1;
1355 return 0;
1358 int known_condition_false(struct expression *expr)
1360 if (!expr)
1361 return 0;
1363 if (is_zero(expr))
1364 return 1;
1366 if (expr->type == EXPR_CALL) {
1367 if (sym_name_is("__builtin_constant_p", expr->fn))
1368 return 1;
1370 return 0;
1373 int implied_condition_true(struct expression *expr)
1375 sval_t tmp;
1377 if (!expr)
1378 return 0;
1380 if (known_condition_true(expr))
1381 return 1;
1382 if (get_implied_value(expr, &tmp) && tmp.value)
1383 return 1;
1385 if (expr->type == EXPR_POSTOP)
1386 return implied_condition_true(expr->unop);
1388 if (expr->type == EXPR_PREOP && expr->op == SPECIAL_DECREMENT)
1389 return implied_not_equal(expr->unop, 1);
1390 if (expr->type == EXPR_PREOP && expr->op == SPECIAL_INCREMENT)
1391 return implied_not_equal(expr->unop, -1);
1393 expr = strip_expr(expr);
1394 switch (expr->type) {
1395 case EXPR_COMPARE:
1396 if (do_comparison(expr) == 1)
1397 return 1;
1398 break;
1399 case EXPR_PREOP:
1400 if (expr->op == '!') {
1401 if (implied_condition_false(expr->unop))
1402 return 1;
1403 break;
1405 break;
1406 default:
1407 if (implied_not_equal(expr, 0) == 1)
1408 return 1;
1409 break;
1411 return 0;
1414 int implied_condition_false(struct expression *expr)
1416 struct expression *tmp;
1417 sval_t sval;
1419 if (!expr)
1420 return 0;
1422 if (known_condition_false(expr))
1423 return 1;
1425 switch (expr->type) {
1426 case EXPR_COMPARE:
1427 if (do_comparison(expr) == 2)
1428 return 1;
1429 case EXPR_PREOP:
1430 if (expr->op == '!') {
1431 if (implied_condition_true(expr->unop))
1432 return 1;
1433 break;
1435 tmp = strip_expr(expr);
1436 if (tmp != expr)
1437 return implied_condition_false(tmp);
1438 break;
1439 default:
1440 if (get_implied_value(expr, &sval) && sval.value == 0)
1441 return 1;
1442 break;
1444 return 0;
1447 int can_integer_overflow(struct symbol *type, struct expression *expr)
1449 int op;
1450 sval_t lmax, rmax, res;
1452 if (!type)
1453 type = &int_ctype;
1455 expr = strip_expr(expr);
1457 if (expr->type == EXPR_ASSIGNMENT) {
1458 switch(expr->op) {
1459 case SPECIAL_MUL_ASSIGN:
1460 op = '*';
1461 break;
1462 case SPECIAL_ADD_ASSIGN:
1463 op = '+';
1464 break;
1465 case SPECIAL_SHL_ASSIGN:
1466 op = SPECIAL_LEFTSHIFT;
1467 break;
1468 default:
1469 return 0;
1471 } else if (expr->type == EXPR_BINOP) {
1472 if (expr->op != '*' && expr->op != '+' && expr->op != SPECIAL_LEFTSHIFT)
1473 return 0;
1474 op = expr->op;
1475 } else {
1476 return 0;
1479 get_absolute_max(expr->left, &lmax);
1480 get_absolute_max(expr->right, &rmax);
1482 if (sval_binop_overflows(lmax, op, rmax))
1483 return 1;
1485 res = sval_binop(lmax, op, rmax);
1486 if (sval_cmp(res, sval_type_max(type)) > 0)
1487 return 1;
1488 return 0;