Search in sources :

Example 16 with Constraint

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

the class BytecodeEGWalkerExecuteTest method exceptional_paths_should_be_enqueued.

@Test
public void exceptional_paths_should_be_enqueued() {
    MethodBehavior mb = walker.getMethodBehavior(BytecodeEGWalkerExecuteTest.class.getCanonicalName() + "#enqueue_exceptional_paths(Lorg/sonar/java/bytecode/se/BytecodeEGWalkerExecuteTest;)Ljava/lang/Object;", squidClassLoader);
    assertThat(mb.yields()).hasSize(2);
    List<Constraint> resultConstraints = mb.yields().stream().map(y -> ((HappyPathYield) y).resultConstraint()).map(c -> c.get(ObjectConstraint.class)).collect(Collectors.toList());
    assertThat(resultConstraints).contains(ObjectConstraint.NOT_NULL, ObjectConstraint.NULL);
}
Also used : ASTORE(org.objectweb.asm.Opcodes.ASTORE) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SETestUtils(org.sonar.java.se.SETestUtils) DMUL(org.objectweb.asm.Opcodes.DMUL) LSUB(org.objectweb.asm.Opcodes.LSUB) MethodYield(org.sonar.java.se.xproc.MethodYield) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) LSHR(org.objectweb.asm.Opcodes.LSHR) DSUB(org.objectweb.asm.Opcodes.DSUB) LMUL(org.objectweb.asm.Opcodes.LMUL) Mockito.doThrow(org.mockito.Mockito.doThrow) LSHL(org.objectweb.asm.Opcodes.LSHL) BooleanConstraint(org.sonar.java.se.constraint.BooleanConstraint) TypedConstraint(org.sonar.java.se.constraint.TypedConstraint) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) Collectors(java.util.stream.Collectors) BinarySymbolicValue(org.sonar.java.se.symbolicvalues.BinarySymbolicValue) LAND(org.objectweb.asm.Opcodes.LAND) List(java.util.List) LXOR(org.objectweb.asm.Opcodes.LXOR) Instruction(org.sonar.java.bytecode.cfg.Instruction) GOTO(org.objectweb.asm.Opcodes.GOTO) Constraint(org.sonar.java.se.constraint.Constraint) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) LUSHR(org.objectweb.asm.Opcodes.LUSHR) BeforeClass(org.junit.BeforeClass) ProgramState(org.sonar.java.se.ProgramState) Label(org.objectweb.asm.Label) Mockito.spy(org.mockito.Mockito.spy) ConstraintsByDomain(org.sonar.java.se.constraint.ConstraintsByDomain) ArrayList(java.util.ArrayList) Instructions(org.sonar.java.bytecode.cfg.Instructions) DivisionByZeroCheck(org.sonar.java.se.checks.DivisionByZeroCheck) ISTORE(org.objectweb.asm.Opcodes.ISTORE) INVOKESTATIC(org.objectweb.asm.Opcodes.INVOKESTATIC) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) BytecodeCFG(org.sonar.java.bytecode.cfg.BytecodeCFG) Before(org.junit.Before) JavaParser(org.sonar.java.ast.parser.JavaParser) LOR(org.objectweb.asm.Opcodes.LOR) ICONST_4(org.objectweb.asm.Opcodes.ICONST_4) Opcodes(org.objectweb.asm.Opcodes) DREM(org.objectweb.asm.Opcodes.DREM) ICONST_3(org.objectweb.asm.Opcodes.ICONST_3) ICONST_2(org.objectweb.asm.Opcodes.ICONST_2) ICONST_1(org.objectweb.asm.Opcodes.ICONST_1) ICONST_0(org.objectweb.asm.Opcodes.ICONST_0) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) DADD(org.objectweb.asm.Opcodes.DADD) Type(org.sonar.plugins.java.api.semantic.Type) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue) File(java.io.File) LADD(org.objectweb.asm.Opcodes.LADD) CFG(org.sonar.java.cfg.CFG) Printer(org.objectweb.asm.util.Printer) RETURN(org.objectweb.asm.Opcodes.RETURN) SemanticModel(org.sonar.java.resolve.SemanticModel) LREM(org.objectweb.asm.Opcodes.LREM) BehaviorCache(org.sonar.java.se.xproc.BehaviorCache) ILOAD(org.objectweb.asm.Opcodes.ILOAD) ProgramPoint(org.sonar.java.se.ProgramPoint) Preconditions(com.google.common.base.Preconditions) DDIV(org.objectweb.asm.Opcodes.DDIV) Collections(java.util.Collections) HappyPathYield(org.sonar.java.se.xproc.HappyPathYield) LDIV(org.objectweb.asm.Opcodes.LDIV) 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) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) Test(org.junit.Test)

Example 17 with Constraint

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

the class BytecodeEGWalkerExecuteTest method assertBinarySymbolicValue.

private void assertBinarySymbolicValue(int[] opcodes, Class<? extends BinarySymbolicValue> binarySvClass) {
    SymbolicValue sv1 = new SymbolicValue();
    SymbolicValue sv2 = new SymbolicValue();
    ProgramState initState = ProgramState.EMPTY_STATE.stackValue(sv2).stackValue(sv1);
    for (int opcode : opcodes) {
        ProgramState programState = execute(new Instruction(opcode), initState);
        ProgramState.Pop pop = programState.unstackValue(1);
        assertStack(programState, new Constraint[][] { { ObjectConstraint.NOT_NULL } });
        SymbolicValue result = pop.values.get(0);
        assertThat(result).isNotEqualTo(sv1);
        assertThat(result).isNotEqualTo(sv2);
        assertThat(result).isInstanceOf(binarySvClass);
        assertThat(isDoubleOrLong(programState, result)).isEqualTo(LONG_OPCODE.contains(opcode));
        BinarySymbolicValue andSv = (BinarySymbolicValue) result;
        assertThat(andSv.getRightOp()).isEqualTo(sv1);
        assertThat(andSv.getLeftOp()).isEqualTo(sv2);
    }
}
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) BinarySymbolicValue(org.sonar.java.se.symbolicvalues.BinarySymbolicValue)

Example 18 with Constraint

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

the class BytecodeEGWalkerExecuteTest method test_iconst.

@Test
public void test_iconst() throws Exception {
    ProgramState programState = execute(new Instruction(Opcodes.ICONST_0));
    assertStack(programState, new Constraint[][] { { DivisionByZeroCheck.ZeroConstraint.ZERO, BooleanConstraint.FALSE, ObjectConstraint.NOT_NULL } });
    programState = execute(new Instruction(Opcodes.ICONST_1));
    assertStack(programState, new Constraint[][] { { DivisionByZeroCheck.ZeroConstraint.NON_ZERO, BooleanConstraint.TRUE, ObjectConstraint.NOT_NULL } });
    int[] opCodesConst = new int[] { Opcodes.ICONST_M1, Opcodes.ICONST_2, Opcodes.ICONST_3, Opcodes.ICONST_4, Opcodes.ICONST_5 };
    for (int opcode : opCodesConst) {
        programState = execute(new Instruction(opcode));
        assertStack(programState, new Constraint[][] { { DivisionByZeroCheck.ZeroConstraint.NON_ZERO, ObjectConstraint.NOT_NULL } });
    }
}
Also used : ProgramState(org.sonar.java.se.ProgramState) Instruction(org.sonar.java.bytecode.cfg.Instruction) 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 19 with Constraint

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

the class BytecodeEGWalkerExecuteTest method assertConsume2produceNotNull.

private void assertConsume2produceNotNull(int... opcodes) {
    SymbolicValue sv1 = new SymbolicValue();
    SymbolicValue sv2 = new SymbolicValue();
    ProgramState initState = ProgramState.EMPTY_STATE.stackValue(sv2).stackValue(sv1);
    for (int opcode : opcodes) {
        ProgramState programState = execute(new Instruction(opcode), initState);
        ProgramState.Pop pop = programState.unstackValue(1);
        assertStack(programState, new Constraint[][] { { ObjectConstraint.NOT_NULL } });
        SymbolicValue result = pop.values.get(0);
        assertThat(result).isNotEqualTo(sv1);
        assertThat(result).isNotEqualTo(sv2);
        assertThat(isDoubleOrLong(programState, result)).isEqualTo(LONG_OPCODE.contains(opcode));
    }
}
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)

Example 20 with Constraint

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

the class MethodBehaviorTest method result_with_boolean_constraint_should_be_reduced.

@Test
public void result_with_boolean_constraint_should_be_reduced() {
    MethodBehavior mb = new MethodBehavior("foo()Z");
    addYield(mb, BooleanConstraint.TRUE);
    addYield(mb, BooleanConstraint.FALSE);
    mb.completed();
    assertThat(mb.yields()).hasSize(1);
    assertThat(((HappyPathYield) mb.yields().get(0)).resultConstraint()).isNull();
    mb = new MethodBehavior("foo()Z");
    addYield(mb, BooleanConstraint.TRUE, ObjectConstraint.NULL);
    addYield(mb, BooleanConstraint.FALSE, ObjectConstraint.NOT_NULL);
    mb.completed();
    assertThat(mb.yields()).hasSize(2);
    List<Constraint> resultConstraints = mb.yields().stream().map(y -> ((HappyPathYield) y).resultConstraint().get(BooleanConstraint.class)).collect(Collectors.toList());
    assertThat(resultConstraints).contains(BooleanConstraint.TRUE, BooleanConstraint.FALSE);
}
Also used : ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) SETestUtils.createSymbolicExecutionVisitor(org.sonar.java.se.SETestUtils.createSymbolicExecutionVisitor) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) Collectors(java.util.stream.Collectors) ConstraintsByDomain(org.sonar.java.se.constraint.ConstraintsByDomain) SETestUtils.getMethodBehavior(org.sonar.java.se.SETestUtils.getMethodBehavior) List(java.util.List) Pair(org.sonar.java.se.Pair) SemanticModel(org.sonar.java.resolve.SemanticModel) SymbolicExecutionVisitor(org.sonar.java.se.SymbolicExecutionVisitor) SETestUtils.createSymbolicExecutionVisitorAndSemantic(org.sonar.java.se.SETestUtils.createSymbolicExecutionVisitorAndSemantic) BooleanConstraint(org.sonar.java.se.constraint.BooleanConstraint) Constraint(org.sonar.java.se.constraint.Constraint) Nullable(javax.annotation.Nullable) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) BooleanConstraint(org.sonar.java.se.constraint.BooleanConstraint) Constraint(org.sonar.java.se.constraint.Constraint) SETestUtils.getMethodBehavior(org.sonar.java.se.SETestUtils.getMethodBehavior) 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