Search in sources :

Example 31 with MethodBehavior

use of org.sonar.java.se.xproc.MethodBehavior in project sonar-java by SonarSource.

the class DebugMethodYieldsCheck method checkEndOfExecution.

@Override
public void checkEndOfExecution(CheckerContext context) {
    MethodBehavior mb = ((CheckerDispatcher) context).methodBehavior();
    IdentifierTree methodName = methodNames.pop();
    if (mb != null) {
        reportIssue(methodName, String.format("Method '%s' has %d method yields.", methodName.name(), mb.yields().size()), flowFromYield(mb, methodName));
    }
}
Also used : CheckerDispatcher(org.sonar.java.se.CheckerDispatcher) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 32 with MethodBehavior

use of org.sonar.java.se.xproc.MethodBehavior 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 33 with MethodBehavior

use of org.sonar.java.se.xproc.MethodBehavior in project sonar-java by SonarSource.

the class BytecodeEGWalkerExecuteTest method exceptional_paths_should_be_enqueued.

@Test
public void exceptional_paths_should_be_enqueued() {
    MethodBehavior mb = walker.getMethodBehavior(BytecodeEGWalkerExecuteTest.class.getCanonicalName() + "#enqueue_exceptional_paths(Lorg/sonar/java/bytecode/se/BytecodeEGWalkerExecuteTest;)Ljava/lang/Object;", squidClassLoader);
    assertThat(mb.yields()).hasSize(2);
    List<Constraint> resultConstraints = mb.yields().stream().map(y -> ((HappyPathYield) y).resultConstraint()).map(c -> c.get(ObjectConstraint.class)).collect(Collectors.toList());
    assertThat(resultConstraints).contains(ObjectConstraint.NOT_NULL, ObjectConstraint.NULL);
}
Also used : ASTORE(org.objectweb.asm.Opcodes.ASTORE) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SETestUtils(org.sonar.java.se.SETestUtils) DMUL(org.objectweb.asm.Opcodes.DMUL) LSUB(org.objectweb.asm.Opcodes.LSUB) MethodYield(org.sonar.java.se.xproc.MethodYield) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) LSHR(org.objectweb.asm.Opcodes.LSHR) DSUB(org.objectweb.asm.Opcodes.DSUB) LMUL(org.objectweb.asm.Opcodes.LMUL) Mockito.doThrow(org.mockito.Mockito.doThrow) LSHL(org.objectweb.asm.Opcodes.LSHL) BooleanConstraint(org.sonar.java.se.constraint.BooleanConstraint) TypedConstraint(org.sonar.java.se.constraint.TypedConstraint) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) Collectors(java.util.stream.Collectors) BinarySymbolicValue(org.sonar.java.se.symbolicvalues.BinarySymbolicValue) LAND(org.objectweb.asm.Opcodes.LAND) List(java.util.List) LXOR(org.objectweb.asm.Opcodes.LXOR) Instruction(org.sonar.java.bytecode.cfg.Instruction) GOTO(org.objectweb.asm.Opcodes.GOTO) Constraint(org.sonar.java.se.constraint.Constraint) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) LUSHR(org.objectweb.asm.Opcodes.LUSHR) BeforeClass(org.junit.BeforeClass) ProgramState(org.sonar.java.se.ProgramState) Label(org.objectweb.asm.Label) Mockito.spy(org.mockito.Mockito.spy) ConstraintsByDomain(org.sonar.java.se.constraint.ConstraintsByDomain) ArrayList(java.util.ArrayList) Instructions(org.sonar.java.bytecode.cfg.Instructions) DivisionByZeroCheck(org.sonar.java.se.checks.DivisionByZeroCheck) ISTORE(org.objectweb.asm.Opcodes.ISTORE) INVOKESTATIC(org.objectweb.asm.Opcodes.INVOKESTATIC) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) BytecodeCFG(org.sonar.java.bytecode.cfg.BytecodeCFG) Before(org.junit.Before) JavaParser(org.sonar.java.ast.parser.JavaParser) LOR(org.objectweb.asm.Opcodes.LOR) ICONST_4(org.objectweb.asm.Opcodes.ICONST_4) Opcodes(org.objectweb.asm.Opcodes) DREM(org.objectweb.asm.Opcodes.DREM) ICONST_3(org.objectweb.asm.Opcodes.ICONST_3) ICONST_2(org.objectweb.asm.Opcodes.ICONST_2) ICONST_1(org.objectweb.asm.Opcodes.ICONST_1) ICONST_0(org.objectweb.asm.Opcodes.ICONST_0) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) DADD(org.objectweb.asm.Opcodes.DADD) Type(org.sonar.plugins.java.api.semantic.Type) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue) File(java.io.File) LADD(org.objectweb.asm.Opcodes.LADD) CFG(org.sonar.java.cfg.CFG) Printer(org.objectweb.asm.util.Printer) RETURN(org.objectweb.asm.Opcodes.RETURN) SemanticModel(org.sonar.java.resolve.SemanticModel) LREM(org.objectweb.asm.Opcodes.LREM) BehaviorCache(org.sonar.java.se.xproc.BehaviorCache) ILOAD(org.objectweb.asm.Opcodes.ILOAD) ProgramPoint(org.sonar.java.se.ProgramPoint) Preconditions(com.google.common.base.Preconditions) DDIV(org.objectweb.asm.Opcodes.DDIV) Collections(java.util.Collections) HappyPathYield(org.sonar.java.se.xproc.HappyPathYield) LDIV(org.objectweb.asm.Opcodes.LDIV) BooleanConstraint(org.sonar.java.se.constraint.BooleanConstraint) TypedConstraint(org.sonar.java.se.constraint.TypedConstraint) Constraint(org.sonar.java.se.constraint.Constraint) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) Test(org.junit.Test)

Example 34 with MethodBehavior

use of org.sonar.java.se.xproc.MethodBehavior in project sonar-java by SonarSource.

the class BytecodeSECheckTest method method_with_numerical_operations.

@Test
public void method_with_numerical_operations() throws Exception {
    BytecodeEGWalker walker = getEGWalker();
    MethodBehavior behavior = walker.getMethodBehavior("org.sonar.java.bytecode.se.BytecodeSECheckTest#foo(II)I", squidClassLoader);
    assertThat(behavior).isNotNull();
    assertThat(behavior.yields()).hasSize(3);
    assertThat(behavior.happyPathYields().map(HappyPathYield::toString).collect(Collectors.toList())).containsExactly("{params: [[], []], result: null (0)}");
    assertThat(behavior.exceptionalPathYields().map(ExceptionalYield::toString).collect(Collectors.toList())).containsExactly("{params: [[NON_ZERO], []], exceptional (java.lang.IndexOutOfBoundsException)}", "{params: [[], []], exceptional (java.lang.IndexOutOfBoundsException)}");
}
Also used : MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) Test(org.junit.Test)

Example 35 with MethodBehavior

use of org.sonar.java.se.xproc.MethodBehavior in project sonar-java by SonarSource.

the class BehaviorCacheTest method compute_beahvior_only_once.

@Test
public void compute_beahvior_only_once() throws Exception {
    SymbolicExecutionVisitor sev = createSymbolicExecutionVisitor("src/test/resources/se/ComputeBehaviorOnce.java");
    assertThat(sev.behaviorCache.behaviors.entrySet()).hasSize(5);
    assertThat(logTester.logs(LoggerLevel.DEBUG)).containsOnlyOnce("Could not complete symbolic execution: ");
    assertThat(sev.behaviorCache.behaviors.values()).allMatch(MethodBehavior::isVisited);
}
Also used : SETestUtils.getMethodBehavior(org.sonar.java.se.SETestUtils.getMethodBehavior) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) SETestUtils.createSymbolicExecutionVisitor(org.sonar.java.se.SETestUtils.createSymbolicExecutionVisitor) Test(org.junit.Test)

Aggregations

MethodBehavior (org.sonar.java.se.xproc.MethodBehavior)36 Test (org.junit.Test)30 SemanticModel (org.sonar.java.resolve.SemanticModel)10 BehaviorCache (org.sonar.java.se.xproc.BehaviorCache)10 HappyPathYield (org.sonar.java.se.xproc.HappyPathYield)10 MethodYield (org.sonar.java.se.xproc.MethodYield)10 List (java.util.List)8 Collectors (java.util.stream.Collectors)8 SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)8 Type (org.sonar.plugins.java.api.semantic.Type)8 Lists (com.google.common.collect.Lists)6 SquidClassLoader (org.sonar.java.bytecode.loader.SquidClassLoader)6 ProgramState (org.sonar.java.se.ProgramState)6 DivisionByZeroCheck (org.sonar.java.se.checks.DivisionByZeroCheck)6 BooleanConstraint (org.sonar.java.se.constraint.BooleanConstraint)6 ObjectConstraint (org.sonar.java.se.constraint.ObjectConstraint)6 ExceptionalYield (org.sonar.java.se.xproc.ExceptionalYield)6 File (java.io.File)5 Collection (java.util.Collection)5 Set (java.util.Set)5