free_strict: pass the correct expression to free hooks.
[smatch.git] / smatch_points_to_container.c
blob08b2e40c05dddc12d1ea6023fe8f82c719a4a747
1 /*
2 * Copyright (C) 2022 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 "ctype.h"
19 #include "smatch.h"
20 #include "smatch_slist.h"
21 #include "smatch_extra.h"
23 static int my_id;
25 static char *handle_container_of_assign(struct expression *expr, struct expression **ptr)
27 struct expression *right, *orig;
28 struct symbol *type;
29 sval_t sval;
30 char buf[64];
32 type = get_type(expr->left);
33 if (!type || type->type != SYM_PTR)
34 return NULL;
36 right = strip_expr(expr->right);
37 if (right->type != EXPR_BINOP || right->op != '-')
38 return NULL;
40 if (!get_value(right->right, &sval) ||
41 sval.value < 0 || sval.value > MTAG_OFFSET_MASK)
42 return NULL;
44 orig = get_assigned_expr(right->left);
45 if (!orig)
46 return NULL;
48 snprintf(buf, sizeof(buf), "(%lld<~$)", sval.value);
49 *ptr = orig;
50 return alloc_string(buf);
54 static void match_assign(struct expression *expr)
56 struct expression *ptr = NULL;
57 char *container_str, *name;
58 struct symbol *sym;
59 char buf[64];
61 if (expr->op != '=')
62 return;
64 if (is_fake_call(expr->right) || __in_fake_parameter_assign ||
65 __in_fake_struct_assign)
66 return;
68 container_str = handle_container_of_assign(expr, &ptr);
69 if (!container_str || !ptr)
70 return;
71 name = expr_to_var_sym(ptr, &sym);
72 if (!name || !sym)
73 goto free;
75 snprintf(buf, sizeof(buf), "%s %s", name, container_str);
76 set_state(my_id, buf, sym, alloc_state_expr(expr->left));
78 free:
79 free_string(name);
80 free_string(container_str);
83 static void return_str_hook(struct expression *expr, const char *ret_str)
85 struct expression *call, *arg;
86 struct symbol *sym;
87 int offset, param;
88 char buf[32];
89 char *name;
91 if (!expr || expr->type != EXPR_ASSIGNMENT)
92 return;
93 call = expr;
94 while (call && call->type == EXPR_ASSIGNMENT)
95 call = strip_expr(call->right);
96 if (!call || call->type != EXPR_CALL)
97 return;
99 if (!get_offset_param(ret_str, &offset, &param))
100 return;
102 arg = get_argument_from_call_expr(call->args, param);
103 arg = strip_expr(arg);
104 if (!arg)
105 return;
107 name = expr_to_var_sym(arg, &sym);
108 if (!name || !sym)
109 return;
111 snprintf(buf, sizeof(buf), "%s (%d<~$)", name, offset);
112 set_state(my_id, buf, sym, alloc_state_expr(expr->left));
115 struct expression *get_stored_container(struct expression *expr, int offset)
117 struct smatch_state *state;
118 struct symbol *sym;
119 char buf[64];
120 char *name;
122 name = expr_to_var_sym(expr, &sym);
123 if (!name || !sym)
124 return NULL;
126 snprintf(buf, sizeof(buf), "%s (%d<~$)", name, offset);
127 free_string(name);
128 state = get_state(my_id, buf, sym);
129 if (!state)
130 return NULL;
131 return state->data;
134 void register_points_to_container(int id)
136 my_id = id;
138 set_dynamic_states(my_id);
139 add_hook(&match_assign, ASSIGNMENT_HOOK_AFTER);
140 add_return_string_hook(return_str_hook);
141 add_modification_hook(my_id, &set_undefined);