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