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

Split Close
Expand all
Collapse all
          --- old/usr/src/tools/smatch/src/smatch.h
          +++ new/usr/src/tools/smatch/src/smatch.h
↓ open down ↓ 15 lines elided ↑ open up ↑
  16   16   *
  17   17   * Copyright 2019 Joyent, Inc.
  18   18   */
  19   19  
  20   20  #ifndef         SMATCH_H_
  21   21  # define        SMATCH_H_
  22   22  
  23   23  #include <stdio.h>
  24   24  #include <string.h>
  25   25  #include <limits.h>
       26 +#include <float.h>
  26   27  #include <sys/time.h>
  27   28  #include <sqlite3.h>
  28   29  #include "lib.h"
  29   30  #include "allocate.h"
  30   31  #include "scope.h"
  31   32  #include "parse.h"
  32   33  #include "expression.h"
  33   34  #include "avl.h"
  34   35  
  35   36  typedef struct {
  36   37          struct symbol *type;
  37   38          union {
  38   39                  long long value;
  39   40                  unsigned long long uvalue;
       41 +                float fvalue;
       42 +                double dvalue;
       43 +                long double ldvalue;
  40   44          };
  41   45  } sval_t;
  42   46  
  43   47  typedef long long mtag_t;
  44   48  
  45   49  struct smatch_state {
  46   50          const char *name;
  47   51          void *data;
  48   52  };
  49   53  #define STATE(_x) static struct smatch_state _x = { .name = #_x }
↓ open down ↓ 149 lines elided ↑ open up ↑
 199  203  
 200  204  int outside_of_function(void);
 201  205  const char *get_filename(void);
 202  206  const char *get_base_file(void);
 203  207  char *get_function(void);
 204  208  int get_lineno(void);
 205  209  extern int final_pass;
 206  210  extern struct symbol *cur_func_sym;
 207  211  extern int option_debug;
 208  212  extern int local_debug;
      213 +extern int debug_db;
 209  214  bool debug_implied(void);
 210  215  extern int option_info;
 211  216  extern int option_spammy;
 212  217  extern int option_timeout;
 213  218  extern char *trace_variable;
 214  219  extern struct stree *global_states;
 215  220  int is_skipped_function(void);
 216  221  int is_silenced_function(void);
 217  222  extern bool implications_off;
 218  223  
↓ open down ↓ 14 lines elided ↑ open up ↑
 233  238   * sm_fatal(): an internal error of some kind that should immediately exit
 234  239   * sm_ierror(): an internal error
 235  240   * sm_perror(): an internal error from parsing input source
 236  241   * sm_error(): an error from input source
 237  242   * sm_warning(): a warning from input source
 238  243   * sm_info(): info message (from option_info)
 239  244   * sm_debug(): debug message
 240  245   * sm_msg(): other message (please avoid using this)
 241  246   */
 242  247  
 243      -#define sm_printf(msg...) do { if (final_pass || option_debug || local_debug) fprintf(sm_outfd, msg); } while (0)
      248 +#define sm_printf(msg...) do {                                          \
      249 +        if (final_pass || option_debug || local_debug || debug_db)      \
      250 +                fprintf(sm_outfd, msg);                                 \
      251 +} while (0)
 244  252  
 245  253  static inline void sm_prefix(void)
 246  254  {
 247  255          sm_printf("%s: %s:%d %s() ", progname, get_filename(), get_lineno(), get_function());
 248  256  }
 249  257  
 250  258  static inline void print_implied_debug_msg();
 251  259  
 252  260  extern bool __silence_warnings_for_stmt;
 253  261  
 254  262  #define sm_print_msg(type, msg...) \
 255  263  do {                                                           \
 256  264          print_implied_debug_msg();                             \
 257      -        if (!final_pass && !option_debug && !local_debug)      \
      265 +        if (!final_pass && !option_debug && !local_debug && !debug_db)    \
 258  266                  break;                                         \
 259  267          if (__silence_warnings_for_stmt && !option_debug && !local_debug) \
 260  268                  break;                                         \
 261  269          if (!option_info && is_silenced_function())            \
 262  270                  break;                                         \
 263  271          sm_prefix();                                           \
 264  272          if (type == 1) {                                       \
 265  273                  sm_printf("warn: ");                           \
 266  274                  sm_nr_checks++;                                \
 267  275          } else if (type == 2) {                                \
↓ open down ↓ 2 lines elided ↑ open up ↑
 270  278          } else if (type == 3) {                                \
 271  279                  sm_printf("parse error: ");                    \
 272  280                  sm_nr_errors++;                                \
 273  281          }                                                      \
 274  282          sm_printf(msg);                                        \
 275  283          sm_printf("\n");                                       \
 276  284  } while (0)
 277  285  
 278  286  #define sm_msg(msg...) do { sm_print_msg(0, msg); } while (0)
 279  287  
 280      -#define local_debug(msg...)                                     \
 281      -do {                                                            \
 282      -        if (local_debug)                                        \
 283      -                sm_msg(msg);                                    \
 284      -} while (0)
 285      -
 286  288  extern char *implied_debug_msg;
 287  289  static inline void print_implied_debug_msg(void)
 288  290  {
 289  291          static struct symbol *last_printed = NULL;
 290  292  
 291  293          if (!implied_debug_msg)
 292  294                  return;
 293  295          if (last_printed == cur_func_sym)
 294  296                  return;
 295  297          last_printed = cur_func_sym;
 296  298          sm_msg("%s", implied_debug_msg);
 297  299  }
 298  300  
 299  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)
 300  303  
 301  304  #define sm_info(msg...) do {                                    \
 302  305          if (option_debug || (option_info && final_pass)) {      \
 303  306                  sm_prefix();                                    \
 304  307                  sm_printf("info: ");                            \
 305  308                  sm_printf(msg);                                 \
 306  309                  sm_printf("\n");                                \
 307  310          }                                                       \
 308  311  } while(0)
 309  312  
↓ open down ↓ 121 lines elided ↑ open up ↑
 431  434  int get_struct_and_member(struct expression *expr, const char **type, const char **member);
 432  435  char *get_member_name(struct expression *expr);
 433  436  char *get_fnptr_name(struct expression *expr);
 434  437  int cmp_pos(struct position pos1, struct position pos2);
 435  438  int positions_eq(struct position pos1, struct position pos2);
 436  439  struct statement *get_current_statement(void);
 437  440  struct statement *get_prev_statement(void);
 438  441  struct expression *get_last_expr_from_expression_stmt(struct expression *expr);
 439  442  int get_param_num_from_sym(struct symbol *sym);
 440  443  int get_param_num(struct expression *expr);
      444 +struct symbol *get_param_sym_from_num(int num);
 441  445  int ms_since(struct timeval *start);
 442  446  int parent_is_gone_var_sym(const char *name, struct symbol *sym);
 443  447  int parent_is_gone(struct expression *expr);
 444  448  int invert_op(int op);
 445  449  int op_remove_assign(int op);
 446  450  int expr_equiv(struct expression *one, struct expression *two);
 447  451  void push_int(struct int_stack **stack, int num);
 448  452  int pop_int(struct int_stack **stack);
 449  453  
 450  454  /* smatch_type.c */
↓ open down ↓ 348 lines elided ↑ open up ↑
 799  803          PARAM_CLEARED   = 101,
 800  804          PARAM_LIMIT     = 103,
 801  805          PARAM_FILTER    = 104,
 802  806  
 803  807          PARAM_VALUE     = 1001,
 804  808          BUF_SIZE        = 1002,
 805  809          CAPPED_DATA     = 1004,
 806  810          RETURN_VALUE    = 1005,
 807  811          DEREFERENCE     = 1006,
 808  812          RANGE_CAP       = 1007,
 809      -        LOCK_HELD       = 1008,
 810      -        LOCK_RELEASED   = 1009,
 811  813          ABSOLUTE_LIMITS = 1010,
 812  814          PARAM_ADD       = 1012,
 813  815          PARAM_FREED     = 1013,
 814  816          DATA_SOURCE     = 1014,
 815  817          FUZZY_MAX       = 1015,
 816  818          HARD_MAX        = 2015,
 817  819          STR_LEN         = 1016,
 818  820          ARRAY_LEN       = 1017,
 819  821          CAPABLE         = 1018,
 820  822          NS_CAPABLE      = 1019,
↓ open down ↓ 17 lines elided ↑ open up ↑
 838  840          STMT_CNT        = 1037,
 839  841          TERMINATED      = 1038,
 840  842  
 841  843          /* put random temporary stuff in the 7000-7999 range for testing */
 842  844          USER_DATA       = 8017,
 843  845          USER_DATA_SET   = 9017,
 844  846          NO_OVERFLOW     = 8018,
 845  847          NO_OVERFLOW_SIMPLE = 8019,
 846  848          LOCKED          = 8020,
 847  849          UNLOCKED        = 8021,
      850 +        HALF_LOCKED     = 9022,
      851 +        LOCK_RESTORED   = 9023,
      852 +        KNOWN_LOCKED    = 9024,
      853 +        KNOWN_UNLOCKED  = 9025,
 848  854          SET_FS          = 8022,
 849  855          ATOMIC_INC      = 8023,
 850  856          ATOMIC_DEC      = 8024,
 851  857          NO_SIDE_EFFECT  = 8025,
 852  858          FN_ARG_LINK     = 8028,
 853  859          DATA_VALUE      = 8029,
 854  860          ARRAYSIZE_ARG   = 8033,
 855  861          SIZEOF_ARG      = 8034,
 856  862          MEMORY_TAG      = 8036,
 857  863          MTAG_ASSIGN     = 8035,
↓ open down ↓ 22 lines elided ↑ open up ↑
 880  886  struct range_list *db_return_vals_no_args(struct expression *expr);
 881  887  char *return_state_to_var_sym(struct expression *expr, int param, const char *key, struct symbol **sym);
 882  888  char *get_chunk_from_key(struct expression *arg, char *key, struct symbol **sym, struct var_sym_list **vsl);
 883  889  char *get_variable_from_key(struct expression *arg, const char *key, struct symbol **sym);
 884  890  const char *state_name_to_param_name(const char *state_name, const char *param_name);
 885  891  const char *get_param_name_var_sym(const char *name, struct symbol *sym);
 886  892  const char *get_param_name(struct sm_state *sm);
 887  893  const char *get_mtag_name_var_sym(const char *state_name, struct symbol *sym);
 888  894  const char *get_mtag_name_expr(struct expression *expr);
 889  895  char *get_data_info_name(struct expression *expr);
      896 +char *sm_to_arg_name(struct expression *expr, struct sm_state *sm);
 890  897  int is_recursive_member(const char *param_name);
 891  898  
 892  899  char *escape_newlines(const char *str);
 893  900  void sql_exec(struct sqlite3 *db, int (*callback)(void*, int, char**, char**), void *data, const char *sql);
 894  901  
 895  902  #define sql_helper(db, call_back, data, sql...)                                 \
 896  903  do {                                                                            \
 897  904          char sql_txt[1024];                                                     \
 898  905                                                                                  \
 899  906          sqlite3_snprintf(sizeof(sql_txt), sql_txt, sql);                        \
 900      -        sm_debug("debug: %s\n", sql_txt);                                       \
      907 +        db_debug("debug: %s\n", sql_txt);                                       \
 901  908          sql_exec(db, call_back, data, sql_txt);                                 \
 902  909  } while (0)
 903  910  
 904  911  
 905  912  #define run_sql(call_back, data, sql...)                                        \
 906  913  do {                                                                            \
 907  914          if (option_no_db)                                                       \
 908  915                  break;                                                          \
 909  916          sql_helper(smatch_db, call_back, data, sql);                            \
 910  917  } while (0)
↓ open down ↓ 13 lines elided ↑ open up ↑
 924  931          if (_db) {                                                              \
 925  932                  char buf[1024];                                                 \
 926  933                  char *err, *p = buf;                                            \
 927  934                  int rc;                                                         \
 928  935                                                                                  \
 929  936                  p += snprintf(p, buf + sizeof(buf) - p,                         \
 930  937                                "insert %sinto %s values (",                      \
 931  938                                ignore ? "or ignore " : "", #table);              \
 932  939                  p += snprintf(p, buf + sizeof(buf) - p, values);                \
 933  940                  p += snprintf(p, buf + sizeof(buf) - p, ");");                  \
 934      -                sm_debug("mem-db: %s\n", buf);                                  \
      941 +                db_debug("mem-db: %s\n", buf);                                  \
 935  942                  rc = sqlite3_exec(_db, buf, NULL, NULL, &err);                  \
 936  943                  if (rc != SQLITE_OK) {                                          \
 937  944                          sm_ierror("SQL error #2: %s", err);                     \
 938  945                          sm_ierror("SQL: '%s'", buf);                            \
 939  946                          parse_error = 1;                                        \
 940  947                  }                                                               \
 941  948                  break;                                                          \
 942  949          }                                                                       \
 943  950          if (option_info) {                                                      \
 944  951                  FILE *tmp_fd = sm_outfd;                                        \
↓ open down ↓ 55 lines elided ↑ open up ↑
1000 1007  int open_schema_file(const char *schema);
1001 1008  struct token *get_tokens_file(const char *filename);
1002 1009  
1003 1010  /* smatch.c */
1004 1011  extern char *option_debug_check;
1005 1012  extern char *option_project_str;
1006 1013  extern char *bin_dir;
1007 1014  extern char *data_dir;
1008 1015  extern int option_no_data;
1009 1016  extern int option_full_path;
1010      -extern int option_param_mapper;
1011 1017  extern int option_call_tree;
1012 1018  extern int num_checks;
1013 1019  
1014 1020  enum project_type {
1015 1021          PROJ_NONE,
1016 1022          PROJ_KERNEL,
1017 1023          PROJ_WINE,
1018 1024          PROJ_ILLUMOS_KERNEL,
1019 1025          PROJ_ILLUMOS_USER,
1020 1026          PROJ_UNKNOWN,
↓ open down ↓ 34 lines elided ↑ open up ↑
1055 1061  void print_held_locks();
1056 1062  
1057 1063  /* check_assigned_expr.c */
1058 1064  struct expression *get_assigned_expr(struct expression *expr);
1059 1065  struct expression *get_assigned_expr_name_sym(const char *name, struct symbol *sym);
1060 1066  /* smatch_return_to_param.c */
1061 1067  void __add_return_to_param_mapping(struct expression *assign, const char *return_string);
1062 1068  char *map_call_to_param_name_sym(struct expression *expr, struct symbol **sym);
1063 1069  
1064 1070  /* smatch_comparison.c */
     1071 +extern int comparison_id;
1065 1072  #define UNKNOWN_COMPARISON 0
1066 1073  #define IMPOSSIBLE_COMPARISON -1
1067 1074  struct compare_data {
1068 1075          /* The ->left and ->right expression pointers might be NULL (I'm lazy) */
1069 1076          struct expression *left;
1070 1077          const char *left_var;
1071 1078          struct var_sym_list *left_vsl;
1072 1079          int comparison;
1073 1080          struct expression *right;
1074 1081          const char *right_var;
↓ open down ↓ 35 lines elided ↑ open up ↑
1110 1117  void __compare_param_limit_hook(struct expression *left_expr, struct expression *right_expr,
1111 1118                                  const char *state_name,
1112 1119                                  struct smatch_state *true_state, struct smatch_state *false_state);
1113 1120  int impossibly_high_comparison(struct expression *expr);
1114 1121  
1115 1122  /* smatch_sval.c */
1116 1123  sval_t *sval_alloc(sval_t sval);
1117 1124  sval_t *sval_alloc_permanent(sval_t sval);
1118 1125  sval_t sval_blank(struct expression *expr);
1119 1126  sval_t sval_type_val(struct symbol *type, long long val);
     1127 +sval_t sval_type_fval(struct symbol *type, long double fval);
1120 1128  sval_t sval_from_val(struct expression *expr, long long val);
     1129 +sval_t sval_from_fval(struct expression *expr, long double fval);
1121 1130  int sval_is_ptr(sval_t sval);
     1131 +bool sval_is_fp(sval_t sval);
1122 1132  int sval_unsigned(sval_t sval);
1123 1133  int sval_signed(sval_t sval);
1124 1134  int sval_bits(sval_t sval);
1125 1135  int sval_bits_used(sval_t sval);
1126 1136  int sval_is_negative(sval_t sval);
1127 1137  int sval_is_positive(sval_t sval);
1128 1138  int sval_is_min(sval_t sval);
1129 1139  int sval_is_max(sval_t sval);
1130 1140  int sval_is_a_min(sval_t sval);
1131 1141  int sval_is_a_max(sval_t sval);
↓ open down ↓ 32 lines elided ↑ open up ↑
1164 1174  int has_symbol(struct expression *expr, struct symbol *sym);
1165 1175  int has_variable(struct expression *expr, struct expression *var);
1166 1176  int has_inc_dec(struct expression *expr);
1167 1177  
1168 1178  /* smatch_stored_conditions.c */
1169 1179  struct smatch_state *get_stored_condition(struct expression *expr);
1170 1180  struct expression_list *get_conditions(struct expression *expr);
1171 1181  struct sm_state *stored_condition_implication_hook(struct expression *expr,
1172 1182                          struct state_list **true_stack,
1173 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);
1174 1188  
1175 1189  /* check_string_len.c */
1176 1190  int get_formatted_string_size(struct expression *call, int arg);
1177 1191  int get_formatted_string_min_size(struct expression *call, int arg);
1178 1192  
1179 1193  /* smatch_param_set.c */
1180 1194  int param_was_set(struct expression *expr);
1181 1195  int param_was_set_var_sym(const char *name, struct symbol *sym);
1182 1196  void print_limited_param_set(int return_id, char *return_ranges, struct expression *expr);
1183 1197  /* smatch_param_filter.c */
↓ open down ↓ 81 lines elided ↑ open up ↑
1265 1279  bool is_ignored_kernel_data(const char *name);
1266 1280  
1267 1281  static inline bool type_is_ptr(struct symbol *type)
1268 1282  {
1269 1283          return type &&
1270 1284                 (type->type == SYM_PTR ||
1271 1285                  type->type == SYM_ARRAY ||
1272 1286                  type->type == SYM_FN);
1273 1287  }
1274 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 +
1275 1297  static inline int type_bits(struct symbol *type)
1276 1298  {
1277 1299          if (!type)
1278 1300                  return 0;
1279 1301          if (type_is_ptr(type))
1280 1302                  return bits_in_pointer;
1281 1303          if (!type->examined)
1282 1304                  examine_symbol_type(type);
1283 1305          return type->bit_size;
1284 1306  }
↓ open down ↓ 21 lines elided ↑ open up ↑
1306 1328  }
1307 1329  
1308 1330  static inline int sval_positive_bits(sval_t sval)
1309 1331  {
1310 1332          return type_positive_bits(sval.type);
1311 1333  }
1312 1334  
1313 1335  /*
1314 1336   * Returns -1 if one is smaller, 0 if they are the same and 1 if two is larger.
1315 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 +
1316 1378  static inline int sval_cmp(sval_t one, sval_t two)
1317 1379  {
1318 1380          struct symbol *type;
1319 1381  
     1382 +        if (sval_is_fp(one) || sval_is_fp(two))
     1383 +                return fp_cmp(one, two);
     1384 +
1320 1385          type = one.type;
1321 1386          if (sval_positive_bits(two) > sval_positive_bits(one))
1322 1387                  type = two.type;
1323 1388          if (type_bits(type) < 31)
1324 1389                  type = &int_ctype;
1325 1390  
1326 1391          one = sval_cast(type, one);
1327 1392          two = sval_cast(type, two);
1328 1393  
1329 1394          if (type_unsigned(type)) {
↓ open down ↓ 15 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX