Search in sources :

Example 16 with ConstraintsByDomain

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

the class MethodBehavior method createYield.

public void createYield(ExplodedGraph.Node node, boolean storeNodeForReporting) {
    ExplodedGraph.Node nodeForYield = null;
    if (storeNodeForReporting) {
        nodeForYield = node;
    }
    MethodYield yield;
    boolean expectReturnValue = !(isConstructor() || isVoidMethod());
    SymbolicValue resultSV = node.programState.exitValue();
    if ((resultSV == null && expectReturnValue) || resultSV instanceof SymbolicValue.ExceptionalSymbolicValue) {
        ExceptionalYield exceptionalYield = new ExceptionalYield(nodeForYield, this);
        if (resultSV != null) {
            Type type = ((SymbolicValue.ExceptionalSymbolicValue) resultSV).exceptionType();
            String typeName = null;
            if (type != null) {
                typeName = type.fullyQualifiedName();
            }
            exceptionalYield.setExceptionType(typeName);
        }
        yield = exceptionalYield;
    } else {
        HappyPathYield happyPathYield = new HappyPathYield(nodeForYield, this);
        if (expectReturnValue) {
            ConstraintsByDomain cleanup = cleanup(node.programState.getConstraints(resultSV), org.objectweb.asm.Type.getReturnType(signature.substring(signature.indexOf('('))));
            if (cleanup.isEmpty()) {
                cleanup = null;
            }
            happyPathYield.setResult(parameters.indexOf(resultSV), cleanup);
        }
        yield = happyPathYield;
    }
    addParameterConstraints(node, yield);
    yields.add(yield);
}
Also used : ConstraintsByDomain(org.sonar.java.se.constraint.ConstraintsByDomain) ExplodedGraph(org.sonar.java.se.ExplodedGraph) Type(org.sonar.plugins.java.api.semantic.Type) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue)

Example 17 with ConstraintsByDomain

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

the class BytecodeSECheckTest method zeroness_check_shifts.

@Test
public void zeroness_check_shifts() {
    int[] opCodes = { Opcodes.ISHL, Opcodes.LSHL, Opcodes.ISHR, Opcodes.LSHR, Opcodes.IUSHR, Opcodes.LUSHR };
    for (int shiftOpCode : opCodes) {
        Instruction instruction = new Instruction(shiftOpCode);
        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).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, 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();
    }
}
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 18 with ConstraintsByDomain

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

the class BytecodeSECheckTest method zeroness_check_add.

@Test
public void zeroness_check_add() {
    int[] opCodes = { Opcodes.DADD, Opcodes.FADD, Opcodes.IADD, Opcodes.LADD };
    for (int addOpCode : opCodes) {
        Instruction instruction = new Instruction(addOpCode);
        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(sv2);
        constraints = ps.getConstraints(peekValue);
        assertThat(constraints.get(ZeroConstraint.class)).isEqualTo(ZeroConstraint.NON_ZERO);
        assertThat(constraints.get(BooleanConstraint.class)).isEqualTo(BooleanConstraint.TRUE);
        ps = execute(instruction, nonZeroZeroPs);
        peekValue = ps.peekValue();
        assertThat(peekValue).isEqualTo(sv1);
        constraints = ps.getConstraints(peekValue);
        assertThat(constraints.get(ZeroConstraint.class)).isEqualTo(ZeroConstraint.NON_ZERO);
        assertThat(constraints.get(BooleanConstraint.class)).isEqualTo(BooleanConstraint.TRUE);
        ps = execute(instruction, nonZeroNonZeroPs);
        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 19 with ConstraintsByDomain

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

the class BytecodeSECheckTest method zeroness_check_negations.

@Test
public void zeroness_check_negations() {
    SymbolicValue sv1 = new SymbolicValue();
    int[] opCodes = { Opcodes.DNEG, Opcodes.FNEG, Opcodes.INEG, Opcodes.LNEG };
    ProgramState zeroPs = ProgramState.EMPTY_STATE;
    zeroPs = zeroPs.stackValue(sv1).addConstraints(sv1, ConstraintsByDomain.empty().put(ZeroConstraint.ZERO).put(BooleanConstraint.FALSE));
    ProgramState nonZeroPs = ProgramState.EMPTY_STATE;
    nonZeroPs = nonZeroPs.stackValue(sv1).addConstraints(sv1, ConstraintsByDomain.empty().put(ZeroConstraint.NON_ZERO).put(BooleanConstraint.TRUE));
    ProgramState noConstraint = ProgramState.EMPTY_STATE.stackValue(sv1);
    for (int negOpCode : opCodes) {
        Instruction instruction = new Instruction(negOpCode);
        ProgramState ps = execute(instruction, zeroPs);
        SymbolicValue peekValue = ps.peekValue();
        assertThat(peekValue).isEqualTo(sv1);
        ConstraintsByDomain constraints = ps.getConstraints(peekValue);
        assertThat(constraints.get(ZeroConstraint.class)).isEqualTo(ZeroConstraint.ZERO);
        assertThat(constraints.get(BooleanConstraint.class)).isEqualTo(BooleanConstraint.FALSE);
        ps = execute(instruction, nonZeroPs);
        peekValue = ps.peekValue();
        assertThat(peekValue).isNotEqualTo(sv1);
        constraints = ps.getConstraints(peekValue);
        assertThat(constraints.get(ZeroConstraint.class)).isEqualTo(ZeroConstraint.NON_ZERO);
        assertThat(constraints.get(BooleanConstraint.class)).isNull();
        ps = execute(instruction, noConstraint);
        peekValue = ps.peekValue();
        assertThat(peekValue).isNotEqualTo(sv1);
        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 20 with ConstraintsByDomain

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

the class BytecodeSECheckTest method zeroness_check_div_rem.

@Test
public void zeroness_check_div_rem() {
    int[] opCodes = { Opcodes.DDIV, Opcodes.FDIV, Opcodes.IDIV, Opcodes.LDIV, Opcodes.DREM, Opcodes.FREM, Opcodes.IREM, Opcodes.LREM };
    Set<Integer> remOpcodes = ImmutableSet.of(Opcodes.DREM, Opcodes.FREM, Opcodes.IREM, Opcodes.LREM);
    for (int divOpCode : opCodes) {
        Instruction instruction = new Instruction(divOpCode);
        ProgramState ps = execute(instruction, zeroZeroPs);
        assertThat(ps).isNull();
        ps = execute(instruction, zeroNonZeroPs);
        assertThat(ps).isNull();
        ps = execute(instruction, nonZeroZeroPs);
        SymbolicValue result = ps.peekValue();
        assertThat(result).isEqualTo(sv2);
        ConstraintsByDomain constraints = ps.getConstraints(result);
        assertThat(constraints.get(ZeroConstraint.class)).isEqualTo(ZeroConstraint.ZERO);
        assertThat(constraints.get(BooleanConstraint.class)).isEqualTo(BooleanConstraint.FALSE);
        ps = execute(instruction, nonZeroNonZeroPs);
        result = ps.peekValue();
        assertThat(result).isNotIn(sv1, sv2);
        constraints = ps.getConstraints(result);
        if (remOpcodes.contains(divOpCode)) {
            assertThat(constraints.get(ZeroConstraint.class)).isNull();
        } else {
            assertThat(constraints.get(ZeroConstraint.class)).isEqualTo(ZeroConstraint.NON_ZERO);
        }
        assertThat(constraints.get(BooleanConstraint.class)).isNull();
        ps = execute(instruction, noConstraints);
        result = ps.peekValue();
        assertThat(result).isNotIn(sv1, sv2);
        constraints = ps.getConstraints(result);
        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)

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