2 * sparse/check_hold_dev.c
4 * Copyright (C) 2009 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
11 * This check is supposed to find bugs in reference counting using dev_hold()
14 * When a device is first held, if an error happens later in the function
15 * it needs to be released on all the error paths.
20 #include "smatch_extra.h"
21 #include "smatch_slist.h"
28 static void match_dev_hold(const char *fn
, struct expression
*expr
, void *data
)
30 struct expression
*arg_expr
;
32 arg_expr
= get_argument_from_call_expr(expr
->args
, 0);
33 set_state_expr(my_id
, arg_expr
, &held
);
36 static void match_dev_put(const char *fn
, struct expression
*expr
, void *data
)
38 struct expression
*arg_expr
;
40 arg_expr
= get_argument_from_call_expr(expr
->args
, 0);
41 set_state_expr(my_id
, arg_expr
, &released
);
44 static void match_returns_held(const char *fn
, struct expression
*call_expr
,
45 struct expression
*assign_expr
, void *unused
)
48 set_state_expr(my_id
, assign_expr
->left
, &held
);
51 static void match_returns_null(const char *fn
, struct expression
*call_expr
,
52 struct expression
*assign_expr
, void *unused
)
55 set_state_expr(my_id
, assign_expr
->left
, &released
);
58 static void check_for_held(void)
60 struct state_list
*slist
;
63 slist
= get_all_states(my_id
);
64 FOR_EACH_PTR(slist
, tmp
) {
65 if (slist_has_state(tmp
->possible
, &held
)) {
66 sm_msg("warn: '%s' held on error path.",
69 } END_FOR_EACH_PTR(tmp
);
73 static void print_returns_held(struct expression
*expr
)
79 sm
= get_sm_state_expr(my_id
, expr
);
82 if (slist_has_state(sm
->possible
, &held
))
83 sm_info("returned dev is held.");
86 static void match_return(struct expression
*ret_value
)
88 print_returns_held(ret_value
);
89 if (!is_error_return(ret_value
))
94 static void register_returns_held_funcs(void)
99 token
= get_tokens_file("kernel.returns_held_funcs");
102 if (token_type(token
) != TOKEN_STREAMBEGIN
)
105 while (token_type(token
) != TOKEN_STREAMEND
) {
106 if (token_type(token
) != TOKEN_IDENT
)
108 func
= show_ident(token
->ident
);
109 return_implies_state(func
, valid_ptr_min
, valid_ptr_max
,
110 &match_returns_held
, NULL
);
111 return_implies_state(func
, 0, 0, &match_returns_null
,
118 void check_hold_dev(int id
)
120 if (option_project
!= PROJ_KERNEL
)
124 add_function_hook("dev_hold", &match_dev_hold
, NULL
);
125 add_function_hook("dev_put", &match_dev_put
, NULL
);
126 register_returns_held_funcs();
127 add_hook(&match_return
, RETURN_HOOK
);