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 _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
)
45 if (strncmp(key
, "$$", 2))
48 snprintf(fullname
, 256, "%s%s", name
, key
+ 2);
51 size
= strtoul(value
, NULL
, 10);
55 set_state(my_size_id
, fullname
, sym
, alloc_state_num(size
));
58 static int bytes_to_elements(struct expression
*expr
, int bytes
)
62 type
= get_type(expr
);
65 if (type
->type
== SYM_PTR
)
66 type
= get_base_type(type
);
68 if (bits_to_bytes(type
->bit_size
) == 0)
71 return bytes
/ bits_to_bytes(type
->bit_size
);
74 static int get_initializer_size(struct expression
*expr
)
78 return expr
->string
->length
;
79 case EXPR_INITIALIZER
: {
80 struct expression
*tmp
;
84 FOR_EACH_PTR(expr
->expr_list
, tmp
) {
85 if (tmp
->type
== EXPR_INDEX
&& tmp
->idx_to
> max
)
88 } END_FOR_EACH_PTR(tmp
);
94 return get_array_size(expr
);
99 static float get_cast_ratio(struct expression
*unstripped
)
101 struct expression
*start_expr
;
102 struct symbol
*start_type
;
103 struct symbol
*end_type
;
107 start_expr
= strip_expr(unstripped
);
108 start_type
= get_type(start_expr
);
109 end_type
= get_type(unstripped
);
110 if (!start_type
|| !end_type
)
113 if (start_type
->type
== SYM_PTR
)
114 start_bytes
= (get_base_type(start_type
))->ctype
.alignment
;
115 if (start_type
->type
== SYM_ARRAY
)
116 start_bytes
= (get_base_type(start_type
))->bit_size
/ 8;
117 if (end_type
->type
== SYM_PTR
)
118 end_bytes
= (get_base_type(end_type
))->ctype
.alignment
;
119 if (end_type
->type
== SYM_ARRAY
)
120 end_bytes
= (get_base_type(end_type
))->bit_size
/ 8;
122 if (!start_bytes
|| !end_bytes
)
124 return start_bytes
/ end_bytes
;
128 static int db_size_callback(void *unused
, int argc
, char **argv
, char **azColName
)
130 db_size
= atoi(argv
[0]);
134 static int size_from_db(struct expression
*expr
)
141 name
= get_member_name(expr
);
146 run_sql(db_size_callback
, "select size from type_size where type = '%s'",
151 static int get_real_array_size(struct expression
*expr
)
157 type
= get_type(expr
);
158 if (!type
|| type
->type
!= SYM_ARRAY
)
161 cast_ratio
= get_cast_ratio(expr
);
163 ret
= get_expression_value(type
->array_size
);
164 /* Dynamically sized array are -1 in sparse */
167 /* People put one element arrays on the end of structs */
171 return ret
* cast_ratio
;
174 static int get_size_from_initializer(struct expression
*expr
)
178 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
->initializer
)
180 if (expr
->symbol
->initializer
== expr
) /* int a = a; */
182 cast_ratio
= get_cast_ratio(expr
);
183 return get_initializer_size(expr
->symbol
->initializer
) * cast_ratio
;
186 static int get_stored_size(struct expression
*expr
)
188 struct sm_state
*sm
, *tmp
;
193 cast_ratio
= get_cast_ratio(expr
);
195 sm
= get_sm_state_expr(my_size_id
, expr
);
199 FOR_EACH_PTR(sm
->possible
, tmp
) {
200 if (PTR_INT(tmp
->state
->data
) > max
)
201 max
= PTR_INT(tmp
->state
->data
);
202 } END_FOR_EACH_PTR(tmp
);
204 ret
= bytes_to_elements(expr
, max
);
205 return ret
* cast_ratio
;
208 static int get_size_from_strlen(struct expression
*expr
)
210 struct smatch_state
*state
;
213 state
= get_state_expr(my_strlen_id
, expr
);
214 if (!state
|| !state
->data
)
216 if (get_implied_max((struct expression
*)state
->data
, &len
))
217 return len
+ 1; /* add one because strlen doesn't include the NULL */
221 int get_array_size(struct expression
*expr
)
225 expr
= strip_expr(expr
);
229 /* strcpy(foo, "BAR"); */
230 if (expr
->type
== EXPR_STRING
)
231 return expr
->string
->length
;
234 ret
= get_real_array_size(expr
);
238 /* buf = malloc(1024); */
239 ret
= get_stored_size(expr
);
243 /* char *foo = "BAR" */
244 ret
= get_size_from_initializer(expr
);
248 /* if (strlen(foo) > 4) */
249 ret
= get_size_from_strlen(expr
);
255 int get_array_size_bytes(struct expression
*expr
)
264 if (expr
->type
== EXPR_STRING
)
265 return expr
->string
->length
;
267 tmp
= get_type(expr
);
271 if (tmp
->type
== SYM_ARRAY
) {
272 tmp
= get_base_type(tmp
);
273 element_size
= tmp
->bit_size
/ 8;
274 } else if (tmp
->type
== SYM_PTR
) {
275 tmp
= get_base_type(tmp
);
276 element_size
= bits_to_bytes(tmp
->bit_size
);
280 array_size
= get_array_size(expr
);
282 return array_size
* element_size
;
284 return size_from_db(expr
);
287 static void match_strlen_condition(struct expression
*expr
)
289 struct expression
*left
;
290 struct expression
*right
;
291 struct expression
*str
= NULL
;
293 int strlen_right
= 0;
295 struct smatch_state
*true_state
= NULL
;
296 struct smatch_state
*false_state
= NULL
;
298 if (expr
->type
!= EXPR_COMPARE
)
300 left
= strip_expr(expr
->left
);
301 right
= strip_expr(expr
->right
);
303 if (left
->type
== EXPR_CALL
&& sym_name_is("strlen", left
->fn
)) {
304 str
= get_argument_from_call_expr(left
->args
, 0);
307 if (right
->type
== EXPR_CALL
&& sym_name_is("strlen", right
->fn
)) {
308 str
= get_argument_from_call_expr(right
->args
, 0);
312 if (!strlen_left
&& !strlen_right
)
314 if (strlen_left
&& strlen_right
)
318 if (!get_value(right
, &val
))
322 if (!get_value(left
, &val
))
326 if (expr
->op
== SPECIAL_EQUAL
) {
327 set_true_false_states_expr(my_size_id
, str
, alloc_state_num(val
+ 1), NULL
);
330 if (expr
->op
== SPECIAL_NOTEQUAL
) {
331 set_true_false_states_expr(my_size_id
, str
, NULL
, alloc_state_num(val
+ 1));
337 case SPECIAL_UNSIGNED_LT
:
339 true_state
= alloc_state_num(val
);
341 false_state
= alloc_state_num(val
+ 1);
344 case SPECIAL_UNSIGNED_LTE
:
346 true_state
= alloc_state_num(val
+ 1);
348 false_state
= alloc_state_num(val
);
351 case SPECIAL_UNSIGNED_GTE
:
353 false_state
= alloc_state_num(val
);
355 true_state
= alloc_state_num(val
+ 1);
358 case SPECIAL_UNSIGNED_GT
:
360 false_state
= alloc_state_num(val
+ 1);
362 true_state
= alloc_state_num(val
);
365 set_true_false_states_expr(my_size_id
, str
, true_state
, false_state
);
368 static struct expression
*strip_ampersands(struct expression
*expr
)
372 if (expr
->type
!= EXPR_PREOP
)
376 type
= get_type(expr
->unop
);
377 if (!type
|| type
->type
!= SYM_ARRAY
)
382 static void match_array_assignment(struct expression
*expr
)
384 struct expression
*left
;
385 struct expression
*right
;
390 left
= strip_expr(expr
->left
);
391 right
= strip_expr(expr
->right
);
392 right
= strip_ampersands(right
);
393 array_size
= get_array_size_bytes(right
);
395 set_state_expr(my_size_id
, left
, alloc_state_num(array_size
));
398 static void match_alloc(const char *fn
, struct expression
*expr
, void *_size_arg
)
400 int size_arg
= PTR_INT(_size_arg
);
401 struct expression
*right
;
402 struct expression
*arg
;
405 right
= strip_expr(expr
->right
);
406 arg
= get_argument_from_call_expr(right
->args
, size_arg
);
407 if (!get_implied_value(arg
, &bytes
))
410 set_state_expr(my_size_id
, expr
->left
, alloc_state_num(bytes
));
413 char *member
= get_member_name(expr
->left
);
416 sm_msg("info: '%s' allocated_buf_size %lld",
422 static void match_calloc(const char *fn
, struct expression
*expr
, void *unused
)
424 struct expression
*right
;
425 struct expression
*arg
;
429 right
= strip_expr(expr
->right
);
430 arg
= get_argument_from_call_expr(right
->args
, 0);
431 if (!get_implied_value(arg
, &elements
))
433 arg
= get_argument_from_call_expr(right
->args
, 1);
434 if (!get_implied_value(arg
, &size
))
436 set_state_expr(my_size_id
, expr
->left
, alloc_state_num(elements
* size
));
439 static void match_strlen(const char *fn
, struct expression
*expr
, void *unused
)
441 struct expression
*right
;
442 struct expression
*str
;
443 struct expression
*len_expr
;
445 struct smatch_state
*state
;
447 right
= strip_expr(expr
->right
);
448 str
= get_argument_from_call_expr(right
->args
, 0);
449 len_expr
= strip_expr(expr
->left
);
451 len_name
= get_variable_from_expr(len_expr
, NULL
);
455 state
= __alloc_smatch_state(0);
456 state
->name
= len_name
;
457 state
->data
= len_expr
;
458 set_state_expr(my_strlen_id
, str
, state
);
461 static void match_limited(const char *fn
, struct expression
*expr
, void *_limiter
)
463 struct limiter
*limiter
= (struct limiter
*)_limiter
;
464 struct expression
*dest
;
465 struct expression
*size_expr
;
468 dest
= get_argument_from_call_expr(expr
->args
, limiter
->buf_arg
);
469 size_expr
= get_argument_from_call_expr(expr
->args
, limiter
->limit_arg
);
470 if (!get_implied_max(size_expr
, &size
))
472 set_state_expr(my_size_id
, dest
, alloc_state_num(size
));
475 static void match_strcpy(const char *fn
, struct expression
*expr
, void *unused
)
477 struct expression fake_assign
;
479 fake_assign
.op
= '=';
480 fake_assign
.left
= get_argument_from_call_expr(expr
->args
, 0);
481 fake_assign
.right
= get_argument_from_call_expr(expr
->args
, 1);
482 match_array_assignment(&fake_assign
);
485 static void match_strndup(const char *fn
, struct expression
*expr
, void *unused
)
487 struct expression
*fn_expr
;
488 struct expression
*size_expr
;
491 fn_expr
= strip_expr(expr
->right
);
492 size_expr
= get_argument_from_call_expr(fn_expr
->args
, 1);
493 if (!get_implied_max(size_expr
, &size
))
496 /* It's easy to forget space for the NUL char */
498 set_state_expr(my_size_id
, expr
->left
, alloc_state_num(size
));
501 static void match_call(struct expression
*expr
)
504 struct expression
*arg
;
508 name
= get_fnptr_name(expr
->fn
);
513 FOR_EACH_PTR(expr
->args
, arg
) {
514 bytes
= get_array_size_bytes(arg
);
516 sm_msg("info: passes_buffer '%s' %d '$$' %d %s",
518 is_static(expr
->fn
) ? "static" : "global");
520 } END_FOR_EACH_PTR(arg
);
525 static void struct_member_callback(char *fn
, char *global_static
, int param
, char *printed_name
, struct smatch_state
*state
)
527 if (state
== &merged
)
529 sm_msg("info: passes_buffer '%s' %d '%s' %s %s", fn
, param
, printed_name
, state
->name
, global_static
);
532 static void match_func_end(struct symbol
*sym
)
534 memset(params_set
, 0, sizeof(params_set
));
537 void register_buf_size(int id
)
541 add_definition_db_callback(set_param_buf_size
, BUF_SIZE
);
543 add_function_assign_hook("malloc", &match_alloc
, INT_PTR(0));
544 add_function_assign_hook("calloc", &match_calloc
, NULL
);
545 add_function_assign_hook("memdup", &match_alloc
, INT_PTR(1));
546 if (option_project
== PROJ_KERNEL
) {
547 add_function_assign_hook("kmalloc", &match_alloc
, INT_PTR(0));
548 add_function_assign_hook("kzalloc", &match_alloc
, INT_PTR(0));
549 add_function_assign_hook("vmalloc", &match_alloc
, INT_PTR(0));
550 add_function_assign_hook("__vmalloc", &match_alloc
, INT_PTR(0));
551 add_function_assign_hook("kcalloc", &match_calloc
, NULL
);
552 add_function_assign_hook("drm_malloc_ab", &match_calloc
, NULL
);
553 add_function_assign_hook("drm_calloc_large", &match_calloc
, NULL
);
554 add_function_assign_hook("kmemdup", &match_alloc
, INT_PTR(1));
555 add_function_assign_hook("kmemdup_user", &match_alloc
, INT_PTR(1));
557 add_hook(&match_array_assignment
, ASSIGNMENT_HOOK
);
558 add_hook(&match_strlen_condition
, CONDITION_HOOK
);
559 add_function_assign_hook("strlen", &match_strlen
, NULL
);
561 add_function_hook("strlcpy", &match_limited
, &b0_l2
);
562 add_function_hook("strlcat", &match_limited
, &b0_l2
);
563 add_function_hook("memscan", &match_limited
, &b0_l2
);
565 add_function_hook("strcpy", &match_strcpy
, NULL
);
567 add_function_assign_hook("strndup", match_strndup
, NULL
);
568 if (option_project
== PROJ_KERNEL
)
569 add_function_assign_hook("kstrndup", match_strndup
, NULL
);
572 add_hook(&match_call
, FUNCTION_CALL_HOOK
);
573 add_member_info_callback(my_size_id
, struct_member_callback
);
576 add_hook(&match_func_end
, END_FUNC_HOOK
);
577 add_modification_hook(my_size_id
, &set_undefined
);
580 void register_strlen(int id
)
583 add_modification_hook(my_strlen_id
, &set_undefined
);