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"
33 #include "smatch_constants.h"
35 typedef long long mtag_t
;
41 #define STATE(_x) static struct smatch_state _x = { .name = #_x }
42 extern struct smatch_state undefined
;
43 extern struct smatch_state ghost
;
44 extern struct smatch_state merged
;
45 extern struct smatch_state true_state
;
46 extern struct smatch_state false_state
;
47 DECLARE_ALLOCATOR(smatch_state
);
49 static inline void *INT_PTR(int i
)
51 return (void *)(long)i
;
54 static inline int PTR_INT(void *p
)
64 DECLARE_ALLOCATOR(tracker
);
65 DECLARE_PTR_LIST(tracker_list
, struct tracker
);
66 DECLARE_PTR_LIST(stree_stack
, struct stree
);
68 /* The first 3 struct members must match struct tracker */
73 unsigned short merged
:1;
75 struct smatch_state
*state
;
77 struct sm_state
*left
;
78 struct sm_state
*right
;
79 struct state_list
*possible
;
86 DECLARE_ALLOCATOR(var_sym
);
87 DECLARE_PTR_LIST(var_sym_list
, struct var_sym
);
93 DECLARE_PTR_LIST(constraint_list
, struct constraint
);
99 extern struct alloc_info
*alloc_funcs
;
102 unsigned long long set
;
103 unsigned long long possible
;
115 ASSIGNMENT_HOOK_AFTER
,
117 GLOBAL_ASSIGNMENT_HOOK
,
122 WHOLE_CONDITION_HOOK
,
123 FUNCTION_CALL_HOOK_BEFORE
,
125 CALL_HOOK_AFTER_INLINE
,
126 FUNCTION_CALL_HOOK_AFTER_DB
,
127 CALL_ASSIGNMENT_HOOK
,
128 MACRO_ASSIGNMENT_HOOK
,
153 void add_hook(void *func
, enum hook_type type
);
154 typedef struct smatch_state
*(merge_func_t
)(struct smatch_state
*s1
, struct smatch_state
*s2
);
155 typedef struct smatch_state
*(unmatched_func_t
)(struct sm_state
*state
);
156 void add_merge_hook(int client_id
, merge_func_t
*func
);
157 void add_unmatched_state_hook(int client_id
, unmatched_func_t
*func
);
158 void add_pre_merge_hook(int client_id
, void (*hook
)(struct sm_state
*cur
, struct sm_state
*other
));
159 typedef void (scope_hook
)(void *data
);
160 void add_scope_hook(scope_hook
*hook
, void *data
);
161 typedef void (param_key_hook
)(struct expression
*expr
, const char *name
, struct symbol
*sym
, void *data
);
162 typedef void (func_hook
)(const char *fn
, struct expression
*expr
, void *data
);
163 typedef void (implication_hook
)(const char *fn
, struct expression
*call_expr
,
164 struct expression
*assign_expr
, void *data
);
165 typedef void (return_implies_hook
)(struct expression
*call_expr
,
166 int param
, char *key
, char *value
);
167 typedef int (implied_return_hook
)(struct expression
*call_expr
, void *info
, struct range_list
**rl
);
168 void add_function_hook(const char *look_for
, func_hook
*call_back
, void *data
);
169 void add_function_hook_late(const char *look_for
, func_hook
*call_back
, void *info
);
171 void add_function_assign_hook(const char *look_for
, func_hook
*call_back
,
173 void register_func_hooks_from_file(const char *file
,
174 func_hook
*call_back
, void *info
);
175 void register_assign_hooks_from_file(const char *file
,
176 func_hook
*call_back
, void *info
);
177 void add_implied_return_hook(const char *look_for
,
178 implied_return_hook
*call_back
,
180 void add_macro_assign_hook(const char *look_for
, func_hook
*call_back
,
182 void add_macro_assign_hook_extra(const char *look_for
, func_hook
*call_back
,
184 void return_implies_state(const char *look_for
, long long start
, long long end
,
185 implication_hook
*call_back
, void *info
);
186 void return_implies_state_sval(const char *look_for
, sval_t start
, sval_t end
,
187 implication_hook
*call_back
, void *info
);
188 void return_implies_exact(const char *look_for
, sval_t start
, sval_t end
,
189 implication_hook
*call_back
, void *info
);
190 struct range_list
*get_range_implications(const char *fn
);
191 void select_return_states_hook(int type
, return_implies_hook
*callback
);
192 void select_return_states_before(void (*fn
)(void));
193 void select_return_states_after(void (*fn
)(void));
194 void add_function_param_key_hook(const char *look_for
, param_key_hook
*call_back
,
195 int param
, const char *key
, void *info
);
196 void return_implies_param_key(const char *look_for
, sval_t start
, sval_t end
,
197 param_key_hook
*call_back
,
198 int param
, const char *key
, void *info
);
199 void return_implies_param_key_exact(const char *look_for
, sval_t start
, sval_t end
,
200 param_key_hook
*call_back
,
201 int param
, const char *key
, void *info
);
202 void select_return_param_key(int type
, param_key_hook
*callback
);
203 int get_implied_return(struct expression
*expr
, struct range_list
**rl
);
204 void allocate_hook_memory(void);
205 void allocate_tracker_array(int num_checks
);
207 struct modification_data
{
208 struct smatch_state
*prev
;
209 struct expression
*cur
;
212 typedef void (modification_hook
)(struct sm_state
*sm
, struct expression
*mod_expr
);
213 bool is_sub_member(const char *name
, struct symbol
*sym
, struct sm_state
*sm
);
214 void add_modification_hook(int owner
, modification_hook
*call_back
);
215 void add_modification_hook_late(int owner
, modification_hook
*call_back
);
216 struct smatch_state
*get_modification_state(struct expression
*expr
);
218 int outside_of_function(void);
219 const char *get_filename(void);
220 const char *get_base_file(void);
221 char *get_function(void);
222 int get_lineno(void);
223 extern int final_pass
;
224 extern struct symbol
*cur_func_sym
;
225 extern int option_debug
;
226 extern int local_debug
;
228 extern bool implied_debug
;
229 bool debug_implied(void);
230 bool debug_on(const char *check_name
, const char *var
);
231 extern int option_info
;
232 extern int option_spammy
;
233 extern int option_pedantic
;
234 extern int option_print_names
;
235 extern char *trace_variable
;
236 extern struct stree
*global_states
;
237 void set_function_skipped(void);
238 int is_skipped_function(void);
239 int is_silenced_function(void);
240 extern bool implications_off
;
242 /* smatch_impossible.c */
243 int is_impossible_path(void);
244 void set_path_impossible(void);
246 extern FILE *sm_outfd
;
247 extern FILE *sql_outfd
;
248 extern FILE *caller_info_fd
;
249 extern int sm_nr_checks
;
250 extern int sm_nr_errors
;
253 * How to use these routines:
255 * sm_fatal(): an internal error of some kind that should immediately exit
256 * sm_ierror(): an internal error
257 * sm_perror(): an internal error from parsing input source
258 * sm_error(): an error from input source
259 * sm_warning(): a warning from input source
260 * sm_info(): info message (from option_info)
261 * sm_debug(): debug message
262 * sm_msg(): other message (please avoid using this)
265 #define sm_printf(msg...) do { \
266 if (final_pass || option_debug || local_debug || debug_db) \
267 fprintf(sm_outfd, msg); \
270 static inline void sm_prefix(void)
272 sm_printf("%s:%d %s() ", get_filename(), get_lineno(), get_function());
273 if (option_info
|| !option_print_names
)
275 sm_printf("[smatch.%s] ", __CHECKNAME__
);
278 static inline void print_implied_debug_msg();
280 extern bool __silence_warnings_for_stmt
;
282 #define sm_print_msg(type, msg...) \
284 print_implied_debug_msg(); \
285 if (!final_pass && !option_debug && !local_debug && !debug_db) \
287 if (__silence_warnings_for_stmt && !option_debug && !local_debug) \
289 if (!option_info && is_silenced_function()) \
293 sm_printf("warn: "); \
295 } else if (type == 2) { \
296 sm_printf("error: "); \
298 } else if (type == 3) { \
299 sm_printf("parse error: "); \
306 #define sm_msg(msg...) do { sm_print_msg(0, msg); } while (0)
308 extern char *implied_debug_msg
;
309 static inline void print_implied_debug_msg(void)
311 static struct symbol
*last_printed
= NULL
;
313 if (!implied_debug_msg
)
315 if (last_printed
== cur_func_sym
)
317 last_printed
= cur_func_sym
;
318 sm_msg("%s", implied_debug_msg
);
321 #define sm_debug(msg...) do { if (option_debug) sm_printf(msg); } while (0)
322 #define db_debug(msg...) do { if (option_debug || debug_db) sm_printf(msg); } while (0)
324 #define sm_info(msg...) do { \
325 if (option_debug || (option_info && final_pass)) { \
327 sm_printf("info: "); \
333 #define sm_warning(msg...) do { sm_print_msg(1, msg); } while (0)
334 #define sm_error(msg...) do { sm_print_msg(2, msg); } while (0)
335 #define sm_perror(msg...) do { sm_print_msg(3, msg); } while (0)
336 #define sm_spam(msg...) do { if (option_pedantic) sm_print_msg(1, msg); } while (0)
338 static inline void sm_fatal(const char *fmt
, ...)
343 vfprintf(sm_outfd
, fmt
, args
);
346 fprintf(sm_outfd
, "\n");
351 static inline void sm_ierror(const char *fmt
, ...)
357 fprintf(sm_outfd
, "internal error: ");
360 vfprintf(sm_outfd
, fmt
, args
);
363 fprintf(sm_outfd
, "\n");
365 #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
367 bool has_states(struct stree
*stree
, int owner
);
368 struct smatch_state
*__get_state(int owner
, const char *name
, struct symbol
*sym
);
369 struct smatch_state
*get_state(int owner
, const char *name
, struct symbol
*sym
);
370 struct smatch_state
*get_state_expr(int owner
, struct expression
*expr
);
371 struct state_list
*get_possible_states(int owner
, const char *name
,
373 struct state_list
*get_possible_states_expr(int owner
, struct expression
*expr
);
374 struct sm_state
*set_state(int owner
, const char *name
, struct symbol
*sym
,
375 struct smatch_state
*state
);
376 struct sm_state
*set_state_expr(int owner
, struct expression
*expr
,
377 struct smatch_state
*state
);
378 void __delete_state(int owner
, const char *name
, struct symbol
*sym
);
379 void __delete_all_states_sym(struct symbol
*sym
);
380 void set_true_false_states(int owner
, const char *name
, struct symbol
*sym
,
381 struct smatch_state
*true_state
,
382 struct smatch_state
*false_state
);
383 void set_true_false_states_expr(int owner
, struct expression
*expr
,
384 struct smatch_state
*true_state
,
385 struct smatch_state
*false_state
);
387 struct stree
*get_all_states_from_stree(int owner
, struct stree
*source
);
388 struct stree
*get_all_states_stree(int id
);
389 struct stree
*__get_cur_stree(void);
390 int is_reachable(void);
391 void add_get_state_hook(void (*fn
)(int owner
, const char *name
, struct symbol
*sym
));
393 /* smatch_helper.c */
394 DECLARE_PTR_LIST(int_stack
, int);
395 char *alloc_string(const char *str
);
396 char *alloc_string_newline(const char *str
);
397 void free_string(char *str
);
398 void remove_parens(char *str
);
399 struct smatch_state
*alloc_state_num(int num
);
400 struct smatch_state
*alloc_state_str(const char *name
);
401 struct smatch_state
*merge_str_state(struct smatch_state
*s1
, struct smatch_state
*s2
);
402 struct smatch_state
*alloc_state_expr(struct expression
*expr
);
403 struct expression
*get_argument_from_call_expr(struct expression_list
*args
,
405 struct expression
*get_array_expr(struct expression
*expr
);
407 char *expr_to_var(struct expression
*expr
);
408 struct symbol
*expr_to_sym(struct expression
*expr
);
409 char *expr_to_str(struct expression
*expr
);
410 char *expr_to_str_sym(struct expression
*expr
,
411 struct symbol
**sym_ptr
);
412 char *expr_to_var_sym(struct expression
*expr
,
413 struct symbol
**sym_ptr
);
414 char *expr_to_known_chunk_sym(struct expression
*expr
, struct symbol
**sym
);
415 char *expr_to_chunk_sym_vsl(struct expression
*expr
, struct symbol
**sym
, struct var_sym_list
**vsl
);
416 int get_complication_score(struct expression
*expr
);
418 int sym_name_is(const char *name
, struct expression
*expr
);
419 int get_const_value(struct expression
*expr
, sval_t
*sval
);
420 int get_value(struct expression
*expr
, sval_t
*val
);
421 int get_implied_value(struct expression
*expr
, sval_t
*val
);
422 int get_implied_value_fast(struct expression
*expr
, sval_t
*sval
);
423 int get_implied_min(struct expression
*expr
, sval_t
*sval
);
424 int get_implied_max(struct expression
*expr
, sval_t
*val
);
425 int get_hard_max(struct expression
*expr
, sval_t
*sval
);
426 int get_fuzzy_min(struct expression
*expr
, sval_t
*min
);
427 int get_fuzzy_max(struct expression
*expr
, sval_t
*max
);
428 int get_absolute_min(struct expression
*expr
, sval_t
*sval
);
429 int get_absolute_max(struct expression
*expr
, sval_t
*sval
);
430 int parse_call_math(struct expression
*expr
, char *math
, sval_t
*val
);
431 int parse_call_math_rl(struct expression
*call
, const char *math
, struct range_list
**rl
);
432 const char *get_allocation_math(struct expression
*expr
);
433 char *get_value_in_terms_of_parameter_math(struct expression
*expr
);
434 char *get_value_in_terms_of_parameter_math_var_sym(const char *var
, struct symbol
*sym
);
435 int expr_is_zero(struct expression
*expr
);
436 int known_condition_true(struct expression
*expr
);
437 int known_condition_false(struct expression
*expr
);
438 int implied_condition_true(struct expression
*expr
);
439 int implied_condition_false(struct expression
*expr
);
440 int can_integer_overflow(struct symbol
*type
, struct expression
*expr
);
441 void clear_math_cache(void);
442 void set_fast_math_only(void);
443 void clear_fast_math_only(void);
445 int is_array(struct expression
*expr
);
446 struct expression
*get_array_base(struct expression
*expr
);
447 struct expression
*get_array_offset(struct expression
*expr
);
448 const char *show_state(struct smatch_state
*state
);
449 struct statement
*get_expression_statement(struct expression
*expr
);
450 struct expression
*strip_parens(struct expression
*expr
);
451 struct expression
*strip_expr(struct expression
*expr
);
452 struct expression
*strip_expr_set_parent(struct expression
*expr
);
453 void scoped_state(int my_id
, const char *name
, struct symbol
*sym
);
454 int is_error_return(struct expression
*expr
);
455 int getting_address(struct expression
*expr
);
456 int get_struct_and_member(struct expression
*expr
, const char **type
, const char **member
);
457 char *get_member_name(struct expression
*expr
);
458 char *get_fnptr_name(struct expression
*expr
);
459 int cmp_pos(struct position pos1
, struct position pos2
);
460 int positions_eq(struct position pos1
, struct position pos2
);
461 struct statement
*get_current_statement(void);
462 struct statement
*get_prev_statement(void);
463 struct expression
*get_last_expr_from_expression_stmt(struct expression
*expr
);
465 #define RETURN_VAR -1
466 #define LOCAL_SCOPE -2
467 #define FILE_SCOPE -3
468 #define GLOBAL_SCOPE -4
469 #define UNKNOWN_SCOPE -5
471 char *get_param_var_sym_var_sym(const char *name
, struct symbol
*sym
, struct expression
*ret_expr
, struct symbol
**sym_p
);
472 char *get_param_name_sym(struct expression
*expr
, struct symbol
**sym_p
);
473 int get_param_key_from_var_sym(const char *name
, struct symbol
*sym
,
474 struct expression
*ret_expr
,
476 int get_param_key_from_sm(struct sm_state
*sm
, struct expression
*ret_expr
,
478 int map_to_param(const char *name
, struct symbol
*sym
);
479 int get_param_num_from_sym(struct symbol
*sym
);
480 int get_param_num(struct expression
*expr
);
481 struct symbol
*get_param_sym_from_num(int num
);
483 int ms_since(struct timeval
*start
);
484 int parent_is_gone_var_sym(const char *name
, struct symbol
*sym
);
485 int parent_is_gone(struct expression
*expr
);
486 bool is_noderef_ptr(struct expression
*expr
);
487 int invert_op(int op
);
488 int op_remove_assign(int op
);
489 int expr_equiv(struct expression
*one
, struct expression
*two
);
490 void push_int(struct int_stack
**stack
, int num
);
491 int pop_int(struct int_stack
**stack
);
494 struct symbol
*get_real_base_type(struct symbol
*sym
);
495 int type_bytes(struct symbol
*type
);
496 int array_bytes(struct symbol
*type
);
497 struct symbol
*get_pointer_type(struct expression
*expr
);
498 struct symbol
*get_type(struct expression
*expr
);
499 struct symbol
*get_final_type(struct expression
*expr
);
500 struct symbol
*get_promoted_type(struct symbol
*left
, struct symbol
*right
);
501 int type_signed(struct symbol
*base_type
);
502 int expr_unsigned(struct expression
*expr
);
503 int expr_signed(struct expression
*expr
);
504 int returns_unsigned(struct symbol
*base_type
);
505 int is_pointer(struct expression
*expr
);
506 int returns_pointer(struct symbol
*base_type
);
507 sval_t
sval_type_max(struct symbol
*base_type
);
508 sval_t
sval_type_min(struct symbol
*base_type
);
509 int nr_bits(struct expression
*expr
);
510 int is_void_pointer(struct expression
*expr
);
511 int is_char_pointer(struct expression
*expr
);
512 int is_string(struct expression
*expr
);
513 bool is_struct_ptr(struct symbol
*type
);
514 int is_static(struct expression
*expr
);
515 bool is_local_variable(struct expression
*expr
);
516 int types_equiv(struct symbol
*one
, struct symbol
*two
);
517 bool type_fits(struct symbol
*type
, struct symbol
*test
);
519 const char *global_static();
520 struct symbol
*cur_func_return_type(void);
521 struct symbol
*get_arg_type(struct expression
*fn
, int arg
);
522 struct symbol
*get_member_type_from_key(struct expression
*expr
, const char *key
);
523 struct symbol
*get_arg_type_from_key(struct expression
*fn
, int param
, struct expression
*arg
, const char *key
);
524 int is_struct(struct expression
*expr
);
525 char *type_to_str(struct symbol
*type
);
527 /* smatch_ignore.c */
528 void add_ignore(int owner
, const char *name
, struct symbol
*sym
);
529 int is_ignored(int owner
, const char *name
, struct symbol
*sym
);
530 void add_ignore_expr(int owner
, struct expression
*expr
);
531 int is_ignored_expr(int owner
, struct expression
*expr
);
534 struct smatch_state
*alloc_var_sym_state(const char *var
, struct symbol
*sym
);
535 struct var_sym
*alloc_var_sym(const char *var
, struct symbol
*sym
);
536 struct var_sym_list
*expr_to_vsl(struct expression
*expr
);
537 void add_var_sym(struct var_sym_list
**list
, const char *var
, struct symbol
*sym
);
538 void add_var_sym_expr(struct var_sym_list
**list
, struct expression
*expr
);
539 void del_var_sym(struct var_sym_list
**list
, const char *var
, struct symbol
*sym
);
540 int in_var_sym_list(struct var_sym_list
*list
, const char *var
, struct symbol
*sym
);
541 struct var_sym_list
*clone_var_sym_list(struct var_sym_list
*from_vsl
);
542 void merge_var_sym_list(struct var_sym_list
**dest
, struct var_sym_list
*src
);
543 struct var_sym_list
*combine_var_sym_lists(struct var_sym_list
*one
, struct var_sym_list
*two
);
544 int var_sym_lists_equiv(struct var_sym_list
*one
, struct var_sym_list
*two
);
545 void free_var_sym_list(struct var_sym_list
**list
);
546 void free_var_syms_and_list(struct var_sym_list
**list
);
549 struct tracker
*alloc_tracker(int owner
, const char *name
, struct symbol
*sym
);
550 void add_tracker(struct tracker_list
**list
, int owner
, const char *name
,
552 void add_tracker_expr(struct tracker_list
**list
, int owner
, struct expression
*expr
);
553 void del_tracker(struct tracker_list
**list
, int owner
, const char *name
,
555 int in_tracker_list(struct tracker_list
*list
, int owner
, const char *name
,
557 void free_tracker_list(struct tracker_list
**list
);
558 void free_trackers_and_list(struct tracker_list
**list
);
560 /* smatch_conditions */
561 int in_condition(void);
565 extern int __in_fake_assign
;
566 extern int __in_fake_parameter_assign
;
567 extern int __in_fake_struct_assign
;
568 extern int __in_fake_var_assign
;
569 extern int __fake_state_cnt
;
570 extern int in_fake_env
;
571 void smatch (struct string_list
*filelist
);
572 int inside_loop(void);
573 int definitely_inside_loop(void);
574 struct expression
*get_switch_expr(void);
575 int in_expression_statement(void);
576 void __process_post_op_stack(void);
577 void __split_expr(struct expression
*expr
);
578 void __split_label_stmt(struct statement
*stmt
);
579 void __split_stmt(struct statement
*stmt
);
580 extern int __in_function_def
;
581 extern int __in_unmatched_hook
;
582 extern int option_assume_loops
;
583 extern int option_two_passes
;
584 extern int option_no_db
;
585 extern int option_file_output
;
586 extern int option_time
;
587 extern int option_time_stmt
;
588 extern struct expression_list
*big_expression_stack
;
589 extern struct expression_list
*big_condition_stack
;
590 extern struct statement_list
*big_statement_stack
;
591 int is_assigned_call(struct expression
*expr
);
592 int is_fake_assigned_call(struct expression
*expr
);
593 void add_function_data(unsigned long *fn_data
);
594 int inlinable(struct expression
*expr
);
595 extern int __inline_call
;
596 extern struct expression
*__inline_fn
;
597 extern int __in_pre_condition
;
598 extern int __bail_on_rest_of_function
;
599 extern struct statement
*__prev_stmt
;
600 extern struct statement
*__cur_stmt
;
601 extern struct statement
*__next_stmt
;
602 void init_fake_env(void);
603 void end_fake_env(void);
604 int time_parsing_function(void);
605 bool taking_too_long(void);
606 int is_last_stmt(struct statement
*cur_stmt
);
608 /* smatch_struct_assignment.c */
609 struct expression
*get_faked_expression(void);
610 void __fake_struct_member_assignments(struct expression
*expr
);
612 /* smatch_project.c */
613 int is_no_inline_function(const char *function
);
615 /* smatch_conditions */
616 void __split_whole_condition(struct expression
*expr
);
617 void __handle_logic(struct expression
*expr
);
618 int is_condition(struct expression
*expr
);
619 int __handle_condition_assigns(struct expression
*expr
);
620 int __handle_select_assigns(struct expression
*expr
);
621 int __handle_expr_statement_assigns(struct expression
*expr
);
623 /* smatch_implied.c */
624 struct range_list_stack
;
625 void param_limit_implications(struct expression
*expr
, int param
, char *key
, char *value
, struct stree
**implied
);
626 struct stree
*__implied_case_stree(struct expression
*switch_expr
,
627 struct range_list
*case_rl
,
628 struct range_list_stack
**remaining_cases
,
629 struct stree
**raw_stree
);
630 void overwrite_states_using_pool(struct sm_state
*gate_sm
, struct sm_state
*pool_sm
);
631 int assume(struct expression
*expr
);
632 void end_assume(void);
633 int impossible_assumption(struct expression
*left
, int op
, sval_t sval
);
636 bool has_dynamic_states(unsigned short owner
);
637 void set_dynamic_states(unsigned short owner
);
639 /* smatch_extras.c */
640 int in_warn_on_macro(void);
641 #define SMATCH_EXTRA 5 /* this is my_id from smatch extra set in smatch.c */
642 extern int RETURN_ID
;
649 char *get_other_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
);
650 char *map_call_to_other_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
);
651 char *map_long_to_short_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
, bool use_stack
);
653 #define STRLEN_MAX_RET 1010101
655 /* smatch_absolute.c */
656 int get_absolute_min_helper(struct expression
*expr
, sval_t
*sval
);
657 int get_absolute_max_helper(struct expression
*expr
, sval_t
*sval
);
659 /* smatch_type_value.c */
660 void clear_type_value_cache(void);
661 int get_db_type_rl(struct expression
*expr
, struct range_list
**rl
);
663 /* smatch_data_val.c */
664 int get_mtag_rl(struct expression
*expr
, struct range_list
**rl
);
665 /* smatch_array_values.c */
666 int get_array_rl(struct expression
*expr
, struct range_list
**rl
);
668 /* smatch_states.c */
669 struct stree
*__swap_cur_stree(struct stree
*stree
);
670 void __push_fake_cur_stree();
671 struct stree
*__pop_fake_cur_stree();
672 void __free_fake_cur_stree();
673 void __set_fake_cur_stree_fast(struct stree
*stree
);
674 void __pop_fake_cur_stree_fast(void);
675 void __merge_stree_into_cur(struct stree
*stree
);
677 int unreachable(void);
678 void __set_cur_stree_readonly(void);
679 void __set_cur_stree_writable(void);
680 void __set_sm(struct sm_state
*sm
);
681 void __set_sm_cur_stree(struct sm_state
*sm
);
682 void __set_sm_fake_stree(struct sm_state
*sm
);
683 void __set_true_false_sm(struct sm_state
*true_state
,
684 struct sm_state
*false_state
);
685 void nullify_path(void);
686 void __match_nullify_path_hook(const char *fn
, struct expression
*expr
,
688 void __unnullify_path(void);
689 int __path_is_null(void);
690 void save_all_states(void);
691 void restore_all_states(void);
692 void free_goto_stack(void);
693 void clear_all_states(void);
695 struct sm_state
*get_sm_state(int owner
, const char *name
,
697 struct sm_state
*get_sm_state_expr(int owner
, struct expression
*expr
);
698 void __push_true_states(void);
699 void __use_false_states(void);
700 void __discard_false_states(void);
701 void __merge_false_states(void);
702 void __merge_true_states(void);
704 void __negate_cond_stacks(void);
705 void __use_pre_cond_states(void);
706 void __use_cond_true_states(void);
707 void __use_cond_false_states(void);
708 void __push_cond_stacks(void);
709 void __fold_in_set_states(void);
710 void __free_set_states(void);
711 struct stree
*__copy_cond_true_states(void);
712 struct stree
*__copy_cond_false_states(void);
713 struct stree
*__pop_cond_true_stack(void);
714 struct stree
*__pop_cond_false_stack(void);
715 void __and_cond_states(void);
716 void __or_cond_states(void);
717 void __save_pre_cond_states(void);
718 void __discard_pre_cond_states(void);
719 struct stree
*__get_true_states(void);
720 struct stree
*__get_false_states(void);
721 void __use_cond_states(void);
722 extern struct state_list
*__last_base_slist
;
724 void __push_continues(void);
725 void __discard_continues(void);
726 void __process_continues(void);
727 void __merge_continues(void);
729 void __push_breaks(void);
730 void __process_breaks(void);
731 int __has_breaks(void);
732 void __merge_breaks(void);
733 void __use_breaks(void);
735 void __save_switch_states(struct expression
*switch_expr
);
736 void __discard_switches(void);
737 int have_remaining_cases(void);
738 void __merge_switches(struct expression
*switch_expr
, struct range_list
*case_rl
);
739 void __push_default(void);
740 void __set_default(void);
741 int __pop_default(void);
743 void __push_conditions(void);
744 void __discard_conditions(void);
746 void __save_gotos(const char *name
, struct symbol
*sym
);
747 void __merge_gotos(const char *name
, struct symbol
*sym
);
749 void __discard_fake_states(struct expression
*call
);
751 void __print_cur_stree(void);
752 bool __print_states(const char *owner
);
753 typedef void (check_tracker_hook
)(int owner
, const char *name
, struct symbol
*sym
, struct smatch_state
*state
);
754 void add_check_tracker(const char *check_name
, check_tracker_hook
*fn
);
757 void __pass_to_client(void *data
, enum hook_type type
);
758 void __pass_case_to_client(struct expression
*switch_expr
,
759 struct range_list
*rl
);
760 int __has_merge_function(int client_id
);
761 struct smatch_state
*__client_merge_function(int owner
,
762 struct smatch_state
*s1
,
763 struct smatch_state
*s2
);
764 struct smatch_state
*__client_unmatched_state_function(struct sm_state
*sm
);
765 void call_pre_merge_hook(struct sm_state
*cur
, struct sm_state
*other
);
766 void __push_scope_hooks(void);
767 void __call_scope_hooks(void);
769 /* smatch_function_hooks.c */
770 void create_function_hook_hash(void);
771 void __match_initializer_call(struct symbol
*sym
);
777 * Changing these numbers is a pain. Don't do it. If you ever use a
778 * number it can't be re-used right away so there may be gaps.
779 * We select these in order by type so if the order matters, then give
780 * it a number below 100-999,9000-9999 ranges. */
792 ABSOLUTE_LIMITS
= 1010,
806 UNTRACKED_PARAM
= 1023,
812 COMPARE_LIMIT
= 1028,
813 PARAM_COMPARE
= 1029,
816 CONSTRAINT_REQUIRED
= 1033,
826 /* put random temporary stuff in the 7000-7999 range for testing */
828 USER_DATA_SET
= 9017,
832 NO_OVERFLOW_SIMPLE
= 8019,
838 KNOWN_UNLOCKED
= 9025,
844 NO_SIDE_EFFECT
= 8025,
847 ARRAYSIZE_ARG
= 8033,
862 extern struct sqlite3
*smatch_db
;
863 extern struct sqlite3
*mem_db
;
864 extern struct sqlite3
*cache_db
;
866 bool db_incomplete(void);
867 void db_ignore_states(int id
);
868 void select_caller_info_hook(void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
), int type
);
869 void add_member_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
));
870 void add_caller_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
));
871 void add_return_info_callback(int owner
,
872 void (*callback
)(int return_id
, char *return_ranges
,
873 struct expression
*returned_expr
,
875 const char *printed_name
,
876 struct sm_state
*sm
));
877 void add_split_return_callback(void (*fn
)(int return_id
, char *return_ranges
, struct expression
*returned_expr
));
878 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
));
879 void select_call_implies_hook(int type
, void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
));
880 void select_return_implies_hook(int type
, void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
));
881 struct range_list
*db_return_vals(struct expression
*expr
);
882 struct range_list
*db_return_vals_from_str(const char *fn_name
);
883 struct range_list
*db_return_vals_no_args(struct expression
*expr
);
884 char *get_chunk_from_key(struct expression
*arg
, char *key
, struct symbol
**sym
, struct var_sym_list
**vsl
);
885 char *get_variable_from_key(struct expression
*arg
, const char *key
, struct symbol
**sym
);
886 char *get_name_sym_from_key(struct expression
*expr
, int param
, const char *key
, struct symbol
**sym
);
887 char *get_param_name_var_sym(const char *name
, struct symbol
*sym
);
888 char *get_param_name(struct sm_state
*sm
);
889 const char *get_mtag_name_var_sym(const char *state_name
, struct symbol
*sym
);
890 const char *get_mtag_name_expr(struct expression
*expr
);
891 char *get_data_info_name(struct expression
*expr
);
892 char *sm_to_arg_name(struct expression
*expr
, struct sm_state
*sm
);
893 int is_recursive_member(const char *param_name
);
895 char *escape_newlines(const char *str
);
896 void sql_exec(struct sqlite3
*db
, int (*callback
)(void*, int, char**, char**), void *data
, const char *sql
);
898 #define sql_helper(db, call_back, data, sql...) \
900 char sql_txt[1024]; \
902 sqlite3_snprintf(sizeof(sql_txt), sql_txt, sql); \
903 db_debug("debug: %s\n", sql_txt); \
904 sql_exec(db, call_back, data, sql_txt); \
908 #define run_sql(call_back, data, sql...) \
912 sql_helper(smatch_db, call_back, data, sql); \
915 #define mem_sql(call_back, data, sql...) \
916 sql_helper(mem_db, call_back, data, sql)
918 #define cache_sql(call_back, data, sql...) \
919 sql_helper(cache_db, call_back, data, sql)
921 #define sql_insert_helper(table, db, ignore, late, values...) \
923 struct sqlite3 *_db = db; \
925 if (__inline_fn && !_db) \
929 char *err, *p = buf; \
932 p += snprintf(p, buf + sizeof(buf) - p, \
933 "insert %sinto %s values (", \
934 ignore ? "or ignore " : "", #table); \
935 p += snprintf(p, buf + sizeof(buf) - p, values); \
936 p += snprintf(p, buf + sizeof(buf) - p, ");"); \
937 db_debug("mem-db: %s\n", buf); \
938 rc = sqlite3_exec(_db, buf, NULL, NULL, &err); \
939 if (rc != SQLITE_OK) { \
940 sm_ierror("SQL error #2: %s", err); \
941 sm_ierror("SQL: '%s'", buf); \
947 FILE *tmp_fd = sm_outfd; \
948 sm_outfd = sql_outfd; \
950 sm_printf("SQL%s: insert %sinto " #table " values(", \
951 late ? "_late" : "", ignore ? "or ignore " : ""); \
958 #define sql_insert(table, values...) sql_insert_helper(table, 0, 0, 0, values);
959 #define sql_insert_or_ignore(table, values...) sql_insert_helper(table, 0, 1, 0, values);
960 #define sql_insert_late(table, values...) sql_insert_helper(table, 0, 0, 1, values);
961 #define sql_insert_cache(table, values...) sql_insert_helper(table, cache_db, 1, 0, values);
963 char *get_static_filter(struct symbol
*sym
);
965 void sql_insert_return_states(int return_id
, const char *return_ranges
,
966 int type
, int param
, const char *key
, const char *value
);
967 void sql_insert_caller_info(struct expression
*call
, int type
, int param
,
968 const char *key
, const char *value
);
969 void sql_insert_function_ptr(const char *fn
, const char *struct_name
);
970 void sql_insert_return_values(const char *return_values
);
971 void sql_insert_return_implies(int type
, int param
, const char *key
, const char *value
);
972 void sql_insert_function_type_size(const char *member
, const char *ranges
);
973 void sql_insert_function_type_info(int type
, const char *struct_type
, const char *member
, const char *value
);
974 void sql_insert_type_info(int type
, const char *member
, const char *value
);
975 void sql_insert_local_values(const char *name
, const char *value
);
976 void sql_insert_function_type_value(const char *type
, const char *value
);
977 void sql_insert_function_type(int param
, const char *value
);
978 void sql_insert_parameter_name(int param
, const char *value
);
979 void sql_insert_data_info(struct expression
*data
, int type
, const char *value
);
980 void sql_insert_data_info_var_sym(const char *var
, struct symbol
*sym
, int type
, const char *value
);
981 void sql_save_constraint(const char *con
);
982 void sql_save_constraint_required(const char *data
, int op
, const char *limit
);
983 void sql_copy_constraint_required(const char *new_limit
, const char *old_limit
);
984 void sql_insert_fn_ptr_data_link(const char *ptr
, const char *data
);
985 void sql_insert_fn_data_link(struct expression
*fn
, int type
, int param
, const char *key
, const char *value
);
986 void sql_insert_mtag_about(mtag_t tag
, const char *left_name
, const char *right_name
);
987 void sql_insert_mtag_info(mtag_t tag
, int type
, const char *value
);
988 void sql_insert_mtag_map(mtag_t container
, int container_offset
, mtag_t tag
, int tag_offset
);
989 void sql_insert_mtag_alias(mtag_t orig
, mtag_t alias
);
990 int mtag_map_select_container(mtag_t tag
, int container_offset
, mtag_t
*container
);
991 int mtag_map_select_tag(mtag_t container
, int offset
, mtag_t
*tag
);
992 struct smatch_state
*get_mtag_return(struct expression
*expr
, struct smatch_state
*state
);
993 struct range_list
*swap_mtag_seed(struct expression
*expr
, struct range_list
*rl
);
995 void sql_select_return_states(const char *cols
, struct expression
*call
,
996 int (*callback
)(void*, int, char**, char**), void *info
);
997 void sql_select_call_implies(const char *cols
, struct expression
*call
,
998 int (*callback
)(void*, int, char**, char**));
1000 void open_smatch_db(char *db_file
);
1002 /* smatch_files.c */
1003 int open_data_file(const char *filename
);
1004 int open_schema_file(const char *schema
);
1005 struct token
*get_tokens_file(const char *filename
);
1006 struct string_list
*load_strings_from_file(const char *project
, const char *filename
);
1009 extern char *option_debug_check
;
1010 extern char *option_debug_var
;
1011 extern char *option_project_str
;
1012 extern char *bin_dir
;
1013 extern char *data_dir
;
1014 extern int option_no_data
;
1015 extern int option_full_path
;
1016 extern int option_call_tree
;
1017 extern int num_checks
;
1023 PROJ_ILLUMOS_KERNEL
,
1027 extern enum project_type option_project
;
1028 const char *check_name(unsigned short id
);
1029 int id_from_name(const char *name
);
1032 /* smatch_buf_size.c */
1033 int get_array_size(struct expression
*expr
);
1034 int get_array_size_bytes(struct expression
*expr
);
1035 int get_array_size_bytes_min(struct expression
*expr
);
1036 int get_array_size_bytes_max(struct expression
*expr
);
1037 struct range_list
*get_array_size_bytes_rl(struct expression
*expr
);
1038 int get_real_array_size(struct expression
*expr
);
1039 int last_member_is_resizable(struct symbol
*type
);
1040 /* smatch_strlen.c */
1041 int get_implied_strlen(struct expression
*expr
, struct range_list
**rl
);
1042 int get_size_from_strlen(struct expression
*expr
);
1044 /* smatch_capped.c */
1045 int is_capped(struct expression
*expr
);
1046 int is_capped_var_sym(const char *name
, struct symbol
*sym
);
1048 /* smatch_kernel_user_data.c */
1049 int is_user_macro(struct expression
*expr
);
1050 int is_capped_user_data(struct expression
*expr
);
1051 int implied_user_data(struct expression
*expr
, struct range_list
**rl
);
1052 struct stree
*get_user_stree(void);
1053 int get_user_rl(struct expression
*expr
, struct range_list
**rl
);
1054 int is_user_rl(struct expression
*expr
);
1055 int get_user_rl_var_sym(const char *name
, struct symbol
*sym
, struct range_list
**rl
);
1056 bool user_rl_capped(struct expression
*expr
);
1057 struct range_list
*var_user_rl(struct expression
*expr
);
1058 bool we_pass_user_data(struct expression
*call
);
1059 /* smatch_points_to_user_data.c */
1060 bool is_user_data_fn(struct symbol
*fn
);
1061 bool is_skb_data(struct expression
*expr
);
1062 bool points_to_user_data(struct expression
*expr
);
1063 void set_points_to_user_data(struct expression
*expr
);
1065 /* check_locking.c */
1066 void print_held_locks();
1068 /* check_assigned_expr.c */
1069 extern int check_assigned_expr_id
;
1070 struct expression
*get_assigned_expr(struct expression
*expr
);
1071 struct sm_state
*get_assigned_sm(struct expression
*expr
);
1072 struct expression
*get_assigned_expr_recurse(struct expression
*expr
);
1073 struct expression
*get_assigned_expr_name_sym(const char *name
, struct symbol
*sym
);
1074 struct expression
*get_assigned_expr_name_sym_recurse(const char *name
, struct symbol
*sym
);
1076 /* smatch_return_to_param.c */
1077 void __add_return_to_param_mapping(struct expression
*assign
, const char *return_string
);
1078 char *map_call_to_param_name_sym(struct expression
*expr
, struct symbol
**sym
);
1080 /* smatch_comparison.c */
1081 extern int comparison_id
;
1082 #define UNKNOWN_COMPARISON 0
1083 #define IMPOSSIBLE_COMPARISON -1
1084 struct compare_data
{
1085 /* The ->left and ->right expression pointers might be NULL (I'm lazy) */
1086 struct expression
*left
;
1087 const char *left_var
;
1088 struct var_sym_list
*left_vsl
;
1090 struct expression
*right
;
1091 const char *right_var
;
1092 struct var_sym_list
*right_vsl
;
1094 DECLARE_ALLOCATOR(compare_data
);
1095 struct smatch_state
*alloc_compare_state(
1096 struct expression
*left
,
1097 const char *left_var
, struct var_sym_list
*left_vsl
,
1099 struct expression
*right
,
1100 const char *right_var
, struct var_sym_list
*right_vsl
);
1101 int comparison_intersection(int orig
, int op
);
1102 int merge_comparisons(int one
, int two
);
1103 int combine_comparisons(int left_compare
, int right_compare
);
1104 int state_to_comparison(struct smatch_state
*state
);
1105 struct smatch_state
*merge_compare_states(struct smatch_state
*s1
, struct smatch_state
*s2
);
1106 int get_comparison(struct expression
*left
, struct expression
*right
);
1107 int get_comparison_no_extra(struct expression
*a
, struct expression
*b
);
1108 int get_comparison_strings(const char *one
, const char *two
);
1109 int possible_comparison(struct expression
*a
, int comparison
, struct expression
*b
);
1110 struct state_list
*get_all_comparisons(struct expression
*expr
);
1111 struct state_list
*get_all_possible_equal_comparisons(struct expression
*expr
);
1112 void __add_return_comparison(struct expression
*call
, const char *range
);
1113 void __add_comparison_info(struct expression
*expr
, struct expression
*call
, const char *range
);
1114 char *get_printed_param_name(struct expression
*call
, const char *param_name
, struct symbol
*param_sym
);
1115 char *name_sym_to_param_comparison(const char *name
, struct symbol
*sym
);
1116 char *expr_equal_to_param(struct expression
*expr
, int ignore
);
1117 char *expr_lte_to_param(struct expression
*expr
, int ignore
);
1118 char *expr_param_comparison(struct expression
*expr
, int ignore
);
1119 int flip_comparison(int op
);
1120 int negate_comparison(int op
);
1121 int remove_unsigned_from_comparison(int op
);
1122 int param_compare_limit_is_impossible(struct expression
*expr
, int left_param
, char *left_key
, char *value
);
1123 void filter_by_comparison(struct range_list
**rl
, int comparison
, struct range_list
*right
);
1124 void __compare_param_limit_hook(struct expression
*left_expr
, struct expression
*right_expr
,
1125 const char *state_name
,
1126 struct smatch_state
*true_state
, struct smatch_state
*false_state
);
1127 int impossibly_high_comparison(struct expression
*expr
);
1130 sval_t
*sval_alloc(sval_t sval
);
1131 sval_t
*sval_alloc_permanent(sval_t sval
);
1132 sval_t
sval_blank(struct expression
*expr
);
1133 sval_t
sval_type_val(struct symbol
*type
, long long val
);
1134 sval_t
sval_type_fval(struct symbol
*type
, long double fval
);
1135 sval_t
sval_from_val(struct expression
*expr
, long long val
);
1136 sval_t
sval_from_fval(struct expression
*expr
, long double fval
);
1137 int sval_is_ptr(sval_t sval
);
1138 bool sval_is_fp(sval_t sval
);
1139 int sval_unsigned(sval_t sval
);
1140 int sval_signed(sval_t sval
);
1141 int sval_bits(sval_t sval
);
1142 int sval_bits_used(sval_t sval
);
1143 int sval_is_negative(sval_t sval
);
1144 int sval_is_positive(sval_t sval
);
1145 int sval_is_min(sval_t sval
);
1146 int sval_is_max(sval_t sval
);
1147 int sval_is_a_min(sval_t sval
);
1148 int sval_is_a_max(sval_t sval
);
1149 int sval_is_negative_min(sval_t sval
);
1150 int sval_cmp_t(struct symbol
*type
, sval_t one
, sval_t two
);
1151 int sval_cmp_val(sval_t one
, long long val
);
1152 sval_t
sval_min(sval_t one
, sval_t two
);
1153 sval_t
sval_min_nonneg(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
);
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
);
1178 /* smatch_start_states.c */
1179 struct stree
*get_start_states(void);
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
);
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 /* smatch_comparison.c */
1197 struct sm_state
*comparison_implication_hook(struct expression
*expr
,
1198 struct state_list
**true_stack
,
1199 struct state_list
**false_stack
);
1201 /* check_string_len.c */
1202 int get_formatted_string_size(struct expression
*call
, int arg
);
1203 int get_formatted_string_min_size(struct expression
*call
, int arg
);
1205 /* smatch_param_set.c */
1206 int param_was_set(struct expression
*expr
);
1207 int param_was_set_var_sym(const char *name
, struct symbol
*sym
);
1208 void print_limited_param_set(int return_id
, char *return_ranges
, struct expression
*expr
);
1210 bool parent_was_PARAM_CLEAR(const char *name
, struct symbol
*sym
);
1211 bool parent_was_PARAM_CLEAR_ZERO(const char *name
, struct symbol
*sym
);
1213 /* smatch_param_filter.c */
1214 int param_has_filter_data(struct sm_state
*sm
);
1216 /* smatch_links.c */
1217 void set_up_link_functions(int id
, int linkid
);
1218 struct smatch_state
*merge_link_states(struct smatch_state
*s1
, struct smatch_state
*s2
);
1219 void store_link(int link_id
, const char *name
, struct symbol
*sym
, const char *link_name
, struct symbol
*link_sym
);
1221 /* check_buf_comparison */
1222 const char *limit_type_str(unsigned int limit_type
);
1223 struct expression
*get_size_variable(struct expression
*buf
, int *limit_type
);
1224 struct expression
*get_array_variable(struct expression
*size
);
1225 int buf_comparison_index_ok(struct expression
*expr
);
1227 /* smatch_untracked_param.c */
1228 void mark_untracked(struct expression
*expr
, int param
, const char *key
, const char *value
);
1229 void add_untracked_param_hook(void (func
)(struct expression
*call
, int param
));
1230 void add_lost_param_hook(void (func
)(struct expression
*call
, int param
));
1231 void mark_all_params_untracked(int return_id
, char *return_ranges
, struct expression
*expr
);
1233 /* smatch_strings.c */
1234 struct state_list
*get_strings(struct expression
*expr
);
1235 struct expression
*fake_string_from_mtag(mtag_t tag
);
1237 /* smatch_estate.c */
1238 int estate_get_single_value(struct smatch_state
*state
, sval_t
*sval
);
1240 /* smatch_address.c */
1241 int get_address_rl(struct expression
*expr
, struct range_list
**rl
);
1242 int get_member_offset(struct symbol
*type
, const char *member_name
);
1243 int get_member_offset_from_deref(struct expression
*expr
);
1245 /* for now this is in smatch_used_parameter.c */
1246 void __get_state_hook(int owner
, const char *name
, struct symbol
*sym
);
1247 extern int __ignore_param_used
;
1249 /* smatch_buf_comparison.c */
1250 int db_var_is_array_limit(struct expression
*array
, const char *name
, struct var_sym_list
*vsl
);
1252 struct range_list
*get_fs(void);
1254 struct stree
*get_all_return_states(void);
1255 struct stree_stack
*get_all_return_strees(void);
1256 int on_atomic_dec_path(void);
1257 int was_inced(const char *name
, struct symbol
*sym
);
1258 void set_refcount_inc(char *name
, struct symbol
*sym
);
1259 void set_refcount_dec(char *name
, struct symbol
*sym
);
1261 /* smatch_constraints.c */
1262 char *get_constraint_str(struct expression
*expr
);
1263 struct constraint_list
*get_constraints(struct expression
*expr
);
1264 char *unmet_constraint(struct expression
*data
, struct expression
*offset
);
1265 char *get_required_constraint(const char *data_str
);
1267 /* smatch_container_of.c */
1268 int get_param_from_container_of(struct expression
*expr
);
1269 int get_offset_from_container_of(struct expression
*expr
);
1270 char *get_container_name(struct expression
*container
, struct expression
*expr
);
1273 mtag_t
str_to_mtag(const char *str
);
1274 int get_string_mtag(struct expression
*expr
, mtag_t
*tag
);
1275 int get_toplevel_mtag(struct symbol
*sym
, mtag_t
*tag
);
1276 int create_mtag_alias(mtag_t tag
, struct expression
*expr
, mtag_t
*new);
1277 int expr_to_mtag_offset(struct expression
*expr
, mtag_t
*tag
, int *offset
);
1278 void update_mtag_data(struct expression
*expr
, struct smatch_state
*state
);
1279 int get_mtag_sval(struct expression
*expr
, sval_t
*sval
);
1281 /* Trinity fuzzer stuff */
1282 const char *get_syscall_arg_type(struct symbol
*sym
);
1284 /* smatch_bit_info.c */
1285 struct bit_info
*rl_to_binfo(struct range_list
*rl
);
1286 struct bit_info
*get_bit_info(struct expression
*expr
);
1287 struct bit_info
*get_bit_info_var_sym(const char *name
, struct symbol
*sym
);
1288 /* smatch_mem_tracker.c */
1289 extern int option_mem
;
1290 unsigned long get_mem_kb(void);
1291 unsigned long get_max_memory(void);
1293 /* check_is_nospec.c */
1294 bool is_nospec(struct expression
*expr
);
1295 long get_stmt_cnt(void);
1297 /* smatch_nul_terminator.c */
1298 bool is_nul_terminated_var_sym(const char *name
, struct symbol
*sym
);
1299 bool is_nul_terminated(struct expression
*expr
);
1300 /* check_kernel.c */
1301 bool is_ignored_kernel_data(const char *name
);
1303 bool is_fresh_alloc_var_sym(const char *var
, struct symbol
*sym
);
1304 bool is_fresh_alloc(struct expression
*expr
);
1305 bool is_freed_var_sym(const char *name
, struct symbol
*sym
);
1306 void track_freed_param(struct expression
*expr
, struct smatch_state
*state
);
1307 void track_freed_param_var_sym(const char *name
, struct symbol
*sym
,
1308 struct smatch_state
*state
);
1310 static inline bool type_is_ptr(struct symbol
*type
)
1313 (type
->type
== SYM_PTR
||
1314 type
->type
== SYM_ARRAY
||
1315 type
->type
== SYM_FN
);
1318 static inline bool type_is_fp(struct symbol
*type
)
1321 (type
== &float_ctype
||
1322 type
== &double_ctype
||
1323 type
== &ldouble_ctype
);
1326 static inline int type_bits(struct symbol
*type
)
1330 if (type_is_ptr(type
))
1331 return bits_in_pointer
;
1332 if (!type
->examined
)
1333 examine_symbol_type(type
);
1334 return type
->bit_size
;
1337 static inline int type_unsigned(struct symbol
*base_type
)
1341 if (is_ptr_type(base_type
))
1343 if (base_type
->ctype
.modifiers
& MOD_UNSIGNED
)
1348 static inline int type_positive_bits(struct symbol
*type
)
1352 if (is_ptr_type(type
))
1353 return bits_in_pointer
;
1354 if (type_unsigned(type
))
1355 return type_bits(type
);
1356 return type_bits(type
) - 1;
1359 static inline int sval_positive_bits(sval_t sval
)
1361 return type_positive_bits(sval
.type
);
1365 * Returns -1 if one is smaller, 0 if they are the same and 1 if two is larger.
1368 static inline int fp_cmp(sval_t one
, sval_t two
)
1370 struct symbol
*type
;
1372 if (sval_is_fp(one
) && sval_is_fp(two
))
1373 type
= type_bits(one
.type
) > type_bits(two
.type
) ? one
.type
: two
.type
;
1374 else if (sval_is_fp(one
))
1379 one
= sval_cast(type
, one
);
1380 two
= sval_cast(type
, two
);
1382 if (one
.type
== &float_ctype
) {
1383 if (one
.fvalue
< two
.fvalue
)
1385 if (one
.fvalue
== two
.fvalue
)
1389 if (one
.type
== &double_ctype
) {
1390 if (one
.dvalue
< two
.dvalue
)
1392 if (one
.dvalue
== two
.dvalue
)
1396 if (one
.type
== &ldouble_ctype
) {
1397 if (one
.ldvalue
< two
.ldvalue
)
1399 if (one
.ldvalue
== two
.ldvalue
)
1403 sm_perror("bad type in fp_cmp(): %s", type_to_str(type
));
1407 static inline int sval_cmp(sval_t one
, sval_t two
)
1409 struct symbol
*type
;
1411 if (sval_is_fp(one
) || sval_is_fp(two
))
1412 return fp_cmp(one
, two
);
1415 if (sval_positive_bits(two
) > sval_positive_bits(one
))
1417 if (type_bits(type
) < 31)
1420 one
= sval_cast(type
, one
);
1421 two
= sval_cast(type
, two
);
1423 if (type_unsigned(type
)) {
1424 if (one
.uvalue
< two
.uvalue
)
1426 if (one
.uvalue
== two
.uvalue
)
1430 /* fix me handle type promotion and unsigned values */
1431 if (one
.value
< two
.value
)
1433 if (one
.value
== two
.value
)
1438 #endif /* !SMATCH_H_ */