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