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