ranges: don't allow inverted ranges
[smatch.git] / smatch_buf_size.c
blob8fa541655fdb78b8cd09f607c4cd71eea801f8a5
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 = expr_to_var(expr);
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 void db_returns_buf_size(struct expression *expr, int param, char *unused, char *math)
178 struct expression *call;
179 sval_t sval;
181 if (expr->type != EXPR_ASSIGNMENT)
182 return;
183 call = strip_expr(expr->right);
185 if (!parse_call_math(call, math, &sval))
186 return;
187 set_state_expr(my_size_id, expr->left, alloc_state_num(sval.value));
190 static int get_real_array_size(struct expression *expr)
192 struct symbol *type;
193 sval_t sval;
195 if (expr->type == EXPR_BINOP) /* array elements foo[5] */
196 return 0;
198 type = get_type(expr);
199 if (!type || type->type != SYM_ARRAY)
200 return 0;
202 if (!get_implied_value(type->array_size, &sval))
203 return 0;
205 /* People put one element arrays on the end of structs */
206 if (sval.value == 1)
207 return 0;
209 return sval.value;
212 static int get_size_from_initializer(struct expression *expr)
214 if (expr->type != EXPR_SYMBOL || !expr->symbol->initializer)
215 return 0;
216 if (expr->symbol->initializer == expr) /* int a = a; */
217 return 0;
218 return get_initializer_size(expr->symbol->initializer);
221 static int get_stored_size_bytes(struct expression *expr)
223 struct smatch_state *state;
225 state = get_state_expr(my_size_id, expr);
226 if (!state)
227 return 0;
228 return PTR_INT(state->data);
231 static int get_stored_size_bytes_min(struct expression *expr)
233 struct sm_state *sm, *tmp;
234 int min = 0;
236 sm = get_sm_state_expr(my_size_id, expr);
237 if (!sm)
238 return 0;
239 FOR_EACH_PTR(sm->possible, tmp) {
240 if (PTR_INT(tmp->state->data) <= 0)
241 continue;
242 if (PTR_INT(tmp->state->data) < min)
243 min = PTR_INT(tmp->state->data);
244 } END_FOR_EACH_PTR(tmp);
246 return min;
249 static int get_bytes_from_address(struct expression *expr)
251 struct symbol *type;
252 int ret;
254 if (!option_spammy)
255 return 0;
256 if (expr->type != EXPR_PREOP || expr->op != '&')
257 return 0;
258 type = get_type(expr);
259 if (!type)
260 return 0;
262 if (type->type == SYM_PTR)
263 type = get_base_type(type);
265 ret = bits_to_bytes(type->bit_size);
266 if (ret == -1)
267 return 0;
268 if (ret == 1)
269 return 0; /* ignore char pointers */
271 return ret;
274 static int get_size_from_strlen(struct expression *expr)
276 struct smatch_state *state;
277 sval_t len;
279 state = get_state_expr(my_strlen_id, expr);
280 if (!state || !state->data)
281 return 0;
282 if (!get_implied_max((struct expression *)state->data, &len))
283 return 0;
284 if (sval_is_max(len))
285 return 0;
286 return len.value + 1; /* add one because strlen doesn't include the NULL */
289 static struct expression *remove_addr_fluff(struct expression *expr)
291 struct expression *tmp;
292 sval_t sval;
294 expr = strip_expr(expr);
296 /* remove '&' and '*' operations that cancel */
297 while (expr->type == EXPR_PREOP && expr->op == '&') {
298 tmp = strip_expr(expr->unop);
299 if (tmp->type != EXPR_PREOP)
300 break;
301 if (tmp->op != '*')
302 break;
303 expr = strip_expr(tmp->unop);
306 /* "foo + 0" is just "foo" */
307 if (expr->type == EXPR_BINOP && expr->op == '+' &&
308 get_value(expr->right, &sval) && sval.value == 0)
309 return expr->left;
311 return expr;
314 int get_array_size_bytes(struct expression *expr)
316 int size;
318 expr = remove_addr_fluff(expr);
319 if (!expr)
320 return 0;
322 /* strcpy(foo, "BAR"); */
323 if (expr->type == EXPR_STRING)
324 return expr->string->length;
326 /* buf[4] */
327 size = get_real_array_size(expr);
328 if (size)
329 return elements_to_bytes(expr, size);
331 /* buf = malloc(1024); */
332 size = get_stored_size_bytes(expr);
333 if (size)
334 return size;
336 /* char *foo = "BAR" */
337 size = get_size_from_initializer(expr);
338 if (size)
339 return elements_to_bytes(expr, size);
341 size = get_bytes_from_address(expr);
342 if (size)
343 return size;
345 /* if (strlen(foo) > 4) */
346 size = get_size_from_strlen(expr);
347 if (size)
348 return size;
350 return size_from_db(expr);
353 int get_array_size_bytes_min(struct expression *expr)
355 int size;
356 int tmp;
358 size = get_array_size_bytes(expr);
360 tmp = get_stored_size_bytes_min(expr);
361 if (size <= 0 || (tmp >= 1 && tmp < size))
362 size = tmp;
363 return size;
366 int get_array_size(struct expression *expr)
368 int bytes;
370 bytes = get_array_size_bytes(expr);
371 return bytes_to_elements(expr, bytes);
374 static void match_strlen_condition(struct expression *expr)
376 struct expression *left;
377 struct expression *right;
378 struct expression *str = NULL;
379 int strlen_left = 0;
380 int strlen_right = 0;
381 sval_t sval;
382 struct smatch_state *true_state = NULL;
383 struct smatch_state *false_state = NULL;
385 if (expr->type != EXPR_COMPARE)
386 return;
387 left = strip_expr(expr->left);
388 right = strip_expr(expr->right);
390 if (left->type == EXPR_CALL && sym_name_is("strlen", left->fn)) {
391 str = get_argument_from_call_expr(left->args, 0);
392 strlen_left = 1;
394 if (right->type == EXPR_CALL && sym_name_is("strlen", right->fn)) {
395 str = get_argument_from_call_expr(right->args, 0);
396 strlen_right = 1;
399 if (!strlen_left && !strlen_right)
400 return;
401 if (strlen_left && strlen_right)
402 return;
404 if (strlen_left) {
405 if (!get_value(right, &sval))
406 return;
408 if (strlen_right) {
409 if (!get_value(left, &sval))
410 return;
413 if (expr->op == SPECIAL_EQUAL) {
414 set_true_false_states_expr(my_size_id, str, alloc_state_num(sval.value + 1), NULL);
415 return;
417 if (expr->op == SPECIAL_NOTEQUAL) {
418 set_true_false_states_expr(my_size_id, str, NULL, alloc_state_num(sval.value + 1));
419 return;
422 switch (expr->op) {
423 case '<':
424 case SPECIAL_UNSIGNED_LT:
425 if (strlen_left)
426 true_state = alloc_state_num(sval.value);
427 else
428 false_state = alloc_state_num(sval.value + 1);
429 break;
430 case SPECIAL_LTE:
431 case SPECIAL_UNSIGNED_LTE:
432 if (strlen_left)
433 true_state = alloc_state_num(sval.value + 1);
434 else
435 false_state = alloc_state_num(sval.value);
436 break;
437 case SPECIAL_GTE:
438 case SPECIAL_UNSIGNED_GTE:
439 if (strlen_left)
440 false_state = alloc_state_num(sval.value);
441 else
442 true_state = alloc_state_num(sval.value + 1);
443 break;
444 case '>':
445 case SPECIAL_UNSIGNED_GT:
446 if (strlen_left)
447 false_state = alloc_state_num(sval.value + 1);
448 else
449 true_state = alloc_state_num(sval.value);
450 break;
452 set_true_false_states_expr(my_size_id, str, true_state, false_state);
455 static struct expression *strip_ampersands(struct expression *expr)
457 struct symbol *type;
459 if (expr->type != EXPR_PREOP)
460 return expr;
461 if (expr->op != '&')
462 return expr;
463 type = get_type(expr->unop);
464 if (!type || type->type != SYM_ARRAY)
465 return expr;
466 return expr->unop;
469 static void match_array_assignment(struct expression *expr)
471 struct expression *left;
472 struct expression *right;
473 int array_size;
475 if (expr->op != '=')
476 return;
477 left = strip_expr(expr->left);
478 right = strip_expr(expr->right);
479 right = strip_ampersands(right);
480 array_size = get_array_size_bytes(right);
481 if (array_size)
482 set_state_expr(my_size_id, left, alloc_state_num(array_size));
485 static void info_record_alloction(struct expression *buffer, struct expression *size)
487 char *name;
488 sval_t sval;
490 if (!option_info)
491 return;
493 name = get_member_name(buffer);
494 if (!name && is_static(buffer))
495 name = expr_to_var(buffer);
496 if (!name)
497 return;
498 if (get_implied_value(size, &sval))
499 sm_msg("info: '%s' allocated_buf_size %s", name, sval_to_str(sval));
500 else
501 sm_msg("info: '%s' allocated_buf_size -1", name);
502 free_string(name);
505 static void match_alloc(const char *fn, struct expression *expr, void *_size_arg)
507 int size_arg = PTR_INT(_size_arg);
508 struct expression *right;
509 struct expression *arg;
510 sval_t bytes;
512 right = strip_expr(expr->right);
513 arg = get_argument_from_call_expr(right->args, size_arg);
515 info_record_alloction(expr->left, arg);
517 if (!get_implied_value(arg, &bytes))
518 return;
519 set_state_expr(my_size_id, expr->left, alloc_state_num(bytes.value));
522 static void match_calloc(const char *fn, struct expression *expr, void *unused)
524 struct expression *right;
525 struct expression *arg;
526 sval_t elements;
527 sval_t size;
529 right = strip_expr(expr->right);
530 arg = get_argument_from_call_expr(right->args, 0);
531 if (!get_implied_value(arg, &elements))
532 return;
533 arg = get_argument_from_call_expr(right->args, 1);
534 if (!get_implied_value(arg, &size))
535 return;
536 set_state_expr(my_size_id, expr->left, alloc_state_num(elements.value * size.value));
539 static void match_strlen(const char *fn, struct expression *expr, void *unused)
541 struct expression *right;
542 struct expression *str;
543 struct expression *len_expr;
544 char *len_name;
545 struct smatch_state *state;
547 right = strip_expr(expr->right);
548 str = get_argument_from_call_expr(right->args, 0);
549 len_expr = strip_expr(expr->left);
551 len_name = expr_to_var(len_expr);
552 if (!len_name)
553 return;
555 state = __alloc_smatch_state(0);
556 state->name = len_name;
557 state->data = len_expr;
558 set_state_expr(my_strlen_id, str, state);
561 static void match_limited(const char *fn, struct expression *expr, void *_limiter)
563 struct limiter *limiter = (struct limiter *)_limiter;
564 struct expression *dest;
565 struct expression *size_expr;
566 sval_t size;
568 dest = get_argument_from_call_expr(expr->args, limiter->buf_arg);
569 size_expr = get_argument_from_call_expr(expr->args, limiter->limit_arg);
570 if (!get_implied_max(size_expr, &size))
571 return;
572 set_state_expr(my_size_id, dest, alloc_state_num(size.value));
575 static void match_strcpy(const char *fn, struct expression *expr, void *unused)
577 struct expression fake_assign;
579 fake_assign.op = '=';
580 fake_assign.left = get_argument_from_call_expr(expr->args, 0);
581 fake_assign.right = get_argument_from_call_expr(expr->args, 1);
582 match_array_assignment(&fake_assign);
585 static void match_strndup(const char *fn, struct expression *expr, void *unused)
587 struct expression *fn_expr;
588 struct expression *size_expr;
589 sval_t size;
591 fn_expr = strip_expr(expr->right);
592 size_expr = get_argument_from_call_expr(fn_expr->args, 1);
593 if (!get_implied_max(size_expr, &size))
594 return;
596 /* It's easy to forget space for the NUL char */
597 size.value++;
598 set_state_expr(my_size_id, expr->left, alloc_state_num(size.value));
601 static void match_call(struct expression *expr)
603 char *name;
604 struct expression *arg;
605 int bytes;
606 int i;
608 name = get_fnptr_name(expr->fn);
609 if (!name)
610 return;
612 i = 0;
613 FOR_EACH_PTR(expr->args, arg) {
614 bytes = get_array_size_bytes(arg);
615 if (bytes > 1)
616 sm_msg("info: passes_buffer '%s' %d '$$' %d %s",
617 name, i, bytes,
618 is_static(expr->fn) ? "static" : "global");
619 i++;
620 } END_FOR_EACH_PTR(arg);
622 free_string(name);
625 static void struct_member_callback(char *fn, char *global_static, int param, char *printed_name, struct smatch_state *state)
627 if (state == &merged)
628 return;
629 sm_msg("info: passes_buffer '%s' %d '%s' %s %s", fn, param, printed_name, state->name, global_static);
632 static void match_func_end(struct symbol *sym)
634 memset(params_set, 0, sizeof(params_set));
637 void register_buf_size(int id)
639 my_size_id = id;
641 add_definition_db_callback(set_param_buf_size, BUF_SIZE);
642 add_db_return_states_callback(BUF_SIZE, &db_returns_buf_size);
644 add_function_assign_hook("malloc", &match_alloc, INT_PTR(0));
645 add_function_assign_hook("calloc", &match_calloc, NULL);
646 add_function_assign_hook("memdup", &match_alloc, INT_PTR(1));
647 if (option_project == PROJ_KERNEL) {
648 add_function_assign_hook("kmalloc", &match_alloc, INT_PTR(0));
649 add_function_assign_hook("kzalloc", &match_alloc, INT_PTR(0));
650 add_function_assign_hook("vmalloc", &match_alloc, INT_PTR(0));
651 add_function_assign_hook("__vmalloc", &match_alloc, INT_PTR(0));
652 add_function_assign_hook("kcalloc", &match_calloc, NULL);
653 add_function_assign_hook("kmalloc_array", &match_calloc, NULL);
654 add_function_assign_hook("drm_malloc_ab", &match_calloc, NULL);
655 add_function_assign_hook("drm_calloc_large", &match_calloc, NULL);
656 add_function_assign_hook("sock_kmalloc", &match_alloc, INT_PTR(1));
657 add_function_assign_hook("kmemdup", &match_alloc, INT_PTR(1));
658 add_function_assign_hook("kmemdup_user", &match_alloc, INT_PTR(1));
660 add_hook(&match_array_assignment, ASSIGNMENT_HOOK);
661 add_hook(&match_strlen_condition, CONDITION_HOOK);
662 add_function_assign_hook("strlen", &match_strlen, NULL);
664 add_function_assign_hook("strndup", match_strndup, NULL);
665 if (option_project == PROJ_KERNEL)
666 add_function_assign_hook("kstrndup", match_strndup, NULL);
668 add_hook(&match_func_end, END_FUNC_HOOK);
669 add_modification_hook(my_size_id, &set_undefined);
671 add_merge_hook(my_size_id, &merge_func);
674 void register_strlen(int id)
676 my_strlen_id = id;
677 add_modification_hook(my_strlen_id, &set_undefined);
678 add_merge_hook(my_strlen_id, &merge_func);
681 void register_buf_size_late(int id)
683 add_function_hook("strlcpy", &match_limited, &b0_l2);
684 add_function_hook("strlcat", &match_limited, &b0_l2);
685 add_function_hook("memscan", &match_limited, &b0_l2);
687 add_function_hook("strcpy", &match_strcpy, NULL);
689 if (option_info) {
690 add_hook(&match_call, FUNCTION_CALL_HOOK);
691 add_member_info_callback(my_size_id, struct_member_callback);