use of org.sonar.java.se.symbolicvalues.SymbolicValue in project sonar-java by SonarSource.
the class MethodBehavior method addParameterConstraints.
private void addParameterConstraints(ExplodedGraph.Node node, MethodYield yield) {
// add the constraints on all the parameters
int index = 0;
for (SymbolicValue parameter : parameters) {
ConstraintsByDomain constraints = node.programState.getConstraints(parameter);
if (constraints == null) {
constraints = ConstraintsByDomain.empty();
} else {
// cleanup based on signature
org.objectweb.asm.Type[] argumentTypes = org.objectweb.asm.Type.getArgumentTypes(signature.substring(signature.indexOf('(')));
constraints = cleanup(constraints, argumentTypes[index]);
}
yield.parametersConstraints.add(constraints);
index++;
}
}
use of org.sonar.java.se.symbolicvalues.SymbolicValue in project sonar-java by SonarSource.
the class BytecodeEGWalkerTest method generateMethodBehavior.
@Test
public void generateMethodBehavior() throws Exception {
MethodBehavior methodBehavior = getMethodBehavior("fun(ZLjava/lang/Object;)Ljava/lang/Object;");
assertThat(methodBehavior.yields()).hasSize(2);
SymbolicValue svFirstArg = new SymbolicValue();
SymbolicValue svsecondArg = new SymbolicValue();
SymbolicValue svResult = new SymbolicValue();
List<SymbolicValue> invocationArguments = Lists.newArrayList(svFirstArg, svsecondArg);
List<ObjectConstraint> collect = methodBehavior.yields().stream().map(my -> {
Collection<ProgramState> ps = my.statesAfterInvocation(invocationArguments, Lists.newArrayList(), ProgramState.EMPTY_STATE, () -> svResult).collect(Collectors.toList());
assertThat(ps).hasSize(1);
ProgramState next = ps.iterator().next();
return next.getConstraint(svResult, ObjectConstraint.class);
}).collect(Collectors.toList());
assertThat(collect).hasSize(2).containsOnly(ObjectConstraint.NOT_NULL, ObjectConstraint.NULL);
List<HappyPathYield> nullConstraintOnResult = methodBehavior.happyPathYields().filter(my -> ObjectConstraint.NULL.equals(my.resultConstraint().get(ObjectConstraint.class))).collect(Collectors.toList());
assertThat(nullConstraintOnResult).hasSize(1);
HappyPathYield nullConstraintResult = nullConstraintOnResult.get(0);
Collection<ProgramState> ps = nullConstraintResult.statesAfterInvocation(invocationArguments, Lists.newArrayList(), ProgramState.EMPTY_STATE, () -> svResult).collect(Collectors.toList());
assertThat(ps).hasSize(1);
ObjectConstraint constraint = ps.iterator().next().getConstraint(svsecondArg, ObjectConstraint.class);
assertThat(constraint).isSameAs(ObjectConstraint.NULL);
}
use of org.sonar.java.se.symbolicvalues.SymbolicValue in project sonar-java by SonarSource.
the class BytecodeEGWalkerTest method test_starting_states.
@Test
public void test_starting_states() throws Exception {
BytecodeEGWalker walker = new BytecodeEGWalker(null, semanticModel);
String signature = "type#foo()V";
walker.methodBehavior = new MethodBehavior(signature);
ProgramState startingState = Iterables.getOnlyElement(walker.startingStates(signature, ProgramState.EMPTY_STATE, false));
SymbolicValue thisSv = startingState.getValue(0);
assertThat(thisSv).isNotNull();
assertThat(startingState.getConstraints(thisSv).get(ObjectConstraint.class)).isEqualTo(ObjectConstraint.NOT_NULL);
startingState = Iterables.getOnlyElement(walker.startingStates(signature, ProgramState.EMPTY_STATE, true));
assertThat(startingState).isEqualTo(ProgramState.EMPTY_STATE);
signature = "type#foo(DIJ)V";
walker.methodBehavior = new MethodBehavior(signature);
startingState = Iterables.getOnlyElement(walker.startingStates(signature, ProgramState.EMPTY_STATE, true));
assertThat(startingState.getValue(0)).isNotNull();
SymbolicValue doubleArg = startingState.getValue(0);
assertThat(startingState.getConstraint(doubleArg, BytecodeEGWalker.StackValueCategoryConstraint.class)).isEqualTo(BytecodeEGWalker.StackValueCategoryConstraint.LONG_OR_DOUBLE);
assertThat(startingState.getValue(1)).isNull();
assertThat(startingState.getValue(2)).isNotNull();
SymbolicValue longArg = startingState.getValue(3);
assertThat(longArg).isNotNull();
assertThat(startingState.getConstraint(longArg, BytecodeEGWalker.StackValueCategoryConstraint.class)).isEqualTo(BytecodeEGWalker.StackValueCategoryConstraint.LONG_OR_DOUBLE);
}
use of org.sonar.java.se.symbolicvalues.SymbolicValue 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.symbolicvalues.SymbolicValue 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");
}
}
Aggregations