-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsmatch_power_of_two.c
210 lines (167 loc) · 4.9 KB
/
smatch_power_of_two.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
* Copyright (C) 2021
*
* 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"
#include "smatch_extra.h"
#include "smatch_slist.h"
static int my_id;
STATE(power_of_two);
static struct smatch_state *unmatched_state(struct sm_state *sm)
{
struct smatch_state *state;
sval_t sval;
state = get_state(SMATCH_EXTRA, sm->name, sm->sym);
if (!state)
return &undefined;
if (!estate_get_single_value(state, &sval))
return &undefined;
if (!(sval.uvalue & (sval.uvalue - 1)))
return &power_of_two;
return &undefined;
}
static bool implied_power_of_two(struct expression *expr)
{
sval_t sval;
if (!get_implied_value(expr, &sval))
return false;
if (!(sval.uvalue & (sval.uvalue - 1)))
return true;
return false;
}
bool is_power_of_two(struct expression *expr)
{
expr = strip_expr(expr);
if (expr->type == EXPR_BINOP &&
expr->op == SPECIAL_LEFTSHIFT &&
is_power_of_two(expr->left))
return true;
if (implied_power_of_two(expr))
return true;
if (get_state_expr(my_id, expr) == &power_of_two)
return true;
return false;
}
static void set_power_of_two(struct expression *expr)
{
struct symbol *type;
type = get_type(expr);
if (type_bits(type) == 1)
return;
set_state_expr(my_id, expr->left, &power_of_two);
}
static bool is_sign_expansion(struct expression *expr)
{
struct range_list *rl;
struct symbol *type_left;
struct symbol *type_right;
type_left = get_type(expr->left);
type_right = get_type(expr->right);
if (!type_left || !type_right)
return true;
if (type_bits(type_left) <= type_bits(type_right))
return false;
get_absolute_rl(expr->right, &rl);
if (sval_is_negative(rl_min(rl)))
return true;
return false;
}
static void match_assign(struct expression *expr)
{
if (expr->op != '=')
return;
if (implied_power_of_two(expr->right)) {
if (get_state_expr(my_id, expr->left))
set_state_expr(my_id, expr->left, &power_of_two);
return;
}
if (is_sign_expansion(expr))
return;
if (is_power_of_two(expr->right))
set_power_of_two(expr->left);
}
static bool is_minus_mask(struct expression *left, struct expression *right)
{
if (right->type != EXPR_BINOP ||
right->op != '-')
return false;
if (right->right->value != 1)
return false;
if (expr_equiv(left, right->left))
return true;
return false;
}
static void match_condition(struct expression *expr)
{
expr = strip_expr(expr);
if (expr->type != EXPR_BINOP ||
expr->op != '&')
return;
if (is_minus_mask(strip_expr(expr->left), strip_expr(expr->right))) {
set_true_false_states_expr(my_id, expr->left, NULL, &power_of_two);
return;
}
if (is_minus_mask(strip_expr(expr->right), strip_expr(expr->left))) {
set_true_false_states_expr(my_id, expr->right, NULL, &power_of_two);
return;
}
}
static void caller_info_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
{
if (sm->state != &power_of_two)
return;
sql_insert_caller_info(call, POWER_OF_TWO, param, printed_name, "");
}
static void set_power_of_two_name_sym(const char *name, struct symbol *sym, char *value)
{
set_state(my_id, name, sym, &power_of_two);
}
static void return_info_callback(int return_id, char *return_ranges,
struct expression *returned_expr,
int param,
const char *printed_name,
struct sm_state *sm)
{
struct smatch_state *estate;
sval_t sval;
if (sm->state != &power_of_two)
return;
if (param != -1 && !param_was_set_var_sym(sm->name, sm->sym))
return;
estate = get_state(SMATCH_EXTRA, sm->name, sm->sym);
if (estate_get_single_value(estate, &sval))
return;
sql_insert_return_states(return_id, return_ranges, POWER_OF_TWO_SET, param, printed_name, "");
}
static void returns_power_of_two_set(struct expression *expr, int param, char *key, char *value)
{
char *name;
struct symbol *sym;
name = get_name_sym_from_param_key(expr, param, key, &sym);
if (!name)
return;
set_state(my_id, name, sym, &power_of_two);
}
void register_power_of_two(int id)
{
my_id = id;
add_hook(&match_assign, ASSIGNMENT_HOOK);
add_hook(&match_condition, CONDITION_HOOK);
add_unmatched_state_hook(my_id, &unmatched_state);
add_caller_info_callback(my_id, caller_info_callback);
select_caller_name_sym(set_power_of_two_name_sym, POWER_OF_TWO);
add_return_info_callback(my_id, return_info_callback);
select_return_states_hook(POWER_OF_TWO_SET, &returns_power_of_two_set);
}