2 * smatch/smatch_parse_call_math.c
4 * Copyright (C) 2012 Oracle.
6 * Licensed under the Open Software License version 1.1
11 #include "smatch_slist.h"
12 #include "smatch_extra.h"
19 } alloc_functions
[] = {
24 {"__vmalloc_node", 0},
27 DECLARE_PTR_LIST(sval_list
, sval_t
);
29 static struct sval_list
*num_list
;
30 static struct string_list
*op_list
;
32 static void push_val(sval_t sval
)
36 p
= malloc(sizeof(*p
));
38 add_ptr_list(&num_list
, p
);
41 static sval_t
pop_val()
47 sm_msg("internal bug: %s popping empty list", __func__
);
48 ret
.type
= &llong_ctype
;
52 p
= last_ptr_list((struct ptr_list
*)num_list
);
53 delete_ptr_list_last((struct ptr_list
**)&num_list
);
60 static void push_op(char c
)
66 add_ptr_list(&op_list
, p
);
75 sm_msg("internal smatch error %s", __func__
);
79 p
= last_ptr_list((struct ptr_list
*)op_list
);
81 delete_ptr_list_last((struct ptr_list
**)&op_list
);
88 static int op_precedence(char c
)
102 static int top_op_precedence()
109 p
= last_ptr_list((struct ptr_list
*)op_list
);
110 return op_precedence(p
[0]);
113 static void pop_until(char c
)
119 while (top_op_precedence() && op_precedence(c
) <= top_op_precedence()) {
123 res
= sval_binop(left
, op
, right
);
128 static int get_implied_param(struct expression
*call
, int param
, sval_t
*sval
)
130 struct expression
*arg
;
132 arg
= get_argument_from_call_expr(call
->args
, param
);
133 return get_implied_value(arg
, sval
);
136 static int read_number(struct expression
*call
, char *p
, char **end
, sval_t
*sval
)
145 param
= strtol(p
, &p
, 10);
146 if (!get_implied_param(call
, param
, sval
))
150 sval
->type
= &llong_ctype
;
151 sval
->value
= strtoll(p
, end
, 10);
158 static char *read_op(char *p
)
174 int parse_call_math(struct expression
*call
, char *math
, sval_t
*sval
)
179 /* try to implement shunting yard algorithm. */
184 sm_msg("parsing %s", c
);
186 /* read a number and push it onto the number stack */
187 if (!read_number(call
, c
, &c
, &tmp
))
192 sm_msg("val = %s remaining = %s", sval_to_str(tmp
), c
);
202 sm_msg("op = %c remaining = %s", *c
, c
);
213 pop_until(0); /* discard stack */
217 static struct smatch_state
*alloc_state_sname(char *sname
)
219 struct smatch_state
*state
;
221 state
= __alloc_smatch_state(0);
223 state
->data
= INT_PTR(1);
227 static int get_arg_number(struct expression
*expr
)
233 expr
= strip_expr(expr
);
234 if (expr
->type
!= EXPR_SYMBOL
)
239 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
243 } END_FOR_EACH_PTR(arg
);
248 static int format_expr_helper(char *buf
, int remaining
, struct expression
*expr
)
257 if (expr
->type
== EXPR_BINOP
) {
258 ret
= format_expr_helper(cur
, remaining
, expr
->left
);
266 ret
= snprintf(cur
, remaining
, " %s ", show_special(expr
->op
));
272 ret
= format_expr_helper(cur
, remaining
, expr
->right
);
282 arg
= get_arg_number(expr
);
284 ret
= snprintf(cur
, remaining
, "<%d>", arg
);
291 if (get_implied_value(expr
, &sval
)) {
292 ret
= snprintf(cur
, remaining
, "%s", sval_to_str(sval
));
302 static char *format_expr(struct expression
*expr
)
307 ret
= format_expr_helper(buf
, sizeof(buf
), expr
);
311 return alloc_sname(buf
);
314 static void match_alloc(const char *fn
, struct expression
*expr
, void *_size_arg
)
316 int size_arg
= PTR_INT(_size_arg
);
317 struct expression
*right
;
318 struct expression
*size_expr
;
321 right
= strip_expr(expr
->right
);
322 size_expr
= get_argument_from_call_expr(right
->args
, size_arg
);
324 sname
= format_expr(size_expr
);
327 set_state_expr(my_id
, expr
->left
, alloc_state_sname(sname
));
330 static char *swap_format(struct expression
*call
, char *format
)
332 static char buf
[256];
335 struct expression
*arg
;
340 if (format
[0] == '<' && format
[2] == '>' && format
[3] == '\0') {
341 param
= strtol(format
+ 1, NULL
, 10);
342 arg
= get_argument_from_call_expr(call
->args
, param
);
345 return format_expr(arg
);
354 param
= strtol(p
, &p
, 10);
358 arg
= get_argument_from_call_expr(call
->args
, param
);
361 param
= get_arg_number(arg
);
363 ret
= snprintf(out
, buf
+ sizeof(buf
) - out
, "<%ld>", param
);
365 if (out
>= buf
+ sizeof(buf
))
367 } else if (get_implied_value(arg
, &sval
)) {
368 ret
= snprintf(out
, buf
+ sizeof(buf
) - out
, "%s", sval_to_str(sval
));
370 if (out
>= buf
+ sizeof(buf
))
382 return alloc_sname(buf
);
385 static char *buf_size_recipe
;
386 static int db_buf_size_callback(void *unused
, int argc
, char **argv
, char **azColName
)
391 if (!buf_size_recipe
)
392 buf_size_recipe
= alloc_sname(argv
[0]);
393 else if (strcmp(buf_size_recipe
, argv
[0]) != 0)
394 buf_size_recipe
= alloc_sname("invalid");
398 static char *get_allocation_recipe_from_call(struct expression
*expr
)
401 static char sql_filter
[1024];
404 expr
= strip_expr(expr
);
405 if (expr
->fn
->type
!= EXPR_SYMBOL
)
407 sym
= expr
->fn
->symbol
;
411 for (i
= 0; i
< ARRAY_SIZE(alloc_functions
); i
++) {
412 if (strcmp(sym
->ident
->name
, alloc_functions
[i
].func
) == 0) {
415 snprintf(buf
, sizeof(buf
), "<%d>", alloc_functions
[i
].param
);
416 buf_size_recipe
= alloc_sname(buf
);
417 return swap_format(expr
, buf_size_recipe
);
421 if (sym
->ctype
.modifiers
& MOD_STATIC
) {
422 snprintf(sql_filter
, 1024, "file = '%s' and function = '%s';",
423 get_filename(), sym
->ident
->name
);
425 snprintf(sql_filter
, 1024, "function = '%s' and static = 0;",
429 buf_size_recipe
= NULL
;
430 run_sql(db_buf_size_callback
, "select value from return_states where type=%d and %s",
431 BUF_SIZE
, sql_filter
);
432 if (!buf_size_recipe
|| strcmp(buf_size_recipe
, "invalid") == 0)
434 return swap_format(expr
, buf_size_recipe
);
437 static void match_call_assignment(struct expression
*expr
)
441 sname
= get_allocation_recipe_from_call(expr
->right
);
444 set_state_expr(my_id
, expr
->left
, alloc_state_sname(sname
));
447 static void match_returns_call(struct expression
*call
)
450 struct range_list
*rl
;
452 sname
= get_allocation_recipe_from_call(call
);
454 sm_msg("sname = %s", sname
);
457 get_implied_range_list(call
, &rl
);
458 rl
= cast_rl(cur_func_return_type(), rl
);
459 sm_msg("info: return_allocation %d '%s' '%s' %s",
460 get_return_id(), show_ranges(rl
), sname
, global_static());
463 static void match_return(struct expression
*expr
)
465 struct smatch_state
*state
;
466 struct range_list
*rl
;
468 expr
= strip_expr(expr
);
472 if (expr
->type
== EXPR_CALL
) {
473 match_returns_call(expr
);
477 state
= get_state_expr(my_id
, expr
);
478 if (!state
|| !state
->data
)
480 get_implied_range_list(expr
, &rl
);
481 rl
= cast_rl(cur_func_return_type(), rl
);
482 sm_msg("info: return_allocation %d '%s' '%s' %s",
483 get_return_id(), show_ranges(rl
), state
->name
, global_static());
486 void register_parse_call_math(int id
)
493 for (i
= 0; i
< ARRAY_SIZE(alloc_functions
); i
++)
494 add_function_assign_hook(alloc_functions
[i
].func
, &match_alloc
,
495 INT_PTR(alloc_functions
[i
].param
));
496 add_hook(&match_call_assignment
, CALL_ASSIGNMENT_HOOK
);
497 add_hook(&match_return
, RETURN_HOOK
);