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);
}
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);
}
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();
});
}
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);
}
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();
}
Aggregations