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