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
23 #include "smatch_slist.h"
24 #include "smatch_extra.h"
26 static bool get_rl_sval(struct expression
*expr
, int implied
, int *recurse_cnt
, struct range_list
**res
, sval_t
*sval_res
);
27 static bool get_rl_internal(struct expression
*expr
, int implied
, int *recurse_cnt
, struct range_list
**res
);
28 static bool handle_variable(struct expression
*expr
, int implied
, int *recurse_cnt
, struct range_list
**res
, sval_t
*res_sval
);
29 static struct range_list
*(*custom_handle_variable
)(struct expression
*expr
);
31 static bool get_implied_value_internal(struct expression
*expr
, int *recurse_cnt
, sval_t
*res_sval
);
32 static int get_absolute_rl_internal(struct expression
*expr
, struct range_list
**rl
, int *recurse_cnt
);
34 static sval_t zero
= {.type
= &int_ctype
, {.value
= 0} };
35 static sval_t one
= {.type
= &int_ctype
, {.value
= 1} };
37 static int fast_math_only
;
39 struct range_list
*rl_zero(void)
41 static struct range_list
*zero_perm
;
44 zero_perm
= clone_rl_permanent(alloc_rl(zero
, zero
));
48 struct range_list
*rl_one(void)
50 static struct range_list
*one_perm
;
53 one_perm
= clone_rl_permanent(alloc_rl(one
, one
));
67 static bool last_stmt_rl(struct statement
*stmt
, int implied
, int *recurse_cnt
, struct range_list
**res
, sval_t
*res_sval
)
69 struct expression
*expr
;
74 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
75 if (stmt
->type
== STMT_LABEL
) {
76 if (stmt
->label_statement
&&
77 stmt
->label_statement
->type
== STMT_EXPRESSION
)
78 expr
= stmt
->label_statement
->expression
;
81 } else if (stmt
->type
== STMT_EXPRESSION
) {
82 expr
= stmt
->expression
;
86 return get_rl_sval(expr
, implied
, recurse_cnt
, res
, res_sval
);
89 static bool handle_expression_statement_rl(struct expression
*expr
, int implied
,
90 int *recurse_cnt
, struct range_list
**res
, sval_t
*res_sval
)
92 return last_stmt_rl(get_expression_statement(expr
), implied
, recurse_cnt
, res
, res_sval
);
95 static bool handle_address(struct expression
*expr
, int implied
, int *recurse_cnt
, struct range_list
**res
, sval_t
*res_sval
)
97 struct range_list
*rl
;
103 if (implied
== RL_EXACT
)
106 if (custom_handle_variable
) {
107 rl
= custom_handle_variable(expr
);
115 if (get_mtag_sval(expr
, &sval
)) {
121 if (get_address_rl(expr
, res
)) {
129 static bool handle_ampersand_rl(struct expression
*expr
, int implied
, int *recurse_cnt
, struct range_list
**res
, sval_t
*res_sval
)
131 return handle_address(expr
, implied
, recurse_cnt
, res
, res_sval
);
134 static bool handle_negate_rl(struct expression
*expr
, int implied
, int *recurse_cnt
, struct range_list
**res
, sval_t
*res_sval
)
136 if (known_condition_true(expr
->unop
)) {
140 if (known_condition_false(expr
->unop
)) {
145 if (implied
== RL_EXACT
)
148 if (implied_condition_true(expr
->unop
)) {
152 if (implied_condition_false(expr
->unop
)) {
157 *res
= alloc_rl(zero
, one
);
161 static bool handle_bitwise_negate(struct expression
*expr
, int implied
, int *recurse_cnt
, sval_t
*res_sval
)
163 struct range_list
*rl
;
166 if (!get_rl_sval(expr
->unop
, implied
, recurse_cnt
, &rl
, &sval
))
168 if (!sval
.type
&& !rl_to_sval(rl
, &sval
))
170 sval
= sval_preop(sval
, '~');
171 sval_cast(get_type(expr
->unop
), sval
);
176 static bool untrusted_type_min(struct expression
*expr
)
178 struct range_list
*rl
;
180 rl
= var_user_rl(expr
);
181 return rl
&& sval_is_min(rl_min(rl
));
184 static bool handle_minus_preop(struct expression
*expr
, int implied
, int *recurse_cnt
, struct range_list
**res
, sval_t
*res_sval
)
186 struct range_list
*rl
;
187 struct range_list
*ret
= NULL
;
189 sval_t neg_one
= { .value
= -1 };
190 sval_t zero
= { .value
= 0 };
193 if (!get_rl_sval(expr
->unop
, implied
, recurse_cnt
, &rl
, &sval
))
196 *res_sval
= sval_preop(sval
, '-');
200 * One complication is that -INT_MIN is still INT_MIN because of integer
201 * overflows... But how many times do we set a time out to INT_MIN?
202 * So normally when we call abs() then it does return a positive value.
206 neg_one
.type
= zero
.type
= type
;
208 if (sval_is_negative(rl_min(rl
))) {
209 struct range_list
*neg
;
210 struct data_range
*drange
;
211 sval_t new_min
, new_max
;
213 neg
= alloc_rl(sval_type_min(type
), neg_one
);
214 neg
= rl_intersection(rl
, neg
);
216 if (sval_is_min(rl_min(neg
)) && !sval_is_min(rl_max(neg
)))
217 neg
= remove_range(neg
, sval_type_min(type
), sval_type_min(type
));
219 FOR_EACH_PTR(neg
, drange
) {
220 new_min
= drange
->max
;
221 new_min
.value
= -new_min
.value
;
222 new_max
= drange
->min
;
223 new_max
.value
= -new_max
.value
;
224 add_range(&ret
, new_min
, new_max
);
225 } END_FOR_EACH_PTR(drange
);
227 if (untrusted_type_min(expr
))
228 add_range(&ret
, sval_type_min(type
), sval_type_min(type
));
231 if (!sval_is_negative(rl_max(rl
))) {
232 struct range_list
*pos
;
233 struct data_range
*drange
;
234 sval_t new_min
, new_max
;
236 pos
= alloc_rl(zero
, sval_type_max(type
));
237 pos
= rl_intersection(rl
, pos
);
239 FOR_EACH_PTR(pos
, drange
) {
240 new_min
= drange
->max
;
241 new_min
.value
= -new_min
.value
;
242 new_max
= drange
->min
;
243 new_max
.value
= -new_max
.value
;
244 add_range(&ret
, new_min
, new_max
);
245 } END_FOR_EACH_PTR(drange
);
252 static bool handle_preop_rl(struct expression
*expr
, int implied
, int *recurse_cnt
, struct range_list
**res
, sval_t
*res_sval
)
256 return handle_ampersand_rl(expr
, implied
, recurse_cnt
, res
, res_sval
);
258 return handle_negate_rl(expr
, implied
, recurse_cnt
, res
, res_sval
);
260 return handle_bitwise_negate(expr
, implied
, recurse_cnt
, res_sval
);
262 return handle_minus_preop(expr
, implied
, recurse_cnt
, res
, res_sval
);
264 return handle_variable(expr
, implied
, recurse_cnt
, res
, res_sval
);
266 return handle_expression_statement_rl(expr
, implied
, recurse_cnt
, res
, res_sval
);
272 static bool handle_divide_rl(struct expression
*expr
, int implied
, int *recurse_cnt
, struct range_list
**res
)
274 struct range_list
*left_rl
= NULL
;
275 struct range_list
*right_rl
= NULL
;
278 type
= get_type(expr
);
280 get_rl_internal(expr
->left
, implied
, recurse_cnt
, &left_rl
);
281 left_rl
= cast_rl(type
, left_rl
);
282 get_rl_internal(expr
->right
, implied
, recurse_cnt
, &right_rl
);
283 right_rl
= cast_rl(type
, right_rl
);
285 if (!left_rl
|| !right_rl
)
288 if (implied
!= RL_REAL_ABSOLUTE
) {
289 if (is_whole_rl(left_rl
) || is_whole_rl(right_rl
))
293 *res
= rl_binop(left_rl
, '/', right_rl
);
297 static int handle_offset_subtraction(struct expression
*expr
)
299 struct expression
*left
, *right
;
300 struct symbol
*left_sym
, *right_sym
;
302 int left_offset
, right_offset
;
304 type
= get_type(expr
);
305 if (!type
|| type
->type
!= SYM_PTR
)
307 type
= get_real_base_type(type
);
308 if (!type
|| (type_bits(type
) != 8 && (type
!= &void_ctype
)))
311 left
= strip_expr(expr
->left
);
312 right
= strip_expr(expr
->right
);
314 if (left
->type
!= EXPR_PREOP
|| left
->op
!= '&')
316 left
= strip_expr(left
->unop
);
318 left_sym
= expr_to_sym(left
);
319 right_sym
= expr_to_sym(right
);
320 if (!left_sym
|| left_sym
!= right_sym
)
323 left_offset
= get_member_offset_from_deref(left
);
324 if (right
->type
== EXPR_SYMBOL
)
327 if (right
->type
!= EXPR_PREOP
|| right
->op
!= '&')
329 right
= strip_expr(right
->unop
);
330 right_offset
= get_member_offset_from_deref(right
);
332 if (left_offset
< 0 || right_offset
< 0)
335 return left_offset
- right_offset
;
338 static bool max_is_unknown_max(struct range_list
*rl
)
341 * The issue with this code is that we had:
342 * if (foo > 1) return 1 - foo;
343 * Ideally we would say that returns s32min-(-1) but what Smatch
344 * was saying was that the lowest possible value was "1 - INT_MAX"
346 * My solution is to ignore max values for int or larger. I keep
347 * the max for shorts etc, because those might be worthwhile.
349 * The problem with just returning 1 - INT_MAX is that that is
350 * treated as useful information but s32min is treated as basically
354 if (type_bits(rl_type(rl
)) < 31)
356 return sval_is_max(rl_max(rl
));
359 static bool handle_add_rl(struct expression
*expr
, int implied
, int *recurse_cnt
, struct range_list
**res
)
361 struct range_list
*left_rl
= NULL
;
362 struct range_list
*right_rl
= NULL
;
363 struct range_list
*valid
;
367 type
= get_type(expr
);
369 get_rl_internal(expr
->left
, implied
, recurse_cnt
, &left_rl
);
370 left_rl
= cast_rl(type
, left_rl
);
371 get_rl_internal(expr
->right
, implied
, recurse_cnt
, &right_rl
);
372 right_rl
= cast_rl(type
, right_rl
);
377 if (type_is_ptr(type
) && !var_user_rl(expr
->right
)) {
378 valid
= rl_intersection(left_rl
, valid_ptr_rl
);
379 if (valid
&& rl_equiv(valid
, left_rl
))
386 if (sval_binop_overflows(rl_min(left_rl
), expr
->op
, rl_min(right_rl
)))
388 if (sval_binop_overflows(rl_max(left_rl
), expr
->op
, rl_max(right_rl
)))
391 min
= sval_binop(rl_min(left_rl
), expr
->op
, rl_min(right_rl
));
392 max
= sval_binop(rl_max(left_rl
), expr
->op
, rl_max(right_rl
));
394 *res
= alloc_rl(min
, max
);
398 static bool handle_subtract_rl(struct expression
*expr
, int implied
, int *recurse_cnt
, struct range_list
**res
)
401 struct range_list
*left_orig
, *right_orig
;
402 struct range_list
*left_rl
, *right_rl
;
403 sval_t min
, max
, tmp
;
407 type
= get_type(expr
);
409 offset
= handle_offset_subtraction(expr
);
414 *res
= alloc_rl(tmp
, tmp
);
418 comparison
= get_comparison(expr
->left
, expr
->right
);
421 get_rl_internal(expr
->left
, implied
, recurse_cnt
, &left_orig
);
422 left_rl
= cast_rl(type
, left_orig
);
424 get_rl_internal(expr
->right
, implied
, recurse_cnt
, &right_orig
);
425 right_rl
= cast_rl(type
, right_orig
);
427 if ((!left_rl
|| !right_rl
) &&
428 (implied
== RL_EXACT
|| implied
== RL_HARD
|| implied
== RL_FUZZY
))
432 left_rl
= alloc_whole_rl(type
);
434 right_rl
= alloc_whole_rl(type
);
436 /* negative values complicate everything fix this later */
437 if (sval_is_negative(rl_min(right_rl
)))
439 max
= rl_max(left_rl
);
440 min
= sval_type_min(type
);
442 switch (comparison
) {
444 case SPECIAL_UNSIGNED_GT
:
445 min
= sval_type_val(type
, 1);
446 max
= rl_max(left_rl
);
449 case SPECIAL_UNSIGNED_GTE
:
450 min
= sval_type_val(type
, 0);
451 max
= rl_max(left_rl
);
454 min
= sval_type_val(type
, 0);
455 max
= sval_type_val(type
, 0);
458 case SPECIAL_UNSIGNED_LT
:
459 max
= sval_type_val(type
, -1);
462 case SPECIAL_UNSIGNED_LTE
:
463 max
= sval_type_val(type
, 0);
466 if (!left_orig
|| !right_orig
)
468 *res
= rl_binop(left_rl
, '-', right_rl
);
472 if (!max_is_unknown_max(right_rl
) &&
473 !sval_binop_overflows(rl_min(left_rl
), '-', rl_max(right_rl
))) {
474 tmp
= sval_binop(rl_min(left_rl
), '-', rl_max(right_rl
));
475 if (sval_cmp(tmp
, min
) > 0)
479 if (!sval_is_max(rl_max(left_rl
))) {
480 tmp
= sval_binop(rl_max(left_rl
), '-', rl_min(right_rl
));
481 if (sval_cmp(tmp
, max
) < 0)
485 if (sval_is_min(min
) && sval_is_max(max
))
488 *res
= cast_rl(type
, alloc_rl(min
, max
));
492 static bool handle_mod_rl(struct expression
*expr
, int implied
, int *recurse_cnt
, struct range_list
**res
)
494 struct range_list
*rl
;
495 sval_t left
, right
, sval
;
497 if (implied
== RL_EXACT
) {
498 if (!get_implied_value(expr
->right
, &right
))
500 if (!get_implied_value(expr
->left
, &left
))
502 sval
= sval_binop(left
, '%', right
);
503 *res
= alloc_rl(sval
, sval
);
506 /* if we can't figure out the right side it's probably hopeless */
507 if (!get_implied_value_internal(expr
->right
, recurse_cnt
, &right
))
510 right
= sval_cast(get_type(expr
), right
);
513 if (get_rl_internal(expr
->left
, implied
, recurse_cnt
, &rl
) && rl
&&
514 rl_max(rl
).uvalue
< right
.uvalue
)
515 right
.uvalue
= rl_max(rl
).uvalue
;
517 *res
= alloc_rl(sval_cast(right
.type
, zero
), right
);
521 static bool handle_bitwise_AND(struct expression
*expr
, int implied
, int *recurse_cnt
, struct range_list
**res
)
524 struct range_list
*left_rl
, *right_rl
;
527 if (implied
!= RL_IMPLIED
&& implied
!= RL_ABSOLUTE
&& implied
!= RL_REAL_ABSOLUTE
)
530 type
= get_type(expr
);
532 if (!get_rl_internal(expr
->left
, implied
, recurse_cnt
, &left_rl
))
533 left_rl
= alloc_whole_rl(type
);
534 left_rl
= cast_rl(type
, left_rl
);
536 new_recurse
= *recurse_cnt
;
537 if (*recurse_cnt
>= 200)
538 new_recurse
= 100; /* Let's try super hard to get the mask */
539 if (!get_rl_internal(expr
->right
, implied
, &new_recurse
, &right_rl
))
540 right_rl
= alloc_whole_rl(type
);
541 right_rl
= cast_rl(type
, right_rl
);
542 *recurse_cnt
= new_recurse
;
544 *res
= rl_binop(left_rl
, '&', right_rl
);
548 static bool use_rl_binop(struct expression
*expr
, int implied
, int *recurse_cnt
, struct range_list
**res
)
551 struct range_list
*left_rl
, *right_rl
;
553 if (implied
!= RL_IMPLIED
&& implied
!= RL_ABSOLUTE
&& implied
!= RL_REAL_ABSOLUTE
)
556 type
= get_type(expr
);
558 get_absolute_rl_internal(expr
->left
, &left_rl
, recurse_cnt
);
559 get_absolute_rl_internal(expr
->right
, &right_rl
, recurse_cnt
);
560 left_rl
= cast_rl(type
, left_rl
);
561 right_rl
= cast_rl(type
, right_rl
);
562 if (!left_rl
|| !right_rl
)
565 *res
= rl_binop(left_rl
, expr
->op
, right_rl
);
569 static bool handle_right_shift(struct expression
*expr
, int implied
, int *recurse_cnt
, struct range_list
**res
)
571 struct range_list
*left_rl
, *right_rl
;
574 if (implied
== RL_EXACT
|| implied
== RL_HARD
)
577 if (get_rl_internal(expr
->left
, implied
, recurse_cnt
, &left_rl
)) {
578 max
= rl_max(left_rl
);
579 min
= rl_min(left_rl
);
581 if (implied
== RL_FUZZY
)
583 max
= sval_type_max(get_type(expr
->left
));
584 min
= sval_type_val(get_type(expr
->left
), 0);
587 if (get_rl_internal(expr
->right
, implied
, recurse_cnt
, &right_rl
) &&
588 !sval_is_negative(rl_min(right_rl
))) {
589 min
= sval_binop(min
, SPECIAL_RIGHTSHIFT
, rl_max(right_rl
));
590 max
= sval_binop(max
, SPECIAL_RIGHTSHIFT
, rl_min(right_rl
));
591 } else if (!sval_is_negative(min
)) {
593 max
= sval_type_max(max
.type
);
598 *res
= alloc_rl(min
, max
);
602 static bool handle_left_shift(struct expression
*expr
, int implied
, int *recurse_cnt
, struct range_list
**res
)
604 struct range_list
*left_rl
, *rl
;
607 if (implied
== RL_EXACT
|| implied
== RL_HARD
)
609 /* this is hopeless without the right side */
610 if (!get_implied_value_internal(expr
->right
, recurse_cnt
, &right
))
612 if (!get_rl_internal(expr
->left
, implied
, recurse_cnt
, &left_rl
)) {
613 if (implied
== RL_FUZZY
)
615 left_rl
= alloc_whole_rl(get_type(expr
->left
));
618 rl
= rl_binop(left_rl
, SPECIAL_LEFTSHIFT
, alloc_rl(right
, right
));
625 static bool handle_known_binop(struct expression
*expr
, sval_t
*res
)
629 if (!get_value(expr
->left
, &left
))
631 if (!get_value(expr
->right
, &right
))
633 *res
= sval_binop(left
, expr
->op
, right
);
637 static int has_actual_ranges(struct range_list
*rl
)
639 struct data_range
*tmp
;
641 FOR_EACH_PTR(rl
, tmp
) {
642 if (sval_cmp(tmp
->min
, tmp
->max
) != 0)
644 } END_FOR_EACH_PTR(tmp
);
648 static struct range_list
*handle_implied_binop(struct range_list
*left_rl
, int op
, struct range_list
*right_rl
)
650 struct range_list
*res_rl
;
651 struct data_range
*left_drange
, *right_drange
;
654 if (!left_rl
|| !right_rl
)
656 if (has_actual_ranges(left_rl
))
658 if (has_actual_ranges(right_rl
))
661 if (ptr_list_size((struct ptr_list
*)left_rl
) * ptr_list_size((struct ptr_list
*)right_rl
) > 20)
666 FOR_EACH_PTR(left_rl
, left_drange
) {
667 FOR_EACH_PTR(right_rl
, right_drange
) {
668 if ((op
== '%' || op
== '/') &&
669 right_drange
->min
.value
== 0)
671 res
= sval_binop(left_drange
->min
, op
, right_drange
->min
);
672 add_range(&res_rl
, res
, res
);
673 } END_FOR_EACH_PTR(right_drange
);
674 } END_FOR_EACH_PTR(left_drange
);
679 static bool handle_binop_rl_helper(struct expression
*expr
, int implied
, int *recurse_cnt
, struct range_list
**res
, sval_t
*res_sval
)
682 struct range_list
*left_rl
= NULL
;
683 struct range_list
*right_rl
= NULL
;
684 struct range_list
*rl
;
687 type
= get_promoted_type(get_type(expr
->left
), get_type(expr
->right
));
688 get_rl_internal(expr
->left
, implied
, recurse_cnt
, &left_rl
);
689 left_rl
= cast_rl(type
, left_rl
);
690 get_rl_internal(expr
->right
, implied
, recurse_cnt
, &right_rl
);
691 right_rl
= cast_rl(type
, right_rl
);
693 rl
= handle_implied_binop(left_rl
, expr
->op
, right_rl
);
701 return handle_mod_rl(expr
, implied
, recurse_cnt
, res
);
703 return handle_bitwise_AND(expr
, implied
, recurse_cnt
, res
);
706 return use_rl_binop(expr
, implied
, recurse_cnt
, res
);
707 case SPECIAL_RIGHTSHIFT
:
708 return handle_right_shift(expr
, implied
, recurse_cnt
, res
);
709 case SPECIAL_LEFTSHIFT
:
710 return handle_left_shift(expr
, implied
, recurse_cnt
, res
);
712 return handle_add_rl(expr
, implied
, recurse_cnt
, res
);
714 return handle_subtract_rl(expr
, implied
, recurse_cnt
, res
);
716 return handle_divide_rl(expr
, implied
, recurse_cnt
, res
);
719 if (!left_rl
|| !right_rl
)
722 if (sval_binop_overflows(rl_min(left_rl
), expr
->op
, rl_min(right_rl
)))
724 if (sval_binop_overflows(rl_max(left_rl
), expr
->op
, rl_max(right_rl
)))
727 min
= sval_binop(rl_min(left_rl
), expr
->op
, rl_min(right_rl
));
728 max
= sval_binop(rl_max(left_rl
), expr
->op
, rl_max(right_rl
));
730 *res
= alloc_rl(min
, max
);
735 static bool handle_binop_rl(struct expression
*expr
, int implied
, int *recurse_cnt
, struct range_list
**res
, sval_t
*res_sval
)
737 struct smatch_state
*state
;
738 struct range_list
*rl
;
741 if (handle_known_binop(expr
, &val
)) {
745 if (implied
== RL_EXACT
)
748 if (custom_handle_variable
) {
749 rl
= custom_handle_variable(expr
);
756 state
= get_extra_state(expr
);
757 if (state
&& !is_whole_rl(estate_rl(state
))) {
758 if (implied
!= RL_HARD
|| estate_has_hard_max(state
)) {
759 *res
= clone_rl(estate_rl(state
));
764 return handle_binop_rl_helper(expr
, implied
, recurse_cnt
, res
, res_sval
);
767 static int do_comparison(struct expression
*expr
)
769 struct range_list
*left_ranges
= NULL
;
770 struct range_list
*right_ranges
= NULL
;
771 int poss_true
, poss_false
;
774 type
= get_type(expr
);
775 get_absolute_rl(expr
->left
, &left_ranges
);
776 get_absolute_rl(expr
->right
, &right_ranges
);
778 left_ranges
= cast_rl(type
, left_ranges
);
779 right_ranges
= cast_rl(type
, right_ranges
);
781 poss_true
= possibly_true_rl(left_ranges
, expr
->op
, right_ranges
);
782 poss_false
= possibly_false_rl(left_ranges
, expr
->op
, right_ranges
);
784 if (!poss_true
&& !poss_false
)
786 if (poss_true
&& !poss_false
)
788 if (!poss_true
&& poss_false
)
793 static bool handle_comparison_rl(struct expression
*expr
, int implied
, int *recurse_cnt
, struct range_list
**res
, sval_t
*res_sval
)
798 if (expr
->op
== SPECIAL_EQUAL
&& expr
->left
->type
== EXPR_TYPE
) {
799 struct symbol
*left
, *right
;
801 if (expr
->right
->type
!= EXPR_TYPE
)
804 left
= get_real_base_type(expr
->left
->symbol
);
805 right
= get_real_base_type(expr
->right
->symbol
);
806 if (type_bits(left
) == type_bits(right
) &&
807 type_positive_bits(left
) == type_positive_bits(right
))
814 if (get_value(expr
->left
, &left
) && get_value(expr
->right
, &right
)) {
815 struct data_range tmp_left
, tmp_right
;
819 tmp_right
.min
= right
;
820 tmp_right
.max
= right
;
821 if (true_comparison_range(&tmp_left
, expr
->op
, &tmp_right
))
828 if (implied
== RL_EXACT
)
831 cmp
= do_comparison(expr
);
841 *res
= alloc_rl(zero
, one
);
845 static bool handle_logical_rl(struct expression
*expr
, int implied
, int *recurse_cnt
, struct range_list
**res
, sval_t
*res_sval
)
851 if (implied
== RL_EXACT
) {
852 if (get_value(expr
->left
, &left
))
854 if (get_value(expr
->right
, &right
))
857 if (get_implied_value_internal(expr
->left
, recurse_cnt
, &left
))
859 if (get_implied_value_internal(expr
->right
, recurse_cnt
, &right
))
864 case SPECIAL_LOGICAL_OR
:
865 if (left_known
&& left
.value
)
867 if (right_known
&& right
.value
)
869 if (left_known
&& right_known
)
872 case SPECIAL_LOGICAL_AND
:
873 if (left_known
&& right_known
) {
874 if (left
.value
&& right
.value
)
883 if (implied
== RL_EXACT
)
886 *res
= alloc_rl(zero
, one
);
897 static bool handle_conditional_rl(struct expression
*expr
, int implied
, int *recurse_cnt
, struct range_list
**res
, sval_t
*res_sval
)
899 struct expression
*cond_true
;
900 struct range_list
*true_rl
, *false_rl
;
902 int final_pass_orig
= final_pass
;
904 cond_true
= expr
->cond_true
;
906 cond_true
= expr
->conditional
;
908 if (known_condition_true(expr
->conditional
))
909 return get_rl_sval(cond_true
, implied
, recurse_cnt
, res
, res_sval
);
910 if (known_condition_false(expr
->conditional
))
911 return get_rl_sval(expr
->cond_false
, implied
, recurse_cnt
, res
, res_sval
);
913 if (implied
== RL_EXACT
)
916 if (implied_condition_true(expr
->conditional
))
917 return get_rl_sval(cond_true
, implied
, recurse_cnt
, res
, res_sval
);
918 if (implied_condition_false(expr
->conditional
))
919 return get_rl_sval(expr
->cond_false
, implied
, recurse_cnt
, res
, res_sval
);
921 /* this becomes a problem with deeply nested conditional statements */
922 if (fast_math_only
|| low_on_memory())
925 type
= get_type(expr
);
927 __push_fake_cur_stree();
929 __split_whole_condition(expr
->conditional
);
931 get_rl_internal(cond_true
, implied
, recurse_cnt
, &true_rl
);
932 __push_true_states();
933 __use_false_states();
935 get_rl_internal(expr
->cond_false
, implied
, recurse_cnt
, &false_rl
);
936 __merge_true_states();
937 __free_fake_cur_stree();
938 final_pass
= final_pass_orig
;
940 if (!true_rl
|| !false_rl
)
942 true_rl
= cast_rl(type
, true_rl
);
943 false_rl
= cast_rl(type
, false_rl
);
945 *res
= rl_union(true_rl
, false_rl
);
949 static bool get_fuzzy_max_helper(struct expression
*expr
, sval_t
*max
)
951 struct smatch_state
*state
;
954 if (get_hard_max(expr
, &sval
)) {
959 state
= get_extra_state(expr
);
960 if (!state
|| !estate_has_fuzzy_max(state
))
962 *max
= sval_cast(get_type(expr
), estate_get_fuzzy_max(state
));
966 static bool get_fuzzy_min_helper(struct expression
*expr
, sval_t
*min
)
968 struct smatch_state
*state
;
971 state
= get_extra_state(expr
);
972 if (!state
|| !estate_rl(state
))
975 sval
= estate_min(state
);
976 if (sval_is_negative(sval
) && sval_is_min(sval
))
979 if (sval_is_max(sval
))
982 *min
= sval_cast(get_type(expr
), sval
);
986 int get_const_value(struct expression
*expr
, sval_t
*sval
)
991 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
994 if (!(sym
->ctype
.modifiers
& MOD_CONST
))
996 if (get_value(sym
->initializer
, &right
)) {
997 *sval
= sval_cast(get_type(expr
), right
);
1003 struct range_list
*var_to_absolute_rl(struct expression
*expr
)
1005 struct smatch_state
*state
;
1006 struct range_list
*rl
;
1008 state
= get_extra_state(expr
);
1009 if (!state
|| is_whole_rl(estate_rl(state
))) {
1010 state
= get_real_absolute_state(expr
);
1011 if (state
&& state
->data
&& !estate_is_whole(state
))
1012 return clone_rl(estate_rl(state
));
1013 if (get_mtag_rl(expr
, &rl
))
1015 if (get_db_type_rl(expr
, &rl
) && !is_whole_rl(rl
))
1017 return alloc_whole_rl(get_type(expr
));
1019 /* err on the side of saying things are possible */
1020 if (!estate_rl(state
))
1021 return alloc_whole_rl(get_type(expr
));
1022 return clone_rl(estate_rl(state
));
1025 static bool is_param_sym(struct expression
*expr
)
1027 if (expr
->type
!= EXPR_SYMBOL
)
1029 if (get_param_num(expr
) < 0)
1034 static bool handle_variable(struct expression
*expr
, int implied
, int *recurse_cnt
, struct range_list
**res
, sval_t
*res_sval
)
1036 struct smatch_state
*state
;
1037 struct range_list
*rl
;
1038 sval_t sval
, min
, max
;
1039 struct symbol
*type
;
1041 if (get_const_value(expr
, &sval
)) {
1046 if (implied
== RL_EXACT
)
1049 if (custom_handle_variable
) {
1050 rl
= custom_handle_variable(expr
);
1052 if (!rl_to_sval(rl
, res_sval
))
1055 *res
= var_to_absolute_rl(expr
);
1060 if (get_mtag_sval(expr
, &sval
)) {
1065 type
= get_type(expr
);
1067 ((type
->type
== SYM_ARRAY
&& !is_param_sym(expr
)) ||
1068 type
->type
== SYM_FN
))
1069 return handle_address(expr
, implied
, recurse_cnt
, res
, res_sval
);
1071 /* FIXME: call rl_to_sval() on the results */
1077 state
= get_extra_state(expr
);
1079 if (implied
== RL_HARD
)
1081 if (get_mtag_rl(expr
, res
))
1083 if (get_db_type_rl(expr
, res
))
1085 if (is_array(expr
) && get_array_rl(expr
, res
))
1089 if (implied
== RL_HARD
&& !estate_has_hard_max(state
))
1091 *res
= clone_rl(estate_rl(state
));
1093 case RL_REAL_ABSOLUTE
: {
1094 struct smatch_state
*abs_state
;
1096 state
= get_extra_state(expr
);
1097 abs_state
= get_real_absolute_state(expr
);
1099 if (estate_rl(state
) && estate_rl(abs_state
)) {
1100 *res
= clone_rl(rl_intersection(estate_rl(state
),
1101 estate_rl(abs_state
)));
1103 } else if (estate_rl(state
)) {
1104 *res
= clone_rl(estate_rl(state
));
1106 } else if (estate_is_empty(state
)) {
1108 * FIXME: we don't handle empty extra states correctly.
1110 * The real abs rl is supposed to be filtered by the
1111 * extra state if there is one. We don't bother keeping
1112 * the abs state in sync all the time because we know it
1113 * will be filtered later.
1115 * It's not totally obvious to me how they should be
1116 * handled. Perhaps we should take the whole rl and
1117 * filter by the imaginary states. Perhaps we should
1118 * just go with the empty state.
1120 * Anyway what we currently do is return NULL here and
1121 * that gets translated into the whole range in
1122 * get_real_absolute_rl().
1126 } else if (estate_rl(abs_state
)) {
1127 *res
= clone_rl(estate_rl(abs_state
));
1131 if (get_mtag_rl(expr
, res
))
1133 if (get_db_type_rl(expr
, res
))
1135 if (is_array(expr
) && get_array_rl(expr
, res
))
1140 if (!get_fuzzy_min_helper(expr
, &min
))
1141 min
= sval_type_min(get_type(expr
));
1142 if (!get_fuzzy_max_helper(expr
, &max
))
1144 /* fuzzy ranges are often inverted */
1145 if (sval_cmp(min
, max
) > 0) {
1150 *res
= alloc_rl(min
, max
);
1156 static sval_t
handle_sizeof(struct expression
*expr
)
1161 ret
= sval_blank(expr
);
1162 sym
= expr
->cast_type
;
1164 sym
= evaluate_expression(expr
->cast_expression
);
1166 __silence_warnings_for_stmt
= true;
1171 * Expressions of restricted types will possibly get
1172 * promoted - check that here. I'm not sure how this works,
1173 * the problem is that sizeof(le16) shouldn't be promoted and
1174 * the original code did that... Let's if zero this out and
1178 if (is_restricted_type(sym
)) {
1179 if (type_bits(sym
) < bits_in_int
)
1183 if (is_fouled_type(sym
))
1186 examine_symbol_type(sym
);
1188 ret
.type
= size_t_ctype
;
1189 if (type_bits(sym
) <= 0) /* sizeof(void) */ {
1190 if (get_real_base_type(sym
) == &void_ctype
)
1195 ret
.value
= type_bytes(sym
);
1200 static bool handle_strlen(struct expression
*expr
, int implied
, int *recurse_cnt
, struct range_list
**res
, sval_t
*res_sval
)
1202 struct expression
*arg
, *tmp
;
1204 sval_t ret
= { .type
= &ulong_ctype
};
1205 struct range_list
*rl
;
1207 arg
= get_argument_from_call_expr(expr
->args
, 0);
1210 if (arg
->type
== EXPR_STRING
) {
1211 ret
.value
= arg
->string
->length
- 1;
1215 if (implied
== RL_EXACT
)
1217 if (get_implied_value(arg
, &tag
) &&
1218 (tmp
= fake_string_from_mtag(tag
.uvalue
))) {
1219 ret
.value
= tmp
->string
->length
- 1;
1224 if (implied
== RL_HARD
|| implied
== RL_FUZZY
)
1227 if (get_implied_return(expr
, &rl
)) {
1235 static bool handle_builtin_constant_p(struct expression
*expr
, int implied
, int *recurse_cnt
, sval_t
*res_sval
)
1237 struct expression
*arg
;
1238 struct range_list
*rl
;
1240 arg
= get_argument_from_call_expr(expr
->args
, 0);
1241 if (get_rl_internal(arg
, RL_EXACT
, recurse_cnt
, &rl
))
1248 static bool handle__builtin_choose_expr(struct expression
*expr
, int implied
, int *recurse_cnt
, struct range_list
**res
, sval_t
*res_sval
)
1250 struct expression
*const_expr
, *expr1
, *expr2
;
1253 const_expr
= get_argument_from_call_expr(expr
->args
, 0);
1254 expr1
= get_argument_from_call_expr(expr
->args
, 1);
1255 expr2
= get_argument_from_call_expr(expr
->args
, 2);
1257 if (!get_value(const_expr
, &sval
) || !expr1
|| !expr2
)
1260 return get_rl_sval(expr1
, implied
, recurse_cnt
, res
, res_sval
);
1262 return get_rl_sval(expr2
, implied
, recurse_cnt
, res
, res_sval
);
1265 int smatch_fls(unsigned long long value
)
1269 for (i
= 63; i
>= 0; i
--) {
1270 if (value
& 1ULL << i
)
1276 static bool handle_ffs(struct expression
*expr
, int implied
, int *recurse_cnt
, struct range_list
**res
, sval_t
*res_sval
)
1278 struct expression
*arg
;
1279 struct bit_info
*bits
;
1280 sval_t high
= { .type
= &int_ctype
};
1281 sval_t low
= { .type
= &int_ctype
};
1283 arg
= get_argument_from_call_expr(expr
->args
, 0);
1285 bits
= get_bit_info(arg
);
1286 if (bits
->possible
== 0) {
1292 high
.value
= ffsll(bits
->set
);
1294 high
.value
= smatch_fls(bits
->possible
);
1296 low
.value
= ffsll(bits
->possible
);
1298 *res
= alloc_rl(low
, high
);
1302 static bool handle_call_rl(struct expression
*expr
, int implied
, int *recurse_cnt
, struct range_list
**res
, sval_t
*res_sval
)
1304 struct range_list
*rl
;
1306 if (sym_name_is("__builtin_constant_p", expr
->fn
))
1307 return handle_builtin_constant_p(expr
, implied
, recurse_cnt
, res_sval
);
1309 if (sym_name_is("__builtin_choose_expr", expr
->fn
))
1310 return handle__builtin_choose_expr(expr
, implied
, recurse_cnt
, res
, res_sval
);
1312 if (sym_name_is("__builtin_expect", expr
->fn
) ||
1313 sym_name_is("__builtin_bswap16", expr
->fn
) ||
1314 sym_name_is("__builtin_bswap32", expr
->fn
) ||
1315 sym_name_is("__builtin_bswap64", expr
->fn
)) {
1316 struct expression
*arg
;
1318 arg
= get_argument_from_call_expr(expr
->args
, 0);
1319 return get_rl_sval(arg
, implied
, recurse_cnt
, res
, res_sval
);
1322 if (sym_name_is("__builtin_ffs", expr
->fn
) ||
1323 sym_name_is("__builtin_ffsl", expr
->fn
) ||
1324 sym_name_is("__builtin_ffsll", expr
->fn
))
1325 return handle_ffs(expr
, implied
, recurse_cnt
, res
, res_sval
);
1327 if (sym_name_is("strlen", expr
->fn
))
1328 return handle_strlen(expr
, implied
, recurse_cnt
, res
, res_sval
);
1330 if (implied
== RL_EXACT
|| implied
== RL_HARD
)
1333 if (custom_handle_variable
) {
1334 rl
= custom_handle_variable(expr
);
1341 /* Ugh... get_implied_return() sets *rl to NULL on failure */
1342 if (get_implied_return(expr
, &rl
)) {
1346 rl
= db_return_vals(expr
);
1354 static bool handle_cast(struct expression
*expr
, int implied
, int *recurse_cnt
, struct range_list
**res
, sval_t
*res_sval
)
1356 struct range_list
*rl
;
1357 struct symbol
*type
;
1360 type
= get_type(expr
);
1361 if (get_rl_sval(expr
->cast_expression
, implied
, recurse_cnt
, &rl
, &sval
)) {
1363 *res_sval
= sval_cast(type
, sval
);
1365 *res
= cast_rl(type
, rl
);
1368 if (implied
== RL_ABSOLUTE
|| implied
== RL_REAL_ABSOLUTE
) {
1369 *res
= alloc_whole_rl(type
);
1372 if (implied
== RL_IMPLIED
&& type
&&
1373 type_bits(type
) > 0 && type_bits(type
) < 32) {
1374 *res
= alloc_whole_rl(type
);
1380 static bool get_offset_from_down(struct expression
*expr
, int implied
, int *recurse_cnt
, struct range_list
**res
, sval_t
*res_sval
)
1382 struct expression
*index
;
1383 struct symbol
*type
= expr
->in
;
1384 struct range_list
*rl
;
1385 struct symbol
*field
;
1387 sval_t sval
= { .type
= ssize_t_ctype
};
1388 sval_t tmp_sval
= {};
1391 * FIXME: I don't really know what I'm doing here. I wish that I
1392 * could just get rid of the __builtin_offset() function and use:
1393 * "&((struct bpf_prog *)NULL)->insns[fprog->len]" instead...
1394 * Anyway, I have done the minimum ammount of work to get that
1395 * expression to work.
1399 if (expr
->op
!= '.' || !expr
->down
||
1400 expr
->down
->type
!= EXPR_OFFSETOF
||
1401 expr
->down
->op
!= '[' ||
1405 index
= expr
->down
->index
;
1407 examine_symbol_type(type
);
1408 type
= get_real_base_type(type
);
1411 field
= find_identifier(expr
->ident
, type
->symbol_list
, &offset
);
1415 type
= get_real_base_type(field
);
1416 if (!type
|| type
->type
!= SYM_ARRAY
)
1418 type
= get_real_base_type(type
);
1420 if (get_implied_value_internal(index
, recurse_cnt
, &sval
)) {
1421 res_sval
->type
= ssize_t_ctype
;
1422 res_sval
->value
= offset
+ sval
.value
* type_bytes(type
);
1426 if (!get_rl_sval(index
, implied
, recurse_cnt
, &rl
, &tmp_sval
))
1430 * I'm not sure why get_rl_sval() would return an sval when
1431 * get_implied_value_internal() failed but it does when I
1432 * parse drivers/net/ethernet/mellanox/mlx5/core/en/monitor_stats.c
1435 if (tmp_sval
.type
) {
1436 res_sval
->type
= ssize_t_ctype
;
1437 res_sval
->value
= offset
+ sval
.value
* type_bytes(type
);
1441 sval
.value
= type_bytes(type
);
1442 rl
= rl_binop(rl
, '*', alloc_rl(sval
, sval
));
1443 sval
.value
= offset
;
1444 *res
= rl_binop(rl
, '+', alloc_rl(sval
, sval
));
1448 static bool get_offset_from_in(struct expression
*expr
, int implied
, int *recurse_cnt
, struct range_list
**res
, sval_t
*res_sval
)
1450 struct symbol
*type
= get_real_base_type(expr
->in
);
1451 struct symbol
*field
;
1454 if (expr
->op
!= '.' || !type
|| !expr
->ident
)
1457 field
= find_identifier(expr
->ident
, type
->symbol_list
, &offset
);
1461 res_sval
->type
= size_t_ctype
;
1462 res_sval
->value
= offset
;
1467 static bool handle_offsetof_rl(struct expression
*expr
, int implied
, int *recurse_cnt
, struct range_list
**res
, sval_t
*res_sval
)
1469 if (get_offset_from_down(expr
, implied
, recurse_cnt
, res
, res_sval
))
1472 if (get_offset_from_in(expr
, implied
, recurse_cnt
, res
, res_sval
))
1475 evaluate_expression(expr
);
1476 if (expr
->type
== EXPR_VALUE
) {
1477 *res_sval
= sval_from_val(expr
, expr
->value
);
1483 static bool get_rl_sval(struct expression
*expr
, int implied
, int *recurse_cnt
, struct range_list
**res
, sval_t
*sval_res
)
1485 struct range_list
*rl
= (void *)-1UL;
1486 struct symbol
*type
;
1489 type
= get_type(expr
);
1490 expr
= strip_parens(expr
);
1494 if (++(*recurse_cnt
) >= 200)
1497 switch(expr
->type
) {
1499 case EXPR_FORCE_CAST
:
1500 case EXPR_IMPLIED_CAST
:
1501 handle_cast(expr
, implied
, recurse_cnt
, &rl
, &sval
);
1505 expr
= strip_expr(expr
);
1509 switch (expr
->type
) {
1511 sval
= sval_from_val(expr
, expr
->value
);
1514 sval
= sval_from_fval(expr
, expr
->fvalue
);
1517 handle_preop_rl(expr
, implied
, recurse_cnt
, &rl
, &sval
);
1520 get_rl_sval(expr
->unop
, implied
, recurse_cnt
, &rl
, &sval
);
1523 handle_binop_rl(expr
, implied
, recurse_cnt
, &rl
, &sval
);
1526 handle_comparison_rl(expr
, implied
, recurse_cnt
, &rl
, &sval
);
1529 handle_logical_rl(expr
, implied
, recurse_cnt
, &rl
, &sval
);
1531 case EXPR_PTRSIZEOF
:
1533 sval
= handle_sizeof(expr
);
1536 case EXPR_CONDITIONAL
:
1537 handle_conditional_rl(expr
, implied
, recurse_cnt
, &rl
, &sval
);
1540 handle_call_rl(expr
, implied
, recurse_cnt
, &rl
, &sval
);
1543 if (get_mtag_sval(expr
, &sval
))
1545 if (implied
== RL_EXACT
)
1547 rl
= alloc_rl(valid_ptr_min_sval
, valid_ptr_max_sval
);
1550 handle_offsetof_rl(expr
, implied
, recurse_cnt
, &rl
, &sval
);
1553 evaluate_expression(expr
);
1554 if (expr
->type
== EXPR_VALUE
)
1555 sval
= sval_from_val(expr
, expr
->value
);
1558 handle_variable(expr
, implied
, recurse_cnt
, &rl
, &sval
);
1562 if (rl
== (void *)-1UL)
1565 if (sval
.type
|| (rl
&& rl_to_sval(rl
, &sval
))) {
1569 if (implied
== RL_EXACT
)
1576 if (type
&& (implied
== RL_ABSOLUTE
|| implied
== RL_REAL_ABSOLUTE
)) {
1577 *res
= alloc_whole_rl(type
);
1583 static bool get_rl_internal(struct expression
*expr
, int implied
, int *recurse_cnt
, struct range_list
**res
)
1585 struct range_list
*rl
= NULL
;
1588 if (!get_rl_sval(expr
, implied
, recurse_cnt
, &rl
, &sval
))
1592 *res
= alloc_rl(sval
, sval
);
1598 static bool get_rl_helper(struct expression
*expr
, int implied
, struct range_list
**res
)
1600 struct range_list
*rl
= NULL
;
1602 int recurse_cnt
= 0;
1604 if (get_value(expr
, &sval
)) {
1605 *res
= alloc_rl(sval
, sval
);
1609 if (!get_rl_sval(expr
, implied
, &recurse_cnt
, &rl
, &sval
))
1613 *res
= alloc_rl(sval
, sval
);
1620 struct expression
*expr
;
1622 } cached_results
[24];
1623 static int cache_idx
;
1625 void clear_math_cache(void)
1627 memset(cached_results
, 0, sizeof(cached_results
));
1630 void set_fast_math_only(void)
1635 void clear_fast_math_only(void)
1641 * Don't cache EXPR_VALUE because values are fast already.
1644 static bool get_value_literal(struct expression
*expr
, sval_t
*res_sval
)
1646 struct expression
*tmp
;
1647 int recurse_cnt
= 0;
1649 tmp
= strip_expr(expr
);
1650 if (!tmp
|| tmp
->type
!= EXPR_VALUE
)
1653 return get_rl_sval(expr
, RL_EXACT
, &recurse_cnt
, NULL
, res_sval
);
1656 /* returns 1 if it can get a value literal or else returns 0 */
1657 int get_value(struct expression
*expr
, sval_t
*res_sval
)
1659 struct range_list
*(*orig_custom_fn
)(struct expression
*expr
);
1660 int recurse_cnt
= 0;
1664 if (get_value_literal(expr
, res_sval
))
1668 * This only handles RL_EXACT because other expr statements can be
1669 * different at different points. Like the list iterator, for example.
1671 for (i
= 0; i
< ARRAY_SIZE(cached_results
); i
++) {
1672 if (expr
== cached_results
[i
].expr
) {
1673 if (cached_results
[i
].sval
.type
) {
1674 *res_sval
= cached_results
[i
].sval
;
1681 orig_custom_fn
= custom_handle_variable
;
1682 custom_handle_variable
= NULL
;
1683 get_rl_sval(expr
, RL_EXACT
, &recurse_cnt
, NULL
, &sval
);
1685 custom_handle_variable
= orig_custom_fn
;
1687 cached_results
[cache_idx
].expr
= expr
;
1688 cached_results
[cache_idx
].sval
= sval
;
1689 cache_idx
= (cache_idx
+ 1) % ARRAY_SIZE(cached_results
);
1698 static bool get_implied_value_internal(struct expression
*expr
, int *recurse_cnt
, sval_t
*res_sval
)
1700 struct range_list
*rl
;
1702 res_sval
->type
= NULL
;
1704 if (!get_rl_sval(expr
, RL_IMPLIED
, recurse_cnt
, &rl
, res_sval
))
1706 if (!res_sval
->type
&& !rl_to_sval(rl
, res_sval
))
1711 int get_implied_value(struct expression
*expr
, sval_t
*sval
)
1713 struct range_list
*rl
;
1715 if (!get_rl_helper(expr
, RL_IMPLIED
, &rl
) ||
1716 !rl_to_sval(rl
, sval
))
1721 int get_implied_value_fast(struct expression
*expr
, sval_t
*sval
)
1723 struct range_list
*rl
;
1731 set_fast_math_only();
1732 if (get_rl_helper(expr
, RL_IMPLIED
, &rl
) &&
1733 rl_to_sval(rl
, sval
))
1735 clear_fast_math_only();
1741 int get_implied_min(struct expression
*expr
, sval_t
*sval
)
1743 struct range_list
*rl
;
1745 if (!get_rl_helper(expr
, RL_IMPLIED
, &rl
) || !rl
)
1751 int get_implied_max(struct expression
*expr
, sval_t
*sval
)
1753 struct range_list
*rl
;
1755 if (!get_rl_helper(expr
, RL_IMPLIED
, &rl
) || !rl
)
1761 int get_implied_rl(struct expression
*expr
, struct range_list
**rl
)
1763 if (!get_rl_helper(expr
, RL_IMPLIED
, rl
) || !*rl
)
1768 static int get_absolute_rl_internal(struct expression
*expr
, struct range_list
**rl
, int *recurse_cnt
)
1771 get_rl_internal(expr
, RL_ABSOLUTE
, recurse_cnt
, rl
);
1773 *rl
= alloc_whole_rl(get_type(expr
));
1777 int get_absolute_rl(struct expression
*expr
, struct range_list
**rl
)
1780 get_rl_helper(expr
, RL_ABSOLUTE
, rl
);
1782 *rl
= alloc_whole_rl(get_type(expr
));
1786 int get_real_absolute_rl(struct expression
*expr
, struct range_list
**rl
)
1789 get_rl_helper(expr
, RL_REAL_ABSOLUTE
, rl
);
1791 *rl
= alloc_whole_rl(get_type(expr
));
1795 int custom_get_absolute_rl(struct expression
*expr
,
1796 struct range_list
*(*fn
)(struct expression
*expr
),
1797 struct range_list
**rl
)
1802 custom_handle_variable
= fn
;
1803 ret
= get_rl_helper(expr
, RL_REAL_ABSOLUTE
, rl
);
1804 custom_handle_variable
= NULL
;
1808 int get_implied_rl_var_sym(const char *var
, struct symbol
*sym
, struct range_list
**rl
)
1810 struct smatch_state
*state
;
1812 state
= get_state(SMATCH_EXTRA
, var
, sym
);
1813 *rl
= estate_rl(state
);
1819 int get_hard_max(struct expression
*expr
, sval_t
*sval
)
1821 struct range_list
*rl
;
1823 if (!get_rl_helper(expr
, RL_HARD
, &rl
) || !rl
)
1829 int get_fuzzy_min(struct expression
*expr
, sval_t
*sval
)
1831 struct range_list
*rl
;
1834 if (!get_rl_helper(expr
, RL_FUZZY
, &rl
) || !rl
)
1837 if (sval_is_negative(tmp
) && sval_is_min(tmp
))
1843 int get_fuzzy_max(struct expression
*expr
, sval_t
*sval
)
1845 struct range_list
*rl
;
1848 if (!get_rl_helper(expr
, RL_FUZZY
, &rl
) || !rl
)
1851 if (max
.uvalue
> INT_MAX
- 10000)
1857 int get_absolute_min(struct expression
*expr
, sval_t
*sval
)
1859 struct range_list
*rl
;
1860 struct symbol
*type
;
1862 type
= get_type(expr
);
1864 type
= &llong_ctype
; // FIXME: this is wrong but places assume get type can't fail.
1866 get_rl_helper(expr
, RL_REAL_ABSOLUTE
, &rl
);
1870 *sval
= sval_type_min(type
);
1872 if (sval_cmp(*sval
, sval_type_min(type
)) < 0)
1873 *sval
= sval_type_min(type
);
1877 int get_absolute_max(struct expression
*expr
, sval_t
*sval
)
1879 struct range_list
*rl
;
1880 struct symbol
*type
;
1882 type
= get_type(expr
);
1884 type
= &llong_ctype
;
1886 get_rl_helper(expr
, RL_REAL_ABSOLUTE
, &rl
);
1890 *sval
= sval_type_max(type
);
1892 if (sval_cmp(sval_type_max(type
), *sval
) < 0)
1893 *sval
= sval_type_max(type
);
1897 int known_condition_true(struct expression
*expr
)
1904 if (__inline_fn
&& get_param_num(expr
) >= 0) {
1905 if (get_implied_value(expr
, &tmp
) && tmp
.value
)
1910 if (get_value(expr
, &tmp
) && tmp
.value
)
1916 int known_condition_false(struct expression
*expr
)
1923 if (__inline_fn
&& get_param_num(expr
) >= 0) {
1924 if (get_implied_value(expr
, &tmp
) && tmp
.value
== 0)
1929 if (expr_is_zero(expr
))
1935 int implied_condition_true(struct expression
*expr
)
1942 if (known_condition_true(expr
))
1944 if (get_implied_value(expr
, &tmp
) && tmp
.value
)
1947 if (expr
->type
== EXPR_POSTOP
)
1948 return implied_condition_true(expr
->unop
);
1950 if (expr
->type
== EXPR_PREOP
&& expr
->op
== SPECIAL_DECREMENT
)
1951 return implied_not_equal(expr
->unop
, 1);
1952 if (expr
->type
== EXPR_PREOP
&& expr
->op
== SPECIAL_INCREMENT
)
1953 return implied_not_equal(expr
->unop
, -1);
1955 expr
= strip_expr(expr
);
1956 switch (expr
->type
) {
1958 if (do_comparison(expr
) == 1)
1962 if (expr
->op
== '!') {
1963 if (implied_condition_false(expr
->unop
))
1969 if (implied_not_equal(expr
, 0) == 1)
1976 int implied_condition_false(struct expression
*expr
)
1978 struct expression
*tmp
;
1984 if (known_condition_false(expr
))
1987 switch (expr
->type
) {
1989 if (do_comparison(expr
) == 2)
1992 if (expr
->op
== '!') {
1993 if (implied_condition_true(expr
->unop
))
1997 tmp
= strip_expr(expr
);
1999 return implied_condition_false(tmp
);
2002 if (get_implied_value(expr
, &sval
) && sval
.value
== 0)