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 int nr_children
;
83 struct smatch_state
*state
;
85 struct sm_state
*left
;
86 struct sm_state
*right
;
87 struct state_list
*possible
;
94 DECLARE_ALLOCATOR(var_sym
);
95 DECLARE_PTR_LIST(var_sym_list
, struct var_sym
);
101 DECLARE_PTR_LIST(constraint_list
, struct constraint
);
111 ASSIGNMENT_HOOK_AFTER
,
113 GLOBAL_ASSIGNMENT_HOOK
,
118 WHOLE_CONDITION_HOOK
,
119 FUNCTION_CALL_HOOK_BEFORE
,
121 CALL_HOOK_AFTER_INLINE
,
122 FUNCTION_CALL_HOOK_AFTER_DB
,
123 CALL_ASSIGNMENT_HOOK
,
124 MACRO_ASSIGNMENT_HOOK
,
149 void add_hook(void *func
, enum hook_type type
);
150 typedef struct smatch_state
*(merge_func_t
)(struct smatch_state
*s1
, struct smatch_state
*s2
);
151 typedef struct smatch_state
*(unmatched_func_t
)(struct sm_state
*state
);
152 void add_merge_hook(int client_id
, merge_func_t
*func
);
153 void add_unmatched_state_hook(int client_id
, unmatched_func_t
*func
);
154 void add_pre_merge_hook(int client_id
, void (*hook
)(struct sm_state
*sm
));
155 typedef void (scope_hook
)(void *data
);
156 void add_scope_hook(scope_hook
*hook
, void *data
);
157 typedef void (func_hook
)(const char *fn
, struct expression
*expr
, void *data
);
158 typedef void (implication_hook
)(const char *fn
, struct expression
*call_expr
,
159 struct expression
*assign_expr
, void *data
);
160 typedef void (return_implies_hook
)(struct expression
*call_expr
,
161 int param
, char *key
, char *value
);
162 typedef int (implied_return_hook
)(struct expression
*call_expr
, void *info
, struct range_list
**rl
);
163 void add_function_hook(const char *look_for
, func_hook
*call_back
, void *data
);
165 void add_function_assign_hook(const char *look_for
, func_hook
*call_back
,
167 void add_implied_return_hook(const char *look_for
,
168 implied_return_hook
*call_back
,
170 void add_macro_assign_hook(const char *look_for
, func_hook
*call_back
,
172 void add_macro_assign_hook_extra(const char *look_for
, func_hook
*call_back
,
174 void return_implies_state(const char *look_for
, long long start
, long long end
,
175 implication_hook
*call_back
, void *info
);
176 void select_return_states_hook(int type
, return_implies_hook
*callback
);
177 void select_return_states_before(void (*fn
)(void));
178 void select_return_states_after(void (*fn
)(void));
179 int get_implied_return(struct expression
*expr
, struct range_list
**rl
);
180 void allocate_hook_memory(void);
182 struct modification_data
{
183 struct smatch_state
*prev
;
184 struct expression
*cur
;
187 typedef void (modification_hook
)(struct sm_state
*sm
, struct expression
*mod_expr
);
188 void add_modification_hook(int owner
, modification_hook
*call_back
);
189 void add_modification_hook_late(int owner
, modification_hook
*call_back
);
190 struct smatch_state
*get_modification_state(struct expression
*expr
);
192 int outside_of_function(void);
193 const char *get_filename(void);
194 const char *get_base_file(void);
195 char *get_function(void);
196 int get_lineno(void);
197 extern int final_pass
;
198 extern struct symbol
*cur_func_sym
;
199 extern int option_debug
;
200 extern int local_debug
;
201 extern int option_info
;
202 extern int option_spammy
;
203 extern char *trace_variable
;
204 extern struct stree
*global_states
;
205 int is_silenced_function(void);
207 /* smatch_impossible.c */
208 int is_impossible_path(void);
209 void set_path_impossible(void);
211 extern FILE *sm_outfd
;
212 extern FILE *sql_outfd
;
213 extern FILE *caller_info_fd
;
214 #define sm_printf(msg...) do { if (final_pass || option_debug || local_debug) fprintf(sm_outfd, msg); } while (0)
216 static inline void sm_prefix(void)
218 sm_printf("%s:%d %s() ", get_filename(), get_lineno(), get_function());
221 static inline void print_implied_debug_msg();
223 extern bool __silence_warnings_for_stmt
;
225 #define sm_msg(msg...) \
227 print_implied_debug_msg(); \
228 if (!final_pass && !option_debug && !local_debug) \
230 if (__silence_warnings_for_stmt && !option_debug && !local_debug) \
232 if (!option_info && is_silenced_function()) \
239 #define local_debug(msg...) \
245 extern char *implied_debug_msg
;
246 static inline void print_implied_debug_msg(void)
248 static struct symbol
*last_printed
= NULL
;
250 if (!implied_debug_msg
)
252 if (last_printed
== cur_func_sym
)
254 last_printed
= cur_func_sym
;
255 sm_msg("%s", implied_debug_msg
);
258 #define sm_debug(msg...) do { if (option_debug) sm_printf(msg); } while (0)
260 #define sm_info(msg...) do { \
261 if (option_debug || (option_info && final_pass)) { \
263 sm_printf("info: "); \
269 #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
271 struct smatch_state
*__get_state(int owner
, const char *name
, struct symbol
*sym
);
272 struct smatch_state
*get_state(int owner
, const char *name
, struct symbol
*sym
);
273 struct smatch_state
*get_state_expr(int owner
, struct expression
*expr
);
274 struct state_list
*get_possible_states(int owner
, const char *name
,
276 struct state_list
*get_possible_states_expr(int owner
, struct expression
*expr
);
277 struct sm_state
*set_state(int owner
, const char *name
, struct symbol
*sym
,
278 struct smatch_state
*state
);
279 struct sm_state
*set_state_expr(int owner
, struct expression
*expr
,
280 struct smatch_state
*state
);
281 void delete_state(int owner
, const char *name
, struct symbol
*sym
);
282 void delete_state_expr(int owner
, struct expression
*expr
);
283 void __delete_all_states_sym(struct symbol
*sym
);
284 void set_true_false_states(int owner
, const char *name
, struct symbol
*sym
,
285 struct smatch_state
*true_state
,
286 struct smatch_state
*false_state
);
287 void set_true_false_states_expr(int owner
, struct expression
*expr
,
288 struct smatch_state
*true_state
,
289 struct smatch_state
*false_state
);
291 struct stree
*get_all_states_from_stree(int owner
, struct stree
*source
);
292 struct stree
*get_all_states_stree(int id
);
293 struct stree
*__get_cur_stree(void);
294 int is_reachable(void);
295 void add_get_state_hook(void (*fn
)(int owner
, const char *name
, struct symbol
*sym
));
297 /* smatch_helper.c */
298 DECLARE_PTR_LIST(int_stack
, int);
299 char *alloc_string(const char *str
);
300 void free_string(char *str
);
301 void append(char *dest
, const char *data
, int buff_len
);
302 void remove_parens(char *str
);
303 struct smatch_state
*alloc_state_num(int num
);
304 struct smatch_state
*alloc_state_str(const char *name
);
305 struct smatch_state
*alloc_state_expr(struct expression
*expr
);
306 struct expression
*get_argument_from_call_expr(struct expression_list
*args
,
309 char *expr_to_var(struct expression
*expr
);
310 struct symbol
*expr_to_sym(struct expression
*expr
);
311 char *expr_to_str(struct expression
*expr
);
312 char *expr_to_str_sym(struct expression
*expr
,
313 struct symbol
**sym_ptr
);
314 char *expr_to_var_sym(struct expression
*expr
,
315 struct symbol
**sym_ptr
);
316 char *expr_to_known_chunk_sym(struct expression
*expr
, struct symbol
**sym
);
317 char *expr_to_chunk_sym_vsl(struct expression
*expr
, struct symbol
**sym
, struct var_sym_list
**vsl
);
318 int get_complication_score(struct expression
*expr
);
320 int sym_name_is(const char *name
, struct expression
*expr
);
321 int get_const_value(struct expression
*expr
, sval_t
*sval
);
322 int get_value(struct expression
*expr
, sval_t
*val
);
323 int get_implied_value(struct expression
*expr
, sval_t
*val
);
324 int get_implied_min(struct expression
*expr
, sval_t
*sval
);
325 int get_implied_max(struct expression
*expr
, sval_t
*val
);
326 int get_hard_max(struct expression
*expr
, sval_t
*sval
);
327 int get_fuzzy_min(struct expression
*expr
, sval_t
*min
);
328 int get_fuzzy_max(struct expression
*expr
, sval_t
*max
);
329 int get_absolute_min(struct expression
*expr
, sval_t
*sval
);
330 int get_absolute_max(struct expression
*expr
, sval_t
*sval
);
331 int parse_call_math(struct expression
*expr
, char *math
, sval_t
*val
);
332 int parse_call_math_rl(struct expression
*call
, char *math
, struct range_list
**rl
);
333 char *get_value_in_terms_of_parameter_math(struct expression
*expr
);
334 char *get_value_in_terms_of_parameter_math_var_sym(const char *var
, struct symbol
*sym
);
335 int is_zero(struct expression
*expr
);
336 int known_condition_true(struct expression
*expr
);
337 int known_condition_false(struct expression
*expr
);
338 int implied_condition_true(struct expression
*expr
);
339 int implied_condition_false(struct expression
*expr
);
340 int can_integer_overflow(struct symbol
*type
, struct expression
*expr
);
341 void clear_math_cache(void);
343 int is_array(struct expression
*expr
);
344 struct expression
*get_array_base(struct expression
*expr
);
345 struct expression
*get_array_offset(struct expression
*expr
);
346 const char *show_state(struct smatch_state
*state
);
347 struct statement
*get_expression_statement(struct expression
*expr
);
348 struct expression
*strip_parens(struct expression
*expr
);
349 struct expression
*strip_expr(struct expression
*expr
);
350 struct expression
*strip_expr_set_parent(struct expression
*expr
);
351 void scoped_state(int my_id
, const char *name
, struct symbol
*sym
);
352 int is_error_return(struct expression
*expr
);
353 int getting_address(void);
354 int get_struct_and_member(struct expression
*expr
, const char **type
, const char **member
);
355 char *get_member_name(struct expression
*expr
);
356 char *get_fnptr_name(struct expression
*expr
);
357 int cmp_pos(struct position pos1
, struct position pos2
);
358 int positions_eq(struct position pos1
, struct position pos2
);
359 struct statement
*get_current_statement(void);
360 struct statement
*get_prev_statement(void);
361 struct expression
*get_last_expr_from_expression_stmt(struct expression
*expr
);
362 int get_param_num_from_sym(struct symbol
*sym
);
363 int get_param_num(struct expression
*expr
);
364 int ms_since(struct timeval
*start
);
365 int parent_is_gone_var_sym(const char *name
, struct symbol
*sym
);
366 int parent_is_gone(struct expression
*expr
);
367 int invert_op(int op
);
368 int expr_equiv(struct expression
*one
, struct expression
*two
);
369 void push_int(struct int_stack
**stack
, int num
);
370 int pop_int(struct int_stack
**stack
);
373 struct symbol
*get_real_base_type(struct symbol
*sym
);
374 int type_bytes(struct symbol
*type
);
375 int array_bytes(struct symbol
*type
);
376 struct symbol
*get_pointer_type(struct expression
*expr
);
377 struct symbol
*get_type(struct expression
*expr
);
378 struct symbol
*get_final_type(struct expression
*expr
);
379 struct symbol
*get_promoted_type(struct symbol
*left
, struct symbol
*right
);
380 int type_signed(struct symbol
*base_type
);
381 int expr_unsigned(struct expression
*expr
);
382 int expr_signed(struct expression
*expr
);
383 int returns_unsigned(struct symbol
*base_type
);
384 int is_pointer(struct expression
*expr
);
385 int returns_pointer(struct symbol
*base_type
);
386 sval_t
sval_type_max(struct symbol
*base_type
);
387 sval_t
sval_type_min(struct symbol
*base_type
);
388 int nr_bits(struct expression
*expr
);
389 int is_void_pointer(struct expression
*expr
);
390 int is_char_pointer(struct expression
*expr
);
391 int is_string(struct expression
*expr
);
392 int is_static(struct expression
*expr
);
393 int is_local_variable(struct expression
*expr
);
394 int types_equiv(struct symbol
*one
, struct symbol
*two
);
396 const char *global_static();
397 struct symbol
*cur_func_return_type(void);
398 struct symbol
*get_arg_type(struct expression
*fn
, int arg
);
399 struct symbol
*get_member_type_from_key(struct expression
*expr
, const char *key
);
400 int is_struct(struct expression
*expr
);
401 char *type_to_str(struct symbol
*type
);
403 /* smatch_ignore.c */
404 void add_ignore(int owner
, const char *name
, struct symbol
*sym
);
405 int is_ignored(int owner
, const char *name
, struct symbol
*sym
);
406 void add_ignore_expr(int owner
, struct expression
*expr
);
407 int is_ignored_expr(int owner
, struct expression
*expr
);
410 struct var_sym
*alloc_var_sym(const char *var
, struct symbol
*sym
);
411 struct var_sym_list
*expr_to_vsl(struct expression
*expr
);
412 void add_var_sym(struct var_sym_list
**list
, const char *var
, struct symbol
*sym
);
413 void add_var_sym_expr(struct var_sym_list
**list
, struct expression
*expr
);
414 void del_var_sym(struct var_sym_list
**list
, const char *var
, struct symbol
*sym
);
415 int in_var_sym_list(struct var_sym_list
*list
, const char *var
, struct symbol
*sym
);
416 struct var_sym_list
*clone_var_sym_list(struct var_sym_list
*from_vsl
);
417 void merge_var_sym_list(struct var_sym_list
**dest
, struct var_sym_list
*src
);
418 struct var_sym_list
*combine_var_sym_lists(struct var_sym_list
*one
, struct var_sym_list
*two
);
419 int var_sym_lists_equiv(struct var_sym_list
*one
, struct var_sym_list
*two
);
420 void free_var_sym_list(struct var_sym_list
**list
);
421 void free_var_syms_and_list(struct var_sym_list
**list
);
424 struct tracker
*alloc_tracker(int owner
, const char *name
, struct symbol
*sym
);
425 void add_tracker(struct tracker_list
**list
, int owner
, const char *name
,
427 void add_tracker_expr(struct tracker_list
**list
, int owner
, struct expression
*expr
);
428 void del_tracker(struct tracker_list
**list
, int owner
, const char *name
,
430 int in_tracker_list(struct tracker_list
*list
, int owner
, const char *name
,
432 void free_tracker_list(struct tracker_list
**list
);
433 void free_trackers_and_list(struct tracker_list
**list
);
435 /* smatch_conditions */
436 int in_condition(void);
440 extern int __in_fake_assign
;
441 extern int __in_fake_struct_assign
;
442 extern int in_fake_env
;
443 void smatch (int argc
, char **argv
);
444 int inside_loop(void);
445 int definitely_inside_loop(void);
446 struct expression
*get_switch_expr(void);
447 int in_expression_statement(void);
448 void __process_post_op_stack(void);
449 void __split_expr(struct expression
*expr
);
450 void __split_label_stmt(struct statement
*stmt
);
451 void __split_stmt(struct statement
*stmt
);
452 extern int __in_function_def
;
453 extern int option_assume_loops
;
454 extern int option_two_passes
;
455 extern int option_no_db
;
456 extern int option_file_output
;
457 extern int option_time
;
458 extern struct expression_list
*big_expression_stack
;
459 extern struct expression_list
*big_condition_stack
;
460 extern struct statement_list
*big_statement_stack
;
461 int is_assigned_call(struct expression
*expr
);
462 int inlinable(struct expression
*expr
);
463 extern int __inline_call
;
464 extern struct expression
*__inline_fn
;
465 extern int __in_pre_condition
;
466 extern int __bail_on_rest_of_function
;
467 extern struct statement
*__prev_stmt
;
468 extern struct statement
*__cur_stmt
;
469 extern struct statement
*__next_stmt
;
470 void init_fake_env(void);
471 void end_fake_env(void);
472 int time_parsing_function(void);
474 /* smatch_struct_assignment.c */
475 struct expression
*get_faked_expression(void);
476 void __fake_struct_member_assignments(struct expression
*expr
);
478 /* smatch_project.c */
479 int is_no_inline_function(const char *function
);
481 /* smatch_conditions */
482 void __split_whole_condition(struct expression
*expr
);
483 void __handle_logic(struct expression
*expr
);
484 int is_condition(struct expression
*expr
);
485 int __handle_condition_assigns(struct expression
*expr
);
486 int __handle_select_assigns(struct expression
*expr
);
487 int __handle_expr_statement_assigns(struct expression
*expr
);
489 /* smatch_implied.c */
490 extern int option_debug_implied
;
491 extern int option_debug_related
;
492 struct range_list_stack
;
493 void param_limit_implications(struct expression
*expr
, int param
, char *key
, char *value
);
494 struct stree
*__implied_case_stree(struct expression
*switch_expr
,
495 struct range_list
*case_rl
,
496 struct range_list_stack
**remaining_cases
,
497 struct stree
**raw_stree
);
498 void overwrite_states_using_pool(struct sm_state
*gate_sm
, struct sm_state
*pool_sm
);
499 int assume(struct expression
*expr
);
500 void end_assume(void);
501 int impossible_assumption(struct expression
*left
, int op
, sval_t sval
);
503 /* smatch_extras.c */
504 #define SMATCH_EXTRA 5 /* this is my_id from smatch extra set in smatch.c */
505 extern int RETURN_ID
;
512 #define MTAG_ALIAS_BIT (1ULL << 63)
513 #define MTAG_OFFSET_MASK 0xfffULL
515 extern long long valid_ptr_min
, valid_ptr_max
;
516 extern sval_t valid_ptr_min_sval
, valid_ptr_max_sval
;
517 extern struct range_list
*valid_ptr_rl
;
518 static const sval_t array_min_sval
= {
522 static const sval_t array_max_sval
= {
526 static const sval_t text_seg_min
= {
528 {.value
= 100000000},
530 static const sval_t text_seg_max
= {
532 {.value
= 177777777},
534 static const sval_t data_seg_min
= {
536 {.value
= 200000000},
538 static const sval_t data_seg_max
= {
540 {.value
= 277777777},
542 static const sval_t bss_seg_min
= {
544 {.value
= 300000000},
546 static const sval_t bss_seg_max
= {
548 {.value
= 377777777},
550 static const sval_t stack_seg_min
= {
552 {.value
= 400000000},
554 static const sval_t stack_seg_max
= {
556 {.value
= 477777777},
558 static const sval_t kmalloc_seg_min
= {
560 {.value
= 500000000},
562 static const sval_t kmalloc_seg_max
= {
564 {.value
= 577777777},
566 static const sval_t vmalloc_seg_min
= {
568 {.value
= 600000000},
570 static const sval_t vmalloc_seg_max
= {
572 {.value
= 677777777},
574 static const sval_t fn_ptr_min
= {
576 {.value
= 700000000},
578 static const sval_t fn_ptr_max
= {
580 {.value
= 777777777},
583 char *get_other_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
);
584 char *map_call_to_other_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
);
585 char *map_long_to_short_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
);
586 char *map_long_to_short_name_sym_nostack(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
);
588 #define STRLEN_MAX_RET 1010101
590 /* smatch_absolute.c */
591 int get_absolute_min_helper(struct expression
*expr
, sval_t
*sval
);
592 int get_absolute_max_helper(struct expression
*expr
, sval_t
*sval
);
594 /* smatch_local_values.c */
595 int get_local_rl(struct expression
*expr
, struct range_list
**rl
);
596 int get_local_max_helper(struct expression
*expr
, sval_t
*sval
);
597 int get_local_min_helper(struct expression
*expr
, sval_t
*sval
);
599 /* smatch_type_value.c */
600 int get_db_type_rl(struct expression
*expr
, struct range_list
**rl
);
601 /* smatch_data_val.c */
602 int get_mtag_rl(struct expression
*expr
, struct range_list
**rl
);
604 /* smatch_states.c */
605 void __swap_cur_stree(struct stree
*stree
);
606 void __push_fake_cur_stree();
607 struct stree
*__pop_fake_cur_stree();
608 void __free_fake_cur_stree();
609 void __set_fake_cur_stree_fast(struct stree
*stree
);
610 void __pop_fake_cur_stree_fast(void);
611 void __merge_stree_into_cur(struct stree
*stree
);
613 int unreachable(void);
614 void __set_sm(struct sm_state
*sm
);
615 void __set_sm_cur_stree(struct sm_state
*sm
);
616 void __set_sm_fake_stree(struct sm_state
*sm
);
617 void __set_true_false_sm(struct sm_state
*true_state
,
618 struct sm_state
*false_state
);
619 void nullify_path(void);
620 void __match_nullify_path_hook(const char *fn
, struct expression
*expr
,
622 void __unnullify_path(void);
623 int __path_is_null(void);
624 void save_all_states(void);
625 void restore_all_states(void);
626 void free_goto_stack(void);
627 void clear_all_states(void);
629 struct sm_state
*get_sm_state(int owner
, const char *name
,
631 struct sm_state
*get_sm_state_expr(int owner
, struct expression
*expr
);
632 void __push_true_states(void);
633 void __use_false_states(void);
634 void __discard_false_states(void);
635 void __merge_false_states(void);
636 void __merge_true_states(void);
638 void __negate_cond_stacks(void);
639 void __use_pre_cond_states(void);
640 void __use_cond_true_states(void);
641 void __use_cond_false_states(void);
642 void __push_cond_stacks(void);
643 void __fold_in_set_states(void);
644 void __free_set_states(void);
645 struct stree
*__copy_cond_true_states(void);
646 struct stree
*__copy_cond_false_states(void);
647 struct stree
*__pop_cond_true_stack(void);
648 struct stree
*__pop_cond_false_stack(void);
649 void __and_cond_states(void);
650 void __or_cond_states(void);
651 void __save_pre_cond_states(void);
652 void __discard_pre_cond_states(void);
653 struct stree
*__get_true_states(void);
654 struct stree
*__get_false_states(void);
655 void __use_cond_states(void);
656 extern struct state_list
*__last_base_slist
;
658 void __push_continues(void);
659 void __discard_continues(void);
660 void __process_continues(void);
661 void __merge_continues(void);
663 void __push_breaks(void);
664 void __process_breaks(void);
665 int __has_breaks(void);
666 void __merge_breaks(void);
667 void __use_breaks(void);
669 void __save_switch_states(struct expression
*switch_expr
);
670 void __discard_switches(void);
671 int have_remaining_cases(void);
672 void __merge_switches(struct expression
*switch_expr
, struct range_list
*case_rl
);
673 void __push_default(void);
674 void __set_default(void);
675 int __pop_default(void);
677 void __push_conditions(void);
678 void __discard_conditions(void);
680 void __save_gotos(const char *name
, struct symbol
*sym
);
681 void __merge_gotos(const char *name
, struct symbol
*sym
);
683 void __print_cur_stree(void);
686 void __pass_to_client(void *data
, enum hook_type type
);
687 void __pass_to_client_no_data(enum hook_type type
);
688 void __pass_case_to_client(struct expression
*switch_expr
,
689 struct range_list
*rl
);
690 int __has_merge_function(int client_id
);
691 struct smatch_state
*__client_merge_function(int owner
,
692 struct smatch_state
*s1
,
693 struct smatch_state
*s2
);
694 struct smatch_state
*__client_unmatched_state_function(struct sm_state
*sm
);
695 void call_pre_merge_hook(struct sm_state
*sm
);
696 void __push_scope_hooks(void);
697 void __call_scope_hooks(void);
699 /* smatch_function_hooks.c */
700 void create_function_hook_hash(void);
701 void __match_initializer_call(struct symbol
*sym
);
707 * Changing these numbers is a pain. Don't do it. If you ever use a
708 * number it can't be re-used right away so there may be gaps.
709 * We select these in order by type so if the order matters, then give
710 * it a number below 100-999,9000-9999 ranges. */
724 LOCK_RELEASED
= 1009,
725 ABSOLUTE_LIMITS
= 1010,
737 UNTRACKED_PARAM
= 1023,
742 COMPARE_LIMIT
= 1028,
743 PARAM_COMPARE
= 1029,
746 CONSTRAINT_REQUIRED
= 1033,
749 /* put random temporary stuff in the 7000-7999 range for testing */
751 USER_DATA3_SET
= 9017,
753 NO_OVERFLOW_SIMPLE
= 8019,
759 NO_SIDE_EFFECT
= 8025,
762 ARRAYSIZE_ARG
= 8033,
769 extern struct sqlite3
*smatch_db
;
770 extern struct sqlite3
*mem_db
;
771 extern struct sqlite3
*cache_db
;
773 void debug_sql(struct sqlite3
*db
, const char *sql
);
774 void db_ignore_states(int id
);
775 void select_caller_info_hook(void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
), int type
);
776 void add_member_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
));
777 void add_split_return_callback(void (*fn
)(int return_id
, char *return_ranges
, struct expression
*returned_expr
));
778 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
));
779 void select_call_implies_hook(int type
, void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
));
780 void select_return_implies_hook(int type
, void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
));
781 struct range_list
*db_return_vals(struct expression
*expr
);
782 struct range_list
*db_return_vals_from_str(const char *fn_name
);
783 char *return_state_to_var_sym(struct expression
*expr
, int param
, const char *key
, struct symbol
**sym
);
784 char *get_chunk_from_key(struct expression
*arg
, char *key
, struct symbol
**sym
, struct var_sym_list
**vsl
);
785 char *get_variable_from_key(struct expression
*arg
, const char *key
, struct symbol
**sym
);
786 const char *state_name_to_param_name(const char *state_name
, const char *param_name
);
787 const char *get_param_name_var_sym(const char *name
, struct symbol
*sym
);
788 const char *get_param_name(struct sm_state
*sm
);
789 const char *get_mtag_name_var_sym(const char *state_name
, struct symbol
*sym
);
790 const char *get_mtag_name_expr(struct expression
*expr
);
791 char *get_data_info_name(struct expression
*expr
);
793 char *escape_newlines(char *str
);
794 void sql_exec(struct sqlite3
*db
, int (*callback
)(void*, int, char**, char**), void *data
, const char *sql
);
796 #define sql_helper(db, call_back, data, sql...) \
798 char sql_txt[1024]; \
800 sqlite3_snprintf(sizeof(sql_txt), sql_txt, sql); \
801 sm_debug("debug: %s\n", sql_txt); \
802 debug_sql(db, sql_txt); \
803 sql_exec(db, call_back, data, sql_txt); \
807 #define run_sql(call_back, data, sql...) \
811 sql_helper(smatch_db, call_back, data, sql); \
814 #define mem_sql(call_back, data, sql...) \
815 sql_helper(mem_db, call_back, data, sql)
817 #define cache_sql(call_back, data, sql...) \
818 sql_helper(cache_db, call_back, data, sql)
820 #define sql_insert_helper(table, db, ignore, late, values...) \
822 struct sqlite3 *_db = db; \
824 if (__inline_fn && !_db) \
828 char *err, *p = buf; \
831 p += snprintf(p, buf + sizeof(buf) - p, \
832 "insert %sinto %s values (", \
833 ignore ? "or ignore " : "", #table); \
834 p += snprintf(p, buf + sizeof(buf) - p, values); \
835 p += snprintf(p, buf + sizeof(buf) - p, ");"); \
836 sm_debug("mem-db: %s\n", buf); \
837 rc = sqlite3_exec(_db, buf, NULL, NULL, &err); \
838 if (rc != SQLITE_OK) { \
839 fprintf(stderr, "SQL error #2: %s\n", err); \
840 fprintf(stderr, "SQL: '%s'\n", buf); \
846 FILE *tmp_fd = sm_outfd; \
847 sm_outfd = sql_outfd; \
849 sm_printf("SQL%s: insert %sinto " #table " values(", \
850 late ? "_late" : "", ignore ? "or ignore " : ""); \
857 #define sql_insert(table, values...) sql_insert_helper(table, 0, 0, 0, values);
858 #define sql_insert_or_ignore(table, values...) sql_insert_helper(table, 0, 1, 0, values);
859 #define sql_insert_late(table, values...) sql_insert_helper(table, 0, 0, 1, values);
860 #define sql_insert_cache(table, values...) sql_insert_helper(table, cache_db, 1, 0, values);
862 char *get_static_filter(struct symbol
*sym
);
864 void sql_insert_return_states(int return_id
, const char *return_ranges
,
865 int type
, int param
, const char *key
, const char *value
);
866 void sql_insert_caller_info(struct expression
*call
, int type
, int param
,
867 const char *key
, const char *value
);
868 void sql_insert_function_ptr(const char *fn
, const char *struct_name
);
869 void sql_insert_return_values(const char *return_values
);
870 void sql_insert_return_implies(int type
, int param
, const char *key
, const char *value
);
871 void sql_insert_function_type_size(const char *member
, const char *ranges
);
872 void sql_insert_function_type_info(int type
, const char *struct_type
, const char *member
, const char *value
);
873 void sql_insert_type_info(int type
, const char *member
, const char *value
);
874 void sql_insert_local_values(const char *name
, const char *value
);
875 void sql_insert_function_type_value(const char *type
, const char *value
);
876 void sql_insert_function_type(int param
, const char *value
);
877 void sql_insert_parameter_name(int param
, const char *value
);
878 void sql_insert_data_info(struct expression
*data
, int type
, const char *value
);
879 void sql_insert_data_info_var_sym(const char *var
, struct symbol
*sym
, int type
, const char *value
);
880 void sql_save_constraint(const char *con
);
881 void sql_save_constraint_required(const char *data
, int op
, const char *limit
);
882 void sql_copy_constraint_required(const char *new_limit
, const char *old_limit
);
883 void sql_insert_fn_ptr_data_link(const char *ptr
, const char *data
);
884 void sql_insert_fn_data_link(struct expression
*fn
, int type
, int param
, const char *key
, const char *value
);
885 void sql_insert_mtag_about(mtag_t tag
, const char *left_name
, const char *right_name
);
886 void insert_mtag_data(sval_t sval
, struct range_list
*rl
);
887 void sql_insert_mtag_map(mtag_t tag
, int offset
, mtag_t container
);
888 void sql_insert_mtag_alias(mtag_t orig
, mtag_t alias
);
889 int mtag_map_select_container(mtag_t tag
, int offset
, mtag_t
*container
);
890 int mtag_map_select_tag(mtag_t container
, int offset
, mtag_t
*tag
);
892 void sql_select_return_states(const char *cols
, struct expression
*call
,
893 int (*callback
)(void*, int, char**, char**), void *info
);
894 void sql_select_call_implies(const char *cols
, struct expression
*call
,
895 int (*callback
)(void*, int, char**, char**));
897 void open_smatch_db(char *db_file
);
900 int open_data_file(const char *filename
);
901 int open_schema_file(const char *schema
);
902 struct token
*get_tokens_file(const char *filename
);
905 extern char *option_debug_check
;
906 extern char *option_project_str
;
907 extern char *bin_dir
;
908 extern char *data_dir
;
909 extern int option_no_data
;
910 extern int option_full_path
;
911 extern int option_param_mapper
;
912 extern int option_call_tree
;
913 extern int num_checks
;
921 extern enum project_type option_project
;
922 const char *check_name(unsigned short id
);
923 int id_from_name(const char *name
);
926 /* smatch_buf_size.c */
927 int get_array_size(struct expression
*expr
);
928 int get_array_size_bytes(struct expression
*expr
);
929 int get_array_size_bytes_min(struct expression
*expr
);
930 int get_array_size_bytes_max(struct expression
*expr
);
931 struct range_list
*get_array_size_bytes_rl(struct expression
*expr
);
932 int get_real_array_size(struct expression
*expr
);
933 int last_member_is_resizable(struct symbol
*type
);
934 /* smatch_strlen.c */
935 int get_implied_strlen(struct expression
*expr
, struct range_list
**rl
);
936 int get_size_from_strlen(struct expression
*expr
);
938 /* smatch_capped.c */
939 int is_capped(struct expression
*expr
);
940 int is_capped_var_sym(const char *name
, struct symbol
*sym
);
942 /* check_user_data.c */
943 int is_user_macro(struct expression
*expr
);
944 int is_user_data(struct expression
*expr
);
945 int is_capped_user_data(struct expression
*expr
);
946 int implied_user_data(struct expression
*expr
, struct range_list
**rl
);
947 struct stree
*get_user_stree(void);
948 int get_user_rl(struct expression
*expr
, struct range_list
**rl
);
949 int get_user_rl_spammy(struct expression
*expr
, struct range_list
**rl
);
950 int is_user_rl(struct expression
*expr
);
951 int get_user_rl_var_sym(const char *name
, struct symbol
*sym
, struct range_list
**rl
);
953 /* check_locking.c */
954 void print_held_locks();
956 /* check_assigned_expr.c */
957 struct expression
*get_assigned_expr(struct expression
*expr
);
958 struct expression
*get_assigned_expr_name_sym(const char *name
, struct symbol
*sym
);
959 /* smatch_return_to_param.c */
960 void __add_return_to_param_mapping(struct expression
*assign
, const char *return_string
);
961 char *map_call_to_param_name_sym(struct expression
*expr
, struct symbol
**sym
);
963 /* smatch_comparison.c */
964 struct compare_data
{
965 /* The ->left and ->right expression pointers might be NULL (I'm lazy) */
966 struct expression
*left
;
967 const char *left_var
;
968 struct var_sym_list
*left_vsl
;
970 struct expression
*right
;
971 const char *right_var
;
972 struct var_sym_list
*right_vsl
;
974 DECLARE_ALLOCATOR(compare_data
);
975 struct smatch_state
*alloc_compare_state(
976 struct expression
*left
,
977 const char *left_var
, struct var_sym_list
*left_vsl
,
979 struct expression
*right
,
980 const char *right_var
, struct var_sym_list
*right_vsl
);
981 int filter_comparison(int orig
, int op
);
982 int merge_comparisons(int one
, int two
);
983 int combine_comparisons(int left_compare
, int right_compare
);
984 int state_to_comparison(struct smatch_state
*state
);
985 struct smatch_state
*merge_compare_states(struct smatch_state
*s1
, struct smatch_state
*s2
);
986 int get_comparison(struct expression
*left
, struct expression
*right
);
987 int get_comparison_strings(const char *one
, const char *two
);
988 int possible_comparison(struct expression
*a
, int comparison
, struct expression
*b
);
989 struct state_list
*get_all_comparisons(struct expression
*expr
);
990 struct state_list
*get_all_possible_equal_comparisons(struct expression
*expr
);
991 void __add_return_comparison(struct expression
*call
, const char *range
);
992 void __add_comparison_info(struct expression
*expr
, struct expression
*call
, const char *range
);
993 char *get_printed_param_name(struct expression
*call
, const char *param_name
, struct symbol
*param_sym
);
994 char *name_sym_to_param_comparison(const char *name
, struct symbol
*sym
);
995 char *expr_equal_to_param(struct expression
*expr
, int ignore
);
996 char *expr_lte_to_param(struct expression
*expr
, int ignore
);
997 char *expr_param_comparison(struct expression
*expr
, int ignore
);
998 int flip_comparison(int op
);
999 int negate_comparison(int op
);
1000 int remove_unsigned_from_comparison(int op
);
1001 int param_compare_limit_is_impossible(struct expression
*expr
, int left_param
, char *left_key
, char *value
);
1002 void filter_by_comparison(struct range_list
**rl
, int comparison
, struct range_list
*right
);
1003 struct sm_state
*comparison_implication_hook(struct expression
*expr
,
1004 struct state_list
**true_stack
,
1005 struct state_list
**false_stack
);
1006 void __compare_param_limit_hook(struct expression
*left_expr
, struct expression
*right_expr
,
1007 const char *state_name
,
1008 struct smatch_state
*true_state
, struct smatch_state
*false_state
);
1009 int impossibly_high_comparison(struct expression
*expr
);
1012 sval_t
*sval_alloc(sval_t sval
);
1013 sval_t
*sval_alloc_permanent(sval_t sval
);
1014 sval_t
sval_blank(struct expression
*expr
);
1015 sval_t
sval_type_val(struct symbol
*type
, long long val
);
1016 sval_t
sval_from_val(struct expression
*expr
, long long val
);
1017 int sval_is_ptr(sval_t sval
);
1018 int sval_unsigned(sval_t sval
);
1019 int sval_signed(sval_t sval
);
1020 int sval_bits(sval_t sval
);
1021 int sval_bits_used(sval_t sval
);
1022 int sval_is_negative(sval_t sval
);
1023 int sval_is_positive(sval_t sval
);
1024 int sval_is_min(sval_t sval
);
1025 int sval_is_max(sval_t sval
);
1026 int sval_is_a_min(sval_t sval
);
1027 int sval_is_a_max(sval_t sval
);
1028 int sval_is_negative_min(sval_t sval
);
1029 int sval_cmp_t(struct symbol
*type
, sval_t one
, sval_t two
);
1030 int sval_cmp_val(sval_t one
, long long val
);
1031 sval_t
sval_min(sval_t one
, sval_t two
);
1032 sval_t
sval_max(sval_t one
, sval_t two
);
1033 int sval_too_low(struct symbol
*type
, sval_t sval
);
1034 int sval_too_high(struct symbol
*type
, sval_t sval
);
1035 int sval_fits(struct symbol
*type
, sval_t sval
);
1036 sval_t
sval_cast(struct symbol
*type
, sval_t sval
);
1037 sval_t
sval_preop(sval_t sval
, int op
);
1038 sval_t
sval_binop(sval_t left
, int op
, sval_t right
);
1039 int sval_binop_overflows(sval_t left
, int op
, sval_t right
);
1040 unsigned long long fls_mask(unsigned long long uvalue
);
1041 unsigned long long sval_fls_mask(sval_t sval
);
1042 const char *sval_to_str(sval_t sval
);
1043 const char *sval_to_numstr(sval_t sval
);
1044 sval_t
ll_to_sval(long long val
);
1046 /* smatch_string_list.c */
1047 int list_has_string(struct string_list
*str_list
, const char *str
);
1048 void insert_string(struct string_list
**str_list
, const char *str
);
1049 struct string_list
*clone_str_list(struct string_list
*orig
);
1050 struct string_list
*combine_string_lists(struct string_list
*one
, struct string_list
*two
);
1052 /* smatch_start_states.c */
1053 struct stree
*get_start_states(void);
1055 /* smatch_recurse.c */
1056 int has_symbol(struct expression
*expr
, struct symbol
*sym
);
1057 int has_variable(struct expression
*expr
, struct expression
*var
);
1058 int has_inc_dec(struct expression
*expr
);
1060 /* smatch_stored_conditions.c */
1061 struct smatch_state
*get_stored_condition(struct expression
*expr
);
1062 struct expression_list
*get_conditions(struct expression
*expr
);
1063 struct sm_state
*stored_condition_implication_hook(struct expression
*expr
,
1064 struct state_list
**true_stack
,
1065 struct state_list
**false_stack
);
1067 /* check_string_len.c */
1068 int get_formatted_string_size(struct expression
*call
, int arg
);
1070 /* smatch_param_set.c */
1071 int param_was_set(struct expression
*expr
);
1072 int param_was_set_var_sym(const char *name
, struct symbol
*sym
);
1073 /* smatch_param_filter.c */
1074 int param_has_filter_data(struct sm_state
*sm
);
1076 /* smatch_links.c */
1077 void set_up_link_functions(int id
, int linkid
);
1078 struct smatch_state
*merge_link_states(struct smatch_state
*s1
, struct smatch_state
*s2
);
1079 void store_link(int link_id
, const char *name
, struct symbol
*sym
, const char *link_name
, struct symbol
*link_sym
);
1081 /* smatch_auto_copy.c */
1082 void set_auto_copy(int owner
);
1084 /* check_buf_comparison */
1085 struct expression
*get_size_variable(struct expression
*buf
);
1086 struct expression
*get_array_variable(struct expression
*size
);
1088 /* smatch_untracked_param.c */
1089 void mark_untracked(struct expression
*expr
, int param
, const char *key
, const char *value
);
1090 void add_untracked_param_hook(void (func
)(struct expression
*call
, int param
));
1091 void mark_all_params_untracked(int return_id
, char *return_ranges
, struct expression
*expr
);
1093 /* smatch_strings.c */
1094 struct state_list
*get_strings(struct expression
*expr
);
1095 struct expression
*fake_string_from_mtag(mtag_t tag
);
1097 /* smatch_estate.c */
1098 int estate_get_single_value(struct smatch_state
*state
, sval_t
*sval
);
1100 /* smatch_address.c */
1101 int get_address_rl(struct expression
*expr
, struct range_list
**rl
);
1102 int get_member_offset(struct symbol
*type
, const char *member_name
);
1103 int get_member_offset_from_deref(struct expression
*expr
);
1105 /* for now this is in smatch_used_parameter.c */
1106 void __get_state_hook(int owner
, const char *name
, struct symbol
*sym
);
1108 /* smatch_buf_comparison.c */
1109 int db_var_is_array_limit(struct expression
*array
, const char *name
, struct var_sym_list
*vsl
);
1111 struct stree
*get_all_return_states(void);
1112 struct stree_stack
*get_all_return_strees(void);
1113 int on_atomic_dec_path(void);
1114 int was_inced(const char *name
, struct symbol
*sym
);
1116 /* smatch_constraints.c */
1117 char *get_constraint_str(struct expression
*expr
);
1118 struct constraint_list
*get_constraints(struct expression
*expr
);
1119 char *unmet_constraint(struct expression
*data
, struct expression
*offset
);
1120 char *get_required_constraint(const char *data_str
);
1122 /* smatch_container_of.c */
1123 int get_param_from_container_of(struct expression
*expr
);
1124 int get_offset_from_container_of(struct expression
*expr
);
1127 int get_string_mtag(struct expression
*expr
, mtag_t
*tag
);
1128 int get_toplevel_mtag(struct symbol
*sym
, mtag_t
*tag
);
1129 int get_mtag(struct expression
*expr
, mtag_t
*tag
);
1130 int get_mtag_offset(struct expression
*expr
, mtag_t
*tag
, int *offset
);
1131 int create_mtag_alias(mtag_t tag
, struct expression
*expr
, mtag_t
*new);
1132 int expr_to_mtag_offset(struct expression
*expr
, mtag_t
*tag
, int *offset
);
1133 void update_mtag_data(struct expression
*expr
);
1134 int get_mtag_sval(struct expression
*expr
, sval_t
*sval
);
1135 int get_mtag_addr_sval(struct expression
*expr
, sval_t
*sval
);
1137 /* smatch_mem_tracker.c */
1138 extern int option_mem
;
1139 unsigned long get_max_memory(void);
1141 /* check_is_nospec.c */
1142 bool is_nospec(struct expression
*expr
);
1144 static inline int type_bits(struct symbol
*type
)
1148 if (type
->type
== SYM_PTR
) /* Sparse doesn't set this for &pointers */
1149 return bits_in_pointer
;
1150 if (type
->type
== SYM_ARRAY
)
1151 return bits_in_pointer
;
1152 if (!type
->examined
)
1153 examine_symbol_type(type
);
1154 return type
->bit_size
;
1157 static inline bool type_is_ptr(struct symbol
*type
)
1159 return type
&& (type
->type
== SYM_PTR
|| type
->type
== SYM_ARRAY
);
1162 static inline int type_unsigned(struct symbol
*base_type
)
1166 if (base_type
->ctype
.modifiers
& MOD_UNSIGNED
)
1171 static inline int type_positive_bits(struct symbol
*type
)
1175 if (type
->type
== SYM_ARRAY
)
1176 return bits_in_pointer
- 1;
1177 if (type_unsigned(type
))
1178 return type_bits(type
);
1179 return type_bits(type
) - 1;
1182 static inline int sval_positive_bits(sval_t sval
)
1184 return type_positive_bits(sval
.type
);
1188 * Returns -1 if one is smaller, 0 if they are the same and 1 if two is larger.
1190 static inline int sval_cmp(sval_t one
, sval_t two
)
1192 struct symbol
*type
;
1195 if (sval_positive_bits(two
) > sval_positive_bits(one
))
1197 if (type_bits(type
) < 31)
1200 one
= sval_cast(type
, one
);
1201 two
= sval_cast(type
, two
);
1203 if (type_unsigned(type
)) {
1204 if (one
.uvalue
< two
.uvalue
)
1206 if (one
.uvalue
== two
.uvalue
)
1210 /* fix me handle type promotion and unsigned values */
1211 if (one
.value
< two
.value
)
1213 if (one
.value
== two
.value
)
1218 #endif /* !SMATCH_H_ */