*new* check_kernel.c: handle tomoyo_memory_ok() and friends
[smatch.git] / smatch_conditions.c
blobece0cce6ce9cd79030b30b5c2b7586e70bae55c7
1 /*
2 * sparse/smatch_conditions.c
4 * Copyright (C) 2006,2008 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
11 * The simplest type of condition is
12 * if (a) { ...
14 * The next simplest kind of conditions is
15 * if (a && b) { c;
16 * In that case 'a' is true when we get to 'b' and both are true
17 * when we get to c.
19 * Or's are a little more complicated.
20 * if (a || b) { c;
21 * We know 'a' is not true when we get to 'b' but it may be true
22 * when we get to c.
24 * If we mix and's and or's that's even more complicated.
25 * if (a && b && c || a && d) { d ;
26 * 'a' is true when we evaluate 'b', and 'd'.
27 * 'b' is true when we evaluate 'c' but otherwise we don't.
29 * The other thing that complicates matters is if we negate
30 * some if conditions.
31 * if (!a) { ...
32 * Smatch has passes the un-negated version to the client and flip
33 * the true and false values internally. This makes it easier
34 * to write checks.
36 * And negations can be part of a compound.
37 * if (a && !(b || c)) { d;
38 * In that situation we multiply the negative through to simplify
39 * stuff so that we can remove the parens like this:
40 * if (a && !b && !c) { d;
42 * One other thing is that:
43 * if ((a) != 0){ ...
44 * that's basically the same as testing for just 'a' and we simplify
45 * comparisons with zero before passing it to the script.
49 #include "smatch.h"
50 #include "smatch_slist.h"
51 #include "smatch_extra.h"
52 #include "smatch_expression_stacks.h"
54 static void split_conditions(struct expression *expr);
56 static int is_logical_and(struct expression *expr)
58 if (expr->op == SPECIAL_LOGICAL_AND)
59 return 1;
60 return 0;
63 static int handle_zero_comparisons(struct expression *expr)
65 struct expression *tmp = NULL;
67 // if left is zero or right is zero
68 if (is_zero(expr->left))
69 tmp = expr->right;
70 else if (is_zero(expr->right))
71 tmp = expr->left;
72 else
73 return 0;
75 // "if (foo != 0)" is the same as "if (foo)"
76 if (expr->op == SPECIAL_NOTEQUAL) {
77 split_conditions(tmp);
78 return 1;
81 // "if (foo == 0)" is the same as "if (!foo)"
82 if (expr->op == SPECIAL_EQUAL) {
83 split_conditions(tmp);
84 __negate_cond_stacks();
85 return 1;
88 return 0;
92 * This function is for handling calls to likely/unlikely
95 static int ignore_builtin_expect(struct expression *expr)
97 if (sym_name_is("__builtin_expect", expr->fn)) {
98 split_conditions(first_ptr_list((struct ptr_list *) expr->args));
99 return 1;
101 if (sym_name_is("__builtin_constant_p", expr->fn)) {
102 split_conditions(first_ptr_list((struct ptr_list *) expr->args));
103 return 1;
105 return 0;
109 * handle_compound_stmt() is for: foo = ({blah; blah; blah; 1})
112 static void handle_compound_stmt(struct statement *stmt)
114 struct expression *expr = NULL;
115 struct statement *last;
116 struct statement *s;
118 last = last_ptr_list((struct ptr_list *)stmt->stmts);
119 if (last->type != STMT_EXPRESSION) {
120 last = NULL;
121 } else {
122 expr = last->expression;
124 FOR_EACH_PTR(stmt->stmts, s) {
125 if (s != last)
126 __split_statements(s);
127 } END_FOR_EACH_PTR(s);
128 split_conditions(expr);
129 return;
132 static int handle_preop(struct expression *expr)
134 struct statement *stmt;
136 if (expr->op == '!') {
137 split_conditions(expr->unop);
138 __negate_cond_stacks();
139 return 1;
141 stmt = get_block_thing(expr);
142 if (stmt) {
143 handle_compound_stmt(stmt);
144 return 1;
146 return 0;
149 static void handle_logical(struct expression *expr)
152 * If we come to an "and" expr then:
153 * We split the left side.
154 * We keep all the current states.
155 * We split the right side.
156 * We keep all the states from both true sides.
158 * If it's an "or" expr then:
159 * We save the current slist.
160 * We split the left side.
161 * We use the false states for the right side.
162 * We split the right side.
163 * We save all the states that are the same on both sides.
166 split_conditions(expr->left);
168 if (!is_logical_and(expr))
169 __use_cond_false_states();
171 __push_cond_stacks();
173 __save_pre_cond_states();
174 split_conditions(expr->right);
175 __discard_pre_cond_states();
177 if (is_logical_and(expr)) {
178 __and_cond_states();
179 } else {
180 __or_cond_states();
182 __use_cond_true_states();
185 static struct state_list *combine(struct state_list *orig, struct state_list *fake,
186 struct state_list *new)
188 struct state_list *ret = NULL;
190 overwrite_slist(orig, &ret);
191 overwrite_slist(fake, &ret);
192 overwrite_slist(new, &ret);
193 free_slist(&new);
195 return ret;
199 * handle_select()
200 * if ((aaa()?bbb():ccc())) { ...
202 * This is almost the same as:
203 * if ((aaa() && bbb()) || (!aaa() && ccc())) { ...
205 * It's a bit complicated because we shouldn't pass aaa()
206 * to the clients more than once.
209 static void handle_select(struct expression *expr)
211 struct state_list *a_T = NULL;
212 struct state_list *a_F = NULL;
213 struct state_list *a_T_b_T = NULL;
214 struct state_list *a_T_b_F = NULL;
215 struct state_list *a_T_b_fake = NULL;
216 struct state_list *a_F_c_T = NULL;
217 struct state_list *a_F_c_F = NULL;
218 struct state_list *a_F_c_fake = NULL;
219 struct sm_state *sm;
222 * Imagine we have this: if (a ? b : c) { ...
224 * The condition is true if "a" is true and "b" is true or
225 * "a" is false and "c" is true. It's false if "a" is true
226 * and "b" is false or "a" is false and "c" is false.
228 * The variable name "a_T_b_T" stands for "a true b true" etc.
230 * But if we know "b" is true then we can simpilify things.
231 * The condition is true if "a" is true or if "a" is false and
232 * "c" is true. The only way the condition can be false is if
233 * "a" is false and "c" is false.
235 * The remaining thing is the "a_T_b_fake". When we simplify
236 * the equations we have to take into consideration that other
237 * states may have changed that don't play into the true false
238 * equation. Take the following example:
239 * if ({
240 * (flags) = __raw_local_irq_save();
241 * _spin_trylock(lock) ? 1 :
242 * ({ raw_local_irq_restore(flags); 0; });
243 * })
244 * Smatch has to record that the irq flags were restored on the
245 * false path.
249 __save_pre_cond_states();
251 split_conditions(expr->conditional);
253 a_T = __copy_cond_true_states();
254 a_F = __copy_cond_false_states();
256 __push_cond_stacks();
257 __push_fake_cur_slist();
258 split_conditions(expr->cond_true);
259 a_T_b_fake = __pop_fake_cur_slist();
260 a_T_b_T = combine(a_T, a_T_b_fake, __pop_cond_true_stack());
261 a_T_b_F = combine(a_T, a_T_b_fake,__pop_cond_false_stack());
263 __use_cond_false_states();
265 __push_cond_stacks();
266 __push_fake_cur_slist();
267 split_conditions(expr->cond_false);
268 a_F_c_fake = __pop_fake_cur_slist();
269 a_F_c_T = combine(a_F, a_F_c_fake, __pop_cond_true_stack());
270 a_F_c_F = combine(a_F, a_F_c_fake, __pop_cond_false_stack());
272 /* We have to restore the pre condition states so that
273 implied_condition_true() will use the right cur_slist */
274 __use_pre_cond_states();
276 if (implied_condition_true(expr->cond_true)) {
277 free_slist(&a_T_b_T);
278 free_slist(&a_T_b_F);
279 a_T_b_T = clone_slist(a_T);
280 overwrite_slist(a_T_b_fake, &a_T_b_T);
282 if (implied_condition_false(expr->cond_true)) {
283 free_slist(&a_T_b_T);
284 free_slist(&a_T_b_F);
285 a_T_b_F = clone_slist(a_T);
286 overwrite_slist(a_T_b_fake, &a_T_b_F);
288 if (implied_condition_true(expr->cond_false)) {
289 free_slist(&a_F_c_T);
290 free_slist(&a_F_c_F);
291 a_F_c_T = clone_slist(a_F);
292 overwrite_slist(a_F_c_fake, &a_F_c_T);
294 if (implied_condition_false(expr->cond_false)) {
295 free_slist(&a_F_c_T);
296 free_slist(&a_F_c_F);
297 a_F_c_F = clone_slist(a_F);
298 overwrite_slist(a_F_c_fake, &a_F_c_F);
301 merge_slist(&a_T_b_T, a_F_c_T);
302 merge_slist(&a_T_b_F, a_F_c_F);
304 __pop_cond_true_stack();
305 __pop_cond_false_stack();
306 __push_cond_stacks();
307 FOR_EACH_PTR(a_T_b_T, sm) {
308 __set_true_false_sm(sm, NULL);
309 } END_FOR_EACH_PTR(sm);
310 FOR_EACH_PTR(a_T_b_F, sm) {
311 __set_true_false_sm(NULL, sm);
312 } END_FOR_EACH_PTR(sm);
315 static void split_conditions(struct expression *expr)
317 if (option_debug) {
318 char *cond = get_variable_from_expr_complex(expr, NULL);
320 sm_debug("%d in split_conditions (%s)\n", get_lineno(), cond);
321 free_string(cond);
324 expr = strip_expr(expr);
325 if (!expr)
326 return;
328 switch (expr->type) {
329 case EXPR_LOGICAL:
330 handle_logical(expr);
331 return;
332 case EXPR_COMPARE:
333 if (handle_zero_comparisons(expr))
334 return;
335 break;
336 case EXPR_CALL:
337 if (ignore_builtin_expect(expr))
338 return;
339 break;
340 case EXPR_PREOP:
341 if (handle_preop(expr))
342 return;
343 break;
344 case EXPR_CONDITIONAL:
345 case EXPR_SELECT:
346 handle_select(expr);
347 return;
350 /* fixme: this should be in smatch_flow.c
351 but because of the funny stuff we do with conditions
352 it's awkward to put it there. We would need to
353 call CONDITION_HOOK in smatch_flow as well.
355 push_expression(&big_expression_stack, expr);
356 if (expr->type == EXPR_COMPARE) {
357 if (expr->left->type != EXPR_POSTOP)
358 __split_expr(expr->left);
359 if (expr->right->type != EXPR_POSTOP)
360 __split_expr(expr->right);
361 } else if (expr->type != EXPR_POSTOP) {
362 __split_expr(expr);
364 __pass_to_client(expr, CONDITION_HOOK);
365 if (expr->type == EXPR_COMPARE) {
366 if (expr->left->type == EXPR_POSTOP)
367 __split_expr(expr->left);
368 if (expr->right->type == EXPR_POSTOP)
369 __split_expr(expr->right);
370 } else if (expr->type == EXPR_POSTOP) {
371 __split_expr(expr);
373 pop_expression(&big_expression_stack);
376 static int inside_condition;
377 void __split_whole_condition(struct expression *expr)
379 sm_debug("%d in __split_whole_condition\n", get_lineno());
380 inside_condition++;
381 __save_pre_cond_states();
382 __push_cond_stacks();
383 /* it's a hack, but it's sometimes handy to have this stuff
384 on the big_expression_stack. */
385 push_expression(&big_expression_stack, expr);
386 if (expr)
387 split_conditions(expr);
388 __use_cond_states();
389 __pass_to_client(expr, WHOLE_CONDITION_HOOK);
390 pop_expression(&big_expression_stack);
391 inside_condition--;
392 sm_debug("%d done __split_whole_condition\n", get_lineno());
395 static int is_condition_assign(struct expression *expr)
397 struct expression *right;
399 right = strip_expr(expr->right);
400 switch(right->type) {
401 case EXPR_LOGICAL:
402 case EXPR_COMPARE:
403 break;
404 case EXPR_PREOP:
405 if (right->op == '!')
406 break;
407 return 0;
408 default:
409 return 0;
411 return 1;
414 int __handle_condition_assigns(struct expression *expr)
416 struct expression *right;
418 right = strip_expr(expr->right);
419 if (!is_condition_assign(expr))
420 return 0;
422 sm_debug("%d in __handle_condition_assigns\n", get_lineno());
423 inside_condition++;
424 __save_pre_cond_states();
425 __push_cond_stacks();
426 /* it's a hack, but it's sometimes handy to have this stuff
427 on the big_expression_stack. */
428 push_expression(&big_expression_stack, right);
429 split_conditions(right);
430 set_true_false_states_expr(SMATCH_EXTRA, expr->left, alloc_extra_state(1), alloc_extra_state(0));
431 __use_cond_states();
432 __pass_to_client(right, WHOLE_CONDITION_HOOK);
433 pop_expression(&big_expression_stack);
434 inside_condition--;
435 sm_debug("%d done __handle_condition_assigns\n", get_lineno());
437 __push_true_states();
438 __use_false_states();
439 __merge_true_states();
440 return 1;
443 static int is_select_assign(struct expression *expr)
445 struct expression *right;
447 right = strip_expr(expr->right);
448 if (right->type == EXPR_CONDITIONAL)
449 return 1;
450 if (right->type == EXPR_SELECT)
451 return 1;
452 return 0;
455 static struct expression *alloc_pseudo_assign(struct expression *left, struct expression *right)
457 struct expression *e_assign;
459 e_assign = alloc_expression(left->pos, EXPR_ASSIGNMENT);
460 e_assign->op = (int)'=';
461 e_assign->left = left;
462 e_assign->right = right;
463 return e_assign;
466 int __handle_select_assigns(struct expression *expr)
468 struct expression *right;
469 struct expression *fake_expr;
470 struct state_list *final_states = NULL;
471 struct sm_state *sm;
472 int is_true;
473 int is_false;
475 if (!is_select_assign(expr))
476 return 0;
477 sm_debug("%d in __handle_ternary_assigns\n", get_lineno());
478 right = strip_expr(expr->right);
480 is_true = implied_condition_true(right->conditional);
481 is_false = implied_condition_false(right->conditional);
483 /* hah hah. the ultra fake out */
484 __save_pre_cond_states();
485 __split_whole_condition(right->conditional);
487 if (!is_false) {
488 if (right->cond_true)
489 fake_expr = alloc_pseudo_assign(expr->left, right->cond_true);
490 else
491 fake_expr = alloc_pseudo_assign(expr->left, right->conditional);
492 __split_expr(fake_expr);
493 final_states = clone_slist(__get_cur_slist());
496 __use_false_states();
497 if (!is_true) {
498 fake_expr = alloc_pseudo_assign(expr->left, right->cond_false);
499 __split_expr(fake_expr);
500 merge_slist(&final_states, __get_cur_slist());
503 __use_pre_cond_states();
505 FOR_EACH_PTR(final_states, sm) {
506 __set_sm(sm);
507 } END_FOR_EACH_PTR(sm);
509 free_slist(&final_states);
511 sm_debug("%d done __handle_ternary_assigns\n", get_lineno());
513 return 1;
516 int in_condition(void)
518 return inside_condition;