Search in sources :

Example 61 with ProgramState

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();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ConstraintsByDomain(org.sonar.java.se.constraint.ConstraintsByDomain) ProgramState(org.sonar.java.se.ProgramState) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) Constraint(org.sonar.java.se.constraint.Constraint)

Example 62 with ProgramState

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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ProgramState(org.sonar.java.se.ProgramState)

Example 63 with ProgramState

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);
    }
}
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 64 with ProgramState

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");
    }
}
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 65 with ProgramState

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 } });
}
Also used : ProgramState(org.sonar.java.se.ProgramState) Instruction(org.sonar.java.bytecode.cfg.Instruction) 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