2 * smatch/smatch_buf_size.c
4 * Copyright (C) 2010 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
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
;
30 static struct limiter b0_l2
= {0, 2};
32 static void set_undefined(struct sm_state
*sm
)
34 if (sm
->state
!= &undefined
)
35 set_state(sm
->owner
, sm
->name
, sm
->sym
, &undefined
);
38 static struct smatch_state
*merge_func(struct smatch_state
*s1
, struct smatch_state
*s2
)
40 if (PTR_INT(s1
->data
) == PTR_INT(s2
->data
))
45 void set_param_buf_size(const char *name
, struct symbol
*sym
, char *key
, char *value
)
50 if (strncmp(key
, "$$", 2))
53 snprintf(fullname
, 256, "%s%s", name
, key
+ 2);
56 size
= strtoul(value
, NULL
, 10);
60 set_state(my_size_id
, fullname
, sym
, alloc_state_num(size
));
63 static int bytes_per_element(struct expression
*expr
)
68 if (expr
->type
== EXPR_STRING
)
70 type
= get_type(expr
);
74 if (type
->type
!= SYM_PTR
&& type
->type
!= SYM_ARRAY
)
77 type
= get_base_type(type
);
78 bpe
= bits_to_bytes(type
->bit_size
);
80 if (bpe
== -1) /* void pointer */
86 static int bytes_to_elements(struct expression
*expr
, int bytes
)
90 bpe
= bytes_per_element(expr
);
96 static int elements_to_bytes(struct expression
*expr
, int elements
)
100 bpe
= bytes_per_element(expr
);
101 return elements
* bpe
;
104 static int get_initializer_size(struct expression
*expr
)
106 switch (expr
->type
) {
108 return expr
->string
->length
;
109 case EXPR_INITIALIZER
: {
110 struct expression
*tmp
;
114 FOR_EACH_PTR(expr
->expr_list
, tmp
) {
115 if (tmp
->type
== EXPR_INDEX
&& tmp
->idx_to
> max
)
118 } END_FOR_EACH_PTR(tmp
);
124 return get_array_size(expr
);
130 static int db_size_callback(void *unused
, int argc
, char **argv
, char **azColName
)
133 db_size
= atoi(argv
[0]);
139 static int size_from_db(struct expression
*expr
)
141 int this_file_only
= 0;
147 name
= get_member_name(expr
);
148 if (!name
&& is_static(expr
)) {
149 name
= expr_to_var(expr
);
156 run_sql(db_size_callback
, "select size from type_size where type = '%s' and file = '%s'",
157 name
, get_filename());
165 run_sql(db_size_callback
, "select size from type_size where type = '%s'",
174 static void db_returns_buf_size(struct expression
*expr
, int param
, char *unused
, char *math
)
176 struct expression
*call
;
179 if (expr
->type
!= EXPR_ASSIGNMENT
)
181 call
= strip_expr(expr
->right
);
183 if (!parse_call_math(call
, math
, &sval
))
185 set_state_expr(my_size_id
, expr
->left
, alloc_state_num(sval
.value
));
188 int get_real_array_size(struct expression
*expr
)
193 if (expr
->type
== EXPR_BINOP
) /* array elements foo[5] */
196 type
= get_type(expr
);
197 if (!type
|| type
->type
!= SYM_ARRAY
)
200 if (!get_implied_value(type
->array_size
, &sval
))
203 /* People put one element arrays on the end of structs */
210 static int get_size_from_initializer(struct expression
*expr
)
212 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
|| !expr
->symbol
->initializer
)
214 if (expr
->symbol
->initializer
== expr
) /* int a = a; */
216 return get_initializer_size(expr
->symbol
->initializer
);
219 static int get_stored_size_bytes(struct expression
*expr
)
221 struct smatch_state
*state
;
223 state
= get_state_expr(my_size_id
, expr
);
226 return PTR_INT(state
->data
);
229 static int get_stored_size_bytes_min(struct expression
*expr
)
231 struct sm_state
*sm
, *tmp
;
234 sm
= get_sm_state_expr(my_size_id
, expr
);
237 FOR_EACH_PTR(sm
->possible
, tmp
) {
238 if (PTR_INT(tmp
->state
->data
) <= 0)
240 if (PTR_INT(tmp
->state
->data
) < min
)
241 min
= PTR_INT(tmp
->state
->data
);
242 } END_FOR_EACH_PTR(tmp
);
247 static int get_bytes_from_address(struct expression
*expr
)
254 if (expr
->type
!= EXPR_PREOP
|| expr
->op
!= '&')
256 type
= get_type(expr
);
260 if (type
->type
== SYM_PTR
)
261 type
= get_base_type(type
);
263 ret
= bits_to_bytes(type
->bit_size
);
267 return 0; /* ignore char pointers */
272 static int get_size_from_strlen(struct expression
*expr
)
274 struct smatch_state
*state
;
277 state
= get_state_expr(my_strlen_id
, expr
);
278 if (!state
|| !state
->data
)
280 if (!get_implied_max((struct expression
*)state
->data
, &len
))
282 if (sval_is_max(len
))
284 return len
.value
+ 1; /* add one because strlen doesn't include the NULL */
287 static struct expression
*remove_addr_fluff(struct expression
*expr
)
289 struct expression
*tmp
;
292 expr
= strip_expr(expr
);
294 /* remove '&' and '*' operations that cancel */
295 while (expr
&& expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
296 tmp
= strip_expr(expr
->unop
);
297 if (tmp
->type
!= EXPR_PREOP
)
301 expr
= strip_expr(tmp
->unop
);
307 /* "foo + 0" is just "foo" */
308 if (expr
->type
== EXPR_BINOP
&& expr
->op
== '+' &&
309 get_value(expr
->right
, &sval
) && sval
.value
== 0)
315 static int is_last_member_of_struct(struct symbol
*sym
, struct ident
*member
)
321 FOR_EACH_PTR_REVERSE(sym
->symbol_list
, tmp
) {
322 if (i
++ || !tmp
->ident
)
324 if (tmp
->ident
== member
)
327 } END_FOR_EACH_PTR_REVERSE(tmp
);
332 static int get_stored_size_end_struct_bytes(struct expression
*expr
)
337 struct smatch_state
*state
;
340 if (expr
->type
== EXPR_BINOP
) /* array elements foo[5] */
343 type
= get_type(expr
);
344 if (!type
|| type
->type
!= SYM_ARRAY
)
347 if (!get_implied_value(type
->array_size
, &sval
))
350 if (sval
.value
!= 0 && sval
.value
!= 1)
353 name
= expr_to_var_sym(expr
, &sym
);
355 if (!sym
|| !sym
->ident
|| !sym
->ident
->name
)
360 if (sym
->type
!= SYM_NODE
)
363 state
= get_state(my_size_id
, sym
->ident
->name
, sym
);
364 if (!state
|| !state
->data
)
367 sym
= get_real_base_type(sym
);
368 if (!sym
|| sym
->type
!= SYM_PTR
)
370 sym
= get_real_base_type(sym
);
371 if (!sym
|| sym
->type
!= SYM_STRUCT
)
373 if (!is_last_member_of_struct(sym
, expr
->member
))
376 return PTR_INT(state
->data
) - bits_to_bytes(sym
->bit_size
) +
377 bits_to_bytes(type
->bit_size
);
380 int get_array_size_bytes(struct expression
*expr
)
384 expr
= remove_addr_fluff(expr
);
388 /* strcpy(foo, "BAR"); */
389 if (expr
->type
== EXPR_STRING
)
390 return expr
->string
->length
;
393 size
= get_real_array_size(expr
);
395 return elements_to_bytes(expr
, size
);
397 /* buf = malloc(1024); */
398 size
= get_stored_size_bytes(expr
);
402 size
= get_stored_size_end_struct_bytes(expr
);
406 /* char *foo = "BAR" */
407 size
= get_size_from_initializer(expr
);
409 return elements_to_bytes(expr
, size
);
411 size
= get_bytes_from_address(expr
);
415 /* if (strlen(foo) > 4) */
416 size
= get_size_from_strlen(expr
);
420 return size_from_db(expr
);
423 int get_array_size_bytes_min(struct expression
*expr
)
428 size
= get_array_size_bytes(expr
);
430 tmp
= get_stored_size_bytes_min(expr
);
431 if (size
<= 0 || (tmp
>= 1 && tmp
< size
))
436 int get_array_size(struct expression
*expr
)
440 bytes
= get_array_size_bytes(expr
);
441 return bytes_to_elements(expr
, bytes
);
444 static void match_strlen_condition(struct expression
*expr
)
446 struct expression
*left
;
447 struct expression
*right
;
448 struct expression
*str
= NULL
;
450 int strlen_right
= 0;
452 struct smatch_state
*true_state
= NULL
;
453 struct smatch_state
*false_state
= NULL
;
455 if (expr
->type
!= EXPR_COMPARE
)
457 left
= strip_expr(expr
->left
);
458 right
= strip_expr(expr
->right
);
460 if (left
->type
== EXPR_CALL
&& sym_name_is("strlen", left
->fn
)) {
461 str
= get_argument_from_call_expr(left
->args
, 0);
464 if (right
->type
== EXPR_CALL
&& sym_name_is("strlen", right
->fn
)) {
465 str
= get_argument_from_call_expr(right
->args
, 0);
469 if (!strlen_left
&& !strlen_right
)
471 if (strlen_left
&& strlen_right
)
475 if (!get_value(right
, &sval
))
479 if (!get_value(left
, &sval
))
483 if (expr
->op
== SPECIAL_EQUAL
) {
484 set_true_false_states_expr(my_size_id
, str
, alloc_state_num(sval
.value
+ 1), NULL
);
487 if (expr
->op
== SPECIAL_NOTEQUAL
) {
488 set_true_false_states_expr(my_size_id
, str
, NULL
, alloc_state_num(sval
.value
+ 1));
494 case SPECIAL_UNSIGNED_LT
:
496 true_state
= alloc_state_num(sval
.value
);
498 false_state
= alloc_state_num(sval
.value
+ 1);
501 case SPECIAL_UNSIGNED_LTE
:
503 true_state
= alloc_state_num(sval
.value
+ 1);
505 false_state
= alloc_state_num(sval
.value
);
508 case SPECIAL_UNSIGNED_GTE
:
510 false_state
= alloc_state_num(sval
.value
);
512 true_state
= alloc_state_num(sval
.value
+ 1);
515 case SPECIAL_UNSIGNED_GT
:
517 false_state
= alloc_state_num(sval
.value
+ 1);
519 true_state
= alloc_state_num(sval
.value
);
522 set_true_false_states_expr(my_size_id
, str
, true_state
, false_state
);
525 static struct expression
*strip_ampersands(struct expression
*expr
)
529 if (expr
->type
!= EXPR_PREOP
)
533 type
= get_type(expr
->unop
);
534 if (!type
|| type
->type
!= SYM_ARRAY
)
539 static void match_array_assignment(struct expression
*expr
)
541 struct expression
*left
;
542 struct expression
*right
;
547 left
= strip_expr(expr
->left
);
548 right
= strip_expr(expr
->right
);
549 right
= strip_ampersands(right
);
550 array_size
= get_array_size_bytes(right
);
552 set_state_expr(my_size_id
, left
, alloc_state_num(array_size
));
555 static void info_record_alloction(struct expression
*buffer
, struct expression
*size
)
563 name
= get_member_name(buffer
);
564 if (!name
&& is_static(buffer
))
565 name
= expr_to_var(buffer
);
568 if (get_implied_value(size
, &sval
))
569 sql_insert_type_size(name
, sval
.value
);
571 sql_insert_type_size(name
, -1);
576 static void match_alloc(const char *fn
, struct expression
*expr
, void *_size_arg
)
578 int size_arg
= PTR_INT(_size_arg
);
579 struct expression
*right
;
580 struct expression
*arg
;
583 right
= strip_expr(expr
->right
);
584 arg
= get_argument_from_call_expr(right
->args
, size_arg
);
586 info_record_alloction(expr
->left
, arg
);
588 if (!get_implied_value(arg
, &bytes
))
590 set_state_expr(my_size_id
, expr
->left
, alloc_state_num(bytes
.value
));
593 static void match_calloc(const char *fn
, struct expression
*expr
, void *unused
)
595 struct expression
*right
;
596 struct expression
*arg
;
600 right
= strip_expr(expr
->right
);
601 arg
= get_argument_from_call_expr(right
->args
, 0);
602 if (!get_implied_value(arg
, &elements
))
604 arg
= get_argument_from_call_expr(right
->args
, 1);
605 if (!get_implied_value(arg
, &size
))
607 set_state_expr(my_size_id
, expr
->left
, alloc_state_num(elements
.value
* size
.value
));
610 static void match_strlen(const char *fn
, struct expression
*expr
, void *unused
)
612 struct expression
*right
;
613 struct expression
*str
;
614 struct expression
*len_expr
;
616 struct smatch_state
*state
;
618 right
= strip_expr(expr
->right
);
619 str
= get_argument_from_call_expr(right
->args
, 0);
620 len_expr
= strip_expr(expr
->left
);
622 len_name
= expr_to_var(len_expr
);
626 state
= __alloc_smatch_state(0);
627 state
->name
= len_name
;
628 state
->data
= len_expr
;
629 set_state_expr(my_strlen_id
, str
, state
);
632 static void match_limited(const char *fn
, struct expression
*expr
, void *_limiter
)
634 struct limiter
*limiter
= (struct limiter
*)_limiter
;
635 struct expression
*dest
;
636 struct expression
*size_expr
;
639 dest
= get_argument_from_call_expr(expr
->args
, limiter
->buf_arg
);
640 size_expr
= get_argument_from_call_expr(expr
->args
, limiter
->limit_arg
);
641 if (!get_implied_max(size_expr
, &size
))
643 set_state_expr(my_size_id
, dest
, alloc_state_num(size
.value
));
646 static void match_strcpy(const char *fn
, struct expression
*expr
, void *unused
)
648 struct expression fake_assign
;
650 fake_assign
.op
= '=';
651 fake_assign
.left
= get_argument_from_call_expr(expr
->args
, 0);
652 fake_assign
.right
= get_argument_from_call_expr(expr
->args
, 1);
653 match_array_assignment(&fake_assign
);
656 static void match_strndup(const char *fn
, struct expression
*expr
, void *unused
)
658 struct expression
*fn_expr
;
659 struct expression
*size_expr
;
662 fn_expr
= strip_expr(expr
->right
);
663 size_expr
= get_argument_from_call_expr(fn_expr
->args
, 1);
664 if (!get_implied_max(size_expr
, &size
))
667 /* It's easy to forget space for the NUL char */
669 set_state_expr(my_size_id
, expr
->left
, alloc_state_num(size
.value
));
672 static void match_call(struct expression
*expr
)
674 struct expression
*arg
;
679 FOR_EACH_PTR(expr
->args
, arg
) {
680 bytes
= get_array_size_bytes(arg
);
684 snprintf(buf
, sizeof(buf
), "%d", bytes
);
685 sql_insert_caller_info(expr
, BUF_SIZE
, i
, "$$", buf
);
688 } END_FOR_EACH_PTR(arg
);
691 static void struct_member_callback(struct expression
*call
, int param
, char *printed_name
, struct smatch_state
*state
)
693 if (state
== &merged
)
695 sql_insert_caller_info(call
, BUF_SIZE
, param
, printed_name
, state
->name
);
698 void register_buf_size(int id
)
702 add_definition_db_callback(set_param_buf_size
, BUF_SIZE
);
703 add_db_return_states_callback(BUF_SIZE
, &db_returns_buf_size
);
705 add_function_assign_hook("malloc", &match_alloc
, INT_PTR(0));
706 add_function_assign_hook("calloc", &match_calloc
, NULL
);
707 add_function_assign_hook("memdup", &match_alloc
, INT_PTR(1));
708 if (option_project
== PROJ_KERNEL
) {
709 add_function_assign_hook("kmalloc", &match_alloc
, INT_PTR(0));
710 add_function_assign_hook("kzalloc", &match_alloc
, INT_PTR(0));
711 add_function_assign_hook("vmalloc", &match_alloc
, INT_PTR(0));
712 add_function_assign_hook("__vmalloc", &match_alloc
, INT_PTR(0));
713 add_function_assign_hook("kcalloc", &match_calloc
, NULL
);
714 add_function_assign_hook("kmalloc_array", &match_calloc
, NULL
);
715 add_function_assign_hook("drm_malloc_ab", &match_calloc
, NULL
);
716 add_function_assign_hook("drm_calloc_large", &match_calloc
, NULL
);
717 add_function_assign_hook("sock_kmalloc", &match_alloc
, INT_PTR(1));
718 add_function_assign_hook("kmemdup", &match_alloc
, INT_PTR(1));
719 add_function_assign_hook("kmemdup_user", &match_alloc
, INT_PTR(1));
721 add_hook(&match_array_assignment
, ASSIGNMENT_HOOK
);
722 add_hook(&match_strlen_condition
, CONDITION_HOOK
);
723 add_function_assign_hook("strlen", &match_strlen
, NULL
);
725 add_function_assign_hook("strndup", match_strndup
, NULL
);
726 if (option_project
== PROJ_KERNEL
)
727 add_function_assign_hook("kstrndup", match_strndup
, NULL
);
729 add_modification_hook(my_size_id
, &set_undefined
);
731 add_merge_hook(my_size_id
, &merge_func
);
734 void register_strlen(int id
)
737 add_modification_hook(my_strlen_id
, &set_undefined
);
738 add_merge_hook(my_strlen_id
, &merge_func
);
741 void register_buf_size_late(int id
)
743 add_function_hook("strlcpy", &match_limited
, &b0_l2
);
744 add_function_hook("strlcat", &match_limited
, &b0_l2
);
745 add_function_hook("memscan", &match_limited
, &b0_l2
);
747 add_function_hook("strcpy", &match_strcpy
, NULL
);
749 add_hook(&match_call
, FUNCTION_CALL_HOOK
);
750 add_member_info_callback(my_size_id
, struct_member_callback
);