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