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