4 * Copyright (C) 2006 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
19 #include "expression.h"
25 #define STATE(_x) static struct smatch_state _x = { .name = #_x }
26 extern struct smatch_state undefined
;
27 extern struct smatch_state merged
;
28 extern struct smatch_state true_state
;
29 extern struct smatch_state false_state
;
30 DECLARE_ALLOCATOR(smatch_state
);
32 static inline void *INT_PTR(int i
)
34 return (void *)(long)i
;
37 static inline int PTR_INT(void *p
)
44 unsigned int merged
:1;
45 unsigned int implied
:1;
48 struct smatch_state
*state
;
50 struct state_list
*pool
;
51 struct sm_state
*left
;
52 struct sm_state
*right
;
53 unsigned int nr_children
;
54 struct state_list
*possible
;
62 DECLARE_ALLOCATOR(tracker
);
63 DECLARE_PTR_LIST(tracker_list
, struct tracker
);
80 MACRO_ASSIGNMENT_HOOK
,
97 void add_hook(void *func
, enum hook_type type
);
98 typedef struct smatch_state
*(merge_func_t
)(struct smatch_state
*s1
, struct smatch_state
*s2
);
99 typedef struct smatch_state
*(unmatched_func_t
)(struct sm_state
*state
);
100 void add_merge_hook(int client_id
, merge_func_t
*func
);
101 void add_unmatched_state_hook(int client_id
, unmatched_func_t
*func
);
102 typedef void (scope_hook
)(void *data
);
103 void add_scope_hook(scope_hook
*hook
, void *data
);
104 typedef void (func_hook
)(const char *fn
, struct expression
*expr
, void *data
);
105 typedef void (implication_hook
)(const char *fn
, struct expression
*call_expr
,
106 struct expression
*assign_expr
, void *data
);
107 typedef void (return_implies_hook
)(struct expression
*call_expr
,
108 int param
, char *key
, char *value
);
109 void add_function_hook(const char *look_for
, func_hook
*call_back
, void *data
);
111 void add_function_assign_hook(const char *look_for
, func_hook
*call_back
,
113 void add_macro_assign_hook(const char *look_for
, func_hook
*call_back
,
115 void return_implies_state(const char *look_for
, long long start
, long long end
,
116 implication_hook
*call_back
, void *info
);
117 void add_db_return_implies_callback(int type
, return_implies_hook
*callback
);
118 typedef void (modification_hook
)(struct sm_state
*sm
);
119 void add_modification_hook(int owner
, modification_hook
*call_back
);
120 void add_indirect_modification_hook(int owner
, modification_hook
*call_back
);
122 const char *get_filename(void);
123 char *get_function(void);
124 int get_lineno(void);
125 extern int final_pass
;
126 extern struct symbol
*cur_func_sym
;
127 extern int option_debug
;
129 extern FILE *sm_outfd
;
130 #define sm_printf(msg...) do { if (final_pass) fprintf(sm_outfd, msg); } while (0)
132 static inline void sm_prefix(void)
134 sm_printf("%s:%d %s() ", get_filename(), get_lineno(), get_function());
137 static inline void print_implied_debug_msg();
139 #define sm_msg(msg...) \
141 print_implied_debug_msg(); \
142 if (!option_debug && !final_pass) \
149 extern char *implied_debug_msg
;
150 static inline void print_implied_debug_msg()
152 static struct symbol
*last_printed
= NULL
;
154 if (!implied_debug_msg
)
156 if (last_printed
== cur_func_sym
)
158 last_printed
= cur_func_sym
;
159 sm_msg("%s", implied_debug_msg
);
162 #define sm_debug(msg...) do { if (option_debug) sm_printf(msg); } while (0)
164 #define sm_info(msg...) do { \
165 if (option_debug || (option_info && final_pass)) { \
167 sm_printf("info: "); \
173 struct smatch_state
*get_state(int owner
, const char *name
, struct symbol
*sym
);
174 struct smatch_state
*get_state_expr(int owner
, struct expression
*expr
);
175 struct state_list
*get_possible_states(int owner
, const char *name
,
177 struct state_list
*get_possible_states_expr(int owner
, struct expression
*expr
);
178 struct sm_state
*set_state(int owner
, const char *name
, struct symbol
*sym
,
179 struct smatch_state
*state
);
180 struct sm_state
*set_state_expr(int owner
, struct expression
*expr
,
181 struct smatch_state
*state
);
182 void delete_state(int owner
, const char *name
, struct symbol
*sym
);
183 void delete_state_expr(int owner
, struct expression
*expr
);
184 void set_true_false_states(int owner
, const char *name
, struct symbol
*sym
,
185 struct smatch_state
*true_state
,
186 struct smatch_state
*false_state
);
187 void set_true_false_states_expr(int owner
, struct expression
*expr
,
188 struct smatch_state
*true_state
,
189 struct smatch_state
*false_state
);
191 struct state_list
*get_all_states(int id
);
192 int is_reachable(void);
194 /* smatch_helper.c */
195 char *alloc_string(const char *str
);
196 void free_string(char *str
);
197 struct smatch_state
*alloc_state_num(int num
);
198 struct expression
*get_argument_from_call_expr(struct expression_list
*args
,
200 char *get_variable_from_expr_complex(struct expression
*expr
,
201 struct symbol
**sym_ptr
);
202 char *get_variable_from_expr(struct expression
*expr
,
203 struct symbol
**sym_ptr
);
204 int sym_name_is(const char *name
, struct expression
*expr
);
205 int get_value(struct expression
*expr
, long long *val
);
206 int get_implied_value(struct expression
*expr
, long long *val
);
207 int get_implied_max(struct expression
*expr
, long long *val
);
208 int get_implied_min(struct expression
*expr
, long long *val
);
209 int get_fuzzy_min(struct expression
*expr
, long long *min
);
210 int get_fuzzy_max(struct expression
*expr
, long long *max
);
211 int get_absolute_min(struct expression
*expr
, long long *val
);
212 int get_absolute_max(struct expression
*expr
, long long *val
);
213 int is_zero(struct expression
*expr
);
214 int known_condition_true(struct expression
*expr
);
215 int known_condition_false(struct expression
*expr
);
216 int implied_condition_true(struct expression
*expr
);
217 int implied_condition_false(struct expression
*expr
);
219 int is_array(struct expression
*expr
);
220 struct expression
*get_array_name(struct expression
*expr
);
221 struct expression
*get_array_offset(struct expression
*expr
);
222 const char *show_state(struct smatch_state
*state
);
223 struct statement
*get_expression_statement(struct expression
*expr
);
224 struct expression
*strip_parens(struct expression
*expr
);
225 struct expression
*strip_expr(struct expression
*expr
);
226 void scoped_state(int my_id
, const char *name
, struct symbol
*sym
);
227 int is_error_return(struct expression
*expr
);
228 int getting_address(void);
229 char *get_member_name(struct expression
*expr
);
230 char *get_fnptr_name(struct expression
*expr
);
231 int positions_eq(struct position pos1
, struct position pos2
);
234 struct symbol
*get_real_base_type(struct symbol
*sym
);
235 struct symbol
*get_pointer_type(struct expression
*expr
);
236 struct symbol
*get_type(struct expression
*expr
);
237 int type_unsigned(struct symbol
*base_type
);
238 int expr_unsigned(struct expression
*expr
);
239 int returns_unsigned(struct symbol
*base_type
);
240 int is_pointer(struct expression
*expr
);
241 int returns_pointer(struct symbol
*base_type
);
242 long long type_max(struct symbol
*base_type
);
243 long long type_min(struct symbol
*base_type
);
244 int nr_bits(struct expression
*expr
);
245 int is_static(struct expression
*expr
);
246 const char *global_static();
248 /* smatch_ignore.c */
249 void add_ignore(int owner
, const char *name
, struct symbol
*sym
);
250 int is_ignored(int owner
, const char *name
, struct symbol
*sym
);
253 struct tracker
*alloc_tracker(int owner
, const char *name
, struct symbol
*sym
);
254 void add_tracker(struct tracker_list
**list
, int owner
, const char *name
,
256 void add_tracker_expr(struct tracker_list
**list
, int owner
, struct expression
*expr
);
257 void del_tracker(struct tracker_list
**list
, int owner
, const char *name
,
259 int in_tracker_list(struct tracker_list
*list
, int owner
, const char *name
,
261 void free_tracker_list(struct tracker_list
**list
);
262 void free_trackers_and_list(struct tracker_list
**list
);
264 /* smatch_conditions */
265 int in_condition(void);
269 void smatch (int argc
, char **argv
);
270 int inside_loop(void);
271 int in_expression_statement(void);
272 void __split_expr(struct expression
*expr
);
273 void __split_stmt(struct statement
*stmt
);
274 extern int option_assume_loops
;
275 extern int option_known_conditions
;
276 extern int option_two_passes
;
277 extern int option_no_db
;
278 extern int option_file_output
;
279 extern struct expression_list
*big_expression_stack
;
280 extern struct statement_list
*big_statement_stack
;
281 extern int __in_pre_condition
;
282 extern int __bail_on_rest_of_function
;
284 /* smatch_conditions */
285 void __split_whole_condition(struct expression
*expr
);
286 void __handle_logic(struct expression
*expr
);
287 int __is_condition_assign(struct expression
*expr
);
288 int __handle_condition_assigns(struct expression
*expr
);
289 int __handle_select_assigns(struct expression
*expr
);
290 int __handle_expr_statement_assigns(struct expression
*expr
);
292 /* smatch_implied.c */
293 extern int option_debug_implied
;
294 extern int option_debug_related
;
295 extern int option_no_implied
;
296 void get_implications(char *name
, struct symbol
*sym
, int comparison
, long long num
,
297 struct state_list
**true_states
,
298 struct state_list
**false_states
);
299 struct range_list_stack
;
300 struct state_list
*__implied_case_slist(struct expression
*switch_expr
,
301 struct expression
*case_expr
,
302 struct range_list_stack
**remaining_cases
,
303 struct state_list
**raw_slist
);
304 struct range_list
*__get_implied_values(struct expression
*switch_expr
);
306 /* smatch_extras.c */
307 #define SMATCH_EXTRA 1 /* this is my_id from smatch extra set in smatch.c */
313 extern struct data_range whole_range
;
314 static const long long valid_ptr_max
= LONG_MAX
;
315 static const long long valid_ptr_min
= 4096;
317 /* smatch_states.c */
318 void __push_fake_cur_slist();
319 struct state_list
*__pop_fake_cur_slist();
320 void __merge_slist_into_cur(struct state_list
*slist
);
322 int unreachable(void);
323 void __set_sm(struct sm_state
*sm
);
324 struct state_list
*__get_cur_slist(void);
325 void __set_true_false_sm(struct sm_state
*true_state
,
326 struct sm_state
*false_state
);
327 void nullify_path(void);
328 void __match_nullify_path_hook(const char *fn
, struct expression
*expr
,
330 void __unnullify_path(void);
331 int __path_is_null(void);
332 void clear_all_states(void);
334 struct sm_state
*get_sm_state(int owner
, const char *name
,
336 struct sm_state
*get_sm_state_expr(int owner
, struct expression
*expr
);
337 void __push_true_states(void);
338 void __use_false_states(void);
339 void __discard_false_states(void);
340 void __merge_false_states(void);
341 void __merge_true_states(void);
343 void __negate_cond_stacks(void);
344 void __use_pre_cond_states(void);
345 void __use_cond_true_states(void);
346 void __use_cond_false_states(void);
347 void __push_cond_stacks(void);
348 struct state_list
*__copy_cond_true_states(void);
349 struct state_list
*__copy_cond_false_states(void);
350 struct state_list
*__pop_cond_true_stack(void);
351 struct state_list
*__pop_cond_false_stack(void);
352 void __and_cond_states(void);
353 void __or_cond_states(void);
354 void __save_pre_cond_states(void);
355 void __discard_pre_cond_states(void);
356 void __use_cond_states(void);
357 extern struct state_list
*__last_base_slist
;
359 void __warn_on_silly_pre_loops(void);
361 void __push_continues(void);
362 void __discard_continues(void);
363 void __process_continues(void);
364 void __merge_continues(void);
366 void __push_breaks(void);
367 void __process_breaks(void);
368 void __merge_breaks(void);
369 void __use_breaks(void);
371 void __save_switch_states(struct expression
*switch_expr
);
372 void __discard_switches(void);
373 void __merge_switches(struct expression
*switch_expr
, struct expression
*case_expr
);
374 void __push_default(void);
375 void __set_default(void);
376 int __pop_default(void);
378 void __push_conditions(void);
379 void __discard_conditions(void);
381 void __save_gotos(const char *name
);
382 void __merge_gotos(const char *name
);
384 void __print_cur_slist(void);
387 void __pass_to_client(void *data
, enum hook_type type
);
388 void __pass_to_client_no_data(enum hook_type type
);
389 void __pass_case_to_client(struct expression
*switch_expr
,
390 struct expression
*case_expr
);
391 int __has_merge_function(int client_id
);
392 struct smatch_state
*__client_merge_function(int owner
,
393 struct smatch_state
*s1
,
394 struct smatch_state
*s2
);
395 struct smatch_state
*__client_unmatched_state_function(struct sm_state
*sm
);
396 void __push_scope_hooks(void);
397 void __call_scope_hooks(void);
399 /* smatch_function_hooks.c */
400 void create_function_hook_hash(void);
401 void __match_initializer_call(struct symbol
*sym
);
414 void add_definition_db_callback(void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
), int type
);
415 void add_member_info_callback(int owner
, void (*callback
)(char *fn
, char *global_static
, int param
, char *printed_name
, struct smatch_state
*state
));
416 void add_db_fn_call_callback(int type
, void (*callback
)(struct expression
*arg
, char *value
));
417 struct range_list
*db_return_vals(struct expression
*expr
);
419 #define run_sql(call_back, sql...) \
421 char sql_txt[1024]; \
422 snprintf(sql_txt, 1024, sql); \
423 sm_debug("%s\n", sql_txt); \
424 sql_exec(call_back, sql_txt); \
427 void sql_exec(int (*callback
)(void*, int, char**, char**), const char *sql
);
428 void open_smatch_db(void);
431 struct token
*get_tokens_file(const char *filename
);
434 extern char *option_project_str
;
435 extern char *data_dir
;
436 extern int option_no_data
;
437 extern int option_spammy
;
438 extern int option_full_path
;
439 extern int option_param_mapper
;
440 extern int option_info
;
441 extern int option_call_tree
;
442 extern int num_checks
;
449 extern enum project_type option_project
;
450 const char *check_name(unsigned short id
);
453 /* smatch_buf_size.c */
454 int get_array_size(struct expression
*expr
);
455 int get_array_size_bytes(struct expression
*expr
);
457 /* smatch_capped.c */
458 int is_capped(struct expression
*expr
);
460 /* check_user_data.c */
461 int is_user_data(struct expression
*expr
);
463 /* check_locking.c */
464 void print_held_locks();
466 /* check_assigned_expr.c */
467 struct expression
*get_assigned_expr(struct expression
*expr
);
469 #endif /* !SMATCH_H_ */