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