use of org.sonar.java.se.ProgramState in project sonar-java by SonarSource.
the class RelationalSymbolicValueTest method test_constraints_are_copied_over_transitive_relations.
@Test
public void test_constraints_are_copied_over_transitive_relations() throws Exception {
ProgramState ps = ProgramState.EMPTY_STATE;
ps = Iterables.getOnlyElement(a.setConstraint(ps, ObjectConstraint.NULL));
RelationalSymbolicValue ab = relationalSV(Tree.Kind.EQUAL_TO, a, b);
ps = setTrue(ps, ab);
assertNullConstraint(ps, b);
RelationalSymbolicValue cd = relationalSV(Tree.Kind.EQUAL_TO, c, d);
ps = setTrue(ps, cd);
RelationalSymbolicValue de = relationalSV(Tree.Kind.EQUAL_TO, d, e);
ps = setTrue(ps, de);
assertNoConstraints(ps, c);
assertNoConstraints(ps, d);
assertNoConstraints(ps, e);
assertThat(ps.getConstraint(relationalSV(Tree.Kind.EQUAL_TO, c, e), BooleanConstraint.class)).isEqualTo(TRUE);
// this relation will connect two distinct groups of relations (ab) -- (cde)
RelationalSymbolicValue bc = relationalSV(Tree.Kind.EQUAL_TO, b, c);
ps = setTrue(ps, bc);
// we assert that NULL was copied over to all values
assertNullConstraint(ps, b);
assertNullConstraint(ps, c);
assertNullConstraint(ps, d);
assertNullConstraint(ps, e);
}
use of org.sonar.java.se.ProgramState in project sonar-java by SonarSource.
the class RelationalSymbolicValueTest method recursion_on_copy_constraint_should_stop_distilled.
@Test
public void recursion_on_copy_constraint_should_stop_distilled() {
ProgramState ps = ProgramState.EMPTY_STATE;
SymbolicValue sv0 = new SymbolicValue();
ps = ps.addConstraint(sv0, TRUE);
ps = ps.addConstraint(neq(sv0, NULL_LITERAL), TRUE);
SymbolicValue sv1 = new SymbolicValue();
ps = ps.addConstraint(eq(sv1, NULL_LITERAL), TRUE);
ps = ps.addConstraint(neq(sv1, sv0), TRUE);
SymbolicValue sv2 = new SymbolicValue();
ps = ps.addConstraint(sv2, ObjectConstraint.NULL);
ps = ps.addConstraint(eq(sv2, sv1), TRUE);
RelationalSymbolicValue sv2NeqNull = neq(sv2, NULL_LITERAL);
ps = ps.addConstraint(eq(sv2, sv2NeqNull), TRUE);
ps = ps.addConstraint(eq(sv1, sv2NeqNull), TRUE);
List<ProgramState> result = sv2NeqNull.setConstraint(ps, FALSE);
assertThat(result).hasSize(1);
assertThat(result.get(0).getConstraint(sv2, BooleanConstraint.class)).isEqualTo(BooleanConstraint.FALSE);
}
use of org.sonar.java.se.ProgramState in project sonar-java by SonarSource.
the class BytecodeEGWalkerTest method verify_behavior_of_fun2_method.
@Test
public void verify_behavior_of_fun2_method() throws Exception {
MethodBehavior methodBehavior = getMethodBehavior("fun2(Z)Ljava/lang/Object;");
assertThat(methodBehavior.yields()).hasSize(2);
SymbolicValue svFirstArg = new SymbolicValue();
SymbolicValue svResult = new SymbolicValue();
List<SymbolicValue> invocationArguments = Lists.newArrayList(svFirstArg);
List<HappyPathYield> oneYield = methodBehavior.happyPathYields().filter(my -> ObjectConstraint.NULL.equals(my.resultConstraint().get(ObjectConstraint.class))).collect(Collectors.toList());
assertThat(oneYield).hasSize(1);
HappyPathYield yield = oneYield.get(0);
Collection<ProgramState> pss = yield.statesAfterInvocation(invocationArguments, Lists.newArrayList(), ProgramState.EMPTY_STATE, () -> svResult).collect(Collectors.toList());
assertThat(pss).hasSize(1);
ProgramState ps = pss.iterator().next();
assertThat(ps.getConstraint(svFirstArg, ObjectConstraint.class)).isNull();
assertThat(ps.getConstraint(svFirstArg, BooleanConstraint.class)).isSameAs(BooleanConstraint.TRUE);
assertThat(ps.getConstraint(svFirstArg, DivisionByZeroCheck.ZeroConstraint.class)).isNull();
oneYield = methodBehavior.happyPathYields().filter(my -> ObjectConstraint.NOT_NULL.equals(my.resultConstraint().get(ObjectConstraint.class))).collect(Collectors.toList());
assertThat(oneYield).hasSize(1);
yield = oneYield.get(0);
pss = yield.statesAfterInvocation(invocationArguments, Lists.newArrayList(), ProgramState.EMPTY_STATE, () -> svResult).collect(Collectors.toList());
assertThat(pss).hasSize(1);
ps = pss.iterator().next();
assertThat(ps.getConstraint(svFirstArg, ObjectConstraint.class)).isNull();
assertThat(ps.getConstraint(svFirstArg, BooleanConstraint.class)).isSameAs(BooleanConstraint.FALSE);
assertThat(ps.getConstraint(svFirstArg, DivisionByZeroCheck.ZeroConstraint.class)).isNull();
}
use of org.sonar.java.se.ProgramState in project sonar-java by SonarSource.
the class MethodYieldTest method test_creation_of_states.
@Test
public void test_creation_of_states() throws Exception {
SymbolicExecutionVisitor sev = createSymbolicExecutionVisitor("src/test/files/se/XProcYields.java");
MethodBehavior mb = getMethodBehavior(sev, "foo");
ProgramState ps = ProgramState.EMPTY_STATE;
SymbolicValue sv1 = new SymbolicValue();
SymbolicValue sv2 = new SymbolicValue();
SymbolicValue sv3 = new SymbolicValue();
Symbol sym = new JavaSymbol.VariableJavaSymbol(0, "myVar", new JavaSymbol.MethodJavaSymbol(0, "dummy", null));
ps = ps.put(sym, sv1);
MethodYield methodYield = mb.happyPathYields().findFirst().get();
Stream<ProgramState> generatedStatesFromFirstYield = methodYield.statesAfterInvocation(Lists.newArrayList(sv1, sv2), Lists.newArrayList(), ps, () -> sv3);
assertThat(generatedStatesFromFirstYield).hasSize(1);
}
use of org.sonar.java.se.ProgramState in project sonar-java by SonarSource.
the class ConstraintManager method assumeDual.
public Pair<List<ProgramState>, List<ProgramState>> assumeDual(ProgramState programState) {
ProgramState.Pop unstack = programState.unstackValue(1);
SymbolicValue sv = unstack.values.get(0);
List<ProgramState> falseConstraint = sv.setConstraint(unstack.state, BooleanConstraint.FALSE);
List<ProgramState> trueConstraint = sv.setConstraint(unstack.state, BooleanConstraint.TRUE);
return new Pair<>(falseConstraint, trueConstraint);
}
Aggregations