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_function_hooks.c
+++ new/usr/src/tools/smatch/src/smatch_function_hooks.c
1 1 /*
2 2 * Copyright (C) 2009 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 * There are several types of function hooks:
20 20 * add_function_hook() - For any time a function is called.
21 21 * add_function_assign_hook() - foo = the_function().
22 22 * add_implied_return_hook() - Calculates the implied return value.
23 23 * add_macro_assign_hook() - foo = the_macro().
24 24 * return_implies_state() - For when a return value of 1 implies locked
25 25 * and 0 implies unlocked. etc. etc.
26 26 *
27 27 */
28 28
29 29 #include <stdlib.h>
30 30 #include <stdio.h>
31 31 #include "smatch.h"
32 32 #include "smatch_slist.h"
33 33 #include "smatch_extra.h"
34 34 #include "smatch_function_hashtable.h"
35 35
36 36 struct fcall_back {
37 37 int type;
38 38 struct data_range *range;
39 39 union {
40 40 func_hook *call_back;
41 41 implication_hook *ranged;
42 42 implied_return_hook *implied_return;
43 43 } u;
44 44 void *info;
45 45 };
46 46
47 47 ALLOCATOR(fcall_back, "call backs");
48 48 DECLARE_PTR_LIST(call_back_list, struct fcall_back);
49 49
50 50 DEFINE_FUNCTION_HASHTABLE_STATIC(callback, struct fcall_back, struct call_back_list);
51 51 static struct hashtable *func_hash;
52 52
53 53 int __in_fake_parameter_assign;
54 54
55 55 #define REGULAR_CALL 0
56 56 #define RANGED_CALL 1
57 57 #define ASSIGN_CALL 2
58 58 #define IMPLIED_RETURN 3
59 59 #define MACRO_ASSIGN 4
60 60 #define MACRO_ASSIGN_EXTRA 5
61 61
62 62 struct return_implies_callback {
63 63 int type;
64 64 return_implies_hook *callback;
65 65 };
66 66 ALLOCATOR(return_implies_callback, "return_implies callbacks");
67 67 DECLARE_PTR_LIST(db_implies_list, struct return_implies_callback);
68 68 static struct db_implies_list *db_return_states_list;
69 69
70 70 typedef void (void_fn)(void);
71 71 DECLARE_PTR_LIST(void_fn_list, void_fn *);
72 72 static struct void_fn_list *return_states_before;
73 73 static struct void_fn_list *return_states_after;
74 74
75 75 static struct fcall_back *alloc_fcall_back(int type, void *call_back,
76 76 void *info)
77 77 {
78 78 struct fcall_back *cb;
79 79
80 80 cb = __alloc_fcall_back(0);
81 81 cb->type = type;
82 82 cb->u.call_back = call_back;
83 83 cb->info = info;
84 84 return cb;
85 85 }
86 86
87 87 void add_function_hook(const char *look_for, func_hook *call_back, void *info)
88 88 {
89 89 struct fcall_back *cb;
90 90
91 91 cb = alloc_fcall_back(REGULAR_CALL, call_back, info);
92 92 add_callback(func_hash, look_for, cb);
93 93 }
94 94
95 95 void add_function_assign_hook(const char *look_for, func_hook *call_back,
96 96 void *info)
97 97 {
98 98 struct fcall_back *cb;
99 99
100 100 cb = alloc_fcall_back(ASSIGN_CALL, call_back, info);
101 101 add_callback(func_hash, look_for, cb);
102 102 }
103 103
104 104 void add_implied_return_hook(const char *look_for,
105 105 implied_return_hook *call_back,
106 106 void *info)
107 107 {
108 108 struct fcall_back *cb;
109 109
110 110 cb = alloc_fcall_back(IMPLIED_RETURN, call_back, info);
111 111 add_callback(func_hash, look_for, cb);
112 112 }
113 113
114 114 void add_macro_assign_hook(const char *look_for, func_hook *call_back,
115 115 void *info)
116 116 {
117 117 struct fcall_back *cb;
118 118
119 119 cb = alloc_fcall_back(MACRO_ASSIGN, call_back, info);
120 120 add_callback(func_hash, look_for, cb);
121 121 }
122 122
123 123 void add_macro_assign_hook_extra(const char *look_for, func_hook *call_back,
124 124 void *info)
125 125 {
126 126 struct fcall_back *cb;
127 127
128 128 cb = alloc_fcall_back(MACRO_ASSIGN_EXTRA, call_back, info);
129 129 add_callback(func_hash, look_for, cb);
130 130 }
131 131
132 132 void return_implies_state(const char *look_for, long long start, long long end,
133 133 implication_hook *call_back, void *info)
134 134 {
135 135 struct fcall_back *cb;
136 136
137 137 cb = alloc_fcall_back(RANGED_CALL, call_back, info);
138 138 cb->range = alloc_range_perm(ll_to_sval(start), ll_to_sval(end));
139 139 add_callback(func_hash, look_for, cb);
140 140 }
141 141
142 142 void return_implies_state_sval(const char *look_for, sval_t start, sval_t end,
143 143 implication_hook *call_back, void *info)
144 144 {
145 145 struct fcall_back *cb;
146 146
147 147 cb = alloc_fcall_back(RANGED_CALL, call_back, info);
148 148 cb->range = alloc_range_perm(start, end);
149 149 add_callback(func_hash, look_for, cb);
150 150 }
151 151
152 152 void select_return_states_hook(int type, return_implies_hook *callback)
153 153 {
154 154 struct return_implies_callback *cb = __alloc_return_implies_callback(0);
155 155
156 156 cb->type = type;
157 157 cb->callback = callback;
158 158 add_ptr_list(&db_return_states_list, cb);
159 159 }
160 160
161 161 void select_return_states_before(void_fn *fn)
162 162 {
163 163 void_fn **p = malloc(sizeof(void_fn *));
164 164 *p = fn;
165 165 add_ptr_list(&return_states_before, p);
166 166 }
167 167
168 168 void select_return_states_after(void_fn *fn)
169 169 {
170 170 void_fn **p = malloc(sizeof(void_fn *));
171 171 *p = fn;
172 172 add_ptr_list(&return_states_after, p);
173 173 }
174 174
175 175 static void call_return_states_before_hooks(void)
176 176 {
177 177 void_fn **fn;
178 178
179 179 FOR_EACH_PTR(return_states_before, fn) {
180 180 (*fn)();
181 181 } END_FOR_EACH_PTR(fn);
182 182 }
183 183
184 184 static void call_return_states_after_hooks(struct expression *expr)
185 185 {
186 186 void_fn **fn;
187 187
188 188 FOR_EACH_PTR(return_states_after, fn) {
189 189 (*fn)();
190 190 } END_FOR_EACH_PTR(fn);
191 191 __pass_to_client(expr, FUNCTION_CALL_HOOK_AFTER_DB);
192 192 }
193 193
194 194 static int call_call_backs(struct call_back_list *list, int type,
195 195 const char *fn, struct expression *expr)
196 196 {
197 197 struct fcall_back *tmp;
198 198 int handled = 0;
199 199
200 200 FOR_EACH_PTR(list, tmp) {
201 201 if (tmp->type == type) {
202 202 (tmp->u.call_back)(fn, expr, tmp->info);
203 203 handled = 1;
204 204 }
205 205 } END_FOR_EACH_PTR(tmp);
206 206
207 207 return handled;
208 208 }
209 209
210 210 static void call_ranged_call_backs(struct call_back_list *list,
211 211 const char *fn, struct expression *call_expr,
212 212 struct expression *assign_expr)
213 213 {
214 214 struct fcall_back *tmp;
215 215
216 216 FOR_EACH_PTR(list, tmp) {
217 217 (tmp->u.ranged)(fn, call_expr, assign_expr, tmp->info);
218 218 } END_FOR_EACH_PTR(tmp);
219 219 }
220 220
221 221 static struct call_back_list *get_same_ranged_call_backs(struct call_back_list *list,
222 222 struct data_range *drange)
223 223 {
224 224 struct call_back_list *ret = NULL;
225 225 struct fcall_back *tmp;
226 226
227 227 FOR_EACH_PTR(list, tmp) {
228 228 if (tmp->type != RANGED_CALL)
229 229 continue;
230 230 if (ranges_equiv(tmp->range, drange))
231 231 add_ptr_list(&ret, tmp);
232 232 } END_FOR_EACH_PTR(tmp);
233 233 return ret;
234 234 }
235 235
236 236 static int in_list_exact_sval(struct range_list *list, struct data_range *drange)
237 237 {
238 238 struct data_range *tmp;
239 239
240 240 FOR_EACH_PTR(list, tmp) {
241 241 if (ranges_equiv(tmp, drange))
242 242 return 1;
243 243 } END_FOR_EACH_PTR(tmp);
244 244 return 0;
245 245 }
246 246
247 247 static int assign_ranged_funcs(const char *fn, struct expression *expr,
248 248 struct call_back_list *call_backs)
249 249 {
250 250 struct fcall_back *tmp;
251 251 struct sm_state *sm;
252 252 char *var_name;
253 253 struct symbol *sym;
254 254 struct smatch_state *estate;
255 255 struct stree *tmp_stree;
256 256 struct stree *final_states = NULL;
257 257 struct range_list *handled_ranges = NULL;
258 258 struct call_back_list *same_range_call_backs = NULL;
259 259 struct range_list *rl;
260 260 int handled = 0;
261 261
262 262 if (!call_backs)
263 263 return 0;
264 264
265 265 var_name = expr_to_var_sym(expr->left, &sym);
266 266 if (!var_name || !sym)
267 267 goto free;
268 268
269 269 FOR_EACH_PTR(call_backs, tmp) {
270 270 if (tmp->type != RANGED_CALL)
271 271 continue;
272 272
273 273 if (in_list_exact_sval(handled_ranges, tmp->range))
274 274 continue;
275 275 __push_fake_cur_stree();
276 276 tack_on(&handled_ranges, tmp->range);
277 277
278 278 same_range_call_backs = get_same_ranged_call_backs(call_backs, tmp->range);
279 279 call_ranged_call_backs(same_range_call_backs, fn, expr->right, expr);
280 280 __free_ptr_list((struct ptr_list **)&same_range_call_backs);
281 281
282 282 rl = alloc_rl(tmp->range->min, tmp->range->max);
283 283 rl = cast_rl(get_type(expr->left), rl);
284 284 estate = alloc_estate_rl(rl);
285 285 set_extra_mod(var_name, sym, expr->left, estate);
286 286
287 287 tmp_stree = __pop_fake_cur_stree();
288 288 merge_fake_stree(&final_states, tmp_stree);
289 289 free_stree(&tmp_stree);
290 290 handled = 1;
291 291 } END_FOR_EACH_PTR(tmp);
292 292
293 293 FOR_EACH_SM(final_states, sm) {
294 294 __set_sm(sm);
295 295 } END_FOR_EACH_SM(sm);
296 296
297 297 free_stree(&final_states);
298 298 free:
299 299 free_string(var_name);
300 300 return handled;
301 301 }
302 302
303 303 static void call_implies_callbacks(int comparison, struct expression *expr, sval_t sval, int left, struct stree **implied_true, struct stree **implied_false)
304 304 {
305 305 struct call_back_list *call_backs;
306 306 struct fcall_back *tmp;
307 307 const char *fn;
308 308 struct data_range *value_range;
309 309 struct stree *true_states = NULL;
310 310 struct stree *false_states = NULL;
311 311 struct stree *tmp_stree;
312 312
313 313 *implied_true = NULL;
314 314 *implied_false = NULL;
315 315 if (expr->fn->type != EXPR_SYMBOL || !expr->fn->symbol)
316 316 return;
317 317 fn = expr->fn->symbol->ident->name;
318 318 call_backs = search_callback(func_hash, (char *)expr->fn->symbol->ident->name);
319 319 if (!call_backs)
320 320 return;
321 321 value_range = alloc_range(sval, sval);
322 322
323 323 /* set true states */
324 324 __push_fake_cur_stree();
325 325 FOR_EACH_PTR(call_backs, tmp) {
326 326 if (tmp->type != RANGED_CALL)
327 327 continue;
328 328 if (!true_comparison_range_LR(comparison, tmp->range, value_range, left))
329 329 continue;
330 330 (tmp->u.ranged)(fn, expr, NULL, tmp->info);
331 331 } END_FOR_EACH_PTR(tmp);
332 332 tmp_stree = __pop_fake_cur_stree();
333 333 merge_fake_stree(&true_states, tmp_stree);
334 334 free_stree(&tmp_stree);
335 335
336 336 /* set false states */
337 337 __push_fake_cur_stree();
338 338 FOR_EACH_PTR(call_backs, tmp) {
339 339 if (tmp->type != RANGED_CALL)
340 340 continue;
341 341 if (!false_comparison_range_LR(comparison, tmp->range, value_range, left))
342 342 continue;
343 343 (tmp->u.ranged)(fn, expr, NULL, tmp->info);
344 344 } END_FOR_EACH_PTR(tmp);
345 345 tmp_stree = __pop_fake_cur_stree();
346 346 merge_fake_stree(&false_states, tmp_stree);
347 347 free_stree(&tmp_stree);
348 348
349 349 *implied_true = true_states;
↓ open down ↓ |
349 lines elided |
↑ open up ↑ |
350 350 *implied_false = false_states;
351 351 }
352 352
353 353 struct db_callback_info {
354 354 int true_side;
355 355 int comparison;
356 356 struct expression *expr;
357 357 struct range_list *rl;
358 358 int left;
359 359 struct stree *stree;
360 + struct stree *implied;
360 361 struct db_implies_list *callbacks;
361 362 int prev_return_id;
362 363 int cull;
363 364 int has_states;
364 365 char *ret_str;
365 366 struct smatch_state *ret_state;
366 367 struct expression *var_expr;
367 368 int handled;
368 369 };
369 370
371 +static void set_implied_states(struct db_callback_info *db_info)
372 +{
373 + struct sm_state *sm;
374 +
375 + FOR_EACH_SM(db_info->implied, sm) {
376 + __set_sm(sm);
377 + } END_FOR_EACH_SM(sm);
378 +
379 + free_stree(&db_info->implied);
380 +}
381 +
370 382 static void store_return_state(struct db_callback_info *db_info, const char *ret_str, struct smatch_state *state)
371 383 {
372 384 db_info->ret_str = alloc_sname(ret_str),
373 385 db_info->ret_state = state;
374 386 }
375 387
376 388 static bool fake_a_param_assignment(struct expression *expr, const char *return_str, struct smatch_state *orig)
377 389 {
378 390 struct expression *arg, *left, *right, *tmp, *fake_assign;
379 391 char *p;
380 392 int param;
381 393 char buf[256];
382 394 char *str;
383 395
384 396 if (expr->type != EXPR_ASSIGNMENT || expr->op != '=')
385 397 return false;
386 398 left = expr->left;
387 399 right = expr->right;
388 400
389 401 while (right->type == EXPR_ASSIGNMENT)
390 402 right = strip_expr(right->right);
391 403 if (!right || right->type != EXPR_CALL)
392 404 return false;
393 405
394 406 p = strchr(return_str, '[');
395 407 if (!p)
396 408 return false;
397 409
398 410 p++;
399 411 if (p[0] == '=' && p[1] == '=')
400 412 p += 2;
401 413 if (p[0] != '$')
402 414 return false;
403 415
404 416 snprintf(buf, sizeof(buf), "%s", p);
405 417
406 418 p = buf;
407 419 p += 1;
408 420 param = strtol(p, &p, 10);
409 421
410 422 p = strchr(p, ']');
411 423 if (!p || *p != ']')
412 424 return false;
413 425 *p = '\0';
414 426
415 427 arg = get_argument_from_call_expr(right->args, param);
416 428 if (!arg)
417 429 return false;
418 430
419 431 /* There should be a get_other_name() function which returns an expr */
420 432 tmp = get_assigned_expr(arg);
421 433 if (tmp)
422 434 arg = tmp;
423 435
424 436 /*
425 437 * This is a sanity check to prevent side effects from evaluating stuff
426 438 * twice.
427 439 */
428 440 str = expr_to_chunk_sym_vsl(arg, NULL, NULL);
429 441 if (!str)
430 442 return false;
431 443 free_string(str);
432 444
433 445 right = gen_expression_from_key(arg, buf);
434 446 if (!right) /* Mostly fails for binops like [$0 + 4032] */
435 447 return false;
436 448 fake_assign = assign_expression(left, '=', right);
437 449 __in_fake_parameter_assign++;
438 450 __split_expr(fake_assign);
439 451 __in_fake_parameter_assign--;
440 452
441 453 /*
442 454 * If the return is "0-65531[$0->nla_len - 4]" the faked expression
443 455 * is maybe (-4)-65531 but we know it is in the 0-65531 range so both
444 456 * parts have to be considered. We use _nomod() because it's not really
445 457 * another modification, it's just a clarification.
446 458 *
447 459 */
448 460 if (estate_rl(orig)) {
449 461 struct smatch_state *faked;
450 462 struct range_list *rl;
451 463
452 464 faked = get_extra_state(left);
↓ open down ↓ |
73 lines elided |
↑ open up ↑ |
453 465 if (estate_rl(faked)) {
454 466 rl = rl_intersection(estate_rl(faked), estate_rl(orig));
455 467 if (rl)
456 468 set_extra_expr_nomod(expr, alloc_estate_rl(rl));
457 469 }
458 470 }
459 471
460 472 return true;
461 473 }
462 474
475 +static void set_fresh_mtag_returns(struct db_callback_info *db_info)
476 +{
477 + struct expression *expr = db_info->expr->left;
478 + struct smatch_state *state;
479 +
480 + if (!db_info->ret_state)
481 + return;
482 +
483 + state = alloc_estate_rl(cast_rl(get_type(expr), clone_rl(estate_rl(db_info->ret_state))));
484 + state = get_mtag_return(db_info->expr, state);
485 + if (!state)
486 + return;
487 +
488 + set_real_absolute(expr, state);
489 + set_extra_expr_mod(expr, state);
490 +
491 + db_info->ret_state = NULL;
492 + db_info->ret_str = NULL;
493 +}
494 +
463 495 static void set_return_assign_state(struct db_callback_info *db_info)
464 496 {
465 497 struct expression *expr = db_info->expr->left;
466 498 struct smatch_state *state;
467 499
468 500 if (!db_info->ret_state)
469 501 return;
470 502
471 503 state = alloc_estate_rl(cast_rl(get_type(expr), clone_rl(estate_rl(db_info->ret_state))));
472 504 if (!fake_a_param_assignment(db_info->expr, db_info->ret_str, state))
473 505 set_extra_expr_mod(expr, state);
474 506
475 507 db_info->ret_state = NULL;
476 508 db_info->ret_str = NULL;
477 509 }
478 510
479 511 static void set_other_side_state(struct db_callback_info *db_info)
480 512 {
481 513 struct expression *expr = db_info->var_expr;
482 514 struct smatch_state *state;
483 515
484 516 if (!db_info->ret_state)
485 517 return;
486 518
487 519 state = alloc_estate_rl(cast_rl(get_type(expr), clone_rl(estate_rl(db_info->ret_state))));
488 520 set_extra_expr_nomod(expr, state);
489 521 db_info->ret_state = NULL;
490 522 db_info->ret_str = NULL;
491 523 }
492 524
493 525 static void handle_ret_equals_param(char *ret_string, struct range_list *rl, struct expression *call)
494 526 {
495 527 char *str;
496 528 long long param;
497 529 struct expression *arg;
498 530 struct range_list *orig;
499 531
500 532 str = strstr(ret_string, "==$");
501 533 if (!str)
502 534 return;
503 535 str += 3;
504 536 param = strtoll(str, NULL, 10);
505 537 arg = get_argument_from_call_expr(call->args, param);
506 538 if (!arg)
507 539 return;
508 540 get_absolute_rl(arg, &orig);
509 541 rl = rl_intersection(orig, rl);
510 542 if (!rl)
511 543 return;
512 544 set_extra_expr_nomod(arg, alloc_estate_rl(rl));
513 545 }
514 546
515 547 static int impossible_limit(struct expression *expr, int param, char *key, char *value)
516 548 {
517 549 struct expression *arg;
518 550 struct smatch_state *state;
519 551 struct range_list *passed;
520 552 struct range_list *limit;
521 553 struct symbol *compare_type;
522 554
523 555 while (expr->type == EXPR_ASSIGNMENT)
524 556 expr = strip_expr(expr->right);
525 557 if (expr->type != EXPR_CALL)
526 558 return 0;
527 559
528 560 arg = get_argument_from_call_expr(expr->args, param);
529 561 if (!arg)
530 562 return 0;
531 563
532 564 if (strcmp(key, "$") == 0) {
533 565 if (!get_implied_rl(arg, &passed))
534 566 return 0;
535 567
536 568 compare_type = get_arg_type(expr->fn, param);
537 569 } else {
538 570 char *name;
539 571 struct symbol *sym;
540 572
541 573 name = get_variable_from_key(arg, key, &sym);
542 574 if (!name || !sym)
543 575 return 0;
544 576
545 577 state = get_state(SMATCH_EXTRA, name, sym);
546 578 if (!state) {
547 579 free_string(name);
548 580 return 0;
549 581 }
550 582 passed = estate_rl(state);
551 583 if (!passed || is_whole_rl(passed)) {
552 584 free_string(name);
553 585 return 0;
554 586 }
555 587
556 588 compare_type = get_member_type_from_key(arg, key);
557 589 }
558 590
559 591 passed = cast_rl(compare_type, passed);
560 592 call_results_to_rl(expr, compare_type, value, &limit);
561 593 if (!limit || is_whole_rl(limit))
562 594 return 0;
563 595 if (possibly_true_rl(passed, SPECIAL_EQUAL, limit))
564 596 return 0;
565 597 if (option_debug || local_debug)
566 598 sm_msg("impossible: %d '%s' limit '%s' == '%s'", param, key, show_rl(passed), value);
567 599 return 1;
568 600 }
569 601
570 602 static int is_impossible_data(int type, struct expression *expr, int param, char *key, char *value)
571 603 {
572 604 if (type == PARAM_LIMIT && impossible_limit(expr, param, key, value))
573 605 return 1;
574 606 if (type == COMPARE_LIMIT && param_compare_limit_is_impossible(expr, param, key, value)) {
575 607 if (local_debug)
576 608 sm_msg("param_compare_limit_is_impossible: %d %s %s", param, key, value);
577 609 return 1;
578 610 }
579 611 return 0;
580 612 }
581 613
582 614 static int func_type_mismatch(struct expression *expr, const char *value)
583 615 {
584 616 struct symbol *type;
585 617
586 618 /* This makes faking returns easier */
587 619 if (!value || value[0] == '\0')
588 620 return 0;
589 621
590 622 while (expr->type == EXPR_ASSIGNMENT)
591 623 expr = strip_expr(expr->right);
592 624
593 625 /*
594 626 * Short cut: We only care about function pointers that are struct
595 627 * members.
596 628 *
597 629 */
598 630 if (expr->fn->type == EXPR_SYMBOL)
599 631 return 0;
600 632
601 633 type = get_type(expr->fn);
602 634 if (!type)
603 635 return 0;
604 636 if (type->type == SYM_PTR)
605 637 type = get_real_base_type(type);
606 638
607 639 if (strcmp(type_to_str(type), value) == 0)
608 640 return 0;
609 641
610 642 return 1;
611 643 }
612 644
613 645 static int db_compare_callback(void *_info, int argc, char **argv, char **azColName)
614 646 {
615 647 struct db_callback_info *db_info = _info;
616 648 struct range_list *var_rl = db_info->rl;
617 649 struct range_list *ret_range;
618 650 int type, param;
619 651 char *ret_str, *key, *value;
620 652 struct return_implies_callback *tmp;
621 653 struct stree *stree;
622 654 int return_id;
623 655 int comparison;
624 656
625 657 if (argc != 6)
626 658 return 0;
627 659
↓ open down ↓ |
155 lines elided |
↑ open up ↑ |
628 660 return_id = atoi(argv[0]);
629 661 ret_str = argv[1];
630 662 type = atoi(argv[2]);
631 663 param = atoi(argv[3]);
632 664 key = argv[4];
633 665 value = argv[5];
634 666
635 667 db_info->has_states = 1;
636 668 if (db_info->prev_return_id != -1 && type == INTERNAL) {
637 669 set_other_side_state(db_info);
670 + set_implied_states(db_info);
638 671 stree = __pop_fake_cur_stree();
639 -
640 672 if (!db_info->cull)
641 673 merge_fake_stree(&db_info->stree, stree);
642 674 free_stree(&stree);
643 675 __push_fake_cur_stree();
644 676 db_info->cull = 0;
645 677 }
646 678 db_info->prev_return_id = return_id;
647 679
648 680 if (type == INTERNAL && func_type_mismatch(db_info->expr, value))
649 681 db_info->cull = 1;
650 682 if (db_info->cull)
651 683 return 0;
652 684 if (type == CULL_PATH) {
653 685 db_info->cull = 1;
654 686 return 0;
655 687 }
656 688
657 689 if (is_impossible_data(type, db_info->expr, param, key, value)) {
658 690 db_info->cull = 1;
659 691 return 0;
660 692 }
661 693
662 694 call_results_to_rl(db_info->expr, get_type(strip_expr(db_info->expr)), ret_str, &ret_range);
663 695 ret_range = cast_rl(get_type(db_info->expr), ret_range);
664 696 if (!ret_range)
↓ open down ↓ |
15 lines elided |
↑ open up ↑ |
665 697 ret_range = alloc_whole_rl(get_type(db_info->expr));
666 698
667 699 comparison = db_info->comparison;
668 700 if (db_info->left)
669 701 comparison = flip_comparison(comparison);
670 702
671 703 if (db_info->true_side) {
672 704 if (!possibly_true_rl(var_rl, comparison, ret_range))
673 705 return 0;
674 706 if (type == PARAM_LIMIT)
675 - param_limit_implications(db_info->expr, param, key, value);
707 + param_limit_implications(db_info->expr, param, key, value, &db_info->implied);
676 708 filter_by_comparison(&var_rl, comparison, ret_range);
677 709 filter_by_comparison(&ret_range, flip_comparison(comparison), var_rl);
678 710 } else {
679 711 if (!possibly_false_rl(var_rl, comparison, ret_range))
680 712 return 0;
681 713 if (type == PARAM_LIMIT)
682 - param_limit_implications(db_info->expr, param, key, value);
714 + param_limit_implications(db_info->expr, param, key, value, &db_info->implied);
683 715 filter_by_comparison(&var_rl, negate_comparison(comparison), ret_range);
684 716 filter_by_comparison(&ret_range, flip_comparison(negate_comparison(comparison)), var_rl);
685 717 }
686 718
687 719 handle_ret_equals_param(ret_str, ret_range, db_info->expr);
688 720
689 721 if (type == INTERNAL) {
690 722 set_state(-1, "unnull_path", NULL, &true_state);
691 723 __add_return_comparison(strip_expr(db_info->expr), ret_str);
692 724 __add_return_to_param_mapping(db_info->expr, ret_str);
693 725 store_return_state(db_info, ret_str, alloc_estate_rl(clone_rl(var_rl)));
694 726 }
695 727
696 728 FOR_EACH_PTR(db_info->callbacks, tmp) {
697 729 if (tmp->type == type)
698 730 tmp->callback(db_info->expr, param, key, value);
699 731 } END_FOR_EACH_PTR(tmp);
700 732
701 733 return 0;
702 734 }
703 735
704 736 static void compare_db_return_states_callbacks(struct expression *left, int comparison, struct expression *right, struct stree *implied_true, struct stree *implied_false)
705 737 {
706 738 struct stree *orig_states;
707 739 struct stree *stree;
708 740 struct stree *true_states;
709 741 struct stree *false_states;
710 742 struct sm_state *sm;
711 743 struct db_callback_info db_info = {};
712 744 struct expression *var_expr;
713 745 struct expression *call_expr;
714 746 struct range_list *rl;
715 747 int call_on_left;
716 748
717 749 orig_states = clone_stree(__get_cur_stree());
718 750
719 751 /* legacy cruft. need to fix call_implies_callbacks(). */
720 752 call_on_left = 1;
721 753 call_expr = left;
722 754 var_expr = right;
723 755 if (left->type != EXPR_CALL) {
724 756 call_on_left = 0;
725 757 call_expr = right;
726 758 var_expr = left;
727 759 }
728 760
729 761 get_absolute_rl(var_expr, &rl);
730 762
731 763 db_info.comparison = comparison;
732 764 db_info.expr = call_expr;
733 765 db_info.rl = rl;
734 766 db_info.left = call_on_left;
735 767 db_info.callbacks = db_return_states_list;
736 768 db_info.var_expr = var_expr;
↓ open down ↓ |
44 lines elided |
↑ open up ↑ |
737 769
738 770 call_return_states_before_hooks();
739 771
740 772 db_info.true_side = 1;
741 773 db_info.stree = NULL;
742 774 db_info.prev_return_id = -1;
743 775 __push_fake_cur_stree();
744 776 sql_select_return_states("return_id, return, type, parameter, key, value",
745 777 call_expr, db_compare_callback, &db_info);
746 778 set_other_side_state(&db_info);
779 + set_implied_states(&db_info);
747 780 stree = __pop_fake_cur_stree();
748 781 if (!db_info.cull)
749 782 merge_fake_stree(&db_info.stree, stree);
750 783 free_stree(&stree);
751 784 true_states = db_info.stree;
752 785 if (!true_states && db_info.has_states) {
753 786 __push_fake_cur_stree();
754 787 set_path_impossible();
755 788 true_states = __pop_fake_cur_stree();
756 789 }
757 790
758 791 nullify_path();
759 792 __unnullify_path();
760 793 FOR_EACH_SM(orig_states, sm) {
761 794 __set_sm_cur_stree(sm);
↓ open down ↓ |
5 lines elided |
↑ open up ↑ |
762 795 } END_FOR_EACH_SM(sm);
763 796
764 797 db_info.true_side = 0;
765 798 db_info.stree = NULL;
766 799 db_info.prev_return_id = -1;
767 800 db_info.cull = 0;
768 801 __push_fake_cur_stree();
769 802 sql_select_return_states("return_id, return, type, parameter, key, value", call_expr,
770 803 db_compare_callback, &db_info);
771 804 set_other_side_state(&db_info);
805 + set_implied_states(&db_info);
772 806 stree = __pop_fake_cur_stree();
773 807 if (!db_info.cull)
774 808 merge_fake_stree(&db_info.stree, stree);
775 809 free_stree(&stree);
776 810 false_states = db_info.stree;
777 811 if (!false_states && db_info.has_states) {
778 812 __push_fake_cur_stree();
779 813 set_path_impossible();
780 814 false_states = __pop_fake_cur_stree();
781 815 }
782 816
783 817 nullify_path();
784 818 __unnullify_path();
785 819 FOR_EACH_SM(orig_states, sm) {
786 820 __set_sm_cur_stree(sm);
787 821 } END_FOR_EACH_SM(sm);
788 822
789 823 free_stree(&orig_states);
790 824
791 825 FOR_EACH_SM(true_states, sm) {
792 826 __set_true_false_sm(sm, NULL);
793 827 } END_FOR_EACH_SM(sm);
794 828 FOR_EACH_SM(false_states, sm) {
795 829 __set_true_false_sm(NULL, sm);
796 830 } END_FOR_EACH_SM(sm);
797 831
798 832 free_stree(&true_states);
799 833 free_stree(&false_states);
800 834
801 835 call_return_states_after_hooks(call_expr);
802 836
803 837 FOR_EACH_SM(implied_true, sm) {
804 838 __set_true_false_sm(sm, NULL);
805 839 } END_FOR_EACH_SM(sm);
806 840 FOR_EACH_SM(implied_false, sm) {
807 841 __set_true_false_sm(NULL, sm);
808 842 } END_FOR_EACH_SM(sm);
809 843 }
810 844
811 845 void function_comparison(struct expression *left, int comparison, struct expression *right)
812 846 {
813 847 struct expression *var_expr;
814 848 struct expression *call_expr;
815 849 struct stree *implied_true = NULL;
816 850 struct stree *implied_false = NULL;
817 851 struct range_list *rl;
818 852 sval_t sval;
819 853 int call_on_left;
820 854
821 855 if (unreachable())
822 856 return;
823 857
824 858 /* legacy cruft. need to fix call_implies_callbacks(). */
825 859 call_on_left = 1;
826 860 call_expr = left;
827 861 var_expr = right;
828 862 if (left->type != EXPR_CALL) {
829 863 call_on_left = 0;
830 864 call_expr = right;
831 865 var_expr = left;
832 866 }
833 867
834 868 get_absolute_rl(var_expr, &rl);
835 869
836 870 if (rl_to_sval(rl, &sval))
837 871 call_implies_callbacks(comparison, call_expr, sval, call_on_left, &implied_true, &implied_false);
838 872
839 873 compare_db_return_states_callbacks(left, comparison, right, implied_true, implied_false);
840 874 free_stree(&implied_true);
841 875 free_stree(&implied_false);
842 876 }
843 877
844 878 static void call_ranged_return_hooks(struct db_callback_info *db_info)
845 879 {
846 880 struct call_back_list *call_backs;
847 881 struct expression *expr;
848 882 struct fcall_back *tmp;
849 883 char *fn;
850 884
851 885 expr = strip_expr(db_info->expr);
852 886 while (expr->type == EXPR_ASSIGNMENT)
853 887 expr = strip_expr(expr->right);
854 888 if (expr->type != EXPR_CALL ||
855 889 expr->fn->type != EXPR_SYMBOL)
856 890 return;
857 891
858 892 fn = expr->fn->symbol_name->name;
859 893
860 894 call_backs = search_callback(func_hash, fn);
861 895 FOR_EACH_PTR(call_backs, tmp) {
862 896 struct range_list *range_rl;
863 897
864 898 if (tmp->type != RANGED_CALL)
865 899 continue;
866 900 range_rl = alloc_rl(tmp->range->min, tmp->range->max);
867 901 range_rl = cast_rl(estate_type(db_info->ret_state), range_rl);
868 902 if (possibly_true_rl(range_rl, SPECIAL_EQUAL, estate_rl(db_info->ret_state)))
869 903 (tmp->u.ranged)(fn, expr, db_info->expr, tmp->info);
870 904 } END_FOR_EACH_PTR(tmp);
871 905 }
872 906
873 907 static int db_assign_return_states_callback(void *_info, int argc, char **argv, char **azColName)
874 908 {
875 909 struct db_callback_info *db_info = _info;
876 910 struct range_list *ret_range;
877 911 int type, param;
878 912 char *ret_str, *key, *value;
879 913 struct return_implies_callback *tmp;
880 914 struct stree *stree;
881 915 int return_id;
882 916
883 917 if (argc != 6)
884 918 return 0;
885 919
↓ open down ↓ |
104 lines elided |
↑ open up ↑ |
886 920 return_id = atoi(argv[0]);
887 921 ret_str = argv[1];
888 922 type = atoi(argv[2]);
889 923 param = atoi(argv[3]);
890 924 key = argv[4];
891 925 value = argv[5];
892 926
893 927 if (db_info->prev_return_id != -1 && type == INTERNAL) {
894 928 call_ranged_return_hooks(db_info);
895 929 set_return_assign_state(db_info);
930 + set_implied_states(db_info);
896 931 stree = __pop_fake_cur_stree();
897 932 if (!db_info->cull)
898 933 merge_fake_stree(&db_info->stree, stree);
899 934 free_stree(&stree);
900 935 __push_fake_cur_stree();
901 936 db_info->cull = 0;
902 937 }
903 938 db_info->prev_return_id = return_id;
904 939
905 940 if (type == INTERNAL && func_type_mismatch(db_info->expr, value))
906 941 db_info->cull = 1;
907 942 if (db_info->cull)
908 943 return 0;
↓ open down ↓ |
3 lines elided |
↑ open up ↑ |
909 944 if (type == CULL_PATH) {
910 945 db_info->cull = 1;
911 946 return 0;
912 947 }
913 948 if (is_impossible_data(type, db_info->expr, param, key, value)) {
914 949 db_info->cull = 1;
915 950 return 0;
916 951 }
917 952
918 953 if (type == PARAM_LIMIT)
919 - param_limit_implications(db_info->expr, param, key, value);
954 + param_limit_implications(db_info->expr, param, key, value, &db_info->implied);
920 955
921 956 db_info->handled = 1;
922 957 call_results_to_rl(db_info->expr->right, get_type(strip_expr(db_info->expr->right)), ret_str, &ret_range);
923 958 if (!ret_range)
924 959 ret_range = alloc_whole_rl(get_type(strip_expr(db_info->expr->right)));
925 960 ret_range = cast_rl(get_type(db_info->expr->right), ret_range);
926 961
927 962 if (type == INTERNAL) {
928 963 set_state(-1, "unnull_path", NULL, &true_state);
929 964 __add_return_comparison(strip_expr(db_info->expr->right), ret_str);
930 965 __add_comparison_info(db_info->expr->left, strip_expr(db_info->expr->right), ret_str);
931 966 __add_return_to_param_mapping(db_info->expr, ret_str);
932 967 store_return_state(db_info, ret_str, alloc_estate_rl(ret_range));
968 + set_fresh_mtag_returns(db_info);
933 969 }
934 970
935 971 FOR_EACH_PTR(db_return_states_list, tmp) {
936 972 if (tmp->type == type)
937 973 tmp->callback(db_info->expr, param, key, value);
938 974 } END_FOR_EACH_PTR(tmp);
939 975
940 976 return 0;
941 977 }
942 978
943 979 static int db_return_states_assign(struct expression *expr)
944 980 {
945 981 struct expression *right;
946 982 struct sm_state *sm;
947 983 struct stree *stree;
948 984 struct db_callback_info db_info = {};
949 985
950 986 right = strip_expr(expr->right);
951 987
952 988 db_info.prev_return_id = -1;
953 989 db_info.expr = expr;
954 990 db_info.stree = NULL;
955 991 db_info.handled = 0;
956 992
957 993 call_return_states_before_hooks();
958 994
959 995 __push_fake_cur_stree();
960 996 sql_select_return_states("return_id, return, type, parameter, key, value",
↓ open down ↓ |
18 lines elided |
↑ open up ↑ |
961 997 right, db_assign_return_states_callback, &db_info);
962 998 if (option_debug) {
963 999 sm_msg("%s return_id %d return_ranges %s",
964 1000 db_info.cull ? "culled" : "merging",
965 1001 db_info.prev_return_id,
966 1002 db_info.ret_state ? db_info.ret_state->name : "'<empty>'");
967 1003 }
968 1004 if (db_info.handled)
969 1005 call_ranged_return_hooks(&db_info);
970 1006 set_return_assign_state(&db_info);
1007 + set_implied_states(&db_info);
971 1008 stree = __pop_fake_cur_stree();
972 1009 if (!db_info.cull)
973 1010 merge_fake_stree(&db_info.stree, stree);
974 1011 free_stree(&stree);
975 1012
976 1013 if (!db_info.stree && db_info.cull) { /* this means we culled everything */
977 1014 set_extra_expr_mod(expr->left, alloc_estate_whole(get_type(expr->left)));
978 1015 set_path_impossible();
979 1016 }
980 1017 FOR_EACH_SM(db_info.stree, sm) {
981 1018 __set_sm(sm);
982 1019 } END_FOR_EACH_SM(sm);
983 1020
984 1021 free_stree(&db_info.stree);
985 1022 call_return_states_after_hooks(right);
986 1023
987 1024 return db_info.handled;
988 1025 }
989 1026
990 1027 static int handle_implied_return(struct expression *expr)
991 1028 {
992 1029 struct range_list *rl;
993 1030
994 1031 if (!get_implied_return(expr->right, &rl))
995 1032 return 0;
996 1033 rl = cast_rl(get_type(expr->left), rl);
997 1034 set_extra_expr_mod(expr->left, alloc_estate_rl(rl));
998 1035 return 1;
999 1036 }
1000 1037
1001 1038 static void match_assign_call(struct expression *expr)
1002 1039 {
1003 1040 struct call_back_list *call_backs;
1004 1041 const char *fn;
1005 1042 struct expression *right;
1006 1043 int handled = 0;
1007 1044 struct range_list *rl;
1008 1045
1009 1046 if (expr->op != '=')
1010 1047 return;
1011 1048
1012 1049 right = strip_expr(expr->right);
1013 1050 if (right->fn->type != EXPR_SYMBOL || !right->fn->symbol) {
1014 1051 handled |= db_return_states_assign(expr);
1015 1052 if (!handled)
1016 1053 goto assigned_unknown;
1017 1054 return;
1018 1055 }
1019 1056 if (is_fake_call(right)) {
1020 1057 set_extra_expr_mod(expr->left, alloc_estate_whole(get_type(expr->left)));
1021 1058 return;
1022 1059 }
1023 1060
1024 1061 fn = right->fn->symbol->ident->name;
1025 1062 call_backs = search_callback(func_hash, (char *)fn);
1026 1063
1027 1064 /*
1028 1065 * The ordering here is sort of important.
1029 1066 * One example, of how this matters is that when we do:
1030 1067 *
1031 1068 * len = strlen(str);
1032 1069 *
1033 1070 * That is handled by smatch_common_functions.c and smatch_strlen.c.
1034 1071 * They use implied_return and function_assign_hook respectively.
1035 1072 * We want to get the implied return first before we do the function
1036 1073 * assignment hook otherwise we end up writing the wrong thing for len
1037 1074 * in smatch_extra.c because we assume that it already holds the
1038 1075 * strlen() when we haven't set it yet.
1039 1076 */
1040 1077
1041 1078 if (db_return_states_assign(expr) == 1)
1042 1079 handled = 1;
1043 1080 else
1044 1081 handled = assign_ranged_funcs(fn, expr, call_backs);
1045 1082 handled |= handle_implied_return(expr);
1046 1083
1047 1084
1048 1085 call_call_backs(call_backs, ASSIGN_CALL, fn, expr);
1049 1086
1050 1087 if (handled)
1051 1088 return;
1052 1089
1053 1090 assigned_unknown:
1054 1091 get_absolute_rl(expr->right, &rl);
1055 1092 rl = cast_rl(get_type(expr->left), rl);
1056 1093 set_extra_expr_mod(expr->left, alloc_estate_rl(rl));
1057 1094 }
1058 1095
1059 1096 static int db_return_states_callback(void *_info, int argc, char **argv, char **azColName)
1060 1097 {
1061 1098 struct db_callback_info *db_info = _info;
1062 1099 struct range_list *ret_range;
1063 1100 int type, param;
1064 1101 char *ret_str, *key, *value;
1065 1102 struct return_implies_callback *tmp;
1066 1103 struct stree *stree;
1067 1104 int return_id;
1068 1105 char buf[64];
1069 1106
1070 1107 if (argc != 6)
1071 1108 return 0;
↓ open down ↓ |
91 lines elided |
↑ open up ↑ |
1072 1109
1073 1110 return_id = atoi(argv[0]);
1074 1111 ret_str = argv[1];
1075 1112 type = atoi(argv[2]);
1076 1113 param = atoi(argv[3]);
1077 1114 key = argv[4];
1078 1115 value = argv[5];
1079 1116
1080 1117 if (db_info->prev_return_id != -1 && type == INTERNAL) {
1081 1118 call_ranged_return_hooks(db_info);
1119 + set_implied_states(db_info);
1082 1120 stree = __pop_fake_cur_stree();
1083 1121 if (!db_info->cull)
1084 1122 merge_fake_stree(&db_info->stree, stree);
1085 1123 free_stree(&stree);
1086 1124 __push_fake_cur_stree();
1087 1125 __unnullify_path();
1088 1126 db_info->cull = 0;
1089 1127 }
1090 1128 db_info->prev_return_id = return_id;
1091 1129
1092 1130 if (type == INTERNAL && func_type_mismatch(db_info->expr, value))
1093 1131 db_info->cull = 1;
1094 1132 if (db_info->cull)
1095 1133 return 0;
↓ open down ↓ |
4 lines elided |
↑ open up ↑ |
1096 1134 if (type == CULL_PATH) {
1097 1135 db_info->cull = 1;
1098 1136 return 0;
1099 1137 }
1100 1138 if (is_impossible_data(type, db_info->expr, param, key, value)) {
1101 1139 db_info->cull = 1;
1102 1140 return 0;
1103 1141 }
1104 1142
1105 1143 if (type == PARAM_LIMIT)
1106 - param_limit_implications(db_info->expr, param, key, value);
1144 + param_limit_implications(db_info->expr, param, key, value, &db_info->implied);
1107 1145
1108 1146 call_results_to_rl(db_info->expr, get_type(strip_expr(db_info->expr)), ret_str, &ret_range);
1109 1147 ret_range = cast_rl(get_type(db_info->expr), ret_range);
1110 1148
1111 1149 if (type == INTERNAL) {
1112 1150 struct smatch_state *state;
1113 1151
1114 1152 set_state(-1, "unnull_path", NULL, &true_state);
1115 1153 __add_return_comparison(strip_expr(db_info->expr), ret_str);
1116 1154 __add_return_to_param_mapping(db_info->expr, ret_str);
1117 1155 /*
1118 1156 * We want to store the return values so that we can split the strees
1119 1157 * in smatch_db.c. This uses set_state() directly because it's not a
1120 1158 * real smatch_extra state.
1121 1159 */
1122 1160 snprintf(buf, sizeof(buf), "return %p", db_info->expr);
1123 1161 state = alloc_estate_rl(ret_range);
1124 1162 set_state(SMATCH_EXTRA, buf, NULL, state);
1125 1163 store_return_state(db_info, ret_str, state);
1126 1164 }
1127 1165
1128 1166 FOR_EACH_PTR(db_return_states_list, tmp) {
1129 1167 if (tmp->type == type)
1130 1168 tmp->callback(db_info->expr, param, key, value);
1131 1169 } END_FOR_EACH_PTR(tmp);
1132 1170
1133 1171
1134 1172 return 0;
1135 1173 }
1136 1174
1137 1175 static void db_return_states(struct expression *expr)
1138 1176 {
1139 1177 struct sm_state *sm;
1140 1178 struct stree *stree;
1141 1179 struct db_callback_info db_info = {};
1142 1180
1143 1181 if (!__get_cur_stree()) /* no return functions */
1144 1182 return;
1145 1183
1146 1184 db_info.prev_return_id = -1;
↓ open down ↓ |
30 lines elided |
↑ open up ↑ |
1147 1185 db_info.expr = expr;
1148 1186 db_info.stree = NULL;
1149 1187
1150 1188 call_return_states_before_hooks();
1151 1189
1152 1190 __push_fake_cur_stree();
1153 1191 __unnullify_path();
1154 1192 sql_select_return_states("return_id, return, type, parameter, key, value",
1155 1193 expr, db_return_states_callback, &db_info);
1156 1194 call_ranged_return_hooks(&db_info);
1195 + set_implied_states(&db_info);
1157 1196 stree = __pop_fake_cur_stree();
1158 1197 if (!db_info.cull)
1159 1198 merge_fake_stree(&db_info.stree, stree);
1160 1199 free_stree(&stree);
1161 1200
1162 1201 FOR_EACH_SM(db_info.stree, sm) {
1163 1202 __set_sm(sm);
1164 1203 } END_FOR_EACH_SM(sm);
1165 1204
1166 1205 free_stree(&db_info.stree);
1167 1206 call_return_states_after_hooks(expr);
1168 1207 }
1169 1208
1170 1209 static int is_condition_call(struct expression *expr)
1171 1210 {
1172 1211 struct expression *tmp;
1173 1212
1174 1213 FOR_EACH_PTR_REVERSE(big_condition_stack, tmp) {
1175 1214 if (expr == tmp || expr_get_parent_expr(expr) == tmp)
1176 1215 return 1;
1177 1216 if (tmp->pos.line < expr->pos.line)
1178 1217 return 0;
1179 1218 } END_FOR_EACH_PTR_REVERSE(tmp);
1180 1219
1181 1220 return 0;
1182 1221 }
1183 1222
1184 1223 static void db_return_states_call(struct expression *expr)
1185 1224 {
1186 1225 if (unreachable())
1187 1226 return;
1188 1227
1189 1228 if (is_assigned_call(expr))
1190 1229 return;
1191 1230 if (is_condition_call(expr))
1192 1231 return;
1193 1232 db_return_states(expr);
1194 1233 }
1195 1234
1196 1235 static void match_function_call(struct expression *expr)
1197 1236 {
1198 1237 struct call_back_list *call_backs;
1199 1238 struct expression *fn;
1200 1239
1201 1240 fn = strip_expr(expr->fn);
1202 1241 if (fn->type == EXPR_SYMBOL && fn->symbol) {
1203 1242 call_backs = search_callback(func_hash, (char *)fn->symbol->ident->name);
1204 1243 if (call_backs)
1205 1244 call_call_backs(call_backs, REGULAR_CALL,
1206 1245 fn->symbol->ident->name, expr);
1207 1246 }
1208 1247 db_return_states_call(expr);
1209 1248 }
1210 1249
1211 1250 static void match_macro_assign(struct expression *expr)
1212 1251 {
1213 1252 struct call_back_list *call_backs;
1214 1253 const char *macro;
1215 1254 struct expression *right;
1216 1255
1217 1256 right = strip_expr(expr->right);
1218 1257 macro = get_macro_name(right->pos);
1219 1258 call_backs = search_callback(func_hash, (char *)macro);
1220 1259 if (!call_backs)
1221 1260 return;
1222 1261 call_call_backs(call_backs, MACRO_ASSIGN, macro, expr);
1223 1262 call_call_backs(call_backs, MACRO_ASSIGN_EXTRA, macro, expr);
1224 1263 }
1225 1264
1226 1265 int get_implied_return(struct expression *expr, struct range_list **rl)
1227 1266 {
1228 1267 struct call_back_list *call_backs;
1229 1268 struct fcall_back *tmp;
1230 1269 int handled = 0;
1231 1270 char *fn;
1232 1271
1233 1272 *rl = NULL;
1234 1273
1235 1274 expr = strip_expr(expr);
1236 1275 fn = expr_to_var(expr->fn);
1237 1276 if (!fn)
1238 1277 goto out;
1239 1278
1240 1279 call_backs = search_callback(func_hash, fn);
1241 1280
1242 1281 FOR_EACH_PTR(call_backs, tmp) {
1243 1282 if (tmp->type == IMPLIED_RETURN) {
1244 1283 (tmp->u.implied_return)(expr, tmp->info, rl);
1245 1284 handled = 1;
1246 1285 }
1247 1286 } END_FOR_EACH_PTR(tmp);
1248 1287
1249 1288 out:
1250 1289 free_string(fn);
1251 1290 return handled;
1252 1291 }
1253 1292
1254 1293 void create_function_hook_hash(void)
1255 1294 {
1256 1295 func_hash = create_function_hashtable(5000);
1257 1296 }
1258 1297
1259 1298 void register_function_hooks(int id)
1260 1299 {
1261 1300 add_hook(&match_function_call, CALL_HOOK_AFTER_INLINE);
1262 1301 add_hook(&match_assign_call, CALL_ASSIGNMENT_HOOK);
1263 1302 add_hook(&match_macro_assign, MACRO_ASSIGNMENT_HOOK);
1264 1303 }
↓ open down ↓ |
98 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX