11f5207b7SJohn Levon /* 21f5207b7SJohn Levon * Copyright (C) 2006 Dan Carpenter. 31f5207b7SJohn Levon * 41f5207b7SJohn Levon * This program is free software; you can redistribute it and/or 51f5207b7SJohn Levon * modify it under the terms of the GNU General Public License 61f5207b7SJohn Levon * as published by the Free Software Foundation; either version 2 71f5207b7SJohn Levon * of the License, or (at your option) any later version. 81f5207b7SJohn Levon * 91f5207b7SJohn Levon * This program is distributed in the hope that it will be useful, 101f5207b7SJohn Levon * but WITHOUT ANY WARRANTY; without even the implied warranty of 111f5207b7SJohn Levon * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 121f5207b7SJohn Levon * GNU General Public License for more details. 131f5207b7SJohn Levon * 141f5207b7SJohn Levon * You should have received a copy of the GNU General Public License 151f5207b7SJohn Levon * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt 167ae7577cSJohn Levon * 177ae7577cSJohn Levon * Copyright 2019 Joyent, Inc. 181f5207b7SJohn Levon */ 191f5207b7SJohn Levon 201f5207b7SJohn Levon #ifndef SMATCH_H_ 211f5207b7SJohn Levon # define SMATCH_H_ 221f5207b7SJohn Levon 231f5207b7SJohn Levon #include <stdio.h> 241f5207b7SJohn Levon #include <string.h> 251f5207b7SJohn Levon #include <limits.h> 261f5207b7SJohn Levon #include <sys/time.h> 271f5207b7SJohn Levon #include <sqlite3.h> 281f5207b7SJohn Levon #include "lib.h" 291f5207b7SJohn Levon #include "allocate.h" 301f5207b7SJohn Levon #include "scope.h" 311f5207b7SJohn Levon #include "parse.h" 321f5207b7SJohn Levon #include "expression.h" 331f5207b7SJohn Levon #include "avl.h" 341f5207b7SJohn Levon 351f5207b7SJohn Levon typedef struct { 361f5207b7SJohn Levon struct symbol *type; 371f5207b7SJohn Levon union { 381f5207b7SJohn Levon long long value; 391f5207b7SJohn Levon unsigned long long uvalue; 401f5207b7SJohn Levon }; 411f5207b7SJohn Levon } sval_t; 421f5207b7SJohn Levon 431f5207b7SJohn Levon typedef long long mtag_t; 441f5207b7SJohn Levon 451f5207b7SJohn Levon struct smatch_state { 461f5207b7SJohn Levon const char *name; 471f5207b7SJohn Levon void *data; 481f5207b7SJohn Levon }; 491f5207b7SJohn Levon #define STATE(_x) static struct smatch_state _x = { .name = #_x } 501f5207b7SJohn Levon extern struct smatch_state undefined; 511f5207b7SJohn Levon extern struct smatch_state ghost; 521f5207b7SJohn Levon extern struct smatch_state merged; 531f5207b7SJohn Levon extern struct smatch_state true_state; 541f5207b7SJohn Levon extern struct smatch_state false_state; 551f5207b7SJohn Levon DECLARE_ALLOCATOR(smatch_state); 561f5207b7SJohn Levon 571f5207b7SJohn Levon static inline void *INT_PTR(int i) 581f5207b7SJohn Levon { 591f5207b7SJohn Levon return (void *)(long)i; 601f5207b7SJohn Levon } 611f5207b7SJohn Levon 621f5207b7SJohn Levon static inline int PTR_INT(void *p) 631f5207b7SJohn Levon { 641f5207b7SJohn Levon return (int)(long)p; 651f5207b7SJohn Levon } 661f5207b7SJohn Levon 671f5207b7SJohn Levon struct tracker { 681f5207b7SJohn Levon char *name; 691f5207b7SJohn Levon struct symbol *sym; 701f5207b7SJohn Levon unsigned short owner; 711f5207b7SJohn Levon }; 721f5207b7SJohn Levon DECLARE_ALLOCATOR(tracker); 731f5207b7SJohn Levon DECLARE_PTR_LIST(tracker_list, struct tracker); 741f5207b7SJohn Levon DECLARE_PTR_LIST(stree_stack, struct stree); 751f5207b7SJohn Levon 761f5207b7SJohn Levon /* The first 3 struct members must match struct tracker */ 771f5207b7SJohn Levon struct sm_state { 781f5207b7SJohn Levon const char *name; 791f5207b7SJohn Levon struct symbol *sym; 801f5207b7SJohn Levon unsigned short owner; 811f5207b7SJohn Levon unsigned short merged:1; 821f5207b7SJohn Levon unsigned int line; 831f5207b7SJohn Levon struct smatch_state *state; 841f5207b7SJohn Levon struct stree *pool; 851f5207b7SJohn Levon struct sm_state *left; 861f5207b7SJohn Levon struct sm_state *right; 871f5207b7SJohn Levon struct state_list *possible; 881f5207b7SJohn Levon }; 891f5207b7SJohn Levon 901f5207b7SJohn Levon struct var_sym { 911f5207b7SJohn Levon char *var; 921f5207b7SJohn Levon struct symbol *sym; 931f5207b7SJohn Levon }; 941f5207b7SJohn Levon DECLARE_ALLOCATOR(var_sym); 951f5207b7SJohn Levon DECLARE_PTR_LIST(var_sym_list, struct var_sym); 961f5207b7SJohn Levon 971f5207b7SJohn Levon struct constraint { 981f5207b7SJohn Levon int op; 991f5207b7SJohn Levon int id; 1001f5207b7SJohn Levon }; 1011f5207b7SJohn Levon DECLARE_PTR_LIST(constraint_list, struct constraint); 1021f5207b7SJohn Levon 103*efe51d0cSJohn Levon struct bit_info { 104*efe51d0cSJohn Levon unsigned long long set; 105*efe51d0cSJohn Levon unsigned long long possible; 106*efe51d0cSJohn Levon }; 107*efe51d0cSJohn Levon 1081f5207b7SJohn Levon enum hook_type { 1091f5207b7SJohn Levon EXPR_HOOK, 1101f5207b7SJohn Levon STMT_HOOK, 1111f5207b7SJohn Levon STMT_HOOK_AFTER, 1121f5207b7SJohn Levon SYM_HOOK, 1131f5207b7SJohn Levon STRING_HOOK, 1141f5207b7SJohn Levon DECLARATION_HOOK, 1151f5207b7SJohn Levon ASSIGNMENT_HOOK, 1161f5207b7SJohn Levon ASSIGNMENT_HOOK_AFTER, 1171f5207b7SJohn Levon RAW_ASSIGNMENT_HOOK, 1181f5207b7SJohn Levon GLOBAL_ASSIGNMENT_HOOK, 1191f5207b7SJohn Levon LOGIC_HOOK, 1201f5207b7SJohn Levon CONDITION_HOOK, 1211f5207b7SJohn Levon PRELOOP_HOOK, 1221f5207b7SJohn Levon SELECT_HOOK, 1231f5207b7SJohn Levon WHOLE_CONDITION_HOOK, 1241f5207b7SJohn Levon FUNCTION_CALL_HOOK_BEFORE, 1251f5207b7SJohn Levon FUNCTION_CALL_HOOK, 1261f5207b7SJohn Levon CALL_HOOK_AFTER_INLINE, 1271f5207b7SJohn Levon FUNCTION_CALL_HOOK_AFTER_DB, 1281f5207b7SJohn Levon CALL_ASSIGNMENT_HOOK, 1291f5207b7SJohn Levon MACRO_ASSIGNMENT_HOOK, 1301f5207b7SJohn Levon BINOP_HOOK, 1311f5207b7SJohn Levon OP_HOOK, 1321f5207b7SJohn Levon DEREF_HOOK, 1331f5207b7SJohn Levon CASE_HOOK, 1341f5207b7SJohn Levon ASM_HOOK, 1351f5207b7SJohn Levon CAST_HOOK, 1361f5207b7SJohn Levon SIZEOF_HOOK, 1371f5207b7SJohn Levon BASE_HOOK, 1381f5207b7SJohn Levon FUNC_DEF_HOOK, 1391f5207b7SJohn Levon AFTER_DEF_HOOK, 1401f5207b7SJohn Levon END_FUNC_HOOK, 1411f5207b7SJohn Levon AFTER_FUNC_HOOK, 1421f5207b7SJohn Levon RETURN_HOOK, 1431f5207b7SJohn Levon INLINE_FN_START, 1441f5207b7SJohn Levon INLINE_FN_END, 1451f5207b7SJohn Levon END_FILE_HOOK, 1461f5207b7SJohn Levon NUM_HOOKS, 1471f5207b7SJohn Levon }; 1481f5207b7SJohn Levon 1491f5207b7SJohn Levon #define TRUE 1 1501f5207b7SJohn Levon #define FALSE 0 1511f5207b7SJohn Levon 1521f5207b7SJohn Levon struct range_list; 1531f5207b7SJohn Levon 1541f5207b7SJohn Levon void add_hook(void *func, enum hook_type type); 1551f5207b7SJohn Levon typedef struct smatch_state *(merge_func_t)(struct smatch_state *s1, struct smatch_state *s2); 1561f5207b7SJohn Levon typedef struct smatch_state *(unmatched_func_t)(struct sm_state *state); 1571f5207b7SJohn Levon void add_merge_hook(int client_id, merge_func_t *func); 1581f5207b7SJohn Levon void add_unmatched_state_hook(int client_id, unmatched_func_t *func); 1591f5207b7SJohn Levon void add_pre_merge_hook(int client_id, void (*hook)(struct sm_state *sm)); 1601f5207b7SJohn Levon typedef void (scope_hook)(void *data); 1611f5207b7SJohn Levon void add_scope_hook(scope_hook *hook, void *data); 1621f5207b7SJohn Levon typedef void (func_hook)(const char *fn, struct expression *expr, void *data); 1631f5207b7SJohn Levon typedef void (implication_hook)(const char *fn, struct expression *call_expr, 1641f5207b7SJohn Levon struct expression *assign_expr, void *data); 1651f5207b7SJohn Levon typedef void (return_implies_hook)(struct expression *call_expr, 1661f5207b7SJohn Levon int param, char *key, char *value); 1671f5207b7SJohn Levon typedef int (implied_return_hook)(struct expression *call_expr, void *info, struct range_list **rl); 1681f5207b7SJohn Levon void add_function_hook(const char *look_for, func_hook *call_back, void *data); 1691f5207b7SJohn Levon 1701f5207b7SJohn Levon void add_function_assign_hook(const char *look_for, func_hook *call_back, 1711f5207b7SJohn Levon void *info); 1721f5207b7SJohn Levon void add_implied_return_hook(const char *look_for, 1731f5207b7SJohn Levon implied_return_hook *call_back, 1741f5207b7SJohn Levon void *info); 1751f5207b7SJohn Levon void add_macro_assign_hook(const char *look_for, func_hook *call_back, 1761f5207b7SJohn Levon void *info); 1771f5207b7SJohn Levon void add_macro_assign_hook_extra(const char *look_for, func_hook *call_back, 1781f5207b7SJohn Levon void *info); 1791f5207b7SJohn Levon void return_implies_state(const char *look_for, long long start, long long end, 1801f5207b7SJohn Levon implication_hook *call_back, void *info); 181*efe51d0cSJohn Levon void return_implies_state_sval(const char *look_for, sval_t start, sval_t end, 182*efe51d0cSJohn Levon implication_hook *call_back, void *info); 1831f5207b7SJohn Levon void select_return_states_hook(int type, return_implies_hook *callback); 1841f5207b7SJohn Levon void select_return_states_before(void (*fn)(void)); 1851f5207b7SJohn Levon void select_return_states_after(void (*fn)(void)); 1861f5207b7SJohn Levon int get_implied_return(struct expression *expr, struct range_list **rl); 1871f5207b7SJohn Levon void allocate_hook_memory(void); 1881f5207b7SJohn Levon 1891f5207b7SJohn Levon struct modification_data { 1901f5207b7SJohn Levon struct smatch_state *prev; 1911f5207b7SJohn Levon struct expression *cur; 1921f5207b7SJohn Levon }; 1931f5207b7SJohn Levon 1941f5207b7SJohn Levon typedef void (modification_hook)(struct sm_state *sm, struct expression *mod_expr); 1951f5207b7SJohn Levon void add_modification_hook(int owner, modification_hook *call_back); 1961f5207b7SJohn Levon void add_modification_hook_late(int owner, modification_hook *call_back); 1971f5207b7SJohn Levon struct smatch_state *get_modification_state(struct expression *expr); 1981f5207b7SJohn Levon 1991f5207b7SJohn Levon int outside_of_function(void); 2001f5207b7SJohn Levon const char *get_filename(void); 2011f5207b7SJohn Levon const char *get_base_file(void); 2021f5207b7SJohn Levon char *get_function(void); 2031f5207b7SJohn Levon int get_lineno(void); 2041f5207b7SJohn Levon extern int final_pass; 2051f5207b7SJohn Levon extern struct symbol *cur_func_sym; 2061f5207b7SJohn Levon extern int option_debug; 2071f5207b7SJohn Levon extern int local_debug; 2081f5207b7SJohn Levon extern int option_info; 2091f5207b7SJohn Levon extern int option_spammy; 2107ae7577cSJohn Levon extern int option_timeout; 2111f5207b7SJohn Levon extern char *trace_variable; 2121f5207b7SJohn Levon extern struct stree *global_states; 2131f5207b7SJohn Levon int is_skipped_function(void); 2141f5207b7SJohn Levon int is_silenced_function(void); 215*efe51d0cSJohn Levon extern bool implications_off; 2161f5207b7SJohn Levon 2171f5207b7SJohn Levon /* smatch_impossible.c */ 2181f5207b7SJohn Levon int is_impossible_path(void); 2191f5207b7SJohn Levon void set_path_impossible(void); 2201f5207b7SJohn Levon 2211f5207b7SJohn Levon extern FILE *sm_outfd; 2221f5207b7SJohn Levon extern FILE *sql_outfd; 2231f5207b7SJohn Levon extern FILE *caller_info_fd; 2241f5207b7SJohn Levon extern int sm_nr_checks; 2251f5207b7SJohn Levon extern int sm_nr_errors; 2261f5207b7SJohn Levon extern const char *progname; 2271f5207b7SJohn Levon 2281f5207b7SJohn Levon /* 2291f5207b7SJohn Levon * How to use these routines: 2301f5207b7SJohn Levon * 2311f5207b7SJohn Levon * sm_fatal(): an internal error of some kind that should immediately exit 2321f5207b7SJohn Levon * sm_ierror(): an internal error 2331f5207b7SJohn Levon * sm_perror(): an internal error from parsing input source 2341f5207b7SJohn Levon * sm_error(): an error from input source 2351f5207b7SJohn Levon * sm_warning(): a warning from input source 2361f5207b7SJohn Levon * sm_info(): info message (from option_info) 2371f5207b7SJohn Levon * sm_debug(): debug message 2381f5207b7SJohn Levon * sm_msg(): other message (please avoid using this) 2391f5207b7SJohn Levon */ 2401f5207b7SJohn Levon 2411f5207b7SJohn Levon #define sm_printf(msg...) do { if (final_pass || option_debug || local_debug) fprintf(sm_outfd, msg); } while (0) 2421f5207b7SJohn Levon 2431f5207b7SJohn Levon static inline void sm_prefix(void) 2441f5207b7SJohn Levon { 2451f5207b7SJohn Levon sm_printf("%s: %s:%d %s() ", progname, get_filename(), get_lineno(), get_function()); 2461f5207b7SJohn Levon } 2471f5207b7SJohn Levon 2481f5207b7SJohn Levon static inline void print_implied_debug_msg(); 2491f5207b7SJohn Levon 2501f5207b7SJohn Levon extern bool __silence_warnings_for_stmt; 2511f5207b7SJohn Levon 2521f5207b7SJohn Levon #define sm_print_msg(type, msg...) \ 2531f5207b7SJohn Levon do { \ 2541f5207b7SJohn Levon print_implied_debug_msg(); \ 2551f5207b7SJohn Levon if (!final_pass && !option_debug && !local_debug) \ 2561f5207b7SJohn Levon break; \ 2571f5207b7SJohn Levon if (__silence_warnings_for_stmt && !option_debug && !local_debug) \ 2581f5207b7SJohn Levon break; \ 2591f5207b7SJohn Levon if (!option_info && is_silenced_function()) \ 2601f5207b7SJohn Levon break; \ 2611f5207b7SJohn Levon sm_prefix(); \ 2621f5207b7SJohn Levon if (type == 1) { \ 2631f5207b7SJohn Levon sm_printf("warn: "); \ 2641f5207b7SJohn Levon sm_nr_checks++; \ 2651f5207b7SJohn Levon } else if (type == 2) { \ 2661f5207b7SJohn Levon sm_printf("error: "); \ 2671f5207b7SJohn Levon sm_nr_checks++; \ 2681f5207b7SJohn Levon } else if (type == 3) { \ 2691f5207b7SJohn Levon sm_printf("parse error: "); \ 2701f5207b7SJohn Levon sm_nr_errors++; \ 2711f5207b7SJohn Levon } \ 2721f5207b7SJohn Levon sm_printf(msg); \ 2731f5207b7SJohn Levon sm_printf("\n"); \ 2741f5207b7SJohn Levon } while (0) 2751f5207b7SJohn Levon 2761f5207b7SJohn Levon #define sm_msg(msg...) do { sm_print_msg(0, msg); } while (0) 2771f5207b7SJohn Levon 2781f5207b7SJohn Levon #define local_debug(msg...) \ 2791f5207b7SJohn Levon do { \ 2801f5207b7SJohn Levon if (local_debug) \ 2811f5207b7SJohn Levon sm_msg(msg); \ 2821f5207b7SJohn Levon } while (0) 2831f5207b7SJohn Levon 2841f5207b7SJohn Levon extern char *implied_debug_msg; 2851f5207b7SJohn Levon static inline void print_implied_debug_msg(void) 2861f5207b7SJohn Levon { 2871f5207b7SJohn Levon static struct symbol *last_printed = NULL; 2881f5207b7SJohn Levon 2891f5207b7SJohn Levon if (!implied_debug_msg) 2901f5207b7SJohn Levon return; 2911f5207b7SJohn Levon if (last_printed == cur_func_sym) 2921f5207b7SJohn Levon return; 2931f5207b7SJohn Levon last_printed = cur_func_sym; 2941f5207b7SJohn Levon sm_msg("%s", implied_debug_msg); 2951f5207b7SJohn Levon } 2961f5207b7SJohn Levon 2971f5207b7SJohn Levon #define sm_debug(msg...) do { if (option_debug) sm_printf(msg); } while (0) 2981f5207b7SJohn Levon 2991f5207b7SJohn Levon #define sm_info(msg...) do { \ 3001f5207b7SJohn Levon if (option_debug || (option_info && final_pass)) { \ 3011f5207b7SJohn Levon sm_prefix(); \ 3021f5207b7SJohn Levon sm_printf("info: "); \ 3031f5207b7SJohn Levon sm_printf(msg); \ 3041f5207b7SJohn Levon sm_printf("\n"); \ 3051f5207b7SJohn Levon } \ 3061f5207b7SJohn Levon } while(0) 3071f5207b7SJohn Levon 3081f5207b7SJohn Levon #define sm_warning(msg...) do { sm_print_msg(1, msg); } while (0) 3091f5207b7SJohn Levon #define sm_error(msg...) do { sm_print_msg(2, msg); } while (0) 3101f5207b7SJohn Levon #define sm_perror(msg...) do { sm_print_msg(3, msg); } while (0) 3111f5207b7SJohn Levon 3121f5207b7SJohn Levon static inline void sm_fatal(const char *fmt, ...) 3131f5207b7SJohn Levon { 3141f5207b7SJohn Levon va_list args; 3151f5207b7SJohn Levon 3161f5207b7SJohn Levon va_start(args, fmt); 3171f5207b7SJohn Levon vfprintf(sm_outfd, fmt, args); 3181f5207b7SJohn Levon va_end(args); 3191f5207b7SJohn Levon 3201f5207b7SJohn Levon fprintf(sm_outfd, "\n"); 3211f5207b7SJohn Levon 3221f5207b7SJohn Levon exit(1); 3231f5207b7SJohn Levon } 3241f5207b7SJohn Levon 3251f5207b7SJohn Levon static inline void sm_ierror(const char *fmt, ...) 3261f5207b7SJohn Levon { 3271f5207b7SJohn Levon va_list args; 3281f5207b7SJohn Levon 3291f5207b7SJohn Levon sm_nr_errors++; 3301f5207b7SJohn Levon 3311f5207b7SJohn Levon fprintf(sm_outfd, "internal error: "); 3321f5207b7SJohn Levon 3331f5207b7SJohn Levon va_start(args, fmt); 3341f5207b7SJohn Levon vfprintf(sm_outfd, fmt, args); 3351f5207b7SJohn Levon va_end(args); 3361f5207b7SJohn Levon 3371f5207b7SJohn Levon fprintf(sm_outfd, "\n"); 3381f5207b7SJohn Levon } 3391f5207b7SJohn Levon #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1)) 3401f5207b7SJohn Levon 3411f5207b7SJohn Levon struct smatch_state *__get_state(int owner, const char *name, struct symbol *sym); 3421f5207b7SJohn Levon struct smatch_state *get_state(int owner, const char *name, struct symbol *sym); 3431f5207b7SJohn Levon struct smatch_state *get_state_expr(int owner, struct expression *expr); 3441f5207b7SJohn Levon struct state_list *get_possible_states(int owner, const char *name, 3451f5207b7SJohn Levon struct symbol *sym); 3461f5207b7SJohn Levon struct state_list *get_possible_states_expr(int owner, struct expression *expr); 3471f5207b7SJohn Levon struct sm_state *set_state(int owner, const char *name, struct symbol *sym, 3481f5207b7SJohn Levon struct smatch_state *state); 3491f5207b7SJohn Levon struct sm_state *set_state_expr(int owner, struct expression *expr, 3501f5207b7SJohn Levon struct smatch_state *state); 3511f5207b7SJohn Levon void delete_state(int owner, const char *name, struct symbol *sym); 3521f5207b7SJohn Levon void delete_state_expr(int owner, struct expression *expr); 3531f5207b7SJohn Levon void __delete_all_states_sym(struct symbol *sym); 3541f5207b7SJohn Levon void set_true_false_states(int owner, const char *name, struct symbol *sym, 3551f5207b7SJohn Levon struct smatch_state *true_state, 3561f5207b7SJohn Levon struct smatch_state *false_state); 3571f5207b7SJohn Levon void set_true_false_states_expr(int owner, struct expression *expr, 3581f5207b7SJohn Levon struct smatch_state *true_state, 3591f5207b7SJohn Levon struct smatch_state *false_state); 3601f5207b7SJohn Levon 3611f5207b7SJohn Levon struct stree *get_all_states_from_stree(int owner, struct stree *source); 3621f5207b7SJohn Levon struct stree *get_all_states_stree(int id); 3631f5207b7SJohn Levon struct stree *__get_cur_stree(void); 3641f5207b7SJohn Levon int is_reachable(void); 3651f5207b7SJohn Levon void add_get_state_hook(void (*fn)(int owner, const char *name, struct symbol *sym)); 3661f5207b7SJohn Levon 3671f5207b7SJohn Levon /* smatch_helper.c */ 3681f5207b7SJohn Levon DECLARE_PTR_LIST(int_stack, int); 3691f5207b7SJohn Levon char *alloc_string(const char *str); 3701f5207b7SJohn Levon void free_string(char *str); 3711f5207b7SJohn Levon void append(char *dest, const char *data, int buff_len); 3721f5207b7SJohn Levon void remove_parens(char *str); 3731f5207b7SJohn Levon struct smatch_state *alloc_state_num(int num); 3741f5207b7SJohn Levon struct smatch_state *alloc_state_str(const char *name); 375*efe51d0cSJohn Levon struct smatch_state *merge_str_state(struct smatch_state *s1, struct smatch_state *s2); 3761f5207b7SJohn Levon struct smatch_state *alloc_state_expr(struct expression *expr); 3771f5207b7SJohn Levon struct expression *get_argument_from_call_expr(struct expression_list *args, 3781f5207b7SJohn Levon int num); 3791f5207b7SJohn Levon 3801f5207b7SJohn Levon char *expr_to_var(struct expression *expr); 3811f5207b7SJohn Levon struct symbol *expr_to_sym(struct expression *expr); 3821f5207b7SJohn Levon char *expr_to_str(struct expression *expr); 3831f5207b7SJohn Levon char *expr_to_str_sym(struct expression *expr, 3841f5207b7SJohn Levon struct symbol **sym_ptr); 3851f5207b7SJohn Levon char *expr_to_var_sym(struct expression *expr, 3861f5207b7SJohn Levon struct symbol **sym_ptr); 3871f5207b7SJohn Levon char *expr_to_known_chunk_sym(struct expression *expr, struct symbol **sym); 3881f5207b7SJohn Levon char *expr_to_chunk_sym_vsl(struct expression *expr, struct symbol **sym, struct var_sym_list **vsl); 3891f5207b7SJohn Levon int get_complication_score(struct expression *expr); 3901f5207b7SJohn Levon 3911f5207b7SJohn Levon int sym_name_is(const char *name, struct expression *expr); 3921f5207b7SJohn Levon int get_const_value(struct expression *expr, sval_t *sval); 3931f5207b7SJohn Levon int get_value(struct expression *expr, sval_t *val); 3941f5207b7SJohn Levon int get_implied_value(struct expression *expr, sval_t *val); 3951f5207b7SJohn Levon int get_implied_min(struct expression *expr, sval_t *sval); 3961f5207b7SJohn Levon int get_implied_max(struct expression *expr, sval_t *val); 3971f5207b7SJohn Levon int get_hard_max(struct expression *expr, sval_t *sval); 3981f5207b7SJohn Levon int get_fuzzy_min(struct expression *expr, sval_t *min); 3991f5207b7SJohn Levon int get_fuzzy_max(struct expression *expr, sval_t *max); 4001f5207b7SJohn Levon int get_absolute_min(struct expression *expr, sval_t *sval); 4011f5207b7SJohn Levon int get_absolute_max(struct expression *expr, sval_t *sval); 4021f5207b7SJohn Levon int parse_call_math(struct expression *expr, char *math, sval_t *val); 403*efe51d0cSJohn Levon int parse_call_math_rl(struct expression *call, const char *math, struct range_list **rl); 4041f5207b7SJohn Levon char *get_value_in_terms_of_parameter_math(struct expression *expr); 4051f5207b7SJohn Levon char *get_value_in_terms_of_parameter_math_var_sym(const char *var, struct symbol *sym); 4061f5207b7SJohn Levon int is_zero(struct expression *expr); 4071f5207b7SJohn Levon int known_condition_true(struct expression *expr); 4081f5207b7SJohn Levon int known_condition_false(struct expression *expr); 4091f5207b7SJohn Levon int implied_condition_true(struct expression *expr); 4101f5207b7SJohn Levon int implied_condition_false(struct expression *expr); 4111f5207b7SJohn Levon int can_integer_overflow(struct symbol *type, struct expression *expr); 4121f5207b7SJohn Levon void clear_math_cache(void); 4131f5207b7SJohn Levon 4141f5207b7SJohn Levon int is_array(struct expression *expr); 4151f5207b7SJohn Levon struct expression *get_array_base(struct expression *expr); 4161f5207b7SJohn Levon struct expression *get_array_offset(struct expression *expr); 4171f5207b7SJohn Levon const char *show_state(struct smatch_state *state); 4181f5207b7SJohn Levon struct statement *get_expression_statement(struct expression *expr); 4191f5207b7SJohn Levon struct expression *strip_parens(struct expression *expr); 4201f5207b7SJohn Levon struct expression *strip_expr(struct expression *expr); 4211f5207b7SJohn Levon struct expression *strip_expr_set_parent(struct expression *expr); 4221f5207b7SJohn Levon void scoped_state(int my_id, const char *name, struct symbol *sym); 4231f5207b7SJohn Levon int is_error_return(struct expression *expr); 4241f5207b7SJohn Levon int getting_address(void); 4251f5207b7SJohn Levon int get_struct_and_member(struct expression *expr, const char **type, const char **member); 4261f5207b7SJohn Levon char *get_member_name(struct expression *expr); 4271f5207b7SJohn Levon char *get_fnptr_name(struct expression *expr); 4281f5207b7SJohn Levon int cmp_pos(struct position pos1, struct position pos2); 4291f5207b7SJohn Levon int positions_eq(struct position pos1, struct position pos2); 4301f5207b7SJohn Levon struct statement *get_current_statement(void); 4311f5207b7SJohn Levon struct statement *get_prev_statement(void); 4321f5207b7SJohn Levon struct expression *get_last_expr_from_expression_stmt(struct expression *expr); 4331f5207b7SJohn Levon int get_param_num_from_sym(struct symbol *sym); 4341f5207b7SJohn Levon int get_param_num(struct expression *expr); 4351f5207b7SJohn Levon int ms_since(struct timeval *start); 4361f5207b7SJohn Levon int parent_is_gone_var_sym(const char *name, struct symbol *sym); 4371f5207b7SJohn Levon int parent_is_gone(struct expression *expr); 4381f5207b7SJohn Levon int invert_op(int op); 439*efe51d0cSJohn Levon int op_remove_assign(int op); 4401f5207b7SJohn Levon int expr_equiv(struct expression *one, struct expression *two); 4411f5207b7SJohn Levon void push_int(struct int_stack **stack, int num); 4421f5207b7SJohn Levon int pop_int(struct int_stack **stack); 4431f5207b7SJohn Levon 4441f5207b7SJohn Levon /* smatch_type.c */ 4451f5207b7SJohn Levon struct symbol *get_real_base_type(struct symbol *sym); 4461f5207b7SJohn Levon int type_bytes(struct symbol *type); 4471f5207b7SJohn Levon int array_bytes(struct symbol *type); 4481f5207b7SJohn Levon struct symbol *get_pointer_type(struct expression *expr); 4491f5207b7SJohn Levon struct symbol *get_type(struct expression *expr); 4501f5207b7SJohn Levon struct symbol *get_final_type(struct expression *expr); 4511f5207b7SJohn Levon struct symbol *get_promoted_type(struct symbol *left, struct symbol *right); 4521f5207b7SJohn Levon int type_signed(struct symbol *base_type); 4531f5207b7SJohn Levon int expr_unsigned(struct expression *expr); 4541f5207b7SJohn Levon int expr_signed(struct expression *expr); 4551f5207b7SJohn Levon int returns_unsigned(struct symbol *base_type); 4561f5207b7SJohn Levon int is_pointer(struct expression *expr); 4571f5207b7SJohn Levon int returns_pointer(struct symbol *base_type); 4581f5207b7SJohn Levon sval_t sval_type_max(struct symbol *base_type); 4591f5207b7SJohn Levon sval_t sval_type_min(struct symbol *base_type); 4601f5207b7SJohn Levon int nr_bits(struct expression *expr); 4611f5207b7SJohn Levon int is_void_pointer(struct expression *expr); 4621f5207b7SJohn Levon int is_char_pointer(struct expression *expr); 4631f5207b7SJohn Levon int is_string(struct expression *expr); 4641f5207b7SJohn Levon int is_static(struct expression *expr); 4651f5207b7SJohn Levon int is_local_variable(struct expression *expr); 4661f5207b7SJohn Levon int types_equiv(struct symbol *one, struct symbol *two); 4671f5207b7SJohn Levon int fn_static(void); 4681f5207b7SJohn Levon const char *global_static(); 4691f5207b7SJohn Levon struct symbol *cur_func_return_type(void); 4701f5207b7SJohn Levon struct symbol *get_arg_type(struct expression *fn, int arg); 4711f5207b7SJohn Levon struct symbol *get_member_type_from_key(struct expression *expr, const char *key); 4721f5207b7SJohn Levon struct symbol *get_arg_type_from_key(struct expression *fn, int param, struct expression *arg, const char *key); 4731f5207b7SJohn Levon int is_struct(struct expression *expr); 4741f5207b7SJohn Levon char *type_to_str(struct symbol *type); 4751f5207b7SJohn Levon 4761f5207b7SJohn Levon /* smatch_ignore.c */ 4771f5207b7SJohn Levon void add_ignore(int owner, const char *name, struct symbol *sym); 4781f5207b7SJohn Levon int is_ignored(int owner, const char *name, struct symbol *sym); 4791f5207b7SJohn Levon void add_ignore_expr(int owner, struct expression *expr); 4801f5207b7SJohn Levon int is_ignored_expr(int owner, struct expression *expr); 4811f5207b7SJohn Levon 4821f5207b7SJohn Levon /* smatch_var_sym */ 4831f5207b7SJohn Levon struct var_sym *alloc_var_sym(const char *var, struct symbol *sym); 4841f5207b7SJohn Levon struct var_sym_list *expr_to_vsl(struct expression *expr); 4851f5207b7SJohn Levon void add_var_sym(struct var_sym_list **list, const char *var, struct symbol *sym); 4861f5207b7SJohn Levon void add_var_sym_expr(struct var_sym_list **list, struct expression *expr); 4871f5207b7SJohn Levon void del_var_sym(struct var_sym_list **list, const char *var, struct symbol *sym); 4881f5207b7SJohn Levon int in_var_sym_list(struct var_sym_list *list, const char *var, struct symbol *sym); 4891f5207b7SJohn Levon struct var_sym_list *clone_var_sym_list(struct var_sym_list *from_vsl); 4901f5207b7SJohn Levon void merge_var_sym_list(struct var_sym_list **dest, struct var_sym_list *src); 4911f5207b7SJohn Levon struct var_sym_list *combine_var_sym_lists(struct var_sym_list *one, struct var_sym_list *two); 4921f5207b7SJohn Levon int var_sym_lists_equiv(struct var_sym_list *one, struct var_sym_list *two); 4931f5207b7SJohn Levon void free_var_sym_list(struct var_sym_list **list); 4941f5207b7SJohn Levon void free_var_syms_and_list(struct var_sym_list **list); 4951f5207b7SJohn Levon 4961f5207b7SJohn Levon /* smatch_tracker */ 4971f5207b7SJohn Levon struct tracker *alloc_tracker(int owner, const char *name, struct symbol *sym); 4981f5207b7SJohn Levon void add_tracker(struct tracker_list **list, int owner, const char *name, 4991f5207b7SJohn Levon struct symbol *sym); 5001f5207b7SJohn Levon void add_tracker_expr(struct tracker_list **list, int owner, struct expression *expr); 5011f5207b7SJohn Levon void del_tracker(struct tracker_list **list, int owner, const char *name, 5021f5207b7SJohn Levon struct symbol *sym); 5031f5207b7SJohn Levon int in_tracker_list(struct tracker_list *list, int owner, const char *name, 5041f5207b7SJohn Levon struct symbol *sym); 5051f5207b7SJohn Levon void free_tracker_list(struct tracker_list **list); 5061f5207b7SJohn Levon void free_trackers_and_list(struct tracker_list **list); 5071f5207b7SJohn Levon 5081f5207b7SJohn Levon /* smatch_conditions */ 5091f5207b7SJohn Levon int in_condition(void); 5101f5207b7SJohn Levon 5111f5207b7SJohn Levon /* smatch_flow.c */ 5121f5207b7SJohn Levon 5131f5207b7SJohn Levon extern int __in_fake_assign; 5141f5207b7SJohn Levon extern int __in_fake_parameter_assign; 5151f5207b7SJohn Levon extern int __in_fake_struct_assign; 5161f5207b7SJohn Levon extern int in_fake_env; 517*efe51d0cSJohn Levon void smatch (struct string_list *filelist); 5181f5207b7SJohn Levon int inside_loop(void); 5191f5207b7SJohn Levon int definitely_inside_loop(void); 5201f5207b7SJohn Levon struct expression *get_switch_expr(void); 5211f5207b7SJohn Levon int in_expression_statement(void); 5221f5207b7SJohn Levon void __process_post_op_stack(void); 5231f5207b7SJohn Levon void __split_expr(struct expression *expr); 5241f5207b7SJohn Levon void __split_label_stmt(struct statement *stmt); 5251f5207b7SJohn Levon void __split_stmt(struct statement *stmt); 5261f5207b7SJohn Levon extern int __in_function_def; 5271f5207b7SJohn Levon extern int option_assume_loops; 5281f5207b7SJohn Levon extern int option_two_passes; 5291f5207b7SJohn Levon extern int option_no_db; 5301f5207b7SJohn Levon extern int option_file_output; 5311f5207b7SJohn Levon extern int option_time; 5321f5207b7SJohn Levon extern struct expression_list *big_expression_stack; 5331f5207b7SJohn Levon extern struct expression_list *big_condition_stack; 5341f5207b7SJohn Levon extern struct statement_list *big_statement_stack; 5351f5207b7SJohn Levon int is_assigned_call(struct expression *expr); 5361f5207b7SJohn Levon int inlinable(struct expression *expr); 5371f5207b7SJohn Levon extern int __inline_call; 5381f5207b7SJohn Levon extern struct expression *__inline_fn; 5391f5207b7SJohn Levon extern int __in_pre_condition; 5401f5207b7SJohn Levon extern int __bail_on_rest_of_function; 5411f5207b7SJohn Levon extern struct statement *__prev_stmt; 5421f5207b7SJohn Levon extern struct statement *__cur_stmt; 5431f5207b7SJohn Levon extern struct statement *__next_stmt; 5441f5207b7SJohn Levon void init_fake_env(void); 5451f5207b7SJohn Levon void end_fake_env(void); 5461f5207b7SJohn Levon int time_parsing_function(void); 547*efe51d0cSJohn Levon bool taking_too_long(void); 5481f5207b7SJohn Levon 5491f5207b7SJohn Levon /* smatch_struct_assignment.c */ 5501f5207b7SJohn Levon struct expression *get_faked_expression(void); 5511f5207b7SJohn Levon void __fake_struct_member_assignments(struct expression *expr); 5521f5207b7SJohn Levon 5531f5207b7SJohn Levon /* smatch_project.c */ 5541f5207b7SJohn Levon int is_no_inline_function(const char *function); 5551f5207b7SJohn Levon 5561f5207b7SJohn Levon /* smatch_conditions */ 5571f5207b7SJohn Levon void __split_whole_condition(struct expression *expr); 5581f5207b7SJohn Levon void __handle_logic(struct expression *expr); 5591f5207b7SJohn Levon int is_condition(struct expression *expr); 5601f5207b7SJohn Levon int __handle_condition_assigns(struct expression *expr); 5611f5207b7SJohn Levon int __handle_select_assigns(struct expression *expr); 5621f5207b7SJohn Levon int __handle_expr_statement_assigns(struct expression *expr); 5631f5207b7SJohn Levon 5641f5207b7SJohn Levon /* smatch_implied.c */ 5651f5207b7SJohn Levon struct range_list_stack; 5661f5207b7SJohn Levon void param_limit_implications(struct expression *expr, int param, char *key, char *value); 5671f5207b7SJohn Levon struct stree *__implied_case_stree(struct expression *switch_expr, 5681f5207b7SJohn Levon struct range_list *case_rl, 5691f5207b7SJohn Levon struct range_list_stack **remaining_cases, 5701f5207b7SJohn Levon struct stree **raw_stree); 5711f5207b7SJohn Levon void overwrite_states_using_pool(struct sm_state *gate_sm, struct sm_state *pool_sm); 5721f5207b7SJohn Levon int assume(struct expression *expr); 5731f5207b7SJohn Levon void end_assume(void); 5741f5207b7SJohn Levon int impossible_assumption(struct expression *left, int op, sval_t sval); 5751f5207b7SJohn Levon 576*efe51d0cSJohn Levon /* smatch_slist.h */ 577*efe51d0cSJohn Levon bool has_dynamic_states(unsigned short owner); 578*efe51d0cSJohn Levon void set_dynamic_states(unsigned short owner); 579*efe51d0cSJohn Levon 5801f5207b7SJohn Levon /* smatch_extras.c */ 581*efe51d0cSJohn Levon int in_warn_on_macro(void); 5821f5207b7SJohn Levon #define SMATCH_EXTRA 5 /* this is my_id from smatch extra set in smatch.c */ 5831f5207b7SJohn Levon extern int RETURN_ID; 5841f5207b7SJohn Levon 5851f5207b7SJohn Levon struct data_range { 5861f5207b7SJohn Levon sval_t min; 5871f5207b7SJohn Levon sval_t max; 5881f5207b7SJohn Levon }; 5891f5207b7SJohn Levon 5901f5207b7SJohn Levon #define MTAG_ALIAS_BIT (1ULL << 63) 5911f5207b7SJohn Levon #define MTAG_OFFSET_MASK 0xfffULL 592*efe51d0cSJohn Levon #define MTAG_SEED 0xdead << 12 5931f5207b7SJohn Levon 594*efe51d0cSJohn Levon const extern unsigned long valid_ptr_min; 595*efe51d0cSJohn Levon extern unsigned long valid_ptr_max; 596*efe51d0cSJohn Levon extern const sval_t valid_ptr_min_sval; 597*efe51d0cSJohn Levon extern sval_t valid_ptr_max_sval; 5981f5207b7SJohn Levon extern struct range_list *valid_ptr_rl; 599*efe51d0cSJohn Levon void alloc_valid_ptr_rl(void); 600*efe51d0cSJohn Levon 6011f5207b7SJohn Levon static const sval_t array_min_sval = { 6021f5207b7SJohn Levon .type = &ptr_ctype, 6031f5207b7SJohn Levon {.value = 100000}, 6041f5207b7SJohn Levon }; 6051f5207b7SJohn Levon static const sval_t array_max_sval = { 6061f5207b7SJohn Levon .type = &ptr_ctype, 607*efe51d0cSJohn Levon {.value = ULONG_MAX - 4095}, 6081f5207b7SJohn Levon }; 6091f5207b7SJohn Levon static const sval_t text_seg_min = { 6101f5207b7SJohn Levon .type = &ptr_ctype, 611*efe51d0cSJohn Levon {.value = 4096}, 6121f5207b7SJohn Levon }; 6131f5207b7SJohn Levon static const sval_t text_seg_max = { 6141f5207b7SJohn Levon .type = &ptr_ctype, 615*efe51d0cSJohn Levon {.value = ULONG_MAX - 4095}, 6161f5207b7SJohn Levon }; 6171f5207b7SJohn Levon static const sval_t data_seg_min = { 6181f5207b7SJohn Levon .type = &ptr_ctype, 619*efe51d0cSJohn Levon {.value = 4096}, 6201f5207b7SJohn Levon }; 6211f5207b7SJohn Levon static const sval_t data_seg_max = { 6221f5207b7SJohn Levon .type = &ptr_ctype, 623*efe51d0cSJohn Levon {.value = ULONG_MAX - 4095}, 6241f5207b7SJohn Levon }; 6251f5207b7SJohn Levon static const sval_t bss_seg_min = { 6261f5207b7SJohn Levon .type = &ptr_ctype, 627*efe51d0cSJohn Levon {.value = 4096}, 6281f5207b7SJohn Levon }; 6291f5207b7SJohn Levon static const sval_t bss_seg_max = { 6301f5207b7SJohn Levon .type = &ptr_ctype, 631*efe51d0cSJohn Levon {.value = ULONG_MAX - 4095}, 6321f5207b7SJohn Levon }; 6331f5207b7SJohn Levon static const sval_t stack_seg_min = { 6341f5207b7SJohn Levon .type = &ptr_ctype, 635*efe51d0cSJohn Levon {.value = 4096}, 6361f5207b7SJohn Levon }; 6371f5207b7SJohn Levon static const sval_t stack_seg_max = { 6381f5207b7SJohn Levon .type = &ptr_ctype, 639*efe51d0cSJohn Levon {.value = ULONG_MAX - 4095}, 6401f5207b7SJohn Levon }; 6411f5207b7SJohn Levon static const sval_t kmalloc_seg_min = { 6421f5207b7SJohn Levon .type = &ptr_ctype, 643*efe51d0cSJohn Levon {.value = 4096}, 6441f5207b7SJohn Levon }; 6451f5207b7SJohn Levon static const sval_t kmalloc_seg_max = { 6461f5207b7SJohn Levon .type = &ptr_ctype, 647*efe51d0cSJohn Levon {.value = ULONG_MAX - 4095}, 6481f5207b7SJohn Levon }; 6491f5207b7SJohn Levon static const sval_t vmalloc_seg_min = { 6501f5207b7SJohn Levon .type = &ptr_ctype, 651*efe51d0cSJohn Levon {.value = 4096}, 6521f5207b7SJohn Levon }; 6531f5207b7SJohn Levon static const sval_t vmalloc_seg_max = { 6541f5207b7SJohn Levon .type = &ptr_ctype, 655*efe51d0cSJohn Levon {.value = ULONG_MAX - 4095}, 6561f5207b7SJohn Levon }; 6571f5207b7SJohn Levon static const sval_t fn_ptr_min = { 6581f5207b7SJohn Levon .type = &ptr_ctype, 659*efe51d0cSJohn Levon {.value = 4096}, 6601f5207b7SJohn Levon }; 6611f5207b7SJohn Levon static const sval_t fn_ptr_max = { 6621f5207b7SJohn Levon .type = &ptr_ctype, 663*efe51d0cSJohn Levon {.value = ULONG_MAX - 4095}, 6641f5207b7SJohn Levon }; 6651f5207b7SJohn Levon 6661f5207b7SJohn Levon char *get_other_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym); 6671f5207b7SJohn Levon char *map_call_to_other_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym); 668*efe51d0cSJohn Levon char *map_long_to_short_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym, bool use_stack); 6691f5207b7SJohn Levon 6701f5207b7SJohn Levon #define STRLEN_MAX_RET 1010101 6711f5207b7SJohn Levon 6721f5207b7SJohn Levon /* smatch_absolute.c */ 6731f5207b7SJohn Levon int get_absolute_min_helper(struct expression *expr, sval_t *sval); 6741f5207b7SJohn Levon int get_absolute_max_helper(struct expression *expr, sval_t *sval); 6751f5207b7SJohn Levon 6761f5207b7SJohn Levon /* smatch_local_values.c */ 6771f5207b7SJohn Levon int get_local_rl(struct expression *expr, struct range_list **rl); 6781f5207b7SJohn Levon int get_local_max_helper(struct expression *expr, sval_t *sval); 6791f5207b7SJohn Levon int get_local_min_helper(struct expression *expr, sval_t *sval); 6801f5207b7SJohn Levon 6811f5207b7SJohn Levon /* smatch_type_value.c */ 6821f5207b7SJohn Levon int get_db_type_rl(struct expression *expr, struct range_list **rl); 6831f5207b7SJohn Levon /* smatch_data_val.c */ 6841f5207b7SJohn Levon int get_mtag_rl(struct expression *expr, struct range_list **rl); 6851f5207b7SJohn Levon /* smatch_array_values.c */ 6861f5207b7SJohn Levon int get_array_rl(struct expression *expr, struct range_list **rl); 6871f5207b7SJohn Levon 6881f5207b7SJohn Levon /* smatch_states.c */ 6891f5207b7SJohn Levon void __swap_cur_stree(struct stree *stree); 6901f5207b7SJohn Levon void __push_fake_cur_stree(); 6911f5207b7SJohn Levon struct stree *__pop_fake_cur_stree(); 6921f5207b7SJohn Levon void __free_fake_cur_stree(); 6931f5207b7SJohn Levon void __set_fake_cur_stree_fast(struct stree *stree); 6941f5207b7SJohn Levon void __pop_fake_cur_stree_fast(void); 6951f5207b7SJohn Levon void __merge_stree_into_cur(struct stree *stree); 6961f5207b7SJohn Levon 6971f5207b7SJohn Levon int unreachable(void); 6981f5207b7SJohn Levon void __set_sm(struct sm_state *sm); 6991f5207b7SJohn Levon void __set_sm_cur_stree(struct sm_state *sm); 7001f5207b7SJohn Levon void __set_sm_fake_stree(struct sm_state *sm); 7011f5207b7SJohn Levon void __set_true_false_sm(struct sm_state *true_state, 7021f5207b7SJohn Levon struct sm_state *false_state); 7031f5207b7SJohn Levon void nullify_path(void); 7041f5207b7SJohn Levon void __match_nullify_path_hook(const char *fn, struct expression *expr, 7051f5207b7SJohn Levon void *unused); 7061f5207b7SJohn Levon void __unnullify_path(void); 7071f5207b7SJohn Levon int __path_is_null(void); 7081f5207b7SJohn Levon void save_all_states(void); 7091f5207b7SJohn Levon void restore_all_states(void); 7101f5207b7SJohn Levon void free_goto_stack(void); 7111f5207b7SJohn Levon void clear_all_states(void); 7121f5207b7SJohn Levon 7131f5207b7SJohn Levon struct sm_state *get_sm_state(int owner, const char *name, 7141f5207b7SJohn Levon struct symbol *sym); 7151f5207b7SJohn Levon struct sm_state *get_sm_state_expr(int owner, struct expression *expr); 7161f5207b7SJohn Levon void __push_true_states(void); 7171f5207b7SJohn Levon void __use_false_states(void); 7181f5207b7SJohn Levon void __discard_false_states(void); 7191f5207b7SJohn Levon void __merge_false_states(void); 7201f5207b7SJohn Levon void __merge_true_states(void); 7211f5207b7SJohn Levon 7221f5207b7SJohn Levon void __negate_cond_stacks(void); 7231f5207b7SJohn Levon void __use_pre_cond_states(void); 7241f5207b7SJohn Levon void __use_cond_true_states(void); 7251f5207b7SJohn Levon void __use_cond_false_states(void); 7261f5207b7SJohn Levon void __push_cond_stacks(void); 7271f5207b7SJohn Levon void __fold_in_set_states(void); 7281f5207b7SJohn Levon void __free_set_states(void); 7291f5207b7SJohn Levon struct stree *__copy_cond_true_states(void); 7301f5207b7SJohn Levon struct stree *__copy_cond_false_states(void); 7311f5207b7SJohn Levon struct stree *__pop_cond_true_stack(void); 7321f5207b7SJohn Levon struct stree *__pop_cond_false_stack(void); 7331f5207b7SJohn Levon void __and_cond_states(void); 7341f5207b7SJohn Levon void __or_cond_states(void); 7351f5207b7SJohn Levon void __save_pre_cond_states(void); 7361f5207b7SJohn Levon void __discard_pre_cond_states(void); 7371f5207b7SJohn Levon struct stree *__get_true_states(void); 7381f5207b7SJohn Levon struct stree *__get_false_states(void); 7391f5207b7SJohn Levon void __use_cond_states(void); 7401f5207b7SJohn Levon extern struct state_list *__last_base_slist; 7411f5207b7SJohn Levon 7421f5207b7SJohn Levon void __push_continues(void); 7431f5207b7SJohn Levon void __discard_continues(void); 7441f5207b7SJohn Levon void __process_continues(void); 7451f5207b7SJohn Levon void __merge_continues(void); 7461f5207b7SJohn Levon 7471f5207b7SJohn Levon void __push_breaks(void); 7481f5207b7SJohn Levon void __process_breaks(void); 7491f5207b7SJohn Levon int __has_breaks(void); 7501f5207b7SJohn Levon void __merge_breaks(void); 7511f5207b7SJohn Levon void __use_breaks(void); 7521f5207b7SJohn Levon 7531f5207b7SJohn Levon void __save_switch_states(struct expression *switch_expr); 7541f5207b7SJohn Levon void __discard_switches(void); 7551f5207b7SJohn Levon int have_remaining_cases(void); 7561f5207b7SJohn Levon void __merge_switches(struct expression *switch_expr, struct range_list *case_rl); 7571f5207b7SJohn Levon void __push_default(void); 7581f5207b7SJohn Levon void __set_default(void); 7591f5207b7SJohn Levon int __pop_default(void); 7601f5207b7SJohn Levon 7611f5207b7SJohn Levon void __push_conditions(void); 7621f5207b7SJohn Levon void __discard_conditions(void); 7631f5207b7SJohn Levon 7641f5207b7SJohn Levon void __save_gotos(const char *name, struct symbol *sym); 7651f5207b7SJohn Levon void __merge_gotos(const char *name, struct symbol *sym); 7661f5207b7SJohn Levon 7671f5207b7SJohn Levon void __print_cur_stree(void); 7681f5207b7SJohn Levon 7691f5207b7SJohn Levon /* smatch_hooks.c */ 7701f5207b7SJohn Levon void __pass_to_client(void *data, enum hook_type type); 7711f5207b7SJohn Levon void __pass_to_client_no_data(enum hook_type type); 7721f5207b7SJohn Levon void __pass_case_to_client(struct expression *switch_expr, 7731f5207b7SJohn Levon struct range_list *rl); 7741f5207b7SJohn Levon int __has_merge_function(int client_id); 7751f5207b7SJohn Levon struct smatch_state *__client_merge_function(int owner, 7761f5207b7SJohn Levon struct smatch_state *s1, 7771f5207b7SJohn Levon struct smatch_state *s2); 7781f5207b7SJohn Levon struct smatch_state *__client_unmatched_state_function(struct sm_state *sm); 7791f5207b7SJohn Levon void call_pre_merge_hook(struct sm_state *sm); 7801f5207b7SJohn Levon void __push_scope_hooks(void); 7811f5207b7SJohn Levon void __call_scope_hooks(void); 7821f5207b7SJohn Levon 7831f5207b7SJohn Levon /* smatch_function_hooks.c */ 7841f5207b7SJohn Levon void create_function_hook_hash(void); 7851f5207b7SJohn Levon void __match_initializer_call(struct symbol *sym); 7861f5207b7SJohn Levon 7871f5207b7SJohn Levon /* smatch_db.c */ 7881f5207b7SJohn Levon enum info_type { 7891f5207b7SJohn Levon INTERNAL = 0, 7901f5207b7SJohn Levon /* 7911f5207b7SJohn Levon * Changing these numbers is a pain. Don't do it. If you ever use a 7921f5207b7SJohn Levon * number it can't be re-used right away so there may be gaps. 7931f5207b7SJohn Levon * We select these in order by type so if the order matters, then give 7941f5207b7SJohn Levon * it a number below 100-999,9000-9999 ranges. */ 7951f5207b7SJohn Levon 7961f5207b7SJohn Levon PARAM_CLEARED = 101, 7971f5207b7SJohn Levon PARAM_LIMIT = 103, 7981f5207b7SJohn Levon PARAM_FILTER = 104, 7991f5207b7SJohn Levon 8001f5207b7SJohn Levon PARAM_VALUE = 1001, 8011f5207b7SJohn Levon BUF_SIZE = 1002, 8021f5207b7SJohn Levon CAPPED_DATA = 1004, 8031f5207b7SJohn Levon RETURN_VALUE = 1005, 8041f5207b7SJohn Levon DEREFERENCE = 1006, 8051f5207b7SJohn Levon RANGE_CAP = 1007, 8061f5207b7SJohn Levon LOCK_HELD = 1008, 8071f5207b7SJohn Levon LOCK_RELEASED = 1009, 8081f5207b7SJohn Levon ABSOLUTE_LIMITS = 1010, 8091f5207b7SJohn Levon PARAM_ADD = 1012, 8101f5207b7SJohn Levon PARAM_FREED = 1013, 8111f5207b7SJohn Levon DATA_SOURCE = 1014, 8121f5207b7SJohn Levon FUZZY_MAX = 1015, 813*efe51d0cSJohn Levon HARD_MAX = 2015, 8141f5207b7SJohn Levon STR_LEN = 1016, 8151f5207b7SJohn Levon ARRAY_LEN = 1017, 8161f5207b7SJohn Levon CAPABLE = 1018, 8171f5207b7SJohn Levon NS_CAPABLE = 1019, 8181f5207b7SJohn Levon CONTAINER = 1020, 8191f5207b7SJohn Levon CASTED_CALL = 1021, 8201f5207b7SJohn Levon TYPE_LINK = 1022, 8211f5207b7SJohn Levon UNTRACKED_PARAM = 1023, 822*efe51d0cSJohn Levon LOST_PARAM = 2023, 8231f5207b7SJohn Levon CULL_PATH = 1024, 8241f5207b7SJohn Levon PARAM_SET = 1025, 8251f5207b7SJohn Levon PARAM_USED = 1026, 8261f5207b7SJohn Levon BYTE_UNITS = 1027, 8271f5207b7SJohn Levon COMPARE_LIMIT = 1028, 8281f5207b7SJohn Levon PARAM_COMPARE = 1029, 8291f5207b7SJohn Levon CONSTRAINT = 1031, 8301f5207b7SJohn Levon PASSES_TYPE = 1032, 8311f5207b7SJohn Levon CONSTRAINT_REQUIRED = 1033, 832*efe51d0cSJohn Levon BIT_INFO = 1034, 8331f5207b7SJohn Levon NOSPEC = 1035, 8341f5207b7SJohn Levon NOSPEC_WB = 1036, 8351f5207b7SJohn Levon STMT_CNT = 1037, 8361f5207b7SJohn Levon TERMINATED = 1038, 8371f5207b7SJohn Levon 8381f5207b7SJohn Levon /* put random temporary stuff in the 7000-7999 range for testing */ 839*efe51d0cSJohn Levon USER_DATA = 8017, 840*efe51d0cSJohn Levon USER_DATA_SET = 9017, 8411f5207b7SJohn Levon NO_OVERFLOW = 8018, 8421f5207b7SJohn Levon NO_OVERFLOW_SIMPLE = 8019, 8431f5207b7SJohn Levon LOCKED = 8020, 8441f5207b7SJohn Levon UNLOCKED = 8021, 8451f5207b7SJohn Levon SET_FS = 8022, 8461f5207b7SJohn Levon ATOMIC_INC = 8023, 8471f5207b7SJohn Levon ATOMIC_DEC = 8024, 8481f5207b7SJohn Levon NO_SIDE_EFFECT = 8025, 8491f5207b7SJohn Levon FN_ARG_LINK = 8028, 8501f5207b7SJohn Levon DATA_VALUE = 8029, 8511f5207b7SJohn Levon ARRAYSIZE_ARG = 8033, 8521f5207b7SJohn Levon SIZEOF_ARG = 8034, 8531f5207b7SJohn Levon MEMORY_TAG = 8036, 8541f5207b7SJohn Levon MTAG_ASSIGN = 8035, 8551f5207b7SJohn Levon STRING_VALUE = 8041, 856*efe51d0cSJohn Levon 857*efe51d0cSJohn Levon BYTE_COUNT = 8050, 858*efe51d0cSJohn Levon ELEM_COUNT = 8051, 859*efe51d0cSJohn Levon ELEM_LAST = 8052, 860*efe51d0cSJohn Levon USED_LAST = 8053, 861*efe51d0cSJohn Levon USED_COUNT = 8054, 8621f5207b7SJohn Levon }; 8631f5207b7SJohn Levon 8641f5207b7SJohn Levon extern struct sqlite3 *smatch_db; 8651f5207b7SJohn Levon extern struct sqlite3 *mem_db; 8661f5207b7SJohn Levon extern struct sqlite3 *cache_db; 8671f5207b7SJohn Levon 8681f5207b7SJohn Levon void db_ignore_states(int id); 8691f5207b7SJohn Levon void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type); 8701f5207b7SJohn Levon void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm)); 8711f5207b7SJohn Levon void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr)); 8721f5207b7SJohn Levon 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)); 8731f5207b7SJohn Levon void select_call_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value)); 8741f5207b7SJohn Levon void select_return_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value)); 8751f5207b7SJohn Levon struct range_list *db_return_vals(struct expression *expr); 8761f5207b7SJohn Levon struct range_list *db_return_vals_from_str(const char *fn_name); 8771f5207b7SJohn Levon char *return_state_to_var_sym(struct expression *expr, int param, const char *key, struct symbol **sym); 8781f5207b7SJohn Levon char *get_chunk_from_key(struct expression *arg, char *key, struct symbol **sym, struct var_sym_list **vsl); 8791f5207b7SJohn Levon char *get_variable_from_key(struct expression *arg, const char *key, struct symbol **sym); 8801f5207b7SJohn Levon const char *state_name_to_param_name(const char *state_name, const char *param_name); 8811f5207b7SJohn Levon const char *get_param_name_var_sym(const char *name, struct symbol *sym); 8821f5207b7SJohn Levon const char *get_param_name(struct sm_state *sm); 8831f5207b7SJohn Levon const char *get_mtag_name_var_sym(const char *state_name, struct symbol *sym); 8841f5207b7SJohn Levon const char *get_mtag_name_expr(struct expression *expr); 8851f5207b7SJohn Levon char *get_data_info_name(struct expression *expr); 886*efe51d0cSJohn Levon int is_recursive_member(const char *param_name); 8871f5207b7SJohn Levon 8881f5207b7SJohn Levon char *escape_newlines(const char *str); 8891f5207b7SJohn Levon void sql_exec(struct sqlite3 *db, int (*callback)(void*, int, char**, char**), void *data, const char *sql); 8901f5207b7SJohn Levon 8911f5207b7SJohn Levon #define sql_helper(db, call_back, data, sql...) \ 8921f5207b7SJohn Levon do { \ 8931f5207b7SJohn Levon char sql_txt[1024]; \ 8941f5207b7SJohn Levon \ 8951f5207b7SJohn Levon sqlite3_snprintf(sizeof(sql_txt), sql_txt, sql); \ 8961f5207b7SJohn Levon sm_debug("debug: %s\n", sql_txt); \ 8971f5207b7SJohn Levon sql_exec(db, call_back, data, sql_txt); \ 8981f5207b7SJohn Levon } while (0) 8991f5207b7SJohn Levon 9001f5207b7SJohn Levon 9011f5207b7SJohn Levon #define run_sql(call_back, data, sql...) \ 9021f5207b7SJohn Levon do { \ 9031f5207b7SJohn Levon if (option_no_db) \ 9041f5207b7SJohn Levon break; \ 9051f5207b7SJohn Levon sql_helper(smatch_db, call_back, data, sql); \ 9061f5207b7SJohn Levon } while (0) 9071f5207b7SJohn Levon 9081f5207b7SJohn Levon #define mem_sql(call_back, data, sql...) \ 9091f5207b7SJohn Levon sql_helper(mem_db, call_back, data, sql) 9101f5207b7SJohn Levon 9111f5207b7SJohn Levon #define cache_sql(call_back, data, sql...) \ 9121f5207b7SJohn Levon sql_helper(cache_db, call_back, data, sql) 9131f5207b7SJohn Levon 9141f5207b7SJohn Levon #define sql_insert_helper(table, db, ignore, late, values...) \ 9151f5207b7SJohn Levon do { \ 9161f5207b7SJohn Levon struct sqlite3 *_db = db; \ 9171f5207b7SJohn Levon \ 9181f5207b7SJohn Levon if (__inline_fn && !_db) \ 9191f5207b7SJohn Levon _db = mem_db; \ 9201f5207b7SJohn Levon if (_db) { \ 9211f5207b7SJohn Levon char buf[1024]; \ 9221f5207b7SJohn Levon char *err, *p = buf; \ 9231f5207b7SJohn Levon int rc; \ 9241f5207b7SJohn Levon \ 9251f5207b7SJohn Levon p += snprintf(p, buf + sizeof(buf) - p, \ 9261f5207b7SJohn Levon "insert %sinto %s values (", \ 9271f5207b7SJohn Levon ignore ? "or ignore " : "", #table); \ 9281f5207b7SJohn Levon p += snprintf(p, buf + sizeof(buf) - p, values); \ 9291f5207b7SJohn Levon p += snprintf(p, buf + sizeof(buf) - p, ");"); \ 9301f5207b7SJohn Levon sm_debug("mem-db: %s\n", buf); \ 9311f5207b7SJohn Levon rc = sqlite3_exec(_db, buf, NULL, NULL, &err); \ 9321f5207b7SJohn Levon if (rc != SQLITE_OK) { \ 9331f5207b7SJohn Levon sm_ierror("SQL error #2: %s", err); \ 9341f5207b7SJohn Levon sm_ierror("SQL: '%s'", buf); \ 9351f5207b7SJohn Levon parse_error = 1; \ 9361f5207b7SJohn Levon } \ 9371f5207b7SJohn Levon break; \ 9381f5207b7SJohn Levon } \ 9391f5207b7SJohn Levon if (option_info) { \ 9401f5207b7SJohn Levon FILE *tmp_fd = sm_outfd; \ 9411f5207b7SJohn Levon sm_outfd = sql_outfd; \ 9421f5207b7SJohn Levon sm_prefix(); \ 9431f5207b7SJohn Levon sm_printf("SQL%s: insert %sinto " #table " values(", \ 9441f5207b7SJohn Levon late ? "_late" : "", ignore ? "or ignore " : ""); \ 9451f5207b7SJohn Levon sm_printf(values); \ 9461f5207b7SJohn Levon sm_printf(");\n"); \ 9471f5207b7SJohn Levon sm_outfd = tmp_fd; \ 9481f5207b7SJohn Levon } \ 9491f5207b7SJohn Levon } while (0) 9501f5207b7SJohn Levon 9511f5207b7SJohn Levon #define sql_insert(table, values...) sql_insert_helper(table, 0, 0, 0, values); 9521f5207b7SJohn Levon #define sql_insert_or_ignore(table, values...) sql_insert_helper(table, 0, 1, 0, values); 9531f5207b7SJohn Levon #define sql_insert_late(table, values...) sql_insert_helper(table, 0, 0, 1, values); 9541f5207b7SJohn Levon #define sql_insert_cache(table, values...) sql_insert_helper(table, cache_db, 1, 0, values); 9551f5207b7SJohn Levon 9561f5207b7SJohn Levon char *get_static_filter(struct symbol *sym); 9571f5207b7SJohn Levon 9581f5207b7SJohn Levon void sql_insert_return_states(int return_id, const char *return_ranges, 9591f5207b7SJohn Levon int type, int param, const char *key, const char *value); 9601f5207b7SJohn Levon void sql_insert_caller_info(struct expression *call, int type, int param, 9611f5207b7SJohn Levon const char *key, const char *value); 9621f5207b7SJohn Levon void sql_insert_function_ptr(const char *fn, const char *struct_name); 9631f5207b7SJohn Levon void sql_insert_return_values(const char *return_values); 9641f5207b7SJohn Levon void sql_insert_return_implies(int type, int param, const char *key, const char *value); 9651f5207b7SJohn Levon void sql_insert_function_type_size(const char *member, const char *ranges); 9661f5207b7SJohn Levon void sql_insert_function_type_info(int type, const char *struct_type, const char *member, const char *value); 9671f5207b7SJohn Levon void sql_insert_type_info(int type, const char *member, const char *value); 9681f5207b7SJohn Levon void sql_insert_local_values(const char *name, const char *value); 9691f5207b7SJohn Levon void sql_insert_function_type_value(const char *type, const char *value); 9701f5207b7SJohn Levon void sql_insert_function_type(int param, const char *value); 9711f5207b7SJohn Levon void sql_insert_parameter_name(int param, const char *value); 9721f5207b7SJohn Levon void sql_insert_data_info(struct expression *data, int type, const char *value); 9731f5207b7SJohn Levon void sql_insert_data_info_var_sym(const char *var, struct symbol *sym, int type, const char *value); 9741f5207b7SJohn Levon void sql_save_constraint(const char *con); 9751f5207b7SJohn Levon void sql_save_constraint_required(const char *data, int op, const char *limit); 9761f5207b7SJohn Levon void sql_copy_constraint_required(const char *new_limit, const char *old_limit); 9771f5207b7SJohn Levon void sql_insert_fn_ptr_data_link(const char *ptr, const char *data); 9781f5207b7SJohn Levon void sql_insert_fn_data_link(struct expression *fn, int type, int param, const char *key, const char *value); 9791f5207b7SJohn Levon void sql_insert_mtag_about(mtag_t tag, const char *left_name, const char *right_name); 9801f5207b7SJohn Levon void sql_insert_mtag_map(mtag_t tag, int offset, mtag_t container); 9811f5207b7SJohn Levon void sql_insert_mtag_alias(mtag_t orig, mtag_t alias); 9821f5207b7SJohn Levon int mtag_map_select_container(mtag_t tag, int offset, mtag_t *container); 9831f5207b7SJohn Levon int mtag_map_select_tag(mtag_t container, int offset, mtag_t *tag); 984*efe51d0cSJohn Levon struct smatch_state *swap_mtag_return(struct expression *expr, struct smatch_state *state); 985*efe51d0cSJohn Levon struct range_list *swap_mtag_seed(struct expression *expr, struct range_list *rl); 9861f5207b7SJohn Levon 9871f5207b7SJohn Levon void sql_select_return_states(const char *cols, struct expression *call, 9881f5207b7SJohn Levon int (*callback)(void*, int, char**, char**), void *info); 9891f5207b7SJohn Levon void sql_select_call_implies(const char *cols, struct expression *call, 9901f5207b7SJohn Levon int (*callback)(void*, int, char**, char**)); 9911f5207b7SJohn Levon 9921f5207b7SJohn Levon void open_smatch_db(char *db_file); 9931f5207b7SJohn Levon 9941f5207b7SJohn Levon /* smatch_files.c */ 9951f5207b7SJohn Levon int open_data_file(const char *filename); 9961f5207b7SJohn Levon int open_schema_file(const char *schema); 9971f5207b7SJohn Levon struct token *get_tokens_file(const char *filename); 9981f5207b7SJohn Levon 9991f5207b7SJohn Levon /* smatch.c */ 10001f5207b7SJohn Levon extern char *option_debug_check; 10011f5207b7SJohn Levon extern char *option_project_str; 10021f5207b7SJohn Levon extern char *bin_dir; 10031f5207b7SJohn Levon extern char *data_dir; 10041f5207b7SJohn Levon extern int option_no_data; 10051f5207b7SJohn Levon extern int option_full_path; 10061f5207b7SJohn Levon extern int option_param_mapper; 10071f5207b7SJohn Levon extern int option_call_tree; 10081f5207b7SJohn Levon extern int num_checks; 10091f5207b7SJohn Levon 10101f5207b7SJohn Levon enum project_type { 10111f5207b7SJohn Levon PROJ_NONE, 10121f5207b7SJohn Levon PROJ_KERNEL, 10131f5207b7SJohn Levon PROJ_WINE, 10141f5207b7SJohn Levon PROJ_ILLUMOS_KERNEL, 10151f5207b7SJohn Levon PROJ_ILLUMOS_USER, 10161f5207b7SJohn Levon PROJ_UNKNOWN, 10171f5207b7SJohn Levon }; 10181f5207b7SJohn Levon extern enum project_type option_project; 10191f5207b7SJohn Levon const char *check_name(unsigned short id); 10201f5207b7SJohn Levon int id_from_name(const char *name); 10211f5207b7SJohn Levon 10221f5207b7SJohn Levon 10231f5207b7SJohn Levon /* smatch_buf_size.c */ 10241f5207b7SJohn Levon int get_array_size(struct expression *expr); 10251f5207b7SJohn Levon int get_array_size_bytes(struct expression *expr); 10261f5207b7SJohn Levon int get_array_size_bytes_min(struct expression *expr); 10271f5207b7SJohn Levon int get_array_size_bytes_max(struct expression *expr); 10281f5207b7SJohn Levon struct range_list *get_array_size_bytes_rl(struct expression *expr); 10291f5207b7SJohn Levon int get_real_array_size(struct expression *expr); 10301f5207b7SJohn Levon int last_member_is_resizable(struct symbol *type); 10311f5207b7SJohn Levon /* smatch_strlen.c */ 10321f5207b7SJohn Levon int get_implied_strlen(struct expression *expr, struct range_list **rl); 10331f5207b7SJohn Levon int get_size_from_strlen(struct expression *expr); 10341f5207b7SJohn Levon 10351f5207b7SJohn Levon /* smatch_capped.c */ 10361f5207b7SJohn Levon int is_capped(struct expression *expr); 10371f5207b7SJohn Levon int is_capped_var_sym(const char *name, struct symbol *sym); 10381f5207b7SJohn Levon 10391f5207b7SJohn Levon /* check_user_data.c */ 10401f5207b7SJohn Levon int is_user_macro(struct expression *expr); 10411f5207b7SJohn Levon int is_capped_user_data(struct expression *expr); 10421f5207b7SJohn Levon int implied_user_data(struct expression *expr, struct range_list **rl); 10431f5207b7SJohn Levon struct stree *get_user_stree(void); 10441f5207b7SJohn Levon int get_user_rl(struct expression *expr, struct range_list **rl); 10451f5207b7SJohn Levon int is_user_rl(struct expression *expr); 10461f5207b7SJohn Levon int get_user_rl_var_sym(const char *name, struct symbol *sym, struct range_list **rl); 1047*efe51d0cSJohn Levon bool user_rl_capped(struct expression *expr); 1048*efe51d0cSJohn Levon struct range_list *var_user_rl(struct expression *expr); 10491f5207b7SJohn Levon 10501f5207b7SJohn Levon /* check_locking.c */ 10511f5207b7SJohn Levon void print_held_locks(); 10521f5207b7SJohn Levon 10531f5207b7SJohn Levon /* check_assigned_expr.c */ 10541f5207b7SJohn Levon struct expression *get_assigned_expr(struct expression *expr); 10551f5207b7SJohn Levon struct expression *get_assigned_expr_name_sym(const char *name, struct symbol *sym); 10561f5207b7SJohn Levon /* smatch_return_to_param.c */ 10571f5207b7SJohn Levon void __add_return_to_param_mapping(struct expression *assign, const char *return_string); 10581f5207b7SJohn Levon char *map_call_to_param_name_sym(struct expression *expr, struct symbol **sym); 10591f5207b7SJohn Levon 10601f5207b7SJohn Levon /* smatch_comparison.c */ 10611f5207b7SJohn Levon struct compare_data { 10621f5207b7SJohn Levon /* The ->left and ->right expression pointers might be NULL (I'm lazy) */ 10631f5207b7SJohn Levon struct expression *left; 10641f5207b7SJohn Levon const char *left_var; 10651f5207b7SJohn Levon struct var_sym_list *left_vsl; 10661f5207b7SJohn Levon int comparison; 10671f5207b7SJohn Levon struct expression *right; 10681f5207b7SJohn Levon const char *right_var; 10691f5207b7SJohn Levon struct var_sym_list *right_vsl; 10701f5207b7SJohn Levon }; 10711f5207b7SJohn Levon DECLARE_ALLOCATOR(compare_data); 10721f5207b7SJohn Levon struct smatch_state *alloc_compare_state( 10731f5207b7SJohn Levon struct expression *left, 10741f5207b7SJohn Levon const char *left_var, struct var_sym_list *left_vsl, 10751f5207b7SJohn Levon int comparison, 10761f5207b7SJohn Levon struct expression *right, 10771f5207b7SJohn Levon const char *right_var, struct var_sym_list *right_vsl); 10781f5207b7SJohn Levon int filter_comparison(int orig, int op); 10791f5207b7SJohn Levon int merge_comparisons(int one, int two); 10801f5207b7SJohn Levon int combine_comparisons(int left_compare, int right_compare); 10811f5207b7SJohn Levon int state_to_comparison(struct smatch_state *state); 10821f5207b7SJohn Levon struct smatch_state *merge_compare_states(struct smatch_state *s1, struct smatch_state *s2); 10831f5207b7SJohn Levon int get_comparison(struct expression *left, struct expression *right); 1084*efe51d0cSJohn Levon int get_comparison_no_extra(struct expression *a, struct expression *b); 10851f5207b7SJohn Levon int get_comparison_strings(const char *one, const char *two); 10861f5207b7SJohn Levon int possible_comparison(struct expression *a, int comparison, struct expression *b); 10871f5207b7SJohn Levon struct state_list *get_all_comparisons(struct expression *expr); 10881f5207b7SJohn Levon struct state_list *get_all_possible_equal_comparisons(struct expression *expr); 10891f5207b7SJohn Levon void __add_return_comparison(struct expression *call, const char *range); 10901f5207b7SJohn Levon void __add_comparison_info(struct expression *expr, struct expression *call, const char *range); 10911f5207b7SJohn Levon char *get_printed_param_name(struct expression *call, const char *param_name, struct symbol *param_sym); 10921f5207b7SJohn Levon char *name_sym_to_param_comparison(const char *name, struct symbol *sym); 10931f5207b7SJohn Levon char *expr_equal_to_param(struct expression *expr, int ignore); 10941f5207b7SJohn Levon char *expr_lte_to_param(struct expression *expr, int ignore); 10951f5207b7SJohn Levon char *expr_param_comparison(struct expression *expr, int ignore); 10961f5207b7SJohn Levon int flip_comparison(int op); 10971f5207b7SJohn Levon int negate_comparison(int op); 10981f5207b7SJohn Levon int remove_unsigned_from_comparison(int op); 10991f5207b7SJohn Levon int param_compare_limit_is_impossible(struct expression *expr, int left_param, char *left_key, char *value); 11001f5207b7SJohn Levon void filter_by_comparison(struct range_list **rl, int comparison, struct range_list *right); 11011f5207b7SJohn Levon struct sm_state *comparison_implication_hook(struct expression *expr, 11021f5207b7SJohn Levon struct state_list **true_stack, 11031f5207b7SJohn Levon struct state_list **false_stack); 11041f5207b7SJohn Levon void __compare_param_limit_hook(struct expression *left_expr, struct expression *right_expr, 11051f5207b7SJohn Levon const char *state_name, 11061f5207b7SJohn Levon struct smatch_state *true_state, struct smatch_state *false_state); 11071f5207b7SJohn Levon int impossibly_high_comparison(struct expression *expr); 11081f5207b7SJohn Levon 11091f5207b7SJohn Levon /* smatch_sval.c */ 11101f5207b7SJohn Levon sval_t *sval_alloc(sval_t sval); 11111f5207b7SJohn Levon sval_t *sval_alloc_permanent(sval_t sval); 11121f5207b7SJohn Levon sval_t sval_blank(struct expression *expr); 11131f5207b7SJohn Levon sval_t sval_type_val(struct symbol *type, long long val); 11141f5207b7SJohn Levon sval_t sval_from_val(struct expression *expr, long long val); 11151f5207b7SJohn Levon int sval_is_ptr(sval_t sval); 11161f5207b7SJohn Levon int sval_unsigned(sval_t sval); 11171f5207b7SJohn Levon int sval_signed(sval_t sval); 11181f5207b7SJohn Levon int sval_bits(sval_t sval); 11191f5207b7SJohn Levon int sval_bits_used(sval_t sval); 11201f5207b7SJohn Levon int sval_is_negative(sval_t sval); 11211f5207b7SJohn Levon int sval_is_positive(sval_t sval); 11221f5207b7SJohn Levon int sval_is_min(sval_t sval); 11231f5207b7SJohn Levon int sval_is_max(sval_t sval); 11241f5207b7SJohn Levon int sval_is_a_min(sval_t sval); 11251f5207b7SJohn Levon int sval_is_a_max(sval_t sval); 11261f5207b7SJohn Levon int sval_is_negative_min(sval_t sval); 11271f5207b7SJohn Levon int sval_cmp_t(struct symbol *type, sval_t one, sval_t two); 11281f5207b7SJohn Levon int sval_cmp_val(sval_t one, long long val); 11291f5207b7SJohn Levon sval_t sval_min(sval_t one, sval_t two); 11301f5207b7SJohn Levon sval_t sval_max(sval_t one, sval_t two); 11311f5207b7SJohn Levon int sval_too_low(struct symbol *type, sval_t sval); 11321f5207b7SJohn Levon int sval_too_high(struct symbol *type, sval_t sval); 11331f5207b7SJohn Levon int sval_fits(struct symbol *type, sval_t sval); 11341f5207b7SJohn Levon sval_t sval_cast(struct symbol *type, sval_t sval); 11351f5207b7SJohn Levon sval_t sval_preop(sval_t sval, int op); 11361f5207b7SJohn Levon sval_t sval_binop(sval_t left, int op, sval_t right); 11371f5207b7SJohn Levon int sval_binop_overflows(sval_t left, int op, sval_t right); 11381f5207b7SJohn Levon int sval_binop_overflows_no_sign(sval_t left, int op, sval_t right); 1139*efe51d0cSJohn Levon int find_first_zero_bit(unsigned long long uvalue); 1140*efe51d0cSJohn Levon int sm_fls64(unsigned long long uvalue); 11411f5207b7SJohn Levon unsigned long long fls_mask(unsigned long long uvalue); 11421f5207b7SJohn Levon unsigned long long sval_fls_mask(sval_t sval); 11431f5207b7SJohn Levon const char *sval_to_str(sval_t sval); 1144*efe51d0cSJohn Levon const char *sval_to_str_or_err_ptr(sval_t sval); 11451f5207b7SJohn Levon const char *sval_to_numstr(sval_t sval); 11461f5207b7SJohn Levon sval_t ll_to_sval(long long val); 11471f5207b7SJohn Levon 11481f5207b7SJohn Levon /* smatch_string_list.c */ 11491f5207b7SJohn Levon int list_has_string(struct string_list *str_list, const char *str); 1150*efe51d0cSJohn Levon int insert_string(struct string_list **str_list, const char *str); 11511f5207b7SJohn Levon struct string_list *clone_str_list(struct string_list *orig); 11521f5207b7SJohn Levon struct string_list *combine_string_lists(struct string_list *one, struct string_list *two); 11531f5207b7SJohn Levon 11541f5207b7SJohn Levon /* smatch_start_states.c */ 11551f5207b7SJohn Levon struct stree *get_start_states(void); 11561f5207b7SJohn Levon 11571f5207b7SJohn Levon /* smatch_recurse.c */ 11581f5207b7SJohn Levon int has_symbol(struct expression *expr, struct symbol *sym); 11591f5207b7SJohn Levon int has_variable(struct expression *expr, struct expression *var); 11601f5207b7SJohn Levon int has_inc_dec(struct expression *expr); 11611f5207b7SJohn Levon 11621f5207b7SJohn Levon /* smatch_stored_conditions.c */ 11631f5207b7SJohn Levon struct smatch_state *get_stored_condition(struct expression *expr); 11641f5207b7SJohn Levon struct expression_list *get_conditions(struct expression *expr); 11651f5207b7SJohn Levon struct sm_state *stored_condition_implication_hook(struct expression *expr, 11661f5207b7SJohn Levon struct state_list **true_stack, 11671f5207b7SJohn Levon struct state_list **false_stack); 11681f5207b7SJohn Levon 11691f5207b7SJohn Levon /* check_string_len.c */ 11701f5207b7SJohn Levon int get_formatted_string_size(struct expression *call, int arg); 1171*efe51d0cSJohn Levon int get_formatted_string_min_size(struct expression *call, int arg); 11721f5207b7SJohn Levon 11731f5207b7SJohn Levon /* smatch_param_set.c */ 11741f5207b7SJohn Levon int param_was_set(struct expression *expr); 11751f5207b7SJohn Levon int param_was_set_var_sym(const char *name, struct symbol *sym); 11761f5207b7SJohn Levon /* smatch_param_filter.c */ 11771f5207b7SJohn Levon int param_has_filter_data(struct sm_state *sm); 11781f5207b7SJohn Levon 11791f5207b7SJohn Levon /* smatch_links.c */ 11801f5207b7SJohn Levon void set_up_link_functions(int id, int linkid); 11811f5207b7SJohn Levon struct smatch_state *merge_link_states(struct smatch_state *s1, struct smatch_state *s2); 11821f5207b7SJohn Levon void store_link(int link_id, const char *name, struct symbol *sym, const char *link_name, struct symbol *link_sym); 11831f5207b7SJohn Levon 11841f5207b7SJohn Levon /* smatch_auto_copy.c */ 11851f5207b7SJohn Levon void set_auto_copy(int owner); 11861f5207b7SJohn Levon 11871f5207b7SJohn Levon /* check_buf_comparison */ 1188*efe51d0cSJohn Levon const char *limit_type_str(unsigned int limit_type); 1189*efe51d0cSJohn Levon struct expression *get_size_variable(struct expression *buf, int *limit_type); 11901f5207b7SJohn Levon struct expression *get_array_variable(struct expression *size); 1191*efe51d0cSJohn Levon int buf_comparison_index_ok(struct expression *expr); 11921f5207b7SJohn Levon 11931f5207b7SJohn Levon /* smatch_untracked_param.c */ 11941f5207b7SJohn Levon void mark_untracked(struct expression *expr, int param, const char *key, const char *value); 11951f5207b7SJohn Levon void add_untracked_param_hook(void (func)(struct expression *call, int param)); 1196*efe51d0cSJohn Levon void add_lost_param_hook(void (func)(struct expression *call, int param)); 11971f5207b7SJohn Levon void mark_all_params_untracked(int return_id, char *return_ranges, struct expression *expr); 11981f5207b7SJohn Levon 11991f5207b7SJohn Levon /* smatch_strings.c */ 12001f5207b7SJohn Levon struct state_list *get_strings(struct expression *expr); 12011f5207b7SJohn Levon struct expression *fake_string_from_mtag(mtag_t tag); 12021f5207b7SJohn Levon 12031f5207b7SJohn Levon /* smatch_estate.c */ 12041f5207b7SJohn Levon int estate_get_single_value(struct smatch_state *state, sval_t *sval); 12051f5207b7SJohn Levon 12061f5207b7SJohn Levon /* smatch_address.c */ 12071f5207b7SJohn Levon int get_address_rl(struct expression *expr, struct range_list **rl); 12081f5207b7SJohn Levon int get_member_offset(struct symbol *type, const char *member_name); 12091f5207b7SJohn Levon int get_member_offset_from_deref(struct expression *expr); 12101f5207b7SJohn Levon 12111f5207b7SJohn Levon /* for now this is in smatch_used_parameter.c */ 12121f5207b7SJohn Levon void __get_state_hook(int owner, const char *name, struct symbol *sym); 12131f5207b7SJohn Levon 12141f5207b7SJohn Levon /* smatch_buf_comparison.c */ 12151f5207b7SJohn Levon int db_var_is_array_limit(struct expression *array, const char *name, struct var_sym_list *vsl); 12161f5207b7SJohn Levon 12171f5207b7SJohn Levon struct stree *get_all_return_states(void); 12181f5207b7SJohn Levon struct stree_stack *get_all_return_strees(void); 12191f5207b7SJohn Levon int on_atomic_dec_path(void); 12201f5207b7SJohn Levon int was_inced(const char *name, struct symbol *sym); 12211f5207b7SJohn Levon 12221f5207b7SJohn Levon /* smatch_constraints.c */ 12231f5207b7SJohn Levon char *get_constraint_str(struct expression *expr); 12241f5207b7SJohn Levon struct constraint_list *get_constraints(struct expression *expr); 12251f5207b7SJohn Levon char *unmet_constraint(struct expression *data, struct expression *offset); 12261f5207b7SJohn Levon char *get_required_constraint(const char *data_str); 12271f5207b7SJohn Levon 12281f5207b7SJohn Levon /* smatch_container_of.c */ 12291f5207b7SJohn Levon int get_param_from_container_of(struct expression *expr); 12301f5207b7SJohn Levon int get_offset_from_container_of(struct expression *expr); 1231*efe51d0cSJohn Levon char *get_container_name(struct expression *container, struct expression *expr); 12321f5207b7SJohn Levon 12331f5207b7SJohn Levon /* smatch_mtag.c */ 12341f5207b7SJohn Levon int get_string_mtag(struct expression *expr, mtag_t *tag); 12351f5207b7SJohn Levon int get_toplevel_mtag(struct symbol *sym, mtag_t *tag); 12361f5207b7SJohn Levon int create_mtag_alias(mtag_t tag, struct expression *expr, mtag_t *new); 12371f5207b7SJohn Levon int expr_to_mtag_offset(struct expression *expr, mtag_t *tag, int *offset); 12381f5207b7SJohn Levon void update_mtag_data(struct expression *expr); 12391f5207b7SJohn Levon int get_mtag_sval(struct expression *expr, sval_t *sval); 12401f5207b7SJohn Levon 12411f5207b7SJohn Levon /* Trinity fuzzer stuff */ 12421f5207b7SJohn Levon const char *get_syscall_arg_type(struct symbol *sym); 12431f5207b7SJohn Levon 1244*efe51d0cSJohn Levon /* smatch_bit_info.c */ 1245*efe51d0cSJohn Levon struct bit_info *get_bit_info(struct expression *expr); 1246*efe51d0cSJohn Levon struct bit_info *get_bit_info_var_sym(const char *name, struct symbol *sym); 12471f5207b7SJohn Levon /* smatch_mem_tracker.c */ 12481f5207b7SJohn Levon extern int option_mem; 1249*efe51d0cSJohn Levon unsigned long get_mem_kb(void); 12501f5207b7SJohn Levon unsigned long get_max_memory(void); 12511f5207b7SJohn Levon 12521f5207b7SJohn Levon /* check_is_nospec.c */ 12531f5207b7SJohn Levon bool is_nospec(struct expression *expr); 1254*efe51d0cSJohn Levon long get_stmt_cnt(void); 12551f5207b7SJohn Levon 12561f5207b7SJohn Levon /* smatch_nul_terminator.c */ 12571f5207b7SJohn Levon bool is_nul_terminated(struct expression *expr); 1258*efe51d0cSJohn Levon /* check_kernel.c */ 1259*efe51d0cSJohn Levon bool is_ignored_kernel_data(const char *name); 1260*efe51d0cSJohn Levon 1261*efe51d0cSJohn Levon static inline bool type_is_ptr(struct symbol *type) 1262*efe51d0cSJohn Levon { 1263*efe51d0cSJohn Levon return type && 1264*efe51d0cSJohn Levon (type->type == SYM_PTR || 1265*efe51d0cSJohn Levon type->type == SYM_ARRAY || 1266*efe51d0cSJohn Levon type->type == SYM_FN); 1267*efe51d0cSJohn Levon } 12681f5207b7SJohn Levon 12691f5207b7SJohn Levon static inline int type_bits(struct symbol *type) 12701f5207b7SJohn Levon { 12711f5207b7SJohn Levon if (!type) 12721f5207b7SJohn Levon return 0; 1273*efe51d0cSJohn Levon if (type_is_ptr(type)) 12741f5207b7SJohn Levon return bits_in_pointer; 12751f5207b7SJohn Levon if (!type->examined) 12761f5207b7SJohn Levon examine_symbol_type(type); 12771f5207b7SJohn Levon return type->bit_size; 12781f5207b7SJohn Levon } 12791f5207b7SJohn Levon 12801f5207b7SJohn Levon static inline int type_unsigned(struct symbol *base_type) 12811f5207b7SJohn Levon { 12821f5207b7SJohn Levon if (!base_type) 12831f5207b7SJohn Levon return 0; 1284*efe51d0cSJohn Levon if (is_ptr_type(base_type)) 1285*efe51d0cSJohn Levon return 1; 12861f5207b7SJohn Levon if (base_type->ctype.modifiers & MOD_UNSIGNED) 12871f5207b7SJohn Levon return 1; 12881f5207b7SJohn Levon return 0; 12891f5207b7SJohn Levon } 12901f5207b7SJohn Levon 12911f5207b7SJohn Levon static inline int type_positive_bits(struct symbol *type) 12921f5207b7SJohn Levon { 12931f5207b7SJohn Levon if (!type) 12941f5207b7SJohn Levon return 0; 1295*efe51d0cSJohn Levon if (is_ptr_type(type)) 1296*efe51d0cSJohn Levon return bits_in_pointer; 12971f5207b7SJohn Levon if (type_unsigned(type)) 12981f5207b7SJohn Levon return type_bits(type); 12991f5207b7SJohn Levon return type_bits(type) - 1; 13001f5207b7SJohn Levon } 13011f5207b7SJohn Levon 13021f5207b7SJohn Levon static inline int sval_positive_bits(sval_t sval) 13031f5207b7SJohn Levon { 13041f5207b7SJohn Levon return type_positive_bits(sval.type); 13051f5207b7SJohn Levon } 13061f5207b7SJohn Levon 13071f5207b7SJohn Levon /* 13081f5207b7SJohn Levon * Returns -1 if one is smaller, 0 if they are the same and 1 if two is larger. 13091f5207b7SJohn Levon */ 13101f5207b7SJohn Levon static inline int sval_cmp(sval_t one, sval_t two) 13111f5207b7SJohn Levon { 13121f5207b7SJohn Levon struct symbol *type; 13131f5207b7SJohn Levon 13141f5207b7SJohn Levon type = one.type; 13151f5207b7SJohn Levon if (sval_positive_bits(two) > sval_positive_bits(one)) 13161f5207b7SJohn Levon type = two.type; 13171f5207b7SJohn Levon if (type_bits(type) < 31) 13181f5207b7SJohn Levon type = &int_ctype; 13191f5207b7SJohn Levon 13201f5207b7SJohn Levon one = sval_cast(type, one); 13211f5207b7SJohn Levon two = sval_cast(type, two); 13221f5207b7SJohn Levon 13231f5207b7SJohn Levon if (type_unsigned(type)) { 13241f5207b7SJohn Levon if (one.uvalue < two.uvalue) 13251f5207b7SJohn Levon return -1; 13261f5207b7SJohn Levon if (one.uvalue == two.uvalue) 13271f5207b7SJohn Levon return 0; 13281f5207b7SJohn Levon return 1; 13291f5207b7SJohn Levon } 13301f5207b7SJohn Levon /* fix me handle type promotion and unsigned values */ 13311f5207b7SJohn Levon if (one.value < two.value) 13321f5207b7SJohn Levon return -1; 13331f5207b7SJohn Levon if (one.value == two.value) 13341f5207b7SJohn Levon return 0; 13351f5207b7SJohn Levon return 1; 13361f5207b7SJohn Levon } 13371f5207b7SJohn Levon 13381f5207b7SJohn Levon #endif /* !SMATCH_H_ */ 1339