Print this page
new smatch
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/tools/smatch/src/smatch_estate.c
+++ new/usr/src/tools/smatch/src/smatch_estate.c
1 1 /*
2 2 * Copyright (C) 2010 Dan Carpenter.
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 /*
19 19 * smatch_dinfo.c has helper functions for handling data_info structs
20 20 *
21 21 */
22 22
23 23 #include <stdlib.h>
24 24 #ifndef __USE_ISOC99
25 25 #define __USE_ISOC99
26 26 #endif
27 27 #include <limits.h>
28 28 #include "parse.h"
29 29 #include "smatch.h"
30 30 #include "smatch_slist.h"
31 31 #include "smatch_extra.h"
32 32
33 33 struct smatch_state *merge_estates(struct smatch_state *s1, struct smatch_state *s2)
34 34 {
35 35 struct smatch_state *tmp;
36 36 struct range_list *value_ranges;
37 37 struct related_list *rlist;
38 38
39 39 if (estates_equiv(s1, s2))
40 40 return s1;
41 41
42 42 value_ranges = rl_union(estate_rl(s1), estate_rl(s2));
43 43 tmp = alloc_estate_rl(value_ranges);
44 44 rlist = get_shared_relations(estate_related(s1), estate_related(s2));
45 45 set_related(tmp, rlist);
↓ open down ↓ |
45 lines elided |
↑ open up ↑ |
46 46
47 47 if ((estate_has_hard_max(s1) && (!estate_rl(s2) || estate_has_hard_max(s2))) ||
48 48 (estate_has_hard_max(s2) && (!estate_rl(s1) || estate_has_hard_max(s1))))
49 49 estate_set_hard_max(tmp);
50 50
51 51 estate_set_fuzzy_max(tmp, sval_max(estate_get_fuzzy_max(s1), estate_get_fuzzy_max(s2)));
52 52
53 53 if (estate_capped(s1) && estate_capped(s2))
54 54 estate_set_capped(tmp);
55 55
56 + if (estate_treat_untagged(s1) && estate_treat_untagged(s2))
57 + estate_set_treat_untagged(tmp);
58 +
56 59 return tmp;
57 60 }
58 61
59 62 struct data_info *get_dinfo(struct smatch_state *state)
60 63 {
61 64 if (!state)
62 65 return NULL;
63 66 return (struct data_info *)state->data;
64 67 }
65 68
66 69 struct range_list *estate_rl(struct smatch_state *state)
67 70 {
68 71 if (!state)
69 72 return NULL;
70 73 return get_dinfo(state)->value_ranges;
71 74 }
72 75
73 76 struct related_list *estate_related(struct smatch_state *state)
74 77 {
75 78 if (!state)
76 79 return NULL;
77 80 return get_dinfo(state)->related;
78 81 }
79 82
80 83 sval_t estate_get_fuzzy_max(struct smatch_state *state)
81 84 {
82 85 sval_t empty = {};
83 86
84 87 if (!state || !get_dinfo(state))
85 88 return empty;
86 89 return get_dinfo(state)->fuzzy_max;
87 90 }
88 91
89 92 int estate_has_fuzzy_max(struct smatch_state *state)
90 93 {
91 94 if (estate_get_fuzzy_max(state).type)
92 95 return 1;
93 96 return 0;
94 97 }
95 98
96 99 void estate_set_fuzzy_max(struct smatch_state *state, sval_t fuzzy_max)
97 100 {
98 101 if (!rl_has_sval(estate_rl(state), fuzzy_max))
99 102 return;
100 103 get_dinfo(state)->fuzzy_max = fuzzy_max;
101 104 }
102 105
103 106 void estate_copy_fuzzy_max(struct smatch_state *new, struct smatch_state *old)
104 107 {
105 108 if (!estate_has_fuzzy_max(old))
106 109 return;
107 110 estate_set_fuzzy_max(new, estate_get_fuzzy_max(old));
108 111 }
↓ open down ↓ |
43 lines elided |
↑ open up ↑ |
109 112
110 113 void estate_clear_fuzzy_max(struct smatch_state *state)
111 114 {
112 115 sval_t empty = {};
113 116
114 117 get_dinfo(state)->fuzzy_max = empty;
115 118 }
116 119
117 120 int estate_has_hard_max(struct smatch_state *state)
118 121 {
119 - if (!state)
122 + if (!state || !estate_rl(state))
120 123 return 0;
121 124 return get_dinfo(state)->hard_max;
122 125 }
123 126
124 127 void estate_set_hard_max(struct smatch_state *state)
125 128 {
126 129 get_dinfo(state)->hard_max = 1;
127 130 }
128 131
129 132 void estate_clear_hard_max(struct smatch_state *state)
130 133 {
131 134 get_dinfo(state)->hard_max = 0;
132 135 }
133 136
134 137 int estate_get_hard_max(struct smatch_state *state, sval_t *sval)
135 138 {
136 139 if (!state || !get_dinfo(state)->hard_max || !estate_rl(state))
137 140 return 0;
138 141 *sval = rl_max(estate_rl(state));
139 142 return 1;
140 143 }
141 144
142 145 bool estate_capped(struct smatch_state *state)
143 146 {
144 147 if (!state)
145 148 return false;
146 149 /* impossible states are capped */
↓ open down ↓ |
17 lines elided |
↑ open up ↑ |
147 150 if (!estate_rl(state))
148 151 return true;
149 152 return get_dinfo(state)->capped;
150 153 }
151 154
152 155 void estate_set_capped(struct smatch_state *state)
153 156 {
154 157 get_dinfo(state)->capped = true;
155 158 }
156 159
160 +bool estate_treat_untagged(struct smatch_state *state)
161 +{
162 + if (!state)
163 + return false;
164 +
165 + /* impossible states are capped */
166 + if (!estate_rl(state))
167 + return true;
168 +
169 + return get_dinfo(state)->treat_untagged;
170 +}
171 +
172 +void estate_set_treat_untagged(struct smatch_state *state)
173 +{
174 + get_dinfo(state)->treat_untagged = true;
175 +}
176 +
157 177 sval_t estate_min(struct smatch_state *state)
158 178 {
159 179 return rl_min(estate_rl(state));
160 180 }
161 181
162 182 sval_t estate_max(struct smatch_state *state)
163 183 {
164 184 return rl_max(estate_rl(state));
165 185 }
166 186
167 187 struct symbol *estate_type(struct smatch_state *state)
168 188 {
169 189 return rl_max(estate_rl(state)).type;
170 190 }
171 191
172 192 static int rlists_equiv(struct related_list *one, struct related_list *two)
173 193 {
174 194 struct relation *one_rel;
175 195 struct relation *two_rel;
176 196
177 197 PREPARE_PTR_LIST(one, one_rel);
178 198 PREPARE_PTR_LIST(two, two_rel);
179 199 for (;;) {
180 200 if (!one_rel && !two_rel)
181 201 return 1;
182 202 if (!one_rel || !two_rel)
183 203 return 0;
184 204 if (one_rel->sym != two_rel->sym)
185 205 return 0;
186 206 if (strcmp(one_rel->name, two_rel->name))
187 207 return 0;
188 208 NEXT_PTR_LIST(one_rel);
189 209 NEXT_PTR_LIST(two_rel);
190 210 }
191 211 FINISH_PTR_LIST(two_rel);
192 212 FINISH_PTR_LIST(one_rel);
193 213
194 214 return 1;
195 215 }
196 216
↓ open down ↓ |
30 lines elided |
↑ open up ↑ |
197 217 int estates_equiv(struct smatch_state *one, struct smatch_state *two)
198 218 {
199 219 if (!one || !two)
200 220 return 0;
201 221 if (one == two)
202 222 return 1;
203 223 if (!rlists_equiv(estate_related(one), estate_related(two)))
204 224 return 0;
205 225 if (estate_capped(one) != estate_capped(two))
206 226 return 0;
227 + if (estate_treat_untagged(one) != estate_treat_untagged(two))
228 + return 0;
207 229 if (strcmp(one->name, two->name) == 0)
208 230 return 1;
209 231 return 0;
210 232 }
211 233
212 234 int estate_is_whole(struct smatch_state *state)
213 235 {
214 236 return is_whole_rl(estate_rl(state));
215 237 }
216 238
217 239 int estate_is_empty(struct smatch_state *state)
218 240 {
219 241 return state && !estate_rl(state);
220 242 }
221 243
222 244 int estate_is_unknown(struct smatch_state *state)
223 245 {
224 246 if (!estate_is_whole(state))
225 247 return 0;
226 248 if (estate_related(state))
↓ open down ↓ |
10 lines elided |
↑ open up ↑ |
227 249 return 0;
228 250 if (estate_has_fuzzy_max(state))
229 251 return 0;
230 252 return 1;
231 253 }
232 254
233 255 int estate_get_single_value(struct smatch_state *state, sval_t *sval)
234 256 {
235 257 sval_t min, max;
236 258
259 + if (!estate_rl(state))
260 + return 0;
237 261 min = rl_min(estate_rl(state));
238 262 max = rl_max(estate_rl(state));
239 263 if (sval_cmp(min, max) != 0)
240 264 return 0;
241 265 *sval = min;
242 266 return 1;
243 267 }
244 268
245 269 static struct data_info *alloc_dinfo(void)
246 270 {
247 271 struct data_info *ret;
248 272
249 273 ret = __alloc_data_info(0);
250 274 memset(ret, 0, sizeof(*ret));
251 275 return ret;
252 276 }
253 277
254 278 static struct data_info *alloc_dinfo_range(sval_t min, sval_t max)
255 279 {
256 280 struct data_info *ret;
257 281
258 282 ret = alloc_dinfo();
259 283 add_range(&ret->value_ranges, min, max);
260 284 return ret;
261 285 }
262 286
263 287 static struct data_info *alloc_dinfo_range_list(struct range_list *rl)
264 288 {
265 289 struct data_info *ret;
266 290
267 291 ret = alloc_dinfo();
268 292 ret->value_ranges = rl;
269 293 return ret;
270 294 }
271 295
272 296 static struct data_info *clone_dinfo(struct data_info *dinfo)
273 297 {
274 298 struct data_info *ret;
275 299
276 300 ret = alloc_dinfo();
277 301 ret->related = clone_related_list(dinfo->related);
278 302 ret->value_ranges = clone_rl(dinfo->value_ranges);
279 303 ret->hard_max = dinfo->hard_max;
280 304 ret->fuzzy_max = dinfo->fuzzy_max;
281 305 return ret;
282 306 }
283 307
284 308 struct smatch_state *clone_estate(struct smatch_state *state)
285 309 {
286 310 struct smatch_state *ret;
287 311
288 312 if (!state)
289 313 return NULL;
290 314
291 315 ret = __alloc_smatch_state(0);
292 316 ret->name = state->name;
293 317 ret->data = clone_dinfo(get_dinfo(state));
294 318 return ret;
295 319 }
296 320
297 321 struct smatch_state *clone_partial_estate(struct smatch_state *state, struct range_list *rl)
298 322 {
299 323 struct smatch_state *ret;
300 324
301 325 if (!state)
302 326 return NULL;
303 327
304 328 rl = cast_rl(estate_type(state), rl);
305 329
306 330 ret = alloc_estate_rl(rl);
307 331 set_related(ret, clone_related_list(estate_related(state)));
308 332 if (estate_has_hard_max(state))
309 333 estate_set_hard_max(ret);
310 334 if (estate_has_fuzzy_max(state))
311 335 estate_set_fuzzy_max(ret, estate_get_fuzzy_max(state));
312 336
313 337 return ret;
314 338 }
315 339
316 340 struct smatch_state *alloc_estate_empty(void)
317 341 {
318 342 struct smatch_state *state;
319 343 struct data_info *dinfo;
320 344
321 345 dinfo = alloc_dinfo();
322 346 state = __alloc_smatch_state(0);
323 347 state->data = dinfo;
324 348 state->name = "";
325 349 return state;
326 350 }
327 351
328 352 struct smatch_state *alloc_estate_whole(struct symbol *type)
329 353 {
330 354 return alloc_estate_rl(alloc_whole_rl(type));
331 355 }
332 356
333 357 struct smatch_state *extra_empty(void)
334 358 {
335 359 struct smatch_state *ret;
336 360
337 361 ret = __alloc_smatch_state(0);
338 362 ret->name = "empty";
339 363 ret->data = alloc_dinfo();
340 364 return ret;
341 365 }
342 366
343 367 struct smatch_state *alloc_estate_sval(sval_t sval)
344 368 {
345 369 struct smatch_state *state;
346 370
347 371 state = __alloc_smatch_state(0);
348 372 state->data = alloc_dinfo_range(sval, sval);
349 373 state->name = show_rl(get_dinfo(state)->value_ranges);
350 374 estate_set_hard_max(state);
351 375 estate_set_fuzzy_max(state, sval);
352 376 return state;
353 377 }
354 378
355 379 struct smatch_state *alloc_estate_range(sval_t min, sval_t max)
356 380 {
357 381 struct smatch_state *state;
358 382
359 383 state = __alloc_smatch_state(0);
360 384 state->data = alloc_dinfo_range(min, max);
361 385 state->name = show_rl(get_dinfo(state)->value_ranges);
362 386 return state;
363 387 }
364 388
365 389 struct smatch_state *alloc_estate_rl(struct range_list *rl)
366 390 {
367 391 struct smatch_state *state;
368 392
369 393 if (!rl)
370 394 return extra_empty();
371 395
372 396 state = __alloc_smatch_state(0);
373 397 state->data = alloc_dinfo_range_list(rl);
374 398 state->name = show_rl(rl);
375 399 return state;
376 400 }
377 401
378 402 struct smatch_state *clone_estate_cast(struct symbol *type, struct smatch_state *state)
379 403 {
380 404 struct smatch_state *ret;
381 405 struct data_info *dinfo;
382 406
383 407 if (!state)
384 408 return NULL;
385 409
386 410 dinfo = alloc_dinfo();
387 411 dinfo->value_ranges = clone_rl(cast_rl(type, estate_rl(state)));
388 412
389 413 ret = __alloc_smatch_state(0);
390 414 ret->name = show_rl(dinfo->value_ranges);
391 415 ret->data = dinfo;
392 416
393 417 return ret;
394 418 }
395 419
396 420 struct smatch_state *get_implied_estate(struct expression *expr)
397 421 {
398 422 struct smatch_state *state;
399 423 struct range_list *rl;
400 424
401 425 state = get_state_expr(SMATCH_EXTRA, expr);
402 426 if (state)
403 427 return state;
404 428 if (!get_implied_rl(expr, &rl))
405 429 rl = alloc_whole_rl(get_type(expr));
406 430 return alloc_estate_rl(rl);
407 431 }
408 432
409 433 /*
410 434 * One of the complications is that smatch tries to free a bunch of data at the
411 435 * end of every function.
412 436 */
413 437 struct data_info *clone_dinfo_perm(struct data_info *dinfo)
414 438 {
415 439 struct data_info *ret;
416 440
417 441 ret = malloc(sizeof(*ret));
418 442 memset(ret, 0, sizeof(*ret));
419 443 ret->related = NULL;
420 444 ret->value_ranges = clone_rl_permanent(dinfo->value_ranges);
421 445 ret->hard_max = 0;
422 446 ret->fuzzy_max = dinfo->fuzzy_max;
423 447 return ret;
424 448 }
425 449
426 450 struct smatch_state *clone_estate_perm(struct smatch_state *state)
427 451 {
428 452 struct smatch_state *ret;
429 453
430 454 ret = malloc(sizeof(*ret));
431 455 ret->name = alloc_string(state->name);
432 456 ret->data = clone_dinfo_perm(get_dinfo(state));
433 457 return ret;
434 458 }
435 459
436 460
↓ open down ↓ |
190 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX