Compiler back-end defect detection method and system based on diversified guidance
By conducting diverse boot detection methods on the compiler, diversified program variants are generated and differential testing is carried out, the problem of inability to effectively detect back-end defects in the compiler in the existing technology is solved, and the coverage and efficiency of the test are improved , especially for detection of miscompilation errors.
Patent Information
- Application Number
- CN202410509696.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-04-26
- Publication Date
- 2024-11-22
- Estimated Expiration
- 2044-04-26
AI Technical Summary
The prior art cannot effectively detect defects in the backend in the compiler, especially miscompilation errors. This defect is difficult to detect and can easily have serious impact on the program.
Using a detection method based on diversified guidance, the seed program is preprocessed, inserted, deleted and replaced the mutation operator, and the diverse program variant operator is generated, and the UCB-1 algorithm is used to select the mutation operator, and the generated program is used. Variants perform differential testing to detect back-end defects in the compiler.
It improves the coverage and efficiency of compiler testing, can effectively detect defects in the backend in the compiler, especially difficult to detect miscompilation errors, and improves the quality and accuracy of the test.
Smart Images

Figure CN118467345B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the fields of software testing and compiler testing, and mainly to mid- and back-end fuzz testing of compilers. Background Art
[0002] With the development of science and technology and the improvement of economic level, more and more software are put into the market and put into operation, including service software, aerospace software, etc. For the software to run normally, it is inseparable from the compiler. The role of the compiler is to convert the written program into a binary program that can be run in the corresponding environment.
[0003] Compilers are essentially basic software. Like other traditional software, they inevitably have some defects. Compiler defects may cause errors in programs compiled with them, resulting in huge economic losses and even life-threatening situations. For example, the XcodeGhost incident was about Apple's Xcode compiler, which was created with a malicious variant that introduced a backdoor to all applications compiled with it, threatening the security of these applications. In this incident, more than 4,000 mobile applications were affected, including WeChat and banking applications. For developers, if a defective compiler is used to compile a program, when the program fails, the developer will first think that it is a problem with his own code, because compilers are usually considered to have high security and are not prone to errors, which makes it extremely difficult for developers to debug the program. Therefore, it is crucial to ensure the security of the compiler, and a lot of work related to compiler testing has emerged, all of which aims to improve the reliability of the compiler.
[0004] Compilers are generally composed of three components, namely the front end, the middle end, and the back end. The front end of the compiler mainly verifies the correctness of the program structure and static semantics; the middle end of the compiler improves the quality of the intermediate code through a machine-independent optimizer; and the back end of the compiler further performs target machine-related optimizations on the intermediate code and creates an executable file for the target machine based on the optimized intermediate code. There may be defects in each component of the compiler, and compiler front-end defects are usually some warning information defects and error prompt defects. These defects usually cause trouble to the developer during the debugging process, but the impact of this defect is relatively small. Compiler middle and back-end defects usually include compiler internal errors, miscompilation and other defects. Compiler internal errors will be reported during the compilation process and error messages will be given, prompting developers to submit defect reports to the corresponding defect library to facilitate compiler maintainers to fix the defect; while miscompilation errors cannot be seen in an intuitive way. This defect will cause a correct program to exhibit incorrect behavior after compilation. This type of miscompilation defect is considered to be the most difficult defect to detect, and it is easy to cause serious impact on the program itself.
[0005] This invention patent mainly addresses the problem that the existing technology cannot effectively detect defects in the middle and back ends of the compiler, and proposes a new feasibility technology to better detect defects that are difficult to detect, such as internal errors in the compiler and miscompilations. Summary of the invention
[0006] Purpose of the invention: In view of the problems existing in the above-mentioned background technology, the purpose of the present invention is to provide a compiler back-end defect detection method and system based on diversified guidance to improve the coverage and test efficiency of compiler testing.
[0007] Technical solution: To achieve the above purpose, the technical solution adopted by the present invention is:
[0008] A compiler back-end defect detection method based on diversified guidance includes the following steps:
[0009] Preprocess the seed program so that the processed program can detect changes in local variables;
[0010] Build a code block corpus, where code blocks are extracted from real-world compiler bug libraries and projects, and after extraction, verify whether the code block will cause program variant compilation errors after being inserted into the program. If a front-end error occurs, the code block is discarded;
[0011] Design a variety of mutation operators, including insertion mutation operators, deletion mutation operators and replacement mutation operators; select mutation operators based on a diversified guidance strategy to generate program variants, so that the seed program mutates in a direction with more diversified structures and control flows, while ensuring the effectiveness of program variants during the mutation process, and select mutation operators in combination with the UCB-1 algorithm to generate effective and diversified program variants; in the UCB-1 algorithm, the reward value of the mutation operator is obtained through program diversity and effectiveness indicators, if the program distance of the program variant generated in a certain iteration is greater than the average program distance of all previous iterations, and the program variant generated in this iteration is valid, then increase the reward value of the mutation operator selected in this iteration; the distance between two programs is measured by the control flow graph and program statements;
[0012] Differentially test the compiler using the generated program variants to detect back-end defects in the compiler.
[0013] Preferably, the seed program is selected from a seed program corpus, and the preprocessing of the seed program is hashing local variables in the program, including:
[0014] Convert the seed program into an abstract syntax tree, and then extract all the code block nodes in the tree through depth-first traversal; traverse each code block node in turn, extract the local variables, and then insert the checksum and the hash operation function of the local variables into the end of the code block; if the last statement of the code block is a return statement, insert the hash operation function above the return statement to ensure that the program can execute the hash operation.
[0015] Preferably, the steps of extracting and screening code blocks in the code block corpus include:
[0016] First, convert the code to be operated into AST, and obtain and save all variable names and their corresponding data types appearing in the program into a dictionary through depth-first traversal;
[0017] Then, a node node of the specified type is obtained through depth-first traversal, and the node node is traversed to extract all variable sets A1 and declared variable set A2 under the node. The difference between sets A1 and A2 is the undeclared variable set A3 under the node.
[0018] Then, the code block information is saved. First, all undeclared variables in A3 and their corresponding data types are written to the beginning of the file in the form of comments. Then, the node is converted into code form and written to the end of the file.
[0019] After extracting the code block, verify whether the code block will cause errors in compiling the program variant after being inserted into the program.
[0020] Preferably, the code block feasibility verification method includes:
[0021] First, declare a main function, then insert the code block to be verified into the main function. According to the comment information of the code block, declare and initialize the undeclared variables in the code block, and insert it to the beginning of the main function. Through the above operations, construct a code program that theoretically conforms to the grammatical conventions, and then use the compiler to compile the code program. If an error is reported during the compilation process, the code block will be discarded.
[0022] Preferably, the insertion mutation operator includes inserting assignment statements, while code blocks, doWhile code blocks, if code blocks, for code blocks, switch code blocks and return statements into the program; the deletion mutation operator includes deleting assignment statements, if code blocks, for code blocks, return statements and goto statements; the replacement mutation operator includes replacing an assignment statement with a function.
[0023] Preferably, the distance Dist between two programs p1 and p2 is calculated as follows:
[0024] Dist(p1,p2)=GED(g1,g2)+Jaccard(p1,p2)
[0025] Where g1 and g2 represent the control flow graphs of p1 and p2 respectively, GED(·) represents the graph edit distance between p1 and p2, and Jaccard(·) represents the Jaccard distance between program statements in p1 and p2. The calculation formula of GED(·) is as follows:
[0026]
[0027] Among them, e i represents the editing operations of nodes and edges in the control flow graph, including deletion, insertion, and replacement operations; γ(g1,g2) represents all the editing operations required from g1 to g2, and c(e i ) is the editing operation i The cost function is used to measure the weights of different editing operations; the Jaccard (·) calculation formula is as follows:
[0028]
[0029] Where Stmt(·) represents each line of statements in the program, Stmt(p1)∩Stmt(p2) represents the number of identical statements in p1 and p2, and Stmt(p1)∪Stmt(p2) represents the number of all different statements in p1 and p2;
[0030] The way to measure the effectiveness of a program is to use clang-16 to compile the program variant. If no error is reported, it indicates that the program front-end is wrong. Then use the Sanitizer tool in clang-16 to detect undefined behavior in the program variant. If no error is reported, it indicates that there is no undefined behavior in the program variant. If the program passes the above two tests, it indicates that the program variant is valid.
[0031] As a preferred method, the UCB-1 algorithm formula is as follows:
[0032]
[0033] Among them, result(t) represents the mutation operator selected in the tth iteration, t j represents the total number of mutation operators selected until the tth iteration, x jiRepresents the reward value of the jth mutation operator when it is selected for the i-th time; if the program distance of the program variant generated in the t-th iteration is greater than the average program distance of the previous t-1 iterations, and the program variant is valid, then the reward value of the mutation operator selected at this time is increased by 1.
[0034] Preferably, the generated program variants are used to perform differential testing on the compiler, including: during the test, compiling the same program to be tested using different compiler optimization options or different compilers, comparing the differences between the program output results, and then finding the compilation that may have defects through a majority voting algorithm.
[0035] A compiler mid- and back-end defect detection system based on diversified guidance, comprising:
[0036] The program preprocessing module is used to preprocess the seed program. The processed program can detect the changes of local variables.
[0037] A code block corpus building module is used to build a code block corpus, where code blocks are extracted from real-world compiler defect libraries and projects, and after the extraction is completed, it is verified whether the code block will cause a program variant compilation error after being inserted into the program. If a front-end error occurs, the code block is discarded;
[0038] A program mutation module is used to design a variety of mutation operators, including insertion mutation operators, deletion mutation operators and replacement mutation operators; based on the diversified guidance strategy, mutation operators are selected to generate program variants, so that the seed program mutates in the direction of more diversified structure and control flow, while ensuring the validity of the program variants during the mutation process, and selecting mutation operators in combination with the UCB-1 algorithm to generate effective and diversified program variants; in the UCB-1 algorithm, the reward value of the mutation operator is obtained through the diversity and validity indicators of the program, if the program distance of the program variant generated in a certain iteration is greater than the average program distance of all previous iterations, and the program variant generated in this iteration is valid, then the reward value of the mutation operator selected in this iteration is increased; the distance between two programs is measured by the control flow graph and program statements;
[0039] And the differential testing module is used to perform differential testing on the compiler using the generated program variants to detect back-end defects in the compiler.
[0040] A computer program product includes a computer program / instruction. When the computer program / instruction is executed by a processor, the steps of a back-end defect detection method in a compiler based on diversified guidance are implemented.
[0041] Beneficial effects: Compared with the prior art, the method for detecting defects in the compiler's back-end based on diversified guidance provided by the present invention proposes a seed program preprocessing method that is helpful for detecting defects in the compiler's back-end, and designs and implements a variety of mutation operators including insertion mutation, deletion mutation and replacement mutation, further improving the testing effect of the compiler's back-end; at the same time, the present invention proposes a diversified guidance strategy to improve the efficiency of compiler testing and improve the quality of testing in order to address the problem of excessive mutation space. BRIEF DESCRIPTION OF THE DRAWINGS
[0042] Figure 1 It is a flow chart of a method for detecting defects in the back-end of a compiler based on diversified guidance provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0043] 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.
[0044] like Figure 1 As shown, the embodiment of the present invention provides a compiler back-end defect detection method based on diversified guidance, which mainly includes program preprocessing, program mutation and differential testing. The specific implementation of each step is as follows:
[0045] Step S1: Program preprocessing
[0046] In this embodiment, a batch of seed programs are generated using C program random generator Csmith, and these seed programs are used as seed program corpus. A program (here referred to as seed program) is selected from the seed program corpus, and local variables are hashed. Before introducing this operation, the characteristics of the seed program (these programs are generated by the csmith tool) are first introduced: the program contains a global variable checksum, which performs corresponding operations (here referred to as hash operations) with all global variables of numerical types in the program. The value of the checksum changes accordingly during the operation, but after multiple operations, the final result is a definite value. Through a series of hash operations, the final output checksum can reflect the changes of global variables of various numerical types in the program (i.e., if a compiler defect causes the program to make an error during the operation, and then causes the global variable value at a certain position to make an error, it will be reflected by the final checksum). That is to say, in the seed program, through hash operations, we can monitor the changes of global variables in the program, but the seed program does not monitor the changes of local variables, which may lead to the omission of some compiler defect monitoring. Therefore, during the preprocessing here, we introduced the monitoring of program local variables, that is, local variable hash processing. If the seed program after preprocessing generates an incorrect value at a certain position in the program due to compiler error compilation, then the preprocessing method can monitor the changes of the local variable and report the compiler defect during the differential test.
[0047] First, a C language seed program is selected from the seed program corpus and converted into an abstract syntax tree (Abstract Syntax Tree, AST for short). Then, all code block nodes in the tree are extracted through depth-first traversal. Each code block node is traversed in turn to extract local variables that meet the following two criteria:
[0048] (1) Local variables are assigned initial values when they are defined; (2) Local variables should be numeric types. Principle (1) ensures that local variables have definite values when performing hash operations and do not introduce undefined behavior; Principle (2) ensures that hash operations can be performed correctly and do not cause program errors.
[0049] Then insert the checksum and local variable hash functions to the end of the code block. Note that if the last statement in the code block is a return statement, the hash function should be inserted above the return statement to ensure that the program can execute the hash operation.
[0050] Step S2: Program mutation stage
[0051] This embodiment designs 13 mutation operators and diversified mutation operator selection strategies to ensure the generation of diversified program variants. The specific mutation operator description is shown in Table 1 below:
[0052] Table 1 Mutation operators and their descriptions
[0053]
[0054]
[0055] Step S2.1: Code block extraction and screening
[0056] The mutation operators include insertion mutation operators, which mainly include the insertion of assignment statements, while structures, if structures, for structures, switch structures and other code blocks. These structures are extracted from real-world compiler defect libraries and projects. After the extraction is completed, it is also necessary to verify whether the code block will cause program variant compilation errors after being inserted into the program (due to errors in the program's own syntax, etc.). The method is to complete the code block to make it a complete program, and then select a compiler to compile it to see if there is a front-end error. If so, the code block is discarded, otherwise it is retained.
[0057] In this embodiment, the code block extraction method is as follows: first, the code to be operated is converted into AST, and all variable names appearing in the program and their corresponding data types are obtained and saved in the dictionary variable2type through depth-first traversal; then, a node node of a specified type (i.e., an assignment statement or a control flow structure, etc.) is obtained through depth-first traversal, and the node node is traversed to extract all variable sets A1 and declared variable sets A2 under the node. Then, the difference between sets A1 and A2 is the undeclared variable set A3 under the node; then, the code block information is saved, and all undeclared variables in A3 and their corresponding data types (which can be obtained in variable2type) are first written to the beginning of the file in the form of comments, and then node is converted into code form and written to the end of the file.
[0058] After extracting the code block above, you also need to verify whether the code block will cause a compilation error of the program variant after being inserted into the program (due to errors in the program's own syntax, etc.). The feasibility verification method of the code block is as follows: first declare a main function with an empty function body, and then insert the code block to be verified into the main function. According to the comment information of the code block, declare and initialize the variables that are not declared in the code block, and insert it to the beginning of the main function; through the above operations, construct a code program that theoretically complies with the grammatical conventions, and then use Clang-18 to compile the code program. If an error is reported during the compilation process (referring to an error in the program itself), then discard the code block. Through the code block verification operation, the number of program variants that generate syntax errors in subsequent program mutations can be reduced.
[0059] Step S2.2: Specific implementation of 13 mutation operators
[0060] Implementation method of insert mutation operator: They are implemented on AST. First, select the code block structure to be inserted, and then select a code block of the corresponding type from the code block corpus; then we need to convert the code block into an AST node by constructing an empty main function and inserting the code block into the main function. At this time, there is no need to declare the undeclared variables in the code block. Use pycpaser to convert the main function into AST, and then extract the code block node under the main function; because the undeclared variable names in the code block may be repeated with the variable names in the seed program, resulting in program errors after insertion, these undeclared variables need to be renamed here, renamed to 8-bit strings composed of random letters, numbers, and underscores (cannot start with numbers); after renaming, variable declaration and initialization are also required. In principle, initialization is preferred. The global variables already in the seed program, that is, if the data types of the global variables and the variables to be declared are the same, they are reused, otherwise, the variables to be declared are assigned values within the numerical range of their data types. Inspired by previous work, compiler testing will be more effective if undefined behavior is not handled so conservatively, so when assigning values, a boundary value corresponding to the assigned data type is set with a half probability; after declaring and initializing the variables, the statement needs to be converted into an AST node to facilitate subsequent insertion into the AST; so far, the AST node list of undeclared variables and the AST node of the code block to be inserted have been obtained; then a suitable insertion position is selected, first randomly select a function node, then select one of the random code block nodes, insert the above converted code block AST node to be inserted into it, and insert all the AST nodes of the undeclared variables into the beginning of the function node. In particular, when inserting a return statement, you need to first randomly select a function node that is not of void type, and then obtain the data type of the function. As above, the variable returned by return will preferentially reuse the global variable of the same type in the seed program, otherwise a custom declaration and initialization will be performed. Finally, the constructed declaration node (if any) will be inserted into the beginning of the function node, and the return node will be randomly inserted into a code block node in the function.
[0061] Implementation method of deletion mutation operator: First, select the code structure to be deleted (represented as S here), obtain all the code block nodes of the seed program through depth-first traversal, traverse each code block node in turn, find the node of the same type as S, and store the code block node and index information in the list L; finally, randomly select a node from the list L and delete it according to the code block node and index information. In particular, after deleting the goto statement node, you also need to delete the corresponding label node. Traverse the function node where the deleted goto statement is located through depth-first traversal, find the corresponding label node and delete it.
[0062] Replacement mutation operator implementation method: Here, the right half of an assignment statement is converted into a function call. First, create a function AST template, including a function declaration, a function body, and a return statement, but the function name, the required parameters of the function, and the specific return statement need to be filled in according to the specific statement; then extract all the variables and corresponding types in the assignment statement, map these variables to the function parameters, and the variables of the return statement in the function body need to be replaced with the function parameters accordingly, and name the new function (the naming rules are the same as those in the above insertion mutation operator method); insert the newly defined function above above all functions of the seed program, and call it at the corresponding assignment statement position.
[0063] Step S2.3: Diversified mutation operator selection strategy
[0064] In order to select more effective mutation operators in the process of program mutation, thereby improving the efficiency of compiler testing and the coverage of compiler testing, a diversity-based guidance strategy is introduced. Intuitively, diverse program structures and control flow information can trigger as many compiler backend defects as possible. Therefore, it is necessary to ensure that the program generated by each mutation is different from the previous program variant as much as possible, and at the same time, it is necessary to ensure that the generated program variant is as effective as possible (i.e., it can be compiled by the compiler front end and does not contain undefined behavior). The present invention uses program distance to measure the difference between two programs, and the distance between programs is calculated based on their control flow graphs and program statements. The main purpose of calculating program distance is to generate diverse program variants; at the same time, after each selection of mutation operators and mutation, it is necessary to measure whether the program is valid, use a specified compiler to compile the program, if no front-end error is reported, it means that the program variant is valid in syntax and structure, and then use the undefined behavior detection tool to detect whether the program variant contains undefined behavior. Through the above operations, the distance between the program and the previous program can be obtained. If the distance is greater than the average of all previous distances, it is considered that the program diversity has increased. If the program is still a valid program, then the reward value of the mutation operator selected for this mutation is increased.
[0065] In the process of generating program variants, the strategy of selecting mutation operators is to ensure that the generated program variants are as different from the previous ones as possible and the generated program variants are as effective as possible. The specific implementation is as follows:
[0066] In order to ensure that the generated program variants are as different as possible, the present invention uses the distance between programs to measure the difference between two programs. The distance Dist between two programs pl and p2 is measured by their control flow graphs and program statements. The specific calculation formula is as follows:
[0067] Dist(p1,p2)=GED(g1,g2)+Jaccard(p1,p2)
[0068] Where g1 and g2 represent the control flow graphs of p1 and p2 respectively, GED(·) represents the graph edit distance between p1 and p2, and Jaccard(·) represents the Jaccard distance between program statements in p1 and p2. The calculation formula of GED(·) is as follows:
[0069]
[0070] Among them, e i represents the editing operations of nodes and edges in the control flow graph, including deletion, insertion, and replacement operations. γ(g1, g2) represents all the editing operations required to go from g1 to g2. c(e i ) is the editing operation i The cost function is used to measure the weights of different editing operations. In this embodiment, all editing operations are set to 1 by default. In addition, the Jaccard (·) calculation formula is as follows:
[0071]
[0072] Among them, Stmt(·) represents each line of statements in the program, Stmt(p1)∩Stmt(p2) represents the number of identical statements in p1 and p2, and Stmt(p1)∪Stmt(p2) represents the number of all different statements in p1 and p2.
[0073] In the present invention, the program mutation process is continuous, the current mutation operation is only related to the state of the previous program variant, and a seed program can reach the state of a certain program variant after multiple mutations. After each program mutation, the program variant needs to calculate the program distance Dist(·) with the program in the previous state, and save the program distance for subsequent calculation of the average program distance. The calculation formula is as follows:
[0074]
[0075] Where AvgDist(n) represents the average of the program distances generated for n program variants; Dist(p i , p i -1) represents the program distance of the i-th program variant. Note that if the mutation starts from the seed program, the program distance is the distance between the program variant and the seed program, otherwise it is the distance between the program variant and the previous program variant.
[0076] The above operations measure the differences between programs. During the implementation of the invention, it is also necessary to measure the effectiveness of the program to ensure that more effective program variants are used for compiler testing. The specific implementation method is to use clang-16 to compile the program variant. If no error is reported, it means that the program has no front-end errors such as syntax, and then use the Sanitizer tools in clang-16 (including MemorySanitizer, AddressSanitizer and UndefinedBehaviorSanitizer) to detect undefined behaviors in the program variant. If no error is reported, it means that there is no undefined behavior in the program variant. If the program passes the above two tests, it means that the program variant is valid.
[0077] The present invention measures the diversity and effectiveness of the program, and applies these two indicators to the selection of mutation operators in combination with the UCB-1 algorithm. The UCB-1 algorithm formula is as follows:
[0078]
[0079] Among them, result(t) represents the mutation operator selected in the tth iteration, t j represents the total number of mutation operators selected until the tth iteration, x ji It represents the reward value of the jth mutation operator when it is selected for the i-th time. The reward value of the mutation operator is obtained through the above two indicators. In the implementation process, if the program distance of the program variant generated by the t-th iteration is greater than the average program distance AvgDist(t-1), and the program variant is valid, then the reward value of the selected mutation operator is increased by 1.
[0080] Step S3: Differential testing phase
[0081] Since the present invention is mainly used to detect back-end defects in compilers, including miscompilation defects, which are difficult to express in an intuitive way, it is not feasible to directly judge by the output results of the compiler or the output results of the program running. Here, the differential test technology is needed. During the test process, different optimization options of the compiler or different compilers are used to compile the same program to be tested, and the differences between the output results of the programs are compared. Then, the compiler that may have defects is found through the majority voting algorithm.
[0082] In the first two steps, a lot of diverse and effective program variants are generated. In this step, these program variants are used to test the compiler. In the present embodiment, the compilers mainly tested include compilers such as GCC, LLVM, and ICX, and their various optimization levels are tested simultaneously. The optimization options mainly involved include -O0, -O1, -O2, -O3, -Os, etc. For the same program variant, different compilers and different optimization options are used to compile, and then the compiled program is run to compare their output results. Use the majority voting algorithm again. If most of the output results are consistent, and only a few output results are inconsistent, then the compilers or optimization options of those inconsistent results are marked as defective.
[0083] During the implementation of the present invention, multiple compiler defects have been found on the latest compiler, as shown in Table 2, which is the status statistics of the submitted compiler defect reports.
[0084] Table 2 Statistics of submitted compiler defect reports
[0085] Bug Report Status GCC LLVM ICX total Confirm / Fix 4 1 22 27 To be determined 0 3 0 3 repeat 2 1 3 6 total 6 5 25 36
[0086] As shown in Table 3, it is the statistics of back-end defect types in the submitted compilers.
[0087] Table 3 Statistics of backend defect types in submitted compilers
[0088]
[0089] The above results show that a total of 36 compiler backend defects were reported on GCC, LLVM, and ICX compilers, indicating that this method can effectively detect compiler backend defects, and most of the defect reports have been confirmed or fixed by developers. Among them, the majority of the defect reports were miscompilation type, with 26, followed by crashes and hangs, with 6 and 4 respectively.
[0090] Based on the same inventive concept, an embodiment of the present invention also provides a compiler mid- and back-end defect detection system based on diversified guidance, including: a program preprocessing module, used to preprocess a seed program, and the processed program can detect changes in local variables; a code block corpus construction module, used to construct a code block corpus, wherein the code blocks are extracted from real-world compiler defect libraries and projects, and after the extraction is completed, it is verified whether the code blocks will cause program variant compilation errors after being inserted into the program, and if a front-end error occurs, the code block is discarded; a program mutation module, used to design a variety of mutation operators, including insertion mutation operators, deletion mutation operators and replacement mutation operators; based on a diversified guidance strategy, mutation operators are selected to generate program variants, so that the seed program mutates in a direction with more diversified structure and control flow, while ensuring the validity of the program variants during the mutation process, and selecting mutation operators in combination with the UCB-1 algorithm to generate effective and diversified program variants; and a differential testing module, used to use the generated program variants to perform differential testing on the compiler to detect mid- and back-end defects in the compiler.
[0091] An embodiment of the present invention further provides a computer program product, including a computer program / instruction, which, when executed by a processor, implements the steps of the back-end defect detection method in a compiler based on diversified guidance.
[0092] 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 back-end defect detection method based on diversified guidance, characterized in that: The steps include: Preprocess the seed program, and the preprocessed program can detect the changes of local variables; Build a code block corpus, where code blocks are extracted from real-world compiler bug libraries and projects, and after extraction, verify whether the code block will cause program variant compilation errors after being inserted into the program. If a front-end error occurs, the code block is discarded; A variety of mutation operators are designed, including insertion mutation operators, deletion mutation operators and replacement mutation operators; mutation operators are selected based on a diversified guidance strategy to generate program variants, so that the seed program mutates in a direction with more diversified structures and control flows, while ensuring the effectiveness of program variants during the mutation process, and the mutation operator is selected in combination with the UCB-1 algorithm to generate effective and diversified program variants; in the UCB-1 algorithm, the reward value of the mutation operator is obtained through the diversity and effectiveness indicators of the program, if the program distance of the program variant generated in a certain iteration is greater than the average program distance of all previous iterations, and the program variant generated in this iteration is valid, then the reward value of the mutation operator selected in this iteration is increased; the distance between the two programs is measured by the control flow graph and the program statement; the UCB-1 algorithm formula is as follows: Among them, result(t) represents the mutation operator selected in the tth iteration, t j represents the total number of mutation operators selected until the tth iteration, x ji represents the reward value of the jth mutation operator when it is selected for the i-th time; if the program distance of the program variant generated in the t-th iteration is greater than the average program distance of the previous t-1 iterations, and the program variant is valid, then the reward value of the mutation operator selected at this time is increased by 1; Differentially test the compiler using the generated program variants to detect back-end defects in the compiler.
2. The method for detecting compiler defects in the middle and back end based on diversified guidance according to claim 1, characterized in that: The seed program is selected from a seed program corpus, and the preprocessing of the seed program is hashing local variables in the program, including: Convert the seed program into an abstract syntax tree, and then extract all the code block nodes in the tree through depth-first traversal; traverse each code block node in turn, extract the local variables, and then insert the checksum and the hash operation function of the local variables into the end of the code block; if the last statement of the code block is a return statement, insert the hash operation function above the return statement to ensure that the program can execute the hash operation.
3. The method for detecting compiler defects in the middle and back end based on diversified guidance according to claim 1, characterized in that: The steps of extracting and screening code blocks in the code block corpus include: First, convert the code to be operated into AST, and obtain and save all variable names and their corresponding data types appearing in the program into a dictionary through depth-first traversal; Then, a node node of the specified type is obtained through depth-first traversal, and the node node is traversed to extract all variable sets A1 and declared variable set A2 under the node. The difference between sets A1 and A2 is the undeclared variable set A3 under the node. Then, the code block information is saved. First, all undeclared variables in A3 and their corresponding data types are written to the beginning of the file in the form of comments. Then, the node is converted into code form and written to the end of the file. After extracting the code block, verify whether the code block will cause errors in compiling the program variant after being inserted into the program.
4. The method for detecting compiler defects in the middle and back end based on diversified guidance according to claim 3, characterized in that: Code block feasibility verification methods include: First, declare a main function, then insert the code block to be verified into the main function. According to the comment information of the code block, declare and initialize the undeclared variables in the code block, and insert it to the beginning of the main function. Through the above operations, construct a code program that theoretically conforms to the grammatical conventions, and then use the compiler to compile the code program. If an error is reported during the compilation process, the code block will be discarded.
5. The method for detecting compiler defects in the middle and back end based on diversified guidance according to claim 1, characterized in that: The insertion mutation operator includes inserting assignment statements, while code blocks, doWhile code blocks, if code blocks, for code blocks, switch code blocks and return statements into the program; the deletion mutation operator includes deleting assignment statements, if code blocks, for code blocks, return statements and goto statements; the replacement mutation operator includes replacing an assignment statement with a function.
6. The method for detecting compiler defects in the middle and back end based on diversified guidance according to claim 1, characterized in that: The distance Dist between two programs p1 and p2 is calculated as follows: Dist(p1,p2)=GED(g1,g2)+Jaccard(p1,p2) Where g1 and g2 represent the control flow graphs of p1 and p2 respectively, GED(·) represents the graph edit distance between p1 and p2, and Jaccard(·) represents the Jaccard distance between program statements in p1 and p2. The calculation formula of GED(·) is as follows: Among them, e i represents the editing operations of nodes and edges in the control flow graph, including deletion, insertion, and replacement operations; γ(g1,g2) represents all the editing operations required from g1 to g2, and c(e i ) is the editing operation i The cost function is used to measure the weights of different editing operations; the Jaccard (·) calculation formula is as follows: Where Stmt(·) represents each line of statements in the program, Stmt(p1)∩Stmt(p2) represents the number of identical statements in p1 and p2, and Stmt(p1)∪Stmt(p2) represents the number of all different statements in p1 and p2; The way to measure the effectiveness of a program is to use clang-16 to compile the program variant. If no error is reported, it indicates that the program front-end is wrong. Then use the Sanitizer tool in clang-16 to detect undefined behavior in the program variant. If no error is reported, it indicates that there is no undefined behavior in the program variant. If the program passes the above two tests, it indicates that the program variant is valid.
7. The method for detecting compiler defects in the middle and back end based on diversified guidance according to claim 1, characterized in that: Use the generated program variants to perform differential testing on the compiler, including: during the test process, compile the same program under test with different compiler optimization options or different compilers, compare the differences between the program output results, and then use the majority voting algorithm to find the compiler that may have defects.
8. A compiler mid- and back-end defect detection system based on diversified guidance, characterized in that: include: The program preprocessing module is used to preprocess the seed program. The preprocessed program can detect the changes of local variables. A code block corpus building module is used to build a code block corpus, where code blocks are extracted from real-world compiler defect libraries and projects, and after the extraction is completed, it is verified whether the code block will cause a program variant compilation error after being inserted into the program. If a front-end error occurs, the code block is discarded; The program mutation module is used to design a variety of mutation operators, including insertion mutation operators, deletion mutation operators and replacement mutation operators; based on the diversified guidance strategy, mutation operators are selected to generate program variants, so that the seed program mutates in the direction of more diversified structure and control flow, while ensuring the effectiveness of program variants during the mutation process, and combining the UCB-1 algorithm to select mutation operators to generate effective and diversified program variants; in the UCB-1 algorithm, the reward value of the mutation operator is obtained through the diversity and effectiveness indicators of the program. If the program distance of the program variant generated in a certain iteration is greater than the average program distance of all previous iterations, and the program variant generated in this iteration is valid, the reward value of the mutation operator selected in this iteration is increased; the distance between the two programs is measured by the control flow graph and the program statement; the UCB-1 algorithm formula is as follows: Among them, result(t) represents the mutation operator selected in the tth iteration, t j represents the total number of mutation operators selected until the tth iteration, x ji represents the reward value of the jth mutation operator when it is selected for the i-th time; if the program distance of the program variant generated in the t-th iteration is greater than the average program distance of the previous t-1 iterations, and the program variant is valid, then the reward value of the mutation operator selected at this time is increased by 1; And the differential testing module is used to perform differential testing on the compiler using the generated program variants to detect back-end defects in the compiler.
9. A computer program product comprising a computer program / instructions, characterized in that When the computer program / instruction is executed by a processor, the steps of a back-end defect detection method in a compiler based on diversified guidance according to any one of claims 1 to 7 are implemented.
Citation Information
Patent Citations
Compiler defect positioning method based on reinforcement learning
CN112181420A
Compiler defect positioning method based on big language model enabling
CN117453539A
