Print this page
11506 smatch resync
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/tools/smatch/src/check_held_dev.c
+++ new/usr/src/tools/smatch/src/check_held_dev.c
1 1 /*
2 2 * Copyright (C) 2009 Dan Carpenter.
3 3 *
4 4 * This program is free software; you can redistribute it and/or
5 5 * modify it under the terms of the GNU General Public License
6 6 * as published by the Free Software Foundation; either version 2
7 7 * of the License, or (at your option) any later version.
8 8 *
9 9 * This program is distributed in the hope that it will be useful,
10 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 * GNU General Public License for more details.
13 13 *
14 14 * You should have received a copy of the GNU General Public License
15 15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
16 16 */
17 17
18 18 /*
19 19 * This check is supposed to find bugs in reference counting using dev_hold()
20 20 * and dev_put().
21 21 *
22 22 * When a device is first held, if an error happens later in the function
23 23 * it needs to be released on all the error paths.
24 24 *
25 25 */
26 26
27 27 #include "smatch.h"
28 28 #include "smatch_extra.h"
29 29 #include "smatch_slist.h"
30 30
31 31 static int my_id;
32 32
33 33 STATE(held);
34 34 STATE(released);
35 35
36 36 static void match_dev_hold(const char *fn, struct expression *expr, void *data)
37 37 {
38 38 struct expression *arg_expr;
39 39
40 40 arg_expr = get_argument_from_call_expr(expr->args, 0);
41 41 set_state_expr(my_id, arg_expr, &held);
42 42 }
43 43
44 44 static void match_dev_put(const char *fn, struct expression *expr, void *data)
45 45 {
46 46 struct expression *arg_expr;
47 47
48 48 arg_expr = get_argument_from_call_expr(expr->args, 0);
49 49 set_state_expr(my_id, arg_expr, &released);
50 50 }
51 51
52 52 static void match_returns_held(const char *fn, struct expression *call_expr,
53 53 struct expression *assign_expr, void *unused)
54 54 {
55 55 if (assign_expr)
56 56 set_state_expr(my_id, assign_expr->left, &held);
57 57 }
58 58
59 59 static void match_returns_null(const char *fn, struct expression *call_expr,
60 60 struct expression *assign_expr, void *unused)
61 61 {
62 62 if (assign_expr)
63 63 set_state_expr(my_id, assign_expr->left, &released);
64 64 }
65 65
66 66 static void check_for_held(void)
67 67 {
68 68 struct stree *stree;
69 69 struct sm_state *tmp;
70 70
71 71 stree = __get_cur_stree();
72 72 FOR_EACH_MY_SM(my_id, stree, tmp) {
73 73 if (slist_has_state(tmp->possible, &held)) {
74 74 sm_warning("'%s' held on error path.",
75 75 tmp->name);
76 76 }
77 77 } END_FOR_EACH_SM(tmp);
78 78 }
79 79
80 80 static void print_returns_held(struct expression *expr)
81 81 {
82 82 struct sm_state *sm;
83 83
84 84 if (!option_info)
85 85 return;
86 86 sm = get_sm_state_expr(my_id, expr);
87 87 if (!sm)
88 88 return;
89 89 if (slist_has_state(sm->possible, &held))
90 90 sm_info("returned dev is held.");
91 91 }
92 92
93 93 static void match_return(struct expression *ret_value)
94 94 {
95 95 print_returns_held(ret_value);
96 96 if (!is_error_return(ret_value))
97 97 return;
98 98 check_for_held();
99 99 }
100 100
101 101 static void register_returns_held_funcs(void)
102 102 {
103 103 struct token *token;
104 104 const char *func;
105 105
↓ open down ↓ |
105 lines elided |
↑ open up ↑ |
106 106 token = get_tokens_file("kernel.returns_held_funcs");
107 107 if (!token)
108 108 return;
109 109 if (token_type(token) != TOKEN_STREAMBEGIN)
110 110 return;
111 111 token = token->next;
112 112 while (token_type(token) != TOKEN_STREAMEND) {
113 113 if (token_type(token) != TOKEN_IDENT)
114 114 return;
115 115 func = show_ident(token->ident);
116 - return_implies_state(func, valid_ptr_min, valid_ptr_max,
116 + return_implies_state_sval(func, valid_ptr_min_sval, valid_ptr_max_sval,
117 117 &match_returns_held, NULL);
118 118 return_implies_state(func, 0, 0, &match_returns_null,
119 119 NULL);
120 120 token = token->next;
121 121 }
122 122 clear_token_alloc();
123 123 }
124 124
125 125 void check_held_dev(int id)
126 126 {
127 127 if (option_project != PROJ_KERNEL)
128 128 return;
129 129
130 130 my_id = id;
131 131 add_function_hook("dev_hold", &match_dev_hold, NULL);
132 132 add_function_hook("dev_put", &match_dev_put, NULL);
133 133 register_returns_held_funcs();
134 134 add_hook(&match_return, RETURN_HOOK);
135 135 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX