use of org.sonar.java.se.xproc.BehaviorCache in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method behavior_with_no_yield_should_stack_value.
@Test
public void behavior_with_no_yield_should_stack_value() throws Exception {
BehaviorCache behaviorCache = new BehaviorCache(squidClassLoader);
MethodBehavior methodBehavior = behaviorCache.get("org.mypackage.MyClass#MyMethod()Ljava/lang/Exception;");
methodBehavior.completed();
BytecodeEGWalker walker = new BytecodeEGWalker(behaviorCache, semanticModel);
walker.programState = ProgramState.EMPTY_STATE;
CFG.IBlock block = mock(CFG.IBlock.class);
when(block.successors()).thenReturn(Collections.emptySet());
walker.programPosition = new ProgramPoint(block);
walker.workList.clear();
walker.executeInstruction(new Instruction(INVOKESTATIC, new Instruction.FieldOrMethod("org.mypackage.MyClass", "MyMethod", "()Ljava/lang/Exception;")));
assertThat(walker.workList.getFirst().programState.peekValue()).isNotNull();
}
use of org.sonar.java.se.xproc.BehaviorCache in project sonar-java by SonarSource.
the class ExplodedGraphWalkerTest method test_cleanup_state.
@Test
public void test_cleanup_state() {
final int[] steps = new int[2];
JavaCheckVerifier.verifyNoIssue("src/test/files/se/SeEngineTestCleanupState.java", new SymbolicExecutionVisitor(Collections.emptyList(), new BehaviorCache(new SquidClassLoader(new ArrayList<>()))) {
@Override
public void visitNode(Tree tree) {
ExplodedGraphWalker explodedGraphWalker = new ExplodedGraphWalker(this.behaviorCache, (SemanticModel) context.getSemanticModel(), false);
explodedGraphWalker.visitMethod((MethodTree) tree, methodBehaviorForSymbol(((MethodTree) tree).symbol()));
steps[0] += explodedGraphWalker.steps;
}
});
JavaCheckVerifier.verifyNoIssue("src/test/files/se/SeEngineTestCleanupState.java", new SymbolicExecutionVisitor(Collections.emptyList(), new BehaviorCache(new SquidClassLoader(new ArrayList<>()))) {
@Override
public void visitNode(Tree tree) {
ExplodedGraphWalker explodedGraphWalker = new ExplodedGraphWalker(this.behaviorCache, (SemanticModel) context.getSemanticModel());
MethodTree methodTree = (MethodTree) tree;
explodedGraphWalker.visitMethod(methodTree, methodBehaviorForSymbol(methodTree.symbol()));
steps[1] += explodedGraphWalker.steps;
}
});
assertThat(steps[0]).isPositive();
assertThat(steps[0]).isGreaterThan(steps[1]);
}
use of org.sonar.java.se.xproc.BehaviorCache in project sonar-java by SonarSource.
the class ExplodedGraphWalkerTest method use_false_branch_on_loop_when_reaching_max_exec_program_point.
@Test
public void use_false_branch_on_loop_when_reaching_max_exec_program_point() {
ProgramPoint[] programPoints = new ProgramPoint[2];
JavaCheckVerifier.verifyNoIssue("src/test/files/se/SeEngineTestMaxExecProgramPoint.java", new SymbolicExecutionVisitor(Collections.emptyList(), new BehaviorCache(new SquidClassLoader(new ArrayList<>()))) {
private ExplodedGraphWalker explodedGraphWalker = null;
@Override
public void visitNode(Tree tree) {
if (explodedGraphWalker == null) {
explodedGraphWalker = new ExplodedGraphWalker(this.behaviorCache, (SemanticModel) context.getSemanticModel()) {
boolean shouldEnqueueFalseBranch = false;
@Override
public void enqueue(ProgramPoint programPoint, ProgramState programState, boolean exitPath) {
int nbOfExecution = programState.numberOfTimeVisited(programPoint);
if (nbOfExecution > MAX_EXEC_PROGRAM_POINT) {
shouldEnqueueFalseBranch = true;
programPoints[0] = programPoint;
} else {
shouldEnqueueFalseBranch = false;
}
int workListSize = workList.size();
super.enqueue(programPoint, programState, exitPath);
assertThat(workList.size()).isEqualTo(workListSize + 1);
if (shouldEnqueueFalseBranch) {
assertThat(programPoints[1]).isNull();
programPoints[1] = workList.peekFirst().programPoint;
}
}
};
}
MethodTree methodTree = (MethodTree) tree;
explodedGraphWalker.visitMethod(methodTree, methodBehaviorForSymbol(methodTree.symbol()));
}
});
// we reached the max number of execution of a program point
assertThat(programPoints[0]).isNotNull();
// B2 - for each
assertThat(programPoints[0].block.id()).isEqualTo(2);
// we enqueued a new node in the workList after reaching the max number of execeution point
assertThat(programPoints[1]).isNotNull();
// B1 - using the false branch to exit the loop
assertThat(programPoints[1].block.id()).isEqualTo(1);
}
use of org.sonar.java.se.xproc.BehaviorCache in project sonar-java by SonarSource.
the class ExplodedGraphWalkerTest method different_exceptions_lead_to_different_program_states_with_catch_exception_block.
@Test
public void different_exceptions_lead_to_different_program_states_with_catch_exception_block() {
Set<Type> encounteredExceptions = new HashSet<>();
int[] tested = { 0 };
JavaCheckVerifier.verifyNoIssue("src/test/files/se/ExceptionalSymbolicValueStacked.java", new SymbolicExecutionVisitor(Collections.emptyList(), new BehaviorCache(new SquidClassLoader(new ArrayList<>()))) {
private ExplodedGraphWalker explodedGraphWalker;
@Override
public void visitNode(Tree tree) {
if (explodedGraphWalker == null) {
explodedGraphWalker = new ExplodedGraphWalker(this.behaviorCache, (SemanticModel) context.getSemanticModel()) {
private ExplodedGraph.Node firstExceptionalNode = null;
@Override
public void enqueue(ProgramPoint newProgramPoint, ProgramState programState, boolean exitPath, MethodYield methodYield) {
SymbolicValue.ExceptionalSymbolicValue exceptionSV = null;
SymbolicValue peekValue = programState.peekValue();
boolean getNode = false;
if (peekValue instanceof SymbolicValue.ExceptionalSymbolicValue) {
exceptionSV = (SymbolicValue.ExceptionalSymbolicValue) peekValue;
Type exceptionType = exceptionSV.exceptionType();
if (exceptionType != null && (exceptionType.is("org.foo.MyException1") || exceptionType.is("org.foo.MyException2"))) {
encounteredExceptions.add(exceptionType);
getNode = true;
}
}
int workListSize = workList.size();
super.enqueue(newProgramPoint, programState, exitPath, methodYield);
if (getNode) {
if (firstExceptionalNode == null) {
firstExceptionalNode = workList.peekFirst();
}
assertThat(workList.size()).as("Should have created a new node in the graph for each of the exceptions").isEqualTo(workListSize + 1);
assertThat(workList.peekFirst().programState.peekValue()).as("Exceptional Symbolic Value should stay on the stack").isEqualTo(exceptionSV);
tested[0]++;
}
}
};
}
MethodTree methodTree = (MethodTree) tree;
if ("foo".equals(methodTree.symbol().name())) {
explodedGraphWalker.visitMethod(methodTree, methodBehaviorForSymbol(methodTree.symbol()));
} else {
super.visitNode(methodTree);
}
}
});
assertThat(encounteredExceptions).hasSize(2);
assertThat(tested[0]).isEqualTo(2);
}
use of org.sonar.java.se.xproc.BehaviorCache in project sonar-java by SonarSource.
the class SETestUtils method createSymbolicExecutionVisitorAndSemantic.
public static Pair<SymbolicExecutionVisitor, SemanticModel> createSymbolicExecutionVisitorAndSemantic(String fileName, boolean crossFileEnabled, SECheck... checks) {
File file = new File(fileName);
CompilationUnitTree cut = (CompilationUnitTree) PARSER.parse(file);
SemanticModel semanticModel = SemanticModel.createFor(cut, CLASSLOADER);
SymbolicExecutionVisitor sev = new SymbolicExecutionVisitor(Arrays.asList(checks), new BehaviorCache(CLASSLOADER, crossFileEnabled));
sev.scanFile(new DefaultJavaFileScannerContext(cut, file, semanticModel, null, new JavaVersionImpl(8), true));
return new Pair<>(sev, semanticModel);
}
Aggregations