Print this page
11972 resync smatch
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/tools/smatch/src/check_nospec.c
+++ new/usr/src/tools/smatch/src/check_nospec.c
1 1 /*
2 2 * Copyright (C) 2018 Oracle.
3 3 *
4 4 * This program is free software; you can redistribute it and/or
5 5 * modify it under the terms of the GNU General Public License
6 6 * as published by the Free Software Foundation; either version 2
7 7 * of the License, or (at your option) any later version.
8 8 *
9 9 * This program is distributed in the hope that it will be useful,
10 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 * GNU General Public License for more details.
13 13 *
14 14 * You should have received a copy of the GNU General Public License
15 15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
16 16 */
17 17
18 18 #include <stdlib.h>
19 19 #include "parse.h"
20 20 #include "smatch.h"
21 21 #include "smatch_slist.h"
22 22 #include "smatch_extra.h"
23 23
24 24 static int my_id;
25 25 static int barrier_id;
26 26
27 27 STATE(nospec);
28 28
29 29 static bool in_nospec_stmt;
30 30
31 31 static struct smatch_state *unmatched_state(struct sm_state *sm)
32 32 {
33 33 struct range_list *rl;
34 34
35 35 if (__in_function_def && !get_user_rl_var_sym(sm->name, sm->sym, &rl))
36 36 return &nospec;
37 37 return &undefined;
38 38 }
39 39
40 40 bool is_nospec(struct expression *expr)
41 41 {
42 42 char *macro;
43 43
44 44 if (in_nospec_stmt)
45 45 return true;
46 46 if (!expr)
47 47 return false;
48 48 if (get_state_expr(my_id, expr) == &nospec)
49 49 return true;
50 50 macro = get_macro_name(expr->pos);
51 51 if (macro && strcmp(macro, "array_index_nospec") == 0)
52 52 return true;
53 53 return false;
54 54 }
55 55
56 56 static void nospec_assign(struct expression *expr)
57 57 {
58 58 if (is_nospec(expr->right))
59 59 set_state_expr(my_id, expr->left, &nospec);
60 60 }
61 61
62 62 static void set_param_nospec(const char *name, struct symbol *sym, char *key, char *value)
63 63 {
64 64 char fullname[256];
65 65
66 66 if (strcmp(key, "*$") == 0)
67 67 snprintf(fullname, sizeof(fullname), "*%s", name);
68 68 else if (strncmp(key, "$", 1) == 0)
69 69 snprintf(fullname, 256, "%s%s", name, key + 1);
70 70 else
71 71 return;
72 72
73 73 set_state(my_id, fullname, sym, &nospec);
74 74 }
75 75
76 76 static void match_call_info(struct expression *expr)
77 77 {
78 78 struct expression *arg;
79 79 int i = 0;
80 80
81 81 FOR_EACH_PTR(expr->args, arg) {
82 82 if (get_state_expr(my_id, arg) == &nospec)
83 83 sql_insert_caller_info(expr, NOSPEC, i, "$", "");
84 84 i++;
85 85 } END_FOR_EACH_PTR(arg);
86 86 }
87 87
88 88 static void struct_member_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
89 89 {
90 90 struct range_list *rl;
91 91
92 92 if (!get_user_rl_var_sym(sm->name, sm->sym, &rl))
93 93 return;
94 94 sql_insert_caller_info(call, NOSPEC, param, printed_name, "");
95 95 }
96 96
97 97 static void returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
98 98 {
99 99 struct stree *start_states = get_start_states();
100 100 struct symbol *returned_sym;
101 101 struct sm_state *sm;
102 102 const char *param_name;
103 103 struct range_list *rl;
104 104 int param;
105 105
106 106 returned_sym = expr_to_sym(expr);
107 107
108 108 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
109 109 if (get_state_stree(start_states, my_id, sm->name, sm->sym) == sm->state)
110 110 continue;
111 111 param = get_param_num_from_sym(sm->sym);
112 112 if (param < 0) {
113 113 if (!returned_sym || returned_sym != sm->sym)
114 114 continue;
115 115 param = -1;
116 116 }
117 117
118 118 param_name = get_param_name(sm);
119 119 if (!param_name)
120 120 continue;
121 121 if (param != -1 && strcmp(param_name, "$") == 0)
122 122 continue;
123 123
124 124 if (!get_user_rl_var_sym(sm->name, sm->sym, &rl))
125 125 continue;
126 126
127 127 sql_insert_return_states(return_id, return_ranges, NOSPEC, param, param_name, "");
128 128 } END_FOR_EACH_SM(sm);
129 129
130 130 if (is_nospec(expr) && get_user_rl(expr, &rl))
131 131 sql_insert_return_states(return_id, return_ranges, NOSPEC, -1, "$", "");
132 132
133 133 if (get_state(barrier_id, "barrier", NULL) == &nospec)
134 134 sql_insert_return_states(return_id, return_ranges, NOSPEC_WB, -1, "", "");
135 135 }
136 136
137 137 static int is_return_statement(void)
138 138 {
139 139 if (__cur_stmt && __cur_stmt->type == STMT_RETURN)
140 140 return 1;
141 141 return 0;
142 142 }
143 143
144 144 static void db_returns_nospec(struct expression *expr, int param, char *key, char *value)
145 145 {
146 146 struct expression *call;
147 147 struct expression *arg;
148 148 char *name;
149 149 struct symbol *sym;
150 150
151 151 call = expr;
152 152 while (call->type == EXPR_ASSIGNMENT)
153 153 call = strip_expr(call->right);
154 154 if (call->type != EXPR_CALL)
155 155 return;
156 156
157 157 if (param == -1 && expr->type == EXPR_ASSIGNMENT) {
158 158 name = get_variable_from_key(expr->left, key, &sym);
159 159 } else if (param == -1 && is_return_statement()) {
160 160 in_nospec_stmt = true;
161 161 return;
162 162 } else {
163 163 arg = get_argument_from_call_expr(call->args, param);
164 164 if (!arg)
165 165 return;
166 166 name = get_variable_from_key(arg, key, &sym);
167 167 }
168 168 if (!name || !sym)
169 169 goto free;
170 170
171 171 set_state(my_id, name, sym, &nospec);
↓ open down ↓ |
171 lines elided |
↑ open up ↑ |
172 172 free:
173 173 free_string(name);
174 174 }
175 175
176 176 static int is_nospec_asm(struct statement *stmt)
177 177 {
178 178 char *macro;
179 179
180 180 if (!stmt || stmt->type != STMT_ASM)
181 181 return 0;
182 + if (!stmt->asm_string)
183 + return 0;
182 184 macro = get_macro_name(stmt->asm_string->pos);
183 185 if (!macro || strcmp(macro, "CALL_NOSPEC") != 0)
184 186 return 0;
185 187 return 1;
186 188 }
187 189
188 190 static void match_asm(struct statement *stmt)
189 191 {
190 192 if (is_nospec_asm(stmt))
191 193 in_nospec_stmt = true;
192 194 }
193 195
194 196 static void match_after_nospec_asm(struct statement *stmt)
195 197 {
196 198 in_nospec_stmt = false;
197 199 }
198 200
199 201 static void mark_user_data_as_nospec(void)
200 202 {
201 203 struct stree *stree;
202 204 struct symbol *type;
203 205 struct sm_state *sm;
204 206
205 207 stree = get_user_stree();
206 208 FOR_EACH_SM(stree, sm) {
207 209 if (is_whole_rl(estate_rl(sm->state)))
208 210 continue;
209 211 type = estate_type(sm->state);
210 212 if (!type || type->type != SYM_BASETYPE)
211 213 continue;
212 214 if (!is_capped_var_sym(sm->name, sm->sym))
213 215 continue;
214 216 set_state(my_id, sm->name, sm->sym, &nospec);
215 217 } END_FOR_EACH_SM(sm);
216 218 free_stree(&stree);
217 219 }
218 220
219 221 static void match_barrier(struct statement *stmt)
220 222 {
221 223 char *macro;
222 224
223 225 macro = get_macro_name(stmt->pos);
224 226 if (!macro)
225 227 return;
226 228 if (strcmp(macro, "rmb") != 0 &&
227 229 strcmp(macro, "smp_rmb") != 0 &&
228 230 strcmp(macro, "barrier_nospec") != 0 &&
229 231 strcmp(macro, "preempt_disable") != 0)
230 232 return;
231 233
232 234 set_state(barrier_id, "barrier", NULL, &nospec);
233 235 mark_user_data_as_nospec();
234 236 }
235 237
236 238 static void db_returns_barrier(struct expression *expr, int param, char *key, char *value)
237 239 {
238 240 mark_user_data_as_nospec();
239 241 }
240 242
241 243 static void select_return_stmt_cnt(struct expression *expr, int param, char *key, char *value)
242 244 {
243 245 int cnt;
244 246
245 247 cnt = atoi(value);
246 248 if (cnt > 400)
247 249 mark_user_data_as_nospec();
248 250 }
249 251
250 252 void check_nospec(int id)
251 253 {
252 254 my_id = id;
253 255
254 256 add_hook(&nospec_assign, ASSIGNMENT_HOOK);
255 257
256 258 select_caller_info_hook(set_param_nospec, NOSPEC);
257 259 add_unmatched_state_hook(my_id, &unmatched_state);
258 260
259 261 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
260 262 add_member_info_callback(my_id, struct_member_callback);
261 263 add_split_return_callback(&returned_struct_members);
262 264 select_return_states_hook(NOSPEC, &db_returns_nospec);
263 265 select_return_states_hook(NOSPEC_WB, &db_returns_barrier);
264 266 select_return_states_hook(STMT_CNT, &select_return_stmt_cnt);
265 267
266 268 add_hook(&match_asm, ASM_HOOK);
267 269 add_hook(&match_after_nospec_asm, STMT_HOOK_AFTER);
268 270 }
269 271
270 272 void check_nospec_barrier(int id)
271 273 {
272 274 barrier_id = id;
273 275
274 276 add_hook(&match_barrier, ASM_HOOK);
275 277 }
↓ open down ↓ |
84 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX