Search in sources :

Example 16 with SymbolicValue

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

the class ExplodedGraphWalker method executeNewClass.

private void executeNewClass(NewClassTree tree) {
    NewClassTree newClassTree = tree;
    programState = programState.unstackValue(newClassTree.arguments().size()).state;
    // Enqueue exceptional paths
    ((CFG.Block) node.programPoint.block).exceptions().forEach(b -> enqueue(new ProgramPoint(b), programState, !b.isCatchBlock()));
    SymbolicValue svNewClass = constraintManager.createSymbolicValue(newClassTree);
    programState = programState.stackValue(svNewClass);
    programState = svNewClass.setSingleConstraint(programState, ObjectConstraint.NOT_NULL);
}
Also used : CFG(org.sonar.java.cfg.CFG) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)

Example 17 with SymbolicValue

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

the class ExplodedGraphWalker method executeVariable.

/**
 * @see JLS8 4.12.5 for details
 */
private void executeVariable(VariableTree variableTree, @Nullable Tree terminator) {
    Symbol variableSymbol = variableTree.symbol();
    if (variableTree.initializer() == null) {
        SymbolicValue sv = null;
        if (terminator != null && terminator.is(Tree.Kind.FOR_EACH_STATEMENT)) {
            sv = constraintManager.createSymbolicValue(variableTree);
            if (isAnnotatedNonNull(variableSymbol)) {
                programState = programState.addConstraint(sv, ObjectConstraint.NOT_NULL);
            }
        } else if (variableTree.parent().is(Tree.Kind.CATCH)) {
            sv = handleCatchVariable(variableSymbol.type());
            // an exception have been thrown and caught, stack must be cleared
            // see notes in JVMS 8 - ยง6.5. - instruction "athrow"
            programState = programState.clearStack();
            // exception variable is not null by definition
            programState = programState.addConstraint(sv, ObjectConstraint.NOT_NULL);
        }
        if (sv != null) {
            programState = programState.put(variableSymbol, sv);
        }
    } else {
        ProgramState.Pop unstack = programState.unstackValue(1);
        programState = unstack.state;
        programState = programState.put(variableSymbol, unstack.values.get(0));
    }
}
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 18 with SymbolicValue

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

the class ExplodedGraphWalker method executeMemberSelect.

private void executeMemberSelect(MemberSelectExpressionTree mse) {
    if (!"class".equals(mse.identifier().name())) {
        ProgramState.Pop unstackMSE = programState.unstackValue(1);
        programState = unstackMSE.state;
    }
    if (ExpressionUtils.isSelectOnThisOrSuper(mse)) {
        executeIdentifier(mse.identifier());
    } else {
        SymbolicValue mseValue = constraintManager.createSymbolicValue(mse);
        programState = programState.stackValue(mseValue);
    }
}
Also used : SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)

Example 19 with SymbolicValue

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

the class ExplodedGraphWalker method handleCatchVariable.

private SymbolicValue handleCatchVariable(Type caughtType) {
    SymbolicValue peekValue = programState.peekValue();
    SymbolicValue.ExceptionalSymbolicValue sv = null;
    Type exceptionType = null;
    // FIXME SONARJAVA-2069 every path conducting to a catch block should have an exceptional symbolic value on top of the stack
    if (peekValue instanceof SymbolicValue.ExceptionalSymbolicValue) {
        sv = (SymbolicValue.ExceptionalSymbolicValue) peekValue;
        exceptionType = sv.exceptionType();
    }
    if (exceptionType == null || exceptionType.isUnknown()) {
        // unknown exception, create an exception of the adequate type
        sv = constraintManager.createExceptionalSymbolicValue(caughtType);
    }
    // use a dedicated SV encapsulating the caught exception
    return constraintManager.createCaughtExceptionSymbolicValue(sv);
}
Also used : JavaType(org.sonar.java.resolve.JavaType) Type(org.sonar.plugins.java.api.semantic.Type) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)

Example 20 with SymbolicValue

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

the class FlowComputation method flow.

private static Set<Flow> flow(ExplodedGraph.Node currentNode, Set<SymbolicValue> symbolicValues, Predicate<Constraint> addToFlow, Predicate<Constraint> terminateTraversal, List<Class<? extends Constraint>> domains, Set<Symbol> symbols, boolean skipExceptionMessages) {
    Set<SymbolicValue> allSymbolicValues = symbolicValues.stream().map(FlowComputation::computedFrom).flatMap(Set::stream).collect(Collectors.toSet());
    PSet<Symbol> trackedSymbols = PCollections.emptySet();
    for (Symbol symbol : symbols) {
        trackedSymbols = trackedSymbols.add(symbol);
    }
    if (symbols.isEmpty()) {
        for (SymbolicValue symbolicValue : symbolicValues) {
            for (Symbol symbol : symbolicValue.computedFromSymbols()) {
                trackedSymbols = trackedSymbols.add(symbol);
            }
        }
    }
    FlowComputation flowComputation = new FlowComputation(allSymbolicValues, addToFlow, terminateTraversal, domains, skipExceptionMessages);
    return flowComputation.run(currentNode, trackedSymbols);
}
Also used : JavaSymbol(org.sonar.java.resolve.JavaSymbol) Symbol(org.sonar.plugins.java.api.semantic.Symbol) BinarySymbolicValue(org.sonar.java.se.symbolicvalues.BinarySymbolicValue) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue)

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