db: always split the return values if we're not using --info
[smatch.git] / check_kernel.c
blobb5ba69f27caca7a7ffab49fe5286ea5b3e4d1021
1 /*
2 * sparse/check_kernel.c
4 * Copyright (C) 2010 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
11 * This is kernel specific stuff for smatch_extra.
14 #include "scope.h"
15 #include "smatch.h"
16 #include "smatch_extra.h"
18 static int implied_err_cast_return(struct expression *call, void *unused, struct range_list **rl)
20 struct expression *arg;
22 arg = get_argument_from_call_expr(call->args, 0);
23 if (!get_implied_rl(arg, rl))
24 *rl = alloc_rl(ll_to_sval(-4095), ll_to_sval(-1));
25 return 1;
28 static int implied_copy_return(struct expression *call, void *unused, struct range_list **rl)
30 struct expression *arg;
31 sval_t max;
33 arg = get_argument_from_call_expr(call->args, 2);
34 get_absolute_max(arg, &max);
35 *rl = alloc_rl(ll_to_sval(0), max);
36 return 1;
39 static void match_param_valid_ptr(const char *fn, struct expression *call_expr,
40 struct expression *assign_expr, void *_param)
42 int param = PTR_INT(_param);
43 struct expression *arg;
44 struct smatch_state *pre_state;
45 struct smatch_state *end_state;
47 arg = get_argument_from_call_expr(call_expr->args, param);
48 pre_state = get_state_expr(SMATCH_EXTRA, arg);
49 end_state = estate_filter_range(pre_state, ll_to_sval(-4095), ll_to_sval(0));
50 set_extra_expr_nomod(arg, end_state);
53 static void match_param_err_or_null(const char *fn, struct expression *call_expr,
54 struct expression *assign_expr, void *_param)
56 int param = PTR_INT(_param);
57 struct expression *arg;
58 struct range_list *rl;
59 struct smatch_state *pre_state;
60 struct smatch_state *end_state;
62 arg = get_argument_from_call_expr(call_expr->args, param);
63 pre_state = get_state_expr(SMATCH_EXTRA, arg);
64 rl = alloc_rl(ll_to_sval(-4095), ll_to_sval(0));
65 rl = rl_intersection(estate_rl(pre_state), rl);
66 rl = cast_rl(estate_type(pre_state), rl);
67 end_state = alloc_estate_rl(rl);
68 set_extra_expr_nomod(arg, end_state);
71 static void match_not_err(const char *fn, struct expression *call_expr,
72 struct expression *assign_expr, void *unused)
74 struct expression *arg;
75 struct smatch_state *pre_state;
76 struct smatch_state *new_state;
78 arg = get_argument_from_call_expr(call_expr->args, 0);
79 pre_state = get_state_expr(SMATCH_EXTRA, arg);
80 new_state = estate_filter_range(pre_state, sval_type_min(&long_ctype), ll_to_sval(-1));
81 set_extra_expr_nomod(arg, new_state);
84 static void match_err(const char *fn, struct expression *call_expr,
85 struct expression *assign_expr, void *unused)
87 struct expression *arg;
88 struct smatch_state *pre_state;
89 struct smatch_state *new_state;
91 arg = get_argument_from_call_expr(call_expr->args, 0);
92 pre_state = get_state_expr(SMATCH_EXTRA, arg);
93 new_state = estate_filter_range(pre_state, sval_type_min(&long_ctype), ll_to_sval(-4096));
94 new_state = estate_filter_range(new_state, ll_to_sval(0), sval_type_max(&long_ctype));
95 set_extra_expr_nomod(arg, new_state);
98 static void match_container_of_macro(const char *fn, struct expression *expr, void *unused)
100 set_extra_expr_mod(expr->left, alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval));
103 static void match_container_of(struct expression *expr)
105 struct expression *right = expr->right;
106 char *macro;
109 * The problem here is that sometimes the container_of() macro is itself
110 * inside a macro and get_macro() only returns the name of the outside
111 * macro.
115 * This actually an expression statement assignment but smatch_flow
116 * pre-mangles it for us so we only get the last chunk:
117 * sk = (typeof(sk))((char *)__mptr - offsetof(...))
120 macro = get_macro_name(right->pos);
121 if (!macro)
122 return;
123 if (right->type != EXPR_CAST)
124 return;
125 right = strip_expr(right);
126 if (right->type != EXPR_BINOP || right->op != '-' ||
127 right->left->type != EXPR_CAST)
128 return;
129 right = strip_expr(right->left);
130 if (right->type != EXPR_SYMBOL)
131 return;
132 if (!right->symbol->ident ||
133 strcmp(right->symbol->ident->name, "__mptr") != 0)
134 return;
135 set_extra_expr_mod(expr->left, alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval));
138 static int match_next_bit(struct expression *call, void *unused, struct range_list **rl)
140 struct expression *start_arg;
141 struct expression *size_arg;
142 struct symbol *type;
143 sval_t min, max, tmp;
145 size_arg = get_argument_from_call_expr(call->args, 1);
146 /* btw. there isn't a start_arg for find_first_bit() */
147 start_arg = get_argument_from_call_expr(call->args, 2);
149 type = get_type(call);
150 min = sval_type_val(type, 0);
151 max = sval_type_val(type, sizeof(long long) * 8);
153 if (get_implied_max(size_arg, &tmp) && tmp.uvalue < max.value)
154 max = tmp;
155 if (start_arg && get_implied_min(start_arg, &tmp) && !sval_is_negative(tmp))
156 min = tmp;
157 if (sval_cmp(min, max) > 0)
158 max = min;
159 min = sval_cast(type, min);
160 max = sval_cast(type, max);
161 *rl = alloc_rl(min, max);
162 return 1;
165 static void find_module_init_exit(struct symbol_list *sym_list)
167 struct symbol *sym;
168 struct symbol *fn;
169 struct statement *stmt;
170 char *name;
171 int init;
172 int count;
175 * This is more complicated because Sparse ignores the "alias"
176 * attribute. I search backwards because module_init() is normally at
177 * the end of the file.
179 count = 0;
180 FOR_EACH_PTR_REVERSE(sym_list, sym) {
181 if (sym->type != SYM_NODE)
182 continue;
183 if (!(sym->ctype.modifiers & MOD_STATIC))
184 continue;
185 fn = get_base_type(sym);
186 if (!fn)
187 continue;
188 if (fn->type != SYM_FN)
189 continue;
190 if (!sym->ident || !sym->ident->name)
191 continue;
192 if (!fn->inline_stmt)
193 continue;
194 if (strcmp(sym->ident->name, "__inittest") == 0)
195 init = 1;
196 else if (strcmp(sym->ident->name, "__exittest") == 0)
197 init = 0;
198 else
199 continue;
201 count++;
203 stmt = first_ptr_list((struct ptr_list *)fn->inline_stmt->stmts);
204 if (!stmt || stmt->type != STMT_RETURN)
205 continue;
206 name = expr_to_var(stmt->ret_value);
207 if (!name)
208 continue;
209 if (init)
210 sql_insert_function_ptr(name, "(struct module)->init");
211 else
212 sql_insert_function_ptr(name, "(struct module)->exit");
213 free_string(name);
214 if (count >= 2)
215 return;
216 } END_FOR_EACH_PTR_REVERSE(sym);
219 static void match_end_file(struct symbol_list *sym_list)
221 struct symbol *sym;
223 /* find the last static symbol in the file */
224 FOR_EACH_PTR_REVERSE(sym_list, sym) {
225 if (!(sym->ctype.modifiers & MOD_STATIC))
226 continue;
227 if (!sym->scope)
228 continue;
229 find_module_init_exit(sym->scope->symbols);
230 return;
231 } END_FOR_EACH_PTR_REVERSE(sym);
234 void check_kernel(int id)
236 if (option_project != PROJ_KERNEL)
237 return;
239 add_implied_return_hook("ERR_PTR", &implied_err_cast_return, NULL);
240 add_implied_return_hook("ERR_CAST", &implied_err_cast_return, NULL);
241 add_implied_return_hook("PTR_ERR", &implied_err_cast_return, NULL);
242 return_implies_state("IS_ERR_OR_NULL", 0, 0, &match_param_valid_ptr, (void *)0);
243 return_implies_state("IS_ERR_OR_NULL", 1, 1, &match_param_err_or_null, (void *)0);
244 return_implies_state("IS_ERR", 0, 0, &match_not_err, NULL);
245 return_implies_state("IS_ERR", 1, 1, &match_err, NULL);
246 return_implies_state("tomoyo_memory_ok", 1, 1, &match_param_valid_ptr, (void *)0);
248 add_macro_assign_hook_extra("container_of", &match_container_of_macro, NULL);
249 add_hook(match_container_of, ASSIGNMENT_HOOK);
251 add_implied_return_hook("copy_to_user", &implied_copy_return, NULL);
252 add_implied_return_hook("__copy_to_user", &implied_copy_return, NULL);
253 add_implied_return_hook("copy_from_user", &implied_copy_return, NULL);
254 add_implied_return_hook("__copy_fom_user", &implied_copy_return, NULL);
255 add_implied_return_hook("clear_user", &implied_copy_return, NULL);
257 add_implied_return_hook("find_next_bit", &match_next_bit, NULL);
258 add_implied_return_hook("find_next_zero_bit", &match_next_bit, NULL);
259 add_implied_return_hook("find_first_bit", &match_next_bit, NULL);
260 add_implied_return_hook("find_first_zero_bit", &match_next_bit, NULL);
262 add_function_hook("__ftrace_bad_type", &__match_nullify_path_hook, NULL);
264 if (option_info)
265 add_hook(match_end_file, END_FILE_HOOK);