2 * sparse/smatch_function_hooks.c
4 * Copyright (C) 2009 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
11 * There are three types of function hooks:
12 * add_function_hook() - For any time a function is called.
13 * add_function_assign_hook() - foo = the_function().
14 * add_conditional_hook() - For when the return value implies something.
15 * For example a return value of 1 might mean
16 * a lock is held and 0 means it is not held.
24 #include "smatch_slist.h"
25 #include "smatch_extra.h"
29 struct data_range
*range
;
34 ALLOCATOR(fcall_back
, "call backs");
35 DECLARE_PTR_LIST(call_back_list
, struct fcall_back
);
37 static struct hsearch_data func_hash
;
39 #define REGULAR_CALL 0
40 #define CONDITIONAL_CALL 1
44 static struct fcall_back
*alloc_fcall_back(int type
, func_hook
*call_back
,
47 struct fcall_back
*cb
;
49 cb
= __alloc_fcall_back(0);
51 cb
->call_back
= call_back
;
56 static struct call_back_list
*get_call_backs(const char *look_for
)
60 e
.key
= (char *)look_for
;
61 hsearch_r(e
, FIND
, &ep
, &func_hash
);
64 return (struct call_back_list
*)ep
->data
;
67 static void add_cb_hook(const char *look_for
, struct fcall_back
*cb
)
72 e
.key
= alloc_string(look_for
);
73 hsearch_r(e
, FIND
, &ep
, &func_hash
);
75 struct call_back_list
*list
= NULL
;
77 add_ptr_list(&list
, cb
);
82 add_ptr_list((struct call_back_list
**)&ep
->data
, cb
);
85 if (!hsearch_r(e
, ENTER
, &ep
, &func_hash
)) {
86 printf("Error hash table too small in smatch_function_hooks.c\n");
92 void add_function_hook(const char *look_for
, func_hook
*call_back
, void *info
)
94 struct fcall_back
*cb
;
96 cb
= alloc_fcall_back(REGULAR_CALL
, call_back
, info
);
97 add_cb_hook(look_for
, cb
);
100 void add_conditional_hook(const char *look_for
, func_hook
*call_back
,
103 struct fcall_back
*cb
;
105 cb
= alloc_fcall_back(CONDITIONAL_CALL
, call_back
, info
);
106 add_cb_hook(look_for
, cb
);
109 void add_function_assign_hook(const char *look_for
, func_hook
*call_back
,
112 struct fcall_back
*cb
;
114 cb
= alloc_fcall_back(ASSIGN_CALL
, call_back
, info
);
115 add_cb_hook(look_for
, cb
);
118 void return_implies_state(const char *look_for
, long long start
, long long end
,
119 implication_hook
*call_back
, void *info
)
121 struct fcall_back
*cb
;
123 cb
= alloc_fcall_back(RANGED_CALL
, (func_hook
*)call_back
, info
);
124 cb
->range
= alloc_range_perm(start
, end
);
125 add_cb_hook(look_for
, cb
);
128 static void call_call_backs(struct call_back_list
*list
, int type
,
129 const char *fn
, struct expression
*expr
)
131 struct fcall_back
*tmp
;
133 FOR_EACH_PTR(list
, tmp
) {
134 if (tmp
->type
== type
)
135 (tmp
->call_back
)(fn
, expr
, tmp
->info
);
136 } END_FOR_EACH_PTR(tmp
);
139 static void call_ranged_call_backs(struct call_back_list
*list
,
140 const char *fn
, struct expression
*call_expr
,
141 struct expression
*assign_expr
)
143 struct fcall_back
*tmp
;
145 FOR_EACH_PTR(list
, tmp
) {
146 ((implication_hook
*)(tmp
->call_back
))(fn
, call_expr
, assign_expr
, tmp
->info
);
147 } END_FOR_EACH_PTR(tmp
);
150 static void match_function_call(struct expression
*expr
)
152 struct call_back_list
*call_backs
;
154 if (expr
->fn
->type
!= EXPR_SYMBOL
|| !expr
->fn
->symbol
)
156 call_backs
= get_call_backs(expr
->fn
->symbol
->ident
->name
);
159 call_call_backs(call_backs
, REGULAR_CALL
, expr
->fn
->symbol
->ident
->name
,
163 static void assign_condition_funcs(const char *fn
, struct expression
*expr
,
164 struct call_back_list
*call_backs
)
166 struct fcall_back
*tmp
;
171 struct smatch_state
*zero_state
, *non_zero_state
;
173 var_name
= get_variable_from_expr(expr
->left
, &sym
);
174 if (!var_name
|| !sym
)
177 __fake_conditions
= 1;
178 FOR_EACH_PTR(call_backs
, tmp
) {
179 if (tmp
->type
!= CONDITIONAL_CALL
)
183 (tmp
->call_back
)(fn
, expr
->right
, tmp
->info
);
184 } END_FOR_EACH_PTR(tmp
);
186 zero_state
= alloc_extra_state(0);
187 non_zero_state
= add_filter(extra_undefined(), 0);
188 set_true_false_states(SMATCH_EXTRA
, var_name
, sym
, non_zero_state
, zero_state
);
190 __fake_conditions
= 0;
195 merge_slist(&__fake_cond_true
, __fake_cond_false
);
197 FOR_EACH_PTR(__fake_cond_true
, sm
) {
199 } END_FOR_EACH_PTR(sm
);
200 free_slist(&__fake_cond_true
);
201 free_slist(&__fake_cond_false
);
203 free_string(var_name
);
206 static struct call_back_list
*get_same_ranged_call_backs(struct call_back_list
*list
,
207 struct data_range
*drange
)
209 struct call_back_list
*ret
= NULL
;
210 struct fcall_back
*tmp
;
212 FOR_EACH_PTR(list
, tmp
) {
213 if (tmp
->range
->min
== drange
->min
&& tmp
->range
->max
== drange
->max
)
214 add_ptr_list(&ret
, tmp
);
215 } END_FOR_EACH_PTR(tmp
);
219 static void assign_ranged_funcs(const char *fn
, struct expression
*expr
,
220 struct call_back_list
*call_backs
)
222 struct fcall_back
*tmp
;
226 struct smatch_state
*extra_state
;
227 struct state_list
*final_states
= NULL
;
228 struct range_list
*handled_ranges
= NULL
;
229 struct call_back_list
*same_range_call_backs
= NULL
;
231 var_name
= get_variable_from_expr(expr
->left
, &sym
);
232 if (!var_name
|| !sym
)
236 FOR_EACH_PTR(call_backs
, tmp
) {
237 if (tmp
->type
!= RANGED_CALL
)
239 if (in_list_exact(handled_ranges
, tmp
->range
))
241 tack_on(&handled_ranges
, tmp
->range
);
243 same_range_call_backs
= get_same_ranged_call_backs(call_backs
, tmp
->range
);
244 call_ranged_call_backs(same_range_call_backs
, fn
, expr
->right
, expr
);
245 __free_ptr_list((struct ptr_list
**)&same_range_call_backs
);
247 extra_state
= alloc_extra_state_range(tmp
->range
->min
, tmp
->range
->max
);
248 set_state(SMATCH_EXTRA
, var_name
, sym
, extra_state
);
250 merge_slist(&final_states
, __fake_cur_slist
);
251 free_slist(&__fake_cur_slist
);
252 } END_FOR_EACH_PTR(tmp
);
255 FOR_EACH_PTR(final_states
, sm
) {
257 } END_FOR_EACH_PTR(sm
);
259 free_slist(&final_states
);
261 free_string(var_name
);
264 void function_comparison(int comparison
, struct expression
*expr
, long long value
, int left
)
266 struct call_back_list
*call_backs
;
267 struct fcall_back
*tmp
;
269 struct data_range
*value_range
;
270 struct state_list
*true_states
= NULL
;
271 struct state_list
*false_states
= NULL
;
274 if (expr
->fn
->type
!= EXPR_SYMBOL
|| !expr
->fn
->symbol
)
276 fn
= expr
->fn
->symbol
->ident
->name
;
277 call_backs
= get_call_backs(expr
->fn
->symbol
->ident
->name
);
280 value_range
= alloc_range(value
, value
);
283 /* set true states */
284 FOR_EACH_PTR(call_backs
, tmp
) {
285 if (tmp
->type
!= RANGED_CALL
)
287 if (!true_comparison_range_lr(comparison
, tmp
->range
, value_range
, left
))
289 ((implication_hook
*)(tmp
->call_back
))(fn
, expr
, NULL
, tmp
->info
);
290 merge_slist(&true_states
, __fake_cur_slist
);
291 free_slist(&__fake_cur_slist
);
292 } END_FOR_EACH_PTR(tmp
);
294 /* set false states */
295 FOR_EACH_PTR(call_backs
, tmp
) {
296 if (tmp
->type
!= RANGED_CALL
)
298 if (!false_comparison_range_lr(comparison
, tmp
->range
, value_range
, left
))
300 ((implication_hook
*)(tmp
->call_back
))(fn
, expr
, NULL
, tmp
->info
);
301 merge_slist(&false_states
, __fake_cur_slist
);
302 free_slist(&__fake_cur_slist
);
303 } END_FOR_EACH_PTR(tmp
);
306 FOR_EACH_PTR(true_states
, sm
) {
307 __set_true_false_sm(sm
, NULL
);
308 } END_FOR_EACH_PTR(sm
);
309 FOR_EACH_PTR(false_states
, sm
) {
310 __set_true_false_sm(NULL
, sm
);
311 } END_FOR_EACH_PTR(sm
);
313 if (true_states
&& !false_states
)
314 sm_msg("warning: unhandled false condition.");
315 if (!true_states
&& false_states
)
316 sm_msg("warning: unhandled true condition.");
317 free_slist(&true_states
);
318 free_slist(&false_states
);
321 static void match_assign_call(struct expression
*expr
)
323 struct call_back_list
*call_backs
;
325 struct expression
*right
;
327 right
= strip_expr(expr
->right
);
328 if (right
->fn
->type
!= EXPR_SYMBOL
|| !right
->fn
->symbol
)
330 fn
= right
->fn
->symbol
->ident
->name
;
331 call_backs
= get_call_backs(fn
);
334 call_call_backs(call_backs
, ASSIGN_CALL
, fn
, expr
);
335 assign_condition_funcs(fn
, expr
, call_backs
);
336 assign_ranged_funcs(fn
, expr
, call_backs
);
339 static void match_conditional_call(struct expression
*expr
)
341 struct call_back_list
*call_backs
;
342 struct fcall_back
*tmp
;
346 expr
= strip_expr(expr
);
347 if (expr
->type
!= EXPR_CALL
)
350 if (expr
->fn
->type
!= EXPR_SYMBOL
|| !expr
->fn
->symbol
)
353 fn
= expr
->fn
->symbol
->ident
->name
;
354 call_backs
= get_call_backs(fn
);
357 __fake_conditions
= 1;
358 FOR_EACH_PTR(call_backs
, tmp
) {
359 if (tmp
->type
!= CONDITIONAL_CALL
)
362 (tmp
->call_back
)(fn
, expr
, tmp
->info
);
364 FOR_EACH_PTR(__fake_cond_true
, sm
) {
365 __set_true_false_sm(sm
, NULL
);
366 } END_FOR_EACH_PTR(sm
);
367 free_slist(&__fake_cond_true
);
369 FOR_EACH_PTR(__fake_cond_false
, sm
) {
370 __set_true_false_sm(NULL
, sm
);
371 } END_FOR_EACH_PTR(sm
);
372 free_slist(&__fake_cond_false
);
374 } END_FOR_EACH_PTR(tmp
);
375 __fake_conditions
= 0;
378 void create_function_hash(void)
380 hcreate_r(10000, &func_hash
); // Apparently 1000 is too few...
383 void register_function_hooks(int id
)
385 add_hook(&match_function_call
, FUNCTION_CALL_HOOK
);
386 add_hook(&match_assign_call
, CALL_ASSIGNMENT_HOOK
);
387 add_hook(&match_conditional_call
, CONDITION_HOOK
);