11f5207b7SJohn Levon /*
21f5207b7SJohn Levon * Copyright (C) 2009 Dan Carpenter.
31f5207b7SJohn Levon *
41f5207b7SJohn Levon * This program is free software; you can redistribute it and/or
51f5207b7SJohn Levon * modify it under the terms of the GNU General Public License
61f5207b7SJohn Levon * as published by the Free Software Foundation; either version 2
71f5207b7SJohn Levon * of the License, or (at your option) any later version.
81f5207b7SJohn Levon *
91f5207b7SJohn Levon * This program is distributed in the hope that it will be useful,
101f5207b7SJohn Levon * but WITHOUT ANY WARRANTY; without even the implied warranty of
111f5207b7SJohn Levon * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
121f5207b7SJohn Levon * GNU General Public License for more details.
131f5207b7SJohn Levon *
141f5207b7SJohn Levon * You should have received a copy of the GNU General Public License
151f5207b7SJohn Levon * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
161f5207b7SJohn Levon */
171f5207b7SJohn Levon
181f5207b7SJohn Levon /*
191f5207b7SJohn Levon * This check is supposed to find bugs in reference counting using dev_hold()
201f5207b7SJohn Levon * and dev_put().
211f5207b7SJohn Levon *
221f5207b7SJohn Levon * When a device is first held, if an error happens later in the function
231f5207b7SJohn Levon * it needs to be released on all the error paths.
241f5207b7SJohn Levon *
251f5207b7SJohn Levon */
261f5207b7SJohn Levon
271f5207b7SJohn Levon #include "smatch.h"
281f5207b7SJohn Levon #include "smatch_extra.h"
291f5207b7SJohn Levon #include "smatch_slist.h"
301f5207b7SJohn Levon
311f5207b7SJohn Levon static int my_id;
321f5207b7SJohn Levon
331f5207b7SJohn Levon STATE(held);
341f5207b7SJohn Levon STATE(released);
351f5207b7SJohn Levon
match_dev_hold(const char * fn,struct expression * expr,void * data)361f5207b7SJohn Levon static void match_dev_hold(const char *fn, struct expression *expr, void *data)
371f5207b7SJohn Levon {
381f5207b7SJohn Levon struct expression *arg_expr;
391f5207b7SJohn Levon
401f5207b7SJohn Levon arg_expr = get_argument_from_call_expr(expr->args, 0);
411f5207b7SJohn Levon set_state_expr(my_id, arg_expr, &held);
421f5207b7SJohn Levon }
431f5207b7SJohn Levon
match_dev_put(const char * fn,struct expression * expr,void * data)441f5207b7SJohn Levon static void match_dev_put(const char *fn, struct expression *expr, void *data)
451f5207b7SJohn Levon {
461f5207b7SJohn Levon struct expression *arg_expr;
471f5207b7SJohn Levon
481f5207b7SJohn Levon arg_expr = get_argument_from_call_expr(expr->args, 0);
491f5207b7SJohn Levon set_state_expr(my_id, arg_expr, &released);
501f5207b7SJohn Levon }
511f5207b7SJohn Levon
match_returns_held(const char * fn,struct expression * call_expr,struct expression * assign_expr,void * unused)521f5207b7SJohn Levon static void match_returns_held(const char *fn, struct expression *call_expr,
531f5207b7SJohn Levon struct expression *assign_expr, void *unused)
541f5207b7SJohn Levon {
551f5207b7SJohn Levon if (assign_expr)
561f5207b7SJohn Levon set_state_expr(my_id, assign_expr->left, &held);
571f5207b7SJohn Levon }
581f5207b7SJohn Levon
match_returns_null(const char * fn,struct expression * call_expr,struct expression * assign_expr,void * unused)591f5207b7SJohn Levon static void match_returns_null(const char *fn, struct expression *call_expr,
601f5207b7SJohn Levon struct expression *assign_expr, void *unused)
611f5207b7SJohn Levon {
621f5207b7SJohn Levon if (assign_expr)
631f5207b7SJohn Levon set_state_expr(my_id, assign_expr->left, &released);
641f5207b7SJohn Levon }
651f5207b7SJohn Levon
check_for_held(void)661f5207b7SJohn Levon static void check_for_held(void)
671f5207b7SJohn Levon {
681f5207b7SJohn Levon struct stree *stree;
691f5207b7SJohn Levon struct sm_state *tmp;
701f5207b7SJohn Levon
711f5207b7SJohn Levon stree = __get_cur_stree();
721f5207b7SJohn Levon FOR_EACH_MY_SM(my_id, stree, tmp) {
731f5207b7SJohn Levon if (slist_has_state(tmp->possible, &held)) {
741f5207b7SJohn Levon sm_warning("'%s' held on error path.",
751f5207b7SJohn Levon tmp->name);
761f5207b7SJohn Levon }
771f5207b7SJohn Levon } END_FOR_EACH_SM(tmp);
781f5207b7SJohn Levon }
791f5207b7SJohn Levon
print_returns_held(struct expression * expr)801f5207b7SJohn Levon static void print_returns_held(struct expression *expr)
811f5207b7SJohn Levon {
821f5207b7SJohn Levon struct sm_state *sm;
831f5207b7SJohn Levon
841f5207b7SJohn Levon if (!option_info)
851f5207b7SJohn Levon return;
861f5207b7SJohn Levon sm = get_sm_state_expr(my_id, expr);
871f5207b7SJohn Levon if (!sm)
881f5207b7SJohn Levon return;
891f5207b7SJohn Levon if (slist_has_state(sm->possible, &held))
901f5207b7SJohn Levon sm_info("returned dev is held.");
911f5207b7SJohn Levon }
921f5207b7SJohn Levon
match_return(struct expression * ret_value)931f5207b7SJohn Levon static void match_return(struct expression *ret_value)
941f5207b7SJohn Levon {
951f5207b7SJohn Levon print_returns_held(ret_value);
961f5207b7SJohn Levon if (!is_error_return(ret_value))
971f5207b7SJohn Levon return;
981f5207b7SJohn Levon check_for_held();
991f5207b7SJohn Levon }
1001f5207b7SJohn Levon
register_returns_held_funcs(void)1011f5207b7SJohn Levon static void register_returns_held_funcs(void)
1021f5207b7SJohn Levon {
1031f5207b7SJohn Levon struct token *token;
1041f5207b7SJohn Levon const char *func;
1051f5207b7SJohn Levon
1061f5207b7SJohn Levon token = get_tokens_file("kernel.returns_held_funcs");
1071f5207b7SJohn Levon if (!token)
1081f5207b7SJohn Levon return;
1091f5207b7SJohn Levon if (token_type(token) != TOKEN_STREAMBEGIN)
1101f5207b7SJohn Levon return;
1111f5207b7SJohn Levon token = token->next;
1121f5207b7SJohn Levon while (token_type(token) != TOKEN_STREAMEND) {
1131f5207b7SJohn Levon if (token_type(token) != TOKEN_IDENT)
1141f5207b7SJohn Levon return;
1151f5207b7SJohn Levon func = show_ident(token->ident);
116*efe51d0cSJohn Levon return_implies_state_sval(func, valid_ptr_min_sval, valid_ptr_max_sval,
1171f5207b7SJohn Levon &match_returns_held, NULL);
1181f5207b7SJohn Levon return_implies_state(func, 0, 0, &match_returns_null,
1191f5207b7SJohn Levon NULL);
1201f5207b7SJohn Levon token = token->next;
1211f5207b7SJohn Levon }
1221f5207b7SJohn Levon clear_token_alloc();
1231f5207b7SJohn Levon }
1241f5207b7SJohn Levon
check_held_dev(int id)1251f5207b7SJohn Levon void check_held_dev(int id)
1261f5207b7SJohn Levon {
1271f5207b7SJohn Levon if (option_project != PROJ_KERNEL)
1281f5207b7SJohn Levon return;
1291f5207b7SJohn Levon
1301f5207b7SJohn Levon my_id = id;
1311f5207b7SJohn Levon add_function_hook("dev_hold", &match_dev_hold, NULL);
1321f5207b7SJohn Levon add_function_hook("dev_put", &match_dev_put, NULL);
1331f5207b7SJohn Levon register_returns_held_funcs();
1341f5207b7SJohn Levon add_hook(&match_return, RETURN_HOOK);
1351f5207b7SJohn Levon }
136