use of org.sonar.java.se.symbolicvalues.SymbolicValue in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method test_xReturn.
@Test
public void test_xReturn() throws Exception {
SymbolicValue returnValue = new SymbolicValue();
int[] opcodes = { Opcodes.IRETURN, Opcodes.LRETURN, Opcodes.FRETURN, Opcodes.DRETURN, Opcodes.ARETURN };
for (int opcode : opcodes) {
ProgramState programState = execute(new Instruction(opcode), ProgramState.EMPTY_STATE.stackValue(returnValue));
assertThat(programState.peekValue()).isNull();
assertThat(programState.exitValue()).isEqualTo(returnValue);
}
}
use of org.sonar.java.se.symbolicvalues.SymbolicValue in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method test_dup2_x2_form2.
// see https://docs.oracle.com/javase/specs/jvms/se9/html/jvms-6.html#jvms-6.5.dup2_x2
@Test
public void test_dup2_x2_form2() throws Exception {
SymbolicValue sv1 = new SymbolicValue();
SymbolicValue sv2 = new SymbolicValue();
SymbolicValue sv3 = new SymbolicValue();
SymbolicValue sv4 = new SymbolicValue();
ProgramState startingState = ProgramState.EMPTY_STATE.stackValue(sv4).stackValue(sv3).stackValue(sv2).stackValue(sv1);
startingState = setDoubleOrLong(startingState, sv1, true);
ProgramState programState = execute(new Instruction(Opcodes.DUP2_X2), startingState);
ProgramState.Pop pop = programState.unstackValue(6);
assertThat(pop.values).containsExactly(sv1, sv2, sv3, sv1, sv4);
}
use of org.sonar.java.se.symbolicvalues.SymbolicValue in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method test_dup2.
@Test
public void test_dup2() throws Exception {
SymbolicValue sv1 = new SymbolicValue();
SymbolicValue sv2 = new SymbolicValue();
ProgramState programState = execute(new Instruction(Opcodes.DUP2), ProgramState.EMPTY_STATE.stackValue(sv2).stackValue(sv1));
ProgramState.Pop pop = programState.unstackValue(4);
assertThat(pop.values).containsExactly(sv1, sv2, sv1, sv2);
assertThat(pop.state).isEqualTo(ProgramState.EMPTY_STATE);
assertThatThrownBy(() -> execute(new Instruction(Opcodes.DUP2))).hasMessage("DUP2 needs at least 1 value on stack");
}
use of org.sonar.java.se.symbolicvalues.SymbolicValue in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method assertBinarySymbolicValue.
private void assertBinarySymbolicValue(int[] opcodes, Class<? extends BinarySymbolicValue> binarySvClass) {
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(result).isInstanceOf(binarySvClass);
assertThat(isDoubleOrLong(programState, result)).isEqualTo(LONG_OPCODE.contains(opcode));
BinarySymbolicValue andSv = (BinarySymbolicValue) result;
assertThat(andSv.getRightOp()).isEqualTo(sv1);
assertThat(andSv.getLeftOp()).isEqualTo(sv2);
}
}
use of org.sonar.java.se.symbolicvalues.SymbolicValue in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method test_array_load.
@Test
public void test_array_load() throws Exception {
int[] loadRefOpcodes = new int[] { Opcodes.IALOAD, Opcodes.LALOAD, Opcodes.FALOAD, Opcodes.DALOAD, Opcodes.AALOAD, Opcodes.BALOAD, Opcodes.CALOAD, Opcodes.SALOAD };
SymbolicValue array = new SymbolicValue();
SymbolicValue index = new SymbolicValue();
ProgramState initState = ProgramState.EMPTY_STATE.stackValue(array).stackValue(index);
for (int opcode : loadRefOpcodes) {
ProgramState programState = execute(new Instruction(opcode), initState);
if (opcode != Opcodes.AALOAD) {
assertStack(programState, ObjectConstraint.NOT_NULL);
}
ProgramState.Pop result = programState.unstackValue(1);
assertThat(result.values).hasSize(1);
assertThat(result.values.get(0)).isNotEqualTo(array);
assertThat(result.values.get(0)).isNotEqualTo(index);
if (opcode == Opcodes.DALOAD || opcode == Opcodes.LALOAD) {
assertThat(isDoubleOrLong(programState, result.values.get(0))).isTrue();
}
}
}
Aggregations