Search in sources :

Example 71 with ProgramState

use of org.sonar.java.se.ProgramState 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 72 with ProgramState

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

the class BytecodeEGWalkerExecuteTest method test_array_load.

@Test
public void test_array_load() throws Exception {
    int[] loadRefOpcodes = new int[] { Opcodes.IALOAD, Opcodes.LALOAD, Opcodes.FALOAD, Opcodes.DALOAD, Opcodes.AALOAD, Opcodes.BALOAD, Opcodes.CALOAD, Opcodes.SALOAD };
    SymbolicValue array = new SymbolicValue();
    SymbolicValue index = new SymbolicValue();
    ProgramState initState = ProgramState.EMPTY_STATE.stackValue(array).stackValue(index);
    for (int opcode : loadRefOpcodes) {
        ProgramState programState = execute(new Instruction(opcode), initState);
        if (opcode != Opcodes.AALOAD) {
            assertStack(programState, ObjectConstraint.NOT_NULL);
        }
        ProgramState.Pop result = programState.unstackValue(1);
        assertThat(result.values).hasSize(1);
        assertThat(result.values.get(0)).isNotEqualTo(array);
        assertThat(result.values.get(0)).isNotEqualTo(index);
        if (opcode == Opcodes.DALOAD || opcode == Opcodes.LALOAD) {
            assertThat(isDoubleOrLong(programState, result.values.get(0))).isTrue();
        }
    }
}
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 73 with ProgramState

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

the class BytecodeEGWalkerExecuteTest method test_dup_x2_long_double.

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

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

the class BytecodeEGWalkerExecuteTest method test_multianewarray.

@Test
public void test_multianewarray() throws Exception {
    ProgramState programState = execute(new Instruction.MultiANewArrayInsn("B", 1), ProgramState.EMPTY_STATE.stackValue(new SymbolicValue()));
    assertStack(programState, ObjectConstraint.NOT_NULL);
    programState = execute(new Instruction.MultiANewArrayInsn("B", 2), ProgramState.EMPTY_STATE.stackValue(new SymbolicValue()).stackValue(new SymbolicValue()));
    assertStack(programState, ObjectConstraint.NOT_NULL);
    assertThatThrownBy(() -> execute(new Instruction.MultiANewArrayInsn("B", 2))).hasMessage("MULTIANEWARRAY needs 2 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 75 with ProgramState

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

the class BytecodeEGWalkerExecuteTest method test_newarray.

@Test
public void test_newarray() throws Exception {
    SymbolicValue size = new SymbolicValue();
    int[] opcodes = { Opcodes.NEWARRAY, Opcodes.ANEWARRAY };
    for (int opcode : opcodes) {
        ProgramState programState = execute(new Instruction(opcode), ProgramState.EMPTY_STATE.stackValue(size));
        assertThat(programState.peekValue()).isNotEqualTo(size);
        assertStack(programState, ObjectConstraint.NOT_NULL);
        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

ProgramState (org.sonar.java.se.ProgramState)105 SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)72 Test (org.junit.Test)71 Instruction (org.sonar.java.bytecode.cfg.Instruction)61 RelationalSymbolicValue (org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)52 BinarySymbolicValue (org.sonar.java.se.symbolicvalues.BinarySymbolicValue)45 BooleanConstraint (org.sonar.java.se.constraint.BooleanConstraint)33 ObjectConstraint (org.sonar.java.se.constraint.ObjectConstraint)32 ProgramPoint (org.sonar.java.se.ProgramPoint)30 Constraint (org.sonar.java.se.constraint.Constraint)30 TypedConstraint (org.sonar.java.se.constraint.TypedConstraint)25 ConstraintsByDomain (org.sonar.java.se.constraint.ConstraintsByDomain)11 List (java.util.List)9 Collectors (java.util.stream.Collectors)8 Type (org.sonar.plugins.java.api.semantic.Type)8 VisibleForTesting (com.google.common.annotations.VisibleForTesting)7 ExplodedGraph (org.sonar.java.se.ExplodedGraph)7 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)7 Lists (com.google.common.collect.Lists)6 ArrayList (java.util.ArrayList)6