Search in sources :

Example 6 with BehaviorCache

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();
}
Also used : BytecodeCFG(org.sonar.java.bytecode.cfg.BytecodeCFG) CFG(org.sonar.java.cfg.CFG) ProgramPoint(org.sonar.java.se.ProgramPoint) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) BehaviorCache(org.sonar.java.se.xproc.BehaviorCache) Instruction(org.sonar.java.bytecode.cfg.Instruction) Test(org.junit.Test)

Example 7 with BehaviorCache

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]);
}
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 8 with BehaviorCache

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);
}
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 9 with BehaviorCache

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);
}
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 10 with BehaviorCache

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);
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) JavaVersionImpl(org.sonar.java.model.JavaVersionImpl) DefaultJavaFileScannerContext(org.sonar.java.model.DefaultJavaFileScannerContext) SemanticModel(org.sonar.java.resolve.SemanticModel) BehaviorCache(org.sonar.java.se.xproc.BehaviorCache) File(java.io.File)

Aggregations

BehaviorCache (org.sonar.java.se.xproc.BehaviorCache)11 Test (org.junit.Test)6 SquidClassLoader (org.sonar.java.bytecode.loader.SquidClassLoader)5 ArrayList (java.util.ArrayList)4 SemanticModel (org.sonar.java.resolve.SemanticModel)3 SETestUtils.createSymbolicExecutionVisitor (org.sonar.java.se.SETestUtils.createSymbolicExecutionVisitor)3 MethodBehavior (org.sonar.java.se.xproc.MethodBehavior)3 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)3 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)3 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)3 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)3 Tree (org.sonar.plugins.java.api.tree.Tree)3 File (java.io.File)2 ObjectConstraint (org.sonar.java.se.constraint.ObjectConstraint)2 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)2 HashSet (java.util.HashSet)1 Before (org.junit.Before)1 BytecodeCFG (org.sonar.java.bytecode.cfg.BytecodeCFG)1 Instruction (org.sonar.java.bytecode.cfg.Instruction)1 CFG (org.sonar.java.cfg.CFG)1