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 int is_recursive_member(const char *name)
 772 {
 773         char buf[256];
 774         const char *p, *next;
 775         int size;
 776 
 777         p = strchr(name, '>');
 778         if (!p)
 779                 return 0;
 780         p++;
 781         while (true) {
 782                 next = strchr(p, '>');
 783                 if (!next)
 784                         return 0;
 785                 next++;
 786 
 787                 size = next - p;
 788                 if (size >= sizeof(buf))
 789                         return 0;
 790                 memcpy(buf, p, size);
 791                 buf[size] = '\0';
 792                 if (strstr(next, buf))
 793                         return 1;
 794                 p = next;
 795         }
 796 }
 797 
 798 char *sm_to_arg_name(struct expression *expr, struct sm_state *sm)
 799 {
 800         struct symbol *sym;
 801         const char *sm_name;
 802         char *name;
 803         bool is_address = false;
 804         bool add_star = false;
 805         char buf[256];
 806         char *ret = NULL;
 807         int len;
 808 
 809         expr = strip_expr(expr);
 810         if (!expr)
 811                 return NULL;
 812 
 813         if (expr->type == EXPR_PREOP && expr->op == '&') {
 814                 expr = strip_expr(expr->unop);
 815                 is_address = true;
 816         }
 817 
 818         name = expr_to_var_sym(expr, &sym);
 819         if (!name || !sym)
 820                 goto free;
 821         if (sym != sm->sym)
 822                 goto free;
 823 
 824         sm_name = sm->name;
 825         add_star = false;
 826         if (sm_name[0] == '*') {
 827                 add_star = true;
 828                 sm_name++;
 829         }
 830 
 831         len = strlen(name);
 832         if (strncmp(name, sm_name, len) != 0)
 833                 goto free;
 834         if (sm_name[len] == '\0') {
 835                 snprintf(buf, sizeof(buf), "%s%s$",
 836                          add_star ? "*" : "", is_address ? "*" : "");
 837         } else {
 838                 if (sm_name[len] != '.' && sm_name[len] != '-')
 839                         goto free;
 840                 if (sm_name[len] == '-')
 841                         len++;
 842                 // FIXME does is_address really imply that sm_name[len] == '-'
 843                 snprintf(buf, sizeof(buf), "%s$->%s", add_star ? "*" : "",
 844                          sm_name + len);
 845         }
 846 
 847         ret = alloc_sname(buf);
 848 free:
 849         free_string(name);
 850         return ret;
 851 }
 852 
 853 static void print_struct_members(struct expression *call, struct expression *expr, int param, struct stree *stree,
 854         void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
 855 {
 856         struct sm_state *sm;
 857         const char *sm_name;
 858         char *name;
 859         struct symbol *sym;
 860         int len;
 861         char printed_name[256];
 862         int is_address = 0;
 863         bool add_star;
 864         struct symbol *type;
 865 
 866         expr = strip_expr(expr);
 867         if (!expr)
 868                 return;
 869         type = get_type(expr);
 870         if (type && type_bits(type) < type_bits(&ulong_ctype))
 871                 return;
 872 
 873         if (expr->type == EXPR_PREOP && expr->op == '&') {
 874                 expr = strip_expr(expr->unop);
 875                 is_address = 1;
 876         }
 877 
 878         name = expr_to_var_sym(expr, &sym);
 879         if (!name || !sym)
 880                 goto free;
 881 
 882         len = strlen(name);
 883         FOR_EACH_SM(stree, sm) {
 884                 if (sm->sym != sym)
 885                         continue;
 886                 sm_name = sm->name;
 887                 add_star = false;
 888                 if (sm_name[0] == '*') {
 889                         add_star = true;
 890                         sm_name++;
 891                 }
 892                 // FIXME: simplify?
 893                 if (!add_star && strcmp(name, sm_name) == 0) {
 894                         if (is_address)
 895                                 snprintf(printed_name, sizeof(printed_name), "*$");
 896                         else /* these are already handled. fixme: handle them here */
 897                                 continue;
 898                 } else if (add_star && strcmp(name, sm_name) == 0) {
 899                         snprintf(printed_name, sizeof(printed_name), "%s*$",
 900                                  is_address ? "*" : "");
 901                 } else if (strncmp(name, sm_name, len) == 0) {
 902                         if (sm_name[len] != '.' && sm_name[len] != '-')
 903                                 continue;
 904                         if (is_address)
 905                                 snprintf(printed_name, sizeof(printed_name),
 906                                          "%s$->%s", add_star ? "*" : "",
 907                                          sm_name + len + 1);
 908                         else
 909                                 snprintf(printed_name, sizeof(printed_name),
 910                                          "%s$%s", add_star ? "*" : "",
 911                                          sm_name + len);
 912                 } else {
 913                         continue;
 914                 }
 915                 if (is_recursive_member(printed_name))
 916                         continue;
 917                 callback(call, param, printed_name, sm);
 918         } END_FOR_EACH_SM(sm);
 919 free:
 920         free_string(name);
 921 }
 922 
 923 static void match_call_info(struct expression *call)
 924 {
 925         struct member_info_callback *cb;
 926         struct expression *arg;
 927         struct stree *stree;
 928         char *name;
 929         int i;
 930 
 931         name = get_fnptr_name(call->fn);
 932         if (!name)
 933                 return;
 934 
 935         FOR_EACH_PTR(member_callbacks, cb) {
 936                 stree = get_all_states_stree(cb->owner);
 937                 i = 0;
 938                 FOR_EACH_PTR(call->args, arg) {
 939                         print_struct_members(call, arg, i, stree, cb->callback);
 940                         i++;
 941                 } END_FOR_EACH_PTR(arg);
 942                 free_stree(&stree);
 943         } END_FOR_EACH_PTR(cb);
 944 
 945         free_string(name);
 946 }
 947 
 948 static int get_param(int param, char **name, struct symbol **sym)
 949 {
 950         struct symbol *arg;
 951         int i;
 952 
 953         i = 0;
 954         FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
 955                 /*
 956                  * this is a temporary hack to work around a bug (I think in sparse?)
 957                  * 2.6.37-rc1:fs/reiserfs/journal.o
 958                  * If there is a function definition without parameter name found
 959                  * after a function implementation then it causes a crash.
 960                  * int foo() {}
 961                  * int bar(char *);
 962                  */
 963                 if (arg->ident->name < (char *)100)
 964                         continue;
 965                 if (i == param) {
 966                         *name = arg->ident->name;
 967                         *sym = arg;
 968                         return TRUE;
 969                 }
 970                 i++;
 971         } END_FOR_EACH_PTR(arg);
 972 
 973         return FALSE;
 974 }
 975 
 976 static int function_signature_matches(const char *sig)
 977 {
 978         char *my_sig;
 979 
 980         my_sig = function_signature();
 981         if (!sig || !my_sig)
 982                 return 1;  /* default to matching */
 983         if (strcmp(my_sig, sig) == 0)
 984                   return 1;
 985         return 0;
 986 }
 987 
 988 static int caller_info_callback(void *_data, int argc, char **argv, char **azColName)
 989 {
 990         struct select_caller_info_data *data = _data;
 991         int func_id;
 992         long type;
 993         long param;
 994         char *key;
 995         char *value;
 996         char *name = NULL;
 997         struct symbol *sym = NULL;
 998         struct def_callback *def_callback;
 999         struct stree *stree;
1000         struct timeval cur_time;
1001 
1002         data->results = 1;
1003 
1004         if (argc != 5)
1005                 return 0;
1006 
1007         gettimeofday(&cur_time, NULL);
1008         if (cur_time.tv_sec - data->start_time.tv_sec > 10)
1009                 return 0;
1010 
1011         func_id = atoi(argv[0]);
1012         errno = 0;
1013         type = strtol(argv[1], NULL, 10);
1014         param = strtol(argv[2], NULL, 10);
1015         if (errno)
1016                 return 0;
1017         key = argv[3];
1018         value = argv[4];
1019 
1020         if (data->prev_func_id == -1)
1021                 data->prev_func_id = func_id;
1022         if (func_id != data->prev_func_id) {
1023                 stree = __pop_fake_cur_stree();
1024                 if (!data->ignore)
1025                         merge_stree(&data->final_states, stree);
1026                 free_stree(&stree);
1027                 __push_fake_cur_stree();
1028                 __unnullify_path();
1029                 data->prev_func_id = func_id;
1030                 data->ignore = 0;
1031         }
1032 
1033         if (data->ignore)
1034                 return 0;
1035         if (type == INTERNAL &&
1036             !function_signature_matches(value)) {
1037                 data->ignore = 1;
1038                 return 0;
1039         }
1040 
1041         if (param >= 0 && !get_param(param, &name, &sym))
1042                 return 0;
1043 
1044         FOR_EACH_PTR(select_caller_info_callbacks, def_callback) {
1045                 if (def_callback->hook_type == type)
1046                         def_callback->callback(name, sym, key, value);
1047         } END_FOR_EACH_PTR(def_callback);
1048 
1049         return 0;
1050 }
1051 
1052 static struct string_list *ptr_names_done;
1053 static struct string_list *ptr_names;
1054 
1055 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
1056 {
1057         insert_string(&ptr_names, alloc_string(argv[0]));
1058         return 0;
1059 }
1060 
1061 static char *get_next_ptr_name(void)
1062 {
1063         char *ptr;
1064 
1065         FOR_EACH_PTR(ptr_names, ptr) {
1066                 if (!insert_string(&ptr_names_done, ptr))
1067                         continue;
1068                 return ptr;
1069         } END_FOR_EACH_PTR(ptr);
1070         return NULL;
1071 }
1072 
1073 static void get_ptr_names(const char *file, const char *name)
1074 {
1075         char sql_filter[1024];
1076         int before, after;
1077 
1078         if (file) {
1079                 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
1080                          file, name);
1081         } else {
1082                 snprintf(sql_filter, 1024, "function = '%s';", name);
1083         }
1084 
1085         before = ptr_list_size((struct ptr_list *)ptr_names);
1086 
1087         run_sql(get_ptr_name, NULL,
1088                 "select distinct ptr from function_ptr where %s",
1089                 sql_filter);
1090 
1091         after = ptr_list_size((struct ptr_list *)ptr_names);
1092         if (before == after)
1093                 return;
1094 
1095         while ((name = get_next_ptr_name()))
1096                 get_ptr_names(NULL, name);
1097 }
1098 
1099 static void match_data_from_db(struct symbol *sym)
1100 {
1101         struct select_caller_info_data data = { .prev_func_id = -1 };
1102         struct sm_state *sm;
1103         struct stree *stree;
1104         struct timeval end_time;
1105 
1106         if (!sym || !sym->ident)
1107                 return;
1108 
1109         gettimeofday(&data.start_time, NULL);
1110 
1111         __push_fake_cur_stree();
1112         __unnullify_path();
1113 
1114         if (!__inline_fn) {
1115                 char *ptr;
1116 
1117                 if (sym->ctype.modifiers & MOD_STATIC)
1118                         get_ptr_names(get_base_file(), sym->ident->name);
1119                 else
1120                         get_ptr_names(NULL, sym->ident->name);
1121 
1122                 if (ptr_list_size((struct ptr_list *)ptr_names) > 20) {
1123                         __free_ptr_list((struct ptr_list **)&ptr_names);
1124                         __free_ptr_list((struct ptr_list **)&ptr_names_done);
1125                         __free_fake_cur_stree();
1126                         return;
1127                 }
1128 
1129                 sql_select_caller_info(&data,
1130                                        "call_id, type, parameter, key, value",
1131                                        sym);
1132 
1133 
1134                 stree = __pop_fake_cur_stree();
1135                 if (!data.ignore)
1136                         merge_stree(&data.final_states, stree);
1137                 free_stree(&stree);
1138                 __push_fake_cur_stree();
1139                 __unnullify_path();
1140                 data.prev_func_id = -1;
1141                 data.ignore = 0;
1142                 data.results = 0;
1143 
1144                 FOR_EACH_PTR(ptr_names, ptr) {
1145                         run_sql(caller_info_callback, &data,
1146                                 "select call_id, type, parameter, key, value"
1147                                 " from common_caller_info where function = '%s' order by call_id",
1148                                 ptr);
1149                 } END_FOR_EACH_PTR(ptr);
1150 
1151                 if (data.results) {
1152                         FOR_EACH_PTR(ptr_names, ptr) {
1153                                 free_string(ptr);
1154                         } END_FOR_EACH_PTR(ptr);
1155                         goto free_ptr_names;
1156                 }
1157 
1158                 FOR_EACH_PTR(ptr_names, ptr) {
1159                         run_sql(caller_info_callback, &data,
1160                                 "select call_id, type, parameter, key, value"
1161                                 " from caller_info where function = '%s' order by call_id",
1162                                 ptr);
1163                         free_string(ptr);
1164                 } END_FOR_EACH_PTR(ptr);
1165 
1166 free_ptr_names:
1167                 __free_ptr_list((struct ptr_list **)&ptr_names);
1168                 __free_ptr_list((struct ptr_list **)&ptr_names_done);
1169         } else {
1170                 sql_select_caller_info(&data,
1171                                        "call_id, type, parameter, key, value",
1172                                        sym);
1173         }
1174 
1175         stree = __pop_fake_cur_stree();
1176         if (!data.ignore)
1177                 merge_stree(&data.final_states, stree);
1178         free_stree(&stree);
1179 
1180         gettimeofday(&end_time, NULL);
1181         if (end_time.tv_sec - data.start_time.tv_sec <= 10) {
1182                 FOR_EACH_SM(data.final_states, sm) {
1183                         __set_sm(sm);
1184                 } END_FOR_EACH_SM(sm);
1185         }
1186 
1187         free_stree(&data.final_states);
1188 }
1189 
1190 static int return_implies_callbacks(void *_info, int argc, char **argv, char **azColName)
1191 {
1192         struct implies_info *info = _info;
1193         struct db_implies_callback *cb;
1194         struct expression *arg = NULL;
1195         int type;
1196         int param;
1197 
1198         if (argc != 5)
1199                 return 0;
1200 
1201         type = atoi(argv[1]);
1202         param = atoi(argv[2]);
1203 
1204         FOR_EACH_PTR(info->cb_list, cb) {
1205                 if (cb->type != type)
1206                         continue;
1207                 if (param != -1) {
1208                         arg = get_argument_from_call_expr(info->expr->args, param);
1209                         if (!arg)
1210                                 continue;
1211                 }
1212                 cb->callback(info->expr, arg, argv[3], argv[4]);
1213         } END_FOR_EACH_PTR(cb);
1214 
1215         return 0;
1216 }
1217 
1218 static int call_implies_callbacks(void *_info, int argc, char **argv, char **azColName)
1219 {
1220         struct implies_info *info = _info;
1221         struct db_implies_callback *cb;
1222         struct expression *arg;
1223         struct symbol *sym;
1224         char *name;
1225         int type;
1226         int param;
1227 
1228         if (argc != 5)
1229                 return 0;
1230 
1231         type = atoi(argv[1]);
1232         param = atoi(argv[2]);
1233 
1234         if (!get_param(param, &name, &sym))
1235                 return 0;
1236         arg = symbol_expression(sym);
1237         if (!arg)
1238                 return 0;
1239 
1240         FOR_EACH_PTR(info->cb_list, cb) {
1241                 if (cb->type != type)
1242                         continue;
1243                 cb->callback(info->expr, arg, argv[3], argv[4]);
1244         } END_FOR_EACH_PTR(cb);
1245 
1246         return 0;
1247 }
1248 
1249 static void match_return_implies(struct expression *expr)
1250 {
1251         struct implies_info info = {
1252                 .type = RETURN_IMPLIES,
1253                 .cb_list = return_implies_cb_list,
1254         };
1255 
1256         if (expr->fn->type != EXPR_SYMBOL ||
1257             !expr->fn->symbol)
1258                 return;
1259         info.expr = expr;
1260         info.sym = expr->fn->symbol;
1261         sql_select_implies("function, type, parameter, key, value", &info,
1262                            return_implies_callbacks);
1263 }
1264 
1265 static void match_call_implies(struct symbol *sym)
1266 {
1267         struct implies_info info = {
1268                 .type = CALL_IMPLIES,
1269                 .cb_list = call_implies_cb_list,
1270         };
1271 
1272         if (!sym || !sym->ident)
1273                 return;
1274 
1275         info.sym = sym;
1276         sql_select_implies("function, type, parameter, key, value", &info,
1277                            call_implies_callbacks);
1278 }
1279 
1280 static char *get_fn_param_str(struct expression *expr)
1281 {
1282         struct expression *tmp;
1283         int param;
1284         char buf[32];
1285 
1286         tmp = get_assigned_expr(expr);
1287         if (tmp)
1288                 expr = tmp;
1289         expr = strip_expr(expr);
1290         if (!expr || expr->type != EXPR_CALL)
1291                 return NULL;
1292         expr = strip_expr(expr->fn);
1293         if (!expr || expr->type != EXPR_SYMBOL)
1294                 return NULL;
1295         param = get_param_num(expr);
1296         if (param < 0)
1297                 return NULL;
1298 
1299         snprintf(buf, sizeof(buf), "[r $%d]", param);
1300         return alloc_sname(buf);
1301 }
1302 
1303 static char *get_return_compare_is_param(struct expression *expr)
1304 {
1305         char *var;
1306         char buf[256];
1307         int comparison;
1308         int param;
1309 
1310         param = get_param_num(expr);
1311         if (param < 0)
1312                 return NULL;
1313 
1314         var = expr_to_var(expr);
1315         if (!var)
1316                 return NULL;
1317         snprintf(buf, sizeof(buf), "%s orig", var);
1318         comparison = get_comparison_strings(var, buf);
1319         free_string(var);
1320 
1321         if (!comparison)
1322                 return NULL;
1323 
1324         snprintf(buf, sizeof(buf), "[%s$%d]", show_special(comparison), param);
1325         return alloc_sname(buf);
1326 }
1327 
1328 static char *get_return_compare_str(struct expression *expr)
1329 {
1330         char *compare_str;
1331 
1332         compare_str = get_return_compare_is_param(expr);
1333         if (compare_str)
1334                 return compare_str;
1335 
1336         compare_str = expr_lte_to_param(expr, -1);
1337         if (compare_str)
1338                 return compare_str;
1339 
1340         return expr_param_comparison(expr, -1);
1341 }
1342 
1343 static const char *get_return_ranges_str(struct expression *expr, struct range_list **rl_p)
1344 {
1345         struct range_list *rl;
1346         char *return_ranges;
1347         sval_t sval;
1348         char *fn_param_str;
1349         char *compare_str;
1350         char *math_str;
1351         char buf[128];
1352 
1353         *rl_p = NULL;
1354 
1355         if (!expr)
1356                 return alloc_sname("");
1357 
1358         if (get_implied_value(expr, &sval)) {
1359                 sval = sval_cast(cur_func_return_type(), sval);
1360                 *rl_p = alloc_rl(sval, sval);
1361                 return sval_to_str_or_err_ptr(sval);
1362         }
1363 
1364         fn_param_str = get_fn_param_str(expr);
1365         compare_str = expr_equal_to_param(expr, -1);
1366         math_str = get_value_in_terms_of_parameter_math(expr);
1367 
1368         if (get_implied_rl(expr, &rl) && !is_whole_rl(rl)) {
1369                 rl = cast_rl(cur_func_return_type(), rl);
1370                 return_ranges = show_rl(rl);
1371         } else if (get_imaginary_absolute(expr, &rl)){
1372                 rl = cast_rl(cur_func_return_type(), rl);
1373                 return alloc_sname(show_rl(rl));
1374         } else {
1375                 get_absolute_rl(expr, &rl);
1376                 rl = cast_rl(cur_func_return_type(), rl);
1377                 return_ranges = show_rl(rl);
1378         }
1379         *rl_p = rl;
1380 
1381         if (fn_param_str) {
1382                 snprintf(buf, sizeof(buf), "%s%s", return_ranges, fn_param_str);
1383                 return alloc_sname(buf);
1384         }
1385         if (compare_str) {
1386                 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1387                 return alloc_sname(buf);
1388         }
1389         if (math_str) {
1390                 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, math_str);
1391                 return alloc_sname(buf);
1392         }
1393         compare_str = get_return_compare_str(expr);
1394         if (compare_str) {
1395                 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1396                 return alloc_sname(buf);
1397         }
1398 
1399         return return_ranges;
1400 }
1401 
1402 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
1403 {
1404         sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", function_signature());
1405 }
1406 
1407 static bool call_return_state_hooks_conditional(struct expression *expr)
1408 {
1409         int final_pass_orig = final_pass;
1410         static int recurse;
1411 
1412         if (recurse >= 2)
1413                 return false;
1414         if (!expr ||
1415             (expr->type != EXPR_CONDITIONAL && expr->type != EXPR_SELECT))
1416                 return false;
1417 
1418         recurse++;
1419 
1420         __push_fake_cur_stree();
1421 
1422         final_pass = 0;
1423         __split_whole_condition(expr->conditional);
1424         final_pass = final_pass_orig;
1425 
1426         call_return_state_hooks(expr->cond_true ?: expr->conditional);
1427 
1428         __push_true_states();
1429         __use_false_states();
1430 
1431         call_return_state_hooks(expr->cond_false);
1432 
1433         __merge_true_states();
1434         __free_fake_cur_stree();
1435 
1436         recurse--;
1437         return true;
1438 }
1439 
1440 static void call_return_state_hooks_compare(struct expression *expr)
1441 {
1442         struct returned_state_callback *cb;
1443         char *return_ranges;
1444         int final_pass_orig = final_pass;
1445         sval_t sval = { .type = &int_ctype };
1446         sval_t ret;
1447 
1448         if (!get_implied_value(expr, &ret))
1449                 ret.value = -1;
1450 
1451         __push_fake_cur_stree();
1452 
1453         final_pass = 0;
1454         __split_whole_condition(expr);
1455         final_pass = final_pass_orig;
1456 
1457         if (ret.value != 0) {
1458                 return_ranges = alloc_sname("1");
1459                 sval.value = 1;
1460                 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1461 
1462                 return_id++;
1463                 FOR_EACH_PTR(returned_state_callbacks, cb) {
1464                         cb->callback(return_id, return_ranges, expr);
1465                 } END_FOR_EACH_PTR(cb);
1466         }
1467 
1468         __push_true_states();
1469         __use_false_states();
1470 
1471         if (ret.value != 1) {
1472                 return_ranges = alloc_sname("0");
1473                 sval.value = 0;
1474                 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_sval(sval));
1475 
1476                 return_id++;
1477                 FOR_EACH_PTR(returned_state_callbacks, cb) {
1478                         cb->callback(return_id, return_ranges, expr);
1479                 } END_FOR_EACH_PTR(cb);
1480         }
1481 
1482         __merge_true_states();
1483         __free_fake_cur_stree();
1484 }
1485 
1486 static int ptr_in_list(struct sm_state *sm, struct state_list *slist)
1487 {
1488         struct sm_state *tmp;
1489 
1490         FOR_EACH_PTR(slist, tmp) {
1491                 if (strcmp(tmp->state->name, sm->state->name) == 0)
1492                         return 1;
1493         } END_FOR_EACH_PTR(tmp);
1494 
1495         return 0;
1496 }
1497 
1498 static int split_possible_helper(struct sm_state *sm, struct expression *expr)
1499 {
1500         struct returned_state_callback *cb;
1501         struct range_list *rl;
1502         char *return_ranges;
1503         struct sm_state *tmp;
1504         int ret = 0;
1505         int nr_possible, nr_states;
1506         char *compare_str;
1507         char buf[128];
1508         struct state_list *already_handled = NULL;
1509         sval_t sval;
1510 
1511         if (!sm || !sm->merged)
1512                 return 0;
1513 
1514         if (too_many_possible(sm))
1515                 return 0;
1516 
1517         /* bail if it gets too complicated */
1518         nr_possible = 0;
1519         FOR_EACH_PTR(sm->possible, tmp) {
1520                 if (tmp->merged)
1521                         continue;
1522                 if (ptr_in_list(tmp, already_handled))
1523                         continue;
1524                 add_ptr_list(&already_handled, tmp);
1525                 nr_possible++;
1526         } END_FOR_EACH_PTR(tmp);
1527         free_slist(&already_handled);
1528         nr_states = get_db_state_count();
1529         if (nr_states * nr_possible >= 2000)
1530                 return 0;
1531 
1532         FOR_EACH_PTR(sm->possible, tmp) {
1533                 if (tmp->merged)
1534                         continue;
1535                 if (ptr_in_list(tmp, already_handled))
1536                         continue;
1537                 add_ptr_list(&already_handled, tmp);
1538 
1539                 ret = 1;
1540                 __push_fake_cur_stree();
1541 
1542                 overwrite_states_using_pool(sm, tmp);
1543 
1544                 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
1545                 return_ranges = show_rl(rl);
1546                 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(clone_rl(rl)));
1547                 if (!rl_to_sval(rl, &sval)) {
1548                         compare_str = get_return_compare_str(expr);
1549                         if (compare_str) {
1550                                 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1551                                 return_ranges = alloc_sname(buf);
1552                         }
1553                 }
1554 
1555                 return_id++;
1556                 FOR_EACH_PTR(returned_state_callbacks, cb) {
1557                         cb->callback(return_id, return_ranges, expr);
1558                 } END_FOR_EACH_PTR(cb);
1559 
1560                 __free_fake_cur_stree();
1561         } END_FOR_EACH_PTR(tmp);
1562 
1563         free_slist(&already_handled);
1564 
1565         return ret;
1566 }
1567 
1568 static int call_return_state_hooks_split_possible(struct expression *expr)
1569 {
1570         struct sm_state *sm;
1571 
1572         if (!expr || expr_equal_to_param(expr, -1))
1573                 return 0;
1574 
1575         sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1576         return split_possible_helper(sm, expr);
1577 }
1578 
1579 static bool has_possible_negative(struct sm_state *sm)
1580 {
1581         struct sm_state *tmp;
1582 
1583         if (!type_signed(estate_type(sm->state)))
1584                 return false;
1585 
1586         FOR_EACH_PTR(sm->possible, tmp) {
1587                 if (!estate_rl(tmp->state))
1588                         continue;
1589                 if (sval_is_negative(estate_min(tmp->state)) &&
1590                     sval_is_negative(estate_max(tmp->state)))
1591                         return true;
1592         } END_FOR_EACH_PTR(tmp);
1593 
1594         return false;
1595 }
1596 
1597 static bool has_separate_zero_null(struct sm_state *sm)
1598 {
1599         struct sm_state *tmp;
1600         sval_t sval;
1601 
1602         FOR_EACH_PTR(sm->possible, tmp) {
1603                 if (!estate_get_single_value(tmp->state, &sval))
1604                         continue;
1605                 if (sval.value == 0)
1606                         return true;
1607         } END_FOR_EACH_PTR(tmp);
1608 
1609         return false;
1610 }
1611 
1612 static int split_positive_from_negative(struct expression *expr)
1613 {
1614         struct sm_state *sm;
1615         struct returned_state_callback *cb;
1616         struct range_list *rl;
1617         const char *return_ranges;
1618         struct range_list *ret_rl;
1619         bool separate_zero;
1620         int undo;
1621 
1622         /* We're going to print the states 3 times */
1623         if (get_db_state_count() > 10000 / 3)
1624                 return 0;
1625 
1626         if (!get_implied_rl(expr, &rl) || !rl)
1627                 return 0;
1628         /* Forget about INT_MAX and larger */
1629         if (rl_max(rl).value <= 0)
1630                 return 0;
1631         if (!sval_is_negative(rl_min(rl)))
1632                 return 0;
1633 
1634         sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1635         if (!sm)
1636                 return 0;
1637         if (!has_possible_negative(sm))
1638                 return 0;
1639         separate_zero = has_separate_zero_null(sm);
1640 
1641         if (!assume(compare_expression(expr, separate_zero ? '>' : SPECIAL_GTE, zero_expr())))
1642                 return 0;
1643 
1644         return_id++;
1645         return_ranges = get_return_ranges_str(expr, &ret_rl);
1646         set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1647         FOR_EACH_PTR(returned_state_callbacks, cb) {
1648                 cb->callback(return_id, (char *)return_ranges, expr);
1649         } END_FOR_EACH_PTR(cb);
1650 
1651         end_assume();
1652 
1653         if (separate_zero) {
1654                 undo = assume(compare_expression(expr, SPECIAL_EQUAL, zero_expr()));
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                 if (undo)
1664                         end_assume();
1665         }
1666 
1667         undo = assume(compare_expression(expr, '<', zero_expr()));
1668 
1669         return_id++;
1670         return_ranges = get_return_ranges_str(expr, &ret_rl);
1671         set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1672         FOR_EACH_PTR(returned_state_callbacks, cb) {
1673                 cb->callback(return_id, (char *)return_ranges, expr);
1674         } END_FOR_EACH_PTR(cb);
1675 
1676         if (undo)
1677                 end_assume();
1678 
1679         return 1;
1680 }
1681 
1682 static int call_return_state_hooks_split_null_non_null_zero(struct expression *expr)
1683 {
1684         struct returned_state_callback *cb;
1685         struct range_list *rl;
1686         struct range_list *nonnull_rl;
1687         sval_t null_sval;
1688         struct range_list *null_rl = NULL;
1689         char *return_ranges;
1690         struct sm_state *sm;
1691         struct smatch_state *state;
1692         int nr_states;
1693         int final_pass_orig = final_pass;
1694 
1695         if (!expr || expr_equal_to_param(expr, -1))
1696                 return 0;
1697         if (expr->type == EXPR_CALL)
1698                 return 0;
1699 
1700         sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1701         if (!sm)
1702                 return 0;
1703         if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
1704                 return 0;
1705         state = sm->state;
1706         if (!estate_rl(state))
1707                 return 0;
1708         if (estate_min(state).value == 0 && estate_max(state).value == 0)
1709                 return 0;
1710         if (!has_separate_zero_null(sm))
1711                 return 0;
1712 
1713         nr_states = get_db_state_count();
1714         if (option_info && nr_states >= 1500)
1715                 return 0;
1716 
1717         rl = estate_rl(state);
1718 
1719         __push_fake_cur_stree();
1720 
1721         final_pass = 0;
1722         __split_whole_condition(expr);
1723         final_pass = final_pass_orig;
1724 
1725         nonnull_rl = rl_filter(rl, rl_zero());
1726         return_ranges = show_rl(nonnull_rl);
1727         set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonnull_rl));
1728 
1729         return_id++;
1730         FOR_EACH_PTR(returned_state_callbacks, cb) {
1731                 cb->callback(return_id, return_ranges, expr);
1732         } END_FOR_EACH_PTR(cb);
1733 
1734         __push_true_states();
1735         __use_false_states();
1736 
1737         return_ranges = alloc_sname("0");
1738         null_sval = sval_type_val(rl_type(rl), 0);
1739         add_range(&null_rl, null_sval, null_sval);
1740         set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(null_rl));
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         __merge_true_states();
1747         __free_fake_cur_stree();
1748 
1749         return 1;
1750 }
1751 
1752 static bool is_kernel_success_fail(struct sm_state *sm)
1753 {
1754         struct sm_state *tmp;
1755         struct range_list *rl;
1756         bool has_zero = false;
1757         bool has_neg = false;
1758 
1759         if (!type_signed(estate_type(sm->state)))
1760                 return false;
1761 
1762         FOR_EACH_PTR(sm->possible, tmp) {
1763                 rl = estate_rl(tmp->state);
1764                 if (!rl)
1765                         return false;
1766                 if (rl_min(rl).value == 0 && rl_max(rl).value == 0) {
1767                         has_zero = true;
1768                         continue;
1769                 }
1770                 has_neg = true;
1771                 if (rl_min(rl).value >= -4095 && rl_max(rl).value < 0)
1772                         continue;
1773                 if (strcmp(tmp->state->name, "s32min-(-1)") == 0)
1774                         continue;
1775                 if (strcmp(tmp->state->name, "s32min-(-1),1-s32max") == 0)
1776                         continue;
1777                 return false;
1778         } END_FOR_EACH_PTR(tmp);
1779 
1780         return has_zero && has_neg;
1781 }
1782 
1783 static int call_return_state_hooks_split_success_fail(struct expression *expr)
1784 {
1785         struct sm_state *sm;
1786         struct range_list *rl;
1787         struct range_list *nonzero_rl;
1788         sval_t zero_sval;
1789         struct range_list *zero_rl = NULL;
1790         int nr_states;
1791         struct returned_state_callback *cb;
1792         char *return_ranges;
1793         int final_pass_orig = final_pass;
1794 
1795         if (option_project != PROJ_KERNEL)
1796                 return 0;
1797 
1798         nr_states = get_db_state_count();
1799         if (nr_states > 2000)
1800                 return 0;
1801 
1802         sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1803         if (!sm)
1804                 return 0;
1805         if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
1806                 return 0;
1807         if (!is_kernel_success_fail(sm))
1808                 return 0;
1809 
1810         rl = estate_rl(sm->state);
1811         if (!rl)
1812                 return 0;
1813 
1814         __push_fake_cur_stree();
1815 
1816         final_pass = 0;
1817         __split_whole_condition(expr);
1818         final_pass = final_pass_orig;
1819 
1820         nonzero_rl = rl_filter(rl, rl_zero());
1821         nonzero_rl = cast_rl(cur_func_return_type(), nonzero_rl);
1822         return_ranges = show_rl(nonzero_rl);
1823         set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonzero_rl));
1824 
1825         return_id++;
1826         FOR_EACH_PTR(returned_state_callbacks, cb) {
1827                 cb->callback(return_id, return_ranges, expr);
1828         } END_FOR_EACH_PTR(cb);
1829 
1830         __push_true_states();
1831         __use_false_states();
1832 
1833         return_ranges = alloc_sname("0");
1834         zero_sval = sval_type_val(rl_type(rl), 0);
1835         add_range(&zero_rl, zero_sval, zero_sval);
1836         set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(zero_rl));
1837         return_id++;
1838         FOR_EACH_PTR(returned_state_callbacks, cb) {
1839                 cb->callback(return_id, return_ranges, expr);
1840         } END_FOR_EACH_PTR(cb);
1841 
1842         __merge_true_states();
1843         __free_fake_cur_stree();
1844 
1845         return 1;
1846 }
1847 
1848 static int is_boolean(struct expression *expr)
1849 {
1850         struct range_list *rl;
1851 
1852         if (!get_implied_rl(expr, &rl))
1853                 return 0;
1854         if (rl_min(rl).value == 0 && rl_max(rl).value == 1)
1855                 return 1;
1856         return 0;
1857 }
1858 
1859 static int splitable_function_call(struct expression *expr)
1860 {
1861         struct sm_state *sm;
1862         char buf[64];
1863 
1864         if (!expr || expr->type != EXPR_CALL)
1865                 return 0;
1866         snprintf(buf, sizeof(buf), "return %p", expr);
1867         sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
1868         return split_possible_helper(sm, expr);
1869 }
1870 
1871 static struct sm_state *find_bool_param(void)
1872 {
1873         struct stree *start_states;
1874         struct symbol *arg;
1875         struct sm_state *sm, *tmp;
1876         sval_t sval;
1877 
1878         start_states = get_start_states();
1879 
1880         FOR_EACH_PTR_REVERSE(cur_func_sym->ctype.base_type->arguments, arg) {
1881                 if (!arg->ident)
1882                         continue;
1883                 sm = get_sm_state_stree(start_states, SMATCH_EXTRA, arg->ident->name, arg);
1884                 if (!sm)
1885                         continue;
1886                 if (rl_min(estate_rl(sm->state)).value != 0 ||
1887                     rl_max(estate_rl(sm->state)).value != 1)
1888                         continue;
1889                 goto found;
1890         } END_FOR_EACH_PTR_REVERSE(arg);
1891 
1892         return NULL;
1893 
1894 found:
1895         /*
1896          * Check if it's splitable.  If not, then splitting it up is likely not
1897          * useful for the callers.
1898          */
1899         FOR_EACH_PTR(sm->possible, tmp) {
1900                 if (is_merged(tmp))
1901                         continue;
1902                 if (!estate_get_single_value(tmp->state, &sval))
1903                         return NULL;
1904         } END_FOR_EACH_PTR(tmp);
1905 
1906         return sm;
1907 }
1908 
1909 static int split_on_bool_sm(struct sm_state *sm, struct expression *expr)
1910 {
1911         struct returned_state_callback *cb;
1912         struct range_list *ret_rl;
1913         const char *return_ranges;
1914         struct sm_state *tmp;
1915         int ret = 0;
1916         struct state_list *already_handled = NULL;
1917 
1918         if (!sm || !sm->merged)
1919                 return 0;
1920 
1921         if (too_many_possible(sm))
1922                 return 0;
1923 
1924         FOR_EACH_PTR(sm->possible, tmp) {
1925                 if (tmp->merged)
1926                         continue;
1927                 if (ptr_in_list(tmp, already_handled))
1928                         continue;
1929                 add_ptr_list(&already_handled, tmp);
1930 
1931                 ret = 1;
1932                 __push_fake_cur_stree();
1933 
1934                 overwrite_states_using_pool(sm, tmp);
1935 
1936                 return_ranges = get_return_ranges_str(expr, &ret_rl);
1937                 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1938                 return_id++;
1939                 FOR_EACH_PTR(returned_state_callbacks, cb) {
1940                         cb->callback(return_id, (char *)return_ranges, expr);
1941                 } END_FOR_EACH_PTR(cb);
1942 
1943                 __free_fake_cur_stree();
1944         } END_FOR_EACH_PTR(tmp);
1945 
1946         free_slist(&already_handled);
1947 
1948         return ret;
1949 }
1950 
1951 static int split_by_bool_param(struct expression *expr)
1952 {
1953         struct sm_state *start_sm, *sm;
1954         sval_t sval;
1955 
1956         start_sm = find_bool_param();
1957         if (!start_sm)
1958                 return 0;
1959         sm = get_sm_state(SMATCH_EXTRA, start_sm->name, start_sm->sym);
1960         if (!sm || estate_get_single_value(sm->state, &sval))
1961                 return 0;
1962 
1963         if (get_db_state_count() * 2 >= 2000)
1964                 return 0;
1965 
1966         return split_on_bool_sm(sm, expr);
1967 }
1968 
1969 static int split_by_null_nonnull_param(struct expression *expr)
1970 {
1971         struct symbol *arg;
1972         struct sm_state *sm;
1973         int nr_possible;
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 (!has_separate_zero_null(sm))
1991                 return 0;
1992 
1993         nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
1994         if (get_db_state_count() * nr_possible >= 2000)
1995                 return 0;
1996 
1997         return split_on_bool_sm(sm, expr);
1998 }
1999 
2000 struct expression *strip_expr_statement(struct expression *expr)
2001 {
2002         struct expression *orig = expr;
2003         struct statement *stmt, *last_stmt;
2004 
2005         if (!expr)
2006                 return NULL;
2007         if (expr->type == EXPR_PREOP && expr->op == '(')
2008                 expr = expr->unop;
2009         if (expr->type != EXPR_STATEMENT)
2010                 return orig;
2011         stmt = expr->statement;
2012         if (!stmt || stmt->type != STMT_COMPOUND)
2013                 return orig;
2014 
2015         last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
2016         if (!last_stmt || last_stmt->type == STMT_LABEL)
2017                 last_stmt = last_stmt->label_statement;
2018         if (!last_stmt || last_stmt->type != STMT_EXPRESSION)
2019                 return orig;
2020         return strip_expr(last_stmt->expression);
2021 }
2022 
2023 static void call_return_state_hooks(struct expression *expr)
2024 {
2025         struct returned_state_callback *cb;
2026         struct range_list *ret_rl;
2027         const char *return_ranges;
2028         int nr_states;
2029         sval_t sval;
2030 
2031         if (__path_is_null())
2032                 return;
2033 
2034         expr = strip_expr(expr);
2035         expr = strip_expr_statement(expr);
2036 
2037         if (is_impossible_path())
2038                 goto vanilla;
2039 
2040         if (expr && (expr->type == EXPR_COMPARE ||
2041                      !get_implied_value(expr, &sval)) &&
2042             (is_condition(expr) || is_boolean(expr))) {
2043                 call_return_state_hooks_compare(expr);
2044                 return;
2045         } else if (call_return_state_hooks_conditional(expr)) {
2046                 return;
2047         } else if (call_return_state_hooks_split_possible(expr)) {
2048                 return;
2049         } else if (split_positive_from_negative(expr)) {
2050                 return;
2051         } else if (call_return_state_hooks_split_null_non_null_zero(expr)) {
2052                 return;
2053         } else if (call_return_state_hooks_split_success_fail(expr)) {
2054                 return;
2055         } else if (splitable_function_call(expr)) {
2056                 return;
2057         } else if (split_by_bool_param(expr)) {
2058         } else if (split_by_null_nonnull_param(expr)) {
2059                 return;
2060         }
2061 
2062 vanilla:
2063         return_ranges = get_return_ranges_str(expr, &ret_rl);
2064         set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
2065 
2066         return_id++;
2067         nr_states = get_db_state_count();
2068         if (nr_states >= 10000) {
2069                 match_return_info(return_id, (char *)return_ranges, expr);
2070                 print_limited_param_set(return_id, (char *)return_ranges, expr);
2071                 mark_all_params_untracked(return_id, (char *)return_ranges, expr);
2072                 return;
2073         }
2074         FOR_EACH_PTR(returned_state_callbacks, cb) {
2075                 cb->callback(return_id, (char *)return_ranges, expr);
2076         } END_FOR_EACH_PTR(cb);
2077 }
2078 
2079 static void print_returned_struct_members(int return_id, char *return_ranges, struct expression *expr)
2080 {
2081         struct returned_member_callback *cb;
2082         struct stree *stree;
2083         struct sm_state *sm;
2084         struct symbol *type;
2085         char *name;
2086         char member_name[256];
2087         int len;
2088 
2089         type = get_type(expr);
2090         if (!type || type->type != SYM_PTR)
2091                 return;
2092         name = expr_to_var(expr);
2093         if (!name)
2094                 return;
2095 
2096         member_name[sizeof(member_name) - 1] = '\0';
2097         strcpy(member_name, "$");
2098 
2099         len = strlen(name);
2100         FOR_EACH_PTR(returned_member_callbacks, cb) {
2101                 stree = __get_cur_stree();
2102                 FOR_EACH_MY_SM(cb->owner, stree, sm) {
2103                         if (sm->name[0] == '*' && strcmp(sm->name + 1, name) == 0) {
2104                                 strcpy(member_name, "*$");
2105                                 cb->callback(return_id, return_ranges, expr, member_name, sm->state);
2106                                 continue;
2107                         }
2108                         if (strncmp(sm->name, name, len) != 0)
2109                                 continue;
2110                         if (strncmp(sm->name + len, "->", 2) != 0)
2111                                 continue;
2112                         snprintf(member_name, sizeof(member_name), "$%s", sm->name + len);
2113                         cb->callback(return_id, return_ranges, expr, member_name, sm->state);
2114                 } END_FOR_EACH_SM(sm);
2115         } END_FOR_EACH_PTR(cb);
2116 
2117         free_string(name);
2118 }
2119 
2120 static void reset_memdb(struct symbol *sym)
2121 {
2122         mem_sql(NULL, NULL, "delete from caller_info;");
2123         mem_sql(NULL, NULL, "delete from return_states;");
2124         mem_sql(NULL, NULL, "delete from call_implies;");
2125         mem_sql(NULL, NULL, "delete from return_implies;");
2126 }
2127 
2128 static void match_end_func_info(struct symbol *sym)
2129 {
2130         if (__path_is_null())
2131                 return;
2132         call_return_state_hooks(NULL);
2133 }
2134 
2135 static void match_after_func(struct symbol *sym)
2136 {
2137         if (!__inline_fn)
2138                 reset_memdb(sym);
2139 }
2140 
2141 static void init_memdb(void)
2142 {
2143         char *err = NULL;
2144         int rc;
2145         const char *schema_files[] = {
2146                 "db/db.schema",
2147                 "db/caller_info.schema",
2148                 "db/common_caller_info.schema",
2149                 "db/return_states.schema",
2150                 "db/function_type_size.schema",
2151                 "db/type_size.schema",
2152                 "db/function_type_info.schema",
2153                 "db/type_info.schema",
2154                 "db/call_implies.schema",
2155                 "db/return_implies.schema",
2156                 "db/function_ptr.schema",
2157                 "db/local_values.schema",
2158                 "db/function_type_value.schema",
2159                 "db/type_value.schema",
2160                 "db/function_type.schema",
2161                 "db/data_info.schema",
2162                 "db/parameter_name.schema",
2163                 "db/constraints.schema",
2164                 "db/constraints_required.schema",
2165                 "db/fn_ptr_data_link.schema",
2166                 "db/fn_data_link.schema",
2167                 "db/mtag_about.schema",
2168                 "db/mtag_map.schema",
2169                 "db/mtag_data.schema",
2170                 "db/mtag_alias.schema",
2171         };
2172         static char buf[4096];
2173         int fd;
2174         int ret;
2175         int i;
2176 
2177         rc = sqlite3_open(":memory:", &mem_db);
2178         if (rc != SQLITE_OK) {
2179                 sm_ierror("starting In-Memory database.");
2180                 return;
2181         }
2182 
2183         for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
2184                 fd = open_schema_file(schema_files[i]);
2185                 if (fd < 0)
2186                         continue;
2187                 ret = read(fd, buf, sizeof(buf));
2188                 if (ret < 0) {
2189                         sm_ierror("failed to read: %s", schema_files[i]);
2190                         continue;
2191                 }
2192                 close(fd);
2193                 if (ret == sizeof(buf)) {
2194                         sm_ierror("Schema file too large:  %s (limit %zd bytes)",
2195                                schema_files[i], sizeof(buf));
2196                         continue;
2197                 }
2198                 buf[ret] = '\0';
2199                 rc = sqlite3_exec(mem_db, buf, NULL, NULL, &err);
2200                 if (rc != SQLITE_OK) {
2201                         sm_ierror("SQL error #2: %s", err);
2202                         sm_ierror("%s", buf);
2203                 }
2204         }
2205 }
2206 
2207 static void init_cachedb(void)
2208 {
2209         char *err = NULL;
2210         int rc;
2211         const char *schema_files[] = {
2212                 "db/call_implies.schema",
2213                 "db/return_implies.schema",
2214                 "db/type_info.schema",
2215                 "db/mtag_data.schema",
2216                 "db/sink_info.schema",
2217         };
2218         static char buf[4096];
2219         int fd;
2220         int ret;
2221         int i;
2222 
2223         rc = sqlite3_open(":memory:", &cache_db);
2224         if (rc != SQLITE_OK) {
2225                 sm_ierror("starting In-Memory database.");
2226                 return;
2227         }
2228 
2229         for (i = 0; i < ARRAY_SIZE(schema_files); i++) {
2230                 fd = open_schema_file(schema_files[i]);
2231                 if (fd < 0)
2232                         continue;
2233                 ret = read(fd, buf, sizeof(buf));
2234                 if (ret < 0) {
2235                         sm_ierror("failed to read: %s", schema_files[i]);
2236                         continue;
2237                 }
2238                 close(fd);
2239                 if (ret == sizeof(buf)) {
2240                         sm_ierror("Schema file too large:  %s (limit %zd bytes)",
2241                                schema_files[i], sizeof(buf));
2242                         continue;
2243                 }
2244                 buf[ret] = '\0';
2245                 rc = sqlite3_exec(cache_db, buf, NULL, NULL, &err);
2246                 if (rc != SQLITE_OK) {
2247                         sm_ierror("SQL error #2: %s", err);
2248                         sm_ierror("%s", buf);
2249                 }
2250         }
2251 }
2252 
2253 static int save_cache_data(void *_table, int argc, char **argv, char **azColName)
2254 {
2255         static char buf[4096];
2256         char tmp[256];
2257         char *p = buf;
2258         char *table = _table;
2259         int i;
2260 
2261 
2262         p += snprintf(p, 4096 - (p - buf), "insert or ignore into %s values (", table);
2263         for (i = 0; i < argc; i++) {
2264                 if (i)
2265                         p += snprintf(p, 4096 - (p - buf), ", ");
2266                 sqlite3_snprintf(sizeof(tmp), tmp, "%q", escape_newlines(argv[i]));
2267                 p += snprintf(p, 4096 - (p - buf), "'%s'", tmp);
2268 
2269         }
2270         p += snprintf(p, 4096 - (p - buf), ");");
2271         if (p - buf > 4096)
2272                 return 0;
2273 
2274         sm_msg("SQL: %s", buf);
2275         return 0;
2276 }
2277 
2278 static void dump_cache(struct symbol_list *sym_list)
2279 {
2280         if (!option_info)
2281                 return;
2282         cache_sql(&save_cache_data, (char *)"type_info", "select * from type_info;");
2283         cache_sql(&save_cache_data, (char *)"return_implies", "select * from return_implies;");
2284         cache_sql(&save_cache_data, (char *)"call_implies", "select * from call_implies;");
2285         cache_sql(&save_cache_data, (char *)"mtag_data", "select * from mtag_data;");
2286         cache_sql(&save_cache_data, (char *)"sink_info", "select * from sink_info;");
2287 }
2288 
2289 void open_smatch_db(char *db_file)
2290 {
2291         int rc;
2292 
2293         if (option_no_db)
2294                 return;
2295 
2296         use_states = malloc(num_checks + 1);
2297         memset(use_states, 0xff, num_checks + 1);
2298 
2299         init_memdb();
2300         init_cachedb();
2301 
2302         rc = sqlite3_open_v2(db_file, &smatch_db, SQLITE_OPEN_READONLY, NULL);
2303         if (rc != SQLITE_OK) {
2304                 option_no_db = 1;
2305                 return;
2306         }
2307         run_sql(NULL, NULL,
2308                 "PRAGMA cache_size = %d;", SQLITE_CACHE_PAGES);
2309         return;
2310 }
2311 
2312 static void register_common_funcs(void)
2313 {
2314         struct token *token;
2315         char *func;
2316         char filename[256];
2317 
2318         if (option_project == PROJ_NONE)
2319                 strcpy(filename, "common_functions");
2320         else
2321                 snprintf(filename, 256, "%s.common_functions", option_project_str);
2322 
2323         token = get_tokens_file(filename);
2324         if (!token)
2325                 return;
2326         if (token_type(token) != TOKEN_STREAMBEGIN)
2327                 return;
2328         token = token->next;
2329         while (token_type(token) != TOKEN_STREAMEND) {
2330                 if (token_type(token) != TOKEN_IDENT)
2331                         return;
2332                 func = alloc_string(show_ident(token->ident));
2333                 add_ptr_list(&common_funcs, func);
2334                 token = token->next;
2335         }
2336         clear_token_alloc();
2337 }
2338 
2339 static char *get_next_string(char **str)
2340 {
2341         static char string[256];
2342         char *start;
2343         char *p = *str;
2344         int len, i, j;
2345 
2346         if (*p == '\0')
2347                 return NULL;
2348         start = p;
2349 
2350         while (*p != '\0' && *p != '\n') {
2351                 if (*p == '\\' && *(p + 1) == ' ') {
2352                         p += 2;
2353                         continue;
2354                 }
2355                 if (*p == ' ')
2356                         break;
2357                 p++;
2358         }
2359 
2360         len = p - start;
2361         if (len >= sizeof(string)) {
2362                 memcpy(string, start, sizeof(string));
2363                 string[sizeof(string) - 1] = '\0';
2364                 sm_ierror("return_fix: '%s' too long", string);
2365                 **str = '\0';
2366                 return NULL;
2367         }
2368         memcpy(string, start, len);
2369         string[len] = '\0';
2370         for (i = 0; i < sizeof(string) - 1; i++) {
2371                 if (string[i] == '\\' && string[i + 1] == ' ') {
2372                         for (j = i; string[j] != '\0'; j++)
2373                                 string[j] = string[j + 1];
2374                 }
2375         }
2376         if (*p != '\0')
2377                 p++;
2378         *str = p;
2379         return string;
2380 }
2381 
2382 static void register_return_replacements(void)
2383 {
2384         char *func, *orig, *new;
2385         char filename[256];
2386         char buf[4096];
2387         int fd, ret, i;
2388         char *p;
2389 
2390         snprintf(filename, 256, "db/%s.return_fixes", option_project_str);
2391         fd = open_schema_file(filename);
2392         if (fd < 0)
2393                 return;
2394         ret = read(fd, buf, sizeof(buf));
2395         close(fd);
2396         if (ret < 0)
2397                 return;
2398         if (ret == sizeof(buf)) {
2399                 sm_ierror("file too large:  %s (limit %zd bytes)",
2400                        filename, sizeof(buf));
2401                 return;
2402         }
2403         buf[ret] = '\0';
2404 
2405         p = buf;
2406         while (*p) {
2407                 get_next_string(&p);
2408                 replace_count++;
2409         }
2410         if (replace_count == 0 || replace_count % 3 != 0) {
2411                 replace_count = 0;
2412                 return;
2413         }
2414         replace_table = malloc(replace_count * sizeof(char *));
2415 
2416         p = buf;
2417         i = 0;
2418         while (*p) {
2419                 func = alloc_string(get_next_string(&p));
2420                 orig = alloc_string(get_next_string(&p));
2421                 new  = alloc_string(get_next_string(&p));
2422 
2423                 replace_table[i++] = func;
2424                 replace_table[i++] = orig;
2425                 replace_table[i++] = new;
2426         }
2427 }
2428 
2429 void register_definition_db_callbacks(int id)
2430 {
2431         add_hook(&match_call_info, FUNCTION_CALL_HOOK);
2432         add_split_return_callback(match_return_info);
2433         add_split_return_callback(print_returned_struct_members);
2434         add_hook(&call_return_state_hooks, RETURN_HOOK);
2435         add_hook(&match_end_func_info, END_FUNC_HOOK);
2436         add_hook(&match_after_func, AFTER_FUNC_HOOK);
2437 
2438         add_hook(&match_data_from_db, FUNC_DEF_HOOK);
2439         add_hook(&match_call_implies, FUNC_DEF_HOOK);
2440         add_hook(&match_return_implies, CALL_HOOK_AFTER_INLINE);
2441 
2442         register_common_funcs();
2443         register_return_replacements();
2444 
2445         add_hook(&dump_cache, END_FILE_HOOK);
2446 }
2447 
2448 void register_db_call_marker(int id)
2449 {
2450         add_hook(&match_call_marker, FUNCTION_CALL_HOOK);
2451 }
2452 
2453 char *return_state_to_var_sym(struct expression *expr, int param, const char *key, struct symbol **sym)
2454 {
2455         struct expression *arg;
2456         char *name = NULL;
2457         char member_name[256];
2458 
2459         *sym = NULL;
2460 
2461         if (param == -1) {
2462                 const char *star = "";
2463 
2464                 if (expr->type != EXPR_ASSIGNMENT)
2465                         return NULL;
2466                 if (get_type(expr->left) == &int_ctype && strcmp(key, "$") != 0)
2467                         return NULL;
2468                 name = expr_to_var_sym(expr->left, sym);
2469                 if (!name)
2470                         return NULL;
2471                 if (key[0] == '*') {
2472                         star = "*";
2473                         key++;
2474                 }
2475                 if (strncmp(key, "$", 1) != 0)
2476                         return name;
2477                 snprintf(member_name, sizeof(member_name), "%s%s%s", star, name, key + 1);
2478                 free_string(name);
2479                 return alloc_string(member_name);
2480         }
2481 
2482         while (expr->type == EXPR_ASSIGNMENT)
2483                 expr = strip_expr(expr->right);
2484         if (expr->type != EXPR_CALL)
2485                 return NULL;
2486 
2487         arg = get_argument_from_call_expr(expr->args, param);
2488         if (!arg)
2489                 return NULL;
2490 
2491         return get_variable_from_key(arg, key, sym);
2492 }
2493 
2494 char *get_variable_from_key(struct expression *arg, const char *key, struct symbol **sym)
2495 {
2496         char buf[256];
2497         char *tmp;
2498         int star_cnt = 0;
2499 
2500         if (!arg)
2501                 return NULL;
2502 
2503         arg = strip_expr(arg);
2504 
2505         if (strcmp(key, "$") == 0)
2506                 return expr_to_var_sym(arg, sym);
2507 
2508         if (strcmp(key, "*$") == 0) {
2509                 if (arg->type == EXPR_PREOP && arg->op == '&') {
2510                         arg = strip_expr(arg->unop);
2511                         return expr_to_var_sym(arg, sym);
2512                 } else {
2513                         tmp = expr_to_var_sym(arg, sym);
2514                         if (!tmp)
2515                                 return NULL;
2516                         snprintf(buf, sizeof(buf), "*%s", tmp);
2517                         free_string(tmp);
2518                         return alloc_string(buf);
2519                 }
2520         }
2521 
2522         while (key[0] == '*') {
2523                 star_cnt++;
2524                 key++;
2525         }
2526 
2527         if (arg->type == EXPR_PREOP && arg->op == '&' && star_cnt) {
2528                 arg = strip_expr(arg->unop);
2529                 star_cnt--;
2530         }
2531 
2532         if (arg->type == EXPR_PREOP && arg->op == '&') {
2533                 arg = strip_expr(arg->unop);
2534                 tmp = expr_to_var_sym(arg, sym);
2535                 if (!tmp)
2536                         return NULL;
2537                 snprintf(buf, sizeof(buf), "%.*s%s.%s",
2538                          star_cnt, "**********", tmp, key + 3);
2539                 return alloc_string(buf);
2540         }
2541 
2542         tmp = expr_to_var_sym(arg, sym);
2543         if (!tmp)
2544                 return NULL;
2545         snprintf(buf, sizeof(buf), "%.*s%s%s", star_cnt, "**********", tmp, key + 1);
2546         free_string(tmp);
2547         return alloc_string(buf);
2548 }
2549 
2550 char *get_chunk_from_key(struct expression *arg, char *key, struct symbol **sym, struct var_sym_list **vsl)
2551 {
2552         *vsl = NULL;
2553 
2554         if (strcmp("$", key) == 0)
2555                 return expr_to_chunk_sym_vsl(arg, sym, vsl);
2556         return get_variable_from_key(arg, key, sym);
2557 }
2558 
2559 const char *state_name_to_param_name(const char *state_name, const char *param_name)
2560 {
2561         int star_cnt = 0;
2562         int name_len;
2563         char buf[256];
2564 
2565         name_len = strlen(param_name);
2566 
2567         while (state_name[0] == '*') {
2568                 star_cnt++;
2569                 state_name++;
2570         }
2571 
2572         /* ten out of ten stars! */
2573         if (star_cnt > 10)
2574                 return NULL;
2575 
2576         if (strcmp(state_name, param_name) == 0) {
2577                 snprintf(buf, sizeof(buf), "%.*s$", star_cnt, "**********");
2578                 return alloc_sname(buf);
2579         }
2580 
2581         if (state_name[name_len] == '-' && /* check for '-' from "->" */
2582             strncmp(state_name, param_name, name_len) == 0) {
2583                 snprintf(buf, sizeof(buf), "%.*s$%s", star_cnt, "**********", state_name + name_len);
2584                 return alloc_sname(buf);
2585         }
2586         return NULL;
2587 }
2588 
2589 const char *get_param_name_var_sym(const char *name, struct symbol *sym)
2590 {
2591         if (!sym || !sym->ident)
2592                 return NULL;
2593 
2594         return state_name_to_param_name(name, sym->ident->name);
2595 }
2596 
2597 const char *get_mtag_name_var_sym(const char *state_name, struct symbol *sym)
2598 {
2599         struct symbol *type;
2600         const char *sym_name;
2601         int name_len;
2602         static char buf[256];
2603 
2604         /*
2605          * mtag_name is different from param_name because mtags can be a struct
2606          * instead of a struct pointer.  But we want to treat it like a pointer
2607          * because really an mtag is a pointer.  Or in other words, if you pass
2608          * a struct foo then you want to talk about foo.bar but with an mtag
2609          * you want to refer to it as foo->bar.
2610          *
2611          */
2612 
2613         if (!sym || !sym->ident)
2614                 return NULL;
2615 
2616         type = get_real_base_type(sym);
2617         if (type && type->type == SYM_BASETYPE)
2618                 return "*$";
2619 
2620         sym_name = sym->ident->name;
2621         name_len = strlen(sym_name);
2622 
2623         if (state_name[name_len] == '.' && /* check for '-' from "->" */
2624             strncmp(state_name, sym_name, name_len) == 0) {
2625                 snprintf(buf, sizeof(buf), "$->%s", state_name + name_len + 1);
2626                 return buf;
2627         }
2628 
2629         return state_name_to_param_name(state_name, sym_name);
2630 }
2631 
2632 const char *get_mtag_name_expr(struct expression *expr)
2633 {
2634         char *name;
2635         struct symbol *sym;
2636         const char *ret = NULL;
2637 
2638         name = expr_to_var_sym(expr, &sym);
2639         if (!name || !sym)
2640                 goto free;
2641 
2642         ret = get_mtag_name_var_sym(name, sym);
2643 free:
2644         free_string(name);
2645         return ret;
2646 }
2647 
2648 const char *get_param_name(struct sm_state *sm)
2649 {
2650         return get_param_name_var_sym(sm->name, sm->sym);
2651 }
2652 
2653 char *get_data_info_name(struct expression *expr)
2654 {
2655         struct symbol *sym;
2656         char *name;
2657         char buf[256];
2658         char *ret = NULL;
2659 
2660         expr = strip_expr(expr);
2661         name = get_member_name(expr);
2662         if (name)
2663                 return name;
2664         name = expr_to_var_sym(expr, &sym);
2665         if (!name || !sym)
2666                 goto free;
2667         if (!(sym->ctype.modifiers & MOD_TOPLEVEL))
2668                 goto free;
2669         if (sym->ctype.modifiers & MOD_STATIC)
2670                 snprintf(buf, sizeof(buf), "static %s", name);
2671         else
2672                 snprintf(buf, sizeof(buf), "global %s", name);
2673         ret = alloc_sname(buf);
2674 free:
2675         free_string(name);
2676         return ret;
2677 }