use of org.sonar.java.se.ProgramState in project sonar-java by SonarSource.
the class NullDereferenceCheck method checkPostStatement.
@Override
public ProgramState checkPostStatement(CheckerContext context, Tree syntaxNode) {
if (syntaxNode.is(Tree.Kind.SWITCH_STATEMENT, Tree.Kind.THROW_STATEMENT) && context.getConstraintManager().isNull(context.getState(), context.getState().peekValue())) {
NullDereferenceIssue issue = new NullDereferenceIssue(context.getNode(), context.getState().peekValue(), syntaxNode);
detectedIssues.peek().add(issue);
context.createSink();
return context.getState();
}
List<ProgramState> programStates = setNullConstraint(context, syntaxNode);
for (ProgramState programState : programStates) {
context.addTransition(programState);
}
return context.getState();
}
use of org.sonar.java.se.ProgramState in project sonar-java by SonarSource.
the class RelationalSymbolicValue method copyAllConstraints.
private List<ProgramState> copyAllConstraints(ProgramState initialState, Set<RelationalSymbolicValue> knownRelations) {
ProgramState programState = initialState;
if (programState.canReach(leftOp) || programState.canReach(rightOp)) {
programState = programState.addConstraint(this, BooleanConstraint.TRUE);
}
List<ProgramState> results = new ArrayList<>();
List<ProgramState> copiedConstraints = copyConstraintFromTo(leftOp, rightOp, programState, knownRelations);
if (Kind.METHOD_EQUALS == kind || Kind.NOT_METHOD_EQUALS == kind) {
copiedConstraints = addNullConstraintsForBooleanWrapper(programState, copiedConstraints);
}
for (ProgramState ps : copiedConstraints) {
List<ProgramState> copiedConstraintsRightToLeft = copyConstraintFromTo(rightOp, leftOp, ps, knownRelations);
if (copiedConstraintsRightToLeft.size() == 1 && copiedConstraintsRightToLeft.get(0).equals(programState)) {
results.add(programState.addConstraint(this, BooleanConstraint.TRUE));
} else {
results.addAll(copiedConstraintsRightToLeft);
}
}
return results;
}
use of org.sonar.java.se.ProgramState in project sonar-java by SonarSource.
the class StreamConsumedCheck method handleMethodInvocation.
private ProgramState handleMethodInvocation(CheckerContext context, MethodInvocationTree mit) {
ProgramState programState = context.getState();
programState = removeConstraintOnArgs(programState, mit.arguments().size());
SymbolicValue invocationTarget = invocationTarget(programState, mit);
if ((isIntermediateOperation(mit) || isTerminalOperation(mit)) && isPipelineConsumed(programState, invocationTarget)) {
reportIssue(mit, "Refactor this code so that this consumed stream pipeline is not reused.", flow(invocationTarget, context.getNode()));
return null;
}
if (isIntermediateOperation(mit)) {
// intermediate operations return same stream pipeline, so we reuse SV
context.getConstraintManager().setValueFactory(() -> invocationTarget);
return Iterables.getOnlyElement(invocationTarget.setConstraint(programState, StreamPipelineConstraint.NOT_CONSUMED));
}
if (isTerminalOperation(mit)) {
return Iterables.getOnlyElement(invocationTarget.setConstraint(programState, StreamPipelineConstraint.CONSUMED));
}
if (mit.symbol().isUnknown()) {
// lambdas used in pipelines are sometimes not resolved properly, this is to shutdown the noise
programState = programState.removeConstraintsOnDomain(invocationTarget, StreamPipelineConstraint.class);
}
return programState;
}
use of org.sonar.java.se.ProgramState in project sonar-java by SonarSource.
the class BytecodeEGWalkerTest method generateMethodBehavior.
@Test
public void generateMethodBehavior() throws Exception {
MethodBehavior methodBehavior = getMethodBehavior("fun(ZLjava/lang/Object;)Ljava/lang/Object;");
assertThat(methodBehavior.yields()).hasSize(2);
SymbolicValue svFirstArg = new SymbolicValue();
SymbolicValue svsecondArg = new SymbolicValue();
SymbolicValue svResult = new SymbolicValue();
List<SymbolicValue> invocationArguments = Lists.newArrayList(svFirstArg, svsecondArg);
List<ObjectConstraint> collect = methodBehavior.yields().stream().map(my -> {
Collection<ProgramState> ps = my.statesAfterInvocation(invocationArguments, Lists.newArrayList(), ProgramState.EMPTY_STATE, () -> svResult).collect(Collectors.toList());
assertThat(ps).hasSize(1);
ProgramState next = ps.iterator().next();
return next.getConstraint(svResult, ObjectConstraint.class);
}).collect(Collectors.toList());
assertThat(collect).hasSize(2).containsOnly(ObjectConstraint.NOT_NULL, ObjectConstraint.NULL);
List<HappyPathYield> nullConstraintOnResult = methodBehavior.happyPathYields().filter(my -> ObjectConstraint.NULL.equals(my.resultConstraint().get(ObjectConstraint.class))).collect(Collectors.toList());
assertThat(nullConstraintOnResult).hasSize(1);
HappyPathYield nullConstraintResult = nullConstraintOnResult.get(0);
Collection<ProgramState> ps = nullConstraintResult.statesAfterInvocation(invocationArguments, Lists.newArrayList(), ProgramState.EMPTY_STATE, () -> svResult).collect(Collectors.toList());
assertThat(ps).hasSize(1);
ObjectConstraint constraint = ps.iterator().next().getConstraint(svsecondArg, ObjectConstraint.class);
assertThat(constraint).isSameAs(ObjectConstraint.NULL);
}
use of org.sonar.java.se.ProgramState in project sonar-java by SonarSource.
the class BytecodeEGWalkerTest method test_starting_states.
@Test
public void test_starting_states() throws Exception {
BytecodeEGWalker walker = new BytecodeEGWalker(null, semanticModel);
String signature = "type#foo()V";
walker.methodBehavior = new MethodBehavior(signature);
ProgramState startingState = Iterables.getOnlyElement(walker.startingStates(signature, ProgramState.EMPTY_STATE, false));
SymbolicValue thisSv = startingState.getValue(0);
assertThat(thisSv).isNotNull();
assertThat(startingState.getConstraints(thisSv).get(ObjectConstraint.class)).isEqualTo(ObjectConstraint.NOT_NULL);
startingState = Iterables.getOnlyElement(walker.startingStates(signature, ProgramState.EMPTY_STATE, true));
assertThat(startingState).isEqualTo(ProgramState.EMPTY_STATE);
signature = "type#foo(DIJ)V";
walker.methodBehavior = new MethodBehavior(signature);
startingState = Iterables.getOnlyElement(walker.startingStates(signature, ProgramState.EMPTY_STATE, true));
assertThat(startingState.getValue(0)).isNotNull();
SymbolicValue doubleArg = startingState.getValue(0);
assertThat(startingState.getConstraint(doubleArg, BytecodeEGWalker.StackValueCategoryConstraint.class)).isEqualTo(BytecodeEGWalker.StackValueCategoryConstraint.LONG_OR_DOUBLE);
assertThat(startingState.getValue(1)).isNull();
assertThat(startingState.getValue(2)).isNotNull();
SymbolicValue longArg = startingState.getValue(3);
assertThat(longArg).isNotNull();
assertThat(startingState.getConstraint(longArg, BytecodeEGWalker.StackValueCategoryConstraint.class)).isEqualTo(BytecodeEGWalker.StackValueCategoryConstraint.LONG_OR_DOUBLE);
}
Aggregations