mtag/mtag_data: re-use the same code to store data and retrieve it
[smatch.git] / smatch_mtag.c
blobf3de116dfdd69f28b0115aa34462f8093a34c3dd
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 &= ~MTAG_ALIAS_BIT;
80 *tag &= ~MTAG_OFFSET_MASK;
82 return *tag;
85 static void alloc_assign(const char *fn, struct expression *expr, void *unused)
87 struct expression *left, *right;
88 char *left_name, *right_name;
89 struct symbol *left_sym;
90 char buf[256];
91 mtag_t tag;
93 if (expr->type != EXPR_ASSIGNMENT || expr->op != '=')
94 return;
95 left = strip_expr(expr->left);
96 right = strip_expr(expr->right);
97 if (right->type != EXPR_CALL || right->fn->type != EXPR_SYMBOL)
98 return;
100 left_name = expr_to_str_sym(left, &left_sym);
101 right_name = expr_to_str(right);
103 snprintf(buf, sizeof(buf), "%s %s %s %s", get_filename(), get_function(),
104 left_name, right_name);
105 tag = str_to_tag(buf);
107 sql_insert_mtag_about(tag, left_name, right_name);
109 if (left_name && left_sym)
110 set_state(my_id, left_name, left_sym, alloc_tag_state(tag));
112 free_string(left_name);
113 free_string(right_name);
116 int get_toplevel_mtag(struct symbol *sym, mtag_t *tag)
118 char buf[256];
120 if (!sym)
121 return 0;
123 if (!sym->ident ||
124 !(sym->ctype.modifiers & MOD_TOPLEVEL))
125 return 0;
127 snprintf(buf, sizeof(buf), "%s %s",
128 (sym->ctype.modifiers & MOD_STATIC) ? get_filename() : "extern",
129 sym->ident->name);
130 *tag = str_to_tag(buf);
131 return 1;
134 int get_deref_mtag(struct expression *expr, mtag_t *tag)
136 mtag_t container_tag, member_tag;
137 int offset;
140 * I'm not totally sure what I'm doing...
142 * This is supposed to get something like "global_var->ptr", but I don't
143 * feel like it's complete at all.
147 if (!get_mtag(expr->unop, &container_tag))
148 return 0;
150 offset = get_member_offset_from_deref(expr);
151 if (offset < 0)
152 return 0;
154 if (!mtag_map_select_tag(container_tag, -offset, &member_tag))
155 return 0;
157 *tag = member_tag;
158 return 1;
161 static void global_variable(struct symbol *sym)
163 mtag_t tag;
165 if (!get_toplevel_mtag(sym, &tag))
166 return;
168 sql_insert_mtag_about(tag,
169 sym->ident->name,
170 (sym->ctype.modifiers & MOD_STATIC) ? get_filename() : "extern");
173 static void db_returns_buf_size(struct expression *expr, int param, char *unused, char *math)
175 struct expression *call;
176 struct range_list *rl;
178 if (expr->type != EXPR_ASSIGNMENT)
179 return;
180 call = strip_expr(expr->right);
182 if (!parse_call_math_rl(call, math, &rl))
183 return;
184 // rl = cast_rl(&int_ctype, rl);
185 // set_state_expr(my_size_id, expr->left, alloc_estate_rl(rl));
188 static void db_returns_memory_tag(struct expression *expr, int param, char *key, char *value)
190 struct expression *call, *arg;
191 mtag_t tag, alias;
192 char *name;
193 struct symbol *sym;
195 call = strip_expr(expr);
196 while (call->type == EXPR_ASSIGNMENT)
197 call = strip_expr(call->right);
198 if (call->type != EXPR_CALL)
199 return;
201 tag = strtoul(value, NULL, 10);
203 if (!create_mtag_alias(tag, call, &alias))
204 return;
206 arg = get_argument_from_call_expr(call->args, param);
207 if (!arg)
208 return;
210 name = get_variable_from_key(arg, key, &sym);
211 if (!name || !sym)
212 goto free;
214 set_state(my_id, name, sym, alloc_tag_state(alias));
215 free:
216 free_string(name);
219 static void match_call_info(struct expression *expr)
221 struct smatch_state *state;
222 struct expression *arg;
223 int i = -1;
225 FOR_EACH_PTR(expr->args, arg) {
226 i++;
227 state = get_state_expr(my_id, arg);
228 if (!state || !state->data)
229 continue;
230 sql_insert_caller_info(expr, MEMORY_TAG, i, "$", state->name);
231 } END_FOR_EACH_PTR(arg);
234 static void save_caller_info(const char *name, struct symbol *sym, char *key, char *value)
236 struct smatch_state *state;
237 char fullname[256];
238 mtag_t tag;
240 if (strncmp(key, "$", 1) != 0)
241 return;
243 tag = atoll(value);
244 snprintf(fullname, 256, "%s%s", name, key + 1);
245 state = alloc_tag_state(tag);
246 set_state(my_id, fullname, sym, state);
249 static int get_array_mtag_offset(struct expression *expr, mtag_t *tag, int *offset)
251 struct expression *array, *offset_expr;
252 struct symbol *type;
253 sval_t sval;
255 if (!is_array(expr))
256 return 0;
258 array = get_array_base(expr);
259 if (!array)
260 return 0;
261 type = get_type(array);
262 if (!type || type->type != SYM_ARRAY)
263 return 0;
264 type = get_real_base_type(type);
265 if (!type_bytes(type))
266 return 0;
268 if (!get_mtag(array, tag))
269 return 0;
271 offset_expr = get_array_offset(expr);
272 if (!get_value(offset_expr, &sval))
273 return 0;
274 *offset = sval.value * type_bytes(type);
276 return 1;
279 static int get_implied_mtag_offset(struct expression *expr, mtag_t *tag, int *offset)
281 struct smatch_state *state;
282 struct symbol *type;
283 sval_t sval;
285 type = get_type(expr);
286 if (!type_is_ptr(type))
287 return 0;
288 state = get_extra_state(expr);
289 if (!state || !estate_get_single_value(state, &sval))
290 return 0;
292 *tag = sval.uvalue & ~MTAG_OFFSET_MASK;
293 *offset = sval.uvalue & MTAG_OFFSET_MASK;
294 return 1;
297 static int get_mtag_cnt;
298 int get_mtag(struct expression *expr, mtag_t *tag)
300 struct smatch_state *state;
301 int ret = 0;
303 expr = strip_expr(expr);
304 if (!expr)
305 return 0;
307 if (get_mtag_cnt > 0)
308 return 0;
310 get_mtag_cnt++;
312 /* FIXME: This doesn't feel like the right thing at all */
313 if (expr->type == EXPR_PREOP) {
314 if (expr->op == '&')
315 expr = strip_expr(expr->unop);
316 if (expr->op == '*')
317 expr = strip_expr(expr->unop);
320 switch (expr->type) {
321 case EXPR_SYMBOL:
322 if (get_toplevel_mtag(expr->symbol, tag)) {
323 ret = 1;
324 goto dec_cnt;
326 break;
327 case EXPR_DEREF:
328 if (get_deref_mtag(expr, tag)) {
329 ret = 1;
330 goto dec_cnt;
332 break;
335 state = get_state_expr(my_id, expr);
336 if (!state)
337 goto dec_cnt;
338 if (state->data) {
339 *tag = *(mtag_t *)state->data;
340 ret = 1;
341 goto dec_cnt;
344 dec_cnt:
345 get_mtag_cnt--;
346 return ret;
349 int get_mtag_offset(struct expression *expr, mtag_t *tag, int *offset)
351 int val;
353 if (!expr)
354 return 0;
355 if (expr->type == EXPR_PREOP && expr->op == '*')
356 return get_mtag_offset(expr->unop, tag, offset);
357 if (get_implied_mtag_offset(expr, tag, offset))
358 return 1;
359 if (!get_mtag(expr, tag))
360 return 0;
361 expr = strip_expr(expr);
362 if (expr->type == EXPR_SYMBOL) {
363 *offset = 0;
364 return 1;
366 val = get_member_offset_from_deref(expr);
367 if (val < 0)
368 return 0;
369 *offset = val;
370 return 1;
373 int create_mtag_alias(mtag_t tag, struct expression *expr, mtag_t *new)
375 char buf[256];
376 int lines_from_start;
377 char *str;
380 * We need the alias to be unique. It's not totally required that it
381 * be the same from one DB build to then next, but it makes debugging
382 * a bit simpler.
386 if (!cur_func_sym)
387 return 0;
389 lines_from_start = expr->pos.line - cur_func_sym->pos.line;
390 str = expr_to_str(expr);
391 snprintf(buf, sizeof(buf), "%lld %d %s", tag, lines_from_start, str);
392 free_string(str);
394 *new = str_to_tag(buf);
395 sql_insert_mtag_alias(tag, *new);
397 return 1;
400 int expr_to_mtag_name_offset(struct expression *expr, mtag_t *tag, char **name, int *offset)
402 static char buf[256];
404 *offset = 0;
405 if (name)
406 *name = 0;
408 expr = strip_expr(expr);
409 if (!expr)
410 return 0;
412 if (is_array(expr)) {
413 if (name) /* i don't know how to return a name for this */
414 return 0;
415 return get_array_mtag_offset(expr, tag, offset);
417 if (expr->type == EXPR_DEREF) {
418 *offset = get_member_offset_from_deref(expr);
419 if (*offset < 0)
420 return 0;
421 snprintf(buf, sizeof(buf), "$->%s", expr->member->name);
422 if (name)
423 *name = buf;
424 return get_mtag(expr->deref, tag);
427 snprintf(buf, sizeof(buf), "*$");
428 if (name)
429 *name = buf;
431 return get_mtag(expr, tag);
434 int get_mtag_sval(struct expression *expr, sval_t *sval)
436 struct symbol *type;
437 mtag_t tag;
438 int offset = 0;
440 if (bits_in_pointer != 64)
441 return 0;
443 expr = strip_expr(expr);
445 type = get_type(expr);
446 if (!type_is_ptr(type))
447 return 0;
449 * There are only three options:
451 * 1) An array address:
452 * p = array;
453 * 2) An address like so:
454 * p = &my_struct->member;
455 * 3) A pointer:
456 * p = pointer;
460 if (type->type == SYM_ARRAY && get_toplevel_mtag(expr->symbol, &tag))
461 goto found;
463 if (get_implied_mtag_offset(expr, &tag, &offset))
464 goto found;
466 if (expr->type != EXPR_PREOP || expr->op != '&')
467 return 0;
468 expr = strip_expr(expr->unop);
470 if (!expr_to_mtag_name_offset(expr, &tag, NULL, &offset))
471 return 0;
472 if (offset > MTAG_OFFSET_MASK)
473 offset = MTAG_OFFSET_MASK;
475 found:
476 sval->type = type;
477 sval->uvalue = tag | offset;
479 return 1;
482 static void print_stored_to_mtag(int return_id, char *return_ranges, struct expression *expr)
484 struct sm_state *sm;
485 char buf[256];
486 const char *param_name;
487 int param;
489 FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
490 if (!sm->state->data)
491 continue;
493 param = get_param_num_from_sym(sm->sym);
494 if (param < 0)
495 continue;
496 param_name = get_param_name(sm);
497 if (!param_name)
498 continue;
499 if (strcmp(param_name, "$") == 0)
500 continue;
502 snprintf(buf, sizeof(buf), "%lld", *(mtag_t *)sm->state->data);
503 sql_insert_return_states(return_id, return_ranges, MEMORY_TAG, param, param_name, buf);
504 } END_FOR_EACH_SM(sm);
507 void register_mtag(int id)
509 my_id = id;
513 * The mtag stuff only works on 64 systems because we store the
514 * information in the pointer itself.
515 * bit 63 : set for alias mtags
516 * bit 62-12: mtag hash
517 * bit 11-0 : offset
520 if (bits_in_pointer != 64)
521 return;
523 add_hook(&global_variable, BASE_HOOK);
525 add_function_assign_hook("kmalloc", &alloc_assign, NULL);
526 add_function_assign_hook("kzalloc", &alloc_assign, NULL);
528 select_return_states_hook(BUF_SIZE, &db_returns_buf_size);
530 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
531 select_caller_info_hook(save_caller_info, MEMORY_TAG);
532 add_split_return_callback(&print_stored_to_mtag);
533 select_return_states_hook(MEMORY_TAG, db_returns_memory_tag);