use of org.sonar.java.se.ProgramState in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method test_invoke_instance_method.
@Test
public void test_invoke_instance_method() throws Exception {
int[] opcodes = new int[] { Opcodes.INVOKESPECIAL, Opcodes.INVOKEVIRTUAL, Opcodes.INVOKEINTERFACE };
for (int opcode : opcodes) {
SymbolicValue thisSv = new SymbolicValue();
ProgramState stateWithThis = ProgramState.EMPTY_STATE.stackValue(thisSv);
ProgramState programState = execute(invokeMethod(opcode, "methodWithoutArgument", "()V"), stateWithThis);
assertEmptyStack(programState);
assertThat(programState.getConstraints(thisSv).get(ObjectConstraint.class)).isEqualTo(ObjectConstraint.NOT_NULL);
programState = execute(invokeMethod(opcode, "finalVoid", "()V"), stateWithThis);
assertEmptyStack(programState);
assertThat(programState.getConstraints(thisSv).get(ObjectConstraint.class)).isEqualTo(ObjectConstraint.NOT_NULL);
programState = execute(invokeMethod(opcode, "booleanMethod", "()Z"), stateWithThis);
assertStack(programState, new Constraint[] { null });
assertThat(isDoubleOrLong(programState, programState.peekValue())).isFalse();
SymbolicValue arg = new SymbolicValue();
programState = execute(invokeMethod(opcode, "intMethodWithIntArgument", "(I)I"), stateWithThis.stackValue(arg));
assertStack(programState, new Constraint[] { null });
assertThat(programState.peekValue()).isNotEqualTo(arg);
programState = execute(invokeMethod(opcode, "methodWithIntIntArgument", "(II)V"), stateWithThis.stackValue(arg).stackValue(arg));
assertEmptyStack(programState);
assertThatThrownBy(() -> execute(invokeMethod(opcode, "methodWithIntIntArgument", "(II)V"), stateWithThis)).isInstanceOf(IllegalStateException.class);
programState = execute(invokeMethod(opcode, "returningLong", "()J"), stateWithThis);
assertThat(isDoubleOrLong(programState, programState.peekValue())).isTrue();
programState = execute(invokeMethod(opcode, "returningDouble", "()D"), stateWithThis);
assertThat(isDoubleOrLong(programState, programState.peekValue())).isTrue();
}
}
use of org.sonar.java.se.ProgramState 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");
}
}
use of org.sonar.java.se.ProgramState 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");
}
use of org.sonar.java.se.ProgramState 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);
}
use of org.sonar.java.se.ProgramState 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 } });
}
Aggregations