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
);
603 /* smatch_array_values.c */
604 int get_array_rl(struct expression
*expr
, struct range_list
**rl
);
606 /* smatch_states.c */
607 void __swap_cur_stree(struct stree
*stree
);
608 void __push_fake_cur_stree();
609 struct stree
*__pop_fake_cur_stree();
610 void __free_fake_cur_stree();
611 void __set_fake_cur_stree_fast(struct stree
*stree
);
612 void __pop_fake_cur_stree_fast(void);
613 void __merge_stree_into_cur(struct stree
*stree
);
615 int unreachable(void);
616 void __set_sm(struct sm_state
*sm
);
617 void __set_sm_cur_stree(struct sm_state
*sm
);
618 void __set_sm_fake_stree(struct sm_state
*sm
);
619 void __set_true_false_sm(struct sm_state
*true_state
,
620 struct sm_state
*false_state
);
621 void nullify_path(void);
622 void __match_nullify_path_hook(const char *fn
, struct expression
*expr
,
624 void __unnullify_path(void);
625 int __path_is_null(void);
626 void save_all_states(void);
627 void restore_all_states(void);
628 void free_goto_stack(void);
629 void clear_all_states(void);
631 struct sm_state
*get_sm_state(int owner
, const char *name
,
633 struct sm_state
*get_sm_state_expr(int owner
, struct expression
*expr
);
634 void __push_true_states(void);
635 void __use_false_states(void);
636 void __discard_false_states(void);
637 void __merge_false_states(void);
638 void __merge_true_states(void);
640 void __negate_cond_stacks(void);
641 void __use_pre_cond_states(void);
642 void __use_cond_true_states(void);
643 void __use_cond_false_states(void);
644 void __push_cond_stacks(void);
645 void __fold_in_set_states(void);
646 void __free_set_states(void);
647 struct stree
*__copy_cond_true_states(void);
648 struct stree
*__copy_cond_false_states(void);
649 struct stree
*__pop_cond_true_stack(void);
650 struct stree
*__pop_cond_false_stack(void);
651 void __and_cond_states(void);
652 void __or_cond_states(void);
653 void __save_pre_cond_states(void);
654 void __discard_pre_cond_states(void);
655 struct stree
*__get_true_states(void);
656 struct stree
*__get_false_states(void);
657 void __use_cond_states(void);
658 extern struct state_list
*__last_base_slist
;
660 void __push_continues(void);
661 void __discard_continues(void);
662 void __process_continues(void);
663 void __merge_continues(void);
665 void __push_breaks(void);
666 void __process_breaks(void);
667 int __has_breaks(void);
668 void __merge_breaks(void);
669 void __use_breaks(void);
671 void __save_switch_states(struct expression
*switch_expr
);
672 void __discard_switches(void);
673 int have_remaining_cases(void);
674 void __merge_switches(struct expression
*switch_expr
, struct range_list
*case_rl
);
675 void __push_default(void);
676 void __set_default(void);
677 int __pop_default(void);
679 void __push_conditions(void);
680 void __discard_conditions(void);
682 void __save_gotos(const char *name
, struct symbol
*sym
);
683 void __merge_gotos(const char *name
, struct symbol
*sym
);
685 void __print_cur_stree(void);
688 void __pass_to_client(void *data
, enum hook_type type
);
689 void __pass_to_client_no_data(enum hook_type type
);
690 void __pass_case_to_client(struct expression
*switch_expr
,
691 struct range_list
*rl
);
692 int __has_merge_function(int client_id
);
693 struct smatch_state
*__client_merge_function(int owner
,
694 struct smatch_state
*s1
,
695 struct smatch_state
*s2
);
696 struct smatch_state
*__client_unmatched_state_function(struct sm_state
*sm
);
697 void call_pre_merge_hook(struct sm_state
*sm
);
698 void __push_scope_hooks(void);
699 void __call_scope_hooks(void);
701 /* smatch_function_hooks.c */
702 void create_function_hook_hash(void);
703 void __match_initializer_call(struct symbol
*sym
);
709 * Changing these numbers is a pain. Don't do it. If you ever use a
710 * number it can't be re-used right away so there may be gaps.
711 * We select these in order by type so if the order matters, then give
712 * it a number below 100-999,9000-9999 ranges. */
726 LOCK_RELEASED
= 1009,
727 ABSOLUTE_LIMITS
= 1010,
739 UNTRACKED_PARAM
= 1023,
744 COMPARE_LIMIT
= 1028,
745 PARAM_COMPARE
= 1029,
748 CONSTRAINT_REQUIRED
= 1033,
751 /* put random temporary stuff in the 7000-7999 range for testing */
753 USER_DATA3_SET
= 9017,
755 NO_OVERFLOW_SIMPLE
= 8019,
761 NO_SIDE_EFFECT
= 8025,
764 ARRAYSIZE_ARG
= 8033,
771 extern struct sqlite3
*smatch_db
;
772 extern struct sqlite3
*mem_db
;
773 extern struct sqlite3
*cache_db
;
775 void debug_sql(struct sqlite3
*db
, const char *sql
);
776 void db_ignore_states(int id
);
777 void select_caller_info_hook(void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
), int type
);
778 void add_member_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
));
779 void add_split_return_callback(void (*fn
)(int return_id
, char *return_ranges
, struct expression
*returned_expr
));
780 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
));
781 void select_call_implies_hook(int type
, void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
));
782 void select_return_implies_hook(int type
, void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
));
783 struct range_list
*db_return_vals(struct expression
*expr
);
784 struct range_list
*db_return_vals_from_str(const char *fn_name
);
785 char *return_state_to_var_sym(struct expression
*expr
, int param
, const char *key
, struct symbol
**sym
);
786 char *get_chunk_from_key(struct expression
*arg
, char *key
, struct symbol
**sym
, struct var_sym_list
**vsl
);
787 char *get_variable_from_key(struct expression
*arg
, const char *key
, struct symbol
**sym
);
788 const char *state_name_to_param_name(const char *state_name
, const char *param_name
);
789 const char *get_param_name_var_sym(const char *name
, struct symbol
*sym
);
790 const char *get_param_name(struct sm_state
*sm
);
791 const char *get_mtag_name_var_sym(const char *state_name
, struct symbol
*sym
);
792 const char *get_mtag_name_expr(struct expression
*expr
);
793 char *get_data_info_name(struct expression
*expr
);
795 char *escape_newlines(char *str
);
796 void sql_exec(struct sqlite3
*db
, int (*callback
)(void*, int, char**, char**), void *data
, const char *sql
);
798 #define sql_helper(db, call_back, data, sql...) \
800 char sql_txt[1024]; \
802 sqlite3_snprintf(sizeof(sql_txt), sql_txt, sql); \
803 sm_debug("debug: %s\n", sql_txt); \
804 debug_sql(db, sql_txt); \
805 sql_exec(db, call_back, data, sql_txt); \
809 #define run_sql(call_back, data, sql...) \
813 sql_helper(smatch_db, call_back, data, sql); \
816 #define mem_sql(call_back, data, sql...) \
817 sql_helper(mem_db, call_back, data, sql)
819 #define cache_sql(call_back, data, sql...) \
820 sql_helper(cache_db, call_back, data, sql)
822 #define sql_insert_helper(table, db, ignore, late, values...) \
824 struct sqlite3 *_db = db; \
826 if (__inline_fn && !_db) \
830 char *err, *p = buf; \
833 p += snprintf(p, buf + sizeof(buf) - p, \
834 "insert %sinto %s values (", \
835 ignore ? "or ignore " : "", #table); \
836 p += snprintf(p, buf + sizeof(buf) - p, values); \
837 p += snprintf(p, buf + sizeof(buf) - p, ");"); \
838 sm_debug("mem-db: %s\n", buf); \
839 rc = sqlite3_exec(_db, buf, NULL, NULL, &err); \
840 if (rc != SQLITE_OK) { \
841 fprintf(stderr, "SQL error #2: %s\n", err); \
842 fprintf(stderr, "SQL: '%s'\n", buf); \
848 FILE *tmp_fd = sm_outfd; \
849 sm_outfd = sql_outfd; \
851 sm_printf("SQL%s: insert %sinto " #table " values(", \
852 late ? "_late" : "", ignore ? "or ignore " : ""); \
859 #define sql_insert(table, values...) sql_insert_helper(table, 0, 0, 0, values);
860 #define sql_insert_or_ignore(table, values...) sql_insert_helper(table, 0, 1, 0, values);
861 #define sql_insert_late(table, values...) sql_insert_helper(table, 0, 0, 1, values);
862 #define sql_insert_cache(table, values...) sql_insert_helper(table, cache_db, 1, 0, values);
864 char *get_static_filter(struct symbol
*sym
);
866 void sql_insert_return_states(int return_id
, const char *return_ranges
,
867 int type
, int param
, const char *key
, const char *value
);
868 void sql_insert_caller_info(struct expression
*call
, int type
, int param
,
869 const char *key
, const char *value
);
870 void sql_insert_function_ptr(const char *fn
, const char *struct_name
);
871 void sql_insert_return_values(const char *return_values
);
872 void sql_insert_return_implies(int type
, int param
, const char *key
, const char *value
);
873 void sql_insert_function_type_size(const char *member
, const char *ranges
);
874 void sql_insert_function_type_info(int type
, const char *struct_type
, const char *member
, const char *value
);
875 void sql_insert_type_info(int type
, const char *member
, const char *value
);
876 void sql_insert_local_values(const char *name
, const char *value
);
877 void sql_insert_function_type_value(const char *type
, const char *value
);
878 void sql_insert_function_type(int param
, const char *value
);
879 void sql_insert_parameter_name(int param
, const char *value
);
880 void sql_insert_data_info(struct expression
*data
, int type
, const char *value
);
881 void sql_insert_data_info_var_sym(const char *var
, struct symbol
*sym
, int type
, const char *value
);
882 void sql_save_constraint(const char *con
);
883 void sql_save_constraint_required(const char *data
, int op
, const char *limit
);
884 void sql_copy_constraint_required(const char *new_limit
, const char *old_limit
);
885 void sql_insert_fn_ptr_data_link(const char *ptr
, const char *data
);
886 void sql_insert_fn_data_link(struct expression
*fn
, int type
, int param
, const char *key
, const char *value
);
887 void sql_insert_mtag_about(mtag_t tag
, const char *left_name
, const char *right_name
);
888 void insert_mtag_data(sval_t sval
, struct range_list
*rl
);
889 void sql_insert_mtag_map(mtag_t tag
, int offset
, mtag_t container
);
890 void sql_insert_mtag_alias(mtag_t orig
, mtag_t alias
);
891 int mtag_map_select_container(mtag_t tag
, int offset
, mtag_t
*container
);
892 int mtag_map_select_tag(mtag_t container
, int offset
, mtag_t
*tag
);
894 void sql_select_return_states(const char *cols
, struct expression
*call
,
895 int (*callback
)(void*, int, char**, char**), void *info
);
896 void sql_select_call_implies(const char *cols
, struct expression
*call
,
897 int (*callback
)(void*, int, char**, char**));
899 void open_smatch_db(char *db_file
);
902 int open_data_file(const char *filename
);
903 int open_schema_file(const char *schema
);
904 struct token
*get_tokens_file(const char *filename
);
907 extern char *option_debug_check
;
908 extern char *option_project_str
;
909 extern char *bin_dir
;
910 extern char *data_dir
;
911 extern int option_no_data
;
912 extern int option_full_path
;
913 extern int option_param_mapper
;
914 extern int option_call_tree
;
915 extern int num_checks
;
923 extern enum project_type option_project
;
924 const char *check_name(unsigned short id
);
925 int id_from_name(const char *name
);
928 /* smatch_buf_size.c */
929 int get_array_size(struct expression
*expr
);
930 int get_array_size_bytes(struct expression
*expr
);
931 int get_array_size_bytes_min(struct expression
*expr
);
932 int get_array_size_bytes_max(struct expression
*expr
);
933 struct range_list
*get_array_size_bytes_rl(struct expression
*expr
);
934 int get_real_array_size(struct expression
*expr
);
935 int last_member_is_resizable(struct symbol
*type
);
936 /* smatch_strlen.c */
937 int get_implied_strlen(struct expression
*expr
, struct range_list
**rl
);
938 int get_size_from_strlen(struct expression
*expr
);
940 /* smatch_capped.c */
941 int is_capped(struct expression
*expr
);
942 int is_capped_var_sym(const char *name
, struct symbol
*sym
);
944 /* check_user_data.c */
945 int is_user_macro(struct expression
*expr
);
946 int is_user_data(struct expression
*expr
);
947 int is_capped_user_data(struct expression
*expr
);
948 int implied_user_data(struct expression
*expr
, struct range_list
**rl
);
949 struct stree
*get_user_stree(void);
950 int get_user_rl(struct expression
*expr
, struct range_list
**rl
);
951 int get_user_rl_spammy(struct expression
*expr
, struct range_list
**rl
);
952 int is_user_rl(struct expression
*expr
);
953 int get_user_rl_var_sym(const char *name
, struct symbol
*sym
, struct range_list
**rl
);
955 /* check_locking.c */
956 void print_held_locks();
958 /* check_assigned_expr.c */
959 struct expression
*get_assigned_expr(struct expression
*expr
);
960 struct expression
*get_assigned_expr_name_sym(const char *name
, struct symbol
*sym
);
961 /* smatch_return_to_param.c */
962 void __add_return_to_param_mapping(struct expression
*assign
, const char *return_string
);
963 char *map_call_to_param_name_sym(struct expression
*expr
, struct symbol
**sym
);
965 /* smatch_comparison.c */
966 struct compare_data
{
967 /* The ->left and ->right expression pointers might be NULL (I'm lazy) */
968 struct expression
*left
;
969 const char *left_var
;
970 struct var_sym_list
*left_vsl
;
972 struct expression
*right
;
973 const char *right_var
;
974 struct var_sym_list
*right_vsl
;
976 DECLARE_ALLOCATOR(compare_data
);
977 struct smatch_state
*alloc_compare_state(
978 struct expression
*left
,
979 const char *left_var
, struct var_sym_list
*left_vsl
,
981 struct expression
*right
,
982 const char *right_var
, struct var_sym_list
*right_vsl
);
983 int filter_comparison(int orig
, int op
);
984 int merge_comparisons(int one
, int two
);
985 int combine_comparisons(int left_compare
, int right_compare
);
986 int state_to_comparison(struct smatch_state
*state
);
987 struct smatch_state
*merge_compare_states(struct smatch_state
*s1
, struct smatch_state
*s2
);
988 int get_comparison(struct expression
*left
, struct expression
*right
);
989 int get_comparison_strings(const char *one
, const char *two
);
990 int possible_comparison(struct expression
*a
, int comparison
, struct expression
*b
);
991 struct state_list
*get_all_comparisons(struct expression
*expr
);
992 struct state_list
*get_all_possible_equal_comparisons(struct expression
*expr
);
993 void __add_return_comparison(struct expression
*call
, const char *range
);
994 void __add_comparison_info(struct expression
*expr
, struct expression
*call
, const char *range
);
995 char *get_printed_param_name(struct expression
*call
, const char *param_name
, struct symbol
*param_sym
);
996 char *name_sym_to_param_comparison(const char *name
, struct symbol
*sym
);
997 char *expr_equal_to_param(struct expression
*expr
, int ignore
);
998 char *expr_lte_to_param(struct expression
*expr
, int ignore
);
999 char *expr_param_comparison(struct expression
*expr
, int ignore
);
1000 int flip_comparison(int op
);
1001 int negate_comparison(int op
);
1002 int remove_unsigned_from_comparison(int op
);
1003 int param_compare_limit_is_impossible(struct expression
*expr
, int left_param
, char *left_key
, char *value
);
1004 void filter_by_comparison(struct range_list
**rl
, int comparison
, struct range_list
*right
);
1005 struct sm_state
*comparison_implication_hook(struct expression
*expr
,
1006 struct state_list
**true_stack
,
1007 struct state_list
**false_stack
);
1008 void __compare_param_limit_hook(struct expression
*left_expr
, struct expression
*right_expr
,
1009 const char *state_name
,
1010 struct smatch_state
*true_state
, struct smatch_state
*false_state
);
1011 int impossibly_high_comparison(struct expression
*expr
);
1014 sval_t
*sval_alloc(sval_t sval
);
1015 sval_t
*sval_alloc_permanent(sval_t sval
);
1016 sval_t
sval_blank(struct expression
*expr
);
1017 sval_t
sval_type_val(struct symbol
*type
, long long val
);
1018 sval_t
sval_from_val(struct expression
*expr
, long long val
);
1019 int sval_is_ptr(sval_t sval
);
1020 int sval_unsigned(sval_t sval
);
1021 int sval_signed(sval_t sval
);
1022 int sval_bits(sval_t sval
);
1023 int sval_bits_used(sval_t sval
);
1024 int sval_is_negative(sval_t sval
);
1025 int sval_is_positive(sval_t sval
);
1026 int sval_is_min(sval_t sval
);
1027 int sval_is_max(sval_t sval
);
1028 int sval_is_a_min(sval_t sval
);
1029 int sval_is_a_max(sval_t sval
);
1030 int sval_is_negative_min(sval_t sval
);
1031 int sval_cmp_t(struct symbol
*type
, sval_t one
, sval_t two
);
1032 int sval_cmp_val(sval_t one
, long long val
);
1033 sval_t
sval_min(sval_t one
, sval_t two
);
1034 sval_t
sval_max(sval_t one
, sval_t two
);
1035 int sval_too_low(struct symbol
*type
, sval_t sval
);
1036 int sval_too_high(struct symbol
*type
, sval_t sval
);
1037 int sval_fits(struct symbol
*type
, sval_t sval
);
1038 sval_t
sval_cast(struct symbol
*type
, sval_t sval
);
1039 sval_t
sval_preop(sval_t sval
, int op
);
1040 sval_t
sval_binop(sval_t left
, int op
, sval_t right
);
1041 int sval_binop_overflows(sval_t left
, int op
, sval_t right
);
1042 unsigned long long fls_mask(unsigned long long uvalue
);
1043 unsigned long long sval_fls_mask(sval_t sval
);
1044 const char *sval_to_str(sval_t sval
);
1045 const char *sval_to_numstr(sval_t sval
);
1046 sval_t
ll_to_sval(long long val
);
1048 /* smatch_string_list.c */
1049 int list_has_string(struct string_list
*str_list
, const char *str
);
1050 void insert_string(struct string_list
**str_list
, const char *str
);
1051 struct string_list
*clone_str_list(struct string_list
*orig
);
1052 struct string_list
*combine_string_lists(struct string_list
*one
, struct string_list
*two
);
1054 /* smatch_start_states.c */
1055 struct stree
*get_start_states(void);
1057 /* smatch_recurse.c */
1058 int has_symbol(struct expression
*expr
, struct symbol
*sym
);
1059 int has_variable(struct expression
*expr
, struct expression
*var
);
1060 int has_inc_dec(struct expression
*expr
);
1062 /* smatch_stored_conditions.c */
1063 struct smatch_state
*get_stored_condition(struct expression
*expr
);
1064 struct expression_list
*get_conditions(struct expression
*expr
);
1065 struct sm_state
*stored_condition_implication_hook(struct expression
*expr
,
1066 struct state_list
**true_stack
,
1067 struct state_list
**false_stack
);
1069 /* check_string_len.c */
1070 int get_formatted_string_size(struct expression
*call
, int arg
);
1072 /* smatch_param_set.c */
1073 int param_was_set(struct expression
*expr
);
1074 int param_was_set_var_sym(const char *name
, struct symbol
*sym
);
1075 /* smatch_param_filter.c */
1076 int param_has_filter_data(struct sm_state
*sm
);
1078 /* smatch_links.c */
1079 void set_up_link_functions(int id
, int linkid
);
1080 struct smatch_state
*merge_link_states(struct smatch_state
*s1
, struct smatch_state
*s2
);
1081 void store_link(int link_id
, const char *name
, struct symbol
*sym
, const char *link_name
, struct symbol
*link_sym
);
1083 /* smatch_auto_copy.c */
1084 void set_auto_copy(int owner
);
1086 /* check_buf_comparison */
1087 struct expression
*get_size_variable(struct expression
*buf
);
1088 struct expression
*get_array_variable(struct expression
*size
);
1090 /* smatch_untracked_param.c */
1091 void mark_untracked(struct expression
*expr
, int param
, const char *key
, const char *value
);
1092 void add_untracked_param_hook(void (func
)(struct expression
*call
, int param
));
1093 void mark_all_params_untracked(int return_id
, char *return_ranges
, struct expression
*expr
);
1095 /* smatch_strings.c */
1096 struct state_list
*get_strings(struct expression
*expr
);
1097 struct expression
*fake_string_from_mtag(mtag_t tag
);
1099 /* smatch_estate.c */
1100 int estate_get_single_value(struct smatch_state
*state
, sval_t
*sval
);
1102 /* smatch_address.c */
1103 int get_address_rl(struct expression
*expr
, struct range_list
**rl
);
1104 int get_member_offset(struct symbol
*type
, const char *member_name
);
1105 int get_member_offset_from_deref(struct expression
*expr
);
1107 /* for now this is in smatch_used_parameter.c */
1108 void __get_state_hook(int owner
, const char *name
, struct symbol
*sym
);
1110 /* smatch_buf_comparison.c */
1111 int db_var_is_array_limit(struct expression
*array
, const char *name
, struct var_sym_list
*vsl
);
1113 struct stree
*get_all_return_states(void);
1114 struct stree_stack
*get_all_return_strees(void);
1115 int on_atomic_dec_path(void);
1116 int was_inced(const char *name
, struct symbol
*sym
);
1118 /* smatch_constraints.c */
1119 char *get_constraint_str(struct expression
*expr
);
1120 struct constraint_list
*get_constraints(struct expression
*expr
);
1121 char *unmet_constraint(struct expression
*data
, struct expression
*offset
);
1122 char *get_required_constraint(const char *data_str
);
1124 /* smatch_container_of.c */
1125 int get_param_from_container_of(struct expression
*expr
);
1126 int get_offset_from_container_of(struct expression
*expr
);
1129 int get_string_mtag(struct expression
*expr
, mtag_t
*tag
);
1130 int get_toplevel_mtag(struct symbol
*sym
, mtag_t
*tag
);
1131 int get_mtag(struct expression
*expr
, mtag_t
*tag
);
1132 int get_mtag_offset(struct expression
*expr
, mtag_t
*tag
, int *offset
);
1133 int create_mtag_alias(mtag_t tag
, struct expression
*expr
, mtag_t
*new);
1134 int expr_to_mtag_offset(struct expression
*expr
, mtag_t
*tag
, int *offset
);
1135 void update_mtag_data(struct expression
*expr
);
1136 int get_mtag_sval(struct expression
*expr
, sval_t
*sval
);
1137 int get_mtag_addr_sval(struct expression
*expr
, sval_t
*sval
);
1139 /* smatch_mem_tracker.c */
1140 extern int option_mem
;
1141 unsigned long get_max_memory(void);
1143 /* check_is_nospec.c */
1144 bool is_nospec(struct expression
*expr
);
1146 static inline int type_bits(struct symbol
*type
)
1150 if (type
->type
== SYM_PTR
) /* Sparse doesn't set this for &pointers */
1151 return bits_in_pointer
;
1152 if (type
->type
== SYM_ARRAY
)
1153 return bits_in_pointer
;
1154 if (!type
->examined
)
1155 examine_symbol_type(type
);
1156 return type
->bit_size
;
1159 static inline bool type_is_ptr(struct symbol
*type
)
1161 return type
&& (type
->type
== SYM_PTR
|| type
->type
== SYM_ARRAY
);
1164 static inline int type_unsigned(struct symbol
*base_type
)
1168 if (base_type
->ctype
.modifiers
& MOD_UNSIGNED
)
1173 static inline int type_positive_bits(struct symbol
*type
)
1177 if (type
->type
== SYM_ARRAY
)
1178 return bits_in_pointer
- 1;
1179 if (type_unsigned(type
))
1180 return type_bits(type
);
1181 return type_bits(type
) - 1;
1184 static inline int sval_positive_bits(sval_t sval
)
1186 return type_positive_bits(sval
.type
);
1190 * Returns -1 if one is smaller, 0 if they are the same and 1 if two is larger.
1192 static inline int sval_cmp(sval_t one
, sval_t two
)
1194 struct symbol
*type
;
1197 if (sval_positive_bits(two
) > sval_positive_bits(one
))
1199 if (type_bits(type
) < 31)
1202 one
= sval_cast(type
, one
);
1203 two
= sval_cast(type
, two
);
1205 if (type_unsigned(type
)) {
1206 if (one
.uvalue
< two
.uvalue
)
1208 if (one
.uvalue
== two
.uvalue
)
1212 /* fix me handle type promotion and unsigned values */
1213 if (one
.value
< two
.value
)
1215 if (one
.value
== two
.value
)
1220 #endif /* !SMATCH_H_ */