Intelligent contract automatic repair method based on AST-T5 pre-training model
Through the automatic smart contract repair method based on the AST-T5 pre-trained model, the problems of low efficiency and high gas consumption in the existing technology are solved, and efficient and automated smart contract vulnerability repair is achieved, especially in complex code modification and reentry vulnerability repair.
Patent Information
- Application Number
- CN202510022008.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-01-07
- Publication Date
- 2025-04-15
AI Technical Summary
The existing smart contract vulnerability repair technology has problems such as low repair rate, explosive search space, and high gas consumption, especially for complex multi-line code modification and reentry vulnerabilities.
The smart contract automatic repair method based on the AST-T5 pre-trained model is adopted, and the model is fine-tuned to generate a function-level Solidity source code repair model, combining BPE word segmentation and bundle search algorithm to achieve automatic repair of smart contract vulnerabilities.
It improves the automation and efficiency of smart contract vulnerability repair, reduces the requirements for detection granularity, reduces the demand for training data volume, and improves the reentry vulnerability repair rate and gas consumption efficiency.
Smart Images

Figure CN119829101A_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the field of automatic repair of smart contract vulnerabilities in blockchain security, and relates to an automatic repair method for smart contracts based on an AST-T5 pre-training model. Background Art
[0002] Since smart contracts cannot be modified once deployed, it is very important to ensure the security of smart contracts themselves. There have been a lot of studies on smart contract vulnerability detection, but the work related to vulnerability repair has progressed slowly. Existing smart contract vulnerability repair technologies can be roughly divided into three categories:
[0003] Search-based repair technology, which usually uses heuristic strategies to build a patch space from previous patches and generates effective patches by searching the patch space. In 2020, Yu et al. proposed SCRepair, a repair technology based on heuristic search, which uses the fitness of the population in the genetic algorithm as a heuristic to promote the generation of effective patches. This technology can be regarded as the application of the GenProg algorithm in the traditional APR to smart contract vulnerability repair, and innovatively considers the gas consumption of the patch. However, this method has a low repair rate and suffers from the problem of search space explosion, which is inefficient.
[0004] Pattern-based repair technology, which mainly develops corresponding repair templates by manually analyzing the characteristics of different vulnerabilities, and then modifies the vulnerability code according to the template to achieve vulnerability repair. In 2024, Gao et al. proposed Sguard+, which is an improvement on the Sguard tool and also a pattern-based repair method. In addition to designing a vulnerability detection module that takes vulnerability prevention measures into account, they also improved the repair template in Sguard so that it can better repair integer overflow vulnerabilities and reentrancy vulnerabilities. Although Sguard+ performs well in repairing integer overflow vulnerabilities, reentrancy vulnerabilities and other vulnerabilities, it relies on fixed repair templates and cannot implement repair operations that require complex code changes. For example, its implementation of the CEI (Checks-Effects-Interactions) repair mode for reentrancy vulnerabilities is poor. In addition, it relies too much on the implementation of mutexes when repairing reentrancy vulnerabilities, and the gas consumption of generating patches is high.
[0005] A repair technology based on deep learning. This technology mainly regards the repair of vulnerabilities as a neural machine translation (NMT) task, that is, "translating" the vulnerability code into correct code. Its model structure is usually an encoder-decoder structure. The encoder converts the variable-length vulnerability code into a fixed-length context vector, and the decoder generates a prediction result based on the context vector output by the encoder. In 2023, Zhou et al. proposed SmartRep. They trained an encoder-decoder model to repair single-line vulnerabilities in smart contracts. The encoder of their model is a dual encoder structure, which is used to process a three-line context and the grammatical information obtained from the AST of the vulnerability function. Finally, SmartRep can repair more than 12 typical vulnerabilities. Although SmartRep supports multiple vulnerability types, it is limited to the repair of single-line vulnerabilities. The repair of many vulnerabilities cannot be achieved by simply modifying a single line of code. For example, it is not very appropriate to repair reentrancy vulnerabilities; in addition, the tool has a low degree of automation. Before repairing the vulnerability, it is necessary to accurately locate the vulnerability code line, which has high requirements for the granularity of vulnerability detection; finally, the tool has a low repair rate for several common vulnerabilities.
[0006] The present invention adopts a repair technology based on deep learning. By fine-tuning the AST-T5 pre-trained model, a function-level Solidity source code repair model is trained. Compared with the pattern-based repair method, the present invention uses a deep learning model to achieve more complex code modifications; compared with the existing deep learning method (the quality of the training data pulled from github cannot be guaranteed), the present invention uses a pre-trained model for fine-tuning, which requires less training data and only requires manual standardization of a small amount of data, alleviating the problem of smart contract vulnerabilities-missing repair data sets; in addition, the present invention is no longer limited to the repair of single-line vulnerabilities, and the structural perception characteristics of the AST-T5 pre-trained model can better generate multi-line code structures. Summary of the invention
[0007] The purpose of the present invention is to provide an automatic repair method for smart contracts based on the AST-T5 pre-trained model. To realize the automation of smart contract vulnerability repair, it is usually necessary to include three processes: vulnerability detection, patch generation, and patch verification. The present invention locates the vulnerability function by scheduling the existing detection tools; manually annotates the vulnerability-repair function pairs as training data to fine-tune the AST-T5 pre-trained model to repair vulnerabilities that require complex multi-line code modifications, wherein the BPE word segmentation technology is used to solve the problem of out-of-vocabulary, and the beam search algorithm is used to improve the quality of the model generation sequence; finally, the candidate repair function is verified and the effective repair function with the lowest gas consumption is output.
[0008] In order to achieve the above object, the technical solution of the present invention is as follows: a smart contract automatic repair method based on the AST-T5 pre-training model, the method comprising the following steps:
[0009] The training process of the model used to fix the vulnerable function,
[0010] The execution process of the entire smart contract automatic repair method.
[0011] The training process of the model used to repair the vulnerability function is as follows:
[0012] (1) Dataset construction,
[0013] The data set used in the present invention mainly has three sources, one is the data set on the open source website, including the smartbugs-curated data set on github and the reentrancy_solidity_function data set on huggingface, where smartbugs-curated is a complete vulnerability contract and reentrancy-solidity-function is a vulnerability function; the second is the data set in the existing smart contract repair paper, including the smartfix-benchmarks in the SmartFix paper and the PVD data set in the Sguard+ paper, both of which are complete vulnerability contracts; the last is a data set containing 47000+ real smart contracts in the real world, which includes both vulnerability contracts and security contracts. First, the Slither static analysis tool is used to detect 47000+ real contracts. Since Slither is a detection tool based on static analysis, it can quickly complete the detection of a large number of contracts. Due to the fact that the tool has many false positives, it is also necessary to manually check the detection results to filter out the real vulnerability functions. The data sets of several other complete vulnerability contracts are marked with vulnerability locations, and the vulnerability functions can be easily found. After obtaining the vulnerable function dataset, it is necessary to manually repair the vulnerable function and construct a vulnerability-repair function pair as the training data of the model. The following are several repair criteria for manual repair: change the code as little as possible, choose repair behaviors with less gas consumption as much as possible, and comply with the mainstream specifications of smart contract development as much as possible.
[0014] (2) Data preprocessing,
[0015] After obtaining the vulnerability-repair function dataset, data preprocessing is required. First, convert the vulnerability-repair function pair dataset in Excel format into csv format, and place the vulnerability function and repair function under the source and target column names respectively; then format the vulnerability function and repair function to remove the line breaks and extra spaces in them. The presence of these characters will affect the reasoning effect of the model, so that a standard dataset in csv format is obtained; finally, divide the dataset into training set, validation set and test set in a ratio of 6:2:2 for model training.
[0016] (3) BPE segmentation,
[0017] In order to deal with the problem in the traditional vocabulary method that the model cannot handle words that have not appeared in the training data (that is, the out-of-vocabulary problem), the present invention uses the BPE word segmentation technology to further divide the vocabulary into sub-words with smaller granularity, and covers more words through the combination of sub-words. At the same time, it can also reduce the size of the vocabulary and effectively reduce the complexity of the model.
[0018] (4) Model fine-tuning,
[0019] The present invention fine-tunes the AST-T5 model, finds the model with the smallest loss on the validation set by adjusting the training parameters, and evaluates the generalization ability of the model on the test set.
[0020] (5) beam search,
[0021] In the reasoning process of the model, the decoder does not directly output a complete text sequence, but generates a token each time. The decoder predicts the possibility of the next token based on the context vector output by the encoder and the currently selected token. The traditional greedy search algorithm selects the word with the highest probability as the output of the next step each time, which only considers the local optimum and does not necessarily obtain the global optimal result; while the exhaustive search tries all possible sequences and selects the sequence with the highest probability as the output. When the size of the vocabulary and the length of the sequence increase, the number of possible sequences will increase exponentially. The beam search method used in the present invention balances the search efficiency and quality. By setting the beam width, a certain number of optimal sequence candidates are always retained during the process of the decoder generating tokens. Each time a token is selected, a different token is added after the current sequence candidate, and the sequences with the largest probability of the sequence after adding the token are retained as the optimal sequence candidates for the next step. This avoids falling into the local optimal solution and does not cause the search space explosion problem. The final model will output the optimal sequence candidates with the beam width as the repair function candidates.
[0022] The execution process of the entire smart contract automatic repair method is as follows:
[0023] Step 1) Take the complete smart contract to be repaired as input. The preprocessor will first check the Solidity version of the smart contract. If the Solidity version is higher than 0.8.0, the detection and verification of integer overflow vulnerabilities will be skipped. This is because Solidity 0.8.0 and later versions introduced automatic protection for integer overflow. If it is still repaired, it will lead to redundant protection and increase the gas consumption of function calls. Then, the VaaS tool based on formal verification is used to detect integer overflow vulnerabilities, unchecked return value vulnerabilities, Suicide vulnerabilities, and tx.origin vulnerabilities. The deep learning-based TMP is used to detect reentrancy vulnerabilities and output a list of [{"vulnerabilities":["",""],"function_code":""},{}], where each element in the list represents a dictionary of vulnerable functions, where vulnerabilities represents the vulnerability type of the vulnerable function, and function_code represents the Solidity source code of the vulnerable function.
[0024] Step 2) Preprocess the vulnerable function, delete the line breaks and repeated spaces to get the text to be repaired, select the corresponding vulnerability repair model according to the vulnerability type in the dictionary, take the text to be repaired as input, and the model outputs the optimal sequence candidates. These optimal sequence candidates are several possible repair functions given by the model. Finally, insert line breaks and indents to get the candidate function set.
[0025] Step 3) Verify the candidate functions from high to low probability according to the decoder output. For each possible repair function in the candidate function set, replace the vulnerable function in the contract with it to obtain a possible repair contract; use Remix to compile the repair contract to be verified to determine whether the candidate function can be compiled. If not, verify the next candidate function; if the compilation passes, use VaaS and TMP to detect the corresponding vulnerability type to determine whether the vulnerability in the function has been repaired. If the above two steps of verification are passed, the current candidate function is directly used as the final repair result of the current vulnerability repair, and the complete contract applying the function is output to reduce the overall repair time.
[0026] Among them, for functions containing more than one vulnerability, after completing the repair of a vulnerability, the repaired vulnerability type is removed from the dictionary and the original vulnerability function code is replaced with the repaired function, and then the corresponding vulnerability repair model is called for repair and verification. This process is repeated until all vulnerabilities are repaired.
[0027] Among them, in the case where only the vulnerability function code is provided, the user needs to determine the type of vulnerability that needs to be repaired and select the corresponding vulnerability repair function to repair it. The model will output several possible repair functions. Due to the lack of complete contract code, it cannot be compiled and tested. The user needs to manually verify and select the possible repair functions.
[0028] The smart contract automatic repair model based on the AST-T5 pre-trained model is used to implement the smart contract automatic repair method based on the AST-T5 pre-trained model. The model includes a vulnerability detection and positioning module, a vulnerability function repair module, and a candidate function verification module.
[0029] Among them, the vulnerability detection and positioning module: The vulnerability detection and positioning module mainly uses a variety of existing smart contract vulnerability detection tools to detect vulnerabilities and locate vulnerable functions. According to the implementation technology of different vulnerability detection tools, corresponding tools are arranged to detect specific types of vulnerabilities. For example, the deep learning-based method has a higher detection accuracy for reentrancy vulnerabilities, while the formal verification-based method is good at detecting integer overflow vulnerabilities. Through the collaboration of multiple vulnerability detection tools, the target vulnerability function is located.
[0030] Vulnerable function repair module: The vulnerability function repair module mainly uses multiple fine-tuned AST-T5 models to repair vulnerable functions. When the vulnerable function is input, the BPE word segmentation technology is used to convert the function input sequence into a subword sequence, thereby solving the OOV (Out of Vocabulary) problem; when the model is output, the beam search algorithm is used to consider more sequence generation situations and output a series of high-quality candidate function sequences.
[0031] Candidate function verification module, the candidate function verification module is mainly used to verify a series of candidate function sequences output by the vulnerability function repair module. This module uses the Solidity compiler and the existing vulnerability detection tools to verify whether the candidate function is compilable and whether the vulnerability is repaired. Once a function that meets these two conditions is found, other candidate functions will no longer be verified to save repair time. Finally, the contract that replaces the vulnerable function with the function that meets the conditions is output to complete the entire repair process.
[0032] An electronic device comprises a memory, a processor and a computer program stored in the memory and executable on the processor, wherein when the processor executes the program, the automatic repair method for smart contracts based on the AST-T5 pre-training model is implemented.
[0033] A computer-readable storage medium having computer instructions stored thereon, which, when executed by a processor, implement the automatic repair method for smart contracts based on the AST-T5 pre-training model.
[0034] Beneficial effects: The present invention provides a complete set of automatic smart contract vulnerability repair processes to solve some problems of smart contract vulnerability repair based on deep learning and smart contract vulnerability repair based on traditional methods. Compared with the prior art, the method of the present invention has the following advantages:
[0035] (1) Low requirements for vulnerability detection granularity. The present invention only needs to provide the vulnerability type and the function where the vulnerability is located, and the model will automatically find the specific location of the vulnerability code and modify it. However, the current smart contract repair technology based on deep learning methods requires a single line of vulnerability code as input, which has a higher requirement for detection granularity.
[0036] (2) The present invention realizes a complete process of automatic repair of smart contracts with a high degree of automation. The present invention fully realizes the three common steps in automatic program repair: vulnerability detection, patch generation, and patch verification. When using it, the user only needs to input the contract to be repaired, and the present invention can provide the repaired contract. In addition, the present invention also supports users to only transmit the vulnerable function for repair.
[0037] (3) The present invention requires less training data. Compared with the current deep learning-based repair technology that uses mixed data pulled from GitHub to train the model, the use of the pre-trained model of the present invention allows users to complete model training by manually annotating only a small amount of data, and achieve better repair results for the corresponding vulnerability types.
[0038] (4) Compared with the pattern-based repair method, the present invention has better repair effects on some types of vulnerabilities. The present invention uses the ability of deep learning models to learn complex code modifications to better implement the CEI model of reentrancy vulnerability repair, and also alleviates the problems of over-repair and unnecessary repair in the pattern-based repair method to a certain extent. In terms of vulnerability repair rate, the present invention has a repair rate of 90.3% for reentrancy vulnerabilities, which is on par with the most advanced pattern-based repair technology Sguard+; in terms of gas consumption, the gas consumption of the reentrancy vulnerability patch generated by this method is much lower than the mutual exclusion lock method commonly used in the pattern-based repair method, which is specifically manifested in that the execution gas of the repair function is reduced by 83% and the transaction gas is reduced by 33%. In addition, the integer overflow vulnerability patch generated by this method also takes into account the conditional checks that have been implemented inside the function, which avoids excessive repair while also saving a lot of gas consumption. BRIEF DESCRIPTION OF THE DRAWINGS
[0039] Figure 1 It is a flow chart of the present invention. DETAILED DESCRIPTION
[0040] In order to deepen the recognition and understanding of the present invention, the technical solution of the present invention is described in detail below with reference to the accompanying drawings:
[0041] The automatic repair method for smart contracts based on the AST-T5 pre-trained model is mainly described in detail from two aspects. The first part is the training process of the model used to repair the vulnerability function in the present invention, and the second part is the execution process of the entire automatic repair method for smart contracts.
[0042] 1. Training process
[0043] (1) Dataset construction
[0044] The data sets used in the present invention mainly have three sources. The first is the data sets on open source websites, including the smartbugs-curated data set on github and the reentrancy_solidity_function data set on huggingface, where smartbugs-curated is a complete vulnerability contract and reentrancy-solidity-function is a vulnerability function; the second is the data sets in the existing smart contract repair papers, including the smartfix-benchmarks in the SmartFix paper and the PVD data set in the Sguard+ paper, both of which are complete vulnerability contracts; the last is a data set containing 47000+ real smart contracts in the real world, which includes both vulnerability contracts and security contracts. First, the Slither static analysis tool is used to detect 47000+ real contracts. Since Slither is a detection tool based on static analysis, it can quickly complete the detection of a large number of contracts. Since the tool has many false positives, it is also necessary to manually check the detection results to filter out the real vulnerability functions. The data sets of several other complete vulnerability contracts are marked with vulnerability locations, and the vulnerability functions can be easily found. After obtaining the vulnerable function dataset, it is necessary to manually repair the vulnerable function and construct a vulnerability-repair function pair as the training data for the model. The following are several repair criteria for manual repair: change the code as little as possible, choose repair behaviors that consume less gas as much as possible, and comply with the mainstream specifications of smart contract development as much as possible.
[0045] (2) Data preprocessing
[0046] After obtaining the vulnerability-repair function dataset, data preprocessing is required. First, convert the vulnerability-repair function pair dataset in Excel format into csv format, and place the vulnerability function and repair function under the source and target column names respectively; then format the vulnerability function and repair function to remove the line breaks and extra spaces in them, as the presence of these characters will affect the inference effect of the model, so that we can obtain a standard dataset in csv format; finally, divide the dataset into training set, validation set and test set in a ratio of 6:2:2 for model training.
[0047] (3) BPE segmentation
[0048] In order to deal with the problem in the traditional vocabulary method that the model cannot handle vocabulary that has not appeared in the training data (that is, the out-of-vocabulary problem), the present invention uses BPE word segmentation technology to further divide the vocabulary into sub-words with smaller granularity, and cover more vocabulary through the combination of sub-words. At the same time, it can also reduce the size of the vocabulary and effectively reduce the complexity of the model.
[0049] (4) Model fine-tuning
[0050] The present invention fine-tunes the AST-T5 model, finds the model with the smallest loss on the validation set by adjusting the training parameters, and evaluates the generalization ability of the model on the test set.
[0051] (5) Beam Search
[0052] In the reasoning process of the model, the decoder does not directly output a complete text sequence, but generates a token each time. The decoder predicts the possibility of the next token based on the context vector output by the encoder and the currently selected token. The traditional greedy search algorithm selects the word with the highest probability as the output of the next step each time, which only considers the local optimum and may not necessarily obtain the global optimal result; while the exhaustive search tries all possible sequences and selects the sequence with the highest probability as the output. When the size of the vocabulary and the length of the sequence increase, the number of possible sequences will increase exponentially. The beam search method used in the present invention balances the search efficiency and quality. By setting the beam width, a certain number of optimal sequence candidates are always retained during the process of the decoder generating tokens. Each time a token is selected, a different token is added after the current sequence candidate, and the sequences with the largest probability of the sequence after adding the token are retained as the optimal sequence candidates for the next step. This avoids falling into the local optimal solution and does not cause the search space explosion problem. Finally, the model outputs the optimal sequence candidates with the beam width as the repair function candidates.
[0053] 2. Execution process
[0054] Figure 1 The entire smart contract automatic repair process is demonstrated. The whole process can be divided into three steps: vulnerability function location, vulnerability function repair, and candidate function verification.
[0055] Step 1) Take the complete smart contract to be repaired as input. The preprocessor will first check the Solidity version of the smart contract. If the Solidity version is higher than 0.8.0, the detection and verification of integer overflow vulnerabilities will be skipped. This is because Solidity 0.8.0 and later versions introduce automatic protection for integer overflow. If it is still repaired, it will lead to redundant protection and increase the gas consumption of function calls. Then use the VaaS tool based on formal verification to detect integer overflow vulnerabilities, unchecked return value vulnerabilities, Suicide vulnerabilities, and tx.origin vulnerabilities. Use TMP based on deep learning to detect reentrancy vulnerabilities and output a list of [{"vulnerabilities":["",""],"function_code":""},{}], where each element in the list represents a dictionary of vulnerable functions, where vulnerabilities represents the type of vulnerability that exists in the vulnerable function, and function_code represents the Solidity source code of the vulnerable function.
[0056] Step 2) Preprocess the vulnerable function, delete the line breaks and repeated spaces to get the text to be repaired, select the corresponding vulnerability repair model according to the vulnerability type in the dictionary, take the text to be repaired as input, and the model outputs the optimal sequence candidates. These optimal sequence candidates are several possible repair functions given by the model. Finally, insert line breaks and indents to get the candidate function set.
[0057] Step 3) Verify the candidate functions from high to low probability according to the decoder output. For each possible repair function in the candidate function set, use it to replace the vulnerable function in the contract to obtain a possible repair contract; use Remix to compile the repair contract to be verified to determine whether the candidate function can be compiled. If it cannot be compiled, verify the next candidate function; if the compilation passes, use VaaS and TMP to detect the corresponding vulnerability type to determine whether the vulnerability in the function has been repaired. If the above two steps of verification are passed, the current candidate function is directly used as the final repair result of the current vulnerability repair, and the complete contract that applies the function is output to reduce the overall repair time.
[0058] (Additional step 1) For those functions that contain more than one vulnerability, after completing the repair of a vulnerability, remove the repaired vulnerability type in the dictionary and replace the original vulnerable function code with the repaired function, then call the corresponding vulnerability repair model to repair and verify, and repeat this process until all vulnerabilities are repaired.
[0059] (Additional step 2) In the case where only the vulnerability function code is provided, the user needs to determine the type of vulnerability that needs to be repaired and select the corresponding vulnerability repair function to repair it. The model will output several possible repair functions. Due to the lack of complete contract code, it cannot be compiled and tested. The user needs to manually verify and select the possible repair functions.
[0060] Example:
[0061] For the convenience of description, we assume the following simplified application example:
[0062] The preprocessor first checks the Solidity version of the contract. Here we assume that the Solidity version of the contract to be repaired is lower than 0.8.0, and the integer overflow vulnerability needs to be repaired. Then the vulnerability function location module calls the existing detection tool to detect the smart contract and output the vulnerability function information. The vulnerability function information output here is:
[0063] [{"vulnerabilities":["IOU"],"function_code":"
[0064] function Deposit()public payable{
[0065] balances[msg.sender]+=msg.value;
[0066] Log.AddMessage(msg.sender,msg.value,\"Put\");
[0067] }
[0068] "},{"vulnerabilities":["IOU","REN"],"function_code":"
[0069] function Collect(uint256_am)public payable{
[0070] if(balances[msg.sender]>=MinSum&&balances[msg.sender]>=_am){
[0071] if(msg.sender.call.value(_am)()){
[0072] balances[msg.sender]-=_am;
[0073] Log.AddMessage(msg.sender,_am,\"Collect\");
[0074] }
[0075] }
[0076] }
[0077] "}]
[0078] Here we take the repair of the Collect function as an example. From the above vulnerable function information, we can see that the Collect function has two types of vulnerabilities, namely integer overflow vulnerability and reentrancy vulnerability. We first repair the reentrancy vulnerability and remove the line breaks and repeated spaces in the vulnerable function code to get "function Collect(uint256_am)publicpayable{if(balances[msg.sender]>=MinSum&&balances[msg.sender]>=_am){if(msg.sender.call.value(_am)()){balances[msg.sender]-=_am;Log.AddMessage(msg.sender,_am,\"Collect\");}}}", which is passed as input to the reentrancy vulnerability repair model, and the model outputs five possible repair function text sequences. The format handler adds line breaks and indents to these function text sequences to make them conform to the function writing format of Solidity.
[0079] The candidate function verification module receives these five possible repair functions and replaces the vulnerable function in the original contract to obtain a possible repair contract; then the compiler is used to verify the compilability of the contract, and the vulnerability detection tool is used to verify whether the vulnerability is repaired. After verification, it is found that the first result in the possible repair function meets the conditions. At this point, the reentrancy vulnerability in the Collect function is repaired, and the vulnerability information of the Collect vulnerability function is updated to {"vulnerabilities":["IOU"],"function_code":"
[0080] function Collect(uint256_am)public payable{
[0081] if(balances[msg.sender]>=MinSum&&balances[msg.sender]>=_am){
[0082] balances[msg.sender]-=_am;
[0083] if(msg.sender.call.value(_am)()){
[0084] Log.AddMessage(msg.sender,_am,\"Collect\");
[0085] }
[0086] else{
[0087] revert();
[0088] }
[0089] }
[0090] }
[0091] "}
[0092] The repair process of the integer overflow vulnerability is the same as that of the reentrancy vulnerability, so it is omitted here.
[0093] Follow the above steps to fix the Deposit and Collect vulnerability functions in turn, and finally get a complete repaired contract.
[0094] It should be noted that the above embodiments are only preferred embodiments of the present invention. For ordinary technicians in this technical field, several improvements and equivalent substitutions can be made without departing from the principles of the present invention. These technical solutions after improvements and equivalent substitutions to the claims of the present invention all fall within the protection scope of the present invention.
Claims
1. A smart contract automatic repair method based on the AST-T5 pre-training model, characterized in that: The method comprises the following steps: The training process of the model used to fix the vulnerable function, The execution process of the entire smart contract automatic repair method.
2. According to claim 1, a smart contract automatic repair method based on the AST-T5 pre-training model is characterized in that: The training process of the model used to fix the vulnerability function is as follows: (1) Dataset construction, The datasets used come from three sources. The first is the datasets on open source websites, including the smartbugs-curated dataset on github and the reentrancy_solidity_function dataset on huggingface, where smartbugs-curated is a complete vulnerable contract and reentrancy-solidity-function is a vulnerable function; the second is the datasets in existing smart contract repair papers, including smartfix-benchmarks in the SmartFix paper and the PVD dataset in the Sguard+ paper, both of which are complete vulnerable contracts; the last is a dataset containing 47,000+ real-world smart contracts, including both vulnerable contracts and secure contracts. First, the Slither static analysis tool is used to detect 47,000+ real contracts. The detection results need to be manually checked to screen out the real vulnerable functions. The other datasets of several complete vulnerable contracts have the vulnerability locations marked. After obtaining the vulnerable function dataset, the vulnerable function needs to be manually repaired to construct a vulnerability-repair function pair as the training data for the model. (2) Data preprocessing, After obtaining the vulnerability-repair function dataset, data preprocessing is required. First, convert the vulnerability-repair function pair dataset in Excel format into csv format, and place the vulnerability function and repair function under the source and target column names respectively; then format the vulnerability function and repair function to remove the line breaks and extra spaces in them. The presence of these characters will affect the reasoning effect of the model, so that a standard dataset in csv format is obtained; finally, divide the dataset into training set, validation set and test set in a ratio of 6:2:2 for model training. (3) BPE segmentation, Using BPE word segmentation technology, the vocabulary is further divided into smaller sub-words. By combining sub-words, more vocabulary can be covered. At the same time, the size of the vocabulary can be reduced, effectively reducing the complexity of the model. (4) Model fine-tuning, Fine-tune the AST-T5 model, find the model with the smallest loss on the validation set by adjusting the training parameters, and evaluate the generalization ability of the model on the test set. (5) beam search, During the inference process of the model, the decoder does not directly output a complete text sequence, but generates a token each time. The decoder predicts the possibility of the next token based on the context vector output by the encoder and the currently selected token.
3. According to claim 1, a smart contract automatic repair method based on the AST-T5 pre-training model is characterized in that: The execution process of the entire smart contract automatic repair method is as follows: Step 1) Take the complete smart contract to be repaired as input. The preprocessor will first check the Solidity version of the smart contract. If the Solidity version is higher than 0.8.0, the detection and verification of integer overflow vulnerabilities will be skipped. If it is still repaired, it will lead to redundant protection and increase the gas consumption of function calls. Then, the VaaS tool based on formal verification is used to detect integer overflow vulnerabilities, unchecked return value vulnerabilities, Suicide vulnerabilities, and tx.origin vulnerabilities. The deep learning-based TMP is used to detect reentrancy vulnerabilities and output a list of [{"vulnerabilities":["",""],"function_code":""},{}], where each element in the list represents a dictionary of vulnerable functions, where vulnerabilities represents the vulnerability type of the vulnerable function, and function_code represents the Solidity source code of the vulnerable function. Step 2) Preprocess the vulnerable function, delete the line breaks and repeated spaces to get the text to be repaired, select the corresponding vulnerability repair model according to the vulnerability type in the dictionary, take the text to be repaired as input, and the model outputs the optimal sequence candidates. These optimal sequence candidates are several possible repair functions given by the model. Finally, insert line breaks and indents to get the candidate function set. Step 3) Verify the candidate functions from high to low probability according to the decoder output. For each possible repair function in the candidate function set, use it to replace the vulnerable function in the contract to obtain a possible repair contract; use Remix to compile the repair contract to be verified to determine whether the candidate function can be compiled. If not, verify the next candidate function; If the compilation is successful, VaaS and TMP are used to detect the corresponding vulnerability type to determine whether the vulnerability in the function has been fixed. If the above two-step verification is passed, the current candidate function is directly used as the final repair result of the current vulnerability repair, and the complete contract applying the function is output to reduce the overall repair time.
4. According to claim 3, a smart contract automatic repair method based on the AST-T5 pre-training model is characterized in that: For functions containing more than one vulnerability, after completing the repair of a vulnerability, remove the repaired vulnerability type in the dictionary and replace the original vulnerability function code with the repaired function. Then call the corresponding vulnerability repair model to repair and verify. Repeat this process until all vulnerabilities are repaired.
5. According to claim 3, a smart contract automatic repair method based on the AST-T5 pre-training model is characterized in that: In the case where only the vulnerability function code is provided, the user needs to determine the type of vulnerability that needs to be repaired and select the corresponding vulnerability repair model to repair it. The model will output several possible repair functions. Due to the lack of complete contract code, it cannot be compiled and tested, but the model will sort the candidate repair functions according to their likelihood. Users can verify and select the candidate repair functions from high to low likelihood, thereby speeding up the verification process.
6. The smart contract automatic repair model based on the AST-T5 pre-trained model is characterized by: A smart contract automatic repair method based on the AST-T5 pre-training model is used to implement any one of claims 1-5, wherein the model includes a vulnerability detection and positioning module, a vulnerability function repair module, and a candidate function verification module. Among them, vulnerability detection and positioning module: The vulnerability detection and positioning module mainly uses a variety of existing smart contract vulnerability detection tools to detect vulnerabilities and locate vulnerability functions. According to the implementation technology of different vulnerability detection tools, corresponding tools are arranged to detect specific types of vulnerabilities. Vulnerable function repair module, which mainly uses multiple fine-tuned AST-T5 models to repair vulnerable functions. When the vulnerable function is input, the BPE word segmentation technology is used to convert the function input sequence into a subword sequence, thereby solving the OOV (Out of Vocabulary) problem; when the model is output, the beam search algorithm is used to consider more sequence generation situations and output a series of high-quality candidate function sequences. Candidate function verification module, the candidate function verification module is mainly used to verify a series of candidate function sequences output by the vulnerability function repair module. This module uses the Solidity compiler and the existing vulnerability detection tools to verify whether the candidate function is compilable and whether the vulnerability is repaired. Once a function that meets these two conditions is found, other candidate functions will no longer be verified to save repair time. Finally, the contract that replaces the vulnerable function with the function that meets the conditions is output to complete the entire repair process.
7. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein: When the processor executes the program, the smart contract automatic repair method based on the AST-T5 pre-training model as described in any one of claims 1 to 6 above is implemented.
8. A computer-readable storage medium having computer instructions stored thereon, characterized in that: When the computer instruction is executed by the processor, the automatic repair method for smart contracts based on the AST-T5 pre-training model as described in any one of claims 1 to 6 is implemented.
Citation Information
Patent Citations
Block chain smart contract security detection method and device, and storage medium
CN116610326A
Attack detection and protection method for Ethereum-alliance chain heterogeneous cross-chain system
CN116800482A
Intelligent contract vulnerability repairing method based on pre-training and patch sorting technology
CN118821145A
Method and system for supporting smart contracts in a blockchain network
WO2022078632A1
Cited By
Intelligent contract vulnerability detection and repair system based on heterogeneous graph neural network
CN121543093A