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