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 static int get_size_from_initializer(struct expression
*expr
)
162 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
->initializer
)
164 if (expr
->symbol
->initializer
== expr
) /* int a = a; */
166 cast_ratio
= get_cast_ratio(expr
);
167 return get_initializer_size(expr
->symbol
->initializer
) * cast_ratio
;
170 static int get_stored_size(struct expression
*expr
)
173 struct smatch_state
*state
;
177 type
= get_type(expr
);
181 cast_ratio
= get_cast_ratio(expr
);
183 state
= get_state_expr(my_size_id
, expr
);
184 if (!state
|| !state
->data
)
186 if (type
->type
== SYM_PTR
)
187 type
= get_base_type(type
);
188 if (!type
->ctype
.alignment
)
190 ret
= PTR_INT(state
->data
) / type
->ctype
.alignment
;
191 return ret
* cast_ratio
;
194 int get_array_size(struct expression
*expr
)
196 struct smatch_state
*state
;
200 expr
= strip_expr(expr
);
204 if (expr
->type
== EXPR_STRING
)
205 return expr
->string
->length
;
207 ret
= get_real_array_size(expr
);
211 ret
= get_stored_size(expr
);
215 ret
= get_size_from_initializer(expr
);
219 state
= get_state_expr(my_strlen_id
, expr
);
220 if (!state
|| !state
->data
)
222 if (get_implied_max((struct expression
*)state
->data
, &len
))
223 return len
+ 1; /* add one because strlen doesn't include the NULL */
227 int get_array_size_bytes(struct expression
*expr
)
236 if (expr
->type
== EXPR_STRING
)
237 return expr
->string
->length
;
239 tmp
= get_type(expr
);
243 if (tmp
->type
== SYM_ARRAY
) {
244 tmp
= get_base_type(tmp
);
245 element_size
= tmp
->bit_size
/ 8;
246 } else if (tmp
->type
== SYM_PTR
) {
247 tmp
= get_base_type(tmp
);
248 element_size
= tmp
->ctype
.alignment
;
252 array_size
= get_array_size(expr
);
254 return array_size
* element_size
;
256 return size_from_db(expr
);
259 static void match_strlen_condition(struct expression
*expr
)
261 struct expression
*left
;
262 struct expression
*right
;
263 struct expression
*str
= NULL
;
265 int strlen_right
= 0;
267 struct smatch_state
*true_state
= NULL
;
268 struct smatch_state
*false_state
= NULL
;
270 if (expr
->type
!= EXPR_COMPARE
)
272 left
= strip_expr(expr
->left
);
273 right
= strip_expr(expr
->right
);
275 if (left
->type
== EXPR_CALL
&& sym_name_is("strlen", left
->fn
)) {
276 str
= get_argument_from_call_expr(left
->args
, 0);
279 if (right
->type
== EXPR_CALL
&& sym_name_is("strlen", right
->fn
)) {
280 str
= get_argument_from_call_expr(right
->args
, 0);
284 if (!strlen_left
&& !strlen_right
)
286 if (strlen_left
&& strlen_right
)
290 if (!get_value(right
, &val
))
294 if (!get_value(left
, &val
))
298 if (expr
->op
== SPECIAL_EQUAL
) {
299 set_true_false_states_expr(my_size_id
, str
, alloc_state_num(val
+ 1), NULL
);
302 if (expr
->op
== SPECIAL_NOTEQUAL
) {
303 set_true_false_states_expr(my_size_id
, str
, NULL
, alloc_state_num(val
+ 1));
309 case SPECIAL_UNSIGNED_LT
:
311 true_state
= alloc_state_num(val
);
313 false_state
= alloc_state_num(val
+ 1);
316 case SPECIAL_UNSIGNED_LTE
:
318 true_state
= alloc_state_num(val
+ 1);
320 false_state
= alloc_state_num(val
);
323 case SPECIAL_UNSIGNED_GTE
:
325 false_state
= alloc_state_num(val
);
327 true_state
= alloc_state_num(val
+ 1);
330 case SPECIAL_UNSIGNED_GT
:
332 false_state
= alloc_state_num(val
+ 1);
334 true_state
= alloc_state_num(val
);
337 set_true_false_states_expr(my_size_id
, str
, true_state
, false_state
);
340 static struct expression
*strip_ampersands(struct expression
*expr
)
344 if (expr
->type
!= EXPR_PREOP
)
348 type
= get_type(expr
->unop
);
349 if (!type
|| type
->type
!= SYM_ARRAY
)
354 static void match_array_assignment(struct expression
*expr
)
356 struct expression
*left
;
357 struct expression
*right
;
362 left
= strip_expr(expr
->left
);
363 right
= strip_expr(expr
->right
);
364 right
= strip_ampersands(right
);
365 array_size
= get_array_size_bytes(right
);
367 set_state_expr(my_size_id
, left
, alloc_state_num(array_size
));
370 static void match_alloc(const char *fn
, struct expression
*expr
, void *_size_arg
)
372 int size_arg
= PTR_INT(_size_arg
);
373 struct expression
*right
;
374 struct expression
*arg
;
377 right
= strip_expr(expr
->right
);
378 arg
= get_argument_from_call_expr(right
->args
, size_arg
);
379 if (!get_implied_value(arg
, &bytes
))
382 set_state_expr(my_size_id
, expr
->left
, alloc_state_num(bytes
));
385 char *member
= get_member_name(expr
->left
);
388 sm_msg("info: '%s' allocated_buf_size %lld",
394 static void match_calloc(const char *fn
, struct expression
*expr
, void *unused
)
396 struct expression
*right
;
397 struct expression
*arg
;
401 right
= strip_expr(expr
->right
);
402 arg
= get_argument_from_call_expr(right
->args
, 0);
403 if (!get_implied_value(arg
, &elements
))
405 arg
= get_argument_from_call_expr(right
->args
, 1);
406 if (!get_implied_value(arg
, &size
))
408 set_state_expr(my_size_id
, expr
->left
, alloc_state_num(elements
* size
));
411 static void match_strlen(const char *fn
, struct expression
*expr
, void *unused
)
413 struct expression
*right
;
414 struct expression
*str
;
415 struct expression
*len_expr
;
417 struct smatch_state
*state
;
419 right
= strip_expr(expr
->right
);
420 str
= get_argument_from_call_expr(right
->args
, 0);
421 len_expr
= strip_expr(expr
->left
);
423 len_name
= get_variable_from_expr(len_expr
, NULL
);
427 state
= __alloc_smatch_state(0);
428 state
->name
= len_name
;
429 state
->data
= len_expr
;
430 set_state_expr(my_strlen_id
, str
, state
);
433 static void match_limited(const char *fn
, struct expression
*expr
, void *_limiter
)
435 struct limiter
*limiter
= (struct limiter
*)_limiter
;
436 struct expression
*dest
;
437 struct expression
*size_expr
;
440 dest
= get_argument_from_call_expr(expr
->args
, limiter
->buf_arg
);
441 size_expr
= get_argument_from_call_expr(expr
->args
, limiter
->limit_arg
);
442 if (!get_implied_max(size_expr
, &size
))
444 set_state_expr(my_size_id
, dest
, alloc_state_num(size
));
447 static void match_strcpy(const char *fn
, struct expression
*expr
, void *unused
)
449 struct expression fake_assign
;
451 fake_assign
.op
= '=';
452 fake_assign
.left
= get_argument_from_call_expr(expr
->args
, 0);
453 fake_assign
.right
= get_argument_from_call_expr(expr
->args
, 1);
454 match_array_assignment(&fake_assign
);
457 static void match_strndup(const char *fn
, struct expression
*expr
, void *unused
)
459 struct expression
*fn_expr
;
460 struct expression
*size_expr
;
463 fn_expr
= strip_expr(expr
->right
);
464 size_expr
= get_argument_from_call_expr(fn_expr
->args
, 1);
465 if (!get_implied_max(size_expr
, &size
))
468 /* It's easy to forget space for the NUL char */
470 set_state_expr(my_size_id
, expr
->left
, alloc_state_num(size
));
473 static void match_call(struct expression
*expr
)
476 struct expression
*arg
;
480 name
= get_fnptr_name(expr
->fn
);
485 FOR_EACH_PTR(expr
->args
, arg
) {
486 bytes
= get_array_size_bytes(arg
);
488 sm_msg("info: passes_buffer '%s' %d '$$' %d %s",
490 is_static(expr
->fn
) ? "static" : "global");
492 } END_FOR_EACH_PTR(arg
);
497 static void struct_member_callback(char *fn
, char *global_static
, int param
, char *printed_name
, struct smatch_state
*state
)
499 if (state
== &merged
)
501 sm_msg("info: passes_buffer '%s' %d '%s' %s %s", fn
, param
, printed_name
, state
->name
, global_static
);
504 static void match_func_end(struct symbol
*sym
)
506 memset(params_set
, 0, sizeof(params_set
));
509 void register_buf_size(int id
)
513 add_definition_db_callback(set_param_buf_size
, BUF_SIZE
);
515 add_function_assign_hook("malloc", &match_alloc
, INT_PTR(0));
516 add_function_assign_hook("calloc", &match_calloc
, NULL
);
517 add_function_assign_hook("memdup", &match_alloc
, INT_PTR(1));
518 if (option_project
== PROJ_KERNEL
) {
519 add_function_assign_hook("kmalloc", &match_alloc
, INT_PTR(0));
520 add_function_assign_hook("kzalloc", &match_alloc
, INT_PTR(0));
521 add_function_assign_hook("vmalloc", &match_alloc
, INT_PTR(0));
522 add_function_assign_hook("__vmalloc", &match_alloc
, INT_PTR(0));
523 add_function_assign_hook("kcalloc", &match_calloc
, NULL
);
524 add_function_assign_hook("drm_malloc_ab", &match_calloc
, NULL
);
525 add_function_assign_hook("drm_calloc_large", &match_calloc
, NULL
);
526 add_function_assign_hook("kmemdup", &match_alloc
, INT_PTR(1));
527 add_function_assign_hook("kmemdup_user", &match_alloc
, INT_PTR(1));
529 add_hook(&match_array_assignment
, ASSIGNMENT_HOOK
);
530 add_hook(&match_strlen_condition
, CONDITION_HOOK
);
531 add_function_assign_hook("strlen", &match_strlen
, NULL
);
533 add_function_hook("strlcpy", &match_limited
, &b0_l2
);
534 add_function_hook("strlcat", &match_limited
, &b0_l2
);
535 add_function_hook("memscan", &match_limited
, &b0_l2
);
537 add_function_hook("strcpy", &match_strcpy
, NULL
);
539 add_function_assign_hook("strndup", match_strndup
, NULL
);
540 if (option_project
== PROJ_KERNEL
)
541 add_function_assign_hook("kstrndup", match_strndup
, NULL
);
544 add_hook(&match_call
, FUNCTION_CALL_HOOK
);
545 add_member_info_callback(my_size_id
, struct_member_callback
);
548 add_hook(&match_func_end
, END_FUNC_HOOK
);
549 add_modification_hook(my_size_id
, &set_undefined
);
552 void register_strlen(int id
)
555 add_modification_hook(my_strlen_id
, &set_undefined
);