2 * Copyright (C) 2012 Oracle.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
19 #include "smatch_slist.h"
20 #include "smatch_extra.h"
27 } alloc_functions
[] = {
32 {"__vmalloc_node", 0},
35 static struct range_list_stack
*rl_stack
;
36 static struct string_list
*op_list
;
38 static void push_op(char c
)
44 add_ptr_list(&op_list
, p
);
47 static char pop_op(void)
53 sm_msg("internal smatch error %s", __func__
);
57 p
= last_ptr_list((struct ptr_list
*)op_list
);
59 delete_ptr_list_last((struct ptr_list
**)&op_list
);
66 static int op_precedence(char c
)
80 static int top_op_precedence(void)
87 p
= last_ptr_list((struct ptr_list
*)op_list
);
88 return op_precedence(p
[0]);
91 static void rl_pop_until(char c
)
94 struct range_list
*left
, *right
;
95 struct range_list
*res
;
97 while (top_op_precedence() && op_precedence(c
) <= top_op_precedence()) {
99 right
= pop_rl(&rl_stack
);
100 left
= pop_rl(&rl_stack
);
101 res
= rl_binop(left
, op
, right
);
102 push_rl(&rl_stack
, res
);
106 static void rl_discard_stacks(void)
114 static int read_rl_from_var(struct expression
*call
, char *p
, char **end
, struct range_list
**rl
)
116 struct expression
*arg
;
117 struct smatch_state
*state
;
125 param
= strtol(p
, &p
, 10);
127 arg
= get_argument_from_call_expr(call
->args
, param
);
131 if (*p
!= '-' && *p
!= '.') {
132 get_absolute_rl(arg
, rl
);
137 *end
= strchr(p
, ' ');
139 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
140 arg
= strip_expr(arg
->unop
);
148 name
= expr_to_var_sym(arg
, &sym
);
151 snprintf(buf
, sizeof(buf
), "%s%s", name
, star
? "->" : ".");
154 if (*end
- p
+ strlen(buf
) >= sizeof(buf
))
156 strncat(buf
, p
, *end
- p
);
158 state
= get_state(SMATCH_EXTRA
, buf
, sym
);
161 *rl
= estate_rl(state
);
165 static int read_var_num(struct expression
*call
, char *p
, char **end
, struct range_list
**rl
)
173 return read_rl_from_var(call
, p
, end
, rl
);
175 sval
.type
= &llong_ctype
;
176 sval
.value
= strtoll(p
, end
, 10);
179 *rl
= alloc_rl(sval
, sval
);
183 static char *read_op(char *p
)
199 int parse_call_math_rl(struct expression
*call
, char *math
, struct range_list
**rl
)
201 struct range_list
*tmp
;
204 /* try to implement shunting yard algorithm. */
209 sm_msg("parsing %s", c
);
211 /* read a number and push it onto the number stack */
212 if (!read_var_num(call
, c
, &c
, &tmp
))
214 push_rl(&rl_stack
, tmp
);
217 sm_msg("val = %s remaining = %s", show_rl(tmp
), c
);
221 if (*c
== ']' && *(c
+ 1) == '\0')
229 sm_msg("op = %c remaining = %s", *c
, c
);
237 *rl
= pop_rl(&rl_stack
);
244 int parse_call_math(struct expression
*call
, char *math
, sval_t
*sval
)
246 struct range_list
*rl
;
248 if (!parse_call_math_rl(call
, math
, &rl
))
250 if (!rl_to_sval(rl
, sval
))
255 static struct smatch_state
*alloc_state_sname(char *sname
)
257 struct smatch_state
*state
;
259 state
= __alloc_smatch_state(0);
261 state
->data
= INT_PTR(1);
265 static int get_arg_number(struct expression
*expr
)
271 expr
= strip_expr(expr
);
272 if (expr
->type
!= EXPR_SYMBOL
)
277 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
281 } END_FOR_EACH_PTR(arg
);
286 static int format_name_sym_helper(char *buf
, int remaining
, char *name
, struct symbol
*sym
)
293 if (!name
|| !sym
|| !sym
->ident
)
295 arg
= get_param_num_from_sym(sym
);
298 if (param_was_set_var_sym(name
, sym
))
301 param_name
= sym
->ident
->name
;
302 name_len
= strlen(param_name
);
304 if (name
[name_len
] == '\0')
305 ret
= snprintf(buf
, remaining
, "$%d", arg
);
306 else if (name
[name_len
] == '-')
307 ret
= snprintf(buf
, remaining
, "$%d%s", arg
, name
+ name_len
);
322 static int format_variable_helper(char *buf
, int remaining
, struct expression
*expr
)
327 name
= expr_to_var_sym(expr
, &sym
);
328 return format_name_sym_helper(buf
, remaining
, name
, sym
);
331 static int format_call_to_param_mapping(char *buf
, int remaining
, struct expression
*expr
)
336 name
= map_call_to_param_name_sym(expr
, &sym
);
337 return format_name_sym_helper(buf
, remaining
, name
, sym
);
340 static int format_expr_helper(char *buf
, int remaining
, struct expression
*expr
)
351 if (expr
->type
== EXPR_BINOP
) {
352 ret
= format_expr_helper(cur
, remaining
, expr
->left
);
360 ret
= snprintf(cur
, remaining
, " %s ", show_special(expr
->op
));
366 ret
= format_expr_helper(cur
, remaining
, expr
->right
);
376 if (get_implied_value(expr
, &sval
)) {
377 ret
= snprintf(cur
, remaining
, "%s", sval_to_str(sval
));
384 if (expr
->type
== EXPR_CALL
)
385 return format_call_to_param_mapping(cur
, remaining
, expr
);
387 return format_variable_helper(cur
, remaining
, expr
);
390 static char *format_expr(struct expression
*expr
)
395 ret
= format_expr_helper(buf
, sizeof(buf
), expr
);
399 return alloc_sname(buf
);
402 char *get_value_in_terms_of_parameter_math(struct expression
*expr
)
404 struct expression
*tmp
;
409 tmp
= get_assigned_expr(expr
);
413 if (get_implied_value(expr
, &dummy
))
416 ret
= format_expr_helper(buf
, sizeof(buf
), expr
);
420 return alloc_sname(buf
);
423 char *get_value_in_terms_of_parameter_math_var_sym(const char *name
, struct symbol
*sym
)
425 struct expression
*expr
;
429 expr
= get_assigned_expr_name_sym(name
, sym
);
433 ret
= format_expr_helper(buf
, sizeof(buf
), expr
);
437 return alloc_sname(buf
);
441 static void match_alloc(const char *fn
, struct expression
*expr
, void *_size_arg
)
443 int size_arg
= PTR_INT(_size_arg
);
444 struct expression
*right
;
445 struct expression
*size_expr
;
448 right
= strip_expr(expr
->right
);
449 size_expr
= get_argument_from_call_expr(right
->args
, size_arg
);
451 sname
= format_expr(size_expr
);
454 set_state_expr(my_id
, expr
->left
, alloc_state_sname(sname
));
457 static char *swap_format(struct expression
*call
, char *format
)
462 struct expression
*arg
;
467 if (format
[0] == '$' && format
[2] == '\0') {
468 param
= strtol(format
+ 1, NULL
, 10);
469 arg
= get_argument_from_call_expr(call
->args
, param
);
472 return format_expr(arg
);
481 param
= strtol(p
, &p
, 10);
482 arg
= get_argument_from_call_expr(call
->args
, param
);
485 param
= get_arg_number(arg
);
487 ret
= snprintf(out
, buf
+ sizeof(buf
) - out
, "$%ld", param
);
489 if (out
>= buf
+ sizeof(buf
))
491 } else if (get_implied_value(arg
, &sval
)) {
492 ret
= snprintf(out
, buf
+ sizeof(buf
) - out
, "%s", sval_to_str(sval
));
494 if (out
>= buf
+ sizeof(buf
))
507 return alloc_sname(buf
);
510 static char *buf_size_recipe
;
511 static int db_buf_size_callback(void *unused
, int argc
, char **argv
, char **azColName
)
516 if (!buf_size_recipe
)
517 buf_size_recipe
= alloc_sname(argv
[0]);
518 else if (strcmp(buf_size_recipe
, argv
[0]) != 0)
519 buf_size_recipe
= alloc_sname("invalid");
523 static char *get_allocation_recipe_from_call(struct expression
*expr
)
526 static char sql_filter
[1024];
529 if (is_fake_call(expr
))
531 expr
= strip_expr(expr
);
532 if (expr
->fn
->type
!= EXPR_SYMBOL
)
534 sym
= expr
->fn
->symbol
;
538 for (i
= 0; i
< ARRAY_SIZE(alloc_functions
); i
++) {
539 if (strcmp(sym
->ident
->name
, alloc_functions
[i
].func
) == 0) {
542 snprintf(buf
, sizeof(buf
), "$%d", alloc_functions
[i
].param
);
543 buf_size_recipe
= alloc_sname(buf
);
544 return swap_format(expr
, buf_size_recipe
);
548 if (sym
->ctype
.modifiers
& MOD_STATIC
) {
549 snprintf(sql_filter
, 1024, "file = '%s' and function = '%s';",
550 get_filename(), sym
->ident
->name
);
552 snprintf(sql_filter
, 1024, "function = '%s' and static = 0;",
556 buf_size_recipe
= NULL
;
557 run_sql(db_buf_size_callback
, NULL
,
558 "select value from return_states where type=%d and %s",
559 BUF_SIZE
, sql_filter
);
560 if (!buf_size_recipe
|| strcmp(buf_size_recipe
, "invalid") == 0)
562 return swap_format(expr
, buf_size_recipe
);
565 static void match_call_assignment(struct expression
*expr
)
569 sname
= get_allocation_recipe_from_call(expr
->right
);
572 set_state_expr(my_id
, expr
->left
, alloc_state_sname(sname
));
575 static void match_returns_call(int return_id
, char *return_ranges
, struct expression
*call
)
579 sname
= get_allocation_recipe_from_call(call
);
581 sm_msg("sname = %s", sname
);
585 sql_insert_return_states(return_id
, return_ranges
, BUF_SIZE
, -1, "",
589 static void print_returned_allocations(int return_id
, char *return_ranges
, struct expression
*expr
)
591 struct smatch_state
*state
;
595 expr
= strip_expr(expr
);
599 if (expr
->type
== EXPR_CALL
) {
600 match_returns_call(return_id
, return_ranges
, expr
);
604 name
= expr_to_var_sym(expr
, &sym
);
608 state
= get_state(my_id
, name
, sym
);
609 if (!state
|| !state
->data
)
612 sql_insert_return_states(return_id
, return_ranges
, BUF_SIZE
, -1, "",
618 void register_parse_call_math(int id
)
624 for (i
= 0; i
< ARRAY_SIZE(alloc_functions
); i
++)
625 add_function_assign_hook(alloc_functions
[i
].func
, &match_alloc
,
626 INT_PTR(alloc_functions
[i
].param
));
627 add_hook(&match_call_assignment
, CALL_ASSIGNMENT_HOOK
);
628 add_split_return_callback(print_returned_allocations
);