Search in sources :

Example 11 with SymbolicValue

use of org.sonar.java.se.symbolicvalues.SymbolicValue in project sonar-java by SonarSource.

the class ExplodedGraphWalker method visit.

private void visit(Tree tree, @Nullable Tree terminator) {
    if (!checkerDispatcher.executeCheckPreStatement(tree)) {
        // Some of the check pre statement sink the execution on this node.
        return;
    }
    switch(tree.kind()) {
        case METHOD_INVOCATION:
            MethodInvocationTree mit = (MethodInvocationTree) tree;
            if (SYSTEM_EXIT_MATCHER.matches(mit)) {
                // System exit is a sink of execution
                return;
            }
            executeMethodInvocation(mit);
            return;
        case LABELED_STATEMENT:
        case SWITCH_STATEMENT:
        case EXPRESSION_STATEMENT:
        case PARENTHESIZED_EXPRESSION:
            throw new IllegalStateException("Cannot appear in CFG: " + tree.kind().name());
        case VARIABLE:
            executeVariable((VariableTree) tree, terminator);
            break;
        case TYPE_CAST:
            executeTypeCast((TypeCastTree) tree);
            break;
        case ASSIGNMENT:
        case MULTIPLY_ASSIGNMENT:
        case DIVIDE_ASSIGNMENT:
        case REMAINDER_ASSIGNMENT:
        case PLUS_ASSIGNMENT:
        case MINUS_ASSIGNMENT:
        case LEFT_SHIFT_ASSIGNMENT:
        case RIGHT_SHIFT_ASSIGNMENT:
        case UNSIGNED_RIGHT_SHIFT_ASSIGNMENT:
            executeAssignment((AssignmentExpressionTree) tree);
            break;
        case AND_ASSIGNMENT:
        case XOR_ASSIGNMENT:
        case OR_ASSIGNMENT:
            executeLogicalAssignment((AssignmentExpressionTree) tree);
            break;
        case ARRAY_ACCESS_EXPRESSION:
            executeArrayAccessExpression((ArrayAccessExpressionTree) tree);
            break;
        case NEW_ARRAY:
            executeNewArray((NewArrayTree) tree);
            break;
        case NEW_CLASS:
            executeNewClass((NewClassTree) tree);
            break;
        case MULTIPLY:
        case DIVIDE:
        case REMAINDER:
        case PLUS:
        case MINUS:
        case LEFT_SHIFT:
        case RIGHT_SHIFT:
        case UNSIGNED_RIGHT_SHIFT:
        case AND:
        case XOR:
        case OR:
        case GREATER_THAN:
        case GREATER_THAN_OR_EQUAL_TO:
        case LESS_THAN:
        case LESS_THAN_OR_EQUAL_TO:
        case EQUAL_TO:
        case NOT_EQUAL_TO:
            executeBinaryExpression(tree);
            break;
        case POSTFIX_INCREMENT:
        case POSTFIX_DECREMENT:
        case PREFIX_INCREMENT:
        case PREFIX_DECREMENT:
        case UNARY_MINUS:
        case UNARY_PLUS:
        case BITWISE_COMPLEMENT:
        case LOGICAL_COMPLEMENT:
        case INSTANCE_OF:
            executeUnaryExpression(tree);
            break;
        case IDENTIFIER:
            executeIdentifier((IdentifierTree) tree);
            break;
        case MEMBER_SELECT:
            executeMemberSelect((MemberSelectExpressionTree) tree);
            break;
        case INT_LITERAL:
        case LONG_LITERAL:
        case FLOAT_LITERAL:
        case DOUBLE_LITERAL:
        case CHAR_LITERAL:
        case STRING_LITERAL:
            SymbolicValue val = constraintManager.createSymbolicValue(tree);
            programState = programState.stackValue(val);
            programState = programState.addConstraint(val, ObjectConstraint.NOT_NULL);
            break;
        case BOOLEAN_LITERAL:
            boolean value = Boolean.parseBoolean(((LiteralTree) tree).value());
            programState = programState.stackValue(value ? SymbolicValue.TRUE_LITERAL : SymbolicValue.FALSE_LITERAL);
            break;
        case NULL_LITERAL:
            programState = programState.stackValue(SymbolicValue.NULL_LITERAL);
            break;
        case LAMBDA_EXPRESSION:
        case METHOD_REFERENCE:
            programState = programState.stackValue(constraintManager.createSymbolicValue(tree));
            break;
        case ASSERT_STATEMENT:
            executeAssertStatement(tree);
            return;
        default:
    }
    checkerDispatcher.executeCheckPostStatement(tree);
    clearStack(tree);
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)

Example 12 with SymbolicValue

use of org.sonar.java.se.symbolicvalues.SymbolicValue in project sonar-java by SonarSource.

the class ExplodedGraphWalker method executeTypeCast.

private void executeTypeCast(TypeCastTree typeCast) {
    Type type = typeCast.type().symbolType();
    if (type.isPrimitive()) {
        JavaType expType = (JavaType) typeCast.expression().symbolType();
        // create SV to consume factory if any
        SymbolicValue castSV = constraintManager.createSymbolicValue(typeCast);
        // if exp type is a primitive and subtype of cast type, we can reuse the same symbolic value
        if (!expType.isPrimitive() || !new Types().isSubtype(expType, (JavaType) type)) {
            ProgramState.Pop unstack = programState.unstackValue(1);
            programState = unstack.state;
            programState = programState.stackValue(castSV);
        }
    }
}
Also used : Types(org.sonar.java.resolve.Types) JavaType(org.sonar.java.resolve.JavaType) Type(org.sonar.plugins.java.api.semantic.Type) JavaType(org.sonar.java.resolve.JavaType) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)

Example 13 with SymbolicValue

use of org.sonar.java.se.symbolicvalues.SymbolicValue in project sonar-java by SonarSource.

the class ExplodedGraphWalker method executeAssignment.

private void executeAssignment(AssignmentExpressionTree tree) {
    ProgramState.Pop unstack;
    SymbolicValue value;
    if (tree.is(Tree.Kind.ASSIGNMENT)) {
        unstack = ExpressionUtils.isSimpleAssignment(tree) ? programState.unstackValue(1) : programState.unstackValue(2);
        value = unstack.values.get(0);
    } else {
        unstack = programState.unstackValue(2);
        value = constraintManager.createSymbolicValue(tree);
    }
    programState = unstack.state;
    Symbol symbol = null;
    if (tree.variable().is(Tree.Kind.IDENTIFIER) || ExpressionUtils.isSelectOnThisOrSuper(tree)) {
        symbol = ExpressionUtils.extractIdentifier(tree).symbol();
        programState = programState.put(symbol, value);
    }
    programState = programState.stackValue(value, symbol);
}
Also used : JavaSymbol(org.sonar.java.resolve.JavaSymbol) Symbol(org.sonar.plugins.java.api.semantic.Symbol) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)

Example 14 with SymbolicValue

use of org.sonar.java.se.symbolicvalues.SymbolicValue in project sonar-java by SonarSource.

the class ExplodedGraphWalker method executeLogicalAssignment.

private void executeLogicalAssignment(AssignmentExpressionTree tree) {
    ExpressionTree variable = tree.variable();
    // FIXME handle also assignments with this SONARJAVA-2242
    if (variable.is(Tree.Kind.IDENTIFIER)) {
        ProgramState.Pop unstack = programState.unstackValue(2);
        ProgramState.SymbolicValueSymbol assignedTo = unstack.valuesAndSymbols.get(1);
        ProgramState.SymbolicValueSymbol value = unstack.valuesAndSymbols.get(0);
        programState = unstack.state;
        SymbolicValue symbolicValue = constraintManager.createBinarySymbolicValue(tree, ImmutableList.of(assignedTo, value));
        Symbol symbol = ((IdentifierTree) variable).symbol();
        programState = programState.stackValue(symbolicValue, symbol);
        programState = programState.put(symbol, symbolicValue);
    }
}
Also used : JavaSymbol(org.sonar.java.resolve.JavaSymbol) Symbol(org.sonar.plugins.java.api.semantic.Symbol) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)

Example 15 with SymbolicValue

use of org.sonar.java.se.symbolicvalues.SymbolicValue in project sonar-java by SonarSource.

the class ExplodedGraphWalker method executeUnaryExpression.

private void executeUnaryExpression(Tree tree) {
    // consume one and produce one
    ProgramState.Pop unstackUnary = programState.unstackValue(1);
    programState = unstackUnary.state;
    SymbolicValue unarySymbolicValue = constraintManager.createSymbolicValue(tree);
    unarySymbolicValue.computedFrom(unstackUnary.valuesAndSymbols);
    ProgramState.SymbolicValueSymbol symbolicValueSymbol = unstackUnary.valuesAndSymbols.get(0);
    if (tree.is(Tree.Kind.POSTFIX_DECREMENT, Tree.Kind.POSTFIX_INCREMENT)) {
        programState = programState.stackValue(symbolicValueSymbol.sv, symbolicValueSymbol.symbol);
    } else {
        programState = programState.stackValue(unarySymbolicValue);
    }
    if (tree.is(Tree.Kind.POSTFIX_DECREMENT, Tree.Kind.POSTFIX_INCREMENT, Tree.Kind.PREFIX_DECREMENT, Tree.Kind.PREFIX_INCREMENT) && symbolicValueSymbol.symbol != null) {
        programState = programState.put(symbolicValueSymbol.symbol, unarySymbolicValue);
    }
}
Also used : SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)

Aggregations

SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)132 RelationalSymbolicValue (org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)94 Test (org.junit.Test)79 ProgramState (org.sonar.java.se.ProgramState)74 BinarySymbolicValue (org.sonar.java.se.symbolicvalues.BinarySymbolicValue)55 Instruction (org.sonar.java.bytecode.cfg.Instruction)52 ObjectConstraint (org.sonar.java.se.constraint.ObjectConstraint)38 BooleanConstraint (org.sonar.java.se.constraint.BooleanConstraint)36 ProgramPoint (org.sonar.java.se.ProgramPoint)30 Constraint (org.sonar.java.se.constraint.Constraint)29 TypedConstraint (org.sonar.java.se.constraint.TypedConstraint)22 Type (org.sonar.plugins.java.api.semantic.Type)18 Symbol (org.sonar.plugins.java.api.semantic.Symbol)17 JavaSymbol (org.sonar.java.resolve.JavaSymbol)16 ConstraintsByDomain (org.sonar.java.se.constraint.ConstraintsByDomain)16 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)15 List (java.util.List)13 Collectors (java.util.stream.Collectors)11 VisibleForTesting (com.google.common.annotations.VisibleForTesting)10 Lists (com.google.common.collect.Lists)10