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(llong_list
, long long);
29 static struct llong_list
*num_list
;
30 static struct string_list
*op_list
;
32 static void push_val(long long val
)
36 p
= malloc(sizeof(*p
));
38 add_ptr_list(&num_list
, p
);
41 static long long pop_val()
48 p
= last_ptr_list((struct ptr_list
*)num_list
);
49 delete_ptr_list_last((struct ptr_list
**)&num_list
);
56 static void push_op(char c
)
62 add_ptr_list(&op_list
, p
);
71 sm_msg("internal smatch error %s", __func__
);
75 p
= last_ptr_list((struct ptr_list
*)op_list
);
77 delete_ptr_list_last((struct ptr_list
**)&op_list
);
84 static int op_precedence(char c
)
98 static int top_op_precedence()
105 p
= last_ptr_list((struct ptr_list
*)op_list
);
106 return op_precedence(p
[0]);
109 static long long do_op(long long left
, char op
, long long right
)
126 static void pop_until(char c
)
129 long long left
, right
, res
;
131 while (top_op_precedence() && op_precedence(c
) <= top_op_precedence()) {
135 res
= do_op(left
, op
, right
);
140 static int get_implied_param(struct expression
*call
, int param
, long long *val
)
142 struct expression
*arg
;
144 arg
= get_argument_from_call_expr(call
->args
, param
);
145 return get_implied_value(arg
, val
);
148 static int read_number(struct expression
*call
, char *p
, char **end
, long long *val
)
157 param
= strtol(p
, &p
, 10);
158 if (!get_implied_param(call
, param
, val
))
162 *val
= strtoll(p
, end
, 10);
169 static char *read_op(char *p
)
185 int parse_call_math(struct expression
*call
, char *math
, long long *val
)
190 /* try to implement shunting yard algorithm. */
195 sm_msg("parsing %s", c
);
197 /* read a number and push it onto the number stack */
198 if (!read_number(call
, c
, &c
, &tmp
))
203 sm_msg("val = %lld remaining = %s", tmp
, c
);
213 sm_msg("op = %c remaining = %s", *c
, c
);
224 pop_until(0); /* discard stack */
228 static struct smatch_state
*alloc_state_sname(char *sname
)
230 struct smatch_state
*state
;
232 state
= __alloc_smatch_state(0);
234 state
->data
= INT_PTR(1);
238 static int get_arg_number(struct expression
*expr
)
244 expr
= strip_expr(expr
);
245 if (expr
->type
!= EXPR_SYMBOL
)
250 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
254 } END_FOR_EACH_PTR(arg
);
259 static int format_expr_helper(char *buf
, int remaining
, struct expression
*expr
)
267 if (expr
->type
== EXPR_BINOP
) {
268 ret
= format_expr_helper(cur
, remaining
, expr
->left
);
276 ret
= snprintf(cur
, remaining
, " %s ", show_special(expr
->op
));
282 ret
= format_expr_helper(cur
, remaining
, expr
->right
);
292 val
= get_arg_number(expr
);
294 ret
= snprintf(cur
, remaining
, "<%lld>", val
);
301 if (get_implied_value(expr
, &val
)) {
302 ret
= snprintf(cur
, remaining
, "%lld", val
);
312 static char *format_expr(struct expression
*expr
)
317 ret
= format_expr_helper(buf
, sizeof(buf
), expr
);
321 return alloc_sname(buf
);
324 static void match_alloc(const char *fn
, struct expression
*expr
, void *_size_arg
)
326 int size_arg
= PTR_INT(_size_arg
);
327 struct expression
*right
;
328 struct expression
*size_expr
;
331 right
= strip_expr(expr
->right
);
332 size_expr
= get_argument_from_call_expr(right
->args
, size_arg
);
334 sname
= format_expr(size_expr
);
337 set_state_expr(my_id
, expr
->left
, alloc_state_sname(sname
));
340 static char *swap_format(struct expression
*call
, char *format
)
342 static char buf
[256];
345 struct expression
*arg
;
350 if (format
[0] == '<' && format
[2] == '>' && format
[3] == '\0') {
351 param
= strtol(format
+ 1, NULL
, 10);
352 arg
= get_argument_from_call_expr(call
->args
, param
);
355 return format_expr(arg
);
364 param
= strtol(p
, &p
, 10);
368 arg
= get_argument_from_call_expr(call
->args
, param
);
371 param
= get_arg_number(arg
);
373 ret
= snprintf(out
, buf
+ sizeof(buf
) - out
, "<%ld>", param
);
375 if (out
>= buf
+ sizeof(buf
))
377 } else if (get_implied_value(arg
, &val
)) {
378 ret
= snprintf(out
, buf
+ sizeof(buf
) - out
, "%lld", val
);
380 if (out
>= buf
+ sizeof(buf
))
392 return alloc_sname(buf
);
395 static char *buf_size_recipe
;
396 static int db_buf_size_callback(void *unused
, int argc
, char **argv
, char **azColName
)
401 if (!buf_size_recipe
)
402 buf_size_recipe
= alloc_sname(argv
[0]);
403 else if (strcmp(buf_size_recipe
, argv
[0]) != 0)
404 buf_size_recipe
= alloc_sname("invalid");
408 static char *get_allocation_recipe_from_call(struct expression
*expr
)
411 static char sql_filter
[1024];
414 expr
= strip_expr(expr
);
415 if (expr
->fn
->type
!= EXPR_SYMBOL
)
417 sym
= expr
->fn
->symbol
;
421 for (i
= 0; i
< ARRAY_SIZE(alloc_functions
); i
++) {
422 if (strcmp(sym
->ident
->name
, alloc_functions
[i
].func
) == 0) {
425 snprintf(buf
, sizeof(buf
), "<%d>", alloc_functions
[i
].param
);
426 buf_size_recipe
= alloc_sname(buf
);
427 return swap_format(expr
, buf_size_recipe
);
431 if (sym
->ctype
.modifiers
& MOD_STATIC
) {
432 snprintf(sql_filter
, 1024, "file = '%s' and function = '%s';",
433 get_filename(), sym
->ident
->name
);
435 snprintf(sql_filter
, 1024, "function = '%s' and static = 0;",
439 buf_size_recipe
= NULL
;
440 run_sql(db_buf_size_callback
, "select value from return_states where type=%d and %s",
441 BUF_SIZE
, sql_filter
);
442 if (!buf_size_recipe
|| strcmp(buf_size_recipe
, "invalid") == 0)
444 return swap_format(expr
, buf_size_recipe
);
447 static void match_call_assignment(struct expression
*expr
)
451 sname
= get_allocation_recipe_from_call(expr
->right
);
454 set_state_expr(my_id
, expr
->left
, alloc_state_sname(sname
));
457 static void match_returns_call(struct expression
*call
)
460 struct range_list
*rl
;
462 sname
= get_allocation_recipe_from_call(call
);
464 sm_msg("sname = %s", sname
);
467 get_implied_range_list(call
, &rl
);
468 sm_msg("info: return_allocation %d '%s' '%s' %s",
469 get_return_id(), show_ranges(rl
), sname
, global_static());
472 static void match_return(struct expression
*expr
)
474 struct smatch_state
*state
;
475 struct range_list
*rl
;
477 expr
= strip_expr(expr
);
481 if (expr
->type
== EXPR_CALL
) {
482 match_returns_call(expr
);
486 state
= get_state_expr(my_id
, expr
);
487 if (!state
|| !state
->data
)
489 get_implied_range_list(expr
, &rl
);
490 sm_msg("info: return_allocation %d '%s' '%s' %s",
491 get_return_id(), show_ranges(rl
), state
->name
, global_static());
494 void register_parse_call_math(int id
)
501 for (i
= 0; i
< ARRAY_SIZE(alloc_functions
); i
++)
502 add_function_assign_hook(alloc_functions
[i
].func
, &match_alloc
,
503 INT_PTR(alloc_functions
[i
].param
));
504 add_hook(&match_call_assignment
, CALL_ASSIGNMENT_HOOK
);
505 add_hook(&match_return
, RETURN_HOOK
);