2 * Copyright (C) 2019 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 * There are a bunch of allocation functions where we allocate some memory,
20 * set up some struct members and then return the allocated memory. One
21 * nice thing about this is that we just one pointer to the allocated memory
22 * so what we can do is we can generate a mtag alias for it in the caller.
26 #include "smatch_extra.h"
27 #include "smatch_slist.h"
33 struct alloc_info
*alloc_funcs
;
35 struct alloc_info kernel_allocation_funcs
[] = {
44 {"kmalloc_array", 0, 1},
48 {"dma_alloc_attrs", 1},
49 {"pci_alloc_consistent", 1},
50 {"pci_alloc_coherent", 1},
54 {"__alloc_bootmem", 0},
56 {"dma_alloc_contiguous", 1},
57 {"dma_alloc_coherent", 1},
61 struct alloc_info general_allocation_funcs
[] = {
69 static void pre_merge_hook(struct sm_state
*cur
, struct sm_state
*other
)
71 struct smatch_state
*state
;
74 state
= get_state(SMATCH_EXTRA
, cur
->name
, cur
->sym
);
75 if (estate_get_single_value(state
, &sval
) && sval
.value
== 0)
76 set_state(my_id
, cur
->name
, cur
->sym
, &undefined
);
79 static int fresh_callback(void *fresh
, int argc
, char **argv
, char **azColName
)
85 static int fresh_from_db(struct expression
*call
)
89 if (is_fake_call(call
))
92 /* for function pointers assume everything is used */
93 if (call
->fn
->type
!= EXPR_SYMBOL
)
96 run_sql(&fresh_callback
, &fresh
,
97 "select * from return_states where %s and type = %d and parameter = -1 and key = '$' limit 1;",
98 get_static_filter(call
->fn
->symbol
), FRESH_ALLOC
);
102 bool is_fresh_alloc_var_sym(const char *var
, struct symbol
*sym
)
104 return get_state(my_id
, var
, sym
) == &fresh
;
107 bool is_fresh_alloc(struct expression
*expr
)
115 if (get_implied_value_fast(expr
, &sval
) && sval
.value
== 0)
118 if (get_state_expr(my_id
, expr
) == &fresh
)
121 if (expr
->type
!= EXPR_CALL
)
123 if (fresh_from_db(expr
))
126 while (alloc_funcs
[++i
].fn
) {
127 if (sym_name_is(kernel_allocation_funcs
[i
].fn
, expr
->fn
))
133 static void record_alloc_func(int return_id
, char *return_ranges
, struct expression
*expr
)
135 if (!is_fresh_alloc(expr
))
137 sql_insert_return_states(return_id
, return_ranges
, FRESH_ALLOC
, -1, "$", "");
140 static void set_unfresh(struct expression
*expr
)
144 sm
= get_sm_state_expr(my_id
, expr
);
147 if (!slist_has_state(sm
->possible
, &fresh
))
149 // TODO call unfresh hooks
150 set_state_expr(my_id
, expr
, &undefined
);
153 static void match_assign(struct expression
*expr
)
155 set_unfresh(expr
->right
);
158 static void match_call(struct expression
*expr
)
160 struct expression
*arg
;
162 FOR_EACH_PTR(expr
->args
, arg
) {
164 } END_FOR_EACH_PTR(arg
);
167 static struct expression
*handled
;
168 static void set_fresh(struct expression
*expr
)
170 struct range_list
*rl
;
172 expr
= strip_expr(expr
);
173 if (expr
->type
!= EXPR_SYMBOL
)
178 get_absolute_rl(expr
, &rl
);
179 rl
= rl_intersection(rl
, valid_ptr_rl
);
182 set_state_expr(my_id
, expr
, &fresh
);
186 static void returns_fresh_alloc(struct expression
*expr
, int param
, char *key
, char *value
)
188 if (param
!= -1 || !key
|| strcmp(key
, "$") != 0)
190 if (expr
->type
!= EXPR_ASSIGNMENT
)
193 set_fresh(expr
->left
);
196 static void match_alloc(const char *fn
, struct expression
*expr
, void *_size_arg
)
198 set_fresh(expr
->left
);
201 void register_fresh_alloc(int id
)
207 if (option_project
== PROJ_KERNEL
)
208 alloc_funcs
= kernel_allocation_funcs
;
210 alloc_funcs
= general_allocation_funcs
;
213 while (alloc_funcs
[++i
].fn
)
214 add_function_assign_hook(alloc_funcs
[i
].fn
, &match_alloc
, 0);
216 add_split_return_callback(&record_alloc_func
);
217 select_return_states_hook(FRESH_ALLOC
, &returns_fresh_alloc
);
218 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
219 add_hook(&match_call
, FUNCTION_CALL_HOOK
);
221 add_pre_merge_hook(my_id
, &pre_merge_hook
);