use of org.sonar.java.se.ProgramState in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method test_bipush.
@Test
public void test_bipush() throws Exception {
ProgramState programState = execute(new Instruction(Opcodes.BIPUSH, 42));
assertStack(programState, new Constraint[][] { { DivisionByZeroCheck.ZeroConstraint.NON_ZERO, ObjectConstraint.NOT_NULL } });
programState = execute(new Instruction(Opcodes.BIPUSH, 1));
assertStack(programState, new Constraint[][] { { DivisionByZeroCheck.ZeroConstraint.NON_ZERO, ObjectConstraint.NOT_NULL, BooleanConstraint.TRUE } });
programState = execute(new Instruction(Opcodes.BIPUSH, 0));
assertStack(programState, new Constraint[][] { { DivisionByZeroCheck.ZeroConstraint.ZERO, ObjectConstraint.NOT_NULL, BooleanConstraint.FALSE } });
}
use of org.sonar.java.se.ProgramState in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method test_iinc.
@Test
public void test_iinc() throws Exception {
SymbolicValue sv = new SymbolicValue();
ProgramState programState = ProgramState.EMPTY_STATE.put(2, sv);
programState = execute(new Instruction(Opcodes.IINC, 2), programState);
SymbolicValue result = programState.getValue(2);
assertThat(result).isNotEqualTo(sv);
assertThat(programState.getConstraint(result, ObjectConstraint.class)).isEqualTo(ObjectConstraint.NOT_NULL);
assertEmptyStack(programState);
assertThatThrownBy(() -> execute(new Instruction(Opcodes.IINC, 1), ProgramState.EMPTY_STATE)).hasMessage("Local variable 1 not found");
}
use of org.sonar.java.se.ProgramState in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method test_aconst_null.
@Test
public void test_aconst_null() throws Exception {
ProgramState programState = execute(new Instruction(Opcodes.ACONST_NULL));
assertStack(programState, ObjectConstraint.NULL);
}
use of org.sonar.java.se.ProgramState in project sonar-java by SonarSource.
the class BytecodeSECheckTest method zeroness_check_mul.
@Test
public void zeroness_check_mul() {
int[] opCodes = { Opcodes.DMUL, Opcodes.FMUL, Opcodes.IMUL, Opcodes.LMUL };
for (int mulOpCode : opCodes) {
Instruction instruction = new Instruction(mulOpCode);
ProgramState ps = execute(instruction, zeroZeroPs);
SymbolicValue peekValue = ps.peekValue();
assertThat(peekValue).isEqualTo(sv2);
ConstraintsByDomain constraints = ps.getConstraints(peekValue);
assertThat(constraints.get(ZeroConstraint.class)).isEqualTo(ZeroConstraint.ZERO);
assertThat(constraints.get(BooleanConstraint.class)).isEqualTo(BooleanConstraint.FALSE);
ps = execute(instruction, zeroNonZeroPs);
peekValue = ps.peekValue();
assertThat(peekValue).isEqualTo(sv1);
constraints = ps.getConstraints(peekValue);
assertThat(constraints.get(ZeroConstraint.class)).isEqualTo(ZeroConstraint.ZERO);
assertThat(constraints.get(BooleanConstraint.class)).isEqualTo(BooleanConstraint.FALSE);
ps = execute(instruction, nonZeroZeroPs);
peekValue = ps.peekValue();
assertThat(peekValue).isEqualTo(sv2);
constraints = ps.getConstraints(peekValue);
assertThat(constraints.get(ZeroConstraint.class)).isEqualTo(ZeroConstraint.ZERO);
assertThat(constraints.get(BooleanConstraint.class)).isEqualTo(BooleanConstraint.FALSE);
ps = execute(instruction, nonZeroNonZeroPs);
peekValue = ps.peekValue();
assertThat(peekValue).isNotIn(sv1, sv2);
constraints = ps.getConstraints(peekValue);
assertThat(constraints.get(ZeroConstraint.class)).isEqualTo(ZeroConstraint.NON_ZERO);
assertThat(constraints.get(BooleanConstraint.class)).isNull();
ps = execute(instruction, noConstraints);
peekValue = ps.peekValue();
assertThat(peekValue).isNotIn(sv1, sv2);
constraints = ps.getConstraints(peekValue);
assertThat(constraints.get(ZeroConstraint.class)).isNull();
assertThat(constraints.get(BooleanConstraint.class)).isNull();
}
}
use of org.sonar.java.se.ProgramState in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method test_dup_x2.
@Test
public void test_dup_x2() throws Exception {
SymbolicValue sv1 = new SymbolicValue();
SymbolicValue sv2 = new SymbolicValue();
SymbolicValue sv3 = new SymbolicValue();
ProgramState programState = execute(new Instruction(Opcodes.DUP_X2), ProgramState.EMPTY_STATE.stackValue(sv3).stackValue(sv2).stackValue(sv1));
ProgramState.Pop pop = programState.unstackValue(4);
assertThat(pop.values).containsExactly(sv1, sv2, sv3, sv1);
assertThat(pop.state).isEqualTo(ProgramState.EMPTY_STATE);
assertThatThrownBy(() -> execute(new Instruction(Opcodes.DUP_X2), ProgramState.EMPTY_STATE.stackValue(sv1).stackValue(sv2))).hasMessage("DUP_X2 needs 3 values on stack");
}
Aggregations