A model training method, a program repair method, a device, a storage medium, and a program product

By constructing and training a target program repair model, and using reinforcement learning algorithms to automatically repair smart contracts, the problems of low efficiency and low accuracy in existing technologies are solved, and efficient and accurate smart contract repair is achieved.

CN122173394APending Publication Date: 2026-06-09CHINA MOBILE (SUZHOU) SOFTWARE TECH CO LTD +1

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
CHINA MOBILE (SUZHOU) SOFTWARE TECH CO LTD
Filing Date
2026-02-10
Publication Date
2026-06-09

AI Technical Summary

Technical Problem

Existing methods for repairing smart contracts are inefficient and inaccurate, mainly due to their reliance on human analysis.

Method used

By identifying abnormal subroutines and syntactic structure information in the sample program, an initial program repair model is constructed, and a target program repair model is obtained by training it using a target reinforcement learning algorithm. State, action, and reward information are set to automatically repair smart contracts.

Benefits of technology

It enables automated repair of smart contracts, improving repair efficiency and accuracy, and solving the problems of low efficiency and low accuracy of manual repair methods.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122173394A_ABST
    Figure CN122173394A_ABST
Patent Text Reader

Abstract

The application provides a model training method, which comprises the following steps: determining a sample abnormal subprogram in a sample program, and determining sample syntax structure information of the sample abnormal subprogram based on the sample abnormal subprogram; constructing an initial program repair model, and setting state information, action information and reward information for the initial program repair model; training the initial program repair model after the setting based on the sample abnormal subprogram and the sample syntax structure information by using a target reinforcement learning algorithm, to obtain a target program repair model, thereby solving the problems of poor efficiency and low accuracy of the related art in repairing the smart contract. The application also discloses a program repair method, an electronic device, a storage medium and a program product.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of smart contracts, and in particular to a model training method, a program repair method, a device, a storage medium, and a program product. Background Technology

[0002] With the development of technology, smart contracts have been widely used in crowdfunding, healthcare, and other fields. However, since smart contracts are difficult to modify once deployed, it is crucial to detect and fix vulnerabilities before deployment. Currently, the common practice is for experts to manually analyze smart contracts to identify and fix vulnerabilities. However, this manual approach to fixing smart contracts is not only inefficient but also has low accuracy. Summary of the Invention

[0003] To address the aforementioned technical problems, embodiments of this application provide a model training and program repair method, apparatus, device, storage medium, and program product, which solves the problems of low efficiency and low accuracy in related technologies for repairing smart contracts.

[0004] To achieve the above objectives, the technical solution of this application embodiment is implemented as follows: A model training method, the method comprising: Identify the sample exception subroutines in the sample program, and determine the sample syntax structure information of the sample exception subroutines based on the sample exception subroutines; Construct an initial program repair model, and set status information, action information, and reward information for the initial program repair model; Based on the sample anomaly subroutine and the sample syntax structure information, the initial program repair model is trained using a target reinforcement learning algorithm to obtain the target program repair model.

[0005] In the above scheme, setting state information, action information, and reward information for the initial program repair model includes: For the initial program repair model, a state space is set based on the sample abnormal subroutine and the sample syntax structure information; wherein, the state information includes the state space; Obtain multiple repair operations for historical programs, and set an action space based on the multiple repair operations; wherein, the action information includes the action space; Obtain the sample benchmark program corresponding to the sample program, and set a reward function based on the sample program, the repaired sample program, and the sample benchmark program; wherein, the reward information includes the reward function.

[0006] In the above scheme, setting the reward function based on the sample program, the repaired sample program, and the sample benchmark program includes: Based on the sample program, the sample benchmark program, and the repaired sample program, multiple sub-reward functions are set; wherein, the reward function includes the multiple sub-reward functions; Determine the weight of each sub-reward function; The reward function is set based on each sub-reward function and the weight of each sub-reward function.

[0007] In the above scheme, based on the sample program, the sample benchmark program, and the repaired sample program, multiple sub-reward functions are set, including: Based on the repaired sample program, the sample program, and the sample benchmark program, a sub-reward function is set for the accuracy of the repaired sample program; Based on the compilation results of the repaired sample program, a sub-reward function for the feasibility of the repaired sample program is set. Based on the first computing resources consumed by running the sample program and the second computing resources consumed by running the repaired sample program, a sub-reward function is set for the running efficiency of the repaired sample program.

[0008] In the above scheme, the step of setting a sub-reward function for the accuracy of the repaired sample program based on the repaired sample program, the sample program, and the sample benchmark program includes: The repaired sample program is processed using a target detection algorithm, and a first sub-reward function is set based on the detection results; Based on the first complexity of the repaired sample program and the second complexity of the sample benchmark program, a second sub-reward function is set; A third sub-reward function is set based on the similarity between the sample program and the repaired sample program.

[0009] In the above scheme, the step of training the initial program repair model using a target reinforcement learning algorithm based on the sample abnormal subroutine and the sample syntax structure information to obtain the target program repair model includes: The sample anomaly subroutine is serialized to obtain a sample subroutine sequence; The sample syntax structure information is processed using a preorder traversal algorithm to obtain a sample syntax structure sequence; Based on the sample subroutine sequence and the sample syntax structure sequence, the target reinforcement learning algorithm is used to train the initial program repair model after the settings are configured, so as to obtain the target program repair model.

[0010] A program repair method, the method comprising: Obtain the program to be repaired; The target program is obtained by using a target program repair model to repair the program to be repaired. The target program repair model is obtained by training using the model training method described above.

[0011] A model training device, the device comprising: The determining unit is used to determine the sample abnormal subroutines in the sample program, and to determine the sample syntax structure information of the sample abnormal subroutines based on the sample abnormal subroutines; A construction unit is used to construct an initial program repair model and set status information, action information, and reward information for the initial program repair model; The training unit is used to train the initial program repair model based on the sample abnormal subroutine and the sample syntax structure information using a target reinforcement learning algorithm to obtain the target program repair model.

[0012] A program repair device, the device comprising: The acquisition unit is used to acquire the program to be repaired. The processing unit is used to repair the program to be repaired using the target program repair model to obtain the target program; The target program repair model is obtained by training using the model training method described above.

[0013] An electronic device, characterized in that the device comprises: a processor, a memory, and a communication bus; The communication bus is used to realize the communication connection between the processor and the memory; The processor is used to execute programs in memory to implement the steps of the above-described model training method or the steps of the above-described program repair method.

[0014] A computer-readable storage medium storing one or more programs that can be executed by one or more processors to implement the steps of the model training method or program repair method described above.

[0015] A computer program product comprising a computer program that, when executed by a processor, implements the above-described method.

[0016] The model training method, program repair method, apparatus, device, computer-readable storage medium, and computer program product provided in this application can identify abnormal subroutines in a sample program and determine their sample syntax structure information based on the abnormal subroutines; construct an initial program repair model and set state information, action information, and reward information for the initial program repair model; and train the initial program repair model using a target reinforcement learning algorithm based on the abnormal subroutines and their sample syntax structure information to obtain a target program repair model. Thus, the target program repair model can be trained on the parameterized initial program repair model based on the abnormal subroutines and their sample syntax structure information in the sample program. The model can then deeply learn the feature information of the abnormal subroutines by analyzing them, thereby obtaining a target repair model that can accurately repair the program. Based on this target repair model, the program can be repaired. This not only allows for automatic program repair through the trained target repair model but also improves the accuracy of program repair, thereby solving the problems of low efficiency and low accuracy in related technologies for repairing smart contracts. Attached Figure Description

[0017] Figure 1 A schematic flowchart of a model training method provided for an embodiment of this application; Figure 2 A schematic diagram illustrating the process of training a target program to repair a model in an embodiment of this application; Figure 3 A flowchart illustrating a program repair method provided for an embodiment of this application; Figure 4 This is a schematic diagram of the structure of a model training device provided in an embodiment of this application; Figure 5 This is a schematic diagram of the structure of a program repair device provided in an embodiment of this application; Figure 6 This is a schematic diagram of the structure of a model training device provided in an embodiment of this application; Figure 7 This is a schematic diagram of the structure of a program repair device provided in an embodiment of this application. Detailed Implementation

[0018] The technical solutions in the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings.

[0019] It should be understood that the phrases "embodiments of this application" or "foreign embodiments" throughout the specification mean that a specific feature, structure, or characteristic related to an embodiment is included in at least one embodiment of this application. Therefore, "embodiments of this application" or "in the foreign embodiments" appearing throughout the specification do not necessarily refer to the same embodiment. Furthermore, these specific features, structures, or characteristics can be combined in any suitable manner in one or more embodiments. In the various embodiments of this application, the sequence numbers of the above-described processes do not imply a sequential order of execution; the execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of this application. The sequence numbers of the above-described embodiments are merely descriptive and do not represent the superiority or inferiority of the embodiments.

[0020] Unless otherwise specified, any step in the embodiments of this application performed by the electronic device may be executed by the processor of the electronic device. It is also worth noting that the embodiments of this application do not limit the order in which the electronic device performs the following steps. Furthermore, the methods used to process data in different embodiments may be the same or different methods. It should also be noted that any step in the embodiments of this application can be executed independently by the electronic device; that is, when the electronic device performs any step in the following embodiments, it may not depend on the execution of other steps.

[0021] It should be understood that the specific embodiments described herein are for illustrative purposes only and are not intended to limit the scope of this application.

[0022] This application provides a model training method, which can be applied to a model training device, as described above. Figure 1 As shown, the method may include the following steps: Step 101: Identify the sample exception subroutines in the sample program, and determine the sample syntax structure information of the sample exception subroutines based on them.

[0023] In this embodiment, the sample program can be a historical program used for model training, i.e., a program that has run over a period of time. In one possible implementation, the sample program can be a computer program of a smart contract, which is essentially code; that is, the sample program can also be called sample code.

[0024] In this embodiment, the sample exception subroutine can be a vulnerable portion of the sample program (i.e., sample code), and it can also be referred to as sample exception subcode. Specifically, the sample exception subroutine can be processed using an automated static analysis (SAST) algorithm or an automated dynamic analysis (DAST) algorithm to obtain the line of code in the sample code that contains the vulnerability. Then, the preceding and following lines of code in the sample program can be obtained, and these three lines of code can be combined to obtain the sample exception subcode (i.e., the sample exception subroutine).

[0025] It should be noted that a sample program (i.e., sample code) may contain multiple lines of vulnerable code. Each line of vulnerable code corresponds to a code combination, and each code combination is a sample exception subroutine. That is, a sample program may have only one sample exception subroutine or multiple sample exception subroutines.

[0026] In this embodiment, the sample anomaly subroutine can be data in text format. Specifically, an initial sample anomaly subroutine can be determined from the sample program first, and then its format can be converted to obtain a text-formatted sample anomaly subroutine.

[0027] In this embodiment of the application, after obtaining the sample exception subroutine, the solidity parser in the syntax parser can be used to process the sample exception subroutine to obtain the abstract syntax tree (AST), that is, the sample syntax structure information.

[0028] In one possible implementation, the parser can refer to another language recognition tool (ANTLR).

[0029] Step 102: Construct an initial program repair model and set status information, action information, and reward information for the initial program repair model.

[0030] In this embodiment, the initial program repair model can be an initially created, untrained model for program repair. In one possible implementation, the initial program repair model can refer to a Transformer model with a Structure Aware Sparse Attention (SASA) mechanism.

[0031] In this embodiment, the state information, action information, and reward information are the state information, action information, and reward information set for the initial program repair model in the network model corresponding to the target reinforcement learning algorithm. The initial program repair model is the agent in the target reinforcement learning algorithm.

[0032] It should be noted that the status information can be set based on the sample abnormal subroutine and AST (i.e., sample syntax structure information); the action information can be set based on multiple pre-set repair operations (i.e., multiple repair actions); and the reward information can be determined based on the sample program, the sample standard program corresponding to the sample program, and the repaired sample program.

[0033] Step 103: Based on the sample abnormal subroutines and sample syntax structure information, the initial program repair model is trained using a target reinforcement learning algorithm to obtain the target program repair model.

[0034] In this embodiment, the sample anomaly subroutine and AST can be used as input parameters to the configured initial program repair model. Specifically, the sample anomaly subroutine and AST can be input into the configured initial program repair model and processed using a target reinforcement learning algorithm to train the initial program repair model, thereby obtaining the target program repair model. The target program repair model is used to repair the code of a smart contract.

[0035] It should be noted that the target reinforcement learning algorithm can refer to an algorithm used for deep reinforcement learning; in one feasible implementation, the target reinforcement learning algorithm can be a policy gradient algorithm.

[0036] Based on the foregoing embodiments, in other embodiments of this application, step 102 can be implemented in the following ways: Step 102a: For the initial program repair model, set up the state space based on the sample abnormal subroutines and sample syntax structure information.

[0037] The state information includes the state space.

[0038] In this embodiment of the application, the sample exception subroutine and the sample syntax structure information can be serialized first and second, respectively, to obtain a first sequence and a second sequence. Then, the two sequences are converted into vectors. After that, the obtained vectors can be summarized and the state space S is set according to the summarized vectors.

[0039] Step 102b: Obtain multiple repair operations for the historical program and set the action space based on the multiple repair operations.

[0040] Among them, motion information includes motion space.

[0041] In this embodiment, the action space represents all actions that the agent can take; the historical program can refer to the code of a smart contract that has been run over a period of time; and multiple repair operations refer to multiple actions performed to repair the code. In one possible implementation, the multiple repair operations can be designed by considering the characteristics of smart contract vulnerabilities, taking into account traditional programming language repair actions, Solidity's official recommendations, and avoiding an excessively large action space that would lead to learning difficulties. Specifically, the action space can be as shown in Table 1 below.

[0042]

[0043] Table 1 Action Space It should be noted that the repair operations (also known as repair actions) contained in the action space are not static. If the number of vulnerabilities in the smart contract does not decrease or even increases after a certain repair action is performed, a negative reward will be configured for the agent to reject the action. Once the negative reward reaches a threshold, the repair action will be deleted.

[0044] In this application embodiment, a comprehensive list of repair operations (i.e. repair actions) can be designed based on domestic and international research and the characteristics of the vulnerability itself. This list can then guide the agent in the reinforcement learning algorithm to generate effective repair actions, thereby increasing the pertinence and accuracy of program repair.

[0045] Step 102c: Obtain the sample benchmark program corresponding to the sample program, and set the reward function based on the sample program, the repaired sample program, and the sample benchmark program.

[0046] The reward information includes the reward function.

[0047] In this embodiment, the sample benchmark program can refer to an accurate and error-free sample program, i.e., a sample program without any vulnerabilities; the repaired sample program can refer to a program repaired by the initial program repair model. It should be noted that the sample benchmark program and the repaired sample program are usually different. The sample benchmark program is a confirmed accurate and error-free sample program, while the sample program repaired by the initial program repair model usually still has vulnerabilities. However, in certain special cases (e.g., a smart contract with only one simple vulnerability), the repaired sample program is actually identical to the sample benchmark program.

[0048] In this embodiment of the application, multiple sub-reward functions can be set according to the sample program, the repaired sample program, and the sample benchmark program, and the weight of each sub-reward function can be determined. Then, a reward function can be set according to each sub-reward function and the weight of each sub-reward function.

[0049] In one feasible approach, the reward function can be formulated as shown in formula (1).

[0050] Formula (1) Where R(s,a,s') represents the reward function, which is the immediate reward obtained by transitioning from state s to state s' after performing action a; This represents the positive reward given when the reward function meets the corresponding condition; This represents a negative reward given when the reward function meets the corresponding conditions.

[0051] In this embodiment of the application, step 102c can be implemented in the following way.

[0052] Step 102c1: Based on the sample program, the sample benchmark program, and the repaired sample program, set up multiple sub-reward functions.

[0053] The reward function includes multiple sub-reward functions.

[0054] In this embodiment, multiple sub-reward functions can be set based on the sample program, the sample benchmark program, and the repaired sample program, considering the accuracy, feasibility, and operational efficiency of the repaired sample program. In one feasible approach, typically five sub-reward functions are set.

[0055] In this embodiment of the application, step 102c1 can be implemented in the following manner.

[0056] A1. Based on the repaired sample program, sample program, and sample benchmark program, set a sub-reward function for the accuracy of the repaired sample program.

[0057] In this embodiment, a first sub-reward function can be set based on the vulnerability detection results of the repaired sample program, a second sub-reward function can be set based on the complexity of the repaired sample program and the sample benchmark program, and then a third sub-reward function can be set based on the similarity between the repaired sample program and the sample program.

[0058] In the embodiments of this application, step A1 can be implemented in the following way.

[0059] a1. The target detection algorithm is used to detect and process the repaired sample program, and the first sub-reward function is set based on the detection results.

[0060] In this embodiment, the target detection algorithm may refer to a vulnerability detection algorithm, which is used to determine whether the patched sample program, i.e., the patched smart contract, still has vulnerabilities. The vulnerability detection algorithm may be Smartcheck or Oyente, or other tools; no specific limitation is made here.

[0061] In this embodiment, a vulnerability detection algorithm can be directly used to detect and process the patched sample program to obtain the detection result of the program vulnerability. Then, a first sub-reward function can be set based on the detection result. .

[0062] It should be noted that if the number of vulnerabilities in the patched sample program decreases or becomes zero (i.e., there are no vulnerabilities) after the application patching operation, a positive reward will be given. However, if the vulnerability detection algorithm exceeds the time limit or the number of vulnerabilities found does not decrease, a negative reward will be given.

[0063] a2. Based on the first complexity of the repaired sample program and the second complexity of the sample benchmark program, set the second sub-reward function.

[0064] In this embodiment, program complexity can refer to the code entropy of the program (i.e., code). It should be noted that the code entropy of a vulnerable program differs significantly from that of a program without vulnerabilities. For example, the code entropy of a vulnerability-free code segment (i.e., the program) is 4.38, while the code entropy of a vulnerable code segment is 3.27, a substantial difference.

[0065] In this embodiment, an N-gram language model can be used to divide the repaired sample program into multiple subsequences of length N. Then, the probability of each subsequence appearing in the entire repaired sample program can be calculated. Furthermore, the entropy value of each subsequence can be determined based on its probability, and the multiple entropy values ​​are summed to obtain the first complexity of the repaired sample program. In one possible implementation, N is 3.

[0066] The formula for calculating the first complexity is shown in formula (2).

[0067] Formula (2) in, Indicates the first complexity. This represents the probability of the i-th subsequence occurring, where i represents the number of subsequences.

[0068] Then, the second complexity of the sample benchmark procedure can be calculated using the same method and formula (2). Furthermore, the second sub-reward function can be set based on the first and second complexities. .

[0069] It should be noted that if the entropy value of the smart contract code after the repair operation is close to the entropy value of the smart contract code without the vulnerability (i.e., the difference between the two is less than or equal to the threshold), a positive reward will be given; otherwise (i.e., the difference between the two is greater than the threshold), a negative reward will be given.

[0070] a3. Set a third sub-reward function based on the similarity between the sample program and the repaired sample program.

[0071] In this embodiment, a word-to-vector (Word2Vec) model can be used to process the sample program and the repaired sample program respectively to obtain a first target vector and a second target vector. Then, the first target vector and the second target vector can be calculated according to the following formula (3) to obtain the cosine similarity between the two, that is, the similarity between the sample program and the repaired sample program. After that, a third sub-reward function can be set according to the similarity. .

[0072] Formula (3) in, Indicates similarity. Describes the first target vector. This represents the second target vector.

[0073] It should be noted that if the similarity is less than the threshold, a positive reward is given; otherwise, a negative reward is given.

[0074] A2. Based on the compilation results of the repaired sample program, set a sub-reward function for the feasibility of the repaired sample program.

[0075] In this embodiment, the compilation result of the repaired sample program refers to the compilation result of the smart contract code; the sub-reward function for the feasibility of the repaired sample program can become the fourth sub-reward function. The compilation results are used to determine whether the smart contract's code can run correctly.

[0076] In this embodiment of the application, if the compilation result indicates that the repaired sample program has been successfully compiled, that is, it can run normally, a positive reward will be given; otherwise, a negative reward will be given.

[0077] A3. Based on the first computing resources consumed by running the sample program and the second computing resources consumed by running the repaired sample program, set a sub-reward function for the running efficiency of the repaired sample program.

[0078] In this embodiment, the computational resources of a program can refer to the gas consumption of the code, that is, the computational resources required to run the code. Generally, the more computational resources required to run the code, the worse the running efficiency, while the less computational resources consumed, the higher the running efficiency.

[0079] Specifically, the Web3.py utility in the Python platform can be used to simulate the execution of the sample program and the repaired sample program to obtain the first and second computing resources. Then, based on the first and second computing resources, a sub-reward function, i.e., the fifth sub-reward function, can be set to optimize the running efficiency of the repaired sample program. .

[0080] It should be noted that if the second computing resource consumed by the repaired sample program is less than the first computing resource consumed by the sample program, a positive reward will be given; otherwise, a negative reward will be given.

[0081] Step 102c2: Determine the weight of each sub-reward function.

[0082] In this embodiment, the weights represent the importance of each sub-reward function. It should be noted that the sum of the weights of each sub-reward function is 1, and these weights can be set based on human experience.

[0083] Step 102c3: Set the reward function based on each sub-reward function and the weight of each sub-reward function.

[0084] In this embodiment of the application, each sub-reward function can be multiplied by the weight of each sub-reward function, and the resulting multiple products can be summed to obtain the reward function, i.e., the reward function R can be as follows.

[0085]

[0086] in, This represents the first weight of the first sub-reward function. This represents the second weight of the second sub-reward function. This represents the third weight of the third sub-reward function. This represents the fourth weight of the fourth sub-reward function. This represents the fifth weight of the fifth sub-reward function.

[0087] In this embodiment of the application, a multi-index reward function is designed by integrating multiple factors. This reward function provides a comprehensive evaluation standard for the repair of smart contracts, ensuring the effectiveness and feasibility of the repair strategy and the final repaired program in multiple dimensions.

[0088] In other embodiments of this application, step 103 can be implemented in the following ways.

[0089] Step 103a: Serialize the sample anomaly subroutine to obtain the sample subroutine sequence.

[0090] In the embodiments of this application, such as Figure 2 As shown, the sample abnormal subroutine (i.e., the specific three-line vulnerable code snippet) can first be converted into a token sequence, i.e., the first token sequence. Then, the code in the line containing the vulnerability can be converted into a token sequence in the same way to obtain the second token sequence. Specifically, the code snippet or line of code can first be segmented into tokens, i.e., the code is split into variable names, function names, words, identifiers, operators, etc. Each part can then be converted into its corresponding token, thus obtaining the final token sequence.

[0091] Subsequently, since the first token sequence of the three-line vulnerability code snippet is longer than the second token sequence of the code in the line containing the vulnerability, in order to ensure that the model can better understand the meaning and context of the code during training, a padding operation (i.e., using padding symbol 0) can be used to pad the shorter second token sequence to the same length as the longer first token sequence. Furthermore, the padded second token sequence can be concatenated with the first token sequence to form a complete third token sequence.

[0092] Step 103b: Process the sample syntax structure information using a preorder traversal algorithm to obtain the sample syntax structure sequence.

[0093] In the embodiments of this application, such as Figure 2 As shown, the preorder traversal algorithm can be used to process the sample syntax structure information, i.e., the abstract syntax tree (AST), to obtain the sample syntax structure sequence, i.e., the AST sequence.

[0094] Step 103c: Based on the sample subroutine sequence and sample syntax structure sequence, the initial program repair model is trained using a target reinforcement learning algorithm to obtain the target program repair model.

[0095] In the embodiments of this application, such as Figure 2As shown, a pre-set vocabulary can be obtained, and the sample subroutine sequence (i.e., the third token sequence) can be processed according to this vocabulary to map each token in the third token sequence to an identifier ID, thus obtaining an ID sequence. Then, the ID sequence and the AST sequence can be input into the Word2Vec model for vector transformation, thereby obtaining the sample subroutine vector of the sample subroutine sequence and the sample syntax structure vector of the sample syntax structure sequence. Subsequently, the sample subroutine vector and the sample syntax structure vector can be used as input parameters for the initial program repair model. That is, the sample subroutine vector and the sample syntax structure vector can be input into the initial program repair model and processed using a target reinforcement learning algorithm to train the initial program repair model, thereby obtaining the target program repair model. The target program repair model is used to repair the code of the smart contract.

[0096] In this embodiment, the vocabulary defines the correspondence between tokens and IDs. For example, if a token is StringLiteral, its corresponding ID can be set to 0; if a token is FunctionDeinition, its corresponding ID can be set to 1. Specifically, the category attributes (such as variable declarations, function definitions, and other syntactic units) and their specific data types (such as strings, booleans, hexadecimal numbers, etc.) of each identifier in the AST sequence can be extracted using a Solidity parser. Then, an ID is set for each category attribute and each data type. For example, the FunctionDeinition is set to ID 1. Furthermore, the top 300 most frequently occurring category attributes and data types can be statistically analyzed and included in a table along with their corresponding IDs to obtain the vocabulary. This allows the model to better understand the syntactic information of the code, thereby enabling more accurate program repair.

[0097] It should be noted that, as Figure 2As shown, the target program repair model can be a Transformer model with a SASA mechanism. The SASA mechanism can sparsify the computation of the self-attention mechanism and combine it with the structural characteristics of the code. This not only improves the performance of long sequence tasks but also helps to reduce memory usage and computational complexity. Its core idea is to combine the structural information of the sequence to guide the attention distribution so as to avoid the entire learning process of the model being interfered with by irrelevant or redundant information. Specifically, in the multi-head attention layer of the Transformer, the original self-attention mechanism is replaced with a sparse attention mechanism that integrates structural information. In this way, when calculating the attention score, the structural guidance matrix can be multiplied with the original attention score matrix, so that the model can ignore the attention between non-structurally related position pairs, thereby reducing invalid attention computation. In one feasible way, the attention calculation formula is shown in the following formula (4).

[0098] Formula (4) Where Q is the query matrix, K is the key matrix, and V is the value matrix. Here, S represents the dimension of the key vectors, and S is the structural guidance matrix. It's important to note that the structural guidance matrix S is constructed based on the input AST sequence, reflecting the structural relationships between code elements. Through this attention calculation method, the model can effectively process and understand the source code and its AST representation. This preserves the importance of code structure while reducing attention allocation to irrelevant locations, thereby improving the efficiency and accuracy of code sequence modeling.

[0099] In the embodiments of this application, the execution process of the Transformer model can be regarded as a Markov Decision process (MDP) in continuous states.

[0100] In this embodiment, the sample subroutine vector and sample syntax structure vector, i.e., the state space S, can be used as input to the agent of the reinforcement learning algorithm, i.e., the policy gradient algorithm. Then, the encoding module in the agent (i.e., the Transformer model with SASA mechanism) can encode the input vector to obtain the encoded vector. Then, the decoding module in the agent can output multiple candidate repair actions based on the input encoded vector. Further, the policy gradient algorithm calculates the probability of each candidate repair action and selects the candidate repair action with the highest probability as the optimal repair action. Then, the optimal repair actions output at multiple different times can be summarized to obtain an action sequence. Further, the reward function R of the entire policy gradient algorithm can be calculated after executing the repair actions in the action sequence, and the objective function can be determined based on the reward function R. Then, the parameters of the agent can be optimized based on the objective function, i.e., the parameters of the Transformer model, to guide the agent to learn to recommend correct and effective repair actions. After that, the adjusted agent can continue to process the input sample subroutine vector and sample syntax structure vector. This training process continues until the value of the objective function meets the condition, and then the loop stops, thereby obtaining the target program repair model.

[0101] In this embodiment of the application, the objective function takes the form of: ;in, The objective function is... These are the parameters that need to be adjusted for the Transformer model; This represents a series of state and action pairs determined by the model parameters θ; This represents the probability of an action; T is the total number of time steps in the decision-making cycle. It is the immediate reward obtained by transitioning from state s to state s′ after performing action a in state s at time step t. It should be noted that... It can be calculated through step 102c.

[0102] In this embodiment, the objective function is maximized if it satisfies the condition. Specifically, an Epsilon-Greedy strategy can be used to maximize the objective function. This strategy strikes a balance between a greedy strategy (always choosing the current best action) and random exploration, i.e., at each decision moment, the agent will choose the action with probability 1- Select the currently estimated optimal action, and with probability The exercise is randomly selected for exploration, and as the training progresses, As the value gradually decreases, the exploration frequency will decrease accordingly, thereby improving the efficiency of utilizing the known optimal strategy.

[0103] After applying the Epsilon-Greedy policy, although the form of the objective function is adjusted to suit the exploration part, the basic idea is still to optimize the model by increasing the probability of high-reward actions. Therefore, when updating parameters, in order to optimize the objective function, the principle of policy gradient can be followed, and the parameters of the Transformer model can be iteratively updated through the gradient ascent algorithm. The iteration function can be shown in the following formula (5).

[0104] Formula (5) Where k is the number of iterations. and These represent the current and the model parameters after the next update, respectively. It's the learning rate, which controls the step size of parameter updates. The objective function J is about the model parameters. In the current parameters The gradient is used to make full use of the known optimal strategy while maintaining sufficient exploratory nature, which helps to escape the local optimum trap and discover more efficient repair strategies.

[0105] In this embodiment, by introducing reinforcement learning algorithms into the field of smart contract vulnerability repair, the application scope of artificial intelligence is expanded. Furthermore, by combining the two inputs, AST sequence and source code sequence, as input parameters of the model, rich input features can be provided to the model. By using the policy gradient algorithm and introducing a Transformer model with SASA mechanism into the agent of the policy gradient algorithm, the structural information of the code itself can be better understood, thereby improving the accuracy and generalization ability of vulnerability repair.

[0106] The model training method provided in the embodiments of this application can train an initial program repair model with set parameters to obtain a target program repair model based on the sample abnormal subroutines and their sample syntax structure information in the sample program. The model can then analyze the sample abnormal subroutines and their sample syntax structure information to deeply learn the feature information of the sample abnormal subroutines, thereby obtaining a target repair model that can accurately repair the program. The program is then repaired based on this target repair model. In this way, not only can the program be automatically repaired through the trained target repair model, but the accuracy of program repair can also be improved, thereby solving the problems of low efficiency and low accuracy in the methods of repairing smart contracts in related technologies.

[0107] Based on the foregoing embodiments, embodiments of this application provide a program repair method, referring to... Figure 3 As shown, the method may include the following steps: Step 201: Obtain the program to be repaired.

[0108] In this embodiment, the program to be repaired can be a vulnerable smart contract program, i.e., the code of the smart contract to be repaired. It should be noted that the program to be repaired may have only one vulnerability or multiple vulnerabilities.

[0109] Step 202: Use the target program repair model to repair the program to be repaired to obtain the target program.

[0110] Specifically, the program to be repaired can be input as an input parameter into the target program repair model, which can then process the program to be repaired, that is, repair the vulnerabilities in the program to be repaired, so as to obtain the accurate target program.

[0111] It should be noted that the target program repair model can be a Transformer model with SASA mechanism.

[0112] The target program repair model can be trained in the following ways: B1. Identify the sample exception subroutines in the sample program, and determine the sample syntax structure information of the sample exception subroutines based on them.

[0113] B2. Construct an initial program repair model and set status information, action information, and reward information for the initial program repair model.

[0114] B3. Based on the sample abnormal subroutines and sample syntax structure information, the target reinforcement learning algorithm is used to train the initial program repair model to obtain the target program repair model.

[0115] It should be noted that the descriptions of the same steps and contents as in other embodiments in this embodiment can be found in the descriptions in other embodiments, and will not be repeated here.

[0116] The program repair method provided in this application can quickly repair vulnerabilities in the code of a smart contract by training a target program repair model based on the sample abnormal subroutines and their sample syntax structure information in the sample program. This is unlike related technologies where manual repair of the smart contract code is required. Furthermore, the model can deeply learn the feature information of the sample abnormal subroutines by analyzing the sample abnormal subroutines and their sample syntax structure information, thereby obtaining a target repair model that can accurately repair the program. Based on this target repair model, the program can be accurately repaired. This not only improves the efficiency of program repair but also the accuracy of program repair, thus solving the problems of low efficiency and low accuracy in related technologies for repairing smart contracts.

[0117] This application provides a model training device that can be applied to... Figure 1 In the model training method provided in the corresponding embodiment, refer to Figure 4 As shown, the device 3 may include: a determining unit 31, a constructing unit 32, and a training unit 33, wherein: The determining unit 31 is used to determine the sample exception subroutine in the sample program, and to determine the sample syntax structure information of the sample exception subroutine based on the sample exception subroutine. Construction unit 32 is used to construct an initial program repair model and set status information, action information and reward information for the initial program repair model; Training unit 33 is used to train the initial program repair model based on the sample abnormal subroutines and sample syntax structure information using a target reinforcement learning algorithm to obtain the target program repair model.

[0118] In other embodiments of this application, the construction unit 32 is also used to perform the following steps: For the initial program repair model, a state space is set up based on the sample abnormal subroutines and sample syntax structure information; wherein, the state information includes the state space; Obtain multiple repair operations for historical programs, and set an action space based on the multiple repair operations; wherein, the action information includes the action space; Obtain the sample benchmark program corresponding to the sample program, and set the reward function based on the sample program, the repaired sample program, and the sample benchmark program; wherein, the reward information includes the reward function.

[0119] In other embodiments of this application, the construction unit 32 is also used to perform the following steps: Based on the sample program, the sample benchmark program, and the repaired sample program, multiple sub-reward functions are set; among them, the reward function includes multiple sub-reward functions; Determine the weight of each sub-reward function; The reward function is set based on each sub-reward function and its weight.

[0120] In other embodiments of this application, the construction unit 32 is also used to perform the following steps: Based on the repaired sample program, sample program, and sample benchmark program, a sub-reward function is set for the accuracy of the repaired sample program. Based on the compilation results of the repaired sample program, a sub-reward function for the feasibility of the repaired sample program is set. Based on the first computing resources consumed by running the sample program and the second computing resources consumed by running the repaired sample program, a sub-reward function is set for the running efficiency of the repaired sample program.

[0121] In other embodiments of this application, the construction unit 32 is also used to perform the following steps: The repaired sample program is processed using an object detection algorithm, and the first sub-reward function is set based on the detection results; Based on the first complexity of the repaired sample program and the second complexity of the sample benchmark program, a second sub-reward function is set. A third sub-reward function is set based on the similarity between the sample program and the repaired sample program.

[0122] In other embodiments of this application, the training unit 33 is further configured to perform the following steps: The sample anomaly subroutine is serialized to obtain the sample subroutine sequence; The sample syntax structure information is processed using a preorder traversal algorithm to obtain the sample syntax structure sequence; Based on the sample subroutine sequence and sample syntax structure sequence, the initial program repair model is trained using a target reinforcement learning algorithm to obtain the target program repair model.

[0123] It should be noted that the specific implementation process of the steps performed by each unit in the embodiments of this application can be referred to Figure 1 The implementation process of the model training method provided in the corresponding embodiment will not be described in detail here.

[0124] The model training apparatus provided in the embodiments of this application can train an initial program repair model with set parameters to obtain a target program repair model based on the sample abnormal subroutines and sample syntax structure information of the sample program. The model can then analyze the sample abnormal subroutines and sample syntax structure information to deeply learn the feature information of the sample abnormal subroutines, thereby obtaining a target repair model that can accurately repair the program. The program is then repaired based on the target repair model. In this way, not only can the program be automatically repaired through the trained target repair model, but the accuracy of program repair can also be improved, thereby solving the problems of low efficiency and low accuracy of smart contract repair methods in related technologies.

[0125] This application provides a program repair device, which can be applied to... Figure 3 In the corresponding implementation method for program repair, refer to Figure 5 As shown, the program repair device 4 may include: an acquisition unit 41 and a processing unit 42, wherein: Acquisition unit 41 is used to acquire the program to be repaired; Processing unit 42 is used to perform repair processing on the program to be repaired using the target program repair model to obtain the target program; The target program repair model was trained using the following method: Identify the sample exception subroutines in the sample program, and determine the sample syntax structure information of the sample exception subroutines based on them; Construct an initial program repair model and set status information, action information, and reward information for the initial program repair model; Based on the sample anomaly subroutines and sample syntax structure information, a target reinforcement learning algorithm is used to train the initial program repair model to obtain the target program repair model.

[0126] It should be noted that the specific implementation process of the steps performed by each unit in the embodiments of this application can be referred to Figure 3 The implementation process of the program repair method provided in the corresponding embodiment will not be described in detail here.

[0127] The program repair apparatus provided in the embodiments of this application can quickly repair vulnerabilities in the code of a smart contract to be repaired based on a target program repair model trained from sample abnormal subroutines and sample syntax structure information of the sample program. This is unlike related technologies where manual repair of the smart contract code is required. Furthermore, the model can deeply learn the feature information of the sample abnormal subroutines by analyzing the sample abnormal subroutines and sample syntax structure information, thereby obtaining a target repair model that can accurately repair the program. Based on this target repair model, the program can be accurately repaired. This not only improves the efficiency of program repair but also improves the accuracy of program repair, thereby solving the problems of low efficiency and low accuracy in the methods of repairing smart contracts in related technologies.

[0128] This application provides an electronic device that may include a processor, a memory, and a communication bus, wherein, referring to... Figure 6 As shown, the electronic device may include a model training device 5, the processor may include a first processor 51, the memory may include a first memory 52, and the communication bus may include a first communication bus 53. The first communication bus 53 is used to realize the communication connection between the first processor 51 and the first memory 52; The first processor 51 is used to execute the model training program in the first memory 52 to perform the following steps: Identify the sample exception subroutines in the sample program, and determine the sample syntax structure information of the sample exception subroutines based on them; Construct an initial program repair model and set status information, action information, and reward information for the initial program repair model; Based on the sample anomaly subroutines and sample syntax structure information, a target reinforcement learning algorithm is used to train the initial program repair model to obtain the target program repair model.

[0129] In other embodiments of this application, the first processor 51 is used to execute the model training program in the first memory 52, and may also implement the following steps: For the initial program repair model, a state space is set up based on the sample abnormal subroutines and sample syntax structure information; wherein, the state information includes the state space; Obtain multiple repair operations for historical programs, and set an action space based on the multiple repair operations; wherein, the action information includes the action space; Obtain the sample benchmark program corresponding to the sample program, and set the reward function based on the sample program, the repaired sample program, and the sample benchmark program; wherein, the reward information includes the reward function.

[0130] In other embodiments of this application, the first processor 51 is used to execute the model training program in the first memory 52, and may also implement the following steps: Based on the sample program, the sample benchmark program, and the repaired sample program, multiple sub-reward functions are set; among them, the reward function includes multiple sub-reward functions; Determine the weight of each sub-reward function; The reward function is set based on each sub-reward function and its weight.

[0131] In other embodiments of this application, the first processor 51 is used to execute the model training program in the first memory 52, and may also implement the following steps: Based on the repaired sample program, sample program, and sample benchmark program, a sub-reward function is set for the accuracy of the repaired sample program. Based on the compilation results of the repaired sample program, a sub-reward function for the feasibility of the repaired sample program is set. Based on the first computing resources consumed by running the sample program and the second computing resources consumed by running the repaired sample program, a sub-reward function is set for the running efficiency of the repaired sample program.

[0132] In other embodiments of this application, the first processor 51 is used to execute the model training program in the first memory 52, and may also implement the following steps: The repaired sample program is processed using an object detection algorithm, and the first sub-reward function is set based on the detection results; Based on the first complexity of the repaired sample program and the second complexity of the sample benchmark program, a second sub-reward function is set. A third sub-reward function is set based on the similarity between the sample program and the repaired sample program.

[0133] In other embodiments of this application, the first processor 51 is used to execute the model training program in the first memory 52, and may also implement the following steps: The sample anomaly subroutine is serialized to obtain the sample subroutine sequence; The sample syntax structure information is processed using a preorder traversal algorithm to obtain the sample syntax structure sequence; Based on the sample subroutine sequence and sample syntax structure sequence, the initial program repair model is trained using a target reinforcement learning algorithm to obtain the target program repair model.

[0134] It should be noted that a detailed description of the steps performed by the first processor can be found in [reference needed]. Figure 1 The model training method provided in the corresponding embodiments will not be described in detail here.

[0135] The model training device provided in the embodiments of this application can train an initial program repair model with set parameters to obtain a target program repair model based on the sample abnormal subroutines and sample syntax structure information of the sample program. The model can then analyze the sample abnormal subroutines and sample syntax structure information to deeply learn the feature information of the sample abnormal subroutines, thereby obtaining a target repair model that can accurately repair the program. Based on this target repair model, the program can be repaired. In this way, not only can the program be automatically repaired through the trained target repair model, but the accuracy of program repair can also be improved, thereby solving the problems of low efficiency and low accuracy of smart contract repair methods in related technologies.

[0136] This application provides an electronic device that may include a processor, a memory, and a communication bus, wherein, referring to... Figure 7 As shown, the electronic device may include a program training device 6, a processor may include a second processor 61, a memory may include a second memory 62, and a communication bus may include a second communication bus 63. The second communication bus 63 is used to realize the communication connection between the second processor 61 and the second memory 62; The second processor 61 is used to execute the program repair program in the second memory 62 to perform the following steps: Obtain the program to be repaired; The target program is obtained by using the target program repair model to repair the program to be repaired. The target program repair model was trained using the following method: Identify the sample exception subroutines in the sample program, and determine the sample syntax structure information of the sample exception subroutines based on them; Construct an initial program repair model and set status information, action information, and reward information for the initial program repair model; Based on the sample anomaly subroutines and sample syntax structure information, a target reinforcement learning algorithm is used to train the initial program repair model to obtain the target program repair model.

[0137] It should be noted that a detailed description of the steps performed by the second processor can be found in [reference needed]. Figure 3 The corresponding implementation method for program repair is not described in detail here.

[0138] The program repair device provided in the embodiments of this application can quickly repair vulnerabilities in the code of a smart contract to be repaired based on a target program repair model trained from sample abnormal subroutines and sample syntax structure information of the sample program. This is unlike related technologies where manual repair of the smart contract code is required. Furthermore, the model can deeply learn the feature information of the sample abnormal subroutines by analyzing the sample abnormal subroutines and sample syntax structure information, thereby obtaining a target repair model that can accurately repair the program. Based on this target repair model, the program can be accurately repaired. This not only improves the efficiency of program repair but also improves the accuracy of program repair, thereby solving the problems of low efficiency and low accuracy in the methods of repairing smart contracts in related technologies.

[0139] Based on the foregoing embodiments, embodiments of this application provide a computer-readable storage medium storing one or more programs, which can be executed by one or more processors to implement... Figure 1 The corresponding implementation provides the model training method and Figure 3 The corresponding implementation provides the steps of the program repair method.

[0140] Based on the foregoing embodiments, embodiments of this application provide a computer program product, including a computer program that can be executed by a first processor 51 and a second processor 61 to perform... Figure 1 The corresponding implementation provides the model training method and Figure 3 The corresponding implementation provides the steps of the program repair method.

[0141] The above are merely specific embodiments of this application, but the scope of protection of this application is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the scope of the technology disclosed in this application should be included within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the scope of the claims.

Claims

1. A model training method, characterized in that, The method includes: Identify the sample exception subroutines in the sample program, and determine the sample syntax structure information of the sample exception subroutines based on the sample exception subroutines; Construct an initial program repair model, and set status information, action information, and reward information for the initial program repair model; Based on the sample anomaly subroutine and the sample syntax structure information, the initial program repair model is trained using a target reinforcement learning algorithm to obtain the target program repair model.

2. The method according to claim 1, characterized in that, The setting of state information, action information, and reward information for the initial program repair model includes: For the initial program repair model, a state space is set based on the sample abnormal subroutine and the sample syntax structure information; wherein, the state information includes the state space; Obtain multiple repair operations for historical programs, and set an action space based on the multiple repair operations; wherein, the action information includes the action space; Obtain the sample benchmark program corresponding to the sample program, and set a reward function based on the sample program, the repaired sample program, and the sample benchmark program; wherein, the reward information includes the reward function.

3. The method according to claim 2, characterized in that, The step of setting a reward function based on the sample program, the repaired sample program, and the sample benchmark program includes: Based on the sample program, the sample benchmark program, and the repaired sample program, multiple sub-reward functions are set; wherein, the reward function includes the multiple sub-reward functions; Determine the weight of each sub-reward function; The reward function is set based on each sub-reward function and the weight of each sub-reward function.

4. The method according to claim 3, characterized in that, Based on the sample program, the sample benchmark program, and the repaired sample program, multiple sub-reward functions are set, including: Based on the repaired sample program, the sample program, and the sample benchmark program, a sub-reward function is set for the accuracy of the repaired sample program; Based on the compilation results of the repaired sample program, a sub-reward function for the feasibility of the repaired sample program is set. Based on the first computing resources consumed by running the sample program and the second computing resources consumed by running the repaired sample program, a sub-reward function is set for the running efficiency of the repaired sample program.

5. The method according to claim 4, characterized in that, The step of setting a sub-reward function for the accuracy of the repaired sample program based on the repaired sample program, the sample program, and the sample benchmark program includes: The repaired sample program is processed using a target detection algorithm, and a first sub-reward function is set based on the detection results; Based on the first complexity of the repaired sample program and the second complexity of the sample benchmark program, a second sub-reward function is set; A third sub-reward function is set based on the similarity between the sample program and the repaired sample program.

6. The method according to claim 1, characterized in that, The step of training the initial program repair model using a target reinforcement learning algorithm based on the sample abnormal subroutine and the sample syntax structure information to obtain a target program repair model includes: The sample anomaly subroutine is serialized to obtain a sample subroutine sequence; The sample syntax structure information is processed using a preorder traversal algorithm to obtain a sample syntax structure sequence; Based on the sample subroutine sequence and the sample syntax structure sequence, the target reinforcement learning algorithm is used to train the initial program repair model after the settings are configured, so as to obtain the target program repair model.

7. A program repair method, characterized in that, The method includes: Obtain the program to be repaired; The target program is obtained by using a target program repair model to repair the program to be repaired. The target program repair model is obtained by training the model training method as described in any one of claims 1-6.

8. An electronic device, characterized in that, The device includes: a processor, a memory, and a communication bus; The communication bus is used to realize the communication connection between the processor and the memory; The processor is used to execute a program in memory to implement the steps of the model training method as described in any one of claims 1-6, or to implement the steps of the program repair method as described in claim 7.

9. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores one or more programs, which can be executed by one or more processors to implement the steps of the model training method as described in any one of claims 1-6, or the program repair method as described in claim 7.

10. A computer program product, the computer program product comprising a computer program, characterized in that, When the computer program is executed by a processor, it implements the method according to any one of claims 1-6 or 7.