mtag: fix expr_to_mtag_offset()
[smatch.git] / smatch_container_of.c
blob2643bca45998be994b01396fac60e193b80eb7a7
1 /*
2 * Copyright (C) 2017 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
18 #include "smatch.h"
19 #include "smatch_slist.h"
20 #include "smatch_extra.h"
22 static int my_id;
23 static int param_id;
25 static struct stree *used_stree;
26 static struct stree_stack *saved_stack;
28 STATE(used);
30 int get_param_from_container_of(struct expression *expr)
32 struct expression *param_expr;
33 struct symbol *type;
34 sval_t sval;
35 int param;
38 type = get_type(expr);
39 if (!type || type->type != SYM_PTR)
40 return -1;
42 expr = strip_expr(expr);
43 if (expr->type != EXPR_BINOP || expr->op != '-')
44 return -1;
46 if (!get_value(expr->right, &sval))
47 return -1;
48 if (sval.value < 0 || sval.value > 4096)
49 return -1;
51 param_expr = get_assigned_expr(expr->left);
52 if (!param_expr)
53 return -1;
54 param = get_param_num(param_expr);
55 if (param < 0)
56 return -1;
58 return param;
61 int get_offset_from_container_of(struct expression *expr)
63 struct expression *param_expr;
64 struct symbol *type;
65 sval_t sval;
67 type = get_type(expr);
68 if (!type || type->type != SYM_PTR)
69 return -1;
71 expr = strip_expr(expr);
72 if (expr->type != EXPR_BINOP || expr->op != '-')
73 return -1;
75 if (!get_value(expr->right, &sval))
76 return -1;
77 if (sval.value < 0 || sval.value > 4096)
78 return -1;
80 param_expr = get_assigned_expr(expr->left);
81 if (!param_expr)
82 return -1;
84 return sval.value;
87 static int get_container_arg(struct symbol *sym)
89 struct expression *__mptr;
90 int param;
92 if (!sym || !sym->ident)
93 return -1;
95 __mptr = get_assigned_expr_name_sym(sym->ident->name, sym);
96 param = get_param_from_container_of(__mptr);
98 return param;
101 static int get_container_offset(struct symbol *sym)
103 struct expression *__mptr;
104 int offset;
106 if (!sym || !sym->ident)
107 return -1;
109 __mptr = get_assigned_expr_name_sym(sym->ident->name, sym);
110 offset = get_offset_from_container_of(__mptr);
112 return offset;
115 static char *get_container_name(struct sm_state *sm, int offset)
117 static char buf[256];
118 const char *name;
120 name = get_param_name(sm);
121 if (!name)
122 return NULL;
124 if (name[0] == '$')
125 snprintf(buf, sizeof(buf), "$(-%d)%s", offset, name + 1);
126 else if (name[0] == '*' || name[1] == '$')
127 snprintf(buf, sizeof(buf), "*$(-%d)%s", offset, name + 2);
128 else
129 return NULL;
131 return buf;
134 static void get_state_hook(int owner, const char *name, struct symbol *sym)
136 int arg;
138 if (!option_info)
139 return;
140 if (__in_fake_assign)
141 return;
143 arg = get_container_arg(sym);
144 if (arg >= 0)
145 set_state_stree(&used_stree, my_id, name, sym, &used);
148 static void set_param_used(struct expression *call, struct expression *arg, char *key, char *unused)
150 struct symbol *sym;
151 char *name;
152 int arg_nr;
154 name = get_variable_from_key(arg, key, &sym);
155 if (!name || !sym)
156 goto free;
158 arg_nr = get_container_arg(sym);
159 if (arg_nr >= 0)
160 set_state(my_id, name, sym, &used);
161 free:
162 free_string(name);
165 static void process_states(void)
167 struct sm_state *tmp;
168 int arg, offset;
169 const char *name;
171 FOR_EACH_SM(used_stree, tmp) {
172 arg = get_container_arg(tmp->sym);
173 offset = get_container_offset(tmp->sym);
174 if (arg < 0 || offset < 0)
175 continue;
176 name = get_container_name(tmp, offset);
177 if (!name)
178 continue;
179 sql_insert_return_implies(CONTAINER, arg, name, "");
180 } END_FOR_EACH_SM(tmp);
182 free_stree(&used_stree);
185 static void match_function_def(struct symbol *sym)
187 free_stree(&used_stree);
190 static void match_save_states(struct expression *expr)
192 push_stree(&saved_stack, used_stree);
193 used_stree = NULL;
196 static void match_restore_states(struct expression *expr)
198 free_stree(&used_stree);
199 used_stree = pop_stree(&saved_stack);
202 static void print_returns_container_of(int return_id, char *return_ranges, struct expression *expr)
204 int offset;
205 int param;
206 char key[64];
207 char value[64];
209 param = get_param_from_container_of(expr);
210 if (param < 0)
211 return;
212 offset = get_offset_from_container_of(expr);
213 if (offset < 0)
214 return;
216 snprintf(key, sizeof(key), "%d", param);
217 snprintf(value, sizeof(value), "-%d", offset);
219 /* no need to add it to return_implies because it's not really param_used */
220 sql_insert_return_states(return_id, return_ranges, CONTAINER, -1,
221 key, value);
224 static void returns_container_of(struct expression *expr, int param, char *key, char *value)
226 struct expression *call, *arg;
227 int offset;
228 char buf[64];
230 if (expr->type != EXPR_ASSIGNMENT || expr->op != '=')
231 return;
232 call = strip_expr(expr->right);
233 if (call->type != EXPR_CALL)
234 return;
235 if (param != -1)
236 return;
237 param = atoi(key);
238 offset = atoi(value);
240 arg = get_argument_from_call_expr(call->args, param);
241 if (!arg)
242 return;
243 if (arg->type != EXPR_SYMBOL)
244 return;
245 param = get_param_num(arg);
246 if (param < 0)
247 return;
248 snprintf(buf, sizeof(buf), "$(%d)", offset);
249 sql_insert_return_implies(CONTAINER, param, buf, "");
252 static int get_shared_cnt(const char *one, const char *two)
254 int i;
256 i = 0;
257 while (true) {
258 if (!one[i] || !two[i])
259 break;
260 if (one[i] != two[i])
261 break;
262 i++;
264 if (i == 0)
265 return 0;
266 i--;
267 while (i > 0 && (one[i] == '>' || one[i] == '-' || one[i] == '.'))
268 i--;
270 return i + 1;
273 static int build_offset_str(struct expression *expr, const char *name,
274 int shared, char *buf, int size, int op)
276 int chop = 0;
277 int offset;
278 int i;
280 i = shared;
281 while (name[i]) {
282 if (name[i] == '.' || name[i] == '-')
283 chop++;
284 i++;
287 // FIXME: Handle more chops
288 if (chop > 1)
289 return 0;
291 if (chop == 0) {
292 offset = 0;
293 } else {
294 offset = get_member_offset_from_deref(expr);
295 if (offset < 0)
296 return 0;
299 snprintf(buf, size, "%c%d", (op == '+') ? '+' : '-', offset);
300 return 1;
303 static void match_call(struct expression *call)
305 struct expression *fn, *arg;
306 char *fn_name, *arg_name;
307 int param, shared;
308 char minus_str[64];
309 char plus_str[64];
310 char offset_str[64];
311 bool star;
314 * We're trying to link the function with the parameter. There are a
315 * couple ways this can be passed:
316 * foo->func(foo, ...);
317 * foo->func(foo->x, ...);
318 * foo->bar.func(&foo->bar, ...);
319 * foo->bar->baz->func(foo, ...);
321 * So the method is basically to subtract the offsets until we get to
322 * the common bit, then add the member offsets to get the parameter.
324 * If we're taking an address then the offset math is not stared,
325 * otherwise it is. Starred means dereferenced.
327 fn = strip_expr(call->fn);
328 fn_name = expr_to_var(fn);
329 if (!fn_name)
330 return;
332 param = -1;
333 FOR_EACH_PTR(call->args, arg) {
334 param++;
336 arg = strip_expr(arg);
337 star = true;
338 if (arg->type == EXPR_PREOP && arg->op == '&') {
339 arg = strip_expr(arg->unop);
340 star = false;
343 arg_name = expr_to_var(arg);
344 if (!arg_name)
345 continue;
346 shared = get_shared_cnt(fn_name, arg_name);
347 if (!shared)
348 goto free_arg_name;
349 if (!build_offset_str(fn, fn_name, shared, minus_str, sizeof(minus_str), '-'))
350 goto free_arg_name;
351 if (!build_offset_str(arg, arg_name, shared, plus_str, sizeof(plus_str), '+'))
352 goto free_arg_name;
353 if (star)
354 snprintf(offset_str, sizeof(offset_str), "*(%s%s)", minus_str, plus_str);
355 else
356 snprintf(offset_str, sizeof(offset_str), "%s%s", minus_str, plus_str);
357 sql_insert_caller_info(call, CONTAINER, param, offset_str, "$(-1)");
358 free_arg_name:
359 free_string(arg_name);
360 } END_FOR_EACH_PTR(arg);
362 free_string(fn_name);
365 static void db_passed_container(const char *name, struct symbol *sym, char *key, char *value)
367 sval_t offset = {
368 .type = &int_ctype,
370 const char *arg_offset;
371 int star = 0;
372 int val;
374 if (key[0] == '*') {
375 star = 1;
376 key += 2;
379 val = atoi(key);
380 if (val < -4095 || val > 0)
381 return;
382 offset.value = -val;
383 arg_offset = strchr(key, '+');
384 if (!arg_offset)
385 return;
386 val = atoi(arg_offset + 1);
387 if (val > 4095 || val < 0)
388 return;
389 offset.value |= val << 16;
390 if (star)
391 offset.value |= 1ULL << 31;
393 set_state(param_id, name, sym, alloc_estate_sval(offset));
396 struct db_info {
397 struct symbol *arg;
398 int prev_offset;
399 struct range_list *rl;
400 int star;
401 struct stree *stree;
404 static struct symbol *get_member_from_offset(struct symbol *sym, int offset)
406 struct symbol *type, *tmp;
407 int cur;
409 type = get_real_base_type(sym);
410 if (!type || type->type != SYM_PTR)
411 return NULL;
412 type = get_real_base_type(type);
413 if (!type || type->type != SYM_STRUCT)
414 return NULL;
416 cur = 0;
417 FOR_EACH_PTR(type->symbol_list, tmp) {
418 cur = ALIGN(cur, tmp->ctype.alignment);
419 if (offset == cur)
420 return tmp;
421 cur += type_bytes(tmp);
422 } END_FOR_EACH_PTR(tmp);
423 return NULL;
426 static struct symbol *get_member_type_from_offset(struct symbol *sym, int offset)
428 struct symbol *base_type;
429 struct symbol *member;
431 base_type = get_real_base_type(sym);
432 if (base_type && base_type->type == SYM_PTR)
433 base_type = get_real_base_type(base_type);
434 if (offset == 0 && base_type && base_type->type == SYM_BASETYPE)
435 return base_type;
437 member = get_member_from_offset(sym, offset);
438 if (!member)
439 return NULL;
440 return get_real_base_type(member);
443 static const char *get_name_from_offset(struct symbol *arg, int offset)
445 struct symbol *member, *type;
446 const char *name;
447 static char fullname[256];
449 name = arg->ident->name;
451 type = get_real_base_type(arg);
452 if (!type || type->type != SYM_PTR)
453 return name;
455 type = get_real_base_type(type);
456 if (!type)
457 return NULL;
458 if (type->type != SYM_STRUCT) {
459 snprintf(fullname, sizeof(fullname), "*%s", name);
460 return fullname;
463 member = get_member_from_offset(arg, offset);
464 if (!member)
465 return NULL;
467 snprintf(fullname, sizeof(fullname), "%s->%s", name, member->ident->name);
468 return fullname;
471 static void set_param_value(struct stree **stree, struct symbol *arg, int offset, struct range_list *rl)
473 const char *name;
475 name = get_name_from_offset(arg, offset);
476 if (!name)
477 return;
478 set_state_stree(stree, SMATCH_EXTRA, name, arg, alloc_estate_rl(rl));
481 static int save_vals(void *_db_info, int argc, char **argv, char **azColName)
483 struct db_info *db_info = _db_info;
484 struct symbol *type;
485 struct range_list *rl;
486 int offset = 0;
487 const char *value;
489 if (argc == 2) {
490 offset = atoi(argv[0]);
491 value = argv[1];
492 } else {
493 value = argv[0];
496 if (db_info->prev_offset != -1 &&
497 db_info->prev_offset != offset) {
498 set_param_value(&db_info->stree, db_info->arg, db_info->prev_offset, db_info->rl);
499 db_info->rl = NULL;
502 db_info->prev_offset = offset;
504 type = get_real_base_type(db_info->arg);
505 if (db_info->star)
506 goto found_type;
507 if (type->type != SYM_PTR)
508 return 0;
509 type = get_real_base_type(type);
510 if (type->type == SYM_BASETYPE)
511 goto found_type;
512 type = get_member_type_from_offset(db_info->arg, offset);
513 found_type:
514 str_to_rl(type, (char *)value, &rl);
515 if (db_info->rl)
516 db_info->rl = rl_union(db_info->rl, rl);
517 else
518 db_info->rl = rl;
520 return 0;
523 static struct stree *load_tag_info_sym(mtag_t tag, struct symbol *arg, int arg_offset, int star)
525 struct db_info db_info = {
526 .arg = arg,
527 .prev_offset = -1,
528 .star = star,
530 struct symbol *type;
532 if (!tag || !arg->ident)
533 return NULL;
535 type = get_real_base_type(arg);
536 if (!type)
537 return NULL;
538 if (!star) {
539 if (type->type != SYM_PTR)
540 return NULL;
541 type = get_real_base_type(type);
542 if (!type)
543 return NULL;
546 if (star || type->type == SYM_BASETYPE) {
547 run_sql(save_vals, &db_info,
548 "select value from mtag_data where tag = %lld and offset = %d and type = %d;",
549 tag, arg_offset, DATA_VALUE);
550 } else { /* presumably the parameter is a struct pointer */
551 run_sql(save_vals, &db_info,
552 "select offset, value from mtag_data where tag = %lld and type = %d;",
553 tag, DATA_VALUE);
556 if (db_info.prev_offset != -1)
557 set_param_value(&db_info.stree, arg, db_info.prev_offset, db_info.rl);
559 // FIXME: handle an offset correctly
560 if (!star && !arg_offset) {
561 sval_t sval;
563 sval.type = get_real_base_type(arg);
564 sval.uvalue = tag;
565 set_state_stree(&db_info.stree, SMATCH_EXTRA, arg->ident->name, arg, alloc_estate_sval(sval));
567 return db_info.stree;
570 static void handle_passed_container(struct symbol *sym)
572 struct symbol *arg;
573 struct smatch_state *state;
574 struct sm_state *sm;
575 struct stree *stree;
576 mtag_t fn_tag, container_tag, arg_tag;
577 sval_t offset;
578 int container_offset, arg_offset;
579 int star;
581 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
582 state = get_state(param_id, arg->ident->name, arg);
583 if (state)
584 goto found;
585 } END_FOR_EACH_PTR(arg);
587 return;
588 found:
589 if (!estate_get_single_value(state, &offset))
590 return;
591 container_offset = -(offset.value & 0xffff);
592 arg_offset = (offset.value & 0xfff0000) >> 16;
593 star = !!(offset.value & (1ULL << 31));
595 if (!get_toplevel_mtag(cur_func_sym, &fn_tag))
596 return;
597 if (!mtag_map_select_container(fn_tag, container_offset, &container_tag))
598 return;
599 if (!arg_offset || star) {
600 arg_tag = container_tag;
601 } else {
602 if (!mtag_map_select_tag(container_tag, -arg_offset, &arg_tag))
603 return;
606 stree = load_tag_info_sym(arg_tag, arg, arg_offset, star);
607 FOR_EACH_SM(stree, sm) {
608 set_state(sm->owner, sm->name, sm->sym, sm->state);
609 } END_FOR_EACH_SM(sm);
610 free_stree(&stree);
613 void register_container_of(int id)
615 my_id = id;
617 add_hook(&match_function_def, FUNC_DEF_HOOK);
619 add_get_state_hook(&get_state_hook);
621 add_hook(&match_save_states, INLINE_FN_START);
622 add_hook(&match_restore_states, INLINE_FN_END);
624 select_return_implies_hook(CONTAINER, &set_param_used);
625 all_return_states_hook(&process_states);
627 add_split_return_callback(&print_returns_container_of);
628 select_return_states_hook(CONTAINER, &returns_container_of);
630 add_hook(&match_call, FUNCTION_CALL_HOOK);
633 static struct smatch_state *unmatched_state(struct sm_state *sm)
635 return alloc_estate_whole(estate_type(sm->state));
638 void register_container_of2(int id)
640 param_id = id;
642 select_caller_info_hook(db_passed_container, CONTAINER);
643 add_hook(&handle_passed_container, AFTER_DEF_HOOK);
644 add_unmatched_state_hook(param_id, &unmatched_state);
645 add_merge_hook(param_id, &merge_estates);