Search in sources :

Example 1 with ConstraintsByDomain

use of org.sonar.java.se.constraint.ConstraintsByDomain in project sonar-java by SonarSource.

the class ProgramState method cleanupConstraints.

public ProgramState cleanupConstraints(Collection<SymbolicValue> protectedSymbolicValues) {
    class CleanAction implements BiConsumer<SymbolicValue, ConstraintsByDomain> {

        boolean newProgramState = false;

        PMap<SymbolicValue, ConstraintsByDomain> newConstraints = constraints;

        PMap<SymbolicValue, Integer> newReferences = references;

        @Override
        public void accept(SymbolicValue symbolicValue, ConstraintsByDomain constraintPMap) {
            constraintPMap.forEach((domain, constraint) -> {
                if (!protectedSymbolicValues.contains(symbolicValue) && !isReachable(symbolicValue, newReferences) && isDisposable(symbolicValue, constraint) && !inStack(stack, symbolicValue)) {
                    newProgramState = true;
                    ConstraintsByDomain removed = newConstraints.get(symbolicValue).remove(domain);
                    if (removed.isEmpty()) {
                        newConstraints = newConstraints.remove(symbolicValue);
                    } else {
                        newConstraints = newConstraints.put(symbolicValue, removed);
                    }
                    newReferences = newReferences.remove(symbolicValue);
                }
            });
        }
    }
    CleanAction cleanAction = new CleanAction();
    constraints.forEach(cleanAction);
    return cleanAction.newProgramState ? new ProgramState(values, cleanAction.newReferences, cleanAction.newConstraints, visitedPoints, stack, exitSymbolicValue) : this;
}
Also used : ConstraintsByDomain(org.sonar.java.se.constraint.ConstraintsByDomain) PMap(org.sonar.java.collections.PMap) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) BinarySymbolicValue(org.sonar.java.se.symbolicvalues.BinarySymbolicValue) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue) BiConsumer(java.util.function.BiConsumer)

Example 2 with ConstraintsByDomain

use of org.sonar.java.se.constraint.ConstraintsByDomain in project sonar-java by SonarSource.

the class ProgramState method removeConstraintsOnDomain.

public ProgramState removeConstraintsOnDomain(SymbolicValue sv, Class<? extends Constraint> domain) {
    ConstraintsByDomain svConstraint = constraints.get(sv);
    if (svConstraint == null) {
        return this;
    }
    ConstraintsByDomain newConstraintForSv = svConstraint.remove(domain);
    if (newConstraintForSv.isEmpty()) {
        return new ProgramState(this, constraints.remove(sv));
    }
    return addConstraints(sv, newConstraintForSv);
}
Also used : ConstraintsByDomain(org.sonar.java.se.constraint.ConstraintsByDomain)

Example 3 with ConstraintsByDomain

use of org.sonar.java.se.constraint.ConstraintsByDomain in project sonar-java by SonarSource.

the class ExceptionalYieldChecker method hasConstraintOtherThanNonNull.

private static boolean hasConstraintOtherThanNonNull(ProgramState.SymbolicValueSymbol svs, ProgramState ps) {
    SymbolicValue sv = svs.symbolicValue();
    ConstraintsByDomain constraints = ps.getConstraints(sv);
    return constraints != null && !hasOnlyNonNullConstraint(constraints);
}
Also used : ConstraintsByDomain(org.sonar.java.se.constraint.ConstraintsByDomain) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue)

Example 4 with ConstraintsByDomain

use of org.sonar.java.se.constraint.ConstraintsByDomain in project sonar-java by SonarSource.

the class MethodBehavior method addParameterConstraints.

private void addParameterConstraints(ExplodedGraph.Node node, MethodYield yield) {
    // add the constraints on all the parameters
    int index = 0;
    for (SymbolicValue parameter : parameters) {
        ConstraintsByDomain constraints = node.programState.getConstraints(parameter);
        if (constraints == null) {
            constraints = ConstraintsByDomain.empty();
        } else {
            // cleanup based on signature
            org.objectweb.asm.Type[] argumentTypes = org.objectweb.asm.Type.getArgumentTypes(signature.substring(signature.indexOf('(')));
            constraints = cleanup(constraints, argumentTypes[index]);
        }
        yield.parametersConstraints.add(constraints);
        index++;
    }
}
Also used : ConstraintsByDomain(org.sonar.java.se.constraint.ConstraintsByDomain) Type(org.sonar.plugins.java.api.semantic.Type) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) BooleanConstraint(org.sonar.java.se.constraint.BooleanConstraint)

Example 5 with ConstraintsByDomain

use of org.sonar.java.se.constraint.ConstraintsByDomain in project sonar-java by SonarSource.

the class MethodBehavior method getOnlyConstraintDifferenceIndex.

private static Optional<Integer> getOnlyConstraintDifferenceIndex(HappyPathYield yield1, HappyPathYield yield2) {
    List<ConstraintsByDomain> constraints1 = new ArrayList<>(yield1.parametersConstraints);
    constraints1.add(yield1.resultConstraint());
    List<ConstraintsByDomain> constraints2 = new ArrayList<>(yield2.parametersConstraints);
    constraints2.add(yield2.resultConstraint());
    List<Integer> diff = new ArrayList<>();
    for (int i = 0; i < constraints1.size(); i++) {
        if (!Objects.equals(constraints1.get(i), constraints2.get(i))) {
            diff.add(i);
        }
    }
    if (diff.size() != 1) {
        return Optional.empty();
    }
    return Optional.of(diff.get(0));
}
Also used : ConstraintsByDomain(org.sonar.java.se.constraint.ConstraintsByDomain) ArrayList(java.util.ArrayList) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) BooleanConstraint(org.sonar.java.se.constraint.BooleanConstraint)

Aggregations

ConstraintsByDomain (org.sonar.java.se.constraint.ConstraintsByDomain)20 SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)12 BooleanConstraint (org.sonar.java.se.constraint.BooleanConstraint)9 ProgramState (org.sonar.java.se.ProgramState)8 Test (org.junit.Test)7 ProgramPoint (org.sonar.java.se.ProgramPoint)6 Instruction (org.sonar.java.bytecode.cfg.Instruction)5 ZeroConstraint (org.sonar.java.se.checks.DivisionByZeroCheck.ZeroConstraint)5 Constraint (org.sonar.java.se.constraint.Constraint)5 ObjectConstraint (org.sonar.java.se.constraint.ObjectConstraint)4 RelationalSymbolicValue (org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)3 Type (org.sonar.plugins.java.api.semantic.Type)3 ArrayList (java.util.ArrayList)2 TypedConstraint (org.sonar.java.se.constraint.TypedConstraint)2 BinarySymbolicValue (org.sonar.java.se.symbolicvalues.BinarySymbolicValue)2 LinkedHashSet (java.util.LinkedHashSet)1 BiConsumer (java.util.function.BiConsumer)1 PMap (org.sonar.java.collections.PMap)1 ExplodedGraph (org.sonar.java.se.ExplodedGraph)1 SETestUtils.getMethodBehavior (org.sonar.java.se.SETestUtils.getMethodBehavior)1