Search in sources :

Example 6 with RelationalSymbolicValue

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();
    }
}
Also used : ProgramState(org.sonar.java.se.ProgramState) Instruction(org.sonar.java.bytecode.cfg.Instruction) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue) BinarySymbolicValue(org.sonar.java.se.symbolicvalues.BinarySymbolicValue) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue) BooleanConstraint(org.sonar.java.se.constraint.BooleanConstraint) TypedConstraint(org.sonar.java.se.constraint.TypedConstraint) Constraint(org.sonar.java.se.constraint.Constraint) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) ProgramPoint(org.sonar.java.se.ProgramPoint) Test(org.junit.Test)

Example 7 with RelationalSymbolicValue

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);
    }
}
Also used : ProgramState(org.sonar.java.se.ProgramState) Instruction(org.sonar.java.bytecode.cfg.Instruction) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue) BinarySymbolicValue(org.sonar.java.se.symbolicvalues.BinarySymbolicValue) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue) BooleanConstraint(org.sonar.java.se.constraint.BooleanConstraint) TypedConstraint(org.sonar.java.se.constraint.TypedConstraint) Constraint(org.sonar.java.se.constraint.Constraint) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) ProgramPoint(org.sonar.java.se.ProgramPoint) Test(org.junit.Test)

Example 8 with RelationalSymbolicValue

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));
}
Also used : ConstraintsByDomain(org.sonar.java.se.constraint.ConstraintsByDomain) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)

Example 9 with RelationalSymbolicValue

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);
    }
}
Also used : ProgramState(org.sonar.java.se.ProgramState) Instruction(org.sonar.java.bytecode.cfg.Instruction) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue) BinarySymbolicValue(org.sonar.java.se.symbolicvalues.BinarySymbolicValue) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue) BooleanConstraint(org.sonar.java.se.constraint.BooleanConstraint) TypedConstraint(org.sonar.java.se.constraint.TypedConstraint) Constraint(org.sonar.java.se.constraint.Constraint) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) ProgramPoint(org.sonar.java.se.ProgramPoint) Test(org.junit.Test)

Example 10 with RelationalSymbolicValue

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);
}
Also used : ProgramState(org.sonar.java.se.ProgramState) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)

Aggregations

RelationalSymbolicValue (org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)10 SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)7 Test (org.junit.Test)6 ProgramState (org.sonar.java.se.ProgramState)5 BooleanConstraint (org.sonar.java.se.constraint.BooleanConstraint)4 Constraint (org.sonar.java.se.constraint.Constraint)4 ObjectConstraint (org.sonar.java.se.constraint.ObjectConstraint)4 Instruction (org.sonar.java.bytecode.cfg.Instruction)3 ProgramPoint (org.sonar.java.se.ProgramPoint)3 TypedConstraint (org.sonar.java.se.constraint.TypedConstraint)3 BinarySymbolicValue (org.sonar.java.se.symbolicvalues.BinarySymbolicValue)3 UnclosedResourcesCheck (org.sonar.java.se.checks.UnclosedResourcesCheck)1 ConstraintsByDomain (org.sonar.java.se.constraint.ConstraintsByDomain)1