Search in sources :

Example 1 with LeakedToResult

use of org.checkerframework.common.aliasing.qual.LeakedToResult in project checker-framework by typetools.

the class AliasingTransfer method processPostconditions.

/**
 * Handling pseudo-assignments. Called by {@code CFAbstractTransfer.visitMethodInvocation()}.
 *
 * <p>Case 2: Given a method call, traverses all formal parameters of the method declaration,
 * and if it doesn't have the {@literal @}NonLeaked or {@literal @}LeakedToResult annotations,
 * we remove the node of the respective argument in the method call from the store. If parameter
 * has {@literal @}LeakedToResult, {@code visitMethodInvocation()} handles it.
 */
@Override
protected void processPostconditions(MethodInvocationNode n, CFStore store, ExecutableElement methodElement, Tree tree) {
    super.processPostconditions(n, store, methodElement, tree);
    if (TreeUtils.isEnumSuper(n.getTree())) {
        // Skipping the init() method for enums.
        return;
    }
    List<Node> args = n.getArguments();
    List<? extends VariableElement> params = methodElement.getParameters();
    assert (args.size() == params.size()) : "Number of arguments in " + "the method call " + n.toString() + " is different from the" + " number of parameters for the method declaration: " + methodElement.getSimpleName().toString();
    AnnotatedExecutableType annotatedType = factory.getAnnotatedType(methodElement);
    List<AnnotatedTypeMirror> paramTypes = annotatedType.getParameterTypes();
    for (int i = 0; i < args.size(); i++) {
        Node arg = args.get(i);
        AnnotatedTypeMirror paramType = paramTypes.get(i);
        if (!paramType.hasAnnotation(NonLeaked.class) && !paramType.hasAnnotation(LeakedToResult.class)) {
            store.clearValue(FlowExpressions.internalReprOf(factory, arg));
        }
    }
    // Now, doing the same as above for the receiver parameter
    Node receiver = n.getTarget().getReceiver();
    AnnotatedDeclaredType receiverType = annotatedType.getReceiverType();
    if (receiverType != null && !receiverType.hasAnnotation(LeakedToResult.class) && !receiverType.hasAnnotation(NonLeaked.class)) {
        store.clearValue(FlowExpressions.internalReprOf(factory, receiver));
    }
}
Also used : AnnotatedExecutableType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType) AssignmentNode(org.checkerframework.dataflow.cfg.node.AssignmentNode) ObjectCreationNode(org.checkerframework.dataflow.cfg.node.ObjectCreationNode) MethodInvocationNode(org.checkerframework.dataflow.cfg.node.MethodInvocationNode) Node(org.checkerframework.dataflow.cfg.node.Node) AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) LeakedToResult(org.checkerframework.common.aliasing.qual.LeakedToResult) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 2 with LeakedToResult

use of org.checkerframework.common.aliasing.qual.LeakedToResult in project checker-framework by typetools.

the class AliasingTransfer method visitMethodInvocation.

/**
 * Case 3: Given a method invocation expression, if the parent of the expression is not a
 * statement, check if there are any arguments of the method call annotated as
 * {@literal @}LeakedToResult and remove it from the store, since it might be leaked.
 */
@Override
public TransferResult<CFValue, CFStore> visitMethodInvocation(MethodInvocationNode n, TransferInput<CFValue, CFStore> in) {
    Tree parent = n.getTreePath().getParentPath().getLeaf();
    boolean parentIsStatement = parent.getKind() == Kind.EXPRESSION_STATEMENT;
    if (!parentIsStatement) {
        ExecutableElement methodElement = TreeUtils.elementFromUse(n.getTree());
        List<Node> args = n.getArguments();
        List<? extends VariableElement> params = methodElement.getParameters();
        assert (args.size() == params.size()) : "Number of arguments in " + "the method call " + n.toString() + " is different from the" + " number of parameters for the method declaration: " + methodElement.getSimpleName().toString();
        CFStore store = in.getRegularStore();
        for (int i = 0; i < args.size(); i++) {
            Node arg = args.get(i);
            VariableElement param = params.get(i);
            if (factory.getAnnotatedType(param).hasAnnotation(LeakedToResult.class)) {
                // If argument can leak to result, and parent is not a
                // single statement, remove that node from store.
                store.clearValue(FlowExpressions.internalReprOf(factory, arg));
            }
        }
        // Now, doing the same as above for the receiver parameter
        Node receiver = n.getTarget().getReceiver();
        AnnotatedExecutableType annotatedType = factory.getAnnotatedType(methodElement);
        AnnotatedDeclaredType receiverType = annotatedType.getReceiverType();
        if (receiverType != null && receiverType.hasAnnotation(LeakedToResult.class)) {
            store.clearValue(FlowExpressions.internalReprOf(factory, receiver));
        }
    }
    // pseudo-assignments.
    return super.visitMethodInvocation(n, in);
}
Also used : AnnotatedExecutableType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType) CFStore(org.checkerframework.framework.flow.CFStore) ExecutableElement(javax.lang.model.element.ExecutableElement) AssignmentNode(org.checkerframework.dataflow.cfg.node.AssignmentNode) ObjectCreationNode(org.checkerframework.dataflow.cfg.node.ObjectCreationNode) MethodInvocationNode(org.checkerframework.dataflow.cfg.node.MethodInvocationNode) Node(org.checkerframework.dataflow.cfg.node.Node) AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) LeakedToResult(org.checkerframework.common.aliasing.qual.LeakedToResult) Tree(com.sun.source.tree.Tree) VariableElement(javax.lang.model.element.VariableElement)

Aggregations

LeakedToResult (org.checkerframework.common.aliasing.qual.LeakedToResult)2 AssignmentNode (org.checkerframework.dataflow.cfg.node.AssignmentNode)2 MethodInvocationNode (org.checkerframework.dataflow.cfg.node.MethodInvocationNode)2 Node (org.checkerframework.dataflow.cfg.node.Node)2 ObjectCreationNode (org.checkerframework.dataflow.cfg.node.ObjectCreationNode)2 AnnotatedDeclaredType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType)2 AnnotatedExecutableType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType)2 Tree (com.sun.source.tree.Tree)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1 VariableElement (javax.lang.model.element.VariableElement)1 CFStore (org.checkerframework.framework.flow.CFStore)1 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)1