Detailed Description
In order to make the objects, technical solutions and advantages of the present invention more apparent, the present invention will be described in further detail below with reference to specific embodiments and accompanying drawings.
An embodiment of the present invention provides a method for locating abnormal memory access, which is used to effectively locate abnormal memory access, and referring to fig. 1, the method includes the following steps:
s101, monitoring a memory with abnormal access, and capturing access behaviors;
the memory with abnormal access can be monitored by configuring relevant register sets (such as a WATCH register set or a JTAG register set), and when instructions access the monitored memory, the access behaviors of the instructions are captured.
S102, obtaining an access instruction according to the access behavior, and positioning the access behavior through the access instruction;
when the memory is monitored by configuring the WATCH register group, the access instruction can be obtained by inquiring the EPC register; or,
when monitoring memory by configuring the JTAG register set, the access instruction may be obtained by querying the DEPC register.
S103, analyzing the access instruction to obtain an analysis result;
analyzing characteristics of the access instruction that may affect the register and/or the memory when the access instruction is executed may analyze at least one of the following aspects:
determining the type of the access instruction;
analyzing the bit width operated by the access instruction;
analyzing whether the offset of the register is used;
check if the access instruction is in a delay slot, etc.
And S104, replacing the access instruction to complete corresponding actions according to the analyzed result.
Specifically, the register and/or the memory related to completing the action of the access instruction is modified according to the analyzed result, so that the corresponding action can be completed instead of the access instruction, and the program can be continuously executed.
The embodiment of the invention can also comprise:
analyzing and acquiring the access times of the access instruction, checking the access instruction according to the order of the access times from few to many, and positioning the abnormal access of the memory more quickly; because the probability of an abnormal access occurring is low, it is usually normal access behavior with multiple accesses.
According to the embodiment of the invention, the access instruction is continuously checked and analyzed, so that abnormal access of the memory can be effectively and timely positioned; and the corresponding action is completed instead of the access instruction, so that the condition that the system needs to be reset is avoided, the program can be continuously executed, and the abnormal access of the memory can be continuously positioned. Meanwhile, the access instructions are checked from the order of few access times to many access times, and abnormal access of the memory can be positioned more quickly.
Referring to fig. 2, a flow chart of another embodiment of the present invention includes the following steps:
s201, monitoring the memory with abnormal access, and capturing access behaviors;
the memory area with abnormal access is determined in advance, and in order to locate the memory abnormal access, the memory with abnormal access needs to be monitored to capture the access behavior, namely which instructions access the memory.
Monitoring the memory with abnormal access requires configuring the relevant register set in order to capture the access behavior when the memory access occurs, where the access behavior may include reading and writing the memory or some execution action such as instruction fetching. The related register set may be a WATCH register set or a JTAG register set, for example: the WATCHLO and WATCHHI registers of the WATCH register set may be set and then monitored. When the specified access policy is satisfied, the CPU sets the ExcCode field in the Cause Register to 23, which indicates that the specified access behavior occurs, and jumps to the exception handling code section, thereby capturing the access behavior. The exception handling code segment is a code segment defined and added in advance, and the specific implementation of the exception handling code segment can be different according to the needs of a program, for example, some processing can be performed on memory access exceptions or Cache exceptions.
S202, obtaining an access instruction to obtain an instruction address;
an access instruction refers to an instruction that accesses the memory being monitored. When capturing access behaviors in step S101, it is necessary to acquire addresses of these access instructions that generate the access behaviors. The address to fetch the access instruction may be obtained by querying the associated register, such as by querying an EPC register when capturing access behavior using a WATCH register set. The EPC register is a 64-bit register that holds the address at which the program continues to execute after an exception, and in the event of an access action, the value in the EPC register is the address of the instruction that caused the access action. By capturing access behavior if the JTAG register set is used, the address of the instruction may be obtained by querying the DEPC register.
S203, acquiring the access times of the access instruction;
in the captured access behavior, some accesses are abnormal accesses causing abnormal accesses to the memory; in most cases, normal access is often performed, and the same access instruction may be repeatedly accessed for multiple times, and in this case, the access times of the repeated access instructions can be recorded, which has the advantages that: when the instructions are analyzed subsequently, the analysis can be started from the instructions with less access times, because the access times are less, the probability that abnormal access causes abnormal access of the memory is high, and the instructions are more favorably and quickly positioned.
It should be noted that this step is not a necessary step in the embodiments of the present invention, but is an preferable scheme, and by adding the acquisition of the instruction access times, the instruction causing the memory exception access can be located more quickly.
S204, positioning a function causing abnormal memory access according to the instruction address;
after the address of the access instruction is obtained in step S202, the function where the access instruction is located can be found by looking up the binary file of the program, and if the function is found to be originally not to access the monitored memory, the function is a function that causes abnormal access to the memory.
For example, in the memory assignment function memset (), the first argument is a pointer to the memory first address to be assigned. Assuming that the address of the access instruction is obtained in step S202, and the access behavior is found by looking up the program binary file, which is caused by the memset () function, it can be analyzed whether the first argument of the memset () is error, and if there is no error, it indicates that this is a normal access; on the contrary, if the parameter is found to have a problem, for example, the pointer value has been changed, or becomes a wild pointer with an uncertain value, the access is an abnormal access, that is, the function causing the memory abnormal access is located because the first parameter setting error of the memset () function causes the memory abnormal access, and then the occurrence of the memory abnormal access phenomenon can be avoided by modifying the corresponding parameter.
S205, analyzing the access instruction to obtain an analysis result;
in step S204, the instruction causing the memory access exception is located, but the program is in the exception handling (see step S201, the program jumps to the exception handling when the program has the set access behavior to the monitored memory), and in order to continue the execution of the program, the access instruction needs to be analyzed to obtain an analysis result.
The analysis of the access instruction specifically includes: modifying the register and/or the memory related to completing the action of the access instruction so as to complete the corresponding action instead of the access instruction, and then needing to complete the corresponding action by replacing the access instruction so as to enable the program to continue to execute completely.
Analyzing the access instructions may include at least one of:
1) determining the type of the access instruction;
determining the type of instruction being accessed may be aware of the operation of the instruction on memory or registers. If the instruction type is a LOAD type instruction (LOAD instruction), it can be known that the instruction LOADs the value of the memory into the register; or the instruction type is a STORE type instruction, it can be known that the instruction loads the register value into the memory.
2) Analyzing the bit width operated by the access instruction;
in the MIPS instruction, some instructions can operate only one byte (8 bits), and some instructions can operate one Half Word (16 bits); there are also instructions that can operate on a Word (16 bits) or Double Word (64 bits) that require analysis of which case the access instruction belongs to.
3) Analyzing whether a register-based offset is used;
some instructions (e.g., LOAD type instructions or STORE type instructions) operate on a memory or a register using a register-based offset, such as an lw (LOAD word) instruction in LOAD type instructions, which is used to LOAD a word (signed) from the memory into the register, occupying 32 bits of storage space, wherein the 0 th bit to the 15 th bit are register-based offsets. The analysis of the remaining instructions may refer to the relevant MIPS definitions, which are not described herein.
4) Checking whether the access instruction is in a Delay Slot;
during the execution of a MIPS Pipeline (Pipeline), a conditional branch (conditional branch) usually cannot immediately determine whether to jump; at this point, for the pipeline to function properly, the MIPS pipeline first executes another instruction (or instructions) that is not dependent on the conditional branch, as if a slot is formed between the conditional branch instruction and its jump destination instruction for execution of the other instruction, this slot being referred to as a delay slot.
Checking whether the instruction is in the delay slot, specifically by querying a BD bit in a Cause Register; if the BD bit is 1, the instruction is in the delay slot; otherwise, the instruction is not located in the delay slot.
If the access instruction is in the delay slot, the last instruction is a conditional branch, i.e. there is a jump instruction. When the program returns from the exception processing, the next instruction of the access instruction is not executed, but the jump instruction is executed, but the program executes the instruction in the delay slot due to the existence of the delay slot, and then executes the jump instruction after the exception processing returns, so that the program can generate dead loop repeatedly. In order to prevent this, it is necessary to analyze which instruction is the next instruction of the jump instruction, and then to perform the purpose of jumping to the next instruction instead of the jump instruction.
If the access instruction is not located in the delay slot, there will be no such occurrence of a dead loop, and the program will then execute the next instruction of the access instruction, and therefore, only the access instruction needs to be analyzed.
S206, replacing the access instruction to complete corresponding actions according to the analysis result;
after analyzing the instruction function through step S205, the corresponding action may be performed instead of accessing the instruction according to the analysis result. An instruction may need to modify a register associated with the instruction if it is to perform a corresponding action, or may need to modify the contents of memory associated with the accessed instruction if it is to perform an operation on memory.
In this step, the register related to completing the action of the access instruction is modified, so that the corresponding action can be completed instead of the access instruction, and the program can continue to execute normally.
Taking the function to be completed instead of the LOAD type instruction (such as instructions LW, LL, LWR, etc.) as an example, because the LOAD type instruction needs to read data from the memory into the register, and the operation performed by the access instruction can be known through the analysis in step S105, the value of the corresponding memory can be read according to the analysis result in step S105, and the value of the corresponding register can be changed to replace the function to be completed by the LOAD type instruction. It should be noted that, in practice, some other registers need to be modified according to the function and purpose of the instruction, such as modifying an EPC register, so that the next instruction is executed after the present instruction is executed.
Corresponding actions are completed by replacing access instructions, so that the program can be continuously and normally executed, the condition that the system needs to be reset is avoided, and normal services are not influenced while abnormal access of the memory is positioned.
S207, continuing to monitor the set memory area;
the monitoring of the memory is continued, the access behavior is obtained, and if the access behavior occurs, the execution is continued from step S202.
According to the embodiment of the invention, the access instruction is continuously checked and analyzed, so that abnormal access of the memory can be effectively and timely positioned; and the corresponding action is completed instead of the access instruction, so that the condition that the system needs to be reset is avoided, the program can be continuously executed, and the abnormal access of the memory can be continuously positioned. Meanwhile, the access instructions are checked from the order of few access times to many access times, and abnormal access of the memory can be positioned more quickly.
Referring to fig. 3, an embodiment of the present invention further provides a memory exception access positioning apparatus 30, including:
a monitoring unit 301, configured to monitor a memory with abnormal access and capture an access behavior;
an obtaining unit 302, configured to obtain an access instruction according to the access behavior, and locate the access behavior through the access instruction;
an analyzing unit 303, configured to analyze the access instruction to obtain an analysis result;
and a replacing unit 304, configured to replace the access instruction to complete a corresponding action according to the analysis result.
The monitoring unit 301 may monitor the memory accessed abnormally by configuring a related register set (e.g., a WATCH register set or a JTAG register set), and when an instruction accesses the related memory, an access behavior occurs, and at this time, the system may change some bits in the related register (e.g., a case register) to capture the access behavior.
The obtaining unit 302 is further configured to:
when monitoring the memory by configuring a WATCH register group, querying an EPC register to obtain the access instruction; or,
when monitoring the memory by configuring the JTAG register set, the DEPC register is queried to obtain the access instruction.
When an access action occurs, the program jumps to the exception handling code, and the fetch unit 302 may fetch the address of the access instruction by querying an EPC register or a DEPC register in which the address of the access instruction that caused the access action is stored.
The memory abnormal access positioning device 30 further includes:
a counting unit 305, configured to obtain access times of the access instruction, and view the access instruction according to an order of the access times from a few to a large number, so that memory abnormal access can be located faster.
Because the monitored memory is accessed not only by abnormal accesses but also by normal accesses, and the normal accesses are often the most. In order to quickly locate the access instruction generating the abnormal access, the access times of each instruction can be counted by the counting unit 305, and the access times are checked according to the sequence from a few to a large number, so that the probability that the abnormal instruction is checked first is higher, and the abnormal access of the memory is favorably and quickly located.
The analyzing unit 303 is further configured to:
analyzing the characteristics of the access instruction that may affect the register and/or the memory when the access instruction is executed, and at least the following units may be further included:
a type analysis unit for determining a type of the access instruction; or,
a bit width analysis unit, configured to analyze a bit width operated by the access instruction; or,
an offset analyzing unit for analyzing whether a register-based offset is used; or
And the delay slot analysis unit is used for checking whether the access instruction is in the delay slot.
The replacement unit 304 is further configured to:
modifying registers and/or memory associated with performing the access instruction action so that the corresponding action can be performed in place of the access instruction.
In order to avoid the problem of system reset caused by the fact that the program processes the exception return, the access instruction causing the exception needs to be replaced to complete corresponding actions, so that the program can be continuously executed, and the access instruction is continuously checked in the execution process, so that the instruction causing the memory exception access is effectively positioned.
According to the embodiment of the invention, the access instruction is continuously checked and analyzed, so that abnormal access of the memory can be effectively and timely positioned; and the corresponding action is completed instead of the access instruction, so that the condition that the system needs to be reset is avoided, the program can be continuously executed, and the abnormal access of the memory can be continuously positioned. Meanwhile, the access instructions are checked from the order of few access times to many access times, and abnormal access of the memory can be positioned more quickly.
It will be understood by those skilled in the art that all or part of the processes of the methods of the embodiments described above can be implemented by a computer program, which can be stored in a computer-readable storage medium, and when executed, can include the processes of the embodiments of the methods described above. The storage medium may be a magnetic disk, an optical disk, a Read-Only Memory (ROM), a Random Access Memory (RAM), or the like.
The above-mentioned preferred embodiments, further illustrating the objects, technical solutions and advantages of the present invention, should be understood that the above-mentioned are only preferred embodiments of the present invention and should not be construed as limiting the present invention, and any modifications, equivalents, improvements and the like made within the spirit and principle of the present invention should be included in the protection scope of the present invention.