2 * Copyright (C) 2009 Dan Carpenter.
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 number of ways that variables are modified:
21 * 2) increment/decrement
23 * 4) inside functions.
25 * For setting stuff inside a function then, of course, it's more accurate if
26 * you have the cross function database built. Otherwise we are super
27 * aggressive about marking things as modified and if you have "frob(foo);" then
28 * we assume "foo->bar" is modified.
34 #include "smatch_extra.h"
35 #include "smatch_slist.h"
43 static modification_hook
**hooks
;
44 static modification_hook
**hooks_late
;
46 ALLOCATOR(modification_data
, "modification data");
49 static struct smatch_state
*alloc_my_state(struct expression
*expr
, struct smatch_state
*prev
)
51 struct smatch_state
*state
;
52 struct modification_data
*data
;
55 state
= __alloc_smatch_state(0);
56 expr
= strip_expr(expr
);
57 name
= expr_to_str(expr
);
58 state
->name
= alloc_sname(name
);
61 data
= __alloc_modification_data(0);
69 void add_modification_hook(int owner
, modification_hook
*call_back
)
72 sm_fatal("multiple modification hooks for %s", check_name(owner
));
73 hooks
[owner
] = call_back
;
76 void add_modification_hook_late(int owner
, modification_hook
*call_back
)
78 if (hooks_late
[owner
])
79 sm_fatal("multiple late modification hooks for %s", check_name(owner
));
80 hooks_late
[owner
] = call_back
;
83 static int matches(char *name
, struct symbol
*sym
, struct sm_state
*sm
)
91 if (strncmp(sm
->name
, name
, len
) == 0) {
92 if (sm
->name
[len
] == '\0')
94 if (sm
->name
[len
] == '-' || sm
->name
[len
] == '.')
97 if (sm
->name
[0] != '*')
99 if (strncmp(sm
->name
+ 1, name
, len
) == 0) {
100 if (sm
->name
[len
+ 1] == '\0')
102 if (sm
->name
[len
+ 1] == '-' || sm
->name
[len
+ 1] == '.')
108 static void call_modification_hooks_name_sym(char *name
, struct symbol
*sym
, struct expression
*mod_expr
, int late
)
111 struct smatch_state
*prev
;
114 prev
= get_state(my_id
, name
, sym
);
116 if (cur_func_sym
&& !__in_fake_assign
)
117 set_state(my_id
, name
, sym
, alloc_my_state(mod_expr
, prev
));
119 FOR_EACH_SM(__get_cur_stree(), sm
) {
120 if (sm
->owner
> num_checks
)
122 match
= matches(name
, sym
, sm
);
126 if (late
== EARLY
|| late
== BOTH
) {
127 if (hooks
[sm
->owner
])
128 (hooks
[sm
->owner
])(sm
, mod_expr
);
130 if (late
== LATE
|| late
== BOTH
) {
131 if (hooks_late
[sm
->owner
])
132 (hooks_late
[sm
->owner
])(sm
, mod_expr
);
135 } END_FOR_EACH_SM(sm
);
138 static void call_modification_hooks(struct expression
*expr
, struct expression
*mod_expr
, int late
)
144 update_mtag_data(expr
);
146 name
= expr_to_known_chunk_sym(expr
, &sym
);
149 call_modification_hooks_name_sym(name
, sym
, mod_expr
, late
);
154 static void db_param_add(struct expression
*expr
, int param
, char *key
, char *value
)
156 struct expression
*arg
, *gen_expr
;
157 char *name
, *other_name
;
158 struct symbol
*sym
, *other_sym
;
160 while (expr
->type
== EXPR_ASSIGNMENT
)
161 expr
= strip_expr(expr
->right
);
162 if (expr
->type
!= EXPR_CALL
)
165 arg
= get_argument_from_call_expr(expr
->args
, param
);
169 gen_expr
= gen_expression_from_key(arg
, key
);
171 update_mtag_data(gen_expr
);
173 name
= get_variable_from_key(arg
, key
, &sym
);
178 call_modification_hooks_name_sym(name
, sym
, expr
, BOTH
);
181 other_name
= map_long_to_short_name_sym(name
, sym
, &other_sym
);
184 call_modification_hooks_name_sym(other_name
, other_sym
, expr
, BOTH
);
186 free_string(other_name
);
193 static void match_assign(struct expression
*expr
, int late
)
195 call_modification_hooks(expr
->left
, expr
, late
);
198 static void unop_expr(struct expression
*expr
, int late
)
200 if (expr
->op
!= SPECIAL_DECREMENT
&& expr
->op
!= SPECIAL_INCREMENT
)
203 call_modification_hooks(expr
->unop
, expr
, late
);
206 static void match_call(struct expression
*expr
)
208 struct expression
*arg
, *tmp
;
210 /* If we have the DB then trust the DB */
214 FOR_EACH_PTR(expr
->args
, arg
) {
215 tmp
= strip_expr(arg
);
216 if (tmp
->type
== EXPR_PREOP
&& tmp
->op
== '&')
217 call_modification_hooks(tmp
->unop
, expr
, BOTH
);
219 call_modification_hooks(deref_expression(tmp
), expr
, BOTH
);
220 } END_FOR_EACH_PTR(arg
);
223 static void asm_expr(struct statement
*stmt
, int late
)
225 struct expression
*expr
;
228 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
230 case 0: /* identifier */
231 case 1: /* constraint */
234 case 2: /* expression */
236 call_modification_hooks(expr
, NULL
, late
);
239 } END_FOR_EACH_PTR(expr
);
243 static void match_assign_early(struct expression
*expr
)
245 match_assign(expr
, EARLY
);
248 static void unop_expr_early(struct expression
*expr
)
250 unop_expr(expr
, EARLY
);
253 static void asm_expr_early(struct statement
*stmt
)
255 asm_expr(stmt
, EARLY
);
258 static void match_assign_late(struct expression
*expr
)
260 match_assign(expr
, LATE
);
263 static void unop_expr_late(struct expression
*expr
)
265 unop_expr(expr
, LATE
);
268 static void asm_expr_late(struct statement
*stmt
)
270 asm_expr(stmt
, LATE
);
273 struct smatch_state
*get_modification_state(struct expression
*expr
)
275 return get_state_expr(my_id
, expr
);
278 void register_modification_hooks(int id
)
282 hooks
= malloc((num_checks
+ 1) * sizeof(*hooks
));
283 memset(hooks
, 0, (num_checks
+ 1) * sizeof(*hooks
));
284 hooks_late
= malloc((num_checks
+ 1) * sizeof(*hooks
));
285 memset(hooks_late
, 0, (num_checks
+ 1) * sizeof(*hooks
));
287 add_hook(&match_assign_early
, ASSIGNMENT_HOOK
);
288 add_hook(&unop_expr_early
, OP_HOOK
);
289 add_hook(&asm_expr_early
, ASM_HOOK
);
292 void register_modification_hooks_late(int id
)
294 add_hook(&match_call
, FUNCTION_CALL_HOOK
);
296 select_return_states_hook(PARAM_ADD
, &db_param_add
);
297 select_return_states_hook(PARAM_SET
, &db_param_add
);
299 add_hook(&match_assign_late
, ASSIGNMENT_HOOK_AFTER
);
300 add_hook(&unop_expr_late
, OP_HOOK
);
301 add_hook(&asm_expr_late
, ASM_HOOK
);