Print this page
12724 update smatch to 0.6.1-rc1-il-5
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/tools/smatch/src/smatch_container_of.c
+++ new/usr/src/tools/smatch/src/smatch_container_of.c
1 1 /*
2 2 * Copyright (C) 2017 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 "smatch.h"
19 19 #include "smatch_slist.h"
20 20 #include "smatch_extra.h"
21 21
22 22 static int my_id;
23 23 static int param_id;
24 24
25 25 int get_param_from_container_of(struct expression *expr)
26 26 {
27 27 struct expression *param_expr;
28 28 struct symbol *type;
29 29 sval_t sval;
30 30 int param;
31 31
32 32
33 33 type = get_type(expr);
34 34 if (!type || type->type != SYM_PTR)
35 35 return -1;
36 36
37 37 expr = strip_expr(expr);
38 38 if (expr->type != EXPR_BINOP || expr->op != '-')
39 39 return -1;
40 40
41 41 if (!get_value(expr->right, &sval))
42 42 return -1;
43 43 if (sval.value < 0 || sval.value > 4096)
44 44 return -1;
45 45
46 46 param_expr = get_assigned_expr(expr->left);
47 47 if (!param_expr)
48 48 return -1;
49 49 param = get_param_num(param_expr);
50 50 if (param < 0)
51 51 return -1;
52 52
53 53 return param;
54 54 }
55 55
56 56 int get_offset_from_container_of(struct expression *expr)
57 57 {
58 58 struct expression *param_expr;
59 59 struct symbol *type;
60 60 sval_t sval;
61 61
62 62 type = get_type(expr);
63 63 if (!type || type->type != SYM_PTR)
64 64 return -1;
65 65
66 66 expr = strip_expr(expr);
67 67 if (expr->type != EXPR_BINOP || expr->op != '-')
68 68 return -1;
69 69
70 70 if (!get_value(expr->right, &sval))
71 71 return -1;
72 72 if (sval.value < 0 || sval.value > 4096)
73 73 return -1;
74 74
75 75 param_expr = get_assigned_expr(expr->left);
76 76 if (!param_expr)
77 77 return -1;
78 78
79 79 return sval.value;
80 80 }
81 81
82 82 static void print_returns_container_of(int return_id, char *return_ranges, struct expression *expr)
83 83 {
84 84 int offset;
85 85 int param;
86 86 char key[64];
87 87 char value[64];
88 88
89 89 param = get_param_from_container_of(expr);
90 90 if (param < 0)
91 91 return;
92 92 offset = get_offset_from_container_of(expr);
93 93 if (offset < 0)
94 94 return;
95 95
96 96 snprintf(key, sizeof(key), "%d", param);
97 97 snprintf(value, sizeof(value), "-%d", offset);
98 98
99 99 /* no need to add it to return_implies because it's not really param_used */
100 100 sql_insert_return_states(return_id, return_ranges, CONTAINER, -1,
101 101 key, value);
102 102 }
103 103
104 104 static int get_deref_count(struct expression *expr)
105 105 {
106 106 int cnt = 0;
107 107
108 108 while (expr && expr->type == EXPR_DEREF) {
109 109 expr = expr->deref;
110 110 if (expr->type == EXPR_PREOP && expr->op == '*')
111 111 expr = expr->unop;
112 112 cnt++;
113 113 if (cnt > 100)
114 114 return -1;
115 115 }
116 116 return cnt;
117 117 }
118 118
119 119 static struct expression *get_partial_deref(struct expression *expr, int cnt)
120 120 {
121 121 while (--cnt >= 0) {
122 122 if (!expr || expr->type != EXPR_DEREF)
123 123 return expr;
124 124 expr = expr->deref;
125 125 if (expr->type == EXPR_PREOP && expr->op == '*')
126 126 expr = expr->unop;
127 127 }
128 128 return expr;
129 129 }
130 130
131 131 static int partial_deref_to_offset_str(struct expression *expr, int cnt, char op, char *buf, int size)
132 132 {
133 133 int n, offset;
134 134
135 135 if (cnt == 0)
136 136 return snprintf(buf, size, "%c0", op);
137 137
138 138 n = 0;
139 139 while (--cnt >= 0) {
140 140 offset = get_member_offset_from_deref(expr);
141 141 if (offset < 0)
142 142 return -1;
143 143 n += snprintf(buf + n, size - n, "%c%d", op, offset);
144 144 if (expr->type != EXPR_DEREF)
145 145 return -1;
146 146 expr = expr->deref;
147 147 if (expr->type == EXPR_PREOP && expr->op == '*')
148 148 expr = expr->unop;
149 149 }
150 150
151 151 return n;
152 152 }
153 153
154 154 static char *get_shared_str(struct expression *expr, struct expression *container)
155 155 {
156 156 struct expression *one, *two;
157 157 int exp, cont, min, ret, n;
158 158 static char buf[48];
159 159
160 160 exp = get_deref_count(expr);
161 161 cont = get_deref_count(container);
162 162 if (exp < 0 || cont < 0)
163 163 return NULL;
164 164
165 165 min = (exp < cont) ? exp : cont;
166 166 while (min >= 0) {
167 167 one = get_partial_deref(expr, exp - min);
168 168 two = get_partial_deref(container, cont - min);
169 169 if (expr_equiv(one, two))
170 170 goto found;
171 171 min--;
172 172 }
173 173
174 174 return NULL;
175 175
176 176 found:
177 177 ret = partial_deref_to_offset_str(expr, exp - min, '-', buf, sizeof(buf));
178 178 if (ret < 0)
179 179 return NULL;
180 180 n = ret;
181 181 ret = partial_deref_to_offset_str(container, cont - min, '+', buf + ret, sizeof(buf) - ret);
182 182 if (ret < 0)
183 183 return NULL;
184 184 n += ret;
185 185 if (n >= sizeof(buf))
186 186 return NULL;
187 187
188 188 return buf;
189 189 }
190 190
191 191 static char *get_stored_container_name(struct expression *container,
192 192 struct expression *expr)
193 193 {
194 194 struct smatch_state *state;
195 195 static char buf[64];
196 196 char *p;
197 197 int param;
198 198
199 199 if (!container || container->type != EXPR_SYMBOL)
200 200 return NULL;
201 201 if (!expr || expr->type != EXPR_SYMBOL)
202 202 return NULL;
203 203 state = get_state_expr(param_id, expr);
204 204 if (!state)
205 205 return NULL;
206 206
207 207 snprintf(buf, sizeof(buf), "%s", state->name);
208 208 p = strchr(buf, '|');
209 209 if (!p)
210 210 return NULL;
211 211 *p = '\0';
212 212 param = atoi(p + 2);
213 213 if (get_param_sym_from_num(param) == container->symbol)
214 214 return buf;
215 215 return NULL;
216 216 }
217 217
218 218 static char *get_container_name_helper(struct expression *container, struct expression *expr)
219 219 {
220 220 struct symbol *container_sym, *sym;
221 221 static char buf[64];
222 222 char *ret, *shared;
223 223 bool star;
224 224
225 225 expr = strip_expr(expr);
226 226 container = strip_expr(container);
227 227 if (!expr || !container)
228 228 return NULL;
229 229
230 230 ret = get_stored_container_name(container, expr);
231 231 if (ret)
232 232 return ret;
233 233
234 234 sym = expr_to_sym(expr);
235 235 container_sym = expr_to_sym(container);
236 236 if (!sym || !container_sym)
237 237 return NULL;
238 238 if (sym != container_sym)
239 239 return NULL;
240 240
241 241 if (container->type == EXPR_DEREF)
242 242 star = true;
243 243 else
244 244 star = false;
245 245
246 246 if (container->type == EXPR_PREOP && container->op == '&')
247 247 container = strip_expr(container->unop);
248 248 if (expr->type == EXPR_PREOP && expr->op == '&')
249 249 expr = strip_expr(expr->unop);
250 250
251 251 shared = get_shared_str(expr, container);
252 252 if (!shared)
253 253 return NULL;
254 254 if (star)
255 255 snprintf(buf, sizeof(buf), "*(%s)", shared);
256 256 else
257 257 snprintf(buf, sizeof(buf), "%s", shared);
258 258
259 259 return buf;
260 260 }
261 261
262 262 char *get_container_name(struct expression *container, struct expression *expr)
263 263 {
264 264 char *ret;
265 265
266 266 ret = get_container_name_helper(container, expr);
267 267 if (ret)
268 268 return ret;
269 269
270 270 ret = get_container_name_helper(get_assigned_expr(container), expr);
271 271 if (ret)
272 272 return ret;
273 273
274 274 ret = get_container_name_helper(container, get_assigned_expr(expr));
275 275 if (ret)
276 276 return ret;
277 277
278 278 ret = get_container_name_helper(get_assigned_expr(container),
279 279 get_assigned_expr(expr));
280 280 if (ret)
281 281 return ret;
282 282
283 283 return NULL;
284 284 }
285 285
286 286 static bool is_fn_ptr(struct expression *expr)
287 287 {
288 288 struct symbol *type;
289 289
290 290 if (!expr)
291 291 return false;
292 292 if (expr->type != EXPR_SYMBOL && expr->type != EXPR_DEREF)
293 293 return false;
294 294
295 295 type = get_type(expr);
296 296 if (!type || type->type != SYM_PTR)
297 297 return false;
298 298 type = get_real_base_type(type);
299 299 if (!type || type->type != SYM_FN)
300 300 return false;
301 301 return true;
302 302 }
303 303
304 304 static void match_call(struct expression *call)
305 305 {
306 306 struct expression *fn, *arg, *tmp;
307 307 bool found = false;
308 308 int fn_param, param;
309 309 char buf[32];
310 310 char *name;
311 311
312 312 /*
313 313 * We're trying to link the function with the parameter. There are a
314 314 * couple ways this can be passed:
315 315 * foo->func(foo, ...);
316 316 * foo->func(foo->x, ...);
317 317 * foo->bar.func(&foo->bar, ...);
318 318 * foo->bar->baz->func(foo, ...);
319 319 *
320 320 * So the method is basically to subtract the offsets until we get to
321 321 * the common bit, then add the member offsets to get the parameter.
322 322 *
323 323 * If we're taking an address then the offset math is not stared,
324 324 * otherwise it is. Starred means dereferenced.
325 325 */
326 326 fn = strip_expr(call->fn);
327 327
328 328 param = -1;
329 329 FOR_EACH_PTR(call->args, arg) {
330 330 param++;
331 331
332 332 name = get_container_name(arg, fn);
333 333 if (!name)
334 334 continue;
335 335
336 336 found = true;
337 337 sql_insert_caller_info(call, CONTAINER, param, name, "$(-1)");
338 338 } END_FOR_EACH_PTR(arg);
339 339
340 340 if (found)
341 341 return;
342 342
343 343 fn_param = -1;
344 344 FOR_EACH_PTR(call->args, arg) {
345 345 fn_param++;
346 346 if (!is_fn_ptr(arg))
347 347 continue;
348 348 param = -1;
349 349 FOR_EACH_PTR(call->args, tmp) {
350 350 param++;
351 351
352 352 /* the function isn't it's own container */
353 353 if (arg == tmp)
354 354 continue;
355 355
356 356 name = get_container_name(tmp, arg);
357 357 if (!name)
358 358 continue;
359 359
360 360 snprintf(buf, sizeof(buf), "$%d", param);
361 361 sql_insert_caller_info(call, CONTAINER, fn_param, name, buf);
362 362 return;
363 363 } END_FOR_EACH_PTR(tmp);
364 364 } END_FOR_EACH_PTR(arg);
365 365 }
366 366
367 367 static void db_passed_container(const char *name, struct symbol *sym, char *key, char *value)
368 368 {
369 369 char buf[64];
370 370
371 371 snprintf(buf, sizeof(buf), "%s|%s", key, value);
372 372 set_state(param_id, name, sym, alloc_state_str(buf));
373 373 }
374 374
375 375 struct db_info {
376 376 struct symbol *arg;
377 377 int prev_offset;
378 378 struct range_list *rl;
379 379 int star;
380 380 struct stree *stree;
381 381 };
382 382
383 383 static struct symbol *get_member_from_offset(struct symbol *sym, int offset)
384 384 {
385 385 struct symbol *type, *tmp;
386 386 int cur;
387 387
388 388 type = get_real_base_type(sym);
389 389 if (!type || type->type != SYM_PTR)
390 390 return NULL;
391 391 type = get_real_base_type(type);
392 392 if (!type || type->type != SYM_STRUCT)
393 393 return NULL;
394 394
395 395 cur = 0;
396 396 FOR_EACH_PTR(type->symbol_list, tmp) {
397 397 cur = ALIGN(cur, tmp->ctype.alignment);
398 398 if (offset == cur)
399 399 return tmp;
400 400 cur += type_bytes(tmp);
401 401 } END_FOR_EACH_PTR(tmp);
402 402 return NULL;
403 403 }
404 404
405 405 static struct symbol *get_member_type_from_offset(struct symbol *sym, int offset)
406 406 {
407 407 struct symbol *base_type;
408 408 struct symbol *member;
409 409
410 410 base_type = get_real_base_type(sym);
411 411 if (base_type && base_type->type == SYM_PTR)
412 412 base_type = get_real_base_type(base_type);
413 413 if (offset == 0 && base_type && base_type->type == SYM_BASETYPE)
414 414 return base_type;
415 415
416 416 member = get_member_from_offset(sym, offset);
417 417 if (!member)
418 418 return NULL;
419 419 return get_real_base_type(member);
420 420 }
421 421
422 422 static const char *get_name_from_offset(struct symbol *arg, int offset)
423 423 {
424 424 struct symbol *member, *type;
425 425 const char *name;
426 426 static char fullname[256];
427 427
428 428 name = arg->ident->name;
429 429
430 430 type = get_real_base_type(arg);
431 431 if (!type || type->type != SYM_PTR)
432 432 return name;
433 433
434 434 type = get_real_base_type(type);
435 435 if (!type)
436 436 return NULL;
437 437 if (type->type != SYM_STRUCT) {
438 438 snprintf(fullname, sizeof(fullname), "*%s", name);
439 439 return fullname;
440 440 }
441 441
442 442 member = get_member_from_offset(arg, offset);
443 443 if (!member || !member->ident)
444 444 return NULL;
445 445
446 446 snprintf(fullname, sizeof(fullname), "%s->%s", name, member->ident->name);
447 447 return fullname;
448 448 }
449 449
450 450 static void set_param_value(struct stree **stree, struct symbol *arg, int offset, struct range_list *rl)
451 451 {
452 452 const char *name;
453 453
454 454 name = get_name_from_offset(arg, offset);
455 455 if (!name)
456 456 return;
457 457 set_state_stree(stree, SMATCH_EXTRA, name, arg, alloc_estate_rl(rl));
458 458 }
459 459
460 460 static int save_vals(void *_db_info, int argc, char **argv, char **azColName)
461 461 {
462 462 struct db_info *db_info = _db_info;
463 463 struct symbol *type;
464 464 struct range_list *rl;
465 465 int offset = 0;
466 466 const char *value;
467 467
468 468 if (argc == 2) {
469 469 offset = atoi(argv[0]);
470 470 value = argv[1];
471 471 } else {
472 472 value = argv[0];
473 473 }
474 474
475 475 if (db_info->prev_offset != -1 &&
476 476 db_info->prev_offset != offset) {
477 477 set_param_value(&db_info->stree, db_info->arg, db_info->prev_offset, db_info->rl);
478 478 db_info->rl = NULL;
479 479 }
480 480
481 481 db_info->prev_offset = offset;
482 482
483 483 type = get_real_base_type(db_info->arg);
484 484 if (db_info->star)
485 485 goto found_type;
486 486 if (type->type != SYM_PTR)
487 487 return 0;
488 488 type = get_real_base_type(type);
489 489 if (type->type == SYM_BASETYPE)
490 490 goto found_type;
491 491 type = get_member_type_from_offset(db_info->arg, offset);
492 492 found_type:
493 493 str_to_rl(type, (char *)value, &rl);
494 494 if (db_info->rl)
495 495 db_info->rl = rl_union(db_info->rl, rl);
496 496 else
497 497 db_info->rl = rl;
498 498
499 499 return 0;
500 500 }
501 501
502 502 static struct stree *load_tag_info_sym(mtag_t tag, struct symbol *arg, int arg_offset, int star)
503 503 {
504 504 struct db_info db_info = {
505 505 .arg = arg,
506 506 .prev_offset = -1,
507 507 .star = star,
508 508 };
509 509 struct symbol *type;
510 510
511 511 if (!tag || !arg->ident)
512 512 return NULL;
513 513
514 514 type = get_real_base_type(arg);
515 515 if (!type)
516 516 return NULL;
517 517 if (!star) {
518 518 if (type->type != SYM_PTR)
519 519 return NULL;
520 520 type = get_real_base_type(type);
↓ open down ↓ |
520 lines elided |
↑ open up ↑ |
521 521 if (!type)
522 522 return NULL;
523 523 }
524 524
525 525 if (star || type->type == SYM_BASETYPE) {
526 526 run_sql(save_vals, &db_info,
527 527 "select value from mtag_data where tag = %lld and offset = %d and type = %d;",
528 528 tag, arg_offset, DATA_VALUE);
529 529 } else { /* presumably the parameter is a struct pointer */
530 530 run_sql(save_vals, &db_info,
531 - "select offset, value from mtag_data where tag = %lld and type = %d;",
531 + "select offset, value from mtag_data where tag = %lld and type = %d order by offset;",
532 532 tag, DATA_VALUE);
533 533 }
534 534
535 535 if (db_info.prev_offset != -1)
536 536 set_param_value(&db_info.stree, arg, db_info.prev_offset, db_info.rl);
537 537
538 538 // FIXME: handle an offset correctly
539 539 if (!star && !arg_offset) {
540 540 sval_t sval;
541 541
542 542 sval.type = get_real_base_type(arg);
543 543 sval.uvalue = tag;
544 544 set_state_stree(&db_info.stree, SMATCH_EXTRA, arg->ident->name, arg, alloc_estate_sval(sval));
545 545 }
546 546 return db_info.stree;
547 547 }
548 548
549 549 static void load_container_data(struct symbol *arg, const char *info)
550 550 {
551 551 mtag_t cur_tag, container_tag, arg_tag;
552 552 int container_offset, arg_offset;
553 553 struct sm_state *sm;
554 554 struct stree *stree;
555 555 char *p, *cont;
556 556 char copy[64];
557 557 bool star = 0;
558 558
559 559 snprintf(copy, sizeof(copy), "%s", info);
560 560 p = strchr(copy, '|');
561 561 if (!p)
562 562 return;
563 563 *p = '\0';
564 564 cont = p + 1;
565 565 p = copy;
566 566 if (p[0] == '*') {
567 567 star = 1;
568 568 p += 2;
569 569 }
570 570
571 571 if (strcmp(cont, "$(-1)") != 0)
↓ open down ↓ |
30 lines elided |
↑ open up ↑ |
572 572 return;
573 573
574 574 if (!get_toplevel_mtag(cur_func_sym, &cur_tag))
575 575 return;
576 576
577 577 while (true) {
578 578 container_offset = strtoul(p, &p, 0);
579 579 if (local_debug)
580 580 sm_msg("%s: cur_tag = %llu container_offset = %d",
581 581 __func__, cur_tag, container_offset);
582 - if (!mtag_map_select_container(cur_tag, container_offset, &container_tag))
582 + if (!mtag_map_select_container(cur_tag, -container_offset, &container_tag))
583 583 return;
584 584 cur_tag = container_tag;
585 585 if (local_debug)
586 586 sm_msg("%s: container_tag = %llu p = '%s'",
587 587 __func__, container_tag, p);
588 588 if (!p)
589 589 return;
590 590 if (p[0] != '-')
591 591 break;
592 592 p++;
593 593 }
594 594
595 595 if (p[0] != '+')
↓ open down ↓ |
3 lines elided |
↑ open up ↑ |
596 596 return;
597 597
598 598 p++;
599 599 arg_offset = strtoul(p, &p, 0);
600 600 if (p && *p && *p != ')')
601 601 return;
602 602
603 603 if (!arg_offset || star) {
604 604 arg_tag = container_tag;
605 605 } else {
606 - if (!mtag_map_select_tag(container_tag, -arg_offset, &arg_tag))
606 + if (!mtag_map_select_tag(container_tag, arg_offset, &arg_tag))
607 607 return;
608 608 }
609 609
610 610 stree = load_tag_info_sym(arg_tag, arg, arg_offset, star);
611 611 FOR_EACH_SM(stree, sm) {
612 612 set_state(sm->owner, sm->name, sm->sym, sm->state);
613 613 } END_FOR_EACH_SM(sm);
614 614 free_stree(&stree);
615 615 }
616 616
617 617 static void handle_passed_container(struct symbol *sym)
618 618 {
619 619 struct symbol *arg;
620 620 struct smatch_state *state;
621 621
622 622 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
623 623 state = get_state(param_id, arg->ident->name, arg);
624 624 if (!state || state == &merged)
625 625 continue;
626 626 load_container_data(arg, state->name);
627 627 } END_FOR_EACH_PTR(arg);
628 628 }
629 629
630 630 void register_container_of(int id)
631 631 {
632 632 my_id = id;
633 633
634 634 add_split_return_callback(&print_returns_container_of);
635 635 add_hook(&match_call, FUNCTION_CALL_HOOK);
636 636 }
637 637
638 638 void register_container_of2(int id)
639 639 {
640 640 param_id = id;
641 641
642 642 set_dynamic_states(param_id);
643 643 select_caller_info_hook(db_passed_container, CONTAINER);
644 644 add_merge_hook(param_id, &merge_str_state);
645 645 add_hook(&handle_passed_container, AFTER_DEF_HOOK);
646 646 }
647 647
↓ open down ↓ |
31 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX