Search in sources :

Example 1 with Instruction

use of org.sonar.java.bytecode.cfg.Instruction 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)

Example 2 with Instruction

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

the class BytecodeEGWalkerExecuteTest method test_instanceof.

@Test
public void test_instanceof() throws Exception {
    SymbolicValue sv = new SymbolicValue();
    ProgramState programState = execute(new Instruction(Opcodes.INSTANCEOF), ProgramState.EMPTY_STATE.stackValue(sv));
    SymbolicValue result = programState.peekValue();
    assertThat(result).isInstanceOf(SymbolicValue.InstanceOfSymbolicValue.class);
    assertThat(result.computedFrom().get(0)).isEqualTo(sv);
    assertThatThrownBy(() -> execute(new Instruction(Opcodes.INSTANCEOF))).hasMessage("INSTANCEOF 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) Test(org.junit.Test)

Example 3 with Instruction

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

the class BytecodeEGWalkerExecuteTest method test_dup2_x2_form3.

@Test
public void test_dup2_x2_form3() throws Exception {
    SymbolicValue sv1 = new SymbolicValue();
    SymbolicValue sv2 = new SymbolicValue();
    SymbolicValue sv3 = new SymbolicValue();
    SymbolicValue sv4 = new SymbolicValue();
    ProgramState startingState = ProgramState.EMPTY_STATE.stackValue(sv4).stackValue(sv3).stackValue(sv2).stackValue(sv1);
    startingState = setDoubleOrLong(startingState, sv3, true);
    ProgramState programState = execute(new Instruction(Opcodes.DUP2_X2), startingState);
    ProgramState.Pop pop = programState.unstackValue(6);
    assertThat(pop.values).containsExactly(sv1, sv2, sv3, sv1, sv2, sv4);
}
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 4 with Instruction

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

the class BytecodeEGWalkerExecuteTest method test_dconst.

@Test
public void test_dconst() throws Exception {
    ProgramState programState = execute(new Instruction(Opcodes.DCONST_0));
    assertStack(programState, new Constraint[][] { { DivisionByZeroCheck.ZeroConstraint.ZERO, BooleanConstraint.FALSE, ObjectConstraint.NOT_NULL } });
    programState = execute(new Instruction(Opcodes.DCONST_1));
    assertStack(programState, new Constraint[][] { { DivisionByZeroCheck.ZeroConstraint.NON_ZERO, BooleanConstraint.TRUE, ObjectConstraint.NOT_NULL } });
}
Also used : ProgramState(org.sonar.java.se.ProgramState) Instruction(org.sonar.java.bytecode.cfg.Instruction) Test(org.junit.Test)

Example 5 with Instruction

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

the class BytecodeEGWalkerExecuteTest method test_pop2_long_double.

@Test
public void test_pop2_long_double() throws Exception {
    SymbolicValue normalSv = new SymbolicValue();
    SymbolicValue longSv = new SymbolicValue();
    ProgramState startingState = ProgramState.EMPTY_STATE.stackValue(normalSv).stackValue(longSv);
    startingState = setDoubleOrLong(startingState, longSv, true);
    ProgramState programState = execute(new Instruction(Opcodes.POP2), startingState);
    assertThat(programState.peekValue()).isEqualTo(normalSv);
}
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)

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