use of org.sonar.java.se.symbolicvalues.RelationalSymbolicValue in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method test_compare_with_zero.
@Test
public void test_compare_with_zero() {
SymbolicValue sv = new SymbolicValue();
int[] opcodes = { Opcodes.IFEQ, Opcodes.IFNE, Opcodes.IFLT, Opcodes.IFGE };
for (int opcode : opcodes) {
ProgramState programState = walker.branchingState(new Instruction(opcode), ProgramState.EMPTY_STATE.stackValue(sv));
RelationalSymbolicValue relSV = (RelationalSymbolicValue) programState.peekValue();
assertThat(relSV.getLeftOp()).isSameAs(sv);
assertThat(relSV.getRightOp()).isNotSameAs(sv);
assertThat(programState.getConstraints(relSV.getRightOp()).hasConstraint(DivisionByZeroCheck.ZeroConstraint.ZERO)).isTrue();
}
// these opcodes inverse operator and swap operands
int[] swapOperandsOpcodes = { Opcodes.IFLE, Opcodes.IFGT };
for (int opcode : swapOperandsOpcodes) {
ProgramState programState = walker.branchingState(new Instruction(opcode), ProgramState.EMPTY_STATE.stackValue(sv));
RelationalSymbolicValue relSV = (RelationalSymbolicValue) programState.peekValue();
assertThat(relSV.getRightOp()).isSameAs(sv);
assertThat(relSV.getLeftOp()).isNotSameAs(sv);
assertThat(programState.getConstraints(relSV.getLeftOp()).hasConstraint(DivisionByZeroCheck.ZeroConstraint.ZERO)).isTrue();
}
}
use of org.sonar.java.se.symbolicvalues.RelationalSymbolicValue in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method test_compare_with_null.
@Test
public void test_compare_with_null() {
SymbolicValue sv = new SymbolicValue();
int[] opcodes = { Opcodes.IFNULL, Opcodes.IFNONNULL };
for (int opcode : opcodes) {
ProgramState programState = walker.branchingState(new Instruction(opcode), ProgramState.EMPTY_STATE.stackValue(sv));
RelationalSymbolicValue relSV = (RelationalSymbolicValue) programState.peekValue();
assertThat(relSV.getLeftOp()).isSameAs(sv);
assertThat(relSV.getRightOp()).isSameAs(SymbolicValue.NULL_LITERAL);
}
}
use of org.sonar.java.se.symbolicvalues.RelationalSymbolicValue in project sonar-java by SonarSource.
the class ProgramState method addConstraint.
public ProgramState addConstraint(SymbolicValue symbolicValue, Constraint constraint) {
Preconditions.checkState(!(symbolicValue instanceof RelationalSymbolicValue && constraint == BooleanConstraint.FALSE), "Relations stored in PS should always use TRUE constraint. SV: %s", symbolicValue);
ConstraintsByDomain constraintsForSV = constraints.get(symbolicValue);
if (constraintsForSV == null) {
constraintsForSV = ConstraintsByDomain.empty();
}
return addConstraints(symbolicValue, constraintsForSV.put(constraint));
}
use of org.sonar.java.se.symbolicvalues.RelationalSymbolicValue in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method test_compare_instructions.
@Test
public void test_compare_instructions() {
int[] opcodes = { Opcodes.IF_ICMPEQ, Opcodes.IF_ICMPNE, Opcodes.IF_ICMPLT, Opcodes.IF_ICMPGE, Opcodes.IF_ACMPEQ, Opcodes.IF_ACMPNE };
SymbolicValue left = new SymbolicValue();
SymbolicValue right = new SymbolicValue();
for (int opcode : opcodes) {
ProgramState programState = walker.branchingState(new Instruction(opcode), ProgramState.EMPTY_STATE.stackValue(left).stackValue(right));
RelationalSymbolicValue relSV = (RelationalSymbolicValue) programState.peekValue();
assertThat(relSV.getLeftOp()).isSameAs(left);
assertThat(relSV.getRightOp()).isSameAs(right);
}
// these opcodes inverse operator and swap operands
int[] swapOperandsOpcodes = { Opcodes.IF_ICMPLE, Opcodes.IF_ICMPGT };
for (int opcode : swapOperandsOpcodes) {
ProgramState programState = walker.branchingState(new Instruction(opcode), ProgramState.EMPTY_STATE.stackValue(left).stackValue(right));
RelationalSymbolicValue relSV = (RelationalSymbolicValue) programState.peekValue();
assertThat(relSV.getRightOp()).isSameAs(left);
assertThat(relSV.getLeftOp()).isSameAs(right);
}
}
use of org.sonar.java.se.symbolicvalues.RelationalSymbolicValue in project sonar-java by SonarSource.
the class DivisionByZeroCheckTest method copyConstraint.
private DivisionByZeroCheck.ZeroConstraint copyConstraint(SymbolicValue a, SymbolicValue b, RelationalSymbolicValue.Kind relation, @Nullable DivisionByZeroCheck.ZeroConstraint expected) {
ProgramState ps = Iterables.getOnlyElement(a.setConstraint(ProgramState.EMPTY_STATE, ZERO));
RelationalSymbolicValue rel = new RelationalSymbolicValue(relation);
SymbolicValueTestUtil.computedFrom(rel, b, a);
ps = Iterables.getOnlyElement(rel.setConstraint(ps, BooleanConstraint.TRUE));
return ps.getConstraint(b, DivisionByZeroCheck.ZeroConstraint.class);
}
Aggregations