1 /* 2 * Copyright (C) 2006 Dan Carpenter. 3 * 4 * This program is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU General Public License 6 * as published by the Free Software Foundation; either version 2 7 * of the License, or (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt 16 * 17 * 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; 64 } 65 66 static inline int PTR_INT(void *p) 67 { 68 return (int)(long)p; 69 } 70 71 struct tracker { 72 char *name; 73 struct symbol *sym; 74 unsigned short owner; 75 }; 76 DECLARE_ALLOCATOR(tracker); 77 DECLARE_PTR_LIST(tracker_list, struct tracker); 78 DECLARE_PTR_LIST(stree_stack, struct stree); 79 80 /* The first 3 struct members must match struct tracker */ 81 struct sm_state { 82 const char *name; 83 struct symbol *sym; 84 unsigned short owner; 85 unsigned short merged:1; 86 unsigned int line; 87 struct smatch_state *state; 88 struct stree *pool; 89 struct sm_state *left; 90 struct sm_state *right; 91 struct state_list *possible; 92 }; 93 94 struct var_sym { 95 char *var; 96 struct symbol *sym; 97 }; 98 DECLARE_ALLOCATOR(var_sym); 99 DECLARE_PTR_LIST(var_sym_list, struct var_sym); 100 101 struct constraint { 102 int op; 103 int id; 104 }; 105 DECLARE_PTR_LIST(constraint_list, struct constraint); 106 107 struct alloc_info { 108 const char *fn; 109 int size_param, nr; 110 }; 111 extern struct alloc_info *alloc_funcs; 112 113 struct bit_info { 114 unsigned long long set; 115 unsigned long long possible; 116 }; 117 118 enum hook_type { 119 EXPR_HOOK, 120 EXPR_HOOK_AFTER, 121 STMT_HOOK, 122 STMT_HOOK_AFTER, 123 SYM_HOOK, 124 STRING_HOOK, 125 DECLARATION_HOOK, 126 ASSIGNMENT_HOOK, 127 ASSIGNMENT_HOOK_AFTER, 128 RAW_ASSIGNMENT_HOOK, 129 GLOBAL_ASSIGNMENT_HOOK, 130 LOGIC_HOOK, 131 CONDITION_HOOK, 132 PRELOOP_HOOK, 133 SELECT_HOOK, 134 WHOLE_CONDITION_HOOK, 135 FUNCTION_CALL_HOOK_BEFORE, 136 FUNCTION_CALL_HOOK, 137 CALL_HOOK_AFTER_INLINE, 138 FUNCTION_CALL_HOOK_AFTER_DB, 139 CALL_ASSIGNMENT_HOOK, 140 MACRO_ASSIGNMENT_HOOK, 141 BINOP_HOOK, 142 OP_HOOK, 143 DEREF_HOOK, 144 CASE_HOOK, 145 ASM_HOOK, 146 CAST_HOOK, 147 SIZEOF_HOOK, 148 BASE_HOOK, 149 FUNC_DEF_HOOK, 150 AFTER_DEF_HOOK, 151 END_FUNC_HOOK, 152 AFTER_FUNC_HOOK, 153 RETURN_HOOK, 154 INLINE_FN_START, 155 INLINE_FN_END, 156 END_FILE_HOOK, 157 NUM_HOOKS, 158 }; 159 160 #define TRUE 1 161 #define FALSE 0 162 163 struct range_list; 164 165 void add_hook(void *func, enum hook_type type); 166 typedef struct smatch_state *(merge_func_t)(struct smatch_state *s1, struct smatch_state *s2); 167 typedef struct smatch_state *(unmatched_func_t)(struct sm_state *state); 168 void add_merge_hook(int client_id, merge_func_t *func); 169 void add_unmatched_state_hook(int client_id, unmatched_func_t *func); 170 void add_pre_merge_hook(int client_id, void (*hook)(struct sm_state *cur, struct sm_state *other)); 171 typedef void (scope_hook)(void *data); 172 void add_scope_hook(scope_hook *hook, void *data); 173 typedef void (func_hook)(const char *fn, struct expression *expr, void *data); 174 typedef void (implication_hook)(const char *fn, struct expression *call_expr, 175 struct expression *assign_expr, void *data); 176 typedef void (return_implies_hook)(struct expression *call_expr, 177 int param, char *key, char *value); 178 typedef int (implied_return_hook)(struct expression *call_expr, void *info, struct range_list **rl); 179 void add_function_hook(const char *look_for, func_hook *call_back, void *data); 180 181 void add_function_assign_hook(const char *look_for, func_hook *call_back, 182 void *info); 183 void add_implied_return_hook(const char *look_for, 184 implied_return_hook *call_back, 185 void *info); 186 void add_macro_assign_hook(const char *look_for, func_hook *call_back, 187 void *info); 188 void add_macro_assign_hook_extra(const char *look_for, func_hook *call_back, 189 void *info); 190 void return_implies_state(const char *look_for, long long start, long long end, 191 implication_hook *call_back, void *info); 192 void return_implies_state_sval(const char *look_for, sval_t start, sval_t end, 193 implication_hook *call_back, void *info); 194 void select_return_states_hook(int type, return_implies_hook *callback); 195 void select_return_states_before(void (*fn)(void)); 196 void select_return_states_after(void (*fn)(void)); 197 int get_implied_return(struct expression *expr, struct range_list **rl); 198 void allocate_hook_memory(void); 199 200 struct modification_data { 201 struct smatch_state *prev; 202 struct expression *cur; 203 }; 204 205 typedef void (modification_hook)(struct sm_state *sm, struct expression *mod_expr); 206 void add_modification_hook(int owner, modification_hook *call_back); 207 void add_modification_hook_late(int owner, modification_hook *call_back); 208 struct smatch_state *get_modification_state(struct expression *expr); 209 210 int outside_of_function(void); 211 const char *get_filename(void); 212 const char *get_base_file(void); 213 char *get_function(void); 214 int get_lineno(void); 215 extern int final_pass; 216 extern struct symbol *cur_func_sym; 217 extern int option_debug; 218 extern int local_debug; 219 extern int debug_db; 220 bool debug_implied(void); 221 extern int option_info; 222 extern int option_spammy; 223 extern int option_timeout; 224 extern char *trace_variable; 225 extern struct stree *global_states; 226 int is_skipped_function(void); 227 int is_silenced_function(void); 228 extern bool implications_off; 229 230 /* smatch_impossible.c */ 231 int is_impossible_path(void); 232 void set_path_impossible(void); 233 234 extern FILE *sm_outfd; 235 extern FILE *sql_outfd; 236 extern FILE *caller_info_fd; 237 extern int sm_nr_checks; 238 extern int sm_nr_errors; 239 extern const char *progname; 240 241 /* 242 * How to use these routines: 243 * 244 * sm_fatal(): an internal error of some kind that should immediately exit 245 * sm_ierror(): an internal error 246 * sm_perror(): an internal error from parsing input source 247 * sm_error(): an error from input source 248 * sm_warning(): a warning from input source 249 * sm_info(): info message (from option_info) 250 * sm_debug(): debug message 251 * sm_msg(): other message (please avoid using this) 252 */ 253 254 #define sm_printf(msg...) do { \ 255 if (final_pass || option_debug || local_debug || debug_db) \ 256 fprintf(sm_outfd, msg); \ 257 } while (0) 258 259 static inline void sm_prefix(void) 260 { 261 sm_printf("%s: %s:%d %s() ", progname, get_filename(), get_lineno(), get_function()); 262 } 263 264 static inline void print_implied_debug_msg(); 265 266 extern bool __silence_warnings_for_stmt; 267 268 #define sm_print_msg(type, msg...) \ 269 do { \ 270 print_implied_debug_msg(); \ 271 if (!final_pass && !option_debug && !local_debug && !debug_db) \ 272 break; \ 273 if (__silence_warnings_for_stmt && !option_debug && !local_debug) \ 274 break; \ 275 if (!option_info && is_silenced_function()) \ 276 break; \ 277 sm_prefix(); \ 278 if (type == 1) { \ 279 sm_printf("warn: "); \ 280 sm_nr_checks++; \ 281 } else if (type == 2) { \ 282 sm_printf("error: "); \ 283 sm_nr_checks++; \ 284 } else if (type == 3) { \ 285 sm_printf("parse error: "); \ 286 sm_nr_errors++; \ 287 } \ 288 sm_printf(msg); \ 289 sm_printf("\n"); \ 290 } while (0) 291 292 #define sm_msg(msg...) do { sm_print_msg(0, msg); } while (0) 293 294 extern char *implied_debug_msg; 295 static inline void print_implied_debug_msg(void) 296 { 297 static struct symbol *last_printed = NULL; 298 299 if (!implied_debug_msg) 300 return; 301 if (last_printed == cur_func_sym) 302 return; 303 last_printed = cur_func_sym; 304 sm_msg("%s", implied_debug_msg); 305 } 306 307 #define sm_debug(msg...) do { if (option_debug) sm_printf(msg); } while (0) 308 #define db_debug(msg...) do { if (option_debug || debug_db) sm_printf(msg); } while (0) 309 310 #define sm_info(msg...) do { \ 311 if (option_debug || (option_info && final_pass)) { \ 312 sm_prefix(); \ 313 sm_printf("info: "); \ 314 sm_printf(msg); \ 315 sm_printf("\n"); \ 316 } \ 317 } while(0) 318 319 #define sm_warning(msg...) do { sm_print_msg(1, msg); } while (0) 320 #define sm_error(msg...) do { sm_print_msg(2, msg); } while (0) 321 #define sm_perror(msg...) do { sm_print_msg(3, msg); } while (0) 322 323 static inline void sm_fatal(const char *fmt, ...) 324 { 325 va_list args; 326 327 va_start(args, fmt); 328 vfprintf(sm_outfd, fmt, args); 329 va_end(args); 330 331 fprintf(sm_outfd, "\n"); 332 333 exit(1); 334 } 335 336 static inline void sm_ierror(const char *fmt, ...) 337 { 338 va_list args; 339 340 sm_nr_errors++; 341 342 fprintf(sm_outfd, "internal error: "); 343 344 va_start(args, fmt); 345 vfprintf(sm_outfd, fmt, args); 346 va_end(args); 347 348 fprintf(sm_outfd, "\n"); 349 } 350 #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1)) 351 352 struct smatch_state *__get_state(int owner, const char *name, struct symbol *sym); 353 struct smatch_state *get_state(int owner, const char *name, struct symbol *sym); 354 struct smatch_state *get_state_expr(int owner, struct expression *expr); 355 struct state_list *get_possible_states(int owner, const char *name, 356 struct symbol *sym); 357 struct state_list *get_possible_states_expr(int owner, struct expression *expr); 358 struct sm_state *set_state(int owner, const char *name, struct symbol *sym, 359 struct smatch_state *state); 360 struct sm_state *set_state_expr(int owner, struct expression *expr, 361 struct smatch_state *state); 362 void delete_state(int owner, const char *name, struct symbol *sym); 363 void delete_state_expr(int owner, struct expression *expr); 364 void __delete_all_states_sym(struct symbol *sym); 365 void set_true_false_states(int owner, const char *name, struct symbol *sym, 366 struct smatch_state *true_state, 367 struct smatch_state *false_state); 368 void set_true_false_states_expr(int owner, struct expression *expr, 369 struct smatch_state *true_state, 370 struct smatch_state *false_state); 371 372 struct stree *get_all_states_from_stree(int owner, struct stree *source); 373 struct stree *get_all_states_stree(int id); 374 struct stree *__get_cur_stree(void); 375 int is_reachable(void); 376 void add_get_state_hook(void (*fn)(int owner, const char *name, struct symbol *sym)); 377 378 /* smatch_helper.c */ 379 DECLARE_PTR_LIST(int_stack, int); 380 char *alloc_string(const char *str); 381 char *alloc_string_newline(const char *str); 382 void free_string(char *str); 383 void append(char *dest, const char *data, int buff_len); 384 void remove_parens(char *str); 385 struct smatch_state *alloc_state_num(int num); 386 struct smatch_state *alloc_state_str(const char *name); 387 struct smatch_state *merge_str_state(struct smatch_state *s1, struct smatch_state *s2); 388 struct smatch_state *alloc_state_expr(struct expression *expr); 389 struct expression *get_argument_from_call_expr(struct expression_list *args, 390 int num); 391 392 char *expr_to_var(struct expression *expr); 393 struct symbol *expr_to_sym(struct expression *expr); 394 char *expr_to_str(struct expression *expr); 395 char *expr_to_str_sym(struct expression *expr, 396 struct symbol **sym_ptr); 397 char *expr_to_var_sym(struct expression *expr, 398 struct symbol **sym_ptr); 399 char *expr_to_known_chunk_sym(struct expression *expr, struct symbol **sym); 400 char *expr_to_chunk_sym_vsl(struct expression *expr, struct symbol **sym, struct var_sym_list **vsl); 401 int get_complication_score(struct expression *expr); 402 403 int sym_name_is(const char *name, struct expression *expr); 404 int get_const_value(struct expression *expr, sval_t *sval); 405 int get_value(struct expression *expr, sval_t *val); 406 int get_implied_value(struct expression *expr, sval_t *val); 407 int get_implied_value_fast(struct expression *expr, sval_t *sval); 408 int get_implied_min(struct expression *expr, sval_t *sval); 409 int get_implied_max(struct expression *expr, sval_t *val); 410 int get_hard_max(struct expression *expr, sval_t *sval); 411 int get_fuzzy_min(struct expression *expr, sval_t *min); 412 int get_fuzzy_max(struct expression *expr, sval_t *max); 413 int get_absolute_min(struct expression *expr, sval_t *sval); 414 int get_absolute_max(struct expression *expr, sval_t *sval); 415 int parse_call_math(struct expression *expr, char *math, sval_t *val); 416 int parse_call_math_rl(struct expression *call, const char *math, struct range_list **rl); 417 const char *get_allocation_math(struct expression *expr); 418 char *get_value_in_terms_of_parameter_math(struct expression *expr); 419 char *get_value_in_terms_of_parameter_math_var_sym(const char *var, struct symbol *sym); 420 int expr_is_zero(struct expression *expr); 421 int known_condition_true(struct expression *expr); 422 int known_condition_false(struct expression *expr); 423 int implied_condition_true(struct expression *expr); 424 int implied_condition_false(struct expression *expr); 425 int can_integer_overflow(struct symbol *type, struct expression *expr); 426 void clear_math_cache(void); 427 void set_fast_math_only(void); 428 void clear_fast_math_only(void); 429 430 int is_array(struct expression *expr); 431 struct expression *get_array_base(struct expression *expr); 432 struct expression *get_array_offset(struct expression *expr); 433 const char *show_state(struct smatch_state *state); 434 struct statement *get_expression_statement(struct expression *expr); 435 struct expression *strip_parens(struct expression *expr); 436 struct expression *strip_expr(struct expression *expr); 437 struct expression *strip_expr_set_parent(struct expression *expr); 438 void scoped_state(int my_id, const char *name, struct symbol *sym); 439 int is_error_return(struct expression *expr); 440 int getting_address(struct expression *expr); 441 int get_struct_and_member(struct expression *expr, const char **type, const char **member); 442 char *get_member_name(struct expression *expr); 443 char *get_fnptr_name(struct expression *expr); 444 int cmp_pos(struct position pos1, struct position pos2); 445 int positions_eq(struct position pos1, struct position pos2); 446 struct statement *get_current_statement(void); 447 struct statement *get_prev_statement(void); 448 struct expression *get_last_expr_from_expression_stmt(struct expression *expr); 449 int get_param_num_from_sym(struct symbol *sym); 450 int get_param_num(struct expression *expr); 451 struct symbol *get_param_sym_from_num(int num); 452 int ms_since(struct timeval *start); 453 int parent_is_gone_var_sym(const char *name, struct symbol *sym); 454 int parent_is_gone(struct expression *expr); 455 int invert_op(int op); 456 int op_remove_assign(int op); 457 int expr_equiv(struct expression *one, struct expression *two); 458 void push_int(struct int_stack **stack, int num); 459 int pop_int(struct int_stack **stack); 460 461 /* smatch_type.c */ 462 struct symbol *get_real_base_type(struct symbol *sym); 463 int type_bytes(struct symbol *type); 464 int array_bytes(struct symbol *type); 465 struct symbol *get_pointer_type(struct expression *expr); 466 struct symbol *get_type(struct expression *expr); 467 struct symbol *get_final_type(struct expression *expr); 468 struct symbol *get_promoted_type(struct symbol *left, struct symbol *right); 469 int type_signed(struct symbol *base_type); 470 int expr_unsigned(struct expression *expr); 471 int expr_signed(struct expression *expr); 472 int returns_unsigned(struct symbol *base_type); 473 int is_pointer(struct expression *expr); 474 int returns_pointer(struct symbol *base_type); 475 sval_t sval_type_max(struct symbol *base_type); 476 sval_t sval_type_min(struct symbol *base_type); 477 int nr_bits(struct expression *expr); 478 int is_void_pointer(struct expression *expr); 479 int is_char_pointer(struct expression *expr); 480 int is_string(struct expression *expr); 481 int is_static(struct expression *expr); 482 bool is_local_variable(struct expression *expr); 483 int types_equiv(struct symbol *one, struct symbol *two); 484 int fn_static(void); 485 const char *global_static(); 486 struct symbol *cur_func_return_type(void); 487 struct symbol *get_arg_type(struct expression *fn, int arg); 488 struct symbol *get_member_type_from_key(struct expression *expr, const char *key); 489 struct symbol *get_arg_type_from_key(struct expression *fn, int param, struct expression *arg, const char *key); 490 int is_struct(struct expression *expr); 491 char *type_to_str(struct symbol *type); 492 493 /* smatch_ignore.c */ 494 void add_ignore(int owner, const char *name, struct symbol *sym); 495 int is_ignored(int owner, const char *name, struct symbol *sym); 496 void add_ignore_expr(int owner, struct expression *expr); 497 int is_ignored_expr(int owner, struct expression *expr); 498 499 /* smatch_var_sym */ 500 struct var_sym *alloc_var_sym(const char *var, struct symbol *sym); 501 struct var_sym_list *expr_to_vsl(struct expression *expr); 502 void add_var_sym(struct var_sym_list **list, const char *var, struct symbol *sym); 503 void add_var_sym_expr(struct var_sym_list **list, struct expression *expr); 504 void del_var_sym(struct var_sym_list **list, const char *var, struct symbol *sym); 505 int in_var_sym_list(struct var_sym_list *list, const char *var, struct symbol *sym); 506 struct var_sym_list *clone_var_sym_list(struct var_sym_list *from_vsl); 507 void merge_var_sym_list(struct var_sym_list **dest, struct var_sym_list *src); 508 struct var_sym_list *combine_var_sym_lists(struct var_sym_list *one, struct var_sym_list *two); 509 int var_sym_lists_equiv(struct var_sym_list *one, struct var_sym_list *two); 510 void free_var_sym_list(struct var_sym_list **list); 511 void free_var_syms_and_list(struct var_sym_list **list); 512 513 /* smatch_tracker */ 514 struct tracker *alloc_tracker(int owner, const char *name, struct symbol *sym); 515 void add_tracker(struct tracker_list **list, int owner, const char *name, 516 struct symbol *sym); 517 void add_tracker_expr(struct tracker_list **list, int owner, struct expression *expr); 518 void del_tracker(struct tracker_list **list, int owner, const char *name, 519 struct symbol *sym); 520 int in_tracker_list(struct tracker_list *list, int owner, const char *name, 521 struct symbol *sym); 522 void free_tracker_list(struct tracker_list **list); 523 void free_trackers_and_list(struct tracker_list **list); 524 525 /* smatch_conditions */ 526 int in_condition(void); 527 528 /* smatch_flow.c */ 529 530 extern int __in_fake_assign; 531 extern int __in_fake_parameter_assign; 532 extern int __in_fake_struct_assign; 533 extern int in_fake_env; 534 void smatch (struct string_list *filelist); 535 int inside_loop(void); 536 int definitely_inside_loop(void); 537 struct expression *get_switch_expr(void); 538 int in_expression_statement(void); 539 void __process_post_op_stack(void); 540 void __split_expr(struct expression *expr); 541 void __split_label_stmt(struct statement *stmt); 542 void __split_stmt(struct statement *stmt); 543 extern int __in_function_def; 544 extern int __in_unmatched_hook; 545 extern int option_assume_loops; 546 extern int option_two_passes; 547 extern int option_no_db; 548 extern int option_file_output; 549 extern int option_time; 550 extern struct expression_list *big_expression_stack; 551 extern struct expression_list *big_condition_stack; 552 extern struct statement_list *big_statement_stack; 553 int is_assigned_call(struct expression *expr); 554 int inlinable(struct expression *expr); 555 extern int __inline_call; 556 extern struct expression *__inline_fn; 557 extern int __in_pre_condition; 558 extern int __bail_on_rest_of_function; 559 extern struct statement *__prev_stmt; 560 extern struct statement *__cur_stmt; 561 extern struct statement *__next_stmt; 562 void init_fake_env(void); 563 void end_fake_env(void); 564 int time_parsing_function(void); 565 bool taking_too_long(void); 566 567 /* smatch_struct_assignment.c */ 568 struct expression *get_faked_expression(void); 569 void __fake_struct_member_assignments(struct expression *expr); 570 571 /* smatch_project.c */ 572 int is_no_inline_function(const char *function); 573 574 /* smatch_conditions */ 575 void __split_whole_condition(struct expression *expr); 576 void __handle_logic(struct expression *expr); 577 int is_condition(struct expression *expr); 578 int __handle_condition_assigns(struct expression *expr); 579 int __handle_select_assigns(struct expression *expr); 580 int __handle_expr_statement_assigns(struct expression *expr); 581 582 /* smatch_implied.c */ 583 struct range_list_stack; 584 void param_limit_implications(struct expression *expr, int param, char *key, char *value); 585 struct stree *__implied_case_stree(struct expression *switch_expr, 586 struct range_list *case_rl, 587 struct range_list_stack **remaining_cases, 588 struct stree **raw_stree); 589 void overwrite_states_using_pool(struct sm_state *gate_sm, struct sm_state *pool_sm); 590 int assume(struct expression *expr); 591 void end_assume(void); 592 int impossible_assumption(struct expression *left, int op, sval_t sval); 593 594 /* smatch_slist.h */ 595 bool has_dynamic_states(unsigned short owner); 596 void set_dynamic_states(unsigned short owner); 597 598 /* smatch_extras.c */ 599 int in_warn_on_macro(void); 600 #define SMATCH_EXTRA 5 /* this is my_id from smatch extra set in smatch.c */ 601 extern int RETURN_ID; 602 603 struct data_range { 604 sval_t min; 605 sval_t max; 606 }; 607 608 #define MTAG_ALIAS_BIT (1ULL << 63) 609 #define MTAG_OFFSET_MASK 0xfffULL 610 #define MTAG_SEED 0xdead << 12 611 612 const extern unsigned long valid_ptr_min; 613 extern unsigned long valid_ptr_max; 614 extern const sval_t valid_ptr_min_sval; 615 extern sval_t valid_ptr_max_sval; 616 extern struct range_list *valid_ptr_rl; 617 void alloc_valid_ptr_rl(void); 618 619 static const sval_t array_min_sval = { 620 .type = &ptr_ctype, 621 {.value = 100000}, 622 }; 623 static const sval_t array_max_sval = { 624 .type = &ptr_ctype, 625 {.value = ULONG_MAX - 4095}, 626 }; 627 static const sval_t text_seg_min = { 628 .type = &ptr_ctype, 629 {.value = 4096}, 630 }; 631 static const sval_t text_seg_max = { 632 .type = &ptr_ctype, 633 {.value = ULONG_MAX - 4095}, 634 }; 635 static const sval_t data_seg_min = { 636 .type = &ptr_ctype, 637 {.value = 4096}, 638 }; 639 static const sval_t data_seg_max = { 640 .type = &ptr_ctype, 641 {.value = ULONG_MAX - 4095}, 642 }; 643 static const sval_t bss_seg_min = { 644 .type = &ptr_ctype, 645 {.value = 4096}, 646 }; 647 static const sval_t bss_seg_max = { 648 .type = &ptr_ctype, 649 {.value = ULONG_MAX - 4095}, 650 }; 651 static const sval_t stack_seg_min = { 652 .type = &ptr_ctype, 653 {.value = 4096}, 654 }; 655 static const sval_t stack_seg_max = { 656 .type = &ptr_ctype, 657 {.value = ULONG_MAX - 4095}, 658 }; 659 static const sval_t kmalloc_seg_min = { 660 .type = &ptr_ctype, 661 {.value = 4096}, 662 }; 663 static const sval_t kmalloc_seg_max = { 664 .type = &ptr_ctype, 665 {.value = ULONG_MAX - 4095}, 666 }; 667 static const sval_t vmalloc_seg_min = { 668 .type = &ptr_ctype, 669 {.value = 4096}, 670 }; 671 static const sval_t vmalloc_seg_max = { 672 .type = &ptr_ctype, 673 {.value = ULONG_MAX - 4095}, 674 }; 675 static const sval_t fn_ptr_min = { 676 .type = &ptr_ctype, 677 {.value = 4096}, 678 }; 679 static const sval_t fn_ptr_max = { 680 .type = &ptr_ctype, 681 {.value = ULONG_MAX - 4095}, 682 }; 683 684 char *get_other_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym); 685 char *map_call_to_other_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym); 686 char *map_long_to_short_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym, bool use_stack); 687 688 #define STRLEN_MAX_RET 1010101 689 690 /* smatch_absolute.c */ 691 int get_absolute_min_helper(struct expression *expr, sval_t *sval); 692 int get_absolute_max_helper(struct expression *expr, sval_t *sval); 693 694 /* smatch_type_value.c */ 695 int get_db_type_rl(struct expression *expr, struct range_list **rl); 696 /* smatch_data_val.c */ 697 int get_mtag_rl(struct expression *expr, struct range_list **rl); 698 /* smatch_array_values.c */ 699 int get_array_rl(struct expression *expr, struct range_list **rl); 700 701 /* smatch_states.c */ 702 struct stree *__swap_cur_stree(struct stree *stree); 703 void __push_fake_cur_stree(); 704 struct stree *__pop_fake_cur_stree(); 705 void __free_fake_cur_stree(); 706 void __set_fake_cur_stree_fast(struct stree *stree); 707 void __pop_fake_cur_stree_fast(void); 708 void __merge_stree_into_cur(struct stree *stree); 709 710 int unreachable(void); 711 void __set_cur_stree_readonly(void); 712 void __set_cur_stree_writable(void); 713 void __set_sm(struct sm_state *sm); 714 void __set_sm_cur_stree(struct sm_state *sm); 715 void __set_sm_fake_stree(struct sm_state *sm); 716 void __set_true_false_sm(struct sm_state *true_state, 717 struct sm_state *false_state); 718 void nullify_path(void); 719 void __match_nullify_path_hook(const char *fn, struct expression *expr, 720 void *unused); 721 void __unnullify_path(void); 722 int __path_is_null(void); 723 void save_all_states(void); 724 void restore_all_states(void); 725 void free_goto_stack(void); 726 void clear_all_states(void); 727 728 struct sm_state *get_sm_state(int owner, const char *name, 729 struct symbol *sym); 730 struct sm_state *get_sm_state_expr(int owner, struct expression *expr); 731 void __push_true_states(void); 732 void __use_false_states(void); 733 void __discard_false_states(void); 734 void __merge_false_states(void); 735 void __merge_true_states(void); 736 737 void __negate_cond_stacks(void); 738 void __use_pre_cond_states(void); 739 void __use_cond_true_states(void); 740 void __use_cond_false_states(void); 741 void __push_cond_stacks(void); 742 void __fold_in_set_states(void); 743 void __free_set_states(void); 744 struct stree *__copy_cond_true_states(void); 745 struct stree *__copy_cond_false_states(void); 746 struct stree *__pop_cond_true_stack(void); 747 struct stree *__pop_cond_false_stack(void); 748 void __and_cond_states(void); 749 void __or_cond_states(void); 750 void __save_pre_cond_states(void); 751 void __discard_pre_cond_states(void); 752 struct stree *__get_true_states(void); 753 struct stree *__get_false_states(void); 754 void __use_cond_states(void); 755 extern struct state_list *__last_base_slist; 756 757 void __push_continues(void); 758 void __discard_continues(void); 759 void __process_continues(void); 760 void __merge_continues(void); 761 762 void __push_breaks(void); 763 void __process_breaks(void); 764 int __has_breaks(void); 765 void __merge_breaks(void); 766 void __use_breaks(void); 767 768 void __save_switch_states(struct expression *switch_expr); 769 void __discard_switches(void); 770 int have_remaining_cases(void); 771 void __merge_switches(struct expression *switch_expr, struct range_list *case_rl); 772 void __push_default(void); 773 void __set_default(void); 774 int __pop_default(void); 775 776 void __push_conditions(void); 777 void __discard_conditions(void); 778 779 void __save_gotos(const char *name, struct symbol *sym); 780 void __merge_gotos(const char *name, struct symbol *sym); 781 782 void __print_cur_stree(void); 783 784 /* smatch_hooks.c */ 785 void __pass_to_client(void *data, enum hook_type type); 786 void __pass_case_to_client(struct expression *switch_expr, 787 struct range_list *rl); 788 int __has_merge_function(int client_id); 789 struct smatch_state *__client_merge_function(int owner, 790 struct smatch_state *s1, 791 struct smatch_state *s2); 792 struct smatch_state *__client_unmatched_state_function(struct sm_state *sm); 793 void call_pre_merge_hook(struct sm_state *cur, struct sm_state *other); 794 void __push_scope_hooks(void); 795 void __call_scope_hooks(void); 796 797 /* smatch_function_hooks.c */ 798 void create_function_hook_hash(void); 799 void __match_initializer_call(struct symbol *sym); 800 801 /* smatch_db.c */ 802 enum info_type { 803 INTERNAL = 0, 804 /* 805 * Changing these numbers is a pain. Don't do it. If you ever use a 806 * number it can't be re-used right away so there may be gaps. 807 * We select these in order by type so if the order matters, then give 808 * it a number below 100-999,9000-9999 ranges. */ 809 810 PARAM_CLEARED = 101, 811 PARAM_LIMIT = 103, 812 PARAM_FILTER = 104, 813 814 PARAM_VALUE = 1001, 815 BUF_SIZE = 1002, 816 CAPPED_DATA = 1004, 817 RETURN_VALUE = 1005, 818 DEREFERENCE = 1006, 819 RANGE_CAP = 1007, 820 ABSOLUTE_LIMITS = 1010, 821 PARAM_ADD = 1012, 822 PARAM_FREED = 1013, 823 DATA_SOURCE = 1014, 824 FUZZY_MAX = 1015, 825 HARD_MAX = 2015, 826 STR_LEN = 1016, 827 ARRAY_LEN = 1017, 828 CAPABLE = 1018, 829 NS_CAPABLE = 1019, 830 CONTAINER = 1020, 831 CASTED_CALL = 1021, 832 TYPE_LINK = 1022, 833 UNTRACKED_PARAM = 1023, 834 LOST_PARAM = 2023, 835 CULL_PATH = 1024, 836 PARAM_SET = 1025, 837 PARAM_USED = 1026, 838 BYTE_UNITS = 1027, 839 COMPARE_LIMIT = 1028, 840 PARAM_COMPARE = 1029, 841 CONSTRAINT = 1031, 842 PASSES_TYPE = 1032, 843 CONSTRAINT_REQUIRED = 1033, 844 BIT_INFO = 1034, 845 NOSPEC = 1035, 846 NOSPEC_WB = 1036, 847 STMT_CNT = 1037, 848 TERMINATED = 1038, 849 FRESH_ALLOC = 1044, 850 851 /* put random temporary stuff in the 7000-7999 range for testing */ 852 USER_DATA = 8017, 853 USER_DATA_SET = 9017, 854 NO_OVERFLOW = 8018, 855 NO_OVERFLOW_SIMPLE = 8019, 856 LOCKED = 8020, 857 UNLOCKED = 8021, 858 HALF_LOCKED = 9022, 859 LOCK_RESTORED = 9023, 860 KNOWN_LOCKED = 9024, 861 KNOWN_UNLOCKED = 9025, 862 SET_FS = 8022, 863 ATOMIC_INC = 8023, 864 ATOMIC_DEC = 8024, 865 NO_SIDE_EFFECT = 8025, 866 FN_ARG_LINK = 8028, 867 DATA_VALUE = 8029, 868 ARRAYSIZE_ARG = 8033, 869 SIZEOF_ARG = 8034, 870 MEMORY_TAG = 8036, 871 MTAG_ASSIGN = 8035, 872 STRING_VALUE = 8041, 873 874 BYTE_COUNT = 8050, 875 ELEM_COUNT = 8051, 876 ELEM_LAST = 8052, 877 USED_LAST = 8053, 878 USED_COUNT = 8054, 879 }; 880 881 extern struct sqlite3 *smatch_db; 882 extern struct sqlite3 *mem_db; 883 extern struct sqlite3 *cache_db; 884 885 void db_ignore_states(int id); 886 void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type); 887 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm)); 888 void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr)); 889 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)); 890 void select_call_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value)); 891 void select_return_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value)); 892 struct range_list *db_return_vals(struct expression *expr); 893 struct range_list *db_return_vals_from_str(const char *fn_name); 894 struct range_list *db_return_vals_no_args(struct expression *expr); 895 char *return_state_to_var_sym(struct expression *expr, int param, const char *key, struct symbol **sym); 896 char *get_chunk_from_key(struct expression *arg, char *key, struct symbol **sym, struct var_sym_list **vsl); 897 char *get_variable_from_key(struct expression *arg, const char *key, struct symbol **sym); 898 const char *state_name_to_param_name(const char *state_name, const char *param_name); 899 const char *get_param_name_var_sym(const char *name, struct symbol *sym); 900 const char *get_param_name(struct sm_state *sm); 901 const char *get_mtag_name_var_sym(const char *state_name, struct symbol *sym); 902 const char *get_mtag_name_expr(struct expression *expr); 903 char *get_data_info_name(struct expression *expr); 904 char *sm_to_arg_name(struct expression *expr, struct sm_state *sm); 905 int is_recursive_member(const char *param_name); 906 907 char *escape_newlines(const char *str); 908 void sql_exec(struct sqlite3 *db, int (*callback)(void*, int, char**, char**), void *data, const char *sql); 909 910 #define sql_helper(db, call_back, data, sql...) \ 911 do { \ 912 char sql_txt[1024]; \ 913 \ 914 sqlite3_snprintf(sizeof(sql_txt), sql_txt, sql); \ 915 db_debug("debug: %s\n", sql_txt); \ 916 sql_exec(db, call_back, data, sql_txt); \ 917 } while (0) 918 919 920 #define run_sql(call_back, data, sql...) \ 921 do { \ 922 if (option_no_db) \ 923 break; \ 924 sql_helper(smatch_db, call_back, data, sql); \ 925 } while (0) 926 927 #define mem_sql(call_back, data, sql...) \ 928 sql_helper(mem_db, call_back, data, sql) 929 930 #define cache_sql(call_back, data, sql...) \ 931 sql_helper(cache_db, call_back, data, sql) 932 933 #define sql_insert_helper(table, db, ignore, late, values...) \ 934 do { \ 935 struct sqlite3 *_db = db; \ 936 \ 937 if (__inline_fn && !_db) \ 938 _db = mem_db; \ 939 if (_db) { \ 940 char buf[1024]; \ 941 char *err, *p = buf; \ 942 int rc; \ 943 \ 944 p += snprintf(p, buf + sizeof(buf) - p, \ 945 "insert %sinto %s values (", \ 946 ignore ? "or ignore " : "", #table); \ 947 p += snprintf(p, buf + sizeof(buf) - p, values); \ 948 p += snprintf(p, buf + sizeof(buf) - p, ");"); \ 949 db_debug("mem-db: %s\n", buf); \ 950 rc = sqlite3_exec(_db, buf, NULL, NULL, &err); \ 951 if (rc != SQLITE_OK) { \ 952 sm_ierror("SQL error #2: %s", err); \ 953 sm_ierror("SQL: '%s'", buf); \ 954 parse_error = 1; \ 955 } \ 956 break; \ 957 } \ 958 if (option_info) { \ 959 FILE *tmp_fd = sm_outfd; \ 960 sm_outfd = sql_outfd; \ 961 sm_prefix(); \ 962 sm_printf("SQL%s: insert %sinto " #table " values(", \ 963 late ? "_late" : "", ignore ? "or ignore " : ""); \ 964 sm_printf(values); \ 965 sm_printf(");\n"); \ 966 sm_outfd = tmp_fd; \ 967 } \ 968 } while (0) 969 970 #define sql_insert(table, values...) sql_insert_helper(table, 0, 0, 0, values); 971 #define sql_insert_or_ignore(table, values...) sql_insert_helper(table, 0, 1, 0, values); 972 #define sql_insert_late(table, values...) sql_insert_helper(table, 0, 0, 1, values); 973 #define sql_insert_cache(table, values...) sql_insert_helper(table, cache_db, 1, 0, values); 974 975 char *get_static_filter(struct symbol *sym); 976 977 void sql_insert_return_states(int return_id, const char *return_ranges, 978 int type, int param, const char *key, const char *value); 979 void sql_insert_caller_info(struct expression *call, int type, int param, 980 const char *key, const char *value); 981 void sql_insert_function_ptr(const char *fn, const char *struct_name); 982 void sql_insert_return_values(const char *return_values); 983 void sql_insert_return_implies(int type, int param, const char *key, const char *value); 984 void sql_insert_function_type_size(const char *member, const char *ranges); 985 void sql_insert_function_type_info(int type, const char *struct_type, const char *member, const char *value); 986 void sql_insert_type_info(int type, const char *member, const char *value); 987 void sql_insert_local_values(const char *name, const char *value); 988 void sql_insert_function_type_value(const char *type, const char *value); 989 void sql_insert_function_type(int param, const char *value); 990 void sql_insert_parameter_name(int param, const char *value); 991 void sql_insert_data_info(struct expression *data, int type, const char *value); 992 void sql_insert_data_info_var_sym(const char *var, struct symbol *sym, int type, const char *value); 993 void sql_save_constraint(const char *con); 994 void sql_save_constraint_required(const char *data, int op, const char *limit); 995 void sql_copy_constraint_required(const char *new_limit, const char *old_limit); 996 void sql_insert_fn_ptr_data_link(const char *ptr, const char *data); 997 void sql_insert_fn_data_link(struct expression *fn, int type, int param, const char *key, const char *value); 998 void sql_insert_mtag_about(mtag_t tag, const char *left_name, const char *right_name); 999 void sql_insert_mtag_map(mtag_t tag, int offset, mtag_t container); 1000 void sql_insert_mtag_alias(mtag_t orig, mtag_t alias); 1001 int mtag_map_select_container(mtag_t tag, int offset, mtag_t *container); 1002 int mtag_map_select_tag(mtag_t container, int offset, mtag_t *tag); 1003 struct smatch_state *swap_mtag_return(struct expression *expr, struct smatch_state *state); 1004 struct range_list *swap_mtag_seed(struct expression *expr, struct range_list *rl); 1005 1006 void sql_select_return_states(const char *cols, struct expression *call, 1007 int (*callback)(void*, int, char**, char**), void *info); 1008 void sql_select_call_implies(const char *cols, struct expression *call, 1009 int (*callback)(void*, int, char**, char**)); 1010 1011 void open_smatch_db(char *db_file); 1012 1013 /* smatch_files.c */ 1014 int open_data_file(const char *filename); 1015 int open_schema_file(const char *schema); 1016 struct token *get_tokens_file(const char *filename); 1017 1018 /* smatch.c */ 1019 extern char *option_debug_check; 1020 extern char *option_project_str; 1021 extern char *bin_dir; 1022 extern char *data_dir; 1023 extern int option_no_data; 1024 extern int option_full_path; 1025 extern int option_call_tree; 1026 extern int num_checks; 1027 1028 enum project_type { 1029 PROJ_NONE, 1030 PROJ_KERNEL, 1031 PROJ_WINE, 1032 PROJ_ILLUMOS_KERNEL, 1033 PROJ_ILLUMOS_USER, 1034 PROJ_UNKNOWN, 1035 }; 1036 extern enum project_type option_project; 1037 const char *check_name(unsigned short id); 1038 int id_from_name(const char *name); 1039 1040 1041 /* smatch_buf_size.c */ 1042 int get_array_size(struct expression *expr); 1043 int get_array_size_bytes(struct expression *expr); 1044 int get_array_size_bytes_min(struct expression *expr); 1045 int get_array_size_bytes_max(struct expression *expr); 1046 struct range_list *get_array_size_bytes_rl(struct expression *expr); 1047 int get_real_array_size(struct expression *expr); 1048 int last_member_is_resizable(struct symbol *type); 1049 /* smatch_strlen.c */ 1050 int get_implied_strlen(struct expression *expr, struct range_list **rl); 1051 int get_size_from_strlen(struct expression *expr); 1052 1053 /* smatch_capped.c */ 1054 int is_capped(struct expression *expr); 1055 int is_capped_var_sym(const char *name, struct symbol *sym); 1056 1057 /* check_user_data.c */ 1058 int is_user_macro(struct expression *expr); 1059 int is_capped_user_data(struct expression *expr); 1060 int implied_user_data(struct expression *expr, struct range_list **rl); 1061 struct stree *get_user_stree(void); 1062 int get_user_rl(struct expression *expr, struct range_list **rl); 1063 int is_user_rl(struct expression *expr); 1064 int get_user_rl_var_sym(const char *name, struct symbol *sym, struct range_list **rl); 1065 bool user_rl_capped(struct expression *expr); 1066 struct range_list *var_user_rl(struct expression *expr); 1067 1068 /* check_locking.c */ 1069 void print_held_locks(); 1070 1071 /* check_assigned_expr.c */ 1072 struct expression *get_assigned_expr(struct expression *expr); 1073 struct expression *get_assigned_expr_name_sym(const char *name, struct symbol *sym); 1074 /* smatch_return_to_param.c */ 1075 void __add_return_to_param_mapping(struct expression *assign, const char *return_string); 1076 char *map_call_to_param_name_sym(struct expression *expr, struct symbol **sym); 1077 1078 /* smatch_comparison.c */ 1079 extern int comparison_id; 1080 #define UNKNOWN_COMPARISON 0 1081 #define IMPOSSIBLE_COMPARISON -1 1082 struct compare_data { 1083 /* The ->left and ->right expression pointers might be NULL (I'm lazy) */ 1084 struct expression *left; 1085 const char *left_var; 1086 struct var_sym_list *left_vsl; 1087 int comparison; 1088 struct expression *right; 1089 const char *right_var; 1090 struct var_sym_list *right_vsl; 1091 }; 1092 DECLARE_ALLOCATOR(compare_data); 1093 struct smatch_state *alloc_compare_state( 1094 struct expression *left, 1095 const char *left_var, struct var_sym_list *left_vsl, 1096 int comparison, 1097 struct expression *right, 1098 const char *right_var, struct var_sym_list *right_vsl); 1099 int comparison_intersection(int orig, int op); 1100 int merge_comparisons(int one, int two); 1101 int combine_comparisons(int left_compare, int right_compare); 1102 int state_to_comparison(struct smatch_state *state); 1103 struct smatch_state *merge_compare_states(struct smatch_state *s1, struct smatch_state *s2); 1104 int get_comparison(struct expression *left, struct expression *right); 1105 int get_comparison_no_extra(struct expression *a, struct expression *b); 1106 int get_comparison_strings(const char *one, const char *two); 1107 int possible_comparison(struct expression *a, int comparison, struct expression *b); 1108 struct state_list *get_all_comparisons(struct expression *expr); 1109 struct state_list *get_all_possible_equal_comparisons(struct expression *expr); 1110 void __add_return_comparison(struct expression *call, const char *range); 1111 void __add_comparison_info(struct expression *expr, struct expression *call, const char *range); 1112 char *get_printed_param_name(struct expression *call, const char *param_name, struct symbol *param_sym); 1113 char *name_sym_to_param_comparison(const char *name, struct symbol *sym); 1114 char *expr_equal_to_param(struct expression *expr, int ignore); 1115 char *expr_lte_to_param(struct expression *expr, int ignore); 1116 char *expr_param_comparison(struct expression *expr, int ignore); 1117 int flip_comparison(int op); 1118 int negate_comparison(int op); 1119 int remove_unsigned_from_comparison(int op); 1120 int param_compare_limit_is_impossible(struct expression *expr, int left_param, char *left_key, char *value); 1121 void filter_by_comparison(struct range_list **rl, int comparison, struct range_list *right); 1122 struct sm_state *comparison_implication_hook(struct expression *expr, 1123 struct state_list **true_stack, 1124 struct state_list **false_stack); 1125 void __compare_param_limit_hook(struct expression *left_expr, struct expression *right_expr, 1126 const char *state_name, 1127 struct smatch_state *true_state, struct smatch_state *false_state); 1128 int impossibly_high_comparison(struct expression *expr); 1129 1130 /* smatch_sval.c */ 1131 sval_t *sval_alloc(sval_t sval); 1132 sval_t *sval_alloc_permanent(sval_t sval); 1133 sval_t sval_blank(struct expression *expr); 1134 sval_t sval_type_val(struct symbol *type, long long val); 1135 sval_t sval_type_fval(struct symbol *type, long double fval); 1136 sval_t sval_from_val(struct expression *expr, long long val); 1137 sval_t sval_from_fval(struct expression *expr, long double fval); 1138 int sval_is_ptr(sval_t sval); 1139 bool sval_is_fp(sval_t sval); 1140 int sval_unsigned(sval_t sval); 1141 int sval_signed(sval_t sval); 1142 int sval_bits(sval_t sval); 1143 int sval_bits_used(sval_t sval); 1144 int sval_is_negative(sval_t sval); 1145 int sval_is_positive(sval_t sval); 1146 int sval_is_min(sval_t sval); 1147 int sval_is_max(sval_t sval); 1148 int sval_is_a_min(sval_t sval); 1149 int sval_is_a_max(sval_t sval); 1150 int sval_is_negative_min(sval_t sval); 1151 int sval_cmp_t(struct symbol *type, sval_t one, sval_t two); 1152 int sval_cmp_val(sval_t one, long long val); 1153 sval_t sval_min(sval_t one, sval_t two); 1154 sval_t sval_max(sval_t one, sval_t two); 1155 int sval_too_low(struct symbol *type, sval_t sval); 1156 int sval_too_high(struct symbol *type, sval_t sval); 1157 int sval_fits(struct symbol *type, sval_t sval); 1158 sval_t sval_cast(struct symbol *type, sval_t sval); 1159 sval_t sval_preop(sval_t sval, int op); 1160 sval_t sval_binop(sval_t left, int op, sval_t right); 1161 int sval_binop_overflows(sval_t left, int op, sval_t right); 1162 int sval_binop_overflows_no_sign(sval_t left, int op, sval_t right); 1163 int find_first_zero_bit(unsigned long long uvalue); 1164 int sm_fls64(unsigned long long uvalue); 1165 unsigned long long fls_mask(unsigned long long uvalue); 1166 unsigned long long sval_fls_mask(sval_t sval); 1167 const char *sval_to_str(sval_t sval); 1168 const char *sval_to_str_or_err_ptr(sval_t sval); 1169 const char *sval_to_numstr(sval_t sval); 1170 sval_t ll_to_sval(long long val); 1171 1172 /* smatch_string_list.c */ 1173 int list_has_string(struct string_list *str_list, const char *str); 1174 int insert_string(struct string_list **str_list, const char *str); 1175 struct string_list *clone_str_list(struct string_list *orig); 1176 struct string_list *combine_string_lists(struct string_list *one, struct string_list *two); 1177 1178 /* smatch_start_states.c */ 1179 struct stree *get_start_states(void); 1180 1181 /* smatch_recurse.c */ 1182 int has_symbol(struct expression *expr, struct symbol *sym); 1183 int has_variable(struct expression *expr, struct expression *var); 1184 int has_inc_dec(struct expression *expr); 1185 1186 /* smatch_stored_conditions.c */ 1187 struct smatch_state *get_stored_condition(struct expression *expr); 1188 struct expression_list *get_conditions(struct expression *expr); 1189 struct sm_state *stored_condition_implication_hook(struct expression *expr, 1190 struct state_list **true_stack, 1191 struct state_list **false_stack); 1192 /* smatch_parsed_conditions.c */ 1193 struct sm_state *parsed_condition_implication_hook(struct expression *expr, 1194 struct state_list **true_stack, 1195 struct state_list **false_stack); 1196 1197 /* check_string_len.c */ 1198 int get_formatted_string_size(struct expression *call, int arg); 1199 int get_formatted_string_min_size(struct expression *call, int arg); 1200 1201 /* smatch_param_set.c */ 1202 int param_was_set(struct expression *expr); 1203 int param_was_set_var_sym(const char *name, struct symbol *sym); 1204 void print_limited_param_set(int return_id, char *return_ranges, struct expression *expr); 1205 /* smatch_param_filter.c */ 1206 int param_has_filter_data(struct sm_state *sm); 1207 1208 /* smatch_links.c */ 1209 void set_up_link_functions(int id, int linkid); 1210 struct smatch_state *merge_link_states(struct smatch_state *s1, struct smatch_state *s2); 1211 void store_link(int link_id, const char *name, struct symbol *sym, const char *link_name, struct symbol *link_sym); 1212 1213 /* check_buf_comparison */ 1214 const char *limit_type_str(unsigned int limit_type); 1215 struct expression *get_size_variable(struct expression *buf, int *limit_type); 1216 struct expression *get_array_variable(struct expression *size); 1217 int buf_comparison_index_ok(struct expression *expr); 1218 1219 /* smatch_untracked_param.c */ 1220 void mark_untracked(struct expression *expr, int param, const char *key, const char *value); 1221 void add_untracked_param_hook(void (func)(struct expression *call, int param)); 1222 void add_lost_param_hook(void (func)(struct expression *call, int param)); 1223 void mark_all_params_untracked(int return_id, char *return_ranges, struct expression *expr); 1224 1225 /* smatch_strings.c */ 1226 struct state_list *get_strings(struct expression *expr); 1227 struct expression *fake_string_from_mtag(mtag_t tag); 1228 1229 /* smatch_estate.c */ 1230 int estate_get_single_value(struct smatch_state *state, sval_t *sval); 1231 1232 /* smatch_address.c */ 1233 int get_address_rl(struct expression *expr, struct range_list **rl); 1234 int get_member_offset(struct symbol *type, const char *member_name); 1235 int get_member_offset_from_deref(struct expression *expr); 1236 1237 /* for now this is in smatch_used_parameter.c */ 1238 void __get_state_hook(int owner, const char *name, struct symbol *sym); 1239 1240 /* smatch_buf_comparison.c */ 1241 int db_var_is_array_limit(struct expression *array, const char *name, struct var_sym_list *vsl); 1242 1243 struct stree *get_all_return_states(void); 1244 struct stree_stack *get_all_return_strees(void); 1245 int on_atomic_dec_path(void); 1246 int was_inced(const char *name, struct symbol *sym); 1247 1248 /* smatch_constraints.c */ 1249 char *get_constraint_str(struct expression *expr); 1250 struct constraint_list *get_constraints(struct expression *expr); 1251 char *unmet_constraint(struct expression *data, struct expression *offset); 1252 char *get_required_constraint(const char *data_str); 1253 1254 /* smatch_container_of.c */ 1255 int get_param_from_container_of(struct expression *expr); 1256 int get_offset_from_container_of(struct expression *expr); 1257 char *get_container_name(struct expression *container, struct expression *expr); 1258 1259 /* smatch_mtag.c */ 1260 mtag_t str_to_mtag(const char *str); 1261 int get_string_mtag(struct expression *expr, mtag_t *tag); 1262 int get_toplevel_mtag(struct symbol *sym, mtag_t *tag); 1263 int create_mtag_alias(mtag_t tag, struct expression *expr, mtag_t *new); 1264 int expr_to_mtag_offset(struct expression *expr, mtag_t *tag, int *offset); 1265 void update_mtag_data(struct expression *expr, struct smatch_state *state); 1266 int get_mtag_sval(struct expression *expr, sval_t *sval); 1267 1268 /* Trinity fuzzer stuff */ 1269 const char *get_syscall_arg_type(struct symbol *sym); 1270 1271 /* smatch_bit_info.c */ 1272 struct bit_info *rl_to_binfo(struct range_list *rl); 1273 struct bit_info *get_bit_info(struct expression *expr); 1274 struct bit_info *get_bit_info_var_sym(const char *name, struct symbol *sym); 1275 /* smatch_mem_tracker.c */ 1276 extern int option_mem; 1277 unsigned long get_mem_kb(void); 1278 unsigned long get_max_memory(void); 1279 1280 /* check_is_nospec.c */ 1281 bool is_nospec(struct expression *expr); 1282 long get_stmt_cnt(void); 1283 1284 /* smatch_nul_terminator.c */ 1285 bool is_nul_terminated_var_sym(const char *name, struct symbol *sym); 1286 bool is_nul_terminated(struct expression *expr); 1287 /* check_kernel.c */ 1288 bool is_ignored_kernel_data(const char *name); 1289 1290 bool is_fresh_alloc_var_sym(const char *var, struct symbol *sym); 1291 bool is_fresh_alloc(struct expression *expr); 1292 static inline bool type_is_ptr(struct symbol *type) 1293 { 1294 return type && 1295 (type->type == SYM_PTR || 1296 type->type == SYM_ARRAY || 1297 type->type == SYM_FN); 1298 } 1299 1300 static inline bool type_is_fp(struct symbol *type) 1301 { 1302 return type && 1303 (type == &float_ctype || 1304 type == &double_ctype || 1305 type == &ldouble_ctype); 1306 } 1307 1308 static inline int type_bits(struct symbol *type) 1309 { 1310 if (!type) 1311 return 0; 1312 if (type_is_ptr(type)) 1313 return bits_in_pointer; 1314 if (!type->examined) 1315 examine_symbol_type(type); 1316 return type->bit_size; 1317 } 1318 1319 static inline int type_unsigned(struct symbol *base_type) 1320 { 1321 if (!base_type) 1322 return 0; 1323 if (is_ptr_type(base_type)) 1324 return 1; 1325 if (base_type->ctype.modifiers & MOD_UNSIGNED) 1326 return 1; 1327 return 0; 1328 } 1329 1330 static inline int type_positive_bits(struct symbol *type) 1331 { 1332 if (!type) 1333 return 0; 1334 if (is_ptr_type(type)) 1335 return bits_in_pointer; 1336 if (type_unsigned(type)) 1337 return type_bits(type); 1338 return type_bits(type) - 1; 1339 } 1340 1341 static inline int sval_positive_bits(sval_t sval) 1342 { 1343 return type_positive_bits(sval.type); 1344 } 1345 1346 /* 1347 * Returns -1 if one is smaller, 0 if they are the same and 1 if two is larger. 1348 */ 1349 1350 static inline int fp_cmp(sval_t one, sval_t two) 1351 { 1352 struct symbol *type; 1353 1354 if (sval_is_fp(one) && sval_is_fp(two)) 1355 type = type_bits(one.type) > type_bits(two.type) ? one.type : two.type; 1356 else if (sval_is_fp(one)) 1357 type = one.type; 1358 else 1359 type = two.type; 1360 1361 one = sval_cast(type, one); 1362 two = sval_cast(type, two); 1363 1364 if (one.type == &float_ctype) { 1365 if (one.fvalue < two.fvalue) 1366 return -1; 1367 if (one.fvalue == two.fvalue) 1368 return 0; 1369 return 1; 1370 } 1371 if (one.type == &double_ctype) { 1372 if (one.dvalue < two.dvalue) 1373 return -1; 1374 if (one.dvalue == two.dvalue) 1375 return 0; 1376 return 1; 1377 } 1378 if (one.type == &ldouble_ctype) { 1379 if (one.ldvalue < two.ldvalue) 1380 return -1; 1381 if (one.ldvalue == two.ldvalue) 1382 return 0; 1383 return 1; 1384 } 1385 sm_perror("bad type in fp_cmp(): %s", type_to_str(type)); 1386 return 1; 1387 } 1388 1389 static inline int sval_cmp(sval_t one, sval_t two) 1390 { 1391 struct symbol *type; 1392 1393 if (sval_is_fp(one) || sval_is_fp(two)) 1394 return fp_cmp(one, two); 1395 1396 type = one.type; 1397 if (sval_positive_bits(two) > sval_positive_bits(one)) 1398 type = two.type; 1399 if (type_bits(type) < 31) 1400 type = &int_ctype; 1401 1402 one = sval_cast(type, one); 1403 two = sval_cast(type, two); 1404 1405 if (type_unsigned(type)) { 1406 if (one.uvalue < two.uvalue) 1407 return -1; 1408 if (one.uvalue == two.uvalue) 1409 return 0; 1410 return 1; 1411 } 1412 /* fix me handle type promotion and unsigned values */ 1413 if (one.value < two.value) 1414 return -1; 1415 if (one.value == two.value) 1416 return 0; 1417 return 1; 1418 } 1419 1420 #endif /* !SMATCH_H_ */ 1421