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 void discard_stacks()
136 static int get_implied_param(struct expression
*call
, int param
, sval_t
*sval
)
138 struct expression
*arg
;
140 arg
= get_argument_from_call_expr(call
->args
, param
);
141 return get_implied_value(arg
, sval
);
144 static int read_number(struct expression
*call
, char *p
, char **end
, sval_t
*sval
)
153 param
= strtol(p
, &p
, 10);
154 if (!get_implied_param(call
, param
, sval
))
158 sval
->type
= &llong_ctype
;
159 sval
->value
= strtoll(p
, end
, 10);
166 static char *read_op(char *p
)
182 int parse_call_math(struct expression
*call
, char *math
, sval_t
*sval
)
187 /* try to implement shunting yard algorithm. */
192 sm_msg("parsing %s", c
);
194 /* read a number and push it onto the number stack */
195 if (!read_number(call
, c
, &c
, &tmp
))
200 sm_msg("val = %s remaining = %s", sval_to_str(tmp
), c
);
210 sm_msg("op = %c remaining = %s", *c
, c
);
225 static struct smatch_state
*alloc_state_sname(char *sname
)
227 struct smatch_state
*state
;
229 state
= __alloc_smatch_state(0);
231 state
->data
= INT_PTR(1);
235 static int get_arg_number(struct expression
*expr
)
241 expr
= strip_expr(expr
);
242 if (expr
->type
!= EXPR_SYMBOL
)
247 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
251 } END_FOR_EACH_PTR(arg
);
256 static int format_expr_helper(char *buf
, int remaining
, struct expression
*expr
)
268 if (expr
->type
== EXPR_BINOP
) {
269 ret
= format_expr_helper(cur
, remaining
, expr
->left
);
277 ret
= snprintf(cur
, remaining
, " %s ", show_special(expr
->op
));
283 ret
= format_expr_helper(cur
, remaining
, expr
->right
);
293 arg
= get_arg_number(expr
);
295 ret
= snprintf(cur
, remaining
, "<%d>", arg
);
302 if (get_implied_value(expr
, &sval
)) {
303 ret
= snprintf(cur
, remaining
, "%s", sval_to_str(sval
));
313 static char *format_expr(struct expression
*expr
)
318 ret
= format_expr_helper(buf
, sizeof(buf
), expr
);
322 return alloc_sname(buf
);
325 static void match_alloc(const char *fn
, struct expression
*expr
, void *_size_arg
)
327 int size_arg
= PTR_INT(_size_arg
);
328 struct expression
*right
;
329 struct expression
*size_expr
;
332 right
= strip_expr(expr
->right
);
333 size_expr
= get_argument_from_call_expr(right
->args
, size_arg
);
335 sname
= format_expr(size_expr
);
338 set_state_expr(my_id
, expr
->left
, alloc_state_sname(sname
));
341 static char *swap_format(struct expression
*call
, char *format
)
343 static char buf
[256];
346 struct expression
*arg
;
351 if (format
[0] == '<' && format
[2] == '>' && format
[3] == '\0') {
352 param
= strtol(format
+ 1, NULL
, 10);
353 arg
= get_argument_from_call_expr(call
->args
, param
);
356 return format_expr(arg
);
365 param
= strtol(p
, &p
, 10);
369 arg
= get_argument_from_call_expr(call
->args
, param
);
372 param
= get_arg_number(arg
);
374 ret
= snprintf(out
, buf
+ sizeof(buf
) - out
, "<%ld>", param
);
376 if (out
>= buf
+ sizeof(buf
))
378 } else if (get_implied_value(arg
, &sval
)) {
379 ret
= snprintf(out
, buf
+ sizeof(buf
) - out
, "%s", sval_to_str(sval
));
381 if (out
>= buf
+ sizeof(buf
))
393 return alloc_sname(buf
);
396 static char *buf_size_recipe
;
397 static int db_buf_size_callback(void *unused
, int argc
, char **argv
, char **azColName
)
402 if (!buf_size_recipe
)
403 buf_size_recipe
= alloc_sname(argv
[0]);
404 else if (strcmp(buf_size_recipe
, argv
[0]) != 0)
405 buf_size_recipe
= alloc_sname("invalid");
409 static char *get_allocation_recipe_from_call(struct expression
*expr
)
412 static char sql_filter
[1024];
415 expr
= strip_expr(expr
);
416 if (expr
->fn
->type
!= EXPR_SYMBOL
)
418 sym
= expr
->fn
->symbol
;
422 for (i
= 0; i
< ARRAY_SIZE(alloc_functions
); i
++) {
423 if (strcmp(sym
->ident
->name
, alloc_functions
[i
].func
) == 0) {
426 snprintf(buf
, sizeof(buf
), "<%d>", alloc_functions
[i
].param
);
427 buf_size_recipe
= alloc_sname(buf
);
428 return swap_format(expr
, buf_size_recipe
);
432 if (sym
->ctype
.modifiers
& MOD_STATIC
) {
433 snprintf(sql_filter
, 1024, "file = '%s' and function = '%s';",
434 get_filename(), sym
->ident
->name
);
436 snprintf(sql_filter
, 1024, "function = '%s' and static = 0;",
440 buf_size_recipe
= NULL
;
441 run_sql(db_buf_size_callback
, "select value from return_states where type=%d and %s",
442 BUF_SIZE
, sql_filter
);
443 if (!buf_size_recipe
|| strcmp(buf_size_recipe
, "invalid") == 0)
445 return swap_format(expr
, buf_size_recipe
);
448 static void match_call_assignment(struct expression
*expr
)
452 sname
= get_allocation_recipe_from_call(expr
->right
);
455 set_state_expr(my_id
, expr
->left
, alloc_state_sname(sname
));
458 static void match_returns_call(int return_id
, char *return_ranges
, struct expression
*call
)
462 sname
= get_allocation_recipe_from_call(call
);
464 sm_msg("sname = %s", sname
);
468 sql_insert_return_states(return_id
, return_ranges
, BUF_SIZE
, -1, "",
472 static void print_returned_allocations(int return_id
, char *return_ranges
, struct expression
*expr
)
474 struct smatch_state
*state
;
478 expr
= strip_expr(expr
);
482 if (expr
->type
== EXPR_CALL
) {
483 match_returns_call(return_id
, return_ranges
, expr
);
487 name
= expr_to_var_sym(expr
, &sym
);
491 state
= get_state(my_id
, name
, sym
);
492 if (!state
|| !state
->data
)
495 sql_insert_return_states(return_id
, return_ranges
, BUF_SIZE
, -1, "",
501 void register_parse_call_math(int id
)
507 for (i
= 0; i
< ARRAY_SIZE(alloc_functions
); i
++)
508 add_function_assign_hook(alloc_functions
[i
].func
, &match_alloc
,
509 INT_PTR(alloc_functions
[i
].param
));
510 add_hook(&match_call_assignment
, CALL_ASSIGNMENT_HOOK
);
511 add_returned_state_callback(print_returned_allocations
);