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