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
31 #include "expression.h"
38 unsigned long long uvalue
;
45 typedef long long mtag_t
;
51 #define STATE(_x) static struct smatch_state _x = { .name = #_x }
52 extern struct smatch_state undefined
;
53 extern struct smatch_state ghost
;
54 extern struct smatch_state merged
;
55 extern struct smatch_state true_state
;
56 extern struct smatch_state false_state
;
57 DECLARE_ALLOCATOR(smatch_state
);
59 static inline void *INT_PTR(int i
)
61 return (void *)(long)i
;
64 static inline int PTR_INT(void *p
)
74 DECLARE_ALLOCATOR(tracker
);
75 DECLARE_PTR_LIST(tracker_list
, struct tracker
);
76 DECLARE_PTR_LIST(stree_stack
, struct stree
);
78 /* The first 3 struct members must match struct tracker */
83 unsigned short merged
:1;
85 struct smatch_state
*state
;
87 struct sm_state
*left
;
88 struct sm_state
*right
;
89 struct state_list
*possible
;
96 DECLARE_ALLOCATOR(var_sym
);
97 DECLARE_PTR_LIST(var_sym_list
, struct var_sym
);
103 DECLARE_PTR_LIST(constraint_list
, struct constraint
);
109 extern struct alloc_info
*alloc_funcs
;
112 unsigned long long set
;
113 unsigned long long possible
;
125 ASSIGNMENT_HOOK_AFTER
,
127 GLOBAL_ASSIGNMENT_HOOK
,
132 WHOLE_CONDITION_HOOK
,
133 FUNCTION_CALL_HOOK_BEFORE
,
135 CALL_HOOK_AFTER_INLINE
,
136 FUNCTION_CALL_HOOK_AFTER_DB
,
137 CALL_ASSIGNMENT_HOOK
,
138 MACRO_ASSIGNMENT_HOOK
,
163 void add_hook(void *func
, enum hook_type type
);
164 typedef struct smatch_state
*(merge_func_t
)(struct smatch_state
*s1
, struct smatch_state
*s2
);
165 typedef struct smatch_state
*(unmatched_func_t
)(struct sm_state
*state
);
166 void add_merge_hook(int client_id
, merge_func_t
*func
);
167 void add_unmatched_state_hook(int client_id
, unmatched_func_t
*func
);
168 void add_pre_merge_hook(int client_id
, void (*hook
)(struct sm_state
*cur
, struct sm_state
*other
));
169 typedef void (scope_hook
)(void *data
);
170 void add_scope_hook(scope_hook
*hook
, void *data
);
171 typedef void (func_hook
)(const char *fn
, struct expression
*expr
, void *data
);
172 typedef void (implication_hook
)(const char *fn
, struct expression
*call_expr
,
173 struct expression
*assign_expr
, void *data
);
174 typedef void (return_implies_hook
)(struct expression
*call_expr
,
175 int param
, char *key
, char *value
);
176 typedef int (implied_return_hook
)(struct expression
*call_expr
, void *info
, struct range_list
**rl
);
177 void add_function_hook(const char *look_for
, func_hook
*call_back
, void *data
);
179 void add_function_assign_hook(const char *look_for
, func_hook
*call_back
,
181 void add_implied_return_hook(const char *look_for
,
182 implied_return_hook
*call_back
,
184 void add_macro_assign_hook(const char *look_for
, func_hook
*call_back
,
186 void add_macro_assign_hook_extra(const char *look_for
, func_hook
*call_back
,
188 void return_implies_state(const char *look_for
, long long start
, long long end
,
189 implication_hook
*call_back
, void *info
);
190 void return_implies_state_sval(const char *look_for
, sval_t start
, sval_t end
,
191 implication_hook
*call_back
, void *info
);
192 void select_return_states_hook(int type
, return_implies_hook
*callback
);
193 void select_return_states_before(void (*fn
)(void));
194 void select_return_states_after(void (*fn
)(void));
195 int get_implied_return(struct expression
*expr
, struct range_list
**rl
);
196 void allocate_hook_memory(void);
197 void allocate_tracker_array(int num_checks
);
199 struct modification_data
{
200 struct smatch_state
*prev
;
201 struct expression
*cur
;
204 typedef void (modification_hook
)(struct sm_state
*sm
, struct expression
*mod_expr
);
205 void add_modification_hook(int owner
, modification_hook
*call_back
);
206 void add_modification_hook_late(int owner
, modification_hook
*call_back
);
207 struct smatch_state
*get_modification_state(struct expression
*expr
);
209 int outside_of_function(void);
210 const char *get_filename(void);
211 const char *get_base_file(void);
212 char *get_function(void);
213 int get_lineno(void);
214 extern int final_pass
;
215 extern struct symbol
*cur_func_sym
;
216 extern int option_debug
;
217 extern int local_debug
;
219 bool debug_implied(void);
220 extern int option_info
;
221 extern int option_spammy
;
222 extern char *trace_variable
;
223 extern struct stree
*global_states
;
224 int is_skipped_function(void);
225 int is_silenced_function(void);
226 extern bool implications_off
;
228 /* smatch_impossible.c */
229 int is_impossible_path(void);
230 void set_path_impossible(void);
232 extern FILE *sm_outfd
;
233 extern FILE *sql_outfd
;
234 extern FILE *caller_info_fd
;
235 extern int sm_nr_checks
;
236 extern int sm_nr_errors
;
239 * How to use these routines:
241 * sm_fatal(): an internal error of some kind that should immediately exit
242 * sm_ierror(): an internal error
243 * sm_perror(): an internal error from parsing input source
244 * sm_error(): an error from input source
245 * sm_warning(): a warning from input source
246 * sm_info(): info message (from option_info)
247 * sm_debug(): debug message
248 * sm_msg(): other message (please avoid using this)
251 #define sm_printf(msg...) do { \
252 if (final_pass || option_debug || local_debug || debug_db) \
253 fprintf(sm_outfd, msg); \
256 static inline void sm_prefix(void)
258 sm_printf("%s:%d %s() ", get_filename(), get_lineno(), get_function());
261 static inline void print_implied_debug_msg();
263 extern bool __silence_warnings_for_stmt
;
265 #define sm_print_msg(type, msg...) \
267 print_implied_debug_msg(); \
268 if (!final_pass && !option_debug && !local_debug && !debug_db) \
270 if (__silence_warnings_for_stmt && !option_debug && !local_debug) \
272 if (!option_info && is_silenced_function()) \
276 sm_printf("warn: "); \
278 } else if (type == 2) { \
279 sm_printf("error: "); \
281 } else if (type == 3) { \
282 sm_printf("parse error: "); \
289 #define sm_msg(msg...) do { sm_print_msg(0, msg); } while (0)
291 extern char *implied_debug_msg
;
292 static inline void print_implied_debug_msg(void)
294 static struct symbol
*last_printed
= NULL
;
296 if (!implied_debug_msg
)
298 if (last_printed
== cur_func_sym
)
300 last_printed
= cur_func_sym
;
301 sm_msg("%s", implied_debug_msg
);
304 #define sm_debug(msg...) do { if (option_debug) sm_printf(msg); } while (0)
305 #define db_debug(msg...) do { if (option_debug || debug_db) sm_printf(msg); } while (0)
307 #define sm_info(msg...) do { \
308 if (option_debug || (option_info && final_pass)) { \
310 sm_printf("info: "); \
316 #define sm_warning(msg...) do { sm_print_msg(1, msg); } while (0)
317 #define sm_error(msg...) do { sm_print_msg(2, msg); } while (0)
318 #define sm_perror(msg...) do { sm_print_msg(3, msg); } while (0)
320 static inline void sm_fatal(const char *fmt
, ...)
325 vfprintf(sm_outfd
, fmt
, args
);
328 fprintf(sm_outfd
, "\n");
333 static inline void sm_ierror(const char *fmt
, ...)
339 fprintf(sm_outfd
, "internal error: ");
342 vfprintf(sm_outfd
, fmt
, args
);
345 fprintf(sm_outfd
, "\n");
347 #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
349 struct smatch_state
*__get_state(int owner
, const char *name
, struct symbol
*sym
);
350 struct smatch_state
*get_state(int owner
, const char *name
, struct symbol
*sym
);
351 struct smatch_state
*get_state_expr(int owner
, struct expression
*expr
);
352 struct state_list
*get_possible_states(int owner
, const char *name
,
354 struct state_list
*get_possible_states_expr(int owner
, struct expression
*expr
);
355 struct sm_state
*set_state(int owner
, const char *name
, struct symbol
*sym
,
356 struct smatch_state
*state
);
357 struct sm_state
*set_state_expr(int owner
, struct expression
*expr
,
358 struct smatch_state
*state
);
359 void delete_state(int owner
, const char *name
, struct symbol
*sym
);
360 void delete_state_expr(int owner
, struct expression
*expr
);
361 void __delete_all_states_sym(struct symbol
*sym
);
362 void set_true_false_states(int owner
, const char *name
, struct symbol
*sym
,
363 struct smatch_state
*true_state
,
364 struct smatch_state
*false_state
);
365 void set_true_false_states_expr(int owner
, struct expression
*expr
,
366 struct smatch_state
*true_state
,
367 struct smatch_state
*false_state
);
369 struct stree
*get_all_states_from_stree(int owner
, struct stree
*source
);
370 struct stree
*get_all_states_stree(int id
);
371 struct stree
*__get_cur_stree(void);
372 int is_reachable(void);
373 void add_get_state_hook(void (*fn
)(int owner
, const char *name
, struct symbol
*sym
));
375 /* smatch_helper.c */
376 DECLARE_PTR_LIST(int_stack
, int);
377 char *alloc_string(const char *str
);
378 char *alloc_string_newline(const char *str
);
379 void free_string(char *str
);
380 void append(char *dest
, const char *data
, int buff_len
);
381 void remove_parens(char *str
);
382 struct smatch_state
*alloc_state_num(int num
);
383 struct smatch_state
*alloc_state_str(const char *name
);
384 struct smatch_state
*merge_str_state(struct smatch_state
*s1
, struct smatch_state
*s2
);
385 struct smatch_state
*alloc_state_expr(struct expression
*expr
);
386 struct expression
*get_argument_from_call_expr(struct expression_list
*args
,
388 struct expression
*get_array_expr(struct expression
*expr
);
390 char *expr_to_var(struct expression
*expr
);
391 struct symbol
*expr_to_sym(struct expression
*expr
);
392 char *expr_to_str(struct expression
*expr
);
393 char *expr_to_str_sym(struct expression
*expr
,
394 struct symbol
**sym_ptr
);
395 char *expr_to_var_sym(struct expression
*expr
,
396 struct symbol
**sym_ptr
);
397 char *expr_to_known_chunk_sym(struct expression
*expr
, struct symbol
**sym
);
398 char *expr_to_chunk_sym_vsl(struct expression
*expr
, struct symbol
**sym
, struct var_sym_list
**vsl
);
399 int get_complication_score(struct expression
*expr
);
401 int sym_name_is(const char *name
, struct expression
*expr
);
402 int get_const_value(struct expression
*expr
, sval_t
*sval
);
403 int get_value(struct expression
*expr
, sval_t
*val
);
404 int get_implied_value(struct expression
*expr
, sval_t
*val
);
405 int get_implied_value_fast(struct expression
*expr
, sval_t
*sval
);
406 int get_implied_min(struct expression
*expr
, sval_t
*sval
);
407 int get_implied_max(struct expression
*expr
, sval_t
*val
);
408 int get_hard_max(struct expression
*expr
, sval_t
*sval
);
409 int get_fuzzy_min(struct expression
*expr
, sval_t
*min
);
410 int get_fuzzy_max(struct expression
*expr
, sval_t
*max
);
411 int get_absolute_min(struct expression
*expr
, sval_t
*sval
);
412 int get_absolute_max(struct expression
*expr
, sval_t
*sval
);
413 int parse_call_math(struct expression
*expr
, char *math
, sval_t
*val
);
414 int parse_call_math_rl(struct expression
*call
, const char *math
, struct range_list
**rl
);
415 const char *get_allocation_math(struct expression
*expr
);
416 char *get_value_in_terms_of_parameter_math(struct expression
*expr
);
417 char *get_value_in_terms_of_parameter_math_var_sym(const char *var
, struct symbol
*sym
);
418 int expr_is_zero(struct expression
*expr
);
419 int known_condition_true(struct expression
*expr
);
420 int known_condition_false(struct expression
*expr
);
421 int implied_condition_true(struct expression
*expr
);
422 int implied_condition_false(struct expression
*expr
);
423 int can_integer_overflow(struct symbol
*type
, struct expression
*expr
);
424 void clear_math_cache(void);
425 void set_fast_math_only(void);
426 void clear_fast_math_only(void);
428 int is_array(struct expression
*expr
);
429 struct expression
*get_array_base(struct expression
*expr
);
430 struct expression
*get_array_offset(struct expression
*expr
);
431 const char *show_state(struct smatch_state
*state
);
432 struct statement
*get_expression_statement(struct expression
*expr
);
433 struct expression
*strip_parens(struct expression
*expr
);
434 struct expression
*strip_expr(struct expression
*expr
);
435 struct expression
*strip_expr_set_parent(struct expression
*expr
);
436 void scoped_state(int my_id
, const char *name
, struct symbol
*sym
);
437 int is_error_return(struct expression
*expr
);
438 int getting_address(struct expression
*expr
);
439 int get_struct_and_member(struct expression
*expr
, const char **type
, const char **member
);
440 char *get_member_name(struct expression
*expr
);
441 char *get_fnptr_name(struct expression
*expr
);
442 int cmp_pos(struct position pos1
, struct position pos2
);
443 int positions_eq(struct position pos1
, struct position pos2
);
444 struct statement
*get_current_statement(void);
445 struct statement
*get_prev_statement(void);
446 struct expression
*get_last_expr_from_expression_stmt(struct expression
*expr
);
448 #define RETURN_VAR -1
449 #define LOCAL_SCOPE -2
450 #define FILE_SCOPE -3
451 #define GLOBAL_SCOPE -4
452 #define UNKNOWN_SCOPE -5
453 int get_param_num_from_sym(struct symbol
*sym
);
454 int get_param_num(struct expression
*expr
);
455 struct symbol
*get_param_sym_from_num(int num
);
457 int ms_since(struct timeval
*start
);
458 int parent_is_gone_var_sym(const char *name
, struct symbol
*sym
);
459 int parent_is_gone(struct expression
*expr
);
460 int invert_op(int op
);
461 int op_remove_assign(int op
);
462 int expr_equiv(struct expression
*one
, struct expression
*two
);
463 void push_int(struct int_stack
**stack
, int num
);
464 int pop_int(struct int_stack
**stack
);
467 struct symbol
*get_real_base_type(struct symbol
*sym
);
468 int type_bytes(struct symbol
*type
);
469 int array_bytes(struct symbol
*type
);
470 struct symbol
*get_pointer_type(struct expression
*expr
);
471 struct symbol
*get_type(struct expression
*expr
);
472 struct symbol
*get_final_type(struct expression
*expr
);
473 struct symbol
*get_promoted_type(struct symbol
*left
, struct symbol
*right
);
474 int type_signed(struct symbol
*base_type
);
475 int expr_unsigned(struct expression
*expr
);
476 int expr_signed(struct expression
*expr
);
477 int returns_unsigned(struct symbol
*base_type
);
478 int is_pointer(struct expression
*expr
);
479 int returns_pointer(struct symbol
*base_type
);
480 sval_t
sval_type_max(struct symbol
*base_type
);
481 sval_t
sval_type_min(struct symbol
*base_type
);
482 int nr_bits(struct expression
*expr
);
483 int is_void_pointer(struct expression
*expr
);
484 int is_char_pointer(struct expression
*expr
);
485 int is_string(struct expression
*expr
);
486 bool is_struct_ptr(struct symbol
*type
);
487 int is_static(struct expression
*expr
);
488 bool is_local_variable(struct expression
*expr
);
489 int types_equiv(struct symbol
*one
, struct symbol
*two
);
490 bool type_fits(struct symbol
*type
, struct symbol
*test
);
492 const char *global_static();
493 struct symbol
*cur_func_return_type(void);
494 struct symbol
*get_arg_type(struct expression
*fn
, int arg
);
495 struct symbol
*get_member_type_from_key(struct expression
*expr
, const char *key
);
496 struct symbol
*get_arg_type_from_key(struct expression
*fn
, int param
, struct expression
*arg
, const char *key
);
497 int is_struct(struct expression
*expr
);
498 char *type_to_str(struct symbol
*type
);
500 /* smatch_ignore.c */
501 void add_ignore(int owner
, const char *name
, struct symbol
*sym
);
502 int is_ignored(int owner
, const char *name
, struct symbol
*sym
);
503 void add_ignore_expr(int owner
, struct expression
*expr
);
504 int is_ignored_expr(int owner
, struct expression
*expr
);
507 struct var_sym
*alloc_var_sym(const char *var
, struct symbol
*sym
);
508 struct var_sym_list
*expr_to_vsl(struct expression
*expr
);
509 void add_var_sym(struct var_sym_list
**list
, const char *var
, struct symbol
*sym
);
510 void add_var_sym_expr(struct var_sym_list
**list
, struct expression
*expr
);
511 void del_var_sym(struct var_sym_list
**list
, const char *var
, struct symbol
*sym
);
512 int in_var_sym_list(struct var_sym_list
*list
, const char *var
, struct symbol
*sym
);
513 struct var_sym_list
*clone_var_sym_list(struct var_sym_list
*from_vsl
);
514 void merge_var_sym_list(struct var_sym_list
**dest
, struct var_sym_list
*src
);
515 struct var_sym_list
*combine_var_sym_lists(struct var_sym_list
*one
, struct var_sym_list
*two
);
516 int var_sym_lists_equiv(struct var_sym_list
*one
, struct var_sym_list
*two
);
517 void free_var_sym_list(struct var_sym_list
**list
);
518 void free_var_syms_and_list(struct var_sym_list
**list
);
521 struct tracker
*alloc_tracker(int owner
, const char *name
, struct symbol
*sym
);
522 void add_tracker(struct tracker_list
**list
, int owner
, const char *name
,
524 void add_tracker_expr(struct tracker_list
**list
, int owner
, struct expression
*expr
);
525 void del_tracker(struct tracker_list
**list
, int owner
, const char *name
,
527 int in_tracker_list(struct tracker_list
*list
, int owner
, const char *name
,
529 void free_tracker_list(struct tracker_list
**list
);
530 void free_trackers_and_list(struct tracker_list
**list
);
532 /* smatch_conditions */
533 int in_condition(void);
537 extern int __in_fake_assign
;
538 extern int __in_fake_parameter_assign
;
539 extern int __in_fake_struct_assign
;
540 extern int in_fake_env
;
541 void smatch (struct string_list
*filelist
);
542 int inside_loop(void);
543 int definitely_inside_loop(void);
544 struct expression
*get_switch_expr(void);
545 int in_expression_statement(void);
546 void __process_post_op_stack(void);
547 void __split_expr(struct expression
*expr
);
548 void __split_label_stmt(struct statement
*stmt
);
549 void __split_stmt(struct statement
*stmt
);
550 extern int __in_function_def
;
551 extern int __in_unmatched_hook
;
552 extern int option_assume_loops
;
553 extern int option_two_passes
;
554 extern int option_no_db
;
555 extern int option_file_output
;
556 extern int option_time
;
557 extern struct expression_list
*big_expression_stack
;
558 extern struct expression_list
*big_condition_stack
;
559 extern struct statement_list
*big_statement_stack
;
560 int is_assigned_call(struct expression
*expr
);
561 int inlinable(struct expression
*expr
);
562 extern int __inline_call
;
563 extern struct expression
*__inline_fn
;
564 extern int __in_pre_condition
;
565 extern int __bail_on_rest_of_function
;
566 extern struct statement
*__prev_stmt
;
567 extern struct statement
*__cur_stmt
;
568 extern struct statement
*__next_stmt
;
569 void init_fake_env(void);
570 void end_fake_env(void);
571 int time_parsing_function(void);
572 bool taking_too_long(void);
574 /* smatch_struct_assignment.c */
575 struct expression
*get_faked_expression(void);
576 void __fake_struct_member_assignments(struct expression
*expr
);
578 /* smatch_project.c */
579 int is_no_inline_function(const char *function
);
581 /* smatch_conditions */
582 void __split_whole_condition(struct expression
*expr
);
583 void __handle_logic(struct expression
*expr
);
584 int is_condition(struct expression
*expr
);
585 int __handle_condition_assigns(struct expression
*expr
);
586 int __handle_select_assigns(struct expression
*expr
);
587 int __handle_expr_statement_assigns(struct expression
*expr
);
589 /* smatch_implied.c */
590 struct range_list_stack
;
591 void param_limit_implications(struct expression
*expr
, int param
, char *key
, char *value
, struct stree
**implied
);
592 struct stree
*__implied_case_stree(struct expression
*switch_expr
,
593 struct range_list
*case_rl
,
594 struct range_list_stack
**remaining_cases
,
595 struct stree
**raw_stree
);
596 void overwrite_states_using_pool(struct sm_state
*gate_sm
, struct sm_state
*pool_sm
);
597 int assume(struct expression
*expr
);
598 void end_assume(void);
599 int impossible_assumption(struct expression
*left
, int op
, sval_t sval
);
602 bool has_dynamic_states(unsigned short owner
);
603 void set_dynamic_states(unsigned short owner
);
605 /* smatch_extras.c */
606 int in_warn_on_macro(void);
607 #define SMATCH_EXTRA 5 /* this is my_id from smatch extra set in smatch.c */
608 extern int RETURN_ID
;
615 #define MTAG_ALIAS_BIT (1ULL << 63)
616 #define MTAG_OFFSET_MASK 0xfffULL
617 #define MTAG_SEED 0xdead << 12
619 const extern unsigned long valid_ptr_min
;
620 extern unsigned long valid_ptr_max
;
621 extern const sval_t valid_ptr_min_sval
;
622 extern sval_t valid_ptr_max_sval
;
623 extern struct range_list
*valid_ptr_rl
;
624 void alloc_valid_ptr_rl(void);
626 static const sval_t array_min_sval
= {
630 static const sval_t array_max_sval
= {
632 {.value
= ULONG_MAX
- 4095},
634 static const sval_t text_seg_min
= {
638 static const sval_t text_seg_max
= {
640 {.value
= ULONG_MAX
- 4095},
642 static const sval_t data_seg_min
= {
646 static const sval_t data_seg_max
= {
648 {.value
= ULONG_MAX
- 4095},
650 static const sval_t bss_seg_min
= {
654 static const sval_t bss_seg_max
= {
656 {.value
= ULONG_MAX
- 4095},
658 static const sval_t stack_seg_min
= {
662 static const sval_t stack_seg_max
= {
664 {.value
= ULONG_MAX
- 4095},
666 static const sval_t kmalloc_seg_min
= {
670 static const sval_t kmalloc_seg_max
= {
672 {.value
= ULONG_MAX
- 4095},
674 static const sval_t vmalloc_seg_min
= {
678 static const sval_t vmalloc_seg_max
= {
680 {.value
= ULONG_MAX
- 4095},
682 static const sval_t fn_ptr_min
= {
686 static const sval_t fn_ptr_max
= {
688 {.value
= ULONG_MAX
- 4095},
691 char *get_other_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
);
692 char *map_call_to_other_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
);
693 char *map_long_to_short_name_sym(const char *name
, struct symbol
*sym
, struct symbol
**new_sym
, bool use_stack
);
695 #define STRLEN_MAX_RET 1010101
697 /* smatch_absolute.c */
698 int get_absolute_min_helper(struct expression
*expr
, sval_t
*sval
);
699 int get_absolute_max_helper(struct expression
*expr
, sval_t
*sval
);
701 /* smatch_type_value.c */
702 int get_db_type_rl(struct expression
*expr
, struct range_list
**rl
);
703 /* smatch_data_val.c */
704 int get_mtag_rl(struct expression
*expr
, struct range_list
**rl
);
705 /* smatch_array_values.c */
706 int get_array_rl(struct expression
*expr
, struct range_list
**rl
);
708 /* smatch_states.c */
709 struct stree
*__swap_cur_stree(struct stree
*stree
);
710 void __push_fake_cur_stree();
711 struct stree
*__pop_fake_cur_stree();
712 void __free_fake_cur_stree();
713 void __set_fake_cur_stree_fast(struct stree
*stree
);
714 void __pop_fake_cur_stree_fast(void);
715 void __merge_stree_into_cur(struct stree
*stree
);
717 int unreachable(void);
718 void __set_cur_stree_readonly(void);
719 void __set_cur_stree_writable(void);
720 void __set_sm(struct sm_state
*sm
);
721 void __set_sm_cur_stree(struct sm_state
*sm
);
722 void __set_sm_fake_stree(struct sm_state
*sm
);
723 void __set_true_false_sm(struct sm_state
*true_state
,
724 struct sm_state
*false_state
);
725 void nullify_path(void);
726 void __match_nullify_path_hook(const char *fn
, struct expression
*expr
,
728 void __unnullify_path(void);
729 int __path_is_null(void);
730 void save_all_states(void);
731 void restore_all_states(void);
732 void free_goto_stack(void);
733 void clear_all_states(void);
735 struct sm_state
*get_sm_state(int owner
, const char *name
,
737 struct sm_state
*get_sm_state_expr(int owner
, struct expression
*expr
);
738 void __push_true_states(void);
739 void __use_false_states(void);
740 void __discard_false_states(void);
741 void __merge_false_states(void);
742 void __merge_true_states(void);
744 void __negate_cond_stacks(void);
745 void __use_pre_cond_states(void);
746 void __use_cond_true_states(void);
747 void __use_cond_false_states(void);
748 void __push_cond_stacks(void);
749 void __fold_in_set_states(void);
750 void __free_set_states(void);
751 struct stree
*__copy_cond_true_states(void);
752 struct stree
*__copy_cond_false_states(void);
753 struct stree
*__pop_cond_true_stack(void);
754 struct stree
*__pop_cond_false_stack(void);
755 void __and_cond_states(void);
756 void __or_cond_states(void);
757 void __save_pre_cond_states(void);
758 void __discard_pre_cond_states(void);
759 struct stree
*__get_true_states(void);
760 struct stree
*__get_false_states(void);
761 void __use_cond_states(void);
762 extern struct state_list
*__last_base_slist
;
764 void __push_continues(void);
765 void __discard_continues(void);
766 void __process_continues(void);
767 void __merge_continues(void);
769 void __push_breaks(void);
770 void __process_breaks(void);
771 int __has_breaks(void);
772 void __merge_breaks(void);
773 void __use_breaks(void);
775 void __save_switch_states(struct expression
*switch_expr
);
776 void __discard_switches(void);
777 int have_remaining_cases(void);
778 void __merge_switches(struct expression
*switch_expr
, struct range_list
*case_rl
);
779 void __push_default(void);
780 void __set_default(void);
781 int __pop_default(void);
783 void __push_conditions(void);
784 void __discard_conditions(void);
786 void __save_gotos(const char *name
, struct symbol
*sym
);
787 void __merge_gotos(const char *name
, struct symbol
*sym
);
789 void __print_cur_stree(void);
790 bool __print_states(const char *owner
);
791 typedef void (check_tracker_hook
)(int owner
, const char *name
, struct symbol
*sym
, struct smatch_state
*state
);
792 void add_check_tracker(const char *check_name
, check_tracker_hook
*fn
);
795 void __pass_to_client(void *data
, enum hook_type type
);
796 void __pass_case_to_client(struct expression
*switch_expr
,
797 struct range_list
*rl
);
798 int __has_merge_function(int client_id
);
799 struct smatch_state
*__client_merge_function(int owner
,
800 struct smatch_state
*s1
,
801 struct smatch_state
*s2
);
802 struct smatch_state
*__client_unmatched_state_function(struct sm_state
*sm
);
803 void call_pre_merge_hook(struct sm_state
*cur
, struct sm_state
*other
);
804 void __push_scope_hooks(void);
805 void __call_scope_hooks(void);
807 /* smatch_function_hooks.c */
808 void create_function_hook_hash(void);
809 void __match_initializer_call(struct symbol
*sym
);
815 * Changing these numbers is a pain. Don't do it. If you ever use a
816 * number it can't be re-used right away so there may be gaps.
817 * We select these in order by type so if the order matters, then give
818 * it a number below 100-999,9000-9999 ranges. */
830 ABSOLUTE_LIMITS
= 1010,
843 UNTRACKED_PARAM
= 1023,
849 COMPARE_LIMIT
= 1028,
850 PARAM_COMPARE
= 1029,
853 CONSTRAINT_REQUIRED
= 1033,
862 /* put random temporary stuff in the 7000-7999 range for testing */
864 USER_DATA_SET
= 9017,
866 NO_OVERFLOW_SIMPLE
= 8019,
870 LOCK_RESTORED
= 9023,
872 KNOWN_UNLOCKED
= 9025,
876 NO_SIDE_EFFECT
= 8025,
879 ARRAYSIZE_ARG
= 8033,
892 extern struct sqlite3
*smatch_db
;
893 extern struct sqlite3
*mem_db
;
894 extern struct sqlite3
*cache_db
;
896 void db_ignore_states(int id
);
897 void select_caller_info_hook(void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
), int type
);
898 void add_member_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
));
899 void add_caller_info_callback(int owner
, void (*callback
)(struct expression
*call
, int param
, char *printed_name
, struct sm_state
*sm
));
900 void add_split_return_callback(void (*fn
)(int return_id
, char *return_ranges
, struct expression
*returned_expr
));
901 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
));
902 void select_call_implies_hook(int type
, void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
));
903 void select_return_implies_hook(int type
, void (*callback
)(struct expression
*call
, struct expression
*arg
, char *key
, char *value
));
904 struct range_list
*db_return_vals(struct expression
*expr
);
905 struct range_list
*db_return_vals_from_str(const char *fn_name
);
906 struct range_list
*db_return_vals_no_args(struct expression
*expr
);
907 char *return_state_to_var_sym(struct expression
*expr
, int param
, const char *key
, struct symbol
**sym
);
908 char *get_chunk_from_key(struct expression
*arg
, char *key
, struct symbol
**sym
, struct var_sym_list
**vsl
);
909 char *get_variable_from_key(struct expression
*arg
, const char *key
, struct symbol
**sym
);
910 const char *state_name_to_param_name(const char *state_name
, const char *param_name
);
911 const char *get_param_name_var_sym(const char *name
, struct symbol
*sym
);
912 const char *get_param_name(struct sm_state
*sm
);
913 const char *get_mtag_name_var_sym(const char *state_name
, struct symbol
*sym
);
914 const char *get_mtag_name_expr(struct expression
*expr
);
915 char *get_data_info_name(struct expression
*expr
);
916 char *sm_to_arg_name(struct expression
*expr
, struct sm_state
*sm
);
917 int is_recursive_member(const char *param_name
);
919 char *escape_newlines(const char *str
);
920 void sql_exec(struct sqlite3
*db
, int (*callback
)(void*, int, char**, char**), void *data
, const char *sql
);
922 #define sql_helper(db, call_back, data, sql...) \
924 char sql_txt[1024]; \
926 sqlite3_snprintf(sizeof(sql_txt), sql_txt, sql); \
927 db_debug("debug: %s\n", sql_txt); \
928 sql_exec(db, call_back, data, sql_txt); \
932 #define run_sql(call_back, data, sql...) \
936 sql_helper(smatch_db, call_back, data, sql); \
939 #define mem_sql(call_back, data, sql...) \
940 sql_helper(mem_db, call_back, data, sql)
942 #define cache_sql(call_back, data, sql...) \
943 sql_helper(cache_db, call_back, data, sql)
945 #define sql_insert_helper(table, db, ignore, late, values...) \
947 struct sqlite3 *_db = db; \
949 if (__inline_fn && !_db) \
953 char *err, *p = buf; \
956 p += snprintf(p, buf + sizeof(buf) - p, \
957 "insert %sinto %s values (", \
958 ignore ? "or ignore " : "", #table); \
959 p += snprintf(p, buf + sizeof(buf) - p, values); \
960 p += snprintf(p, buf + sizeof(buf) - p, ");"); \
961 db_debug("mem-db: %s\n", buf); \
962 rc = sqlite3_exec(_db, buf, NULL, NULL, &err); \
963 if (rc != SQLITE_OK) { \
964 sm_ierror("SQL error #2: %s", err); \
965 sm_ierror("SQL: '%s'", buf); \
971 FILE *tmp_fd = sm_outfd; \
972 sm_outfd = sql_outfd; \
974 sm_printf("SQL%s: insert %sinto " #table " values(", \
975 late ? "_late" : "", ignore ? "or ignore " : ""); \
982 #define sql_insert(table, values...) sql_insert_helper(table, 0, 0, 0, values);
983 #define sql_insert_or_ignore(table, values...) sql_insert_helper(table, 0, 1, 0, values);
984 #define sql_insert_late(table, values...) sql_insert_helper(table, 0, 0, 1, values);
985 #define sql_insert_cache(table, values...) sql_insert_helper(table, cache_db, 1, 0, values);
987 char *get_static_filter(struct symbol
*sym
);
989 void sql_insert_return_states(int return_id
, const char *return_ranges
,
990 int type
, int param
, const char *key
, const char *value
);
991 void sql_insert_caller_info(struct expression
*call
, int type
, int param
,
992 const char *key
, const char *value
);
993 void sql_insert_function_ptr(const char *fn
, const char *struct_name
);
994 void sql_insert_return_values(const char *return_values
);
995 void sql_insert_return_implies(int type
, int param
, const char *key
, const char *value
);
996 void sql_insert_function_type_size(const char *member
, const char *ranges
);
997 void sql_insert_function_type_info(int type
, const char *struct_type
, const char *member
, const char *value
);
998 void sql_insert_type_info(int type
, const char *member
, const char *value
);
999 void sql_insert_local_values(const char *name
, const char *value
);
1000 void sql_insert_function_type_value(const char *type
, const char *value
);
1001 void sql_insert_function_type(int param
, const char *value
);
1002 void sql_insert_parameter_name(int param
, const char *value
);
1003 void sql_insert_data_info(struct expression
*data
, int type
, const char *value
);
1004 void sql_insert_data_info_var_sym(const char *var
, struct symbol
*sym
, int type
, const char *value
);
1005 void sql_save_constraint(const char *con
);
1006 void sql_save_constraint_required(const char *data
, int op
, const char *limit
);
1007 void sql_copy_constraint_required(const char *new_limit
, const char *old_limit
);
1008 void sql_insert_fn_ptr_data_link(const char *ptr
, const char *data
);
1009 void sql_insert_fn_data_link(struct expression
*fn
, int type
, int param
, const char *key
, const char *value
);
1010 void sql_insert_mtag_about(mtag_t tag
, const char *left_name
, const char *right_name
);
1011 void sql_insert_mtag_info(mtag_t tag
, int type
, const char *value
);
1012 void sql_insert_mtag_map(mtag_t container
, int container_offset
, mtag_t tag
, int tag_offset
);
1013 void sql_insert_mtag_alias(mtag_t orig
, mtag_t alias
);
1014 int mtag_map_select_container(mtag_t tag
, int container_offset
, mtag_t
*container
);
1015 int mtag_map_select_tag(mtag_t container
, int offset
, mtag_t
*tag
);
1016 struct smatch_state
*get_mtag_return(struct expression
*expr
, struct smatch_state
*state
);
1017 struct range_list
*swap_mtag_seed(struct expression
*expr
, struct range_list
*rl
);
1019 void sql_select_return_states(const char *cols
, struct expression
*call
,
1020 int (*callback
)(void*, int, char**, char**), void *info
);
1021 void sql_select_call_implies(const char *cols
, struct expression
*call
,
1022 int (*callback
)(void*, int, char**, char**));
1024 void open_smatch_db(char *db_file
);
1026 /* smatch_files.c */
1027 int open_data_file(const char *filename
);
1028 int open_schema_file(const char *schema
);
1029 struct token
*get_tokens_file(const char *filename
);
1032 extern char *option_debug_check
;
1033 extern char *option_project_str
;
1034 extern char *bin_dir
;
1035 extern char *data_dir
;
1036 extern int option_no_data
;
1037 extern int option_full_path
;
1038 extern int option_call_tree
;
1039 extern int num_checks
;
1045 PROJ_ILLUMOS_KERNEL
,
1049 extern enum project_type option_project
;
1050 const char *check_name(unsigned short id
);
1051 int id_from_name(const char *name
);
1054 /* smatch_buf_size.c */
1055 int get_array_size(struct expression
*expr
);
1056 int get_array_size_bytes(struct expression
*expr
);
1057 int get_array_size_bytes_min(struct expression
*expr
);
1058 int get_array_size_bytes_max(struct expression
*expr
);
1059 struct range_list
*get_array_size_bytes_rl(struct expression
*expr
);
1060 int get_real_array_size(struct expression
*expr
);
1061 int last_member_is_resizable(struct symbol
*type
);
1062 /* smatch_strlen.c */
1063 int get_implied_strlen(struct expression
*expr
, struct range_list
**rl
);
1064 int get_size_from_strlen(struct expression
*expr
);
1066 /* smatch_capped.c */
1067 int is_capped(struct expression
*expr
);
1068 int is_capped_var_sym(const char *name
, struct symbol
*sym
);
1070 /* check_user_data.c */
1071 int is_user_macro(struct expression
*expr
);
1072 int is_capped_user_data(struct expression
*expr
);
1073 int implied_user_data(struct expression
*expr
, struct range_list
**rl
);
1074 struct stree
*get_user_stree(void);
1075 int get_user_rl(struct expression
*expr
, struct range_list
**rl
);
1076 int is_user_rl(struct expression
*expr
);
1077 int get_user_rl_var_sym(const char *name
, struct symbol
*sym
, struct range_list
**rl
);
1078 bool user_rl_capped(struct expression
*expr
);
1079 struct range_list
*var_user_rl(struct expression
*expr
);
1081 /* check_locking.c */
1082 void print_held_locks();
1084 /* check_assigned_expr.c */
1085 extern int check_assigned_expr_id
;
1086 struct expression
*get_assigned_expr(struct expression
*expr
);
1087 struct expression
*get_assigned_expr_name_sym(const char *name
, struct symbol
*sym
);
1088 /* smatch_return_to_param.c */
1089 void __add_return_to_param_mapping(struct expression
*assign
, const char *return_string
);
1090 char *map_call_to_param_name_sym(struct expression
*expr
, struct symbol
**sym
);
1092 /* smatch_comparison.c */
1093 extern int comparison_id
;
1094 #define UNKNOWN_COMPARISON 0
1095 #define IMPOSSIBLE_COMPARISON -1
1096 struct compare_data
{
1097 /* The ->left and ->right expression pointers might be NULL (I'm lazy) */
1098 struct expression
*left
;
1099 const char *left_var
;
1100 struct var_sym_list
*left_vsl
;
1102 struct expression
*right
;
1103 const char *right_var
;
1104 struct var_sym_list
*right_vsl
;
1106 DECLARE_ALLOCATOR(compare_data
);
1107 struct smatch_state
*alloc_compare_state(
1108 struct expression
*left
,
1109 const char *left_var
, struct var_sym_list
*left_vsl
,
1111 struct expression
*right
,
1112 const char *right_var
, struct var_sym_list
*right_vsl
);
1113 int comparison_intersection(int orig
, int op
);
1114 int merge_comparisons(int one
, int two
);
1115 int combine_comparisons(int left_compare
, int right_compare
);
1116 int state_to_comparison(struct smatch_state
*state
);
1117 struct smatch_state
*merge_compare_states(struct smatch_state
*s1
, struct smatch_state
*s2
);
1118 int get_comparison(struct expression
*left
, struct expression
*right
);
1119 int get_comparison_no_extra(struct expression
*a
, struct expression
*b
);
1120 int get_comparison_strings(const char *one
, const char *two
);
1121 int possible_comparison(struct expression
*a
, int comparison
, struct expression
*b
);
1122 struct state_list
*get_all_comparisons(struct expression
*expr
);
1123 struct state_list
*get_all_possible_equal_comparisons(struct expression
*expr
);
1124 void __add_return_comparison(struct expression
*call
, const char *range
);
1125 void __add_comparison_info(struct expression
*expr
, struct expression
*call
, const char *range
);
1126 char *get_printed_param_name(struct expression
*call
, const char *param_name
, struct symbol
*param_sym
);
1127 char *name_sym_to_param_comparison(const char *name
, struct symbol
*sym
);
1128 char *expr_equal_to_param(struct expression
*expr
, int ignore
);
1129 char *expr_lte_to_param(struct expression
*expr
, int ignore
);
1130 char *expr_param_comparison(struct expression
*expr
, int ignore
);
1131 int flip_comparison(int op
);
1132 int negate_comparison(int op
);
1133 int remove_unsigned_from_comparison(int op
);
1134 int param_compare_limit_is_impossible(struct expression
*expr
, int left_param
, char *left_key
, char *value
);
1135 void filter_by_comparison(struct range_list
**rl
, int comparison
, struct range_list
*right
);
1136 void __compare_param_limit_hook(struct expression
*left_expr
, struct expression
*right_expr
,
1137 const char *state_name
,
1138 struct smatch_state
*true_state
, struct smatch_state
*false_state
);
1139 int impossibly_high_comparison(struct expression
*expr
);
1142 sval_t
*sval_alloc(sval_t sval
);
1143 sval_t
*sval_alloc_permanent(sval_t sval
);
1144 sval_t
sval_blank(struct expression
*expr
);
1145 sval_t
sval_type_val(struct symbol
*type
, long long val
);
1146 sval_t
sval_type_fval(struct symbol
*type
, long double fval
);
1147 sval_t
sval_from_val(struct expression
*expr
, long long val
);
1148 sval_t
sval_from_fval(struct expression
*expr
, long double fval
);
1149 int sval_is_ptr(sval_t sval
);
1150 bool sval_is_fp(sval_t sval
);
1151 int sval_unsigned(sval_t sval
);
1152 int sval_signed(sval_t sval
);
1153 int sval_bits(sval_t sval
);
1154 int sval_bits_used(sval_t sval
);
1155 int sval_is_negative(sval_t sval
);
1156 int sval_is_positive(sval_t sval
);
1157 int sval_is_min(sval_t sval
);
1158 int sval_is_max(sval_t sval
);
1159 int sval_is_a_min(sval_t sval
);
1160 int sval_is_a_max(sval_t sval
);
1161 int sval_is_negative_min(sval_t sval
);
1162 int sval_cmp_t(struct symbol
*type
, sval_t one
, sval_t two
);
1163 int sval_cmp_val(sval_t one
, long long val
);
1164 sval_t
sval_min(sval_t one
, sval_t two
);
1165 sval_t
sval_max(sval_t one
, sval_t two
);
1166 int sval_too_low(struct symbol
*type
, sval_t sval
);
1167 int sval_too_high(struct symbol
*type
, sval_t sval
);
1168 int sval_fits(struct symbol
*type
, sval_t sval
);
1169 sval_t
sval_cast(struct symbol
*type
, sval_t sval
);
1170 sval_t
sval_preop(sval_t sval
, int op
);
1171 sval_t
sval_binop(sval_t left
, int op
, sval_t right
);
1172 int sval_binop_overflows(sval_t left
, int op
, sval_t right
);
1173 int sval_binop_overflows_no_sign(sval_t left
, int op
, sval_t right
);
1174 int find_first_zero_bit(unsigned long long uvalue
);
1175 int sm_fls64(unsigned long long uvalue
);
1176 unsigned long long fls_mask(unsigned long long uvalue
);
1177 unsigned long long sval_fls_mask(sval_t sval
);
1178 const char *sval_to_str(sval_t sval
);
1179 const char *sval_to_str_or_err_ptr(sval_t sval
);
1180 const char *sval_to_numstr(sval_t sval
);
1181 sval_t
ll_to_sval(long long val
);
1183 /* smatch_string_list.c */
1184 int list_has_string(struct string_list
*str_list
, const char *str
);
1185 int insert_string(struct string_list
**str_list
, const char *str
);
1186 struct string_list
*clone_str_list(struct string_list
*orig
);
1187 struct string_list
*combine_string_lists(struct string_list
*one
, struct string_list
*two
);
1189 /* smatch_start_states.c */
1190 struct stree
*get_start_states(void);
1192 /* smatch_recurse.c */
1193 int has_symbol(struct expression
*expr
, struct symbol
*sym
);
1194 int has_variable(struct expression
*expr
, struct expression
*var
);
1195 int has_inc_dec(struct expression
*expr
);
1197 /* smatch_stored_conditions.c */
1198 struct smatch_state
*get_stored_condition(struct expression
*expr
);
1199 struct expression_list
*get_conditions(struct expression
*expr
);
1200 struct sm_state
*stored_condition_implication_hook(struct expression
*expr
,
1201 struct state_list
**true_stack
,
1202 struct state_list
**false_stack
);
1203 /* smatch_parsed_conditions.c */
1204 struct sm_state
*parsed_condition_implication_hook(struct expression
*expr
,
1205 struct state_list
**true_stack
,
1206 struct state_list
**false_stack
);
1207 /* smatch_comparison.c */
1208 struct sm_state
*comparison_implication_hook(struct expression
*expr
,
1209 struct state_list
**true_stack
,
1210 struct state_list
**false_stack
);
1212 /* check_string_len.c */
1213 int get_formatted_string_size(struct expression
*call
, int arg
);
1214 int get_formatted_string_min_size(struct expression
*call
, int arg
);
1216 /* smatch_param_set.c */
1217 int param_was_set(struct expression
*expr
);
1218 int param_was_set_var_sym(const char *name
, struct symbol
*sym
);
1219 void print_limited_param_set(int return_id
, char *return_ranges
, struct expression
*expr
);
1220 /* smatch_param_filter.c */
1221 int param_has_filter_data(struct sm_state
*sm
);
1223 /* smatch_links.c */
1224 void set_up_link_functions(int id
, int linkid
);
1225 struct smatch_state
*merge_link_states(struct smatch_state
*s1
, struct smatch_state
*s2
);
1226 void store_link(int link_id
, const char *name
, struct symbol
*sym
, const char *link_name
, struct symbol
*link_sym
);
1228 /* check_buf_comparison */
1229 const char *limit_type_str(unsigned int limit_type
);
1230 struct expression
*get_size_variable(struct expression
*buf
, int *limit_type
);
1231 struct expression
*get_array_variable(struct expression
*size
);
1232 int buf_comparison_index_ok(struct expression
*expr
);
1234 /* smatch_untracked_param.c */
1235 void mark_untracked(struct expression
*expr
, int param
, const char *key
, const char *value
);
1236 void add_untracked_param_hook(void (func
)(struct expression
*call
, int param
));
1237 void add_lost_param_hook(void (func
)(struct expression
*call
, int param
));
1238 void mark_all_params_untracked(int return_id
, char *return_ranges
, struct expression
*expr
);
1240 /* smatch_strings.c */
1241 struct state_list
*get_strings(struct expression
*expr
);
1242 struct expression
*fake_string_from_mtag(mtag_t tag
);
1244 /* smatch_estate.c */
1245 int estate_get_single_value(struct smatch_state
*state
, sval_t
*sval
);
1247 /* smatch_address.c */
1248 int get_address_rl(struct expression
*expr
, struct range_list
**rl
);
1249 int get_member_offset(struct symbol
*type
, const char *member_name
);
1250 int get_member_offset_from_deref(struct expression
*expr
);
1252 /* for now this is in smatch_used_parameter.c */
1253 void __get_state_hook(int owner
, const char *name
, struct symbol
*sym
);
1255 /* smatch_buf_comparison.c */
1256 int db_var_is_array_limit(struct expression
*array
, const char *name
, struct var_sym_list
*vsl
);
1258 struct stree
*get_all_return_states(void);
1259 struct stree_stack
*get_all_return_strees(void);
1260 int on_atomic_dec_path(void);
1261 int was_inced(const char *name
, struct symbol
*sym
);
1263 /* smatch_constraints.c */
1264 char *get_constraint_str(struct expression
*expr
);
1265 struct constraint_list
*get_constraints(struct expression
*expr
);
1266 char *unmet_constraint(struct expression
*data
, struct expression
*offset
);
1267 char *get_required_constraint(const char *data_str
);
1269 /* smatch_container_of.c */
1270 int get_param_from_container_of(struct expression
*expr
);
1271 int get_offset_from_container_of(struct expression
*expr
);
1272 char *get_container_name(struct expression
*container
, struct expression
*expr
);
1275 mtag_t
str_to_mtag(const char *str
);
1276 int get_string_mtag(struct expression
*expr
, mtag_t
*tag
);
1277 int get_toplevel_mtag(struct symbol
*sym
, mtag_t
*tag
);
1278 int create_mtag_alias(mtag_t tag
, struct expression
*expr
, mtag_t
*new);
1279 int expr_to_mtag_offset(struct expression
*expr
, mtag_t
*tag
, int *offset
);
1280 void update_mtag_data(struct expression
*expr
, struct smatch_state
*state
);
1281 int get_mtag_sval(struct expression
*expr
, sval_t
*sval
);
1283 /* Trinity fuzzer stuff */
1284 const char *get_syscall_arg_type(struct symbol
*sym
);
1286 /* smatch_bit_info.c */
1287 struct bit_info
*rl_to_binfo(struct range_list
*rl
);
1288 struct bit_info
*get_bit_info(struct expression
*expr
);
1289 struct bit_info
*get_bit_info_var_sym(const char *name
, struct symbol
*sym
);
1290 /* smatch_mem_tracker.c */
1291 extern int option_mem
;
1292 unsigned long get_mem_kb(void);
1293 unsigned long get_max_memory(void);
1295 /* check_is_nospec.c */
1296 bool is_nospec(struct expression
*expr
);
1297 long get_stmt_cnt(void);
1299 /* smatch_nul_terminator.c */
1300 bool is_nul_terminated_var_sym(const char *name
, struct symbol
*sym
);
1301 bool is_nul_terminated(struct expression
*expr
);
1302 /* check_kernel.c */
1303 bool is_ignored_kernel_data(const char *name
);
1305 bool is_fresh_alloc_var_sym(const char *var
, struct symbol
*sym
);
1306 bool is_fresh_alloc(struct expression
*expr
);
1307 static inline bool type_is_ptr(struct symbol
*type
)
1310 (type
->type
== SYM_PTR
||
1311 type
->type
== SYM_ARRAY
||
1312 type
->type
== SYM_FN
);
1315 static inline bool type_is_fp(struct symbol
*type
)
1318 (type
== &float_ctype
||
1319 type
== &double_ctype
||
1320 type
== &ldouble_ctype
);
1323 static inline int type_bits(struct symbol
*type
)
1327 if (type_is_ptr(type
))
1328 return bits_in_pointer
;
1329 if (!type
->examined
)
1330 examine_symbol_type(type
);
1331 return type
->bit_size
;
1334 static inline int type_unsigned(struct symbol
*base_type
)
1338 if (is_ptr_type(base_type
))
1340 if (base_type
->ctype
.modifiers
& MOD_UNSIGNED
)
1345 static inline int type_positive_bits(struct symbol
*type
)
1349 if (is_ptr_type(type
))
1350 return bits_in_pointer
;
1351 if (type_unsigned(type
))
1352 return type_bits(type
);
1353 return type_bits(type
) - 1;
1356 static inline int sval_positive_bits(sval_t sval
)
1358 return type_positive_bits(sval
.type
);
1362 * Returns -1 if one is smaller, 0 if they are the same and 1 if two is larger.
1365 static inline int fp_cmp(sval_t one
, sval_t two
)
1367 struct symbol
*type
;
1369 if (sval_is_fp(one
) && sval_is_fp(two
))
1370 type
= type_bits(one
.type
) > type_bits(two
.type
) ? one
.type
: two
.type
;
1371 else if (sval_is_fp(one
))
1376 one
= sval_cast(type
, one
);
1377 two
= sval_cast(type
, two
);
1379 if (one
.type
== &float_ctype
) {
1380 if (one
.fvalue
< two
.fvalue
)
1382 if (one
.fvalue
== two
.fvalue
)
1386 if (one
.type
== &double_ctype
) {
1387 if (one
.dvalue
< two
.dvalue
)
1389 if (one
.dvalue
== two
.dvalue
)
1393 if (one
.type
== &ldouble_ctype
) {
1394 if (one
.ldvalue
< two
.ldvalue
)
1396 if (one
.ldvalue
== two
.ldvalue
)
1400 sm_perror("bad type in fp_cmp(): %s", type_to_str(type
));
1404 static inline int sval_cmp(sval_t one
, sval_t two
)
1406 struct symbol
*type
;
1408 if (sval_is_fp(one
) || sval_is_fp(two
))
1409 return fp_cmp(one
, two
);
1412 if (sval_positive_bits(two
) > sval_positive_bits(one
))
1414 if (type_bits(type
) < 31)
1417 one
= sval_cast(type
, one
);
1418 two
= sval_cast(type
, two
);
1420 if (type_unsigned(type
)) {
1421 if (one
.uvalue
< two
.uvalue
)
1423 if (one
.uvalue
== two
.uvalue
)
1427 /* fix me handle type promotion and unsigned values */
1428 if (one
.value
< two
.value
)
1430 if (one
.value
== two
.value
)
1435 #endif /* !SMATCH_H_ */