Search in sources :

Example 26 with Instruction

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

the class BytecodeEGWalkerExecuteTest method test_aconst_null.

@Test
public void test_aconst_null() throws Exception {
    ProgramState programState = execute(new Instruction(Opcodes.ACONST_NULL));
    assertStack(programState, ObjectConstraint.NULL);
}
Also used : ProgramState(org.sonar.java.se.ProgramState) Instruction(org.sonar.java.bytecode.cfg.Instruction) Test(org.junit.Test)

Example 27 with Instruction

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

the class BytecodeSECheckTest method zeroness_check_mul.

@Test
public void zeroness_check_mul() {
    int[] opCodes = { Opcodes.DMUL, Opcodes.FMUL, Opcodes.IMUL, Opcodes.LMUL };
    for (int mulOpCode : opCodes) {
        Instruction instruction = new Instruction(mulOpCode);
        ProgramState ps = execute(instruction, zeroZeroPs);
        SymbolicValue peekValue = ps.peekValue();
        assertThat(peekValue).isEqualTo(sv2);
        ConstraintsByDomain constraints = ps.getConstraints(peekValue);
        assertThat(constraints.get(ZeroConstraint.class)).isEqualTo(ZeroConstraint.ZERO);
        assertThat(constraints.get(BooleanConstraint.class)).isEqualTo(BooleanConstraint.FALSE);
        ps = execute(instruction, zeroNonZeroPs);
        peekValue = ps.peekValue();
        assertThat(peekValue).isEqualTo(sv1);
        constraints = ps.getConstraints(peekValue);
        assertThat(constraints.get(ZeroConstraint.class)).isEqualTo(ZeroConstraint.ZERO);
        assertThat(constraints.get(BooleanConstraint.class)).isEqualTo(BooleanConstraint.FALSE);
        ps = execute(instruction, nonZeroZeroPs);
        peekValue = ps.peekValue();
        assertThat(peekValue).isEqualTo(sv2);
        constraints = ps.getConstraints(peekValue);
        assertThat(constraints.get(ZeroConstraint.class)).isEqualTo(ZeroConstraint.ZERO);
        assertThat(constraints.get(BooleanConstraint.class)).isEqualTo(BooleanConstraint.FALSE);
        ps = execute(instruction, nonZeroNonZeroPs);
        peekValue = ps.peekValue();
        assertThat(peekValue).isNotIn(sv1, sv2);
        constraints = ps.getConstraints(peekValue);
        assertThat(constraints.get(ZeroConstraint.class)).isEqualTo(ZeroConstraint.NON_ZERO);
        assertThat(constraints.get(BooleanConstraint.class)).isNull();
        ps = execute(instruction, noConstraints);
        peekValue = ps.peekValue();
        assertThat(peekValue).isNotIn(sv1, sv2);
        constraints = ps.getConstraints(peekValue);
        assertThat(constraints.get(ZeroConstraint.class)).isNull();
        assertThat(constraints.get(BooleanConstraint.class)).isNull();
    }
}
Also used : ConstraintsByDomain(org.sonar.java.se.constraint.ConstraintsByDomain) ProgramState(org.sonar.java.se.ProgramState) Instruction(org.sonar.java.bytecode.cfg.Instruction) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) BooleanConstraint(org.sonar.java.se.constraint.BooleanConstraint) ProgramPoint(org.sonar.java.se.ProgramPoint) ZeroConstraint(org.sonar.java.se.checks.DivisionByZeroCheck.ZeroConstraint) Test(org.junit.Test)

Example 28 with Instruction

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

the class BytecodeEGWalkerExecuteTest method test_dup_x2.

@Test
public void test_dup_x2() throws Exception {
    SymbolicValue sv1 = new SymbolicValue();
    SymbolicValue sv2 = new SymbolicValue();
    SymbolicValue sv3 = new SymbolicValue();
    ProgramState programState = execute(new Instruction(Opcodes.DUP_X2), ProgramState.EMPTY_STATE.stackValue(sv3).stackValue(sv2).stackValue(sv1));
    ProgramState.Pop pop = programState.unstackValue(4);
    assertThat(pop.values).containsExactly(sv1, sv2, sv3, sv1);
    assertThat(pop.state).isEqualTo(ProgramState.EMPTY_STATE);
    assertThatThrownBy(() -> execute(new Instruction(Opcodes.DUP_X2), ProgramState.EMPTY_STATE.stackValue(sv1).stackValue(sv2))).hasMessage("DUP_X2 needs 3 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 29 with Instruction

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

the class ConstraintManagerTest method createBinarySV.

private static SymbolicValue createBinarySV(int opcode, SymbolicValue sv1, SymbolicValue sv2) {
    ConstraintManager constraintManager = new ConstraintManager();
    List<ProgramState.SymbolicValueSymbol> computedFrom = Arrays.asList(new ProgramState.SymbolicValueSymbol(sv1, null), new ProgramState.SymbolicValueSymbol(sv2, null));
    Instruction inst = new Instruction(opcode);
    return constraintManager.createBinarySymbolicValue(inst, computedFrom);
}
Also used : ProgramState(org.sonar.java.se.ProgramState) Instruction(org.sonar.java.bytecode.cfg.Instruction)

Example 30 with Instruction

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

the class BytecodeEGWalkerExecuteTest method test_array_store.

@Test
public void test_array_store() throws Exception {
    int[] storeArrayOpcodes = new int[] { Opcodes.IASTORE, Opcodes.LASTORE, Opcodes.FASTORE, Opcodes.DASTORE, Opcodes.AASTORE, Opcodes.BASTORE, Opcodes.CASTORE, Opcodes.SASTORE };
    SymbolicValue array = new SymbolicValue();
    SymbolicValue index = new SymbolicValue();
    SymbolicValue value = new SymbolicValue();
    ProgramState initState = ProgramState.EMPTY_STATE.stackValue(array).stackValue(index).stackValue(value);
    for (int opcode : storeArrayOpcodes) {
        ProgramState ps = execute(new Instruction(opcode), initState);
        assertEmptyStack(ps);
    }
}
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