Search in sources :

Example 1 with Constraint

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);
}
Also used : ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) BooleanConstraint(org.sonar.java.se.constraint.BooleanConstraint) Constraint(org.sonar.java.se.constraint.Constraint) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) Test(org.junit.Test)

Example 2 with Constraint

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;
}
Also used : ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) BooleanConstraint(org.sonar.java.se.constraint.BooleanConstraint) Constraint(org.sonar.java.se.constraint.Constraint) ArrayList(java.util.ArrayList) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) BinarySymbolicValue(org.sonar.java.se.symbolicvalues.BinarySymbolicValue) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)

Example 3 with Constraint

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);
}
Also used : BytecodeTestClass(org.sonar.java.bytecode.se.testdata.BytecodeTestClass) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) Iterables(com.google.common.collect.Iterables) BeforeClass(org.junit.BeforeClass) ProgramState(org.sonar.java.se.ProgramState) MethodYield(org.sonar.java.se.xproc.MethodYield) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ExceptionEnqueue(org.sonar.java.bytecode.se.testdata.ExceptionEnqueue) DivisionByZeroCheck(org.sonar.java.se.checks.DivisionByZeroCheck) Lists(com.google.common.collect.Lists) Assertions.assertThat(org.fest.assertions.Assertions.assertThat) FinalBytecodeTestClass(org.sonar.java.bytecode.se.testdata.FinalBytecodeTestClass) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) BooleanConstraint(org.sonar.java.se.constraint.BooleanConstraint) JavaParser(org.sonar.java.ast.parser.JavaParser) Collection(java.util.Collection) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader) MaxRelationBytecode(org.sonar.java.bytecode.se.testdata.MaxRelationBytecode) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) Type(org.sonar.plugins.java.api.semantic.Type) Collectors(java.util.stream.Collectors) File(java.io.File) List(java.util.List) Rule(org.junit.Rule) LogTester(org.sonar.api.utils.log.LogTester) SemanticModel(org.sonar.java.resolve.SemanticModel) ByteStreams(com.google.common.io.ByteStreams) BehaviorCache(org.sonar.java.se.xproc.BehaviorCache) Constraint(org.sonar.java.se.constraint.Constraint) ExceptionalYield(org.sonar.java.se.xproc.ExceptionalYield) HappyPathYield(org.sonar.java.se.xproc.HappyPathYield) LoggerLevel(org.sonar.api.utils.log.LoggerLevel) HappyPathYield(org.sonar.java.se.xproc.HappyPathYield) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) Collection(java.util.Collection) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) ProgramState(org.sonar.java.se.ProgramState) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) Test(org.junit.Test)

Example 4 with Constraint

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();
    }
}
Also used : 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) Test(org.junit.Test)

Example 5 with Constraint

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");
    }
}
Also used : ProgramState(org.sonar.java.se.ProgramState) Instruction(org.sonar.java.bytecode.cfg.Instruction) 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) Test(org.junit.Test)

Aggregations

Constraint (org.sonar.java.se.constraint.Constraint)20 ObjectConstraint (org.sonar.java.se.constraint.ObjectConstraint)19 BooleanConstraint (org.sonar.java.se.constraint.BooleanConstraint)18 SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)12 ProgramState (org.sonar.java.se.ProgramState)11 Test (org.junit.Test)10 ConstraintsByDomain (org.sonar.java.se.constraint.ConstraintsByDomain)9 RelationalSymbolicValue (org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)9 List (java.util.List)8 Collectors (java.util.stream.Collectors)8 ProgramPoint (org.sonar.java.se.ProgramPoint)8 TypedConstraint (org.sonar.java.se.constraint.TypedConstraint)8 BinarySymbolicValue (org.sonar.java.se.symbolicvalues.BinarySymbolicValue)8 SemanticModel (org.sonar.java.resolve.SemanticModel)6 Instruction (org.sonar.java.bytecode.cfg.Instruction)5 Lists (com.google.common.collect.Lists)4 ArrayList (java.util.ArrayList)4 Collection (java.util.Collection)4 Nullable (javax.annotation.Nullable)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4