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