kernel_err_ptr: track error pointers in the database
[smatch.git] / smatch.h
blob520bbdb3621169904a8b3195670663d87414e0cd
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 #define GLOBAL_STATE(_x) struct smatch_state _x = { .name = #_x }
43 extern struct smatch_state undefined;
44 extern struct smatch_state merged;
45 extern struct smatch_state true_state;
46 extern struct smatch_state false_state;
47 extern struct smatch_state unit_bit;
48 extern struct smatch_state unit_byte;
49 extern struct smatch_state unit_array_size;
50 extern struct smatch_state unit_long;
51 extern struct smatch_state unit_page;
52 extern struct smatch_state unit_msec;
53 extern struct smatch_state unit_ns;
54 extern struct smatch_state unit_jiffy;
55 DECLARE_ALLOCATOR(smatch_state);
57 static inline void *INT_PTR(int i)
59 return (void *)(long)i;
62 static inline int PTR_INT(void *p)
64 return (int)(long)p;
67 struct tracker {
68 char *name;
69 struct symbol *sym;
70 unsigned short owner;
72 DECLARE_ALLOCATOR(tracker);
73 DECLARE_PTR_LIST(tracker_list, struct tracker);
74 DECLARE_PTR_LIST(stree_stack, struct stree);
76 /* The first 3 struct members must match struct tracker */
77 struct sm_state {
78 const char *name;
79 struct symbol *sym;
80 unsigned short owner;
81 unsigned short merged:1;
82 unsigned short leaf:1;
83 unsigned int line;
84 struct smatch_state *state;
85 struct stree *pool;
86 struct sm_state *left;
87 struct sm_state *right;
88 struct state_list *possible;
91 struct var_sym {
92 char *var;
93 struct symbol *sym;
95 DECLARE_ALLOCATOR(var_sym);
96 DECLARE_PTR_LIST(var_sym_list, struct var_sym);
98 struct constraint {
99 int op;
100 int id;
102 DECLARE_PTR_LIST(constraint_list, struct constraint);
104 struct alloc_info {
105 const char *fn;
106 int size_param, nr;
108 extern struct alloc_info *alloc_funcs;
110 struct bit_info {
111 unsigned long long set;
112 unsigned long long possible;
115 enum hook_type {
116 EXPR_HOOK,
117 EXPR_HOOK_AFTER,
118 STMT_HOOK,
119 STMT_HOOK_AFTER,
120 SYM_HOOK,
121 STRING_HOOK,
122 DECLARATION_HOOK,
123 DECLARATION_HOOK_AFTER,
124 ASSIGNMENT_HOOK,
125 ASSIGNMENT_HOOK_AFTER,
126 RAW_ASSIGNMENT_HOOK,
127 GLOBAL_ASSIGNMENT_HOOK,
128 LOGIC_HOOK,
129 CONDITION_HOOK,
130 PRELOOP_HOOK,
131 POSTLOOP_HOOK,
132 AFTER_LOOP_NO_BREAKS,
133 SELECT_HOOK,
134 WHOLE_CONDITION_HOOK,
135 FUNCTION_CALL_HOOK_BEFORE,
136 FUNCTION_CALL_HOOK,
137 CALL_HOOK_AFTER_INLINE,
138 FUNCTION_CALL_HOOK_AFTER_DB,
139 CALL_ASSIGNMENT_HOOK,
140 MACRO_ASSIGNMENT_HOOK,
141 BINOP_HOOK,
142 OP_HOOK,
143 DEREF_HOOK, /* DEREF_HOOK is junk. Better to use add_dereference_hook() */
144 CASE_HOOK,
145 ASM_HOOK,
146 CAST_HOOK,
147 SIZEOF_HOOK,
148 BASE_HOOK,
149 FUNC_DEF_HOOK,
150 AFTER_DEF_HOOK,
151 END_FUNC_HOOK,
152 AFTER_FUNC_HOOK,
153 RETURN_HOOK,
154 INLINE_FN_START,
155 INLINE_FN_END,
156 END_FILE_HOOK,
157 NUM_HOOKS,
160 #define TRUE 1
161 #define FALSE 0
163 struct range_list;
165 typedef void (void_fn)(void);
166 typedef void (expr_func)(struct expression *expr);
167 typedef void (stmt_func)(struct statement *stmt);
168 typedef void (sym_func)(struct symbol *sym);
169 typedef void (name_sym_hook)(struct expression *expr, const char *name, struct symbol *sym);
170 typedef void (sm_hook)(struct sm_state *sm, struct expression *mod_expr);
171 typedef void (string_hook)(struct expression *expr, const char *str);
172 DECLARE_PTR_LIST(void_fn_list, void_fn);
173 DECLARE_PTR_LIST(expr_fn_list, expr_func);
174 DECLARE_PTR_LIST(stmt_fn_list, stmt_func);
175 DECLARE_PTR_LIST(sym_fn_list, sym_func);
176 DECLARE_PTR_LIST(name_sym_fn_list, name_sym_hook);
177 DECLARE_PTR_LIST(string_hook_list, string_hook);
178 void call_void_fns(struct void_fn_list *list);
179 void call_expr_fns(struct expr_fn_list *list, struct expression *expr);
180 void call_stmt_fns(struct stmt_fn_list *list, struct statement *stmt);
181 void call_sym_fns(struct sym_fn_list *list, struct symbol *sym);
182 void call_name_sym_fns(struct name_sym_fn_list *list, struct expression *expr, const char *name, struct symbol *sym);
183 void call_string_hooks(struct string_hook_list *list, struct expression *expr, const char *str);
185 void add_dereference_hook(expr_func *fn);
187 struct allocation_info {
188 const char *fn_name;
189 const char *size_str;
190 struct expression *total_size;
191 struct expression *nr_elems;
192 struct expression *elem_size;
193 long min, max;
194 bool zeroed;
195 bool safe; /* safe from overflows */
197 typedef void (alloc_hook)(struct expression *expr, const char *name, struct symbol *sym, struct allocation_info *info);
198 void add_allocation_hook(alloc_hook *func);
200 void add_hook(void *func, enum hook_type type);
201 typedef struct smatch_state *(merge_func_t)(struct smatch_state *s1, struct smatch_state *s2);
202 typedef struct smatch_state *(unmatched_func_t)(struct sm_state *state);
203 void add_merge_hook(int client_id, merge_func_t *func);
204 void add_unmatched_state_hook(int client_id, unmatched_func_t *func);
205 void add_pre_merge_hook(int client_id, void (*hook)(struct sm_state *cur, struct sm_state *other));
206 typedef void (scope_hook)(void *data);
207 void add_scope_hook(scope_hook *hook, void *data);
208 void add_return_string_hook(string_hook *fn);
209 typedef void (param_key_hook)(struct expression *expr, const char *name, struct symbol *sym, void *data);
210 typedef void (func_hook)(const char *fn, struct expression *expr, void *data);
211 typedef void (implication_hook)(const char *fn, struct expression *call_expr,
212 struct expression *assign_expr, void *data);
213 typedef void (return_implies_hook)(struct expression *call_expr,
214 int param, char *key, char *value);
215 typedef int (implied_return_hook)(struct expression *call_expr, void *info, struct range_list **rl);
216 void add_function_hook_early(const char *look_for, func_hook *call_back, void *data);
217 void add_function_hook(const char *look_for, func_hook *call_back, void *data);
218 void add_function_hook_late(const char *look_for, func_hook *call_back, void *info);
220 void add_function_assign_hook(const char *look_for, func_hook *call_back,
221 void *info);
222 void register_func_hooks_from_file(const char *file,
223 func_hook *call_back, void *info);
224 void register_assign_hooks_from_file(const char *file,
225 func_hook *call_back, void *info);
226 void add_implied_return_hook(const char *look_for,
227 implied_return_hook *call_back,
228 void *info);
229 void add_macro_assign_hook(const char *look_for, func_hook *call_back,
230 void *info);
231 void add_macro_assign_hook_extra(const char *look_for, func_hook *call_back,
232 void *info);
233 void return_implies_state(const char *look_for, long long start, long long end,
234 implication_hook *call_back, void *info);
235 void return_implies_state_sval(const char *look_for, sval_t start, sval_t end,
236 implication_hook *call_back, void *info);
237 void return_implies_exact(const char *look_for, sval_t start, sval_t end,
238 implication_hook *call_back, void *info);
239 struct range_list *get_range_implications(const char *fn);
240 void select_return_states_hook(int type, return_implies_hook *callback);
241 void select_return_states_before(void (*fn)(void));
242 void select_return_states_after(void (*fn)(void));
243 void add_function_param_key_hook(const char *look_for, param_key_hook *call_back,
244 int param, const char *key, void *info);
245 void add_function_param_key_hook_early(const char *look_for, param_key_hook *call_back,
246 int param, const char *key, void *info);
247 void add_function_param_key_hook_late(const char *look_for, param_key_hook *call_back,
248 int param, const char *key, void *info);
249 void return_implies_param_key(const char *look_for, sval_t start, sval_t end,
250 param_key_hook *call_back,
251 int param, const char *key, void *info);
252 void return_implies_param_key_exact(const char *look_for, sval_t start, sval_t end,
253 param_key_hook *call_back,
254 int param, const char *key, void *info);
255 void select_return_param_key(int type, param_key_hook *callback);
256 bool get_implied_return(struct expression *expr, struct range_list **rl);
257 void allocate_hook_memory(void);
258 void allocate_tracker_array(int num_checks);
260 struct modification_data {
261 struct smatch_state *prev;
262 struct expression *cur;
265 void allocate_modification_hooks(void);
266 bool is_sub_member(const char *name, struct symbol *sym, struct sm_state *sm);
267 void add_all_modifications_hook(int owner, name_sym_hook *hook);
268 void add_modification_hook(int owner, sm_hook *call_back);
269 void add_modification_hook_late(int owner, sm_hook *call_back);
270 struct smatch_state *get_modification_state(struct expression *expr);
272 int outside_of_function(void);
273 const char *get_filename(void);
274 extern int base_file_stream;
275 const char *get_base_file(void);
276 unsigned long long get_file_id(void);
277 unsigned long long get_base_file_id(void);
278 char *get_function(void);
279 extern int __smatch_lineno;
280 int get_lineno(void);
281 extern int final_pass;
282 extern struct symbol *cur_func_sym;
283 extern int option_debug;
284 extern int local_debug;
285 extern int debug_db;
286 extern bool implied_debug;
287 bool debug_implied(void);
288 bool debug_on(const char *check_name, const char *var);
289 extern int option_info;
290 extern int option_spammy;
291 extern int option_pedantic;
292 extern int option_print_names;
293 extern char *trace_variable;
294 extern struct stree *global_states;
295 void set_function_skipped(void);
296 int is_skipped_function(void);
297 int is_silenced_function(void);
298 extern bool implications_off;
300 /* smatch_impossible.c */
301 int is_impossible_path(void);
302 void set_true_path_impossible(void);
303 void set_false_path_impossible(void);
304 void set_path_impossible(void);
306 extern FILE *sm_outfd;
307 extern FILE *sql_outfd;
308 extern FILE *caller_info_fd;
309 extern int sm_nr_checks;
310 extern int sm_nr_errors;
313 * How to use these routines:
315 * sm_fatal(): an internal error of some kind that should immediately exit
316 * sm_ierror(): an internal error
317 * sm_perror(): an internal error from parsing input source
318 * sm_error(): an error from input source
319 * sm_warning(): a warning from input source
320 * sm_info(): info message (from option_info)
321 * sm_debug(): debug message
322 * sm_msg(): other message (please avoid using this)
325 #define sm_printf(msg...) do { \
326 if (final_pass || option_debug || local_debug || debug_db) \
327 fprintf(sm_outfd, msg); \
328 } while (0)
330 static inline void sm_prefix(void)
332 sm_printf("%s:%d %s() ", get_filename(), get_lineno(), get_function());
333 if (option_info || !option_print_names)
334 return;
335 sm_printf("[smatch.%s] ", __CHECKNAME__);
338 static inline void print_implied_debug_msg();
340 extern bool __silence_warnings_for_stmt;
342 #define sm_print_msg(type, msg...) \
343 do { \
344 print_implied_debug_msg(); \
345 if (!final_pass && !option_debug && !local_debug && !debug_db) \
346 break; \
347 if (__silence_warnings_for_stmt && !option_debug && !local_debug) \
348 break; \
349 if (!option_info && is_silenced_function()) \
350 break; \
351 sm_prefix(); \
352 if (type == 1) { \
353 sm_printf("warn: "); \
354 sm_nr_checks++; \
355 } else if (type == 2) { \
356 sm_printf("error: "); \
357 sm_nr_checks++; \
358 } else if (type == 3) { \
359 sm_printf("parse error: "); \
360 sm_nr_errors++; \
361 } else if (type == 4) { \
362 sm_printf("pedantic: "); \
364 sm_printf(msg); \
365 sm_printf("\n"); \
366 } while (0)
368 #define sm_msg(msg...) do { sm_print_msg(0, msg); } while (0)
370 extern char *implied_debug_msg;
371 static inline void print_implied_debug_msg(void)
373 static struct symbol *last_printed = NULL;
375 if (!implied_debug_msg)
376 return;
377 if (last_printed == cur_func_sym)
378 return;
379 last_printed = cur_func_sym;
380 sm_msg("%s", implied_debug_msg);
383 #define sm_debug(msg...) do { if (option_debug) sm_printf(msg); } while (0)
384 #define db_debug(msg...) do { if (option_debug || debug_db) sm_printf(msg); } while (0)
386 #define sm_info(msg...) do { \
387 if (option_debug || (option_info && final_pass)) { \
388 sm_prefix(); \
389 sm_printf("info: "); \
390 sm_printf(msg); \
391 sm_printf("\n"); \
393 } while(0)
395 #define sm_warning(msg...) do { sm_print_msg(1, msg); } while (0)
396 #define sm_warning_line(line, msg...) do { \
397 int __orig = __smatch_lineno; \
398 __smatch_lineno = line; \
399 sm_print_msg(1, msg); \
400 __smatch_lineno = __orig; \
401 } while (0)
402 #define sm_error(msg...) do { sm_print_msg(2, msg); } while (0)
403 #define sm_perror(msg...) do { sm_print_msg(3, msg); } while (0)
404 #define sm_pedantic(msg...) do { if (option_pedantic) sm_print_msg(4, msg); } while (0)
406 static inline void sm_fatal(const char *fmt, ...)
408 va_list args;
410 va_start(args, fmt);
411 vfprintf(sm_outfd, fmt, args);
412 va_end(args);
414 fprintf(sm_outfd, "\n");
416 exit(1);
419 static inline void sm_ierror(const char *fmt, ...)
421 va_list args;
423 sm_nr_errors++;
425 fprintf(sm_outfd, "internal error: ");
427 va_start(args, fmt);
428 vfprintf(sm_outfd, fmt, args);
429 va_end(args);
431 fprintf(sm_outfd, "\n");
433 #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
435 bool has_states(struct stree *stree, int owner);
436 struct smatch_state *__get_state(int owner, const char *name, struct symbol *sym);
437 struct smatch_state *get_state(int owner, const char *name, struct symbol *sym);
438 struct smatch_state *get_state_expr(int owner, struct expression *expr);
439 bool has_possible_state(int owner, const char *name, struct symbol *sym, struct smatch_state *state);
440 bool expr_has_possible_state(int owner, struct expression *expr, struct smatch_state *state);
441 struct state_list *get_possible_states(int owner, const char *name,
442 struct symbol *sym);
443 struct state_list *get_possible_states_expr(int owner, struct expression *expr);
444 struct sm_state *set_state(int owner, const char *name, struct symbol *sym,
445 struct smatch_state *state);
446 struct sm_state *set_state_expr(int owner, struct expression *expr,
447 struct smatch_state *state);
448 void __delete_state(int owner, const char *name, struct symbol *sym);
449 void __delete_all_states_sym(struct symbol *sym);
450 void set_true_false_states(int owner, const char *name, struct symbol *sym,
451 struct smatch_state *true_state,
452 struct smatch_state *false_state);
453 void set_true_false_states_expr(int owner, struct expression *expr,
454 struct smatch_state *true_state,
455 struct smatch_state *false_state);
457 struct stree *get_all_states_from_stree(int owner, struct stree *source);
458 struct stree *get_all_states_stree(int id);
459 struct stree *__get_cur_stree(void);
460 int is_reachable(void);
461 void add_get_state_hook(void (*fn)(int owner, const char *name, struct symbol *sym));
463 /* smatch_ssa.c */
464 char *ssa_name(const char *name);
465 void set_ssa_state(int owner, const char *name, struct symbol *sym, struct smatch_state *state);
466 void update_ssa_state(int owner, const char *name, struct symbol *sym,
467 struct smatch_state *state);
468 void update_ssa_sm(int owner, struct sm_state *sm, struct smatch_state *state);
469 void set_ssa_state_expr(int owner, struct expression *expr, struct smatch_state *state);
470 struct sm_state *get_ssa_sm_state(int owner, const char *name, struct symbol *sym);
471 struct sm_state *get_ssa_sm_state_expr(int owner, struct expression *expr);
472 struct smatch_state *get_ssa_state(int owner, const char *name, struct symbol *sym);
473 struct smatch_state *get_ssa_state_expr(int owner, struct expression *expr);
475 /* smatch_helper.c */
476 DECLARE_PTR_LIST(int_stack, int);
477 char *alloc_string(const char *str);
478 char *alloc_string_newline(const char *str);
479 void free_string(char *str);
480 void remove_parens(char *str);
481 struct smatch_state *alloc_state_num(int num);
482 struct smatch_state *alloc_state_str(const char *name);
483 struct smatch_state *merge_str_state(struct smatch_state *s1, struct smatch_state *s2);
484 struct smatch_state *alloc_state_expr(struct expression *expr);
485 struct expression *get_assigned_call(struct expression *expr);
486 struct expression *get_argument_from_call_expr(struct expression_list *args,
487 int num);
488 struct expression *get_array_expr(struct expression *expr);
490 char *expr_to_var(struct expression *expr);
491 struct symbol *expr_to_sym(struct expression *expr);
492 char *expr_to_str(struct expression *expr);
493 char *expr_to_str_sym(struct expression *expr,
494 struct symbol **sym_ptr);
495 char *expr_to_var_sym(struct expression *expr,
496 struct symbol **sym_ptr);
497 char *expr_to_known_chunk_sym(struct expression *expr, struct symbol **sym);
498 char *expr_to_chunk_sym_vsl(struct expression *expr, struct symbol **sym, struct var_sym_list **vsl);
499 int get_complication_score(struct expression *expr);
501 int sym_name_is(const char *name, struct expression *expr);
502 int get_const_value(struct expression *expr, sval_t *sval);
503 int get_value(struct expression *expr, sval_t *val);
504 int get_implied_value(struct expression *expr, sval_t *val);
505 int get_implied_value_fast(struct expression *expr, sval_t *sval);
506 int get_implied_min(struct expression *expr, sval_t *sval);
507 int get_implied_max(struct expression *expr, sval_t *val);
508 int get_hard_max(struct expression *expr, sval_t *sval);
509 int get_fuzzy_min(struct expression *expr, sval_t *min);
510 int get_fuzzy_max(struct expression *expr, sval_t *max);
511 int get_absolute_min(struct expression *expr, sval_t *sval);
512 int get_absolute_max(struct expression *expr, sval_t *sval);
513 int parse_call_math(struct expression *expr, char *math, sval_t *val);
514 int parse_call_math_rl(struct expression *call, const char *math, struct range_list **rl);
515 const char *get_allocation_math(struct expression *expr);
516 char *get_value_in_terms_of_parameter_math(struct expression *expr);
517 char *get_value_in_terms_of_parameter_math_var_sym(const char *var, struct symbol *sym);
518 int expr_is_zero(struct expression *expr);
519 int known_condition_true(struct expression *expr);
520 int known_condition_false(struct expression *expr);
521 int implied_condition_true(struct expression *expr);
522 int implied_condition_false(struct expression *expr);
523 int can_integer_overflow(struct symbol *type, struct expression *expr);
524 void clear_math_cache(void);
525 void clear_strip_cache(void);
526 void set_fast_math_only(void);
527 void clear_fast_math_only(void);
529 int is_array(struct expression *expr);
530 struct expression *get_array_base(struct expression *expr);
531 struct expression *get_array_offset(struct expression *expr);
532 const char *show_state(struct smatch_state *state);
533 struct statement *get_expression_statement(struct expression *expr);
534 struct expression *strip__builtin_choose_expr(struct expression *expr);
535 struct expression *strip_Generic(struct expression *expr);
536 struct expression *strip_parens(struct expression *expr);
537 struct expression *strip_expr(struct expression *expr);
538 struct expression *strip_no_cast(struct expression *expr);
539 struct expression *strip_expr_set_parent(struct expression *expr);
540 void scoped_state(int my_id, const char *name, struct symbol *sym);
541 int is_error_return(struct expression *expr);
542 int getting_address(struct expression *expr);
543 int get_struct_and_member(struct expression *expr, const char **type, const char **member);
544 char *get_member_name(struct expression *expr);
545 char *get_fnptr_name(struct expression *expr);
546 int cmp_pos(struct position pos1, struct position pos2);
547 int positions_eq(struct position pos1, struct position pos2);
548 struct statement *get_current_statement(void);
549 struct statement *get_prev_statement(void);
550 struct expression *get_last_expr_from_expression_stmt(struct expression *expr);
552 enum { RET_SUCCESS, RET_FAIL, RET_UNKNOWN };
553 int success_fail_return(struct range_list *rl);
555 #define RETURN_VAR -1
556 #define LOCAL_SCOPE -2
557 #define FILE_SCOPE -3
558 #define GLOBAL_SCOPE -4
559 #define UNKNOWN_SCOPE -5
561 char *swap_names(const char *orig, const char *remove, const char *add);
562 char *get_param_var_sym_var_sym(const char *name, struct symbol *sym, struct expression *ret_expr, struct symbol **sym_p);
563 char *get_param_name_sym(struct expression *expr, struct symbol **sym_p);
564 int get_return_param_key_from_var_sym(const char *name, struct symbol *sym,
565 struct expression *ret_expr,
566 const char **key);
567 int get_param_key_from_var_sym(const char *name, struct symbol *sym,
568 struct expression *ret_expr,
569 const char **key);
570 int get_param_key_from_sm(struct sm_state *sm, struct expression *ret_expr,
571 const char **key);
572 int get_param_key_from_expr(struct expression *expr, struct expression *ret_expr,
573 const char **key);
574 const char *get_param_key_swap_dollar(struct expression *expr);
575 int map_to_param(const char *name, struct symbol *sym);
576 int get_param_num_from_sym(struct symbol *sym);
577 int get_param_num(struct expression *expr);
578 struct symbol *get_param_sym_from_num(int num);
579 struct expression *map_container_of_to_simpler_expr_key(struct expression *expr, const char *orig_key, char **new_key);
580 const char *get_container_of_str(struct expression *expr);
581 bool get_offset_param(const char *ret_str, int *offset, int *param);
582 /* smatch_points_to_container.c */
583 struct expression *get_stored_container(struct expression *expr, int offset);
585 int ms_since(struct timeval *start);
586 int parent_is_gone_var_sym(const char *name, struct symbol *sym);
587 int parent_is_gone(struct expression *expr);
588 bool is_noderef_ptr(struct expression *expr);
589 int invert_op(int op);
590 int op_remove_assign(int op);
591 int expr_equiv(struct expression *one, struct expression *two);
592 void push_int(struct int_stack **stack, int num);
593 int pop_int(struct int_stack **stack);
594 bool macro_to_ul(const char *macro, unsigned long *val);
596 /* smatch_type.c */
597 struct symbol *get_real_base_type(struct symbol *sym);
598 int type_bytes(struct symbol *type);
599 int array_bytes(struct symbol *type);
600 struct symbol *get_pointer_type(struct expression *expr);
601 struct symbol *get_type(struct expression *expr);
602 struct symbol *get_comparison_type(struct expression *expr);
603 struct symbol *get_final_type(struct expression *expr);
604 struct symbol *get_promoted_type(struct symbol *left, struct symbol *right);
605 int type_signed(struct symbol *base_type);
606 int expr_unsigned(struct expression *expr);
607 int expr_signed(struct expression *expr);
608 int returns_unsigned(struct symbol *base_type);
609 int is_pointer(struct expression *expr);
610 bool is_void_ptr(struct symbol *type);
611 int returns_pointer(struct symbol *base_type);
612 sval_t sval_type_max(struct symbol *base_type);
613 sval_t sval_type_min(struct symbol *base_type);
614 int nr_bits(struct expression *expr);
615 int is_void_pointer(struct expression *expr);
616 int is_char_pointer(struct expression *expr);
617 int is_string(struct expression *expr);
618 bool is_struct_ptr(struct symbol *type);
619 int is_static(struct expression *expr);
620 bool is_local_variable(struct expression *expr);
621 int types_equiv(struct symbol *one, struct symbol *two);
622 bool type_fits(struct symbol *type, struct symbol *test);
623 int fn_static(void);
624 const char *global_static();
625 struct symbol *cur_func_return_type(void);
626 struct symbol *get_arg_type(struct expression *fn, int arg);
627 struct symbol *get_member_type_from_key(struct expression *expr, const char *key);
628 struct symbol *get_arg_type_from_key(struct expression *fn, int param, struct expression *arg, const char *key);
629 int is_struct(struct expression *expr);
630 char *type_to_str(struct symbol *type);
632 /* smatch_ignore.c */
633 void add_ignore(int owner, const char *name, struct symbol *sym);
634 int is_ignored(int owner, const char *name, struct symbol *sym);
635 void add_ignore_expr(int owner, struct expression *expr);
636 int is_ignored_expr(int owner, struct expression *expr);
638 /* smatch_var_sym */
639 struct smatch_state *alloc_var_sym_state(const char *var, struct symbol *sym);
640 struct var_sym *alloc_var_sym(const char *var, struct symbol *sym);
641 struct var_sym_list *expr_to_vsl(struct expression *expr);
642 void add_var_sym(struct var_sym_list **list, const char *var, struct symbol *sym);
643 void add_var_sym_expr(struct var_sym_list **list, struct expression *expr);
644 void del_var_sym(struct var_sym_list **list, const char *var, struct symbol *sym);
645 int in_var_sym_list(struct var_sym_list *list, const char *var, struct symbol *sym);
646 struct var_sym_list *clone_var_sym_list(struct var_sym_list *from_vsl);
647 void merge_var_sym_list(struct var_sym_list **dest, struct var_sym_list *src);
648 struct var_sym_list *combine_var_sym_lists(struct var_sym_list *one, struct var_sym_list *two);
649 int var_sym_lists_equiv(struct var_sym_list *one, struct var_sym_list *two);
650 void free_var_sym_list(struct var_sym_list **list);
651 void free_var_syms_and_list(struct var_sym_list **list);
653 /* smatch_tracker */
654 struct tracker *alloc_tracker(int owner, const char *name, struct symbol *sym);
655 void add_tracker(struct tracker_list **list, int owner, const char *name,
656 struct symbol *sym);
657 void add_tracker_expr(struct tracker_list **list, int owner, struct expression *expr);
658 void del_tracker(struct tracker_list **list, int owner, const char *name,
659 struct symbol *sym);
660 int in_tracker_list(struct tracker_list *list, int owner, const char *name,
661 struct symbol *sym);
662 void free_tracker_list(struct tracker_list **list);
663 void free_trackers_and_list(struct tracker_list **list);
665 /* smatch_conditions */
666 int in_condition(void);
668 /* smatch_flow.c */
670 extern int __in_fake_assign;
671 extern int __in_fake_parameter_assign;
672 extern int __in_fake_struct_assign;
673 extern int __in_buf_clear;
674 extern int __in_fake_var_assign;
675 extern int __in_builtin_overflow_func;
676 extern int __fake_state_cnt;
677 extern int __debug_skip;
678 extern int in_fake_env;
679 bool is_fake_var_assign(struct expression *expr);
680 void smatch (struct string_list *filelist);
681 int inside_loop(void);
682 int definitely_inside_loop(void);
683 struct expression *get_switch_expr(void);
684 int in_expression_statement(void);
685 void __process_post_op_stack(void);
686 void __split_expr(struct expression *expr);
687 void __split_label_stmt(struct statement *stmt);
688 void __split_stmt(struct statement *stmt);
689 extern int __in_function_def;
690 extern int __in_unmatched_hook;
691 extern int option_assume_loops;
692 extern int option_two_passes;
693 extern int option_no_db;
694 extern int option_file_output;
695 extern int option_time;
696 extern int option_time_stmt;
697 extern struct expression_list *big_expression_stack;
698 extern struct expression_list *big_condition_stack;
699 extern struct statement_list *big_statement_stack;
700 int is_condition_call(struct expression *expr);
701 int is_assigned_call(struct expression *expr);
702 int is_fake_assigned_call(struct expression *expr);
703 void add_function_data(unsigned long *fn_data);
704 int inlinable(struct expression *expr);
705 extern int __inline_call;
706 extern struct expression *__inline_fn;
707 extern int __in_pre_condition;
708 extern int __bail_on_rest_of_function;
709 extern struct statement *__prev_stmt;
710 extern struct statement *__cur_stmt;
711 extern struct statement *__next_stmt;
712 void init_fake_env(void);
713 void end_fake_env(void);
714 int time_parsing_function(void);
715 bool taking_too_long(void);
716 struct statement *get_last_stmt(void);
717 int is_last_stmt(struct statement *cur_stmt);
719 /* smatch_struct_assignment.c */
720 struct expression *get_faked_expression(void);
721 void __fake_struct_member_assignments(struct expression *expr);
722 void create_recursive_fake_assignments(struct expression *expr,
723 void (*assign_handler)(struct expression *expr, void *data),
724 void *data);
726 /* smatch_project.c */
727 int is_no_inline_function(const char *function);
729 /* smatch_conditions */
730 void __split_whole_condition(struct expression *expr);
731 void __handle_logic(struct expression *expr);
732 int is_condition(struct expression *expr);
733 int __handle_condition_assigns(struct expression *expr);
734 int __handle_select_assigns(struct expression *expr);
735 int __handle_expr_statement_assigns(struct expression *expr);
737 /* smatch_implied.c */
738 struct range_list_stack;
739 void param_limit_implications(struct expression *expr, int param, char *key, char *value, struct stree **implied);
740 struct stree *__implied_case_stree(struct expression *switch_expr,
741 struct range_list *case_rl,
742 struct range_list_stack **remaining_cases,
743 struct stree **raw_stree);
744 void overwrite_states_using_pool(struct sm_state *gate_sm, struct sm_state *pool_sm);
745 int assume(struct expression *expr);
746 void end_assume(void);
747 int impossible_assumption(struct expression *left, int op, sval_t sval);
749 /* smatch_slist.h */
750 bool has_dynamic_states(unsigned short owner);
751 void set_dynamic_states(unsigned short owner);
753 /* smatch_extras.c */
754 int in_warn_on_macro(void);
755 extern int SMATCH_EXTRA;
756 extern int RETURN_ID;
758 struct data_range {
759 sval_t min;
760 sval_t max;
763 char *get_other_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym);
764 char *map_call_to_other_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym);
765 char *map_long_to_short_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym, bool use_stack);
767 #define STRLEN_MAX_RET 1010101
769 /* smatch_absolute.c */
770 int get_absolute_min_helper(struct expression *expr, sval_t *sval);
771 int get_absolute_max_helper(struct expression *expr, sval_t *sval);
773 /* smatch_type_value.c */
774 void disable_type_val_lookups(void);
775 void enable_type_val_lookups(void);
776 void clear_type_value_cache(void);
777 int get_db_type_rl(struct expression *expr, struct range_list **rl);
779 /* smatch_data_val.c */
780 int get_mtag_rl(struct expression *expr, struct range_list **rl);
781 /* smatch_array_values.c */
782 void clear_array_values_cache(void);
783 int get_array_rl(struct expression *expr, struct range_list **rl);
785 /* smatch_states.c */
786 struct stree *__swap_cur_stree(struct stree *stree);
787 void __push_fake_cur_stree();
788 struct stree *__pop_fake_cur_stree();
789 void __free_fake_cur_stree();
790 void __set_fake_cur_stree_fast(struct stree *stree);
791 void __pop_fake_cur_stree_fast(void);
792 void __merge_stree_into_cur(struct stree *stree);
794 int unreachable(void);
795 void __set_cur_stree_readonly(void);
796 void __set_cur_stree_writable(void);
797 void __set_sm(struct sm_state *sm);
798 void __set_sm_cur_stree(struct sm_state *sm);
799 void __set_true_false_sm(struct sm_state *true_state,
800 struct sm_state *false_state);
801 void nullify_path(void);
802 void __match_nullify_path_hook(const char *fn, struct expression *expr,
803 void *unused);
804 void __unnullify_path(void);
805 int __path_is_null(void);
806 void save_all_states(void);
807 void restore_all_states(void);
808 void free_goto_stack(void);
809 void clear_all_states(void);
811 struct sm_state *get_sm_state(int owner, const char *name,
812 struct symbol *sym);
813 struct sm_state *get_sm_state_expr(int owner, struct expression *expr);
814 void __push_true_states(void);
815 void __use_false_states(void);
816 void __discard_false_states(void);
817 void __merge_false_states(void);
818 void __merge_true_states(void);
820 void __negate_cond_stacks(void);
821 void __use_pre_cond_states(void);
822 void __use_cond_true_states(void);
823 void __use_cond_false_states(void);
824 void __push_cond_stacks(void);
825 void __fold_in_set_states(void);
826 void __free_set_states(void);
827 struct stree *__copy_cond_true_states(void);
828 struct stree *__copy_cond_false_states(void);
829 struct stree *__pop_cond_true_stack(void);
830 struct stree *__pop_cond_false_stack(void);
831 void __and_cond_states(void);
832 void __or_cond_states(void);
833 void __save_pre_cond_states(void);
834 void __discard_pre_cond_states(void);
835 struct stree *__get_true_states(void);
836 struct stree *__get_false_states(void);
837 void __use_cond_states(void);
838 extern struct state_list *__last_base_slist;
840 void __push_continues(void);
841 void __discard_continues(void);
842 void __process_continues(void);
843 void __merge_continues(void);
845 void __push_breaks(void);
846 void __process_breaks(void);
847 int __has_breaks(void);
848 void __merge_breaks(void);
849 void __use_breaks(void);
851 void __save_switch_states(struct expression *switch_expr);
852 void __discard_switches(void);
853 int have_remaining_cases(void);
854 void __merge_switches(struct expression *switch_expr, struct range_list *case_rl);
855 void __push_default(void);
856 void __set_default(void);
857 bool __has_default_case(void);
858 int __pop_default(void);
860 void __push_conditions(void);
861 void __discard_conditions(void);
863 void __save_gotos(const char *name, struct symbol *sym);
864 void __merge_gotos(const char *name, struct symbol *sym);
866 void __discard_fake_states(struct expression *call);
868 void __print_cur_stree(void);
869 bool __print_states(const char *owner);
870 typedef void (check_tracker_hook)(int owner, const char *name, struct symbol *sym, struct smatch_state *state);
871 void add_check_tracker(const char *check_name, check_tracker_hook *fn);
873 /* smatch_hooks.c */
874 void __pass_to_client(void *data, enum hook_type type);
875 void __pass_case_to_client(struct expression *switch_expr,
876 struct range_list *rl);
877 int __has_merge_function(int client_id);
878 struct smatch_state *__client_merge_function(int owner,
879 struct smatch_state *s1,
880 struct smatch_state *s2);
881 struct smatch_state *__client_unmatched_state_function(struct sm_state *sm);
882 void call_pre_merge_hook(struct sm_state *cur, struct sm_state *other);
883 void __push_scope_hooks(void);
884 void __call_scope_hooks(void);
885 void add_array_initialized_hook(void (*hook)(struct expression *array, int nr));
886 void __call_array_initialized_hooks(struct expression *array, int nr);
888 /* smatch_function_hooks.c */
889 void add_fake_call_after_return(struct expression *call);
890 void create_function_hook_hash(void);
891 void __match_initializer_call(struct symbol *sym);
893 struct expression *get_unfaked_call(void);
894 void fake_param_assign_helper(struct expression *call, struct expression *fake_assign);
896 /* smatch_db.c */
897 enum info_type {
898 INTERNAL = 0,
900 * Changing these numbers is a pain. Don't do it. If you ever use a
901 * number it can't be re-used right away so there may be gaps.
902 * We select these in order by type so if the order matters, then give
903 * it a number below 100-999,9000-9999 ranges. */
905 PARAM_LIMIT = 103,
906 PARAM_FILTER = 104,
908 RELEASE = 500,
909 BUF_CLEARED = 501,
910 BUF_ADD = 502,
912 PARAM_VALUE = 1001,
913 BUF_SIZE = 1002,
914 CAPPED_DATA = 1004,
915 RETURN_VALUE = 1005,
916 DEREFERENCE = 1006,
917 RANGE_CAP = 1007,
918 ABSOLUTE_LIMITS = 1010,
919 PARAM_ADD = 1012,
920 PARAM_FREED = 1013,
921 MAYBE_FREED = 2014,
922 DATA_SOURCE = 1014,
923 FUZZY_MAX = 1015,
924 HARD_MAX = 2015,
925 STR_LEN = 1016,
926 ARRAY_LEN = 1017,
927 CAPABLE = 1018,
928 NS_CAPABLE = 1019,
929 CONTAINER = 1020,
930 CASTED_CALL = 1021,
931 TYPE_LINK = 1022,
932 UNTRACKED_PARAM = 1023,
933 LOST_PARAM = 2023,
934 CULL_PATH = 1024,
935 PARAM_SET = 1025,
936 PARAM_USED = 1026,
937 BYTE_UNITS = 1027,
938 COMPARE_LIMIT = 1028,
939 PARAM_COMPARE = 1029,
940 CONSTRAINT = 1031,
941 PASSES_TYPE = 1032,
942 CONSTRAINT_REQUIRED = 1033,
943 BIT_INFO = 1034,
944 NOSPEC = 1035,
945 NOSPEC_WB = 1036,
946 STMT_CNT = 1037,
947 TERMINATED = 1038,
948 SLEEP = 1039,
949 FRESH_ALLOC = 1044,
950 ALLOCATOR = 1045,
951 FUNC_TIME = 1047,
952 POWER_OF_TWO = 1048,
953 POWER_OF_TWO_SET = 1049,
954 BIT_SET = 1051,
955 BIT_CLEAR = 1052,
956 BIT_IS_SET = 1053,
957 BIT_IS_CLEAR = 1054,
959 NEGATIVE_ERROR = 1057,
960 ERR_PTR = 1060,
961 PREEMPT_ADD = 2054,
962 PREEMPT_SUB = 2055,
963 /* put random temporary stuff in the 7000-7999 range for testing */
964 HOST_DATA = 7016,
965 HOST_DATA_SET = 7017,
966 HOST_PTR = 7018,
967 HOST_PTR_SET = 7019,
968 USER_DATA = 8017,
969 USER_DATA_SET = 9017,
970 USER_PTR = 9018,
971 USER_PTR_SET = 9019,
972 NO_OVERFLOW = 8018,
973 NO_OVERFLOW_SIMPLE = 8019,
974 LOCK = 8020,
975 UNLOCK = 8021,
976 HALF_LOCKED = 9022,
977 RESTORE = 9023,
978 KNOWN_LOCKED = 9024,
979 KNOWN_UNLOCKED = 9025,
980 IGNORE_LOCK = 9026,
981 SET_FS = 8022,
982 ATOMIC_INC = 8023,
983 ATOMIC_DEC = 8024,
984 REFCOUNT_INIT = 9025,
985 REFCOUNT_INC = 9027,
986 REFCOUNT_DEC = 9028,
987 NO_SIDE_EFFECT = 8025,
988 FN_ARG_LINK = 8028,
989 DATA_VALUE = 8029,
990 ARRAYSIZE_ARG = 8033,
991 SIZEOF_ARG = 8034,
992 MEMORY_TAG = 8036,
993 MTAG_ASSIGN = 8035,
994 UNITS = 8040,
995 STRING_VALUE = 8041,
996 ALLOC = 8045,
998 BYTE_COUNT = 8050,
999 ELEM_COUNT = 8051,
1000 ELEM_LAST = 8052,
1001 USED_LAST = 8053,
1002 USED_COUNT = 8054,
1004 /* Do not use numbers above 100k */
1007 extern struct sqlite3 *smatch_db;
1008 extern struct sqlite3 *mem_db;
1009 extern struct sqlite3 *cache_db;
1011 bool db_incomplete(void);
1012 void db_ignore_states(int id);
1013 typedef bool (delete_hook)(struct expression *expr);
1014 void add_delete_return_hook(delete_hook *hook);
1015 void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type);
1016 void select_caller_name_sym(void (*fn)(const char *name, struct symbol *sym, char *value), int type);
1017 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm));
1018 void add_caller_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm));
1019 void add_return_info_callback(int owner,
1020 void (*callback)(int return_id, char *return_ranges,
1021 struct expression *returned_expr,
1022 int param,
1023 const char *printed_name,
1024 struct sm_state *sm));
1025 void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr));
1026 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));
1027 void select_call_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value));
1028 void select_return_implies_hook_early(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value));
1029 void select_return_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value));
1030 struct range_list *db_return_vals(struct expression *expr);
1031 struct range_list *db_return_vals_from_str(const char *fn_name);
1032 struct range_list *db_return_vals_no_args(struct expression *expr);
1033 bool split_param_key(const char *value, int *param, char *key, int len);
1034 bool get_implied_rl_from_call_str(struct expression *expr, const char *data, struct range_list **rl);
1035 char *get_chunk_from_key(struct expression *arg, char *key, struct symbol **sym, struct var_sym_list **vsl);
1036 char *get_variable_from_key(struct expression *arg, const char *key, struct symbol **sym);
1037 char *get_name_sym_from_param_key(struct expression *expr, int param, const char *key, struct symbol **sym);
1038 char *get_param_name_var_sym(const char *name, struct symbol *sym);
1039 char *get_param_name(struct sm_state *sm);
1040 const char *get_mtag_name_var_sym(const char *state_name, struct symbol *sym);
1041 const char *get_mtag_name_expr(struct expression *expr);
1042 char *get_data_info_name(struct expression *expr);
1043 char *sm_to_arg_name(struct expression *expr, struct sm_state *sm);
1044 int is_recursive_member(const char *param_name);
1046 char *escape_newlines(const char *str);
1047 void sql_exec(struct sqlite3 *db, int (*callback)(void*, int, char**, char**), void *data, const char *sql);
1049 #define sql_helper(db, call_back, data, sql...) \
1050 do { \
1051 char sql_txt[1024]; \
1053 sqlite3_snprintf(sizeof(sql_txt), sql_txt, sql); \
1054 db_debug("debug: %s\n", sql_txt); \
1055 sql_exec(db, call_back, data, sql_txt); \
1056 } while (0)
1059 #define run_sql(call_back, data, sql...) \
1060 do { \
1061 if (option_no_db) \
1062 break; \
1063 sql_helper(smatch_db, call_back, data, sql); \
1064 } while (0)
1066 #define mem_sql(call_back, data, sql...) \
1067 sql_helper(mem_db, call_back, data, sql)
1069 #define cache_sql(call_back, data, sql...) \
1070 sql_helper(cache_db, call_back, data, sql)
1072 #define sql_insert_helper(table, db, ignore, late, values...) \
1073 do { \
1074 struct sqlite3 *_db = db; \
1076 if (__inline_fn && !_db) \
1077 _db = mem_db; \
1078 if (_db) { \
1079 char buf[1024]; \
1080 char *err, *p = buf; \
1081 int rc; \
1083 p += snprintf(p, buf + sizeof(buf) - p, \
1084 "insert %sinto %s values (", \
1085 ignore ? "or ignore " : "", #table); \
1086 p += snprintf(p, buf + sizeof(buf) - p, values); \
1087 p += snprintf(p, buf + sizeof(buf) - p, ");"); \
1088 db_debug("mem-db: %s\n", buf); \
1089 rc = sqlite3_exec(_db, buf, NULL, NULL, &err); \
1090 if (rc != SQLITE_OK) { \
1091 sm_ierror("SQL error #2: %s", err); \
1092 sm_ierror("SQL: '%s'", buf); \
1093 parse_error = 1; \
1095 break; \
1097 if (option_info) { \
1098 FILE *tmp_fd = sm_outfd; \
1099 sm_outfd = sql_outfd; \
1100 sm_prefix(); \
1101 sm_printf("SQL%s: insert %sinto " #table " values(", \
1102 late ? "_late" : "", ignore ? "or ignore " : ""); \
1103 sm_printf(values); \
1104 sm_printf(");\n"); \
1105 sm_outfd = tmp_fd; \
1107 } while (0)
1109 #define sql_insert(table, values...) sql_insert_helper(table, NULL, 0, 0, values);
1110 #define sql_insert_or_ignore(table, values...) sql_insert_helper(table, NULL, 1, 0, values);
1111 #define sql_insert_late(table, values...) sql_insert_helper(table, NULL, 0, 1, values);
1112 #define sql_insert_cache(table, values...) sql_insert_helper(table, cache_db, 1, 0, values);
1113 #define sql_insert_cache_or_ignore(table, values...) sql_insert_helper(table, cache_db, 1, 0, values);
1115 char *get_static_filter(struct symbol *sym);
1117 void sql_insert_return_states(int return_id, const char *return_ranges,
1118 int type, int param, const char *key, const char *value);
1119 void sql_insert_caller_info(struct expression *call, int type, int param,
1120 const char *key, const char *value);
1121 void sql_insert_function_ptr(const char *fn, const char *struct_name);
1122 void sql_insert_return_values(const char *return_values);
1123 void sql_insert_return_implies(int type, int param, const char *key, const char *value);
1124 void sql_insert_function_type_size(const char *member, const char *ranges);
1125 void sql_insert_function_type_info(int type, const char *struct_type, const char *member, const char *value);
1126 void sql_insert_type_info(int type, const char *member, const char *value);
1127 void sql_insert_local_values(const char *name, const char *value);
1128 void sql_insert_function_type_value(const char *type, const char *value);
1129 void sql_insert_function_type(int param, const char *value);
1130 void sql_insert_parameter_name(int param, const char *value);
1131 void sql_insert_data_info(struct expression *data, int type, const char *value);
1132 void sql_insert_data_info_var_sym(const char *var, struct symbol *sym, int type, const char *value);
1133 void sql_save_constraint(const char *con);
1134 void sql_save_constraint_required(const char *data, int op, const char *limit);
1135 void sql_copy_constraint_required(const char *new_limit, const char *old_limit);
1136 void sql_insert_fn_ptr_data_link(const char *ptr, const char *data);
1137 void sql_insert_fn_data_link(struct expression *fn, int type, int param, const char *key, const char *value);
1138 void sql_insert_mtag_about(mtag_t tag, const char *left_name, const char *right_name);
1139 void sql_insert_mtag_info(mtag_t tag, int type, const char *value);
1140 void sql_insert_mtag_map(mtag_t container, int container_offset, mtag_t tag, int tag_offset);
1141 void sql_insert_mtag_alias(mtag_t orig, mtag_t alias);
1142 int mtag_map_select_container(mtag_t tag, int container_offset, mtag_t *container);
1143 int mtag_map_select_tag(mtag_t container, int offset, mtag_t *tag);
1144 struct smatch_state *get_mtag_return(struct expression *expr, struct smatch_state *state);
1145 struct range_list *swap_mtag_seed(struct expression *expr, struct range_list *rl);
1147 bool is_fn_ptr(struct expression *fn);
1148 void sql_select_return_states(const char *cols, struct expression *call,
1149 int (*callback)(void*, int, char**, char**), void *info);
1150 void sql_select_call_implies(const char *cols, struct expression *call,
1151 int (*callback)(void*, int, char**, char**));
1153 void open_smatch_db(char *db_file);
1155 /* smatch_files.c */
1156 int open_data_file(const char *filename);
1157 int open_schema_file(const char *schema);
1158 struct token *get_tokens_file(const char *filename);
1159 struct string_list *load_strings_from_file(const char *project, const char *filename);
1161 /* smatch.c */
1162 extern char *option_debug_check;
1163 extern char *option_debug_var;
1164 extern char *option_process_function;
1165 extern char *option_project_str;
1166 extern char *bin_dir;
1167 extern char *data_dir;
1168 extern int option_no_data;
1169 extern int option_full_path;
1170 extern int option_call_tree;
1171 extern int num_checks;
1173 enum project_type {
1174 PROJ_NONE,
1175 PROJ_KERNEL,
1176 PROJ_WINE,
1177 PROJ_ILLUMOS_KERNEL,
1178 PROJ_ILLUMOS_USER,
1179 PROJ_UNKNOWN,
1181 extern enum project_type option_project;
1182 const char *check_name(unsigned short id);
1183 int id_from_name(const char *name);
1186 /* smatch_buf_size.c */
1187 int bytes_to_elements(struct expression *expr, int bytes);
1188 int get_array_size(struct expression *expr);
1189 int get_array_size_bytes(struct expression *expr);
1190 int get_array_size_bytes_min(struct expression *expr);
1191 int get_array_size_bytes_max(struct expression *expr);
1192 struct range_list *get_array_size_bytes_rl(struct expression *expr);
1193 int get_real_array_size(struct expression *expr);
1194 int last_member_is_resizable(struct symbol *type);
1195 /* smatch_strlen.c */
1196 bool is_strlen(struct expression *expr);
1197 int get_implied_strlen(struct expression *expr, struct range_list **rl);
1198 int get_size_from_strlen(struct expression *expr);
1200 /* smatch_capped.c */
1201 int is_capped(struct expression *expr);
1202 int is_capped_var_sym(const char *name, struct symbol *sym);
1204 /* smatch_kernel_user_data.c */
1205 int is_user_macro(struct expression *expr);
1206 int is_capped_user_data(struct expression *expr);
1207 int implied_user_data(struct expression *expr, struct range_list **rl);
1208 struct stree *get_user_stree(void);
1209 int get_user_rl(struct expression *expr, struct range_list **rl);
1210 int is_user_rl(struct expression *expr);
1211 int get_user_rl_var_sym(const char *name, struct symbol *sym, struct range_list **rl);
1212 void mark_as_user_data(struct expression *expr, bool isnew);
1213 bool user_rl_capped_var_sym(const char *name, struct symbol *sym);
1214 bool user_rl_capped(struct expression *expr);
1215 struct range_list *var_user_rl(struct expression *expr);
1216 bool we_pass_user_data(struct expression *call);
1217 /* smatch_points_to_user_data.c */
1218 bool is_user_data_fn(struct symbol *fn);
1219 bool is_skb_data(struct expression *expr);
1220 bool is_socket_stuff(struct symbol *sym);
1221 bool points_to_user_data(struct expression *expr);
1222 void set_array_user_ptr(struct expression *expr, bool is_new);
1224 /* smatch_kernel_host_data.c */
1225 int is_host_macro(struct expression *expr);
1226 int get_host_data_fn_param(const char *fn);
1227 int is_capped_host_data(struct expression *expr);
1228 int implied_host_data(struct expression *expr, struct range_list **rl);
1229 struct stree *get_host_stree(void);
1230 int get_host_rl(struct expression *expr, struct range_list **rl);
1231 int is_host_rl(struct expression *expr);
1232 int get_host_rl_var_sym(const char *name, struct symbol *sym, struct range_list **rl);
1233 bool host_rl_capped(struct expression *expr);
1234 struct range_list *var_host_rl(struct expression *expr);
1235 bool we_pass_host_data(struct expression *call);
1236 /* smatch_points_to_host_data.c */
1237 bool is_host_data_fn(struct symbol *fn);
1238 bool points_to_host_data(struct expression *expr);
1239 void set_points_to_host_data(struct expression *expr, bool is_new);
1240 bool is_fn_points_to_host_data(const char *fn);
1241 /* check_locking.c */
1242 void print_held_locks();
1243 /* preempt */
1244 void __preempt_add(void);
1245 void __preempt_sub(void);
1246 int get_preempt_cnt(void);
1247 void clear_preempt_cnt(void);
1248 bool function_decrements_preempt(void);
1249 void add_sleep_callback(expr_func *fn);
1250 unsigned long GFP_DIRECT_RECLAIM(void);
1251 unsigned long GFP_ATOMIC(void);
1253 /* check_assigned_expr.c */
1254 extern int check_assigned_expr_id;
1255 struct expression *get_assigned_expr(struct expression *expr);
1256 struct sm_state *get_assigned_sm(struct expression *expr);
1257 struct expression *get_assigned_expr_recurse(struct expression *expr);
1258 struct expression *get_assigned_expr_name_sym(const char *name, struct symbol *sym);
1259 struct expression *get_assigned_expr_name_sym_recurse(const char *name, struct symbol *sym);
1261 /* smatch_return_to_param.c */
1262 char *map_call_to_param_name_sym(struct expression *expr, struct symbol **sym);
1264 /* smatch_comparison.c */
1265 extern int comparison_id;
1266 #define UNKNOWN_COMPARISON 0
1267 #define IMPOSSIBLE_COMPARISON -1
1268 struct compare_data {
1269 /* The ->left and ->right expression pointers might be NULL (I'm lazy) */
1270 struct expression *left;
1271 const char *left_var;
1272 struct var_sym_list *left_vsl;
1273 int comparison;
1274 struct expression *right;
1275 const char *right_var;
1276 struct var_sym_list *right_vsl;
1278 DECLARE_ALLOCATOR(compare_data);
1279 struct smatch_state *alloc_compare_state(
1280 struct expression *left,
1281 const char *left_var, struct var_sym_list *left_vsl,
1282 int comparison,
1283 struct expression *right,
1284 const char *right_var, struct var_sym_list *right_vsl);
1285 int comparison_intersection(int orig, int op);
1286 int merge_comparisons(int one, int two);
1287 int combine_comparisons(int left_compare, int right_compare);
1288 int state_to_comparison(struct smatch_state *state);
1289 struct smatch_state *merge_compare_states(struct smatch_state *s1, struct smatch_state *s2);
1290 int get_comparison(struct expression *left, struct expression *right);
1291 int get_comparison_no_extra(struct expression *a, struct expression *b);
1292 int get_comparison_strings(const char *one, const char *two);
1293 int possible_comparison(struct expression *a, int comparison, struct expression *b);
1294 struct state_list *get_all_comparisons(struct expression *expr);
1295 struct state_list *get_all_possible_equal_comparisons(struct expression *expr);
1296 void __add_comparison_info(struct expression *expr, struct expression *call, const char *range);
1297 char *get_printed_param_name(struct expression *call, const char *param_name, struct symbol *param_sym);
1298 char *name_sym_to_param_comparison(const char *name, struct symbol *sym);
1299 char *expr_equal_to_param(struct expression *expr, int ignore);
1300 char *expr_lte_to_param(struct expression *expr, int ignore);
1301 char *expr_param_comparison(struct expression *expr, int ignore);
1302 int flip_comparison(int op);
1303 int negate_comparison(int op);
1304 int remove_unsigned_from_comparison(int op);
1305 int param_compare_limit_is_impossible(struct expression *expr, int left_param, char *left_key, char *value);
1306 void filter_by_comparison(struct range_list **rl, int comparison, struct range_list *right);
1307 void __compare_param_limit_hook(struct expression *left_expr, struct expression *right_expr,
1308 const char *state_name,
1309 struct smatch_state *true_state, struct smatch_state *false_state);
1310 int impossibly_high_comparison(struct expression *expr);
1311 void add_comparison_var_sym(
1312 struct expression *left_expr,
1313 const char *left_name, struct var_sym_list *left_vsl,
1314 int comparison,
1315 struct expression *right_expr,
1316 const char *right_name, struct var_sym_list *right_vsl,
1317 struct expression *mod_expr);
1319 /* smatch_sval.c */
1320 sval_t *sval_alloc(sval_t sval);
1321 sval_t *sval_alloc_permanent(sval_t sval);
1322 sval_t sval_blank(struct expression *expr);
1323 sval_t sval_type_val(struct symbol *type, long long val);
1324 sval_t sval_type_fval(struct symbol *type, long double fval);
1325 sval_t sval_from_val(struct expression *expr, long long val);
1326 sval_t sval_from_fval(struct expression *expr, long double fval);
1327 int sval_is_ptr(sval_t sval);
1328 bool sval_is_fp(sval_t sval);
1329 int sval_unsigned(sval_t sval);
1330 int sval_signed(sval_t sval);
1331 int sval_bits(sval_t sval);
1332 int sval_bits_used(sval_t sval);
1333 int sval_is_negative(sval_t sval);
1334 int sval_is_positive(sval_t sval);
1335 int sval_is_min(sval_t sval);
1336 int sval_is_max(sval_t sval);
1337 int sval_is_a_min(sval_t sval);
1338 int sval_is_a_max(sval_t sval);
1339 int sval_is_negative_min(sval_t sval);
1340 int sval_cmp_t(struct symbol *type, sval_t one, sval_t two);
1341 int sval_cmp_val(sval_t one, long long val);
1342 sval_t sval_min(sval_t one, sval_t two);
1343 sval_t sval_min_nonneg(sval_t one, sval_t two);
1344 sval_t sval_max(sval_t one, sval_t two);
1345 int sval_too_low(struct symbol *type, sval_t sval);
1346 int sval_too_high(struct symbol *type, sval_t sval);
1347 int sval_fits(struct symbol *type, sval_t sval);
1348 sval_t sval_cast(struct symbol *type, sval_t sval);
1349 sval_t sval_preop(sval_t sval, int op);
1350 sval_t sval_binop(sval_t left, int op, sval_t right);
1351 int sval_binop_overflows(sval_t left, int op, sval_t right);
1352 int sval_binop_overflows_no_sign(sval_t left, int op, sval_t right);
1353 int find_first_zero_bit(unsigned long long uvalue);
1354 int sm_fls64(unsigned long long uvalue);
1355 unsigned long long fls_mask(unsigned long long uvalue);
1356 unsigned long long sval_fls_mask(sval_t sval);
1357 const char *sval_to_str(sval_t sval);
1358 const char *sval_to_str_or_err_ptr(sval_t sval);
1359 const char *sval_to_numstr(sval_t sval);
1360 sval_t ll_to_sval(long long val);
1362 /* smatch_string_list.c */
1363 int list_has_string(struct string_list *str_list, const char *str);
1364 int insert_string(struct string_list **str_list, const char *str);
1365 struct string_list *clone_str_list(struct string_list *orig);
1366 struct string_list *combine_string_lists(struct string_list *one, struct string_list *two);
1368 /* smatch_start_states.c */
1369 struct stree *get_start_states(void);
1371 /* smatch_recurse.c */
1372 int recurse(struct expression *expr,
1373 int (func)(struct expression *expr, void *p),
1374 void *param, int nr);
1375 int has_symbol(struct expression *expr, struct symbol *sym);
1376 int has_variable(struct expression *expr, struct expression *var);
1377 int has_inc_dec(struct expression *expr);
1379 /* smatch_stored_conditions.c */
1380 struct smatch_state *get_stored_condition(struct expression *expr);
1381 struct expression_list *get_conditions(struct expression *expr);
1382 struct sm_state *stored_condition_implication_hook(struct expression *expr,
1383 struct state_list **true_stack,
1384 struct state_list **false_stack);
1385 /* smatch_parsed_conditions.c */
1386 struct sm_state *parsed_condition_implication_hook(struct expression *expr,
1387 struct state_list **true_stack,
1388 struct state_list **false_stack);
1389 /* smatch_comparison.c */
1390 struct sm_state *comparison_implication_hook(struct expression *expr,
1391 struct state_list **true_stack,
1392 struct state_list **false_stack);
1394 /* check_string_len.c */
1395 int get_formatted_string_size(struct expression *call, int arg);
1396 int get_formatted_string_min_size(struct expression *call, int arg);
1398 /* smatch_param_set.c */
1399 int param_was_set(struct expression *expr);
1400 int param_was_set_var_sym(const char *name, struct symbol *sym);
1401 void print_limited_param_set(int return_id, char *return_ranges, struct expression *expr);
1402 /* param_clear */
1403 void __promote_sets_to_clears(int return_id, char *return_ranges, struct expression *expr);
1404 bool parent_was_PARAM_CLEAR(const char *name, struct symbol *sym);
1405 bool parent_was_PARAM_CLEAR_ZERO(const char *name, struct symbol *sym);
1407 /* smatch_param_filter.c */
1408 int param_has_filter_data(struct sm_state *sm);
1410 /* smatch_links.c */
1411 void set_up_link_functions(int id, int linkid);
1412 struct smatch_state *merge_link_states(struct smatch_state *s1, struct smatch_state *s2);
1413 void store_link(int link_id, const char *name, struct symbol *sym, const char *link_name, struct symbol *link_sym);
1415 /* check_buf_comparison */
1416 const char *limit_type_str(unsigned int limit_type);
1417 struct expression *get_size_variable(struct expression *buf, int *limit_type);
1418 struct expression *get_array_variable(struct expression *size);
1419 int buf_comparison_index_ok(struct expression *expr);
1420 bool buf_comp_has_bytes(struct expression *buf, struct expression *var);
1421 bool buf_comp2_has_bytes(struct expression *buf_expr, struct expression *var);
1423 /* smatch_untracked_param.c */
1424 void mark_untracked(struct expression *expr, int param, const char *key, const char *value);
1425 void mark_call_params_untracked(struct expression *call);
1426 void add_untracked_param_hook(void (func)(struct expression *call, int param));
1427 void add_lost_param_hook(void (func)(struct expression *call, int param));
1428 void mark_all_params_untracked(int return_id, char *return_ranges, struct expression *expr);
1429 /* smatch_untracked_var.c */
1430 bool is_untracked(struct expression *expr);
1432 /* smatch_strings.c */
1433 struct state_list *get_strings(struct expression *expr);
1434 struct expression *fake_string_from_mtag(mtag_t tag);
1436 /* smatch_estate.c */
1437 int estate_get_single_value(struct smatch_state *state, sval_t *sval);
1439 /* smatch_address.c */
1440 int get_address_rl(struct expression *expr, struct range_list **rl);
1441 int get_member_offset(struct symbol *type, const char *member_name);
1442 int get_member_offset_from_deref(struct expression *expr);
1444 /* for now this is in smatch_used_parameter.c */
1445 void __get_state_hook(int owner, const char *name, struct symbol *sym);
1446 extern int __ignore_param_used;
1448 /* smatch_buf_comparison.c */
1449 int db_var_is_array_limit(struct expression *array, const char *name, struct var_sym_list *vsl);
1451 struct range_list *get_fs(void);
1453 struct stree *get_all_return_states(void);
1454 struct stree_stack *get_all_return_strees(void);
1455 int on_atomic_dec_path(void);
1456 int was_inced(const char *name, struct symbol *sym);
1457 void set_refcount_inc(char *name, struct symbol *sym);
1458 void set_refcount_dec(char *name, struct symbol *sym);
1459 void add_refcount_init_hook(name_sym_hook *hook);
1460 void add_refcount_inc_hook(name_sym_hook *hook);
1461 void add_refcount_dec_hook(name_sym_hook *hook);
1463 /* smatch_constraints.c */
1464 char *get_constraint_str(struct expression *expr);
1465 struct constraint_list *get_constraints(struct expression *expr);
1466 char *unmet_constraint(struct expression *data, struct expression *offset);
1467 char *get_required_constraint(const char *data_str);
1469 /* smatch_container_of.c */
1470 int get_param_from_container_of(struct expression *expr);
1471 int get_offset_from_container_of(struct expression *expr);
1472 char *get_container_name(struct expression *container, struct expression *expr);
1474 /* smatch_mtag.c */
1475 unsigned long long str_to_llu_hash_helper(const char *str);
1476 unsigned long long str_to_llu_hash(const char *str);
1477 struct symbol *get_symbol_from_mtag(mtag_t tag);
1478 mtag_t str_to_mtag(const char *str);
1479 int get_string_mtag(struct expression *expr, mtag_t *tag);
1480 int get_toplevel_mtag(struct symbol *sym, mtag_t *tag);
1481 int create_mtag_alias(mtag_t tag, struct expression *expr, mtag_t *new);
1482 int expr_to_mtag_offset(struct expression *expr, mtag_t *tag, int *offset);
1483 void update_mtag_data(struct expression *expr, struct smatch_state *state);
1484 int get_mtag_sval(struct expression *expr, sval_t *sval);
1486 /* Trinity fuzzer stuff */
1487 const char *get_syscall_arg_type(struct symbol *sym);
1489 /* smatch_bits.c */
1490 int binfo_equiv(struct bit_info *one, struct bit_info *two);
1491 struct bit_info *alloc_bit_info(unsigned long long set, unsigned long long possible);
1492 struct smatch_state *alloc_bstate(unsigned long long set, unsigned long long possible);
1493 struct smatch_state *merge_bstates(struct smatch_state *one_state, struct smatch_state *two_state);
1495 /* smatch_param_bits_set.c */
1496 void __set_param_modified_helper(struct expression *expr, struct smatch_state *state);
1497 void __set_param_modified_helper_sym(const char *name, struct symbol *sym,
1498 struct smatch_state *state);
1500 /* smatch_param_bits_clear.c */
1501 void __set_param_modified_helper_clear(struct expression *expr, struct smatch_state *state);
1502 void __set_param_modified_helper_sym_clear(const char *name, struct symbol *sym,
1503 struct smatch_state *state);
1505 /* smatch_bit_info.c */
1506 struct bit_info *rl_to_binfo(struct range_list *rl);
1507 struct bit_info *get_bit_info(struct expression *expr);
1508 struct bit_info *get_bit_info_var_sym(const char *name, struct symbol *sym);
1510 /* smatch_mem_tracker.c */
1511 extern int option_mem;
1512 unsigned long get_mem_kb(void);
1513 unsigned long get_max_memory(void);
1515 /* smatch_goto_tracker.c */
1516 struct sm_state *get_goto_sm_state(void);
1518 /* check_is_nospec.c */
1519 bool is_nospec(struct expression *expr);
1520 long get_stmt_cnt(void);
1522 /* check_kernel_units.c */
1523 struct smatch_state *get_units(struct expression *expr);
1524 char *get_unit_str(struct expression *expr);
1525 bool is_array_size_units(struct expression *expr);
1526 /* smatch_nul_terminator.c */
1527 bool is_nul_terminated_var_sym(const char *name, struct symbol *sym);
1528 bool is_nul_terminated(struct expression *expr);
1529 /* check_kernel.c */
1530 bool is_ignored_kernel_data(const char *name);
1531 int get_gfp_param(struct expression *expr);
1533 bool is_fresh_alloc_var_sym(const char *var, struct symbol *sym);
1534 bool is_fresh_alloc(struct expression *expr);
1535 bool is_freed_var_sym(const char *name, struct symbol *sym);
1536 void track_freed_param(struct expression *expr, struct smatch_state *state);
1537 void track_freed_param_var_sym(const char *name, struct symbol *sym,
1538 struct smatch_state *state);
1539 bool is_part_of_condition(struct expression *expr);
1540 bool is_percent_p_print(struct expression *expr);
1542 void add_free_hook(name_sym_hook *hook);
1544 /* smatch_unconstant_macros.c */
1545 int is_unconstant_macro(struct expression *expr);
1547 /* smatch_assigned_state.c */
1548 void add_state_assigned_hook(int owner, sm_hook *call_back);
1549 void add_ssa_state_assigned_hook(int owner, sm_hook *call_back);
1551 bool possible_err_ptr(struct expression *expr);
1553 static inline bool type_is_ptr(struct symbol *type)
1555 return type &&
1556 (type->type == SYM_PTR ||
1557 type->type == SYM_ARRAY ||
1558 type->type == SYM_FN);
1561 static inline bool type_is_fp(struct symbol *type)
1563 return type &&
1564 (type == &float_ctype ||
1565 type == &double_ctype ||
1566 type == &ldouble_ctype);
1569 static inline int type_bits(struct symbol *type)
1571 if (!type)
1572 return 0;
1573 if (type_is_ptr(type))
1574 return bits_in_pointer;
1575 if (!type->examined)
1576 examine_symbol_type(type);
1577 return type->bit_size;
1580 static inline int type_unsigned(struct symbol *base_type)
1582 if (!base_type)
1583 return 0;
1584 if (is_ptr_type(base_type))
1585 return 1;
1586 if (base_type->ctype.modifiers & MOD_UNSIGNED)
1587 return 1;
1588 return 0;
1591 static inline int type_positive_bits(struct symbol *type)
1593 if (!type)
1594 return 0;
1595 if (is_ptr_type(type))
1596 return bits_in_pointer;
1597 if (type_unsigned(type))
1598 return type_bits(type);
1599 return type_bits(type) - 1;
1602 static inline int sval_positive_bits(sval_t sval)
1604 return type_positive_bits(sval.type);
1608 * Returns -1 if one is smaller, 0 if they are the same and 1 if two is larger.
1611 static inline int fp_cmp(sval_t one, sval_t two)
1613 struct symbol *type;
1615 if (sval_is_fp(one) && sval_is_fp(two))
1616 type = type_bits(one.type) > type_bits(two.type) ? one.type : two.type;
1617 else if (sval_is_fp(one))
1618 type = one.type;
1619 else
1620 type = two.type;
1622 one = sval_cast(type, one);
1623 two = sval_cast(type, two);
1625 if (one.type == &float_ctype) {
1626 if (one.fvalue < two.fvalue)
1627 return -1;
1628 if (one.fvalue == two.fvalue)
1629 return 0;
1630 return 1;
1632 if (one.type == &double_ctype) {
1633 if (one.dvalue < two.dvalue)
1634 return -1;
1635 if (one.dvalue == two.dvalue)
1636 return 0;
1637 return 1;
1639 if (one.type == &ldouble_ctype) {
1640 if (one.ldvalue < two.ldvalue)
1641 return -1;
1642 if (one.ldvalue == two.ldvalue)
1643 return 0;
1644 return 1;
1646 sm_perror("bad type in fp_cmp(): %s", type_to_str(type));
1647 return 1;
1650 static inline int sval_cmp(sval_t one, sval_t two)
1652 struct symbol *type;
1654 if (sval_is_fp(one) || sval_is_fp(two))
1655 return fp_cmp(one, two);
1657 type = one.type;
1658 if (sval_positive_bits(two) > sval_positive_bits(one))
1659 type = two.type;
1660 if (type_bits(type) < 31)
1661 type = &int_ctype;
1663 one = sval_cast(type, one);
1664 two = sval_cast(type, two);
1666 if (type_unsigned(type)) {
1667 if (one.uvalue < two.uvalue)
1668 return -1;
1669 if (one.uvalue == two.uvalue)
1670 return 0;
1671 return 1;
1673 /* fix me handle type promotion and unsigned values */
1674 if (one.value < two.value)
1675 return -1;
1676 if (one.value == two.value)
1677 return 0;
1678 return 1;
1681 #endif /* !SMATCH_H_ */