stored_conditions: ignore known conditions more thoroughly
[smatch.git] / smatch.h
blob63b826e377129232e68241ad8ec30af737128e4b
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 <float.h>
25 #include <sys/time.h>
26 #include <sqlite3.h>
27 #include "lib.h"
28 #include "allocate.h"
29 #include "scope.h"
30 #include "parse.h"
31 #include "expression.h"
32 #include "avl.h"
33 #include "smatch_constants.h"
35 typedef long long mtag_t;
37 struct smatch_state {
38 const char *name;
39 void *data;
41 #define STATE(_x) static struct smatch_state _x = { .name = #_x }
42 extern struct smatch_state undefined;
43 extern struct smatch_state merged;
44 extern struct smatch_state true_state;
45 extern struct smatch_state false_state;
46 DECLARE_ALLOCATOR(smatch_state);
48 static inline void *INT_PTR(int i)
50 return (void *)(long)i;
53 static inline int PTR_INT(void *p)
55 return (int)(long)p;
58 struct tracker {
59 char *name;
60 struct symbol *sym;
61 unsigned short owner;
63 DECLARE_ALLOCATOR(tracker);
64 DECLARE_PTR_LIST(tracker_list, struct tracker);
65 DECLARE_PTR_LIST(stree_stack, struct stree);
67 /* The first 3 struct members must match struct tracker */
68 struct sm_state {
69 const char *name;
70 struct symbol *sym;
71 unsigned short owner;
72 unsigned short merged:1;
73 unsigned short leaf:1;
74 unsigned int line;
75 struct smatch_state *state;
76 struct stree *pool;
77 struct sm_state *left;
78 struct sm_state *right;
79 struct state_list *possible;
82 struct var_sym {
83 char *var;
84 struct symbol *sym;
86 DECLARE_ALLOCATOR(var_sym);
87 DECLARE_PTR_LIST(var_sym_list, struct var_sym);
89 struct constraint {
90 int op;
91 int id;
93 DECLARE_PTR_LIST(constraint_list, struct constraint);
95 struct alloc_info {
96 const char *fn;
97 int size_param, nr;
99 extern struct alloc_info *alloc_funcs;
101 struct bit_info {
102 unsigned long long set;
103 unsigned long long possible;
106 enum hook_type {
107 EXPR_HOOK,
108 EXPR_HOOK_AFTER,
109 STMT_HOOK,
110 STMT_HOOK_AFTER,
111 SYM_HOOK,
112 STRING_HOOK,
113 DECLARATION_HOOK,
114 DECLARATION_HOOK_AFTER,
115 ASSIGNMENT_HOOK,
116 ASSIGNMENT_HOOK_AFTER,
117 RAW_ASSIGNMENT_HOOK,
118 GLOBAL_ASSIGNMENT_HOOK,
119 LOGIC_HOOK,
120 CONDITION_HOOK,
121 PRELOOP_HOOK,
122 SELECT_HOOK,
123 WHOLE_CONDITION_HOOK,
124 FUNCTION_CALL_HOOK_BEFORE,
125 FUNCTION_CALL_HOOK,
126 CALL_HOOK_AFTER_INLINE,
127 FUNCTION_CALL_HOOK_AFTER_DB,
128 CALL_ASSIGNMENT_HOOK,
129 MACRO_ASSIGNMENT_HOOK,
130 BINOP_HOOK,
131 OP_HOOK,
132 DEREF_HOOK,
133 CASE_HOOK,
134 ASM_HOOK,
135 CAST_HOOK,
136 SIZEOF_HOOK,
137 BASE_HOOK,
138 FUNC_DEF_HOOK,
139 AFTER_DEF_HOOK,
140 END_FUNC_HOOK,
141 AFTER_FUNC_HOOK,
142 RETURN_HOOK,
143 INLINE_FN_START,
144 INLINE_FN_END,
145 END_FILE_HOOK,
146 NUM_HOOKS,
149 #define TRUE 1
150 #define FALSE 0
152 struct range_list;
154 void add_hook(void *func, enum hook_type type);
155 typedef struct smatch_state *(merge_func_t)(struct smatch_state *s1, struct smatch_state *s2);
156 typedef struct smatch_state *(unmatched_func_t)(struct sm_state *state);
157 void add_merge_hook(int client_id, merge_func_t *func);
158 void add_unmatched_state_hook(int client_id, unmatched_func_t *func);
159 void add_pre_merge_hook(int client_id, void (*hook)(struct sm_state *cur, struct sm_state *other));
160 typedef void (scope_hook)(void *data);
161 void add_scope_hook(scope_hook *hook, void *data);
162 typedef void (param_key_hook)(struct expression *expr, const char *name, struct symbol *sym, void *data);
163 typedef void (func_hook)(const char *fn, struct expression *expr, void *data);
164 typedef void (implication_hook)(const char *fn, struct expression *call_expr,
165 struct expression *assign_expr, void *data);
166 typedef void (return_implies_hook)(struct expression *call_expr,
167 int param, char *key, char *value);
168 typedef int (implied_return_hook)(struct expression *call_expr, void *info, struct range_list **rl);
169 void add_function_hook(const char *look_for, func_hook *call_back, void *data);
170 void add_function_hook_late(const char *look_for, func_hook *call_back, void *info);
172 void add_function_assign_hook(const char *look_for, func_hook *call_back,
173 void *info);
174 void register_func_hooks_from_file(const char *file,
175 func_hook *call_back, void *info);
176 void register_assign_hooks_from_file(const char *file,
177 func_hook *call_back, void *info);
178 void add_implied_return_hook(const char *look_for,
179 implied_return_hook *call_back,
180 void *info);
181 void add_macro_assign_hook(const char *look_for, func_hook *call_back,
182 void *info);
183 void add_macro_assign_hook_extra(const char *look_for, func_hook *call_back,
184 void *info);
185 void return_implies_state(const char *look_for, long long start, long long end,
186 implication_hook *call_back, void *info);
187 void return_implies_state_sval(const char *look_for, sval_t start, sval_t end,
188 implication_hook *call_back, void *info);
189 void return_implies_exact(const char *look_for, sval_t start, sval_t end,
190 implication_hook *call_back, void *info);
191 struct range_list *get_range_implications(const char *fn);
192 void select_return_states_hook(int type, return_implies_hook *callback);
193 void select_return_states_before(void (*fn)(void));
194 void select_return_states_after(void (*fn)(void));
195 void add_function_param_key_hook(const char *look_for, param_key_hook *call_back,
196 int param, const char *key, void *info);
197 void add_function_param_key_hook_late(const char *look_for, param_key_hook *call_back,
198 int param, const char *key, void *info);
199 void return_implies_param_key(const char *look_for, sval_t start, sval_t end,
200 param_key_hook *call_back,
201 int param, const char *key, void *info);
202 void return_implies_param_key_exact(const char *look_for, sval_t start, sval_t end,
203 param_key_hook *call_back,
204 int param, const char *key, void *info);
205 void select_return_param_key(int type, param_key_hook *callback);
206 int get_implied_return(struct expression *expr, struct range_list **rl);
207 void allocate_hook_memory(void);
208 void allocate_tracker_array(int num_checks);
210 struct modification_data {
211 struct smatch_state *prev;
212 struct expression *cur;
215 typedef void (modification_hook)(struct sm_state *sm, struct expression *mod_expr);
216 bool is_sub_member(const char *name, struct symbol *sym, struct sm_state *sm);
217 void add_modification_hook(int owner, modification_hook *call_back);
218 void add_modification_hook_late(int owner, modification_hook *call_back);
219 struct smatch_state *get_modification_state(struct expression *expr);
221 int outside_of_function(void);
222 const char *get_filename(void);
223 const char *get_base_file(void);
224 char *get_function(void);
225 int get_lineno(void);
226 extern int final_pass;
227 extern struct symbol *cur_func_sym;
228 extern int option_debug;
229 extern int local_debug;
230 extern int debug_db;
231 extern bool implied_debug;
232 bool debug_implied(void);
233 bool debug_on(const char *check_name, const char *var);
234 extern int option_info;
235 extern int option_spammy;
236 extern int option_pedantic;
237 extern int option_print_names;
238 extern char *trace_variable;
239 extern struct stree *global_states;
240 void set_function_skipped(void);
241 int is_skipped_function(void);
242 int is_silenced_function(void);
243 extern bool implications_off;
245 /* smatch_impossible.c */
246 int is_impossible_path(void);
247 void set_path_impossible(void);
249 extern FILE *sm_outfd;
250 extern FILE *sql_outfd;
251 extern FILE *caller_info_fd;
252 extern int sm_nr_checks;
253 extern int sm_nr_errors;
256 * How to use these routines:
258 * sm_fatal(): an internal error of some kind that should immediately exit
259 * sm_ierror(): an internal error
260 * sm_perror(): an internal error from parsing input source
261 * sm_error(): an error from input source
262 * sm_warning(): a warning from input source
263 * sm_info(): info message (from option_info)
264 * sm_debug(): debug message
265 * sm_msg(): other message (please avoid using this)
268 #define sm_printf(msg...) do { \
269 if (final_pass || option_debug || local_debug || debug_db) \
270 fprintf(sm_outfd, msg); \
271 } while (0)
273 static inline void sm_prefix(void)
275 sm_printf("%s:%d %s() ", get_filename(), get_lineno(), get_function());
276 if (option_info || !option_print_names)
277 return;
278 sm_printf("[smatch.%s] ", __CHECKNAME__);
281 static inline void print_implied_debug_msg();
283 extern bool __silence_warnings_for_stmt;
285 #define sm_print_msg(type, msg...) \
286 do { \
287 print_implied_debug_msg(); \
288 if (!final_pass && !option_debug && !local_debug && !debug_db) \
289 break; \
290 if (__silence_warnings_for_stmt && !option_debug && !local_debug) \
291 break; \
292 if (!option_info && is_silenced_function()) \
293 break; \
294 sm_prefix(); \
295 if (type == 1) { \
296 sm_printf("warn: "); \
297 sm_nr_checks++; \
298 } else if (type == 2) { \
299 sm_printf("error: "); \
300 sm_nr_checks++; \
301 } else if (type == 3) { \
302 sm_printf("parse error: "); \
303 sm_nr_errors++; \
305 sm_printf(msg); \
306 sm_printf("\n"); \
307 } while (0)
309 #define sm_msg(msg...) do { sm_print_msg(0, msg); } while (0)
311 extern char *implied_debug_msg;
312 static inline void print_implied_debug_msg(void)
314 static struct symbol *last_printed = NULL;
316 if (!implied_debug_msg)
317 return;
318 if (last_printed == cur_func_sym)
319 return;
320 last_printed = cur_func_sym;
321 sm_msg("%s", implied_debug_msg);
324 #define sm_debug(msg...) do { if (option_debug) sm_printf(msg); } while (0)
325 #define db_debug(msg...) do { if (option_debug || debug_db) sm_printf(msg); } while (0)
327 #define sm_info(msg...) do { \
328 if (option_debug || (option_info && final_pass)) { \
329 sm_prefix(); \
330 sm_printf("info: "); \
331 sm_printf(msg); \
332 sm_printf("\n"); \
334 } while(0)
336 #define sm_warning(msg...) do { sm_print_msg(1, msg); } while (0)
337 #define sm_warning_line(line, msg...) do { \
338 int __orig = __smatch_lineno; \
339 __smatch_lineno = line; \
340 sm_print_msg(1, msg); \
341 __smatch_lineno = __orig; \
342 } while (0)
343 #define sm_error(msg...) do { sm_print_msg(2, msg); } while (0)
344 #define sm_perror(msg...) do { sm_print_msg(3, msg); } while (0)
345 #define sm_spam(msg...) do { if (option_pedantic) sm_print_msg(1, msg); } while (0)
347 static inline void sm_fatal(const char *fmt, ...)
349 va_list args;
351 va_start(args, fmt);
352 vfprintf(sm_outfd, fmt, args);
353 va_end(args);
355 fprintf(sm_outfd, "\n");
357 exit(1);
360 static inline void sm_ierror(const char *fmt, ...)
362 va_list args;
364 sm_nr_errors++;
366 fprintf(sm_outfd, "internal error: ");
368 va_start(args, fmt);
369 vfprintf(sm_outfd, fmt, args);
370 va_end(args);
372 fprintf(sm_outfd, "\n");
374 #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
376 bool has_states(struct stree *stree, int owner);
377 struct smatch_state *__get_state(int owner, const char *name, struct symbol *sym);
378 struct smatch_state *get_state(int owner, const char *name, struct symbol *sym);
379 struct smatch_state *get_state_expr(int owner, struct expression *expr);
380 bool has_possible_state(int owner, const char *name, struct symbol *sym, struct smatch_state *state);
381 bool expr_has_possible_state(int owner, struct expression *expr, struct smatch_state *state);
382 struct state_list *get_possible_states(int owner, const char *name,
383 struct symbol *sym);
384 struct state_list *get_possible_states_expr(int owner, struct expression *expr);
385 struct sm_state *set_state(int owner, const char *name, struct symbol *sym,
386 struct smatch_state *state);
387 struct sm_state *set_state_expr(int owner, struct expression *expr,
388 struct smatch_state *state);
389 void __delete_state(int owner, const char *name, struct symbol *sym);
390 void __delete_all_states_sym(struct symbol *sym);
391 void set_true_false_states(int owner, const char *name, struct symbol *sym,
392 struct smatch_state *true_state,
393 struct smatch_state *false_state);
394 void set_true_false_states_expr(int owner, struct expression *expr,
395 struct smatch_state *true_state,
396 struct smatch_state *false_state);
398 struct stree *get_all_states_from_stree(int owner, struct stree *source);
399 struct stree *get_all_states_stree(int id);
400 struct stree *__get_cur_stree(void);
401 int is_reachable(void);
402 void add_get_state_hook(void (*fn)(int owner, const char *name, struct symbol *sym));
404 /* smatch_ssa.c */
405 char *ssa_name(const char *name);
406 void set_ssa_state(int owner, const char *name, struct symbol *sym, struct smatch_state *state);
407 void update_ssa_state(int owner, const char *name, struct symbol *sym,
408 struct smatch_state *state);
409 void set_ssa_state_expr(int owner, struct expression *expr, struct smatch_state *state);
410 struct sm_state *get_ssa_sm_state(int owner, const char *name, struct symbol *sym);
411 struct sm_state *get_ssa_sm_state_expr(int owner, struct expression *expr);
412 struct smatch_state *get_ssa_state(int owner, const char *name, struct symbol *sym);
413 struct smatch_state *get_ssa_state_expr(int owner, struct expression *expr);
415 /* smatch_helper.c */
416 DECLARE_PTR_LIST(int_stack, int);
417 char *alloc_string(const char *str);
418 char *alloc_string_newline(const char *str);
419 void free_string(char *str);
420 void remove_parens(char *str);
421 struct smatch_state *alloc_state_num(int num);
422 struct smatch_state *alloc_state_str(const char *name);
423 struct smatch_state *merge_str_state(struct smatch_state *s1, struct smatch_state *s2);
424 struct smatch_state *alloc_state_expr(struct expression *expr);
425 struct expression *get_argument_from_call_expr(struct expression_list *args,
426 int num);
427 struct expression *get_array_expr(struct expression *expr);
429 char *expr_to_var(struct expression *expr);
430 struct symbol *expr_to_sym(struct expression *expr);
431 char *expr_to_str(struct expression *expr);
432 char *expr_to_str_sym(struct expression *expr,
433 struct symbol **sym_ptr);
434 char *expr_to_var_sym(struct expression *expr,
435 struct symbol **sym_ptr);
436 char *expr_to_known_chunk_sym(struct expression *expr, struct symbol **sym);
437 char *expr_to_chunk_sym_vsl(struct expression *expr, struct symbol **sym, struct var_sym_list **vsl);
438 int get_complication_score(struct expression *expr);
440 int sym_name_is(const char *name, struct expression *expr);
441 int get_const_value(struct expression *expr, sval_t *sval);
442 int get_value(struct expression *expr, sval_t *val);
443 int get_implied_value(struct expression *expr, sval_t *val);
444 int get_implied_value_fast(struct expression *expr, sval_t *sval);
445 int get_implied_min(struct expression *expr, sval_t *sval);
446 int get_implied_max(struct expression *expr, sval_t *val);
447 int get_hard_max(struct expression *expr, sval_t *sval);
448 int get_fuzzy_min(struct expression *expr, sval_t *min);
449 int get_fuzzy_max(struct expression *expr, sval_t *max);
450 int get_absolute_min(struct expression *expr, sval_t *sval);
451 int get_absolute_max(struct expression *expr, sval_t *sval);
452 int parse_call_math(struct expression *expr, char *math, sval_t *val);
453 int parse_call_math_rl(struct expression *call, const char *math, struct range_list **rl);
454 const char *get_allocation_math(struct expression *expr);
455 char *get_value_in_terms_of_parameter_math(struct expression *expr);
456 char *get_value_in_terms_of_parameter_math_var_sym(const char *var, struct symbol *sym);
457 int expr_is_zero(struct expression *expr);
458 int known_condition_true(struct expression *expr);
459 int known_condition_false(struct expression *expr);
460 int implied_condition_true(struct expression *expr);
461 int implied_condition_false(struct expression *expr);
462 int can_integer_overflow(struct symbol *type, struct expression *expr);
463 void clear_math_cache(void);
464 void set_fast_math_only(void);
465 void clear_fast_math_only(void);
467 int is_array(struct expression *expr);
468 struct expression *get_array_base(struct expression *expr);
469 struct expression *get_array_offset(struct expression *expr);
470 const char *show_state(struct smatch_state *state);
471 struct statement *get_expression_statement(struct expression *expr);
472 struct expression *strip_parens(struct expression *expr);
473 struct expression *strip_expr(struct expression *expr);
474 struct expression *strip_expr_set_parent(struct expression *expr);
475 void scoped_state(int my_id, const char *name, struct symbol *sym);
476 int is_error_return(struct expression *expr);
477 int getting_address(struct expression *expr);
478 int get_struct_and_member(struct expression *expr, const char **type, const char **member);
479 char *get_member_name(struct expression *expr);
480 char *get_fnptr_name(struct expression *expr);
481 int cmp_pos(struct position pos1, struct position pos2);
482 int positions_eq(struct position pos1, struct position pos2);
483 struct statement *get_current_statement(void);
484 struct statement *get_prev_statement(void);
485 struct expression *get_last_expr_from_expression_stmt(struct expression *expr);
487 #define RETURN_VAR -1
488 #define LOCAL_SCOPE -2
489 #define FILE_SCOPE -3
490 #define GLOBAL_SCOPE -4
491 #define UNKNOWN_SCOPE -5
493 char *swap_names(const char *orig, const char *remove, const char *add);
494 char *get_param_var_sym_var_sym(const char *name, struct symbol *sym, struct expression *ret_expr, struct symbol **sym_p);
495 char *get_param_name_sym(struct expression *expr, struct symbol **sym_p);
496 int get_param_key_from_var_sym(const char *name, struct symbol *sym,
497 struct expression *ret_expr,
498 const char **key);
499 int get_param_key_from_sm(struct sm_state *sm, struct expression *ret_expr,
500 const char **key);
501 int map_to_param(const char *name, struct symbol *sym);
502 int get_param_num_from_sym(struct symbol *sym);
503 int get_param_num(struct expression *expr);
504 struct symbol *get_param_sym_from_num(int num);
506 int ms_since(struct timeval *start);
507 int parent_is_gone_var_sym(const char *name, struct symbol *sym);
508 int parent_is_gone(struct expression *expr);
509 bool is_noderef_ptr(struct expression *expr);
510 int invert_op(int op);
511 int op_remove_assign(int op);
512 int expr_equiv(struct expression *one, struct expression *two);
513 void push_int(struct int_stack **stack, int num);
514 int pop_int(struct int_stack **stack);
516 /* smatch_type.c */
517 struct symbol *get_real_base_type(struct symbol *sym);
518 int type_bytes(struct symbol *type);
519 int array_bytes(struct symbol *type);
520 struct symbol *get_pointer_type(struct expression *expr);
521 struct symbol *get_type(struct expression *expr);
522 struct symbol *get_comparison_type(struct expression *expr);
523 struct symbol *get_final_type(struct expression *expr);
524 struct symbol *get_promoted_type(struct symbol *left, struct symbol *right);
525 int type_signed(struct symbol *base_type);
526 int expr_unsigned(struct expression *expr);
527 int expr_signed(struct expression *expr);
528 int returns_unsigned(struct symbol *base_type);
529 int is_pointer(struct expression *expr);
530 int returns_pointer(struct symbol *base_type);
531 sval_t sval_type_max(struct symbol *base_type);
532 sval_t sval_type_min(struct symbol *base_type);
533 int nr_bits(struct expression *expr);
534 int is_void_pointer(struct expression *expr);
535 int is_char_pointer(struct expression *expr);
536 int is_string(struct expression *expr);
537 bool is_struct_ptr(struct symbol *type);
538 int is_static(struct expression *expr);
539 bool is_local_variable(struct expression *expr);
540 int types_equiv(struct symbol *one, struct symbol *two);
541 bool type_fits(struct symbol *type, struct symbol *test);
542 int fn_static(void);
543 const char *global_static();
544 struct symbol *cur_func_return_type(void);
545 struct symbol *get_arg_type(struct expression *fn, int arg);
546 struct symbol *get_member_type_from_key(struct expression *expr, const char *key);
547 struct symbol *get_arg_type_from_key(struct expression *fn, int param, struct expression *arg, const char *key);
548 int is_struct(struct expression *expr);
549 char *type_to_str(struct symbol *type);
551 /* smatch_ignore.c */
552 void add_ignore(int owner, const char *name, struct symbol *sym);
553 int is_ignored(int owner, const char *name, struct symbol *sym);
554 void add_ignore_expr(int owner, struct expression *expr);
555 int is_ignored_expr(int owner, struct expression *expr);
557 /* smatch_var_sym */
558 struct smatch_state *alloc_var_sym_state(const char *var, struct symbol *sym);
559 struct var_sym *alloc_var_sym(const char *var, struct symbol *sym);
560 struct var_sym_list *expr_to_vsl(struct expression *expr);
561 void add_var_sym(struct var_sym_list **list, const char *var, struct symbol *sym);
562 void add_var_sym_expr(struct var_sym_list **list, struct expression *expr);
563 void del_var_sym(struct var_sym_list **list, const char *var, struct symbol *sym);
564 int in_var_sym_list(struct var_sym_list *list, const char *var, struct symbol *sym);
565 struct var_sym_list *clone_var_sym_list(struct var_sym_list *from_vsl);
566 void merge_var_sym_list(struct var_sym_list **dest, struct var_sym_list *src);
567 struct var_sym_list *combine_var_sym_lists(struct var_sym_list *one, struct var_sym_list *two);
568 int var_sym_lists_equiv(struct var_sym_list *one, struct var_sym_list *two);
569 void free_var_sym_list(struct var_sym_list **list);
570 void free_var_syms_and_list(struct var_sym_list **list);
572 /* smatch_tracker */
573 struct tracker *alloc_tracker(int owner, const char *name, struct symbol *sym);
574 void add_tracker(struct tracker_list **list, int owner, const char *name,
575 struct symbol *sym);
576 void add_tracker_expr(struct tracker_list **list, int owner, struct expression *expr);
577 void del_tracker(struct tracker_list **list, int owner, const char *name,
578 struct symbol *sym);
579 int in_tracker_list(struct tracker_list *list, int owner, const char *name,
580 struct symbol *sym);
581 void free_tracker_list(struct tracker_list **list);
582 void free_trackers_and_list(struct tracker_list **list);
584 /* smatch_conditions */
585 int in_condition(void);
587 /* smatch_flow.c */
589 extern int __in_fake_assign;
590 extern int __in_fake_parameter_assign;
591 extern int __in_fake_struct_assign;
592 extern int __in_fake_var_assign;
593 extern int __fake_state_cnt;
594 extern int in_fake_env;
595 void smatch (struct string_list *filelist);
596 int inside_loop(void);
597 int definitely_inside_loop(void);
598 struct expression *get_switch_expr(void);
599 int in_expression_statement(void);
600 void __process_post_op_stack(void);
601 void __split_expr(struct expression *expr);
602 void __split_label_stmt(struct statement *stmt);
603 void __split_stmt(struct statement *stmt);
604 extern int __in_function_def;
605 extern int __in_unmatched_hook;
606 extern int option_assume_loops;
607 extern int option_two_passes;
608 extern int option_no_db;
609 extern int option_file_output;
610 extern int option_time;
611 extern int option_time_stmt;
612 extern struct expression_list *big_expression_stack;
613 extern struct expression_list *big_condition_stack;
614 extern struct statement_list *big_statement_stack;
615 int is_assigned_call(struct expression *expr);
616 int is_fake_assigned_call(struct expression *expr);
617 void add_function_data(unsigned long *fn_data);
618 int inlinable(struct expression *expr);
619 extern int __inline_call;
620 extern struct expression *__inline_fn;
621 extern int __in_pre_condition;
622 extern int __bail_on_rest_of_function;
623 extern struct statement *__prev_stmt;
624 extern struct statement *__cur_stmt;
625 extern struct statement *__next_stmt;
626 void init_fake_env(void);
627 void end_fake_env(void);
628 int time_parsing_function(void);
629 bool taking_too_long(void);
630 struct statement *get_last_stmt(void);
631 int is_last_stmt(struct statement *cur_stmt);
633 /* smatch_struct_assignment.c */
634 struct expression *get_faked_expression(void);
635 void __fake_struct_member_assignments(struct expression *expr);
637 /* smatch_project.c */
638 int is_no_inline_function(const char *function);
640 /* smatch_conditions */
641 void __split_whole_condition(struct expression *expr);
642 void __handle_logic(struct expression *expr);
643 int is_condition(struct expression *expr);
644 int __handle_condition_assigns(struct expression *expr);
645 int __handle_select_assigns(struct expression *expr);
646 int __handle_expr_statement_assigns(struct expression *expr);
648 /* smatch_implied.c */
649 struct range_list_stack;
650 void param_limit_implications(struct expression *expr, int param, char *key, char *value, struct stree **implied);
651 struct stree *__implied_case_stree(struct expression *switch_expr,
652 struct range_list *case_rl,
653 struct range_list_stack **remaining_cases,
654 struct stree **raw_stree);
655 void overwrite_states_using_pool(struct sm_state *gate_sm, struct sm_state *pool_sm);
656 int assume(struct expression *expr);
657 void end_assume(void);
658 int impossible_assumption(struct expression *left, int op, sval_t sval);
660 /* smatch_slist.h */
661 bool has_dynamic_states(unsigned short owner);
662 void set_dynamic_states(unsigned short owner);
664 /* smatch_extras.c */
665 int in_warn_on_macro(void);
666 #define SMATCH_EXTRA 5 /* this is my_id from smatch extra set in smatch.c */
667 extern int RETURN_ID;
669 struct data_range {
670 sval_t min;
671 sval_t max;
674 char *get_other_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym);
675 char *map_call_to_other_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym);
676 char *map_long_to_short_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym, bool use_stack);
678 #define STRLEN_MAX_RET 1010101
680 /* smatch_absolute.c */
681 int get_absolute_min_helper(struct expression *expr, sval_t *sval);
682 int get_absolute_max_helper(struct expression *expr, sval_t *sval);
684 /* smatch_type_value.c */
685 void clear_type_value_cache(void);
686 int get_db_type_rl(struct expression *expr, struct range_list **rl);
688 /* smatch_data_val.c */
689 int get_mtag_rl(struct expression *expr, struct range_list **rl);
690 /* smatch_array_values.c */
691 int get_array_rl(struct expression *expr, struct range_list **rl);
693 /* smatch_states.c */
694 struct stree *__swap_cur_stree(struct stree *stree);
695 void __push_fake_cur_stree();
696 struct stree *__pop_fake_cur_stree();
697 void __free_fake_cur_stree();
698 void __set_fake_cur_stree_fast(struct stree *stree);
699 void __pop_fake_cur_stree_fast(void);
700 void __merge_stree_into_cur(struct stree *stree);
702 int unreachable(void);
703 void __set_cur_stree_readonly(void);
704 void __set_cur_stree_writable(void);
705 void __set_sm(struct sm_state *sm);
706 void __set_sm_cur_stree(struct sm_state *sm);
707 void __set_true_false_sm(struct sm_state *true_state,
708 struct sm_state *false_state);
709 void nullify_path(void);
710 void __match_nullify_path_hook(const char *fn, struct expression *expr,
711 void *unused);
712 void __unnullify_path(void);
713 int __path_is_null(void);
714 void save_all_states(void);
715 void restore_all_states(void);
716 void free_goto_stack(void);
717 void clear_all_states(void);
719 struct sm_state *get_sm_state(int owner, const char *name,
720 struct symbol *sym);
721 struct sm_state *get_sm_state_expr(int owner, struct expression *expr);
722 void __push_true_states(void);
723 void __use_false_states(void);
724 void __discard_false_states(void);
725 void __merge_false_states(void);
726 void __merge_true_states(void);
728 void __negate_cond_stacks(void);
729 void __use_pre_cond_states(void);
730 void __use_cond_true_states(void);
731 void __use_cond_false_states(void);
732 void __push_cond_stacks(void);
733 void __fold_in_set_states(void);
734 void __free_set_states(void);
735 struct stree *__copy_cond_true_states(void);
736 struct stree *__copy_cond_false_states(void);
737 struct stree *__pop_cond_true_stack(void);
738 struct stree *__pop_cond_false_stack(void);
739 void __and_cond_states(void);
740 void __or_cond_states(void);
741 void __save_pre_cond_states(void);
742 void __discard_pre_cond_states(void);
743 struct stree *__get_true_states(void);
744 struct stree *__get_false_states(void);
745 void __use_cond_states(void);
746 extern struct state_list *__last_base_slist;
748 void __push_continues(void);
749 void __discard_continues(void);
750 void __process_continues(void);
751 void __merge_continues(void);
753 void __push_breaks(void);
754 void __process_breaks(void);
755 int __has_breaks(void);
756 void __merge_breaks(void);
757 void __use_breaks(void);
759 void __save_switch_states(struct expression *switch_expr);
760 void __discard_switches(void);
761 int have_remaining_cases(void);
762 void __merge_switches(struct expression *switch_expr, struct range_list *case_rl);
763 void __push_default(void);
764 void __set_default(void);
765 int __pop_default(void);
767 void __push_conditions(void);
768 void __discard_conditions(void);
770 void __save_gotos(const char *name, struct symbol *sym);
771 void __merge_gotos(const char *name, struct symbol *sym);
773 void __discard_fake_states(struct expression *call);
775 void __print_cur_stree(void);
776 bool __print_states(const char *owner);
777 typedef void (check_tracker_hook)(int owner, const char *name, struct symbol *sym, struct smatch_state *state);
778 void add_check_tracker(const char *check_name, check_tracker_hook *fn);
780 /* smatch_hooks.c */
781 void __pass_to_client(void *data, enum hook_type type);
782 void __pass_case_to_client(struct expression *switch_expr,
783 struct range_list *rl);
784 int __has_merge_function(int client_id);
785 struct smatch_state *__client_merge_function(int owner,
786 struct smatch_state *s1,
787 struct smatch_state *s2);
788 struct smatch_state *__client_unmatched_state_function(struct sm_state *sm);
789 void call_pre_merge_hook(struct sm_state *cur, struct sm_state *other);
790 void __push_scope_hooks(void);
791 void __call_scope_hooks(void);
792 void add_array_initialized_hook(void (*hook)(struct expression *array, int nr));
793 void __call_array_initialized_hooks(struct expression *array, int nr);
795 /* smatch_function_hooks.c */
796 void create_function_hook_hash(void);
797 void __match_initializer_call(struct symbol *sym);
799 /* smatch_db.c */
800 enum info_type {
801 INTERNAL = 0,
803 * Changing these numbers is a pain. Don't do it. If you ever use a
804 * number it can't be re-used right away so there may be gaps.
805 * We select these in order by type so if the order matters, then give
806 * it a number below 100-999,9000-9999 ranges. */
808 PARAM_CLEARED = 101,
809 PARAM_LIMIT = 103,
810 PARAM_FILTER = 104,
812 PARAM_VALUE = 1001,
813 BUF_SIZE = 1002,
814 CAPPED_DATA = 1004,
815 RETURN_VALUE = 1005,
816 DEREFERENCE = 1006,
817 RANGE_CAP = 1007,
818 ABSOLUTE_LIMITS = 1010,
819 PARAM_ADD = 1012,
820 PARAM_FREED = 1013,
821 MAYBE_FREED = 2014,
822 DATA_SOURCE = 1014,
823 FUZZY_MAX = 1015,
824 HARD_MAX = 2015,
825 STR_LEN = 1016,
826 ARRAY_LEN = 1017,
827 CAPABLE = 1018,
828 NS_CAPABLE = 1019,
829 CONTAINER = 1020,
830 CASTED_CALL = 1021,
831 TYPE_LINK = 1022,
832 UNTRACKED_PARAM = 1023,
833 LOST_PARAM = 2023,
834 CULL_PATH = 1024,
835 PARAM_SET = 1025,
836 PARAM_USED = 1026,
837 BYTE_UNITS = 1027,
838 COMPARE_LIMIT = 1028,
839 PARAM_COMPARE = 1029,
840 CONSTRAINT = 1031,
841 PASSES_TYPE = 1032,
842 CONSTRAINT_REQUIRED = 1033,
843 BIT_INFO = 1034,
844 NOSPEC = 1035,
845 NOSPEC_WB = 1036,
846 STMT_CNT = 1037,
847 TERMINATED = 1038,
848 FRESH_ALLOC = 1044,
849 ALLOCATOR = 1045,
850 FUNC_TIME = 1047,
851 POWER_OF_TWO = 1048,
852 POWER_OF_TWO_SET = 1049,
853 BIT_SET = 1051,
854 BIT_CLEAR = 1052,
855 BIT_IS_SET = 1053,
856 BIT_IS_CLEAR = 1054,
858 /* put random temporary stuff in the 7000-7999 range for testing */
859 USER_DATA = 8017,
860 USER_DATA_SET = 9017,
861 USER_PTR = 9018,
862 USER_PTR_SET = 9019,
863 NO_OVERFLOW = 8018,
864 NO_OVERFLOW_SIMPLE = 8019,
865 LOCK = 8020,
866 UNLOCK = 8021,
867 HALF_LOCKED = 9022,
868 RESTORE = 9023,
869 KNOWN_LOCKED = 9024,
870 KNOWN_UNLOCKED = 9025,
871 IGNORE_LOCK = 9026,
872 SET_FS = 8022,
873 ATOMIC_INC = 8023,
874 ATOMIC_DEC = 8024,
875 REFCOUNT = 9025,
876 NO_SIDE_EFFECT = 8025,
877 FN_ARG_LINK = 8028,
878 DATA_VALUE = 8029,
879 ARRAYSIZE_ARG = 8033,
880 SIZEOF_ARG = 8034,
881 MEMORY_TAG = 8036,
882 MTAG_ASSIGN = 8035,
883 STRING_VALUE = 8041,
884 ALLOC = 8045,
885 RELEASE = 8046,
887 BYTE_COUNT = 8050,
888 ELEM_COUNT = 8051,
889 ELEM_LAST = 8052,
890 USED_LAST = 8053,
891 USED_COUNT = 8054,
894 extern struct sqlite3 *smatch_db;
895 extern struct sqlite3 *mem_db;
896 extern struct sqlite3 *cache_db;
898 bool db_incomplete(void);
899 void db_ignore_states(int id);
900 typedef bool (delete_hook)(struct expression *expr);
901 void add_delete_return_hook(delete_hook *hook);
902 void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type);
903 void select_caller_name_sym(void (*fn)(const char *name, struct symbol *sym, char *value), int type);
904 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm));
905 void add_caller_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm));
906 void add_return_info_callback(int owner,
907 void (*callback)(int return_id, char *return_ranges,
908 struct expression *returned_expr,
909 int param,
910 const char *printed_name,
911 struct sm_state *sm));
912 void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr));
913 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));
914 void select_call_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value));
915 void select_return_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value));
916 struct range_list *db_return_vals(struct expression *expr);
917 struct range_list *db_return_vals_from_str(const char *fn_name);
918 struct range_list *db_return_vals_no_args(struct expression *expr);
919 char *get_chunk_from_key(struct expression *arg, char *key, struct symbol **sym, struct var_sym_list **vsl);
920 char *get_variable_from_key(struct expression *arg, const char *key, struct symbol **sym);
921 char *get_name_sym_from_key(struct expression *expr, int param, const char *key, struct symbol **sym);
922 char *get_param_name_var_sym(const char *name, struct symbol *sym);
923 char *get_param_name(struct sm_state *sm);
924 const char *get_mtag_name_var_sym(const char *state_name, struct symbol *sym);
925 const char *get_mtag_name_expr(struct expression *expr);
926 char *get_data_info_name(struct expression *expr);
927 char *sm_to_arg_name(struct expression *expr, struct sm_state *sm);
928 int is_recursive_member(const char *param_name);
930 char *escape_newlines(const char *str);
931 void sql_exec(struct sqlite3 *db, int (*callback)(void*, int, char**, char**), void *data, const char *sql);
933 #define sql_helper(db, call_back, data, sql...) \
934 do { \
935 char sql_txt[1024]; \
937 sqlite3_snprintf(sizeof(sql_txt), sql_txt, sql); \
938 db_debug("debug: %s\n", sql_txt); \
939 sql_exec(db, call_back, data, sql_txt); \
940 } while (0)
943 #define run_sql(call_back, data, sql...) \
944 do { \
945 if (option_no_db) \
946 break; \
947 sql_helper(smatch_db, call_back, data, sql); \
948 } while (0)
950 #define mem_sql(call_back, data, sql...) \
951 sql_helper(mem_db, call_back, data, sql)
953 #define cache_sql(call_back, data, sql...) \
954 sql_helper(cache_db, call_back, data, sql)
956 #define sql_insert_helper(table, db, ignore, late, values...) \
957 do { \
958 struct sqlite3 *_db = db; \
960 if (__inline_fn && !_db) \
961 _db = mem_db; \
962 if (_db) { \
963 char buf[1024]; \
964 char *err, *p = buf; \
965 int rc; \
967 p += snprintf(p, buf + sizeof(buf) - p, \
968 "insert %sinto %s values (", \
969 ignore ? "or ignore " : "", #table); \
970 p += snprintf(p, buf + sizeof(buf) - p, values); \
971 p += snprintf(p, buf + sizeof(buf) - p, ");"); \
972 db_debug("mem-db: %s\n", buf); \
973 rc = sqlite3_exec(_db, buf, NULL, NULL, &err); \
974 if (rc != SQLITE_OK) { \
975 sm_ierror("SQL error #2: %s", err); \
976 sm_ierror("SQL: '%s'", buf); \
977 parse_error = 1; \
979 break; \
981 if (option_info) { \
982 FILE *tmp_fd = sm_outfd; \
983 sm_outfd = sql_outfd; \
984 sm_prefix(); \
985 sm_printf("SQL%s: insert %sinto " #table " values(", \
986 late ? "_late" : "", ignore ? "or ignore " : ""); \
987 sm_printf(values); \
988 sm_printf(");\n"); \
989 sm_outfd = tmp_fd; \
991 } while (0)
993 #define sql_insert(table, values...) sql_insert_helper(table, 0, 0, 0, values);
994 #define sql_insert_or_ignore(table, values...) sql_insert_helper(table, 0, 1, 0, values);
995 #define sql_insert_late(table, values...) sql_insert_helper(table, 0, 0, 1, values);
996 #define sql_insert_cache(table, values...) sql_insert_helper(table, cache_db, 1, 0, values);
998 char *get_static_filter(struct symbol *sym);
1000 void sql_insert_return_states(int return_id, const char *return_ranges,
1001 int type, int param, const char *key, const char *value);
1002 void sql_insert_caller_info(struct expression *call, int type, int param,
1003 const char *key, const char *value);
1004 void sql_insert_function_ptr(const char *fn, const char *struct_name);
1005 void sql_insert_return_values(const char *return_values);
1006 void sql_insert_return_implies(int type, int param, const char *key, const char *value);
1007 void sql_insert_function_type_size(const char *member, const char *ranges);
1008 void sql_insert_function_type_info(int type, const char *struct_type, const char *member, const char *value);
1009 void sql_insert_type_info(int type, const char *member, const char *value);
1010 void sql_insert_local_values(const char *name, const char *value);
1011 void sql_insert_function_type_value(const char *type, const char *value);
1012 void sql_insert_function_type(int param, const char *value);
1013 void sql_insert_parameter_name(int param, const char *value);
1014 void sql_insert_data_info(struct expression *data, int type, const char *value);
1015 void sql_insert_data_info_var_sym(const char *var, struct symbol *sym, int type, const char *value);
1016 void sql_save_constraint(const char *con);
1017 void sql_save_constraint_required(const char *data, int op, const char *limit);
1018 void sql_copy_constraint_required(const char *new_limit, const char *old_limit);
1019 void sql_insert_fn_ptr_data_link(const char *ptr, const char *data);
1020 void sql_insert_fn_data_link(struct expression *fn, int type, int param, const char *key, const char *value);
1021 void sql_insert_mtag_about(mtag_t tag, const char *left_name, const char *right_name);
1022 void sql_insert_mtag_info(mtag_t tag, int type, const char *value);
1023 void sql_insert_mtag_map(mtag_t container, int container_offset, mtag_t tag, int tag_offset);
1024 void sql_insert_mtag_alias(mtag_t orig, mtag_t alias);
1025 int mtag_map_select_container(mtag_t tag, int container_offset, mtag_t *container);
1026 int mtag_map_select_tag(mtag_t container, int offset, mtag_t *tag);
1027 struct smatch_state *get_mtag_return(struct expression *expr, struct smatch_state *state);
1028 struct range_list *swap_mtag_seed(struct expression *expr, struct range_list *rl);
1030 bool is_fn_ptr(struct expression *fn);
1031 void sql_select_return_states(const char *cols, struct expression *call,
1032 int (*callback)(void*, int, char**, char**), void *info);
1033 void sql_select_call_implies(const char *cols, struct expression *call,
1034 int (*callback)(void*, int, char**, char**));
1036 void open_smatch_db(char *db_file);
1038 /* smatch_files.c */
1039 int open_data_file(const char *filename);
1040 int open_schema_file(const char *schema);
1041 struct token *get_tokens_file(const char *filename);
1042 struct string_list *load_strings_from_file(const char *project, const char *filename);
1044 /* smatch.c */
1045 extern char *option_debug_check;
1046 extern char *option_debug_var;
1047 extern char *option_project_str;
1048 extern char *bin_dir;
1049 extern char *data_dir;
1050 extern int option_no_data;
1051 extern int option_full_path;
1052 extern int option_call_tree;
1053 extern int num_checks;
1055 enum project_type {
1056 PROJ_NONE,
1057 PROJ_KERNEL,
1058 PROJ_WINE,
1059 PROJ_ILLUMOS_KERNEL,
1060 PROJ_ILLUMOS_USER,
1061 PROJ_UNKNOWN,
1063 extern enum project_type option_project;
1064 const char *check_name(unsigned short id);
1065 int id_from_name(const char *name);
1068 /* smatch_buf_size.c */
1069 int get_array_size(struct expression *expr);
1070 int get_array_size_bytes(struct expression *expr);
1071 int get_array_size_bytes_min(struct expression *expr);
1072 int get_array_size_bytes_max(struct expression *expr);
1073 struct range_list *get_array_size_bytes_rl(struct expression *expr);
1074 int get_real_array_size(struct expression *expr);
1075 int last_member_is_resizable(struct symbol *type);
1076 /* smatch_strlen.c */
1077 int get_implied_strlen(struct expression *expr, struct range_list **rl);
1078 int get_size_from_strlen(struct expression *expr);
1080 /* smatch_capped.c */
1081 int is_capped(struct expression *expr);
1082 int is_capped_var_sym(const char *name, struct symbol *sym);
1084 /* smatch_kernel_user_data.c */
1085 int is_user_macro(struct expression *expr);
1086 int is_capped_user_data(struct expression *expr);
1087 int implied_user_data(struct expression *expr, struct range_list **rl);
1088 struct stree *get_user_stree(void);
1089 int get_user_rl(struct expression *expr, struct range_list **rl);
1090 int is_user_rl(struct expression *expr);
1091 int get_user_rl_var_sym(const char *name, struct symbol *sym, struct range_list **rl);
1092 bool user_rl_capped(struct expression *expr);
1093 struct range_list *var_user_rl(struct expression *expr);
1094 bool we_pass_user_data(struct expression *call);
1095 /* smatch_points_to_user_data.c */
1096 bool is_user_data_fn(struct symbol *fn);
1097 bool is_skb_data(struct expression *expr);
1098 bool points_to_user_data(struct expression *expr);
1099 void set_points_to_user_data(struct expression *expr);
1101 /* check_locking.c */
1102 void print_held_locks();
1104 /* check_assigned_expr.c */
1105 extern int check_assigned_expr_id;
1106 struct expression *get_assigned_expr(struct expression *expr);
1107 struct sm_state *get_assigned_sm(struct expression *expr);
1108 struct expression *get_assigned_expr_recurse(struct expression *expr);
1109 struct expression *get_assigned_expr_name_sym(const char *name, struct symbol *sym);
1110 struct expression *get_assigned_expr_name_sym_recurse(const char *name, struct symbol *sym);
1112 /* smatch_return_to_param.c */
1113 void __add_return_to_param_mapping(struct expression *assign, const char *return_string);
1114 char *map_call_to_param_name_sym(struct expression *expr, struct symbol **sym);
1116 /* smatch_comparison.c */
1117 extern int comparison_id;
1118 #define UNKNOWN_COMPARISON 0
1119 #define IMPOSSIBLE_COMPARISON -1
1120 struct compare_data {
1121 /* The ->left and ->right expression pointers might be NULL (I'm lazy) */
1122 struct expression *left;
1123 const char *left_var;
1124 struct var_sym_list *left_vsl;
1125 int comparison;
1126 struct expression *right;
1127 const char *right_var;
1128 struct var_sym_list *right_vsl;
1130 DECLARE_ALLOCATOR(compare_data);
1131 struct smatch_state *alloc_compare_state(
1132 struct expression *left,
1133 const char *left_var, struct var_sym_list *left_vsl,
1134 int comparison,
1135 struct expression *right,
1136 const char *right_var, struct var_sym_list *right_vsl);
1137 int comparison_intersection(int orig, int op);
1138 int merge_comparisons(int one, int two);
1139 int combine_comparisons(int left_compare, int right_compare);
1140 int state_to_comparison(struct smatch_state *state);
1141 struct smatch_state *merge_compare_states(struct smatch_state *s1, struct smatch_state *s2);
1142 int get_comparison(struct expression *left, struct expression *right);
1143 int get_comparison_no_extra(struct expression *a, struct expression *b);
1144 int get_comparison_strings(const char *one, const char *two);
1145 int possible_comparison(struct expression *a, int comparison, struct expression *b);
1146 struct state_list *get_all_comparisons(struct expression *expr);
1147 struct state_list *get_all_possible_equal_comparisons(struct expression *expr);
1148 void __add_return_comparison(struct expression *call, const char *range);
1149 void __add_comparison_info(struct expression *expr, struct expression *call, const char *range);
1150 char *get_printed_param_name(struct expression *call, const char *param_name, struct symbol *param_sym);
1151 char *name_sym_to_param_comparison(const char *name, struct symbol *sym);
1152 char *expr_equal_to_param(struct expression *expr, int ignore);
1153 char *expr_lte_to_param(struct expression *expr, int ignore);
1154 char *expr_param_comparison(struct expression *expr, int ignore);
1155 int flip_comparison(int op);
1156 int negate_comparison(int op);
1157 int remove_unsigned_from_comparison(int op);
1158 int param_compare_limit_is_impossible(struct expression *expr, int left_param, char *left_key, char *value);
1159 void filter_by_comparison(struct range_list **rl, int comparison, struct range_list *right);
1160 void __compare_param_limit_hook(struct expression *left_expr, struct expression *right_expr,
1161 const char *state_name,
1162 struct smatch_state *true_state, struct smatch_state *false_state);
1163 int impossibly_high_comparison(struct expression *expr);
1165 /* smatch_sval.c */
1166 sval_t *sval_alloc(sval_t sval);
1167 sval_t *sval_alloc_permanent(sval_t sval);
1168 sval_t sval_blank(struct expression *expr);
1169 sval_t sval_type_val(struct symbol *type, long long val);
1170 sval_t sval_type_fval(struct symbol *type, long double fval);
1171 sval_t sval_from_val(struct expression *expr, long long val);
1172 sval_t sval_from_fval(struct expression *expr, long double fval);
1173 int sval_is_ptr(sval_t sval);
1174 bool sval_is_fp(sval_t sval);
1175 int sval_unsigned(sval_t sval);
1176 int sval_signed(sval_t sval);
1177 int sval_bits(sval_t sval);
1178 int sval_bits_used(sval_t sval);
1179 int sval_is_negative(sval_t sval);
1180 int sval_is_positive(sval_t sval);
1181 int sval_is_min(sval_t sval);
1182 int sval_is_max(sval_t sval);
1183 int sval_is_a_min(sval_t sval);
1184 int sval_is_a_max(sval_t sval);
1185 int sval_is_negative_min(sval_t sval);
1186 int sval_cmp_t(struct symbol *type, sval_t one, sval_t two);
1187 int sval_cmp_val(sval_t one, long long val);
1188 sval_t sval_min(sval_t one, sval_t two);
1189 sval_t sval_min_nonneg(sval_t one, sval_t two);
1190 sval_t sval_max(sval_t one, sval_t two);
1191 int sval_too_low(struct symbol *type, sval_t sval);
1192 int sval_too_high(struct symbol *type, sval_t sval);
1193 int sval_fits(struct symbol *type, sval_t sval);
1194 sval_t sval_cast(struct symbol *type, sval_t sval);
1195 sval_t sval_preop(sval_t sval, int op);
1196 sval_t sval_binop(sval_t left, int op, sval_t right);
1197 int sval_binop_overflows(sval_t left, int op, sval_t right);
1198 int sval_binop_overflows_no_sign(sval_t left, int op, sval_t right);
1199 int find_first_zero_bit(unsigned long long uvalue);
1200 int sm_fls64(unsigned long long uvalue);
1201 unsigned long long fls_mask(unsigned long long uvalue);
1202 unsigned long long sval_fls_mask(sval_t sval);
1203 const char *sval_to_str(sval_t sval);
1204 const char *sval_to_str_or_err_ptr(sval_t sval);
1205 const char *sval_to_numstr(sval_t sval);
1206 sval_t ll_to_sval(long long val);
1208 /* smatch_string_list.c */
1209 int list_has_string(struct string_list *str_list, const char *str);
1210 int insert_string(struct string_list **str_list, const char *str);
1211 struct string_list *clone_str_list(struct string_list *orig);
1212 struct string_list *combine_string_lists(struct string_list *one, struct string_list *two);
1214 /* smatch_start_states.c */
1215 struct stree *get_start_states(void);
1217 /* smatch_recurse.c */
1218 int has_symbol(struct expression *expr, struct symbol *sym);
1219 int has_variable(struct expression *expr, struct expression *var);
1220 int has_inc_dec(struct expression *expr);
1222 /* smatch_stored_conditions.c */
1223 struct smatch_state *get_stored_condition(struct expression *expr);
1224 struct expression_list *get_conditions(struct expression *expr);
1225 struct sm_state *stored_condition_implication_hook(struct expression *expr,
1226 struct state_list **true_stack,
1227 struct state_list **false_stack);
1228 /* smatch_parsed_conditions.c */
1229 struct sm_state *parsed_condition_implication_hook(struct expression *expr,
1230 struct state_list **true_stack,
1231 struct state_list **false_stack);
1232 /* smatch_comparison.c */
1233 struct sm_state *comparison_implication_hook(struct expression *expr,
1234 struct state_list **true_stack,
1235 struct state_list **false_stack);
1237 /* check_string_len.c */
1238 int get_formatted_string_size(struct expression *call, int arg);
1239 int get_formatted_string_min_size(struct expression *call, int arg);
1241 /* smatch_param_set.c */
1242 int param_was_set(struct expression *expr);
1243 int param_was_set_var_sym(const char *name, struct symbol *sym);
1244 void print_limited_param_set(int return_id, char *return_ranges, struct expression *expr);
1245 /* param_clear */
1246 bool parent_was_PARAM_CLEAR(const char *name, struct symbol *sym);
1247 bool parent_was_PARAM_CLEAR_ZERO(const char *name, struct symbol *sym);
1249 /* smatch_param_filter.c */
1250 int param_has_filter_data(struct sm_state *sm);
1252 /* smatch_links.c */
1253 void set_up_link_functions(int id, int linkid);
1254 struct smatch_state *merge_link_states(struct smatch_state *s1, struct smatch_state *s2);
1255 void store_link(int link_id, const char *name, struct symbol *sym, const char *link_name, struct symbol *link_sym);
1257 /* check_buf_comparison */
1258 const char *limit_type_str(unsigned int limit_type);
1259 struct expression *get_size_variable(struct expression *buf, int *limit_type);
1260 struct expression *get_array_variable(struct expression *size);
1261 int buf_comparison_index_ok(struct expression *expr);
1263 /* smatch_untracked_param.c */
1264 void mark_untracked(struct expression *expr, int param, const char *key, const char *value);
1265 void add_untracked_param_hook(void (func)(struct expression *call, int param));
1266 void add_lost_param_hook(void (func)(struct expression *call, int param));
1267 void mark_all_params_untracked(int return_id, char *return_ranges, struct expression *expr);
1269 /* smatch_strings.c */
1270 struct state_list *get_strings(struct expression *expr);
1271 struct expression *fake_string_from_mtag(mtag_t tag);
1273 /* smatch_estate.c */
1274 int estate_get_single_value(struct smatch_state *state, sval_t *sval);
1276 /* smatch_address.c */
1277 int get_address_rl(struct expression *expr, struct range_list **rl);
1278 int get_member_offset(struct symbol *type, const char *member_name);
1279 int get_member_offset_from_deref(struct expression *expr);
1281 /* for now this is in smatch_used_parameter.c */
1282 void __get_state_hook(int owner, const char *name, struct symbol *sym);
1283 extern int __ignore_param_used;
1285 /* smatch_buf_comparison.c */
1286 int db_var_is_array_limit(struct expression *array, const char *name, struct var_sym_list *vsl);
1288 struct range_list *get_fs(void);
1290 struct stree *get_all_return_states(void);
1291 struct stree_stack *get_all_return_strees(void);
1292 int on_atomic_dec_path(void);
1293 int was_inced(const char *name, struct symbol *sym);
1294 void set_refcount_inc(char *name, struct symbol *sym);
1295 void set_refcount_dec(char *name, struct symbol *sym);
1297 /* smatch_constraints.c */
1298 char *get_constraint_str(struct expression *expr);
1299 struct constraint_list *get_constraints(struct expression *expr);
1300 char *unmet_constraint(struct expression *data, struct expression *offset);
1301 char *get_required_constraint(const char *data_str);
1303 /* smatch_container_of.c */
1304 int get_param_from_container_of(struct expression *expr);
1305 int get_offset_from_container_of(struct expression *expr);
1306 char *get_container_name(struct expression *container, struct expression *expr);
1308 /* smatch_mtag.c */
1309 mtag_t str_to_mtag(const char *str);
1310 int get_string_mtag(struct expression *expr, mtag_t *tag);
1311 int get_toplevel_mtag(struct symbol *sym, mtag_t *tag);
1312 int create_mtag_alias(mtag_t tag, struct expression *expr, mtag_t *new);
1313 int expr_to_mtag_offset(struct expression *expr, mtag_t *tag, int *offset);
1314 void update_mtag_data(struct expression *expr, struct smatch_state *state);
1315 int get_mtag_sval(struct expression *expr, sval_t *sval);
1317 /* Trinity fuzzer stuff */
1318 const char *get_syscall_arg_type(struct symbol *sym);
1320 /* smatch_bits.c */
1321 int binfo_equiv(struct bit_info *one, struct bit_info *two);
1322 struct bit_info *alloc_bit_info(unsigned long long set, unsigned long long possible);
1323 struct smatch_state *alloc_bstate(unsigned long long set, unsigned long long possible);
1324 struct smatch_state *merge_bstates(struct smatch_state *one_state, struct smatch_state *two_state);
1326 /* smatch_param_bits_set.c */
1327 void __set_param_modified_helper(struct expression *expr, struct smatch_state *state);
1328 void __set_param_modified_helper_sym(const char *name, struct symbol *sym,
1329 struct smatch_state *state);
1331 /* smatch_param_bits_clear.c */
1332 void __set_param_modified_helper_clear(struct expression *expr, struct smatch_state *state);
1333 void __set_param_modified_helper_sym_clear(const char *name, struct symbol *sym,
1334 struct smatch_state *state);
1336 /* smatch_bit_info.c */
1337 struct bit_info *rl_to_binfo(struct range_list *rl);
1338 struct bit_info *get_bit_info(struct expression *expr);
1339 struct bit_info *get_bit_info_var_sym(const char *name, struct symbol *sym);
1341 /* smatch_mem_tracker.c */
1342 extern int option_mem;
1343 unsigned long get_mem_kb(void);
1344 unsigned long get_max_memory(void);
1346 /* check_is_nospec.c */
1347 bool is_nospec(struct expression *expr);
1348 long get_stmt_cnt(void);
1350 /* smatch_nul_terminator.c */
1351 bool is_nul_terminated_var_sym(const char *name, struct symbol *sym);
1352 bool is_nul_terminated(struct expression *expr);
1353 /* check_kernel.c */
1354 bool is_ignored_kernel_data(const char *name);
1355 int get_gfp_param(struct expression *expr);
1357 bool is_fresh_alloc_var_sym(const char *var, struct symbol *sym);
1358 bool is_fresh_alloc(struct expression *expr);
1359 bool is_freed_var_sym(const char *name, struct symbol *sym);
1360 void track_freed_param(struct expression *expr, struct smatch_state *state);
1361 void track_freed_param_var_sym(const char *name, struct symbol *sym,
1362 struct smatch_state *state);
1364 static inline bool type_is_ptr(struct symbol *type)
1366 return type &&
1367 (type->type == SYM_PTR ||
1368 type->type == SYM_ARRAY ||
1369 type->type == SYM_FN);
1372 static inline bool type_is_fp(struct symbol *type)
1374 return type &&
1375 (type == &float_ctype ||
1376 type == &double_ctype ||
1377 type == &ldouble_ctype);
1380 static inline int type_bits(struct symbol *type)
1382 if (!type)
1383 return 0;
1384 if (type_is_ptr(type))
1385 return bits_in_pointer;
1386 if (!type->examined)
1387 examine_symbol_type(type);
1388 return type->bit_size;
1391 static inline int type_unsigned(struct symbol *base_type)
1393 if (!base_type)
1394 return 0;
1395 if (is_ptr_type(base_type))
1396 return 1;
1397 if (base_type->ctype.modifiers & MOD_UNSIGNED)
1398 return 1;
1399 return 0;
1402 static inline int type_positive_bits(struct symbol *type)
1404 if (!type)
1405 return 0;
1406 if (is_ptr_type(type))
1407 return bits_in_pointer;
1408 if (type_unsigned(type))
1409 return type_bits(type);
1410 return type_bits(type) - 1;
1413 static inline int sval_positive_bits(sval_t sval)
1415 return type_positive_bits(sval.type);
1419 * Returns -1 if one is smaller, 0 if they are the same and 1 if two is larger.
1422 static inline int fp_cmp(sval_t one, sval_t two)
1424 struct symbol *type;
1426 if (sval_is_fp(one) && sval_is_fp(two))
1427 type = type_bits(one.type) > type_bits(two.type) ? one.type : two.type;
1428 else if (sval_is_fp(one))
1429 type = one.type;
1430 else
1431 type = two.type;
1433 one = sval_cast(type, one);
1434 two = sval_cast(type, two);
1436 if (one.type == &float_ctype) {
1437 if (one.fvalue < two.fvalue)
1438 return -1;
1439 if (one.fvalue == two.fvalue)
1440 return 0;
1441 return 1;
1443 if (one.type == &double_ctype) {
1444 if (one.dvalue < two.dvalue)
1445 return -1;
1446 if (one.dvalue == two.dvalue)
1447 return 0;
1448 return 1;
1450 if (one.type == &ldouble_ctype) {
1451 if (one.ldvalue < two.ldvalue)
1452 return -1;
1453 if (one.ldvalue == two.ldvalue)
1454 return 0;
1455 return 1;
1457 sm_perror("bad type in fp_cmp(): %s", type_to_str(type));
1458 return 1;
1461 static inline int sval_cmp(sval_t one, sval_t two)
1463 struct symbol *type;
1465 if (sval_is_fp(one) || sval_is_fp(two))
1466 return fp_cmp(one, two);
1468 type = one.type;
1469 if (sval_positive_bits(two) > sval_positive_bits(one))
1470 type = two.type;
1471 if (type_bits(type) < 31)
1472 type = &int_ctype;
1474 one = sval_cast(type, one);
1475 two = sval_cast(type, two);
1477 if (type_unsigned(type)) {
1478 if (one.uvalue < two.uvalue)
1479 return -1;
1480 if (one.uvalue == two.uvalue)
1481 return 0;
1482 return 1;
1484 /* fix me handle type promotion and unsigned values */
1485 if (one.value < two.value)
1486 return -1;
1487 if (one.value == two.value)
1488 return 0;
1489 return 1;
1492 #endif /* !SMATCH_H_ */