use of org.sonar.java.bytecode.loader.SquidClassLoader 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.bytecode.loader.SquidClassLoader 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.bytecode.loader.SquidClassLoader 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.bytecode.loader.SquidClassLoader in project sonar-java by SonarSource.
the class TypeSubstitutionSolverTest method type_hierarchy_visit_should_be_limited.
@Test
public void type_hierarchy_visit_should_be_limited() {
ParametrizedTypeCache parametrizedTypeCache = new ParametrizedTypeCache();
BytecodeCompleter bytecodeCompleter = new BytecodeCompleter(new SquidClassLoader(new ArrayList<>()), parametrizedTypeCache);
Symbols symbols = new Symbols(bytecodeCompleter);
ActionParser<Tree> parser = JavaParser.createParser();
SemanticModel semanticModel = new SemanticModel(bytecodeCompleter);
Resolve resolve = new Resolve(symbols, bytecodeCompleter, parametrizedTypeCache);
TypeAndReferenceSolver typeAndReferenceSolver = new TypeAndReferenceSolver(semanticModel, symbols, resolve, parametrizedTypeCache);
CompilationUnitTree tree = (CompilationUnitTree) parser.parse(new File("src/test/files/sym/ComplexHierarchy.java"));
new FirstPass(semanticModel, symbols, resolve, parametrizedTypeCache, typeAndReferenceSolver).visitCompilationUnit(tree);
typeAndReferenceSolver.visitCompilationUnit(tree);
ClassTree classTree = (ClassTree) tree.types().get(tree.types().size() - 1);
JavaType site = (JavaType) classTree.symbol().type();
MethodInvocationTree mit = (MethodInvocationTree) ((ExpressionStatementTree) ((MethodTree) classTree.members().get(0)).block().body().get(0)).expression();
TypeSubstitutionSolver typeSubstitutionSolver = Mockito.spy(new TypeSubstitutionSolver(parametrizedTypeCache, symbols));
// call with empty formals should return.
typeSubstitutionSolver.applySiteSubstitutionToFormalParameters(new ArrayList<>(), site);
verify(typeSubstitutionSolver, times(0)).applySiteSubstitutionToFormalParameters(anyList(), any(JavaType.class), anySet());
JavaSymbol.MethodJavaSymbol methodJavaSymbol = (JavaSymbol.MethodJavaSymbol) mit.symbol();
typeSubstitutionSolver.applySiteSubstitutionToFormalParameters(((MethodJavaType) methodJavaSymbol.type).argTypes, site);
verify(typeSubstitutionSolver, times(11)).applySiteSubstitutionToFormalParameters(anyList(), any(JavaType.class), anySet());
}
use of org.sonar.java.bytecode.loader.SquidClassLoader in project sonar-java by SonarSource.
the class DebugCheckTestUtils method verifyIssuesWithMaxSteps.
static void verifyIssuesWithMaxSteps(String sourcefile, SECheck check, int maxSteps) {
BehaviorCache behaviorCache = new BehaviorCache(new SquidClassLoader(new ArrayList<>()));
SymbolicExecutionVisitor sev = new SymbolicExecutionVisitor(Collections.singletonList(check), behaviorCache) {
@Override
protected ExplodedGraphWalker getWalker() {
return new ExplodedGraphWalker(Collections.singletonList(check), behaviorCache, (SemanticModel) context.getSemanticModel()) {
@Override
protected int maxSteps() {
return maxSteps;
}
};
}
@Override
public void scanFile(JavaFileScannerContext context) {
super.scanFile(context);
// the check has been executed, but we still need to call the scan manually to report the issues
check.scanFile(context);
}
};
JavaCheckVerifier.verify(sourcefile, sev);
}
Aggregations