use of org.sonar.java.se.ProgramState 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.ProgramState 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();
}
}
}
use of org.sonar.java.se.ProgramState in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method test_dup_x2_long_double.
@Test
public void test_dup_x2_long_double() throws Exception {
SymbolicValue normalSv = new SymbolicValue();
SymbolicValue longSv = new SymbolicValue();
SymbolicValue another = new SymbolicValue();
ProgramState startingState = ProgramState.EMPTY_STATE.stackValue(another).stackValue(longSv).stackValue(normalSv);
startingState = setDoubleOrLong(startingState, longSv, true);
ProgramState programState = execute(new Instruction(Opcodes.DUP_X2), startingState);
ProgramState.Pop pop = programState.unstackValue(4);
assertThat(pop.values).containsExactly(normalSv, longSv, normalSv, another);
}
use of org.sonar.java.se.ProgramState in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method test_multianewarray.
@Test
public void test_multianewarray() throws Exception {
ProgramState programState = execute(new Instruction.MultiANewArrayInsn("B", 1), ProgramState.EMPTY_STATE.stackValue(new SymbolicValue()));
assertStack(programState, ObjectConstraint.NOT_NULL);
programState = execute(new Instruction.MultiANewArrayInsn("B", 2), ProgramState.EMPTY_STATE.stackValue(new SymbolicValue()).stackValue(new SymbolicValue()));
assertStack(programState, ObjectConstraint.NOT_NULL);
assertThatThrownBy(() -> execute(new Instruction.MultiANewArrayInsn("B", 2))).hasMessage("MULTIANEWARRAY needs 2 values on stack");
}
use of org.sonar.java.se.ProgramState in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method test_newarray.
@Test
public void test_newarray() throws Exception {
SymbolicValue size = new SymbolicValue();
int[] opcodes = { Opcodes.NEWARRAY, Opcodes.ANEWARRAY };
for (int opcode : opcodes) {
ProgramState programState = execute(new Instruction(opcode), ProgramState.EMPTY_STATE.stackValue(size));
assertThat(programState.peekValue()).isNotEqualTo(size);
assertStack(programState, ObjectConstraint.NOT_NULL);
assertThatThrownBy(() -> execute(new Instruction(opcode), ProgramState.EMPTY_STATE)).hasMessage(Printer.OPCODES[opcode] + " needs 1 values on stack");
}
}
Aggregations