function_ptrs: don't store nonsense fake pointers
[smatch.git] / smatch.h
blob0dd35d39f08632f75330e349ab148071749ff59b
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 struct range_list *get_range_implications(const char *fn);
193 void select_return_states_hook(int type, return_implies_hook *callback);
194 void select_return_states_before(void (*fn)(void));
195 void select_return_states_after(void (*fn)(void));
196 int get_implied_return(struct expression *expr, struct range_list **rl);
197 void allocate_hook_memory(void);
198 void allocate_tracker_array(int num_checks);
200 struct modification_data {
201 struct smatch_state *prev;
202 struct expression *cur;
205 typedef void (modification_hook)(struct sm_state *sm, struct expression *mod_expr);
206 void add_modification_hook(int owner, modification_hook *call_back);
207 void add_modification_hook_late(int owner, modification_hook *call_back);
208 struct smatch_state *get_modification_state(struct expression *expr);
210 int outside_of_function(void);
211 const char *get_filename(void);
212 const char *get_base_file(void);
213 char *get_function(void);
214 int get_lineno(void);
215 extern int final_pass;
216 extern struct symbol *cur_func_sym;
217 extern int option_debug;
218 extern int local_debug;
219 extern int debug_db;
220 bool debug_implied(void);
221 extern int option_info;
222 extern int option_spammy;
223 extern char *trace_variable;
224 extern struct stree *global_states;
225 int is_skipped_function(void);
226 int is_silenced_function(void);
227 extern bool implications_off;
229 /* smatch_impossible.c */
230 int is_impossible_path(void);
231 void set_path_impossible(void);
233 extern FILE *sm_outfd;
234 extern FILE *sql_outfd;
235 extern FILE *caller_info_fd;
236 extern int sm_nr_checks;
237 extern int sm_nr_errors;
240 * How to use these routines:
242 * sm_fatal(): an internal error of some kind that should immediately exit
243 * sm_ierror(): an internal error
244 * sm_perror(): an internal error from parsing input source
245 * sm_error(): an error from input source
246 * sm_warning(): a warning from input source
247 * sm_info(): info message (from option_info)
248 * sm_debug(): debug message
249 * sm_msg(): other message (please avoid using this)
252 #define sm_printf(msg...) do { \
253 if (final_pass || option_debug || local_debug || debug_db) \
254 fprintf(sm_outfd, msg); \
255 } while (0)
257 static inline void sm_prefix(void)
259 sm_printf("%s:%d %s() ", get_filename(), get_lineno(), get_function());
262 static inline void print_implied_debug_msg();
264 extern bool __silence_warnings_for_stmt;
266 #define sm_print_msg(type, msg...) \
267 do { \
268 print_implied_debug_msg(); \
269 if (!final_pass && !option_debug && !local_debug && !debug_db) \
270 break; \
271 if (__silence_warnings_for_stmt && !option_debug && !local_debug) \
272 break; \
273 if (!option_info && is_silenced_function()) \
274 break; \
275 sm_prefix(); \
276 if (type == 1) { \
277 sm_printf("warn: "); \
278 sm_nr_checks++; \
279 } else if (type == 2) { \
280 sm_printf("error: "); \
281 sm_nr_checks++; \
282 } else if (type == 3) { \
283 sm_printf("parse error: "); \
284 sm_nr_errors++; \
286 sm_printf(msg); \
287 sm_printf("\n"); \
288 } while (0)
290 #define sm_msg(msg...) do { sm_print_msg(0, msg); } while (0)
292 extern char *implied_debug_msg;
293 static inline void print_implied_debug_msg(void)
295 static struct symbol *last_printed = NULL;
297 if (!implied_debug_msg)
298 return;
299 if (last_printed == cur_func_sym)
300 return;
301 last_printed = cur_func_sym;
302 sm_msg("%s", implied_debug_msg);
305 #define sm_debug(msg...) do { if (option_debug) sm_printf(msg); } while (0)
306 #define db_debug(msg...) do { if (option_debug || debug_db) sm_printf(msg); } while (0)
308 #define sm_info(msg...) do { \
309 if (option_debug || (option_info && final_pass)) { \
310 sm_prefix(); \
311 sm_printf("info: "); \
312 sm_printf(msg); \
313 sm_printf("\n"); \
315 } while(0)
317 #define sm_warning(msg...) do { sm_print_msg(1, msg); } while (0)
318 #define sm_error(msg...) do { sm_print_msg(2, msg); } while (0)
319 #define sm_perror(msg...) do { sm_print_msg(3, msg); } while (0)
321 static inline void sm_fatal(const char *fmt, ...)
323 va_list args;
325 va_start(args, fmt);
326 vfprintf(sm_outfd, fmt, args);
327 va_end(args);
329 fprintf(sm_outfd, "\n");
331 exit(1);
334 static inline void sm_ierror(const char *fmt, ...)
336 va_list args;
338 sm_nr_errors++;
340 fprintf(sm_outfd, "internal error: ");
342 va_start(args, fmt);
343 vfprintf(sm_outfd, fmt, args);
344 va_end(args);
346 fprintf(sm_outfd, "\n");
348 #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
350 struct smatch_state *__get_state(int owner, const char *name, struct symbol *sym);
351 struct smatch_state *get_state(int owner, const char *name, struct symbol *sym);
352 struct smatch_state *get_state_expr(int owner, struct expression *expr);
353 struct state_list *get_possible_states(int owner, const char *name,
354 struct symbol *sym);
355 struct state_list *get_possible_states_expr(int owner, struct expression *expr);
356 struct sm_state *set_state(int owner, const char *name, struct symbol *sym,
357 struct smatch_state *state);
358 struct sm_state *set_state_expr(int owner, struct expression *expr,
359 struct smatch_state *state);
360 void delete_state(int owner, const char *name, struct symbol *sym);
361 void delete_state_expr(int owner, struct expression *expr);
362 void __delete_all_states_sym(struct symbol *sym);
363 void set_true_false_states(int owner, const char *name, struct symbol *sym,
364 struct smatch_state *true_state,
365 struct smatch_state *false_state);
366 void set_true_false_states_expr(int owner, struct expression *expr,
367 struct smatch_state *true_state,
368 struct smatch_state *false_state);
370 struct stree *get_all_states_from_stree(int owner, struct stree *source);
371 struct stree *get_all_states_stree(int id);
372 struct stree *__get_cur_stree(void);
373 int is_reachable(void);
374 void add_get_state_hook(void (*fn)(int owner, const char *name, struct symbol *sym));
376 /* smatch_helper.c */
377 DECLARE_PTR_LIST(int_stack, int);
378 char *alloc_string(const char *str);
379 char *alloc_string_newline(const char *str);
380 void free_string(char *str);
381 void append(char *dest, const char *data, int buff_len);
382 void remove_parens(char *str);
383 struct smatch_state *alloc_state_num(int num);
384 struct smatch_state *alloc_state_str(const char *name);
385 struct smatch_state *merge_str_state(struct smatch_state *s1, struct smatch_state *s2);
386 struct smatch_state *alloc_state_expr(struct expression *expr);
387 struct expression *get_argument_from_call_expr(struct expression_list *args,
388 int num);
389 struct expression *get_array_expr(struct expression *expr);
391 char *expr_to_var(struct expression *expr);
392 struct symbol *expr_to_sym(struct expression *expr);
393 char *expr_to_str(struct expression *expr);
394 char *expr_to_str_sym(struct expression *expr,
395 struct symbol **sym_ptr);
396 char *expr_to_var_sym(struct expression *expr,
397 struct symbol **sym_ptr);
398 char *expr_to_known_chunk_sym(struct expression *expr, struct symbol **sym);
399 char *expr_to_chunk_sym_vsl(struct expression *expr, struct symbol **sym, struct var_sym_list **vsl);
400 int get_complication_score(struct expression *expr);
402 int sym_name_is(const char *name, struct expression *expr);
403 int get_const_value(struct expression *expr, sval_t *sval);
404 int get_value(struct expression *expr, sval_t *val);
405 int get_implied_value(struct expression *expr, sval_t *val);
406 int get_implied_value_fast(struct expression *expr, sval_t *sval);
407 int get_implied_min(struct expression *expr, sval_t *sval);
408 int get_implied_max(struct expression *expr, sval_t *val);
409 int get_hard_max(struct expression *expr, sval_t *sval);
410 int get_fuzzy_min(struct expression *expr, sval_t *min);
411 int get_fuzzy_max(struct expression *expr, sval_t *max);
412 int get_absolute_min(struct expression *expr, sval_t *sval);
413 int get_absolute_max(struct expression *expr, sval_t *sval);
414 int parse_call_math(struct expression *expr, char *math, sval_t *val);
415 int parse_call_math_rl(struct expression *call, const char *math, struct range_list **rl);
416 const char *get_allocation_math(struct expression *expr);
417 char *get_value_in_terms_of_parameter_math(struct expression *expr);
418 char *get_value_in_terms_of_parameter_math_var_sym(const char *var, struct symbol *sym);
419 int expr_is_zero(struct expression *expr);
420 int known_condition_true(struct expression *expr);
421 int known_condition_false(struct expression *expr);
422 int implied_condition_true(struct expression *expr);
423 int implied_condition_false(struct expression *expr);
424 int can_integer_overflow(struct symbol *type, struct expression *expr);
425 void clear_math_cache(void);
426 void set_fast_math_only(void);
427 void clear_fast_math_only(void);
429 int is_array(struct expression *expr);
430 struct expression *get_array_base(struct expression *expr);
431 struct expression *get_array_offset(struct expression *expr);
432 const char *show_state(struct smatch_state *state);
433 struct statement *get_expression_statement(struct expression *expr);
434 struct expression *strip_parens(struct expression *expr);
435 struct expression *strip_expr(struct expression *expr);
436 struct expression *strip_expr_set_parent(struct expression *expr);
437 void scoped_state(int my_id, const char *name, struct symbol *sym);
438 int is_error_return(struct expression *expr);
439 int getting_address(struct expression *expr);
440 int get_struct_and_member(struct expression *expr, const char **type, const char **member);
441 char *get_member_name(struct expression *expr);
442 char *get_fnptr_name(struct expression *expr);
443 int cmp_pos(struct position pos1, struct position pos2);
444 int positions_eq(struct position pos1, struct position pos2);
445 struct statement *get_current_statement(void);
446 struct statement *get_prev_statement(void);
447 struct expression *get_last_expr_from_expression_stmt(struct expression *expr);
449 #define RETURN_VAR -1
450 #define LOCAL_SCOPE -2
451 #define FILE_SCOPE -3
452 #define GLOBAL_SCOPE -4
453 #define UNKNOWN_SCOPE -5
454 int get_param_key_from_var_sym(const char *name, struct symbol *sym,
455 struct expression *ret_expr,
456 const char **key);
457 int get_param_key_from_sm(struct sm_state *sm, struct expression *ret_expr,
458 const char **key);
459 int get_param_num_from_sym(struct symbol *sym);
460 int get_param_num(struct expression *expr);
461 struct symbol *get_param_sym_from_num(int num);
463 int ms_since(struct timeval *start);
464 int parent_is_gone_var_sym(const char *name, struct symbol *sym);
465 int parent_is_gone(struct expression *expr);
466 int invert_op(int op);
467 int op_remove_assign(int op);
468 int expr_equiv(struct expression *one, struct expression *two);
469 void push_int(struct int_stack **stack, int num);
470 int pop_int(struct int_stack **stack);
472 /* smatch_type.c */
473 struct symbol *get_real_base_type(struct symbol *sym);
474 int type_bytes(struct symbol *type);
475 int array_bytes(struct symbol *type);
476 struct symbol *get_pointer_type(struct expression *expr);
477 struct symbol *get_type(struct expression *expr);
478 struct symbol *get_final_type(struct expression *expr);
479 struct symbol *get_promoted_type(struct symbol *left, struct symbol *right);
480 int type_signed(struct symbol *base_type);
481 int expr_unsigned(struct expression *expr);
482 int expr_signed(struct expression *expr);
483 int returns_unsigned(struct symbol *base_type);
484 int is_pointer(struct expression *expr);
485 int returns_pointer(struct symbol *base_type);
486 sval_t sval_type_max(struct symbol *base_type);
487 sval_t sval_type_min(struct symbol *base_type);
488 int nr_bits(struct expression *expr);
489 int is_void_pointer(struct expression *expr);
490 int is_char_pointer(struct expression *expr);
491 int is_string(struct expression *expr);
492 bool is_struct_ptr(struct symbol *type);
493 int is_static(struct expression *expr);
494 bool is_local_variable(struct expression *expr);
495 int types_equiv(struct symbol *one, struct symbol *two);
496 bool type_fits(struct symbol *type, struct symbol *test);
497 int fn_static(void);
498 const char *global_static();
499 struct symbol *cur_func_return_type(void);
500 struct symbol *get_arg_type(struct expression *fn, int arg);
501 struct symbol *get_member_type_from_key(struct expression *expr, const char *key);
502 struct symbol *get_arg_type_from_key(struct expression *fn, int param, struct expression *arg, const char *key);
503 int is_struct(struct expression *expr);
504 char *type_to_str(struct symbol *type);
506 /* smatch_ignore.c */
507 void add_ignore(int owner, const char *name, struct symbol *sym);
508 int is_ignored(int owner, const char *name, struct symbol *sym);
509 void add_ignore_expr(int owner, struct expression *expr);
510 int is_ignored_expr(int owner, struct expression *expr);
512 /* smatch_var_sym */
513 struct smatch_state *alloc_var_sym_state(const char *var, struct symbol *sym);
514 struct var_sym *alloc_var_sym(const char *var, struct symbol *sym);
515 struct var_sym_list *expr_to_vsl(struct expression *expr);
516 void add_var_sym(struct var_sym_list **list, const char *var, struct symbol *sym);
517 void add_var_sym_expr(struct var_sym_list **list, struct expression *expr);
518 void del_var_sym(struct var_sym_list **list, const char *var, struct symbol *sym);
519 int in_var_sym_list(struct var_sym_list *list, const char *var, struct symbol *sym);
520 struct var_sym_list *clone_var_sym_list(struct var_sym_list *from_vsl);
521 void merge_var_sym_list(struct var_sym_list **dest, struct var_sym_list *src);
522 struct var_sym_list *combine_var_sym_lists(struct var_sym_list *one, struct var_sym_list *two);
523 int var_sym_lists_equiv(struct var_sym_list *one, struct var_sym_list *two);
524 void free_var_sym_list(struct var_sym_list **list);
525 void free_var_syms_and_list(struct var_sym_list **list);
527 /* smatch_tracker */
528 struct tracker *alloc_tracker(int owner, const char *name, struct symbol *sym);
529 void add_tracker(struct tracker_list **list, int owner, const char *name,
530 struct symbol *sym);
531 void add_tracker_expr(struct tracker_list **list, int owner, struct expression *expr);
532 void del_tracker(struct tracker_list **list, int owner, const char *name,
533 struct symbol *sym);
534 int in_tracker_list(struct tracker_list *list, int owner, const char *name,
535 struct symbol *sym);
536 void free_tracker_list(struct tracker_list **list);
537 void free_trackers_and_list(struct tracker_list **list);
539 /* smatch_conditions */
540 int in_condition(void);
542 /* smatch_flow.c */
544 extern int __in_fake_assign;
545 extern int __in_fake_parameter_assign;
546 extern int __in_fake_struct_assign;
547 extern int __in_fake_var_assign;
548 extern int __fake_state_cnt;
549 extern int in_fake_env;
550 void smatch (struct string_list *filelist);
551 int inside_loop(void);
552 int definitely_inside_loop(void);
553 struct expression *get_switch_expr(void);
554 int in_expression_statement(void);
555 void __process_post_op_stack(void);
556 void __split_expr(struct expression *expr);
557 void __split_label_stmt(struct statement *stmt);
558 void __split_stmt(struct statement *stmt);
559 extern int __in_function_def;
560 extern int __in_unmatched_hook;
561 extern int option_assume_loops;
562 extern int option_two_passes;
563 extern int option_no_db;
564 extern int option_file_output;
565 extern int option_time;
566 extern struct expression_list *big_expression_stack;
567 extern struct expression_list *big_condition_stack;
568 extern struct statement_list *big_statement_stack;
569 int is_assigned_call(struct expression *expr);
570 int is_fake_assigned_call(struct expression *expr);
571 void add_function_data(unsigned long *fn_data);
572 int inlinable(struct expression *expr);
573 extern int __inline_call;
574 extern struct expression *__inline_fn;
575 extern int __in_pre_condition;
576 extern int __bail_on_rest_of_function;
577 extern struct statement *__prev_stmt;
578 extern struct statement *__cur_stmt;
579 extern struct statement *__next_stmt;
580 void init_fake_env(void);
581 void end_fake_env(void);
582 int time_parsing_function(void);
583 bool taking_too_long(void);
585 /* smatch_struct_assignment.c */
586 struct expression *get_faked_expression(void);
587 void __fake_struct_member_assignments(struct expression *expr);
589 /* smatch_project.c */
590 int is_no_inline_function(const char *function);
592 /* smatch_conditions */
593 void __split_whole_condition(struct expression *expr);
594 void __handle_logic(struct expression *expr);
595 int is_condition(struct expression *expr);
596 int __handle_condition_assigns(struct expression *expr);
597 int __handle_select_assigns(struct expression *expr);
598 int __handle_expr_statement_assigns(struct expression *expr);
600 /* smatch_implied.c */
601 struct range_list_stack;
602 void param_limit_implications(struct expression *expr, int param, char *key, char *value, struct stree **implied);
603 struct stree *__implied_case_stree(struct expression *switch_expr,
604 struct range_list *case_rl,
605 struct range_list_stack **remaining_cases,
606 struct stree **raw_stree);
607 void overwrite_states_using_pool(struct sm_state *gate_sm, struct sm_state *pool_sm);
608 int assume(struct expression *expr);
609 void end_assume(void);
610 int impossible_assumption(struct expression *left, int op, sval_t sval);
612 /* smatch_slist.h */
613 bool has_dynamic_states(unsigned short owner);
614 void set_dynamic_states(unsigned short owner);
616 /* smatch_extras.c */
617 int in_warn_on_macro(void);
618 #define SMATCH_EXTRA 5 /* this is my_id from smatch extra set in smatch.c */
619 extern int RETURN_ID;
621 struct data_range {
622 sval_t min;
623 sval_t max;
626 #define MTAG_ALIAS_BIT (1ULL << 63)
627 #define MTAG_OFFSET_MASK 0xfffULL
628 #define MTAG_SEED 0xdead << 12
630 const extern unsigned long valid_ptr_min;
631 extern unsigned long valid_ptr_max;
632 extern const sval_t valid_ptr_min_sval;
633 extern sval_t valid_ptr_max_sval;
634 extern struct range_list *valid_ptr_rl;
635 void alloc_valid_ptr_rl(void);
637 static const sval_t array_min_sval = {
638 .type = &ptr_ctype,
639 {.value = 100000},
641 static const sval_t array_max_sval = {
642 .type = &ptr_ctype,
643 {.value = ULONG_MAX - 4095},
645 static const sval_t text_seg_min = {
646 .type = &ptr_ctype,
647 {.value = 4096},
649 static const sval_t text_seg_max = {
650 .type = &ptr_ctype,
651 {.value = ULONG_MAX - 4095},
653 static const sval_t data_seg_min = {
654 .type = &ptr_ctype,
655 {.value = 4096},
657 static const sval_t data_seg_max = {
658 .type = &ptr_ctype,
659 {.value = ULONG_MAX - 4095},
661 static const sval_t bss_seg_min = {
662 .type = &ptr_ctype,
663 {.value = 4096},
665 static const sval_t bss_seg_max = {
666 .type = &ptr_ctype,
667 {.value = ULONG_MAX - 4095},
669 static const sval_t stack_seg_min = {
670 .type = &ptr_ctype,
671 {.value = 4096},
673 static const sval_t stack_seg_max = {
674 .type = &ptr_ctype,
675 {.value = ULONG_MAX - 4095},
677 static const sval_t kmalloc_seg_min = {
678 .type = &ptr_ctype,
679 {.value = 4096},
681 static const sval_t kmalloc_seg_max = {
682 .type = &ptr_ctype,
683 {.value = ULONG_MAX - 4095},
685 static const sval_t vmalloc_seg_min = {
686 .type = &ptr_ctype,
687 {.value = 4096},
689 static const sval_t vmalloc_seg_max = {
690 .type = &ptr_ctype,
691 {.value = ULONG_MAX - 4095},
693 static const sval_t fn_ptr_min = {
694 .type = &ptr_ctype,
695 {.value = 4096},
697 static const sval_t fn_ptr_max = {
698 .type = &ptr_ctype,
699 {.value = ULONG_MAX - 4095},
702 char *get_other_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym);
703 char *map_call_to_other_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym);
704 char *map_long_to_short_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym, bool use_stack);
706 #define STRLEN_MAX_RET 1010101
708 /* smatch_absolute.c */
709 int get_absolute_min_helper(struct expression *expr, sval_t *sval);
710 int get_absolute_max_helper(struct expression *expr, sval_t *sval);
712 /* smatch_type_value.c */
713 int get_db_type_rl(struct expression *expr, struct range_list **rl);
714 /* smatch_data_val.c */
715 int get_mtag_rl(struct expression *expr, struct range_list **rl);
716 /* smatch_array_values.c */
717 int get_array_rl(struct expression *expr, struct range_list **rl);
719 /* smatch_states.c */
720 struct stree *__swap_cur_stree(struct stree *stree);
721 void __push_fake_cur_stree();
722 struct stree *__pop_fake_cur_stree();
723 void __free_fake_cur_stree();
724 void __set_fake_cur_stree_fast(struct stree *stree);
725 void __pop_fake_cur_stree_fast(void);
726 void __merge_stree_into_cur(struct stree *stree);
728 int unreachable(void);
729 void __set_cur_stree_readonly(void);
730 void __set_cur_stree_writable(void);
731 void __set_sm(struct sm_state *sm);
732 void __set_sm_cur_stree(struct sm_state *sm);
733 void __set_sm_fake_stree(struct sm_state *sm);
734 void __set_true_false_sm(struct sm_state *true_state,
735 struct sm_state *false_state);
736 void nullify_path(void);
737 void __match_nullify_path_hook(const char *fn, struct expression *expr,
738 void *unused);
739 void __unnullify_path(void);
740 int __path_is_null(void);
741 void save_all_states(void);
742 void restore_all_states(void);
743 void free_goto_stack(void);
744 void clear_all_states(void);
746 struct sm_state *get_sm_state(int owner, const char *name,
747 struct symbol *sym);
748 struct sm_state *get_sm_state_expr(int owner, struct expression *expr);
749 void __push_true_states(void);
750 void __use_false_states(void);
751 void __discard_false_states(void);
752 void __merge_false_states(void);
753 void __merge_true_states(void);
755 void __negate_cond_stacks(void);
756 void __use_pre_cond_states(void);
757 void __use_cond_true_states(void);
758 void __use_cond_false_states(void);
759 void __push_cond_stacks(void);
760 void __fold_in_set_states(void);
761 void __free_set_states(void);
762 struct stree *__copy_cond_true_states(void);
763 struct stree *__copy_cond_false_states(void);
764 struct stree *__pop_cond_true_stack(void);
765 struct stree *__pop_cond_false_stack(void);
766 void __and_cond_states(void);
767 void __or_cond_states(void);
768 void __save_pre_cond_states(void);
769 void __discard_pre_cond_states(void);
770 struct stree *__get_true_states(void);
771 struct stree *__get_false_states(void);
772 void __use_cond_states(void);
773 extern struct state_list *__last_base_slist;
775 void __push_continues(void);
776 void __discard_continues(void);
777 void __process_continues(void);
778 void __merge_continues(void);
780 void __push_breaks(void);
781 void __process_breaks(void);
782 int __has_breaks(void);
783 void __merge_breaks(void);
784 void __use_breaks(void);
786 void __save_switch_states(struct expression *switch_expr);
787 void __discard_switches(void);
788 int have_remaining_cases(void);
789 void __merge_switches(struct expression *switch_expr, struct range_list *case_rl);
790 void __push_default(void);
791 void __set_default(void);
792 int __pop_default(void);
794 void __push_conditions(void);
795 void __discard_conditions(void);
797 void __save_gotos(const char *name, struct symbol *sym);
798 void __merge_gotos(const char *name, struct symbol *sym);
800 void __discard_fake_states(void);
802 void __print_cur_stree(void);
803 bool __print_states(const char *owner);
804 typedef void (check_tracker_hook)(int owner, const char *name, struct symbol *sym, struct smatch_state *state);
805 void add_check_tracker(const char *check_name, check_tracker_hook *fn);
807 /* smatch_hooks.c */
808 void __pass_to_client(void *data, enum hook_type type);
809 void __pass_case_to_client(struct expression *switch_expr,
810 struct range_list *rl);
811 int __has_merge_function(int client_id);
812 struct smatch_state *__client_merge_function(int owner,
813 struct smatch_state *s1,
814 struct smatch_state *s2);
815 struct smatch_state *__client_unmatched_state_function(struct sm_state *sm);
816 void call_pre_merge_hook(struct sm_state *cur, struct sm_state *other);
817 void __push_scope_hooks(void);
818 void __call_scope_hooks(void);
820 /* smatch_function_hooks.c */
821 void create_function_hook_hash(void);
822 void __match_initializer_call(struct symbol *sym);
824 /* smatch_db.c */
825 enum info_type {
826 INTERNAL = 0,
828 * Changing these numbers is a pain. Don't do it. If you ever use a
829 * number it can't be re-used right away so there may be gaps.
830 * We select these in order by type so if the order matters, then give
831 * it a number below 100-999,9000-9999 ranges. */
833 PARAM_CLEARED = 101,
834 PARAM_LIMIT = 103,
835 PARAM_FILTER = 104,
837 PARAM_VALUE = 1001,
838 BUF_SIZE = 1002,
839 CAPPED_DATA = 1004,
840 RETURN_VALUE = 1005,
841 DEREFERENCE = 1006,
842 RANGE_CAP = 1007,
843 ABSOLUTE_LIMITS = 1010,
844 PARAM_ADD = 1012,
845 PARAM_FREED = 1013,
846 MAYBE_FREED = 2014,
847 DATA_SOURCE = 1014,
848 FUZZY_MAX = 1015,
849 HARD_MAX = 2015,
850 STR_LEN = 1016,
851 ARRAY_LEN = 1017,
852 CAPABLE = 1018,
853 NS_CAPABLE = 1019,
854 CONTAINER = 1020,
855 CASTED_CALL = 1021,
856 TYPE_LINK = 1022,
857 UNTRACKED_PARAM = 1023,
858 LOST_PARAM = 2023,
859 CULL_PATH = 1024,
860 PARAM_SET = 1025,
861 PARAM_USED = 1026,
862 BYTE_UNITS = 1027,
863 COMPARE_LIMIT = 1028,
864 PARAM_COMPARE = 1029,
865 CONSTRAINT = 1031,
866 PASSES_TYPE = 1032,
867 CONSTRAINT_REQUIRED = 1033,
868 BIT_INFO = 1034,
869 NOSPEC = 1035,
870 NOSPEC_WB = 1036,
871 STMT_CNT = 1037,
872 TERMINATED = 1038,
873 FRESH_ALLOC = 1044,
874 ALLOCATOR = 1045,
876 /* put random temporary stuff in the 7000-7999 range for testing */
877 USER_DATA = 8017,
878 USER_DATA_SET = 9017,
879 USER_PTR = 9018,
880 USER_PTR_SET = 9019,
881 NO_OVERFLOW = 8018,
882 NO_OVERFLOW_SIMPLE = 8019,
883 LOCKED = 8020,
884 UNLOCKED = 8021,
885 HALF_LOCKED = 9022,
886 LOCK_RESTORED = 9023,
887 KNOWN_LOCKED = 9024,
888 KNOWN_UNLOCKED = 9025,
889 SET_FS = 8022,
890 ATOMIC_INC = 8023,
891 ATOMIC_DEC = 8024,
892 REFCOUNT = 9025,
893 NO_SIDE_EFFECT = 8025,
894 FN_ARG_LINK = 8028,
895 DATA_VALUE = 8029,
896 ARRAYSIZE_ARG = 8033,
897 SIZEOF_ARG = 8034,
898 MEMORY_TAG = 8036,
899 MTAG_ASSIGN = 8035,
900 STRING_VALUE = 8041,
901 ALLOC = 8045,
902 RELEASE = 8046,
904 BYTE_COUNT = 8050,
905 ELEM_COUNT = 8051,
906 ELEM_LAST = 8052,
907 USED_LAST = 8053,
908 USED_COUNT = 8054,
911 extern struct sqlite3 *smatch_db;
912 extern struct sqlite3 *mem_db;
913 extern struct sqlite3 *cache_db;
915 bool db_incomplete(void);
916 void db_ignore_states(int id);
917 void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type);
918 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm));
919 void add_caller_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm));
920 void add_return_info_callback(int owner,
921 void (*callback)(int return_id, char *return_ranges,
922 struct expression *returned_expr,
923 int param,
924 const char *printed_name,
925 struct sm_state *sm));
926 void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr));
927 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));
928 void select_call_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value));
929 void select_return_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value));
930 struct range_list *db_return_vals(struct expression *expr);
931 struct range_list *db_return_vals_from_str(const char *fn_name);
932 struct range_list *db_return_vals_no_args(struct expression *expr);
933 char *return_state_to_var_sym(struct expression *expr, int param, const char *key, struct symbol **sym);
934 char *get_chunk_from_key(struct expression *arg, char *key, struct symbol **sym, struct var_sym_list **vsl);
935 char *get_variable_from_key(struct expression *arg, const char *key, struct symbol **sym);
936 char *get_name_sym_from_key(struct expression *expr, int param, const char *key, struct symbol **sym);
937 const char *state_name_to_param_name(const char *state_name, const char *param_name);
938 const char *get_param_name_var_sym(const char *name, struct symbol *sym);
939 const char *get_param_name(struct sm_state *sm);
940 const char *get_mtag_name_var_sym(const char *state_name, struct symbol *sym);
941 const char *get_mtag_name_expr(struct expression *expr);
942 char *get_data_info_name(struct expression *expr);
943 char *sm_to_arg_name(struct expression *expr, struct sm_state *sm);
944 int is_recursive_member(const char *param_name);
946 char *escape_newlines(const char *str);
947 void sql_exec(struct sqlite3 *db, int (*callback)(void*, int, char**, char**), void *data, const char *sql);
949 #define sql_helper(db, call_back, data, sql...) \
950 do { \
951 char sql_txt[1024]; \
953 sqlite3_snprintf(sizeof(sql_txt), sql_txt, sql); \
954 db_debug("debug: %s\n", sql_txt); \
955 sql_exec(db, call_back, data, sql_txt); \
956 } while (0)
959 #define run_sql(call_back, data, sql...) \
960 do { \
961 if (option_no_db) \
962 break; \
963 sql_helper(smatch_db, call_back, data, sql); \
964 } while (0)
966 #define mem_sql(call_back, data, sql...) \
967 sql_helper(mem_db, call_back, data, sql)
969 #define cache_sql(call_back, data, sql...) \
970 sql_helper(cache_db, call_back, data, sql)
972 #define sql_insert_helper(table, db, ignore, late, values...) \
973 do { \
974 struct sqlite3 *_db = db; \
976 if (__inline_fn && !_db) \
977 _db = mem_db; \
978 if (_db) { \
979 char buf[1024]; \
980 char *err, *p = buf; \
981 int rc; \
983 p += snprintf(p, buf + sizeof(buf) - p, \
984 "insert %sinto %s values (", \
985 ignore ? "or ignore " : "", #table); \
986 p += snprintf(p, buf + sizeof(buf) - p, values); \
987 p += snprintf(p, buf + sizeof(buf) - p, ");"); \
988 db_debug("mem-db: %s\n", buf); \
989 rc = sqlite3_exec(_db, buf, NULL, NULL, &err); \
990 if (rc != SQLITE_OK) { \
991 sm_ierror("SQL error #2: %s", err); \
992 sm_ierror("SQL: '%s'", buf); \
993 parse_error = 1; \
995 break; \
997 if (option_info) { \
998 FILE *tmp_fd = sm_outfd; \
999 sm_outfd = sql_outfd; \
1000 sm_prefix(); \
1001 sm_printf("SQL%s: insert %sinto " #table " values(", \
1002 late ? "_late" : "", ignore ? "or ignore " : ""); \
1003 sm_printf(values); \
1004 sm_printf(");\n"); \
1005 sm_outfd = tmp_fd; \
1007 } while (0)
1009 #define sql_insert(table, values...) sql_insert_helper(table, 0, 0, 0, values);
1010 #define sql_insert_or_ignore(table, values...) sql_insert_helper(table, 0, 1, 0, values);
1011 #define sql_insert_late(table, values...) sql_insert_helper(table, 0, 0, 1, values);
1012 #define sql_insert_cache(table, values...) sql_insert_helper(table, cache_db, 1, 0, values);
1014 char *get_static_filter(struct symbol *sym);
1016 void sql_insert_return_states(int return_id, const char *return_ranges,
1017 int type, int param, const char *key, const char *value);
1018 void sql_insert_caller_info(struct expression *call, int type, int param,
1019 const char *key, const char *value);
1020 void sql_insert_function_ptr(const char *fn, const char *struct_name);
1021 void sql_insert_return_values(const char *return_values);
1022 void sql_insert_return_implies(int type, int param, const char *key, const char *value);
1023 void sql_insert_function_type_size(const char *member, const char *ranges);
1024 void sql_insert_function_type_info(int type, const char *struct_type, const char *member, const char *value);
1025 void sql_insert_type_info(int type, const char *member, const char *value);
1026 void sql_insert_local_values(const char *name, const char *value);
1027 void sql_insert_function_type_value(const char *type, const char *value);
1028 void sql_insert_function_type(int param, const char *value);
1029 void sql_insert_parameter_name(int param, const char *value);
1030 void sql_insert_data_info(struct expression *data, int type, const char *value);
1031 void sql_insert_data_info_var_sym(const char *var, struct symbol *sym, int type, const char *value);
1032 void sql_save_constraint(const char *con);
1033 void sql_save_constraint_required(const char *data, int op, const char *limit);
1034 void sql_copy_constraint_required(const char *new_limit, const char *old_limit);
1035 void sql_insert_fn_ptr_data_link(const char *ptr, const char *data);
1036 void sql_insert_fn_data_link(struct expression *fn, int type, int param, const char *key, const char *value);
1037 void sql_insert_mtag_about(mtag_t tag, const char *left_name, const char *right_name);
1038 void sql_insert_mtag_info(mtag_t tag, int type, const char *value);
1039 void sql_insert_mtag_map(mtag_t container, int container_offset, mtag_t tag, int tag_offset);
1040 void sql_insert_mtag_alias(mtag_t orig, mtag_t alias);
1041 int mtag_map_select_container(mtag_t tag, int container_offset, mtag_t *container);
1042 int mtag_map_select_tag(mtag_t container, int offset, mtag_t *tag);
1043 struct smatch_state *get_mtag_return(struct expression *expr, struct smatch_state *state);
1044 struct range_list *swap_mtag_seed(struct expression *expr, struct range_list *rl);
1046 void sql_select_return_states(const char *cols, struct expression *call,
1047 int (*callback)(void*, int, char**, char**), void *info);
1048 void sql_select_call_implies(const char *cols, struct expression *call,
1049 int (*callback)(void*, int, char**, char**));
1051 void open_smatch_db(char *db_file);
1053 /* smatch_files.c */
1054 int open_data_file(const char *filename);
1055 int open_schema_file(const char *schema);
1056 struct token *get_tokens_file(const char *filename);
1058 /* smatch.c */
1059 extern char *option_debug_check;
1060 extern char *option_project_str;
1061 extern char *bin_dir;
1062 extern char *data_dir;
1063 extern int option_no_data;
1064 extern int option_full_path;
1065 extern int option_call_tree;
1066 extern int num_checks;
1068 enum project_type {
1069 PROJ_NONE,
1070 PROJ_KERNEL,
1071 PROJ_WINE,
1072 PROJ_ILLUMOS_KERNEL,
1073 PROJ_ILLUMOS_USER,
1074 PROJ_UNKNOWN,
1076 extern enum project_type option_project;
1077 const char *check_name(unsigned short id);
1078 int id_from_name(const char *name);
1081 /* smatch_buf_size.c */
1082 int get_array_size(struct expression *expr);
1083 int get_array_size_bytes(struct expression *expr);
1084 int get_array_size_bytes_min(struct expression *expr);
1085 int get_array_size_bytes_max(struct expression *expr);
1086 struct range_list *get_array_size_bytes_rl(struct expression *expr);
1087 int get_real_array_size(struct expression *expr);
1088 int last_member_is_resizable(struct symbol *type);
1089 /* smatch_strlen.c */
1090 int get_implied_strlen(struct expression *expr, struct range_list **rl);
1091 int get_size_from_strlen(struct expression *expr);
1093 /* smatch_capped.c */
1094 int is_capped(struct expression *expr);
1095 int is_capped_var_sym(const char *name, struct symbol *sym);
1097 /* smatch_kernel_user_data.c */
1098 int is_user_macro(struct expression *expr);
1099 int is_capped_user_data(struct expression *expr);
1100 int implied_user_data(struct expression *expr, struct range_list **rl);
1101 struct stree *get_user_stree(void);
1102 int get_user_rl(struct expression *expr, struct range_list **rl);
1103 int is_user_rl(struct expression *expr);
1104 int get_user_rl_var_sym(const char *name, struct symbol *sym, struct range_list **rl);
1105 bool user_rl_capped(struct expression *expr);
1106 struct range_list *var_user_rl(struct expression *expr);
1107 bool we_pass_user_data(struct expression *call);
1108 /* smatch_points_to_user_data.c */
1109 bool is_user_data_fn(struct symbol *fn);
1110 bool is_skb_data(struct expression *expr);
1111 bool points_to_user_data(struct expression *expr);
1112 void set_points_to_user_data(struct expression *expr);
1114 /* check_locking.c */
1115 void print_held_locks();
1117 /* check_assigned_expr.c */
1118 extern int check_assigned_expr_id;
1119 struct expression *get_assigned_expr(struct expression *expr);
1120 struct expression *get_assigned_expr_name_sym(const char *name, struct symbol *sym);
1121 /* smatch_return_to_param.c */
1122 void __add_return_to_param_mapping(struct expression *assign, const char *return_string);
1123 char *map_call_to_param_name_sym(struct expression *expr, struct symbol **sym);
1125 /* smatch_comparison.c */
1126 extern int comparison_id;
1127 #define UNKNOWN_COMPARISON 0
1128 #define IMPOSSIBLE_COMPARISON -1
1129 struct compare_data {
1130 /* The ->left and ->right expression pointers might be NULL (I'm lazy) */
1131 struct expression *left;
1132 const char *left_var;
1133 struct var_sym_list *left_vsl;
1134 int comparison;
1135 struct expression *right;
1136 const char *right_var;
1137 struct var_sym_list *right_vsl;
1139 DECLARE_ALLOCATOR(compare_data);
1140 struct smatch_state *alloc_compare_state(
1141 struct expression *left,
1142 const char *left_var, struct var_sym_list *left_vsl,
1143 int comparison,
1144 struct expression *right,
1145 const char *right_var, struct var_sym_list *right_vsl);
1146 int comparison_intersection(int orig, int op);
1147 int merge_comparisons(int one, int two);
1148 int combine_comparisons(int left_compare, int right_compare);
1149 int state_to_comparison(struct smatch_state *state);
1150 struct smatch_state *merge_compare_states(struct smatch_state *s1, struct smatch_state *s2);
1151 int get_comparison(struct expression *left, struct expression *right);
1152 int get_comparison_no_extra(struct expression *a, struct expression *b);
1153 int get_comparison_strings(const char *one, const char *two);
1154 int possible_comparison(struct expression *a, int comparison, struct expression *b);
1155 struct state_list *get_all_comparisons(struct expression *expr);
1156 struct state_list *get_all_possible_equal_comparisons(struct expression *expr);
1157 void __add_return_comparison(struct expression *call, const char *range);
1158 void __add_comparison_info(struct expression *expr, struct expression *call, const char *range);
1159 char *get_printed_param_name(struct expression *call, const char *param_name, struct symbol *param_sym);
1160 char *name_sym_to_param_comparison(const char *name, struct symbol *sym);
1161 char *expr_equal_to_param(struct expression *expr, int ignore);
1162 char *expr_lte_to_param(struct expression *expr, int ignore);
1163 char *expr_param_comparison(struct expression *expr, int ignore);
1164 int flip_comparison(int op);
1165 int negate_comparison(int op);
1166 int remove_unsigned_from_comparison(int op);
1167 int param_compare_limit_is_impossible(struct expression *expr, int left_param, char *left_key, char *value);
1168 void filter_by_comparison(struct range_list **rl, int comparison, struct range_list *right);
1169 void __compare_param_limit_hook(struct expression *left_expr, struct expression *right_expr,
1170 const char *state_name,
1171 struct smatch_state *true_state, struct smatch_state *false_state);
1172 int impossibly_high_comparison(struct expression *expr);
1174 /* smatch_sval.c */
1175 sval_t *sval_alloc(sval_t sval);
1176 sval_t *sval_alloc_permanent(sval_t sval);
1177 sval_t sval_blank(struct expression *expr);
1178 sval_t sval_type_val(struct symbol *type, long long val);
1179 sval_t sval_type_fval(struct symbol *type, long double fval);
1180 sval_t sval_from_val(struct expression *expr, long long val);
1181 sval_t sval_from_fval(struct expression *expr, long double fval);
1182 int sval_is_ptr(sval_t sval);
1183 bool sval_is_fp(sval_t sval);
1184 int sval_unsigned(sval_t sval);
1185 int sval_signed(sval_t sval);
1186 int sval_bits(sval_t sval);
1187 int sval_bits_used(sval_t sval);
1188 int sval_is_negative(sval_t sval);
1189 int sval_is_positive(sval_t sval);
1190 int sval_is_min(sval_t sval);
1191 int sval_is_max(sval_t sval);
1192 int sval_is_a_min(sval_t sval);
1193 int sval_is_a_max(sval_t sval);
1194 int sval_is_negative_min(sval_t sval);
1195 int sval_cmp_t(struct symbol *type, sval_t one, sval_t two);
1196 int sval_cmp_val(sval_t one, long long val);
1197 sval_t sval_min(sval_t one, sval_t two);
1198 sval_t sval_max(sval_t one, sval_t two);
1199 int sval_too_low(struct symbol *type, sval_t sval);
1200 int sval_too_high(struct symbol *type, sval_t sval);
1201 int sval_fits(struct symbol *type, sval_t sval);
1202 sval_t sval_cast(struct symbol *type, sval_t sval);
1203 sval_t sval_preop(sval_t sval, int op);
1204 sval_t sval_binop(sval_t left, int op, sval_t right);
1205 int sval_binop_overflows(sval_t left, int op, sval_t right);
1206 int sval_binop_overflows_no_sign(sval_t left, int op, sval_t right);
1207 int find_first_zero_bit(unsigned long long uvalue);
1208 int sm_fls64(unsigned long long uvalue);
1209 unsigned long long fls_mask(unsigned long long uvalue);
1210 unsigned long long sval_fls_mask(sval_t sval);
1211 const char *sval_to_str(sval_t sval);
1212 const char *sval_to_str_or_err_ptr(sval_t sval);
1213 const char *sval_to_numstr(sval_t sval);
1214 sval_t ll_to_sval(long long val);
1216 /* smatch_string_list.c */
1217 int list_has_string(struct string_list *str_list, const char *str);
1218 int insert_string(struct string_list **str_list, const char *str);
1219 struct string_list *clone_str_list(struct string_list *orig);
1220 struct string_list *combine_string_lists(struct string_list *one, struct string_list *two);
1222 /* smatch_start_states.c */
1223 struct stree *get_start_states(void);
1225 /* smatch_recurse.c */
1226 int has_symbol(struct expression *expr, struct symbol *sym);
1227 int has_variable(struct expression *expr, struct expression *var);
1228 int has_inc_dec(struct expression *expr);
1230 /* smatch_stored_conditions.c */
1231 struct smatch_state *get_stored_condition(struct expression *expr);
1232 struct expression_list *get_conditions(struct expression *expr);
1233 struct sm_state *stored_condition_implication_hook(struct expression *expr,
1234 struct state_list **true_stack,
1235 struct state_list **false_stack);
1236 /* smatch_parsed_conditions.c */
1237 struct sm_state *parsed_condition_implication_hook(struct expression *expr,
1238 struct state_list **true_stack,
1239 struct state_list **false_stack);
1240 /* smatch_comparison.c */
1241 struct sm_state *comparison_implication_hook(struct expression *expr,
1242 struct state_list **true_stack,
1243 struct state_list **false_stack);
1245 /* check_string_len.c */
1246 int get_formatted_string_size(struct expression *call, int arg);
1247 int get_formatted_string_min_size(struct expression *call, int arg);
1249 /* smatch_param_set.c */
1250 int param_was_set(struct expression *expr);
1251 int param_was_set_var_sym(const char *name, struct symbol *sym);
1252 void print_limited_param_set(int return_id, char *return_ranges, struct expression *expr);
1253 /* smatch_param_filter.c */
1254 int param_has_filter_data(struct sm_state *sm);
1256 /* smatch_links.c */
1257 void set_up_link_functions(int id, int linkid);
1258 struct smatch_state *merge_link_states(struct smatch_state *s1, struct smatch_state *s2);
1259 void store_link(int link_id, const char *name, struct symbol *sym, const char *link_name, struct symbol *link_sym);
1261 /* check_buf_comparison */
1262 const char *limit_type_str(unsigned int limit_type);
1263 struct expression *get_size_variable(struct expression *buf, int *limit_type);
1264 struct expression *get_array_variable(struct expression *size);
1265 int buf_comparison_index_ok(struct expression *expr);
1267 /* smatch_untracked_param.c */
1268 void mark_untracked(struct expression *expr, int param, const char *key, const char *value);
1269 void add_untracked_param_hook(void (func)(struct expression *call, int param));
1270 void add_lost_param_hook(void (func)(struct expression *call, int param));
1271 void mark_all_params_untracked(int return_id, char *return_ranges, struct expression *expr);
1273 /* smatch_strings.c */
1274 struct state_list *get_strings(struct expression *expr);
1275 struct expression *fake_string_from_mtag(mtag_t tag);
1277 /* smatch_estate.c */
1278 int estate_get_single_value(struct smatch_state *state, sval_t *sval);
1280 /* smatch_address.c */
1281 int get_address_rl(struct expression *expr, struct range_list **rl);
1282 int get_member_offset(struct symbol *type, const char *member_name);
1283 int get_member_offset_from_deref(struct expression *expr);
1285 /* for now this is in smatch_used_parameter.c */
1286 void __get_state_hook(int owner, const char *name, struct symbol *sym);
1288 /* smatch_buf_comparison.c */
1289 int db_var_is_array_limit(struct expression *array, const char *name, struct var_sym_list *vsl);
1291 struct range_list *get_fs(void);
1293 struct stree *get_all_return_states(void);
1294 struct stree_stack *get_all_return_strees(void);
1295 int on_atomic_dec_path(void);
1296 int was_inced(const char *name, struct symbol *sym);
1297 void set_refcount_inc(char *name, struct symbol *sym);
1298 void set_refcount_dec(char *name, struct symbol *sym);
1300 /* smatch_constraints.c */
1301 char *get_constraint_str(struct expression *expr);
1302 struct constraint_list *get_constraints(struct expression *expr);
1303 char *unmet_constraint(struct expression *data, struct expression *offset);
1304 char *get_required_constraint(const char *data_str);
1306 /* smatch_container_of.c */
1307 int get_param_from_container_of(struct expression *expr);
1308 int get_offset_from_container_of(struct expression *expr);
1309 char *get_container_name(struct expression *container, struct expression *expr);
1311 /* smatch_mtag.c */
1312 mtag_t str_to_mtag(const char *str);
1313 int get_string_mtag(struct expression *expr, mtag_t *tag);
1314 int get_toplevel_mtag(struct symbol *sym, mtag_t *tag);
1315 int create_mtag_alias(mtag_t tag, struct expression *expr, mtag_t *new);
1316 int expr_to_mtag_offset(struct expression *expr, mtag_t *tag, int *offset);
1317 void update_mtag_data(struct expression *expr, struct smatch_state *state);
1318 int get_mtag_sval(struct expression *expr, sval_t *sval);
1320 /* Trinity fuzzer stuff */
1321 const char *get_syscall_arg_type(struct symbol *sym);
1323 /* smatch_bit_info.c */
1324 struct bit_info *rl_to_binfo(struct range_list *rl);
1325 struct bit_info *get_bit_info(struct expression *expr);
1326 struct bit_info *get_bit_info_var_sym(const char *name, struct symbol *sym);
1327 /* smatch_mem_tracker.c */
1328 extern int option_mem;
1329 unsigned long get_mem_kb(void);
1330 unsigned long get_max_memory(void);
1332 /* check_is_nospec.c */
1333 bool is_nospec(struct expression *expr);
1334 long get_stmt_cnt(void);
1336 /* smatch_nul_terminator.c */
1337 bool is_nul_terminated_var_sym(const char *name, struct symbol *sym);
1338 bool is_nul_terminated(struct expression *expr);
1339 /* check_kernel.c */
1340 bool is_ignored_kernel_data(const char *name);
1342 bool is_fresh_alloc_var_sym(const char *var, struct symbol *sym);
1343 bool is_fresh_alloc(struct expression *expr);
1344 bool is_freed_var_sym(const char *name, struct symbol *sym);
1345 void track_freed_param(struct expression *expr, struct smatch_state *state);
1346 void track_freed_param_var_sym(const char *name, struct symbol *sym,
1347 struct smatch_state *state);
1349 static inline bool type_is_ptr(struct symbol *type)
1351 return type &&
1352 (type->type == SYM_PTR ||
1353 type->type == SYM_ARRAY ||
1354 type->type == SYM_FN);
1357 static inline bool type_is_fp(struct symbol *type)
1359 return type &&
1360 (type == &float_ctype ||
1361 type == &double_ctype ||
1362 type == &ldouble_ctype);
1365 static inline int type_bits(struct symbol *type)
1367 if (!type)
1368 return 0;
1369 if (type_is_ptr(type))
1370 return bits_in_pointer;
1371 if (!type->examined)
1372 examine_symbol_type(type);
1373 return type->bit_size;
1376 static inline int type_unsigned(struct symbol *base_type)
1378 if (!base_type)
1379 return 0;
1380 if (is_ptr_type(base_type))
1381 return 1;
1382 if (base_type->ctype.modifiers & MOD_UNSIGNED)
1383 return 1;
1384 return 0;
1387 static inline int type_positive_bits(struct symbol *type)
1389 if (!type)
1390 return 0;
1391 if (is_ptr_type(type))
1392 return bits_in_pointer;
1393 if (type_unsigned(type))
1394 return type_bits(type);
1395 return type_bits(type) - 1;
1398 static inline int sval_positive_bits(sval_t sval)
1400 return type_positive_bits(sval.type);
1404 * Returns -1 if one is smaller, 0 if they are the same and 1 if two is larger.
1407 static inline int fp_cmp(sval_t one, sval_t two)
1409 struct symbol *type;
1411 if (sval_is_fp(one) && sval_is_fp(two))
1412 type = type_bits(one.type) > type_bits(two.type) ? one.type : two.type;
1413 else if (sval_is_fp(one))
1414 type = one.type;
1415 else
1416 type = two.type;
1418 one = sval_cast(type, one);
1419 two = sval_cast(type, two);
1421 if (one.type == &float_ctype) {
1422 if (one.fvalue < two.fvalue)
1423 return -1;
1424 if (one.fvalue == two.fvalue)
1425 return 0;
1426 return 1;
1428 if (one.type == &double_ctype) {
1429 if (one.dvalue < two.dvalue)
1430 return -1;
1431 if (one.dvalue == two.dvalue)
1432 return 0;
1433 return 1;
1435 if (one.type == &ldouble_ctype) {
1436 if (one.ldvalue < two.ldvalue)
1437 return -1;
1438 if (one.ldvalue == two.ldvalue)
1439 return 0;
1440 return 1;
1442 sm_perror("bad type in fp_cmp(): %s", type_to_str(type));
1443 return 1;
1446 static inline int sval_cmp(sval_t one, sval_t two)
1448 struct symbol *type;
1450 if (sval_is_fp(one) || sval_is_fp(two))
1451 return fp_cmp(one, two);
1453 type = one.type;
1454 if (sval_positive_bits(two) > sval_positive_bits(one))
1455 type = two.type;
1456 if (type_bits(type) < 31)
1457 type = &int_ctype;
1459 one = sval_cast(type, one);
1460 two = sval_cast(type, two);
1462 if (type_unsigned(type)) {
1463 if (one.uvalue < two.uvalue)
1464 return -1;
1465 if (one.uvalue == two.uvalue)
1466 return 0;
1467 return 1;
1469 /* fix me handle type promotion and unsigned values */
1470 if (one.value < two.value)
1471 return -1;
1472 if (one.value == two.value)
1473 return 0;
1474 return 1;
1477 #endif /* !SMATCH_H_ */