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 void set_param_buf_size(const char *name
, struct symbol
*sym
, char *key
, char *value
)
39 if (strncmp(key
, "$$", 2))
42 snprintf(fullname
, 256, "%s%s", name
, key
+ 2);
45 size
= strtoul(value
, NULL
, 10);
49 set_state(my_size_id
, fullname
, sym
, alloc_state_num(size
));
52 static int get_initializer_size(struct expression
*expr
)
56 return expr
->string
->length
;
57 case EXPR_INITIALIZER
: {
58 struct expression
*tmp
;
62 FOR_EACH_PTR(expr
->expr_list
, tmp
) {
63 if (tmp
->type
== EXPR_INDEX
&& tmp
->idx_to
> max
)
66 } END_FOR_EACH_PTR(tmp
);
72 return get_array_size(expr
);
77 static float get_cast_ratio(struct expression
*unstripped
)
79 struct expression
*start_expr
;
80 struct symbol
*start_type
;
81 struct symbol
*end_type
;
85 start_expr
= strip_expr(unstripped
);
86 start_type
= get_type(start_expr
);
87 end_type
= get_type(unstripped
);
88 if (!start_type
|| !end_type
)
91 if (start_type
->type
== SYM_PTR
)
92 start_bytes
= (get_base_type(start_type
))->ctype
.alignment
;
93 if (start_type
->type
== SYM_ARRAY
)
94 start_bytes
= (get_base_type(start_type
))->bit_size
/ 8;
95 if (end_type
->type
== SYM_PTR
)
96 end_bytes
= (get_base_type(end_type
))->ctype
.alignment
;
97 if (end_type
->type
== SYM_ARRAY
)
98 end_bytes
= (get_base_type(end_type
))->bit_size
/ 8;
100 if (!start_bytes
|| !end_bytes
)
102 return start_bytes
/ end_bytes
;
105 int get_array_size(struct expression
*expr
)
108 struct smatch_state
*state
;
113 if (expr
->type
== EXPR_STRING
)
114 return expr
->string
->length
;
116 cast_ratio
= get_cast_ratio(expr
);
117 expr
= strip_expr(expr
);
118 tmp
= get_type(expr
);
122 if (tmp
->type
== SYM_ARRAY
) {
123 ret
= get_expression_value(tmp
->array_size
);
124 /* Dynamically sized array are -1 in sparse */
127 /* People put one element arrays on the end of structs */
131 return ret
* cast_ratio
;
134 state
= get_state_expr(my_size_id
, expr
);
135 if (state
== &merged
)
137 if (state
&& state
->data
) {
138 if (tmp
->type
== SYM_PTR
)
139 tmp
= get_base_type(tmp
);
140 if (!tmp
->ctype
.alignment
)
142 ret
= PTR_INT(state
->data
) / tmp
->ctype
.alignment
;
143 return ret
* cast_ratio
;
146 if (expr
->type
== EXPR_SYMBOL
&& expr
->symbol
->initializer
) {
147 if (expr
->symbol
->initializer
!= expr
) /* int a = a; */
148 return get_initializer_size(expr
->symbol
->initializer
) * cast_ratio
;
151 state
= get_state_expr(my_strlen_id
, expr
);
152 if (!state
|| !state
->data
)
154 if (get_implied_max((struct expression
*)state
->data
, &len
))
155 return len
+ 1; /* add one because strlen doesn't include the NULL */
160 static int db_size_callback(void *unused
, int argc
, char **argv
, char **azColName
)
162 db_size
= atoi(argv
[0]);
166 static int size_from_db(struct expression
*expr
)
173 name
= get_member_name(expr
);
178 run_sql(db_size_callback
, "select size from type_size where type = '%s'",
183 int get_array_size_bytes(struct expression
*expr
)
192 if (expr
->type
== EXPR_STRING
)
193 return expr
->string
->length
;
195 tmp
= get_type(expr
);
199 if (tmp
->type
== SYM_ARRAY
) {
200 tmp
= get_base_type(tmp
);
201 element_size
= tmp
->bit_size
/ 8;
202 } else if (tmp
->type
== SYM_PTR
) {
203 tmp
= get_base_type(tmp
);
204 element_size
= tmp
->ctype
.alignment
;
208 array_size
= get_array_size(expr
);
210 return array_size
* element_size
;
212 return size_from_db(expr
);
215 static void match_strlen_condition(struct expression
*expr
)
217 struct expression
*left
;
218 struct expression
*right
;
219 struct expression
*str
= NULL
;
221 int strlen_right
= 0;
223 struct smatch_state
*true_state
= NULL
;
224 struct smatch_state
*false_state
= NULL
;
226 if (expr
->type
!= EXPR_COMPARE
)
228 left
= strip_expr(expr
->left
);
229 right
= strip_expr(expr
->right
);
231 if (left
->type
== EXPR_CALL
&& sym_name_is("strlen", left
->fn
)) {
232 str
= get_argument_from_call_expr(left
->args
, 0);
235 if (right
->type
== EXPR_CALL
&& sym_name_is("strlen", right
->fn
)) {
236 str
= get_argument_from_call_expr(right
->args
, 0);
240 if (!strlen_left
&& !strlen_right
)
242 if (strlen_left
&& strlen_right
)
246 if (!get_value(right
, &val
))
250 if (!get_value(left
, &val
))
254 if (expr
->op
== SPECIAL_EQUAL
) {
255 set_true_false_states_expr(my_size_id
, str
, alloc_state_num(val
+ 1), NULL
);
258 if (expr
->op
== SPECIAL_NOTEQUAL
) {
259 set_true_false_states_expr(my_size_id
, str
, NULL
, alloc_state_num(val
+ 1));
265 case SPECIAL_UNSIGNED_LT
:
267 true_state
= alloc_state_num(val
);
269 false_state
= alloc_state_num(val
+ 1);
272 case SPECIAL_UNSIGNED_LTE
:
274 true_state
= alloc_state_num(val
+ 1);
276 false_state
= alloc_state_num(val
);
279 case SPECIAL_UNSIGNED_GTE
:
281 false_state
= alloc_state_num(val
);
283 true_state
= alloc_state_num(val
+ 1);
286 case SPECIAL_UNSIGNED_GT
:
288 false_state
= alloc_state_num(val
+ 1);
290 true_state
= alloc_state_num(val
);
293 set_true_false_states_expr(my_size_id
, str
, true_state
, false_state
);
296 static struct expression
*strip_ampersands(struct expression
*expr
)
300 if (expr
->type
!= EXPR_PREOP
)
304 type
= get_type(expr
->unop
);
305 if (!type
|| type
->type
!= SYM_ARRAY
)
310 static void match_array_assignment(struct expression
*expr
)
312 struct expression
*left
;
313 struct expression
*right
;
318 left
= strip_expr(expr
->left
);
319 right
= strip_expr(expr
->right
);
320 right
= strip_ampersands(right
);
321 array_size
= get_array_size_bytes(right
);
323 set_state_expr(my_size_id
, left
, alloc_state_num(array_size
));
326 static void match_alloc(const char *fn
, struct expression
*expr
, void *_size_arg
)
328 int size_arg
= PTR_INT(_size_arg
);
329 struct expression
*right
;
330 struct expression
*arg
;
333 right
= strip_expr(expr
->right
);
334 arg
= get_argument_from_call_expr(right
->args
, size_arg
);
335 if (!get_implied_value(arg
, &bytes
))
338 set_state_expr(my_size_id
, expr
->left
, alloc_state_num(bytes
));
341 char *member
= get_member_name(expr
->left
);
344 sm_msg("info: '%s' allocated_buf_size %lld",
350 static void match_calloc(const char *fn
, struct expression
*expr
, void *unused
)
352 struct expression
*right
;
353 struct expression
*arg
;
357 right
= strip_expr(expr
->right
);
358 arg
= get_argument_from_call_expr(right
->args
, 0);
359 if (!get_implied_value(arg
, &elements
))
361 arg
= get_argument_from_call_expr(right
->args
, 1);
362 if (!get_implied_value(arg
, &size
))
364 set_state_expr(my_size_id
, expr
->left
, alloc_state_num(elements
* size
));
367 static void match_strlen(const char *fn
, struct expression
*expr
, void *unused
)
369 struct expression
*right
;
370 struct expression
*str
;
371 struct expression
*len_expr
;
373 struct smatch_state
*state
;
375 right
= strip_expr(expr
->right
);
376 str
= get_argument_from_call_expr(right
->args
, 0);
377 len_expr
= strip_expr(expr
->left
);
379 len_name
= get_variable_from_expr(len_expr
, NULL
);
383 state
= __alloc_smatch_state(0);
384 state
->name
= len_name
;
385 state
->data
= len_expr
;
386 set_state_expr(my_strlen_id
, str
, state
);
389 static void match_limited(const char *fn
, struct expression
*expr
, void *_limiter
)
391 struct limiter
*limiter
= (struct limiter
*)_limiter
;
392 struct expression
*dest
;
393 struct expression
*size_expr
;
396 dest
= get_argument_from_call_expr(expr
->args
, limiter
->buf_arg
);
397 size_expr
= get_argument_from_call_expr(expr
->args
, limiter
->limit_arg
);
398 if (!get_implied_max(size_expr
, &size
))
400 set_state_expr(my_size_id
, dest
, alloc_state_num(size
));
403 static void match_strcpy(const char *fn
, struct expression
*expr
, void *unused
)
405 struct expression fake_assign
;
407 fake_assign
.op
= '=';
408 fake_assign
.left
= get_argument_from_call_expr(expr
->args
, 0);
409 fake_assign
.right
= get_argument_from_call_expr(expr
->args
, 1);
410 match_array_assignment(&fake_assign
);
413 static void match_strndup(const char *fn
, struct expression
*expr
, void *unused
)
415 struct expression
*fn_expr
;
416 struct expression
*size_expr
;
419 fn_expr
= strip_expr(expr
->right
);
420 size_expr
= get_argument_from_call_expr(fn_expr
->args
, 1);
421 if (!get_implied_max(size_expr
, &size
))
424 /* It's easy to forget space for the NUL char */
426 set_state_expr(my_size_id
, expr
->left
, alloc_state_num(size
));
429 static void match_call(struct expression
*expr
)
432 struct expression
*arg
;
436 name
= get_fnptr_name(expr
->fn
);
441 FOR_EACH_PTR(expr
->args
, arg
) {
442 bytes
= get_array_size_bytes(arg
);
444 sm_msg("info: passes_buffer '%s' %d '$$' %d %s",
446 is_static(expr
->fn
) ? "static" : "global");
448 } END_FOR_EACH_PTR(arg
);
453 static void struct_member_callback(char *fn
, char *global_static
, int param
, char *printed_name
, struct smatch_state
*state
)
455 if (state
== &merged
)
457 sm_msg("info: passes_buffer '%s' %d '%s' %s %s", fn
, param
, printed_name
, state
->name
, global_static
);
460 static void match_func_end(struct symbol
*sym
)
462 memset(params_set
, 0, sizeof(params_set
));
465 void register_buf_size(int id
)
469 add_definition_db_callback(set_param_buf_size
, BUF_SIZE
);
471 add_function_assign_hook("malloc", &match_alloc
, INT_PTR(0));
472 add_function_assign_hook("calloc", &match_calloc
, NULL
);
473 add_function_assign_hook("memdup", &match_alloc
, INT_PTR(1));
474 if (option_project
== PROJ_KERNEL
) {
475 add_function_assign_hook("kmalloc", &match_alloc
, INT_PTR(0));
476 add_function_assign_hook("kzalloc", &match_alloc
, INT_PTR(0));
477 add_function_assign_hook("vmalloc", &match_alloc
, INT_PTR(0));
478 add_function_assign_hook("__vmalloc", &match_alloc
, INT_PTR(0));
479 add_function_assign_hook("kcalloc", &match_calloc
, NULL
);
480 add_function_assign_hook("drm_malloc_ab", &match_calloc
, NULL
);
481 add_function_assign_hook("drm_calloc_large", &match_calloc
, NULL
);
482 add_function_assign_hook("kmemdup", &match_alloc
, INT_PTR(1));
483 add_function_assign_hook("kmemdup_user", &match_alloc
, INT_PTR(1));
485 add_hook(&match_array_assignment
, ASSIGNMENT_HOOK
);
486 add_hook(&match_strlen_condition
, CONDITION_HOOK
);
487 add_function_assign_hook("strlen", &match_strlen
, NULL
);
489 add_function_hook("strlcpy", &match_limited
, &b0_l2
);
490 add_function_hook("strlcat", &match_limited
, &b0_l2
);
491 add_function_hook("memscan", &match_limited
, &b0_l2
);
493 add_function_hook("strcpy", &match_strcpy
, NULL
);
495 add_function_assign_hook("strndup", match_strndup
, NULL
);
496 if (option_project
== PROJ_KERNEL
)
497 add_function_assign_hook("kstrndup", match_strndup
, NULL
);
500 add_hook(&match_call
, FUNCTION_CALL_HOOK
);
501 add_member_info_callback(my_size_id
, struct_member_callback
);
504 add_hook(&match_func_end
, END_FUNC_HOOK
);
507 void register_strlen(int id
)