Search in sources :

Example 21 with SymbolicValue

use of org.sonar.java.se.symbolicvalues.SymbolicValue 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 22 with SymbolicValue

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

the class ProgramState method getValuesWithConstraints.

public List<SymbolicValue> getValuesWithConstraints(final Constraint constraint) {
    final List<SymbolicValue> result = new ArrayList<>();
    constraints.forEach((symbolicValue, constraintByDomain) -> {
        Constraint find = constraintByDomain.get(constraint.getClass());
        if (constraint.equals(find)) {
            result.add(symbolicValue);
        }
    });
    return result;
}
Also used : ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) BooleanConstraint(org.sonar.java.se.constraint.BooleanConstraint) Constraint(org.sonar.java.se.constraint.Constraint) ArrayList(java.util.ArrayList) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) BinarySymbolicValue(org.sonar.java.se.symbolicvalues.BinarySymbolicValue) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)

Example 23 with SymbolicValue

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

the class ConstraintManager method createBinarySymbolicValue.

public SymbolicValue createBinarySymbolicValue(Instruction inst, List<ProgramState.SymbolicValueSymbol> computedFrom) {
    SymbolicValue result;
    switch(inst.opcode) {
        case IAND:
        case LAND:
            result = new SymbolicValue.AndSymbolicValue();
            result.computedFrom(computedFrom);
            break;
        case IOR:
        case LOR:
            result = new SymbolicValue.OrSymbolicValue();
            result.computedFrom(computedFrom);
            break;
        case IXOR:
        case LXOR:
            result = new SymbolicValue.XorSymbolicValue();
            result.computedFrom(computedFrom);
            break;
        case IF_ICMPEQ:
        case IF_ACMPEQ:
        case IFEQ:
        case IFNULL:
            result = createRelationalSymbolicValue(Kind.EQUAL, computedFrom);
            break;
        case IFNE:
        case IFNONNULL:
        case IF_ICMPNE:
        case IF_ACMPNE:
            result = createRelationalSymbolicValue(Kind.NOT_EQUAL, computedFrom);
            break;
        case IF_ICMPLT:
        case IFLT:
            result = createRelationalSymbolicValue(Kind.LESS_THAN, computedFrom);
            break;
        case IF_ICMPGE:
        case IFGE:
            result = createRelationalSymbolicValue(Kind.GREATER_THAN_OR_EQUAL, computedFrom);
            break;
        case IF_ICMPGT:
        case IFGT:
            result = createRelationalSymbolicValue(Kind.LESS_THAN, Lists.reverse(computedFrom));
            break;
        case IF_ICMPLE:
        case IFLE:
            result = createRelationalSymbolicValue(Kind.GREATER_THAN_OR_EQUAL, Lists.reverse(computedFrom));
            break;
        default:
            throw new IllegalStateException("Unexpected kind for binary SV");
    }
    return result;
}
Also used : RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue)

Example 24 with SymbolicValue

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

the class ConstraintManager method createMethodSymbolicValue.

public SymbolicValue createMethodSymbolicValue(MethodInvocationTree syntaxNode, List<ProgramState.SymbolicValueSymbol> values) {
    SymbolicValue result;
    if (ExplodedGraphWalker.EQUALS_METHODS.anyMatch(syntaxNode)) {
        result = new RelationalSymbolicValue(RelationalSymbolicValue.Kind.METHOD_EQUALS);
        ProgramState.SymbolicValueSymbol leftOp = values.get(1);
        ProgramState.SymbolicValueSymbol rightOp = values.get(0);
        result.computedFrom(ImmutableList.of(rightOp, leftOp));
    } else {
        result = createDefaultSymbolicValue();
    }
    return result;
}
Also used : ProgramState(org.sonar.java.se.ProgramState) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue)

Example 25 with SymbolicValue

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

the class ConstraintManager method createDefaultSymbolicValue.

public SymbolicValue createDefaultSymbolicValue() {
    SymbolicValue result;
    result = symbolicValueFactory == null ? new SymbolicValue() : symbolicValueFactory.createSymbolicValue();
    symbolicValueFactory = null;
    return result;
}
Also used : RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue) 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