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 #define GLOBAL_STATE(_x) struct smatch_state _x = { .name = #_x }
43 extern struct smatch_state undefined
;
44 extern struct smatch_state merged
;
45 extern struct smatch_state true_state
;
46 extern struct smatch_state false_state
;
47 extern struct smatch_state unit_bit
;
48 extern struct smatch_state unit_byte
;
49 extern struct smatch_state unit_array_size
;
50 extern struct smatch_state unit_long
;
51 extern struct smatch_state unit_page
;
52 extern struct smatch_state unit_msec
;
53 extern struct smatch_state unit_ns
;
54 extern struct smatch_state unit_jiffy
;
55 DECLARE_ALLOCATOR(smatch_state
);
57 static inline void *INT_PTR(int i
)
59 return (void *)(long)i
;
62 static inline int PTR_INT(void *p
)
72 DECLARE_ALLOCATOR(tracker
);
73 DECLARE_PTR_LIST(tracker_list
, struct tracker
);
74 DECLARE_PTR_LIST(stree_stack
, struct stree
);
76 /* The first 3 struct members must match struct tracker */
81 unsigned short merged
:1;
82 unsigned short leaf
:1;
84 struct smatch_state
*state
;
86 struct sm_state
*left
;
87 struct sm_state
*right
;
88 struct state_list
*possible
;
95 DECLARE_ALLOCATOR(var_sym
);
96 DECLARE_PTR_LIST(var_sym_list
, struct var_sym
);
102 DECLARE_PTR_LIST(constraint_list
, struct constraint
);
108 extern struct alloc_info
*alloc_funcs
;
111 unsigned long long set
;
112 unsigned long long possible
;
123 DECLARATION_HOOK_AFTER
,
125 ASSIGNMENT_HOOK_AFTER
,
127 GLOBAL_ASSIGNMENT_HOOK
,
132 AFTER_LOOP_NO_BREAKS
,
134 WHOLE_CONDITION_HOOK
,
135 FUNCTION_CALL_HOOK_BEFORE
,
137 CALL_HOOK_AFTER_INLINE
,
138 FUNCTION_CALL_HOOK_AFTER_DB
,
139 CALL_ASSIGNMENT_HOOK
,
140 MACRO_ASSIGNMENT_HOOK
,
143 DEREF_HOOK
, /* DEREF_HOOK is junk. Better to use add_dereference_hook() */
165 typedef void (void_fn
)(void);
166 typedef void (expr_func
)(struct expression
*expr
);
167 typedef void (stmt_func
)(struct statement
*stmt
);
168 typedef void (sym_func
)(struct symbol
*sym
);
169 typedef void (name_sym_hook
)(struct expression
*expr
, const char *name
, struct symbol
*sym
);
170 typedef void (sm_hook
)(struct sm_state
*sm
, struct expression
*mod_expr
);
171 typedef void (string_hook
)(struct expression
*expr
, const char *str
);
172 DECLARE_PTR_LIST(void_fn_list
, void_fn
);
173 DECLARE_PTR_LIST(expr_fn_list
, expr_func
);
174 DECLARE_PTR_LIST(stmt_fn_list
, stmt_func
);
175 DECLARE_PTR_LIST(sym_fn_list
, sym_func
);
176 DECLARE_PTR_LIST(name_sym_fn_list
, name_sym_hook
);
177 DECLARE_PTR_LIST(string_hook_list
, string_hook
);
178 void call_void_fns(struct void_fn_list
*list
);
179 void call_expr_fns(struct expr_fn_list
*list
, struct expression
*expr
);
180 void call_stmt_fns(struct stmt_fn_list
*list
, struct statement
*stmt
);
181 void call_sym_fns(struct sym_fn_list
*list
, struct symbol
*sym
);
182 void call_name_sym_fns(struct name_sym_fn_list
*list
, struct expression
*expr
, const char *name
, struct symbol
*sym
);
183 void call_string_hooks(struct string_hook_list
*list
, struct expression
*expr
, const char *str
);
185 void add_dereference_hook(expr_func
*fn
);
187 struct allocation_info
{
189 const char *size_str
;
190 struct expression
*total_size
;
191 struct expression
*nr_elems
;
192 struct expression
*elem_size
;
195 bool safe
; /* safe from overflows */
197 typedef void (alloc_hook
)(struct expression
*expr
, const char *name
, struct symbol
*sym
, struct allocation_info
*info
);
198 void add_allocation_hook(alloc_hook
*func
);
200 void add_hook(void *func
, enum hook_type type
);
201 typedef struct smatch_state
*(merge_func_t
)(struct smatch_state
*s1
, struct smatch_state
*s2
);
202 typedef struct smatch_state
*(unmatched_func_t
)(struct sm_state
*state
);
203 void add_merge_hook(int client_id
, merge_func_t
*func
);
204 void add_unmatched_state_hook(int client_id
, unmatched_func_t
*func
);
205 void add_pre_merge_hook(int client_id
, void (*hook
)(struct sm_state
*cur
, struct sm_state
*other
));
206 typedef void (scope_hook
)(void *data
);
207 void add_scope_hook(scope_hook
*hook
, void *data
);
208 void add_return_string_hook(string_hook
*fn
);
209 typedef void (param_key_hook
)(struct expression
*expr
, const char *name
, struct symbol
*sym
, void *data
);
210 typedef void (func_hook
)(const char *fn
, struct expression
*expr
, void *data
);
211 typedef void (implication_hook
)(const char *fn
, struct expression
*call_expr
,
212 struct expression
*assign_expr
, void *data
);
213 typedef void (return_implies_hook
)(struct expression
*call_expr
,
214 int param
, char *key
, char *value
);
215 typedef int (implied_return_hook
)(struct expression
*call_expr
, void *info
, struct range_list
**rl
);
216 void add_function_hook_early(const char *look_for
, func_hook
*call_back
, void *data
);
217 void add_function_hook(const char *look_for
, func_hook
*call_back
, void *data
);
218 void add_function_hook_late(const char *look_for
, func_hook
*call_back
, void *info
);
220 void add_function_assign_hook(const char *look_for
, func_hook
*call_back
,
222 void register_func_hooks_from_file(const char *file
,
223 func_hook
*call_back
, void *info
);
224 void register_assign_hooks_from_file(const char *file
,
225 func_hook
*call_back
, void *info
);
226 void add_implied_return_hook(const char *look_for
,
227 implied_return_hook
*call_back
,
229 void add_macro_assign_hook(const char *look_for
, func_hook
*call_back
,
231 void add_macro_assign_hook_extra(const char *look_for
, func_hook
*call_back
,
233 void return_implies_state(const char *look_for
, long long start
, long long end
,
234 implication_hook
*call_back
, void *info
);
235 void return_implies_state_sval(const char *look_for
, sval_t start
, sval_t end
,
236 implication_hook
*call_back
, void *info
);
237 void return_implies_exact(const char *look_for
, sval_t start
, sval_t end
,
238 implication_hook
*call_back
, void *info
);
239 struct range_list
*get_range_implications(const char *fn
);
240 void select_return_states_hook(int type
, return_implies_hook
*callback
);
241 void select_return_states_before(void (*fn
)(void));
242 void select_return_states_after(void (*fn
)(void));
243 void add_function_param_key_hook(const char *look_for
, param_key_hook
*call_back
,
244 int param
, const char *key
, void *info
);
245 void add_function_param_key_hook_early(const char *look_for
, param_key_hook
*call_back
,
246 int param
, const char *key
, void *info
);
247 void add_function_param_key_hook_late(const char *look_for
, param_key_hook
*call_back
,
248 int param
, const char *key
, void *info
);
249 void return_implies_param_key(const char *look_for
, sval_t start
, sval_t end
,
250 param_key_hook
*call_back
,
251 int param
, const char *key
, void *info
);
252 void return_implies_param_key_exact(const char *look_for
, sval_t start
, sval_t end
,
253 param_key_hook
*call_back
,
254 int param
, const char *key
, void *info
);
255 void select_return_param_key(int type
, param_key_hook
*callback
);
256 bool get_implied_return(struct expression
*expr
, struct range_list
**rl
);
257 void allocate_hook_memory(void);
258 void allocate_tracker_array(int num_checks
);
260 struct modification_data
{
261 struct smatch_state
*prev
;
262 struct expression
*cur
;
265 void allocate_modification_hooks(void);
266 bool is_sub_member(const char *name
, struct symbol
*sym
, struct sm_state
*sm
);
267 void add_all_modifications_hook(int owner
, name_sym_hook
*hook
);
268 void add_modification_hook(int owner
, sm_hook
*call_back
);
269 void add_modification_hook_late(int owner
, sm_hook
*call_back
);
270 struct smatch_state
*get_modification_state(struct expression
*expr
);
272 int outside_of_function(void);
273 const char *get_filename(void);
274 extern int base_file_stream
;
275 const char *get_base_file(void);
276 unsigned long long get_file_id(void);
277 unsigned long long get_base_file_id(void);
278 char *get_function(void);
279 extern int __smatch_lineno
;
280 int get_lineno(void);
281 extern int final_pass
;
282 extern struct symbol
*cur_func_sym
;
283 extern int option_debug
;
284 extern int local_debug
;
286 extern bool implied_debug
;
287 bool debug_implied(void);
288 bool debug_on(const char *check_name
, const char *var
);
289 extern int option_info
;
290 extern int option_spammy
;
291 extern int option_pedantic
;
292 extern int option_print_names
;
293 extern char *trace_variable
;
294 extern struct stree
*global_states
;
295 void set_function_skipped(void);
296 int is_skipped_function(void);
297 int is_silenced_function(void);
298 extern bool implications_off
;
300 /* smatch_impossible.c */
301 int is_impossible_path(void);
302 void set_true_path_impossible(void);
303 void set_false_path_impossible(void);
304 void set_path_impossible(void);
306 extern FILE *sm_outfd
;
307 extern FILE *sql_outfd
;
308 extern FILE *caller_info_fd
;
309 extern int sm_nr_checks
;
310 extern int sm_nr_errors
;
313 * How to use these routines:
315 * sm_fatal(): an internal error of some kind that should immediately exit
316 * sm_ierror(): an internal error
317 * sm_perror(): an internal error from parsing input source
318 * sm_error(): an error from input source
319 * sm_warning(): a warning from input source
320 * sm_info(): info message (from option_info)
321 * sm_debug(): debug message
322 * sm_msg(): other message (please avoid using this)
325 #define sm_printf(msg...) do { \
326 if (final_pass || option_debug || local_debug || debug_db) \
327 fprintf(sm_outfd, msg); \
330 static inline void sm_prefix(void)
332 sm_printf("%s:%d %s() ", get_filename(), get_lineno(), get_function());
333 if (option_info
|| !option_print_names
)
335 sm_printf("[smatch.%s] ", __CHECKNAME__
);
338 static inline void print_implied_debug_msg();
340 extern bool __silence_warnings_for_stmt
;
342 #define sm_print_msg(type, msg...) \
344 print_implied_debug_msg(); \
345 if (!final_pass && !option_debug && !local_debug && !debug_db) \
347 if (__silence_warnings_for_stmt && !option_debug && !local_debug) \
349 if (!option_info && is_silenced_function()) \
353 sm_printf("warn: "); \
355 } else if (type == 2) { \
356 sm_printf("error: "); \
358 } else if (type == 3) { \
359 sm_printf("parse error: "); \
361 } else if (type == 4) { \
362 sm_printf("pedantic: "); \
368 #define sm_msg(msg...) do { sm_print_msg(0, msg); } while (0)
370 extern char *implied_debug_msg
;
371 static inline void print_implied_debug_msg(void)
373 static struct symbol
*last_printed
= NULL
;
375 if (!implied_debug_msg
)
377 if (last_printed
== cur_func_sym
)
379 last_printed
= cur_func_sym
;
380 sm_msg("%s", implied_debug_msg
);
383 #define sm_debug(msg...) do { if (option_debug) sm_printf(msg); } while (0)
384 #define db_debug(msg...) do { if (option_debug || debug_db) sm_printf(msg); } while (0)
386 #define sm_info(msg...) do { \
387 if (option_debug || (option_info && final_pass)) { \
389 sm_printf("info: "); \
395 #define sm_warning(msg...) do { sm_print_msg(1, msg); } while (0)
396 #define sm_warning_line(line, msg...) do { \
397 int __orig = __smatch_lineno; \
398 __smatch_lineno = line; \
399 sm_print_msg(1, msg); \
400 __smatch_lineno = __orig; \
402 #define sm_error(msg...) do { sm_print_msg(2, msg); } while (0)
403 #define sm_perror(msg...) do { sm_print_msg(3, msg); } while (0)
404 #define sm_pedantic(msg...) do { if (option_pedantic) sm_print_msg(4, msg); } while (0)
406 static inline void sm_fatal(const char *fmt
, ...)
411 vfprintf(sm_outfd
, fmt
, args
);
414 fprintf(sm_outfd
, "\n");
419 static inline void sm_ierror(const char *fmt
, ...)
425 fprintf(sm_outfd
, "internal error: ");
428 vfprintf(sm_outfd
, fmt
, args
);
431 fprintf(sm_outfd
, "\n");
433 #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
435 bool has_states(struct stree
*stree
, int owner
);
436 struct smatch_state
*__get_state(int owner
, const char *name
, struct symbol
*sym
);
437 struct smatch_state
*get_state(int owner
, const char *name
, struct symbol
*sym
);
438 struct smatch_state
*get_state_expr(int owner
, struct expression
*expr
);
439 bool has_possible_state(int owner
, const char *name
, struct symbol
*sym
, struct smatch_state
*state
);
440 bool expr_has_possible_state(int owner
, struct expression
*expr
, struct smatch_state
*state
);
441 struct state_list
*get_possible_states(int owner
, const char *name
,
443 struct state_list
*get_possible_states_expr(int owner
, struct expression
*expr
);
444 struct sm_state
*set_state(int owner
, const char *name
, struct symbol
*sym
,
445 struct smatch_state
*state
);
446 struct sm_state
*set_state_expr(int owner
, struct expression
*expr
,
447 struct smatch_state
*state
);
448 void __delete_state(int owner
, const char *name
, struct symbol
*sym
);
449 void __delete_all_states_sym(struct symbol
*sym
);
450 void set_true_false_states(int owner
, const char *name
, struct symbol
*sym
,
451 struct smatch_state
*true_state
,
452 struct smatch_state
*false_state
);
453 void set_true_false_states_expr(int owner
, struct expression
*expr
,
454 struct smatch_state
*true_state
,
455 struct smatch_state
*false_state
);
457 struct stree
*get_all_states_from_stree(int owner
, struct stree
*source
);
458 struct stree
*get_all_states_stree(int id
);
459 struct stree
*__get_cur_stree(void);
460 int is_reachable(void);
461 void add_get_state_hook(void (*fn
)(int owner
, const char *name
, struct symbol
*sym
));
464 char *ssa_name(const char *name
);
465 void set_ssa_state(int owner
, const char *name
, struct symbol
*sym
, struct smatch_state
*state
);
466 void update_ssa_state(int owner
, const char *name
, struct symbol
*sym
,
467 struct smatch_state
*state
);
468 void update_ssa_sm(int owner
, struct sm_state
*sm
, struct smatch_state
*state
);
469 void set_ssa_state_expr(int owner
, struct expression
*expr
, struct smatch_state
*state
);
470 struct sm_state
*get_ssa_sm_state(int owner
, const char *name
, struct symbol
*sym
);
471 struct sm_state
*get_ssa_sm_state_expr(int owner
, struct expression
*expr
);
472 struct smatch_state
*get_ssa_state(int owner
, const char *name
, struct symbol
*sym
);
473 struct smatch_state
*get_ssa_state_expr(int owner
, struct expression
*expr
);
475 /* smatch_helper.c */
476 DECLARE_PTR_LIST(int_stack
, int);
477 char *alloc_string(const char *str
);
478 char *alloc_string_newline(const char *str
);
479 void free_string(char *str
);
480 void remove_parens(char *str
);
481 struct smatch_state
*alloc_state_num(int num
);
482 struct smatch_state
*alloc_state_str(const char *name
);
483 struct smatch_state
*merge_str_state(struct smatch_state
*s1
, struct smatch_state
*s2
);
484 struct smatch_state
*alloc_state_expr(struct expression
*expr
);
485 struct expression
*get_assigned_call(struct expression
*expr
);
486 struct expression
*get_argument_from_call_expr(struct expression_list
*args
,
488 struct expression
*get_array_expr(struct expression
*expr
);
490 char *expr_to_var(struct expression
*expr
);
491 struct symbol
*expr_to_sym(struct expression
*expr
);
492 char *expr_to_str(struct expression
*expr
);
493 char *expr_to_str_sym(struct expression
*expr
,
494 struct symbol
**sym_ptr
);
495 char *expr_to_var_sym(struct expression
*expr
,
496 struct symbol
**sym_ptr
);
497 char *expr_to_known_chunk_sym(struct expression
*expr
, struct symbol
**sym
);
498 char *expr_to_chunk_sym_vsl(struct expression
*expr
, struct symbol
**sym
, struct var_sym_list
**vsl
);
499 int get_complication_score(struct expression
*expr
);
501 int sym_name_is(const char *name
, struct expression
*expr
);
502 int get_const_value(struct expression
*expr
, sval_t
*sval
);
503 int get_value(struct expression
*expr
, sval_t
*val
);
504 int get_implied_value(struct expression
*expr
, sval_t
*val
);
505 int get_implied_value_fast(struct expression
*expr
, sval_t
*sval
);
506 int get_implied_min(struct expression
*expr
, sval_t
*sval
);
507 int get_implied_max(struct expression
*expr
, sval_t
*val
);
508 int get_hard_max(struct expression
*expr
, sval_t
*sval
);
509 int get_fuzzy_min(struct expression
*expr
, sval_t
*min
);
510 int get_fuzzy_max(struct expression
*expr
, sval_t
*max
);
511 int get_absolute_min(struct expression
*expr
, sval_t
*sval
);
512 int get_absolute_max(struct expression
*expr
, sval_t
*sval
);
513 int parse_call_math(struct expression
*expr
, char *math
, sval_t
*val
);
514 int parse_call_math_rl(struct expression
*call
, const char *math
, struct range_list
**rl
);
515 const char *get_allocation_math(struct expression
*expr
);
516 char *get_value_in_terms_of_parameter_math(struct expression
*expr
);
517 char *get_value_in_terms_of_parameter_math_var_sym(const char *var
, struct symbol
*sym
);
518 int expr_is_zero(struct expression
*expr
);
519 int known_condition_true(struct expression
*expr
);
520 int known_condition_false(struct expression
*expr
);
521 int implied_condition_true(struct expression
*expr
);
522 int implied_condition_false(struct expression
*expr
);
523 int can_integer_overflow(struct symbol
*type
, struct expression
*expr
);
524 void clear_math_cache(void);
525 void clear_strip_cache(void);
526 void set_fast_math_only(void);
527 void clear_fast_math_only(void);
529 int is_array(struct expression
*expr
);
530 struct expression
*get_array_base(struct expression
*expr
);
531 struct expression
*get_array_offset(struct expression
*expr
);
532 const char *show_state(struct smatch_state
*state
);
533 struct statement
*get_expression_statement(struct expression
*expr
);
534 struct expression
*strip__builtin_choose_expr(struct expression
*expr
);
535 struct expression
*strip_Generic(struct expression
*expr
);
536 struct expression
*strip_parens(struct expression
*expr
);
537 struct expression
*strip_expr(struct expression
*expr
);
538 struct expression
*strip_no_cast(struct expression
*expr
);
539 struct expression
*strip_expr_set_parent(struct expression
*expr
);
540 void scoped_state(int my_id
, const char *name
, struct symbol
*sym
);
541 int is_error_return(struct expression
*expr
);
542 int getting_address(struct expression
*expr
);
543 int get_struct_and_member(struct expression
*expr
, const char **type
, const char **member
);
544 char *get_member_name(struct expression
*expr
);
545 char *get_fnptr_name(struct expression
*expr
);
546 int cmp_pos(struct position pos1
, struct position pos2
);
547 int positions_eq(struct position pos1
, struct position pos2
);
548 struct statement
*get_current_statement(void);
549 struct statement
*get_prev_statement(void);
550 struct expression
*get_last_expr_from_expression_stmt(struct expression
*expr
);
552 enum { RET_SUCCESS
, RET_FAIL
, RET_UNKNOWN
};
553 int success_fail_return(struct range_list
*rl
);
555 #define RETURN_VAR -1
556 #define LOCAL_SCOPE -2
557 #define FILE_SCOPE -3
558 #define GLOBAL_SCOPE -4
559 #define UNKNOWN_SCOPE -5
561 char *swap_names(const char *orig
, const char *remove
, const char *add
);
562 char *get_param_var_sym_var_sym(const char *name
, struct symbol
*sym
, struct expression
*ret_expr
, struct symbol
**sym_p
);
563 char *get_param_name_sym(struct expression
*expr
, struct symbol
**sym_p
);
564 int get_return_param_key_from_var_sym(const char *name
, struct symbol
*sym
,
565 struct expression
*ret_expr
,
567 int get_param_key_from_var_sym(const char *name
, struct symbol
*sym
,
568 struct expression
*ret_expr
,
570 int get_param_key_from_sm(struct sm_state
*sm
, struct expression
*ret_expr
,
572 int get_param_key_from_expr(struct expression
*expr
, struct expression
*ret_expr
,
574 const char *get_param_key_swap_dollar(struct expression
*expr
);
575 int map_to_param(const char *name
, struct symbol
*sym
);
576 int get_param_num_from_sym(struct symbol
*sym
);
577 int get_param_num(struct expression
*expr
);
578 struct symbol
*get_param_sym_from_num(int num
);
579 struct expression
*map_container_of_to_simpler_expr_key(struct expression
*expr
, const char *orig_key
, char **new_key
);
580 const char *get_container_of_str(struct expression
*expr
);
581 bool get_offset_param(const char *ret_str
, int *offset
, int *param
);
582 /* smatch_points_to_container.c */
583 struct expression
*get_stored_container(struct expression
*expr
, int offset
);
585 int ms_since(struct timeval
*start
);
586 int parent_is_gone_var_sym(const char *name
, struct symbol
*sym
);
587 int parent_is_gone(struct expression
*expr
);
588 bool is_noderef_ptr(struct expression
*expr
);
589 int invert_op(int op
);
590 int op_remove_assign(int op
);
591 int expr_equiv(struct expression
*one
, struct expression
*two
);
592 void push_int(struct int_stack
**stack
, int num
);
593 int pop_int(struct int_stack
**stack
);
594 bool macro_to_ul(const char *macro
, unsigned long *val
);
597 struct symbol
*get_real_base_type(struct symbol
*sym
);
598 int type_bytes(struct symbol
*type
);
599 int array_bytes(struct symbol
*type
);
600 struct symbol
*get_pointer_type(struct expression
*expr
);
601 struct symbol
*get_type(struct expression
*expr
);
602 struct symbol
*get_comparison_type(struct expression
*expr
);
603 struct symbol
*get_final_type(struct expression
*expr
);
604 struct symbol
*get_promoted_type(struct symbol
*left
, struct symbol
*right
);
605 int type_signed(struct symbol
*base_type
);
606 int expr_unsigned(struct expression
*expr
);
607 int expr_signed(struct expression
*expr
);
608 int returns_unsigned(struct symbol
*base_type
);
609 int is_pointer(struct expression
*expr
);
610 bool is_void_ptr(struct symbol
*type
);
611 int returns_pointer(struct symbol
*base_type
);
612 sval_t
sval_type_max(struct symbol
*base_type
);
613 sval_t
sval_type_min(struct symbol
*base_type
);
614 int nr_bits(struct expression
*expr
);
615 int is_void_pointer(struct expression
*expr
);
616 int is_char_pointer(struct expression
*expr
);
617 int is_string(struct expression
*expr
);
618 bool is_struct_ptr(struct symbol
*type
);
619 int is_static(struct expression
*expr
);
620 bool is_local_variable(struct expression
*expr
);
621 int types_equiv(struct symbol
*one
, struct symbol
*two
);
622 bool type_fits(struct symbol
*type
, struct symbol
*test
);
624 const char *global_static();
625 struct symbol
*cur_func_return_type(void);
626 struct symbol
*get_arg_type(struct expression
*fn
, int arg
);
627 struct symbol
*get_member_type_from_key(struct expression
*expr
, const char *key
);
628 struct symbol
*get_arg_type_from_key(struct expression
*fn
, int param
, struct expression
*arg
, const char *key
);
629 int is_struct(struct expression
*expr
);
630 char *type_to_str(struct symbol
*type
);
632 /* smatch_ignore.c */
633 void add_ignore(int owner
, const char *name
, struct symbol
*sym
);
634 int is_ignored(int owner
, const char *name
, struct symbol
*sym
);
635 void add_ignore_expr(int owner
, struct expression
*expr
);
636 int is_ignored_expr(int owner
, struct expression
*expr
);
639 struct smatch_state
*alloc_var_sym_state(const char *var
, struct symbol
*sym
);
640 struct var_sym
*alloc_var_sym(const char *var
, struct symbol
*sym
);
641 struct var_sym_list
*expr_to_vsl(struct expression
*expr
);
642 void add_var_sym(struct var_sym_list
**list
, const char *var
, struct symbol
*sym
);
643 void add_var_sym_expr(struct var_sym_list
**list
, struct expression
*expr
);
644 void del_var_sym(struct var_sym_list
**list
, const char *var
, struct symbol
*sym
);
645 int in_var_sym_list(struct var_sym_list
*list
, const char *var
, struct symbol
*sym
);
646 struct var_sym_list
*clone_var_sym_list(struct var_sym_list
*from_vsl
);
647 void merge_var_sym_list(struct var_sym_list
**dest
, struct var_sym_list
*src
);
648 struct var_sym_list
*combine_var_sym_lists(struct var_sym_list
*one
, struct var_sym_list
*two
);
649 int var_sym_lists_equiv(struct var_sym_list
*one
, struct var_sym_list
*two
);
650 void free_var_sym_list(struct var_sym_list
**list
);
651 void free_var_syms_and_list(struct var_sym_list
**list
);
654 struct tracker
*alloc_tracker(int owner
, const char *name
, struct symbol
*sym
);
655 void add_tracker(struct tracker_list
**list
, int owner
, const char *name
,
657 void add_tracker_expr(struct tracker_list
**list
, int owner
, struct expression
*expr
);
658 void del_tracker(struct tracker_list
**list
, int owner
, const char *name
,
660 int in_tracker_list(struct tracker_list
*list
, int owner
, const char *name
,
662 void free_tracker_list(struct tracker_list
**list
);
663 void free_trackers_and_list(struct tracker_list
**list
);
665 /* smatch_conditions */
666 int in_condition(void);
670 extern int __in_fake_assign
;
671 extern int __in_fake_parameter_assign
;
672 extern int __in_fake_struct_assign
;
673 extern int __in_buf_clear
;
674 extern int __in_fake_var_assign
;
675 extern int __in_builtin_overflow_func
;
676 extern int __fake_state_cnt
;
677 extern int __debug_skip
;
678 extern int in_fake_env
;
679 bool is_fake_var_assign(struct expression
*expr
);
680 void smatch (struct string_list
*filelist
);
681 int inside_loop(void);
682 int definitely_inside_loop(void);
683 struct expression
*get_switch_expr(void);
684 int in_expression_statement(void);
685 void __process_post_op_stack(void);
686 void __split_expr(struct expression
*expr
);
687 void __split_label_stmt(struct statement
*stmt
);
688 void __split_stmt(struct statement
*stmt
);
689 extern int __in_function_def
;
690 extern int __in_unmatched_hook
;
691 extern int option_assume_loops
;
692 extern int option_two_passes
;
693 extern int option_no_db
;
694 extern int option_file_output
;
695 extern int option_time
;
696 extern int option_time_stmt
;
697 extern struct expression_list
*big_expression_stack
;
698 extern struct expression_list
*big_condition_stack
;
699 extern struct statement_list
*big_statement_stack
;
700 int is_condition_call(struct expression
*expr
);
701 int is_assigned_call(struct expression
*expr
);
702 int is_fake_assigned_call(struct expression
*expr
);
703 void add_function_data(unsigned long *fn_data
);
704 int inlinable(struct expression
*expr
);
705 extern int __inline_call
;
706 extern struct expression
*__inline_fn
;
707 extern int __in_pre_condition
;
708 extern int __bail_on_rest_of_function
;
709 extern struct statement
*__prev_stmt
;
710 extern struct statement
*__cur_stmt
;
711 extern struct statement
*__next_stmt
;
712 void init_fake_env(void);
713 void end_fake_env(void);
714 int time_parsing_function(void);
715 bool taking_too_long(void);
716 struct statement
*get_last_stmt(void);
717 int is_last_stmt(struct statement
*cur_stmt
);
719 /* smatch_struct_assignment.c */
720 struct expression
*get_faked_expression(void);
721 void __fake_struct_member_assignments(struct expression
*expr
);
722 void create_recursive_fake_assignments(struct expression
*expr
,
723 void (*assign_handler
)(struct expression
*expr
, void *data
),
726 /* smatch_project.c */
727 int is_no_inline_function(const char *function
);
729 /* smatch_conditions */
730 void __split_whole_condition(struct expression
*expr
);
731 void __handle_logic(struct expression
*expr
);
732 int is_condition(struct expression
*expr
);
733 int __handle_condition_assigns(struct expression
*expr
);
734 int __handle_select_assigns(struct expression
*expr
);
735 int __handle_expr_statement_assigns(struct expression
*expr
);
737 /* smatch_implied.c */
738 struct range_list_stack
;
739 void param_limit_implications(struct expression
*expr
, int param
, char *key
, char *value
, struct stree
**implied
);
740 struct stree
*__implied_case_stree(struct expression
*switch_expr
,
741 struct range_list
*case_rl
,
742 struct range_list_stack
**remaining_cases
,
743 struct stree
**raw_stree
);
744 void overwrite_states_using_pool(struct sm_state
*gate_sm
, struct sm_state
*pool_sm
);
745 int assume(struct expression
*expr
);
746 void end_assume(void);
747 int impossible_assumption(struct expression
*left
, int op
, sval_t sval
);
750 bool has_dynamic_states(unsigned short owner
);
751 void set_dynamic_states(unsigned short owner
);
753 /* smatch_extras.c */
754 int in_warn_on_macro(void);
755 extern int SMATCH_EXTRA
;
756 extern int RETURN_ID
;
763 char *get_other_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
);
764 char *map_call_to_other_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
);
765 char *map_long_to_short_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
, bool use_stack
);
767 #define STRLEN_MAX_RET 1010101
769 /* smatch_absolute.c */
770 int get_absolute_min_helper(struct expression
*expr
, sval_t
*sval
);
771 int get_absolute_max_helper(struct expression
*expr
, sval_t
*sval
);
773 /* smatch_type_value.c */
774 void disable_type_val_lookups(void);
775 void enable_type_val_lookups(void);
776 void clear_type_value_cache(void);
777 int get_db_type_rl(struct expression
*expr
, struct range_list
**rl
);
779 /* smatch_data_val.c */
780 int get_mtag_rl(struct expression
*expr
, struct range_list
**rl
);
781 /* smatch_array_values.c */
782 void clear_array_values_cache(void);
783 int get_array_rl(struct expression
*expr
, struct range_list
**rl
);
785 /* smatch_states.c */
786 struct stree
*__swap_cur_stree(struct stree
*stree
);
787 void __push_fake_cur_stree();
788 struct stree
*__pop_fake_cur_stree();
789 void __free_fake_cur_stree();
790 void __set_fake_cur_stree_fast(struct stree
*stree
);
791 void __pop_fake_cur_stree_fast(void);
792 void __merge_stree_into_cur(struct stree
*stree
);
794 int unreachable(void);
795 void __set_cur_stree_readonly(void);
796 void __set_cur_stree_writable(void);
797 void __set_sm(struct sm_state
*sm
);
798 void __set_sm_cur_stree(struct sm_state
*sm
);
799 void __set_true_false_sm(struct sm_state
*true_state
,
800 struct sm_state
*false_state
);
801 void nullify_path(void);
802 void __match_nullify_path_hook(const char *fn
, struct expression
*expr
,
804 void __unnullify_path(void);
805 int __path_is_null(void);
806 void save_all_states(void);
807 void restore_all_states(void);
808 void free_goto_stack(void);
809 void clear_all_states(void);
811 struct sm_state
*get_sm_state(int owner
, const char *name
,
813 struct sm_state
*get_sm_state_expr(int owner
, struct expression
*expr
);
814 void __push_true_states(void);
815 void __use_false_states(void);
816 void __discard_false_states(void);
817 void __merge_false_states(void);
818 void __merge_true_states(void);
820 void __negate_cond_stacks(void);
821 void __use_pre_cond_states(void);
822 void __use_cond_true_states(void);
823 void __use_cond_false_states(void);
824 void __push_cond_stacks(void);
825 void __fold_in_set_states(void);
826 void __free_set_states(void);
827 struct stree
*__copy_cond_true_states(void);
828 struct stree
*__copy_cond_false_states(void);
829 struct stree
*__pop_cond_true_stack(void);
830 struct stree
*__pop_cond_false_stack(void);
831 void __and_cond_states(void);
832 void __or_cond_states(void);
833 void __save_pre_cond_states(void);
834 void __discard_pre_cond_states(void);
835 struct stree
*__get_true_states(void);
836 struct stree
*__get_false_states(void);
837 void __use_cond_states(void);
838 extern struct state_list
*__last_base_slist
;
840 void __push_continues(void);
841 void __discard_continues(void);
842 void __process_continues(void);
843 void __merge_continues(void);
845 void __push_breaks(void);
846 void __process_breaks(void);
847 int __has_breaks(void);
848 void __merge_breaks(void);
849 void __use_breaks(void);
851 void __save_switch_states(struct expression
*switch_expr
);
852 void __discard_switches(void);
853 int have_remaining_cases(void);
854 void __merge_switches(struct expression
*switch_expr
, struct range_list
*case_rl
);
855 void __push_default(void);
856 void __set_default(void);
857 bool __has_default_case(void);
858 int __pop_default(void);
860 void __push_conditions(void);
861 void __discard_conditions(void);
863 void __save_gotos(const char *name
, struct symbol
*sym
);
864 void __merge_gotos(const char *name
, struct symbol
*sym
);
866 void __discard_fake_states(struct expression
*call
);
868 void __print_cur_stree(void);
869 bool __print_states(const char *owner
);
870 typedef void (check_tracker_hook
)(int owner
, const char *name
, struct symbol
*sym
, struct smatch_state
*state
);
871 void add_check_tracker(const char *check_name
, check_tracker_hook
*fn
);
874 void __pass_to_client(void *data
, enum hook_type type
);
875 void __pass_case_to_client(struct expression
*switch_expr
,
876 struct range_list
*rl
);
877 int __has_merge_function(int client_id
);
878 struct smatch_state
*__client_merge_function(int owner
,
879 struct smatch_state
*s1
,
880 struct smatch_state
*s2
);
881 struct smatch_state
*__client_unmatched_state_function(struct sm_state
*sm
);
882 void call_pre_merge_hook(struct sm_state
*cur
, struct sm_state
*other
);
883 void __push_scope_hooks(void);
884 void __call_scope_hooks(void);
885 void add_array_initialized_hook(void (*hook
)(struct expression
*array
, int nr
));
886 void __call_array_initialized_hooks(struct expression
*array
, int nr
);
888 /* smatch_function_hooks.c */
889 void add_fake_call_after_return(struct expression
*call
);
890 void create_function_hook_hash(void);
891 void __match_initializer_call(struct symbol
*sym
);
893 struct expression
*get_unfaked_call(void);
894 void fake_param_assign_helper(struct expression
*call
, struct expression
*fake_assign
);
900 * Changing these numbers is a pain. Don't do it. If you ever use a
901 * number it can't be re-used right away so there may be gaps.
902 * We select these in order by type so if the order matters, then give
903 * it a number below 100-999,9000-9999 ranges. */
918 ABSOLUTE_LIMITS
= 1010,
932 UNTRACKED_PARAM
= 1023,
938 COMPARE_LIMIT
= 1028,
939 PARAM_COMPARE
= 1029,
942 CONSTRAINT_REQUIRED
= 1033,
953 POWER_OF_TWO_SET
= 1049,
959 NEGATIVE_ERROR
= 1057,
962 /* put random temporary stuff in the 7000-7999 range for testing */
964 HOST_DATA_SET
= 7017,
968 USER_DATA_SET
= 9017,
972 NO_OVERFLOW_SIMPLE
= 8019,
978 KNOWN_UNLOCKED
= 9025,
983 REFCOUNT_INIT
= 9025,
986 NO_SIDE_EFFECT
= 8025,
989 ARRAYSIZE_ARG
= 8033,
1003 /* Do not use numbers above 100k */
1006 extern struct sqlite3
*smatch_db
;
1007 extern struct sqlite3
*mem_db
;
1008 extern struct sqlite3
*cache_db
;
1010 bool db_incomplete(void);
1011 void db_ignore_states(int id
);
1012 typedef bool (delete_hook
)(struct expression
*expr
);
1013 void add_delete_return_hook(delete_hook
*hook
);
1014 void select_caller_info_hook(void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
), int type
);
1015 void select_caller_name_sym(void (*fn
)(const char *name
, struct symbol
*sym
, char *value
), int type
);
1016 void add_member_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
));
1017 void add_caller_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
));
1018 void add_return_info_callback(int owner
,
1019 void (*callback
)(int return_id
, char *return_ranges
,
1020 struct expression
*returned_expr
,
1022 const char *printed_name
,
1023 struct sm_state
*sm
));
1024 void add_split_return_callback(void (*fn
)(int return_id
, char *return_ranges
, struct expression
*returned_expr
));
1025 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
));
1026 void select_call_implies_hook(int type
, void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
));
1027 void select_return_implies_hook_early(int type
, void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
));
1028 void select_return_implies_hook(int type
, void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
));
1029 struct range_list
*db_return_vals(struct expression
*expr
);
1030 struct range_list
*db_return_vals_from_str(const char *fn_name
);
1031 struct range_list
*db_return_vals_no_args(struct expression
*expr
);
1032 bool split_param_key(const char *value
, int *param
, char *key
, int len
);
1033 bool get_implied_rl_from_call_str(struct expression
*expr
, const char *data
, struct range_list
**rl
);
1034 char *get_chunk_from_key(struct expression
*arg
, char *key
, struct symbol
**sym
, struct var_sym_list
**vsl
);
1035 char *get_variable_from_key(struct expression
*arg
, const char *key
, struct symbol
**sym
);
1036 char *get_name_sym_from_param_key(struct expression
*expr
, int param
, const char *key
, struct symbol
**sym
);
1037 char *get_param_name_var_sym(const char *name
, struct symbol
*sym
);
1038 char *get_param_name(struct sm_state
*sm
);
1039 const char *get_mtag_name_var_sym(const char *state_name
, struct symbol
*sym
);
1040 const char *get_mtag_name_expr(struct expression
*expr
);
1041 char *get_data_info_name(struct expression
*expr
);
1042 char *sm_to_arg_name(struct expression
*expr
, struct sm_state
*sm
);
1043 int is_recursive_member(const char *param_name
);
1045 char *escape_newlines(const char *str
);
1046 void sql_exec(struct sqlite3
*db
, int (*callback
)(void*, int, char**, char**), void *data
, const char *sql
);
1048 #define sql_helper(db, call_back, data, sql...) \
1050 char sql_txt[1024]; \
1052 sqlite3_snprintf(sizeof(sql_txt), sql_txt, sql); \
1053 db_debug("debug: %s\n", sql_txt); \
1054 sql_exec(db, call_back, data, sql_txt); \
1058 #define run_sql(call_back, data, sql...) \
1062 sql_helper(smatch_db, call_back, data, sql); \
1065 #define mem_sql(call_back, data, sql...) \
1066 sql_helper(mem_db, call_back, data, sql)
1068 #define cache_sql(call_back, data, sql...) \
1069 sql_helper(cache_db, call_back, data, sql)
1071 #define sql_insert_helper(table, db, ignore, late, values...) \
1073 struct sqlite3 *_db = db; \
1075 if (__inline_fn && !_db) \
1079 char *err, *p = buf; \
1082 p += snprintf(p, buf + sizeof(buf) - p, \
1083 "insert %sinto %s values (", \
1084 ignore ? "or ignore " : "", #table); \
1085 p += snprintf(p, buf + sizeof(buf) - p, values); \
1086 p += snprintf(p, buf + sizeof(buf) - p, ");"); \
1087 db_debug("mem-db: %s\n", buf); \
1088 rc = sqlite3_exec(_db, buf, NULL, NULL, &err); \
1089 if (rc != SQLITE_OK) { \
1090 sm_ierror("SQL error #2: %s", err); \
1091 sm_ierror("SQL: '%s'", buf); \
1096 if (option_info) { \
1097 FILE *tmp_fd = sm_outfd; \
1098 sm_outfd = sql_outfd; \
1100 sm_printf("SQL%s: insert %sinto " #table " values(", \
1101 late ? "_late" : "", ignore ? "or ignore " : ""); \
1102 sm_printf(values); \
1103 sm_printf(");\n"); \
1104 sm_outfd = tmp_fd; \
1108 #define sql_insert(table, values...) sql_insert_helper(table, NULL, 0, 0, values);
1109 #define sql_insert_or_ignore(table, values...) sql_insert_helper(table, NULL, 1, 0, values);
1110 #define sql_insert_late(table, values...) sql_insert_helper(table, NULL, 0, 1, values);
1111 #define sql_insert_cache(table, values...) sql_insert_helper(table, cache_db, 1, 0, values);
1112 #define sql_insert_cache_or_ignore(table, values...) sql_insert_helper(table, cache_db, 1, 0, values);
1114 char *get_static_filter(struct symbol
*sym
);
1116 void sql_insert_return_states(int return_id
, const char *return_ranges
,
1117 int type
, int param
, const char *key
, const char *value
);
1118 void sql_insert_caller_info(struct expression
*call
, int type
, int param
,
1119 const char *key
, const char *value
);
1120 void sql_insert_function_ptr(const char *fn
, const char *struct_name
);
1121 void sql_insert_return_values(const char *return_values
);
1122 void sql_insert_return_implies(int type
, int param
, const char *key
, const char *value
);
1123 void sql_insert_function_type_size(const char *member
, const char *ranges
);
1124 void sql_insert_function_type_info(int type
, const char *struct_type
, const char *member
, const char *value
);
1125 void sql_insert_type_info(int type
, const char *member
, const char *value
);
1126 void sql_insert_local_values(const char *name
, const char *value
);
1127 void sql_insert_function_type_value(const char *type
, const char *value
);
1128 void sql_insert_function_type(int param
, const char *value
);
1129 void sql_insert_parameter_name(int param
, const char *value
);
1130 void sql_insert_data_info(struct expression
*data
, int type
, const char *value
);
1131 void sql_insert_data_info_var_sym(const char *var
, struct symbol
*sym
, int type
, const char *value
);
1132 void sql_save_constraint(const char *con
);
1133 void sql_save_constraint_required(const char *data
, int op
, const char *limit
);
1134 void sql_copy_constraint_required(const char *new_limit
, const char *old_limit
);
1135 void sql_insert_fn_ptr_data_link(const char *ptr
, const char *data
);
1136 void sql_insert_fn_data_link(struct expression
*fn
, int type
, int param
, const char *key
, const char *value
);
1137 void sql_insert_mtag_about(mtag_t tag
, const char *left_name
, const char *right_name
);
1138 void sql_insert_mtag_info(mtag_t tag
, int type
, const char *value
);
1139 void sql_insert_mtag_map(mtag_t container
, int container_offset
, mtag_t tag
, int tag_offset
);
1140 void sql_insert_mtag_alias(mtag_t orig
, mtag_t alias
);
1141 int mtag_map_select_container(mtag_t tag
, int container_offset
, mtag_t
*container
);
1142 int mtag_map_select_tag(mtag_t container
, int offset
, mtag_t
*tag
);
1143 struct smatch_state
*get_mtag_return(struct expression
*expr
, struct smatch_state
*state
);
1144 struct range_list
*swap_mtag_seed(struct expression
*expr
, struct range_list
*rl
);
1146 bool is_fn_ptr(struct expression
*fn
);
1147 void sql_select_return_states(const char *cols
, struct expression
*call
,
1148 int (*callback
)(void*, int, char**, char**), void *info
);
1149 void sql_select_call_implies(const char *cols
, struct expression
*call
,
1150 int (*callback
)(void*, int, char**, char**));
1152 void open_smatch_db(char *db_file
);
1154 /* smatch_files.c */
1155 int open_data_file(const char *filename
);
1156 int open_schema_file(const char *schema
);
1157 struct token
*get_tokens_file(const char *filename
);
1158 struct string_list
*load_strings_from_file(const char *project
, const char *filename
);
1161 extern char *option_debug_check
;
1162 extern char *option_debug_var
;
1163 extern char *option_process_function
;
1164 extern char *option_project_str
;
1165 extern char *bin_dir
;
1166 extern char *data_dir
;
1167 extern int option_no_data
;
1168 extern int option_full_path
;
1169 extern int option_call_tree
;
1170 extern int num_checks
;
1176 PROJ_ILLUMOS_KERNEL
,
1180 extern enum project_type option_project
;
1181 const char *check_name(unsigned short id
);
1182 int id_from_name(const char *name
);
1185 /* smatch_buf_size.c */
1186 int bytes_to_elements(struct expression
*expr
, int bytes
);
1187 int get_array_size(struct expression
*expr
);
1188 int get_array_size_bytes(struct expression
*expr
);
1189 int get_array_size_bytes_min(struct expression
*expr
);
1190 int get_array_size_bytes_max(struct expression
*expr
);
1191 struct range_list
*get_array_size_bytes_rl(struct expression
*expr
);
1192 int get_real_array_size(struct expression
*expr
);
1193 int last_member_is_resizable(struct symbol
*type
);
1194 /* smatch_strlen.c */
1195 bool is_strlen(struct expression
*expr
);
1196 int get_implied_strlen(struct expression
*expr
, struct range_list
**rl
);
1197 int get_size_from_strlen(struct expression
*expr
);
1199 /* smatch_capped.c */
1200 int is_capped(struct expression
*expr
);
1201 int is_capped_var_sym(const char *name
, struct symbol
*sym
);
1203 /* smatch_kernel_user_data.c */
1204 int is_user_macro(struct expression
*expr
);
1205 int is_capped_user_data(struct expression
*expr
);
1206 int implied_user_data(struct expression
*expr
, struct range_list
**rl
);
1207 struct stree
*get_user_stree(void);
1208 int get_user_rl(struct expression
*expr
, struct range_list
**rl
);
1209 int is_user_rl(struct expression
*expr
);
1210 int get_user_rl_var_sym(const char *name
, struct symbol
*sym
, struct range_list
**rl
);
1211 void mark_as_user_data(struct expression
*expr
, bool isnew
);
1212 bool user_rl_capped_var_sym(const char *name
, struct symbol
*sym
);
1213 bool user_rl_capped(struct expression
*expr
);
1214 struct range_list
*var_user_rl(struct expression
*expr
);
1215 bool we_pass_user_data(struct expression
*call
);
1216 /* smatch_points_to_user_data.c */
1217 bool is_user_data_fn(struct symbol
*fn
);
1218 bool is_skb_data(struct expression
*expr
);
1219 bool is_socket_stuff(struct symbol
*sym
);
1220 bool points_to_user_data(struct expression
*expr
);
1221 void set_array_user_ptr(struct expression
*expr
, bool is_new
);
1223 /* smatch_kernel_host_data.c */
1224 int is_host_macro(struct expression
*expr
);
1225 int get_host_data_fn_param(const char *fn
);
1226 int is_capped_host_data(struct expression
*expr
);
1227 int implied_host_data(struct expression
*expr
, struct range_list
**rl
);
1228 struct stree
*get_host_stree(void);
1229 int get_host_rl(struct expression
*expr
, struct range_list
**rl
);
1230 int is_host_rl(struct expression
*expr
);
1231 int get_host_rl_var_sym(const char *name
, struct symbol
*sym
, struct range_list
**rl
);
1232 bool host_rl_capped(struct expression
*expr
);
1233 struct range_list
*var_host_rl(struct expression
*expr
);
1234 bool we_pass_host_data(struct expression
*call
);
1235 /* smatch_points_to_host_data.c */
1236 bool is_host_data_fn(struct symbol
*fn
);
1237 bool points_to_host_data(struct expression
*expr
);
1238 void set_points_to_host_data(struct expression
*expr
, bool is_new
);
1239 bool is_fn_points_to_host_data(const char *fn
);
1240 /* check_locking.c */
1241 void print_held_locks();
1243 void __preempt_add(void);
1244 void __preempt_sub(void);
1245 int get_preempt_cnt(void);
1246 void clear_preempt_cnt(void);
1247 bool function_decrements_preempt(void);
1248 void add_sleep_callback(expr_func
*fn
);
1249 unsigned long GFP_DIRECT_RECLAIM(void);
1250 unsigned long GFP_ATOMIC(void);
1252 /* check_assigned_expr.c */
1253 extern int check_assigned_expr_id
;
1254 struct expression
*get_assigned_expr(struct expression
*expr
);
1255 struct sm_state
*get_assigned_sm(struct expression
*expr
);
1256 struct expression
*get_assigned_expr_recurse(struct expression
*expr
);
1257 struct expression
*get_assigned_expr_name_sym(const char *name
, struct symbol
*sym
);
1258 struct expression
*get_assigned_expr_name_sym_recurse(const char *name
, struct symbol
*sym
);
1260 /* smatch_return_to_param.c */
1261 char *map_call_to_param_name_sym(struct expression
*expr
, struct symbol
**sym
);
1263 /* smatch_comparison.c */
1264 extern int comparison_id
;
1265 #define UNKNOWN_COMPARISON 0
1266 #define IMPOSSIBLE_COMPARISON -1
1267 struct compare_data
{
1268 /* The ->left and ->right expression pointers might be NULL (I'm lazy) */
1269 struct expression
*left
;
1270 const char *left_var
;
1271 struct var_sym_list
*left_vsl
;
1273 struct expression
*right
;
1274 const char *right_var
;
1275 struct var_sym_list
*right_vsl
;
1277 DECLARE_ALLOCATOR(compare_data
);
1278 struct smatch_state
*alloc_compare_state(
1279 struct expression
*left
,
1280 const char *left_var
, struct var_sym_list
*left_vsl
,
1282 struct expression
*right
,
1283 const char *right_var
, struct var_sym_list
*right_vsl
);
1284 int comparison_intersection(int orig
, int op
);
1285 int merge_comparisons(int one
, int two
);
1286 int combine_comparisons(int left_compare
, int right_compare
);
1287 int state_to_comparison(struct smatch_state
*state
);
1288 struct smatch_state
*merge_compare_states(struct smatch_state
*s1
, struct smatch_state
*s2
);
1289 int get_comparison(struct expression
*left
, struct expression
*right
);
1290 int get_comparison_no_extra(struct expression
*a
, struct expression
*b
);
1291 int get_comparison_strings(const char *one
, const char *two
);
1292 int possible_comparison(struct expression
*a
, int comparison
, struct expression
*b
);
1293 struct state_list
*get_all_comparisons(struct expression
*expr
);
1294 struct state_list
*get_all_possible_equal_comparisons(struct expression
*expr
);
1295 void __add_comparison_info(struct expression
*expr
, struct expression
*call
, const char *range
);
1296 char *get_printed_param_name(struct expression
*call
, const char *param_name
, struct symbol
*param_sym
);
1297 char *name_sym_to_param_comparison(const char *name
, struct symbol
*sym
);
1298 char *expr_equal_to_param(struct expression
*expr
, int ignore
);
1299 char *expr_lte_to_param(struct expression
*expr
, int ignore
);
1300 char *expr_param_comparison(struct expression
*expr
, int ignore
);
1301 int flip_comparison(int op
);
1302 int negate_comparison(int op
);
1303 int remove_unsigned_from_comparison(int op
);
1304 int param_compare_limit_is_impossible(struct expression
*expr
, int left_param
, char *left_key
, char *value
);
1305 void filter_by_comparison(struct range_list
**rl
, int comparison
, struct range_list
*right
);
1306 void __compare_param_limit_hook(struct expression
*left_expr
, struct expression
*right_expr
,
1307 const char *state_name
,
1308 struct smatch_state
*true_state
, struct smatch_state
*false_state
);
1309 int impossibly_high_comparison(struct expression
*expr
);
1310 void add_comparison_var_sym(
1311 struct expression
*left_expr
,
1312 const char *left_name
, struct var_sym_list
*left_vsl
,
1314 struct expression
*right_expr
,
1315 const char *right_name
, struct var_sym_list
*right_vsl
,
1316 struct expression
*mod_expr
);
1319 sval_t
*sval_alloc(sval_t sval
);
1320 sval_t
*sval_alloc_permanent(sval_t sval
);
1321 sval_t
sval_blank(struct expression
*expr
);
1322 sval_t
sval_type_val(struct symbol
*type
, long long val
);
1323 sval_t
sval_type_fval(struct symbol
*type
, long double fval
);
1324 sval_t
sval_from_val(struct expression
*expr
, long long val
);
1325 sval_t
sval_from_fval(struct expression
*expr
, long double fval
);
1326 int sval_is_ptr(sval_t sval
);
1327 bool sval_is_fp(sval_t sval
);
1328 int sval_unsigned(sval_t sval
);
1329 int sval_signed(sval_t sval
);
1330 int sval_bits(sval_t sval
);
1331 int sval_bits_used(sval_t sval
);
1332 int sval_is_negative(sval_t sval
);
1333 int sval_is_positive(sval_t sval
);
1334 int sval_is_min(sval_t sval
);
1335 int sval_is_max(sval_t sval
);
1336 int sval_is_a_min(sval_t sval
);
1337 int sval_is_a_max(sval_t sval
);
1338 int sval_is_negative_min(sval_t sval
);
1339 int sval_cmp_t(struct symbol
*type
, sval_t one
, sval_t two
);
1340 int sval_cmp_val(sval_t one
, long long val
);
1341 sval_t
sval_min(sval_t one
, sval_t two
);
1342 sval_t
sval_min_nonneg(sval_t one
, sval_t two
);
1343 sval_t
sval_max(sval_t one
, sval_t two
);
1344 int sval_too_low(struct symbol
*type
, sval_t sval
);
1345 int sval_too_high(struct symbol
*type
, sval_t sval
);
1346 int sval_fits(struct symbol
*type
, sval_t sval
);
1347 sval_t
sval_cast(struct symbol
*type
, sval_t sval
);
1348 sval_t
sval_preop(sval_t sval
, int op
);
1349 sval_t
sval_binop(sval_t left
, int op
, sval_t right
);
1350 int sval_binop_overflows(sval_t left
, int op
, sval_t right
);
1351 int sval_binop_overflows_no_sign(sval_t left
, int op
, sval_t right
);
1352 int find_first_zero_bit(unsigned long long uvalue
);
1353 int sm_fls64(unsigned long long uvalue
);
1354 unsigned long long fls_mask(unsigned long long uvalue
);
1355 unsigned long long sval_fls_mask(sval_t sval
);
1356 const char *sval_to_str(sval_t sval
);
1357 const char *sval_to_str_or_err_ptr(sval_t sval
);
1358 const char *sval_to_numstr(sval_t sval
);
1359 sval_t
ll_to_sval(long long val
);
1361 /* smatch_string_list.c */
1362 int list_has_string(struct string_list
*str_list
, const char *str
);
1363 int insert_string(struct string_list
**str_list
, const char *str
);
1364 struct string_list
*clone_str_list(struct string_list
*orig
);
1365 struct string_list
*combine_string_lists(struct string_list
*one
, struct string_list
*two
);
1367 /* smatch_start_states.c */
1368 struct stree
*get_start_states(void);
1370 /* smatch_recurse.c */
1371 int recurse(struct expression
*expr
,
1372 int (func
)(struct expression
*expr
, void *p
),
1373 void *param
, int nr
);
1374 int has_symbol(struct expression
*expr
, struct symbol
*sym
);
1375 int has_variable(struct expression
*expr
, struct expression
*var
);
1376 int has_inc_dec(struct expression
*expr
);
1378 /* smatch_stored_conditions.c */
1379 struct smatch_state
*get_stored_condition(struct expression
*expr
);
1380 struct expression_list
*get_conditions(struct expression
*expr
);
1381 struct sm_state
*stored_condition_implication_hook(struct expression
*expr
,
1382 struct state_list
**true_stack
,
1383 struct state_list
**false_stack
);
1384 /* smatch_parsed_conditions.c */
1385 struct sm_state
*parsed_condition_implication_hook(struct expression
*expr
,
1386 struct state_list
**true_stack
,
1387 struct state_list
**false_stack
);
1388 /* smatch_comparison.c */
1389 struct sm_state
*comparison_implication_hook(struct expression
*expr
,
1390 struct state_list
**true_stack
,
1391 struct state_list
**false_stack
);
1393 /* check_string_len.c */
1394 int get_formatted_string_size(struct expression
*call
, int arg
);
1395 int get_formatted_string_min_size(struct expression
*call
, int arg
);
1397 /* smatch_param_set.c */
1398 int param_was_set(struct expression
*expr
);
1399 int param_was_set_var_sym(const char *name
, struct symbol
*sym
);
1400 void print_limited_param_set(int return_id
, char *return_ranges
, struct expression
*expr
);
1402 void __promote_sets_to_clears(int return_id
, char *return_ranges
, struct expression
*expr
);
1403 bool parent_was_PARAM_CLEAR(const char *name
, struct symbol
*sym
);
1404 bool parent_was_PARAM_CLEAR_ZERO(const char *name
, struct symbol
*sym
);
1406 /* smatch_param_filter.c */
1407 int param_has_filter_data(struct sm_state
*sm
);
1409 /* smatch_links.c */
1410 void set_up_link_functions(int id
, int linkid
);
1411 struct smatch_state
*merge_link_states(struct smatch_state
*s1
, struct smatch_state
*s2
);
1412 void store_link(int link_id
, const char *name
, struct symbol
*sym
, const char *link_name
, struct symbol
*link_sym
);
1414 /* check_buf_comparison */
1415 const char *limit_type_str(unsigned int limit_type
);
1416 struct expression
*get_size_variable(struct expression
*buf
, int *limit_type
);
1417 struct expression
*get_array_variable(struct expression
*size
);
1418 int buf_comparison_index_ok(struct expression
*expr
);
1419 bool buf_comp_has_bytes(struct expression
*buf
, struct expression
*var
);
1420 bool buf_comp2_has_bytes(struct expression
*buf_expr
, struct expression
*var
);
1422 /* smatch_untracked_param.c */
1423 void mark_untracked(struct expression
*expr
, int param
, const char *key
, const char *value
);
1424 void mark_call_params_untracked(struct expression
*call
);
1425 void add_untracked_param_hook(void (func
)(struct expression
*call
, int param
));
1426 void add_lost_param_hook(void (func
)(struct expression
*call
, int param
));
1427 void mark_all_params_untracked(int return_id
, char *return_ranges
, struct expression
*expr
);
1428 /* smatch_untracked_var.c */
1429 bool is_untracked(struct expression
*expr
);
1431 /* smatch_strings.c */
1432 struct state_list
*get_strings(struct expression
*expr
);
1433 struct expression
*fake_string_from_mtag(mtag_t tag
);
1435 /* smatch_estate.c */
1436 int estate_get_single_value(struct smatch_state
*state
, sval_t
*sval
);
1438 /* smatch_address.c */
1439 int get_address_rl(struct expression
*expr
, struct range_list
**rl
);
1440 int get_member_offset(struct symbol
*type
, const char *member_name
);
1441 int get_member_offset_from_deref(struct expression
*expr
);
1443 /* for now this is in smatch_used_parameter.c */
1444 void __get_state_hook(int owner
, const char *name
, struct symbol
*sym
);
1445 extern int __ignore_param_used
;
1447 /* smatch_buf_comparison.c */
1448 int db_var_is_array_limit(struct expression
*array
, const char *name
, struct var_sym_list
*vsl
);
1450 struct range_list
*get_fs(void);
1452 struct stree
*get_all_return_states(void);
1453 struct stree_stack
*get_all_return_strees(void);
1454 int on_atomic_dec_path(void);
1455 int was_inced(const char *name
, struct symbol
*sym
);
1456 void set_refcount_inc(char *name
, struct symbol
*sym
);
1457 void set_refcount_dec(char *name
, struct symbol
*sym
);
1458 void add_refcount_init_hook(name_sym_hook
*hook
);
1459 void add_refcount_inc_hook(name_sym_hook
*hook
);
1460 void add_refcount_dec_hook(name_sym_hook
*hook
);
1462 /* smatch_constraints.c */
1463 char *get_constraint_str(struct expression
*expr
);
1464 struct constraint_list
*get_constraints(struct expression
*expr
);
1465 char *unmet_constraint(struct expression
*data
, struct expression
*offset
);
1466 char *get_required_constraint(const char *data_str
);
1468 /* smatch_container_of.c */
1469 int get_param_from_container_of(struct expression
*expr
);
1470 int get_offset_from_container_of(struct expression
*expr
);
1471 char *get_container_name(struct expression
*container
, struct expression
*expr
);
1474 unsigned long long str_to_llu_hash_helper(const char *str
);
1475 unsigned long long str_to_llu_hash(const char *str
);
1476 struct symbol
*get_symbol_from_mtag(mtag_t tag
);
1477 mtag_t
str_to_mtag(const char *str
);
1478 int get_string_mtag(struct expression
*expr
, mtag_t
*tag
);
1479 int get_toplevel_mtag(struct symbol
*sym
, mtag_t
*tag
);
1480 int create_mtag_alias(mtag_t tag
, struct expression
*expr
, mtag_t
*new);
1481 int expr_to_mtag_offset(struct expression
*expr
, mtag_t
*tag
, int *offset
);
1482 void update_mtag_data(struct expression
*expr
, struct smatch_state
*state
);
1483 int get_mtag_sval(struct expression
*expr
, sval_t
*sval
);
1485 /* Trinity fuzzer stuff */
1486 const char *get_syscall_arg_type(struct symbol
*sym
);
1489 int binfo_equiv(struct bit_info
*one
, struct bit_info
*two
);
1490 struct bit_info
*alloc_bit_info(unsigned long long set
, unsigned long long possible
);
1491 struct smatch_state
*alloc_bstate(unsigned long long set
, unsigned long long possible
);
1492 struct smatch_state
*merge_bstates(struct smatch_state
*one_state
, struct smatch_state
*two_state
);
1494 /* smatch_param_bits_set.c */
1495 void __set_param_modified_helper(struct expression
*expr
, struct smatch_state
*state
);
1496 void __set_param_modified_helper_sym(const char *name
, struct symbol
*sym
,
1497 struct smatch_state
*state
);
1499 /* smatch_param_bits_clear.c */
1500 void __set_param_modified_helper_clear(struct expression
*expr
, struct smatch_state
*state
);
1501 void __set_param_modified_helper_sym_clear(const char *name
, struct symbol
*sym
,
1502 struct smatch_state
*state
);
1504 /* smatch_bit_info.c */
1505 struct bit_info
*rl_to_binfo(struct range_list
*rl
);
1506 struct bit_info
*get_bit_info(struct expression
*expr
);
1507 struct bit_info
*get_bit_info_var_sym(const char *name
, struct symbol
*sym
);
1509 /* smatch_mem_tracker.c */
1510 extern int option_mem
;
1511 unsigned long get_mem_kb(void);
1512 unsigned long get_max_memory(void);
1514 /* smatch_goto_tracker.c */
1515 struct sm_state
*get_goto_sm_state(void);
1517 /* check_is_nospec.c */
1518 bool is_nospec(struct expression
*expr
);
1519 long get_stmt_cnt(void);
1521 /* check_kernel_units.c */
1522 struct smatch_state
*get_units(struct expression
*expr
);
1523 char *get_unit_str(struct expression
*expr
);
1524 bool is_array_size_units(struct expression
*expr
);
1525 /* smatch_nul_terminator.c */
1526 bool is_nul_terminated_var_sym(const char *name
, struct symbol
*sym
);
1527 bool is_nul_terminated(struct expression
*expr
);
1528 /* check_kernel.c */
1529 bool is_ignored_kernel_data(const char *name
);
1530 int get_gfp_param(struct expression
*expr
);
1532 bool is_fresh_alloc_var_sym(const char *var
, struct symbol
*sym
);
1533 bool is_fresh_alloc(struct expression
*expr
);
1534 bool is_freed_var_sym(const char *name
, struct symbol
*sym
);
1535 void track_freed_param(struct expression
*expr
, struct smatch_state
*state
);
1536 void track_freed_param_var_sym(const char *name
, struct symbol
*sym
,
1537 struct smatch_state
*state
);
1538 bool is_part_of_condition(struct expression
*expr
);
1539 bool is_percent_p_print(struct expression
*expr
);
1541 void add_free_hook(name_sym_hook
*hook
);
1543 /* smatch_unconstant_macros.c */
1544 int is_unconstant_macro(struct expression
*expr
);
1546 /* smatch_assigned_state.c */
1547 void add_state_assigned_hook(int owner
, sm_hook
*call_back
);
1548 void add_ssa_state_assigned_hook(int owner
, sm_hook
*call_back
);
1550 static inline bool type_is_ptr(struct symbol
*type
)
1553 (type
->type
== SYM_PTR
||
1554 type
->type
== SYM_ARRAY
||
1555 type
->type
== SYM_FN
);
1558 static inline bool type_is_fp(struct symbol
*type
)
1561 (type
== &float_ctype
||
1562 type
== &double_ctype
||
1563 type
== &ldouble_ctype
);
1566 static inline int type_bits(struct symbol
*type
)
1570 if (type_is_ptr(type
))
1571 return bits_in_pointer
;
1572 if (!type
->examined
)
1573 examine_symbol_type(type
);
1574 return type
->bit_size
;
1577 static inline int type_unsigned(struct symbol
*base_type
)
1581 if (is_ptr_type(base_type
))
1583 if (base_type
->ctype
.modifiers
& MOD_UNSIGNED
)
1588 static inline int type_positive_bits(struct symbol
*type
)
1592 if (is_ptr_type(type
))
1593 return bits_in_pointer
;
1594 if (type_unsigned(type
))
1595 return type_bits(type
);
1596 return type_bits(type
) - 1;
1599 static inline int sval_positive_bits(sval_t sval
)
1601 return type_positive_bits(sval
.type
);
1605 * Returns -1 if one is smaller, 0 if they are the same and 1 if two is larger.
1608 static inline int fp_cmp(sval_t one
, sval_t two
)
1610 struct symbol
*type
;
1612 if (sval_is_fp(one
) && sval_is_fp(two
))
1613 type
= type_bits(one
.type
) > type_bits(two
.type
) ? one
.type
: two
.type
;
1614 else if (sval_is_fp(one
))
1619 one
= sval_cast(type
, one
);
1620 two
= sval_cast(type
, two
);
1622 if (one
.type
== &float_ctype
) {
1623 if (one
.fvalue
< two
.fvalue
)
1625 if (one
.fvalue
== two
.fvalue
)
1629 if (one
.type
== &double_ctype
) {
1630 if (one
.dvalue
< two
.dvalue
)
1632 if (one
.dvalue
== two
.dvalue
)
1636 if (one
.type
== &ldouble_ctype
) {
1637 if (one
.ldvalue
< two
.ldvalue
)
1639 if (one
.ldvalue
== two
.ldvalue
)
1643 sm_perror("bad type in fp_cmp(): %s", type_to_str(type
));
1647 static inline int sval_cmp(sval_t one
, sval_t two
)
1649 struct symbol
*type
;
1651 if (sval_is_fp(one
) || sval_is_fp(two
))
1652 return fp_cmp(one
, two
);
1655 if (sval_positive_bits(two
) > sval_positive_bits(one
))
1657 if (type_bits(type
) < 31)
1660 one
= sval_cast(type
, one
);
1661 two
= sval_cast(type
, two
);
1663 if (type_unsigned(type
)) {
1664 if (one
.uvalue
< two
.uvalue
)
1666 if (one
.uvalue
== two
.uvalue
)
1670 /* fix me handle type promotion and unsigned values */
1671 if (one
.value
< two
.value
)
1673 if (one
.value
== two
.value
)
1678 #endif /* !SMATCH_H_ */