Search in sources :

Example 56 with ProgramState

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

the class ParameterNullnessCheck method checkPreStatement.

@Override
public ProgramState checkPreStatement(CheckerContext context, Tree syntaxNode) {
    ProgramState state = context.getState();
    if (syntaxNode.is(Tree.Kind.METHOD_INVOCATION)) {
        MethodInvocationTree mit = (MethodInvocationTree) syntaxNode;
        checkParameters(mit, mit.symbol(), mit.arguments(), state);
    } else if (syntaxNode.is(Tree.Kind.NEW_CLASS)) {
        NewClassTree nct = (NewClassTree) syntaxNode;
        checkParameters(nct, nct.constructorSymbol(), nct.arguments(), state);
    }
    return state;
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) ProgramState(org.sonar.java.se.ProgramState)

Example 57 with ProgramState

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

the class RelationalSymbolicValue method addNullConstraintsForBooleanWrapper.

private List<ProgramState> addNullConstraintsForBooleanWrapper(ProgramState initialProgramState, List<ProgramState> copiedConstraints) {
    BooleanConstraint leftConstraint = initialProgramState.getConstraint(leftOp, BooleanConstraint.class);
    BooleanConstraint rightConstraint = initialProgramState.getConstraint(rightOp, BooleanConstraint.class);
    if (leftConstraint != null && rightConstraint == null && !isEquality()) {
        List<ProgramState> nullConstraints = copiedConstraints.stream().flatMap(ps -> rightOp.setConstraint(ps, ObjectConstraint.NULL).stream()).map(ps -> ps.removeConstraintsOnDomain(rightOp, BooleanConstraint.class)).collect(Collectors.toList());
        return ImmutableList.<ProgramState>builder().addAll(copiedConstraints).addAll(nullConstraints).build();
    }
    return copiedConstraints;
}
Also used : ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) LESS_THAN(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue.Kind.LESS_THAN) ProgramState(org.sonar.java.se.ProgramState) Set(java.util.Set) Deque(java.util.Deque) ConstraintsByDomain(org.sonar.java.se.constraint.ConstraintsByDomain) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) GREATER_THAN_OR_EQUAL(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue.Kind.GREATER_THAN_OR_EQUAL) HashSet(java.util.HashSet) METHOD_EQUALS(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue.Kind.METHOD_EQUALS) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) BooleanConstraint(org.sonar.java.se.constraint.BooleanConstraint) Constraint(org.sonar.java.se.constraint.Constraint) ArrayDeque(java.util.ArrayDeque) CheckForNull(javax.annotation.CheckForNull) Collections(java.util.Collections) Symbol(org.sonar.plugins.java.api.semantic.Symbol) Nullable(javax.annotation.Nullable) EQUAL(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue.Kind.EQUAL) BooleanConstraint(org.sonar.java.se.constraint.BooleanConstraint) ProgramState(org.sonar.java.se.ProgramState)

Example 58 with ProgramState

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

the class RelationalSymbolicValue method copyConstraintFromTo.

private List<ProgramState> copyConstraintFromTo(SymbolicValue from, SymbolicValue to, ProgramState programState, Set<RelationalSymbolicValue> knownRelations) {
    List<ProgramState> states = new ArrayList<>();
    states.add(programState);
    ConstraintsByDomain leftConstraints = programState.getConstraints(from);
    if (leftConstraints == null) {
        return states;
    }
    leftConstraints.forEach((d, c) -> {
        Constraint constraint = c.copyOver(kind);
        if (constraint != null) {
            List<ProgramState> newStates = applyConstraint(constraint, to, states, knownRelations);
            states.clear();
            states.addAll(newStates);
        }
    });
    return states;
}
Also used : ConstraintsByDomain(org.sonar.java.se.constraint.ConstraintsByDomain) 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) ProgramState(org.sonar.java.se.ProgramState)

Example 59 with ProgramState

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

the class RelationalSymbolicValue method getNewProgramStates.

private static List<ProgramState> getNewProgramStates(ProgramState initialProgramState, Set<RelationalSymbolicValue> newRelations, Set<RelationalSymbolicValue> knownRelations) {
    List<ProgramState> programStates = new ArrayList<>();
    programStates.add(initialProgramState);
    for (RelationalSymbolicValue relationalSymbolicValue : newRelations) {
        List<ProgramState> intermediateStates = new ArrayList<>();
        for (ProgramState programState : programStates) {
            intermediateStates.addAll(relationalSymbolicValue.copyAllConstraints(programState, knownRelations));
        }
        programStates = intermediateStates;
    }
    return programStates;
}
Also used : ArrayList(java.util.ArrayList) ProgramState(org.sonar.java.se.ProgramState)

Example 60 with ProgramState

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

the class HappyPathYield method statesAfterInvocation.

@Override
public Stream<ProgramState> statesAfterInvocation(List<SymbolicValue> invocationArguments, List<Type> invocationTypes, ProgramState programState, Supplier<SymbolicValue> svSupplier) {
    Stream<ProgramState> results = parametersAfterInvocation(invocationArguments, invocationTypes, programState);
    // applied all constraints from parameters, stack return value
    SymbolicValue sv;
    if (resultIndex < 0 || resultIndex == invocationArguments.size()) {
        // if returnIndex is the size of invocationArguments : returning vararg parameter on a call with no elements specified
        sv = svSupplier.get();
    } else {
        // returned SV is the same as one of the arguments.
        sv = invocationArguments.get(resultIndex);
    }
    // sv can be null if method is void
    if (sv != null) {
        results = results.map(s -> s.stackValue(sv));
        if (resultConstraint != null) {
            results = results.map(s -> s.addConstraints(sv, resultConstraint));
        }
    }
    return results.distinct();
}
Also used : ProgramState(org.sonar.java.se.ProgramState) HashCodeBuilder(org.apache.commons.lang.builder.HashCodeBuilder) ExplodedGraph(org.sonar.java.se.ExplodedGraph) Type(org.sonar.plugins.java.api.semantic.Type) ConstraintsByDomain(org.sonar.java.se.constraint.ConstraintsByDomain) Supplier(java.util.function.Supplier) Collectors(java.util.stream.Collectors) List(java.util.List) Stream(java.util.stream.Stream) EqualsBuilder(org.apache.commons.lang.builder.EqualsBuilder) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Constraint(org.sonar.java.se.constraint.Constraint) CheckForNull(javax.annotation.CheckForNull) Nullable(javax.annotation.Nullable) ProgramState(org.sonar.java.se.ProgramState) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue)

Aggregations

ProgramState (org.sonar.java.se.ProgramState)105 SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)72 Test (org.junit.Test)71 Instruction (org.sonar.java.bytecode.cfg.Instruction)61 RelationalSymbolicValue (org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)52 BinarySymbolicValue (org.sonar.java.se.symbolicvalues.BinarySymbolicValue)45 BooleanConstraint (org.sonar.java.se.constraint.BooleanConstraint)33 ObjectConstraint (org.sonar.java.se.constraint.ObjectConstraint)32 ProgramPoint (org.sonar.java.se.ProgramPoint)30 Constraint (org.sonar.java.se.constraint.Constraint)30 TypedConstraint (org.sonar.java.se.constraint.TypedConstraint)25 ConstraintsByDomain (org.sonar.java.se.constraint.ConstraintsByDomain)11 List (java.util.List)9 Collectors (java.util.stream.Collectors)8 Type (org.sonar.plugins.java.api.semantic.Type)8 VisibleForTesting (com.google.common.annotations.VisibleForTesting)7 ExplodedGraph (org.sonar.java.se.ExplodedGraph)7 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)7 Lists (com.google.common.collect.Lists)6 ArrayList (java.util.ArrayList)6