estate: rename is_whole_range() to estate_is_whole()
[smatch.git] / smatch.h
blob3aee55221417b1fb1b5da03f520891ecc3b0b9f7
1 /*
2 * sparse/smatch.h
4 * Copyright (C) 2006 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
10 #ifndef SMATCH_H_
11 # define SMATCH_H_
13 #include <stdio.h>
14 #include <string.h>
15 #include <limits.h>
16 #include "lib.h"
17 #include "allocate.h"
18 #include "parse.h"
19 #include "expression.h"
21 typedef struct {
22 struct symbol *type;
23 union {
24 long long value;
25 unsigned long long uvalue;
27 } sval_t;
29 struct smatch_state {
30 const char *name;
31 void *data;
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)
47 return (int)(long)p;
50 struct sm_state {
51 unsigned short owner;
52 unsigned int merged:1;
53 unsigned int implied:1;
54 const char *name;
55 struct symbol *sym;
56 struct smatch_state *state;
57 unsigned int line;
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;
65 struct tracker {
66 int owner;
67 char *name;
68 struct symbol *sym;
70 DECLARE_ALLOCATOR(tracker);
71 DECLARE_PTR_LIST(tracker_list, struct tracker);
73 enum hook_type {
74 EXPR_HOOK,
75 STMT_HOOK,
76 SYM_HOOK,
77 STRING_HOOK,
78 DECLARATION_HOOK,
79 ASSIGNMENT_HOOK,
80 RAW_ASSIGNMENT_HOOK,
81 GLOBAL_ASSIGNMENT_HOOK,
82 LOGIC_HOOK,
83 CONDITION_HOOK,
84 PRELOOP_HOOK,
85 SELECT_HOOK,
86 WHOLE_CONDITION_HOOK,
87 FUNCTION_CALL_HOOK,
88 CALL_ASSIGNMENT_HOOK,
89 MACRO_ASSIGNMENT_HOOK,
90 BINOP_HOOK,
91 OP_HOOK,
92 DEREF_HOOK,
93 CASE_HOOK,
94 ASM_HOOK,
95 CAST_HOOK,
96 SIZEOF_HOOK,
97 BASE_HOOK,
98 FUNC_DEF_HOOK,
99 AFTER_DEF_HOOK,
100 END_FUNC_HOOK,
101 RETURN_HOOK,
102 END_FILE_HOOK,
105 #define TRUE 1
106 #define FALSE 0
108 struct range_list;
110 void add_hook(void *func, enum hook_type type);
111 typedef struct smatch_state *(merge_func_t)(struct smatch_state *s1, struct smatch_state *s2);
112 typedef struct smatch_state *(unmatched_func_t)(struct sm_state *state);
113 void add_merge_hook(int client_id, merge_func_t *func);
114 void add_unmatched_state_hook(int client_id, unmatched_func_t *func);
115 typedef void (scope_hook)(void *data);
116 void add_scope_hook(scope_hook *hook, void *data);
117 typedef void (func_hook)(const char *fn, struct expression *expr, void *data);
118 typedef void (implication_hook)(const char *fn, struct expression *call_expr,
119 struct expression *assign_expr, void *data);
120 typedef void (return_implies_hook)(struct expression *call_expr,
121 int param, char *key, char *value);
122 typedef int (implied_return_hook)(struct expression *call_expr, void *info, struct range_list **rl);
123 void add_function_hook(const char *look_for, func_hook *call_back, void *data);
125 void add_function_assign_hook(const char *look_for, func_hook *call_back,
126 void *info);
127 void add_implied_return_hook(const char *look_for,
128 implied_return_hook *call_back,
129 void *info);
130 void add_macro_assign_hook(const char *look_for, func_hook *call_back,
131 void *info);
132 void add_macro_assign_hook_extra(const char *look_for, func_hook *call_back,
133 void *info);
134 void return_implies_state(const char *look_for, long long start, long long end,
135 implication_hook *call_back, void *info);
136 void add_db_return_states_callback(int type, return_implies_hook *callback);
137 void add_db_return_states_before(void (*fn)(void));
138 void add_db_return_states_after(void (*fn)(void));
139 int get_implied_return(struct expression *expr, struct range_list **rl);
141 typedef void (modification_hook)(struct sm_state *sm);
142 void add_modification_hook(int owner, modification_hook *call_back);
143 void add_indirect_modification_hook(int owner, modification_hook *call_back);
145 const char *get_filename(void);
146 char *get_function(void);
147 int get_lineno(void);
148 extern int final_pass;
149 extern struct symbol *cur_func_sym;
150 extern int option_debug;
151 extern int local_debug;
153 extern FILE *sm_outfd;
154 #define sm_printf(msg...) do { if (final_pass) fprintf(sm_outfd, msg); } while (0)
156 static inline void sm_prefix(void)
158 sm_printf("%s:%d %s() ", get_filename(), get_lineno(), get_function());
161 static inline void print_implied_debug_msg();
163 #define sm_msg(msg...) \
164 do { \
165 print_implied_debug_msg(); \
166 if (!option_debug && !final_pass) \
167 break; \
168 sm_prefix(); \
169 sm_printf(msg); \
170 sm_printf("\n"); \
171 } while (0)
173 extern char *implied_debug_msg;
174 static inline void print_implied_debug_msg()
176 static struct symbol *last_printed = NULL;
178 if (!implied_debug_msg)
179 return;
180 if (last_printed == cur_func_sym)
181 return;
182 last_printed = cur_func_sym;
183 sm_msg("%s", implied_debug_msg);
186 #define sm_debug(msg...) do { if (option_debug) sm_printf(msg); } while (0)
188 #define sm_info(msg...) do { \
189 if (option_debug || (option_info && final_pass)) { \
190 sm_prefix(); \
191 sm_printf("info: "); \
192 sm_printf(msg); \
193 sm_printf("\n"); \
195 } while(0)
197 struct smatch_state *get_state(int owner, const char *name, struct symbol *sym);
198 struct smatch_state *get_state_expr(int owner, struct expression *expr);
199 struct state_list *get_possible_states(int owner, const char *name,
200 struct symbol *sym);
201 struct state_list *get_possible_states_expr(int owner, struct expression *expr);
202 struct sm_state *set_state(int owner, const char *name, struct symbol *sym,
203 struct smatch_state *state);
204 struct sm_state *set_state_expr(int owner, struct expression *expr,
205 struct smatch_state *state);
206 void delete_state(int owner, const char *name, struct symbol *sym);
207 void delete_state_expr(int owner, struct expression *expr);
208 void set_true_false_states(int owner, const char *name, struct symbol *sym,
209 struct smatch_state *true_state,
210 struct smatch_state *false_state);
211 void set_true_false_states_expr(int owner, struct expression *expr,
212 struct smatch_state *true_state,
213 struct smatch_state *false_state);
215 struct state_list *get_all_states_slist(int owner, struct state_list *source);
216 struct state_list *get_all_states(int id);
217 int is_reachable(void);
219 /* smatch_helper.c */
220 char *alloc_string(const char *str);
221 void free_string(char *str);
222 struct smatch_state *alloc_state_num(int num);
223 struct expression *get_argument_from_call_expr(struct expression_list *args,
224 int num);
225 char *get_variable_from_expr_complex(struct expression *expr,
226 struct symbol **sym_ptr);
227 char *get_variable_from_expr(struct expression *expr,
228 struct symbol **sym_ptr);
229 int sym_name_is(const char *name, struct expression *expr);
230 int get_value(struct expression *expr, sval_t *val);
231 int get_implied_value(struct expression *expr, sval_t *val);
232 int get_implied_min(struct expression *expr, sval_t *sval);
233 int get_implied_max(struct expression *expr, sval_t *val);
234 int get_hard_max(struct expression *expr, sval_t *sval);
235 int get_fuzzy_min(struct expression *expr, sval_t *min);
236 int get_fuzzy_max(struct expression *expr, sval_t *max);
237 int get_absolute_min(struct expression *expr, sval_t *sval);
238 int get_absolute_max(struct expression *expr, sval_t *sval);
239 int parse_call_math(struct expression *expr, char *math, sval_t *val);
240 int is_zero(struct expression *expr);
241 int known_condition_true(struct expression *expr);
242 int known_condition_false(struct expression *expr);
243 int implied_condition_true(struct expression *expr);
244 int implied_condition_false(struct expression *expr);
246 int is_array(struct expression *expr);
247 struct expression *get_array_name(struct expression *expr);
248 struct expression *get_array_offset(struct expression *expr);
249 const char *show_state(struct smatch_state *state);
250 struct statement *get_expression_statement(struct expression *expr);
251 struct expression *strip_parens(struct expression *expr);
252 struct expression *strip_expr(struct expression *expr);
253 void scoped_state(int my_id, const char *name, struct symbol *sym);
254 int is_error_return(struct expression *expr);
255 int getting_address(void);
256 char *get_member_name(struct expression *expr);
257 char *get_fnptr_name(struct expression *expr);
258 int positions_eq(struct position pos1, struct position pos2);
259 struct statement *get_current_statement(void);
260 int get_param_num_from_sym(struct symbol *sym);
262 /* smatch_type.c */
263 struct symbol *get_real_base_type(struct symbol *sym);
264 int type_bits(struct symbol *type);
265 int type_positive_bits(struct symbol *type);
266 struct symbol *get_pointer_type(struct expression *expr);
267 struct symbol *get_type(struct expression *expr);
268 int type_unsigned(struct symbol *base_type);
269 int type_signed(struct symbol *base_type);
270 int expr_unsigned(struct expression *expr);
271 int returns_unsigned(struct symbol *base_type);
272 int is_pointer(struct expression *expr);
273 int returns_pointer(struct symbol *base_type);
274 sval_t sval_type_max(struct symbol *base_type);
275 sval_t sval_type_min(struct symbol *base_type);
276 int nr_bits(struct expression *expr);
277 int is_static(struct expression *expr);
278 int types_equiv(struct symbol *one, struct symbol *two);
279 const char *global_static();
280 struct symbol *cur_func_return_type(void);
281 struct symbol *get_arg_type(struct expression *fn, int arg);
282 struct symbol *get_member_type_from_key(struct expression *expr, char *key);
284 /* smatch_ignore.c */
285 void add_ignore(int owner, const char *name, struct symbol *sym);
286 int is_ignored(int owner, const char *name, struct symbol *sym);
288 /* smatch_tracker */
289 struct tracker *alloc_tracker(int owner, const char *name, struct symbol *sym);
290 void add_tracker(struct tracker_list **list, int owner, const char *name,
291 struct symbol *sym);
292 void add_tracker_expr(struct tracker_list **list, int owner, struct expression *expr);
293 void del_tracker(struct tracker_list **list, int owner, const char *name,
294 struct symbol *sym);
295 int in_tracker_list(struct tracker_list *list, int owner, const char *name,
296 struct symbol *sym);
297 void free_tracker_list(struct tracker_list **list);
298 void free_trackers_and_list(struct tracker_list **list);
300 /* smatch_conditions */
301 int in_condition(void);
303 /* smatch_flow.c */
305 void smatch (int argc, char **argv);
306 int inside_loop(void);
307 struct expression *get_switch_expr(void);
308 int in_expression_statement(void);
309 void __split_expr(struct expression *expr);
310 void __split_stmt(struct statement *stmt);
311 extern int option_assume_loops;
312 extern int option_known_conditions;
313 extern int option_two_passes;
314 extern int option_no_db;
315 extern int option_file_output;
316 extern struct expression_list *big_expression_stack;
317 extern struct statement_list *big_statement_stack;
318 extern int __in_pre_condition;
319 extern int __bail_on_rest_of_function;
321 /* smatch_conditions */
322 void __split_whole_condition(struct expression *expr);
323 void __handle_logic(struct expression *expr);
324 int is_condition(struct expression *expr);
325 int __handle_condition_assigns(struct expression *expr);
326 int __handle_select_assigns(struct expression *expr);
327 int __handle_expr_statement_assigns(struct expression *expr);
329 /* smatch_implied.c */
330 extern int option_debug_implied;
331 extern int option_debug_related;
332 extern int option_no_implied;
333 void get_implications(char *name, struct symbol *sym, int comparison, long long num,
334 struct state_list **true_states,
335 struct state_list **false_states);
336 struct range_list_stack;
337 struct state_list *__implied_case_slist(struct expression *switch_expr,
338 struct expression *case_expr,
339 struct range_list_stack **remaining_cases,
340 struct state_list **raw_slist);
341 struct range_list *__get_implied_values(struct expression *switch_expr);
342 void overwrite_states_using_pool(struct sm_state *sm);
344 /* smatch_extras.c */
345 #define SMATCH_EXTRA 1 /* this is my_id from smatch extra set in smatch.c */
347 struct data_range {
348 sval_t min;
349 sval_t max;
352 static const long long valid_ptr_max = LONG_MAX;
353 static const long long valid_ptr_min = 4096;
354 static const sval_t valid_ptr_max_sval = {
355 .type = &ptr_ctype,
356 .value = LONG_MAX,
358 static const sval_t valid_ptr_min_sval = {
359 .type = &ptr_ctype,
360 .value = 4096,
363 /* smatch_absolute.c */
364 int get_absolute_min_helper(struct expression *expr, sval_t *sval);
365 int get_absolute_max_helper(struct expression *expr, sval_t *sval);
367 /* smatch_states.c */
368 void __push_fake_cur_slist();
369 struct state_list *__pop_fake_cur_slist();
370 void __merge_slist_into_cur(struct state_list *slist);
372 int unreachable(void);
373 void __set_sm(struct sm_state *sm);
374 struct state_list *__get_cur_slist(void);
375 void __set_true_false_sm(struct sm_state *true_state,
376 struct sm_state *false_state);
377 void nullify_path(void);
378 void __match_nullify_path_hook(const char *fn, struct expression *expr,
379 void *unused);
380 void __unnullify_path(void);
381 int __path_is_null(void);
382 void clear_all_states(void);
384 struct sm_state *get_sm_state(int owner, const char *name,
385 struct symbol *sym);
386 struct sm_state *get_sm_state_expr(int owner, struct expression *expr);
387 void __push_true_states(void);
388 void __use_false_states(void);
389 void __discard_false_states(void);
390 void __merge_false_states(void);
391 void __merge_true_states(void);
393 void __negate_cond_stacks(void);
394 void __use_pre_cond_states(void);
395 void __use_cond_true_states(void);
396 void __use_cond_false_states(void);
397 void __push_cond_stacks(void);
398 struct state_list *__copy_cond_true_states(void);
399 struct state_list *__copy_cond_false_states(void);
400 struct state_list *__pop_cond_true_stack(void);
401 struct state_list *__pop_cond_false_stack(void);
402 void __and_cond_states(void);
403 void __or_cond_states(void);
404 void __save_pre_cond_states(void);
405 void __discard_pre_cond_states(void);
406 void __use_cond_states(void);
407 extern struct state_list *__last_base_slist;
409 void __warn_on_silly_pre_loops(void);
411 void __push_continues(void);
412 void __discard_continues(void);
413 void __process_continues(void);
414 void __merge_continues(void);
416 void __push_breaks(void);
417 void __process_breaks(void);
418 int __has_breaks(void);
419 void __merge_breaks(void);
420 void __use_breaks(void);
422 void __save_switch_states(struct expression *switch_expr);
423 void __discard_switches(void);
424 void __merge_switches(struct expression *switch_expr, struct expression *case_expr);
425 void __push_default(void);
426 void __set_default(void);
427 int __pop_default(void);
429 void __push_conditions(void);
430 void __discard_conditions(void);
432 void __save_gotos(const char *name);
433 void __merge_gotos(const char *name);
435 void __print_cur_slist(void);
437 /* smatch_hooks.c */
438 void __pass_to_client(void *data, enum hook_type type);
439 void __pass_to_client_no_data(enum hook_type type);
440 void __pass_case_to_client(struct expression *switch_expr,
441 struct expression *case_expr);
442 int __has_merge_function(int client_id);
443 struct smatch_state *__client_merge_function(int owner,
444 struct smatch_state *s1,
445 struct smatch_state *s2);
446 struct smatch_state *__client_unmatched_state_function(struct sm_state *sm);
447 void __push_scope_hooks(void);
448 void __call_scope_hooks(void);
450 /* smatch_function_hooks.c */
451 void create_function_hook_hash(void);
452 void __match_initializer_call(struct symbol *sym);
454 /* smatch_db.c */
455 enum info_type {
456 INTERNAL = 0,
457 PARAM_VALUE,
458 BUF_SIZE,
459 USER_DATA,
460 CAPPED_DATA,
461 RETURN_VALUE,
462 DEREFERENCE,
463 RANGE_CAP,
464 LOCK_HELD,
465 LOCK_RELEASED,
466 ABSOLUTE_LIMITS,
467 LIMITED_VALUE,
468 ADDED_VALUE,
469 FILTER_VALUE,
472 void add_definition_db_callback(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type);
473 void add_member_info_callback(int owner, void (*callback)(char *fn, char *global_static, int param, char *printed_name, struct smatch_state *state));
474 void add_returned_state_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr, struct state_list *slist));
475 void add_returned_member_callback(int owner, void (*callback)(int return_id, char *return_ranges, char *printed_name, struct smatch_state *state));
476 void add_db_fn_call_callback(int type, void (*callback)(struct expression *arg, char *value));
477 struct range_list *db_return_vals(struct expression *expr);
479 #define run_sql(call_back, sql...) \
480 do { \
481 char sql_txt[1024]; \
482 snprintf(sql_txt, 1024, sql); \
483 sm_debug("%s\n", sql_txt); \
484 sql_exec(call_back, sql_txt); \
485 } while (0)
487 void sql_exec(int (*callback)(void*, int, char**, char**), const char *sql);
488 void open_smatch_db(void);
490 /* smatch_files.c */
491 struct token *get_tokens_file(const char *filename);
493 /* smatch.c */
494 extern char *option_project_str;
495 extern char *data_dir;
496 extern int option_no_data;
497 extern int option_spammy;
498 extern int option_full_path;
499 extern int option_param_mapper;
500 extern int option_info;
501 extern int option_call_tree;
502 extern int num_checks;
504 enum project_type {
505 PROJ_NONE,
506 PROJ_KERNEL,
507 PROJ_WINE,
509 extern enum project_type option_project;
510 const char *check_name(unsigned short id);
513 /* smatch_buf_size.c */
514 int get_array_size(struct expression *expr);
515 int get_array_size_bytes(struct expression *expr);
516 int get_array_size_bytes_min(struct expression *expr);
518 /* smatch_capped.c */
519 int is_capped(struct expression *expr);
521 /* check_user_data.c */
522 int is_user_data(struct expression *expr);
524 /* check_locking.c */
525 void print_held_locks();
527 /* check_assigned_expr.c */
528 struct expression *get_assigned_expr(struct expression *expr);
530 /* smatch_sval.c */
531 sval_t *sval_alloc(sval_t sval);
532 sval_t *sval_alloc_permanent(sval_t sval);
533 sval_t sval_blank(struct expression *expr);
534 sval_t sval_type_val(struct symbol *type, long long val);
535 sval_t sval_from_val(struct expression *expr, long long val);
536 int sval_unsigned(sval_t sval);
537 int sval_signed(sval_t sval);
538 int sval_bits(sval_t sval);
539 int sval_positive_bits(sval_t sval);
540 int sval_bits_used(sval_t sval);
541 int sval_is_negative(sval_t sval);
542 int sval_is_positive(sval_t sval);
543 int sval_is_min(sval_t sval);
544 int sval_is_max(sval_t sval);
545 int sval_is_a_min(sval_t sval);
546 int sval_is_a_max(sval_t sval);
547 int sval_cmp(sval_t one, sval_t two);
548 int sval_cmp_t(struct symbol *type, sval_t one, sval_t two);
549 int sval_cmp_val(sval_t one, long long val);
550 int sval_too_low(struct symbol *type, sval_t sval);
551 int sval_too_high(struct symbol *type, sval_t sval);
552 int sval_fits(struct symbol *type, sval_t sval);
553 sval_t sval_cast(struct symbol *type, sval_t sval);
554 sval_t sval_preop(sval_t sval, int op);
555 sval_t sval_binop(sval_t left, int op, sval_t right);
556 int sval_binop_overflows(sval_t left, int op, sval_t right);
557 const char *sval_to_str(sval_t sval);
558 const char *sval_to_numstr(sval_t sval);
559 sval_t ll_to_sval(long long val);
561 #endif /* !SMATCH_H_ */