Search in sources :

Example 46 with SquidClassLoader

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]);
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) SemanticModel(org.sonar.java.resolve.SemanticModel) ArrayList(java.util.ArrayList) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) Tree(org.sonar.plugins.java.api.tree.Tree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) BehaviorCache(org.sonar.java.se.xproc.BehaviorCache) SETestUtils.createSymbolicExecutionVisitor(org.sonar.java.se.SETestUtils.createSymbolicExecutionVisitor) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader) Test(org.junit.Test)

Example 47 with SquidClassLoader

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);
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ArrayList(java.util.ArrayList) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) Tree(org.sonar.plugins.java.api.tree.Tree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) BehaviorCache(org.sonar.java.se.xproc.BehaviorCache) SETestUtils.createSymbolicExecutionVisitor(org.sonar.java.se.SETestUtils.createSymbolicExecutionVisitor) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader) Test(org.junit.Test)

Example 48 with SquidClassLoader

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);
}
Also used : MethodYield(org.sonar.java.se.xproc.MethodYield) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ArrayList(java.util.ArrayList) BehaviorCache(org.sonar.java.se.xproc.BehaviorCache) SETestUtils.createSymbolicExecutionVisitor(org.sonar.java.se.SETestUtils.createSymbolicExecutionVisitor) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader) Type(org.sonar.plugins.java.api.semantic.Type) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) Tree(org.sonar.plugins.java.api.tree.Tree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 49 with SquidClassLoader

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());
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ArrayList(java.util.ArrayList) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) Tree(org.sonar.plugins.java.api.tree.Tree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) File(java.io.File) Test(org.junit.Test)

Example 50 with SquidClassLoader

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);
}
Also used : ExplodedGraphWalker(org.sonar.java.se.ExplodedGraphWalker) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) ArrayList(java.util.ArrayList) BehaviorCache(org.sonar.java.se.xproc.BehaviorCache) SymbolicExecutionVisitor(org.sonar.java.se.SymbolicExecutionVisitor) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader)

Aggregations

SquidClassLoader (org.sonar.java.bytecode.loader.SquidClassLoader)53 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)37 File (java.io.File)24 Test (org.junit.Test)18 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)12 ArrayList (java.util.ArrayList)11 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)10 SemanticModel (org.sonar.java.resolve.SemanticModel)9 Tree (org.sonar.plugins.java.api.tree.Tree)9 List (java.util.List)7 Collectors (java.util.stream.Collectors)6 BeforeClass (org.junit.BeforeClass)6 BehaviorCache (org.sonar.java.se.xproc.BehaviorCache)6 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)5 Opcodes (org.objectweb.asm.Opcodes)5 JavaParser (org.sonar.java.ast.parser.JavaParser)5 IOException (java.io.IOException)4 Collections (java.util.Collections)4 Before (org.junit.Before)4 Label (org.objectweb.asm.Label)4