Search in sources :

Example 76 with ProgramState

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

the class BytecodeEGWalkerExecuteTest method test_dup2_x2_form4.

@Test
public void test_dup2_x2_form4() 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, sv1, true);
    startingState = setDoubleOrLong(startingState, sv2, true);
    ProgramState programState = execute(new Instruction(Opcodes.DUP2_X2), startingState);
    ProgramState.Pop pop = programState.unstackValue(6);
    assertThat(pop.values).containsExactly(sv1, sv2, sv1, sv3, 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 77 with ProgramState

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

the class BytecodeEGWalkerExecuteTest method test_sipush.

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

Example 78 with ProgramState

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

the class BytecodeEGWalkerExecuteTest method test_invoke_static.

@Test
public void test_invoke_static() throws Exception {
    ProgramState programState = execute(invokeStatic("staticMethod", "()V"));
    assertEmptyStack(programState);
    programState = execute(invokeStatic("staticBooleanMethod", "()Z"));
    assertStack(programState, new Constraint[][] { { ObjectConstraint.NOT_NULL, BooleanConstraint.FALSE } });
    assertThat(isDoubleOrLong(programState, programState.peekValue())).isFalse();
    SymbolicValue arg = new SymbolicValue();
    programState = execute(invokeStatic("staticIntMethodWithIntArgument", "(I)I"), ProgramState.EMPTY_STATE.stackValue(arg));
    assertStack(programState, new Constraint[][] { { ObjectConstraint.NOT_NULL, DivisionByZeroCheck.ZeroConstraint.ZERO } });
    assertThat(programState.peekValue()).isNotEqualTo(arg);
    assertThat(isDoubleOrLong(programState, programState.peekValue())).isFalse();
    programState = execute(invokeStatic("staticMethodWithIntIntArgument", "(II)V"), ProgramState.EMPTY_STATE.stackValue(arg).stackValue(arg));
    assertEmptyStack(programState);
    programState = execute(invokeStatic("staticMethodWithIntIntArgument", "(II)V"), ProgramState.EMPTY_STATE.stackValue(arg).stackValue(arg));
    assertEmptyStack(programState);
    programState = execute(invokeStatic("staticReturningLong", "()J"), ProgramState.EMPTY_STATE);
    assertThat(isDoubleOrLong(programState, programState.peekValue())).isTrue();
    programState = execute(invokeStatic("staticReturningDouble", "()D"), ProgramState.EMPTY_STATE);
    assertThat(isDoubleOrLong(programState, programState.peekValue())).isTrue();
    assertThatThrownBy(() -> execute(invokeStatic("staticBooleanMethodWithIntArgument", "(I)V"))).hasMessage("Arguments mismatch for INVOKE");
}
Also used : ProgramState(org.sonar.java.se.ProgramState) 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 79 with ProgramState

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

the class BytecodeEGWalkerExecuteTest method execute.

private ProgramState execute(Instruction instruction, ProgramState startingState) {
    CFG.IBlock block = mock(CFG.IBlock.class);
    when(block.successors()).thenReturn(Collections.emptySet());
    walker.programPosition = new ProgramPoint(block);
    walker.programState = startingState;
    walker.workList.clear();
    walker.executeInstruction(instruction);
    ProgramState programState = walker.programState;
    // X-PROC will enqueue new nodes
    if (instruction.isInvoke() && !walker.workList.isEmpty()) {
        programState = walker.workList.getFirst().programState;
    }
    return programState;
}
Also used : BytecodeCFG(org.sonar.java.bytecode.cfg.BytecodeCFG) CFG(org.sonar.java.cfg.CFG) ProgramPoint(org.sonar.java.se.ProgramPoint) ProgramState(org.sonar.java.se.ProgramState)

Example 80 with ProgramState

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

the class BytecodeEGWalkerExecuteTest method test_nullness_check.

@Test
public void test_nullness_check() throws Exception {
    SymbolicValue thisSv = new SymbolicValue();
    ProgramState startingState = ProgramState.EMPTY_STATE.stackValue(thisSv);
    ProgramState programState = execute(invokeMethod(Opcodes.INVOKESPECIAL, "methodWithoutArgument", "()V"), startingState);
    assertThat(hasConstraint(thisSv, programState, ObjectConstraint.NOT_NULL)).isTrue();
    programState = execute(invokeStatic("staticBooleanMethod", "()Z"), startingState);
    assertStack(programState, new Constraint[][] { { ObjectConstraint.NOT_NULL, BooleanConstraint.FALSE }, { null } });
    programState = execute(invokeMethod(Opcodes.INVOKESPECIAL, "methodWithIntArgument", "(I)V"), startingState.stackValue(new SymbolicValue()));
    assertThat(hasConstraint(thisSv, programState, ObjectConstraint.NOT_NULL)).isTrue();
}
Also used : ProgramState(org.sonar.java.se.ProgramState) 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

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