Search in sources :

Example 11 with Constraint

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

the class BytecodeEGWalkerTest method unchecked_exceptions_should_be_enqueued.

@Test
public void unchecked_exceptions_should_be_enqueued() {
    MethodBehavior mb = getMethodBehavior(ExceptionEnqueue.class, "test(Lorg/sonar/java/bytecode/se/testdata/ExceptionEnqueue;)Ljava/lang/Object;");
    List<Constraint> resultConstraint = mb.happyPathYields().map(y -> y.resultConstraint().get(ObjectConstraint.class)).collect(Collectors.toList());
    assertThat(resultConstraint).contains(ObjectConstraint.NOT_NULL, ObjectConstraint.NULL);
    List<String> exceptions = mb.exceptionalPathYields().map(y -> y.exceptionType(semanticModel).fullyQualifiedName()).collect(Collectors.toList());
    assertThat(exceptions).contains("org.sonar.java.bytecode.se.testdata.ExceptionEnqueue$ExceptionCatch", "org.sonar.java.bytecode.se.testdata.ExceptionEnqueue$ThrowableCatch", "org.sonar.java.bytecode.se.testdata.ExceptionEnqueue$ErrorCatch");
}
Also used : BytecodeTestClass(org.sonar.java.bytecode.se.testdata.BytecodeTestClass) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) Iterables(com.google.common.collect.Iterables) BeforeClass(org.junit.BeforeClass) ProgramState(org.sonar.java.se.ProgramState) MethodYield(org.sonar.java.se.xproc.MethodYield) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ExceptionEnqueue(org.sonar.java.bytecode.se.testdata.ExceptionEnqueue) DivisionByZeroCheck(org.sonar.java.se.checks.DivisionByZeroCheck) Lists(com.google.common.collect.Lists) Assertions.assertThat(org.fest.assertions.Assertions.assertThat) FinalBytecodeTestClass(org.sonar.java.bytecode.se.testdata.FinalBytecodeTestClass) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) BooleanConstraint(org.sonar.java.se.constraint.BooleanConstraint) JavaParser(org.sonar.java.ast.parser.JavaParser) Collection(java.util.Collection) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader) MaxRelationBytecode(org.sonar.java.bytecode.se.testdata.MaxRelationBytecode) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) Type(org.sonar.plugins.java.api.semantic.Type) Collectors(java.util.stream.Collectors) File(java.io.File) List(java.util.List) Rule(org.junit.Rule) LogTester(org.sonar.api.utils.log.LogTester) SemanticModel(org.sonar.java.resolve.SemanticModel) ByteStreams(com.google.common.io.ByteStreams) BehaviorCache(org.sonar.java.se.xproc.BehaviorCache) Constraint(org.sonar.java.se.constraint.Constraint) ExceptionalYield(org.sonar.java.se.xproc.ExceptionalYield) HappyPathYield(org.sonar.java.se.xproc.HappyPathYield) LoggerLevel(org.sonar.api.utils.log.LoggerLevel) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) BooleanConstraint(org.sonar.java.se.constraint.BooleanConstraint) Constraint(org.sonar.java.se.constraint.Constraint) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) Test(org.junit.Test)

Example 12 with Constraint

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

the class ProgramState method addConstraintTransitively.

public ProgramState addConstraintTransitively(SymbolicValue symbolicValue, Constraint constraint) {
    List<SymbolicValue> transitiveSymbolicValues = knownRelations().stream().filter(rsv -> rsv.isEquality() && (rsv.getLeftOp() == symbolicValue || rsv.getRightOp() == symbolicValue)).map(rsv -> rsv.getLeftOp() == symbolicValue ? rsv.getRightOp() : rsv.getLeftOp()).collect(Collectors.toList());
    ProgramState ps = addConstraint(symbolicValue, constraint);
    for (SymbolicValue sv : transitiveSymbolicValues) {
        ps = ps.addConstraint(sv, constraint);
    }
    return ps;
}
Also used : ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) CustomUnclosedResourcesCheck(org.sonar.java.se.checks.CustomUnclosedResourcesCheck) ConstraintsByDomain(org.sonar.java.se.constraint.ConstraintsByDomain) ArrayList(java.util.ArrayList) ConstraintManager(org.sonar.java.se.constraint.ConstraintManager) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) BiConsumer(java.util.function.BiConsumer) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) BooleanConstraint(org.sonar.java.se.constraint.BooleanConstraint) Nullable(javax.annotation.Nullable) UnclosedResourcesCheck(org.sonar.java.se.checks.UnclosedResourcesCheck) ImmutableSet(com.google.common.collect.ImmutableSet) PMap(org.sonar.java.collections.PMap) Collection(java.util.Collection) PStack(org.sonar.java.collections.PStack) Set(java.util.Set) Type(org.sonar.plugins.java.api.semantic.Type) Collectors(java.util.stream.Collectors) BinarySymbolicValue(org.sonar.java.se.symbolicvalues.BinarySymbolicValue) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue) Objects(java.util.Objects) PCollections(org.sonar.java.collections.PCollections) List(java.util.List) StreamConsumedCheck(org.sonar.java.se.checks.StreamConsumedCheck) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Constraint(org.sonar.java.se.constraint.Constraint) Collections(java.util.Collections) CheckForNull(javax.annotation.CheckForNull) LocksNotUnlockedCheck(org.sonar.java.se.checks.LocksNotUnlockedCheck) Symbol(org.sonar.plugins.java.api.semantic.Symbol) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) BinarySymbolicValue(org.sonar.java.se.symbolicvalues.BinarySymbolicValue) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)

Example 13 with Constraint

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

the class ExceptionalYieldChecker method flowsForMethodArguments.

private static Set<Flow> flowsForMethodArguments(ExplodedGraph.Node node, MethodInvocationTree mit, int parameterCausingExceptionIndex) {
    ProgramState programState = node.programState;
    List<ProgramState.SymbolicValueSymbol> arguments = Lists.reverse(programState.peekValuesAndSymbols(mit.arguments().size()));
    SymbolicValue parameterCausingExceptionSV = arguments.get(parameterCausingExceptionIndex).symbolicValue();
    Set<SymbolicValue> argSymbolicValues = new LinkedHashSet<>();
    Set<Symbol> argSymbols = new LinkedHashSet<>();
    arguments.stream().filter(svs -> parameterCausingExceptionSV == svs.symbolicValue() || hasConstraintOtherThanNonNull(svs, programState)).forEach(svs -> {
        argSymbolicValues.add(svs.symbolicValue());
        Symbol symbol = svs.symbol();
        if (symbol != null) {
            argSymbols.add(symbol);
        }
    });
    List<Class<? extends Constraint>> domains = domainsFromArguments(programState, argSymbolicValues);
    return FlowComputation.flow(node, argSymbolicValues, c -> true, c -> false, domains, argSymbols);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) ProgramState(org.sonar.java.se.ProgramState) ConstraintsByDomain(org.sonar.java.se.constraint.ConstraintsByDomain) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) Lists(com.google.common.collect.Lists) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) LinkedHashSet(java.util.LinkedHashSet) ExceptionalCheckBasedYield(org.sonar.java.se.xproc.ExceptionalCheckBasedYield) ImmutableSet(com.google.common.collect.ImmutableSet) Collection(java.util.Collection) Set(java.util.Set) ExplodedGraph(org.sonar.java.se.ExplodedGraph) Tree(org.sonar.plugins.java.api.tree.Tree) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) Collectors(java.util.stream.Collectors) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) Objects(java.util.Objects) List(java.util.List) Flow(org.sonar.java.se.Flow) FlowComputation(org.sonar.java.se.FlowComputation) Constraint(org.sonar.java.se.constraint.Constraint) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Collections(java.util.Collections) Symbol(org.sonar.plugins.java.api.semantic.Symbol) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) Constraint(org.sonar.java.se.constraint.Constraint) Symbol(org.sonar.plugins.java.api.semantic.Symbol) ProgramState(org.sonar.java.se.ProgramState) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue)

Example 14 with Constraint

use of org.sonar.java.se.constraint.Constraint 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 15 with Constraint

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

the class ExceptionalCheckBasedYield method applicableOnVarArgs.

private boolean applicableOnVarArgs(List<Type> invocationTypes) {
    int numberParametersYield = parametersConstraints.size();
    int numberArgumentsInCall = invocationTypes.size();
    if (numberParametersYield > numberArgumentsInCall) {
        // VarArgs method called without variadic parameter
        return true;
    }
    ConstraintsByDomain lastParamConstraint = parametersConstraints.get(numberParametersYield - 1);
    if (lastParamConstraint.isEmpty()) {
        // no constraint on the last parameter on yield side
        return true;
    }
    if (numberParametersYield != numberArgumentsInCall) {
        // there is a constraint on last parameter, but varArgs method called with multiple arguments in variadic part
        return false;
    }
    // compatible number of parameters, type must be compatible on arguments side
    Type lastArgumentType = invocationTypes.get(numberArgumentsInCall - 1);
    return !isMethodVarargs || (lastArgumentType.isArray() || lastArgumentType.is("<nulltype>"));
}
Also used : ConstraintsByDomain(org.sonar.java.se.constraint.ConstraintsByDomain) Type(org.sonar.plugins.java.api.semantic.Type) Constraint(org.sonar.java.se.constraint.Constraint)

Aggregations

Constraint (org.sonar.java.se.constraint.Constraint)20 ObjectConstraint (org.sonar.java.se.constraint.ObjectConstraint)19 BooleanConstraint (org.sonar.java.se.constraint.BooleanConstraint)18 SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)12 ProgramState (org.sonar.java.se.ProgramState)11 Test (org.junit.Test)10 ConstraintsByDomain (org.sonar.java.se.constraint.ConstraintsByDomain)9 RelationalSymbolicValue (org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)9 List (java.util.List)8 Collectors (java.util.stream.Collectors)8 ProgramPoint (org.sonar.java.se.ProgramPoint)8 TypedConstraint (org.sonar.java.se.constraint.TypedConstraint)8 BinarySymbolicValue (org.sonar.java.se.symbolicvalues.BinarySymbolicValue)8 SemanticModel (org.sonar.java.resolve.SemanticModel)6 Instruction (org.sonar.java.bytecode.cfg.Instruction)5 Lists (com.google.common.collect.Lists)4 ArrayList (java.util.ArrayList)4 Collection (java.util.Collection)4 Nullable (javax.annotation.Nullable)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4