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
[] = {
33 {"__vmalloc_node", 0},
36 static struct range_list_stack
*rl_stack
;
37 static struct string_list
*op_list
;
39 static void push_op(char c
)
45 add_ptr_list(&op_list
, p
);
48 static char pop_op(void)
54 sm_perror("%s: no op_list", __func__
);
58 p
= last_ptr_list((struct ptr_list
*)op_list
);
60 delete_ptr_list_last((struct ptr_list
**)&op_list
);
67 static int op_precedence(char c
)
81 static int top_op_precedence(void)
88 p
= last_ptr_list((struct ptr_list
*)op_list
);
89 return op_precedence(p
[0]);
92 static void rl_pop_until(char c
)
95 struct range_list
*left
, *right
;
96 struct range_list
*res
;
98 while (top_op_precedence() && op_precedence(c
) <= top_op_precedence()) {
100 right
= pop_rl(&rl_stack
);
101 left
= pop_rl(&rl_stack
);
102 res
= rl_binop(left
, op
, right
);
104 res
= alloc_whole_rl(&llong_ctype
);
105 push_rl(&rl_stack
, res
);
109 static void rl_discard_stacks(void)
117 static int read_rl_from_var(struct expression
*call
, const char *p
, const char **end
, struct range_list
**rl
)
119 struct expression
*arg
;
120 struct smatch_state
*state
;
127 if (!call
|| call
->type
!= EXPR_CALL
)
130 param
= strtol(p
, (char **)&p
, 10);
132 arg
= get_argument_from_call_expr(call
->args
, param
);
136 if (*p
!= '-' && *p
!= '.') {
137 get_absolute_rl(arg
, rl
);
142 *end
= strchr(p
, ' ');
144 if (arg
->type
== EXPR_PREOP
&& arg
->op
== '&') {
145 arg
= strip_expr(arg
->unop
);
153 name
= expr_to_var_sym(arg
, &sym
);
156 snprintf(buf
, sizeof(buf
), "%s%s", name
, star
? "->" : ".");
159 if (*end
- p
+ strlen(buf
) >= sizeof(buf
))
161 strncat(buf
, p
, *end
- p
);
163 state
= get_state(SMATCH_EXTRA
, buf
, sym
);
166 *rl
= estate_rl(state
);
170 static int read_var_num(struct expression
*call
, const char *p
, const char **end
, struct range_list
**rl
)
178 return read_rl_from_var(call
, p
, end
, rl
);
180 sval
.type
= &llong_ctype
;
181 sval
.value
= strtoll(p
, (char **)end
, 10);
184 *rl
= alloc_rl(sval
, sval
);
188 static const char *read_op(const char *p
)
204 int parse_call_math_rl(struct expression
*call
, const char *math
, struct range_list
**rl
)
206 struct range_list
*tmp
;
209 /* try to implement shunting yard algorithm. */
214 sm_msg("parsing %s", c
);
216 /* read a number and push it onto the number stack */
217 if (!read_var_num(call
, c
, &c
, &tmp
))
219 push_rl(&rl_stack
, tmp
);
222 sm_msg("val = %s remaining = %s", show_rl(tmp
), c
);
226 if (*c
== ']' && *(c
+ 1) == '\0')
234 sm_msg("op = %c remaining = %s", *c
, c
);
242 *rl
= pop_rl(&rl_stack
);
249 int parse_call_math(struct expression
*call
, char *math
, sval_t
*sval
)
251 struct range_list
*rl
;
253 if (!parse_call_math_rl(call
, math
, &rl
))
255 if (!rl_to_sval(rl
, sval
))
260 static struct smatch_state
*alloc_state_sname(char *sname
)
262 struct smatch_state
*state
;
264 state
= __alloc_smatch_state(0);
266 state
->data
= INT_PTR(1);
270 static int get_arg_number(struct expression
*expr
)
276 expr
= strip_expr(expr
);
277 if (expr
->type
!= EXPR_SYMBOL
)
282 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
286 } END_FOR_EACH_PTR(arg
);
291 static int format_name_sym_helper(char *buf
, int remaining
, char *name
, struct symbol
*sym
)
299 if (!name
|| !sym
|| !sym
->ident
)
301 arg
= get_param_num_from_sym(sym
);
304 if (param_was_set_var_sym(name
, sym
))
307 param_name
= sym
->ident
->name
;
309 name_len
= strlen(param_name
);
312 ret
= snprintf(buf
, remaining
, "$%d", arg
);
313 else if (name_len
< len
&& name
[name_len
] == '-')
314 ret
= snprintf(buf
, remaining
, "$%d%s", arg
, name
+ name_len
);
329 static int format_variable_helper(char *buf
, int remaining
, struct expression
*expr
)
334 name
= expr_to_var_sym(expr
, &sym
);
335 if (param_was_set_var_sym(name
, sym
))
337 return format_name_sym_helper(buf
, remaining
, name
, sym
);
340 static int format_call_to_param_mapping(char *buf
, int remaining
, struct expression
*expr
)
345 name
= map_call_to_param_name_sym(expr
, &sym
);
346 if (param_was_set_var_sym(name
, sym
))
348 return format_name_sym_helper(buf
, remaining
, name
, sym
);
351 static int is_mtag_sval(sval_t sval
)
353 if (!is_ptr_type(sval
.type
))
355 if (sval_cmp(sval
, valid_ptr_min_sval
) >= 0 &&
356 sval_cmp(sval
, valid_ptr_max_sval
) <= 0)
361 static int format_expr_helper(char *buf
, int remaining
, struct expression
*expr
)
372 if (expr
->type
== EXPR_BINOP
) {
373 ret
= format_expr_helper(cur
, remaining
, expr
->left
);
381 ret
= snprintf(cur
, remaining
, " %s ", show_special(expr
->op
));
387 ret
= format_expr_helper(cur
, remaining
, expr
->right
);
397 if (!param_was_set(expr
) && get_implied_value(expr
, &sval
) && !is_mtag_sval(sval
)) {
398 ret
= snprintf(cur
, remaining
, "%s", sval_to_str(sval
));
405 if (expr
->type
== EXPR_CALL
)
406 return format_call_to_param_mapping(cur
, remaining
, expr
);
408 return format_variable_helper(cur
, remaining
, expr
);
411 static char *format_expr(struct expression
*expr
)
416 ret
= format_expr_helper(buf
, sizeof(buf
), expr
);
420 return alloc_sname(buf
);
423 char *get_value_in_terms_of_parameter_math(struct expression
*expr
)
425 struct expression
*tmp
;
430 tmp
= get_assigned_expr(expr
);
433 if (param_was_set(expr
))
436 if (get_implied_value(expr
, &dummy
))
439 ret
= format_expr_helper(buf
, sizeof(buf
), expr
);
443 return alloc_sname(buf
);
446 char *get_value_in_terms_of_parameter_math_var_sym(const char *name
, struct symbol
*sym
)
448 struct expression
*tmp
, *expr
;
454 expr
= get_assigned_expr_name_sym(name
, sym
);
457 while ((tmp
= get_assigned_expr(expr
))) {
458 expr
= strip_expr(tmp
);
463 if (get_implied_value(expr
, &sval
))
466 ret
= format_expr_helper(buf
, sizeof(buf
), expr
);
470 return alloc_sname(buf
);
474 static void match_alloc(const char *fn
, struct expression
*expr
, void *_size_arg
)
476 int size_arg
= PTR_INT(_size_arg
);
477 struct expression
*right
;
478 struct expression
*size_expr
;
481 right
= strip_expr(expr
->right
);
482 size_expr
= get_argument_from_call_expr(right
->args
, size_arg
);
484 sname
= format_expr(size_expr
);
487 set_state_expr(my_id
, expr
->left
, alloc_state_sname(sname
));
490 static char *swap_format(struct expression
*call
, char *format
)
495 struct expression
*arg
;
500 if (format
[0] == '$' && format
[2] == '\0') {
501 param
= strtol(format
+ 1, NULL
, 10);
502 arg
= get_argument_from_call_expr(call
->args
, param
);
505 return format_expr(arg
);
514 param
= strtol(p
, (char **)&p
, 10);
515 arg
= get_argument_from_call_expr(call
->args
, param
);
518 param
= get_arg_number(arg
);
520 ret
= snprintf(out
, buf
+ sizeof(buf
) - out
, "$%ld", param
);
522 if (out
>= buf
+ sizeof(buf
))
524 } else if (get_implied_value(arg
, &sval
)) {
525 ret
= snprintf(out
, buf
+ sizeof(buf
) - out
, "%s", sval_to_str(sval
));
527 if (out
>= buf
+ sizeof(buf
))
540 return alloc_sname(buf
);
543 static char *buf_size_recipe
;
544 static int db_buf_size_callback(void *unused
, int argc
, char **argv
, char **azColName
)
549 if (!buf_size_recipe
)
550 buf_size_recipe
= alloc_sname(argv
[0]);
551 else if (strcmp(buf_size_recipe
, argv
[0]) != 0)
552 buf_size_recipe
= alloc_sname("invalid");
556 static char *get_allocation_recipe_from_call(struct expression
*expr
)
559 static char sql_filter
[1024];
562 if (is_fake_call(expr
))
564 expr
= strip_expr(expr
);
565 if (expr
->fn
->type
!= EXPR_SYMBOL
)
567 sym
= expr
->fn
->symbol
;
571 for (i
= 0; i
< ARRAY_SIZE(alloc_functions
); i
++) {
572 if (strcmp(sym
->ident
->name
, alloc_functions
[i
].func
) == 0) {
575 snprintf(buf
, sizeof(buf
), "$%d", alloc_functions
[i
].param
);
576 buf_size_recipe
= alloc_sname(buf
);
577 return swap_format(expr
, buf_size_recipe
);
581 if (sym
->ctype
.modifiers
& MOD_STATIC
) {
582 snprintf(sql_filter
, 1024, "file = '%s' and function = '%s';",
583 get_filename(), sym
->ident
->name
);
585 snprintf(sql_filter
, 1024, "function = '%s' and static = 0;",
589 buf_size_recipe
= NULL
;
590 run_sql(db_buf_size_callback
, NULL
,
591 "select value from return_states where type=%d and %s",
592 BUF_SIZE
, sql_filter
);
593 if (!buf_size_recipe
|| strcmp(buf_size_recipe
, "invalid") == 0)
595 /* Known sizes should be handled in smatch_buf_size.c */
596 if (!strchr(buf_size_recipe
, '$'))
598 return swap_format(expr
, buf_size_recipe
);
601 static void match_call_assignment(struct expression
*expr
)
605 sname
= get_allocation_recipe_from_call(expr
->right
);
608 set_state_expr(my_id
, expr
->left
, alloc_state_sname(sname
));
611 const char *get_allocation_math(struct expression
*expr
)
613 struct expression
*tmp
;
614 struct smatch_state
*state
;
617 expr
= strip_expr(expr
);
618 while ((tmp
= get_assigned_expr(expr
))) {
619 if (cnt
++ > 5) /* assignments to self cause infinite loops */
621 expr
= strip_expr(tmp
);
626 if (expr
->type
== EXPR_CALL
)
627 return get_allocation_recipe_from_call(expr
);
629 state
= get_state_expr(my_id
, expr
);
630 if (!state
|| !state
->data
)
636 void register_parse_call_math(int id
)
642 set_dynamic_states(my_id
);
644 for (i
= 0; i
< ARRAY_SIZE(alloc_functions
); i
++)
645 add_function_assign_hook(alloc_functions
[i
].func
, &match_alloc
,
646 INT_PTR(alloc_functions
[i
].param
));
647 add_hook(&match_call_assignment
, CALL_ASSIGNMENT_HOOK
);