2 * smatch/smatch_buf_size.c
4 * Copyright (C) 2010 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
13 #include "smatch_slist.h"
14 #include "smatch_extra.h"
22 * This check has two smatch IDs.
23 * my_size_id - used to store the size of arrays.
24 * my_strlen_id - track the strlen() of buffers.
26 static int my_size_id
;
27 static int my_strlen_id
;
29 static char *alloc_num(long long num
)
31 static char buff
[256];
33 if (num
== whole_range
.min
)
34 snprintf(buff
, 255, "min");
35 else if (num
== whole_range
.max
)
36 snprintf(buff
, 255, "max");
38 snprintf(buff
, 255, "(%lld)", num
);
40 snprintf(buff
, 255, "%lld", num
);
43 return alloc_sname(buff
);
46 static struct smatch_state
*alloc_my_state(int val
)
48 struct smatch_state
*state
;
50 state
= __alloc_smatch_state(0);
51 state
->name
= alloc_num(val
);
52 state
->data
= malloc(sizeof(int));
53 *(int *)state
->data
= val
;
57 static int get_initializer_size(struct expression
*expr
)
61 return expr
->string
->length
;
62 case EXPR_INITIALIZER
: {
63 struct expression
*tmp
;
67 FOR_EACH_PTR(expr
->expr_list
, tmp
) {
68 if (tmp
->type
== EXPR_INDEX
&& tmp
->idx_to
> max
)
71 } END_FOR_EACH_PTR(tmp
);
77 return get_array_size(expr
);
82 static float get_cast_ratio(struct expression
*unstripped
)
84 struct expression
*start_expr
;
85 struct symbol
*start_type
;
86 struct symbol
*end_type
;
90 start_expr
= strip_expr(unstripped
);
91 start_type
= get_type(start_expr
);
92 end_type
= get_type(unstripped
);
93 if (!start_type
|| !end_type
)
96 if (start_type
->type
== SYM_PTR
)
97 start_bytes
= (get_base_type(start_type
))->ctype
.alignment
;
98 if (start_type
->type
== SYM_ARRAY
)
99 start_bytes
= (get_base_type(start_type
))->bit_size
/ 8;
100 if (end_type
->type
== SYM_PTR
)
101 end_bytes
= (get_base_type(end_type
))->ctype
.alignment
;
102 if (end_type
->type
== SYM_ARRAY
)
103 end_bytes
= (get_base_type(end_type
))->bit_size
/ 8;
105 if (!start_bytes
|| !end_bytes
)
107 return start_bytes
/ end_bytes
;
110 int get_array_size(struct expression
*expr
)
113 struct smatch_state
*state
;
118 if (expr
->type
== EXPR_STRING
)
119 return expr
->string
->length
;
121 cast_ratio
= get_cast_ratio(expr
);
122 expr
= strip_expr(expr
);
123 tmp
= get_type(expr
);
127 if (tmp
->type
== SYM_ARRAY
) {
128 ret
= get_expression_value(tmp
->array_size
);
132 return ret
* cast_ratio
;
135 state
= get_state_expr(my_size_id
, expr
);
136 if (state
== &merged
)
138 if (state
&& state
->data
) {
139 if (tmp
->type
== SYM_PTR
)
140 tmp
= get_base_type(tmp
);
141 if (!tmp
->ctype
.alignment
)
143 ret
= *(int *)state
->data
/ tmp
->ctype
.alignment
;
144 return ret
* cast_ratio
;
147 if (expr
->type
== EXPR_SYMBOL
&& expr
->symbol
->initializer
) {
148 if (expr
->symbol
->initializer
!= expr
) /* int a = a; */
149 return get_initializer_size(expr
->symbol
->initializer
) * cast_ratio
;
152 state
= get_state_expr(my_strlen_id
, expr
);
153 if (!state
|| !state
->data
)
155 if (get_implied_max((struct expression
*)state
->data
, &len
))
156 return len
+ 1; /* add one because strlen doesn't include the NULL */
160 int get_array_size_bytes(struct expression
*expr
)
166 if (expr
->type
== EXPR_STRING
)
167 return expr
->string
->length
;
169 tmp
= get_type(expr
);
173 if (tmp
->type
== SYM_ARRAY
) {
174 tmp
= get_base_type(tmp
);
175 element_size
= tmp
->bit_size
/ 8;
176 } else if (tmp
->type
== SYM_PTR
) {
177 tmp
= get_base_type(tmp
);
178 element_size
= tmp
->ctype
.alignment
;
182 array_size
= get_array_size(expr
);
183 return array_size
* element_size
;
186 static void match_strlen_condition(struct expression
*expr
)
188 struct expression
*left
;
189 struct expression
*right
;
190 struct expression
*str
;
192 int strlen_right
= 0;
194 struct smatch_state
*true_state
= NULL
;
195 struct smatch_state
*false_state
= NULL
;
197 if (expr
->type
!= EXPR_COMPARE
)
199 left
= strip_expr(expr
->left
);
200 right
= strip_expr(expr
->right
);
202 if (left
->type
== EXPR_CALL
&& sym_name_is("strlen", left
->fn
)) {
203 str
= get_argument_from_call_expr(left
->args
, 0);
206 if (right
->type
== EXPR_CALL
&& sym_name_is("strlen", right
->fn
)) {
207 str
= get_argument_from_call_expr(right
->args
, 0);
211 if (!strlen_left
&& !strlen_right
)
213 if (strlen_left
&& strlen_right
)
217 if (!get_value(right
, &val
))
221 if (!get_value(left
, &val
))
225 if (expr
->op
== SPECIAL_EQUAL
) {
226 set_true_false_states_expr(my_size_id
, str
, alloc_my_state(val
+ 1), NULL
);
229 if (expr
->op
== SPECIAL_NOTEQUAL
) {
230 set_true_false_states_expr(my_size_id
, str
, NULL
, alloc_my_state(val
+ 1));
236 case SPECIAL_UNSIGNED_LT
:
238 true_state
= alloc_my_state(val
);
240 false_state
= alloc_my_state(val
+ 1);
243 case SPECIAL_UNSIGNED_LTE
:
245 true_state
= alloc_my_state(val
+ 1);
247 false_state
= alloc_my_state(val
);
250 case SPECIAL_UNSIGNED_GTE
:
252 false_state
= alloc_my_state(val
);
254 true_state
= alloc_my_state(val
+ 1);
257 case SPECIAL_UNSIGNED_GT
:
259 false_state
= alloc_my_state(val
+ 1);
261 true_state
= alloc_my_state(val
);
264 set_true_false_states_expr(my_size_id
, str
, true_state
, false_state
);
267 static struct expression
*strip_ampersands(struct expression
*expr
)
271 if (expr
->type
!= EXPR_PREOP
)
275 type
= get_type(expr
->unop
);
276 if (!type
|| type
->type
!= SYM_ARRAY
)
281 static void match_array_assignment(struct expression
*expr
)
283 struct expression
*left
;
284 struct expression
*right
;
289 left
= strip_expr(expr
->left
);
290 right
= strip_expr(expr
->right
);
291 right
= strip_ampersands(right
);
292 array_size
= get_array_size_bytes(right
);
294 set_state_expr(my_size_id
, left
, alloc_my_state(array_size
));
297 static void match_malloc(const char *fn
, struct expression
*expr
, void *unused
)
299 struct expression
*right
;
300 struct expression
*arg
;
303 right
= strip_expr(expr
->right
);
304 arg
= get_argument_from_call_expr(right
->args
, 0);
305 if (!get_implied_value(arg
, &bytes
))
307 set_state_expr(my_size_id
, expr
->left
, alloc_my_state(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_my_state(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 void register_buf_size(int id
)
352 add_hook(&match_array_assignment
, ASSIGNMENT_HOOK
);
353 add_hook(&match_strlen_condition
, CONDITION_HOOK
);
354 add_function_assign_hook("malloc", &match_malloc
, NULL
);
355 add_function_assign_hook("calloc", &match_calloc
, NULL
);
356 add_function_assign_hook("strlen", &match_strlen
, NULL
);
357 if (option_project
== PROJ_KERNEL
) {
358 add_function_assign_hook("kmalloc", &match_malloc
, NULL
);
359 add_function_assign_hook("kzalloc", &match_malloc
, NULL
);
360 add_function_assign_hook("vmalloc", &match_malloc
, NULL
);
361 add_function_assign_hook("__vmalloc", &match_malloc
, NULL
);
362 add_function_assign_hook("kcalloc", &match_calloc
, NULL
);
363 add_function_assign_hook("drm_malloc_ab", &match_calloc
, NULL
);
364 add_function_assign_hook("drm_calloc_large", &match_calloc
, NULL
);
368 void register_strlen(int id
)