An API misuse detection method based on hypergraph neural network

By constructing an API-using hypergraph and introducing hypergraph neural networks and attention mechanisms, the limitations of existing API misuse detection methods are addressed, achieving more efficient API misuse detection and improving detection accuracy and adaptability.

CN119597522BActive Publication Date: 2025-12-30BEIJING UNIV OF TECH
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202411647159.2
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-11-18
Publication Date
2025-12-30
Estimated Expiration
2044-11-18

AI Technical Summary

Technical Problem

Existing API misuse detection methods have limitations in handling issues such as lack of detailed documentation, delayed document updates, difficulty in capturing semantic information through natural language processing, and insufficient coverage of frequent usage patterns, resulting in poor API misuse detection performance.

Method used

The API is built using a hypergraph, which introduces a hypergraph neural network and attention mechanism. The hyperedge captures the high-order dependencies of API calls, parameter passing and control flow operations. The CodeBERT pre-trained model is used to generate node embedding vectors, and features are aggregated through multi-layer convolution and pooling operations. The hypergraph neural network is then trained to detect misuse.

Benefits of technology

It improves the accuracy and comprehensiveness of API misuse detection, better adapts to rapidly changing software development environments, and captures high-order dependencies in complex API usage scenarios.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119597522B_ABST
    Figure CN119597522B_ABST
Patent Text Reader

Abstract

The application relates to the field of software engineering, and discloses an API misuse detection method based on a hypergraph neural network, which aims to improve the accuracy of API misuse detection results, and comprises the following steps: step S1, constructing a labeled code set containing API correct use samples and misuse samples; step S2, constructing an API use graph according to the abstract syntax tree structure of source code; step S3, generating an API use hypergraph based on the API use graph; step S4, introducing a hypergraph attention mechanism, training a hypergraph neural network, and obtaining an API misuse detection model; and step S5, converting the source code to be detected into a hypergraph structure, inputting the hypergraph structure into the trained hypergraph neural network, and obtaining an API misuse detection result. The application applies the hypergraph neural network to API misuse detection, uses hyperedges to capture high-order dependencies and complex interaction relationships in source code, can process diversified API use modes, and can improve the accuracy of detection results.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of software engineering technology, and more specifically to an API misuse detection method based on hypergraph neural networks. Background Technology

[0002] In modern software development, efficient reuse of existing software has become a cornerstone for developers to complete development tasks quickly and efficiently. In this process, Application Programming Interfaces (APIs) play a crucial role, enabling developers to easily reuse third-party libraries or software frameworks. However, in practice, developers often face the problem of insufficient understanding of the correct usage of APIs, such as confusing the order of function calls or using incorrect parameters, which can lead to API misuse. API misuse not only creates security vulnerabilities but can also cause software errors and crashes. Furthermore, due to the wide variety of APIs, their complex usage, incomplete documentation, and outdated information, developers face significant challenges in learning and applying APIs, making API misuse very common in software development. Therefore, accurate detection of API misuse is of paramount importance for improving the quality and efficiency of software development.

[0003] Currently, methods for automatically detecting API misuse in code snippets can be broadly categorized into two types. The first type primarily relies on natural language processing (NLP) techniques. By analyzing descriptive information about APIs from reference documents, Q&A forums, and other sources, it extracts API usage guidelines or further constructs API constraint knowledge graphs for misuse detection. The second type utilizes dependency pattern mining techniques, which analyze program dependency graphs and API call sequences from project source code to uncover common API usage patterns. These methods typically presuppose that any deviation from these frequent usage patterns may constitute potential API misuse.

[0004] While existing methods have achieved some success, several limitations remain. First, many libraries lack comprehensive and high-quality documentation, making it difficult to extract API usage guidelines. Second, documentation updates often lag behind actual API version changes, potentially leading to incorrect misuse detection based on outdated information. Third, natural language processing (NLP) techniques struggle to fully capture semantic information and context when processing descriptive text, meaning the extracted API usage guidelines may not align with actual development requirements. Furthermore, frequent usage patterns require predefined thresholds; therefore, for newly released or less frequently used APIs, insufficient usage data prevents the discovery of frequent usage patterns. Existing API misuse detection methods require continuous improvement to better adapt to the rapidly changing software development environment. Summary of the Invention

[0005] This invention addresses the limitations of existing API misuse detection methods by proposing a hypergraph neural network-based API misuse detection method. This invention is achieved through the following technical solution:

[0006] Step S1: Construct a dataset containing samples of correct API usage and samples of misuse;

[0007] Step S2: Construct an API usage graph based on the abstract syntax tree structure of the source code;

[0008] Step S3: Generate an API usage hypergraph based on the API usage graph;

[0009] Step S4: Introduce the hypergraph attention mechanism, train the hypergraph neural network, and obtain the API misuse detection model;

[0010] Step S5: Convert the source code to be detected into a hypergraph structure and input it into the trained hypergraph neural network to obtain the API misuse detection results.

[0011] Furthermore, in the API misuse detection method based on hypergraph neural networks described in this invention, wherein:

[0012] The implementation process of step S1 is as follows:

[0013] Step S1-1: Collect high-quality target API client code and its library source code from Github to build the correct API usage code set.

[0014] The code management platform GitHub was used to search for target APIs to find projects that frequently use these APIs, and projects with high numbers of "likes" were selected from the search results. Further filtering of files within these projects removed configuration definitions and other files, retaining only .java files containing one or more target APIs and with more than 100 lines of code. Since most files on GitHub are cloned, a tag-based code cloning deduplication method was used to avoid wasting time and space storing redundant information. Additionally, for each target API, the source code files of its respective library were collected. These source files are all open-source and contain complete library code information.

[0015] Step S1-2 uses keywords to search and filter posts on GitHub and Stack Overflow that involve the target API, extracts samples of misused code, and constructs a set of API misuse codes.

[0016] Select open-source projects on GitHub that contain the target API, especially large, active, and well-maintained projects. Use the API name as a keyword to search and filter issue and pull request posts related to the target API, paying particular attention to those tagged with bugs, fixes, errors, etc., which are related to bug fixing. Extract code samples from these posts, ensuring that the obtained code blocks are large enough to include complete functions or classes to preserve the context information of the API calls. Similarly, use the API name, misuse, error, exception, issue, bug, etc., as keywords to search posts on Stack Overflow and extract API misuse code blocks. Merge the data collected from GitHub and Stack Overflow, removing duplicate and redundant data to form an API misuse code set.

[0017] The target APIs were selected from the MUBench dataset, widely used in API misuse detection, and the top 100 most frequently discussed Java APIs on the question-and-answer website Stack Overflow. The most frequently discussed APIs on Stack Overflow are typically those frequently used in actual development, and developers often have questions about their usage.

[0018] In step S2:

[0019] Due to its highly structured nature, source code often lacks intuitive information regarding method call relationships and parameter passing order, making direct source code analysis difficult. Therefore, static code analysis is performed on the constructed codebase. First, an Abstract Syntax Tree (AST) is generated from the source code. Furthermore, considering that API misuse primarily stems from improper method call order, incorrect exception handling, missing pre / postconditions, and incorrect parameter values, a parsing construction is performed on the AST to extract relevant API usage and context information, constructing an API Usage Graph (AUG). AUG is a directed graph with labeled nodes and edges, specifically designed to represent the specificities of API usage. AUG is denoted as G. b =(V b E b ), where the node set V b This includes action nodes and data nodes, namely API methods, operators and parameters, and objects. Edge set E b It represents the data flow and control flow relationships between node elements, including definition edges, parameter edges, call edges, exception-throwing edges, and sequence edges.

[0020] In step S3:

[0021] In a regular graph, each edge is limited to connecting only two nodes, restricting its ability to represent only binary relationships between objects. To capture more complex relationships, hypergraphs were introduced. As an extension of regular graphs, edges in a hypergraph can connect to any number of nodes, thus overcoming the limitations of binary relationships. Thanks to its flexible hyperedge design, hypergraphs can reveal higher-order relationships that are difficult for regular graphs to represent, compensating for the shortcomings of regular graphs in representing data correlations.

[0022] In this invention, the AUG obtained in step S2 is used to construct an API usage hypergraph. The nodes in the AUG are used as nodes of the hypergraph. A set of hyperedges is constructed based on the association between the nodes in the AUG, including call order hyperedges, exception handling hyperedges, condition dependency hyperedges, and parameter checking hyperedges.

[0023] Specifically, call order hyperedges are used to represent sequential dependencies between API calls. In practical API applications, the order of method calls is crucial; an incorrect call order can lead to functional malfunctions or security vulnerabilities. By extracting the order edge information from AUG, API nodes that are called in sequence are connected together.

[0024] Secondly, the exception handling hyperedge is used to capture the correlation between the throwing and handling of exceptions during API calls. This is done by associating the API node that throws the exception with the operator node that catches the exception.

[0025] Furthermore, conditional dependency hyperedges are used to represent necessary conditional dependencies before and after an API call. Before an API call, resource preparation or state checks are often required; similarly, cleanup or resource release operations are typically performed after the API call. Because of the complex dependencies between API calls and their preconditions and postconditions, by analyzing the definition edges, parameter edges, and control flow edges in the AUG, the dependent operations are connected to the API node through conditional dependency hyperedges, thus providing higher-dimensional dependency information through the hypergraph.

[0026] Finally, the parameter checking hyperedge is used to capture the parameter passing and usage in API calls. When multiple APIs pass, modify, and validate the same parameter, any non-standard operation may cause abnormal system behavior or security vulnerabilities. Therefore, a parameter checking hyperedge was designed to extract key information from parameter edges in AUG and connect API nodes with parameter nodes. This hyperedge can reflect the flow of parameters in different API calls to detect misuse errors caused by incorrect parameters.

[0027] The hypergraph in this invention is defined as Juzhong This represents the node set, and the node set V in the graph used by the API. bSame, ε b Let W represent the set of hyperedges, and W be a diagonal matrix representing the weights of the hyperedges. This invention does not consider the mutual influence between different hyperedges; therefore, each hyperedge is assigned a weight value of 1.0. Since each hyperedge includes a set of adjacent nodes, all hyperedges can be constructed into a single set. In a matrix H of dimension , if a node v belongs to some hyperedge e, then h(v, e) = 1, otherwise it is 0.

[0028] The implementation process of step S4 is as follows:

[0029] Step S4-1: Use Hypergraph on API node set Each node in the process undergoes feature initialization.

[0030] To enable the hypergraph neural network to fully learn the semantic information of the nodes, an initial embedding vector needs to be generated for each node in the hypergraph node set used by the API. By expanding the context information of the nodes and combining it with the context input in the code, the embedding vector of the nodes is generated after encoding.

[0031] We employ the CodeBERT pre-trained model, which is trained on large-scale code corpora and natural language data, and is capable of generating high-dimensional embedding representations of code snippets. Given an API, we use the node set of a hypergraph. For each node Taking the expanded context as input, CodeBERT encoding will output a fixed-dimensional embedding vector. Where d is the node embedding dimension. Through experimental analysis of the model's detection performance under different embedding dimensions, it was found that the model performs best on all datasets when the node embedding dimension is 96.

[0032] If the context information corresponding to a node contains multiple parts, mean pooling is used to aggregate the embedding vectors of each part to obtain the final embedding representation of the node.

[0033]

[0034] in, For node υ i Multiple context fragments, x i It is the final aggregated embedding vector of the node.

[0035] Using the method described above, each node in the API hypergraph is encoded as an embedding vector. The set of embedding vectors for all nodes is represented as the initial feature space X of the nodes. b ={x1, x2, ..., x n}, where n is the total number of nodes. For each node, d is the feature vector, and d is the dimension of the embedding vector.

[0036] Step S4-2: Input the node embedding vector and the hypergraph structure into the hypergraph neural network, introduce the hyperedge attention mechanism for training, and obtain the API misuse detection model.

[0037] The basic idea of ​​a hypergraph neural network is to represent hypergraph data as a matrix and perform convolution operations. Through multiple convolutional layers, it learns node representations, thereby improving the model's expressive power. The hypergraph neural network performs feature smoothing on the convolutional layers using formula (1):

[0038]

[0039] Where σ(·) is the nonlinear activation function ReLU(·), l∈L is the number of layers in the hypergraph neural network, L is the total number of layers, W is the diagonal matrix representing the hyperedge weights, and X l D is the feature matrix of the input nodes of the l-th layer. v and D e These are diagonal matrices representing the node degrees and hyperedge degrees of the hypergraph used by the API, respectively. It is the hypergraph incidence matrix, Θ l These are the weight parameters for the feature transformation of the l-th layer nodes.

[0040] The number of convolutional layers in a hypergraph neural network is also a key factor affecting model performance. This invention analyzes the detection performance of the model under different numbers of convolutional layers through experiments and determines that the optimal performance is achieved when L=2.

[0041] Since different types of hyperedges contain different semantic information, this invention introduces an attention mechanism for various types of hyperedges during the propagation of node information. By assigning different weights to hyperedges, its influence in the propagation of node features is dynamically adjusted. Node υ i For a superedge of type t weight The calculation is shown in formula (2), where t∈{1, 2, 3, 4}:

[0042]

[0043] Where LeakyReLU is a non-linear activation function, N j t Indicates the superedge The set of nodes contained in α t This represents the attention vector of a hyperedge of type t.

[0044] The hypergraph correlation matrix H is updated using the hyperedge attention mechanism, generating a weighted correlation matrix. Where β is the attention weight matrix and H is the original association matrix of the API using the hypergraph.

[0045] After the convolutional layers, the pooling layers are used to aggregate the node features of the hypergraph to generate a global graph embedding representation. In this invention, average pooling is used to aggregate the features of the hypergraph nodes, as shown in formula (3):

[0046]

[0047] in, It is the set of nodes in a hypergraph. It is the node υ after the Lth convolution layer. i Feature representation, It is the generated global graph embedding representation.

[0048] Get the global embedding z g Then, it is passed to the fully connected layer and the output layer for API misuse detection. The fully connected layer maps the embedding to a scalar, as shown in Equation (4), and the result represents the probability that the graph is an API misuse:

[0049]

[0050] Among them, W c This represents the weight matrix, where b is the bias term. It is the model's predicted label for the image.

[0051] In the API misuse detection model, the cross-entropy loss function is used to optimize the model, and the loss function is shown in Equation (5):

[0052]

[0053] Where N is the number of samples, y i This is the true label of the i-th sample (1 indicates misuse, 0 indicates correct use). It is the model's predicted label for the i-th sample.

[0054] The hypergraph model in this invention is implemented using PyTorch. During the gradient descent learning process, the Adam optimizer is used to adaptively learn the trainable parameters. Specifically, the learning rate is set to 0.001, the initial value of the weight decay is 0.0001, and the number of iterations is 500.

[0055] Step S5: Convert the source code to be detected into a hypergraph structure and input it into the trained hypergraph neural network to obtain the API misuse detection results.

[0056] The code to be detected is obtained and converted into an API usage hypergraph. This code is then fed into a pre-trained hypergraph neural network. The hypergraph neural network performs feature aggregation on the nodes and edges in the hypergraph and analyzes whether the current code exhibits API misuse behavior based on the feature patterns learned during previous training, outputting the API misuse detection result.

[0057] The beneficial effects of this invention are:

[0058] Compared with existing technologies, this invention proposes an API misuse detection method based on hypergraph neural networks and attention mechanisms. By constructing an API usage hypergraph, this invention captures high-order dependencies between API calls, parameter passing, and control flow operations. Unlike traditional graph structures that cannot express the relationships between multiple nodes, this invention utilizes hyperedges to model complex interactions between multiple nodes and introduces a hyperedge attention mechanism to weight the importance of different types of hyperedges, further enhancing the model's ability to handle complex API usage scenarios. Furthermore, this invention uses a hypergraph neural network to perform feature learning on the API usage hypergraph, automatically extracting contextual dependencies and interaction information in API usage, thus improving the accuracy and comprehensiveness of API misuse detection. Attached Figure Description

[0059] To more clearly illustrate the technical solutions in the embodiments of the present invention, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0060] Figure 1 This is a flowchart illustrating an API misuse detection method based on a hypergraph neural network, provided in an embodiment of the present invention.

[0061] Figure 2 This is an API usage diagram constructed based on the code fragment to be detected in an embodiment of the present invention.

[0062] Figure 3 This is a schematic diagram of a hypergraph convolutional layer in an embodiment of the present invention. Detailed Implementation

[0063] To make the objectives and technical solutions of this invention clearer, the technical solutions of this invention will be clearly and completely described below with reference to the accompanying drawings of the embodiments of this invention. Obviously, the described embodiments are only a part of the embodiments of this invention, and not all of them. All other embodiments obtained by those skilled in the art based on the embodiments of this invention without creative effort are within the scope of protection of this invention.

[0064] This invention discloses an API misuse detection method based on a hypergraph neural network, such as... Figure 1 As shown, it includes:

[0065] Step S1: Construct a set of annotated codes containing samples of correct API usage and samples of misuse;

[0066] Step S2: Construct an API usage graph based on the abstract syntax tree structure of the source code;

[0067] Figure 2 This invention demonstrates an example of converting a code snippet written by a developer into an AUG graph, where boxes represent action nodes, ellipses represent data nodes, dashed lines represent data flow edges, and solid lines represent control flow edges.

[0068] Step S3: Generate an API usage hypergraph based on the API usage graph;

[0069] Based on the information obtained from AUG in step S2, further information is extracted. The nodes in AUG are used as nodes of the hypergraph. A set of hyperedges is constructed according to the relationships between the nodes in AUG, including call order hyperedges, exception handling hyperedges, conditional dependency hyperedges, and parameter checking hyperedges, thereby constructing the hypergraph. in This represents the node set, and the node set V in the graph used by the API. b Same; ε b Let W represent the set of hyperedges, and W be a diagonal matrix representing the weights of the hyperedges. This invention does not consider the mutual influence between different hyperedges, so each hyperedge is assigned a weight value of 1.0.

[0070] Step S4: Introduce the hypergraph attention mechanism, train the hypergraph neural network, and obtain the API misuse detection model;

[0071] A hypergraph attention-based API misuse detection model was trained using data to obtain model parameters, including:

[0072] Step S4-1: Use Hypergraph on API node set Each node in the process is initialized with features;

[0073] Given an API, use the node set of the hypergraph. For each node The expanded context is fed into the CodeBERT pre-trained model, and through encoding, it outputs a fixed-dimensional embedding vector. Here, 'd' represents the node embedding dimension. In the model, the embedding dimension is a crucial parameter. If the embedding dimension is set too low, it reduces the discriminative power between nodes, resulting in overly coarse feature representations and making it difficult for the model to capture sufficiently detailed node features, thus affecting detection performance. Conversely, an excessively high embedding dimension increases model complexity and can easily lead to overfitting. Furthermore, high-dimensional embedding vectors increase computational overhead, impacting overall detection efficiency. Therefore, through experimental analysis of the model's detection performance under different embedding dimensions, it was found that a node embedding dimension of 96 yields the best results across various datasets.

[0074] If the context information corresponding to a node contains multiple parts, mean pooling is used to aggregate the embedding vectors of each part to obtain the final embedding representation of the node.

[0075]

[0076] in, For node υ i Multiple context fragments, x i It is the final aggregated embedding vector of the node.

[0077] Using the method described above, each node in the API hypergraph is encoded as an embedding vector. The set of embedding vectors for all nodes is represented as the initial feature space X of the nodes. b ={x1, x2, ..., x n}, where n is the total number of nodes. For each node, there is a feature vector, and d is the dimension of the embedding vector, where d is 96.

[0078] Step S4-2: Input the node embedding vector and hypergraph structure into the hypergraph neural network, introduce the hyperedge attention mechanism for training, and obtain the API misuse detection model;

[0079] The constructed API is input into the hypergraph neural network using the hypergraph and the node embedding vectors obtained in step S4-1 for training. The hypergraph neural network propagates and updates the node features in the hypergraph through multiple convolutional operations, with each convolutional layer aggregating the node features through hyperedges. Figure 3 This is a schematic diagram of a hypergraph convolutional layer. In the diagram, υ1, υ2, ..., υ8 represent nodes in the hypergraph, and e1, e2, ..., e8 represent nodes in the hypergraph. m Let x1, x2, ..., x be the superedges. n This represents the node features after hypergraph convolution. The convolution operation is shown in equation (1):

[0080]

[0081] Where σ(·) is the nonlinear activation function ReLU(·), l∈L is the number of layers in the hypergraph neural network, L is the total number of layers, W is a diagonal matrix representing the weights of the hyperedges. This invention does not consider the mutual influence between different hyperedges; therefore, each hyperedge is assigned a weight value of 1.0. l D is the feature matrix of the input nodes of the l-th layer. v and D e These are diagonal matrices representing the node degrees and hyperedge degrees of the hypergraph used by the API, respectively. It is the weighted hypergraph incidence matrix, Θ l These are the weight parameters for the feature transformation of the l-th layer nodes.

[0082] This invention introduces an attention mechanism for various types of hyperedges during the propagation of node information, dynamically adjusting the influence of each hyperedge on node feature aggregation. Node υ i For a superedge of type t weight The calculation is shown in formula (2), where t∈{1, 2, 3, 4}:

[0083]

[0084] Where LeakyReLU is a non-linear activation function, N j t Indicates the superedge The set of nodes connected, α t Let x represent the attention vector of a hyperedge of type t. i It is node υ i eigenvectors.

[0085] The attention weights between the hyperedge and the node are calculated using the hyperedge attention mechanism, and the adjacency matrix H is weighted to generate a new weighted matrix. Where β is the attention weight matrix.

[0086] The number of convolutional layers in a hypergraph neural network is also a key factor affecting model performance. An appropriate number of convolutional layers can effectively capture the complex interactions between nodes in the API usage hypergraph, improving the model's accuracy in misuse detection. Too few convolutional layers may fail to fully capture deep relationships across nodes, leading to insufficient feature learning and affecting detection performance. As the number of convolutional layers increases, the model's non-linear expressive power is enhanced, enabling it to better learn complex contextual dependencies between nodes. However, too many convolutional layers can lead to an overly complex model, increasing the risk of overfitting and significantly increasing computational costs. Therefore, this invention analyzes the detection performance of the model under different numbers of convolutional layers through experiments, determining that L=2 yields the optimal result.

[0087] After the convolutional layer, the features of all nodes in the hypergraph are aggregated through mean pooling to generate a global graph embedding vector, as shown in formula (3):

[0088]

[0089] in, It is the set of nodes in a hypergraph. It is the node υ after the Lth convolution layer. i Feature representation, It is the generated global graph embedding representation.

[0090] The generated global embedding z g The input is fed into the fully connected layer and the output layer. The probability that the graph belongs to API misuse is calculated by the sigmoid activation function, as shown in formula (4):

[0091]

[0092] Among them, W c This represents the weight matrix, where b is the bias term. It is the model's predicted label for the image.

[0093] During model training, the cross-entropy loss function is used to optimize the model. The loss function is shown in Equation (5):

[0094]

[0095] Where N is the number of samples, y i This is the true label of the i-th sample (1 indicates misuse, 0 indicates correct use). It is the model's predicted label for the i-th sample.

[0096] The hypergraph model in this invention is implemented using PyTorch. During the gradient descent learning process, the Adam optimizer is used to adaptively learn the trainable parameters. Specifically, the learning rate is set to 0.001, the initial value of the weight decay is 0.0001, and the number of iterations is 500.

[0097] Step S5: Convert the source code to be detected into a hypergraph structure and input it into the trained hypergraph neural network to obtain the API misuse detection results.

[0098] The code to be detected is obtained and converted into an API usage hypergraph. This code is then fed into a pre-trained hypergraph neural network. The hypergraph neural network performs feature aggregation on the nodes and edges in the hypergraph and analyzes whether the current code exhibits API misuse behavior based on the feature patterns learned during previous training, outputting the API misuse detection result.

Claims

1. A method for API misuse detection based on hypergraph neural network, characterized in that, The method comprises the following steps: Step S1, constructing a data set containing API correct use samples and misuse samples; Step S2, constructing an API use graph according to the abstract syntax tree structure of the source code; Step S3, generating an API use hypergraph based on the API use graph; Step S4, introducing a hypergraph attention mechanism, training a hypergraph neural network, and obtaining an API misuse detection model; Step S5, converting the source code to be detected into a hypergraph structure and inputting it into the trained hypergraph neural network to obtain an API misuse detection result; The API uses a graph definition as G b = (V b , E b ), where the node set V b includes action nodes and data nodes, i.e. API methods, operators and parameters, objects; the edge set E b represents the data flow and control flow relationship between node elements, including definition edges, parameter edges, call edges, thrown exception edges and sequential edges; The hypergraph is defined as Wherein represents a node set, and the API uses the node set V in the graph b Same; ε b represents a hyperedge set, including four groups of hyperedges, namely, call sequence hyperedges, exception handling hyperedges, conditional dependence hyperedges and parameter check hyperedges W is a diagonal matrix representing the weight of the hyperedge; The step S4 comprises: Step S4-1, using hypergraph for API of each node in the node set feature initialization; Step S4-2, inputting the node embedding vector and the hypergraph structure into the hypergraph neural network, introducing a hyperedge attention mechanism for training, and obtaining an API misuse detection model; The process of initializing the features of the nodes in the hypergraph is as follows: Given API uses a node set of a hypergraph For each node The extended context is input into the CodeBERT pre-training model, and an embedding vector of a fixed dimension is output through encoding Where d is the embedding dimension of the node If the context information corresponding to a node contains multiple parts, the embedding vectors of each part are aggregated by using the mean-pooling method, and the final embedding representation of the node is as follows: wherein, is a plurality of context segments for the node v i x i is the final aggregated embedding vector for the node. The API uses each node in the hypergraph, which is encoded as an embedding vector; the set of embedding vectors for all nodes is represented as the initial feature space X of the nodes. b ={x1,x2,…,x n }, where n is the total number of nodes. The feature vector for each node.

2. The method of claim 1, wherein, In the training process of the hypergraph neural network, the formula of the convolution transformation is as follows: where σ(·) is a nonlinear activation function ReLU(·), l e L is the number of layers of the hypergraph neural network, L is the total number of layers, which is 2 here, X (l) is the input node feature matrix of the lth layer, D v and D e respectively represent the diagonal matrix of the node degree and the hyperedge degree of the API using hypergraph, is the weighted hypergraph adjacency matrix, Θ (l) is the weight parameter of the lth layer node feature transformation.

3. The method of claim 1, wherein, During the propagation of node information, an attention mechanism is introduced for various types of hyperedges, node v i For a superedge of type t weight The calculation is shown below, t∈{1,2,3,4}: where LeakyReLU is a nonlinear activation function, denotes a hyperedge the set of nodes contained in the hyperedge, a t denotes an attention vector for hyperedges of type t; Through the super-edge attention mechanism, update the hypergraph association matrix H to generate the weighted association matrix where β is the attention weight matrix, and H is the original association matrix of the API use hypergraph.

4. The method of claim 1, wherein, In the training process of the hypergraph neural network, the loss function is used as the condition for judging whether the training is completed, and the loss function is as follows: where N is the number of samples, y i is the true label of the i-th sample, 1 indicates misuse, 0 indicates correct use, is the predicted label of the i-th sample by the model.

Citation Information

Patent Citations

  • API misuse detection method based on decision tree algorithm

    CN114153721A

  • API misuse detection method and system based on library source code and client source code mining

    CN115705290A