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 __delete_state(int owner
, const char *name
, struct symbol
*sym
);
456 void __delete_all_states_sym(struct symbol
*sym
);
457 void set_true_false_states(int owner
, const char *name
, struct symbol
*sym
,
458 struct smatch_state
*true_state
,
459 struct smatch_state
*false_state
);
460 void set_true_false_states_expr(int owner
, struct expression
*expr
,
461 struct smatch_state
*true_state
,
462 struct smatch_state
*false_state
);
464 struct stree
*get_all_states_from_stree(int owner
, struct stree
*source
);
465 struct stree
*get_all_states_stree(int id
);
466 struct stree
*__get_cur_stree(void);
467 int is_reachable(void);
468 void add_get_state_hook(void (*fn
)(int owner
, const char *name
, struct symbol
*sym
));
470 static inline void set_undefined(struct sm_state
*sm
, struct expression
*mod_expr
)
472 set_state(sm
->owner
, sm
->name
, sm
->sym
, &undefined
);
476 char *ssa_name(const char *name
);
477 void set_ssa_state(int owner
, const char *name
, struct symbol
*sym
, struct smatch_state
*state
);
478 void update_ssa_state(int owner
, const char *name
, struct symbol
*sym
,
479 struct smatch_state
*state
);
480 void update_ssa_sm(int owner
, struct sm_state
*sm
, struct smatch_state
*state
);
481 void set_ssa_state_expr(int owner
, struct expression
*expr
, struct smatch_state
*state
);
482 struct sm_state
*get_ssa_sm_state(int owner
, const char *name
, struct symbol
*sym
);
483 struct sm_state
*get_ssa_sm_state_expr(int owner
, struct expression
*expr
);
484 struct smatch_state
*get_ssa_state(int owner
, const char *name
, struct symbol
*sym
);
485 struct smatch_state
*get_ssa_state_expr(int owner
, struct expression
*expr
);
487 /* smatch_helper.c */
488 DECLARE_PTR_LIST(int_stack
, int);
489 char *alloc_string(const char *str
);
490 char *alloc_string_newline(const char *str
);
491 void free_string(char *str
);
492 void remove_parens(char *str
);
493 struct smatch_state
*alloc_state_num(int num
);
494 struct smatch_state
*alloc_state_str(const char *name
);
495 struct smatch_state
*merge_str_state(struct smatch_state
*s1
, struct smatch_state
*s2
);
496 struct smatch_state
*alloc_state_expr(struct expression
*expr
);
497 struct expression
*get_assigned_call(struct expression
*expr
);
498 struct expression
*get_argument_from_call_expr(struct expression_list
*args
,
500 struct expression
*get_array_expr(struct expression
*expr
);
502 char *expr_to_var(struct expression
*expr
);
503 struct symbol
*expr_to_sym(struct expression
*expr
);
504 char *expr_to_str(struct expression
*expr
);
505 char *expr_to_str_sym(struct expression
*expr
,
506 struct symbol
**sym_ptr
);
507 char *expr_to_var_sym(struct expression
*expr
,
508 struct symbol
**sym_ptr
);
509 char *expr_to_known_chunk_sym(struct expression
*expr
, struct symbol
**sym
);
510 char *expr_to_chunk_sym_vsl(struct expression
*expr
, struct symbol
**sym
, struct var_sym_list
**vsl
);
511 int get_complication_score(struct expression
*expr
);
513 int sym_name_is(const char *name
, struct expression
*expr
);
514 int get_const_value(struct expression
*expr
, sval_t
*sval
);
515 int get_value(struct expression
*expr
, sval_t
*val
);
516 int get_implied_value(struct expression
*expr
, sval_t
*val
);
517 int get_implied_value_fast(struct expression
*expr
, sval_t
*sval
);
518 int get_implied_min(struct expression
*expr
, sval_t
*sval
);
519 int get_implied_max(struct expression
*expr
, sval_t
*val
);
520 int get_hard_max(struct expression
*expr
, sval_t
*sval
);
521 int get_fuzzy_min(struct expression
*expr
, sval_t
*min
);
522 int get_fuzzy_max(struct expression
*expr
, sval_t
*max
);
523 int get_absolute_min(struct expression
*expr
, sval_t
*sval
);
524 int get_absolute_max(struct expression
*expr
, sval_t
*sval
);
525 int parse_call_math(struct expression
*expr
, char *math
, sval_t
*val
);
526 int parse_call_math_rl(struct expression
*call
, const char *math
, struct range_list
**rl
);
527 const char *get_allocation_math(struct expression
*expr
);
528 char *get_value_in_terms_of_parameter_math(struct expression
*expr
);
529 char *get_value_in_terms_of_parameter_math_var_sym(const char *var
, struct symbol
*sym
);
530 int expr_is_zero(struct expression
*expr
);
531 int known_condition_true(struct expression
*expr
);
532 int known_condition_false(struct expression
*expr
);
533 int implied_condition_true(struct expression
*expr
);
534 int implied_condition_false(struct expression
*expr
);
535 int can_integer_overflow(struct symbol
*type
, struct expression
*expr
);
536 void clear_math_cache(void);
537 void clear_strip_cache(void);
538 void set_fast_math_only(void);
539 void clear_fast_math_only(void);
541 int is_array(struct expression
*expr
);
542 struct expression
*get_array_base(struct expression
*expr
);
543 struct expression
*get_array_offset(struct expression
*expr
);
544 const char *show_state(struct smatch_state
*state
);
545 struct statement
*get_expression_statement(struct expression
*expr
);
546 struct expression
*strip__builtin_choose_expr(struct expression
*expr
);
547 struct expression
*strip_Generic(struct expression
*expr
);
548 struct expression
*strip_parens(struct expression
*expr
);
549 struct expression
*strip_expr(struct expression
*expr
);
550 struct expression
*strip_no_cast(struct expression
*expr
);
551 struct expression
*strip_expr_set_parent(struct expression
*expr
);
552 void scoped_state(int my_id
, const char *name
, struct symbol
*sym
);
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 __split_whole_condition(struct expression
*expr
);
746 void __handle_logic(struct expression
*expr
);
747 int is_condition(struct expression
*expr
);
748 int __handle_condition_assigns(struct expression
*expr
);
749 int __handle_select_assigns(struct expression
*expr
);
750 int __handle_expr_statement_assigns(struct expression
*expr
);
752 /* smatch_implied.c */
753 struct range_list_stack
;
754 void param_limit_implications(struct expression
*expr
, int param
, char *key
, char *value
, struct stree
**implied
);
755 struct stree
*__implied_case_stree(struct expression
*switch_expr
,
756 struct range_list
*case_rl
,
757 struct range_list_stack
**remaining_cases
,
758 struct stree
**raw_stree
);
759 void overwrite_states_using_pool(struct sm_state
*gate_sm
, struct sm_state
*pool_sm
);
760 int assume(struct expression
*expr
);
761 void end_assume(void);
762 int impossible_assumption(struct expression
*left
, int op
, sval_t sval
);
765 bool has_dynamic_states(unsigned short owner
);
766 void set_dynamic_states(unsigned short owner
);
768 /* smatch_extras.c */
769 int in_warn_on_macro(void);
770 extern int SMATCH_EXTRA
;
771 extern int RETURN_ID
;
778 char *get_other_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
);
779 char *map_call_to_other_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
);
780 char *map_long_to_short_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
, bool use_stack
);
782 #define STRLEN_MAX_RET 1010101
784 /* smatch_absolute.c */
785 int get_absolute_min_helper(struct expression
*expr
, sval_t
*sval
);
786 int get_absolute_max_helper(struct expression
*expr
, sval_t
*sval
);
788 /* smatch_type_value.c */
789 void disable_type_val_lookups(void);
790 void enable_type_val_lookups(void);
791 void clear_type_value_cache(void);
792 int get_db_type_rl(struct expression
*expr
, struct range_list
**rl
);
794 /* smatch_data_val.c */
795 int get_mtag_rl(struct expression
*expr
, struct range_list
**rl
);
796 /* smatch_array_values.c */
797 void clear_array_values_cache(void);
798 int get_array_rl(struct expression
*expr
, struct range_list
**rl
);
800 /* smatch_states.c */
801 struct stree
*__swap_cur_stree(struct stree
*stree
);
802 void __push_fake_cur_stree();
803 struct stree
*__pop_fake_cur_stree();
804 void __free_fake_cur_stree();
805 void __set_fake_cur_stree_fast(struct stree
*stree
);
806 void __pop_fake_cur_stree_fast(void);
807 void __merge_stree_into_cur(struct stree
*stree
);
809 int unreachable(void);
810 void __set_cur_stree_readonly(void);
811 void __set_cur_stree_writable(void);
812 void __set_sm(struct sm_state
*sm
);
813 void __set_sm_cur_stree(struct sm_state
*sm
);
814 void __set_true_false_sm(struct sm_state
*true_state
,
815 struct sm_state
*false_state
);
816 void nullify_path(void);
817 void __match_nullify_path_hook(const char *fn
, struct expression
*expr
,
819 void __unnullify_path(void);
820 int __path_is_null(void);
821 void save_all_states(void);
822 void restore_all_states(void);
823 void free_goto_stack(void);
824 void clear_all_states(void);
826 struct sm_state
*get_sm_state(int owner
, const char *name
,
828 struct sm_state
*get_sm_state_expr(int owner
, struct expression
*expr
);
829 void __push_true_states(void);
830 void __use_false_states(void);
831 void __discard_false_states(void);
832 void __merge_false_states(void);
833 void __merge_true_states(void);
835 void __negate_cond_stacks(void);
836 void __use_pre_cond_states(void);
837 void __use_cond_true_states(void);
838 void __use_cond_false_states(void);
839 void __push_cond_stacks(void);
840 void __fold_in_set_states(void);
841 void __free_set_states(void);
842 struct stree
*__copy_cond_true_states(void);
843 struct stree
*__copy_cond_false_states(void);
844 struct stree
*__pop_cond_true_stack(void);
845 struct stree
*__pop_cond_false_stack(void);
846 void __and_cond_states(void);
847 void __or_cond_states(void);
848 void __save_pre_cond_states(void);
849 void __discard_pre_cond_states(void);
850 struct stree
*__get_true_states(void);
851 struct stree
*__get_false_states(void);
852 void __use_cond_states(void);
853 extern struct state_list
*__last_base_slist
;
855 void __push_continues(void);
856 void __discard_continues(void);
857 void __process_continues(void);
858 void __merge_continues(void);
860 void __push_breaks(void);
861 void __process_breaks(void);
862 int __has_breaks(void);
863 void __merge_breaks(void);
864 void __use_breaks(void);
866 void __save_switch_states(struct expression
*switch_expr
);
867 void __discard_switches(void);
868 int have_remaining_cases(void);
869 void __merge_switches(struct expression
*switch_expr
, struct range_list
*case_rl
);
870 void __push_default(void);
871 void __set_default(void);
872 bool __has_default_case(void);
873 int __pop_default(void);
875 void __push_conditions(void);
876 void __discard_conditions(void);
878 void __save_gotos(const char *name
, struct symbol
*sym
);
879 void __merge_gotos(const char *name
, struct symbol
*sym
);
881 void __discard_fake_states(struct expression
*call
);
883 void __print_cur_stree(void);
884 bool __print_states(const char *owner
);
885 typedef void (check_tracker_hook
)(int owner
, const char *name
, struct symbol
*sym
, struct smatch_state
*state
);
886 void add_check_tracker(const char *check_name
, check_tracker_hook
*fn
);
889 void __pass_to_client(void *data
, enum hook_type type
);
890 void __pass_case_to_client(struct expression
*switch_expr
,
891 struct range_list
*rl
);
892 int __has_merge_function(int client_id
);
893 struct smatch_state
*__client_merge_function(int owner
,
894 struct smatch_state
*s1
,
895 struct smatch_state
*s2
);
896 struct smatch_state
*__client_unmatched_state_function(struct sm_state
*sm
);
897 void call_pre_merge_hook(struct sm_state
*cur
, struct sm_state
*other
);
898 void __push_scope_hooks(void);
899 void __call_scope_hooks(void);
900 void __call_all_scope_hooks(void);
901 void add_array_initialized_hook(void (*hook
)(struct expression
*array
, int nr
));
902 void __call_array_initialized_hooks(struct expression
*array
, int nr
);
904 /* smatch_function_hooks.c */
905 void add_fake_call_after_return(struct expression
*call
);
906 void create_function_hook_hash(void);
907 void __match_initializer_call(struct symbol
*sym
);
909 struct expression
*get_unfaked_call(void);
910 void fake_param_assign_helper(struct expression
*call
, struct expression
*fake_assign
, bool shallow
);
916 * Changing these numbers is a pain. Don't do it. If you ever use a
917 * number it can't be re-used right away so there may be gaps.
918 * We select these in order by type so if the order matters, then give
919 * it a number below 100-999,9000-9999 ranges. */
934 ABSOLUTE_LIMITS
= 1010,
948 UNTRACKED_PARAM
= 1023,
954 COMPARE_LIMIT
= 1028,
955 PARAM_COMPARE
= 1029,
958 CONSTRAINT_REQUIRED
= 1033,
969 POWER_OF_TWO_SET
= 1049,
975 NEGATIVE_ERROR
= 1057,
980 TASK_NOT_RUNNING
= 2064,
981 /* put random temporary stuff in the 7000-7999 range for testing */
983 HOST_DATA_SET
= 7017,
987 USER_DATA_SET
= 9017,
991 NO_OVERFLOW_SIMPLE
= 8019,
997 KNOWN_UNLOCKED
= 9025,
1002 REFCOUNT_INIT
= 9025,
1003 REFCOUNT_INC
= 9027,
1004 REFCOUNT_DEC
= 9028,
1005 NO_SIDE_EFFECT
= 8025,
1008 ARRAYSIZE_ARG
= 8033,
1013 STRING_VALUE
= 8041,
1022 /* Do not use numbers above 100k */
1025 extern struct sqlite3
*smatch_db
;
1026 extern struct sqlite3
*mem_db
;
1027 extern struct sqlite3
*cache_db
;
1029 bool db_incomplete(void);
1030 void db_ignore_states(int id
);
1031 typedef bool (delete_hook
)(struct expression
*expr
);
1032 void add_delete_return_hook(delete_hook
*hook
);
1033 void select_caller_info_hook(void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
), int type
);
1034 void select_caller_name_sym(void (*fn
)(const char *name
, struct symbol
*sym
, char *value
), int type
);
1035 void add_member_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
));
1036 void add_caller_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
));
1037 void add_return_info_callback(int owner
,
1038 void (*callback
)(int return_id
, char *return_ranges
,
1039 struct expression
*returned_expr
,
1041 const char *printed_name
,
1042 struct sm_state
*sm
));
1043 void add_split_return_callback(void (*fn
)(int return_id
, char *return_ranges
, struct expression
*returned_expr
));
1044 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
));
1045 void select_call_implies_hook(int type
, void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
));
1046 void select_return_implies_hook_early(int type
, void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
));
1047 void select_return_implies_hook(int type
, void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
));
1048 struct range_list
*db_return_vals(struct expression
*expr
);
1049 struct range_list
*db_return_vals_from_str(const char *fn_name
);
1050 struct range_list
*db_return_vals_no_args(struct expression
*expr
);
1051 bool split_param_key(const char *value
, int *param
, char *key
, int len
);
1052 bool get_implied_rl_from_call_str(struct expression
*expr
, const char *data
, struct range_list
**rl
);
1053 char *get_chunk_from_key(struct expression
*arg
, char *key
, struct symbol
**sym
, struct var_sym_list
**vsl
);
1054 char *get_variable_from_key(struct expression
*arg
, const char *key
, struct symbol
**sym
);
1055 struct expression
*get_function_param(struct expression
*expr
, int param
);
1056 char *get_name_sym_from_param_key(struct expression
*expr
, int param
, const char *key
, struct symbol
**sym
);
1057 char *get_param_name_var_sym(const char *name
, struct symbol
*sym
);
1058 char *get_param_name(struct sm_state
*sm
);
1059 const char *get_mtag_name_var_sym(const char *state_name
, struct symbol
*sym
);
1060 const char *get_mtag_name_expr(struct expression
*expr
);
1061 char *get_data_info_name(struct expression
*expr
);
1062 char *sm_to_arg_name(struct expression
*expr
, struct sm_state
*sm
);
1063 int is_recursive_member(const char *param_name
);
1065 char *escape_newlines(const char *str
);
1066 void sql_exec(struct sqlite3
*db
, int (*callback
)(void*, int, char**, char**), void *data
, const char *sql
);
1068 #define sql_helper(db, call_back, data, sql...) \
1070 char sql_txt[1024]; \
1072 sqlite3_snprintf(sizeof(sql_txt), sql_txt, sql); \
1073 db_debug("debug: %s\n", sql_txt); \
1074 sql_exec(db, call_back, data, sql_txt); \
1078 #define run_sql(call_back, data, sql...) \
1082 sql_helper(smatch_db, call_back, data, sql); \
1085 #define mem_sql(call_back, data, sql...) \
1086 sql_helper(mem_db, call_back, data, sql)
1088 #define cache_sql(call_back, data, sql...) \
1089 sql_helper(cache_db, call_back, data, sql)
1091 #define sql_insert_helper(table, db, ignore, late, values...) \
1093 struct sqlite3 *_db = db; \
1095 if (__inline_fn && !_db) \
1099 char *err, *p = buf; \
1102 p += snprintf(p, buf + sizeof(buf) - p, \
1103 "insert %sinto %s values (", \
1104 ignore ? "or ignore " : "", #table); \
1105 p += snprintf(p, buf + sizeof(buf) - p, values); \
1106 p += snprintf(p, buf + sizeof(buf) - p, ");"); \
1107 db_debug("mem-db: %s\n", buf); \
1108 rc = sqlite3_exec(_db, buf, NULL, NULL, &err); \
1109 if (rc != SQLITE_OK) { \
1110 sm_ierror("SQL error #2: %s", err); \
1111 sm_ierror("SQL: '%s'", buf); \
1116 if (option_info) { \
1117 FILE *tmp_fd = sm_outfd; \
1118 sm_outfd = sql_outfd; \
1120 sm_printf("SQL%s: insert %sinto " #table " values(", \
1121 late ? "_late" : "", ignore ? "or ignore " : ""); \
1122 sm_printf(values); \
1123 sm_printf(");\n"); \
1124 sm_outfd = tmp_fd; \
1128 #define sql_insert(table, values...) sql_insert_helper(table, NULL, 0, 0, values);
1129 #define sql_insert_or_ignore(table, values...) sql_insert_helper(table, NULL, 1, 0, values);
1130 #define sql_insert_late(table, values...) sql_insert_helper(table, NULL, 0, 1, values);
1131 #define sql_insert_cache(table, values...) sql_insert_helper(table, cache_db, 1, 0, values);
1132 #define sql_insert_cache_or_ignore(table, values...) sql_insert_helper(table, cache_db, 1, 0, values);
1134 char *get_static_filter(struct symbol
*sym
);
1136 void sql_insert_return_states(int return_id
, const char *return_ranges
,
1137 int type
, int param
, const char *key
, const char *value
);
1138 void sql_insert_caller_info(struct expression
*call
, int type
, int param
,
1139 const char *key
, const char *value
);
1140 void sql_insert_function_ptr(const char *fn
, const char *struct_name
);
1141 void sql_insert_return_values(const char *return_values
);
1142 void sql_insert_return_implies(int type
, int param
, const char *key
, const char *value
);
1143 void sql_insert_function_type_size(const char *member
, const char *ranges
);
1144 void sql_insert_function_type_info(int type
, const char *struct_type
, const char *member
, const char *value
);
1145 void sql_insert_type_info(int type
, const char *member
, const char *value
);
1146 void sql_insert_local_values(const char *name
, const char *value
);
1147 void sql_insert_function_type_value(const char *type
, const char *value
);
1148 void sql_insert_function_type(int param
, const char *value
);
1149 void sql_insert_parameter_name(int param
, const char *value
);
1150 void sql_insert_data_info(struct expression
*data
, int type
, const char *value
);
1151 void sql_insert_data_info_var_sym(const char *var
, struct symbol
*sym
, int type
, const char *value
);
1152 void sql_save_constraint(const char *con
);
1153 void sql_save_constraint_required(const char *data
, int op
, const char *limit
);
1154 void sql_copy_constraint_required(const char *new_limit
, const char *old_limit
);
1155 void sql_insert_fn_ptr_data_link(const char *ptr
, const char *data
);
1156 void sql_insert_fn_data_link(struct expression
*fn
, int type
, int param
, const char *key
, const char *value
);
1157 void sql_insert_mtag_about(mtag_t tag
, const char *left_name
, const char *right_name
);
1158 void sql_insert_mtag_info(mtag_t tag
, int type
, const char *value
);
1159 void sql_insert_mtag_map(mtag_t container
, int container_offset
, mtag_t tag
, int tag_offset
);
1160 void sql_insert_mtag_alias(mtag_t orig
, mtag_t alias
);
1161 int mtag_map_select_container(mtag_t tag
, int container_offset
, mtag_t
*container
);
1162 int mtag_map_select_tag(mtag_t container
, int offset
, mtag_t
*tag
);
1163 struct smatch_state
*get_mtag_return(struct expression
*expr
, struct smatch_state
*state
);
1164 struct range_list
*swap_mtag_seed(struct expression
*expr
, struct range_list
*rl
);
1166 bool is_fn_ptr(struct expression
*fn
);
1167 void sql_select_return_states(const char *cols
, struct expression
*call
,
1168 int (*callback
)(void*, int, char**, char**), void *info
);
1169 void sql_select_call_implies(const char *cols
, struct expression
*call
,
1170 int (*callback
)(void*, int, char**, char**));
1172 void open_smatch_db(char *db_file
);
1174 /* smatch_files.c */
1175 int open_data_file(const char *filename
);
1176 int open_schema_file(const char *schema
);
1177 struct token
*get_tokens_file(const char *filename
);
1178 struct string_list
*load_strings_from_file(const char *project
, const char *filename
);
1181 extern char *option_debug_check
;
1182 extern char *option_debug_var
;
1183 extern char *option_process_function
;
1184 extern char *option_project_str
;
1185 extern char *bin_dir
;
1186 extern char *data_dir
;
1187 extern int option_no_data
;
1188 extern int option_full_path
;
1189 extern int option_call_tree
;
1190 extern int num_checks
;
1196 PROJ_ILLUMOS_KERNEL
,
1200 extern enum project_type option_project
;
1201 const char *check_name(unsigned short id
);
1202 int id_from_name(const char *name
);
1205 /* smatch_buf_size.c */
1206 int bytes_to_elements(struct expression
*expr
, int bytes
);
1207 int get_array_size(struct expression
*expr
);
1208 int get_array_size_bytes(struct expression
*expr
);
1209 int get_array_size_bytes_min(struct expression
*expr
);
1210 int get_array_size_bytes_max(struct expression
*expr
);
1211 struct range_list
*get_array_size_bytes_rl(struct expression
*expr
);
1212 int get_real_array_size(struct expression
*expr
);
1213 int last_member_is_resizable(struct symbol
*type
);
1214 /* smatch_strlen.c */
1215 bool is_strlen(struct expression
*expr
);
1216 int get_implied_strlen(struct expression
*expr
, struct range_list
**rl
);
1217 int get_size_from_strlen(struct expression
*expr
);
1219 /* smatch_capped.c */
1220 int is_capped(struct expression
*expr
);
1221 int is_capped_var_sym(const char *name
, struct symbol
*sym
);
1223 /* smatch_kernel_user_data.c */
1224 int is_user_macro(struct expression
*expr
);
1225 int is_capped_user_data(struct expression
*expr
);
1226 int implied_user_data(struct expression
*expr
, struct range_list
**rl
);
1227 struct stree
*get_user_stree(void);
1228 int get_user_rl(struct expression
*expr
, struct range_list
**rl
);
1229 int is_user_rl(struct expression
*expr
);
1230 int get_user_rl_var_sym(const char *name
, struct symbol
*sym
, struct range_list
**rl
);
1231 void mark_as_user_data(struct expression
*expr
, bool isnew
);
1232 bool user_rl_capped_var_sym(const char *name
, struct symbol
*sym
);
1233 bool user_rl_capped(struct expression
*expr
);
1234 struct range_list
*var_user_rl(struct expression
*expr
);
1235 bool we_pass_user_data(struct expression
*call
);
1236 /* smatch_points_to_user_data.c */
1237 bool is_user_data_fn(struct symbol
*fn
);
1238 bool is_skb_data(struct expression
*expr
);
1239 bool is_socket_stuff(struct symbol
*sym
);
1240 bool points_to_user_data(struct expression
*expr
);
1241 void set_array_user_ptr(struct expression
*expr
, bool is_new
);
1243 /* smatch_kernel_host_data.c */
1244 int is_host_macro(struct expression
*expr
);
1245 int get_host_data_fn_param(const char *fn
);
1246 int is_capped_host_data(struct expression
*expr
);
1247 int implied_host_data(struct expression
*expr
, struct range_list
**rl
);
1248 struct stree
*get_host_stree(void);
1249 int get_host_rl(struct expression
*expr
, struct range_list
**rl
);
1250 int is_host_rl(struct expression
*expr
);
1251 int get_host_rl_var_sym(const char *name
, struct symbol
*sym
, struct range_list
**rl
);
1252 bool host_rl_capped(struct expression
*expr
);
1253 struct range_list
*var_host_rl(struct expression
*expr
);
1254 bool we_pass_host_data(struct expression
*call
);
1255 /* smatch_points_to_host_data.c */
1256 bool is_host_data_fn(struct symbol
*fn
);
1257 bool points_to_host_data(struct expression
*expr
);
1258 void set_points_to_host_data(struct expression
*expr
, bool is_new
);
1259 bool is_fn_points_to_host_data(const char *fn
);
1260 /* check_locking.c */
1261 void print_held_locks();
1263 void __preempt_add(void);
1264 void __preempt_sub(void);
1265 int get_preempt_cnt(void);
1266 void clear_preempt_cnt(void);
1267 bool function_decrements_preempt(void);
1268 void add_sleep_callback(expr_func
*fn
);
1269 unsigned long GFP_DIRECT_RECLAIM(void);
1270 unsigned long GFP_ATOMIC(void);
1272 /* check_assigned_expr.c */
1273 extern int check_assigned_expr_id
;
1274 struct expression
*get_assigned_expr(struct expression
*expr
);
1275 struct sm_state
*get_assigned_sm(struct expression
*expr
);
1276 struct expression
*get_assigned_expr_recurse(struct expression
*expr
);
1277 struct expression
*get_assigned_expr_name_sym(const char *name
, struct symbol
*sym
);
1278 struct expression
*get_assigned_expr_name_sym_recurse(const char *name
, struct symbol
*sym
);
1280 /* smatch_return_to_param.c */
1281 char *map_call_to_param_name_sym(struct expression
*expr
, struct symbol
**sym
);
1283 /* smatch_comparison.c */
1284 extern int comparison_id
;
1285 #define UNKNOWN_COMPARISON 0
1286 #define IMPOSSIBLE_COMPARISON -1
1287 struct compare_data
{
1288 /* The ->left and ->right expression pointers might be NULL (I'm lazy) */
1289 struct expression
*left
;
1290 const char *left_var
;
1291 struct var_sym_list
*left_vsl
;
1293 struct expression
*right
;
1294 const char *right_var
;
1295 struct var_sym_list
*right_vsl
;
1297 DECLARE_ALLOCATOR(compare_data
);
1298 struct smatch_state
*alloc_compare_state(
1299 struct expression
*left
,
1300 const char *left_var
, struct var_sym_list
*left_vsl
,
1302 struct expression
*right
,
1303 const char *right_var
, struct var_sym_list
*right_vsl
);
1304 int comparison_intersection(int orig
, int op
);
1305 int merge_comparisons(int one
, int two
);
1306 int combine_comparisons(int left_compare
, int right_compare
);
1307 int state_to_comparison(struct smatch_state
*state
);
1308 struct smatch_state
*merge_compare_states(struct smatch_state
*s1
, struct smatch_state
*s2
);
1309 int get_comparison(struct expression
*left
, struct expression
*right
);
1310 int get_comparison_no_extra(struct expression
*a
, struct expression
*b
);
1311 int get_comparison_strings(const char *one
, const char *two
);
1312 int possible_comparison(struct expression
*a
, int comparison
, struct expression
*b
);
1313 struct state_list
*get_all_comparisons(struct expression
*expr
);
1314 struct state_list
*get_all_possible_equal_comparisons(struct expression
*expr
);
1315 void __add_comparison_info(struct expression
*expr
, struct expression
*call
, const char *range
);
1316 char *get_printed_param_name(struct expression
*call
, const char *param_name
, struct symbol
*param_sym
);
1317 char *name_sym_to_param_comparison(const char *name
, struct symbol
*sym
);
1318 char *expr_equal_to_param(struct expression
*expr
, int ignore
);
1319 char *expr_lte_to_param(struct expression
*expr
, int ignore
);
1320 char *expr_param_comparison(struct expression
*expr
, int ignore
);
1321 int flip_comparison(int op
);
1322 int negate_comparison(int op
);
1323 int remove_unsigned_from_comparison(int op
);
1324 int param_compare_limit_is_impossible(struct expression
*expr
, int left_param
, char *left_key
, char *value
);
1325 void filter_by_comparison(struct range_list
**rl
, int comparison
, struct range_list
*right
);
1326 void __compare_param_limit_hook(struct expression
*left_expr
, struct expression
*right_expr
,
1327 const char *state_name
,
1328 struct smatch_state
*true_state
, struct smatch_state
*false_state
);
1329 int impossibly_high_comparison(struct expression
*expr
);
1330 void add_comparison_var_sym(
1331 struct expression
*left_expr
,
1332 const char *left_name
, struct var_sym_list
*left_vsl
,
1334 struct expression
*right_expr
,
1335 const char *right_name
, struct var_sym_list
*right_vsl
,
1336 struct expression
*mod_expr
);
1339 sval_t
*sval_alloc(sval_t sval
);
1340 sval_t
*sval_alloc_permanent(sval_t sval
);
1341 sval_t
sval_blank(struct expression
*expr
);
1342 sval_t
sval_type_val(struct symbol
*type
, long long val
);
1343 sval_t
sval_type_fval(struct symbol
*type
, long double fval
);
1344 sval_t
sval_from_val(struct expression
*expr
, long long val
);
1345 sval_t
sval_from_fval(struct expression
*expr
, long double fval
);
1346 int sval_is_ptr(sval_t sval
);
1347 bool sval_is_fp(sval_t sval
);
1348 int sval_unsigned(sval_t sval
);
1349 int sval_signed(sval_t sval
);
1350 int sval_bits(sval_t sval
);
1351 int sval_bits_used(sval_t sval
);
1352 int sval_is_negative(sval_t sval
);
1353 int sval_is_positive(sval_t sval
);
1354 int sval_is_min(sval_t sval
);
1355 int sval_is_max(sval_t sval
);
1356 int sval_is_a_min(sval_t sval
);
1357 int sval_is_a_max(sval_t sval
);
1358 int sval_is_negative_min(sval_t sval
);
1359 int sval_cmp_t(struct symbol
*type
, sval_t one
, sval_t two
);
1360 int sval_cmp_val(sval_t one
, long long val
);
1361 sval_t
sval_min(sval_t one
, sval_t two
);
1362 sval_t
sval_min_nonneg(sval_t one
, sval_t two
);
1363 sval_t
sval_max(sval_t one
, sval_t two
);
1364 int sval_too_low(struct symbol
*type
, sval_t sval
);
1365 int sval_too_high(struct symbol
*type
, sval_t sval
);
1366 int sval_fits(struct symbol
*type
, sval_t sval
);
1367 sval_t
sval_cast(struct symbol
*type
, sval_t sval
);
1368 sval_t
sval_preop(sval_t sval
, int op
);
1369 sval_t
sval_binop(sval_t left
, int op
, sval_t right
);
1370 int sval_binop_overflows(sval_t left
, int op
, sval_t right
);
1371 int sval_binop_overflows_no_sign(sval_t left
, int op
, sval_t right
);
1372 int find_first_zero_bit(unsigned long long uvalue
);
1373 int sm_fls64(unsigned long long uvalue
);
1374 unsigned long long fls_mask(unsigned long long uvalue
);
1375 unsigned long long sval_fls_mask(sval_t sval
);
1376 const char *sval_to_str(sval_t sval
);
1377 const char *sval_to_str_or_err_ptr(sval_t sval
);
1378 const char *sval_to_numstr(sval_t sval
);
1379 sval_t
ll_to_sval(long long val
);
1381 /* smatch_string_list.c */
1382 int list_has_string(struct string_list
*str_list
, const char *str
);
1383 int insert_string(struct string_list
**str_list
, const char *str
);
1384 struct string_list
*clone_str_list(struct string_list
*orig
);
1385 struct string_list
*combine_string_lists(struct string_list
*one
, struct string_list
*two
);
1387 /* smatch_start_states.c */
1388 struct stree
*get_start_states(void);
1390 /* smatch_recurse.c */
1391 int recurse(struct expression
*expr
,
1392 int (func
)(struct expression
*expr
, void *p
),
1393 void *param
, int nr
);
1394 int has_symbol(struct expression
*expr
, struct symbol
*sym
);
1395 int has_variable(struct expression
*expr
, struct expression
*var
);
1396 int has_inc_dec(struct expression
*expr
);
1398 /* smatch_stored_conditions.c */
1399 struct smatch_state
*get_stored_condition(struct expression
*expr
);
1400 struct expression_list
*get_conditions(struct expression
*expr
);
1401 struct sm_state
*stored_condition_implication_hook(struct expression
*expr
,
1402 struct state_list
**true_stack
,
1403 struct state_list
**false_stack
);
1404 /* smatch_parsed_conditions.c */
1405 struct sm_state
*parsed_condition_implication_hook(struct expression
*expr
,
1406 struct state_list
**true_stack
,
1407 struct state_list
**false_stack
);
1408 /* smatch_comparison.c */
1409 struct sm_state
*comparison_implication_hook(struct expression
*expr
,
1410 struct state_list
**true_stack
,
1411 struct state_list
**false_stack
);
1413 /* check_string_len.c */
1414 int get_formatted_string_size(struct expression
*call
, int arg
);
1415 int get_formatted_string_min_size(struct expression
*call
, int arg
);
1417 /* smatch_param_set.c */
1418 int param_was_set(struct expression
*expr
);
1419 int param_was_set_var_sym(const char *name
, struct symbol
*sym
);
1420 void print_limited_param_set(int return_id
, char *return_ranges
, struct expression
*expr
);
1422 void __promote_sets_to_clears(int return_id
, char *return_ranges
, struct expression
*expr
);
1423 bool parent_was_PARAM_CLEAR(const char *name
, struct symbol
*sym
);
1424 bool parent_was_PARAM_CLEAR_ZERO(const char *name
, struct symbol
*sym
);
1426 /* smatch_param_filter.c */
1427 int param_has_filter_data(struct sm_state
*sm
);
1429 /* smatch_links.c */
1430 void set_up_link_functions(int id
, int linkid
);
1431 struct smatch_state
*merge_link_states(struct smatch_state
*s1
, struct smatch_state
*s2
);
1432 void store_link(int link_id
, const char *name
, struct symbol
*sym
, const char *link_name
, struct symbol
*link_sym
);
1434 /* check_buf_comparison */
1435 const char *limit_type_str(unsigned int limit_type
);
1436 struct expression
*get_size_variable(struct expression
*buf
, int *limit_type
);
1437 struct expression
*get_array_variable(struct expression
*size
);
1438 int buf_comparison_index_ok(struct expression
*expr
);
1439 bool buf_comp_has_bytes(struct expression
*buf
, struct expression
*var
);
1440 bool buf_comp2_has_bytes(struct expression
*buf_expr
, struct expression
*var
);
1442 /* smatch_untracked_param.c */
1443 void mark_untracked(struct expression
*expr
, int param
, const char *key
, const char *value
);
1444 void mark_call_params_untracked(struct expression
*call
);
1445 void add_untracked_param_hook(void (func
)(struct expression
*call
, int param
));
1446 void add_lost_param_hook(void (func
)(struct expression
*call
, int param
));
1447 void mark_all_params_untracked(int return_id
, char *return_ranges
, struct expression
*expr
);
1448 /* smatch_untracked_var.c */
1449 bool is_untracked(struct expression
*expr
);
1451 /* smatch_strings.c */
1452 struct state_list
*get_strings(struct expression
*expr
);
1453 struct expression
*fake_string_from_mtag(mtag_t tag
);
1455 /* smatch_estate.c */
1456 int estate_get_single_value(struct smatch_state
*state
, sval_t
*sval
);
1458 /* smatch_address.c */
1459 int get_address_rl(struct expression
*expr
, struct range_list
**rl
);
1460 int get_member_offset(struct symbol
*type
, const char *member_name
);
1461 int get_member_offset_from_deref(struct expression
*expr
);
1463 /* for now this is in smatch_used_parameter.c */
1464 void __get_state_hook(int owner
, const char *name
, struct symbol
*sym
);
1465 extern int __ignore_param_used
;
1467 /* smatch_buf_comparison.c */
1468 int db_var_is_array_limit(struct expression
*array
, const char *name
, struct var_sym_list
*vsl
);
1470 struct range_list
*get_fs(void);
1472 struct stree
*get_all_return_states(void);
1473 struct stree_stack
*get_all_return_strees(void);
1474 int on_atomic_dec_path(void);
1475 int was_inced(const char *name
, struct symbol
*sym
);
1476 void set_refcount_inc(char *name
, struct symbol
*sym
);
1477 void set_refcount_dec(char *name
, struct symbol
*sym
);
1478 void add_refcount_init_hook(name_sym_hook
*hook
);
1479 void add_refcount_inc_hook(name_sym_hook
*hook
);
1480 void add_refcount_dec_hook(name_sym_hook
*hook
);
1482 /* smatch_constraints.c */
1483 char *get_constraint_str(struct expression
*expr
);
1484 struct constraint_list
*get_constraints(struct expression
*expr
);
1485 char *unmet_constraint(struct expression
*data
, struct expression
*offset
);
1486 char *get_required_constraint(const char *data_str
);
1488 /* smatch_container_of.c */
1489 int get_param_from_container_of(struct expression
*expr
);
1490 int get_offset_from_container_of(struct expression
*expr
);
1491 char *get_container_name(struct expression
*container
, struct expression
*expr
);
1494 unsigned long long str_to_llu_hash_helper(const char *str
);
1495 unsigned long long str_to_llu_hash(const char *str
);
1496 struct symbol
*get_symbol_from_mtag(mtag_t tag
);
1497 mtag_t
str_to_mtag(const char *str
);
1498 int get_string_mtag(struct expression
*expr
, mtag_t
*tag
);
1499 int get_toplevel_mtag(struct symbol
*sym
, mtag_t
*tag
);
1500 int create_mtag_alias(mtag_t tag
, struct expression
*expr
, mtag_t
*new);
1501 int expr_to_mtag_offset(struct expression
*expr
, mtag_t
*tag
, int *offset
);
1502 void update_mtag_data(struct expression
*expr
, struct smatch_state
*state
);
1503 int get_mtag_sval(struct expression
*expr
, sval_t
*sval
);
1505 /* Trinity fuzzer stuff */
1506 const char *get_syscall_arg_type(struct symbol
*sym
);
1509 int binfo_equiv(struct bit_info
*one
, struct bit_info
*two
);
1510 struct bit_info
*alloc_bit_info(unsigned long long set
, unsigned long long possible
);
1511 struct smatch_state
*alloc_bstate(unsigned long long set
, unsigned long long possible
);
1512 struct smatch_state
*merge_bstates(struct smatch_state
*one_state
, struct smatch_state
*two_state
);
1514 /* smatch_param_bits_set.c */
1515 void __set_param_modified_helper(struct expression
*expr
, struct smatch_state
*state
);
1516 void __set_param_modified_helper_sym(const char *name
, struct symbol
*sym
,
1517 struct smatch_state
*state
);
1519 /* smatch_param_bits_clear.c */
1520 void __set_param_modified_helper_clear(struct expression
*expr
, struct smatch_state
*state
);
1521 void __set_param_modified_helper_sym_clear(const char *name
, struct symbol
*sym
,
1522 struct smatch_state
*state
);
1524 /* smatch_bit_info.c */
1525 struct bit_info
*rl_to_binfo(struct range_list
*rl
);
1526 struct bit_info
*get_bit_info(struct expression
*expr
);
1527 struct bit_info
*get_bit_info_var_sym(const char *name
, struct symbol
*sym
);
1529 /* smatch_mem_tracker.c */
1530 extern int option_mem
;
1531 unsigned long get_mem_kb(void);
1532 unsigned long get_max_memory(void);
1534 /* smatch_goto_tracker.c */
1535 struct sm_state
*get_goto_sm_state(void);
1537 /* check_is_nospec.c */
1538 bool is_nospec(struct expression
*expr
);
1539 long get_stmt_cnt(void);
1541 /* check_kernel_units.c */
1542 struct smatch_state
*get_units(struct expression
*expr
);
1543 char *get_unit_str(struct expression
*expr
);
1544 bool is_array_size_units(struct expression
*expr
);
1545 /* smatch_nul_terminator.c */
1546 bool is_nul_terminated_var_sym(const char *name
, struct symbol
*sym
);
1547 bool is_nul_terminated(struct expression
*expr
);
1548 /* check_kernel.c */
1549 bool is_ignored_kernel_data(const char *name
);
1550 int get_gfp_param(struct expression
*expr
);
1551 struct expression
*get_netdev_priv(struct expression
*dev
);
1553 bool is_fresh_alloc_var_sym(const char *var
, struct symbol
*sym
);
1554 bool is_fresh_alloc(struct expression
*expr
);
1555 bool is_freed_var_sym(const char *name
, struct symbol
*sym
);
1556 void track_freed_param(struct expression
*expr
, struct smatch_state
*state
);
1557 void track_freed_param_var_sym(const char *name
, struct symbol
*sym
,
1558 struct smatch_state
*state
);
1559 bool is_part_of_condition(struct expression
*expr
);
1560 bool is_percent_p_print(struct expression
*expr
);
1562 void add_free_hook(name_sym_hook
*hook
);
1564 /* smatch_unconstant_macros.c */
1565 int is_unconstant_macro(struct expression
*expr
);
1567 /* smatch_assigned_state.c */
1568 void add_state_assigned_hook(int owner
, sm_hook
*call_back
);
1569 void add_ssa_state_assigned_hook(int owner
, sm_hook
*call_back
);
1571 /* check_returns_negative_error_code.c */
1572 bool holds_kernel_error_codes(struct expression
*expr
);
1573 bool possible_err_ptr(struct expression
*expr
);
1574 bool has_devm_cleanup(void);
1575 void add_set_current_state_hook(string_hook
*hook
);
1576 bool task_not_running(void);
1578 static inline bool type_is_ptr(struct symbol
*type
)
1581 (type
->type
== SYM_PTR
||
1582 type
->type
== SYM_ARRAY
||
1583 type
->type
== SYM_FN
);
1586 static inline bool type_is_fp(struct symbol
*type
)
1589 (type
== &float_ctype
||
1590 type
== &double_ctype
||
1591 type
== &ldouble_ctype
);
1594 static inline int type_bits(struct symbol
*type
)
1598 if (type_is_ptr(type
))
1599 return bits_in_pointer
;
1600 if (!type
->examined
)
1601 examine_symbol_type(type
);
1602 return type
->bit_size
;
1605 static inline int type_unsigned(struct symbol
*base_type
)
1609 if (is_ptr_type(base_type
))
1611 if (base_type
->ctype
.modifiers
& MOD_UNSIGNED
)
1616 static inline int type_positive_bits(struct symbol
*type
)
1620 if (is_ptr_type(type
))
1621 return bits_in_pointer
;
1622 if (type_unsigned(type
))
1623 return type_bits(type
);
1624 return type_bits(type
) - 1;
1627 static inline int sval_positive_bits(sval_t sval
)
1629 return type_positive_bits(sval
.type
);
1633 * Returns -1 if one is smaller, 0 if they are the same and 1 if two is larger.
1636 static inline int fp_cmp(sval_t one
, sval_t two
)
1638 struct symbol
*type
;
1640 if (sval_is_fp(one
) && sval_is_fp(two
))
1641 type
= type_bits(one
.type
) > type_bits(two
.type
) ? one
.type
: two
.type
;
1642 else if (sval_is_fp(one
))
1647 one
= sval_cast(type
, one
);
1648 two
= sval_cast(type
, two
);
1650 if (one
.type
== &float_ctype
) {
1651 if (one
.fvalue
< two
.fvalue
)
1653 if (one
.fvalue
== two
.fvalue
)
1657 if (one
.type
== &double_ctype
) {
1658 if (one
.dvalue
< two
.dvalue
)
1660 if (one
.dvalue
== two
.dvalue
)
1664 if (one
.type
== &ldouble_ctype
) {
1665 if (one
.ldvalue
< two
.ldvalue
)
1667 if (one
.ldvalue
== two
.ldvalue
)
1671 sm_perror("bad type in fp_cmp(): %s", type_to_str(type
));
1675 static inline int sval_cmp(sval_t one
, sval_t two
)
1677 struct symbol
*type
;
1679 if (sval_is_fp(one
) || sval_is_fp(two
))
1680 return fp_cmp(one
, two
);
1683 if (sval_positive_bits(two
) > sval_positive_bits(one
))
1685 if (type_bits(type
) < 31)
1688 one
= sval_cast(type
, one
);
1689 two
= sval_cast(type
, two
);
1691 if (type_unsigned(type
)) {
1692 if (one
.uvalue
< two
.uvalue
)
1694 if (one
.uvalue
== two
.uvalue
)
1698 /* fix me handle type promotion and unsigned values */
1699 if (one
.value
< two
.value
)
1701 if (one
.value
== two
.value
)
1706 #endif /* !SMATCH_H_ */