Distributed application intention consistency identification method and device, equipment and storage medium
By obtaining the source code and annotations of the smart contract, using the intermediate representation language and pre-trained embedding model to determine the consistency of smart contract intentions, the problem of poor universality and poor effect of intention recognition in the existing technology is solved, and a more efficient smart contract is achieved Code comment consistency detection.
Patent Information
- Application Number
- CN202411486017.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-23
- Publication Date
- 2024-12-31
- Estimated Expiration
- 2044-10-23
AI Technical Summary
The existing smart contract intent consistency recognition methods have poor universality and poor recognition effect, especially when the code is inconsistent with API documents, it is difficult to detect effectively. It is time-consuming and labor-intensive to formulate rules based on rules, and learning-based methods lack large-scale labeling. The data leads to poor results.
By obtaining the source code and annotations of distributed applications, generating annotation prompts, using the intermediate representation language to extract code features, and embed and transform the annotation and code through a pre-trained embedding model. Combining the random forest classifier to determine the consistency of intentions, two Stage pre-training and variation analysis optimization model.
It improves the universality and recognition effect of intention consistency recognition without considering the annotation type, and enhances the accuracy and efficiency of inconsistent detection of smart contract code annotation.
Smart Images

Figure CN119225708A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of consistency recognition technology, and in particular to a distributed application Figure 1 A method, device, equipment and storage medium for consistency recognition. Background Art
[0002] Blockchain is a secure shared data ledger that forms a continuous and tamper-proof data chain by distributing data on multiple nodes in the network, thereby achieving decentralized data management and transaction records. Blockchain technology has been continuously developing and has evolved from blockchain 1.0 to blockchain 2.0 and then to blockchain 3.0. Among them, smart contracts, as one of the representative technologies of blockchain 2.0, play an important role. The essence of a smart contract is a piece of code. Once the preset conditions are met, the contract will be automatically executed, and the execution process is irreversible. Smart contracts have been widely used to create distributed applications, including but not limited to game platforms, cryptocurrencies, and decentralized finance. With the development of blockchain technology and the popularity of distributed applications, the number of smart contracts is increasing day by day, and people are paying more and more attention to the security of smart contracts. In smart contracts, developers often use texts such as comments to describe their intentions and implement the corresponding code. However, the negligence or lack of experience of developers may cause the actual code logic to be inconsistent with the developer's true intentions. This inconsistency will affect the code reader's correct understanding of the code, and in serious cases, it will also lead to economic losses. Unfortunately, this inconsistency of intentions often does not cause runtime errors and is easily ignored.
[0003] Existing code intent inconsistency detection utilizes different code intent descriptions. For example, DAppHunter first triggers DApp to generate transactions by simulating front-end actions, and infers expected behaviors from the perspective of user intent. Then, it captures transaction data sent to the blockchain wallet and extracts behaviors from the perspective of transaction semantics. At the same time, it captures the actual behaviors of smart contracts by analyzing transaction execution logs. If any two of the above three behaviors do not match, it is detected as inconsistent. In addition, the prior art also proposes DocCon, which is used to detect inconsistencies between the API documentation of the Solidity smart contract library and the corresponding code. DocCon successfully detects multiple API documentation errors in the smart contract library, including parameter mismatches, missing requirements, and outdated descriptions. In addition, the prior art also proposes SmartCoCo, which defines rule templates, extracts annotation constraints and code facts through a series of propagation and binding strategies, and associates and compares them, thereby realizing the detection of three types of code annotation inconsistencies in smart contracts. This is also the only existing work on code annotation inconsistency detection in the field of smart contracts. There are also some intention inconsistency detection works based on non-smart contract languages and using annotations as intent descriptions, mainly including rule-based methods and learning-based methods. Rule-based methods use predefined rules, natural language processing, and static analysis techniques to detect inconsistent code comments; learning-based methods attempt to use machine learning techniques to detect inconsistent code comments.
[0004] However, in real scenarios, the code and its corresponding API documentation and views are usually not generated synchronously. In some cases, the code may not even have a corresponding API view, so it is necessary to make an intentional decision based on the DApp view and API documentation. Figure 1 Conformity testing is not always effective or feasible.
[0005] Existing rule-based methods usually focus on specific types of comments, which makes the formulated rules unable to be applied to detect inconsistent code comments of other types or other languages. Moreover, formulating rules requires a lot of analysis work, which is time-consuming and labor-intensive.
[0006] The effectiveness of existing learning-based methods depends on a large amount of labeled data, while the lack of large-scale labeled data in smart contracts reduces the effectiveness of existing learning-based methods and leads to poor consistency recognition results. Summary of the invention
[0007] The present invention provides a distributed application intention inconsistency identification method, device, equipment and storage medium, which are used to solve the existing intention Figure 1 The technical problems of poor universality and poor recognition effect of consistency recognition methods are solved.
[0008] The present invention provides a distributed application Figure 1Consistency identification methods include:
[0009] Obtaining source code of a distributed application and annotations of the source code;
[0010] Generate annotation prompts for the annotations;
[0011] Extracting code features of the source code through an intermediate representation language;
[0012] Converting the annotation prompt into an annotation embedding and converting the code feature into a code embedding through a pre-trained embedding model;
[0013] Determine the meaning of the distributed application according to the annotation embedding and the code embedding Figure 1 Consistency.
[0014] Optionally, the step of generating the annotation prompt of the annotation includes:
[0015] Get the function name of the function corresponding to the comment;
[0016] The function name and the comment are encapsulated as a comment prompt.
[0017] Optionally, the step of extracting code features of the source code by using an intermediate representation language includes:
[0018] Convert the source code into an intermediate representation code, and generate an abstract syntax tree of the intermediate representation code;
[0019] Obtaining a function call relationship of the intermediate representation code;
[0020] Expanding the abstract syntax tree using the function call relationship to obtain an extended abstract syntax tree, wherein the extended abstract syntax tree includes a plurality of nodes;
[0021] Extract the code element corresponding to each node at the position of the intermediate representation code;
[0022] Obtaining a built-in function of the intermediate representation function, and generating an operation code of the built-in function;
[0023] Obtaining edges of nodes in the extended abstract syntax tree, and extracting node numbers at both ends of the edge and the sequence number of the edge;
[0024] A code signature of the source code is generated using the position, the operation code, and the sequence number.
[0025] Optionally, the judgment of the meaning of the distributed application based on the annotation embedding and the code embedding is Figure 1 The steps to achieve this include:
[0026] Calculating a first similarity score between the comment embedding and the code embedding;
[0027] Concatenating the comment embedding, the code embedding, and the first similarity score to obtain a function feature;
[0028] The function features are input into a random forest-based classifier, and a judgment result of whether there is inconsistent intent is output.
[0029] Optionally, the training process of the pre-trained embedding model includes:
[0030] Obtain a code comment pair; the code comment pair includes a code sample and a comment sample;
[0031] Inputting the code sample into a first embedding model to be trained to obtain a code embedding result;
[0032] Inputting the annotation sample into a second embedding model to be trained to obtain an annotation embedding result;
[0033] The first embedding model to be trained and the second embedding model to be trained are adjusted by using the code embedding result, the comment embedding result and a preset first loss function to obtain a first-stage pre-trained code embedding model and a first-stage pre-trained comment embedding model;
[0034] generating mutation data of the code sample;
[0035] Input the code sample and the variant data into the first-stage pre-trained code embedding model respectively, and obtain the second-stage pre-trained code embedding model by combining with the preset second loss function training;
[0036] The first-stage pre-trained annotation embedding model and the second-stage pre-trained code embedding model are fine-tuned by presetting a third loss function to obtain a pre-trained embedding model.
[0037] The present invention also provides a distributed application Figure 1 The consistency recognition device comprises:
[0038] A source code and annotation acquisition module, used to acquire the source code of the distributed application and the annotations of the source code;
[0039] An annotation prompt generating module, used for generating annotation prompts for the annotations;
[0040] A code feature extraction module, used to extract code features of the source code through an intermediate representation language;
[0041] An embedding conversion module, configured to convert the annotation prompt into an annotation embedding and convert the code feature into a code embedding through a pre-trained embedding model;
[0042] meaning Figure 1 A consistency judgment module is used to judge the consistency of the distributed application according to the annotation embedding and the code embedding. Figure 1 Consistency.
[0043] Optionally, the annotation prompt generating module includes:
[0044] The function name acquisition submodule is used to obtain the function name corresponding to the annotation;
[0045] The comment prompt encapsulation submodule is used to encapsulate the function name and the comment into a comment prompt.
[0046] Optionally, the code feature extraction module includes:
[0047] An abstract syntax tree generation submodule, used for converting the source code into an intermediate representation code and generating an abstract syntax tree of the intermediate representation code;
[0048] A function call relationship acquisition submodule, used to acquire the function call relationship of the intermediate representation code;
[0049] An extension submodule, configured to extend the abstract syntax tree using the function call relationship to obtain an extended abstract syntax tree, wherein the extended abstract syntax tree includes a plurality of nodes;
[0050] A position extraction submodule, used to extract the position of the code element corresponding to each node in the intermediate representation code;
[0051] An operation code generation submodule, used for obtaining a built-in function of the intermediate representation function and generating an operation code of the built-in function;
[0052] A sequence number extraction submodule, used to obtain the edges of the nodes in the extended abstract syntax tree, and extract the node numbers at both ends of the edge and the sequence number of the edge;
[0053] A code feature generation submodule is used to generate a code feature of the source code by using the position, the operation code and the sequence number.
[0054] The present invention also provides an electronic device, the device comprising a processor and a memory:
[0055] The memory is used to store program code and transmit the program code to the processor;
[0056] The processor is used to execute the distributed application program according to the instructions in the program code. Figure 1 Consistency identification method.
[0057] The present invention also provides a computer-readable storage medium, wherein the computer-readable storage medium is used to store program code, wherein the program code is used to execute the distributed application program described in any one of the above items. Figure 1 Consistency identification method.
[0058] It can be seen from the above technical scheme that the present invention has the following advantages: the present invention obtains the source code and source code comments of the distributed application; generates comment prompts for the comments; extracts code features of the source code through an intermediate representation language; converts the comment prompts into comment embeddings through a pre-trained embedding model, and converts the code features into code embeddings; and determines the meaning of the distributed application according to the comment embeddings and the code embeddings. Figure 1 No need to consider the type of annotation, which improves the Figure 1 The universality and recognition effect of consistency recognition. BRIEF DESCRIPTION OF THE DRAWINGS
[0059] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. 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 paying creative labor.
[0060] Figure 1 A distributed application provided by an embodiment of the present invention Figure 1 Flow chart of the steps of the consistency identification method;
[0061] Figure 2 A flowchart of a method for identifying inconsistent distributed application intentions provided by another embodiment of the present invention;
[0062] Figure 3 This is a diagram of Yul AST extension based on function calls;
[0063] Figure 4 This is a one-hot encoding structure diagram of the built-in function based on the opcode;
[0064] Figure 5 Schematic diagram of training the embedding model;
[0065] Figure 6 A distributed application provided by an embodiment of the present invention Figure 1 Structural block diagram of the consistency recognition device. DETAILED DESCRIPTION
[0066] The embodiment of the present invention provides a distributed application intention inconsistency identification method, device, equipment and storage medium, which is used to solve the existing intention Figure 1The technical problems of poor universality and poor recognition effect of consistency recognition methods are solved.
[0067] In order to make the purpose, features and advantages of the present invention more obvious and easy to understand, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the embodiments described below 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 ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0068] See also Figure 1 , Figure 1 A distributed application provided by an embodiment of the present invention Figure 1 Flow chart of the steps of the consistency identification method.
[0069] A distributed application provided by the present invention Figure 1 The consistency identification method may specifically include the following steps:
[0070] Step 101, obtaining source code of a distributed application and annotations of the source code;
[0071] In smart contracts, developers often use comments and other text to describe their intentions and implement the corresponding code. However, due to negligence or lack of experience of developers, the actual code logic may not be completely consistent with the developer's true intentions. This inconsistency will affect the code reader's correct understanding of the code, and in serious cases, it will also lead to economic losses. Therefore, it is necessary to conduct consistency analysis on the source code and its corresponding comments.
[0072] Step 102, generating an annotation prompt for the annotation;
[0073] In the embodiment of the present invention, a specific template may be used to encapsulate annotation information to obtain annotation prompts.
[0074] Step 103, extracting code features of the source code through an intermediate representation language;
[0075] In the embodiment of the present invention, the code features of the source code may be extracted through the intermediate representation language Yul.
[0076] Yul is an intermediate representation language designed for the Ethereum virtual machine. It can be used directly to write smart contracts or as an intermediate representation for high-level programming languages such as Solidity. As an intermediate representation language, Yul supports a variety of smart contract programming languages, such as Solidity, Move, Flint, Logiken, Fe, etc.
[0077] Step 104, converting the annotation prompt into an annotation embedding and converting the code feature into a code embedding through a pre-trained embedding model;
[0078] In the embodiment of the present invention, the pre-trained embedding model may include a comment embedding model and a code embedding model; comment embedding may be generated by inputting comment prompts into the comment embedding model, and code embedding may be generated by inputting code features into the code embedding model.
[0079] In one example, the annotation embedding model can be CodeBERT. CodeBERT shows significant advantages in embedding natural language (such as annotations): on the one hand, CodeBERT uses the Transformer architecture and the self-attention mechanism to capture contextual information in the annotations; on the other hand, CodeBERT has been trained on a wide range of diverse datasets and has strong semantic representation capabilities, and can recognize and encode complex semantics in the annotations. By inputting the annotation prompts into the CodeBERT model, a high-dimensional annotation embedding with rich semantic information can be obtained.
[0080] In one example, the code embedding model may be a heterogeneous graph neural network model UniMP. UniMP can process node and edge features simultaneously and has a large number of parameters, so it can effectively extract and learn information in the graph. UniMP learns the features of nodes and edges in the graph by propagating and aggregating features in the graph to generate code embedding.
[0081] Step 105, judging the meaning of the distributed application according to the annotation embedding and the code embedding Figure 1 Consistency.
[0082] In the embodiment of the present invention, by analyzing the annotation embedding and the code embedding, it can be determined whether there is a problem of inconsistent intent in the distributed application.
[0083] The invention obtains source code and comments of the distributed application; generates comment prompts of the comments; extracts code features of the source code through an intermediate representation language; converts the comment prompts into comment embeddings through a pre-trained embedding model, and converts the code features into code embeddings; and determines the meaning of the distributed application according to the comment embeddings and the code embeddings. Figure 1 No need to consider the type of annotation, which improves the Figure 1 The universality and recognition effect of consistency recognition.
[0084] See also Figure 2 , Figure 2 A flowchart of a method for identifying inconsistent distributed application intentions provided by another embodiment of the present invention. Specifically, the following steps may be included:
[0085] Step 201, obtaining source code of a distributed application and annotations of the source code;
[0086] Step 202, obtaining the function name of the function corresponding to the annotation;
[0087] Step 203, encapsulating the function name and the comment into a comment prompt;
[0088] In a specific implementation, the function name of the function annotated with comments can be obtained, and the function name and the comment can be encapsulated into a comment prompt (Prompt).
[0089] The use of annotation prompts can effectively improve the performance of the model in natural language processing tasks and help to better understand the relationship between functions and annotations.
[0090] Step 204, extracting code features of the source code by using an intermediate representation language;
[0091] In the embodiment of the present invention, step 204 may include the following sub-steps:
[0092] S41, converting the source code into an intermediate representation code; and generating an abstract syntax tree of the intermediate representation code;
[0093] S42, obtaining a function call relationship of the intermediate representation code;
[0094] S43, using the function call relationship to expand the abstract syntax tree to obtain an extended abstract syntax tree, wherein the extended abstract syntax tree includes a plurality of nodes;
[0095] S44, extracting the position of the code element corresponding to each node in the intermediate representation code;
[0096] S45, obtaining a built-in function of the intermediate representation function, and generating an operation code of the built-in function;
[0097] S46, obtaining the edges of the nodes in the extended abstract syntax tree, and extracting the node numbers at both ends of the edges and the sequence number of the edges;
[0098] S47, generating a code feature of the source code using the position, the operation code and the sequence number.
[0099] In a specific implementation, the step of extracting code features of source code may include three steps, namely code conversion, AST expansion and feature extraction.
[0100] Among them, code conversion: first use the compiler to convert the source code into Yul code and its corresponding abstract syntax tree (AST). Yul AST is a tree structure used to represent the grammatical structure and hierarchical relationship of Yul code. It realizes the decoupling of syntax and semantics, in which each node represents a code structure independent of specific grammatical rules, which enhances the flexibility of code analysis and helps to better understand and capture the potential intentions in the code. The Yul AST generated by the compiler contains different types of nodes, such as YulCode, YulObject, YulIf, YulFunctionCall, YulFunctionDefinition, etc. Each node corresponds to a code element of different granularity and type in the Yul code.
[0101] AST extension: Considering that the function call in the code reflects the logic of the code, the embodiment of the present invention will extract the AST at the function level and expand and splice the function syntax tree according to the calling relationship between functions to obtain the extended abstract syntax tree Yul_AST+. The extended schematic diagram is shown in Figure 3 Specifically, since the called functions may also call other functions, unrestricted expansion may cause the AST+ to be too large. Therefore, the expansion of the AST is limited to M iterations. The specific value of M is determined by parameter experiments.
[0102] Feature extraction: The AST+ obtained by AST expansion contains multiple node types, and each type of node corresponds to code elements of different granularity. The types of edges connecting different node types are also different. The present invention extracts the code features of AST+:
[0103] For nodes, the position of the code element corresponding to the node in the Yul code is extracted as a feature to reflect the relationship between AST+ and Yul code. In addition, considering that some functions in the Yul code are built-in functions, these built-in functions implement underlying operations and logical calculations, corresponding to the opcodes in the EVM. Different built-in functions are associated with different logics, so a one-hot encoding is used to describe the opcode corresponding to each function and use it as a feature. Figure 4 A one-hot encoding diagram is shown, indicating that this built-in function corresponds to the AND opcode.
[0104] For edges, the node numbers at both ends of the edge and the sequence number of the edge starting from the parent node of the edge are extracted as features. These features reflect the relationship between different types of nodes and code elements.
[0105] Step 205, converting the annotation prompt into an annotation embedding and converting the code feature into a code embedding through a pre-trained embedding model;
[0106] In the embodiment of the present invention, the pre-trained embedding model may include a comment embedding model and a code embedding model; comment embedding may be generated by inputting comment prompts into the comment embedding model, and code embedding may be generated by inputting code features into the code embedding model.
[0107] In one example, in order to enable the embedding model to better learn and capture the semantics in the code and comments and generate higher quality data embedding, further model training is required. In view of the inconsistent characteristics of code comments in smart contracts, the present invention sets up a two-stage pre-training, including contrastive learning and variation analysis. After pre-training, the model is fine-tuned using labeled data. The schematic diagram of model training is shown in Figure 2. Figure 5 As shown. The following is a detailed description of the two-stage pre-training and model fine-tuning process. Specifically, it may include the following steps:
[0108] S51, obtaining a code comment pair; the code comment pair includes a code sample and a comment sample;
[0109] S52, inputting the code sample into a first embedding model to be trained to obtain a code embedding result;
[0110] S53, inputting the annotation sample into a second embedding model to be trained to obtain an annotation embedding result;
[0111] S54, using the code embedding result, the comment embedding result and a preset first loss function to adjust the first embedding model to be trained and the second embedding model to be trained to obtain a first-stage pre-trained code embedding model and a first-stage pre-trained comment embedding model;
[0112] In a specific implementation, the two-stage pre-training may include contrastive learning and variation analysis. Contrastive learning is widely used in the field of deep learning, which refers to achieving effective representation learning by comparing the relative relationship between samples. Specifically, in each batch, Code-annotation pairs are used for learning and training. code comment pairs, which we denote as ( ,in Represents the comment in the code comment pair. Indicates the code in this code comment pair. For each comment , and the matching positive sample is , and the remaining code is considered as a negative sample. The goal of this invention is to make As low as possible. To achieve this goal, we introduce a function called the first loss The loss function is used for pre-training of contrastive learning. The formula of the first loss function is as follows:
[0113]
[0114] in, and is the index of the code or comment in the code-comment pair, and No. Normalized embeddings of code and comments for code-comment pairs, is the total number of code-comment pairs.
[0115] S55, generating variation data of the code sample;
[0116] S56, inputting the code sample and the variant data into the first-stage pre-trained code embedding model respectively, and training with a preset second loss function to obtain a second-stage pre-trained code embedding model;
[0117] In the embodiment of the present invention, variation analysis is also performed on the code comment pairs to optimize the first-stage pre-trained embedding model to obtain a second embedding model.
[0118] The idea of mutation analysis comes from mutation testing commonly used in software testing. Mutation testing is a technique for evaluating the quality of a test suite by making small changes (mutations) to a software program. Considering that the code elements that cause inconsistent code comments may be very small, a model that detects inconsistent intent needs to have the ability to pay attention to these tiny elements. Therefore, a mutation analysis task is designed in the present invention to drive the model to capture subtle changes in the code. It includes two types of mutation operators: operator mutation and key code statement deletion mutation, which correspond to the operator level and statement level respectively.
[0119] Operator mutation refers to changing the built-in function related to the operation to the built-in function with the opposite logic, such as changing "add" to "sub" and changing "lt" to "gt". Operators include arithmetic operators and logical operators. Among them, arithmetic operators are the key to realizing smart contract business. For example, in smart contracts, transactions involve the increase or decrease of balances in different accounts, and token destruction involves the decrease of the number of tokens. Logical operators are often used in judgment statements, and their changes will directly affect the results of logical judgments.
[0120] Another type of mutation is the key code statement deletion mutation, which refers to deleting some key code statements. The logic contained in the key code statement usually directly corresponds to a part of the annotation. The absence of the key code statement is likely to cause inconsistent code annotations. Therefore, the present invention trains a key statement recognition model based on machine learning to complete the recognition of key statements, randomly delete key statements, and realize the key code statement deletion mutation. Specifically, the key statements in a large number of functions are marked, and the annotations, codes and corresponding labels are input into the classification model based on random forest. The classification model learns to fit the input data to realize the automatic classification of key statements.
[0121] The code data before mutation And the mutated code data The first stage pre-trained code embedding model is used for learning. The normalized embedding of the original data is represented as , the normalized embedding of the variant data is expressed as The optimization goal of the mutation analysis phase is to expand the gap between the original code and the mutation code. The preset second loss function used is for:
[0122]
[0123] in represents the number of variants, represents the probability of applying the softmax function to the similarity score between the original code embedding and itself, It is calculated The similarity scores of the variant code embeddings and the original code embeddings are applied The probability obtained by the function indicates the The possibility that the variant code data is consistent with the original code data.
[0124] The optimization goal is As high as possible, and As low as possible. Calculated The sum of the negative log-likelihood values of By minimizing , which can effectively separate the code before mutation And the mutated code The embedding distance between them enables the model to pay attention to small changes in elements.
[0125] S57, fine-tuning the first-stage pre-trained annotation embedding model and the second-stage pre-trained code embedding model by presetting a third loss function to obtain a pre-trained embedding model.
[0126] Model fine-tuning refers to the process of further training a pre-trained model using a new dataset to adapt it to a specific task or domain. During the fine-tuning process, some parameters of the embedded model are fixed so that the model maintains a certain stability during the fine-tuning process. Subsequently, inconsistency detection is performed based on the fine-tuning dataset, and the detection results are compared with the labels. In the fine-tuning stage, the third loss function used as follows:
[0127]
[0128] in, is the total amount of data, It is The label of the function (0 or 1), The model predicts The probability that the function is a positive sample.
[0129] It measures the distance between the predicted probability distribution and the true distribution of the label, by optimizing , it is possible to adjust the model parameters and improve its performance in the code comment inconsistency detection task.
[0130] Step 206, judging the meaning of the distributed application according to the annotation embedding and the code embedding Figure 1 Consistency.
[0131] In this embodiment of the present invention, step 206 may include the following sub-steps:
[0132] S61, calculating a first similarity score between the comment embedding and the code embedding;
[0133] Compared with consistent code comment pairs, the difference between comment embedding and code embedding is larger in inconsistent code comments, which is manifested as a larger distance between embeddings. Therefore, the similarity score SimScore between comment embedding and code embedding can be calculated using the following formula.
[0134]
[0135] in, and represents the embedding of code and comments, and Correspondence represents the normalized embeddings of code and comments.
[0136] The value range of is -1 to 1. The closer the result is to 1, the closer the direction of the code and the comment in the feature space is. When it is smaller, it means that the code and the comment are quite different in the feature space, in which case it is considered that there is an inconsistency in intent. In the present invention, a threshold is set (obtained through parameter experiments). When the code-comment similarity score of a function is less than the threshold, it is considered that the function has a code-comment inconsistency, and vice versa.
[0137] S62, concatenating the comment embedding, the code embedding and the first similarity score to obtain a function feature;
[0138] S63, input the function feature into a random forest-based classifier, and output a judgment result of whether there is an inconsistent intention.
[0139] In an embodiment of the present invention, a classifier based on random forest can be designed, and the function features obtained by splicing code embedding, comment embedding, and similarity scores are taken as input, and the judgment result of whether there is inconsistency is taken as output, so as to determine whether there is an intention inconsistency problem in the distributed application.
[0140] The invention obtains source code and comments of the distributed application; generates comment prompts of the comments; extracts code features of the source code through an intermediate representation language; converts the comment prompts into comment embeddings through a pre-trained embedding model, and converts the code features into code embeddings; and determines the meaning of the distributed application according to the comment embeddings and the code embeddings. Figure 1 No need to consider the type of annotation, which improves the Figure 1 The universality and recognition effect of consistency recognition.
[0141] See also Figure 6 , Figure 6 A distributed application provided by an embodiment of the present invention Figure 1 Structural block diagram of the consistency recognition device.
[0142] The embodiment of the present invention provides a distributed application Figure 1 The consistency recognition device comprises:
[0143] A source code and annotation acquisition module 601 is used to acquire the source code of a distributed application and the annotations of the source code;
[0144] An annotation prompt generating module 602, used to generate an annotation prompt for the annotation;
[0145] A code feature extraction module 603, used to extract code features of the source code through an intermediate representation language;
[0146] An embedding conversion module 604, configured to convert the annotation prompt into an annotation embedding and convert the code feature into a code embedding through a pre-trained embedding model;
[0147] meaning Figure 1 The consistency judgment module 605 is used to judge the consistency of the distributed application according to the annotation embedding and the code embedding. Figure 1 Consistency.
[0148] In the embodiment of the present invention, the annotation prompt generating module 602 includes:
[0149] The function name acquisition submodule is used to obtain the function name corresponding to the annotation;
[0150] The comment prompt encapsulation submodule is used to encapsulate the function name and the comment into a comment prompt.
[0151] In the embodiment of the present invention, the code feature extraction module 603 includes:
[0152] An abstract syntax tree generation submodule, used for converting the source code into an intermediate representation code and generating an abstract syntax tree of the intermediate representation code;
[0153] A function call relationship acquisition submodule, used to acquire the function call relationship of the intermediate representation code;
[0154] An extension submodule, configured to extend the abstract syntax tree using the function call relationship to obtain an extended abstract syntax tree, wherein the extended abstract syntax tree includes a plurality of nodes;
[0155] A position extraction submodule, used to extract the position of the code element corresponding to each node in the intermediate representation code;
[0156] An operation code generation submodule, used for obtaining a built-in function of the intermediate representation function and generating an operation code of the built-in function;
[0157] A sequence number extraction submodule, used to obtain the edges of the nodes in the extended abstract syntax tree, and extract the node numbers at both ends of the edge and the sequence number of the edge;
[0158] A code feature generation submodule is used to generate a code feature of the source code by using the position, the operation code and the sequence number.
[0159] In the embodiment of the present invention, Figure 1 The consistency judgment module 605 includes:
[0160] A first similarity score calculation submodule, configured to calculate a first similarity score between the annotation embedding and the code embedding;
[0161] A function feature generation submodule, used for concatenating the comment embedding, the code embedding and the first similarity score to obtain a function feature;
[0162] The judgment submodule is used to input the function features into a random forest-based classifier and output a judgment result of whether there is an inconsistent intention.
[0163] In an embodiment of the present invention, the training process of the pre-trained embedding model includes:
[0164] A code comment pair acquisition module, used to acquire a code comment pair; the code comment pair includes a code sample and a comment sample;
[0165] A code embedding result generating module, used for inputting the code sample into a first embedding model to be trained to obtain a code embedding result;
[0166] An annotation embedding result generating module, used for inputting the annotation sample into a second embedding model to be trained to obtain an annotation embedding result;
[0167] A first-stage pre-trained embedding model generation module, used to adjust the first embedding model to be trained and the second embedding model to be trained by using the code embedding result, the comment embedding result and a preset first loss function to obtain a first-stage pre-trained code embedding model and a first-stage pre-trained comment embedding model;
[0168] A variation data generation module, used to generate variation data of the code sample;
[0169] A second-stage pre-trained code embedding model generation module, used to input the code sample and the variant data into the first-stage pre-trained code embedding model respectively, and obtain the second-stage pre-trained code embedding model by combining the preset second loss function training;
[0170] A fine-tuning module is used to fine-tune the first-stage pre-trained annotation embedding model and the second-stage pre-trained code embedding model by presetting a third loss function to obtain a pre-trained embedding model.
[0171] An embodiment of the present invention further provides an electronic device, the device comprising a processor and a memory:
[0172] The memory is used to store program code and transmit the program code to the processor;
[0173] The processor is used to execute the distributed application program according to the instructions in the program code. Figure 1 Consistency identification method.
[0174] The embodiment of the present invention further provides a computer-readable storage medium, wherein the computer-readable storage medium is used to store program code, and the program code is used to execute the distributed application program described in the embodiment of the present invention. Figure 1 Consistency identification method.
[0175] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and units described above can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.
[0176] The various embodiments in this specification are described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same or similar parts between the various embodiments can be referenced to each other.
[0177] It should be understood by those skilled in the art that the embodiments of the present invention can be provided as methods, devices or computer program products. Therefore, the embodiments of the present invention can be in the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the embodiments of the present invention can be in the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0178] The embodiments of the present invention are described with reference to the flowcharts and / or block diagrams of the methods, terminal devices (systems), and computer program products according to the embodiments of the present invention. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of the processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing terminal device to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing terminal device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0179] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing terminal device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce a manufactured product including an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.
[0180] These computer program instructions can also be loaded onto a computer or other programmable data processing terminal device so that a series of operating steps are executed on the computer or other programmable terminal device to produce a computer-implemented process, thereby providing instructions for implementing the process in the computer or other programmable terminal device. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.
[0181] Although the preferred embodiments of the present invention have been described, those skilled in the art may make additional changes and modifications to these embodiments once they have learned the basic creative concept. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments and all changes and modifications that fall within the scope of the embodiments of the present invention.
[0182] Finally, it should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or terminal device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or terminal device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the existence of other identical elements in the process, method, article or terminal device including the elements.
[0183] As described above, the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit the same. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that the technical solutions described in the aforementioned embodiments may still be modified, or some of the technical features thereof may be replaced by equivalents. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A distributed application intent consistency identification method, characterized in that: include: Obtaining source code of a distributed application and annotations of the source code; Generate annotation prompts for the annotations; Extracting code features of the source code through an intermediate representation language; Converting the annotation prompt into an annotation embedding and converting the code feature into a code embedding through a pre-trained embedding model; The intent consistency of the distributed application is determined according to the annotation embedding and the code embedding.
2. The method according to claim 1, characterized in that The step of generating the annotation prompt of the annotation comprises: Get the function name of the function corresponding to the comment; The function name and the comment are encapsulated as a comment prompt.
3. The method according to claim 1, characterized in that The step of extracting the code features of the source code by using an intermediate representation language comprises: Convert the source code into an intermediate representation code and generate an abstract syntax tree of the intermediate representation code; Obtaining a function call relationship of the intermediate representation code; Expanding the abstract syntax tree using the function call relationship to obtain an extended abstract syntax tree, wherein the extended abstract syntax tree includes a plurality of nodes; Extract the code element corresponding to each node at the position of the intermediate representation code; Obtaining a built-in function of the intermediate representation function, and generating an operation code of the built-in function; Obtaining edges of nodes in the extended abstract syntax tree, and extracting node numbers at both ends of the edge and the sequence number of the edge; A code signature of the source code is generated using the position, the operation code, and the sequence number.
4. The method according to claim 1, characterized in that: The step of determining the consistency of the intention of the distributed application according to the annotation embedding and the code embedding includes: Calculating a first similarity score between the comment embedding and the code embedding; Concatenating the comment embedding, the code embedding, and the first similarity score to obtain a function feature; The function features are input into a random forest-based classifier, and a judgment result of whether there is inconsistent intent is output.
5. The method according to claim 1, characterized in that The training process of the pre-trained embedding model includes: Obtain a code comment pair; the code comment pair includes a code sample and a comment sample; Inputting the code sample into a first embedding model to be trained to obtain a code embedding result; Inputting the annotation sample into a second embedding model to be trained to obtain an annotation embedding result; The first embedding model to be trained and the second embedding model to be trained are adjusted by using the code embedding result, the comment embedding result and a preset first loss function to obtain a first-stage pre-trained code embedding model and a first-stage pre-trained comment embedding model; generating mutation data of the code sample; Input the code sample and the variant data into the first-stage pre-trained code embedding model respectively, and obtain the second-stage pre-trained code embedding model by combining with the preset second loss function training; The first-stage pre-trained annotation embedding model and the second-stage pre-trained code embedding model are fine-tuned by presetting a third loss function to obtain a pre-trained embedding model.
6. A distributed application intent consistency identification device, characterized in that: include: A source code and annotation acquisition module, used to acquire the source code of the distributed application and the annotations of the source code; An annotation prompt generating module, used for generating annotation prompts for the annotations; A code feature extraction module, used to extract code features of the source code through an intermediate representation language; An embedding conversion module, configured to convert the annotation prompt into an annotation embedding and convert the code feature into a code embedding through a pre-trained embedding model; An intent consistency judgment module is used to judge the intent consistency of the distributed application according to the annotation embedding and the code embedding.
7. The device according to claim 6, characterized in that The annotation prompt generation module includes: The function name acquisition submodule is used to obtain the function name corresponding to the annotation; The comment prompt encapsulation submodule is used to encapsulate the function name and the comment into a comment prompt.
8. The device according to claim 6, characterized in that The code feature extraction module comprises: An abstract syntax tree generation submodule, used for converting the source code into an intermediate representation code and generating an abstract syntax tree of the intermediate representation code; A function call relationship acquisition submodule, used to acquire the function call relationship of the intermediate representation code; An extension submodule, configured to extend the abstract syntax tree using the function call relationship to obtain an extended abstract syntax tree, wherein the extended abstract syntax tree includes a plurality of nodes; A position extraction submodule, used to extract the position of the code element corresponding to each node in the intermediate representation code; An operation code generation submodule, used for obtaining a built-in function of the intermediate representation function and generating an operation code of the built-in function; A sequence number extraction submodule, used to obtain the edges of the nodes in the extended abstract syntax tree, and extract the node numbers at both ends of the edge and the sequence number of the edge; A code feature generation submodule is used to generate a code feature of the source code by using the position, the operation code and the sequence number.
9. An electronic device, characterized in that: The device comprises a processor and a memory: The memory is used to store program code and transmit the program code to the processor; The processor is used to execute the distributed application intent consistency identification method described in any one of claims 1-5 according to the instructions in the program code.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium is used to store program code, and the program code is used to execute the distributed application intent consistency identification method described in any one of claims 1-5.
Citation Information
Patent Citations
Code segment annotation generation method and system and readable storage medium
CN113065322A
Code detection method and related equipment
CN117667183A
Automated test script generation from voice or speech in natural language based on code comments
IN201841002384A
Methods, systems, articles of manufacture and apparatus to identify code semantics
US20220107792A1
Cited By
Code annotation analysis method, computer equipment, storage medium and product
CN120122988A
Code comment analysis method, computer device, storage medium and product
CN120122988B
