A token-level method for automatically generating program input grammar

The token-level program input grammar generation method addresses the challenge of complex input formats by synthesizing context-free grammars through symbolic execution and generalization, improving the efficiency and completeness of automatic software testing.

CN114706760BActive Publication Date: 2024-12-06NAT UNIV OF DEFENSE TECH
View PDF 4 Cites 0 Cited by

Patent Information

Application Number
CN202210259489.9
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-03-16
Publication Date
2024-12-06
Estimated Expiration
2042-03-16

AI Technical Summary

Technical Problem

Existing automatic software testing methods struggle with complex input formats due to the lack of available input grammars, leading to inefficient and incomplete testing, especially when valid inputs are not accessible, limiting the effectiveness of grammar learning and testing tools.

Method used

A token-level program input grammar generation method that utilizes symbolic execution to generate token sequences, convert them into regular expressions, merge non-terminal symbols, and perform character-level generalization to synthesize context-free grammars, using tools like GADSE and Glade for efficient grammar synthesis.

Benefits of technology

Improves the effectiveness and efficiency of automatic testing by generating complete and accurate grammars, enhancing grammar-based testing tools and achieving higher code coverage and error detection.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114706760B_ABST
    Figure CN114706760B_ABST
Patent Text Reader

Abstract

This invention discloses a method for automatically generating token-level program input grammars. The steps include: S01. Obtaining a string that conforms to the parser's grammar as initial input and performing symbolic execution to generate a set of token sequences corresponding to the parser and collecting the upper and lower bounds of each token; S02. Generalizing the token sequence set using regular expressions to generate regular expressions; S03. Merging the specified non-terminal symbols in the generalized regular expressions to transform them into a context-free grammar; S04. Using the upper and lower bounds of the tokens as constraints, performing character generalization on the character-level production rules of each token in the context-free grammar to obtain the final grammar output. This invention has the advantages of simple implementation, high generation effectiveness and efficiency, and complete grammar.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of program automatic testing, and in particular to a method for automatically generating a token-level program input grammar. Background Art

[0002] In the process of automatic software testing, the software usually only accepts inputs in certain specific formats, and if an input does not meet the format requirements, it will be rejected by the software and an input error report may be generated. For input formats with complex structures, there are usually some input grammars that can be specified by regular expressions or context-free grammars, such as programming language grammars. However, the above input grammars may not be available. Even if there are some descriptions of input grammars, such as natural language descriptions, the grammars may be incomplete and rarely in machine-readable formats. When the input grammar of software with complex input formats is not available, automatic testing of the software becomes very challenging. For example, in the process of symbolic execution and fuzz testing, a large number of inputs that do not meet the input format requirements may be generated and rejected by the software, making the testing process invalid or inefficient, such as resulting in low code coverage or failure to detect program errors, especially functional errors. If the input is rejected in the format checking stage, the software will not execute the test function code. Therefore, grammar learning can improve the automatic testing capabilities of software that accepts complex inputs. Grammar learning can significantly improve the effectiveness and efficiency of automatic testing tools. For example, the learned grammar can be used for grammar-based gray-box fuzz testing or grammar-oriented symbolic execution.

[0003] Automatically synthesizing input grammars for complex parsing programs is challenging. Traditional grammar synthesis methods have different feasibility, effectiveness, and efficiency. Different methods perform differently in various aspects, such as the availability of software source code (e.g., white box or black box), the expressiveness of the learned grammar, and the type of available oracle. The current state-of-the-art grammar learning methods usually assume that there is a set of initial valid inputs. Given an initial input set α, the target grammar is obtained by continuously expanding the grammar range, where the input is usually given at the character level, such as a string or byte file. However, valid inputs for programs are sometimes not available, and if valid inputs are required to have a good distribution, such inputs may not be available. In the absence of input, the above grammar learning methods cannot be used. Using symbolic execution to generate valid character-level inputs for grammar learning can solve the above problems, but the learning process based on character-level inputs is not efficient and may even limit the effectiveness of the learned grammar. Summary of the invention

[0004] The technical problem to be solved by the present invention is: in response to the technical problems existing in the prior art, the present invention provides a token-level program input grammar automatic generation method with simple implementation method, high effectiveness and efficiency of grammar generation and complete grammar, which can significantly improve the automatic testing capability of software that accepts complex input and improve the effectiveness and efficiency of automatic testing tools.

[0005] In order to solve the above technical problems, the technical solution proposed by the present invention is:

[0006] A token-level program input grammar automatic generation method, the steps comprising:

[0007] Step S01. Obtain a string that conforms to the parsing program grammar as the initial input and perform symbolic execution to generate a token sequence set corresponding to the parsing program and collect the upper and lower bounds of each token;

[0008] Step S02: generalize the token sequence set to generate a regular expression;

[0009] Step S03. merging the designated non-terminal symbols in the generalized regular expression to convert it into a context-free grammar;

[0010] Step S04. Using the upper and lower bounds of the token as constraints, character generalization is performed on the character-level production formula of each token in the context-free grammar to obtain the final grammar output, wherein the character-level production formula is used to represent the character-level value corresponding to each token.

[0011] Furthermore, in the step S01, the test case automatic generation tool GADSE (Grammar-Agnostic Dynamic Symbolic Execution) is used to automatically generate a set of token sequences that can be received by the parsing program and collect the upper and lower bounds corresponding to each token.

[0012] Furthermore, the regular expression generalization in step S02 uses repetition generalization and alternation generalization, wherein the repetition generalization is to repeat a given substring in the grammar, and the alternation generalization is to decompose a substring in the grammar and form an alternation.

[0013] Furthermore, the regular expression generalization in step S02 also uses exchange generalization, and the exchange generalization is performed by dividing the current expression L into three parts and exchanging the first part and the third part.

[0014] Furthermore, after each generalization in step S02, the step of testing each possible token regular expression includes: generating a representative token sequence, and then using the upper and lower bounds of the token as constraints, converting the generated token sequence into character-level input to test the validity of the current token sequence.

[0015] Furthermore, the step S03 includes: traversing all non-terminal symbols in the generalized regular expression, determining whether any non-terminal symbols are equivalent, and if so, merging the equivalent non-terminal symbols into one non-terminal symbol, and the regular expression after merging is the context-free grammar obtained by transformation.

[0016] Furthermore, in step S04, the upper limit and the lower limit of each token character are calculated using SMT, and each token character is generalized to a value between the upper limit and the lower limit to achieve character generalization.

[0017] Furthermore, after the character is generalized in step S04, the generalized character value is placed in the sequence where the token character appears and the validity of the character value is checked to check the generalized character value.

[0018] A token-level program input grammar automatic generation device, comprising:

[0019] The symbolic execution module is used to obtain a string that conforms to the parser grammar as the initial input and perform symbolic execution, generate a token sequence set corresponding to the parser and collect the upper and lower bounds of each token;

[0020] A first generalization module, used to perform regular expression generalization on the token sequence set to generate a regular expression;

[0021] A second generalization module is used to merge the designated non-terminal symbols in the generalized regular expression and convert it into a context-free grammar;

[0022] The third generalization module is used to use the upper and lower bounds of the token as constraints to perform character generalization on the character-level production formula of each token in the context-free grammar to obtain the final grammar output, wherein the character-level production formula is used to represent the character-level value corresponding to each token.

[0023] A computer-readable storage medium storing a computer program, wherein the computer program implements the above method when executed.

[0024] Compared with the prior art, the advantages of the present invention are:

[0025] 1. The present invention generates a token sequence for a program with a complex input format by using grammar-independent program symbol execution, and collects the upper and lower bounds of each token at the same time. Then, the generated token sequence is used to synthesize the input grammar, which is first generalized into a regular expression, and the non-terminal symbols in the regular expression are merged to convert it into a context-free grammar. Then, the character-level values ​​corresponding to each token in the context-free grammar are generalized in combination with the upper and lower bounds of the token value to further expand the grammar range, and the final grammar output is obtained. Token-level grammar synthesis can be achieved. Compared with the traditional character-level input learning method, the effectiveness and efficiency of grammar synthesis can be effectively improved. The token-level synthesis can also improve the integrity of grammar generation.

[0026] 2. The present invention further combines the test case automatic generation technology and uses GADSE to provide a large number of well-distributed initial inputs for grammar learning, further improving the automation level and effect of grammar synthesis. Grammar synthesis is implemented based on the token level. During the parsing process, one token represents a character sequence, which can save a lot of GADSE partitioning time and achieve faster and higher-level grammar synthesis.

[0027] 3. The present invention further considers the generation rules of non-terminal symbols, introduces a new exchange generalization operation based on the repetition and alternation operations, and realizes exchange generalization by dividing the expression into three parts and exchanging the first and third parts, which can further improve the accuracy of grammar generation.

[0028] 4. The present invention utilizes symbolic execution to collect token constraints and converts them into SMT optimization problems, so that SMT optimization can be used to generalize possible character-level values ​​of tokens, which can further improve the accuracy of the grammar. BRIEF DESCRIPTION OF THE DRAWINGS

[0029] Figure 1 It is a schematic diagram of the implementation flow of the token-level program input grammar automatic generation method of this embodiment.

[0030] Figure 2 It is a specific flowchart of the method for automatically generating a program input grammar at the token level of this embodiment.

[0031] Figure 3 It is a schematic diagram of an example program used in a specific application embodiment of the present invention.

[0032] Figure 4 It is a token constraint diagram obtained by using the GADSE tool in a specific application embodiment of the present invention.

[0033] Figure 5It is a schematic diagram of a token sequence obtained by using the GADSE tool in a specific application embodiment of the present invention.

[0034] Figure 6 It is a schematic diagram of the grammar synthesis result obtained in a specific application embodiment of the present invention. DETAILED DESCRIPTION

[0035] The present invention is further described below in conjunction with the accompanying drawings and specific preferred embodiments, but the protection scope of the present invention is not limited thereby.

[0036] like Figure 1 , 2 As shown, the steps of the token-level program input grammar automatic generation method of this embodiment include:

[0037] Step S01. Obtain a string that conforms to the parsing program grammar as the initial input and perform symbolic execution to generate a token sequence set corresponding to the parsing program and collect the upper and lower bounds of each token;

[0038] Step S02: Generalize the token sequence set to generate a regular expression;

[0039] Step S03. Merge the designated non-terminal symbols in the generalized regular expression and convert it into a context-free grammar;

[0040] Step S04. Using the upper and lower bounds of the token as constraints, character-level productions of each token in the context-free grammar are character-generalized to obtain the final grammar output. The character-level productions are used to represent the character-level values ​​corresponding to each token.

[0041] In lexical analysis, the input is first marked as a token sequence, and then the token sequence is checked according to the grammar rules in syntax analysis. The token sequence is closer to the production rules in the grammar. Therefore, for grammar learning, if it is possible to perform token-level learning of grammar, that is, to learn grammar based on token sequences, the learning process will be more efficient and the results will be more accurate and complete.

[0042] This embodiment uses a string that conforms to the grammar of the parsing program as the initial input and performs symbolic execution on the initial input to automatically generate a set of legal token sequences corresponding to the parsing program. That is, by first using grammar-independent program symbolic execution to generate a token sequence for a program with a complex input format, and collecting the upper and lower bounds of each token, and then using the generated token sequence to synthesize the input grammar, first using a generalization operation on the token sequence to generalize it into a regular expression, and then performing a merge operation on the non-terminal symbols in the generalized regular expression to obtain the recursive properties of the grammar and convert it into a context-free grammar, and then combining the upper and lower bounds of the token value to perform character generalization on the character-level values ​​corresponding to each token in the context-free grammar to further expand the grammar range, and obtain the final grammar output, which can achieve token-level grammar synthesis based on symbolic execution.

[0043] As a white-box learning method, the token-level grammar learning method based on symbolic execution in this embodiment can effectively improve the effectiveness and efficiency of grammar synthesis compared with the traditional character-level input learning method, and achieve more effective grammar synthesis. The token-level synthesis can also improve the completeness of grammar generation, and is particularly suitable for automatic learning and generation of input grammar for parsing programs with good grammar distribution and unavailable input samples.

[0044] In step S01 of this embodiment, the test case automatic generation tool GADSE is used to automatically generate a set of token sequences that can be received by the parsing program and collect the upper and lower bounds corresponding to each token, and the upper and lower bounds corresponding to each token are used as the constraint values ​​corresponding to each token. By combining the test case automatic generation technology, GADSE can provide a large number of well-distributed initial inputs for grammar learning, further improving the automation and effect of grammar synthesis.

[0045] As Figure 3 Take the example grammar in the example, where the upper part is the example grammar and the lower part is the parsing program of the grammar. The program actually implements a context-sensitive grammar. The grammar in the example program, where (...)+ and (...)* are regular expression symbols, <t>Represents a single character, <id>Represents an uppercase identifier with a length greater than 1. <number>Must be a single digit number, and <sep>Indicates a special separation symbol. The grammar requires that the tags should have the same name, for example, AA is effective, but AA is invalid. The above can be implemented using a recursive descent parser, and utilizing a stack to check if tags have the same name.

[0046] In a specific application embodiment, firstly, an initial input is given. ; ", and then use GADSE to perform symbolic execution on the initial input to generate the set of legal token sequences corresponding to the parsing program and the constraint values ​​corresponding to each token. There are 6 token values ​​in the example program. GADSE first generates constraints for the tokens in the parsing program, where the length of the token is 1, such as Figure 4 As shown. GADSE then generates token-level path constraints and solves them to obtain token sequences. By generating new token sequences to search the token-level path space, GADSE can explore the path space of the parser more effectively. Figure 5 As shown in Figure 1, the upper part represents the token-level path constraints of the initial input, and the lower part represents the two valid token sequences generated by GADSE exploring the path space of the parser.

[0047] This embodiment specifically uses repetition generalization and alternation generalization when generalizing regular expressions. Repetition generalization is to repeat a given substring in the grammar (for example, aa can be generalized to a(a)*), and alternation generalization is to decompose substrings in the grammar and form alternations (for example, (ab)* can be generalized to (a|b)*). For the token sequence generated in step S01, the token sequence is used as the initial input, and the regular generalization operation is applied to each token sequence to iteratively generate candidate regular grammars. Then, the correctness of these candidate grammars is checked by generating new inputs from the candidate grammars and checking whether the new inputs are accepted by the parsing program. The generalization operation continues to be applied to the candidate grammars that pass the check until generalization is impossible, and the final regular grammar is obtained, and then step S03 is entered.

[0048] In step S02 of this embodiment, the black box grammar synthesizer Glade is specifically used to generalize the regular expression. Glade starts from a set of valid inputs. For each input i, Glade gradually generalizes i to a regular expression e. The generalization process is a series of regular expressions L0,…,Ln, where L0 only contains i and Ln is e. For each 0≤i≤n-1, Each step of generalization attempts to expand Li, but at the same time, it is also necessary to avoid over-generalization. Glade provides two generalization operations: repetition and alternation. In repetition generalization, Li is first divided into three parts, that is, Li = α1α2α3, and then the second and third parts are repeated as follows:

[0049] α1([α2] alt )*[α3] rep (1)

[0050] The expressions in []rep and []alt respectively indicate that repetition and alternation are to be used to generalize the expressions.

[0051] Since there may be multiple partitioning situations, we further check the new regular expression by generating several strings for the new expression and checking whether these strings are acceptable to the program. If all the strings are valid, the new expression is taken as a candidate generalization expression. For example, for the example program, suppose i is AA; 0 And using repetition to generalize i, we can get the following four candidate rules and pass the check:

[0052] ([AA;] alt ) * [0 ] rep (2)

[0053] ([A] alt ) * [A;0 ] rep

[0054] A([A] alt ) * [;0 ] rep

[0055] AA([;0] alt )*[ ] rep

[0056] Glade further selects the repetition with the largest length, namely the first expression (AA; length is 3) as the generalized expression.

[0057] The alternation operation is to divide Li into two parts, that is, Li = a1α2, and recursively apply repetition and alternation to the first part and the second part respectively, that is, the rule is:

[0058] [α1] rep +[α2] alt (3)

[0059] Similar to the repeated operation, this embodiment also checks different candidates during the alternating operation and selects one of the candidates as the candidate generalized expression Li+1. For example, for the expression in formula (2), if AA cannot be separated, the expression can be obtained by simply repeating the expression:

[0060] ([AA;] rep )*[0 ] rep (4)

[0061] For each generalization step, Glade can ensure that Li is a subset of Li+1, and by checking possible generalization expressions, it can also try not to over-generalize. By default, the initial input i is first generated by repetition, AA; the generalization process of O is as follows:

[0062]

[0063] The above process has a total of 9 steps. Based on this generalization process, Glade generates the following grammar for the final regular expression:

[0064] S:= (S1) * 0 S1: =(A) + ; (5)

[0065] Glade can also generalize a single character by trying all possible characters. For example, the above context-free grammar can be generated into the following grammar:

[0066] S:= (S1) * (0|...|9) S1: =(A|...|Z) + (:|;) (6)

[0067] The final context-free grammar above is close to Figure 2 grammar in. For multiple initial valid inputs, Glade can generalize a grammar for each input and unify the generated grammar into one as a result. However, Glade needs to assume that the initial input is valid. The distribution of valid input also determines the completeness of the comprehensive grammar. Without the input grammar, it is difficult to obtain well-distributed valid input, such as input that covers all production rules and grammar terminal symbols. This embodiment can obtain well-distributed valid input by using dynamic symbolic execution (DSE) as the input generated by the program. Since Glade is synthesized based on the character level, in each generalization step, there are many possible partitions at the character level, so Glade needs a lot of time to check the validity of each partition, which also limits the generalization ability of Glade. This embodiment implements grammar synthesis based on the token level. During the parsing process, a token represents a character sequence, and Glade does not need to partition the token string, so a lot of partitioning time can be saved, and faster and higher-level grammar synthesis can be achieved.

[0068] Considering the non-terminal symbol <content>The production rule is:

[0069] <cpmtent> := <value> ( <sep> <content>)* (7)

[0070] in, <value>There are two cases, either an identifier or a number.

[0071] If the current expression is AA; 0, Glade's current generalization operation limits the generalization ability, because only sub-expressions (such as AA; or; 0) can be repeated. However, if you want to generalize to the following expression:

[0072] <value> ( <sep> <content>)* (8)

[0073] It is necessary to have both ;0 and ;AA (or AA; and 0;), which can be obtained by exchanging the first and third parts. Therefore, by exchanging the first and third parts, the generalization range can be expanded, generalizing AA;0 to <content>Regular expression for .

[0074] This embodiment utilizes the above features and also uses exchange ([]exh) generalization when generalizing the regular expression in step S02, that is, introduces an exchange generalization operation for generalization. The exchange generalization divides the current expression L into three parts and exchanges the first and third parts, which can be expressed as:

[0075] ((([α1] rep +[α3] rep )[α2] rep )*([α1] rep +[α3] rep )) (9)

[0076] For example, using the exchange operation, the token sequence can be generalized to the following three candidate regular token expressions:

[0077] LTR([IS] alt )*[NLTR] rep (10)

[0078] LTRI([SN] alt )*[LTR] rep

[0079] LTR([ISN] exh )[LTR] rep

[0080] For simplicity, in the above expressions, we use the first letter of the second part of each token value to represent the token, for example, S for T_Sep.

[0081] This embodiment also includes checking each possible token regular expression in each of the above generalization steps, specifically including: first generating a representative token sequence; then, converting the token sequence into character-level input through token value constraints (upper and lower bounds) and character-level constants (collected in GADSE) to check its validity. For example, LTRNSILTR (used to test the last expression) and LTRISISNLTR are converted into 0;AA and AA;AA;0 , they are all accepted by the parser, that is, they are all valid.

[0082] After checking the validity of the token, this embodiment gives priority to switching generalization to continue generalization. The generalization process of LTRISNLTR can be specifically expressed as follows:

[0083]

[0084] Each letter represents a token, corresponding to the first letter of the second part of each token value.

[0085] Following the above steps, we can get the following context-free grammar:

[0086] S:=(LTR S1 LTR)S1:=(S2 S)*S2 S2:=S3|S4 S3:=I S4:=N

[0087] In this embodiment, step S03 further merges the non-terminal symbols in the regular expression of step S02 to convert it into a context-free grammar. The steps include: traversing all non-terminal symbols in the generalized regular expression, determining whether any of the non-terminal symbols are equivalent, and if so, merging the equivalent non-terminal symbols into one non-terminal symbol. The regular expression after the merging is the converted context-free grammar.

[0088] As in the above context-free grammar, S1, S2, S3 and S4 are mergeable and can be replaced by S1. In addition, for the first production rule, S and S1 are also mergeable, but S and S2 are not mergeable. Therefore, this embodiment only merges S and S1 in the first rule, and the following grammar can be obtained after the merger:

[0089] S:=(LTR S LTR)|S1 S1:=S1SS1|I|N.

[0090] In step S04 of this embodiment, each token non-terminal symbol in the grammar result of step S3 is combined with the upper and lower limits of the token value to perform character generalization to further expand the grammar scope and obtain the final grammar. For each token character t, grammar synthesis is used to generalize its possible character values. After character generalization, the generalized character value is placed in the sequence where the token character appears and the validity of the character value is checked to check the generalized character value.

[0091] In step S04 of this embodiment, SMT is specifically used to calculate the upper and lower limits of each token character, and each token character is generalized to a value between the upper and lower limits to achieve character generalization. By using symbolic execution to collect token constraints and converting them into SMT optimization problems, SMT optimization can be used to generalize possible character-level values ​​of tokens, which can further improve the accuracy of the grammar.

[0092] For example, token I has a character value "AA", which can be generalized to the regular expression (A)+. In addition, the character-level value of each token is generalized by the token constraints collected in the first stage of Gadse. The upper and lower limits of each character are calculated using SMT optimization theory, and the token is generalized to a value between the upper and lower limits. For example, for the token value L, its constraint is s[0] = '<'; therefore, its value can only be '<'. The constraint for the token value N is s[0] ≥ '0' ∧ s[0] ≤ '9', so the value of N is between '0' and '9'. Finally, based on the above steps, the context-free grammar of formula (12) can be synthesized. This grammar is consistent with Figure 4 The grammars in are equivalent.

[0093]

[0094] In a specific application embodiment, Figure 3 Taking the last token sequence in as an example, the detailed steps for synthesizing the token sequence include:

[0095] Step 1: For the last token sequence, i.e., LTRILTR, repeat, replace, and exchange the token sequence based on the token synthesis;

[0096] Step 2: Test each possible token regular expression in each generalization step: first generate a representative token sequence; then, convert the token sequence into character-level input through token value constraints and constants to test validity.

[0097] Step 3: Merge the non-terminal symbols in the regular expression obtained after regular generalization to obtain the recursive property of the grammar and convert it into a context-free grammar.

[0098] Step 4: For each token value, generalize its character-level value through the token constraints (upper and lower bounds) collected by GADSE in step S01, where the upper and lower bounds of each character in the token are calculated using SMT optimization theory, and the token is generalized to a value between the lower and upper bounds. Based on the token sequence and token value constraints, the final context-free grammar output can be synthesized.

[0099] In a specific application embodiment, the grammar generation is implemented on a JPF-based concolic execution engine and Glade that is independent of grammar. The grammar result is as follows: Figure 6 As shown. Figure 6 It can be seen that the grammar finally generated by the synthesis of the present invention is consistent with the target grammar, and compared with the traditional character-level grammar constraints, the present invention can greatly improve the efficiency of grammar synthesis and the integrity of the grammar. Specifically, compared with the state-of-the-arts (i.e. Glade and Arvada) equipped with character-level symbolic execution, the F1 scores of the method of the present invention are improved by 5.24 times and 3.54 times respectively. Compared with Arvada, the present invention can achieve 141.93 times time acceleration in grammar synthesis, that is, the present invention can effectively improve the precision and recall rate of grammar generation.

[0100] The token-level program input grammar automatic generation device of this embodiment includes:

[0101] The symbolic execution module is used to obtain a string that conforms to the parser grammar as the initial input and perform symbolic execution, generate a token sequence set corresponding to the parser and collect the upper and lower bounds of each token;

[0102] The first generalization module is used to generalize the regular expression of the token sequence set to generate a regular expression;

[0103] The second generalization module is used to merge the specified non-terminal symbols in the generalized regular expression and convert it into a context-free grammar;

[0104] The third generalization module is used to use the upper and lower bounds of the token as constraints to perform character generalization on the character-level production formula of each token in the context-free grammar to obtain the final grammar output. The character-level production formula is used to represent the character-level value corresponding to each token.

[0105] The token-level program input grammar automatic generation device of this embodiment corresponds one to one with the token-level program input grammar automatic generation method described above, and will not be described one by one here.

[0106] This embodiment also provides a computer-readable storage medium storing a computer program, and the above method is implemented when the computer program is executed.

[0107] The above is only a preferred embodiment of the present invention, and does not limit the present invention in any form. Although the present invention has been disclosed as a preferred embodiment, it is not intended to limit the present invention. Therefore, any simple modification, equivalent change and modification made to the above embodiment according to the technical essence of the present invention without departing from the content of the technical solution of the present invention shall fall within the scope of protection of the technical solution of the present invention.< / content> < / content> < / sep> < / value> < / value> < / content> < / sep> < / value> < / cpmtent> < / content> < / sep> < / number> < / id> < / t>

Claims

1. A token-level program input grammar automatic generation method, characterized in that the steps include: Step S01. Obtain a string that conforms to the parsing program grammar as the initial input and perform symbolic execution to generate a token sequence set corresponding to the parsing program and collect the upper and lower bounds of each token; Step S02: generalize the token sequence set to generate a regular expression; Step S03. merging the designated non-terminal symbols in the generalized regular expression to convert it into a context-free grammar; Step S04. Using the upper and lower bounds of the token as constraints, character generalization is performed on the character-level production formula of each token in the context-free grammar to obtain a final grammar output, wherein the character-level production formula is used to represent the character-level value corresponding to each token; In step S02, the regular expression generalization is performed using repetition generalization and alternation generalization, wherein the repetition generalization is to repeat a given substring in the grammar, and the alternation generalization is to decompose a substring in the grammar and form an alternation; In the step S04, the upper limit and the lower limit of each token character are calculated using SMT, and each token character is generalized to a value between the upper limit and the lower limit to achieve character generalization.

2. The method for automatically generating a token-level program input grammar according to claim 1, characterized in that: In the step S01, the test case automatic generation tool GADSE is used to automatically generate a set of token sequences that can be received by the parsing program and collect the upper and lower bounds corresponding to each token.

3. The method for automatically generating a token-level program input grammar according to claim 1, characterized in that: The regular expression generalization in step S02 also uses exchange generalization, which is performed by dividing the current expression L into three parts and exchanging the first part and the third part.

4. The method for automatically generating a token-level program input grammar according to claim 1, characterized in that: After each generalization in step S02, the step of testing each possible token regular expression includes: generating a representative token sequence, and then using the upper and lower bounds of the token as constraints, converting the generated token sequence into character-level input to test the validity of the current token sequence.

5. The method for automatically generating a token-level program input grammar according to any one of claims 1 to 4, characterized in that: The step S03 includes: traversing all non-terminal symbols in the generalized regular expression, determining whether any non-terminal symbols are equivalent, and if so, merging the equivalent non-terminal symbols into one non-terminal symbol, and the regular expression after merging is the context-free grammar obtained by transformation.

6. The method for automatically generating a token-level program input grammar according to claim 5, characterized in that: After the character is generalized in step S04, the generalized character value is placed in the sequence where the token character appears and the validity of the character value is checked to check the generalized character value.

7. A token-level program input grammar automatic generation device, characterized in that: include: The symbolic execution module is used to obtain a string that conforms to the parser grammar as the initial input and perform symbolic execution, generate a token sequence set corresponding to the parser and collect the upper and lower bounds of each token; A first generalization module, used to perform regular expression generalization on the token sequence set to generate a regular expression; A second generalization module is used to merge the designated non-terminal symbols in the generalized regular expression and convert it into a context-free grammar; A third generalization module is used to use the upper and lower bounds of the token as constraints to perform character generalization on the character-level production formula of each token in the context-free grammar to obtain a final grammar output, wherein the character-level production formula is used to represent the character-level value corresponding to each token; The first generalization module performs regular expression generalization using repetition generalization and alternation generalization, wherein the repetition generalization is to repeat a given substring in the grammar, and the alternation generalization is to decompose a substring in the grammar and form an alternation; In the third generalization module, SMT is used to calculate the upper limit and lower limit of each token character, and each token character is generalized to a value between the upper limit and the lower limit to achieve character generalization.

8. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed, the method according to any one of claims 1 to 6 is implemented.

Citation Information

Patent Citations

  • Test case generation method restrained by context-free grammar

    CN102141959A

  • Unit test automatic execution method based on test case automatic generation algorithm

    CN110543421A

  • Dynamic lexer object construction

    US20180232219A1

  • Mapping Natural Language To Queries Using A Query Grammar

    US20210019309A1