Search in sources :

Example 1 with ProgramPoint

use of org.sonar.java.se.ProgramPoint 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 ProgramPoint

use of org.sonar.java.se.ProgramPoint 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 ProgramPoint

use of org.sonar.java.se.ProgramPoint 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 ProgramPoint

use of org.sonar.java.se.ProgramPoint 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 ProgramPoint

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

the class BytecodeEGWalker method handleBranching.

private void handleBranching(Instruction terminator) {
    programState = branchingState(terminator, programState);
    Pair<List<ProgramState>, List<ProgramState>> pair = constraintManager.assumeDual(programState);
    ProgramPoint falsePP = new ProgramPoint(((BytecodeCFG.Block) programPosition.block).falseSuccessor());
    ProgramPoint truePP = new ProgramPoint(((BytecodeCFG.Block) programPosition.block).trueSuccessor());
    pair.a.forEach(s -> enqueue(falsePP, s));
    pair.b.forEach(s -> enqueue(truePP, s));
}
Also used : ProgramPoint(org.sonar.java.se.ProgramPoint) BytecodeCFG(org.sonar.java.bytecode.cfg.BytecodeCFG) ImmutableList(com.google.common.collect.ImmutableList) LinkedList(java.util.LinkedList) List(java.util.List)

Aggregations

ProgramPoint (org.sonar.java.se.ProgramPoint)14 BytecodeCFG (org.sonar.java.bytecode.cfg.BytecodeCFG)12 Test (org.junit.Test)8 RelationalSymbolicValue (org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)6 SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)6 Label (org.objectweb.asm.Label)5 Instruction (org.sonar.java.bytecode.cfg.Instruction)5 CFG (org.sonar.java.cfg.CFG)5 ProgramState (org.sonar.java.se.ProgramState)5 BinarySymbolicValue (org.sonar.java.se.symbolicvalues.BinarySymbolicValue)5 List (java.util.List)3 Instructions (org.sonar.java.bytecode.cfg.Instructions)3 ExplodedGraph (org.sonar.java.se.ExplodedGraph)3 BooleanConstraint (org.sonar.java.se.constraint.BooleanConstraint)3 Constraint (org.sonar.java.se.constraint.Constraint)3 ObjectConstraint (org.sonar.java.se.constraint.ObjectConstraint)3 TypedConstraint (org.sonar.java.se.constraint.TypedConstraint)3 BehaviorCache (org.sonar.java.se.xproc.BehaviorCache)3 MethodBehavior (org.sonar.java.se.xproc.MethodBehavior)3 Preconditions (com.google.common.base.Preconditions)2