A method and device for detecting out-of-bounds memory access in a system

By jumping to the memory detection function to detect the memory access address in the embedded system, the problems of low real-time and high consumption of memory out-of-bounds detection in the embedded system are solved, and memory out-of-bounds detection with high real-time and low performance impacts are achieved.

CN114625646BActive Publication Date: 2025-06-24FIBERHOME TELECOMMUNICATION TECHNOLOGIES CO LTD
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202210246000.4
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-03-14
Publication Date
2025-06-24
Estimated Expiration
2042-03-14

AI Technical Summary

Technical Problem

In embedded systems, the problems of prior art for detecting memory out-of-bounds are limited by low real-time, high consumption and unsuitable for third-party detection tools, especially in the case of small memory capacity.

Method used

By jumping to the memory detection function before memory access, we detect whether the access address belongs to the interval address, thereby real-time detection of memory out-of-bounds. The method includes applying for memory, generating jump instruction code snippet templates, modifying the instruction set to call the memory detection function, and determining whether the memory access address belongs to the memory interval address.

Benefits of technology

Memory out-of-bounds detection with high real-time and low performance impact is achieved, and memory out-of-bounds abnormalities can be detected immediately, reducing positioning time, and is not limited by the programming language compiler version and processor framework.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114625646B_ABST
    Figure CN114625646B_ABST
Patent Text Reader

Abstract

The present invention discloses a method and apparatus for detecting out-of-bounds access of system memory. The method includes: applying for memory, searching for the address of a memory detection function, and simultaneously generating a jump instruction code segment template and recording the address of the memory interval; when detecting out-of-bounds access of system memory, if a memory access instruction in the program is encountered, jump to the jump instruction code segment template corresponding to the memory access instruction, and modify the instruction set in the jump instruction code segment generated by the jump instruction code segment template; the modified jump instruction code segment calls the memory detection function, and the memory detection function determines whether the memory address to be accessed by the memory access instruction belongs to the memory interval address. If so, it indicates out-of-bounds access of memory. The method for detecting out-of-bounds access of system memory in the present invention has good real-time performance. Once out-of-bounds access of memory occurs, an exception can be immediately detected, and an exception is directly thrown at the instruction where out-of-bounds access of memory occurs, reducing the positioning time.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the field of embedded technology, and more specifically, relates to a method and device for detecting out-of-bounds system memory. Background Art

[0002] Memory out-of-bounds has always been a common problem in the C language. There are two common location methods: one is to add guard characters to the memory block and check whether the guard characters are modified when the memory is released to determine out-of-bounds. However, this method has a delay, that is, it is not clear when the memory out-of-bounds occurs, nor is it clear what causes it. It can only be checked by reading the code, and the location speed is slow. The other is to set memory protection pages. Each time memory is allocated, at least three memory pages are allocated. The operating system limits that the memory read and write attributes can only be set in units of pages. Generally, at least one page is required for the memory usage block, one page at the tail needs to be set to non-readable and non-writable attributes, and one page for the memory management header needs to be set to readable attributes. In this way, at least 3 pages are required in total. The size of one page is about 4KB. Under 4GB of memory, at most 350,000 memory blocks can be allocated. The memory pages before and after are set to non-readable and non-writable states. Once the memory is out-of-bounds, a hardware error of the memory manager can be triggered. This method has high real-time performance, but consumes a large amount of memory. In an embedded system, the memory capacity is usually small, and large software cannot be started and run normally.

[0003] The third-party detection tool for memory out-of-bounds is the Address Sanitizer (ASAN for short). ASAN runs slowly, is not supported by all compiler versions and processor frameworks, and after official testing by the clang compiler, it is confirmed that the running speed drops by 2 times after adding ASAN. Using ASAN in an actual project will cause the running speed to decrease. Therefore, third-party detection tools are not applicable.

[0004] In view of this, overcoming the deficiencies of the existing technology products is an urgent problem to be solved in this technical field. Summary of the Invention

[0005] In view of the above defects or improvement requirements of the prior art, the present invention provides a method and device for detecting out-of-bounds system memory, aiming to solve the technical problem that third-party detection tools are not applicable for out-of-bounds memory in an embedded system with a small memory capacity by jumping to a memory detection function before memory access to detect whether the access address belongs to an interval address.

[0006] In a first aspect, the present invention provides a method for detecting out-of-bounds system memory, the method comprising:

[0007] Allocate memory, search for the address of the memory detection function, and at the same time generate a jump instruction code segment template and record the address of the memory interval;

[0008] When detecting that the system memory access exceeds the boundary, if a memory access instruction in the program is encountered, jump to the jump instruction code segment template corresponding to the memory access instruction, and modify the instruction set in the jump instruction code segment generated by the jump instruction code segment template;

[0009] The modified jump instruction code segment calls the memory detection function, and the memory detection function determines whether the memory address to be accessed by the memory access instruction belongs to the memory interval address. If it belongs, the memory access exceeds the boundary.

[0010] Preferably, the method of jumping to the jump instruction code segment template corresponding to the memory access instruction and modifying the instruction set in the jump instruction code segment generated by the jump instruction code segment template when a memory access instruction in the program is encountered includes:

[0011] When the memory access instruction is an immediate addressing instruction, use the immediate number in the memory access instruction as the source assignment and pass it to the parameter passing register corresponding to the processor framework, and modify the parameter passing instruction in the jump instruction code segment according to the memory access instruction.

[0012] Preferably, the method of jumping to the jump instruction code segment template corresponding to the memory access instruction and modifying the instruction set in the jump instruction code segment generated by the jump instruction code segment template when a memory access instruction in the program is encountered further includes:

[0013] When the memory access instruction is a register addressing instruction, obtain the register corresponding to the processor framework, and use the memory access address existing in the register as the first memory access address;

[0014] Use the first memory access address as the source assignment and pass it to the parameter passing register corresponding to the processor framework, and modify the parameter passing instruction in the jump instruction code segment according to the first memory access instruction.

[0015] Preferably, the method of jumping to the jump instruction code segment template corresponding to the memory access instruction and modifying the instruction set in the jump instruction code segment generated by the jump instruction code segment template when a memory access instruction in the program is encountered further includes:

[0016] When the memory access instruction is a base plus index addressing instruction, obtain the base register and index register corresponding to the processor framework;

[0017] Use the sum of the addresses of the base register and the index register as the second memory access address, and use the sum of the addresses of the base register and the index register as the source assignment and pass it to the parameter passing register corresponding to the processor framework;

[0018] Modify the parameter passing instruction in the jump instruction code segment according to the second memory access instruction.

[0019] Preferably, at the end of the jump instruction code segment are the memory access instruction and the jump instruction that jumps to the next instruction of the memory access instruction.

[0020] Preferably, the method further includes:

[0021] The jump instruction code segment saves the context of the memory access instruction. If the memory detection function determines that the address of the memory interval block does not belong to the memory interval address, the jump instruction code segment restores the context of the memory access instruction and continues to execute the memory access instruction.

[0022] Preferably, to store the jump instruction code segment, search for the address of the memory detection function, and at the same time generate a jump instruction code segment template and record the address of the memory interval, the specific method includes:

[0023] The jump instruction code segment allocates memory space on the stack and pushes the register value corresponding to the memory access instruction onto the stack;

[0024] Save the address and length corresponding to the memory access instruction in the parameter passing register corresponding to the memory access instruction;

[0025] The jump instruction segment template assembly instruction sequence.

[0026] Preferably, modify the instruction set in the jump instruction code segment generated by the jump instruction code segment template, where the instruction set specifically includes: the input parameter instruction, the memory access instruction, and the instruction for jumping to the address of the next instruction of the memory access instruction.

[0027] Preferably, the method further includes:

[0028] When stopping the detection of system memory out-of-bounds, the jump instruction of the jump instruction code segment is modified to the memory access instruction.

[0029] In a second aspect, the present invention further includes a device for detecting system memory out-of-bounds, the device includes:

[0030] At least one processor; and a memory communicatively connected to the at least one processor; wherein, the memory stores instructions executable by the at least one processor, and the instructions are programmed to execute the method for detecting system memory out-of-bounds described in the first aspect.

[0031] Generally speaking, compared with the prior art by the above technical solution conceived by the present invention, the following beneficial effects are obtained:

[0032] A method and device for detecting system memory out-of-bounds provided by the present invention have good real-time performance in detecting system memory out-of-bounds. Once memory out-of-bounds occurs, an exception can be immediately detected, and an exception can be directly thrown at the instruction where memory out-of-bounds occurs, reducing the positioning time. Moreover, it can be customized to be enabled or stopped, with little performance impact, and can be started together with the normal version without the need to recompile the version, and is not restricted by the programming language compiler version and the processor framework. BRIEF DESCRIPTION OF THE DRAWINGS

[0033] Figure 1 is a flowchart of the method for detecting system memory out-of-bounds in the first embodiment;

[0034] Figure 2 is a flowchart of the jump process in the method for detecting system memory out-of-bounds in the first embodiment;

[0035] Figure 3 is a flowchart of the jump code segment in the method for detecting system memory out-of-bounds in the first embodiment;

[0036] Figure 4 is a schematic diagram of the device for detecting system memory out-of-bounds in the second embodiment. DETAILED DESCRIPTION OF THE EMBODIMENTS

[0037] In order to make the objectives, technical solutions and advantages of the present invention clearer, the present invention will be further described in detail below with reference to the accompanying drawings and embodiments. 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. In addition, the technical features involved in the various embodiments of the present invention described below can be combined with each other as long as they do not conflict with each other.

[0038] In the description of the present invention, the orientation or positional relationship indicated by terms such as "inner", "outer", "longitudinal", "lateral", "upper", "lower", "top", "bottom", etc. is based on the orientation or positional relationship shown in the drawings, and is only for the convenience of describing the present invention rather than requiring the present invention to be constructed and operated in a specific orientation. Therefore, it should not be construed as a limitation of the present invention.

[0039] In the present invention, unless otherwise clearly defined and limited, the first feature being "above" or "below" the second feature may include the first and second features being in direct contact, or may include the first and second features not being in direct contact but being in contact through other features therebetween. Moreover, the first feature being "above", "over" and "on" the second feature includes the first feature being directly above and obliquely above the second feature, or merely indicating that the first feature has a higher horizontal height than the second feature. The first feature being "below", "beneath" and "under" the second feature includes the first feature being directly below and obliquely below the second feature, or merely indicating that the first feature has a lower horizontal height than the second feature.

[0040] Example 1:

[0041] This Example 1 provides a method for detecting out-of-bounds system memory, as Figure 1 shown, the method includes the following steps:

[0042] S101: Allocate memory, search for the address of the memory detection function, and at the same time generate a jump instruction code segment template and record the address of the memory interval.

[0043] When the program starts, allocate a memory area to store the jump code segment, search for the address of the memory detection function, and at the same time generate several jump instruction code segment templates. The jump instruction code segment templates corresponding to different processor architectures are also different, but the memory area used to store the jump code segment generally does not exceed 200 bytes.

[0044] Save the address and length of the memory to be accessed into the parameter register. The detection function is a function edited in C language. The main function of the detection function is to find whether the currently accessed memory address is within the recorded memory interval block address. The detection function edited in C language is finally compiled into a section of assembly instructions, which will be loaded into a memory address for storage when the program starts. This memory address is randomly allocated by the operating system, so it is necessary to search for the memory address of the detection function, and the subsequent instructions will jump to the memory address of the detection function to execute the memory detection function. Common methods of memory address management algorithms include linked lists, hash tables, bitmaps, etc., and there are also asan's shadow memory algorithms, etc. The function address can be found by traversing the symbols in the library and checking whether the symbol name is the function name to be queried. After calling the memory detection function, the memory detection function checks whether the currently accessed memory address belongs to the memory interval address. If it is the memory interval address, an exception is thrown and the program terminates abnormally. If it is not the memory interval address, the instruction continues to be executed.

[0045] The jump instruction code segment template is generated relying on assembly language and is an assembly instruction sequence used to complete the function.

[0046] S102: When detecting out-of-bounds system memory, if a memory access instruction in the program is encountered, jump to the jump instruction code segment template corresponding to the memory access instruction, and modify the instruction set in the jump instruction code segment generated by the jump instruction code segment template.

[0047] The jump instruction code segment template has a corresponding relationship with the type of memory access instruction in the program. The types of memory access instructions are divided into immediate addressing instructions, register addressing instructions, and base plus index addressing instructions. The jump instruction code segment templates also correspond to immediate addressing templates, register addressing templates, and base plus index addressing templates, except that the jump instruction code segment templates corresponding to different processor architectures are also different.

[0048] The slave function for detecting out-of-bounds memory in the system memory parses the function instructions in the program one by one starting from the function entry, and determines whether the function instructions in the program are memory access instructions. If they are not memory access instructions, continue to parse the next instruction or the instructions of the next function; if they are memory access instructions, jump to the jump instruction code segment template corresponding to the memory access instruction, modify the instruction set in the jump instruction code segment generated by the jump instruction code segment template, and save the modified jump instruction code segment in the memory applied for in S101.

[0049] S103: The modified jump instruction code segment calls the memory detection function, and the memory detection function determines whether the memory address to be accessed by the memory access instruction belongs to the memory interval address. If it belongs, the memory is out of bounds.

[0050] When the program runs to the original memory access instruction (which has been replaced with a jump instruction), it will jump to the modified jump instruction code segment. The jump instruction code segment saves the context of the current code and calls the memory detection function. The memory detection function finds out whether the memory address to be accessed by the original memory access instruction is an interval address according to the memory interval storage algorithm. If it is an interval address, an exception is thrown, it is determined that there is out-of-bounds memory, and the program stops running; if the memory address to be accessed by the original memory access instruction is not an interval address, the saved code context is restored, the original memory access instruction is executed, and the program jumps to the next instruction of the original memory access instruction to continue execution.

[0051] In the first embodiment of the present invention, in order to adapt to the processor framework where the program is located, so as to assign and transfer the immediate number in the immediate number addressing type instruction to the corresponding parameter passing register, in combination with the embodiments of the present invention, there is also a preferred implementation scheme. Specifically, as Figure 2 shown, the detection method includes:

[0052] Step S201: If a memory access instruction in the program is encountered, jump to the jump instruction code segment template corresponding to the memory access instruction, and modify the instruction set in the jump instruction code segment generated by the jump instruction code segment template.

[0053] Step S202: Determine the type of the memory access instruction.

[0054] If the memory access instruction is an immediate number addressing type instruction, perform step S203, use the immediate number in the memory access instruction as the source to assign and transfer it to the parameter passing register corresponding to the processor framework, and modify the parameter passing instruction in the jump instruction code segment according to the memory access instruction.

[0055] Immediate addressing directly accesses the data in the memory address. The immediate number is the memory access address. According to the memory access address, the parameter passing instruction in the jump instruction code segment is modified, and the immediate number is passed as the source assignment to the corresponding parameter passing register in the processor framework.

[0056] For example, the template for 32-bit powerpc is as follows:

[0057] addi r1,r1,-64 / / 0: Allocate byte space on the stack

[0058] stw r3,8(r1) / / 1: Push the r3 register onto the stack

[0059] mflr r3 / / 2:

[0060] stw r3,12(r1) / / 3: Push lr onto the stack

[0061] stw r4,16(r1) / / 4: Push the r4 register onto the stack

[0062] stw r5,20(r1) / / 5: Push the r5 register onto the stack

[0063] stw r0,24(r1) / / 6: Push r0 onto the stack

[0064] stw r6,28(r1) / / 7: Push the r6 register onto the stack

[0065] stw r7,32(r1) / / 8: Push r6 onto the stack

[0066] stw r8,36(r1) / / 9: Push r6 onto the stack

[0067] stw r9,40(r1) / / 10: Push r6 onto the stack

[0068] stw r10,44(r1) / / 11: Push r6 onto the stack

[0069] stw r11,48(r1) / / 12: Push r11 onto the stack

[0070] addi r3,r0,0 / / 13: Store rA in r3 for passing the base address, this instruction needs to be modified later

[0071] addi r4,r0,0 / / 14: Store rB in r4 for passing the index, this instruction needs to be modified later

[0072] li r5,0 / / 15: Store d in r5 for passing the offset, this instruction needs to be modified later

[0073] bl memcheckfunc / / 16: Call the memory detection function to generate the address of the previous query function of the module

[0074] lwz r3,12(r1) / / 17:

[0075] mtlr r3 / / 18: Restore lr

[0076] lwz r3,8(r1) / / 19: Restore r3

[0077] lwz r4,16(r1) / / 20: Restore r4

[0078] lwz r5,20(r1) / / 21: Restore r5

[0079] lwz r0,24(r1) / / 21: Restore r0

[0080] lwz r6,28(r1) / / 23: Restore r6

[0081] lwz r7,32(r1) / / 24: Restore r7

[0082] lwz r8,36(r1) / / 25: Restore r8

[0083] lwz r9,40(r1) / / 26: Restore r9

[0084] lwz r10,44(r1) / / 27: Restore r10

[0085] lwz r11,48(r1) / / 28: Restore r11

[0086] addi r1,r1,64 / / 29: Restore the stack

[0087] nop / / 30: Source instruction, this instruction needs to be modified later

[0088] bl / / 31: Jump to the next instruction after the original instruction and continue execution, this instruction needs to be modified later

[0089] Select the corresponding parameter-passing register according to the processor framework where the program is located. For example, r3-r10 are parameter-passing registers on the powerpc platform, r0-r3 are parameter-passing registers on the arm platform, and rdi, rsi, rdx, rcx, r8, r9 are parameter-passing registers on the X86_64 platform. The parameters are agreed upon in the order of the registers. For example, on powerpc, the first parameter is r3, the second is r4, and so on. Taking the powerpc platform as an example, if there are three incoming parameters, r3 is used to pass the base address, r4 is used to pass the index, and r5 is used to pass the offset. For immediate addressing instructions, the immediate value is assigned as the source and assigned to the r3 register, and the r4 and r5 registers are assigned 0.

[0090] For example, for the instruction stw r31,(0x1234) in the 32-bit powerpc cpu architecture, this instruction writes the value in the memory cell 0x1234 into the register r31. Then the input parameter of the memory detection function is 0x1234. The modification process is to assign 0x1234 to the base address parameter-passing register. The addi r3,r0,0 instruction is modified to li r3 0x1234; the addi r4,r0,0 instruction is modified to li r4,0; the addi r5,r0,0 modification instruction is modified to li r5,0. Li is an instruction for operating on immediate values, which stores the immediate value in a register, and addi is an instruction that takes the value of a register, adds it to an immediate number, and stores the result in a register.

[0091] In the first embodiment of the present invention, in order to adapt to the processor framework where the program is located, so as to pass the sum of the addresses of the base register and the index register in the register addressing instruction as an assignment to the corresponding parameter-passing register, in combination with the embodiments of the present invention, there is also a preferred implementation scheme. Specifically, as Figure 2 shown, if a memory access instruction in the program is encountered, jump to the jump instruction code segment template corresponding to the memory access instruction, and modify the instruction set in the jump instruction code segment generated by the jump instruction code segment template. The method includes:

[0092] If the memory access instruction is a register addressing instruction, perform step S204 to obtain the register corresponding to the processor framework, and use the memory access address existing in the register as the first memory access address.

[0093] Use the first memory access address as the source assignment and pass it to the parameter-passing register corresponding to the processor framework, and modify the parameter-passing instruction in the jump instruction code segment according to the first memory access instruction.

[0094] The three types of addressing modes include immediate addressing instructions, register addressing instructions, and base plus index addressing instructions, all of which can be calculated using the formula effective address = base + index + offset. Immediate addressing means the base is an immediate value, and both the index and offset are 0; register addressing means the base is a register, and both the index and offset are 0; base plus index addressing means the base is a register, the index is a register, and the offset is 0.

[0095] The parameter-passing register corresponding to the processor framework is used to calculate the memory access address and pass the modified memory access address to the memory detection function to detect whether there is a memory out-of-bounds in the system.

[0096] For example, in the 32-bit powerpc cpu architecture, the instruction stw r31,8(r14). The memory address stored in the register r14 in this instruction. The function of this instruction is to write the address value corresponding to 8 bytes of this memory address to the register r31. Then the input parameter of the memory detection function is the register r14. The modification process is to assign r14 to the base parameter-passing register, assign the offset of 8 bytes to the offset parameter-passing register. The addi r3,r0,0 instruction is modified to addi r3,r14,0; the addi r4,r0,0 instruction is modified to li r4,0; the li r5,0 instruction is modified to li r5,8.

[0097] In the first embodiment of the present invention, in order to adapt to the processor framework where the program is located, so as to pass the sum of the addresses of the base register and the index register in the base plus index addressing instruction as an assignment to the corresponding parameter-passing register. Combining with the embodiments of the present invention, there is also a preferred implementation scheme. Specifically, as Figure 2 shown, the memory access instruction is modified to the instruction set of the jump instruction code segment in the corresponding jump instruction code segment template.

[0098] If the memory access instruction is a base plus index addressing instruction, step S205 is performed to obtain the base register and the index register corresponding to the processor framework.

[0099] The sum of the addresses of the base register and the index register is used as the second memory access address, and the sum of the addresses of the base register and the index register is used as the source assignment to be passed to the parameter-passing register corresponding to the processor framework.

[0100] Modify the parameter-passing instruction in the jump instruction code segment according to the second memory access instruction.

[0101] For example, in the 32-bit PowerPC CPU architecture, the instruction "stw r31,r14,r15" means that the value in register r14 is used as a base memory address, added to the value in register r15 to obtain a memory address, and then the data in this memory address is written to register r31. So the input parameter of the memory detection function is the sum of the value in register r14 and the value in register r15. The modification process is to assign r14 to the base address passing register, assign r15 to the index passing register, modify the instruction "addi r3,r0,0" to "addi r3,r14,0"; modify "addi r4,r0,0" to "addi r4,r15,0"; and do not modify "li r5,0".

[0102] In the first embodiment of the present invention, in order to continue to execute the original instruction when there is no system memory out-of-bounds, in combination with the embodiments of the present invention, there is also a preferred implementation scheme. Specifically, as Figure 2 shown, after the parameter passing instruction in the jump instruction code segment is modified, step S206 is performed. At the end of the jump instruction code segment are the memory access instruction and the jump instruction that jumps to the next instruction of the memory access instruction.

[0103] In Figure 2 step S206, two instructions are required at the end of the jump instruction code segment, namely the memory access instruction and the jump instruction. The memory access instruction is for executing the original memory access, and the jump pointer is because the code jumps from the original code segment to the jump instruction code segment. If the jump instruction code segment is executed, it needs to jump back to the original code segment to continue execution. Since the memory access instruction has been executed, it needs to jump to the next instruction of the memory access instruction.

[0104] In the first embodiment of the present invention, in order to ensure that the program can continue to execute the original logic and function after the memory detection function is executed, in combination with the embodiments of the present invention, there is also a preferred implementation scheme. Specifically, the method further includes:

[0105] The jump instruction code segment saves the context of the memory access instruction. If the memory detection function determines that the address of the memory interval block does not belong to the memory interval address, the jump instruction code segment restores the context of the memory access instruction and continues to execute the memory access instruction.

[0106] The context of the memory access instruction refers to the register values corresponding to the memory access instruction. Saving the context of the memory access instruction means storing all the register values corresponding to the memory access instruction on the stack.

[0107] Storing the registers on the stack allows the registers stored on the stack to be restored to the corresponding registers after the memory detection function is executed, so that the function continues to execute without changing the original logic and function of the function.

[0108] In the jump code segment program, after the memory access instruction is replaced with a jump instruction, as Figure 3 shown, the steps are as follows:

[0109] S301: Save the context of the memory access instruction and go to S302.

[0110] The jump code segment mainly completes the following functions: saving the context of the memory access instruction and calling the memory detection function.

[0111] S302: Check whether the memory access address belongs to the interval address. If so, jump to S304; if not, jump to S303.

[0112] The memory detection function finds whether the currently accessed memory address is an interval address according to the memory interval storage algorithm.

[0113] S303: Execute the original instruction.

[0114] If the memory detection function checks that the access address does not belong to the interval address, it restores the saved context of the memory access instruction, executes the original memory instruction, and jumps to the next instruction of the original instruction to continue execution.

[0115] S304: The program terminates abnormally.

[0116] If it is an interval address, an exception is thrown and the program stops running.

[0117] Combined with the embodiments of the present invention, there is also a preferred implementation solution. Specifically, the storage jump instruction code segment searches for the address of the memory detection function, and at the same time generates a jump instruction code segment template and records the address of the memory interval. The specific method includes:

[0118] Allocate memory space on the stack of the jump instruction code segment, and push the register value corresponding to the memory access instruction onto the stack.

[0119] The stack pointer is offset by a preset length to apply for memory on the stack, allocate memory space on the stack, save the context, and push the current register value onto the stack. Save the address and length of the accessed memory into the parameter passing register.

[0120] Save the address and length corresponding to the memory access instruction in the parameter passing register corresponding to the memory access instruction.

[0121] The jump instruction segment template assembly instruction sequence. The jump instruction segment template is assembly code written in assembly language. The jump instruction code segment has five functions, including:

[0122] 1. Save the context and push the current register value onto the stack;

[0123] 2. Calculate the memory access address and save the address and length of the memory access to the parameter passing register;

[0124] 3. Call the memory detection function to perform memory attribute detection;

[0125] 4. Restore the context and restore the register values saved on the stack to the corresponding registers;

[0126] 5. Execute the original memory access instruction and jump to the next instruction after the address of the original memory access instruction.

[0127] In the first embodiment of the present invention, in combination with the embodiments of the present invention, there is also a preferred implementation. Specifically, when detecting that the system memory is out of bounds, modify the instruction set in the jump instruction code segment generated by the jump instruction code segment template, where the instruction set specifically includes: an input parameter instruction, retaining the memory access instruction, and an instruction for jumping to the address of the next instruction of the memory access instruction.

[0128] In the first embodiment of the present invention, in order to customize the enabling or disabling of the method for detecting system memory out-of-bounds, in combination with the embodiments of the present invention, there is also a preferred implementation. Specifically, when detecting that the system memory is out of bounds, if a memory access instruction in the program is encountered, jump to the jump instruction code segment template corresponding to the memory access instruction and modify the instruction set in the jump instruction code segment generated by the jump instruction code segment template. The method further includes:

[0129] When stopping the detection of system memory out-of-bounds, modify the jump instruction in the jump instruction code segment to the memory access instruction.

[0130] If the memory detection function is not required, modify the memory jump instruction to the original memory access instruction to turn off the function. It is also possible to preset the code range before activating the function. After the program starts, the start address and end address of the specified function can be obtained, and when analyzing the instructions, only analyze whether the instructions within this range are memory access instructions. The same applies to modules or dynamic libraries, and the corresponding start address and end address can be obtained.

[0131] Embodiment 2:

[0132] The second embodiment of the present invention provides a device for detecting system memory out-of-bounds, and the device includes:

[0133] At least one processor; and a memory communicatively connected to the at least one processor; wherein, the memory stores instructions executable by the at least one processor, and the instructions are configured by the program to execute any one of the methods for detecting system memory out-of-bounds in the first embodiment.

[0134] Such as Figure 4As shown, it is a schematic diagram of the device for detecting out-of-bounds memory in the embodiment of the present invention. The device for detecting out-of-bounds memory in this embodiment includes one or more processors 21 and a memory 22. Among them, Figure 4 Take one processor 21 as an example.

[0135] The processor 21 and the memory 22 can be connected through a bus or other means. Figure 4 Take the connection through the bus as an example.

[0136] The memory 22, as a non-volatile computer-readable storage medium, can be used to store non-volatile software programs and non-volatile computer-executable programs, such as the method for detecting out-of-bounds memory in the first embodiment. The processor 21 executes the method for detecting out-of-bounds memory by running the non-volatile software programs and instructions stored in the memory 22.

[0137] The memory 22 may include high-speed random access memory, and may also include non-volatile memory, such as at least one disk storage device, a flash memory device, or other non-volatile solid-state storage devices. In some embodiments, the memory 22 may optionally include a memory remotely set relative to the processor 21, and these remote memories can be connected to the processor 21 through a network. Examples of the above-mentioned network include but are not limited to the Internet, an enterprise intranet, a local area network, a mobile communication network, and combinations thereof.

[0138] The program instructions / modules are stored in the memory 22, and when executed by the one or more processors 21, they execute the method for detecting out-of-bounds memory in the first embodiment above. For example, they execute the Figures 1 to 3 respective steps shown above.

[0139] It should be noted that for the content such as information interaction and execution process between modules and units in the above-mentioned device and system, since it is based on the same concept as the method embodiment of the present invention, the specific content can be referred to the description in the method embodiment of the present invention, and will not be elaborated here.

[0140] Those of ordinary skill in the art can understand that all or part of the steps in the various methods of the embodiment can be completed by instructing relevant hardware through a program, and this program can be stored in a computer-readable storage medium. The storage medium may include: read-only memory (ROM, Read Only Memory), random access memory (RAM, Random Access Memory), a magnetic disk or an optical disc, etc.

[0141] Those skilled in the art can easily understand that the above are only the preferred embodiments of the present invention, and are not intended to limit the present invention. Any modifications, equivalent replacements, improvements, etc. made within the spirit and principle of the present invention shall be included within the protection scope of the present invention.

Claims

1. A method for detecting out-of-bounds access in system memory, characterized in that, The method includes: Apply for memory, search for the address of the memory detection function, and at the same time generate several jump instruction code segment templates and record the addresses of memory intervals; When detecting system memory out-of-bounds, if a memory access instruction in the program is encountered, jump to the jump instruction code segment template corresponding to the memory access instruction, and modify the instruction set in the jump instruction code segment generated by the jump instruction code segment template; wherein, when the program runs to the original memory access instruction, the original memory access instruction has been replaced by the instruction set in the jump instruction code segment; The modified jump instruction code segment calls the memory detection function, and the memory detection function determines whether the memory address to be accessed by the memory access instruction belongs to the memory interval address. If so, the memory is out-of-bounds; Wherein, when stopping the detection of system memory out-of-bounds, the jump instruction of the jump instruction code segment is modified to the memory access instruction.

2. The method for detecting out-of-bounds memory in the detection system according to claim 1, wherein The step of, if a memory access instruction in the program is encountered, jump to the jump instruction code segment template corresponding to the memory access instruction, and modify the instruction set in the jump instruction code segment generated by the jump instruction code segment template, the method includes: If the memory access instruction is an immediate addressing instruction, use the immediate number in the memory access instruction as the source assignment and pass it to the corresponding parameter passing register of the processor framework, and modify the parameter passing instruction in the jump instruction code segment according to the memory access instruction.

3. The method for detecting out-of-bounds memory of a detection system according to claim 1, characterized in that, The step of, if a memory access instruction in the program is encountered, jump to the jump instruction code segment template corresponding to the memory access instruction, and modify the instruction set in the jump instruction code segment generated by the jump instruction code segment template, the method further includes: If the memory access instruction is a register addressing instruction, obtain the register corresponding to the processor framework, and use the memory access address existing in the register as the first memory access address; Use the first memory access address as the source assignment and pass it to the corresponding parameter passing register of the processor framework, and modify the parameter passing instruction in the jump instruction code segment according to the first memory access address.

4. The method for detecting out-of-bounds memory in the detection system according to claim 1, characterized in that, The step of, if a memory access instruction in the program is encountered, jump to the jump instruction code segment template corresponding to the memory access instruction, and modify the instruction set in the jump instruction code segment generated by the jump instruction code segment template, the method further includes: If the memory access instruction is a base plus index addressing instruction, obtain the base register and index register corresponding to the processor framework; Use the sum of the addresses of the base register and the index register as the second memory access address, and use the sum of the addresses of the base register and the index register as the source assignment and pass it to the corresponding parameter passing register of the processor framework; Modify the parameter passing instruction in the jump instruction code segment according to the second memory access address.

5. The method for detecting out-of-bounds memory in the detection system according to claim 1, wherein At the end of the jump instruction code segment are the memory access instruction and the jump instruction that jumps to the next instruction of the memory access instruction.

6. The method for detecting out-of-bounds memory of the detection system according to claim 5, wherein, The method further includes: The jump instruction code segment saves the context of the memory access instruction. If the memory detection function determines that the memory address to be accessed does not belong to the memory interval address, the jump instruction code segment restores the context of the memory access instruction and continues to execute the memory access instruction.

7. The method for detecting out-of-bounds memory in the detection system according to claim 1, wherein, The method for applying for memory, searching for the address of the memory detection function, and generating several jump instruction code segment templates and recording the addresses of the memory intervals specifically includes: The jump instruction code segment allocates memory space on the stack and pushes the register values corresponding to the memory access instruction onto the stack; Save the address and length corresponding to the memory access instruction in the parameter passing register corresponding to the memory access instruction; The jump instruction code segment template assembly instruction sequence.

8. The method for detecting out-of-bounds memory in the detection system according to claim 1, wherein, Modify the instruction set in the jump instruction code segment generated by the jump instruction code segment template, where the instruction set specifically includes: a parameter passing instruction, the memory access instruction, and an instruction for jumping to the address of the next instruction of the memory access instruction.

9. A device for detecting out-of-bounds memory access in a system, characterized in that, The device includes: At least one processor; And a memory communicatively connected to the at least one processor; wherein, the memory stores instructions executable by the at least one processor, and the instructions are programmed to execute the method for detecting memory out-of-bounds of the detection system according to any one of claims 1-8.

Citation Information

Patent Citations

  • Memory border crossing detection method, device and equipment and storage medium

    CN111124921A