conditions: pass SELECT_HOOK for select assignments
[smatch.git] / smatch_mtag.c
blobd2a8b0c274fbb142077f5973cf4e9ef164754fd5
1 /*
2 * Copyright (C) 2017 Oracle. All rights reserved.
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 * One problem that I have is that it's really hard to track how pointers are
20 * passed around. For example, it would be nice to know that the probe() and
21 * remove() functions get the same pci_dev pointer. It would be good to know
22 * what pointers we're passing to the open() and close() functions. But that
23 * information gets lost in a call tree full of function pointer calls.
25 * I think the first step is to start naming specific pointers. So when a
26 * pointer is allocated, then it gets a tag. So calls to kmalloc() generate a
27 * tag. But we might not use that, because there might be a better name like
28 * framebuffer_alloc(). The framebuffer_alloc() is interesting because there is
29 * one per driver and it's passed around to all the file operations.
31 * Perhaps we could make a list of functions like framebuffer_alloc() which take
32 * a size and say that those are the interesting alloc functions.
34 * Another place where we would maybe name the pointer is when they are passed
35 * to the probe(). Because that's an important pointer, since there is one
36 * per driver (sort of).
38 * My vision is that you could take a pointer and trace it back to a global. So
39 * I'm going to track that pointer_tag - 28 bytes takes you to another pointer
40 * tag. You could follow that one back and so on. Also when we pass a pointer
41 * to a function that would be recorded as sort of a link or path or something.
45 #include "smatch.h"
46 #include "smatch_slist.h"
47 #include "smatch_extra.h"
49 #include <openssl/md5.h>
51 static int my_id;
53 static struct smatch_state *alloc_tag_state(mtag_t tag)
55 struct smatch_state *state;
56 char buf[64];
58 state = __alloc_smatch_state(0);
59 snprintf(buf, sizeof(buf), "%lld", tag);
60 state->name = alloc_sname(buf);
61 state->data = malloc(sizeof(mtag_t));
62 *(mtag_t *)state->data = tag;
64 return state;
67 static mtag_t str_to_tag(const char *str)
69 unsigned char c[MD5_DIGEST_LENGTH];
70 unsigned long long *tag = (unsigned long long *)&c;
71 MD5_CTX mdContext;
72 int len;
74 len = strlen(str);
75 MD5_Init(&mdContext);
76 MD5_Update(&mdContext, str, len);
77 MD5_Final(c, &mdContext);
79 *tag &= ~(1ULL << 63);
81 return *tag;
84 static void alloc_assign(const char *fn, struct expression *expr, void *unused)
86 struct expression *left, *right;
87 char *left_name, *right_name;
88 struct symbol *left_sym;
89 char buf[256];
90 mtag_t tag;
92 if (expr->type != EXPR_ASSIGNMENT || expr->op != '=')
93 return;
94 left = strip_expr(expr->left);
95 right = strip_expr(expr->right);
96 if (right->type != EXPR_CALL || right->fn->type != EXPR_SYMBOL)
97 return;
99 left_name = expr_to_str_sym(left, &left_sym);
100 right_name = expr_to_str(right);
102 snprintf(buf, sizeof(buf), "%s %s %s %s", get_filename(), get_function(),
103 left_name, right_name);
104 tag = str_to_tag(buf);
106 sql_insert_mtag_about(tag, left_name, right_name);
108 if (left_name && left_sym)
109 set_state(my_id, left_name, left_sym, alloc_tag_state(tag));
111 free_string(left_name);
112 free_string(right_name);
115 int get_toplevel_mtag(struct symbol *sym, mtag_t *tag)
117 char buf[256];
119 if (!sym)
120 return 0;
122 if (!sym->ident ||
123 !(sym->ctype.modifiers & MOD_TOPLEVEL))
124 return 0;
126 snprintf(buf, sizeof(buf), "%s %s",
127 (sym->ctype.modifiers & MOD_STATIC) ? get_filename() : "extern",
128 sym->ident->name);
129 *tag = str_to_tag(buf);
130 return 1;
133 static void global_variable(struct symbol *sym)
135 mtag_t tag;
137 if (!get_toplevel_mtag(sym, &tag))
138 return;
140 sql_insert_mtag_about(tag,
141 sym->ident->name,
142 (sym->ctype.modifiers & MOD_STATIC) ? get_filename() : "extern");
145 static void db_returns_buf_size(struct expression *expr, int param, char *unused, char *math)
147 struct expression *call;
148 struct range_list *rl;
150 if (expr->type != EXPR_ASSIGNMENT)
151 return;
152 call = strip_expr(expr->right);
154 if (!parse_call_math_rl(call, math, &rl))
155 return;
156 // rl = cast_rl(&int_ctype, rl);
157 // set_state_expr(my_size_id, expr->left, alloc_estate_rl(rl));
160 static void match_call_info(struct expression *expr)
162 struct smatch_state *state;
163 struct expression *arg;
164 int i = -1;
166 FOR_EACH_PTR(expr->args, arg) {
167 i++;
168 state = get_state_expr(my_id, arg);
169 if (!state || !state->data)
170 continue;
171 sql_insert_caller_info(expr, MEMORY_TAG, i, "$", state->name);
172 } END_FOR_EACH_PTR(arg);
175 static void save_caller_info(const char *name, struct symbol *sym, char *key, char *value)
177 struct smatch_state *state;
178 char fullname[256];
179 mtag_t tag;
181 if (strncmp(key, "$", 1) != 0)
182 return;
184 tag = atoll(value);
185 snprintf(fullname, 256, "%s%s", name, key + 1);
186 state = alloc_tag_state(tag);
187 set_state(my_id, fullname, sym, state);
190 static int get_array_mtag(struct expression *expr, mtag_t *tag)
192 struct expression *base, *offset;
193 sval_t sval;
194 mtag_t base_tag;
195 char buf[256];
197 if (!is_array(expr))
198 return 0;
200 base = get_array_base(expr);
201 if (!base)
202 return 0;
203 offset = get_array_offset(expr);
204 if (!offset)
205 return 0;
207 if (!get_implied_value(offset, &sval))
208 return 0;
210 if (!get_mtag(base, &base_tag))
211 return 0;
213 snprintf(buf, sizeof(buf), "%lld + %lld", base_tag, sval.value);
214 *tag = str_to_tag(buf);
216 return 1;
219 static int get_mtag_cnt;
220 int get_mtag(struct expression *expr, mtag_t *tag)
222 struct smatch_state *state;
223 int ret = 0;
225 expr = strip_expr(expr);
226 if (!expr)
227 return 0;
229 if (get_mtag_cnt > 0)
230 return 0;
232 get_mtag_cnt++;
234 /* FIXME: This doesn't feel like the right thing at all */
235 if (expr->type == EXPR_PREOP) {
236 if (expr->op == '&')
237 expr = strip_expr(expr->unop);
238 if (expr->op == '*')
239 expr = strip_expr(expr->unop);
242 switch (expr->type) {
243 case EXPR_SYMBOL:
244 if (get_toplevel_mtag(expr->symbol, tag)) {
245 ret = 1;
246 goto dec_cnt;
248 break;
249 case EXPR_DEREF:
250 if (get_deref_mtag(expr, tag)) {
251 ret = 1;
252 goto dec_cnt;
254 break;
255 case EXPR_BINOP:
256 ret = get_array_mtag(expr, tag);
257 goto dec_cnt;
260 state = get_state_expr(my_id, expr);
261 if (!state)
262 goto dec_cnt;
263 if (state->data) {
264 *tag = *(mtag_t *)state->data;
265 ret = 1;
266 goto dec_cnt;
269 dec_cnt:
270 get_mtag_cnt--;
271 return ret;
274 int create_mtag_alias(mtag_t tag, struct expression *expr, mtag_t *new)
276 char buf[256];
277 int lines_from_start;
278 char *str;
281 * We need the alias to be unique. It's not totally required that it
282 * be the same from one DB build to then next, but it makes debugging
283 * a bit simpler.
287 if (!cur_func_sym)
288 return 0;
290 lines_from_start = expr->pos.line - cur_func_sym->pos.line;
291 str = expr_to_str(expr);
292 snprintf(buf, sizeof(buf), "%lld %d %s", tag, lines_from_start, str);
293 free_string(str);
295 *new = str_to_tag(buf);
297 return 1;
300 int expr_to_mtag_name_offset(struct expression *expr, mtag_t *tag, char **name, int *offset)
302 static char buf[256];
304 *offset = 0;
305 *name = 0;
307 expr = strip_expr(expr);
308 if (!expr)
309 return 0;
311 if (expr->type == EXPR_DEREF) {
312 *offset = get_member_offset_from_deref(expr);
313 if (*offset < 0)
314 return 0;
315 snprintf(buf, sizeof(buf), "$->%s", expr->member->name);
316 *name = buf;
317 return get_mtag(expr->deref, tag);
320 snprintf(buf, sizeof(buf), "*$");
321 *name = buf;
323 return get_mtag(expr, tag);
326 void register_mtag(int id)
328 my_id = id;
330 add_hook(&global_variable, BASE_HOOK);
332 add_function_assign_hook("kmalloc", &alloc_assign, NULL);
333 add_function_assign_hook("kzalloc", &alloc_assign, NULL);
335 select_return_states_hook(BUF_SIZE, &db_returns_buf_size);
337 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
338 select_caller_info_hook(save_caller_info, MEMORY_TAG);