debug: move __print_states() to smatch_states.c
[smatch.git] / smatch.h
blob219dca729779324e2a7adddaa3759d08ee5f96ff
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"
34 typedef struct {
35 struct symbol *type;
36 union {
37 long long value;
38 unsigned long long uvalue;
39 float fvalue;
40 double dvalue;
41 long double ldvalue;
43 } sval_t;
45 typedef long long mtag_t;
47 struct smatch_state {
48 const char *name;
49 void *data;
51 #define STATE(_x) static struct smatch_state _x = { .name = #_x }
52 extern struct smatch_state undefined;
53 extern struct smatch_state ghost;
54 extern struct smatch_state merged;
55 extern struct smatch_state true_state;
56 extern struct smatch_state false_state;
57 DECLARE_ALLOCATOR(smatch_state);
59 static inline void *INT_PTR(int i)
61 return (void *)(long)i;
64 static inline int PTR_INT(void *p)
66 return (int)(long)p;
69 struct tracker {
70 char *name;
71 struct symbol *sym;
72 unsigned short owner;
74 DECLARE_ALLOCATOR(tracker);
75 DECLARE_PTR_LIST(tracker_list, struct tracker);
76 DECLARE_PTR_LIST(stree_stack, struct stree);
78 /* The first 3 struct members must match struct tracker */
79 struct sm_state {
80 const char *name;
81 struct symbol *sym;
82 unsigned short owner;
83 unsigned short merged:1;
84 unsigned int line;
85 struct smatch_state *state;
86 struct stree *pool;
87 struct sm_state *left;
88 struct sm_state *right;
89 struct state_list *possible;
92 struct var_sym {
93 char *var;
94 struct symbol *sym;
96 DECLARE_ALLOCATOR(var_sym);
97 DECLARE_PTR_LIST(var_sym_list, struct var_sym);
99 struct constraint {
100 int op;
101 int id;
103 DECLARE_PTR_LIST(constraint_list, struct constraint);
105 struct alloc_info {
106 const char *fn;
107 int size_param, nr;
109 extern struct alloc_info *alloc_funcs;
111 struct bit_info {
112 unsigned long long set;
113 unsigned long long possible;
116 enum hook_type {
117 EXPR_HOOK,
118 EXPR_HOOK_AFTER,
119 STMT_HOOK,
120 STMT_HOOK_AFTER,
121 SYM_HOOK,
122 STRING_HOOK,
123 DECLARATION_HOOK,
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 SELECT_HOOK,
132 WHOLE_CONDITION_HOOK,
133 FUNCTION_CALL_HOOK_BEFORE,
134 FUNCTION_CALL_HOOK,
135 CALL_HOOK_AFTER_INLINE,
136 FUNCTION_CALL_HOOK_AFTER_DB,
137 CALL_ASSIGNMENT_HOOK,
138 MACRO_ASSIGNMENT_HOOK,
139 BINOP_HOOK,
140 OP_HOOK,
141 DEREF_HOOK,
142 CASE_HOOK,
143 ASM_HOOK,
144 CAST_HOOK,
145 SIZEOF_HOOK,
146 BASE_HOOK,
147 FUNC_DEF_HOOK,
148 AFTER_DEF_HOOK,
149 END_FUNC_HOOK,
150 AFTER_FUNC_HOOK,
151 RETURN_HOOK,
152 INLINE_FN_START,
153 INLINE_FN_END,
154 END_FILE_HOOK,
155 NUM_HOOKS,
158 #define TRUE 1
159 #define FALSE 0
161 struct range_list;
163 void add_hook(void *func, enum hook_type type);
164 typedef struct smatch_state *(merge_func_t)(struct smatch_state *s1, struct smatch_state *s2);
165 typedef struct smatch_state *(unmatched_func_t)(struct sm_state *state);
166 void add_merge_hook(int client_id, merge_func_t *func);
167 void add_unmatched_state_hook(int client_id, unmatched_func_t *func);
168 void add_pre_merge_hook(int client_id, void (*hook)(struct sm_state *cur, struct sm_state *other));
169 typedef void (scope_hook)(void *data);
170 void add_scope_hook(scope_hook *hook, void *data);
171 typedef void (func_hook)(const char *fn, struct expression *expr, void *data);
172 typedef void (implication_hook)(const char *fn, struct expression *call_expr,
173 struct expression *assign_expr, void *data);
174 typedef void (return_implies_hook)(struct expression *call_expr,
175 int param, char *key, char *value);
176 typedef int (implied_return_hook)(struct expression *call_expr, void *info, struct range_list **rl);
177 void add_function_hook(const char *look_for, func_hook *call_back, void *data);
179 void add_function_assign_hook(const char *look_for, func_hook *call_back,
180 void *info);
181 void add_implied_return_hook(const char *look_for,
182 implied_return_hook *call_back,
183 void *info);
184 void add_macro_assign_hook(const char *look_for, func_hook *call_back,
185 void *info);
186 void add_macro_assign_hook_extra(const char *look_for, func_hook *call_back,
187 void *info);
188 void return_implies_state(const char *look_for, long long start, long long end,
189 implication_hook *call_back, void *info);
190 void return_implies_state_sval(const char *look_for, sval_t start, sval_t end,
191 implication_hook *call_back, void *info);
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 int get_implied_return(struct expression *expr, struct range_list **rl);
196 void allocate_hook_memory(void);
198 struct modification_data {
199 struct smatch_state *prev;
200 struct expression *cur;
203 typedef void (modification_hook)(struct sm_state *sm, struct expression *mod_expr);
204 void add_modification_hook(int owner, modification_hook *call_back);
205 void add_modification_hook_late(int owner, modification_hook *call_back);
206 struct smatch_state *get_modification_state(struct expression *expr);
208 int outside_of_function(void);
209 const char *get_filename(void);
210 const char *get_base_file(void);
211 char *get_function(void);
212 int get_lineno(void);
213 extern int final_pass;
214 extern struct symbol *cur_func_sym;
215 extern int option_debug;
216 extern int local_debug;
217 extern int debug_db;
218 bool debug_implied(void);
219 extern int option_info;
220 extern int option_spammy;
221 extern char *trace_variable;
222 extern struct stree *global_states;
223 int is_skipped_function(void);
224 int is_silenced_function(void);
225 extern bool implications_off;
227 /* smatch_impossible.c */
228 int is_impossible_path(void);
229 void set_path_impossible(void);
231 extern FILE *sm_outfd;
232 extern FILE *sql_outfd;
233 extern FILE *caller_info_fd;
234 extern int sm_nr_checks;
235 extern int sm_nr_errors;
238 * How to use these routines:
240 * sm_fatal(): an internal error of some kind that should immediately exit
241 * sm_ierror(): an internal error
242 * sm_perror(): an internal error from parsing input source
243 * sm_error(): an error from input source
244 * sm_warning(): a warning from input source
245 * sm_info(): info message (from option_info)
246 * sm_debug(): debug message
247 * sm_msg(): other message (please avoid using this)
250 #define sm_printf(msg...) do { \
251 if (final_pass || option_debug || local_debug || debug_db) \
252 fprintf(sm_outfd, msg); \
253 } while (0)
255 static inline void sm_prefix(void)
257 sm_printf("%s:%d %s() ", get_filename(), get_lineno(), get_function());
260 static inline void print_implied_debug_msg();
262 extern bool __silence_warnings_for_stmt;
264 #define sm_print_msg(type, msg...) \
265 do { \
266 print_implied_debug_msg(); \
267 if (!final_pass && !option_debug && !local_debug && !debug_db) \
268 break; \
269 if (__silence_warnings_for_stmt && !option_debug && !local_debug) \
270 break; \
271 if (!option_info && is_silenced_function()) \
272 break; \
273 sm_prefix(); \
274 if (type == 1) { \
275 sm_printf("warn: "); \
276 sm_nr_checks++; \
277 } else if (type == 2) { \
278 sm_printf("error: "); \
279 sm_nr_checks++; \
280 } else if (type == 3) { \
281 sm_printf("parse error: "); \
282 sm_nr_errors++; \
284 sm_printf(msg); \
285 sm_printf("\n"); \
286 } while (0)
288 #define sm_msg(msg...) do { sm_print_msg(0, msg); } while (0)
290 extern char *implied_debug_msg;
291 static inline void print_implied_debug_msg(void)
293 static struct symbol *last_printed = NULL;
295 if (!implied_debug_msg)
296 return;
297 if (last_printed == cur_func_sym)
298 return;
299 last_printed = cur_func_sym;
300 sm_msg("%s", implied_debug_msg);
303 #define sm_debug(msg...) do { if (option_debug) sm_printf(msg); } while (0)
304 #define db_debug(msg...) do { if (option_debug || debug_db) sm_printf(msg); } while (0)
306 #define sm_info(msg...) do { \
307 if (option_debug || (option_info && final_pass)) { \
308 sm_prefix(); \
309 sm_printf("info: "); \
310 sm_printf(msg); \
311 sm_printf("\n"); \
313 } while(0)
315 #define sm_warning(msg...) do { sm_print_msg(1, msg); } while (0)
316 #define sm_error(msg...) do { sm_print_msg(2, msg); } while (0)
317 #define sm_perror(msg...) do { sm_print_msg(3, msg); } while (0)
319 static inline void sm_fatal(const char *fmt, ...)
321 va_list args;
323 va_start(args, fmt);
324 vfprintf(sm_outfd, fmt, args);
325 va_end(args);
327 fprintf(sm_outfd, "\n");
329 exit(1);
332 static inline void sm_ierror(const char *fmt, ...)
334 va_list args;
336 sm_nr_errors++;
338 fprintf(sm_outfd, "internal error: ");
340 va_start(args, fmt);
341 vfprintf(sm_outfd, fmt, args);
342 va_end(args);
344 fprintf(sm_outfd, "\n");
346 #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
348 struct smatch_state *__get_state(int owner, const char *name, struct symbol *sym);
349 struct smatch_state *get_state(int owner, const char *name, struct symbol *sym);
350 struct smatch_state *get_state_expr(int owner, struct expression *expr);
351 struct state_list *get_possible_states(int owner, const char *name,
352 struct symbol *sym);
353 struct state_list *get_possible_states_expr(int owner, struct expression *expr);
354 struct sm_state *set_state(int owner, const char *name, struct symbol *sym,
355 struct smatch_state *state);
356 struct sm_state *set_state_expr(int owner, struct expression *expr,
357 struct smatch_state *state);
358 void delete_state(int owner, const char *name, struct symbol *sym);
359 void delete_state_expr(int owner, struct expression *expr);
360 void __delete_all_states_sym(struct symbol *sym);
361 void set_true_false_states(int owner, const char *name, struct symbol *sym,
362 struct smatch_state *true_state,
363 struct smatch_state *false_state);
364 void set_true_false_states_expr(int owner, struct expression *expr,
365 struct smatch_state *true_state,
366 struct smatch_state *false_state);
368 struct stree *get_all_states_from_stree(int owner, struct stree *source);
369 struct stree *get_all_states_stree(int id);
370 struct stree *__get_cur_stree(void);
371 int is_reachable(void);
372 void add_get_state_hook(void (*fn)(int owner, const char *name, struct symbol *sym));
374 /* smatch_helper.c */
375 DECLARE_PTR_LIST(int_stack, int);
376 char *alloc_string(const char *str);
377 char *alloc_string_newline(const char *str);
378 void free_string(char *str);
379 void append(char *dest, const char *data, int buff_len);
380 void remove_parens(char *str);
381 struct smatch_state *alloc_state_num(int num);
382 struct smatch_state *alloc_state_str(const char *name);
383 struct smatch_state *merge_str_state(struct smatch_state *s1, struct smatch_state *s2);
384 struct smatch_state *alloc_state_expr(struct expression *expr);
385 struct expression *get_argument_from_call_expr(struct expression_list *args,
386 int num);
388 char *expr_to_var(struct expression *expr);
389 struct symbol *expr_to_sym(struct expression *expr);
390 char *expr_to_str(struct expression *expr);
391 char *expr_to_str_sym(struct expression *expr,
392 struct symbol **sym_ptr);
393 char *expr_to_var_sym(struct expression *expr,
394 struct symbol **sym_ptr);
395 char *expr_to_known_chunk_sym(struct expression *expr, struct symbol **sym);
396 char *expr_to_chunk_sym_vsl(struct expression *expr, struct symbol **sym, struct var_sym_list **vsl);
397 int get_complication_score(struct expression *expr);
399 int sym_name_is(const char *name, struct expression *expr);
400 int get_const_value(struct expression *expr, sval_t *sval);
401 int get_value(struct expression *expr, sval_t *val);
402 int get_implied_value(struct expression *expr, sval_t *val);
403 int get_implied_value_fast(struct expression *expr, sval_t *sval);
404 int get_implied_min(struct expression *expr, sval_t *sval);
405 int get_implied_max(struct expression *expr, sval_t *val);
406 int get_hard_max(struct expression *expr, sval_t *sval);
407 int get_fuzzy_min(struct expression *expr, sval_t *min);
408 int get_fuzzy_max(struct expression *expr, sval_t *max);
409 int get_absolute_min(struct expression *expr, sval_t *sval);
410 int get_absolute_max(struct expression *expr, sval_t *sval);
411 int parse_call_math(struct expression *expr, char *math, sval_t *val);
412 int parse_call_math_rl(struct expression *call, const char *math, struct range_list **rl);
413 const char *get_allocation_math(struct expression *expr);
414 char *get_value_in_terms_of_parameter_math(struct expression *expr);
415 char *get_value_in_terms_of_parameter_math_var_sym(const char *var, struct symbol *sym);
416 int expr_is_zero(struct expression *expr);
417 int known_condition_true(struct expression *expr);
418 int known_condition_false(struct expression *expr);
419 int implied_condition_true(struct expression *expr);
420 int implied_condition_false(struct expression *expr);
421 int can_integer_overflow(struct symbol *type, struct expression *expr);
422 void clear_math_cache(void);
423 void set_fast_math_only(void);
424 void clear_fast_math_only(void);
426 int is_array(struct expression *expr);
427 struct expression *get_array_base(struct expression *expr);
428 struct expression *get_array_offset(struct expression *expr);
429 const char *show_state(struct smatch_state *state);
430 struct statement *get_expression_statement(struct expression *expr);
431 struct expression *strip_parens(struct expression *expr);
432 struct expression *strip_expr(struct expression *expr);
433 struct expression *strip_expr_set_parent(struct expression *expr);
434 void scoped_state(int my_id, const char *name, struct symbol *sym);
435 int is_error_return(struct expression *expr);
436 int getting_address(struct expression *expr);
437 int get_struct_and_member(struct expression *expr, const char **type, const char **member);
438 char *get_member_name(struct expression *expr);
439 char *get_fnptr_name(struct expression *expr);
440 int cmp_pos(struct position pos1, struct position pos2);
441 int positions_eq(struct position pos1, struct position pos2);
442 struct statement *get_current_statement(void);
443 struct statement *get_prev_statement(void);
444 struct expression *get_last_expr_from_expression_stmt(struct expression *expr);
445 int get_param_num_from_sym(struct symbol *sym);
446 int get_param_num(struct expression *expr);
447 struct symbol *get_param_sym_from_num(int num);
448 int ms_since(struct timeval *start);
449 int parent_is_gone_var_sym(const char *name, struct symbol *sym);
450 int parent_is_gone(struct expression *expr);
451 int invert_op(int op);
452 int op_remove_assign(int op);
453 int expr_equiv(struct expression *one, struct expression *two);
454 void push_int(struct int_stack **stack, int num);
455 int pop_int(struct int_stack **stack);
457 /* smatch_type.c */
458 struct symbol *get_real_base_type(struct symbol *sym);
459 int type_bytes(struct symbol *type);
460 int array_bytes(struct symbol *type);
461 struct symbol *get_pointer_type(struct expression *expr);
462 struct symbol *get_type(struct expression *expr);
463 struct symbol *get_final_type(struct expression *expr);
464 struct symbol *get_promoted_type(struct symbol *left, struct symbol *right);
465 int type_signed(struct symbol *base_type);
466 int expr_unsigned(struct expression *expr);
467 int expr_signed(struct expression *expr);
468 int returns_unsigned(struct symbol *base_type);
469 int is_pointer(struct expression *expr);
470 int returns_pointer(struct symbol *base_type);
471 sval_t sval_type_max(struct symbol *base_type);
472 sval_t sval_type_min(struct symbol *base_type);
473 int nr_bits(struct expression *expr);
474 int is_void_pointer(struct expression *expr);
475 int is_char_pointer(struct expression *expr);
476 int is_string(struct expression *expr);
477 int is_static(struct expression *expr);
478 bool is_local_variable(struct expression *expr);
479 int types_equiv(struct symbol *one, struct symbol *two);
480 int fn_static(void);
481 const char *global_static();
482 struct symbol *cur_func_return_type(void);
483 struct symbol *get_arg_type(struct expression *fn, int arg);
484 struct symbol *get_member_type_from_key(struct expression *expr, const char *key);
485 struct symbol *get_arg_type_from_key(struct expression *fn, int param, struct expression *arg, const char *key);
486 int is_struct(struct expression *expr);
487 char *type_to_str(struct symbol *type);
489 /* smatch_ignore.c */
490 void add_ignore(int owner, const char *name, struct symbol *sym);
491 int is_ignored(int owner, const char *name, struct symbol *sym);
492 void add_ignore_expr(int owner, struct expression *expr);
493 int is_ignored_expr(int owner, struct expression *expr);
495 /* smatch_var_sym */
496 struct var_sym *alloc_var_sym(const char *var, struct symbol *sym);
497 struct var_sym_list *expr_to_vsl(struct expression *expr);
498 void add_var_sym(struct var_sym_list **list, const char *var, struct symbol *sym);
499 void add_var_sym_expr(struct var_sym_list **list, struct expression *expr);
500 void del_var_sym(struct var_sym_list **list, const char *var, struct symbol *sym);
501 int in_var_sym_list(struct var_sym_list *list, const char *var, struct symbol *sym);
502 struct var_sym_list *clone_var_sym_list(struct var_sym_list *from_vsl);
503 void merge_var_sym_list(struct var_sym_list **dest, struct var_sym_list *src);
504 struct var_sym_list *combine_var_sym_lists(struct var_sym_list *one, struct var_sym_list *two);
505 int var_sym_lists_equiv(struct var_sym_list *one, struct var_sym_list *two);
506 void free_var_sym_list(struct var_sym_list **list);
507 void free_var_syms_and_list(struct var_sym_list **list);
509 /* smatch_tracker */
510 struct tracker *alloc_tracker(int owner, const char *name, struct symbol *sym);
511 void add_tracker(struct tracker_list **list, int owner, const char *name,
512 struct symbol *sym);
513 void add_tracker_expr(struct tracker_list **list, int owner, struct expression *expr);
514 void del_tracker(struct tracker_list **list, int owner, const char *name,
515 struct symbol *sym);
516 int in_tracker_list(struct tracker_list *list, int owner, const char *name,
517 struct symbol *sym);
518 void free_tracker_list(struct tracker_list **list);
519 void free_trackers_and_list(struct tracker_list **list);
521 /* smatch_conditions */
522 int in_condition(void);
524 /* smatch_flow.c */
526 extern int __in_fake_assign;
527 extern int __in_fake_parameter_assign;
528 extern int __in_fake_struct_assign;
529 extern int in_fake_env;
530 void smatch (struct string_list *filelist);
531 int inside_loop(void);
532 int definitely_inside_loop(void);
533 struct expression *get_switch_expr(void);
534 int in_expression_statement(void);
535 void __process_post_op_stack(void);
536 void __split_expr(struct expression *expr);
537 void __split_label_stmt(struct statement *stmt);
538 void __split_stmt(struct statement *stmt);
539 extern int __in_function_def;
540 extern int __in_unmatched_hook;
541 extern int option_assume_loops;
542 extern int option_two_passes;
543 extern int option_no_db;
544 extern int option_file_output;
545 extern int option_time;
546 extern struct expression_list *big_expression_stack;
547 extern struct expression_list *big_condition_stack;
548 extern struct statement_list *big_statement_stack;
549 int is_assigned_call(struct expression *expr);
550 int inlinable(struct expression *expr);
551 extern int __inline_call;
552 extern struct expression *__inline_fn;
553 extern int __in_pre_condition;
554 extern int __bail_on_rest_of_function;
555 extern struct statement *__prev_stmt;
556 extern struct statement *__cur_stmt;
557 extern struct statement *__next_stmt;
558 void init_fake_env(void);
559 void end_fake_env(void);
560 int time_parsing_function(void);
561 bool taking_too_long(void);
563 /* smatch_struct_assignment.c */
564 struct expression *get_faked_expression(void);
565 void __fake_struct_member_assignments(struct expression *expr);
567 /* smatch_project.c */
568 int is_no_inline_function(const char *function);
570 /* smatch_conditions */
571 void __split_whole_condition(struct expression *expr);
572 void __handle_logic(struct expression *expr);
573 int is_condition(struct expression *expr);
574 int __handle_condition_assigns(struct expression *expr);
575 int __handle_select_assigns(struct expression *expr);
576 int __handle_expr_statement_assigns(struct expression *expr);
578 /* smatch_implied.c */
579 struct range_list_stack;
580 void param_limit_implications(struct expression *expr, int param, char *key, char *value);
581 struct stree *__implied_case_stree(struct expression *switch_expr,
582 struct range_list *case_rl,
583 struct range_list_stack **remaining_cases,
584 struct stree **raw_stree);
585 void overwrite_states_using_pool(struct sm_state *gate_sm, struct sm_state *pool_sm);
586 int assume(struct expression *expr);
587 void end_assume(void);
588 int impossible_assumption(struct expression *left, int op, sval_t sval);
590 /* smatch_slist.h */
591 bool has_dynamic_states(unsigned short owner);
592 void set_dynamic_states(unsigned short owner);
594 /* smatch_extras.c */
595 int in_warn_on_macro(void);
596 #define SMATCH_EXTRA 5 /* this is my_id from smatch extra set in smatch.c */
597 extern int RETURN_ID;
599 struct data_range {
600 sval_t min;
601 sval_t max;
604 #define MTAG_ALIAS_BIT (1ULL << 63)
605 #define MTAG_OFFSET_MASK 0xfffULL
606 #define MTAG_SEED 0xdead << 12
608 const extern unsigned long valid_ptr_min;
609 extern unsigned long valid_ptr_max;
610 extern const sval_t valid_ptr_min_sval;
611 extern sval_t valid_ptr_max_sval;
612 extern struct range_list *valid_ptr_rl;
613 void alloc_valid_ptr_rl(void);
615 static const sval_t array_min_sval = {
616 .type = &ptr_ctype,
617 {.value = 100000},
619 static const sval_t array_max_sval = {
620 .type = &ptr_ctype,
621 {.value = ULONG_MAX - 4095},
623 static const sval_t text_seg_min = {
624 .type = &ptr_ctype,
625 {.value = 4096},
627 static const sval_t text_seg_max = {
628 .type = &ptr_ctype,
629 {.value = ULONG_MAX - 4095},
631 static const sval_t data_seg_min = {
632 .type = &ptr_ctype,
633 {.value = 4096},
635 static const sval_t data_seg_max = {
636 .type = &ptr_ctype,
637 {.value = ULONG_MAX - 4095},
639 static const sval_t bss_seg_min = {
640 .type = &ptr_ctype,
641 {.value = 4096},
643 static const sval_t bss_seg_max = {
644 .type = &ptr_ctype,
645 {.value = ULONG_MAX - 4095},
647 static const sval_t stack_seg_min = {
648 .type = &ptr_ctype,
649 {.value = 4096},
651 static const sval_t stack_seg_max = {
652 .type = &ptr_ctype,
653 {.value = ULONG_MAX - 4095},
655 static const sval_t kmalloc_seg_min = {
656 .type = &ptr_ctype,
657 {.value = 4096},
659 static const sval_t kmalloc_seg_max = {
660 .type = &ptr_ctype,
661 {.value = ULONG_MAX - 4095},
663 static const sval_t vmalloc_seg_min = {
664 .type = &ptr_ctype,
665 {.value = 4096},
667 static const sval_t vmalloc_seg_max = {
668 .type = &ptr_ctype,
669 {.value = ULONG_MAX - 4095},
671 static const sval_t fn_ptr_min = {
672 .type = &ptr_ctype,
673 {.value = 4096},
675 static const sval_t fn_ptr_max = {
676 .type = &ptr_ctype,
677 {.value = ULONG_MAX - 4095},
680 char *get_other_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym);
681 char *map_call_to_other_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym);
682 char *map_long_to_short_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym, bool use_stack);
684 #define STRLEN_MAX_RET 1010101
686 /* smatch_absolute.c */
687 int get_absolute_min_helper(struct expression *expr, sval_t *sval);
688 int get_absolute_max_helper(struct expression *expr, sval_t *sval);
690 /* smatch_type_value.c */
691 int get_db_type_rl(struct expression *expr, struct range_list **rl);
692 /* smatch_data_val.c */
693 int get_mtag_rl(struct expression *expr, struct range_list **rl);
694 /* smatch_array_values.c */
695 int get_array_rl(struct expression *expr, struct range_list **rl);
697 /* smatch_states.c */
698 struct stree *__swap_cur_stree(struct stree *stree);
699 void __push_fake_cur_stree();
700 struct stree *__pop_fake_cur_stree();
701 void __free_fake_cur_stree();
702 void __set_fake_cur_stree_fast(struct stree *stree);
703 void __pop_fake_cur_stree_fast(void);
704 void __merge_stree_into_cur(struct stree *stree);
706 int unreachable(void);
707 void __set_cur_stree_readonly(void);
708 void __set_cur_stree_writable(void);
709 void __set_sm(struct sm_state *sm);
710 void __set_sm_cur_stree(struct sm_state *sm);
711 void __set_sm_fake_stree(struct sm_state *sm);
712 void __set_true_false_sm(struct sm_state *true_state,
713 struct sm_state *false_state);
714 void nullify_path(void);
715 void __match_nullify_path_hook(const char *fn, struct expression *expr,
716 void *unused);
717 void __unnullify_path(void);
718 int __path_is_null(void);
719 void save_all_states(void);
720 void restore_all_states(void);
721 void free_goto_stack(void);
722 void clear_all_states(void);
724 struct sm_state *get_sm_state(int owner, const char *name,
725 struct symbol *sym);
726 struct sm_state *get_sm_state_expr(int owner, struct expression *expr);
727 void __push_true_states(void);
728 void __use_false_states(void);
729 void __discard_false_states(void);
730 void __merge_false_states(void);
731 void __merge_true_states(void);
733 void __negate_cond_stacks(void);
734 void __use_pre_cond_states(void);
735 void __use_cond_true_states(void);
736 void __use_cond_false_states(void);
737 void __push_cond_stacks(void);
738 void __fold_in_set_states(void);
739 void __free_set_states(void);
740 struct stree *__copy_cond_true_states(void);
741 struct stree *__copy_cond_false_states(void);
742 struct stree *__pop_cond_true_stack(void);
743 struct stree *__pop_cond_false_stack(void);
744 void __and_cond_states(void);
745 void __or_cond_states(void);
746 void __save_pre_cond_states(void);
747 void __discard_pre_cond_states(void);
748 struct stree *__get_true_states(void);
749 struct stree *__get_false_states(void);
750 void __use_cond_states(void);
751 extern struct state_list *__last_base_slist;
753 void __push_continues(void);
754 void __discard_continues(void);
755 void __process_continues(void);
756 void __merge_continues(void);
758 void __push_breaks(void);
759 void __process_breaks(void);
760 int __has_breaks(void);
761 void __merge_breaks(void);
762 void __use_breaks(void);
764 void __save_switch_states(struct expression *switch_expr);
765 void __discard_switches(void);
766 int have_remaining_cases(void);
767 void __merge_switches(struct expression *switch_expr, struct range_list *case_rl);
768 void __push_default(void);
769 void __set_default(void);
770 int __pop_default(void);
772 void __push_conditions(void);
773 void __discard_conditions(void);
775 void __save_gotos(const char *name, struct symbol *sym);
776 void __merge_gotos(const char *name, struct symbol *sym);
778 void __print_cur_stree(void);
779 bool __print_states(const char *owner);
781 /* smatch_hooks.c */
782 void __pass_to_client(void *data, enum hook_type type);
783 void __pass_case_to_client(struct expression *switch_expr,
784 struct range_list *rl);
785 int __has_merge_function(int client_id);
786 struct smatch_state *__client_merge_function(int owner,
787 struct smatch_state *s1,
788 struct smatch_state *s2);
789 struct smatch_state *__client_unmatched_state_function(struct sm_state *sm);
790 void call_pre_merge_hook(struct sm_state *cur, struct sm_state *other);
791 void __push_scope_hooks(void);
792 void __call_scope_hooks(void);
794 /* smatch_function_hooks.c */
795 void create_function_hook_hash(void);
796 void __match_initializer_call(struct symbol *sym);
798 /* smatch_db.c */
799 enum info_type {
800 INTERNAL = 0,
802 * Changing these numbers is a pain. Don't do it. If you ever use a
803 * number it can't be re-used right away so there may be gaps.
804 * We select these in order by type so if the order matters, then give
805 * it a number below 100-999,9000-9999 ranges. */
807 PARAM_CLEARED = 101,
808 PARAM_LIMIT = 103,
809 PARAM_FILTER = 104,
811 PARAM_VALUE = 1001,
812 BUF_SIZE = 1002,
813 CAPPED_DATA = 1004,
814 RETURN_VALUE = 1005,
815 DEREFERENCE = 1006,
816 RANGE_CAP = 1007,
817 ABSOLUTE_LIMITS = 1010,
818 PARAM_ADD = 1012,
819 PARAM_FREED = 1013,
820 DATA_SOURCE = 1014,
821 FUZZY_MAX = 1015,
822 HARD_MAX = 2015,
823 STR_LEN = 1016,
824 ARRAY_LEN = 1017,
825 CAPABLE = 1018,
826 NS_CAPABLE = 1019,
827 CONTAINER = 1020,
828 CASTED_CALL = 1021,
829 TYPE_LINK = 1022,
830 UNTRACKED_PARAM = 1023,
831 LOST_PARAM = 2023,
832 CULL_PATH = 1024,
833 PARAM_SET = 1025,
834 PARAM_USED = 1026,
835 BYTE_UNITS = 1027,
836 COMPARE_LIMIT = 1028,
837 PARAM_COMPARE = 1029,
838 CONSTRAINT = 1031,
839 PASSES_TYPE = 1032,
840 CONSTRAINT_REQUIRED = 1033,
841 BIT_INFO = 1034,
842 NOSPEC = 1035,
843 NOSPEC_WB = 1036,
844 STMT_CNT = 1037,
845 TERMINATED = 1038,
846 FRESH_ALLOC = 1044,
848 /* put random temporary stuff in the 7000-7999 range for testing */
849 USER_DATA = 8017,
850 USER_DATA_SET = 9017,
851 NO_OVERFLOW = 8018,
852 NO_OVERFLOW_SIMPLE = 8019,
853 LOCKED = 8020,
854 UNLOCKED = 8021,
855 HALF_LOCKED = 9022,
856 LOCK_RESTORED = 9023,
857 KNOWN_LOCKED = 9024,
858 KNOWN_UNLOCKED = 9025,
859 SET_FS = 8022,
860 ATOMIC_INC = 8023,
861 ATOMIC_DEC = 8024,
862 NO_SIDE_EFFECT = 8025,
863 FN_ARG_LINK = 8028,
864 DATA_VALUE = 8029,
865 ARRAYSIZE_ARG = 8033,
866 SIZEOF_ARG = 8034,
867 MEMORY_TAG = 8036,
868 MTAG_ASSIGN = 8035,
869 STRING_VALUE = 8041,
871 BYTE_COUNT = 8050,
872 ELEM_COUNT = 8051,
873 ELEM_LAST = 8052,
874 USED_LAST = 8053,
875 USED_COUNT = 8054,
878 extern struct sqlite3 *smatch_db;
879 extern struct sqlite3 *mem_db;
880 extern struct sqlite3 *cache_db;
882 void db_ignore_states(int id);
883 void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type);
884 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm));
885 void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr));
886 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));
887 void select_call_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value));
888 void select_return_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value));
889 struct range_list *db_return_vals(struct expression *expr);
890 struct range_list *db_return_vals_from_str(const char *fn_name);
891 struct range_list *db_return_vals_no_args(struct expression *expr);
892 char *return_state_to_var_sym(struct expression *expr, int param, const char *key, struct symbol **sym);
893 char *get_chunk_from_key(struct expression *arg, char *key, struct symbol **sym, struct var_sym_list **vsl);
894 char *get_variable_from_key(struct expression *arg, const char *key, struct symbol **sym);
895 const char *state_name_to_param_name(const char *state_name, const char *param_name);
896 const char *get_param_name_var_sym(const char *name, struct symbol *sym);
897 const char *get_param_name(struct sm_state *sm);
898 const char *get_mtag_name_var_sym(const char *state_name, struct symbol *sym);
899 const char *get_mtag_name_expr(struct expression *expr);
900 char *get_data_info_name(struct expression *expr);
901 char *sm_to_arg_name(struct expression *expr, struct sm_state *sm);
902 int is_recursive_member(const char *param_name);
904 char *escape_newlines(const char *str);
905 void sql_exec(struct sqlite3 *db, int (*callback)(void*, int, char**, char**), void *data, const char *sql);
907 #define sql_helper(db, call_back, data, sql...) \
908 do { \
909 char sql_txt[1024]; \
911 sqlite3_snprintf(sizeof(sql_txt), sql_txt, sql); \
912 db_debug("debug: %s\n", sql_txt); \
913 sql_exec(db, call_back, data, sql_txt); \
914 } while (0)
917 #define run_sql(call_back, data, sql...) \
918 do { \
919 if (option_no_db) \
920 break; \
921 sql_helper(smatch_db, call_back, data, sql); \
922 } while (0)
924 #define mem_sql(call_back, data, sql...) \
925 sql_helper(mem_db, call_back, data, sql)
927 #define cache_sql(call_back, data, sql...) \
928 sql_helper(cache_db, call_back, data, sql)
930 #define sql_insert_helper(table, db, ignore, late, values...) \
931 do { \
932 struct sqlite3 *_db = db; \
934 if (__inline_fn && !_db) \
935 _db = mem_db; \
936 if (_db) { \
937 char buf[1024]; \
938 char *err, *p = buf; \
939 int rc; \
941 p += snprintf(p, buf + sizeof(buf) - p, \
942 "insert %sinto %s values (", \
943 ignore ? "or ignore " : "", #table); \
944 p += snprintf(p, buf + sizeof(buf) - p, values); \
945 p += snprintf(p, buf + sizeof(buf) - p, ");"); \
946 db_debug("mem-db: %s\n", buf); \
947 rc = sqlite3_exec(_db, buf, NULL, NULL, &err); \
948 if (rc != SQLITE_OK) { \
949 sm_ierror("SQL error #2: %s", err); \
950 sm_ierror("SQL: '%s'", buf); \
951 parse_error = 1; \
953 break; \
955 if (option_info) { \
956 FILE *tmp_fd = sm_outfd; \
957 sm_outfd = sql_outfd; \
958 sm_prefix(); \
959 sm_printf("SQL%s: insert %sinto " #table " values(", \
960 late ? "_late" : "", ignore ? "or ignore " : ""); \
961 sm_printf(values); \
962 sm_printf(");\n"); \
963 sm_outfd = tmp_fd; \
965 } while (0)
967 #define sql_insert(table, values...) sql_insert_helper(table, 0, 0, 0, values);
968 #define sql_insert_or_ignore(table, values...) sql_insert_helper(table, 0, 1, 0, values);
969 #define sql_insert_late(table, values...) sql_insert_helper(table, 0, 0, 1, values);
970 #define sql_insert_cache(table, values...) sql_insert_helper(table, cache_db, 1, 0, values);
972 char *get_static_filter(struct symbol *sym);
974 void sql_insert_return_states(int return_id, const char *return_ranges,
975 int type, int param, const char *key, const char *value);
976 void sql_insert_caller_info(struct expression *call, int type, int param,
977 const char *key, const char *value);
978 void sql_insert_function_ptr(const char *fn, const char *struct_name);
979 void sql_insert_return_values(const char *return_values);
980 void sql_insert_return_implies(int type, int param, const char *key, const char *value);
981 void sql_insert_function_type_size(const char *member, const char *ranges);
982 void sql_insert_function_type_info(int type, const char *struct_type, const char *member, const char *value);
983 void sql_insert_type_info(int type, const char *member, const char *value);
984 void sql_insert_local_values(const char *name, const char *value);
985 void sql_insert_function_type_value(const char *type, const char *value);
986 void sql_insert_function_type(int param, const char *value);
987 void sql_insert_parameter_name(int param, const char *value);
988 void sql_insert_data_info(struct expression *data, int type, const char *value);
989 void sql_insert_data_info_var_sym(const char *var, struct symbol *sym, int type, const char *value);
990 void sql_save_constraint(const char *con);
991 void sql_save_constraint_required(const char *data, int op, const char *limit);
992 void sql_copy_constraint_required(const char *new_limit, const char *old_limit);
993 void sql_insert_fn_ptr_data_link(const char *ptr, const char *data);
994 void sql_insert_fn_data_link(struct expression *fn, int type, int param, const char *key, const char *value);
995 void sql_insert_mtag_about(mtag_t tag, const char *left_name, const char *right_name);
996 void sql_insert_mtag_map(mtag_t tag, int offset, mtag_t container);
997 void sql_insert_mtag_alias(mtag_t orig, mtag_t alias);
998 int mtag_map_select_container(mtag_t tag, int offset, mtag_t *container);
999 int mtag_map_select_tag(mtag_t container, int offset, mtag_t *tag);
1000 struct smatch_state *get_mtag_return(struct expression *expr, struct smatch_state *state);
1001 struct range_list *swap_mtag_seed(struct expression *expr, struct range_list *rl);
1003 void sql_select_return_states(const char *cols, struct expression *call,
1004 int (*callback)(void*, int, char**, char**), void *info);
1005 void sql_select_call_implies(const char *cols, struct expression *call,
1006 int (*callback)(void*, int, char**, char**));
1008 void open_smatch_db(char *db_file);
1010 /* smatch_files.c */
1011 int open_data_file(const char *filename);
1012 int open_schema_file(const char *schema);
1013 struct token *get_tokens_file(const char *filename);
1015 /* smatch.c */
1016 extern char *option_debug_check;
1017 extern char *option_project_str;
1018 extern char *bin_dir;
1019 extern char *data_dir;
1020 extern int option_no_data;
1021 extern int option_full_path;
1022 extern int option_call_tree;
1023 extern int num_checks;
1025 enum project_type {
1026 PROJ_NONE,
1027 PROJ_KERNEL,
1028 PROJ_WINE,
1029 PROJ_ILLUMOS_KERNEL,
1030 PROJ_ILLUMOS_USER,
1031 PROJ_UNKNOWN,
1033 extern enum project_type option_project;
1034 const char *check_name(unsigned short id);
1035 int id_from_name(const char *name);
1038 /* smatch_buf_size.c */
1039 int get_array_size(struct expression *expr);
1040 int get_array_size_bytes(struct expression *expr);
1041 int get_array_size_bytes_min(struct expression *expr);
1042 int get_array_size_bytes_max(struct expression *expr);
1043 struct range_list *get_array_size_bytes_rl(struct expression *expr);
1044 int get_real_array_size(struct expression *expr);
1045 int last_member_is_resizable(struct symbol *type);
1046 /* smatch_strlen.c */
1047 int get_implied_strlen(struct expression *expr, struct range_list **rl);
1048 int get_size_from_strlen(struct expression *expr);
1050 /* smatch_capped.c */
1051 int is_capped(struct expression *expr);
1052 int is_capped_var_sym(const char *name, struct symbol *sym);
1054 /* check_user_data.c */
1055 int is_user_macro(struct expression *expr);
1056 int is_capped_user_data(struct expression *expr);
1057 int implied_user_data(struct expression *expr, struct range_list **rl);
1058 struct stree *get_user_stree(void);
1059 int get_user_rl(struct expression *expr, struct range_list **rl);
1060 int is_user_rl(struct expression *expr);
1061 int get_user_rl_var_sym(const char *name, struct symbol *sym, struct range_list **rl);
1062 bool user_rl_capped(struct expression *expr);
1063 struct range_list *var_user_rl(struct expression *expr);
1065 /* check_locking.c */
1066 void print_held_locks();
1068 /* check_assigned_expr.c */
1069 struct expression *get_assigned_expr(struct expression *expr);
1070 struct expression *get_assigned_expr_name_sym(const char *name, struct symbol *sym);
1071 /* smatch_return_to_param.c */
1072 void __add_return_to_param_mapping(struct expression *assign, const char *return_string);
1073 char *map_call_to_param_name_sym(struct expression *expr, struct symbol **sym);
1075 /* smatch_comparison.c */
1076 extern int comparison_id;
1077 #define UNKNOWN_COMPARISON 0
1078 #define IMPOSSIBLE_COMPARISON -1
1079 struct compare_data {
1080 /* The ->left and ->right expression pointers might be NULL (I'm lazy) */
1081 struct expression *left;
1082 const char *left_var;
1083 struct var_sym_list *left_vsl;
1084 int comparison;
1085 struct expression *right;
1086 const char *right_var;
1087 struct var_sym_list *right_vsl;
1089 DECLARE_ALLOCATOR(compare_data);
1090 struct smatch_state *alloc_compare_state(
1091 struct expression *left,
1092 const char *left_var, struct var_sym_list *left_vsl,
1093 int comparison,
1094 struct expression *right,
1095 const char *right_var, struct var_sym_list *right_vsl);
1096 int comparison_intersection(int orig, int op);
1097 int merge_comparisons(int one, int two);
1098 int combine_comparisons(int left_compare, int right_compare);
1099 int state_to_comparison(struct smatch_state *state);
1100 struct smatch_state *merge_compare_states(struct smatch_state *s1, struct smatch_state *s2);
1101 int get_comparison(struct expression *left, struct expression *right);
1102 int get_comparison_no_extra(struct expression *a, struct expression *b);
1103 int get_comparison_strings(const char *one, const char *two);
1104 int possible_comparison(struct expression *a, int comparison, struct expression *b);
1105 struct state_list *get_all_comparisons(struct expression *expr);
1106 struct state_list *get_all_possible_equal_comparisons(struct expression *expr);
1107 void __add_return_comparison(struct expression *call, const char *range);
1108 void __add_comparison_info(struct expression *expr, struct expression *call, const char *range);
1109 char *get_printed_param_name(struct expression *call, const char *param_name, struct symbol *param_sym);
1110 char *name_sym_to_param_comparison(const char *name, struct symbol *sym);
1111 char *expr_equal_to_param(struct expression *expr, int ignore);
1112 char *expr_lte_to_param(struct expression *expr, int ignore);
1113 char *expr_param_comparison(struct expression *expr, int ignore);
1114 int flip_comparison(int op);
1115 int negate_comparison(int op);
1116 int remove_unsigned_from_comparison(int op);
1117 int param_compare_limit_is_impossible(struct expression *expr, int left_param, char *left_key, char *value);
1118 void filter_by_comparison(struct range_list **rl, int comparison, struct range_list *right);
1119 struct sm_state *comparison_implication_hook(struct expression *expr,
1120 struct state_list **true_stack,
1121 struct state_list **false_stack);
1122 void __compare_param_limit_hook(struct expression *left_expr, struct expression *right_expr,
1123 const char *state_name,
1124 struct smatch_state *true_state, struct smatch_state *false_state);
1125 int impossibly_high_comparison(struct expression *expr);
1127 /* smatch_sval.c */
1128 sval_t *sval_alloc(sval_t sval);
1129 sval_t *sval_alloc_permanent(sval_t sval);
1130 sval_t sval_blank(struct expression *expr);
1131 sval_t sval_type_val(struct symbol *type, long long val);
1132 sval_t sval_type_fval(struct symbol *type, long double fval);
1133 sval_t sval_from_val(struct expression *expr, long long val);
1134 sval_t sval_from_fval(struct expression *expr, long double fval);
1135 int sval_is_ptr(sval_t sval);
1136 bool sval_is_fp(sval_t sval);
1137 int sval_unsigned(sval_t sval);
1138 int sval_signed(sval_t sval);
1139 int sval_bits(sval_t sval);
1140 int sval_bits_used(sval_t sval);
1141 int sval_is_negative(sval_t sval);
1142 int sval_is_positive(sval_t sval);
1143 int sval_is_min(sval_t sval);
1144 int sval_is_max(sval_t sval);
1145 int sval_is_a_min(sval_t sval);
1146 int sval_is_a_max(sval_t sval);
1147 int sval_is_negative_min(sval_t sval);
1148 int sval_cmp_t(struct symbol *type, sval_t one, sval_t two);
1149 int sval_cmp_val(sval_t one, long long val);
1150 sval_t sval_min(sval_t one, sval_t two);
1151 sval_t sval_max(sval_t one, sval_t two);
1152 int sval_too_low(struct symbol *type, sval_t sval);
1153 int sval_too_high(struct symbol *type, sval_t sval);
1154 int sval_fits(struct symbol *type, sval_t sval);
1155 sval_t sval_cast(struct symbol *type, sval_t sval);
1156 sval_t sval_preop(sval_t sval, int op);
1157 sval_t sval_binop(sval_t left, int op, sval_t right);
1158 int sval_binop_overflows(sval_t left, int op, sval_t right);
1159 int sval_binop_overflows_no_sign(sval_t left, int op, sval_t right);
1160 int find_first_zero_bit(unsigned long long uvalue);
1161 int sm_fls64(unsigned long long uvalue);
1162 unsigned long long fls_mask(unsigned long long uvalue);
1163 unsigned long long sval_fls_mask(sval_t sval);
1164 const char *sval_to_str(sval_t sval);
1165 const char *sval_to_str_or_err_ptr(sval_t sval);
1166 const char *sval_to_numstr(sval_t sval);
1167 sval_t ll_to_sval(long long val);
1169 /* smatch_string_list.c */
1170 int list_has_string(struct string_list *str_list, const char *str);
1171 int insert_string(struct string_list **str_list, const char *str);
1172 struct string_list *clone_str_list(struct string_list *orig);
1173 struct string_list *combine_string_lists(struct string_list *one, struct string_list *two);
1175 /* smatch_start_states.c */
1176 struct stree *get_start_states(void);
1178 /* smatch_recurse.c */
1179 int has_symbol(struct expression *expr, struct symbol *sym);
1180 int has_variable(struct expression *expr, struct expression *var);
1181 int has_inc_dec(struct expression *expr);
1183 /* smatch_stored_conditions.c */
1184 struct smatch_state *get_stored_condition(struct expression *expr);
1185 struct expression_list *get_conditions(struct expression *expr);
1186 struct sm_state *stored_condition_implication_hook(struct expression *expr,
1187 struct state_list **true_stack,
1188 struct state_list **false_stack);
1189 /* smatch_parsed_conditions.c */
1190 struct sm_state *parsed_condition_implication_hook(struct expression *expr,
1191 struct state_list **true_stack,
1192 struct state_list **false_stack);
1194 /* check_string_len.c */
1195 int get_formatted_string_size(struct expression *call, int arg);
1196 int get_formatted_string_min_size(struct expression *call, int arg);
1198 /* smatch_param_set.c */
1199 int param_was_set(struct expression *expr);
1200 int param_was_set_var_sym(const char *name, struct symbol *sym);
1201 void print_limited_param_set(int return_id, char *return_ranges, struct expression *expr);
1202 /* smatch_param_filter.c */
1203 int param_has_filter_data(struct sm_state *sm);
1205 /* smatch_links.c */
1206 void set_up_link_functions(int id, int linkid);
1207 struct smatch_state *merge_link_states(struct smatch_state *s1, struct smatch_state *s2);
1208 void store_link(int link_id, const char *name, struct symbol *sym, const char *link_name, struct symbol *link_sym);
1210 /* check_buf_comparison */
1211 const char *limit_type_str(unsigned int limit_type);
1212 struct expression *get_size_variable(struct expression *buf, int *limit_type);
1213 struct expression *get_array_variable(struct expression *size);
1214 int buf_comparison_index_ok(struct expression *expr);
1216 /* smatch_untracked_param.c */
1217 void mark_untracked(struct expression *expr, int param, const char *key, const char *value);
1218 void add_untracked_param_hook(void (func)(struct expression *call, int param));
1219 void add_lost_param_hook(void (func)(struct expression *call, int param));
1220 void mark_all_params_untracked(int return_id, char *return_ranges, struct expression *expr);
1222 /* smatch_strings.c */
1223 struct state_list *get_strings(struct expression *expr);
1224 struct expression *fake_string_from_mtag(mtag_t tag);
1226 /* smatch_estate.c */
1227 int estate_get_single_value(struct smatch_state *state, sval_t *sval);
1229 /* smatch_address.c */
1230 int get_address_rl(struct expression *expr, struct range_list **rl);
1231 int get_member_offset(struct symbol *type, const char *member_name);
1232 int get_member_offset_from_deref(struct expression *expr);
1234 /* for now this is in smatch_used_parameter.c */
1235 void __get_state_hook(int owner, const char *name, struct symbol *sym);
1237 /* smatch_buf_comparison.c */
1238 int db_var_is_array_limit(struct expression *array, const char *name, struct var_sym_list *vsl);
1240 struct stree *get_all_return_states(void);
1241 struct stree_stack *get_all_return_strees(void);
1242 int on_atomic_dec_path(void);
1243 int was_inced(const char *name, struct symbol *sym);
1245 /* smatch_constraints.c */
1246 char *get_constraint_str(struct expression *expr);
1247 struct constraint_list *get_constraints(struct expression *expr);
1248 char *unmet_constraint(struct expression *data, struct expression *offset);
1249 char *get_required_constraint(const char *data_str);
1251 /* smatch_container_of.c */
1252 int get_param_from_container_of(struct expression *expr);
1253 int get_offset_from_container_of(struct expression *expr);
1254 char *get_container_name(struct expression *container, struct expression *expr);
1256 /* smatch_mtag.c */
1257 mtag_t str_to_mtag(const char *str);
1258 int get_string_mtag(struct expression *expr, mtag_t *tag);
1259 int get_toplevel_mtag(struct symbol *sym, mtag_t *tag);
1260 int create_mtag_alias(mtag_t tag, struct expression *expr, mtag_t *new);
1261 int expr_to_mtag_offset(struct expression *expr, mtag_t *tag, int *offset);
1262 void update_mtag_data(struct expression *expr, struct smatch_state *state);
1263 int get_mtag_sval(struct expression *expr, sval_t *sval);
1265 /* Trinity fuzzer stuff */
1266 const char *get_syscall_arg_type(struct symbol *sym);
1268 /* smatch_bit_info.c */
1269 struct bit_info *rl_to_binfo(struct range_list *rl);
1270 struct bit_info *get_bit_info(struct expression *expr);
1271 struct bit_info *get_bit_info_var_sym(const char *name, struct symbol *sym);
1272 /* smatch_mem_tracker.c */
1273 extern int option_mem;
1274 unsigned long get_mem_kb(void);
1275 unsigned long get_max_memory(void);
1277 /* check_is_nospec.c */
1278 bool is_nospec(struct expression *expr);
1279 long get_stmt_cnt(void);
1281 /* smatch_nul_terminator.c */
1282 bool is_nul_terminated_var_sym(const char *name, struct symbol *sym);
1283 bool is_nul_terminated(struct expression *expr);
1284 /* check_kernel.c */
1285 bool is_ignored_kernel_data(const char *name);
1287 bool is_fresh_alloc_var_sym(const char *var, struct symbol *sym);
1288 bool is_fresh_alloc(struct expression *expr);
1289 static inline bool type_is_ptr(struct symbol *type)
1291 return type &&
1292 (type->type == SYM_PTR ||
1293 type->type == SYM_ARRAY ||
1294 type->type == SYM_FN);
1297 static inline bool type_is_fp(struct symbol *type)
1299 return type &&
1300 (type == &float_ctype ||
1301 type == &double_ctype ||
1302 type == &ldouble_ctype);
1305 static inline int type_bits(struct symbol *type)
1307 if (!type)
1308 return 0;
1309 if (type_is_ptr(type))
1310 return bits_in_pointer;
1311 if (!type->examined)
1312 examine_symbol_type(type);
1313 return type->bit_size;
1316 static inline int type_unsigned(struct symbol *base_type)
1318 if (!base_type)
1319 return 0;
1320 if (is_ptr_type(base_type))
1321 return 1;
1322 if (base_type->ctype.modifiers & MOD_UNSIGNED)
1323 return 1;
1324 return 0;
1327 static inline int type_positive_bits(struct symbol *type)
1329 if (!type)
1330 return 0;
1331 if (is_ptr_type(type))
1332 return bits_in_pointer;
1333 if (type_unsigned(type))
1334 return type_bits(type);
1335 return type_bits(type) - 1;
1338 static inline int sval_positive_bits(sval_t sval)
1340 return type_positive_bits(sval.type);
1344 * Returns -1 if one is smaller, 0 if they are the same and 1 if two is larger.
1347 static inline int fp_cmp(sval_t one, sval_t two)
1349 struct symbol *type;
1351 if (sval_is_fp(one) && sval_is_fp(two))
1352 type = type_bits(one.type) > type_bits(two.type) ? one.type : two.type;
1353 else if (sval_is_fp(one))
1354 type = one.type;
1355 else
1356 type = two.type;
1358 one = sval_cast(type, one);
1359 two = sval_cast(type, two);
1361 if (one.type == &float_ctype) {
1362 if (one.fvalue < two.fvalue)
1363 return -1;
1364 if (one.fvalue == two.fvalue)
1365 return 0;
1366 return 1;
1368 if (one.type == &double_ctype) {
1369 if (one.dvalue < two.dvalue)
1370 return -1;
1371 if (one.dvalue == two.dvalue)
1372 return 0;
1373 return 1;
1375 if (one.type == &ldouble_ctype) {
1376 if (one.ldvalue < two.ldvalue)
1377 return -1;
1378 if (one.ldvalue == two.ldvalue)
1379 return 0;
1380 return 1;
1382 sm_perror("bad type in fp_cmp(): %s", type_to_str(type));
1383 return 1;
1386 static inline int sval_cmp(sval_t one, sval_t two)
1388 struct symbol *type;
1390 if (sval_is_fp(one) || sval_is_fp(two))
1391 return fp_cmp(one, two);
1393 type = one.type;
1394 if (sval_positive_bits(two) > sval_positive_bits(one))
1395 type = two.type;
1396 if (type_bits(type) < 31)
1397 type = &int_ctype;
1399 one = sval_cast(type, one);
1400 two = sval_cast(type, two);
1402 if (type_unsigned(type)) {
1403 if (one.uvalue < two.uvalue)
1404 return -1;
1405 if (one.uvalue == two.uvalue)
1406 return 0;
1407 return 1;
1409 /* fix me handle type promotion and unsigned values */
1410 if (one.value < two.value)
1411 return -1;
1412 if (one.value == two.value)
1413 return 0;
1414 return 1;
1417 #endif /* !SMATCH_H_ */