buf_size: pull handling of normal arrays into separate function
[smatch.git] / smatch_buf_size.c
blob4f870e90ae94184290d298b58af0ffd2da08e496
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 static void set_undefined(struct sm_state *sm)
36 if (sm->state != &undefined)
37 set_state(sm->owner, sm->name, sm->sym, &undefined);
40 void set_param_buf_size(const char *name, struct symbol *sym, char *key, char *value)
42 char fullname[256];
43 unsigned int size;
45 if (strncmp(key, "$$", 2))
46 return;
48 snprintf(fullname, 256, "%s%s", name, key + 2);
50 errno = 0;
51 size = strtoul(value, NULL, 10);
52 if (errno)
53 return;
55 set_state(my_size_id, fullname, sym, alloc_state_num(size));
58 static int get_initializer_size(struct expression *expr)
60 switch (expr->type) {
61 case EXPR_STRING:
62 return expr->string->length;
63 case EXPR_INITIALIZER: {
64 struct expression *tmp;
65 int i = 0;
66 int max = 0;
68 FOR_EACH_PTR(expr->expr_list, tmp) {
69 if (tmp->type == EXPR_INDEX && tmp->idx_to > max)
70 max = tmp->idx_to;
71 i++;
72 } END_FOR_EACH_PTR(tmp);
73 if (max)
74 return max + 1;
75 return i;
77 case EXPR_SYMBOL:
78 return get_array_size(expr);
80 return 0;
83 static float get_cast_ratio(struct expression *unstripped)
85 struct expression *start_expr;
86 struct symbol *start_type;
87 struct symbol *end_type;
88 int start_bytes = 0;
89 int end_bytes = 0;
91 start_expr = strip_expr(unstripped);
92 start_type = get_type(start_expr);
93 end_type = get_type(unstripped);
94 if (!start_type || !end_type)
95 return 1;
97 if (start_type->type == SYM_PTR)
98 start_bytes = (get_base_type(start_type))->ctype.alignment;
99 if (start_type->type == SYM_ARRAY)
100 start_bytes = (get_base_type(start_type))->bit_size / 8;
101 if (end_type->type == SYM_PTR)
102 end_bytes = (get_base_type(end_type))->ctype.alignment;
103 if (end_type->type == SYM_ARRAY)
104 end_bytes = (get_base_type(end_type))->bit_size / 8;
106 if (!start_bytes || !end_bytes)
107 return 1;
108 return start_bytes / end_bytes;
111 static int db_size;
112 static int db_size_callback(void *unused, int argc, char **argv, char **azColName)
114 db_size = atoi(argv[0]);
115 return 0;
118 static int size_from_db(struct expression *expr)
120 char *name;
122 if (!option_spammy)
123 return 0;
125 name = get_member_name(expr);
126 if (!name)
127 return 0;
129 db_size = 0;
130 run_sql(db_size_callback, "select size from type_size where type = '%s'",
131 name);
132 return db_size;
135 static int get_real_array_size(struct expression *expr)
137 struct symbol *type;
138 float cast_ratio;
139 int ret;
141 type = get_type(expr);
142 if (!type || type->type != SYM_ARRAY)
143 return 0;
145 cast_ratio = get_cast_ratio(expr);
147 ret = get_expression_value(type->array_size);
148 /* Dynamically sized array are -1 in sparse */
149 if (ret <= 0)
150 return 0;
151 /* People put one element arrays on the end of structs */
152 if (ret == 1)
153 return 0;
155 return ret * cast_ratio;
158 int get_array_size(struct expression *expr)
160 struct symbol *tmp;
161 struct smatch_state *state;
162 int ret = 0;
163 float cast_ratio;
164 long long len;
166 if (expr->type == EXPR_STRING)
167 return expr->string->length;
169 ret = get_real_array_size(expr);
170 if (ret)
171 return ret;
173 cast_ratio = get_cast_ratio(expr);
174 expr = strip_expr(expr);
175 tmp = get_type(expr);
176 if (!tmp)
177 return 0;
179 state = get_state_expr(my_size_id, expr);
180 if (state == &merged)
181 return 0;
182 if (state && state->data) {
183 if (tmp->type == SYM_PTR)
184 tmp = get_base_type(tmp);
185 if (!tmp->ctype.alignment)
186 return 0;
187 ret = PTR_INT(state->data) / tmp->ctype.alignment;
188 return ret * cast_ratio;
191 if (expr->type == EXPR_SYMBOL && expr->symbol->initializer) {
192 if (expr->symbol->initializer != expr) /* int a = a; */
193 return get_initializer_size(expr->symbol->initializer) * cast_ratio;
196 state = get_state_expr(my_strlen_id, expr);
197 if (!state || !state->data)
198 return 0;
199 if (get_implied_max((struct expression *)state->data, &len))
200 return len + 1; /* add one because strlen doesn't include the NULL */
201 return 0;
204 int get_array_size_bytes(struct expression *expr)
206 struct symbol *tmp;
207 int array_size;
208 int element_size;
210 if (!expr)
211 return 0;
213 if (expr->type == EXPR_STRING)
214 return expr->string->length;
216 tmp = get_type(expr);
217 if (!tmp)
218 return 0;
220 if (tmp->type == SYM_ARRAY) {
221 tmp = get_base_type(tmp);
222 element_size = tmp->bit_size / 8;
223 } else if (tmp->type == SYM_PTR) {
224 tmp = get_base_type(tmp);
225 element_size = tmp->ctype.alignment;
226 } else {
227 return 0;
229 array_size = get_array_size(expr);
230 if (array_size)
231 return array_size * element_size;
233 return size_from_db(expr);
236 static void match_strlen_condition(struct expression *expr)
238 struct expression *left;
239 struct expression *right;
240 struct expression *str = NULL;
241 int strlen_left = 0;
242 int strlen_right = 0;
243 long long val;
244 struct smatch_state *true_state = NULL;
245 struct smatch_state *false_state = NULL;
247 if (expr->type != EXPR_COMPARE)
248 return;
249 left = strip_expr(expr->left);
250 right = strip_expr(expr->right);
252 if (left->type == EXPR_CALL && sym_name_is("strlen", left->fn)) {
253 str = get_argument_from_call_expr(left->args, 0);
254 strlen_left = 1;
256 if (right->type == EXPR_CALL && sym_name_is("strlen", right->fn)) {
257 str = get_argument_from_call_expr(right->args, 0);
258 strlen_right = 1;
261 if (!strlen_left && !strlen_right)
262 return;
263 if (strlen_left && strlen_right)
264 return;
266 if (strlen_left) {
267 if (!get_value(right, &val))
268 return;
270 if (strlen_right) {
271 if (!get_value(left, &val))
272 return;
275 if (expr->op == SPECIAL_EQUAL) {
276 set_true_false_states_expr(my_size_id, str, alloc_state_num(val + 1), NULL);
277 return;
279 if (expr->op == SPECIAL_NOTEQUAL) {
280 set_true_false_states_expr(my_size_id, str, NULL, alloc_state_num(val + 1));
281 return;
284 switch (expr->op) {
285 case '<':
286 case SPECIAL_UNSIGNED_LT:
287 if (strlen_left)
288 true_state = alloc_state_num(val);
289 else
290 false_state = alloc_state_num(val + 1);
291 break;
292 case SPECIAL_LTE:
293 case SPECIAL_UNSIGNED_LTE:
294 if (strlen_left)
295 true_state = alloc_state_num(val + 1);
296 else
297 false_state = alloc_state_num(val);
298 break;
299 case SPECIAL_GTE:
300 case SPECIAL_UNSIGNED_GTE:
301 if (strlen_left)
302 false_state = alloc_state_num(val);
303 else
304 true_state = alloc_state_num(val + 1);
305 break;
306 case '>':
307 case SPECIAL_UNSIGNED_GT:
308 if (strlen_left)
309 false_state = alloc_state_num(val + 1);
310 else
311 true_state = alloc_state_num(val);
312 break;
314 set_true_false_states_expr(my_size_id, str, true_state, false_state);
317 static struct expression *strip_ampersands(struct expression *expr)
319 struct symbol *type;
321 if (expr->type != EXPR_PREOP)
322 return expr;
323 if (expr->op != '&')
324 return expr;
325 type = get_type(expr->unop);
326 if (!type || type->type != SYM_ARRAY)
327 return expr;
328 return expr->unop;
331 static void match_array_assignment(struct expression *expr)
333 struct expression *left;
334 struct expression *right;
335 int array_size;
337 if (expr->op != '=')
338 return;
339 left = strip_expr(expr->left);
340 right = strip_expr(expr->right);
341 right = strip_ampersands(right);
342 array_size = get_array_size_bytes(right);
343 if (array_size)
344 set_state_expr(my_size_id, left, alloc_state_num(array_size));
347 static void match_alloc(const char *fn, struct expression *expr, void *_size_arg)
349 int size_arg = PTR_INT(_size_arg);
350 struct expression *right;
351 struct expression *arg;
352 long long bytes;
354 right = strip_expr(expr->right);
355 arg = get_argument_from_call_expr(right->args, size_arg);
356 if (!get_implied_value(arg, &bytes))
357 return;
359 set_state_expr(my_size_id, expr->left, alloc_state_num(bytes));
361 if (option_info) {
362 char *member = get_member_name(expr->left);
364 if (member)
365 sm_msg("info: '%s' allocated_buf_size %lld",
366 member, bytes);
367 free_string(member);
371 static void match_calloc(const char *fn, struct expression *expr, void *unused)
373 struct expression *right;
374 struct expression *arg;
375 long long elements;
376 long long size;
378 right = strip_expr(expr->right);
379 arg = get_argument_from_call_expr(right->args, 0);
380 if (!get_implied_value(arg, &elements))
381 return;
382 arg = get_argument_from_call_expr(right->args, 1);
383 if (!get_implied_value(arg, &size))
384 return;
385 set_state_expr(my_size_id, expr->left, alloc_state_num(elements * size));
388 static void match_strlen(const char *fn, struct expression *expr, void *unused)
390 struct expression *right;
391 struct expression *str;
392 struct expression *len_expr;
393 char *len_name;
394 struct smatch_state *state;
396 right = strip_expr(expr->right);
397 str = get_argument_from_call_expr(right->args, 0);
398 len_expr = strip_expr(expr->left);
400 len_name = get_variable_from_expr(len_expr, NULL);
401 if (!len_name)
402 return;
404 state = __alloc_smatch_state(0);
405 state->name = len_name;
406 state->data = len_expr;
407 set_state_expr(my_strlen_id, str, state);
410 static void match_limited(const char *fn, struct expression *expr, void *_limiter)
412 struct limiter *limiter = (struct limiter *)_limiter;
413 struct expression *dest;
414 struct expression *size_expr;
415 long long size;
417 dest = get_argument_from_call_expr(expr->args, limiter->buf_arg);
418 size_expr = get_argument_from_call_expr(expr->args, limiter->limit_arg);
419 if (!get_implied_max(size_expr, &size))
420 return;
421 set_state_expr(my_size_id, dest, alloc_state_num(size));
424 static void match_strcpy(const char *fn, struct expression *expr, void *unused)
426 struct expression fake_assign;
428 fake_assign.op = '=';
429 fake_assign.left = get_argument_from_call_expr(expr->args, 0);
430 fake_assign.right = get_argument_from_call_expr(expr->args, 1);
431 match_array_assignment(&fake_assign);
434 static void match_strndup(const char *fn, struct expression *expr, void *unused)
436 struct expression *fn_expr;
437 struct expression *size_expr;
438 long long size;
440 fn_expr = strip_expr(expr->right);
441 size_expr = get_argument_from_call_expr(fn_expr->args, 1);
442 if (!get_implied_max(size_expr, &size))
443 return;
445 /* It's easy to forget space for the NUL char */
446 size++;
447 set_state_expr(my_size_id, expr->left, alloc_state_num(size));
450 static void match_call(struct expression *expr)
452 char *name;
453 struct expression *arg;
454 int bytes;
455 int i;
457 name = get_fnptr_name(expr->fn);
458 if (!name)
459 return;
461 i = 0;
462 FOR_EACH_PTR(expr->args, arg) {
463 bytes = get_array_size_bytes(arg);
464 if (bytes > 1)
465 sm_msg("info: passes_buffer '%s' %d '$$' %d %s",
466 name, i, bytes,
467 is_static(expr->fn) ? "static" : "global");
468 i++;
469 } END_FOR_EACH_PTR(arg);
471 free_string(name);
474 static void struct_member_callback(char *fn, char *global_static, int param, char *printed_name, struct smatch_state *state)
476 if (state == &merged)
477 return;
478 sm_msg("info: passes_buffer '%s' %d '%s' %s %s", fn, param, printed_name, state->name, global_static);
481 static void match_func_end(struct symbol *sym)
483 memset(params_set, 0, sizeof(params_set));
486 void register_buf_size(int id)
488 my_size_id = id;
490 add_definition_db_callback(set_param_buf_size, BUF_SIZE);
492 add_function_assign_hook("malloc", &match_alloc, INT_PTR(0));
493 add_function_assign_hook("calloc", &match_calloc, NULL);
494 add_function_assign_hook("memdup", &match_alloc, INT_PTR(1));
495 if (option_project == PROJ_KERNEL) {
496 add_function_assign_hook("kmalloc", &match_alloc, INT_PTR(0));
497 add_function_assign_hook("kzalloc", &match_alloc, INT_PTR(0));
498 add_function_assign_hook("vmalloc", &match_alloc, INT_PTR(0));
499 add_function_assign_hook("__vmalloc", &match_alloc, INT_PTR(0));
500 add_function_assign_hook("kcalloc", &match_calloc, NULL);
501 add_function_assign_hook("drm_malloc_ab", &match_calloc, NULL);
502 add_function_assign_hook("drm_calloc_large", &match_calloc, NULL);
503 add_function_assign_hook("kmemdup", &match_alloc, INT_PTR(1));
504 add_function_assign_hook("kmemdup_user", &match_alloc, INT_PTR(1));
506 add_hook(&match_array_assignment, ASSIGNMENT_HOOK);
507 add_hook(&match_strlen_condition, CONDITION_HOOK);
508 add_function_assign_hook("strlen", &match_strlen, NULL);
510 add_function_hook("strlcpy", &match_limited, &b0_l2);
511 add_function_hook("strlcat", &match_limited, &b0_l2);
512 add_function_hook("memscan", &match_limited, &b0_l2);
514 add_function_hook("strcpy", &match_strcpy, NULL);
516 add_function_assign_hook("strndup", match_strndup, NULL);
517 if (option_project == PROJ_KERNEL)
518 add_function_assign_hook("kstrndup", match_strndup, NULL);
520 if (option_info) {
521 add_hook(&match_call, FUNCTION_CALL_HOOK);
522 add_member_info_callback(my_size_id, struct_member_callback);
525 add_hook(&match_func_end, END_FUNC_HOOK);
526 add_modification_hook(my_size_id, &set_undefined);
529 void register_strlen(int id)
531 my_strlen_id = id;
532 add_modification_hook(my_strlen_id, &set_undefined);