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