Search in sources :

Example 6 with ConstraintsByDomain

use of org.sonar.java.se.constraint.ConstraintsByDomain in project sonar-java by SonarSource.

the class BytecodeEGWalkerExecuteTest method assertStack.

private void assertStack(ProgramState ps, Constraint[]... constraints) {
    ProgramState.Pop pop = ps.unstackValue(constraints.length);
    assertEmptyStack(pop.state);
    assertThat(pop.valuesAndSymbols).hasSize(constraints.length);
    List<SymbolicValue> symbolicValues = pop.values;
    int idx = 0;
    for (SymbolicValue sv : symbolicValues) {
        ConstraintsByDomain constraintsByDomain = ps.getConstraints(sv);
        if (constraintsByDomain != null) {
            constraintsByDomain = constraintsByDomain.remove(BytecodeEGWalker.StackValueCategoryConstraint.class);
        }
        for (Constraint expectedConstraint : constraints[idx]) {
            if (expectedConstraint != null) {
                Class<? extends Constraint> expectedConstraintDomain = expectedConstraint.getClass();
                Constraint constraint = constraintsByDomain.get(expectedConstraintDomain);
                assertThat(constraint).isEqualTo(expectedConstraint);
                constraintsByDomain = constraintsByDomain.remove(expectedConstraintDomain);
            } else {
                assertThat(constraintsByDomain).isNull();
            }
        }
        if (constraintsByDomain != null) {
            assertThat(constraintsByDomain.isEmpty()).isTrue();
        }
        idx++;
    }
}
Also used : ConstraintsByDomain(org.sonar.java.se.constraint.ConstraintsByDomain) 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) ProgramState(org.sonar.java.se.ProgramState) 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)

Example 7 with ConstraintsByDomain

use of org.sonar.java.se.constraint.ConstraintsByDomain in project sonar-java by SonarSource.

the class BytecodeEGWalkerExecuteTest method method_returning_new_should_have_not_null_result.

@Test
public void method_returning_new_should_have_not_null_result() {
    MethodBehavior mb = walker.getMethodBehavior(BytecodeEGWalkerExecuteTest.class.getCanonicalName() + "#newObject()Ljava/lang/Object;", squidClassLoader);
    List<MethodYield> yields = mb.yields();
    assertThat(yields).hasSize(1);
    MethodYield yield = yields.get(0);
    assertThat(yield).isInstanceOf(HappyPathYield.class);
    ConstraintsByDomain resultConstraint = ((HappyPathYield) yield).resultConstraint();
    assertThat(resultConstraint).isNotNull();
    assertThat(resultConstraint.get(ObjectConstraint.class)).isEqualTo(ObjectConstraint.NOT_NULL);
    TypedConstraint typeConstraint = (TypedConstraint) resultConstraint.get(TypedConstraint.class);
    assertThat(typeConstraint.type.equals("java.lang.String")).isTrue();
}
Also used : ConstraintsByDomain(org.sonar.java.se.constraint.ConstraintsByDomain) HappyPathYield(org.sonar.java.se.xproc.HappyPathYield) MethodYield(org.sonar.java.se.xproc.MethodYield) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) TypedConstraint(org.sonar.java.se.constraint.TypedConstraint) Test(org.junit.Test)

Example 8 with ConstraintsByDomain

use of org.sonar.java.se.constraint.ConstraintsByDomain 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();
    }
}
Also used : ConstraintsByDomain(org.sonar.java.se.constraint.ConstraintsByDomain) ProgramState(org.sonar.java.se.ProgramState) Instruction(org.sonar.java.bytecode.cfg.Instruction) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) BooleanConstraint(org.sonar.java.se.constraint.BooleanConstraint) ProgramPoint(org.sonar.java.se.ProgramPoint) ZeroConstraint(org.sonar.java.se.checks.DivisionByZeroCheck.ZeroConstraint) Test(org.junit.Test)

Example 9 with ConstraintsByDomain

use of org.sonar.java.se.constraint.ConstraintsByDomain in project sonar-java by SonarSource.

the class MethodYieldTest method test_yield_equality.

@Test
public void test_yield_equality() {
    MethodBehavior methodBehavior = mockMethodBehavior(1, false);
    MethodYield yield = newMethodYield(methodBehavior);
    MethodYield otherYield;
    assertThat(yield).isNotEqualTo(null);
    assertThat(yield).isNotEqualTo(new Object());
    // same instance
    assertThat(yield).isEqualTo(yield);
    // method behavior not taken into account
    MethodYield myYield = newMethodYield(null);
    assertThat(yield).isEqualTo(myYield);
    // node not taken into account
    otherYield = newMethodYield(mockNode(), methodBehavior);
    assertThat(yield).isEqualTo(otherYield);
    // same arity and constraints on parameters but exceptional path
    otherYield = new ExceptionalYield(methodBehavior);
    assertThat(yield).isNotEqualTo(otherYield);
    // same arity and constraints on parameters but happy path path
    otherYield = new HappyPathYield(methodBehavior);
    assertThat(yield).isNotEqualTo(otherYield);
    ConstraintsByDomain nullConstraint = ConstraintsByDomain.empty().put(ObjectConstraint.NULL);
    MethodYield yield1 = newMethodYield(methodBehavior);
    yield1.parametersConstraints.add(nullConstraint);
    yield1.parametersConstraints.add(ConstraintsByDomain.empty());
    yield1.parametersConstraints.add(nullConstraint);
    MethodYield yield2 = newMethodYield(methodBehavior);
    yield2.parametersConstraints.add(nullConstraint);
    yield2.parametersConstraints.add(nullConstraint);
    yield2.parametersConstraints.add(ConstraintsByDomain.empty());
    assertThat(yield1).isNotEqualTo(yield2);
}
Also used : ConstraintsByDomain(org.sonar.java.se.constraint.ConstraintsByDomain) SETestUtils.mockMethodBehavior(org.sonar.java.se.SETestUtils.mockMethodBehavior) SETestUtils.getMethodBehavior(org.sonar.java.se.SETestUtils.getMethodBehavior) Test(org.junit.Test)

Example 10 with ConstraintsByDomain

use of org.sonar.java.se.constraint.ConstraintsByDomain 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)

Aggregations

ConstraintsByDomain (org.sonar.java.se.constraint.ConstraintsByDomain)20 SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)12 BooleanConstraint (org.sonar.java.se.constraint.BooleanConstraint)9 ProgramState (org.sonar.java.se.ProgramState)8 Test (org.junit.Test)7 ProgramPoint (org.sonar.java.se.ProgramPoint)6 Instruction (org.sonar.java.bytecode.cfg.Instruction)5 ZeroConstraint (org.sonar.java.se.checks.DivisionByZeroCheck.ZeroConstraint)5 Constraint (org.sonar.java.se.constraint.Constraint)5 ObjectConstraint (org.sonar.java.se.constraint.ObjectConstraint)4 RelationalSymbolicValue (org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)3 Type (org.sonar.plugins.java.api.semantic.Type)3 ArrayList (java.util.ArrayList)2 TypedConstraint (org.sonar.java.se.constraint.TypedConstraint)2 BinarySymbolicValue (org.sonar.java.se.symbolicvalues.BinarySymbolicValue)2 LinkedHashSet (java.util.LinkedHashSet)1 BiConsumer (java.util.function.BiConsumer)1 PMap (org.sonar.java.collections.PMap)1 ExplodedGraph (org.sonar.java.se.ExplodedGraph)1 SETestUtils.getMethodBehavior (org.sonar.java.se.SETestUtils.getMethodBehavior)1