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 /* if we have the db this is handled in smatch_function_hooks.c */
440 if (inlinable(expr
->fn
))
443 slist
= get_all_states(SMATCH_EXTRA
);
445 FOR_EACH_PTR(expr
->args
, arg
) {
446 tmp
= strip_expr(arg
);
447 if (tmp
->type
== EXPR_PREOP
&& tmp
->op
== '&')
448 set_extra_expr_mod(tmp
->unop
, alloc_estate_whole(get_type(tmp
->unop
)));
450 clear_the_pointed_at(tmp
, slist
);
451 } END_FOR_EACH_PTR(arg
);
456 static int types_equiv_or_pointer(struct symbol
*one
, struct symbol
*two
)
460 if (one
->type
== SYM_PTR
&& two
->type
== SYM_PTR
)
462 return types_equiv(one
, two
);
465 static void match_vanilla_assign(struct expression
*left
, struct expression
*right
)
467 struct range_list
*rl
= NULL
;
468 struct symbol
*right_sym
;
469 struct symbol
*left_type
;
470 struct symbol
*right_type
;
471 char *right_name
= NULL
;
475 struct smatch_state
*state
;
477 name
= expr_to_var_sym(left
, &sym
);
481 left_type
= get_type(left
);
482 right_type
= get_type(right
);
484 right_name
= expr_to_var_sym(right
, &right_sym
);
485 if (right_name
&& right_sym
&&
486 types_equiv_or_pointer(left_type
, right_type
)) {
487 set_equiv(left
, right
);
491 if (get_implied_rl(right
, &rl
)) {
492 rl
= cast_rl(left_type
, rl
);
493 state
= alloc_estate_rl(rl
);
494 if (get_hard_max(right
, &tmp
))
495 estate_set_hard_max(state
);
497 rl
= alloc_whole_rl(right_type
);
498 rl
= cast_rl(left_type
, rl
);
499 state
= alloc_estate_rl(rl
);
501 set_extra_mod(name
, sym
, state
);
503 free_string(right_name
);
506 static int op_remove_assign(int op
)
509 case SPECIAL_ADD_ASSIGN
:
511 case SPECIAL_SUB_ASSIGN
:
513 case SPECIAL_MUL_ASSIGN
:
515 case SPECIAL_DIV_ASSIGN
:
517 case SPECIAL_MOD_ASSIGN
:
519 case SPECIAL_AND_ASSIGN
:
521 case SPECIAL_OR_ASSIGN
:
523 case SPECIAL_XOR_ASSIGN
:
525 case SPECIAL_SHL_ASSIGN
:
526 return SPECIAL_LEFTSHIFT
;
527 case SPECIAL_SHR_ASSIGN
:
528 return SPECIAL_RIGHTSHIFT
;
534 static void match_assign(struct expression
*expr
)
536 struct range_list
*rl
= NULL
;
537 struct expression
*left
;
538 struct expression
*right
;
539 struct expression
*binop_expr
;
540 struct symbol
*left_type
;
541 struct symbol
*right_type
;
544 sval_t left_min
, left_max
;
545 sval_t right_min
, right_max
;
546 sval_t res_min
, res_max
;
548 left
= strip_expr(expr
->left
);
550 right
= strip_expr(expr
->right
);
551 while (right
->type
== EXPR_ASSIGNMENT
&& right
->op
== '=')
552 right
= strip_expr(right
->left
);
554 if (is_condition(expr
->right
))
555 return; /* handled in smatch_condition.c */
556 if (expr
->op
== '=' && right
->type
== EXPR_CALL
)
557 return; /* handled in smatch_function_hooks.c */
558 if (expr
->op
== '=') {
559 match_vanilla_assign(left
, right
);
563 name
= expr_to_var_sym(left
, &sym
);
567 left_type
= get_type(left
);
568 right_type
= get_type(right
);
570 res_min
= sval_type_min(right_type
);
571 res_max
= sval_type_max(right_type
);
574 case SPECIAL_ADD_ASSIGN
:
575 get_absolute_max(left
, &left_max
);
576 get_absolute_max(right
, &right_max
);
577 if (sval_binop_overflows(left_max
, '+', right_max
))
579 if (get_implied_min(left
, &left_min
) &&
580 !sval_is_negative_min(left_min
) &&
581 get_implied_min(right
, &right_min
) &&
582 !sval_is_negative_min(right_min
)) {
583 res_min
= sval_binop(left_min
, '+', right_min
);
584 res_min
= sval_cast(right_type
, res_min
);
586 if (inside_loop()) /* we are assuming loops don't lead to wrapping */
588 res_max
= sval_binop(left_max
, '+', right_max
);
589 res_max
= sval_cast(right_type
, res_max
);
591 case SPECIAL_SUB_ASSIGN
:
592 if (get_implied_max(left
, &left_max
) &&
593 !sval_is_max(left_max
) &&
594 get_implied_min(right
, &right_min
) &&
595 !sval_is_min(right_min
)) {
596 res_max
= sval_binop(left_max
, '-', right_min
);
597 res_max
= sval_cast(right_type
, res_max
);
601 if (get_implied_min(left
, &left_min
) &&
602 !sval_is_min(left_min
) &&
603 get_implied_max(right
, &right_max
) &&
604 !sval_is_max(right_max
)) {
605 res_min
= sval_binop(left_min
, '-', right_max
);
606 res_min
= sval_cast(right_type
, res_min
);
609 case SPECIAL_AND_ASSIGN
:
610 case SPECIAL_MOD_ASSIGN
:
611 binop_expr
= binop_expression(expr
->left
,
612 op_remove_assign(expr
->op
),
614 if (get_implied_rl(binop_expr
, &rl
)) {
615 rl
= cast_rl(left_type
, rl
);
616 set_extra_mod(name
, sym
, alloc_estate_rl(rl
));
620 rl
= cast_rl(left_type
, alloc_rl(res_min
, res_max
));
621 set_extra_mod(name
, sym
, alloc_estate_rl(rl
));
626 static struct smatch_state
*increment_state(struct smatch_state
*state
)
628 sval_t min
= estate_min(state
);
629 sval_t max
= estate_max(state
);
631 if (!estate_rl(state
))
635 max
= sval_type_max(max
.type
);
637 if (!sval_is_min(min
) && !sval_is_max(min
))
639 if (!sval_is_min(max
) && !sval_is_max(max
))
641 return alloc_estate_range(min
, max
);
644 static struct smatch_state
*decrement_state(struct smatch_state
*state
)
646 sval_t min
= estate_min(state
);
647 sval_t max
= estate_max(state
);
649 if (!estate_rl(state
))
653 min
= sval_type_min(min
.type
);
655 if (!sval_is_min(min
) && !sval_is_max(min
))
657 if (!sval_is_min(max
) && !sval_is_max(max
))
659 return alloc_estate_range(min
, max
);
662 static void unop_expr(struct expression
*expr
)
664 struct smatch_state
*state
;
666 if (expr
->smatch_flags
& Handled
)
670 case SPECIAL_INCREMENT
:
671 state
= get_state_expr(SMATCH_EXTRA
, expr
->unop
);
672 state
= increment_state(state
);
674 state
= alloc_estate_whole(get_type(expr
));
675 set_extra_expr_mod(expr
->unop
, state
);
677 case SPECIAL_DECREMENT
:
678 state
= get_state_expr(SMATCH_EXTRA
, expr
->unop
);
679 state
= decrement_state(state
);
681 state
= alloc_estate_whole(get_type(expr
));
682 set_extra_expr_mod(expr
->unop
, state
);
689 static void asm_expr(struct statement
*stmt
)
692 struct expression
*expr
;
696 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
698 case 0: /* identifier */
699 case 1: /* constraint */
702 case 2: /* expression */
704 type
= get_type(strip_expr(expr
));
705 set_extra_expr_mod(expr
, alloc_estate_whole(type
));
708 } END_FOR_EACH_PTR(expr
);
711 static void delete_state_tracker(struct tracker
*t
)
713 remove_from_equiv(t
->name
, t
->sym
);
714 delete_state(t
->owner
, t
->name
, t
->sym
);
717 static void scoped_state_extra(const char *name
, struct symbol
*sym
)
721 t
= alloc_tracker(SMATCH_EXTRA
, name
, sym
);
722 add_scope_hook((scope_hook
*)&delete_state_tracker
, t
);
725 static void match_declarations(struct symbol
*sym
)
730 name
= sym
->ident
->name
;
731 if (!sym
->initializer
) {
732 set_state(SMATCH_EXTRA
, name
, sym
, alloc_estate_whole(get_real_base_type(sym
)));
733 scoped_state_extra(name
, sym
);
738 static void check_dereference(struct expression
*expr
)
740 set_extra_expr_nomod(expr
, alloc_estate_range(valid_ptr_min_sval
, valid_ptr_max_sval
));
743 static void match_dereferences(struct expression
*expr
)
745 if (expr
->type
!= EXPR_PREOP
)
747 check_dereference(expr
->unop
);
750 static void match_pointer_as_array(struct expression
*expr
)
754 check_dereference(expr
->unop
->left
);
757 static void set_param_dereferenced(struct expression
*arg
, char *unused
)
759 check_dereference(arg
);
762 static void match_function_def(struct symbol
*sym
)
766 FOR_EACH_PTR(sym
->ctype
.base_type
->arguments
, arg
) {
769 set_state(my_id
, arg
->ident
->name
, arg
, alloc_estate_whole(get_real_base_type(arg
)));
770 } END_FOR_EACH_PTR(arg
);
773 static int match_func_comparison(struct expression
*expr
)
775 struct expression
*left
= strip_expr(expr
->left
);
776 struct expression
*right
= strip_expr(expr
->right
);
779 if (left
->type
== EXPR_CALL
) {
780 if (!get_implied_value(right
, &sval
))
782 function_comparison(expr
->op
, left
, sval
, 1);
786 if (right
->type
== EXPR_CALL
) {
787 if (!get_implied_value(left
, &sval
))
789 function_comparison(expr
->op
, right
, sval
, 0);
796 static sval_t
add_one(sval_t sval
)
802 static sval_t
sub_one(sval_t sval
)
808 static void handle_comparison(struct symbol
*type
, struct expression
*left
, int op
, struct expression
*right
)
810 struct range_list
*left_orig
;
811 struct range_list
*left_true
;
812 struct range_list
*left_false
;
813 struct range_list
*right_orig
;
814 struct range_list
*right_true
;
815 struct range_list
*right_false
;
816 struct smatch_state
*left_true_state
;
817 struct smatch_state
*left_false_state
;
818 struct smatch_state
*right_true_state
;
819 struct smatch_state
*right_false_state
;
820 sval_t min
, max
, dummy
;
822 int right_postop
= 0;
824 if (left
->op
== SPECIAL_INCREMENT
|| left
->op
== SPECIAL_DECREMENT
) {
825 if (left
->type
== EXPR_POSTOP
) {
826 left
->smatch_flags
|= Handled
;
827 left_postop
= left
->op
;
829 left
= strip_expr(left
->unop
);
831 while (left
->type
== EXPR_ASSIGNMENT
)
832 left
= strip_expr(left
->left
);
834 if (right
->op
== SPECIAL_INCREMENT
|| right
->op
== SPECIAL_DECREMENT
) {
835 if (right
->type
== EXPR_POSTOP
) {
836 right
->smatch_flags
|= Handled
;
837 right_postop
= right
->op
;
839 right
= strip_expr(right
->unop
);
842 if (get_implied_rl(left
, &left_orig
)) {
843 left_orig
= cast_rl(type
, left_orig
);
845 min
= sval_type_min(get_type(left
));
846 max
= sval_type_max(get_type(left
));
847 left_orig
= cast_rl(type
, alloc_rl(min
, max
));
850 if (get_implied_rl(right
, &right_orig
)) {
851 right_orig
= cast_rl(type
, right_orig
);
853 min
= sval_type_min(get_type(right
));
854 max
= sval_type_max(get_type(right
));
855 right_orig
= cast_rl(type
, alloc_rl(min
, max
));
857 min
= sval_type_min(type
);
858 max
= sval_type_max(type
);
860 left_true
= clone_rl(left_orig
);
861 left_false
= clone_rl(left_orig
);
862 right_true
= clone_rl(right_orig
);
863 right_false
= clone_rl(right_orig
);
867 case SPECIAL_UNSIGNED_LT
:
868 left_true
= remove_range(left_orig
, rl_max(right_orig
), max
);
869 if (!sval_is_min(rl_min(right_orig
))) {
870 left_false
= remove_range(left_orig
, min
, sub_one(rl_min(right_orig
)));
873 right_true
= remove_range(right_orig
, min
, rl_min(left_orig
));
874 if (!sval_is_max(rl_max(left_orig
)))
875 right_false
= remove_range(right_orig
, add_one(rl_max(left_orig
)), max
);
877 case SPECIAL_UNSIGNED_LTE
:
879 if (!sval_is_max(rl_max(right_orig
)))
880 left_true
= remove_range(left_orig
, add_one(rl_max(right_orig
)), max
);
881 left_false
= remove_range(left_orig
, min
, rl_min(right_orig
));
883 if (!sval_is_min(rl_min(left_orig
)))
884 right_true
= remove_range(right_orig
, min
, sub_one(rl_min(left_orig
)));
885 right_false
= remove_range(right_orig
, rl_max(left_orig
), max
);
887 if (sval_cmp(rl_min(left_orig
), rl_min(right_orig
)) == 0)
888 left_false
= remove_range(left_false
, rl_min(left_orig
), rl_min(left_orig
));
889 if (sval_cmp(rl_max(left_orig
), rl_max(right_orig
)) == 0)
890 right_false
= remove_range(right_false
, rl_max(left_orig
), rl_max(left_orig
));
893 if (!sval_is_max(rl_max(right_orig
))) {
894 left_true
= remove_range(left_true
, add_one(rl_max(right_orig
)), max
);
896 if (!sval_is_min(rl_min(right_orig
))) {
897 left_true
= remove_range(left_true
, min
, sub_one(rl_min(right_orig
)));
899 if (sval_cmp(rl_min(right_orig
), rl_max(right_orig
)) == 0)
900 left_false
= remove_range(left_orig
, rl_min(right_orig
), rl_min(right_orig
));
902 if (!sval_is_max(rl_max(left_orig
)))
903 right_true
= remove_range(right_true
, add_one(rl_max(left_orig
)), max
);
904 if (!sval_is_min(rl_min(left_orig
)))
905 right_true
= remove_range(right_true
, min
, sub_one(rl_min(left_orig
)));
906 if (sval_cmp(rl_min(left_orig
), rl_max(left_orig
)) == 0)
907 right_false
= remove_range(right_orig
, rl_min(left_orig
), rl_min(left_orig
));
909 case SPECIAL_UNSIGNED_GTE
:
911 if (!sval_is_min(rl_min(right_orig
)))
912 left_true
= remove_range(left_orig
, min
, sub_one(rl_min(right_orig
)));
913 left_false
= remove_range(left_orig
, rl_max(right_orig
), max
);
915 if (!sval_is_max(rl_max(left_orig
)))
916 right_true
= remove_range(right_orig
, add_one(rl_max(left_orig
)), max
);
917 right_false
= remove_range(right_orig
, min
, rl_min(left_orig
));
919 if (sval_cmp(rl_min(left_orig
), rl_min(right_orig
)) == 0)
920 right_false
= remove_range(right_false
, rl_min(left_orig
), rl_min(left_orig
));
921 if (sval_cmp(rl_max(left_orig
), rl_max(right_orig
)) == 0)
922 left_false
= remove_range(left_false
, rl_max(left_orig
), rl_max(left_orig
));
925 case SPECIAL_UNSIGNED_GT
:
926 left_true
= remove_range(left_orig
, min
, rl_min(right_orig
));
927 if (!sval_is_max(rl_max(right_orig
)))
928 left_false
= remove_range(left_orig
, add_one(rl_max(right_orig
)), max
);
930 right_true
= remove_range(right_orig
, rl_max(left_orig
), max
);
931 if (!sval_is_min(rl_min(left_orig
)))
932 right_false
= remove_range(right_orig
, min
, sub_one(rl_min(left_orig
)));
934 case SPECIAL_NOTEQUAL
:
935 if (!sval_is_max(rl_max(right_orig
)))
936 left_false
= remove_range(left_false
, add_one(rl_max(right_orig
)), max
);
937 if (!sval_is_min(rl_min(right_orig
)))
938 left_false
= remove_range(left_false
, min
, sub_one(rl_min(right_orig
)));
939 if (sval_cmp(rl_min(right_orig
), rl_max(right_orig
)) == 0)
940 left_true
= remove_range(left_orig
, rl_min(right_orig
), rl_min(right_orig
));
942 if (!sval_is_max(rl_max(left_orig
)))
943 right_false
= remove_range(right_false
, add_one(rl_max(left_orig
)), max
);
944 if (!sval_is_min(rl_min(left_orig
)))
945 right_false
= remove_range(right_false
, min
, sub_one(rl_min(left_orig
)));
946 if (sval_cmp(rl_min(left_orig
), rl_max(left_orig
)) == 0)
947 right_true
= remove_range(right_orig
, rl_min(left_orig
), rl_min(left_orig
));
953 left_true
= rl_truncate_cast(get_type(left
), left_true
);
954 left_false
= rl_truncate_cast(get_type(left
), left_false
);
955 right_true
= rl_truncate_cast(get_type(right
), right_true
);
956 right_false
= rl_truncate_cast(get_type(right
), right_false
);
958 left_true_state
= alloc_estate_rl(left_true
);
959 left_false_state
= alloc_estate_rl(left_false
);
960 right_true_state
= alloc_estate_rl(right_true
);
961 right_false_state
= alloc_estate_rl(right_false
);
965 case SPECIAL_UNSIGNED_LT
:
966 case SPECIAL_UNSIGNED_LTE
:
968 if (get_hard_max(right
, &dummy
))
969 estate_set_hard_max(left_true_state
);
970 if (get_hard_max(left
, &dummy
))
971 estate_set_hard_max(right_false_state
);
974 case SPECIAL_UNSIGNED_GT
:
975 case SPECIAL_UNSIGNED_GTE
:
977 if (get_hard_max(left
, &dummy
))
978 estate_set_hard_max(right_true_state
);
979 if (get_hard_max(right
, &dummy
))
980 estate_set_hard_max(left_false_state
);
984 if (get_hard_max(left
, &dummy
)) {
985 estate_set_hard_max(left_true_state
);
986 estate_set_hard_max(left_false_state
);
988 if (get_hard_max(right
, &dummy
)) {
989 estate_set_hard_max(right_true_state
);
990 estate_set_hard_max(right_false_state
);
993 if (left_postop
== SPECIAL_INCREMENT
) {
994 left_true_state
= increment_state(left_true_state
);
995 left_false_state
= increment_state(left_false_state
);
997 if (left_postop
== SPECIAL_DECREMENT
) {
998 left_true_state
= decrement_state(left_true_state
);
999 left_false_state
= decrement_state(left_false_state
);
1001 if (right_postop
== SPECIAL_INCREMENT
) {
1002 right_true_state
= increment_state(right_true_state
);
1003 right_false_state
= increment_state(right_false_state
);
1005 if (right_postop
== SPECIAL_DECREMENT
) {
1006 right_true_state
= decrement_state(right_true_state
);
1007 right_false_state
= decrement_state(right_false_state
);
1010 set_extra_expr_true_false(left
, left_true_state
, left_false_state
);
1011 set_extra_expr_true_false(right
, right_true_state
, right_false_state
);
1014 static int opposite_op(int op
)
1025 static int is_simple_math(struct expression
*expr
)
1029 if (expr
->type
!= EXPR_BINOP
)
1031 if (!opposite_op(expr
->op
))
1036 static void move_known_values(struct expression
**left_p
, struct expression
**right_p
)
1038 struct expression
*left
= *left_p
;
1039 struct expression
*right
= *right_p
;
1042 if (get_value(left
, &sval
)) {
1043 if (!is_simple_math(right
))
1045 if (right
->op
== '+' && get_value(right
->left
, &sval
)) {
1046 *left_p
= binop_expression(left
, opposite_op(right
->op
), right
->left
);
1047 *right_p
= right
->right
;
1050 if (get_value(right
->right
, &sval
)) {
1051 *left_p
= binop_expression(left
, opposite_op(right
->op
), right
->right
);
1052 *right_p
= right
->left
;
1057 if (get_value(right
, &sval
)) {
1058 if (!is_simple_math(left
))
1060 if (left
->op
== '+' && get_value(left
->left
, &sval
)) {
1061 *right_p
= binop_expression(right
, opposite_op(left
->op
), left
->left
);
1062 *left_p
= left
->right
;
1066 if (get_value(left
->right
, &sval
)) {
1067 *right_p
= binop_expression(right
, opposite_op(left
->op
), left
->right
);
1068 *left_p
= left
->left
;
1075 static void match_comparison(struct expression
*expr
)
1077 struct expression
*left_orig
= strip_expr(expr
->left
);
1078 struct expression
*right_orig
= strip_expr(expr
->right
);
1079 struct expression
*left
, *right
;
1080 struct expression
*prev
;
1081 struct symbol
*type
;
1083 if (match_func_comparison(expr
))
1086 type
= get_type(expr
);
1088 type
= &llong_ctype
;
1092 move_known_values(&left
, &right
);
1093 handle_comparison(type
, left
, expr
->op
, right
);
1095 prev
= get_assigned_expr(left_orig
);
1096 if (is_simple_math(prev
)) {
1099 move_known_values(&left
, &right
);
1100 handle_comparison(type
, left
, expr
->op
, right
);
1103 prev
= get_assigned_expr(right_orig
);
1104 if (is_simple_math(prev
)) {
1107 move_known_values(&left
, &right
);
1108 handle_comparison(type
, left
, expr
->op
, right
);
1114 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
1115 void __extra_match_condition(struct expression
*expr
)
1119 struct smatch_state
*pre_state
;
1120 struct smatch_state
*true_state
;
1121 struct smatch_state
*false_state
;
1123 expr
= strip_expr(expr
);
1124 switch (expr
->type
) {
1126 function_comparison(SPECIAL_NOTEQUAL
, expr
, ll_to_sval(0), 1);
1133 zero
= sval_blank(expr
);
1136 name
= expr_to_var_sym(expr
, &sym
);
1139 pre_state
= get_state(my_id
, name
, sym
);
1140 true_state
= estate_filter_sval(pre_state
, zero
);
1141 if (possibly_true(expr
, SPECIAL_EQUAL
, zero_expr()))
1142 false_state
= alloc_estate_sval(zero
);
1144 false_state
= alloc_estate_empty();
1145 set_extra_true_false(name
, sym
, true_state
, false_state
);
1150 match_comparison(expr
);
1152 case EXPR_ASSIGNMENT
:
1153 __extra_match_condition(expr
->left
);
1158 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
1159 int implied_not_equal(struct expression
*expr
, long long val
)
1161 return !possibly_false(expr
, SPECIAL_NOTEQUAL
, value_expr(val
));
1164 static void struct_member_callback(struct expression
*call
, int param
, char *printed_name
, struct smatch_state
*state
)
1166 if (estate_is_whole(state
))
1168 sql_insert_caller_info(call
, PARAM_VALUE
, param
, printed_name
, state
->name
);
1171 static void db_limited_before(void)
1173 unmatched_slist
= clone_slist(__get_cur_slist());
1176 static void db_limited_after(void)
1178 free_slist(&unmatched_slist
);
1181 static void db_param_limit_filter(struct expression
*expr
, int param
, char *key
, char *value
, int mod
)
1183 struct expression
*arg
;
1186 struct sm_state
*sm
;
1187 struct symbol
*type
;
1188 struct range_list
*rl
;
1189 struct range_list
*limit
;
1190 struct range_list
*new;
1192 while (expr
->type
== EXPR_ASSIGNMENT
)
1193 expr
= strip_expr(expr
->right
);
1194 if (expr
->type
!= EXPR_CALL
)
1197 arg
= get_argument_from_call_expr(expr
->args
, param
);
1201 name
= get_variable_from_key(arg
, key
, &sym
);
1205 sm
= get_sm_state(SMATCH_EXTRA
, name
, sym
);
1206 type
= get_member_type_from_key(arg
, key
);
1208 rl
= estate_rl(sm
->state
);
1210 rl
= alloc_whole_rl(type
);
1212 call_results_to_rl(expr
, type
, value
, &limit
);
1213 new = rl_intersection(rl
, limit
);
1215 /* We want to preserve the implications here */
1216 if (sm
&& rl_equiv(estate_rl(sm
->state
), new)) {
1220 set_extra_mod(name
, sym
, alloc_estate_rl(new));
1222 set_extra_nomod(name
, sym
, alloc_estate_rl(new));
1229 static void db_param_limit(struct expression
*expr
, int param
, char *key
, char *value
)
1231 db_param_limit_filter(expr
, param
, key
, value
, 0);
1234 static void db_param_filter(struct expression
*expr
, int param
, char *key
, char *value
)
1236 db_param_limit_filter(expr
, param
, key
, value
, 1);
1239 static void db_param_add(struct expression
*expr
, int param
, char *key
, char *value
)
1241 struct expression
*arg
;
1244 struct symbol
*type
;
1245 struct smatch_state
*state
;
1246 struct range_list
*added
= NULL
;
1247 struct range_list
*new;
1249 while (expr
->type
== EXPR_ASSIGNMENT
)
1250 expr
= strip_expr(expr
->right
);
1251 if (expr
->type
!= EXPR_CALL
)
1254 arg
= get_argument_from_call_expr(expr
->args
, param
);
1257 name
= get_variable_from_key(arg
, key
, &sym
);
1261 type
= get_member_type_from_key(arg
, key
);
1262 state
= get_state(SMATCH_EXTRA
, name
, sym
);
1264 call_results_to_rl(expr
, type
, value
, &added
);
1265 new = rl_union(estate_rl(state
), added
);
1267 new = alloc_whole_rl(type
);
1270 set_extra_mod(name
, sym
, alloc_estate_rl(new));
1275 static void db_returned_member_info(struct expression
*expr
, int param
, char *key
, char *value
)
1277 struct symbol
*type
;
1280 char member_name
[256];
1281 struct range_list
*rl
;
1283 if (expr
->type
!= EXPR_ASSIGNMENT
)
1286 name
= expr_to_var_sym(expr
->left
, &sym
);
1289 snprintf(member_name
, sizeof(member_name
), "%s%s", name
, key
+ 2);
1290 type
= get_member_type_from_key(expr
->left
, key
);
1293 call_results_to_rl(expr
->right
, type
, value
, &rl
);
1294 set_extra_mod(member_name
, sym
, alloc_estate_rl(rl
));
1300 static void returned_member_callback(int return_id
, char *return_ranges
, char *printed_name
, struct smatch_state
*state
)
1302 if (estate_is_whole(state
))
1305 sql_insert_return_states(return_id
, return_ranges
, RETURN_VALUE
, -1,
1306 printed_name
, state
->name
);
1309 static void match_call_info(struct expression
*expr
)
1311 struct range_list
*rl
= NULL
;
1312 struct expression
*arg
;
1313 struct symbol
*type
;
1316 FOR_EACH_PTR(expr
->args
, arg
) {
1317 type
= get_arg_type(expr
->fn
, i
);
1319 if (get_implied_rl(arg
, &rl
))
1320 rl
= cast_rl(type
, rl
);
1322 rl
= cast_rl(type
, alloc_whole_rl(get_type(arg
)));
1324 sql_insert_caller_info(expr
, PARAM_VALUE
, i
, "$$", show_rl(rl
));
1326 } END_FOR_EACH_PTR(arg
);
1329 static void set_param_value(const char *name
, struct symbol
*sym
, char *key
, char *value
)
1331 struct range_list
*rl
= NULL
;
1332 struct smatch_state
*state
;
1333 struct symbol
*type
;
1336 if (strcmp(key
, "*$$") == 0)
1337 snprintf(fullname
, sizeof(fullname
), "*%s", name
);
1338 else if (strncmp(key
, "$$", 2) == 0)
1339 snprintf(fullname
, 256, "%s%s", name
, key
+ 2);
1343 type
= get_member_type_from_key(symbol_expression(sym
), key
);
1344 str_to_rl(type
, value
, &rl
);
1345 state
= alloc_estate_rl(rl
);
1346 set_state(SMATCH_EXTRA
, fullname
, sym
, state
);
1349 void register_smatch_extra(int id
)
1353 add_merge_hook(my_id
, &merge_estates
);
1354 add_unmatched_state_hook(my_id
, &unmatched_state
);
1355 add_hook(&match_function_def
, FUNC_DEF_HOOK
);
1356 add_hook(&match_declarations
, DECLARATION_HOOK
);
1357 add_definition_db_callback(set_param_value
, PARAM_VALUE
);
1358 add_db_return_states_callback(RETURN_VALUE
, &db_returned_member_info
);
1359 add_db_return_states_before(&db_limited_before
);
1360 add_db_return_states_callback(LIMITED_VALUE
, &db_param_limit
);
1361 add_db_return_states_callback(FILTER_VALUE
, &db_param_filter
);
1362 add_db_return_states_callback(ADDED_VALUE
, &db_param_add
);
1363 add_db_return_states_after(&db_limited_after
);
1366 void register_smatch_extra_late(int id
)
1368 add_hook(&match_dereferences
, DEREF_HOOK
);
1369 add_hook(&match_pointer_as_array
, OP_HOOK
);
1370 add_db_fn_call_callback(DEREFERENCE
, &set_param_dereferenced
);
1371 add_hook(&match_function_call
, FUNCTION_CALL_HOOK
);
1372 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
1373 add_hook(&unop_expr
, OP_HOOK
);
1374 add_hook(&asm_expr
, ASM_HOOK
);
1376 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
1377 add_member_info_callback(my_id
, struct_member_callback
);
1378 add_returned_member_callback(my_id
, returned_member_callback
);