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"
49 static modification_hook
**hooks
;
50 static modification_hook
**indirect_hooks
; /* parent struct modified etc */
51 static modification_hook
**hooks_late
;
52 static modification_hook
**indirect_hooks_late
; /* parent struct modified etc */
54 ALLOCATOR(modification_data
, "modification data");
57 static struct smatch_state
*alloc_my_state(struct expression
*expr
, struct smatch_state
*prev
)
59 struct smatch_state
*state
;
60 struct modification_data
*data
;
63 state
= __alloc_smatch_state(0);
64 expr
= strip_expr(expr
);
65 name
= expr_to_str(expr
);
66 state
->name
= alloc_sname(name
);
69 data
= __alloc_modification_data(0);
77 void add_modification_hook(int owner
, modification_hook
*call_back
)
79 hooks
[owner
] = call_back
;
82 void add_indirect_modification_hook(int owner
, modification_hook
*call_back
)
84 indirect_hooks
[owner
] = call_back
;
87 void add_modification_hook_late(int owner
, modification_hook
*call_back
)
89 hooks_late
[owner
] = call_back
;
92 void add_indirect_modification_hook_late(int owner
, modification_hook
*call_back
)
94 indirect_hooks_late
[owner
] = call_back
;
97 static int matches(char *name
, struct symbol
*sym
, struct sm_state
*sm
)
105 if (strncmp(sm
->name
, name
, len
) == 0) {
106 if (sm
->name
[len
] == '\0')
108 if (sm
->name
[len
] == '-' || sm
->name
[len
] == '.')
109 return match_indirect
;
111 if (sm
->name
[0] != '*')
113 if (strncmp(sm
->name
+ 1, name
, len
) == 0) {
114 if (sm
->name
[len
+ 1] == '\0')
115 return match_indirect
;
116 if (sm
->name
[len
+ 1] == '-' || sm
->name
[len
+ 1] == '.')
117 return match_indirect
;
122 static void call_modification_hooks_name_sym(char *name
, struct symbol
*sym
, struct expression
*mod_expr
, int late
)
125 struct smatch_state
*prev
;
128 prev
= get_state(my_id
, name
, sym
);
129 set_state(my_id
, name
, sym
, alloc_my_state(mod_expr
, prev
));
131 FOR_EACH_SM(__get_cur_stree(), sm
) {
132 if (sm
->owner
> num_checks
)
134 match
= matches(name
, sym
, sm
);
138 if (late
== EARLY
|| late
== BOTH
) {
139 if (hooks
[sm
->owner
])
140 (hooks
[sm
->owner
])(sm
, mod_expr
);
141 if (match
== match_indirect
&& indirect_hooks
[sm
->owner
])
142 (indirect_hooks
[sm
->owner
])(sm
, mod_expr
);
144 if (late
== LATE
|| late
== BOTH
) {
145 if (hooks_late
[sm
->owner
])
146 (hooks_late
[sm
->owner
])(sm
, mod_expr
);
147 if (match
== match_indirect
&& indirect_hooks_late
[sm
->owner
])
148 (indirect_hooks_late
[sm
->owner
])(sm
, mod_expr
);
151 } END_FOR_EACH_SM(sm
);
154 static void call_modification_hooks(struct expression
*expr
, struct expression
*mod_expr
, int late
)
159 name
= expr_to_known_chunk_sym(expr
, &sym
);
162 call_modification_hooks_name_sym(name
, sym
, mod_expr
, late
);
167 static void db_param_add(struct expression
*expr
, int param
, char *key
, char *value
)
169 struct expression
*arg
;
173 while (expr
->type
== EXPR_ASSIGNMENT
)
174 expr
= strip_expr(expr
->right
);
175 if (expr
->type
!= EXPR_CALL
)
178 arg
= get_argument_from_call_expr(expr
->args
, param
);
182 name
= get_variable_from_key(arg
, key
, &sym
);
186 call_modification_hooks_name_sym(name
, sym
, expr
, BOTH
);
191 static void match_assign(struct expression
*expr
, int late
)
193 call_modification_hooks(expr
->left
, expr
, late
);
196 static void unop_expr(struct expression
*expr
, int late
)
198 if (expr
->op
!= SPECIAL_DECREMENT
&& expr
->op
!= SPECIAL_INCREMENT
)
201 call_modification_hooks(expr
->unop
, expr
, late
);
204 static void match_call(struct expression
*expr
)
206 struct expression
*arg
, *tmp
;
208 FOR_EACH_PTR(expr
->args
, arg
) {
209 tmp
= strip_expr(arg
);
210 if (tmp
->type
== EXPR_PREOP
&& tmp
->op
== '&')
211 call_modification_hooks(tmp
->unop
, expr
, BOTH
);
212 else if (option_no_db
)
213 call_modification_hooks(deref_expression(tmp
), expr
, BOTH
);
214 } END_FOR_EACH_PTR(arg
);
217 static void asm_expr(struct statement
*stmt
, int late
)
219 struct expression
*expr
;
222 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
224 case 0: /* identifier */
225 case 1: /* constraint */
228 case 2: /* expression */
230 call_modification_hooks(expr
, NULL
, late
);
233 } END_FOR_EACH_PTR(expr
);
237 static void match_assign_early(struct expression
*expr
)
239 match_assign(expr
, EARLY
);
242 static void unop_expr_early(struct expression
*expr
)
244 unop_expr(expr
, EARLY
);
247 static void asm_expr_early(struct statement
*stmt
)
249 asm_expr(stmt
, EARLY
);
252 static void match_assign_late(struct expression
*expr
)
254 match_assign(expr
, LATE
);
257 static void unop_expr_late(struct expression
*expr
)
259 unop_expr(expr
, LATE
);
262 static void asm_expr_late(struct statement
*stmt
)
264 asm_expr(stmt
, LATE
);
267 struct smatch_state
*get_modification_state(struct expression
*expr
)
269 return get_state_expr(my_id
, expr
);
272 void register_modification_hooks(int id
)
276 hooks
= malloc((num_checks
+ 1) * sizeof(*hooks
));
277 memset(hooks
, 0, (num_checks
+ 1) * sizeof(*hooks
));
278 indirect_hooks
= malloc((num_checks
+ 1) * sizeof(*hooks
));
279 memset(indirect_hooks
, 0, (num_checks
+ 1) * sizeof(*hooks
));
280 hooks_late
= malloc((num_checks
+ 1) * sizeof(*hooks
));
281 memset(hooks_late
, 0, (num_checks
+ 1) * sizeof(*hooks
));
282 indirect_hooks_late
= malloc((num_checks
+ 1) * sizeof(*hooks
));
283 memset(indirect_hooks_late
, 0, (num_checks
+ 1) * sizeof(*hooks
));
285 add_hook(&match_assign_early
, ASSIGNMENT_HOOK
);
286 add_hook(&unop_expr_early
, OP_HOOK
);
287 add_hook(&asm_expr_early
, ASM_HOOK
);
290 void register_modification_hooks_late(int id
)
292 add_hook(&match_call
, FUNCTION_CALL_HOOK
);
294 add_hook(&match_assign_late
, ASSIGNMENT_HOOK_AFTER
);
295 add_hook(&unop_expr_late
, OP_HOOK
);
296 add_hook(&asm_expr_late
, ASM_HOOK
);
298 select_return_states_hook(PARAM_ADD
, &db_param_add
);
299 select_return_states_hook(PARAM_SET
, &db_param_add
);