-
Notifications
You must be signed in to change notification settings - Fork 14
/
check_impossible_mask.c
122 lines (99 loc) · 2.58 KB
/
check_impossible_mask.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* Copyright (C) 2012 Oracle.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
*/
#include "smatch.h"
static int my_id;
#if 0
static unsigned long long find_possible_bits(struct expression *expr)
{
sval_t sval;
unsigned long long ret;
int set;
int i;
expr = strip_expr(expr);
if (get_implied_value(expr, &sval))
return sval.uvalue;
if (expr->type == EXPR_BINOP && (expr->op == '&' || expr->op == '|')) {
unsigned long long left, right;
left = find_possible_bits(expr->left);
if (!left)
return 0;
right = find_possible_bits(expr->right);
if (!right)
return 0;
if (expr->op == '&')
return left & right;
return left | right;
}
get_absolute_max(expr, &sval);
ret = sval.value;
set = false;
for (i = 63; i >= 0; i--) {
if (ret & 1 << i)
set = true;
if (set)
ret |= 1 << i;
}
return ret;
}
#endif
static unsigned long long get_possible_bits(struct expression *expr)
{
sval_t sval;
expr = strip_expr(expr);
if (expr->type != EXPR_BINOP)
return 0;
if (expr->op != '&')
return 0;
if (!get_implied_value(expr->right, &sval))
return 0;
return sval.uvalue;
}
static void match_condition(struct expression *expr)
{
struct symbol *type;
sval_t sval;
unsigned long long left_mask, right_mask;
char *str;
type = get_type(expr);
if (!type)
type = &int_ctype;
if (expr->type != EXPR_COMPARE)
return;
if (expr->op != SPECIAL_EQUAL && expr->op != SPECIAL_NOTEQUAL)
return;
if (!get_value(expr->right, &sval))
return;
right_mask = sval.uvalue;
left_mask = get_possible_bits(expr->left);
if (!left_mask)
return;
if (type_bits(type) < 64) {
left_mask &= (1ULL << type_bits(type)) - 1;
right_mask &= (1ULL << type_bits(type)) - 1;
}
if ((left_mask & right_mask) == right_mask)
return;
str = expr_to_str(expr);
sm_warning("masked condition '%s' is always %s.", str,
expr->op == SPECIAL_EQUAL ? "false" : "true");
free_string(str);
}
void check_impossible_mask(int id)
{
my_id = id;
add_hook(&match_condition, CONDITION_HOOK);
}