db/kernel.no_inline: don't parse the bite swap functions inline
[smatch.git] / smatch_math.c
blob1b588943637981301463df8d8b779e5cc84a569d
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 sval;
125 rl = _get_rl(expr->unop, implied, recurse_cnt);
126 if (!rl_to_sval(rl, &sval))
127 return NULL;
128 sval = sval_preop(sval, '-');
129 return alloc_rl(sval, sval);
132 static struct range_list *handle_preop_rl(struct expression *expr, int implied, int *recurse_cnt)
134 switch (expr->op) {
135 case '&':
136 return handle_ampersand_rl(expr, implied, recurse_cnt);
137 case '!':
138 return handle_negate_rl(expr, implied, recurse_cnt);
139 case '~':
140 return handle_bitwise_negate(expr, implied, recurse_cnt);
141 case '-':
142 return handle_minus_preop(expr, implied, recurse_cnt);
143 case '*':
144 return handle_variable(expr, implied, recurse_cnt);
145 case '(':
146 return handle_expression_statement_rl(expr, implied, recurse_cnt);
147 default:
148 return NULL;
152 static struct range_list *handle_divide_rl(struct expression *expr, int implied, int *recurse_cnt)
154 struct range_list *left_rl, *right_rl;
155 struct symbol *type;
157 type = get_type(expr);
159 left_rl = _get_rl(expr->left, implied, recurse_cnt);
160 left_rl = cast_rl(type, left_rl);
161 right_rl = _get_rl(expr->right, implied, recurse_cnt);
162 right_rl = cast_rl(type, right_rl);
164 if (!left_rl || !right_rl)
165 return NULL;
167 if (implied != RL_REAL_ABSOLUTE) {
168 if (is_whole_rl(left_rl) || is_whole_rl(right_rl))
169 return NULL;
172 return rl_binop(left_rl, '/', right_rl);
175 static struct range_list *handle_subtract_rl(struct expression *expr, int implied, int *recurse_cnt)
177 struct symbol *type;
178 struct range_list *left_rl, *right_rl;
179 sval_t max, min, tmp;
180 int comparison;
182 type = get_type(expr);
183 comparison = get_comparison(expr->left, expr->right);
185 left_rl = _get_rl(expr->left, implied, recurse_cnt);
186 left_rl = cast_rl(type, left_rl);
187 right_rl = _get_rl(expr->right, implied, recurse_cnt);
188 right_rl = cast_rl(type, right_rl);
190 if ((!left_rl || !right_rl) &&
191 (implied == RL_EXACT || implied == RL_HARD || implied == RL_FUZZY))
192 return NULL;
194 if (!left_rl)
195 left_rl = alloc_whole_rl(type);
196 if (!right_rl)
197 right_rl = alloc_whole_rl(type);
199 /* negative values complicate everything fix this later */
200 if (sval_is_negative(rl_min(right_rl)))
201 return NULL;
202 max = rl_max(left_rl);
204 switch (comparison) {
205 case '>':
206 case SPECIAL_UNSIGNED_GT:
207 min = sval_type_val(type, 1);
208 max = rl_max(left_rl);
209 break;
210 case SPECIAL_GTE:
211 case SPECIAL_UNSIGNED_GTE:
212 min = sval_type_val(type, 0);
213 max = rl_max(left_rl);
214 break;
215 default:
216 if (sval_binop_overflows(rl_min(left_rl), '-', rl_max(right_rl)))
217 return NULL;
218 min = sval_type_min(type);
221 if (!sval_binop_overflows(rl_min(left_rl), '-', rl_max(right_rl))) {
222 tmp = sval_binop(rl_min(left_rl), '-', rl_max(right_rl));
223 if (sval_cmp(tmp, min) > 0)
224 min = tmp;
227 if (!sval_is_max(rl_max(left_rl))) {
228 tmp = sval_binop(rl_max(left_rl), '-', rl_min(right_rl));
229 if (sval_cmp(tmp, max) < 0)
230 max = tmp;
233 if (sval_is_min(min) && sval_is_max(max))
234 return NULL;
236 return cast_rl(type, alloc_rl(min, max));
239 static struct range_list *handle_mod_rl(struct expression *expr, int implied, int *recurse_cnt)
241 struct range_list *rl;
242 sval_t left, right, sval;
244 if (implied == RL_EXACT) {
245 if (!get_value(expr->right, &right))
246 return NULL;
247 if (!get_value(expr->left, &left))
248 return NULL;
249 sval = sval_binop(left, '%', right);
250 return alloc_rl(sval, sval);
252 /* if we can't figure out the right side it's probably hopeless */
253 if (!get_implied_value_internal(expr->right, &right, recurse_cnt))
254 return NULL;
256 right = sval_cast(get_type(expr), right);
257 right.value--;
259 rl = _get_rl(expr->left, implied, recurse_cnt);
260 if (rl && rl_max(rl).uvalue < right.uvalue)
261 right.uvalue = rl_max(rl).uvalue;
263 return alloc_rl(sval_cast(right.type, zero), right);
266 static sval_t sval_lowest_set_bit(sval_t sval)
268 int i;
269 int found = 0;
271 for (i = 0; i < 64; i++) {
272 if (sval.uvalue & 1ULL << i) {
273 if (!found++)
274 continue;
275 sval.uvalue &= ~(1ULL << i);
278 return sval;
281 static struct range_list *handle_bitwise_AND(struct expression *expr, int implied, int *recurse_cnt)
283 struct symbol *type;
284 struct range_list *left_rl, *right_rl;
285 sval_t known;
287 if (implied != RL_IMPLIED && implied != RL_ABSOLUTE && implied != RL_REAL_ABSOLUTE)
288 return NULL;
290 type = get_type(expr);
292 if (get_implied_value_internal(expr->left, &known, recurse_cnt)) {
293 sval_t min;
295 min = sval_lowest_set_bit(known);
296 left_rl = alloc_rl(min, known);
297 left_rl = cast_rl(type, left_rl);
298 add_range(&left_rl, sval_type_val(type, 0), sval_type_val(type, 0));
299 } else {
300 left_rl = _get_rl(expr->left, implied, recurse_cnt);
301 if (left_rl) {
302 left_rl = cast_rl(type, left_rl);
303 left_rl = alloc_rl(sval_type_val(type, 0), rl_max(left_rl));
304 } else {
305 if (implied == RL_HARD)
306 return NULL;
307 left_rl = alloc_whole_rl(type);
311 if (get_implied_value_internal(expr->right, &known, recurse_cnt)) {
312 sval_t min, left_max, mod;
314 min = sval_lowest_set_bit(known);
315 right_rl = alloc_rl(min, known);
316 right_rl = cast_rl(type, right_rl);
317 add_range(&right_rl, sval_type_val(type, 0), sval_type_val(type, 0));
319 if (min.value != 0) {
320 left_max = rl_max(left_rl);
321 mod = sval_binop(left_max, '%', min);
322 if (mod.value) {
323 left_max = sval_binop(left_max, '-', mod);
324 left_max.value++;
325 if (left_max.value > 0 && sval_cmp(left_max, rl_max(left_rl)) < 0)
326 left_rl = remove_range(left_rl, left_max, rl_max(left_rl));
329 } else {
330 right_rl = _get_rl(expr->right, implied, recurse_cnt);
331 if (right_rl) {
332 right_rl = cast_rl(type, right_rl);
333 right_rl = alloc_rl(sval_type_val(type, 0), rl_max(right_rl));
334 } else {
335 if (implied == RL_HARD)
336 return NULL;
337 right_rl = alloc_whole_rl(type);
341 return rl_intersection(left_rl, right_rl);
344 static struct range_list *use_rl_binop(struct expression *expr, int implied, int *recurse_cnt)
346 struct symbol *type;
347 struct range_list *left_rl, *right_rl;
349 if (implied != RL_IMPLIED && implied != RL_ABSOLUTE && implied != RL_REAL_ABSOLUTE)
350 return NULL;
352 type = get_type(expr);
354 get_absolute_rl_internal(expr->left, &left_rl, recurse_cnt);
355 get_absolute_rl_internal(expr->right, &right_rl, recurse_cnt);
356 left_rl = cast_rl(type, left_rl);
357 right_rl = cast_rl(type, right_rl);
358 if (!left_rl || !right_rl)
359 return NULL;
361 return rl_binop(left_rl, expr->op, right_rl);
364 static struct range_list *handle_right_shift(struct expression *expr, int implied, int *recurse_cnt)
366 struct range_list *left_rl;
367 sval_t right;
368 sval_t min, max;
370 if (implied == RL_EXACT || implied == RL_HARD)
371 return NULL;
373 left_rl = _get_rl(expr->left, implied, recurse_cnt);
374 if (left_rl) {
375 max = rl_max(left_rl);
376 min = rl_min(left_rl);
377 } else {
378 if (implied == RL_FUZZY)
379 return NULL;
380 max = sval_type_max(get_type(expr->left));
381 min = sval_type_val(get_type(expr->left), 0);
384 if (get_implied_value_internal(expr->right, &right, recurse_cnt)) {
385 min = sval_binop(min, SPECIAL_RIGHTSHIFT, right);
386 max = sval_binop(max, SPECIAL_RIGHTSHIFT, right);
387 } else if (!sval_is_negative(min)) {
388 min.value = 0;
389 max = sval_type_max(max.type);
390 } else {
391 return NULL;
394 return alloc_rl(min, max);
397 static struct range_list *handle_left_shift(struct expression *expr, int implied, int *recurse_cnt)
399 struct range_list *left_rl, *res;
400 sval_t right;
401 sval_t min, max;
402 int add_zero = 0;
404 if (implied == RL_EXACT || implied == RL_HARD)
405 return NULL;
406 /* this is hopeless without the right side */
407 if (!get_implied_value_internal(expr->right, &right, recurse_cnt))
408 return NULL;
409 left_rl = _get_rl(expr->left, implied, recurse_cnt);
410 if (left_rl) {
411 max = rl_max(left_rl);
412 min = rl_min(left_rl);
413 if (min.value == 0) {
414 min.value = 1;
415 add_zero = 1;
417 } else {
418 if (implied == RL_FUZZY)
419 return NULL;
420 max = sval_type_max(get_type(expr->left));
421 min = sval_type_val(get_type(expr->left), 1);
422 add_zero = 1;
425 max = sval_binop(max, SPECIAL_LEFTSHIFT, right);
426 min = sval_binop(min, SPECIAL_LEFTSHIFT, right);
427 res = alloc_rl(min, max);
428 if (add_zero)
429 res = rl_union(res, rl_zero());
430 return res;
433 static struct range_list *handle_known_binop(struct expression *expr)
435 sval_t left, right;
437 if (!get_value(expr->left, &left))
438 return NULL;
439 if (!get_value(expr->right, &right))
440 return NULL;
441 left = sval_binop(left, expr->op, right);
442 return alloc_rl(left, left);
445 static int has_actual_ranges(struct range_list *rl)
447 struct data_range *tmp;
449 FOR_EACH_PTR(rl, tmp) {
450 if (sval_cmp(tmp->min, tmp->max) != 0)
451 return 1;
452 } END_FOR_EACH_PTR(tmp);
453 return 0;
456 static struct range_list *handle_implied_binop(struct range_list *left_rl, int op, struct range_list *right_rl)
458 struct range_list *res_rl;
459 struct data_range *left_drange, *right_drange;
460 sval_t res;
462 if (!left_rl || !right_rl)
463 return NULL;
464 if (has_actual_ranges(left_rl))
465 return NULL;
466 if (has_actual_ranges(right_rl))
467 return NULL;
469 if (ptr_list_size((struct ptr_list *)left_rl) * ptr_list_size((struct ptr_list *)right_rl) > 20)
470 return NULL;
472 res_rl = NULL;
474 FOR_EACH_PTR(left_rl, left_drange) {
475 FOR_EACH_PTR(right_rl, right_drange) {
476 if ((op == '%' || op == '/') &&
477 right_drange->min.value == 0)
478 return NULL;
479 res = sval_binop(left_drange->min, op, right_drange->min);
480 add_range(&res_rl, res, res);
481 } END_FOR_EACH_PTR(right_drange);
482 } END_FOR_EACH_PTR(left_drange);
484 return res_rl;
487 static struct range_list *handle_binop_rl(struct expression *expr, int implied, int *recurse_cnt)
489 struct smatch_state *state;
490 struct symbol *type;
491 struct range_list *left_rl, *right_rl, *rl;
492 sval_t min, max;
494 rl = handle_known_binop(expr);
495 if (rl)
496 return rl;
497 if (implied == RL_EXACT)
498 return NULL;
500 state = get_extra_state(expr);
501 if (state && !is_whole_rl(estate_rl(state)))
502 return clone_rl(estate_rl(state));
504 type = get_type(expr);
505 left_rl = _get_rl(expr->left, implied, recurse_cnt);
506 left_rl = cast_rl(type, left_rl);
507 right_rl = _get_rl(expr->right, implied, recurse_cnt);
508 right_rl = cast_rl(type, right_rl);
510 if (!left_rl && !right_rl)
511 return NULL;
513 rl = handle_implied_binop(left_rl, expr->op, right_rl);
514 if (rl)
515 return rl;
517 switch (expr->op) {
518 case '%':
519 return handle_mod_rl(expr, implied, recurse_cnt);
520 case '&':
521 return handle_bitwise_AND(expr, implied, recurse_cnt);
522 case '|':
523 case '^':
524 return use_rl_binop(expr, implied, recurse_cnt);
525 case SPECIAL_RIGHTSHIFT:
526 return handle_right_shift(expr, implied, recurse_cnt);
527 case SPECIAL_LEFTSHIFT:
528 return handle_left_shift(expr, implied, recurse_cnt);
529 case '-':
530 return handle_subtract_rl(expr, implied, recurse_cnt);
531 case '/':
532 return handle_divide_rl(expr, implied, recurse_cnt);
535 if (!left_rl || !right_rl)
536 return NULL;
538 if (sval_binop_overflows(rl_min(left_rl), expr->op, rl_min(right_rl)))
539 return NULL;
540 if (sval_binop_overflows(rl_max(left_rl), expr->op, rl_max(right_rl)))
541 return NULL;
543 min = sval_binop(rl_min(left_rl), expr->op, rl_min(right_rl));
544 max = sval_binop(rl_max(left_rl), expr->op, rl_max(right_rl));
546 return alloc_rl(min, max);
549 static int do_comparison(struct expression *expr)
551 struct range_list *left_ranges = NULL;
552 struct range_list *right_ranges = NULL;
553 int poss_true, poss_false;
554 struct symbol *type;
556 type = get_type(expr);
557 get_absolute_rl(expr->left, &left_ranges);
558 get_absolute_rl(expr->right, &right_ranges);
560 left_ranges = cast_rl(type, left_ranges);
561 right_ranges = cast_rl(type, right_ranges);
563 poss_true = possibly_true_rl(left_ranges, expr->op, right_ranges);
564 poss_false = possibly_false_rl(left_ranges, expr->op, right_ranges);
566 free_rl(&left_ranges);
567 free_rl(&right_ranges);
569 if (!poss_true && !poss_false)
570 return 0x0;
571 if (poss_true && !poss_false)
572 return 0x1;
573 if (!poss_true && poss_false)
574 return 0x2;
575 return 0x3;
578 static struct range_list *handle_comparison_rl(struct expression *expr, int implied, int *recurse_cnt)
580 sval_t left, right;
581 int res;
583 if (expr->op == SPECIAL_EQUAL && expr->left->type == EXPR_TYPE) {
584 struct symbol *left, *right;
586 left = get_real_base_type(expr->left->symbol);
587 right = get_real_base_type(expr->left->symbol);
588 if (left == right)
589 return rl_one();
590 return rl_zero();
593 if (get_value(expr->left, &left) && get_value(expr->right, &right)) {
594 struct data_range tmp_left, tmp_right;
596 tmp_left.min = left;
597 tmp_left.max = left;
598 tmp_right.min = right;
599 tmp_right.max = right;
600 if (true_comparison_range(&tmp_left, expr->op, &tmp_right))
601 return rl_one();
602 return rl_zero();
605 if (implied == RL_EXACT)
606 return NULL;
608 res = do_comparison(expr);
609 if (res == 1)
610 return rl_one();
611 if (res == 2)
612 return rl_zero();
614 return alloc_rl(zero, one);
617 static struct range_list *handle_logical_rl(struct expression *expr, int implied, int *recurse_cnt)
619 sval_t left, right;
620 int left_known = 0;
621 int right_known = 0;
623 if (implied == RL_EXACT) {
624 if (get_value(expr->left, &left))
625 left_known = 1;
626 if (get_value(expr->right, &right))
627 right_known = 1;
628 } else {
629 if (get_implied_value_internal(expr->left, &left, recurse_cnt))
630 left_known = 1;
631 if (get_implied_value_internal(expr->right, &right, recurse_cnt))
632 right_known = 1;
635 switch (expr->op) {
636 case SPECIAL_LOGICAL_OR:
637 if (left_known && left.value)
638 return rl_one();
639 if (right_known && right.value)
640 return rl_one();
641 if (left_known && right_known)
642 return rl_zero();
643 break;
644 case SPECIAL_LOGICAL_AND:
645 if (left_known && right_known) {
646 if (left.value && right.value)
647 return rl_one();
648 return rl_zero();
650 break;
651 default:
652 return NULL;
655 if (implied == RL_EXACT)
656 return NULL;
658 return alloc_rl(zero, one);
661 static struct range_list *handle_conditional_rl(struct expression *expr, int implied, int *recurse_cnt)
663 struct range_list *true_rl, *false_rl;
664 struct symbol *type;
665 int final_pass_orig = final_pass;
667 if (known_condition_true(expr->conditional))
668 return _get_rl(expr->cond_true, implied, recurse_cnt);
669 if (known_condition_false(expr->conditional))
670 return _get_rl(expr->cond_false, implied, recurse_cnt);
672 if (implied == RL_EXACT)
673 return NULL;
675 if (implied_condition_true(expr->conditional))
676 return _get_rl(expr->cond_true, implied, recurse_cnt);
677 if (implied_condition_false(expr->conditional))
678 return _get_rl(expr->cond_false, implied, recurse_cnt);
681 /* this becomes a problem with deeply nested conditional statements */
682 if (low_on_memory())
683 return NULL;
685 type = get_type(expr);
687 __push_fake_cur_stree();
688 final_pass = 0;
689 __split_whole_condition(expr->conditional);
690 true_rl = _get_rl(expr->cond_true, implied, recurse_cnt);
691 __push_true_states();
692 __use_false_states();
693 false_rl = _get_rl(expr->cond_false, implied, recurse_cnt);
694 __merge_true_states();
695 __free_fake_cur_stree();
696 final_pass = final_pass_orig;
698 if (!true_rl || !false_rl)
699 return NULL;
700 true_rl = cast_rl(type, true_rl);
701 false_rl = cast_rl(type, false_rl);
703 return rl_union(true_rl, false_rl);
706 static int get_fuzzy_max_helper(struct expression *expr, sval_t *max)
708 struct smatch_state *state;
709 sval_t sval;
711 if (get_hard_max(expr, &sval)) {
712 *max = sval;
713 return 1;
716 state = get_extra_state(expr);
717 if (!state || !estate_has_fuzzy_max(state))
718 return 0;
719 *max = sval_cast(get_type(expr), estate_get_fuzzy_max(state));
720 return 1;
723 static int get_fuzzy_min_helper(struct expression *expr, sval_t *min)
725 struct smatch_state *state;
726 sval_t sval;
728 state = get_extra_state(expr);
729 if (!state || !estate_rl(state))
730 return 0;
732 sval = estate_min(state);
733 if (sval_is_negative(sval) && sval_is_min(sval))
734 return 0;
736 if (sval_is_max(sval))
737 return 0;
739 *min = sval_cast(get_type(expr), sval);
740 return 1;
743 int get_const_value(struct expression *expr, sval_t *sval)
745 struct symbol *sym;
746 sval_t right;
748 if (expr->type != EXPR_SYMBOL || !expr->symbol)
749 return 0;
750 sym = expr->symbol;
751 if (!(sym->ctype.modifiers & MOD_CONST))
752 return 0;
753 if (get_value(sym->initializer, &right)) {
754 *sval = sval_cast(get_type(expr), right);
755 return 1;
757 return 0;
760 struct range_list *var_to_absolute_rl(struct expression *expr)
762 struct smatch_state *state;
763 struct range_list *rl;
765 state = get_extra_state(expr);
766 if (!state || is_whole_rl(estate_rl(state))) {
767 state = get_real_absolute_state(expr);
768 if (state && state->data && !estate_is_whole(state))
769 return clone_rl(estate_rl(state));
770 if (get_local_rl(expr, &rl) && !is_whole_rl(rl))
771 return rl;
772 if (get_db_type_rl(expr, &rl) && !is_whole_rl(rl))
773 return rl;
774 return alloc_whole_rl(get_type(expr));
776 /* err on the side of saying things are possible */
777 if (!estate_rl(state))
778 return alloc_whole_rl(get_type(expr));
779 return clone_rl(estate_rl(state));
782 static struct range_list *handle_variable(struct expression *expr, int implied, int *recurse_cnt)
784 struct smatch_state *state;
785 struct range_list *rl;
786 sval_t sval, min, max;
788 if (get_const_value(expr, &sval))
789 return alloc_rl(sval, sval);
791 if (custom_handle_variable) {
792 rl = custom_handle_variable(expr);
793 if (!rl)
794 return var_to_absolute_rl(expr);
795 return rl;
798 switch (implied) {
799 case RL_EXACT:
800 return NULL;
801 case RL_HARD:
802 case RL_IMPLIED:
803 case RL_ABSOLUTE:
804 state = get_extra_state(expr);
805 if (!state || !state->data) {
806 if (implied == RL_HARD)
807 return NULL;
808 if (get_local_rl(expr, &rl))
809 return rl;
810 if (get_db_type_rl(expr, &rl))
811 return rl;
812 return NULL;
814 if (implied == RL_HARD && !estate_has_hard_max(state))
815 return NULL;
816 return clone_rl(estate_rl(state));
817 case RL_REAL_ABSOLUTE:
818 state = get_extra_state(expr);
819 if (!state || !state->data || is_whole_rl(estate_rl(state))) {
820 state = get_real_absolute_state(expr);
821 if (state && state->data)
822 return clone_rl(estate_rl(state));
823 if (get_local_rl(expr, &rl))
824 return rl;
825 if (get_db_type_rl(expr, &rl))
826 return rl;
827 return NULL;
829 return clone_rl(estate_rl(state));
830 case RL_FUZZY:
831 if (!get_fuzzy_min_helper(expr, &min))
832 min = sval_type_min(get_type(expr));
833 if (!get_fuzzy_max_helper(expr, &max))
834 return NULL;
835 /* fuzzy ranges are often inverted */
836 if (sval_cmp(min, max) > 0) {
837 sval = min;
838 min = max;
839 max = sval;
841 return alloc_rl(min, max);
843 return NULL;
846 static sval_t handle_sizeof(struct expression *expr)
848 struct symbol *sym;
849 sval_t ret;
851 ret = sval_blank(expr);
852 sym = expr->cast_type;
853 if (!sym) {
854 sym = evaluate_expression(expr->cast_expression);
856 * Expressions of restricted types will possibly get
857 * promoted - check that here
859 if (!sym)
860 sym = &int_ctype;
861 else if (is_restricted_type(sym)) {
862 if (type_bits(sym) < bits_in_int)
863 sym = &int_ctype;
864 } else if (is_fouled_type(sym)) {
865 sym = &int_ctype;
868 examine_symbol_type(sym);
870 ret.type = size_t_ctype;
871 if (type_bits(sym) <= 0) /* sizeof(void) */ {
872 if (get_real_base_type(sym) == &void_ctype)
873 ret.value = 1;
874 else
875 ret.value = 0;
876 } else
877 ret.value = type_bytes(sym);
879 return ret;
882 static struct range_list *handle_strlen(struct expression *expr, int implied, int *recurse_cnt)
884 struct range_list *rl;
885 struct expression *arg;
886 sval_t sval = { .type = &ulong_ctype };
888 if (implied == RL_EXACT)
889 return NULL;
891 arg = get_argument_from_call_expr(expr->args, 0);
892 if (!arg)
893 return NULL;
894 if (arg->type == EXPR_STRING) {
895 sval.value = arg->string->length - 1;
896 return alloc_rl(sval, sval);
899 if (implied == RL_HARD || implied == RL_FUZZY)
900 return NULL;
902 if (get_implied_return(expr, &rl))
903 return rl;
905 return NULL;
908 static struct range_list *handle_call_rl(struct expression *expr, int implied, int *recurse_cnt)
910 struct range_list *rl;
912 if (sym_name_is("__builtin_expect", expr->fn) ||
913 sym_name_is("__builtin_bswap16", expr->fn) ||
914 sym_name_is("__builtin_bswap32", expr->fn) ||
915 sym_name_is("__builtin_bswap64", expr->fn)) {
916 struct expression *arg;
918 arg = get_argument_from_call_expr(expr->args, 0);
919 return _get_rl(arg, implied, recurse_cnt);
922 if (sym_name_is("strlen", expr->fn))
923 return handle_strlen(expr, implied, recurse_cnt);
925 if (implied == RL_EXACT || implied == RL_HARD || implied == RL_FUZZY)
926 return NULL;
928 if (custom_handle_variable) {
929 rl = custom_handle_variable(expr);
930 if (rl)
931 return rl;
934 if (get_implied_return(expr, &rl))
935 return rl;
936 return db_return_vals(expr);
939 static struct range_list *handle_cast(struct expression *expr, int implied, int *recurse_cnt)
941 struct range_list *rl;
942 struct symbol *type;
944 type = get_type(expr);
945 rl = _get_rl(expr->cast_expression, implied, recurse_cnt);
946 if (rl)
947 return cast_rl(type, rl);
948 if (implied == RL_ABSOLUTE || implied == RL_REAL_ABSOLUTE)
949 return alloc_whole_rl(type);
950 if (implied == RL_IMPLIED && type &&
951 type_bits(type) > 0 && type_bits(type) < 32)
952 return alloc_whole_rl(type);
953 return NULL;
956 static struct range_list *_get_rl(struct expression *expr, int implied, int *recurse_cnt)
958 struct range_list *rl;
959 struct symbol *type;
960 sval_t sval;
962 type = get_type(expr);
963 expr = strip_parens(expr);
964 if (!expr)
965 return NULL;
967 if (++(*recurse_cnt) >= 200)
968 return NULL;
970 switch(expr->type) {
971 case EXPR_CAST:
972 case EXPR_FORCE_CAST:
973 case EXPR_IMPLIED_CAST:
974 rl = handle_cast(expr, implied, recurse_cnt);
975 goto out_cast;
978 expr = strip_expr(expr);
979 if (!expr)
980 return NULL;
982 switch (expr->type) {
983 case EXPR_VALUE:
984 sval = sval_from_val(expr, expr->value);
985 rl = alloc_rl(sval, sval);
986 break;
987 case EXPR_PREOP:
988 rl = handle_preop_rl(expr, implied, recurse_cnt);
989 break;
990 case EXPR_POSTOP:
991 rl = _get_rl(expr->unop, implied, recurse_cnt);
992 break;
993 case EXPR_BINOP:
994 rl = handle_binop_rl(expr, implied, recurse_cnt);
995 break;
996 case EXPR_COMPARE:
997 rl = handle_comparison_rl(expr, implied, recurse_cnt);
998 break;
999 case EXPR_LOGICAL:
1000 rl = handle_logical_rl(expr, implied, recurse_cnt);
1001 break;
1002 case EXPR_PTRSIZEOF:
1003 case EXPR_SIZEOF:
1004 sval = handle_sizeof(expr);
1005 rl = alloc_rl(sval, sval);
1006 break;
1007 case EXPR_SELECT:
1008 case EXPR_CONDITIONAL:
1009 rl = handle_conditional_rl(expr, implied, recurse_cnt);
1010 break;
1011 case EXPR_CALL:
1012 rl = handle_call_rl(expr, implied, recurse_cnt);
1013 break;
1014 default:
1015 rl = handle_variable(expr, implied, recurse_cnt);
1018 out_cast:
1019 if (rl)
1020 return rl;
1021 if (type && (implied == RL_ABSOLUTE || implied == RL_REAL_ABSOLUTE))
1022 return alloc_whole_rl(type);
1023 return NULL;
1026 /* returns 1 if it can get a value literal or else returns 0 */
1027 int get_value(struct expression *expr, sval_t *sval)
1029 struct range_list *rl;
1030 int recurse_cnt = 0;
1032 rl = _get_rl(expr, RL_EXACT, &recurse_cnt);
1033 if (!rl_to_sval(rl, sval))
1034 return 0;
1035 return 1;
1038 static int get_implied_value_internal(struct expression *expr, sval_t *sval, int *recurse_cnt)
1040 struct range_list *rl;
1042 rl = _get_rl(expr, RL_IMPLIED, recurse_cnt);
1043 if (!rl_to_sval(rl, sval))
1044 return 0;
1045 return 1;
1048 int get_implied_value(struct expression *expr, sval_t *sval)
1050 struct range_list *rl;
1051 int recurse_cnt = 0;
1053 rl = _get_rl(expr, RL_IMPLIED, &recurse_cnt);
1054 if (!rl_to_sval(rl, sval))
1055 return 0;
1056 return 1;
1059 int get_implied_min(struct expression *expr, sval_t *sval)
1061 struct range_list *rl;
1062 int recurse_cnt = 0;
1064 rl = _get_rl(expr, RL_IMPLIED, &recurse_cnt);
1065 if (!rl)
1066 return 0;
1067 *sval = rl_min(rl);
1068 return 1;
1071 int get_implied_max(struct expression *expr, sval_t *sval)
1073 struct range_list *rl;
1074 int recurse_cnt = 0;
1076 rl = _get_rl(expr, RL_IMPLIED, &recurse_cnt);
1077 if (!rl)
1078 return 0;
1079 *sval = rl_max(rl);
1080 return 1;
1083 int get_implied_rl(struct expression *expr, struct range_list **rl)
1085 int recurse_cnt = 0;
1087 *rl = _get_rl(expr, RL_IMPLIED, &recurse_cnt);
1088 if (*rl)
1089 return 1;
1090 return 0;
1093 static int get_absolute_rl_internal(struct expression *expr, struct range_list **rl, int *recurse_cnt)
1095 *rl = _get_rl(expr, RL_ABSOLUTE, recurse_cnt);
1096 if (!*rl)
1097 *rl = alloc_whole_rl(get_type(expr));
1098 return 1;
1101 int get_absolute_rl(struct expression *expr, struct range_list **rl)
1103 int recurse_cnt = 0;
1105 *rl = _get_rl(expr, RL_ABSOLUTE, &recurse_cnt);
1106 if (!*rl)
1107 *rl = alloc_whole_rl(get_type(expr));
1108 return 1;
1111 int get_real_absolute_rl(struct expression *expr, struct range_list **rl)
1113 int recurse_cnt = 0;
1115 *rl = _get_rl(expr, RL_REAL_ABSOLUTE, &recurse_cnt);
1116 if (!*rl)
1117 *rl = alloc_whole_rl(get_type(expr));
1118 return 1;
1121 int custom_get_absolute_rl(struct expression *expr,
1122 struct range_list *(*fn)(struct expression *expr),
1123 struct range_list **rl)
1125 int recurse_cnt = 0;
1127 *rl = NULL;
1128 custom_handle_variable = fn;
1129 *rl = _get_rl(expr, RL_REAL_ABSOLUTE, &recurse_cnt);
1130 custom_handle_variable = NULL;
1131 return 1;
1134 int get_implied_rl_var_sym(const char *var, struct symbol *sym, struct range_list **rl)
1136 struct smatch_state *state;
1138 state = get_state(SMATCH_EXTRA, var, sym);
1139 *rl = estate_rl(state);
1140 if (*rl)
1141 return 1;
1142 return 0;
1145 int get_hard_max(struct expression *expr, sval_t *sval)
1147 struct range_list *rl;
1148 int recurse_cnt = 0;
1150 rl = _get_rl(expr, RL_HARD, &recurse_cnt);
1151 if (!rl)
1152 return 0;
1153 *sval = rl_max(rl);
1154 return 1;
1157 int get_fuzzy_min(struct expression *expr, sval_t *sval)
1159 struct range_list *rl;
1160 sval_t tmp;
1161 int recurse_cnt = 0;
1163 rl = _get_rl(expr, RL_FUZZY, &recurse_cnt);
1164 if (!rl)
1165 return 0;
1166 tmp = rl_min(rl);
1167 if (sval_is_negative(tmp) && sval_is_min(tmp))
1168 return 0;
1169 *sval = tmp;
1170 return 1;
1173 int get_fuzzy_max(struct expression *expr, sval_t *sval)
1175 struct range_list *rl;
1176 sval_t max;
1177 int recurse_cnt = 0;
1179 rl = _get_rl(expr, RL_FUZZY, &recurse_cnt);
1180 if (!rl)
1181 return 0;
1182 max = rl_max(rl);
1183 if (max.uvalue > INT_MAX - 10000)
1184 return 0;
1185 *sval = max;
1186 return 1;
1189 int get_absolute_min(struct expression *expr, sval_t *sval)
1191 struct range_list *rl;
1192 struct symbol *type;
1193 int recurse_cnt = 0;
1195 type = get_type(expr);
1196 if (!type)
1197 type = &llong_ctype; // FIXME: this is wrong but places assume get type can't fail.
1198 rl = _get_rl(expr, RL_ABSOLUTE, &recurse_cnt);
1199 if (rl)
1200 *sval = rl_min(rl);
1201 else
1202 *sval = sval_type_min(type);
1204 if (sval_cmp(*sval, sval_type_min(type)) < 0)
1205 *sval = sval_type_min(type);
1206 return 1;
1209 int get_absolute_max(struct expression *expr, sval_t *sval)
1211 struct range_list *rl;
1212 struct symbol *type;
1213 int recurse_cnt = 0;
1215 type = get_type(expr);
1216 if (!type)
1217 type = &llong_ctype;
1218 rl = _get_rl(expr, RL_ABSOLUTE, &recurse_cnt);
1219 if (rl)
1220 *sval = rl_max(rl);
1221 else
1222 *sval = sval_type_max(type);
1224 if (sval_cmp(sval_type_max(type), *sval) < 0)
1225 *sval = sval_type_max(type);
1226 return 1;
1229 int known_condition_true(struct expression *expr)
1231 sval_t tmp;
1233 if (!expr)
1234 return 0;
1236 if (get_value(expr, &tmp) && tmp.value)
1237 return 1;
1239 return 0;
1242 int known_condition_false(struct expression *expr)
1244 if (!expr)
1245 return 0;
1247 if (is_zero(expr))
1248 return 1;
1250 if (expr->type == EXPR_CALL) {
1251 if (sym_name_is("__builtin_constant_p", expr->fn))
1252 return 1;
1254 return 0;
1257 int implied_condition_true(struct expression *expr)
1259 sval_t tmp;
1261 if (!expr)
1262 return 0;
1264 if (known_condition_true(expr))
1265 return 1;
1266 if (get_implied_value(expr, &tmp) && tmp.value)
1267 return 1;
1269 if (expr->type == EXPR_POSTOP)
1270 return implied_condition_true(expr->unop);
1272 if (expr->type == EXPR_PREOP && expr->op == SPECIAL_DECREMENT)
1273 return implied_not_equal(expr->unop, 1);
1274 if (expr->type == EXPR_PREOP && expr->op == SPECIAL_INCREMENT)
1275 return implied_not_equal(expr->unop, -1);
1277 expr = strip_expr(expr);
1278 switch (expr->type) {
1279 case EXPR_COMPARE:
1280 if (do_comparison(expr) == 1)
1281 return 1;
1282 break;
1283 case EXPR_PREOP:
1284 if (expr->op == '!') {
1285 if (implied_condition_false(expr->unop))
1286 return 1;
1287 break;
1289 break;
1290 default:
1291 if (implied_not_equal(expr, 0) == 1)
1292 return 1;
1293 break;
1295 return 0;
1298 int implied_condition_false(struct expression *expr)
1300 struct expression *tmp;
1301 sval_t sval;
1303 if (!expr)
1304 return 0;
1306 if (known_condition_false(expr))
1307 return 1;
1309 switch (expr->type) {
1310 case EXPR_COMPARE:
1311 if (do_comparison(expr) == 2)
1312 return 1;
1313 case EXPR_PREOP:
1314 if (expr->op == '!') {
1315 if (implied_condition_true(expr->unop))
1316 return 1;
1317 break;
1319 tmp = strip_expr(expr);
1320 if (tmp != expr)
1321 return implied_condition_false(tmp);
1322 break;
1323 default:
1324 if (get_implied_value(expr, &sval) && sval.value == 0)
1325 return 1;
1326 break;
1328 return 0;
1331 int can_integer_overflow(struct symbol *type, struct expression *expr)
1333 int op;
1334 sval_t lmax, rmax, res;
1336 if (!type)
1337 type = &int_ctype;
1339 expr = strip_expr(expr);
1341 if (expr->type == EXPR_ASSIGNMENT) {
1342 switch(expr->op) {
1343 case SPECIAL_MUL_ASSIGN:
1344 op = '*';
1345 break;
1346 case SPECIAL_ADD_ASSIGN:
1347 op = '+';
1348 break;
1349 case SPECIAL_SHL_ASSIGN:
1350 op = SPECIAL_LEFTSHIFT;
1351 break;
1352 default:
1353 return 0;
1355 } else if (expr->type == EXPR_BINOP) {
1356 if (expr->op != '*' && expr->op != '+' && expr->op != SPECIAL_LEFTSHIFT)
1357 return 0;
1358 op = expr->op;
1359 } else {
1360 return 0;
1363 get_absolute_max(expr->left, &lmax);
1364 get_absolute_max(expr->right, &rmax);
1366 if (sval_binop_overflows(lmax, op, rmax))
1367 return 1;
1369 res = sval_binop(lmax, op, rmax);
1370 if (sval_cmp(res, sval_type_max(type)) > 0)
1371 return 1;
1372 return 0;