trinity: Proof of concept to auto-generate trinity spec files
[smatch.git] / smatch.h
blob6dae6934feabc3dc4b0847ffd4774c6e265270bc
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_silenced_function(void);
207 /* smatch_impossible.c */
208 int is_impossible_path(void);
209 void set_path_impossible(void);
211 extern FILE *sm_outfd;
212 extern FILE *sql_outfd;
213 extern FILE *caller_info_fd;
214 #define sm_printf(msg...) do { if (final_pass || option_debug || local_debug) fprintf(sm_outfd, msg); } while (0)
216 static inline void sm_prefix(void)
218 sm_printf("%s:%d %s() ", get_filename(), get_lineno(), get_function());
221 static inline void print_implied_debug_msg();
223 extern bool __silence_warnings_for_stmt;
225 #define sm_msg(msg...) \
226 do { \
227 print_implied_debug_msg(); \
228 if (!final_pass && !option_debug && !local_debug) \
229 break; \
230 if (__silence_warnings_for_stmt && !option_debug && !local_debug) \
231 break; \
232 if (!option_info && is_silenced_function()) \
233 break; \
234 sm_prefix(); \
235 sm_printf(msg); \
236 sm_printf("\n"); \
237 } while (0)
239 #define local_debug(msg...) \
240 do { \
241 if (local_debug) \
242 sm_msg(msg); \
243 } while (0)
245 extern char *implied_debug_msg;
246 static inline void print_implied_debug_msg(void)
248 static struct symbol *last_printed = NULL;
250 if (!implied_debug_msg)
251 return;
252 if (last_printed == cur_func_sym)
253 return;
254 last_printed = cur_func_sym;
255 sm_msg("%s", implied_debug_msg);
258 #define sm_debug(msg...) do { if (option_debug) sm_printf(msg); } while (0)
260 #define sm_info(msg...) do { \
261 if (option_debug || (option_info && final_pass)) { \
262 sm_prefix(); \
263 sm_printf("info: "); \
264 sm_printf(msg); \
265 sm_printf("\n"); \
267 } while(0)
269 #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
271 struct smatch_state *__get_state(int owner, const char *name, struct symbol *sym);
272 struct smatch_state *get_state(int owner, const char *name, struct symbol *sym);
273 struct smatch_state *get_state_expr(int owner, struct expression *expr);
274 struct state_list *get_possible_states(int owner, const char *name,
275 struct symbol *sym);
276 struct state_list *get_possible_states_expr(int owner, struct expression *expr);
277 struct sm_state *set_state(int owner, const char *name, struct symbol *sym,
278 struct smatch_state *state);
279 struct sm_state *set_state_expr(int owner, struct expression *expr,
280 struct smatch_state *state);
281 void delete_state(int owner, const char *name, struct symbol *sym);
282 void delete_state_expr(int owner, struct expression *expr);
283 void __delete_all_states_sym(struct symbol *sym);
284 void set_true_false_states(int owner, const char *name, struct symbol *sym,
285 struct smatch_state *true_state,
286 struct smatch_state *false_state);
287 void set_true_false_states_expr(int owner, struct expression *expr,
288 struct smatch_state *true_state,
289 struct smatch_state *false_state);
291 struct stree *get_all_states_from_stree(int owner, struct stree *source);
292 struct stree *get_all_states_stree(int id);
293 struct stree *__get_cur_stree(void);
294 int is_reachable(void);
295 void add_get_state_hook(void (*fn)(int owner, const char *name, struct symbol *sym));
297 /* smatch_helper.c */
298 DECLARE_PTR_LIST(int_stack, int);
299 char *alloc_string(const char *str);
300 void free_string(char *str);
301 void append(char *dest, const char *data, int buff_len);
302 void remove_parens(char *str);
303 struct smatch_state *alloc_state_num(int num);
304 struct smatch_state *alloc_state_str(const char *name);
305 struct smatch_state *alloc_state_expr(struct expression *expr);
306 struct expression *get_argument_from_call_expr(struct expression_list *args,
307 int num);
309 char *expr_to_var(struct expression *expr);
310 struct symbol *expr_to_sym(struct expression *expr);
311 char *expr_to_str(struct expression *expr);
312 char *expr_to_str_sym(struct expression *expr,
313 struct symbol **sym_ptr);
314 char *expr_to_var_sym(struct expression *expr,
315 struct symbol **sym_ptr);
316 char *expr_to_known_chunk_sym(struct expression *expr, struct symbol **sym);
317 char *expr_to_chunk_sym_vsl(struct expression *expr, struct symbol **sym, struct var_sym_list **vsl);
318 int get_complication_score(struct expression *expr);
320 int sym_name_is(const char *name, struct expression *expr);
321 int get_const_value(struct expression *expr, sval_t *sval);
322 int get_value(struct expression *expr, sval_t *val);
323 int get_implied_value(struct expression *expr, sval_t *val);
324 int get_implied_min(struct expression *expr, sval_t *sval);
325 int get_implied_max(struct expression *expr, sval_t *val);
326 int get_hard_max(struct expression *expr, sval_t *sval);
327 int get_fuzzy_min(struct expression *expr, sval_t *min);
328 int get_fuzzy_max(struct expression *expr, sval_t *max);
329 int get_absolute_min(struct expression *expr, sval_t *sval);
330 int get_absolute_max(struct expression *expr, sval_t *sval);
331 int parse_call_math(struct expression *expr, char *math, sval_t *val);
332 int parse_call_math_rl(struct expression *call, char *math, struct range_list **rl);
333 char *get_value_in_terms_of_parameter_math(struct expression *expr);
334 char *get_value_in_terms_of_parameter_math_var_sym(const char *var, struct symbol *sym);
335 int is_zero(struct expression *expr);
336 int known_condition_true(struct expression *expr);
337 int known_condition_false(struct expression *expr);
338 int implied_condition_true(struct expression *expr);
339 int implied_condition_false(struct expression *expr);
340 int can_integer_overflow(struct symbol *type, struct expression *expr);
341 void clear_math_cache(void);
343 int is_array(struct expression *expr);
344 struct expression *get_array_base(struct expression *expr);
345 struct expression *get_array_offset(struct expression *expr);
346 const char *show_state(struct smatch_state *state);
347 struct statement *get_expression_statement(struct expression *expr);
348 struct expression *strip_parens(struct expression *expr);
349 struct expression *strip_expr(struct expression *expr);
350 struct expression *strip_expr_set_parent(struct expression *expr);
351 void scoped_state(int my_id, const char *name, struct symbol *sym);
352 int is_error_return(struct expression *expr);
353 int getting_address(void);
354 int get_struct_and_member(struct expression *expr, const char **type, const char **member);
355 char *get_member_name(struct expression *expr);
356 char *get_fnptr_name(struct expression *expr);
357 int cmp_pos(struct position pos1, struct position pos2);
358 int positions_eq(struct position pos1, struct position pos2);
359 struct statement *get_current_statement(void);
360 struct statement *get_prev_statement(void);
361 struct expression *get_last_expr_from_expression_stmt(struct expression *expr);
362 int get_param_num_from_sym(struct symbol *sym);
363 int get_param_num(struct expression *expr);
364 int ms_since(struct timeval *start);
365 int parent_is_gone_var_sym(const char *name, struct symbol *sym);
366 int parent_is_gone(struct expression *expr);
367 int invert_op(int op);
368 int expr_equiv(struct expression *one, struct expression *two);
369 void push_int(struct int_stack **stack, int num);
370 int pop_int(struct int_stack **stack);
372 /* smatch_type.c */
373 struct symbol *get_real_base_type(struct symbol *sym);
374 int type_bytes(struct symbol *type);
375 int array_bytes(struct symbol *type);
376 struct symbol *get_pointer_type(struct expression *expr);
377 struct symbol *get_type(struct expression *expr);
378 struct symbol *get_final_type(struct expression *expr);
379 struct symbol *get_promoted_type(struct symbol *left, struct symbol *right);
380 int type_signed(struct symbol *base_type);
381 int expr_unsigned(struct expression *expr);
382 int expr_signed(struct expression *expr);
383 int returns_unsigned(struct symbol *base_type);
384 int is_pointer(struct expression *expr);
385 int returns_pointer(struct symbol *base_type);
386 sval_t sval_type_max(struct symbol *base_type);
387 sval_t sval_type_min(struct symbol *base_type);
388 int nr_bits(struct expression *expr);
389 int is_void_pointer(struct expression *expr);
390 int is_char_pointer(struct expression *expr);
391 int is_string(struct expression *expr);
392 int is_static(struct expression *expr);
393 int is_local_variable(struct expression *expr);
394 int types_equiv(struct symbol *one, struct symbol *two);
395 int fn_static(void);
396 const char *global_static();
397 struct symbol *cur_func_return_type(void);
398 struct symbol *get_arg_type(struct expression *fn, int arg);
399 struct symbol *get_member_type_from_key(struct expression *expr, const char *key);
400 struct symbol *get_arg_type_from_key(struct expression *fn, int param, struct expression *arg, const char *key);
401 int is_struct(struct expression *expr);
402 char *type_to_str(struct symbol *type);
404 /* smatch_ignore.c */
405 void add_ignore(int owner, const char *name, struct symbol *sym);
406 int is_ignored(int owner, const char *name, struct symbol *sym);
407 void add_ignore_expr(int owner, struct expression *expr);
408 int is_ignored_expr(int owner, struct expression *expr);
410 /* smatch_var_sym */
411 struct var_sym *alloc_var_sym(const char *var, struct symbol *sym);
412 struct var_sym_list *expr_to_vsl(struct expression *expr);
413 void add_var_sym(struct var_sym_list **list, const char *var, struct symbol *sym);
414 void add_var_sym_expr(struct var_sym_list **list, struct expression *expr);
415 void del_var_sym(struct var_sym_list **list, const char *var, struct symbol *sym);
416 int in_var_sym_list(struct var_sym_list *list, const char *var, struct symbol *sym);
417 struct var_sym_list *clone_var_sym_list(struct var_sym_list *from_vsl);
418 void merge_var_sym_list(struct var_sym_list **dest, struct var_sym_list *src);
419 struct var_sym_list *combine_var_sym_lists(struct var_sym_list *one, struct var_sym_list *two);
420 int var_sym_lists_equiv(struct var_sym_list *one, struct var_sym_list *two);
421 void free_var_sym_list(struct var_sym_list **list);
422 void free_var_syms_and_list(struct var_sym_list **list);
424 /* smatch_tracker */
425 struct tracker *alloc_tracker(int owner, const char *name, struct symbol *sym);
426 void add_tracker(struct tracker_list **list, int owner, const char *name,
427 struct symbol *sym);
428 void add_tracker_expr(struct tracker_list **list, int owner, struct expression *expr);
429 void del_tracker(struct tracker_list **list, int owner, const char *name,
430 struct symbol *sym);
431 int in_tracker_list(struct tracker_list *list, int owner, const char *name,
432 struct symbol *sym);
433 void free_tracker_list(struct tracker_list **list);
434 void free_trackers_and_list(struct tracker_list **list);
436 /* smatch_conditions */
437 int in_condition(void);
439 /* smatch_flow.c */
441 extern int __in_fake_assign;
442 extern int __in_fake_parameter_assign;
443 extern int __in_fake_struct_assign;
444 extern int in_fake_env;
445 void smatch (int argc, char **argv);
446 int inside_loop(void);
447 int definitely_inside_loop(void);
448 struct expression *get_switch_expr(void);
449 int in_expression_statement(void);
450 void __process_post_op_stack(void);
451 void __split_expr(struct expression *expr);
452 void __split_label_stmt(struct statement *stmt);
453 void __split_stmt(struct statement *stmt);
454 extern int __in_function_def;
455 extern int option_assume_loops;
456 extern int option_two_passes;
457 extern int option_no_db;
458 extern int option_file_output;
459 extern int option_time;
460 extern struct expression_list *big_expression_stack;
461 extern struct expression_list *big_condition_stack;
462 extern struct statement_list *big_statement_stack;
463 int is_assigned_call(struct expression *expr);
464 int inlinable(struct expression *expr);
465 extern int __inline_call;
466 extern struct expression *__inline_fn;
467 extern int __in_pre_condition;
468 extern int __bail_on_rest_of_function;
469 extern struct statement *__prev_stmt;
470 extern struct statement *__cur_stmt;
471 extern struct statement *__next_stmt;
472 void init_fake_env(void);
473 void end_fake_env(void);
474 int time_parsing_function(void);
476 /* smatch_struct_assignment.c */
477 struct expression *get_faked_expression(void);
478 void __fake_struct_member_assignments(struct expression *expr);
480 /* smatch_project.c */
481 int is_no_inline_function(const char *function);
483 /* smatch_conditions */
484 void __split_whole_condition(struct expression *expr);
485 void __handle_logic(struct expression *expr);
486 int is_condition(struct expression *expr);
487 int __handle_condition_assigns(struct expression *expr);
488 int __handle_select_assigns(struct expression *expr);
489 int __handle_expr_statement_assigns(struct expression *expr);
491 /* smatch_implied.c */
492 extern int option_debug_implied;
493 extern int option_debug_related;
494 struct range_list_stack;
495 void param_limit_implications(struct expression *expr, int param, char *key, char *value);
496 struct stree *__implied_case_stree(struct expression *switch_expr,
497 struct range_list *case_rl,
498 struct range_list_stack **remaining_cases,
499 struct stree **raw_stree);
500 void overwrite_states_using_pool(struct sm_state *gate_sm, struct sm_state *pool_sm);
501 int assume(struct expression *expr);
502 void end_assume(void);
503 int impossible_assumption(struct expression *left, int op, sval_t sval);
505 /* smatch_extras.c */
506 #define SMATCH_EXTRA 5 /* this is my_id from smatch extra set in smatch.c */
507 extern int RETURN_ID;
509 struct data_range {
510 sval_t min;
511 sval_t max;
514 #define MTAG_ALIAS_BIT (1ULL << 63)
515 #define MTAG_OFFSET_MASK 0xfffULL
517 extern long long valid_ptr_min, valid_ptr_max;
518 extern sval_t valid_ptr_min_sval, valid_ptr_max_sval;
519 extern struct range_list *valid_ptr_rl;
520 static const sval_t array_min_sval = {
521 .type = &ptr_ctype,
522 {.value = 100000},
524 static const sval_t array_max_sval = {
525 .type = &ptr_ctype,
526 {.value = 199999},
528 static const sval_t text_seg_min = {
529 .type = &ptr_ctype,
530 {.value = 100000000},
532 static const sval_t text_seg_max = {
533 .type = &ptr_ctype,
534 {.value = 177777777},
536 static const sval_t data_seg_min = {
537 .type = &ptr_ctype,
538 {.value = 200000000},
540 static const sval_t data_seg_max = {
541 .type = &ptr_ctype,
542 {.value = 277777777},
544 static const sval_t bss_seg_min = {
545 .type = &ptr_ctype,
546 {.value = 300000000},
548 static const sval_t bss_seg_max = {
549 .type = &ptr_ctype,
550 {.value = 377777777},
552 static const sval_t stack_seg_min = {
553 .type = &ptr_ctype,
554 {.value = 400000000},
556 static const sval_t stack_seg_max = {
557 .type = &ptr_ctype,
558 {.value = 477777777},
560 static const sval_t kmalloc_seg_min = {
561 .type = &ptr_ctype,
562 {.value = 500000000},
564 static const sval_t kmalloc_seg_max = {
565 .type = &ptr_ctype,
566 {.value = 577777777},
568 static const sval_t vmalloc_seg_min = {
569 .type = &ptr_ctype,
570 {.value = 600000000},
572 static const sval_t vmalloc_seg_max = {
573 .type = &ptr_ctype,
574 {.value = 677777777},
576 static const sval_t fn_ptr_min = {
577 .type = &ptr_ctype,
578 {.value = 700000000},
580 static const sval_t fn_ptr_max = {
581 .type = &ptr_ctype,
582 {.value = 777777777},
585 char *get_other_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym);
586 char *map_call_to_other_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym);
587 char *map_long_to_short_name_sym(const char *name, struct symbol *sym, struct symbol **new_sym);
588 char *map_long_to_short_name_sym_nostack(const char *name, struct symbol *sym, struct symbol **new_sym);
590 #define STRLEN_MAX_RET 1010101
592 /* smatch_absolute.c */
593 int get_absolute_min_helper(struct expression *expr, sval_t *sval);
594 int get_absolute_max_helper(struct expression *expr, sval_t *sval);
596 /* smatch_local_values.c */
597 int get_local_rl(struct expression *expr, struct range_list **rl);
598 int get_local_max_helper(struct expression *expr, sval_t *sval);
599 int get_local_min_helper(struct expression *expr, sval_t *sval);
601 /* smatch_type_value.c */
602 int get_db_type_rl(struct expression *expr, struct range_list **rl);
603 /* smatch_data_val.c */
604 int get_mtag_rl(struct expression *expr, struct range_list **rl);
605 /* smatch_array_values.c */
606 int get_array_rl(struct expression *expr, struct range_list **rl);
608 /* smatch_states.c */
609 void __swap_cur_stree(struct stree *stree);
610 void __push_fake_cur_stree();
611 struct stree *__pop_fake_cur_stree();
612 void __free_fake_cur_stree();
613 void __set_fake_cur_stree_fast(struct stree *stree);
614 void __pop_fake_cur_stree_fast(void);
615 void __merge_stree_into_cur(struct stree *stree);
617 int unreachable(void);
618 void __set_sm(struct sm_state *sm);
619 void __set_sm_cur_stree(struct sm_state *sm);
620 void __set_sm_fake_stree(struct sm_state *sm);
621 void __set_true_false_sm(struct sm_state *true_state,
622 struct sm_state *false_state);
623 void nullify_path(void);
624 void __match_nullify_path_hook(const char *fn, struct expression *expr,
625 void *unused);
626 void __unnullify_path(void);
627 int __path_is_null(void);
628 void save_all_states(void);
629 void restore_all_states(void);
630 void free_goto_stack(void);
631 void clear_all_states(void);
633 struct sm_state *get_sm_state(int owner, const char *name,
634 struct symbol *sym);
635 struct sm_state *get_sm_state_expr(int owner, struct expression *expr);
636 void __push_true_states(void);
637 void __use_false_states(void);
638 void __discard_false_states(void);
639 void __merge_false_states(void);
640 void __merge_true_states(void);
642 void __negate_cond_stacks(void);
643 void __use_pre_cond_states(void);
644 void __use_cond_true_states(void);
645 void __use_cond_false_states(void);
646 void __push_cond_stacks(void);
647 void __fold_in_set_states(void);
648 void __free_set_states(void);
649 struct stree *__copy_cond_true_states(void);
650 struct stree *__copy_cond_false_states(void);
651 struct stree *__pop_cond_true_stack(void);
652 struct stree *__pop_cond_false_stack(void);
653 void __and_cond_states(void);
654 void __or_cond_states(void);
655 void __save_pre_cond_states(void);
656 void __discard_pre_cond_states(void);
657 struct stree *__get_true_states(void);
658 struct stree *__get_false_states(void);
659 void __use_cond_states(void);
660 extern struct state_list *__last_base_slist;
662 void __push_continues(void);
663 void __discard_continues(void);
664 void __process_continues(void);
665 void __merge_continues(void);
667 void __push_breaks(void);
668 void __process_breaks(void);
669 int __has_breaks(void);
670 void __merge_breaks(void);
671 void __use_breaks(void);
673 void __save_switch_states(struct expression *switch_expr);
674 void __discard_switches(void);
675 int have_remaining_cases(void);
676 void __merge_switches(struct expression *switch_expr, struct range_list *case_rl);
677 void __push_default(void);
678 void __set_default(void);
679 int __pop_default(void);
681 void __push_conditions(void);
682 void __discard_conditions(void);
684 void __save_gotos(const char *name, struct symbol *sym);
685 void __merge_gotos(const char *name, struct symbol *sym);
687 void __print_cur_stree(void);
689 /* smatch_hooks.c */
690 void __pass_to_client(void *data, enum hook_type type);
691 void __pass_to_client_no_data(enum hook_type type);
692 void __pass_case_to_client(struct expression *switch_expr,
693 struct range_list *rl);
694 int __has_merge_function(int client_id);
695 struct smatch_state *__client_merge_function(int owner,
696 struct smatch_state *s1,
697 struct smatch_state *s2);
698 struct smatch_state *__client_unmatched_state_function(struct sm_state *sm);
699 void call_pre_merge_hook(struct sm_state *sm);
700 void __push_scope_hooks(void);
701 void __call_scope_hooks(void);
703 /* smatch_function_hooks.c */
704 void create_function_hook_hash(void);
705 void __match_initializer_call(struct symbol *sym);
707 /* smatch_db.c */
708 enum info_type {
709 INTERNAL = 0,
711 * Changing these numbers is a pain. Don't do it. If you ever use a
712 * number it can't be re-used right away so there may be gaps.
713 * We select these in order by type so if the order matters, then give
714 * it a number below 100-999,9000-9999 ranges. */
716 PARAM_CLEARED = 101,
717 PARAM_LIMIT = 103,
718 PARAM_FILTER = 104,
720 PARAM_VALUE = 1001,
721 BUF_SIZE = 1002,
722 USER_DATA = 1003,
723 CAPPED_DATA = 1004,
724 RETURN_VALUE = 1005,
725 DEREFERENCE = 1006,
726 RANGE_CAP = 1007,
727 LOCK_HELD = 1008,
728 LOCK_RELEASED = 1009,
729 ABSOLUTE_LIMITS = 1010,
730 PARAM_ADD = 1012,
731 PARAM_FREED = 1013,
732 DATA_SOURCE = 1014,
733 FUZZY_MAX = 1015,
734 STR_LEN = 1016,
735 ARRAY_LEN = 1017,
736 CAPABLE = 1018,
737 NS_CAPABLE = 1019,
738 CONTAINER = 1020,
739 CASTED_CALL = 1021,
740 TYPE_LINK = 1022,
741 UNTRACKED_PARAM = 1023,
742 CULL_PATH = 1024,
743 PARAM_SET = 1025,
744 PARAM_USED = 1026,
745 BYTE_UNITS = 1027,
746 COMPARE_LIMIT = 1028,
747 PARAM_COMPARE = 1029,
748 CONSTRAINT = 1031,
749 PASSES_TYPE = 1032,
750 CONSTRAINT_REQUIRED = 1033,
751 NOSPEC = 1035,
752 NOSPEC_WB = 1036,
753 TERMINATED = 1038,
755 /* put random temporary stuff in the 7000-7999 range for testing */
756 USER_DATA3 = 8017,
757 USER_DATA3_SET = 9017,
758 NO_OVERFLOW = 8018,
759 NO_OVERFLOW_SIMPLE = 8019,
760 LOCKED = 8020,
761 UNLOCKED = 8021,
762 SET_FS = 8022,
763 ATOMIC_INC = 8023,
764 ATOMIC_DEC = 8024,
765 NO_SIDE_EFFECT = 8025,
766 FN_ARG_LINK = 8028,
767 DATA_VALUE = 8029,
768 ARRAYSIZE_ARG = 8033,
769 SIZEOF_ARG = 8034,
770 MEMORY_TAG = 8036,
771 MTAG_ASSIGN = 8035,
772 STRING_VALUE = 8041,
775 extern struct sqlite3 *smatch_db;
776 extern struct sqlite3 *mem_db;
777 extern struct sqlite3 *cache_db;
779 void debug_sql(struct sqlite3 *db, const char *sql);
780 void db_ignore_states(int id);
781 void select_caller_info_hook(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type);
782 void add_member_info_callback(int owner, void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm));
783 void add_split_return_callback(void (*fn)(int return_id, char *return_ranges, struct expression *returned_expr));
784 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));
785 void select_call_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value));
786 void select_return_implies_hook(int type, void (*callback)(struct expression *call, struct expression *arg, char *key, char *value));
787 struct range_list *db_return_vals(struct expression *expr);
788 struct range_list *db_return_vals_from_str(const char *fn_name);
789 char *return_state_to_var_sym(struct expression *expr, int param, const char *key, struct symbol **sym);
790 char *get_chunk_from_key(struct expression *arg, char *key, struct symbol **sym, struct var_sym_list **vsl);
791 char *get_variable_from_key(struct expression *arg, const char *key, struct symbol **sym);
792 const char *state_name_to_param_name(const char *state_name, const char *param_name);
793 const char *get_param_name_var_sym(const char *name, struct symbol *sym);
794 const char *get_param_name(struct sm_state *sm);
795 const char *get_mtag_name_var_sym(const char *state_name, struct symbol *sym);
796 const char *get_mtag_name_expr(struct expression *expr);
797 char *get_data_info_name(struct expression *expr);
799 char *escape_newlines(char *str);
800 void sql_exec(struct sqlite3 *db, int (*callback)(void*, int, char**, char**), void *data, const char *sql);
802 #define sql_helper(db, call_back, data, sql...) \
803 do { \
804 char sql_txt[1024]; \
806 sqlite3_snprintf(sizeof(sql_txt), sql_txt, sql); \
807 sm_debug("debug: %s\n", sql_txt); \
808 debug_sql(db, sql_txt); \
809 sql_exec(db, call_back, data, sql_txt); \
810 } while (0)
813 #define run_sql(call_back, data, sql...) \
814 do { \
815 if (option_no_db) \
816 break; \
817 sql_helper(smatch_db, call_back, data, sql); \
818 } while (0)
820 #define mem_sql(call_back, data, sql...) \
821 sql_helper(mem_db, call_back, data, sql)
823 #define cache_sql(call_back, data, sql...) \
824 sql_helper(cache_db, call_back, data, sql)
826 #define sql_insert_helper(table, db, ignore, late, values...) \
827 do { \
828 struct sqlite3 *_db = db; \
830 if (__inline_fn && !_db) \
831 _db = mem_db; \
832 if (_db) { \
833 char buf[1024]; \
834 char *err, *p = buf; \
835 int rc; \
837 p += snprintf(p, buf + sizeof(buf) - p, \
838 "insert %sinto %s values (", \
839 ignore ? "or ignore " : "", #table); \
840 p += snprintf(p, buf + sizeof(buf) - p, values); \
841 p += snprintf(p, buf + sizeof(buf) - p, ");"); \
842 sm_debug("mem-db: %s\n", buf); \
843 rc = sqlite3_exec(_db, buf, NULL, NULL, &err); \
844 if (rc != SQLITE_OK) { \
845 fprintf(stderr, "SQL error #2: %s\n", err); \
846 fprintf(stderr, "SQL: '%s'\n", buf); \
847 parse_error = 1; \
849 break; \
851 if (option_info) { \
852 FILE *tmp_fd = sm_outfd; \
853 sm_outfd = sql_outfd; \
854 sm_prefix(); \
855 sm_printf("SQL%s: insert %sinto " #table " values(", \
856 late ? "_late" : "", ignore ? "or ignore " : ""); \
857 sm_printf(values); \
858 sm_printf(");\n"); \
859 sm_outfd = tmp_fd; \
861 } while (0)
863 #define sql_insert(table, values...) sql_insert_helper(table, 0, 0, 0, values);
864 #define sql_insert_or_ignore(table, values...) sql_insert_helper(table, 0, 1, 0, values);
865 #define sql_insert_late(table, values...) sql_insert_helper(table, 0, 0, 1, values);
866 #define sql_insert_cache(table, values...) sql_insert_helper(table, cache_db, 1, 0, values);
868 char *get_static_filter(struct symbol *sym);
870 void sql_insert_return_states(int return_id, const char *return_ranges,
871 int type, int param, const char *key, const char *value);
872 void sql_insert_caller_info(struct expression *call, int type, int param,
873 const char *key, const char *value);
874 void sql_insert_function_ptr(const char *fn, const char *struct_name);
875 void sql_insert_return_values(const char *return_values);
876 void sql_insert_return_implies(int type, int param, const char *key, const char *value);
877 void sql_insert_function_type_size(const char *member, const char *ranges);
878 void sql_insert_function_type_info(int type, const char *struct_type, const char *member, const char *value);
879 void sql_insert_type_info(int type, const char *member, const char *value);
880 void sql_insert_local_values(const char *name, const char *value);
881 void sql_insert_function_type_value(const char *type, const char *value);
882 void sql_insert_function_type(int param, const char *value);
883 void sql_insert_parameter_name(int param, const char *value);
884 void sql_insert_data_info(struct expression *data, int type, const char *value);
885 void sql_insert_data_info_var_sym(const char *var, struct symbol *sym, int type, const char *value);
886 void sql_save_constraint(const char *con);
887 void sql_save_constraint_required(const char *data, int op, const char *limit);
888 void sql_copy_constraint_required(const char *new_limit, const char *old_limit);
889 void sql_insert_fn_ptr_data_link(const char *ptr, const char *data);
890 void sql_insert_fn_data_link(struct expression *fn, int type, int param, const char *key, const char *value);
891 void sql_insert_mtag_about(mtag_t tag, const char *left_name, const char *right_name);
892 void insert_mtag_data(sval_t sval, struct range_list *rl);
893 void sql_insert_mtag_map(mtag_t tag, int offset, mtag_t container);
894 void sql_insert_mtag_alias(mtag_t orig, mtag_t alias);
895 int mtag_map_select_container(mtag_t tag, int offset, mtag_t *container);
896 int mtag_map_select_tag(mtag_t container, int offset, mtag_t *tag);
898 void sql_select_return_states(const char *cols, struct expression *call,
899 int (*callback)(void*, int, char**, char**), void *info);
900 void sql_select_call_implies(const char *cols, struct expression *call,
901 int (*callback)(void*, int, char**, char**));
903 void open_smatch_db(char *db_file);
905 /* smatch_files.c */
906 int open_data_file(const char *filename);
907 int open_schema_file(const char *schema);
908 struct token *get_tokens_file(const char *filename);
910 /* smatch.c */
911 extern char *option_debug_check;
912 extern char *option_project_str;
913 extern char *bin_dir;
914 extern char *data_dir;
915 extern int option_no_data;
916 extern int option_full_path;
917 extern int option_param_mapper;
918 extern int option_call_tree;
919 extern int num_checks;
921 enum project_type {
922 PROJ_NONE,
923 PROJ_KERNEL,
924 PROJ_WINE,
925 PROJ_UNKNOWN,
927 extern enum project_type option_project;
928 const char *check_name(unsigned short id);
929 int id_from_name(const char *name);
932 /* smatch_buf_size.c */
933 int get_array_size(struct expression *expr);
934 int get_array_size_bytes(struct expression *expr);
935 int get_array_size_bytes_min(struct expression *expr);
936 int get_array_size_bytes_max(struct expression *expr);
937 struct range_list *get_array_size_bytes_rl(struct expression *expr);
938 int get_real_array_size(struct expression *expr);
939 int last_member_is_resizable(struct symbol *type);
940 /* smatch_strlen.c */
941 int get_implied_strlen(struct expression *expr, struct range_list **rl);
942 int get_size_from_strlen(struct expression *expr);
944 /* smatch_capped.c */
945 int is_capped(struct expression *expr);
946 int is_capped_var_sym(const char *name, struct symbol *sym);
948 /* check_user_data.c */
949 int is_user_macro(struct expression *expr);
950 int is_user_data(struct expression *expr);
951 int is_capped_user_data(struct expression *expr);
952 int implied_user_data(struct expression *expr, struct range_list **rl);
953 struct stree *get_user_stree(void);
954 int get_user_rl(struct expression *expr, struct range_list **rl);
955 int get_user_rl_spammy(struct expression *expr, struct range_list **rl);
956 int is_user_rl(struct expression *expr);
957 int get_user_rl_var_sym(const char *name, struct symbol *sym, struct range_list **rl);
959 /* check_locking.c */
960 void print_held_locks();
962 /* check_assigned_expr.c */
963 struct expression *get_assigned_expr(struct expression *expr);
964 struct expression *get_assigned_expr_name_sym(const char *name, struct symbol *sym);
965 /* smatch_return_to_param.c */
966 void __add_return_to_param_mapping(struct expression *assign, const char *return_string);
967 char *map_call_to_param_name_sym(struct expression *expr, struct symbol **sym);
969 /* smatch_comparison.c */
970 struct compare_data {
971 /* The ->left and ->right expression pointers might be NULL (I'm lazy) */
972 struct expression *left;
973 const char *left_var;
974 struct var_sym_list *left_vsl;
975 int comparison;
976 struct expression *right;
977 const char *right_var;
978 struct var_sym_list *right_vsl;
980 DECLARE_ALLOCATOR(compare_data);
981 struct smatch_state *alloc_compare_state(
982 struct expression *left,
983 const char *left_var, struct var_sym_list *left_vsl,
984 int comparison,
985 struct expression *right,
986 const char *right_var, struct var_sym_list *right_vsl);
987 int filter_comparison(int orig, int op);
988 int merge_comparisons(int one, int two);
989 int combine_comparisons(int left_compare, int right_compare);
990 int state_to_comparison(struct smatch_state *state);
991 struct smatch_state *merge_compare_states(struct smatch_state *s1, struct smatch_state *s2);
992 int get_comparison(struct expression *left, struct expression *right);
993 int get_comparison_strings(const char *one, const char *two);
994 int possible_comparison(struct expression *a, int comparison, struct expression *b);
995 struct state_list *get_all_comparisons(struct expression *expr);
996 struct state_list *get_all_possible_equal_comparisons(struct expression *expr);
997 void __add_return_comparison(struct expression *call, const char *range);
998 void __add_comparison_info(struct expression *expr, struct expression *call, const char *range);
999 char *get_printed_param_name(struct expression *call, const char *param_name, struct symbol *param_sym);
1000 char *name_sym_to_param_comparison(const char *name, struct symbol *sym);
1001 char *expr_equal_to_param(struct expression *expr, int ignore);
1002 char *expr_lte_to_param(struct expression *expr, int ignore);
1003 char *expr_param_comparison(struct expression *expr, int ignore);
1004 int flip_comparison(int op);
1005 int negate_comparison(int op);
1006 int remove_unsigned_from_comparison(int op);
1007 int param_compare_limit_is_impossible(struct expression *expr, int left_param, char *left_key, char *value);
1008 void filter_by_comparison(struct range_list **rl, int comparison, struct range_list *right);
1009 struct sm_state *comparison_implication_hook(struct expression *expr,
1010 struct state_list **true_stack,
1011 struct state_list **false_stack);
1012 void __compare_param_limit_hook(struct expression *left_expr, struct expression *right_expr,
1013 const char *state_name,
1014 struct smatch_state *true_state, struct smatch_state *false_state);
1015 int impossibly_high_comparison(struct expression *expr);
1017 /* smatch_sval.c */
1018 sval_t *sval_alloc(sval_t sval);
1019 sval_t *sval_alloc_permanent(sval_t sval);
1020 sval_t sval_blank(struct expression *expr);
1021 sval_t sval_type_val(struct symbol *type, long long val);
1022 sval_t sval_from_val(struct expression *expr, long long val);
1023 int sval_is_ptr(sval_t sval);
1024 int sval_unsigned(sval_t sval);
1025 int sval_signed(sval_t sval);
1026 int sval_bits(sval_t sval);
1027 int sval_bits_used(sval_t sval);
1028 int sval_is_negative(sval_t sval);
1029 int sval_is_positive(sval_t sval);
1030 int sval_is_min(sval_t sval);
1031 int sval_is_max(sval_t sval);
1032 int sval_is_a_min(sval_t sval);
1033 int sval_is_a_max(sval_t sval);
1034 int sval_is_negative_min(sval_t sval);
1035 int sval_cmp_t(struct symbol *type, sval_t one, sval_t two);
1036 int sval_cmp_val(sval_t one, long long val);
1037 sval_t sval_min(sval_t one, sval_t two);
1038 sval_t sval_max(sval_t one, sval_t two);
1039 int sval_too_low(struct symbol *type, sval_t sval);
1040 int sval_too_high(struct symbol *type, sval_t sval);
1041 int sval_fits(struct symbol *type, sval_t sval);
1042 sval_t sval_cast(struct symbol *type, sval_t sval);
1043 sval_t sval_preop(sval_t sval, int op);
1044 sval_t sval_binop(sval_t left, int op, sval_t right);
1045 int sval_binop_overflows(sval_t left, int op, sval_t right);
1046 int sval_binop_overflows_no_sign(sval_t left, int op, sval_t right);
1047 unsigned long long fls_mask(unsigned long long uvalue);
1048 unsigned long long sval_fls_mask(sval_t sval);
1049 const char *sval_to_str(sval_t sval);
1050 const char *sval_to_numstr(sval_t sval);
1051 sval_t ll_to_sval(long long val);
1053 /* smatch_string_list.c */
1054 int list_has_string(struct string_list *str_list, const char *str);
1055 void insert_string(struct string_list **str_list, const char *str);
1056 struct string_list *clone_str_list(struct string_list *orig);
1057 struct string_list *combine_string_lists(struct string_list *one, struct string_list *two);
1059 /* smatch_start_states.c */
1060 struct stree *get_start_states(void);
1062 /* smatch_recurse.c */
1063 int has_symbol(struct expression *expr, struct symbol *sym);
1064 int has_variable(struct expression *expr, struct expression *var);
1065 int has_inc_dec(struct expression *expr);
1067 /* smatch_stored_conditions.c */
1068 struct smatch_state *get_stored_condition(struct expression *expr);
1069 struct expression_list *get_conditions(struct expression *expr);
1070 struct sm_state *stored_condition_implication_hook(struct expression *expr,
1071 struct state_list **true_stack,
1072 struct state_list **false_stack);
1074 /* check_string_len.c */
1075 int get_formatted_string_size(struct expression *call, int arg);
1077 /* smatch_param_set.c */
1078 int param_was_set(struct expression *expr);
1079 int param_was_set_var_sym(const char *name, struct symbol *sym);
1080 /* smatch_param_filter.c */
1081 int param_has_filter_data(struct sm_state *sm);
1083 /* smatch_links.c */
1084 void set_up_link_functions(int id, int linkid);
1085 struct smatch_state *merge_link_states(struct smatch_state *s1, struct smatch_state *s2);
1086 void store_link(int link_id, const char *name, struct symbol *sym, const char *link_name, struct symbol *link_sym);
1088 /* smatch_auto_copy.c */
1089 void set_auto_copy(int owner);
1091 /* check_buf_comparison */
1092 struct expression *get_size_variable(struct expression *buf);
1093 struct expression *get_array_variable(struct expression *size);
1095 /* smatch_untracked_param.c */
1096 void mark_untracked(struct expression *expr, int param, const char *key, const char *value);
1097 void add_untracked_param_hook(void (func)(struct expression *call, int param));
1098 void mark_all_params_untracked(int return_id, char *return_ranges, struct expression *expr);
1100 /* smatch_strings.c */
1101 struct state_list *get_strings(struct expression *expr);
1102 struct expression *fake_string_from_mtag(mtag_t tag);
1104 /* smatch_estate.c */
1105 int estate_get_single_value(struct smatch_state *state, sval_t *sval);
1107 /* smatch_address.c */
1108 int get_address_rl(struct expression *expr, struct range_list **rl);
1109 int get_member_offset(struct symbol *type, const char *member_name);
1110 int get_member_offset_from_deref(struct expression *expr);
1112 /* for now this is in smatch_used_parameter.c */
1113 void __get_state_hook(int owner, const char *name, struct symbol *sym);
1115 /* smatch_buf_comparison.c */
1116 int db_var_is_array_limit(struct expression *array, const char *name, struct var_sym_list *vsl);
1118 struct stree *get_all_return_states(void);
1119 struct stree_stack *get_all_return_strees(void);
1120 int on_atomic_dec_path(void);
1121 int was_inced(const char *name, struct symbol *sym);
1123 /* smatch_constraints.c */
1124 char *get_constraint_str(struct expression *expr);
1125 struct constraint_list *get_constraints(struct expression *expr);
1126 char *unmet_constraint(struct expression *data, struct expression *offset);
1127 char *get_required_constraint(const char *data_str);
1129 /* smatch_container_of.c */
1130 int get_param_from_container_of(struct expression *expr);
1131 int get_offset_from_container_of(struct expression *expr);
1133 /* smatch_mtag.c */
1134 int get_string_mtag(struct expression *expr, mtag_t *tag);
1135 int get_toplevel_mtag(struct symbol *sym, mtag_t *tag);
1136 int get_mtag(struct expression *expr, mtag_t *tag);
1137 int get_mtag_offset(struct expression *expr, mtag_t *tag, int *offset);
1138 int create_mtag_alias(mtag_t tag, struct expression *expr, mtag_t *new);
1139 int expr_to_mtag_offset(struct expression *expr, mtag_t *tag, int *offset);
1140 void update_mtag_data(struct expression *expr);
1141 int get_mtag_sval(struct expression *expr, sval_t *sval);
1142 int get_mtag_addr_sval(struct expression *expr, sval_t *sval);
1144 /* Trinity fuzzer stuff */
1145 const char *get_syscall_arg_type(struct symbol *sym);
1147 /* smatch_mem_tracker.c */
1148 extern int option_mem;
1149 unsigned long get_max_memory(void);
1151 /* check_is_nospec.c */
1152 bool is_nospec(struct expression *expr);
1154 static inline int type_bits(struct symbol *type)
1156 if (!type)
1157 return 0;
1158 if (type->type == SYM_PTR) /* Sparse doesn't set this for &pointers */
1159 return bits_in_pointer;
1160 if (type->type == SYM_ARRAY)
1161 return bits_in_pointer;
1162 if (!type->examined)
1163 examine_symbol_type(type);
1164 return type->bit_size;
1167 static inline bool type_is_ptr(struct symbol *type)
1169 return type && (type->type == SYM_PTR || type->type == SYM_ARRAY);
1172 static inline int type_unsigned(struct symbol *base_type)
1174 if (!base_type)
1175 return 0;
1176 if (base_type->ctype.modifiers & MOD_UNSIGNED)
1177 return 1;
1178 return 0;
1181 static inline int type_positive_bits(struct symbol *type)
1183 if (!type)
1184 return 0;
1185 if (type->type == SYM_ARRAY)
1186 return bits_in_pointer - 1;
1187 if (type_unsigned(type))
1188 return type_bits(type);
1189 return type_bits(type) - 1;
1192 static inline int sval_positive_bits(sval_t sval)
1194 return type_positive_bits(sval.type);
1198 * Returns -1 if one is smaller, 0 if they are the same and 1 if two is larger.
1200 static inline int sval_cmp(sval_t one, sval_t two)
1202 struct symbol *type;
1204 type = one.type;
1205 if (sval_positive_bits(two) > sval_positive_bits(one))
1206 type = two.type;
1207 if (type_bits(type) < 31)
1208 type = &int_ctype;
1210 one = sval_cast(type, one);
1211 two = sval_cast(type, two);
1213 if (type_unsigned(type)) {
1214 if (one.uvalue < two.uvalue)
1215 return -1;
1216 if (one.uvalue == two.uvalue)
1217 return 0;
1218 return 1;
1220 /* fix me handle type promotion and unsigned values */
1221 if (one.value < two.value)
1222 return -1;
1223 if (one.value == two.value)
1224 return 0;
1225 return 1;
1228 #endif /* !SMATCH_H_ */