Class CallResolver

java.lang.Object
de.fraunhofer.aisec.cpg.passes.Pass
de.fraunhofer.aisec.cpg.passes.CallResolver
All Implemented Interfaces:
java.util.function.Consumer<TranslationResult>

public class CallResolver
extends Pass
Resolves CallExpression and NewExpression targets.

A CallExpression specifies the method that wants to be called via Node.getName(). The call target is a method of the same class the caller belongs to, so the name is resolved to the appropriate MethodDeclaration. This pass also takes into consideration that a method might not be present in the current class, but rather has its implementation in a superclass, and sets the pointer accordingly.

Constructor calls with ConstructExpression are resolved in such a way that their ConstructExpression.getInstantiates() points to the correct RecordDeclaration. Additionally, the ConstructExpression.getConstructor() is set to the according ConstructorDeclaration

  • Field Details

  • Constructor Details

    • CallResolver

      public CallResolver()
  • Method Details

    • cleanup

      public void cleanup()
      Specified by:
      cleanup in class Pass
    • accept

      public void accept​(@NonNull TranslationResult translationResult)
    • findRecords

      protected void findRecords​(@NonNull de.fraunhofer.aisec.cpg.graph.Node node, RecordDeclaration curClass)
    • findTemplates

      protected void findTemplates​(@NonNull de.fraunhofer.aisec.cpg.graph.Node node, RecordDeclaration curClass)
      Caches all TemplateDeclarations in templateList
      Parameters:
      node -
      curClass -
    • registerMethods

      protected void registerMethods​(RecordDeclaration currentClass, de.fraunhofer.aisec.cpg.graph.Node parent, @NonNull de.fraunhofer.aisec.cpg.graph.Node currentNode)
    • fixInitializers

      protected void fixInitializers​(@NonNull de.fraunhofer.aisec.cpg.graph.Node node, RecordDeclaration curClass)
    • addImplicitTemplateParametersToCall

      public static void addImplicitTemplateParametersToCall​(java.util.List<de.fraunhofer.aisec.cpg.graph.Node> templateParams, de.fraunhofer.aisec.cpg.graph.statements.expressions.ConstructExpression constructExpression)
      Adds implicit duplicates of the TemplateParams to the implicit ConstructExpression
      Parameters:
      templateParams - of the VariableDeclaration/NewExpression
      constructExpression - duplicate TemplateParameters (implicit) to preserve AST, as ConstructExpression uses AST as well as the VariableDeclaration/NewExpression
    • handleSuperCall

      protected void handleSuperCall​(RecordDeclaration curClass, CallExpression call)
      Handle calls in the form of super.call() or ClassName.super.call() , conforming to JLS13 ยง15.12.1
      Parameters:
      curClass - The class containing the call
      call - The call to be resolved
    • handleSpecificSupertype

      protected RecordDeclaration handleSpecificSupertype​(RecordDeclaration curClass, CallExpression call)
    • resolve

      protected void resolve​(@NonNull de.fraunhofer.aisec.cpg.graph.Node node, RecordDeclaration curClass)
    • handleCallExpression

      protected void handleCallExpression​(RecordDeclaration curClass, CallExpression call)
    • isInstantiated

      protected boolean isInstantiated​(de.fraunhofer.aisec.cpg.graph.Node callParameter, Declaration templateParameter)
      Checks if the provided call parameter can instantiate the required template parameter
      Parameters:
      callParameter -
      templateParameter -
      Returns:
      If the TemplateParameter is an TypeParamDeclaration, the callParameter must be an ObjectType => returns true If the TemplateParameter is a ParamVariableDeclaration, the callParamerter must be an Expression and its type must match the type of the ParamVariableDeclaration (same type or subtype) => returns true Otherwise return false
    • getParameterizedSignaturesFromInitialization

      protected java.util.Map<ParameterizedType,​TypeParamDeclaration> getParameterizedSignaturesFromInitialization​(java.util.Map<Declaration,​de.fraunhofer.aisec.cpg.graph.Node> intialization)
      Gets all ParameterizedTypes from the initialization signature
      Parameters:
      intialization - mapping of the declaration of the template parameters to the explicit values the template is instantiated with
      Returns:
      mapping of the parameterizedtypes to the corresponding TypeParamDeclaration in the template
    • handleImplicitTemplateParameter

      protected void handleImplicitTemplateParameter​(FunctionTemplateDeclaration functionTemplateDeclaration, int index, java.util.Map<Declaration,​de.fraunhofer.aisec.cpg.graph.Node> instantiationSignature, java.util.Map<de.fraunhofer.aisec.cpg.graph.Node,​TemplateDeclaration.TemplateInitialization> instantiationType, java.util.Map<Declaration,​java.lang.Integer> orderedInitializationSignature)
      Check if we are handling an implicit template parameter, if so set instantiationSignature, instantiationType and orderedInitializationSignature maps accordningly
      Parameters:
      functionTemplateDeclaration - functionTemplate we have identified
      index - position of the templateParameter we are currently handling
      instantiationSignature - mapping of the Declaration represeting a template parameter to the value that initializes that template parameter
      instantiationType - mapping of the instantiation value to the instantiation type (depends on resolution TemplateDeclaration.TemplateInitialization
      orderedInitializationSignature - mapping of the ordering of the template parameters
    • constructTemplateInitializationSignatureFromTemplateParameters

      protected @Nullable java.util.Map<Declaration,​de.fraunhofer.aisec.cpg.graph.Node> constructTemplateInitializationSignatureFromTemplateParameters​(FunctionTemplateDeclaration functionTemplateDeclaration, CallExpression templateCall, java.util.Map<de.fraunhofer.aisec.cpg.graph.Node,​TemplateDeclaration.TemplateInitialization> instantiationType, java.util.Map<Declaration,​java.lang.Integer> orderedInitializationSignature, java.util.List<ParameterizedType> explicitInstantiated)
      Creates a Mapping between the Parameters of the TemplateDeclaration and the Values provided for the instantiation of the template (Only the ones that are in defined in the instantiation -> no defaults or implicit). Additionally, it fills the maps and lists mentioned below:
      Parameters:
      functionTemplateDeclaration - functionTemplate we have identified that should be instantiated
      templateCall - callExpression that instantiates the template
      instantiationType - mapping of the instantiation value to the instantiation type (depends * on resolution TemplateDeclaration.TemplateInitialization
      orderedInitializationSignature - mapping of the ordering of the template parameters
      explicitInstantiated - list of all ParameterizedTypes which are explicitly instantiated
      Returns:
      mapping containing the all elements of the signature of the TemplateDeclaration as key and the Type/Expression the Parameter is initialized with. This function returns null if the {ParamVariableDeclaration, TypeParamDeclaration} do not match the provided value for initialization -> initialization not possible
    • getTemplateInitializationSignature

      protected java.util.Map<Declaration,​de.fraunhofer.aisec.cpg.graph.Node> getTemplateInitializationSignature​(FunctionTemplateDeclaration functionTemplateDeclaration, CallExpression templateCall, java.util.Map<de.fraunhofer.aisec.cpg.graph.Node,​TemplateDeclaration.TemplateInitialization> instantiationType, java.util.Map<Declaration,​java.lang.Integer> orderedInitializationSignature, java.util.List<ParameterizedType> explicitInstantiated)
      Parameters:
      functionTemplateDeclaration - functionTemplate we have identified that should be instantiated
      templateCall - callExpression that instantiates the template
      instantiationType - mapping of the instantiation value to the instantiation type (depends on resolution TemplateDeclaration.TemplateInitialization
      orderedInitializationSignature - mapping of the ordering of the template parameters
      explicitInstantiated - list of all ParameterizedTypes which are explicitly instantiated
      Returns:
      mapping containing the all elements of the signature of the TemplateDeclaration as key and the Type/Expression the Parameter is initialized with. This function returns null if the {ParamVariableDeclaration, TypeParamDeclaration} do not match the provided value for initialization -> initialization not possible
    • handleTemplateFunctionCalls

      protected boolean handleTemplateFunctionCalls​(@Nullable RecordDeclaration curClass, @NonNull CallExpression templateCall, boolean applyInference)
      Parameters:
      curClass - class the invoked method must be part of.
      templateCall - call to instantiate and invoke a function template
      applyInference - if the resolution was unsuccessful and applyInference is true the call will resolve to a instantiation/invocation of an inferred template
      Returns:
      true if resolution was successful, false if not
    • applyTemplateInstantiation

      protected void applyTemplateInstantiation​(CallExpression templateCall, FunctionTemplateDeclaration functionTemplateDeclaration, FunctionDeclaration function, java.util.Map<Declaration,​de.fraunhofer.aisec.cpg.graph.Node> initializationSignature, java.util.Map<de.fraunhofer.aisec.cpg.graph.Node,​TemplateDeclaration.TemplateInitialization> initializationType, java.util.Map<Declaration,​java.lang.Integer> orderedInitializationSignature)
      Performs all necessary steps to make a CallExpression instantiate a template: 1. Set TemplateInstantiation Edge from CallExpression to Template 2. Set Invokes Edge to all realizations of the Tempalte 3. Set return type of the CallExpression and checks if it uses a ParameterizedType and therefore has to be instantiated 4. Set Template Parameters Edge from the CallExpression to all Instantiation Values 5. Set DFG Edges from instantiation to ParamVariableDeclaration in TemplateDeclaration
      Parameters:
      templateCall - call to instantiate and invoke a function template
      functionTemplateDeclaration - functionTemplate we have identified that should be instantiated
      function - FunctionDeclaration representing the realization of the template
      initializationSignature - mapping containing the all elements of the signature of the TemplateDeclaration as key and the Type/Expression the Parameter is initialized with.
      initializationType - mapping of the instantiation value to the instantiation type (depends on resolution TemplateDeclaration.TemplateInitialization
      orderedInitializationSignature - mapping of the ordering of the template parameters
    • checkArgumentValidity

      protected boolean checkArgumentValidity​(FunctionDeclaration functionDeclaration, java.util.List<Type> functionDeclarationSignature, CallExpression templateCallExpression, java.util.List<ParameterizedType> explicitInstantiation)
      Parameters:
      functionDeclaration - FunctionDeclaration realization of the template
      functionDeclarationSignature - Signature of the realization FunctionDeclaration, but replacing the ParameterizedTypes with the ones provided in the instantiation
      templateCallExpression - CallExpression that instantiates the template
      explicitInstantiation - list of the explicitly instantiated type paramters
      Returns:
      true if the instantiation of the template is compatible with the templatedeclaration, false otherwise
    • getCallSignature

      protected java.util.List<Type> getCallSignature​(FunctionDeclaration function, java.util.Map<ParameterizedType,​TypeParamDeclaration> parameterizedTypeResolution, java.util.Map<Declaration,​de.fraunhofer.aisec.cpg.graph.Node> initializationSignature)
      Parameters:
      function - FunctionDeclaration realization of the template
      parameterizedTypeResolution - mapping of ParameterizedTypes to the TypeParamDeclarations that define them, used to backwards resolve
      initializationSignature - mapping between the ParamDeclaration of the template and the corresponding instantiations
      Returns:
      List of Types representing the Signature of the FunctionDeclaration, but ParameterizedTypes (which depend on the specific instantiation of the template) are resolved to the values the Template is instantiated with.
    • resolveArguments

      protected void resolveArguments​(CallExpression call, RecordDeclaration curClass)
    • compatibleSignatures

      protected boolean compatibleSignatures​(java.util.List<Type> callSignature, java.util.List<Type> functionSignature)
      Parameters:
      callSignature - Type signature of the CallExpression
      functionSignature - Type signature of the FunctionDeclaration
      Returns:
      true if the CallExpression signature can be transformed into the FunctionDeclaration signature by means of casting
    • signatureWithImplicitCastTransformation

      protected java.util.List<CastExpression> signatureWithImplicitCastTransformation​(java.util.List<Type> callSignature, java.util.List<Expression> arguments, java.util.List<Type> functionSignature)
      Computes the implicit casts that are necessary to reach the
      Parameters:
      callSignature - signature of the call we want to find invocation targets for by performing implicit casts
      arguments - arguments of the call
      functionSignature - Types of the signature of the possible invocation candidate
      Returns:
      List containing either null on the i-th position (if the type of i-th argument of the call equals the type of the i-th argument of the FunctionDeclaration) or a CastExpression on the i-th position (if the argument of the call can be casted to match the type of the argument at the i-th position of the FunctionDeclaration). If the list is empty the signature of the FunctionDeclaration cannot be reached through implicit casts
    • getCallSignatureWithDefaults

      protected java.util.List<Type> getCallSignatureWithDefaults​(CallExpression call, FunctionDeclaration functionDeclaration)
      Parameters:
      call - CallExpression
      functionDeclaration - FunctionDeclaration the CallExpression was resolved to
      Returns:
      list containing the signature containing all argument types including the default arguments
    • resolveWithImplicitCast

      protected java.util.List<FunctionDeclaration> resolveWithImplicitCast​(CallExpression call, java.util.List<FunctionDeclaration> initialInvocationCandidates)
      modifies: call arguments by applying implicit casts
      Parameters:
      call - we want to find invocation targets for by performing implicit casts
      Returns:
      list of invocation candidates by applying implicit casts
    • resolveWithImplicitCastFunc

      protected java.util.List<FunctionDeclaration> resolveWithImplicitCastFunc​(CallExpression call)
      Parameters:
      call - we want to find invocation targets for by performing implicit casts
      Returns:
      list of invocation candidates by applying implicit casts
    • checkMostCommonImplicitCast

      protected void checkMostCommonImplicitCast​(java.util.List<CastExpression> implicitCasts, java.util.List<CastExpression> implicitCastTargets)
      Checks if the current casts are compatible with the casts necessary to match with a new FunctionDeclaration. If a one argument would need to be casted in two different types it would be modified to a cast to UnknownType
      Parameters:
      implicitCasts - current Cast
      implicitCastTargets - new Cast
    • applyImplicitCastToArguments

      protected void applyImplicitCastToArguments​(CallExpression call, java.util.List<CastExpression> implicitCasts)
      Changes the arguments of the CallExpression to use the implcit casts instead
      Parameters:
      call - CallExpression
      implicitCasts - Casts
    • applyImplicitCastToArguments

      protected void applyImplicitCastToArguments​(de.fraunhofer.aisec.cpg.graph.statements.expressions.ConstructExpression constructExpression, java.util.List<CastExpression> implicitCasts)
      Changes the arguments of the ConstructExpression to use the implcit casts instead
      Parameters:
      constructExpression - ConstructExpression
      implicitCasts - Casts
    • resolveWithDefaultArgs

      protected java.util.List<FunctionDeclaration> resolveWithDefaultArgs​(CallExpression call, java.util.List<FunctionDeclaration> initialInvocationCandidates)
      Resolves a CallExpression to the potential target FunctionDeclarations by checking for ommitted arguments due to previously defined default arguments
      Parameters:
      call - CallExpression
      Returns:
      List of FunctionDeclarations that are the target of the CallExpression (will be connected with an invokes edge)
    • resolveWithDefaultArgsFunc

      protected java.util.List<FunctionDeclaration> resolveWithDefaultArgsFunc​(CallExpression call)
      Parameters:
      call - we want to find invocation targets for by adding the default arguments to the signature
      Returns:
      list of invocation candidates that have matching signature when considering default arguments
    • handleNormalCalls

      protected void handleNormalCalls​(RecordDeclaration curClass, CallExpression call)
    • handleNormalCallCXX

      protected void handleNormalCallCXX​(RecordDeclaration curClass, CallExpression call)
    • createInferredFunction

      protected void createInferredFunction​(java.util.List<FunctionDeclaration> invocationCandidates, CallExpression call)
    • handleMethodCall

      protected void handleMethodCall​(RecordDeclaration curClass, CallExpression call)
    • handleCXXMethodCall

      protected java.util.List<FunctionDeclaration> handleCXXMethodCall​(RecordDeclaration curClass, java.util.Set<Type> possibleContainingTypes, CallExpression call)
      Parameters:
      call -
      Returns:
      FunctionDeclarations that are invocation candidates for the MethodCall call using C++ resolution techniques
    • createMethodDummies

      protected void createMethodDummies​(java.util.List<FunctionDeclaration> invocationCandidates, java.util.Set<Type> possibleContainingTypes, CallExpression call)
      Creates an inferred element for each RecordDeclaration if the invocationCandidates are empty
      Parameters:
      invocationCandidates -
      possibleContainingTypes -
      call -
    • inferRecordDeclaration

      protected RecordDeclaration inferRecordDeclaration​(Type type)
      Infers a record declaration.

      TODO: Merge this with the (almost) same function in the VariableUsageResolver.

      Parameters:
      type - the object type representing a record that we want to infer.
      Returns:
      the inferred record declaration.
    • shouldSearchForInvokesInParent

      protected boolean shouldSearchForInvokesInParent​(CallExpression call)
      In C++ search we don't search in the parent if there is a potential candidate with matching name
      Parameters:
      call -
      Returns:
      true if we should stop searching parent, false otherwise
    • resolveConstructExpression

      protected void resolveConstructExpression​(de.fraunhofer.aisec.cpg.graph.statements.expressions.ConstructExpression constructExpression)
    • addRecursiveDefaultTemplateArgs

      protected void addRecursiveDefaultTemplateArgs​(de.fraunhofer.aisec.cpg.graph.statements.expressions.ConstructExpression constructExpression, ClassTemplateDeclaration template)
      Adds the resolved default template arguments recursively to the templateParameter list of the ConstructExpression until a fixpoint is reached e.g. template<class Type1, class Type2 = Type1>
      Parameters:
      constructExpression -
      template -
    • applyMissingParams

      protected void applyMissingParams​(ClassTemplateDeclaration template, de.fraunhofer.aisec.cpg.graph.statements.expressions.ConstructExpression constructExpression, java.util.Map<de.fraunhofer.aisec.cpg.graph.Node,​de.fraunhofer.aisec.cpg.graph.Node> templateParametersExplicitInitialization, java.util.Map<de.fraunhofer.aisec.cpg.graph.Node,​de.fraunhofer.aisec.cpg.graph.Node> templateParameterRealDefaultInitialization)
      Apply missingParameters (either explicit or defaults) to the ConstructExpression and its type
      Parameters:
      template - Template which is instantiated by the ConstructExpression
      constructExpression -
      templateParametersExplicitInitialization - mapping of the template parameter to the explicit instantiation
      templateParameterRealDefaultInitialization - mapping of template parameter to its real default (no recursive)
    • handleExplicitTemplateParameters

      protected void handleExplicitTemplateParameters​(de.fraunhofer.aisec.cpg.graph.statements.expressions.ConstructExpression constructExpression, ClassTemplateDeclaration template, java.util.Map<de.fraunhofer.aisec.cpg.graph.Node,​de.fraunhofer.aisec.cpg.graph.Node> templateParametersExplicitInitialization)
      Matches declared template arguments to the explicit instantiation
      Parameters:
      constructExpression - containing the explicit instantiation
      template - containing declared template arguments
      templateParametersExplicitInitialization - mapping of the template parameter to the explicit instantiation
    • handleDefaultTemplateParameters

      protected void handleDefaultTemplateParameters​(ClassTemplateDeclaration template, java.util.Map<de.fraunhofer.aisec.cpg.graph.Node,​de.fraunhofer.aisec.cpg.graph.Node> templateParameterRealDefaultInitialization)
      Matches declared template arguments to their defaults (without defaults of a previously defined template argument)
      Parameters:
      template - containing template argumetns
      templateParameterRealDefaultInitialization - mapping of template parameter to its real default (no recursive)
    • handleFunctionPointerCall

      protected void handleFunctionPointerCall​(CallExpression call, de.fraunhofer.aisec.cpg.graph.Node pointer)
    • resolveExplicitConstructorInvocation

      protected void resolveExplicitConstructorInvocation​(ExplicitConstructorInvocation eci)
    • handlePossibleStaticImport

      protected boolean handlePossibleStaticImport​(@Nullable CallExpression call, RecordDeclaration curClass)
    • generateInferredStaticallyImportedMethods

      protected void generateInferredStaticallyImportedMethods​(@NonNull CallExpression call, @NonNull java.lang.String name, @NonNull java.util.List<FunctionDeclaration> invokes, RecordDeclaration curClass)
    • createInferredFunctionTemplate

      protected FunctionTemplateDeclaration createInferredFunctionTemplate​(RecordDeclaration containingRecord, CallExpression call)
      Create an inferred FunctionTemplateDeclaration if a call to an FunctionTemplate could not be resolved
      Parameters:
      containingRecord -
      call -
      Returns:
      inferred FunctionTemplateDeclaration which can be invoked by the call
    • createInferredFunctionDeclaration

      protected @NonNull FunctionDeclaration createInferredFunctionDeclaration​(RecordDeclaration containingRecord, java.lang.String name, java.lang.String code, boolean isStatic, java.util.List<Type> signature)
    • createInferredConstructor

      protected ConstructorDeclaration createInferredConstructor​(@NonNull RecordDeclaration containingRecord, java.util.List<Type> signature)
    • getPossibleContainingTypes

      protected java.util.Set<Type> getPossibleContainingTypes​(de.fraunhofer.aisec.cpg.graph.Node node, RecordDeclaration curClass)
    • getInvocationCandidatesFromRecord

      protected java.util.List<FunctionDeclaration> getInvocationCandidatesFromRecord​(RecordDeclaration recordDeclaration, java.lang.String name, CallExpression call)
    • getInvocationCandidatesFromParents

      protected java.util.List<FunctionDeclaration> getInvocationCandidatesFromParents​(java.lang.String name, CallExpression call, java.util.Set<RecordDeclaration> possibleTypes)
    • shouldContinueSearchInParent

      protected boolean shouldContinueSearchInParent​(RecordDeclaration recordDeclaration, java.lang.String name)
      In C++ if there is a method that matches the name we are looking for, we have to stop searching in the parents even if the signature of the method does not match
      Parameters:
      recordDeclaration -
      name -
      Returns:
      true if there is no method in the recordDeclaration where the name of the method matches with the provided name. false otherwise
    • getOverridingCandidates

      protected java.util.Set<FunctionDeclaration> getOverridingCandidates​(java.util.Set<Type> possibleSubTypes, FunctionDeclaration declaration)
    • getConstructorDeclarationDirectMatch

      protected ConstructorDeclaration getConstructorDeclarationDirectMatch​(java.util.List<Type> signature, RecordDeclaration recordDeclaration)
      Parameters:
      signature - of the ConstructExpression
      recordDeclaration - matching the class the ConstructExpression wants to construct
      Returns:
      ConstructorDeclaration that matches the provided signature
    • resolveConstructorWithDefaults

      protected ConstructorDeclaration resolveConstructorWithDefaults​(de.fraunhofer.aisec.cpg.graph.statements.expressions.ConstructExpression constructExpression, java.util.List<Type> signature, RecordDeclaration recordDeclaration)
      Parameters:
      constructExpression - we want to find an invocation target for
      signature - of the ConstructExpression (without defaults)
      recordDeclaration - associated with the Object the ConstructExpression constructs
      Returns:
      a ConstructDeclaration that matches with the signature of the ConstructExpression with added default arguments. The default arguments are added to the arguments edge of the ConstructExpression
    • resolveConstructorWithImplicitCast

      protected ConstructorDeclaration resolveConstructorWithImplicitCast​(de.fraunhofer.aisec.cpg.graph.statements.expressions.ConstructExpression constructExpression, RecordDeclaration recordDeclaration)
      Parameters:
      constructExpression - we want to find an invocation target for
      recordDeclaration - associated with the Object the ConstructExpression constructs
      Returns:
      a ConstructDeclaration that matches the signature of the ConstructExpression by applying one or more implicit casts to the primitive type arguments of the ConstructExpressions. The arguments are proxied through a CastExpression to the type required by the ConstructDeclaration.
    • getConstructorDeclaration

      protected @NonNull ConstructorDeclaration getConstructorDeclaration​(de.fraunhofer.aisec.cpg.graph.statements.expressions.ConstructExpression constructExpression, RecordDeclaration recordDeclaration)
      Parameters:
      constructExpression - we want to find an invocation target for
      recordDeclaration - associated with the Object the ConstructExpression constructs
      Returns:
      a ConstructDeclaration that is an invocation of the given ConstructExpression. If there is no valid ConstructDeclaration we will create an implicit ConstructDeclaration that matches the ConstructExpression.
    • getConstructorDeclarationForExplicitInvocation

      protected @NonNull ConstructorDeclaration getConstructorDeclarationForExplicitInvocation​(java.util.List<Type> signature, RecordDeclaration recordDeclaration)