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
));
471 char *ssa_name(const char *name
);
472 void set_ssa_state(int owner
, const char *name
, struct symbol
*sym
, struct smatch_state
*state
);
473 void update_ssa_state(int owner
, const char *name
, struct symbol
*sym
,
474 struct smatch_state
*state
);
475 void update_ssa_sm(int owner
, struct sm_state
*sm
, struct smatch_state
*state
);
476 void set_ssa_state_expr(int owner
, struct expression
*expr
, struct smatch_state
*state
);
477 struct sm_state
*get_ssa_sm_state(int owner
, const char *name
, struct symbol
*sym
);
478 struct sm_state
*get_ssa_sm_state_expr(int owner
, struct expression
*expr
);
479 struct smatch_state
*get_ssa_state(int owner
, const char *name
, struct symbol
*sym
);
480 struct smatch_state
*get_ssa_state_expr(int owner
, struct expression
*expr
);
482 /* smatch_helper.c */
483 DECLARE_PTR_LIST(int_stack
, int);
484 char *alloc_string(const char *str
);
485 char *alloc_string_newline(const char *str
);
486 void free_string(char *str
);
487 void remove_parens(char *str
);
488 struct smatch_state
*alloc_state_num(int num
);
489 struct smatch_state
*alloc_state_str(const char *name
);
490 struct smatch_state
*merge_str_state(struct smatch_state
*s1
, struct smatch_state
*s2
);
491 struct smatch_state
*alloc_state_expr(struct expression
*expr
);
492 struct expression
*get_assigned_call(struct expression
*expr
);
493 struct expression
*get_argument_from_call_expr(struct expression_list
*args
,
495 struct expression
*get_array_expr(struct expression
*expr
);
497 char *expr_to_var(struct expression
*expr
);
498 struct symbol
*expr_to_sym(struct expression
*expr
);
499 char *expr_to_str(struct expression
*expr
);
500 char *expr_to_str_sym(struct expression
*expr
,
501 struct symbol
**sym_ptr
);
502 char *expr_to_var_sym(struct expression
*expr
,
503 struct symbol
**sym_ptr
);
504 char *expr_to_known_chunk_sym(struct expression
*expr
, struct symbol
**sym
);
505 char *expr_to_chunk_sym_vsl(struct expression
*expr
, struct symbol
**sym
, struct var_sym_list
**vsl
);
506 int get_complication_score(struct expression
*expr
);
508 int sym_name_is(const char *name
, struct expression
*expr
);
509 int get_const_value(struct expression
*expr
, sval_t
*sval
);
510 int get_value(struct expression
*expr
, sval_t
*val
);
511 int get_implied_value(struct expression
*expr
, sval_t
*val
);
512 int get_implied_value_fast(struct expression
*expr
, sval_t
*sval
);
513 int get_implied_min(struct expression
*expr
, sval_t
*sval
);
514 int get_implied_max(struct expression
*expr
, sval_t
*val
);
515 int get_hard_max(struct expression
*expr
, sval_t
*sval
);
516 int get_fuzzy_min(struct expression
*expr
, sval_t
*min
);
517 int get_fuzzy_max(struct expression
*expr
, sval_t
*max
);
518 int get_absolute_min(struct expression
*expr
, sval_t
*sval
);
519 int get_absolute_max(struct expression
*expr
, sval_t
*sval
);
520 int parse_call_math(struct expression
*expr
, char *math
, sval_t
*val
);
521 int parse_call_math_rl(struct expression
*call
, const char *math
, struct range_list
**rl
);
522 const char *get_allocation_math(struct expression
*expr
);
523 char *get_value_in_terms_of_parameter_math(struct expression
*expr
);
524 char *get_value_in_terms_of_parameter_math_var_sym(const char *var
, struct symbol
*sym
);
525 int expr_is_zero(struct expression
*expr
);
526 int known_condition_true(struct expression
*expr
);
527 int known_condition_false(struct expression
*expr
);
528 int implied_condition_true(struct expression
*expr
);
529 int implied_condition_false(struct expression
*expr
);
530 int can_integer_overflow(struct symbol
*type
, struct expression
*expr
);
531 void clear_math_cache(void);
532 void clear_strip_cache(void);
533 void set_fast_math_only(void);
534 void clear_fast_math_only(void);
536 int is_array(struct expression
*expr
);
537 struct expression
*get_array_base(struct expression
*expr
);
538 struct expression
*get_array_offset(struct expression
*expr
);
539 const char *show_state(struct smatch_state
*state
);
540 struct statement
*get_expression_statement(struct expression
*expr
);
541 struct expression
*strip__builtin_choose_expr(struct expression
*expr
);
542 struct expression
*strip_Generic(struct expression
*expr
);
543 struct expression
*strip_parens(struct expression
*expr
);
544 struct expression
*strip_expr(struct expression
*expr
);
545 struct expression
*strip_no_cast(struct expression
*expr
);
546 struct expression
*strip_expr_set_parent(struct expression
*expr
);
547 void scoped_state(int my_id
, const char *name
, struct symbol
*sym
);
548 int is_error_return(struct expression
*expr
);
549 int getting_address(struct expression
*expr
);
550 int get_struct_and_member(struct expression
*expr
, const char **type
, const char **member
);
551 char *get_member_name(struct expression
*expr
);
552 char *get_fnptr_name(struct expression
*expr
);
553 int cmp_pos(struct position pos1
, struct position pos2
);
554 int positions_eq(struct position pos1
, struct position pos2
);
555 struct statement
*get_current_statement(void);
556 struct statement
*get_prev_statement(void);
557 struct expression
*get_last_expr_from_expression_stmt(struct expression
*expr
);
559 enum { RET_SUCCESS
, RET_FAIL
, RET_UNKNOWN
};
560 int success_fail_return(struct range_list
*rl
);
562 #define RETURN_VAR -1
563 #define LOCAL_SCOPE -2
564 #define FILE_SCOPE -3
565 #define GLOBAL_SCOPE -4
566 #define UNKNOWN_SCOPE -5
568 char *swap_names(const char *orig
, const char *remove
, const char *add
);
569 char *get_param_var_sym_var_sym(const char *name
, struct symbol
*sym
, struct expression
*ret_expr
, struct symbol
**sym_p
);
570 char *get_param_name_sym(struct expression
*expr
, struct symbol
**sym_p
);
571 int get_return_param_key_from_var_sym(const char *name
, struct symbol
*sym
,
572 struct expression
*ret_expr
,
574 int get_param_key_from_var_sym(const char *name
, struct symbol
*sym
,
575 struct expression
*ret_expr
,
577 int get_param_key_from_sm(struct sm_state
*sm
, struct expression
*ret_expr
,
579 int get_param_key_from_expr(struct expression
*expr
, struct expression
*ret_expr
,
581 const char *get_param_key_swap_dollar(struct expression
*expr
);
582 int map_to_param(const char *name
, struct symbol
*sym
);
583 int get_param_num_from_sym(struct symbol
*sym
);
584 int get_param_num(struct expression
*expr
);
585 struct symbol
*get_param_sym_from_num(int num
);
586 struct expression
*map_container_of_to_simpler_expr_key(struct expression
*expr
, const char *orig_key
, char **new_key
);
587 const char *get_container_of_str(struct expression
*expr
);
588 bool get_offset_param(const char *ret_str
, int *offset
, int *param
);
589 /* smatch_points_to_container.c */
590 struct expression
*get_stored_container(struct expression
*expr
, int offset
);
592 int ms_since(struct timeval
*start
);
593 int parent_is_gone_var_sym(const char *name
, struct symbol
*sym
);
594 int parent_is_gone(struct expression
*expr
);
595 bool is_noderef_ptr(struct expression
*expr
);
596 int invert_op(int op
);
597 int op_remove_assign(int op
);
598 int expr_equiv(struct expression
*one
, struct expression
*two
);
599 void push_int(struct int_stack
**stack
, int num
);
600 int pop_int(struct int_stack
**stack
);
601 bool macro_to_ul(const char *macro
, unsigned long *val
);
604 struct symbol
*get_real_base_type(struct symbol
*sym
);
605 int type_bytes(struct symbol
*type
);
606 int array_bytes(struct symbol
*type
);
607 struct symbol
*get_pointer_type(struct expression
*expr
);
608 struct symbol
*get_type(struct expression
*expr
);
609 struct symbol
*get_comparison_type(struct expression
*expr
);
610 struct symbol
*get_final_type(struct expression
*expr
);
611 struct symbol
*get_promoted_type(struct symbol
*left
, struct symbol
*right
);
612 int type_signed(struct symbol
*base_type
);
613 int expr_unsigned(struct expression
*expr
);
614 int expr_signed(struct expression
*expr
);
615 int returns_unsigned(struct symbol
*base_type
);
616 int is_pointer(struct expression
*expr
);
617 bool is_void_ptr(struct symbol
*type
);
618 int returns_pointer(struct symbol
*base_type
);
619 sval_t
sval_type_max(struct symbol
*base_type
);
620 sval_t
sval_type_min(struct symbol
*base_type
);
621 int nr_bits(struct expression
*expr
);
622 int is_void_pointer(struct expression
*expr
);
623 int is_char_pointer(struct expression
*expr
);
624 int is_string(struct expression
*expr
);
625 bool is_struct_ptr(struct symbol
*type
);
626 int is_static(struct expression
*expr
);
627 bool is_local_variable(struct expression
*expr
);
628 int types_equiv(struct symbol
*one
, struct symbol
*two
);
629 bool type_fits(struct symbol
*type
, struct symbol
*test
);
631 const char *global_static();
632 struct symbol
*cur_func_return_type(void);
633 struct symbol
*get_arg_type(struct expression
*fn
, int arg
);
634 struct symbol
*get_member_type_from_key(struct expression
*expr
, const char *key
);
635 struct symbol
*get_arg_type_from_key(struct expression
*fn
, int param
, struct expression
*arg
, const char *key
);
636 int is_struct(struct expression
*expr
);
637 char *type_to_str(struct symbol
*type
);
639 /* smatch_ignore.c */
640 void add_ignore(int owner
, const char *name
, struct symbol
*sym
);
641 int is_ignored(int owner
, const char *name
, struct symbol
*sym
);
642 void add_ignore_expr(int owner
, struct expression
*expr
);
643 int is_ignored_expr(int owner
, struct expression
*expr
);
646 struct smatch_state
*alloc_var_sym_state(const char *var
, struct symbol
*sym
);
647 struct var_sym
*alloc_var_sym(const char *var
, struct symbol
*sym
);
648 struct var_sym_list
*expr_to_vsl(struct expression
*expr
);
649 void add_var_sym(struct var_sym_list
**list
, const char *var
, struct symbol
*sym
);
650 void add_var_sym_expr(struct var_sym_list
**list
, struct expression
*expr
);
651 void del_var_sym(struct var_sym_list
**list
, const char *var
, struct symbol
*sym
);
652 int in_var_sym_list(struct var_sym_list
*list
, const char *var
, struct symbol
*sym
);
653 struct var_sym_list
*clone_var_sym_list(struct var_sym_list
*from_vsl
);
654 void merge_var_sym_list(struct var_sym_list
**dest
, struct var_sym_list
*src
);
655 struct var_sym_list
*combine_var_sym_lists(struct var_sym_list
*one
, struct var_sym_list
*two
);
656 int var_sym_lists_equiv(struct var_sym_list
*one
, struct var_sym_list
*two
);
657 void free_var_sym_list(struct var_sym_list
**list
);
658 void free_var_syms_and_list(struct var_sym_list
**list
);
661 struct tracker
*alloc_tracker(int owner
, const char *name
, struct symbol
*sym
);
662 void add_tracker(struct tracker_list
**list
, int owner
, const char *name
,
664 void add_tracker_expr(struct tracker_list
**list
, int owner
, struct expression
*expr
);
665 void del_tracker(struct tracker_list
**list
, int owner
, const char *name
,
667 int in_tracker_list(struct tracker_list
*list
, int owner
, const char *name
,
669 void free_tracker_list(struct tracker_list
**list
);
670 void free_trackers_and_list(struct tracker_list
**list
);
672 /* smatch_conditions */
673 int in_condition(void);
677 extern int __in_fake_assign
;
678 extern int __in_fake_parameter_assign
;
679 extern int __in_fake_struct_assign
;
680 extern int __in_buf_clear
;
681 extern int __in_fake_var_assign
;
682 extern int __in_builtin_overflow_func
;
683 extern int __fake_state_cnt
;
684 extern int __debug_skip
;
685 extern int in_fake_env
;
686 bool is_fake_var_assign(struct expression
*expr
);
687 void smatch (struct string_list
*filelist
);
688 int inside_loop(void);
689 int definitely_inside_loop(void);
690 void add_once_through_hook(bool_stmt_func
*fn
);
691 struct expression
*get_switch_expr(void);
692 int in_expression_statement(void);
693 void __process_post_op_stack(void);
694 void parse_assignment(struct expression
*expr
, bool shallow
);
695 void __split_expr(struct expression
*expr
);
696 void __split_label_stmt(struct statement
*stmt
);
697 void __split_stmt(struct statement
*stmt
);
698 extern int __in_function_def
;
699 extern int __in_unmatched_hook
;
700 extern int option_assume_loops
;
701 extern int option_two_passes
;
702 extern int option_no_db
;
703 extern int option_file_output
;
704 extern int option_time
;
705 extern int option_time_stmt
;
706 extern struct expression_list
*big_expression_stack
;
707 extern struct expression_list
*big_condition_stack
;
708 extern struct statement_list
*big_statement_stack
;
709 int is_condition_call(struct expression
*expr
);
710 int is_assigned_call(struct expression
*expr
);
711 int is_fake_assigned_call(struct expression
*expr
);
712 void add_function_data(unsigned long *fn_data
);
713 int inlinable(struct expression
*expr
);
714 extern int __inline_call
;
715 extern struct expression
*__inline_fn
;
716 extern int __in_pre_condition
;
717 extern int __bail_on_rest_of_function
;
718 extern struct statement
*__prev_stmt
;
719 extern struct statement
*__cur_stmt
;
720 extern struct statement
*__next_stmt
;
721 void init_fake_env(void);
722 void end_fake_env(void);
723 int time_parsing_function(void);
724 bool taking_too_long(void);
725 struct statement
*get_last_stmt(void);
726 int is_last_stmt(struct statement
*cur_stmt
);
728 /* smatch_struct_assignment.c */
729 struct expression
*get_faked_expression(void);
730 void __fake_struct_member_assignments(struct expression
*expr
);
731 void create_recursive_fake_assignments(struct expression
*expr
,
732 void (*assign_handler
)(struct expression
*expr
, void *data
),
735 /* smatch_project.c */
736 int is_no_inline_function(const char *function
);
738 /* smatch_conditions */
739 void __split_whole_condition(struct expression
*expr
);
740 void __handle_logic(struct expression
*expr
);
741 int is_condition(struct expression
*expr
);
742 int __handle_condition_assigns(struct expression
*expr
);
743 int __handle_select_assigns(struct expression
*expr
);
744 int __handle_expr_statement_assigns(struct expression
*expr
);
746 /* smatch_implied.c */
747 struct range_list_stack
;
748 void param_limit_implications(struct expression
*expr
, int param
, char *key
, char *value
, struct stree
**implied
);
749 struct stree
*__implied_case_stree(struct expression
*switch_expr
,
750 struct range_list
*case_rl
,
751 struct range_list_stack
**remaining_cases
,
752 struct stree
**raw_stree
);
753 void overwrite_states_using_pool(struct sm_state
*gate_sm
, struct sm_state
*pool_sm
);
754 int assume(struct expression
*expr
);
755 void end_assume(void);
756 int impossible_assumption(struct expression
*left
, int op
, sval_t sval
);
759 bool has_dynamic_states(unsigned short owner
);
760 void set_dynamic_states(unsigned short owner
);
762 /* smatch_extras.c */
763 int in_warn_on_macro(void);
764 extern int SMATCH_EXTRA
;
765 extern int RETURN_ID
;
772 char *get_other_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
);
773 char *map_call_to_other_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
);
774 char *map_long_to_short_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
, bool use_stack
);
776 #define STRLEN_MAX_RET 1010101
778 /* smatch_absolute.c */
779 int get_absolute_min_helper(struct expression
*expr
, sval_t
*sval
);
780 int get_absolute_max_helper(struct expression
*expr
, sval_t
*sval
);
782 /* smatch_type_value.c */
783 void disable_type_val_lookups(void);
784 void enable_type_val_lookups(void);
785 void clear_type_value_cache(void);
786 int get_db_type_rl(struct expression
*expr
, struct range_list
**rl
);
788 /* smatch_data_val.c */
789 int get_mtag_rl(struct expression
*expr
, struct range_list
**rl
);
790 /* smatch_array_values.c */
791 void clear_array_values_cache(void);
792 int get_array_rl(struct expression
*expr
, struct range_list
**rl
);
794 /* smatch_states.c */
795 struct stree
*__swap_cur_stree(struct stree
*stree
);
796 void __push_fake_cur_stree();
797 struct stree
*__pop_fake_cur_stree();
798 void __free_fake_cur_stree();
799 void __set_fake_cur_stree_fast(struct stree
*stree
);
800 void __pop_fake_cur_stree_fast(void);
801 void __merge_stree_into_cur(struct stree
*stree
);
803 int unreachable(void);
804 void __set_cur_stree_readonly(void);
805 void __set_cur_stree_writable(void);
806 void __set_sm(struct sm_state
*sm
);
807 void __set_sm_cur_stree(struct sm_state
*sm
);
808 void __set_true_false_sm(struct sm_state
*true_state
,
809 struct sm_state
*false_state
);
810 void nullify_path(void);
811 void __match_nullify_path_hook(const char *fn
, struct expression
*expr
,
813 void __unnullify_path(void);
814 int __path_is_null(void);
815 void save_all_states(void);
816 void restore_all_states(void);
817 void free_goto_stack(void);
818 void clear_all_states(void);
820 struct sm_state
*get_sm_state(int owner
, const char *name
,
822 struct sm_state
*get_sm_state_expr(int owner
, struct expression
*expr
);
823 void __push_true_states(void);
824 void __use_false_states(void);
825 void __discard_false_states(void);
826 void __merge_false_states(void);
827 void __merge_true_states(void);
829 void __negate_cond_stacks(void);
830 void __use_pre_cond_states(void);
831 void __use_cond_true_states(void);
832 void __use_cond_false_states(void);
833 void __push_cond_stacks(void);
834 void __fold_in_set_states(void);
835 void __free_set_states(void);
836 struct stree
*__copy_cond_true_states(void);
837 struct stree
*__copy_cond_false_states(void);
838 struct stree
*__pop_cond_true_stack(void);
839 struct stree
*__pop_cond_false_stack(void);
840 void __and_cond_states(void);
841 void __or_cond_states(void);
842 void __save_pre_cond_states(void);
843 void __discard_pre_cond_states(void);
844 struct stree
*__get_true_states(void);
845 struct stree
*__get_false_states(void);
846 void __use_cond_states(void);
847 extern struct state_list
*__last_base_slist
;
849 void __push_continues(void);
850 void __discard_continues(void);
851 void __process_continues(void);
852 void __merge_continues(void);
854 void __push_breaks(void);
855 void __process_breaks(void);
856 int __has_breaks(void);
857 void __merge_breaks(void);
858 void __use_breaks(void);
860 void __save_switch_states(struct expression
*switch_expr
);
861 void __discard_switches(void);
862 int have_remaining_cases(void);
863 void __merge_switches(struct expression
*switch_expr
, struct range_list
*case_rl
);
864 void __push_default(void);
865 void __set_default(void);
866 bool __has_default_case(void);
867 int __pop_default(void);
869 void __push_conditions(void);
870 void __discard_conditions(void);
872 void __save_gotos(const char *name
, struct symbol
*sym
);
873 void __merge_gotos(const char *name
, struct symbol
*sym
);
875 void __discard_fake_states(struct expression
*call
);
877 void __print_cur_stree(void);
878 bool __print_states(const char *owner
);
879 typedef void (check_tracker_hook
)(int owner
, const char *name
, struct symbol
*sym
, struct smatch_state
*state
);
880 void add_check_tracker(const char *check_name
, check_tracker_hook
*fn
);
883 void __pass_to_client(void *data
, enum hook_type type
);
884 void __pass_case_to_client(struct expression
*switch_expr
,
885 struct range_list
*rl
);
886 int __has_merge_function(int client_id
);
887 struct smatch_state
*__client_merge_function(int owner
,
888 struct smatch_state
*s1
,
889 struct smatch_state
*s2
);
890 struct smatch_state
*__client_unmatched_state_function(struct sm_state
*sm
);
891 void call_pre_merge_hook(struct sm_state
*cur
, struct sm_state
*other
);
892 void __push_scope_hooks(void);
893 void __call_scope_hooks(void);
894 void add_array_initialized_hook(void (*hook
)(struct expression
*array
, int nr
));
895 void __call_array_initialized_hooks(struct expression
*array
, int nr
);
897 /* smatch_function_hooks.c */
898 void add_fake_call_after_return(struct expression
*call
);
899 void create_function_hook_hash(void);
900 void __match_initializer_call(struct symbol
*sym
);
902 struct expression
*get_unfaked_call(void);
903 void fake_param_assign_helper(struct expression
*call
, struct expression
*fake_assign
, bool shallow
);
909 * Changing these numbers is a pain. Don't do it. If you ever use a
910 * number it can't be re-used right away so there may be gaps.
911 * We select these in order by type so if the order matters, then give
912 * it a number below 100-999,9000-9999 ranges. */
927 ABSOLUTE_LIMITS
= 1010,
941 UNTRACKED_PARAM
= 1023,
947 COMPARE_LIMIT
= 1028,
948 PARAM_COMPARE
= 1029,
951 CONSTRAINT_REQUIRED
= 1033,
962 POWER_OF_TWO_SET
= 1049,
968 NEGATIVE_ERROR
= 1057,
973 TASK_NOT_RUNNING
= 2064,
974 /* put random temporary stuff in the 7000-7999 range for testing */
976 HOST_DATA_SET
= 7017,
980 USER_DATA_SET
= 9017,
984 NO_OVERFLOW_SIMPLE
= 8019,
990 KNOWN_UNLOCKED
= 9025,
995 REFCOUNT_INIT
= 9025,
998 NO_SIDE_EFFECT
= 8025,
1001 ARRAYSIZE_ARG
= 8033,
1006 STRING_VALUE
= 8041,
1015 /* Do not use numbers above 100k */
1018 extern struct sqlite3
*smatch_db
;
1019 extern struct sqlite3
*mem_db
;
1020 extern struct sqlite3
*cache_db
;
1022 bool db_incomplete(void);
1023 void db_ignore_states(int id
);
1024 typedef bool (delete_hook
)(struct expression
*expr
);
1025 void add_delete_return_hook(delete_hook
*hook
);
1026 void select_caller_info_hook(void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
), int type
);
1027 void select_caller_name_sym(void (*fn
)(const char *name
, struct symbol
*sym
, char *value
), int type
);
1028 void add_member_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
));
1029 void add_caller_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
));
1030 void add_return_info_callback(int owner
,
1031 void (*callback
)(int return_id
, char *return_ranges
,
1032 struct expression
*returned_expr
,
1034 const char *printed_name
,
1035 struct sm_state
*sm
));
1036 void add_split_return_callback(void (*fn
)(int return_id
, char *return_ranges
, struct expression
*returned_expr
));
1037 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
));
1038 void select_call_implies_hook(int type
, void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
));
1039 void select_return_implies_hook_early(int type
, void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
));
1040 void select_return_implies_hook(int type
, void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
));
1041 struct range_list
*db_return_vals(struct expression
*expr
);
1042 struct range_list
*db_return_vals_from_str(const char *fn_name
);
1043 struct range_list
*db_return_vals_no_args(struct expression
*expr
);
1044 bool split_param_key(const char *value
, int *param
, char *key
, int len
);
1045 bool get_implied_rl_from_call_str(struct expression
*expr
, const char *data
, struct range_list
**rl
);
1046 char *get_chunk_from_key(struct expression
*arg
, char *key
, struct symbol
**sym
, struct var_sym_list
**vsl
);
1047 char *get_variable_from_key(struct expression
*arg
, const char *key
, struct symbol
**sym
);
1048 struct expression
*get_function_param(struct expression
*expr
, int param
);
1049 char *get_name_sym_from_param_key(struct expression
*expr
, int param
, const char *key
, struct symbol
**sym
);
1050 char *get_param_name_var_sym(const char *name
, struct symbol
*sym
);
1051 char *get_param_name(struct sm_state
*sm
);
1052 const char *get_mtag_name_var_sym(const char *state_name
, struct symbol
*sym
);
1053 const char *get_mtag_name_expr(struct expression
*expr
);
1054 char *get_data_info_name(struct expression
*expr
);
1055 char *sm_to_arg_name(struct expression
*expr
, struct sm_state
*sm
);
1056 int is_recursive_member(const char *param_name
);
1058 char *escape_newlines(const char *str
);
1059 void sql_exec(struct sqlite3
*db
, int (*callback
)(void*, int, char**, char**), void *data
, const char *sql
);
1061 #define sql_helper(db, call_back, data, sql...) \
1063 char sql_txt[1024]; \
1065 sqlite3_snprintf(sizeof(sql_txt), sql_txt, sql); \
1066 db_debug("debug: %s\n", sql_txt); \
1067 sql_exec(db, call_back, data, sql_txt); \
1071 #define run_sql(call_back, data, sql...) \
1075 sql_helper(smatch_db, call_back, data, sql); \
1078 #define mem_sql(call_back, data, sql...) \
1079 sql_helper(mem_db, call_back, data, sql)
1081 #define cache_sql(call_back, data, sql...) \
1082 sql_helper(cache_db, call_back, data, sql)
1084 #define sql_insert_helper(table, db, ignore, late, values...) \
1086 struct sqlite3 *_db = db; \
1088 if (__inline_fn && !_db) \
1092 char *err, *p = buf; \
1095 p += snprintf(p, buf + sizeof(buf) - p, \
1096 "insert %sinto %s values (", \
1097 ignore ? "or ignore " : "", #table); \
1098 p += snprintf(p, buf + sizeof(buf) - p, values); \
1099 p += snprintf(p, buf + sizeof(buf) - p, ");"); \
1100 db_debug("mem-db: %s\n", buf); \
1101 rc = sqlite3_exec(_db, buf, NULL, NULL, &err); \
1102 if (rc != SQLITE_OK) { \
1103 sm_ierror("SQL error #2: %s", err); \
1104 sm_ierror("SQL: '%s'", buf); \
1109 if (option_info) { \
1110 FILE *tmp_fd = sm_outfd; \
1111 sm_outfd = sql_outfd; \
1113 sm_printf("SQL%s: insert %sinto " #table " values(", \
1114 late ? "_late" : "", ignore ? "or ignore " : ""); \
1115 sm_printf(values); \
1116 sm_printf(");\n"); \
1117 sm_outfd = tmp_fd; \
1121 #define sql_insert(table, values...) sql_insert_helper(table, NULL, 0, 0, values);
1122 #define sql_insert_or_ignore(table, values...) sql_insert_helper(table, NULL, 1, 0, values);
1123 #define sql_insert_late(table, values...) sql_insert_helper(table, NULL, 0, 1, values);
1124 #define sql_insert_cache(table, values...) sql_insert_helper(table, cache_db, 1, 0, values);
1125 #define sql_insert_cache_or_ignore(table, values...) sql_insert_helper(table, cache_db, 1, 0, values);
1127 char *get_static_filter(struct symbol
*sym
);
1129 void sql_insert_return_states(int return_id
, const char *return_ranges
,
1130 int type
, int param
, const char *key
, const char *value
);
1131 void sql_insert_caller_info(struct expression
*call
, int type
, int param
,
1132 const char *key
, const char *value
);
1133 void sql_insert_function_ptr(const char *fn
, const char *struct_name
);
1134 void sql_insert_return_values(const char *return_values
);
1135 void sql_insert_return_implies(int type
, int param
, const char *key
, const char *value
);
1136 void sql_insert_function_type_size(const char *member
, const char *ranges
);
1137 void sql_insert_function_type_info(int type
, const char *struct_type
, const char *member
, const char *value
);
1138 void sql_insert_type_info(int type
, const char *member
, const char *value
);
1139 void sql_insert_local_values(const char *name
, const char *value
);
1140 void sql_insert_function_type_value(const char *type
, const char *value
);
1141 void sql_insert_function_type(int param
, const char *value
);
1142 void sql_insert_parameter_name(int param
, const char *value
);
1143 void sql_insert_data_info(struct expression
*data
, int type
, const char *value
);
1144 void sql_insert_data_info_var_sym(const char *var
, struct symbol
*sym
, int type
, const char *value
);
1145 void sql_save_constraint(const char *con
);
1146 void sql_save_constraint_required(const char *data
, int op
, const char *limit
);
1147 void sql_copy_constraint_required(const char *new_limit
, const char *old_limit
);
1148 void sql_insert_fn_ptr_data_link(const char *ptr
, const char *data
);
1149 void sql_insert_fn_data_link(struct expression
*fn
, int type
, int param
, const char *key
, const char *value
);
1150 void sql_insert_mtag_about(mtag_t tag
, const char *left_name
, const char *right_name
);
1151 void sql_insert_mtag_info(mtag_t tag
, int type
, const char *value
);
1152 void sql_insert_mtag_map(mtag_t container
, int container_offset
, mtag_t tag
, int tag_offset
);
1153 void sql_insert_mtag_alias(mtag_t orig
, mtag_t alias
);
1154 int mtag_map_select_container(mtag_t tag
, int container_offset
, mtag_t
*container
);
1155 int mtag_map_select_tag(mtag_t container
, int offset
, mtag_t
*tag
);
1156 struct smatch_state
*get_mtag_return(struct expression
*expr
, struct smatch_state
*state
);
1157 struct range_list
*swap_mtag_seed(struct expression
*expr
, struct range_list
*rl
);
1159 bool is_fn_ptr(struct expression
*fn
);
1160 void sql_select_return_states(const char *cols
, struct expression
*call
,
1161 int (*callback
)(void*, int, char**, char**), void *info
);
1162 void sql_select_call_implies(const char *cols
, struct expression
*call
,
1163 int (*callback
)(void*, int, char**, char**));
1165 void open_smatch_db(char *db_file
);
1167 /* smatch_files.c */
1168 int open_data_file(const char *filename
);
1169 int open_schema_file(const char *schema
);
1170 struct token
*get_tokens_file(const char *filename
);
1171 struct string_list
*load_strings_from_file(const char *project
, const char *filename
);
1174 extern char *option_debug_check
;
1175 extern char *option_debug_var
;
1176 extern char *option_process_function
;
1177 extern char *option_project_str
;
1178 extern char *bin_dir
;
1179 extern char *data_dir
;
1180 extern int option_no_data
;
1181 extern int option_full_path
;
1182 extern int option_call_tree
;
1183 extern int num_checks
;
1189 PROJ_ILLUMOS_KERNEL
,
1193 extern enum project_type option_project
;
1194 const char *check_name(unsigned short id
);
1195 int id_from_name(const char *name
);
1198 /* smatch_buf_size.c */
1199 int bytes_to_elements(struct expression
*expr
, int bytes
);
1200 int get_array_size(struct expression
*expr
);
1201 int get_array_size_bytes(struct expression
*expr
);
1202 int get_array_size_bytes_min(struct expression
*expr
);
1203 int get_array_size_bytes_max(struct expression
*expr
);
1204 struct range_list
*get_array_size_bytes_rl(struct expression
*expr
);
1205 int get_real_array_size(struct expression
*expr
);
1206 int last_member_is_resizable(struct symbol
*type
);
1207 /* smatch_strlen.c */
1208 bool is_strlen(struct expression
*expr
);
1209 int get_implied_strlen(struct expression
*expr
, struct range_list
**rl
);
1210 int get_size_from_strlen(struct expression
*expr
);
1212 /* smatch_capped.c */
1213 int is_capped(struct expression
*expr
);
1214 int is_capped_var_sym(const char *name
, struct symbol
*sym
);
1216 /* smatch_kernel_user_data.c */
1217 int is_user_macro(struct expression
*expr
);
1218 int is_capped_user_data(struct expression
*expr
);
1219 int implied_user_data(struct expression
*expr
, struct range_list
**rl
);
1220 struct stree
*get_user_stree(void);
1221 int get_user_rl(struct expression
*expr
, struct range_list
**rl
);
1222 int is_user_rl(struct expression
*expr
);
1223 int get_user_rl_var_sym(const char *name
, struct symbol
*sym
, struct range_list
**rl
);
1224 void mark_as_user_data(struct expression
*expr
, bool isnew
);
1225 bool user_rl_capped_var_sym(const char *name
, struct symbol
*sym
);
1226 bool user_rl_capped(struct expression
*expr
);
1227 struct range_list
*var_user_rl(struct expression
*expr
);
1228 bool we_pass_user_data(struct expression
*call
);
1229 /* smatch_points_to_user_data.c */
1230 bool is_user_data_fn(struct symbol
*fn
);
1231 bool is_skb_data(struct expression
*expr
);
1232 bool is_socket_stuff(struct symbol
*sym
);
1233 bool points_to_user_data(struct expression
*expr
);
1234 void set_array_user_ptr(struct expression
*expr
, bool is_new
);
1236 /* smatch_kernel_host_data.c */
1237 int is_host_macro(struct expression
*expr
);
1238 int get_host_data_fn_param(const char *fn
);
1239 int is_capped_host_data(struct expression
*expr
);
1240 int implied_host_data(struct expression
*expr
, struct range_list
**rl
);
1241 struct stree
*get_host_stree(void);
1242 int get_host_rl(struct expression
*expr
, struct range_list
**rl
);
1243 int is_host_rl(struct expression
*expr
);
1244 int get_host_rl_var_sym(const char *name
, struct symbol
*sym
, struct range_list
**rl
);
1245 bool host_rl_capped(struct expression
*expr
);
1246 struct range_list
*var_host_rl(struct expression
*expr
);
1247 bool we_pass_host_data(struct expression
*call
);
1248 /* smatch_points_to_host_data.c */
1249 bool is_host_data_fn(struct symbol
*fn
);
1250 bool points_to_host_data(struct expression
*expr
);
1251 void set_points_to_host_data(struct expression
*expr
, bool is_new
);
1252 bool is_fn_points_to_host_data(const char *fn
);
1253 /* check_locking.c */
1254 void print_held_locks();
1256 void __preempt_add(void);
1257 void __preempt_sub(void);
1258 int get_preempt_cnt(void);
1259 void clear_preempt_cnt(void);
1260 bool function_decrements_preempt(void);
1261 void add_sleep_callback(expr_func
*fn
);
1262 unsigned long GFP_DIRECT_RECLAIM(void);
1263 unsigned long GFP_ATOMIC(void);
1265 /* check_assigned_expr.c */
1266 extern int check_assigned_expr_id
;
1267 struct expression
*get_assigned_expr(struct expression
*expr
);
1268 struct sm_state
*get_assigned_sm(struct expression
*expr
);
1269 struct expression
*get_assigned_expr_recurse(struct expression
*expr
);
1270 struct expression
*get_assigned_expr_name_sym(const char *name
, struct symbol
*sym
);
1271 struct expression
*get_assigned_expr_name_sym_recurse(const char *name
, struct symbol
*sym
);
1273 /* smatch_return_to_param.c */
1274 char *map_call_to_param_name_sym(struct expression
*expr
, struct symbol
**sym
);
1276 /* smatch_comparison.c */
1277 extern int comparison_id
;
1278 #define UNKNOWN_COMPARISON 0
1279 #define IMPOSSIBLE_COMPARISON -1
1280 struct compare_data
{
1281 /* The ->left and ->right expression pointers might be NULL (I'm lazy) */
1282 struct expression
*left
;
1283 const char *left_var
;
1284 struct var_sym_list
*left_vsl
;
1286 struct expression
*right
;
1287 const char *right_var
;
1288 struct var_sym_list
*right_vsl
;
1290 DECLARE_ALLOCATOR(compare_data
);
1291 struct smatch_state
*alloc_compare_state(
1292 struct expression
*left
,
1293 const char *left_var
, struct var_sym_list
*left_vsl
,
1295 struct expression
*right
,
1296 const char *right_var
, struct var_sym_list
*right_vsl
);
1297 int comparison_intersection(int orig
, int op
);
1298 int merge_comparisons(int one
, int two
);
1299 int combine_comparisons(int left_compare
, int right_compare
);
1300 int state_to_comparison(struct smatch_state
*state
);
1301 struct smatch_state
*merge_compare_states(struct smatch_state
*s1
, struct smatch_state
*s2
);
1302 int get_comparison(struct expression
*left
, struct expression
*right
);
1303 int get_comparison_no_extra(struct expression
*a
, struct expression
*b
);
1304 int get_comparison_strings(const char *one
, const char *two
);
1305 int possible_comparison(struct expression
*a
, int comparison
, struct expression
*b
);
1306 struct state_list
*get_all_comparisons(struct expression
*expr
);
1307 struct state_list
*get_all_possible_equal_comparisons(struct expression
*expr
);
1308 void __add_comparison_info(struct expression
*expr
, struct expression
*call
, const char *range
);
1309 char *get_printed_param_name(struct expression
*call
, const char *param_name
, struct symbol
*param_sym
);
1310 char *name_sym_to_param_comparison(const char *name
, struct symbol
*sym
);
1311 char *expr_equal_to_param(struct expression
*expr
, int ignore
);
1312 char *expr_lte_to_param(struct expression
*expr
, int ignore
);
1313 char *expr_param_comparison(struct expression
*expr
, int ignore
);
1314 int flip_comparison(int op
);
1315 int negate_comparison(int op
);
1316 int remove_unsigned_from_comparison(int op
);
1317 int param_compare_limit_is_impossible(struct expression
*expr
, int left_param
, char *left_key
, char *value
);
1318 void filter_by_comparison(struct range_list
**rl
, int comparison
, struct range_list
*right
);
1319 void __compare_param_limit_hook(struct expression
*left_expr
, struct expression
*right_expr
,
1320 const char *state_name
,
1321 struct smatch_state
*true_state
, struct smatch_state
*false_state
);
1322 int impossibly_high_comparison(struct expression
*expr
);
1323 void add_comparison_var_sym(
1324 struct expression
*left_expr
,
1325 const char *left_name
, struct var_sym_list
*left_vsl
,
1327 struct expression
*right_expr
,
1328 const char *right_name
, struct var_sym_list
*right_vsl
,
1329 struct expression
*mod_expr
);
1332 sval_t
*sval_alloc(sval_t sval
);
1333 sval_t
*sval_alloc_permanent(sval_t sval
);
1334 sval_t
sval_blank(struct expression
*expr
);
1335 sval_t
sval_type_val(struct symbol
*type
, long long val
);
1336 sval_t
sval_type_fval(struct symbol
*type
, long double fval
);
1337 sval_t
sval_from_val(struct expression
*expr
, long long val
);
1338 sval_t
sval_from_fval(struct expression
*expr
, long double fval
);
1339 int sval_is_ptr(sval_t sval
);
1340 bool sval_is_fp(sval_t sval
);
1341 int sval_unsigned(sval_t sval
);
1342 int sval_signed(sval_t sval
);
1343 int sval_bits(sval_t sval
);
1344 int sval_bits_used(sval_t sval
);
1345 int sval_is_negative(sval_t sval
);
1346 int sval_is_positive(sval_t sval
);
1347 int sval_is_min(sval_t sval
);
1348 int sval_is_max(sval_t sval
);
1349 int sval_is_a_min(sval_t sval
);
1350 int sval_is_a_max(sval_t sval
);
1351 int sval_is_negative_min(sval_t sval
);
1352 int sval_cmp_t(struct symbol
*type
, sval_t one
, sval_t two
);
1353 int sval_cmp_val(sval_t one
, long long val
);
1354 sval_t
sval_min(sval_t one
, sval_t two
);
1355 sval_t
sval_min_nonneg(sval_t one
, sval_t two
);
1356 sval_t
sval_max(sval_t one
, sval_t two
);
1357 int sval_too_low(struct symbol
*type
, sval_t sval
);
1358 int sval_too_high(struct symbol
*type
, sval_t sval
);
1359 int sval_fits(struct symbol
*type
, sval_t sval
);
1360 sval_t
sval_cast(struct symbol
*type
, sval_t sval
);
1361 sval_t
sval_preop(sval_t sval
, int op
);
1362 sval_t
sval_binop(sval_t left
, int op
, sval_t right
);
1363 int sval_binop_overflows(sval_t left
, int op
, sval_t right
);
1364 int sval_binop_overflows_no_sign(sval_t left
, int op
, sval_t right
);
1365 int find_first_zero_bit(unsigned long long uvalue
);
1366 int sm_fls64(unsigned long long uvalue
);
1367 unsigned long long fls_mask(unsigned long long uvalue
);
1368 unsigned long long sval_fls_mask(sval_t sval
);
1369 const char *sval_to_str(sval_t sval
);
1370 const char *sval_to_str_or_err_ptr(sval_t sval
);
1371 const char *sval_to_numstr(sval_t sval
);
1372 sval_t
ll_to_sval(long long val
);
1374 /* smatch_string_list.c */
1375 int list_has_string(struct string_list
*str_list
, const char *str
);
1376 int insert_string(struct string_list
**str_list
, const char *str
);
1377 struct string_list
*clone_str_list(struct string_list
*orig
);
1378 struct string_list
*combine_string_lists(struct string_list
*one
, struct string_list
*two
);
1380 /* smatch_start_states.c */
1381 struct stree
*get_start_states(void);
1383 /* smatch_recurse.c */
1384 int recurse(struct expression
*expr
,
1385 int (func
)(struct expression
*expr
, void *p
),
1386 void *param
, int nr
);
1387 int has_symbol(struct expression
*expr
, struct symbol
*sym
);
1388 int has_variable(struct expression
*expr
, struct expression
*var
);
1389 int has_inc_dec(struct expression
*expr
);
1391 /* smatch_stored_conditions.c */
1392 struct smatch_state
*get_stored_condition(struct expression
*expr
);
1393 struct expression_list
*get_conditions(struct expression
*expr
);
1394 struct sm_state
*stored_condition_implication_hook(struct expression
*expr
,
1395 struct state_list
**true_stack
,
1396 struct state_list
**false_stack
);
1397 /* smatch_parsed_conditions.c */
1398 struct sm_state
*parsed_condition_implication_hook(struct expression
*expr
,
1399 struct state_list
**true_stack
,
1400 struct state_list
**false_stack
);
1401 /* smatch_comparison.c */
1402 struct sm_state
*comparison_implication_hook(struct expression
*expr
,
1403 struct state_list
**true_stack
,
1404 struct state_list
**false_stack
);
1406 /* check_string_len.c */
1407 int get_formatted_string_size(struct expression
*call
, int arg
);
1408 int get_formatted_string_min_size(struct expression
*call
, int arg
);
1410 /* smatch_param_set.c */
1411 int param_was_set(struct expression
*expr
);
1412 int param_was_set_var_sym(const char *name
, struct symbol
*sym
);
1413 void print_limited_param_set(int return_id
, char *return_ranges
, struct expression
*expr
);
1415 void __promote_sets_to_clears(int return_id
, char *return_ranges
, struct expression
*expr
);
1416 bool parent_was_PARAM_CLEAR(const char *name
, struct symbol
*sym
);
1417 bool parent_was_PARAM_CLEAR_ZERO(const char *name
, struct symbol
*sym
);
1419 /* smatch_param_filter.c */
1420 int param_has_filter_data(struct sm_state
*sm
);
1422 /* smatch_links.c */
1423 void set_up_link_functions(int id
, int linkid
);
1424 struct smatch_state
*merge_link_states(struct smatch_state
*s1
, struct smatch_state
*s2
);
1425 void store_link(int link_id
, const char *name
, struct symbol
*sym
, const char *link_name
, struct symbol
*link_sym
);
1427 /* check_buf_comparison */
1428 const char *limit_type_str(unsigned int limit_type
);
1429 struct expression
*get_size_variable(struct expression
*buf
, int *limit_type
);
1430 struct expression
*get_array_variable(struct expression
*size
);
1431 int buf_comparison_index_ok(struct expression
*expr
);
1432 bool buf_comp_has_bytes(struct expression
*buf
, struct expression
*var
);
1433 bool buf_comp2_has_bytes(struct expression
*buf_expr
, struct expression
*var
);
1435 /* smatch_untracked_param.c */
1436 void mark_untracked(struct expression
*expr
, int param
, const char *key
, const char *value
);
1437 void mark_call_params_untracked(struct expression
*call
);
1438 void add_untracked_param_hook(void (func
)(struct expression
*call
, int param
));
1439 void add_lost_param_hook(void (func
)(struct expression
*call
, int param
));
1440 void mark_all_params_untracked(int return_id
, char *return_ranges
, struct expression
*expr
);
1441 /* smatch_untracked_var.c */
1442 bool is_untracked(struct expression
*expr
);
1444 /* smatch_strings.c */
1445 struct state_list
*get_strings(struct expression
*expr
);
1446 struct expression
*fake_string_from_mtag(mtag_t tag
);
1448 /* smatch_estate.c */
1449 int estate_get_single_value(struct smatch_state
*state
, sval_t
*sval
);
1451 /* smatch_address.c */
1452 int get_address_rl(struct expression
*expr
, struct range_list
**rl
);
1453 int get_member_offset(struct symbol
*type
, const char *member_name
);
1454 int get_member_offset_from_deref(struct expression
*expr
);
1456 /* for now this is in smatch_used_parameter.c */
1457 void __get_state_hook(int owner
, const char *name
, struct symbol
*sym
);
1458 extern int __ignore_param_used
;
1460 /* smatch_buf_comparison.c */
1461 int db_var_is_array_limit(struct expression
*array
, const char *name
, struct var_sym_list
*vsl
);
1463 struct range_list
*get_fs(void);
1465 struct stree
*get_all_return_states(void);
1466 struct stree_stack
*get_all_return_strees(void);
1467 int on_atomic_dec_path(void);
1468 int was_inced(const char *name
, struct symbol
*sym
);
1469 void set_refcount_inc(char *name
, struct symbol
*sym
);
1470 void set_refcount_dec(char *name
, struct symbol
*sym
);
1471 void add_refcount_init_hook(name_sym_hook
*hook
);
1472 void add_refcount_inc_hook(name_sym_hook
*hook
);
1473 void add_refcount_dec_hook(name_sym_hook
*hook
);
1475 /* smatch_constraints.c */
1476 char *get_constraint_str(struct expression
*expr
);
1477 struct constraint_list
*get_constraints(struct expression
*expr
);
1478 char *unmet_constraint(struct expression
*data
, struct expression
*offset
);
1479 char *get_required_constraint(const char *data_str
);
1481 /* smatch_container_of.c */
1482 int get_param_from_container_of(struct expression
*expr
);
1483 int get_offset_from_container_of(struct expression
*expr
);
1484 char *get_container_name(struct expression
*container
, struct expression
*expr
);
1487 unsigned long long str_to_llu_hash_helper(const char *str
);
1488 unsigned long long str_to_llu_hash(const char *str
);
1489 struct symbol
*get_symbol_from_mtag(mtag_t tag
);
1490 mtag_t
str_to_mtag(const char *str
);
1491 int get_string_mtag(struct expression
*expr
, mtag_t
*tag
);
1492 int get_toplevel_mtag(struct symbol
*sym
, mtag_t
*tag
);
1493 int create_mtag_alias(mtag_t tag
, struct expression
*expr
, mtag_t
*new);
1494 int expr_to_mtag_offset(struct expression
*expr
, mtag_t
*tag
, int *offset
);
1495 void update_mtag_data(struct expression
*expr
, struct smatch_state
*state
);
1496 int get_mtag_sval(struct expression
*expr
, sval_t
*sval
);
1498 /* Trinity fuzzer stuff */
1499 const char *get_syscall_arg_type(struct symbol
*sym
);
1502 int binfo_equiv(struct bit_info
*one
, struct bit_info
*two
);
1503 struct bit_info
*alloc_bit_info(unsigned long long set
, unsigned long long possible
);
1504 struct smatch_state
*alloc_bstate(unsigned long long set
, unsigned long long possible
);
1505 struct smatch_state
*merge_bstates(struct smatch_state
*one_state
, struct smatch_state
*two_state
);
1507 /* smatch_param_bits_set.c */
1508 void __set_param_modified_helper(struct expression
*expr
, struct smatch_state
*state
);
1509 void __set_param_modified_helper_sym(const char *name
, struct symbol
*sym
,
1510 struct smatch_state
*state
);
1512 /* smatch_param_bits_clear.c */
1513 void __set_param_modified_helper_clear(struct expression
*expr
, struct smatch_state
*state
);
1514 void __set_param_modified_helper_sym_clear(const char *name
, struct symbol
*sym
,
1515 struct smatch_state
*state
);
1517 /* smatch_bit_info.c */
1518 struct bit_info
*rl_to_binfo(struct range_list
*rl
);
1519 struct bit_info
*get_bit_info(struct expression
*expr
);
1520 struct bit_info
*get_bit_info_var_sym(const char *name
, struct symbol
*sym
);
1522 /* smatch_mem_tracker.c */
1523 extern int option_mem
;
1524 unsigned long get_mem_kb(void);
1525 unsigned long get_max_memory(void);
1527 /* smatch_goto_tracker.c */
1528 struct sm_state
*get_goto_sm_state(void);
1530 /* check_is_nospec.c */
1531 bool is_nospec(struct expression
*expr
);
1532 long get_stmt_cnt(void);
1534 /* check_kernel_units.c */
1535 struct smatch_state
*get_units(struct expression
*expr
);
1536 char *get_unit_str(struct expression
*expr
);
1537 bool is_array_size_units(struct expression
*expr
);
1538 /* smatch_nul_terminator.c */
1539 bool is_nul_terminated_var_sym(const char *name
, struct symbol
*sym
);
1540 bool is_nul_terminated(struct expression
*expr
);
1541 /* check_kernel.c */
1542 bool is_ignored_kernel_data(const char *name
);
1543 int get_gfp_param(struct expression
*expr
);
1544 struct expression
*get_netdev_priv(struct expression
*dev
);
1546 bool is_fresh_alloc_var_sym(const char *var
, struct symbol
*sym
);
1547 bool is_fresh_alloc(struct expression
*expr
);
1548 bool is_freed_var_sym(const char *name
, struct symbol
*sym
);
1549 void track_freed_param(struct expression
*expr
, struct smatch_state
*state
);
1550 void track_freed_param_var_sym(const char *name
, struct symbol
*sym
,
1551 struct smatch_state
*state
);
1552 bool is_part_of_condition(struct expression
*expr
);
1553 bool is_percent_p_print(struct expression
*expr
);
1555 void add_free_hook(name_sym_hook
*hook
);
1557 /* smatch_unconstant_macros.c */
1558 int is_unconstant_macro(struct expression
*expr
);
1560 /* smatch_assigned_state.c */
1561 void add_state_assigned_hook(int owner
, sm_hook
*call_back
);
1562 void add_ssa_state_assigned_hook(int owner
, sm_hook
*call_back
);
1564 /* check_returns_negative_error_code.c */
1565 bool holds_kernel_error_codes(struct expression
*expr
);
1566 bool possible_err_ptr(struct expression
*expr
);
1567 bool has_devm_cleanup(void);
1568 void add_set_current_state_hook(string_hook
*hook
);
1569 bool task_not_running(void);
1571 static inline bool type_is_ptr(struct symbol
*type
)
1574 (type
->type
== SYM_PTR
||
1575 type
->type
== SYM_ARRAY
||
1576 type
->type
== SYM_FN
);
1579 static inline bool type_is_fp(struct symbol
*type
)
1582 (type
== &float_ctype
||
1583 type
== &double_ctype
||
1584 type
== &ldouble_ctype
);
1587 static inline int type_bits(struct symbol
*type
)
1591 if (type_is_ptr(type
))
1592 return bits_in_pointer
;
1593 if (!type
->examined
)
1594 examine_symbol_type(type
);
1595 return type
->bit_size
;
1598 static inline int type_unsigned(struct symbol
*base_type
)
1602 if (is_ptr_type(base_type
))
1604 if (base_type
->ctype
.modifiers
& MOD_UNSIGNED
)
1609 static inline int type_positive_bits(struct symbol
*type
)
1613 if (is_ptr_type(type
))
1614 return bits_in_pointer
;
1615 if (type_unsigned(type
))
1616 return type_bits(type
);
1617 return type_bits(type
) - 1;
1620 static inline int sval_positive_bits(sval_t sval
)
1622 return type_positive_bits(sval
.type
);
1626 * Returns -1 if one is smaller, 0 if they are the same and 1 if two is larger.
1629 static inline int fp_cmp(sval_t one
, sval_t two
)
1631 struct symbol
*type
;
1633 if (sval_is_fp(one
) && sval_is_fp(two
))
1634 type
= type_bits(one
.type
) > type_bits(two
.type
) ? one
.type
: two
.type
;
1635 else if (sval_is_fp(one
))
1640 one
= sval_cast(type
, one
);
1641 two
= sval_cast(type
, two
);
1643 if (one
.type
== &float_ctype
) {
1644 if (one
.fvalue
< two
.fvalue
)
1646 if (one
.fvalue
== two
.fvalue
)
1650 if (one
.type
== &double_ctype
) {
1651 if (one
.dvalue
< two
.dvalue
)
1653 if (one
.dvalue
== two
.dvalue
)
1657 if (one
.type
== &ldouble_ctype
) {
1658 if (one
.ldvalue
< two
.ldvalue
)
1660 if (one
.ldvalue
== two
.ldvalue
)
1664 sm_perror("bad type in fp_cmp(): %s", type_to_str(type
));
1668 static inline int sval_cmp(sval_t one
, sval_t two
)
1670 struct symbol
*type
;
1672 if (sval_is_fp(one
) || sval_is_fp(two
))
1673 return fp_cmp(one
, two
);
1676 if (sval_positive_bits(two
) > sval_positive_bits(one
))
1678 if (type_bits(type
) < 31)
1681 one
= sval_cast(type
, one
);
1682 two
= sval_cast(type
, two
);
1684 if (type_unsigned(type
)) {
1685 if (one
.uvalue
< two
.uvalue
)
1687 if (one
.uvalue
== two
.uvalue
)
1691 /* fix me handle type promotion and unsigned values */
1692 if (one
.value
< two
.value
)
1694 if (one
.value
== two
.value
)
1699 #endif /* !SMATCH_H_ */