2 * sparse/smatch_param_limit.c
4 * Copyright (C) 2012 Oracle.
6 * Licensed under the Open Software License version 1.1
11 * This is for functions like this:
15 * if (a >= 0 && a < 10) {
22 * If we pass in 5, it returns 1.
24 * It's a bit complicated because we can't just consider the final value, we
25 * have to always consider the passed in value.
31 #include "smatch_extra.h"
32 #include "smatch_slist.h"
38 static struct state_list
*start_states
;
39 static struct state_list_stack
*saved_stack
;
41 static void save_start_states(struct statement
*stmt
)
43 start_states
= get_all_states(SMATCH_EXTRA
);
46 static void match_end_func(void)
48 free_slist(&start_states
);
51 static struct smatch_state
*unmatched_state(struct sm_state
*sm
)
56 static struct smatch_state
*filter_my_sm(struct sm_state
*sm
)
58 struct range_list
*ret
= NULL
;
60 struct smatch_state
*estate
;
62 FOR_EACH_PTR(sm
->possible
, tmp
) {
63 if (tmp
->state
== &merged
)
65 if (tmp
->state
== &original
) {
66 estate
= get_state_slist(tmp
->pool
, SMATCH_EXTRA
, tmp
->name
, tmp
->sym
);
68 // sm_msg("debug: no value found in pool %p", tmp->pool);
74 ret
= rl_union(ret
, estate_rl(estate
));
75 } END_FOR_EACH_PTR(tmp
);
77 return alloc_estate_rl(ret
);
80 struct smatch_state
*get_orig_estate(const char *name
, struct symbol
*sym
)
83 struct smatch_state
*state
;
85 sm
= get_sm_state(my_id
, name
, sym
);
87 return filter_my_sm(sm
);
89 state
= get_state(SMATCH_EXTRA
, name
, sym
);
92 return alloc_estate_rl(alloc_whole_rl(get_real_base_type(sym
)));
95 static void print_return_value_param(int return_id
, char *return_ranges
, struct expression
*expr
)
97 struct state_list
*extra_slist
;
99 struct sm_state
*my_sm
;
100 struct smatch_state
*state
;
103 extra_slist
= get_all_states(SMATCH_EXTRA
);
105 FOR_EACH_PTR(extra_slist
, tmp
) {
106 if (!tmp
->sym
|| !tmp
->sym
->ident
|| strcmp(tmp
->name
, tmp
->sym
->ident
->name
) != 0)
109 param
= get_param_num_from_sym(tmp
->sym
);
113 my_sm
= get_sm_state(my_id
, tmp
->name
, tmp
->sym
);
115 struct smatch_state
*old
;
117 if (estate_is_whole(tmp
->state
))
119 old
= get_state_slist(start_states
, SMATCH_EXTRA
, tmp
->name
, tmp
->sym
);
120 if (old
&& estates_equiv(old
, tmp
->state
))
122 sql_insert_return_states(return_id
, return_ranges
,
123 LIMITED_VALUE
, param
, "$$",
128 state
= filter_my_sm(my_sm
);
131 /* This represents an impossible state. I screwd up. Bail. */
132 if (!estate_rl(state
))
134 if (estate_is_whole(state
))
136 sql_insert_return_states(return_id
, return_ranges
,
137 LIMITED_VALUE
, param
, "$$",
139 } END_FOR_EACH_PTR(tmp
);
141 free_slist(&extra_slist
);
144 static void extra_mod_hook(const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
146 struct smatch_state
*orig_vals
;
149 param
= get_param_num_from_sym(sym
);
153 /* we are only saving params for now */
154 if (!sym
->ident
|| strcmp(name
, sym
->ident
->name
) != 0)
157 orig_vals
= get_orig_estate(name
, sym
);
158 set_state(my_id
, name
, sym
, orig_vals
);
161 static void match_save_states(struct expression
*expr
)
163 push_slist(&saved_stack
, start_states
);
167 static void match_restore_states(struct expression
*expr
)
169 start_states
= pop_slist(&saved_stack
);
172 void register_param_limit(int id
)
176 add_hook(&save_start_states
, AFTER_DEF_HOOK
);
177 add_extra_mod_hook(&extra_mod_hook
);
178 add_unmatched_state_hook(my_id
, &unmatched_state
);
179 add_split_return_callback(&print_return_value_param
);
180 add_hook(&match_end_func
, END_FUNC_HOOK
);
181 add_hook(&match_save_states
, INLINE_FN_START
);
182 add_hook(&match_restore_states
, INLINE_FN_END
);