2 * Copyright (C) 2006 Dan Carpenter.
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.
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.
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
31 #include "expression.h"
34 typedef long long mtag_t
;
40 #define STATE(_x) static struct smatch_state _x = { .name = #_x }
41 extern struct smatch_state undefined
;
42 extern struct smatch_state ghost
;
43 extern struct smatch_state merged
;
44 extern struct smatch_state true_state
;
45 extern struct smatch_state false_state
;
46 DECLARE_ALLOCATOR(smatch_state
);
48 static inline void *INT_PTR(int i
)
50 return (void *)(long)i
;
53 static inline int PTR_INT(void *p
)
63 DECLARE_ALLOCATOR(tracker
);
64 DECLARE_PTR_LIST(tracker_list
, struct tracker
);
65 DECLARE_PTR_LIST(stree_stack
, struct stree
);
67 /* The first 3 struct members must match struct tracker */
72 unsigned short merged
:1;
74 struct smatch_state
*state
;
76 struct sm_state
*left
;
77 struct sm_state
*right
;
78 struct state_list
*possible
;
85 DECLARE_ALLOCATOR(var_sym
);
86 DECLARE_PTR_LIST(var_sym_list
, struct var_sym
);
92 DECLARE_PTR_LIST(constraint_list
, struct constraint
);
98 extern struct alloc_info
*alloc_funcs
;
101 unsigned long long set
;
102 unsigned long long possible
;
114 ASSIGNMENT_HOOK_AFTER
,
116 GLOBAL_ASSIGNMENT_HOOK
,
121 WHOLE_CONDITION_HOOK
,
122 FUNCTION_CALL_HOOK_BEFORE
,
124 CALL_HOOK_AFTER_INLINE
,
125 FUNCTION_CALL_HOOK_AFTER_DB
,
126 CALL_ASSIGNMENT_HOOK
,
127 MACRO_ASSIGNMENT_HOOK
,
152 void add_hook(void *func
, enum hook_type type
);
153 typedef struct smatch_state
*(merge_func_t
)(struct smatch_state
*s1
, struct smatch_state
*s2
);
154 typedef struct smatch_state
*(unmatched_func_t
)(struct sm_state
*state
);
155 void add_merge_hook(int client_id
, merge_func_t
*func
);
156 void add_unmatched_state_hook(int client_id
, unmatched_func_t
*func
);
157 void add_pre_merge_hook(int client_id
, void (*hook
)(struct sm_state
*cur
, struct sm_state
*other
));
158 typedef void (scope_hook
)(void *data
);
159 void add_scope_hook(scope_hook
*hook
, void *data
);
160 typedef void (func_hook
)(const char *fn
, struct expression
*expr
, void *data
);
161 typedef void (implication_hook
)(const char *fn
, struct expression
*call_expr
,
162 struct expression
*assign_expr
, void *data
);
163 typedef void (return_implies_hook
)(struct expression
*call_expr
,
164 int param
, char *key
, char *value
);
165 typedef int (implied_return_hook
)(struct expression
*call_expr
, void *info
, struct range_list
**rl
);
166 void add_function_hook(const char *look_for
, func_hook
*call_back
, void *data
);
168 void add_function_assign_hook(const char *look_for
, func_hook
*call_back
,
170 void register_func_hooks_from_file(const char *file
,
171 func_hook
*call_back
, void *info
);
172 void register_assign_hooks_from_file(const char *file
,
173 func_hook
*call_back
, void *info
);
174 void add_implied_return_hook(const char *look_for
,
175 implied_return_hook
*call_back
,
177 void add_macro_assign_hook(const char *look_for
, func_hook
*call_back
,
179 void add_macro_assign_hook_extra(const char *look_for
, func_hook
*call_back
,
181 void return_implies_state(const char *look_for
, long long start
, long long end
,
182 implication_hook
*call_back
, void *info
);
183 void return_implies_state_sval(const char *look_for
, sval_t start
, sval_t end
,
184 implication_hook
*call_back
, void *info
);
185 void return_implies_exact(const char *look_for
, sval_t start
, sval_t end
,
186 implication_hook
*call_back
, void *info
);
187 struct range_list
*get_range_implications(const char *fn
);
188 void select_return_states_hook(int type
, return_implies_hook
*callback
);
189 void select_return_states_before(void (*fn
)(void));
190 void select_return_states_after(void (*fn
)(void));
191 int get_implied_return(struct expression
*expr
, struct range_list
**rl
);
192 void allocate_hook_memory(void);
193 void allocate_tracker_array(int num_checks
);
195 struct modification_data
{
196 struct smatch_state
*prev
;
197 struct expression
*cur
;
200 typedef void (modification_hook
)(struct sm_state
*sm
, struct expression
*mod_expr
);
201 bool is_sub_member(const char *name
, struct symbol
*sym
, struct sm_state
*sm
);
202 void add_modification_hook(int owner
, modification_hook
*call_back
);
203 void add_modification_hook_late(int owner
, modification_hook
*call_back
);
204 struct smatch_state
*get_modification_state(struct expression
*expr
);
206 int outside_of_function(void);
207 const char *get_filename(void);
208 const char *get_base_file(void);
209 char *get_function(void);
210 int get_lineno(void);
211 extern int final_pass
;
212 extern struct symbol
*cur_func_sym
;
213 extern int option_debug
;
214 extern int local_debug
;
216 extern bool implied_debug
;
217 bool debug_implied(void);
218 bool debug_on(const char *check_name
, const char *var
);
219 extern int option_info
;
220 extern int option_spammy
;
221 extern int option_print_names
;
222 extern char *trace_variable
;
223 extern struct stree
*global_states
;
224 void set_function_skipped(void);
225 int is_skipped_function(void);
226 int is_silenced_function(void);
227 extern bool implications_off
;
229 /* smatch_impossible.c */
230 int is_impossible_path(void);
231 void set_path_impossible(void);
233 extern FILE *sm_outfd
;
234 extern FILE *sql_outfd
;
235 extern FILE *caller_info_fd
;
236 extern int sm_nr_checks
;
237 extern int sm_nr_errors
;
240 * How to use these routines:
242 * sm_fatal(): an internal error of some kind that should immediately exit
243 * sm_ierror(): an internal error
244 * sm_perror(): an internal error from parsing input source
245 * sm_error(): an error from input source
246 * sm_warning(): a warning from input source
247 * sm_info(): info message (from option_info)
248 * sm_debug(): debug message
249 * sm_msg(): other message (please avoid using this)
252 #define sm_printf(msg...) do { \
253 if (final_pass || option_debug || local_debug || debug_db) \
254 fprintf(sm_outfd, msg); \
257 static inline void sm_prefix(void)
259 sm_printf("%s:%d %s() ", get_filename(), get_lineno(), get_function());
260 if (option_info
|| !option_print_names
)
262 sm_printf("[smatch.%s] ", __CHECKNAME__
);
265 static inline void print_implied_debug_msg();
267 extern bool __silence_warnings_for_stmt
;
269 #define sm_print_msg(type, msg...) \
271 print_implied_debug_msg(); \
272 if (!final_pass && !option_debug && !local_debug && !debug_db) \
274 if (__silence_warnings_for_stmt && !option_debug && !local_debug) \
276 if (!option_info && is_silenced_function()) \
280 sm_printf("warn: "); \
282 } else if (type == 2) { \
283 sm_printf("error: "); \
285 } else if (type == 3) { \
286 sm_printf("parse error: "); \
293 #define sm_msg(msg...) do { sm_print_msg(0, msg); } while (0)
295 extern char *implied_debug_msg
;
296 static inline void print_implied_debug_msg(void)
298 static struct symbol
*last_printed
= NULL
;
300 if (!implied_debug_msg
)
302 if (last_printed
== cur_func_sym
)
304 last_printed
= cur_func_sym
;
305 sm_msg("%s", implied_debug_msg
);
308 #define sm_debug(msg...) do { if (option_debug) sm_printf(msg); } while (0)
309 #define db_debug(msg...) do { if (option_debug || debug_db) sm_printf(msg); } while (0)
311 #define sm_info(msg...) do { \
312 if (option_debug || (option_info && final_pass)) { \
314 sm_printf("info: "); \
320 #define sm_warning(msg...) do { sm_print_msg(1, msg); } while (0)
321 #define sm_error(msg...) do { sm_print_msg(2, msg); } while (0)
322 #define sm_perror(msg...) do { sm_print_msg(3, msg); } while (0)
324 static inline void sm_fatal(const char *fmt
, ...)
329 vfprintf(sm_outfd
, fmt
, args
);
332 fprintf(sm_outfd
, "\n");
337 static inline void sm_ierror(const char *fmt
, ...)
343 fprintf(sm_outfd
, "internal error: ");
346 vfprintf(sm_outfd
, fmt
, args
);
349 fprintf(sm_outfd
, "\n");
351 #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
353 bool has_states(int owner
);
354 struct smatch_state
*__get_state(int owner
, const char *name
, struct symbol
*sym
);
355 struct smatch_state
*get_state(int owner
, const char *name
, struct symbol
*sym
);
356 struct smatch_state
*get_state_expr(int owner
, struct expression
*expr
);
357 struct state_list
*get_possible_states(int owner
, const char *name
,
359 struct state_list
*get_possible_states_expr(int owner
, struct expression
*expr
);
360 struct sm_state
*set_state(int owner
, const char *name
, struct symbol
*sym
,
361 struct smatch_state
*state
);
362 struct sm_state
*set_state_expr(int owner
, struct expression
*expr
,
363 struct smatch_state
*state
);
364 void delete_state(int owner
, const char *name
, struct symbol
*sym
);
365 void delete_state_expr(int owner
, struct expression
*expr
);
366 void __delete_all_states_sym(struct symbol
*sym
);
367 void set_true_false_states(int owner
, const char *name
, struct symbol
*sym
,
368 struct smatch_state
*true_state
,
369 struct smatch_state
*false_state
);
370 void set_true_false_states_expr(int owner
, struct expression
*expr
,
371 struct smatch_state
*true_state
,
372 struct smatch_state
*false_state
);
374 struct stree
*get_all_states_from_stree(int owner
, struct stree
*source
);
375 struct stree
*get_all_states_stree(int id
);
376 struct stree
*__get_cur_stree(void);
377 int is_reachable(void);
378 void add_get_state_hook(void (*fn
)(int owner
, const char *name
, struct symbol
*sym
));
380 /* smatch_helper.c */
381 DECLARE_PTR_LIST(int_stack
, int);
382 char *alloc_string(const char *str
);
383 char *alloc_string_newline(const char *str
);
384 void free_string(char *str
);
385 void append(char *dest
, const char *data
, int buff_len
);
386 void remove_parens(char *str
);
387 struct smatch_state
*alloc_state_num(int num
);
388 struct smatch_state
*alloc_state_str(const char *name
);
389 struct smatch_state
*merge_str_state(struct smatch_state
*s1
, struct smatch_state
*s2
);
390 struct smatch_state
*alloc_state_expr(struct expression
*expr
);
391 struct expression
*get_argument_from_call_expr(struct expression_list
*args
,
393 struct expression
*get_array_expr(struct expression
*expr
);
395 char *expr_to_var(struct expression
*expr
);
396 struct symbol
*expr_to_sym(struct expression
*expr
);
397 char *expr_to_str(struct expression
*expr
);
398 char *expr_to_str_sym(struct expression
*expr
,
399 struct symbol
**sym_ptr
);
400 char *expr_to_var_sym(struct expression
*expr
,
401 struct symbol
**sym_ptr
);
402 char *expr_to_known_chunk_sym(struct expression
*expr
, struct symbol
**sym
);
403 char *expr_to_chunk_sym_vsl(struct expression
*expr
, struct symbol
**sym
, struct var_sym_list
**vsl
);
404 int get_complication_score(struct expression
*expr
);
406 int sym_name_is(const char *name
, struct expression
*expr
);
407 int get_const_value(struct expression
*expr
, sval_t
*sval
);
408 int get_value(struct expression
*expr
, sval_t
*val
);
409 int get_implied_value(struct expression
*expr
, sval_t
*val
);
410 int get_implied_value_fast(struct expression
*expr
, sval_t
*sval
);
411 int get_implied_min(struct expression
*expr
, sval_t
*sval
);
412 int get_implied_max(struct expression
*expr
, sval_t
*val
);
413 int get_hard_max(struct expression
*expr
, sval_t
*sval
);
414 int get_fuzzy_min(struct expression
*expr
, sval_t
*min
);
415 int get_fuzzy_max(struct expression
*expr
, sval_t
*max
);
416 int get_absolute_min(struct expression
*expr
, sval_t
*sval
);
417 int get_absolute_max(struct expression
*expr
, sval_t
*sval
);
418 int parse_call_math(struct expression
*expr
, char *math
, sval_t
*val
);
419 int parse_call_math_rl(struct expression
*call
, const char *math
, struct range_list
**rl
);
420 const char *get_allocation_math(struct expression
*expr
);
421 char *get_value_in_terms_of_parameter_math(struct expression
*expr
);
422 char *get_value_in_terms_of_parameter_math_var_sym(const char *var
, struct symbol
*sym
);
423 int expr_is_zero(struct expression
*expr
);
424 int known_condition_true(struct expression
*expr
);
425 int known_condition_false(struct expression
*expr
);
426 int implied_condition_true(struct expression
*expr
);
427 int implied_condition_false(struct expression
*expr
);
428 int can_integer_overflow(struct symbol
*type
, struct expression
*expr
);
429 void clear_math_cache(void);
430 void set_fast_math_only(void);
431 void clear_fast_math_only(void);
433 int is_array(struct expression
*expr
);
434 struct expression
*get_array_base(struct expression
*expr
);
435 struct expression
*get_array_offset(struct expression
*expr
);
436 const char *show_state(struct smatch_state
*state
);
437 struct statement
*get_expression_statement(struct expression
*expr
);
438 struct expression
*strip_parens(struct expression
*expr
);
439 struct expression
*strip_expr(struct expression
*expr
);
440 struct expression
*strip_expr_set_parent(struct expression
*expr
);
441 void scoped_state(int my_id
, const char *name
, struct symbol
*sym
);
442 int is_error_return(struct expression
*expr
);
443 int getting_address(struct expression
*expr
);
444 int get_struct_and_member(struct expression
*expr
, const char **type
, const char **member
);
445 char *get_member_name(struct expression
*expr
);
446 char *get_fnptr_name(struct expression
*expr
);
447 int cmp_pos(struct position pos1
, struct position pos2
);
448 int positions_eq(struct position pos1
, struct position pos2
);
449 struct statement
*get_current_statement(void);
450 struct statement
*get_prev_statement(void);
451 struct expression
*get_last_expr_from_expression_stmt(struct expression
*expr
);
453 #define RETURN_VAR -1
454 #define LOCAL_SCOPE -2
455 #define FILE_SCOPE -3
456 #define GLOBAL_SCOPE -4
457 #define UNKNOWN_SCOPE -5
459 char *get_param_var_sym_var_sym(const char *name
, struct symbol
*sym
, struct expression
*ret_expr
, struct symbol
**sym_p
);
460 char *get_param_name_sym(struct expression
*expr
, struct symbol
**sym_p
);
461 int get_param_key_from_var_sym(const char *name
, struct symbol
*sym
,
462 struct expression
*ret_expr
,
464 int get_param_key_from_sm(struct sm_state
*sm
, struct expression
*ret_expr
,
466 int map_to_param(const char *name
, struct symbol
*sym
);
467 int get_param_num_from_sym(struct symbol
*sym
);
468 int get_param_num(struct expression
*expr
);
469 struct symbol
*get_param_sym_from_num(int num
);
471 int ms_since(struct timeval
*start
);
472 int parent_is_gone_var_sym(const char *name
, struct symbol
*sym
);
473 int parent_is_gone(struct expression
*expr
);
474 bool is_noderef_ptr(struct expression
*expr
);
475 int invert_op(int op
);
476 int op_remove_assign(int op
);
477 int expr_equiv(struct expression
*one
, struct expression
*two
);
478 void push_int(struct int_stack
**stack
, int num
);
479 int pop_int(struct int_stack
**stack
);
482 struct symbol
*get_real_base_type(struct symbol
*sym
);
483 int type_bytes(struct symbol
*type
);
484 int array_bytes(struct symbol
*type
);
485 struct symbol
*get_pointer_type(struct expression
*expr
);
486 struct symbol
*get_type(struct expression
*expr
);
487 struct symbol
*get_final_type(struct expression
*expr
);
488 struct symbol
*get_promoted_type(struct symbol
*left
, struct symbol
*right
);
489 int type_signed(struct symbol
*base_type
);
490 int expr_unsigned(struct expression
*expr
);
491 int expr_signed(struct expression
*expr
);
492 int returns_unsigned(struct symbol
*base_type
);
493 int is_pointer(struct expression
*expr
);
494 int returns_pointer(struct symbol
*base_type
);
495 sval_t
sval_type_max(struct symbol
*base_type
);
496 sval_t
sval_type_min(struct symbol
*base_type
);
497 int nr_bits(struct expression
*expr
);
498 int is_void_pointer(struct expression
*expr
);
499 int is_char_pointer(struct expression
*expr
);
500 int is_string(struct expression
*expr
);
501 bool is_struct_ptr(struct symbol
*type
);
502 int is_static(struct expression
*expr
);
503 bool is_local_variable(struct expression
*expr
);
504 int types_equiv(struct symbol
*one
, struct symbol
*two
);
505 bool type_fits(struct symbol
*type
, struct symbol
*test
);
507 const char *global_static();
508 struct symbol
*cur_func_return_type(void);
509 struct symbol
*get_arg_type(struct expression
*fn
, int arg
);
510 struct symbol
*get_member_type_from_key(struct expression
*expr
, const char *key
);
511 struct symbol
*get_arg_type_from_key(struct expression
*fn
, int param
, struct expression
*arg
, const char *key
);
512 int is_struct(struct expression
*expr
);
513 char *type_to_str(struct symbol
*type
);
515 /* smatch_ignore.c */
516 void add_ignore(int owner
, const char *name
, struct symbol
*sym
);
517 int is_ignored(int owner
, const char *name
, struct symbol
*sym
);
518 void add_ignore_expr(int owner
, struct expression
*expr
);
519 int is_ignored_expr(int owner
, struct expression
*expr
);
522 struct smatch_state
*alloc_var_sym_state(const char *var
, struct symbol
*sym
);
523 struct var_sym
*alloc_var_sym(const char *var
, struct symbol
*sym
);
524 struct var_sym_list
*expr_to_vsl(struct expression
*expr
);
525 void add_var_sym(struct var_sym_list
**list
, const char *var
, struct symbol
*sym
);
526 void add_var_sym_expr(struct var_sym_list
**list
, struct expression
*expr
);
527 void del_var_sym(struct var_sym_list
**list
, const char *var
, struct symbol
*sym
);
528 int in_var_sym_list(struct var_sym_list
*list
, const char *var
, struct symbol
*sym
);
529 struct var_sym_list
*clone_var_sym_list(struct var_sym_list
*from_vsl
);
530 void merge_var_sym_list(struct var_sym_list
**dest
, struct var_sym_list
*src
);
531 struct var_sym_list
*combine_var_sym_lists(struct var_sym_list
*one
, struct var_sym_list
*two
);
532 int var_sym_lists_equiv(struct var_sym_list
*one
, struct var_sym_list
*two
);
533 void free_var_sym_list(struct var_sym_list
**list
);
534 void free_var_syms_and_list(struct var_sym_list
**list
);
537 struct tracker
*alloc_tracker(int owner
, const char *name
, struct symbol
*sym
);
538 void add_tracker(struct tracker_list
**list
, int owner
, const char *name
,
540 void add_tracker_expr(struct tracker_list
**list
, int owner
, struct expression
*expr
);
541 void del_tracker(struct tracker_list
**list
, int owner
, const char *name
,
543 int in_tracker_list(struct tracker_list
*list
, int owner
, const char *name
,
545 void free_tracker_list(struct tracker_list
**list
);
546 void free_trackers_and_list(struct tracker_list
**list
);
548 /* smatch_conditions */
549 int in_condition(void);
553 extern int __in_fake_assign
;
554 extern int __in_fake_parameter_assign
;
555 extern int __in_fake_struct_assign
;
556 extern int __in_fake_var_assign
;
557 extern int __fake_state_cnt
;
558 extern int in_fake_env
;
559 void smatch (struct string_list
*filelist
);
560 int inside_loop(void);
561 int definitely_inside_loop(void);
562 struct expression
*get_switch_expr(void);
563 int in_expression_statement(void);
564 void __process_post_op_stack(void);
565 void __split_expr(struct expression
*expr
);
566 void __split_label_stmt(struct statement
*stmt
);
567 void __split_stmt(struct statement
*stmt
);
568 extern int __in_function_def
;
569 extern int __in_unmatched_hook
;
570 extern int option_assume_loops
;
571 extern int option_two_passes
;
572 extern int option_no_db
;
573 extern int option_file_output
;
574 extern int option_time
;
575 extern int option_time_stmt
;
576 extern struct expression_list
*big_expression_stack
;
577 extern struct expression_list
*big_condition_stack
;
578 extern struct statement_list
*big_statement_stack
;
579 int is_assigned_call(struct expression
*expr
);
580 int is_fake_assigned_call(struct expression
*expr
);
581 void add_function_data(unsigned long *fn_data
);
582 int inlinable(struct expression
*expr
);
583 extern int __inline_call
;
584 extern struct expression
*__inline_fn
;
585 extern int __in_pre_condition
;
586 extern int __bail_on_rest_of_function
;
587 extern struct statement
*__prev_stmt
;
588 extern struct statement
*__cur_stmt
;
589 extern struct statement
*__next_stmt
;
590 void init_fake_env(void);
591 void end_fake_env(void);
592 int time_parsing_function(void);
593 bool taking_too_long(void);
594 int is_last_stmt(struct statement
*cur_stmt
);
596 /* smatch_struct_assignment.c */
597 struct expression
*get_faked_expression(void);
598 void __fake_struct_member_assignments(struct expression
*expr
);
600 /* smatch_project.c */
601 int is_no_inline_function(const char *function
);
603 /* smatch_conditions */
604 void __split_whole_condition(struct expression
*expr
);
605 void __handle_logic(struct expression
*expr
);
606 int is_condition(struct expression
*expr
);
607 int __handle_condition_assigns(struct expression
*expr
);
608 int __handle_select_assigns(struct expression
*expr
);
609 int __handle_expr_statement_assigns(struct expression
*expr
);
611 /* smatch_implied.c */
612 struct range_list_stack
;
613 void param_limit_implications(struct expression
*expr
, int param
, char *key
, char *value
, struct stree
**implied
);
614 struct stree
*__implied_case_stree(struct expression
*switch_expr
,
615 struct range_list
*case_rl
,
616 struct range_list_stack
**remaining_cases
,
617 struct stree
**raw_stree
);
618 void overwrite_states_using_pool(struct sm_state
*gate_sm
, struct sm_state
*pool_sm
);
619 int assume(struct expression
*expr
);
620 void end_assume(void);
621 int impossible_assumption(struct expression
*left
, int op
, sval_t sval
);
624 bool has_dynamic_states(unsigned short owner
);
625 void set_dynamic_states(unsigned short owner
);
627 /* smatch_extras.c */
628 int in_warn_on_macro(void);
629 #define SMATCH_EXTRA 5 /* this is my_id from smatch extra set in smatch.c */
630 extern int RETURN_ID
;
637 #define MTAG_ALIAS_BIT (1ULL << 63)
638 #define MTAG_OFFSET_MASK 0xfffULL
639 #define MTAG_SEED 0xdead << 12
641 const extern unsigned long valid_ptr_min
;
642 extern unsigned long valid_ptr_max
;
643 extern const sval_t valid_ptr_min_sval
;
644 extern sval_t valid_ptr_max_sval
;
645 extern struct range_list
*valid_ptr_rl
;
646 void alloc_valid_ptr_rl(void);
648 static const sval_t array_min_sval
= {
652 static const sval_t array_max_sval
= {
654 {.value
= ULONG_MAX
- 4095},
656 static const sval_t text_seg_min
= {
660 static const sval_t text_seg_max
= {
662 {.value
= ULONG_MAX
- 4095},
664 static const sval_t data_seg_min
= {
668 static const sval_t data_seg_max
= {
670 {.value
= ULONG_MAX
- 4095},
672 static const sval_t bss_seg_min
= {
676 static const sval_t bss_seg_max
= {
678 {.value
= ULONG_MAX
- 4095},
680 static const sval_t stack_seg_min
= {
684 static const sval_t stack_seg_max
= {
686 {.value
= ULONG_MAX
- 4095},
688 static const sval_t kmalloc_seg_min
= {
692 static const sval_t kmalloc_seg_max
= {
694 {.value
= ULONG_MAX
- 4095},
696 static const sval_t vmalloc_seg_min
= {
700 static const sval_t vmalloc_seg_max
= {
702 {.value
= ULONG_MAX
- 4095},
704 static const sval_t fn_ptr_min
= {
708 static const sval_t fn_ptr_max
= {
710 {.value
= ULONG_MAX
- 4095},
713 char *get_other_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
);
714 char *map_call_to_other_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
);
715 char *map_long_to_short_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
, bool use_stack
);
717 #define STRLEN_MAX_RET 1010101
719 /* smatch_absolute.c */
720 int get_absolute_min_helper(struct expression
*expr
, sval_t
*sval
);
721 int get_absolute_max_helper(struct expression
*expr
, sval_t
*sval
);
723 /* smatch_type_value.c */
724 void clear_type_value_cache(void);
725 int get_db_type_rl(struct expression
*expr
, struct range_list
**rl
);
727 /* smatch_data_val.c */
728 int get_mtag_rl(struct expression
*expr
, struct range_list
**rl
);
729 /* smatch_array_values.c */
730 int get_array_rl(struct expression
*expr
, struct range_list
**rl
);
732 /* smatch_states.c */
733 struct stree
*__swap_cur_stree(struct stree
*stree
);
734 void __push_fake_cur_stree();
735 struct stree
*__pop_fake_cur_stree();
736 void __free_fake_cur_stree();
737 void __set_fake_cur_stree_fast(struct stree
*stree
);
738 void __pop_fake_cur_stree_fast(void);
739 void __merge_stree_into_cur(struct stree
*stree
);
741 int unreachable(void);
742 void __set_cur_stree_readonly(void);
743 void __set_cur_stree_writable(void);
744 void __set_sm(struct sm_state
*sm
);
745 void __set_sm_cur_stree(struct sm_state
*sm
);
746 void __set_sm_fake_stree(struct sm_state
*sm
);
747 void __set_true_false_sm(struct sm_state
*true_state
,
748 struct sm_state
*false_state
);
749 void nullify_path(void);
750 void __match_nullify_path_hook(const char *fn
, struct expression
*expr
,
752 void __unnullify_path(void);
753 int __path_is_null(void);
754 void save_all_states(void);
755 void restore_all_states(void);
756 void free_goto_stack(void);
757 void clear_all_states(void);
759 struct sm_state
*get_sm_state(int owner
, const char *name
,
761 struct sm_state
*get_sm_state_expr(int owner
, struct expression
*expr
);
762 void __push_true_states(void);
763 void __use_false_states(void);
764 void __discard_false_states(void);
765 void __merge_false_states(void);
766 void __merge_true_states(void);
768 void __negate_cond_stacks(void);
769 void __use_pre_cond_states(void);
770 void __use_cond_true_states(void);
771 void __use_cond_false_states(void);
772 void __push_cond_stacks(void);
773 void __fold_in_set_states(void);
774 void __free_set_states(void);
775 struct stree
*__copy_cond_true_states(void);
776 struct stree
*__copy_cond_false_states(void);
777 struct stree
*__pop_cond_true_stack(void);
778 struct stree
*__pop_cond_false_stack(void);
779 void __and_cond_states(void);
780 void __or_cond_states(void);
781 void __save_pre_cond_states(void);
782 void __discard_pre_cond_states(void);
783 struct stree
*__get_true_states(void);
784 struct stree
*__get_false_states(void);
785 void __use_cond_states(void);
786 extern struct state_list
*__last_base_slist
;
788 void __push_continues(void);
789 void __discard_continues(void);
790 void __process_continues(void);
791 void __merge_continues(void);
793 void __push_breaks(void);
794 void __process_breaks(void);
795 int __has_breaks(void);
796 void __merge_breaks(void);
797 void __use_breaks(void);
799 void __save_switch_states(struct expression
*switch_expr
);
800 void __discard_switches(void);
801 int have_remaining_cases(void);
802 void __merge_switches(struct expression
*switch_expr
, struct range_list
*case_rl
);
803 void __push_default(void);
804 void __set_default(void);
805 int __pop_default(void);
807 void __push_conditions(void);
808 void __discard_conditions(void);
810 void __save_gotos(const char *name
, struct symbol
*sym
);
811 void __merge_gotos(const char *name
, struct symbol
*sym
);
813 void __discard_fake_states(struct expression
*call
);
815 void __print_cur_stree(void);
816 bool __print_states(const char *owner
);
817 typedef void (check_tracker_hook
)(int owner
, const char *name
, struct symbol
*sym
, struct smatch_state
*state
);
818 void add_check_tracker(const char *check_name
, check_tracker_hook
*fn
);
821 void __pass_to_client(void *data
, enum hook_type type
);
822 void __pass_case_to_client(struct expression
*switch_expr
,
823 struct range_list
*rl
);
824 int __has_merge_function(int client_id
);
825 struct smatch_state
*__client_merge_function(int owner
,
826 struct smatch_state
*s1
,
827 struct smatch_state
*s2
);
828 struct smatch_state
*__client_unmatched_state_function(struct sm_state
*sm
);
829 void call_pre_merge_hook(struct sm_state
*cur
, struct sm_state
*other
);
830 void __push_scope_hooks(void);
831 void __call_scope_hooks(void);
833 /* smatch_function_hooks.c */
834 void create_function_hook_hash(void);
835 void __match_initializer_call(struct symbol
*sym
);
841 * Changing these numbers is a pain. Don't do it. If you ever use a
842 * number it can't be re-used right away so there may be gaps.
843 * We select these in order by type so if the order matters, then give
844 * it a number below 100-999,9000-9999 ranges. */
856 ABSOLUTE_LIMITS
= 1010,
870 UNTRACKED_PARAM
= 1023,
876 COMPARE_LIMIT
= 1028,
877 PARAM_COMPARE
= 1029,
880 CONSTRAINT_REQUIRED
= 1033,
890 /* put random temporary stuff in the 7000-7999 range for testing */
892 USER_DATA_SET
= 9017,
896 NO_OVERFLOW_SIMPLE
= 8019,
900 LOCK_RESTORED
= 9023,
902 KNOWN_UNLOCKED
= 9025,
907 NO_SIDE_EFFECT
= 8025,
910 ARRAYSIZE_ARG
= 8033,
925 extern struct sqlite3
*smatch_db
;
926 extern struct sqlite3
*mem_db
;
927 extern struct sqlite3
*cache_db
;
929 bool db_incomplete(void);
930 void db_ignore_states(int id
);
931 void select_caller_info_hook(void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
), int type
);
932 void add_member_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
));
933 void add_caller_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
));
934 void add_return_info_callback(int owner
,
935 void (*callback
)(int return_id
, char *return_ranges
,
936 struct expression
*returned_expr
,
938 const char *printed_name
,
939 struct sm_state
*sm
));
940 void add_split_return_callback(void (*fn
)(int return_id
, char *return_ranges
, struct expression
*returned_expr
));
941 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
));
942 void select_call_implies_hook(int type
, void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
));
943 void select_return_implies_hook(int type
, void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
));
944 struct range_list
*db_return_vals(struct expression
*expr
);
945 struct range_list
*db_return_vals_from_str(const char *fn_name
);
946 struct range_list
*db_return_vals_no_args(struct expression
*expr
);
947 char *get_chunk_from_key(struct expression
*arg
, char *key
, struct symbol
**sym
, struct var_sym_list
**vsl
);
948 char *get_variable_from_key(struct expression
*arg
, const char *key
, struct symbol
**sym
);
949 char *get_name_sym_from_key(struct expression
*expr
, int param
, const char *key
, struct symbol
**sym
);
950 char *get_param_name_var_sym(const char *name
, struct symbol
*sym
);
951 char *get_param_name(struct sm_state
*sm
);
952 const char *get_mtag_name_var_sym(const char *state_name
, struct symbol
*sym
);
953 const char *get_mtag_name_expr(struct expression
*expr
);
954 char *get_data_info_name(struct expression
*expr
);
955 char *sm_to_arg_name(struct expression
*expr
, struct sm_state
*sm
);
956 int is_recursive_member(const char *param_name
);
958 char *escape_newlines(const char *str
);
959 void sql_exec(struct sqlite3
*db
, int (*callback
)(void*, int, char**, char**), void *data
, const char *sql
);
961 #define sql_helper(db, call_back, data, sql...) \
963 char sql_txt[1024]; \
965 sqlite3_snprintf(sizeof(sql_txt), sql_txt, sql); \
966 db_debug("debug: %s\n", sql_txt); \
967 sql_exec(db, call_back, data, sql_txt); \
971 #define run_sql(call_back, data, sql...) \
975 sql_helper(smatch_db, call_back, data, sql); \
978 #define mem_sql(call_back, data, sql...) \
979 sql_helper(mem_db, call_back, data, sql)
981 #define cache_sql(call_back, data, sql...) \
982 sql_helper(cache_db, call_back, data, sql)
984 #define sql_insert_helper(table, db, ignore, late, values...) \
986 struct sqlite3 *_db = db; \
988 if (__inline_fn && !_db) \
992 char *err, *p = buf; \
995 p += snprintf(p, buf + sizeof(buf) - p, \
996 "insert %sinto %s values (", \
997 ignore ? "or ignore " : "", #table); \
998 p += snprintf(p, buf + sizeof(buf) - p, values); \
999 p += snprintf(p, buf + sizeof(buf) - p, ");"); \
1000 db_debug("mem-db: %s\n", buf); \
1001 rc = sqlite3_exec(_db, buf, NULL, NULL, &err); \
1002 if (rc != SQLITE_OK) { \
1003 sm_ierror("SQL error #2: %s", err); \
1004 sm_ierror("SQL: '%s'", buf); \
1009 if (option_info) { \
1010 FILE *tmp_fd = sm_outfd; \
1011 sm_outfd = sql_outfd; \
1013 sm_printf("SQL%s: insert %sinto " #table " values(", \
1014 late ? "_late" : "", ignore ? "or ignore " : ""); \
1015 sm_printf(values); \
1016 sm_printf(");\n"); \
1017 sm_outfd = tmp_fd; \
1021 #define sql_insert(table, values...) sql_insert_helper(table, 0, 0, 0, values);
1022 #define sql_insert_or_ignore(table, values...) sql_insert_helper(table, 0, 1, 0, values);
1023 #define sql_insert_late(table, values...) sql_insert_helper(table, 0, 0, 1, values);
1024 #define sql_insert_cache(table, values...) sql_insert_helper(table, cache_db, 1, 0, values);
1026 char *get_static_filter(struct symbol
*sym
);
1028 void sql_insert_return_states(int return_id
, const char *return_ranges
,
1029 int type
, int param
, const char *key
, const char *value
);
1030 void sql_insert_caller_info(struct expression
*call
, int type
, int param
,
1031 const char *key
, const char *value
);
1032 void sql_insert_function_ptr(const char *fn
, const char *struct_name
);
1033 void sql_insert_return_values(const char *return_values
);
1034 void sql_insert_return_implies(int type
, int param
, const char *key
, const char *value
);
1035 void sql_insert_function_type_size(const char *member
, const char *ranges
);
1036 void sql_insert_function_type_info(int type
, const char *struct_type
, const char *member
, const char *value
);
1037 void sql_insert_type_info(int type
, const char *member
, const char *value
);
1038 void sql_insert_local_values(const char *name
, const char *value
);
1039 void sql_insert_function_type_value(const char *type
, const char *value
);
1040 void sql_insert_function_type(int param
, const char *value
);
1041 void sql_insert_parameter_name(int param
, const char *value
);
1042 void sql_insert_data_info(struct expression
*data
, int type
, const char *value
);
1043 void sql_insert_data_info_var_sym(const char *var
, struct symbol
*sym
, int type
, const char *value
);
1044 void sql_save_constraint(const char *con
);
1045 void sql_save_constraint_required(const char *data
, int op
, const char *limit
);
1046 void sql_copy_constraint_required(const char *new_limit
, const char *old_limit
);
1047 void sql_insert_fn_ptr_data_link(const char *ptr
, const char *data
);
1048 void sql_insert_fn_data_link(struct expression
*fn
, int type
, int param
, const char *key
, const char *value
);
1049 void sql_insert_mtag_about(mtag_t tag
, const char *left_name
, const char *right_name
);
1050 void sql_insert_mtag_info(mtag_t tag
, int type
, const char *value
);
1051 void sql_insert_mtag_map(mtag_t container
, int container_offset
, mtag_t tag
, int tag_offset
);
1052 void sql_insert_mtag_alias(mtag_t orig
, mtag_t alias
);
1053 int mtag_map_select_container(mtag_t tag
, int container_offset
, mtag_t
*container
);
1054 int mtag_map_select_tag(mtag_t container
, int offset
, mtag_t
*tag
);
1055 struct smatch_state
*get_mtag_return(struct expression
*expr
, struct smatch_state
*state
);
1056 struct range_list
*swap_mtag_seed(struct expression
*expr
, struct range_list
*rl
);
1058 void sql_select_return_states(const char *cols
, struct expression
*call
,
1059 int (*callback
)(void*, int, char**, char**), void *info
);
1060 void sql_select_call_implies(const char *cols
, struct expression
*call
,
1061 int (*callback
)(void*, int, char**, char**));
1063 void open_smatch_db(char *db_file
);
1065 /* smatch_files.c */
1066 int open_data_file(const char *filename
);
1067 int open_schema_file(const char *schema
);
1068 struct token
*get_tokens_file(const char *filename
);
1071 extern char *option_debug_check
;
1072 extern char *option_debug_var
;
1073 extern char *option_project_str
;
1074 extern char *bin_dir
;
1075 extern char *data_dir
;
1076 extern int option_no_data
;
1077 extern int option_full_path
;
1078 extern int option_call_tree
;
1079 extern int num_checks
;
1085 PROJ_ILLUMOS_KERNEL
,
1089 extern enum project_type option_project
;
1090 const char *check_name(unsigned short id
);
1091 int id_from_name(const char *name
);
1094 /* smatch_buf_size.c */
1095 int get_array_size(struct expression
*expr
);
1096 int get_array_size_bytes(struct expression
*expr
);
1097 int get_array_size_bytes_min(struct expression
*expr
);
1098 int get_array_size_bytes_max(struct expression
*expr
);
1099 struct range_list
*get_array_size_bytes_rl(struct expression
*expr
);
1100 int get_real_array_size(struct expression
*expr
);
1101 int last_member_is_resizable(struct symbol
*type
);
1102 /* smatch_strlen.c */
1103 int get_implied_strlen(struct expression
*expr
, struct range_list
**rl
);
1104 int get_size_from_strlen(struct expression
*expr
);
1106 /* smatch_capped.c */
1107 int is_capped(struct expression
*expr
);
1108 int is_capped_var_sym(const char *name
, struct symbol
*sym
);
1110 /* smatch_kernel_user_data.c */
1111 int is_user_macro(struct expression
*expr
);
1112 int is_capped_user_data(struct expression
*expr
);
1113 int implied_user_data(struct expression
*expr
, struct range_list
**rl
);
1114 struct stree
*get_user_stree(void);
1115 int get_user_rl(struct expression
*expr
, struct range_list
**rl
);
1116 int is_user_rl(struct expression
*expr
);
1117 int get_user_rl_var_sym(const char *name
, struct symbol
*sym
, struct range_list
**rl
);
1118 bool user_rl_capped(struct expression
*expr
);
1119 struct range_list
*var_user_rl(struct expression
*expr
);
1120 bool we_pass_user_data(struct expression
*call
);
1121 /* smatch_points_to_user_data.c */
1122 bool is_user_data_fn(struct symbol
*fn
);
1123 bool is_skb_data(struct expression
*expr
);
1124 bool points_to_user_data(struct expression
*expr
);
1125 void set_points_to_user_data(struct expression
*expr
);
1127 /* check_locking.c */
1128 void print_held_locks();
1130 /* check_assigned_expr.c */
1131 extern int check_assigned_expr_id
;
1132 struct expression
*get_assigned_expr(struct expression
*expr
);
1133 struct expression
*get_assigned_expr_recurse(struct expression
*expr
);
1134 struct expression
*get_assigned_expr_name_sym(const char *name
, struct symbol
*sym
);
1135 struct expression
*get_assigned_expr_name_sym_recurse(const char *name
, struct symbol
*sym
);
1137 /* smatch_return_to_param.c */
1138 void __add_return_to_param_mapping(struct expression
*assign
, const char *return_string
);
1139 char *map_call_to_param_name_sym(struct expression
*expr
, struct symbol
**sym
);
1141 /* smatch_comparison.c */
1142 extern int comparison_id
;
1143 #define UNKNOWN_COMPARISON 0
1144 #define IMPOSSIBLE_COMPARISON -1
1145 struct compare_data
{
1146 /* The ->left and ->right expression pointers might be NULL (I'm lazy) */
1147 struct expression
*left
;
1148 const char *left_var
;
1149 struct var_sym_list
*left_vsl
;
1151 struct expression
*right
;
1152 const char *right_var
;
1153 struct var_sym_list
*right_vsl
;
1155 DECLARE_ALLOCATOR(compare_data
);
1156 struct smatch_state
*alloc_compare_state(
1157 struct expression
*left
,
1158 const char *left_var
, struct var_sym_list
*left_vsl
,
1160 struct expression
*right
,
1161 const char *right_var
, struct var_sym_list
*right_vsl
);
1162 int comparison_intersection(int orig
, int op
);
1163 int merge_comparisons(int one
, int two
);
1164 int combine_comparisons(int left_compare
, int right_compare
);
1165 int state_to_comparison(struct smatch_state
*state
);
1166 struct smatch_state
*merge_compare_states(struct smatch_state
*s1
, struct smatch_state
*s2
);
1167 int get_comparison(struct expression
*left
, struct expression
*right
);
1168 int get_comparison_no_extra(struct expression
*a
, struct expression
*b
);
1169 int get_comparison_strings(const char *one
, const char *two
);
1170 int possible_comparison(struct expression
*a
, int comparison
, struct expression
*b
);
1171 struct state_list
*get_all_comparisons(struct expression
*expr
);
1172 struct state_list
*get_all_possible_equal_comparisons(struct expression
*expr
);
1173 void __add_return_comparison(struct expression
*call
, const char *range
);
1174 void __add_comparison_info(struct expression
*expr
, struct expression
*call
, const char *range
);
1175 char *get_printed_param_name(struct expression
*call
, const char *param_name
, struct symbol
*param_sym
);
1176 char *name_sym_to_param_comparison(const char *name
, struct symbol
*sym
);
1177 char *expr_equal_to_param(struct expression
*expr
, int ignore
);
1178 char *expr_lte_to_param(struct expression
*expr
, int ignore
);
1179 char *expr_param_comparison(struct expression
*expr
, int ignore
);
1180 int flip_comparison(int op
);
1181 int negate_comparison(int op
);
1182 int remove_unsigned_from_comparison(int op
);
1183 int param_compare_limit_is_impossible(struct expression
*expr
, int left_param
, char *left_key
, char *value
);
1184 void filter_by_comparison(struct range_list
**rl
, int comparison
, struct range_list
*right
);
1185 void __compare_param_limit_hook(struct expression
*left_expr
, struct expression
*right_expr
,
1186 const char *state_name
,
1187 struct smatch_state
*true_state
, struct smatch_state
*false_state
);
1188 int impossibly_high_comparison(struct expression
*expr
);
1191 sval_t
*sval_alloc(sval_t sval
);
1192 sval_t
*sval_alloc_permanent(sval_t sval
);
1193 sval_t
sval_blank(struct expression
*expr
);
1194 sval_t
sval_type_val(struct symbol
*type
, long long val
);
1195 sval_t
sval_type_fval(struct symbol
*type
, long double fval
);
1196 sval_t
sval_from_val(struct expression
*expr
, long long val
);
1197 sval_t
sval_from_fval(struct expression
*expr
, long double fval
);
1198 int sval_is_ptr(sval_t sval
);
1199 bool sval_is_fp(sval_t sval
);
1200 int sval_unsigned(sval_t sval
);
1201 int sval_signed(sval_t sval
);
1202 int sval_bits(sval_t sval
);
1203 int sval_bits_used(sval_t sval
);
1204 int sval_is_negative(sval_t sval
);
1205 int sval_is_positive(sval_t sval
);
1206 int sval_is_min(sval_t sval
);
1207 int sval_is_max(sval_t sval
);
1208 int sval_is_a_min(sval_t sval
);
1209 int sval_is_a_max(sval_t sval
);
1210 int sval_is_negative_min(sval_t sval
);
1211 int sval_cmp_t(struct symbol
*type
, sval_t one
, sval_t two
);
1212 int sval_cmp_val(sval_t one
, long long val
);
1213 sval_t
sval_min(sval_t one
, sval_t two
);
1214 sval_t
sval_min_nonneg(sval_t one
, sval_t two
);
1215 sval_t
sval_max(sval_t one
, sval_t two
);
1216 int sval_too_low(struct symbol
*type
, sval_t sval
);
1217 int sval_too_high(struct symbol
*type
, sval_t sval
);
1218 int sval_fits(struct symbol
*type
, sval_t sval
);
1219 sval_t
sval_cast(struct symbol
*type
, sval_t sval
);
1220 sval_t
sval_preop(sval_t sval
, int op
);
1221 sval_t
sval_binop(sval_t left
, int op
, sval_t right
);
1222 int sval_binop_overflows(sval_t left
, int op
, sval_t right
);
1223 int sval_binop_overflows_no_sign(sval_t left
, int op
, sval_t right
);
1224 int find_first_zero_bit(unsigned long long uvalue
);
1225 int sm_fls64(unsigned long long uvalue
);
1226 unsigned long long fls_mask(unsigned long long uvalue
);
1227 unsigned long long sval_fls_mask(sval_t sval
);
1228 const char *sval_to_str(sval_t sval
);
1229 const char *sval_to_str_or_err_ptr(sval_t sval
);
1230 const char *sval_to_numstr(sval_t sval
);
1231 sval_t
ll_to_sval(long long val
);
1233 /* smatch_string_list.c */
1234 int list_has_string(struct string_list
*str_list
, const char *str
);
1235 int insert_string(struct string_list
**str_list
, const char *str
);
1236 struct string_list
*clone_str_list(struct string_list
*orig
);
1237 struct string_list
*combine_string_lists(struct string_list
*one
, struct string_list
*two
);
1239 /* smatch_start_states.c */
1240 struct stree
*get_start_states(void);
1242 /* smatch_recurse.c */
1243 int has_symbol(struct expression
*expr
, struct symbol
*sym
);
1244 int has_variable(struct expression
*expr
, struct expression
*var
);
1245 int has_inc_dec(struct expression
*expr
);
1247 /* smatch_stored_conditions.c */
1248 struct smatch_state
*get_stored_condition(struct expression
*expr
);
1249 struct expression_list
*get_conditions(struct expression
*expr
);
1250 struct sm_state
*stored_condition_implication_hook(struct expression
*expr
,
1251 struct state_list
**true_stack
,
1252 struct state_list
**false_stack
);
1253 /* smatch_parsed_conditions.c */
1254 struct sm_state
*parsed_condition_implication_hook(struct expression
*expr
,
1255 struct state_list
**true_stack
,
1256 struct state_list
**false_stack
);
1257 /* smatch_comparison.c */
1258 struct sm_state
*comparison_implication_hook(struct expression
*expr
,
1259 struct state_list
**true_stack
,
1260 struct state_list
**false_stack
);
1262 /* check_string_len.c */
1263 int get_formatted_string_size(struct expression
*call
, int arg
);
1264 int get_formatted_string_min_size(struct expression
*call
, int arg
);
1266 /* smatch_param_set.c */
1267 int param_was_set(struct expression
*expr
);
1268 int param_was_set_var_sym(const char *name
, struct symbol
*sym
);
1269 void print_limited_param_set(int return_id
, char *return_ranges
, struct expression
*expr
);
1270 /* smatch_param_filter.c */
1271 int param_has_filter_data(struct sm_state
*sm
);
1273 /* smatch_links.c */
1274 void set_up_link_functions(int id
, int linkid
);
1275 struct smatch_state
*merge_link_states(struct smatch_state
*s1
, struct smatch_state
*s2
);
1276 void store_link(int link_id
, const char *name
, struct symbol
*sym
, const char *link_name
, struct symbol
*link_sym
);
1278 /* check_buf_comparison */
1279 const char *limit_type_str(unsigned int limit_type
);
1280 struct expression
*get_size_variable(struct expression
*buf
, int *limit_type
);
1281 struct expression
*get_array_variable(struct expression
*size
);
1282 int buf_comparison_index_ok(struct expression
*expr
);
1284 /* smatch_untracked_param.c */
1285 void mark_untracked(struct expression
*expr
, int param
, const char *key
, const char *value
);
1286 void add_untracked_param_hook(void (func
)(struct expression
*call
, int param
));
1287 void add_lost_param_hook(void (func
)(struct expression
*call
, int param
));
1288 void mark_all_params_untracked(int return_id
, char *return_ranges
, struct expression
*expr
);
1290 /* smatch_strings.c */
1291 struct state_list
*get_strings(struct expression
*expr
);
1292 struct expression
*fake_string_from_mtag(mtag_t tag
);
1294 /* smatch_estate.c */
1295 int estate_get_single_value(struct smatch_state
*state
, sval_t
*sval
);
1297 /* smatch_address.c */
1298 int get_address_rl(struct expression
*expr
, struct range_list
**rl
);
1299 int get_member_offset(struct symbol
*type
, const char *member_name
);
1300 int get_member_offset_from_deref(struct expression
*expr
);
1302 /* for now this is in smatch_used_parameter.c */
1303 void __get_state_hook(int owner
, const char *name
, struct symbol
*sym
);
1304 extern int __ignore_param_used
;
1306 /* smatch_buf_comparison.c */
1307 int db_var_is_array_limit(struct expression
*array
, const char *name
, struct var_sym_list
*vsl
);
1309 struct range_list
*get_fs(void);
1311 struct stree
*get_all_return_states(void);
1312 struct stree_stack
*get_all_return_strees(void);
1313 int on_atomic_dec_path(void);
1314 int was_inced(const char *name
, struct symbol
*sym
);
1315 void set_refcount_inc(char *name
, struct symbol
*sym
);
1316 void set_refcount_dec(char *name
, struct symbol
*sym
);
1318 /* smatch_constraints.c */
1319 char *get_constraint_str(struct expression
*expr
);
1320 struct constraint_list
*get_constraints(struct expression
*expr
);
1321 char *unmet_constraint(struct expression
*data
, struct expression
*offset
);
1322 char *get_required_constraint(const char *data_str
);
1324 /* smatch_container_of.c */
1325 int get_param_from_container_of(struct expression
*expr
);
1326 int get_offset_from_container_of(struct expression
*expr
);
1327 char *get_container_name(struct expression
*container
, struct expression
*expr
);
1330 mtag_t
str_to_mtag(const char *str
);
1331 int get_string_mtag(struct expression
*expr
, mtag_t
*tag
);
1332 int get_toplevel_mtag(struct symbol
*sym
, mtag_t
*tag
);
1333 int create_mtag_alias(mtag_t tag
, struct expression
*expr
, mtag_t
*new);
1334 int expr_to_mtag_offset(struct expression
*expr
, mtag_t
*tag
, int *offset
);
1335 void update_mtag_data(struct expression
*expr
, struct smatch_state
*state
);
1336 int get_mtag_sval(struct expression
*expr
, sval_t
*sval
);
1338 /* Trinity fuzzer stuff */
1339 const char *get_syscall_arg_type(struct symbol
*sym
);
1341 /* smatch_bit_info.c */
1342 struct bit_info
*rl_to_binfo(struct range_list
*rl
);
1343 struct bit_info
*get_bit_info(struct expression
*expr
);
1344 struct bit_info
*get_bit_info_var_sym(const char *name
, struct symbol
*sym
);
1345 /* smatch_mem_tracker.c */
1346 extern int option_mem
;
1347 unsigned long get_mem_kb(void);
1348 unsigned long get_max_memory(void);
1350 /* check_is_nospec.c */
1351 bool is_nospec(struct expression
*expr
);
1352 long get_stmt_cnt(void);
1354 /* smatch_nul_terminator.c */
1355 bool is_nul_terminated_var_sym(const char *name
, struct symbol
*sym
);
1356 bool is_nul_terminated(struct expression
*expr
);
1357 /* check_kernel.c */
1358 bool is_ignored_kernel_data(const char *name
);
1360 bool is_fresh_alloc_var_sym(const char *var
, struct symbol
*sym
);
1361 bool is_fresh_alloc(struct expression
*expr
);
1362 bool is_freed_var_sym(const char *name
, struct symbol
*sym
);
1363 void track_freed_param(struct expression
*expr
, struct smatch_state
*state
);
1364 void track_freed_param_var_sym(const char *name
, struct symbol
*sym
,
1365 struct smatch_state
*state
);
1367 static inline bool type_is_ptr(struct symbol
*type
)
1370 (type
->type
== SYM_PTR
||
1371 type
->type
== SYM_ARRAY
||
1372 type
->type
== SYM_FN
);
1375 static inline bool type_is_fp(struct symbol
*type
)
1378 (type
== &float_ctype
||
1379 type
== &double_ctype
||
1380 type
== &ldouble_ctype
);
1383 static inline int type_bits(struct symbol
*type
)
1387 if (type_is_ptr(type
))
1388 return bits_in_pointer
;
1389 if (!type
->examined
)
1390 examine_symbol_type(type
);
1391 return type
->bit_size
;
1394 static inline int type_unsigned(struct symbol
*base_type
)
1398 if (is_ptr_type(base_type
))
1400 if (base_type
->ctype
.modifiers
& MOD_UNSIGNED
)
1405 static inline int type_positive_bits(struct symbol
*type
)
1409 if (is_ptr_type(type
))
1410 return bits_in_pointer
;
1411 if (type_unsigned(type
))
1412 return type_bits(type
);
1413 return type_bits(type
) - 1;
1416 static inline int sval_positive_bits(sval_t sval
)
1418 return type_positive_bits(sval
.type
);
1422 * Returns -1 if one is smaller, 0 if they are the same and 1 if two is larger.
1425 static inline int fp_cmp(sval_t one
, sval_t two
)
1427 struct symbol
*type
;
1429 if (sval_is_fp(one
) && sval_is_fp(two
))
1430 type
= type_bits(one
.type
) > type_bits(two
.type
) ? one
.type
: two
.type
;
1431 else if (sval_is_fp(one
))
1436 one
= sval_cast(type
, one
);
1437 two
= sval_cast(type
, two
);
1439 if (one
.type
== &float_ctype
) {
1440 if (one
.fvalue
< two
.fvalue
)
1442 if (one
.fvalue
== two
.fvalue
)
1446 if (one
.type
== &double_ctype
) {
1447 if (one
.dvalue
< two
.dvalue
)
1449 if (one
.dvalue
== two
.dvalue
)
1453 if (one
.type
== &ldouble_ctype
) {
1454 if (one
.ldvalue
< two
.ldvalue
)
1456 if (one
.ldvalue
== two
.ldvalue
)
1460 sm_perror("bad type in fp_cmp(): %s", type_to_str(type
));
1464 static inline int sval_cmp(sval_t one
, sval_t two
)
1466 struct symbol
*type
;
1468 if (sval_is_fp(one
) || sval_is_fp(two
))
1469 return fp_cmp(one
, two
);
1472 if (sval_positive_bits(two
) > sval_positive_bits(one
))
1474 if (type_bits(type
) < 31)
1477 one
= sval_cast(type
, one
);
1478 two
= sval_cast(type
, two
);
1480 if (type_unsigned(type
)) {
1481 if (one
.uvalue
< two
.uvalue
)
1483 if (one
.uvalue
== two
.uvalue
)
1487 /* fix me handle type promotion and unsigned values */
1488 if (one
.value
< two
.value
)
1490 if (one
.value
== two
.value
)
1495 #endif /* !SMATCH_H_ */