Print this page
12826 update to smatch 0.6.1-rc1-il-6
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/tools/smatch/src/smatch_db.c
+++ new/usr/src/tools/smatch/src/smatch_db.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 #include <string.h>
19 19 #include <errno.h>
20 20 #include <unistd.h>
21 21 #include <ctype.h>
22 22 #include "smatch.h"
23 23 #include "smatch_slist.h"
24 24 #include "smatch_extra.h"
25 25
26 26 struct sqlite3 *smatch_db;
27 27 struct sqlite3 *mem_db;
28 28 struct sqlite3 *cache_db;
29 29
30 30 int debug_db;
31 31
32 32 static int return_id;
33 33
34 34 static void call_return_state_hooks(struct expression *expr);
35 35
36 36 #define SQLITE_CACHE_PAGES 1000
37 37
38 38 struct def_callback {
39 39 int hook_type;
40 40 void (*callback)(const char *name, struct symbol *sym, char *key, char *value);
41 41 };
42 42 ALLOCATOR(def_callback, "definition db hook callbacks");
43 43 DECLARE_PTR_LIST(callback_list, struct def_callback);
44 44 static struct callback_list *select_caller_info_callbacks;
45 45
46 46 struct member_info_callback {
47 47 int owner;
48 48 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm);
49 49 };
50 50 ALLOCATOR(member_info_callback, "caller_info callbacks");
51 51 DECLARE_PTR_LIST(member_info_cb_list, struct member_info_callback);
52 52 static struct member_info_cb_list *member_callbacks;
53 53 static struct member_info_cb_list *member_callbacks_new;
54 54
55 55 struct returned_state_callback {
56 56 void (*callback)(int return_id, char *return_ranges, struct expression *return_expr);
57 57 };
58 58 ALLOCATOR(returned_state_callback, "returned state callbacks");
59 59 DECLARE_PTR_LIST(returned_state_cb_list, struct returned_state_callback);
60 60 static struct returned_state_cb_list *returned_state_callbacks;
61 61
62 62 struct returned_member_callback {
63 63 int owner;
64 64 void (*callback)(int return_id, char *return_ranges, struct expression *expr, char *printed_name, struct smatch_state *state);
65 65 };
66 66 ALLOCATOR(returned_member_callback, "returned member callbacks");
67 67 DECLARE_PTR_LIST(returned_member_cb_list, struct returned_member_callback);
68 68 static struct returned_member_cb_list *returned_member_callbacks;
69 69
70 70 struct db_implies_callback {
71 71 int type;
72 72 void (*callback)(struct expression *call, struct expression *arg, char *key, char *value);
73 73 };
74 74 ALLOCATOR(db_implies_callback, "return_implies callbacks");
75 75 DECLARE_PTR_LIST(db_implies_cb_list, struct db_implies_callback);
76 76 static struct db_implies_cb_list *return_implies_cb_list;
77 77 static struct db_implies_cb_list *call_implies_cb_list;
78 78
79 79 /* silently truncates if needed. */
80 80 char *escape_newlines(const char *str)
81 81 {
82 82 char buf[1024] = "";
83 83 bool found = false;
84 84 int i, j;
85 85
86 86 for (i = 0, j = 0; str[i] != '\0' && j != sizeof(buf); i++, j++) {
87 87 if (str[i] != '\r' && str[i] != '\n') {
88 88 buf[j] = str[i];
89 89 continue;
90 90 }
91 91
92 92 found = true;
93 93 buf[j++] = '\\';
94 94 if (j == sizeof(buf))
95 95 break;
96 96 buf[j] = 'n';
97 97 }
98 98
99 99 if (!found)
100 100 return alloc_sname(str);
101 101
102 102 if (j == sizeof(buf))
103 103 buf[j - 1] = '\0';
104 104 return alloc_sname(buf);
105 105 }
106 106
107 107 static int print_sql_output(void *unused, int argc, char **argv, char **azColName)
108 108 {
109 109 int i;
110 110
111 111 for (i = 0; i < argc; i++) {
112 112 if (i != 0)
113 113 sm_printf(", ");
114 114 sm_printf("%s", argv[i]);
115 115 }
116 116 sm_printf("\n");
117 117 return 0;
118 118 }
119 119
120 120 void sql_exec(struct sqlite3 *db, int (*callback)(void*, int, char**, char**), void *data, const char *sql)
121 121 {
122 122 char *err = NULL;
123 123 int rc;
124 124
125 125 if (!db)
126 126 return;
127 127
128 128 if (option_debug || debug_db) {
129 129 sm_msg("%s", sql);
130 130 if (strncasecmp(sql, "select", strlen("select")) == 0)
131 131 sqlite3_exec(db, sql, print_sql_output, NULL, NULL);
132 132 }
133 133
134 134 rc = sqlite3_exec(db, sql, callback, data, &err);
135 135 if (rc != SQLITE_OK && !parse_error) {
136 136 sm_ierror("%s:%d SQL error #2: %s\n", get_filename(), get_lineno(), err);
137 137 sm_ierror("%s:%d SQL: '%s'\n", get_filename(), get_lineno(), sql);
138 138 parse_error = 1;
139 139 }
140 140 }
141 141
142 142 static int replace_count;
143 143 static char **replace_table;
144 144 static const char *replace_return_ranges(const char *return_ranges)
145 145 {
146 146 int i;
147 147
148 148 if (!get_function()) {
149 149 /* I have no idea why EXPORT_SYMBOL() is here */
150 150 return return_ranges;
151 151 }
152 152 for (i = 0; i < replace_count; i += 3) {
153 153 if (strcmp(replace_table[i + 0], get_function()) == 0) {
154 154 if (strcmp(replace_table[i + 1], return_ranges) == 0)
155 155 return replace_table[i + 2];
156 156 }
157 157 }
158 158 return return_ranges;
159 159 }
160 160
161 161
162 162 static char *use_states;
163 163 static int get_db_state_count(void)
164 164 {
165 165 struct sm_state *sm;
166 166 int count = 0;
167 167
168 168 FOR_EACH_SM(__get_cur_stree(), sm) {
169 169 if (sm->owner == USHRT_MAX)
170 170 continue;
171 171 if (use_states[sm->owner])
172 172 count++;
173 173 } END_FOR_EACH_SM(sm);
174 174 return count;
175 175 }
176 176
177 177 void db_ignore_states(int id)
178 178 {
179 179 use_states[id] = 0;
180 180 }
181 181
182 182 unsigned long long __fn_mtag;
183 183 static void set_fn_mtag(struct symbol *sym)
184 184 {
185 185 char buf[128];
186 186
187 187 if (cur_func_sym->ctype.modifiers & MOD_STATIC)
188 188 snprintf(buf, sizeof(buf), "%s %s", get_base_file(), get_function());
189 189 else
190 190 snprintf(buf, sizeof(buf), "extern %s", get_function());
191 191
192 192 __fn_mtag = str_to_mtag(buf);
193 193 }
194 194
195 195 void sql_insert_return_states(int return_id, const char *return_ranges,
196 196 int type, int param, const char *key, const char *value)
197 197 {
198 198 unsigned long long id;
199 199
200 200
201 201 if (key && strlen(key) >= 80)
202 202 return;
203 203 if (__inline_fn)
204 204 id = (unsigned long)__inline_fn;
205 205 else
206 206 id = __fn_mtag;
207 207
208 208 return_ranges = replace_return_ranges(return_ranges);
209 209 sql_insert(return_states, "'%s', '%s', %llu, %d, '%s', %d, %d, %d, '%s', '%s'",
210 210 get_base_file(), get_function(), id, return_id,
211 211 return_ranges, fn_static(), type, param, key, value);
212 212 }
213 213
214 214 static struct string_list *common_funcs;
215 215 static int is_common_function(const char *fn)
216 216 {
217 217 char *tmp;
218 218
219 219 if (!fn)
220 220 return 0;
221 221
222 222 if (strncmp(fn, "__builtin_", 10) == 0)
223 223 return 1;
224 224
225 225 FOR_EACH_PTR(common_funcs, tmp) {
226 226 if (strcmp(tmp, fn) == 0)
227 227 return 1;
228 228 } END_FOR_EACH_PTR(tmp);
229 229
230 230 return 0;
231 231 }
232 232
233 233 static char *function_signature(void)
234 234 {
235 235 return type_to_str(get_real_base_type(cur_func_sym));
236 236 }
237 237
238 238 void sql_insert_caller_info(struct expression *call, int type,
239 239 int param, const char *key, const char *value)
240 240 {
241 241 FILE *tmp_fd = sm_outfd;
242 242 char *fn;
243 243
244 244 if (!option_info && !__inline_call)
245 245 return;
246 246
247 247 if (key && strlen(key) >= 80)
248 248 return;
249 249
250 250 fn = get_fnptr_name(call->fn);
251 251 if (!fn)
252 252 return;
253 253
254 254 if (__inline_call) {
255 255 mem_sql(NULL, NULL,
256 256 "insert into caller_info values ('%s', '%s', '%s', %lu, %d, %d, %d, '%s', '%s');",
257 257 get_base_file(), get_function(), fn, (unsigned long)call,
258 258 is_static(call->fn), type, param, key, value);
259 259 }
260 260
261 261 if (!option_info)
262 262 return;
263 263
264 264 if (strncmp(fn, "__builtin_", 10) == 0)
265 265 return;
266 266 if (type != INTERNAL && is_common_function(fn))
267 267 return;
268 268
269 269 sm_outfd = caller_info_fd;
270 270 sm_msg("SQL_caller_info: insert into caller_info values ("
271 271 "'%s', '%s', '%s', %%CALL_ID%%, %d, %d, %d, '%s', '%s');",
272 272 get_base_file(), get_function(), fn, is_static(call->fn),
273 273 type, param, key, value);
274 274 sm_outfd = tmp_fd;
275 275
276 276 free_string(fn);
277 277 }
278 278
279 279 void sql_insert_function_ptr(const char *fn, const char *struct_name)
280 280 {
281 281 sql_insert_or_ignore(function_ptr, "'%s', '%s', '%s', 0",
282 282 get_base_file(), fn, struct_name);
283 283 }
284 284
285 285 void sql_insert_return_implies(int type, int param, const char *key, const char *value)
286 286 {
287 287 sql_insert_or_ignore(return_implies, "'%s', '%s', %lu, %d, %d, %d, '%s', '%s'",
288 288 get_base_file(), get_function(), (unsigned long)__inline_fn,
289 289 fn_static(), type, param, key, value);
290 290 }
291 291
292 292 void sql_insert_call_implies(int type, int param, const char *key, const char *value)
293 293 {
294 294 sql_insert_or_ignore(call_implies, "'%s', '%s', %lu, %d, %d, %d, '%s', '%s'",
295 295 get_base_file(), get_function(), (unsigned long)__inline_fn,
296 296 fn_static(), type, param, key, value);
297 297 }
298 298
299 299 void sql_insert_function_type_size(const char *member, const char *ranges)
300 300 {
301 301 sql_insert(function_type_size, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), member, ranges);
302 302 }
303 303
304 304 void sql_insert_function_type_info(int type, const char *struct_type, const char *member, const char *value)
305 305 {
306 306 sql_insert(function_type_info, "'%s', '%s', %d, '%s', '%s', '%s'", get_base_file(), get_function(), type, struct_type, member, value);
307 307 }
308 308
309 309 void sql_insert_type_info(int type, const char *member, const char *value)
310 310 {
311 311 sql_insert_cache(type_info, "'%s', %d, '%s', '%s'", get_base_file(), type, member, value);
312 312 }
313 313
314 314 void sql_insert_local_values(const char *name, const char *value)
315 315 {
316 316 sql_insert(local_values, "'%s', '%s', '%s'", get_base_file(), name, value);
317 317 }
318 318
319 319 void sql_insert_function_type_value(const char *type, const char *value)
320 320 {
321 321 sql_insert(function_type_value, "'%s', '%s', '%s', '%s'", get_base_file(), get_function(), type, value);
322 322 }
323 323
324 324 void sql_insert_function_type(int param, const char *value)
325 325 {
326 326 sql_insert(function_type, "'%s', '%s', %d, %d, '%s'",
327 327 get_base_file(), get_function(), fn_static(), param, value);
328 328 }
329 329
330 330 void sql_insert_parameter_name(int param, const char *value)
331 331 {
332 332 sql_insert(parameter_name, "'%s', '%s', %d, %d, '%s'",
333 333 get_base_file(), get_function(), fn_static(), param, value);
334 334 }
335 335
336 336 void sql_insert_data_info(struct expression *data, int type, const char *value)
337 337 {
338 338 char *data_name;
339 339
340 340 data_name = get_data_info_name(data);
341 341 if (!data_name)
342 342 return;
343 343 sql_insert(data_info, "'%s', '%s', %d, '%s'",
344 344 is_static(data) ? get_base_file() : "extern",
345 345 data_name, type, value);
346 346 }
347 347
348 348 void sql_insert_data_info_var_sym(const char *var, struct symbol *sym, int type, const char *value)
349 349 {
350 350 sql_insert(data_info, "'%s', '%s', %d, '%s'",
351 351 (sym->ctype.modifiers & MOD_STATIC) ? get_base_file() : "extern",
352 352 var, type, value);
353 353 }
354 354
355 355 void sql_save_constraint(const char *con)
356 356 {
357 357 if (!option_info)
358 358 return;
359 359
360 360 sm_msg("SQL: insert or ignore into constraints (str) values('%s');", escape_newlines(con));
361 361 }
362 362
363 363 void sql_save_constraint_required(const char *data, int op, const char *limit)
364 364 {
365 365 sql_insert_or_ignore(constraints_required, "'%s', '%s', '%s'", data, show_special(op), limit);
366 366 }
367 367
368 368 void sql_copy_constraint_required(const char *new_limit, const char *old_limit)
369 369 {
370 370 if (!option_info)
371 371 return;
372 372
373 373 sm_msg("SQL_late: insert or ignore into constraints_required (data, op, bound) "
374 374 "select constraints_required.data, constraints_required.op, '%s' from "
375 375 "constraints_required where bound = '%s';", new_limit, old_limit);
376 376 }
377 377
378 378 void sql_insert_fn_ptr_data_link(const char *ptr, const char *data)
379 379 {
380 380 sql_insert_or_ignore(fn_ptr_data_link, "'%s', '%s'", ptr, data);
381 381 }
382 382
383 383 void sql_insert_fn_data_link(struct expression *fn, int type, int param, const char *key, const char *value)
384 384 {
385 385 if (fn->type != EXPR_SYMBOL || !fn->symbol->ident)
386 386 return;
387 387
388 388 sql_insert(fn_data_link, "'%s', '%s', %d, %d, %d, '%s', '%s'",
389 389 (fn->symbol->ctype.modifiers & MOD_STATIC) ? get_base_file() : "extern",
390 390 fn->symbol->ident->name,
391 391 !!(fn->symbol->ctype.modifiers & MOD_STATIC),
392 392 type, param, key, value);
393 393 }
394 394
395 395 void sql_insert_mtag_about(mtag_t tag, const char *left_name, const char *right_name)
396 396 {
397 397 sql_insert_cache(mtag_about, "%lld, '%s', '%s', %d, '%s', '%s'",
398 398 tag, get_filename(), get_function(), get_lineno(),
399 399 left_name, right_name);
400 400 }
401 401
402 402 void sql_insert_mtag_info(mtag_t tag, int type, const char *value)
403 403 {
404 404 sql_insert_cache(mtag_info, "'%s', %lld, %d, '%s'", get_filename(), tag, type, value);
405 405 }
406 406
407 407 void sql_insert_mtag_map(mtag_t container, int container_offset, mtag_t tag, int tag_offset)
408 408 {
409 409 sql_insert(mtag_map, "%lld, %d, %lld, %d", container, container_offset, tag, tag_offset);
410 410 }
411 411
412 412 void sql_insert_mtag_alias(mtag_t orig, mtag_t alias)
413 413 {
414 414 sql_insert(mtag_alias, "%lld, %lld", orig, alias);
415 415 }
416 416
417 417 static int save_mtag(void *_tag, int argc, char **argv, char **azColName)
418 418 {
419 419 mtag_t *saved_tag = _tag;
420 420 mtag_t new_tag;
421 421
422 422 new_tag = strtoll(argv[0], NULL, 10);
423 423
424 424 if (!*saved_tag)
425 425 *saved_tag = new_tag;
426 426 else if (*saved_tag != new_tag)
427 427 *saved_tag = -1ULL;
428 428
429 429 return 0;
430 430 }
431 431
432 432 int mtag_map_select_container(mtag_t tag, int container_offset, mtag_t *container)
433 433 {
434 434 mtag_t tmp = 0;
435 435
436 436 run_sql(save_mtag, &tmp,
437 437 "select container from mtag_map where tag = %lld and container_offset = %d and tag_offset = 0;",
438 438 tag, container_offset);
439 439
440 440 if (tmp == 0 || tmp == -1ULL)
441 441 return 0;
442 442 *container = tmp;
443 443 return 1;
444 444 }
445 445
446 446 int mtag_map_select_tag(mtag_t container, int offset, mtag_t *tag)
447 447 {
448 448 mtag_t tmp = 0;
449 449
450 450 run_sql(save_mtag, &tmp,
451 451 "select tag from mtag_map where container = %lld and container_offset = %d;",
452 452 container, offset);
453 453
454 454 if (tmp == 0 || tmp == -1ULL)
455 455 return 0;
456 456 *tag = tmp;
457 457 return 1;
458 458 }
459 459
460 460 char *get_static_filter(struct symbol *sym)
461 461 {
462 462 static char sql_filter[1024];
463 463
464 464 /* This can only happen on buggy code. Return invalid SQL. */
465 465 if (!sym) {
466 466 sql_filter[0] = '\0';
467 467 return sql_filter;
468 468 }
469 469
470 470 if (sym->ctype.modifiers & MOD_STATIC) {
471 471 snprintf(sql_filter, sizeof(sql_filter),
472 472 "file = '%s' and function = '%s' and static = '1'",
473 473 get_base_file(), sym->ident->name);
474 474 } else {
475 475 snprintf(sql_filter, sizeof(sql_filter),
476 476 "function = '%s' and static = '0'", sym->ident->name);
477 477 }
478 478
479 479 return sql_filter;
480 480 }
481 481
482 482 static int get_row_count(void *_row_count, int argc, char **argv, char **azColName)
483 483 {
484 484 int *row_count = _row_count;
485 485
486 486 *row_count = 0;
487 487 if (argc != 1)
488 488 return 0;
489 489 *row_count = atoi(argv[0]);
490 490 return 0;
491 491 }
492 492
493 493 static void mark_call_params_untracked(struct expression *call)
494 494 {
495 495 struct expression *arg;
496 496 int i = 0;
497 497
498 498 FOR_EACH_PTR(call->args, arg) {
499 499 mark_untracked(call, i++, "$", NULL);
500 500 } END_FOR_EACH_PTR(arg);
501 501 }
502 502
503 503 static void sql_select_return_states_pointer(const char *cols,
504 504 struct expression *call, int (*callback)(void*, int, char**, char**), void *info)
505 505 {
506 506 char *ptr;
507 507 int return_count = 0;
508 508
509 509 ptr = get_fnptr_name(call->fn);
510 510 if (!ptr)
511 511 return;
512 512
513 513 run_sql(get_row_count, &return_count,
514 514 "select count(*) from return_states join function_ptr "
515 515 "where return_states.function == function_ptr.function and "
516 516 "ptr = '%s' and searchable = 1 and type = %d;", ptr, INTERNAL);
517 517 /* The magic number 100 is just from testing on the kernel. */
518 518 if (return_count > 100) {
519 519 mark_call_params_untracked(call);
520 520 return;
521 521 }
522 522
523 523 run_sql(callback, info,
524 524 "select %s from return_states join function_ptr where "
525 525 "return_states.function == function_ptr.function and ptr = '%s' "
526 526 "and searchable = 1 "
527 527 "order by function_ptr.file, return_states.file, return_id, type;",
528 528 cols, ptr);
529 529 }
530 530
531 531 static int is_local_symbol(struct expression *expr)
532 532 {
533 533 if (expr->type != EXPR_SYMBOL)
534 534 return 0;
535 535 if (expr->symbol->ctype.modifiers & (MOD_NONLOCAL | MOD_STATIC | MOD_ADDRESSABLE))
536 536 return 0;
537 537 return 1;
538 538 }
539 539
540 540 void sql_select_return_states(const char *cols, struct expression *call,
541 541 int (*callback)(void*, int, char**, char**), void *info)
542 542 {
543 543 struct expression *fn;
544 544 int row_count = 0;
545 545
546 546 if (is_fake_call(call))
547 547 return;
548 548
549 549 fn = strip_expr(call->fn);
550 550 if (fn->type != EXPR_SYMBOL || !fn->symbol || is_local_symbol(fn)) {
551 551 sql_select_return_states_pointer(cols, call, callback, info);
552 552 return;
553 553 }
554 554
555 555 if (inlinable(fn)) {
556 556 mem_sql(callback, info,
557 557 "select %s from return_states where call_id = '%lu' order by return_id, type;",
558 558 cols, (unsigned long)call);
559 559 return;
560 560 }
561 561
562 562 run_sql(get_row_count, &row_count, "select count(*) from return_states where %s;",
563 563 get_static_filter(fn->symbol));
564 564 if (row_count > 3000)
565 565 return;
566 566
567 567 run_sql(callback, info, "select %s from return_states where %s order by file, return_id, type;",
568 568 cols, get_static_filter(fn->symbol));
569 569 }
570 570
571 571 #define CALL_IMPLIES 0
572 572 #define RETURN_IMPLIES 1
573 573
574 574 struct implies_info {
575 575 int type;
576 576 struct db_implies_cb_list *cb_list;
577 577 struct expression *expr;
578 578 struct symbol *sym;
579 579 };
580 580
581 581 void sql_select_implies(const char *cols, struct implies_info *info,
582 582 int (*callback)(void*, int, char**, char**))
583 583 {
584 584 if (info->type == RETURN_IMPLIES && inlinable(info->expr->fn)) {
585 585 mem_sql(callback, info,
586 586 "select %s from return_implies where call_id = '%lu';",
587 587 cols, (unsigned long)info->expr);
588 588 return;
589 589 }
590 590
591 591 run_sql(callback, info, "select %s from %s_implies where %s;",
592 592 cols,
593 593 info->type == CALL_IMPLIES ? "call" : "return",
594 594 get_static_filter(info->sym));
595 595 }
596 596
597 597 struct select_caller_info_data {
598 598 struct stree *final_states;
599 599 struct timeval start_time;
600 600 int prev_func_id;
601 601 int ignore;
602 602 int results;
603 603 };
604 604
605 605 static int caller_info_callback(void *_data, int argc, char **argv, char **azColName);
606 606
607 607 static void sql_select_caller_info(struct select_caller_info_data *data,
608 608 const char *cols, struct symbol *sym)
609 609 {
610 610 if (__inline_fn) {
611 611 mem_sql(caller_info_callback, data,
612 612 "select %s from caller_info where call_id = %lu;",
613 613 cols, (unsigned long)__inline_fn);
614 614 return;
615 615 }
616 616
617 617 if (sym->ident->name && is_common_function(sym->ident->name))
618 618 return;
619 619 run_sql(caller_info_callback, data,
620 620 "select %s from common_caller_info where %s order by call_id;",
621 621 cols, get_static_filter(sym));
622 622 if (data->results)
623 623 return;
624 624
625 625 run_sql(caller_info_callback, data,
626 626 "select %s from caller_info where %s order by call_id;",
627 627 cols, get_static_filter(sym));
628 628 }
629 629
630 630 void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
631 631 {
632 632 struct def_callback *def_callback = __alloc_def_callback(0);
633 633
634 634 def_callback->hook_type = type;
635 635 def_callback->callback = callback;
636 636 add_ptr_list(&select_caller_info_callbacks, def_callback);
637 637 }
638 638
639 639 /*
640 640 * These call backs are used when the --info option is turned on to print struct
641 641 * member information. For example foo->bar could have a state in
642 642 * smatch_extra.c and also check_user.c.
643 643 */
644 644 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
645 645 {
646 646 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
647 647
648 648 member_callback->owner = owner;
649 649 member_callback->callback = callback;
650 650 add_ptr_list(&member_callbacks, member_callback);
651 651 }
652 652
653 653 void add_caller_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
654 654 {
655 655 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
656 656
657 657 member_callback->owner = owner;
658 658 member_callback->callback = callback;
659 659 add_ptr_list(&member_callbacks_new, member_callback);
660 660 }
661 661
662 662 void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr))
663 663 {
664 664 struct returned_state_callback *callback = __alloc_returned_state_callback(0);
665 665
666 666 callback->callback = fn;
667 667 add_ptr_list(&returned_state_callbacks, callback);
668 668 }
669 669
670 670 void add_returned_member_callback(int owner, void (*callback)(int return_id, char *return_ranges, struct expression *expr, char *printed_name, struct smatch_state *state))
671 671 {
672 672 struct returned_member_callback *member_callback = __alloc_returned_member_callback(0);
673 673
674 674 member_callback->owner = owner;
675 675 member_callback->callback = callback;
676 676 add_ptr_list(&returned_member_callbacks, member_callback);
677 677 }
678 678
679 679 void select_call_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value))
680 680 {
681 681 struct db_implies_callback *cb = __alloc_db_implies_callback(0);
682 682
683 683 cb->type = type;
684 684 cb->callback = callback;
685 685 add_ptr_list(&call_implies_cb_list, cb);
686 686 }
687 687
688 688 void select_return_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value))
689 689 {
690 690 struct db_implies_callback *cb = __alloc_db_implies_callback(0);
691 691
692 692 cb->type = type;
693 693 cb->callback = callback;
694 694 add_ptr_list(&return_implies_cb_list, cb);
695 695 }
696 696
697 697 struct return_info {
698 698 struct expression *static_returns_call;
699 699 struct symbol *return_type;
700 700 struct range_list *return_range_list;
701 701 };
702 702
703 703 static int db_return_callback(void *_ret_info, int argc, char **argv, char **azColName)
704 704 {
705 705 struct return_info *ret_info = _ret_info;
706 706 struct range_list *rl;
707 707 struct expression *call_expr = ret_info->static_returns_call;
708 708
709 709 if (argc != 1)
710 710 return 0;
711 711 call_results_to_rl(call_expr, ret_info->return_type, argv[0], &rl);
712 712 ret_info->return_range_list = rl_union(ret_info->return_range_list, rl);
713 713 return 0;
714 714 }
715 715
716 716 struct range_list *db_return_vals(struct expression *expr)
717 717 {
718 718 struct return_info ret_info = {};
719 719 char buf[64];
720 720 struct sm_state *sm;
721 721
722 722 if (is_fake_call(expr))
723 723 return NULL;
724 724
725 725 snprintf(buf, sizeof(buf), "return %p", expr);
726 726 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
727 727 if (sm)
728 728 return clone_rl(estate_rl(sm->state));
729 729 ret_info.static_returns_call = expr;
730 730 ret_info.return_type = get_type(expr);
731 731 if (!ret_info.return_type)
732 732 return NULL;
733 733
734 734 if (expr->fn->type != EXPR_SYMBOL || !expr->fn->symbol)
735 735 return NULL;
736 736
737 737 ret_info.return_range_list = NULL;
738 738 if (inlinable(expr->fn)) {
739 739 mem_sql(db_return_callback, &ret_info,
740 740 "select distinct return from return_states where call_id = '%lu';",
741 741 (unsigned long)expr);
742 742 } else {
743 743 run_sql(db_return_callback, &ret_info,
744 744 "select distinct return from return_states where %s;",
745 745 get_static_filter(expr->fn->symbol));
746 746 }
747 747 return ret_info.return_range_list;
748 748 }
749 749
750 750 struct range_list *db_return_vals_from_str(const char *fn_name)
751 751 {
752 752 struct return_info ret_info;
753 753
754 754 ret_info.static_returns_call = NULL;
755 755 ret_info.return_type = &llong_ctype;
756 756 ret_info.return_range_list = NULL;
757 757
758 758 run_sql(db_return_callback, &ret_info,
759 759 "select distinct return from return_states where function = '%s';",
760 760 fn_name);
761 761 return ret_info.return_range_list;
762 762 }
763 763
764 764 /*
765 765 * This is used when we have a function that takes a function pointer as a
766 766 * parameter. "frob(blah, blah, my_function);" We know that the return values
767 767 * from frob() come from my_funcion() so we want to find the possible returns
768 768 * of my_function(), but we don't know which arguments are passed to it.
769 769 *
770 770 */
771 771 struct range_list *db_return_vals_no_args(struct expression *expr)
772 772 {
773 773 struct return_info ret_info = {};
774 774
775 775 if (!expr || expr->type != EXPR_SYMBOL)
776 776 return NULL;
777 777
778 778 ret_info.static_returns_call = expr;
779 779 ret_info.return_type = get_type(expr);
780 780 ret_info.return_type = get_real_base_type(ret_info.return_type);
781 781 if (!ret_info.return_type)
782 782 return NULL;
783 783
784 784 run_sql(db_return_callback, &ret_info,
785 785 "select distinct return from return_states where %s;",
786 786 get_static_filter(expr->symbol));
787 787
788 788 return ret_info.return_range_list;
789 789 }
790 790
791 791 static void match_call_marker(struct expression *expr)
792 792 {
793 793 struct symbol *type;
794 794
795 795 type = get_type(expr->fn);
796 796 if (type && type->type == SYM_PTR)
797 797 type = get_real_base_type(type);
798 798
799 799 /*
800 800 * we just want to record something in the database so that if we have
801 801 * two calls like: frob(4); frob(some_unkown); then on the receiving
802 802 * side we know that sometimes frob is called with unknown parameters.
803 803 */
804 804
805 805 sql_insert_caller_info(expr, INTERNAL, -1, "%call_marker%", type_to_str(type));
806 806 }
807 807
808 808 int is_recursive_member(const char *name)
809 809 {
810 810 char buf[256];
811 811 const char *p, *next;
812 812 int size;
813 813
814 814 p = strchr(name, '>');
815 815 if (!p)
816 816 return 0;
817 817 p++;
818 818 while (true) {
819 819 next = strchr(p, '>');
820 820 if (!next)
821 821 return 0;
822 822 next++;
823 823
824 824 size = next - p;
825 825 if (size >= sizeof(buf))
826 826 return 0;
827 827 memcpy(buf, p, size);
828 828 buf[size] = '\0';
829 829 if (strstr(next, buf))
830 830 return 1;
831 831 p = next;
832 832 }
833 833 }
834 834
835 835 char *sm_to_arg_name(struct expression *expr, struct sm_state *sm)
836 836 {
837 837 struct symbol *sym;
838 838 const char *sm_name;
839 839 char *name;
840 840 bool is_address = false;
841 841 bool add_star = false;
842 842 char buf[256];
843 843 char *ret = NULL;
844 844 int len;
845 845
846 846 expr = strip_expr(expr);
847 847 if (!expr)
848 848 return NULL;
849 849
850 850 if (expr->type == EXPR_PREOP && expr->op == '&') {
851 851 expr = strip_expr(expr->unop);
852 852 is_address = true;
853 853 }
854 854
855 855 name = expr_to_var_sym(expr, &sym);
856 856 if (!name || !sym)
857 857 goto free;
858 858 if (sym != sm->sym)
859 859 goto free;
860 860
861 861 sm_name = sm->name;
862 862 add_star = false;
863 863 if (sm_name[0] == '*') {
864 864 add_star = true;
865 865 sm_name++;
866 866 }
867 867
868 868 len = strlen(name);
869 869 if (strncmp(name, sm_name, len) != 0)
870 870 goto free;
871 871 if (sm_name[len] == '\0') {
872 872 snprintf(buf, sizeof(buf), "%s%s$",
873 873 add_star ? "*" : "", is_address ? "*" : "");
874 874 } else {
875 875 if (sm_name[len] != '.' && sm_name[len] != '-')
876 876 goto free;
877 877 if (sm_name[len] == '-')
878 878 len++;
879 879 // FIXME does is_address really imply that sm_name[len] == '-'
880 880 snprintf(buf, sizeof(buf), "%s$->%s", add_star ? "*" : "",
881 881 sm_name + len);
882 882 }
883 883
884 884 ret = alloc_sname(buf);
885 885 free:
886 886 free_string(name);
887 887 return ret;
888 888 }
889 889
890 890 static void print_struct_members(struct expression *call, struct expression *expr, int param, struct stree *stree,
891 891 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm),
892 892 bool new)
893 893 {
894 894 struct sm_state *sm;
895 895 const char *sm_name;
896 896 char *name;
897 897 struct symbol *sym;
898 898 int len;
899 899 char printed_name[256];
900 900 int is_address = 0;
901 901 bool add_star;
902 902 struct symbol *type;
903 903
904 904 expr = strip_expr(expr);
905 905 if (!expr)
906 906 return;
907 907 type = get_type(expr);
908 908 if (type && type_bits(type) < type_bits(&ulong_ctype))
909 909 return;
910 910
911 911 if (expr->type == EXPR_PREOP && expr->op == '&') {
912 912 expr = strip_expr(expr->unop);
913 913 is_address = 1;
914 914 }
915 915
916 916 name = expr_to_var_sym(expr, &sym);
917 917 if (!name || !sym)
918 918 goto free;
919 919
920 920 len = strlen(name);
921 921 FOR_EACH_SM(stree, sm) {
922 922 if (sm->sym != sym)
923 923 continue;
924 924 sm_name = sm->name;
925 925 add_star = false;
926 926 if (sm_name[0] == '*') {
927 927 add_star = true;
928 928 sm_name++;
929 929 }
930 930 // FIXME: simplify?
931 931 if (!add_star && strcmp(name, sm_name) == 0) {
932 932 if (is_address) {
933 933 snprintf(printed_name, sizeof(printed_name), "*$");
934 934 } else {
935 935 if (new)
936 936 snprintf(printed_name, sizeof(printed_name), "$");
937 937 else
938 938 continue;
939 939 }
940 940 } else if (add_star && strcmp(name, sm_name) == 0) {
941 941 snprintf(printed_name, sizeof(printed_name), "%s*$",
942 942 is_address ? "*" : "");
943 943 } else if (strncmp(name, sm_name, len) == 0) {
944 944 if (sm_name[len] != '.' && sm_name[len] != '-')
945 945 continue;
946 946 if (is_address && sm_name[len] == '.') {
947 947 snprintf(printed_name, sizeof(printed_name),
948 948 "%s$->%s", add_star ? "*" : "",
949 949 sm_name + len + 1);
950 950 } else if (is_address && sm_name[len] == '-') {
951 951 snprintf(printed_name, sizeof(printed_name),
952 952 "%s(*$)%s", add_star ? "*" : "",
953 953 sm_name + len);
954 954 } else {
955 955 snprintf(printed_name, sizeof(printed_name),
956 956 "%s$%s", add_star ? "*" : "",
957 957 sm_name + len);
958 958 }
959 959 } else {
960 960 continue;
961 961 }
962 962 if (is_recursive_member(printed_name))
963 963 continue;
964 964 callback(call, param, printed_name, sm);
965 965 } END_FOR_EACH_SM(sm);
966 966 free:
967 967 free_string(name);
968 968 }
969 969
970 970 static void match_call_info(struct expression *call)
971 971 {
972 972 struct member_info_callback *cb;
973 973 struct expression *arg;
974 974 struct stree *stree;
975 975 char *name;
976 976 int i;
977 977
978 978 name = get_fnptr_name(call->fn);
979 979 if (!name)
980 980 return;
981 981
982 982 FOR_EACH_PTR(member_callbacks, cb) {
983 983 stree = get_all_states_stree(cb->owner);
984 984 i = 0;
985 985 FOR_EACH_PTR(call->args, arg) {
986 986 print_struct_members(call, arg, i, stree, cb->callback, 0);
987 987 i++;
988 988 } END_FOR_EACH_PTR(arg);
989 989 free_stree(&stree);
990 990 } END_FOR_EACH_PTR(cb);
991 991
992 992 free_string(name);
993 993 }
994 994
995 995 static void match_call_info_new(struct expression *call)
996 996 {
997 997 struct member_info_callback *cb;
998 998 struct expression *arg;
999 999 struct stree *stree;
1000 1000 char *name;
1001 1001 int i;
1002 1002
1003 1003 name = get_fnptr_name(call->fn);
1004 1004 if (!name)
1005 1005 return;
1006 1006
1007 1007 FOR_EACH_PTR(member_callbacks_new, cb) {
1008 1008 stree = get_all_states_stree(cb->owner);
1009 1009 i = 0;
1010 1010 FOR_EACH_PTR(call->args, arg) {
1011 1011 print_struct_members(call, arg, i, stree, cb->callback, 1);
1012 1012 i++;
1013 1013 } END_FOR_EACH_PTR(arg);
1014 1014 free_stree(&stree);
1015 1015 } END_FOR_EACH_PTR(cb);
1016 1016
1017 1017 free_string(name);
1018 1018 }
1019 1019
1020 1020 static int get_param(int param, char **name, struct symbol **sym)
1021 1021 {
1022 1022 struct symbol *arg;
1023 1023 int i;
1024 1024
1025 1025 i = 0;
1026 1026 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
1027 1027 /*
1028 1028 * this is a temporary hack to work around a bug (I think in sparse?)
1029 1029 * 2.6.37-rc1:fs/reiserfs/journal.o
1030 1030 * If there is a function definition without parameter name found
1031 1031 * after a function implementation then it causes a crash.
1032 1032 * int foo() {}
1033 1033 * int bar(char *);
1034 1034 */
1035 1035 if (arg->ident->name < (char *)100)
1036 1036 continue;
1037 1037 if (i == param) {
1038 1038 *name = arg->ident->name;
1039 1039 *sym = arg;
1040 1040 return TRUE;
1041 1041 }
1042 1042 i++;
1043 1043 } END_FOR_EACH_PTR(arg);
1044 1044
1045 1045 return FALSE;
1046 1046 }
1047 1047
1048 1048 static int function_signature_matches(const char *sig)
1049 1049 {
1050 1050 char *my_sig;
1051 1051
1052 1052 my_sig = function_signature();
1053 1053 if (!sig || !my_sig)
1054 1054 return 1; /* default to matching */
1055 1055 if (strcmp(my_sig, sig) == 0)
1056 1056 return 1;
1057 1057 return 0;
1058 1058 }
1059 1059
1060 1060 static int caller_info_callback(void *_data, int argc, char **argv, char **azColName)
1061 1061 {
1062 1062 struct select_caller_info_data *data = _data;
1063 1063 int func_id;
1064 1064 long type;
1065 1065 long param;
1066 1066 char *key;
1067 1067 char *value;
1068 1068 char *name = NULL;
1069 1069 struct symbol *sym = NULL;
1070 1070 struct def_callback *def_callback;
1071 1071 struct stree *stree;
1072 1072 struct timeval cur_time;
1073 1073
1074 1074 data->results = 1;
1075 1075
1076 1076 if (argc != 5)
1077 1077 return 0;
1078 1078
1079 1079 gettimeofday(&cur_time, NULL);
1080 1080 if (cur_time.tv_sec - data->start_time.tv_sec > 10)
1081 1081 return 0;
1082 1082
1083 1083 func_id = atoi(argv[0]);
1084 1084 errno = 0;
1085 1085 type = strtol(argv[1], NULL, 10);
1086 1086 param = strtol(argv[2], NULL, 10);
1087 1087 if (errno)
1088 1088 return 0;
1089 1089 key = argv[3];
1090 1090 value = argv[4];
1091 1091
1092 1092 if (data->prev_func_id == -1)
1093 1093 data->prev_func_id = func_id;
1094 1094 if (func_id != data->prev_func_id) {
1095 1095 stree = __pop_fake_cur_stree();
1096 1096 if (!data->ignore)
1097 1097 merge_stree(&data->final_states, stree);
1098 1098 free_stree(&stree);
1099 1099 __push_fake_cur_stree();
1100 1100 __unnullify_path();
1101 1101 data->prev_func_id = func_id;
1102 1102 data->ignore = 0;
1103 1103 }
1104 1104
1105 1105 if (data->ignore)
1106 1106 return 0;
1107 1107 if (type == INTERNAL &&
1108 1108 !function_signature_matches(value)) {
1109 1109 data->ignore = 1;
1110 1110 return 0;
1111 1111 }
1112 1112
1113 1113 if (param >= 0 && !get_param(param, &name, &sym))
1114 1114 return 0;
1115 1115
1116 1116 FOR_EACH_PTR(select_caller_info_callbacks, def_callback) {
1117 1117 if (def_callback->hook_type == type)
1118 1118 def_callback->callback(name, sym, key, value);
1119 1119 } END_FOR_EACH_PTR(def_callback);
1120 1120
1121 1121 return 0;
1122 1122 }
1123 1123
1124 1124 static struct string_list *ptr_names_done;
1125 1125 static struct string_list *ptr_names;
1126 1126
1127 1127 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
1128 1128 {
1129 1129 insert_string(&ptr_names, alloc_string(argv[0]));
1130 1130 return 0;
1131 1131 }
1132 1132
1133 1133 static char *get_next_ptr_name(void)
1134 1134 {
1135 1135 char *ptr;
1136 1136
1137 1137 FOR_EACH_PTR(ptr_names, ptr) {
1138 1138 if (!insert_string(&ptr_names_done, ptr))
1139 1139 continue;
1140 1140 return ptr;
1141 1141 } END_FOR_EACH_PTR(ptr);
1142 1142 return NULL;
1143 1143 }
1144 1144
1145 1145 static void get_ptr_names(const char *file, const char *name)
1146 1146 {
1147 1147 char sql_filter[1024];
1148 1148 int before, after;
1149 1149
1150 1150 if (file) {
1151 1151 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
1152 1152 file, name);
1153 1153 } else {
1154 1154 snprintf(sql_filter, 1024, "function = '%s';", name);
1155 1155 }
1156 1156
1157 1157 before = ptr_list_size((struct ptr_list *)ptr_names);
1158 1158
1159 1159 run_sql(get_ptr_name, NULL,
1160 1160 "select distinct ptr from function_ptr where %s",
1161 1161 sql_filter);
1162 1162
1163 1163 after = ptr_list_size((struct ptr_list *)ptr_names);
1164 1164 if (before == after)
1165 1165 return;
1166 1166
1167 1167 while ((name = get_next_ptr_name()))
1168 1168 get_ptr_names(NULL, name);
1169 1169 }
1170 1170
1171 1171 static void match_data_from_db(struct symbol *sym)
1172 1172 {
1173 1173 struct select_caller_info_data data = { .prev_func_id = -1 };
1174 1174 struct sm_state *sm;
1175 1175 struct stree *stree;
1176 1176 struct timeval end_time;
1177 1177
1178 1178 if (!sym || !sym->ident)
1179 1179 return;
1180 1180
1181 1181 set_fn_mtag(sym);
1182 1182 gettimeofday(&data.start_time, NULL);
1183 1183
1184 1184 __push_fake_cur_stree();
1185 1185 __unnullify_path();
1186 1186
1187 1187 if (!__inline_fn) {
1188 1188 char *ptr;
1189 1189
1190 1190 if (sym->ctype.modifiers & MOD_STATIC)
1191 1191 get_ptr_names(get_base_file(), sym->ident->name);
1192 1192 else
1193 1193 get_ptr_names(NULL, sym->ident->name);
1194 1194
1195 1195 if (ptr_list_size((struct ptr_list *)ptr_names) > 20) {
1196 1196 __free_ptr_list((struct ptr_list **)&ptr_names);
1197 1197 __free_ptr_list((struct ptr_list **)&ptr_names_done);
1198 1198 __free_fake_cur_stree();
1199 1199 return;
1200 1200 }
1201 1201
1202 1202 sql_select_caller_info(&data,
1203 1203 "call_id, type, parameter, key, value",
1204 1204 sym);
1205 1205
1206 1206
1207 1207 stree = __pop_fake_cur_stree();
1208 1208 if (!data.ignore)
1209 1209 merge_stree(&data.final_states, stree);
1210 1210 free_stree(&stree);
1211 1211 __push_fake_cur_stree();
1212 1212 __unnullify_path();
1213 1213 data.prev_func_id = -1;
1214 1214 data.ignore = 0;
1215 1215 data.results = 0;
1216 1216
1217 1217 FOR_EACH_PTR(ptr_names, ptr) {
1218 1218 run_sql(caller_info_callback, &data,
1219 1219 "select call_id, type, parameter, key, value"
1220 1220 " from common_caller_info where function = '%s' order by call_id",
1221 1221 ptr);
1222 1222 } END_FOR_EACH_PTR(ptr);
1223 1223
1224 1224 if (data.results) {
1225 1225 FOR_EACH_PTR(ptr_names, ptr) {
1226 1226 free_string(ptr);
1227 1227 } END_FOR_EACH_PTR(ptr);
1228 1228 goto free_ptr_names;
1229 1229 }
1230 1230
1231 1231 FOR_EACH_PTR(ptr_names, ptr) {
1232 1232 run_sql(caller_info_callback, &data,
1233 1233 "select call_id, type, parameter, key, value"
1234 1234 " from caller_info where function = '%s' order by call_id",
1235 1235 ptr);
1236 1236 free_string(ptr);
1237 1237 } END_FOR_EACH_PTR(ptr);
1238 1238
1239 1239 free_ptr_names:
1240 1240 __free_ptr_list((struct ptr_list **)&ptr_names);
1241 1241 __free_ptr_list((struct ptr_list **)&ptr_names_done);
1242 1242 } else {
1243 1243 sql_select_caller_info(&data,
1244 1244 "call_id, type, parameter, key, value",
1245 1245 sym);
1246 1246 }
1247 1247
1248 1248 stree = __pop_fake_cur_stree();
1249 1249 if (!data.ignore)
1250 1250 merge_stree(&data.final_states, stree);
1251 1251 free_stree(&stree);
1252 1252
1253 1253 gettimeofday(&end_time, NULL);
1254 1254 if (end_time.tv_sec - data.start_time.tv_sec <= 10) {
1255 1255 FOR_EACH_SM(data.final_states, sm) {
1256 1256 __set_sm(sm);
1257 1257 } END_FOR_EACH_SM(sm);
1258 1258 }
1259 1259
1260 1260 free_stree(&data.final_states);
1261 1261 }
1262 1262
1263 1263 static int return_implies_callbacks(void *_info, int argc, char **argv, char **azColName)
1264 1264 {
1265 1265 struct implies_info *info = _info;
1266 1266 struct db_implies_callback *cb;
1267 1267 struct expression *arg = NULL;
1268 1268 int type;
1269 1269 int param;
1270 1270
1271 1271 if (argc != 5)
1272 1272 return 0;
1273 1273
1274 1274 type = atoi(argv[1]);
1275 1275 param = atoi(argv[2]);
1276 1276
1277 1277 FOR_EACH_PTR(info->cb_list, cb) {
1278 1278 if (cb->type != type)
1279 1279 continue;
1280 1280 if (param != -1) {
1281 1281 arg = get_argument_from_call_expr(info->expr->args, param);
1282 1282 if (!arg)
1283 1283 continue;
1284 1284 }
1285 1285 cb->callback(info->expr, arg, argv[3], argv[4]);
1286 1286 } END_FOR_EACH_PTR(cb);
1287 1287
1288 1288 return 0;
1289 1289 }
1290 1290
1291 1291 static int call_implies_callbacks(void *_info, int argc, char **argv, char **azColName)
1292 1292 {
1293 1293 struct implies_info *info = _info;
1294 1294 struct db_implies_callback *cb;
1295 1295 struct expression *arg;
1296 1296 struct symbol *sym;
1297 1297 char *name;
1298 1298 int type;
1299 1299 int param;
1300 1300
1301 1301 if (argc != 5)
1302 1302 return 0;
1303 1303
1304 1304 type = atoi(argv[1]);
1305 1305 param = atoi(argv[2]);
1306 1306
1307 1307 if (!get_param(param, &name, &sym))
1308 1308 return 0;
1309 1309 arg = symbol_expression(sym);
1310 1310 if (!arg)
1311 1311 return 0;
1312 1312
1313 1313 FOR_EACH_PTR(info->cb_list, cb) {
1314 1314 if (cb->type != type)
1315 1315 continue;
1316 1316 cb->callback(info->expr, arg, argv[3], argv[4]);
1317 1317 } END_FOR_EACH_PTR(cb);
1318 1318
1319 1319 return 0;
1320 1320 }
1321 1321
1322 1322 static void match_return_implies(struct expression *expr)
1323 1323 {
1324 1324 struct implies_info info = {
1325 1325 .type = RETURN_IMPLIES,
1326 1326 .cb_list = return_implies_cb_list,
1327 1327 };
1328 1328
1329 1329 if (expr->fn->type != EXPR_SYMBOL ||
1330 1330 !expr->fn->symbol)
1331 1331 return;
1332 1332 info.expr = expr;
1333 1333 info.sym = expr->fn->symbol;
1334 1334 sql_select_implies("function, type, parameter, key, value", &info,
1335 1335 return_implies_callbacks);
1336 1336 }
1337 1337
1338 1338 static void match_call_implies(struct symbol *sym)
1339 1339 {
1340 1340 struct implies_info info = {
1341 1341 .type = CALL_IMPLIES,
1342 1342 .cb_list = call_implies_cb_list,
1343 1343 };
1344 1344
1345 1345 if (!sym || !sym->ident)
1346 1346 return;
1347 1347
1348 1348 info.sym = sym;
1349 1349 sql_select_implies("function, type, parameter, key, value", &info,
1350 1350 call_implies_callbacks);
1351 1351 }
1352 1352
1353 1353 static char *get_fn_param_str(struct expression *expr)
1354 1354 {
1355 1355 struct expression *tmp;
1356 1356 int param;
1357 1357 char buf[32];
1358 1358
1359 1359 tmp = get_assigned_expr(expr);
1360 1360 if (tmp)
1361 1361 expr = tmp;
1362 1362 expr = strip_expr(expr);
1363 1363 if (!expr || expr->type != EXPR_CALL)
1364 1364 return NULL;
1365 1365 expr = strip_expr(expr->fn);
1366 1366 if (!expr || expr->type != EXPR_SYMBOL)
1367 1367 return NULL;
1368 1368 param = get_param_num(expr);
1369 1369 if (param < 0)
1370 1370 return NULL;
1371 1371
1372 1372 snprintf(buf, sizeof(buf), "[r $%d]", param);
1373 1373 return alloc_sname(buf);
1374 1374 }
1375 1375
1376 1376 static char *get_return_compare_is_param(struct expression *expr)
1377 1377 {
1378 1378 char *var;
1379 1379 char buf[256];
1380 1380 int comparison;
1381 1381 int param;
1382 1382
1383 1383 param = get_param_num(expr);
1384 1384 if (param < 0)
1385 1385 return NULL;
1386 1386
1387 1387 var = expr_to_var(expr);
1388 1388 if (!var)
1389 1389 return NULL;
1390 1390 snprintf(buf, sizeof(buf), "%s orig", var);
1391 1391 comparison = get_comparison_strings(var, buf);
1392 1392 free_string(var);
1393 1393
1394 1394 if (!comparison)
1395 1395 return NULL;
1396 1396
1397 1397 snprintf(buf, sizeof(buf), "[%s$%d]", show_special(comparison), param);
1398 1398 return alloc_sname(buf);
1399 1399 }
1400 1400
1401 1401 static char *get_return_compare_str(struct expression *expr)
1402 1402 {
1403 1403 char *compare_str;
1404 1404
1405 1405 compare_str = get_return_compare_is_param(expr);
1406 1406 if (compare_str)
1407 1407 return compare_str;
1408 1408
1409 1409 compare_str = expr_lte_to_param(expr, -1);
1410 1410 if (compare_str)
1411 1411 return compare_str;
1412 1412
1413 1413 return expr_param_comparison(expr, -1);
1414 1414 }
1415 1415
1416 1416 static const char *get_return_ranges_str(struct expression *expr, struct range_list **rl_p)
1417 1417 {
1418 1418 struct range_list *rl;
1419 1419 char *return_ranges;
1420 1420 sval_t sval;
1421 1421 char *fn_param_str;
1422 1422 char *compare_str;
1423 1423 char *math_str;
1424 1424 char buf[128];
1425 1425
1426 1426 *rl_p = NULL;
1427 1427
1428 1428 if (!expr)
1429 1429 return alloc_sname("");
1430 1430
1431 1431 if (get_implied_value(expr, &sval)) {
1432 1432 sval = sval_cast(cur_func_return_type(), sval);
1433 1433 *rl_p = alloc_rl(sval, sval);
1434 1434 return sval_to_str_or_err_ptr(sval);
1435 1435 }
1436 1436
1437 1437 fn_param_str = get_fn_param_str(expr);
1438 1438 compare_str = expr_equal_to_param(expr, -1);
1439 1439 math_str = get_value_in_terms_of_parameter_math(expr);
1440 1440
1441 1441 if (get_implied_rl(expr, &rl) && !is_whole_rl(rl)) {
1442 1442 rl = cast_rl(cur_func_return_type(), rl);
1443 1443 return_ranges = show_rl(rl);
1444 1444 } else if (get_imaginary_absolute(expr, &rl)){
1445 1445 rl = cast_rl(cur_func_return_type(), rl);
1446 1446 return alloc_sname(show_rl(rl));
1447 1447 } else {
1448 1448 get_absolute_rl(expr, &rl);
1449 1449 rl = cast_rl(cur_func_return_type(), rl);
1450 1450 return_ranges = show_rl(rl);
1451 1451 }
1452 1452 *rl_p = rl;
1453 1453
1454 1454 if (fn_param_str) {
1455 1455 snprintf(buf, sizeof(buf), "%s%s", return_ranges, fn_param_str);
1456 1456 return alloc_sname(buf);
1457 1457 }
1458 1458 if (compare_str) {
1459 1459 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1460 1460 return alloc_sname(buf);
1461 1461 }
1462 1462 if (math_str) {
1463 1463 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, math_str);
1464 1464 return alloc_sname(buf);
1465 1465 }
1466 1466 compare_str = get_return_compare_str(expr);
1467 1467 if (compare_str) {
1468 1468 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1469 1469 return alloc_sname(buf);
1470 1470 }
1471 1471
1472 1472 return return_ranges;
1473 1473 }
1474 1474
1475 1475 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
1476 1476 {
1477 1477 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", function_signature());
1478 1478 }
1479 1479
1480 1480 static bool call_return_state_hooks_conditional(struct expression *expr)
1481 1481 {
1482 1482 int final_pass_orig = final_pass;
1483 1483 static int recurse;
1484 1484
1485 1485 if (recurse >= 2)
1486 1486 return false;
1487 1487 if (!expr ||
1488 1488 (expr->type != EXPR_CONDITIONAL && expr->type != EXPR_SELECT))
1489 1489 return false;
1490 1490
1491 1491 recurse++;
1492 1492
1493 1493 __push_fake_cur_stree();
1494 1494
1495 1495 final_pass = 0;
1496 1496 __split_whole_condition(expr->conditional);
1497 1497 final_pass = final_pass_orig;
1498 1498
1499 1499 call_return_state_hooks(expr->cond_true ?: expr->conditional);
1500 1500
1501 1501 __push_true_states();
1502 1502 __use_false_states();
1503 1503
1504 1504 call_return_state_hooks(expr->cond_false);
1505 1505
1506 1506 __merge_true_states();
1507 1507 __free_fake_cur_stree();
1508 1508
1509 1509 recurse--;
1510 1510 return true;
1511 1511 }
1512 1512
1513 1513 static void call_return_state_hooks_compare(struct expression *expr)
1514 1514 {
1515 1515 struct returned_state_callback *cb;
1516 1516 char *return_ranges;
1517 1517 int final_pass_orig = final_pass;
1518 1518 sval_t sval = { .type = &int_ctype };
1519 1519 sval_t ret;
1520 1520
1521 1521 if (!get_implied_value(expr, &ret))
1522 1522 ret.value = -1;
1523 1523
1524 1524 __push_fake_cur_stree();
1525 1525
1526 1526 final_pass = 0;
1527 1527 __split_whole_condition(expr);
1528 1528 final_pass = final_pass_orig;
1529 1529
1530 1530 if (ret.value != 0) {
1531 1531 return_ranges = alloc_sname("1");
1532 1532 sval.value = 1;
1533 1533 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1534 1534
1535 1535 return_id++;
1536 1536 FOR_EACH_PTR(returned_state_callbacks, cb) {
1537 1537 cb->callback(return_id, return_ranges, expr);
1538 1538 } END_FOR_EACH_PTR(cb);
1539 1539 }
1540 1540
1541 1541 __push_true_states();
1542 1542 __use_false_states();
1543 1543
1544 1544 if (ret.value != 1) {
1545 1545 return_ranges = alloc_sname("0");
1546 1546 sval.value = 0;
1547 1547 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1548 1548
1549 1549 return_id++;
1550 1550 FOR_EACH_PTR(returned_state_callbacks, cb) {
1551 1551 cb->callback(return_id, return_ranges, expr);
1552 1552 } END_FOR_EACH_PTR(cb);
1553 1553 }
1554 1554
1555 1555 __merge_true_states();
1556 1556 __free_fake_cur_stree();
1557 1557 }
1558 1558
1559 1559 static int ptr_in_list(struct sm_state *sm, struct state_list *slist)
1560 1560 {
1561 1561 struct sm_state *tmp;
1562 1562
1563 1563 FOR_EACH_PTR(slist, tmp) {
1564 1564 if (strcmp(tmp->state->name, sm->state->name) == 0)
1565 1565 return 1;
1566 1566 } END_FOR_EACH_PTR(tmp);
1567 1567
1568 1568 return 0;
1569 1569 }
1570 1570
1571 1571 static int split_possible_helper(struct sm_state *sm, struct expression *expr)
1572 1572 {
1573 1573 struct returned_state_callback *cb;
1574 1574 struct range_list *rl;
1575 1575 char *return_ranges;
1576 1576 struct sm_state *tmp;
1577 1577 int ret = 0;
1578 1578 int nr_possible, nr_states;
1579 1579 char *compare_str;
1580 1580 char buf[128];
1581 1581 struct state_list *already_handled = NULL;
1582 1582 sval_t sval;
1583 1583
1584 1584 if (!sm || !sm->merged)
1585 1585 return 0;
1586 1586
1587 1587 if (too_many_possible(sm))
1588 1588 return 0;
1589 1589
1590 1590 /* bail if it gets too complicated */
1591 1591 nr_possible = 0;
1592 1592 FOR_EACH_PTR(sm->possible, tmp) {
1593 1593 if (tmp->merged)
1594 1594 continue;
1595 1595 if (ptr_in_list(tmp, already_handled))
1596 1596 continue;
1597 1597 add_ptr_list(&already_handled, tmp);
1598 1598 nr_possible++;
1599 1599 } END_FOR_EACH_PTR(tmp);
1600 1600 free_slist(&already_handled);
1601 1601 nr_states = get_db_state_count();
1602 1602 if (nr_states * nr_possible >= 2000)
1603 1603 return 0;
1604 1604
1605 1605 FOR_EACH_PTR(sm->possible, tmp) {
1606 1606 if (tmp->merged)
1607 1607 continue;
1608 1608 if (ptr_in_list(tmp, already_handled))
1609 1609 continue;
1610 1610 add_ptr_list(&already_handled, tmp);
1611 1611
1612 1612 ret = 1;
1613 1613 __push_fake_cur_stree();
1614 1614
1615 1615 overwrite_states_using_pool(sm, tmp);
1616 1616
1617 1617 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
1618 1618 return_ranges = show_rl(rl);
1619 1619 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(clone_rl(rl)));
1620 1620 if (!rl_to_sval(rl, &sval)) {
1621 1621 compare_str = get_return_compare_str(expr);
1622 1622 if (compare_str) {
1623 1623 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1624 1624 return_ranges = alloc_sname(buf);
1625 1625 }
1626 1626 }
1627 1627
1628 1628 return_id++;
1629 1629 FOR_EACH_PTR(returned_state_callbacks, cb) {
1630 1630 cb->callback(return_id, return_ranges, expr);
1631 1631 } END_FOR_EACH_PTR(cb);
1632 1632
1633 1633 __free_fake_cur_stree();
1634 1634 } END_FOR_EACH_PTR(tmp);
1635 1635
1636 1636 free_slist(&already_handled);
1637 1637
1638 1638 return ret;
1639 1639 }
1640 1640
1641 1641 static int call_return_state_hooks_split_possible(struct expression *expr)
1642 1642 {
1643 1643 struct sm_state *sm;
1644 1644
1645 1645 if (!expr)
1646 1646 return 0;
1647 1647
1648 1648 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1649 1649 return split_possible_helper(sm, expr);
1650 1650 }
1651 1651
1652 1652 static bool has_possible_negative(struct sm_state *sm)
1653 1653 {
1654 1654 struct sm_state *tmp;
1655 1655
1656 1656 if (!type_signed(estate_type(sm->state)))
1657 1657 return false;
1658 1658
1659 1659 FOR_EACH_PTR(sm->possible, tmp) {
1660 1660 if (!estate_rl(tmp->state))
1661 1661 continue;
1662 1662 if (sval_is_negative(estate_min(tmp->state)) &&
1663 1663 sval_is_negative(estate_max(tmp->state)))
1664 1664 return true;
1665 1665 } END_FOR_EACH_PTR(tmp);
1666 1666
1667 1667 return false;
1668 1668 }
1669 1669
1670 1670 static bool has_separate_zero_null(struct sm_state *sm)
1671 1671 {
1672 1672 struct sm_state *tmp;
1673 1673 sval_t sval;
1674 1674
1675 1675 FOR_EACH_PTR(sm->possible, tmp) {
1676 1676 if (!estate_get_single_value(tmp->state, &sval))
1677 1677 continue;
1678 1678 if (sval.value == 0)
1679 1679 return true;
1680 1680 } END_FOR_EACH_PTR(tmp);
1681 1681
1682 1682 return false;
1683 1683 }
1684 1684
1685 1685 static int split_positive_from_negative(struct expression *expr)
1686 1686 {
1687 1687 struct sm_state *sm;
1688 1688 struct returned_state_callback *cb;
1689 1689 struct range_list *rl;
1690 1690 const char *return_ranges;
1691 1691 struct range_list *ret_rl;
1692 1692 bool separate_zero;
1693 1693 int undo;
1694 1694
1695 1695 /* We're going to print the states 3 times */
1696 1696 if (get_db_state_count() > 10000 / 3)
1697 1697 return 0;
1698 1698
1699 1699 if (!get_implied_rl(expr, &rl) || !rl)
1700 1700 return 0;
1701 1701 /* Forget about INT_MAX and larger */
1702 1702 if (rl_max(rl).value <= 0)
1703 1703 return 0;
1704 1704 if (!sval_is_negative(rl_min(rl)))
1705 1705 return 0;
1706 1706
1707 1707 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1708 1708 if (!sm)
1709 1709 return 0;
1710 1710 if (!has_possible_negative(sm))
1711 1711 return 0;
1712 1712 separate_zero = has_separate_zero_null(sm);
1713 1713
1714 1714 if (!assume(compare_expression(expr, separate_zero ? '>' : SPECIAL_GTE, zero_expr())))
1715 1715 return 0;
1716 1716
1717 1717 return_id++;
1718 1718 return_ranges = get_return_ranges_str(expr, &ret_rl);
1719 1719 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1720 1720 FOR_EACH_PTR(returned_state_callbacks, cb) {
1721 1721 cb->callback(return_id, (char *)return_ranges, expr);
1722 1722 } END_FOR_EACH_PTR(cb);
1723 1723
1724 1724 end_assume();
1725 1725
1726 1726 if (separate_zero) {
1727 1727 undo = assume(compare_expression(expr, SPECIAL_EQUAL, zero_expr()));
1728 1728
1729 1729 return_id++;
1730 1730 return_ranges = get_return_ranges_str(expr, &ret_rl);
1731 1731 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1732 1732 FOR_EACH_PTR(returned_state_callbacks, cb) {
1733 1733 cb->callback(return_id, (char *)return_ranges, expr);
1734 1734 } END_FOR_EACH_PTR(cb);
1735 1735
1736 1736 if (undo)
1737 1737 end_assume();
1738 1738 }
1739 1739
1740 1740 undo = assume(compare_expression(expr, '<', zero_expr()));
1741 1741
1742 1742 return_id++;
1743 1743 return_ranges = get_return_ranges_str(expr, &ret_rl);
1744 1744 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1745 1745 FOR_EACH_PTR(returned_state_callbacks, cb) {
1746 1746 cb->callback(return_id, (char *)return_ranges, expr);
1747 1747 } END_FOR_EACH_PTR(cb);
1748 1748
1749 1749 if (undo)
1750 1750 end_assume();
1751 1751
1752 1752 return 1;
1753 1753 }
1754 1754
1755 1755 static int call_return_state_hooks_split_null_non_null_zero(struct expression *expr)
1756 1756 {
1757 1757 struct returned_state_callback *cb;
1758 1758 struct range_list *rl;
1759 1759 struct range_list *nonnull_rl;
1760 1760 sval_t null_sval;
1761 1761 struct range_list *null_rl = NULL;
1762 1762 char *return_ranges;
1763 1763 struct sm_state *sm;
1764 1764 struct smatch_state *state;
1765 1765 int nr_states;
1766 1766 int final_pass_orig = final_pass;
1767 1767
1768 1768 if (!expr || expr_equal_to_param(expr, -1))
1769 1769 return 0;
1770 1770 if (expr->type == EXPR_CALL)
1771 1771 return 0;
1772 1772
1773 1773 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1774 1774 if (!sm)
1775 1775 return 0;
1776 1776 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
1777 1777 return 0;
1778 1778 state = sm->state;
1779 1779 if (!estate_rl(state))
1780 1780 return 0;
1781 1781 if (estate_min(state).value == 0 && estate_max(state).value == 0)
1782 1782 return 0;
1783 1783 if (has_possible_negative(sm))
1784 1784 return 0;
1785 1785 if (!has_separate_zero_null(sm))
1786 1786 return 0;
1787 1787
1788 1788 nr_states = get_db_state_count();
1789 1789 if (option_info && nr_states >= 1500)
1790 1790 return 0;
1791 1791
1792 1792 rl = estate_rl(state);
1793 1793
1794 1794 __push_fake_cur_stree();
1795 1795
1796 1796 final_pass = 0;
1797 1797 __split_whole_condition(expr);
1798 1798 final_pass = final_pass_orig;
1799 1799
1800 1800 nonnull_rl = rl_filter(rl, rl_zero());
1801 1801 return_ranges = show_rl(nonnull_rl);
1802 1802 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonnull_rl));
1803 1803
1804 1804 return_id++;
1805 1805 FOR_EACH_PTR(returned_state_callbacks, cb) {
1806 1806 cb->callback(return_id, return_ranges, expr);
1807 1807 } END_FOR_EACH_PTR(cb);
1808 1808
1809 1809 __push_true_states();
1810 1810 __use_false_states();
1811 1811
1812 1812 return_ranges = alloc_sname("0");
1813 1813 null_sval = sval_type_val(rl_type(rl), 0);
1814 1814 add_range(&null_rl, null_sval, null_sval);
1815 1815 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(null_rl));
1816 1816 return_id++;
1817 1817 FOR_EACH_PTR(returned_state_callbacks, cb) {
1818 1818 cb->callback(return_id, return_ranges, expr);
1819 1819 } END_FOR_EACH_PTR(cb);
1820 1820
1821 1821 __merge_true_states();
1822 1822 __free_fake_cur_stree();
1823 1823
1824 1824 return 1;
1825 1825 }
1826 1826
1827 1827 static bool is_kernel_success_fail(struct sm_state *sm)
1828 1828 {
1829 1829 struct sm_state *tmp;
1830 1830 struct range_list *rl;
1831 1831 bool has_zero = false;
1832 1832 bool has_neg = false;
1833 1833
1834 1834 if (!type_signed(estate_type(sm->state)))
1835 1835 return false;
1836 1836
1837 1837 FOR_EACH_PTR(sm->possible, tmp) {
1838 1838 rl = estate_rl(tmp->state);
1839 1839 if (!rl)
1840 1840 return false;
1841 1841 if (rl_min(rl).value == 0 && rl_max(rl).value == 0) {
1842 1842 has_zero = true;
1843 1843 continue;
1844 1844 }
1845 1845 has_neg = true;
1846 1846 if (rl_min(rl).value >= -4095 && rl_max(rl).value < 0)
1847 1847 continue;
1848 1848 if (strcmp(tmp->state->name, "s32min-(-1)") == 0)
1849 1849 continue;
1850 1850 if (strcmp(tmp->state->name, "s32min-(-1),1-s32max") == 0)
1851 1851 continue;
1852 1852 return false;
1853 1853 } END_FOR_EACH_PTR(tmp);
1854 1854
1855 1855 return has_zero && has_neg;
1856 1856 }
1857 1857
1858 1858 static int call_return_state_hooks_split_success_fail(struct expression *expr)
1859 1859 {
1860 1860 struct sm_state *sm;
1861 1861 struct range_list *rl;
1862 1862 struct range_list *nonzero_rl;
1863 1863 sval_t zero_sval;
1864 1864 struct range_list *zero_rl = NULL;
1865 1865 int nr_states;
1866 1866 struct returned_state_callback *cb;
1867 1867 char *return_ranges;
1868 1868 int final_pass_orig = final_pass;
1869 1869
1870 1870 if (option_project != PROJ_KERNEL)
1871 1871 return 0;
1872 1872
1873 1873 nr_states = get_db_state_count();
1874 1874 if (nr_states > 2000)
1875 1875 return 0;
1876 1876
1877 1877 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1878 1878 if (!sm)
1879 1879 return 0;
1880 1880 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
1881 1881 return 0;
1882 1882 if (!is_kernel_success_fail(sm))
1883 1883 return 0;
1884 1884
1885 1885 rl = estate_rl(sm->state);
1886 1886 if (!rl)
1887 1887 return 0;
1888 1888
1889 1889 __push_fake_cur_stree();
1890 1890
1891 1891 final_pass = 0;
1892 1892 __split_whole_condition(expr);
1893 1893 final_pass = final_pass_orig;
1894 1894
1895 1895 nonzero_rl = rl_filter(rl, rl_zero());
1896 1896 nonzero_rl = cast_rl(cur_func_return_type(), nonzero_rl);
1897 1897 return_ranges = show_rl(nonzero_rl);
1898 1898 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonzero_rl));
1899 1899
1900 1900 return_id++;
1901 1901 FOR_EACH_PTR(returned_state_callbacks, cb) {
1902 1902 cb->callback(return_id, return_ranges, expr);
1903 1903 } END_FOR_EACH_PTR(cb);
1904 1904
1905 1905 __push_true_states();
1906 1906 __use_false_states();
1907 1907
1908 1908 return_ranges = alloc_sname("0");
1909 1909 zero_sval = sval_type_val(rl_type(rl), 0);
1910 1910 add_range(&zero_rl, zero_sval, zero_sval);
1911 1911 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(zero_rl));
1912 1912 return_id++;
1913 1913 FOR_EACH_PTR(returned_state_callbacks, cb) {
1914 1914 cb->callback(return_id, return_ranges, expr);
1915 1915 } END_FOR_EACH_PTR(cb);
1916 1916
1917 1917 __merge_true_states();
1918 1918 __free_fake_cur_stree();
1919 1919
1920 1920 return 1;
1921 1921 }
1922 1922
1923 1923 static int is_boolean(struct expression *expr)
1924 1924 {
1925 1925 struct range_list *rl;
1926 1926
1927 1927 if (!get_implied_rl(expr, &rl))
1928 1928 return 0;
1929 1929 if (rl_min(rl).value == 0 && rl_max(rl).value == 1)
1930 1930 return 1;
1931 1931 return 0;
1932 1932 }
1933 1933
1934 1934 static int splitable_function_call(struct expression *expr)
1935 1935 {
1936 1936 struct sm_state *sm;
1937 1937 char buf[64];
1938 1938
1939 1939 if (!expr || expr->type != EXPR_CALL)
1940 1940 return 0;
1941 1941 snprintf(buf, sizeof(buf), "return %p", expr);
1942 1942 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
1943 1943 return split_possible_helper(sm, expr);
1944 1944 }
1945 1945
1946 1946 static struct sm_state *find_bool_param(void)
1947 1947 {
1948 1948 struct stree *start_states;
1949 1949 struct symbol *arg;
1950 1950 struct sm_state *sm, *tmp;
1951 1951 sval_t sval;
1952 1952
1953 1953 start_states = get_start_states();
1954 1954
1955 1955 FOR_EACH_PTR_REVERSE(cur_func_sym->ctype.base_type->arguments, arg) {
1956 1956 if (!arg->ident)
1957 1957 continue;
1958 1958 sm = get_sm_state_stree(start_states, SMATCH_EXTRA, arg->ident->name, arg);
1959 1959 if (!sm)
1960 1960 continue;
1961 1961 if (rl_min(estate_rl(sm->state)).value != 0 ||
1962 1962 rl_max(estate_rl(sm->state)).value != 1)
1963 1963 continue;
1964 1964 goto found;
1965 1965 } END_FOR_EACH_PTR_REVERSE(arg);
1966 1966
1967 1967 return NULL;
1968 1968
1969 1969 found:
1970 1970 /*
1971 1971 * Check if it's splitable. If not, then splitting it up is likely not
1972 1972 * useful for the callers.
1973 1973 */
1974 1974 FOR_EACH_PTR(sm->possible, tmp) {
1975 1975 if (is_merged(tmp))
1976 1976 continue;
1977 1977 if (!estate_get_single_value(tmp->state, &sval))
1978 1978 return NULL;
1979 1979 } END_FOR_EACH_PTR(tmp);
1980 1980
1981 1981 return sm;
1982 1982 }
1983 1983
1984 1984 static int split_on_bool_sm(struct sm_state *sm, struct expression *expr)
1985 1985 {
1986 1986 struct returned_state_callback *cb;
1987 1987 struct range_list *ret_rl;
1988 1988 const char *return_ranges;
1989 1989 struct sm_state *tmp;
1990 1990 int ret = 0;
1991 1991 struct state_list *already_handled = NULL;
1992 1992
1993 1993 if (!sm || !sm->merged)
1994 1994 return 0;
1995 1995
1996 1996 if (too_many_possible(sm))
1997 1997 return 0;
1998 1998
1999 1999 FOR_EACH_PTR(sm->possible, tmp) {
2000 2000 if (tmp->merged)
2001 2001 continue;
2002 2002 if (ptr_in_list(tmp, already_handled))
2003 2003 continue;
2004 2004 add_ptr_list(&already_handled, tmp);
2005 2005
2006 2006 ret = 1;
2007 2007 __push_fake_cur_stree();
2008 2008
2009 2009 overwrite_states_using_pool(sm, tmp);
2010 2010
2011 2011 return_ranges = get_return_ranges_str(expr, &ret_rl);
2012 2012 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
2013 2013 return_id++;
2014 2014 FOR_EACH_PTR(returned_state_callbacks, cb) {
2015 2015 cb->callback(return_id, (char *)return_ranges, expr);
2016 2016 } END_FOR_EACH_PTR(cb);
2017 2017
2018 2018 __free_fake_cur_stree();
2019 2019 } END_FOR_EACH_PTR(tmp);
2020 2020
2021 2021 free_slist(&already_handled);
2022 2022
2023 2023 return ret;
2024 2024 }
2025 2025
2026 2026 static int split_by_bool_param(struct expression *expr)
2027 2027 {
2028 2028 struct sm_state *start_sm, *sm;
2029 2029 sval_t sval;
2030 2030
2031 2031 start_sm = find_bool_param();
2032 2032 if (!start_sm)
2033 2033 return 0;
2034 2034 sm = get_sm_state(SMATCH_EXTRA, start_sm->name, start_sm->sym);
2035 2035 if (!sm || estate_get_single_value(sm->state, &sval))
2036 2036 return 0;
2037 2037
2038 2038 if (get_db_state_count() * 2 >= 2000)
2039 2039 return 0;
2040 2040
2041 2041 return split_on_bool_sm(sm, expr);
2042 2042 }
2043 2043
2044 2044 static int split_by_null_nonnull_param(struct expression *expr)
2045 2045 {
2046 2046 struct symbol *arg;
2047 2047 struct sm_state *sm;
2048 2048 int nr_possible;
2049 2049
2050 2050 /* function must only take one pointer */
2051 2051 if (ptr_list_size((struct ptr_list *)cur_func_sym->ctype.base_type->arguments) != 1)
2052 2052 return 0;
2053 2053 arg = first_ptr_list((struct ptr_list *)cur_func_sym->ctype.base_type->arguments);
2054 2054 if (!arg->ident)
2055 2055 return 0;
2056 2056 if (get_real_base_type(arg)->type != SYM_PTR)
2057 2057 return 0;
2058 2058
2059 2059 if (param_was_set_var_sym(arg->ident->name, arg))
2060 2060 return 0;
2061 2061 sm = get_sm_state(SMATCH_EXTRA, arg->ident->name, arg);
2062 2062 if (!sm)
2063 2063 return 0;
2064 2064
2065 2065 if (!has_separate_zero_null(sm))
2066 2066 return 0;
2067 2067
2068 2068 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
2069 2069 if (get_db_state_count() * nr_possible >= 2000)
2070 2070 return 0;
2071 2071
2072 2072 return split_on_bool_sm(sm, expr);
2073 2073 }
2074 2074
2075 2075 struct expression *strip_expr_statement(struct expression *expr)
2076 2076 {
2077 2077 struct expression *orig = expr;
2078 2078 struct statement *stmt, *last_stmt;
2079 2079
2080 2080 if (!expr)
2081 2081 return NULL;
2082 2082 if (expr->type == EXPR_PREOP && expr->op == '(')
2083 2083 expr = expr->unop;
2084 2084 if (expr->type != EXPR_STATEMENT)
2085 2085 return orig;
2086 2086 stmt = expr->statement;
2087 2087 if (!stmt || stmt->type != STMT_COMPOUND)
2088 2088 return orig;
2089 2089
2090 2090 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
2091 2091 if (!last_stmt || last_stmt->type == STMT_LABEL)
2092 2092 last_stmt = last_stmt->label_statement;
2093 2093 if (!last_stmt || last_stmt->type != STMT_EXPRESSION)
2094 2094 return orig;
2095 2095 return strip_expr(last_stmt->expression);
2096 2096 }
2097 2097
2098 2098 static bool is_kernel_error_path(struct expression *expr)
2099 2099 {
2100 2100 struct range_list *rl;
2101 2101
2102 2102 /*
2103 2103 * Splitting up returns requires resources. It also requires resources
2104 2104 * for the caller. It doesn't seem worth it to split anything up.
2105 2105 */
2106 2106 if (!get_implied_rl(expr, &rl))
2107 2107 return false;
2108 2108 if (rl_type(rl) != &int_ctype)
2109 2109 return false;
2110 2110 if (rl_min(rl).value >= -4095 &&
2111 2111 rl_max(rl).value < 0)
2112 2112 return true;
2113 2113 return false;
2114 2114 }
2115 2115
2116 2116 static void call_return_state_hooks(struct expression *expr)
2117 2117 {
2118 2118 struct returned_state_callback *cb;
2119 2119 struct range_list *ret_rl;
2120 2120 const char *return_ranges;
2121 2121 int nr_states;
2122 2122 sval_t sval;
2123 2123
2124 2124 if (__path_is_null())
2125 2125 return;
2126 2126
2127 2127 expr = strip_expr(expr);
2128 2128 expr = strip_expr_statement(expr);
2129 2129
2130 2130 if (is_impossible_path())
2131 2131 goto vanilla;
2132 2132
2133 2133 if (expr && (expr->type == EXPR_COMPARE ||
2134 2134 !get_implied_value(expr, &sval)) &&
2135 2135 (is_condition(expr) || is_boolean(expr))) {
2136 2136 call_return_state_hooks_compare(expr);
2137 2137 return;
2138 2138 } else if (call_return_state_hooks_conditional(expr)) {
2139 2139 return;
2140 2140 } else if (is_kernel_error_path(expr)) {
2141 2141 goto vanilla;
2142 2142 } else if (call_return_state_hooks_split_possible(expr)) {
2143 2143 return;
2144 2144 } else if (split_positive_from_negative(expr)) {
2145 2145 return;
2146 2146 } else if (call_return_state_hooks_split_null_non_null_zero(expr)) {
2147 2147 return;
2148 2148 } else if (call_return_state_hooks_split_success_fail(expr)) {
2149 2149 return;
2150 2150 } else if (splitable_function_call(expr)) {
2151 2151 return;
2152 2152 } else if (split_by_bool_param(expr)) {
2153 2153 } else if (split_by_null_nonnull_param(expr)) {
2154 2154 return;
2155 2155 }
2156 2156
2157 2157 vanilla:
2158 2158 return_ranges = get_return_ranges_str(expr, &ret_rl);
2159 2159 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
2160 2160
2161 2161 return_id++;
2162 2162 nr_states = get_db_state_count();
2163 2163 if (nr_states >= 10000) {
2164 2164 match_return_info(return_id, (char *)return_ranges, expr);
2165 2165 print_limited_param_set(return_id, (char *)return_ranges, expr);
2166 2166 mark_all_params_untracked(return_id, (char *)return_ranges, expr);
2167 2167 return;
2168 2168 }
2169 2169 FOR_EACH_PTR(returned_state_callbacks, cb) {
2170 2170 cb->callback(return_id, (char *)return_ranges, expr);
2171 2171 } END_FOR_EACH_PTR(cb);
2172 2172 }
2173 2173
2174 2174 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
2175 2175 {
2176 2176 struct returned_member_callback *cb;
2177 2177 struct stree *stree;
2178 2178 struct sm_state *sm;
2179 2179 struct symbol *type;
2180 2180 char *name;
2181 2181 char member_name[256];
2182 2182 int len;
2183 2183
2184 2184 type = get_type(expr);
2185 2185 if (!type || type->type != SYM_PTR)
2186 2186 return;
2187 2187 name = expr_to_var(expr);
2188 2188 if (!name)
2189 2189 return;
2190 2190
2191 2191 member_name[sizeof(member_name) - 1] = '\0';
2192 2192 strcpy(member_name, "$");
2193 2193
2194 2194 len = strlen(name);
2195 2195 FOR_EACH_PTR(returned_member_callbacks, cb) {
2196 2196 stree = __get_cur_stree();
2197 2197 FOR_EACH_MY_SM(cb->owner, stree, sm) {
2198 2198 if (sm->name[0] == '*' && strcmp(sm->name + 1, name) == 0) {
2199 2199 strcpy(member_name, "*$");
2200 2200 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
2201 2201 continue;
2202 2202 }
2203 2203 if (strncmp(sm->name, name, len) != 0)
2204 2204 continue;
2205 2205 if (strncmp(sm->name + len, "->", 2) != 0)
2206 2206 continue;
2207 2207 snprintf(member_name, sizeof(member_name), "$%s", sm->name + len);
2208 2208 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
2209 2209 } END_FOR_EACH_SM(sm);
2210 2210 } END_FOR_EACH_PTR(cb);
2211 2211
2212 2212 free_string(name);
2213 2213 }
2214 2214
2215 2215 static void reset_memdb(struct symbol *sym)
2216 2216 {
2217 2217 mem_sql(NULL, NULL, "delete from caller_info;");
2218 2218 mem_sql(NULL, NULL, "delete from return_states;");
2219 2219 mem_sql(NULL, NULL, "delete from call_implies;");
2220 2220 mem_sql(NULL, NULL, "delete from return_implies;");
2221 2221 }
2222 2222
2223 2223 static void match_end_func_info(struct symbol *sym)
2224 2224 {
2225 2225 if (__path_is_null())
2226 2226 return;
2227 2227 call_return_state_hooks(NULL);
2228 2228 }
2229 2229
2230 2230 static void match_after_func(struct symbol *sym)
2231 2231 {
2232 2232 if (!__inline_fn)
2233 2233 reset_memdb(sym);
2234 2234 }
2235 2235
2236 2236 static void init_memdb(void)
2237 2237 {
2238 2238 char *err = NULL;
2239 2239 int rc;
2240 2240 const char *schema_files[] = {
2241 2241 "db/db.schema",
2242 2242 "db/caller_info.schema",
2243 2243 "db/common_caller_info.schema",
2244 2244 "db/return_states.schema",
2245 2245 "db/function_type_size.schema",
2246 2246 "db/type_size.schema",
2247 2247 "db/function_type_info.schema",
2248 2248 "db/type_info.schema",
2249 2249 "db/call_implies.schema",
2250 2250 "db/return_implies.schema",
2251 2251 "db/function_ptr.schema",
2252 2252 "db/local_values.schema",
2253 2253 "db/function_type_value.schema",
2254 2254 "db/type_value.schema",
2255 2255 "db/function_type.schema",
2256 2256 "db/data_info.schema",
2257 2257 "db/parameter_name.schema",
2258 2258 "db/constraints.schema",
2259 2259 "db/constraints_required.schema",
2260 2260 "db/fn_ptr_data_link.schema",
2261 2261 "db/fn_data_link.schema",
2262 2262 "db/mtag_about.schema",
2263 2263 "db/mtag_info.schema",
2264 2264 "db/mtag_map.schema",
2265 2265 "db/mtag_data.schema",
2266 2266 "db/mtag_alias.schema",
2267 2267 };
2268 2268 static char buf[4096];
2269 2269 int fd;
2270 2270 int ret;
2271 2271 int i;
2272 2272
2273 2273 rc = sqlite3_open(":memory:", &mem_db);
2274 2274 if (rc != SQLITE_OK) {
2275 2275 sm_ierror("starting In-Memory database.");
2276 2276 return;
2277 2277 }
2278 2278
2279 2279 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
2280 2280 fd = open_schema_file(schema_files[i]);
2281 2281 if (fd < 0)
2282 2282 continue;
2283 2283 ret = read(fd, buf, sizeof(buf));
2284 2284 if (ret < 0) {
2285 2285 sm_ierror("failed to read: %s", schema_files[i]);
2286 2286 continue;
2287 2287 }
2288 2288 close(fd);
2289 2289 if (ret == sizeof(buf)) {
2290 2290 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2291 2291 schema_files[i], sizeof(buf));
2292 2292 continue;
2293 2293 }
2294 2294 buf[ret] = '\0';
2295 2295 rc = sqlite3_exec(mem_db, buf, NULL, NULL, &err);
2296 2296 if (rc != SQLITE_OK) {
2297 2297 sm_ierror("SQL error #2: %s", err);
2298 2298 sm_ierror("%s", buf);
2299 2299 }
2300 2300 }
2301 2301 }
2302 2302
2303 2303 static void init_cachedb(void)
2304 2304 {
2305 2305 char *err = NULL;
2306 2306 int rc;
2307 2307 const char *schema_files[] = {
2308 2308 "db/call_implies.schema",
2309 2309 "db/return_implies.schema",
2310 2310 "db/type_info.schema",
2311 2311 "db/mtag_about.schema",
2312 2312 "db/mtag_data.schema",
2313 2313 "db/mtag_info.schema",
2314 2314 "db/sink_info.schema",
2315 2315 };
2316 2316 static char buf[4096];
2317 2317 int fd;
2318 2318 int ret;
2319 2319 int i;
2320 2320
2321 2321 rc = sqlite3_open(":memory:", &cache_db);
2322 2322 if (rc != SQLITE_OK) {
2323 2323 sm_ierror("starting In-Memory database.");
2324 2324 return;
2325 2325 }
2326 2326
2327 2327 for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
2328 2328 fd = open_schema_file(schema_files[i]);
2329 2329 if (fd < 0)
2330 2330 continue;
2331 2331 ret = read(fd, buf, sizeof(buf));
2332 2332 if (ret < 0) {
2333 2333 sm_ierror("failed to read: %s", schema_files[i]);
2334 2334 continue;
2335 2335 }
2336 2336 close(fd);
2337 2337 if (ret == sizeof(buf)) {
2338 2338 sm_ierror("Schema file too large: %s (limit %zd bytes)",
2339 2339 schema_files[i], sizeof(buf));
2340 2340 continue;
2341 2341 }
2342 2342 buf[ret] = '\0';
2343 2343 rc = sqlite3_exec(cache_db, buf, NULL, NULL, &err);
2344 2344 if (rc != SQLITE_OK) {
2345 2345 sm_ierror("SQL error #2: %s", err);
2346 2346 sm_ierror("%s", buf);
2347 2347 }
2348 2348 }
2349 2349 }
2350 2350
2351 2351 static int save_cache_data(void *_table, int argc, char **argv, char **azColName)
2352 2352 {
2353 2353 static char buf[4096];
2354 2354 char tmp[256];
2355 2355 char *p = buf;
2356 2356 char *table = _table;
2357 2357 int i;
2358 2358
2359 2359
2360 2360 p += snprintf(p, 4096 - (p - buf), "insert or ignore into %s values (", table);
2361 2361 for (i = 0; i < argc; i++) {
2362 2362 if (i)
2363 2363 p += snprintf(p, 4096 - (p - buf), ", ");
2364 2364 sqlite3_snprintf(sizeof(tmp), tmp, "%q", escape_newlines(argv[i]));
2365 2365 p += snprintf(p, 4096 - (p - buf), "'%s'", tmp);
2366 2366
2367 2367 }
2368 2368 p += snprintf(p, 4096 - (p - buf), ");");
2369 2369 if (p - buf > 4096)
2370 2370 return 0;
2371 2371
2372 2372 sm_msg("SQL: %s", buf);
2373 2373 return 0;
2374 2374 }
2375 2375
2376 2376 static void dump_cache(struct symbol_list *sym_list)
2377 2377 {
2378 2378 const char *cache_tables[] = {
2379 2379 "type_info", "return_implies", "call_implies", "mtag_data",
2380 2380 "mtag_info", "mtag_about", "sink_info",
2381 2381 };
2382 2382 char buf[64];
2383 2383 int i;
2384 2384
2385 2385 if (!option_info)
2386 2386 return;
2387 2387
2388 2388 for (i = 0; i < ARRAY_SIZE(cache_tables); i++) {
2389 2389 snprintf(buf, sizeof(buf), "select * from %s;", cache_tables[i]);
2390 2390 cache_sql(&save_cache_data, (char *)cache_tables[i], buf);
2391 2391 }
2392 2392 }
2393 2393
2394 2394 void open_smatch_db(char *db_file)
2395 2395 {
2396 2396 int rc;
2397 2397
2398 2398 if (option_no_db)
2399 2399 return;
2400 2400
2401 2401 use_states = malloc(num_checks + 1);
2402 2402 memset(use_states, 0xff, num_checks + 1);
2403 2403
2404 2404 init_memdb();
2405 2405 init_cachedb();
2406 2406
2407 2407 rc = sqlite3_open_v2(db_file, &smatch_db, SQLITE_OPEN_READONLY, NULL);
2408 2408 if (rc != SQLITE_OK) {
2409 2409 option_no_db = 1;
2410 2410 return;
2411 2411 }
2412 2412 run_sql(NULL, NULL,
2413 2413 "PRAGMA cache_size = %d;", SQLITE_CACHE_PAGES);
2414 2414 return;
2415 2415 }
2416 2416
2417 2417 static void register_common_funcs(void)
2418 2418 {
2419 2419 struct token *token;
2420 2420 char *func;
2421 2421 char filename[256];
2422 2422
2423 2423 if (option_project == PROJ_NONE)
2424 2424 strcpy(filename, "common_functions");
2425 2425 else
2426 2426 snprintf(filename, 256, "%s.common_functions", option_project_str);
2427 2427
2428 2428 token = get_tokens_file(filename);
2429 2429 if (!token)
2430 2430 return;
2431 2431 if (token_type(token) != TOKEN_STREAMBEGIN)
2432 2432 return;
2433 2433 token = token->next;
2434 2434 while (token_type(token) != TOKEN_STREAMEND) {
2435 2435 if (token_type(token) != TOKEN_IDENT)
2436 2436 return;
2437 2437 func = alloc_string(show_ident(token->ident));
2438 2438 add_ptr_list(&common_funcs, func);
2439 2439 token = token->next;
2440 2440 }
2441 2441 clear_token_alloc();
2442 2442 }
2443 2443
2444 2444 static char *get_next_string(char **str)
2445 2445 {
2446 2446 static char string[256];
2447 2447 char *start;
2448 2448 char *p = *str;
2449 2449 int len, i, j;
2450 2450
2451 2451 if (*p == '\0')
2452 2452 return NULL;
2453 2453 start = p;
2454 2454
2455 2455 while (*p != '\0' && *p != '\n') {
2456 2456 if (*p == '\\' && *(p + 1) == ' ') {
2457 2457 p += 2;
2458 2458 continue;
2459 2459 }
2460 2460 if (*p == ' ')
2461 2461 break;
2462 2462 p++;
2463 2463 }
2464 2464
2465 2465 len = p - start;
2466 2466 if (len >= sizeof(string)) {
2467 2467 memcpy(string, start, sizeof(string));
2468 2468 string[sizeof(string) - 1] = '\0';
2469 2469 sm_ierror("return_fix: '%s' too long", string);
2470 2470 **str = '\0';
2471 2471 return NULL;
2472 2472 }
2473 2473 memcpy(string, start, len);
2474 2474 string[len] = '\0';
2475 2475 for (i = 0; i < sizeof(string) - 1; i++) {
2476 2476 if (string[i] == '\\' && string[i + 1] == ' ') {
2477 2477 for (j = i; string[j] != '\0'; j++)
2478 2478 string[j] = string[j + 1];
2479 2479 }
2480 2480 }
2481 2481 if (*p != '\0')
2482 2482 p++;
2483 2483 *str = p;
2484 2484 return string;
2485 2485 }
2486 2486
2487 2487 static void register_return_replacements(void)
2488 2488 {
2489 2489 char *func, *orig, *new;
2490 2490 char filename[256];
2491 2491 char buf[4096];
2492 2492 int fd, ret, i;
2493 2493 char *p;
2494 2494
2495 2495 snprintf(filename, 256, "db/%s.return_fixes", option_project_str);
2496 2496 fd = open_schema_file(filename);
2497 2497 if (fd < 0)
2498 2498 return;
2499 2499 ret = read(fd, buf, sizeof(buf));
2500 2500 close(fd);
2501 2501 if (ret < 0)
2502 2502 return;
2503 2503 if (ret == sizeof(buf)) {
2504 2504 sm_ierror("file too large: %s (limit %zd bytes)",
2505 2505 filename, sizeof(buf));
2506 2506 return;
2507 2507 }
2508 2508 buf[ret] = '\0';
2509 2509
2510 2510 p = buf;
2511 2511 while (*p) {
2512 2512 get_next_string(&p);
2513 2513 replace_count++;
2514 2514 }
2515 2515 if (replace_count == 0 || replace_count % 3 != 0) {
2516 2516 replace_count = 0;
2517 2517 return;
2518 2518 }
2519 2519 replace_table = malloc(replace_count * sizeof(char *));
2520 2520
2521 2521 p = buf;
2522 2522 i = 0;
2523 2523 while (*p) {
2524 2524 func = alloc_string(get_next_string(&p));
2525 2525 orig = alloc_string(get_next_string(&p));
2526 2526 new = alloc_string(get_next_string(&p));
2527 2527
2528 2528 replace_table[i++] = func;
2529 2529 replace_table[i++] = orig;
2530 2530 replace_table[i++] = new;
2531 2531 }
2532 2532 }
2533 2533
2534 2534 void register_definition_db_callbacks(int id)
2535 2535 {
2536 2536 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
2537 2537 add_hook(&match_call_info_new, FUNCTION_CALL_HOOK);
2538 2538 add_split_return_callback(match_return_info);
2539 2539 add_split_return_callback(print_returned_struct_members);
2540 2540 add_hook(&call_return_state_hooks, RETURN_HOOK);
2541 2541 add_hook(&match_end_func_info, END_FUNC_HOOK);
2542 2542 add_hook(&match_after_func, AFTER_FUNC_HOOK);
2543 2543
2544 2544 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
2545 2545 add_hook(&match_call_implies, FUNC_DEF_HOOK);
2546 2546 add_hook(&match_return_implies, CALL_HOOK_AFTER_INLINE);
2547 2547
2548 2548 register_common_funcs();
2549 2549 register_return_replacements();
2550 2550
2551 2551 add_hook(&dump_cache, END_FILE_HOOK);
2552 2552 }
2553 2553
2554 2554 void register_db_call_marker(int id)
2555 2555 {
2556 2556 add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
2557 2557 }
2558 2558
2559 2559 char *return_state_to_var_sym(struct expression *expr, int param, const char *key, struct symbol **sym)
2560 2560 {
2561 2561 struct expression *arg;
2562 2562 char *name = NULL;
2563 2563 char member_name[256];
2564 2564
2565 2565 *sym = NULL;
2566 2566
2567 2567 if (param == -1) {
2568 2568 const char *star = "";
2569 2569
2570 2570 if (expr->type != EXPR_ASSIGNMENT)
2571 2571 return NULL;
2572 2572 if (get_type(expr->left) == &int_ctype && strcmp(key, "$") != 0)
2573 2573 return NULL;
2574 2574 name = expr_to_var_sym(expr->left, sym);
2575 2575 if (!name)
2576 2576 return NULL;
2577 2577 if (key[0] == '*') {
2578 2578 star = "*";
2579 2579 key++;
2580 2580 }
2581 2581 if (strncmp(key, "$", 1) != 0)
2582 2582 return name;
2583 2583 snprintf(member_name, sizeof(member_name), "%s%s%s", star, name, key + 1);
2584 2584 free_string(name);
2585 2585 return alloc_string(member_name);
2586 2586 }
2587 2587
2588 2588 while (expr->type == EXPR_ASSIGNMENT)
2589 2589 expr = strip_expr(expr->right);
2590 2590 if (expr->type != EXPR_CALL)
2591 2591 return NULL;
2592 2592
2593 2593 arg = get_argument_from_call_expr(expr->args, param);
2594 2594 if (!arg)
2595 2595 return NULL;
2596 2596
2597 2597 return get_variable_from_key(arg, key, sym);
2598 2598 }
2599 2599
2600 2600 char *get_variable_from_key(struct expression *arg, const char *key, struct symbol **sym)
2601 2601 {
2602 2602 struct symbol *type;
2603 2603 char buf[256];
2604 2604 char *tmp;
2605 2605 int star_cnt = 0;
2606 2606 bool add_dot = false;
2607 2607
2608 2608 if (!arg)
2609 2609 return NULL;
2610 2610
2611 2611 arg = strip_expr(arg);
2612 2612
2613 2613 if (strcmp(key, "$") == 0)
2614 2614 return expr_to_var_sym(arg, sym);
2615 2615
2616 2616 if (strcmp(key, "*$") == 0) {
2617 2617 if (arg->type == EXPR_PREOP && arg->op == '&') {
2618 2618 arg = strip_expr(arg->unop);
2619 2619 return expr_to_var_sym(arg, sym);
2620 2620 } else {
2621 2621 tmp = expr_to_var_sym(arg, sym);
2622 2622 if (!tmp)
2623 2623 return NULL;
2624 2624 snprintf(buf, sizeof(buf), "*%s", tmp);
2625 2625 free_string(tmp);
2626 2626 return alloc_string(buf);
2627 2627 }
2628 2628 }
2629 2629
2630 2630 if (strncmp(key, "(*$)", 4) == 0) {
2631 2631 char buf[64];
2632 2632
2633 2633 if (arg->type == EXPR_PREOP && arg->op == '&') {
2634 2634 arg = strip_expr(arg->unop);
2635 2635 snprintf(buf, sizeof(buf), "$%s", key + 4);
2636 2636 return get_variable_from_key(arg, buf, sym);
2637 2637 } else {
2638 2638 tmp = expr_to_var_sym(arg, sym);
2639 2639 if (!tmp)
2640 2640 return NULL;
2641 2641 snprintf(buf, sizeof(buf), "(*%s)%s", tmp, key + 4);
2642 2642 free_string(tmp);
2643 2643 return alloc_string(buf);
2644 2644 }
2645 2645 }
2646 2646
2647 2647 while (key[0] == '*') {
2648 2648 star_cnt++;
2649 2649 key++;
2650 2650 }
2651 2651
2652 2652 /*
2653 2653 * FIXME: This is a hack.
2654 2654 * We should be able to parse expressions like (*$)->foo and *$->foo.
2655 2655 */
2656 2656 type = get_type(arg);
2657 2657 if (is_struct_ptr(type))
2658 2658 add_dot = true;
2659 2659
2660 2660 if (arg->type == EXPR_PREOP && arg->op == '&' && star_cnt && !add_dot) {
2661 2661 arg = strip_expr(arg->unop);
2662 2662 star_cnt--;
2663 2663 }
2664 2664
2665 2665 if (arg->type == EXPR_PREOP && arg->op == '&') {
2666 2666 arg = strip_expr(arg->unop);
2667 2667 tmp = expr_to_var_sym(arg, sym);
2668 2668 if (!tmp)
2669 2669 return NULL;
2670 2670 snprintf(buf, sizeof(buf), "%.*s%s.%s",
2671 2671 star_cnt, "**********", tmp, key + 3);
2672 2672 return alloc_string(buf);
2673 2673 }
2674 2674
2675 2675 tmp = expr_to_var_sym(arg, sym);
2676 2676 if (!tmp)
2677 2677 return NULL;
2678 2678 snprintf(buf, sizeof(buf), "%.*s%s%s", star_cnt, "**********", tmp, key + 1);
2679 2679 free_string(tmp);
2680 2680 return alloc_string(buf);
2681 2681 }
2682 2682
2683 2683 char *get_chunk_from_key(struct expression *arg, char *key, struct symbol **sym, struct var_sym_list **vsl)
2684 2684 {
2685 2685 *vsl = NULL;
2686 2686
2687 2687 if (strcmp("$", key) == 0)
2688 2688 return expr_to_chunk_sym_vsl(arg, sym, vsl);
2689 2689 return get_variable_from_key(arg, key, sym);
2690 2690 }
2691 2691
2692 2692 const char *state_name_to_param_name(const char *state_name, const char *param_name)
2693 2693 {
2694 2694 int star_cnt = 0;
2695 2695 int name_len;
2696 2696 char buf[256];
2697 2697
2698 2698 name_len = strlen(param_name);
2699 2699
2700 2700 while (state_name[0] == '*') {
2701 2701 star_cnt++;
2702 2702 state_name++;
2703 2703 }
2704 2704
2705 2705 /* ten out of ten stars! */
2706 2706 if (star_cnt > 10)
2707 2707 return NULL;
2708 2708
2709 2709 if (strncmp(state_name, "(*", 2) == 0 &&
2710 2710 strncmp(state_name + 2, param_name, name_len) == 0 &&
2711 2711 state_name[name_len + 2] == ')') {
↓ open down ↓ |
2711 lines elided |
↑ open up ↑ |
2712 2712 snprintf(buf, sizeof(buf), "%.*s(*$)%s", star_cnt, "**********",
2713 2713 state_name + name_len + 3);
2714 2714 return alloc_sname(buf);
2715 2715 }
2716 2716
2717 2717 if (strcmp(state_name, param_name) == 0) {
2718 2718 snprintf(buf, sizeof(buf), "%.*s$", star_cnt, "**********");
2719 2719 return alloc_sname(buf);
2720 2720 }
2721 2721
2722 - if (state_name[name_len] == '-' && /* check for '-' from "->" */
2723 - strncmp(state_name, param_name, name_len) == 0) {
2722 + /* check for '-' from "->" */
2723 + if (strncmp(state_name, param_name, name_len) == 0 &&
2724 + state_name[name_len] == '-') {
2724 2725 snprintf(buf, sizeof(buf), "%.*s$%s", star_cnt, "**********", state_name + name_len);
2725 2726 return alloc_sname(buf);
2726 2727 }
2727 2728 return NULL;
2728 2729 }
2729 2730
2730 2731 const char *get_param_name_var_sym(const char *name, struct symbol *sym)
2731 2732 {
2732 2733 if (!sym || !sym->ident)
2733 2734 return NULL;
2734 2735
2735 2736 return state_name_to_param_name(name, sym->ident->name);
2736 2737 }
2737 2738
2738 2739 const char *get_mtag_name_var_sym(const char *state_name, struct symbol *sym)
2739 2740 {
2740 2741 struct symbol *type;
2741 2742 const char *sym_name;
2742 2743 int name_len;
2743 2744 static char buf[256];
2744 2745
2745 2746 /*
2746 2747 * mtag_name is different from param_name because mtags can be a struct
2747 2748 * instead of a struct pointer. But we want to treat it like a pointer
2748 2749 * because really an mtag is a pointer. Or in other words, if you pass
2749 2750 * a struct foo then you want to talk about foo.bar but with an mtag
2750 2751 * you want to refer to it as foo->bar.
2751 2752 *
2752 2753 */
2753 2754
2754 2755 if (!sym || !sym->ident)
2755 2756 return NULL;
2756 2757
2757 2758 type = get_real_base_type(sym);
2758 2759 if (type && type->type == SYM_BASETYPE)
2759 2760 return "*$";
2760 2761
2761 2762 sym_name = sym->ident->name;
2762 2763 name_len = strlen(sym_name);
2763 2764
2764 2765 if (state_name[name_len] == '.' && /* check for '-' from "->" */
2765 2766 strncmp(state_name, sym_name, name_len) == 0) {
2766 2767 snprintf(buf, sizeof(buf), "$->%s", state_name + name_len + 1);
2767 2768 return buf;
2768 2769 }
2769 2770
2770 2771 return state_name_to_param_name(state_name, sym_name);
2771 2772 }
2772 2773
2773 2774 const char *get_mtag_name_expr(struct expression *expr)
2774 2775 {
2775 2776 char *name;
2776 2777 struct symbol *sym;
2777 2778 const char *ret = NULL;
2778 2779
2779 2780 name = expr_to_var_sym(expr, &sym);
2780 2781 if (!name || !sym)
2781 2782 goto free;
2782 2783
2783 2784 ret = get_mtag_name_var_sym(name, sym);
2784 2785 free:
2785 2786 free_string(name);
2786 2787 return ret;
2787 2788 }
2788 2789
2789 2790 const char *get_param_name(struct sm_state *sm)
2790 2791 {
2791 2792 return get_param_name_var_sym(sm->name, sm->sym);
2792 2793 }
2793 2794
2794 2795 char *get_data_info_name(struct expression *expr)
2795 2796 {
2796 2797 struct symbol *sym;
2797 2798 char *name;
2798 2799 char buf[256];
2799 2800 char *ret = NULL;
2800 2801
2801 2802 expr = strip_expr(expr);
2802 2803 name = get_member_name(expr);
2803 2804 if (name)
2804 2805 return name;
2805 2806 name = expr_to_var_sym(expr, &sym);
2806 2807 if (!name || !sym)
2807 2808 goto free;
2808 2809 if (!(sym->ctype.modifiers & MOD_TOPLEVEL))
2809 2810 goto free;
2810 2811 if (sym->ctype.modifiers & MOD_STATIC)
2811 2812 snprintf(buf, sizeof(buf), "static %s", name);
2812 2813 else
2813 2814 snprintf(buf, sizeof(buf), "global %s", name);
2814 2815 ret = alloc_sname(buf);
2815 2816 free:
2816 2817 free_string(name);
2817 2818 return ret;
2818 2819 }
↓ open down ↓ |
85 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX