2 * Copyright (C) 2006 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
31 #include "expression.h"
33 #include "smatch_constants.h"
35 typedef long long mtag_t
;
41 #define STATE(_x) static struct smatch_state _x = { .name = #_x }
42 extern struct smatch_state undefined
;
43 extern struct smatch_state merged
;
44 extern struct smatch_state true_state
;
45 extern struct smatch_state false_state
;
46 DECLARE_ALLOCATOR(smatch_state
);
48 static inline void *INT_PTR(int i
)
50 return (void *)(long)i
;
53 static inline int PTR_INT(void *p
)
63 DECLARE_ALLOCATOR(tracker
);
64 DECLARE_PTR_LIST(tracker_list
, struct tracker
);
65 DECLARE_PTR_LIST(stree_stack
, struct stree
);
67 /* The first 3 struct members must match struct tracker */
72 unsigned short merged
:1;
73 unsigned short leaf
:1;
75 struct smatch_state
*state
;
77 struct sm_state
*left
;
78 struct sm_state
*right
;
79 struct state_list
*possible
;
86 DECLARE_ALLOCATOR(var_sym
);
87 DECLARE_PTR_LIST(var_sym_list
, struct var_sym
);
93 DECLARE_PTR_LIST(constraint_list
, struct constraint
);
99 extern struct alloc_info
*alloc_funcs
;
102 unsigned long long set
;
103 unsigned long long possible
;
114 DECLARATION_HOOK_AFTER
,
116 ASSIGNMENT_HOOK_AFTER
,
118 GLOBAL_ASSIGNMENT_HOOK
,
123 WHOLE_CONDITION_HOOK
,
124 FUNCTION_CALL_HOOK_BEFORE
,
126 CALL_HOOK_AFTER_INLINE
,
127 FUNCTION_CALL_HOOK_AFTER_DB
,
128 CALL_ASSIGNMENT_HOOK
,
129 MACRO_ASSIGNMENT_HOOK
,
154 typedef void (void_fn
)(void);
155 typedef void (expr_func
)(struct expression
*expr
);
156 typedef void (stmt_func
)(struct statement
*stmt
);
157 typedef void (sym_func
)(struct symbol
*sym
);
158 typedef void (name_sym_hook
)(struct expression
*expr
, const char *name
, struct symbol
*sym
);
159 DECLARE_PTR_LIST(void_fn_list
, void_fn
);
160 DECLARE_PTR_LIST(expr_fn_list
, expr_func
);
161 DECLARE_PTR_LIST(stmt_fn_list
, stmt_func
);
162 DECLARE_PTR_LIST(sym_fn_list
, sym_func
);
163 DECLARE_PTR_LIST(name_sym_fn_list
, name_sym_hook
);
165 void add_hook(void *func
, enum hook_type type
);
166 typedef struct smatch_state
*(merge_func_t
)(struct smatch_state
*s1
, struct smatch_state
*s2
);
167 typedef struct smatch_state
*(unmatched_func_t
)(struct sm_state
*state
);
168 void add_merge_hook(int client_id
, merge_func_t
*func
);
169 void add_unmatched_state_hook(int client_id
, unmatched_func_t
*func
);
170 void add_pre_merge_hook(int client_id
, void (*hook
)(struct sm_state
*cur
, struct sm_state
*other
));
171 typedef void (scope_hook
)(void *data
);
172 void add_scope_hook(scope_hook
*hook
, void *data
);
173 typedef void (param_key_hook
)(struct expression
*expr
, const char *name
, struct symbol
*sym
, void *data
);
174 typedef void (func_hook
)(const char *fn
, struct expression
*expr
, void *data
);
175 typedef void (implication_hook
)(const char *fn
, struct expression
*call_expr
,
176 struct expression
*assign_expr
, void *data
);
177 typedef void (return_implies_hook
)(struct expression
*call_expr
,
178 int param
, char *key
, char *value
);
179 typedef int (implied_return_hook
)(struct expression
*call_expr
, void *info
, struct range_list
**rl
);
180 void add_function_hook(const char *look_for
, func_hook
*call_back
, void *data
);
181 void add_function_hook_late(const char *look_for
, func_hook
*call_back
, void *info
);
183 void add_function_assign_hook(const char *look_for
, func_hook
*call_back
,
185 void register_func_hooks_from_file(const char *file
,
186 func_hook
*call_back
, void *info
);
187 void register_assign_hooks_from_file(const char *file
,
188 func_hook
*call_back
, void *info
);
189 void add_implied_return_hook(const char *look_for
,
190 implied_return_hook
*call_back
,
192 void add_macro_assign_hook(const char *look_for
, func_hook
*call_back
,
194 void add_macro_assign_hook_extra(const char *look_for
, func_hook
*call_back
,
196 void return_implies_state(const char *look_for
, long long start
, long long end
,
197 implication_hook
*call_back
, void *info
);
198 void return_implies_state_sval(const char *look_for
, sval_t start
, sval_t end
,
199 implication_hook
*call_back
, void *info
);
200 void return_implies_exact(const char *look_for
, sval_t start
, sval_t end
,
201 implication_hook
*call_back
, void *info
);
202 struct range_list
*get_range_implications(const char *fn
);
203 void select_return_states_hook(int type
, return_implies_hook
*callback
);
204 void select_return_states_before(void (*fn
)(void));
205 void select_return_states_after(void (*fn
)(void));
206 void add_function_param_key_hook(const char *look_for
, param_key_hook
*call_back
,
207 int param
, const char *key
, void *info
);
208 void add_function_param_key_hook_late(const char *look_for
, param_key_hook
*call_back
,
209 int param
, const char *key
, void *info
);
210 void return_implies_param_key(const char *look_for
, sval_t start
, sval_t end
,
211 param_key_hook
*call_back
,
212 int param
, const char *key
, void *info
);
213 void return_implies_param_key_exact(const char *look_for
, sval_t start
, sval_t end
,
214 param_key_hook
*call_back
,
215 int param
, const char *key
, void *info
);
216 void select_return_param_key(int type
, param_key_hook
*callback
);
217 int get_implied_return(struct expression
*expr
, struct range_list
**rl
);
218 void allocate_hook_memory(void);
219 void allocate_tracker_array(int num_checks
);
221 struct modification_data
{
222 struct smatch_state
*prev
;
223 struct expression
*cur
;
226 typedef void (modification_hook
)(struct sm_state
*sm
, struct expression
*mod_expr
);
227 bool is_sub_member(const char *name
, struct symbol
*sym
, struct sm_state
*sm
);
228 void add_modification_hook(int owner
, modification_hook
*call_back
);
229 void add_modification_hook_late(int owner
, modification_hook
*call_back
);
230 struct smatch_state
*get_modification_state(struct expression
*expr
);
232 int outside_of_function(void);
233 const char *get_filename(void);
234 extern int base_file_stream
;
235 const char *get_base_file(void);
236 char *get_function(void);
237 int get_lineno(void);
238 extern int final_pass
;
239 extern struct symbol
*cur_func_sym
;
240 extern int option_debug
;
241 extern int local_debug
;
243 extern bool implied_debug
;
244 bool debug_implied(void);
245 bool debug_on(const char *check_name
, const char *var
);
246 extern int option_info
;
247 extern int option_spammy
;
248 extern int option_pedantic
;
249 extern int option_print_names
;
250 extern char *trace_variable
;
251 extern struct stree
*global_states
;
252 void set_function_skipped(void);
253 int is_skipped_function(void);
254 int is_silenced_function(void);
255 extern bool implications_off
;
257 /* smatch_impossible.c */
258 int is_impossible_path(void);
259 void set_path_impossible(void);
261 extern FILE *sm_outfd
;
262 extern FILE *sql_outfd
;
263 extern FILE *caller_info_fd
;
264 extern int sm_nr_checks
;
265 extern int sm_nr_errors
;
268 * How to use these routines:
270 * sm_fatal(): an internal error of some kind that should immediately exit
271 * sm_ierror(): an internal error
272 * sm_perror(): an internal error from parsing input source
273 * sm_error(): an error from input source
274 * sm_warning(): a warning from input source
275 * sm_info(): info message (from option_info)
276 * sm_debug(): debug message
277 * sm_msg(): other message (please avoid using this)
280 #define sm_printf(msg...) do { \
281 if (final_pass || option_debug || local_debug || debug_db) \
282 fprintf(sm_outfd, msg); \
285 static inline void sm_prefix(void)
287 sm_printf("%s:%d %s() ", get_filename(), get_lineno(), get_function());
288 if (option_info
|| !option_print_names
)
290 sm_printf("[smatch.%s] ", __CHECKNAME__
);
293 static inline void print_implied_debug_msg();
295 extern bool __silence_warnings_for_stmt
;
297 #define sm_print_msg(type, msg...) \
299 print_implied_debug_msg(); \
300 if (!final_pass && !option_debug && !local_debug && !debug_db) \
302 if (__silence_warnings_for_stmt && !option_debug && !local_debug) \
304 if (!option_info && is_silenced_function()) \
308 sm_printf("warn: "); \
310 } else if (type == 2) { \
311 sm_printf("error: "); \
313 } else if (type == 3) { \
314 sm_printf("parse error: "); \
316 } else if (type == 4) { \
317 sm_printf("pedantic: "); \
323 #define sm_msg(msg...) do { sm_print_msg(0, msg); } while (0)
325 extern char *implied_debug_msg
;
326 static inline void print_implied_debug_msg(void)
328 static struct symbol
*last_printed
= NULL
;
330 if (!implied_debug_msg
)
332 if (last_printed
== cur_func_sym
)
334 last_printed
= cur_func_sym
;
335 sm_msg("%s", implied_debug_msg
);
338 #define sm_debug(msg...) do { if (option_debug) sm_printf(msg); } while (0)
339 #define db_debug(msg...) do { if (option_debug || debug_db) sm_printf(msg); } while (0)
341 #define sm_info(msg...) do { \
342 if (option_debug || (option_info && final_pass)) { \
344 sm_printf("info: "); \
350 #define sm_warning(msg...) do { sm_print_msg(1, msg); } while (0)
351 #define sm_warning_line(line, msg...) do { \
352 int __orig = __smatch_lineno; \
353 __smatch_lineno = line; \
354 sm_print_msg(1, msg); \
355 __smatch_lineno = __orig; \
357 #define sm_error(msg...) do { sm_print_msg(2, msg); } while (0)
358 #define sm_perror(msg...) do { sm_print_msg(3, msg); } while (0)
359 #define sm_pedantic(msg...) do { if (option_pedantic) sm_print_msg(4, msg); } while (0)
361 static inline void sm_fatal(const char *fmt
, ...)
366 vfprintf(sm_outfd
, fmt
, args
);
369 fprintf(sm_outfd
, "\n");
374 static inline void sm_ierror(const char *fmt
, ...)
380 fprintf(sm_outfd
, "internal error: ");
383 vfprintf(sm_outfd
, fmt
, args
);
386 fprintf(sm_outfd
, "\n");
388 #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
390 bool has_states(struct stree
*stree
, int owner
);
391 struct smatch_state
*__get_state(int owner
, const char *name
, struct symbol
*sym
);
392 struct smatch_state
*get_state(int owner
, const char *name
, struct symbol
*sym
);
393 struct smatch_state
*get_state_expr(int owner
, struct expression
*expr
);
394 bool has_possible_state(int owner
, const char *name
, struct symbol
*sym
, struct smatch_state
*state
);
395 bool expr_has_possible_state(int owner
, struct expression
*expr
, struct smatch_state
*state
);
396 struct state_list
*get_possible_states(int owner
, const char *name
,
398 struct state_list
*get_possible_states_expr(int owner
, struct expression
*expr
);
399 struct sm_state
*set_state(int owner
, const char *name
, struct symbol
*sym
,
400 struct smatch_state
*state
);
401 struct sm_state
*set_state_expr(int owner
, struct expression
*expr
,
402 struct smatch_state
*state
);
403 void __delete_state(int owner
, const char *name
, struct symbol
*sym
);
404 void __delete_all_states_sym(struct symbol
*sym
);
405 void set_true_false_states(int owner
, const char *name
, struct symbol
*sym
,
406 struct smatch_state
*true_state
,
407 struct smatch_state
*false_state
);
408 void set_true_false_states_expr(int owner
, struct expression
*expr
,
409 struct smatch_state
*true_state
,
410 struct smatch_state
*false_state
);
412 struct stree
*get_all_states_from_stree(int owner
, struct stree
*source
);
413 struct stree
*get_all_states_stree(int id
);
414 struct stree
*__get_cur_stree(void);
415 int is_reachable(void);
416 void add_get_state_hook(void (*fn
)(int owner
, const char *name
, struct symbol
*sym
));
419 char *ssa_name(const char *name
);
420 void set_ssa_state(int owner
, const char *name
, struct symbol
*sym
, struct smatch_state
*state
);
421 void update_ssa_state(int owner
, const char *name
, struct symbol
*sym
,
422 struct smatch_state
*state
);
423 void set_ssa_state_expr(int owner
, struct expression
*expr
, struct smatch_state
*state
);
424 struct sm_state
*get_ssa_sm_state(int owner
, const char *name
, struct symbol
*sym
);
425 struct sm_state
*get_ssa_sm_state_expr(int owner
, struct expression
*expr
);
426 struct smatch_state
*get_ssa_state(int owner
, const char *name
, struct symbol
*sym
);
427 struct smatch_state
*get_ssa_state_expr(int owner
, struct expression
*expr
);
428 void ssa_set_all_states(int owner
, const char *name
, struct symbol
*sym
, struct smatch_state
*state
);
429 void ssa_set_all_states_expr(int owner
, struct expression
*expr
, struct smatch_state
*state
);
431 /* smatch_helper.c */
432 DECLARE_PTR_LIST(int_stack
, int);
433 char *alloc_string(const char *str
);
434 char *alloc_string_newline(const char *str
);
435 void free_string(char *str
);
436 void remove_parens(char *str
);
437 struct smatch_state
*alloc_state_num(int num
);
438 struct smatch_state
*alloc_state_str(const char *name
);
439 struct smatch_state
*merge_str_state(struct smatch_state
*s1
, struct smatch_state
*s2
);
440 struct smatch_state
*alloc_state_expr(struct expression
*expr
);
441 struct expression
*get_argument_from_call_expr(struct expression_list
*args
,
443 struct expression
*get_array_expr(struct expression
*expr
);
445 char *expr_to_var(struct expression
*expr
);
446 struct symbol
*expr_to_sym(struct expression
*expr
);
447 char *expr_to_str(struct expression
*expr
);
448 char *expr_to_str_sym(struct expression
*expr
,
449 struct symbol
**sym_ptr
);
450 char *expr_to_var_sym(struct expression
*expr
,
451 struct symbol
**sym_ptr
);
452 char *expr_to_known_chunk_sym(struct expression
*expr
, struct symbol
**sym
);
453 char *expr_to_chunk_sym_vsl(struct expression
*expr
, struct symbol
**sym
, struct var_sym_list
**vsl
);
454 int get_complication_score(struct expression
*expr
);
456 int sym_name_is(const char *name
, struct expression
*expr
);
457 int get_const_value(struct expression
*expr
, sval_t
*sval
);
458 int get_value(struct expression
*expr
, sval_t
*val
);
459 int get_implied_value(struct expression
*expr
, sval_t
*val
);
460 int get_implied_value_fast(struct expression
*expr
, sval_t
*sval
);
461 int get_implied_min(struct expression
*expr
, sval_t
*sval
);
462 int get_implied_max(struct expression
*expr
, sval_t
*val
);
463 int get_hard_max(struct expression
*expr
, sval_t
*sval
);
464 int get_fuzzy_min(struct expression
*expr
, sval_t
*min
);
465 int get_fuzzy_max(struct expression
*expr
, sval_t
*max
);
466 int get_absolute_min(struct expression
*expr
, sval_t
*sval
);
467 int get_absolute_max(struct expression
*expr
, sval_t
*sval
);
468 int parse_call_math(struct expression
*expr
, char *math
, sval_t
*val
);
469 int parse_call_math_rl(struct expression
*call
, const char *math
, struct range_list
**rl
);
470 const char *get_allocation_math(struct expression
*expr
);
471 char *get_value_in_terms_of_parameter_math(struct expression
*expr
);
472 char *get_value_in_terms_of_parameter_math_var_sym(const char *var
, struct symbol
*sym
);
473 int expr_is_zero(struct expression
*expr
);
474 int known_condition_true(struct expression
*expr
);
475 int known_condition_false(struct expression
*expr
);
476 int implied_condition_true(struct expression
*expr
);
477 int implied_condition_false(struct expression
*expr
);
478 int can_integer_overflow(struct symbol
*type
, struct expression
*expr
);
479 void clear_math_cache(void);
480 void set_fast_math_only(void);
481 void clear_fast_math_only(void);
483 int is_array(struct expression
*expr
);
484 struct expression
*get_array_base(struct expression
*expr
);
485 struct expression
*get_array_offset(struct expression
*expr
);
486 const char *show_state(struct smatch_state
*state
);
487 struct statement
*get_expression_statement(struct expression
*expr
);
488 struct expression
*strip_parens(struct expression
*expr
);
489 struct expression
*strip_expr(struct expression
*expr
);
490 struct expression
*strip_expr_set_parent(struct expression
*expr
);
491 void scoped_state(int my_id
, const char *name
, struct symbol
*sym
);
492 int is_error_return(struct expression
*expr
);
493 int getting_address(struct expression
*expr
);
494 int get_struct_and_member(struct expression
*expr
, const char **type
, const char **member
);
495 char *get_member_name(struct expression
*expr
);
496 char *get_fnptr_name(struct expression
*expr
);
497 int cmp_pos(struct position pos1
, struct position pos2
);
498 int positions_eq(struct position pos1
, struct position pos2
);
499 struct statement
*get_current_statement(void);
500 struct statement
*get_prev_statement(void);
501 struct expression
*get_last_expr_from_expression_stmt(struct expression
*expr
);
503 #define RETURN_VAR -1
504 #define LOCAL_SCOPE -2
505 #define FILE_SCOPE -3
506 #define GLOBAL_SCOPE -4
507 #define UNKNOWN_SCOPE -5
509 char *swap_names(const char *orig
, const char *remove
, const char *add
);
510 char *get_param_var_sym_var_sym(const char *name
, struct symbol
*sym
, struct expression
*ret_expr
, struct symbol
**sym_p
);
511 char *get_param_name_sym(struct expression
*expr
, struct symbol
**sym_p
);
512 int get_param_key_from_var_sym(const char *name
, struct symbol
*sym
,
513 struct expression
*ret_expr
,
515 int get_param_key_from_sm(struct sm_state
*sm
, struct expression
*ret_expr
,
517 int map_to_param(const char *name
, struct symbol
*sym
);
518 int get_param_num_from_sym(struct symbol
*sym
);
519 int get_param_num(struct expression
*expr
);
520 struct symbol
*get_param_sym_from_num(int num
);
521 struct expression
*map_container_of_to_simpler_expr_key(struct expression
*expr
, const char *orig_key
, char **new_key
);
522 const char *get_container_of_str(struct expression
*expr
);
524 int ms_since(struct timeval
*start
);
525 int parent_is_gone_var_sym(const char *name
, struct symbol
*sym
);
526 int parent_is_gone(struct expression
*expr
);
527 bool is_noderef_ptr(struct expression
*expr
);
528 int invert_op(int op
);
529 int op_remove_assign(int op
);
530 int expr_equiv(struct expression
*one
, struct expression
*two
);
531 void push_int(struct int_stack
**stack
, int num
);
532 int pop_int(struct int_stack
**stack
);
535 struct symbol
*get_real_base_type(struct symbol
*sym
);
536 int type_bytes(struct symbol
*type
);
537 int array_bytes(struct symbol
*type
);
538 struct symbol
*get_pointer_type(struct expression
*expr
);
539 struct symbol
*get_type(struct expression
*expr
);
540 struct symbol
*get_comparison_type(struct expression
*expr
);
541 struct symbol
*get_final_type(struct expression
*expr
);
542 struct symbol
*get_promoted_type(struct symbol
*left
, struct symbol
*right
);
543 int type_signed(struct symbol
*base_type
);
544 int expr_unsigned(struct expression
*expr
);
545 int expr_signed(struct expression
*expr
);
546 int returns_unsigned(struct symbol
*base_type
);
547 int is_pointer(struct expression
*expr
);
548 int returns_pointer(struct symbol
*base_type
);
549 sval_t
sval_type_max(struct symbol
*base_type
);
550 sval_t
sval_type_min(struct symbol
*base_type
);
551 int nr_bits(struct expression
*expr
);
552 int is_void_pointer(struct expression
*expr
);
553 int is_char_pointer(struct expression
*expr
);
554 int is_string(struct expression
*expr
);
555 bool is_struct_ptr(struct symbol
*type
);
556 int is_static(struct expression
*expr
);
557 bool is_local_variable(struct expression
*expr
);
558 int types_equiv(struct symbol
*one
, struct symbol
*two
);
559 bool type_fits(struct symbol
*type
, struct symbol
*test
);
561 const char *global_static();
562 struct symbol
*cur_func_return_type(void);
563 struct symbol
*get_arg_type(struct expression
*fn
, int arg
);
564 struct symbol
*get_member_type_from_key(struct expression
*expr
, const char *key
);
565 struct symbol
*get_arg_type_from_key(struct expression
*fn
, int param
, struct expression
*arg
, const char *key
);
566 int is_struct(struct expression
*expr
);
567 char *type_to_str(struct symbol
*type
);
569 /* smatch_ignore.c */
570 void add_ignore(int owner
, const char *name
, struct symbol
*sym
);
571 int is_ignored(int owner
, const char *name
, struct symbol
*sym
);
572 void add_ignore_expr(int owner
, struct expression
*expr
);
573 int is_ignored_expr(int owner
, struct expression
*expr
);
576 struct smatch_state
*alloc_var_sym_state(const char *var
, struct symbol
*sym
);
577 struct var_sym
*alloc_var_sym(const char *var
, struct symbol
*sym
);
578 struct var_sym_list
*expr_to_vsl(struct expression
*expr
);
579 void add_var_sym(struct var_sym_list
**list
, const char *var
, struct symbol
*sym
);
580 void add_var_sym_expr(struct var_sym_list
**list
, struct expression
*expr
);
581 void del_var_sym(struct var_sym_list
**list
, const char *var
, struct symbol
*sym
);
582 int in_var_sym_list(struct var_sym_list
*list
, const char *var
, struct symbol
*sym
);
583 struct var_sym_list
*clone_var_sym_list(struct var_sym_list
*from_vsl
);
584 void merge_var_sym_list(struct var_sym_list
**dest
, struct var_sym_list
*src
);
585 struct var_sym_list
*combine_var_sym_lists(struct var_sym_list
*one
, struct var_sym_list
*two
);
586 int var_sym_lists_equiv(struct var_sym_list
*one
, struct var_sym_list
*two
);
587 void free_var_sym_list(struct var_sym_list
**list
);
588 void free_var_syms_and_list(struct var_sym_list
**list
);
591 struct tracker
*alloc_tracker(int owner
, const char *name
, struct symbol
*sym
);
592 void add_tracker(struct tracker_list
**list
, int owner
, const char *name
,
594 void add_tracker_expr(struct tracker_list
**list
, int owner
, struct expression
*expr
);
595 void del_tracker(struct tracker_list
**list
, int owner
, const char *name
,
597 int in_tracker_list(struct tracker_list
*list
, int owner
, const char *name
,
599 void free_tracker_list(struct tracker_list
**list
);
600 void free_trackers_and_list(struct tracker_list
**list
);
602 /* smatch_conditions */
603 int in_condition(void);
607 extern int __in_fake_assign
;
608 extern int __in_fake_parameter_assign
;
609 extern int __in_fake_struct_assign
;
610 extern int __in_fake_var_assign
;
611 extern int __fake_state_cnt
;
612 extern int in_fake_env
;
613 void smatch (struct string_list
*filelist
);
614 int inside_loop(void);
615 int definitely_inside_loop(void);
616 struct expression
*get_switch_expr(void);
617 int in_expression_statement(void);
618 void __process_post_op_stack(void);
619 void __split_expr(struct expression
*expr
);
620 void __split_label_stmt(struct statement
*stmt
);
621 void __split_stmt(struct statement
*stmt
);
622 extern int __in_function_def
;
623 extern int __in_unmatched_hook
;
624 extern int option_assume_loops
;
625 extern int option_two_passes
;
626 extern int option_no_db
;
627 extern int option_file_output
;
628 extern int option_time
;
629 extern int option_time_stmt
;
630 extern struct expression_list
*big_expression_stack
;
631 extern struct expression_list
*big_condition_stack
;
632 extern struct statement_list
*big_statement_stack
;
633 int is_assigned_call(struct expression
*expr
);
634 int is_fake_assigned_call(struct expression
*expr
);
635 void add_function_data(unsigned long *fn_data
);
636 int inlinable(struct expression
*expr
);
637 extern int __inline_call
;
638 extern struct expression
*__inline_fn
;
639 extern int __in_pre_condition
;
640 extern int __bail_on_rest_of_function
;
641 extern struct statement
*__prev_stmt
;
642 extern struct statement
*__cur_stmt
;
643 extern struct statement
*__next_stmt
;
644 void init_fake_env(void);
645 void end_fake_env(void);
646 int time_parsing_function(void);
647 bool taking_too_long(void);
648 struct statement
*get_last_stmt(void);
649 int is_last_stmt(struct statement
*cur_stmt
);
651 /* smatch_struct_assignment.c */
652 struct expression
*get_faked_expression(void);
653 void __fake_struct_member_assignments(struct expression
*expr
);
655 /* smatch_project.c */
656 int is_no_inline_function(const char *function
);
658 /* smatch_conditions */
659 void __split_whole_condition(struct expression
*expr
);
660 void __handle_logic(struct expression
*expr
);
661 int is_condition(struct expression
*expr
);
662 int __handle_condition_assigns(struct expression
*expr
);
663 int __handle_select_assigns(struct expression
*expr
);
664 int __handle_expr_statement_assigns(struct expression
*expr
);
666 /* smatch_implied.c */
667 struct range_list_stack
;
668 void param_limit_implications(struct expression
*expr
, int param
, char *key
, char *value
, struct stree
**implied
);
669 struct stree
*__implied_case_stree(struct expression
*switch_expr
,
670 struct range_list
*case_rl
,
671 struct range_list_stack
**remaining_cases
,
672 struct stree
**raw_stree
);
673 void overwrite_states_using_pool(struct sm_state
*gate_sm
, struct sm_state
*pool_sm
);
674 int assume(struct expression
*expr
);
675 void end_assume(void);
676 int impossible_assumption(struct expression
*left
, int op
, sval_t sval
);
679 bool has_dynamic_states(unsigned short owner
);
680 void set_dynamic_states(unsigned short owner
);
682 /* smatch_extras.c */
683 int in_warn_on_macro(void);
684 #define SMATCH_EXTRA 5 /* this is my_id from smatch extra set in smatch.c */
685 extern int RETURN_ID
;
692 char *get_other_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
);
693 char *map_call_to_other_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
);
694 char *map_long_to_short_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
, bool use_stack
);
696 #define STRLEN_MAX_RET 1010101
698 /* smatch_absolute.c */
699 int get_absolute_min_helper(struct expression
*expr
, sval_t
*sval
);
700 int get_absolute_max_helper(struct expression
*expr
, sval_t
*sval
);
702 /* smatch_type_value.c */
703 void clear_type_value_cache(void);
704 int get_db_type_rl(struct expression
*expr
, struct range_list
**rl
);
706 /* smatch_data_val.c */
707 int get_mtag_rl(struct expression
*expr
, struct range_list
**rl
);
708 /* smatch_array_values.c */
709 int get_array_rl(struct expression
*expr
, struct range_list
**rl
);
711 /* smatch_states.c */
712 struct stree
*__swap_cur_stree(struct stree
*stree
);
713 void __push_fake_cur_stree();
714 struct stree
*__pop_fake_cur_stree();
715 void __free_fake_cur_stree();
716 void __set_fake_cur_stree_fast(struct stree
*stree
);
717 void __pop_fake_cur_stree_fast(void);
718 void __merge_stree_into_cur(struct stree
*stree
);
720 int unreachable(void);
721 void __set_cur_stree_readonly(void);
722 void __set_cur_stree_writable(void);
723 void __set_sm(struct sm_state
*sm
);
724 void __set_sm_cur_stree(struct sm_state
*sm
);
725 void __set_true_false_sm(struct sm_state
*true_state
,
726 struct sm_state
*false_state
);
727 void nullify_path(void);
728 void __match_nullify_path_hook(const char *fn
, struct expression
*expr
,
730 void __unnullify_path(void);
731 int __path_is_null(void);
732 void save_all_states(void);
733 void restore_all_states(void);
734 void free_goto_stack(void);
735 void clear_all_states(void);
737 struct sm_state
*get_sm_state(int owner
, const char *name
,
739 struct sm_state
*get_sm_state_expr(int owner
, struct expression
*expr
);
740 void __push_true_states(void);
741 void __use_false_states(void);
742 void __discard_false_states(void);
743 void __merge_false_states(void);
744 void __merge_true_states(void);
746 void __negate_cond_stacks(void);
747 void __use_pre_cond_states(void);
748 void __use_cond_true_states(void);
749 void __use_cond_false_states(void);
750 void __push_cond_stacks(void);
751 void __fold_in_set_states(void);
752 void __free_set_states(void);
753 struct stree
*__copy_cond_true_states(void);
754 struct stree
*__copy_cond_false_states(void);
755 struct stree
*__pop_cond_true_stack(void);
756 struct stree
*__pop_cond_false_stack(void);
757 void __and_cond_states(void);
758 void __or_cond_states(void);
759 void __save_pre_cond_states(void);
760 void __discard_pre_cond_states(void);
761 struct stree
*__get_true_states(void);
762 struct stree
*__get_false_states(void);
763 void __use_cond_states(void);
764 extern struct state_list
*__last_base_slist
;
766 void __push_continues(void);
767 void __discard_continues(void);
768 void __process_continues(void);
769 void __merge_continues(void);
771 void __push_breaks(void);
772 void __process_breaks(void);
773 int __has_breaks(void);
774 void __merge_breaks(void);
775 void __use_breaks(void);
777 void __save_switch_states(struct expression
*switch_expr
);
778 void __discard_switches(void);
779 int have_remaining_cases(void);
780 void __merge_switches(struct expression
*switch_expr
, struct range_list
*case_rl
);
781 void __push_default(void);
782 void __set_default(void);
783 int __pop_default(void);
785 void __push_conditions(void);
786 void __discard_conditions(void);
788 void __save_gotos(const char *name
, struct symbol
*sym
);
789 void __merge_gotos(const char *name
, struct symbol
*sym
);
791 void __discard_fake_states(struct expression
*call
);
793 void __print_cur_stree(void);
794 bool __print_states(const char *owner
);
795 typedef void (check_tracker_hook
)(int owner
, const char *name
, struct symbol
*sym
, struct smatch_state
*state
);
796 void add_check_tracker(const char *check_name
, check_tracker_hook
*fn
);
799 void __pass_to_client(void *data
, enum hook_type type
);
800 void __pass_case_to_client(struct expression
*switch_expr
,
801 struct range_list
*rl
);
802 int __has_merge_function(int client_id
);
803 struct smatch_state
*__client_merge_function(int owner
,
804 struct smatch_state
*s1
,
805 struct smatch_state
*s2
);
806 struct smatch_state
*__client_unmatched_state_function(struct sm_state
*sm
);
807 void call_pre_merge_hook(struct sm_state
*cur
, struct sm_state
*other
);
808 void __push_scope_hooks(void);
809 void __call_scope_hooks(void);
810 void add_array_initialized_hook(void (*hook
)(struct expression
*array
, int nr
));
811 void __call_array_initialized_hooks(struct expression
*array
, int nr
);
813 /* smatch_function_hooks.c */
814 void create_function_hook_hash(void);
815 void __match_initializer_call(struct symbol
*sym
);
821 * Changing these numbers is a pain. Don't do it. If you ever use a
822 * number it can't be re-used right away so there may be gaps.
823 * We select these in order by type so if the order matters, then give
824 * it a number below 100-999,9000-9999 ranges. */
836 ABSOLUTE_LIMITS
= 1010,
850 UNTRACKED_PARAM
= 1023,
856 COMPARE_LIMIT
= 1028,
857 PARAM_COMPARE
= 1029,
860 CONSTRAINT_REQUIRED
= 1033,
871 POWER_OF_TWO_SET
= 1049,
879 /* put random temporary stuff in the 7000-7999 range for testing */
881 USER_DATA_SET
= 9017,
885 NO_OVERFLOW_SIMPLE
= 8019,
891 KNOWN_UNLOCKED
= 9025,
897 NO_SIDE_EFFECT
= 8025,
900 ARRAYSIZE_ARG
= 8033,
915 extern struct sqlite3
*smatch_db
;
916 extern struct sqlite3
*mem_db
;
917 extern struct sqlite3
*cache_db
;
919 bool db_incomplete(void);
920 void db_ignore_states(int id
);
921 typedef bool (delete_hook
)(struct expression
*expr
);
922 void add_delete_return_hook(delete_hook
*hook
);
923 void select_caller_info_hook(void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
), int type
);
924 void select_caller_name_sym(void (*fn
)(const char *name
, struct symbol
*sym
, char *value
), int type
);
925 void add_member_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
));
926 void add_caller_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
));
927 void add_return_info_callback(int owner
,
928 void (*callback
)(int return_id
, char *return_ranges
,
929 struct expression
*returned_expr
,
931 const char *printed_name
,
932 struct sm_state
*sm
));
933 void add_split_return_callback(void (*fn
)(int return_id
, char *return_ranges
, struct expression
*returned_expr
));
934 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
));
935 void select_call_implies_hook(int type
, void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
));
936 void select_return_implies_hook(int type
, void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
));
937 struct range_list
*db_return_vals(struct expression
*expr
);
938 struct range_list
*db_return_vals_from_str(const char *fn_name
);
939 struct range_list
*db_return_vals_no_args(struct expression
*expr
);
940 bool get_implied_rl_from_call_str(struct expression
*expr
, const char *data
, struct range_list
**rl
);
941 char *get_chunk_from_key(struct expression
*arg
, char *key
, struct symbol
**sym
, struct var_sym_list
**vsl
);
942 char *get_variable_from_key(struct expression
*arg
, const char *key
, struct symbol
**sym
);
943 char *get_name_sym_from_param_key(struct expression
*expr
, int param
, const char *key
, struct symbol
**sym
);
944 char *get_param_name_var_sym(const char *name
, struct symbol
*sym
);
945 char *get_param_name(struct sm_state
*sm
);
946 const char *get_mtag_name_var_sym(const char *state_name
, struct symbol
*sym
);
947 const char *get_mtag_name_expr(struct expression
*expr
);
948 char *get_data_info_name(struct expression
*expr
);
949 char *sm_to_arg_name(struct expression
*expr
, struct sm_state
*sm
);
950 int is_recursive_member(const char *param_name
);
952 char *escape_newlines(const char *str
);
953 void sql_exec(struct sqlite3
*db
, int (*callback
)(void*, int, char**, char**), void *data
, const char *sql
);
955 #define sql_helper(db, call_back, data, sql...) \
957 char sql_txt[1024]; \
959 sqlite3_snprintf(sizeof(sql_txt), sql_txt, sql); \
960 db_debug("debug: %s\n", sql_txt); \
961 sql_exec(db, call_back, data, sql_txt); \
965 #define run_sql(call_back, data, sql...) \
969 sql_helper(smatch_db, call_back, data, sql); \
972 #define mem_sql(call_back, data, sql...) \
973 sql_helper(mem_db, call_back, data, sql)
975 #define cache_sql(call_back, data, sql...) \
976 sql_helper(cache_db, call_back, data, sql)
978 #define sql_insert_helper(table, db, ignore, late, values...) \
980 struct sqlite3 *_db = db; \
982 if (__inline_fn && !_db) \
986 char *err, *p = buf; \
989 p += snprintf(p, buf + sizeof(buf) - p, \
990 "insert %sinto %s values (", \
991 ignore ? "or ignore " : "", #table); \
992 p += snprintf(p, buf + sizeof(buf) - p, values); \
993 p += snprintf(p, buf + sizeof(buf) - p, ");"); \
994 db_debug("mem-db: %s\n", buf); \
995 rc = sqlite3_exec(_db, buf, NULL, NULL, &err); \
996 if (rc != SQLITE_OK) { \
997 sm_ierror("SQL error #2: %s", err); \
998 sm_ierror("SQL: '%s'", buf); \
1003 if (option_info) { \
1004 FILE *tmp_fd = sm_outfd; \
1005 sm_outfd = sql_outfd; \
1007 sm_printf("SQL%s: insert %sinto " #table " values(", \
1008 late ? "_late" : "", ignore ? "or ignore " : ""); \
1009 sm_printf(values); \
1010 sm_printf(");\n"); \
1011 sm_outfd = tmp_fd; \
1015 #define sql_insert(table, values...) sql_insert_helper(table, 0, 0, 0, values);
1016 #define sql_insert_or_ignore(table, values...) sql_insert_helper(table, 0, 1, 0, values);
1017 #define sql_insert_late(table, values...) sql_insert_helper(table, 0, 0, 1, values);
1018 #define sql_insert_cache(table, values...) sql_insert_helper(table, cache_db, 1, 0, values);
1020 char *get_static_filter(struct symbol
*sym
);
1022 void sql_insert_return_states(int return_id
, const char *return_ranges
,
1023 int type
, int param
, const char *key
, const char *value
);
1024 void sql_insert_caller_info(struct expression
*call
, int type
, int param
,
1025 const char *key
, const char *value
);
1026 void sql_insert_function_ptr(const char *fn
, const char *struct_name
);
1027 void sql_insert_return_values(const char *return_values
);
1028 void sql_insert_return_implies(int type
, int param
, const char *key
, const char *value
);
1029 void sql_insert_function_type_size(const char *member
, const char *ranges
);
1030 void sql_insert_function_type_info(int type
, const char *struct_type
, const char *member
, const char *value
);
1031 void sql_insert_type_info(int type
, const char *member
, const char *value
);
1032 void sql_insert_local_values(const char *name
, const char *value
);
1033 void sql_insert_function_type_value(const char *type
, const char *value
);
1034 void sql_insert_function_type(int param
, const char *value
);
1035 void sql_insert_parameter_name(int param
, const char *value
);
1036 void sql_insert_data_info(struct expression
*data
, int type
, const char *value
);
1037 void sql_insert_data_info_var_sym(const char *var
, struct symbol
*sym
, int type
, const char *value
);
1038 void sql_save_constraint(const char *con
);
1039 void sql_save_constraint_required(const char *data
, int op
, const char *limit
);
1040 void sql_copy_constraint_required(const char *new_limit
, const char *old_limit
);
1041 void sql_insert_fn_ptr_data_link(const char *ptr
, const char *data
);
1042 void sql_insert_fn_data_link(struct expression
*fn
, int type
, int param
, const char *key
, const char *value
);
1043 void sql_insert_mtag_about(mtag_t tag
, const char *left_name
, const char *right_name
);
1044 void sql_insert_mtag_info(mtag_t tag
, int type
, const char *value
);
1045 void sql_insert_mtag_map(mtag_t container
, int container_offset
, mtag_t tag
, int tag_offset
);
1046 void sql_insert_mtag_alias(mtag_t orig
, mtag_t alias
);
1047 int mtag_map_select_container(mtag_t tag
, int container_offset
, mtag_t
*container
);
1048 int mtag_map_select_tag(mtag_t container
, int offset
, mtag_t
*tag
);
1049 struct smatch_state
*get_mtag_return(struct expression
*expr
, struct smatch_state
*state
);
1050 struct range_list
*swap_mtag_seed(struct expression
*expr
, struct range_list
*rl
);
1052 bool is_fn_ptr(struct expression
*fn
);
1053 void sql_select_return_states(const char *cols
, struct expression
*call
,
1054 int (*callback
)(void*, int, char**, char**), void *info
);
1055 void sql_select_call_implies(const char *cols
, struct expression
*call
,
1056 int (*callback
)(void*, int, char**, char**));
1058 void open_smatch_db(char *db_file
);
1060 /* smatch_files.c */
1061 int open_data_file(const char *filename
);
1062 int open_schema_file(const char *schema
);
1063 struct token
*get_tokens_file(const char *filename
);
1064 struct string_list
*load_strings_from_file(const char *project
, const char *filename
);
1067 extern char *option_debug_check
;
1068 extern char *option_debug_var
;
1069 extern char *option_project_str
;
1070 extern char *bin_dir
;
1071 extern char *data_dir
;
1072 extern int option_no_data
;
1073 extern int option_full_path
;
1074 extern int option_call_tree
;
1075 extern int num_checks
;
1081 PROJ_ILLUMOS_KERNEL
,
1085 extern enum project_type option_project
;
1086 const char *check_name(unsigned short id
);
1087 int id_from_name(const char *name
);
1090 /* smatch_buf_size.c */
1091 int get_array_size(struct expression
*expr
);
1092 int get_array_size_bytes(struct expression
*expr
);
1093 int get_array_size_bytes_min(struct expression
*expr
);
1094 int get_array_size_bytes_max(struct expression
*expr
);
1095 struct range_list
*get_array_size_bytes_rl(struct expression
*expr
);
1096 int get_real_array_size(struct expression
*expr
);
1097 int last_member_is_resizable(struct symbol
*type
);
1098 /* smatch_strlen.c */
1099 int get_implied_strlen(struct expression
*expr
, struct range_list
**rl
);
1100 int get_size_from_strlen(struct expression
*expr
);
1102 /* smatch_capped.c */
1103 int is_capped(struct expression
*expr
);
1104 int is_capped_var_sym(const char *name
, struct symbol
*sym
);
1106 /* smatch_kernel_user_data.c */
1107 int is_user_macro(struct expression
*expr
);
1108 int is_capped_user_data(struct expression
*expr
);
1109 int implied_user_data(struct expression
*expr
, struct range_list
**rl
);
1110 struct stree
*get_user_stree(void);
1111 int get_user_rl(struct expression
*expr
, struct range_list
**rl
);
1112 int is_user_rl(struct expression
*expr
);
1113 int get_user_rl_var_sym(const char *name
, struct symbol
*sym
, struct range_list
**rl
);
1114 bool user_rl_capped(struct expression
*expr
);
1115 struct range_list
*var_user_rl(struct expression
*expr
);
1116 bool we_pass_user_data(struct expression
*call
);
1117 /* smatch_points_to_user_data.c */
1118 bool is_user_data_fn(struct symbol
*fn
);
1119 bool is_skb_data(struct expression
*expr
);
1120 bool points_to_user_data(struct expression
*expr
);
1121 void set_points_to_user_data(struct expression
*expr
);
1123 /* check_locking.c */
1124 void print_held_locks();
1126 void __preempt_add(void);
1127 void __preempt_sub(void);
1128 int get_preempt_cnt(void);
1129 void clear_preempt_cnt(void);
1130 bool function_decrements_preempt(void);
1131 void add_sleep_callback(void_fn
*fn
);
1132 unsigned long GFP_DIRECT_RECLAIM(void);
1133 unsigned long GFP_ATOMIC(void);
1135 /* check_assigned_expr.c */
1136 extern int check_assigned_expr_id
;
1137 struct expression
*get_assigned_expr(struct expression
*expr
);
1138 struct sm_state
*get_assigned_sm(struct expression
*expr
);
1139 struct expression
*get_assigned_expr_recurse(struct expression
*expr
);
1140 struct expression
*get_assigned_expr_name_sym(const char *name
, struct symbol
*sym
);
1141 struct expression
*get_assigned_expr_name_sym_recurse(const char *name
, struct symbol
*sym
);
1143 /* smatch_return_to_param.c */
1144 void __add_return_to_param_mapping(struct expression
*assign
, const char *return_string
);
1145 char *map_call_to_param_name_sym(struct expression
*expr
, struct symbol
**sym
);
1147 /* smatch_comparison.c */
1148 extern int comparison_id
;
1149 #define UNKNOWN_COMPARISON 0
1150 #define IMPOSSIBLE_COMPARISON -1
1151 struct compare_data
{
1152 /* The ->left and ->right expression pointers might be NULL (I'm lazy) */
1153 struct expression
*left
;
1154 const char *left_var
;
1155 struct var_sym_list
*left_vsl
;
1157 struct expression
*right
;
1158 const char *right_var
;
1159 struct var_sym_list
*right_vsl
;
1161 DECLARE_ALLOCATOR(compare_data
);
1162 struct smatch_state
*alloc_compare_state(
1163 struct expression
*left
,
1164 const char *left_var
, struct var_sym_list
*left_vsl
,
1166 struct expression
*right
,
1167 const char *right_var
, struct var_sym_list
*right_vsl
);
1168 int comparison_intersection(int orig
, int op
);
1169 int merge_comparisons(int one
, int two
);
1170 int combine_comparisons(int left_compare
, int right_compare
);
1171 int state_to_comparison(struct smatch_state
*state
);
1172 struct smatch_state
*merge_compare_states(struct smatch_state
*s1
, struct smatch_state
*s2
);
1173 int get_comparison(struct expression
*left
, struct expression
*right
);
1174 int get_comparison_no_extra(struct expression
*a
, struct expression
*b
);
1175 int get_comparison_strings(const char *one
, const char *two
);
1176 int possible_comparison(struct expression
*a
, int comparison
, struct expression
*b
);
1177 struct state_list
*get_all_comparisons(struct expression
*expr
);
1178 struct state_list
*get_all_possible_equal_comparisons(struct expression
*expr
);
1179 void __add_return_comparison(struct expression
*call
, const char *range
);
1180 void __add_comparison_info(struct expression
*expr
, struct expression
*call
, const char *range
);
1181 char *get_printed_param_name(struct expression
*call
, const char *param_name
, struct symbol
*param_sym
);
1182 char *name_sym_to_param_comparison(const char *name
, struct symbol
*sym
);
1183 char *expr_equal_to_param(struct expression
*expr
, int ignore
);
1184 char *expr_lte_to_param(struct expression
*expr
, int ignore
);
1185 char *expr_param_comparison(struct expression
*expr
, int ignore
);
1186 int flip_comparison(int op
);
1187 int negate_comparison(int op
);
1188 int remove_unsigned_from_comparison(int op
);
1189 int param_compare_limit_is_impossible(struct expression
*expr
, int left_param
, char *left_key
, char *value
);
1190 void filter_by_comparison(struct range_list
**rl
, int comparison
, struct range_list
*right
);
1191 void __compare_param_limit_hook(struct expression
*left_expr
, struct expression
*right_expr
,
1192 const char *state_name
,
1193 struct smatch_state
*true_state
, struct smatch_state
*false_state
);
1194 int impossibly_high_comparison(struct expression
*expr
);
1197 sval_t
*sval_alloc(sval_t sval
);
1198 sval_t
*sval_alloc_permanent(sval_t sval
);
1199 sval_t
sval_blank(struct expression
*expr
);
1200 sval_t
sval_type_val(struct symbol
*type
, long long val
);
1201 sval_t
sval_type_fval(struct symbol
*type
, long double fval
);
1202 sval_t
sval_from_val(struct expression
*expr
, long long val
);
1203 sval_t
sval_from_fval(struct expression
*expr
, long double fval
);
1204 int sval_is_ptr(sval_t sval
);
1205 bool sval_is_fp(sval_t sval
);
1206 int sval_unsigned(sval_t sval
);
1207 int sval_signed(sval_t sval
);
1208 int sval_bits(sval_t sval
);
1209 int sval_bits_used(sval_t sval
);
1210 int sval_is_negative(sval_t sval
);
1211 int sval_is_positive(sval_t sval
);
1212 int sval_is_min(sval_t sval
);
1213 int sval_is_max(sval_t sval
);
1214 int sval_is_a_min(sval_t sval
);
1215 int sval_is_a_max(sval_t sval
);
1216 int sval_is_negative_min(sval_t sval
);
1217 int sval_cmp_t(struct symbol
*type
, sval_t one
, sval_t two
);
1218 int sval_cmp_val(sval_t one
, long long val
);
1219 sval_t
sval_min(sval_t one
, sval_t two
);
1220 sval_t
sval_min_nonneg(sval_t one
, sval_t two
);
1221 sval_t
sval_max(sval_t one
, sval_t two
);
1222 int sval_too_low(struct symbol
*type
, sval_t sval
);
1223 int sval_too_high(struct symbol
*type
, sval_t sval
);
1224 int sval_fits(struct symbol
*type
, sval_t sval
);
1225 sval_t
sval_cast(struct symbol
*type
, sval_t sval
);
1226 sval_t
sval_preop(sval_t sval
, int op
);
1227 sval_t
sval_binop(sval_t left
, int op
, sval_t right
);
1228 int sval_binop_overflows(sval_t left
, int op
, sval_t right
);
1229 int sval_binop_overflows_no_sign(sval_t left
, int op
, sval_t right
);
1230 int find_first_zero_bit(unsigned long long uvalue
);
1231 int sm_fls64(unsigned long long uvalue
);
1232 unsigned long long fls_mask(unsigned long long uvalue
);
1233 unsigned long long sval_fls_mask(sval_t sval
);
1234 const char *sval_to_str(sval_t sval
);
1235 const char *sval_to_str_or_err_ptr(sval_t sval
);
1236 const char *sval_to_numstr(sval_t sval
);
1237 sval_t
ll_to_sval(long long val
);
1239 /* smatch_string_list.c */
1240 int list_has_string(struct string_list
*str_list
, const char *str
);
1241 int insert_string(struct string_list
**str_list
, const char *str
);
1242 struct string_list
*clone_str_list(struct string_list
*orig
);
1243 struct string_list
*combine_string_lists(struct string_list
*one
, struct string_list
*two
);
1245 /* smatch_start_states.c */
1246 struct stree
*get_start_states(void);
1248 /* smatch_recurse.c */
1249 int has_symbol(struct expression
*expr
, struct symbol
*sym
);
1250 int has_variable(struct expression
*expr
, struct expression
*var
);
1251 int has_inc_dec(struct expression
*expr
);
1253 /* smatch_stored_conditions.c */
1254 struct smatch_state
*get_stored_condition(struct expression
*expr
);
1255 struct expression_list
*get_conditions(struct expression
*expr
);
1256 struct sm_state
*stored_condition_implication_hook(struct expression
*expr
,
1257 struct state_list
**true_stack
,
1258 struct state_list
**false_stack
);
1259 /* smatch_parsed_conditions.c */
1260 struct sm_state
*parsed_condition_implication_hook(struct expression
*expr
,
1261 struct state_list
**true_stack
,
1262 struct state_list
**false_stack
);
1263 /* smatch_comparison.c */
1264 struct sm_state
*comparison_implication_hook(struct expression
*expr
,
1265 struct state_list
**true_stack
,
1266 struct state_list
**false_stack
);
1268 /* check_string_len.c */
1269 int get_formatted_string_size(struct expression
*call
, int arg
);
1270 int get_formatted_string_min_size(struct expression
*call
, int arg
);
1272 /* smatch_param_set.c */
1273 int param_was_set(struct expression
*expr
);
1274 int param_was_set_var_sym(const char *name
, struct symbol
*sym
);
1275 void print_limited_param_set(int return_id
, char *return_ranges
, struct expression
*expr
);
1277 bool parent_was_PARAM_CLEAR(const char *name
, struct symbol
*sym
);
1278 bool parent_was_PARAM_CLEAR_ZERO(const char *name
, struct symbol
*sym
);
1280 /* smatch_param_filter.c */
1281 int param_has_filter_data(struct sm_state
*sm
);
1283 /* smatch_links.c */
1284 void set_up_link_functions(int id
, int linkid
);
1285 struct smatch_state
*merge_link_states(struct smatch_state
*s1
, struct smatch_state
*s2
);
1286 void store_link(int link_id
, const char *name
, struct symbol
*sym
, const char *link_name
, struct symbol
*link_sym
);
1288 /* check_buf_comparison */
1289 const char *limit_type_str(unsigned int limit_type
);
1290 struct expression
*get_size_variable(struct expression
*buf
, int *limit_type
);
1291 struct expression
*get_array_variable(struct expression
*size
);
1292 int buf_comparison_index_ok(struct expression
*expr
);
1294 /* smatch_untracked_param.c */
1295 void mark_untracked(struct expression
*expr
, int param
, const char *key
, const char *value
);
1296 void add_untracked_param_hook(void (func
)(struct expression
*call
, int param
));
1297 void add_lost_param_hook(void (func
)(struct expression
*call
, int param
));
1298 void mark_all_params_untracked(int return_id
, char *return_ranges
, struct expression
*expr
);
1300 /* smatch_strings.c */
1301 struct state_list
*get_strings(struct expression
*expr
);
1302 struct expression
*fake_string_from_mtag(mtag_t tag
);
1304 /* smatch_estate.c */
1305 int estate_get_single_value(struct smatch_state
*state
, sval_t
*sval
);
1307 /* smatch_address.c */
1308 int get_address_rl(struct expression
*expr
, struct range_list
**rl
);
1309 int get_member_offset(struct symbol
*type
, const char *member_name
);
1310 int get_member_offset_from_deref(struct expression
*expr
);
1312 /* for now this is in smatch_used_parameter.c */
1313 void __get_state_hook(int owner
, const char *name
, struct symbol
*sym
);
1314 extern int __ignore_param_used
;
1316 /* smatch_buf_comparison.c */
1317 int db_var_is_array_limit(struct expression
*array
, const char *name
, struct var_sym_list
*vsl
);
1319 struct range_list
*get_fs(void);
1321 struct stree
*get_all_return_states(void);
1322 struct stree_stack
*get_all_return_strees(void);
1323 int on_atomic_dec_path(void);
1324 int was_inced(const char *name
, struct symbol
*sym
);
1325 void set_refcount_inc(char *name
, struct symbol
*sym
);
1326 void set_refcount_dec(char *name
, struct symbol
*sym
);
1328 /* smatch_constraints.c */
1329 char *get_constraint_str(struct expression
*expr
);
1330 struct constraint_list
*get_constraints(struct expression
*expr
);
1331 char *unmet_constraint(struct expression
*data
, struct expression
*offset
);
1332 char *get_required_constraint(const char *data_str
);
1334 /* smatch_container_of.c */
1335 int get_param_from_container_of(struct expression
*expr
);
1336 int get_offset_from_container_of(struct expression
*expr
);
1337 char *get_container_name(struct expression
*container
, struct expression
*expr
);
1340 mtag_t
str_to_mtag(const char *str
);
1341 int get_string_mtag(struct expression
*expr
, mtag_t
*tag
);
1342 int get_toplevel_mtag(struct symbol
*sym
, mtag_t
*tag
);
1343 int create_mtag_alias(mtag_t tag
, struct expression
*expr
, mtag_t
*new);
1344 int expr_to_mtag_offset(struct expression
*expr
, mtag_t
*tag
, int *offset
);
1345 void update_mtag_data(struct expression
*expr
, struct smatch_state
*state
);
1346 int get_mtag_sval(struct expression
*expr
, sval_t
*sval
);
1348 /* Trinity fuzzer stuff */
1349 const char *get_syscall_arg_type(struct symbol
*sym
);
1352 int binfo_equiv(struct bit_info
*one
, struct bit_info
*two
);
1353 struct bit_info
*alloc_bit_info(unsigned long long set
, unsigned long long possible
);
1354 struct smatch_state
*alloc_bstate(unsigned long long set
, unsigned long long possible
);
1355 struct smatch_state
*merge_bstates(struct smatch_state
*one_state
, struct smatch_state
*two_state
);
1357 /* smatch_param_bits_set.c */
1358 void __set_param_modified_helper(struct expression
*expr
, struct smatch_state
*state
);
1359 void __set_param_modified_helper_sym(const char *name
, struct symbol
*sym
,
1360 struct smatch_state
*state
);
1362 /* smatch_param_bits_clear.c */
1363 void __set_param_modified_helper_clear(struct expression
*expr
, struct smatch_state
*state
);
1364 void __set_param_modified_helper_sym_clear(const char *name
, struct symbol
*sym
,
1365 struct smatch_state
*state
);
1367 /* smatch_bit_info.c */
1368 struct bit_info
*rl_to_binfo(struct range_list
*rl
);
1369 struct bit_info
*get_bit_info(struct expression
*expr
);
1370 struct bit_info
*get_bit_info_var_sym(const char *name
, struct symbol
*sym
);
1372 /* smatch_mem_tracker.c */
1373 extern int option_mem
;
1374 unsigned long get_mem_kb(void);
1375 unsigned long get_max_memory(void);
1377 /* check_is_nospec.c */
1378 bool is_nospec(struct expression
*expr
);
1379 long get_stmt_cnt(void);
1381 /* smatch_nul_terminator.c */
1382 bool is_nul_terminated_var_sym(const char *name
, struct symbol
*sym
);
1383 bool is_nul_terminated(struct expression
*expr
);
1384 /* check_kernel.c */
1385 bool is_ignored_kernel_data(const char *name
);
1386 int get_gfp_param(struct expression
*expr
);
1388 bool is_fresh_alloc_var_sym(const char *var
, struct symbol
*sym
);
1389 bool is_fresh_alloc(struct expression
*expr
);
1390 bool is_freed_var_sym(const char *name
, struct symbol
*sym
);
1391 void track_freed_param(struct expression
*expr
, struct smatch_state
*state
);
1392 void track_freed_param_var_sym(const char *name
, struct symbol
*sym
,
1393 struct smatch_state
*state
);
1395 static inline bool type_is_ptr(struct symbol
*type
)
1398 (type
->type
== SYM_PTR
||
1399 type
->type
== SYM_ARRAY
||
1400 type
->type
== SYM_FN
);
1403 static inline bool type_is_fp(struct symbol
*type
)
1406 (type
== &float_ctype
||
1407 type
== &double_ctype
||
1408 type
== &ldouble_ctype
);
1411 static inline int type_bits(struct symbol
*type
)
1415 if (type_is_ptr(type
))
1416 return bits_in_pointer
;
1417 if (!type
->examined
)
1418 examine_symbol_type(type
);
1419 return type
->bit_size
;
1422 static inline int type_unsigned(struct symbol
*base_type
)
1426 if (is_ptr_type(base_type
))
1428 if (base_type
->ctype
.modifiers
& MOD_UNSIGNED
)
1433 static inline int type_positive_bits(struct symbol
*type
)
1437 if (is_ptr_type(type
))
1438 return bits_in_pointer
;
1439 if (type_unsigned(type
))
1440 return type_bits(type
);
1441 return type_bits(type
) - 1;
1444 static inline int sval_positive_bits(sval_t sval
)
1446 return type_positive_bits(sval
.type
);
1450 * Returns -1 if one is smaller, 0 if they are the same and 1 if two is larger.
1453 static inline int fp_cmp(sval_t one
, sval_t two
)
1455 struct symbol
*type
;
1457 if (sval_is_fp(one
) && sval_is_fp(two
))
1458 type
= type_bits(one
.type
) > type_bits(two
.type
) ? one
.type
: two
.type
;
1459 else if (sval_is_fp(one
))
1464 one
= sval_cast(type
, one
);
1465 two
= sval_cast(type
, two
);
1467 if (one
.type
== &float_ctype
) {
1468 if (one
.fvalue
< two
.fvalue
)
1470 if (one
.fvalue
== two
.fvalue
)
1474 if (one
.type
== &double_ctype
) {
1475 if (one
.dvalue
< two
.dvalue
)
1477 if (one
.dvalue
== two
.dvalue
)
1481 if (one
.type
== &ldouble_ctype
) {
1482 if (one
.ldvalue
< two
.ldvalue
)
1484 if (one
.ldvalue
== two
.ldvalue
)
1488 sm_perror("bad type in fp_cmp(): %s", type_to_str(type
));
1492 static inline int sval_cmp(sval_t one
, sval_t two
)
1494 struct symbol
*type
;
1496 if (sval_is_fp(one
) || sval_is_fp(two
))
1497 return fp_cmp(one
, two
);
1500 if (sval_positive_bits(two
) > sval_positive_bits(one
))
1502 if (type_bits(type
) < 31)
1505 one
= sval_cast(type
, one
);
1506 two
= sval_cast(type
, two
);
1508 if (type_unsigned(type
)) {
1509 if (one
.uvalue
< two
.uvalue
)
1511 if (one
.uvalue
== two
.uvalue
)
1515 /* fix me handle type promotion and unsigned values */
1516 if (one
.value
< two
.value
)
1518 if (one
.value
== two
.value
)
1523 #endif /* !SMATCH_H_ */