use of org.sonar.java.bytecode.cfg.Instruction in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method test_arraylength.
@Test
public void test_arraylength() throws Exception {
SymbolicValue arrayRef = new SymbolicValue();
ProgramState programState = execute(new Instruction(Opcodes.ARRAYLENGTH), ProgramState.EMPTY_STATE.stackValue(arrayRef));
SymbolicValue length = programState.peekValue();
assertStack(programState, new Constraint[] { null });
assertThat(length).isNotEqualTo(arrayRef);
assertThatThrownBy(() -> execute(new Instruction(Opcodes.ARRAYLENGTH), ProgramState.EMPTY_STATE)).hasMessage("ARRAYLENGTH needs 1 values on stack");
}
use of org.sonar.java.bytecode.cfg.Instruction in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method test_monitor_enter_exit.
@Test
public void test_monitor_enter_exit() throws Exception {
int[] opcodes = { Opcodes.MONITORENTER, Opcodes.MONITOREXIT };
for (int opcode : opcodes) {
ProgramState programState = execute(new Instruction(opcode), ProgramState.EMPTY_STATE.stackValue(new SymbolicValue()));
assertEmptyStack(programState);
assertThatThrownBy(() -> execute(new Instruction(opcode))).hasMessage(Printer.OPCODES[opcode] + " needs 1 values on stack");
}
}
use of org.sonar.java.bytecode.cfg.Instruction in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method test_checkcast.
@Test
public void test_checkcast() throws Exception {
SymbolicValue objectRef = new SymbolicValue();
ProgramState programState = execute(new Instruction(Opcodes.CHECKCAST), ProgramState.EMPTY_STATE.stackValue(objectRef));
assertThat(programState.peekValue()).isEqualTo(objectRef);
assertThatThrownBy(() -> execute(new Instruction(Opcodes.CHECKCAST), ProgramState.EMPTY_STATE)).hasMessage("CHECKCAST needs 1 value on stack");
}
use of org.sonar.java.bytecode.cfg.Instruction in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method test_iconst.
@Test
public void test_iconst() throws Exception {
ProgramState programState = execute(new Instruction(Opcodes.ICONST_0));
assertStack(programState, new Constraint[][] { { DivisionByZeroCheck.ZeroConstraint.ZERO, BooleanConstraint.FALSE, ObjectConstraint.NOT_NULL } });
programState = execute(new Instruction(Opcodes.ICONST_1));
assertStack(programState, new Constraint[][] { { DivisionByZeroCheck.ZeroConstraint.NON_ZERO, BooleanConstraint.TRUE, ObjectConstraint.NOT_NULL } });
int[] opCodesConst = new int[] { Opcodes.ICONST_M1, Opcodes.ICONST_2, Opcodes.ICONST_3, Opcodes.ICONST_4, Opcodes.ICONST_5 };
for (int opcode : opCodesConst) {
programState = execute(new Instruction(opcode));
assertStack(programState, new Constraint[][] { { DivisionByZeroCheck.ZeroConstraint.NON_ZERO, ObjectConstraint.NOT_NULL } });
}
}
use of org.sonar.java.bytecode.cfg.Instruction in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method assertConsume2produceNotNull.
private void assertConsume2produceNotNull(int... opcodes) {
SymbolicValue sv1 = new SymbolicValue();
SymbolicValue sv2 = new SymbolicValue();
ProgramState initState = ProgramState.EMPTY_STATE.stackValue(sv2).stackValue(sv1);
for (int opcode : opcodes) {
ProgramState programState = execute(new Instruction(opcode), initState);
ProgramState.Pop pop = programState.unstackValue(1);
assertStack(programState, new Constraint[][] { { ObjectConstraint.NOT_NULL } });
SymbolicValue result = pop.values.get(0);
assertThat(result).isNotEqualTo(sv1);
assertThat(result).isNotEqualTo(sv2);
assertThat(isDoubleOrLong(programState, result)).isEqualTo(LONG_OPCODE.contains(opcode));
}
}
Aggregations