Search in sources :

Example 41 with ProgramState

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

the class CheckerDispatcher method executeCheckPreStatement.

public boolean executeCheckPreStatement(Instruction instruction) {
    this.currentInstruction = instruction;
    ProgramState ps;
    for (BytecodeSECheck checker : checks) {
        ps = checker.checkPreStatement(this, instruction);
        if (ps == null) {
            return false;
        }
        explodedGraphWalker.programState = ps;
    }
    return true;
}
Also used : ProgramState(org.sonar.java.se.ProgramState)

Example 42 with ProgramState

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

the class CheckerDispatcher method addTransition.

public void addTransition(ProgramState state) {
    ProgramState oldState = explodedGraphWalker.programState;
    explodedGraphWalker.programState = state;
    currentCheckerIndex++;
    executePost();
    currentCheckerIndex--;
    explodedGraphWalker.programState = oldState;
    this.transition = true;
}
Also used : ProgramState(org.sonar.java.se.ProgramState)

Example 43 with ProgramState

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

the class RelationalSymbolicValueTest method test_constraint_copy_of_sv_with_no_constraints_is_symmetric.

@Test
public void test_constraint_copy_of_sv_with_no_constraints_is_symmetric() throws Exception {
    ProgramState ps = ProgramState.EMPTY_STATE;
    SymbolicValue svZero = new SymbolicValue();
    ps = Iterables.getOnlyElement(svZero.setConstraint(ps, ZERO));
    SymbolicValue sv = new SymbolicValue();
    RelationalSymbolicValue neq = new RelationalSymbolicValue(NOT_EQUAL, svZero, sv);
    ProgramState psWithNeq = setTrue(ps, neq);
    assertThat(psWithNeq.getConstraint(svZero, ZERO.getClass())).isEqualTo(ZERO);
    neq = new RelationalSymbolicValue(NOT_EQUAL, sv, svZero);
    psWithNeq = setTrue(ps, neq);
    assertThat(psWithNeq.getConstraint(svZero, ZERO.getClass())).isEqualTo(ZERO);
}
Also used : ProgramState(org.sonar.java.se.ProgramState) Test(org.junit.Test)

Example 44 with ProgramState

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

the class RelationalSymbolicValueTest method test_conjuction_equal.

@Test
public void test_conjuction_equal() throws Exception {
    RelationalSymbolicValue aLEb = relationalSV(Tree.Kind.LESS_THAN_OR_EQUAL_TO, a, b);
    RelationalSymbolicValue bLEa = relationalSV(Tree.Kind.LESS_THAN_OR_EQUAL_TO, b, a);
    RelationalSymbolicValue aEb = relationalSV(Tree.Kind.EQUAL_TO, a, b);
    ProgramState state = Iterables.getOnlyElement(aEb.setConstraint(stateWithRelations(aLEb, bLEa), TRUE));
    assertThat(state.getConstraint(aEb, BooleanConstraint.class)).isEqualTo(TRUE);
}
Also used : ProgramState(org.sonar.java.se.ProgramState) Test(org.junit.Test)

Example 45 with ProgramState

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

the class RelationalSymbolicValueTest method test_constraint_copy_of_not_null_constraint.

@Test
public void test_constraint_copy_of_not_null_constraint() throws Exception {
    ProgramState ps = ProgramState.EMPTY_STATE;
    SymbolicValue svNotNull = new SymbolicValue();
    ps = Iterables.getOnlyElement(svNotNull.setConstraint(ps, ObjectConstraint.NOT_NULL));
    SymbolicValue sv = new SymbolicValue();
    // sv != NOT_NULL
    RelationalSymbolicValue neq = new RelationalSymbolicValue(NOT_EQUAL, svNotNull, sv);
    ProgramState result = setTrue(ps, neq);
    assertNoConstraints(result, sv);
    // sv != NULL
    neq = new RelationalSymbolicValue(NOT_EQUAL, NULL_LITERAL, sv);
    result = setTrue(ps, neq);
    assertThat(result.getConstraint(sv, ObjectConstraint.class)).isEqualTo(ObjectConstraint.NOT_NULL);
    // sv == NULL
    RelationalSymbolicValue eq = new RelationalSymbolicValue(EQUAL, NULL_LITERAL, sv);
    result = setTrue(ps, eq);
    assertNullConstraint(result, sv);
    // sv == NOT_NULL
    eq = new RelationalSymbolicValue(METHOD_EQUALS, sv, svNotNull);
    result = setTrue(ps, eq);
    assertThat(result.getConstraint(sv, ObjectConstraint.class)).isEqualTo(ObjectConstraint.NOT_NULL);
}
Also used : ProgramState(org.sonar.java.se.ProgramState) Test(org.junit.Test)

Aggregations

ProgramState (org.sonar.java.se.ProgramState)105 SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)72 Test (org.junit.Test)71 Instruction (org.sonar.java.bytecode.cfg.Instruction)61 RelationalSymbolicValue (org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)52 BinarySymbolicValue (org.sonar.java.se.symbolicvalues.BinarySymbolicValue)45 BooleanConstraint (org.sonar.java.se.constraint.BooleanConstraint)33 ObjectConstraint (org.sonar.java.se.constraint.ObjectConstraint)32 ProgramPoint (org.sonar.java.se.ProgramPoint)30 Constraint (org.sonar.java.se.constraint.Constraint)30 TypedConstraint (org.sonar.java.se.constraint.TypedConstraint)25 ConstraintsByDomain (org.sonar.java.se.constraint.ConstraintsByDomain)11 List (java.util.List)9 Collectors (java.util.stream.Collectors)8 Type (org.sonar.plugins.java.api.semantic.Type)8 VisibleForTesting (com.google.common.annotations.VisibleForTesting)7 ExplodedGraph (org.sonar.java.se.ExplodedGraph)7 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)7 Lists (com.google.common.collect.Lists)6 ArrayList (java.util.ArrayList)6