Search in sources :

Example 31 with SymbolicValue

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

the class MethodBehavior method addParameterConstraints.

private void addParameterConstraints(ExplodedGraph.Node node, MethodYield yield) {
    // add the constraints on all the parameters
    int index = 0;
    for (SymbolicValue parameter : parameters) {
        ConstraintsByDomain constraints = node.programState.getConstraints(parameter);
        if (constraints == null) {
            constraints = ConstraintsByDomain.empty();
        } else {
            // cleanup based on signature
            org.objectweb.asm.Type[] argumentTypes = org.objectweb.asm.Type.getArgumentTypes(signature.substring(signature.indexOf('(')));
            constraints = cleanup(constraints, argumentTypes[index]);
        }
        yield.parametersConstraints.add(constraints);
        index++;
    }
}
Also used : ConstraintsByDomain(org.sonar.java.se.constraint.ConstraintsByDomain) Type(org.sonar.plugins.java.api.semantic.Type) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) BooleanConstraint(org.sonar.java.se.constraint.BooleanConstraint)

Example 32 with SymbolicValue

use of org.sonar.java.se.symbolicvalues.SymbolicValue 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 33 with SymbolicValue

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

the class BytecodeEGWalkerTest method test_starting_states.

@Test
public void test_starting_states() throws Exception {
    BytecodeEGWalker walker = new BytecodeEGWalker(null, semanticModel);
    String signature = "type#foo()V";
    walker.methodBehavior = new MethodBehavior(signature);
    ProgramState startingState = Iterables.getOnlyElement(walker.startingStates(signature, ProgramState.EMPTY_STATE, false));
    SymbolicValue thisSv = startingState.getValue(0);
    assertThat(thisSv).isNotNull();
    assertThat(startingState.getConstraints(thisSv).get(ObjectConstraint.class)).isEqualTo(ObjectConstraint.NOT_NULL);
    startingState = Iterables.getOnlyElement(walker.startingStates(signature, ProgramState.EMPTY_STATE, true));
    assertThat(startingState).isEqualTo(ProgramState.EMPTY_STATE);
    signature = "type#foo(DIJ)V";
    walker.methodBehavior = new MethodBehavior(signature);
    startingState = Iterables.getOnlyElement(walker.startingStates(signature, ProgramState.EMPTY_STATE, true));
    assertThat(startingState.getValue(0)).isNotNull();
    SymbolicValue doubleArg = startingState.getValue(0);
    assertThat(startingState.getConstraint(doubleArg, BytecodeEGWalker.StackValueCategoryConstraint.class)).isEqualTo(BytecodeEGWalker.StackValueCategoryConstraint.LONG_OR_DOUBLE);
    assertThat(startingState.getValue(1)).isNull();
    assertThat(startingState.getValue(2)).isNotNull();
    SymbolicValue longArg = startingState.getValue(3);
    assertThat(longArg).isNotNull();
    assertThat(startingState.getConstraint(longArg, BytecodeEGWalker.StackValueCategoryConstraint.class)).isEqualTo(BytecodeEGWalker.StackValueCategoryConstraint.LONG_OR_DOUBLE);
}
Also used : MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) ProgramState(org.sonar.java.se.ProgramState) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) Test(org.junit.Test)

Example 34 with SymbolicValue

use of org.sonar.java.se.symbolicvalues.SymbolicValue 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 35 with SymbolicValue

use of org.sonar.java.se.symbolicvalues.SymbolicValue 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

SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)132 RelationalSymbolicValue (org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)94 Test (org.junit.Test)79 ProgramState (org.sonar.java.se.ProgramState)74 BinarySymbolicValue (org.sonar.java.se.symbolicvalues.BinarySymbolicValue)55 Instruction (org.sonar.java.bytecode.cfg.Instruction)52 ObjectConstraint (org.sonar.java.se.constraint.ObjectConstraint)38 BooleanConstraint (org.sonar.java.se.constraint.BooleanConstraint)36 ProgramPoint (org.sonar.java.se.ProgramPoint)30 Constraint (org.sonar.java.se.constraint.Constraint)29 TypedConstraint (org.sonar.java.se.constraint.TypedConstraint)22 Type (org.sonar.plugins.java.api.semantic.Type)18 Symbol (org.sonar.plugins.java.api.semantic.Symbol)17 JavaSymbol (org.sonar.java.resolve.JavaSymbol)16 ConstraintsByDomain (org.sonar.java.se.constraint.ConstraintsByDomain)16 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)15 List (java.util.List)13 Collectors (java.util.stream.Collectors)11 VisibleForTesting (com.google.common.annotations.VisibleForTesting)10 Lists (com.google.common.collect.Lists)10