2 * sparse/smatch_extra.c
4 * Copyright (C) 2008 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
11 * smatch_extra.c is supposed to track the value of every variable.
23 #include "smatch_slist.h"
24 #include "smatch_extra.h"
28 struct string_list
*__ignored_macros
= NULL
;
29 static int in_warn_on_macro()
31 struct statement
*stmt
;
35 stmt
= get_current_statement();
36 macro
= get_macro_name(stmt
->pos
);
40 FOR_EACH_PTR(__ignored_macros
, tmp
) {
41 if (!strcmp(tmp
, macro
))
43 } END_FOR_EACH_PTR(tmp
);
47 typedef void (mod_hook
)(const char *name
, struct symbol
*sym
, struct smatch_state
*state
);
48 DECLARE_PTR_LIST(void_fn_list
, mod_hook
*);
49 static struct void_fn_list
*extra_mod_hooks
;
50 void add_extra_mod_hook(mod_hook
*fn
)
52 mod_hook
**p
= malloc(sizeof(mod_hook
*));
54 add_ptr_list(&extra_mod_hooks
, p
);
57 void call_extra_mod_hooks(const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
61 FOR_EACH_PTR(extra_mod_hooks
, fn
) {
62 (*fn
)(name
, sym
, state
);
63 } END_FOR_EACH_PTR(fn
);
66 struct sm_state
*set_extra_mod(const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
68 if (in_warn_on_macro())
70 remove_from_equiv(name
, sym
);
71 call_extra_mod_hooks(name
, sym
, state
);
72 return set_state(SMATCH_EXTRA
, name
, sym
, state
);
75 struct sm_state
*set_extra_expr_mod(struct expression
*expr
, struct smatch_state
*state
)
79 struct sm_state
*ret
= NULL
;
81 expr
= strip_expr(expr
);
82 name
= expr_to_var_sym(expr
, &sym
);
85 ret
= set_extra_mod(name
, sym
, state
);
91 void set_extra_nomod(const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
94 struct smatch_state
*orig_state
;
96 orig_state
= get_state(SMATCH_EXTRA
, name
, sym
);
98 if (!estate_related(orig_state
)) {
99 set_state(SMATCH_EXTRA
, name
, sym
, state
);
103 set_related(state
, estate_related(orig_state
));
104 FOR_EACH_PTR(estate_related(orig_state
), rel
) {
105 if (option_debug_related
)
106 sm_msg("updating related %s to %s", rel
->name
, state
->name
);
107 set_state(SMATCH_EXTRA
, rel
->name
, rel
->sym
, state
);
108 } END_FOR_EACH_PTR(rel
);
112 * This is for return_implies_state() hooks which modify a SMATCH_EXTRA state
114 void set_extra_expr_nomod(struct expression
*expr
, struct smatch_state
*state
)
119 name
= expr_to_var_sym(expr
, &sym
);
122 set_extra_nomod(name
, sym
, state
);
128 static void set_extra_true_false(const char *name
, struct symbol
*sym
,
129 struct smatch_state
*true_state
,
130 struct smatch_state
*false_state
)
132 struct relation
*rel
;
133 struct smatch_state
*orig_state
;
135 if (in_warn_on_macro())
138 orig_state
= get_state(SMATCH_EXTRA
, name
, sym
);
140 if (!estate_related(orig_state
)) {
141 set_true_false_states(SMATCH_EXTRA
, name
, sym
, true_state
, false_state
);
146 set_related(true_state
, estate_related(orig_state
));
148 set_related(false_state
, estate_related(orig_state
));
150 FOR_EACH_PTR(estate_related(orig_state
), rel
) {
151 set_true_false_states(SMATCH_EXTRA
, rel
->name
, rel
->sym
,
152 true_state
, false_state
);
153 } END_FOR_EACH_PTR(rel
);
156 static void set_extra_expr_true_false(struct expression
*expr
,
157 struct smatch_state
*true_state
,
158 struct smatch_state
*false_state
)
163 expr
= strip_expr(expr
);
164 name
= expr_to_var_sym(expr
, &sym
);
167 set_extra_true_false(name
, sym
, true_state
, false_state
);
172 static struct sm_state
*handle_canonical_while_count_down(struct statement
*loop
)
174 struct expression
*iter_var
;
175 struct expression
*condition
;
177 struct smatch_state
*estate
;
180 condition
= strip_expr(loop
->iterator_pre_condition
);
183 if (condition
->type
!= EXPR_PREOP
&& condition
->type
!= EXPR_POSTOP
)
185 if (condition
->op
!= SPECIAL_DECREMENT
)
188 iter_var
= condition
->unop
;
189 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
192 if (sval_cmp_val(estate_min(sm
->state
), 0) < 0)
194 start
= estate_max(sm
->state
);
195 if (sval_cmp_val(start
, 0) <= 0)
197 if (!sval_is_max(start
))
200 if (condition
->type
== EXPR_PREOP
) {
201 estate
= alloc_estate_range(sval_type_val(start
.type
, 1), start
);
202 if (estate_has_hard_max(sm
->state
))
203 estate_set_hard_max(estate
);
204 set_extra_expr_mod(iter_var
, estate
);
206 if (condition
->type
== EXPR_POSTOP
) {
207 estate
= alloc_estate_range(sval_type_val(start
.type
, 0), start
);
208 if (estate_has_hard_max(sm
->state
))
209 estate_set_hard_max(estate
);
210 set_extra_expr_mod(iter_var
, estate
);
212 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
215 static struct sm_state
*handle_canonical_for_inc(struct expression
*iter_expr
,
216 struct expression
*condition
)
218 struct expression
*iter_var
;
220 struct smatch_state
*estate
;
221 sval_t start
, end
, dummy
;
223 iter_var
= iter_expr
->unop
;
224 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
227 if (!estate_get_single_value(sm
->state
, &start
))
229 if (!get_implied_max(condition
->right
, &end
))
230 end
= sval_type_max(get_type(iter_var
));
232 if (get_sm_state_expr(SMATCH_EXTRA
, condition
->left
) != sm
)
235 switch (condition
->op
) {
236 case SPECIAL_NOTEQUAL
:
238 if (!sval_is_min(end
))
246 if (sval_cmp(end
, start
) < 0)
248 estate
= alloc_estate_range(start
, end
);
249 if (get_hard_max(condition
->right
, &dummy
))
250 estate_set_hard_max(estate
);
251 set_extra_expr_mod(iter_var
, estate
);
252 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
255 static struct sm_state
*handle_canonical_for_dec(struct expression
*iter_expr
,
256 struct expression
*condition
)
258 struct expression
*iter_var
;
260 struct smatch_state
*estate
;
263 iter_var
= iter_expr
->unop
;
264 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
267 if (!estate_get_single_value(sm
->state
, &start
))
269 if (!get_implied_min(condition
->right
, &end
))
270 end
= sval_type_min(get_type(iter_var
));
271 if (get_sm_state_expr(SMATCH_EXTRA
, condition
->left
) != sm
)
274 switch (condition
->op
) {
275 case SPECIAL_NOTEQUAL
:
277 if (!sval_is_min(end
) && !sval_is_max(end
))
285 if (sval_cmp(end
, start
) > 0)
287 estate
= alloc_estate_range(end
, start
);
288 estate_set_hard_max(estate
);
289 set_extra_expr_mod(iter_var
, estate
);
290 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
293 static struct sm_state
*handle_canonical_for_loops(struct statement
*loop
)
295 struct expression
*iter_expr
;
296 struct expression
*condition
;
298 if (!loop
->iterator_post_statement
)
300 if (loop
->iterator_post_statement
->type
!= STMT_EXPRESSION
)
302 iter_expr
= loop
->iterator_post_statement
->expression
;
303 if (!loop
->iterator_pre_condition
)
305 if (loop
->iterator_pre_condition
->type
!= EXPR_COMPARE
)
307 condition
= loop
->iterator_pre_condition
;
309 if (iter_expr
->op
== SPECIAL_INCREMENT
)
310 return handle_canonical_for_inc(iter_expr
, condition
);
311 if (iter_expr
->op
== SPECIAL_DECREMENT
)
312 return handle_canonical_for_dec(iter_expr
, condition
);
316 struct sm_state
*__extra_handle_canonical_loops(struct statement
*loop
, struct state_list
**slist
)
318 struct sm_state
*ret
;
320 __push_fake_cur_slist();
321 if (!loop
->iterator_post_statement
)
322 ret
= handle_canonical_while_count_down(loop
);
324 ret
= handle_canonical_for_loops(loop
);
325 *slist
= __pop_fake_cur_slist();
329 int __iterator_unchanged(struct sm_state
*sm
)
333 if (get_sm_state(my_id
, sm
->name
, sm
->sym
) == sm
)
338 static void while_count_down_after(struct sm_state
*sm
, struct expression
*condition
)
342 /* paranoid checking. prolly not needed */
343 condition
= strip_expr(condition
);
346 if (condition
->type
!= EXPR_PREOP
&& condition
->type
!= EXPR_POSTOP
)
348 if (condition
->op
!= SPECIAL_DECREMENT
)
350 after_value
= estate_min(sm
->state
);
352 set_extra_mod(sm
->name
, sm
->sym
, alloc_estate_sval(after_value
));
355 void __extra_pre_loop_hook_after(struct sm_state
*sm
,
356 struct statement
*iterator
,
357 struct expression
*condition
)
359 struct expression
*iter_expr
;
361 struct smatch_state
*state
;
364 while_count_down_after(sm
, condition
);
368 iter_expr
= iterator
->expression
;
370 if (condition
->type
!= EXPR_COMPARE
)
372 if (iter_expr
->op
== SPECIAL_INCREMENT
) {
373 limit
= sval_binop(estate_max(sm
->state
), '+',
374 sval_type_val(estate_type(sm
->state
), 1));
376 limit
= sval_binop(estate_min(sm
->state
), '-',
377 sval_type_val(estate_type(sm
->state
), 1));
379 if (!estate_has_hard_max(sm
->state
) && !__has_breaks()) {
380 if (iter_expr
->op
== SPECIAL_INCREMENT
)
381 state
= alloc_estate_range(estate_min(sm
->state
), limit
);
383 state
= alloc_estate_range(limit
, estate_max(sm
->state
));
385 state
= alloc_estate_sval(limit
);
387 if (!estate_has_hard_max(sm
->state
))
388 estate_clear_hard_max(state
);
389 set_extra_mod(sm
->name
, sm
->sym
, state
);
392 static struct state_list
*unmatched_slist
;
393 static struct smatch_state
*unmatched_state(struct sm_state
*sm
)
395 struct smatch_state
*state
;
397 if (unmatched_slist
) {
398 state
= get_state_slist(unmatched_slist
, SMATCH_EXTRA
, sm
->name
, sm
->sym
);
402 return alloc_estate_whole(estate_type(sm
->state
));
405 static void clear_the_pointed_at(struct expression
*expr
, struct state_list
*slist
)
409 struct sm_state
*tmp
;
411 name
= expr_to_var_sym(expr
, &sym
);
415 FOR_EACH_PTR(slist
, tmp
) {
416 if (tmp
->name
[0] != '*')
420 if (strcmp(tmp
->name
+ 1, name
) != 0)
422 set_extra_mod(tmp
->name
, tmp
->sym
, alloc_estate_whole(estate_type(tmp
->state
)));
423 } END_FOR_EACH_PTR(tmp
);
429 static void match_function_call(struct expression
*expr
)
431 struct expression
*arg
;
432 struct expression
*tmp
;
433 struct state_list
*slist
;
435 slist
= get_all_states(SMATCH_EXTRA
);
437 FOR_EACH_PTR(expr
->args
, arg
) {
438 tmp
= strip_expr(arg
);
439 if (tmp
->type
== EXPR_PREOP
&& tmp
->op
== '&')
440 set_extra_expr_mod(tmp
->unop
, alloc_estate_whole(get_type(tmp
->unop
)));
442 clear_the_pointed_at(tmp
, slist
);
443 } END_FOR_EACH_PTR(arg
);
448 static int types_equiv_or_pointer(struct symbol
*one
, struct symbol
*two
)
452 if (one
->type
== SYM_PTR
&& two
->type
== SYM_PTR
)
454 return types_equiv(one
, two
);
457 static void match_assign(struct expression
*expr
)
459 struct range_list
*rl
= NULL
;
460 struct expression
*left
;
461 struct expression
*right
;
462 struct symbol
*right_sym
;
463 char *right_name
= NULL
;
468 sval_t right_min
, right_max
;
471 if (is_condition(expr
->right
))
473 left
= strip_expr(expr
->left
);
474 name
= expr_to_var_sym(left
, &sym
);
477 right
= strip_parens(expr
->right
);
478 while (right
->type
== EXPR_ASSIGNMENT
&& right
->op
== '=')
479 right
= strip_parens(right
->left
);
481 if (expr
->op
== '=' && right
->type
== EXPR_CALL
)
482 goto free
; /* these are handled in smatch_function_hooks.c */
484 right_name
= expr_to_var_sym(right
, &right_sym
);
485 if (expr
->op
== '=' && right_name
&& right_sym
&&
486 types_equiv_or_pointer(get_type(expr
->left
), get_type(expr
->right
))) {
487 set_equiv(left
, right
);
491 right_min
= sval_type_min(get_type(expr
->right
));
492 right_max
= sval_type_max(get_type(expr
->right
));
494 known
= get_implied_value(right
, &value
);
497 struct smatch_state
*state
;
499 if (get_implied_rl(right
, &rl
)) {
500 rl
= cast_rl(get_type(expr
->left
), rl
);
501 state
= alloc_estate_rl(rl
);
502 if (get_hard_max(right
, &tmp
))
503 estate_set_hard_max(state
);
505 rl
= alloc_whole_rl(get_type(right
));
506 rl
= cast_rl(get_type(expr
->left
), rl
);
507 state
= alloc_estate_rl(rl
);
509 set_extra_mod(name
, sym
, state
);
512 case SPECIAL_ADD_ASSIGN
:
513 if (get_implied_min(left
, &tmp
)) {
515 right_min
= sval_binop(tmp
, '+', value
);
519 if (!inside_loop() && known
&& get_implied_max(left
, &tmp
))
520 right_max
= sval_binop(tmp
, '+', value
);
522 case SPECIAL_SUB_ASSIGN
:
523 if (get_implied_max(left
, &tmp
)) {
525 right_max
= sval_binop(tmp
, '-', value
);
529 if (!inside_loop() && known
&& get_implied_min(left
, &tmp
))
530 right_min
= sval_binop(tmp
, '-', value
);
533 if (!sval_is_min(right_min
) || !sval_is_max(right_max
))
534 rl
= cast_rl(get_type(expr
->left
), alloc_rl(right_min
, right_max
));
536 rl
= alloc_whole_rl(get_type(expr
->left
));
537 set_extra_mod(name
, sym
, alloc_estate_rl(rl
));
539 free_string(right_name
);
543 static void reset_struct_members(struct sm_state
*sm
)
545 if (!estate_rl(sm
->state
))
547 set_extra_mod(sm
->name
, sm
->sym
, alloc_estate_whole(estate_type(sm
->state
)));
550 static struct smatch_state
*increment_state(struct smatch_state
*state
)
552 sval_t min
= estate_min(state
);
553 sval_t max
= estate_max(state
);
555 if (!estate_rl(state
))
559 max
= sval_type_max(max
.type
);
561 if (!sval_is_min(min
) && !sval_is_max(min
))
563 if (!sval_is_min(max
) && !sval_is_max(max
))
565 return alloc_estate_range(min
, max
);
568 static struct smatch_state
*decrement_state(struct smatch_state
*state
)
570 sval_t min
= estate_min(state
);
571 sval_t max
= estate_max(state
);
573 if (!estate_rl(state
))
577 min
= sval_type_min(min
.type
);
579 if (!sval_is_min(min
) && !sval_is_max(min
))
581 if (!sval_is_min(max
) && !sval_is_max(max
))
583 return alloc_estate_range(min
, max
);
586 static void unop_expr(struct expression
*expr
)
588 struct smatch_state
*state
;
590 if (expr
->smatch_flags
& Handled
)
594 case SPECIAL_INCREMENT
:
595 state
= get_state_expr(SMATCH_EXTRA
, expr
->unop
);
596 state
= increment_state(state
);
598 state
= alloc_estate_whole(get_type(expr
));
599 set_extra_expr_mod(expr
->unop
, state
);
601 case SPECIAL_DECREMENT
:
602 state
= get_state_expr(SMATCH_EXTRA
, expr
->unop
);
603 state
= decrement_state(state
);
605 state
= alloc_estate_whole(get_type(expr
));
606 set_extra_expr_mod(expr
->unop
, state
);
613 static void asm_expr(struct statement
*stmt
)
616 struct expression
*expr
;
619 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
621 case 0: /* identifier */
622 case 1: /* constraint */
625 case 2: /* expression */
627 set_extra_expr_mod(expr
, alloc_estate_whole(get_type(expr
)));
630 } END_FOR_EACH_PTR(expr
);
633 static void delete_state_tracker(struct tracker
*t
)
635 remove_from_equiv(t
->name
, t
->sym
);
636 delete_state(t
->owner
, t
->name
, t
->sym
);
639 static void scoped_state_extra(const char *name
, struct symbol
*sym
)
643 t
= alloc_tracker(SMATCH_EXTRA
, name
, sym
);
644 add_scope_hook((scope_hook
*)&delete_state_tracker
, t
);
647 static void match_declarations(struct symbol
*sym
)
652 name
= sym
->ident
->name
;
653 if (!sym
->initializer
) {
654 set_state(SMATCH_EXTRA
, name
, sym
, alloc_estate_whole(get_real_base_type(sym
)));
655 scoped_state_extra(name
, sym
);
660 static void check_dereference(struct expression
*expr
)
662 set_extra_expr_nomod(expr
, alloc_estate_range(valid_ptr_min_sval
, valid_ptr_max_sval
));
665 static void match_dereferences(struct expression
*expr
)
667 if (expr
->type
!= EXPR_PREOP
)
669 check_dereference(expr
->unop
);
672 static void match_pointer_as_array(struct expression
*expr
)
676 check_dereference(expr
->unop
->left
);
679 static void set_param_dereferenced(struct expression
*arg
, char *unused
)
681 check_dereference(arg
);
684 static void match_function_def(struct symbol
*sym
)
688 FOR_EACH_PTR(sym
->ctype
.base_type
->arguments
, arg
) {
691 set_state(my_id
, arg
->ident
->name
, arg
, alloc_estate_whole(get_real_base_type(arg
)));
692 } END_FOR_EACH_PTR(arg
);
695 static int match_func_comparison(struct expression
*expr
)
697 struct expression
*left
= strip_expr(expr
->left
);
698 struct expression
*right
= strip_expr(expr
->right
);
701 if (left
->type
== EXPR_CALL
) {
702 if (!get_implied_value(right
, &sval
))
704 function_comparison(expr
->op
, left
, sval
, 1);
708 if (right
->type
== EXPR_CALL
) {
709 if (!get_implied_value(left
, &sval
))
711 function_comparison(expr
->op
, right
, sval
, 0);
718 static sval_t
add_one(sval_t sval
)
724 static sval_t
sub_one(sval_t sval
)
730 static void match_comparison(struct expression
*expr
)
732 struct expression
*left
= strip_expr(expr
->left
);
733 struct expression
*right
= strip_expr(expr
->right
);
735 struct range_list
*left_orig
;
736 struct range_list
*left_true
;
737 struct range_list
*left_false
;
738 struct range_list
*right_orig
;
739 struct range_list
*right_true
;
740 struct range_list
*right_false
;
741 struct smatch_state
*left_true_state
;
742 struct smatch_state
*left_false_state
;
743 struct smatch_state
*right_true_state
;
744 struct smatch_state
*right_false_state
;
745 sval_t min
, max
, dummy
;
747 int right_postop
= 0;
749 if (match_func_comparison(expr
))
752 if (left
->op
== SPECIAL_INCREMENT
|| left
->op
== SPECIAL_DECREMENT
) {
753 if (left
->type
== EXPR_POSTOP
) {
754 left
->smatch_flags
|= Handled
;
755 left_postop
= left
->op
;
757 left
= strip_expr(left
->unop
);
759 while (left
->type
== EXPR_ASSIGNMENT
)
760 left
= strip_expr(left
->left
);
762 if (right
->op
== SPECIAL_INCREMENT
|| right
->op
== SPECIAL_DECREMENT
) {
763 if (right
->type
== EXPR_POSTOP
) {
764 right
->smatch_flags
|= Handled
;
765 right_postop
= right
->op
;
767 right
= strip_expr(right
->unop
);
770 type
= get_type(expr
);
774 if (get_implied_rl(left
, &left_orig
)) {
775 left_orig
= cast_rl(type
, left_orig
);
777 min
= sval_type_min(get_type(left
));
778 max
= sval_type_max(get_type(left
));
779 left_orig
= cast_rl(type
, alloc_rl(min
, max
));
782 if (get_implied_rl(right
, &right_orig
)) {
783 right_orig
= cast_rl(type
, right_orig
);
785 min
= sval_type_min(get_type(right
));
786 max
= sval_type_max(get_type(right
));
787 right_orig
= cast_rl(type
, alloc_rl(min
, max
));
789 min
= sval_type_min(type
);
790 max
= sval_type_max(type
);
792 left_true
= clone_rl(left_orig
);
793 left_false
= clone_rl(left_orig
);
794 right_true
= clone_rl(right_orig
);
795 right_false
= clone_rl(right_orig
);
799 case SPECIAL_UNSIGNED_LT
:
800 left_true
= remove_range(left_orig
, rl_max(right_orig
), max
);
801 if (!sval_is_min(rl_min(right_orig
))) {
802 left_false
= remove_range(left_orig
, min
, sub_one(rl_min(right_orig
)));
805 right_true
= remove_range(right_orig
, min
, rl_min(left_orig
));
806 if (!sval_is_max(rl_max(left_orig
)))
807 right_false
= remove_range(right_orig
, add_one(rl_max(left_orig
)), max
);
809 case SPECIAL_UNSIGNED_LTE
:
811 if (!sval_is_max(rl_max(right_orig
)))
812 left_true
= remove_range(left_orig
, add_one(rl_max(right_orig
)), max
);
813 left_false
= remove_range(left_orig
, min
, rl_min(right_orig
));
815 if (!sval_is_min(rl_min(left_orig
)))
816 right_true
= remove_range(right_orig
, min
, sub_one(rl_min(left_orig
)));
817 right_false
= remove_range(right_orig
, rl_max(left_orig
), max
);
819 if (sval_cmp(rl_min(left_orig
), rl_min(right_orig
)) == 0)
820 left_false
= remove_range(left_false
, rl_min(left_orig
), rl_min(left_orig
));
821 if (sval_cmp(rl_max(left_orig
), rl_max(right_orig
)) == 0)
822 right_false
= remove_range(right_false
, rl_max(left_orig
), rl_max(left_orig
));
825 if (!sval_is_max(rl_max(right_orig
))) {
826 left_true
= remove_range(left_true
, add_one(rl_max(right_orig
)), max
);
828 if (!sval_is_min(rl_min(right_orig
))) {
829 left_true
= remove_range(left_true
, min
, sub_one(rl_min(right_orig
)));
831 if (sval_cmp(rl_min(right_orig
), rl_max(right_orig
)) == 0)
832 left_false
= remove_range(left_orig
, rl_min(right_orig
), rl_min(right_orig
));
834 if (!sval_is_max(rl_max(left_orig
)))
835 right_true
= remove_range(right_true
, add_one(rl_max(left_orig
)), max
);
836 if (!sval_is_min(rl_min(left_orig
)))
837 right_true
= remove_range(right_true
, min
, sub_one(rl_min(left_orig
)));
838 if (sval_cmp(rl_min(left_orig
), rl_max(left_orig
)) == 0)
839 right_false
= remove_range(right_orig
, rl_min(left_orig
), rl_min(left_orig
));
841 case SPECIAL_UNSIGNED_GTE
:
843 if (!sval_is_min(rl_min(right_orig
)))
844 left_true
= remove_range(left_orig
, min
, sub_one(rl_min(right_orig
)));
845 left_false
= remove_range(left_orig
, rl_max(right_orig
), max
);
847 if (!sval_is_max(rl_max(left_orig
)))
848 right_true
= remove_range(right_orig
, add_one(rl_max(left_orig
)), max
);
849 right_false
= remove_range(right_orig
, min
, rl_min(left_orig
));
851 if (sval_cmp(rl_min(left_orig
), rl_min(right_orig
)) == 0)
852 right_false
= remove_range(right_false
, rl_min(left_orig
), rl_min(left_orig
));
853 if (sval_cmp(rl_max(left_orig
), rl_max(right_orig
)) == 0)
854 left_false
= remove_range(left_false
, rl_max(left_orig
), rl_max(left_orig
));
857 case SPECIAL_UNSIGNED_GT
:
858 left_true
= remove_range(left_orig
, min
, rl_min(right_orig
));
859 if (!sval_is_max(rl_max(right_orig
)))
860 left_false
= remove_range(left_orig
, add_one(rl_max(right_orig
)), max
);
862 right_true
= remove_range(right_orig
, rl_max(left_orig
), max
);
863 if (!sval_is_min(rl_min(left_orig
)))
864 right_false
= remove_range(right_orig
, min
, sub_one(rl_min(left_orig
)));
866 case SPECIAL_NOTEQUAL
:
867 if (!sval_is_max(rl_max(right_orig
)))
868 left_false
= remove_range(left_false
, add_one(rl_max(right_orig
)), max
);
869 if (!sval_is_min(rl_min(right_orig
)))
870 left_false
= remove_range(left_false
, min
, sub_one(rl_min(right_orig
)));
871 if (sval_cmp(rl_min(right_orig
), rl_max(right_orig
)) == 0)
872 left_true
= remove_range(left_orig
, rl_min(right_orig
), rl_min(right_orig
));
874 if (!sval_is_max(rl_max(left_orig
)))
875 right_false
= remove_range(right_false
, add_one(rl_max(left_orig
)), max
);
876 if (!sval_is_min(rl_min(left_orig
)))
877 right_false
= remove_range(right_false
, min
, sub_one(rl_min(left_orig
)));
878 if (sval_cmp(rl_min(left_orig
), rl_max(left_orig
)) == 0)
879 right_true
= remove_range(right_orig
, rl_min(left_orig
), rl_min(left_orig
));
885 left_true_state
= alloc_estate_rl(cast_rl(get_type(left
), left_true
));
886 left_false_state
= alloc_estate_rl(cast_rl(get_type(left
), left_false
));
887 right_true_state
= alloc_estate_rl(cast_rl(get_type(right
), right_true
));
888 right_false_state
= alloc_estate_rl(cast_rl(get_type(right
), right_false
));
892 case SPECIAL_UNSIGNED_LT
:
893 case SPECIAL_UNSIGNED_LTE
:
895 if (get_hard_max(right
, &dummy
))
896 estate_set_hard_max(left_true_state
);
897 if (get_hard_max(left
, &dummy
))
898 estate_set_hard_max(right_false_state
);
901 case SPECIAL_UNSIGNED_GT
:
902 case SPECIAL_UNSIGNED_GTE
:
904 if (get_hard_max(left
, &dummy
))
905 estate_set_hard_max(right_true_state
);
906 if (get_hard_max(right
, &dummy
))
907 estate_set_hard_max(left_false_state
);
911 if (get_hard_max(left
, &dummy
)) {
912 estate_set_hard_max(left_true_state
);
913 estate_set_hard_max(left_false_state
);
915 if (get_hard_max(right
, &dummy
)) {
916 estate_set_hard_max(right_true_state
);
917 estate_set_hard_max(right_false_state
);
920 if (left_postop
== SPECIAL_INCREMENT
) {
921 left_true_state
= increment_state(left_true_state
);
922 left_false_state
= increment_state(left_false_state
);
924 if (left_postop
== SPECIAL_DECREMENT
) {
925 left_true_state
= decrement_state(left_true_state
);
926 left_false_state
= decrement_state(left_false_state
);
928 if (right_postop
== SPECIAL_INCREMENT
) {
929 right_true_state
= increment_state(right_true_state
);
930 right_false_state
= increment_state(right_false_state
);
932 if (right_postop
== SPECIAL_DECREMENT
) {
933 right_true_state
= decrement_state(right_true_state
);
934 right_false_state
= decrement_state(right_false_state
);
937 set_extra_expr_true_false(left
, left_true_state
, left_false_state
);
938 set_extra_expr_true_false(right
, right_true_state
, right_false_state
);
941 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
942 void __extra_match_condition(struct expression
*expr
)
946 struct smatch_state
*pre_state
;
947 struct smatch_state
*true_state
;
948 struct smatch_state
*false_state
;
950 expr
= strip_expr(expr
);
951 switch (expr
->type
) {
953 function_comparison(SPECIAL_NOTEQUAL
, expr
, ll_to_sval(0), 1);
960 zero
= sval_blank(expr
);
963 name
= expr_to_var_sym(expr
, &sym
);
966 pre_state
= get_state(my_id
, name
, sym
);
967 true_state
= estate_filter_sval(pre_state
, zero
);
968 if (possibly_true(expr
, SPECIAL_EQUAL
, zero_expr()))
969 false_state
= alloc_estate_sval(zero
);
971 false_state
= alloc_estate_empty();
972 set_extra_true_false(name
, sym
, true_state
, false_state
);
977 match_comparison(expr
);
979 case EXPR_ASSIGNMENT
:
980 __extra_match_condition(expr
->left
);
985 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
986 int implied_not_equal(struct expression
*expr
, long long val
)
988 return !possibly_false(expr
, SPECIAL_NOTEQUAL
, value_expr(val
));
991 static void struct_member_callback(char *fn
, char *global_static
, int param
, char *printed_name
, struct smatch_state
*state
)
993 if (estate_is_whole(state
))
995 sm_msg("info: passes param_value '%s' %d '%s' %s %s", fn
, param
, printed_name
, state
->name
, global_static
);
998 static void db_limited_before(void)
1000 unmatched_slist
= clone_slist(__get_cur_slist());
1003 static void db_limited_after(void)
1005 free_slist(&unmatched_slist
);
1008 static void db_param_limit_filter(struct expression
*expr
, int param
, char *key
, char *value
)
1010 struct expression
*arg
;
1013 struct sm_state
*sm
;
1014 struct symbol
*type
;
1015 struct range_list
*rl
;
1016 struct range_list
*limit
;
1017 struct range_list
*new;
1019 while (expr
->type
== EXPR_ASSIGNMENT
)
1020 expr
= strip_expr(expr
->right
);
1021 if (expr
->type
!= EXPR_CALL
)
1024 arg
= get_argument_from_call_expr(expr
->args
, param
);
1028 name
= get_variable_from_key(arg
, key
, &sym
);
1032 sm
= get_sm_state(SMATCH_EXTRA
, name
, sym
);
1033 type
= get_member_type_from_key(arg
, key
);
1035 rl
= estate_rl(sm
->state
);
1037 rl
= alloc_whole_rl(type
);
1039 str_to_rl(type
, value
, &limit
);
1040 new = rl_intersection(rl
, limit
);
1042 /* We want to preserve the implications here */
1043 if (sm
&& rl_equiv(estate_rl(sm
->state
), new))
1046 set_extra_nomod(name
, sym
, alloc_estate_rl(new));
1052 static void db_param_add(struct expression
*expr
, int param
, char *key
, char *value
)
1054 struct expression
*arg
;
1057 struct symbol
*type
;
1058 struct smatch_state
*state
;
1059 struct range_list
*added
= NULL
;
1060 struct range_list
*new;
1062 while (expr
->type
== EXPR_ASSIGNMENT
)
1063 expr
= strip_expr(expr
->right
);
1064 if (expr
->type
!= EXPR_CALL
)
1067 arg
= get_argument_from_call_expr(expr
->args
, param
);
1070 name
= get_variable_from_key(arg
, key
, &sym
);
1074 type
= get_member_type_from_key(arg
, key
);
1075 state
= get_state(SMATCH_EXTRA
, name
, sym
);
1077 str_to_rl(type
, value
, &added
);
1078 new = rl_union(estate_rl(state
), added
);
1080 new = alloc_whole_rl(type
);
1083 set_extra_mod(name
, sym
, alloc_estate_rl(new));
1088 static void db_returned_states_param(struct expression
*expr
, int param
, char *key
, char *value
)
1090 struct expression
*arg
;
1091 struct symbol
*type
;
1092 struct range_list
*rl
;
1093 char member_name
[256];
1097 while (expr
->type
== EXPR_ASSIGNMENT
)
1098 expr
= strip_expr(expr
->right
);
1099 if (expr
->type
!= EXPR_CALL
)
1101 arg
= get_argument_from_call_expr(expr
->args
, param
);
1102 arg
= strip_expr(arg
);
1106 if (strcmp(key
, "*$$") == 0) {
1111 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
1112 arg
= strip_expr(arg
->unop
);
1113 name
= expr_to_var_sym(arg
, &sym
);
1115 char *tmp
= expr_to_var_sym(arg
, &sym
);
1119 snprintf(buf
, sizeof(buf
), "*%s", tmp
);
1120 name
= alloc_string(buf
);
1123 if (!name
|| !sym
) {
1127 type
= get_type(arg
);
1128 str_to_rl(type
, value
, &rl
);
1129 set_extra_mod(name
, sym
, alloc_estate_rl(rl
));
1134 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
1135 arg
= strip_expr(arg
->unop
);
1136 name
= expr_to_var_sym(arg
, &sym
);
1139 snprintf(member_name
, sizeof(member_name
), "%s.%s", name
, key
+ 4);
1141 name
= expr_to_var_sym(arg
, &sym
);
1144 snprintf(member_name
, sizeof(member_name
), "%s%s", name
, key
+ 2);
1146 type
= get_member_type_from_key(arg
, key
);
1149 str_to_rl(type
, value
, &rl
);
1150 set_extra_mod(member_name
, sym
, alloc_estate_rl(rl
));
1155 static void db_returned_member_info(struct expression
*expr
, int param
, char *key
, char *value
)
1157 struct symbol
*type
;
1160 char member_name
[256];
1161 struct range_list
*rl
;
1163 if (expr
->type
!= EXPR_ASSIGNMENT
)
1166 name
= expr_to_var_sym(expr
->left
, &sym
);
1169 snprintf(member_name
, sizeof(member_name
), "%s%s", name
, key
+ 2);
1170 type
= get_member_type_from_key(expr
->left
, key
);
1173 str_to_rl(type
, value
, &rl
);
1174 set_extra_mod(member_name
, sym
, alloc_estate_rl(rl
));
1180 static void returned_member_callback(int return_id
, char *return_ranges
, char *printed_name
, struct smatch_state
*state
)
1182 if (estate_is_whole(state
))
1185 sql_insert_return_states(return_id
, return_ranges
, RETURN_VALUE
, -1,
1186 printed_name
, state
->name
);
1189 static void match_call_info(struct expression
*expr
)
1191 struct range_list
*rl
= NULL
;
1192 struct expression
*arg
;
1193 struct symbol
*type
;
1197 name
= get_fnptr_name(expr
->fn
);
1201 FOR_EACH_PTR(expr
->args
, arg
) {
1202 type
= get_arg_type(expr
->fn
, i
);
1204 if (get_implied_rl(arg
, &rl
))
1205 rl
= cast_rl(type
, rl
);
1207 rl
= cast_rl(type
, alloc_whole_rl(get_type(arg
)));
1209 sm_msg("info: passes param_value '%s' %d '$$' %s %s",
1210 name
, i
, show_rl(rl
),
1211 is_static(expr
->fn
) ? "static" : "global");
1213 } END_FOR_EACH_PTR(arg
);
1218 static void set_param_value(const char *name
, struct symbol
*sym
, char *key
, char *value
)
1220 struct range_list
*rl
= NULL
;
1221 struct smatch_state
*state
;
1222 struct symbol
*type
;
1225 if (strncmp(key
, "$$", 2))
1228 snprintf(fullname
, 256, "%s%s", name
, key
+ 2);
1229 type
= get_member_type_from_key(symbol_expression(sym
), key
);
1230 str_to_rl(type
, value
, &rl
);
1231 state
= alloc_estate_rl(rl
);
1232 set_state(SMATCH_EXTRA
, fullname
, sym
, state
);
1235 void register_smatch_extra(int id
)
1239 add_merge_hook(my_id
, &merge_estates
);
1240 add_unmatched_state_hook(my_id
, &unmatched_state
);
1241 add_hook(&match_function_def
, FUNC_DEF_HOOK
);
1242 add_hook(&match_declarations
, DECLARATION_HOOK
);
1243 add_definition_db_callback(set_param_value
, PARAM_VALUE
);
1244 add_db_return_states_callback(RETURN_VALUE
, &db_returned_member_info
);
1245 add_db_return_states_callback(PARAM_VALUE
, &db_returned_states_param
);
1246 add_db_return_states_before(&db_limited_before
);
1247 add_db_return_states_callback(LIMITED_VALUE
, &db_param_limit_filter
);
1248 add_db_return_states_callback(FILTER_VALUE
, &db_param_limit_filter
);
1249 add_db_return_states_callback(ADDED_VALUE
, &db_param_add
);
1250 add_db_return_states_after(&db_limited_after
);
1253 void register_smatch_extra_late(int id
)
1255 add_hook(&match_dereferences
, DEREF_HOOK
);
1256 add_hook(&match_pointer_as_array
, OP_HOOK
);
1257 add_db_fn_call_callback(DEREFERENCE
, &set_param_dereferenced
);
1258 add_indirect_modification_hook(SMATCH_EXTRA
, reset_struct_members
);
1260 add_hook(&match_function_call
, FUNCTION_CALL_HOOK
);
1261 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
1262 add_hook(&unop_expr
, OP_HOOK
);
1263 add_hook(&asm_expr
, ASM_HOOK
);
1266 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
1267 add_member_info_callback(my_id
, struct_member_callback
);
1268 add_returned_member_callback(my_id
, returned_member_callback
);