db_caller_info: add some comments
[smatch.git] / smatch_buf_size.c
blobee03fdce5c44a0efc47e3d6927b4a859e9a01ad6
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 static struct smatch_state *merge_func(struct smatch_state *s1, struct smatch_state *s2)
42 if (PTR_INT(s1->data) == PTR_INT(s2->data))
43 return s1;
44 return &undefined;
47 void set_param_buf_size(const char *name, struct symbol *sym, char *key, char *value)
49 char fullname[256];
50 unsigned int size;
52 if (strncmp(key, "$$", 2))
53 return;
55 snprintf(fullname, 256, "%s%s", name, key + 2);
57 errno = 0;
58 size = strtoul(value, NULL, 10);
59 if (errno)
60 return;
62 set_state(my_size_id, fullname, sym, alloc_state_num(size));
65 static int bytes_per_element(struct expression *expr)
67 struct symbol *type;
68 int bpe;
70 if (expr->type == EXPR_STRING)
71 return 1;
72 type = get_type(expr);
73 if (!type)
74 return 0;
76 if (type->type != SYM_PTR && type->type != SYM_ARRAY)
77 return 0;
79 type = get_base_type(type);
80 bpe = bits_to_bytes(type->bit_size);
82 if (bpe == -1) /* void pointer */
83 bpe = 1;
85 return bpe;
88 static int bytes_to_elements(struct expression *expr, int bytes)
90 int bpe;
92 bpe = bytes_per_element(expr);
93 if (bpe == 0)
94 return 0;
95 return bytes / bpe;
98 static int elements_to_bytes(struct expression *expr, int elements)
100 int bpe;
102 bpe = bytes_per_element(expr);
103 return elements * bpe;
106 static int get_initializer_size(struct expression *expr)
108 switch (expr->type) {
109 case EXPR_STRING:
110 return expr->string->length;
111 case EXPR_INITIALIZER: {
112 struct expression *tmp;
113 int i = 0;
114 int max = 0;
116 FOR_EACH_PTR(expr->expr_list, tmp) {
117 if (tmp->type == EXPR_INDEX && tmp->idx_to > max)
118 max = tmp->idx_to;
119 i++;
120 } END_FOR_EACH_PTR(tmp);
121 if (max)
122 return max + 1;
123 return i;
125 case EXPR_SYMBOL:
126 return get_array_size(expr);
128 return 0;
131 static int db_size;
132 static int db_size_callback(void *unused, int argc, char **argv, char **azColName)
134 if (db_size == 0)
135 db_size = atoi(argv[0]);
136 else
137 db_size = -1;
138 return 0;
141 static int size_from_db(struct expression *expr)
143 int this_file_only = 0;
144 char *name;
146 if (!option_spammy)
147 return 0;
149 name = get_member_name(expr);
150 if (!name && is_static(expr)) {
151 name = get_variable_from_expr(expr, NULL);
152 this_file_only = 1;
154 if (!name)
155 return 0;
157 db_size = 0;
158 run_sql(db_size_callback, "select size from type_size where type = '%s' and file = '%s'",
159 name, get_filename());
160 if (db_size == -1)
161 return 0;
162 if (db_size != 0)
163 return db_size;
164 if (this_file_only)
165 return 0;
167 run_sql(db_size_callback, "select size from type_size where type = '%s'",
168 name);
170 if (db_size == -1)
171 db_size = 0;
173 return db_size;
176 static int get_real_array_size(struct expression *expr)
178 struct symbol *type;
179 int ret;
181 if (expr->type == EXPR_BINOP) /* array elements foo[5] */
182 return 0;
184 type = get_type(expr);
185 if (!type || type->type != SYM_ARRAY)
186 return 0;
188 ret = get_expression_value(type->array_size);
189 /* Dynamically sized array are -1 in sparse */
190 if (ret <= 0)
191 return 0;
192 /* People put one element arrays on the end of structs */
193 if (ret == 1)
194 return 0;
196 return ret;
199 static int get_size_from_initializer(struct expression *expr)
201 if (expr->type != EXPR_SYMBOL || !expr->symbol->initializer)
202 return 0;
203 if (expr->symbol->initializer == expr) /* int a = a; */
204 return 0;
205 return get_initializer_size(expr->symbol->initializer);
208 static int get_stored_size_bytes(struct expression *expr)
210 struct smatch_state *state;
212 state = get_state_expr(my_size_id, expr);
213 if (!state)
214 return 0;
215 return PTR_INT(state->data);
218 static int get_stored_size_bytes_min(struct expression *expr)
220 struct sm_state *sm, *tmp;
221 int min = 0;
223 sm = get_sm_state_expr(my_size_id, expr);
224 if (!sm)
225 return 0;
226 FOR_EACH_PTR(sm->possible, tmp) {
227 if (PTR_INT(tmp->state->data) <= 0)
228 continue;
229 if (PTR_INT(tmp->state->data) < min)
230 min = PTR_INT(tmp->state->data);
231 } END_FOR_EACH_PTR(tmp);
233 return min;
236 static int get_bytes_from_address(struct expression *expr)
238 struct symbol *type;
239 int ret;
241 if (!option_spammy)
242 return 0;
243 if (expr->type != EXPR_PREOP || expr->op != '&')
244 return 0;
245 type = get_type(expr);
246 if (!type)
247 return 0;
249 if (type->type == SYM_PTR)
250 type = get_base_type(type);
252 ret = bits_to_bytes(type->bit_size);
253 if (ret == -1)
254 return 0;
255 if (ret == 1)
256 return 0; /* ignore char pointers */
258 return ret;
261 static int get_size_from_strlen(struct expression *expr)
263 struct smatch_state *state;
264 long long len;
266 state = get_state_expr(my_strlen_id, expr);
267 if (!state || !state->data)
268 return 0;
269 if (get_implied_max((struct expression *)state->data, &len))
270 return len + 1; /* add one because strlen doesn't include the NULL */
271 return 0;
274 static struct expression *remove_addr_fluff(struct expression *expr)
276 struct expression *tmp;
277 long long val;
279 expr = strip_expr(expr);
281 /* remove '&' and '*' operations that cancel */
282 while (expr->type == EXPR_PREOP && expr->op == '&') {
283 tmp = strip_expr(expr->unop);
284 if (tmp->type != EXPR_PREOP)
285 break;
286 if (tmp->op != '*')
287 break;
288 expr = strip_expr(tmp->unop);
291 /* "foo + 0" is just "foo" */
292 if (expr->type == EXPR_BINOP && expr->op == '+' &&
293 get_value(expr->right, &val) && val == 0)
294 return expr->left;
296 return expr;
299 int get_array_size_bytes(struct expression *expr)
301 int size;
303 expr = remove_addr_fluff(expr);
304 if (!expr)
305 return 0;
307 /* strcpy(foo, "BAR"); */
308 if (expr->type == EXPR_STRING)
309 return expr->string->length;
311 /* buf[4] */
312 size = get_real_array_size(expr);
313 if (size)
314 return elements_to_bytes(expr, size);
316 /* buf = malloc(1024); */
317 size = get_stored_size_bytes(expr);
318 if (size)
319 return size;
321 /* char *foo = "BAR" */
322 size = get_size_from_initializer(expr);
323 if (size)
324 return elements_to_bytes(expr, size);
326 size = get_bytes_from_address(expr);
327 if (size)
328 return size;
330 /* if (strlen(foo) > 4) */
331 size = get_size_from_strlen(expr);
332 if (size)
333 return size;
335 return size_from_db(expr);
338 int get_array_size_bytes_min(struct expression *expr)
340 int size;
341 int tmp;
343 size = get_array_size_bytes(expr);
345 tmp = get_stored_size_bytes_min(expr);
346 if (size <= 0 || (tmp >= 1 && tmp < size))
347 size = tmp;
348 return size;
351 int get_array_size(struct expression *expr)
353 int bytes;
355 bytes = get_array_size_bytes(expr);
356 return bytes_to_elements(expr, bytes);
359 static void match_strlen_condition(struct expression *expr)
361 struct expression *left;
362 struct expression *right;
363 struct expression *str = NULL;
364 int strlen_left = 0;
365 int strlen_right = 0;
366 long long val;
367 struct smatch_state *true_state = NULL;
368 struct smatch_state *false_state = NULL;
370 if (expr->type != EXPR_COMPARE)
371 return;
372 left = strip_expr(expr->left);
373 right = strip_expr(expr->right);
375 if (left->type == EXPR_CALL && sym_name_is("strlen", left->fn)) {
376 str = get_argument_from_call_expr(left->args, 0);
377 strlen_left = 1;
379 if (right->type == EXPR_CALL && sym_name_is("strlen", right->fn)) {
380 str = get_argument_from_call_expr(right->args, 0);
381 strlen_right = 1;
384 if (!strlen_left && !strlen_right)
385 return;
386 if (strlen_left && strlen_right)
387 return;
389 if (strlen_left) {
390 if (!get_value(right, &val))
391 return;
393 if (strlen_right) {
394 if (!get_value(left, &val))
395 return;
398 if (expr->op == SPECIAL_EQUAL) {
399 set_true_false_states_expr(my_size_id, str, alloc_state_num(val + 1), NULL);
400 return;
402 if (expr->op == SPECIAL_NOTEQUAL) {
403 set_true_false_states_expr(my_size_id, str, NULL, alloc_state_num(val + 1));
404 return;
407 switch (expr->op) {
408 case '<':
409 case SPECIAL_UNSIGNED_LT:
410 if (strlen_left)
411 true_state = alloc_state_num(val);
412 else
413 false_state = alloc_state_num(val + 1);
414 break;
415 case SPECIAL_LTE:
416 case SPECIAL_UNSIGNED_LTE:
417 if (strlen_left)
418 true_state = alloc_state_num(val + 1);
419 else
420 false_state = alloc_state_num(val);
421 break;
422 case SPECIAL_GTE:
423 case SPECIAL_UNSIGNED_GTE:
424 if (strlen_left)
425 false_state = alloc_state_num(val);
426 else
427 true_state = alloc_state_num(val + 1);
428 break;
429 case '>':
430 case SPECIAL_UNSIGNED_GT:
431 if (strlen_left)
432 false_state = alloc_state_num(val + 1);
433 else
434 true_state = alloc_state_num(val);
435 break;
437 set_true_false_states_expr(my_size_id, str, true_state, false_state);
440 static struct expression *strip_ampersands(struct expression *expr)
442 struct symbol *type;
444 if (expr->type != EXPR_PREOP)
445 return expr;
446 if (expr->op != '&')
447 return expr;
448 type = get_type(expr->unop);
449 if (!type || type->type != SYM_ARRAY)
450 return expr;
451 return expr->unop;
454 static void match_array_assignment(struct expression *expr)
456 struct expression *left;
457 struct expression *right;
458 int array_size;
460 if (expr->op != '=')
461 return;
462 left = strip_expr(expr->left);
463 right = strip_expr(expr->right);
464 right = strip_ampersands(right);
465 array_size = get_array_size_bytes(right);
466 if (array_size)
467 set_state_expr(my_size_id, left, alloc_state_num(array_size));
470 static void info_record_alloction(struct expression *buffer, struct expression *size)
472 char *name;
473 long long val;
475 if (!option_info)
476 return;
478 name = get_member_name(buffer);
479 if (!name && is_static(buffer))
480 name = get_variable_from_expr(buffer, NULL);
481 if (!name)
482 return;
483 if (!get_implied_value(size, &val))
484 val = -1;
485 sm_msg("info: '%s' allocated_buf_size %lld", name, val);
486 free_string(name);
489 static void match_alloc(const char *fn, struct expression *expr, void *_size_arg)
491 int size_arg = PTR_INT(_size_arg);
492 struct expression *right;
493 struct expression *arg;
494 long long bytes;
496 right = strip_expr(expr->right);
497 arg = get_argument_from_call_expr(right->args, size_arg);
499 info_record_alloction(expr->left, arg);
501 if (!get_implied_value(arg, &bytes))
502 return;
503 set_state_expr(my_size_id, expr->left, alloc_state_num(bytes));
506 static void match_calloc(const char *fn, struct expression *expr, void *unused)
508 struct expression *right;
509 struct expression *arg;
510 long long elements;
511 long long size;
513 right = strip_expr(expr->right);
514 arg = get_argument_from_call_expr(right->args, 0);
515 if (!get_implied_value(arg, &elements))
516 return;
517 arg = get_argument_from_call_expr(right->args, 1);
518 if (!get_implied_value(arg, &size))
519 return;
520 set_state_expr(my_size_id, expr->left, alloc_state_num(elements * size));
523 static void match_strlen(const char *fn, struct expression *expr, void *unused)
525 struct expression *right;
526 struct expression *str;
527 struct expression *len_expr;
528 char *len_name;
529 struct smatch_state *state;
531 right = strip_expr(expr->right);
532 str = get_argument_from_call_expr(right->args, 0);
533 len_expr = strip_expr(expr->left);
535 len_name = get_variable_from_expr(len_expr, NULL);
536 if (!len_name)
537 return;
539 state = __alloc_smatch_state(0);
540 state->name = len_name;
541 state->data = len_expr;
542 set_state_expr(my_strlen_id, str, state);
545 static void match_limited(const char *fn, struct expression *expr, void *_limiter)
547 struct limiter *limiter = (struct limiter *)_limiter;
548 struct expression *dest;
549 struct expression *size_expr;
550 long long size;
552 dest = get_argument_from_call_expr(expr->args, limiter->buf_arg);
553 size_expr = get_argument_from_call_expr(expr->args, limiter->limit_arg);
554 if (!get_implied_max(size_expr, &size))
555 return;
556 set_state_expr(my_size_id, dest, alloc_state_num(size));
559 static void match_strcpy(const char *fn, struct expression *expr, void *unused)
561 struct expression fake_assign;
563 fake_assign.op = '=';
564 fake_assign.left = get_argument_from_call_expr(expr->args, 0);
565 fake_assign.right = get_argument_from_call_expr(expr->args, 1);
566 match_array_assignment(&fake_assign);
569 static void match_strndup(const char *fn, struct expression *expr, void *unused)
571 struct expression *fn_expr;
572 struct expression *size_expr;
573 long long size;
575 fn_expr = strip_expr(expr->right);
576 size_expr = get_argument_from_call_expr(fn_expr->args, 1);
577 if (!get_implied_max(size_expr, &size))
578 return;
580 /* It's easy to forget space for the NUL char */
581 size++;
582 set_state_expr(my_size_id, expr->left, alloc_state_num(size));
585 static void match_call(struct expression *expr)
587 char *name;
588 struct expression *arg;
589 int bytes;
590 int i;
592 name = get_fnptr_name(expr->fn);
593 if (!name)
594 return;
596 i = 0;
597 FOR_EACH_PTR(expr->args, arg) {
598 bytes = get_array_size_bytes(arg);
599 if (bytes > 1)
600 sm_msg("info: passes_buffer '%s' %d '$$' %d %s",
601 name, i, bytes,
602 is_static(expr->fn) ? "static" : "global");
603 i++;
604 } END_FOR_EACH_PTR(arg);
606 free_string(name);
609 static void struct_member_callback(char *fn, char *global_static, int param, char *printed_name, struct smatch_state *state)
611 if (state == &merged)
612 return;
613 sm_msg("info: passes_buffer '%s' %d '%s' %s %s", fn, param, printed_name, state->name, global_static);
616 static void match_func_end(struct symbol *sym)
618 memset(params_set, 0, sizeof(params_set));
621 void register_buf_size(int id)
623 my_size_id = id;
625 add_definition_db_callback(set_param_buf_size, BUF_SIZE);
627 add_function_assign_hook("malloc", &match_alloc, INT_PTR(0));
628 add_function_assign_hook("calloc", &match_calloc, NULL);
629 add_function_assign_hook("memdup", &match_alloc, INT_PTR(1));
630 if (option_project == PROJ_KERNEL) {
631 add_function_assign_hook("kmalloc", &match_alloc, INT_PTR(0));
632 add_function_assign_hook("kzalloc", &match_alloc, INT_PTR(0));
633 add_function_assign_hook("vmalloc", &match_alloc, INT_PTR(0));
634 add_function_assign_hook("__vmalloc", &match_alloc, INT_PTR(0));
635 add_function_assign_hook("kcalloc", &match_calloc, NULL);
636 add_function_assign_hook("kmalloc_array", &match_calloc, NULL);
637 add_function_assign_hook("drm_malloc_ab", &match_calloc, NULL);
638 add_function_assign_hook("drm_calloc_large", &match_calloc, NULL);
639 add_function_assign_hook("sock_kmalloc", &match_alloc, INT_PTR(1));
640 add_function_assign_hook("kmemdup", &match_alloc, INT_PTR(1));
641 add_function_assign_hook("kmemdup_user", &match_alloc, INT_PTR(1));
643 add_hook(&match_array_assignment, ASSIGNMENT_HOOK);
644 add_hook(&match_strlen_condition, CONDITION_HOOK);
645 add_function_assign_hook("strlen", &match_strlen, NULL);
647 add_function_assign_hook("strndup", match_strndup, NULL);
648 if (option_project == PROJ_KERNEL)
649 add_function_assign_hook("kstrndup", match_strndup, NULL);
651 add_hook(&match_func_end, END_FUNC_HOOK);
652 add_modification_hook(my_size_id, &set_undefined);
654 add_merge_hook(my_size_id, &merge_func);
657 void register_strlen(int id)
659 my_strlen_id = id;
660 add_modification_hook(my_strlen_id, &set_undefined);
661 add_merge_hook(my_strlen_id, &merge_func);
664 void register_buf_size_late(int id)
666 add_function_hook("strlcpy", &match_limited, &b0_l2);
667 add_function_hook("strlcat", &match_limited, &b0_l2);
668 add_function_hook("memscan", &match_limited, &b0_l2);
670 add_function_hook("strcpy", &match_strcpy, NULL);
672 if (option_info) {
673 add_hook(&match_call, FUNCTION_CALL_HOOK);
674 add_member_info_callback(my_size_id, struct_member_callback);