xref: /illumos-gate/usr/src/tools/smatch/src/smatch.h (revision 7ae7577c)
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
16*7ae7577cSJohn Levon  *
17*7ae7577cSJohn 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>
261f5207b7SJohn Levon #include <sys/time.h>
271f5207b7SJohn Levon #include <sqlite3.h>
281f5207b7SJohn Levon #include "lib.h"
291f5207b7SJohn Levon #include "allocate.h"
301f5207b7SJohn Levon #include "scope.h"
311f5207b7SJohn Levon #include "parse.h"
321f5207b7SJohn Levon #include "expression.h"
331f5207b7SJohn Levon #include "avl.h"
341f5207b7SJohn Levon 
351f5207b7SJohn Levon typedef struct {
361f5207b7SJohn Levon 	struct symbol *type;
371f5207b7SJohn Levon 	union {
381f5207b7SJohn Levon 		long long value;
391f5207b7SJohn Levon 		unsigned long long uvalue;
401f5207b7SJohn Levon 	};
411f5207b7SJohn Levon } sval_t;
421f5207b7SJohn Levon 
431f5207b7SJohn Levon typedef long long mtag_t;
441f5207b7SJohn Levon 
451f5207b7SJohn Levon struct smatch_state {
461f5207b7SJohn Levon 	const char *name;
471f5207b7SJohn Levon 	void *data;
481f5207b7SJohn Levon };
491f5207b7SJohn Levon #define STATE(_x) static struct smatch_state _x = { .name = #_x }
501f5207b7SJohn Levon extern struct smatch_state undefined;
511f5207b7SJohn Levon extern struct smatch_state ghost;
521f5207b7SJohn Levon extern struct smatch_state merged;
531f5207b7SJohn Levon extern struct smatch_state true_state;
541f5207b7SJohn Levon extern struct smatch_state false_state;
551f5207b7SJohn Levon DECLARE_ALLOCATOR(smatch_state);
561f5207b7SJohn Levon 
571f5207b7SJohn Levon static inline void *INT_PTR(int i)
581f5207b7SJohn Levon {
591f5207b7SJohn Levon 	return (void *)(long)i;
601f5207b7SJohn Levon }
611f5207b7SJohn Levon 
621f5207b7SJohn Levon static inline int PTR_INT(void *p)
631f5207b7SJohn Levon {
641f5207b7SJohn Levon 	return (int)(long)p;
651f5207b7SJohn Levon }
661f5207b7SJohn Levon 
671f5207b7SJohn Levon struct tracker {
681f5207b7SJohn Levon 	char *name;
691f5207b7SJohn Levon 	struct symbol *sym;
701f5207b7SJohn Levon 	unsigned short owner;
711f5207b7SJohn Levon };
721f5207b7SJohn Levon DECLARE_ALLOCATOR(tracker);
731f5207b7SJohn Levon DECLARE_PTR_LIST(tracker_list, struct tracker);
741f5207b7SJohn Levon DECLARE_PTR_LIST(stree_stack, struct stree);
751f5207b7SJohn Levon 
761f5207b7SJohn Levon /* The first 3 struct members must match struct tracker */
771f5207b7SJohn Levon struct sm_state {
781f5207b7SJohn Levon 	const char *name;
791f5207b7SJohn Levon 	struct symbol *sym;
801f5207b7SJohn Levon 	unsigned short owner;
811f5207b7SJohn Levon 	unsigned short merged:1;
821f5207b7SJohn Levon 	unsigned short skip_implications:1;
831f5207b7SJohn Levon 	unsigned int nr_children;
841f5207b7SJohn Levon 	unsigned int line;
851f5207b7SJohn Levon   	struct smatch_state *state;
861f5207b7SJohn Levon 	struct stree *pool;
871f5207b7SJohn Levon 	struct sm_state *left;
881f5207b7SJohn Levon 	struct sm_state *right;
891f5207b7SJohn Levon 	struct state_list *possible;
901f5207b7SJohn Levon };
911f5207b7SJohn Levon 
921f5207b7SJohn Levon struct var_sym {
931f5207b7SJohn Levon 	char *var;
941f5207b7SJohn Levon 	struct symbol *sym;
951f5207b7SJohn Levon };
961f5207b7SJohn Levon DECLARE_ALLOCATOR(var_sym);
971f5207b7SJohn Levon DECLARE_PTR_LIST(var_sym_list, struct var_sym);
981f5207b7SJohn Levon 
991f5207b7SJohn Levon struct constraint {
1001f5207b7SJohn Levon 	int op;
1011f5207b7SJohn Levon 	int id;
1021f5207b7SJohn Levon };
1031f5207b7SJohn Levon DECLARE_PTR_LIST(constraint_list, struct constraint);
1041f5207b7SJohn Levon 
1051f5207b7SJohn Levon enum hook_type {
1061f5207b7SJohn Levon 	EXPR_HOOK,
1071f5207b7SJohn Levon 	STMT_HOOK,
1081f5207b7SJohn Levon 	STMT_HOOK_AFTER,
1091f5207b7SJohn Levon 	SYM_HOOK,
1101f5207b7SJohn Levon 	STRING_HOOK,
1111f5207b7SJohn Levon 	DECLARATION_HOOK,
1121f5207b7SJohn Levon 	ASSIGNMENT_HOOK,
1131f5207b7SJohn Levon 	ASSIGNMENT_HOOK_AFTER,
1141f5207b7SJohn Levon 	RAW_ASSIGNMENT_HOOK,
1151f5207b7SJohn Levon 	GLOBAL_ASSIGNMENT_HOOK,
1161f5207b7SJohn Levon 	LOGIC_HOOK,
1171f5207b7SJohn Levon 	CONDITION_HOOK,
1181f5207b7SJohn Levon 	PRELOOP_HOOK,
1191f5207b7SJohn Levon 	SELECT_HOOK,
1201f5207b7SJohn Levon 	WHOLE_CONDITION_HOOK,
1211f5207b7SJohn Levon 	FUNCTION_CALL_HOOK_BEFORE,
1221f5207b7SJohn Levon 	FUNCTION_CALL_HOOK,
1231f5207b7SJohn Levon 	CALL_HOOK_AFTER_INLINE,
1241f5207b7SJohn Levon 	FUNCTION_CALL_HOOK_AFTER_DB,
1251f5207b7SJohn Levon 	CALL_ASSIGNMENT_HOOK,
1261f5207b7SJohn Levon 	MACRO_ASSIGNMENT_HOOK,
1271f5207b7SJohn Levon 	BINOP_HOOK,
1281f5207b7SJohn Levon 	OP_HOOK,
1291f5207b7SJohn Levon 	DEREF_HOOK,
1301f5207b7SJohn Levon 	CASE_HOOK,
1311f5207b7SJohn Levon 	ASM_HOOK,
1321f5207b7SJohn Levon 	CAST_HOOK,
1331f5207b7SJohn Levon 	SIZEOF_HOOK,
1341f5207b7SJohn Levon 	BASE_HOOK,
1351f5207b7SJohn Levon 	FUNC_DEF_HOOK,
1361f5207b7SJohn Levon 	AFTER_DEF_HOOK,
1371f5207b7SJohn Levon 	END_FUNC_HOOK,
1381f5207b7SJohn Levon 	AFTER_FUNC_HOOK,
1391f5207b7SJohn Levon 	RETURN_HOOK,
1401f5207b7SJohn Levon 	INLINE_FN_START,
1411f5207b7SJohn Levon 	INLINE_FN_END,
1421f5207b7SJohn Levon 	END_FILE_HOOK,
1431f5207b7SJohn Levon 	NUM_HOOKS,
1441f5207b7SJohn Levon };
1451f5207b7SJohn Levon 
1461f5207b7SJohn Levon #define TRUE 1
1471f5207b7SJohn Levon #define FALSE 0
1481f5207b7SJohn Levon 
1491f5207b7SJohn Levon struct range_list;
1501f5207b7SJohn Levon 
1511f5207b7SJohn Levon void add_hook(void *func, enum hook_type type);
1521f5207b7SJohn Levon typedef struct smatch_state *(merge_func_t)(struct smatch_state *s1, struct smatch_state *s2);
1531f5207b7SJohn Levon typedef struct smatch_state *(unmatched_func_t)(struct sm_state *state);
1541f5207b7SJohn Levon void add_merge_hook(int client_id, merge_func_t *func);
1551f5207b7SJohn Levon void add_unmatched_state_hook(int client_id, unmatched_func_t *func);
1561f5207b7SJohn Levon void add_pre_merge_hook(int client_id, void (*hook)(struct sm_state *sm));
1571f5207b7SJohn Levon typedef void (scope_hook)(void *data);
1581f5207b7SJohn Levon void add_scope_hook(scope_hook *hook, void *data);
1591f5207b7SJohn Levon typedef void (func_hook)(const char *fn, struct expression *expr, void *data);
1601f5207b7SJohn Levon typedef void (implication_hook)(const char *fn, struct expression *call_expr,
1611f5207b7SJohn Levon 				struct expression *assign_expr, void *data);
1621f5207b7SJohn Levon typedef void (return_implies_hook)(struct expression *call_expr,
1631f5207b7SJohn Levon 				   int param, char *key, char *value);
1641f5207b7SJohn Levon typedef int (implied_return_hook)(struct expression *call_expr, void *info, struct range_list **rl);
1651f5207b7SJohn Levon void add_function_hook(const char *look_for, func_hook *call_back, void *data);
1661f5207b7SJohn Levon 
1671f5207b7SJohn Levon void add_function_assign_hook(const char *look_for, func_hook *call_back,
1681f5207b7SJohn Levon 			      void *info);
1691f5207b7SJohn Levon void add_implied_return_hook(const char *look_for,
1701f5207b7SJohn Levon 			     implied_return_hook *call_back,
1711f5207b7SJohn Levon 			     void *info);
1721f5207b7SJohn Levon void add_macro_assign_hook(const char *look_for, func_hook *call_back,
1731f5207b7SJohn Levon 			      void *info);
1741f5207b7SJohn Levon void add_macro_assign_hook_extra(const char *look_for, func_hook *call_back,
1751f5207b7SJohn Levon 			      void *info);
1761f5207b7SJohn Levon void return_implies_state(const char *look_for, long long start, long long end,
1771f5207b7SJohn Levon 			 implication_hook *call_back, void *info);
1781f5207b7SJohn Levon void select_return_states_hook(int type, return_implies_hook *callback);
1791f5207b7SJohn Levon void select_return_states_before(void (*fn)(void));
1801f5207b7SJohn Levon void select_return_states_after(void (*fn)(void));
1811f5207b7SJohn Levon int get_implied_return(struct expression *expr, struct range_list **rl);
1821f5207b7SJohn Levon void allocate_hook_memory(void);
1831f5207b7SJohn Levon 
1841f5207b7SJohn Levon struct modification_data {
1851f5207b7SJohn Levon 	struct smatch_state *prev;
1861f5207b7SJohn Levon 	struct expression *cur;
1871f5207b7SJohn Levon };
1881f5207b7SJohn Levon 
1891f5207b7SJohn Levon typedef void (modification_hook)(struct sm_state *sm, struct expression *mod_expr);
1901f5207b7SJohn Levon void add_modification_hook(int owner, modification_hook *call_back);
1911f5207b7SJohn Levon void add_modification_hook_late(int owner, modification_hook *call_back);
1921f5207b7SJohn Levon struct smatch_state *get_modification_state(struct expression *expr);
1931f5207b7SJohn Levon 
1941f5207b7SJohn Levon int outside_of_function(void);
1951f5207b7SJohn Levon const char *get_filename(void);
1961f5207b7SJohn Levon const char *get_base_file(void);
1971f5207b7SJohn Levon char *get_function(void);
1981f5207b7SJohn Levon int get_lineno(void);
1991f5207b7SJohn Levon extern int final_pass;
2001f5207b7SJohn Levon extern struct symbol *cur_func_sym;
2011f5207b7SJohn Levon extern int option_debug;
2021f5207b7SJohn Levon extern int local_debug;
2031f5207b7SJohn Levon extern int option_info;
2041f5207b7SJohn Levon extern int option_spammy;
205*7ae7577cSJohn Levon extern int option_timeout;
2061f5207b7SJohn Levon extern char *trace_variable;
2071f5207b7SJohn Levon extern struct stree *global_states;
2081f5207b7SJohn Levon int is_skipped_function(void);
2091f5207b7SJohn Levon int is_silenced_function(void);
2101f5207b7SJohn Levon 
2111f5207b7SJohn Levon /* smatch_impossible.c */
2121f5207b7SJohn Levon int is_impossible_path(void);
2131f5207b7SJohn Levon void set_path_impossible(void);
2141f5207b7SJohn Levon 
2151f5207b7SJohn Levon extern FILE *sm_outfd;
2161f5207b7SJohn Levon extern FILE *sql_outfd;
2171f5207b7SJohn Levon extern FILE *caller_info_fd;
2181f5207b7SJohn Levon extern int sm_nr_checks;
2191f5207b7SJohn Levon extern int sm_nr_errors;
2201f5207b7SJohn Levon extern const char *progname;
2211f5207b7SJohn Levon 
2221f5207b7SJohn Levon /*
2231f5207b7SJohn Levon  * How to use these routines:
2241f5207b7SJohn Levon  *
2251f5207b7SJohn Levon  * sm_fatal(): an internal error of some kind that should immediately exit
2261f5207b7SJohn Levon  * sm_ierror(): an internal error
2271f5207b7SJohn Levon  * sm_perror(): an internal error from parsing input source
2281f5207b7SJohn Levon  * sm_error(): an error from input source
2291f5207b7SJohn Levon  * sm_warning(): a warning from input source
2301f5207b7SJohn Levon  * sm_info(): info message (from option_info)
2311f5207b7SJohn Levon  * sm_debug(): debug message
2321f5207b7SJohn Levon  * sm_msg(): other message (please avoid using this)
2331f5207b7SJohn Levon  */
2341f5207b7SJohn Levon 
2351f5207b7SJohn Levon #define sm_printf(msg...) do { if (final_pass || option_debug || local_debug) fprintf(sm_outfd, msg); } while (0)
2361f5207b7SJohn Levon 
2371f5207b7SJohn Levon static inline void sm_prefix(void)
2381f5207b7SJohn Levon {
2391f5207b7SJohn Levon 	sm_printf("%s: %s:%d %s() ", progname, get_filename(), get_lineno(), get_function());
2401f5207b7SJohn Levon }
2411f5207b7SJohn Levon 
2421f5207b7SJohn Levon static inline void print_implied_debug_msg();
2431f5207b7SJohn Levon 
2441f5207b7SJohn Levon extern bool __silence_warnings_for_stmt;
2451f5207b7SJohn Levon 
2461f5207b7SJohn Levon #define sm_print_msg(type, msg...) \
2471f5207b7SJohn Levon do {                                                           \
2481f5207b7SJohn Levon 	print_implied_debug_msg();                             \
2491f5207b7SJohn Levon 	if (!final_pass && !option_debug && !local_debug)      \
2501f5207b7SJohn Levon 		break;                                         \
2511f5207b7SJohn Levon 	if (__silence_warnings_for_stmt && !option_debug && !local_debug) \
2521f5207b7SJohn Levon 		break;					       \
2531f5207b7SJohn Levon 	if (!option_info && is_silenced_function())	       \
2541f5207b7SJohn Levon 		break;					       \
2551f5207b7SJohn Levon 	sm_prefix();					       \
2561f5207b7SJohn Levon 	if (type == 1) {				       \
2571f5207b7SJohn Levon 		sm_printf("warn: ");			       \
2581f5207b7SJohn Levon 		sm_nr_checks++;			    	       \
2591f5207b7SJohn Levon 	} else if (type == 2) {				       \
2601f5207b7SJohn Levon 		sm_printf("error: ");			       \
2611f5207b7SJohn Levon 		sm_nr_checks++;				       \
2621f5207b7SJohn Levon 	} else if (type == 3) {				       \
2631f5207b7SJohn Levon 		sm_printf("parse error: ");		       \
2641f5207b7SJohn Levon 		sm_nr_errors++;				       \
2651f5207b7SJohn Levon 	}						       \
2661f5207b7SJohn Levon         sm_printf(msg);                                        \
2671f5207b7SJohn Levon         sm_printf("\n");                                       \
2681f5207b7SJohn Levon } while (0)
2691f5207b7SJohn Levon 
2701f5207b7SJohn Levon #define sm_msg(msg...) do { sm_print_msg(0, msg); } while (0)
2711f5207b7SJohn Levon 
2721f5207b7SJohn Levon #define local_debug(msg...)					\
2731f5207b7SJohn Levon do {								\
2741f5207b7SJohn Levon 	if (local_debug)					\
2751f5207b7SJohn Levon 		sm_msg(msg);					\
2761f5207b7SJohn Levon } while (0)
2771f5207b7SJohn Levon 
2781f5207b7SJohn Levon extern char *implied_debug_msg;
2791f5207b7SJohn Levon static inline void print_implied_debug_msg(void)
2801f5207b7SJohn Levon {
2811f5207b7SJohn Levon 	static struct symbol *last_printed = NULL;
2821f5207b7SJohn Levon 
2831f5207b7SJohn Levon 	if (!implied_debug_msg)
2841f5207b7SJohn Levon 		return;
2851f5207b7SJohn Levon 	if (last_printed == cur_func_sym)
2861f5207b7SJohn Levon 		return;
2871f5207b7SJohn Levon 	last_printed = cur_func_sym;
2881f5207b7SJohn Levon 	sm_msg("%s", implied_debug_msg);
2891f5207b7SJohn Levon }
2901f5207b7SJohn Levon 
2911f5207b7SJohn Levon #define sm_debug(msg...) do { if (option_debug) sm_printf(msg); } while (0)
2921f5207b7SJohn Levon 
2931f5207b7SJohn Levon #define sm_info(msg...) do {					\
2941f5207b7SJohn Levon 	if (option_debug || (option_info && final_pass)) {	\
2951f5207b7SJohn Levon 		sm_prefix();					\
2961f5207b7SJohn Levon 		sm_printf("info: ");				\
2971f5207b7SJohn Levon 		sm_printf(msg);					\
2981f5207b7SJohn Levon 		sm_printf("\n");				\
2991f5207b7SJohn Levon 	}							\
3001f5207b7SJohn Levon } while(0)
3011f5207b7SJohn Levon 
3021f5207b7SJohn Levon #define sm_warning(msg...) do { sm_print_msg(1, msg); } while (0)
3031f5207b7SJohn Levon #define sm_error(msg...) do { sm_print_msg(2, msg); } while (0)
3041f5207b7SJohn Levon #define sm_perror(msg...) do { sm_print_msg(3, msg); } while (0)
3051f5207b7SJohn Levon 
3061f5207b7SJohn Levon static inline void sm_fatal(const char *fmt, ...)
3071f5207b7SJohn Levon {
3081f5207b7SJohn Levon 	va_list args;
3091f5207b7SJohn Levon 
3101f5207b7SJohn Levon 	va_start(args, fmt);
3111f5207b7SJohn Levon 	vfprintf(sm_outfd, fmt, args);
3121f5207b7SJohn Levon 	va_end(args);
3131f5207b7SJohn Levon 
3141f5207b7SJohn Levon 	fprintf(sm_outfd, "\n");
3151f5207b7SJohn Levon 
3161f5207b7SJohn Levon 	exit(1);
3171f5207b7SJohn Levon }
3181f5207b7SJohn Levon 
3191f5207b7SJohn Levon static inline void sm_ierror(const char *fmt, ...)
3201f5207b7SJohn Levon {
3211f5207b7SJohn Levon 	va_list args;
3221f5207b7SJohn Levon 
3231f5207b7SJohn Levon 	sm_nr_errors++;
3241f5207b7SJohn Levon 
3251f5207b7SJohn Levon 	fprintf(sm_outfd, "internal error: ");
3261f5207b7SJohn Levon 
3271f5207b7SJohn Levon 	va_start(args, fmt);
3281f5207b7SJohn Levon 	vfprintf(sm_outfd, fmt, args);
3291f5207b7SJohn Levon 	va_end(args);
3301f5207b7SJohn Levon 
3311f5207b7SJohn Levon 	fprintf(sm_outfd, "\n");
3321f5207b7SJohn Levon }
3331f5207b7SJohn Levon #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
3341f5207b7SJohn Levon 
3351f5207b7SJohn Levon struct smatch_state *__get_state(int owner, const char *name, struct symbol *sym);
3361f5207b7SJohn Levon struct smatch_state *get_state(int owner, const char *name, struct symbol *sym);
3371f5207b7SJohn Levon struct smatch_state *get_state_expr(int owner, struct expression *expr);
3381f5207b7SJohn Levon struct state_list *get_possible_states(int owner, const char *name,
3391f5207b7SJohn Levon 				       struct symbol *sym);
3401f5207b7SJohn Levon struct state_list *get_possible_states_expr(int owner, struct expression *expr);
3411f5207b7SJohn Levon struct sm_state *set_state(int owner, const char *name, struct symbol *sym,
3421f5207b7SJohn Levon 	       struct smatch_state *state);
3431f5207b7SJohn Levon struct sm_state *set_state_expr(int owner, struct expression *expr,
3441f5207b7SJohn Levon 		struct smatch_state *state);
3451f5207b7SJohn Levon void delete_state(int owner, const char *name, struct symbol *sym);
3461f5207b7SJohn Levon void delete_state_expr(int owner, struct expression *expr);
3471f5207b7SJohn Levon void __delete_all_states_sym(struct symbol *sym);
3481f5207b7SJohn Levon void set_true_false_states(int owner, const char *name, struct symbol *sym,
3491f5207b7SJohn Levon 			   struct smatch_state *true_state,
3501f5207b7SJohn Levon 			   struct smatch_state *false_state);
3511f5207b7SJohn Levon void set_true_false_states_expr(int owner, struct expression *expr,
3521f5207b7SJohn Levon 			   struct smatch_state *true_state,
3531f5207b7SJohn Levon 			   struct smatch_state *false_state);
3541f5207b7SJohn Levon 
3551f5207b7SJohn Levon struct stree *get_all_states_from_stree(int owner, struct stree *source);
3561f5207b7SJohn Levon struct stree *get_all_states_stree(int id);
3571f5207b7SJohn Levon struct stree *__get_cur_stree(void);
3581f5207b7SJohn Levon int is_reachable(void);
3591f5207b7SJohn Levon void add_get_state_hook(void (*fn)(int owner, const char *name, struct symbol *sym));
3601f5207b7SJohn Levon 
3611f5207b7SJohn Levon /* smatch_helper.c */
3621f5207b7SJohn Levon DECLARE_PTR_LIST(int_stack, int);
3631f5207b7SJohn Levon char *alloc_string(const char *str);
3641f5207b7SJohn Levon void free_string(char *str);
3651f5207b7SJohn Levon void append(char *dest, const char *data, int buff_len);
3661f5207b7SJohn Levon void remove_parens(char *str);
3671f5207b7SJohn Levon struct smatch_state *alloc_state_num(int num);
3681f5207b7SJohn Levon struct smatch_state *alloc_state_str(const char *name);
3691f5207b7SJohn Levon struct smatch_state *alloc_state_expr(struct expression *expr);
3701f5207b7SJohn Levon struct expression *get_argument_from_call_expr(struct expression_list *args,
3711f5207b7SJohn Levon 					       int num);
3721f5207b7SJohn Levon 
3731f5207b7SJohn Levon char *expr_to_var(struct expression *expr);
3741f5207b7SJohn Levon struct symbol *expr_to_sym(struct expression *expr);
3751f5207b7SJohn Levon char *expr_to_str(struct expression *expr);
3761f5207b7SJohn Levon char *expr_to_str_sym(struct expression *expr,
3771f5207b7SJohn Levon 				     struct symbol **sym_ptr);
3781f5207b7SJohn Levon char *expr_to_var_sym(struct expression *expr,
3791f5207b7SJohn Levon 			     struct symbol **sym_ptr);
3801f5207b7SJohn Levon char *expr_to_known_chunk_sym(struct expression *expr, struct symbol **sym);
3811f5207b7SJohn Levon char *expr_to_chunk_sym_vsl(struct expression *expr, struct symbol **sym, struct var_sym_list **vsl);
3821f5207b7SJohn Levon int get_complication_score(struct expression *expr);
3831f5207b7SJohn Levon 
3841f5207b7SJohn Levon int sym_name_is(const char *name, struct expression *expr);
3851f5207b7SJohn Levon int get_const_value(struct expression *expr, sval_t *sval);
3861f5207b7SJohn Levon int get_value(struct expression *expr, sval_t *val);
3871f5207b7SJohn Levon int get_implied_value(struct expression *expr, sval_t *val);
3881f5207b7SJohn Levon int get_implied_min(struct expression *expr, sval_t *sval);
3891f5207b7SJohn Levon int get_implied_max(struct expression *expr, sval_t *val);
3901f5207b7SJohn Levon int get_hard_max(struct expression *expr, sval_t *sval);
3911f5207b7SJohn Levon int get_fuzzy_min(struct expression *expr, sval_t *min);
3921f5207b7SJohn Levon int get_fuzzy_max(struct expression *expr, sval_t *max);
3931f5207b7SJohn Levon int get_absolute_min(struct expression *expr, sval_t *sval);
3941f5207b7SJohn Levon int get_absolute_max(struct expression *expr, sval_t *sval);
3951f5207b7SJohn Levon int parse_call_math(struct expression *expr, char *math, sval_t *val);
3961f5207b7SJohn Levon int parse_call_math_rl(struct expression *call, char *math, struct range_list **rl);
3971f5207b7SJohn Levon char *get_value_in_terms_of_parameter_math(struct expression *expr);
3981f5207b7SJohn Levon char *get_value_in_terms_of_parameter_math_var_sym(const char *var, struct symbol *sym);
3991f5207b7SJohn Levon int is_zero(struct expression *expr);
4001f5207b7SJohn Levon int known_condition_true(struct expression *expr);
4011f5207b7SJohn Levon int known_condition_false(struct expression *expr);
4021f5207b7SJohn Levon int implied_condition_true(struct expression *expr);
4031f5207b7SJohn Levon int implied_condition_false(struct expression *expr);
4041f5207b7SJohn Levon int can_integer_overflow(struct symbol *type, struct expression *expr);
4051f5207b7SJohn Levon void clear_math_cache(void);
4061f5207b7SJohn Levon 
4071f5207b7SJohn Levon int is_array(struct expression *expr);
4081f5207b7SJohn Levon struct expression *get_array_base(struct expression *expr);
4091f5207b7SJohn Levon struct expression *get_array_offset(struct expression *expr);
4101f5207b7SJohn Levon const char *show_state(struct smatch_state *state);
4111f5207b7SJohn Levon struct statement *get_expression_statement(struct expression *expr);
4121f5207b7SJohn Levon struct expression *strip_parens(struct expression *expr);
4131f5207b7SJohn Levon struct expression *strip_expr(struct expression *expr);
4141f5207b7SJohn Levon struct expression *strip_expr_set_parent(struct expression *expr);
4151f5207b7SJohn Levon void scoped_state(int my_id, const char *name, struct symbol *sym);
4161f5207b7SJohn Levon int is_error_return(struct expression *expr);
4171f5207b7SJohn Levon int getting_address(void);
4181f5207b7SJohn Levon int get_struct_and_member(struct expression *expr, const char **type, const char **member);
4191f5207b7SJohn Levon char *get_member_name(struct expression *expr);
4201f5207b7SJohn Levon char *get_fnptr_name(struct expression *expr);
4211f5207b7SJohn Levon int cmp_pos(struct position pos1, struct position pos2);
4221f5207b7SJohn Levon int positions_eq(struct position pos1, struct position pos2);
4231f5207b7SJohn Levon struct statement *get_current_statement(void);
4241f5207b7SJohn Levon struct statement *get_prev_statement(void);
4251f5207b7SJohn Levon struct expression *get_last_expr_from_expression_stmt(struct expression *expr);
4261f5207b7SJohn Levon int get_param_num_from_sym(struct symbol *sym);
4271f5207b7SJohn Levon int get_param_num(struct expression *expr);
4281f5207b7SJohn Levon int ms_since(struct timeval *start);
4291f5207b7SJohn Levon int parent_is_gone_var_sym(const char *name, struct symbol *sym);
4301f5207b7SJohn Levon int parent_is_gone(struct expression *expr);
4311f5207b7SJohn Levon int invert_op(int op);
4321f5207b7SJohn Levon int expr_equiv(struct expression *one, struct expression *two);
4331f5207b7SJohn Levon void push_int(struct int_stack **stack, int num);
4341f5207b7SJohn Levon int pop_int(struct int_stack **stack);
4351f5207b7SJohn Levon 
4361f5207b7SJohn Levon /* smatch_type.c */
4371f5207b7SJohn Levon struct symbol *get_real_base_type(struct symbol *sym);
4381f5207b7SJohn Levon int type_bytes(struct symbol *type);
4391f5207b7SJohn Levon int array_bytes(struct symbol *type);
4401f5207b7SJohn Levon struct symbol *get_pointer_type(struct expression *expr);
4411f5207b7SJohn Levon struct symbol *get_type(struct expression *expr);
4421f5207b7SJohn Levon struct symbol *get_final_type(struct expression *expr);
4431f5207b7SJohn Levon struct symbol *get_promoted_type(struct symbol *left, struct symbol *right);
4441f5207b7SJohn Levon int type_signed(struct symbol *base_type);
4451f5207b7SJohn Levon int expr_unsigned(struct expression *expr);
4461f5207b7SJohn Levon int expr_signed(struct expression *expr);
4471f5207b7SJohn Levon int returns_unsigned(struct symbol *base_type);
4481f5207b7SJohn Levon int is_pointer(struct expression *expr);
4491f5207b7SJohn Levon int returns_pointer(struct symbol *base_type);
4501f5207b7SJohn Levon sval_t sval_type_max(struct symbol *base_type);
4511f5207b7SJohn Levon sval_t sval_type_min(struct symbol *base_type);
4521f5207b7SJohn Levon int nr_bits(struct expression *expr);
4531f5207b7SJohn Levon int is_void_pointer(struct expression *expr);
4541f5207b7SJohn Levon int is_char_pointer(struct expression *expr);
4551f5207b7SJohn Levon int is_string(struct expression *expr);
4561f5207b7SJohn Levon int is_static(struct expression *expr);
4571f5207b7SJohn Levon int is_local_variable(struct expression *expr);
4581f5207b7SJohn Levon int types_equiv(struct symbol *one, struct symbol *two);
4591f5207b7SJohn Levon int fn_static(void);
4601f5207b7SJohn Levon const char *global_static();
4611f5207b7SJohn Levon struct symbol *cur_func_return_type(void);
4621f5207b7SJohn Levon struct symbol *get_arg_type(struct expression *fn, int arg);
4631f5207b7SJohn Levon struct symbol *get_member_type_from_key(struct expression *expr, const char *key);
4641f5207b7SJohn Levon struct symbol *get_arg_type_from_key(struct expression *fn, int param, struct expression *arg, const char *key);
4651f5207b7SJohn Levon int is_struct(struct expression *expr);
4661f5207b7SJohn Levon char *type_to_str(struct symbol *type);
4671f5207b7SJohn Levon 
4681f5207b7SJohn Levon /* smatch_ignore.c */
4691f5207b7SJohn Levon void add_ignore(int owner, const char *name, struct symbol *sym);
4701f5207b7SJohn Levon int is_ignored(int owner, const char *name, struct symbol *sym);
4711f5207b7SJohn Levon void add_ignore_expr(int owner, struct expression *expr);
4721f5207b7SJohn Levon int is_ignored_expr(int owner, struct expression *expr);
4731f5207b7SJohn Levon 
4741f5207b7SJohn Levon /* smatch_var_sym */
4751f5207b7SJohn Levon struct var_sym *alloc_var_sym(const char *var, struct symbol *sym);
4761f5207b7SJohn Levon struct var_sym_list *expr_to_vsl(struct expression *expr);
4771f5207b7SJohn Levon void add_var_sym(struct var_sym_list **list, const char *var, struct symbol *sym);
4781f5207b7SJohn Levon void add_var_sym_expr(struct var_sym_list **list, struct expression *expr);
4791f5207b7SJohn Levon void del_var_sym(struct var_sym_list **list, const char *var, struct symbol *sym);
4801f5207b7SJohn Levon int in_var_sym_list(struct var_sym_list *list, const char *var, struct symbol *sym);
4811f5207b7SJohn Levon struct var_sym_list *clone_var_sym_list(struct var_sym_list *from_vsl);
4821f5207b7SJohn Levon void merge_var_sym_list(struct var_sym_list **dest, struct var_sym_list *src);
4831f5207b7SJohn Levon struct var_sym_list *combine_var_sym_lists(struct var_sym_list *one, struct var_sym_list *two);
4841f5207b7SJohn Levon int var_sym_lists_equiv(struct var_sym_list *one, struct var_sym_list *two);
4851f5207b7SJohn Levon void free_var_sym_list(struct var_sym_list **list);
4861f5207b7SJohn Levon void free_var_syms_and_list(struct var_sym_list **list);
4871f5207b7SJohn Levon 
4881f5207b7SJohn Levon /* smatch_tracker */
4891f5207b7SJohn Levon struct tracker *alloc_tracker(int owner, const char *name, struct symbol *sym);
4901f5207b7SJohn Levon void add_tracker(struct tracker_list **list, int owner, const char *name,
4911f5207b7SJohn Levon 		struct symbol *sym);
4921f5207b7SJohn Levon void add_tracker_expr(struct tracker_list **list, int owner, struct expression *expr);
4931f5207b7SJohn Levon void del_tracker(struct tracker_list **list, int owner, const char *name,
4941f5207b7SJohn Levon 		struct symbol *sym);
4951f5207b7SJohn Levon int in_tracker_list(struct tracker_list *list, int owner, const char *name,
4961f5207b7SJohn Levon 		struct symbol *sym);
4971f5207b7SJohn Levon void free_tracker_list(struct tracker_list **list);
4981f5207b7SJohn Levon void free_trackers_and_list(struct tracker_list **list);
4991f5207b7SJohn Levon 
5001f5207b7SJohn Levon /* smatch_conditions */
5011f5207b7SJohn Levon int in_condition(void);
5021f5207b7SJohn Levon 
5031f5207b7SJohn Levon /* smatch_flow.c */
5041f5207b7SJohn Levon 
5051f5207b7SJohn Levon extern int __in_fake_assign;
5061f5207b7SJohn Levon extern int __in_fake_parameter_assign;
5071f5207b7SJohn Levon extern int __in_fake_struct_assign;
5081f5207b7SJohn Levon extern int in_fake_env;
5091f5207b7SJohn Levon void smatch (int argc, char **argv);
5101f5207b7SJohn Levon int inside_loop(void);
5111f5207b7SJohn Levon int definitely_inside_loop(void);
5121f5207b7SJohn Levon struct expression *get_switch_expr(void);
5131f5207b7SJohn Levon int in_expression_statement(void);
5141f5207b7SJohn Levon void __process_post_op_stack(void);
5151f5207b7SJohn Levon void __split_expr(struct expression *expr);
5161f5207b7SJohn Levon void __split_label_stmt(struct statement *stmt);
5171f5207b7SJohn Levon void __split_stmt(struct statement *stmt);
5181f5207b7SJohn Levon extern int __in_function_def;
5191f5207b7SJohn Levon extern int option_assume_loops;
5201f5207b7SJohn Levon extern int option_two_passes;
5211f5207b7SJohn Levon extern int option_no_db;
5221f5207b7SJohn Levon extern int option_file_output;
5231f5207b7SJohn Levon extern int option_time;
5241f5207b7SJohn Levon extern struct expression_list *big_expression_stack;
5251f5207b7SJohn Levon extern struct expression_list *big_condition_stack;
5261f5207b7SJohn Levon extern struct statement_list *big_statement_stack;
5271f5207b7SJohn Levon int is_assigned_call(struct expression *expr);
5281f5207b7SJohn Levon int inlinable(struct expression *expr);
5291f5207b7SJohn Levon extern int __inline_call;
5301f5207b7SJohn Levon extern struct expression *__inline_fn;
5311f5207b7SJohn Levon extern int __in_pre_condition;
5321f5207b7SJohn Levon extern int __bail_on_rest_of_function;
5331f5207b7SJohn Levon extern struct statement *__prev_stmt;
5341f5207b7SJohn Levon extern struct statement *__cur_stmt;
5351f5207b7SJohn Levon extern struct statement *__next_stmt;
5361f5207b7SJohn Levon void init_fake_env(void);
5371f5207b7SJohn Levon void end_fake_env(void);
5381f5207b7SJohn Levon int time_parsing_function(void);
5391f5207b7SJohn Levon 
5401f5207b7SJohn Levon /* smatch_struct_assignment.c */
5411f5207b7SJohn Levon struct expression *get_faked_expression(void);
5421f5207b7SJohn Levon void __fake_struct_member_assignments(struct expression *expr);
5431f5207b7SJohn Levon 
5441f5207b7SJohn Levon /* smatch_project.c */
5451f5207b7SJohn Levon int is_no_inline_function(const char *function);
5461f5207b7SJohn Levon 
5471f5207b7SJohn Levon /* smatch_conditions */
5481f5207b7SJohn Levon void __split_whole_condition(struct expression *expr);
5491f5207b7SJohn Levon void __handle_logic(struct expression *expr);
5501f5207b7SJohn Levon int is_condition(struct expression *expr);
5511f5207b7SJohn Levon int __handle_condition_assigns(struct expression *expr);
5521f5207b7SJohn Levon int __handle_select_assigns(struct expression *expr);
5531f5207b7SJohn Levon int __handle_expr_statement_assigns(struct expression *expr);
5541f5207b7SJohn Levon 
5551f5207b7SJohn Levon /* smatch_implied.c */
5561f5207b7SJohn Levon extern int option_debug_implied;
5571f5207b7SJohn Levon extern int option_debug_related;
5581f5207b7SJohn Levon struct range_list_stack;
5591f5207b7SJohn Levon void param_limit_implications(struct expression *expr, int param, char *key, char *value);
5601f5207b7SJohn Levon struct stree *__implied_case_stree(struct expression *switch_expr,
5611f5207b7SJohn Levon 				   struct range_list *case_rl,
5621f5207b7SJohn Levon 				   struct range_list_stack **remaining_cases,
5631f5207b7SJohn Levon 				   struct stree **raw_stree);
5641f5207b7SJohn Levon void overwrite_states_using_pool(struct sm_state *gate_sm, struct sm_state *pool_sm);
5651f5207b7SJohn Levon int assume(struct expression *expr);
5661f5207b7SJohn Levon void end_assume(void);
5671f5207b7SJohn Levon int impossible_assumption(struct expression *left, int op, sval_t sval);
5681f5207b7SJohn Levon 
5691f5207b7SJohn Levon /* smatch_extras.c */
5701f5207b7SJohn Levon #define SMATCH_EXTRA 5 /* this is my_id from smatch extra set in smatch.c */
5711f5207b7SJohn Levon extern int RETURN_ID;
5721f5207b7SJohn Levon 
5731f5207b7SJohn Levon struct data_range {
5741f5207b7SJohn Levon 	sval_t min;
5751f5207b7SJohn Levon 	sval_t max;
5761f5207b7SJohn Levon };
5771f5207b7SJohn Levon 
5781f5207b7SJohn Levon #define MTAG_ALIAS_BIT (1ULL << 63)
5791f5207b7SJohn Levon #define MTAG_OFFSET_MASK 0xfffULL
5801f5207b7SJohn Levon 
5811f5207b7SJohn Levon extern long long valid_ptr_min, valid_ptr_max;
5821f5207b7SJohn Levon extern sval_t valid_ptr_min_sval, valid_ptr_max_sval;
5831f5207b7SJohn Levon extern struct range_list *valid_ptr_rl;
5841f5207b7SJohn Levon static const sval_t array_min_sval = {
5851f5207b7SJohn Levon 	.type = &ptr_ctype,
5861f5207b7SJohn Levon 	{.value = 100000},
5871f5207b7SJohn Levon };
5881f5207b7SJohn Levon static const sval_t array_max_sval = {
5891f5207b7SJohn Levon 	.type = &ptr_ctype,
5901f5207b7SJohn Levon 	{.value = 199999},
5911f5207b7SJohn Levon };
5921f5207b7SJohn Levon static const sval_t text_seg_min = {
5931f5207b7SJohn Levon 	.type = &ptr_ctype,
5941f5207b7SJohn Levon 	{.value = 100000000},
5951f5207b7SJohn Levon };
5961f5207b7SJohn Levon static const sval_t text_seg_max = {
5971f5207b7SJohn Levon 	.type = &ptr_ctype,
5981f5207b7SJohn Levon 	{.value = 177777777},
5991f5207b7SJohn Levon };
6001f5207b7SJohn Levon static const sval_t data_seg_min = {
6011f5207b7SJohn Levon 	.type = &ptr_ctype,
6021f5207b7SJohn Levon 	{.value = 200000000},
6031f5207b7SJohn Levon };
6041f5207b7SJohn Levon static const sval_t data_seg_max = {
6051f5207b7SJohn Levon 	.type = &ptr_ctype,
6061f5207b7SJohn Levon 	{.value = 277777777},
6071f5207b7SJohn Levon };
6081f5207b7SJohn Levon static const sval_t bss_seg_min = {
6091f5207b7SJohn Levon 	.type = &ptr_ctype,
6101f5207b7SJohn Levon 	{.value = 300000000},
6111f5207b7SJohn Levon };
6121f5207b7SJohn Levon static const sval_t bss_seg_max = {
6131f5207b7SJohn Levon 	.type = &ptr_ctype,
6141f5207b7SJohn Levon 	{.value = 377777777},
6151f5207b7SJohn Levon };
6161f5207b7SJohn Levon static const sval_t stack_seg_min = {
6171f5207b7SJohn Levon 	.type = &ptr_ctype,
6181f5207b7SJohn Levon 	{.value = 400000000},
6191f5207b7SJohn Levon };
6201f5207b7SJohn Levon static const sval_t stack_seg_max = {
6211f5207b7SJohn Levon 	.type = &ptr_ctype,
6221f5207b7SJohn Levon 	{.value = 477777777},
6231f5207b7SJohn Levon };
6241f5207b7SJohn Levon static const sval_t kmalloc_seg_min = {
6251f5207b7SJohn Levon 	.type = &ptr_ctype,
6261f5207b7SJohn Levon 	{.value = 500000000},
6271f5207b7SJohn Levon };
6281f5207b7SJohn Levon static const sval_t kmalloc_seg_max = {
6291f5207b7SJohn Levon 	.type = &ptr_ctype,
6301f5207b7SJohn Levon 	{.value = 577777777},
6311f5207b7SJohn Levon };
6321f5207b7SJohn Levon static const sval_t vmalloc_seg_min = {
6331f5207b7SJohn Levon 	.type = &ptr_ctype,
6341f5207b7SJohn Levon 	{.value = 600000000},
6351f5207b7SJohn Levon };
6361f5207b7SJohn Levon static const sval_t vmalloc_seg_max = {
6371f5207b7SJohn Levon 	.type = &ptr_ctype,
6381f5207b7SJohn Levon 	{.value = 677777777},
6391f5207b7SJohn Levon };
6401f5207b7SJohn Levon static const sval_t fn_ptr_min = {
6411f5207b7SJohn Levon 	.type = &ptr_ctype,
6421f5207b7SJohn Levon 	{.value = 700000000},
6431f5207b7SJohn Levon };
6441f5207b7SJohn Levon static const sval_t fn_ptr_max = {
6451f5207b7SJohn Levon 	.type = &ptr_ctype,
6461f5207b7SJohn Levon 	{.value = 777777777},
6471f5207b7SJohn Levon };
6481f5207b7SJohn Levon 
6491f5207b7SJohn Levon char *get_other_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym);
6501f5207b7SJohn Levon char *map_call_to_other_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym);
6511f5207b7SJohn Levon char *map_long_to_short_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym);
6521f5207b7SJohn Levon char *map_long_to_short_name_sym_nostack(const char *name, struct symbol *sym, struct symbol **new_sym);
6531f5207b7SJohn Levon 
6541f5207b7SJohn Levon #define STRLEN_MAX_RET 1010101
6551f5207b7SJohn Levon 
6561f5207b7SJohn Levon /* smatch_absolute.c */
6571f5207b7SJohn Levon int get_absolute_min_helper(struct expression *expr, sval_t *sval);
6581f5207b7SJohn Levon int get_absolute_max_helper(struct expression *expr, sval_t *sval);
6591f5207b7SJohn Levon 
6601f5207b7SJohn Levon /* smatch_local_values.c */
6611f5207b7SJohn Levon int get_local_rl(struct expression *expr, struct range_list **rl);
6621f5207b7SJohn Levon int get_local_max_helper(struct expression *expr, sval_t *sval);
6631f5207b7SJohn Levon int get_local_min_helper(struct expression *expr, sval_t *sval);
6641f5207b7SJohn Levon 
6651f5207b7SJohn Levon /* smatch_type_value.c */
6661f5207b7SJohn Levon int get_db_type_rl(struct expression *expr, struct range_list **rl);
6671f5207b7SJohn Levon /* smatch_data_val.c */
6681f5207b7SJohn Levon int get_mtag_rl(struct expression *expr, struct range_list **rl);
6691f5207b7SJohn Levon /* smatch_array_values.c */
6701f5207b7SJohn Levon int get_array_rl(struct expression *expr, struct range_list **rl);
6711f5207b7SJohn Levon 
6721f5207b7SJohn Levon /* smatch_states.c */
6731f5207b7SJohn Levon void __swap_cur_stree(struct stree *stree);
6741f5207b7SJohn Levon void __push_fake_cur_stree();
6751f5207b7SJohn Levon struct stree *__pop_fake_cur_stree();
6761f5207b7SJohn Levon void __free_fake_cur_stree();
6771f5207b7SJohn Levon void __set_fake_cur_stree_fast(struct stree *stree);
6781f5207b7SJohn Levon void __pop_fake_cur_stree_fast(void);
6791f5207b7SJohn Levon void __merge_stree_into_cur(struct stree *stree);
6801f5207b7SJohn Levon 
6811f5207b7SJohn Levon int unreachable(void);
6821f5207b7SJohn Levon void __set_sm(struct sm_state *sm);
6831f5207b7SJohn Levon void __set_sm_cur_stree(struct sm_state *sm);
6841f5207b7SJohn Levon void __set_sm_fake_stree(struct sm_state *sm);
6851f5207b7SJohn Levon void __set_true_false_sm(struct sm_state *true_state,
6861f5207b7SJohn Levon 			struct sm_state *false_state);
6871f5207b7SJohn Levon void nullify_path(void);
6881f5207b7SJohn Levon void __match_nullify_path_hook(const char *fn, struct expression *expr,
6891f5207b7SJohn Levon 			       void *unused);
6901f5207b7SJohn Levon void __unnullify_path(void);
6911f5207b7SJohn Levon int __path_is_null(void);
6921f5207b7SJohn Levon void save_all_states(void);
6931f5207b7SJohn Levon void restore_all_states(void);
6941f5207b7SJohn Levon void free_goto_stack(void);
6951f5207b7SJohn Levon void clear_all_states(void);
6961f5207b7SJohn Levon 
6971f5207b7SJohn Levon struct sm_state *get_sm_state(int owner, const char *name,
6981f5207b7SJohn Levon 				struct symbol *sym);
6991f5207b7SJohn Levon struct sm_state *get_sm_state_expr(int owner, struct expression *expr);
7001f5207b7SJohn Levon void __push_true_states(void);
7011f5207b7SJohn Levon void __use_false_states(void);
7021f5207b7SJohn Levon void __discard_false_states(void);
7031f5207b7SJohn Levon void __merge_false_states(void);
7041f5207b7SJohn Levon void __merge_true_states(void);
7051f5207b7SJohn Levon 
7061f5207b7SJohn Levon void __negate_cond_stacks(void);
7071f5207b7SJohn Levon void __use_pre_cond_states(void);
7081f5207b7SJohn Levon void __use_cond_true_states(void);
7091f5207b7SJohn Levon void __use_cond_false_states(void);
7101f5207b7SJohn Levon void __push_cond_stacks(void);
7111f5207b7SJohn Levon void __fold_in_set_states(void);
7121f5207b7SJohn Levon void __free_set_states(void);
7131f5207b7SJohn Levon struct stree *__copy_cond_true_states(void);
7141f5207b7SJohn Levon struct stree *__copy_cond_false_states(void);
7151f5207b7SJohn Levon struct stree *__pop_cond_true_stack(void);
7161f5207b7SJohn Levon struct stree *__pop_cond_false_stack(void);
7171f5207b7SJohn Levon void __and_cond_states(void);
7181f5207b7SJohn Levon void __or_cond_states(void);
7191f5207b7SJohn Levon void __save_pre_cond_states(void);
7201f5207b7SJohn Levon void __discard_pre_cond_states(void);
7211f5207b7SJohn Levon struct stree *__get_true_states(void);
7221f5207b7SJohn Levon struct stree *__get_false_states(void);
7231f5207b7SJohn Levon void __use_cond_states(void);
7241f5207b7SJohn Levon extern struct state_list *__last_base_slist;
7251f5207b7SJohn Levon 
7261f5207b7SJohn Levon void __push_continues(void);
7271f5207b7SJohn Levon void __discard_continues(void);
7281f5207b7SJohn Levon void __process_continues(void);
7291f5207b7SJohn Levon void __merge_continues(void);
7301f5207b7SJohn Levon 
7311f5207b7SJohn Levon void __push_breaks(void);
7321f5207b7SJohn Levon void __process_breaks(void);
7331f5207b7SJohn Levon int __has_breaks(void);
7341f5207b7SJohn Levon void __merge_breaks(void);
7351f5207b7SJohn Levon void __use_breaks(void);
7361f5207b7SJohn Levon 
7371f5207b7SJohn Levon void __save_switch_states(struct expression *switch_expr);
7381f5207b7SJohn Levon void __discard_switches(void);
7391f5207b7SJohn Levon int have_remaining_cases(void);
7401f5207b7SJohn Levon void __merge_switches(struct expression *switch_expr, struct range_list *case_rl);
7411f5207b7SJohn Levon void __push_default(void);
7421f5207b7SJohn Levon void __set_default(void);
7431f5207b7SJohn Levon int __pop_default(void);
7441f5207b7SJohn Levon 
7451f5207b7SJohn Levon void __push_conditions(void);
7461f5207b7SJohn Levon void __discard_conditions(void);
7471f5207b7SJohn Levon 
7481f5207b7SJohn Levon void __save_gotos(const char *name, struct symbol *sym);
7491f5207b7SJohn Levon void __merge_gotos(const char *name, struct symbol *sym);
7501f5207b7SJohn Levon 
7511f5207b7SJohn Levon void __print_cur_stree(void);
7521f5207b7SJohn Levon 
7531f5207b7SJohn Levon /* smatch_hooks.c */
7541f5207b7SJohn Levon void __pass_to_client(void *data, enum hook_type type);
7551f5207b7SJohn Levon void __pass_to_client_no_data(enum hook_type type);
7561f5207b7SJohn Levon void __pass_case_to_client(struct expression *switch_expr,
7571f5207b7SJohn Levon 			   struct range_list *rl);
7581f5207b7SJohn Levon int __has_merge_function(int client_id);
7591f5207b7SJohn Levon struct smatch_state *__client_merge_function(int owner,
7601f5207b7SJohn Levon 					     struct smatch_state *s1,
7611f5207b7SJohn Levon 					     struct smatch_state *s2);
7621f5207b7SJohn Levon struct smatch_state *__client_unmatched_state_function(struct sm_state *sm);
7631f5207b7SJohn Levon void call_pre_merge_hook(struct sm_state *sm);
7641f5207b7SJohn Levon void __push_scope_hooks(void);
7651f5207b7SJohn Levon void __call_scope_hooks(void);
7661f5207b7SJohn Levon 
7671f5207b7SJohn Levon /* smatch_function_hooks.c */
7681f5207b7SJohn Levon void create_function_hook_hash(void);
7691f5207b7SJohn Levon void __match_initializer_call(struct symbol *sym);
7701f5207b7SJohn Levon 
7711f5207b7SJohn Levon /* smatch_db.c */
7721f5207b7SJohn Levon enum info_type {
7731f5207b7SJohn Levon 	INTERNAL	= 0,
7741f5207b7SJohn Levon 	/*
7751f5207b7SJohn Levon 	 * Changing these numbers is a pain.  Don't do it.  If you ever use a
7761f5207b7SJohn Levon 	 * number it can't be re-used right away so there may be gaps.
7771f5207b7SJohn Levon 	 * We select these in order by type so if the order matters, then give
7781f5207b7SJohn Levon 	 * it a number below 100-999,9000-9999 ranges. */
7791f5207b7SJohn Levon 
7801f5207b7SJohn Levon 	PARAM_CLEARED	= 101,
7811f5207b7SJohn Levon 	PARAM_LIMIT	= 103,
7821f5207b7SJohn Levon 	PARAM_FILTER	= 104,
7831f5207b7SJohn Levon 
7841f5207b7SJohn Levon 	PARAM_VALUE	= 1001,
7851f5207b7SJohn Levon 	BUF_SIZE	= 1002,
7861f5207b7SJohn Levon 	USER_DATA	= 1003,
7871f5207b7SJohn Levon 	CAPPED_DATA	= 1004,
7881f5207b7SJohn Levon 	RETURN_VALUE	= 1005,
7891f5207b7SJohn Levon 	DEREFERENCE	= 1006,
7901f5207b7SJohn Levon 	RANGE_CAP	= 1007,
7911f5207b7SJohn Levon 	LOCK_HELD	= 1008,
7921f5207b7SJohn Levon 	LOCK_RELEASED	= 1009,
7931f5207b7SJohn Levon 	ABSOLUTE_LIMITS	= 1010,
7941f5207b7SJohn Levon 	PARAM_ADD	= 1012,
7951f5207b7SJohn Levon 	PARAM_FREED	= 1013,
7961f5207b7SJohn Levon 	DATA_SOURCE	= 1014,
7971f5207b7SJohn Levon 	FUZZY_MAX	= 1015,
7981f5207b7SJohn Levon 	STR_LEN		= 1016,
7991f5207b7SJohn Levon 	ARRAY_LEN	= 1017,
8001f5207b7SJohn Levon 	CAPABLE		= 1018,
8011f5207b7SJohn Levon 	NS_CAPABLE	= 1019,
8021f5207b7SJohn Levon 	CONTAINER	= 1020,
8031f5207b7SJohn Levon 	CASTED_CALL	= 1021,
8041f5207b7SJohn Levon 	TYPE_LINK	= 1022,
8051f5207b7SJohn Levon 	UNTRACKED_PARAM = 1023,
8061f5207b7SJohn Levon 	CULL_PATH	= 1024,
8071f5207b7SJohn Levon 	PARAM_SET	= 1025,
8081f5207b7SJohn Levon 	PARAM_USED	= 1026,
8091f5207b7SJohn Levon 	BYTE_UNITS      = 1027,
8101f5207b7SJohn Levon 	COMPARE_LIMIT	= 1028,
8111f5207b7SJohn Levon 	PARAM_COMPARE	= 1029,
8121f5207b7SJohn Levon 	CONSTRAINT	= 1031,
8131f5207b7SJohn Levon 	PASSES_TYPE	= 1032,
8141f5207b7SJohn Levon 	CONSTRAINT_REQUIRED = 1033,
8151f5207b7SJohn Levon 	NOSPEC		= 1035,
8161f5207b7SJohn Levon 	NOSPEC_WB	= 1036,
8171f5207b7SJohn Levon 	STMT_CNT	= 1037,
8181f5207b7SJohn Levon 	TERMINATED	= 1038,
8191f5207b7SJohn Levon 
8201f5207b7SJohn Levon 	/* put random temporary stuff in the 7000-7999 range for testing */
8211f5207b7SJohn Levon 	USER_DATA3	= 8017,
8221f5207b7SJohn Levon 	USER_DATA3_SET	= 9017,
8231f5207b7SJohn Levon 	NO_OVERFLOW	= 8018,
8241f5207b7SJohn Levon 	NO_OVERFLOW_SIMPLE = 8019,
8251f5207b7SJohn Levon 	LOCKED		= 8020,
8261f5207b7SJohn Levon 	UNLOCKED	= 8021,
8271f5207b7SJohn Levon 	SET_FS		= 8022,
8281f5207b7SJohn Levon 	ATOMIC_INC	= 8023,
8291f5207b7SJohn Levon 	ATOMIC_DEC	= 8024,
8301f5207b7SJohn Levon 	NO_SIDE_EFFECT  = 8025,
8311f5207b7SJohn Levon 	FN_ARG_LINK	= 8028,
8321f5207b7SJohn Levon 	DATA_VALUE	= 8029,
8331f5207b7SJohn Levon 	ARRAYSIZE_ARG	= 8033,
8341f5207b7SJohn Levon 	SIZEOF_ARG	= 8034,
8351f5207b7SJohn Levon 	MEMORY_TAG	= 8036,
8361f5207b7SJohn Levon 	MTAG_ASSIGN	= 8035,
8371f5207b7SJohn Levon 	STRING_VALUE	= 8041,
8381f5207b7SJohn Levon };
8391f5207b7SJohn Levon 
8401f5207b7SJohn Levon extern struct sqlite3 *smatch_db;
8411f5207b7SJohn Levon extern struct sqlite3 *mem_db;
8421f5207b7SJohn Levon extern struct sqlite3 *cache_db;
8431f5207b7SJohn Levon 
8441f5207b7SJohn Levon void db_ignore_states(int id);
8451f5207b7SJohn Levon void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type);
8461f5207b7SJohn Levon void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm));
8471f5207b7SJohn Levon void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr));
8481f5207b7SJohn 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));
8491f5207b7SJohn Levon void select_call_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value));
8501f5207b7SJohn Levon void select_return_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value));
8511f5207b7SJohn Levon struct range_list *db_return_vals(struct expression *expr);
8521f5207b7SJohn Levon struct range_list *db_return_vals_from_str(const char *fn_name);
8531f5207b7SJohn Levon char *return_state_to_var_sym(struct expression *expr, int param, const char *key, struct symbol **sym);
8541f5207b7SJohn Levon char *get_chunk_from_key(struct expression *arg, char *key, struct symbol **sym, struct var_sym_list **vsl);
8551f5207b7SJohn Levon char *get_variable_from_key(struct expression *arg, const char *key, struct symbol **sym);
8561f5207b7SJohn Levon const char *state_name_to_param_name(const char *state_name, const char *param_name);
8571f5207b7SJohn Levon const char *get_param_name_var_sym(const char *name, struct symbol *sym);
8581f5207b7SJohn Levon const char *get_param_name(struct sm_state *sm);
8591f5207b7SJohn Levon const char *get_mtag_name_var_sym(const char *state_name, struct symbol *sym);
8601f5207b7SJohn Levon const char *get_mtag_name_expr(struct expression *expr);
8611f5207b7SJohn Levon char *get_data_info_name(struct expression *expr);
8621f5207b7SJohn Levon 
8631f5207b7SJohn Levon char *escape_newlines(const char *str);
8641f5207b7SJohn Levon void sql_exec(struct sqlite3 *db, int (*callback)(void*, int, char**, char**), void *data, const char *sql);
8651f5207b7SJohn Levon 
8661f5207b7SJohn Levon #define sql_helper(db, call_back, data, sql...)					\
8671f5207b7SJohn Levon do {										\
8681f5207b7SJohn Levon 	char sql_txt[1024];							\
8691f5207b7SJohn Levon 										\
8701f5207b7SJohn Levon 	sqlite3_snprintf(sizeof(sql_txt), sql_txt, sql);			\
8711f5207b7SJohn Levon 	sm_debug("debug: %s\n", sql_txt);					\
8721f5207b7SJohn Levon 	sql_exec(db, call_back, data, sql_txt);					\
8731f5207b7SJohn Levon } while (0)
8741f5207b7SJohn Levon 
8751f5207b7SJohn Levon 
8761f5207b7SJohn Levon #define run_sql(call_back, data, sql...)					\
8771f5207b7SJohn Levon do {										\
8781f5207b7SJohn Levon 	if (option_no_db)							\
8791f5207b7SJohn Levon 		break;								\
8801f5207b7SJohn Levon 	sql_helper(smatch_db, call_back, data, sql);				\
8811f5207b7SJohn Levon } while (0)
8821f5207b7SJohn Levon 
8831f5207b7SJohn Levon #define mem_sql(call_back, data, sql...)					\
8841f5207b7SJohn Levon 	sql_helper(mem_db, call_back, data, sql)
8851f5207b7SJohn Levon 
8861f5207b7SJohn Levon #define cache_sql(call_back, data, sql...)					\
8871f5207b7SJohn Levon 	sql_helper(cache_db, call_back, data, sql)
8881f5207b7SJohn Levon 
8891f5207b7SJohn Levon #define sql_insert_helper(table, db, ignore, late, values...)			\
8901f5207b7SJohn Levon do {										\
8911f5207b7SJohn Levon 	struct sqlite3 *_db = db;						\
8921f5207b7SJohn Levon 										\
8931f5207b7SJohn Levon 	if (__inline_fn && !_db)						\
8941f5207b7SJohn Levon 		_db = mem_db;							\
8951f5207b7SJohn Levon 	if (_db) {								\
8961f5207b7SJohn Levon 		char buf[1024];							\
8971f5207b7SJohn Levon 		char *err, *p = buf;						\
8981f5207b7SJohn Levon 		int rc;								\
8991f5207b7SJohn Levon 										\
9001f5207b7SJohn Levon 		p += snprintf(p, buf + sizeof(buf) - p,				\
9011f5207b7SJohn Levon 			      "insert %sinto %s values (",			\
9021f5207b7SJohn Levon 			      ignore ? "or ignore " : "", #table);		\
9031f5207b7SJohn Levon 		p += snprintf(p, buf + sizeof(buf) - p, values);		\
9041f5207b7SJohn Levon 		p += snprintf(p, buf + sizeof(buf) - p, ");");			\
9051f5207b7SJohn Levon 		sm_debug("mem-db: %s\n", buf);					\
9061f5207b7SJohn Levon 		rc = sqlite3_exec(_db, buf, NULL, NULL, &err);			\
9071f5207b7SJohn Levon 		if (rc != SQLITE_OK) {						\
9081f5207b7SJohn Levon 			sm_ierror("SQL error #2: %s", err);			\
9091f5207b7SJohn Levon 			sm_ierror("SQL: '%s'", buf);				\
9101f5207b7SJohn Levon 			parse_error = 1;					\
9111f5207b7SJohn Levon 		}								\
9121f5207b7SJohn Levon 		break;								\
9131f5207b7SJohn Levon 	}									\
9141f5207b7SJohn Levon 	if (option_info) {							\
9151f5207b7SJohn Levon 		FILE *tmp_fd = sm_outfd;					\
9161f5207b7SJohn Levon 		sm_outfd = sql_outfd;						\
9171f5207b7SJohn Levon 		sm_prefix();							\
9181f5207b7SJohn Levon 	        sm_printf("SQL%s: insert %sinto " #table " values(",		\
9191f5207b7SJohn Levon 			  late ? "_late" : "", ignore ? "or ignore " : "");	\
9201f5207b7SJohn Levon 	        sm_printf(values);						\
9211f5207b7SJohn Levon 	        sm_printf(");\n");						\
9221f5207b7SJohn Levon 		sm_outfd = tmp_fd;						\
9231f5207b7SJohn Levon 	}									\
9241f5207b7SJohn Levon } while (0)
9251f5207b7SJohn Levon 
9261f5207b7SJohn Levon #define sql_insert(table, values...) sql_insert_helper(table, 0, 0, 0, values);
9271f5207b7SJohn Levon #define sql_insert_or_ignore(table, values...) sql_insert_helper(table, 0, 1, 0, values);
9281f5207b7SJohn Levon #define sql_insert_late(table, values...) sql_insert_helper(table, 0, 0, 1, values);
9291f5207b7SJohn Levon #define sql_insert_cache(table, values...) sql_insert_helper(table, cache_db, 1, 0, values);
9301f5207b7SJohn Levon 
9311f5207b7SJohn Levon char *get_static_filter(struct symbol *sym);
9321f5207b7SJohn Levon 
9331f5207b7SJohn Levon void sql_insert_return_states(int return_id, const char *return_ranges,
9341f5207b7SJohn Levon 		int type, int param, const char *key, const char *value);
9351f5207b7SJohn Levon void sql_insert_caller_info(struct expression *call, int type, int param,
9361f5207b7SJohn Levon 		const char *key, const char *value);
9371f5207b7SJohn Levon void sql_insert_function_ptr(const char *fn, const char *struct_name);
9381f5207b7SJohn Levon void sql_insert_return_values(const char *return_values);
9391f5207b7SJohn Levon void sql_insert_return_implies(int type, int param, const char *key, const char *value);
9401f5207b7SJohn Levon void sql_insert_function_type_size(const char *member, const char *ranges);
9411f5207b7SJohn Levon void sql_insert_function_type_info(int type, const char *struct_type, const char *member, const char *value);
9421f5207b7SJohn Levon void sql_insert_type_info(int type, const char *member, const char *value);
9431f5207b7SJohn Levon void sql_insert_local_values(const char *name, const char *value);
9441f5207b7SJohn Levon void sql_insert_function_type_value(const char *type, const char *value);
9451f5207b7SJohn Levon void sql_insert_function_type(int param, const char *value);
9461f5207b7SJohn Levon void sql_insert_parameter_name(int param, const char *value);
9471f5207b7SJohn Levon void sql_insert_data_info(struct expression *data, int type, const char *value);
9481f5207b7SJohn Levon void sql_insert_data_info_var_sym(const char *var, struct symbol *sym, int type, const char *value);
9491f5207b7SJohn Levon void sql_save_constraint(const char *con);
9501f5207b7SJohn Levon void sql_save_constraint_required(const char *data, int op, const char *limit);
9511f5207b7SJohn Levon void sql_copy_constraint_required(const char *new_limit, const char *old_limit);
9521f5207b7SJohn Levon void sql_insert_fn_ptr_data_link(const char *ptr, const char *data);
9531f5207b7SJohn Levon void sql_insert_fn_data_link(struct expression *fn, int type, int param, const char *key, const char *value);
9541f5207b7SJohn Levon void sql_insert_mtag_about(mtag_t tag, const char *left_name, const char *right_name);
9551f5207b7SJohn Levon void insert_mtag_data(sval_t sval, struct range_list *rl);
9561f5207b7SJohn Levon void sql_insert_mtag_map(mtag_t tag, int offset, mtag_t container);
9571f5207b7SJohn Levon void sql_insert_mtag_alias(mtag_t orig, mtag_t alias);
9581f5207b7SJohn Levon int mtag_map_select_container(mtag_t tag, int offset, mtag_t *container);
9591f5207b7SJohn Levon int mtag_map_select_tag(mtag_t container, int offset, mtag_t *tag);
9601f5207b7SJohn Levon 
9611f5207b7SJohn Levon void sql_select_return_states(const char *cols, struct expression *call,
9621f5207b7SJohn Levon 	int (*callback)(void*, int, char**, char**), void *info);
9631f5207b7SJohn Levon void sql_select_call_implies(const char *cols, struct expression *call,
9641f5207b7SJohn Levon 	int (*callback)(void*, int, char**, char**));
9651f5207b7SJohn Levon 
9661f5207b7SJohn Levon void open_smatch_db(char *db_file);
9671f5207b7SJohn Levon 
9681f5207b7SJohn Levon /* smatch_files.c */
9691f5207b7SJohn Levon int open_data_file(const char *filename);
9701f5207b7SJohn Levon int open_schema_file(const char *schema);
9711f5207b7SJohn Levon struct token *get_tokens_file(const char *filename);
9721f5207b7SJohn Levon 
9731f5207b7SJohn Levon /* smatch.c */
9741f5207b7SJohn Levon extern char *option_debug_check;
9751f5207b7SJohn Levon extern char *option_project_str;
9761f5207b7SJohn Levon extern char *bin_dir;
9771f5207b7SJohn Levon extern char *data_dir;
9781f5207b7SJohn Levon extern int option_no_data;
9791f5207b7SJohn Levon extern int option_full_path;
9801f5207b7SJohn Levon extern int option_param_mapper;
9811f5207b7SJohn Levon extern int option_call_tree;
9821f5207b7SJohn Levon extern int num_checks;
9831f5207b7SJohn Levon 
9841f5207b7SJohn Levon enum project_type {
9851f5207b7SJohn Levon 	PROJ_NONE,
9861f5207b7SJohn Levon 	PROJ_KERNEL,
9871f5207b7SJohn Levon 	PROJ_WINE,
9881f5207b7SJohn Levon 	PROJ_ILLUMOS_KERNEL,
9891f5207b7SJohn Levon 	PROJ_ILLUMOS_USER,
9901f5207b7SJohn Levon 	PROJ_UNKNOWN,
9911f5207b7SJohn Levon };
9921f5207b7SJohn Levon extern enum project_type option_project;
9931f5207b7SJohn Levon const char *check_name(unsigned short id);
9941f5207b7SJohn Levon int id_from_name(const char *name);
9951f5207b7SJohn Levon 
9961f5207b7SJohn Levon 
9971f5207b7SJohn Levon /* smatch_buf_size.c */
9981f5207b7SJohn Levon int get_array_size(struct expression *expr);
9991f5207b7SJohn Levon int get_array_size_bytes(struct expression *expr);
10001f5207b7SJohn Levon int get_array_size_bytes_min(struct expression *expr);
10011f5207b7SJohn Levon int get_array_size_bytes_max(struct expression *expr);
10021f5207b7SJohn Levon struct range_list *get_array_size_bytes_rl(struct expression *expr);
10031f5207b7SJohn Levon int get_real_array_size(struct expression *expr);
10041f5207b7SJohn Levon int last_member_is_resizable(struct symbol *type);
10051f5207b7SJohn Levon /* smatch_strlen.c */
10061f5207b7SJohn Levon int get_implied_strlen(struct expression *expr, struct range_list **rl);
10071f5207b7SJohn Levon int get_size_from_strlen(struct expression *expr);
10081f5207b7SJohn Levon 
10091f5207b7SJohn Levon /* smatch_capped.c */
10101f5207b7SJohn Levon int is_capped(struct expression *expr);
10111f5207b7SJohn Levon int is_capped_var_sym(const char *name, struct symbol *sym);
10121f5207b7SJohn Levon 
10131f5207b7SJohn Levon /* check_user_data.c */
10141f5207b7SJohn Levon int is_user_macro(struct expression *expr);
10151f5207b7SJohn Levon int is_user_data(struct expression *expr);
10161f5207b7SJohn Levon int is_capped_user_data(struct expression *expr);
10171f5207b7SJohn Levon int implied_user_data(struct expression *expr, struct range_list **rl);
10181f5207b7SJohn Levon struct stree *get_user_stree(void);
10191f5207b7SJohn Levon int get_user_rl(struct expression *expr, struct range_list **rl);
10201f5207b7SJohn Levon int get_user_rl_spammy(struct expression *expr, struct range_list **rl);
10211f5207b7SJohn Levon int is_user_rl(struct expression *expr);
10221f5207b7SJohn Levon int get_user_rl_var_sym(const char *name, struct symbol *sym, struct range_list **rl);
10231f5207b7SJohn Levon 
10241f5207b7SJohn Levon /* check_locking.c */
10251f5207b7SJohn Levon void print_held_locks();
10261f5207b7SJohn Levon 
10271f5207b7SJohn Levon /* check_assigned_expr.c */
10281f5207b7SJohn Levon struct expression *get_assigned_expr(struct expression *expr);
10291f5207b7SJohn Levon struct expression *get_assigned_expr_name_sym(const char *name, struct symbol *sym);
10301f5207b7SJohn Levon /* smatch_return_to_param.c */
10311f5207b7SJohn Levon void __add_return_to_param_mapping(struct expression *assign, const char *return_string);
10321f5207b7SJohn Levon char *map_call_to_param_name_sym(struct expression *expr, struct symbol **sym);
10331f5207b7SJohn Levon 
10341f5207b7SJohn Levon /* smatch_comparison.c */
10351f5207b7SJohn Levon struct compare_data {
10361f5207b7SJohn Levon 	/* The ->left and ->right expression pointers might be NULL (I'm lazy) */
10371f5207b7SJohn Levon 	struct expression *left;
10381f5207b7SJohn Levon 	const char *left_var;
10391f5207b7SJohn Levon 	struct var_sym_list *left_vsl;
10401f5207b7SJohn Levon 	int comparison;
10411f5207b7SJohn Levon 	struct expression *right;
10421f5207b7SJohn Levon 	const char *right_var;
10431f5207b7SJohn Levon 	struct var_sym_list *right_vsl;
10441f5207b7SJohn Levon };
10451f5207b7SJohn Levon DECLARE_ALLOCATOR(compare_data);
10461f5207b7SJohn Levon struct smatch_state *alloc_compare_state(
10471f5207b7SJohn Levon 		struct expression *left,
10481f5207b7SJohn Levon 		const char *left_var, struct var_sym_list *left_vsl,
10491f5207b7SJohn Levon 		int comparison,
10501f5207b7SJohn Levon 		struct expression *right,
10511f5207b7SJohn Levon 		const char *right_var, struct var_sym_list *right_vsl);
10521f5207b7SJohn Levon int filter_comparison(int orig, int op);
10531f5207b7SJohn Levon int merge_comparisons(int one, int two);
10541f5207b7SJohn Levon int combine_comparisons(int left_compare, int right_compare);
10551f5207b7SJohn Levon int state_to_comparison(struct smatch_state *state);
10561f5207b7SJohn Levon struct smatch_state *merge_compare_states(struct smatch_state *s1, struct smatch_state *s2);
10571f5207b7SJohn Levon int get_comparison(struct expression *left, struct expression *right);
10581f5207b7SJohn Levon int get_comparison_strings(const char *one, const char *two);
10591f5207b7SJohn Levon int possible_comparison(struct expression *a, int comparison, struct expression *b);
10601f5207b7SJohn Levon struct state_list *get_all_comparisons(struct expression *expr);
10611f5207b7SJohn Levon struct state_list *get_all_possible_equal_comparisons(struct expression *expr);
10621f5207b7SJohn Levon void __add_return_comparison(struct expression *call, const char *range);
10631f5207b7SJohn Levon void __add_comparison_info(struct expression *expr, struct expression *call, const char *range);
10641f5207b7SJohn Levon char *get_printed_param_name(struct expression *call, const char *param_name, struct symbol *param_sym);
10651f5207b7SJohn Levon char *name_sym_to_param_comparison(const char *name, struct symbol *sym);
10661f5207b7SJohn Levon char *expr_equal_to_param(struct expression *expr, int ignore);
10671f5207b7SJohn Levon char *expr_lte_to_param(struct expression *expr, int ignore);
10681f5207b7SJohn Levon char *expr_param_comparison(struct expression *expr, int ignore);
10691f5207b7SJohn Levon int flip_comparison(int op);
10701f5207b7SJohn Levon int negate_comparison(int op);
10711f5207b7SJohn Levon int remove_unsigned_from_comparison(int op);
10721f5207b7SJohn Levon int param_compare_limit_is_impossible(struct expression *expr, int left_param, char *left_key, char *value);
10731f5207b7SJohn Levon void filter_by_comparison(struct range_list **rl, int comparison, struct range_list *right);
10741f5207b7SJohn Levon struct sm_state *comparison_implication_hook(struct expression *expr,
10751f5207b7SJohn Levon 			struct state_list **true_stack,
10761f5207b7SJohn Levon 			struct state_list **false_stack);
10771f5207b7SJohn Levon void __compare_param_limit_hook(struct expression *left_expr, struct expression *right_expr,
10781f5207b7SJohn Levon 				const char *state_name,
10791f5207b7SJohn Levon 				struct smatch_state *true_state, struct smatch_state *false_state);
10801f5207b7SJohn Levon int impossibly_high_comparison(struct expression *expr);
10811f5207b7SJohn Levon 
10821f5207b7SJohn Levon /* smatch_sval.c */
10831f5207b7SJohn Levon sval_t *sval_alloc(sval_t sval);
10841f5207b7SJohn Levon sval_t *sval_alloc_permanent(sval_t sval);
10851f5207b7SJohn Levon sval_t sval_blank(struct expression *expr);
10861f5207b7SJohn Levon sval_t sval_type_val(struct symbol *type, long long val);
10871f5207b7SJohn Levon sval_t sval_from_val(struct expression *expr, long long val);
10881f5207b7SJohn Levon int sval_is_ptr(sval_t sval);
10891f5207b7SJohn Levon int sval_unsigned(sval_t sval);
10901f5207b7SJohn Levon int sval_signed(sval_t sval);
10911f5207b7SJohn Levon int sval_bits(sval_t sval);
10921f5207b7SJohn Levon int sval_bits_used(sval_t sval);
10931f5207b7SJohn Levon int sval_is_negative(sval_t sval);
10941f5207b7SJohn Levon int sval_is_positive(sval_t sval);
10951f5207b7SJohn Levon int sval_is_min(sval_t sval);
10961f5207b7SJohn Levon int sval_is_max(sval_t sval);
10971f5207b7SJohn Levon int sval_is_a_min(sval_t sval);
10981f5207b7SJohn Levon int sval_is_a_max(sval_t sval);
10991f5207b7SJohn Levon int sval_is_negative_min(sval_t sval);
11001f5207b7SJohn Levon int sval_cmp_t(struct symbol *type, sval_t one, sval_t two);
11011f5207b7SJohn Levon int sval_cmp_val(sval_t one, long long val);
11021f5207b7SJohn Levon sval_t sval_min(sval_t one, sval_t two);
11031f5207b7SJohn Levon sval_t sval_max(sval_t one, sval_t two);
11041f5207b7SJohn Levon int sval_too_low(struct symbol *type, sval_t sval);
11051f5207b7SJohn Levon int sval_too_high(struct symbol *type, sval_t sval);
11061f5207b7SJohn Levon int sval_fits(struct symbol *type, sval_t sval);
11071f5207b7SJohn Levon sval_t sval_cast(struct symbol *type, sval_t sval);
11081f5207b7SJohn Levon sval_t sval_preop(sval_t sval, int op);
11091f5207b7SJohn Levon sval_t sval_binop(sval_t left, int op, sval_t right);
11101f5207b7SJohn Levon int sval_binop_overflows(sval_t left, int op, sval_t right);
11111f5207b7SJohn Levon int sval_binop_overflows_no_sign(sval_t left, int op, sval_t right);
11121f5207b7SJohn Levon unsigned long long fls_mask(unsigned long long uvalue);
11131f5207b7SJohn Levon unsigned long long sval_fls_mask(sval_t sval);
11141f5207b7SJohn Levon const char *sval_to_str(sval_t sval);
11151f5207b7SJohn Levon const char *sval_to_numstr(sval_t sval);
11161f5207b7SJohn Levon sval_t ll_to_sval(long long val);
11171f5207b7SJohn Levon 
11181f5207b7SJohn Levon /* smatch_string_list.c */
11191f5207b7SJohn Levon int list_has_string(struct string_list *str_list, const char *str);
11201f5207b7SJohn Levon void insert_string(struct string_list **str_list, const char *str);
11211f5207b7SJohn Levon struct string_list *clone_str_list(struct string_list *orig);
11221f5207b7SJohn Levon struct string_list *combine_string_lists(struct string_list *one, struct string_list *two);
11231f5207b7SJohn Levon 
11241f5207b7SJohn Levon /* smatch_start_states.c */
11251f5207b7SJohn Levon struct stree *get_start_states(void);
11261f5207b7SJohn Levon 
11271f5207b7SJohn Levon /* smatch_recurse.c */
11281f5207b7SJohn Levon int has_symbol(struct expression *expr, struct symbol *sym);
11291f5207b7SJohn Levon int has_variable(struct expression *expr, struct expression *var);
11301f5207b7SJohn Levon int has_inc_dec(struct expression *expr);
11311f5207b7SJohn Levon 
11321f5207b7SJohn Levon /* smatch_stored_conditions.c */
11331f5207b7SJohn Levon struct smatch_state *get_stored_condition(struct expression *expr);
11341f5207b7SJohn Levon struct expression_list *get_conditions(struct expression *expr);
11351f5207b7SJohn Levon struct sm_state *stored_condition_implication_hook(struct expression *expr,
11361f5207b7SJohn Levon 			struct state_list **true_stack,
11371f5207b7SJohn Levon 			struct state_list **false_stack);
11381f5207b7SJohn Levon 
11391f5207b7SJohn Levon /* check_string_len.c */
11401f5207b7SJohn Levon int get_formatted_string_size(struct expression *call, int arg);
11411f5207b7SJohn Levon 
11421f5207b7SJohn Levon /* smatch_param_set.c */
11431f5207b7SJohn Levon int param_was_set(struct expression *expr);
11441f5207b7SJohn Levon int param_was_set_var_sym(const char *name, struct symbol *sym);
11451f5207b7SJohn Levon /* smatch_param_filter.c */
11461f5207b7SJohn Levon int param_has_filter_data(struct sm_state *sm);
11471f5207b7SJohn Levon 
11481f5207b7SJohn Levon /* smatch_links.c */
11491f5207b7SJohn Levon void set_up_link_functions(int id, int linkid);
11501f5207b7SJohn Levon struct smatch_state *merge_link_states(struct smatch_state *s1, struct smatch_state *s2);
11511f5207b7SJohn Levon void store_link(int link_id, const char *name, struct symbol *sym, const char *link_name, struct symbol *link_sym);
11521f5207b7SJohn Levon 
11531f5207b7SJohn Levon /* smatch_auto_copy.c */
11541f5207b7SJohn Levon void set_auto_copy(int owner);
11551f5207b7SJohn Levon 
11561f5207b7SJohn Levon /* check_buf_comparison */
11571f5207b7SJohn Levon struct expression *get_size_variable(struct expression *buf);
11581f5207b7SJohn Levon struct expression *get_array_variable(struct expression *size);
11591f5207b7SJohn Levon 
11601f5207b7SJohn Levon /* smatch_untracked_param.c */
11611f5207b7SJohn Levon void mark_untracked(struct expression *expr, int param, const char *key, const char *value);
11621f5207b7SJohn Levon void add_untracked_param_hook(void (func)(struct expression *call, int param));
11631f5207b7SJohn Levon void mark_all_params_untracked(int return_id, char *return_ranges, struct expression *expr);
11641f5207b7SJohn Levon 
11651f5207b7SJohn Levon /* smatch_strings.c */
11661f5207b7SJohn Levon struct state_list *get_strings(struct expression *expr);
11671f5207b7SJohn Levon struct expression *fake_string_from_mtag(mtag_t tag);
11681f5207b7SJohn Levon 
11691f5207b7SJohn Levon /* smatch_estate.c */
11701f5207b7SJohn Levon int estate_get_single_value(struct smatch_state *state, sval_t *sval);
11711f5207b7SJohn Levon 
11721f5207b7SJohn Levon /* smatch_address.c */
11731f5207b7SJohn Levon int get_address_rl(struct expression *expr, struct range_list **rl);
11741f5207b7SJohn Levon int get_member_offset(struct symbol *type, const char *member_name);
11751f5207b7SJohn Levon int get_member_offset_from_deref(struct expression *expr);
11761f5207b7SJohn Levon 
11771f5207b7SJohn Levon /* for now this is in smatch_used_parameter.c */
11781f5207b7SJohn Levon void __get_state_hook(int owner, const char *name, struct symbol *sym);
11791f5207b7SJohn Levon 
11801f5207b7SJohn Levon /* smatch_buf_comparison.c */
11811f5207b7SJohn Levon int db_var_is_array_limit(struct expression *array, const char *name, struct var_sym_list *vsl);
11821f5207b7SJohn Levon 
11831f5207b7SJohn Levon struct stree *get_all_return_states(void);
11841f5207b7SJohn Levon struct stree_stack *get_all_return_strees(void);
11851f5207b7SJohn Levon int on_atomic_dec_path(void);
11861f5207b7SJohn Levon int was_inced(const char *name, struct symbol *sym);
11871f5207b7SJohn Levon 
11881f5207b7SJohn Levon /* smatch_constraints.c */
11891f5207b7SJohn Levon char *get_constraint_str(struct expression *expr);
11901f5207b7SJohn Levon struct constraint_list *get_constraints(struct expression *expr);
11911f5207b7SJohn Levon char *unmet_constraint(struct expression *data, struct expression *offset);
11921f5207b7SJohn Levon char *get_required_constraint(const char *data_str);
11931f5207b7SJohn Levon 
11941f5207b7SJohn Levon /* smatch_container_of.c */
11951f5207b7SJohn Levon int get_param_from_container_of(struct expression *expr);
11961f5207b7SJohn Levon int get_offset_from_container_of(struct expression *expr);
11971f5207b7SJohn Levon 
11981f5207b7SJohn Levon /* smatch_mtag.c */
11991f5207b7SJohn Levon int get_string_mtag(struct expression *expr, mtag_t *tag);
12001f5207b7SJohn Levon int get_toplevel_mtag(struct symbol *sym, mtag_t *tag);
12011f5207b7SJohn Levon int get_mtag(struct expression *expr, mtag_t *tag);
12021f5207b7SJohn Levon int get_mtag_offset(struct expression *expr, mtag_t *tag, int *offset);
12031f5207b7SJohn Levon int create_mtag_alias(mtag_t tag, struct expression *expr, mtag_t *new);
12041f5207b7SJohn Levon int expr_to_mtag_offset(struct expression *expr, mtag_t *tag, int *offset);
12051f5207b7SJohn Levon void update_mtag_data(struct expression *expr);
12061f5207b7SJohn Levon int get_mtag_sval(struct expression *expr, sval_t *sval);
12071f5207b7SJohn Levon int get_mtag_addr_sval(struct expression *expr, sval_t *sval);
12081f5207b7SJohn Levon 
12091f5207b7SJohn Levon /* Trinity fuzzer stuff */
12101f5207b7SJohn Levon const char *get_syscall_arg_type(struct symbol *sym);
12111f5207b7SJohn Levon 
12121f5207b7SJohn Levon /* smatch_mem_tracker.c */
12131f5207b7SJohn Levon extern int option_mem;
12141f5207b7SJohn Levon unsigned long get_max_memory(void);
12151f5207b7SJohn Levon 
12161f5207b7SJohn Levon /* check_is_nospec.c */
12171f5207b7SJohn Levon bool is_nospec(struct expression *expr);
12181f5207b7SJohn Levon 
12191f5207b7SJohn Levon /* smatch_nul_terminator.c */
12201f5207b7SJohn Levon bool is_nul_terminated(struct expression *expr);
12211f5207b7SJohn Levon 
12221f5207b7SJohn Levon static inline int type_bits(struct symbol *type)
12231f5207b7SJohn Levon {
12241f5207b7SJohn Levon 	if (!type)
12251f5207b7SJohn Levon 		return 0;
12261f5207b7SJohn Levon 	if (type->type == SYM_PTR)  /* Sparse doesn't set this for &pointers */
12271f5207b7SJohn Levon 		return bits_in_pointer;
12281f5207b7SJohn Levon 	if (type->type == SYM_ARRAY)
12291f5207b7SJohn Levon 		return bits_in_pointer;
12301f5207b7SJohn Levon 	if (!type->examined)
12311f5207b7SJohn Levon 		examine_symbol_type(type);
12321f5207b7SJohn Levon 	return type->bit_size;
12331f5207b7SJohn Levon }
12341f5207b7SJohn Levon 
12351f5207b7SJohn Levon static inline bool type_is_ptr(struct symbol *type)
12361f5207b7SJohn Levon {
12371f5207b7SJohn Levon 	return type && (type->type == SYM_PTR || type->type == SYM_ARRAY);
12381f5207b7SJohn Levon }
12391f5207b7SJohn Levon 
12401f5207b7SJohn Levon static inline int type_unsigned(struct symbol *base_type)
12411f5207b7SJohn Levon {
12421f5207b7SJohn Levon 	if (!base_type)
12431f5207b7SJohn Levon 		return 0;
12441f5207b7SJohn Levon 	if (base_type->ctype.modifiers & MOD_UNSIGNED)
12451f5207b7SJohn Levon 		return 1;
12461f5207b7SJohn Levon 	return 0;
12471f5207b7SJohn Levon }
12481f5207b7SJohn Levon 
12491f5207b7SJohn Levon static inline int type_positive_bits(struct symbol *type)
12501f5207b7SJohn Levon {
12511f5207b7SJohn Levon 	if (!type)
12521f5207b7SJohn Levon 		return 0;
12531f5207b7SJohn Levon 	if (type->type == SYM_ARRAY)
12541f5207b7SJohn Levon 		return bits_in_pointer - 1;
12551f5207b7SJohn Levon 	if (type_unsigned(type))
12561f5207b7SJohn Levon 		return type_bits(type);
12571f5207b7SJohn Levon 	return type_bits(type) - 1;
12581f5207b7SJohn Levon }
12591f5207b7SJohn Levon 
12601f5207b7SJohn Levon static inline int sval_positive_bits(sval_t sval)
12611f5207b7SJohn Levon {
12621f5207b7SJohn Levon 	return type_positive_bits(sval.type);
12631f5207b7SJohn Levon }
12641f5207b7SJohn Levon 
12651f5207b7SJohn Levon /*
12661f5207b7SJohn Levon  * Returns -1 if one is smaller, 0 if they are the same and 1 if two is larger.
12671f5207b7SJohn Levon  */
12681f5207b7SJohn Levon static inline int sval_cmp(sval_t one, sval_t two)
12691f5207b7SJohn Levon {
12701f5207b7SJohn Levon 	struct symbol *type;
12711f5207b7SJohn Levon 
12721f5207b7SJohn Levon 	type = one.type;
12731f5207b7SJohn Levon 	if (sval_positive_bits(two) > sval_positive_bits(one))
12741f5207b7SJohn Levon 		type = two.type;
12751f5207b7SJohn Levon 	if (type_bits(type) < 31)
12761f5207b7SJohn Levon 		type = &int_ctype;
12771f5207b7SJohn Levon 
12781f5207b7SJohn Levon 	one = sval_cast(type, one);
12791f5207b7SJohn Levon 	two = sval_cast(type, two);
12801f5207b7SJohn Levon 
12811f5207b7SJohn Levon 	if (type_unsigned(type)) {
12821f5207b7SJohn Levon 		if (one.uvalue < two.uvalue)
12831f5207b7SJohn Levon 			return -1;
12841f5207b7SJohn Levon 		if (one.uvalue == two.uvalue)
12851f5207b7SJohn Levon 			return 0;
12861f5207b7SJohn Levon 		return 1;
12871f5207b7SJohn Levon 	}
12881f5207b7SJohn Levon 	/* fix me handle type promotion and unsigned values */
12891f5207b7SJohn Levon 	if (one.value < two.value)
12901f5207b7SJohn Levon 		return -1;
12911f5207b7SJohn Levon 	if (one.value == two.value)
12921f5207b7SJohn Levon 		return 0;
12931f5207b7SJohn Levon 	return 1;
12941f5207b7SJohn Levon }
12951f5207b7SJohn Levon 
12961f5207b7SJohn Levon #endif 	    /* !SMATCH_H_ */
1297