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