implied: handle ->implied states differently
[smatch.git] / smatch.h
blob1268d7f132ce9193613ac0b586e803128d9da832
1 /*
2 * Copyright (C) 2006 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
18 #ifndef SMATCH_H_
19 # define SMATCH_H_
21 #include <stdio.h>
22 #include <string.h>
23 #include <limits.h>
24 #include <sys/time.h>
25 #include "lib.h"
26 #include "allocate.h"
27 #include "scope.h"
28 #include "parse.h"
29 #include "expression.h"
30 #include "avl.h"
32 typedef struct {
33 struct symbol *type;
34 union {
35 long long value;
36 unsigned long long uvalue;
38 } sval_t;
40 struct smatch_state {
41 const char *name;
42 void *data;
44 #define STATE(_x) static struct smatch_state _x = { .name = #_x }
45 extern struct smatch_state undefined;
46 extern struct smatch_state ghost;
47 extern struct smatch_state merged;
48 extern struct smatch_state true_state;
49 extern struct smatch_state false_state;
50 DECLARE_ALLOCATOR(smatch_state);
52 static inline void *INT_PTR(int i)
54 return (void *)(long)i;
57 static inline int PTR_INT(void *p)
59 return (int)(long)p;
62 struct tracker {
63 char *name;
64 struct symbol *sym;
65 unsigned short owner;
67 DECLARE_ALLOCATOR(tracker);
68 DECLARE_PTR_LIST(tracker_list, struct tracker);
69 DECLARE_PTR_LIST(stree_stack, struct stree);
71 /* The first 3 struct members must match struct tracker */
72 struct sm_state {
73 const char *name;
74 struct symbol *sym;
75 unsigned short owner;
76 unsigned short merged:1;
77 unsigned short implied:1;
78 unsigned int nr_children;
79 unsigned int line;
80 struct smatch_state *state;
81 struct stree *pool;
82 struct sm_state *left;
83 struct sm_state *right;
84 struct state_list *possible;
87 struct var_sym {
88 char *var;
89 struct symbol *sym;
91 DECLARE_ALLOCATOR(var_sym);
92 DECLARE_PTR_LIST(var_sym_list, struct var_sym);
94 enum hook_type {
95 EXPR_HOOK,
96 STMT_HOOK,
97 STMT_HOOK_AFTER,
98 SYM_HOOK,
99 STRING_HOOK,
100 DECLARATION_HOOK,
101 ASSIGNMENT_HOOK,
102 RAW_ASSIGNMENT_HOOK,
103 GLOBAL_ASSIGNMENT_HOOK,
104 LOGIC_HOOK,
105 CONDITION_HOOK,
106 PRELOOP_HOOK,
107 SELECT_HOOK,
108 WHOLE_CONDITION_HOOK,
109 FUNCTION_CALL_HOOK,
110 CALL_HOOK_AFTER_INLINE,
111 FUNCTION_CALL_HOOK_AFTER_DB,
112 CALL_ASSIGNMENT_HOOK,
113 MACRO_ASSIGNMENT_HOOK,
114 BINOP_HOOK,
115 OP_HOOK,
116 DEREF_HOOK,
117 CASE_HOOK,
118 ASM_HOOK,
119 CAST_HOOK,
120 SIZEOF_HOOK,
121 BASE_HOOK,
122 FUNC_DEF_HOOK,
123 AFTER_DEF_HOOK,
124 END_FUNC_HOOK,
125 AFTER_FUNC_HOOK,
126 RETURN_HOOK,
127 INLINE_FN_START,
128 INLINE_FN_END,
129 END_FILE_HOOK,
130 NUM_HOOKS,
133 #define TRUE 1
134 #define FALSE 0
136 struct range_list;
138 void add_hook(void *func, enum hook_type type);
139 typedef struct smatch_state *(merge_func_t)(struct smatch_state *s1, struct smatch_state *s2);
140 typedef struct smatch_state *(unmatched_func_t)(struct sm_state *state);
141 void add_merge_hook(int client_id, merge_func_t *func);
142 void add_unmatched_state_hook(int client_id, unmatched_func_t *func);
143 void add_pre_merge_hook(int client_id, void (*hook)(struct sm_state *sm));
144 typedef void (scope_hook)(void *data);
145 void add_scope_hook(scope_hook *hook, void *data);
146 typedef void (func_hook)(const char *fn, struct expression *expr, void *data);
147 typedef void (implication_hook)(const char *fn, struct expression *call_expr,
148 struct expression *assign_expr, void *data);
149 typedef void (return_implies_hook)(struct expression *call_expr,
150 int param, char *key, char *value);
151 typedef int (implied_return_hook)(struct expression *call_expr, void *info, struct range_list **rl);
152 void add_function_hook(const char *look_for, func_hook *call_back, void *data);
154 void add_function_assign_hook(const char *look_for, func_hook *call_back,
155 void *info);
156 void add_implied_return_hook(const char *look_for,
157 implied_return_hook *call_back,
158 void *info);
159 void add_macro_assign_hook(const char *look_for, func_hook *call_back,
160 void *info);
161 void add_macro_assign_hook_extra(const char *look_for, func_hook *call_back,
162 void *info);
163 void return_implies_state(const char *look_for, long long start, long long end,
164 implication_hook *call_back, void *info);
165 void select_return_states_hook(int type, return_implies_hook *callback);
166 void select_return_states_before(void (*fn)(void));
167 void select_return_states_after(void (*fn)(void));
168 int get_implied_return(struct expression *expr, struct range_list **rl);
169 void allocate_hook_memory(void);
171 struct modification_data {
172 struct smatch_state *prev;
173 struct expression *cur;
176 typedef void (modification_hook)(struct sm_state *sm, struct expression *mod_expr);
177 void add_modification_hook(int owner, modification_hook *call_back);
178 void add_indirect_modification_hook(int owner, modification_hook *call_back);
179 void add_modification_hook_late(int owner, modification_hook *call_back);
180 void add_indirect_modification_hook_late(int owner, modification_hook *call_back);
181 struct smatch_state *get_modification_state(struct expression *expr);
183 int outside_of_function(void);
184 const char *get_filename(void);
185 const char *get_base_file(void);
186 char *get_function(void);
187 int get_lineno(void);
188 extern int final_pass;
189 extern struct symbol *cur_func_sym;
190 extern int option_debug;
191 extern int local_debug;
192 extern int option_info;
193 extern char *trace_variable;
194 extern struct stree *global_states;
195 int is_silenced_function(void);
197 /* smatch_impossible.c */
198 int is_impossible_path(void);
200 extern FILE *sm_outfd;
201 #define sm_printf(msg...) do { if (final_pass || option_debug || local_debug) fprintf(sm_outfd, msg); } while (0)
203 static inline void sm_prefix(void)
205 sm_printf("%s:%d %s() ", get_filename(), get_lineno(), get_function());
208 static inline void print_implied_debug_msg();
210 #define sm_msg(msg...) \
211 do { \
212 print_implied_debug_msg(); \
213 if (!option_debug && !final_pass && !local_debug) \
214 break; \
215 if (!option_info && is_silenced_function()) \
216 break; \
217 sm_prefix(); \
218 sm_printf(msg); \
219 sm_printf("\n"); \
220 } while (0)
222 #define local_debug(msg...) \
223 do { \
224 if (local_debug) \
225 sm_msg(msg); \
226 } while (0)
228 extern char *implied_debug_msg;
229 static inline void print_implied_debug_msg(void)
231 static struct symbol *last_printed = NULL;
233 if (!implied_debug_msg)
234 return;
235 if (last_printed == cur_func_sym)
236 return;
237 last_printed = cur_func_sym;
238 sm_msg("%s", implied_debug_msg);
241 #define sm_debug(msg...) do { if (option_debug) sm_printf(msg); } while (0)
243 #define sm_info(msg...) do { \
244 if (option_debug || (option_info && final_pass)) { \
245 sm_prefix(); \
246 sm_printf("info: "); \
247 sm_printf(msg); \
248 sm_printf("\n"); \
250 } while(0)
252 #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
254 struct smatch_state *get_state(int owner, const char *name, struct symbol *sym);
255 struct smatch_state *get_state_expr(int owner, struct expression *expr);
256 struct state_list *get_possible_states(int owner, const char *name,
257 struct symbol *sym);
258 struct state_list *get_possible_states_expr(int owner, struct expression *expr);
259 struct sm_state *set_state(int owner, const char *name, struct symbol *sym,
260 struct smatch_state *state);
261 struct sm_state *set_state_expr(int owner, struct expression *expr,
262 struct smatch_state *state);
263 void delete_state(int owner, const char *name, struct symbol *sym);
264 void delete_state_expr(int owner, struct expression *expr);
265 void __delete_all_states_sym(struct symbol *sym);
266 void set_true_false_states(int owner, const char *name, struct symbol *sym,
267 struct smatch_state *true_state,
268 struct smatch_state *false_state);
269 void set_true_false_states_expr(int owner, struct expression *expr,
270 struct smatch_state *true_state,
271 struct smatch_state *false_state);
273 struct stree *get_all_states_from_stree(int owner, struct stree *source);
274 struct stree *get_all_states_stree(int id);
275 struct stree *__get_cur_stree(void);
276 int is_reachable(void);
278 /* smatch_helper.c */
279 DECLARE_PTR_LIST(int_stack, int);
280 char *alloc_string(const char *str);
281 void free_string(char *str);
282 void append(char *dest, const char *data, int buff_len);
283 void remove_parens(char *str);
284 struct smatch_state *alloc_state_num(int num);
285 struct smatch_state *alloc_state_str(const char *name);
286 struct smatch_state *alloc_state_expr(struct expression *expr);
287 struct expression *get_argument_from_call_expr(struct expression_list *args,
288 int num);
290 char *expr_to_var(struct expression *expr);
291 struct symbol *expr_to_sym(struct expression *expr);
292 char *expr_to_str(struct expression *expr);
293 char *expr_to_str_sym(struct expression *expr,
294 struct symbol **sym_ptr);
295 char *expr_to_var_sym(struct expression *expr,
296 struct symbol **sym_ptr);
297 char *expr_to_known_chunk_sym(struct expression *expr, struct symbol **sym);
298 char *expr_to_chunk_sym_vsl(struct expression *expr, struct symbol **sym, struct var_sym_list **vsl);
299 int get_complication_score(struct expression *expr);
301 int sym_name_is(const char *name, struct expression *expr);
302 int get_const_value(struct expression *expr, sval_t *sval);
303 int get_value(struct expression *expr, sval_t *val);
304 int get_implied_value(struct expression *expr, sval_t *val);
305 int get_implied_min(struct expression *expr, sval_t *sval);
306 int get_implied_max(struct expression *expr, sval_t *val);
307 int get_hard_max(struct expression *expr, sval_t *sval);
308 int get_fuzzy_min(struct expression *expr, sval_t *min);
309 int get_fuzzy_max(struct expression *expr, sval_t *max);
310 int get_absolute_min(struct expression *expr, sval_t *sval);
311 int get_absolute_max(struct expression *expr, sval_t *sval);
312 int parse_call_math(struct expression *expr, char *math, sval_t *val);
313 int parse_call_math_rl(struct expression *call, char *math, struct range_list **rl);
314 char *get_value_in_terms_of_parameter_math(struct expression *expr);
315 char *get_value_in_terms_of_parameter_math_var_sym(const char *var, struct symbol *sym);
316 int is_zero(struct expression *expr);
317 int known_condition_true(struct expression *expr);
318 int known_condition_false(struct expression *expr);
319 int implied_condition_true(struct expression *expr);
320 int implied_condition_false(struct expression *expr);
321 int can_integer_overflow(struct symbol *type, struct expression *expr);
323 int is_array(struct expression *expr);
324 struct expression *get_array_base(struct expression *expr);
325 struct expression *get_array_offset(struct expression *expr);
326 const char *show_state(struct smatch_state *state);
327 struct statement *get_expression_statement(struct expression *expr);
328 struct expression *strip_parens(struct expression *expr);
329 struct expression *strip_expr(struct expression *expr);
330 void scoped_state(int my_id, const char *name, struct symbol *sym);
331 int is_error_return(struct expression *expr);
332 int getting_address(void);
333 char *get_member_name(struct expression *expr);
334 char *get_fnptr_name(struct expression *expr);
335 int cmp_pos(struct position pos1, struct position pos2);
336 int positions_eq(struct position pos1, struct position pos2);
337 struct statement *get_current_statement(void);
338 struct statement *get_prev_statement(void);
339 int get_param_num_from_sym(struct symbol *sym);
340 int get_param_num(struct expression *expr);
341 int ms_since(struct timeval *start);
342 int parent_is_gone_var_sym(const char *name, struct symbol *sym);
343 int parent_is_gone(struct expression *expr);
344 int invert_op(int op);
345 int expr_equiv(struct expression *one, struct expression *two);
346 void push_int(struct int_stack **stack, int num);
347 int pop_int(struct int_stack **stack);
349 /* smatch_type.c */
350 struct symbol *get_real_base_type(struct symbol *sym);
351 int type_bytes(struct symbol *type);
352 struct symbol *get_pointer_type(struct expression *expr);
353 struct symbol *get_type(struct expression *expr);
354 int type_signed(struct symbol *base_type);
355 int expr_unsigned(struct expression *expr);
356 int expr_signed(struct expression *expr);
357 int returns_unsigned(struct symbol *base_type);
358 int is_pointer(struct expression *expr);
359 int returns_pointer(struct symbol *base_type);
360 sval_t sval_type_max(struct symbol *base_type);
361 sval_t sval_type_min(struct symbol *base_type);
362 int nr_bits(struct expression *expr);
363 int is_void_pointer(struct expression *expr);
364 int is_char_pointer(struct expression *expr);
365 int is_string(struct expression *expr);
366 int is_static(struct expression *expr);
367 int is_local_variable(struct expression *expr);
368 int types_equiv(struct symbol *one, struct symbol *two);
369 int fn_static(void);
370 const char *global_static();
371 struct symbol *cur_func_return_type(void);
372 struct symbol *get_arg_type(struct expression *fn, int arg);
373 struct symbol *get_member_type_from_key(struct expression *expr, char *key);
374 int is_struct(struct expression *expr);
375 char *type_to_str(struct symbol *type);
377 /* smatch_ignore.c */
378 void add_ignore(int owner, const char *name, struct symbol *sym);
379 int is_ignored(int owner, const char *name, struct symbol *sym);
381 /* smatch_var_sym */
382 struct var_sym *alloc_var_sym(const char *var, struct symbol *sym);
383 struct var_sym_list *expr_to_vsl(struct expression *expr);
384 void add_var_sym(struct var_sym_list **list, const char *var, struct symbol *sym);
385 void add_var_sym_expr(struct var_sym_list **list, struct expression *expr);
386 void del_var_sym(struct var_sym_list **list, const char *var, struct symbol *sym);
387 int in_var_sym_list(struct var_sym_list *list, const char *var, struct symbol *sym);
388 struct var_sym_list *clone_var_sym_list(struct var_sym_list *from_vsl);
389 void merge_var_sym_list(struct var_sym_list **dest, struct var_sym_list *src);
390 struct var_sym_list *combine_var_sym_lists(struct var_sym_list *one, struct var_sym_list *two);
391 void free_var_sym_list(struct var_sym_list **list);
392 void free_var_syms_and_list(struct var_sym_list **list);
394 /* smatch_tracker */
395 struct tracker *alloc_tracker(int owner, const char *name, struct symbol *sym);
396 void add_tracker(struct tracker_list **list, int owner, const char *name,
397 struct symbol *sym);
398 void add_tracker_expr(struct tracker_list **list, int owner, struct expression *expr);
399 void del_tracker(struct tracker_list **list, int owner, const char *name,
400 struct symbol *sym);
401 int in_tracker_list(struct tracker_list *list, int owner, const char *name,
402 struct symbol *sym);
403 void free_tracker_list(struct tracker_list **list);
404 void free_trackers_and_list(struct tracker_list **list);
406 /* smatch_conditions */
407 int in_condition(void);
409 /* smatch_flow.c */
411 extern int __in_fake_assign;
412 void smatch (int argc, char **argv);
413 int inside_loop(void);
414 int definitely_inside_loop(void);
415 struct expression *get_switch_expr(void);
416 int in_expression_statement(void);
417 void __process_post_op_stack(void);
418 void set_parent_expr(struct expression *expr, struct expression *parent);
419 void __split_expr(struct expression *expr);
420 void __split_label_stmt(struct statement *stmt);
421 void __split_stmt(struct statement *stmt);
422 extern int __in_function_def;
423 extern int option_assume_loops;
424 extern int option_two_passes;
425 extern int option_no_db;
426 extern int option_file_output;
427 extern int option_time;
428 extern struct expression_list *big_expression_stack;
429 extern struct expression_list *big_condition_stack;
430 extern struct statement_list *big_statement_stack;
431 int is_assigned_call(struct expression *expr);
432 int inlinable(struct expression *expr);
433 extern int __inline_call;
434 extern struct expression *__inline_fn;
435 extern int __in_pre_condition;
436 extern int __bail_on_rest_of_function;
437 extern struct statement *__prev_stmt;
438 extern struct statement *__cur_stmt;
439 extern struct statement *__next_stmt;
441 /* smatch_struct_assignment.c */
442 struct expression *get_faked_expression(void);
443 void __fake_struct_member_assignments(struct expression *expr);
445 /* smatch_project.c */
446 int is_no_inline_function(const char *function);
448 /* smatch_conditions */
449 void __split_whole_condition(struct expression *expr);
450 void __handle_logic(struct expression *expr);
451 int is_condition(struct expression *expr);
452 int __handle_condition_assigns(struct expression *expr);
453 int __handle_select_assigns(struct expression *expr);
454 int __handle_expr_statement_assigns(struct expression *expr);
456 /* smatch_implied.c */
457 extern int option_debug_implied;
458 extern int option_debug_related;
459 struct range_list_stack;
460 void param_limit_implications(struct expression *expr, int param, char *key, char *value);
461 struct stree *__implied_case_stree(struct expression *switch_expr,
462 struct range_list *case_rl,
463 struct range_list_stack **remaining_cases,
464 struct stree **raw_stree);
465 void overwrite_states_using_pool(struct sm_state *gate_sm, struct sm_state *pool_sm);
466 int assume(struct expression *expr);
467 void end_assume(void);
469 /* smatch_extras.c */
470 #define SMATCH_EXTRA 1 /* this is my_id from smatch extra set in smatch.c */
471 extern int RETURN_ID;
473 struct data_range {
474 sval_t min;
475 sval_t max;
478 extern long long valid_ptr_min, valid_ptr_max;
479 extern sval_t valid_ptr_min_sval, valid_ptr_max_sval;
480 static const sval_t array_min_sval = {
481 .type = &ptr_ctype,
482 {.value = 100000},
484 static const sval_t array_max_sval = {
485 .type = &ptr_ctype,
486 {.value = 199999},
488 static const sval_t text_seg_min = {
489 .type = &ptr_ctype,
490 {.value = 100000000},
492 static const sval_t text_seg_max = {
493 .type = &ptr_ctype,
494 {.value = 177777777},
496 static const sval_t data_seg_min = {
497 .type = &ptr_ctype,
498 {.value = 200000000},
500 static const sval_t data_seg_max = {
501 .type = &ptr_ctype,
502 {.value = 277777777},
504 static const sval_t bss_seg_min = {
505 .type = &ptr_ctype,
506 {.value = 300000000},
508 static const sval_t bss_seg_max = {
509 .type = &ptr_ctype,
510 {.value = 377777777},
512 static const sval_t stack_seg_min = {
513 .type = &ptr_ctype,
514 {.value = 400000000},
516 static const sval_t stack_seg_max = {
517 .type = &ptr_ctype,
518 {.value = 477777777},
520 static const sval_t kmalloc_seg_min = {
521 .type = &ptr_ctype,
522 {.value = 500000000},
524 static const sval_t kmalloc_seg_max = {
525 .type = &ptr_ctype,
526 {.value = 577777777},
528 static const sval_t vmalloc_seg_min = {
529 .type = &ptr_ctype,
530 {.value = 600000000},
532 static const sval_t vmalloc_seg_max = {
533 .type = &ptr_ctype,
534 {.value = 677777777},
537 char *get_other_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym);
539 #define STRLEN_MAX_RET 1010101
541 /* smatch_absolute.c */
542 int get_absolute_min_helper(struct expression *expr, sval_t *sval);
543 int get_absolute_max_helper(struct expression *expr, sval_t *sval);
545 /* smatch_local_values.c */
546 int get_local_rl(struct expression *expr, struct range_list **rl);
547 int get_local_max_helper(struct expression *expr, sval_t *sval);
548 int get_local_min_helper(struct expression *expr, sval_t *sval);
550 /* smatch_type_value.c */
551 int get_db_type_rl(struct expression *expr, struct range_list **rl);
553 /* smatch_states.c */
554 void __swap_cur_stree(struct stree *stree);
555 void __push_fake_cur_stree();
556 struct stree *__pop_fake_cur_stree();
557 void __free_fake_cur_stree();
558 void __set_fake_cur_stree_fast(struct stree *stree);
559 void __pop_fake_cur_stree_fast(void);
560 void __merge_stree_into_cur(struct stree *stree);
562 int unreachable(void);
563 void __set_sm(struct sm_state *sm);
564 void __set_sm_cur_stree(struct sm_state *sm);
565 void __set_sm_fake_stree(struct sm_state *sm);
566 void __set_true_false_sm(struct sm_state *true_state,
567 struct sm_state *false_state);
568 void nullify_path(void);
569 void __match_nullify_path_hook(const char *fn, struct expression *expr,
570 void *unused);
571 void __unnullify_path(void);
572 int __path_is_null(void);
573 void save_all_states(void);
574 void restore_all_states(void);
575 void free_goto_stack(void);
576 void clear_all_states(void);
578 struct sm_state *get_sm_state(int owner, const char *name,
579 struct symbol *sym);
580 struct sm_state *get_sm_state_expr(int owner, struct expression *expr);
581 void __push_true_states(void);
582 void __use_false_states(void);
583 void __discard_false_states(void);
584 void __merge_false_states(void);
585 void __merge_true_states(void);
587 void __negate_cond_stacks(void);
588 void __use_pre_cond_states(void);
589 void __use_cond_true_states(void);
590 void __use_cond_false_states(void);
591 void __push_cond_stacks(void);
592 void __fold_in_set_states(void);
593 void __free_set_states(void);
594 struct stree *__copy_cond_true_states(void);
595 struct stree *__copy_cond_false_states(void);
596 struct stree *__pop_cond_true_stack(void);
597 struct stree *__pop_cond_false_stack(void);
598 void __and_cond_states(void);
599 void __or_cond_states(void);
600 void __save_pre_cond_states(void);
601 void __discard_pre_cond_states(void);
602 struct stree *__get_true_states(void);
603 struct stree *__get_false_states(void);
604 void __use_cond_states(void);
605 extern struct state_list *__last_base_slist;
607 void __push_continues(void);
608 void __discard_continues(void);
609 void __process_continues(void);
610 void __merge_continues(void);
612 void __push_breaks(void);
613 void __process_breaks(void);
614 int __has_breaks(void);
615 void __merge_breaks(void);
616 void __use_breaks(void);
618 void __save_switch_states(struct expression *switch_expr);
619 void __discard_switches(void);
620 void __merge_switches(struct expression *switch_expr, struct range_list *case_rl);
621 void __push_default(void);
622 void __set_default(void);
623 int __pop_default(void);
625 void __push_conditions(void);
626 void __discard_conditions(void);
628 void __save_gotos(const char *name);
629 void __merge_gotos(const char *name);
631 void __print_cur_stree(void);
633 /* smatch_hooks.c */
634 void __pass_to_client(void *data, enum hook_type type);
635 void __pass_to_client_no_data(enum hook_type type);
636 void __pass_case_to_client(struct expression *switch_expr,
637 struct range_list *rl);
638 int __has_merge_function(int client_id);
639 struct smatch_state *__client_merge_function(int owner,
640 struct smatch_state *s1,
641 struct smatch_state *s2);
642 struct smatch_state *__client_unmatched_state_function(struct sm_state *sm);
643 void call_pre_merge_hook(struct sm_state *sm);
644 void __push_scope_hooks(void);
645 void __call_scope_hooks(void);
647 /* smatch_function_hooks.c */
648 void create_function_hook_hash(void);
649 void __match_initializer_call(struct symbol *sym);
651 /* smatch_db.c */
652 enum info_type {
653 INTERNAL = 0,
655 * Changing these numbers is a pain. Don't do it. If you ever use a
656 * number it can't be re-used right away so there may be gaps.
657 * We select these in order by type so if the order matters, then give
658 * it a number below 100-999,9000-9999 ranges. */
660 PARAM_CLEARED = 101,
661 PARAM_LIMIT = 103,
662 PARAM_FILTER = 104,
664 PARAM_VALUE = 1001,
665 BUF_SIZE = 1002,
666 USER_DATA = 1003,
667 CAPPED_DATA = 1004,
668 RETURN_VALUE = 1005,
669 DEREFERENCE = 1006,
670 RANGE_CAP = 1007,
671 LOCK_HELD = 1008,
672 LOCK_RELEASED = 1009,
673 ABSOLUTE_LIMITS = 1010,
674 PARAM_ADD = 1012,
675 PARAM_FREED = 1013,
676 DATA_SOURCE = 1014,
677 FUZZY_MAX = 1015,
678 STR_LEN = 1016,
679 ARRAY_LEN = 1017,
680 CAPABLE = 1018,
681 NS_CAPABLE = 1019,
682 TYPE_LINK = 1022,
683 UNTRACKED_PARAM = 1023,
684 CULL_PATH = 1024,
685 PARAM_SET = 1025,
686 PARAM_USED = 1026,
687 PARAM_COMPARE = 1028,
689 /* put random temporary stuff in the 7000-7999 range for testing */
690 USER_DATA3 = 8017,
691 USER_DATA_SET = 9018,
692 USER_DATA_LIMITED = 9019,
695 void debug_sql(const char *sql);
696 void debug_mem_sql(const char *sql);
697 void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type);
698 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm));
699 void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr));
700 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));
701 void select_call_implies_hook(int type, void (*callback)(struct expression *arg, char *key, char *value));
702 struct range_list *db_return_vals(struct expression *expr);
703 struct range_list *db_return_vals_from_str(const char *fn_name);
704 char *return_state_to_var_sym(struct expression *expr, int param, const char *key, struct symbol **sym);
705 char *get_chunk_from_key(struct expression *arg, char *key, struct symbol **sym, struct var_sym_list **vsl);
706 char *get_variable_from_key(struct expression *arg, const char *key, struct symbol **sym);
707 const char *get_param_name_var_sym(const char *name, struct symbol *sym);
708 const char *get_param_name(struct sm_state *sm);
709 char *get_data_info_name(struct expression *expr);
711 #define run_sql(call_back, data, sql...) \
712 do { \
713 char sql_txt[1024]; \
714 if (option_no_db) \
715 break; \
716 snprintf(sql_txt, 1024, sql); \
717 debug_sql(sql_txt); \
718 sql_exec(call_back, data, sql_txt); \
719 } while (0)
721 /* like run_sql() but for the in-memory database */
722 #define mem_sql(call_back, data, sql...) \
723 do { \
724 char sql_txt[1024]; \
726 snprintf(sql_txt, sizeof(sql_txt), sql); \
727 sm_debug("in-mem: %s\n", sql_txt); \
728 debug_mem_sql(sql_txt); \
729 sql_mem_exec(call_back, data, sql_txt); \
730 } while (0)
732 char *get_static_filter(struct symbol *sym);
734 void sql_insert_return_states(int return_id, const char *return_ranges,
735 int type, int param, const char *key, const char *value);
736 void sql_insert_caller_info(struct expression *call, int type, int param,
737 const char *key, const char *value);
738 void sql_insert_function_ptr(const char *fn, const char *struct_name);
739 void sql_insert_return_values(const char *return_values);
740 void sql_insert_call_implies(int type, int param, const char *key, const char *value);
741 void sql_insert_function_type_size(const char *member, const char *ranges);
742 void sql_insert_local_values(const char *name, const char *value);
743 void sql_insert_function_type_value(const char *type, const char *value);
744 void sql_insert_function_type_info(int param, const char *value);
745 void sql_insert_data_info(struct expression *data, int type, const char *value);
747 void sql_select_return_states(const char *cols, struct expression *call,
748 int (*callback)(void*, int, char**, char**), void *info);
749 void sql_select_caller_info(const char *cols, struct symbol *sym,
750 int (*callback)(void*, int, char**, char**));
751 void sql_select_call_implies(const char *cols, struct expression *call,
752 int (*callback)(void*, int, char**, char**));
754 void sql_exec(int (*callback)(void*, int, char**, char**), void *data, const char *sql);
755 void sql_mem_exec(int (*callback)(void*, int, char**, char**), void *data, const char *sql);
757 void open_smatch_db(void);
759 /* smatch_files.c */
760 int open_data_file(const char *filename);
761 struct token *get_tokens_file(const char *filename);
763 /* smatch.c */
764 extern char *option_debug_check;
765 extern char *option_project_str;
766 extern char *data_dir;
767 extern int option_no_data;
768 extern int option_spammy;
769 extern int option_full_path;
770 extern int option_param_mapper;
771 extern int option_call_tree;
772 extern int num_checks;
774 enum project_type {
775 PROJ_NONE,
776 PROJ_KERNEL,
777 PROJ_WINE,
779 extern enum project_type option_project;
780 const char *check_name(unsigned short id);
783 /* smatch_buf_size.c */
784 int get_array_size(struct expression *expr);
785 int get_array_size_bytes(struct expression *expr);
786 int get_array_size_bytes_min(struct expression *expr);
787 int get_array_size_bytes_max(struct expression *expr);
788 struct range_list *get_array_size_bytes_rl(struct expression *expr);
789 int get_real_array_size(struct expression *expr);
790 /* smatch_strlen.c */
791 int get_implied_strlen(struct expression *expr, struct range_list **rl);
792 int get_size_from_strlen(struct expression *expr);
794 /* smatch_capped.c */
795 int is_capped(struct expression *expr);
796 int is_capped_var_sym(const char *name, struct symbol *sym);
798 /* check_user_data.c */
799 int is_user_macro(struct expression *expr);
800 int is_user_data(struct expression *expr);
801 int is_capped_user_data(struct expression *expr);
802 int implied_user_data(struct expression *expr, struct range_list **rl);
803 int get_user_rl(struct expression *expr, struct range_list **rl);
804 int get_user_rl_var_sym(const char *name, struct symbol *sym, struct range_list **rl);
806 /* check_locking.c */
807 void print_held_locks();
809 /* check_assigned_expr.c */
810 struct expression *get_assigned_expr(struct expression *expr);
811 struct expression *get_assigned_expr_name_sym(const char *name, struct symbol *sym);
813 /* smatch_comparison.c */
814 struct compare_data {
815 const char *var1;
816 struct var_sym_list *vsl1;
817 int comparison;
818 const char *var2;
819 struct var_sym_list *vsl2;
821 int get_comparison(struct expression *left, struct expression *right);
822 int get_comparison_strings(const char *one, const char *two);
823 int possible_comparison(struct expression *a, int comparison, struct expression *b);
824 struct state_list *get_all_comparisons(struct expression *expr);
825 struct state_list *get_all_possible_equal_comparisons(struct expression *expr);
826 void __add_comparison_info(struct expression *expr, struct expression *call, const char *range);
827 char *name_sym_to_param_comparison(const char *name, struct symbol *sym);
828 char *expr_equal_to_param(struct expression *expr, int ignore);
829 char *expr_lte_to_param(struct expression *expr, int ignore);
830 char *expr_param_comparison(struct expression *expr, int ignore);
831 int flip_comparison(int op);
832 int negate_comparison(int op);
833 void filter_by_comparison(struct range_list **rl, int comparison, struct range_list *right);
834 struct sm_state *comparison_implication_hook(struct expression *expr,
835 struct state_list **true_stack,
836 struct state_list **false_stack);
838 /* smatch_sval.c */
839 sval_t *sval_alloc(sval_t sval);
840 sval_t *sval_alloc_permanent(sval_t sval);
841 sval_t sval_blank(struct expression *expr);
842 sval_t sval_type_val(struct symbol *type, long long val);
843 sval_t sval_from_val(struct expression *expr, long long val);
844 int sval_unsigned(sval_t sval);
845 int sval_signed(sval_t sval);
846 int sval_bits(sval_t sval);
847 int sval_bits_used(sval_t sval);
848 int sval_is_negative(sval_t sval);
849 int sval_is_positive(sval_t sval);
850 int sval_is_min(sval_t sval);
851 int sval_is_max(sval_t sval);
852 int sval_is_a_min(sval_t sval);
853 int sval_is_a_max(sval_t sval);
854 int sval_is_negative_min(sval_t sval);
855 int sval_cmp_t(struct symbol *type, sval_t one, sval_t two);
856 int sval_cmp_val(sval_t one, long long val);
857 sval_t sval_min(sval_t one, sval_t two);
858 sval_t sval_max(sval_t one, sval_t two);
859 int sval_too_low(struct symbol *type, sval_t sval);
860 int sval_too_high(struct symbol *type, sval_t sval);
861 int sval_fits(struct symbol *type, sval_t sval);
862 sval_t sval_cast(struct symbol *type, sval_t sval);
863 sval_t sval_preop(sval_t sval, int op);
864 sval_t sval_binop(sval_t left, int op, sval_t right);
865 int sval_binop_overflows(sval_t left, int op, sval_t right);
866 unsigned long long fls_mask(unsigned long long uvalue);
867 unsigned long long sval_fls_mask(sval_t sval);
868 const char *sval_to_str(sval_t sval);
869 const char *sval_to_numstr(sval_t sval);
870 sval_t ll_to_sval(long long val);
872 /* smatch_string_list.c */
873 int list_has_string(struct string_list *str_list, const char *str);
874 void insert_string(struct string_list **str_list, const char *str);
875 struct string_list *clone_str_list(struct string_list *orig);
876 struct string_list *combine_string_lists(struct string_list *one, struct string_list *two);
878 /* smatch_start_states.c */
879 struct stree *get_start_states(void);
881 /* smatch_recurse.c */
882 int has_symbol(struct expression *expr, struct symbol *sym);
883 int has_variable(struct expression *expr, struct expression *var);
884 int has_inc_dec(struct expression *expr);
886 /* smatch_stored_conditions.c */
887 struct smatch_state *get_stored_condition(struct expression *expr);
888 struct sm_state *stored_condition_implication_hook(struct expression *expr,
889 struct state_list **true_stack,
890 struct state_list **false_stack);
892 /* check_string_len.c */
893 int get_formatted_string_size(struct expression *call, int arg);
895 /* smatch_param_set.c */
896 int param_was_set(struct expression *expr);
897 int param_was_set_var_sym(const char *name, struct symbol *sym);
898 /* smatch_param_filter.c */
899 int param_has_filter_data(struct sm_state *sm);
901 /* smatch_links.c */
902 void set_up_link_functions(int id, int linkid);
903 struct smatch_state *merge_link_states(struct smatch_state *s1, struct smatch_state *s2);
904 void store_link(int link_id, const char *name, struct symbol *sym, const char *link_name, struct symbol *link_sym);
906 /* smatch_auto_copy.c */
907 void set_auto_copy(int owner);
909 /* check_buf_comparison */
910 struct expression *get_size_variable(struct expression *buf);
912 /* smatch_untracked_param.c */
913 void add_untracked_param_hook(void (func)(struct expression *call, int param));
915 /* smatch_impossible.c */
916 int is_impossible_path(void);
918 /* smatch_strings.c */
919 struct state_list *get_strings(struct expression *expr);
921 /* smatch_estate.c */
922 int estate_get_single_value(struct smatch_state *state, sval_t *sval);
924 /* smatch_address.c */
925 int get_address_rl(struct expression *expr, struct range_list **rl);
926 int get_member_offset(struct symbol *type, char *member_name);
927 int get_member_offset_from_deref(struct expression *expr);
929 /* for now this is in smatch_used_parameter.c */
930 void __get_state_hook(int owner, const char *name, struct symbol *sym);
932 /* smatch_buf_comparison.c */
933 int db_var_is_array_limit(struct expression *array, const char *name, struct var_sym_list *vsl);
935 struct stree *get_all_return_states(void);
936 struct stree_stack *get_all_return_strees(void);
938 static inline int type_bits(struct symbol *type)
940 if (!type)
941 return 0;
942 if (type->type == SYM_PTR) /* Sparse doesn't set this for &pointers */
943 return bits_in_pointer;
944 if (!type->examined)
945 examine_symbol_type(type);
946 return type->bit_size;
949 static inline int type_unsigned(struct symbol *base_type)
951 if (!base_type)
952 return 0;
953 if (base_type->ctype.modifiers & MOD_UNSIGNED)
954 return 1;
955 return 0;
958 static inline int type_positive_bits(struct symbol *type)
960 if (!type)
961 return 0;
962 // if (type->type == SYM_PTR)
963 // return bits_in_pointer;
964 if (type_unsigned(type))
965 return type_bits(type);
966 return type_bits(type) - 1;
969 static inline int sval_positive_bits(sval_t sval)
971 return type_positive_bits(sval.type);
975 * Returns -1 if one is smaller, 0 if they are the same and 1 if two is larger.
977 static inline int sval_cmp(sval_t one, sval_t two)
979 struct symbol *type;
981 type = one.type;
982 if (sval_positive_bits(two) > sval_positive_bits(one))
983 type = two.type;
984 if (type_bits(type) < 31)
985 type = &int_ctype;
987 one = sval_cast(type, one);
988 two = sval_cast(type, two);
990 if (type_unsigned(type)) {
991 if (one.uvalue < two.uvalue)
992 return -1;
993 if (one.uvalue == two.uvalue)
994 return 0;
995 return 1;
997 /* fix me handle type promotion and unsigned values */
998 if (one.value < two.value)
999 return -1;
1000 if (one.value == two.value)
1001 return 0;
1002 return 1;
1005 #endif /* !SMATCH_H_ */