extra: delete match_untracked_array()
[smatch.git] / smatch.h
blob15e129ed41a7472b1e92dbaa324d1b6b112ccef4
1 /*
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
18 #ifndef SMATCH_H_
19 # define SMATCH_H_
21 #include <stdio.h>
22 #include <string.h>
23 #include <limits.h>
24 #include <sys/time.h>
25 #include "lib.h"
26 #include "allocate.h"
27 #include "scope.h"
28 #include "parse.h"
29 #include "expression.h"
30 #include "avl.h"
32 typedef struct {
33 struct symbol *type;
34 union {
35 long long value;
36 unsigned long long uvalue;
38 } sval_t;
40 struct smatch_state {
41 const char *name;
42 void *data;
44 #define STATE(_x) static struct smatch_state _x = { .name = #_x }
45 extern struct smatch_state undefined;
46 extern struct smatch_state ghost;
47 extern struct smatch_state merged;
48 extern struct smatch_state true_state;
49 extern struct smatch_state false_state;
50 DECLARE_ALLOCATOR(smatch_state);
52 static inline void *INT_PTR(int i)
54 return (void *)(long)i;
57 static inline int PTR_INT(void *p)
59 return (int)(long)p;
62 struct tracker {
63 char *name;
64 struct symbol *sym;
65 unsigned short owner;
67 DECLARE_ALLOCATOR(tracker);
68 DECLARE_PTR_LIST(tracker_list, struct tracker);
69 DECLARE_PTR_LIST(stree_stack, struct stree);
71 /* The first 3 struct members must match struct tracker */
72 struct sm_state {
73 const char *name;
74 struct symbol *sym;
75 unsigned short owner;
76 unsigned short merged:1;
77 unsigned int nr_children;
78 unsigned int line;
79 struct smatch_state *state;
80 struct stree *pool;
81 struct sm_state *left;
82 struct sm_state *right;
83 struct state_list *possible;
86 struct var_sym {
87 char *var;
88 struct symbol *sym;
90 DECLARE_ALLOCATOR(var_sym);
91 DECLARE_PTR_LIST(var_sym_list, struct var_sym);
93 struct constraint {
94 int op;
95 int id;
97 DECLARE_PTR_LIST(constraint_list, struct constraint);
99 enum hook_type {
100 EXPR_HOOK,
101 STMT_HOOK,
102 STMT_HOOK_AFTER,
103 SYM_HOOK,
104 STRING_HOOK,
105 DECLARATION_HOOK,
106 ASSIGNMENT_HOOK,
107 ASSIGNMENT_HOOK_AFTER,
108 RAW_ASSIGNMENT_HOOK,
109 GLOBAL_ASSIGNMENT_HOOK,
110 LOGIC_HOOK,
111 CONDITION_HOOK,
112 PRELOOP_HOOK,
113 SELECT_HOOK,
114 WHOLE_CONDITION_HOOK,
115 FUNCTION_CALL_HOOK,
116 CALL_HOOK_AFTER_INLINE,
117 FUNCTION_CALL_HOOK_AFTER_DB,
118 CALL_ASSIGNMENT_HOOK,
119 MACRO_ASSIGNMENT_HOOK,
120 BINOP_HOOK,
121 OP_HOOK,
122 DEREF_HOOK,
123 CASE_HOOK,
124 ASM_HOOK,
125 CAST_HOOK,
126 SIZEOF_HOOK,
127 BASE_HOOK,
128 FUNC_DEF_HOOK,
129 AFTER_DEF_HOOK,
130 END_FUNC_HOOK,
131 AFTER_FUNC_HOOK,
132 RETURN_HOOK,
133 INLINE_FN_START,
134 INLINE_FN_END,
135 END_FILE_HOOK,
136 NUM_HOOKS,
139 #define TRUE 1
140 #define FALSE 0
142 struct range_list;
144 void add_hook(void *func, enum hook_type type);
145 typedef struct smatch_state *(merge_func_t)(struct smatch_state *s1, struct smatch_state *s2);
146 typedef struct smatch_state *(unmatched_func_t)(struct sm_state *state);
147 void add_merge_hook(int client_id, merge_func_t *func);
148 void add_unmatched_state_hook(int client_id, unmatched_func_t *func);
149 void add_pre_merge_hook(int client_id, void (*hook)(struct sm_state *sm));
150 typedef void (scope_hook)(void *data);
151 void add_scope_hook(scope_hook *hook, void *data);
152 typedef void (func_hook)(const char *fn, struct expression *expr, void *data);
153 typedef void (implication_hook)(const char *fn, struct expression *call_expr,
154 struct expression *assign_expr, void *data);
155 typedef void (return_implies_hook)(struct expression *call_expr,
156 int param, char *key, char *value);
157 typedef int (implied_return_hook)(struct expression *call_expr, void *info, struct range_list **rl);
158 void add_function_hook(const char *look_for, func_hook *call_back, void *data);
160 void add_function_assign_hook(const char *look_for, func_hook *call_back,
161 void *info);
162 void add_implied_return_hook(const char *look_for,
163 implied_return_hook *call_back,
164 void *info);
165 void add_macro_assign_hook(const char *look_for, func_hook *call_back,
166 void *info);
167 void add_macro_assign_hook_extra(const char *look_for, func_hook *call_back,
168 void *info);
169 void return_implies_state(const char *look_for, long long start, long long end,
170 implication_hook *call_back, void *info);
171 void select_return_states_hook(int type, return_implies_hook *callback);
172 void select_return_states_before(void (*fn)(void));
173 void select_return_states_after(void (*fn)(void));
174 int get_implied_return(struct expression *expr, struct range_list **rl);
175 void allocate_hook_memory(void);
177 struct modification_data {
178 struct smatch_state *prev;
179 struct expression *cur;
182 typedef void (modification_hook)(struct sm_state *sm, struct expression *mod_expr);
183 void add_modification_hook(int owner, modification_hook *call_back);
184 void add_modification_hook_late(int owner, modification_hook *call_back);
185 struct smatch_state *get_modification_state(struct expression *expr);
187 int outside_of_function(void);
188 const char *get_filename(void);
189 const char *get_base_file(void);
190 char *get_function(void);
191 int get_lineno(void);
192 extern int final_pass;
193 extern struct symbol *cur_func_sym;
194 extern int option_debug;
195 extern int local_debug;
196 extern int option_info;
197 extern int option_spammy;
198 extern char *trace_variable;
199 extern struct stree *global_states;
200 int is_silenced_function(void);
202 /* smatch_impossible.c */
203 int is_impossible_path(void);
204 void set_path_impossible(void);
206 extern FILE *sm_outfd;
207 #define sm_printf(msg...) do { if (final_pass || option_debug || local_debug) fprintf(sm_outfd, msg); } while (0)
209 static inline void sm_prefix(void)
211 sm_printf("%s:%d %s() ", get_filename(), get_lineno(), get_function());
214 static inline void print_implied_debug_msg();
216 #define sm_msg(msg...) \
217 do { \
218 print_implied_debug_msg(); \
219 if (!option_debug && !final_pass && !local_debug) \
220 break; \
221 if (!option_info && is_silenced_function()) \
222 break; \
223 sm_prefix(); \
224 sm_printf(msg); \
225 sm_printf("\n"); \
226 } while (0)
228 #define local_debug(msg...) \
229 do { \
230 if (local_debug) \
231 sm_msg(msg); \
232 } while (0)
234 extern char *implied_debug_msg;
235 static inline void print_implied_debug_msg(void)
237 static struct symbol *last_printed = NULL;
239 if (!implied_debug_msg)
240 return;
241 if (last_printed == cur_func_sym)
242 return;
243 last_printed = cur_func_sym;
244 sm_msg("%s", implied_debug_msg);
247 #define sm_debug(msg...) do { if (option_debug) sm_printf(msg); } while (0)
249 #define sm_info(msg...) do { \
250 if (option_debug || (option_info && final_pass)) { \
251 sm_prefix(); \
252 sm_printf("info: "); \
253 sm_printf(msg); \
254 sm_printf("\n"); \
256 } while(0)
258 #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
260 struct smatch_state *get_state(int owner, const char *name, struct symbol *sym);
261 struct smatch_state *get_state_expr(int owner, struct expression *expr);
262 struct state_list *get_possible_states(int owner, const char *name,
263 struct symbol *sym);
264 struct state_list *get_possible_states_expr(int owner, struct expression *expr);
265 struct sm_state *set_state(int owner, const char *name, struct symbol *sym,
266 struct smatch_state *state);
267 struct sm_state *set_state_expr(int owner, struct expression *expr,
268 struct smatch_state *state);
269 void delete_state(int owner, const char *name, struct symbol *sym);
270 void delete_state_expr(int owner, struct expression *expr);
271 void __delete_all_states_sym(struct symbol *sym);
272 void set_true_false_states(int owner, const char *name, struct symbol *sym,
273 struct smatch_state *true_state,
274 struct smatch_state *false_state);
275 void set_true_false_states_expr(int owner, struct expression *expr,
276 struct smatch_state *true_state,
277 struct smatch_state *false_state);
279 struct stree *get_all_states_from_stree(int owner, struct stree *source);
280 struct stree *get_all_states_stree(int id);
281 struct stree *__get_cur_stree(void);
282 int is_reachable(void);
284 /* smatch_helper.c */
285 DECLARE_PTR_LIST(int_stack, int);
286 char *alloc_string(const char *str);
287 void free_string(char *str);
288 void append(char *dest, const char *data, int buff_len);
289 void remove_parens(char *str);
290 struct smatch_state *alloc_state_num(int num);
291 struct smatch_state *alloc_state_str(const char *name);
292 struct smatch_state *alloc_state_expr(struct expression *expr);
293 struct expression *get_argument_from_call_expr(struct expression_list *args,
294 int num);
296 char *expr_to_var(struct expression *expr);
297 struct symbol *expr_to_sym(struct expression *expr);
298 char *expr_to_str(struct expression *expr);
299 char *expr_to_str_sym(struct expression *expr,
300 struct symbol **sym_ptr);
301 char *expr_to_var_sym(struct expression *expr,
302 struct symbol **sym_ptr);
303 char *expr_to_known_chunk_sym(struct expression *expr, struct symbol **sym);
304 char *expr_to_chunk_sym_vsl(struct expression *expr, struct symbol **sym, struct var_sym_list **vsl);
305 int get_complication_score(struct expression *expr);
307 int sym_name_is(const char *name, struct expression *expr);
308 int get_const_value(struct expression *expr, sval_t *sval);
309 int get_value(struct expression *expr, sval_t *val);
310 int get_implied_value(struct expression *expr, sval_t *val);
311 int get_implied_min(struct expression *expr, sval_t *sval);
312 int get_implied_max(struct expression *expr, sval_t *val);
313 int get_hard_max(struct expression *expr, sval_t *sval);
314 int get_fuzzy_min(struct expression *expr, sval_t *min);
315 int get_fuzzy_max(struct expression *expr, sval_t *max);
316 int get_absolute_min(struct expression *expr, sval_t *sval);
317 int get_absolute_max(struct expression *expr, sval_t *sval);
318 int parse_call_math(struct expression *expr, char *math, sval_t *val);
319 int parse_call_math_rl(struct expression *call, char *math, struct range_list **rl);
320 char *get_value_in_terms_of_parameter_math(struct expression *expr);
321 char *get_value_in_terms_of_parameter_math_var_sym(const char *var, struct symbol *sym);
322 int is_zero(struct expression *expr);
323 int known_condition_true(struct expression *expr);
324 int known_condition_false(struct expression *expr);
325 int implied_condition_true(struct expression *expr);
326 int implied_condition_false(struct expression *expr);
327 int can_integer_overflow(struct symbol *type, struct expression *expr);
329 int is_array(struct expression *expr);
330 struct expression *get_array_base(struct expression *expr);
331 struct expression *get_array_offset(struct expression *expr);
332 const char *show_state(struct smatch_state *state);
333 struct statement *get_expression_statement(struct expression *expr);
334 struct expression *strip_parens(struct expression *expr);
335 struct expression *strip_expr(struct expression *expr);
336 void scoped_state(int my_id, const char *name, struct symbol *sym);
337 int is_error_return(struct expression *expr);
338 int getting_address(void);
339 int get_struct_and_member(struct expression *expr, const char **type, const char **member);
340 char *get_member_name(struct expression *expr);
341 char *get_fnptr_name(struct expression *expr);
342 int cmp_pos(struct position pos1, struct position pos2);
343 int positions_eq(struct position pos1, struct position pos2);
344 struct statement *get_current_statement(void);
345 struct statement *get_prev_statement(void);
346 int get_param_num_from_sym(struct symbol *sym);
347 int get_param_num(struct expression *expr);
348 int ms_since(struct timeval *start);
349 int parent_is_gone_var_sym(const char *name, struct symbol *sym);
350 int parent_is_gone(struct expression *expr);
351 int invert_op(int op);
352 int expr_equiv(struct expression *one, struct expression *two);
353 void push_int(struct int_stack **stack, int num);
354 int pop_int(struct int_stack **stack);
356 /* smatch_type.c */
357 struct symbol *get_real_base_type(struct symbol *sym);
358 int type_bytes(struct symbol *type);
359 int array_bytes(struct symbol *type);
360 struct symbol *get_pointer_type(struct expression *expr);
361 struct symbol *get_type(struct expression *expr);
362 int type_signed(struct symbol *base_type);
363 int expr_unsigned(struct expression *expr);
364 int expr_signed(struct expression *expr);
365 int returns_unsigned(struct symbol *base_type);
366 int is_pointer(struct expression *expr);
367 int returns_pointer(struct symbol *base_type);
368 sval_t sval_type_max(struct symbol *base_type);
369 sval_t sval_type_min(struct symbol *base_type);
370 int nr_bits(struct expression *expr);
371 int is_void_pointer(struct expression *expr);
372 int is_char_pointer(struct expression *expr);
373 int is_string(struct expression *expr);
374 int is_static(struct expression *expr);
375 int is_local_variable(struct expression *expr);
376 int types_equiv(struct symbol *one, struct symbol *two);
377 int fn_static(void);
378 const char *global_static();
379 struct symbol *cur_func_return_type(void);
380 struct symbol *get_arg_type(struct expression *fn, int arg);
381 struct symbol *get_member_type_from_key(struct expression *expr, char *key);
382 int is_struct(struct expression *expr);
383 char *type_to_str(struct symbol *type);
385 /* smatch_ignore.c */
386 void add_ignore(int owner, const char *name, struct symbol *sym);
387 int is_ignored(int owner, const char *name, struct symbol *sym);
389 /* smatch_var_sym */
390 struct var_sym *alloc_var_sym(const char *var, struct symbol *sym);
391 struct var_sym_list *expr_to_vsl(struct expression *expr);
392 void add_var_sym(struct var_sym_list **list, const char *var, struct symbol *sym);
393 void add_var_sym_expr(struct var_sym_list **list, struct expression *expr);
394 void del_var_sym(struct var_sym_list **list, const char *var, struct symbol *sym);
395 int in_var_sym_list(struct var_sym_list *list, const char *var, struct symbol *sym);
396 struct var_sym_list *clone_var_sym_list(struct var_sym_list *from_vsl);
397 void merge_var_sym_list(struct var_sym_list **dest, struct var_sym_list *src);
398 struct var_sym_list *combine_var_sym_lists(struct var_sym_list *one, struct var_sym_list *two);
399 int var_sym_lists_equiv(struct var_sym_list *one, struct var_sym_list *two);
400 void free_var_sym_list(struct var_sym_list **list);
401 void free_var_syms_and_list(struct var_sym_list **list);
403 /* smatch_tracker */
404 struct tracker *alloc_tracker(int owner, const char *name, struct symbol *sym);
405 void add_tracker(struct tracker_list **list, int owner, const char *name,
406 struct symbol *sym);
407 void add_tracker_expr(struct tracker_list **list, int owner, struct expression *expr);
408 void del_tracker(struct tracker_list **list, int owner, const char *name,
409 struct symbol *sym);
410 int in_tracker_list(struct tracker_list *list, int owner, const char *name,
411 struct symbol *sym);
412 void free_tracker_list(struct tracker_list **list);
413 void free_trackers_and_list(struct tracker_list **list);
415 /* smatch_conditions */
416 int in_condition(void);
418 /* smatch_flow.c */
420 extern int __in_fake_assign;
421 void smatch (int argc, char **argv);
422 int inside_loop(void);
423 int definitely_inside_loop(void);
424 struct expression *get_switch_expr(void);
425 int in_expression_statement(void);
426 void __process_post_op_stack(void);
427 void __split_expr(struct expression *expr);
428 void __split_label_stmt(struct statement *stmt);
429 void __split_stmt(struct statement *stmt);
430 extern int __in_function_def;
431 extern int option_assume_loops;
432 extern int option_two_passes;
433 extern int option_no_db;
434 extern int option_file_output;
435 extern int option_time;
436 extern struct expression_list *big_expression_stack;
437 extern struct expression_list *big_condition_stack;
438 extern struct statement_list *big_statement_stack;
439 int is_assigned_call(struct expression *expr);
440 int inlinable(struct expression *expr);
441 extern int __inline_call;
442 extern struct expression *__inline_fn;
443 extern int __in_pre_condition;
444 extern int __bail_on_rest_of_function;
445 extern struct statement *__prev_stmt;
446 extern struct statement *__cur_stmt;
447 extern struct statement *__next_stmt;
448 void init_fake_env(void);
449 void end_fake_env(void);
451 /* smatch_struct_assignment.c */
452 struct expression *get_faked_expression(void);
453 void __fake_struct_member_assignments(struct expression *expr);
455 /* smatch_project.c */
456 int is_no_inline_function(const char *function);
458 /* smatch_conditions */
459 void __split_whole_condition(struct expression *expr);
460 void __handle_logic(struct expression *expr);
461 int is_condition(struct expression *expr);
462 int __handle_condition_assigns(struct expression *expr);
463 int __handle_select_assigns(struct expression *expr);
464 int __handle_expr_statement_assigns(struct expression *expr);
466 /* smatch_implied.c */
467 extern int option_debug_implied;
468 extern int option_debug_related;
469 struct range_list_stack;
470 void param_limit_implications(struct expression *expr, int param, char *key, char *value);
471 struct stree *__implied_case_stree(struct expression *switch_expr,
472 struct range_list *case_rl,
473 struct range_list_stack **remaining_cases,
474 struct stree **raw_stree);
475 void overwrite_states_using_pool(struct sm_state *gate_sm, struct sm_state *pool_sm);
476 int assume(struct expression *expr);
477 void end_assume(void);
479 /* smatch_extras.c */
480 #define SMATCH_EXTRA 2 /* this is my_id from smatch extra set in smatch.c */
481 extern int RETURN_ID;
483 struct data_range {
484 sval_t min;
485 sval_t max;
488 extern long long valid_ptr_min, valid_ptr_max;
489 extern sval_t valid_ptr_min_sval, valid_ptr_max_sval;
490 static const sval_t array_min_sval = {
491 .type = &ptr_ctype,
492 {.value = 100000},
494 static const sval_t array_max_sval = {
495 .type = &ptr_ctype,
496 {.value = 199999},
498 static const sval_t text_seg_min = {
499 .type = &ptr_ctype,
500 {.value = 100000000},
502 static const sval_t text_seg_max = {
503 .type = &ptr_ctype,
504 {.value = 177777777},
506 static const sval_t data_seg_min = {
507 .type = &ptr_ctype,
508 {.value = 200000000},
510 static const sval_t data_seg_max = {
511 .type = &ptr_ctype,
512 {.value = 277777777},
514 static const sval_t bss_seg_min = {
515 .type = &ptr_ctype,
516 {.value = 300000000},
518 static const sval_t bss_seg_max = {
519 .type = &ptr_ctype,
520 {.value = 377777777},
522 static const sval_t stack_seg_min = {
523 .type = &ptr_ctype,
524 {.value = 400000000},
526 static const sval_t stack_seg_max = {
527 .type = &ptr_ctype,
528 {.value = 477777777},
530 static const sval_t kmalloc_seg_min = {
531 .type = &ptr_ctype,
532 {.value = 500000000},
534 static const sval_t kmalloc_seg_max = {
535 .type = &ptr_ctype,
536 {.value = 577777777},
538 static const sval_t vmalloc_seg_min = {
539 .type = &ptr_ctype,
540 {.value = 600000000},
542 static const sval_t vmalloc_seg_max = {
543 .type = &ptr_ctype,
544 {.value = 677777777},
546 static const sval_t fn_ptr_min = {
547 .type = &ptr_ctype,
548 {.value = 700000000},
550 static const sval_t fn_ptr_max = {
551 .type = &ptr_ctype,
552 {.value = 777777777},
555 char *get_other_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym);
556 char *map_call_to_other_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym);
557 char *map_long_to_short_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym);
558 char *map_long_to_short_name_sym_nostack(const char *name, struct symbol *sym, struct symbol **new_sym);
560 #define STRLEN_MAX_RET 1010101
562 /* smatch_absolute.c */
563 int get_absolute_min_helper(struct expression *expr, sval_t *sval);
564 int get_absolute_max_helper(struct expression *expr, sval_t *sval);
566 /* smatch_local_values.c */
567 int get_local_rl(struct expression *expr, struct range_list **rl);
568 int get_local_max_helper(struct expression *expr, sval_t *sval);
569 int get_local_min_helper(struct expression *expr, sval_t *sval);
571 /* smatch_type_value.c */
572 int get_db_type_rl(struct expression *expr, struct range_list **rl);
573 /* smatch_data_val.c */
574 int get_db_data_rl(struct expression *expr, struct range_list **rl);
576 /* smatch_states.c */
577 void __swap_cur_stree(struct stree *stree);
578 void __push_fake_cur_stree();
579 struct stree *__pop_fake_cur_stree();
580 void __free_fake_cur_stree();
581 void __set_fake_cur_stree_fast(struct stree *stree);
582 void __pop_fake_cur_stree_fast(void);
583 void __merge_stree_into_cur(struct stree *stree);
585 int unreachable(void);
586 void __set_sm(struct sm_state *sm);
587 void __set_sm_cur_stree(struct sm_state *sm);
588 void __set_sm_fake_stree(struct sm_state *sm);
589 void __set_true_false_sm(struct sm_state *true_state,
590 struct sm_state *false_state);
591 void nullify_path(void);
592 void __match_nullify_path_hook(const char *fn, struct expression *expr,
593 void *unused);
594 void __unnullify_path(void);
595 int __path_is_null(void);
596 void save_all_states(void);
597 void restore_all_states(void);
598 void free_goto_stack(void);
599 void clear_all_states(void);
601 struct sm_state *get_sm_state(int owner, const char *name,
602 struct symbol *sym);
603 struct sm_state *get_sm_state_expr(int owner, struct expression *expr);
604 void __push_true_states(void);
605 void __use_false_states(void);
606 void __discard_false_states(void);
607 void __merge_false_states(void);
608 void __merge_true_states(void);
610 void __negate_cond_stacks(void);
611 void __use_pre_cond_states(void);
612 void __use_cond_true_states(void);
613 void __use_cond_false_states(void);
614 void __push_cond_stacks(void);
615 void __fold_in_set_states(void);
616 void __free_set_states(void);
617 struct stree *__copy_cond_true_states(void);
618 struct stree *__copy_cond_false_states(void);
619 struct stree *__pop_cond_true_stack(void);
620 struct stree *__pop_cond_false_stack(void);
621 void __and_cond_states(void);
622 void __or_cond_states(void);
623 void __save_pre_cond_states(void);
624 void __discard_pre_cond_states(void);
625 struct stree *__get_true_states(void);
626 struct stree *__get_false_states(void);
627 void __use_cond_states(void);
628 extern struct state_list *__last_base_slist;
630 void __push_continues(void);
631 void __discard_continues(void);
632 void __process_continues(void);
633 void __merge_continues(void);
635 void __push_breaks(void);
636 void __process_breaks(void);
637 int __has_breaks(void);
638 void __merge_breaks(void);
639 void __use_breaks(void);
641 void __save_switch_states(struct expression *switch_expr);
642 void __discard_switches(void);
643 void __merge_switches(struct expression *switch_expr, struct range_list *case_rl);
644 void __push_default(void);
645 void __set_default(void);
646 int __pop_default(void);
648 void __push_conditions(void);
649 void __discard_conditions(void);
651 void __save_gotos(const char *name, struct symbol *sym);
652 void __merge_gotos(const char *name, struct symbol *sym);
654 void __print_cur_stree(void);
656 /* smatch_hooks.c */
657 void __pass_to_client(void *data, enum hook_type type);
658 void __pass_to_client_no_data(enum hook_type type);
659 void __pass_case_to_client(struct expression *switch_expr,
660 struct range_list *rl);
661 int __has_merge_function(int client_id);
662 struct smatch_state *__client_merge_function(int owner,
663 struct smatch_state *s1,
664 struct smatch_state *s2);
665 struct smatch_state *__client_unmatched_state_function(struct sm_state *sm);
666 void call_pre_merge_hook(struct sm_state *sm);
667 void __push_scope_hooks(void);
668 void __call_scope_hooks(void);
670 /* smatch_function_hooks.c */
671 void create_function_hook_hash(void);
672 void __match_initializer_call(struct symbol *sym);
674 /* smatch_db.c */
675 enum info_type {
676 INTERNAL = 0,
678 * Changing these numbers is a pain. Don't do it. If you ever use a
679 * number it can't be re-used right away so there may be gaps.
680 * We select these in order by type so if the order matters, then give
681 * it a number below 100-999,9000-9999 ranges. */
683 PARAM_CLEARED = 101,
684 PARAM_LIMIT = 103,
685 PARAM_FILTER = 104,
687 PARAM_VALUE = 1001,
688 BUF_SIZE = 1002,
689 USER_DATA = 1003,
690 CAPPED_DATA = 1004,
691 RETURN_VALUE = 1005,
692 DEREFERENCE = 1006,
693 RANGE_CAP = 1007,
694 LOCK_HELD = 1008,
695 LOCK_RELEASED = 1009,
696 ABSOLUTE_LIMITS = 1010,
697 PARAM_ADD = 1012,
698 PARAM_FREED = 1013,
699 DATA_SOURCE = 1014,
700 FUZZY_MAX = 1015,
701 STR_LEN = 1016,
702 ARRAY_LEN = 1017,
703 CAPABLE = 1018,
704 NS_CAPABLE = 1019,
705 CONTAINER = 1020,
706 CASTED_CALL = 1021,
707 TYPE_LINK = 1022,
708 UNTRACKED_PARAM = 1023,
709 CULL_PATH = 1024,
710 PARAM_SET = 1025,
711 PARAM_USED = 1026,
712 BYTE_UNITS = 1027,
713 COMPARE_LIMIT = 1028,
714 PARAM_COMPARE = 1029,
715 CONSTRAINT = 1031,
716 PASSES_TYPE = 1032,
717 CONSTRAINT_REQUIRED = 1033,
719 /* put random temporary stuff in the 7000-7999 range for testing */
720 USER_DATA3 = 8017,
721 USER_DATA3_SET = 9017,
722 NO_OVERFLOW = 8018,
723 NO_OVERFLOW_SIMPLE = 8019,
724 LOCKED = 8020,
725 UNLOCKED = 8021,
726 SET_FS = 8022,
727 ATOMIC_INC = 8023,
728 ATOMIC_DEC = 8024,
729 NO_SIDE_EFFECT = 8025,
730 FN_ARG_LINK = 8028,
731 DATA_VALUE = 8029,
732 ARRAYSIZE_ARG = 8033,
733 SIZEOF_ARG = 8034,
736 void debug_sql(const char *sql);
737 void debug_mem_sql(const char *sql);
738 void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type);
739 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm));
740 void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr));
741 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));
742 void select_call_implies_hook(int type, void (*callback)(struct expression *arg, char *key, char *value));
743 struct range_list *db_return_vals(struct expression *expr);
744 struct range_list *db_return_vals_from_str(const char *fn_name);
745 char *return_state_to_var_sym(struct expression *expr, int param, const char *key, struct symbol **sym);
746 char *get_chunk_from_key(struct expression *arg, char *key, struct symbol **sym, struct var_sym_list **vsl);
747 char *get_variable_from_key(struct expression *arg, const char *key, struct symbol **sym);
748 const char *state_name_to_param_name(const char *state_name, const char *param_name);
749 const char *get_param_name_var_sym(const char *name, struct symbol *sym);
750 const char *get_param_name(struct sm_state *sm);
751 char *get_data_info_name(struct expression *expr);
753 #define run_sql(call_back, data, sql...) \
754 do { \
755 char sql_txt[1024]; \
756 if (option_no_db) \
757 break; \
758 snprintf(sql_txt, 1024, sql); \
759 debug_sql(sql_txt); \
760 sql_exec(call_back, data, sql_txt); \
761 } while (0)
763 /* like run_sql() but for the in-memory database */
764 #define mem_sql(call_back, data, sql...) \
765 do { \
766 char sql_txt[1024]; \
768 snprintf(sql_txt, sizeof(sql_txt), sql); \
769 sm_debug("in-mem: %s\n", sql_txt); \
770 debug_mem_sql(sql_txt); \
771 sql_mem_exec(call_back, data, sql_txt); \
772 } while (0)
774 char *get_static_filter(struct symbol *sym);
776 void sql_insert_return_states(int return_id, const char *return_ranges,
777 int type, int param, const char *key, const char *value);
778 void sql_insert_caller_info(struct expression *call, int type, int param,
779 const char *key, const char *value);
780 void sql_insert_function_ptr(const char *fn, const char *struct_name);
781 void sql_insert_return_values(const char *return_values);
782 void sql_insert_call_implies(int type, int param, const char *key, const char *value);
783 void sql_insert_function_type_size(const char *member, const char *ranges);
784 void sql_insert_local_values(const char *name, const char *value);
785 void sql_insert_function_type_value(const char *type, const char *value);
786 void sql_insert_function_type(int param, const char *value);
787 void sql_insert_parameter_name(int param, const char *value);
788 void sql_insert_data_info(struct expression *data, int type, const char *value);
789 void sql_insert_data_info_var_sym(const char *var, struct symbol *sym, int type, const char *value);
790 void sql_save_constraint(const char *con);
791 void sql_save_constraint_required(const char *data, int op, const char *limit);
792 void sql_insert_fn_ptr_data_link(const char *ptr, const char *data);
793 void sql_insert_fn_data_link(struct expression *fn, int type, int param, const char *key, const char *value);
795 void sql_select_return_states(const char *cols, struct expression *call,
796 int (*callback)(void*, int, char**, char**), void *info);
797 void sql_select_call_implies(const char *cols, struct expression *call,
798 int (*callback)(void*, int, char**, char**));
800 void sql_exec(int (*callback)(void*, int, char**, char**), void *data, const char *sql);
801 void sql_mem_exec(int (*callback)(void*, int, char**, char**), void *data, const char *sql);
803 void open_smatch_db(void);
805 /* smatch_files.c */
806 int open_data_file(const char *filename);
807 struct token *get_tokens_file(const char *filename);
809 /* smatch.c */
810 extern char *option_debug_check;
811 extern char *option_project_str;
812 extern char *data_dir;
813 extern int option_no_data;
814 extern int option_full_path;
815 extern int option_param_mapper;
816 extern int option_call_tree;
817 extern int num_checks;
819 enum project_type {
820 PROJ_NONE,
821 PROJ_KERNEL,
822 PROJ_WINE,
824 extern enum project_type option_project;
825 const char *check_name(unsigned short id);
826 int id_from_name(const char *name);
829 /* smatch_buf_size.c */
830 int get_array_size(struct expression *expr);
831 int get_array_size_bytes(struct expression *expr);
832 int get_array_size_bytes_min(struct expression *expr);
833 int get_array_size_bytes_max(struct expression *expr);
834 struct range_list *get_array_size_bytes_rl(struct expression *expr);
835 int get_real_array_size(struct expression *expr);
836 /* smatch_strlen.c */
837 int get_implied_strlen(struct expression *expr, struct range_list **rl);
838 int get_size_from_strlen(struct expression *expr);
840 /* smatch_capped.c */
841 int is_capped(struct expression *expr);
842 int is_capped_var_sym(const char *name, struct symbol *sym);
844 /* check_user_data.c */
845 int is_user_macro(struct expression *expr);
846 int is_user_data(struct expression *expr);
847 int is_capped_user_data(struct expression *expr);
848 int implied_user_data(struct expression *expr, struct range_list **rl);
849 int get_user_rl(struct expression *expr, struct range_list **rl);
850 int get_user_rl_var_sym(const char *name, struct symbol *sym, struct range_list **rl);
852 /* check_locking.c */
853 void print_held_locks();
855 /* check_assigned_expr.c */
856 struct expression *get_assigned_expr(struct expression *expr);
857 struct expression *get_assigned_expr_name_sym(const char *name, struct symbol *sym);
858 /* smatch_return_to_param.c */
859 void __add_return_to_param_mapping(struct expression *assign, const char *return_string);
860 char *map_call_to_param_name_sym(struct expression *expr, struct symbol **sym);
862 /* smatch_comparison.c */
863 struct compare_data {
864 const char *var1;
865 struct var_sym_list *vsl1;
866 int comparison;
867 const char *var2;
868 struct var_sym_list *vsl2;
870 DECLARE_ALLOCATOR(compare_data);
871 struct smatch_state *alloc_compare_state(
872 const char *var1, struct var_sym_list *vsl1,
873 int comparison,
874 const char *var2, struct var_sym_list *vsl2);
875 int merge_comparisons(int one, int two);
876 int combine_comparisons(int left_compare, int right_compare);
877 int state_to_comparison(struct smatch_state *state);
878 struct smatch_state *merge_compare_states(struct smatch_state *s1, struct smatch_state *s2);
879 int get_comparison(struct expression *left, struct expression *right);
880 int get_comparison_strings(const char *one, const char *two);
881 int possible_comparison(struct expression *a, int comparison, struct expression *b);
882 struct state_list *get_all_comparisons(struct expression *expr);
883 struct state_list *get_all_possible_equal_comparisons(struct expression *expr);
884 void __add_return_comparison(struct expression *call, const char *range);
885 void __add_comparison_info(struct expression *expr, struct expression *call, const char *range);
886 char *get_printed_param_name(struct expression *call, const char *param_name, struct symbol *param_sym);
887 char *name_sym_to_param_comparison(const char *name, struct symbol *sym);
888 char *expr_equal_to_param(struct expression *expr, int ignore);
889 char *expr_lte_to_param(struct expression *expr, int ignore);
890 char *expr_param_comparison(struct expression *expr, int ignore);
891 int flip_comparison(int op);
892 int negate_comparison(int op);
893 int param_compare_limit_is_impossible(struct expression *expr, int left_param, char *left_key, char *value);
894 void filter_by_comparison(struct range_list **rl, int comparison, struct range_list *right);
895 struct sm_state *comparison_implication_hook(struct expression *expr,
896 struct state_list **true_stack,
897 struct state_list **false_stack);
898 void __compare_param_limit_hook(struct expression *left_expr, struct expression *right_expr,
899 const char *state_name,
900 struct smatch_state *true_state, struct smatch_state *false_state);
903 /* smatch_sval.c */
904 sval_t *sval_alloc(sval_t sval);
905 sval_t *sval_alloc_permanent(sval_t sval);
906 sval_t sval_blank(struct expression *expr);
907 sval_t sval_type_val(struct symbol *type, long long val);
908 sval_t sval_from_val(struct expression *expr, long long val);
909 int sval_unsigned(sval_t sval);
910 int sval_signed(sval_t sval);
911 int sval_bits(sval_t sval);
912 int sval_bits_used(sval_t sval);
913 int sval_is_negative(sval_t sval);
914 int sval_is_positive(sval_t sval);
915 int sval_is_min(sval_t sval);
916 int sval_is_max(sval_t sval);
917 int sval_is_a_min(sval_t sval);
918 int sval_is_a_max(sval_t sval);
919 int sval_is_negative_min(sval_t sval);
920 int sval_cmp_t(struct symbol *type, sval_t one, sval_t two);
921 int sval_cmp_val(sval_t one, long long val);
922 sval_t sval_min(sval_t one, sval_t two);
923 sval_t sval_max(sval_t one, sval_t two);
924 int sval_too_low(struct symbol *type, sval_t sval);
925 int sval_too_high(struct symbol *type, sval_t sval);
926 int sval_fits(struct symbol *type, sval_t sval);
927 sval_t sval_cast(struct symbol *type, sval_t sval);
928 sval_t sval_preop(sval_t sval, int op);
929 sval_t sval_binop(sval_t left, int op, sval_t right);
930 int sval_binop_overflows(sval_t left, int op, sval_t right);
931 unsigned long long fls_mask(unsigned long long uvalue);
932 unsigned long long sval_fls_mask(sval_t sval);
933 const char *sval_to_str(sval_t sval);
934 const char *sval_to_numstr(sval_t sval);
935 sval_t ll_to_sval(long long val);
937 /* smatch_string_list.c */
938 int list_has_string(struct string_list *str_list, const char *str);
939 void insert_string(struct string_list **str_list, const char *str);
940 struct string_list *clone_str_list(struct string_list *orig);
941 struct string_list *combine_string_lists(struct string_list *one, struct string_list *two);
943 /* smatch_start_states.c */
944 struct stree *get_start_states(void);
946 /* smatch_recurse.c */
947 int has_symbol(struct expression *expr, struct symbol *sym);
948 int has_variable(struct expression *expr, struct expression *var);
949 int has_inc_dec(struct expression *expr);
951 /* smatch_stored_conditions.c */
952 struct smatch_state *get_stored_condition(struct expression *expr);
953 struct expression_list *get_conditions(struct expression *expr);
954 struct sm_state *stored_condition_implication_hook(struct expression *expr,
955 struct state_list **true_stack,
956 struct state_list **false_stack);
958 /* check_string_len.c */
959 int get_formatted_string_size(struct expression *call, int arg);
961 /* smatch_param_set.c */
962 int param_was_set(struct expression *expr);
963 int param_was_set_var_sym(const char *name, struct symbol *sym);
964 /* smatch_param_filter.c */
965 int param_has_filter_data(struct sm_state *sm);
967 /* smatch_links.c */
968 void set_up_link_functions(int id, int linkid);
969 struct smatch_state *merge_link_states(struct smatch_state *s1, struct smatch_state *s2);
970 void store_link(int link_id, const char *name, struct symbol *sym, const char *link_name, struct symbol *link_sym);
972 /* smatch_auto_copy.c */
973 void set_auto_copy(int owner);
975 /* check_buf_comparison */
976 struct expression *get_size_variable(struct expression *buf);
978 /* smatch_untracked_param.c */
979 void mark_untracked(struct expression *expr, int param, const char *key, const char *value);
980 void add_untracked_param_hook(void (func)(struct expression *call, int param));
982 /* smatch_strings.c */
983 struct state_list *get_strings(struct expression *expr);
985 /* smatch_estate.c */
986 int estate_get_single_value(struct smatch_state *state, sval_t *sval);
988 /* smatch_address.c */
989 int get_address_rl(struct expression *expr, struct range_list **rl);
990 int get_member_offset(struct symbol *type, char *member_name);
991 int get_member_offset_from_deref(struct expression *expr);
993 /* for now this is in smatch_used_parameter.c */
994 void __get_state_hook(int owner, const char *name, struct symbol *sym);
996 /* smatch_buf_comparison.c */
997 int db_var_is_array_limit(struct expression *array, const char *name, struct var_sym_list *vsl);
999 struct stree *get_all_return_states(void);
1000 struct stree_stack *get_all_return_strees(void);
1001 int was_inced(const char *name, struct symbol *sym);
1003 /* smatch_constraints.c */
1004 char *get_constraint_str(struct expression *expr);
1005 struct constraint_list *get_constraints(struct expression *expr);
1006 char *unmet_constraint(struct expression *data, struct expression *offset);
1007 char *get_required_constraint(const char *data_str);
1009 static inline int type_bits(struct symbol *type)
1011 if (!type)
1012 return 0;
1013 if (type->type == SYM_PTR) /* Sparse doesn't set this for &pointers */
1014 return bits_in_pointer;
1015 if (type->type == SYM_ARRAY)
1016 return bits_in_pointer;
1017 if (!type->examined)
1018 examine_symbol_type(type);
1019 return type->bit_size;
1022 static inline int type_unsigned(struct symbol *base_type)
1024 if (!base_type)
1025 return 0;
1026 if (base_type->ctype.modifiers & MOD_UNSIGNED)
1027 return 1;
1028 return 0;
1031 static inline int type_positive_bits(struct symbol *type)
1033 if (!type)
1034 return 0;
1035 if (type->type == SYM_ARRAY)
1036 return bits_in_pointer - 1;
1037 if (type_unsigned(type))
1038 return type_bits(type);
1039 return type_bits(type) - 1;
1042 static inline int sval_positive_bits(sval_t sval)
1044 return type_positive_bits(sval.type);
1048 * Returns -1 if one is smaller, 0 if they are the same and 1 if two is larger.
1050 static inline int sval_cmp(sval_t one, sval_t two)
1052 struct symbol *type;
1054 type = one.type;
1055 if (sval_positive_bits(two) > sval_positive_bits(one))
1056 type = two.type;
1057 if (type_bits(type) < 31)
1058 type = &int_ctype;
1060 one = sval_cast(type, one);
1061 two = sval_cast(type, two);
1063 if (type_unsigned(type)) {
1064 if (one.uvalue < two.uvalue)
1065 return -1;
1066 if (one.uvalue == two.uvalue)
1067 return 0;
1068 return 1;
1070 /* fix me handle type promotion and unsigned values */
1071 if (one.value < two.value)
1072 return -1;
1073 if (one.value == two.value)
1074 return 0;
1075 return 1;
1078 #endif /* !SMATCH_H_ */