Search in sources :

Example 81 with SymbolicValue

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

the class NonNullSetToNullCheck method isUndefinedOrNull.

private static boolean isUndefinedOrNull(CheckerContext context, Symbol symbol) {
    ProgramState programState = context.getState();
    SymbolicValue value = programState.getValue(symbol);
    return value == null;
}
Also used : ProgramState(org.sonar.java.se.ProgramState) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue)

Example 82 with SymbolicValue

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

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

the class NullDereferenceCheck method reportIssue.

private void reportIssue(SymbolicValue currentVal, Tree syntaxNode, ExplodedGraph.Node node) {
    String message = "A \"NullPointerException\" could be thrown; ";
    if (syntaxNode.is(Tree.Kind.MEMBER_SELECT) && ((MemberSelectExpressionTree) syntaxNode).expression().is(Tree.Kind.METHOD_INVOCATION)) {
        message += "\"" + SyntaxTreeNameFinder.getName(syntaxNode) + "()\" can return null.";
    } else {
        message += "\"" + SyntaxTreeNameFinder.getName(syntaxNode) + "\" is nullable here.";
    }
    SymbolicValue val = null;
    if (!SymbolicValue.NULL_LITERAL.equals(currentVal)) {
        val = currentVal;
    }
    Symbol dereferencedSymbol = dereferencedSymbol(syntaxNode);
    Set<Flow> flows = FlowComputation.flow(node, val, Lists.newArrayList(ObjectConstraint.class), dereferencedSymbol).stream().filter(f -> !f.isEmpty()).map(f -> addDereferenceMessage(f, syntaxNode)).collect(Collectors.toSet());
    reportIssue(syntaxNode, message, flows);
}
Also used : ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) ProgramState(org.sonar.java.se.ProgramState) Deque(java.util.Deque) CheckerContext(org.sonar.java.se.CheckerContext) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ConstraintManager(org.sonar.java.se.constraint.ConstraintManager) Lists(com.google.common.collect.Lists) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) Nullable(javax.annotation.Nullable) Set(java.util.Set) ExplodedGraph(org.sonar.java.se.ExplodedGraph) Tree(org.sonar.plugins.java.api.tree.Tree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) Collectors(java.util.stream.Collectors) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) List(java.util.List) CFG(org.sonar.java.cfg.CFG) Flow(org.sonar.java.se.Flow) FlowComputation(org.sonar.java.se.FlowComputation) Preconditions(com.google.common.base.Preconditions) Rule(org.sonar.check.Rule) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) ArrayDeque(java.util.ArrayDeque) Symbol(org.sonar.plugins.java.api.semantic.Symbol) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) Flow(org.sonar.java.se.Flow)

Example 84 with SymbolicValue

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

the class NullDereferenceCheck method setNullConstraint.

private static List<ProgramState> setNullConstraint(CheckerContext context, Tree syntaxNode) {
    SymbolicValue val = context.getState().peekValue();
    if (syntaxNode.is(Tree.Kind.METHOD_INVOCATION) && isAnnotatedCheckForNull((MethodInvocationTree) syntaxNode)) {
        Preconditions.checkNotNull(val);
        List<ProgramState> states = new ArrayList<>();
        states.addAll(val.setConstraint(context.getState(), ObjectConstraint.NULL));
        states.addAll(val.setConstraint(context.getState(), ObjectConstraint.NOT_NULL));
        return states;
    }
    return Lists.newArrayList(context.getState());
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ArrayList(java.util.ArrayList) ProgramState(org.sonar.java.se.ProgramState) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue)

Example 85 with SymbolicValue

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

the class NullDereferenceCheck method checkPreStatement.

@Override
public ProgramState checkPreStatement(CheckerContext context, Tree syntaxNode) {
    if (context.getState().peekValue() == null) {
        // stack is empty, nothing to do.
        return context.getState();
    }
    if (syntaxNode.is(Tree.Kind.METHOD_INVOCATION)) {
        MethodInvocationTree methodInvocation = (MethodInvocationTree) syntaxNode;
        Tree methodSelect = methodInvocation.methodSelect();
        if (methodSelect.is(Tree.Kind.MEMBER_SELECT)) {
            SymbolicValue dereferencedSV = context.getState().peekValue(methodInvocation.arguments().size());
            return checkConstraint(context, methodSelect, dereferencedSV);
        }
    }
    if (syntaxNode.is(Tree.Kind.ARRAY_ACCESS_EXPRESSION)) {
        Tree toCheck = ((ArrayAccessExpressionTree) syntaxNode).expression();
        SymbolicValue currentVal = context.getState().peekValue(1);
        return checkConstraint(context, toCheck, currentVal);
    }
    if (syntaxNode.is(Tree.Kind.MEMBER_SELECT)) {
        return checkMemberSelect(context, (MemberSelectExpressionTree) syntaxNode, context.getState().peekValue());
    }
    return context.getState();
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) Tree(org.sonar.plugins.java.api.tree.Tree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) 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