Search in sources :

Example 16 with Instruction

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

the class BytecodeEGWalkerExecuteTest method test_dup2_x1_long_double.

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

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

the class BytecodeEGWalkerExecuteTest method test_putstatic.

@Test
public void test_putstatic() throws Exception {
    ProgramState programState = execute(new Instruction(Opcodes.PUTSTATIC), ProgramState.EMPTY_STATE.stackValue(new SymbolicValue()));
    assertThat(programState.peekValue()).isNull();
}
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 18 with Instruction

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

the class BytecodeEGWalkerExecuteTest method test_lookupswitch.

@Test
public void test_lookupswitch() throws Exception {
    Instructions instr = new Instructions();
    instr.visitVarInsn(ILOAD, 0);
    Label l0 = new Label();
    Label l1 = new Label();
    Label l2 = new Label();
    Label l3 = new Label();
    instr.visitLookupSwitchInsn(l3, new int[] { 0, 1, 2, 50 }, new Label[] { l0, l1, l2, l3 });
    instr.visitLabel(l0);
    instr.visitInsn(ICONST_0);
    instr.visitVarInsn(ISTORE, 1);
    instr.visitJumpInsn(GOTO, l3);
    instr.visitLabel(l1);
    instr.visitInsn(ICONST_0);
    instr.visitVarInsn(ISTORE, 2);
    instr.visitJumpInsn(GOTO, l3);
    instr.visitLabel(l2);
    instr.visitInsn(ICONST_0);
    instr.visitVarInsn(ISTORE, 3);
    instr.visitJumpInsn(GOTO, l3);
    instr.visitLabel(l3);
    instr.visitInsn(RETURN);
    BytecodeCFG cfg = instr.cfg();
    CFG.IBlock<Instruction> entry = cfg.entry();
    BytecodeEGWalker walker = new BytecodeEGWalker(null, null);
    walker.programState = ProgramState.EMPTY_STATE.stackValue(new SymbolicValue());
    walker.handleBlockExit(new ProgramPoint(entry));
    assertThat(walker.workList).hasSize(entry.successors().size());
    walker.workList.forEach(node -> {
        assertThat(node.programState.peekValue()).isNull();
        assertThat(entry.successors().contains(node.programPoint.block)).isTrue();
    });
}
Also used : BytecodeCFG(org.sonar.java.bytecode.cfg.BytecodeCFG) CFG(org.sonar.java.cfg.CFG) ProgramPoint(org.sonar.java.se.ProgramPoint) BytecodeCFG(org.sonar.java.bytecode.cfg.BytecodeCFG) Label(org.objectweb.asm.Label) Instructions(org.sonar.java.bytecode.cfg.Instructions) 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 19 with Instruction

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

the class BytecodeEGWalkerExecuteTest method test_pop2.

@Test
public void test_pop2() throws Exception {
    SymbolicValue sv1 = new SymbolicValue();
    SymbolicValue sv2 = new SymbolicValue();
    SymbolicValue sv3 = new SymbolicValue();
    ProgramState programState = execute(new Instruction(Opcodes.POP2), ProgramState.EMPTY_STATE.stackValue(sv1).stackValue(sv2).stackValue(sv3));
    assertThat(programState.peekValue()).isEqualTo(sv1);
    assertThatThrownBy(() -> execute(new Instruction(Opcodes.POP2))).hasMessage("POP2 on empty 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 20 with Instruction

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

the class BytecodeEGWalkerExecuteTest method test_compare_with_null.

@Test
public void test_compare_with_null() {
    SymbolicValue sv = new SymbolicValue();
    int[] opcodes = { Opcodes.IFNULL, Opcodes.IFNONNULL };
    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()).isSameAs(SymbolicValue.NULL_LITERAL);
    }
}
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)

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