use of org.sonar.java.bytecode.cfg.Instruction in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method test_load.
@Test
public void test_load() throws Exception {
int[] loadRefOpcodes = new int[] { Opcodes.ILOAD, Opcodes.LLOAD, Opcodes.FLOAD, Opcodes.DLOAD, Opcodes.ALOAD };
for (int loadRefOpcode : loadRefOpcodes) {
SymbolicValue loadRef = new SymbolicValue();
ProgramState programState = execute(new Instruction(loadRefOpcode, 0), ProgramState.EMPTY_STATE.put(0, loadRef));
assertThat(programState.peekValue()).isEqualTo(loadRef);
// no SV indexed should failed
assertThatThrownBy(() -> execute(new Instruction(loadRefOpcode, 0), ProgramState.EMPTY_STATE)).hasMessage("Loading a symbolic value unindexed");
}
}
use of org.sonar.java.bytecode.cfg.Instruction in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method test_lconst.
@Test
public void test_lconst() throws Exception {
ProgramState programState = execute(new Instruction(Opcodes.LCONST_0));
assertStack(programState, new Constraint[][] { { DivisionByZeroCheck.ZeroConstraint.ZERO, BooleanConstraint.FALSE, ObjectConstraint.NOT_NULL } });
programState = execute(new Instruction(Opcodes.LCONST_1));
assertStack(programState, new Constraint[][] { { DivisionByZeroCheck.ZeroConstraint.NON_ZERO, BooleanConstraint.TRUE, ObjectConstraint.NOT_NULL } });
}
use of org.sonar.java.bytecode.cfg.Instruction 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();
}
use of org.sonar.java.bytecode.cfg.Instruction in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method test_dup2_x2.
@Test
public void test_dup2_x2() throws Exception {
SymbolicValue sv1 = new SymbolicValue();
SymbolicValue sv2 = new SymbolicValue();
SymbolicValue sv3 = new SymbolicValue();
SymbolicValue sv4 = new SymbolicValue();
ProgramState programState = execute(new Instruction(Opcodes.DUP2_X2), ProgramState.EMPTY_STATE.stackValue(sv4).stackValue(sv3).stackValue(sv2).stackValue(sv1));
ProgramState.Pop pop = programState.unstackValue(6);
assertThat(pop.values).containsExactly(sv1, sv2, sv3, sv4, sv1, sv2);
assertThat(pop.state).isEqualTo(ProgramState.EMPTY_STATE);
assertThatThrownBy(() -> execute(new Instruction(Opcodes.DUP2_X2), ProgramState.EMPTY_STATE.stackValue(sv1).stackValue(sv2).stackValue(sv3))).hasMessage("DUP2_X2 needs 4 values on stack");
}
use of org.sonar.java.bytecode.cfg.Instruction 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