Search in sources :

Example 6 with Instruction

use of org.sonar.java.bytecode.cfg.Instruction in project sonar-java by SonarSource.

the class BytecodeEGWalkerExecuteTest method test_getstatic.

@Test
public void test_getstatic() throws Exception {
    ProgramState programState = execute(new Instruction(Opcodes.GETSTATIC, new Instruction.FieldOrMethod("", "", "D", false)));
    assertThat(programState.peekValue()).isNotNull();
    assertThat(isDoubleOrLong(programState, programState.peekValue())).isTrue();
    programState = execute(new Instruction(Opcodes.GETSTATIC, new Instruction.FieldOrMethod("", "", "I", false)));
    assertThat(programState.peekValue()).isNotNull();
    assertThat(isDoubleOrLong(programState, programState.peekValue())).isFalse();
}
Also used : ProgramState(org.sonar.java.se.ProgramState) Instruction(org.sonar.java.bytecode.cfg.Instruction) Test(org.junit.Test)

Example 7 with Instruction

use of org.sonar.java.bytecode.cfg.Instruction in project sonar-java by SonarSource.

the class BytecodeEGWalkerExecuteTest method test_return.

@Test
public void test_return() throws Exception {
    ProgramState programState = execute(new Instruction(Opcodes.RETURN), ProgramState.EMPTY_STATE);
    assertThat(programState.peekValue()).isNull();
    assertThat(programState.exitValue()).isNull();
}
Also used : ProgramState(org.sonar.java.se.ProgramState) Instruction(org.sonar.java.bytecode.cfg.Instruction) Test(org.junit.Test)

Example 8 with Instruction

use of org.sonar.java.bytecode.cfg.Instruction in project sonar-java by SonarSource.

the class BytecodeEGWalkerExecuteTest method test_compare_with_zero.

@Test
public void test_compare_with_zero() {
    SymbolicValue sv = new SymbolicValue();
    int[] opcodes = { Opcodes.IFEQ, Opcodes.IFNE, Opcodes.IFLT, Opcodes.IFGE };
    for (int opcode : opcodes) {
        ProgramState programState = walker.branchingState(new Instruction(opcode), ProgramState.EMPTY_STATE.stackValue(sv));
        RelationalSymbolicValue relSV = (RelationalSymbolicValue) programState.peekValue();
        assertThat(relSV.getLeftOp()).isSameAs(sv);
        assertThat(relSV.getRightOp()).isNotSameAs(sv);
        assertThat(programState.getConstraints(relSV.getRightOp()).hasConstraint(DivisionByZeroCheck.ZeroConstraint.ZERO)).isTrue();
    }
    // these opcodes inverse operator and swap operands
    int[] swapOperandsOpcodes = { Opcodes.IFLE, Opcodes.IFGT };
    for (int opcode : swapOperandsOpcodes) {
        ProgramState programState = walker.branchingState(new Instruction(opcode), ProgramState.EMPTY_STATE.stackValue(sv));
        RelationalSymbolicValue relSV = (RelationalSymbolicValue) programState.peekValue();
        assertThat(relSV.getRightOp()).isSameAs(sv);
        assertThat(relSV.getLeftOp()).isNotSameAs(sv);
        assertThat(programState.getConstraints(relSV.getLeftOp()).hasConstraint(DivisionByZeroCheck.ZeroConstraint.ZERO)).isTrue();
    }
}
Also used : ProgramState(org.sonar.java.se.ProgramState) Instruction(org.sonar.java.bytecode.cfg.Instruction) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue) 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 9 with Instruction

use of org.sonar.java.bytecode.cfg.Instruction in project sonar-java by SonarSource.

the class BytecodeEGWalkerExecuteTest method test_dup2_long_double.

@Test
public void test_dup2_long_double() throws Exception {
    SymbolicValue longSv = new SymbolicValue();
    SymbolicValue another = new SymbolicValue();
    ProgramState startingState = ProgramState.EMPTY_STATE.stackValue(another).stackValue(longSv);
    startingState = setDoubleOrLong(startingState, longSv, true);
    ProgramState programState = execute(new Instruction(Opcodes.DUP2), startingState);
    ProgramState.Pop pop = programState.unstackValue(4);
    assertThat(pop.values).containsExactly(longSv, longSv, another);
}
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) Test(org.junit.Test)

Example 10 with Instruction

use of org.sonar.java.bytecode.cfg.Instruction in project sonar-java by SonarSource.

the class BytecodeEGWalkerExecuteTest method test_store.

@Test
public void test_store() throws Exception {
    int[] storeOpcodes = new int[] { Opcodes.ISTORE, Opcodes.LSTORE, Opcodes.FSTORE, Opcodes.DSTORE, Opcodes.ASTORE };
    SymbolicValue sv = new SymbolicValue();
    ProgramState startState = ProgramState.EMPTY_STATE.stackValue(sv);
    for (int opcode : storeOpcodes) {
        ProgramState programState = execute(new Instruction(opcode, 67), startState);
        assertThat(programState.getValue(67)).isEqualTo(sv);
    }
}
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

Instruction (org.sonar.java.bytecode.cfg.Instruction)62 ProgramState (org.sonar.java.se.ProgramState)58 Test (org.junit.Test)55 SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)48 RelationalSymbolicValue (org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)43 BinarySymbolicValue (org.sonar.java.se.symbolicvalues.BinarySymbolicValue)40 ProgramPoint (org.sonar.java.se.ProgramPoint)26 BooleanConstraint (org.sonar.java.se.constraint.BooleanConstraint)23 TypedConstraint (org.sonar.java.se.constraint.TypedConstraint)20 Constraint (org.sonar.java.se.constraint.Constraint)18 ObjectConstraint (org.sonar.java.se.constraint.ObjectConstraint)18 BytecodeCFG (org.sonar.java.bytecode.cfg.BytecodeCFG)6 ZeroConstraint (org.sonar.java.se.checks.DivisionByZeroCheck.ZeroConstraint)5 ConstraintsByDomain (org.sonar.java.se.constraint.ConstraintsByDomain)5 VisibleForTesting (com.google.common.annotations.VisibleForTesting)4 Type (org.sonar.plugins.java.api.semantic.Type)4 BehaviorCache (org.sonar.java.se.xproc.BehaviorCache)3 MethodBehavior (org.sonar.java.se.xproc.MethodBehavior)3 Preconditions (com.google.common.base.Preconditions)2 ImmutableList (com.google.common.collect.ImmutableList)2