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_cast(get_type(iter_var
), end
);
232 end
= sval_type_max(get_type(iter_var
));
234 if (get_sm_state_expr(SMATCH_EXTRA
, condition
->left
) != sm
)
237 switch (condition
->op
) {
238 case SPECIAL_NOTEQUAL
:
240 if (!sval_is_min(end
))
248 if (sval_cmp(end
, start
) < 0)
250 estate
= alloc_estate_range(start
, end
);
251 if (get_hard_max(condition
->right
, &dummy
))
252 estate_set_hard_max(estate
);
253 set_extra_expr_mod(iter_var
, estate
);
254 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
257 static struct sm_state
*handle_canonical_for_dec(struct expression
*iter_expr
,
258 struct expression
*condition
)
260 struct expression
*iter_var
;
262 struct smatch_state
*estate
;
265 iter_var
= iter_expr
->unop
;
266 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
269 if (!estate_get_single_value(sm
->state
, &start
))
271 if (!get_implied_min(condition
->right
, &end
))
272 end
= sval_type_min(get_type(iter_var
));
273 if (get_sm_state_expr(SMATCH_EXTRA
, condition
->left
) != sm
)
276 switch (condition
->op
) {
277 case SPECIAL_NOTEQUAL
:
279 if (!sval_is_min(end
) && !sval_is_max(end
))
287 if (sval_cmp(end
, start
) > 0)
289 estate
= alloc_estate_range(end
, start
);
290 estate_set_hard_max(estate
);
291 set_extra_expr_mod(iter_var
, estate
);
292 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
295 static struct sm_state
*handle_canonical_for_loops(struct statement
*loop
)
297 struct expression
*iter_expr
;
298 struct expression
*condition
;
300 if (!loop
->iterator_post_statement
)
302 if (loop
->iterator_post_statement
->type
!= STMT_EXPRESSION
)
304 iter_expr
= loop
->iterator_post_statement
->expression
;
305 if (!loop
->iterator_pre_condition
)
307 if (loop
->iterator_pre_condition
->type
!= EXPR_COMPARE
)
309 condition
= loop
->iterator_pre_condition
;
311 if (iter_expr
->op
== SPECIAL_INCREMENT
)
312 return handle_canonical_for_inc(iter_expr
, condition
);
313 if (iter_expr
->op
== SPECIAL_DECREMENT
)
314 return handle_canonical_for_dec(iter_expr
, condition
);
318 struct sm_state
*__extra_handle_canonical_loops(struct statement
*loop
, struct state_list
**slist
)
320 struct sm_state
*ret
;
322 __push_fake_cur_slist();
323 if (!loop
->iterator_post_statement
)
324 ret
= handle_canonical_while_count_down(loop
);
326 ret
= handle_canonical_for_loops(loop
);
327 *slist
= __pop_fake_cur_slist();
331 int __iterator_unchanged(struct sm_state
*sm
)
335 if (get_sm_state(my_id
, sm
->name
, sm
->sym
) == sm
)
340 static void while_count_down_after(struct sm_state
*sm
, struct expression
*condition
)
344 /* paranoid checking. prolly not needed */
345 condition
= strip_expr(condition
);
348 if (condition
->type
!= EXPR_PREOP
&& condition
->type
!= EXPR_POSTOP
)
350 if (condition
->op
!= SPECIAL_DECREMENT
)
352 after_value
= estate_min(sm
->state
);
354 set_extra_mod(sm
->name
, sm
->sym
, alloc_estate_sval(after_value
));
357 void __extra_pre_loop_hook_after(struct sm_state
*sm
,
358 struct statement
*iterator
,
359 struct expression
*condition
)
361 struct expression
*iter_expr
;
363 struct smatch_state
*state
;
366 while_count_down_after(sm
, condition
);
370 iter_expr
= iterator
->expression
;
372 if (condition
->type
!= EXPR_COMPARE
)
374 if (iter_expr
->op
== SPECIAL_INCREMENT
) {
375 limit
= sval_binop(estate_max(sm
->state
), '+',
376 sval_type_val(estate_type(sm
->state
), 1));
378 limit
= sval_binop(estate_min(sm
->state
), '-',
379 sval_type_val(estate_type(sm
->state
), 1));
381 if (!estate_has_hard_max(sm
->state
) && !__has_breaks()) {
382 if (iter_expr
->op
== SPECIAL_INCREMENT
)
383 state
= alloc_estate_range(estate_min(sm
->state
), limit
);
385 state
= alloc_estate_range(limit
, estate_max(sm
->state
));
387 state
= alloc_estate_sval(limit
);
389 if (!estate_has_hard_max(sm
->state
))
390 estate_clear_hard_max(state
);
391 set_extra_mod(sm
->name
, sm
->sym
, state
);
394 static struct state_list
*unmatched_slist
;
395 static struct smatch_state
*unmatched_state(struct sm_state
*sm
)
397 struct smatch_state
*state
;
399 if (unmatched_slist
) {
400 state
= get_state_slist(unmatched_slist
, SMATCH_EXTRA
, sm
->name
, sm
->sym
);
404 return alloc_estate_whole(estate_type(sm
->state
));
407 static void clear_the_pointed_at(struct expression
*expr
, struct state_list
*slist
)
411 struct sm_state
*tmp
;
413 name
= expr_to_var_sym(expr
, &sym
);
417 FOR_EACH_PTR(slist
, tmp
) {
418 if (tmp
->name
[0] != '*')
422 if (strcmp(tmp
->name
+ 1, name
) != 0)
424 set_extra_mod(tmp
->name
, tmp
->sym
, alloc_estate_whole(estate_type(tmp
->state
)));
425 } END_FOR_EACH_PTR(tmp
);
431 static void match_function_call(struct expression
*expr
)
433 struct expression
*arg
;
434 struct expression
*tmp
;
435 struct state_list
*slist
;
437 slist
= get_all_states(SMATCH_EXTRA
);
439 FOR_EACH_PTR(expr
->args
, arg
) {
440 tmp
= strip_expr(arg
);
441 if (tmp
->type
== EXPR_PREOP
&& tmp
->op
== '&')
442 set_extra_expr_mod(tmp
->unop
, alloc_estate_whole(get_type(tmp
->unop
)));
444 clear_the_pointed_at(tmp
, slist
);
445 } END_FOR_EACH_PTR(arg
);
450 static int types_equiv_or_pointer(struct symbol
*one
, struct symbol
*two
)
454 if (one
->type
== SYM_PTR
&& two
->type
== SYM_PTR
)
456 return types_equiv(one
, two
);
459 static int op_remove_assign(int op
)
462 case SPECIAL_ADD_ASSIGN
:
464 case SPECIAL_SUB_ASSIGN
:
466 case SPECIAL_MUL_ASSIGN
:
468 case SPECIAL_DIV_ASSIGN
:
470 case SPECIAL_MOD_ASSIGN
:
472 case SPECIAL_AND_ASSIGN
:
474 case SPECIAL_OR_ASSIGN
:
476 case SPECIAL_XOR_ASSIGN
:
478 case SPECIAL_SHL_ASSIGN
:
479 return SPECIAL_LEFTSHIFT
;
480 case SPECIAL_SHR_ASSIGN
:
481 return SPECIAL_RIGHTSHIFT
;
487 static void match_assign(struct expression
*expr
)
489 struct range_list
*rl
= NULL
;
490 struct expression
*left
;
491 struct expression
*right
;
492 struct expression
*binop_expr
;
493 struct symbol
*right_sym
;
494 struct symbol
*left_type
;
495 struct symbol
*right_type
;
496 char *right_name
= NULL
;
501 sval_t right_min
, right_max
;
504 left
= strip_expr(expr
->left
);
506 right
= strip_expr(expr
->right
);
507 while (right
->type
== EXPR_ASSIGNMENT
&& right
->op
== '=')
508 right
= strip_expr(right
->left
);
510 if (is_condition(expr
->right
))
511 return; /* handled in smatch_condition.c */
512 if (expr
->op
== '=' && right
->type
== EXPR_CALL
)
513 return; /* handled in smatch_function_hooks.c */
515 name
= expr_to_var_sym(left
, &sym
);
519 left_type
= get_type(left
);
520 right_type
= get_type(right
);
522 right_name
= expr_to_var_sym(right
, &right_sym
);
523 if (expr
->op
== '=' && right_name
&& right_sym
&&
524 types_equiv_or_pointer(left_type
, right_type
)) {
525 set_equiv(left
, right
);
529 right_min
= sval_type_min(right_type
);
530 right_max
= sval_type_max(right_type
);
532 known
= get_implied_value(right
, &value
);
535 struct smatch_state
*state
;
537 if (get_implied_rl(right
, &rl
)) {
538 rl
= cast_rl(left_type
, rl
);
539 state
= alloc_estate_rl(rl
);
540 if (get_hard_max(right
, &tmp
))
541 estate_set_hard_max(state
);
543 rl
= alloc_whole_rl(right_type
);
544 rl
= cast_rl(left_type
, rl
);
545 state
= alloc_estate_rl(rl
);
547 set_extra_mod(name
, sym
, state
);
550 case SPECIAL_ADD_ASSIGN
:
551 if (get_implied_min(left
, &tmp
)) {
553 right_min
= sval_binop(tmp
, '+', value
);
557 if (!inside_loop() && known
&& get_implied_max(left
, &tmp
))
558 right_max
= sval_binop(tmp
, '+', value
);
560 case SPECIAL_SUB_ASSIGN
:
561 if (get_implied_max(left
, &tmp
)) {
563 right_max
= sval_binop(tmp
, '-', value
);
567 if (!inside_loop() && known
&& get_implied_min(left
, &tmp
))
568 right_min
= sval_binop(tmp
, '-', value
);
570 case SPECIAL_AND_ASSIGN
:
571 case SPECIAL_MOD_ASSIGN
:
572 binop_expr
= binop_expression(expr
->left
,
573 op_remove_assign(expr
->op
),
575 if (get_implied_rl(binop_expr
, &rl
)) {
576 rl
= cast_rl(left_type
, rl
);
577 set_extra_mod(name
, sym
, alloc_estate_rl(rl
));
581 if (!sval_is_min(right_min
) || !sval_is_max(right_max
))
582 rl
= cast_rl(left_type
, alloc_rl(right_min
, right_max
));
584 rl
= alloc_whole_rl(left_type
);
585 set_extra_mod(name
, sym
, alloc_estate_rl(rl
));
587 free_string(right_name
);
591 static void reset_struct_members(struct sm_state
*sm
)
593 if (!estate_rl(sm
->state
))
595 set_extra_mod(sm
->name
, sm
->sym
, alloc_estate_whole(estate_type(sm
->state
)));
598 static struct smatch_state
*increment_state(struct smatch_state
*state
)
600 sval_t min
= estate_min(state
);
601 sval_t max
= estate_max(state
);
603 if (!estate_rl(state
))
607 max
= sval_type_max(max
.type
);
609 if (!sval_is_min(min
) && !sval_is_max(min
))
611 if (!sval_is_min(max
) && !sval_is_max(max
))
613 return alloc_estate_range(min
, max
);
616 static struct smatch_state
*decrement_state(struct smatch_state
*state
)
618 sval_t min
= estate_min(state
);
619 sval_t max
= estate_max(state
);
621 if (!estate_rl(state
))
625 min
= sval_type_min(min
.type
);
627 if (!sval_is_min(min
) && !sval_is_max(min
))
629 if (!sval_is_min(max
) && !sval_is_max(max
))
631 return alloc_estate_range(min
, max
);
634 static void unop_expr(struct expression
*expr
)
636 struct smatch_state
*state
;
638 if (expr
->smatch_flags
& Handled
)
642 case SPECIAL_INCREMENT
:
643 state
= get_state_expr(SMATCH_EXTRA
, expr
->unop
);
644 state
= increment_state(state
);
646 state
= alloc_estate_whole(get_type(expr
));
647 set_extra_expr_mod(expr
->unop
, state
);
649 case SPECIAL_DECREMENT
:
650 state
= get_state_expr(SMATCH_EXTRA
, expr
->unop
);
651 state
= decrement_state(state
);
653 state
= alloc_estate_whole(get_type(expr
));
654 set_extra_expr_mod(expr
->unop
, state
);
661 static void asm_expr(struct statement
*stmt
)
664 struct expression
*expr
;
668 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
670 case 0: /* identifier */
671 case 1: /* constraint */
674 case 2: /* expression */
676 type
= get_type(strip_expr(expr
));
677 set_extra_expr_mod(expr
, alloc_estate_whole(type
));
680 } END_FOR_EACH_PTR(expr
);
683 static void delete_state_tracker(struct tracker
*t
)
685 remove_from_equiv(t
->name
, t
->sym
);
686 delete_state(t
->owner
, t
->name
, t
->sym
);
689 static void scoped_state_extra(const char *name
, struct symbol
*sym
)
693 t
= alloc_tracker(SMATCH_EXTRA
, name
, sym
);
694 add_scope_hook((scope_hook
*)&delete_state_tracker
, t
);
697 static void match_declarations(struct symbol
*sym
)
702 name
= sym
->ident
->name
;
703 if (!sym
->initializer
) {
704 set_state(SMATCH_EXTRA
, name
, sym
, alloc_estate_whole(get_real_base_type(sym
)));
705 scoped_state_extra(name
, sym
);
710 static void check_dereference(struct expression
*expr
)
712 set_extra_expr_nomod(expr
, alloc_estate_range(valid_ptr_min_sval
, valid_ptr_max_sval
));
715 static void match_dereferences(struct expression
*expr
)
717 if (expr
->type
!= EXPR_PREOP
)
719 check_dereference(expr
->unop
);
722 static void match_pointer_as_array(struct expression
*expr
)
726 check_dereference(expr
->unop
->left
);
729 static void set_param_dereferenced(struct expression
*arg
, char *unused
)
731 check_dereference(arg
);
734 static void match_function_def(struct symbol
*sym
)
738 FOR_EACH_PTR(sym
->ctype
.base_type
->arguments
, arg
) {
741 set_state(my_id
, arg
->ident
->name
, arg
, alloc_estate_whole(get_real_base_type(arg
)));
742 } END_FOR_EACH_PTR(arg
);
745 static int match_func_comparison(struct expression
*expr
)
747 struct expression
*left
= strip_expr(expr
->left
);
748 struct expression
*right
= strip_expr(expr
->right
);
751 if (left
->type
== EXPR_CALL
) {
752 if (!get_implied_value(right
, &sval
))
754 function_comparison(expr
->op
, left
, sval
, 1);
758 if (right
->type
== EXPR_CALL
) {
759 if (!get_implied_value(left
, &sval
))
761 function_comparison(expr
->op
, right
, sval
, 0);
768 static sval_t
add_one(sval_t sval
)
774 static sval_t
sub_one(sval_t sval
)
780 static void match_comparison(struct expression
*expr
)
782 struct expression
*left
= strip_expr(expr
->left
);
783 struct expression
*right
= strip_expr(expr
->right
);
785 struct range_list
*left_orig
;
786 struct range_list
*left_true
;
787 struct range_list
*left_false
;
788 struct range_list
*right_orig
;
789 struct range_list
*right_true
;
790 struct range_list
*right_false
;
791 struct smatch_state
*left_true_state
;
792 struct smatch_state
*left_false_state
;
793 struct smatch_state
*right_true_state
;
794 struct smatch_state
*right_false_state
;
795 sval_t min
, max
, dummy
;
797 int right_postop
= 0;
799 if (match_func_comparison(expr
))
802 if (left
->op
== SPECIAL_INCREMENT
|| left
->op
== SPECIAL_DECREMENT
) {
803 if (left
->type
== EXPR_POSTOP
) {
804 left
->smatch_flags
|= Handled
;
805 left_postop
= left
->op
;
807 left
= strip_expr(left
->unop
);
809 while (left
->type
== EXPR_ASSIGNMENT
)
810 left
= strip_expr(left
->left
);
812 if (right
->op
== SPECIAL_INCREMENT
|| right
->op
== SPECIAL_DECREMENT
) {
813 if (right
->type
== EXPR_POSTOP
) {
814 right
->smatch_flags
|= Handled
;
815 right_postop
= right
->op
;
817 right
= strip_expr(right
->unop
);
820 type
= get_type(expr
);
824 if (get_implied_rl(left
, &left_orig
)) {
825 left_orig
= cast_rl(type
, left_orig
);
827 min
= sval_type_min(get_type(left
));
828 max
= sval_type_max(get_type(left
));
829 left_orig
= cast_rl(type
, alloc_rl(min
, max
));
832 if (get_implied_rl(right
, &right_orig
)) {
833 right_orig
= cast_rl(type
, right_orig
);
835 min
= sval_type_min(get_type(right
));
836 max
= sval_type_max(get_type(right
));
837 right_orig
= cast_rl(type
, alloc_rl(min
, max
));
839 min
= sval_type_min(type
);
840 max
= sval_type_max(type
);
842 left_true
= clone_rl(left_orig
);
843 left_false
= clone_rl(left_orig
);
844 right_true
= clone_rl(right_orig
);
845 right_false
= clone_rl(right_orig
);
849 case SPECIAL_UNSIGNED_LT
:
850 left_true
= remove_range(left_orig
, rl_max(right_orig
), max
);
851 if (!sval_is_min(rl_min(right_orig
))) {
852 left_false
= remove_range(left_orig
, min
, sub_one(rl_min(right_orig
)));
855 right_true
= remove_range(right_orig
, min
, rl_min(left_orig
));
856 if (!sval_is_max(rl_max(left_orig
)))
857 right_false
= remove_range(right_orig
, add_one(rl_max(left_orig
)), max
);
859 case SPECIAL_UNSIGNED_LTE
:
861 if (!sval_is_max(rl_max(right_orig
)))
862 left_true
= remove_range(left_orig
, add_one(rl_max(right_orig
)), max
);
863 left_false
= remove_range(left_orig
, min
, rl_min(right_orig
));
865 if (!sval_is_min(rl_min(left_orig
)))
866 right_true
= remove_range(right_orig
, min
, sub_one(rl_min(left_orig
)));
867 right_false
= remove_range(right_orig
, rl_max(left_orig
), max
);
869 if (sval_cmp(rl_min(left_orig
), rl_min(right_orig
)) == 0)
870 left_false
= remove_range(left_false
, rl_min(left_orig
), rl_min(left_orig
));
871 if (sval_cmp(rl_max(left_orig
), rl_max(right_orig
)) == 0)
872 right_false
= remove_range(right_false
, rl_max(left_orig
), rl_max(left_orig
));
875 if (!sval_is_max(rl_max(right_orig
))) {
876 left_true
= remove_range(left_true
, add_one(rl_max(right_orig
)), max
);
878 if (!sval_is_min(rl_min(right_orig
))) {
879 left_true
= remove_range(left_true
, min
, sub_one(rl_min(right_orig
)));
881 if (sval_cmp(rl_min(right_orig
), rl_max(right_orig
)) == 0)
882 left_false
= remove_range(left_orig
, rl_min(right_orig
), rl_min(right_orig
));
884 if (!sval_is_max(rl_max(left_orig
)))
885 right_true
= remove_range(right_true
, add_one(rl_max(left_orig
)), max
);
886 if (!sval_is_min(rl_min(left_orig
)))
887 right_true
= remove_range(right_true
, min
, sub_one(rl_min(left_orig
)));
888 if (sval_cmp(rl_min(left_orig
), rl_max(left_orig
)) == 0)
889 right_false
= remove_range(right_orig
, rl_min(left_orig
), rl_min(left_orig
));
891 case SPECIAL_UNSIGNED_GTE
:
893 if (!sval_is_min(rl_min(right_orig
)))
894 left_true
= remove_range(left_orig
, min
, sub_one(rl_min(right_orig
)));
895 left_false
= remove_range(left_orig
, rl_max(right_orig
), max
);
897 if (!sval_is_max(rl_max(left_orig
)))
898 right_true
= remove_range(right_orig
, add_one(rl_max(left_orig
)), max
);
899 right_false
= remove_range(right_orig
, min
, rl_min(left_orig
));
901 if (sval_cmp(rl_min(left_orig
), rl_min(right_orig
)) == 0)
902 right_false
= remove_range(right_false
, rl_min(left_orig
), rl_min(left_orig
));
903 if (sval_cmp(rl_max(left_orig
), rl_max(right_orig
)) == 0)
904 left_false
= remove_range(left_false
, rl_max(left_orig
), rl_max(left_orig
));
907 case SPECIAL_UNSIGNED_GT
:
908 left_true
= remove_range(left_orig
, min
, rl_min(right_orig
));
909 if (!sval_is_max(rl_max(right_orig
)))
910 left_false
= remove_range(left_orig
, add_one(rl_max(right_orig
)), max
);
912 right_true
= remove_range(right_orig
, rl_max(left_orig
), max
);
913 if (!sval_is_min(rl_min(left_orig
)))
914 right_false
= remove_range(right_orig
, min
, sub_one(rl_min(left_orig
)));
916 case SPECIAL_NOTEQUAL
:
917 if (!sval_is_max(rl_max(right_orig
)))
918 left_false
= remove_range(left_false
, add_one(rl_max(right_orig
)), max
);
919 if (!sval_is_min(rl_min(right_orig
)))
920 left_false
= remove_range(left_false
, min
, sub_one(rl_min(right_orig
)));
921 if (sval_cmp(rl_min(right_orig
), rl_max(right_orig
)) == 0)
922 left_true
= remove_range(left_orig
, rl_min(right_orig
), rl_min(right_orig
));
924 if (!sval_is_max(rl_max(left_orig
)))
925 right_false
= remove_range(right_false
, add_one(rl_max(left_orig
)), max
);
926 if (!sval_is_min(rl_min(left_orig
)))
927 right_false
= remove_range(right_false
, min
, sub_one(rl_min(left_orig
)));
928 if (sval_cmp(rl_min(left_orig
), rl_max(left_orig
)) == 0)
929 right_true
= remove_range(right_orig
, rl_min(left_orig
), rl_min(left_orig
));
935 left_true
= rl_truncate_cast(get_type(left
), left_true
);
936 left_false
= rl_truncate_cast(get_type(left
), left_false
);
937 right_true
= rl_truncate_cast(get_type(right
), right_true
);
938 right_false
= rl_truncate_cast(get_type(right
), right_false
);
940 left_true_state
= alloc_estate_rl(left_true
);
941 left_false_state
= alloc_estate_rl(left_false
);
942 right_true_state
= alloc_estate_rl(right_true
);
943 right_false_state
= alloc_estate_rl(right_false
);
947 case SPECIAL_UNSIGNED_LT
:
948 case SPECIAL_UNSIGNED_LTE
:
950 if (get_hard_max(right
, &dummy
))
951 estate_set_hard_max(left_true_state
);
952 if (get_hard_max(left
, &dummy
))
953 estate_set_hard_max(right_false_state
);
956 case SPECIAL_UNSIGNED_GT
:
957 case SPECIAL_UNSIGNED_GTE
:
959 if (get_hard_max(left
, &dummy
))
960 estate_set_hard_max(right_true_state
);
961 if (get_hard_max(right
, &dummy
))
962 estate_set_hard_max(left_false_state
);
966 if (get_hard_max(left
, &dummy
)) {
967 estate_set_hard_max(left_true_state
);
968 estate_set_hard_max(left_false_state
);
970 if (get_hard_max(right
, &dummy
)) {
971 estate_set_hard_max(right_true_state
);
972 estate_set_hard_max(right_false_state
);
975 if (left_postop
== SPECIAL_INCREMENT
) {
976 left_true_state
= increment_state(left_true_state
);
977 left_false_state
= increment_state(left_false_state
);
979 if (left_postop
== SPECIAL_DECREMENT
) {
980 left_true_state
= decrement_state(left_true_state
);
981 left_false_state
= decrement_state(left_false_state
);
983 if (right_postop
== SPECIAL_INCREMENT
) {
984 right_true_state
= increment_state(right_true_state
);
985 right_false_state
= increment_state(right_false_state
);
987 if (right_postop
== SPECIAL_DECREMENT
) {
988 right_true_state
= decrement_state(right_true_state
);
989 right_false_state
= decrement_state(right_false_state
);
992 set_extra_expr_true_false(left
, left_true_state
, left_false_state
);
993 set_extra_expr_true_false(right
, right_true_state
, right_false_state
);
996 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
997 void __extra_match_condition(struct expression
*expr
)
1001 struct smatch_state
*pre_state
;
1002 struct smatch_state
*true_state
;
1003 struct smatch_state
*false_state
;
1005 expr
= strip_expr(expr
);
1006 switch (expr
->type
) {
1008 function_comparison(SPECIAL_NOTEQUAL
, expr
, ll_to_sval(0), 1);
1015 zero
= sval_blank(expr
);
1018 name
= expr_to_var_sym(expr
, &sym
);
1021 pre_state
= get_state(my_id
, name
, sym
);
1022 true_state
= estate_filter_sval(pre_state
, zero
);
1023 if (possibly_true(expr
, SPECIAL_EQUAL
, zero_expr()))
1024 false_state
= alloc_estate_sval(zero
);
1026 false_state
= alloc_estate_empty();
1027 set_extra_true_false(name
, sym
, true_state
, false_state
);
1032 match_comparison(expr
);
1034 case EXPR_ASSIGNMENT
:
1035 __extra_match_condition(expr
->left
);
1040 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
1041 int implied_not_equal(struct expression
*expr
, long long val
)
1043 return !possibly_false(expr
, SPECIAL_NOTEQUAL
, value_expr(val
));
1046 static void struct_member_callback(struct expression
*call
, int param
, char *printed_name
, struct smatch_state
*state
)
1048 if (estate_is_whole(state
))
1050 sql_insert_caller_info(call
, PARAM_VALUE
, param
, printed_name
, state
->name
);
1053 static void db_limited_before(void)
1055 unmatched_slist
= clone_slist(__get_cur_slist());
1058 static void db_limited_after(void)
1060 free_slist(&unmatched_slist
);
1063 static void db_param_limit_filter(struct expression
*expr
, int param
, char *key
, char *value
)
1065 struct expression
*arg
;
1068 struct sm_state
*sm
;
1069 struct symbol
*type
;
1070 struct range_list
*rl
;
1071 struct range_list
*limit
;
1072 struct range_list
*new;
1074 while (expr
->type
== EXPR_ASSIGNMENT
)
1075 expr
= strip_expr(expr
->right
);
1076 if (expr
->type
!= EXPR_CALL
)
1079 arg
= get_argument_from_call_expr(expr
->args
, param
);
1083 name
= get_variable_from_key(arg
, key
, &sym
);
1087 sm
= get_sm_state(SMATCH_EXTRA
, name
, sym
);
1088 type
= get_member_type_from_key(arg
, key
);
1090 rl
= estate_rl(sm
->state
);
1092 rl
= alloc_whole_rl(type
);
1094 str_to_rl(type
, value
, &limit
);
1095 new = rl_intersection(rl
, limit
);
1097 /* We want to preserve the implications here */
1098 if (sm
&& rl_equiv(estate_rl(sm
->state
), new))
1101 set_extra_nomod(name
, sym
, alloc_estate_rl(new));
1107 static void db_param_add(struct expression
*expr
, int param
, char *key
, char *value
)
1109 struct expression
*arg
;
1112 struct symbol
*type
;
1113 struct smatch_state
*state
;
1114 struct range_list
*added
= NULL
;
1115 struct range_list
*new;
1117 while (expr
->type
== EXPR_ASSIGNMENT
)
1118 expr
= strip_expr(expr
->right
);
1119 if (expr
->type
!= EXPR_CALL
)
1122 arg
= get_argument_from_call_expr(expr
->args
, param
);
1125 name
= get_variable_from_key(arg
, key
, &sym
);
1129 type
= get_member_type_from_key(arg
, key
);
1130 state
= get_state(SMATCH_EXTRA
, name
, sym
);
1132 str_to_rl(type
, value
, &added
);
1133 new = rl_union(estate_rl(state
), added
);
1135 new = alloc_whole_rl(type
);
1138 set_extra_mod(name
, sym
, alloc_estate_rl(new));
1143 static void db_returned_states_param(struct expression
*expr
, int param
, char *key
, char *value
)
1145 struct expression
*arg
;
1146 struct symbol
*type
;
1147 struct range_list
*rl
;
1148 char member_name
[256];
1152 while (expr
->type
== EXPR_ASSIGNMENT
)
1153 expr
= strip_expr(expr
->right
);
1154 if (expr
->type
!= EXPR_CALL
)
1156 arg
= get_argument_from_call_expr(expr
->args
, param
);
1157 arg
= strip_expr(arg
);
1161 if (strcmp(key
, "*$$") == 0) {
1166 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
1167 arg
= strip_expr(arg
->unop
);
1168 name
= expr_to_var_sym(arg
, &sym
);
1170 char *tmp
= expr_to_var_sym(arg
, &sym
);
1174 snprintf(buf
, sizeof(buf
), "*%s", tmp
);
1175 name
= alloc_string(buf
);
1178 if (!name
|| !sym
) {
1182 type
= get_type(arg
);
1183 str_to_rl(type
, value
, &rl
);
1184 set_extra_mod(name
, sym
, alloc_estate_rl(rl
));
1189 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
1190 arg
= strip_expr(arg
->unop
);
1191 name
= expr_to_var_sym(arg
, &sym
);
1194 snprintf(member_name
, sizeof(member_name
), "%s.%s", name
, key
+ 4);
1196 name
= expr_to_var_sym(arg
, &sym
);
1199 snprintf(member_name
, sizeof(member_name
), "%s%s", name
, key
+ 2);
1201 type
= get_member_type_from_key(arg
, key
);
1204 str_to_rl(type
, value
, &rl
);
1205 set_extra_mod(member_name
, sym
, alloc_estate_rl(rl
));
1210 static void db_returned_member_info(struct expression
*expr
, int param
, char *key
, char *value
)
1212 struct symbol
*type
;
1215 char member_name
[256];
1216 struct range_list
*rl
;
1218 if (expr
->type
!= EXPR_ASSIGNMENT
)
1221 name
= expr_to_var_sym(expr
->left
, &sym
);
1224 snprintf(member_name
, sizeof(member_name
), "%s%s", name
, key
+ 2);
1225 type
= get_member_type_from_key(expr
->left
, key
);
1228 str_to_rl(type
, value
, &rl
);
1229 set_extra_mod(member_name
, sym
, alloc_estate_rl(rl
));
1235 static void returned_member_callback(int return_id
, char *return_ranges
, char *printed_name
, struct smatch_state
*state
)
1237 if (estate_is_whole(state
))
1240 sql_insert_return_states(return_id
, return_ranges
, RETURN_VALUE
, -1,
1241 printed_name
, state
->name
);
1244 static void match_call_info(struct expression
*expr
)
1246 struct range_list
*rl
= NULL
;
1247 struct expression
*arg
;
1248 struct symbol
*type
;
1251 FOR_EACH_PTR(expr
->args
, arg
) {
1252 type
= get_arg_type(expr
->fn
, i
);
1254 if (get_implied_rl(arg
, &rl
))
1255 rl
= cast_rl(type
, rl
);
1257 rl
= cast_rl(type
, alloc_whole_rl(get_type(arg
)));
1259 sql_insert_caller_info(expr
, PARAM_VALUE
, i
, "$$", show_rl(rl
));
1261 } END_FOR_EACH_PTR(arg
);
1264 static void set_param_value(const char *name
, struct symbol
*sym
, char *key
, char *value
)
1266 struct range_list
*rl
= NULL
;
1267 struct smatch_state
*state
;
1268 struct symbol
*type
;
1271 if (strcmp(key
, "*$$") == 0)
1272 snprintf(fullname
, sizeof(fullname
), "*%s", name
);
1273 else if (strncmp(key
, "$$", 2) == 0)
1274 snprintf(fullname
, 256, "%s%s", name
, key
+ 2);
1278 type
= get_member_type_from_key(symbol_expression(sym
), key
);
1279 str_to_rl(type
, value
, &rl
);
1280 state
= alloc_estate_rl(rl
);
1281 set_state(SMATCH_EXTRA
, fullname
, sym
, state
);
1284 void register_smatch_extra(int id
)
1288 add_merge_hook(my_id
, &merge_estates
);
1289 add_unmatched_state_hook(my_id
, &unmatched_state
);
1290 add_hook(&match_function_def
, FUNC_DEF_HOOK
);
1291 add_hook(&match_declarations
, DECLARATION_HOOK
);
1292 add_definition_db_callback(set_param_value
, PARAM_VALUE
);
1293 add_db_return_states_callback(RETURN_VALUE
, &db_returned_member_info
);
1294 add_db_return_states_callback(PARAM_VALUE
, &db_returned_states_param
);
1295 add_db_return_states_before(&db_limited_before
);
1296 add_db_return_states_callback(LIMITED_VALUE
, &db_param_limit_filter
);
1297 add_db_return_states_callback(FILTER_VALUE
, &db_param_limit_filter
);
1298 add_db_return_states_callback(ADDED_VALUE
, &db_param_add
);
1299 add_db_return_states_after(&db_limited_after
);
1302 void register_smatch_extra_late(int id
)
1304 add_hook(&match_dereferences
, DEREF_HOOK
);
1305 add_hook(&match_pointer_as_array
, OP_HOOK
);
1306 add_db_fn_call_callback(DEREFERENCE
, &set_param_dereferenced
);
1307 add_indirect_modification_hook(SMATCH_EXTRA
, reset_struct_members
);
1309 add_hook(&match_function_call
, FUNCTION_CALL_HOOK
);
1310 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
1311 add_hook(&unop_expr
, OP_HOOK
);
1312 add_hook(&asm_expr
, ASM_HOOK
);
1314 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
1315 add_member_info_callback(my_id
, struct_member_callback
);
1316 add_returned_member_callback(my_id
, returned_member_callback
);