use of org.sonar.java.bytecode.cfg.Instructions 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.Instructions 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.Instructions 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.Instructions in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method test_tableswitch.
@Test
public void test_tableswitch() 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.visitTableSwitchInsn(0, 2, l3, new Label[] { l0, l1, l2 });
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.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();
});
}
Aggregations