API misuse detection method based on hypergraph neural network

By building the API to use hypergraphs and introducing hypergraph attention mechanisms, training hypergraph neural networks solves the limitations of existing API misuse detection methods in the problems of insufficient documentation, update lag and semantic capture, and more efficient and accurate API misuse detection is achieved.

CN119597522AActive Publication Date: 2025-03-11BEIJING UNIV OF TECH

Patent Information

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

AI Technical Summary

Technical Problem

The existing API misuse detection methods have limitations such as insufficient documentation, lagging document updates, difficulty in capturing semantic information and context in natural language processing, and the need for predefined thresholds for frequent usage patterns, which are difficult to effectively adapt in a rapidly changing software development environment.

Method used

Using the API misuse detection method based on hypergraph neural network, the API uses hypergraphs, introduces the hypergraph attention mechanism, trains the hypergraph neural network, generates the API misuse detection model, and then detects the API misuse behavior.

Benefits of technology

By capturing the high-order dependencies between API calls, parameter passing and control flow operations, the accuracy and comprehensiveness of API misuse detection are improved, and it can better adapt to complex API usage scenarios.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119597522A_ABST
    Figure CN119597522A_ABST
Patent Text Reader

Abstract

The invention relates to the field of software engineering, and discloses an API misuse detection method based on a hypergraph neural network in order to improve the accuracy of an API misuse detection result, which comprises the following steps of: S1, constructing a labeling code set containing an API correct use sample and a misuse sample; s2, constructing an API use graph according to the abstract syntax tree structure of the source code; s3, generating an API use hypergraph based on the API use graph; s4, introducing a hypergraph attention mechanism, and training a hypergraph neural network to obtain an API misuse detection model; and S5, converting a source code to be detected into a hypergraph structure, and inputting the hypergraph structure into the trained hypergraph neural network to obtain an API misuse detection result. According to the method, the hypergraph neural network is applied to API misuse detection, hyperedges are used for capturing high-order dependence and complex interaction relations in source codes, diversified API use modes can be processed, and the accuracy of a detection result can be improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of software engineering, and in particular to an API misuse detection method based on a hypergraph neural network. Background Art

[0002] In the field of modern software development, efficient reuse of existing software has become the cornerstone for developers to complete development tasks quickly and efficiently. In this process, the Application Programming Interface (API) plays a vital 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 use of APIs, such as confusing the calling order of certain functions or calling methods with incorrect parameters, which may lead to API misuse. API misuse not only poses security risks, but may also cause software errors and crashes. In addition, due to the wide variety of APIs, complex use, incomplete relevant documentation information, and delayed updates, developers face huge challenges in learning and applying APIs, making API misuse very common in software development. Therefore, accurate detection of API misuse is extremely important for improving the quality and efficiency of software development.

[0003] Currently, methods for automatically detecting API misuse in code snippets can be roughly divided into two categories. The first type of method mainly relies on natural language processing technology, which extracts API usage specifications by analyzing relevant API descriptive information such as reference documents and question-and-answer forums, or further constructs API constraint knowledge graphs for misuse detection. The second type of dependency pattern mining technology is to mine common API usage patterns from project source code by analyzing program dependency graphs, API call sequences, etc. This type of method usually assumes that any behavior that deviates from these frequent usage patterns may constitute potential API misuse.

[0004] Although existing methods have achieved certain results, they still have several limitations. First, many libraries lack detailed and high-quality documentation, from which it is difficult to extract API usage specifications. Secondly, document updates often lag behind the actual changes in API versions, and outdated document information may lead to incorrect misuse detection. Furthermore, natural language processing technology is difficult to fully capture semantic information and context when processing descriptive text, and the extracted API usage specifications may not be combined with actual development requirements. In addition, frequent usage patterns require predefined thresholds, so for newly released or less used APIs, frequent usage patterns cannot be mined due to the lack of sufficient usage data. Existing API misuse detection methods still need to be continuously improved to better adapt to the rapidly changing software development environment. Summary of the invention

[0005] Aiming at the limitations of existing API misuse detection methods, the present invention proposes an API misuse detection method based on a hypergraph neural network. The present invention is implemented through the following technical solutions:

[0006] Step S1, constructing a data set containing API correct usage samples and misuse samples;

[0007] Step S2: construct an API usage graph according to 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 result.

[0011] Furthermore, the API misuse detection method based on hypergraph neural network described in the present invention comprises:

[0012] The implementation process of step S1 is:

[0013] Step S1-1 builds a correct API usage code set from high-quality target API client code and its library source code collected from Github.

[0014] Search for the target API on the code management platform Github to find projects that frequently use these APIs, and filter out projects with a high number of favorites from the search results. Further filter the files in the project, filter out configuration definition files, and only retain .java files that contain one or more target APIs and have more than 100 lines of code. At the same time, since most files on Github are cloned files, a tag-based code cloning and deduplication method is used to avoid wasting time and space to store redundant information. In addition, for each target API, collect the source code files of the library to which it belongs. These source files are all open source and contain complete library code information.

[0015] In step S1-2, keywords are used to search and filter posts involving the target API in Github and Stack Overflow, extract misused code samples, and construct an API misuse code set.

[0016] Select open source projects in Github that contain the target API, especially those large projects that are active and well-maintained; use the API name as a keyword to search and filter the issue and pull request posts involving the target API, paying special attention to those labeled as bug, fix, error, etc. related to error fixes; extract code samples from the posts, and ensure that the obtained code blocks are large enough to include complete functions or classes to retain the contextual information of the API call. Similarly, use API name, misuse, error, exception, issue, bug, etc. as keywords to search for posts in Stack Overflow and extract API misuse code blocks. Merge the data collected from Github and Stack Overflow, clear duplicate and redundant data, and form an API misuse code set.

[0017] The target APIs are selected from the MUBench dataset widely used in the field of API misuse detection and the top 100 Java APIs discussed most frequently on the question-and-answer website StackOverflow. The most frequently discussed APIs on StackOverflow are usually APIs that are frequently used in actual development, and developers often have doubts about how to use them.

[0018] In step S2:

[0019] Due to the highly structured nature of the source code, the calling relationship between methods and the order of parameter passing are not intuitive, and it is difficult to directly analyze the source program. Therefore, static code analysis is performed on the constructed code set. First, an abstract syntax tree (AST) is generated based on the source code. Furthermore, given that API misuse mainly stems from factors such as improper method calling order, incorrect exception handling, missing pre / postconditions, and incorrect parameter values, a parsing structure is performed based on the AST, and relevant API usage and context information are extracted to construct an API usage graph (API Usage Graph, AUG). AUG is a directed graph with labeled nodes and edges, which is specifically used to represent the particularity of API usage. AUG is represented by G b =(V b , E b ), where the node set V b It includes action nodes and data nodes, namely API methods, operators and parameters, and objects. b 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] Since each edge in a normal graph is limited to connecting only two nodes, it can only express binary relationships between objects. In order to capture more complex relationships between objects, a hypergraph structure is introduced. As an extension of a normal graph, the edges in a hypergraph can be connected to any number of nodes, thus breaking through the limitations of binary relationships. Thanks to its flexible hyperedge design, a hypergraph can reveal high-order relationships that are difficult to display in a normal graph structure, making up for the shortcomings of a normal graph in expressing data correlations.

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

[0023] Specifically, the call order hyperedge is used to represent the sequential dependency between API calls. In the actual application of API, the order of method calls is crucial, and incorrect call order may lead to functional failure or security vulnerabilities. By extracting the sequential edge information in AUG, the API nodes that are called in sequence are connected together.

[0024] Secondly, the exception handling hyperedge is used to capture the association between the throwing and handling of exceptions during the API call process. To this end, the API node that throws the exception is associated with the operator node that catches the exception.

[0025] In addition, conditional dependency hyperedges are used to represent the necessary conditional dependencies before and after API calls. Resource preparation or status checks are often required before API calls. Similarly, cleanup or resource release operations are usually performed after the API call. Since there are complex dependencies between API calls and their pre- and post-conditions, by analyzing the definition edges, parameter edges, and control flow edges in the AUG, the pre- and post-dependent operations are connected to the API nodes through conditional dependency hyperedges, thereby providing higher-dimensional dependency information through the hypergraph.

[0026] Finally, the parameter check hyperedge is used to capture the parameter passing and usage in API calls. When multiple APIs pass, modify, and verify the same parameter, any irregular operation may cause abnormal system behavior or security vulnerabilities. Therefore, a parameter check hyperedge is designed to extract key information from the parameter edge 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 the present invention is defined as Juzhong Represents a node set, and the API uses the node set V in the graph bSame, ε b represents a hyperedge set, and W is a diagonal matrix representing hyperedge weights. The present invention does not consider the mutual influence between different hyperedges, so each hyperedge is assigned a weight value of 1.0. Since each hyperedge includes a set of adjacent nodes, all hyperedges can be constructed in one In the dimensional matrix H of , if the node v belongs to a hyperedge e, then h(v, e) = 1, otherwise it is 0.

[0028] The implementation process of step S4 is:

[0029] Step S4-1: Using Hypergraph for API Node set Each node in is initialized with features.

[0030] In order for the hypergraph neural network to fully learn the semantic information of the nodes, it is first necessary to generate an initial embedding vector for each node in the hypergraph node set used by the API. By expanding the context information of the node, combining its context in the code and inputting it into the pre-trained model, the embedding vector of the node is generated after encoding.

[0031] The CodeBERT pre-trained model is used. This model is trained based on large-scale code corpus and natural language data and can generate high-dimensional embedding representations of code snippets. Given an API, use the hypergraph node set For each node Taking the expanded context as input, the CodeBERT encoding will output an embedding vector of fixed dimension. Where d is the embedding dimension of the node. Through experimental analysis of the detection effect of the model under different embedding dimensions, it is found that when the node embedding dimension is 96, the effect on various data is the best.

[0032] If the context information corresponding to a node contains multiple parts, the embedding vector of each part is aggregated by average pooling to obtain the final embedding representation of the node:

[0033]

[0034] in, For node v i Multiple context fragments of x i is the final aggregate embedding vector of the node.

[0035] Through the above method, each node in the API usage hypergraph is encoded as an embedding vector. The set of embedding vectors of all nodes is represented as the node initial feature space X b ={x 1 , x 2, ..., x n}, where n is the total number of nodes, is the feature vector of each node, 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 ​​the hypergraph neural network is to represent the hypergraph data in matrix form and perform convolution operations to learn node representations through multiple convolutional layers to improve the model's expressive power. The hypergraph neural network performs feature smoothing on the convolutional layer using formula (1):

[0038]

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

[0040] The number of convolution layers of the hypergraph neural network is also one of the key factors affecting the performance of the model. The present invention experimentally analyzes the detection effect of the model under different numbers of convolution layers and determines that the effect is optimal when L=2.

[0041] Since different types of hyperedges contain different semantic information, the present invention introduces an attention mechanism for various types of hyperedges during the propagation of node information, and dynamically adjusts their influence in the propagation of node features by assigning different weights to hyperedges. i For a hyperedge of type t Weight The calculation of is shown in formula (2), t∈{1, 2, 3, 4}:

[0042]

[0043] Among them, LeakyReLU is a nonlinear activation function, N j t Represents a hyperedge The set of nodes contained in , α t Represents the attention vector of the hyperedge of type t.

[0044] Through the hyperedge attention mechanism, the hypergraph association matrix H is updated to generate a weighted association matrix Where β is the attention weight matrix and H is the original association matrix of the API usage hypergraph.

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

[0046]

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

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

[0049]

[0050] Among them, W c represents the weight matrix, b is the bias term, 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. The loss function is shown in formula (5):

[0052]

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

[0054] The hypergraph model in the present invention is implemented based on PyTorch, and the Adam optimizer is used to adaptively learn the trainable parameters during the gradient descent learning process of the model. 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 result.

[0056] Obtain the code to be tested, convert it into the form of an API usage hypergraph, and then input it into the trained hypergraph neural network. The hypergraph neural network will perform feature aggregation on the nodes and edges in the hypergraph, and analyze whether the current code has API misuse behavior based on the feature patterns learned in previous training, and output the API misuse detection results.

[0057] The beneficial effects of the present invention are:

[0058] Compared with the prior art, the present invention proposes an API misuse detection method based on a hypergraph neural network and an attention mechanism. By constructing an API usage hypergraph, the present invention captures high-order dependencies between API calls, parameter passing, and control flow operations. Unlike traditional graph structures that cannot express the relationship between multiple nodes, the present invention uses hyperedges to model the complex interactions between multiple nodes, and by introducing a hyperedge attention mechanism, the importance of different types of hyperedges is weighted, further improving the model's ability to handle complex API usage scenarios. In addition, the present invention performs feature learning on the API usage hypergraph through a hypergraph neural network, automatically extracts contextual dependencies and interaction information in API usage, and improves the accuracy and comprehensiveness of API misuse detection. BRIEF DESCRIPTION OF THE DRAWINGS

[0059] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the following briefly introduces the drawings required for use in the description of the embodiments. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.

[0060] Figure 1 A flow chart of a method for detecting API misuse based on a hypergraph neural network is provided in an embodiment of the present invention.

[0061] Figure 2 It is an API usage graph constructed according to the code snippet to be detected in an embodiment of the present invention.

[0062] Figure 3 Schematic diagram of a hypergraph convolutional layer in an embodiment of the present invention. DETAILED DESCRIPTION

[0063] In order to make the purpose and technical solution of the present invention clearer, the technical solution of the present invention will be clearly and completely described below in conjunction with the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative work are within the scope of protection of the present invention.

[0064] The embodiment of the present invention discloses a method for detecting API misuse based on a hypergraph neural network. Figure 1 As shown, including:

[0065] Step S1, constructing a labeled code set including API correct usage samples and misuse samples;

[0066] Step S2: construct an API usage graph according to the abstract syntax tree structure of the source code;

[0067] Figure 2 An example of converting a code snippet written by a developer into an AUG graph in the present invention is shown, wherein the square boxes in the graph represent action nodes, the elliptical boxes represent data nodes, the dotted lines represent data flow edges, and the solid lines represent control flow edges.

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

[0069] Based on the AUG obtained in step S2, further information is extracted, and the nodes in the AUG are used as nodes of the hypergraph. According to the association relationship between the nodes in the AUG, a set of hyperedges is constructed, including call order hyperedges, exception handling hyperedges, conditional dependency hyperedges, and parameter check hyperedges, so as to construct a hypergraph. in Represents a node set, and the API uses the node set V in the graph b Same; ε b represents a hyperedge set, W is a diagonal matrix representing hyperedge weights, and the present 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] Use the data to train the API misuse detection model based on the hypergraph attention mechanism to obtain the model parameters, including:

[0072] Step S4-1: Using Hypergraph for API Node set Initialize the features of each node in;

[0073] The set of nodes that a given API uses for a hypergraph For each node The expanded context is input into the CodeBERT pre-trained model, and a fixed-dimensional embedding vector is output through encoding. Where d is the embedding dimension of the node. In the model, the embedding dimension is a crucial parameter. If the embedding dimension is set too low, the distinction between nodes will be reduced, making the feature expression too coarse, and the model will find it difficult to capture sufficiently detailed node features, thus affecting the detection effect. On the contrary, too high an embedding dimension will increase the complexity of the model and easily lead to overfitting. In addition, high-dimensional embedding vectors will also increase the computational overhead of the model and affect the overall detection efficiency. Therefore, through experimental analysis of the detection effect of the model under different embedding dimensions, it is concluded that when the node embedding dimension is 96, the effect on each data is optimal.

[0074] If the context information corresponding to a node contains multiple parts, the embedding vector of each part is aggregated by average pooling to obtain the final embedding representation of the node:

[0075]

[0076] in, For node v i Multiple context fragments of x i is the final aggregate embedding vector of the node.

[0077] Through the above method, each node in the API usage hypergraph is encoded as an embedding vector. The set of embedding vectors of all nodes is represented as the node initial feature space X b ={x 1 , x 2 , ..., x n}, where n is the total number of nodes, is the feature vector of each node, d is the dimension of the embedding vector, and here d is 96.

[0078] 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;

[0079] The constructed API is input into the hypergraph neural network for training using the hypergraph and the node embedding vector obtained in step S4-1. The hypergraph neural network propagates and updates the node features in the hypergraph through multi-layer convolution operations, and each layer of convolution aggregates the node features through hyperedges. Figure 3 is a schematic diagram of the hypergraph convolutional layer, where υ 1 , 2 , ..., υ 8 represents a node in the hypergraph, e 1 , e 2 ,...e m represents a hyperedge, x 1 , x 2, ..., x n Represents the node features after hypergraph convolution. The convolution operation is shown in formula (1):

[0080]

[0081] Where σ(·) is the nonlinear activation function ReLU(·), l∈L is the number of layers of the hypergraph neural network, L is the total number of layers, W is the diagonal matrix representing the hyperedge weights. The present invention does not consider the mutual influence between different hyperedges, so each hyperedge is assigned a weight value of 1.0, and X l is the input node feature matrix of the lth layer, D v and D e The diagonal matrices representing the node degree and hyperedge degree of the hypergraph used by the API, is the weighted hypergraph incidence matrix, Θ l It is the weight parameter of the feature transformation of the l-th layer node.

[0082] In the process of node information propagation, the present invention introduces an attention mechanism for various hyperedges to dynamically adjust the influence of each hyperedge on node feature aggregation. i For a hyperedge of type t Weight The calculation of is shown in formula (2), t∈{1, 2, 3, 4}:

[0083]

[0084] Among them, LeakyReLU is a nonlinear activation function, N j t Represents a hyperedge The set of connected nodes, α t represents the attention vector of the hyperedge of type t, x i is the node v i The feature vector of .

[0085] Through the hyperedge attention mechanism, the attention weights between the hyperedge and the node are calculated, 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 of the hypergraph neural network is also one of the key factors affecting the performance of the model. The appropriate number of convolutional layers can effectively capture the complex interactions between nodes in the API usage hypergraph and improve the accuracy of the model's misuse detection. Fewer convolutional layers may not be able to fully capture the deep relationships across nodes, resulting in insufficient feature learning and affecting the detection effect. As the number of convolutional layers increases, the nonlinear expression ability of the model is enhanced, enabling it to better learn the complex contextual dependencies between nodes. However, too many convolutional layers may cause the model to be too complex, increase the risk of overfitting, and significantly increase the computational cost. Therefore, the present invention experimentally analyzes the detection effect of the model under different numbers of convolutional layers, and determines that the effect is optimal when L=2.

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

[0088]

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

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

[0091]

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

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

[0094]

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

[0096] The hypergraph model in the present invention is implemented based on PyTorch, and the Adam optimizer is used to adaptively learn the trainable parameters during the gradient descent learning process of the model. 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 result.

[0098] Obtain the code to be tested, convert it into the form of an API usage hypergraph, and then input it into the trained hypergraph neural network. The hypergraph neural network will perform feature aggregation on the nodes and edges in the hypergraph, and analyze whether the current code has API misuse behavior based on the feature patterns learned in previous training, and output the API misuse detection results.

Claims

1. A method for detecting API misuse based on hypergraph neural network, characterized in that: The following steps are involved: Step S1, constructing a data set containing API correct usage samples and misuse samples; Step S2: construct an API usage graph according to the abstract syntax tree structure of the source code; Step S3: Generate an API usage hypergraph based on the API usage graph; Step S4: introduce the hypergraph attention mechanism, train the hypergraph neural network, and obtain the API misuse detection model; 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 result.

2. The method according to claim 1, characterized in that The API uses a graph defined as G b =(V b , E b ), where the node set V b It includes action nodes and data nodes, namely API methods, operators and parameters, and objects; edge set E b Represents the data flow and control flow relationships between node elements, including definition edges, parameter edges, call edges, exception throwing edges, and sequence edges.

3. The method according to claim 1, characterized in that The hypergraph is defined as in Represents a node set, and the API uses the node set V in the graph b Same; ε b Represents a set of hyperedges, including call order hyperedges, exception handling hyperedges, conditional dependency hyperedges, and parameter checking hyperedges; W is a diagonal matrix representing the hyperedge weights.

4. The method according to claim 1, characterized in that: The step S4 comprises: Step S4-1: Using Hypergraph for API Node set Initialize the features of each node in; 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.

5. The method according to claim 4, characterized in that The process of initializing the features of nodes in the hypergraph is: The set of nodes that a given API uses for a hypergraph For each node The expanded context is input into the CodeBERT pre-trained model, and a fixed-dimensional embedding vector 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 vector of each part is aggregated by average pooling, and the final embedding of the node is expressed as follows: in, For node v i Multiple context fragments of x i is the final aggregate embedding vector of the node; Each node in the API usage hypergraph is encoded as an embedding vector; the set of embedding vectors of all nodes is represented as the node initial feature space X b ={x1, x2, ..., x n }, where n is the total number of nodes, is the feature vector for each node.

6. The method according to claim 1, characterized in that In the process of training the hypergraph neural network, the formula of convolution transformation is: Where σ(·) is the nonlinear activation function ReLU(·), l∈L is the number of layers of the hypergraph neural network, L is the total number of layers, which is 2 here, and X l is the input node feature matrix of the lth layer, D v and D e The diagonal matrices representing the node degree and hyperedge degree of the hypergraph used by the API, is the weighted hypergraph incidence matrix, Θ l It is the weight parameter of the feature transformation of the l-th layer node.

7. The method according to claim 1, characterized in that In the process of node information propagation, an attention mechanism for various hyperedges is introduced. i For a hyperedge of type t Weight The calculation of is as follows, t∈{1, 2, 3, 4}: Where LeakyReLU is a nonlinear activation function, N j t Represents a hyperedge The node set contained in , α t represents the attention vector of the hyperedge of type t; Through the hyperedge attention mechanism, the hypergraph association matrix H is updated to generate a weighted association matrix Where β is the attention weight matrix and H is the original association matrix of the API usage hypergraph.

8. The method according to claim 1, characterized in that In the process of training the hypergraph neural network, the loss function is used as a condition to determine whether the training is completed. The loss function is: Where N is the number of samples, y i is the true label of the i-th sample, 1 indicates misuse and 0 indicates correct use. is the model's predicted label for the i-th sample.

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

  • API misuse detection and correction method based on feedback mechanism

    CN116483700A

  • Hypergraph-based Android malicious software detection system and method

    CN117150489A

  • Defect prediction method and system based on hypergraph neural network

    CN117667730A

Cited By

  • Quantum error correction decoding method and decoder based on hypergraph contraction

    CN121390350A