4 * Copyright (C) 2006 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
19 #include "expression.h"
25 unsigned long long uvalue
;
33 #define STATE(_x) static struct smatch_state _x = { .name = #_x }
34 extern struct smatch_state undefined
;
35 extern struct smatch_state merged
;
36 extern struct smatch_state true_state
;
37 extern struct smatch_state false_state
;
38 DECLARE_ALLOCATOR(smatch_state
);
40 static inline void *INT_PTR(int i
)
42 return (void *)(long)i
;
45 static inline int PTR_INT(void *p
)
52 unsigned int merged
:1;
53 unsigned int implied
:1;
56 struct smatch_state
*state
;
58 struct state_list
*pool
;
59 struct sm_state
*left
;
60 struct sm_state
*right
;
61 unsigned int nr_children
;
62 struct state_list
*possible
;
70 DECLARE_ALLOCATOR(tracker
);
71 DECLARE_PTR_LIST(tracker_list
, struct tracker
);
81 GLOBAL_ASSIGNMENT_HOOK
,
88 CALL_HOOK_AFTER_INLINE
,
90 MACRO_ASSIGNMENT_HOOK
,
113 void add_hook(void *func
, enum hook_type type
);
114 typedef struct smatch_state
*(merge_func_t
)(struct smatch_state
*s1
, struct smatch_state
*s2
);
115 typedef struct smatch_state
*(unmatched_func_t
)(struct sm_state
*state
);
116 void add_merge_hook(int client_id
, merge_func_t
*func
);
117 void add_unmatched_state_hook(int client_id
, unmatched_func_t
*func
);
118 typedef void (scope_hook
)(void *data
);
119 void add_scope_hook(scope_hook
*hook
, void *data
);
120 typedef void (func_hook
)(const char *fn
, struct expression
*expr
, void *data
);
121 typedef void (implication_hook
)(const char *fn
, struct expression
*call_expr
,
122 struct expression
*assign_expr
, void *data
);
123 typedef void (return_implies_hook
)(struct expression
*call_expr
,
124 int param
, char *key
, char *value
);
125 typedef int (implied_return_hook
)(struct expression
*call_expr
, void *info
, struct range_list
**rl
);
126 void add_function_hook(const char *look_for
, func_hook
*call_back
, void *data
);
128 void add_function_assign_hook(const char *look_for
, func_hook
*call_back
,
130 void add_implied_return_hook(const char *look_for
,
131 implied_return_hook
*call_back
,
133 void add_macro_assign_hook(const char *look_for
, func_hook
*call_back
,
135 void add_macro_assign_hook_extra(const char *look_for
, func_hook
*call_back
,
137 void return_implies_state(const char *look_for
, long long start
, long long end
,
138 implication_hook
*call_back
, void *info
);
139 void add_db_return_states_callback(int type
, return_implies_hook
*callback
);
140 void add_db_return_states_before(void (*fn
)(void));
141 void add_db_return_states_after(void (*fn
)(void));
142 int get_implied_return(struct expression
*expr
, struct range_list
**rl
);
144 typedef void (modification_hook
)(struct sm_state
*sm
);
145 void add_modification_hook(int owner
, modification_hook
*call_back
);
146 void add_indirect_modification_hook(int owner
, modification_hook
*call_back
);
148 const char *get_filename(void);
149 const char *get_base_file(void);
150 char *get_function(void);
151 int get_lineno(void);
152 extern int final_pass
;
153 extern struct symbol
*cur_func_sym
;
154 extern int option_debug
;
155 extern int local_debug
;
157 extern FILE *sm_outfd
;
158 #define sm_printf(msg...) do { if (final_pass || option_debug) fprintf(sm_outfd, msg); } while (0)
160 static inline void sm_prefix(void)
162 sm_printf("%s:%d %s() ", get_filename(), get_lineno(), get_function());
165 static inline void print_implied_debug_msg();
167 #define sm_msg(msg...) \
169 print_implied_debug_msg(); \
170 if (!option_debug && !final_pass) \
177 #define local_debug(msg...) \
183 extern char *implied_debug_msg
;
184 static inline void print_implied_debug_msg()
186 static struct symbol
*last_printed
= NULL
;
188 if (!implied_debug_msg
)
190 if (last_printed
== cur_func_sym
)
192 last_printed
= cur_func_sym
;
193 sm_msg("%s", implied_debug_msg
);
196 #define sm_debug(msg...) do { if (option_debug) sm_printf(msg); } while (0)
198 #define sm_info(msg...) do { \
199 if (option_debug || (option_info && final_pass)) { \
201 sm_printf("info: "); \
207 struct smatch_state
*get_state(int owner
, const char *name
, struct symbol
*sym
);
208 struct smatch_state
*get_state_expr(int owner
, struct expression
*expr
);
209 struct state_list
*get_possible_states(int owner
, const char *name
,
211 struct state_list
*get_possible_states_expr(int owner
, struct expression
*expr
);
212 struct sm_state
*set_state(int owner
, const char *name
, struct symbol
*sym
,
213 struct smatch_state
*state
);
214 struct sm_state
*set_state_expr(int owner
, struct expression
*expr
,
215 struct smatch_state
*state
);
216 void delete_state(int owner
, const char *name
, struct symbol
*sym
);
217 void delete_state_expr(int owner
, struct expression
*expr
);
218 void set_true_false_states(int owner
, const char *name
, struct symbol
*sym
,
219 struct smatch_state
*true_state
,
220 struct smatch_state
*false_state
);
221 void set_true_false_states_expr(int owner
, struct expression
*expr
,
222 struct smatch_state
*true_state
,
223 struct smatch_state
*false_state
);
225 struct state_list
*get_all_states_slist(int owner
, struct state_list
*source
);
226 struct state_list
*get_all_states(int id
);
227 int is_reachable(void);
229 /* smatch_helper.c */
230 char *alloc_string(const char *str
);
231 void free_string(char *str
);
232 void remove_parens(char *str
);
233 struct smatch_state
*alloc_state_num(int num
);
234 struct expression
*get_argument_from_call_expr(struct expression_list
*args
,
237 char *expr_to_var(struct expression
*expr
);
238 char *expr_to_str(struct expression
*expr
);
239 char *expr_to_str_sym(struct expression
*expr
,
240 struct symbol
**sym_ptr
);
241 char *expr_to_var_sym(struct expression
*expr
,
242 struct symbol
**sym_ptr
);
244 int sym_name_is(const char *name
, struct expression
*expr
);
245 int get_value(struct expression
*expr
, sval_t
*val
);
246 int get_implied_value(struct expression
*expr
, sval_t
*val
);
247 int get_implied_min(struct expression
*expr
, sval_t
*sval
);
248 int get_implied_max(struct expression
*expr
, sval_t
*val
);
249 int get_hard_max(struct expression
*expr
, sval_t
*sval
);
250 int get_fuzzy_min(struct expression
*expr
, sval_t
*min
);
251 int get_fuzzy_max(struct expression
*expr
, sval_t
*max
);
252 int get_absolute_min(struct expression
*expr
, sval_t
*sval
);
253 int get_absolute_max(struct expression
*expr
, sval_t
*sval
);
254 int parse_call_math(struct expression
*expr
, char *math
, sval_t
*val
);
255 int is_zero(struct expression
*expr
);
256 int known_condition_true(struct expression
*expr
);
257 int known_condition_false(struct expression
*expr
);
258 int implied_condition_true(struct expression
*expr
);
259 int implied_condition_false(struct expression
*expr
);
261 int is_array(struct expression
*expr
);
262 struct expression
*get_array_name(struct expression
*expr
);
263 struct expression
*get_array_offset(struct expression
*expr
);
264 const char *show_state(struct smatch_state
*state
);
265 struct statement
*get_expression_statement(struct expression
*expr
);
266 struct expression
*strip_parens(struct expression
*expr
);
267 struct expression
*strip_expr(struct expression
*expr
);
268 void scoped_state(int my_id
, const char *name
, struct symbol
*sym
);
269 int is_error_return(struct expression
*expr
);
270 int getting_address(void);
271 char *get_member_name(struct expression
*expr
);
272 char *get_fnptr_name(struct expression
*expr
);
273 int positions_eq(struct position pos1
, struct position pos2
);
274 struct statement
*get_current_statement(void);
275 int get_param_num_from_sym(struct symbol
*sym
);
278 struct symbol
*get_real_base_type(struct symbol
*sym
);
279 int type_bits(struct symbol
*type
);
280 int type_positive_bits(struct symbol
*type
);
281 struct symbol
*get_pointer_type(struct expression
*expr
);
282 struct symbol
*get_type(struct expression
*expr
);
283 int type_unsigned(struct symbol
*base_type
);
284 int type_signed(struct symbol
*base_type
);
285 int expr_unsigned(struct expression
*expr
);
286 int returns_unsigned(struct symbol
*base_type
);
287 int is_pointer(struct expression
*expr
);
288 int returns_pointer(struct symbol
*base_type
);
289 sval_t
sval_type_max(struct symbol
*base_type
);
290 sval_t
sval_type_min(struct symbol
*base_type
);
291 int nr_bits(struct expression
*expr
);
292 int is_void_pointer(struct expression
*expr
);
293 int is_char_pointer(struct expression
*expr
);
294 int is_static(struct expression
*expr
);
295 int types_equiv(struct symbol
*one
, struct symbol
*two
);
297 const char *global_static();
298 struct symbol
*cur_func_return_type(void);
299 struct symbol
*get_arg_type(struct expression
*fn
, int arg
);
300 struct symbol
*get_member_type_from_key(struct expression
*expr
, char *key
);
302 /* smatch_ignore.c */
303 void add_ignore(int owner
, const char *name
, struct symbol
*sym
);
304 int is_ignored(int owner
, const char *name
, struct symbol
*sym
);
307 struct tracker
*alloc_tracker(int owner
, const char *name
, struct symbol
*sym
);
308 void add_tracker(struct tracker_list
**list
, int owner
, const char *name
,
310 void add_tracker_expr(struct tracker_list
**list
, int owner
, struct expression
*expr
);
311 void del_tracker(struct tracker_list
**list
, int owner
, const char *name
,
313 int in_tracker_list(struct tracker_list
*list
, int owner
, const char *name
,
315 void free_tracker_list(struct tracker_list
**list
);
316 void free_trackers_and_list(struct tracker_list
**list
);
318 /* smatch_conditions */
319 int in_condition(void);
323 void smatch (int argc
, char **argv
);
324 int inside_loop(void);
325 struct expression
*get_switch_expr(void);
326 int in_expression_statement(void);
327 void __process_post_op_stack(void);
328 void __split_expr(struct expression
*expr
);
329 void __split_stmt(struct statement
*stmt
);
330 extern int option_assume_loops
;
331 extern int option_known_conditions
;
332 extern int option_two_passes
;
333 extern int option_no_db
;
334 extern int option_file_output
;
335 extern struct expression_list
*big_expression_stack
;
336 extern struct statement_list
*big_statement_stack
;
337 int inlinable(struct expression
*expr
);
338 extern int __inline_call
;
339 extern struct expression
*__inline_fn
;
340 extern int __in_pre_condition
;
341 extern int __bail_on_rest_of_function
;
343 /* smatch_conditions */
344 void __split_whole_condition(struct expression
*expr
);
345 void __handle_logic(struct expression
*expr
);
346 int is_condition(struct expression
*expr
);
347 int __handle_condition_assigns(struct expression
*expr
);
348 int __handle_select_assigns(struct expression
*expr
);
349 int __handle_expr_statement_assigns(struct expression
*expr
);
351 /* smatch_implied.c */
352 extern int option_debug_implied
;
353 extern int option_debug_related
;
354 extern int option_no_implied
;
355 void get_implications(char *name
, struct symbol
*sym
, int comparison
, long long num
,
356 struct state_list
**true_states
,
357 struct state_list
**false_states
);
358 struct range_list_stack
;
359 struct state_list
*__implied_case_slist(struct expression
*switch_expr
,
360 struct expression
*case_expr
,
361 struct range_list_stack
**remaining_cases
,
362 struct state_list
**raw_slist
);
363 struct range_list
*__get_implied_values(struct expression
*switch_expr
);
364 void overwrite_states_using_pool(struct sm_state
*sm
);
366 /* smatch_extras.c */
367 #define SMATCH_EXTRA 1 /* this is my_id from smatch extra set in smatch.c */
374 static const long long valid_ptr_max
= LONG_MAX
;
375 static const long long valid_ptr_min
= 4096;
376 static const sval_t valid_ptr_max_sval
= {
380 static const sval_t valid_ptr_min_sval
= {
385 /* smatch_absolute.c */
386 int get_absolute_min_helper(struct expression
*expr
, sval_t
*sval
);
387 int get_absolute_max_helper(struct expression
*expr
, sval_t
*sval
);
389 /* smatch_local_values.c */
390 int get_local_rl(struct expression
*expr
, struct range_list
**rl
);
391 int get_local_max_helper(struct expression
*expr
, sval_t
*sval
);
392 int get_local_min_helper(struct expression
*expr
, sval_t
*sval
);
394 /* smatch_states.c */
395 void __push_fake_cur_slist();
396 struct state_list
*__pop_fake_cur_slist();
397 void __merge_slist_into_cur(struct state_list
*slist
);
399 int unreachable(void);
400 void __set_sm(struct sm_state
*sm
);
401 struct state_list
*__get_cur_slist(void);
402 void __set_true_false_sm(struct sm_state
*true_state
,
403 struct sm_state
*false_state
);
404 void nullify_path(void);
405 void __match_nullify_path_hook(const char *fn
, struct expression
*expr
,
407 void __unnullify_path(void);
408 int __path_is_null(void);
409 void save_all_states(void);
410 void nullify_all_states(void);
411 void restore_all_states(void);
412 void clear_all_states(void);
414 struct sm_state
*get_sm_state(int owner
, const char *name
,
416 struct sm_state
*get_sm_state_expr(int owner
, struct expression
*expr
);
417 void __push_true_states(void);
418 void __use_false_states(void);
419 void __discard_false_states(void);
420 void __merge_false_states(void);
421 void __merge_true_states(void);
423 void __negate_cond_stacks(void);
424 void __use_pre_cond_states(void);
425 void __use_cond_true_states(void);
426 void __use_cond_false_states(void);
427 void __push_cond_stacks(void);
428 struct state_list
*__copy_cond_true_states(void);
429 struct state_list
*__copy_cond_false_states(void);
430 struct state_list
*__pop_cond_true_stack(void);
431 struct state_list
*__pop_cond_false_stack(void);
432 void __and_cond_states(void);
433 void __or_cond_states(void);
434 void __save_pre_cond_states(void);
435 void __discard_pre_cond_states(void);
436 void __use_cond_states(void);
437 extern struct state_list
*__last_base_slist
;
439 void __warn_on_silly_pre_loops(void);
441 void __push_continues(void);
442 void __discard_continues(void);
443 void __process_continues(void);
444 void __merge_continues(void);
446 void __push_breaks(void);
447 void __process_breaks(void);
448 int __has_breaks(void);
449 void __merge_breaks(void);
450 void __use_breaks(void);
452 void __save_switch_states(struct expression
*switch_expr
);
453 void __discard_switches(void);
454 void __merge_switches(struct expression
*switch_expr
, struct expression
*case_expr
);
455 void __push_default(void);
456 void __set_default(void);
457 int __pop_default(void);
459 void __push_conditions(void);
460 void __discard_conditions(void);
462 void __save_gotos(const char *name
);
463 void __merge_gotos(const char *name
);
465 void __print_cur_slist(void);
468 void __pass_to_client(void *data
, enum hook_type type
);
469 void __pass_to_client_no_data(enum hook_type type
);
470 void __pass_case_to_client(struct expression
*switch_expr
,
471 struct expression
*case_expr
);
472 int __has_merge_function(int client_id
);
473 struct smatch_state
*__client_merge_function(int owner
,
474 struct smatch_state
*s1
,
475 struct smatch_state
*s2
);
476 struct smatch_state
*__client_unmatched_state_function(struct sm_state
*sm
);
477 void __push_scope_hooks(void);
478 void __call_scope_hooks(void);
480 /* smatch_function_hooks.c */
481 void create_function_hook_hash(void);
482 void __match_initializer_call(struct symbol
*sym
);
496 ABSOLUTE_LIMITS
= 10,
503 void add_definition_db_callback(void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
), int type
);
504 void add_member_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct smatch_state
*state
));
505 void add_returned_state_callback(void (*fn
)(int return_id
, char *return_ranges
, struct expression
*returned_expr
));
506 void add_returned_member_callback(int owner
, void (*callback
)(int return_id
, char *return_ranges
, char *printed_name
, struct smatch_state
*state
));
507 void add_db_fn_call_callback(int type
, void (*callback
)(struct expression
*arg
, char *value
));
508 struct range_list
*db_return_vals(struct expression
*expr
);
509 char *get_variable_from_key(struct expression
*arg
, char *key
, struct symbol
**sym
);
510 const char *get_param_name(struct sm_state
*sm
);
512 #define run_sql(call_back, sql...) \
514 char sql_txt[1024]; \
517 snprintf(sql_txt, 1024, sql); \
518 sm_debug("%s\n", sql_txt); \
519 sql_exec(call_back, sql_txt); \
522 /* like run_sql() but for the in-memory database */
523 #define mem_sql(call_back, sql...) \
525 char sql_txt[1024]; \
527 snprintf(sql_txt, sizeof(sql_txt), sql); \
528 sm_debug("in-mem: %s\n", sql_txt); \
529 sql_mem_exec(call_back, sql_txt); \
532 void sql_insert_return_states(int return_id
, const char *return_ranges
,
533 int type
, int param
, const char *key
, const char *value
);
534 void sql_insert_caller_info(struct expression
*call
, int type
, int param
,
535 const char *key
, const char *value
);
536 void sql_insert_function_ptr(const char *fn
, const char *struct_name
);
537 void sql_insert_return_values(const char *return_values
);
538 void sql_insert_call_implies(int type
, int param
, int value
);
539 void sql_insert_type_size(const char *member
, int size
);
540 void sql_insert_local_values(const char *name
, const char *value
);
542 void sql_select_return_states(const char *cols
, struct expression
*call
,
543 int (*callback
)(void*, int, char**, char**));
544 void sql_select_caller_info(const char *cols
, struct symbol
*sym
,
545 int (*callback
)(void*, int, char**, char**));
546 void sql_select_call_implies(const char *cols
, struct expression
*call
,
547 int (*callback
)(void*, int, char**, char**));
549 void sql_exec(int (*callback
)(void*, int, char**, char**), const char *sql
);
550 void sql_mem_exec(int (*callback
)(void*, int, char**, char**), const char *sql
);
552 void open_smatch_db(void);
555 int open_data_file(const char *filename
);
556 struct token
*get_tokens_file(const char *filename
);
559 extern char *option_project_str
;
560 extern char *data_dir
;
561 extern int option_no_data
;
562 extern int option_spammy
;
563 extern int option_full_path
;
564 extern int option_param_mapper
;
565 extern int option_info
;
566 extern int option_call_tree
;
567 extern int num_checks
;
574 extern enum project_type option_project
;
575 const char *check_name(unsigned short id
);
578 /* smatch_buf_size.c */
579 int get_array_size(struct expression
*expr
);
580 int get_array_size_bytes(struct expression
*expr
);
581 int get_array_size_bytes_min(struct expression
*expr
);
582 int get_real_array_size(struct expression
*expr
);
584 /* smatch_capped.c */
585 int is_capped(struct expression
*expr
);
587 /* check_user_data.c */
588 int is_user_data(struct expression
*expr
);
590 /* check_locking.c */
591 void print_held_locks();
593 /* check_assigned_expr.c */
594 struct expression
*get_assigned_expr(struct expression
*expr
);
596 /* smatch_comparison.c */
597 int get_comparison(struct expression
*left
, struct expression
*right
);
600 sval_t
*sval_alloc(sval_t sval
);
601 sval_t
*sval_alloc_permanent(sval_t sval
);
602 sval_t
sval_blank(struct expression
*expr
);
603 sval_t
sval_type_val(struct symbol
*type
, long long val
);
604 sval_t
sval_from_val(struct expression
*expr
, long long val
);
605 int sval_unsigned(sval_t sval
);
606 int sval_signed(sval_t sval
);
607 int sval_bits(sval_t sval
);
608 int sval_positive_bits(sval_t sval
);
609 int sval_bits_used(sval_t sval
);
610 int sval_is_negative(sval_t sval
);
611 int sval_is_positive(sval_t sval
);
612 int sval_is_min(sval_t sval
);
613 int sval_is_max(sval_t sval
);
614 int sval_is_a_min(sval_t sval
);
615 int sval_is_a_max(sval_t sval
);
616 int sval_is_negative_min(sval_t sval
);
617 int sval_cmp(sval_t one
, sval_t two
);
618 int sval_cmp_t(struct symbol
*type
, sval_t one
, sval_t two
);
619 int sval_cmp_val(sval_t one
, long long val
);
620 sval_t
sval_min(sval_t one
, sval_t two
);
621 sval_t
sval_max(sval_t one
, sval_t two
);
622 int sval_too_low(struct symbol
*type
, sval_t sval
);
623 int sval_too_high(struct symbol
*type
, sval_t sval
);
624 int sval_fits(struct symbol
*type
, sval_t sval
);
625 sval_t
sval_cast(struct symbol
*type
, sval_t sval
);
626 sval_t
sval_preop(sval_t sval
, int op
);
627 sval_t
sval_binop(sval_t left
, int op
, sval_t right
);
628 int sval_binop_overflows(sval_t left
, int op
, sval_t right
);
629 const char *sval_to_str(sval_t sval
);
630 const char *sval_to_numstr(sval_t sval
);
631 sval_t
ll_to_sval(long long val
);
633 /* smatch_string_list.c */
634 int list_has_string(struct string_list
*str_list
, char *str
);
635 void insert_string(struct string_list
**str_list
, char *str
);
636 struct string_list
*clone_str_list(struct string_list
*orig
);
637 struct string_list
*combine_string_lists(struct string_list
*one
, struct string_list
*two
);
639 /* smatch_clear_buffer.c */
640 int is_uninitialized(struct expression
*expr
);
641 int has_uninitialized_members(struct expression
*expr
);
643 #endif /* !SMATCH_H_ */