Compiler warning defect detection method based on deep reinforcement learning variation
Through the mutation operator selection strategy based on deep reinforcement learning, a mutant program with a warning structure is generated, which solves the problem of low warning defect detection efficiency of compiler front-end warning defects in the existing technology, and achieves more efficient compiler testing and quality assurance.
Patent Information
- Application Number
- CN202411803016.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-09
- Publication Date
- 2025-03-14
AI Technical Summary
The prior art cannot effectively detect compiler front-end warning defects, resulting in low testing efficiency and limited detected defects.
Using a compiler warning defect detection method based on deep reinforcement learning mutations, a test program that can trigger the compiler warning mechanism is constructed by generating a mutation program with a warning structure and selecting a mutation operator using a deep reinforcement learning model.
It improves the detection efficiency of the front-end warning defects of the compiler, and the generated test programs are more diverse, which can more effectively detect different types of compiler warning defects, thereby improving the reliability of the compiler.
Smart Images

Figure CN119621530A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the fields of software testing and compiler testing, and mainly to compiler front-end warning defect testing. Background Art
[0002] In the process of software development, the compiler can not only optimize the code and improve the quality of the target code, but also improve the efficiency of software development and help developers find and fix problems before the program runs, thereby improving software reliability. During the program compilation process, if there is undefined behavior of the programming language in the program, such as array access out of bounds and use of uninitialized variables, the compiler will trigger the warning diagnosis mechanism and output corresponding warning information to help developers check the code. Therefore, the compiler can not only improve the cross-platform compatibility of the software through code optimization, but also perform error detection and debugging on the program.
[0003] Currently, the quality of compilers has received widespread attention from all walks of life due to its important role. Whether the compiler can accurately indicate the errors in the program is a criterion for whether it is reliable and effective. In order to ensure the quality of the compiler, compiler testing is one of the important methods. It generates a variety of complex test programs to detect whether the compiler has potential defects. Therefore, how to generate effective and diverse test programs has become one of the key issues. For many developers, the warning information in the front end of the compiler is of great significance in solving potential risks.
[0004] Although developers have done their best to maintain compiler quality and ensure compiler reliability, defects still exist in compilers. Currently, there are few methods for testing compiler warning defects, and most of them randomly generate test programs based on programming language specifications. Such methods cannot construct a large number of warning structures that can trigger the compiler warning mechanism, resulting in low actual testing efficiency and very limited warning defects that can be detected.
[0005] This invention patent mainly addresses the problem that the existing technology cannot effectively detect compiler front-end warning defects, and proposes a new feasibility technology to better detect compiler warning defects, make test program generation more diversified, and make compiler test results more effective. It can better serve compiler testing and detect compiler defects to further ensure the quality of the compiler, thereby improving the reliability of the compiler. Summary of the invention
[0006] Purpose of the invention: In response to the problems existing in the above-mentioned background technology, the present invention provides a compiler warning defect detection method based on deep reinforcement learning variation, which can detect more compiler front-end warning defects and improve the detection efficiency of compiler front-end warning defects.
[0007] Technical solution: To achieve the above purpose, the technical solution adopted by the present invention is a compiler warning defect detection method based on deep reinforcement learning variation, comprising the following steps:
[0008] (1) Generate a seed program using an automated program generation tool and design a mutation operator to mutate the seed program, thereby constructing a mutation program with a warning structure;
[0009] (2) Build a deep reinforcement learning model, determine the state set and action set of the mutant program, and calculate the diversity score of the mutant program constructed from the initial state to the current moment, so as to design the reward function of the mutation operator;
[0010] (3) A mutation operator selection strategy based on deep reinforcement learning, which generates a mutation operator selection strategy by minimizing the loss function of the deep reinforcement learning model or reaching a given number of iterations;
[0011] (4) The generated mutation operator selection strategy is used to construct the mutation program and use it as the input of different compiler front ends. By analyzing the warning diagnostic information output by different compilers, different types of compiler warning defects can be detected.
[0012] Furthermore, the mutation operator in step (1) includes one of an insertion mutation operator, a deletion mutation operator and a replacement mutation operator; the insertion mutation operator includes one of an insertion arithmetic expression, an insertion logical operation statement, an insertion assignment statement, an insertion branch statement, an insertion loop statement, an insertion function body and its calling function; the deletion mutation operator includes one of a deletion expression statement, a deletion assignment statement, a deletion variable data type, a deletion variable modifier, a deletion function declaration, a deletion function body, a deletion loop structure, and a deletion branch control structure; the replacement mutation operator includes one of a replacement variable data type, a replacement variable modifier, a replacement arithmetic operator, a replacement logical operator, a replacement relational operator, a replacement variable name, and a replacement function name.
[0013] Furthermore, the program state set in step (2) is expressed as:
[0014] STATE∈(SENT,EXPR,VAR,OPER)
[0015] Among them, SENT represents the set of all statement type features, EXPR represents the set of all expression features, VAR represents the set of all variable type features, and OPER represents the set of all operation type features.
[0016] Furthermore, the statement type feature refers to the number of different types of program statements in the variant program, including For loop statements, While loop statements, If-else statements, etc. The expression type feature refers to the number of different types of expression statements in the variant program, including arithmetic expressions, relational expressions, logical expressions, etc.; the variable type feature refers to the number of variables with different data types in the current program, including struct, int, char, const, static, etc.; the operation type feature is the number of different types of operators in the variant program, including ++, -, !, ||, >>, etc.
[0017] Furthermore, the program action set in step (2) is expressed as:
[0018] ACTION∈(INSERT,PRUNE,REPLACE)
[0019] Among them, INSERT represents the set of insertion mutation operators, PRUNE represents the set of all deletion mutation operators, and REPLACE represents the set of all replacement mutation operators.
[0020] Furthermore, suppose that the set of variant programs constructed from the initial state to the current moment is P = {P1, P2, ..., P k}, the formula for calculating the diversity score of the mutation program constructed from the initial state to the current moment in step (2) is as follows:
[0021]
[0022] Among them, P i and P j They represent two variant programs in the program set P, k represents the number of variant programs in the program set P, and Jaccard represents the Gerard distance, which is used to calculate the similarity between two variant programs.
[0023] Furthermore, program P i and program P j The Jaccard calculation formula is as follows:
[0024]
[0025] Among them, Stmp(P i ) and Stmp(P j ) represent the program P i and program P j Each line of statement.
[0026] Furthermore, the reward function calculation formula of the mutation operator in step (2) is as follows:
[0027]
[0028] Among them, t represents the current time, m t represents the mutation operator selected at time t, T(m t ) represents m t The number of times it has been chosen to mutate a given seed program. The operator not selected at time l (1≤l≤t) is not a mutation operator m. t When ΔU l =0; otherwise, use the following formula to calculate ΔU l :
[0029] U l =w*div l
[0030] ΔU=U l -U l-1 =w*div l -(w-1)*div l-1 =wΔdiv+div l-1
[0031] Where, Δdiv=div l -div l-1 , ΔU represents the quality improvement of the diversity of the mutation program by the mutation operator selected at time l compared with time l-1, and w represents the number of mutation programs constructed from the initial state to time l.
[0032] Furthermore, the loss function calculation formula of the deep reinforcement learning model in step (3) is as follows:
[0033] dqn loss =(q target -q value ) 2
[0034] Among them, q value Represents the Q value predicted by the model, q target Indicates the target Q value.
[0035] Furthermore, the target Q value is calculated as follows:
[0036] q value = Reward + γ * max (next_q value )*(1-dones)
[0037] Among them, dones indicates whether the final goal has been achieved. If it has been achieved, the value of 1-dones is 0, and the Q value of the next state is no longer considered. γ represents the discount factor, which is used to control the impact of the next program state on the current program state.
[0038] Furthermore, the analysis of warning diagnostic information output by different compilers in step (4) includes collecting warning diagnostics output by compiler front ends and performing differential analysis. If different compilers have inconsistent warning diagnostics, a compiler warning defect is detected.
[0039] Beneficial effects: Compared with the existing technology, its significant effects are as follows: 1. By designing the program mutation operator, the generated mutant program has a certain warning structure, which can trigger more compiler warning defects; 2. The selection strategy of the mutation operator is controlled by the deep reinforcement learning algorithm, so that the program body generated by each mutation has a certain effectiveness, thereby improving the testing efficiency of the compiler, and at the same time, it can also improve the quality of compiler testing. BRIEF DESCRIPTION OF THE DRAWINGS
[0040] Figure 1 It is a flow chart of a compiler warning defect detection method based on deep reinforcement learning variation provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0041] The present invention is further described below in conjunction with the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, rather than all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0042] like Figure 1 As shown, an embodiment of the present invention provides a compiler warning defect detection method based on deep reinforcement learning mutation, which constructs a mutant program with a warning structure by designing a mutation operator, and constructs a deep reinforcement learning model to determine the state set, action set and reward function of the mutant program until the deep reinforcement learning model reaches a convergence state or reaches a given number of iterations, and the training ends, thereby generating a mutation operator selection strategy. The generated mutation operator selection strategy is used to construct a mutant program, and different types of compiler warning defects are detected by differential testing. Specifically, the following steps are included:
[0043] Step 1: Use an automated program generation tool to generate a seed program, and design a mutation operator to perform mutation operations on the seed program, thereby constructing a mutation program with a warning structure. Specifically, it includes:
[0044] Step 101, generating seed programs, using the CSmith tool to automatically generate a certain number of C language programs under default configuration, and using these programs as initial seed programs.
[0045] Step 102, design mutation operators that can construct warning structures, which are insertion mutation operators, deletion mutation operators, and replacement mutation operators. The specific steps for implementation are as follows:
[0046] Step 1021, insert mutation operator, after determining the program AST node, randomly select a node to insert a code snippet that conforms to the context semantics to build a warning structure in the test program. When inserting, you can insert either a single statement or a statement block.
[0047] Step 1022, delete mutation operator, after determining the program AST node, randomly select a node to delete. If the node is a leaf node, delete the leaf node; if the node is a parent node, delete all nodes including the node and its child nodes. It is particularly pointed out that in order to ensure that the program can still be compiled correctly after the mutation is deleted, the present invention will not delete the AST nodes related to the declared, defined or referenced identifiers during the deletion process.
[0048] Step 1023, replace the mutation operator, after determining the program AST node, randomly select a node to replace.
[0049] Step 2: Build a deep reinforcement learning model, determine the state set and action set of the mutation program, and calculate the diversity score of the mutation program constructed from the initial state to the current moment, so as to design the reward function of the mutation operator. Specifically, it includes:
[0050] Step 201, program state extraction and expression, uses four types of static features to represent program state, including statement type features, expression type features, variable type features and operation type features, and expresses the extracted static feature set as:
[0051] STATE∈(SENT,EXPR,VAR,OPER)
[0052] Among them, SENT represents the set of all statement type features, EXPR represents the set of all expression features, VAR represents the set of all variable type features, and OPER represents the set of all operation type features.
[0053] Step 2011, program state storage, for each variant program, record the number of different types of features, use arrays for storage, and convert them into tensors and store them in the neural network when needed.
[0054] Step 202, program action space definition, taking mutation operators as actions that can be executed by the program, and expressing the program action set as:
[0055] ACTION∈(INSERT,PRUNE,REPLACE)
[0056] Among them, INSERT represents the set of insertion mutation operators, PRUNE represents the set of all deletion mutation operators, and REPL4CE represents the set of all replacement mutation operators.
[0057] Step 203, measure the diversity of the variant programs, assuming that the set of variant programs constructed from the initial state to the current moment is P = {P1, P2, ..., P k}, the formula for calculating the diversity score of the mutation program is as follows:
[0058]
[0059] Among them, P i and P j They represent two variant programs in the program set P, k represents the number of variant programs in the program set P, and Jaccard represents the Gerard distance, which is used to calculate the similarity between two variant programs.
[0060] Step 2031, calculate the distance between programs, program P i and program P j The Jaccard calculation formula is as follows:
[0061]
[0062] Among them, Stmp(P i ) and Stmp(P j ) represent the program P i and program P j Each line of statement.
[0063] Step 204, calculate the mutation operator reward value, and the reward function calculation formula is as follows:
[0064]
[0065] Among them, t represents the current time, m t represents the mutation operator selected at time t, T(m t ) represents m t The number of times it has been chosen to mutate a given seed program. The operator not selected at time l (1≤l≤t) is not a mutation operator m. t When ΔU l =0; otherwise, use the following formula to calculate ΔU l :
[0066] U l =w*div l
[0067] ΔU=U l -Ul-1 =w*div l -(w-1)*div l-1 =wΔdiv+div l-1
[0068] Where, Δdiv=div l -div l -1, ΔU represents the quality improvement of the diversity of the mutation program by the mutation operator selected at time l compared with time l-1, and w represents the number of mutation programs constructed from the initial state to time l.
[0069] Step 3: Generate a mutation operator selection strategy based on deep reinforcement learning by minimizing the loss function of the deep reinforcement learning model or reaching a given number of iterations. Specifically, it includes:
[0070] Step 301, select a mutation operator, for each seed program, randomly select a mutation operator to mutate the seed program, and update the program state, and synchronously update the reward value of each mutation operator under the program state.
[0071] Step 302, calculate the loss function of the deep reinforcement learning model. For each mutation, calculate the Q value in the reinforcement learning model through the reward value of the mutation operator, and continuously update the Q value during the compilation process. The calculation formula of the target Q value is as follows:
[0072] q value = Reward + γ * max (next_q value )*(1-dones)
[0073] Among them, dones indicates whether the final goal has been achieved. If it has been achieved, the value of 1-dones is 0, and the Q value of the next state is no longer considered. γ represents the discount factor, which is used to control the impact of the next program state on the current program state.
[0074] Step 303: When the given number of iterations is reached during the mutation process or the loss function of the deep reinforcement learning model reaches a convergence state, the training ends. The loss function calculation formula of the deep reinforcement learning model is as follows:
[0075] dqn loss =(q target -q value ) 2
[0076] Among them, q value Represents the Q value predicted by the model, q target Indicates the target Q value.
[0077] Step 4: Use the generated mutation operator selection strategy to construct a mutation program and use it as the input of different compiler front ends. By analyzing the warning diagnostic information output by different compilers, different types of compiler warning defects can be detected. Specifically, it includes:
[0078] Step 401, constructing a mutation program, given a seed program, using a trained mutation operator selection strategy to select a mutation operator, and generating a corresponding mutation program.
[0079] Step 402, differential testing, inputs the generated variant program into different compiler versions, and obtains the test results by aligning and analyzing the test results.
[0080] Step 4021, warning diagnostic information extraction, collects the output information of different compilers when compiling the variant program, and uses the keyword "warning" to match and extract the warning diagnostic information.
[0081] Step 4022, warning diagnostic information alignment, by analyzing whether the warning diagnostics of different compilers are consistent, determine whether the compiler has a warning defect. Assuming that compiler A has a warning message, but compiler B has no warning message, then the result may be that compiler A has misreported a false warning message, or compiler B has missed the corresponding warning message; assuming that both compilers A and B have warning messages but they are not the same, then the result may be that A or B has an erroneous warning message, and the part of the test program where the error occurred has not been accurately located. Since the test program constructs a warning structure, if compilers A and B do not have warning diagnostic information at the same time, there is a high probability that there is no defect.
[0082] The above is only a preferred embodiment of the present invention. It should be pointed out that for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the principle of the present invention. These improvements and modifications should also be regarded as the scope of protection of the present invention.
Claims
1. A compiler warning defect detection method based on deep reinforcement learning mutation, characterized in that: The steps include: (1) Generate a seed program using an automated program generation tool and design a mutation operator to mutate the seed program, thereby constructing a mutation program with a warning structure; (2) Build a deep reinforcement learning model, determine the state set and action set of the mutant program, and calculate the diversity score of the mutant program constructed from the initial state to the current moment, so as to design the reward function of the mutation operator; (3) A mutation operator selection strategy based on deep reinforcement learning, which generates a mutation operator selection strategy by minimizing the loss function of the deep reinforcement learning model or reaching a given number of iterations; (4) The generated mutation operator selection strategy is used to construct the mutation program and use it as the input of different compiler front ends. By analyzing the warning diagnostic information output by different compilers, different types of compiler warning defects can be detected.
2. The compiler warning defect detection method based on deep reinforcement learning variation according to claim 1 is characterized in that: The mutation operator in step (1) includes one of an insertion mutation operator, a deletion mutation operator and a replacement mutation operator; the insertion mutation operator includes one of an insertion arithmetic expression, an insertion logical operation statement, an insertion assignment statement, an insertion branch statement, an insertion loop statement, an insertion function body and its calling function; the deletion mutation operator includes one of a deletion expression statement, a deletion assignment statement, a deletion variable data type, a deletion variable modifier, a deletion function declaration, a deletion function body, a deletion loop structure, and a deletion branch control structure; the replacement mutation operator includes one of a replacement variable data type, a replacement variable modifier, a replacement arithmetic operator, a replacement logical operator, a replacement relational operator, a replacement variable name, and a replacement function name.
3. The compiler warning defect detection method based on deep reinforcement learning variation according to claim 1 is characterized in that: The program state set in step (2) is expressed as: STATE∈(SENT,EXPR,VAR,OPER) Among them, SENT represents the set of all statement type features, EXPR represents the set of all expression features, VAR represents the set of all variable type features, and OPER represents the set of all operation type features.
4. The compiler warning defect detection method based on deep reinforcement learning variation according to claim 3 is characterized in that: The statement type feature refers to the number of different types of program statements in the variant program, including For loop statements, While loop statements, If-else statements, etc.; The expression type feature refers to the number of different types of expression statements in the variant program, including arithmetic expressions, relational expressions, logical expressions, etc.; the variable type feature refers to the number of variables with different data types in the current program, including struct, int, char, const, static, etc.; the operation type feature refers to the number of different types of operators in the variant program, including ++, --, !, ||, >>, etc.
5. The compiler warning defect detection method based on deep reinforcement learning variation according to claim 1 is characterized in that: The program action set in step (2) is expressed as: ACTION∈(INSERT,PRUNE,REPLACE) Among them, INSERT represents the set of insertion mutation operators, PRUNE represents the set of all deletion mutation operators, and REPLACE represents the set of all replacement mutation operators.
6. The compiler warning defect detection method based on deep reinforcement learning variation according to claim 1 is characterized in that: Assume that the set of variant programs constructed from the initial state to the current moment is P = {P1, P2, ..., P k }, the formula for calculating the diversity score of the mutation program constructed from the initial state to the current moment in step (2) is as follows: Among them, P i and P j They represent two variant programs in the program set P, k represents the number of variant programs in the program set P, and Jaccard represents the Gerard distance, which is used to calculate the similarity between two variant programs.
7. The compiler warning defect detection method based on deep reinforcement learning variation according to claim 6 is characterized in that: Program P i and program P j The Jaccard calculation formula is as follows: Among them, Stmp(P i ) and Stmp(P j ) represent the program P i and program P j Each line of statement.
8. The compiler warning defect detection method based on deep reinforcement learning variation according to claim 1 is characterized in that: The reward function calculation formula of the mutation operator in step (2) is as follows: Among them, t represents the current time, m t represents the mutation operator selected at time t, T(m t ) represents m t The number of times it has been chosen to mutate a given seed program. The operator not selected at time l (1≤l≤t) is not a mutation operator m. t When ΔU l =0; otherwise, use the following formula to calculate ΔU l : U l =w*div l ΔU=U l -U l-1 =w*div l -(w-1)*div l-1 =wΔdiv+div l-1 Where, Δdiv=div l -div l-1 , ΔU represents the quality improvement of the diversity of the mutation program by the mutation operator selected at time l compared with time l-1, and w represents the number of mutation programs constructed from the initial state to time l.
9. The compiler warning defect detection method based on deep reinforcement learning variation according to claim 1 is characterized in that: The loss function calculation formula of the deep reinforcement learning model in step (3) is as follows: Ms loss =(q target -q value ) 2 Among them, q value Represents the Q value predicted by the model, q target Represents the target Q value.
10. The compiler warning defect detection method based on deep reinforcement learning variation according to claim 9, characterized in that: The target Q value is calculated as follows: q value =Reward+γ*max(next_q value )*(1-dones) Among them, dones indicates whether the final goal has been achieved. If it has been achieved, the value of 1-dones is 0, and the Q value of the next state is no longer considered. y represents the discount factor, which is used to control the impact of the next program state on the current program state.
11. The compiler warning defect detection method based on deep reinforcement learning variation according to claim 1 is characterized in that: The step (4) of analyzing the warning diagnostic information output by different compilers includes collecting the warning diagnostics output by the compiler front end and performing differential analysis. If different compilers have inconsistent warning diagnostics, a compiler warning defect is detected.
Citation Information
Patent Citations
Fuzzy testing method and tool based on deterministic strategy and coverage guidance
CN114661577A
Efficient PDF application program fuzzy test case generation method based on deep learning
CN117873888A
Fuzzy testing method for scheduling optimization of deep learning compiler
CN118093363A
Compiler middle-rear end defect detection method and system based on diversified guidance
CN118467345A
Compiler error recovery fault detection method and system based on variation configuration selection
CN118626295A