Print this page
12166 resync smatch to 0.6.1-rc1-il-3


   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  * Copyright 2019 Joyent, Inc.
  18  */
  19 
  20 #ifndef         SMATCH_H_
  21 # define        SMATCH_H_
  22 
  23 #include <stdio.h>
  24 #include <string.h>
  25 #include <limits.h>

  26 #include <sys/time.h>
  27 #include <sqlite3.h>
  28 #include "lib.h"
  29 #include "allocate.h"
  30 #include "scope.h"
  31 #include "parse.h"
  32 #include "expression.h"
  33 #include "avl.h"
  34 
  35 typedef struct {
  36         struct symbol *type;
  37         union {
  38                 long long value;
  39                 unsigned long long uvalue;



  40         };
  41 } sval_t;
  42 
  43 typedef long long mtag_t;
  44 
  45 struct smatch_state {
  46         const char *name;
  47         void *data;
  48 };
  49 #define STATE(_x) static struct smatch_state _x = { .name = #_x }
  50 extern struct smatch_state undefined;
  51 extern struct smatch_state ghost;
  52 extern struct smatch_state merged;
  53 extern struct smatch_state true_state;
  54 extern struct smatch_state false_state;
  55 DECLARE_ALLOCATOR(smatch_state);
  56 
  57 static inline void *INT_PTR(int i)
  58 {
  59         return (void *)(long)i;


 189 
 190 struct modification_data {
 191         struct smatch_state *prev;
 192         struct expression *cur;
 193 };
 194 
 195 typedef void (modification_hook)(struct sm_state *sm, struct expression *mod_expr);
 196 void add_modification_hook(int owner, modification_hook *call_back);
 197 void add_modification_hook_late(int owner, modification_hook *call_back);
 198 struct smatch_state *get_modification_state(struct expression *expr);
 199 
 200 int outside_of_function(void);
 201 const char *get_filename(void);
 202 const char *get_base_file(void);
 203 char *get_function(void);
 204 int get_lineno(void);
 205 extern int final_pass;
 206 extern struct symbol *cur_func_sym;
 207 extern int option_debug;
 208 extern int local_debug;

 209 bool debug_implied(void);
 210 extern int option_info;
 211 extern int option_spammy;
 212 extern int option_timeout;
 213 extern char *trace_variable;
 214 extern struct stree *global_states;
 215 int is_skipped_function(void);
 216 int is_silenced_function(void);
 217 extern bool implications_off;
 218 
 219 /* smatch_impossible.c */
 220 int is_impossible_path(void);
 221 void set_path_impossible(void);
 222 
 223 extern FILE *sm_outfd;
 224 extern FILE *sql_outfd;
 225 extern FILE *caller_info_fd;
 226 extern int sm_nr_checks;
 227 extern int sm_nr_errors;
 228 extern const char *progname;
 229 
 230 /*
 231  * How to use these routines:
 232  *
 233  * sm_fatal(): an internal error of some kind that should immediately exit
 234  * sm_ierror(): an internal error
 235  * sm_perror(): an internal error from parsing input source
 236  * sm_error(): an error from input source
 237  * sm_warning(): a warning from input source
 238  * sm_info(): info message (from option_info)
 239  * sm_debug(): debug message
 240  * sm_msg(): other message (please avoid using this)
 241  */
 242 
 243 #define sm_printf(msg...) do { if (final_pass || option_debug || local_debug) fprintf(sm_outfd, msg); } while (0)



 244 
 245 static inline void sm_prefix(void)
 246 {
 247         sm_printf("%s: %s:%d %s() ", progname, get_filename(), get_lineno(), get_function());
 248 }
 249 
 250 static inline void print_implied_debug_msg();
 251 
 252 extern bool __silence_warnings_for_stmt;
 253 
 254 #define sm_print_msg(type, msg...) \
 255 do {                                                           \
 256         print_implied_debug_msg();                             \
 257         if (!final_pass && !option_debug && !local_debug)      \
 258                 break;                                         \
 259         if (__silence_warnings_for_stmt && !option_debug && !local_debug) \
 260                 break;                                         \
 261         if (!option_info && is_silenced_function())            \
 262                 break;                                         \
 263         sm_prefix();                                           \
 264         if (type == 1) {                                       \
 265                 sm_printf("warn: ");                           \
 266                 sm_nr_checks++;                                \
 267         } else if (type == 2) {                                \
 268                 sm_printf("error: ");                          \
 269                 sm_nr_checks++;                                \
 270         } else if (type == 3) {                                \
 271                 sm_printf("parse error: ");                    \
 272                 sm_nr_errors++;                                \
 273         }                                                      \
 274         sm_printf(msg);                                        \
 275         sm_printf("\n");                                       \
 276 } while (0)
 277 
 278 #define sm_msg(msg...) do { sm_print_msg(0, msg); } while (0)
 279 
 280 #define local_debug(msg...)                                     \
 281 do {                                                            \
 282         if (local_debug)                                        \
 283                 sm_msg(msg);                                    \
 284 } while (0)
 285 
 286 extern char *implied_debug_msg;
 287 static inline void print_implied_debug_msg(void)
 288 {
 289         static struct symbol *last_printed = NULL;
 290 
 291         if (!implied_debug_msg)
 292                 return;
 293         if (last_printed == cur_func_sym)
 294                 return;
 295         last_printed = cur_func_sym;
 296         sm_msg("%s", implied_debug_msg);
 297 }
 298 
 299 #define sm_debug(msg...) do { if (option_debug) sm_printf(msg); } while (0)

 300 
 301 #define sm_info(msg...) do {                                    \
 302         if (option_debug || (option_info && final_pass)) {      \
 303                 sm_prefix();                                    \
 304                 sm_printf("info: ");                            \
 305                 sm_printf(msg);                                 \
 306                 sm_printf("\n");                                \
 307         }                                                       \
 308 } while(0)
 309 
 310 #define sm_warning(msg...) do { sm_print_msg(1, msg); } while (0)
 311 #define sm_error(msg...) do { sm_print_msg(2, msg); } while (0)
 312 #define sm_perror(msg...) do { sm_print_msg(3, msg); } while (0)
 313 
 314 static inline void sm_fatal(const char *fmt, ...)
 315 {
 316         va_list args;
 317 
 318         va_start(args, fmt);
 319         vfprintf(sm_outfd, fmt, args);


 421 struct expression *get_array_base(struct expression *expr);
 422 struct expression *get_array_offset(struct expression *expr);
 423 const char *show_state(struct smatch_state *state);
 424 struct statement *get_expression_statement(struct expression *expr);
 425 struct expression *strip_parens(struct expression *expr);
 426 struct expression *strip_expr(struct expression *expr);
 427 struct expression *strip_expr_set_parent(struct expression *expr);
 428 void scoped_state(int my_id, const char *name, struct symbol *sym);
 429 int is_error_return(struct expression *expr);
 430 int getting_address(struct expression *expr);
 431 int get_struct_and_member(struct expression *expr, const char **type, const char **member);
 432 char *get_member_name(struct expression *expr);
 433 char *get_fnptr_name(struct expression *expr);
 434 int cmp_pos(struct position pos1, struct position pos2);
 435 int positions_eq(struct position pos1, struct position pos2);
 436 struct statement *get_current_statement(void);
 437 struct statement *get_prev_statement(void);
 438 struct expression *get_last_expr_from_expression_stmt(struct expression *expr);
 439 int get_param_num_from_sym(struct symbol *sym);
 440 int get_param_num(struct expression *expr);

 441 int ms_since(struct timeval *start);
 442 int parent_is_gone_var_sym(const char *name, struct symbol *sym);
 443 int parent_is_gone(struct expression *expr);
 444 int invert_op(int op);
 445 int op_remove_assign(int op);
 446 int expr_equiv(struct expression *one, struct expression *two);
 447 void push_int(struct int_stack **stack, int num);
 448 int pop_int(struct int_stack **stack);
 449 
 450 /* smatch_type.c */
 451 struct symbol *get_real_base_type(struct symbol *sym);
 452 int type_bytes(struct symbol *type);
 453 int array_bytes(struct symbol *type);
 454 struct symbol *get_pointer_type(struct expression *expr);
 455 struct symbol *get_type(struct expression *expr);
 456 struct symbol *get_final_type(struct expression *expr);
 457 struct symbol *get_promoted_type(struct symbol *left, struct symbol *right);
 458 int type_signed(struct symbol *base_type);
 459 int expr_unsigned(struct expression *expr);
 460 int expr_signed(struct expression *expr);


 789 
 790 /* smatch_db.c */
 791 enum info_type {
 792         INTERNAL        = 0,
 793         /*
 794          * Changing these numbers is a pain.  Don't do it.  If you ever use a
 795          * number it can't be re-used right away so there may be gaps.
 796          * We select these in order by type so if the order matters, then give
 797          * it a number below 100-999,9000-9999 ranges. */
 798 
 799         PARAM_CLEARED   = 101,
 800         PARAM_LIMIT     = 103,
 801         PARAM_FILTER    = 104,
 802 
 803         PARAM_VALUE     = 1001,
 804         BUF_SIZE        = 1002,
 805         CAPPED_DATA     = 1004,
 806         RETURN_VALUE    = 1005,
 807         DEREFERENCE     = 1006,
 808         RANGE_CAP       = 1007,
 809         LOCK_HELD       = 1008,
 810         LOCK_RELEASED   = 1009,
 811         ABSOLUTE_LIMITS = 1010,
 812         PARAM_ADD       = 1012,
 813         PARAM_FREED     = 1013,
 814         DATA_SOURCE     = 1014,
 815         FUZZY_MAX       = 1015,
 816         HARD_MAX        = 2015,
 817         STR_LEN         = 1016,
 818         ARRAY_LEN       = 1017,
 819         CAPABLE         = 1018,
 820         NS_CAPABLE      = 1019,
 821         CONTAINER       = 1020,
 822         CASTED_CALL     = 1021,
 823         TYPE_LINK       = 1022,
 824         UNTRACKED_PARAM = 1023,
 825         LOST_PARAM      = 2023,
 826         CULL_PATH       = 1024,
 827         PARAM_SET       = 1025,
 828         PARAM_USED      = 1026,
 829         BYTE_UNITS      = 1027,
 830         COMPARE_LIMIT   = 1028,
 831         PARAM_COMPARE   = 1029,
 832         CONSTRAINT      = 1031,
 833         PASSES_TYPE     = 1032,
 834         CONSTRAINT_REQUIRED = 1033,
 835         BIT_INFO        = 1034,
 836         NOSPEC          = 1035,
 837         NOSPEC_WB       = 1036,
 838         STMT_CNT        = 1037,
 839         TERMINATED      = 1038,
 840 
 841         /* put random temporary stuff in the 7000-7999 range for testing */
 842         USER_DATA       = 8017,
 843         USER_DATA_SET   = 9017,
 844         NO_OVERFLOW     = 8018,
 845         NO_OVERFLOW_SIMPLE = 8019,
 846         LOCKED          = 8020,
 847         UNLOCKED        = 8021,




 848         SET_FS          = 8022,
 849         ATOMIC_INC      = 8023,
 850         ATOMIC_DEC      = 8024,
 851         NO_SIDE_EFFECT  = 8025,
 852         FN_ARG_LINK     = 8028,
 853         DATA_VALUE      = 8029,
 854         ARRAYSIZE_ARG   = 8033,
 855         SIZEOF_ARG      = 8034,
 856         MEMORY_TAG      = 8036,
 857         MTAG_ASSIGN     = 8035,
 858         STRING_VALUE    = 8041,
 859 
 860         BYTE_COUNT      = 8050,
 861         ELEM_COUNT      = 8051,
 862         ELEM_LAST       = 8052,
 863         USED_LAST       = 8053,
 864         USED_COUNT      = 8054,
 865 };
 866 
 867 extern struct sqlite3 *smatch_db;


 870 
 871 void db_ignore_states(int id);
 872 void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type);
 873 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm));
 874 void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr));
 875 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));
 876 void select_call_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value));
 877 void select_return_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value));
 878 struct range_list *db_return_vals(struct expression *expr);
 879 struct range_list *db_return_vals_from_str(const char *fn_name);
 880 struct range_list *db_return_vals_no_args(struct expression *expr);
 881 char *return_state_to_var_sym(struct expression *expr, int param, const char *key, struct symbol **sym);
 882 char *get_chunk_from_key(struct expression *arg, char *key, struct symbol **sym, struct var_sym_list **vsl);
 883 char *get_variable_from_key(struct expression *arg, const char *key, struct symbol **sym);
 884 const char *state_name_to_param_name(const char *state_name, const char *param_name);
 885 const char *get_param_name_var_sym(const char *name, struct symbol *sym);
 886 const char *get_param_name(struct sm_state *sm);
 887 const char *get_mtag_name_var_sym(const char *state_name, struct symbol *sym);
 888 const char *get_mtag_name_expr(struct expression *expr);
 889 char *get_data_info_name(struct expression *expr);

 890 int is_recursive_member(const char *param_name);
 891 
 892 char *escape_newlines(const char *str);
 893 void sql_exec(struct sqlite3 *db, int (*callback)(void*, int, char**, char**), void *data, const char *sql);
 894 
 895 #define sql_helper(db, call_back, data, sql...)                                 \
 896 do {                                                                            \
 897         char sql_txt[1024];                                                     \
 898                                                                                 \
 899         sqlite3_snprintf(sizeof(sql_txt), sql_txt, sql);                        \
 900         sm_debug("debug: %s\n", sql_txt);                                       \
 901         sql_exec(db, call_back, data, sql_txt);                                 \
 902 } while (0)
 903 
 904 
 905 #define run_sql(call_back, data, sql...)                                        \
 906 do {                                                                            \
 907         if (option_no_db)                                                       \
 908                 break;                                                          \
 909         sql_helper(smatch_db, call_back, data, sql);                            \
 910 } while (0)
 911 
 912 #define mem_sql(call_back, data, sql...)                                        \
 913         sql_helper(mem_db, call_back, data, sql)
 914 
 915 #define cache_sql(call_back, data, sql...)                                      \
 916         sql_helper(cache_db, call_back, data, sql)
 917 
 918 #define sql_insert_helper(table, db, ignore, late, values...)                   \
 919 do {                                                                            \
 920         struct sqlite3 *_db = db;                                               \
 921                                                                                 \
 922         if (__inline_fn && !_db)                                                \
 923                 _db = mem_db;                                                   \
 924         if (_db) {                                                              \
 925                 char buf[1024];                                                 \
 926                 char *err, *p = buf;                                            \
 927                 int rc;                                                         \
 928                                                                                 \
 929                 p += snprintf(p, buf + sizeof(buf) - p,                         \
 930                               "insert %sinto %s values (",                      \
 931                               ignore ? "or ignore " : "", #table);              \
 932                 p += snprintf(p, buf + sizeof(buf) - p, values);                \
 933                 p += snprintf(p, buf + sizeof(buf) - p, ");");                  \
 934                 sm_debug("mem-db: %s\n", buf);                                  \
 935                 rc = sqlite3_exec(_db, buf, NULL, NULL, &err);                      \
 936                 if (rc != SQLITE_OK) {                                          \
 937                         sm_ierror("SQL error #2: %s", err);                     \
 938                         sm_ierror("SQL: '%s'", buf);                            \
 939                         parse_error = 1;                                        \
 940                 }                                                               \
 941                 break;                                                          \
 942         }                                                                       \
 943         if (option_info) {                                                      \
 944                 FILE *tmp_fd = sm_outfd;                                        \
 945                 sm_outfd = sql_outfd;                                           \
 946                 sm_prefix();                                                    \
 947                 sm_printf("SQL%s: insert %sinto " #table " values(",            \
 948                           late ? "_late" : "", ignore ? "or ignore " : "");     \
 949                 sm_printf(values);                                              \
 950                 sm_printf(");\n");                                              \
 951                 sm_outfd = tmp_fd;                                              \
 952         }                                                                       \
 953 } while (0)
 954 


 990 
 991 void sql_select_return_states(const char *cols, struct expression *call,
 992         int (*callback)(void*, int, char**, char**), void *info);
 993 void sql_select_call_implies(const char *cols, struct expression *call,
 994         int (*callback)(void*, int, char**, char**));
 995 
 996 void open_smatch_db(char *db_file);
 997 
 998 /* smatch_files.c */
 999 int open_data_file(const char *filename);
1000 int open_schema_file(const char *schema);
1001 struct token *get_tokens_file(const char *filename);
1002 
1003 /* smatch.c */
1004 extern char *option_debug_check;
1005 extern char *option_project_str;
1006 extern char *bin_dir;
1007 extern char *data_dir;
1008 extern int option_no_data;
1009 extern int option_full_path;
1010 extern int option_param_mapper;
1011 extern int option_call_tree;
1012 extern int num_checks;
1013 
1014 enum project_type {
1015         PROJ_NONE,
1016         PROJ_KERNEL,
1017         PROJ_WINE,
1018         PROJ_ILLUMOS_KERNEL,
1019         PROJ_ILLUMOS_USER,
1020         PROJ_UNKNOWN,
1021 };
1022 extern enum project_type option_project;
1023 const char *check_name(unsigned short id);
1024 int id_from_name(const char *name);
1025 
1026 
1027 /* smatch_buf_size.c */
1028 int get_array_size(struct expression *expr);
1029 int get_array_size_bytes(struct expression *expr);
1030 int get_array_size_bytes_min(struct expression *expr);


1045 int is_capped_user_data(struct expression *expr);
1046 int implied_user_data(struct expression *expr, struct range_list **rl);
1047 struct stree *get_user_stree(void);
1048 int get_user_rl(struct expression *expr, struct range_list **rl);
1049 int is_user_rl(struct expression *expr);
1050 int get_user_rl_var_sym(const char *name, struct symbol *sym, struct range_list **rl);
1051 bool user_rl_capped(struct expression *expr);
1052 struct range_list *var_user_rl(struct expression *expr);
1053 
1054 /* check_locking.c */
1055 void print_held_locks();
1056 
1057 /* check_assigned_expr.c */
1058 struct expression *get_assigned_expr(struct expression *expr);
1059 struct expression *get_assigned_expr_name_sym(const char *name, struct symbol *sym);
1060 /* smatch_return_to_param.c */
1061 void __add_return_to_param_mapping(struct expression *assign, const char *return_string);
1062 char *map_call_to_param_name_sym(struct expression *expr, struct symbol **sym);
1063 
1064 /* smatch_comparison.c */

1065 #define UNKNOWN_COMPARISON 0
1066 #define IMPOSSIBLE_COMPARISON -1
1067 struct compare_data {
1068         /* The ->left and ->right expression pointers might be NULL (I'm lazy) */
1069         struct expression *left;
1070         const char *left_var;
1071         struct var_sym_list *left_vsl;
1072         int comparison;
1073         struct expression *right;
1074         const char *right_var;
1075         struct var_sym_list *right_vsl;
1076 };
1077 DECLARE_ALLOCATOR(compare_data);
1078 struct smatch_state *alloc_compare_state(
1079                 struct expression *left,
1080                 const char *left_var, struct var_sym_list *left_vsl,
1081                 int comparison,
1082                 struct expression *right,
1083                 const char *right_var, struct var_sym_list *right_vsl);
1084 int comparison_intersection(int orig, int op);


1100 char *expr_lte_to_param(struct expression *expr, int ignore);
1101 char *expr_param_comparison(struct expression *expr, int ignore);
1102 int flip_comparison(int op);
1103 int negate_comparison(int op);
1104 int remove_unsigned_from_comparison(int op);
1105 int param_compare_limit_is_impossible(struct expression *expr, int left_param, char *left_key, char *value);
1106 void filter_by_comparison(struct range_list **rl, int comparison, struct range_list *right);
1107 struct sm_state *comparison_implication_hook(struct expression *expr,
1108                         struct state_list **true_stack,
1109                         struct state_list **false_stack);
1110 void __compare_param_limit_hook(struct expression *left_expr, struct expression *right_expr,
1111                                 const char *state_name,
1112                                 struct smatch_state *true_state, struct smatch_state *false_state);
1113 int impossibly_high_comparison(struct expression *expr);
1114 
1115 /* smatch_sval.c */
1116 sval_t *sval_alloc(sval_t sval);
1117 sval_t *sval_alloc_permanent(sval_t sval);
1118 sval_t sval_blank(struct expression *expr);
1119 sval_t sval_type_val(struct symbol *type, long long val);

1120 sval_t sval_from_val(struct expression *expr, long long val);

1121 int sval_is_ptr(sval_t sval);

1122 int sval_unsigned(sval_t sval);
1123 int sval_signed(sval_t sval);
1124 int sval_bits(sval_t sval);
1125 int sval_bits_used(sval_t sval);
1126 int sval_is_negative(sval_t sval);
1127 int sval_is_positive(sval_t sval);
1128 int sval_is_min(sval_t sval);
1129 int sval_is_max(sval_t sval);
1130 int sval_is_a_min(sval_t sval);
1131 int sval_is_a_max(sval_t sval);
1132 int sval_is_negative_min(sval_t sval);
1133 int sval_cmp_t(struct symbol *type, sval_t one, sval_t two);
1134 int sval_cmp_val(sval_t one, long long val);
1135 sval_t sval_min(sval_t one, sval_t two);
1136 sval_t sval_max(sval_t one, sval_t two);
1137 int sval_too_low(struct symbol *type, sval_t sval);
1138 int sval_too_high(struct symbol *type, sval_t sval);
1139 int sval_fits(struct symbol *type, sval_t sval);
1140 sval_t sval_cast(struct symbol *type, sval_t sval);
1141 sval_t sval_preop(sval_t sval, int op);


1154 /* smatch_string_list.c */
1155 int list_has_string(struct string_list *str_list, const char *str);
1156 int insert_string(struct string_list **str_list, const char *str);
1157 struct string_list *clone_str_list(struct string_list *orig);
1158 struct string_list *combine_string_lists(struct string_list *one, struct string_list *two);
1159 
1160 /* smatch_start_states.c */
1161 struct stree *get_start_states(void);
1162 
1163 /* smatch_recurse.c */
1164 int has_symbol(struct expression *expr, struct symbol *sym);
1165 int has_variable(struct expression *expr, struct expression *var);
1166 int has_inc_dec(struct expression *expr);
1167 
1168 /* smatch_stored_conditions.c */
1169 struct smatch_state *get_stored_condition(struct expression *expr);
1170 struct expression_list *get_conditions(struct expression *expr);
1171 struct sm_state *stored_condition_implication_hook(struct expression *expr,
1172                         struct state_list **true_stack,
1173                         struct state_list **false_stack);




1174 
1175 /* check_string_len.c */
1176 int get_formatted_string_size(struct expression *call, int arg);
1177 int get_formatted_string_min_size(struct expression *call, int arg);
1178 
1179 /* smatch_param_set.c */
1180 int param_was_set(struct expression *expr);
1181 int param_was_set_var_sym(const char *name, struct symbol *sym);
1182 void print_limited_param_set(int return_id, char *return_ranges, struct expression *expr);
1183 /* smatch_param_filter.c */
1184 int param_has_filter_data(struct sm_state *sm);
1185 
1186 /* smatch_links.c */
1187 void set_up_link_functions(int id, int linkid);
1188 struct smatch_state *merge_link_states(struct smatch_state *s1, struct smatch_state *s2);
1189 void store_link(int link_id, const char *name, struct symbol *sym, const char *link_name, struct symbol *link_sym);
1190 
1191 /* check_buf_comparison */
1192 const char *limit_type_str(unsigned int limit_type);
1193 struct expression *get_size_variable(struct expression *buf, int *limit_type);


1255 unsigned long get_max_memory(void);
1256 
1257 /* check_is_nospec.c */
1258 bool is_nospec(struct expression *expr);
1259 long get_stmt_cnt(void);
1260 
1261 /* smatch_nul_terminator.c */
1262 bool is_nul_terminated_var_sym(const char *name, struct symbol *sym);
1263 bool is_nul_terminated(struct expression *expr);
1264 /* check_kernel.c  */
1265 bool is_ignored_kernel_data(const char *name);
1266 
1267 static inline bool type_is_ptr(struct symbol *type)
1268 {
1269         return type &&
1270                (type->type == SYM_PTR ||
1271                 type->type == SYM_ARRAY ||
1272                 type->type == SYM_FN);
1273 }
1274 








1275 static inline int type_bits(struct symbol *type)
1276 {
1277         if (!type)
1278                 return 0;
1279         if (type_is_ptr(type))
1280                 return bits_in_pointer;
1281         if (!type->examined)
1282                 examine_symbol_type(type);
1283         return type->bit_size;
1284 }
1285 
1286 static inline int type_unsigned(struct symbol *base_type)
1287 {
1288         if (!base_type)
1289                 return 0;
1290         if (is_ptr_type(base_type))
1291                 return 1;
1292         if (base_type->ctype.modifiers & MOD_UNSIGNED)
1293                 return 1;
1294         return 0;


1296 
1297 static inline int type_positive_bits(struct symbol *type)
1298 {
1299         if (!type)
1300                 return 0;
1301         if (is_ptr_type(type))
1302                 return bits_in_pointer;
1303         if (type_unsigned(type))
1304                 return type_bits(type);
1305         return type_bits(type) - 1;
1306 }
1307 
1308 static inline int sval_positive_bits(sval_t sval)
1309 {
1310         return type_positive_bits(sval.type);
1311 }
1312 
1313 /*
1314  * Returns -1 if one is smaller, 0 if they are the same and 1 if two is larger.
1315  */








































1316 static inline int sval_cmp(sval_t one, sval_t two)
1317 {
1318         struct symbol *type;
1319 



1320         type = one.type;
1321         if (sval_positive_bits(two) > sval_positive_bits(one))
1322                 type = two.type;
1323         if (type_bits(type) < 31)
1324                 type = &int_ctype;
1325 
1326         one = sval_cast(type, one);
1327         two = sval_cast(type, two);
1328 
1329         if (type_unsigned(type)) {
1330                 if (one.uvalue < two.uvalue)
1331                         return -1;
1332                 if (one.uvalue == two.uvalue)
1333                         return 0;
1334                 return 1;
1335         }
1336         /* fix me handle type promotion and unsigned values */
1337         if (one.value < two.value)
1338                 return -1;
1339         if (one.value == two.value)


   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  * Copyright 2019 Joyent, Inc.
  18  */
  19 
  20 #ifndef         SMATCH_H_
  21 # define        SMATCH_H_
  22 
  23 #include <stdio.h>
  24 #include <string.h>
  25 #include <limits.h>
  26 #include <float.h>
  27 #include <sys/time.h>
  28 #include <sqlite3.h>
  29 #include "lib.h"
  30 #include "allocate.h"
  31 #include "scope.h"
  32 #include "parse.h"
  33 #include "expression.h"
  34 #include "avl.h"
  35 
  36 typedef struct {
  37         struct symbol *type;
  38         union {
  39                 long long value;
  40                 unsigned long long uvalue;
  41                 float fvalue;
  42                 double dvalue;
  43                 long double ldvalue;
  44         };
  45 } sval_t;
  46 
  47 typedef long long mtag_t;
  48 
  49 struct smatch_state {
  50         const char *name;
  51         void *data;
  52 };
  53 #define STATE(_x) static struct smatch_state _x = { .name = #_x }
  54 extern struct smatch_state undefined;
  55 extern struct smatch_state ghost;
  56 extern struct smatch_state merged;
  57 extern struct smatch_state true_state;
  58 extern struct smatch_state false_state;
  59 DECLARE_ALLOCATOR(smatch_state);
  60 
  61 static inline void *INT_PTR(int i)
  62 {
  63         return (void *)(long)i;


 193 
 194 struct modification_data {
 195         struct smatch_state *prev;
 196         struct expression *cur;
 197 };
 198 
 199 typedef void (modification_hook)(struct sm_state *sm, struct expression *mod_expr);
 200 void add_modification_hook(int owner, modification_hook *call_back);
 201 void add_modification_hook_late(int owner, modification_hook *call_back);
 202 struct smatch_state *get_modification_state(struct expression *expr);
 203 
 204 int outside_of_function(void);
 205 const char *get_filename(void);
 206 const char *get_base_file(void);
 207 char *get_function(void);
 208 int get_lineno(void);
 209 extern int final_pass;
 210 extern struct symbol *cur_func_sym;
 211 extern int option_debug;
 212 extern int local_debug;
 213 extern int debug_db;
 214 bool debug_implied(void);
 215 extern int option_info;
 216 extern int option_spammy;
 217 extern int option_timeout;
 218 extern char *trace_variable;
 219 extern struct stree *global_states;
 220 int is_skipped_function(void);
 221 int is_silenced_function(void);
 222 extern bool implications_off;
 223 
 224 /* smatch_impossible.c */
 225 int is_impossible_path(void);
 226 void set_path_impossible(void);
 227 
 228 extern FILE *sm_outfd;
 229 extern FILE *sql_outfd;
 230 extern FILE *caller_info_fd;
 231 extern int sm_nr_checks;
 232 extern int sm_nr_errors;
 233 extern const char *progname;
 234 
 235 /*
 236  * How to use these routines:
 237  *
 238  * sm_fatal(): an internal error of some kind that should immediately exit
 239  * sm_ierror(): an internal error
 240  * sm_perror(): an internal error from parsing input source
 241  * sm_error(): an error from input source
 242  * sm_warning(): a warning from input source
 243  * sm_info(): info message (from option_info)
 244  * sm_debug(): debug message
 245  * sm_msg(): other message (please avoid using this)
 246  */
 247 
 248 #define sm_printf(msg...) do {                                          \
 249         if (final_pass || option_debug || local_debug || debug_db)      \
 250                 fprintf(sm_outfd, msg);                                 \
 251 } while (0)
 252 
 253 static inline void sm_prefix(void)
 254 {
 255         sm_printf("%s: %s:%d %s() ", progname, get_filename(), get_lineno(), get_function());
 256 }
 257 
 258 static inline void print_implied_debug_msg();
 259 
 260 extern bool __silence_warnings_for_stmt;
 261 
 262 #define sm_print_msg(type, msg...) \
 263 do {                                                           \
 264         print_implied_debug_msg();                             \
 265         if (!final_pass && !option_debug && !local_debug && !debug_db)    \
 266                 break;                                         \
 267         if (__silence_warnings_for_stmt && !option_debug && !local_debug) \
 268                 break;                                         \
 269         if (!option_info && is_silenced_function())            \
 270                 break;                                         \
 271         sm_prefix();                                           \
 272         if (type == 1) {                                       \
 273                 sm_printf("warn: ");                           \
 274                 sm_nr_checks++;                                \
 275         } else if (type == 2) {                                \
 276                 sm_printf("error: ");                          \
 277                 sm_nr_checks++;                                \
 278         } else if (type == 3) {                                \
 279                 sm_printf("parse error: ");                    \
 280                 sm_nr_errors++;                                \
 281         }                                                      \
 282         sm_printf(msg);                                        \
 283         sm_printf("\n");                                       \
 284 } while (0)
 285 
 286 #define sm_msg(msg...) do { sm_print_msg(0, msg); } while (0)
 287 






 288 extern char *implied_debug_msg;
 289 static inline void print_implied_debug_msg(void)
 290 {
 291         static struct symbol *last_printed = NULL;
 292 
 293         if (!implied_debug_msg)
 294                 return;
 295         if (last_printed == cur_func_sym)
 296                 return;
 297         last_printed = cur_func_sym;
 298         sm_msg("%s", implied_debug_msg);
 299 }
 300 
 301 #define sm_debug(msg...) do { if (option_debug) sm_printf(msg); } while (0)
 302 #define db_debug(msg...) do { if (option_debug || debug_db) sm_printf(msg); } while (0)
 303 
 304 #define sm_info(msg...) do {                                    \
 305         if (option_debug || (option_info && final_pass)) {      \
 306                 sm_prefix();                                    \
 307                 sm_printf("info: ");                            \
 308                 sm_printf(msg);                                 \
 309                 sm_printf("\n");                                \
 310         }                                                       \
 311 } while(0)
 312 
 313 #define sm_warning(msg...) do { sm_print_msg(1, msg); } while (0)
 314 #define sm_error(msg...) do { sm_print_msg(2, msg); } while (0)
 315 #define sm_perror(msg...) do { sm_print_msg(3, msg); } while (0)
 316 
 317 static inline void sm_fatal(const char *fmt, ...)
 318 {
 319         va_list args;
 320 
 321         va_start(args, fmt);
 322         vfprintf(sm_outfd, fmt, args);


 424 struct expression *get_array_base(struct expression *expr);
 425 struct expression *get_array_offset(struct expression *expr);
 426 const char *show_state(struct smatch_state *state);
 427 struct statement *get_expression_statement(struct expression *expr);
 428 struct expression *strip_parens(struct expression *expr);
 429 struct expression *strip_expr(struct expression *expr);
 430 struct expression *strip_expr_set_parent(struct expression *expr);
 431 void scoped_state(int my_id, const char *name, struct symbol *sym);
 432 int is_error_return(struct expression *expr);
 433 int getting_address(struct expression *expr);
 434 int get_struct_and_member(struct expression *expr, const char **type, const char **member);
 435 char *get_member_name(struct expression *expr);
 436 char *get_fnptr_name(struct expression *expr);
 437 int cmp_pos(struct position pos1, struct position pos2);
 438 int positions_eq(struct position pos1, struct position pos2);
 439 struct statement *get_current_statement(void);
 440 struct statement *get_prev_statement(void);
 441 struct expression *get_last_expr_from_expression_stmt(struct expression *expr);
 442 int get_param_num_from_sym(struct symbol *sym);
 443 int get_param_num(struct expression *expr);
 444 struct symbol *get_param_sym_from_num(int num);
 445 int ms_since(struct timeval *start);
 446 int parent_is_gone_var_sym(const char *name, struct symbol *sym);
 447 int parent_is_gone(struct expression *expr);
 448 int invert_op(int op);
 449 int op_remove_assign(int op);
 450 int expr_equiv(struct expression *one, struct expression *two);
 451 void push_int(struct int_stack **stack, int num);
 452 int pop_int(struct int_stack **stack);
 453 
 454 /* smatch_type.c */
 455 struct symbol *get_real_base_type(struct symbol *sym);
 456 int type_bytes(struct symbol *type);
 457 int array_bytes(struct symbol *type);
 458 struct symbol *get_pointer_type(struct expression *expr);
 459 struct symbol *get_type(struct expression *expr);
 460 struct symbol *get_final_type(struct expression *expr);
 461 struct symbol *get_promoted_type(struct symbol *left, struct symbol *right);
 462 int type_signed(struct symbol *base_type);
 463 int expr_unsigned(struct expression *expr);
 464 int expr_signed(struct expression *expr);


 793 
 794 /* smatch_db.c */
 795 enum info_type {
 796         INTERNAL        = 0,
 797         /*
 798          * Changing these numbers is a pain.  Don't do it.  If you ever use a
 799          * number it can't be re-used right away so there may be gaps.
 800          * We select these in order by type so if the order matters, then give
 801          * it a number below 100-999,9000-9999 ranges. */
 802 
 803         PARAM_CLEARED   = 101,
 804         PARAM_LIMIT     = 103,
 805         PARAM_FILTER    = 104,
 806 
 807         PARAM_VALUE     = 1001,
 808         BUF_SIZE        = 1002,
 809         CAPPED_DATA     = 1004,
 810         RETURN_VALUE    = 1005,
 811         DEREFERENCE     = 1006,
 812         RANGE_CAP       = 1007,


 813         ABSOLUTE_LIMITS = 1010,
 814         PARAM_ADD       = 1012,
 815         PARAM_FREED     = 1013,
 816         DATA_SOURCE     = 1014,
 817         FUZZY_MAX       = 1015,
 818         HARD_MAX        = 2015,
 819         STR_LEN         = 1016,
 820         ARRAY_LEN       = 1017,
 821         CAPABLE         = 1018,
 822         NS_CAPABLE      = 1019,
 823         CONTAINER       = 1020,
 824         CASTED_CALL     = 1021,
 825         TYPE_LINK       = 1022,
 826         UNTRACKED_PARAM = 1023,
 827         LOST_PARAM      = 2023,
 828         CULL_PATH       = 1024,
 829         PARAM_SET       = 1025,
 830         PARAM_USED      = 1026,
 831         BYTE_UNITS      = 1027,
 832         COMPARE_LIMIT   = 1028,
 833         PARAM_COMPARE   = 1029,
 834         CONSTRAINT      = 1031,
 835         PASSES_TYPE     = 1032,
 836         CONSTRAINT_REQUIRED = 1033,
 837         BIT_INFO        = 1034,
 838         NOSPEC          = 1035,
 839         NOSPEC_WB       = 1036,
 840         STMT_CNT        = 1037,
 841         TERMINATED      = 1038,
 842 
 843         /* put random temporary stuff in the 7000-7999 range for testing */
 844         USER_DATA       = 8017,
 845         USER_DATA_SET   = 9017,
 846         NO_OVERFLOW     = 8018,
 847         NO_OVERFLOW_SIMPLE = 8019,
 848         LOCKED          = 8020,
 849         UNLOCKED        = 8021,
 850         HALF_LOCKED     = 9022,
 851         LOCK_RESTORED   = 9023,
 852         KNOWN_LOCKED    = 9024,
 853         KNOWN_UNLOCKED  = 9025,
 854         SET_FS          = 8022,
 855         ATOMIC_INC      = 8023,
 856         ATOMIC_DEC      = 8024,
 857         NO_SIDE_EFFECT  = 8025,
 858         FN_ARG_LINK     = 8028,
 859         DATA_VALUE      = 8029,
 860         ARRAYSIZE_ARG   = 8033,
 861         SIZEOF_ARG      = 8034,
 862         MEMORY_TAG      = 8036,
 863         MTAG_ASSIGN     = 8035,
 864         STRING_VALUE    = 8041,
 865 
 866         BYTE_COUNT      = 8050,
 867         ELEM_COUNT      = 8051,
 868         ELEM_LAST       = 8052,
 869         USED_LAST       = 8053,
 870         USED_COUNT      = 8054,
 871 };
 872 
 873 extern struct sqlite3 *smatch_db;


 876 
 877 void db_ignore_states(int id);
 878 void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type);
 879 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm));
 880 void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr));
 881 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));
 882 void select_call_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value));
 883 void select_return_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value));
 884 struct range_list *db_return_vals(struct expression *expr);
 885 struct range_list *db_return_vals_from_str(const char *fn_name);
 886 struct range_list *db_return_vals_no_args(struct expression *expr);
 887 char *return_state_to_var_sym(struct expression *expr, int param, const char *key, struct symbol **sym);
 888 char *get_chunk_from_key(struct expression *arg, char *key, struct symbol **sym, struct var_sym_list **vsl);
 889 char *get_variable_from_key(struct expression *arg, const char *key, struct symbol **sym);
 890 const char *state_name_to_param_name(const char *state_name, const char *param_name);
 891 const char *get_param_name_var_sym(const char *name, struct symbol *sym);
 892 const char *get_param_name(struct sm_state *sm);
 893 const char *get_mtag_name_var_sym(const char *state_name, struct symbol *sym);
 894 const char *get_mtag_name_expr(struct expression *expr);
 895 char *get_data_info_name(struct expression *expr);
 896 char *sm_to_arg_name(struct expression *expr, struct sm_state *sm);
 897 int is_recursive_member(const char *param_name);
 898 
 899 char *escape_newlines(const char *str);
 900 void sql_exec(struct sqlite3 *db, int (*callback)(void*, int, char**, char**), void *data, const char *sql);
 901 
 902 #define sql_helper(db, call_back, data, sql...)                                 \
 903 do {                                                                            \
 904         char sql_txt[1024];                                                     \
 905                                                                                 \
 906         sqlite3_snprintf(sizeof(sql_txt), sql_txt, sql);                        \
 907         db_debug("debug: %s\n", sql_txt);                                       \
 908         sql_exec(db, call_back, data, sql_txt);                                 \
 909 } while (0)
 910 
 911 
 912 #define run_sql(call_back, data, sql...)                                        \
 913 do {                                                                            \
 914         if (option_no_db)                                                       \
 915                 break;                                                          \
 916         sql_helper(smatch_db, call_back, data, sql);                            \
 917 } while (0)
 918 
 919 #define mem_sql(call_back, data, sql...)                                        \
 920         sql_helper(mem_db, call_back, data, sql)
 921 
 922 #define cache_sql(call_back, data, sql...)                                      \
 923         sql_helper(cache_db, call_back, data, sql)
 924 
 925 #define sql_insert_helper(table, db, ignore, late, values...)                   \
 926 do {                                                                            \
 927         struct sqlite3 *_db = db;                                               \
 928                                                                                 \
 929         if (__inline_fn && !_db)                                                \
 930                 _db = mem_db;                                                   \
 931         if (_db) {                                                              \
 932                 char buf[1024];                                                 \
 933                 char *err, *p = buf;                                            \
 934                 int rc;                                                         \
 935                                                                                 \
 936                 p += snprintf(p, buf + sizeof(buf) - p,                         \
 937                               "insert %sinto %s values (",                      \
 938                               ignore ? "or ignore " : "", #table);              \
 939                 p += snprintf(p, buf + sizeof(buf) - p, values);                \
 940                 p += snprintf(p, buf + sizeof(buf) - p, ");");                  \
 941                 db_debug("mem-db: %s\n", buf);                                  \
 942                 rc = sqlite3_exec(_db, buf, NULL, NULL, &err);                      \
 943                 if (rc != SQLITE_OK) {                                          \
 944                         sm_ierror("SQL error #2: %s", err);                     \
 945                         sm_ierror("SQL: '%s'", buf);                            \
 946                         parse_error = 1;                                        \
 947                 }                                                               \
 948                 break;                                                          \
 949         }                                                                       \
 950         if (option_info) {                                                      \
 951                 FILE *tmp_fd = sm_outfd;                                        \
 952                 sm_outfd = sql_outfd;                                           \
 953                 sm_prefix();                                                    \
 954                 sm_printf("SQL%s: insert %sinto " #table " values(",            \
 955                           late ? "_late" : "", ignore ? "or ignore " : "");     \
 956                 sm_printf(values);                                              \
 957                 sm_printf(");\n");                                              \
 958                 sm_outfd = tmp_fd;                                              \
 959         }                                                                       \
 960 } while (0)
 961 


 997 
 998 void sql_select_return_states(const char *cols, struct expression *call,
 999         int (*callback)(void*, int, char**, char**), void *info);
1000 void sql_select_call_implies(const char *cols, struct expression *call,
1001         int (*callback)(void*, int, char**, char**));
1002 
1003 void open_smatch_db(char *db_file);
1004 
1005 /* smatch_files.c */
1006 int open_data_file(const char *filename);
1007 int open_schema_file(const char *schema);
1008 struct token *get_tokens_file(const char *filename);
1009 
1010 /* smatch.c */
1011 extern char *option_debug_check;
1012 extern char *option_project_str;
1013 extern char *bin_dir;
1014 extern char *data_dir;
1015 extern int option_no_data;
1016 extern int option_full_path;

1017 extern int option_call_tree;
1018 extern int num_checks;
1019 
1020 enum project_type {
1021         PROJ_NONE,
1022         PROJ_KERNEL,
1023         PROJ_WINE,
1024         PROJ_ILLUMOS_KERNEL,
1025         PROJ_ILLUMOS_USER,
1026         PROJ_UNKNOWN,
1027 };
1028 extern enum project_type option_project;
1029 const char *check_name(unsigned short id);
1030 int id_from_name(const char *name);
1031 
1032 
1033 /* smatch_buf_size.c */
1034 int get_array_size(struct expression *expr);
1035 int get_array_size_bytes(struct expression *expr);
1036 int get_array_size_bytes_min(struct expression *expr);


1051 int is_capped_user_data(struct expression *expr);
1052 int implied_user_data(struct expression *expr, struct range_list **rl);
1053 struct stree *get_user_stree(void);
1054 int get_user_rl(struct expression *expr, struct range_list **rl);
1055 int is_user_rl(struct expression *expr);
1056 int get_user_rl_var_sym(const char *name, struct symbol *sym, struct range_list **rl);
1057 bool user_rl_capped(struct expression *expr);
1058 struct range_list *var_user_rl(struct expression *expr);
1059 
1060 /* check_locking.c */
1061 void print_held_locks();
1062 
1063 /* check_assigned_expr.c */
1064 struct expression *get_assigned_expr(struct expression *expr);
1065 struct expression *get_assigned_expr_name_sym(const char *name, struct symbol *sym);
1066 /* smatch_return_to_param.c */
1067 void __add_return_to_param_mapping(struct expression *assign, const char *return_string);
1068 char *map_call_to_param_name_sym(struct expression *expr, struct symbol **sym);
1069 
1070 /* smatch_comparison.c */
1071 extern int comparison_id;
1072 #define UNKNOWN_COMPARISON 0
1073 #define IMPOSSIBLE_COMPARISON -1
1074 struct compare_data {
1075         /* The ->left and ->right expression pointers might be NULL (I'm lazy) */
1076         struct expression *left;
1077         const char *left_var;
1078         struct var_sym_list *left_vsl;
1079         int comparison;
1080         struct expression *right;
1081         const char *right_var;
1082         struct var_sym_list *right_vsl;
1083 };
1084 DECLARE_ALLOCATOR(compare_data);
1085 struct smatch_state *alloc_compare_state(
1086                 struct expression *left,
1087                 const char *left_var, struct var_sym_list *left_vsl,
1088                 int comparison,
1089                 struct expression *right,
1090                 const char *right_var, struct var_sym_list *right_vsl);
1091 int comparison_intersection(int orig, int op);


1107 char *expr_lte_to_param(struct expression *expr, int ignore);
1108 char *expr_param_comparison(struct expression *expr, int ignore);
1109 int flip_comparison(int op);
1110 int negate_comparison(int op);
1111 int remove_unsigned_from_comparison(int op);
1112 int param_compare_limit_is_impossible(struct expression *expr, int left_param, char *left_key, char *value);
1113 void filter_by_comparison(struct range_list **rl, int comparison, struct range_list *right);
1114 struct sm_state *comparison_implication_hook(struct expression *expr,
1115                         struct state_list **true_stack,
1116                         struct state_list **false_stack);
1117 void __compare_param_limit_hook(struct expression *left_expr, struct expression *right_expr,
1118                                 const char *state_name,
1119                                 struct smatch_state *true_state, struct smatch_state *false_state);
1120 int impossibly_high_comparison(struct expression *expr);
1121 
1122 /* smatch_sval.c */
1123 sval_t *sval_alloc(sval_t sval);
1124 sval_t *sval_alloc_permanent(sval_t sval);
1125 sval_t sval_blank(struct expression *expr);
1126 sval_t sval_type_val(struct symbol *type, long long val);
1127 sval_t sval_type_fval(struct symbol *type, long double fval);
1128 sval_t sval_from_val(struct expression *expr, long long val);
1129 sval_t sval_from_fval(struct expression *expr, long double fval);
1130 int sval_is_ptr(sval_t sval);
1131 bool sval_is_fp(sval_t sval);
1132 int sval_unsigned(sval_t sval);
1133 int sval_signed(sval_t sval);
1134 int sval_bits(sval_t sval);
1135 int sval_bits_used(sval_t sval);
1136 int sval_is_negative(sval_t sval);
1137 int sval_is_positive(sval_t sval);
1138 int sval_is_min(sval_t sval);
1139 int sval_is_max(sval_t sval);
1140 int sval_is_a_min(sval_t sval);
1141 int sval_is_a_max(sval_t sval);
1142 int sval_is_negative_min(sval_t sval);
1143 int sval_cmp_t(struct symbol *type, sval_t one, sval_t two);
1144 int sval_cmp_val(sval_t one, long long val);
1145 sval_t sval_min(sval_t one, sval_t two);
1146 sval_t sval_max(sval_t one, sval_t two);
1147 int sval_too_low(struct symbol *type, sval_t sval);
1148 int sval_too_high(struct symbol *type, sval_t sval);
1149 int sval_fits(struct symbol *type, sval_t sval);
1150 sval_t sval_cast(struct symbol *type, sval_t sval);
1151 sval_t sval_preop(sval_t sval, int op);


1164 /* smatch_string_list.c */
1165 int list_has_string(struct string_list *str_list, const char *str);
1166 int insert_string(struct string_list **str_list, const char *str);
1167 struct string_list *clone_str_list(struct string_list *orig);
1168 struct string_list *combine_string_lists(struct string_list *one, struct string_list *two);
1169 
1170 /* smatch_start_states.c */
1171 struct stree *get_start_states(void);
1172 
1173 /* smatch_recurse.c */
1174 int has_symbol(struct expression *expr, struct symbol *sym);
1175 int has_variable(struct expression *expr, struct expression *var);
1176 int has_inc_dec(struct expression *expr);
1177 
1178 /* smatch_stored_conditions.c */
1179 struct smatch_state *get_stored_condition(struct expression *expr);
1180 struct expression_list *get_conditions(struct expression *expr);
1181 struct sm_state *stored_condition_implication_hook(struct expression *expr,
1182                         struct state_list **true_stack,
1183                         struct state_list **false_stack);
1184 /* smatch_parsed_conditions.c */
1185 struct sm_state *parsed_condition_implication_hook(struct expression *expr,
1186                         struct state_list **true_stack,
1187                         struct state_list **false_stack);
1188 
1189 /* check_string_len.c */
1190 int get_formatted_string_size(struct expression *call, int arg);
1191 int get_formatted_string_min_size(struct expression *call, int arg);
1192 
1193 /* smatch_param_set.c */
1194 int param_was_set(struct expression *expr);
1195 int param_was_set_var_sym(const char *name, struct symbol *sym);
1196 void print_limited_param_set(int return_id, char *return_ranges, struct expression *expr);
1197 /* smatch_param_filter.c */
1198 int param_has_filter_data(struct sm_state *sm);
1199 
1200 /* smatch_links.c */
1201 void set_up_link_functions(int id, int linkid);
1202 struct smatch_state *merge_link_states(struct smatch_state *s1, struct smatch_state *s2);
1203 void store_link(int link_id, const char *name, struct symbol *sym, const char *link_name, struct symbol *link_sym);
1204 
1205 /* check_buf_comparison */
1206 const char *limit_type_str(unsigned int limit_type);
1207 struct expression *get_size_variable(struct expression *buf, int *limit_type);


1269 unsigned long get_max_memory(void);
1270 
1271 /* check_is_nospec.c */
1272 bool is_nospec(struct expression *expr);
1273 long get_stmt_cnt(void);
1274 
1275 /* smatch_nul_terminator.c */
1276 bool is_nul_terminated_var_sym(const char *name, struct symbol *sym);
1277 bool is_nul_terminated(struct expression *expr);
1278 /* check_kernel.c  */
1279 bool is_ignored_kernel_data(const char *name);
1280 
1281 static inline bool type_is_ptr(struct symbol *type)
1282 {
1283         return type &&
1284                (type->type == SYM_PTR ||
1285                 type->type == SYM_ARRAY ||
1286                 type->type == SYM_FN);
1287 }
1288 
1289 static inline bool type_is_fp(struct symbol *type)
1290 {
1291         return type &&
1292                (type == &float_ctype ||
1293                 type == &double_ctype ||
1294                 type == &ldouble_ctype);
1295 }
1296 
1297 static inline int type_bits(struct symbol *type)
1298 {
1299         if (!type)
1300                 return 0;
1301         if (type_is_ptr(type))
1302                 return bits_in_pointer;
1303         if (!type->examined)
1304                 examine_symbol_type(type);
1305         return type->bit_size;
1306 }
1307 
1308 static inline int type_unsigned(struct symbol *base_type)
1309 {
1310         if (!base_type)
1311                 return 0;
1312         if (is_ptr_type(base_type))
1313                 return 1;
1314         if (base_type->ctype.modifiers & MOD_UNSIGNED)
1315                 return 1;
1316         return 0;


1318 
1319 static inline int type_positive_bits(struct symbol *type)
1320 {
1321         if (!type)
1322                 return 0;
1323         if (is_ptr_type(type))
1324                 return bits_in_pointer;
1325         if (type_unsigned(type))
1326                 return type_bits(type);
1327         return type_bits(type) - 1;
1328 }
1329 
1330 static inline int sval_positive_bits(sval_t sval)
1331 {
1332         return type_positive_bits(sval.type);
1333 }
1334 
1335 /*
1336  * Returns -1 if one is smaller, 0 if they are the same and 1 if two is larger.
1337  */
1338 
1339 static inline int fp_cmp(sval_t one, sval_t two)
1340 {
1341         struct symbol *type;
1342 
1343         if (sval_is_fp(one) && sval_is_fp(two))
1344                 type = type_bits(one.type) > type_bits(two.type) ? one.type : two.type;
1345         else if (sval_is_fp(one))
1346                 type = one.type;
1347         else
1348                 type = two.type;
1349 
1350         one = sval_cast(type, one);
1351         two = sval_cast(type, two);
1352 
1353         if (one.type == &float_ctype) {
1354                 if (one.fvalue < two.fvalue)
1355                         return -1;
1356                 if (one.fvalue == two.fvalue)
1357                         return 0;
1358                 return 1;
1359         }
1360         if (one.type == &double_ctype) {
1361                 if (one.dvalue < two.dvalue)
1362                         return -1;
1363                 if (one.dvalue == two.dvalue)
1364                         return 0;
1365                 return 1;
1366         }
1367         if (one.type == &ldouble_ctype) {
1368                 if (one.ldvalue < two.ldvalue)
1369                         return -1;
1370                 if (one.ldvalue == two.ldvalue)
1371                         return 0;
1372                 return 1;
1373         }
1374         sm_perror("bad type in fp_cmp(): %s", type_to_str(type));
1375         return 1;
1376 }
1377 
1378 static inline int sval_cmp(sval_t one, sval_t two)
1379 {
1380         struct symbol *type;
1381 
1382         if (sval_is_fp(one) || sval_is_fp(two))
1383                 return fp_cmp(one, two);
1384 
1385         type = one.type;
1386         if (sval_positive_bits(two) > sval_positive_bits(one))
1387                 type = two.type;
1388         if (type_bits(type) < 31)
1389                 type = &int_ctype;
1390 
1391         one = sval_cast(type, one);
1392         two = sval_cast(type, two);
1393 
1394         if (type_unsigned(type)) {
1395                 if (one.uvalue < two.uvalue)
1396                         return -1;
1397                 if (one.uvalue == two.uvalue)
1398                         return 0;
1399                 return 1;
1400         }
1401         /* fix me handle type promotion and unsigned values */
1402         if (one.value < two.value)
1403                 return -1;
1404         if (one.value == two.value)