A method for verifying the consistency of CPU instruction simulation execution effect
By generating instruction flow test examples and performing differential testing on real devices and CPU simulators, the problem of inconsistent execution of simulators and real hardware is solved, the execution accuracy of simulators is improved, and it is suitable for repairs and improvements of multiple scenarios.
Patent Information
- Application Number
- CN202110756227.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-07-05
- Publication Date
- 2024-11-08
- Estimated Expiration
- 2041-07-05
AI Technical Summary
The CPU emulator may be inconsistent with the real hardware when simulating the execution of instructions, resulting in inaccurate execution results and lack of effective verification methods to discover and fix this situation.
By generating instruction flow test examples and performing differential tests on real devices and CPU simulators, comparing the execution results of the two, identifying and fixing inconsistent situations. The methods include parsing instruction binary encoding, generating test examples, initializing devices and simulators, and executing tests Program and compare results.
Improves the instruction execution accuracy of the CPU simulator, and can repair and improve the simulator without special hardware support. It is suitable for identifying the simulator environment and anti-fuzz testing scenarios.
Smart Images

Figure CN113485926B_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the field of information security technology, and in particular relates to a method for verifying the consistency of CPU instruction simulation execution effects. Background Art
[0002] Currently, CPU simulators are widely used in software development and program dynamic analysis. Although many hardware platforms also provide some hardware-based program tracing and debugging support for these needs, such as Intel PT, these hardware features often have some limitations. For example, ARM ETM only provides a limited size of trace buffer (ETB), and the ETB on the Juno development board is only 64KB in size. Compared with hardware-based debugging technology, CPU simulators provide fine-grained program tracing capabilities, complete system monitoring capabilities, and scalability to run on different operating systems and architectures. Therefore, researchers have built various program dynamic analysis platforms based on CPU simulators to support malware analysis, develop live patches, and analyze program crash causes.
[0003] The widespread use of software simulation technologies such as CPU simulators is based on the widely believed assumption that these simulators can accurately simulate the execution effect of each hardware instruction on real hardware. However, this assumption is not always true. The CPU simulator may fail or the implementation is inconsistent with the real hardware, resulting in the execution effect of the instruction being inconsistent with the real device. In order to avoid this situation as much as possible, a method for verifying the consistency of CPU instruction simulation execution is needed to identify such situations and fix them in time. Summary of the invention
[0004] In view of the shortcomings of the prior art, the present invention provides a method for verifying the consistency of CPU instruction simulation execution to help improve the accuracy of CPU simulator simulation instructions. The specific technical solution is as follows:
[0005] A method for verifying the consistency of a CPU instruction simulation execution effect, the method comprising the following steps:
[0006] S1: Generate an instruction stream test case for testing the consistency of instruction execution effect;
[0007] Parse the binary code of the instruction according to the instruction specification manual, identify the specific type of each symbol in the binary code of the instruction, and generate instruction flow test cases by generation or sampling;
[0008] S2: Differential testing of real devices and CPU simulators;
[0009] Initialize the real device and the CPU simulator, keep their initial states consistent, and then execute the instruction flow test case in the real device and the CPU simulator respectively to obtain the execution results of the two; by comparing the execution results of the two, determine whether the running results of the instruction flow test case in the CPU simulator are consistent with those of the real device.
[0010] Furthermore, the method of obtaining the instruction stream test case by generating method is as follows:
[0011] (1) According to the specific types of each symbol, generate a series of initial values for each type of symbol;
[0012] (2) Obtain the value constraints of each symbol according to the instruction specification manual, and obtain as many valid values as possible for each symbol through constraint solving;
[0013] (3) Calculate the union of the initial value set of each symbol of the instruction and the value set obtained by solving the constraint to obtain the valid value set of each symbol;
[0014] (4) Cartesian product calculation is performed on the valid value set of each symbol, and an instruction stream test case is generated.
[0015] Furthermore, S2 is implemented in the following manner:
[0016] S2.1: Add preamble code and postamble code to the instruction flow test case and generate a test program;
[0017] S2.2: Run the test program in the real device and the CPU simulator respectively; the test program performs the following steps:
[0018] (1) Register signal processing functions for each signal that may be triggered;
[0019] (2) Initialize register status;
[0020] (3) Run the instruction flow test case;
[0021] (4) Output current status information, including PC register value, register value operated by instruction, memory operated by instruction, status register value and triggered signal;
[0022] (5) Exit the program;
[0023] S2.3: By comparing the execution results of the two, determine whether the running result of the instruction stream test case in the CPU simulator is consistent with that of the real device; if and only if the following conditions are met, the differential test engine determines that the simulation of the instruction is inconsistent:
[0024] (a) No signal is captured on the real device or CPU simulator, but the register status and memory read and write conditions of the two are different;
[0025] (b) Different signals captured on real devices and CPU simulators;
[0026] (c) The same signal is captured on the real device and the CPU simulator, but the register states of the two are different.
[0027] The beneficial effects of the present invention are as follows:
[0028] The method of the present invention does not require special hardware support and can be used to repair and improve existing CPU simulators. It can also be applied to scenarios such as simulator environment identification, anti-simulator, and anti-fuzzy testing. BRIEF DESCRIPTION OF THE DRAWINGS
[0029] Figure 1 It is a flow chart of verifying the consistency of the execution effect of the instruction of the real device and the CPU simulator of the present invention;
[0030] Figure 2 It is a schematic diagram of the test case generator of the present invention generating a valid value set for each symbol in the instruction encoding;
[0031] Figure 3 It is a schematic diagram of a test program generated by the differential test engine of the present invention for an instruction stream. DETAILED DESCRIPTION
[0032] The present invention will be described in detail below based on the accompanying drawings and preferred embodiments, and the purpose and effects of the present invention will become more clear. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not used to limit the present invention.
[0033] like Figure 1 As shown, the present invention is a method for verifying the consistency of CPU instruction simulation execution, which includes two steps: generating an instruction stream test case for testing the consistency of instruction execution effect and performing a differential test on a real device and a CPU simulator.
[0034] 1. Generate instruction stream test case
[0035] Among them, generating instruction flow test cases is to parse the binary code of the instruction according to the instruction specification manual, identify the specific type of each symbol in the binary code of the instruction, including constant symbols and variable symbols representing registers, direct numbers and other types, and then generate instruction flow test cases by generation or sampling. The details are as follows:
[0036] 1. According to the specific types of each symbol, such as register, immediate value, etc., generate a series of initial values for each type of symbol. Figure 2 As shown, as one of the implementation modes, Figure 2The encoding of the VLD4 instruction in the AArch32 instruction set and the types of each symbol in the instruction according to the instruction specification manual are shown.
[0037] 2. Obtain the value constraints of each symbol according to the instruction specification manual, and obtain as many valid values as possible for each symbol through constraint solving.
[0038] In order to generate a representative instruction stream, the generated instruction stream test case should cover as many execution paths as possible. According to the instruction specification manual, the constraint relationship between each symbol in an instruction can be obtained. By using the SMT solver for solving, the test case generator generates more valid values for each encoding symbol.
[0039] 3. Calculate the union of the initial value set of each symbol of the instruction and the value set obtained by solving the constraints to obtain the valid value set of each symbol.
[0040] 4. Perform Cartesian product calculations on the valid value sets of each symbol and generate instruction stream test cases.
[0041] In addition to the above process, instruction flow test cases can also be generated by sampling qualified instruction flows in real programs as instruction flow test cases. For example, taking the STR type instruction flow as an example, all STR type instruction flows can be identified and extracted from the executable files of the actual software as instruction flow test cases.
[0042] 2. Differential Test
[0043] Initialize the real device and CPU simulator, keep the initial states of the two consistent, then execute the instruction flow test case in the real device and CPU simulator respectively, and obtain the execution results of the two; by comparing the execution results of the two, determine whether the running results of the instruction flow test case in the CPU simulator are consistent with those of the real device. The details are as follows:
[0044] 1. Add pre-code and post-code to the instruction flow test case to generate a test program.
[0045] In order to obtain the pre-code and post-code of the test program, a test program is first written for the null instruction and an executable file is generated. Then, the parts before and after the null instruction in the executable file are extracted respectively.
[0046] 2. Run the test program on the real device and CPU simulator respectively. Figure 3 As shown, during the running process of the test program, the following steps are performed:
[0047] (1) Register signal processing functions for each signal that may be triggered.
[0048] This is to prevent the instruction stream from triggering exceptions such as memory errors and illegal instructions, causing the program to end prematurely.
[0049] (2) Initialize the register state of the environment.
[0050] Specifically, all registers except special registers are assigned values 0. Taking the AArch32 architecture processor as an example, the special registers include R11 (FP), R13 (SP), R14 (LR) and R15 (PC).
[0051] (3) Run the instruction flow test case.
[0052] (4) Output current status information, including the PC register value, the register value of the instruction operation, the memory operated by the instruction, the status register value and the triggered signal.
[0053] Since the signal processing function is registered, even if the instruction stream triggers an exception, the test program can record the signal triggered by the instruction and the status after the instruction is executed. The test program exports the status of the register by putting the register value into the stack and outputting it to the file. By using Capstone to analyze the instruction stream test case, the test program can calculate the memory address that the instruction may read or write. The test program also puts the value in the memory into the stack and exports it to the file.
[0054] (5) Exit the program.
[0055] 3. By comparing the running results of the test program, determine whether the running results of the instruction flow test case in the CPU simulator are inconsistent with the real device. The differential test engine determines that the simulation of the instruction is inaccurate if and only if the following conditions are met:
[0056] (a) No signal is captured on the real device or CPU simulator, but the register status and memory read and write conditions of the two are different;
[0057] (b) Different signals captured on real devices and CPU simulators;
[0058] (c) The same signal is captured on the real device and the CPU simulator, but the register states of the two are different.
[0059] In addition to the above differential testing methods, other methods can also be used to allow real devices and simulators to execute instruction flow test cases and generate execution results that can be used to compare consistency. For example, let the real device and simulator run a special operating system that completes the steps of environment initialization, executing instruction flow, and exporting the final system state.
[0060] Those skilled in the art can understand that the above are only preferred examples of the invention and are not intended to limit the invention. Although the invention is described in detail with reference to the above examples, those skilled in the art can still modify the technical solutions recorded in the above examples or replace some of the technical features therein with equivalents. Any modification, equivalent replacement, etc. made within the spirit and principle of the invention shall be included in the protection scope of the invention.
Claims
1. A method for verifying the consistency of CPU instruction simulation execution effect, characterized in that: The method comprises the following steps: S1: Generate an instruction stream test case for testing the consistency of instruction execution effect; According to the instruction specification manual, the binary code of the instruction is parsed, the specific type of each symbol in the binary code of the instruction is identified, and the instruction flow test case is generated by generation or sampling; the method of obtaining the instruction flow test case by generation is as follows: (1) Generate a series of initial values for each type of symbol according to the specific type of each symbol; (2) Obtain the value constraints of each symbol according to the instruction specification manual, and obtain valid values for each symbol through constraint solving; (3) Calculate the union of the initial value set of each symbol of the instruction and the value set obtained by solving the constraint to obtain the valid value set of each symbol; (4) Perform Cartesian product calculations on the valid value sets of each symbol and generate instruction stream test cases; S2: Differential testing of real devices and CPU simulators; Initialize the real device and the CPU simulator, keep their initial states consistent, and then execute the instruction flow test case in the real device and the CPU simulator respectively to obtain the execution results of the two; by comparing the execution results of the two, determine whether the running results of the instruction flow test case in the CPU simulator are consistent with those of the real device.
2. The method for verifying the consistency of CPU instruction simulation execution effect according to claim 1, characterized in that: The S2 is implemented in the following way: S2.1: Add preamble code and postamble code to the instruction flow test case and generate a test program; S2.2: Run the test program in the real device and the CPU simulator respectively; the test program performs the following steps: (1) Register signal processing functions for each signal that may be triggered; (2) Initialize register status; (3) Run the instruction flow test case; (4) Output current status information, including PC register value, register value operated by instruction, memory operated by instruction, status register value and triggered signal; (5) Exit the program; S2.3: By comparing the execution results of the two, determine whether the running result of the instruction stream test case in the CPU simulator is consistent with that of the real device; if and only if the following conditions are met, the differential test engine determines that the simulation of the instruction is inconsistent: (a) No signal is captured on the real device or CPU simulator, but the register status and memory read and write conditions are different between the two; (b) Different signals captured on real devices and CPU simulators; (c) The same signal is captured on the real device and the CPU simulator, but the register states of the two are different.
Citation Information
Patent Citations
Symbolic-execution-based method for generating test cases with feedback
CN102736979A
Method for automatically generating character string type test case
CN105224455A
Instruction set simulator verification method based on source code instrumentation
CN106776281A
Technologies for application validation in persistent memory systems
CN107278297A
Simulation testing method and system for hydraulic support controller software
CN108170889A
