2 * Copyright (C) 2006,2008 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 * The simplest type of condition is
22 * The next simplest kind of conditions is
24 * In that case 'a' is true when we get to 'b' and both are true
27 * Or's are a little more complicated.
29 * We know 'a' is not true when we get to 'b' but it may be true
32 * If we mix and's and or's that's even more complicated.
33 * if (a && b && c || a && d) { d ;
34 * 'a' is true when we evaluate 'b', and 'd'.
35 * 'b' is true when we evaluate 'c' but otherwise we don't.
37 * The other thing that complicates matters is if we negate
40 * Smatch has passes the un-negated version to the client and flip
41 * the true and false values internally. This makes it easier
44 * And negations can be part of a compound.
45 * if (a && !(b || c)) { d;
46 * In that situation we multiply the negative through to simplify
47 * stuff so that we can remove the parens like this:
48 * if (a && !b && !c) { d;
50 * One other thing is that:
52 * that's basically the same as testing for just 'a' and we simplify
53 * comparisons with zero before passing it to the script.
58 #include "smatch_slist.h"
59 #include "smatch_extra.h"
60 #include "smatch_expression_stacks.h"
62 extern int __expr_stmt_count
;
64 struct expression_list
*big_condition_stack
;
66 static void split_conditions(struct expression
*expr
);
68 static int is_logical_and(struct expression
*expr
)
70 if (expr
->op
== SPECIAL_LOGICAL_AND
)
75 static int handle_zero_comparisons(struct expression
*expr
)
77 struct expression
*tmp
= NULL
;
78 struct expression
*zero
;
80 // if left is zero or right is zero
81 if (expr_is_zero(expr
->left
)) {
82 zero
= strip_expr(expr
->left
);
83 if (zero
->type
!= EXPR_VALUE
)
84 __split_expr(expr
->left
);
86 } else if (expr_is_zero(expr
->right
)) {
87 zero
= strip_expr(expr
->left
);
88 if (zero
->type
!= EXPR_VALUE
)
89 __split_expr(expr
->right
);
95 // "if (foo != 0)" is the same as "if (foo)"
96 if (expr
->op
== SPECIAL_NOTEQUAL
) {
97 split_conditions(tmp
);
101 // "if (foo == 0)" is the same as "if (!foo)"
102 if (expr
->op
== SPECIAL_EQUAL
) {
103 split_conditions(tmp
);
104 __negate_cond_stacks();
112 * This function is for handling calls to likely/unlikely
115 static int ignore_builtin_expect(struct expression
*expr
)
117 if (sym_name_is("__builtin_expect", expr
->fn
)) {
118 split_conditions(first_ptr_list((struct ptr_list
*) expr
->args
));
125 * handle_compound_stmt() is for: foo = ({blah; blah; blah; 1})
128 static void handle_compound_stmt(struct statement
*stmt
)
130 struct expression
*expr
= NULL
;
131 struct statement
*last
;
134 last
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
135 if (last
->type
== STMT_LABEL
) {
136 if (last
->label_statement
&&
137 last
->label_statement
->type
== STMT_EXPRESSION
)
138 expr
= last
->label_statement
->expression
;
141 } else if (last
->type
!= STMT_EXPRESSION
) {
144 expr
= last
->expression
;
147 FOR_EACH_PTR(stmt
->stmts
, s
) {
150 } END_FOR_EACH_PTR(s
);
151 if (last
&& last
->type
== STMT_LABEL
)
152 __split_label_stmt(last
);
153 split_conditions(expr
);
156 static int handle_preop(struct expression
*expr
)
158 struct statement
*stmt
;
160 if (expr
->op
== '!') {
161 split_conditions(expr
->unop
);
162 __negate_cond_stacks();
165 stmt
= get_expression_statement(expr
);
167 handle_compound_stmt(stmt
);
173 static void handle_logical(struct expression
*expr
)
176 * If we come to an "and" expr then:
177 * We split the left side.
178 * We keep all the current states.
179 * We split the right side.
180 * We keep all the states from both true sides.
182 * If it's an "or" expr then:
183 * We save the current slist.
184 * We split the left side.
185 * We use the false states for the right side.
186 * We split the right side.
187 * We save all the states that are the same on both sides.
190 split_conditions(expr
->left
);
192 if (is_logical_and(expr
))
193 __use_cond_true_states();
195 __use_cond_false_states();
197 __push_cond_stacks();
199 __save_pre_cond_states();
200 split_conditions(expr
->right
);
201 __discard_pre_cond_states();
203 if (is_logical_and(expr
))
208 __use_cond_true_states();
211 static struct stree
*combine_strees(struct stree
*orig
, struct stree
*fake
, struct stree
*new)
213 struct stree
*ret
= NULL
;
215 overwrite_stree(orig
, &ret
);
216 overwrite_stree(fake
, &ret
);
217 overwrite_stree(new, &ret
);
225 * if ((aaa()?bbb():ccc())) { ...
227 * This is almost the same as:
228 * if ((aaa() && bbb()) || (!aaa() && ccc())) { ...
230 * It's a bit complicated because we shouldn't pass aaa()
231 * to the clients more than once.
234 static void handle_select(struct expression
*expr
)
236 struct stree
*a_T
= NULL
;
237 struct stree
*a_F
= NULL
;
238 struct stree
*a_T_b_T
= NULL
;
239 struct stree
*a_T_b_F
= NULL
;
240 struct stree
*a_T_b_fake
= NULL
;
241 struct stree
*a_F_c_T
= NULL
;
242 struct stree
*a_F_c_F
= NULL
;
243 struct stree
*a_F_c_fake
= NULL
;
248 * Imagine we have this: if (a ? b : c) { ...
250 * The condition is true if "a" is true and "b" is true or
251 * "a" is false and "c" is true. It's false if "a" is true
252 * and "b" is false or "a" is false and "c" is false.
254 * The variable name "a_T_b_T" stands for "a true b true" etc.
256 * But if we know "b" is true then we can simpilify things.
257 * The condition is true if "a" is true or if "a" is false and
258 * "c" is true. The only way the condition can be false is if
259 * "a" is false and "c" is false.
261 * The remaining thing is the "a_T_b_fake". When we simplify
262 * the equations we have to take into consideration that other
263 * states may have changed that don't play into the true false
264 * equation. Take the following example:
266 * (flags) = __raw_local_irq_save();
267 * _spin_trylock(lock) ? 1 :
268 * ({ raw_local_irq_restore(flags); 0; });
270 * Smatch has to record that the irq flags were restored on the
275 __save_pre_cond_states();
277 split_conditions(expr
->conditional
);
279 a_T
= __copy_cond_true_states();
280 a_F
= __copy_cond_false_states();
282 __use_cond_true_states();
284 __push_cond_stacks();
285 __push_fake_cur_stree();
286 split_conditions(expr
->cond_true
);
287 __process_post_op_stack();
288 a_T_b_fake
= __pop_fake_cur_stree();
289 a_T_b_T
= combine_strees(a_T
, a_T_b_fake
, __pop_cond_true_stack());
290 a_T_b_F
= combine_strees(a_T
, a_T_b_fake
, __pop_cond_false_stack());
292 __use_cond_false_states();
294 __push_cond_stacks();
295 __push_fake_cur_stree();
296 split_conditions(expr
->cond_false
);
297 a_F_c_fake
= __pop_fake_cur_stree();
298 a_F_c_T
= combine_strees(a_F
, a_F_c_fake
, __pop_cond_true_stack());
299 a_F_c_F
= combine_strees(a_F
, a_F_c_fake
, __pop_cond_false_stack());
301 /* We have to restore the pre condition states so that
302 implied_condition_true() will use the right cur_stree */
303 __use_pre_cond_states();
305 if (implied_condition_true(expr
->cond_true
)) {
306 free_stree(&a_T_b_T
);
307 free_stree(&a_T_b_F
);
308 a_T_b_T
= clone_stree(a_T
);
309 overwrite_stree(a_T_b_fake
, &a_T_b_T
);
311 if (implied_condition_false(expr
->cond_true
)) {
312 free_stree(&a_T_b_T
);
313 free_stree(&a_T_b_F
);
314 a_T_b_F
= clone_stree(a_T
);
315 overwrite_stree(a_T_b_fake
, &a_T_b_F
);
317 if (implied_condition_true(expr
->cond_false
)) {
318 free_stree(&a_F_c_T
);
319 free_stree(&a_F_c_F
);
320 a_F_c_T
= clone_stree(a_F
);
321 overwrite_stree(a_F_c_fake
, &a_F_c_T
);
323 if (implied_condition_false(expr
->cond_false
)) {
324 free_stree(&a_F_c_T
);
325 free_stree(&a_F_c_F
);
326 a_F_c_F
= clone_stree(a_F
);
327 overwrite_stree(a_F_c_fake
, &a_F_c_F
);
330 merge_stree(&a_T_b_T
, a_F_c_T
);
331 merge_stree(&a_T_b_F
, a_F_c_F
);
333 tmp
= __pop_cond_true_stack();
335 tmp
= __pop_cond_false_stack();
338 __push_cond_stacks();
339 FOR_EACH_SM(a_T_b_T
, sm
) {
340 __set_true_false_sm(sm
, NULL
);
341 } END_FOR_EACH_SM(sm
);
342 FOR_EACH_SM(a_T_b_F
, sm
) {
343 __set_true_false_sm(NULL
, sm
);
344 } END_FOR_EACH_SM(sm
);
347 free_stree(&a_T_b_fake
);
348 free_stree(&a_F_c_fake
);
349 free_stree(&a_F_c_T
);
350 free_stree(&a_F_c_F
);
351 free_stree(&a_T_b_T
);
352 free_stree(&a_T_b_F
);
357 static void handle_comma(struct expression
*expr
)
359 __split_expr(expr
->left
);
360 split_conditions(expr
->right
);
363 static int make_op_unsigned(int op
)
367 return SPECIAL_UNSIGNED_LT
;
369 return SPECIAL_UNSIGNED_LTE
;
371 return SPECIAL_UNSIGNED_GT
;
373 return SPECIAL_UNSIGNED_GTE
;
378 static void hackup_unsigned_compares(struct expression
*expr
)
380 if (expr
->type
!= EXPR_COMPARE
)
383 if (type_unsigned(get_type(expr
)))
384 expr
->op
= make_op_unsigned(expr
->op
);
387 static bool handle_expr_statement_conditions(struct expression
*expr
)
389 struct statement
*last_stmt
, *stmt
;
390 struct expression
*last_expr
;
392 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '(')
394 if (expr
->type
!= EXPR_STATEMENT
)
397 stmt
= expr
->statement
;
398 if (stmt
->type
!= STMT_COMPOUND
)
401 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
406 * I don't think this is right, but I'm not sure how to handle other
407 * types of statements.
409 if (last_stmt
->type
== STMT_EXPRESSION
)
410 last_expr
= last_stmt
->expression
;
411 else if (last_stmt
->type
== STMT_LABEL
&&
412 last_stmt
->label_statement
->type
== STMT_EXPRESSION
)
413 last_expr
= last_stmt
->label_statement
->expression
;
418 __push_scope_hooks();
419 FOR_EACH_PTR(stmt
->stmts
, stmt
) {
420 if (stmt
== last_stmt
) {
421 if (stmt
->type
== STMT_LABEL
)
422 __split_label_stmt(stmt
);
423 split_conditions(last_expr
);
427 } END_FOR_EACH_PTR(stmt
);
430 __call_scope_hooks();
436 static void do_condition(struct expression
*expr
)
438 __fold_in_set_states();
439 __push_fake_cur_stree();
440 __pass_to_client(expr
, CONDITION_HOOK
);
441 __fold_in_set_states();
444 static void split_conditions(struct expression
*expr
)
447 char *cond
= expr_to_str(expr
);
449 sm_msg("%d in split_conditions (%s)", get_lineno(), cond
);
453 expr
= strip_expr_set_parent(expr
);
455 __fold_in_set_states();
459 if (handle_expr_statement_conditions(expr
))
463 * On fast paths (and also I guess some people think it's cool) people
464 * sometimes use | instead of ||. It works the same basically except
465 * that || implies a memory barrier between conditions. The easiest way
466 * to handle it is by pretending that | also has a barrier and re-using
467 * all the normal condition code. This potentially hides some bugs, but
468 * people who write code like this should just be careful or they
471 * We could potentially treat boolean bitwise & this way but that seems
472 * too complicated to deal with.
474 if (expr
->type
== EXPR_BINOP
&& expr
->op
== '|') {
475 expr_set_parent_expr(expr
->left
, expr
);
476 expr_set_parent_expr(expr
->right
, expr
);
477 handle_logical(expr
);
481 switch (expr
->type
) {
483 expr_set_parent_expr(expr
->left
, expr
);
484 expr_set_parent_expr(expr
->right
, expr
);
485 __pass_to_client(expr
, LOGIC_HOOK
);
486 handle_logical(expr
);
489 expr_set_parent_expr(expr
->left
, expr
);
490 expr_set_parent_expr(expr
->right
, expr
);
491 hackup_unsigned_compares(expr
);
492 if (handle_zero_comparisons(expr
))
496 if (ignore_builtin_expect(expr
))
500 expr_set_parent_expr(expr
->unop
, expr
);
501 if (handle_preop(expr
))
504 case EXPR_CONDITIONAL
:
506 expr_set_parent_expr(expr
->conditional
, expr
);
507 expr_set_parent_expr(expr
->cond_true
, expr
);
508 expr_set_parent_expr(expr
->cond_false
, expr
);
512 expr_set_parent_expr(expr
->left
, expr
);
513 expr_set_parent_expr(expr
->right
, expr
);
518 /* fixme: this should be in smatch_flow.c
519 but because of the funny stuff we do with conditions
520 it's awkward to put it there. We would need to
521 call CONDITION_HOOK in smatch_flow as well.
523 push_expression(&big_expression_stack
, expr
);
524 push_expression(&big_condition_stack
, expr
);
526 if (expr
->type
== EXPR_COMPARE
) {
527 if (expr
->left
->type
!= EXPR_POSTOP
)
528 __split_expr(expr
->left
);
529 if (expr
->right
->type
!= EXPR_POSTOP
)
530 __split_expr(expr
->right
);
531 } else if (expr
->type
!= EXPR_POSTOP
) {
535 if (expr
->type
== EXPR_COMPARE
) {
536 if (expr
->left
->type
== EXPR_POSTOP
)
537 __split_expr(expr
->left
);
538 if (expr
->right
->type
== EXPR_POSTOP
)
539 __split_expr(expr
->right
);
540 } else if (expr
->type
== EXPR_POSTOP
) {
543 __push_fake_cur_stree();
544 __process_post_op_stack();
545 __fold_in_set_states();
546 pop_expression(&big_condition_stack
);
547 pop_expression(&big_expression_stack
);
550 static int inside_condition
;
551 void __split_whole_condition(struct expression
*expr
)
554 __save_pre_cond_states();
555 __push_cond_stacks();
556 /* it's a hack, but it's sometimes handy to have this stuff
557 on the big_expression_stack. */
558 push_expression(&big_expression_stack
, expr
);
559 split_conditions(expr
);
561 __pass_to_client(expr
, WHOLE_CONDITION_HOOK
);
562 pop_expression(&big_expression_stack
);
566 void __handle_logic(struct expression
*expr
)
569 __save_pre_cond_states();
570 __push_cond_stacks();
571 /* it's a hack, but it's sometimes handy to have this stuff
572 on the big_expression_stack. */
573 push_expression(&big_expression_stack
, expr
);
575 split_conditions(expr
);
577 __pass_to_client(expr
, WHOLE_CONDITION_HOOK
);
578 pop_expression(&big_expression_stack
);
579 __merge_false_states();
583 int is_condition(struct expression
*expr
)
586 expr
= strip_expr(expr
);
590 switch (expr
->type
) {
601 int __handle_condition_assigns(struct expression
*expr
)
603 struct expression
*right
;
604 struct stree
*true_stree
, *false_stree
, *fake_stree
;
609 right
= strip_expr(expr
->right
);
610 if (!is_condition(expr
->right
))
614 __save_pre_cond_states();
615 __push_cond_stacks();
616 /* it's a hack, but it's sometimes handy to have this stuff
617 on the big_expression_stack. */
618 push_expression(&big_expression_stack
, right
);
619 split_conditions(right
);
620 true_stree
= __get_true_states();
621 false_stree
= __get_false_states();
623 __push_fake_cur_stree();
624 set_extra_expr_mod(expr
->left
, alloc_estate_sval(sval_type_val(get_type(expr
->left
), 1)));
625 __pass_to_client(right
, WHOLE_CONDITION_HOOK
);
627 fake_stree
= __pop_fake_cur_stree();
628 FOR_EACH_SM(fake_stree
, sm
) {
629 overwrite_sm_state_stree(&true_stree
, sm
);
630 } END_FOR_EACH_SM(sm
);
631 free_stree(&fake_stree
);
633 pop_expression(&big_expression_stack
);
636 __push_true_states();
638 __use_false_states();
639 __push_fake_cur_stree();
640 set_extra_expr_mod(expr
->left
, alloc_estate_sval(sval_type_val(get_type(expr
->left
), 0)));
642 fake_stree
= __pop_fake_cur_stree();
643 FOR_EACH_SM(fake_stree
, sm
) {
644 overwrite_sm_state_stree(&false_stree
, sm
);
645 } END_FOR_EACH_SM(sm
);
646 free_stree(&fake_stree
);
648 __merge_true_states();
649 merge_fake_stree(&true_stree
, false_stree
);
650 free_stree(&false_stree
);
651 FOR_EACH_SM(true_stree
, sm
) {
653 } END_FOR_EACH_SM(sm
);
655 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
660 static int is_select_assign(struct expression
*expr
)
662 struct expression
*right
;
666 right
= strip_expr(expr
->right
);
667 if (right
->type
== EXPR_CONDITIONAL
)
669 if (right
->type
== EXPR_SELECT
)
674 int __handle_select_assigns(struct expression
*expr
)
676 struct expression
*right
, *condition
;
677 struct stree
*final_states
= NULL
;
682 if (!is_select_assign(expr
))
684 right
= strip_expr(expr
->right
);
685 __pass_to_client(right
, SELECT_HOOK
);
687 // FIXME: why is this implied instead of known?
688 is_true
= implied_condition_true(right
->conditional
);
689 is_false
= implied_condition_false(right
->conditional
);
692 * For "x = frob() ?: y;" we only want to parse the frob() call once
693 * so do the assignment and parse the condition in one step.
695 if (right
->cond_true
)
696 condition
= right
->conditional
;
698 condition
= assign_expression(expr
->left
, expr
->op
, right
->conditional
);
700 expr_set_parent_expr(condition
, right
);
702 __save_pre_cond_states();
703 __split_whole_condition(condition
);
705 if (!is_false
&& right
->cond_true
) {
706 struct expression
*fake_expr
;
708 fake_expr
= assign_expression(expr
->left
, expr
->op
, right
->cond_true
);
709 expr_set_parent_expr(fake_expr
, expr
);
710 __split_expr(fake_expr
);
711 final_states
= clone_stree(__get_cur_stree());
712 } else if (!is_false
) {
713 final_states
= clone_stree(__get_cur_stree());
717 __discard_false_states();
718 merge_stree(&final_states
, __get_cur_stree());
720 struct expression
*fake_expr
;
722 __use_false_states();
723 fake_expr
= assign_expression(expr
->left
, expr
->op
, right
->cond_false
);
724 expr_set_parent_expr(fake_expr
, expr
);
725 __split_expr(fake_expr
);
726 merge_stree(&final_states
, __get_cur_stree());
729 __use_pre_cond_states();
731 FOR_EACH_SM(final_states
, sm
) {
733 } END_FOR_EACH_SM(sm
);
735 free_stree(&final_states
);
740 static struct statement
*split_then_return_last(struct statement
*stmt
)
742 struct statement
*tmp
;
743 struct statement
*last_stmt
;
745 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
749 __push_scope_hooks();
750 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
751 if (tmp
== last_stmt
) {
752 if (tmp
->type
== STMT_LABEL
) {
753 __split_label_stmt(tmp
);
754 return tmp
->label_statement
;
759 } END_FOR_EACH_PTR(tmp
);
763 int __handle_expr_statement_assigns(struct expression
*expr
)
765 struct expression
*right
;
766 struct statement
*stmt
;
769 if (right
->type
== EXPR_PREOP
&& right
->op
== '(')
771 if (right
->type
!= EXPR_STATEMENT
)
775 stmt
= right
->statement
;
776 if (stmt
->type
== STMT_COMPOUND
) {
777 struct statement
*last_stmt
;
778 struct expression
*fake_assign
;
779 struct expression fake_expr_stmt
= { .smatch_flags
= Fake
, };
781 last_stmt
= split_then_return_last(stmt
);
787 fake_expr_stmt
.pos
= last_stmt
->pos
;
788 fake_expr_stmt
.type
= EXPR_STATEMENT
;
789 fake_expr_stmt
.op
= 0;
790 fake_expr_stmt
.statement
= last_stmt
;
792 fake_assign
= assign_expression(expr
->left
, expr
->op
, &fake_expr_stmt
);
793 expr_set_parent_expr(fake_assign
, expr
);
794 __split_expr(fake_assign
);
796 __pass_to_client(stmt
, STMT_HOOK_AFTER
);
797 __call_scope_hooks();
798 } else if (stmt
->type
== STMT_EXPRESSION
) {
799 struct expression
*fake_assign
;
801 fake_assign
= assign_expression(expr
->left
, expr
->op
, stmt
->expression
);
802 expr_set_parent_expr(fake_assign
, expr
);
803 __split_expr(fake_assign
);
812 int in_condition(void)
814 return inside_condition
;