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 */
159 int get_array_size_bytes(struct expression
*expr
)
165 if (expr
->type
== EXPR_STRING
)
166 return expr
->string
->length
;
168 tmp
= get_type(expr
);
172 if (tmp
->type
== SYM_ARRAY
) {
173 tmp
= get_base_type(tmp
);
174 element_size
= tmp
->bit_size
/ 8;
175 } else if (tmp
->type
== SYM_PTR
) {
176 tmp
= get_base_type(tmp
);
177 element_size
= tmp
->ctype
.alignment
;
181 array_size
= get_array_size(expr
);
182 return array_size
* element_size
;
185 static void match_strlen_condition(struct expression
*expr
)
187 struct expression
*left
;
188 struct expression
*right
;
189 struct expression
*str
= NULL
;
191 int strlen_right
= 0;
193 struct smatch_state
*true_state
= NULL
;
194 struct smatch_state
*false_state
= NULL
;
196 if (expr
->type
!= EXPR_COMPARE
)
198 left
= strip_expr(expr
->left
);
199 right
= strip_expr(expr
->right
);
201 if (left
->type
== EXPR_CALL
&& sym_name_is("strlen", left
->fn
)) {
202 str
= get_argument_from_call_expr(left
->args
, 0);
205 if (right
->type
== EXPR_CALL
&& sym_name_is("strlen", right
->fn
)) {
206 str
= get_argument_from_call_expr(right
->args
, 0);
210 if (!strlen_left
&& !strlen_right
)
212 if (strlen_left
&& strlen_right
)
216 if (!get_value(right
, &val
))
220 if (!get_value(left
, &val
))
224 if (expr
->op
== SPECIAL_EQUAL
) {
225 set_true_false_states_expr(my_size_id
, str
, alloc_state_num(val
+ 1), NULL
);
228 if (expr
->op
== SPECIAL_NOTEQUAL
) {
229 set_true_false_states_expr(my_size_id
, str
, NULL
, alloc_state_num(val
+ 1));
235 case SPECIAL_UNSIGNED_LT
:
237 true_state
= alloc_state_num(val
);
239 false_state
= alloc_state_num(val
+ 1);
242 case SPECIAL_UNSIGNED_LTE
:
244 true_state
= alloc_state_num(val
+ 1);
246 false_state
= alloc_state_num(val
);
249 case SPECIAL_UNSIGNED_GTE
:
251 false_state
= alloc_state_num(val
);
253 true_state
= alloc_state_num(val
+ 1);
256 case SPECIAL_UNSIGNED_GT
:
258 false_state
= alloc_state_num(val
+ 1);
260 true_state
= alloc_state_num(val
);
263 set_true_false_states_expr(my_size_id
, str
, true_state
, false_state
);
266 static struct expression
*strip_ampersands(struct expression
*expr
)
270 if (expr
->type
!= EXPR_PREOP
)
274 type
= get_type(expr
->unop
);
275 if (!type
|| type
->type
!= SYM_ARRAY
)
280 static void match_array_assignment(struct expression
*expr
)
282 struct expression
*left
;
283 struct expression
*right
;
288 left
= strip_expr(expr
->left
);
289 right
= strip_expr(expr
->right
);
290 right
= strip_ampersands(right
);
291 array_size
= get_array_size_bytes(right
);
293 set_state_expr(my_size_id
, left
, alloc_state_num(array_size
));
296 static void match_alloc(const char *fn
, struct expression
*expr
, void *_size_arg
)
298 int size_arg
= PTR_INT(_size_arg
);
299 struct expression
*right
;
300 struct expression
*arg
;
303 right
= strip_expr(expr
->right
);
304 arg
= get_argument_from_call_expr(right
->args
, size_arg
);
305 if (!get_implied_value(arg
, &bytes
))
307 set_state_expr(my_size_id
, expr
->left
, alloc_state_num(bytes
));
310 static void match_calloc(const char *fn
, struct expression
*expr
, void *unused
)
312 struct expression
*right
;
313 struct expression
*arg
;
317 right
= strip_expr(expr
->right
);
318 arg
= get_argument_from_call_expr(right
->args
, 0);
319 if (!get_implied_value(arg
, &elements
))
321 arg
= get_argument_from_call_expr(right
->args
, 1);
322 if (!get_implied_value(arg
, &size
))
324 set_state_expr(my_size_id
, expr
->left
, alloc_state_num(elements
* size
));
327 static void match_strlen(const char *fn
, struct expression
*expr
, void *unused
)
329 struct expression
*right
;
330 struct expression
*str
;
331 struct expression
*len_expr
;
333 struct smatch_state
*state
;
335 right
= strip_expr(expr
->right
);
336 str
= get_argument_from_call_expr(right
->args
, 0);
337 len_expr
= strip_expr(expr
->left
);
339 len_name
= get_variable_from_expr(len_expr
, NULL
);
343 state
= __alloc_smatch_state(0);
344 state
->name
= len_name
;
345 state
->data
= len_expr
;
346 set_state_expr(my_strlen_id
, str
, state
);
349 static void match_limited(const char *fn
, struct expression
*expr
, void *_limiter
)
351 struct limiter
*limiter
= (struct limiter
*)_limiter
;
352 struct expression
*dest
;
353 struct expression
*size_expr
;
356 dest
= get_argument_from_call_expr(expr
->args
, limiter
->buf_arg
);
357 size_expr
= get_argument_from_call_expr(expr
->args
, limiter
->limit_arg
);
358 if (!get_implied_max(size_expr
, &size
))
360 set_state_expr(my_size_id
, dest
, alloc_state_num(size
));
363 static void match_strcpy(const char *fn
, struct expression
*expr
, void *unused
)
365 struct expression fake_assign
;
367 fake_assign
.op
= '=';
368 fake_assign
.left
= get_argument_from_call_expr(expr
->args
, 0);
369 fake_assign
.right
= get_argument_from_call_expr(expr
->args
, 1);
370 match_array_assignment(&fake_assign
);
373 static void match_strndup(const char *fn
, struct expression
*expr
, void *unused
)
375 struct expression
*fn_expr
;
376 struct expression
*size_expr
;
379 fn_expr
= strip_expr(expr
->right
);
380 size_expr
= get_argument_from_call_expr(fn_expr
->args
, 1);
381 if (!get_implied_max(size_expr
, &size
))
384 /* It's easy to forget space for the NUL char */
386 set_state_expr(my_size_id
, expr
->left
, alloc_state_num(size
));
389 static void match_call(struct expression
*expr
)
392 struct expression
*arg
;
396 name
= get_fnptr_name(expr
->fn
);
401 FOR_EACH_PTR(expr
->args
, arg
) {
402 bytes
= get_array_size_bytes(arg
);
404 sm_msg("info: passes_buffer '%s' %d '$$' %d", name
, i
, bytes
);
406 } END_FOR_EACH_PTR(arg
);
411 static void struct_member_callback(char *fn
, int param
, char *printed_name
, struct smatch_state
*state
)
413 if (state
== &merged
)
415 sm_msg("info: passes_buffer '%s' %d '%s' %s", fn
, param
, printed_name
, state
->name
);
418 static void match_func_end(struct symbol
*sym
)
420 memset(params_set
, 0, sizeof(params_set
));
423 void register_buf_size(int id
)
427 add_definition_db_callback(set_param_buf_size
, BUF_SIZE
);
429 add_function_assign_hook("malloc", &match_alloc
, INT_PTR(0));
430 add_function_assign_hook("calloc", &match_calloc
, NULL
);
431 add_function_assign_hook("memdup", &match_alloc
, INT_PTR(1));
432 if (option_project
== PROJ_KERNEL
) {
433 add_function_assign_hook("kmalloc", &match_alloc
, INT_PTR(0));
434 add_function_assign_hook("kzalloc", &match_alloc
, INT_PTR(0));
435 add_function_assign_hook("vmalloc", &match_alloc
, INT_PTR(0));
436 add_function_assign_hook("__vmalloc", &match_alloc
, INT_PTR(0));
437 add_function_assign_hook("kcalloc", &match_calloc
, NULL
);
438 add_function_assign_hook("drm_malloc_ab", &match_calloc
, NULL
);
439 add_function_assign_hook("drm_calloc_large", &match_calloc
, NULL
);
440 add_function_assign_hook("kmemdup", &match_alloc
, INT_PTR(1));
441 add_function_assign_hook("kmemdup_user", &match_alloc
, INT_PTR(1));
443 add_hook(&match_array_assignment
, ASSIGNMENT_HOOK
);
444 add_hook(&match_strlen_condition
, CONDITION_HOOK
);
445 add_function_assign_hook("strlen", &match_strlen
, NULL
);
447 add_function_hook("strlcpy", &match_limited
, &b0_l2
);
448 add_function_hook("strlcat", &match_limited
, &b0_l2
);
449 add_function_hook("memscan", &match_limited
, &b0_l2
);
451 add_function_hook("strcpy", &match_strcpy
, NULL
);
453 add_function_assign_hook("strndup", match_strndup
, NULL
);
454 if (option_project
== PROJ_KERNEL
)
455 add_function_assign_hook("kstrndup", match_strndup
, NULL
);
458 add_hook(&match_call
, FUNCTION_CALL_HOOK
);
459 add_member_info_callback(my_size_id
, struct_member_callback
);
462 add_hook(&match_func_end
, END_FUNC_HOOK
);
465 void register_strlen(int id
)