@Override public int hashCode() { if (hashCode == 0) { hashCode = Objects.hash(values, constraints, peekValue()); } return hashCode; }
@CheckForNull private Symbol symbolFromStack(SymbolicValue symbolicValue, @Nullable ProgramState programState) { if (programState != null && programState.peekValue() == symbolicValue) { return programState.peekValueSymbol().symbol; } return null; }
private static ProgramState handleShift(ProgramState previousState, ProgramState currentState) { List<SymbolicValue> operands = previousState.peekValues(2); SymbolicValue result = currentState.peekValue(); SymbolicValue op1 = operands.get(0); SymbolicValue op2 = operands.get(0); if (isZero(currentState, op1) || isZero(currentState, op2)) { // shifting by zero or shifting zero: Reuse fist operand return currentState.unstackValue(1).state.stackValue(op1); } return currentState.removeConstraintsOnDomain(result, BooleanConstraint.class).addConstraint(result, ZeroConstraint.NON_ZERO); }
@Override public int hashCode() { if (hashCode == 0) { hashCode = Objects.hash(values, constraints, peekValue()); } return hashCode; }
private Set<Symbol> getReturnSymbolAsTrackedSymbols(List<Integer> parameterIndices) { if (parameterIndices.contains(-1) && node.programState.peekValue() == node.programState.exitValue()) { Symbol symbol = node.programState.peekValueSymbol().symbol(); if (symbol != null) { return Collections.singleton(symbol); } } return Collections.emptySet(); }
@Override public void visitNewClass(NewClassTree syntaxNode) { final SymbolicValue instanceValue = programState.peekValue(); if (isOpeningResource(syntaxNode) && !passedCloseableParameter(instanceValue)) { programState = programState.addConstraintTransitively(instanceValue, OPEN); } }
@Override public void visitNewClass(NewClassTree newClassTree) { if (isCreatingResource(newClassTree)) { openResource(programState.peekValue()); } }
@Override public void visitNewClass(NewClassTree newClassTree) { if (isCreatingResource(newClassTree)) { openResource(programState.peekValue()); } }
@Override public void visitNewClass(NewClassTree syntaxNode) { final SymbolicValue instanceValue = programState.peekValue(); if (isOpeningResource(syntaxNode) && !passedCloseableParameter(instanceValue)) { programState = programState.addConstraintTransitively(instanceValue, OPEN); } }
@Override public void visitReturnStatement(ReturnStatementTree syntaxNode) { ExpressionTree expression = syntaxNode.expression(); if (expression != null) { closeResource(programState.peekValue()); } }
@Override public void visitMethodInvocation(MethodInvocationTree mit) { if (isCreatingResource(mit)) { openResource(programState.peekValue()); } }
private static ProgramState handleNumericalLiteral(CheckerContext context, @Nullable Number value) { ProgramState programState = context.getState(); if (value == null) { return programState; } return programState.addConstraint(programState.peekValue(), new NumericalConstraint(value)); }
private Optional<JavaFileScannerContext.Location> flowFromThrownException(ExplodedGraph.Edge edge) { SymbolicValue peekValue = edge.child.programState.peekValue(); if (peekValue instanceof SymbolicValue.ExceptionalSymbolicValue && (isMethodInvocationNode(edge.parent) || isDivByZeroExceptionalYield(edge))) { Type type = ((SymbolicValue.ExceptionalSymbolicValue) peekValue).exceptionType(); String msg = String.format("%s is thrown.", exceptionName(type)); return Optional.of(location(edge.parent, msg)); } return Optional.empty(); }
private static List<ProgramState> setNullConstraint(CheckerContext context, Tree syntaxNode) { SymbolicValue val = context.getState().peekValue(); if (syntaxNode.is(Tree.Kind.METHOD_INVOCATION) && isAnnotatedCheckForNull((MethodInvocationTree) syntaxNode)) { Preconditions.checkNotNull(val); List<ProgramState> states = new ArrayList<>(); states.addAll(val.setConstraint(context.getState(), ObjectConstraint.NULL)); states.addAll(val.setConstraint(context.getState(), ObjectConstraint.NOT_NULL)); return states; } return Lists.newArrayList(context.getState()); }
private void checkReturnedValue(ReturnStatementTree tree, String nonNullAnnotation) { SymbolicValue returnedValue = programState.peekValue(); ObjectConstraint constraint = programState.getConstraint(returnedValue, ObjectConstraint.class); if (constraint != null && constraint.isNull()) { reportIssue(tree, "This method''s return value is marked \"{0}\" but null is returned.", nonNullAnnotation); } } }
private static ProgramState handleOpenOptionAppend(ProgramState programState, IdentifierTree identifier) { if (identifier.symbolType().is("java.nio.file.StandardOpenOption") && "APPEND".equals(identifier.name())) { return programState.addConstraint(programState.peekValue(), new FileOutputStreamAppendConstraint(identifier)); } return programState; }
private ProgramState handleSpecialMethods(ProgramState ps, MethodInvocationTree mit) { if (isAnnotatedNonNull(mit.symbol())) { return ps.addConstraint(ps.peekValue(), ObjectConstraint.NOT_NULL); } else if (OBJECT_WAIT_MATCHER.matches(mit)) { return ps.resetFieldValues(constraintManager, false); } return ps; }
private ProgramState handleSpecialMethods(ProgramState ps, MethodInvocationTree mit) { if (isAnnotatedNonNull(mit.symbol())) { return ps.addConstraint(ps.peekValue(), ObjectConstraint.NOT_NULL); } else if (OBJECT_WAIT_MATCHER.matches(mit)) { return ps.resetFieldValues(constraintManager, false); } return ps; }
@Override public void visitTypeCast(TypeCastTree tree) { Type type = tree.type().symbolType(); if (type.isPrimitive()) { SymbolicValue sv = programState.peekValue(); if (isZero(sv)) { reuseSymbolicValue(sv); } else if (isNonZero(sv)) { deferConstraint(ZeroConstraint.NON_ZERO); } } }
@Override public void visitLiteral(LiteralTree tree) { String value = tree.value(); SymbolicValue sv = programState.peekValue(); if (tree.is(Tree.Kind.CHAR_LITERAL) && isNullCharacter(value)) { addZeroConstraint(sv, ZeroConstraint.ZERO); } else if (tree.is(Tree.Kind.INT_LITERAL, Tree.Kind.LONG_LITERAL, Tree.Kind.DOUBLE_LITERAL, Tree.Kind.FLOAT_LITERAL)) { addZeroConstraint(sv, isNumberZero(value) ? ZeroConstraint.ZERO : ZeroConstraint.NON_ZERO); } }