use of org.sonar.java.se.ProgramState in project sonar-java by SonarSource.
the class MethodYield method parametersAfterInvocation.
public Stream<ProgramState> parametersAfterInvocation(List<SymbolicValue> invocationArguments, List<Type> invocationTypes, ProgramState programState) {
Set<ProgramState> results = new LinkedHashSet<>();
for (int index = 0; index < invocationArguments.size(); index++) {
ConstraintsByDomain constraints = getConstraint(index, invocationTypes);
if (constraints == null) {
// no constraints on this parameter, let's try next one.
continue;
}
SymbolicValue invokedArg = invocationArguments.get(index);
Set<ProgramState> programStates = programStatesForConstraint(results.isEmpty() ? Lists.newArrayList(programState) : results, invokedArg, constraints);
if (programStates.isEmpty()) {
// TODO there might be some issue to report in this case.
return Stream.empty();
}
results = programStates;
}
// That means that this yield is still possible and we need to stack a returned SV with its eventual constraints.
if (results.isEmpty()) {
results.add(programState);
}
return results.stream();
}
use of org.sonar.java.se.ProgramState in project sonar-java by SonarSource.
the class MethodYield method programStatesForConstraint.
private static Set<ProgramState> programStatesForConstraint(Collection<ProgramState> states, SymbolicValue invokedArg, ConstraintsByDomain constraints) {
Set<ProgramState> programStates = new LinkedHashSet<>(states);
constraints.forEach((d, c) -> {
Set<ProgramState> newPs = new LinkedHashSet<>();
for (ProgramState programState : programStates) {
newPs.addAll(invokedArg.setConstraint(programState, c));
}
programStates.clear();
programStates.addAll(newPs);
});
return programStates;
}
use of org.sonar.java.se.ProgramState 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);
}
}
use of org.sonar.java.se.ProgramState in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method test_load.
@Test
public void test_load() throws Exception {
int[] loadRefOpcodes = new int[] { Opcodes.ILOAD, Opcodes.LLOAD, Opcodes.FLOAD, Opcodes.DLOAD, Opcodes.ALOAD };
for (int loadRefOpcode : loadRefOpcodes) {
SymbolicValue loadRef = new SymbolicValue();
ProgramState programState = execute(new Instruction(loadRefOpcode, 0), ProgramState.EMPTY_STATE.put(0, loadRef));
assertThat(programState.peekValue()).isEqualTo(loadRef);
// no SV indexed should failed
assertThatThrownBy(() -> execute(new Instruction(loadRefOpcode, 0), ProgramState.EMPTY_STATE)).hasMessage("Loading a symbolic value unindexed");
}
}
use of org.sonar.java.se.ProgramState in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method test_lconst.
@Test
public void test_lconst() throws Exception {
ProgramState programState = execute(new Instruction(Opcodes.LCONST_0));
assertStack(programState, new Constraint[][] { { DivisionByZeroCheck.ZeroConstraint.ZERO, BooleanConstraint.FALSE, ObjectConstraint.NOT_NULL } });
programState = execute(new Instruction(Opcodes.LCONST_1));
assertStack(programState, new Constraint[][] { { DivisionByZeroCheck.ZeroConstraint.NON_ZERO, BooleanConstraint.TRUE, ObjectConstraint.NOT_NULL } });
}
Aggregations