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 get_initializer_size(struct expression
*expr
)
62 return expr
->string
->length
;
63 case EXPR_INITIALIZER
: {
64 struct expression
*tmp
;
68 FOR_EACH_PTR(expr
->expr_list
, tmp
) {
69 if (tmp
->type
== EXPR_INDEX
&& tmp
->idx_to
> max
)
72 } END_FOR_EACH_PTR(tmp
);
78 return get_array_size(expr
);
83 static float get_cast_ratio(struct expression
*unstripped
)
85 struct expression
*start_expr
;
86 struct symbol
*start_type
;
87 struct symbol
*end_type
;
91 start_expr
= strip_expr(unstripped
);
92 start_type
= get_type(start_expr
);
93 end_type
= get_type(unstripped
);
94 if (!start_type
|| !end_type
)
97 if (start_type
->type
== SYM_PTR
)
98 start_bytes
= (get_base_type(start_type
))->ctype
.alignment
;
99 if (start_type
->type
== SYM_ARRAY
)
100 start_bytes
= (get_base_type(start_type
))->bit_size
/ 8;
101 if (end_type
->type
== SYM_PTR
)
102 end_bytes
= (get_base_type(end_type
))->ctype
.alignment
;
103 if (end_type
->type
== SYM_ARRAY
)
104 end_bytes
= (get_base_type(end_type
))->bit_size
/ 8;
106 if (!start_bytes
|| !end_bytes
)
108 return start_bytes
/ end_bytes
;
112 static int db_size_callback(void *unused
, int argc
, char **argv
, char **azColName
)
114 db_size
= atoi(argv
[0]);
118 static int size_from_db(struct expression
*expr
)
125 name
= get_member_name(expr
);
130 run_sql(db_size_callback
, "select size from type_size where type = '%s'",
135 static int get_real_array_size(struct expression
*expr
)
141 type
= get_type(expr
);
142 if (!type
|| type
->type
!= SYM_ARRAY
)
145 cast_ratio
= get_cast_ratio(expr
);
147 ret
= get_expression_value(type
->array_size
);
148 /* Dynamically sized array are -1 in sparse */
151 /* People put one element arrays on the end of structs */
155 return ret
* cast_ratio
;
158 int get_array_size(struct expression
*expr
)
161 struct smatch_state
*state
;
166 if (expr
->type
== EXPR_STRING
)
167 return expr
->string
->length
;
169 ret
= get_real_array_size(expr
);
173 cast_ratio
= get_cast_ratio(expr
);
174 expr
= strip_expr(expr
);
175 tmp
= get_type(expr
);
179 state
= get_state_expr(my_size_id
, expr
);
180 if (state
== &merged
)
182 if (state
&& state
->data
) {
183 if (tmp
->type
== SYM_PTR
)
184 tmp
= get_base_type(tmp
);
185 if (!tmp
->ctype
.alignment
)
187 ret
= PTR_INT(state
->data
) / tmp
->ctype
.alignment
;
188 return ret
* cast_ratio
;
191 if (expr
->type
== EXPR_SYMBOL
&& expr
->symbol
->initializer
) {
192 if (expr
->symbol
->initializer
!= expr
) /* int a = a; */
193 return get_initializer_size(expr
->symbol
->initializer
) * cast_ratio
;
196 state
= get_state_expr(my_strlen_id
, expr
);
197 if (!state
|| !state
->data
)
199 if (get_implied_max((struct expression
*)state
->data
, &len
))
200 return len
+ 1; /* add one because strlen doesn't include the NULL */
204 int get_array_size_bytes(struct expression
*expr
)
213 if (expr
->type
== EXPR_STRING
)
214 return expr
->string
->length
;
216 tmp
= get_type(expr
);
220 if (tmp
->type
== SYM_ARRAY
) {
221 tmp
= get_base_type(tmp
);
222 element_size
= tmp
->bit_size
/ 8;
223 } else if (tmp
->type
== SYM_PTR
) {
224 tmp
= get_base_type(tmp
);
225 element_size
= tmp
->ctype
.alignment
;
229 array_size
= get_array_size(expr
);
231 return array_size
* element_size
;
233 return size_from_db(expr
);
236 static void match_strlen_condition(struct expression
*expr
)
238 struct expression
*left
;
239 struct expression
*right
;
240 struct expression
*str
= NULL
;
242 int strlen_right
= 0;
244 struct smatch_state
*true_state
= NULL
;
245 struct smatch_state
*false_state
= NULL
;
247 if (expr
->type
!= EXPR_COMPARE
)
249 left
= strip_expr(expr
->left
);
250 right
= strip_expr(expr
->right
);
252 if (left
->type
== EXPR_CALL
&& sym_name_is("strlen", left
->fn
)) {
253 str
= get_argument_from_call_expr(left
->args
, 0);
256 if (right
->type
== EXPR_CALL
&& sym_name_is("strlen", right
->fn
)) {
257 str
= get_argument_from_call_expr(right
->args
, 0);
261 if (!strlen_left
&& !strlen_right
)
263 if (strlen_left
&& strlen_right
)
267 if (!get_value(right
, &val
))
271 if (!get_value(left
, &val
))
275 if (expr
->op
== SPECIAL_EQUAL
) {
276 set_true_false_states_expr(my_size_id
, str
, alloc_state_num(val
+ 1), NULL
);
279 if (expr
->op
== SPECIAL_NOTEQUAL
) {
280 set_true_false_states_expr(my_size_id
, str
, NULL
, alloc_state_num(val
+ 1));
286 case SPECIAL_UNSIGNED_LT
:
288 true_state
= alloc_state_num(val
);
290 false_state
= alloc_state_num(val
+ 1);
293 case SPECIAL_UNSIGNED_LTE
:
295 true_state
= alloc_state_num(val
+ 1);
297 false_state
= alloc_state_num(val
);
300 case SPECIAL_UNSIGNED_GTE
:
302 false_state
= alloc_state_num(val
);
304 true_state
= alloc_state_num(val
+ 1);
307 case SPECIAL_UNSIGNED_GT
:
309 false_state
= alloc_state_num(val
+ 1);
311 true_state
= alloc_state_num(val
);
314 set_true_false_states_expr(my_size_id
, str
, true_state
, false_state
);
317 static struct expression
*strip_ampersands(struct expression
*expr
)
321 if (expr
->type
!= EXPR_PREOP
)
325 type
= get_type(expr
->unop
);
326 if (!type
|| type
->type
!= SYM_ARRAY
)
331 static void match_array_assignment(struct expression
*expr
)
333 struct expression
*left
;
334 struct expression
*right
;
339 left
= strip_expr(expr
->left
);
340 right
= strip_expr(expr
->right
);
341 right
= strip_ampersands(right
);
342 array_size
= get_array_size_bytes(right
);
344 set_state_expr(my_size_id
, left
, alloc_state_num(array_size
));
347 static void match_alloc(const char *fn
, struct expression
*expr
, void *_size_arg
)
349 int size_arg
= PTR_INT(_size_arg
);
350 struct expression
*right
;
351 struct expression
*arg
;
354 right
= strip_expr(expr
->right
);
355 arg
= get_argument_from_call_expr(right
->args
, size_arg
);
356 if (!get_implied_value(arg
, &bytes
))
359 set_state_expr(my_size_id
, expr
->left
, alloc_state_num(bytes
));
362 char *member
= get_member_name(expr
->left
);
365 sm_msg("info: '%s' allocated_buf_size %lld",
371 static void match_calloc(const char *fn
, struct expression
*expr
, void *unused
)
373 struct expression
*right
;
374 struct expression
*arg
;
378 right
= strip_expr(expr
->right
);
379 arg
= get_argument_from_call_expr(right
->args
, 0);
380 if (!get_implied_value(arg
, &elements
))
382 arg
= get_argument_from_call_expr(right
->args
, 1);
383 if (!get_implied_value(arg
, &size
))
385 set_state_expr(my_size_id
, expr
->left
, alloc_state_num(elements
* size
));
388 static void match_strlen(const char *fn
, struct expression
*expr
, void *unused
)
390 struct expression
*right
;
391 struct expression
*str
;
392 struct expression
*len_expr
;
394 struct smatch_state
*state
;
396 right
= strip_expr(expr
->right
);
397 str
= get_argument_from_call_expr(right
->args
, 0);
398 len_expr
= strip_expr(expr
->left
);
400 len_name
= get_variable_from_expr(len_expr
, NULL
);
404 state
= __alloc_smatch_state(0);
405 state
->name
= len_name
;
406 state
->data
= len_expr
;
407 set_state_expr(my_strlen_id
, str
, state
);
410 static void match_limited(const char *fn
, struct expression
*expr
, void *_limiter
)
412 struct limiter
*limiter
= (struct limiter
*)_limiter
;
413 struct expression
*dest
;
414 struct expression
*size_expr
;
417 dest
= get_argument_from_call_expr(expr
->args
, limiter
->buf_arg
);
418 size_expr
= get_argument_from_call_expr(expr
->args
, limiter
->limit_arg
);
419 if (!get_implied_max(size_expr
, &size
))
421 set_state_expr(my_size_id
, dest
, alloc_state_num(size
));
424 static void match_strcpy(const char *fn
, struct expression
*expr
, void *unused
)
426 struct expression fake_assign
;
428 fake_assign
.op
= '=';
429 fake_assign
.left
= get_argument_from_call_expr(expr
->args
, 0);
430 fake_assign
.right
= get_argument_from_call_expr(expr
->args
, 1);
431 match_array_assignment(&fake_assign
);
434 static void match_strndup(const char *fn
, struct expression
*expr
, void *unused
)
436 struct expression
*fn_expr
;
437 struct expression
*size_expr
;
440 fn_expr
= strip_expr(expr
->right
);
441 size_expr
= get_argument_from_call_expr(fn_expr
->args
, 1);
442 if (!get_implied_max(size_expr
, &size
))
445 /* It's easy to forget space for the NUL char */
447 set_state_expr(my_size_id
, expr
->left
, alloc_state_num(size
));
450 static void match_call(struct expression
*expr
)
453 struct expression
*arg
;
457 name
= get_fnptr_name(expr
->fn
);
462 FOR_EACH_PTR(expr
->args
, arg
) {
463 bytes
= get_array_size_bytes(arg
);
465 sm_msg("info: passes_buffer '%s' %d '$$' %d %s",
467 is_static(expr
->fn
) ? "static" : "global");
469 } END_FOR_EACH_PTR(arg
);
474 static void struct_member_callback(char *fn
, char *global_static
, int param
, char *printed_name
, struct smatch_state
*state
)
476 if (state
== &merged
)
478 sm_msg("info: passes_buffer '%s' %d '%s' %s %s", fn
, param
, printed_name
, state
->name
, global_static
);
481 static void match_func_end(struct symbol
*sym
)
483 memset(params_set
, 0, sizeof(params_set
));
486 void register_buf_size(int id
)
490 add_definition_db_callback(set_param_buf_size
, BUF_SIZE
);
492 add_function_assign_hook("malloc", &match_alloc
, INT_PTR(0));
493 add_function_assign_hook("calloc", &match_calloc
, NULL
);
494 add_function_assign_hook("memdup", &match_alloc
, INT_PTR(1));
495 if (option_project
== PROJ_KERNEL
) {
496 add_function_assign_hook("kmalloc", &match_alloc
, INT_PTR(0));
497 add_function_assign_hook("kzalloc", &match_alloc
, INT_PTR(0));
498 add_function_assign_hook("vmalloc", &match_alloc
, INT_PTR(0));
499 add_function_assign_hook("__vmalloc", &match_alloc
, INT_PTR(0));
500 add_function_assign_hook("kcalloc", &match_calloc
, NULL
);
501 add_function_assign_hook("drm_malloc_ab", &match_calloc
, NULL
);
502 add_function_assign_hook("drm_calloc_large", &match_calloc
, NULL
);
503 add_function_assign_hook("kmemdup", &match_alloc
, INT_PTR(1));
504 add_function_assign_hook("kmemdup_user", &match_alloc
, INT_PTR(1));
506 add_hook(&match_array_assignment
, ASSIGNMENT_HOOK
);
507 add_hook(&match_strlen_condition
, CONDITION_HOOK
);
508 add_function_assign_hook("strlen", &match_strlen
, NULL
);
510 add_function_hook("strlcpy", &match_limited
, &b0_l2
);
511 add_function_hook("strlcat", &match_limited
, &b0_l2
);
512 add_function_hook("memscan", &match_limited
, &b0_l2
);
514 add_function_hook("strcpy", &match_strcpy
, NULL
);
516 add_function_assign_hook("strndup", match_strndup
, NULL
);
517 if (option_project
== PROJ_KERNEL
)
518 add_function_assign_hook("kstrndup", match_strndup
, NULL
);
521 add_hook(&match_call
, FUNCTION_CALL_HOOK
);
522 add_member_info_callback(my_size_id
, struct_member_callback
);
525 add_hook(&match_func_end
, END_FUNC_HOOK
);
526 add_modification_hook(my_size_id
, &set_undefined
);
529 void register_strlen(int id
)
532 add_modification_hook(my_strlen_id
, &set_undefined
);