File feature representation method based on dependency structure of c and c++ software
By constructing a hierarchical file dependency graph for C and C++ software and using graph representation learning techniques, the limitations of existing file feature representation techniques are addressed, resulting in more accurate file similarity assessment.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- NANJING UNIV
- Filing Date
- 2022-03-22
- Publication Date
- 2026-07-21
AI Technical Summary
Existing file feature representation methods based on text semantic analysis have limitations in C and C++ software, making it difficult to effectively extract file features, resulting in poor performance in downstream tasks such as similarity assessment.
By constructing a hierarchical file dependency graph for C and C++ software, sampling node sequences using a random walk strategy, and training a model using graph representation learning techniques, feature representations of files are obtained.
By effectively utilizing the dependencies between files, the consistency of vector representations among similar files is improved, thereby enhancing the performance of downstream tasks such as similarity assessment.
Smart Images

Figure CN114610321B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to dependency analysis and graph representation learning techniques for C and C++ software, specifically to a file feature representation method based on the dependency structure of C and C++ software, belonging to the fields of software engineering and graph representation learning. Background Technology
[0002] With the rapid development of software, its scale and complexity are expanding rapidly, leading to a surge in maintenance and ongoing development costs. A rational system structure is essential to improve software understandability and maintainability. To effectively organize and manage program files, developers need to identify the functions of different files and group files with similar functions into the same directory. This organization not only helps developers gain a macro-level understanding of the system structure but also facilitates root cause analysis when problems arise. However, due to the massive scale and high complexity of software, and the fact that software development often involves team collaboration, individual developers often struggle to accurately grasp the overall system structure and the functions of each file. Even experienced architects may overlook details. To effectively improve developers' efficiency in judging the functional similarity between different files, automated analysis techniques are needed to extract file features, and algorithms such as vector distance are used to evaluate the similarity between different files.
[0003] One type of technique for file feature extraction is based on understanding the function of a file using semantic knowledge. This type of technique treats files as text documents, utilizing textual content such as code, comments, and design documents, and employing natural language processing techniques to extract core semantic information, thereby obtaining feature vectors for each file. However, this method has limitations in C and C++ software applications. This is because some C and C++ files lack effective semantic information. On the one hand, due to macro processing characteristics, there is a discrepancy between the textual information in the source code and the content involved in the actual compilation process. On the other hand, some files lack effective textual content; for example, some public header files lack global symbols such as functions and variables, and some files lack comments, etc.
[0004] To address the problems in the aforementioned technologies, a novel file feature representation technique is needed. In practical software, files with similar functions often have close relationships, which is reflected in the file dependency graph. Similar files exhibit similarities in their local graph structures, providing a reasonable basis for file feature methods based on dependency structures.
[0005] LLVM is an open-source framework for program compilation and analysis launched in 2003 by a team at the University of Illinois. It provides an interface for static analysis of LLVM's intermediate representation code.
[0006] Clang is a front-end subproject of the LLVM project. It performs compilation analysis for C and C++ languages and provides a rich set of functional interfaces. Summary of the Invention
[0007] To address the shortcomings of existing technologies, this invention proposes a file feature representation method based on the dependency structure of C and C++ software. By analyzing dependencies in C and C++ software and using graph representation learning techniques, a file feature representation based on the dependency structure is obtained. This aims to solve the problem that existing text semantic-based feature representation methods have limitations in application scope.
[0008] To achieve the above objectives, the present invention adopts the following technical solution:
[0009] A method for representing file characteristics based on the dependency structure of C and C++ software, characterized in that the method includes the following steps:
[0010] Step 1) Use clang and LLVM analysis techniques to construct a hierarchical file dependency graph for C and C++ software;
[0011] Step 2) Using a biased random walk strategy, node sequence sampling is performed on the file dependency graph to obtain a set of file node sequences;
[0012] Step 3) Construct a graph representation learning model and train the model based on the set of node sequences to obtain the feature representation of the file.
[0013] The above-described file feature representation method based on the dependency structure of C and C++ software is characterized in that step 1) includes the following sub-steps:
[0014] Step 11) Use clang to analyze the #include statements in the code and extract header file dependencies;
[0015] Step 12) Use clang to compile the source files in the C and C++ software into intermediate representation files for LLVM;
[0016] Step 13): Use LLVM to analyze the intermediate representation file obtained in Step 12) and extract the symbol definitions and external symbol usage contained in the source file;
[0017] Step 14): Based on the use of external symbols in Step 13), locate other source files that define the corresponding external symbols, and construct source file dependencies accordingly;
[0018] Step 15): Based on the header file dependencies and source file dependencies extracted in Steps 11) and 15), construct a hierarchical file dependency graph for use as input in Step 2).
[0019] The above-described file feature representation method based on the dependency structure of C and C++ software is characterized in that step 2) includes the following sub-steps:
[0020] Step 21) Specify the step size ι of the random walk, the number of samples η at each node, and the hyperparameters p and q that control the probability of node selection;
[0021] Step 22), take a node from the file dependency graph as the starting node;
[0022] Step 23) Calculate the migration probability of each node among the direct neighbors of the current node, and randomly select a node as the next node to be visited.
[0023] Step 24), repeat step 23) until the node sequence length is equal to ι, and a node sequence is obtained;
[0024] Step 25), repeat steps 22)-24) until every node in the graph has been sampled;
[0025] Step 26), repeat steps 22) to 25) until the number of samples for each node in the graph reaches η;
[0026] Step 27) yields a set of node sequences, which are used as input for step 3).
[0027] The above-described file feature representation method based on the dependency structure of C and C++ software is characterized in that step 3) includes the following sub-steps:
[0028] Step 31) Construct a graph representation learning model and set the number of training rounds γ and the node context width ω;
[0029] Step 32): Take a node sequence from the set of node sequences output in step 2);
[0030] Step 33): Take one node in the node sequence as the center node, and obtain non-current nodes within a left and right length of ω as the context of that node;
[0031] Step 34): Based on the context of the node, use the hierarchical softmax algorithm to calculate the co-occurrence probability of each node in the context with the central node, then calculate the objective function and use the backpropagation algorithm to optimize the model.
[0032] Step 35), repeat steps 33)-34) until all nodes in the node sequence have been trained;
[0033] Step 36), repeat steps 32)-34) until all node sequences have been trained;
[0034] Step 37), repeat steps 32) to 36) until the number of training rounds satisfies γ.
[0035] The above-described file feature representation method based on the dependency structure of C and C++ software is characterized in that the formula for calculating the migration probability in step 23) is:
[0036]
[0037] Where E is the edge set of the dependency graph; Z is the normalization coefficient; π tx It is the unstandardized migration probability, defined as π. tx = α(v,x)*ω(t,x), since the file dependency graph is an unweighted graph, we have π tx =α pq (v,x), the α pq (v,x) is defined as follows:
[0038]
[0039] Where t is the current node of the random walk, and v is the last visited node. Adjusting the hyperparameters p and q can control the tendency of the random walk. The larger p is, the more the walk tends to explore outwards. The larger q is, the closer the exploration range is to the small local graph of the initial node.
[0040] The above-described file feature representation method based on the dependency structure of C and C++ software is characterized in that the input layer of the graph representation learning model in step 31) is the one-hot encoding of nodes, and the file dependency graph is defined as G = (V, E), where a certain file v i one-hot encoding v i A |V|-dimensional vector with the i-th bit set to 1 and the rest to 0, and the hidden layer is a matrix Z∈R. d×|V| It consists of |V| d-dimensional vectors, and its output is represented as φ(v i )=Zv i The output layer is the co-occurrence probability between node pairs calculated using the hierarchical softmax algorithm.
[0041] The above-described file feature representation method based on the dependency structure of C and C++ software is characterized in that the node context in step 33) is represented as N. S (v i ,ω)={v i-w ,…v i+w}\v i , where vi ω is the center node, and ω is the width of the two sides of the window.
[0042] The file feature representation method based on the dependency structure of C and C++ software described above is characterized in that the objective function in step 34) is defined as follows, based on the assumption of independent probability distribution between nodes:
[0043]
[0044] Since the traditional softmax algorithm requires computation on all nodes when calculating the cardinality, which is computationally expensive on large graphs, this step uses a hierarchical softmax algorithm for optimization. Specifically, this involves using Huffman coding to assign nodes to the binary tree based on their frequency of occurrence in the context, ensuring that nodes with higher frequency occurrences have shorter root paths. For example, to node v... j Path representation Where b0 is the root node. It is node v j ,have
[0045]
[0046] Each node in the binary tree can be viewed as a binary classifier, denoted as... Where ψ(b) k )∈R d It is node b k The hierarchical softmax algorithm has a computational complexity of O(log|V|), which is significantly lower than that of the traditional softmax algorithm. After calculating the objective function, the model is optimized using stochastic gradient descent and backpropagation algorithms. The optimization parameters are θ = {φ, ψ}.
[0047] Beneficial Effects: Existing file feature representation methods for C and C++ software mainly employ text semantic analysis techniques. These techniques have limitations in application scenarios and their performance in downstream tasks is not ideal. This method leverages the close dependencies between functionally similar files. Through dependency analysis of C and C++ software, a hierarchical file dependency graph is constructed, and graph representation learning techniques are used to obtain file feature encodings. This technique effectively utilizes the dependencies between files, resulting in more similar vector representations between closely related files, leading to better performance in downstream tasks such as similarity assessment. Attached Figure Description
[0048] Figure 1 This is a flowchart of the construction of the file dependency graph in this invention.
[0049] Figure 2 This is a flowchart of random walk sampling of the dependency graph in this invention.
[0050] Figure 3 This is a flowchart of the training graph representation learning model using a set of node sequences in this invention. Detailed Implementation
[0051] The technical solution of the present invention will be further described in detail below with reference to the accompanying drawings and embodiments.
[0052] Example: Figure 1 The following is a flowchart of the process for constructing a file dependency graph, given as input a C and C++ program and output a hierarchical file dependency graph. Specific steps are as follows:
[0053] Step 11) Use clang's preprocessor analysis interface InclusionDirective to capture the preprocessor's parsing of the #include statement during preprocessing, and obtain the file where the #include statement is located, as well as the file included by the statement.
[0054] Step 12) Based on the results of step 11), export the header file dependencies.
[0055] Step 13) Use clang to compile the source files in the C and C++ software into intermediate representation files for LLVM.
[0056] Step 14) Use LLVM's ModulePass interface to analyze the intermediate representation file obtained in Step 13). First, iterate through the global symbols in the intermediate representation file. If the symbol is defined in the current file or is only visible in the current file, skip the analysis of that symbol. Otherwise, determine whether the symbol is used in the current file, mainly whether a function is called or a variable is used. If so, persist the tuple between the source file corresponding to the intermediate representation file and the symbol used. Then, iterate through all symbol definitions in the intermediate representation file. If the symbol has external linkage, i.e., it is allowed to be used for relocation by other intermediate files during the compilation linking stage, persist the tuple between the source file and the symbol definition.
[0057] Step 15) Based on the symbol usage and symbol definition relationship in Step 14), locate the source file of the symbol being used, and export the dependency relationship between the source file using the symbol and the source file defining the symbol.
[0058] Based on the header file dependencies and source file dependencies extracted in steps 11) and 15), a hierarchical file dependency graph is constructed for use in... Figure 2 Input to the process.
[0059] Figure 2The following is the procedure for random walk sampling of the dependency graph in process 2), taking the file dependency graph as input and outputting a set of node sequences as output. Specific steps are as follows:
[0060] Step 21) Specify the step size ι of the random walk, the number of samples η at each node, and the hyperparameters p and q that control the walk strategy.
[0061] Step 22) Select a node from the file dependency graph as the starting node.
[0062] Step 23) Randomly select a node from the current node's direct neighbors as the next node to be visited. The migration probability of each node is calculated using the following formula: Where E is the edge set of the dependency graph, Z is the standardization coefficient, and π tx The unstandardized migration probability is defined as π. tx = α(v,x)*ω(t,x), since the file dependency graph is an unweighted graph, we have π tx =α pq (v,x). α pq (v,x) is defined as follows: Where node t is the current node and node v is the previously visited node, when traversing from the initial node, the node is selected from its neighbors with equal probability. The probabilistic selection process is implemented using the alias algorithm.
[0063] Step 24) Repeat step 23) until the node sequence length is equal to ι, and a node sequence is obtained.
[0064] Step 25) Repeat steps 22) through 24) until every node in the graph has been sampled by random walk.
[0065] Step 26) Repeat steps 22) through 25) until the number of times each node in the graph is sampled as the initial node reaches η.
[0066] Step 27) yields a set of node sequences for use in... Figure 3 The input for process 3) shown.
[0067] Figure 3 The following is the workflow for process 3) using a set of node sequences to train a graph representation learning model: Input the set of node sequences, output the feature representation of each node in the corresponding file. Specific steps are as follows:
[0068] Step 31) Construct a graph representation of the learning model and randomly initialize the matrices in the hidden layers of the model. Simultaneously, construct a binary tree using Huffman coding based on the frequency of node occurrences and assign nodes to leaf nodes, ensuring that nodes with higher frequency occurrences have shorter root paths. Also, specify the number of training rounds γ for the model.
[0069] Step 32) Take a node sequence from the set of node sequences output in process 2).
[0070] Step 33) Take one node in the node sequence as the center node, and obtain non-current nodes within a left and right length of ω as the context of that node.
[0071] Step 34) Take one node in the node sequence as the center node, and obtain non-current nodes within the left and right length range of ω as the context of the node. If the maximum left and right length does not meet the length ω, then do not take the context of the node.
[0072] Based on the context of each node, a hierarchical softmax algorithm is used to calculate the co-occurrence probability of each node in the context with the central node. Then, the objective function is calculated, and the model is optimized using backpropagation. Based on the assumption of probabilistic independence between nodes, the objective function is defined as:
[0073]
[0074] Where Pr(v) j |v i The hierarchical softmax algorithm is used for calculation. Assuming that in the binary tree constructed in step 31), the distance from the root node to node v is... j The path is b0 is the root node. For node v j ,but Each node in a binary tree can be viewed as a binary classifier, represented as... in For node b k The objective function is represented by a d-dimensional vector. After calculating the objective function, the model is optimized using stochastic gradient descent and backpropagation algorithms, with the optimization parameters being θ = {φ, ψ}.
[0075] Step 35) Repeat steps 33)-34) until all nodes in the node sequence are used as the central node for context and the model is trained.
[0076] Step 36) Repeat steps 32)-34) until all sequences in the node sequence have been trained.
[0077] Step 37) Repeat steps 32) through 36) until the number of training rounds of the model satisfies γ.
[0078] The above are merely preferred embodiments of the present invention. For those skilled in the art, any modifications or equivalent substitutions made to the present invention without departing from the spirit and principles thereof should be included within the scope of protection of the present invention.
Claims
1. A method for representing file characteristics based on the dependency structure of C and C++ software, characterized in that, include: Step 1) Construct a hierarchical file dependency graph: Use clang to extract header file dependencies, use llvm to perform symbol analysis to extract external symbol dependencies between source files, and construct a hierarchical file dependency graph based on the header file dependencies and source file dependencies. Step 2) Random walk sampling: Using a biased random walk strategy that controls the migration probability through hyperparameters p and q, node sequence sampling is performed on the file dependency graph to obtain a set of node sequences; the strategy includes specifying the walk step size l, the number of sampling times n, and hyperparameters p and q, and calculating the migration probability of the current node's neighbors based on p and q to select the next node; Step 3) Training the graph representation learning model: Construct a graph representation learning model, setting the number of training rounds γ and the width ω of the left and right sides of the node context window; based on the node sequence set, take one node sequence from the node sequence set, and then take one node from the node sequence as the center node. Obtain the nodes within each ω range on the left and right sides of its context window as context. Use the hierarchical softmax algorithm to calculate the co-occurrence probability of the center node and the context nodes, and optimize the model based on the objective function and backpropagation to obtain the feature representation of the file; wherein, the objective function is defined based on the independent probability distribution assumption between nodes as follows: , in, Indicates the current central node. Let i and j represent a node in the context. Since the traditional softmax algorithm requires calculation on all nodes when determining the cardinality, which is computationally expensive on large graphs, this step uses a hierarchical softmax algorithm for optimization. Specifically, this includes: using Huffman coding to assign nodes to the binary tree based on their frequency of occurrence in the context. For example, let's assume the node... Path representation ,in As the root node, For nodes Let k be the layer index of a node in the path, then: , In this binary tree, each node is considered a binary classifier, denoted as: ,in It is a node The computational complexity of hierarchical softmax is given by the d-dimensional vector representation. The computational complexity of traditional softmax is After calculating the objective function, the model is optimized using stochastic gradient descent and backpropagation algorithms. The optimization parameters are... .
2. The file feature representation method based on the dependency structure of C and C++ software according to claim 1, characterized in that, Step 1) includes the following sub-steps: Step 11) Use clang to analyze the #include statements in the code and extract header file dependencies; Step 12) Use clang to compile the source files in the C and C++ software into intermediate representation files for LLVM; Step 13): Use LLVM to analyze the intermediate representation file obtained in Step 12) and extract the symbol definitions and external symbol usage contained in the source file; Step 14): Based on the use of external symbols in Step 13), locate other source files that define the corresponding external symbols, and construct source file dependencies accordingly; Step 15): Based on the header file dependencies and source file dependencies extracted in Steps 11) and 15), construct a hierarchical file dependency graph for use as input in Step 2).
3. The file feature representation method based on the dependency structure of C and C++ software according to claim 1, characterized in that, Step 2) includes the following sub-steps: Step 21) Specify the step size of the random walk, the number of samples at each node, and the hyperparameters p and q that control the probability of node selection; Step 22), take a node from the file dependency graph as the starting node; Step 23) Calculate the migration probability of each node among the direct neighbors of the current node, and randomly select a node as the next node to be visited. Step 24), repeat step 23) until the node sequence length is equal to 𝜄, and a node sequence is obtained; Step 25), repeat steps 22)-24) until every node in the graph has been sampled; Step 26), repeat steps 22) through 25) until the number of samples for each node in the graph reaches 𝜂; Step 27) yields a set of node sequences, which are used as input for step 3).
4. The file feature representation method based on the dependency structure of C and C++ software according to claim 1, characterized in that, Step 3) includes the following sub-steps: Step 31) Construct a graph representation learning model and set the number of training rounds 𝛾 and the width 𝜔 of the left and right sides of the node context window; Step 32): Take a node sequence from the set of node sequences output in step 2); Step 33): Take one node in the node sequence as the center node, and obtain the non-current nodes within the width ω range on the left and right sides of the node context window as the context of the center node. Step 34): Based on the context of the node, use the hierarchical softmax algorithm to calculate the co-occurrence probability of each node in the context with the central node, then calculate the objective function and use the backpropagation algorithm to optimize the model. Step 35), repeat steps 33)-34) until all nodes in the node sequence have been trained; Step 36), repeat steps 32)-34) until all node sequences have been trained; Step 37), repeat steps 32) to 36) until the number of training rounds meets 𝛾.
5. The file feature representation method based on the dependency structure of C and C++ software according to claim 3, characterized in that, The formula for calculating the migration probability in step 23) is as follows: , Where E is the edge set of the dependency graph; Z is the standardization coefficient; It is the unstandardized migration probability, defined as Since the file dependency graph is an unweighted graph, there is The The definition is as follows: , Where t is the current node of the random walk, v is the last visited node, and x represents the target neighbor node for calculating the migration probability. The tendency of the random walk is controlled by adjusting the hyperparameters p and q. The larger p is, the more the walk tends to explore outwards. The larger q is, the more the walk tends to explore within the local graph structure centered on the initial node and within a limited distance range.
6. The file feature representation method based on the dependency structure of C and C++ software according to claim 4, characterized in that, The input layer of the graph representation learning model in step 31) is the one-hot encoding of nodes, and the file dependency graph is defined as follows. A certain document one-hot encoding The i-th bit is 1, and the rest are 0. 3D vector, hidden layer is matrix ,Depend on Composed of d-dimensional vectors, its output is represented as The output layer is the co-occurrence probability between node pairs calculated using the hierarchical softmax algorithm.
7. The file feature representation method based on the dependency structure of C and C++ software according to claim 4, characterized in that, The node context in step 33) is represented as follows: ,in This represents the i-th node in the node sequence, i.e., the selected center node, and s represents the index identifier of this node sequence. It represents the width of the left and right sides of the node context window.