use of org.sonar.java.se.constraint.Constraint in project sonar-java by SonarSource.
the class ProgramStateTest method test_learned_constraint_binary_SV.
@Test
public void test_learned_constraint_binary_SV() {
SymbolicValue sv1 = new SymbolicValue();
SymbolicValue sv2 = new SymbolicValue();
RelationalSymbolicValue relation = new RelationalSymbolicValue(RelationalSymbolicValue.Kind.EQUAL);
SymbolicValueTestUtil.computedFrom(relation, sv1, sv2);
ProgramState parent = ProgramState.EMPTY_STATE;
ProgramState child = ProgramState.EMPTY_STATE.addConstraint(relation, BooleanConstraint.TRUE);
Set<LearnedConstraint> learnedConstraints = child.learnedConstraints(parent);
assertThat(learnedConstraints).hasSize(1);
Constraint relationConstraint = Iterables.getOnlyElement(learnedConstraints).constraint();
assertThat(relationConstraint).isEqualTo(BooleanConstraint.TRUE);
}
use of org.sonar.java.se.constraint.Constraint in project sonar-java by SonarSource.
the class ProgramState method getValuesWithConstraints.
public List<SymbolicValue> getValuesWithConstraints(final Constraint constraint) {
final List<SymbolicValue> result = new ArrayList<>();
constraints.forEach((symbolicValue, constraintByDomain) -> {
Constraint find = constraintByDomain.get(constraint.getClass());
if (constraint.equals(find)) {
result.add(symbolicValue);
}
});
return result;
}
use of org.sonar.java.se.constraint.Constraint in project sonar-java by SonarSource.
the class BytecodeEGWalkerTest method generateMethodBehavior.
@Test
public void generateMethodBehavior() throws Exception {
MethodBehavior methodBehavior = getMethodBehavior("fun(ZLjava/lang/Object;)Ljava/lang/Object;");
assertThat(methodBehavior.yields()).hasSize(2);
SymbolicValue svFirstArg = new SymbolicValue();
SymbolicValue svsecondArg = new SymbolicValue();
SymbolicValue svResult = new SymbolicValue();
List<SymbolicValue> invocationArguments = Lists.newArrayList(svFirstArg, svsecondArg);
List<ObjectConstraint> collect = methodBehavior.yields().stream().map(my -> {
Collection<ProgramState> ps = my.statesAfterInvocation(invocationArguments, Lists.newArrayList(), ProgramState.EMPTY_STATE, () -> svResult).collect(Collectors.toList());
assertThat(ps).hasSize(1);
ProgramState next = ps.iterator().next();
return next.getConstraint(svResult, ObjectConstraint.class);
}).collect(Collectors.toList());
assertThat(collect).hasSize(2).containsOnly(ObjectConstraint.NOT_NULL, ObjectConstraint.NULL);
List<HappyPathYield> nullConstraintOnResult = methodBehavior.happyPathYields().filter(my -> ObjectConstraint.NULL.equals(my.resultConstraint().get(ObjectConstraint.class))).collect(Collectors.toList());
assertThat(nullConstraintOnResult).hasSize(1);
HappyPathYield nullConstraintResult = nullConstraintOnResult.get(0);
Collection<ProgramState> ps = nullConstraintResult.statesAfterInvocation(invocationArguments, Lists.newArrayList(), ProgramState.EMPTY_STATE, () -> svResult).collect(Collectors.toList());
assertThat(ps).hasSize(1);
ObjectConstraint constraint = ps.iterator().next().getConstraint(svsecondArg, ObjectConstraint.class);
assertThat(constraint).isSameAs(ObjectConstraint.NULL);
}
use of org.sonar.java.se.constraint.Constraint in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method test_invoke_instance_method.
@Test
public void test_invoke_instance_method() throws Exception {
int[] opcodes = new int[] { Opcodes.INVOKESPECIAL, Opcodes.INVOKEVIRTUAL, Opcodes.INVOKEINTERFACE };
for (int opcode : opcodes) {
SymbolicValue thisSv = new SymbolicValue();
ProgramState stateWithThis = ProgramState.EMPTY_STATE.stackValue(thisSv);
ProgramState programState = execute(invokeMethod(opcode, "methodWithoutArgument", "()V"), stateWithThis);
assertEmptyStack(programState);
assertThat(programState.getConstraints(thisSv).get(ObjectConstraint.class)).isEqualTo(ObjectConstraint.NOT_NULL);
programState = execute(invokeMethod(opcode, "finalVoid", "()V"), stateWithThis);
assertEmptyStack(programState);
assertThat(programState.getConstraints(thisSv).get(ObjectConstraint.class)).isEqualTo(ObjectConstraint.NOT_NULL);
programState = execute(invokeMethod(opcode, "booleanMethod", "()Z"), stateWithThis);
assertStack(programState, new Constraint[] { null });
assertThat(isDoubleOrLong(programState, programState.peekValue())).isFalse();
SymbolicValue arg = new SymbolicValue();
programState = execute(invokeMethod(opcode, "intMethodWithIntArgument", "(I)I"), stateWithThis.stackValue(arg));
assertStack(programState, new Constraint[] { null });
assertThat(programState.peekValue()).isNotEqualTo(arg);
programState = execute(invokeMethod(opcode, "methodWithIntIntArgument", "(II)V"), stateWithThis.stackValue(arg).stackValue(arg));
assertEmptyStack(programState);
assertThatThrownBy(() -> execute(invokeMethod(opcode, "methodWithIntIntArgument", "(II)V"), stateWithThis)).isInstanceOf(IllegalStateException.class);
programState = execute(invokeMethod(opcode, "returningLong", "()J"), stateWithThis);
assertThat(isDoubleOrLong(programState, programState.peekValue())).isTrue();
programState = execute(invokeMethod(opcode, "returningDouble", "()D"), stateWithThis);
assertThat(isDoubleOrLong(programState, programState.peekValue())).isTrue();
}
}
use of org.sonar.java.se.constraint.Constraint in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method test_neg.
@Test
public void test_neg() throws Exception {
SymbolicValue sv = new SymbolicValue();
int[] negOpcodes = new int[] { Opcodes.INEG, Opcodes.LNEG, Opcodes.FNEG, Opcodes.DNEG };
ProgramState initState = ProgramState.EMPTY_STATE.stackValue(sv);
for (int negOpcode : negOpcodes) {
ProgramState programState = execute(new Instruction(negOpcode), initState);
assertStack(programState, new Constraint[][] { { ObjectConstraint.NOT_NULL } });
assertThat(programState.peekValue()).isNotEqualTo(sv);
}
for (int opcode : negOpcodes) {
assertThatThrownBy(() -> execute(new Instruction(opcode), ProgramState.EMPTY_STATE)).hasMessage(Printer.OPCODES[opcode] + " needs 1 values on stack");
}
}
Aggregations