A symbolic execution optimization method based on partial solution caching and reuse
By collecting and updating path constraints during the symbolic execution process and using the partial dereuse mechanism, the cache hit rate is improved, the problem of low symbolic execution efficiency is solved, and the efficiency of test case generation for various programs is improved.
Patent Information
- Application Number
- CN202210259487.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-03-16
- Publication Date
- 2024-09-24
- Estimated Expiration
- 2042-03-16
AI Technical Summary
The problem of low symbolic execution efficiency leads to a low cache hit rate during test case generation, affecting the efficiency of symbolic execution.
By collecting path constraints during the symbolic execution process, it is determined whether the cache is hit. If it is not hit, the solution is performed, the partial solution and the final solution are recorded, and stored in the cache. The cache is updated according to the matching relationship between the partial solution and the atomic constraints, so that Partial decomposition can be reused to improve cache hit rate.
It effectively improves the Cache hit rate, improves the symbolic execution efficiency in the test case generation process, is suitable for all types of programs, and improves the overall symbolic execution efficiency of software testing.
Smart Images

Figure CN114706759B
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of symbolic execution in software automatic testing, and in particular to a symbolic execution optimization method based on partial solution caching and reuse. Background Art
[0002] Software testing is an indispensable part of modern software development process, which is to discover vulnerabilities in software by constructing as many test cases as possible. At present, the scale of most software is quite large, and it is almost impossible for people to manually construct test cases. Therefore, how to automatically generate test cases is a very important issue in software testing. Symbolic execution is a technical means to automatically generate test cases. Symbolic execution symbolizes the input and collects path conditions while executing the program. When a path is explored, the collected path conditions will be handed over to the constraint solver for solution. If the path conditions are satisfyable, the result of the constraint solution is the test case. Symbolic execution can generate a large number of effective test cases by continuously exploring program paths and solving path constraints.
[0003] In the symbolic execution process of generating test cases, since there are often a large number of paths in real programs, the constraint solver needs to be called frequently during the symbolic execution process. During the constraint solving process, the constraint solver will continuously assign and verify the variables in the constraint. Usually, solving a constraint requires multiple assignments and verifications. The assignments that fail to pass the verification during the solving process are called partial solutions. Constraint solving is essentially a search of the input space, which is a very time-consuming process. The efficiency of symbolic execution is heavily dependent on the efficiency of constraint solving. Therefore, the efficiency of constraint solving in symbolic execution will directly affect the efficiency of test case generation.
[0004] There are many ways to improve the efficiency of symbolic execution, such as constraint simplification, solution caching and reuse, etc. The caching mechanism can reduce the calls to the solver and is an effective and widely used optimization method. The basic process of caching is: standardize the constraints, store the constraints and constraint solution results, so that they can be reused when the same or similar constraints are encountered in the future. Usually, the standardized constraints and constraint solution results are stored in the cache as key-value pairs. In the above caching mechanism, only one solution can be stored for one constraint, but in fact, a constraint may have multiple solutions. Therefore, in the traditional caching mechanism, the solution cannot be effectively stored and used, resulting in a low cache hit rate. Therefore, improving the cache hit rate is an important means to improve the efficiency of symbolic execution.
[0005] Some practitioners have proposed to improve the cache hit rate for formulas (or programs) of a specific form under the Caching mechanism. This type of method usually targets formulas with symbolic addresses and merges formulas with similar structures, making it easier for such formulas to hit when performing cache queries. However, this type of solution can only improve the cache hit rate of formulas or programs with symbolic addresses, and has poor versatility, resulting in low overall symbolic execution efficiency during software automatic testing. Therefore, how to universally improve the cache hit rate of various types of programs during the symbolic execution process of software automatic testing, thereby improving the efficiency of symbolic execution when generating test cases, remains an urgent problem to be solved. Summary of the invention
[0006] The technical problem to be solved by the present invention is: in order to address the problem of low symbolic execution efficiency, the cache hit rate can be generally and effectively improved, thereby improving the efficiency of symbolic execution in the test case generation process. The present invention provides a symbolic execution optimization method based on partial caching and reuse.
[0007] In order to solve the above technical problems, the technical solution proposed by the present invention is:
[0008] A symbolic execution optimization method based on partial solution caching and reuse, comprising the steps of:
[0009] Step S1. When using symbolic execution to analyze the program under test to generate test cases, path constraints in the symbolic execution process are collected;
[0010] Step S2. Determine whether the collected path constraints can hit the current Cache. If so, return to step S1. If not, proceed to step S3.
[0011] Step S3. The constraint solver solves the missed path constraints and records the partial solutions and final solutions obtained;
[0012] Step S4. Store the path constraint and the recorded final solution into the cache, and based on the matching relationship between the partial solution and the atomic constraint of the path constraint, store the partial solution and the constraint into the current cache to update the current cache, so that when the same or similar constraints are encountered during the symbolic execution process, the corresponding partial solution can be reused, and return to execute step S1.
[0013] Furthermore, the method for determining whether the cache can be hit in step S2 is:
[0014] If there is a first key-value pair [C, R] in the cache, then the cache is considered to be hit, where C is the constraint that needs to be solved currently and R is the solution corresponding to C in the cache;
[0015] Or if there is a second key-value pair [C1, R] in the cache, where C1 is a subset of the constraint C that needs to be solved, and R is the solution corresponding to C1 in the cache, when R satisfies C, it is considered a cache hit;
[0016] Or if there is a third key-value pair [C2, R] in the cache, where C2 is a superset of the constraint C that currently needs to be solved, and R is the solution corresponding to C2 in the cache, then a cache hit is determined.
[0017] Furthermore, in step S3, an SMT solver is used to solve the missed path constraints.
[0018] Furthermore, in step S3, when the constraint solver solves the missed path constraint, it determines whether the current assignment is a partial solution or a final solution by performing assignments and verifications multiple times. If the current assignment passes the verification, it determines that the current assignment is a final solution; otherwise, it determines that the current assignment is a partial solution.
[0019] Furthermore, the specific steps in step S4 are: for each atomic constraint of the path and each partial solution, determine whether the partial solution satisfies the atomic constraint, and store the partial solution and the corresponding constraint into the cache according to the determination result of whether the partial solution satisfies the atomic constraint.
[0020] Furthermore, storing the partial solution and the corresponding constraint into the cache according to the judgment result of whether the partial solution satisfies the atomic constraint includes: if the partial solution satisfies the atomic constraint, storing the partial solution and the atomic constraint into the cache accordingly; if the partial solution does not satisfy the atomic constraint, inverting the condition of the atomic constraint to generate a new constraint, and storing the new constraint and the partial solution into the cache accordingly.
[0021] A symbolic execution optimization device based on partial solution caching and reuse, comprising:
[0022] A constraint collection module, used for collecting path constraints in the symbolic execution process when the program under test is analyzed using symbolic execution to generate test cases;
[0023] The hit judgment module is used to judge whether the collected path constraints can hit the current cache. If so, it returns to the execution constraint collection module. If not, it goes to the execution constraint solving module.
[0024] A constraint solving module, used for solving the missed path constraints by a constraint solver, and recording the partial solutions and final solutions obtained by solving the path constraints;
[0025] A cache update module is used to store the path constraint and the recorded final solution into the cache, and based on the matching relationship between the partial solution and the atomic constraint of the path constraint, store the partial solution and the constraint into the current cache to update the current cache, so that when the same or similar constraints are encountered during the symbolic execution process, the corresponding partial solution is reused and returned to the execution constraint collection module.
[0026] A symbolic execution optimization device based on partial solution caching and reuse includes a processor and a memory, wherein the memory is used to store a computer program, the processor is used to execute the computer program, and the processor is used to execute the computer program to perform the above method.
[0027] A computer-readable storage medium stores a computer program for being executed by a computer device to implement the above method.
[0028] Compared with the prior art inventions, the advantages of the present invention are:
[0029] 1. The present invention uses symbolic execution to analyze the program under test during the automatic software testing process to generate test cases. By collecting path constraints during the symbolic execution process, each constraint is judged whether it hits the cache. If it does not hit, a constraint solver is used to solve it. While recording the final solution, the partial solution and the corresponding constraint are recorded and stored in the cache, so that the cache can be continuously updated and expanded quickly and accurately using the search results of the symbolic execution. In the subsequent symbolic execution process, when encountering the same or similar constraints, the partial solution previously stored in the cache can be reused, that is, the partial solution is reused, so that the partial solution can be fully utilized to continuously expand the cache, effectively improving the cache hit rate, and then improving the efficiency of symbolic execution in the test case generation process.
[0030] 2. The present invention can be applied to the automatic testing of various programs. It is not targeted at a specific form of formula or program, nor is it limited to a specific search strategy. It can generally improve the cache hit rate of symbolic execution in the process of generating test cases for various programs, thereby improving the overall symbolic execution efficiency during software testing. BRIEF DESCRIPTION OF THE DRAWINGS
[0031] Figure 1 It is a schematic diagram of the implementation flow of the symbolic execution optimization method based on partial solution caching and reuse in this embodiment.
[0032] Figure 2 It is a schematic diagram of the effect of judging whether a constraint can be cached in a specific application embodiment.
[0033] Figure 3 It is a schematic diagram of the effect of recording partial solutions and final solutions in a specific application embodiment.
[0034] Figure 4 It is a schematic diagram of the effect of updating the Cache according to the partial solution and the final solution in a specific application embodiment.
[0035] Figure 5 It is a schematic diagram of the effect of partial solution reuse in a specific application embodiment. DETAILED DESCRIPTION
[0036] 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.
[0037] like Figure 1 As shown, this embodiment provides a symbolic execution optimization method based on partial solution caching and reuse, including the steps of:
[0038] Step S1. When using symbolic execution to analyze the program under test to generate test cases, path constraints in the symbolic execution process are collected;
[0039] Step S2. Determine whether the collected path constraints can hit the current Cache. If so, return to step S1. If not, proceed to step S3.
[0040] Step S3. The constraint solver solves the missed path constraints, records the partial solutions and final solutions obtained, and proceeds to step S4;
[0041] Step S4. Store the path constraints and the recorded final solutions into the cache, and based on the matching relationship between the partial solutions and the atomic constraints of the path constraints, store the partial solutions and the constraints into the current cache to update the current cache, so that when the same or similar constraints are encountered during the symbolic execution process, the corresponding partial solutions are reused, and return to execute step S1.
[0042] This embodiment uses symbolic execution to analyze the program under test during the automatic software testing process to generate test cases. By collecting path constraints in the symbolic execution process, each constraint is judged whether it hits the cache. If it does not hit, the constraint solver is used to solve it. While recording the final solution, the partial solution and the corresponding constraints are recorded and stored in the cache, so that the cache can be continuously updated and expanded quickly and accurately using the search results of symbolic execution. In the subsequent symbolic execution process, when encountering the same or similar constraints, the partial solution previously stored in the cache can be reused, that is, the partial solution is reused, so that the partial solution can be fully utilized to continuously expand the cache, effectively improving the cache hit rate, and then improving the overall symbolic execution efficiency in the test case generation process.
[0043] The above method in this embodiment can be applied to the automatic testing of various programs. It is not targeted at a specific form of formula or program, nor is it limited to a specific search strategy. It can generally improve the cache hit rate of symbolic execution in the process of generating test cases for various programs, thereby improving the symbolic execution efficiency during software testing.
[0044] Assuming that the constraint to be solved is C, in step S2 of this embodiment, the following three mechanisms can be used to determine whether the cache can be hit:
[0045] Mechanism 1: If there is a key-value pair [C, R] in the cache, then the cache hits, where C is the constraint that needs to be solved, and R is the solution corresponding to C in the cache;
[0046] Mechanism 2: If there is a key-value pair [C1, R] in the cache, where C1 is a subset of the constraint C that needs to be solved, and R is the solution corresponding to C1 in the cache, then the cache hits when R satisfies C;
[0047] Mechanism 3: If there is a key-value pair [C2, R] in the cache, where C2 is a superset of the constraint C that currently needs to be solved, and R is the solution corresponding to C2 in the cache, then the cache hits.
[0048] The above three mechanisms can be selected at any one according to actual needs, or selection conditions can be set for each mechanism, and the corresponding mechanism can be used when the preset conditions are met.
[0049] In step S3 of this embodiment, when the constraint solver solves the missed path constraint, it determines whether the current assignment is a partial solution or a final solution by performing multiple assignments and verifications. If the current assignment passes the verification, it determines that the current assignment is a final solution, otherwise it determines that the current assignment is a partial solution. During each assignment verification, the constraint solver extracts all variables in the path constraint and assigns each variable a value according to a preset strategy. If the current assignments of all variables can satisfy the path condition, it determines that the current assignment is a final solution, otherwise it determines that the current assignment is a partial solution and reassigns the variables.
[0050] In this embodiment, in step S3, an SMT solver is specifically used to solve the missed path constraints, that is, the constraint solver is an SMT solver.
[0051] In this embodiment, the specific steps of step S4 are: for each atomic constraint and each partial solution, determine whether the partial solution satisfies the atomic constraint, and store the partial solution and the corresponding constraint into the cache to expand the cache according to the judgment result of whether it satisfies the atomic constraint. Specifically, if the partial solution satisfies the atomic constraint, the partial solution and the atomic constraint are stored in the cache accordingly. If not, it indicates that the partial solution must satisfy the negation of the atomic constraint. Therefore, the condition of the atomic constraint is negated to generate a new constraint, and the new constraint and the partial solution are stored in the cache accordingly. That is, if the partial solution satisfies the atomic constraint, the partial solution and the atomic constraint are directly stored in the cache accordingly. If the partial solution does not satisfy the atomic constraint, the atomic constraint is negated and then stored in the cache accordingly with the partial solution. For example, for each atomic constraint C of the path constraint C1 i And every partial solution S of C1 i , judge S i Does it meet C i , if S i Satisfy C i , {C i , S i} into the Cache, otherwise, Store in cache.
[0052] As Figure 2 The program analysis shown is used as an example to further illustrate the above method of the present invention, where x and y are both 8-bit signed integers, and there are constraints C(x+y>10) and solutions R({x:10,y:0}) in the cache. The process of the symbolic execution optimization method based on partial solution caching and reuse in the program analysis example of this embodiment is as follows:
[0053] Step S1. After using symbolic execution to analyze the program, the constraint that needs to be solved is C1, C is a subset of C1, but R does not satisfy C1, so the cache misses;
[0054] Step S2. Figure 3 As shown, the path constraint C1 is submitted to the constraint solver for solving. When the assignments {x:0, y:0} and {x:58, y:81} fail to be verified, these two assignments are recorded as partial solutions, and the assignment {x:26, y:17} that passes the verification is recorded as the final solution.
[0055] Step S3. Figure 4 As shown, first, the path constraint C1 and the final solution R are stored in the cache, and then, for each atomic constraint C of the path constraint C1, i and every partial solution S of C1 i , judge S i Does it meet C i , if S i Satisfy C i , {C i , S i} into the Cache, otherwise, Stored in cache, the partial solution stored in cache can be reused in subsequent symbolic execution process to improve cache hit rate.
[0056] For example, when the program analysis example of this embodiment needs to solve the following Figure 5 When constraint C2 is shown, C2 can be cached by utilizing the partial solution pre-stored in the cache, thereby improving the cache hit rate and reducing the number of times the solver is called.
[0057] Through the above method, this embodiment can quickly and accurately expand the cache, thereby effectively utilizing partial solutions, improving the cache hit rate, and further improving the efficiency of symbolic execution in the test case generation process, thereby effectively improving the test case generation efficiency. In addition, this embodiment is not targeted at a certain form of formula or program, nor is it limited to a certain specific search strategy. It can be generally applied to various types of programs to improve the cache hit rate, thereby effectively improving the overall symbolic execution efficiency in software automatic testing.
[0058] The symbolic execution optimization device based on partial solution caching and reuse in this embodiment includes:
[0059] A constraint collection module, used for collecting path constraints in the symbolic execution process when the program under test is analyzed using symbolic execution to generate test cases;
[0060] The hit judgment module is used to judge whether the collected path constraints can hit the current cache. If so, it returns to the execution constraint collection module. If not, it goes to the execution constraint solving module.
[0061] A constraint solving module, used for solving the missed path constraints by a constraint solver, and recording the partial solutions and final solutions obtained by solving the path constraints;
[0062] The cache update module is used to store the path constraints and the recorded final solutions into the cache, and according to the matching relationship between the partial solutions and the atomic constraints of the path constraints, store the partial solutions and the constraints into the current cache to update the current cache, so that when the same or similar constraints are encountered during the symbolic execution process, the corresponding partial solutions are reused and returned to the execution constraint collection module.
[0063] The symbolic execution optimization device based on partial solution caching and reuse in this embodiment can also be composed of the following two parts: a processor and a memory, the memory is used to store a computer program, and the processor is used to execute the computer program to execute the above method.
[0064] This embodiment also provides a computer-readable storage medium, in which a computer program for being executed by a computer device to implement the above method is stored.
[0065] 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.
Claims
1. A symbolic execution optimization method based on partial solution caching and reuse, applied to software testing, characterized in that: Includes steps: Step S1. When using symbolic execution to analyze the program under test to generate test cases, path constraints in the symbolic execution process are collected; Step S2. Determine whether the collected path constraints can hit the current Cache. If so, return to step S1. If not, proceed to step S3. Step S3. The constraint solver solves the missed path constraints and records the partial solutions and final solutions obtained; Step S4. The path constraint and the recorded final solution are stored in the cache, and according to the matching relationship between the partial solution and the atomic constraint of the path constraint, the partial solution and the constraint are stored in the current cache to update the current cache, so that when the same or similar constraints are encountered during the symbolic execution process, the corresponding partial solution is reused, and the execution returns to step S1; The specific steps in step S4 are: for each atomic constraint of the path and each partial solution, determine whether the partial solution satisfies the atomic constraint, and store the partial solution and the corresponding constraint into the cache according to the determination result of whether it is satisfied; storing the partial solution and the corresponding constraint into the cache according to the determination result of whether it is satisfied includes: if the partial solution satisfies the atomic constraint, storing the partial solution and the atomic constraint into the cache accordingly; if the partial solution does not satisfy the atomic constraint, inverting the condition of the atomic constraint to generate a new constraint, and storing the new constraint and the partial solution into the cache accordingly.
2. The symbolic execution optimization method based on partial solution caching and reuse according to claim 1, characterized in that: The method for determining whether the cache can be hit in step S2 is: If there is a first key-value pair [C, R] in the cache, then the cache is considered to be hit, where C is the constraint that needs to be solved currently and R is the solution corresponding to C in the cache; Or if there is a second key-value pair [C1, R] in the cache, where C1 is a subset of the constraint C that needs to be solved, and R is the solution corresponding to C1 in the cache, when R satisfies C, it is considered a cache hit; Or if there is a third key-value pair [C2, R] in the cache, where C2 is a superset of the constraint C that currently needs to be solved, and R is the solution corresponding to C2 in the cache, then a cache hit is determined.
3. The symbolic execution optimization method based on partial solution caching and reuse according to claim 1, characterized in that: In step S3, an SMT solver is used to solve the missed path constraints.
4. The symbolic execution optimization method based on partial solution caching and reuse according to claim 1, characterized in that: In step S3, when the constraint solver solves the missed path constraint, it determines whether the current assignment is a partial solution or a final solution by performing assignments and verifications multiple times. If the current assignment passes the verification, it determines that the current assignment is a final solution; otherwise, it determines that the current assignment is a partial solution.
5. A symbolic execution optimization device based on partial solution caching and reuse, applied to software testing, characterized in that: include: A constraint collection module, used for collecting path constraints in the symbolic execution process when the program under test is analyzed using symbolic execution to generate test cases; The hit judgment module is used to judge whether the collected path constraints can hit the current cache. If so, it returns to the execution constraint collection module. If not, it goes to the execution constraint solving module. A constraint solving module, used for solving the missed path constraints by a constraint solver, and recording the partial solutions and final solutions obtained by solving the path constraints; A cache update module, used to store the path constraint and the recorded final solution into the cache, and according to the matching relationship between the partial solution and the atomic constraint of the path constraint, store the partial solution and the constraint into the current cache to update the current cache, so that when the same or similar constraints are encountered during the symbolic execution process, the corresponding partial solution is reused and returned to the execution constraint collection module; In the cache update module, for each atomic constraint of the path and each partial solution, it is determined whether the partial solution satisfies the atomic constraint, and the partial solution and the corresponding constraint are stored in the cache according to the determination result of whether the partial solution satisfies the atomic constraint; the storing of the partial solution and the corresponding constraint in the cache according to the determination result of whether the partial solution satisfies the atomic constraint includes: if the partial solution satisfies the atomic constraint, the partial solution and the atomic constraint are stored in the cache accordingly; if the partial solution does not satisfy the atomic constraint, the condition of the atomic constraint is negated to generate a new constraint, and the new constraint and the partial solution are stored in the cache accordingly.
6. A symbolic execution optimization device based on partial solution caching and reuse, comprising a processor and a memory, wherein the memory is used to store a computer program, and the processor is used to execute the computer program, characterized in that: The processor is configured to execute the computer program to perform the method according to any one of claims 1 to 4.
7. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program for being executed by a computer device to implement the method according to any one of claims 1 to 4.
Citation Information
Patent Citations
Software automatic testing method based on function level selection symbolization mixed execution
CN114138669A
Method and apparatus for finding errors in software programs using satisfiability of constraints
US20040117772A1
