4 * Copyright (C) 2006 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
20 #include "expression.h"
26 unsigned long long uvalue
;
34 #define STATE(_x) static struct smatch_state _x = { .name = #_x }
35 extern struct smatch_state undefined
;
36 extern struct smatch_state merged
;
37 extern struct smatch_state true_state
;
38 extern struct smatch_state false_state
;
39 DECLARE_ALLOCATOR(smatch_state
);
41 static inline void *INT_PTR(int i
)
43 return (void *)(long)i
;
46 static inline int PTR_INT(void *p
)
53 unsigned int merged
:1;
54 unsigned int implied
:1;
57 struct smatch_state
*state
;
59 struct state_list
*pool
;
60 struct sm_state
*left
;
61 struct sm_state
*right
;
62 unsigned int nr_children
;
63 struct state_list
*possible
;
70 DECLARE_ALLOCATOR(var_sym
);
71 DECLARE_PTR_LIST(var_sym_list
, struct var_sym
);
78 DECLARE_ALLOCATOR(tracker
);
79 DECLARE_PTR_LIST(tracker_list
, struct tracker
);
89 GLOBAL_ASSIGNMENT_HOOK
,
96 CALL_HOOK_AFTER_INLINE
,
98 MACRO_ASSIGNMENT_HOOK
,
122 void add_hook(void *func
, enum hook_type type
);
123 typedef struct smatch_state
*(merge_func_t
)(struct smatch_state
*s1
, struct smatch_state
*s2
);
124 typedef struct smatch_state
*(unmatched_func_t
)(struct sm_state
*state
);
125 void add_merge_hook(int client_id
, merge_func_t
*func
);
126 void add_unmatched_state_hook(int client_id
, unmatched_func_t
*func
);
127 typedef void (scope_hook
)(void *data
);
128 void add_scope_hook(scope_hook
*hook
, void *data
);
129 typedef void (func_hook
)(const char *fn
, struct expression
*expr
, void *data
);
130 typedef void (implication_hook
)(const char *fn
, struct expression
*call_expr
,
131 struct expression
*assign_expr
, void *data
);
132 typedef void (return_implies_hook
)(struct expression
*call_expr
,
133 int param
, char *key
, char *value
);
134 typedef int (implied_return_hook
)(struct expression
*call_expr
, void *info
, struct range_list
**rl
);
135 void add_function_hook(const char *look_for
, func_hook
*call_back
, void *data
);
137 void add_function_assign_hook(const char *look_for
, func_hook
*call_back
,
139 void add_implied_return_hook(const char *look_for
,
140 implied_return_hook
*call_back
,
142 void add_macro_assign_hook(const char *look_for
, func_hook
*call_back
,
144 void add_macro_assign_hook_extra(const char *look_for
, func_hook
*call_back
,
146 void return_implies_state(const char *look_for
, long long start
, long long end
,
147 implication_hook
*call_back
, void *info
);
148 void select_return_states_hook(int type
, return_implies_hook
*callback
);
149 void select_return_states_before(void (*fn
)(void));
150 void select_return_states_after(void (*fn
)(void));
151 int get_implied_return(struct expression
*expr
, struct range_list
**rl
);
153 typedef void (modification_hook
)(struct sm_state
*sm
, struct expression
*mod_expr
);
154 void add_modification_hook(int owner
, modification_hook
*call_back
);
155 void add_indirect_modification_hook(int owner
, modification_hook
*call_back
);
157 const char *get_filename(void);
158 const char *get_base_file(void);
159 char *get_function(void);
160 int get_lineno(void);
161 extern int final_pass
;
162 extern struct symbol
*cur_func_sym
;
163 extern int option_debug
;
164 extern int local_debug
;
166 extern FILE *sm_outfd
;
167 #define sm_printf(msg...) do { if (final_pass || option_debug) fprintf(sm_outfd, msg); } while (0)
169 static inline void sm_prefix(void)
171 sm_printf("%s:%d %s() ", get_filename(), get_lineno(), get_function());
174 static inline void print_implied_debug_msg();
176 #define sm_msg(msg...) \
178 print_implied_debug_msg(); \
179 if (!option_debug && !final_pass) \
186 #define local_debug(msg...) \
192 extern char *implied_debug_msg
;
193 static inline void print_implied_debug_msg()
195 static struct symbol
*last_printed
= NULL
;
197 if (!implied_debug_msg
)
199 if (last_printed
== cur_func_sym
)
201 last_printed
= cur_func_sym
;
202 sm_msg("%s", implied_debug_msg
);
205 #define sm_debug(msg...) do { if (option_debug) sm_printf(msg); } while (0)
207 #define sm_info(msg...) do { \
208 if (option_debug || (option_info && final_pass)) { \
210 sm_printf("info: "); \
216 struct smatch_state
*get_state(int owner
, const char *name
, struct symbol
*sym
);
217 struct smatch_state
*get_state_expr(int owner
, struct expression
*expr
);
218 struct state_list
*get_possible_states(int owner
, const char *name
,
220 struct state_list
*get_possible_states_expr(int owner
, struct expression
*expr
);
221 struct sm_state
*set_state(int owner
, const char *name
, struct symbol
*sym
,
222 struct smatch_state
*state
);
223 struct sm_state
*set_state_expr(int owner
, struct expression
*expr
,
224 struct smatch_state
*state
);
225 void delete_state(int owner
, const char *name
, struct symbol
*sym
);
226 void delete_state_expr(int owner
, struct expression
*expr
);
227 void set_true_false_states(int owner
, const char *name
, struct symbol
*sym
,
228 struct smatch_state
*true_state
,
229 struct smatch_state
*false_state
);
230 void set_true_false_states_expr(int owner
, struct expression
*expr
,
231 struct smatch_state
*true_state
,
232 struct smatch_state
*false_state
);
234 struct state_list
*get_all_states_slist(int owner
, struct state_list
*source
);
235 struct state_list
*get_all_states(int id
);
236 int is_reachable(void);
238 /* smatch_helper.c */
239 char *alloc_string(const char *str
);
240 void free_string(char *str
);
241 void append(char *dest
, const char *data
, int buff_len
);
242 void remove_parens(char *str
);
243 struct smatch_state
*alloc_state_num(int num
);
244 struct expression
*get_argument_from_call_expr(struct expression_list
*args
,
247 char *expr_to_var(struct expression
*expr
);
248 char *expr_to_str(struct expression
*expr
);
249 char *expr_to_str_sym(struct expression
*expr
,
250 struct symbol
**sym_ptr
);
251 char *expr_to_var_sym(struct expression
*expr
,
252 struct symbol
**sym_ptr
);
254 int sym_name_is(const char *name
, struct expression
*expr
);
255 int get_value(struct expression
*expr
, sval_t
*val
);
256 int get_implied_value(struct expression
*expr
, sval_t
*val
);
257 int get_implied_min(struct expression
*expr
, sval_t
*sval
);
258 int get_implied_max(struct expression
*expr
, sval_t
*val
);
259 int get_hard_max(struct expression
*expr
, sval_t
*sval
);
260 int get_fuzzy_min(struct expression
*expr
, sval_t
*min
);
261 int get_fuzzy_max(struct expression
*expr
, sval_t
*max
);
262 int get_absolute_min(struct expression
*expr
, sval_t
*sval
);
263 int get_absolute_max(struct expression
*expr
, sval_t
*sval
);
264 int parse_call_math(struct expression
*expr
, char *math
, sval_t
*val
);
265 int is_zero(struct expression
*expr
);
266 int known_condition_true(struct expression
*expr
);
267 int known_condition_false(struct expression
*expr
);
268 int implied_condition_true(struct expression
*expr
);
269 int implied_condition_false(struct expression
*expr
);
271 int is_array(struct expression
*expr
);
272 struct expression
*get_array_name(struct expression
*expr
);
273 struct expression
*get_array_offset(struct expression
*expr
);
274 const char *show_state(struct smatch_state
*state
);
275 struct statement
*get_expression_statement(struct expression
*expr
);
276 struct expression
*strip_parens(struct expression
*expr
);
277 struct expression
*strip_expr(struct expression
*expr
);
278 void scoped_state(int my_id
, const char *name
, struct symbol
*sym
);
279 int is_error_return(struct expression
*expr
);
280 int getting_address(void);
281 char *get_member_name(struct expression
*expr
);
282 char *get_fnptr_name(struct expression
*expr
);
283 int positions_eq(struct position pos1
, struct position pos2
);
284 struct statement
*get_current_statement(void);
285 int get_param_num_from_sym(struct symbol
*sym
);
286 int ms_since(struct timeval
*start
);
289 struct symbol
*get_real_base_type(struct symbol
*sym
);
290 int type_bits(struct symbol
*type
);
291 int type_positive_bits(struct symbol
*type
);
292 struct symbol
*get_pointer_type(struct expression
*expr
);
293 struct symbol
*get_type(struct expression
*expr
);
294 int type_unsigned(struct symbol
*base_type
);
295 int type_signed(struct symbol
*base_type
);
296 int expr_unsigned(struct expression
*expr
);
297 int returns_unsigned(struct symbol
*base_type
);
298 int is_pointer(struct expression
*expr
);
299 int returns_pointer(struct symbol
*base_type
);
300 sval_t
sval_type_max(struct symbol
*base_type
);
301 sval_t
sval_type_min(struct symbol
*base_type
);
302 int nr_bits(struct expression
*expr
);
303 int is_void_pointer(struct expression
*expr
);
304 int is_char_pointer(struct expression
*expr
);
305 int is_static(struct expression
*expr
);
306 int types_equiv(struct symbol
*one
, struct symbol
*two
);
308 const char *global_static();
309 struct symbol
*cur_func_return_type(void);
310 struct symbol
*get_arg_type(struct expression
*fn
, int arg
);
311 struct symbol
*get_member_type_from_key(struct expression
*expr
, char *key
);
313 /* smatch_ignore.c */
314 void add_ignore(int owner
, const char *name
, struct symbol
*sym
);
315 int is_ignored(int owner
, const char *name
, struct symbol
*sym
);
318 struct var_sym
*alloc_var_sym(const char *var
, struct symbol
*sym
);
319 struct var_sym_list
*expr_to_vsl(struct expression
*expr
);
320 void add_var_sym(struct var_sym_list
**list
, const char *var
, struct symbol
*sym
);
321 void add_var_sym_expr(struct var_sym_list
**list
, struct expression
*expr
);
322 void del_var_sym(struct var_sym_list
**list
, const char *var
, struct symbol
*sym
);
323 int in_var_sym_list(struct var_sym_list
*list
, const char *var
, struct symbol
*sym
);
324 struct var_sym_list
*clone_var_sym_list(struct var_sym_list
*from_vsl
);
325 void merge_var_sym_list(struct var_sym_list
**dest
, struct var_sym_list
*src
);
326 struct var_sym_list
*combine_var_sym_lists(struct var_sym_list
*one
, struct var_sym_list
*two
);
327 void free_var_sym_list(struct var_sym_list
**list
);
328 void free_var_syms_and_list(struct var_sym_list
**list
);
331 struct tracker
*alloc_tracker(int owner
, const char *name
, struct symbol
*sym
);
332 void add_tracker(struct tracker_list
**list
, int owner
, const char *name
,
334 void add_tracker_expr(struct tracker_list
**list
, int owner
, struct expression
*expr
);
335 void del_tracker(struct tracker_list
**list
, int owner
, const char *name
,
337 int in_tracker_list(struct tracker_list
*list
, int owner
, const char *name
,
339 void free_tracker_list(struct tracker_list
**list
);
340 void free_trackers_and_list(struct tracker_list
**list
);
342 /* smatch_conditions */
343 int in_condition(void);
347 void smatch (int argc
, char **argv
);
348 int inside_loop(void);
349 struct expression
*get_switch_expr(void);
350 int in_expression_statement(void);
351 void __process_post_op_stack(void);
352 void __split_expr(struct expression
*expr
);
353 void __split_stmt(struct statement
*stmt
);
354 extern int option_assume_loops
;
355 extern int option_known_conditions
;
356 extern int option_two_passes
;
357 extern int option_no_db
;
358 extern int option_file_output
;
359 extern int option_time
;
360 extern struct expression_list
*big_expression_stack
;
361 extern struct statement_list
*big_statement_stack
;
362 int inlinable(struct expression
*expr
);
363 extern int __inline_call
;
364 extern struct expression
*__inline_fn
;
365 extern int __in_pre_condition
;
366 extern int __bail_on_rest_of_function
;
368 /* smatch_conditions */
369 void __split_whole_condition(struct expression
*expr
);
370 void __handle_logic(struct expression
*expr
);
371 int is_condition(struct expression
*expr
);
372 int __handle_condition_assigns(struct expression
*expr
);
373 int __handle_select_assigns(struct expression
*expr
);
374 int __handle_expr_statement_assigns(struct expression
*expr
);
376 /* smatch_implied.c */
377 extern int option_debug_implied
;
378 extern int option_debug_related
;
379 extern int option_no_implied
;
380 struct range_list_stack
;
381 struct state_list
*__implied_case_slist(struct expression
*switch_expr
,
382 struct expression
*case_expr
,
383 struct range_list_stack
**remaining_cases
,
384 struct state_list
**raw_slist
);
385 struct range_list
*__get_implied_values(struct expression
*switch_expr
);
386 void overwrite_states_using_pool(struct sm_state
*sm
);
388 /* smatch_extras.c */
389 #define SMATCH_EXTRA 1 /* this is my_id from smatch extra set in smatch.c */
396 static const long long valid_ptr_max
= LONG_MAX
;
397 static const long long valid_ptr_min
= 4096;
398 static const sval_t valid_ptr_max_sval
= {
402 static const sval_t valid_ptr_min_sval
= {
407 /* smatch_absolute.c */
408 int get_absolute_min_helper(struct expression
*expr
, sval_t
*sval
);
409 int get_absolute_max_helper(struct expression
*expr
, sval_t
*sval
);
411 /* smatch_local_values.c */
412 int get_local_rl(struct expression
*expr
, struct range_list
**rl
);
413 int get_local_max_helper(struct expression
*expr
, sval_t
*sval
);
414 int get_local_min_helper(struct expression
*expr
, sval_t
*sval
);
416 /* smatch_states.c */
417 void __push_fake_cur_slist();
418 struct state_list
*__pop_fake_cur_slist();
419 void __free_fake_cur_slist();
420 void __set_fake_cur_slist_fast(struct state_list
*slist
);
421 void __pop_fake_cur_slist_fast(void);
422 void __merge_slist_into_cur(struct state_list
*slist
);
424 int unreachable(void);
425 void __set_sm(struct sm_state
*sm
);
426 struct state_list
*__get_cur_slist(void);
427 void __set_true_false_sm(struct sm_state
*true_state
,
428 struct sm_state
*false_state
);
429 void nullify_path(void);
430 void __match_nullify_path_hook(const char *fn
, struct expression
*expr
,
432 void __unnullify_path(void);
433 int __path_is_null(void);
434 void save_all_states(void);
435 void nullify_all_states(void);
436 void restore_all_states(void);
437 void clear_all_states(void);
439 struct sm_state
*get_sm_state(int owner
, const char *name
,
441 struct sm_state
*get_sm_state_expr(int owner
, struct expression
*expr
);
442 void __push_true_states(void);
443 void __use_false_states(void);
444 void __discard_false_states(void);
445 void __merge_false_states(void);
446 void __merge_true_states(void);
448 void __negate_cond_stacks(void);
449 void __use_pre_cond_states(void);
450 void __use_cond_true_states(void);
451 void __use_cond_false_states(void);
452 void __push_cond_stacks(void);
453 struct state_list
*__copy_cond_true_states(void);
454 struct state_list
*__copy_cond_false_states(void);
455 struct state_list
*__pop_cond_true_stack(void);
456 struct state_list
*__pop_cond_false_stack(void);
457 void __and_cond_states(void);
458 void __or_cond_states(void);
459 void __save_pre_cond_states(void);
460 void __discard_pre_cond_states(void);
461 void __use_cond_states(void);
462 extern struct state_list
*__last_base_slist
;
464 void __warn_on_silly_pre_loops(void);
466 void __push_continues(void);
467 void __discard_continues(void);
468 void __process_continues(void);
469 void __merge_continues(void);
471 void __push_breaks(void);
472 void __process_breaks(void);
473 int __has_breaks(void);
474 void __merge_breaks(void);
475 void __use_breaks(void);
477 void __save_switch_states(struct expression
*switch_expr
);
478 void __discard_switches(void);
479 void __merge_switches(struct expression
*switch_expr
, struct expression
*case_expr
);
480 void __push_default(void);
481 void __set_default(void);
482 int __pop_default(void);
484 void __push_conditions(void);
485 void __discard_conditions(void);
487 void __save_gotos(const char *name
);
488 void __merge_gotos(const char *name
);
490 void __print_cur_slist(void);
493 void __pass_to_client(void *data
, enum hook_type type
);
494 void __pass_to_client_no_data(enum hook_type type
);
495 void __pass_case_to_client(struct expression
*switch_expr
,
496 struct expression
*case_expr
);
497 int __has_merge_function(int client_id
);
498 struct smatch_state
*__client_merge_function(int owner
,
499 struct smatch_state
*s1
,
500 struct smatch_state
*s2
);
501 struct smatch_state
*__client_unmatched_state_function(struct sm_state
*sm
);
502 void __push_scope_hooks(void);
503 void __call_scope_hooks(void);
505 /* smatch_function_hooks.c */
506 void create_function_hook_hash(void);
507 void __match_initializer_call(struct symbol
*sym
);
521 ABSOLUTE_LIMITS
= 10,
528 void select_caller_info_hook(void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
), int type
);
529 void add_member_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct smatch_state
*state
));
530 void add_split_return_callback(void (*fn
)(int return_id
, char *return_ranges
, struct expression
*returned_expr
));
531 void add_returned_member_callback(int owner
, void (*callback
)(int return_id
, char *return_ranges
, char *printed_name
, struct smatch_state
*state
));
532 void select_call_implies_hook(int type
, void (*callback
)(struct expression
*arg
, char *value
));
533 struct range_list
*db_return_vals(struct expression
*expr
);
534 char *return_state_to_var_sym(struct expression
*expr
, int param
, char *key
, struct symbol
**sym
);
535 char *get_variable_from_key(struct expression
*arg
, char *key
, struct symbol
**sym
);
536 const char *get_param_name(struct sm_state
*sm
);
538 #define run_sql(call_back, sql...) \
540 char sql_txt[1024]; \
543 snprintf(sql_txt, 1024, sql); \
544 sm_debug("%s\n", sql_txt); \
545 sql_exec(call_back, sql_txt); \
548 /* like run_sql() but for the in-memory database */
549 #define mem_sql(call_back, sql...) \
551 char sql_txt[1024]; \
553 snprintf(sql_txt, sizeof(sql_txt), sql); \
554 sm_debug("in-mem: %s\n", sql_txt); \
555 sql_mem_exec(call_back, sql_txt); \
558 void sql_insert_return_states(int return_id
, const char *return_ranges
,
559 int type
, int param
, const char *key
, const char *value
);
560 void sql_insert_caller_info(struct expression
*call
, int type
, int param
,
561 const char *key
, const char *value
);
562 void sql_insert_function_ptr(const char *fn
, const char *struct_name
);
563 void sql_insert_return_values(const char *return_values
);
564 void sql_insert_call_implies(int type
, int param
, int value
);
565 void sql_insert_type_size(const char *member
, int size
);
566 void sql_insert_local_values(const char *name
, const char *value
);
568 void sql_select_return_states(const char *cols
, struct expression
*call
,
569 int (*callback
)(void*, int, char**, char**));
570 void sql_select_caller_info(const char *cols
, struct symbol
*sym
,
571 int (*callback
)(void*, int, char**, char**));
572 void sql_select_call_implies(const char *cols
, struct expression
*call
,
573 int (*callback
)(void*, int, char**, char**));
575 void sql_exec(int (*callback
)(void*, int, char**, char**), const char *sql
);
576 void sql_mem_exec(int (*callback
)(void*, int, char**, char**), const char *sql
);
578 void open_smatch_db(void);
581 int open_data_file(const char *filename
);
582 struct token
*get_tokens_file(const char *filename
);
585 extern char *option_debug_check
;
586 extern char *option_project_str
;
587 extern char *data_dir
;
588 extern int option_no_data
;
589 extern int option_spammy
;
590 extern int option_full_path
;
591 extern int option_param_mapper
;
592 extern int option_info
;
593 extern int option_call_tree
;
594 extern int num_checks
;
601 extern enum project_type option_project
;
602 const char *check_name(unsigned short id
);
605 /* smatch_buf_size.c */
606 int get_array_size(struct expression
*expr
);
607 int get_array_size_bytes(struct expression
*expr
);
608 int get_array_size_bytes_min(struct expression
*expr
);
609 int get_real_array_size(struct expression
*expr
);
611 /* smatch_capped.c */
612 int is_capped(struct expression
*expr
);
613 int is_capped_var_sym(const char *name
, struct symbol
*sym
);
615 /* check_user_data.c */
616 int is_user_macro(struct expression
*expr
);
617 int is_user_data(struct expression
*expr
);
618 int is_capped_user_data(struct expression
*expr
);
620 /* check_locking.c */
621 void print_held_locks();
623 /* check_assigned_expr.c */
624 struct expression
*get_assigned_expr(struct expression
*expr
);
626 /* smatch_comparison.c */
627 int get_comparison(struct expression
*left
, struct expression
*right
);
628 void __add_comparison_info(struct expression
*expr
, struct expression
*call
, const char *range
);
629 char *expr_equal_to_param(struct expression
*expr
);
630 char *expr_lte_to_param(struct expression
*expr
);
633 sval_t
*sval_alloc(sval_t sval
);
634 sval_t
*sval_alloc_permanent(sval_t sval
);
635 sval_t
sval_blank(struct expression
*expr
);
636 sval_t
sval_type_val(struct symbol
*type
, long long val
);
637 sval_t
sval_from_val(struct expression
*expr
, long long val
);
638 int sval_unsigned(sval_t sval
);
639 int sval_signed(sval_t sval
);
640 int sval_bits(sval_t sval
);
641 int sval_positive_bits(sval_t sval
);
642 int sval_bits_used(sval_t sval
);
643 int sval_is_negative(sval_t sval
);
644 int sval_is_positive(sval_t sval
);
645 int sval_is_min(sval_t sval
);
646 int sval_is_max(sval_t sval
);
647 int sval_is_a_min(sval_t sval
);
648 int sval_is_a_max(sval_t sval
);
649 int sval_is_negative_min(sval_t sval
);
650 int sval_cmp(sval_t one
, sval_t two
);
651 int sval_cmp_t(struct symbol
*type
, sval_t one
, sval_t two
);
652 int sval_cmp_val(sval_t one
, long long val
);
653 sval_t
sval_min(sval_t one
, sval_t two
);
654 sval_t
sval_max(sval_t one
, sval_t two
);
655 int sval_too_low(struct symbol
*type
, sval_t sval
);
656 int sval_too_high(struct symbol
*type
, sval_t sval
);
657 int sval_fits(struct symbol
*type
, sval_t sval
);
658 sval_t
sval_cast(struct symbol
*type
, sval_t sval
);
659 sval_t
sval_preop(sval_t sval
, int op
);
660 sval_t
sval_binop(sval_t left
, int op
, sval_t right
);
661 int sval_binop_overflows(sval_t left
, int op
, sval_t right
);
662 const char *sval_to_str(sval_t sval
);
663 const char *sval_to_numstr(sval_t sval
);
664 sval_t
ll_to_sval(long long val
);
666 /* smatch_string_list.c */
667 int list_has_string(struct string_list
*str_list
, char *str
);
668 void insert_string(struct string_list
**str_list
, char *str
);
669 struct string_list
*clone_str_list(struct string_list
*orig
);
670 struct string_list
*combine_string_lists(struct string_list
*one
, struct string_list
*two
);
672 /* smatch_clear_buffer.c */
673 int is_uninitialized(struct expression
*expr
);
674 int has_uninitialized_members(struct expression
*expr
);
676 /* smatch_start_states.c */
677 struct state_list
*get_start_states(void);
679 /* smatch_recurse.c */
680 int has_symbol(struct expression
*expr
, struct symbol
*sym
);
681 int has_variable(struct expression
*expr
, struct expression
*var
);
683 #endif /* !SMATCH_H_ */