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
30 #include "expression.h"
37 unsigned long long uvalue
;
41 typedef long long mtag_t
;
47 #define STATE(_x) static struct smatch_state _x = { .name = #_x }
48 extern struct smatch_state undefined
;
49 extern struct smatch_state ghost
;
50 extern struct smatch_state merged
;
51 extern struct smatch_state true_state
;
52 extern struct smatch_state false_state
;
53 DECLARE_ALLOCATOR(smatch_state
);
55 static inline void *INT_PTR(int i
)
57 return (void *)(long)i
;
60 static inline int PTR_INT(void *p
)
70 DECLARE_ALLOCATOR(tracker
);
71 DECLARE_PTR_LIST(tracker_list
, struct tracker
);
72 DECLARE_PTR_LIST(stree_stack
, struct stree
);
74 /* The first 3 struct members must match struct tracker */
79 unsigned short merged
:1;
80 unsigned short skip_implications
:1;
81 unsigned short nr_children
:14;
83 struct smatch_state
*state
;
85 struct sm_state
*left
;
86 struct sm_state
*right
;
87 struct state_list
*possible
;
89 #define MAX_CHILDREN 0x3FFF /* 2**14 - 1 */
95 DECLARE_ALLOCATOR(var_sym
);
96 DECLARE_PTR_LIST(var_sym_list
, struct var_sym
);
102 DECLARE_PTR_LIST(constraint_list
, struct constraint
);
112 ASSIGNMENT_HOOK_AFTER
,
114 GLOBAL_ASSIGNMENT_HOOK
,
119 WHOLE_CONDITION_HOOK
,
120 FUNCTION_CALL_HOOK_BEFORE
,
122 CALL_HOOK_AFTER_INLINE
,
123 FUNCTION_CALL_HOOK_AFTER_DB
,
124 CALL_ASSIGNMENT_HOOK
,
125 MACRO_ASSIGNMENT_HOOK
,
150 void add_hook(void *func
, enum hook_type type
);
151 typedef struct smatch_state
*(merge_func_t
)(struct smatch_state
*s1
, struct smatch_state
*s2
);
152 typedef struct smatch_state
*(unmatched_func_t
)(struct sm_state
*state
);
153 void add_merge_hook(int client_id
, merge_func_t
*func
);
154 void add_unmatched_state_hook(int client_id
, unmatched_func_t
*func
);
155 void add_pre_merge_hook(int client_id
, void (*hook
)(struct sm_state
*sm
));
156 typedef void (scope_hook
)(void *data
);
157 void add_scope_hook(scope_hook
*hook
, void *data
);
158 typedef void (func_hook
)(const char *fn
, struct expression
*expr
, void *data
);
159 typedef void (implication_hook
)(const char *fn
, struct expression
*call_expr
,
160 struct expression
*assign_expr
, void *data
);
161 typedef void (return_implies_hook
)(struct expression
*call_expr
,
162 int param
, char *key
, char *value
);
163 typedef int (implied_return_hook
)(struct expression
*call_expr
, void *info
, struct range_list
**rl
);
164 void add_function_hook(const char *look_for
, func_hook
*call_back
, void *data
);
166 void add_function_assign_hook(const char *look_for
, func_hook
*call_back
,
168 void add_implied_return_hook(const char *look_for
,
169 implied_return_hook
*call_back
,
171 void add_macro_assign_hook(const char *look_for
, func_hook
*call_back
,
173 void add_macro_assign_hook_extra(const char *look_for
, func_hook
*call_back
,
175 void return_implies_state(const char *look_for
, long long start
, long long end
,
176 implication_hook
*call_back
, void *info
);
177 void select_return_states_hook(int type
, return_implies_hook
*callback
);
178 void select_return_states_before(void (*fn
)(void));
179 void select_return_states_after(void (*fn
)(void));
180 int get_implied_return(struct expression
*expr
, struct range_list
**rl
);
181 void allocate_hook_memory(void);
183 struct modification_data
{
184 struct smatch_state
*prev
;
185 struct expression
*cur
;
188 typedef void (modification_hook
)(struct sm_state
*sm
, struct expression
*mod_expr
);
189 void add_modification_hook(int owner
, modification_hook
*call_back
);
190 void add_modification_hook_late(int owner
, modification_hook
*call_back
);
191 struct smatch_state
*get_modification_state(struct expression
*expr
);
193 int outside_of_function(void);
194 const char *get_filename(void);
195 const char *get_base_file(void);
196 char *get_function(void);
197 int get_lineno(void);
198 extern int final_pass
;
199 extern struct symbol
*cur_func_sym
;
200 extern int option_debug
;
201 extern int local_debug
;
202 extern int option_info
;
203 extern int option_spammy
;
204 extern char *trace_variable
;
205 extern struct stree
*global_states
;
206 int is_skipped_function(void);
207 int is_silenced_function(void);
209 /* smatch_impossible.c */
210 int is_impossible_path(void);
211 void set_path_impossible(void);
213 extern FILE *sm_outfd
;
214 extern FILE *sql_outfd
;
215 extern FILE *caller_info_fd
;
216 extern int sm_nr_checks
;
217 extern int sm_nr_errors
;
220 * How to use these routines:
222 * sm_fatal(): an internal error of some kind that should immediately exit
223 * sm_ierror(): an internal error
224 * sm_perror(): an internal error from parsing input source
225 * sm_error(): an error from input source
226 * sm_warning(): a warning from input source
227 * sm_info(): info message (from option_info)
228 * sm_debug(): debug message
229 * sm_msg(): other message (please avoid using this)
232 #define sm_printf(msg...) do { if (final_pass || option_debug || local_debug) fprintf(sm_outfd, msg); } while (0)
234 static inline void sm_prefix(void)
236 sm_printf("%s:%d %s() ", get_filename(), get_lineno(), get_function());
239 static inline void print_implied_debug_msg();
241 extern bool __silence_warnings_for_stmt
;
243 #define sm_print_msg(type, msg...) \
245 print_implied_debug_msg(); \
246 if (!final_pass && !option_debug && !local_debug) \
248 if (__silence_warnings_for_stmt && !option_debug && !local_debug) \
250 if (!option_info && is_silenced_function()) \
254 sm_printf("warn: "); \
256 } else if (type == 2) { \
257 sm_printf("error: "); \
259 } else if (type == 3) { \
260 sm_printf("parse error: "); \
267 #define sm_msg(msg...) do { sm_print_msg(0, msg); } while (0)
269 #define local_debug(msg...) \
275 extern char *implied_debug_msg
;
276 static inline void print_implied_debug_msg(void)
278 static struct symbol
*last_printed
= NULL
;
280 if (!implied_debug_msg
)
282 if (last_printed
== cur_func_sym
)
284 last_printed
= cur_func_sym
;
285 sm_msg("%s", implied_debug_msg
);
288 #define sm_debug(msg...) do { if (option_debug) sm_printf(msg); } while (0)
290 #define sm_info(msg...) do { \
291 if (option_debug || (option_info && final_pass)) { \
293 sm_printf("info: "); \
299 #define sm_warning(msg...) do { sm_print_msg(1, msg); } while (0)
300 #define sm_error(msg...) do { sm_print_msg(2, msg); } while (0)
301 #define sm_perror(msg...) do { sm_print_msg(3, msg); } while (0)
303 static inline void sm_fatal(const char *fmt
, ...)
308 vfprintf(sm_outfd
, fmt
, args
);
311 fprintf(sm_outfd
, "\n");
316 static inline void sm_ierror(const char *fmt
, ...)
322 fprintf(sm_outfd
, "internal error: ");
325 vfprintf(sm_outfd
, fmt
, args
);
328 fprintf(sm_outfd
, "\n");
330 #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
332 struct smatch_state
*__get_state(int owner
, const char *name
, struct symbol
*sym
);
333 struct smatch_state
*get_state(int owner
, const char *name
, struct symbol
*sym
);
334 struct smatch_state
*get_state_expr(int owner
, struct expression
*expr
);
335 struct state_list
*get_possible_states(int owner
, const char *name
,
337 struct state_list
*get_possible_states_expr(int owner
, struct expression
*expr
);
338 struct sm_state
*set_state(int owner
, const char *name
, struct symbol
*sym
,
339 struct smatch_state
*state
);
340 struct sm_state
*set_state_expr(int owner
, struct expression
*expr
,
341 struct smatch_state
*state
);
342 void delete_state(int owner
, const char *name
, struct symbol
*sym
);
343 void delete_state_expr(int owner
, struct expression
*expr
);
344 void __delete_all_states_sym(struct symbol
*sym
);
345 void set_true_false_states(int owner
, const char *name
, struct symbol
*sym
,
346 struct smatch_state
*true_state
,
347 struct smatch_state
*false_state
);
348 void set_true_false_states_expr(int owner
, struct expression
*expr
,
349 struct smatch_state
*true_state
,
350 struct smatch_state
*false_state
);
352 struct stree
*get_all_states_from_stree(int owner
, struct stree
*source
);
353 struct stree
*get_all_states_stree(int id
);
354 struct stree
*__get_cur_stree(void);
355 int is_reachable(void);
356 void add_get_state_hook(void (*fn
)(int owner
, const char *name
, struct symbol
*sym
));
358 /* smatch_helper.c */
359 DECLARE_PTR_LIST(int_stack
, int);
360 char *alloc_string(const char *str
);
361 void free_string(char *str
);
362 void append(char *dest
, const char *data
, int buff_len
);
363 void remove_parens(char *str
);
364 struct smatch_state
*alloc_state_num(int num
);
365 struct smatch_state
*alloc_state_str(const char *name
);
366 struct smatch_state
*alloc_state_expr(struct expression
*expr
);
367 struct expression
*get_argument_from_call_expr(struct expression_list
*args
,
370 char *expr_to_var(struct expression
*expr
);
371 struct symbol
*expr_to_sym(struct expression
*expr
);
372 char *expr_to_str(struct expression
*expr
);
373 char *expr_to_str_sym(struct expression
*expr
,
374 struct symbol
**sym_ptr
);
375 char *expr_to_var_sym(struct expression
*expr
,
376 struct symbol
**sym_ptr
);
377 char *expr_to_known_chunk_sym(struct expression
*expr
, struct symbol
**sym
);
378 char *expr_to_chunk_sym_vsl(struct expression
*expr
, struct symbol
**sym
, struct var_sym_list
**vsl
);
379 int get_complication_score(struct expression
*expr
);
381 int sym_name_is(const char *name
, struct expression
*expr
);
382 int get_const_value(struct expression
*expr
, sval_t
*sval
);
383 int get_value(struct expression
*expr
, sval_t
*val
);
384 int get_implied_value(struct expression
*expr
, sval_t
*val
);
385 int get_implied_min(struct expression
*expr
, sval_t
*sval
);
386 int get_implied_max(struct expression
*expr
, sval_t
*val
);
387 int get_hard_max(struct expression
*expr
, sval_t
*sval
);
388 int get_fuzzy_min(struct expression
*expr
, sval_t
*min
);
389 int get_fuzzy_max(struct expression
*expr
, sval_t
*max
);
390 int get_absolute_min(struct expression
*expr
, sval_t
*sval
);
391 int get_absolute_max(struct expression
*expr
, sval_t
*sval
);
392 int parse_call_math(struct expression
*expr
, char *math
, sval_t
*val
);
393 int parse_call_math_rl(struct expression
*call
, const char *math
, struct range_list
**rl
);
394 char *get_value_in_terms_of_parameter_math(struct expression
*expr
);
395 char *get_value_in_terms_of_parameter_math_var_sym(const char *var
, struct symbol
*sym
);
396 int is_zero(struct expression
*expr
);
397 int known_condition_true(struct expression
*expr
);
398 int known_condition_false(struct expression
*expr
);
399 int implied_condition_true(struct expression
*expr
);
400 int implied_condition_false(struct expression
*expr
);
401 int can_integer_overflow(struct symbol
*type
, struct expression
*expr
);
402 void clear_math_cache(void);
404 int is_array(struct expression
*expr
);
405 struct expression
*get_array_base(struct expression
*expr
);
406 struct expression
*get_array_offset(struct expression
*expr
);
407 const char *show_state(struct smatch_state
*state
);
408 struct statement
*get_expression_statement(struct expression
*expr
);
409 struct expression
*strip_parens(struct expression
*expr
);
410 struct expression
*strip_expr(struct expression
*expr
);
411 struct expression
*strip_expr_set_parent(struct expression
*expr
);
412 void scoped_state(int my_id
, const char *name
, struct symbol
*sym
);
413 int is_error_return(struct expression
*expr
);
414 int getting_address(void);
415 int get_struct_and_member(struct expression
*expr
, const char **type
, const char **member
);
416 char *get_member_name(struct expression
*expr
);
417 char *get_fnptr_name(struct expression
*expr
);
418 int cmp_pos(struct position pos1
, struct position pos2
);
419 int positions_eq(struct position pos1
, struct position pos2
);
420 struct statement
*get_current_statement(void);
421 struct statement
*get_prev_statement(void);
422 struct expression
*get_last_expr_from_expression_stmt(struct expression
*expr
);
423 int get_param_num_from_sym(struct symbol
*sym
);
424 int get_param_num(struct expression
*expr
);
425 int ms_since(struct timeval
*start
);
426 int parent_is_gone_var_sym(const char *name
, struct symbol
*sym
);
427 int parent_is_gone(struct expression
*expr
);
428 int invert_op(int op
);
429 int expr_equiv(struct expression
*one
, struct expression
*two
);
430 void push_int(struct int_stack
**stack
, int num
);
431 int pop_int(struct int_stack
**stack
);
434 struct symbol
*get_real_base_type(struct symbol
*sym
);
435 int type_bytes(struct symbol
*type
);
436 int array_bytes(struct symbol
*type
);
437 struct symbol
*get_pointer_type(struct expression
*expr
);
438 struct symbol
*get_type(struct expression
*expr
);
439 struct symbol
*get_final_type(struct expression
*expr
);
440 struct symbol
*get_promoted_type(struct symbol
*left
, struct symbol
*right
);
441 int type_signed(struct symbol
*base_type
);
442 int expr_unsigned(struct expression
*expr
);
443 int expr_signed(struct expression
*expr
);
444 int returns_unsigned(struct symbol
*base_type
);
445 int is_pointer(struct expression
*expr
);
446 int returns_pointer(struct symbol
*base_type
);
447 sval_t
sval_type_max(struct symbol
*base_type
);
448 sval_t
sval_type_min(struct symbol
*base_type
);
449 int nr_bits(struct expression
*expr
);
450 int is_void_pointer(struct expression
*expr
);
451 int is_char_pointer(struct expression
*expr
);
452 int is_string(struct expression
*expr
);
453 int is_static(struct expression
*expr
);
454 int is_local_variable(struct expression
*expr
);
455 int types_equiv(struct symbol
*one
, struct symbol
*two
);
457 const char *global_static();
458 struct symbol
*cur_func_return_type(void);
459 struct symbol
*get_arg_type(struct expression
*fn
, int arg
);
460 struct symbol
*get_member_type_from_key(struct expression
*expr
, const char *key
);
461 struct symbol
*get_arg_type_from_key(struct expression
*fn
, int param
, struct expression
*arg
, const char *key
);
462 int is_struct(struct expression
*expr
);
463 char *type_to_str(struct symbol
*type
);
465 /* smatch_ignore.c */
466 void add_ignore(int owner
, const char *name
, struct symbol
*sym
);
467 int is_ignored(int owner
, const char *name
, struct symbol
*sym
);
468 void add_ignore_expr(int owner
, struct expression
*expr
);
469 int is_ignored_expr(int owner
, struct expression
*expr
);
472 struct var_sym
*alloc_var_sym(const char *var
, struct symbol
*sym
);
473 struct var_sym_list
*expr_to_vsl(struct expression
*expr
);
474 void add_var_sym(struct var_sym_list
**list
, const char *var
, struct symbol
*sym
);
475 void add_var_sym_expr(struct var_sym_list
**list
, struct expression
*expr
);
476 void del_var_sym(struct var_sym_list
**list
, const char *var
, struct symbol
*sym
);
477 int in_var_sym_list(struct var_sym_list
*list
, const char *var
, struct symbol
*sym
);
478 struct var_sym_list
*clone_var_sym_list(struct var_sym_list
*from_vsl
);
479 void merge_var_sym_list(struct var_sym_list
**dest
, struct var_sym_list
*src
);
480 struct var_sym_list
*combine_var_sym_lists(struct var_sym_list
*one
, struct var_sym_list
*two
);
481 int var_sym_lists_equiv(struct var_sym_list
*one
, struct var_sym_list
*two
);
482 void free_var_sym_list(struct var_sym_list
**list
);
483 void free_var_syms_and_list(struct var_sym_list
**list
);
486 struct tracker
*alloc_tracker(int owner
, const char *name
, struct symbol
*sym
);
487 void add_tracker(struct tracker_list
**list
, int owner
, const char *name
,
489 void add_tracker_expr(struct tracker_list
**list
, int owner
, struct expression
*expr
);
490 void del_tracker(struct tracker_list
**list
, int owner
, const char *name
,
492 int in_tracker_list(struct tracker_list
*list
, int owner
, const char *name
,
494 void free_tracker_list(struct tracker_list
**list
);
495 void free_trackers_and_list(struct tracker_list
**list
);
497 /* smatch_conditions */
498 int in_condition(void);
502 extern int __in_fake_assign
;
503 extern int __in_fake_parameter_assign
;
504 extern int __in_fake_struct_assign
;
505 extern int in_fake_env
;
506 void smatch (int argc
, char **argv
);
507 int inside_loop(void);
508 int definitely_inside_loop(void);
509 struct expression
*get_switch_expr(void);
510 int in_expression_statement(void);
511 void __process_post_op_stack(void);
512 void __split_expr(struct expression
*expr
);
513 void __split_label_stmt(struct statement
*stmt
);
514 void __split_stmt(struct statement
*stmt
);
515 extern int __in_function_def
;
516 extern int option_assume_loops
;
517 extern int option_two_passes
;
518 extern int option_no_db
;
519 extern int option_file_output
;
520 extern int option_time
;
521 extern struct expression_list
*big_expression_stack
;
522 extern struct expression_list
*big_condition_stack
;
523 extern struct statement_list
*big_statement_stack
;
524 int is_assigned_call(struct expression
*expr
);
525 int inlinable(struct expression
*expr
);
526 extern int __inline_call
;
527 extern struct expression
*__inline_fn
;
528 extern int __in_pre_condition
;
529 extern int __bail_on_rest_of_function
;
530 extern struct statement
*__prev_stmt
;
531 extern struct statement
*__cur_stmt
;
532 extern struct statement
*__next_stmt
;
533 void init_fake_env(void);
534 void end_fake_env(void);
535 int time_parsing_function(void);
537 /* smatch_struct_assignment.c */
538 struct expression
*get_faked_expression(void);
539 void __fake_struct_member_assignments(struct expression
*expr
);
541 /* smatch_project.c */
542 int is_no_inline_function(const char *function
);
544 /* smatch_conditions */
545 void __split_whole_condition(struct expression
*expr
);
546 void __handle_logic(struct expression
*expr
);
547 int is_condition(struct expression
*expr
);
548 int __handle_condition_assigns(struct expression
*expr
);
549 int __handle_select_assigns(struct expression
*expr
);
550 int __handle_expr_statement_assigns(struct expression
*expr
);
552 /* smatch_implied.c */
553 extern int option_debug_implied
;
554 extern int option_debug_related
;
555 struct range_list_stack
;
556 void param_limit_implications(struct expression
*expr
, int param
, char *key
, char *value
);
557 struct stree
*__implied_case_stree(struct expression
*switch_expr
,
558 struct range_list
*case_rl
,
559 struct range_list_stack
**remaining_cases
,
560 struct stree
**raw_stree
);
561 void overwrite_states_using_pool(struct sm_state
*gate_sm
, struct sm_state
*pool_sm
);
562 int assume(struct expression
*expr
);
563 void end_assume(void);
564 int impossible_assumption(struct expression
*left
, int op
, sval_t sval
);
566 /* smatch_extras.c */
567 int in_warn_on_macro(void);
568 #define SMATCH_EXTRA 5 /* this is my_id from smatch extra set in smatch.c */
569 extern int RETURN_ID
;
576 #define MTAG_ALIAS_BIT (1ULL << 63)
577 #define MTAG_OFFSET_MASK 0xfffULL
579 extern long long valid_ptr_min
, valid_ptr_max
;
580 extern sval_t valid_ptr_min_sval
, valid_ptr_max_sval
;
581 extern struct range_list
*valid_ptr_rl
;
582 static const sval_t array_min_sval
= {
586 static const sval_t array_max_sval
= {
590 static const sval_t text_seg_min
= {
592 {.value
= 100000000},
594 static const sval_t text_seg_max
= {
596 {.value
= 177777777},
598 static const sval_t data_seg_min
= {
600 {.value
= 200000000},
602 static const sval_t data_seg_max
= {
604 {.value
= 277777777},
606 static const sval_t bss_seg_min
= {
608 {.value
= 300000000},
610 static const sval_t bss_seg_max
= {
612 {.value
= 377777777},
614 static const sval_t stack_seg_min
= {
616 {.value
= 400000000},
618 static const sval_t stack_seg_max
= {
620 {.value
= 477777777},
622 static const sval_t kmalloc_seg_min
= {
624 {.value
= 500000000},
626 static const sval_t kmalloc_seg_max
= {
628 {.value
= 577777777},
630 static const sval_t vmalloc_seg_min
= {
632 {.value
= 600000000},
634 static const sval_t vmalloc_seg_max
= {
636 {.value
= 677777777},
638 static const sval_t fn_ptr_min
= {
640 {.value
= 700000000},
642 static const sval_t fn_ptr_max
= {
644 {.value
= 777777777},
647 char *get_other_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
);
648 char *map_call_to_other_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
);
649 char *map_long_to_short_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
);
650 char *map_long_to_short_name_sym_nostack(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
);
652 #define STRLEN_MAX_RET 1010101
654 /* smatch_absolute.c */
655 int get_absolute_min_helper(struct expression
*expr
, sval_t
*sval
);
656 int get_absolute_max_helper(struct expression
*expr
, sval_t
*sval
);
658 /* smatch_local_values.c */
659 int get_local_rl(struct expression
*expr
, struct range_list
**rl
);
660 int get_local_max_helper(struct expression
*expr
, sval_t
*sval
);
661 int get_local_min_helper(struct expression
*expr
, sval_t
*sval
);
663 /* smatch_type_value.c */
664 int get_db_type_rl(struct expression
*expr
, struct range_list
**rl
);
665 /* smatch_data_val.c */
666 int get_mtag_rl(struct expression
*expr
, struct range_list
**rl
);
667 /* smatch_array_values.c */
668 int get_array_rl(struct expression
*expr
, struct range_list
**rl
);
670 /* smatch_states.c */
671 void __swap_cur_stree(struct stree
*stree
);
672 void __push_fake_cur_stree();
673 struct stree
*__pop_fake_cur_stree();
674 void __free_fake_cur_stree();
675 void __set_fake_cur_stree_fast(struct stree
*stree
);
676 void __pop_fake_cur_stree_fast(void);
677 void __merge_stree_into_cur(struct stree
*stree
);
679 int unreachable(void);
680 void __set_sm(struct sm_state
*sm
);
681 void __set_sm_cur_stree(struct sm_state
*sm
);
682 void __set_sm_fake_stree(struct sm_state
*sm
);
683 void __set_true_false_sm(struct sm_state
*true_state
,
684 struct sm_state
*false_state
);
685 void nullify_path(void);
686 void __match_nullify_path_hook(const char *fn
, struct expression
*expr
,
688 void __unnullify_path(void);
689 int __path_is_null(void);
690 void save_all_states(void);
691 void restore_all_states(void);
692 void free_goto_stack(void);
693 void clear_all_states(void);
695 struct sm_state
*get_sm_state(int owner
, const char *name
,
697 struct sm_state
*get_sm_state_expr(int owner
, struct expression
*expr
);
698 void __push_true_states(void);
699 void __use_false_states(void);
700 void __discard_false_states(void);
701 void __merge_false_states(void);
702 void __merge_true_states(void);
704 void __negate_cond_stacks(void);
705 void __use_pre_cond_states(void);
706 void __use_cond_true_states(void);
707 void __use_cond_false_states(void);
708 void __push_cond_stacks(void);
709 void __fold_in_set_states(void);
710 void __free_set_states(void);
711 struct stree
*__copy_cond_true_states(void);
712 struct stree
*__copy_cond_false_states(void);
713 struct stree
*__pop_cond_true_stack(void);
714 struct stree
*__pop_cond_false_stack(void);
715 void __and_cond_states(void);
716 void __or_cond_states(void);
717 void __save_pre_cond_states(void);
718 void __discard_pre_cond_states(void);
719 struct stree
*__get_true_states(void);
720 struct stree
*__get_false_states(void);
721 void __use_cond_states(void);
722 extern struct state_list
*__last_base_slist
;
724 void __push_continues(void);
725 void __discard_continues(void);
726 void __process_continues(void);
727 void __merge_continues(void);
729 void __push_breaks(void);
730 void __process_breaks(void);
731 int __has_breaks(void);
732 void __merge_breaks(void);
733 void __use_breaks(void);
735 void __save_switch_states(struct expression
*switch_expr
);
736 void __discard_switches(void);
737 int have_remaining_cases(void);
738 void __merge_switches(struct expression
*switch_expr
, struct range_list
*case_rl
);
739 void __push_default(void);
740 void __set_default(void);
741 int __pop_default(void);
743 void __push_conditions(void);
744 void __discard_conditions(void);
746 void __save_gotos(const char *name
, struct symbol
*sym
);
747 void __merge_gotos(const char *name
, struct symbol
*sym
);
749 void __print_cur_stree(void);
752 void __pass_to_client(void *data
, enum hook_type type
);
753 void __pass_to_client_no_data(enum hook_type type
);
754 void __pass_case_to_client(struct expression
*switch_expr
,
755 struct range_list
*rl
);
756 int __has_merge_function(int client_id
);
757 struct smatch_state
*__client_merge_function(int owner
,
758 struct smatch_state
*s1
,
759 struct smatch_state
*s2
);
760 struct smatch_state
*__client_unmatched_state_function(struct sm_state
*sm
);
761 void call_pre_merge_hook(struct sm_state
*sm
);
762 void __push_scope_hooks(void);
763 void __call_scope_hooks(void);
765 /* smatch_function_hooks.c */
766 void create_function_hook_hash(void);
767 void __match_initializer_call(struct symbol
*sym
);
773 * Changing these numbers is a pain. Don't do it. If you ever use a
774 * number it can't be re-used right away so there may be gaps.
775 * We select these in order by type so if the order matters, then give
776 * it a number below 100-999,9000-9999 ranges. */
790 LOCK_RELEASED
= 1009,
791 ABSOLUTE_LIMITS
= 1010,
803 UNTRACKED_PARAM
= 1023,
808 COMPARE_LIMIT
= 1028,
809 PARAM_COMPARE
= 1029,
812 CONSTRAINT_REQUIRED
= 1033,
818 /* put random temporary stuff in the 7000-7999 range for testing */
820 USER_DATA3_SET
= 9017,
822 NO_OVERFLOW_SIMPLE
= 8019,
828 NO_SIDE_EFFECT
= 8025,
831 ARRAYSIZE_ARG
= 8033,
838 extern struct sqlite3
*smatch_db
;
839 extern struct sqlite3
*mem_db
;
840 extern struct sqlite3
*cache_db
;
842 void db_ignore_states(int id
);
843 void select_caller_info_hook(void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
), int type
);
844 void add_member_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
));
845 void add_split_return_callback(void (*fn
)(int return_id
, char *return_ranges
, struct expression
*returned_expr
));
846 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
));
847 void select_call_implies_hook(int type
, void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
));
848 void select_return_implies_hook(int type
, void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
));
849 struct range_list
*db_return_vals(struct expression
*expr
);
850 struct range_list
*db_return_vals_from_str(const char *fn_name
);
851 char *return_state_to_var_sym(struct expression
*expr
, int param
, const char *key
, struct symbol
**sym
);
852 char *get_chunk_from_key(struct expression
*arg
, char *key
, struct symbol
**sym
, struct var_sym_list
**vsl
);
853 char *get_variable_from_key(struct expression
*arg
, const char *key
, struct symbol
**sym
);
854 const char *state_name_to_param_name(const char *state_name
, const char *param_name
);
855 const char *get_param_name_var_sym(const char *name
, struct symbol
*sym
);
856 const char *get_param_name(struct sm_state
*sm
);
857 const char *get_mtag_name_var_sym(const char *state_name
, struct symbol
*sym
);
858 const char *get_mtag_name_expr(struct expression
*expr
);
859 char *get_data_info_name(struct expression
*expr
);
861 char *escape_newlines(const char *str
);
862 void sql_exec(struct sqlite3
*db
, int (*callback
)(void*, int, char**, char**), void *data
, const char *sql
);
864 #define sql_helper(db, call_back, data, sql...) \
866 char sql_txt[1024]; \
868 sqlite3_snprintf(sizeof(sql_txt), sql_txt, sql); \
869 sm_debug("debug: %s\n", sql_txt); \
870 sql_exec(db, call_back, data, sql_txt); \
874 #define run_sql(call_back, data, sql...) \
878 sql_helper(smatch_db, call_back, data, sql); \
881 #define mem_sql(call_back, data, sql...) \
882 sql_helper(mem_db, call_back, data, sql)
884 #define cache_sql(call_back, data, sql...) \
885 sql_helper(cache_db, call_back, data, sql)
887 #define sql_insert_helper(table, db, ignore, late, values...) \
889 struct sqlite3 *_db = db; \
891 if (__inline_fn && !_db) \
895 char *err, *p = buf; \
898 p += snprintf(p, buf + sizeof(buf) - p, \
899 "insert %sinto %s values (", \
900 ignore ? "or ignore " : "", #table); \
901 p += snprintf(p, buf + sizeof(buf) - p, values); \
902 p += snprintf(p, buf + sizeof(buf) - p, ");"); \
903 sm_debug("mem-db: %s\n", buf); \
904 rc = sqlite3_exec(_db, buf, NULL, NULL, &err); \
905 if (rc != SQLITE_OK) { \
906 sm_ierror("SQL error #2: %s", err); \
907 sm_ierror("SQL: '%s'", buf); \
913 FILE *tmp_fd = sm_outfd; \
914 sm_outfd = sql_outfd; \
916 sm_printf("SQL%s: insert %sinto " #table " values(", \
917 late ? "_late" : "", ignore ? "or ignore " : ""); \
924 #define sql_insert(table, values...) sql_insert_helper(table, 0, 0, 0, values);
925 #define sql_insert_or_ignore(table, values...) sql_insert_helper(table, 0, 1, 0, values);
926 #define sql_insert_late(table, values...) sql_insert_helper(table, 0, 0, 1, values);
927 #define sql_insert_cache(table, values...) sql_insert_helper(table, cache_db, 1, 0, values);
929 char *get_static_filter(struct symbol
*sym
);
931 void sql_insert_return_states(int return_id
, const char *return_ranges
,
932 int type
, int param
, const char *key
, const char *value
);
933 void sql_insert_caller_info(struct expression
*call
, int type
, int param
,
934 const char *key
, const char *value
);
935 void sql_insert_function_ptr(const char *fn
, const char *struct_name
);
936 void sql_insert_return_values(const char *return_values
);
937 void sql_insert_return_implies(int type
, int param
, const char *key
, const char *value
);
938 void sql_insert_function_type_size(const char *member
, const char *ranges
);
939 void sql_insert_function_type_info(int type
, const char *struct_type
, const char *member
, const char *value
);
940 void sql_insert_type_info(int type
, const char *member
, const char *value
);
941 void sql_insert_local_values(const char *name
, const char *value
);
942 void sql_insert_function_type_value(const char *type
, const char *value
);
943 void sql_insert_function_type(int param
, const char *value
);
944 void sql_insert_parameter_name(int param
, const char *value
);
945 void sql_insert_data_info(struct expression
*data
, int type
, const char *value
);
946 void sql_insert_data_info_var_sym(const char *var
, struct symbol
*sym
, int type
, const char *value
);
947 void sql_save_constraint(const char *con
);
948 void sql_save_constraint_required(const char *data
, int op
, const char *limit
);
949 void sql_copy_constraint_required(const char *new_limit
, const char *old_limit
);
950 void sql_insert_fn_ptr_data_link(const char *ptr
, const char *data
);
951 void sql_insert_fn_data_link(struct expression
*fn
, int type
, int param
, const char *key
, const char *value
);
952 void sql_insert_mtag_about(mtag_t tag
, const char *left_name
, const char *right_name
);
953 void insert_mtag_data(sval_t sval
, struct range_list
*rl
);
954 void sql_insert_mtag_map(mtag_t tag
, int offset
, mtag_t container
);
955 void sql_insert_mtag_alias(mtag_t orig
, mtag_t alias
);
956 int mtag_map_select_container(mtag_t tag
, int offset
, mtag_t
*container
);
957 int mtag_map_select_tag(mtag_t container
, int offset
, mtag_t
*tag
);
959 void sql_select_return_states(const char *cols
, struct expression
*call
,
960 int (*callback
)(void*, int, char**, char**), void *info
);
961 void sql_select_call_implies(const char *cols
, struct expression
*call
,
962 int (*callback
)(void*, int, char**, char**));
964 void open_smatch_db(char *db_file
);
967 int open_data_file(const char *filename
);
968 int open_schema_file(const char *schema
);
969 struct token
*get_tokens_file(const char *filename
);
972 extern char *option_debug_check
;
973 extern char *option_project_str
;
974 extern char *bin_dir
;
975 extern char *data_dir
;
976 extern int option_no_data
;
977 extern int option_full_path
;
978 extern int option_param_mapper
;
979 extern int option_call_tree
;
980 extern int num_checks
;
990 extern enum project_type option_project
;
991 const char *check_name(unsigned short id
);
992 int id_from_name(const char *name
);
995 /* smatch_buf_size.c */
996 int get_array_size(struct expression
*expr
);
997 int get_array_size_bytes(struct expression
*expr
);
998 int get_array_size_bytes_min(struct expression
*expr
);
999 int get_array_size_bytes_max(struct expression
*expr
);
1000 struct range_list
*get_array_size_bytes_rl(struct expression
*expr
);
1001 int get_real_array_size(struct expression
*expr
);
1002 int last_member_is_resizable(struct symbol
*type
);
1003 /* smatch_strlen.c */
1004 int get_implied_strlen(struct expression
*expr
, struct range_list
**rl
);
1005 int get_size_from_strlen(struct expression
*expr
);
1007 /* smatch_capped.c */
1008 int is_capped(struct expression
*expr
);
1009 int is_capped_var_sym(const char *name
, struct symbol
*sym
);
1011 /* check_user_data.c */
1012 int is_user_macro(struct expression
*expr
);
1013 int is_user_data(struct expression
*expr
);
1014 int is_capped_user_data(struct expression
*expr
);
1015 int implied_user_data(struct expression
*expr
, struct range_list
**rl
);
1016 struct stree
*get_user_stree(void);
1017 int get_user_rl(struct expression
*expr
, struct range_list
**rl
);
1018 int get_user_rl_spammy(struct expression
*expr
, struct range_list
**rl
);
1019 int is_user_rl(struct expression
*expr
);
1020 int get_user_rl_var_sym(const char *name
, struct symbol
*sym
, struct range_list
**rl
);
1022 /* check_locking.c */
1023 void print_held_locks();
1025 /* check_assigned_expr.c */
1026 struct expression
*get_assigned_expr(struct expression
*expr
);
1027 struct expression
*get_assigned_expr_name_sym(const char *name
, struct symbol
*sym
);
1028 /* smatch_return_to_param.c */
1029 void __add_return_to_param_mapping(struct expression
*assign
, const char *return_string
);
1030 char *map_call_to_param_name_sym(struct expression
*expr
, struct symbol
**sym
);
1032 /* smatch_comparison.c */
1033 struct compare_data
{
1034 /* The ->left and ->right expression pointers might be NULL (I'm lazy) */
1035 struct expression
*left
;
1036 const char *left_var
;
1037 struct var_sym_list
*left_vsl
;
1039 struct expression
*right
;
1040 const char *right_var
;
1041 struct var_sym_list
*right_vsl
;
1043 DECLARE_ALLOCATOR(compare_data
);
1044 struct smatch_state
*alloc_compare_state(
1045 struct expression
*left
,
1046 const char *left_var
, struct var_sym_list
*left_vsl
,
1048 struct expression
*right
,
1049 const char *right_var
, struct var_sym_list
*right_vsl
);
1050 int filter_comparison(int orig
, int op
);
1051 int merge_comparisons(int one
, int two
);
1052 int combine_comparisons(int left_compare
, int right_compare
);
1053 int state_to_comparison(struct smatch_state
*state
);
1054 struct smatch_state
*merge_compare_states(struct smatch_state
*s1
, struct smatch_state
*s2
);
1055 int get_comparison(struct expression
*left
, struct expression
*right
);
1056 int get_comparison_strings(const char *one
, const char *two
);
1057 int possible_comparison(struct expression
*a
, int comparison
, struct expression
*b
);
1058 struct state_list
*get_all_comparisons(struct expression
*expr
);
1059 struct state_list
*get_all_possible_equal_comparisons(struct expression
*expr
);
1060 void __add_return_comparison(struct expression
*call
, const char *range
);
1061 void __add_comparison_info(struct expression
*expr
, struct expression
*call
, const char *range
);
1062 char *get_printed_param_name(struct expression
*call
, const char *param_name
, struct symbol
*param_sym
);
1063 char *name_sym_to_param_comparison(const char *name
, struct symbol
*sym
);
1064 char *expr_equal_to_param(struct expression
*expr
, int ignore
);
1065 char *expr_lte_to_param(struct expression
*expr
, int ignore
);
1066 char *expr_param_comparison(struct expression
*expr
, int ignore
);
1067 int flip_comparison(int op
);
1068 int negate_comparison(int op
);
1069 int remove_unsigned_from_comparison(int op
);
1070 int param_compare_limit_is_impossible(struct expression
*expr
, int left_param
, char *left_key
, char *value
);
1071 void filter_by_comparison(struct range_list
**rl
, int comparison
, struct range_list
*right
);
1072 struct sm_state
*comparison_implication_hook(struct expression
*expr
,
1073 struct state_list
**true_stack
,
1074 struct state_list
**false_stack
);
1075 void __compare_param_limit_hook(struct expression
*left_expr
, struct expression
*right_expr
,
1076 const char *state_name
,
1077 struct smatch_state
*true_state
, struct smatch_state
*false_state
);
1078 int impossibly_high_comparison(struct expression
*expr
);
1081 sval_t
*sval_alloc(sval_t sval
);
1082 sval_t
*sval_alloc_permanent(sval_t sval
);
1083 sval_t
sval_blank(struct expression
*expr
);
1084 sval_t
sval_type_val(struct symbol
*type
, long long val
);
1085 sval_t
sval_from_val(struct expression
*expr
, long long val
);
1086 int sval_is_ptr(sval_t sval
);
1087 int sval_unsigned(sval_t sval
);
1088 int sval_signed(sval_t sval
);
1089 int sval_bits(sval_t sval
);
1090 int sval_bits_used(sval_t sval
);
1091 int sval_is_negative(sval_t sval
);
1092 int sval_is_positive(sval_t sval
);
1093 int sval_is_min(sval_t sval
);
1094 int sval_is_max(sval_t sval
);
1095 int sval_is_a_min(sval_t sval
);
1096 int sval_is_a_max(sval_t sval
);
1097 int sval_is_negative_min(sval_t sval
);
1098 int sval_cmp_t(struct symbol
*type
, sval_t one
, sval_t two
);
1099 int sval_cmp_val(sval_t one
, long long val
);
1100 sval_t
sval_min(sval_t one
, sval_t two
);
1101 sval_t
sval_max(sval_t one
, sval_t two
);
1102 int sval_too_low(struct symbol
*type
, sval_t sval
);
1103 int sval_too_high(struct symbol
*type
, sval_t sval
);
1104 int sval_fits(struct symbol
*type
, sval_t sval
);
1105 sval_t
sval_cast(struct symbol
*type
, sval_t sval
);
1106 sval_t
sval_preop(sval_t sval
, int op
);
1107 sval_t
sval_binop(sval_t left
, int op
, sval_t right
);
1108 int sval_binop_overflows(sval_t left
, int op
, sval_t right
);
1109 int sval_binop_overflows_no_sign(sval_t left
, int op
, sval_t right
);
1110 unsigned long long fls_mask(unsigned long long uvalue
);
1111 unsigned long long sval_fls_mask(sval_t sval
);
1112 const char *sval_to_str(sval_t sval
);
1113 const char *sval_to_numstr(sval_t sval
);
1114 sval_t
ll_to_sval(long long val
);
1116 /* smatch_string_list.c */
1117 int list_has_string(struct string_list
*str_list
, const char *str
);
1118 void insert_string(struct string_list
**str_list
, const char *str
);
1119 struct string_list
*clone_str_list(struct string_list
*orig
);
1120 struct string_list
*combine_string_lists(struct string_list
*one
, struct string_list
*two
);
1122 /* smatch_start_states.c */
1123 struct stree
*get_start_states(void);
1125 /* smatch_recurse.c */
1126 int has_symbol(struct expression
*expr
, struct symbol
*sym
);
1127 int has_variable(struct expression
*expr
, struct expression
*var
);
1128 int has_inc_dec(struct expression
*expr
);
1130 /* smatch_stored_conditions.c */
1131 struct smatch_state
*get_stored_condition(struct expression
*expr
);
1132 struct expression_list
*get_conditions(struct expression
*expr
);
1133 struct sm_state
*stored_condition_implication_hook(struct expression
*expr
,
1134 struct state_list
**true_stack
,
1135 struct state_list
**false_stack
);
1137 /* check_string_len.c */
1138 int get_formatted_string_size(struct expression
*call
, int arg
);
1140 /* smatch_param_set.c */
1141 int param_was_set(struct expression
*expr
);
1142 int param_was_set_var_sym(const char *name
, struct symbol
*sym
);
1143 /* smatch_param_filter.c */
1144 int param_has_filter_data(struct sm_state
*sm
);
1146 /* smatch_links.c */
1147 void set_up_link_functions(int id
, int linkid
);
1148 struct smatch_state
*merge_link_states(struct smatch_state
*s1
, struct smatch_state
*s2
);
1149 void store_link(int link_id
, const char *name
, struct symbol
*sym
, const char *link_name
, struct symbol
*link_sym
);
1151 /* smatch_auto_copy.c */
1152 void set_auto_copy(int owner
);
1154 /* check_buf_comparison */
1155 struct expression
*get_size_variable(struct expression
*buf
);
1156 struct expression
*get_array_variable(struct expression
*size
);
1158 /* smatch_untracked_param.c */
1159 void mark_untracked(struct expression
*expr
, int param
, const char *key
, const char *value
);
1160 void add_untracked_param_hook(void (func
)(struct expression
*call
, int param
));
1161 void mark_all_params_untracked(int return_id
, char *return_ranges
, struct expression
*expr
);
1163 /* smatch_strings.c */
1164 struct state_list
*get_strings(struct expression
*expr
);
1165 struct expression
*fake_string_from_mtag(mtag_t tag
);
1167 /* smatch_estate.c */
1168 int estate_get_single_value(struct smatch_state
*state
, sval_t
*sval
);
1170 /* smatch_address.c */
1171 int get_address_rl(struct expression
*expr
, struct range_list
**rl
);
1172 int get_member_offset(struct symbol
*type
, const char *member_name
);
1173 int get_member_offset_from_deref(struct expression
*expr
);
1175 /* for now this is in smatch_used_parameter.c */
1176 void __get_state_hook(int owner
, const char *name
, struct symbol
*sym
);
1178 /* smatch_buf_comparison.c */
1179 int db_var_is_array_limit(struct expression
*array
, const char *name
, struct var_sym_list
*vsl
);
1181 struct stree
*get_all_return_states(void);
1182 struct stree_stack
*get_all_return_strees(void);
1183 int on_atomic_dec_path(void);
1184 int was_inced(const char *name
, struct symbol
*sym
);
1186 /* smatch_constraints.c */
1187 char *get_constraint_str(struct expression
*expr
);
1188 struct constraint_list
*get_constraints(struct expression
*expr
);
1189 char *unmet_constraint(struct expression
*data
, struct expression
*offset
);
1190 char *get_required_constraint(const char *data_str
);
1192 /* smatch_container_of.c */
1193 int get_param_from_container_of(struct expression
*expr
);
1194 int get_offset_from_container_of(struct expression
*expr
);
1197 int get_string_mtag(struct expression
*expr
, mtag_t
*tag
);
1198 int get_toplevel_mtag(struct symbol
*sym
, mtag_t
*tag
);
1199 int get_mtag(struct expression
*expr
, mtag_t
*tag
);
1200 int get_mtag_offset(struct expression
*expr
, mtag_t
*tag
, int *offset
);
1201 int create_mtag_alias(mtag_t tag
, struct expression
*expr
, mtag_t
*new);
1202 int expr_to_mtag_offset(struct expression
*expr
, mtag_t
*tag
, int *offset
);
1203 void update_mtag_data(struct expression
*expr
);
1204 int get_mtag_sval(struct expression
*expr
, sval_t
*sval
);
1205 int get_mtag_addr_sval(struct expression
*expr
, sval_t
*sval
);
1207 /* Trinity fuzzer stuff */
1208 const char *get_syscall_arg_type(struct symbol
*sym
);
1210 /* smatch_mem_tracker.c */
1211 extern int option_mem
;
1212 unsigned long get_max_memory(void);
1214 /* check_is_nospec.c */
1215 bool is_nospec(struct expression
*expr
);
1217 /* smatch_nul_terminator.c */
1218 bool is_nul_terminated(struct expression
*expr
);
1220 static inline int type_bits(struct symbol
*type
)
1224 if (type
->type
== SYM_PTR
) /* Sparse doesn't set this for &pointers */
1225 return bits_in_pointer
;
1226 if (type
->type
== SYM_ARRAY
)
1227 return bits_in_pointer
;
1228 if (!type
->examined
)
1229 examine_symbol_type(type
);
1230 return type
->bit_size
;
1233 static inline bool type_is_ptr(struct symbol
*type
)
1235 return type
&& (type
->type
== SYM_PTR
|| type
->type
== SYM_ARRAY
);
1238 static inline int type_unsigned(struct symbol
*base_type
)
1242 if (base_type
->ctype
.modifiers
& MOD_UNSIGNED
)
1247 static inline int type_positive_bits(struct symbol
*type
)
1251 if (type
->type
== SYM_ARRAY
)
1252 return bits_in_pointer
- 1;
1253 if (type_unsigned(type
))
1254 return type_bits(type
);
1255 return type_bits(type
) - 1;
1258 static inline int sval_positive_bits(sval_t sval
)
1260 return type_positive_bits(sval
.type
);
1264 * Returns -1 if one is smaller, 0 if they are the same and 1 if two is larger.
1266 static inline int sval_cmp(sval_t one
, sval_t two
)
1268 struct symbol
*type
;
1271 if (sval_positive_bits(two
) > sval_positive_bits(one
))
1273 if (type_bits(type
) < 31)
1276 one
= sval_cast(type
, one
);
1277 two
= sval_cast(type
, two
);
1279 if (type_unsigned(type
)) {
1280 if (one
.uvalue
< two
.uvalue
)
1282 if (one
.uvalue
== two
.uvalue
)
1286 /* fix me handle type promotion and unsigned values */
1287 if (one
.value
< two
.value
)
1289 if (one
.value
== two
.value
)
1294 #endif /* !SMATCH_H_ */