xref: /illumos-gate/usr/src/tools/smatch/src/smatch.h (revision 6523a3aa)
11f5207b7SJohn Levon /*
21f5207b7SJohn Levon  * Copyright (C) 2006 Dan Carpenter.
31f5207b7SJohn Levon  *
41f5207b7SJohn Levon  * This program is free software; you can redistribute it and/or
51f5207b7SJohn Levon  * modify it under the terms of the GNU General Public License
61f5207b7SJohn Levon  * as published by the Free Software Foundation; either version 2
71f5207b7SJohn Levon  * of the License, or (at your option) any later version.
81f5207b7SJohn Levon  *
91f5207b7SJohn Levon  * This program is distributed in the hope that it will be useful,
101f5207b7SJohn Levon  * but WITHOUT ANY WARRANTY; without even the implied warranty of
111f5207b7SJohn Levon  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
121f5207b7SJohn Levon  * GNU General Public License for more details.
131f5207b7SJohn Levon  *
141f5207b7SJohn Levon  * You should have received a copy of the GNU General Public License
151f5207b7SJohn Levon  * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
167ae7577cSJohn Levon  *
177ae7577cSJohn Levon  * Copyright 2019 Joyent, Inc.
181f5207b7SJohn Levon  */
191f5207b7SJohn Levon 
201f5207b7SJohn Levon #ifndef   	SMATCH_H_
211f5207b7SJohn Levon # define   	SMATCH_H_
221f5207b7SJohn Levon 
231f5207b7SJohn Levon #include <stdio.h>
241f5207b7SJohn Levon #include <string.h>
251f5207b7SJohn Levon #include <limits.h>
265a0e240fSJohn Levon #include <float.h>
271f5207b7SJohn Levon #include <sys/time.h>
281f5207b7SJohn Levon #include <sqlite3.h>
291f5207b7SJohn Levon #include "lib.h"
301f5207b7SJohn Levon #include "allocate.h"
311f5207b7SJohn Levon #include "scope.h"
321f5207b7SJohn Levon #include "parse.h"
331f5207b7SJohn Levon #include "expression.h"
341f5207b7SJohn Levon #include "avl.h"
351f5207b7SJohn Levon 
361f5207b7SJohn Levon typedef struct {
371f5207b7SJohn Levon 	struct symbol *type;
381f5207b7SJohn Levon 	union {
391f5207b7SJohn Levon 		long long value;
401f5207b7SJohn Levon 		unsigned long long uvalue;
415a0e240fSJohn Levon 		float fvalue;
425a0e240fSJohn Levon 		double dvalue;
435a0e240fSJohn Levon 		long double ldvalue;
441f5207b7SJohn Levon 	};
451f5207b7SJohn Levon } sval_t;
461f5207b7SJohn Levon 
471f5207b7SJohn Levon typedef long long mtag_t;
481f5207b7SJohn Levon 
491f5207b7SJohn Levon struct smatch_state {
501f5207b7SJohn Levon 	const char *name;
511f5207b7SJohn Levon 	void *data;
521f5207b7SJohn Levon };
531f5207b7SJohn Levon #define STATE(_x) static struct smatch_state _x = { .name = #_x }
541f5207b7SJohn Levon extern struct smatch_state undefined;
551f5207b7SJohn Levon extern struct smatch_state ghost;
561f5207b7SJohn Levon extern struct smatch_state merged;
571f5207b7SJohn Levon extern struct smatch_state true_state;
581f5207b7SJohn Levon extern struct smatch_state false_state;
591f5207b7SJohn Levon DECLARE_ALLOCATOR(smatch_state);
601f5207b7SJohn Levon 
INT_PTR(int i)611f5207b7SJohn Levon static inline void *INT_PTR(int i)
621f5207b7SJohn Levon {
631f5207b7SJohn Levon 	return (void *)(long)i;
641f5207b7SJohn Levon }
651f5207b7SJohn Levon 
PTR_INT(void * p)661f5207b7SJohn Levon static inline int PTR_INT(void *p)
671f5207b7SJohn Levon {
681f5207b7SJohn Levon 	return (int)(long)p;
691f5207b7SJohn Levon }
701f5207b7SJohn Levon 
711f5207b7SJohn Levon struct tracker {
721f5207b7SJohn Levon 	char *name;
731f5207b7SJohn Levon 	struct symbol *sym;
741f5207b7SJohn Levon 	unsigned short owner;
751f5207b7SJohn Levon };
761f5207b7SJohn Levon DECLARE_ALLOCATOR(tracker);
771f5207b7SJohn Levon DECLARE_PTR_LIST(tracker_list, struct tracker);
781f5207b7SJohn Levon DECLARE_PTR_LIST(stree_stack, struct stree);
791f5207b7SJohn Levon 
801f5207b7SJohn Levon /* The first 3 struct members must match struct tracker */
811f5207b7SJohn Levon struct sm_state {
821f5207b7SJohn Levon 	const char *name;
831f5207b7SJohn Levon 	struct symbol *sym;
841f5207b7SJohn Levon 	unsigned short owner;
851f5207b7SJohn Levon 	unsigned short merged:1;
861f5207b7SJohn Levon 	unsigned int line;
871f5207b7SJohn Levon   	struct smatch_state *state;
881f5207b7SJohn Levon 	struct stree *pool;
891f5207b7SJohn Levon 	struct sm_state *left;
901f5207b7SJohn Levon 	struct sm_state *right;
911f5207b7SJohn Levon 	struct state_list *possible;
921f5207b7SJohn Levon };
931f5207b7SJohn Levon 
941f5207b7SJohn Levon struct var_sym {
951f5207b7SJohn Levon 	char *var;
961f5207b7SJohn Levon 	struct symbol *sym;
971f5207b7SJohn Levon };
981f5207b7SJohn Levon DECLARE_ALLOCATOR(var_sym);
991f5207b7SJohn Levon DECLARE_PTR_LIST(var_sym_list, struct var_sym);
1001f5207b7SJohn Levon 
1011f5207b7SJohn Levon struct constraint {
1021f5207b7SJohn Levon 	int op;
1031f5207b7SJohn Levon 	int id;
1041f5207b7SJohn Levon };
1051f5207b7SJohn Levon DECLARE_PTR_LIST(constraint_list, struct constraint);
1061f5207b7SJohn Levon 
10731ad075eSJohn Levon struct alloc_info {
10831ad075eSJohn Levon 	const char *fn;
10931ad075eSJohn Levon 	int size_param, nr;
11031ad075eSJohn Levon };
11131ad075eSJohn Levon extern struct alloc_info *alloc_funcs;
11231ad075eSJohn Levon 
113efe51d0cSJohn Levon struct bit_info {
114efe51d0cSJohn Levon 	unsigned long long set;
115efe51d0cSJohn Levon 	unsigned long long possible;
116efe51d0cSJohn Levon };
117efe51d0cSJohn Levon 
1181f5207b7SJohn Levon enum hook_type {
1191f5207b7SJohn Levon 	EXPR_HOOK,
120c85f09ccSJohn Levon 	EXPR_HOOK_AFTER,
1211f5207b7SJohn Levon 	STMT_HOOK,
1221f5207b7SJohn Levon 	STMT_HOOK_AFTER,
1231f5207b7SJohn Levon 	SYM_HOOK,
1241f5207b7SJohn Levon 	STRING_HOOK,
1251f5207b7SJohn Levon 	DECLARATION_HOOK,
1261f5207b7SJohn Levon 	ASSIGNMENT_HOOK,
1271f5207b7SJohn Levon 	ASSIGNMENT_HOOK_AFTER,
1281f5207b7SJohn Levon 	RAW_ASSIGNMENT_HOOK,
1291f5207b7SJohn Levon 	GLOBAL_ASSIGNMENT_HOOK,
1301f5207b7SJohn Levon 	LOGIC_HOOK,
1311f5207b7SJohn Levon 	CONDITION_HOOK,
1321f5207b7SJohn Levon 	PRELOOP_HOOK,
1331f5207b7SJohn Levon 	SELECT_HOOK,
1341f5207b7SJohn Levon 	WHOLE_CONDITION_HOOK,
1351f5207b7SJohn Levon 	FUNCTION_CALL_HOOK_BEFORE,
1361f5207b7SJohn Levon 	FUNCTION_CALL_HOOK,
1371f5207b7SJohn Levon 	CALL_HOOK_AFTER_INLINE,
1381f5207b7SJohn Levon 	FUNCTION_CALL_HOOK_AFTER_DB,
1391f5207b7SJohn Levon 	CALL_ASSIGNMENT_HOOK,
1401f5207b7SJohn Levon 	MACRO_ASSIGNMENT_HOOK,
1411f5207b7SJohn Levon 	BINOP_HOOK,
1421f5207b7SJohn Levon 	OP_HOOK,
1431f5207b7SJohn Levon 	DEREF_HOOK,
1441f5207b7SJohn Levon 	CASE_HOOK,
1451f5207b7SJohn Levon 	ASM_HOOK,
1461f5207b7SJohn Levon 	CAST_HOOK,
1471f5207b7SJohn Levon 	SIZEOF_HOOK,
1481f5207b7SJohn Levon 	BASE_HOOK,
1491f5207b7SJohn Levon 	FUNC_DEF_HOOK,
1501f5207b7SJohn Levon 	AFTER_DEF_HOOK,
1511f5207b7SJohn Levon 	END_FUNC_HOOK,
1521f5207b7SJohn Levon 	AFTER_FUNC_HOOK,
1531f5207b7SJohn Levon 	RETURN_HOOK,
1541f5207b7SJohn Levon 	INLINE_FN_START,
1551f5207b7SJohn Levon 	INLINE_FN_END,
1561f5207b7SJohn Levon 	END_FILE_HOOK,
1571f5207b7SJohn Levon 	NUM_HOOKS,
1581f5207b7SJohn Levon };
1591f5207b7SJohn Levon 
1601f5207b7SJohn Levon #define TRUE 1
1611f5207b7SJohn Levon #define FALSE 0
1621f5207b7SJohn Levon 
1631f5207b7SJohn Levon struct range_list;
1641f5207b7SJohn Levon 
1651f5207b7SJohn Levon void add_hook(void *func, enum hook_type type);
1661f5207b7SJohn Levon typedef struct smatch_state *(merge_func_t)(struct smatch_state *s1, struct smatch_state *s2);
1671f5207b7SJohn Levon typedef struct smatch_state *(unmatched_func_t)(struct sm_state *state);
1681f5207b7SJohn Levon void add_merge_hook(int client_id, merge_func_t *func);
1691f5207b7SJohn Levon void add_unmatched_state_hook(int client_id, unmatched_func_t *func);
170c85f09ccSJohn Levon void add_pre_merge_hook(int client_id, void (*hook)(struct sm_state *cur, struct sm_state *other));
1711f5207b7SJohn Levon typedef void (scope_hook)(void *data);
1721f5207b7SJohn Levon void add_scope_hook(scope_hook *hook, void *data);
1731f5207b7SJohn Levon typedef void (func_hook)(const char *fn, struct expression *expr, void *data);
1741f5207b7SJohn Levon typedef void (implication_hook)(const char *fn, struct expression *call_expr,
1751f5207b7SJohn Levon 				struct expression *assign_expr, void *data);
1761f5207b7SJohn Levon typedef void (return_implies_hook)(struct expression *call_expr,
1771f5207b7SJohn Levon 				   int param, char *key, char *value);
1781f5207b7SJohn Levon typedef int (implied_return_hook)(struct expression *call_expr, void *info, struct range_list **rl);
1791f5207b7SJohn Levon void add_function_hook(const char *look_for, func_hook *call_back, void *data);
1801f5207b7SJohn Levon 
1811f5207b7SJohn Levon void add_function_assign_hook(const char *look_for, func_hook *call_back,
1821f5207b7SJohn Levon 			      void *info);
1831f5207b7SJohn Levon void add_implied_return_hook(const char *look_for,
1841f5207b7SJohn Levon 			     implied_return_hook *call_back,
1851f5207b7SJohn Levon 			     void *info);
1861f5207b7SJohn Levon void add_macro_assign_hook(const char *look_for, func_hook *call_back,
1871f5207b7SJohn Levon 			      void *info);
1881f5207b7SJohn Levon void add_macro_assign_hook_extra(const char *look_for, func_hook *call_back,
1891f5207b7SJohn Levon 			      void *info);
1901f5207b7SJohn Levon void return_implies_state(const char *look_for, long long start, long long end,
1911f5207b7SJohn Levon 			 implication_hook *call_back, void *info);
192efe51d0cSJohn Levon void return_implies_state_sval(const char *look_for, sval_t start, sval_t end,
193efe51d0cSJohn Levon 			 implication_hook *call_back, void *info);
1941f5207b7SJohn Levon void select_return_states_hook(int type, return_implies_hook *callback);
1951f5207b7SJohn Levon void select_return_states_before(void (*fn)(void));
1961f5207b7SJohn Levon void select_return_states_after(void (*fn)(void));
1971f5207b7SJohn Levon int get_implied_return(struct expression *expr, struct range_list **rl);
1981f5207b7SJohn Levon void allocate_hook_memory(void);
199*6523a3aaSJohn Levon void allocate_tracker_array(int num_checks);
2001f5207b7SJohn Levon 
2011f5207b7SJohn Levon struct modification_data {
2021f5207b7SJohn Levon 	struct smatch_state *prev;
2031f5207b7SJohn Levon 	struct expression *cur;
2041f5207b7SJohn Levon };
2051f5207b7SJohn Levon 
2061f5207b7SJohn Levon typedef void (modification_hook)(struct sm_state *sm, struct expression *mod_expr);
2071f5207b7SJohn Levon void add_modification_hook(int owner, modification_hook *call_back);
2081f5207b7SJohn Levon void add_modification_hook_late(int owner, modification_hook *call_back);
2091f5207b7SJohn Levon struct smatch_state *get_modification_state(struct expression *expr);
2101f5207b7SJohn Levon 
2111f5207b7SJohn Levon int outside_of_function(void);
2121f5207b7SJohn Levon const char *get_filename(void);
2131f5207b7SJohn Levon const char *get_base_file(void);
2141f5207b7SJohn Levon char *get_function(void);
2151f5207b7SJohn Levon int get_lineno(void);
2161f5207b7SJohn Levon extern int final_pass;
2171f5207b7SJohn Levon extern struct symbol *cur_func_sym;
2181f5207b7SJohn Levon extern int option_debug;
2191f5207b7SJohn Levon extern int local_debug;
2205a0e240fSJohn Levon extern int debug_db;
221c85f09ccSJohn Levon bool debug_implied(void);
2221f5207b7SJohn Levon extern int option_info;
2231f5207b7SJohn Levon extern int option_spammy;
2247ae7577cSJohn Levon extern int option_timeout;
2251f5207b7SJohn Levon extern char *trace_variable;
2261f5207b7SJohn Levon extern struct stree *global_states;
2271f5207b7SJohn Levon int is_skipped_function(void);
2281f5207b7SJohn Levon int is_silenced_function(void);
229efe51d0cSJohn Levon extern bool implications_off;
2301f5207b7SJohn Levon 
2311f5207b7SJohn Levon /* smatch_impossible.c */
2321f5207b7SJohn Levon int is_impossible_path(void);
2331f5207b7SJohn Levon void set_path_impossible(void);
2341f5207b7SJohn Levon 
2351f5207b7SJohn Levon extern FILE *sm_outfd;
2361f5207b7SJohn Levon extern FILE *sql_outfd;
2371f5207b7SJohn Levon extern FILE *caller_info_fd;
2381f5207b7SJohn Levon extern int sm_nr_checks;
2391f5207b7SJohn Levon extern int sm_nr_errors;
2401f5207b7SJohn Levon extern const char *progname;
2411f5207b7SJohn Levon 
2421f5207b7SJohn Levon /*
2431f5207b7SJohn Levon  * How to use these routines:
2441f5207b7SJohn Levon  *
2451f5207b7SJohn Levon  * sm_fatal(): an internal error of some kind that should immediately exit
2461f5207b7SJohn Levon  * sm_ierror(): an internal error
2471f5207b7SJohn Levon  * sm_perror(): an internal error from parsing input source
2481f5207b7SJohn Levon  * sm_error(): an error from input source
2491f5207b7SJohn Levon  * sm_warning(): a warning from input source
2501f5207b7SJohn Levon  * sm_info(): info message (from option_info)
2511f5207b7SJohn Levon  * sm_debug(): debug message
2521f5207b7SJohn Levon  * sm_msg(): other message (please avoid using this)
2531f5207b7SJohn Levon  */
2541f5207b7SJohn Levon 
2555a0e240fSJohn Levon #define sm_printf(msg...) do {						\
2565a0e240fSJohn Levon 	if (final_pass || option_debug || local_debug || debug_db)	\
2575a0e240fSJohn Levon 		fprintf(sm_outfd, msg);					\
2585a0e240fSJohn Levon } while (0)
2591f5207b7SJohn Levon 
sm_prefix(void)2601f5207b7SJohn Levon static inline void sm_prefix(void)
2611f5207b7SJohn Levon {
2621f5207b7SJohn Levon 	sm_printf("%s: %s:%d %s() ", progname, get_filename(), get_lineno(), get_function());
2631f5207b7SJohn Levon }
2641f5207b7SJohn Levon 
2651f5207b7SJohn Levon static inline void print_implied_debug_msg();
2661f5207b7SJohn Levon 
2671f5207b7SJohn Levon extern bool __silence_warnings_for_stmt;
2681f5207b7SJohn Levon 
2691f5207b7SJohn Levon #define sm_print_msg(type, msg...) \
2701f5207b7SJohn Levon do {                                                           \
2711f5207b7SJohn Levon 	print_implied_debug_msg();                             \
2725a0e240fSJohn Levon 	if (!final_pass && !option_debug && !local_debug && !debug_db)	  \
2731f5207b7SJohn Levon 		break;                                         \
2741f5207b7SJohn Levon 	if (__silence_warnings_for_stmt && !option_debug && !local_debug) \
2751f5207b7SJohn Levon 		break;					       \
2761f5207b7SJohn Levon 	if (!option_info && is_silenced_function())	       \
2771f5207b7SJohn Levon 		break;					       \
2781f5207b7SJohn Levon 	sm_prefix();					       \
2791f5207b7SJohn Levon 	if (type == 1) {				       \
2801f5207b7SJohn Levon 		sm_printf("warn: ");			       \
2811f5207b7SJohn Levon 		sm_nr_checks++;			    	       \
2821f5207b7SJohn Levon 	} else if (type == 2) {				       \
2831f5207b7SJohn Levon 		sm_printf("error: ");			       \
2841f5207b7SJohn Levon 		sm_nr_checks++;				       \
2851f5207b7SJohn Levon 	} else if (type == 3) {				       \
2861f5207b7SJohn Levon 		sm_printf("parse error: ");		       \
2871f5207b7SJohn Levon 		sm_nr_errors++;				       \
2881f5207b7SJohn Levon 	}						       \
2891f5207b7SJohn Levon         sm_printf(msg);                                        \
2901f5207b7SJohn Levon         sm_printf("\n");                                       \
2911f5207b7SJohn Levon } while (0)
2921f5207b7SJohn Levon 
2931f5207b7SJohn Levon #define sm_msg(msg...) do { sm_print_msg(0, msg); } while (0)
2941f5207b7SJohn Levon 
2951f5207b7SJohn Levon extern char *implied_debug_msg;
print_implied_debug_msg(void)2961f5207b7SJohn Levon static inline void print_implied_debug_msg(void)
2971f5207b7SJohn Levon {
2981f5207b7SJohn Levon 	static struct symbol *last_printed = NULL;
2991f5207b7SJohn Levon 
3001f5207b7SJohn Levon 	if (!implied_debug_msg)
3011f5207b7SJohn Levon 		return;
3021f5207b7SJohn Levon 	if (last_printed == cur_func_sym)
3031f5207b7SJohn Levon 		return;
3041f5207b7SJohn Levon 	last_printed = cur_func_sym;
3051f5207b7SJohn Levon 	sm_msg("%s", implied_debug_msg);
3061f5207b7SJohn Levon }
3071f5207b7SJohn Levon 
3081f5207b7SJohn Levon #define sm_debug(msg...) do { if (option_debug) sm_printf(msg); } while (0)
3095a0e240fSJohn Levon #define db_debug(msg...) do { if (option_debug || debug_db) sm_printf(msg); } while (0)
3101f5207b7SJohn Levon 
3111f5207b7SJohn Levon #define sm_info(msg...) do {					\
3121f5207b7SJohn Levon 	if (option_debug || (option_info && final_pass)) {	\
3131f5207b7SJohn Levon 		sm_prefix();					\
3141f5207b7SJohn Levon 		sm_printf("info: ");				\
3151f5207b7SJohn Levon 		sm_printf(msg);					\
3161f5207b7SJohn Levon 		sm_printf("\n");				\
3171f5207b7SJohn Levon 	}							\
3181f5207b7SJohn Levon } while(0)
3191f5207b7SJohn Levon 
3201f5207b7SJohn Levon #define sm_warning(msg...) do { sm_print_msg(1, msg); } while (0)
3211f5207b7SJohn Levon #define sm_error(msg...) do { sm_print_msg(2, msg); } while (0)
3221f5207b7SJohn Levon #define sm_perror(msg...) do { sm_print_msg(3, msg); } while (0)
3231f5207b7SJohn Levon 
sm_fatal(const char * fmt,...)3241f5207b7SJohn Levon static inline void sm_fatal(const char *fmt, ...)
3251f5207b7SJohn Levon {
3261f5207b7SJohn Levon 	va_list args;
3271f5207b7SJohn Levon 
3281f5207b7SJohn Levon 	va_start(args, fmt);
3291f5207b7SJohn Levon 	vfprintf(sm_outfd, fmt, args);
3301f5207b7SJohn Levon 	va_end(args);
3311f5207b7SJohn Levon 
3321f5207b7SJohn Levon 	fprintf(sm_outfd, "\n");
3331f5207b7SJohn Levon 
3341f5207b7SJohn Levon 	exit(1);
3351f5207b7SJohn Levon }
3361f5207b7SJohn Levon 
sm_ierror(const char * fmt,...)3371f5207b7SJohn Levon static inline void sm_ierror(const char *fmt, ...)
3381f5207b7SJohn Levon {
3391f5207b7SJohn Levon 	va_list args;
3401f5207b7SJohn Levon 
3411f5207b7SJohn Levon 	sm_nr_errors++;
3421f5207b7SJohn Levon 
3431f5207b7SJohn Levon 	fprintf(sm_outfd, "internal error: ");
3441f5207b7SJohn Levon 
3451f5207b7SJohn Levon 	va_start(args, fmt);
3461f5207b7SJohn Levon 	vfprintf(sm_outfd, fmt, args);
3471f5207b7SJohn Levon 	va_end(args);
3481f5207b7SJohn Levon 
3491f5207b7SJohn Levon 	fprintf(sm_outfd, "\n");
3501f5207b7SJohn Levon }
3511f5207b7SJohn Levon #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
3521f5207b7SJohn Levon 
3531f5207b7SJohn Levon struct smatch_state *__get_state(int owner, const char *name, struct symbol *sym);
3541f5207b7SJohn Levon struct smatch_state *get_state(int owner, const char *name, struct symbol *sym);
3551f5207b7SJohn Levon struct smatch_state *get_state_expr(int owner, struct expression *expr);
3561f5207b7SJohn Levon struct state_list *get_possible_states(int owner, const char *name,
3571f5207b7SJohn Levon 				       struct symbol *sym);
3581f5207b7SJohn Levon struct state_list *get_possible_states_expr(int owner, struct expression *expr);
3591f5207b7SJohn Levon struct sm_state *set_state(int owner, const char *name, struct symbol *sym,
3601f5207b7SJohn Levon 	       struct smatch_state *state);
3611f5207b7SJohn Levon struct sm_state *set_state_expr(int owner, struct expression *expr,
3621f5207b7SJohn Levon 		struct smatch_state *state);
3631f5207b7SJohn Levon void delete_state(int owner, const char *name, struct symbol *sym);
3641f5207b7SJohn Levon void delete_state_expr(int owner, struct expression *expr);
3651f5207b7SJohn Levon void __delete_all_states_sym(struct symbol *sym);
3661f5207b7SJohn Levon void set_true_false_states(int owner, const char *name, struct symbol *sym,
3671f5207b7SJohn Levon 			   struct smatch_state *true_state,
3681f5207b7SJohn Levon 			   struct smatch_state *false_state);
3691f5207b7SJohn Levon void set_true_false_states_expr(int owner, struct expression *expr,
3701f5207b7SJohn Levon 			   struct smatch_state *true_state,
3711f5207b7SJohn Levon 			   struct smatch_state *false_state);
3721f5207b7SJohn Levon 
3731f5207b7SJohn Levon struct stree *get_all_states_from_stree(int owner, struct stree *source);
3741f5207b7SJohn Levon struct stree *get_all_states_stree(int id);
3751f5207b7SJohn Levon struct stree *__get_cur_stree(void);
3761f5207b7SJohn Levon int is_reachable(void);
3771f5207b7SJohn Levon void add_get_state_hook(void (*fn)(int owner, const char *name, struct symbol *sym));
3781f5207b7SJohn Levon 
3791f5207b7SJohn Levon /* smatch_helper.c */
3801f5207b7SJohn Levon DECLARE_PTR_LIST(int_stack, int);
3811f5207b7SJohn Levon char *alloc_string(const char *str);
382c85f09ccSJohn Levon char *alloc_string_newline(const char *str);
3831f5207b7SJohn Levon void free_string(char *str);
3841f5207b7SJohn Levon void append(char *dest, const char *data, int buff_len);
3851f5207b7SJohn Levon void remove_parens(char *str);
3861f5207b7SJohn Levon struct smatch_state *alloc_state_num(int num);
3871f5207b7SJohn Levon struct smatch_state *alloc_state_str(const char *name);
388efe51d0cSJohn Levon struct smatch_state *merge_str_state(struct smatch_state *s1, struct smatch_state *s2);
3891f5207b7SJohn Levon struct smatch_state *alloc_state_expr(struct expression *expr);
3901f5207b7SJohn Levon struct expression *get_argument_from_call_expr(struct expression_list *args,
3911f5207b7SJohn Levon 					       int num);
392*6523a3aaSJohn Levon struct expression *get_array_expr(struct expression *expr);
3931f5207b7SJohn Levon 
3941f5207b7SJohn Levon char *expr_to_var(struct expression *expr);
3951f5207b7SJohn Levon struct symbol *expr_to_sym(struct expression *expr);
3961f5207b7SJohn Levon char *expr_to_str(struct expression *expr);
3971f5207b7SJohn Levon char *expr_to_str_sym(struct expression *expr,
3981f5207b7SJohn Levon 				     struct symbol **sym_ptr);
3991f5207b7SJohn Levon char *expr_to_var_sym(struct expression *expr,
4001f5207b7SJohn Levon 			     struct symbol **sym_ptr);
4011f5207b7SJohn Levon char *expr_to_known_chunk_sym(struct expression *expr, struct symbol **sym);
4021f5207b7SJohn Levon char *expr_to_chunk_sym_vsl(struct expression *expr, struct symbol **sym, struct var_sym_list **vsl);
4031f5207b7SJohn Levon int get_complication_score(struct expression *expr);
4041f5207b7SJohn Levon 
4051f5207b7SJohn Levon int sym_name_is(const char *name, struct expression *expr);
4061f5207b7SJohn Levon int get_const_value(struct expression *expr, sval_t *sval);
4071f5207b7SJohn Levon int get_value(struct expression *expr, sval_t *val);
4081f5207b7SJohn Levon int get_implied_value(struct expression *expr, sval_t *val);
40931ad075eSJohn Levon int get_implied_value_fast(struct expression *expr, sval_t *sval);
4101f5207b7SJohn Levon int get_implied_min(struct expression *expr, sval_t *sval);
4111f5207b7SJohn Levon int get_implied_max(struct expression *expr, sval_t *val);
4121f5207b7SJohn Levon int get_hard_max(struct expression *expr, sval_t *sval);
4131f5207b7SJohn Levon int get_fuzzy_min(struct expression *expr, sval_t *min);
4141f5207b7SJohn Levon int get_fuzzy_max(struct expression *expr, sval_t *max);
4151f5207b7SJohn Levon int get_absolute_min(struct expression *expr, sval_t *sval);
4161f5207b7SJohn Levon int get_absolute_max(struct expression *expr, sval_t *sval);
4171f5207b7SJohn Levon int parse_call_math(struct expression *expr, char *math, sval_t *val);
418efe51d0cSJohn Levon int parse_call_math_rl(struct expression *call, const char *math, struct range_list **rl);
419c85f09ccSJohn Levon const char *get_allocation_math(struct expression *expr);
4201f5207b7SJohn Levon char *get_value_in_terms_of_parameter_math(struct expression *expr);
4211f5207b7SJohn Levon char *get_value_in_terms_of_parameter_math_var_sym(const char *var, struct symbol *sym);
422c85f09ccSJohn Levon int expr_is_zero(struct expression *expr);
4231f5207b7SJohn Levon int known_condition_true(struct expression *expr);
4241f5207b7SJohn Levon int known_condition_false(struct expression *expr);
4251f5207b7SJohn Levon int implied_condition_true(struct expression *expr);
4261f5207b7SJohn Levon int implied_condition_false(struct expression *expr);
4271f5207b7SJohn Levon int can_integer_overflow(struct symbol *type, struct expression *expr);
4281f5207b7SJohn Levon void clear_math_cache(void);
429c85f09ccSJohn Levon void set_fast_math_only(void);
430c85f09ccSJohn Levon void clear_fast_math_only(void);
4311f5207b7SJohn Levon 
4321f5207b7SJohn Levon int is_array(struct expression *expr);
4331f5207b7SJohn Levon struct expression *get_array_base(struct expression *expr);
4341f5207b7SJohn Levon struct expression *get_array_offset(struct expression *expr);
4351f5207b7SJohn Levon const char *show_state(struct smatch_state *state);
4361f5207b7SJohn Levon struct statement *get_expression_statement(struct expression *expr);
4371f5207b7SJohn Levon struct expression *strip_parens(struct expression *expr);
4381f5207b7SJohn Levon struct expression *strip_expr(struct expression *expr);
4391f5207b7SJohn Levon struct expression *strip_expr_set_parent(struct expression *expr);
4401f5207b7SJohn Levon void scoped_state(int my_id, const char *name, struct symbol *sym);
4411f5207b7SJohn Levon int is_error_return(struct expression *expr);
442c85f09ccSJohn Levon int getting_address(struct expression *expr);
4431f5207b7SJohn Levon int get_struct_and_member(struct expression *expr, const char **type, const char **member);
4441f5207b7SJohn Levon char *get_member_name(struct expression *expr);
4451f5207b7SJohn Levon char *get_fnptr_name(struct expression *expr);
4461f5207b7SJohn Levon int cmp_pos(struct position pos1, struct position pos2);
4471f5207b7SJohn Levon int positions_eq(struct position pos1, struct position pos2);
4481f5207b7SJohn Levon struct statement *get_current_statement(void);
4491f5207b7SJohn Levon struct statement *get_prev_statement(void);
4501f5207b7SJohn Levon struct expression *get_last_expr_from_expression_stmt(struct expression *expr);
451*6523a3aaSJohn Levon 
452*6523a3aaSJohn Levon #define RETURN_VAR    -1
453*6523a3aaSJohn Levon #define LOCAL_SCOPE   -2
454*6523a3aaSJohn Levon #define FILE_SCOPE    -3
455*6523a3aaSJohn Levon #define GLOBAL_SCOPE  -4
456*6523a3aaSJohn Levon #define UNKNOWN_SCOPE -5
4571f5207b7SJohn Levon int get_param_num_from_sym(struct symbol *sym);
4581f5207b7SJohn Levon int get_param_num(struct expression *expr);
4595a0e240fSJohn Levon struct symbol *get_param_sym_from_num(int num);
460*6523a3aaSJohn Levon 
4611f5207b7SJohn Levon int ms_since(struct timeval *start);
4621f5207b7SJohn Levon int parent_is_gone_var_sym(const char *name, struct symbol *sym);
4631f5207b7SJohn Levon int parent_is_gone(struct expression *expr);
4641f5207b7SJohn Levon int invert_op(int op);
465efe51d0cSJohn Levon int op_remove_assign(int op);
4661f5207b7SJohn Levon int expr_equiv(struct expression *one, struct expression *two);
4671f5207b7SJohn Levon void push_int(struct int_stack **stack, int num);
4681f5207b7SJohn Levon int pop_int(struct int_stack **stack);
4691f5207b7SJohn Levon 
4701f5207b7SJohn Levon /* smatch_type.c */
4711f5207b7SJohn Levon struct symbol *get_real_base_type(struct symbol *sym);
4721f5207b7SJohn Levon int type_bytes(struct symbol *type);
4731f5207b7SJohn Levon int array_bytes(struct symbol *type);
4741f5207b7SJohn Levon struct symbol *get_pointer_type(struct expression *expr);
4751f5207b7SJohn Levon struct symbol *get_type(struct expression *expr);
4761f5207b7SJohn Levon struct symbol *get_final_type(struct expression *expr);
4771f5207b7SJohn Levon struct symbol *get_promoted_type(struct symbol *left, struct symbol *right);
4781f5207b7SJohn Levon int type_signed(struct symbol *base_type);
4791f5207b7SJohn Levon int expr_unsigned(struct expression *expr);
4801f5207b7SJohn Levon int expr_signed(struct expression *expr);
4811f5207b7SJohn Levon int returns_unsigned(struct symbol *base_type);
4821f5207b7SJohn Levon int is_pointer(struct expression *expr);
4831f5207b7SJohn Levon int returns_pointer(struct symbol *base_type);
4841f5207b7SJohn Levon sval_t sval_type_max(struct symbol *base_type);
4851f5207b7SJohn Levon sval_t sval_type_min(struct symbol *base_type);
4861f5207b7SJohn Levon int nr_bits(struct expression *expr);
4871f5207b7SJohn Levon int is_void_pointer(struct expression *expr);
4881f5207b7SJohn Levon int is_char_pointer(struct expression *expr);
4891f5207b7SJohn Levon int is_string(struct expression *expr);
490*6523a3aaSJohn Levon bool is_struct_ptr(struct symbol *type);
4911f5207b7SJohn Levon int is_static(struct expression *expr);
492c85f09ccSJohn Levon bool is_local_variable(struct expression *expr);
4931f5207b7SJohn Levon int types_equiv(struct symbol *one, struct symbol *two);
494*6523a3aaSJohn Levon bool type_fits(struct symbol *type, struct symbol *test);
4951f5207b7SJohn Levon int fn_static(void);
4961f5207b7SJohn Levon const char *global_static();
4971f5207b7SJohn Levon struct symbol *cur_func_return_type(void);
4981f5207b7SJohn Levon struct symbol *get_arg_type(struct expression *fn, int arg);
4991f5207b7SJohn Levon struct symbol *get_member_type_from_key(struct expression *expr, const char *key);
5001f5207b7SJohn Levon struct symbol *get_arg_type_from_key(struct expression *fn, int param, struct expression *arg, const char *key);
5011f5207b7SJohn Levon int is_struct(struct expression *expr);
5021f5207b7SJohn Levon char *type_to_str(struct symbol *type);
5031f5207b7SJohn Levon 
5041f5207b7SJohn Levon /* smatch_ignore.c */
5051f5207b7SJohn Levon void add_ignore(int owner, const char *name, struct symbol *sym);
5061f5207b7SJohn Levon int is_ignored(int owner, const char *name, struct symbol *sym);
5071f5207b7SJohn Levon void add_ignore_expr(int owner, struct expression *expr);
5081f5207b7SJohn Levon int is_ignored_expr(int owner, struct expression *expr);
5091f5207b7SJohn Levon 
5101f5207b7SJohn Levon /* smatch_var_sym */
5111f5207b7SJohn Levon struct var_sym *alloc_var_sym(const char *var, struct symbol *sym);
5121f5207b7SJohn Levon struct var_sym_list *expr_to_vsl(struct expression *expr);
5131f5207b7SJohn Levon void add_var_sym(struct var_sym_list **list, const char *var, struct symbol *sym);
5141f5207b7SJohn Levon void add_var_sym_expr(struct var_sym_list **list, struct expression *expr);
5151f5207b7SJohn Levon void del_var_sym(struct var_sym_list **list, const char *var, struct symbol *sym);
5161f5207b7SJohn Levon int in_var_sym_list(struct var_sym_list *list, const char *var, struct symbol *sym);
5171f5207b7SJohn Levon struct var_sym_list *clone_var_sym_list(struct var_sym_list *from_vsl);
5181f5207b7SJohn Levon void merge_var_sym_list(struct var_sym_list **dest, struct var_sym_list *src);
5191f5207b7SJohn Levon struct var_sym_list *combine_var_sym_lists(struct var_sym_list *one, struct var_sym_list *two);
5201f5207b7SJohn Levon int var_sym_lists_equiv(struct var_sym_list *one, struct var_sym_list *two);
5211f5207b7SJohn Levon void free_var_sym_list(struct var_sym_list **list);
5221f5207b7SJohn Levon void free_var_syms_and_list(struct var_sym_list **list);
5231f5207b7SJohn Levon 
5241f5207b7SJohn Levon /* smatch_tracker */
5251f5207b7SJohn Levon struct tracker *alloc_tracker(int owner, const char *name, struct symbol *sym);
5261f5207b7SJohn Levon void add_tracker(struct tracker_list **list, int owner, const char *name,
5271f5207b7SJohn Levon 		struct symbol *sym);
5281f5207b7SJohn Levon void add_tracker_expr(struct tracker_list **list, int owner, struct expression *expr);
5291f5207b7SJohn Levon void del_tracker(struct tracker_list **list, int owner, const char *name,
5301f5207b7SJohn Levon 		struct symbol *sym);
5311f5207b7SJohn Levon int in_tracker_list(struct tracker_list *list, int owner, const char *name,
5321f5207b7SJohn Levon 		struct symbol *sym);
5331f5207b7SJohn Levon void free_tracker_list(struct tracker_list **list);
5341f5207b7SJohn Levon void free_trackers_and_list(struct tracker_list **list);
5351f5207b7SJohn Levon 
5361f5207b7SJohn Levon /* smatch_conditions */
5371f5207b7SJohn Levon int in_condition(void);
5381f5207b7SJohn Levon 
5391f5207b7SJohn Levon /* smatch_flow.c */
5401f5207b7SJohn Levon 
5411f5207b7SJohn Levon extern int __in_fake_assign;
5421f5207b7SJohn Levon extern int __in_fake_parameter_assign;
5431f5207b7SJohn Levon extern int __in_fake_struct_assign;
5441f5207b7SJohn Levon extern int in_fake_env;
545efe51d0cSJohn Levon void smatch (struct string_list *filelist);
5461f5207b7SJohn Levon int inside_loop(void);
5471f5207b7SJohn Levon int definitely_inside_loop(void);
5481f5207b7SJohn Levon struct expression *get_switch_expr(void);
5491f5207b7SJohn Levon int in_expression_statement(void);
5501f5207b7SJohn Levon void __process_post_op_stack(void);
5511f5207b7SJohn Levon void __split_expr(struct expression *expr);
5521f5207b7SJohn Levon void __split_label_stmt(struct statement *stmt);
5531f5207b7SJohn Levon void __split_stmt(struct statement *stmt);
5541f5207b7SJohn Levon extern int __in_function_def;
555c85f09ccSJohn Levon extern int __in_unmatched_hook;
5561f5207b7SJohn Levon extern int option_assume_loops;
5571f5207b7SJohn Levon extern int option_two_passes;
5581f5207b7SJohn Levon extern int option_no_db;
5591f5207b7SJohn Levon extern int option_file_output;
5601f5207b7SJohn Levon extern int option_time;
5611f5207b7SJohn Levon extern struct expression_list *big_expression_stack;
5621f5207b7SJohn Levon extern struct expression_list *big_condition_stack;
5631f5207b7SJohn Levon extern struct statement_list *big_statement_stack;
5641f5207b7SJohn Levon int is_assigned_call(struct expression *expr);
5651f5207b7SJohn Levon int inlinable(struct expression *expr);
5661f5207b7SJohn Levon extern int __inline_call;
5671f5207b7SJohn Levon extern struct expression *__inline_fn;
5681f5207b7SJohn Levon extern int __in_pre_condition;
5691f5207b7SJohn Levon extern int __bail_on_rest_of_function;
5701f5207b7SJohn Levon extern struct statement *__prev_stmt;
5711f5207b7SJohn Levon extern struct statement *__cur_stmt;
5721f5207b7SJohn Levon extern struct statement *__next_stmt;
5731f5207b7SJohn Levon void init_fake_env(void);
5741f5207b7SJohn Levon void end_fake_env(void);
5751f5207b7SJohn Levon int time_parsing_function(void);
576efe51d0cSJohn Levon bool taking_too_long(void);
5771f5207b7SJohn Levon 
5781f5207b7SJohn Levon /* smatch_struct_assignment.c */
5791f5207b7SJohn Levon struct expression *get_faked_expression(void);
5801f5207b7SJohn Levon void __fake_struct_member_assignments(struct expression *expr);
5811f5207b7SJohn Levon 
5821f5207b7SJohn Levon /* smatch_project.c */
5831f5207b7SJohn Levon int is_no_inline_function(const char *function);
5841f5207b7SJohn Levon 
5851f5207b7SJohn Levon /* smatch_conditions */
5861f5207b7SJohn Levon void __split_whole_condition(struct expression *expr);
5871f5207b7SJohn Levon void __handle_logic(struct expression *expr);
5881f5207b7SJohn Levon int is_condition(struct expression *expr);
5891f5207b7SJohn Levon int __handle_condition_assigns(struct expression *expr);
5901f5207b7SJohn Levon int __handle_select_assigns(struct expression *expr);
5911f5207b7SJohn Levon int __handle_expr_statement_assigns(struct expression *expr);
5921f5207b7SJohn Levon 
5931f5207b7SJohn Levon /* smatch_implied.c */
5941f5207b7SJohn Levon struct range_list_stack;
595*6523a3aaSJohn Levon void param_limit_implications(struct expression *expr, int param, char *key, char *value, struct stree **implied);
5961f5207b7SJohn Levon struct stree *__implied_case_stree(struct expression *switch_expr,
5971f5207b7SJohn Levon 				   struct range_list *case_rl,
5981f5207b7SJohn Levon 				   struct range_list_stack **remaining_cases,
5991f5207b7SJohn Levon 				   struct stree **raw_stree);
6001f5207b7SJohn Levon void overwrite_states_using_pool(struct sm_state *gate_sm, struct sm_state *pool_sm);
6011f5207b7SJohn Levon int assume(struct expression *expr);
6021f5207b7SJohn Levon void end_assume(void);
6031f5207b7SJohn Levon int impossible_assumption(struct expression *left, int op, sval_t sval);
6041f5207b7SJohn Levon 
605efe51d0cSJohn Levon /* smatch_slist.h */
606efe51d0cSJohn Levon bool has_dynamic_states(unsigned short owner);
607efe51d0cSJohn Levon void set_dynamic_states(unsigned short owner);
608efe51d0cSJohn Levon 
6091f5207b7SJohn Levon /* smatch_extras.c */
610efe51d0cSJohn Levon int in_warn_on_macro(void);
6111f5207b7SJohn Levon #define SMATCH_EXTRA 5 /* this is my_id from smatch extra set in smatch.c */
6121f5207b7SJohn Levon extern int RETURN_ID;
6131f5207b7SJohn Levon 
6141f5207b7SJohn Levon struct data_range {
6151f5207b7SJohn Levon 	sval_t min;
6161f5207b7SJohn Levon 	sval_t max;
6171f5207b7SJohn Levon };
6181f5207b7SJohn Levon 
6191f5207b7SJohn Levon #define MTAG_ALIAS_BIT (1ULL << 63)
6201f5207b7SJohn Levon #define MTAG_OFFSET_MASK 0xfffULL
621efe51d0cSJohn Levon #define MTAG_SEED 0xdead << 12
6221f5207b7SJohn Levon 
623efe51d0cSJohn Levon const extern unsigned long valid_ptr_min;
624efe51d0cSJohn Levon extern unsigned long valid_ptr_max;
625efe51d0cSJohn Levon extern const sval_t valid_ptr_min_sval;
626efe51d0cSJohn Levon extern sval_t valid_ptr_max_sval;
6271f5207b7SJohn Levon extern struct range_list *valid_ptr_rl;
628efe51d0cSJohn Levon void alloc_valid_ptr_rl(void);
629efe51d0cSJohn Levon 
6301f5207b7SJohn Levon static const sval_t array_min_sval = {
6311f5207b7SJohn Levon 	.type = &ptr_ctype,
6321f5207b7SJohn Levon 	{.value = 100000},
6331f5207b7SJohn Levon };
6341f5207b7SJohn Levon static const sval_t array_max_sval = {
6351f5207b7SJohn Levon 	.type = &ptr_ctype,
636efe51d0cSJohn Levon 	{.value = ULONG_MAX - 4095},
6371f5207b7SJohn Levon };
6381f5207b7SJohn Levon static const sval_t text_seg_min = {
6391f5207b7SJohn Levon 	.type = &ptr_ctype,
640efe51d0cSJohn Levon 	{.value = 4096},
6411f5207b7SJohn Levon };
6421f5207b7SJohn Levon static const sval_t text_seg_max = {
6431f5207b7SJohn Levon 	.type = &ptr_ctype,
644efe51d0cSJohn Levon 	{.value = ULONG_MAX - 4095},
6451f5207b7SJohn Levon };
6461f5207b7SJohn Levon static const sval_t data_seg_min = {
6471f5207b7SJohn Levon 	.type = &ptr_ctype,
648efe51d0cSJohn Levon 	{.value = 4096},
6491f5207b7SJohn Levon };
6501f5207b7SJohn Levon static const sval_t data_seg_max = {
6511f5207b7SJohn Levon 	.type = &ptr_ctype,
652efe51d0cSJohn Levon 	{.value = ULONG_MAX - 4095},
6531f5207b7SJohn Levon };
6541f5207b7SJohn Levon static const sval_t bss_seg_min = {
6551f5207b7SJohn Levon 	.type = &ptr_ctype,
656efe51d0cSJohn Levon 	{.value = 4096},
6571f5207b7SJohn Levon };
6581f5207b7SJohn Levon static const sval_t bss_seg_max = {
6591f5207b7SJohn Levon 	.type = &ptr_ctype,
660efe51d0cSJohn Levon 	{.value = ULONG_MAX - 4095},
6611f5207b7SJohn Levon };
6621f5207b7SJohn Levon static const sval_t stack_seg_min = {
6631f5207b7SJohn Levon 	.type = &ptr_ctype,
664efe51d0cSJohn Levon 	{.value = 4096},
6651f5207b7SJohn Levon };
6661f5207b7SJohn Levon static const sval_t stack_seg_max = {
6671f5207b7SJohn Levon 	.type = &ptr_ctype,
668efe51d0cSJohn Levon 	{.value = ULONG_MAX - 4095},
6691f5207b7SJohn Levon };
6701f5207b7SJohn Levon static const sval_t kmalloc_seg_min = {
6711f5207b7SJohn Levon 	.type = &ptr_ctype,
672efe51d0cSJohn Levon 	{.value = 4096},
6731f5207b7SJohn Levon };
6741f5207b7SJohn Levon static const sval_t kmalloc_seg_max = {
6751f5207b7SJohn Levon 	.type = &ptr_ctype,
676efe51d0cSJohn Levon 	{.value = ULONG_MAX - 4095},
6771f5207b7SJohn Levon };
6781f5207b7SJohn Levon static const sval_t vmalloc_seg_min = {
6791f5207b7SJohn Levon 	.type = &ptr_ctype,
680efe51d0cSJohn Levon 	{.value = 4096},
6811f5207b7SJohn Levon };
6821f5207b7SJohn Levon static const sval_t vmalloc_seg_max = {
6831f5207b7SJohn Levon 	.type = &ptr_ctype,
684efe51d0cSJohn Levon 	{.value = ULONG_MAX - 4095},
6851f5207b7SJohn Levon };
6861f5207b7SJohn Levon static const sval_t fn_ptr_min = {
6871f5207b7SJohn Levon 	.type = &ptr_ctype,
688efe51d0cSJohn Levon 	{.value = 4096},
6891f5207b7SJohn Levon };
6901f5207b7SJohn Levon static const sval_t fn_ptr_max = {
6911f5207b7SJohn Levon 	.type = &ptr_ctype,
692efe51d0cSJohn Levon 	{.value = ULONG_MAX - 4095},
6931f5207b7SJohn Levon };
6941f5207b7SJohn Levon 
6951f5207b7SJohn Levon char *get_other_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym);
6961f5207b7SJohn Levon char *map_call_to_other_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym);
697efe51d0cSJohn Levon char *map_long_to_short_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym, bool use_stack);
6981f5207b7SJohn Levon 
6991f5207b7SJohn Levon #define STRLEN_MAX_RET 1010101
7001f5207b7SJohn Levon 
7011f5207b7SJohn Levon /* smatch_absolute.c */
7021f5207b7SJohn Levon int get_absolute_min_helper(struct expression *expr, sval_t *sval);
7031f5207b7SJohn Levon int get_absolute_max_helper(struct expression *expr, sval_t *sval);
7041f5207b7SJohn Levon 
7051f5207b7SJohn Levon /* smatch_type_value.c */
7061f5207b7SJohn Levon int get_db_type_rl(struct expression *expr, struct range_list **rl);
7071f5207b7SJohn Levon /* smatch_data_val.c */
7081f5207b7SJohn Levon int get_mtag_rl(struct expression *expr, struct range_list **rl);
7091f5207b7SJohn Levon /* smatch_array_values.c */
7101f5207b7SJohn Levon int get_array_rl(struct expression *expr, struct range_list **rl);
7111f5207b7SJohn Levon 
7121f5207b7SJohn Levon /* smatch_states.c */
713c85f09ccSJohn Levon struct stree *__swap_cur_stree(struct stree *stree);
7141f5207b7SJohn Levon void __push_fake_cur_stree();
7151f5207b7SJohn Levon struct stree *__pop_fake_cur_stree();
7161f5207b7SJohn Levon void __free_fake_cur_stree();
7171f5207b7SJohn Levon void __set_fake_cur_stree_fast(struct stree *stree);
7181f5207b7SJohn Levon void __pop_fake_cur_stree_fast(void);
7191f5207b7SJohn Levon void __merge_stree_into_cur(struct stree *stree);
7201f5207b7SJohn Levon 
7211f5207b7SJohn Levon int unreachable(void);
722c85f09ccSJohn Levon void __set_cur_stree_readonly(void);
723c85f09ccSJohn Levon void __set_cur_stree_writable(void);
7241f5207b7SJohn Levon void __set_sm(struct sm_state *sm);
7251f5207b7SJohn Levon void __set_sm_cur_stree(struct sm_state *sm);
7261f5207b7SJohn Levon void __set_sm_fake_stree(struct sm_state *sm);
7271f5207b7SJohn Levon void __set_true_false_sm(struct sm_state *true_state,
7281f5207b7SJohn Levon 			struct sm_state *false_state);
7291f5207b7SJohn Levon void nullify_path(void);
7301f5207b7SJohn Levon void __match_nullify_path_hook(const char *fn, struct expression *expr,
7311f5207b7SJohn Levon 			       void *unused);
7321f5207b7SJohn Levon void __unnullify_path(void);
7331f5207b7SJohn Levon int __path_is_null(void);
7341f5207b7SJohn Levon void save_all_states(void);
7351f5207b7SJohn Levon void restore_all_states(void);
7361f5207b7SJohn Levon void free_goto_stack(void);
7371f5207b7SJohn Levon void clear_all_states(void);
7381f5207b7SJohn Levon 
7391f5207b7SJohn Levon struct sm_state *get_sm_state(int owner, const char *name,
7401f5207b7SJohn Levon 				struct symbol *sym);
7411f5207b7SJohn Levon struct sm_state *get_sm_state_expr(int owner, struct expression *expr);
7421f5207b7SJohn Levon void __push_true_states(void);
7431f5207b7SJohn Levon void __use_false_states(void);
7441f5207b7SJohn Levon void __discard_false_states(void);
7451f5207b7SJohn Levon void __merge_false_states(void);
7461f5207b7SJohn Levon void __merge_true_states(void);
7471f5207b7SJohn Levon 
7481f5207b7SJohn Levon void __negate_cond_stacks(void);
7491f5207b7SJohn Levon void __use_pre_cond_states(void);
7501f5207b7SJohn Levon void __use_cond_true_states(void);
7511f5207b7SJohn Levon void __use_cond_false_states(void);
7521f5207b7SJohn Levon void __push_cond_stacks(void);
7531f5207b7SJohn Levon void __fold_in_set_states(void);
7541f5207b7SJohn Levon void __free_set_states(void);
7551f5207b7SJohn Levon struct stree *__copy_cond_true_states(void);
7561f5207b7SJohn Levon struct stree *__copy_cond_false_states(void);
7571f5207b7SJohn Levon struct stree *__pop_cond_true_stack(void);
7581f5207b7SJohn Levon struct stree *__pop_cond_false_stack(void);
7591f5207b7SJohn Levon void __and_cond_states(void);
7601f5207b7SJohn Levon void __or_cond_states(void);
7611f5207b7SJohn Levon void __save_pre_cond_states(void);
7621f5207b7SJohn Levon void __discard_pre_cond_states(void);
7631f5207b7SJohn Levon struct stree *__get_true_states(void);
7641f5207b7SJohn Levon struct stree *__get_false_states(void);
7651f5207b7SJohn Levon void __use_cond_states(void);
7661f5207b7SJohn Levon extern struct state_list *__last_base_slist;
7671f5207b7SJohn Levon 
7681f5207b7SJohn Levon void __push_continues(void);
7691f5207b7SJohn Levon void __discard_continues(void);
7701f5207b7SJohn Levon void __process_continues(void);
7711f5207b7SJohn Levon void __merge_continues(void);
7721f5207b7SJohn Levon 
7731f5207b7SJohn Levon void __push_breaks(void);
7741f5207b7SJohn Levon void __process_breaks(void);
7751f5207b7SJohn Levon int __has_breaks(void);
7761f5207b7SJohn Levon void __merge_breaks(void);
7771f5207b7SJohn Levon void __use_breaks(void);
7781f5207b7SJohn Levon 
7791f5207b7SJohn Levon void __save_switch_states(struct expression *switch_expr);
7801f5207b7SJohn Levon void __discard_switches(void);
7811f5207b7SJohn Levon int have_remaining_cases(void);
7821f5207b7SJohn Levon void __merge_switches(struct expression *switch_expr, struct range_list *case_rl);
7831f5207b7SJohn Levon void __push_default(void);
7841f5207b7SJohn Levon void __set_default(void);
7851f5207b7SJohn Levon int __pop_default(void);
7861f5207b7SJohn Levon 
7871f5207b7SJohn Levon void __push_conditions(void);
7881f5207b7SJohn Levon void __discard_conditions(void);
7891f5207b7SJohn Levon 
7901f5207b7SJohn Levon void __save_gotos(const char *name, struct symbol *sym);
7911f5207b7SJohn Levon void __merge_gotos(const char *name, struct symbol *sym);
7921f5207b7SJohn Levon 
7931f5207b7SJohn Levon void __print_cur_stree(void);
794*6523a3aaSJohn Levon bool __print_states(const char *owner);
795*6523a3aaSJohn Levon typedef void (check_tracker_hook)(int owner, const char *name, struct symbol *sym, struct smatch_state *state);
796*6523a3aaSJohn Levon void add_check_tracker(const char *check_name, check_tracker_hook *fn);
7971f5207b7SJohn Levon 
7981f5207b7SJohn Levon /* smatch_hooks.c */
7991f5207b7SJohn Levon void __pass_to_client(void *data, enum hook_type type);
8001f5207b7SJohn Levon void __pass_case_to_client(struct expression *switch_expr,
8011f5207b7SJohn Levon 			   struct range_list *rl);
8021f5207b7SJohn Levon int __has_merge_function(int client_id);
8031f5207b7SJohn Levon struct smatch_state *__client_merge_function(int owner,
8041f5207b7SJohn Levon 					     struct smatch_state *s1,
8051f5207b7SJohn Levon 					     struct smatch_state *s2);
8061f5207b7SJohn Levon struct smatch_state *__client_unmatched_state_function(struct sm_state *sm);
807c85f09ccSJohn Levon void call_pre_merge_hook(struct sm_state *cur, struct sm_state *other);
8081f5207b7SJohn Levon void __push_scope_hooks(void);
8091f5207b7SJohn Levon void __call_scope_hooks(void);
8101f5207b7SJohn Levon 
8111f5207b7SJohn Levon /* smatch_function_hooks.c */
8121f5207b7SJohn Levon void create_function_hook_hash(void);
8131f5207b7SJohn Levon void __match_initializer_call(struct symbol *sym);
8141f5207b7SJohn Levon 
8151f5207b7SJohn Levon /* smatch_db.c */
8161f5207b7SJohn Levon enum info_type {
8171f5207b7SJohn Levon 	INTERNAL	= 0,
8181f5207b7SJohn Levon 	/*
8191f5207b7SJohn Levon 	 * Changing these numbers is a pain.  Don't do it.  If you ever use a
8201f5207b7SJohn Levon 	 * number it can't be re-used right away so there may be gaps.
8211f5207b7SJohn Levon 	 * We select these in order by type so if the order matters, then give
8221f5207b7SJohn Levon 	 * it a number below 100-999,9000-9999 ranges. */
8231f5207b7SJohn Levon 
8241f5207b7SJohn Levon 	PARAM_CLEARED	= 101,
8251f5207b7SJohn Levon 	PARAM_LIMIT	= 103,
8261f5207b7SJohn Levon 	PARAM_FILTER	= 104,
8271f5207b7SJohn Levon 
8281f5207b7SJohn Levon 	PARAM_VALUE	= 1001,
8291f5207b7SJohn Levon 	BUF_SIZE	= 1002,
8301f5207b7SJohn Levon 	CAPPED_DATA	= 1004,
8311f5207b7SJohn Levon 	RETURN_VALUE	= 1005,
8321f5207b7SJohn Levon 	DEREFERENCE	= 1006,
8331f5207b7SJohn Levon 	RANGE_CAP	= 1007,
8341f5207b7SJohn Levon 	ABSOLUTE_LIMITS	= 1010,
8351f5207b7SJohn Levon 	PARAM_ADD	= 1012,
8361f5207b7SJohn Levon 	PARAM_FREED	= 1013,
8371f5207b7SJohn Levon 	DATA_SOURCE	= 1014,
8381f5207b7SJohn Levon 	FUZZY_MAX	= 1015,
839efe51d0cSJohn Levon 	HARD_MAX	= 2015,
8401f5207b7SJohn Levon 	STR_LEN		= 1016,
8411f5207b7SJohn Levon 	ARRAY_LEN	= 1017,
8421f5207b7SJohn Levon 	CAPABLE		= 1018,
8431f5207b7SJohn Levon 	NS_CAPABLE	= 1019,
8441f5207b7SJohn Levon 	CONTAINER	= 1020,
8451f5207b7SJohn Levon 	CASTED_CALL	= 1021,
8461f5207b7SJohn Levon 	TYPE_LINK	= 1022,
8471f5207b7SJohn Levon 	UNTRACKED_PARAM = 1023,
848efe51d0cSJohn Levon 	LOST_PARAM	= 2023,
8491f5207b7SJohn Levon 	CULL_PATH	= 1024,
8501f5207b7SJohn Levon 	PARAM_SET	= 1025,
8511f5207b7SJohn Levon 	PARAM_USED	= 1026,
8521f5207b7SJohn Levon 	BYTE_UNITS      = 1027,
8531f5207b7SJohn Levon 	COMPARE_LIMIT	= 1028,
8541f5207b7SJohn Levon 	PARAM_COMPARE	= 1029,
8551f5207b7SJohn Levon 	CONSTRAINT	= 1031,
8561f5207b7SJohn Levon 	PASSES_TYPE	= 1032,
8571f5207b7SJohn Levon 	CONSTRAINT_REQUIRED = 1033,
858efe51d0cSJohn Levon 	BIT_INFO	= 1034,
8591f5207b7SJohn Levon 	NOSPEC		= 1035,
8601f5207b7SJohn Levon 	NOSPEC_WB	= 1036,
8611f5207b7SJohn Levon 	STMT_CNT	= 1037,
8621f5207b7SJohn Levon 	TERMINATED	= 1038,
86331ad075eSJohn Levon 	FRESH_ALLOC	= 1044,
864*6523a3aaSJohn Levon 	ALLOCATOR	= 1045,
8651f5207b7SJohn Levon 
8661f5207b7SJohn Levon 	/* put random temporary stuff in the 7000-7999 range for testing */
867efe51d0cSJohn Levon 	USER_DATA	= 8017,
868efe51d0cSJohn Levon 	USER_DATA_SET	= 9017,
8691f5207b7SJohn Levon 	NO_OVERFLOW	= 8018,
8701f5207b7SJohn Levon 	NO_OVERFLOW_SIMPLE = 8019,
8711f5207b7SJohn Levon 	LOCKED		= 8020,
8721f5207b7SJohn Levon 	UNLOCKED	= 8021,
8735a0e240fSJohn Levon 	HALF_LOCKED	= 9022,
8745a0e240fSJohn Levon 	LOCK_RESTORED	= 9023,
8755a0e240fSJohn Levon 	KNOWN_LOCKED	= 9024,
8765a0e240fSJohn Levon 	KNOWN_UNLOCKED 	= 9025,
8771f5207b7SJohn Levon 	SET_FS		= 8022,
8781f5207b7SJohn Levon 	ATOMIC_INC	= 8023,
8791f5207b7SJohn Levon 	ATOMIC_DEC	= 8024,
880*6523a3aaSJohn Levon 	REFCOUNT	= 9025,
8811f5207b7SJohn Levon 	NO_SIDE_EFFECT  = 8025,
8821f5207b7SJohn Levon 	FN_ARG_LINK	= 8028,
8831f5207b7SJohn Levon 	DATA_VALUE	= 8029,
8841f5207b7SJohn Levon 	ARRAYSIZE_ARG	= 8033,
8851f5207b7SJohn Levon 	SIZEOF_ARG	= 8034,
8861f5207b7SJohn Levon 	MEMORY_TAG	= 8036,
8871f5207b7SJohn Levon 	MTAG_ASSIGN	= 8035,
8881f5207b7SJohn Levon 	STRING_VALUE	= 8041,
889efe51d0cSJohn Levon 
890efe51d0cSJohn Levon 	BYTE_COUNT	= 8050,
891efe51d0cSJohn Levon 	ELEM_COUNT	= 8051,
892efe51d0cSJohn Levon 	ELEM_LAST	= 8052,
893efe51d0cSJohn Levon 	USED_LAST	= 8053,
894efe51d0cSJohn Levon 	USED_COUNT	= 8054,
8951f5207b7SJohn Levon };
8961f5207b7SJohn Levon 
8971f5207b7SJohn Levon extern struct sqlite3 *smatch_db;
8981f5207b7SJohn Levon extern struct sqlite3 *mem_db;
8991f5207b7SJohn Levon extern struct sqlite3 *cache_db;
9001f5207b7SJohn Levon 
9011f5207b7SJohn Levon void db_ignore_states(int id);
9021f5207b7SJohn Levon void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type);
9031f5207b7SJohn Levon void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm));
904*6523a3aaSJohn Levon void add_caller_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm));
9051f5207b7SJohn Levon void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr));
9061f5207b7SJohn 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));
9071f5207b7SJohn Levon void select_call_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value));
9081f5207b7SJohn Levon void select_return_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value));
9091f5207b7SJohn Levon struct range_list *db_return_vals(struct expression *expr);
9101f5207b7SJohn Levon struct range_list *db_return_vals_from_str(const char *fn_name);
911c85f09ccSJohn Levon struct range_list *db_return_vals_no_args(struct expression *expr);
9121f5207b7SJohn Levon char *return_state_to_var_sym(struct expression *expr, int param, const char *key, struct symbol **sym);
9131f5207b7SJohn Levon char *get_chunk_from_key(struct expression *arg, char *key, struct symbol **sym, struct var_sym_list **vsl);
9141f5207b7SJohn Levon char *get_variable_from_key(struct expression *arg, const char *key, struct symbol **sym);
9151f5207b7SJohn Levon const char *state_name_to_param_name(const char *state_name, const char *param_name);
9161f5207b7SJohn Levon const char *get_param_name_var_sym(const char *name, struct symbol *sym);
9171f5207b7SJohn Levon const char *get_param_name(struct sm_state *sm);
9181f5207b7SJohn Levon const char *get_mtag_name_var_sym(const char *state_name, struct symbol *sym);
9191f5207b7SJohn Levon const char *get_mtag_name_expr(struct expression *expr);
9201f5207b7SJohn Levon char *get_data_info_name(struct expression *expr);
9215a0e240fSJohn Levon char *sm_to_arg_name(struct expression *expr, struct sm_state *sm);
922efe51d0cSJohn Levon int is_recursive_member(const char *param_name);
9231f5207b7SJohn Levon 
9241f5207b7SJohn Levon char *escape_newlines(const char *str);
9251f5207b7SJohn Levon void sql_exec(struct sqlite3 *db, int (*callback)(void*, int, char**, char**), void *data, const char *sql);
9261f5207b7SJohn Levon 
9271f5207b7SJohn Levon #define sql_helper(db, call_back, data, sql...)					\
9281f5207b7SJohn Levon do {										\
9291f5207b7SJohn Levon 	char sql_txt[1024];							\
9301f5207b7SJohn Levon 										\
9311f5207b7SJohn Levon 	sqlite3_snprintf(sizeof(sql_txt), sql_txt, sql);			\
9325a0e240fSJohn Levon 	db_debug("debug: %s\n", sql_txt);					\
9331f5207b7SJohn Levon 	sql_exec(db, call_back, data, sql_txt);					\
9341f5207b7SJohn Levon } while (0)
9351f5207b7SJohn Levon 
9361f5207b7SJohn Levon 
9371f5207b7SJohn Levon #define run_sql(call_back, data, sql...)					\
9381f5207b7SJohn Levon do {										\
9391f5207b7SJohn Levon 	if (option_no_db)							\
9401f5207b7SJohn Levon 		break;								\
9411f5207b7SJohn Levon 	sql_helper(smatch_db, call_back, data, sql);				\
9421f5207b7SJohn Levon } while (0)
9431f5207b7SJohn Levon 
9441f5207b7SJohn Levon #define mem_sql(call_back, data, sql...)					\
9451f5207b7SJohn Levon 	sql_helper(mem_db, call_back, data, sql)
9461f5207b7SJohn Levon 
9471f5207b7SJohn Levon #define cache_sql(call_back, data, sql...)					\
9481f5207b7SJohn Levon 	sql_helper(cache_db, call_back, data, sql)
9491f5207b7SJohn Levon 
9501f5207b7SJohn Levon #define sql_insert_helper(table, db, ignore, late, values...)			\
9511f5207b7SJohn Levon do {										\
9521f5207b7SJohn Levon 	struct sqlite3 *_db = db;						\
9531f5207b7SJohn Levon 										\
9541f5207b7SJohn Levon 	if (__inline_fn && !_db)						\
9551f5207b7SJohn Levon 		_db = mem_db;							\
9561f5207b7SJohn Levon 	if (_db) {								\
9571f5207b7SJohn Levon 		char buf[1024];							\
9581f5207b7SJohn Levon 		char *err, *p = buf;						\
9591f5207b7SJohn Levon 		int rc;								\
9601f5207b7SJohn Levon 										\
9611f5207b7SJohn Levon 		p += snprintf(p, buf + sizeof(buf) - p,				\
9621f5207b7SJohn Levon 			      "insert %sinto %s values (",			\
9631f5207b7SJohn Levon 			      ignore ? "or ignore " : "", #table);		\
9641f5207b7SJohn Levon 		p += snprintf(p, buf + sizeof(buf) - p, values);		\
9651f5207b7SJohn Levon 		p += snprintf(p, buf + sizeof(buf) - p, ");");			\
9665a0e240fSJohn Levon 		db_debug("mem-db: %s\n", buf);					\
9671f5207b7SJohn Levon 		rc = sqlite3_exec(_db, buf, NULL, NULL, &err);			\
9681f5207b7SJohn Levon 		if (rc != SQLITE_OK) {						\
9691f5207b7SJohn Levon 			sm_ierror("SQL error #2: %s", err);			\
9701f5207b7SJohn Levon 			sm_ierror("SQL: '%s'", buf);				\
9711f5207b7SJohn Levon 			parse_error = 1;					\
9721f5207b7SJohn Levon 		}								\
9731f5207b7SJohn Levon 		break;								\
9741f5207b7SJohn Levon 	}									\
9751f5207b7SJohn Levon 	if (option_info) {							\
9761f5207b7SJohn Levon 		FILE *tmp_fd = sm_outfd;					\
9771f5207b7SJohn Levon 		sm_outfd = sql_outfd;						\
9781f5207b7SJohn Levon 		sm_prefix();							\
9791f5207b7SJohn Levon 	        sm_printf("SQL%s: insert %sinto " #table " values(",		\
9801f5207b7SJohn Levon 			  late ? "_late" : "", ignore ? "or ignore " : "");	\
9811f5207b7SJohn Levon 	        sm_printf(values);						\
9821f5207b7SJohn Levon 	        sm_printf(");\n");						\
9831f5207b7SJohn Levon 		sm_outfd = tmp_fd;						\
9841f5207b7SJohn Levon 	}									\
9851f5207b7SJohn Levon } while (0)
9861f5207b7SJohn Levon 
9871f5207b7SJohn Levon #define sql_insert(table, values...) sql_insert_helper(table, 0, 0, 0, values);
9881f5207b7SJohn Levon #define sql_insert_or_ignore(table, values...) sql_insert_helper(table, 0, 1, 0, values);
9891f5207b7SJohn Levon #define sql_insert_late(table, values...) sql_insert_helper(table, 0, 0, 1, values);
9901f5207b7SJohn Levon #define sql_insert_cache(table, values...) sql_insert_helper(table, cache_db, 1, 0, values);
9911f5207b7SJohn Levon 
9921f5207b7SJohn Levon char *get_static_filter(struct symbol *sym);
9931f5207b7SJohn Levon 
9941f5207b7SJohn Levon void sql_insert_return_states(int return_id, const char *return_ranges,
9951f5207b7SJohn Levon 		int type, int param, const char *key, const char *value);
9961f5207b7SJohn Levon void sql_insert_caller_info(struct expression *call, int type, int param,
9971f5207b7SJohn Levon 		const char *key, const char *value);
9981f5207b7SJohn Levon void sql_insert_function_ptr(const char *fn, const char *struct_name);
9991f5207b7SJohn Levon void sql_insert_return_values(const char *return_values);
10001f5207b7SJohn Levon void sql_insert_return_implies(int type, int param, const char *key, const char *value);
10011f5207b7SJohn Levon void sql_insert_function_type_size(const char *member, const char *ranges);
10021f5207b7SJohn Levon void sql_insert_function_type_info(int type, const char *struct_type, const char *member, const char *value);
10031f5207b7SJohn Levon void sql_insert_type_info(int type, const char *member, const char *value);
10041f5207b7SJohn Levon void sql_insert_local_values(const char *name, const char *value);
10051f5207b7SJohn Levon void sql_insert_function_type_value(const char *type, const char *value);
10061f5207b7SJohn Levon void sql_insert_function_type(int param, const char *value);
10071f5207b7SJohn Levon void sql_insert_parameter_name(int param, const char *value);
10081f5207b7SJohn Levon void sql_insert_data_info(struct expression *data, int type, const char *value);
10091f5207b7SJohn Levon void sql_insert_data_info_var_sym(const char *var, struct symbol *sym, int type, const char *value);
10101f5207b7SJohn Levon void sql_save_constraint(const char *con);
10111f5207b7SJohn Levon void sql_save_constraint_required(const char *data, int op, const char *limit);
10121f5207b7SJohn Levon void sql_copy_constraint_required(const char *new_limit, const char *old_limit);
10131f5207b7SJohn Levon void sql_insert_fn_ptr_data_link(const char *ptr, const char *data);
10141f5207b7SJohn Levon void sql_insert_fn_data_link(struct expression *fn, int type, int param, const char *key, const char *value);
10151f5207b7SJohn Levon void sql_insert_mtag_about(mtag_t tag, const char *left_name, const char *right_name);
1016*6523a3aaSJohn Levon void sql_insert_mtag_info(mtag_t tag, int type, const char *value);
1017*6523a3aaSJohn Levon void sql_insert_mtag_map(mtag_t container, int container_offset, mtag_t tag, int tag_offset);
10181f5207b7SJohn Levon void sql_insert_mtag_alias(mtag_t orig, mtag_t alias);
1019*6523a3aaSJohn Levon int mtag_map_select_container(mtag_t tag, int container_offset, mtag_t *container);
10201f5207b7SJohn Levon int mtag_map_select_tag(mtag_t container, int offset, mtag_t *tag);
1021*6523a3aaSJohn Levon struct smatch_state *get_mtag_return(struct expression *expr, struct smatch_state *state);
1022efe51d0cSJohn Levon struct range_list *swap_mtag_seed(struct expression *expr, struct range_list *rl);
10231f5207b7SJohn Levon 
10241f5207b7SJohn Levon void sql_select_return_states(const char *cols, struct expression *call,
10251f5207b7SJohn Levon 	int (*callback)(void*, int, char**, char**), void *info);
10261f5207b7SJohn Levon void sql_select_call_implies(const char *cols, struct expression *call,
10271f5207b7SJohn Levon 	int (*callback)(void*, int, char**, char**));
10281f5207b7SJohn Levon 
10291f5207b7SJohn Levon void open_smatch_db(char *db_file);
10301f5207b7SJohn Levon 
10311f5207b7SJohn Levon /* smatch_files.c */
10321f5207b7SJohn Levon int open_data_file(const char *filename);
10331f5207b7SJohn Levon int open_schema_file(const char *schema);
10341f5207b7SJohn Levon struct token *get_tokens_file(const char *filename);
10351f5207b7SJohn Levon 
10361f5207b7SJohn Levon /* smatch.c */
10371f5207b7SJohn Levon extern char *option_debug_check;
10381f5207b7SJohn Levon extern char *option_project_str;
10391f5207b7SJohn Levon extern char *bin_dir;
10401f5207b7SJohn Levon extern char *data_dir;
10411f5207b7SJohn Levon extern int option_no_data;
10421f5207b7SJohn Levon extern int option_full_path;
10431f5207b7SJohn Levon extern int option_call_tree;
10441f5207b7SJohn Levon extern int num_checks;
10451f5207b7SJohn Levon 
10461f5207b7SJohn Levon enum project_type {
10471f5207b7SJohn Levon 	PROJ_NONE,
10481f5207b7SJohn Levon 	PROJ_KERNEL,
10491f5207b7SJohn Levon 	PROJ_WINE,
10501f5207b7SJohn Levon 	PROJ_ILLUMOS_KERNEL,
10511f5207b7SJohn Levon 	PROJ_ILLUMOS_USER,
10521f5207b7SJohn Levon 	PROJ_UNKNOWN,
10531f5207b7SJohn Levon };
10541f5207b7SJohn Levon extern enum project_type option_project;
10551f5207b7SJohn Levon const char *check_name(unsigned short id);
10561f5207b7SJohn Levon int id_from_name(const char *name);
10571f5207b7SJohn Levon 
10581f5207b7SJohn Levon 
10591f5207b7SJohn Levon /* smatch_buf_size.c */
10601f5207b7SJohn Levon int get_array_size(struct expression *expr);
10611f5207b7SJohn Levon int get_array_size_bytes(struct expression *expr);
10621f5207b7SJohn Levon int get_array_size_bytes_min(struct expression *expr);
10631f5207b7SJohn Levon int get_array_size_bytes_max(struct expression *expr);
10641f5207b7SJohn Levon struct range_list *get_array_size_bytes_rl(struct expression *expr);
10651f5207b7SJohn Levon int get_real_array_size(struct expression *expr);
10661f5207b7SJohn Levon int last_member_is_resizable(struct symbol *type);
10671f5207b7SJohn Levon /* smatch_strlen.c */
10681f5207b7SJohn Levon int get_implied_strlen(struct expression *expr, struct range_list **rl);
10691f5207b7SJohn Levon int get_size_from_strlen(struct expression *expr);
10701f5207b7SJohn Levon 
10711f5207b7SJohn Levon /* smatch_capped.c */
10721f5207b7SJohn Levon int is_capped(struct expression *expr);
10731f5207b7SJohn Levon int is_capped_var_sym(const char *name, struct symbol *sym);
10741f5207b7SJohn Levon 
10751f5207b7SJohn Levon /* check_user_data.c */
10761f5207b7SJohn Levon int is_user_macro(struct expression *expr);
10771f5207b7SJohn Levon int is_capped_user_data(struct expression *expr);
10781f5207b7SJohn Levon int implied_user_data(struct expression *expr, struct range_list **rl);
10791f5207b7SJohn Levon struct stree *get_user_stree(void);
10801f5207b7SJohn Levon int get_user_rl(struct expression *expr, struct range_list **rl);
10811f5207b7SJohn Levon int is_user_rl(struct expression *expr);
10821f5207b7SJohn Levon int get_user_rl_var_sym(const char *name, struct symbol *sym, struct range_list **rl);
1083efe51d0cSJohn Levon bool user_rl_capped(struct expression *expr);
1084efe51d0cSJohn Levon struct range_list *var_user_rl(struct expression *expr);
10851f5207b7SJohn Levon 
10861f5207b7SJohn Levon /* check_locking.c */
10871f5207b7SJohn Levon void print_held_locks();
10881f5207b7SJohn Levon 
10891f5207b7SJohn Levon /* check_assigned_expr.c */
1090*6523a3aaSJohn Levon extern int check_assigned_expr_id;
10911f5207b7SJohn Levon struct expression *get_assigned_expr(struct expression *expr);
10921f5207b7SJohn Levon struct expression *get_assigned_expr_name_sym(const char *name, struct symbol *sym);
10931f5207b7SJohn Levon /* smatch_return_to_param.c */
10941f5207b7SJohn Levon void __add_return_to_param_mapping(struct expression *assign, const char *return_string);
10951f5207b7SJohn Levon char *map_call_to_param_name_sym(struct expression *expr, struct symbol **sym);
10961f5207b7SJohn Levon 
10971f5207b7SJohn Levon /* smatch_comparison.c */
10985a0e240fSJohn Levon extern int comparison_id;
1099c85f09ccSJohn Levon #define UNKNOWN_COMPARISON 0
1100c85f09ccSJohn Levon #define IMPOSSIBLE_COMPARISON -1
11011f5207b7SJohn Levon struct compare_data {
11021f5207b7SJohn Levon 	/* The ->left and ->right expression pointers might be NULL (I'm lazy) */
11031f5207b7SJohn Levon 	struct expression *left;
11041f5207b7SJohn Levon 	const char *left_var;
11051f5207b7SJohn Levon 	struct var_sym_list *left_vsl;
11061f5207b7SJohn Levon 	int comparison;
11071f5207b7SJohn Levon 	struct expression *right;
11081f5207b7SJohn Levon 	const char *right_var;
11091f5207b7SJohn Levon 	struct var_sym_list *right_vsl;
11101f5207b7SJohn Levon };
11111f5207b7SJohn Levon DECLARE_ALLOCATOR(compare_data);
11121f5207b7SJohn Levon struct smatch_state *alloc_compare_state(
11131f5207b7SJohn Levon 		struct expression *left,
11141f5207b7SJohn Levon 		const char *left_var, struct var_sym_list *left_vsl,
11151f5207b7SJohn Levon 		int comparison,
11161f5207b7SJohn Levon 		struct expression *right,
11171f5207b7SJohn Levon 		const char *right_var, struct var_sym_list *right_vsl);
1118c85f09ccSJohn Levon int comparison_intersection(int orig, int op);
11191f5207b7SJohn Levon int merge_comparisons(int one, int two);
11201f5207b7SJohn Levon int combine_comparisons(int left_compare, int right_compare);
11211f5207b7SJohn Levon int state_to_comparison(struct smatch_state *state);
11221f5207b7SJohn Levon struct smatch_state *merge_compare_states(struct smatch_state *s1, struct smatch_state *s2);
11231f5207b7SJohn Levon int get_comparison(struct expression *left, struct expression *right);
1124efe51d0cSJohn Levon int get_comparison_no_extra(struct expression *a, struct expression *b);
11251f5207b7SJohn Levon int get_comparison_strings(const char *one, const char *two);
11261f5207b7SJohn Levon int possible_comparison(struct expression *a, int comparison, struct expression *b);
11271f5207b7SJohn Levon struct state_list *get_all_comparisons(struct expression *expr);
11281f5207b7SJohn Levon struct state_list *get_all_possible_equal_comparisons(struct expression *expr);
11291f5207b7SJohn Levon void __add_return_comparison(struct expression *call, const char *range);
11301f5207b7SJohn Levon void __add_comparison_info(struct expression *expr, struct expression *call, const char *range);
11311f5207b7SJohn Levon char *get_printed_param_name(struct expression *call, const char *param_name, struct symbol *param_sym);
11321f5207b7SJohn Levon char *name_sym_to_param_comparison(const char *name, struct symbol *sym);
11331f5207b7SJohn Levon char *expr_equal_to_param(struct expression *expr, int ignore);
11341f5207b7SJohn Levon char *expr_lte_to_param(struct expression *expr, int ignore);
11351f5207b7SJohn Levon char *expr_param_comparison(struct expression *expr, int ignore);
11361f5207b7SJohn Levon int flip_comparison(int op);
11371f5207b7SJohn Levon int negate_comparison(int op);
11381f5207b7SJohn Levon int remove_unsigned_from_comparison(int op);
11391f5207b7SJohn Levon int param_compare_limit_is_impossible(struct expression *expr, int left_param, char *left_key, char *value);
11401f5207b7SJohn Levon void filter_by_comparison(struct range_list **rl, int comparison, struct range_list *right);
11411f5207b7SJohn Levon void __compare_param_limit_hook(struct expression *left_expr, struct expression *right_expr,
11421f5207b7SJohn Levon 				const char *state_name,
11431f5207b7SJohn Levon 				struct smatch_state *true_state, struct smatch_state *false_state);
11441f5207b7SJohn Levon int impossibly_high_comparison(struct expression *expr);
11451f5207b7SJohn Levon 
11461f5207b7SJohn Levon /* smatch_sval.c */
11471f5207b7SJohn Levon sval_t *sval_alloc(sval_t sval);
11481f5207b7SJohn Levon sval_t *sval_alloc_permanent(sval_t sval);
11491f5207b7SJohn Levon sval_t sval_blank(struct expression *expr);
11501f5207b7SJohn Levon sval_t sval_type_val(struct symbol *type, long long val);
11515a0e240fSJohn Levon sval_t sval_type_fval(struct symbol *type, long double fval);
11521f5207b7SJohn Levon sval_t sval_from_val(struct expression *expr, long long val);
11535a0e240fSJohn Levon sval_t sval_from_fval(struct expression *expr, long double fval);
11541f5207b7SJohn Levon int sval_is_ptr(sval_t sval);
11555a0e240fSJohn Levon bool sval_is_fp(sval_t sval);
11561f5207b7SJohn Levon int sval_unsigned(sval_t sval);
11571f5207b7SJohn Levon int sval_signed(sval_t sval);
11581f5207b7SJohn Levon int sval_bits(sval_t sval);
11591f5207b7SJohn Levon int sval_bits_used(sval_t sval);
11601f5207b7SJohn Levon int sval_is_negative(sval_t sval);
11611f5207b7SJohn Levon int sval_is_positive(sval_t sval);
11621f5207b7SJohn Levon int sval_is_min(sval_t sval);
11631f5207b7SJohn Levon int sval_is_max(sval_t sval);
11641f5207b7SJohn Levon int sval_is_a_min(sval_t sval);
11651f5207b7SJohn Levon int sval_is_a_max(sval_t sval);
11661f5207b7SJohn Levon int sval_is_negative_min(sval_t sval);
11671f5207b7SJohn Levon int sval_cmp_t(struct symbol *type, sval_t one, sval_t two);
11681f5207b7SJohn Levon int sval_cmp_val(sval_t one, long long val);
11691f5207b7SJohn Levon sval_t sval_min(sval_t one, sval_t two);
11701f5207b7SJohn Levon sval_t sval_max(sval_t one, sval_t two);
11711f5207b7SJohn Levon int sval_too_low(struct symbol *type, sval_t sval);
11721f5207b7SJohn Levon int sval_too_high(struct symbol *type, sval_t sval);
11731f5207b7SJohn Levon int sval_fits(struct symbol *type, sval_t sval);
11741f5207b7SJohn Levon sval_t sval_cast(struct symbol *type, sval_t sval);
11751f5207b7SJohn Levon sval_t sval_preop(sval_t sval, int op);
11761f5207b7SJohn Levon sval_t sval_binop(sval_t left, int op, sval_t right);
11771f5207b7SJohn Levon int sval_binop_overflows(sval_t left, int op, sval_t right);
11781f5207b7SJohn Levon int sval_binop_overflows_no_sign(sval_t left, int op, sval_t right);
1179efe51d0cSJohn Levon int find_first_zero_bit(unsigned long long uvalue);
1180efe51d0cSJohn Levon int sm_fls64(unsigned long long uvalue);
11811f5207b7SJohn Levon unsigned long long fls_mask(unsigned long long uvalue);
11821f5207b7SJohn Levon unsigned long long sval_fls_mask(sval_t sval);
11831f5207b7SJohn Levon const char *sval_to_str(sval_t sval);
1184efe51d0cSJohn Levon const char *sval_to_str_or_err_ptr(sval_t sval);
11851f5207b7SJohn Levon const char *sval_to_numstr(sval_t sval);
11861f5207b7SJohn Levon sval_t ll_to_sval(long long val);
11871f5207b7SJohn Levon 
11881f5207b7SJohn Levon /* smatch_string_list.c */
11891f5207b7SJohn Levon int list_has_string(struct string_list *str_list, const char *str);
1190efe51d0cSJohn Levon int insert_string(struct string_list **str_list, const char *str);
11911f5207b7SJohn Levon struct string_list *clone_str_list(struct string_list *orig);
11921f5207b7SJohn Levon struct string_list *combine_string_lists(struct string_list *one, struct string_list *two);
11931f5207b7SJohn Levon 
11941f5207b7SJohn Levon /* smatch_start_states.c */
11951f5207b7SJohn Levon struct stree *get_start_states(void);
11961f5207b7SJohn Levon 
11971f5207b7SJohn Levon /* smatch_recurse.c */
11981f5207b7SJohn Levon int has_symbol(struct expression *expr, struct symbol *sym);
11991f5207b7SJohn Levon int has_variable(struct expression *expr, struct expression *var);
12001f5207b7SJohn Levon int has_inc_dec(struct expression *expr);
12011f5207b7SJohn Levon 
12021f5207b7SJohn Levon /* smatch_stored_conditions.c */
12031f5207b7SJohn Levon struct smatch_state *get_stored_condition(struct expression *expr);
12041f5207b7SJohn Levon struct expression_list *get_conditions(struct expression *expr);
12051f5207b7SJohn Levon struct sm_state *stored_condition_implication_hook(struct expression *expr,
12061f5207b7SJohn Levon 			struct state_list **true_stack,
12071f5207b7SJohn Levon 			struct state_list **false_stack);
12085a0e240fSJohn Levon /* smatch_parsed_conditions.c */
12095a0e240fSJohn Levon struct sm_state *parsed_condition_implication_hook(struct expression *expr,
12105a0e240fSJohn Levon 			struct state_list **true_stack,
12115a0e240fSJohn Levon 			struct state_list **false_stack);
1212*6523a3aaSJohn Levon /* smatch_comparison.c */
1213*6523a3aaSJohn Levon struct sm_state *comparison_implication_hook(struct expression *expr,
1214*6523a3aaSJohn Levon 					     struct state_list **true_stack,
1215*6523a3aaSJohn Levon 					     struct state_list **false_stack);
12161f5207b7SJohn Levon 
12171f5207b7SJohn Levon /* check_string_len.c */
12181f5207b7SJohn Levon int get_formatted_string_size(struct expression *call, int arg);
1219efe51d0cSJohn Levon int get_formatted_string_min_size(struct expression *call, int arg);
12201f5207b7SJohn Levon 
12211f5207b7SJohn Levon /* smatch_param_set.c */
12221f5207b7SJohn Levon int param_was_set(struct expression *expr);
12231f5207b7SJohn Levon int param_was_set_var_sym(const char *name, struct symbol *sym);
1224c85f09ccSJohn Levon void print_limited_param_set(int return_id, char *return_ranges, struct expression *expr);
12251f5207b7SJohn Levon /* smatch_param_filter.c */
12261f5207b7SJohn Levon int param_has_filter_data(struct sm_state *sm);
12271f5207b7SJohn Levon 
12281f5207b7SJohn Levon /* smatch_links.c */
12291f5207b7SJohn Levon void set_up_link_functions(int id, int linkid);
12301f5207b7SJohn Levon struct smatch_state *merge_link_states(struct smatch_state *s1, struct smatch_state *s2);
12311f5207b7SJohn Levon void store_link(int link_id, const char *name, struct symbol *sym, const char *link_name, struct symbol *link_sym);
12321f5207b7SJohn Levon 
12331f5207b7SJohn Levon /* check_buf_comparison */
1234efe51d0cSJohn Levon const char *limit_type_str(unsigned int limit_type);
1235efe51d0cSJohn Levon struct expression *get_size_variable(struct expression *buf, int *limit_type);
12361f5207b7SJohn Levon struct expression *get_array_variable(struct expression *size);
1237efe51d0cSJohn Levon int buf_comparison_index_ok(struct expression *expr);
12381f5207b7SJohn Levon 
12391f5207b7SJohn Levon /* smatch_untracked_param.c */
12401f5207b7SJohn Levon void mark_untracked(struct expression *expr, int param, const char *key, const char *value);
12411f5207b7SJohn Levon void add_untracked_param_hook(void (func)(struct expression *call, int param));
1242efe51d0cSJohn Levon void add_lost_param_hook(void (func)(struct expression *call, int param));
12431f5207b7SJohn Levon void mark_all_params_untracked(int return_id, char *return_ranges, struct expression *expr);
12441f5207b7SJohn Levon 
12451f5207b7SJohn Levon /* smatch_strings.c */
12461f5207b7SJohn Levon struct state_list *get_strings(struct expression *expr);
12471f5207b7SJohn Levon struct expression *fake_string_from_mtag(mtag_t tag);
12481f5207b7SJohn Levon 
12491f5207b7SJohn Levon /* smatch_estate.c */
12501f5207b7SJohn Levon int estate_get_single_value(struct smatch_state *state, sval_t *sval);
12511f5207b7SJohn Levon 
12521f5207b7SJohn Levon /* smatch_address.c */
12531f5207b7SJohn Levon int get_address_rl(struct expression *expr, struct range_list **rl);
12541f5207b7SJohn Levon int get_member_offset(struct symbol *type, const char *member_name);
12551f5207b7SJohn Levon int get_member_offset_from_deref(struct expression *expr);
12561f5207b7SJohn Levon 
12571f5207b7SJohn Levon /* for now this is in smatch_used_parameter.c */
12581f5207b7SJohn Levon void __get_state_hook(int owner, const char *name, struct symbol *sym);
12591f5207b7SJohn Levon 
12601f5207b7SJohn Levon /* smatch_buf_comparison.c */
12611f5207b7SJohn Levon int db_var_is_array_limit(struct expression *array, const char *name, struct var_sym_list *vsl);
12621f5207b7SJohn Levon 
1263*6523a3aaSJohn Levon struct range_list *get_fs(void);
1264*6523a3aaSJohn Levon 
12651f5207b7SJohn Levon struct stree *get_all_return_states(void);
12661f5207b7SJohn Levon struct stree_stack *get_all_return_strees(void);
12671f5207b7SJohn Levon int on_atomic_dec_path(void);
12681f5207b7SJohn Levon int was_inced(const char *name, struct symbol *sym);
1269*6523a3aaSJohn Levon void set_refcount_inc(char *name, struct symbol *sym);
1270*6523a3aaSJohn Levon void set_refcount_dec(char *name, struct symbol *sym);
12711f5207b7SJohn Levon 
12721f5207b7SJohn Levon /* smatch_constraints.c */
12731f5207b7SJohn Levon char *get_constraint_str(struct expression *expr);
12741f5207b7SJohn Levon struct constraint_list *get_constraints(struct expression *expr);
12751f5207b7SJohn Levon char *unmet_constraint(struct expression *data, struct expression *offset);
12761f5207b7SJohn Levon char *get_required_constraint(const char *data_str);
12771f5207b7SJohn Levon 
12781f5207b7SJohn Levon /* smatch_container_of.c */
12791f5207b7SJohn Levon int get_param_from_container_of(struct expression *expr);
12801f5207b7SJohn Levon int get_offset_from_container_of(struct expression *expr);
1281efe51d0cSJohn Levon char *get_container_name(struct expression *container, struct expression *expr);
12821f5207b7SJohn Levon 
12831f5207b7SJohn Levon /* smatch_mtag.c */
128431ad075eSJohn Levon mtag_t str_to_mtag(const char *str);
12851f5207b7SJohn Levon int get_string_mtag(struct expression *expr, mtag_t *tag);
12861f5207b7SJohn Levon int get_toplevel_mtag(struct symbol *sym, mtag_t *tag);
12871f5207b7SJohn Levon int create_mtag_alias(mtag_t tag, struct expression *expr, mtag_t *new);
12881f5207b7SJohn Levon int expr_to_mtag_offset(struct expression *expr, mtag_t *tag, int *offset);
1289c85f09ccSJohn Levon void update_mtag_data(struct expression *expr, struct smatch_state *state);
12901f5207b7SJohn Levon int get_mtag_sval(struct expression *expr, sval_t *sval);
12911f5207b7SJohn Levon 
12921f5207b7SJohn Levon /* Trinity fuzzer stuff */
12931f5207b7SJohn Levon const char *get_syscall_arg_type(struct symbol *sym);
12941f5207b7SJohn Levon 
1295efe51d0cSJohn Levon /* smatch_bit_info.c */
1296c85f09ccSJohn Levon struct bit_info *rl_to_binfo(struct range_list *rl);
1297efe51d0cSJohn Levon struct bit_info *get_bit_info(struct expression *expr);
1298efe51d0cSJohn Levon struct bit_info *get_bit_info_var_sym(const char *name, struct symbol *sym);
12991f5207b7SJohn Levon /* smatch_mem_tracker.c */
13001f5207b7SJohn Levon extern int option_mem;
1301efe51d0cSJohn Levon unsigned long get_mem_kb(void);
13021f5207b7SJohn Levon unsigned long get_max_memory(void);
13031f5207b7SJohn Levon 
13041f5207b7SJohn Levon /* check_is_nospec.c */
13051f5207b7SJohn Levon bool is_nospec(struct expression *expr);
1306efe51d0cSJohn Levon long get_stmt_cnt(void);
13071f5207b7SJohn Levon 
13081f5207b7SJohn Levon /* smatch_nul_terminator.c */
1309c85f09ccSJohn Levon bool is_nul_terminated_var_sym(const char *name, struct symbol *sym);
13101f5207b7SJohn Levon bool is_nul_terminated(struct expression *expr);
1311efe51d0cSJohn Levon /* check_kernel.c  */
1312efe51d0cSJohn Levon bool is_ignored_kernel_data(const char *name);
1313efe51d0cSJohn Levon 
131431ad075eSJohn Levon bool is_fresh_alloc_var_sym(const char *var, struct symbol *sym);
131531ad075eSJohn Levon bool is_fresh_alloc(struct expression *expr);
type_is_ptr(struct symbol * type)1316efe51d0cSJohn Levon static inline bool type_is_ptr(struct symbol *type)
1317efe51d0cSJohn Levon {
1318efe51d0cSJohn Levon 	return type &&
1319efe51d0cSJohn Levon 	       (type->type == SYM_PTR ||
1320efe51d0cSJohn Levon 		type->type == SYM_ARRAY ||
1321efe51d0cSJohn Levon 		type->type == SYM_FN);
1322efe51d0cSJohn Levon }
13231f5207b7SJohn Levon 
type_is_fp(struct symbol * type)13245a0e240fSJohn Levon static inline bool type_is_fp(struct symbol *type)
13255a0e240fSJohn Levon {
13265a0e240fSJohn Levon 	return type &&
13275a0e240fSJohn Levon 	       (type == &float_ctype ||
13285a0e240fSJohn Levon 		type == &double_ctype ||
13295a0e240fSJohn Levon 		type == &ldouble_ctype);
13305a0e240fSJohn Levon }
13315a0e240fSJohn Levon 
type_bits(struct symbol * type)13321f5207b7SJohn Levon static inline int type_bits(struct symbol *type)
13331f5207b7SJohn Levon {
13341f5207b7SJohn Levon 	if (!type)
13351f5207b7SJohn Levon 		return 0;
1336efe51d0cSJohn Levon 	if (type_is_ptr(type))
13371f5207b7SJohn Levon 		return bits_in_pointer;
13381f5207b7SJohn Levon 	if (!type->examined)
13391f5207b7SJohn Levon 		examine_symbol_type(type);
13401f5207b7SJohn Levon 	return type->bit_size;
13411f5207b7SJohn Levon }
13421f5207b7SJohn Levon 
type_unsigned(struct symbol * base_type)13431f5207b7SJohn Levon static inline int type_unsigned(struct symbol *base_type)
13441f5207b7SJohn Levon {
13451f5207b7SJohn Levon 	if (!base_type)
13461f5207b7SJohn Levon 		return 0;
1347efe51d0cSJohn Levon 	if (is_ptr_type(base_type))
1348efe51d0cSJohn Levon 		return 1;
13491f5207b7SJohn Levon 	if (base_type->ctype.modifiers & MOD_UNSIGNED)
13501f5207b7SJohn Levon 		return 1;
13511f5207b7SJohn Levon 	return 0;
13521f5207b7SJohn Levon }
13531f5207b7SJohn Levon 
type_positive_bits(struct symbol * type)13541f5207b7SJohn Levon static inline int type_positive_bits(struct symbol *type)
13551f5207b7SJohn Levon {
13561f5207b7SJohn Levon 	if (!type)
13571f5207b7SJohn Levon 		return 0;
1358efe51d0cSJohn Levon 	if (is_ptr_type(type))
1359efe51d0cSJohn Levon 		return bits_in_pointer;
13601f5207b7SJohn Levon 	if (type_unsigned(type))
13611f5207b7SJohn Levon 		return type_bits(type);
13621f5207b7SJohn Levon 	return type_bits(type) - 1;
13631f5207b7SJohn Levon }
13641f5207b7SJohn Levon 
sval_positive_bits(sval_t sval)13651f5207b7SJohn Levon static inline int sval_positive_bits(sval_t sval)
13661f5207b7SJohn Levon {
13671f5207b7SJohn Levon 	return type_positive_bits(sval.type);
13681f5207b7SJohn Levon }
13691f5207b7SJohn Levon 
13701f5207b7SJohn Levon /*
13711f5207b7SJohn Levon  * Returns -1 if one is smaller, 0 if they are the same and 1 if two is larger.
13721f5207b7SJohn Levon  */
13735a0e240fSJohn Levon 
fp_cmp(sval_t one,sval_t two)13745a0e240fSJohn Levon static inline int fp_cmp(sval_t one, sval_t two)
13755a0e240fSJohn Levon {
13765a0e240fSJohn Levon 	struct symbol *type;
13775a0e240fSJohn Levon 
13785a0e240fSJohn Levon 	if (sval_is_fp(one) && sval_is_fp(two))
13795a0e240fSJohn Levon 		type = type_bits(one.type) > type_bits(two.type) ? one.type : two.type;
13805a0e240fSJohn Levon 	else if (sval_is_fp(one))
13815a0e240fSJohn Levon 		type = one.type;
13825a0e240fSJohn Levon 	else
13835a0e240fSJohn Levon 		type = two.type;
13845a0e240fSJohn Levon 
13855a0e240fSJohn Levon 	one = sval_cast(type, one);
13865a0e240fSJohn Levon 	two = sval_cast(type, two);
13875a0e240fSJohn Levon 
13885a0e240fSJohn Levon 	if (one.type == &float_ctype) {
13895a0e240fSJohn Levon 		if (one.fvalue < two.fvalue)
13905a0e240fSJohn Levon 			return -1;
13915a0e240fSJohn Levon 		if (one.fvalue == two.fvalue)
13925a0e240fSJohn Levon 			return 0;
13935a0e240fSJohn Levon 		return 1;
13945a0e240fSJohn Levon 	}
13955a0e240fSJohn Levon 	if (one.type == &double_ctype) {
13965a0e240fSJohn Levon 		if (one.dvalue < two.dvalue)
13975a0e240fSJohn Levon 			return -1;
13985a0e240fSJohn Levon 		if (one.dvalue == two.dvalue)
13995a0e240fSJohn Levon 			return 0;
14005a0e240fSJohn Levon 		return 1;
14015a0e240fSJohn Levon 	}
14025a0e240fSJohn Levon 	if (one.type == &ldouble_ctype) {
14035a0e240fSJohn Levon 		if (one.ldvalue < two.ldvalue)
14045a0e240fSJohn Levon 			return -1;
14055a0e240fSJohn Levon 		if (one.ldvalue == two.ldvalue)
14065a0e240fSJohn Levon 			return 0;
14075a0e240fSJohn Levon 		return 1;
14085a0e240fSJohn Levon 	}
14095a0e240fSJohn Levon 	sm_perror("bad type in fp_cmp(): %s", type_to_str(type));
14105a0e240fSJohn Levon 	return 1;
14115a0e240fSJohn Levon }
14125a0e240fSJohn Levon 
sval_cmp(sval_t one,sval_t two)14131f5207b7SJohn Levon static inline int sval_cmp(sval_t one, sval_t two)
14141f5207b7SJohn Levon {
14151f5207b7SJohn Levon 	struct symbol *type;
14161f5207b7SJohn Levon 
14175a0e240fSJohn Levon 	if (sval_is_fp(one) || sval_is_fp(two))
14185a0e240fSJohn Levon 		return fp_cmp(one, two);
14195a0e240fSJohn Levon 
14201f5207b7SJohn Levon 	type = one.type;
14211f5207b7SJohn Levon 	if (sval_positive_bits(two) > sval_positive_bits(one))
14221f5207b7SJohn Levon 		type = two.type;
14231f5207b7SJohn Levon 	if (type_bits(type) < 31)
14241f5207b7SJohn Levon 		type = &int_ctype;
14251f5207b7SJohn Levon 
14261f5207b7SJohn Levon 	one = sval_cast(type, one);
14271f5207b7SJohn Levon 	two = sval_cast(type, two);
14281f5207b7SJohn Levon 
14291f5207b7SJohn Levon 	if (type_unsigned(type)) {
14301f5207b7SJohn Levon 		if (one.uvalue < two.uvalue)
14311f5207b7SJohn Levon 			return -1;
14321f5207b7SJohn Levon 		if (one.uvalue == two.uvalue)
14331f5207b7SJohn Levon 			return 0;
14341f5207b7SJohn Levon 		return 1;
14351f5207b7SJohn Levon 	}
14361f5207b7SJohn Levon 	/* fix me handle type promotion and unsigned values */
14371f5207b7SJohn Levon 	if (one.value < two.value)
14381f5207b7SJohn Levon 		return -1;
14391f5207b7SJohn Levon 	if (one.value == two.value)
14401f5207b7SJohn Levon 		return 0;
14411f5207b7SJohn Levon 	return 1;
14421f5207b7SJohn Levon }
14431f5207b7SJohn Levon 
14441f5207b7SJohn Levon #endif 	    /* !SMATCH_H_ */
1445