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