Search in sources :

Example 1 with BytecodeCFG

use of org.sonar.java.bytecode.cfg.BytecodeCFG in project sonar-java by SonarSource.

the class BytecodeEGWalkerExecuteTest method test_switch_enqueuing_in_trycatch.

@Test
public void test_switch_enqueuing_in_trycatch() throws Exception {
    Instructions mv = new Instructions();
    Label l0 = new Label();
    Label l1 = new Label();
    Label l2 = new Label();
    mv.visitTryCatchBlock(l0, l1, l2, "java/lang/Exception");
    mv.visitLabel(l0);
    mv.visitVarInsn(ILOAD, 1);
    mv.visitLookupSwitchInsn(l1, new int[] { 0 }, new Label[] { l1 });
    mv.visitLabel(l1);
    Label l3 = new Label();
    mv.visitJumpInsn(GOTO, l3);
    mv.visitLabel(l2);
    mv.visitVarInsn(ASTORE, 2);
    mv.visitLabel(l3);
    mv.visitInsn(RETURN);
    BytecodeCFG cfg = mv.cfg();
    BytecodeCFG.Block switchBlock = cfg.blocks().get(2);
    assertThat(switchBlock.terminator().opcode).isEqualTo(Opcodes.LOOKUPSWITCH);
    walker.programState = ProgramState.EMPTY_STATE;
    walker.handleBlockExit(new ProgramPoint(switchBlock));
    assertThat(walker.workList).hasSize(1);
}
Also used : ProgramPoint(org.sonar.java.se.ProgramPoint) BytecodeCFG(org.sonar.java.bytecode.cfg.BytecodeCFG) Label(org.objectweb.asm.Label) Instructions(org.sonar.java.bytecode.cfg.Instructions) Test(org.junit.Test)

Example 2 with BytecodeCFG

use of org.sonar.java.bytecode.cfg.BytecodeCFG in project sonar-java by SonarSource.

the class BytecodeEGWalkerExecuteTest method test_goto_enqueuing_in_trycatch.

@Test
public void test_goto_enqueuing_in_trycatch() throws Exception {
    Instructions mv = new Instructions();
    /*
     void test_goto(int i) {
       try {
          switch (i) {
            case 0:
              i = 1; // GOTO within try-catch
              break;
            case 1:
              i = 2;
              break;
          }
          i = 3;
        } catch (Exception e) {
          i = 4;
       }
     }
    */
    Label l0 = new Label();
    Label l1 = new Label();
    Label l2 = new Label();
    mv.visitTryCatchBlock(l0, l1, l2, "java/lang/Exception");
    mv.visitLabel(l0);
    mv.visitVarInsn(ILOAD, 1);
    Label l3 = new Label();
    Label l4 = new Label();
    Label l5 = new Label();
    mv.visitLookupSwitchInsn(l5, new int[] { 0, 1 }, new Label[] { l3, l4 });
    mv.visitLabel(l3);
    mv.visitInsn(ICONST_1);
    mv.visitVarInsn(ISTORE, 1);
    // tested GOTO instruction
    mv.visitJumpInsn(GOTO, l5);
    mv.visitLabel(l4);
    mv.visitInsn(ICONST_2);
    mv.visitVarInsn(ISTORE, 1);
    mv.visitLabel(l5);
    mv.visitInsn(ICONST_3);
    mv.visitVarInsn(ISTORE, 1);
    mv.visitLabel(l1);
    Label l6 = new Label();
    mv.visitJumpInsn(GOTO, l6);
    mv.visitLabel(l2);
    mv.visitVarInsn(ASTORE, 2);
    mv.visitInsn(ICONST_4);
    mv.visitVarInsn(ISTORE, 1);
    mv.visitLabel(l6);
    mv.visitInsn(RETURN);
    BytecodeCFG cfg = mv.cfg();
    BytecodeCFG.Block gotoBlock = cfg.blocks().get(4);
    assertThat(gotoBlock.terminator().opcode).isEqualTo(GOTO);
    walker.programState = ProgramState.EMPTY_STATE;
    walker.handleBlockExit(new ProgramPoint(gotoBlock));
    assertThat(walker.workList).hasSize(1);
}
Also used : ProgramPoint(org.sonar.java.se.ProgramPoint) BytecodeCFG(org.sonar.java.bytecode.cfg.BytecodeCFG) Label(org.objectweb.asm.Label) Instructions(org.sonar.java.bytecode.cfg.Instructions) Test(org.junit.Test)

Example 3 with BytecodeCFG

use of org.sonar.java.bytecode.cfg.BytecodeCFG in project sonar-java by SonarSource.

the class BytecodeEGWalkerExecuteTest method test_lookupswitch.

@Test
public void test_lookupswitch() throws Exception {
    Instructions instr = new Instructions();
    instr.visitVarInsn(ILOAD, 0);
    Label l0 = new Label();
    Label l1 = new Label();
    Label l2 = new Label();
    Label l3 = new Label();
    instr.visitLookupSwitchInsn(l3, new int[] { 0, 1, 2, 50 }, new Label[] { l0, l1, l2, l3 });
    instr.visitLabel(l0);
    instr.visitInsn(ICONST_0);
    instr.visitVarInsn(ISTORE, 1);
    instr.visitJumpInsn(GOTO, l3);
    instr.visitLabel(l1);
    instr.visitInsn(ICONST_0);
    instr.visitVarInsn(ISTORE, 2);
    instr.visitJumpInsn(GOTO, l3);
    instr.visitLabel(l2);
    instr.visitInsn(ICONST_0);
    instr.visitVarInsn(ISTORE, 3);
    instr.visitJumpInsn(GOTO, l3);
    instr.visitLabel(l3);
    instr.visitInsn(RETURN);
    BytecodeCFG cfg = instr.cfg();
    CFG.IBlock<Instruction> entry = cfg.entry();
    BytecodeEGWalker walker = new BytecodeEGWalker(null, null);
    walker.programState = ProgramState.EMPTY_STATE.stackValue(new SymbolicValue());
    walker.handleBlockExit(new ProgramPoint(entry));
    assertThat(walker.workList).hasSize(entry.successors().size());
    walker.workList.forEach(node -> {
        assertThat(node.programState.peekValue()).isNull();
        assertThat(entry.successors().contains(node.programPoint.block)).isTrue();
    });
}
Also used : BytecodeCFG(org.sonar.java.bytecode.cfg.BytecodeCFG) CFG(org.sonar.java.cfg.CFG) ProgramPoint(org.sonar.java.se.ProgramPoint) BytecodeCFG(org.sonar.java.bytecode.cfg.BytecodeCFG) Label(org.objectweb.asm.Label) Instructions(org.sonar.java.bytecode.cfg.Instructions) Instruction(org.sonar.java.bytecode.cfg.Instruction) BinarySymbolicValue(org.sonar.java.se.symbolicvalues.BinarySymbolicValue) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue) Test(org.junit.Test)

Example 4 with BytecodeCFG

use of org.sonar.java.bytecode.cfg.BytecodeCFG in project sonar-java by SonarSource.

the class BytecodeEGWalkerExecuteTest method test_enqueuing_only_happy_path.

@Test
public void test_enqueuing_only_happy_path() {
    BytecodeCFG cfg = SETestUtils.bytecodeCFG(TRY_CATCH_SIGNATURE, squidClassLoader);
    BytecodeCFG.Block b2 = cfg.blocks().get(2);
    walker.workList.clear();
    walker.programState = ProgramState.EMPTY_STATE.stackValue(new SymbolicValue());
    walker.handleBlockExit(new ProgramPoint(b2));
    assertThat(walker.workList).hasSize(1);
    assertThat(walker.workList.getFirst().programPoint.block.id()).isEqualTo(3);
}
Also used : ProgramPoint(org.sonar.java.se.ProgramPoint) BytecodeCFG(org.sonar.java.bytecode.cfg.BytecodeCFG) BinarySymbolicValue(org.sonar.java.se.symbolicvalues.BinarySymbolicValue) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue) Test(org.junit.Test)

Example 5 with BytecodeCFG

use of org.sonar.java.bytecode.cfg.BytecodeCFG in project sonar-java by SonarSource.

the class BytecodeEGWalkerExecuteTest method test_enqueuing_exceptional_yields2.

@Test
public void test_enqueuing_exceptional_yields2() {
    BytecodeCFG cfg = SETestUtils.bytecodeCFG(TRY_WRONG_CATCH_SIGNATURE, squidClassLoader);
    BytecodeCFG.Block b2 = cfg.blocks().get(2);
    walker.programState = ProgramState.EMPTY_STATE.stackValue(new SymbolicValue()).stackValue(new SymbolicValue());
    walker.programPosition = new ProgramPoint(b2).next().next();
    walker.executeInstruction(b2.elements().get(3));
    assertThat(walker.workList).hasSize(3);
    assertThat(walker.workList.pop().programState.exitValue()).isNotNull().isInstanceOf(SymbolicValue.ExceptionalSymbolicValue.class).extracting(sv -> ((SymbolicValue.ExceptionalSymbolicValue) sv).exceptionType().fullyQualifiedName()).containsExactly("java.lang.IllegalStateException");
    assertThat(walker.workList.pop().programState.exitValue()).isNull();
}
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) ProgramPoint(org.sonar.java.se.ProgramPoint) BytecodeCFG(org.sonar.java.bytecode.cfg.BytecodeCFG) BinarySymbolicValue(org.sonar.java.se.symbolicvalues.BinarySymbolicValue) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue) Test(org.junit.Test)

Aggregations

BytecodeCFG (org.sonar.java.bytecode.cfg.BytecodeCFG)8 ProgramPoint (org.sonar.java.se.ProgramPoint)8 Test (org.junit.Test)7 Label (org.objectweb.asm.Label)5 Instructions (org.sonar.java.bytecode.cfg.Instructions)5 BinarySymbolicValue (org.sonar.java.se.symbolicvalues.BinarySymbolicValue)5 RelationalSymbolicValue (org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)5 SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)5 Instruction (org.sonar.java.bytecode.cfg.Instruction)3 CFG (org.sonar.java.cfg.CFG)3 ProgramState (org.sonar.java.se.ProgramState)2 Preconditions (com.google.common.base.Preconditions)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 List (java.util.List)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1