Code execution coverage statistics method, device and computing equipment
By analyzing the function information of the target code and source code, and using disassemblers and simulation verification tools, code coverage statistics without instrumentation were achieved. This solves the software stability and reliability problems caused by instrumentation in existing technologies, and improves the accuracy of calculation results and software production efficiency.
Patent Information
- Application Number
- CN202210245230.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-03-14
- Publication Date
- 2025-01-14
- Estimated Expiration
- 2042-03-14
AI Technical Summary
Existing technologies for calculating code coverage in embedded systems via instrumentation lead to problems in software production processes, such as redundant instrumentation code, increased target code file storage size, and frequent source code modifications, which reduce the stability and reliability of the software.
Using a non-instrumentation method, the function information of the target code and source code is analyzed, and the coverage of the target code and source code is calculated using disassembler and simulation verification tools. Based on the disassembler statistics, the coverage is accurately calculated.
It improves the stability and reliability of software products, reduces labor costs, avoids software vulnerabilities caused by instrumentation and the problem of target code files not being able to be downloaded to the target board, and makes the calculation results more convincing and accurate.
Smart Images

Figure CN114661588B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the embedded field, and in particular to the problem of code coverage statistics of embedded systems. Background Art
[0002] As the complexity of weapons and equipment increases significantly, the embedded systems running on the equipment are becoming more and more complex, and the functional iterations are increasing. There may be some useless code in the code, or branches that cannot be tested during execution, which will bring great vulnerabilities to the software and seriously reduce the reliability of the software. Therefore, it is necessary to build a device or method that can dynamically analyze the code execution coverage to detect and screen the executed and unexecuted code, and perform coverage statistics of the executed code.
[0003] Through coverage statistical analysis, we can obtain the execution coverage of embedded software, check which functions in the software are not executed, which branches in the functions are not executed, and then analyze the reasons why the functions or branches in the software are not executed. We can adjust the test method to test as many functions and function branches as possible, thereby greatly improving the reliability of the software.
[0004] At present, most coverage statistics technologies are to count coverage by inserting stubs in the source code, that is, to write specific flag statements at the function entrance, branch statements, etc., and count the flag information after execution to calculate the coverage. However, this will bring many problems, such as: the need to modify the source code, the increase of the target code file, etc. On the one hand, it increases the labor cost, and on the other hand, due to the large size of the target code, it cannot be downloaded to the target board and a series of other problems.
[0005] Therefore, there is an urgent need to design a method and device that can count coverage without inserting code to solve the above problems: this method and device can avoid problems such as software vulnerabilities caused by redundant inserting code in the software production process, increased storage volume of generated target code files, and frequent source code modifications caused by inserting code, thereby improving the stability and reliability of software products. Summary of the invention
[0006] The purpose of the present invention is to provide a method for counting code execution coverage, which realizes a method and device for counting coverage without inserting code, and can effectively avoid the problems of software vulnerabilities caused by redundant inserting code in the software production process, increased storage volume of generated target code files, and frequent source code modifications caused by inserting code, thereby improving the stability and reliability of software products.
[0007] To achieve the above-mentioned purpose, the present invention provides a method for code execution coverage statistics, the method comprising: parsing the function information of the target code, and obtaining the target code function instruction information, the target code function branch instruction information, and the target code instruction corresponding source code information. The target code instruction corresponding source code information includes the total number of function statements and the number of line numbers with corresponding relationships in the function. The target code coverage is calculated based on the target code function instruction information, the target code function branch instruction information, and the target code instruction corresponding source code information. The source code is abstracted into a syntax tree, and the syntax tree is parsed to obtain the source code function information. The source code function information is parsed to obtain the source code function line number information and the source code function branch line number information. The source code coverage is calculated based on the target code instruction corresponding source code information, the source code function line number information, and the source code function branch line number information.
[0008] As a preferred method, parsing the function information of the target code specifically includes: processing the disassembly file information, extracting the corresponding information between the instruction line number and the instruction address of the disassembly file. Obtaining the instruction line number in the source code corresponding to the instruction line number according to the instruction line number of the disassembly file. Obtaining the instruction address of the instruction execution line number of the source code according to the instruction address dynamically executed according to the instruction line number in the source code.
[0009] As a preferred method, processing the disassembly file information and extracting the line number and instruction address correspondence information of the disassembly file includes: parsing the disassembly file to obtain all function instruction address information and jump instruction information.
[0010] As a preferred method, obtaining the jump information includes: obtaining the address of the jump instruction to determine whether the jump instruction is executed, obtaining the address of the jump to determine whether to enter the True branch, and obtaining the next address of the jump instruction to determine whether to execute the False branch.
[0011] As a preferred method, the source code function information is parsed, and the source code function line number information and the source code function branch line number information are obtained therefrom, including: after obtaining the abstract syntax tree, the line number of the code block in the syntax number is extracted. Whether the code is executed is determined according to the type of the code block, and the instruction execution line number and the instruction address corresponding to the instruction execution line number are recorded. The types of code blocks include for code blocks, if code blocks, else code blocks, while code blocks, switch code blocks, case code blocks, and default code blocks. If the type of the code block is a for code block or a while code block, if the starting line of the branch is executed, it is judged that the False branch of the branch is executed, and the instruction information related to the False branch is recorded. If the code in the branch block is executed, it is judged that the True branch of the branch is executed, and the instruction information related to the True branch is recorded.
[0012] As a preferred method, the required target code coverage is calculated by the following formula. The target code coverage includes the target code total statement coverage, the target code total branch coverage, the target code function statement coverage and / or the target code single function branch coverage, where:
[0013]
[0014]
[0015]
[0016]
[0017] As a preferred method, the required source code coverage is calculated by the following formula. Source code coverage includes source code total statement coverage, source code total branch coverage, source code function statement coverage and / or source code single function branch coverage, where:
[0018]
[0019]
[0020]
[0021]
[0022] As a preferred embodiment, the present invention provides a device for code execution coverage statistics, including: a target code coverage statistics module, which is used to parse the function information of the target code, and obtain the target code function instruction information, the target code function branch instruction information, and the target code instruction corresponding source code information. And it is used to calculate the target code coverage based on the target code function instruction information, the target code function branch instruction information and the target code instruction corresponding source code information. The target code instruction corresponding source code information includes the total number of function statements and the number of line numbers with corresponding relationships in the function. The source code coverage statistics module is used to abstract the source code into a syntax tree, parse the syntax tree to obtain the source code function information. Parse the source code function information to obtain the source code function line number information and the source code function branch line number information. The source code coverage is calculated based on the target code instruction corresponding source code information, the source code function line number information and the source code function branch line number information.
[0023] As a preferred embodiment, the present invention provides a computing device, comprising: one or more processors, a memory, and one or more programs. The one or more programs are stored in the memory and configured to be executed by the one or more processors, and the one or more programs include instructions for executing the code execution coverage statistics method according to the present invention.
[0024] As a preferred embodiment, the present invention provides a computer-readable storage medium storing one or more programs, wherein the one or more programs include instructions that, when executed by a computing device, enable the computing device to execute the method in the code execution coverage statistics method according to the present invention.
[0025] The present invention realizes a method and device for statistical coverage without inserting stub codes, which can effectively avoid the problems of software vulnerabilities caused by redundant stub codes in the software production process, the increase in the storage volume of the generated target code file resulting in the inability to download to the target board, and frequent source code modifications caused by stub codes, thereby improving the stability and reliability of software products and reducing the labor cost of producing software products. BRIEF DESCRIPTION OF THE DRAWINGS
[0026] Figure 1 The first flowchart of the code execution coverage statistics of the present invention is shown in FIG.
[0027] Figure 2 A schematic diagram of storing execution instruction addresses for code execution coverage statistics of the present invention.
[0028] Figure 3 It is a schematic diagram of the storage format of the disassembly file of the present invention.
[0029] Figure 4 Bottom view of the sensor controller programming tooling of the present invention.
[0030] Figure 5 A second flow chart of code execution coverage statistics according to the present invention.
[0031] Figure 6 The third flowchart of the code execution coverage statistics of the present invention is shown.
[0032] Figure 7 It is a schematic diagram of parsing the instruction format of the disassembly file of the present invention.
[0033] Figure 8 Input and output diagram for the GNU compilation tool chain.
[0034] Fig. 9 The simulation platform can count the assembly instruction diagrams that have been run.
[0035] Fig.10 Schematic diagram of the storage format of instruction information in a disassembled file. DETAILED DESCRIPTION
[0036] Hereinafter, embodiments of the method, apparatus, computing device, and storage medium for counting code execution coverage of the present invention will be described with reference to the accompanying drawings.
[0037] The embodiments described herein are specific embodiments of the present invention, which are used to illustrate the concept of the present invention, are illustrative and exemplary, and should not be interpreted as limiting the embodiments of the present invention and the scope of the present invention. In addition to the embodiments described herein, those skilled in the art can also adopt other obvious technical solutions based on the claims and the contents disclosed in the specification of this application, including any obvious replacement and modification of the embodiments described herein.
[0038] Embodiment 1
[0039] Zero-insertion technology needs to analyze the target code and source code of embedded software, and obtain function information and branch information in the function by extracting feature information in the target code and source code. According to the function information and branch information in the function, the corresponding coverage is calculated. The statistical coverage of the present invention includes: statistical instruction coverage, instruction function coverage, instruction function branch coverage, source code line coverage, source code function coverage, and source code function branch coverage of embedded software.
[0040] When processing disassembled file information, the correspondence between line number information and instruction information is fixed, and basically satisfies the following conditions: Figure 6 The format includes instruction address 4, function start address 5, function name 6 and instruction information 7. Instruction address 4 is a hexadecimal number consisting of 4 or 8 digits and letters, and insufficient digits are supplemented with 0 or space. Function start address 5 is a hexadecimal number consisting of 8 digits and letters, and insufficient digits are supplemented with 0. The corresponding information of the file line number and the instruction address is extracted. When calculating, if an instruction of the corresponding line number of the disassembled file is executed, then the corresponding line of the source code is also executed. However, in the disassembled information, only the source code corresponding to the disassembled code that generates the instruction operation is considered to be a valid line. Therefore, in the present invention, only the line number of the source code corresponding to the disassembled code that generates the instruction operation is counted as a valid line to perform coverage calculation.
[0041] like Figure 2 As shown in the figure, in the simulation environment, running embedded software means parsing the compiled binary file of the embedded software into the corresponding assembly instructions and writing them into the simulation hardware environment, and then executing the instructions one by one to simulate the running effect of the embedded software in the real environment. During the running process, the address of each instruction executed can be recorded to obtain the execution information.
[0042] like Figure 2As shown, the recording of instruction execution information is completed by creating a recording device. Each time the processor executes an instruction, the address information is transmitted to the recording device, and the recording device stores the execution address information. In the recording device, a corresponding address table is created according to the received address information to record the execution status. After receiving the address information, the current address table is searched to see if there is the address. If not, an address space is created to map the address. If there is an address table for the address, a +1 operation is performed at the corresponding position of the address table. When execution information is needed, all address tables are searched, and all non-zero addresses are output to the file to achieve the effect of recording the instruction stream.
[0043] Furthermore, the present invention provides a method for calculating code execution coverage statistics, the method comprising:
[0044] like Figure 1 As shown, on the one hand, the method for counting code execution coverage includes a method for counting target code execution coverage, and the method may include the following steps:
[0045] Step 101, parse the function information of the target code to obtain the target code function instruction information, target code function branch instruction information, and target code instruction corresponding source code information. The target code instruction corresponding source code information includes the total number of function statements and the number of corresponding line numbers in the function.
[0046] Step 102, calculating the target code coverage rate according to the target code function instruction information, the target code function branch instruction information and the target code instruction corresponding source code information.
[0047] In step 101, the system disassembly tool uses the existing GNU compilation tool chain. The compilation tool chain already supports the functions of compiling, disassembling, and extracting some symbol information of the program. For example, the tool chain includes a tool called objdump.exe. It uses the -DS1 parameter and takes a program as input. The output is a string of symbol information, such as Figure 8 As shown in the figure, the command is "objdump.exe-SCl test_c6713.out>test.s", and an output file of test.s is obtained.
[0048] In step 101, refer to Fig. 9, parse the disassembly file to obtain all function instruction address information, this process includes extracting the function information of the target code from the disassembly file. For example, the system parses the file path, file name and corresponding code line number: such as the address of the function in the memory and the function name of the seventh line. The ninth line contains the source file path where the function is located, the corresponding file name, and the code line number corresponding to the instruction. The twelfth to fourteenth lines of the program are actually the assembly instructions running in the simulation platform. In the disassembly instruction, the program parses into an eight-digit number to get the instruction address, for example: the instruction address 10800004. The key information that the simulation platform can obtain statistically is the assembly instructions that have been run, and the execution of the source code can be deduced based on the assembly instructions.
[0049] In step 102, the required target code coverage is calculated by the following formula. The target code coverage includes the target code total statement coverage, the target code total branch coverage, the target code function statement coverage and / or the target code single function branch coverage, where:
[0050]
[0051]
[0052]
[0053]
[0054] On the other hand, Figure 1 As shown, the method for code execution coverage statistics also includes a method for source code execution coverage statistics, which includes the following steps:
[0055] Step 201, abstracting the source code into a syntax tree, and parsing the syntax tree to obtain source code function information.
[0056] Step 202, parse the source code function information to obtain source code function line number information and source code function branch line number information.
[0057] Step 203, calculating source code coverage based on source code information corresponding to target code instructions, source code function line number information, and source code function branch line number information.
[0058] In step 201, the branch information of the source code needs to be accurately obtained by analyzing the source code file. In the present invention, the clang tool is used to extract the abstract syntax tree of the source code file, and the branch information of the source code is obtained through the abstract syntax tree. After the abstract syntax tree is obtained, the line numbers of the code blocks such as for, if...else, while, switch...case...default in the syntax tree are extracted. The following three steps can be roughly included: first, the clang tool parses the source code branch statement (abstract syntax tree), then returns the branch information, and finally obtains the branch statement line number.
[0059] Specifically, parsing source code branch statements means using the clang tool to convert the source code into an abstract syntax tree. The abstract syntax tree abstracts the source code into a tree structure, which includes the corresponding identifiers of each source code keyword such as for, while, if, etc., extracts and parses the corresponding identifier to obtain the type of statement in the line, and counts the start and end line numbers of the statement block according to the corresponding type, and extracts the line numbers of the branches in all source codes, as well as the start and end line number information of the statement block. The line number is the line number of the code statement in the source code file, and the branch line number is the line number of the branch.
[0060] In step 202, the source code function information is parsed, and the source code function line number information and the source code function branch line number information are obtained therefrom, including: the line number of the code block in the syntax number is extracted after the abstract syntax tree is obtained. Whether the code is executed is determined according to the type of the code block, and the instruction execution line number and the instruction address corresponding to the instruction execution line number are recorded. The types of code blocks include for code blocks, if code blocks, else code blocks, while code blocks, switch code blocks, case code blocks, and default code blocks. If the type of the code block is a for code block or a while code block, if the starting line of the branch is executed, it is determined that the False branch of the branch is executed, and the instruction information related to the False branch is recorded. If the code in the branch block is executed, it is determined that the True branch of the branch is executed, and the instruction information related to the True branch is recorded.
[0061] Specifically, the jump instruction information needs to be obtained during the parsing process: the jump information needs to obtain three addresses, the address of the jump instruction to determine whether the jump instruction is executed, the jump address to determine whether to enter the True branch, and the next address of the jump instruction to determine whether to execute the False branch. The instruction format of each architecture is different, so the present invention counts the above three addresses of the jump information separately according to the branch type, thereby counting the corresponding number of executed branches.
[0062] In step 203, the required source code coverage is calculated by the following formula. Source code coverage includes source code total statement coverage, source code total branch coverage, source code function statement coverage and / or source code single function branch coverage, where:
[0063]
[0064]
[0065]
[0066]
[0067] refer to Figure 4 , the total number of statements in the target code function and the number of corresponding line numbers between the target code and the source code in the function are analyzed by the following means to calculate the source code coverage:
[0068] 1. Parse the target code file and convert it into a disassembled file. Add corresponding parameters during the conversion and store the parameters in the disassembled file. For example, the parameters may include the source code line number corresponding to the instruction. Specifically, Figure 3 As shown in the figure, all instructions, function names, and source code line numbers in the disassembled file have a fixed format, including the source code file name 1, the source code file line number 2, and the instruction information corresponding to the corresponding line number of the source code file 3. By using regular expressions to process the disassembled file, you can obtain the instruction information of the function, the correspondence between instructions and line numbers, the total number of instructions, etc. Several instructions correspond to one line number. If one of these instructions is executed, it means that the source code of the line number is executed.
[0069] 2. According to the execution instruction information obtained in the simulation and the results of parsing the target code file, the corresponding coverage is calculated to obtain the final result.
[0070] In the present invention, coverage terms are explained as follows:
[0071] The total number of statements is the number of line numbers of all corresponding relations in the source code file in the disassembly file. The number of executed statements is the number of line numbers corresponding to the executed instructions. The total number of function statements is the number of line numbers of all corresponding relations in the function in the source code file in the disassembly file. The number of executed statements in the function is the number of line numbers corresponding to the executed instructions in the function. The total number of function statement branches is the total number of branches in the statements in the function. The number of executed branches in the function statement is the number of branches executed in the statements in the function. The total number of instructions is the total number of instructions in the disassembly file. The number of executed instructions is the number of instructions executed in the disassembly file. The total number of function instructions is the total number of instructions in the function in the disassembly file. The number of executed instructions in the function is the number of instructions executed in the disassembly file. The total number of function instruction branches is the total number of branch instructions in the function in the disassembly file. The number of executed branches in the function instruction is the number of branch instructions executed in the disassembly file.
[0072] In step 101, parsing the function information of the target code specifically includes:
[0073] The disassembled file information is processed as follows to extract the corresponding information between the instruction line number and the instruction address of the disassembled file:
[0074] 1. Count the number of executed branches: parse the disassembly file to obtain all function instruction address information and jump instruction information. Specifically, obtaining jump information includes: obtaining the address of the jump instruction to determine whether the jump instruction is executed. Obtaining the jump address to determine whether to enter the True branch. Obtaining the next address of the jump instruction to determine whether to execute the False branch.
[0075] 2. Count the number of executed statements: It should be pointed out that while parsing the function information of the target code to calculate the target code coverage, the source code coverage is also calculated based on the information. Specifically, there is a correspondence between the target code instructions and the source code - in the disassembly of the target code, there is a correspondence between several instructions and a line of source code. If one of these several instructions is executed, it means that the corresponding line of source code has been executed. Therefore, the present invention calculates the target code coverage and the source code coverage from the execution of the target code.
[0076] like Figure 7 As shown in the figure, an example of a disassembly file of a target code is given, in which the 44th line of code in main.c corresponds to the instruction at address 3f822f. When calculating coverage, the target code coverage is calculated by the executed address: for example, the address of the main function is from 3f822f to 3f823d. If the executed address is within this range, it means that the function has been executed. The instruction coverage can be calculated by using the number of executed addresses within the range and the total number of addresses within the range. In addition, by Figure 7The disassembly file can also extract the line number range of the main function in the source code file, which is 44 to 57. From this, we can further get the line number of the instruction operation, which is also the total number of statements in the main function. In this example, 3f822f corresponds to line 44, and 3f8230, 3f8231, and 3f8232 correspond to line 45. By extracting the instruction line number and the corresponding address, we can know which lines are executed according to the execution status of the instruction, and store the above instructions in the memory.
[0077] After the instruction execution is counted by the above means, the instruction line number in the source code corresponding to the instruction line number can be obtained according to the instruction line number of the disassembled file. And the instruction address of the instruction execution line number of the source code is obtained according to the instruction address dynamically executed according to the instruction line number in the source code. After that, when calculating the coverage rate, the corresponding coverage rate can be obtained according to the corresponding formula of the coverage rate obtaining step of the present invention. For example, in this example, all address line numbers can be counted to obtain the number of executed instruction statements in the functions of the target code and the source code. After the number of executed instruction statements in the function of the source code is divided by the total number of instruction statements of the corresponding function in the source code to obtain the percentage, the corresponding function statement coverage rate in the source code can be obtained. After the number of executed instruction statements in the function of the target code is divided by the total number of instruction statements of the corresponding function in the target code to obtain the percentage, the corresponding function statement coverage rate in the target code can be obtained.
[0078] refer to Fig.10 In the disassembly file, the instruction information is in the form of an instruction, similar to the following format: address, instruction word, instruction operation information. Branch instruction information is the execution information of the instruction operation as a branch instruction. Instructions correspond to source code. When disassembling, specific parameters are used to output source code line number information in the disassembly file. Several instructions correspond to one line number. As long as one of the instructions is executed, it means that the line of code is executed.
[0079] refer to Figure 5 , the code execution coverage statistics method also includes:
[0080] Step 301: According to the execution instruction information obtained in the simulation, specifically: cooperate with the simulation verification tool to obtain the execution PC address information, and analyze the execution coverage of the embedded software. Cooperating with the simulation verification tool to obtain the execution PC address information refers to obtaining the PC (Program Counter) address information of the code execution. The simulation verification tool does not count the PCs and instructions that have not been executed. Therefore, the coverage of the source code and the target code only analyzes the execution instruction coverage. As described in step 203 above, the present invention first uses a simulation means such as a simulation tool to extract the relevant parameters of the coverage from the target file and the source file and outputs them to the coverage statistics calculation module, and then calculates the coverage by the above-mentioned target code execution coverage statistics method and source code execution coverage statistics method.
[0081] Specifically, the simulation verification tool is a simulation platform. The simulation platform contains a virtual CPU module, and the CPU can parse the instructions of the program and execute them. The instructions are stored in the PC address according to the preset rules. Each instruction must have a PC address. The virtual CPU records the PC address that the program passes through during actual operation and compares it with the PC of the disassembly generated file to obtain a final result.
[0082] The PC information of dynamic execution is obtained, which includes the address information of the executed instruction. For example, suppose the target code is disassembled and has 10 address instruction information. These 10 instructions correspond to 10 lines of code in the source code. The instructions of 6 addresses are dynamically executed, and the source code corresponding to the 6 addresses also executes 6 lines of code. Then the target code instruction coverage is 60% and the statement coverage is 60%. In this process, the PC information of dynamic execution provides parameters for the calculation of coverage, such as the number of executed instructions.
[0083] In summary, the method for code execution coverage statistics provided by the present invention realizes a method and device for coverage statistics without inserting code, which can effectively avoid the problems of software vulnerabilities caused by redundant inserted code in the software production process, the increase in the storage volume of the generated target code file resulting in the inability to download to the target board, and frequent source code modifications caused by inserting code, thereby improving the stability and reliability of software products and reducing the labor cost of producing software products.
[0084] Specifically, the present invention can first solve a series of problems such as uncontrollable program operation or even operation errors caused by the use of plug-in codes to perform coverage statistics during coverage statistics. The plug-in code is a flag in the prior art that has nothing to do with code execution and is only used to calculate coverage. Secondly, since the simulation memory is configurable and the present invention does not need to add plug-ins to the source code to calculate the coverage, the target code volume is compressed, so that the target code is not limited by the small hardware memory. Therefore, the present invention can solve the problem of being unable to download to the target board after plug-in from the root. More importantly, the statistics of the real program running process based on instruction stream records are more convincing than the results obtained by the plug-in software algorithm: first, the traditional calculation method only calculates the coverage through the source code, which also includes the problems of source code redundancy and plug-in errors. Second, the source code coverage calculated based on the disassembly statistical data is more accurate.
[0085] The present invention has the following advantages over the prior art:
[0086] (1) Implemented zero-insertion technology for target code and source code coverage statistics
[0087] Zero-insertion statistical coverage requires analysis of the basic characteristics and execution characteristics of embedded software. Without inserting stubs, use the disassembly tool to generate the disassembly file of the embedded software target code, and obtain the desired basic information by analyzing the disassembly file, such as function instruction information, address corresponding source code line number information, etc. Then cooperate with the simulation verification tool to obtain the execution PC address information, analyze the execution coverage of the embedded software, and achieve the purpose of zero-insertion analysis coverage.
[0088] (2) The embedded software target code analysis technology that supports multiple architectures has been implemented. The target code file is used to simultaneously count the source code and target code coverage. It is not only highly stable, reliable, and accurate, but can also be quickly ported to different architectures and has a wide range of applications.
[0089] Corresponding embedded software target code analysis technologies are built for different architectures, such as ARM, SPARC, DSP, PowerPC, etc., which can accurately analyze the function names in the disassembly files of embedded software target codes, all the instruction address information of the functions, the information of branch instructions in the functions, the PC address of the branch instructions, the jump address of the branch instructions, the next address of the branch instructions, the line number information of the source code corresponding to the PC instruction address, etc. Through these basic information, statistical coverage information can be achieved in conjunction with execution information.
[0090] (3) The storage space of data caching technology is optimized and the size of cache files updated in real time is compressed.
[0091] Disassembly and data analysis are both time-consuming stages. For large embedded software, there may be hundreds of thousands or even millions of instructions. Each analysis will take a certain amount of time. In order to reduce the waste of time, data caching technology is proposed. When the target code to be executed remains unchanged, all fixed analysis data is stored as much as possible, such as the disassembly file of the target code, the analysis results of the disassembly file, the analysis results of the source code file, etc. All can be stored, but after the target code file is modified, all cache files need to be updated accordingly.
[0092] Embodiment 2
[0093] See also Figure 3 , which shows a structural diagram of a device for counting code execution coverage, the device comprising:
[0094] The target code coverage rate statistics module is used to parse the function information of the target code, and obtain the target code function instruction information, the target code function branch instruction information, and the target code instruction corresponding source code information. And it is used to calculate the target code coverage rate according to the target code function instruction information, the target code function branch instruction information and the target code instruction corresponding source code information. Specifically, the target code coverage rate statistics module can calculate the target code coverage rate according to the target code execution coverage rate statistics method of the present invention, which will not be repeated here.
[0095] The source code coverage rate statistics module is used to abstract the source code into a syntax tree, parse the syntax tree to obtain source code function information. The source code function information is parsed to obtain source code function line number information and source code function branch line number information. And the source code coverage rate is calculated according to the source code information corresponding to the target code instruction, the source code function line number information and the source code function branch line number information. Specifically, the source code coverage rate statistics module can calculate the source code coverage rate according to the above-mentioned source code execution coverage rate statistics method of the present invention, which will not be repeated here.
[0096] In summary, the device for code execution coverage statistics provided by the present invention realizes a method and device for coverage statistics without inserting code, which can effectively avoid the problems of software vulnerabilities caused by redundant inserted code in the software production process, the increase in the storage volume of the generated target code file resulting in the inability to download to the target board, and frequent source code modifications caused by inserting code, thereby improving the stability and reliability of software products and reducing the labor cost of producing software products.
[0097] Specifically, the present invention can first solve a series of problems such as uncontrollable program operation or even operation errors caused by the use of plug-in codes to perform coverage statistics during coverage statistics. The plug-in code is a flag in the prior art that has nothing to do with code execution and is only used to calculate coverage. Secondly, since the simulation memory is configurable and the present invention does not need to add plug-ins to the source code to calculate the coverage, the target code volume is compressed, so that the target code is not limited by the small hardware memory. Therefore, the present invention can solve the problem of being unable to download to the target board after plug-in from the root. More importantly, the statistics of the real program running process based on instruction stream records are more convincing than the results obtained by the plug-in software algorithm: first, the traditional calculation method only calculates the coverage through the source code, which also includes the problems of source code redundancy and plug-in errors. Second, the source code coverage calculated based on the disassembly statistical data is more accurate.
[0098] The present invention has the following advantages over the prior art:
[0099] (1) Implemented zero-insertion technology for target code and source code coverage statistics
[0100] Zero-insertion statistical coverage requires analysis of the basic characteristics and execution characteristics of embedded software. Without inserting stubs, use the disassembly tool to generate the disassembly file of the embedded software target code, and obtain the desired basic information by analyzing the disassembly file, such as function instruction information, address corresponding source code line number information, etc. Then cooperate with the simulation verification tool to obtain the execution PC address information, analyze the execution coverage of the embedded software, and achieve the purpose of zero-insertion analysis coverage.
[0101] (2) The embedded software target code analysis technology that supports multiple architectures has been implemented. The target code file is used to simultaneously count the source code and target code coverage. It is not only highly stable, reliable, and accurate, but can also be quickly ported to different architectures and has a wide range of applications.
[0102] Corresponding embedded software target code analysis technologies are built for different architectures, such as ARM, SPARC, DSP, PowerPC, etc., which can accurately analyze the function names in the disassembly files of embedded software target codes, all the instruction address information of the functions, the information of branch instructions in the functions, the PC address of the branch instructions, the jump address of the branch instructions, the next address of the branch instructions, the line number information of the source code corresponding to the PC instruction address, etc. Through these basic information, statistical coverage information can be achieved in conjunction with execution information.
[0103] (3) The storage space of data caching technology is optimized and the size of cache files updated in real time is compressed.
[0104] Disassembly and data analysis are both time-consuming stages. For large embedded software, there may be hundreds of thousands or even millions of instructions. Each analysis will take a certain amount of time. In order to reduce the waste of time, data caching technology is proposed. When the target code to be executed remains unchanged, all fixed analysis data is stored as much as possible, such as the disassembly file of the target code, the analysis results of the disassembly file, the analysis results of the source code file, etc. All can be stored, but after the target code file is modified, all cache files need to be updated accordingly.
[0105] Embodiment 3
[0106] The present invention provides a computing device for code execution coverage statistics. The computing device includes: one or more processors; a memory; and one or more programs, wherein the one or more programs are stored in the memory and configured to be executed by the one or more processors, and the one or more programs include instructions for executing the code execution coverage statistics method according to the present invention.
[0107] Furthermore, the present invention provides a computer-readable storage medium storing one or more programs, wherein the one or more programs include instructions, which, when executed by a computing device, enable the computing device to perform the method in the code execution coverage statistics method according to the present invention.
[0108] In summary, the computing device and storage medium for code execution coverage statistics of the present invention realize a method and device for coverage statistics without inserting code, which can effectively avoid the problems of software vulnerabilities caused by redundant inserted code in the software production process, the increase in the storage volume of the generated target code file resulting in the inability to download to the target board, and frequent source code modifications caused by inserting code, thereby improving the stability and reliability of software products and reducing the labor cost of producing software products.
[0109] Specifically, the present invention can first solve a series of problems such as uncontrollable program operation or even operation errors caused by the use of plug-in codes to perform coverage statistics during coverage statistics. The plug-in code is a flag in the prior art that has nothing to do with code execution and is only used to calculate coverage. Secondly, since the simulation memory is configurable and the present invention does not need to add plug-ins to the source code to calculate the coverage, the target code volume is compressed, so that the target code is not limited by the small hardware memory. Therefore, the present invention can solve the problem of being unable to download to the target board after plug-in from the root. More importantly, the statistics of the real program running process based on instruction stream records are more convincing than the results obtained by the plug-in software algorithm: first, the traditional calculation method only calculates the coverage through the source code, which also includes the problems of source code redundancy and plug-in errors. Second, the source code coverage calculated based on the disassembly statistical data is more accurate.
[0110] The present invention has the following advantages over the prior art:
[0111] (1) Implemented zero-insertion technology for target code and source code coverage statistics
[0112] Zero-insertion statistical coverage requires analysis of the basic characteristics and execution characteristics of embedded software. Without inserting stubs, use the disassembly tool to generate the disassembly file of the embedded software target code, and obtain the desired basic information by analyzing the disassembly file, such as function instruction information, address corresponding source code line number information, etc. Then cooperate with the simulation verification tool to obtain the execution PC address information, analyze the execution coverage of the embedded software, and achieve the purpose of zero-insertion analysis coverage.
[0113] (2) The embedded software target code analysis technology that supports multiple architectures has been implemented. The target code file is used to simultaneously count the source code and target code coverage. It is not only highly stable, reliable, and accurate, but can also be quickly ported to different architectures and has a wide range of applications.
[0114] Corresponding embedded software target code analysis technologies are built for different architectures, such as ARM, SPARC, DSP, PowerPC, etc., which can accurately analyze the function names in the disassembly files of embedded software target codes, all the instruction address information of the functions, the information of branch instructions in the functions, the PC address of the branch instructions, the jump address of the branch instructions, the next address of the branch instructions, the line number information of the source code corresponding to the PC instruction address, etc. Through these basic information, statistical coverage information can be achieved in conjunction with execution information.
[0115] (3) The storage space of data caching technology is optimized and the size of cache files updated in real time is compressed.
[0116] Disassembly and data analysis are both time-consuming stages. For large embedded software, there may be hundreds of thousands or even millions of instructions. Each analysis will take a certain amount of time. In order to reduce the waste of time, data caching technology is proposed. When the target code to be executed remains unchanged, all fixed analysis data is stored as much as possible, such as the disassembly file of the target code, the analysis results of the disassembly file, the analysis results of the source code file, etc. All can be stored, but after the target code file is modified, all cache files need to be updated accordingly.
[0117] The above describes the implementation methods of the method, device and computing equipment for code execution coverage statistics of the present invention, and its purpose is to explain the spirit of the present invention. Please note that those skilled in the art can modify and combine the features of the above-mentioned implementation methods without departing from the spirit of the present invention. Therefore, the present invention is not limited to the above-mentioned implementation methods. The specific features of the device and computing equipment for code execution coverage statistics of the present invention, such as shape, size and position, can be specifically designed based on the effects of the features disclosed above, and these designs can be implemented by those skilled in the art. Moreover, the technical features disclosed above are not limited to the combination with other features that have been disclosed. Those skilled in the art can also make other combinations between the technical features according to the purpose of the invention, so as to achieve the purpose of the present invention.
Claims
1. A method for code execution coverage statistics, characterized in that: The method comprises: Parsing the function information of the target code to obtain the target code function instruction information, the target code function branch instruction information, and the target code instruction corresponding source code information; wherein the target code instruction corresponding source code information includes the total number of function statements and the number of line numbers with corresponding relationships in the function; Calculating target code coverage based on the target code function instruction information, target code function branch instruction information and target code instruction corresponding source code information; Abstract the source code into a syntax tree, and parse the syntax tree to obtain source code function information; Parsing the source code function information to obtain source code function line number information and source code function branch line number information; and Calculate the source code coverage rate according to the source code information corresponding to the target code instruction, the source code function line number information and the source code function branch line number information, The function information of parsing the target code specifically includes: Process the disassembled file information, extract the corresponding information between the instruction line number and the instruction address of the disassembled file; obtain the instruction line number in the source code corresponding to the instruction line number according to the instruction line number of the disassembled file; and obtain the instruction address of the instruction execution line number of the source code according to the instruction address dynamically executed by the instruction line number in the source code, The processing of the disassembled file information and extracting the corresponding information between the line number and the instruction address of the disassembled file includes: Parse the disassembly file to obtain all function instruction address information and jump instruction information, Obtaining jump information includes: Get the address of the jump instruction to determine whether the jump instruction is executed; Get the jump address to determine whether to enter the True branch; and Get the next address of the jump instruction to determine whether to execute the False branch; The parsing of the source code function information to obtain source code function line number information and source code function branch line number information includes: After obtaining the abstract syntax tree, extract the line number of the code block in the syntax tree; determine whether the code is executed according to the type of the code block, and record the instruction execution line number and the instruction address corresponding to the instruction execution line number; wherein, The types of code blocks include for code block, if code block, else code block, while code block, switch code block, case code block, and default code block; and If the code block type is a for code block or a while code block, if the branch start line is executed, it is determined that the False branch of the branch is executed, and the instruction information related to the False branch is recorded; if the code in the branch block is executed, it is determined that the True branch of the branch is executed, and the instruction information related to the True branch is recorded; and The required target code coverage is calculated by the following formula; wherein the target code coverage includes the target code total statement coverage, the target code total branch coverage, the target code function statement coverage and / or the target code single function branch coverage; wherein, 2. The method for calculating code execution coverage statistics according to claim 1, characterized in that: The required source code coverage is calculated by the following formula; wherein the source code coverage includes the total source code statement coverage, the total source code branch coverage, the source code function statement coverage and / or the source code single function branch coverage; wherein, 3. A device for code execution coverage statistics, characterized in that: The device comprises: A target code coverage statistics module is used to parse the function information of the target code, and obtain the target code function instruction information, the target code function branch instruction information, and the target code instruction corresponding source code information; and to calculate the target code coverage according to the target code function instruction information, the target code function branch instruction information, and the target code instruction corresponding source code information; wherein the target code instruction corresponding source code information includes the total number of function statements and the number of line numbers with corresponding relationships in the function; The source code coverage statistics module is used to abstract the source code into a syntax tree, parse the syntax tree to obtain source code function information; parse the source code function information to obtain source code function line number information and source code function branch line number information; and calculate the source code coverage according to the source code information corresponding to the target code instruction, the source code function line number information and the source code function branch line number information. The function information of parsing the target code specifically includes: Process the disassembled file information, extract the corresponding information between the instruction line number and the instruction address of the disassembled file; obtain the instruction line number in the source code corresponding to the instruction line number according to the instruction line number of the disassembled file; and obtain the instruction address of the instruction execution line number of the source code according to the instruction address dynamically executed by the instruction line number in the source code, The processing of the disassembled file information and extracting the corresponding information between the line number and the instruction address of the disassembled file includes: Parse the disassembly file to obtain all function instruction address information and jump instruction information, Obtaining jump information includes: Get the address of the jump instruction to determine whether the jump instruction is executed; Get the jump address to determine whether to enter the True branch; and Get the next address of the jump instruction to determine whether to execute the False branch; The parsing of the source code function information to obtain source code function line number information and source code function branch line number information includes: After obtaining the abstract syntax tree, extract the line number of the code block in the syntax tree; determine whether the code is executed according to the type of the code block, and record the instruction execution line number and the instruction address corresponding to the instruction execution line number; wherein, The types of code blocks include for code block, if code block, else code block, while code block, switch code block, case code block, and default code block; and If the code block type is a for code block or a while code block, if the branch start line is executed, it is determined that the False branch of the branch is executed, and the instruction information related to the False branch is recorded; if the code in the branch block is executed, it is determined that the True branch of the branch is executed, and the instruction information related to the True branch is recorded; and The required target code coverage is calculated by the following formula; wherein the target code coverage includes the target code total statement coverage, the target code total branch coverage, the target code function statement coverage and / or the target code single function branch coverage; wherein, 4. A computing device comprising: one or more processors; Memory; as well as One or more devices comprising instructions for executing the method according to claim 1 or 2.
5. A computer-readable storage medium storing one or more programs, the one or more programs comprising instructions which, when executed by a computing device, cause the computing device to perform the method according to claim 1 or 2.
Citation Information
Patent Citations
System and method for realizing universal type target code coverage rate testing based on JTAG (Joint Test Action Group) debug mode
CN104657263A
Non-intrusive coverage rate statistical method for multiple processor architectures
CN111813672A