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 bool (bool_stmt_func
)(struct statement
*stmt
);
169 typedef void (sym_func
)(struct symbol
*sym
);
170 typedef void (name_sym_hook
)(struct expression
*expr
, const char *name
, struct symbol
*sym
);
171 typedef void (sm_hook
)(struct sm_state
*sm
, struct expression
*mod_expr
);
172 typedef void (string_hook
)(struct expression
*expr
, const char *str
);
173 DECLARE_PTR_LIST(void_fn_list
, void_fn
);
174 DECLARE_PTR_LIST(expr_fn_list
, expr_func
);
175 DECLARE_PTR_LIST(stmt_fn_list
, stmt_func
);
176 DECLARE_PTR_LIST(bool_stmt_fn_list
, bool_stmt_func
);
177 DECLARE_PTR_LIST(sym_fn_list
, sym_func
);
178 DECLARE_PTR_LIST(name_sym_fn_list
, name_sym_hook
);
179 DECLARE_PTR_LIST(string_hook_list
, string_hook
);
180 void call_void_fns(struct void_fn_list
*list
);
181 void call_expr_fns(struct expr_fn_list
*list
, struct expression
*expr
);
182 void call_stmt_fns(struct stmt_fn_list
*list
, struct statement
*stmt
);
183 void call_sym_fns(struct sym_fn_list
*list
, struct symbol
*sym
);
184 void call_name_sym_fns(struct name_sym_fn_list
*list
, struct expression
*expr
, const char *name
, struct symbol
*sym
);
185 void call_string_hooks(struct string_hook_list
*list
, struct expression
*expr
, const char *str
);
187 void add_dereference_hook(expr_func
*fn
);
189 struct allocation_info
{
191 const char *size_str
;
192 struct expression
*total_size
;
193 struct expression
*nr_elems
;
194 struct expression
*elem_size
;
197 bool safe
; /* safe from overflows */
199 typedef void (alloc_hook
)(struct expression
*expr
, const char *name
, struct symbol
*sym
, struct allocation_info
*info
);
200 void add_allocation_hook(alloc_hook
*func
);
202 void add_hook(void *func
, enum hook_type type
);
203 typedef struct smatch_state
*(merge_func_t
)(struct smatch_state
*s1
, struct smatch_state
*s2
);
204 typedef struct smatch_state
*(unmatched_func_t
)(struct sm_state
*state
);
205 void add_merge_hook(int client_id
, merge_func_t
*func
);
206 void add_unmatched_state_hook(int client_id
, unmatched_func_t
*func
);
207 void add_pre_merge_hook(int client_id
, void (*hook
)(struct sm_state
*cur
, struct sm_state
*other
));
208 typedef void (scope_hook
)(void *data
);
209 void add_scope_hook(scope_hook
*hook
, void *data
);
210 void add_return_string_hook(string_hook
*fn
);
211 typedef void (param_key_hook
)(struct expression
*expr
, const char *name
, struct symbol
*sym
, void *data
);
212 typedef void (func_hook
)(const char *fn
, struct expression
*expr
, void *data
);
213 typedef void (implication_hook
)(const char *fn
, struct expression
*call_expr
,
214 struct expression
*assign_expr
, void *data
);
215 typedef void (return_implies_hook
)(struct expression
*call_expr
,
216 int param
, char *key
, char *value
);
217 typedef int (implied_return_hook
)(struct expression
*call_expr
, void *info
, struct range_list
**rl
);
218 void add_function_hook_early(const char *look_for
, func_hook
*call_back
, void *data
);
219 void add_function_hook(const char *look_for
, func_hook
*call_back
, void *data
);
220 void add_function_hook_late(const char *look_for
, func_hook
*call_back
, void *info
);
222 void add_function_assign_hook(const char *look_for
, func_hook
*call_back
,
224 void register_func_hooks_from_file(const char *file
,
225 func_hook
*call_back
, void *info
);
226 void register_assign_hooks_from_file(const char *file
,
227 func_hook
*call_back
, void *info
);
228 void add_implied_return_hook(const char *look_for
,
229 implied_return_hook
*call_back
,
231 void add_macro_assign_hook(const char *look_for
, func_hook
*call_back
,
233 void add_macro_assign_hook_extra(const char *look_for
, func_hook
*call_back
,
235 void return_implies_state(const char *look_for
, long long start
, long long end
,
236 implication_hook
*call_back
, void *info
);
237 void return_implies_state_sval(const char *look_for
, sval_t start
, sval_t end
,
238 implication_hook
*call_back
, void *info
);
239 void return_implies_exact(const char *look_for
, sval_t start
, sval_t end
,
240 implication_hook
*call_back
, void *info
);
241 struct range_list
*get_range_implications(const char *fn
);
242 void select_return_states_hook(int type
, return_implies_hook
*callback
);
243 void select_return_states_before(void (*fn
)(void));
244 void select_return_states_after(void (*fn
)(void));
245 void add_param_key_expr_hook(const char *look_for
, expr_func
*call_back
,
246 int param
, const char *key
, void *info
);
247 void add_function_param_key_hook(const char *look_for
, param_key_hook
*call_back
,
248 int param
, const char *key
, void *info
);
249 void add_function_param_key_hook_early(const char *look_for
, param_key_hook
*call_back
,
250 int param
, const char *key
, void *info
);
251 void add_function_param_key_hook_late(const char *look_for
, param_key_hook
*call_back
,
252 int param
, const char *key
, void *info
);
253 void return_implies_param_key(const char *look_for
, sval_t start
, sval_t end
,
254 param_key_hook
*call_back
,
255 int param
, const char *key
, void *info
);
256 void return_implies_param_key_exact(const char *look_for
, sval_t start
, sval_t end
,
257 param_key_hook
*call_back
,
258 int param
, const char *key
, void *info
);
259 void return_implies_param_key_expr(const char *look_for
, sval_t start
, sval_t end
,
260 expr_func
*call_back
,
261 int param
, const char *key
, void *info
);
262 void select_return_param_key(int type
, param_key_hook
*callback
);
263 bool get_implied_return(struct expression
*expr
, struct range_list
**rl
);
264 void allocate_hook_memory(void);
265 void allocate_tracker_array(int num_checks
);
267 struct modification_data
{
268 struct smatch_state
*prev
;
269 struct expression
*cur
;
272 void allocate_modification_hooks(void);
273 bool is_sub_member(const char *name
, struct symbol
*sym
, struct sm_state
*sm
);
274 void add_all_modifications_hook(int owner
, name_sym_hook
*hook
);
275 void add_modification_hook(int owner
, sm_hook
*call_back
);
276 void add_modification_hook_late(int owner
, sm_hook
*call_back
);
277 struct smatch_state
*get_modification_state(struct expression
*expr
);
279 int outside_of_function(void);
280 const char *get_filename(void);
281 extern int base_file_stream
;
282 const char *get_base_file(void);
283 unsigned long long get_file_id(void);
284 unsigned long long get_base_file_id(void);
285 char *get_function(void);
286 extern int __smatch_lineno
;
287 int get_lineno(void);
288 extern int final_pass
;
289 extern struct symbol
*cur_func_sym
;
290 extern int option_debug
;
291 extern int local_debug
;
293 extern bool implied_debug
;
294 bool debug_implied(void);
295 bool debug_on(const char *check_name
, const char *var
);
296 extern int option_info
;
297 extern int option_spammy
;
298 extern int option_pedantic
;
299 extern int option_print_names
;
300 extern char *trace_variable
;
301 extern struct stree
*global_states
;
302 void set_function_skipped(void);
303 int is_skipped_function(void);
304 int is_silenced_function(void);
305 extern bool implications_off
;
307 /* smatch_impossible.c */
308 int is_impossible_path(void);
309 void set_true_path_impossible(void);
310 void set_false_path_impossible(void);
311 void set_path_impossible(void);
313 extern FILE *sm_outfd
;
314 extern FILE *sql_outfd
;
315 extern FILE *caller_info_fd
;
316 extern int sm_nr_checks
;
317 extern int sm_nr_errors
;
320 * How to use these routines:
322 * sm_fatal(): an internal error of some kind that should immediately exit
323 * sm_ierror(): an internal error
324 * sm_perror(): an internal error from parsing input source
325 * sm_error(): an error from input source
326 * sm_warning(): a warning from input source
327 * sm_info(): info message (from option_info)
328 * sm_debug(): debug message
329 * sm_msg(): other message (please avoid using this)
332 #define sm_printf(msg...) do { \
333 if (final_pass || option_debug || local_debug || debug_db) \
334 fprintf(sm_outfd, msg); \
337 static inline void sm_prefix(void)
339 sm_printf("%s:%d %s() ", get_filename(), get_lineno(), get_function());
340 if (option_info
|| !option_print_names
)
342 sm_printf("[smatch.%s] ", __CHECKNAME__
);
345 static inline void print_implied_debug_msg();
347 extern bool __silence_warnings_for_stmt
;
349 #define sm_print_msg(type, msg...) \
351 print_implied_debug_msg(); \
352 if (!final_pass && !option_debug && !local_debug && !debug_db) \
354 if (__silence_warnings_for_stmt && !option_debug && !local_debug) \
356 if (!option_info && is_silenced_function()) \
360 sm_printf("warn: "); \
362 } else if (type == 2) { \
363 sm_printf("error: "); \
365 } else if (type == 3) { \
366 sm_printf("parse error: "); \
368 } else if (type == 4) { \
369 sm_printf("pedantic: "); \
375 #define sm_msg(msg...) do { sm_print_msg(0, msg); } while (0)
377 extern char *implied_debug_msg
;
378 static inline void print_implied_debug_msg(void)
380 static struct symbol
*last_printed
= NULL
;
382 if (!implied_debug_msg
)
384 if (last_printed
== cur_func_sym
)
386 last_printed
= cur_func_sym
;
387 sm_msg("%s", implied_debug_msg
);
390 #define sm_debug(msg...) do { if (option_debug) sm_printf(msg); } while (0)
391 #define db_debug(msg...) do { if (option_debug || debug_db) sm_printf(msg); } while (0)
393 #define sm_info(msg...) do { \
394 if (option_debug || (option_info && final_pass)) { \
396 sm_printf("info: "); \
402 #define sm_warning(msg...) do { sm_print_msg(1, msg); } while (0)
403 #define sm_warning_line(line, msg...) do { \
404 int __orig = __smatch_lineno; \
405 __smatch_lineno = line; \
406 sm_print_msg(1, msg); \
407 __smatch_lineno = __orig; \
409 #define sm_error(msg...) do { sm_print_msg(2, msg); } while (0)
410 #define sm_perror(msg...) do { sm_print_msg(3, msg); } while (0)
411 #define sm_pedantic(msg...) do { if (option_pedantic) sm_print_msg(4, msg); } while (0)
413 static inline void sm_fatal(const char *fmt
, ...)
418 vfprintf(sm_outfd
, fmt
, args
);
421 fprintf(sm_outfd
, "\n");
426 static inline void sm_ierror(const char *fmt
, ...)
432 fprintf(sm_outfd
, "internal error: ");
435 vfprintf(sm_outfd
, fmt
, args
);
438 fprintf(sm_outfd
, "\n");
440 #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
442 bool has_states(struct stree
*stree
, int owner
);
443 struct smatch_state
*__get_state(int owner
, const char *name
, struct symbol
*sym
);
444 struct smatch_state
*get_state(int owner
, const char *name
, struct symbol
*sym
);
445 struct smatch_state
*get_state_expr(int owner
, struct expression
*expr
);
446 bool has_possible_state(int owner
, const char *name
, struct symbol
*sym
, struct smatch_state
*state
);
447 bool expr_has_possible_state(int owner
, struct expression
*expr
, struct smatch_state
*state
);
448 struct state_list
*get_possible_states(int owner
, const char *name
,
450 struct state_list
*get_possible_states_expr(int owner
, struct expression
*expr
);
451 struct sm_state
*set_state(int owner
, const char *name
, struct symbol
*sym
,
452 struct smatch_state
*state
);
453 struct sm_state
*set_state_expr(int owner
, struct expression
*expr
,
454 struct smatch_state
*state
);
455 void preserve_out_of_scope(int owner
);
456 void __delete_state(int owner
, const char *name
, struct symbol
*sym
);
457 void __delete_all_states_sym(struct symbol
*sym
);
458 void set_true_false_states(int owner
, const char *name
, struct symbol
*sym
,
459 struct smatch_state
*true_state
,
460 struct smatch_state
*false_state
);
461 void set_true_false_states_expr(int owner
, struct expression
*expr
,
462 struct smatch_state
*true_state
,
463 struct smatch_state
*false_state
);
465 struct stree
*get_all_states_from_stree(int owner
, struct stree
*source
);
466 struct stree
*get_all_states_stree(int id
);
467 struct stree
*__get_cur_stree(void);
468 int is_reachable(void);
469 void add_get_state_hook(void (*fn
)(int owner
, const char *name
, struct symbol
*sym
));
471 static inline void set_undefined(struct sm_state
*sm
, struct expression
*mod_expr
)
473 set_state(sm
->owner
, sm
->name
, sm
->sym
, &undefined
);
477 char *ssa_name(const char *name
);
478 void set_ssa_state(int owner
, const char *name
, struct symbol
*sym
, struct smatch_state
*state
);
479 void update_ssa_state(int owner
, const char *name
, struct symbol
*sym
,
480 struct smatch_state
*state
);
481 void update_ssa_sm(int owner
, struct sm_state
*sm
, struct smatch_state
*state
);
482 void set_ssa_state_expr(int owner
, struct expression
*expr
, struct smatch_state
*state
);
483 struct sm_state
*get_ssa_sm_state(int owner
, const char *name
, struct symbol
*sym
);
484 struct sm_state
*get_ssa_sm_state_expr(int owner
, struct expression
*expr
);
485 struct smatch_state
*get_ssa_state(int owner
, const char *name
, struct symbol
*sym
);
486 struct smatch_state
*get_ssa_state_expr(int owner
, struct expression
*expr
);
488 /* smatch_helper.c */
489 DECLARE_PTR_LIST(int_stack
, int);
490 char *alloc_string(const char *str
);
491 char *alloc_string_newline(const char *str
);
492 void free_string(char *str
);
493 void remove_parens(char *str
);
494 struct smatch_state
*alloc_state_num(int num
);
495 struct smatch_state
*alloc_state_str(const char *name
);
496 struct smatch_state
*merge_str_state(struct smatch_state
*s1
, struct smatch_state
*s2
);
497 struct smatch_state
*alloc_state_expr(struct expression
*expr
);
498 struct expression
*get_assigned_call(struct expression
*expr
);
499 struct expression
*get_argument_from_call_expr(struct expression_list
*args
,
501 struct expression
*get_array_expr(struct expression
*expr
);
503 char *expr_to_var(struct expression
*expr
);
504 struct symbol
*expr_to_sym(struct expression
*expr
);
505 char *expr_to_str(struct expression
*expr
);
506 char *expr_to_str_sym(struct expression
*expr
,
507 struct symbol
**sym_ptr
);
508 char *expr_to_var_sym(struct expression
*expr
,
509 struct symbol
**sym_ptr
);
510 char *expr_to_known_chunk_sym(struct expression
*expr
, struct symbol
**sym
);
511 char *expr_to_chunk_sym_vsl(struct expression
*expr
, struct symbol
**sym
, struct var_sym_list
**vsl
);
512 int get_complication_score(struct expression
*expr
);
514 int sym_name_is(const char *name
, struct expression
*expr
);
515 int get_const_value(struct expression
*expr
, sval_t
*sval
);
516 int get_value(struct expression
*expr
, sval_t
*val
);
517 int get_implied_value(struct expression
*expr
, sval_t
*val
);
518 int get_implied_value_fast(struct expression
*expr
, sval_t
*sval
);
519 int get_implied_min(struct expression
*expr
, sval_t
*sval
);
520 int get_implied_max(struct expression
*expr
, sval_t
*val
);
521 int get_hard_max(struct expression
*expr
, sval_t
*sval
);
522 int get_fuzzy_min(struct expression
*expr
, sval_t
*min
);
523 int get_fuzzy_max(struct expression
*expr
, sval_t
*max
);
524 int get_absolute_min(struct expression
*expr
, sval_t
*sval
);
525 int get_absolute_max(struct expression
*expr
, sval_t
*sval
);
526 int parse_call_math(struct expression
*expr
, char *math
, sval_t
*val
);
527 int parse_call_math_rl(struct expression
*call
, const char *math
, struct range_list
**rl
);
528 const char *get_allocation_math(struct expression
*expr
);
529 char *get_value_in_terms_of_parameter_math(struct expression
*expr
);
530 char *get_value_in_terms_of_parameter_math_var_sym(const char *var
, struct symbol
*sym
);
531 int expr_is_zero(struct expression
*expr
);
532 int known_condition_true(struct expression
*expr
);
533 int known_condition_false(struct expression
*expr
);
534 int implied_condition_true(struct expression
*expr
);
535 int implied_condition_false(struct expression
*expr
);
536 int can_integer_overflow(struct symbol
*type
, struct expression
*expr
);
537 void clear_math_cache(void);
538 void clear_strip_cache(void);
539 void set_fast_math_only(void);
540 void clear_fast_math_only(void);
542 int is_array(struct expression
*expr
);
543 struct expression
*get_array_base(struct expression
*expr
);
544 struct expression
*get_array_offset(struct expression
*expr
);
545 const char *show_state(struct smatch_state
*state
);
546 struct statement
*get_expression_statement(struct expression
*expr
);
547 struct expression
*strip__builtin_choose_expr(struct expression
*expr
);
548 struct expression
*strip_Generic(struct expression
*expr
);
549 struct expression
*strip_parens(struct expression
*expr
);
550 struct expression
*strip_expr(struct expression
*expr
);
551 struct expression
*strip_no_cast(struct expression
*expr
);
552 struct expression
*strip_expr_set_parent(struct expression
*expr
);
553 int is_error_return(struct expression
*expr
);
554 int getting_address(struct expression
*expr
);
555 int get_struct_and_member(struct expression
*expr
, const char **type
, const char **member
);
556 char *get_member_name(struct expression
*expr
);
557 char *get_fnptr_name(struct expression
*expr
);
558 int cmp_pos(struct position pos1
, struct position pos2
);
559 int positions_eq(struct position pos1
, struct position pos2
);
560 struct statement
*get_current_statement(void);
561 struct statement
*get_prev_statement(void);
562 struct expression
*get_last_expr_from_expression_stmt(struct expression
*expr
);
564 enum { RET_SUCCESS
, RET_FAIL
, RET_UNKNOWN
};
565 int success_fail_return(struct range_list
*rl
);
567 #define RETURN_VAR -1
568 #define LOCAL_SCOPE -2
569 #define FILE_SCOPE -3
570 #define GLOBAL_SCOPE -4
571 #define UNKNOWN_SCOPE -5
573 char *swap_names(const char *orig
, const char *remove
, const char *add
);
574 char *get_param_var_sym_var_sym(const char *name
, struct symbol
*sym
, struct expression
*ret_expr
, struct symbol
**sym_p
);
575 char *get_param_name_sym(struct expression
*expr
, struct symbol
**sym_p
);
576 int get_return_param_key_from_var_sym(const char *name
, struct symbol
*sym
,
577 struct expression
*ret_expr
,
579 int get_param_key_from_var_sym(const char *name
, struct symbol
*sym
,
580 struct expression
*ret_expr
,
582 int get_param_key_from_sm(struct sm_state
*sm
, struct expression
*ret_expr
,
584 int get_param_key_from_expr(struct expression
*expr
, struct expression
*ret_expr
,
586 const char *get_param_key_swap_dollar(struct expression
*expr
);
587 int map_to_param(const char *name
, struct symbol
*sym
);
588 int get_param_num_from_sym(struct symbol
*sym
);
589 int get_param_num(struct expression
*expr
);
590 struct symbol
*get_param_sym_from_num(int num
);
591 struct expression
*map_container_of_to_simpler_expr_key(struct expression
*expr
, const char *orig_key
, char **new_key
);
592 const char *get_container_of_str(struct expression
*expr
);
593 bool get_offset_param(const char *ret_str
, int *offset
, int *param
);
594 /* smatch_points_to_container.c */
595 struct expression
*get_stored_container(struct expression
*expr
, int offset
);
597 int ms_since(struct timeval
*start
);
598 int parent_is_gone_var_sym(const char *name
, struct symbol
*sym
);
599 int parent_is_gone(struct expression
*expr
);
600 bool is_noderef_ptr(struct expression
*expr
);
601 int invert_op(int op
);
602 int op_remove_assign(int op
);
603 int expr_equiv(struct expression
*one
, struct expression
*two
);
604 void push_int(struct int_stack
**stack
, int num
);
605 int pop_int(struct int_stack
**stack
);
606 bool macro_to_ul(const char *macro
, unsigned long *val
);
607 bool has_cleanup(struct expression
*expr
);
610 struct symbol
*get_real_base_type(struct symbol
*sym
);
611 int type_bytes(struct symbol
*type
);
612 int array_bytes(struct symbol
*type
);
613 struct symbol
*get_pointer_type(struct expression
*expr
);
614 struct symbol
*get_type(struct expression
*expr
);
615 struct symbol
*get_comparison_type(struct expression
*expr
);
616 struct symbol
*get_final_type(struct expression
*expr
);
617 struct symbol
*get_promoted_type(struct symbol
*left
, struct symbol
*right
);
618 int type_signed(struct symbol
*base_type
);
619 int expr_unsigned(struct expression
*expr
);
620 int expr_signed(struct expression
*expr
);
621 int returns_unsigned(struct symbol
*base_type
);
622 int is_pointer(struct expression
*expr
);
623 bool is_void_ptr(struct symbol
*type
);
624 int returns_pointer(struct symbol
*base_type
);
625 sval_t
sval_type_max(struct symbol
*base_type
);
626 sval_t
sval_type_min(struct symbol
*base_type
);
627 int nr_bits(struct expression
*expr
);
628 int is_void_pointer(struct expression
*expr
);
629 int is_char_pointer(struct expression
*expr
);
630 int is_string(struct expression
*expr
);
631 bool is_struct_ptr(struct symbol
*type
);
632 int is_static(struct expression
*expr
);
633 bool is_local_variable(struct expression
*expr
);
634 int types_equiv(struct symbol
*one
, struct symbol
*two
);
635 bool type_fits(struct symbol
*type
, struct symbol
*test
);
637 const char *global_static();
638 struct symbol
*cur_func_return_type(void);
639 struct symbol
*get_arg_type(struct expression
*fn
, int arg
);
640 struct symbol
*get_member_type_from_key(struct expression
*expr
, const char *key
);
641 struct symbol
*get_arg_type_from_key(struct expression
*fn
, int param
, struct expression
*arg
, const char *key
);
642 int is_struct(struct expression
*expr
);
643 char *type_to_str(struct symbol
*type
);
645 /* smatch_ignore.c */
646 void add_ignore(int owner
, const char *name
, struct symbol
*sym
);
647 int is_ignored(int owner
, const char *name
, struct symbol
*sym
);
648 void add_ignore_expr(int owner
, struct expression
*expr
);
649 int is_ignored_expr(int owner
, struct expression
*expr
);
652 struct smatch_state
*alloc_var_sym_state(const char *var
, struct symbol
*sym
);
653 struct var_sym
*alloc_var_sym(const char *var
, struct symbol
*sym
);
654 struct var_sym_list
*expr_to_vsl(struct expression
*expr
);
655 void add_var_sym(struct var_sym_list
**list
, const char *var
, struct symbol
*sym
);
656 void add_var_sym_expr(struct var_sym_list
**list
, struct expression
*expr
);
657 void del_var_sym(struct var_sym_list
**list
, const char *var
, struct symbol
*sym
);
658 int in_var_sym_list(struct var_sym_list
*list
, const char *var
, struct symbol
*sym
);
659 struct var_sym_list
*clone_var_sym_list(struct var_sym_list
*from_vsl
);
660 void merge_var_sym_list(struct var_sym_list
**dest
, struct var_sym_list
*src
);
661 struct var_sym_list
*combine_var_sym_lists(struct var_sym_list
*one
, struct var_sym_list
*two
);
662 int var_sym_lists_equiv(struct var_sym_list
*one
, struct var_sym_list
*two
);
663 void free_var_sym_list(struct var_sym_list
**list
);
664 void free_var_syms_and_list(struct var_sym_list
**list
);
667 struct tracker
*alloc_tracker(int owner
, const char *name
, struct symbol
*sym
);
668 void add_tracker(struct tracker_list
**list
, int owner
, const char *name
,
670 void add_tracker_expr(struct tracker_list
**list
, int owner
, struct expression
*expr
);
671 void del_tracker(struct tracker_list
**list
, int owner
, const char *name
,
673 int in_tracker_list(struct tracker_list
*list
, int owner
, const char *name
,
675 void free_tracker_list(struct tracker_list
**list
);
676 void free_trackers_and_list(struct tracker_list
**list
);
678 /* smatch_conditions */
679 int in_condition(void);
683 extern int __in_fake_assign
;
684 extern int __in_fake_parameter_assign
;
685 extern int __in_fake_struct_assign
;
686 extern int __in_buf_clear
;
687 extern int __in_fake_var_assign
;
688 extern int __in_builtin_overflow_func
;
689 extern int __fake_state_cnt
;
690 extern int __debug_skip
;
691 extern int in_fake_env
;
692 bool is_fake_var_assign(struct expression
*expr
);
693 void smatch (struct string_list
*filelist
);
694 int inside_loop(void);
695 int definitely_inside_loop(void);
696 void add_once_through_hook(bool_stmt_func
*fn
);
697 struct expression
*get_switch_expr(void);
698 int in_expression_statement(void);
699 void __process_post_op_stack(void);
700 void parse_assignment(struct expression
*expr
, bool shallow
);
701 void __split_expr(struct expression
*expr
);
702 void __split_label_stmt(struct statement
*stmt
);
703 void __split_stmt(struct statement
*stmt
);
704 extern int __in_function_def
;
705 extern int __in_unmatched_hook
;
706 extern int option_assume_loops
;
707 extern int option_two_passes
;
708 extern int option_no_db
;
709 extern int option_file_output
;
710 extern int option_time
;
711 extern int option_time_stmt
;
712 extern struct expression_list
*big_expression_stack
;
713 extern struct expression_list
*big_condition_stack
;
714 extern struct statement_list
*big_statement_stack
;
715 int is_condition_call(struct expression
*expr
);
716 int is_assigned_call(struct expression
*expr
);
717 int is_fake_assigned_call(struct expression
*expr
);
718 void add_function_data(unsigned long *fn_data
);
719 int inlinable(struct expression
*expr
);
720 extern int __inline_call
;
721 extern struct expression
*__inline_fn
;
722 extern int __in_pre_condition
;
723 extern int __bail_on_rest_of_function
;
724 extern struct statement
*__prev_stmt
;
725 extern struct statement
*__cur_stmt
;
726 extern struct statement
*__next_stmt
;
727 void init_fake_env(void);
728 void end_fake_env(void);
729 int time_parsing_function(void);
730 bool taking_too_long(void);
731 struct statement
*get_last_stmt(void);
732 int is_last_stmt(struct statement
*cur_stmt
);
734 /* smatch_struct_assignment.c */
735 struct expression
*get_faked_expression(void);
736 void __fake_struct_member_assignments(struct expression
*expr
);
737 void create_recursive_fake_assignments(struct expression
*expr
,
738 void (*assign_handler
)(struct expression
*expr
, void *data
),
741 /* smatch_project.c */
742 int is_no_inline_function(const char *function
);
744 /* smatch_conditions */
745 void __set_confidence_implied(void);
746 void __unset_confidence();
747 void __split_whole_condition_tf(struct expression
*expr
, int *known_tf
);
748 void __split_whole_condition(struct expression
*expr
);
749 void __handle_logic(struct expression
*expr
);
750 int is_condition(struct expression
*expr
);
751 int __handle_condition_assigns(struct expression
*expr
);
752 int __handle_select_assigns(struct expression
*expr
);
753 int __handle_expr_statement_assigns(struct expression
*expr
);
755 /* smatch_implied.c */
756 struct range_list_stack
;
757 void turn_off_implications(int id
);
758 void param_limit_implications(struct expression
*expr
, int param
, char *key
, char *value
, struct stree
**implied
);
759 struct stree
*__implied_case_stree(struct expression
*switch_expr
,
760 struct range_list
*case_rl
,
761 struct range_list_stack
**remaining_cases
,
762 struct stree
**raw_stree
);
763 void overwrite_states_using_pool(struct sm_state
*gate_sm
, struct sm_state
*pool_sm
);
764 int assume(struct expression
*expr
);
765 void end_assume(void);
766 int impossible_assumption(struct expression
*left
, int op
, sval_t sval
);
769 bool has_dynamic_states(unsigned short owner
);
770 void set_dynamic_states(unsigned short owner
);
772 /* smatch_extras.c */
773 int in_warn_on_macro(void);
774 extern int SMATCH_EXTRA
;
775 extern int RETURN_ID
;
782 char *get_other_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
);
783 char *map_call_to_other_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
);
784 char *map_long_to_short_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
, bool use_stack
);
786 #define STRLEN_MAX_RET 1010101
788 /* smatch_absolute.c */
789 int get_absolute_min_helper(struct expression
*expr
, sval_t
*sval
);
790 int get_absolute_max_helper(struct expression
*expr
, sval_t
*sval
);
792 /* smatch_type_value.c */
793 void disable_type_val_lookups(void);
794 void enable_type_val_lookups(void);
795 void clear_type_value_cache(void);
796 int get_db_type_rl(struct expression
*expr
, struct range_list
**rl
);
798 /* smatch_data_val.c */
799 int get_mtag_rl(struct expression
*expr
, struct range_list
**rl
);
800 /* smatch_array_values.c */
801 void clear_array_values_cache(void);
802 int get_array_rl(struct expression
*expr
, struct range_list
**rl
);
804 /* smatch_states.c */
805 struct stree
*__swap_cur_stree(struct stree
*stree
);
806 void __push_fake_cur_stree();
807 struct stree
*__pop_fake_cur_stree();
808 void __free_fake_cur_stree();
809 void __set_fake_cur_stree_fast(struct stree
*stree
);
810 void __pop_fake_cur_stree_fast(void);
811 void __merge_stree_into_cur(struct stree
*stree
);
813 int unreachable(void);
814 void __set_cur_stree_readonly(void);
815 void __set_cur_stree_writable(void);
816 void __set_sm(struct sm_state
*sm
);
817 void __set_sm_cur_stree(struct sm_state
*sm
);
818 void __set_true_false_sm(struct sm_state
*true_state
,
819 struct sm_state
*false_state
);
820 void nullify_path(void);
821 void __match_nullify_path_hook(const char *fn
, struct expression
*expr
,
823 void __unnullify_path(void);
824 int __path_is_null(void);
825 void save_all_states(void);
826 void restore_all_states(void);
827 void free_goto_stack(void);
828 void clear_all_states(void);
830 struct sm_state
*get_sm_state(int owner
, const char *name
,
832 struct sm_state
*get_sm_state_expr(int owner
, struct expression
*expr
);
833 void __push_true_states(void);
834 void __use_false_states(void);
835 void __discard_false_states(void);
836 void __merge_false_states(void);
837 void __merge_true_states(void);
839 void __negate_cond_stacks(void);
840 void __use_pre_cond_states(void);
841 void __use_cond_true_states(void);
842 void __use_cond_false_states(void);
843 void __push_cond_stacks(void);
844 void __fold_in_set_states(void);
845 void __free_set_states(void);
846 struct stree
*__copy_cond_true_states(void);
847 struct stree
*__copy_cond_false_states(void);
848 struct stree
*__pop_cond_true_stack(void);
849 struct stree
*__pop_cond_false_stack(void);
850 void __and_cond_states(void);
851 void __or_cond_states(void);
852 void __save_pre_cond_states(void);
853 void __discard_pre_cond_states(void);
854 struct stree
*__get_true_states(void);
855 struct stree
*__get_false_states(void);
856 void __use_cond_states(void);
857 extern struct state_list
*__last_base_slist
;
859 void __push_continues(void);
860 void __discard_continues(void);
861 void __process_continues(void);
862 void __merge_continues(void);
864 void __push_breaks(void);
865 void __process_breaks(void);
866 int __has_breaks(void);
867 void __merge_breaks(void);
868 void __use_breaks(void);
870 void __save_switch_states(struct expression
*switch_expr
);
871 void __discard_switches(void);
872 int have_remaining_cases(void);
873 void __merge_switches(struct expression
*switch_expr
, struct range_list
*case_rl
);
874 void __push_default(void);
875 void __set_default(void);
876 bool __has_default_case(void);
877 int __pop_default(void);
879 void __push_conditions(void);
880 void __discard_conditions(void);
882 void __save_gotos(const char *name
, struct symbol
*sym
);
883 void __merge_gotos(const char *name
, struct symbol
*sym
);
885 void __discard_fake_states(struct expression
*call
);
887 void __print_cur_stree(void);
888 bool __print_states(const char *owner
);
889 typedef void (check_tracker_hook
)(int owner
, const char *name
, struct symbol
*sym
, struct smatch_state
*state
);
890 void add_check_tracker(const char *check_name
, check_tracker_hook
*fn
);
893 void __pass_to_client(void *data
, enum hook_type type
);
894 void __pass_case_to_client(struct expression
*switch_expr
,
895 struct range_list
*rl
);
896 int __has_merge_function(int client_id
);
897 struct smatch_state
*__client_merge_function(int owner
,
898 struct smatch_state
*s1
,
899 struct smatch_state
*s2
);
900 struct smatch_state
*__client_unmatched_state_function(struct sm_state
*sm
);
901 void call_pre_merge_hook(struct sm_state
*cur
, struct sm_state
*other
);
902 void __push_scope_hooks(void);
903 void __call_scope_hooks(void);
904 void __free_scope_hooks(void);
905 void __call_all_scope_hooks(void);
906 void add_array_initialized_hook(void (*hook
)(struct expression
*array
, int nr
));
907 void __call_array_initialized_hooks(struct expression
*array
, int nr
);
909 /* smatch_function_hooks.c */
910 void add_fake_call_after_return(struct expression
*call
);
911 void create_function_hook_hash(void);
912 void __match_initializer_call(struct symbol
*sym
);
914 struct expression
*get_unfaked_call(void);
915 void fake_param_assign_helper(struct expression
*call
, struct expression
*fake_assign
, bool shallow
);
921 * Changing these numbers is a pain. Don't do it. If you ever use a
922 * number it can't be re-used right away so there may be gaps.
923 * We select these in order by type so if the order matters, then give
924 * it a number below 100-999,9000-9999 ranges. */
939 ABSOLUTE_LIMITS
= 1010,
953 UNTRACKED_PARAM
= 1023,
959 COMPARE_LIMIT
= 1028,
960 PARAM_COMPARE
= 1029,
963 CONSTRAINT_REQUIRED
= 1033,
974 POWER_OF_TWO_SET
= 1049,
980 NEGATIVE_ERROR
= 1057,
988 TASK_NOT_RUNNING
= 2064,
989 /* put random temporary stuff in the 7000-7999 range for testing */
991 HOST_DATA_SET
= 7017,
995 USER_DATA_SET
= 9017,
999 NO_OVERFLOW_SIMPLE
= 8019,
1004 KNOWN_LOCKED
= 9024,
1005 KNOWN_UNLOCKED
= 9025,
1010 REFCOUNT_INIT
= 9025,
1011 REFCOUNT_INC
= 9027,
1012 REFCOUNT_DEC
= 9028,
1013 NO_SIDE_EFFECT
= 8025,
1016 ARRAYSIZE_ARG
= 8033,
1021 STRING_VALUE
= 8041,
1030 /* Do not use numbers above 100k */
1033 extern struct sqlite3
*smatch_db
;
1034 extern struct sqlite3
*mem_db
;
1035 extern struct sqlite3
*cache_db
;
1037 bool db_incomplete(void);
1038 void db_ignore_states(int id
);
1039 typedef bool (delete_hook
)(struct expression
*expr
);
1040 void add_delete_return_hook(delete_hook
*hook
);
1041 void select_caller_info_hook(void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
), int type
);
1042 void select_caller_name_sym(void (*fn
)(const char *name
, struct symbol
*sym
, char *value
), int type
);
1043 void add_member_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
));
1044 void add_caller_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
));
1045 void add_return_info_callback(int owner
,
1046 void (*callback
)(int return_id
, char *return_ranges
,
1047 struct expression
*returned_expr
,
1049 const char *printed_name
,
1050 struct sm_state
*sm
));
1051 void add_split_return_callback(void (*fn
)(int return_id
, char *return_ranges
, struct expression
*returned_expr
));
1052 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
));
1053 void select_call_implies_hook(int type
, void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
));
1054 void select_return_implies_hook_early(int type
, void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
));
1055 void select_return_implies_hook(int type
, void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
));
1056 struct range_list
*db_return_vals(struct expression
*expr
);
1057 struct range_list
*db_return_vals_from_str(const char *fn_name
);
1058 struct range_list
*db_return_vals_no_args(struct expression
*expr
);
1059 bool split_param_key(const char *value
, int *param
, char *key
, int len
);
1060 bool get_implied_rl_from_call_str(struct expression
*expr
, const char *data
, struct range_list
**rl
);
1061 char *get_chunk_from_key(struct expression
*arg
, char *key
, struct symbol
**sym
, struct var_sym_list
**vsl
);
1062 char *get_variable_from_key(struct expression
*arg
, const char *key
, struct symbol
**sym
);
1063 struct expression
*get_function_param(struct expression
*expr
, int param
);
1064 char *get_name_sym_from_param_key(struct expression
*expr
, int param
, const char *key
, struct symbol
**sym
);
1065 char *get_param_name_var_sym(const char *name
, struct symbol
*sym
);
1066 char *get_param_name(struct sm_state
*sm
);
1067 const char *get_mtag_name_var_sym(const char *state_name
, struct symbol
*sym
);
1068 const char *get_mtag_name_expr(struct expression
*expr
);
1069 char *get_data_info_name(struct expression
*expr
);
1070 char *sm_to_arg_name(struct expression
*expr
, struct sm_state
*sm
);
1071 int is_recursive_member(const char *param_name
);
1073 char *escape_newlines(const char *str
);
1074 void sql_exec(struct sqlite3
*db
, int (*callback
)(void*, int, char**, char**), void *data
, const char *sql
);
1076 #define sql_helper(db, call_back, data, sql...) \
1078 char sql_txt[1024]; \
1080 sqlite3_snprintf(sizeof(sql_txt), sql_txt, sql); \
1081 db_debug("debug: %s\n", sql_txt); \
1082 sql_exec(db, call_back, data, sql_txt); \
1086 #define run_sql(call_back, data, sql...) \
1090 sql_helper(smatch_db, call_back, data, sql); \
1093 #define mem_sql(call_back, data, sql...) \
1094 sql_helper(mem_db, call_back, data, sql)
1096 #define cache_sql(call_back, data, sql...) \
1097 sql_helper(cache_db, call_back, data, sql)
1099 #define sql_insert_helper(table, db, ignore, late, values...) \
1101 struct sqlite3 *_db = db; \
1103 if (__inline_fn && !_db) \
1107 char *err, *p = buf; \
1110 p += snprintf(p, buf + sizeof(buf) - p, \
1111 "insert %sinto %s values (", \
1112 ignore ? "or ignore " : "", #table); \
1113 p += snprintf(p, buf + sizeof(buf) - p, values); \
1114 p += snprintf(p, buf + sizeof(buf) - p, ");"); \
1115 db_debug("mem-db: %s\n", buf); \
1116 rc = sqlite3_exec(_db, buf, NULL, NULL, &err); \
1117 if (rc != SQLITE_OK) { \
1118 sm_ierror("SQL error #2: %s", err); \
1119 sm_ierror("SQL: '%s'", buf); \
1124 if (option_info) { \
1125 FILE *tmp_fd = sm_outfd; \
1126 sm_outfd = sql_outfd; \
1128 sm_printf("SQL%s: insert %sinto " #table " values(", \
1129 late ? "_late" : "", ignore ? "or ignore " : ""); \
1130 sm_printf(values); \
1131 sm_printf(");\n"); \
1132 sm_outfd = tmp_fd; \
1136 #define sql_insert(table, values...) sql_insert_helper(table, NULL, 0, 0, values);
1137 #define sql_insert_or_ignore(table, values...) sql_insert_helper(table, NULL, 1, 0, values);
1138 #define sql_insert_late(table, values...) sql_insert_helper(table, NULL, 0, 1, values);
1139 #define sql_insert_cache(table, values...) sql_insert_helper(table, cache_db, 1, 0, values);
1140 #define sql_insert_cache_or_ignore(table, values...) sql_insert_helper(table, cache_db, 1, 0, values);
1142 char *get_static_filter(struct symbol
*sym
);
1144 void sql_insert_return_states(int return_id
, const char *return_ranges
,
1145 int type
, int param
, const char *key
, const char *value
);
1146 void sql_insert_caller_info(struct expression
*call
, int type
, int param
,
1147 const char *key
, const char *value
);
1148 void sql_insert_function_ptr(const char *fn
, const char *struct_name
);
1149 void sql_insert_return_values(const char *return_values
);
1150 void sql_insert_return_implies(int type
, int param
, const char *key
, const char *value
);
1151 void sql_insert_function_type_size(const char *member
, const char *ranges
);
1152 void sql_insert_function_type_info(int type
, const char *struct_type
, const char *member
, const char *value
);
1153 void sql_insert_type_info(int type
, const char *member
, const char *value
);
1154 void sql_insert_local_values(const char *name
, const char *value
);
1155 void sql_insert_function_type_value(const char *type
, const char *value
);
1156 void sql_insert_function_type(int param
, const char *value
);
1157 void sql_insert_parameter_name(int param
, const char *value
);
1158 void sql_insert_data_info(struct expression
*data
, int type
, const char *value
);
1159 void sql_insert_data_info_var_sym(const char *var
, struct symbol
*sym
, int type
, const char *value
);
1160 void sql_save_constraint(const char *con
);
1161 void sql_save_constraint_required(const char *data
, int op
, const char *limit
);
1162 void sql_copy_constraint_required(const char *new_limit
, const char *old_limit
);
1163 void sql_insert_fn_ptr_data_link(const char *ptr
, const char *data
);
1164 void sql_insert_fn_data_link(struct expression
*fn
, int type
, int param
, const char *key
, const char *value
);
1165 void sql_insert_mtag_about(mtag_t tag
, const char *left_name
, const char *right_name
);
1166 void sql_insert_mtag_info(mtag_t tag
, int type
, const char *value
);
1167 void sql_insert_mtag_map(mtag_t container
, int container_offset
, mtag_t tag
, int tag_offset
);
1168 void sql_insert_mtag_alias(mtag_t orig
, mtag_t alias
);
1169 int mtag_map_select_container(mtag_t tag
, int container_offset
, mtag_t
*container
);
1170 int mtag_map_select_tag(mtag_t container
, int offset
, mtag_t
*tag
);
1171 struct smatch_state
*get_mtag_return(struct expression
*expr
, struct smatch_state
*state
);
1172 struct range_list
*swap_mtag_seed(struct expression
*expr
, struct range_list
*rl
);
1174 bool is_fn_ptr(struct expression
*fn
);
1175 void sql_select_return_states(const char *cols
, struct expression
*call
,
1176 int (*callback
)(void*, int, char**, char**), void *info
);
1177 void sql_select_call_implies(const char *cols
, struct expression
*call
,
1178 int (*callback
)(void*, int, char**, char**));
1180 void open_smatch_db(char *db_file
);
1182 /* smatch_files.c */
1183 int open_data_file(const char *filename
);
1184 int open_schema_file(const char *schema
);
1185 struct token
*get_tokens_file(const char *filename
);
1186 struct string_list
*load_strings_from_file(const char *project
, const char *filename
);
1189 extern char *option_debug_check
;
1190 extern char *option_debug_var
;
1191 extern char *option_process_function
;
1192 extern char *option_project_str
;
1193 extern char *bin_dir
;
1194 extern char *data_dir
;
1195 extern int option_no_data
;
1196 extern int option_full_path
;
1197 extern int option_call_tree
;
1198 extern int num_checks
;
1204 PROJ_ILLUMOS_KERNEL
,
1208 extern enum project_type option_project
;
1209 const char *check_name(unsigned short id
);
1210 int id_from_name(const char *name
);
1213 /* smatch_buf_size.c */
1214 int bytes_to_elements(struct expression
*expr
, int bytes
);
1215 int get_array_size(struct expression
*expr
);
1216 int get_array_size_bytes(struct expression
*expr
);
1217 int get_array_size_bytes_min(struct expression
*expr
);
1218 int get_array_size_bytes_max(struct expression
*expr
);
1219 struct range_list
*get_array_size_bytes_rl(struct expression
*expr
);
1220 int get_real_array_size(struct expression
*expr
);
1221 int last_member_is_resizable(struct symbol
*type
);
1222 /* smatch_strlen.c */
1223 bool is_strlen(struct expression
*expr
);
1224 int get_implied_strlen(struct expression
*expr
, struct range_list
**rl
);
1225 int get_size_from_strlen(struct expression
*expr
);
1227 /* smatch_capped.c */
1228 int is_capped(struct expression
*expr
);
1229 int is_capped_var_sym(const char *name
, struct symbol
*sym
);
1231 /* smatch_kernel_user_data.c */
1232 int is_user_macro(struct expression
*expr
);
1233 int is_capped_user_data(struct expression
*expr
);
1234 int implied_user_data(struct expression
*expr
, struct range_list
**rl
);
1235 struct stree
*get_user_stree(void);
1236 int get_user_rl(struct expression
*expr
, struct range_list
**rl
);
1237 int is_user_rl(struct expression
*expr
);
1238 int get_user_rl_var_sym(const char *name
, struct symbol
*sym
, struct range_list
**rl
);
1239 void mark_as_user_data(struct expression
*expr
, bool isnew
);
1240 bool user_rl_capped_var_sym(const char *name
, struct symbol
*sym
);
1241 bool user_rl_capped(struct expression
*expr
);
1242 struct range_list
*var_user_rl(struct expression
*expr
);
1243 bool we_pass_user_data(struct expression
*call
);
1244 /* smatch_points_to_user_data.c */
1245 bool is_user_data_fn(struct symbol
*fn
);
1246 bool is_skb_data(struct expression
*expr
);
1247 bool is_socket_stuff(struct symbol
*sym
);
1248 bool points_to_user_data(struct expression
*expr
);
1249 void set_array_user_ptr(struct expression
*expr
, bool is_new
);
1251 /* smatch_kernel_host_data.c */
1252 int is_host_macro(struct expression
*expr
);
1253 int get_host_data_fn_param(const char *fn
);
1254 int is_capped_host_data(struct expression
*expr
);
1255 int implied_host_data(struct expression
*expr
, struct range_list
**rl
);
1256 struct stree
*get_host_stree(void);
1257 int get_host_rl(struct expression
*expr
, struct range_list
**rl
);
1258 int is_host_rl(struct expression
*expr
);
1259 int get_host_rl_var_sym(const char *name
, struct symbol
*sym
, struct range_list
**rl
);
1260 bool host_rl_capped(struct expression
*expr
);
1261 struct range_list
*var_host_rl(struct expression
*expr
);
1262 bool we_pass_host_data(struct expression
*call
);
1263 /* smatch_points_to_host_data.c */
1264 bool is_host_data_fn(struct symbol
*fn
);
1265 bool points_to_host_data(struct expression
*expr
);
1266 void set_points_to_host_data(struct expression
*expr
, bool is_new
);
1267 bool is_fn_points_to_host_data(const char *fn
);
1268 /* check_locking.c */
1269 void print_held_locks();
1271 void __preempt_add(void);
1272 void __preempt_sub(void);
1273 int get_preempt_cnt(void);
1274 void clear_preempt_cnt(void);
1275 bool function_decrements_preempt(void);
1276 void add_sleep_callback(expr_func
*fn
);
1277 unsigned long GFP_DIRECT_RECLAIM(void);
1278 unsigned long GFP_ATOMIC(void);
1279 bool in_irq_context(void);
1280 void clear_irq_context(void);
1282 /* check_assigned_expr.c */
1283 extern int check_assigned_expr_id
;
1284 struct expression
*get_assigned_expr(struct expression
*expr
);
1285 struct sm_state
*get_assigned_sm(struct expression
*expr
);
1286 struct expression
*get_assigned_expr_recurse(struct expression
*expr
);
1287 struct expression
*get_assigned_expr_name_sym(const char *name
, struct symbol
*sym
);
1288 struct expression
*get_assigned_expr_name_sym_recurse(const char *name
, struct symbol
*sym
);
1290 /* smatch_return_to_param.c */
1291 char *map_call_to_param_name_sym(struct expression
*expr
, struct symbol
**sym
);
1293 /* smatch_comparison.c */
1294 extern int comparison_id
;
1295 #define UNKNOWN_COMPARISON 0
1296 #define IMPOSSIBLE_COMPARISON -1
1297 struct compare_data
{
1298 /* The ->left and ->right expression pointers might be NULL (I'm lazy) */
1299 struct expression
*left
;
1300 const char *left_var
;
1301 struct var_sym_list
*left_vsl
;
1303 struct expression
*right
;
1304 const char *right_var
;
1305 struct var_sym_list
*right_vsl
;
1307 DECLARE_ALLOCATOR(compare_data
);
1308 struct smatch_state
*alloc_compare_state(
1309 struct expression
*left
,
1310 const char *left_var
, struct var_sym_list
*left_vsl
,
1312 struct expression
*right
,
1313 const char *right_var
, struct var_sym_list
*right_vsl
);
1314 int comparison_intersection(int orig
, int op
);
1315 int merge_comparisons(int one
, int two
);
1316 int combine_comparisons(int left_compare
, int right_compare
);
1317 int state_to_comparison(struct smatch_state
*state
);
1318 struct smatch_state
*merge_compare_states(struct smatch_state
*s1
, struct smatch_state
*s2
);
1319 int get_comparison(struct expression
*left
, struct expression
*right
);
1320 int get_comparison_no_extra(struct expression
*a
, struct expression
*b
);
1321 int get_comparison_strings(const char *one
, const char *two
);
1322 int possible_comparison(struct expression
*a
, int comparison
, struct expression
*b
);
1323 struct state_list
*get_all_comparisons(struct expression
*expr
);
1324 struct state_list
*get_all_possible_equal_comparisons(struct expression
*expr
);
1325 void __add_comparison_info(struct expression
*expr
, struct expression
*call
, const char *range
);
1326 char *get_printed_param_name(struct expression
*call
, const char *param_name
, struct symbol
*param_sym
);
1327 char *name_sym_to_param_comparison(const char *name
, struct symbol
*sym
);
1328 char *expr_equal_to_param(struct expression
*expr
, int ignore
);
1329 char *expr_lte_to_param(struct expression
*expr
, int ignore
);
1330 char *expr_param_comparison(struct expression
*expr
, int ignore
);
1331 int flip_comparison(int op
);
1332 int negate_comparison(int op
);
1333 int remove_unsigned_from_comparison(int op
);
1334 int param_compare_limit_is_impossible(struct expression
*expr
, int left_param
, char *left_key
, char *value
);
1335 void filter_by_comparison(struct range_list
**rl
, int comparison
, struct range_list
*right
);
1336 void __compare_param_limit_hook(struct expression
*left_expr
, struct expression
*right_expr
,
1337 const char *state_name
,
1338 struct smatch_state
*true_state
, struct smatch_state
*false_state
);
1339 int impossibly_high_comparison(struct expression
*expr
);
1340 void add_comparison_var_sym(
1341 struct expression
*left_expr
,
1342 const char *left_name
, struct var_sym_list
*left_vsl
,
1344 struct expression
*right_expr
,
1345 const char *right_name
, struct var_sym_list
*right_vsl
,
1346 struct expression
*mod_expr
);
1349 sval_t
*sval_alloc(sval_t sval
);
1350 sval_t
*sval_alloc_permanent(sval_t sval
);
1351 sval_t
sval_blank(struct expression
*expr
);
1352 sval_t
sval_type_val(struct symbol
*type
, long long val
);
1353 sval_t
sval_type_fval(struct symbol
*type
, long double fval
);
1354 sval_t
sval_from_val(struct expression
*expr
, long long val
);
1355 sval_t
sval_from_fval(struct expression
*expr
, long double fval
);
1356 int sval_is_ptr(sval_t sval
);
1357 bool sval_is_fp(sval_t sval
);
1358 int sval_unsigned(sval_t sval
);
1359 int sval_signed(sval_t sval
);
1360 int sval_bits(sval_t sval
);
1361 int sval_bits_used(sval_t sval
);
1362 int sval_is_negative(sval_t sval
);
1363 int sval_is_positive(sval_t sval
);
1364 int sval_is_min(sval_t sval
);
1365 int sval_is_max(sval_t sval
);
1366 int sval_is_a_min(sval_t sval
);
1367 int sval_is_a_max(sval_t sval
);
1368 int sval_is_negative_min(sval_t sval
);
1369 int sval_cmp_t(struct symbol
*type
, sval_t one
, sval_t two
);
1370 int sval_cmp_val(sval_t one
, long long val
);
1371 sval_t
sval_min(sval_t one
, sval_t two
);
1372 sval_t
sval_min_nonneg(sval_t one
, sval_t two
);
1373 sval_t
sval_max(sval_t one
, sval_t two
);
1374 int sval_too_low(struct symbol
*type
, sval_t sval
);
1375 int sval_too_high(struct symbol
*type
, sval_t sval
);
1376 int sval_fits(struct symbol
*type
, sval_t sval
);
1377 sval_t
sval_cast(struct symbol
*type
, sval_t sval
);
1378 sval_t
sval_preop(sval_t sval
, int op
);
1379 sval_t
sval_binop(sval_t left
, int op
, sval_t right
);
1380 int sval_binop_overflows(sval_t left
, int op
, sval_t right
);
1381 int sval_binop_overflows_no_sign(sval_t left
, int op
, sval_t right
);
1382 int find_first_zero_bit(unsigned long long uvalue
);
1383 int sm_fls64(unsigned long long uvalue
);
1384 unsigned long long fls_mask(unsigned long long uvalue
);
1385 unsigned long long sval_fls_mask(sval_t sval
);
1386 const char *sval_to_str(sval_t sval
);
1387 const char *sval_to_str_or_err_ptr(sval_t sval
);
1388 const char *sval_to_numstr(sval_t sval
);
1389 sval_t
ll_to_sval(long long val
);
1391 /* smatch_string_list.c */
1392 int list_has_string(struct string_list
*str_list
, const char *str
);
1393 int insert_string(struct string_list
**str_list
, const char *str
);
1394 struct string_list
*clone_str_list(struct string_list
*orig
);
1395 struct string_list
*combine_string_lists(struct string_list
*one
, struct string_list
*two
);
1397 /* smatch_start_states.c */
1398 struct stree
*get_start_states(void);
1400 /* smatch_recurse.c */
1401 int recurse(struct expression
*expr
,
1402 int (func
)(struct expression
*expr
, void *p
),
1403 void *param
, int nr
);
1404 int has_symbol(struct expression
*expr
, struct symbol
*sym
);
1405 int has_variable(struct expression
*expr
, struct expression
*var
);
1406 int has_inc_dec(struct expression
*expr
);
1408 /* smatch_stored_conditions.c */
1409 struct smatch_state
*get_stored_condition(struct expression
*expr
);
1410 struct expression_list
*get_conditions(struct expression
*expr
);
1411 struct sm_state
*stored_condition_implication_hook(struct expression
*expr
,
1412 struct state_list
**true_stack
,
1413 struct state_list
**false_stack
);
1414 /* smatch_parsed_conditions.c */
1415 struct sm_state
*parsed_condition_implication_hook(struct expression
*expr
,
1416 struct state_list
**true_stack
,
1417 struct state_list
**false_stack
);
1418 /* smatch_comparison.c */
1419 struct sm_state
*comparison_implication_hook(struct expression
*expr
,
1420 struct state_list
**true_stack
,
1421 struct state_list
**false_stack
);
1423 /* check_string_len.c */
1424 int get_formatted_string_size(struct expression
*call
, int arg
);
1425 int get_formatted_string_min_size(struct expression
*call
, int arg
);
1427 /* smatch_param_set.c */
1428 int param_was_set(struct expression
*expr
);
1429 int param_was_set_var_sym(const char *name
, struct symbol
*sym
);
1430 void print_limited_param_set(int return_id
, char *return_ranges
, struct expression
*expr
);
1432 void __promote_sets_to_clears(int return_id
, char *return_ranges
, struct expression
*expr
);
1433 bool parent_was_PARAM_CLEAR(const char *name
, struct symbol
*sym
);
1434 bool parent_was_PARAM_CLEAR_ZERO(const char *name
, struct symbol
*sym
);
1436 /* smatch_param_filter.c */
1437 int param_has_filter_data(struct sm_state
*sm
);
1439 /* smatch_links.c */
1440 void set_up_link_functions(int id
, int linkid
);
1441 struct smatch_state
*merge_link_states(struct smatch_state
*s1
, struct smatch_state
*s2
);
1442 void store_link(int link_id
, const char *name
, struct symbol
*sym
, const char *link_name
, struct symbol
*link_sym
);
1444 /* check_buf_comparison */
1445 const char *limit_type_str(unsigned int limit_type
);
1446 struct expression
*get_size_variable(struct expression
*buf
, int *limit_type
);
1447 struct expression
*get_array_variable(struct expression
*size
);
1448 int buf_comparison_index_ok(struct expression
*expr
);
1449 bool buf_comp_has_bytes(struct expression
*buf
, struct expression
*var
);
1450 bool buf_comp2_has_bytes(struct expression
*buf_expr
, struct expression
*var
);
1451 bool buf_has_bytes(struct expression
*buf
, struct expression
*var
);
1453 /* smatch_untracked_param.c */
1454 void mark_untracked(struct expression
*expr
, int param
, char *key
, char *value
);
1455 void mark_call_params_untracked(struct expression
*call
);
1456 void add_untracked_param_hook(void (func
)(struct expression
*call
, int param
));
1457 void add_lost_param_hook(void (func
)(struct expression
*call
, int param
));
1458 void mark_all_params_untracked(int return_id
, char *return_ranges
, struct expression
*expr
);
1459 /* smatch_untracked_var.c */
1460 bool is_untracked(struct expression
*expr
);
1462 /* smatch_strings.c */
1463 struct state_list
*get_strings(struct expression
*expr
);
1464 struct expression
*fake_string_from_mtag(mtag_t tag
);
1466 /* smatch_estate.c */
1467 int estate_get_single_value(struct smatch_state
*state
, sval_t
*sval
);
1469 /* smatch_address.c */
1470 int get_address_rl(struct expression
*expr
, struct range_list
**rl
);
1471 int get_member_offset(struct symbol
*type
, const char *member_name
);
1472 int get_member_offset_from_deref(struct expression
*expr
);
1474 /* for now this is in smatch_used_parameter.c */
1475 void __get_state_hook(int owner
, const char *name
, struct symbol
*sym
);
1476 extern int __ignore_param_used
;
1478 /* smatch_buf_comparison.c */
1479 int db_var_is_array_limit(struct expression
*array
, const char *name
, struct var_sym_list
*vsl
);
1481 struct range_list
*get_fs(void);
1483 struct stree
*get_all_return_states(void);
1484 struct stree_stack
*get_all_return_strees(void);
1485 int on_atomic_dec_path(void);
1486 int was_inced(const char *name
, struct symbol
*sym
);
1487 int refcount_was_inced_name_sym(const char *name
, struct symbol
*sym
, const char *counter_str
);
1488 int refcount_was_inced(struct expression
*expr
, const char *counter_str
);
1489 void set_refcount_inc(char *name
, struct symbol
*sym
);
1490 void set_refcount_dec(char *name
, struct symbol
*sym
);
1491 void add_refcount_init_hook(name_sym_hook
*hook
);
1492 void add_refcount_inc_hook(name_sym_hook
*hook
);
1493 void add_refcount_dec_hook(name_sym_hook
*hook
);
1495 /* smatch_constraints.c */
1496 char *get_constraint_str(struct expression
*expr
);
1497 struct constraint_list
*get_constraints(struct expression
*expr
);
1498 char *unmet_constraint(struct expression
*data
, struct expression
*offset
);
1499 char *get_required_constraint(const char *data_str
);
1501 /* smatch_container_of.c */
1502 int get_param_from_container_of(struct expression
*expr
);
1503 int get_offset_from_container_of(struct expression
*expr
);
1504 char *get_container_name(struct expression
*container
, struct expression
*expr
);
1507 unsigned long long str_to_llu_hash_helper(const char *str
);
1508 unsigned long long str_to_llu_hash(const char *str
);
1509 struct symbol
*get_symbol_from_mtag(mtag_t tag
);
1510 mtag_t
str_to_mtag(const char *str
);
1511 int get_string_mtag(struct expression
*expr
, mtag_t
*tag
);
1512 int get_toplevel_mtag(struct symbol
*sym
, mtag_t
*tag
);
1513 int create_mtag_alias(mtag_t tag
, struct expression
*expr
, mtag_t
*new);
1514 int expr_to_mtag_offset(struct expression
*expr
, mtag_t
*tag
, int *offset
);
1515 void update_mtag_data(struct expression
*expr
, struct smatch_state
*state
);
1516 int get_mtag_sval(struct expression
*expr
, sval_t
*sval
);
1518 /* Trinity fuzzer stuff */
1519 const char *get_syscall_arg_type(struct symbol
*sym
);
1522 int binfo_equiv(struct bit_info
*one
, struct bit_info
*two
);
1523 struct bit_info
*alloc_bit_info(unsigned long long set
, unsigned long long possible
);
1524 struct smatch_state
*alloc_bstate(unsigned long long set
, unsigned long long possible
);
1525 struct smatch_state
*merge_bstates(struct smatch_state
*one_state
, struct smatch_state
*two_state
);
1527 /* smatch_param_bits_set.c */
1528 void __set_param_modified_helper(struct expression
*expr
, struct smatch_state
*state
);
1529 void __set_param_modified_helper_sym(const char *name
, struct symbol
*sym
,
1530 struct smatch_state
*state
);
1532 /* smatch_param_bits_clear.c */
1533 void __set_param_modified_helper_clear(struct expression
*expr
, struct smatch_state
*state
);
1534 void __set_param_modified_helper_sym_clear(const char *name
, struct symbol
*sym
,
1535 struct smatch_state
*state
);
1537 /* smatch_bit_info.c */
1538 struct bit_info
*rl_to_binfo(struct range_list
*rl
);
1539 struct bit_info
*get_bit_info(struct expression
*expr
);
1540 struct bit_info
*get_bit_info_var_sym(const char *name
, struct symbol
*sym
);
1542 /* smatch_mem_tracker.c */
1543 extern int option_mem
;
1544 unsigned long get_mem_kb(void);
1545 unsigned long get_max_memory(void);
1547 /* smatch_goto_tracker.c */
1548 struct sm_state
*get_goto_sm_state(void);
1550 /* check_is_nospec.c */
1551 bool is_nospec(struct expression
*expr
);
1552 long get_stmt_cnt(void);
1554 /* check_kernel_units.c */
1555 struct smatch_state
*get_units(struct expression
*expr
);
1556 char *get_unit_str(struct expression
*expr
);
1557 bool is_array_size_units(struct expression
*expr
);
1558 /* smatch_nul_terminator.c */
1559 bool is_nul_terminated_var_sym(const char *name
, struct symbol
*sym
);
1560 bool is_nul_terminated(struct expression
*expr
);
1561 /* check_kernel.c */
1562 bool is_ignored_kernel_data(const char *name
);
1563 int get_gfp_param(struct expression
*expr
);
1564 struct expression
*get_netdev_priv(struct expression
*dev
);
1566 bool is_fresh_alloc_var_sym(const char *var
, struct symbol
*sym
);
1567 bool is_fresh_alloc(struct expression
*expr
);
1568 bool is_freed_var_sym(const char *name
, struct symbol
*sym
);
1569 void track_freed_param(struct expression
*expr
, struct smatch_state
*state
);
1570 void track_freed_param_var_sym(const char *name
, struct symbol
*sym
,
1571 struct smatch_state
*state
);
1572 bool is_part_of_condition(struct expression
*expr
);
1573 bool is_percent_p_print(struct expression
*expr
);
1575 void add_free_hook(name_sym_hook
*hook
);
1577 /* smatch_unconstant_macros.c */
1578 int is_unconstant_macro(struct expression
*expr
);
1580 /* smatch_assigned_state.c */
1581 void add_state_assigned_hook(int owner
, sm_hook
*call_back
);
1582 void add_ssa_state_assigned_hook(int owner
, sm_hook
*call_back
);
1584 /* check_returns_negative_error_code.c */
1585 bool holds_kernel_error_codes(struct expression
*expr
);
1586 bool possible_err_ptr(struct expression
*expr
);
1587 bool has_devm_cleanup(void);
1588 void add_set_current_state_hook(string_hook
*hook
);
1589 bool task_not_running(void);
1591 static inline bool type_is_ptr(struct symbol
*type
)
1594 (type
->type
== SYM_PTR
||
1595 type
->type
== SYM_ARRAY
||
1596 type
->type
== SYM_FN
);
1599 static inline bool type_is_fp(struct symbol
*type
)
1602 (type
== &float_ctype
||
1603 type
== &double_ctype
||
1604 type
== &ldouble_ctype
);
1607 static inline int type_bits(struct symbol
*type
)
1611 if (type_is_ptr(type
))
1612 return bits_in_pointer
;
1613 if (!type
->examined
)
1614 examine_symbol_type(type
);
1615 return type
->bit_size
;
1618 static inline int type_unsigned(struct symbol
*base_type
)
1622 if (is_ptr_type(base_type
))
1624 if (base_type
->ctype
.modifiers
& MOD_UNSIGNED
)
1629 static inline int type_positive_bits(struct symbol
*type
)
1633 if (is_ptr_type(type
))
1634 return bits_in_pointer
;
1635 if (type_unsigned(type
))
1636 return type_bits(type
);
1637 return type_bits(type
) - 1;
1640 static inline int sval_positive_bits(sval_t sval
)
1642 return type_positive_bits(sval
.type
);
1646 * Returns -1 if one is smaller, 0 if they are the same and 1 if two is larger.
1649 static inline int fp_cmp(sval_t one
, sval_t two
)
1651 struct symbol
*type
;
1653 if (sval_is_fp(one
) && sval_is_fp(two
))
1654 type
= type_bits(one
.type
) > type_bits(two
.type
) ? one
.type
: two
.type
;
1655 else if (sval_is_fp(one
))
1660 one
= sval_cast(type
, one
);
1661 two
= sval_cast(type
, two
);
1663 if (one
.type
== &float_ctype
) {
1664 if (one
.fvalue
< two
.fvalue
)
1666 if (one
.fvalue
== two
.fvalue
)
1670 if (one
.type
== &double_ctype
) {
1671 if (one
.dvalue
< two
.dvalue
)
1673 if (one
.dvalue
== two
.dvalue
)
1677 if (one
.type
== &ldouble_ctype
) {
1678 if (one
.ldvalue
< two
.ldvalue
)
1680 if (one
.ldvalue
== two
.ldvalue
)
1684 sm_perror("bad type in fp_cmp(): %s", type_to_str(type
));
1688 static inline int sval_cmp(sval_t one
, sval_t two
)
1690 struct symbol
*type
;
1692 if (sval_is_fp(one
) || sval_is_fp(two
))
1693 return fp_cmp(one
, two
);
1696 if (sval_positive_bits(two
) > sval_positive_bits(one
))
1698 if (type_bits(type
) < 31)
1701 one
= sval_cast(type
, one
);
1702 two
= sval_cast(type
, two
);
1704 if (type_unsigned(type
)) {
1705 if (one
.uvalue
< two
.uvalue
)
1707 if (one
.uvalue
== two
.uvalue
)
1711 /* fix me handle type promotion and unsigned values */
1712 if (one
.value
< two
.value
)
1714 if (one
.value
== two
.value
)
1719 #endif /* !SMATCH_H_ */