db: caller info needs to record the -1 parameters
[smatch.git] / smatch_buf_size.c
blob020f7397ae2eec993abd57495c11d452ec26f27d
1 /*
2 * smatch/smatch_buf_size.c
4 * Copyright (C) 2010 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
10 #include <stdlib.h>
11 #include <errno.h>
12 #include "parse.h"
13 #include "smatch.h"
14 #include "smatch_slist.h"
15 #include "smatch_extra.h"
18 * This check has two smatch IDs.
19 * my_size_id - used to store the size of arrays.
20 * my_strlen_id - track the strlen() of buffers.
23 static int my_size_id;
24 static int my_strlen_id;
26 struct limiter {
27 int buf_arg;
28 int limit_arg;
30 static struct limiter b0_l2 = {0, 2};
32 static _Bool params_set[32];
34 void set_param_buf_size(const char *name, struct symbol *sym, char *key, char *value)
36 char fullname[256];
37 unsigned int size;
39 if (strncmp(key, "$$", 2))
40 return;
42 snprintf(fullname, 256, "%s%s", name, key + 2);
44 errno = 0;
45 size = strtoul(value, NULL, 10);
46 if (errno)
47 return;
49 set_state(my_size_id, fullname, sym, alloc_state_num(size));
52 static int get_initializer_size(struct expression *expr)
54 switch (expr->type) {
55 case EXPR_STRING:
56 return expr->string->length;
57 case EXPR_INITIALIZER: {
58 struct expression *tmp;
59 int i = 0;
60 int max = 0;
62 FOR_EACH_PTR(expr->expr_list, tmp) {
63 if (tmp->type == EXPR_INDEX && tmp->idx_to > max)
64 max = tmp->idx_to;
65 i++;
66 } END_FOR_EACH_PTR(tmp);
67 if (max)
68 return max + 1;
69 return i;
71 case EXPR_SYMBOL:
72 return get_array_size(expr);
74 return 0;
77 static float get_cast_ratio(struct expression *unstripped)
79 struct expression *start_expr;
80 struct symbol *start_type;
81 struct symbol *end_type;
82 int start_bytes = 0;
83 int end_bytes = 0;
85 start_expr = strip_expr(unstripped);
86 start_type = get_type(start_expr);
87 end_type = get_type(unstripped);
88 if (!start_type || !end_type)
89 return 1;
91 if (start_type->type == SYM_PTR)
92 start_bytes = (get_base_type(start_type))->ctype.alignment;
93 if (start_type->type == SYM_ARRAY)
94 start_bytes = (get_base_type(start_type))->bit_size / 8;
95 if (end_type->type == SYM_PTR)
96 end_bytes = (get_base_type(end_type))->ctype.alignment;
97 if (end_type->type == SYM_ARRAY)
98 end_bytes = (get_base_type(end_type))->bit_size / 8;
100 if (!start_bytes || !end_bytes)
101 return 1;
102 return start_bytes / end_bytes;
105 int get_array_size(struct expression *expr)
107 struct symbol *tmp;
108 struct smatch_state *state;
109 int ret = 0;
110 float cast_ratio;
111 long long len;
113 if (expr->type == EXPR_STRING)
114 return expr->string->length;
116 cast_ratio = get_cast_ratio(expr);
117 expr = strip_expr(expr);
118 tmp = get_type(expr);
119 if (!tmp)
120 return 0;
122 if (tmp->type == SYM_ARRAY) {
123 ret = get_expression_value(tmp->array_size);
124 /* Dynamically sized array are -1 in sparse */
125 if (ret < 0)
126 return 0;
127 /* People put one element arrays on the end of structs */
128 if (ret == 1)
129 return 0;
130 if (ret)
131 return ret * cast_ratio;
134 state = get_state_expr(my_size_id, expr);
135 if (state == &merged)
136 return 0;
137 if (state && state->data) {
138 if (tmp->type == SYM_PTR)
139 tmp = get_base_type(tmp);
140 if (!tmp->ctype.alignment)
141 return 0;
142 ret = PTR_INT(state->data) / tmp->ctype.alignment;
143 return ret * cast_ratio;
146 if (expr->type == EXPR_SYMBOL && expr->symbol->initializer) {
147 if (expr->symbol->initializer != expr) /* int a = a; */
148 return get_initializer_size(expr->symbol->initializer) * cast_ratio;
151 state = get_state_expr(my_strlen_id, expr);
152 if (!state || !state->data)
153 return 0;
154 if (get_implied_max((struct expression *)state->data, &len))
155 return len + 1; /* add one because strlen doesn't include the NULL */
156 return 0;
159 int get_array_size_bytes(struct expression *expr)
161 struct symbol *tmp;
162 int array_size;
163 int element_size;
165 if (expr->type == EXPR_STRING)
166 return expr->string->length;
168 tmp = get_type(expr);
169 if (!tmp)
170 return 0;
172 if (tmp->type == SYM_ARRAY) {
173 tmp = get_base_type(tmp);
174 element_size = tmp->bit_size / 8;
175 } else if (tmp->type == SYM_PTR) {
176 tmp = get_base_type(tmp);
177 element_size = tmp->ctype.alignment;
178 } else {
179 return 0;
181 array_size = get_array_size(expr);
182 return array_size * element_size;
185 static void match_strlen_condition(struct expression *expr)
187 struct expression *left;
188 struct expression *right;
189 struct expression *str = NULL;
190 int strlen_left = 0;
191 int strlen_right = 0;
192 long long val;
193 struct smatch_state *true_state = NULL;
194 struct smatch_state *false_state = NULL;
196 if (expr->type != EXPR_COMPARE)
197 return;
198 left = strip_expr(expr->left);
199 right = strip_expr(expr->right);
201 if (left->type == EXPR_CALL && sym_name_is("strlen", left->fn)) {
202 str = get_argument_from_call_expr(left->args, 0);
203 strlen_left = 1;
205 if (right->type == EXPR_CALL && sym_name_is("strlen", right->fn)) {
206 str = get_argument_from_call_expr(right->args, 0);
207 strlen_right = 1;
210 if (!strlen_left && !strlen_right)
211 return;
212 if (strlen_left && strlen_right)
213 return;
215 if (strlen_left) {
216 if (!get_value(right, &val))
217 return;
219 if (strlen_right) {
220 if (!get_value(left, &val))
221 return;
224 if (expr->op == SPECIAL_EQUAL) {
225 set_true_false_states_expr(my_size_id, str, alloc_state_num(val + 1), NULL);
226 return;
228 if (expr->op == SPECIAL_NOTEQUAL) {
229 set_true_false_states_expr(my_size_id, str, NULL, alloc_state_num(val + 1));
230 return;
233 switch (expr->op) {
234 case '<':
235 case SPECIAL_UNSIGNED_LT:
236 if (strlen_left)
237 true_state = alloc_state_num(val);
238 else
239 false_state = alloc_state_num(val + 1);
240 break;
241 case SPECIAL_LTE:
242 case SPECIAL_UNSIGNED_LTE:
243 if (strlen_left)
244 true_state = alloc_state_num(val + 1);
245 else
246 false_state = alloc_state_num(val);
247 break;
248 case SPECIAL_GTE:
249 case SPECIAL_UNSIGNED_GTE:
250 if (strlen_left)
251 false_state = alloc_state_num(val);
252 else
253 true_state = alloc_state_num(val + 1);
254 break;
255 case '>':
256 case SPECIAL_UNSIGNED_GT:
257 if (strlen_left)
258 false_state = alloc_state_num(val + 1);
259 else
260 true_state = alloc_state_num(val);
261 break;
263 set_true_false_states_expr(my_size_id, str, true_state, false_state);
266 static struct expression *strip_ampersands(struct expression *expr)
268 struct symbol *type;
270 if (expr->type != EXPR_PREOP)
271 return expr;
272 if (expr->op != '&')
273 return expr;
274 type = get_type(expr->unop);
275 if (!type || type->type != SYM_ARRAY)
276 return expr;
277 return expr->unop;
280 static void match_array_assignment(struct expression *expr)
282 struct expression *left;
283 struct expression *right;
284 int array_size;
286 if (expr->op != '=')
287 return;
288 left = strip_expr(expr->left);
289 right = strip_expr(expr->right);
290 right = strip_ampersands(right);
291 array_size = get_array_size_bytes(right);
292 if (array_size)
293 set_state_expr(my_size_id, left, alloc_state_num(array_size));
296 static void match_alloc(const char *fn, struct expression *expr, void *_size_arg)
298 int size_arg = PTR_INT(_size_arg);
299 struct expression *right;
300 struct expression *arg;
301 long long bytes;
303 right = strip_expr(expr->right);
304 arg = get_argument_from_call_expr(right->args, size_arg);
305 if (!get_implied_value(arg, &bytes))
306 return;
307 set_state_expr(my_size_id, expr->left, alloc_state_num(bytes));
310 static void match_calloc(const char *fn, struct expression *expr, void *unused)
312 struct expression *right;
313 struct expression *arg;
314 long long elements;
315 long long size;
317 right = strip_expr(expr->right);
318 arg = get_argument_from_call_expr(right->args, 0);
319 if (!get_implied_value(arg, &elements))
320 return;
321 arg = get_argument_from_call_expr(right->args, 1);
322 if (!get_implied_value(arg, &size))
323 return;
324 set_state_expr(my_size_id, expr->left, alloc_state_num(elements * size));
327 static void match_strlen(const char *fn, struct expression *expr, void *unused)
329 struct expression *right;
330 struct expression *str;
331 struct expression *len_expr;
332 char *len_name;
333 struct smatch_state *state;
335 right = strip_expr(expr->right);
336 str = get_argument_from_call_expr(right->args, 0);
337 len_expr = strip_expr(expr->left);
339 len_name = get_variable_from_expr(len_expr, NULL);
340 if (!len_name)
341 return;
343 state = __alloc_smatch_state(0);
344 state->name = len_name;
345 state->data = len_expr;
346 set_state_expr(my_strlen_id, str, state);
349 static void match_limited(const char *fn, struct expression *expr, void *_limiter)
351 struct limiter *limiter = (struct limiter *)_limiter;
352 struct expression *dest;
353 struct expression *size_expr;
354 long long size;
356 dest = get_argument_from_call_expr(expr->args, limiter->buf_arg);
357 size_expr = get_argument_from_call_expr(expr->args, limiter->limit_arg);
358 if (!get_implied_max(size_expr, &size))
359 return;
360 set_state_expr(my_size_id, dest, alloc_state_num(size));
363 static void match_strcpy(const char *fn, struct expression *expr, void *unused)
365 struct expression fake_assign;
367 fake_assign.op = '=';
368 fake_assign.left = get_argument_from_call_expr(expr->args, 0);
369 fake_assign.right = get_argument_from_call_expr(expr->args, 1);
370 match_array_assignment(&fake_assign);
373 static void match_strndup(const char *fn, struct expression *expr, void *unused)
375 struct expression *fn_expr;
376 struct expression *size_expr;
377 long long size;
379 fn_expr = strip_expr(expr->right);
380 size_expr = get_argument_from_call_expr(fn_expr->args, 1);
381 if (!get_implied_max(size_expr, &size))
382 return;
384 /* It's easy to forget space for the NUL char */
385 size++;
386 set_state_expr(my_size_id, expr->left, alloc_state_num(size));
389 static void match_call(struct expression *expr)
391 char *name;
392 struct expression *arg;
393 int bytes;
394 int i;
396 name = get_fnptr_name(expr->fn);
397 if (!name)
398 return;
400 i = 0;
401 FOR_EACH_PTR(expr->args, arg) {
402 bytes = get_array_size_bytes(arg);
403 if (bytes > 1)
404 sm_msg("info: passes_buffer '%s' %d '$$' %d", name, i, bytes);
405 i++;
406 } END_FOR_EACH_PTR(arg);
408 free_string(name);
411 static void struct_member_callback(char *fn, int param, char *printed_name, struct smatch_state *state)
413 if (state == &merged)
414 return;
415 sm_msg("info: passes_buffer '%s' %d '%s' %s", fn, param, printed_name, state->name);
418 static void match_func_end(struct symbol *sym)
420 memset(params_set, 0, sizeof(params_set));
423 void register_buf_size(int id)
425 my_size_id = id;
427 add_definition_db_callback(set_param_buf_size, BUF_SIZE);
429 add_function_assign_hook("malloc", &match_alloc, INT_PTR(0));
430 add_function_assign_hook("calloc", &match_calloc, NULL);
431 add_function_assign_hook("memdup", &match_alloc, INT_PTR(1));
432 if (option_project == PROJ_KERNEL) {
433 add_function_assign_hook("kmalloc", &match_alloc, INT_PTR(0));
434 add_function_assign_hook("kzalloc", &match_alloc, INT_PTR(0));
435 add_function_assign_hook("vmalloc", &match_alloc, INT_PTR(0));
436 add_function_assign_hook("__vmalloc", &match_alloc, INT_PTR(0));
437 add_function_assign_hook("kcalloc", &match_calloc, NULL);
438 add_function_assign_hook("drm_malloc_ab", &match_calloc, NULL);
439 add_function_assign_hook("drm_calloc_large", &match_calloc, NULL);
440 add_function_assign_hook("kmemdup", &match_alloc, INT_PTR(1));
441 add_function_assign_hook("kmemdup_user", &match_alloc, INT_PTR(1));
443 add_hook(&match_array_assignment, ASSIGNMENT_HOOK);
444 add_hook(&match_strlen_condition, CONDITION_HOOK);
445 add_function_assign_hook("strlen", &match_strlen, NULL);
447 add_function_hook("strlcpy", &match_limited, &b0_l2);
448 add_function_hook("strlcat", &match_limited, &b0_l2);
449 add_function_hook("memscan", &match_limited, &b0_l2);
451 add_function_hook("strcpy", &match_strcpy, NULL);
453 add_function_assign_hook("strndup", match_strndup, NULL);
454 if (option_project == PROJ_KERNEL)
455 add_function_assign_hook("kstrndup", match_strndup, NULL);
457 if (option_info) {
458 add_hook(&match_call, FUNCTION_CALL_HOOK);
459 add_member_info_callback(my_size_id, struct_member_callback);
462 add_hook(&match_func_end, END_FUNC_HOOK);
465 void register_strlen(int id)
467 my_strlen_id = id;